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