Mercurial > geeqie.yaz
annotate src/ui_utildlg.c @ 90:dc3c77d027e6
Tue Oct 31 18:06:42 2006 John Ellis <johne@verizon.net>
* info.c: Increase default info window size to 600x400.
* po/be.po: Update Belarusian translation,
submitted by Pavel Piatruk <berserker@neolocation.com>.
* gqview.desktop: Add additional formats to MimeType list.
author | gqview |
---|---|
date | Tue, 31 Oct 2006 23:20:48 +0000 |
parents | 3263965d5f9e |
children | 71e1ebee420e |
rev | line source |
---|---|
9 | 1 /* |
2 * (SLIK) SimpLIstic sKin functions | |
3 * (C) 2004 John Ellis | |
4 * | |
5 * Author: John Ellis | |
6 * | |
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! | |
10 */ | |
11 | |
12 #ifdef HAVE_CONFIG_H | |
13 # include "config.h" | |
14 #endif | |
15 #include "intl.h" | |
16 | |
17 #include <stdio.h> | |
18 #include <stdlib.h> | |
19 #include <string.h> | |
20 #include <sys/types.h> | |
21 #include <gtk/gtk.h> | |
22 | |
23 #include <gdk/gdkkeysyms.h> /* for keyboard values */ | |
24 | |
25 #include "ui_utildlg.h" | |
26 | |
27 #include "ui_fileops.h" | |
28 #include "ui_misc.h" | |
29 #include "ui_pathsel.h" | |
30 #include "ui_tabcomp.h" | |
31 | |
32 | |
33 /* | |
34 *----------------------------------------------------------------------------- | |
35 * generic dialog | |
36 *----------------------------------------------------------------------------- | |
37 */ | |
38 | |
39 void generic_dialog_close(GenericDialog *gd) | |
40 { | |
41 gtk_widget_destroy(gd->dialog); | |
42 g_free(gd); | |
43 } | |
44 | |
45 static void generic_dialog_click_cb(GtkWidget *widget, gpointer data) | |
46 { | |
47 GenericDialog *gd = data; | |
48 void (*func)(GenericDialog *, gpointer); | |
49 gint auto_close; | |
50 | |
51 func = g_object_get_data(G_OBJECT(widget), "dialog_function"); | |
52 auto_close = gd->auto_close; | |
53 | |
54 if (func) func(gd, gd->data); | |
55 if (auto_close) generic_dialog_close(gd); | |
56 } | |
57 | |
58 static gint generic_dialog_default_key_press_cb(GtkWidget *widget, GdkEventKey *event, gpointer data) | |
59 { | |
60 GenericDialog *gd = data; | |
61 | |
62 if (event->keyval == GDK_Return && GTK_WIDGET_HAS_FOCUS(widget) | |
63 && gd->default_cb) | |
64 { | |
65 gint auto_close; | |
66 | |
67 auto_close = gd->auto_close; | |
68 gd->default_cb(gd, gd->data); | |
69 if (auto_close) generic_dialog_close(gd); | |
70 | |
71 return TRUE; | |
72 } | |
73 return FALSE; | |
74 } | |
75 | |
76 void generic_dialog_attach_default(GenericDialog *gd, GtkWidget *widget) | |
77 { | |
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); | |
81 } | |
82 | |
83 static gint generic_dialog_key_press_cb(GtkWidget *widget, GdkEventKey *event, gpointer data) | |
84 { | |
85 GenericDialog *gd = data; | |
86 | |
87 if (event->keyval == GDK_Escape) | |
88 { | |
89 if (gd->cancel_cb) gd->cancel_cb(gd, gd->data); | |
90 if (gd->auto_close) generic_dialog_click_cb(widget, data); | |
91 return TRUE; | |
92 } | |
93 return FALSE; | |
94 } | |
95 | |
96 static gint generic_dialog_delete_cb(GtkWidget *w, GdkEventAny *event, gpointer data) | |
97 { | |
98 GenericDialog *gd = data; | |
99 gint auto_close; | |
100 | |
101 auto_close = gd->auto_close; | |
102 | |
103 if (gd->cancel_cb) gd->cancel_cb(gd, gd->data); | |
104 if (auto_close) generic_dialog_close(gd); | |
105 | |
106 return TRUE; | |
107 } | |
108 | |
109 static void generic_dialog_show_cb(GtkWidget *widget, gpointer data) | |
110 { | |
111 GenericDialog *gd = data; | |
112 if (gd->cancel_button) | |
113 { | |
114 gtk_box_reorder_child(GTK_BOX(gd->hbox), gd->cancel_button, -1); | |
115 } | |
116 | |
117 g_signal_handlers_disconnect_by_func(G_OBJECT(widget), | |
118 G_CALLBACK(generic_dialog_show_cb), gd); | |
119 } | |
120 | |
121 gint generic_dialog_get_alternative_button_order(GtkWidget *widget) | |
122 { | |
123 GtkSettings *settings; | |
124 GObjectClass *klass; | |
125 gint alternative_order = FALSE; | |
126 | |
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")) | |
130 { | |
131 g_object_get(settings, "gtk-alternative-button-order", &alternative_order, NULL); | |
132 } | |
133 | |
134 return alternative_order; | |
135 } | |
136 | |
137 GtkWidget *generic_dialog_add_button(GenericDialog *gd, const gchar *stock_id, const gchar *text, | |
138 void (*func_cb)(GenericDialog *, gpointer), gint is_default) | |
139 { | |
140 GtkWidget *button; | |
141 gint alternative_order; | |
142 | |
143 button = pref_button_new(NULL, stock_id, text, FALSE, | |
144 G_CALLBACK(generic_dialog_click_cb), gd); | |
145 | |
146 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT); | |
147 g_object_set_data(G_OBJECT(button), "dialog_function", func_cb); | |
148 | |
149 gtk_container_add(GTK_CONTAINER(gd->hbox), button); | |
150 | |
151 alternative_order = generic_dialog_get_alternative_button_order(gd->hbox); | |
152 | |
153 if (is_default) | |
154 { | |
155 gtk_widget_grab_default(button); | |
156 gtk_widget_grab_focus(button); | |
157 gd->default_cb = func_cb; | |
158 | |
159 if (!alternative_order) gtk_box_reorder_child(GTK_BOX(gd->hbox), button, -1); | |
160 } | |
161 else | |
162 { | |
163 if (!alternative_order) gtk_box_reorder_child(GTK_BOX(gd->hbox), button, 0); | |
164 } | |
165 | |
166 gtk_widget_show(button); | |
167 | |
168 return button; | |
169 } | |
170 | |
171 GtkWidget *generic_dialog_add_message(GenericDialog *gd, const gchar *icon_stock_id, | |
172 const gchar *heading, const gchar *text) | |
173 { | |
174 GtkWidget *hbox; | |
175 GtkWidget *vbox; | |
176 GtkWidget *label; | |
177 | |
178 hbox = pref_box_new(gd->vbox, FALSE, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE); | |
179 if (icon_stock_id) | |
180 { | |
181 GtkWidget *image; | |
182 | |
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); | |
187 } | |
188 | |
189 vbox = pref_box_new(hbox, TRUE, GTK_ORIENTATION_VERTICAL, PREF_PAD_SPACE); | |
190 if (heading) | |
191 { | |
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); | |
195 } | |
196 if (text) | |
197 { | |
198 label = pref_label_new(vbox, text); | |
199 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5); | |
15
3263965d5f9e
##### Note: GQview CVS on sourceforge is not always up to date, please use #####
gqview
parents:
9
diff
changeset
|
200 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE); |
9 | 201 } |
202 | |
203 return vbox; | |
204 } | |
205 | |
206 static void generic_dialog_setup(GenericDialog *gd, | |
207 const gchar *title, | |
208 const gchar *wmclass, const gchar *wmsubclass, | |
209 GtkWidget *parent, gint auto_close, | |
210 void (*cancel_cb)(GenericDialog *, gpointer), gpointer data) | |
211 { | |
212 GtkWidget *vbox; | |
213 | |
214 gd->auto_close = auto_close; | |
215 gd->data = data; | |
216 gd->cancel_cb = cancel_cb; | |
217 | |
218 gd->dialog = gtk_window_new(GTK_WINDOW_TOPLEVEL); | |
219 gtk_window_set_type_hint(GTK_WINDOW(gd->dialog), GDK_WINDOW_TYPE_HINT_DIALOG); | |
220 gtk_window_set_wmclass(GTK_WINDOW(gd->dialog), wmsubclass, wmclass); | |
221 if (parent) | |
222 { | |
223 GtkWindow *window = NULL; | |
224 | |
225 if (GTK_IS_WINDOW(parent)) | |
226 { | |
227 window = GTK_WINDOW(parent); | |
228 } | |
229 else | |
230 { | |
231 GtkWidget *top; | |
232 | |
233 top = gtk_widget_get_toplevel(parent); | |
234 if (GTK_IS_WINDOW(top) && GTK_WIDGET_TOPLEVEL(top)) window = GTK_WINDOW(top); | |
235 } | |
236 | |
237 if (window) gtk_window_set_transient_for(GTK_WINDOW(gd->dialog), window); | |
238 } | |
239 | |
240 g_signal_connect(G_OBJECT(gd->dialog), "delete_event", | |
241 G_CALLBACK(generic_dialog_delete_cb), gd); | |
242 g_signal_connect(G_OBJECT(gd->dialog), "key_press_event", | |
243 G_CALLBACK(generic_dialog_key_press_cb), gd); | |
244 | |
245 gtk_window_set_resizable(GTK_WINDOW(gd->dialog), TRUE); | |
246 gtk_window_set_title(GTK_WINDOW (gd->dialog), title); | |
247 gtk_container_set_border_width(GTK_CONTAINER(gd->dialog), PREF_PAD_BORDER); | |
248 | |
249 vbox = gtk_vbox_new(FALSE, PREF_PAD_BUTTON_SPACE); | |
250 gtk_container_add(GTK_CONTAINER(gd->dialog), vbox); | |
251 gtk_widget_show(vbox); | |
252 | |
253 gd->vbox = gtk_vbox_new(FALSE, PREF_PAD_GAP); | |
254 gtk_box_pack_start(GTK_BOX(vbox), gd->vbox, TRUE, TRUE, 0); | |
255 gtk_widget_show(gd->vbox); | |
256 | |
257 gd->hbox = gtk_hbutton_box_new(); | |
258 gtk_button_box_set_layout(GTK_BUTTON_BOX(gd->hbox), GTK_BUTTONBOX_END); | |
259 gtk_box_set_spacing(GTK_BOX(gd->hbox), PREF_PAD_BUTTON_GAP); | |
260 gtk_box_pack_start(GTK_BOX(vbox), gd->hbox, FALSE, FALSE, 0); | |
261 gtk_widget_show(gd->hbox); | |
262 | |
263 if (gd->cancel_cb) | |
264 { | |
265 gd->cancel_button = generic_dialog_add_button(gd, GTK_STOCK_CANCEL, NULL, gd->cancel_cb, TRUE); | |
266 } | |
267 else | |
268 { | |
269 gd->cancel_button = NULL; | |
270 } | |
271 | |
272 if (generic_dialog_get_alternative_button_order(gd->hbox)) | |
273 { | |
274 g_signal_connect(G_OBJECT(gd->dialog), "show", | |
275 G_CALLBACK(generic_dialog_show_cb), gd); | |
276 } | |
277 | |
278 gd->default_cb = NULL; | |
279 } | |
280 | |
281 GenericDialog *generic_dialog_new(const gchar *title, | |
282 const gchar *wmclass, const gchar *wmsubclass, | |
283 GtkWidget *parent, gint auto_close, | |
284 void (*cancel_cb)(GenericDialog *, gpointer), gpointer data) | |
285 { | |
286 GenericDialog *gd; | |
287 | |
288 gd = g_new0(GenericDialog, 1); | |
289 generic_dialog_setup(gd, title, wmclass, wmsubclass, | |
290 parent, auto_close, cancel_cb, data); | |
291 return gd; | |
292 } | |
293 /* | |
294 *----------------------------------------------------------------------------- | |
295 * simple warning dialog | |
296 *----------------------------------------------------------------------------- | |
297 */ | |
298 | |
299 static void warning_dialog_ok_cb(GenericDialog *gd, gpointer data) | |
300 { | |
301 /* no op */ | |
302 } | |
303 | |
304 GenericDialog *warning_dialog(const gchar *heading, const gchar *text, | |
305 const gchar *icon_stock_id, GtkWidget *parent) | |
306 { | |
307 GenericDialog *gd; | |
308 | |
309 gd = generic_dialog_new(heading, PACKAGE, "warning", parent, TRUE, NULL, NULL); | |
310 generic_dialog_add_button(gd, GTK_STOCK_OK, NULL, warning_dialog_ok_cb, TRUE); | |
311 | |
312 generic_dialog_add_message(gd, icon_stock_id, heading, text); | |
313 | |
314 gtk_widget_show(gd->dialog); | |
315 | |
316 return gd; | |
317 } | |
318 | |
319 /* | |
320 *----------------------------------------------------------------------------- | |
321 * generic file ops dialog routines | |
322 *----------------------------------------------------------------------------- | |
323 */ | |
324 | |
325 void file_dialog_close(FileDialog *fd) | |
326 { | |
327 g_free(fd->source_path); | |
328 g_free(fd->dest_path); | |
329 if (fd->source_list) path_list_free(fd->source_list); | |
330 | |
331 generic_dialog_close(GENERIC_DIALOG(fd)); | |
332 } | |
333 | |
334 FileDialog *file_dialog_new(const gchar *title, | |
335 const gchar *wmclass, const gchar *wmsubclass, | |
336 GtkWidget *parent, | |
337 void (*cancel_cb)(FileDialog *, gpointer), gpointer data) | |
338 { | |
339 FileDialog *fd = NULL; | |
340 | |
341 fd = g_new0(FileDialog, 1); | |
342 | |
343 generic_dialog_setup(GENERIC_DIALOG(fd), title, | |
344 wmclass, wmsubclass, parent, FALSE, | |
345 (void *)cancel_cb, data); | |
346 | |
347 return fd; | |
348 } | |
349 | |
350 GtkWidget *file_dialog_add_button(FileDialog *fd, const gchar *stock_id, const gchar *text, | |
351 void (*func_cb)(FileDialog *, gpointer), gint is_default) | |
352 { | |
353 return generic_dialog_add_button(GENERIC_DIALOG(fd), stock_id, text, | |
354 (void *)func_cb, is_default); | |
355 } | |
356 | |
357 static void file_dialog_entry_cb(GtkWidget *widget, gpointer data) | |
358 { | |
359 FileDialog *fd = data; | |
360 g_free(fd->dest_path); | |
361 fd->dest_path = remove_trailing_slash(gtk_entry_get_text(GTK_ENTRY(fd->entry))); | |
362 } | |
363 | |
364 static void file_dialog_entry_enter_cb(const gchar *path, gpointer data) | |
365 { | |
366 GenericDialog *gd = data; | |
367 | |
368 file_dialog_entry_cb(NULL, data); | |
369 | |
370 if (gd->default_cb) gd->default_cb(gd, gd->data); | |
371 } | |
372 | |
373 void file_dialog_add_path_widgets(FileDialog *fd, const gchar *default_path, const gchar *path, | |
374 const gchar *history_key, const gchar *filter, const gchar *filter_desc) | |
375 { | |
376 GtkWidget *tabcomp; | |
377 GtkWidget *list; | |
378 | |
379 if (fd->entry) return; | |
380 | |
381 tabcomp = tab_completion_new_with_history(&fd->entry, NULL, | |
382 history_key, -1, file_dialog_entry_enter_cb, fd); | |
383 gtk_box_pack_end(GTK_BOX(GENERIC_DIALOG(fd)->vbox), tabcomp, FALSE, FALSE, 0); | |
384 generic_dialog_attach_default(GENERIC_DIALOG(fd), fd->entry); | |
385 gtk_widget_show(tabcomp); | |
386 | |
387 if (path && path[0] == '/') | |
388 { | |
389 fd->dest_path = g_strdup(path); | |
390 } | |
391 else | |
392 { | |
393 const gchar *base; | |
394 | |
395 base = tab_completion_set_to_last_history(fd->entry); | |
396 | |
397 if (!base) base = default_path; | |
398 if (!base) base = homedir(); | |
399 | |
400 if (path) | |
401 { | |
402 fd->dest_path = concat_dir_and_file(base, path); | |
403 } | |
404 else | |
405 { | |
406 fd->dest_path = g_strdup(base); | |
407 } | |
408 } | |
409 | |
410 list = path_selection_new_with_files(fd->entry, fd->dest_path, filter, filter_desc); | |
411 path_selection_add_select_func(fd->entry, file_dialog_entry_enter_cb, fd); | |
412 gtk_box_pack_end(GTK_BOX(GENERIC_DIALOG(fd)->vbox), list, TRUE, TRUE, 0); | |
413 gtk_widget_show(list); | |
414 | |
415 gtk_widget_grab_focus(fd->entry); | |
416 if (fd->dest_path) | |
417 { | |
418 gtk_entry_set_text(GTK_ENTRY(fd->entry), fd->dest_path); | |
419 gtk_editable_set_position(GTK_EDITABLE(fd->entry), strlen(fd->dest_path)); | |
420 } | |
421 | |
422 g_signal_connect(G_OBJECT(fd->entry), "changed", | |
423 G_CALLBACK(file_dialog_entry_cb), fd); | |
424 } | |
425 | |
426 void file_dialog_add_filter(FileDialog *fd, const gchar *filter, const gchar *filter_desc, gint set) | |
427 { | |
428 if (!fd->entry) return; | |
429 path_selection_add_filter(fd->entry, filter, filter_desc, set); | |
430 } | |
431 | |
432 void file_dialog_clear_filter(FileDialog *fd) | |
433 { | |
434 if (!fd->entry) return; | |
435 path_selection_clear_filter(fd->entry); | |
436 } | |
437 | |
438 void file_dialog_sync_history(FileDialog *fd, gint dir_only) | |
439 { | |
440 if (!fd->dest_path) return; | |
441 | |
442 if (!dir_only || | |
443 (dir_only && isdir(fd->dest_path)) ) | |
444 { | |
445 tab_completion_append_to_history(fd->entry, fd->dest_path); | |
446 } | |
447 else | |
448 { | |
449 gchar *buf = remove_level_from_path(fd->dest_path); | |
450 tab_completion_append_to_history(fd->entry, buf); | |
451 g_free(buf); | |
452 } | |
453 } | |
454 | |
455 |