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