initial commit, lordsawar source, slightly modified
[lordsawar] / src / editor / select-hidden-ruin-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 #include <assert.h>
23
24 #include "select-hidden-ruin-dialog.h"
25
26 #include "glade-helpers.h"
27 #include "gui/input-helpers.h"
28 #include "ucompose.hpp"
29 #include "defs.h"
30 #include "ruin.h"
31 #include "ruinlist.h"
32 #include "playerlist.h"
33
34 SelectHiddenRuinDialog::SelectHiddenRuinDialog()
35 {
36     selected_hidden_ruin = 0;
37     
38     Glib::RefPtr<Gtk::Builder> xml
39         = Gtk::Builder::create_from_file(get_glade_path()
40                                     + "/select-hidden-ruin-dialog.ui");
41
42     xml->get_widget("dialog", dialog);
43     
44     xml->get_widget("select_button", select_button);
45
46     xml->get_widget("hidden_ruins_treeview", hidden_ruins_treeview);
47     hidden_ruins_list = Gtk::ListStore::create(hidden_ruins_columns);
48     hidden_ruins_treeview->set_model(hidden_ruins_list);
49     hidden_ruins_treeview->append_column("", hidden_ruins_columns.name);
50     hidden_ruins_treeview->set_headers_visible(false);
51
52     Ruinlist *ruinlist = Ruinlist::getInstance();
53     Ruinlist::iterator iter = ruinlist->begin();
54     for (;iter != ruinlist->end(); iter++)
55       if ((*iter)->isHidden() && 
56           (*iter)->getOwner() == Playerlist::getInstance()->getNeutral())
57         addHiddenRuin(*iter);
58       
59     guint32 max = ruinlist->size();
60     if (max)
61       {
62         Gtk::TreeModel::Row row;
63         row = hidden_ruins_treeview->get_model()->children()[0];
64         if(row)
65           hidden_ruins_treeview->get_selection()->select(row);
66       }
67     else
68       select_button->set_sensitive(false);
69
70 }
71
72 SelectHiddenRuinDialog::~SelectHiddenRuinDialog()
73 {
74   delete dialog;
75 }
76 void SelectHiddenRuinDialog::addHiddenRuin(Ruin *ruin)
77 {
78     
79   Glib::ustring s;
80   Gtk::TreeIter i = hidden_ruins_list->append();
81   s = String::ucompose("%1 (%2, %3)", ruin->getName(), ruin->getPos().x,
82                        ruin->getPos().y);
83   (*i)[hidden_ruins_columns.name] = s;
84   (*i)[hidden_ruins_columns.ruin] = ruin;
85 }
86
87 void SelectHiddenRuinDialog::set_parent_window(Gtk::Window &parent)
88 {
89     dialog->set_transient_for(parent);
90     //dialog->set_position(Gtk::WIN_POS_CENTER_ON_PARENT);
91 }
92
93 void SelectHiddenRuinDialog::run()
94 {
95     dialog->show_all();
96     int response = dialog->run();
97
98     if (response != Gtk::RESPONSE_ACCEPT)
99         selected_hidden_ruin = 0;
100     else
101       {
102         Glib::RefPtr<Gtk::TreeSelection> selection = 
103           hidden_ruins_treeview->get_selection();
104         Gtk::TreeModel::iterator iterrow = selection->get_selected();
105
106         if (iterrow) 
107           {
108             Gtk::TreeModel::Row row = *iterrow;
109             selected_hidden_ruin = row[hidden_ruins_columns.ruin];
110           }
111       }
112 }
113