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