initial commit, lordsawar source, slightly modified
[lordsawar] / src / gui / hero-offer-dialog.cpp
1 //  Copyright (C) 2007 Ole Laursen
2 //  Copyright (C) 2007, 2008, 2009 Ben Asselstine
3 //
4 //  This program is free software; you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation; either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU Library General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program; if not, write to the Free Software
16 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
17 //  02110-1301, USA.
18
19 #include <config.h>
20
21 #include <gtkmm.h>
22 #include <sigc++/functors/mem_fun.h>
23
24 #include "hero-offer-dialog.h"
25
26 #include "glade-helpers.h"
27 #include "image-helpers.h"
28 #include "input-helpers.h"
29 #include "ucompose.hpp"
30 #include "defs.h"
31 #include "GameMap.h"
32 #include "File.h"
33 #include "sound.h"
34 #include "city.h"
35
36 HeroOfferDialog::HeroOfferDialog(Player *player, HeroProto *h, City *c, int gold)
37 {
38     city = c;
39     hero = h;
40     
41     Glib::RefPtr<Gtk::Builder> xml
42         = Gtk::Builder::create_from_file(get_glade_path()
43                                     + "/hero-offer-dialog.ui");
44
45     xml->get_widget("dialog", dialog);
46
47     decorate(dialog);
48     window_closed.connect(sigc::mem_fun(dialog, &Gtk::Dialog::hide));
49
50     xml->get_widget("map_image", map_image);
51
52     heromap = new HeroMap(city);
53     heromap->map_changed.connect(
54         sigc::mem_fun(this, &HeroOfferDialog::on_map_changed));
55
56     set_title(String::ucompose(_("Hero offer for %1"),
57                                        player->getName()));
58
59     xml->get_widget("hero_image", hero_image);
60     xml->get_widget("hero_male", male_radiobutton);
61     male_radiobutton->signal_clicked().connect(
62         sigc::mem_fun(this, &HeroOfferDialog::on_male_toggled));
63     
64     male_radiobutton->set_active(hero->getGender() == Hero::MALE);
65     on_male_toggled();
66     
67     xml->get_widget("name", name_entry);
68     name_entry->set_text(hero->getName());
69
70     Gtk::Label *label;
71     xml->get_widget("label", label);
72     
73     Glib::ustring s;
74     if (gold > 0)
75         s = String::ucompose(
76             ngettext("A hero in %2 wants to join you for %1 gold piece!",
77                      "A hero in %2 wants to join you for %1 gold pieces!",
78                      gold), gold, city->getName());
79     else
80         s = String::ucompose(_("A hero in %1 wants to join you!"), city->getName());
81     label->set_text(s);
82 }
83
84 HeroOfferDialog::~HeroOfferDialog()
85 {
86   delete heromap;
87   delete dialog;
88 }
89 void HeroOfferDialog::on_male_toggled()
90 {
91     if (male_radiobutton->get_active())
92         hero_image->property_file()
93             = File::getMiscFile("various/recruit_male.png");
94     else
95         hero_image->property_file()
96             = File::getMiscFile("various/recruit_female.png");
97 }
98
99 void HeroOfferDialog::set_parent_window(Gtk::Window &parent)
100 {
101     dialog->set_transient_for(parent);
102     //dialog->set_position(Gtk::WIN_POS_CENTER_ON_PARENT);
103 }
104
105 void HeroOfferDialog::hide()
106 {
107   dialog->hide();
108 }
109
110 bool HeroOfferDialog::run()
111 {
112     heromap->resize();
113     heromap->draw(Playerlist::getActiveplayer());
114
115     Sound::getInstance()->playMusic("hero", 1);
116     dialog->show_all();
117     int response = dialog->run();
118     Sound::getInstance()->haltMusic();
119
120     if (response == Gtk::RESPONSE_ACCEPT)       // accepted
121       {
122         hero->setName(name_entry->get_text());
123         if (male_radiobutton->get_active() == true && 
124             hero->getGender() == Hero::FEMALE)
125           hero->setGender(Hero::MALE);
126         else if (male_radiobutton->get_active() == false &&
127                  hero->getGender() == Hero::MALE)
128           hero->setGender(Hero::FEMALE);
129         return true;
130       }
131     else
132         return false;
133 }
134
135 void HeroOfferDialog::on_map_changed(Glib::RefPtr<Gdk::Pixmap> map)
136 {
137     map_image->property_pixmap() = map;
138 }
139