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