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