2 * Copyright (c) 2007-2013, Czirkos Zoltan http://code.google.com/p/gdash/
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
19 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
20 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 #include <glib/gi18n.h>
28 #include "gtk/gtkui.hpp"
29 #include "gfx/cellrenderer.hpp"
30 #include "gtk/gtkuisettings.hpp"
31 #include "gtk/gtkpixbuffactory.hpp"
32 #include "settings.hpp"
33 #include "misc/printf.hpp"
34 #include "framework/thememanager.hpp"
36 #include "framework/shadermanager.hpp"
39 #define GDASH_KEYSIM_WHAT_FOR "gdash-keysim-what-for"
40 #define GDASH_RESTAT_BOOL "gdash-restart-bool"
44 GtkWidget
*themecombo
;
45 std::vector
<std::string
> themes
;
49 /* return a list of image gtk_image_filter's. */
50 /* they have floating reference. */
51 /* the list is to be freed by the caller. */
52 static GList
*image_load_filters()
54 GSList
*formats
=gdk_pixbuf_get_formats();
56 GtkFileFilter
*all_filter
;
57 GList
*filters
=NULL
; /* new list of filters */
59 all_filter
=gtk_file_filter_new();
60 gtk_file_filter_set_name(all_filter
, _("All image files"));
62 /* iterate the list of formats given by gdk. create file filters for each. */
63 for (iter
=formats
; iter
!=NULL
; iter
=iter
->next
) {
64 GdkPixbufFormat
*frm
= (GdkPixbufFormat
*) iter
->data
;
65 if (gdk_pixbuf_format_is_disabled(frm
))
67 GtkFileFilter
*filter
=gtk_file_filter_new();
68 gtk_file_filter_set_name(filter
, gdk_pixbuf_format_get_description(frm
));
69 char **extensions
=gdk_pixbuf_format_get_extensions(frm
);
70 for (int i
=0; extensions
[i
]!=NULL
; i
++) {
71 char *pattern
=g_strdup_printf("*.%s", extensions
[i
]);
72 gtk_file_filter_add_pattern(filter
, pattern
);
73 gtk_file_filter_add_pattern(all_filter
, pattern
);
76 g_strfreev(extensions
);
77 filters
= g_list_append(filters
, filter
);
79 g_slist_free(formats
);
81 /* add "all image files" filter */
82 filters
=g_list_prepend(filters
, all_filter
);
87 /* file open dialog, with filters for image types gdk-pixbuf recognizes. */
89 gd_select_image_file(const char *title
, GtkWidget
*parent
)
97 dialog
=gtk_file_chooser_dialog_new (title
, GTK_WINDOW(parent
), GTK_FILE_CHOOSER_ACTION_OPEN
, GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
, GTK_STOCK_OPEN
, GTK_RESPONSE_ACCEPT
, NULL
);
98 gtk_dialog_set_default_response (GTK_DIALOG (dialog
), GTK_RESPONSE_ACCEPT
);
100 /* obtain list of image filters, and add all to the window */
101 filters
=image_load_filters();
102 for (iter
=filters
; iter
!=NULL
; iter
=iter
->next
)
103 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog
), GTK_FILE_FILTER(iter
->data
));
104 g_list_free(filters
);
106 result
=gtk_dialog_run(GTK_DIALOG(dialog
));
107 if (result
==GTK_RESPONSE_ACCEPT
)
108 filename
=gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(dialog
));
111 gtk_widget_destroy (dialog
);
116 static void add_theme_cb(GtkWidget
*widget
, gpointer data
) {
117 ThemeStuff
*themestuff
= (ThemeStuff
*) data
;
119 char *filename
= gd_select_image_file(_("Add Theme from Image File"), gtk_widget_get_toplevel(widget
));
121 if (filename
== NULL
)
124 GTKPixbufFactory gpf
;
125 bool ok
= CellRenderer::is_image_ok_for_theme(gpf
, filename
);
127 /* make up new filename */
128 char *basename
=g_path_get_basename(filename
);
129 char *new_filename
=g_build_path(G_DIR_SEPARATOR_S
, gd_user_config_dir
, basename
, NULL
);
131 /* if file not exists, or exists BUT overwrite allowed */
132 if (!g_file_test(new_filename
, G_FILE_TEST_EXISTS
) || gd_question_yesno(_("Overwrite file?"), new_filename
)) {
133 /* copy theme to user config directory */
135 GError
*error
= NULL
;
136 GdkPixbuf
*pixbuf
= gdk_pixbuf_new_from_file(filename
, NULL
);
139 gdk_pixbuf_save(pixbuf
, new_filename
, "png", &error
, "compression", "9", NULL
);
142 g_object_unref(pixbuf
);
143 char *thm
= g_filename_display_basename(new_filename
);
144 if (strrchr(thm
, '.')) /* remove extension */
145 *strrchr(thm
, '.') = '\0';
146 gtk_combo_box_append_text(GTK_COMBO_BOX(themestuff
->themecombo
), thm
);
147 themestuff
->themes
.push_back(new_filename
);
148 gd_infomessage(_("The new theme is installed."), thm
);
150 } catch (GError
*error
) {
151 gd_errormessage(error
->message
, NULL
);
155 g_free(new_filename
);
159 gd_errormessage(_("The selected image cannot be used as a GDash theme."), NULL
);
164 gboolean
SettingsWindow::keysim_button_keypress_event(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
) {
165 g_assert(event
->type
== GDK_KEY_PRESS
); /* must be true. */
166 gtk_dialog_response(GTK_DIALOG(widget
), event
->keyval
);
167 return TRUE
; /* and say that we processed the key. */
171 void SettingsWindow::keysim_button_clicked_cb(GtkWidget
*button
, gpointer data
) {
172 const char *what_for
= (const char *)g_object_get_data(G_OBJECT(button
), GDASH_KEYSIM_WHAT_FOR
);
173 int *keyval
= (int *) data
;
175 /* dialog which has its keypress event connected to the handler above */
176 // TRANSLATORS: Title text capitalization in English
177 GtkWidget
*dialog
= gtk_dialog_new_with_buttons(_("Select Key"), GTK_WINDOW(gtk_widget_get_toplevel(button
)),
178 GtkDialogFlags(GTK_DIALOG_MODAL
| GTK_DIALOG_NO_SEPARATOR
),
179 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
, NULL
);
180 GtkWidget
*table
= gtk_table_new(1, 1, FALSE
);
181 gtk_table_set_row_spacings(GTK_TABLE(table
), 6);
182 gtk_table_set_col_spacings(GTK_TABLE(table
), 6);
183 gtk_container_set_border_width(GTK_CONTAINER(table
), 6);
184 gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog
)->vbox
), table
);
185 gtk_table_attach_defaults(GTK_TABLE(table
), gd_label_new_leftaligned(_("Press key for action:")), 0, 1, 0, 1);
186 gtk_table_attach_defaults(GTK_TABLE(table
), gd_label_new_leftaligned(CPrintf("<b>%s</b>") % what_for
), 0, 1, 1, 2);
187 g_signal_connect(G_OBJECT(dialog
), "key_press_event", G_CALLBACK(keysim_button_keypress_event
), dialog
);
189 gtk_window_set_resizable(GTK_WINDOW(dialog
), FALSE
);
190 gtk_widget_show_all(dialog
);
191 int result
= gtk_dialog_run(GTK_DIALOG(dialog
));
193 /* if positive, it must be a keyval. gtk_response_cancel and gtk_response delete is negative. */
195 gtk_button_set_label(GTK_BUTTON(button
), gdk_keyval_name(*keyval
));
197 gtk_widget_destroy(dialog
);
201 GtkWidget
*SettingsWindow::gd_keysim_button(Setting
*setting
) {
202 char const *what_for
= setting
->name
;
203 int *keyval
= (int *) setting
->var
;
204 g_assert(keyval
!= NULL
);
206 /* the button shows the current value in its name */
207 GtkWidget
*button
= gtk_button_new_with_label(gdk_keyval_name(*keyval
));
208 g_signal_connect(G_OBJECT(button
), "clicked", G_CALLBACK(keysim_button_clicked_cb
), keyval
);
209 g_object_set_data(G_OBJECT(button
), GDASH_KEYSIM_WHAT_FOR
, (gpointer
) what_for
);
210 gtk_widget_set_tooltip_text(button
, CPrintf(_("Click here to set the key for action: %s")) % what_for
);
216 /* settings window */
217 void SettingsWindow::bool_toggle(GtkWidget
*widget
, gpointer data
) {
218 bool *restart_bool
= (bool *)g_object_get_data(G_OBJECT(widget
), GDASH_RESTAT_BOOL
);
219 Setting
*setting
= static_cast<Setting
*>(data
);
220 bool *bl
= (bool *) setting
->var
;
222 *bl
= gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget
));
223 if (setting
->restart
&& restart_bool
)
224 *restart_bool
= true;
228 void SettingsWindow::int_change(GtkWidget
*widget
, gpointer data
) {
229 bool *restart_bool
= (bool *)g_object_get_data(G_OBJECT(widget
), GDASH_RESTAT_BOOL
);
230 Setting
*setting
= static_cast<Setting
*>(data
);
231 int *value
= (int *) setting
->var
;
232 *value
= gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget
));
233 if (setting
->restart
&& restart_bool
)
234 *restart_bool
= true;
238 void SettingsWindow::stringv_change(GtkWidget
*widget
, gpointer data
) {
239 bool *restart_bool
= (bool *)g_object_get_data(G_OBJECT(widget
), GDASH_RESTAT_BOOL
);
240 Setting
*setting
= static_cast<Setting
*>(data
);
241 int *ptr
= (int *) setting
->var
;
242 *ptr
= gtk_combo_box_get_active(GTK_COMBO_BOX(widget
));
243 /* if nothing selected (for some reason), set to zero. */
246 if (setting
->restart
&& restart_bool
)
247 *restart_bool
= true;
251 void SettingsWindow::theme_change(GtkWidget
*widget
, gpointer data
) {
252 bool *restart_bool
= (bool *)g_object_get_data(G_OBJECT(widget
), GDASH_RESTAT_BOOL
);
253 int *ptr
= (int *) data
;
254 *ptr
= gtk_combo_box_get_active(GTK_COMBO_BOX(widget
));
255 /* if nothing selected (for some reason), set to zero. */
259 *restart_bool
= true;
263 GtkWidget
*SettingsWindow::combo_box_new_from_stringv(const char **str
) {
264 GtkWidget
*combo
= gtk_combo_box_new_text();
265 for (unsigned i
= 0; str
[i
] != NULL
; i
++)
266 gtk_combo_box_append_text(GTK_COMBO_BOX(combo
), _(str
[i
])); // also translate
271 GtkWidget
*SettingsWindow::combo_box_new_from_themelist(std::vector
<std::string
> const &strings
) {
272 GtkWidget
*combo
= gtk_combo_box_new_text();
273 for (unsigned i
= 0; i
!= strings
.size(); i
++) {
274 if (strings
[i
] == "")
275 gtk_combo_box_append_text(GTK_COMBO_BOX(combo
), _("[Default]"));
277 char *thm
= g_filename_display_basename(strings
[i
].c_str());
278 if (strrchr(thm
, '.')) /* remove extension */
279 *strrchr(thm
, '.') = '\0';
280 gtk_combo_box_append_text(GTK_COMBO_BOX(combo
), thm
);
290 * @return true, if a restart is required by some setting changed
292 bool SettingsWindow::do_settings_dialog(Setting
*settings
, PixbufFactory
&pf
) {
293 bool request_restart
= false;
295 // TRANSLATORS: Title text capitalization in English
296 GtkWidget
*dialog
= gtk_dialog_new_with_buttons(_("GDash Preferences"), guess_active_toplevel(),
297 GTK_DIALOG_DESTROY_WITH_PARENT
, NULL
);
298 gtk_dialog_set_has_separator(GTK_DIALOG(dialog
), FALSE
);
300 /* notebook with tabs */
301 GtkWidget
*notebook
= gtk_notebook_new();
302 gtk_container_set_border_width(GTK_CONTAINER(notebook
), 9);
303 gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(dialog
)->vbox
), notebook
);
305 ThemeStuff themestuff
;
306 themestuff
.themecombo
= NULL
;
308 load_themes_list(pf
, themestuff
.themes
, themenum
);
309 gd_settings_array_prepare(settings
, TypeTheme
, themestuff
.themes
, &themenum
);
312 std::vector
<std::string
> shaders
;
314 load_shaders_list(shaders
, shadernum
);
315 gd_settings_array_prepare(settings
, TypeShader
, shaders
, &shadernum
);
319 GtkWidget
*table
= NULL
;
320 for (unsigned i
= 0; settings
[i
].name
!= NULL
; i
++) {
321 GtkWidget
*widget
= NULL
;
323 switch (settings
[i
].type
) {
325 table
= gtk_table_new(1, 1, FALSE
);
326 gtk_container_set_border_width(GTK_CONTAINER(table
), 9);
327 gtk_table_set_row_spacings(GTK_TABLE(table
), 6);
328 gtk_table_set_col_spacings(GTK_TABLE(table
), 12);
329 gtk_notebook_append_page(GTK_NOTEBOOK(notebook
), table
, gd_label_new_leftaligned(_(settings
[i
].name
)));
334 widget
= gtk_check_button_new();
335 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget
), *(bool *)settings
[i
].var
);
336 gtk_table_attach(GTK_TABLE(table
), widget
, 1, 2, row
, row
+ 1, GtkAttachOptions(GTK_EXPAND
| GTK_FILL
), GtkAttachOptions(0), 0, 0);
337 g_signal_connect(G_OBJECT(widget
), "toggled", G_CALLBACK(SettingsWindow::bool_toggle
), &settings
[i
]);
341 widget
= gtk_spin_button_new_with_range(0, 100, 5);
342 gtk_spin_button_set_value(GTK_SPIN_BUTTON(widget
), *(int *)settings
[i
].var
);
343 g_signal_connect(G_OBJECT(widget
), "value-changed", G_CALLBACK(SettingsWindow::int_change
), &settings
[i
]);
344 gtk_table_attach(GTK_TABLE(table
), widget
, 1, 2, row
, row
+ 1, GtkAttachOptions(GTK_EXPAND
| GTK_FILL
), GtkAttachOptions(0), 0, 0);
348 widget
= gtk_spin_button_new_with_range(settings
[i
].min
, settings
[i
].max
, 1);
349 gtk_spin_button_set_value(GTK_SPIN_BUTTON(widget
), *(int *)settings
[i
].var
);
350 g_signal_connect(G_OBJECT(widget
), "value-changed", G_CALLBACK(SettingsWindow::int_change
), &settings
[i
]);
351 gtk_table_attach(GTK_TABLE(table
), widget
, 1, 2, row
, row
+ 1, GtkAttachOptions(GTK_EXPAND
| GTK_FILL
), GtkAttachOptions(0), 0, 0);
357 widget
= SettingsWindow::combo_box_new_from_stringv(settings
[i
].stringv
);
358 gtk_combo_box_set_active(GTK_COMBO_BOX(widget
), *(int *)settings
[i
].var
);
359 g_signal_connect(G_OBJECT(widget
), "changed", G_CALLBACK(SettingsWindow::stringv_change
), &settings
[i
]);
360 gtk_table_attach(GTK_TABLE(table
), widget
, 1, 2, row
, row
+ 1, GtkAttachOptions(GTK_EXPAND
| GTK_FILL
), GtkAttachOptions(0), 0, 0);
364 widget
= gd_keysim_button(&settings
[i
]);
365 gtk_table_attach(GTK_TABLE(table
), widget
, 1, 2, row
, row
+ 1, GtkAttachOptions(GTK_EXPAND
| GTK_FILL
), GtkAttachOptions(0), 0, 0);
370 gtk_widget_set_tooltip_text(widget
, _(settings
[i
].description
));
371 g_object_set_data(G_OBJECT(widget
), GDASH_RESTAT_BOOL
, &request_restart
);
372 GtkWidget
*label
= gd_label_new_leftaligned(_(settings
[i
].name
));
373 gtk_table_attach(GTK_TABLE(table
), label
, 0, 1, row
, row
+ 1, GtkAttachOptions(GTK_EXPAND
| GTK_FILL
), GtkAttachOptions(0), 0, 0);
374 gtk_label_set_mnemonic_widget(GTK_LABEL(label
), widget
);
377 if (settings
[i
].type
== TypeTheme
)
378 themestuff
.themecombo
= widget
;
383 /* add theme button */
384 if (themestuff
.themecombo
!= NULL
) {
385 GtkWidget
*button
= gtk_button_new_with_mnemonic(_("_Add theme"));
386 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog
)->action_area
), button
, FALSE
, FALSE
, 0);
387 g_signal_connect(G_OBJECT(button
), "clicked", G_CALLBACK(add_theme_cb
), &themestuff
);
390 /* add close button */
391 gtk_dialog_add_button(GTK_DIALOG(dialog
), GTK_STOCK_CLOSE
, GTK_RESPONSE_ACCEPT
);
393 gtk_widget_show_all(dialog
);
394 gtk_dialog_run(GTK_DIALOG(dialog
));
396 gtk_widget_destroy(dialog
);
398 gd_theme
= themestuff
.themes
[themenum
];
399 gd_settings_array_unprepare(settings
, TypeTheme
);
401 gd_shader
= shaders
[shadernum
];
402 gd_settings_array_unprepare(settings
, TypeShader
);
405 return request_restart
;