Fix #543: Size of dialog windows in the GTK3 version is too small
[geeqie.git] / src / ui_utildlg.c
1 /*
2  * Copyright (C) 2004 John Ellis
3  * Copyright (C) 2008 - 2016 The Geeqie Team
4  *
5  * Author: John Ellis
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #  include "config.h"
24 #endif
25 #include "intl.h"
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/types.h>
31 #include <gtk/gtk.h>
32
33 #include <gdk/gdkkeysyms.h> /* for keyboard values */
34
35 #include "main.h"
36 #include "ui_utildlg.h"
37
38 #include "filedata.h"
39 #include "rcfile.h"
40 #include "ui_fileops.h"
41 #include "ui_misc.h"
42 #include "ui_pathsel.h"
43 #include "ui_tabcomp.h"
44 #include "window.h"
45
46 /*
47  *-----------------------------------------------------------------------------
48  * generic dialog
49  *-----------------------------------------------------------------------------
50  */
51
52 typedef struct _DialogWindow DialogWindow;
53 struct _DialogWindow
54 {
55         gint x;
56         gint y;
57         gint w;
58         gint h;
59         gchar *title;
60         gchar *role;
61 };
62
63 static GList *dialog_windows = NULL;
64
65 static void generic_dialog_save_window(const gchar *title, const gchar *role, gint x, gint y, gint h, gint w)
66 {
67         GList *work;
68
69         work = g_list_first(dialog_windows);
70         while (work)
71                 {
72                 DialogWindow *dw = work->data;
73                 if (g_strcmp0(dw->title ,title) == 0 && g_strcmp0(dw->role, role) == 0)
74                         {
75                         dw->x = x;
76                         dw->y = y;
77                         dw->w = w;
78                         dw->h = h;
79                         return;
80                         }
81                 work = work->next;
82                 }
83
84         DialogWindow *dw = g_new0(DialogWindow, 1);
85         dw->title = g_strdup(title);
86         dw->role = g_strdup(role);
87         dw->x = x;
88         dw->y = y;
89         dw->w = w;
90         dw->h = h;
91
92         dialog_windows = g_list_append(dialog_windows, dw);
93 }
94
95 static gboolean generic_dialog_find_window(const gchar *title, const gchar *role, gint *x, gint *y, gint *h, gint *w)
96 {
97         GList *work;
98
99         work = g_list_first(dialog_windows);
100         while (work)
101                 {
102                 DialogWindow *dw = work->data;
103
104                 if (g_strcmp0(dw->title,title) == 0 && g_strcmp0(dw->role, role) == 0)
105                         {
106                         *x = dw->x;
107                         *y = dw->y;
108                         *w = dw->w;
109                         *h = dw->h;
110                         return TRUE;
111                         }
112                 work = work->next;
113                 }
114         return FALSE;
115 }
116
117 void generic_dialog_close(GenericDialog *gd)
118 {
119         gchar *ident_string;
120         gchar *full_title;
121         gchar *actual_title;
122         gint x, y, h, w;
123
124         gdk_window_get_root_origin(gtk_widget_get_window (gd->dialog), &x, &y);
125         w = gdk_window_get_width(gtk_widget_get_window (gd->dialog));
126         h = gdk_window_get_height(gtk_widget_get_window (gd->dialog));
127
128         /* The window title is modified in window.c: window_new()
129          * by appending the string " - Geeqie"
130          */
131         ident_string = g_strconcat(" - ", GQ_APPNAME, NULL);
132         full_title = g_strdup(gtk_window_get_title(GTK_WINDOW(gd->dialog)));
133         actual_title = strndup(full_title, g_strrstr(full_title, ident_string) - full_title);
134
135         generic_dialog_save_window(actual_title, gtk_window_get_role(GTK_WINDOW(gd->dialog)), x, y, w, h);
136
137         gtk_widget_destroy(gd->dialog);
138         g_free(gd);
139         g_free(ident_string);
140         g_free(full_title);
141         g_free(actual_title);
142 }
143
144 static void generic_dialog_click_cb(GtkWidget *widget, gpointer data)
145 {
146         GenericDialog *gd = data;
147         void (*func)(GenericDialog *, gpointer);
148         gboolean auto_close;
149
150         func = g_object_get_data(G_OBJECT(widget), "dialog_function");
151         auto_close = gd->auto_close;
152
153         if (func) func(gd, gd->data);
154         if (auto_close) generic_dialog_close(gd);
155 }
156
157 static gboolean generic_dialog_default_key_press_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
158 {
159         GenericDialog *gd = data;
160
161         if (event->keyval == GDK_KEY_Return && gtk_widget_has_focus(widget)
162             && gd->default_cb)
163                 {
164                 gboolean auto_close;
165
166                 auto_close = gd->auto_close;
167                 gd->default_cb(gd, gd->data);
168                 if (auto_close) generic_dialog_close(gd);
169
170                 return TRUE;
171                 }
172         return FALSE;
173 }
174
175 void generic_dialog_attach_default(GenericDialog *gd, GtkWidget *widget)
176 {
177         if (!gd || !widget) return;
178         g_signal_connect(G_OBJECT(widget), "key_press_event",
179                          G_CALLBACK(generic_dialog_default_key_press_cb), gd);
180 }
181
182 static gboolean generic_dialog_key_press_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
183 {
184         GenericDialog *gd = data;
185         gboolean auto_close = gd->auto_close;
186
187         if (event->keyval == GDK_KEY_Escape)
188                 {
189                 if (gd->cancel_cb) gd->cancel_cb(gd, gd->data);
190                 else if (auto_close) generic_dialog_click_cb(widget, data);
191                 return TRUE;
192                 }
193         return FALSE;
194 }
195
196 static gboolean generic_dialog_delete_cb(GtkWidget *w, GdkEventAny *event, gpointer data)
197 {
198         GenericDialog *gd = data;
199         gboolean auto_close;
200
201         auto_close = gd->auto_close;
202
203         if (gd->cancel_cb) gd->cancel_cb(gd, gd->data);
204         if (auto_close) generic_dialog_close(gd);
205
206         return TRUE;
207 }
208
209 static void generic_dialog_show_cb(GtkWidget *widget, gpointer data)
210 {
211         GenericDialog *gd = data;
212         if (gd->cancel_button)
213                 {
214                 gtk_box_reorder_child(GTK_BOX(gd->hbox), gd->cancel_button, -1);
215                 }
216
217         g_signal_handlers_disconnect_by_func(G_OBJECT(widget),
218                                              G_CALLBACK(generic_dialog_show_cb), gd);
219 }
220
221 gboolean generic_dialog_get_alternative_button_order(GtkWidget *widget)
222 {
223         GtkSettings *settings;
224         GObjectClass *klass;
225         gboolean alternative_order = FALSE;
226
227         settings = gtk_settings_get_for_screen(gtk_widget_get_screen(widget));
228         klass = G_OBJECT_CLASS(GTK_SETTINGS_GET_CLASS(settings));
229         if (g_object_class_find_property(klass, "gtk-alternative-button-order"))
230                 {
231                 g_object_get(settings, "gtk-alternative-button-order", &alternative_order, NULL);
232                 }
233
234         return alternative_order;
235 }
236
237 GtkWidget *generic_dialog_add_button(GenericDialog *gd, const gchar *stock_id, const gchar *text,
238                                      void (*func_cb)(GenericDialog *, gpointer), gboolean is_default)
239 {
240         GtkWidget *button;
241         gboolean alternative_order;
242
243         button = pref_button_new(NULL, stock_id, text, FALSE,
244                                  G_CALLBACK(generic_dialog_click_cb), gd);
245
246         gtk_widget_set_can_default(button, TRUE);
247         g_object_set_data(G_OBJECT(button), "dialog_function", func_cb);
248
249         gtk_container_add(GTK_CONTAINER(gd->hbox), button);
250
251         alternative_order = generic_dialog_get_alternative_button_order(gd->hbox);
252
253         if (is_default)
254                 {
255                 gtk_widget_grab_default(button);
256                 gtk_widget_grab_focus(button);
257                 gd->default_cb = func_cb;
258
259                 if (!alternative_order) gtk_box_reorder_child(GTK_BOX(gd->hbox), button, -1);
260                 }
261         else
262                 {
263                 if (!alternative_order) gtk_box_reorder_child(GTK_BOX(gd->hbox), button, 0);
264                 }
265
266         gtk_widget_show(button);
267
268         return button;
269 }
270
271 /**
272  * @brief 
273  * @param gd 
274  * @param icon_stock_id 
275  * @param heading 
276  * @param text 
277  * @param expand Used as the "expand" and "fill" parameters in the eventual call to gtk_box_pack_start() 
278  * @returns 
279  * 
280  * 
281  */
282 GtkWidget *generic_dialog_add_message(GenericDialog *gd, const gchar *icon_stock_id,
283                                       const gchar *heading, const gchar *text, gboolean expand)
284 {
285         GtkWidget *hbox;
286         GtkWidget *vbox;
287         GtkWidget *label;
288
289         hbox = pref_box_new(gd->vbox, expand, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
290         if (icon_stock_id)
291                 {
292                 GtkWidget *image;
293
294                 image = gtk_image_new_from_stock(icon_stock_id, GTK_ICON_SIZE_DIALOG);
295                 gtk_misc_set_alignment(GTK_MISC(image), 0.5, 0.0);
296                 gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 0);
297                 gtk_widget_show(image);
298                 }
299
300         vbox = pref_box_new(hbox, TRUE, GTK_ORIENTATION_VERTICAL, PREF_PAD_SPACE);
301         if (heading)
302                 {
303                 label = pref_label_new(vbox, heading);
304                 pref_label_bold(label, TRUE, TRUE);
305                 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
306                 }
307         if (text)
308                 {
309                 label = pref_label_new(vbox, text);
310                 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
311                 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
312                 }
313
314         return vbox;
315 }
316
317 void generic_dialog_windows_load_config(const gchar **attribute_names, const gchar **attribute_values)
318 {
319         DialogWindow *dw =  g_new0(DialogWindow, 1);
320         gchar *title = NULL;
321         gchar *role = NULL;
322         gint x = 0;
323         gint y = 0;
324         gint w = 0;
325         gint h = 0;
326
327         while (*attribute_names)
328                 {
329                 const gchar *option = *attribute_names++;
330                 const gchar *value = *attribute_values++;
331                 if (READ_CHAR_FULL("title", title)) continue;
332                 if (READ_CHAR_FULL("role", role)) continue;
333                 if (READ_INT_FULL("x", x)) continue;
334                 if (READ_INT_FULL("y", y)) continue;
335                 if (READ_INT_FULL("w", w)) continue;
336                 if (READ_INT_FULL("h", h)) continue;
337
338                 log_printf("unknown attribute %s = %s\n", option, value);
339                 }
340
341         if (title && title[0] != 0)
342                 {
343                 dw->title = g_strdup(title);
344                 dw->role = g_strdup(role);
345                 dw->x = x;
346                 dw->y = y;
347                 dw->w = w;
348                 dw->h = h;
349
350                 dialog_windows = g_list_append(dialog_windows, dw);
351                 }
352 }
353
354 void generic_dialog_windows_write_config(GString *outstr, gint indent)
355 {
356         GList *work;
357
358         WRITE_NL(); WRITE_STRING("<%s>", "dialogs");
359         indent++;
360
361         work = g_list_first(dialog_windows);
362         while (work)
363                 {
364                 DialogWindow *dw = work->data;
365                 WRITE_NL(); WRITE_STRING("<window ");
366                 write_char_option(outstr, indent + 1, "title", dw->title);
367                 write_char_option(outstr, indent + 1, "role", dw->role);
368                 WRITE_INT(*dw, x);
369                 WRITE_INT(*dw, y);
370                 WRITE_INT(*dw, w);
371                 WRITE_INT(*dw, h);
372                 WRITE_STRING("/>");
373                 work = work->next;
374                 }
375         indent--;
376         WRITE_NL(); WRITE_STRING("</%s>", "dialogs");
377 }
378
379 static void generic_dialog_setup(GenericDialog *gd,
380                                  const gchar *title,
381                                  const gchar *role,
382                                  GtkWidget *parent, gboolean auto_close,
383                                  void (*cancel_cb)(GenericDialog *, gpointer), gpointer data)
384 {
385         GtkWidget *vbox;
386         gint x, y, w, h;
387
388         gd->auto_close = auto_close;
389         gd->data = data;
390         gd->cancel_cb = cancel_cb;
391
392         gd->dialog = window_new(GTK_WINDOW_TOPLEVEL, role, NULL, NULL, title);
393         gtk_window_set_type_hint(GTK_WINDOW(gd->dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
394
395         if (options->save_dialog_window_positions)
396                 {
397                 if (generic_dialog_find_window(title, role, &x, &y, &w, &h))
398                         {
399                         gtk_window_set_default_size(GTK_WINDOW(gd->dialog), w, h);
400                         gtk_window_move(GTK_WINDOW(gd->dialog), x, y);
401                         }
402                 }
403
404         if (parent)
405                 {
406                 GtkWindow *window = NULL;
407
408                 if (GTK_IS_WINDOW(parent))
409                         {
410                         window = GTK_WINDOW(parent);
411                         }
412                 else
413                         {
414                         GtkWidget *top;
415
416                         top = gtk_widget_get_toplevel(parent);
417                         if (GTK_IS_WINDOW(top) && gtk_widget_is_toplevel(top)) window = GTK_WINDOW(top);
418                         }
419
420                 if (window) gtk_window_set_transient_for(GTK_WINDOW(gd->dialog), window);
421                 }
422
423         g_signal_connect(G_OBJECT(gd->dialog), "delete_event",
424                          G_CALLBACK(generic_dialog_delete_cb), gd);
425         g_signal_connect(G_OBJECT(gd->dialog), "key_press_event",
426                          G_CALLBACK(generic_dialog_key_press_cb), gd);
427
428         gtk_window_set_resizable(GTK_WINDOW(gd->dialog), TRUE);
429         gtk_container_set_border_width(GTK_CONTAINER(gd->dialog), PREF_PAD_BORDER);
430
431         vbox = gtk_vbox_new(FALSE, PREF_PAD_BUTTON_SPACE);
432         gtk_container_add(GTK_CONTAINER(gd->dialog), vbox);
433         gtk_widget_show(vbox);
434
435         gd->vbox = gtk_vbox_new(FALSE, PREF_PAD_GAP);
436         gtk_box_pack_start(GTK_BOX(vbox), gd->vbox, TRUE, TRUE, 0);
437         gtk_widget_show(gd->vbox);
438
439         gd->hbox = gtk_hbutton_box_new();
440         gtk_button_box_set_layout(GTK_BUTTON_BOX(gd->hbox), GTK_BUTTONBOX_END);
441         gtk_box_set_spacing(GTK_BOX(gd->hbox), PREF_PAD_BUTTON_GAP);
442         gtk_box_pack_start(GTK_BOX(vbox), gd->hbox, FALSE, FALSE, 0);
443         gtk_widget_show(gd->hbox);
444
445         if (gd->cancel_cb)
446                 {
447                 gd->cancel_button = generic_dialog_add_button(gd, GTK_STOCK_CANCEL, NULL, gd->cancel_cb, TRUE);
448                 }
449         else
450                 {
451                 gd->cancel_button = NULL;
452                 }
453
454         if (generic_dialog_get_alternative_button_order(gd->hbox))
455                 {
456                 g_signal_connect(G_OBJECT(gd->dialog), "show",
457                                  G_CALLBACK(generic_dialog_show_cb), gd);
458                 }
459
460         gd->default_cb = NULL;
461 }
462
463 GenericDialog *generic_dialog_new(const gchar *title,
464                                   const gchar *role,
465                                   GtkWidget *parent, gboolean auto_close,
466                                   void (*cancel_cb)(GenericDialog *, gpointer), gpointer data)
467 {
468         GenericDialog *gd;
469
470         gd = g_new0(GenericDialog, 1);
471         generic_dialog_setup(gd, title, role,
472                              parent, auto_close, cancel_cb, data);
473         return gd;
474 }
475 /*
476  *-----------------------------------------------------------------------------
477  * simple warning dialog
478  *-----------------------------------------------------------------------------
479  */
480
481 static void warning_dialog_ok_cb(GenericDialog *gd, gpointer data)
482 {
483         /* no op */
484 }
485
486 GenericDialog *warning_dialog(const gchar *heading, const gchar *text,
487                               const gchar *icon_stock_id, GtkWidget *parent)
488 {
489         GenericDialog *gd;
490
491         gd = generic_dialog_new(heading, "warning", parent, TRUE, NULL, NULL);
492         generic_dialog_add_button(gd, GTK_STOCK_OK, NULL, warning_dialog_ok_cb, TRUE);
493
494         generic_dialog_add_message(gd, icon_stock_id, heading, text, TRUE);
495
496         gtk_widget_show(gd->dialog);
497
498         return gd;
499 }
500
501 /*
502  *-----------------------------------------------------------------------------
503  * generic file ops dialog routines
504  *-----------------------------------------------------------------------------
505  */
506
507 void file_dialog_close(FileDialog *fdlg)
508 {
509         file_data_unref(fdlg->source_fd);
510         g_free(fdlg->dest_path);
511         if (fdlg->source_list) filelist_free(fdlg->source_list);
512
513         generic_dialog_close(GENERIC_DIALOG(fdlg));
514 }
515
516 FileDialog *file_dialog_new(const gchar *title,
517                             const gchar *role,
518                             GtkWidget *parent,
519                             void (*cancel_cb)(FileDialog *, gpointer), gpointer data)
520 {
521         FileDialog *fdlg = NULL;
522
523         fdlg = g_new0(FileDialog, 1);
524
525         generic_dialog_setup(GENERIC_DIALOG(fdlg), title,
526                              role, parent, FALSE,
527                              (gpointer)cancel_cb, data);
528
529         return fdlg;
530 }
531
532 GtkWidget *file_dialog_add_button(FileDialog *fdlg, const gchar *stock_id, const gchar *text,
533                                   void (*func_cb)(FileDialog *, gpointer), gboolean is_default)
534 {
535         return generic_dialog_add_button(GENERIC_DIALOG(fdlg), stock_id, text,
536                                          (gpointer)func_cb, is_default);
537 }
538
539 static void file_dialog_entry_cb(GtkWidget *widget, gpointer data)
540 {
541         FileDialog *fdlg = data;
542         g_free(fdlg->dest_path);
543         fdlg->dest_path = remove_trailing_slash(gtk_entry_get_text(GTK_ENTRY(fdlg->entry)));
544 }
545
546 static void file_dialog_entry_enter_cb(const gchar *path, gpointer data)
547 {
548         GenericDialog *gd = data;
549
550         file_dialog_entry_cb(NULL, data);
551
552         if (gd->default_cb) gd->default_cb(gd, gd->data);
553 }
554
555 void file_dialog_add_path_widgets(FileDialog *fdlg, const gchar *default_path, const gchar *path,
556                                   const gchar *history_key, const gchar *filter, const gchar *filter_desc)
557 {
558         GtkWidget *tabcomp;
559         GtkWidget *list;
560
561         if (fdlg->entry) return;
562
563         tabcomp = tab_completion_new_with_history(&fdlg->entry, NULL,
564                   history_key, -1, file_dialog_entry_enter_cb, fdlg);
565         gtk_box_pack_end(GTK_BOX(GENERIC_DIALOG(fdlg)->vbox), tabcomp, FALSE, FALSE, 0);
566         generic_dialog_attach_default(GENERIC_DIALOG(fdlg), fdlg->entry);
567         gtk_widget_show(tabcomp);
568
569         if (path && path[0] == G_DIR_SEPARATOR)
570                 {
571                 fdlg->dest_path = g_strdup(path);
572                 }
573         else
574                 {
575                 const gchar *base;
576
577                 base = tab_completion_set_to_last_history(fdlg->entry);
578
579                 if (!base) base = default_path;
580                 if (!base) base = homedir();
581
582                 if (path)
583                         {
584                         fdlg->dest_path = g_build_filename(base, path, NULL);
585                         }
586                 else
587                         {
588                         fdlg->dest_path = g_strdup(base);
589                         }
590                 }
591
592         list = path_selection_new_with_files(fdlg->entry, fdlg->dest_path, filter, filter_desc);
593         path_selection_add_select_func(fdlg->entry, file_dialog_entry_enter_cb, fdlg);
594         gtk_box_pack_end(GTK_BOX(GENERIC_DIALOG(fdlg)->vbox), list, TRUE, TRUE, 0);
595         gtk_widget_show(list);
596
597         gtk_widget_grab_focus(fdlg->entry);
598         if (fdlg->dest_path)
599                 {
600                 gtk_entry_set_text(GTK_ENTRY(fdlg->entry), fdlg->dest_path);
601                 gtk_editable_set_position(GTK_EDITABLE(fdlg->entry), strlen(fdlg->dest_path));
602                 }
603
604         g_signal_connect(G_OBJECT(fdlg->entry), "changed",
605                          G_CALLBACK(file_dialog_entry_cb), fdlg);
606 }
607
608 void file_dialog_add_filter(FileDialog *fdlg, const gchar *filter, const gchar *filter_desc, gboolean set)
609 {
610         if (!fdlg->entry) return;
611         path_selection_add_filter(fdlg->entry, filter, filter_desc, set);
612 }
613
614 void file_dialog_clear_filter(FileDialog *fdlg)
615 {
616         if (!fdlg->entry) return;
617         path_selection_clear_filter(fdlg->entry);
618 }
619
620 void file_dialog_sync_history(FileDialog *fdlg, gboolean dir_only)
621 {
622         if (!fdlg->dest_path) return;
623
624         if (!dir_only ||
625             (dir_only && isdir(fdlg->dest_path)) )
626                 {
627                 tab_completion_append_to_history(fdlg->entry, fdlg->dest_path);
628                 }
629         else
630                 {
631                 gchar *buf = remove_level_from_path(fdlg->dest_path);
632                 tab_completion_append_to_history(fdlg->entry, buf);
633                 g_free(buf);
634                 }
635 }
636 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */