initial commit, lordsawar source, slightly modified
[lordsawar] / src / editor / hero-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
23 #include "hero-editor-dialog.h"
24
25 #include "glade-helpers.h"
26 #include "ucompose.hpp"
27 #include "defs.h"
28 #include "hero.h"
29 #include "backpack-editor-dialog.h"
30 #include "Backpack.h"
31
32 HeroEditorDialog::HeroEditorDialog(Hero *hero)
33 {
34   d_hero = hero;
35     
36     Glib::RefPtr<Gtk::Builder> xml
37         = Gtk::Builder::create_from_file(get_glade_path()
38                                     + "/hero-editor-dialog.ui");
39
40     xml->get_widget("dialog", dialog);
41     
42     xml->get_widget("edit_backpack_button", edit_backpack_button);
43     edit_backpack_button->signal_clicked().connect(
44         sigc::mem_fun(this, &HeroEditorDialog::on_edit_backpack_clicked));
45     xml->get_widget("male_radiobutton", male_radiobutton);
46     xml->get_widget("female_radiobutton", female_radiobutton);
47     xml->get_widget("name_entry", name_entry);
48     name_entry->set_text(d_hero->getName());
49     if (d_hero->getGender() == Hero::FEMALE)
50       female_radiobutton->set_active(true);
51     else
52       male_radiobutton->set_active(true);
53 }
54
55 HeroEditorDialog::~HeroEditorDialog()
56 {
57   delete dialog;
58 }
59 void HeroEditorDialog::set_parent_window(Gtk::Window &parent)
60 {
61     dialog->set_transient_for(parent);
62     //dialog->set_position(Gtk::WIN_POS_CENTER_ON_PARENT);
63 }
64
65 void HeroEditorDialog::run()
66 {
67   dialog->show_all();
68   Backpack *original_backpack = new Backpack(*d_hero->getBackpack());
69   int response = dialog->run();
70
71   if (response == Gtk::RESPONSE_ACCEPT) // accepted
72     {
73       d_hero->setName(name_entry->get_text());
74       if (male_radiobutton->get_active() == true)
75         d_hero->setGender(Hero::MALE);
76       else
77         d_hero->setGender(Hero::FEMALE);
78     }
79   else
80     {
81       d_hero->getBackpack()->removeAllFromBackpack();
82       d_hero->getBackpack()->add(original_backpack);
83     }
84 }
85
86 void HeroEditorDialog::on_edit_backpack_clicked()
87 {
88   BackpackEditorDialog d(d_hero->getBackpack());
89   d.run();
90   return;
91 }