initial commit, lordsawar source, slightly modified
[lordsawar] / src / editor / rewardlist-dialog.cpp
1 //  Copyright (C) 2008, 2009 Ben Asselstine
2 //
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.
7 //
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.
12 //
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 
16 //  02110-1301, USA.
17
18 #include <config.h>
19
20 #include <iostream>
21 #include <iomanip>
22 #include <assert.h>
23 #include <libgen.h>
24
25 #include <sigc++/functors/mem_fun.h>
26 #include <sigc++/functors/ptr_fun.h>
27
28 #include <gtkmm.h>
29 #include "rewardlist-dialog.h"
30
31 #include "gui/input-helpers.h"
32 #include "gui/error-utils.h"
33
34 #include "defs.h"
35 #include "Configuration.h"
36 #include "rewardlist.h"
37
38 #include "ucompose.hpp"
39 #include "playerlist.h"
40
41 #include "glade-helpers.h"
42 #include "reward-editor-dialog.h"
43
44
45 RewardlistDialog::RewardlistDialog()
46 {
47     Glib::RefPtr<Gtk::Builder> xml
48         = Gtk::Builder::create_from_file(get_glade_path() + 
49                                     "/reward-list-dialog.ui");
50
51     xml->get_widget("dialog", dialog);
52
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));
63
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);
68
69     Rewardlist *rewardlist = Rewardlist::getInstance();
70     Rewardlist::iterator iter = rewardlist->begin();
71     for (;iter != rewardlist->end(); iter++)
72       addReward(*iter);
73       
74     guint32 max = rewardlist->size();
75     if (max)
76       {
77         Gtk::TreeModel::Row row;
78         row = rewards_treeview->get_model()->children()[0];
79         if(row)
80           rewards_treeview->get_selection()->select(row);
81       }
82
83
84     update_rewardlist_buttons();
85     rewards_treeview->get_selection()->signal_changed().connect
86       (sigc::mem_fun(*this, &RewardlistDialog::on_reward_selected));
87 }
88
89 void
90 RewardlistDialog::update_rewardlist_buttons()
91 {
92   if (!rewards_treeview->get_selection()->get_selected())
93     {
94       remove_button->set_sensitive(false);
95       edit_button->set_sensitive(false);
96     }
97   else
98     {
99       remove_button->set_sensitive(true);
100       edit_button->set_sensitive(true);
101     }
102 }
103
104 RewardlistDialog::~RewardlistDialog()
105 {
106   delete dialog;
107 }
108
109 void RewardlistDialog::addReward(Reward *reward)
110 {
111   Gtk::TreeIter i = rewards_list->append();
112   (*i)[rewards_columns.name] = reward->getName();
113   (*i)[rewards_columns.reward] = reward;
114 }
115
116 void RewardlistDialog::on_reward_selected()
117 {
118   update_rewardlist_buttons();
119 }
120
121 void RewardlistDialog::on_add_clicked()
122 {
123   Player *neutral = Playerlist::getInstance()->getNeutral();
124   RewardEditorDialog d(neutral, true, NULL);
125   d.run();
126   if (d.get_reward())
127     {
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);
133     }
134
135 }
136
137 void RewardlistDialog::on_remove_clicked()
138 {
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();
143
144   if (iterrow) 
145     {
146       Gtk::TreeModel::Row row = *iterrow;
147       Reward *a = row[rewards_columns.reward];
148       rewards_list->erase(iterrow);
149       Rewardlist::getInstance()->remove(a);
150     }
151 }
152
153 void RewardlistDialog::on_edit_clicked()
154 {
155   Glib::RefPtr<Gtk::TreeSelection> selection = 
156     rewards_treeview->get_selection();
157   Gtk::TreeModel::iterator iterrow = selection->get_selected();
158
159   if (iterrow) 
160     {
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);
165       d.run();
166       if (d.get_reward())
167         {
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);
173         }
174       else
175         {
176           rewards_list->erase(iterrow);
177           Rewardlist::getInstance()->remove(reward);
178         }
179     }
180
181 }
182
183 void RewardlistDialog::set_parent_window(Gtk::Window &parent)
184 {
185     dialog->set_transient_for(parent);
186     //dialog->set_position(Gtk::WIN_POS_CENTER_ON_PARENT);
187 }
188
189 int RewardlistDialog::run()
190 {
191     dialog->show_all();
192     return dialog->run();
193 }
194