1 // Copyright (C) 2008, 2009 Ben Asselstine
3 // This program is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation; either version 3 of the License, or
6 // (at your option) any later version.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU Library General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 #include <sigc++/functors/mem_fun.h>
26 #include <sigc++/functors/ptr_fun.h>
29 #include "rewardlist-dialog.h"
31 #include "gui/input-helpers.h"
32 #include "gui/error-utils.h"
35 #include "Configuration.h"
36 #include "rewardlist.h"
38 #include "ucompose.hpp"
39 #include "playerlist.h"
41 #include "glade-helpers.h"
42 #include "reward-editor-dialog.h"
45 RewardlistDialog::RewardlistDialog()
47 Glib::RefPtr<Gtk::Builder> xml
48 = Gtk::Builder::create_from_file(get_glade_path() +
49 "/reward-list-dialog.ui");
51 xml->get_widget("dialog", dialog);
53 xml->get_widget("rewards_treeview", rewards_treeview);
54 xml->get_widget("add_button", add_button);
55 add_button->signal_clicked().connect
56 (sigc::mem_fun(this, &RewardlistDialog::on_add_clicked));
57 xml->get_widget("remove_button", remove_button);
58 remove_button->signal_clicked().connect
59 (sigc::mem_fun(this, &RewardlistDialog::on_remove_clicked));
60 xml->get_widget("edit_button", edit_button);
61 edit_button->signal_clicked().connect
62 (sigc::mem_fun(this, &RewardlistDialog::on_edit_clicked));
64 rewards_list = Gtk::ListStore::create(rewards_columns);
65 rewards_treeview->set_model(rewards_list);
66 rewards_treeview->append_column("", rewards_columns.name);
67 rewards_treeview->set_headers_visible(false);
69 Rewardlist *rewardlist = Rewardlist::getInstance();
70 Rewardlist::iterator iter = rewardlist->begin();
71 for (;iter != rewardlist->end(); iter++)
74 guint32 max = rewardlist->size();
77 Gtk::TreeModel::Row row;
78 row = rewards_treeview->get_model()->children()[0];
80 rewards_treeview->get_selection()->select(row);
84 update_rewardlist_buttons();
85 rewards_treeview->get_selection()->signal_changed().connect
86 (sigc::mem_fun(*this, &RewardlistDialog::on_reward_selected));
90 RewardlistDialog::update_rewardlist_buttons()
92 if (!rewards_treeview->get_selection()->get_selected())
94 remove_button->set_sensitive(false);
95 edit_button->set_sensitive(false);
99 remove_button->set_sensitive(true);
100 edit_button->set_sensitive(true);
104 RewardlistDialog::~RewardlistDialog()
109 void RewardlistDialog::addReward(Reward *reward)
111 Gtk::TreeIter i = rewards_list->append();
112 (*i)[rewards_columns.name] = reward->getName();
113 (*i)[rewards_columns.reward] = reward;
116 void RewardlistDialog::on_reward_selected()
118 update_rewardlist_buttons();
121 void RewardlistDialog::on_add_clicked()
123 Player *neutral = Playerlist::getInstance()->getNeutral();
124 RewardEditorDialog d(neutral, true, NULL);
128 Reward *reward = d.get_reward();
129 Gtk::TreeIter i = rewards_list->append();
130 (*i)[rewards_columns.name] = reward->getName();
131 (*i)[rewards_columns.reward] = reward;
132 Rewardlist::getInstance()->push_back(reward);
137 void RewardlistDialog::on_remove_clicked()
139 //erase the selected row from the treeview
140 //remove the reward from the rewardlist
141 Glib::RefPtr<Gtk::TreeSelection> selection = rewards_treeview->get_selection();
142 Gtk::TreeModel::iterator iterrow = selection->get_selected();
146 Gtk::TreeModel::Row row = *iterrow;
147 Reward *a = row[rewards_columns.reward];
148 rewards_list->erase(iterrow);
149 Rewardlist::getInstance()->remove(a);
153 void RewardlistDialog::on_edit_clicked()
155 Glib::RefPtr<Gtk::TreeSelection> selection =
156 rewards_treeview->get_selection();
157 Gtk::TreeModel::iterator iterrow = selection->get_selected();
161 Gtk::TreeModel::Row row = *iterrow;
162 Reward *reward = row[rewards_columns.reward];
163 Player *neutral = Playerlist::getInstance()->getNeutral();
164 RewardEditorDialog d(neutral, true, reward);
168 Rewardlist::getInstance()->remove(reward);
169 reward = d.get_reward();
170 (*iterrow)[rewards_columns.name] = reward->getName();
171 (*iterrow)[rewards_columns.reward] = reward;
172 Rewardlist::getInstance()->push_back(reward);
176 rewards_list->erase(iterrow);
177 Rewardlist::getInstance()->remove(reward);
183 void RewardlistDialog::set_parent_window(Gtk::Window &parent)
185 dialog->set_transient_for(parent);
186 //dialog->set_position(Gtk::WIN_POS_CENTER_ON_PARENT);
189 int RewardlistDialog::run()
192 return dialog->run();