initial commit, lordsawar source, slightly modified
[lordsawar] / src / editor / temple-editor-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 "temple-editor-dialog.h"
25
26 #include "glade-helpers.h"
27 #include "ucompose.hpp"
28 #include "CreateScenarioRandomize.h"
29 #include "defs.h"
30 #include "temple.h"
31 #include "RenamableLocation.h"
32
33 TempleEditorDialog::TempleEditorDialog(Temple *t, CreateScenarioRandomize *randomizer)
34 {
35     d_randomizer = randomizer;
36     temple = t;
37     
38     Glib::RefPtr<Gtk::Builder> xml
39         = Gtk::Builder::create_from_file(get_glade_path()
40                                     + "/temple-editor-dialog.ui");
41
42     xml->get_widget("dialog", dialog);
43
44     xml->get_widget("name_entry", name_entry);
45     name_entry->set_text(temple->getName());
46
47     xml->get_widget("description_entry", description_entry);
48     description_entry->set_text(temple->getDescription());
49
50     xml->get_widget("type_entry", type_entry);
51     type_entry->set_value(temple->getType());
52     xml->get_widget("randomize_name_button", randomize_name_button);
53     randomize_name_button->signal_clicked().connect(
54         sigc::mem_fun(this, &TempleEditorDialog::on_randomize_name_clicked));
55 }
56
57 TempleEditorDialog::~TempleEditorDialog()
58 {
59   delete dialog;
60 }
61 void TempleEditorDialog::set_parent_window(Gtk::Window &parent)
62 {
63     dialog->set_transient_for(parent);
64     //dialog->set_position(Gtk::WIN_POS_CENTER_ON_PARENT);
65 }
66
67 int TempleEditorDialog::run()
68 {
69     dialog->show_all();
70     int response = dialog->run();
71
72     if (response == Gtk::RESPONSE_ACCEPT)       // accepted
73     {
74       Location *l = temple;
75       RenamableLocation *renamable_temple = static_cast<RenamableLocation*>(l);
76       renamable_temple->setName(name_entry->get_text());
77       temple->setType(type_entry->get_value_as_int());
78       renamable_temple->setDescription(description_entry->get_text());
79     }
80     else
81       {
82         if (name_entry->get_text() != Temple::getDefaultName())
83           d_randomizer->pushRandomTempleName(name_entry->get_text());
84       }
85     return response;
86 }
87
88 void TempleEditorDialog::on_randomize_name_clicked()
89 {
90   std::string existing_name = name_entry->get_text();
91   if (existing_name == Temple::getDefaultName())
92     name_entry->set_text(d_randomizer->popRandomTempleName());
93   else
94     {
95       name_entry->set_text(d_randomizer->popRandomTempleName());
96       d_randomizer->pushRandomTempleName(existing_name);
97     }
98 }