initial commit, lordsawar source, slightly modified
[lordsawar] / src / gui / diplomacy-dialog.cpp
1 //  Copyright (C) 2008, 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 "diplomacy-dialog.h"
24 #include "diplomacy-report-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 "File.h"
32 #include "GraphicsCache.h"
33 #include "playerlist.h"
34 #include "player.h"
35
36 DiplomacyDialog::DiplomacyDialog(Player *player)
37 {
38   GraphicsCache *gc = GraphicsCache::getInstance();
39   Playerlist *pl = Playerlist::getInstance();
40   d_player = player;
41   Glib::RefPtr<Gtk::Builder> xml
42     = Gtk::Builder::create_from_file(get_glade_path() + "/diplomacy-dialog.ui");
43
44   xml->get_widget("dialog", dialog);
45   decorate(dialog);
46   window_closed.connect(sigc::mem_fun(dialog, &Gtk::Dialog::hide));
47
48   xml->get_widget("proposals_table", d_proposals_table);
49   xml->get_widget("offers_table", d_offers_table);
50   xml->get_widget("player_label", d_player_label);
51   xml->get_widget("player_shield_image", d_player_shield_image);
52   xml->get_widget("report_button", d_report_button);
53     
54   d_report_button->signal_clicked().connect
55     (sigc::mem_fun(*this, &DiplomacyDialog::on_report_clicked));
56
57   // put the shields across the top of the proposals table, minus our own
58   guint32 i = 0;
59   guint32 j = 0;
60   for (unsigned int k = 0; k < MAX_PLAYERS; k++)
61     {
62       Player *p = pl->getPlayer(k);
63       if (p == NULL)
64         continue;
65       if (pl->getNeutral() == p)
66         continue;
67       if (p == d_player)
68         continue;
69       Glib::RefPtr<Gdk::Pixbuf> pixbuf= gc->getShieldPic(2, p)->to_pixbuf();
70       Gtk::Image *im = new Gtk::Image();
71       im->property_pixbuf() = pixbuf;
72       //im->set_padding(11, 0);
73       d_proposals_table->attach(*manage(im), i + 0, i + 1, 0, 1, 
74                                 Gtk::SHRINK, Gtk::SHRINK);
75       i++;
76     }
77   d_proposals_table->set_col_spacings (16);
78     
79   d_player_shield_image->property_pixbuf() = gc->getShieldPic(2, d_player)->to_pixbuf();
80
81   d_player_label->set_text(d_player->getName());
82
83   //fill in diplomatic state
84   i = 0;
85   j = 0;
86   for (unsigned int k = 0; k < MAX_PLAYERS; k++)
87     {
88       Player *p = pl->getPlayer(k);
89       if (p == NULL)
90         continue;
91       if (pl->getNeutral() == p)
92         continue;
93       if (p == d_player)
94         continue;
95       if (p->isDead())
96         {
97           i++;
98           continue;
99         }
100       j = 0;
101       Player::DiplomaticState state = d_player->getDiplomaticState (p);
102       Glib::RefPtr<Gdk::Pixbuf> pixbuf= gc->getDiplomacyPic(1, state)->to_pixbuf();
103       Gtk::Image *im = new Gtk::Image();
104       im->property_pixbuf() = pixbuf;
105       d_proposals_table->attach(*manage(im), i + 0, i + 1, j + 1, j + 2, 
106                                 Gtk::SHRINK, Gtk::SHRINK);
107       Player::DiplomaticProposal proposal = p->getDiplomaticProposal (d_player);
108       if (proposal != Player::NO_PROPOSAL)
109         {
110           j = 1;
111           Glib::RefPtr<Gdk::Pixbuf> pixbuf2;
112           switch (proposal)
113             {
114             case Player::PROPOSE_PEACE:
115               pixbuf2 = gc->getDiplomacyPic(1, Player::AT_PEACE)->to_pixbuf();
116               break;
117             case Player::PROPOSE_WAR_IN_FIELD:
118               pixbuf2 = gc->getDiplomacyPic(1, Player::AT_WAR_IN_FIELD)->to_pixbuf();
119               break;
120             case Player::PROPOSE_WAR:
121               pixbuf2 = gc->getDiplomacyPic(1, Player::AT_WAR)->to_pixbuf();
122               break;
123             default:
124               continue;
125             }
126
127           Gtk::Image *im2 = manage(new Gtk::Image());
128           im2->property_pixbuf() = pixbuf2;
129           d_proposals_table->attach(*manage(im2), i + 0, i + 1, j + 1, j + 2, 
130                                     Gtk::SHRINK, Gtk::SHRINK);
131         }
132       i++;
133     }
134
135
136   // fill in the togglebuttons
137   i = 0;
138   j = 0;
139   for (unsigned int k = 0; k < MAX_PLAYERS; k++)
140     {
141       Player *p = pl->getPlayer(k);
142       if (p == NULL)
143         continue;
144       if (pl->getNeutral() == p)
145         continue;
146       if (p == d_player)
147         continue;
148
149       //show the peace radio buttons
150       j = 0;
151       Gtk::RadioButton *radio1= manage(new Gtk::RadioButton);
152       Gtk::Image *im3 = new Gtk::Image();
153       im3->property_pixbuf() = 
154         gc->getDiplomacyPic(1, Player::AT_PEACE)->to_pixbuf();
155       radio1->add(*manage(im3));
156       radio1->set_mode(false);
157       Gtk::RadioButtonGroup group = radio1->get_group();
158       if (p->isDead())
159         radio1->set_sensitive(false);
160       else
161         radio1->set_active (d_player->getDiplomaticProposal(p) == 
162                             Player::PROPOSE_PEACE);
163       radio1->signal_toggled().connect(
164             sigc::bind(sigc::mem_fun
165                        (this, &DiplomacyDialog::on_proposal_toggled),
166                        radio1, p, Player::PROPOSE_PEACE));
167       d_offers_table->attach(*radio1, i, i + 1, j + 0, j + 1,
168                              Gtk::SHRINK, Gtk::SHRINK);
169
170       j = 1;
171       Gtk::RadioButton *radio2= manage(new Gtk::RadioButton);
172       Gtk::Image *im4 = new Gtk::Image();
173       im4->property_pixbuf() = 
174         gc->getDiplomacyPic(1, Player::AT_WAR_IN_FIELD)->to_pixbuf();
175       radio2->add(*manage(im4));
176       radio2->set_mode(false);
177       radio2->set_group(group);
178       if (p->isDead())
179         radio2->set_sensitive(false);
180       else
181         radio2->set_active (d_player->getDiplomaticProposal(p) == 
182                             Player::PROPOSE_WAR_IN_FIELD);
183       radio2->signal_toggled().connect(
184             sigc::bind(sigc::mem_fun(this, 
185                                      &DiplomacyDialog::on_proposal_toggled),
186                        radio2, p, Player::PROPOSE_WAR_IN_FIELD));
187       d_offers_table->attach(*radio2, i, i + 1, j + 0, j + 1,
188                              Gtk::SHRINK, Gtk::SHRINK);
189
190       j = 2;
191       Gtk::RadioButton *radio3= manage(new Gtk::RadioButton);
192       Gtk::Image *im5 = new Gtk::Image();
193       im5->property_pixbuf() = 
194         gc->getDiplomacyPic(1, Player::AT_WAR)->to_pixbuf();
195       radio3->add(*manage(im5));
196       radio3->set_mode(false);
197       radio3->set_group(group);
198       if (p->isDead())
199         radio3->set_sensitive(false);
200       else
201         radio3->set_active (d_player->getDiplomaticProposal(p) == 
202                             Player::PROPOSE_WAR);
203       radio3->signal_toggled().connect(
204             sigc::bind(sigc::mem_fun(this, 
205                                      &DiplomacyDialog::on_proposal_toggled),
206                        radio3, p, Player::PROPOSE_WAR));
207       d_offers_table->attach(*radio3, i, i + 1, j + 0, j + 1,
208                              Gtk::SHRINK, Gtk::SHRINK);
209       i++;
210     }
211 }
212
213 DiplomacyDialog::~DiplomacyDialog()
214 {
215   delete dialog;
216 }
217 void DiplomacyDialog::on_proposal_toggled (Gtk::ToggleButton *toggle, 
218                                            Player *player, 
219                                            Player::DiplomaticProposal proposal)
220 {
221   if (toggle->get_active() == true)
222     d_player->proposeDiplomacy (proposal, player);
223 }
224
225 void DiplomacyDialog::set_parent_window(Gtk::Window &parent)
226 {
227   dialog->set_transient_for(parent);
228   //dialog->set_position(Gtk::WIN_POS_CENTER_ON_PARENT);
229
230 }
231
232 void DiplomacyDialog::on_report_clicked()
233 {
234   DiplomacyReportDialog d(d_player);
235   d.set_parent_window(*dialog);
236   d.run();
237   d.hide();
238 }
239
240 void DiplomacyDialog::hide()
241 {
242   dialog->hide();
243 }
244 void DiplomacyDialog::run()
245 {
246   dialog->show_all();
247   dialog->run();
248 }
249