initial commit, lordsawar source, slightly modified
[lordsawar] / src / gui / diplomacy-report-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-report-dialog.h"
24
25 #include "glade-helpers.h"
26 #include "image-helpers.h"
27 #include "input-helpers.h"
28 #include "ucompose.hpp"
29 #include "defs.h"
30 #include "File.h"
31 #include "GraphicsCache.h"
32 #include "playerlist.h"
33 #include "player.h"
34
35 DiplomacyReportDialog::DiplomacyReportDialog(Player *player)
36 {
37   GraphicsCache *gc = GraphicsCache::getInstance();
38   Playerlist *pl = Playerlist::getInstance();
39   d_player = player;
40   Glib::RefPtr<Gtk::Builder> xml
41     = Gtk::Builder::create_from_file(get_glade_path() + 
42                                 "/diplomacy-report-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("diplomacy_table", d_table);
49
50   int order[MAX_PLAYERS];
51
52   /* find the diplomatic order of the players */
53   for (guint32 i = 0; i < MAX_PLAYERS; i++)
54     {
55       order[i] = -1;
56       for (Playerlist::iterator it = pl->begin(); it != pl->end(); ++it)
57         {
58           if (pl->getNeutral() == *it)
59             continue;
60           if ((*it)->isDead() == true)
61             continue;
62           if (i != (*it)->getDiplomaticRank() - 1)
63             continue;
64           order[i] = (int) (*it)->getId();
65         }
66     }
67
68   /* show the players in order of their diplomatic ranking. */
69   for (guint32 i = 0; i < MAX_PLAYERS; i++)
70     {
71       if (order[i] == -1)
72         continue;
73       Player *p = pl->getPlayer(order[i]);
74
75       Glib::RefPtr<Gdk::Pixbuf> pix = gc->getShieldPic(2, p)->to_pixbuf();
76       Gtk::Image *im = manage(new Gtk::Image());
77       im->property_pixbuf() = pix;
78       d_table->attach(*im, 1, 2, i + 1, i + 2, Gtk::SHRINK, Gtk::SHRINK);
79       Gtk::Image *im2 = manage(new Gtk::Image());
80       im2->property_pixbuf() = pix;
81       d_table->attach(*im2, i + 2, i + 3, 0, 0 + 1, Gtk::SHRINK, Gtk::SHRINK);
82       Gtk::Label *label = manage(new Gtk::Label(p->getDiplomaticTitle()));
83       d_table->attach(*label, 0, 1, i + 1, i + 2, Gtk::SHRINK, Gtk::SHRINK);
84   
85       for (guint32 j = 0; j < MAX_PLAYERS; j++)
86         {
87           if (order[j] == -1)
88             continue;
89           Player::DiplomaticState state;
90           state = p->getDiplomaticState(pl->getPlayer(order[j]));
91           Glib::RefPtr<Gdk::Pixbuf> pix2 = gc->getDiplomacyPic(0, state)->to_pixbuf();
92           Gtk::Image *im3 = manage(new Gtk::Image());
93           im3->property_pixbuf() = pix2;
94           d_table->attach(*im3, i + 2, i + 3, j + 1, j + 2, 
95                           Gtk::SHRINK, Gtk::SHRINK);
96         }
97     }
98 }
99
100 DiplomacyReportDialog::~DiplomacyReportDialog()
101 {
102   delete dialog;
103 }
104 void DiplomacyReportDialog::set_parent_window(Gtk::Window &parent)
105 {
106   dialog->set_transient_for(parent);
107   //dialog->set_position(Gtk::WIN_POS_CENTER_ON_PARENT);
108 }
109
110 void DiplomacyReportDialog::hide()
111 {
112   dialog->hide();
113 }
114
115 void DiplomacyReportDialog::run()
116 {
117   dialog->show_all();
118   dialog->run();
119 }
120