initial commit, lordsawar source, slightly modified
[lordsawar] / src / gui / fight-order-dialog.cpp
1 //  Copyright (C) 2007, 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 <sigc++/functors/mem_fun.h>
21
22 #include "fight-order-dialog.h"
23
24 #include <gtkmm.h>
25 #include "glade-helpers.h"
26 #include "image-helpers.h"
27 #include "ucompose.hpp"
28 #include "defs.h"
29 #include "playerlist.h"
30 #include "player.h"
31 #include "army.h"
32 #include "armysetlist.h"
33 #include "GraphicsCache.h"
34
35 FightOrderDialog::FightOrderDialog(Player *theplayer)
36 {
37     player = theplayer;
38     
39     Glib::RefPtr<Gtk::Builder> xml
40         = Gtk::Builder::create_from_file(get_glade_path()
41                                     + "/fight-order-dialog.ui");
42
43     xml->get_widget("dialog", dialog);
44     decorate(dialog);
45     window_closed.connect(sigc::mem_fun(dialog, &Gtk::Dialog::hide));
46
47     armies_list = Gtk::ListStore::create(armies_columns);
48     xml->get_widget("treeview", armies_treeview);
49     armies_treeview->set_model(armies_list);
50     armies_treeview->append_column("", armies_columns.image);
51     armies_treeview->append_column("", armies_columns.name);
52
53     std::list<guint32> fight_order = theplayer->getFightOrder();
54     std::list<guint32>::iterator it = fight_order.begin();
55     for (; it != fight_order.end(); it++)
56       {
57         addArmyType(*it);
58       }
59     armies_treeview->set_reorderable(true);
60     xml->get_widget("reverse_button", reverse_button);
61     reverse_button->signal_clicked().connect
62       (sigc::mem_fun (*this, &FightOrderDialog::on_reverse_button_clicked));
63 }
64 FightOrderDialog::~FightOrderDialog()
65 {
66   delete dialog;
67 }
68
69 void FightOrderDialog::set_parent_window(Gtk::Window &parent)
70 {
71     dialog->set_transient_for(parent);
72     //dialog->set_position(Gtk::WIN_POS_CENTER_ON_PARENT);
73 }
74
75 void FightOrderDialog::hide()
76 {
77   dialog->hide();
78 }
79
80 void FightOrderDialog::run()
81 {
82     static int width = -1;
83     static int height = -1;
84
85     if (width != -1 && height != -1)
86         dialog->set_default_size(width, height);
87     
88     dialog->show();
89     int response = dialog->run();
90
91     dialog->get_size(width, height);
92
93     if (response == Gtk::RESPONSE_ACCEPT)
94       {
95         std::list<guint32> fight_order;
96         for (Gtk::TreeIter i = armies_list->children().begin(),
97              end = armies_list->children().end(); i != end; ++i) 
98           fight_order.push_back((*i)[armies_columns.army_type]);
99         player->setFightOrder(fight_order);
100
101       }
102 }
103
104 void FightOrderDialog::addArmyType(guint32 army_type)
105 {
106     GraphicsCache *gc = GraphicsCache::getInstance();
107     Gtk::TreeIter i = armies_list->append();
108     Armysetlist *alist = Armysetlist::getInstance();
109     const ArmyProto *a = alist->getArmy(player->getArmyset(), army_type);
110     (*i)[armies_columns.name] = a->getName();
111     (*i)[armies_columns.image] = gc->getArmyPic(player->getArmyset(),
112                                            army_type, player, NULL)->to_pixbuf();
113     (*i)[armies_columns.army_type] = a->getTypeId();
114 }
115
116 void FightOrderDialog::on_reverse_button_clicked()
117 {
118   std::list<int> new_order;
119   Gtk::TreeModel::Children kids = armies_list->children();
120   for (unsigned int i = 0; i < kids.size(); i++)
121     new_order.push_back(kids.size() - i - 1);
122   armies_list->reorder(new_order);
123 }