1 // Copyright (C) 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
21 #include <sigc++/functors/mem_fun.h>
24 #include "backpack-editor-dialog.h"
26 #include "glade-helpers.h"
27 #include "ucompose.hpp"
30 #include "ItemProto.h"
32 #include "select-item-dialog.h"
34 BackpackEditorDialog::BackpackEditorDialog(Backpack *pack)
37 working = new Backpack(*pack);
39 Glib::RefPtr<Gtk::Builder> xml
40 = Gtk::Builder::create_from_file(get_glade_path()
41 + "/backpack-editor-dialog.ui");
43 xml->get_widget("dialog", dialog);
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));
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);
60 item_treeview->get_selection()->signal_changed()
61 .connect(sigc::mem_fun
62 (this, &BackpackEditorDialog::on_item_selection_changed));
66 BackpackEditorDialog::~BackpackEditorDialog()
72 void BackpackEditorDialog::set_parent_window(Gtk::Window &parent)
74 dialog->set_transient_for(parent);
75 //dialog->set_position(Gtk::WIN_POS_CENTER_ON_PARENT);
78 void BackpackEditorDialog::hide()
83 int BackpackEditorDialog::run()
87 on_item_selection_changed();
88 int response = dialog->run();
89 if (response == Gtk::RESPONSE_ACCEPT) // accepted
91 backpack->removeAllFromBackpack();
92 backpack->add(working);
97 void BackpackEditorDialog::on_item_selection_changed()
99 Gtk::TreeIter i = item_treeview->get_selection()->get_selected();
101 remove_button->set_sensitive(true);
103 remove_button->set_sensitive(false);
106 void BackpackEditorDialog::on_remove_item_clicked()
108 Gtk::TreeIter i = item_treeview->get_selection()->get_selected();
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();
118 void BackpackEditorDialog::on_add_item_clicked()
122 const ItemProto *itemproto = d.get_selected_item();
125 Item *item = new Item(*itemproto);
126 working->addToBackpack(item);
128 on_item_selection_changed();
133 void BackpackEditorDialog::add_item(Item *item)
135 Gtk::TreeIter i = item_list->append();
136 (*i)[item_columns.name] = item->getName();
138 (*i)[item_columns.attributes] = item->getBonusDescription();
140 (*i)[item_columns.item] = item;
144 void BackpackEditorDialog::fill_bag()
147 // populate the item list
149 for (Backpack::iterator i = working->begin(); i != working->end(); ++i)