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