1 // Copyright (C) 2007, 2008, 2009 Ben Asselstine
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.
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.
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
21 #include <sigc++/functors/mem_fun.h>
23 #include "tileset-info-dialog.h"
28 #include "glade-helpers.h"
29 #include "ucompose.hpp"
34 TileSetInfoDialog::TileSetInfoDialog(Tileset *tileset, bool readonly)
37 d_readonly = readonly;
39 Glib::RefPtr<Gtk::Builder> xml
40 = Gtk::Builder::create_from_file(get_glade_path()
41 + "/tileset-info-dialog.ui");
43 xml->get_widget("dialog", dialog);
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);
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));
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));
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());
70 subdir_entry->set_sensitive(false);
73 //remove spaces and lowercase the text
74 char *sanify(const char *string)
78 size_t len = strlen(string);
79 result = (char*) malloc (resultlen);
81 for (unsigned int i = 0; i < len; i++)
83 int letter = tolower(string[i]);
84 if (strchr("abcdefghijklmnopqrstuvwxyz0123456789-", letter) == NULL)
88 result = (char *) realloc (result, resultlen);
91 result[resultlen-2] = char(letter);
92 result[resultlen-1] = '\0';
98 void TileSetInfoDialog::on_subdir_changed()
100 std::string dir = File::getUserTilesetDir() + subdir_entry->get_text();
101 if (File::exists(dir) == true)
103 accept_button->set_sensitive(false);
104 status_label->set_markup(String::ucompose("<b>%1</b>",
105 _("That subdirectory is already in use.")));
109 accept_button->set_sensitive(true);
110 status_label->set_markup("");
113 void TileSetInfoDialog::on_name_changed()
115 char *s = sanify(name_entry->get_text().c_str());
116 subdir_entry->set_text(s);
120 TileSetInfoDialog::~TileSetInfoDialog()
124 void TileSetInfoDialog::set_parent_window(Gtk::Window &parent)
126 dialog->set_transient_for(parent);
127 //dialog->set_position(Gtk::WIN_POS_CENTER_ON_PARENT);
130 int TileSetInfoDialog::run()
133 int response = dialog->run();
135 if (response == Gtk::RESPONSE_ACCEPT) // accepted
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());