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