initial commit, lordsawar source, slightly modified
[lordsawar] / src / editor / cityset-info-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 <gtkmm.h>
21 #include <sigc++/functors/mem_fun.h>
22
23 #include "cityset-info-dialog.h"
24
25 #include <string.h>
26 #include <stdlib.h>
27 #include <ctype.h>
28 #include "glade-helpers.h"
29 #include "ucompose.hpp"
30 #include "defs.h"
31 #include "File.h"
32
33
34 CitySetInfoDialog::CitySetInfoDialog(Cityset *cityset, bool readonly)
35 {
36   d_cityset = cityset;
37   d_readonly = readonly;
38     
39     Glib::RefPtr<Gtk::Builder> xml
40         = Gtk::Builder::create_from_file(get_glade_path()
41                                     + "/cityset-info-dialog.ui");
42
43     xml->get_widget("dialog", dialog);
44
45     xml->get_widget("accept_button", accept_button);
46     xml->get_widget("status_label", status_label);
47
48     xml->get_widget("name_entry", name_entry);
49     name_entry->set_text(cityset->getName());
50     if (readonly == false)
51       name_entry->signal_changed().connect
52         (sigc::mem_fun(this, &CitySetInfoDialog::on_name_changed));
53     
54     xml->get_widget("subdir_entry", subdir_entry);
55     subdir_entry->set_text(cityset->getSubDir());
56     if (readonly == false)
57       subdir_entry->signal_changed().connect
58         (sigc::mem_fun(this, &CitySetInfoDialog::on_subdir_changed));
59
60     xml->get_widget("id_spinbutton", id_spinbutton);
61     id_spinbutton->set_value(cityset->getId());
62     id_spinbutton->set_sensitive(false);
63
64     xml->get_widget("copyright_textview", copyright_textview);
65     copyright_textview->get_buffer()->set_text(d_cityset->getCopyright());
66     xml->get_widget("license_textview", license_textview);
67     license_textview->get_buffer()->set_text(d_cityset->getLicense());
68     xml->get_widget("description_textview", description_textview);
69     description_textview->get_buffer()->set_text(cityset->getInfo());
70
71     if (readonly)
72       subdir_entry->set_sensitive(false);
73 }
74
75 //remove spaces and lowercase the text
76 char *sanify(const char *string)
77 {
78   char *result = NULL;
79   size_t resultlen = 1;
80   size_t len = strlen(string);
81   result = (char*) malloc (resultlen);
82   result[0] = '\0';
83   for (unsigned int i = 0; i < len; i++)
84     {
85       int letter = tolower(string[i]);
86       if (strchr("abcdefghijklmnopqrstuvwxyz0123456789-", letter) == NULL)
87         continue;
88
89       resultlen++;
90       result = (char *) realloc (result, resultlen);
91       if (result)
92         {
93           result[resultlen-2] = char(letter);
94           result[resultlen-1] = '\0';
95         }
96     }
97   return result;
98 }
99
100 void CitySetInfoDialog::on_subdir_changed()
101 {
102   std::string dir = File::getUserCitysetDir() + subdir_entry->get_text();
103   if (File::exists(dir) == true)
104     {
105       accept_button->set_sensitive(false);
106       status_label->set_markup(String::ucompose("<b>%1</b>", 
107                                                 _("That subdirectory is already in use.")));
108     }
109   else
110     {
111       accept_button->set_sensitive(true);
112       status_label->set_markup("");
113     }
114 }
115 void CitySetInfoDialog::on_name_changed()
116 {
117   char *s = sanify(name_entry->get_text().c_str());
118   subdir_entry->set_text(s);
119   free (s);
120 }
121
122 CitySetInfoDialog::~CitySetInfoDialog()
123 {
124   delete dialog;
125 }
126 void CitySetInfoDialog::set_parent_window(Gtk::Window &parent)
127 {
128     dialog->set_transient_for(parent);
129     //dialog->set_position(Gtk::WIN_POS_CENTER_ON_PARENT);
130 }
131
132 int CitySetInfoDialog::run()
133 {
134     dialog->show_all();
135     int response = dialog->run();
136
137     if (response == Gtk::RESPONSE_ACCEPT)       // accepted
138     {
139       d_cityset->setName(name_entry->get_text());
140       d_cityset->setId(int(id_spinbutton->get_value()));
141       if (d_readonly == false)
142         d_cityset->setSubDir(subdir_entry->get_text());
143       d_cityset->setCopyright(copyright_textview->get_buffer()->get_text());
144       d_cityset->setLicense(license_textview->get_buffer()->get_text());
145       d_cityset->setInfo(description_textview->get_buffer()->get_text());
146       return response;
147     }
148     return response;
149 }
150