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