initial commit, lordsawar source, slightly modified
[lordsawar] / src / editor / signpost-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 "signpost-editor-dialog.h"
25
26 #include "glade-helpers.h"
27 #include "ucompose.hpp"
28 #include "defs.h"
29 #include "CreateScenarioRandomize.h"
30 #include "signpost.h"
31
32 SignpostEditorDialog::SignpostEditorDialog(Signpost *s, CreateScenarioRandomize *randomizer)
33 {
34     d_randomizer = randomizer;
35     signpost = s;
36     
37     Glib::RefPtr<Gtk::Builder> xml
38         = Gtk::Builder::create_from_file(get_glade_path()
39                                     + "/signpost-editor-dialog.ui");
40
41     xml->get_widget("dialog", dialog);
42
43     xml->get_widget("sign_textview", sign_textview);
44     sign_textview->get_buffer()->set_text(s->getName());
45     
46     xml->get_widget("randomize_button", randomize_button);
47     randomize_button->signal_clicked().connect(
48         sigc::mem_fun(this, &SignpostEditorDialog::on_randomize_clicked));
49 }
50
51 SignpostEditorDialog::~SignpostEditorDialog()
52 {
53   delete dialog;
54 }
55 void SignpostEditorDialog::set_parent_window(Gtk::Window &parent)
56 {
57     dialog->set_transient_for(parent);
58     //dialog->set_position(Gtk::WIN_POS_CENTER_ON_PARENT);
59 }
60
61 int SignpostEditorDialog::run()
62 {
63   dialog->show_all();
64   int response = dialog->run();
65
66   if (response == Gtk::RESPONSE_ACCEPT) // accepted
67     signpost->setName(sign_textview->get_buffer()->get_text());
68   else
69     {
70       if (sign_textview->get_buffer()->get_text() != DEFAULT_SIGNPOST)
71         d_randomizer->pushRandomSignpost
72           (sign_textview->get_buffer()->get_text());
73     }
74   return response;
75 }
76
77 void SignpostEditorDialog::on_randomize_clicked()
78 {
79   std::string existing_name = sign_textview->get_buffer()->get_text();
80   bool dynamic = ((rand() % d_randomizer->getNumSignposts()) == 0);
81   if (existing_name == DEFAULT_SIGNPOST)
82     {
83       if (dynamic)
84         sign_textview->get_buffer()->set_text
85           (d_randomizer->getDynamicSignpost(signpost));
86       else
87         sign_textview->get_buffer()->set_text
88           (d_randomizer->popRandomSignpost());
89     }
90   else
91     {
92       if (dynamic)
93         sign_textview->get_buffer()->set_text
94           (d_randomizer->getDynamicSignpost(signpost));
95       else
96         {
97           sign_textview->get_buffer()->set_text
98             (d_randomizer->popRandomSignpost());
99           d_randomizer->pushRandomSignpost(existing_name);
100         }
101     }
102 }