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