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