1 //  Copyright (C) 2007 Ole Laursen
2 //  Copyright (C) 2007, 2008, 2009, 2014, 2020 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 "ucompose.hpp"
27 #include "defs.h"
28 #include "CreateScenarioRandomize.h"
29 #include "signpost.h"
30 #include "rnd.h"
31 
32 #define method(x) sigc::mem_fun(*this, &SignpostEditorDialog::x)
33 
SignpostEditorDialog(Gtk::Window & parent,Signpost * s,CreateScenarioRandomize * randomizer)34 SignpostEditorDialog::SignpostEditorDialog(Gtk::Window &parent, Signpost *s, CreateScenarioRandomize *randomizer)
35  : LwEditorDialog(parent, "signpost-editor-dialog.ui")
36 {
37     d_randomizer = randomizer;
38     signpost = s;
39 
40     xml->get_widget("sign_textview", sign_textview);
41     sign_textview->get_buffer()->signal_changed().connect
42       (method(on_sign_changed));
43     sign_textview->get_buffer()->set_text(s->getName());
44 
45     xml->get_widget("randomize_button", randomize_button);
46     randomize_button->signal_clicked().connect(method(on_randomize_clicked));
47 }
48 
on_sign_changed()49 void SignpostEditorDialog::on_sign_changed ()
50 {
51   signpost->setName(sign_textview->get_buffer()->get_text());
52 }
53 
run()54 int SignpostEditorDialog::run()
55 {
56   dialog->show_all();
57   return dialog->run();
58 }
59 
on_randomize_clicked()60 void SignpostEditorDialog::on_randomize_clicked()
61 {
62   Glib::ustring existing_name = sign_textview->get_buffer()->get_text();
63   bool dynamic = ((Rnd::rand() % d_randomizer->getNumSignposts()) == 0);
64   if (existing_name == DEFAULT_SIGNPOST)
65     {
66       if (dynamic)
67 	sign_textview->get_buffer()->set_text
68 	  (d_randomizer->getDynamicSignpost(signpost));
69       else
70 	sign_textview->get_buffer()->set_text
71 	  (d_randomizer->popRandomSignpost());
72     }
73   else
74     {
75       if (dynamic)
76 	sign_textview->get_buffer()->set_text
77 	  (d_randomizer->getDynamicSignpost(signpost));
78       else
79 	{
80 	  sign_textview->get_buffer()->set_text
81 	    (d_randomizer->popRandomSignpost());
82 	  d_randomizer->pushRandomSignpost(existing_name);
83 	}
84     }
85 }
86