Debian Codegraph @

src/tools/dlgedit/gui_settings.cc

raw
1 /*
2 Copyright (C) 2002/2003/2004 Kai Sterker <[email protected]>
3 Part of the Adonthell Project <http://adonthell.nongnu.org>
4
5 Dlgedit is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 Dlgedit is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with Dlgedit. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /**
20 * @file gui_settings.cc
21 *
22 * @author Kai Sterker
23 * @brief The Dialogue settings dialog.
24 */
25
26 #include <string.h>
27 #include <sys/stat.h>
28 #include <iostream>
29 #include "cfg_data.h"
30 #include "gui_file.h"
31 #include "gui_dlgedit.h"
32 #include "gui_settings.h"
33
34 // global pointer to the settings dialog
35 GuiSettings * GuiSettings::dialog = NULL;
36
37 // the project selection has changed
38 void on_project_changed (GtkComboBox *widget, gpointer user_data)
39 {
40 // set project basedir accordingly
41 GuiSettings *settings = (GuiSettings *) user_data;
42 settings->setBasedir (settings->getProject());
43 }
44
45 // browse the harddisk for a project
46 void on_browse_basedir_clicked (GtkButton * button, gpointer user_data)
47 {
48 // if a project root exists, use that in file selector
49 std::string dir = CfgData::data->getBasedir (GuiSettings::dialog->getProject ());
50
51 // otherwise revert to directory last opened
52 if (dir == "") dir = GuiDlgedit::window->directory ();
53
54 GtkWindow *parent = GTK_WINDOW (GuiDlgedit::window->getWindow());
55 GuiFile fs (parent, GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, "Select base directory", dir + "/");
56
57 // File selection closed with OK
58 if (fs.run ())
59 {
60 GtkEntry *entry = (GtkEntry *) user_data;
61 std::string file = fs.getSelection ();
62 unsigned int len = file.length () - 1;
63
64 // check if we have a directory
65 struct stat statbuf;
66 stat (file.c_str (), &statbuf);
67
68 if (S_ISDIR (statbuf.st_mode))
69 {
70 // have a directory
71 gtk_entry_set_text (entry, file.substr (0, len).c_str ());
72 }
73 else
74 {
75 // extract directory from file name
76 unsigned int pos = file.rfind ('/');
77 gtk_entry_set_text (entry, file.substr (0, pos).c_str ());
78 }
79
80 gtk_editable_set_position (GTK_EDITABLE(entry), -1);
81 }
82 }
83
84 // OK button pressed
85 void on_ok_button_clicked (GtkButton * button, gpointer user_data)
86 {
87 GuiSettings::dialog->applyChanges ();
88 delete GuiSettings::dialog;
89 }
90
91 // Apply button pressed
92 void on_apply_button_clicked (GtkButton * button, gpointer user_data)
93 {
94 GuiSettings::dialog->applyChanges ();
95 }
96
97 // callback for closing the window
98 void on_close_settings (GtkButton * button, gpointer user_data)
99 {
100 delete GuiSettings::dialog;
101 }
102
103 // ctor
104 GuiSettings::GuiSettings ()
105 {
106 GtkWidget *vbox;
107 GtkWidget *frame;
108 GtkWidget *table;
109 GtkWidget *label;
110 GtkWidget *scrolledwindow;
111 GtkWidget *hbox;
112 GtkWidget *button;
113 GtkWidget *hseparator;
114 GtkWidget *hbuttonbox;
115 GtkWidget *view;
116
117 // the dialog
118 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
119 g_object_set_data (G_OBJECT (window), "window", window);
120 gtk_widget_set_size_request (window, 400, 320);
121 gtk_window_set_title (GTK_WINDOW (window), "Settings");
122 gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
123 gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
124
125 vbox = gtk_vbox_new (FALSE, 2);
126 g_object_ref (vbox);
127 g_object_set_data_full (G_OBJECT (window), "vbox", vbox, (GDestroyNotify) g_object_unref);
128 gtk_widget_show (vbox);
129 gtk_container_add (GTK_CONTAINER (window), vbox);
130
131 // dialogue settings
132 frame = gtk_frame_new ("Dialogue Settings");
133 g_object_ref (frame);
134 g_object_set_data_full (G_OBJECT (window), "frame", frame, (GDestroyNotify) g_object_unref);
135 gtk_widget_show (frame);
136 gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, 0);
137 gtk_container_set_border_width (GTK_CONTAINER (frame), 4);
138
139 table = gtk_table_new (2, 2, FALSE);
140 g_object_ref (table);
141 g_object_set_data_full (G_OBJECT (window), "table", table, (GDestroyNotify) g_object_unref);
142 gtk_widget_show (table);
143 gtk_container_add (GTK_CONTAINER (frame), table);
144 gtk_container_set_border_width (GTK_CONTAINER (table), 4);
145 gtk_table_set_col_spacings (GTK_TABLE (table), 8);
146 gtk_table_set_row_spacings (GTK_TABLE (table), 4);
147
148 // project
149 label = gtk_label_new ("Project");
150 g_object_ref (label);
151 g_object_set_data_full (G_OBJECT (window), "label", label, (GDestroyNotify) g_object_unref);
152 gtk_widget_show (label);
153 gtk_table_attach (GTK_TABLE (table), label, 0, 1, 0, 1, (GtkAttachOptions) (GTK_FILL), (GtkAttachOptions) (0), 0, 8);
154 gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_RIGHT);
155 gtk_misc_set_alignment (GTK_MISC (label), 1, 0.5);
156
157 // description
158 label = gtk_label_new ("Description");
159 g_object_ref (label);
160 g_object_set_data_full (G_OBJECT (window), "label", label, (GDestroyNotify) g_object_unref);
161 gtk_widget_show (label);
162 gtk_table_attach (GTK_TABLE (table), label, 0, 1, 1, 2, (GtkAttachOptions) (GTK_FILL), (GtkAttachOptions) (0), 0, 0);
163 gtk_widget_set_size_request(label, 70, -1);
164 gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_RIGHT);
165 gtk_misc_set_alignment (GTK_MISC (label), 1, 0.5);
166
167 scrolledwindow = gtk_scrolled_window_new (NULL, NULL);
168 g_object_ref (scrolledwindow);
169 g_object_set_data_full (G_OBJECT (window), "scrolledwindow", scrolledwindow, (GDestroyNotify) g_object_unref);
170 gtk_widget_show (scrolledwindow);
171 gtk_table_attach (GTK_TABLE (table), scrolledwindow, 1, 2, 1, 2, (GtkAttachOptions) (GTK_EXPAND | GTK_FILL), (GtkAttachOptions) (GTK_EXPAND | GTK_FILL), 0, 0);
172 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwindow), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
173
174 description = gtk_text_buffer_new (NULL);
175 view = gtk_text_view_new_with_buffer (description);
176 g_object_ref (view);
177 g_object_set_data_full (G_OBJECT (window), "view", view, (GDestroyNotify) g_object_unref);
178 gtk_widget_show (view);
179 gtk_container_add (GTK_CONTAINER (scrolledwindow), view);
180 gtk_widget_set_tooltip_text (view, "Here goes a view of the dialogue");
181 gtk_text_view_set_editable (GTK_TEXT_VIEW (view), TRUE);
182 gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (view), GTK_WRAP_WORD);
183
184 // project selection
185 project = gtk_combo_box_new();
186 g_object_ref (project);
187 g_object_set_data_full (G_OBJECT (window), "project", project, (GDestroyNotify) g_object_unref);
188 gtk_widget_show (project);
189 gtk_table_attach (GTK_TABLE (table), project, 1, 2, 0, 1, (GtkAttachOptions) (GTK_FILL), (GtkAttachOptions) (0), 0, 0);
190 gtk_widget_set_tooltip_text (project, "The project this dialogue belongs to");
191 g_signal_connect (G_OBJECT (project), "changed", G_CALLBACK (on_project_changed), this);
192
193 GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
194 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (project), renderer, TRUE);
195 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (project), renderer, "text", 0, NULL);
196
197 // add available projects to list
198 populateProjects ();
199
200 frame = gtk_frame_new ("Project Settings");
201 g_object_ref (frame);
202 g_object_set_data_full (G_OBJECT (window), "frame", frame, (GDestroyNotify) g_object_unref);
203 gtk_widget_show (frame);
204 gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
205 gtk_container_set_border_width (GTK_CONTAINER (frame), 4);
206
207 hbox = gtk_hbox_new (FALSE, 8);
208 g_object_ref (hbox);
209 g_object_set_data_full (G_OBJECT (window), "hbox", hbox, (GDestroyNotify) g_object_unref);
210 gtk_widget_show (hbox);
211 gtk_container_add (GTK_CONTAINER (frame), hbox);
212 gtk_container_set_border_width (GTK_CONTAINER (hbox), 4);
213
214 // base dir entry
215 label = gtk_label_new ("Base dir");
216 g_object_ref (label);
217 g_object_set_data_full (G_OBJECT (window), "label", label, (GDestroyNotify) g_object_unref);
218 gtk_widget_show (label);
219 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
220 gtk_widget_set_size_request (label, 70, -1);
221 gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_RIGHT);
222 gtk_misc_set_alignment (GTK_MISC (label), 1, 0.5);
223
224 basedir = gtk_entry_new ();
225 g_object_ref (basedir);
226 g_object_set_data_full (G_OBJECT (window), "basedir", basedir, (GDestroyNotify) g_object_unref);
227 gtk_widget_show (basedir);
228 gtk_box_pack_start (GTK_BOX (hbox), basedir, TRUE, TRUE, 0);
229 gtk_widget_set_tooltip_text (basedir, "The directory root of dialogue sources belonging to above project");
230
231 // browse for base dir
232 button = gtk_button_new_with_label ("...");
233 g_object_ref (button);
234 g_object_set_data_full (G_OBJECT (window), "button", button, (GDestroyNotify) g_object_unref);
235 gtk_widget_show (button);
236 gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
237 gtk_widget_set_size_request (button, 30, -1);
238 gtk_widget_set_tooltip_text (button, "Browse your file system for the base directory");
239 g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (on_browse_basedir_clicked), basedir);
240
241 hseparator = gtk_hseparator_new ();
242 g_object_ref (hseparator);
243 g_object_set_data_full (G_OBJECT (window), "hseparator", hseparator, (GDestroyNotify) g_object_unref);
244 gtk_widget_show (hseparator);
245 gtk_box_pack_start (GTK_BOX (vbox), hseparator, FALSE, FALSE, 0);
246
247 hbuttonbox = gtk_hbutton_box_new ();
248 g_object_ref (hbuttonbox);
249 g_object_set_data_full (G_OBJECT (window), "hbuttonbox", hbuttonbox, (GDestroyNotify) g_object_unref);
250 gtk_widget_show (hbuttonbox);
251 gtk_box_pack_start (GTK_BOX (vbox), hbuttonbox, FALSE, FALSE, 0);
252 gtk_button_box_set_layout (GTK_BUTTON_BOX (hbuttonbox), GTK_BUTTONBOX_END);
253 gtk_box_set_spacing (GTK_BOX (hbuttonbox), 0);
254
255 button = gtk_button_new_with_label ("Apply");
256 g_object_ref (button);
257 g_object_set_data_full (G_OBJECT (window), "button", button, (GDestroyNotify) g_object_unref);
258 gtk_widget_show (button);
259 gtk_container_add (GTK_CONTAINER (hbuttonbox), button);
260 gtk_widget_set_can_default (button, TRUE);
261 g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (on_apply_button_clicked), this);
262
263 button = gtk_button_new_with_label ("OK");
264 g_object_ref (button);
265 g_object_set_data_full (G_OBJECT (window), "button", button, (GDestroyNotify) g_object_unref);
266 gtk_widget_show (button);
267 gtk_container_add (GTK_CONTAINER (hbuttonbox), button);
268 gtk_widget_set_can_default (button, TRUE);
269 g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (on_ok_button_clicked), NULL);
270
271 button = gtk_button_new_with_label ("Cancel");
272 g_object_ref (button);
273 g_object_set_data_full (G_OBJECT (window), "button", button, (GDestroyNotify) g_object_unref);
274 gtk_widget_show (button);
275 gtk_container_add (GTK_CONTAINER (hbuttonbox), button);
276 gtk_widget_set_can_default (button, TRUE);
277 g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (on_close_settings), NULL);
278
279 // set transient for dialogue editor main window
280 gtk_window_set_transient_for (GTK_WINDOW (window), GTK_WINDOW (GuiDlgedit::window->getWindow ()));
281
282 dialog = this;
283 entry = NULL;
284 }
285
286 // dtor
287 GuiSettings::~GuiSettings ()
288 {
289 gtk_widget_destroy (window);
290 dialog = NULL;
291 }
292
293 // display the dialog
294 void GuiSettings::display (DlgModuleEntry * e, const std::string & name)
295 {
296 if (entry != e)
297 {
298 entry = e;
299
300 // display the contents of the module
301 setProject (entry->project ());
302 setDescription (entry->description());
303 }
304
305 // set the title
306 gchar *title = g_strjoin (NULL, "Settings - [", name.c_str (), "]", NULL);
307 gtk_window_set_title (GTK_WINDOW (window), title);
308
309 // now show the window
310 gtk_widget_show (window);
311 }
312
313 // store the user's entries
314 void GuiSettings::applyChanges ()
315 {
316 // project
317 std::string project = getProject ();
318
319 // if project changed, update tree
320 if (project != entry->project ())
321 {
322 if (!entry->setProject (project))
323 std::cout << "Loading quests/characters failed!\n";
324 GuiDlgedit::window->updateProject ();
325 }
326
327 // description
328 entry->setDescription (getDescription ());
329
330 // basedir
331 if (project != "none")
332 CfgData::data->setBasedir (project, getBasedir ());
333 }
334
335 // returns selected option
336 std::string GuiSettings::getProject ()
337 {
338 GtkTreeIter iter;
339 gchar *s = NULL;
340
341 if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX(project), &iter))
342 {
343 GtkTreeModel *model = gtk_combo_box_get_model (GTK_COMBO_BOX(project));
344 gtk_tree_model_get (model, &iter, 0, &s, -1);
345 }
346
347 return s ? s : "none";
348 }
349
350 // sets a default option
351 void GuiSettings::setProject (const std::string & label)
352 {
353 GtkTreeIter iter;
354 gchar *option = NULL;
355 GtkTreeModel *model = gtk_combo_box_get_model (GTK_COMBO_BOX(project));
356
357 if (gtk_tree_model_get_iter_first(model, &iter))
358 {
359 do
360 {
361 gtk_tree_model_get (model, &iter, 0, &option, -1);
362 if (strcmp(option, label.c_str()) == 0)
363 {
364 // found our entry -> set as default and return
365 gtk_combo_box_set_active_iter(GTK_COMBO_BOX(project), &iter);
366
367 // update base directory
368 setBasedir (label);
369
370 return;
371 }
372 }
373 while (gtk_tree_model_iter_next(model, &iter));
374 }
375
376 // that project is not available yet, so add it
377 gtk_list_store_append(GTK_LIST_STORE(model), &iter);
378 gtk_list_store_set(GTK_LIST_STORE(model), &iter, 0, label.c_str(), -1);
379 gtk_combo_box_set_active_iter (GTK_COMBO_BOX(project), &iter);
380
381 // update base directory
382 setBasedir (label);
383 }
384
385 // set the module's description
386 void GuiSettings::setDescription (const std::string & desc)
387 {
388 gtk_text_buffer_set_text (description, desc.c_str (), -1);
389 }
390
391 // get the module's description
392 std::string GuiSettings::getDescription ()
393 {
394 GtkTextIter start, end;
395 gtk_text_buffer_get_bounds (description, &start, &end);
396
397 gchar *tmp = gtk_text_buffer_get_text (description, &start, &end, TRUE);
398 std::string text (tmp);
399 g_free (tmp);
400
401 return text;
402 }
403
404 // set project base directory
405 void GuiSettings::setBasedir (const std::string & project)
406 {
407 // do nothing for project "none"
408 if (project == "none")
409 {
410 gtk_entry_set_text (GTK_ENTRY (basedir), "");
411 return;
412 }
413
414 // get base directory of given project from config file
415 std::string dir = CfgData::data->getBasedir (project);
416
417 // update the entry
418 gtk_entry_set_text (GTK_ENTRY (basedir), dir.c_str ());
419 gtk_editable_set_position (GTK_EDITABLE (basedir), -1);
420 }
421
422 // get project base directory
423 std::string GuiSettings::getBasedir ()
424 {
425 return gtk_entry_get_text (GTK_ENTRY (basedir));
426 }
427
428 // add available projects to list
429 void GuiSettings::populateProjects ()
430 {
431 GtkTreeIter iter;
432 GtkListStore *model = gtk_list_store_new (1, G_TYPE_STRING);
433 gtk_combo_box_set_model(GTK_COMBO_BOX(project), GTK_TREE_MODEL(model));
434
435 std::vector<std::string> projects = CfgData::data->projectsFromDatadir ();
436 std::vector<std::string>::iterator i;
437
438 for (i = projects.begin (); i != projects.end (); i++)
439 {
440 gtk_list_store_append(model, &iter);
441 gtk_list_store_set(model, &iter, 0, i->c_str(), -1);
442 }
443 }