1 // Copyright (C) 2007 Ole Laursen
2 // Copyright (C) 2007, 2008, 2009, 2011, 2012, 2014, 2015, 2017,
3 // 2020 Ben Asselstine
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Library General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301, USA.
19
20 #include <config.h>
21
22 #include <gtkmm.h>
23 #include <sigc++/functors/mem_fun.h>
24
25 #include "hero-offer-dialog.h"
26
27 #include "ucompose.hpp"
28 #include "defs.h"
29 #include "GameMap.h"
30 #include "File.h"
31 #include "snd.h"
32 #include "city.h"
33 #include "playerlist.h"
34 #include "ImageCache.h"
35 #include "font-size.h"
36
37 #define method(x) sigc::mem_fun(*this, &HeroOfferDialog::x)
38
HeroOfferDialog(Gtk::Window & parent,Player * player,HeroProto * h,City * c,int gold)39 HeroOfferDialog::HeroOfferDialog(Gtk::Window &parent, Player *player, HeroProto *h, City *c, int gold)
40 : LwDialog(parent, "hero-offer-dialog.ui")
41 {
42 city = c;
43 hero = h;
44
45 xml->get_widget("map_image", map_image);
46
47 heromap = new HeroMap(city);
48 heromap->map_changed.connect(method(on_map_changed));
49
50 dialog->set_title(String::ucompose(_("A Hero for %1"), player->getName()));
51
52 xml->get_widget("hero_image", hero_image);
53 xml->get_widget("hero_male", male_radiobutton);
54 xml->get_widget("hero_female", female_radiobutton);
55 male_radiobutton->set_active(hero->getGender() == Hero::MALE);
56 female_radiobutton->set_active(hero->getGender() == Hero::FEMALE);
57 male_radiobutton->signal_clicked().connect(method(on_toggled));
58
59 on_toggled();
60
61 xml->get_widget("name", name_entry);
62 name_entry->set_text(hero->getName());
63 name_entry->signal_changed().connect (method(on_name_changed));
64
65 xml->get_widget("accept_button", accept_button);
66 Gtk::Label *label;
67 xml->get_widget("label", label);
68
69 Glib::ustring s;
70 if (gold > 0)
71 s = String::ucompose(
72 ngettext("A hero in %2 wants to join you for %1 gold piece!",
73 "A hero in %2 wants to join you for %1 gold pieces!",
74 gold), gold, city->getName());
75 else
76 s = String::ucompose(_("A hero in %1 wants to join you!"), city->getName());
77 label->set_text(s);
78 update_buttons();
79 }
80
update_buttons()81 void HeroOfferDialog::update_buttons()
82 {
83 if (String::utrim(name_entry->get_text()) == "")
84 accept_button->set_sensitive(false);
85 else
86 {
87 accept_button->set_sensitive(true);
88 accept_button->property_can_focus() = true;
89 accept_button->property_can_default() = true;
90 accept_button->property_has_default() = true;
91 name_entry->property_activates_default() = true;
92 accept_button->property_receives_default() = true;
93 }
94 }
95
on_name_changed()96 void HeroOfferDialog::on_name_changed()
97 {
98 update_buttons();
99 }
100
on_toggled()101 void HeroOfferDialog::on_toggled()
102 {
103 if (male_radiobutton->get_active())
104 hero_image->property_pixbuf() =
105 ImageCache::getInstance()->getDialogPic
106 (ImageCache::DIALOG_NEW_HERO_MALE,
107 FontSize::getInstance ()->get_height ())->to_pixbuf();
108 else
109 hero_image->property_pixbuf() =
110 ImageCache::getInstance()->getDialogPic
111 (ImageCache::DIALOG_NEW_HERO_FEMALE,
112 FontSize::getInstance ()->get_height ())->to_pixbuf();
113 }
114
run()115 bool HeroOfferDialog::run()
116 {
117 heromap->resize();
118 heromap->draw();
119
120 Snd::getInstance()->play("hero", 1);
121 dialog->show_all();
122 int response = dialog->run();
123 Snd::getInstance()->halt();
124
125 if (response == Gtk::RESPONSE_ACCEPT) // accepted
126 {
127 hero->setName(String::utrim(name_entry->get_text()));
128 if (male_radiobutton->get_active() == true &&
129 hero->getGender() == Hero::FEMALE)
130 hero->setGender(Hero::MALE);
131 else if (male_radiobutton->get_active() == false &&
132 hero->getGender() == Hero::MALE)
133 hero->setGender(Hero::FEMALE);
134 return true;
135 }
136 else
137 return false;
138 }
139
on_map_changed(Cairo::RefPtr<Cairo::Surface> map)140 void HeroOfferDialog::on_map_changed(Cairo::RefPtr<Cairo::Surface> map)
141 {
142 map_image->property_pixbuf() =
143 Gdk::Pixbuf::create(map, 0, 0, heromap->get_width(), heromap->get_height());
144 }
145
146