1 //  Copyright (C) 2011, 2012, 2014, 2015, 2017 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 "use-item-on-city-dialog.h"
24 
25 #include "input-helpers.h"
26 #include "ucompose.hpp"
27 #include "ImageCache.h"
28 #include "city.h"
29 #include "playerlist.h"
30 
31 #define method(x) sigc::mem_fun(*this, &UseItemOnCityDialog::x)
32 
UseItemOnCityDialog(Gtk::Window & parent,SelectCityMap::Type type)33 UseItemOnCityDialog::UseItemOnCityDialog(Gtk::Window &parent, SelectCityMap::Type type)
34  : LwDialog(parent, "use-item-on-city-dialog.ui")
35 {
36   xml->get_widget("map_image", map_image);
37   xml->get_widget("continue_button", continue_button);
38 
39   citymap = new SelectCityMap(type);
40   citymap->map_changed.connect (method(on_map_changed));
41   citymap->city_selected.connect(sigc::hide(method(on_city_selected)));
42 
43   Gtk::EventBox *map_eventbox;
44   xml->get_widget("map_eventbox", map_eventbox);
45   map_eventbox->add_events(Gdk::BUTTON_PRESS_MASK);
46   map_eventbox->signal_button_press_event().connect
47     (method(on_map_mouse_button_event));
48 
49   continue_button->set_sensitive(false);
50 
51   xml->get_widget("label", label);
52   switch (type)
53     {
54     case SelectCityMap::ANY_CITY:
55       label->set_text(_("Select a city to target."));
56       break;
57     case SelectCityMap::FRIENDLY_CITY:
58       label->set_text(_("Select one of your cities to target."));
59       break;
60     case SelectCityMap::ENEMY_CITY:
61       label->set_text(_("Select an enemy city to target."));
62       break;
63     case SelectCityMap::NEUTRAL_CITY:
64       label->set_text(_("Select a neutral city to target."));
65       break;
66     }
67 }
68 
run()69 City* UseItemOnCityDialog::run()
70 {
71   citymap->resize();
72   citymap->draw();
73   dialog->show_all();
74   dialog->run();
75   return citymap->get_selected_city();
76 }
77 
on_map_changed(Cairo::RefPtr<Cairo::Surface> map)78 void UseItemOnCityDialog::on_map_changed(Cairo::RefPtr<Cairo::Surface> map)
79 {
80   map_image->property_pixbuf() =
81     Gdk::Pixbuf::create(map, 0, 0, citymap->get_width(), citymap->get_height());
82 }
83 
on_city_selected()84 void UseItemOnCityDialog::on_city_selected()
85 {
86   continue_button->set_sensitive(true);
87 }
88 
on_map_mouse_button_event(GdkEventButton * e)89 bool UseItemOnCityDialog::on_map_mouse_button_event(GdkEventButton *e)
90 {
91   if (e->type != GDK_BUTTON_PRESS)
92     return true;	// useless event
93   citymap->mouse_button_event(to_input_event(e));
94   return true;
95 }
96 
97