Fix progress bar text
[geeqie.git] / src / layout.c
1 /*
2  * Copyright (C) 2006 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 "main.h"
23 #include "layout.h"
24
25 #include "color-man.h"
26 #include "filedata.h"
27 #include "histogram.h"
28 #include "image.h"
29 #include "image-overlay.h"
30 #include "layout_config.h"
31 #include "layout_image.h"
32 #include "layout_util.h"
33 #include "menu.h"
34 #include "pixbuf-renderer.h"
35 #include "pixbuf_util.h"
36 #include "utilops.h"
37 #include "view_dir.h"
38 #include "view_file.h"
39 #include "ui_fileops.h"
40 #include "ui_menu.h"
41 #include "ui_misc.h"
42 #include "ui_tabcomp.h"
43 #include "window.h"
44 #include "metadata.h"
45 #include "rcfile.h"
46 #include "bar.h"
47 #include "bar_sort.h"
48 #include "preferences.h"
49
50 #ifdef HAVE_LIRC
51 #include "lirc.h"
52 #endif
53
54 #define MAINWINDOW_DEF_WIDTH 700
55 #define MAINWINDOW_DEF_HEIGHT 500
56
57 #define MAIN_WINDOW_DIV_HPOS (MAINWINDOW_DEF_WIDTH / 2)
58 #define MAIN_WINDOW_DIV_VPOS (MAINWINDOW_DEF_HEIGHT / 2)
59
60 #define TOOLWINDOW_DEF_WIDTH 260
61 #define TOOLWINDOW_DEF_HEIGHT 450
62
63 #define PROGRESS_WIDTH 150
64 #define ZOOM_LABEL_WIDTH 64
65
66 #define PANE_DIVIDER_SIZE 10
67
68
69 GList *layout_window_list = NULL;
70 LayoutWindow *current_lw = NULL;
71
72 static void layout_list_scroll_to_subpart(LayoutWindow *lw, const gchar *needle);
73
74
75 /*
76  *-----------------------------------------------------------------------------
77  * misc
78  *-----------------------------------------------------------------------------
79  */
80
81 gboolean layout_valid(LayoutWindow **lw)
82 {
83         if (*lw == NULL)
84                 {
85                 if (current_lw) *lw = current_lw;
86                 else if (layout_window_list) *lw = layout_window_list->data;
87                 return (*lw != NULL);
88                 }
89         return (g_list_find(layout_window_list, *lw) != NULL);
90 }
91
92 LayoutWindow *layout_find_by_image(ImageWindow *imd)
93 {
94         GList *work;
95
96         work = layout_window_list;
97         while (work)
98                 {
99                 LayoutWindow *lw = work->data;
100                 work = work->next;
101
102                 if (lw->image == imd) return lw;
103                 }
104
105         return NULL;
106 }
107
108 LayoutWindow *layout_find_by_image_fd(ImageWindow *imd)
109 {
110         GList *work;
111
112         work = layout_window_list;
113         while (work)
114                 {
115                 LayoutWindow *lw = work->data;
116                 work = work->next;
117
118                 if (lw->image->image_fd == imd->image_fd)
119                         return lw;
120                 }
121
122         return NULL;
123 }
124
125 LayoutWindow *layout_find_by_layout_id(const gchar *id)
126 {
127         GList *work;
128
129         if (!id || !id[0]) return NULL;
130
131         if (strcmp(id, LAYOUT_ID_CURRENT) == 0)
132                 {
133                 if (current_lw) return current_lw;
134                 if (layout_window_list) return layout_window_list->data;
135                 return NULL;
136                 }
137
138         work = layout_window_list;
139         while (work)
140                 {
141                 LayoutWindow *lw = work->data;
142                 work = work->next;
143
144                 if (lw->options.id && strcmp(id, lw->options.id) == 0)
145                         return lw;
146                 }
147
148         return NULL;
149 }
150
151 static void layout_set_unique_id(LayoutWindow *lw)
152 {
153         char id[10];
154         gint i;
155         if (lw->options.id && lw->options.id[0]) return; /* id is already set */
156
157         g_free(lw->options.id);
158         lw->options.id = NULL;
159
160         if (!layout_find_by_layout_id("main"))
161                 {
162                 lw->options.id = g_strdup("main");
163                 return;
164                 }
165
166         i = 1;
167         while (TRUE)
168                 {
169                 g_snprintf(id, sizeof(id), "lw%d", i);
170                 if (!layout_find_by_layout_id(id))
171                         {
172                         lw->options.id = g_strdup(id);
173                         return;
174                         }
175                 i++;
176                 }
177 }
178
179 static gboolean layout_set_current_cb(GtkWidget *widget, GdkEventFocus *event, gpointer data)
180 {
181         LayoutWindow *lw = data;
182         current_lw = lw;
183         return FALSE;
184 }
185
186 /*
187  *-----------------------------------------------------------------------------
188  * menu, toolbar, and dir view
189  *-----------------------------------------------------------------------------
190  */
191
192 static void layout_path_entry_changed_cb(GtkWidget *widget, gpointer data)
193 {
194         LayoutWindow *lw = data;
195         gchar *buf;
196
197         if (gtk_combo_box_get_active(GTK_COMBO_BOX(widget)) < 0) return;
198
199         buf = g_strdup(gtk_entry_get_text(GTK_ENTRY(lw->path_entry)));
200         if (!lw->dir_fd || strcmp(buf, lw->dir_fd->path) != 0)
201                 {
202                 layout_set_path(lw, buf);
203                 }
204
205         g_free(buf);
206 }
207
208 static void layout_path_entry_tab_cb(const gchar *path, gpointer data)
209 {
210         LayoutWindow *lw = data;
211         gchar *buf;
212
213         buf = g_strdup(path);
214         parse_out_relatives(buf);
215
216         if (isdir(buf))
217                 {
218                 if ((!lw->dir_fd || strcmp(lw->dir_fd->path, buf) != 0) && layout_set_path(lw, buf))
219                         {
220                         gint pos = -1;
221                         /* put the G_DIR_SEPARATOR back, if we are in tab completion for a dir and result was path change */
222                         gtk_editable_insert_text(GTK_EDITABLE(lw->path_entry), G_DIR_SEPARATOR_S, -1, &pos);
223                         gtk_editable_set_position(GTK_EDITABLE(lw->path_entry),
224                                                   strlen(gtk_entry_get_text(GTK_ENTRY(lw->path_entry))));
225                         }
226                 }
227         else if (lw->dir_fd)
228                 {
229                 gchar *base = remove_level_from_path(buf);
230
231                 if (strcmp(lw->dir_fd->path, base) == 0)
232                         {
233                         layout_list_scroll_to_subpart(lw, filename_from_path(buf));
234                         }
235                 g_free(base);
236                 }
237
238         g_free(buf);
239 }
240
241 static void layout_path_entry_cb(const gchar *path, gpointer data)
242 {
243         LayoutWindow *lw = data;
244         gchar *buf;
245
246         buf = g_strdup(path);
247         parse_out_relatives(buf);
248
249         layout_set_path(lw, buf);
250
251         g_free(buf);
252 }
253
254 static void layout_vd_select_cb(ViewDir *vd, FileData *fd, gpointer data)
255 {
256         LayoutWindow *lw = data;
257
258         layout_set_fd(lw, fd);
259 }
260
261 static void layout_path_entry_tab_append_cb(const gchar *path, gpointer data, gint n)
262 {
263         LayoutWindow *lw = data;
264
265         if (!lw || !lw->back_button) return;
266         if (!layout_valid(&lw)) return;
267
268         /* Enable back button if it makes sense */
269         gtk_widget_set_sensitive(lw->back_button, (n > 1));
270 }
271
272 static GtkWidget *layout_tool_setup(LayoutWindow *lw)
273 {
274         GtkWidget *box;
275         GtkWidget *menu_bar;
276         GtkWidget *tabcomp;
277         GtkWidget *toolbar;
278
279         box = gtk_vbox_new(FALSE, 0);
280
281         menu_bar = layout_actions_menu_bar(lw);
282         gtk_box_pack_start(GTK_BOX(box), menu_bar, FALSE, FALSE, 0);
283
284         toolbar = layout_actions_toolbar(lw, TOOLBAR_MAIN);
285         gtk_box_pack_start(GTK_BOX(box), toolbar, FALSE, FALSE, 0);
286         if (lw->options.toolbar_hidden) gtk_widget_hide(toolbar);
287
288         tabcomp = tab_completion_new_with_history(&lw->path_entry, NULL, "path_list", -1,
289                                                   layout_path_entry_cb, lw);
290         tab_completion_add_tab_func(lw->path_entry, layout_path_entry_tab_cb, lw);
291         tab_completion_add_append_func(lw->path_entry, layout_path_entry_tab_append_cb, lw);
292         gtk_box_pack_start(GTK_BOX(box), tabcomp, FALSE, FALSE, 0);
293         gtk_widget_show(tabcomp);
294
295 #if GTK_CHECK_VERSION(3,20,0)
296         g_signal_connect(G_OBJECT(gtk_widget_get_parent(gtk_widget_get_parent(lw->path_entry))), "changed",
297                          G_CALLBACK(layout_path_entry_changed_cb), lw);
298 #else
299         g_signal_connect(G_OBJECT(gtk_widget_get_parent(lw->path_entry)), "changed",
300                          G_CALLBACK(layout_path_entry_changed_cb), lw);
301 #endif
302
303         lw->vd = vd_new(lw->options.dir_view_type, lw->dir_fd);
304         vd_set_layout(lw->vd, lw);
305         vd_set_select_func(lw->vd, layout_vd_select_cb, lw);
306
307         lw->dir_view = lw->vd->widget;
308
309         gtk_box_pack_start(GTK_BOX(box), lw->dir_view, TRUE, TRUE, 0);
310         gtk_widget_show(lw->dir_view);
311
312         gtk_widget_show(box);
313
314         return box;
315 }
316
317 /*
318  *-----------------------------------------------------------------------------
319  * sort button (and menu)
320  *-----------------------------------------------------------------------------
321  */
322
323 static void layout_sort_menu_cb(GtkWidget *widget, gpointer data)
324 {
325         LayoutWindow *lw;
326         SortType type;
327
328         if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) return;
329
330         lw = submenu_item_get_data(widget);
331         if (!lw) return;
332
333         type = (SortType)GPOINTER_TO_INT(data);
334
335         layout_sort_set(lw, type, lw->sort_ascend);
336 }
337
338 static void layout_sort_menu_ascend_cb(GtkWidget *widget, gpointer data)
339 {
340         LayoutWindow *lw = data;
341
342         layout_sort_set(lw, lw->sort_method, !lw->sort_ascend);
343 }
344
345 static void layout_sort_menu_hide_cb(GtkWidget *widget, gpointer data)
346 {
347         /* destroy the menu */
348         g_object_unref(widget);
349 }
350
351 static void layout_sort_button_press_cb(GtkWidget *widget, gpointer data)
352 {
353         LayoutWindow *lw = data;
354         GtkWidget *menu;
355         GdkEvent *event;
356         guint32 etime;
357
358         menu = submenu_add_sort(NULL, G_CALLBACK(layout_sort_menu_cb), lw, FALSE, FALSE, TRUE, lw->sort_method);
359
360         /* take ownership of menu */
361 #ifdef GTK_OBJECT_FLOATING
362         /* GTK+ < 2.10 */
363         g_object_ref(G_OBJECT(menu));
364         gtk_object_sink(GTK_OBJECT(menu));
365 #else
366         /* GTK+ >= 2.10 */
367         g_object_ref_sink(G_OBJECT(menu));
368 #endif
369
370         /* ascending option */
371         menu_item_add_divider(menu);
372         menu_item_add_check(menu, _("Ascending"), lw->sort_ascend, G_CALLBACK(layout_sort_menu_ascend_cb), lw);
373
374         g_signal_connect(G_OBJECT(menu), "selection_done",
375                          G_CALLBACK(layout_sort_menu_hide_cb), NULL);
376
377         event = gtk_get_current_event();
378         if (event)
379                 {
380                 etime = gdk_event_get_time(event);
381                 gdk_event_free(event);
382                 }
383         else
384                 {
385                 etime = 0;
386                 }
387
388         gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, 0, etime);
389 }
390
391 static GtkWidget *layout_sort_button(LayoutWindow *lw)
392 {
393         GtkWidget *button;
394
395         button = gtk_button_new_with_label(sort_type_get_text(lw->sort_method));
396         g_signal_connect(G_OBJECT(button), "clicked",
397                          G_CALLBACK(layout_sort_button_press_cb), lw);
398         gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE);
399
400         return button;
401 }
402
403 /*
404  *-----------------------------------------------------------------------------
405  * status bar
406  *-----------------------------------------------------------------------------
407  */
408
409
410 void layout_status_update_progress(LayoutWindow *lw, gdouble val, const gchar *text)
411 {
412         if (!layout_valid(&lw)) return;
413         if (!lw->info_progress_bar) return;
414
415         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(lw->info_progress_bar), val);
416         gtk_progress_bar_set_text(GTK_PROGRESS_BAR(lw->info_progress_bar), (text) ? text : " ");
417 }
418
419 void layout_status_update_info(LayoutWindow *lw, const gchar *text)
420 {
421         gchar *buf = NULL;
422
423         if (!layout_valid(&lw)) return;
424
425         if (!text)
426                 {
427                 guint n;
428                 gint64 n_bytes = 0;
429
430                 n = layout_list_count(lw, &n_bytes);
431
432                 if (n)
433                         {
434                         guint s;
435                         gint64 s_bytes = 0;
436                         const gchar *ss;
437
438                         if (layout_image_slideshow_active(lw))
439                                 {
440                                 if (!layout_image_slideshow_paused(lw))
441                                         {
442                                         ss = _(" Slideshow");
443                                         }
444                                 else
445                                         {
446                                         ss = _(" Paused");
447                                         }
448                                 }
449                         else
450                                 {
451                                 ss = "";
452                                 }
453
454                         s = layout_selection_count(lw, &s_bytes);
455
456                         layout_bars_new_selection(lw, s);
457
458                         if (s > 0)
459                                 {
460                                 gchar *b = text_from_size_abrev(n_bytes);
461                                 gchar *sb = text_from_size_abrev(s_bytes);
462                                 buf = g_strdup_printf(_("%s, %d files (%s, %d)%s"), b, n, sb, s, ss);
463                                 g_free(b);
464                                 g_free(sb);
465                                 }
466                         else if (n > 0)
467                                 {
468                                 gchar *b = text_from_size_abrev(n_bytes);
469                                 buf = g_strdup_printf(_("%s, %d files%s"), b, n, ss);
470                                 g_free(b);
471                                 }
472                         else
473                                 {
474                                 buf = g_strdup_printf(_("%d files%s"), n, ss);
475                                 }
476
477                         text = buf;
478
479                         image_osd_update(lw->image);
480                         }
481                 else
482                         {
483                         text = "";
484                         }
485         }
486
487         if (lw->info_status) gtk_label_set_text(GTK_LABEL(lw->info_status), text);
488         g_free(buf);
489 }
490
491 void layout_status_update_image(LayoutWindow *lw)
492 {
493         guint64 n;
494
495         if (!layout_valid(&lw) || !lw->image) return;
496         if (!lw->info_zoom || !lw->info_details) return; /*called from layout_style_set */
497
498         n = layout_list_count(lw, NULL);
499
500         if (!n)
501                 {
502                 gtk_label_set_text(GTK_LABEL(lw->info_zoom), "");
503                 gtk_label_set_text(GTK_LABEL(lw->info_details), "");
504                 }
505         else
506                 {
507                 gchar *text;
508                 gchar *b;
509
510                 text = image_zoom_get_as_text(lw->image);
511                 gtk_label_set_text(GTK_LABEL(lw->info_zoom), text);
512                 g_free(text);
513
514                 b = image_get_fd(lw->image) ? text_from_size(image_get_fd(lw->image)->size) : g_strdup("0");
515
516                 if (lw->image->unknown)
517                         {
518                         if (image_get_path(lw->image) && !access_file(image_get_path(lw->image), R_OK))
519                                 {
520                                 text = g_strdup_printf(_("(no read permission) %s bytes"), b);
521                                 }
522                         else
523                                 {
524                                 text = g_strdup_printf(_("( ? x ? ) %s bytes"), b);
525                                 }
526                         }
527                 else
528                         {
529                         gint width, height;
530
531                         image_get_image_size(lw->image, &width, &height);
532                         text = g_strdup_printf(_("( %d x %d ) %s bytes"),
533                                                width, height, b);
534                         }
535
536                 g_signal_emit_by_name (lw->image->pr, "update-pixel");
537
538                 g_free(b);
539
540                 gtk_label_set_text(GTK_LABEL(lw->info_details), text);
541                 g_free(text);
542                 }
543         layout_util_sync_color(lw); /* update color button */
544 }
545
546 void layout_status_update_all(LayoutWindow *lw)
547 {
548         layout_status_update_progress(lw, 0.0, NULL);
549         layout_status_update_info(lw, NULL);
550         layout_status_update_image(lw);
551         layout_util_status_update_write(lw);
552 }
553
554 static GtkWidget *layout_status_label(gchar *text, GtkWidget *box, gboolean start, gint size, gboolean expand)
555 {
556         GtkWidget *label;
557         GtkWidget *frame;
558
559         frame = gtk_frame_new(NULL);
560         if (size) gtk_widget_set_size_request(frame, size, -1);
561         gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_IN);
562         if (start)
563                 {
564                 gtk_box_pack_start(GTK_BOX(box), frame, expand, expand, 0);
565                 }
566         else
567                 {
568                 gtk_box_pack_end(GTK_BOX(box), frame, expand, expand, 0);
569                 }
570         gtk_widget_show(frame);
571
572         label = gtk_label_new(text ? text : "");
573         gtk_container_add(GTK_CONTAINER(frame), label);
574         gtk_widget_show(label);
575
576         return label;
577 }
578
579 static void layout_status_setup(LayoutWindow *lw, GtkWidget *box, gboolean small_format)
580 {
581         GtkWidget *hbox;
582         GtkWidget *toolbar;
583         GtkWidget *toolbar_frame;
584
585         if (lw->info_box) return;
586
587         if (small_format)
588                 {
589                 lw->info_box = gtk_vbox_new(FALSE, 0);
590                 }
591         else
592                 {
593                 lw->info_box = gtk_hbox_new(FALSE, 0);
594                 }
595         gtk_box_pack_end(GTK_BOX(box), lw->info_box, FALSE, FALSE, 0);
596         gtk_widget_show(lw->info_box);
597
598         if (small_format)
599                 {
600                 hbox = gtk_hbox_new(FALSE, 0);
601                 gtk_box_pack_start(GTK_BOX(lw->info_box), hbox, FALSE, FALSE, 0);
602                 gtk_widget_show(hbox);
603                 }
604         else
605                 {
606                 hbox = lw->info_box;
607                 }
608         lw->info_progress_bar = gtk_progress_bar_new();
609         gtk_widget_set_size_request(lw->info_progress_bar, PROGRESS_WIDTH, -1);
610 #if GTK_CHECK_VERSION(3,0,0)
611         gtk_progress_bar_set_text(GTK_PROGRESS_BAR(lw->info_progress_bar), "");
612         gtk_progress_bar_set_show_text(GTK_PROGRESS_BAR(lw->info_progress_bar), TRUE);
613 #endif
614         gtk_box_pack_start(GTK_BOX(hbox), lw->info_progress_bar, FALSE, FALSE, 0);
615         gtk_widget_show(lw->info_progress_bar);
616
617         lw->info_sort = layout_sort_button(lw);
618         gtk_widget_set_tooltip_text(GTK_WIDGET(lw->info_sort), _("Select sort order"));
619         gtk_box_pack_start(GTK_BOX(hbox), lw->info_sort, FALSE, FALSE, 0);
620         gtk_widget_show(lw->info_sort);
621
622         lw->info_status = layout_status_label(NULL, lw->info_box, TRUE, 0, (!small_format));
623         gtk_widget_set_tooltip_text(GTK_WIDGET(lw->info_status), _("Folder contents (files selected)"));
624
625         if (small_format)
626                 {
627                 hbox = gtk_hbox_new(FALSE, 0);
628                 gtk_box_pack_start(GTK_BOX(lw->info_box), hbox, FALSE, FALSE, 0);
629                 gtk_widget_show(hbox);
630                 }
631         lw->info_details = layout_status_label(NULL, hbox, TRUE, 0, TRUE);
632         gtk_widget_set_tooltip_text(GTK_WIDGET(lw->info_details), _("(Image dimensions) Image size"));
633         toolbar = layout_actions_toolbar(lw, TOOLBAR_STATUS);
634
635         toolbar_frame = gtk_frame_new(NULL);
636         gtk_frame_set_shadow_type(GTK_FRAME(toolbar_frame), GTK_SHADOW_IN);
637         gtk_container_add(GTK_CONTAINER(toolbar_frame), toolbar);
638         gtk_widget_show(toolbar_frame);
639         gtk_widget_show(toolbar);
640         gtk_box_pack_end(GTK_BOX(hbox), toolbar_frame, FALSE, FALSE, 0);
641         lw->info_zoom = layout_status_label(NULL, hbox, FALSE, ZOOM_LABEL_WIDTH, FALSE);
642         gtk_widget_set_tooltip_text(GTK_WIDGET(lw->info_zoom), _("Image zoom level"));
643         if (small_format)
644                 {
645                 hbox = gtk_hbox_new(FALSE, 0);
646                 gtk_box_pack_start(GTK_BOX(lw->info_box), hbox, FALSE, FALSE, 0);
647                 gtk_widget_show(hbox);
648                 }
649         lw->info_pixel = layout_status_label(NULL, hbox, FALSE, 0, small_format); /* expand only in small format */
650         gtk_widget_set_tooltip_text(GTK_WIDGET(lw->info_pixel), _("[Pixel x,y coord]: (Pixel R,G,B value)"));
651         if (!lw->options.show_info_pixel) gtk_widget_hide(gtk_widget_get_parent(lw->info_pixel));
652 }
653
654 /*
655  *-----------------------------------------------------------------------------
656  * views
657  *-----------------------------------------------------------------------------
658  */
659
660 static GtkWidget *layout_tools_new(LayoutWindow *lw)
661 {
662         lw->dir_view = layout_tool_setup(lw);
663         return lw->dir_view;
664 }
665
666 static void layout_list_status_cb(ViewFile *vf, gpointer data)
667 {
668         LayoutWindow *lw = data;
669
670         layout_status_update_info(lw, NULL);
671 }
672
673 static void layout_list_thumb_cb(ViewFile *vf, gdouble val, const gchar *text, gpointer data)
674 {
675         LayoutWindow *lw = data;
676
677         layout_status_update_progress(lw, val, text);
678 }
679
680 static void layout_list_sync_thumb(LayoutWindow *lw)
681 {
682         if (lw->vf) vf_thumb_set(lw->vf, lw->options.show_thumbnails);
683 }
684
685 static GtkWidget *layout_list_new(LayoutWindow *lw)
686 {
687         lw->vf = vf_new(lw->options.file_view_type, NULL);
688         vf_set_layout(lw->vf, lw);
689
690         vf_set_status_func(lw->vf, layout_list_status_cb, lw);
691         vf_set_thumb_status_func(lw->vf, layout_list_thumb_cb, lw);
692
693         vf_marks_set(lw->vf, lw->options.show_marks);
694
695         layout_list_sync_thumb(lw);
696
697         return lw->vf->widget;
698 }
699
700 static void layout_list_sync_marks(LayoutWindow *lw)
701 {
702         if (lw->vf) vf_marks_set(lw->vf, lw->options.show_marks);
703 }
704
705 static void layout_list_scroll_to_subpart(LayoutWindow *lw, const gchar *needle)
706 {
707         if (!lw) return;
708 }
709
710 GList *layout_list(LayoutWindow *lw)
711 {
712         if (!layout_valid(&lw)) return NULL;
713
714         if (lw->vf) return vf_get_list(lw->vf);
715
716         return NULL;
717 }
718
719 guint layout_list_count(LayoutWindow *lw, gint64 *bytes)
720 {
721         if (!layout_valid(&lw)) return 0;
722
723         if (lw->vf) return vf_count(lw->vf, bytes);
724
725         return 0;
726 }
727
728 FileData *layout_list_get_fd(LayoutWindow *lw, gint index)
729 {
730         if (!layout_valid(&lw)) return NULL;
731
732         if (lw->vf) return vf_index_get_data(lw->vf, index);
733
734         return NULL;
735 }
736
737 gint layout_list_get_index(LayoutWindow *lw, FileData *fd)
738 {
739         if (!layout_valid(&lw) || !fd) return -1;
740
741         if (lw->vf) return vf_index_by_fd(lw->vf, fd);
742
743         return -1;
744 }
745
746 void layout_list_sync_fd(LayoutWindow *lw, FileData *fd)
747 {
748         if (!layout_valid(&lw)) return;
749
750         if (lw->vf) vf_select_by_fd(lw->vf, fd);
751 }
752
753 static void layout_list_sync_sort(LayoutWindow *lw)
754 {
755         if (!layout_valid(&lw)) return;
756
757         if (lw->vf) vf_sort_set(lw->vf, lw->sort_method, lw->sort_ascend);
758 }
759
760 GList *layout_selection_list(LayoutWindow *lw)
761 {
762         if (!layout_valid(&lw)) return NULL;
763
764         if (layout_image_get_collection(lw, NULL))
765                 {
766                 FileData *fd;
767
768                 fd = layout_image_get_fd(lw);
769                 if (fd) return g_list_append(NULL, file_data_ref(fd));
770                 return NULL;
771                 }
772
773         if (lw->vf) return vf_selection_get_list(lw->vf);
774
775         return NULL;
776 }
777
778 GList *layout_selection_list_by_index(LayoutWindow *lw)
779 {
780         if (!layout_valid(&lw)) return NULL;
781
782         if (lw->vf) return vf_selection_get_list_by_index(lw->vf);
783
784         return NULL;
785 }
786
787 guint layout_selection_count(LayoutWindow *lw, gint64 *bytes)
788 {
789         if (!layout_valid(&lw)) return 0;
790
791         if (lw->vf) return vf_selection_count(lw->vf, bytes);
792
793         return 0;
794 }
795
796 void layout_select_all(LayoutWindow *lw)
797 {
798         if (!layout_valid(&lw)) return;
799
800         if (lw->vf) vf_select_all(lw->vf);
801 }
802
803 void layout_select_none(LayoutWindow *lw)
804 {
805         if (!layout_valid(&lw)) return;
806
807         if (lw->vf) vf_select_none(lw->vf);
808 }
809
810 void layout_select_invert(LayoutWindow *lw)
811 {
812         if (!layout_valid(&lw)) return;
813
814         if (lw->vf) vf_select_invert(lw->vf);
815 }
816
817 void layout_mark_to_selection(LayoutWindow *lw, gint mark, MarkToSelectionMode mode)
818 {
819         if (!layout_valid(&lw)) return;
820
821         if (lw->vf) vf_mark_to_selection(lw->vf, mark, mode);
822 }
823
824 void layout_selection_to_mark(LayoutWindow *lw, gint mark, SelectionToMarkMode mode)
825 {
826         if (!layout_valid(&lw)) return;
827
828         if (lw->vf) vf_selection_to_mark(lw->vf, mark, mode);
829
830         layout_status_update_info(lw, NULL); /* osd in fullscreen mode */
831 }
832
833 void layout_mark_filter_toggle(LayoutWindow *lw, gint mark)
834 {
835         if (!layout_valid(&lw)) return;
836
837         if (lw->vf) vf_mark_filter_toggle(lw->vf, mark);
838 }
839
840
841 /*
842  *-----------------------------------------------------------------------------
843  * access
844  *-----------------------------------------------------------------------------
845  */
846
847 const gchar *layout_get_path(LayoutWindow *lw)
848 {
849         if (!layout_valid(&lw)) return NULL;
850         return lw->dir_fd ? lw->dir_fd->path : NULL;
851 }
852
853 static void layout_sync_path(LayoutWindow *lw)
854 {
855         if (!lw->dir_fd) return;
856
857         if (lw->path_entry) gtk_entry_set_text(GTK_ENTRY(lw->path_entry), lw->dir_fd->path);
858
859         if (lw->vd) vd_set_fd(lw->vd, lw->dir_fd);
860         if (lw->vf) vf_set_fd(lw->vf, lw->dir_fd);
861 }
862
863 gboolean layout_set_path(LayoutWindow *lw, const gchar *path)
864 {
865         FileData *fd;
866         gboolean ret;
867
868         if (!path) return FALSE;
869
870         fd = file_data_new_group(path);
871         ret = layout_set_fd(lw, fd);
872         file_data_unref(fd);
873         return ret;
874 }
875
876
877 gboolean layout_set_fd(LayoutWindow *lw, FileData *fd)
878 {
879         gboolean have_file = FALSE;
880         gboolean dir_changed = TRUE;
881
882         if (!layout_valid(&lw)) return FALSE;
883
884         if (!fd || !isname(fd->path)) return FALSE;
885         if (lw->dir_fd && fd == lw->dir_fd)
886                 {
887                 return TRUE;
888                 }
889
890         if (isdir(fd->path))
891                 {
892                 if (lw->dir_fd)
893                         {
894                         file_data_unregister_real_time_monitor(lw->dir_fd);
895                         file_data_unref(lw->dir_fd);
896                         }
897                 lw->dir_fd = file_data_ref(fd);
898                 file_data_register_real_time_monitor(fd);
899                 }
900         else
901                 {
902                 gchar *base;
903
904                 base = remove_level_from_path(fd->path);
905                 if (lw->dir_fd && strcmp(lw->dir_fd->path, base) == 0)
906                         {
907                         g_free(base);
908                         dir_changed = FALSE;
909                         }
910                 else if (isdir(base))
911                         {
912                         if (lw->dir_fd)
913                                 {
914                                 file_data_unregister_real_time_monitor(lw->dir_fd);
915                                 file_data_unref(lw->dir_fd);
916                                 }
917                         lw->dir_fd = file_data_new_dir(base);
918                         file_data_register_real_time_monitor(lw->dir_fd);
919                         g_free(base);
920                         }
921                 else
922                         {
923                         g_free(base);
924                         return FALSE;
925                         }
926                 if (isfile(fd->path)) have_file = TRUE;
927                 }
928
929         if (lw->path_entry) tab_completion_append_to_history(lw->path_entry, lw->dir_fd->path);
930         layout_sync_path(lw);
931         layout_list_sync_sort(lw);
932
933         if (have_file)
934                 {
935                 gint row;
936
937                 row = layout_list_get_index(lw, fd);
938                 if (row >= 0)
939                         {
940                         layout_image_set_index(lw, row);
941                         }
942                 else
943                         {
944                         layout_image_set_fd(lw, fd);
945                         }
946                 }
947         else if (!options->lazy_image_sync)
948                 {
949                 layout_image_set_index(lw, 0);
950                 }
951
952         if (options->metadata.confirm_on_dir_change && dir_changed)
953                 metadata_write_queue_confirm(FALSE, NULL, NULL);
954
955         return TRUE;
956 }
957
958 static void layout_refresh_lists(LayoutWindow *lw)
959 {
960         if (lw->vd) vd_refresh(lw->vd);
961
962         if (lw->vf)
963                 {
964                 vf_refresh(lw->vf);
965                 vf_thumb_update(lw->vf);
966                 }
967 }
968
969 void layout_refresh(LayoutWindow *lw)
970 {
971         if (!layout_valid(&lw)) return;
972
973         DEBUG_1("layout refresh");
974
975         layout_refresh_lists(lw);
976
977         if (lw->image) layout_image_refresh(lw);
978 }
979
980 void layout_thumb_set(LayoutWindow *lw, gboolean enable)
981 {
982         if (!layout_valid(&lw)) return;
983
984         if (lw->options.show_thumbnails == enable) return;
985
986         lw->options.show_thumbnails = enable;
987
988         layout_util_sync_thumb(lw);
989         layout_list_sync_thumb(lw);
990 }
991
992 void layout_marks_set(LayoutWindow *lw, gboolean enable)
993 {
994         if (!layout_valid(&lw)) return;
995
996         if (lw->options.show_marks == enable) return;
997
998         lw->options.show_marks = enable;
999
1000 //      layout_util_sync_marks(lw);
1001         layout_list_sync_marks(lw);
1002 }
1003
1004 gboolean layout_thumb_get(LayoutWindow *lw)
1005 {
1006         if (!layout_valid(&lw)) return FALSE;
1007
1008         return lw->options.show_thumbnails;
1009 }
1010
1011 gboolean layout_marks_get(LayoutWindow *lw)
1012 {
1013         if (!layout_valid(&lw)) return FALSE;
1014
1015         return lw->options.show_marks;
1016 }
1017
1018 void layout_sort_set(LayoutWindow *lw, SortType type, gboolean ascend)
1019 {
1020         if (!layout_valid(&lw)) return;
1021         if (lw->sort_method == type && lw->sort_ascend == ascend) return;
1022
1023         lw->sort_method = type;
1024         lw->sort_ascend = ascend;
1025
1026         if (lw->info_sort) gtk_label_set_text(GTK_LABEL(gtk_bin_get_child(GTK_BIN(lw->info_sort))),
1027                                               sort_type_get_text(type));
1028         layout_list_sync_sort(lw);
1029 }
1030
1031 gboolean layout_sort_get(LayoutWindow *lw, SortType *type, gboolean *ascend)
1032 {
1033         if (!layout_valid(&lw)) return FALSE;
1034
1035         if (type) *type = lw->sort_method;
1036         if (ascend) *ascend = lw->sort_ascend;
1037
1038         return TRUE;
1039 }
1040
1041 gboolean layout_geometry_get(LayoutWindow *lw, gint *x, gint *y, gint *w, gint *h)
1042 {
1043         GdkWindow *window;
1044         if (!layout_valid(&lw)) return FALSE;
1045
1046         window = gtk_widget_get_window(lw->window);
1047         gdk_window_get_root_origin(window, x, y);
1048         *w = gdk_window_get_width(window);
1049         *h = gdk_window_get_height(window);
1050
1051         return TRUE;
1052 }
1053
1054 gboolean layout_geometry_get_dividers(LayoutWindow *lw, gint *h, gint *v)
1055 {
1056         GtkAllocation h_allocation;
1057         GtkAllocation v_allocation;
1058
1059         if (!layout_valid(&lw)) return FALSE;
1060
1061         if (lw->h_pane)
1062                 {
1063                 GtkWidget *child = gtk_paned_get_child1(GTK_PANED(lw->h_pane));
1064                 gtk_widget_get_allocation(child, &h_allocation);
1065                 }
1066
1067         if (lw->v_pane)
1068                 {
1069                 GtkWidget *child = gtk_paned_get_child1(GTK_PANED(lw->v_pane));
1070                 gtk_widget_get_allocation(child, &v_allocation);
1071                 }
1072
1073         if (lw->h_pane && h_allocation.x >= 0)
1074                 {
1075                 *h = h_allocation.width;
1076                 }
1077         else if (h != &lw->options.main_window.hdivider_pos)
1078                 {
1079                 *h = lw->options.main_window.hdivider_pos;
1080                 }
1081
1082         if (lw->v_pane && v_allocation.x >= 0)
1083                 {
1084                 *v = v_allocation.height;
1085                 }
1086         else if (v != &lw->options.main_window.vdivider_pos)
1087                 {
1088                 *v = lw->options.main_window.vdivider_pos;
1089                 }
1090
1091         return TRUE;
1092 }
1093
1094 void layout_views_set(LayoutWindow *lw, DirViewType dir_view_type, FileViewType file_view_type)
1095 {
1096         if (!layout_valid(&lw)) return;
1097
1098         if (lw->options.dir_view_type == dir_view_type && lw->options.file_view_type == file_view_type) return;
1099
1100         lw->options.dir_view_type = dir_view_type;
1101         lw->options.file_view_type = file_view_type;
1102
1103         layout_style_set(lw, -1, NULL);
1104 }
1105
1106 gboolean layout_views_get(LayoutWindow *lw, DirViewType *dir_view_type, FileViewType *file_view_type)
1107 {
1108         if (!layout_valid(&lw)) return FALSE;
1109
1110         *dir_view_type = lw->options.dir_view_type;
1111         *file_view_type = lw->options.file_view_type;
1112
1113         return TRUE;
1114 }
1115
1116 /*
1117  *-----------------------------------------------------------------------------
1118  * location utils
1119  *-----------------------------------------------------------------------------
1120  */
1121
1122 static gboolean layout_location_single(LayoutLocation l)
1123 {
1124         return (l == LAYOUT_LEFT ||
1125                 l == LAYOUT_RIGHT ||
1126                 l == LAYOUT_TOP ||
1127                 l == LAYOUT_BOTTOM);
1128 }
1129
1130 static gboolean layout_location_vertical(LayoutLocation l)
1131 {
1132         return (l & LAYOUT_TOP ||
1133                 l & LAYOUT_BOTTOM);
1134 }
1135
1136 static gboolean layout_location_first(LayoutLocation l)
1137 {
1138         return (l & LAYOUT_TOP ||
1139                 l & LAYOUT_LEFT);
1140 }
1141
1142 static LayoutLocation layout_grid_compass(LayoutWindow *lw)
1143 {
1144         if (layout_location_single(lw->dir_location)) return lw->dir_location;
1145         if (layout_location_single(lw->file_location)) return lw->file_location;
1146         return lw->image_location;
1147 }
1148
1149 static void layout_location_compute(LayoutLocation l1, LayoutLocation l2,
1150                                     GtkWidget *s1, GtkWidget *s2,
1151                                     GtkWidget **d1, GtkWidget **d2)
1152 {
1153         LayoutLocation l;
1154
1155         l = l1 & l2;    /* get common compass direction */
1156         l = l1 - l;     /* remove it */
1157
1158         if (layout_location_first(l))
1159                 {
1160                 *d1 = s1;
1161                 *d2 = s2;
1162                 }
1163         else
1164                 {
1165                 *d1 = s2;
1166                 *d2 = s1;
1167                 }
1168 }
1169
1170 /*
1171  *-----------------------------------------------------------------------------
1172  * tools window (for floating/hidden)
1173  *-----------------------------------------------------------------------------
1174  */
1175
1176 gboolean layout_geometry_get_tools(LayoutWindow *lw, gint *x, gint *y, gint *w, gint *h, gint *divider_pos)
1177 {
1178         GdkWindow *window;
1179         GtkAllocation allocation;
1180         if (!layout_valid(&lw)) return FALSE;
1181
1182         if (!lw->tools || !gtk_widget_get_visible(lw->tools))
1183                 {
1184                 /* use the stored values (sort of breaks success return value) */
1185
1186                 *divider_pos = lw->options.float_window.vdivider_pos;
1187
1188                 return FALSE;
1189                 }
1190
1191         window = gtk_widget_get_window(lw->tools);
1192         gdk_window_get_root_origin(window, x, y);
1193         *w = gdk_window_get_width(window);
1194         *h = gdk_window_get_height(window);
1195         gtk_widget_get_allocation(gtk_paned_get_child1(GTK_PANED(lw->tools_pane)), &allocation);
1196
1197         if (GTK_IS_VPANED(lw->tools_pane))
1198                 {
1199                 *divider_pos = allocation.height;
1200                 }
1201         else
1202                 {
1203                 *divider_pos = allocation.width;
1204                 }
1205
1206         return TRUE;
1207 }
1208
1209 static void layout_tools_geometry_sync(LayoutWindow *lw)
1210 {
1211         layout_geometry_get_tools(lw, &lw->options.float_window.x, &lw->options.float_window.x,
1212                                   &lw->options.float_window.w, &lw->options.float_window.h, &lw->options.float_window.vdivider_pos);
1213 }
1214
1215 static void layout_tools_hide(LayoutWindow *lw, gboolean hide)
1216 {
1217         if (!lw->tools) return;
1218
1219         if (hide)
1220                 {
1221                 if (gtk_widget_get_visible(lw->tools))
1222                         {
1223                         layout_tools_geometry_sync(lw);
1224                         gtk_widget_hide(lw->tools);
1225                         }
1226                 }
1227         else
1228                 {
1229                 if (!gtk_widget_get_visible(lw->tools))
1230                         {
1231                         gtk_widget_show(lw->tools);
1232                         if (lw->vf) vf_refresh(lw->vf);
1233                         }
1234                 }
1235
1236         lw->options.tools_hidden = hide;
1237 }
1238
1239 static gboolean layout_tools_delete_cb(GtkWidget *widget, GdkEventAny *event, gpointer data)
1240 {
1241         LayoutWindow *lw = data;
1242
1243         layout_tools_float_toggle(lw);
1244
1245         return TRUE;
1246 }
1247
1248 static void layout_tools_setup(LayoutWindow *lw, GtkWidget *tools, GtkWidget *files)
1249 {
1250         GtkWidget *vbox;
1251         GtkWidget *w1, *w2;
1252         gboolean vertical;
1253         gboolean new_window = FALSE;
1254
1255         vertical = (layout_location_single(lw->image_location) && !layout_location_vertical(lw->image_location)) ||
1256                    (!layout_location_single(lw->image_location) && layout_location_vertical(layout_grid_compass(lw)));
1257         /* for now, tools/dir are always first in order */
1258         w1 = tools;
1259         w2 = files;
1260
1261         if (!lw->tools)
1262                 {
1263                 GdkGeometry geometry;
1264                 GdkWindowHints hints;
1265
1266                 lw->tools = window_new(GTK_WINDOW_TOPLEVEL, "tools", PIXBUF_INLINE_ICON_TOOLS, NULL, _("Tools"));
1267                 g_signal_connect(G_OBJECT(lw->tools), "delete_event",
1268                                  G_CALLBACK(layout_tools_delete_cb), lw);
1269                 layout_keyboard_init(lw, lw->tools);
1270
1271                 if (options->save_window_positions)
1272                         {
1273                         hints = GDK_HINT_USER_POS;
1274                         }
1275                 else
1276                         {
1277                         hints = 0;
1278                         }
1279
1280                 geometry.min_width = DEFAULT_MINIMAL_WINDOW_SIZE;
1281                 geometry.min_height = DEFAULT_MINIMAL_WINDOW_SIZE;
1282                 geometry.base_width = TOOLWINDOW_DEF_WIDTH;
1283                 geometry.base_height = TOOLWINDOW_DEF_HEIGHT;
1284                 gtk_window_set_geometry_hints(GTK_WINDOW(lw->tools), NULL, &geometry,
1285                                               GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE | hints);
1286
1287
1288                 gtk_window_set_resizable(GTK_WINDOW(lw->tools), TRUE);
1289                 gtk_container_set_border_width(GTK_CONTAINER(lw->tools), 0);
1290
1291                 new_window = TRUE;
1292                 }
1293         else
1294                 {
1295                 layout_tools_geometry_sync(lw);
1296                 /* dump the contents */
1297                 gtk_widget_destroy(gtk_bin_get_child(GTK_BIN(lw->tools)));
1298                 }
1299
1300         layout_actions_add_window(lw, lw->tools);
1301
1302         vbox = gtk_vbox_new(FALSE, 0);
1303         gtk_container_add(GTK_CONTAINER(lw->tools), vbox);
1304         gtk_widget_show(vbox);
1305
1306         layout_status_setup(lw, vbox, TRUE);
1307
1308         if (vertical)
1309                 {
1310                 lw->tools_pane = gtk_vpaned_new();
1311                 }
1312         else
1313                 {
1314                 lw->tools_pane = gtk_hpaned_new();
1315                 }
1316         gtk_box_pack_start(GTK_BOX(vbox), lw->tools_pane, TRUE, TRUE, 0);
1317         gtk_widget_show(lw->tools_pane);
1318
1319         gtk_paned_pack1(GTK_PANED(lw->tools_pane), w1, FALSE, TRUE);
1320         gtk_paned_pack2(GTK_PANED(lw->tools_pane), w2, TRUE, TRUE);
1321
1322         gtk_widget_show(tools);
1323         gtk_widget_show(files);
1324
1325         if (new_window)
1326                 {
1327                 if (options->save_window_positions)
1328                         {
1329                         gtk_window_set_default_size(GTK_WINDOW(lw->tools), lw->options.float_window.w, lw->options.float_window.h);
1330                         gtk_window_move(GTK_WINDOW(lw->tools), lw->options.float_window.x, lw->options.float_window.y);
1331                         }
1332                 else
1333                         {
1334                         if (vertical)
1335                                 {
1336                                 gtk_window_set_default_size(GTK_WINDOW(lw->tools),
1337                                                             TOOLWINDOW_DEF_WIDTH, TOOLWINDOW_DEF_HEIGHT);
1338                                 }
1339                         else
1340                                 {
1341                                 gtk_window_set_default_size(GTK_WINDOW(lw->tools),
1342                                                             TOOLWINDOW_DEF_HEIGHT, TOOLWINDOW_DEF_WIDTH);
1343                                 }
1344                         }
1345                 }
1346
1347         if (!options->save_window_positions)
1348                 {
1349                 if (vertical)
1350                         {
1351                         lw->options.float_window.vdivider_pos = MAIN_WINDOW_DIV_VPOS;
1352                         }
1353                 else
1354                         {
1355                         lw->options.float_window.vdivider_pos = MAIN_WINDOW_DIV_HPOS;
1356                         }
1357                 }
1358
1359         gtk_paned_set_position(GTK_PANED(lw->tools_pane), lw->options.float_window.vdivider_pos);
1360 }
1361
1362 /*
1363  *-----------------------------------------------------------------------------
1364  * glue (layout arrangement)
1365  *-----------------------------------------------------------------------------
1366  */
1367
1368 static void layout_grid_compute(LayoutWindow *lw,
1369                                 GtkWidget *image, GtkWidget *tools, GtkWidget *files,
1370                                 GtkWidget **w1, GtkWidget **w2, GtkWidget **w3)
1371 {
1372         /* heh, this was fun */
1373
1374         if (layout_location_single(lw->dir_location))
1375                 {
1376                 if (layout_location_first(lw->dir_location))
1377                         {
1378                         *w1 = tools;
1379                         layout_location_compute(lw->file_location, lw->image_location, files, image, w2, w3);
1380                         }
1381                 else
1382                         {
1383                         *w3 = tools;
1384                         layout_location_compute(lw->file_location, lw->image_location, files, image, w1, w2);
1385                         }
1386                 }
1387         else if (layout_location_single(lw->file_location))
1388                 {
1389                 if (layout_location_first(lw->file_location))
1390                         {
1391                         *w1 = files;
1392                         layout_location_compute(lw->dir_location, lw->image_location, tools, image, w2, w3);
1393                         }
1394                 else
1395                         {
1396                         *w3 = files;
1397                         layout_location_compute(lw->dir_location, lw->image_location, tools, image, w1, w2);
1398                         }
1399                 }
1400         else
1401                 {
1402                 /* image */
1403                 if (layout_location_first(lw->image_location))
1404                         {
1405                         *w1 = image;
1406                         layout_location_compute(lw->file_location, lw->dir_location, files, tools, w2, w3);
1407                         }
1408                 else
1409                         {
1410                         *w3 = image;
1411                         layout_location_compute(lw->file_location, lw->dir_location, files, tools, w1, w2);
1412                         }
1413                 }
1414 }
1415
1416 void layout_split_change(LayoutWindow *lw, ImageSplitMode mode)
1417 {
1418         GtkWidget *image;
1419         gint i;
1420
1421         for (i = 0; i < MAX_SPLIT_IMAGES; i++)
1422                 {
1423                 if (lw->split_images[i])
1424                         {
1425                         gtk_widget_hide(lw->split_images[i]->widget);
1426                         if (gtk_widget_get_parent(lw->split_images[i]->widget) != lw->utility_paned)
1427                                 gtk_container_remove(GTK_CONTAINER(gtk_widget_get_parent(lw->split_images[i]->widget)), lw->split_images[i]->widget);
1428                         }
1429                 }
1430         gtk_container_remove(GTK_CONTAINER(lw->utility_paned), lw->split_image_widget);
1431
1432         image = layout_image_setup_split(lw, mode);
1433
1434         gtk_paned_pack1(GTK_PANED(lw->utility_paned), image, TRUE, FALSE);
1435         gtk_widget_show(image);
1436         layout_util_sync(lw);
1437 }
1438
1439 static void layout_grid_setup(LayoutWindow *lw)
1440 {
1441         gint priority_location;
1442         GtkWidget *h;
1443         GtkWidget *v;
1444         GtkWidget *w1, *w2, *w3;
1445
1446         GtkWidget *image_sb; /* image together with sidebars in utility box */
1447         GtkWidget *tools;
1448         GtkWidget *files;
1449
1450         layout_actions_setup(lw);
1451
1452         lw->group_box = gtk_vbox_new(FALSE, 0);
1453         gtk_box_pack_start(GTK_BOX(lw->main_box), lw->group_box, TRUE, TRUE, 0);
1454         gtk_widget_show(lw->group_box);
1455
1456         priority_location = layout_grid_compass(lw);
1457
1458         if (lw->utility_box)
1459                 {
1460                 layout_split_change(lw, lw->split_mode); /* this re-creates image frame for the new configuration */
1461                 image_sb = lw->utility_box;
1462                 }
1463         else
1464                 {
1465                 GtkWidget *image; /* image or split images together */
1466                 image = layout_image_setup_split(lw, lw->split_mode);
1467                 image_sb = layout_bars_prepare(lw, image);
1468                 }
1469
1470         tools = layout_tools_new(lw);
1471         files = layout_list_new(lw);
1472
1473
1474         if (lw->options.tools_float || lw->options.tools_hidden)
1475                 {
1476                 gtk_box_pack_start(GTK_BOX(lw->group_box), image_sb, TRUE, TRUE, 0);
1477                 gtk_widget_show(image_sb);
1478
1479                 layout_tools_setup(lw, tools, files);
1480
1481                 image_grab_focus(lw->image);
1482
1483                 return;
1484                 }
1485         else if (lw->tools)
1486                 {
1487                 layout_tools_geometry_sync(lw);
1488                 gtk_widget_destroy(lw->tools);
1489                 lw->tools = NULL;
1490                 lw->tools_pane = NULL;
1491                 }
1492
1493         layout_status_setup(lw, lw->group_box, FALSE);
1494
1495         layout_grid_compute(lw, image_sb, tools, files, &w1, &w2, &w3);
1496
1497         v = lw->v_pane = gtk_vpaned_new();
1498
1499         h = lw->h_pane = gtk_hpaned_new();
1500
1501         if (!layout_location_vertical(priority_location))
1502                 {
1503                 GtkWidget *tmp;
1504
1505                 tmp = v;
1506                 v = h;
1507                 h = tmp;
1508                 }
1509
1510         gtk_box_pack_start(GTK_BOX(lw->group_box), v, TRUE, TRUE, 0);
1511
1512         if (!layout_location_first(priority_location))
1513                 {
1514                 gtk_paned_pack1(GTK_PANED(v), h, FALSE, TRUE);
1515                 gtk_paned_pack2(GTK_PANED(v), w3, TRUE, TRUE);
1516
1517                 gtk_paned_pack1(GTK_PANED(h), w1, FALSE, TRUE);
1518                 gtk_paned_pack2(GTK_PANED(h), w2, TRUE, TRUE);
1519                 }
1520         else
1521                 {
1522                 gtk_paned_pack1(GTK_PANED(v), w1, FALSE, TRUE);
1523                 gtk_paned_pack2(GTK_PANED(v), h, TRUE, TRUE);
1524
1525                 gtk_paned_pack1(GTK_PANED(h), w2, FALSE, TRUE);
1526                 gtk_paned_pack2(GTK_PANED(h), w3, TRUE, TRUE);
1527                 }
1528
1529         gtk_widget_show(image_sb);
1530         gtk_widget_show(tools);
1531         gtk_widget_show(files);
1532
1533         gtk_widget_show(v);
1534         gtk_widget_show(h);
1535
1536         /* fix to have image pane visible when it is left and priority widget */
1537         if (lw->options.main_window.hdivider_pos == -1 &&
1538             w1 == image_sb &&
1539             !layout_location_vertical(priority_location) &&
1540             layout_location_first(priority_location))
1541                 {
1542                 gtk_widget_set_size_request(image_sb, 200, -1);
1543                 }
1544
1545         gtk_paned_set_position(GTK_PANED(lw->h_pane), lw->options.main_window.hdivider_pos);
1546         gtk_paned_set_position(GTK_PANED(lw->v_pane), lw->options.main_window.vdivider_pos);
1547
1548         image_grab_focus(lw->image);
1549 }
1550
1551 void layout_style_set(LayoutWindow *lw, gint style, const gchar *order)
1552 {
1553         FileData *dir_fd;
1554         gint i;
1555
1556         if (!layout_valid(&lw)) return;
1557
1558         if (style != -1)
1559                 {
1560                 LayoutLocation d, f, i;
1561
1562                 layout_config_parse(style, order, &d,  &f, &i);
1563
1564                 if (lw->dir_location == d &&
1565                     lw->file_location == f &&
1566                     lw->image_location == i) return;
1567
1568                 lw->dir_location = d;
1569                 lw->file_location = f;
1570                 lw->image_location = i;
1571                 }
1572
1573         /* remember state */
1574
1575         /* layout_image_slideshow_stop(lw); slideshow should survive */
1576         layout_image_full_screen_stop(lw);
1577
1578         dir_fd = lw->dir_fd;
1579         if (dir_fd) file_data_unregister_real_time_monitor(dir_fd);
1580         lw->dir_fd = NULL;
1581
1582         layout_geometry_get_dividers(lw, &lw->options.main_window.hdivider_pos, &lw->options.main_window.vdivider_pos);
1583
1584         /* preserve utility_box (image + sidebars), menu_bar and toolbars to be reused later in layout_grid_setup */
1585         /* lw->image is preserved together with lw->utility_box */
1586         if (lw->utility_box) gtk_container_remove(GTK_CONTAINER(gtk_widget_get_parent(lw->utility_box)), lw->utility_box);
1587         if (lw->menu_bar) gtk_container_remove(GTK_CONTAINER(gtk_widget_get_parent(lw->menu_bar)), lw->menu_bar);
1588         for (i = 0; i < TOOLBAR_COUNT; i++)
1589                 if (lw->toolbar[i]) gtk_container_remove(GTK_CONTAINER(gtk_widget_get_parent(lw->toolbar[i])), lw->toolbar[i]);
1590
1591         /* clear it all */
1592
1593         lw->h_pane = NULL;
1594         lw->v_pane = NULL;
1595
1596         lw->path_entry = NULL;
1597         lw->dir_view = NULL;
1598         lw->vd = NULL;
1599
1600         lw->file_view = NULL;
1601         lw->vf = NULL;
1602
1603         lw->info_box = NULL;
1604         lw->info_progress_bar = NULL;
1605         lw->info_sort = NULL;
1606         lw->info_status = NULL;
1607         lw->info_details = NULL;
1608         lw->info_pixel = NULL;
1609         lw->info_zoom = NULL;
1610
1611 /*
1612         if (lw->ui_manager) g_object_unref(lw->ui_manager);
1613         lw->ui_manager = NULL;
1614         lw->action_group = NULL;
1615         lw->action_group_editors = NULL;
1616 */
1617
1618         gtk_container_remove(GTK_CONTAINER(lw->main_box), lw->group_box);
1619         lw->group_box = NULL;
1620
1621         /* re-fill */
1622
1623         layout_grid_setup(lw);
1624         layout_tools_hide(lw, lw->options.tools_hidden);
1625
1626         layout_util_sync(lw);
1627         layout_status_update_all(lw);
1628
1629
1630         // printf("%d %d %d \n", G_OBJECT(lw->utility_box)->ref_count, G_OBJECT(lw->menu_bar)->ref_count, G_OBJECT(lw->toolbar)->ref_count);
1631
1632         /* sync */
1633
1634         if (image_get_fd(lw->image))
1635                 {
1636                 layout_set_fd(lw, image_get_fd(lw->image));
1637                 }
1638         else
1639                 {
1640                 layout_set_fd(lw, dir_fd);
1641                 }
1642         image_top_window_set_sync(lw->image, (lw->options.tools_float || lw->options.tools_hidden));
1643
1644         /* clean up */
1645
1646         file_data_unref(dir_fd);
1647 }
1648
1649 void layout_colors_update(void)
1650 {
1651         GList *work;
1652
1653         work = layout_window_list;
1654         while (work)
1655                 {
1656                 gint i;
1657                 LayoutWindow *lw = work->data;
1658                 work = work->next;
1659
1660                 if (!lw->image) continue;
1661
1662                 for (i = 0; i < MAX_SPLIT_IMAGES; i++)
1663                         {
1664                         if (!lw->split_images[i]) continue;
1665                         image_background_set_color_from_options(lw->split_images[i], !!lw->full_screen);
1666                         }
1667
1668                 image_background_set_color_from_options(lw->image, !!lw->full_screen);
1669                 }
1670 }
1671
1672 void layout_tools_float_toggle(LayoutWindow *lw)
1673 {
1674         gboolean popped;
1675
1676         if (!lw) return;
1677
1678         if (!lw->options.tools_hidden)
1679                 {
1680                 popped = !lw->options.tools_float;
1681                 }
1682         else
1683                 {
1684                 popped = TRUE;
1685                 }
1686
1687         if (lw->options.tools_float == popped)
1688                 {
1689                 if (popped && lw->options.tools_hidden)
1690                         {
1691                         layout_tools_float_set(lw, popped, FALSE);
1692                         }
1693                 }
1694         else
1695                 {
1696                 if (lw->options.tools_float)
1697                         {
1698                         layout_tools_float_set(lw, FALSE, FALSE);
1699                         }
1700                 else
1701                         {
1702                         layout_tools_float_set(lw, TRUE, FALSE);
1703                         }
1704                 }
1705 }
1706
1707 void layout_tools_hide_toggle(LayoutWindow *lw)
1708 {
1709         if (!lw) return;
1710
1711         layout_tools_float_set(lw, lw->options.tools_float, !lw->options.tools_hidden);
1712 }
1713
1714 void layout_tools_float_set(LayoutWindow *lw, gboolean popped, gboolean hidden)
1715 {
1716         if (!layout_valid(&lw)) return;
1717
1718         if (lw->options.tools_float == popped && lw->options.tools_hidden == hidden) return;
1719
1720         if (lw->options.tools_float == popped && lw->options.tools_float && lw->tools)
1721                 {
1722                 layout_tools_hide(lw, hidden);
1723                 return;
1724                 }
1725
1726         lw->options.tools_float = popped;
1727         lw->options.tools_hidden = hidden;
1728
1729         layout_style_set(lw, -1, NULL);
1730 }
1731
1732 gboolean layout_tools_float_get(LayoutWindow *lw, gboolean *popped, gboolean *hidden)
1733 {
1734         if (!layout_valid(&lw)) return FALSE;
1735
1736         *popped = lw->options.tools_float;
1737         *hidden = lw->options.tools_hidden;
1738
1739         return TRUE;
1740 }
1741
1742 void layout_toolbar_toggle(LayoutWindow *lw)
1743 {
1744         if (!layout_valid(&lw)) return;
1745         if (!lw->toolbar) return;
1746
1747         lw->options.toolbar_hidden = !lw->options.toolbar_hidden;
1748
1749         if (lw->options.toolbar_hidden)
1750                 {
1751                 if (gtk_widget_get_visible(lw->toolbar[TOOLBAR_MAIN])) gtk_widget_hide(lw->toolbar[TOOLBAR_MAIN]);
1752                 }
1753         else
1754                 {
1755                 if (!gtk_widget_get_visible(lw->toolbar[TOOLBAR_MAIN])) gtk_widget_show(lw->toolbar[TOOLBAR_MAIN]);
1756                 }
1757 }
1758
1759 void layout_info_pixel_set(LayoutWindow *lw, gboolean show)
1760 {
1761         GtkWidget *frame;
1762
1763         if (!layout_valid(&lw)) return;
1764         if (!lw->info_pixel) return;
1765
1766         lw->options.show_info_pixel = show;
1767
1768         frame = gtk_widget_get_parent(lw->info_pixel);
1769         if (!lw->options.show_info_pixel)
1770                 {
1771                 gtk_widget_hide(frame);
1772                 }
1773         else
1774                 {
1775                 gtk_widget_show(frame);
1776                 }
1777
1778         g_signal_emit_by_name (lw->image->pr, "update-pixel");
1779 }
1780
1781 /*
1782  *-----------------------------------------------------------------------------
1783  * configuration
1784  *-----------------------------------------------------------------------------
1785  */
1786
1787 #define CONFIG_WINDOW_DEF_WIDTH         600
1788 #define CONFIG_WINDOW_DEF_HEIGHT        400
1789
1790 typedef struct _LayoutConfig LayoutConfig;
1791 struct _LayoutConfig
1792 {
1793         LayoutWindow *lw;
1794
1795         GtkWidget *configwindow;
1796         GtkWidget *home_path_entry;
1797         GtkWidget *layout_widget;
1798
1799         LayoutOptions options;
1800 };
1801
1802 static gint layout_config_delete_cb(GtkWidget *w, GdkEventAny *event, gpointer data);
1803
1804 static void layout_config_close_cb(GtkWidget *widget, gpointer data)
1805 {
1806         LayoutConfig *lc = data;
1807
1808         gtk_widget_destroy(lc->configwindow);
1809         free_layout_options_content(&lc->options);
1810         g_free(lc);
1811 }
1812
1813 static gint layout_config_delete_cb(GtkWidget *w, GdkEventAny *event, gpointer data)
1814 {
1815         layout_config_close_cb(w, data);
1816         return TRUE;
1817 }
1818
1819 static void layout_config_apply_cb(GtkWidget *widget, gpointer data)
1820 {
1821         LayoutConfig *lc = data;
1822
1823         g_free(lc->options.order);
1824         lc->options.order = layout_config_get(lc->layout_widget, &lc->options.style);
1825
1826         config_entry_to_option(lc->home_path_entry, &lc->options.home_path, remove_trailing_slash);
1827
1828         layout_apply_options(lc->lw, &lc->options);
1829 }
1830
1831 static void layout_config_help_cb(GtkWidget *widget, gpointer data)
1832 {
1833         help_window_show("GuideOptionsLayout.html");
1834 }
1835
1836 static void layout_config_ok_cb(GtkWidget *widget, gpointer data)
1837 {
1838         LayoutConfig *lc = data;
1839         layout_config_apply_cb(widget, lc);
1840         layout_config_close_cb(widget, lc);
1841 }
1842
1843 static void home_path_set_current_cb(GtkWidget *widget, gpointer data)
1844 {
1845         LayoutConfig *lc = data;
1846         gtk_entry_set_text(GTK_ENTRY(lc->home_path_entry), layout_get_path(lc->lw));
1847 }
1848
1849 static void startup_path_set_current_cb(GtkWidget *widget, gpointer data)
1850 {
1851         LayoutConfig *lc = data;
1852         lc->options.startup_path = STARTUP_PATH_CURRENT;
1853 }
1854
1855 static void startup_path_set_last_cb(GtkWidget *widget, gpointer data)
1856 {
1857         LayoutConfig *lc = data;
1858         lc->options.startup_path = STARTUP_PATH_LAST;
1859 }
1860
1861 static void startup_path_set_home_cb(GtkWidget *widget, gpointer data)
1862 {
1863         LayoutConfig *lc = data;
1864         lc->options.startup_path = STARTUP_PATH_HOME;
1865 }
1866
1867
1868 /*
1869 static void layout_config_save_cb(GtkWidget *widget, gpointer data)
1870 {
1871         layout_config_apply();
1872         save_options(options);
1873 }
1874 */
1875
1876 void layout_show_config_window(LayoutWindow *lw)
1877 {
1878         LayoutConfig *lc;
1879         GtkWidget *win_vbox;
1880         GtkWidget *hbox;
1881         GtkWidget *vbox;
1882         GtkWidget *button;
1883         GtkWidget *ct_button;
1884         GtkWidget *group;
1885         GtkWidget *frame;
1886         GtkWidget *tabcomp;
1887
1888         lc = g_new0(LayoutConfig, 1);
1889         lc->lw = lw;
1890         layout_sync_options_with_current_state(lw);
1891         copy_layout_options(&lc->options, &lw->options);
1892
1893         lc->configwindow = window_new(GTK_WINDOW_TOPLEVEL, "Layout", PIXBUF_INLINE_ICON_CONFIG, NULL, _("Window options and layout"));
1894         gtk_window_set_type_hint(GTK_WINDOW(lc->configwindow), GDK_WINDOW_TYPE_HINT_DIALOG);
1895
1896         g_signal_connect(G_OBJECT(lc->configwindow), "delete_event",
1897                          G_CALLBACK(layout_config_delete_cb), lc);
1898
1899         gtk_window_set_default_size(GTK_WINDOW(lc->configwindow), CONFIG_WINDOW_DEF_WIDTH, CONFIG_WINDOW_DEF_HEIGHT);
1900         gtk_window_set_resizable(GTK_WINDOW(lc->configwindow), TRUE);
1901         gtk_container_set_border_width(GTK_CONTAINER(lc->configwindow), PREF_PAD_BORDER);
1902
1903         win_vbox = gtk_vbox_new(FALSE, PREF_PAD_SPACE);
1904         gtk_container_add(GTK_CONTAINER(lc->configwindow), win_vbox);
1905         gtk_widget_show(win_vbox);
1906
1907         hbox = gtk_hbutton_box_new();
1908         gtk_button_box_set_layout(GTK_BUTTON_BOX(hbox), GTK_BUTTONBOX_END);
1909         gtk_box_set_spacing(GTK_BOX(hbox), PREF_PAD_BUTTON_GAP);
1910         gtk_box_pack_end(GTK_BOX(win_vbox), hbox, FALSE, FALSE, 0);
1911         gtk_widget_show(hbox);
1912
1913         button = pref_button_new(NULL, GTK_STOCK_OK, NULL, FALSE,
1914                                  G_CALLBACK(layout_config_ok_cb), lc);
1915         gtk_container_add(GTK_CONTAINER(hbox), button);
1916         gtk_widget_set_can_default(button, TRUE);
1917         gtk_widget_grab_default(button);
1918         gtk_widget_show(button);
1919
1920         ct_button = button;
1921 /*
1922         button = pref_button_new(NULL, GTK_STOCK_SAVE, NULL, FALSE,
1923                                  G_CALLBACK(layout_config_save_cb), NULL);
1924         gtk_container_add(GTK_CONTAINER(hbox), button);
1925         GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1926         gtk_widget_show(button);
1927 */
1928         button = pref_button_new(NULL, GTK_STOCK_HELP, NULL, FALSE,
1929                                  G_CALLBACK(layout_config_help_cb), lc);
1930         gtk_container_add(GTK_CONTAINER(hbox), button);
1931         gtk_widget_set_can_default(button, TRUE);
1932         gtk_widget_show(button);
1933
1934         button = pref_button_new(NULL, GTK_STOCK_APPLY, NULL, FALSE,
1935                                  G_CALLBACK(layout_config_apply_cb), lc);
1936         gtk_container_add(GTK_CONTAINER(hbox), button);
1937         gtk_widget_set_can_default(button, TRUE);
1938         gtk_widget_show(button);
1939
1940         button = pref_button_new(NULL, GTK_STOCK_CANCEL, NULL, FALSE,
1941                                  G_CALLBACK(layout_config_close_cb), lc);
1942         gtk_container_add(GTK_CONTAINER(hbox), button);
1943         gtk_widget_set_can_default(button, TRUE);
1944         gtk_widget_show(button);
1945
1946         if (!generic_dialog_get_alternative_button_order(lc->configwindow))
1947                 {
1948                 gtk_box_reorder_child(GTK_BOX(hbox), ct_button, -1);
1949                 }
1950
1951         frame = pref_frame_new(win_vbox, TRUE, NULL, GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
1952
1953         vbox = gtk_vbox_new(FALSE, PREF_PAD_SPACE);
1954         gtk_container_add(GTK_CONTAINER(frame), vbox);
1955         gtk_widget_show(vbox);
1956
1957
1958         group = pref_group_new(vbox, FALSE, _("General options"), GTK_ORIENTATION_VERTICAL);
1959
1960         pref_label_new(group, _("Home path (empty to use your home directory)"));
1961         hbox = pref_box_new(group, FALSE, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
1962
1963         tabcomp = tab_completion_new(&lc->home_path_entry, lc->options.home_path, NULL, NULL);
1964         tab_completion_add_select_button(lc->home_path_entry, NULL, TRUE);
1965         gtk_box_pack_start(GTK_BOX(hbox), tabcomp, TRUE, TRUE, 0);
1966         gtk_widget_show(tabcomp);
1967
1968         button = pref_button_new(hbox, NULL, _("Use current"), FALSE,
1969                                  G_CALLBACK(home_path_set_current_cb), lc);
1970
1971         pref_checkbox_new_int(group, _("Show date in directories list view"),
1972                               lc->options.show_directory_date, &lc->options.show_directory_date);
1973
1974         pref_checkbox_new_int(group, _("Exit program when this window is closed"),
1975                               lc->options.exit_on_close, &lc->options.exit_on_close);
1976
1977         group = pref_group_new(vbox, FALSE, _("Start-up directory:"), GTK_ORIENTATION_VERTICAL);
1978
1979         button = pref_radiobutton_new(group, NULL, _("No change"),
1980                                       (lc->options.startup_path == STARTUP_PATH_CURRENT),
1981                                       G_CALLBACK(startup_path_set_current_cb), lc);
1982         button = pref_radiobutton_new(group, button, _("Restore last path"),
1983                                       (lc->options.startup_path == STARTUP_PATH_LAST),
1984                                       G_CALLBACK(startup_path_set_last_cb), lc);
1985         button = pref_radiobutton_new(group, button, _("Home path"),
1986                                       (lc->options.startup_path == STARTUP_PATH_HOME),
1987                                       G_CALLBACK(startup_path_set_home_cb), lc);
1988
1989         group = pref_group_new(vbox, FALSE, _("Layout"), GTK_ORIENTATION_VERTICAL);
1990
1991         lc->layout_widget = layout_config_new();
1992         layout_config_set(lc->layout_widget, lw->options.style, lw->options.order);
1993         gtk_box_pack_start(GTK_BOX(group), lc->layout_widget, TRUE, TRUE, 0);
1994
1995         gtk_widget_show(lc->layout_widget);
1996         gtk_widget_show(lc->configwindow);
1997 }
1998
1999 /*
2000  *-----------------------------------------------------------------------------
2001  * base
2002  *-----------------------------------------------------------------------------
2003  */
2004
2005 void layout_sync_options_with_current_state(LayoutWindow *lw)
2006 {
2007         Histogram *histogram;
2008         if (!layout_valid(&lw)) return;
2009
2010         lw->options.main_window.maximized =  window_maximized(lw->window);
2011         if (!lw->options.main_window.maximized)
2012                 {
2013                 layout_geometry_get(lw, &lw->options.main_window.x, &lw->options.main_window.y,
2014                                     &lw->options.main_window.w, &lw->options.main_window.h);
2015                 }
2016
2017         layout_geometry_get_dividers(lw, &lw->options.main_window.hdivider_pos, &lw->options.main_window.vdivider_pos);
2018
2019 //      layout_sort_get(NULL, &options->file_sort.method, &options->file_sort.ascending);
2020
2021         layout_geometry_get_tools(lw, &lw->options.float_window.x, &lw->options.float_window.y,
2022                                   &lw->options.float_window.w, &lw->options.float_window.h, &lw->options.float_window.vdivider_pos);
2023
2024         lw->options.image_overlay.state = image_osd_get(lw->image);
2025         histogram = image_osd_get_histogram(lw->image);
2026
2027         lw->options.image_overlay.histogram_channel = histogram->histogram_channel;
2028         lw->options.image_overlay.histogram_mode = histogram->histogram_mode;
2029
2030         g_free(lw->options.last_path);
2031         lw->options.last_path = g_strdup(layout_get_path(lw));
2032 }
2033
2034 void layout_apply_options(LayoutWindow *lw, LayoutOptions *lop)
2035 {
2036         gboolean refresh_style;
2037         gboolean refresh_lists;
2038
2039         if (!layout_valid(&lw)) return;
2040 /* FIXME: add other options too */
2041
2042         refresh_style = (lop->style != lw->options.style || strcmp(lop->order, lw->options.order) != 0);
2043         refresh_lists = (lop->show_directory_date != lw->options.show_directory_date);
2044
2045         copy_layout_options(&lw->options, lop);
2046
2047         if (refresh_style) layout_style_set(lw, lw->options.style, lw->options.order);
2048         if (refresh_lists) layout_refresh(lw);
2049 }
2050
2051
2052 void layout_close(LayoutWindow *lw)
2053 {
2054         if (!lw->options.exit_on_close && layout_window_list && layout_window_list->next)
2055                 {
2056                 layout_free(lw);
2057                 }
2058         else
2059                 {
2060                 exit_program();
2061                 }
2062 }
2063
2064 void layout_free(LayoutWindow *lw)
2065 {
2066         gint i;
2067         if (!lw) return;
2068
2069         layout_window_list = g_list_remove(layout_window_list, lw);
2070         if (current_lw == lw) current_lw = NULL;
2071
2072         if (lw->exif_window) g_signal_handlers_disconnect_matched(G_OBJECT(lw->exif_window), G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, lw);
2073
2074         layout_bars_close(lw);
2075
2076         g_object_unref(lw->menu_bar);
2077         g_object_unref(lw->utility_box);
2078
2079         for (i = 0; i < TOOLBAR_COUNT; i++)
2080                 {
2081                 if (lw->toolbar[i]) g_object_unref(lw->toolbar[i]);
2082                 }
2083
2084         gtk_widget_destroy(lw->window);
2085
2086         if (lw->split_image_sizegroup) g_object_unref(lw->split_image_sizegroup);
2087
2088         file_data_unregister_notify_func(layout_image_notify_cb, lw);
2089
2090         if (lw->dir_fd)
2091                 {
2092                 file_data_unregister_real_time_monitor(lw->dir_fd);
2093                 file_data_unref(lw->dir_fd);
2094                 }
2095
2096         free_layout_options_content(&lw->options);
2097         g_free(lw);
2098 }
2099
2100 static gboolean layout_delete_cb(GtkWidget *widget, GdkEventAny *event, gpointer data)
2101 {
2102         LayoutWindow *lw = data;
2103
2104         layout_close(lw);
2105         return TRUE;
2106 }
2107
2108 LayoutWindow *layout_new(FileData *dir_fd, LayoutOptions *lop)
2109 {
2110         return layout_new_with_geometry(dir_fd, lop, NULL);
2111 }
2112
2113 LayoutWindow *layout_new_with_geometry(FileData *dir_fd, LayoutOptions *lop,
2114                                        const gchar *geometry)
2115 {
2116         LayoutWindow *lw;
2117         GdkGeometry hint;
2118         GdkWindowHints hint_mask;
2119         Histogram *histogram;
2120
2121         DEBUG_1("%s layout_new: start", get_exec_time());
2122         lw = g_new0(LayoutWindow, 1);
2123
2124         if (lop)
2125                 copy_layout_options(&lw->options, lop);
2126         else
2127                 init_layout_options(&lw->options);
2128
2129         lw->sort_method = SORT_NAME;
2130         lw->sort_ascend = TRUE;
2131
2132         layout_set_unique_id(lw);
2133 //      lw->options.tools_float = popped;
2134 //      lw->options.tools_hidden = hidden;
2135 //      lw->bar_sort_enabled = options->panels.sort.enabled;
2136 //      lw->bar_enabled = options->panels.info.enabled;
2137
2138         /* default layout */
2139
2140         layout_config_parse(lw->options.style, lw->options.order,
2141                             &lw->dir_location,  &lw->file_location, &lw->image_location);
2142         if (lw->options.dir_view_type >= VIEW_DIR_TYPES_COUNT) lw->options.dir_view_type = 0;
2143         if (lw->options.file_view_type >= VIEW_FILE_TYPES_COUNT) lw->options.file_view_type = 0;
2144
2145         /* divider positions */
2146
2147         if (!options->save_window_positions)
2148                 {
2149                 lw->options.main_window.hdivider_pos = MAIN_WINDOW_DIV_HPOS;
2150                 lw->options.main_window.vdivider_pos = MAIN_WINDOW_DIV_VPOS;
2151                 lw->options.float_window.vdivider_pos = MAIN_WINDOW_DIV_VPOS;
2152                 }
2153
2154         /* window */
2155
2156         lw->window = window_new(GTK_WINDOW_TOPLEVEL, GQ_APPNAME_LC, NULL, NULL, NULL);
2157         gtk_window_set_resizable(GTK_WINDOW(lw->window), TRUE);
2158         gtk_container_set_border_width(GTK_CONTAINER(lw->window), 0);
2159
2160         if (options->save_window_positions)
2161                 {
2162                 hint_mask = GDK_HINT_USER_POS;
2163                 }
2164         else
2165                 {
2166                 hint_mask = 0;
2167                 }
2168
2169         hint.min_width = 32;
2170         hint.min_height = 32;
2171         hint.base_width = 0;
2172         hint.base_height = 0;
2173         gtk_window_set_geometry_hints(GTK_WINDOW(lw->window), NULL, &hint,
2174                                       GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE | hint_mask);
2175
2176         if (options->save_window_positions)
2177                 {
2178                 gtk_window_set_default_size(GTK_WINDOW(lw->window), lw->options.main_window.w, lw->options.main_window.h);
2179 //              if (!layout_window_list)
2180 //                      {
2181                 gtk_window_move(GTK_WINDOW(lw->window), lw->options.main_window.x, lw->options.main_window.y);
2182                 if (lw->options.main_window.maximized) gtk_window_maximize(GTK_WINDOW(lw->window));
2183 //                      }
2184                 }
2185         else
2186                 {
2187                 gtk_window_set_default_size(GTK_WINDOW(lw->window), MAINWINDOW_DEF_WIDTH, MAINWINDOW_DEF_HEIGHT);
2188                 }
2189
2190         g_signal_connect(G_OBJECT(lw->window), "delete_event",
2191                          G_CALLBACK(layout_delete_cb), lw);
2192
2193         g_signal_connect(G_OBJECT(lw->window), "focus-in-event",
2194                          G_CALLBACK(layout_set_current_cb), lw);
2195
2196         layout_keyboard_init(lw, lw->window);
2197
2198 #ifdef HAVE_LIRC
2199         layout_image_lirc_init(lw);
2200 #endif
2201
2202         lw->main_box = gtk_vbox_new(FALSE, 0);
2203         gtk_container_add(GTK_CONTAINER(lw->window), lw->main_box);
2204         gtk_widget_show(lw->main_box);
2205
2206         layout_grid_setup(lw);
2207         image_top_window_set_sync(lw->image, (lw->options.tools_float || lw->options.tools_hidden));
2208
2209         layout_util_sync(lw);
2210         layout_status_update_all(lw);
2211
2212         if (dir_fd)
2213                 {
2214                 layout_set_fd(lw, dir_fd);
2215                 }
2216         else
2217                 {
2218                 GdkPixbuf *pixbuf;
2219
2220                 pixbuf = pixbuf_inline(PIXBUF_INLINE_LOGO);
2221
2222                 /* FIXME: the zoom value set here is the value, which is then copied again and again
2223                    in "Leave Zoom at previous setting" mode. This is not ideal.  */
2224                 image_change_pixbuf(lw->image, pixbuf, 0.0, FALSE);
2225                 g_object_unref(pixbuf);
2226                 }
2227
2228         if (geometry)
2229                 {
2230                 if (!gtk_window_parse_geometry(GTK_WINDOW(lw->window), geometry))
2231                         {
2232                         log_printf("%s", _("Invalid geometry\n"));
2233                         }
2234                 }
2235
2236         gtk_widget_show(lw->window);
2237         layout_tools_hide(lw, lw->options.tools_hidden);
2238
2239         image_osd_set(lw->image, lw->options.image_overlay.state);
2240         histogram = image_osd_get_histogram(lw->image);
2241
2242         histogram->histogram_channel = lw->options.image_overlay.histogram_channel;
2243         histogram->histogram_mode = lw->options.image_overlay.histogram_mode;
2244
2245         layout_window_list = g_list_append(layout_window_list, lw);
2246
2247         file_data_register_notify_func(layout_image_notify_cb, lw, NOTIFY_PRIORITY_LOW);
2248
2249         DEBUG_1("%s layout_new: end", get_exec_time());
2250
2251         return lw;
2252 }
2253
2254 void layout_write_attributes(LayoutOptions *layout, GString *outstr, gint indent)
2255 {
2256         WRITE_NL(); WRITE_CHAR(*layout, id);
2257
2258         WRITE_NL(); WRITE_INT(*layout, style);
2259         WRITE_NL(); WRITE_CHAR(*layout, order);
2260         WRITE_NL(); WRITE_UINT(*layout, dir_view_type);
2261         WRITE_NL(); WRITE_UINT(*layout, file_view_type);
2262         WRITE_NL(); WRITE_BOOL(*layout, show_marks);
2263         WRITE_NL(); WRITE_BOOL(*layout, show_thumbnails);
2264         WRITE_NL(); WRITE_BOOL(*layout, show_directory_date);
2265         WRITE_NL(); WRITE_CHAR(*layout, home_path);
2266         WRITE_NL(); WRITE_CHAR(*layout, last_path);
2267         WRITE_NL(); WRITE_UINT(*layout, startup_path);
2268         WRITE_NL(); WRITE_BOOL(*layout, exit_on_close);
2269         WRITE_SEPARATOR();
2270
2271         WRITE_NL(); WRITE_INT(*layout, main_window.x);
2272         WRITE_NL(); WRITE_INT(*layout, main_window.y);
2273         WRITE_NL(); WRITE_INT(*layout, main_window.w);
2274         WRITE_NL(); WRITE_INT(*layout, main_window.h);
2275         WRITE_NL(); WRITE_BOOL(*layout, main_window.maximized);
2276         WRITE_NL(); WRITE_INT(*layout, main_window.hdivider_pos);
2277         WRITE_NL(); WRITE_INT(*layout, main_window.vdivider_pos);
2278         WRITE_SEPARATOR();
2279
2280         WRITE_NL(); WRITE_INT(*layout, float_window.x);
2281         WRITE_NL(); WRITE_INT(*layout, float_window.y);
2282         WRITE_NL(); WRITE_INT(*layout, float_window.w);
2283         WRITE_NL(); WRITE_INT(*layout, float_window.h);
2284         WRITE_NL(); WRITE_INT(*layout, float_window.vdivider_pos);
2285         WRITE_SEPARATOR();
2286
2287         WRITE_NL(); WRITE_INT(*layout, properties_window.w);
2288         WRITE_NL(); WRITE_INT(*layout, properties_window.h);
2289         WRITE_SEPARATOR();
2290
2291         WRITE_NL(); WRITE_BOOL(*layout, tools_float);
2292         WRITE_NL(); WRITE_BOOL(*layout, tools_hidden);
2293         WRITE_SEPARATOR();
2294
2295         WRITE_NL(); WRITE_BOOL(*layout, toolbar_hidden);
2296         WRITE_NL(); WRITE_BOOL(*layout, show_info_pixel);
2297
2298         WRITE_NL(); WRITE_UINT(*layout, image_overlay.state);
2299         WRITE_NL(); WRITE_INT(*layout, image_overlay.histogram_channel);
2300         WRITE_NL(); WRITE_INT(*layout, image_overlay.histogram_mode);
2301
2302         WRITE_NL(); WRITE_BOOL(*layout, animate);
2303 }
2304
2305
2306 void layout_write_config(LayoutWindow *lw, GString *outstr, gint indent)
2307 {
2308         layout_sync_options_with_current_state(lw);
2309         WRITE_NL(); WRITE_STRING("<layout");
2310         layout_write_attributes(&lw->options, outstr, indent + 1);
2311         WRITE_STRING(">");
2312
2313         bar_sort_write_config(lw->bar_sort, outstr, indent + 1);
2314         bar_write_config(lw->bar, outstr, indent + 1);
2315
2316         WRITE_NL(); WRITE_STRING("</layout>");
2317 }
2318
2319 void layout_load_attributes(LayoutOptions *layout, const gchar **attribute_names, const gchar **attribute_values)
2320 {
2321         gchar *id = NULL;
2322
2323         while (*attribute_names)
2324                 {
2325                 const gchar *option = *attribute_names++;
2326                 const gchar *value = *attribute_values++;
2327
2328                 /* layout options */
2329                 if (READ_CHAR_FULL("id", id)) continue;
2330
2331                 if (READ_INT(*layout, style)) continue;
2332                 if (READ_CHAR(*layout, order)) continue;
2333
2334                 if (READ_UINT(*layout, dir_view_type)) continue;
2335                 if (READ_UINT(*layout, file_view_type)) continue;
2336                 if (READ_BOOL(*layout, show_marks)) continue;
2337                 if (READ_BOOL(*layout, show_thumbnails)) continue;
2338                 if (READ_BOOL(*layout, show_directory_date)) continue;
2339                 if (READ_CHAR(*layout, home_path)) continue;
2340                 if (READ_CHAR(*layout, last_path)) continue;
2341                 if (READ_UINT_CLAMP(*layout, startup_path, 0, STARTUP_PATH_HOME)) continue;
2342                 if (READ_BOOL(*layout, exit_on_close)) continue;
2343
2344                 /* window positions */
2345
2346                 if (READ_INT(*layout, main_window.x)) continue;
2347                 if (READ_INT(*layout, main_window.y)) continue;
2348                 if (READ_INT(*layout, main_window.w)) continue;
2349                 if (READ_INT(*layout, main_window.h)) continue;
2350                 if (READ_BOOL(*layout, main_window.maximized)) continue;
2351                 if (READ_INT(*layout, main_window.hdivider_pos)) continue;
2352                 if (READ_INT(*layout, main_window.vdivider_pos)) continue;
2353
2354                 if (READ_INT(*layout, float_window.x)) continue;
2355                 if (READ_INT(*layout, float_window.y)) continue;
2356                 if (READ_INT(*layout, float_window.w)) continue;
2357                 if (READ_INT(*layout, float_window.h)) continue;
2358                 if (READ_INT(*layout, float_window.vdivider_pos)) continue;
2359
2360                 if (READ_INT(*layout, properties_window.w)) continue;
2361                 if (READ_INT(*layout, properties_window.h)) continue;
2362
2363                 if (READ_BOOL(*layout, tools_float)) continue;
2364                 if (READ_BOOL(*layout, tools_hidden)) continue;
2365                 if (READ_BOOL(*layout, toolbar_hidden)) continue;
2366                 if (READ_BOOL(*layout, show_info_pixel)) continue;
2367
2368                 if (READ_UINT(*layout, image_overlay.state)) continue;
2369                 if (READ_INT(*layout, image_overlay.histogram_channel)) continue;
2370                 if (READ_INT(*layout, image_overlay.histogram_mode)) continue;
2371
2372                 if (READ_BOOL(*layout, animate)) continue;
2373
2374                 log_printf("unknown attribute %s = %s\n", option, value);
2375                 }
2376         if (id && strcmp(id, LAYOUT_ID_CURRENT) != 0)
2377                 {
2378                 g_free(layout->id);
2379                 layout->id = id;
2380                 }
2381         else
2382                 {
2383                 g_free(id);
2384                 }
2385 }
2386
2387 static void layout_config_startup_path(LayoutOptions *lop, gchar **path)
2388 {
2389         switch (lop->startup_path)
2390                 {
2391                 case STARTUP_PATH_LAST:
2392                         *path = (lop->last_path && isdir(lop->last_path)) ? g_strdup(lop->last_path) : get_current_dir();
2393                         break;
2394                 case STARTUP_PATH_HOME:
2395                         *path = (lop->home_path && isdir(lop->home_path)) ? g_strdup(lop->home_path) : g_strdup(homedir());
2396                         break;
2397                 default:
2398                         *path = get_current_dir();
2399                         break;
2400                 }
2401 }
2402
2403
2404 static void layout_config_commandline(LayoutOptions *lop, gchar **path)
2405 {
2406         if (command_line->startup_blank)
2407                 {
2408                 *path = NULL;
2409                 }
2410         else if (command_line->file)
2411                 {
2412                 *path = g_strdup(command_line->file);
2413                 }
2414         else if (command_line->path)
2415                 {
2416                 *path = g_strdup(command_line->path);
2417                 }
2418         else layout_config_startup_path(lop, path);
2419
2420         if (command_line->tools_show)
2421                 {
2422                 lop->tools_float = FALSE;
2423                 lop->tools_hidden = FALSE;
2424                 }
2425         else if (command_line->tools_hide)
2426                 {
2427                 lop->tools_hidden = TRUE;
2428                 }
2429 }
2430
2431 LayoutWindow *layout_new_from_config(const gchar **attribute_names, const gchar **attribute_values, gboolean use_commandline)
2432 {
2433         LayoutOptions lop;
2434         LayoutWindow *lw;
2435         gchar *path = NULL;
2436
2437         init_layout_options(&lop);
2438
2439         if (attribute_names) layout_load_attributes(&lop, attribute_names, attribute_values);
2440
2441         if (use_commandline)
2442                 {
2443                 layout_config_commandline(&lop, &path);
2444                 }
2445         else
2446                 {
2447                 layout_config_startup_path(&lop, &path);
2448                 }
2449
2450         lw = layout_new_with_geometry(NULL, &lop, use_commandline ? command_line->geometry : NULL);
2451         layout_sort_set(lw, options->file_sort.method, options->file_sort.ascending);
2452         layout_set_path(lw, path);
2453
2454         if (use_commandline && command_line->startup_full_screen) layout_image_full_screen_start(lw);
2455         if (use_commandline && command_line->startup_in_slideshow) layout_image_slideshow_start(lw);
2456
2457
2458         g_free(path);
2459         free_layout_options_content(&lop);
2460         return lw;
2461 }
2462
2463 void layout_update_from_config(LayoutWindow *lw, const gchar **attribute_names, const gchar **attribute_values)
2464 {
2465         LayoutOptions lop;
2466
2467         init_layout_options(&lop);
2468
2469         if (attribute_names) layout_load_attributes(&lop, attribute_names, attribute_values);
2470
2471         layout_apply_options(lw, &lop);
2472
2473         free_layout_options_content(&lop);
2474 }
2475
2476
2477 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */