2 * (SLIK) SimpLIstic sKin functions
7 * This software is released under the GNU General Public License (GNU GPL).
8 * Please read the included file COPYING for more information.
9 * This software comes with no warranty of any kind, use at your own risk!
20 #include <sys/types.h>
23 #include <gdk/gdkkeysyms.h> /* for keyboard values */
25 #include "ui_utildlg.h"
27 #include "ui_fileops.h"
29 #include "ui_pathsel.h"
30 #include "ui_tabcomp.h"
34 *-----------------------------------------------------------------------------
36 *-----------------------------------------------------------------------------
39 void generic_dialog_close(GenericDialog *gd)
41 gtk_widget_destroy(gd->dialog);
45 static void generic_dialog_click_cb(GtkWidget *widget, gpointer data)
47 GenericDialog *gd = data;
48 void (*func)(GenericDialog *, gpointer);
51 func = g_object_get_data(G_OBJECT(widget), "dialog_function");
52 auto_close = gd->auto_close;
54 if (func) func(gd, gd->data);
55 if (auto_close) generic_dialog_close(gd);
58 static gint generic_dialog_default_key_press_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
60 GenericDialog *gd = data;
62 if (event->keyval == GDK_Return && GTK_WIDGET_HAS_FOCUS(widget)
67 auto_close = gd->auto_close;
68 gd->default_cb(gd, gd->data);
69 if (auto_close) generic_dialog_close(gd);
76 void generic_dialog_attach_default(GenericDialog *gd, GtkWidget *widget)
78 if (!gd || !widget) return;
79 g_signal_connect(G_OBJECT(widget), "key_press_event",
80 G_CALLBACK(generic_dialog_default_key_press_cb), gd);
83 static gint generic_dialog_key_press_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
85 GenericDialog *gd = data;
87 if (event->keyval == GDK_Escape)
89 if (gd->cancel_cb) gd->cancel_cb(gd, gd->data);
90 if (gd->auto_close) generic_dialog_click_cb(widget, data);
96 static gint generic_dialog_delete_cb(GtkWidget *w, GdkEventAny *event, gpointer data)
98 GenericDialog *gd = data;
101 auto_close = gd->auto_close;
103 if (gd->cancel_cb) gd->cancel_cb(gd, gd->data);
104 if (auto_close) generic_dialog_close(gd);
109 static void generic_dialog_show_cb(GtkWidget *widget, gpointer data)
111 GenericDialog *gd = data;
112 if (gd->cancel_button)
114 gtk_box_reorder_child(GTK_BOX(gd->hbox), gd->cancel_button, -1);
117 g_signal_handlers_disconnect_by_func(G_OBJECT(widget),
118 G_CALLBACK(generic_dialog_show_cb), gd);
121 gint generic_dialog_get_alternative_button_order(GtkWidget *widget)
123 GtkSettings *settings;
125 gint alternative_order = FALSE;
127 settings = gtk_settings_get_for_screen(gtk_widget_get_screen(widget));
128 klass = G_OBJECT_CLASS(GTK_SETTINGS_GET_CLASS(settings));
129 if (g_object_class_find_property(klass, "gtk-alternative-button-order"))
131 g_object_get(settings, "gtk-alternative-button-order", &alternative_order, NULL);
134 return alternative_order;
137 GtkWidget *generic_dialog_add_button(GenericDialog *gd, const gchar *stock_id, const gchar *text,
138 void (*func_cb)(GenericDialog *, gpointer), gint is_default)
141 gint alternative_order;
143 button = pref_button_new(NULL, stock_id, text, FALSE,
144 G_CALLBACK(generic_dialog_click_cb), gd);
146 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
147 g_object_set_data(G_OBJECT(button), "dialog_function", func_cb);
149 gtk_container_add(GTK_CONTAINER(gd->hbox), button);
151 alternative_order = generic_dialog_get_alternative_button_order(gd->hbox);
155 gtk_widget_grab_default(button);
156 gtk_widget_grab_focus(button);
157 gd->default_cb = func_cb;
159 if (!alternative_order) gtk_box_reorder_child(GTK_BOX(gd->hbox), button, -1);
163 if (!alternative_order) gtk_box_reorder_child(GTK_BOX(gd->hbox), button, 0);
166 gtk_widget_show(button);
171 GtkWidget *generic_dialog_add_message(GenericDialog *gd, const gchar *icon_stock_id,
172 const gchar *heading, const gchar *text)
178 hbox = pref_box_new(gd->vbox, FALSE, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
183 image = gtk_image_new_from_stock(icon_stock_id, GTK_ICON_SIZE_DIALOG);
184 gtk_misc_set_alignment(GTK_MISC(image), 0.5, 0.0);
185 gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 0);
186 gtk_widget_show(image);
189 vbox = pref_box_new(hbox, TRUE, GTK_ORIENTATION_VERTICAL, PREF_PAD_SPACE);
192 label = pref_label_new(vbox, heading);
193 pref_label_bold(label, TRUE, TRUE);
194 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
198 label = pref_label_new(vbox, text);
199 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
205 static void generic_dialog_setup(GenericDialog *gd,
207 const gchar *wmclass, const gchar *wmsubclass,
208 GtkWidget *parent, gint auto_close,
209 void (*cancel_cb)(GenericDialog *, gpointer), gpointer data)
213 gd->auto_close = auto_close;
215 gd->cancel_cb = cancel_cb;
217 gd->dialog = gtk_window_new(GTK_WINDOW_TOPLEVEL);
218 gtk_window_set_type_hint(GTK_WINDOW(gd->dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
219 gtk_window_set_wmclass(GTK_WINDOW(gd->dialog), wmsubclass, wmclass);
222 GtkWindow *window = NULL;
224 if (GTK_IS_WINDOW(parent))
226 window = GTK_WINDOW(parent);
232 top = gtk_widget_get_toplevel(parent);
233 if (GTK_IS_WINDOW(top) && GTK_WIDGET_TOPLEVEL(top)) window = GTK_WINDOW(top);
236 if (window) gtk_window_set_transient_for(GTK_WINDOW(gd->dialog), window);
239 g_signal_connect(G_OBJECT(gd->dialog), "delete_event",
240 G_CALLBACK(generic_dialog_delete_cb), gd);
241 g_signal_connect(G_OBJECT(gd->dialog), "key_press_event",
242 G_CALLBACK(generic_dialog_key_press_cb), gd);
244 gtk_window_set_resizable(GTK_WINDOW(gd->dialog), TRUE);
245 gtk_window_set_title(GTK_WINDOW (gd->dialog), title);
246 gtk_container_set_border_width(GTK_CONTAINER(gd->dialog), PREF_PAD_BORDER);
248 vbox = gtk_vbox_new(FALSE, PREF_PAD_BUTTON_SPACE);
249 gtk_container_add(GTK_CONTAINER(gd->dialog), vbox);
250 gtk_widget_show(vbox);
252 gd->vbox = gtk_vbox_new(FALSE, PREF_PAD_GAP);
253 gtk_box_pack_start(GTK_BOX(vbox), gd->vbox, TRUE, TRUE, 0);
254 gtk_widget_show(gd->vbox);
256 gd->hbox = gtk_hbutton_box_new();
257 gtk_button_box_set_layout(GTK_BUTTON_BOX(gd->hbox), GTK_BUTTONBOX_END);
258 gtk_box_set_spacing(GTK_BOX(gd->hbox), PREF_PAD_BUTTON_GAP);
259 gtk_box_pack_start(GTK_BOX(vbox), gd->hbox, FALSE, FALSE, 0);
260 gtk_widget_show(gd->hbox);
264 gd->cancel_button = generic_dialog_add_button(gd, GTK_STOCK_CANCEL, NULL, gd->cancel_cb, TRUE);
268 gd->cancel_button = NULL;
271 if (generic_dialog_get_alternative_button_order(gd->hbox))
273 g_signal_connect(G_OBJECT(gd->dialog), "show",
274 G_CALLBACK(generic_dialog_show_cb), gd);
277 gd->default_cb = NULL;
280 GenericDialog *generic_dialog_new(const gchar *title,
281 const gchar *wmclass, const gchar *wmsubclass,
282 GtkWidget *parent, gint auto_close,
283 void (*cancel_cb)(GenericDialog *, gpointer), gpointer data)
287 gd = g_new0(GenericDialog, 1);
288 generic_dialog_setup(gd, title, wmclass, wmsubclass,
289 parent, auto_close, cancel_cb, data);
293 *-----------------------------------------------------------------------------
294 * simple warning dialog
295 *-----------------------------------------------------------------------------
298 static void warning_dialog_ok_cb(GenericDialog *gd, gpointer data)
303 GenericDialog *warning_dialog(const gchar *heading, const gchar *text,
304 const gchar *icon_stock_id, GtkWidget *parent)
308 gd = generic_dialog_new(heading, PACKAGE, "warning", parent, TRUE, NULL, NULL);
309 generic_dialog_add_button(gd, GTK_STOCK_OK, NULL, warning_dialog_ok_cb, TRUE);
311 generic_dialog_add_message(gd, icon_stock_id, heading, text);
313 gtk_widget_show(gd->dialog);
319 *-----------------------------------------------------------------------------
320 * generic file ops dialog routines
321 *-----------------------------------------------------------------------------
324 void file_dialog_close(FileDialog *fd)
326 g_free(fd->source_path);
327 g_free(fd->dest_path);
328 if (fd->source_list) path_list_free(fd->source_list);
330 generic_dialog_close(GENERIC_DIALOG(fd));
333 FileDialog *file_dialog_new(const gchar *title,
334 const gchar *wmclass, const gchar *wmsubclass,
336 void (*cancel_cb)(FileDialog *, gpointer), gpointer data)
338 FileDialog *fd = NULL;
340 fd = g_new0(FileDialog, 1);
342 generic_dialog_setup(GENERIC_DIALOG(fd), title,
343 wmclass, wmsubclass, parent, FALSE,
344 (void *)cancel_cb, data);
349 GtkWidget *file_dialog_add_button(FileDialog *fd, const gchar *stock_id, const gchar *text,
350 void (*func_cb)(FileDialog *, gpointer), gint is_default)
352 return generic_dialog_add_button(GENERIC_DIALOG(fd), stock_id, text,
353 (void *)func_cb, is_default);
356 static void file_dialog_entry_cb(GtkWidget *widget, gpointer data)
358 FileDialog *fd = data;
359 g_free(fd->dest_path);
360 fd->dest_path = remove_trailing_slash(gtk_entry_get_text(GTK_ENTRY(fd->entry)));
363 static void file_dialog_entry_enter_cb(const gchar *path, gpointer data)
365 GenericDialog *gd = data;
367 file_dialog_entry_cb(NULL, data);
369 if (gd->default_cb) gd->default_cb(gd, gd->data);
372 void file_dialog_add_path_widgets(FileDialog *fd, const gchar *default_path, const gchar *path,
373 const gchar *history_key, const gchar *filter, const gchar *filter_desc)
378 if (fd->entry) return;
380 tabcomp = tab_completion_new_with_history(&fd->entry, NULL,
381 history_key, -1, file_dialog_entry_enter_cb, fd);
382 gtk_box_pack_end(GTK_BOX(GENERIC_DIALOG(fd)->vbox), tabcomp, FALSE, FALSE, 0);
383 generic_dialog_attach_default(GENERIC_DIALOG(fd), fd->entry);
384 gtk_widget_show(tabcomp);
386 if (path && path[0] == '/')
388 fd->dest_path = g_strdup(path);
394 base = tab_completion_set_to_last_history(fd->entry);
396 if (!base) base = default_path;
397 if (!base) base = homedir();
401 fd->dest_path = concat_dir_and_file(base, path);
405 fd->dest_path = g_strdup(base);
409 list = path_selection_new_with_files(fd->entry, fd->dest_path, filter, filter_desc);
410 path_selection_add_select_func(fd->entry, file_dialog_entry_enter_cb, fd);
411 gtk_box_pack_end(GTK_BOX(GENERIC_DIALOG(fd)->vbox), list, TRUE, TRUE, 0);
412 gtk_widget_show(list);
414 gtk_widget_grab_focus(fd->entry);
417 gtk_entry_set_text(GTK_ENTRY(fd->entry), fd->dest_path);
418 gtk_editable_set_position(GTK_EDITABLE(fd->entry), strlen(fd->dest_path));
421 g_signal_connect(G_OBJECT(fd->entry), "changed",
422 G_CALLBACK(file_dialog_entry_cb), fd);
425 void file_dialog_add_filter(FileDialog *fd, const gchar *filter, const gchar *filter_desc, gint set)
427 if (!fd->entry) return;
428 path_selection_add_filter(fd->entry, filter, filter_desc, set);
431 void file_dialog_clear_filter(FileDialog *fd)
433 if (!fd->entry) return;
434 path_selection_clear_filter(fd->entry);
437 void file_dialog_sync_history(FileDialog *fd, gint dir_only)
439 if (!fd->dest_path) return;
442 (dir_only && isdir(fd->dest_path)) )
444 tab_completion_append_to_history(fd->entry, fd->dest_path);
448 gchar *buf = remove_level_from_path(fd->dest_path);
449 tab_completion_append_to_history(fd->entry, buf);