initial commit, lordsawar source, slightly modified
[lordsawar] / src / editor / backpack-editor-dialog.cpp
1 //  Copyright (C) 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 <gtkmm.h>
21 #include <sigc++/functors/mem_fun.h>
22 #include <vector>
23
24 #include "backpack-editor-dialog.h"
25
26 #include "glade-helpers.h"
27 #include "ucompose.hpp"
28 #include "defs.h"
29 #include "Item.h"
30 #include "ItemProto.h"
31 #include "Backpack.h"
32 #include "select-item-dialog.h"
33
34 BackpackEditorDialog::BackpackEditorDialog(Backpack *pack)
35 {
36   backpack = pack;
37   working = new Backpack(*pack);
38     
39   Glib::RefPtr<Gtk::Builder> xml
40     = Gtk::Builder::create_from_file(get_glade_path()
41                                      + "/backpack-editor-dialog.ui");
42
43     xml->get_widget("dialog", dialog);
44
45     xml->get_widget("remove_button", remove_button);
46     xml->get_widget("add_button", add_button);
47     remove_button->signal_clicked()
48         .connect(sigc::mem_fun(this, 
49                                &BackpackEditorDialog::on_remove_item_clicked));
50     add_button->signal_clicked()
51         .connect(sigc::mem_fun(this, 
52                                &BackpackEditorDialog::on_add_item_clicked));
53     
54     item_list = Gtk::ListStore::create(item_columns);
55     xml->get_widget("treeview", item_treeview);
56     item_treeview->set_model(item_list);
57     item_treeview->append_column(_("Name"), item_columns.name);
58     item_treeview->append_column(_("Attributes"), item_columns.attributes);
59
60     item_treeview->get_selection()->signal_changed()
61         .connect(sigc::mem_fun
62                  (this, &BackpackEditorDialog::on_item_selection_changed));
63
64 }
65
66 BackpackEditorDialog::~BackpackEditorDialog()
67 {
68   delete working;
69   delete dialog;
70 }
71
72 void BackpackEditorDialog::set_parent_window(Gtk::Window &parent)
73 {
74     dialog->set_transient_for(parent);
75     //dialog->set_position(Gtk::WIN_POS_CENTER_ON_PARENT);
76 }
77
78 void BackpackEditorDialog::hide()
79 {
80   dialog->hide();
81 }
82
83 int BackpackEditorDialog::run()
84 {
85   dialog->show_all();
86   fill_bag();
87   on_item_selection_changed();
88   int response = dialog->run();
89   if (response == Gtk::RESPONSE_ACCEPT) // accepted
90     {
91       backpack->removeAllFromBackpack();
92       backpack->add(working);
93     }
94   return response;
95 }
96
97 void BackpackEditorDialog::on_item_selection_changed()
98 {
99     Gtk::TreeIter i = item_treeview->get_selection()->get_selected();
100     if (i)
101       remove_button->set_sensitive(true);
102     else
103       remove_button->set_sensitive(false);
104 }
105
106 void BackpackEditorDialog::on_remove_item_clicked()
107 {
108     Gtk::TreeIter i = item_treeview->get_selection()->get_selected();
109     if (i)
110     {
111         Item *item = (*i)[item_columns.item];
112         working->removeFromBackpack(item);
113         item_list->erase(item_treeview->get_selection()->get_selected());
114         on_item_selection_changed();
115     }
116 }
117
118 void BackpackEditorDialog::on_add_item_clicked()
119 {
120   SelectItemDialog d;
121   d.run();
122   const ItemProto *itemproto = d.get_selected_item();
123   if (itemproto)
124     {
125       Item *item = new Item(*itemproto);
126       working->addToBackpack(item);
127       add_item(item);
128       on_item_selection_changed();
129     }
130 }
131
132
133 void BackpackEditorDialog::add_item(Item *item)
134 {
135     Gtk::TreeIter i = item_list->append();
136     (*i)[item_columns.name] = item->getName();
137
138     (*i)[item_columns.attributes] = item->getBonusDescription();
139     
140     (*i)[item_columns.item] = item;
141 }
142
143
144 void BackpackEditorDialog::fill_bag()
145 {
146         
147     // populate the item list
148     item_list->clear();
149     for (Backpack::iterator i = working->begin(); i != working->end(); ++i)
150         add_item(*i);
151
152   return;
153 }