initial commit, lordsawar source, slightly modified
[lordsawar] / src / gui / army-bonus-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 "army-bonus-dialog.h"
25
26 #include "glade-helpers.h"
27 #include "image-helpers.h"
28 #include "ucompose.hpp"
29 #include "defs.h"
30 #include "army.h"
31 #include "armysetlist.h"
32 #include "player.h"
33 #include "GraphicsCache.h"
34 #include "File.h"
35
36 ArmyBonusDialog::ArmyBonusDialog(Player *p)
37 {
38     d_player = p;
39     Glib::RefPtr<Gtk::Builder> xml
40         = Gtk::Builder::create_from_file(get_glade_path()
41                                     + "/army-bonus-dialog.ui");
42
43     xml->get_widget("dialog", dialog);
44     decorate(dialog);
45     window_closed.connect(sigc::mem_fun(dialog, &Gtk::Dialog::hide));
46     dialog->set_icon_from_file(File::getMiscFile("various/castle_icon.png"));
47
48     armies_list = Gtk::ListStore::create(armies_columns);
49     xml->get_widget("treeview", armies_treeview);
50     armies_treeview->set_model(armies_list);
51     armies_treeview->append_column("", armies_columns.image);
52     armies_treeview->append_column("", armies_columns.name);
53     armies_treeview->append_column("Str", armies_columns.str);
54     armies_treeview->append_column("Move", armies_columns.move);
55     armies_treeview->append_column("", armies_columns.move_image);
56     armies_treeview->append_column("Bonus", armies_columns.bonus);
57     armies_treeview->set_headers_visible(true);
58
59     guint32 max = Armysetlist::getInstance()->getSize(d_player->getArmyset());
60     for (unsigned int i = 0; i < max; i++)
61       addArmyType(i);
62 }
63
64 ArmyBonusDialog::~ArmyBonusDialog()
65 {
66   delete dialog;
67 }
68 void ArmyBonusDialog::set_parent_window(Gtk::Window &parent)
69 {
70     dialog->set_transient_for(parent);
71     //dialog->set_position(Gtk::WIN_POS_CENTER_ON_PARENT);
72 }
73
74 void ArmyBonusDialog::hide()
75 {
76   dialog->hide();
77 }
78
79 void ArmyBonusDialog::run()
80 {
81     static int width = -1;
82     static int height = -1;
83
84     if (width != -1 && height != -1)
85         dialog->set_default_size(width, height);
86     
87     dialog->show();
88     dialog->run();
89
90     dialog->get_size(width, height);
91
92 }
93
94 void ArmyBonusDialog::addArmyType(guint32 army_type)
95 {
96     GraphicsCache *gc = GraphicsCache::getInstance();
97     Player *p = d_player;
98     const ArmyProto *a;
99     a = Armysetlist::getInstance()->getArmy(p->getArmyset(), army_type);
100     if (a->isHero())
101       return; //we don't want to show heroes in this list
102     Gtk::TreeIter i = armies_list->append();
103     (*i)[armies_columns.name] = a->getName();
104     (*i)[armies_columns.image] = gc->getArmyPic(p->getArmyset(), army_type,
105                                                 p, NULL)->to_pixbuf();
106     (*i)[armies_columns.str] = a->getStrength();
107     (*i)[armies_columns.move] = a->getMaxMoves();
108     guint32 b = a->getMoveBonus();
109     (*i)[armies_columns.move_image] = gc->getMoveBonusPic(b, false)->to_pixbuf();
110     (*i)[armies_columns.bonus] = "-";
111
112     std::string s = a->getArmyBonusDescription();
113     if (s == "")
114       (*i)[armies_columns.bonus] = "-";
115     else
116       (*i)[armies_columns.bonus] = s;
117 }