Start with "Fit to window" in "Leave Zoom at previous setting" mode
[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
37 #ifdef HAVE_LIRC
38 #include "lirc.h"
39 #endif
40
41 #define MAINWINDOW_DEF_WIDTH 700
42 #define MAINWINDOW_DEF_HEIGHT 500
43
44 #define MAIN_WINDOW_DIV_HPOS (MAINWINDOW_DEF_WIDTH / 2)
45 #define MAIN_WINDOW_DIV_VPOS (MAINWINDOW_DEF_HEIGHT / 2)
46
47 #define TOOLWINDOW_DEF_WIDTH 260
48 #define TOOLWINDOW_DEF_HEIGHT 450
49
50 #define PROGRESS_WIDTH 150
51 #define ZOOM_LABEL_WIDTH 64
52
53 #define PANE_DIVIDER_SIZE 10
54
55
56 GList *layout_window_list = NULL;
57
58
59 static void layout_list_scroll_to_subpart(LayoutWindow *lw, const gchar *needle);
60
61
62 /*
63  *-----------------------------------------------------------------------------
64  * misc
65  *-----------------------------------------------------------------------------
66  */
67
68 gint layout_valid(LayoutWindow **lw)
69 {
70         if (*lw == NULL)
71                 {
72                 if (layout_window_list) *lw = layout_window_list->data;
73                 return (*lw != NULL);
74                 }
75
76         return (g_list_find(layout_window_list, *lw) != NULL);
77 }
78
79 LayoutWindow *layout_find_by_image(ImageWindow *imd)
80 {
81         GList *work;
82
83         work = layout_window_list;
84         while (work)
85                 {
86                 LayoutWindow *lw = work->data;
87                 work = work->next;
88
89                 if (lw->image == imd) return lw;
90                 }
91
92         return NULL;
93 }
94
95 LayoutWindow *layout_find_by_image_fd(ImageWindow *imd)
96 {
97         GList *work;
98
99         work = layout_window_list;
100         while (work)
101                 {
102                 LayoutWindow *lw = work->data;
103                 work = work->next;
104                 if (lw->image->image_fd == imd->image_fd)
105                         return lw;
106
107                 }
108
109         return NULL;
110 }
111
112 /*
113  *-----------------------------------------------------------------------------
114  * menu, toolbar, and dir view
115  *-----------------------------------------------------------------------------
116  */
117
118 static void layout_path_entry_changed_cb(GtkWidget *widget, gpointer data)
119 {
120         LayoutWindow *lw = data;
121         gchar *buf;
122
123         if (gtk_combo_box_get_active(GTK_COMBO_BOX(widget)) < 0) return;
124
125         buf = g_strdup(gtk_entry_get_text(GTK_ENTRY(lw->path_entry)));
126         if (!buf || (lw->dir_fd && strcmp(buf, lw->dir_fd->path) == 0))
127                 {
128                 g_free(buf);
129                 return;
130                 }
131
132         layout_set_path(lw, buf);
133
134         g_free(buf);
135 }
136
137 static void layout_path_entry_tab_cb(const gchar *path, gpointer data)
138 {
139         LayoutWindow *lw = data;
140         gchar *buf;
141         gchar *base;
142
143         buf = g_strdup(path);
144         parse_out_relatives(buf);
145         base = remove_level_from_path(buf);
146
147         if (isdir(buf))
148                 {
149                 if ((!lw->dir_fd || strcmp(lw->dir_fd->path, buf) != 0) && layout_set_path(lw, buf))
150                         {
151                         gint pos = -1;
152                         /* put the G_DIR_SEPARATOR back, if we are in tab completion for a dir and result was path change */
153                         gtk_editable_insert_text(GTK_EDITABLE(lw->path_entry), G_DIR_SEPARATOR_S, -1, &pos);
154                         gtk_editable_set_position(GTK_EDITABLE(lw->path_entry),
155                                                   strlen(gtk_entry_get_text(GTK_ENTRY(lw->path_entry))));
156                         }
157                 }
158         else if (lw->dir_fd && strcmp(lw->dir_fd->path, base) == 0)
159                 {
160                 layout_list_scroll_to_subpart(lw, filename_from_path(buf));
161                 }
162
163         g_free(base);
164         g_free(buf);
165 }
166
167 static void layout_path_entry_cb(const gchar *path, gpointer data)
168 {
169         LayoutWindow *lw = data;
170         gchar *buf;
171
172         buf = g_strdup(path);
173         parse_out_relatives(buf);
174
175         layout_set_path(lw, buf);
176
177         g_free(buf);
178 }
179
180 static void layout_vd_select_cb(ViewDir *vd, const gchar *path, gpointer data)
181 {
182         LayoutWindow *lw = data;
183
184         layout_set_path(lw, path);
185 }
186
187 static void layout_path_entry_tab_append_cb(const gchar *path, gpointer data, gint n)
188 {
189         LayoutWindow *lw = data;
190
191         if (!lw || !lw->back_button) return;
192         if (!layout_valid(&lw)) return;
193
194         if (n >= 2)
195                 {
196                 /* Enable back button */
197                 gtk_widget_set_sensitive(lw->back_button, TRUE);
198                 }
199         else
200                 {
201                 /* Disable back button */
202                 gtk_widget_set_sensitive(lw->back_button, FALSE);
203                 }
204 }
205
206 static GtkWidget *layout_tool_setup(LayoutWindow *lw)
207 {
208         GtkWidget *box;
209         GtkWidget *menu_bar;
210         GtkWidget *tabcomp;
211
212         box = gtk_vbox_new(FALSE, 0);
213
214         menu_bar = layout_actions_menu_bar(lw);
215         gtk_box_pack_start(GTK_BOX(box), menu_bar, FALSE, FALSE, 0);
216         gtk_widget_show(menu_bar);
217
218         lw->toolbar = layout_button_bar(lw);
219         gtk_box_pack_start(GTK_BOX(box), lw->toolbar, FALSE, FALSE, 0);
220         if (!lw->toolbar_hidden) gtk_widget_show(lw->toolbar);
221
222         tabcomp = tab_completion_new_with_history(&lw->path_entry, NULL, "path_list", -1,
223                                                   layout_path_entry_cb, lw);
224         tab_completion_add_tab_func(lw->path_entry, layout_path_entry_tab_cb, lw);
225         tab_completion_add_append_func(lw->path_entry, layout_path_entry_tab_append_cb, lw);
226         gtk_box_pack_start(GTK_BOX(box), tabcomp, FALSE, FALSE, 0);
227         gtk_widget_show(tabcomp);
228
229         g_signal_connect(G_OBJECT(lw->path_entry->parent), "changed",
230                          G_CALLBACK(layout_path_entry_changed_cb), lw);
231
232         lw->vd = vd_new(lw->dir_view_type, lw->dir_fd);
233         vd_set_layout(lw->vd, lw);
234         vd_set_select_func(lw->vd, layout_vd_select_cb, lw);
235
236         lw->dir_view = lw->vd->widget;
237
238         gtk_box_pack_start(GTK_BOX(box), lw->dir_view, TRUE, TRUE, 0);
239         gtk_widget_show(lw->dir_view);
240
241         gtk_widget_show(box);
242
243         return box;
244 }
245
246 /*
247  *-----------------------------------------------------------------------------
248  * sort button (and menu)
249  *-----------------------------------------------------------------------------
250  */
251
252 static void layout_sort_menu_cb(GtkWidget *widget, gpointer data)
253 {
254         LayoutWindow *lw;
255         SortType type;
256
257         if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) return;
258
259         lw = submenu_item_get_data(widget);
260         if (!lw) return;
261
262         type = (SortType)GPOINTER_TO_INT(data);
263
264         layout_sort_set(lw, type, lw->sort_ascend);
265 }
266
267 static void layout_sort_menu_ascend_cb(GtkWidget *widget, gpointer data)
268 {
269         LayoutWindow *lw = data;
270
271         layout_sort_set(lw, lw->sort_method, !lw->sort_ascend);
272 }
273
274 static void layout_sort_menu_hide_cb(GtkWidget *widget, gpointer data)
275 {
276         /* destroy the menu */
277 #if GTK_CHECK_VERSION(2,12,0)
278         g_object_unref(widget);
279 #else
280         gtk_widget_unref(GTK_WIDGET(widget));
281 #endif
282 }
283
284 static void layout_sort_button_press_cb(GtkWidget *widget, gpointer data)
285 {
286         LayoutWindow *lw = data;
287         GtkWidget *menu;
288         GdkEvent *event;
289         guint32 etime;
290
291         menu = submenu_add_sort(NULL, G_CALLBACK(layout_sort_menu_cb), lw, FALSE, FALSE, TRUE, lw->sort_method);
292
293         /* take ownership of menu */
294 #ifdef GTK_OBJECT_FLOATING
295         /* GTK+ < 2.10 */
296         g_object_ref(G_OBJECT(menu));
297         gtk_object_sink(GTK_OBJECT(menu));
298 #else
299         /* GTK+ >= 2.10 */
300         g_object_ref_sink(G_OBJECT(menu));
301 #endif
302
303         /* ascending option */
304         menu_item_add_divider(menu);
305         menu_item_add_check(menu, _("Ascending"), lw->sort_ascend, G_CALLBACK(layout_sort_menu_ascend_cb), lw);
306
307         g_signal_connect(G_OBJECT(menu), "selection_done",
308                          G_CALLBACK(layout_sort_menu_hide_cb), NULL);
309
310         event = gtk_get_current_event();
311         if (event)
312                 {
313                 etime = gdk_event_get_time(event);
314                 gdk_event_free(event);
315                 }
316         else
317                 {
318                 etime = 0;
319                 }
320
321         gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, 0, etime);
322 }
323
324 static GtkWidget *layout_sort_button(LayoutWindow *lw)
325 {
326         GtkWidget *button;
327
328         button = gtk_button_new_with_label(sort_type_get_text(lw->sort_method));
329         g_signal_connect(G_OBJECT(button), "clicked",
330                          G_CALLBACK(layout_sort_button_press_cb), lw);
331         gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE);
332
333         return button;
334 }
335
336 /*
337  *-----------------------------------------------------------------------------
338  * color profile button (and menu)
339  *-----------------------------------------------------------------------------
340  */
341
342 #ifdef HAVE_LCMS
343
344 static void layout_color_menu_enable_cb(GtkWidget *widget, gpointer data)
345 {
346         LayoutWindow *lw = data;
347
348         layout_image_color_profile_set_use(lw, (!layout_image_color_profile_get_use(lw)));
349         layout_image_refresh(lw);
350 }
351
352 static void layout_color_menu_use_image_cb(GtkWidget *widget, gpointer data)
353 {
354         LayoutWindow *lw = data;
355         gint input, screen, use_image;
356
357         if (!layout_image_color_profile_get(lw, &input, &screen, &use_image)) return;
358         layout_image_color_profile_set(lw, input, screen, !use_image);
359         layout_image_refresh(lw);
360 }
361
362 #define COLOR_MENU_KEY "color_menu_key"
363
364 static void layout_color_menu_input_cb(GtkWidget *widget, gpointer data)
365 {
366         LayoutWindow *lw = data;
367         gint type;
368         gint input, screen, use_image;
369
370         if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) return;
371
372         type = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), COLOR_MENU_KEY));
373         if (type < 0 || type >= COLOR_PROFILE_FILE + COLOR_PROFILE_INPUTS) return;
374
375         if (!layout_image_color_profile_get(lw, &input, &screen, &use_image)) return;
376         if (type == input) return;
377
378         layout_image_color_profile_set(lw, type, screen, use_image);
379         layout_image_refresh(lw);
380 }
381
382 static void layout_color_menu_screen_cb(GtkWidget *widget, gpointer data)
383 {
384         LayoutWindow *lw = data;
385         gint type;
386         gint input, screen, use_image;
387
388         if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) return;
389
390         type = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), COLOR_MENU_KEY));
391         if (type < 0 || type > 1) return;
392
393         if (!layout_image_color_profile_get(lw, &input, &screen, &use_image)) return;
394         if (type == screen) return;
395
396         layout_image_color_profile_set(lw, input, type, use_image);
397         layout_image_refresh(lw);
398 }
399
400 static gchar *layout_color_name_parse(const gchar *name)
401 {
402         if (!name) return g_strdup(_("Empty"));
403         return g_strdelimit(g_strdup(name), "_", '-');
404 }
405
406 #endif /* HAVE_LCMS */
407
408 static void layout_color_button_press_cb(GtkWidget *widget, gpointer data)
409 {
410 #ifndef HAVE_LCMS
411         gchar *msg = g_strdup_printf(_("This installation of %s was not built with support for color profiles."), GQ_APPNAME);
412         file_util_warning_dialog(_("Color profiles not supported"),
413                                  msg,
414                                  GTK_STOCK_DIALOG_INFO, widget);
415         g_free(msg);
416         return;
417 #else
418         LayoutWindow *lw = data;
419         GtkWidget *menu;
420         GtkWidget *item;
421         gchar *buf;
422         gint active;
423         gint input = 0;
424         gint screen = 0;
425         gint use_image = 0;
426         gint from_image = 0;
427         gint i;
428         
429         if (!layout_image_color_profile_get(lw, &input, &screen, &use_image)) return;
430
431         from_image = use_image && (layout_image_color_profile_get_from_image(lw) != COLOR_PROFILE_NONE);
432         menu = popup_menu_short_lived();
433
434         active = layout_image_color_profile_get_use(lw);
435         menu_item_add_check(menu, _("Use _color profiles"), active,
436                             G_CALLBACK(layout_color_menu_enable_cb), lw);
437
438         menu_item_add_divider(menu);
439
440         item = menu_item_add_check(menu, _("Use profile from _image"), use_image,
441                             G_CALLBACK(layout_color_menu_use_image_cb), lw);
442         gtk_widget_set_sensitive(item, active);
443
444         for (i = COLOR_PROFILE_SRGB; i < COLOR_PROFILE_FILE; i++)
445                 {
446                 const gchar *label;
447
448                 switch (i)
449                         {
450                         case COLOR_PROFILE_SRGB:        label = _("sRGB"); break;
451                         case COLOR_PROFILE_ADOBERGB:    label = _("AdobeRGB compatible"); break;
452                         default:                        label = "fixme"; break;
453                         }
454                 buf = g_strdup_printf(_("Input _%d: %s"), i, label);
455                 item = menu_item_add_radio(menu, (i == COLOR_PROFILE_SRGB) ? NULL : item,
456                                    buf, (input == i),
457                                    G_CALLBACK(layout_color_menu_input_cb), lw);
458                 g_free(buf);
459                 g_object_set_data(G_OBJECT(item), COLOR_MENU_KEY, GINT_TO_POINTER(i));
460                 gtk_widget_set_sensitive(item, active && !from_image);
461                 }
462
463         for (i = 0; i < COLOR_PROFILE_INPUTS; i++)
464                 {
465                 const gchar *name;
466                 gchar *end;
467
468                 name = options->color_profile.input_name[i];
469                 if (!name) name = filename_from_path(options->color_profile.input_file[i]);
470
471                 end = layout_color_name_parse(name);
472                 buf = g_strdup_printf(_("Input _%d: %s"), i + COLOR_PROFILE_FILE, end);
473                 g_free(end);
474
475                 item = menu_item_add_radio(menu, item,
476                                            buf, (i + COLOR_PROFILE_FILE == input),
477                                            G_CALLBACK(layout_color_menu_input_cb), lw);
478                 g_free(buf);
479                 g_object_set_data(G_OBJECT(item), COLOR_MENU_KEY, GINT_TO_POINTER(i + COLOR_PROFILE_FILE));
480                 gtk_widget_set_sensitive(item, active && options->color_profile.input_file[i] && !from_image);
481                 }
482
483         menu_item_add_divider(menu);
484
485         item = menu_item_add_radio(menu, NULL,
486                                    _("Screen sRGB"), (screen == 0),
487                                    G_CALLBACK(layout_color_menu_screen_cb), lw);
488
489         g_object_set_data(G_OBJECT(item), COLOR_MENU_KEY, GINT_TO_POINTER(0));
490         gtk_widget_set_sensitive(item, active);
491
492         item = menu_item_add_radio(menu, item,
493                                    _("_Screen profile"), (screen == 1),
494                                    G_CALLBACK(layout_color_menu_screen_cb), lw);
495         g_object_set_data(G_OBJECT(item), COLOR_MENU_KEY, GINT_TO_POINTER(1));
496         gtk_widget_set_sensitive(item, active && options->color_profile.screen_file);
497
498         gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, 0, GDK_CURRENT_TIME);
499 #endif /* HAVE_LCMS */
500 }
501
502 static GtkWidget *layout_color_button(LayoutWindow *lw)
503 {
504         GtkWidget *button;
505         GtkWidget *image;
506         gint enable;
507
508         button = gtk_button_new();
509         image = gtk_image_new_from_stock(GTK_STOCK_SELECT_COLOR, GTK_ICON_SIZE_MENU);
510         gtk_container_add(GTK_CONTAINER(button), image);
511         gtk_widget_show(image);
512         g_signal_connect(G_OBJECT(button), "clicked",
513                          G_CALLBACK(layout_color_button_press_cb), lw);
514         gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE);
515
516 #ifdef HAVE_LCMS
517         enable = (lw->image) ? lw->image->color_profile_enable : FALSE;
518 #else
519         enable = FALSE;
520 #endif
521         gtk_widget_set_sensitive(image, enable);
522
523         return button;
524 }
525
526 /*
527  *-----------------------------------------------------------------------------
528  * write button
529  *-----------------------------------------------------------------------------
530  */
531
532 static void layout_write_button_press_cb(GtkWidget *widget, gpointer data)
533 {
534         metadata_write_queue_confirm(NULL, NULL);
535 }
536
537 static GtkWidget *layout_write_button(LayoutWindow *lw)
538 {
539         GtkWidget *button;
540         GtkWidget *image;
541
542         button = gtk_button_new();
543         image = gtk_image_new_from_stock(GTK_STOCK_SAVE, GTK_ICON_SIZE_MENU);
544         gtk_container_add(GTK_CONTAINER(button), image);
545         gtk_widget_show(image);
546         g_signal_connect(G_OBJECT(button), "clicked",
547                          G_CALLBACK(layout_write_button_press_cb), lw);
548         gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE);
549         
550         gtk_widget_set_sensitive(button, metadata_queue_length() > 0);
551         
552         return button;
553 }
554
555
556 /*
557  *-----------------------------------------------------------------------------
558  * status bar
559  *-----------------------------------------------------------------------------
560  */
561
562 void layout_status_update_write(LayoutWindow *lw)
563 {
564         if (!layout_valid(&lw)) return;
565         if (!lw->info_write) return;
566
567         gtk_widget_set_sensitive(lw->info_write, metadata_queue_length() > 0);
568         /* FIXME: maybe show also the number of files */
569 }
570
571 void layout_status_update_write_all(void)
572 {
573         GList *work;
574
575         work = layout_window_list;
576         while (work)
577                 {
578                 LayoutWindow *lw = work->data;
579                 work = work->next;
580
581                 layout_status_update_write(lw);
582                 }
583 }
584
585
586 void layout_status_update_progress(LayoutWindow *lw, gdouble val, const gchar *text)
587 {
588         if (!layout_valid(&lw)) return;
589         if (!lw->info_progress_bar) return;
590
591         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(lw->info_progress_bar), val);
592         gtk_progress_bar_set_text(GTK_PROGRESS_BAR(lw->info_progress_bar), (text) ? text : " ");
593 }
594
595 void layout_status_update_info(LayoutWindow *lw, const gchar *text)
596 {
597         gchar *buf = NULL;
598
599         if (!layout_valid(&lw)) return;
600
601         if (!text)
602                 {
603                 guint n;
604                 gint64 n_bytes = 0;
605         
606                 n = layout_list_count(lw, &n_bytes);
607                 
608                 if (n)
609                         {
610                         guint s;
611                         gint64 s_bytes = 0;
612                         const gchar *ss;
613
614                         if (layout_image_slideshow_active(lw))
615                                 {
616                                 if (!layout_image_slideshow_paused(lw))
617                                         {
618                                         ss = _(" Slideshow");
619                                         }
620                                 else
621                                         {
622                                         ss = _(" Paused");
623                                         }
624                                 }
625                         else
626                                 {
627                                 ss = "";
628                                 }
629         
630                         s = layout_selection_count(lw, &s_bytes);
631         
632                         layout_bars_new_selection(lw, s);
633         
634                         if (s > 0)
635                                 {
636                                 gchar *b = text_from_size_abrev(n_bytes);
637                                 gchar *sb = text_from_size_abrev(s_bytes);
638                                 buf = g_strdup_printf(_("%s, %d files (%s, %d)%s"), b, n, sb, s, ss);
639                                 g_free(b);
640                                 g_free(sb);
641                                 }
642                         else if (n > 0)
643                                 {
644                                 gchar *b = text_from_size_abrev(n_bytes);
645                                 buf = g_strdup_printf(_("%s, %d files%s"), b, n, ss);
646                                 g_free(b);
647                                 }
648                         else
649                                 {
650                                 buf = g_strdup_printf(_("%d files%s"), n, ss);
651                                 }
652         
653                         text = buf;
654         
655                         image_osd_update(lw->image);
656                         }
657                 else
658                         {
659                         text = "";
660                         }
661         }
662         
663         if (lw->info_status) gtk_label_set_text(GTK_LABEL(lw->info_status), text);
664         g_free(buf);
665 }
666
667 void layout_status_update_image(LayoutWindow *lw)
668 {
669         guint64 n;
670
671         if (!layout_valid(&lw) || !lw->image) return;
672
673         n = layout_list_count(lw, NULL);
674         
675         if (!n)
676                 {
677                 gtk_label_set_text(GTK_LABEL(lw->info_zoom), "");
678                 gtk_label_set_text(GTK_LABEL(lw->info_details), "");
679                 }
680         else
681                 {
682                 gchar *text;
683                 gchar *b;
684
685                 text = image_zoom_get_as_text(lw->image);
686                 gtk_label_set_text(GTK_LABEL(lw->info_zoom), text);
687                 g_free(text);
688
689                 b = image_get_fd(lw->image) ? text_from_size(image_get_fd(lw->image)->size) : g_strdup("0");
690
691                 if (lw->image->unknown)
692                         {
693                         if (image_get_path(lw->image) && !access_file(image_get_path(lw->image), R_OK))
694                                 {
695                                 text = g_strdup_printf(_("(no read permission) %s bytes"), b);
696                                 }
697                         else
698                                 {
699                                 text = g_strdup_printf(_("( ? x ? ) %s bytes"), b);
700                                 }
701                         }
702                 else
703                         {
704                         gint width, height;
705         
706                         image_get_image_size(lw->image, &width, &height);
707                         text = g_strdup_printf(_("( %d x %d ) %s bytes"),
708                                                width, height, b);
709                         }
710
711                 g_free(b);
712                 
713                 gtk_label_set_text(GTK_LABEL(lw->info_details), text);
714                 g_free(text);
715                 }
716 }
717
718 void layout_status_update_all(LayoutWindow *lw)
719 {
720         layout_status_update_progress(lw, 0.0, NULL);
721         layout_status_update_info(lw, NULL);
722         layout_status_update_image(lw);
723         layout_status_update_write(lw);
724 }
725
726 static GtkWidget *layout_status_label(gchar *text, GtkWidget *box, gint start, gint size, gint expand)
727 {
728         GtkWidget *label;
729         GtkWidget *frame;
730
731         frame = gtk_frame_new(NULL);
732         if (size) gtk_widget_set_size_request(frame, size, -1);
733         gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_IN);
734         if (start)
735                 {
736                 gtk_box_pack_start(GTK_BOX(box), frame, expand, expand, 0);
737                 }
738         else
739                 {
740                 gtk_box_pack_end(GTK_BOX(box), frame, expand, expand, 0);
741                 }
742         gtk_widget_show(frame);
743
744         label = gtk_label_new(text ? text : "");
745         gtk_container_add(GTK_CONTAINER(frame), label);
746         gtk_widget_show(label);
747
748         return label;
749 }
750
751 static void layout_status_setup(LayoutWindow *lw, GtkWidget *box, gint small_format)
752 {
753         GtkWidget *hbox;
754
755         if (lw->info_box) return;
756
757         if (small_format)
758                 {
759                 lw->info_box = gtk_vbox_new(FALSE, 0);
760                 }
761         else
762                 {
763                 lw->info_box = gtk_hbox_new(FALSE, 0);
764                 }
765         gtk_box_pack_end(GTK_BOX(box), lw->info_box, FALSE, FALSE, 0);
766         gtk_widget_show(lw->info_box);
767
768         if (small_format)
769                 {
770                 hbox = gtk_hbox_new(FALSE, 0);
771                 gtk_box_pack_start(GTK_BOX(lw->info_box), hbox, FALSE, FALSE, 0);
772                 gtk_widget_show(hbox);
773                 }
774         else
775                 {
776                 hbox = lw->info_box;
777                 }
778         lw->info_progress_bar = gtk_progress_bar_new();
779         gtk_widget_set_size_request(lw->info_progress_bar, PROGRESS_WIDTH, -1);
780         gtk_box_pack_start(GTK_BOX(hbox), lw->info_progress_bar, FALSE, FALSE, 0);
781         gtk_widget_show(lw->info_progress_bar);
782
783         lw->info_sort = layout_sort_button(lw);
784         gtk_box_pack_start(GTK_BOX(hbox), lw->info_sort, FALSE, FALSE, 0);
785         gtk_widget_show(lw->info_sort);
786
787         lw->info_color = layout_color_button(lw);
788         gtk_widget_show(lw->info_color);
789
790         lw->info_write = layout_write_button(lw);
791         gtk_widget_show(lw->info_write);
792
793         if (small_format) gtk_box_pack_end(GTK_BOX(hbox), lw->info_color, FALSE, FALSE, 0);
794         if (small_format) gtk_box_pack_end(GTK_BOX(hbox), lw->info_write, FALSE, FALSE, 0);
795
796         lw->info_status = layout_status_label(NULL, lw->info_box, TRUE, 0, (!small_format));
797
798         if (small_format)
799                 {
800                 hbox = gtk_hbox_new(FALSE, 0);
801                 gtk_box_pack_start(GTK_BOX(lw->info_box), hbox, FALSE, FALSE, 0);
802                 gtk_widget_show(hbox);
803                 }
804         else
805                 {
806                 hbox = lw->info_box;
807                 }
808         lw->info_details = layout_status_label(NULL, hbox, TRUE, 0, TRUE);
809         if (!small_format) gtk_box_pack_start(GTK_BOX(hbox), lw->info_color, FALSE, FALSE, 0);
810         if (!small_format) gtk_box_pack_start(GTK_BOX(hbox), lw->info_write, FALSE, FALSE, 0);
811         lw->info_zoom = layout_status_label(NULL, hbox, FALSE, ZOOM_LABEL_WIDTH, FALSE);
812 }
813
814 /*
815  *-----------------------------------------------------------------------------
816  * views
817  *-----------------------------------------------------------------------------
818  */
819
820 static GtkWidget *layout_tools_new(LayoutWindow *lw)
821 {
822         lw->dir_view = layout_tool_setup(lw);
823         return lw->dir_view;
824 }
825
826 static void layout_list_status_cb(ViewFile *vf, gpointer data)
827 {
828         LayoutWindow *lw = data;
829
830         layout_status_update_info(lw, NULL);
831 }
832
833 static void layout_list_thumb_cb(ViewFile *vf, gdouble val, const gchar *text, gpointer data)
834 {
835         LayoutWindow *lw = data;
836
837         layout_status_update_progress(lw, val, text);
838 }
839
840 static GtkWidget *layout_list_new(LayoutWindow *lw)
841 {
842         lw->vf = vf_new(lw->file_view_type, NULL);
843         vf_set_layout(lw->vf, lw);
844
845         vf_set_status_func(lw->vf, layout_list_status_cb, lw);
846         vf_set_thumb_status_func(lw->vf, layout_list_thumb_cb, lw);
847
848         vf_marks_set(lw->vf, lw->marks_enabled);
849
850         switch (lw->file_view_type)
851         {
852         case FILEVIEW_ICON:
853                 break;
854         case FILEVIEW_LIST:
855                 vf_thumb_set(lw->vf, lw->thumbs_enabled);
856                 break;
857         }
858
859         return lw->vf->widget;
860 }
861
862 static void layout_list_sync_thumb(LayoutWindow *lw)
863 {
864         if (lw->vf) vf_thumb_set(lw->vf, lw->thumbs_enabled);
865 }
866
867 static void layout_list_sync_marks(LayoutWindow *lw)
868 {
869         if (lw->vf) vf_marks_set(lw->vf, lw->marks_enabled);
870 }
871
872 static void layout_list_scroll_to_subpart(LayoutWindow *lw, const gchar *needle)
873 {
874         if (!lw) return;
875 #if 0
876         if (lw->vf) vf_scroll_to_subpart(lw->vf, needle);
877 #endif
878 }
879
880 GList *layout_list(LayoutWindow *lw)
881 {
882         if (!layout_valid(&lw)) return NULL;
883
884         if (lw->vf) return vf_get_list(lw->vf);
885
886         return NULL;
887 }
888
889 guint layout_list_count(LayoutWindow *lw, gint64 *bytes)
890 {
891         if (!layout_valid(&lw)) return 0;
892
893         if (lw->vf) return vf_count(lw->vf, bytes);
894
895         return 0;
896 }
897
898 FileData *layout_list_get_fd(LayoutWindow *lw, gint index)
899 {
900         if (!layout_valid(&lw)) return NULL;
901
902         if (lw->vf) return vf_index_get_data(lw->vf, index);
903
904         return NULL;
905 }
906
907 gint layout_list_get_index(LayoutWindow *lw, FileData *fd)
908 {
909         if (!layout_valid(&lw) || !fd) return -1;
910
911         if (lw->vf) return vf_index_by_path(lw->vf, fd->path);
912
913         return -1;
914 }
915
916 void layout_list_sync_fd(LayoutWindow *lw, FileData *fd)
917 {
918         if (!layout_valid(&lw)) return;
919
920         if (lw->vf) vf_select_by_fd(lw->vf, fd);
921 }
922
923 static void layout_list_sync_sort(LayoutWindow *lw)
924 {
925         if (!layout_valid(&lw)) return;
926
927         if (lw->vf) vf_sort_set(lw->vf, lw->sort_method, lw->sort_ascend);
928 }
929
930 GList *layout_selection_list(LayoutWindow *lw)
931 {
932         if (!layout_valid(&lw)) return NULL;
933
934         if (layout_image_get_collection(lw, NULL))
935                 {
936                 FileData *fd;
937
938                 fd = layout_image_get_fd(lw);
939                 if (fd) return g_list_append(NULL, file_data_ref(fd));
940                 return NULL;
941                 }
942
943         if (lw->vf) return vf_selection_get_list(lw->vf);
944
945         return NULL;
946 }
947
948 GList *layout_selection_list_by_index(LayoutWindow *lw)
949 {
950         if (!layout_valid(&lw)) return NULL;
951
952         if (lw->vf) return vf_selection_get_list_by_index(lw->vf);
953
954         return NULL;
955 }
956
957 guint layout_selection_count(LayoutWindow *lw, gint64 *bytes)
958 {
959         if (!layout_valid(&lw)) return 0;
960
961         if (lw->vf) return vf_selection_count(lw->vf, bytes);
962
963         return 0;
964 }
965
966 void layout_select_all(LayoutWindow *lw)
967 {
968         if (!layout_valid(&lw)) return;
969
970         if (lw->vf) vf_select_all(lw->vf);
971 }
972
973 void layout_select_none(LayoutWindow *lw)
974 {
975         if (!layout_valid(&lw)) return;
976
977         if (lw->vf) vf_select_none(lw->vf);
978 }
979
980 void layout_select_invert(LayoutWindow *lw)
981 {
982         if (!layout_valid(&lw)) return;
983
984         if (lw->vf) vf_select_invert(lw->vf);
985 }
986
987 void layout_mark_to_selection(LayoutWindow *lw, gint mark, MarkToSelectionMode mode)
988 {
989         if (!layout_valid(&lw)) return;
990
991         if (lw->vf) vf_mark_to_selection(lw->vf, mark, mode);
992 }
993
994 void layout_selection_to_mark(LayoutWindow *lw, gint mark, SelectionToMarkMode mode)
995 {
996         if (!layout_valid(&lw)) return;
997
998         if (lw->vf) vf_selection_to_mark(lw->vf, mark, mode);
999
1000         layout_status_update_info(lw, NULL); /* osd in fullscreen mode */
1001 }
1002
1003 /*
1004  *-----------------------------------------------------------------------------
1005  * access
1006  *-----------------------------------------------------------------------------
1007  */
1008
1009 const gchar *layout_get_path(LayoutWindow *lw)
1010 {
1011         if (!layout_valid(&lw)) return NULL;
1012         return lw->dir_fd ? lw->dir_fd->path : NULL;
1013 }
1014
1015 static void layout_sync_path(LayoutWindow *lw)
1016 {
1017         if (!lw->dir_fd) return;
1018
1019         if (lw->path_entry) gtk_entry_set_text(GTK_ENTRY(lw->path_entry), lw->dir_fd->path);
1020         if (lw->vd) vd_set_fd(lw->vd, lw->dir_fd);
1021
1022         if (lw->vf) vf_set_fd(lw->vf, lw->dir_fd);
1023 }
1024
1025 gint layout_set_path(LayoutWindow *lw, const gchar *path)
1026 {
1027         FileData *fd;
1028         if (!path) return FALSE;
1029         fd = file_data_new_simple(path);
1030         gint ret = layout_set_fd(lw, fd);
1031         file_data_unref(fd);
1032         return ret;
1033 }
1034
1035
1036 gint layout_set_fd(LayoutWindow *lw, FileData *fd)
1037 {
1038         gint have_file = FALSE;
1039
1040         if (!layout_valid(&lw)) return FALSE;
1041
1042         if (!fd || !isname(fd->path)) return FALSE;
1043         if (lw->dir_fd && fd == lw->dir_fd)
1044                 {
1045                 return TRUE;
1046                 }
1047
1048         if (isdir(fd->path))
1049                 {
1050                 if (lw->dir_fd)
1051                         {
1052                         file_data_unregister_real_time_monitor(lw->dir_fd);
1053                         file_data_unref(lw->dir_fd);
1054                         }
1055                 lw->dir_fd = file_data_ref(fd);
1056                 file_data_register_real_time_monitor(fd);
1057                 }
1058         else
1059                 {
1060                 gchar *base;
1061
1062                 base = remove_level_from_path(fd->path);
1063                 if (lw->dir_fd && strcmp(lw->dir_fd->path, base) == 0)
1064                         {
1065                         g_free(base);
1066                         }
1067                 else if (isdir(base))
1068                         {
1069                         if (lw->dir_fd)
1070                                 {
1071                                 file_data_unregister_real_time_monitor(lw->dir_fd);
1072                                 file_data_unref(lw->dir_fd);
1073                                 }
1074                         lw->dir_fd = file_data_new_simple(base);
1075                         file_data_register_real_time_monitor(lw->dir_fd);
1076                         g_free(base);
1077                         }
1078                 else
1079                         {
1080                         g_free(base);
1081                         return FALSE;
1082                         }
1083                 if (isfile(fd->path)) have_file = TRUE;
1084                 }
1085
1086         if (lw->path_entry) tab_completion_append_to_history(lw->path_entry, lw->dir_fd->path);
1087         layout_sync_path(lw);
1088
1089         if (have_file)
1090                 {
1091                 gint row;
1092
1093                 row = layout_list_get_index(lw, fd);
1094                 if (row >= 0)
1095                         {
1096                         layout_image_set_index(lw, row);
1097                         }
1098                 else
1099                         {
1100                         layout_image_set_fd(lw, fd);
1101                         }
1102                 }
1103         else if (!options->lazy_image_sync)
1104                 {
1105                 layout_image_set_index(lw, 0);
1106                 }
1107
1108         if (options->metadata.confirm_on_dir_change)
1109                 metadata_write_queue_confirm(NULL, NULL);
1110
1111         return TRUE;
1112 }
1113
1114 static void layout_refresh_lists(LayoutWindow *lw)
1115 {
1116         if (lw->vd) vd_refresh(lw->vd);
1117
1118         if (lw->vf) vf_refresh(lw->vf);
1119 }
1120
1121 void layout_refresh(LayoutWindow *lw)
1122 {
1123         if (!layout_valid(&lw)) return;
1124
1125         DEBUG_1("layout refresh");
1126
1127         layout_refresh_lists(lw);
1128
1129         if (lw->image) layout_image_refresh(lw);
1130 }
1131
1132 void layout_thumb_set(LayoutWindow *lw, gint enable)
1133 {
1134         if (!layout_valid(&lw)) return;
1135
1136         if (lw->thumbs_enabled == enable) return;
1137
1138         lw->thumbs_enabled = enable;
1139
1140         layout_util_sync_thumb(lw);
1141         layout_list_sync_thumb(lw);
1142 }
1143
1144 void layout_marks_set(LayoutWindow *lw, gint enable)
1145 {
1146         if (!layout_valid(&lw)) return;
1147
1148         if (lw->marks_enabled == enable) return;
1149
1150         lw->marks_enabled = enable;
1151
1152 //      layout_util_sync_marks(lw);
1153         layout_list_sync_marks(lw);
1154 }
1155
1156 gint layout_thumb_get(LayoutWindow *lw)
1157 {
1158         if (!layout_valid(&lw)) return FALSE;
1159
1160         return lw->thumbs_enabled;
1161 }
1162
1163 gint layout_marks_get(LayoutWindow *lw)
1164 {
1165         if (!layout_valid(&lw)) return FALSE;
1166
1167         return lw->marks_enabled;
1168 }
1169
1170 void layout_sort_set(LayoutWindow *lw, SortType type, gint ascend)
1171 {
1172         if (!layout_valid(&lw)) return;
1173         if (lw->sort_method == type && lw->sort_ascend == ascend) return;
1174
1175         lw->sort_method = type;
1176         lw->sort_ascend = ascend;
1177
1178         if (lw->info_sort) gtk_label_set_text(GTK_LABEL(GTK_BIN(lw->info_sort)->child),
1179                                               sort_type_get_text(type));
1180         layout_list_sync_sort(lw);
1181 }
1182
1183 gint layout_sort_get(LayoutWindow *lw, SortType *type, gint *ascend)
1184 {
1185         if (!layout_valid(&lw)) return FALSE;
1186
1187         if (type) *type = lw->sort_method;
1188         if (ascend) *ascend = lw->sort_ascend;
1189
1190         return TRUE;
1191 }
1192
1193 gint layout_geometry_get(LayoutWindow *lw, gint *x, gint *y, gint *w, gint *h)
1194 {
1195         if (!layout_valid(&lw)) return FALSE;
1196
1197         gdk_window_get_root_origin(lw->window->window, x, y);
1198         gdk_drawable_get_size(lw->window->window, w, h);
1199
1200         return TRUE;
1201 }
1202
1203 gint layout_geometry_get_dividers(LayoutWindow *lw, gint *h, gint *v)
1204 {
1205         if (!layout_valid(&lw)) return FALSE;
1206
1207         if (lw->h_pane && GTK_PANED(lw->h_pane)->child1->allocation.x >= 0)
1208                 {
1209                 *h = GTK_PANED(lw->h_pane)->child1->allocation.width;
1210                 }
1211         else if (h != &lw->div_h)
1212                 {
1213                 *h = lw->div_h;
1214                 }
1215
1216         if (lw->v_pane && GTK_PANED(lw->v_pane)->child1->allocation.x >= 0)
1217                 {
1218                 *v = GTK_PANED(lw->v_pane)->child1->allocation.height;
1219                 }
1220         else if (v != &lw->div_v)
1221                 {
1222                 *v = lw->div_v;
1223                 }
1224
1225         return TRUE;
1226 }
1227
1228 void layout_views_set(LayoutWindow *lw, DirViewType dir_view_type, FileViewType file_view_type)
1229 {
1230         if (!layout_valid(&lw)) return;
1231
1232         if (lw->dir_view_type == dir_view_type && lw->file_view_type == file_view_type) return;
1233
1234         lw->dir_view_type = dir_view_type;
1235         lw->file_view_type = file_view_type;
1236
1237         layout_style_set(lw, -1, NULL);
1238 }
1239
1240 gint layout_views_get(LayoutWindow *lw, DirViewType *dir_view_type, FileViewType *file_view_type)
1241 {
1242         if (!layout_valid(&lw)) return FALSE;
1243
1244         *dir_view_type = lw->dir_view_type;
1245         *file_view_type = lw->file_view_type;
1246
1247         return TRUE;
1248 }
1249
1250 /*
1251  *-----------------------------------------------------------------------------
1252  * location utils
1253  *-----------------------------------------------------------------------------
1254  */
1255
1256 static gint layout_location_single(LayoutLocation l)
1257 {
1258         return (l == LAYOUT_LEFT ||
1259                 l == LAYOUT_RIGHT ||
1260                 l == LAYOUT_TOP ||
1261                 l == LAYOUT_BOTTOM);
1262 }
1263
1264 static gint layout_location_vertical(LayoutLocation l)
1265 {
1266         return (l & LAYOUT_TOP ||
1267                 l & LAYOUT_BOTTOM);
1268 }
1269
1270 static gint layout_location_first(LayoutLocation l)
1271 {
1272         return (l & LAYOUT_TOP ||
1273                 l & LAYOUT_LEFT);
1274 }
1275
1276 static LayoutLocation layout_grid_compass(LayoutWindow *lw)
1277 {
1278         if (layout_location_single(lw->dir_location)) return lw->dir_location;
1279         if (layout_location_single(lw->file_location)) return lw->file_location;
1280         return lw->image_location;
1281 }
1282
1283 static void layout_location_compute(LayoutLocation l1, LayoutLocation l2,
1284                                     GtkWidget *s1, GtkWidget *s2,
1285                                     GtkWidget **d1, GtkWidget **d2)
1286 {
1287         LayoutLocation l;
1288
1289         l = l1 & l2;    /* get common compass direction */
1290         l = l1 - l;     /* remove it */
1291
1292         if (layout_location_first(l))
1293                 {
1294                 *d1 = s1;
1295                 *d2 = s2;
1296                 }
1297         else
1298                 {
1299                 *d1 = s2;
1300                 *d2 = s1;
1301                 }
1302 }
1303
1304 /*
1305  *-----------------------------------------------------------------------------
1306  * tools window (for floating/hidden)
1307  *-----------------------------------------------------------------------------
1308  */
1309
1310 gint layout_geometry_get_tools(LayoutWindow *lw, gint *x, gint *y, gint *w, gint *h, gint *divider_pos)
1311 {
1312         if (!layout_valid(&lw)) return FALSE;
1313
1314         if (!lw->tools || !GTK_WIDGET_VISIBLE(lw->tools))
1315                 {
1316                 /* use the stored values (sort of breaks success return value) */
1317
1318                 *divider_pos = lw->div_float;
1319
1320                 return FALSE;
1321                 }
1322
1323         gdk_window_get_root_origin(lw->tools->window, x, y);
1324         gdk_drawable_get_size(lw->tools->window, w, h);
1325
1326         if (GTK_IS_VPANED(lw->tools_pane))
1327                 {
1328                 *divider_pos = GTK_PANED(lw->tools_pane)->child1->allocation.height;
1329                 }
1330         else
1331                 {
1332                 *divider_pos = GTK_PANED(lw->tools_pane)->child1->allocation.width;
1333                 }
1334
1335         return TRUE;
1336 }
1337
1338 static void layout_tools_geometry_sync(LayoutWindow *lw)
1339 {
1340         layout_geometry_get_tools(lw, &options->layout.float_window.x, &options->layout.float_window.x,
1341                                   &options->layout.float_window.w, &options->layout.float_window.h, &lw->div_float);
1342 }
1343
1344 static void layout_tools_hide(LayoutWindow *lw, gint hide)
1345 {
1346         if (!lw->tools) return;
1347
1348         if (hide)
1349                 {
1350                 if (GTK_WIDGET_VISIBLE(lw->tools))
1351                         {
1352                         layout_tools_geometry_sync(lw);
1353                         gtk_widget_hide(lw->tools);
1354                         }
1355                 }
1356         else
1357                 {
1358                 if (!GTK_WIDGET_VISIBLE(lw->tools))
1359                         {
1360                         gtk_widget_show(lw->tools);
1361                         if (lw->vf) vf_refresh(lw->vf);
1362                         }
1363                 }
1364
1365         lw->tools_hidden = hide;
1366 }
1367
1368 static gint layout_tools_delete_cb(GtkWidget *widget, GdkEventAny *event, gpointer data)
1369 {
1370         LayoutWindow *lw = data;
1371
1372         layout_tools_float_toggle(lw);
1373
1374         return TRUE;
1375 }
1376
1377 static void layout_tools_setup(LayoutWindow *lw, GtkWidget *tools, GtkWidget *files)
1378 {
1379         GtkWidget *vbox;
1380         GtkWidget *w1, *w2;
1381         gint vertical;
1382         gint new_window = FALSE;
1383
1384         vertical = (layout_location_single(lw->image_location) && !layout_location_vertical(lw->image_location)) ||
1385                    (!layout_location_single(lw->image_location) && layout_location_vertical(layout_grid_compass(lw)));
1386 #if 0
1387         layout_location_compute(lw->dir_location, lw->file_location,
1388                                 tools, files, &w1, &w2);
1389 #endif
1390         /* for now, tools/dir are always first in order */
1391         w1 = tools;
1392         w2 = files;
1393
1394         if (!lw->tools)
1395                 {
1396                 GdkGeometry geometry;
1397                 GdkWindowHints hints;
1398
1399                 lw->tools = window_new(GTK_WINDOW_TOPLEVEL, "tools", PIXBUF_INLINE_ICON_TOOLS, NULL, _("Tools"));
1400                 g_signal_connect(G_OBJECT(lw->tools), "delete_event",
1401                                  G_CALLBACK(layout_tools_delete_cb), lw);
1402                 layout_keyboard_init(lw, lw->tools);
1403
1404                 if (options->layout.save_window_positions)
1405                         {
1406                         hints = GDK_HINT_USER_POS;
1407                         }
1408                 else
1409                         {
1410                         hints = 0;
1411                         }
1412
1413                 geometry.min_width = DEFAULT_MINIMAL_WINDOW_SIZE;
1414                 geometry.min_height = DEFAULT_MINIMAL_WINDOW_SIZE;
1415                 geometry.base_width = TOOLWINDOW_DEF_WIDTH;
1416                 geometry.base_height = TOOLWINDOW_DEF_HEIGHT;
1417                 gtk_window_set_geometry_hints(GTK_WINDOW(lw->tools), NULL, &geometry,
1418                                               GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE | hints);
1419
1420
1421                 gtk_window_set_resizable(GTK_WINDOW(lw->tools), TRUE);
1422                 gtk_container_set_border_width(GTK_CONTAINER(lw->tools), 0);
1423
1424                 new_window = TRUE;
1425                 }
1426         else
1427                 {
1428                 layout_tools_geometry_sync(lw);
1429                 /* dump the contents */
1430                 gtk_widget_destroy(GTK_BIN(lw->tools)->child);
1431                 }
1432
1433         layout_actions_add_window(lw, lw->tools);
1434
1435         vbox = gtk_vbox_new(FALSE, 0);
1436         gtk_container_add(GTK_CONTAINER(lw->tools), vbox);
1437         gtk_widget_show(vbox);
1438
1439         layout_status_setup(lw, vbox, TRUE);
1440
1441         if (vertical)
1442                 {
1443                 lw->tools_pane = gtk_vpaned_new();
1444                 }
1445         else
1446                 {
1447                 lw->tools_pane = gtk_hpaned_new();
1448                 }
1449         gtk_box_pack_start(GTK_BOX(vbox), lw->tools_pane, TRUE, TRUE, 0);
1450         gtk_widget_show(lw->tools_pane);
1451
1452         gtk_paned_pack1(GTK_PANED(lw->tools_pane), w1, FALSE, TRUE);
1453         gtk_paned_pack2(GTK_PANED(lw->tools_pane), w2, TRUE, TRUE);
1454
1455         gtk_widget_show(tools);
1456         gtk_widget_show(files);
1457
1458         if (new_window)
1459                 {
1460                 if (options->layout.save_window_positions)
1461                         {
1462                         gtk_window_set_default_size(GTK_WINDOW(lw->tools), options->layout.float_window.w, options->layout.float_window.h);
1463                         gtk_window_move(GTK_WINDOW(lw->tools), options->layout.float_window.x, options->layout.float_window.y);
1464                         }
1465                 else
1466                         {
1467                         if (vertical)
1468                                 {
1469                                 gtk_window_set_default_size(GTK_WINDOW(lw->tools),
1470                                                             TOOLWINDOW_DEF_WIDTH, TOOLWINDOW_DEF_HEIGHT);
1471                                 }
1472                         else
1473                                 {
1474                                 gtk_window_set_default_size(GTK_WINDOW(lw->tools),
1475                                                             TOOLWINDOW_DEF_HEIGHT, TOOLWINDOW_DEF_WIDTH);
1476                                 }
1477                         }
1478                 }
1479
1480         if (!options->layout.save_window_positions)
1481                 {
1482                 if (vertical)
1483                         {
1484                         lw->div_float = MAIN_WINDOW_DIV_VPOS;
1485                         }
1486                 else
1487                         {
1488                         lw->div_float = MAIN_WINDOW_DIV_HPOS;
1489                         }
1490                 }
1491
1492         gtk_paned_set_position(GTK_PANED(lw->tools_pane), lw->div_float);
1493 }
1494
1495 /*
1496  *-----------------------------------------------------------------------------
1497  * glue (layout arrangement)
1498  *-----------------------------------------------------------------------------
1499  */
1500
1501 static void layout_grid_compute(LayoutWindow *lw,
1502                                 GtkWidget *image, GtkWidget *tools, GtkWidget *files,
1503                                 GtkWidget **w1, GtkWidget **w2, GtkWidget **w3)
1504 {
1505         /* heh, this was fun */
1506
1507         if (layout_location_single(lw->dir_location))
1508                 {
1509                 if (layout_location_first(lw->dir_location))
1510                         {
1511                         *w1 = tools;
1512                         layout_location_compute(lw->file_location, lw->image_location, files, image, w2, w3);
1513                         }
1514                 else
1515                         {
1516                         *w3 = tools;
1517                         layout_location_compute(lw->file_location, lw->image_location, files, image, w1, w2);
1518                         }
1519                 }
1520         else if (layout_location_single(lw->file_location))
1521                 {
1522                 if (layout_location_first(lw->file_location))
1523                         {
1524                         *w1 = files;
1525                         layout_location_compute(lw->dir_location, lw->image_location, tools, image, w2, w3);
1526                         }
1527                 else
1528                         {
1529                         *w3 = files;
1530                         layout_location_compute(lw->dir_location, lw->image_location, tools, image, w1, w2);
1531                         }
1532                 }
1533         else
1534                 {
1535                 /* image */
1536                 if (layout_location_first(lw->image_location))
1537                         {
1538                         *w1 = image;
1539                         layout_location_compute(lw->file_location, lw->dir_location, files, tools, w2, w3);
1540                         }
1541                 else
1542                         {
1543                         *w3 = image;
1544                         layout_location_compute(lw->file_location, lw->dir_location, files, tools, w1, w2);
1545                         }
1546                 }
1547 }
1548
1549 void layout_split_change(LayoutWindow *lw, ImageSplitMode mode)
1550 {
1551         GtkWidget *image;
1552         gint i;
1553
1554         for (i = 0; i < MAX_SPLIT_IMAGES; i++)
1555                 {
1556                 if (lw->split_images[i])
1557                         {
1558                         gtk_widget_hide(lw->split_images[i]->widget);
1559                         if (lw->split_images[i]->widget->parent != lw->utility_box)
1560                                 gtk_container_remove(GTK_CONTAINER(lw->split_images[i]->widget->parent), lw->split_images[i]->widget);
1561                         }
1562                 }
1563         gtk_container_remove(GTK_CONTAINER(lw->utility_box), lw->split_image_widget);
1564
1565         image = layout_image_setup_split(lw, mode);
1566
1567         gtk_box_pack_start(GTK_BOX(lw->utility_box), image, TRUE, TRUE, 0);
1568         gtk_box_reorder_child(GTK_BOX(lw->utility_box), image, 0);
1569         gtk_widget_show(image);
1570 }
1571
1572 static void layout_grid_setup(LayoutWindow *lw)
1573 {
1574         gint priority_location;
1575         GtkWidget *h;
1576         GtkWidget *v;
1577         GtkWidget *w1, *w2, *w3;
1578
1579         GtkWidget *image;
1580         GtkWidget *tools;
1581         GtkWidget *files;
1582
1583         layout_actions_setup(lw);
1584         layout_actions_add_window(lw, lw->window);
1585
1586         lw->group_box = gtk_vbox_new(FALSE, 0);
1587         gtk_box_pack_start(GTK_BOX(lw->main_box), lw->group_box, TRUE, TRUE, 0);
1588         gtk_widget_show(lw->group_box);
1589
1590         priority_location = layout_grid_compass(lw);
1591
1592         image = layout_image_setup_split(lw, lw->split_mode);
1593
1594         tools = layout_tools_new(lw);
1595         files = layout_list_new(lw);
1596
1597         image = layout_bars_prepare(lw, image);
1598
1599         if (lw->tools_float || lw->tools_hidden)
1600                 {
1601                 gtk_box_pack_start(GTK_BOX(lw->group_box), image, TRUE, TRUE, 0);
1602                 gtk_widget_show(image);
1603
1604                 layout_tools_setup(lw, tools, files);
1605
1606                 gtk_widget_grab_focus(lw->image->widget);
1607
1608                 return;
1609                 }
1610         else if (lw->tools)
1611                 {
1612                 layout_tools_geometry_sync(lw);
1613                 gtk_widget_destroy(lw->tools);
1614                 lw->tools = NULL;
1615                 lw->tools_pane = NULL;
1616                 }
1617
1618         layout_status_setup(lw, lw->group_box, FALSE);
1619
1620         layout_grid_compute(lw, image, tools, files, &w1, &w2, &w3);
1621
1622         v = lw->v_pane = gtk_vpaned_new();
1623
1624         h = lw->h_pane = gtk_hpaned_new();
1625
1626         if (!layout_location_vertical(priority_location))
1627                 {
1628                 GtkWidget *tmp;
1629
1630                 tmp = v;
1631                 v = h;
1632                 h = tmp;
1633                 }
1634
1635         gtk_box_pack_start(GTK_BOX(lw->group_box), v, TRUE, TRUE, 0);
1636
1637         if (!layout_location_first(priority_location))
1638                 {
1639                 gtk_paned_pack1(GTK_PANED(v), h, FALSE, TRUE);
1640                 gtk_paned_pack2(GTK_PANED(v), w3, TRUE, TRUE);
1641
1642                 gtk_paned_pack1(GTK_PANED(h), w1, FALSE, TRUE);
1643                 gtk_paned_pack2(GTK_PANED(h), w2, TRUE, TRUE);
1644                 }
1645         else
1646                 {
1647                 gtk_paned_pack1(GTK_PANED(v), w1, FALSE, TRUE);
1648                 gtk_paned_pack2(GTK_PANED(v), h, TRUE, TRUE);
1649
1650                 gtk_paned_pack1(GTK_PANED(h), w2, FALSE, TRUE);
1651                 gtk_paned_pack2(GTK_PANED(h), w3, TRUE, TRUE);
1652                 }
1653
1654         gtk_widget_show(image);
1655         gtk_widget_show(tools);
1656         gtk_widget_show(files);
1657
1658         gtk_widget_show(v);
1659         gtk_widget_show(h);
1660
1661         /* fix to have image pane visible when it is left and priority widget */
1662         if (lw->div_h == -1 &&
1663             w1 == image &&
1664             !layout_location_vertical(priority_location) &&
1665             layout_location_first(priority_location))
1666                 {
1667                 gtk_widget_set_size_request(image, 200, -1);
1668                 }
1669
1670         gtk_paned_set_position(GTK_PANED(lw->h_pane), lw->div_h);
1671         gtk_paned_set_position(GTK_PANED(lw->v_pane), lw->div_v);
1672
1673         gtk_widget_grab_focus(lw->image->widget);
1674 }
1675
1676 void layout_style_set(LayoutWindow *lw, gint style, const gchar *order)
1677 {
1678         FileData *dir_fd;
1679         gint i;
1680
1681         if (!layout_valid(&lw)) return;
1682
1683         if (style != -1)
1684                 {
1685                 LayoutLocation d, f, i;
1686
1687                 layout_config_parse(style, order, &d,  &f, &i);
1688
1689                 if (lw->dir_location == d &&
1690                     lw->file_location == f &&
1691                     lw->image_location == i) return;
1692
1693                 lw->dir_location = d;
1694                 lw->file_location = f;
1695                 lw->image_location = i;
1696                 }
1697
1698         /* remember state */
1699
1700         layout_image_slideshow_stop(lw);
1701         layout_image_full_screen_stop(lw);
1702
1703         dir_fd = lw->dir_fd;
1704         if (dir_fd) file_data_unregister_real_time_monitor(dir_fd);
1705         lw->dir_fd = NULL;
1706         lw->image = NULL;
1707         lw->utility_box = NULL;
1708
1709         layout_geometry_get_dividers(lw, &lw->div_h, &lw->div_v);
1710
1711         /* clear it all */
1712
1713         for (i = 0; i < MAX_SPLIT_IMAGES; i++)
1714                 {
1715                 if (lw->split_images[i])
1716                         {
1717                         gtk_widget_hide(lw->split_images[i]->widget);
1718                         gtk_container_remove(GTK_CONTAINER(lw->split_images[i]->widget->parent), lw->split_images[i]->widget);
1719                         }
1720                 }
1721
1722         lw->h_pane = NULL;
1723         lw->v_pane = NULL;
1724
1725         lw->toolbar = NULL;
1726         lw->thumb_button = NULL;
1727         lw->path_entry = NULL;
1728         lw->dir_view = NULL;
1729         lw->vd = NULL;
1730
1731         lw->file_view = NULL;
1732         lw->vf = NULL;
1733         lw->vf = NULL;
1734
1735         lw->info_box = NULL;
1736         lw->info_progress_bar = NULL;
1737         lw->info_sort = NULL;
1738         lw->info_color = NULL;
1739         lw->info_status = NULL;
1740         lw->info_details = NULL;
1741         lw->info_zoom = NULL;
1742
1743         if (lw->ui_manager) g_object_unref(lw->ui_manager);
1744         lw->ui_manager = NULL;
1745         lw->action_group = NULL;
1746         lw->action_group_external = NULL;
1747
1748         gtk_container_remove(GTK_CONTAINER(lw->main_box), lw->group_box);
1749         lw->group_box = NULL;
1750
1751         /* re-fill */
1752
1753         layout_grid_setup(lw);
1754         layout_tools_hide(lw, lw->tools_hidden);
1755
1756         layout_list_sync_sort(lw);
1757         layout_util_sync(lw);
1758         layout_status_update_all(lw);
1759
1760         /* sync */
1761
1762         if (image_get_fd(lw->image))
1763                 {
1764                 layout_set_fd(lw, image_get_fd(lw->image));
1765                 }
1766         else
1767                 {
1768                 layout_set_fd(lw, dir_fd);
1769                 }
1770         image_top_window_set_sync(lw->image, (lw->tools_float || lw->tools_hidden));
1771
1772         /* clean up */
1773
1774         file_data_unref(dir_fd);
1775 }
1776
1777 void layout_styles_update(void)
1778 {
1779         GList *work;
1780
1781         work = layout_window_list;
1782         while (work)
1783                 {
1784                 LayoutWindow *lw = work->data;
1785                 work = work->next;
1786
1787                 layout_style_set(lw, options->layout.style, options->layout.order);
1788                 }
1789 }
1790
1791 void layout_colors_update(void)
1792 {
1793         GList *work;
1794
1795         work = layout_window_list;
1796         while (work)
1797                 {
1798                 LayoutWindow *lw = work->data;
1799                 work = work->next;
1800
1801                 if (!lw->image) continue;
1802                 image_background_set_color(lw->image, options->image.use_custom_border_color ? &options->image.border_color : NULL);
1803                 }
1804 }
1805
1806 void layout_tools_float_toggle(LayoutWindow *lw)
1807 {
1808         gint popped;
1809
1810         if (!lw) return;
1811
1812         if (!lw->tools_hidden)
1813                 {
1814                 popped = !lw->tools_float;
1815                 }
1816         else
1817                 {
1818                 popped = TRUE;
1819                 }
1820
1821         if (lw->tools_float == popped)
1822                 {
1823                 if (popped && lw->tools_hidden)
1824                         {
1825                         layout_tools_float_set(lw, popped, FALSE);
1826                         }
1827                 }
1828         else
1829                 {
1830                 if (lw->tools_float)
1831                         {
1832                         layout_tools_float_set(lw, FALSE, FALSE);
1833                         }
1834                 else
1835                         {
1836                         layout_tools_float_set(lw, TRUE, FALSE);
1837                         }
1838                 }
1839 }
1840
1841 void layout_tools_hide_toggle(LayoutWindow *lw)
1842 {
1843         if (!lw) return;
1844
1845         layout_tools_float_set(lw, lw->tools_float, !lw->tools_hidden);
1846 }
1847
1848 void layout_tools_float_set(LayoutWindow *lw, gint popped, gint hidden)
1849 {
1850         if (!layout_valid(&lw)) return;
1851
1852         if (lw->tools_float == popped && lw->tools_hidden == hidden) return;
1853
1854         if (lw->tools_float == popped && lw->tools_float && lw->tools)
1855                 {
1856                 layout_tools_hide(lw, hidden);
1857                 return;
1858                 }
1859
1860         lw->tools_float = popped;
1861         lw->tools_hidden = hidden;
1862
1863         layout_style_set(lw, -1, NULL);
1864 }
1865
1866 gint layout_tools_float_get(LayoutWindow *lw, gint *popped, gint *hidden)
1867 {
1868         if (!layout_valid(&lw)) return FALSE;
1869
1870         *popped = lw->tools_float;
1871         *hidden = lw->tools_hidden;
1872
1873         return TRUE;
1874 }
1875
1876 void layout_toolbar_toggle(LayoutWindow *lw)
1877 {
1878         if (!layout_valid(&lw)) return;
1879         if (!lw->toolbar) return;
1880
1881         lw->toolbar_hidden = !lw->toolbar_hidden;
1882
1883         if (lw->toolbar_hidden)
1884                 {
1885                 if (GTK_WIDGET_VISIBLE(lw->toolbar)) gtk_widget_hide(lw->toolbar);
1886                 }
1887         else
1888                 {
1889                 if (!GTK_WIDGET_VISIBLE(lw->toolbar)) gtk_widget_show(lw->toolbar);
1890                 }
1891 }
1892
1893 gint layout_toolbar_hidden(LayoutWindow *lw)
1894 {
1895         if (!layout_valid(&lw)) return TRUE;
1896
1897         return lw->toolbar_hidden;
1898 }
1899
1900 /*
1901  *-----------------------------------------------------------------------------
1902  * base
1903  *-----------------------------------------------------------------------------
1904  */
1905
1906 void layout_close(LayoutWindow *lw)
1907 {
1908         if (layout_window_list && layout_window_list->next)
1909                 {
1910                 layout_free(lw);
1911                 }
1912         else
1913                 {
1914                 exit_program();
1915                 }
1916 }
1917
1918 void layout_free(LayoutWindow *lw)
1919 {
1920         if (!lw) return;
1921
1922         layout_window_list = g_list_remove(layout_window_list, lw);
1923
1924         
1925         layout_bars_close(lw);
1926
1927         gtk_widget_destroy(lw->window);
1928         
1929         if (lw->split_image_sizegroup) g_object_unref(lw->split_image_sizegroup);
1930
1931         file_data_unregister_notify_func(layout_image_notify_cb, lw);
1932
1933         if (lw->dir_fd)
1934                 {
1935                 file_data_unregister_real_time_monitor(lw->dir_fd);
1936                 file_data_unref(lw->dir_fd);
1937                 }
1938
1939         g_free(lw);
1940 }
1941
1942 static gint layout_delete_cb(GtkWidget *widget, GdkEventAny *event, gpointer data)
1943 {
1944         LayoutWindow *lw = data;
1945
1946         layout_close(lw);
1947         return TRUE;
1948 }
1949
1950 LayoutWindow *layout_new(FileData *dir_fd, gint popped, gint hidden)
1951 {
1952         return layout_new_with_geometry(dir_fd, popped, hidden, NULL);
1953 }
1954
1955 LayoutWindow *layout_new_with_geometry(FileData *dir_fd, gint popped, gint hidden,
1956                                        const gchar *geometry)
1957 {
1958         LayoutWindow *lw;
1959         GdkGeometry hint;
1960         GdkWindowHints hint_mask;
1961
1962         lw = g_new0(LayoutWindow, 1);
1963
1964         lw->thumbs_enabled = options->layout.show_thumbnails;
1965         lw->marks_enabled = options->layout.show_marks;
1966         lw->sort_method = SORT_NAME;
1967         lw->sort_ascend = TRUE;
1968
1969         lw->tools_float = popped;
1970         lw->tools_hidden = hidden;
1971
1972         lw->toolbar_hidden = options->layout.toolbar_hidden;
1973
1974         lw->utility_box = NULL;
1975         lw->bar_sort = NULL;
1976         lw->bar_sort_enabled = options->panels.sort.enabled;
1977
1978         lw->bar_exif = NULL;
1979         lw->bar_exif_enabled = options->panels.exif.enabled;
1980         lw->bar_exif_advanced = FALSE;
1981         
1982         lw->bar_info = NULL;
1983         lw->bar_info_enabled = options->panels.info.enabled;
1984
1985         /* default layout */
1986
1987         layout_config_parse(options->layout.style, options->layout.order,
1988                             &lw->dir_location,  &lw->file_location, &lw->image_location);
1989         lw->dir_view_type = options->layout.dir_view_type;
1990     if (lw->dir_view_type >= VIEW_DIR_TYPES_COUNT) lw->dir_view_type = 0;
1991         lw->file_view_type = options->layout.file_view_type;
1992     if (lw->file_view_type >= VIEW_FILE_TYPES_COUNT) lw->file_view_type = 0;
1993
1994         /* divider positions */
1995
1996         if (options->layout.save_window_positions)
1997                 {
1998                 lw->div_h = options->layout.main_window.hdivider_pos;
1999                 lw->div_v = options->layout.main_window.vdivider_pos;
2000                 lw->div_float = options->layout.float_window.vdivider_pos;
2001                 lw->bar_exif_width = options->panels.exif.width;
2002                 lw->bar_info_width = options->panels.info.width;
2003                 }
2004         else
2005                 {
2006                 lw->div_h = MAIN_WINDOW_DIV_HPOS;
2007                 lw->div_v = MAIN_WINDOW_DIV_VPOS;
2008                 lw->div_float = MAIN_WINDOW_DIV_VPOS;
2009                 lw->bar_exif_width = PANEL_DEFAULT_WIDTH;
2010                 lw->bar_info_width = PANEL_DEFAULT_WIDTH;
2011                 }
2012
2013         /* window */
2014
2015         lw->window = window_new(GTK_WINDOW_TOPLEVEL, GQ_APPNAME_LC, NULL, NULL, NULL);
2016         gtk_window_set_resizable(GTK_WINDOW(lw->window), TRUE);
2017         gtk_container_set_border_width(GTK_CONTAINER(lw->window), 0);
2018
2019         if (options->layout.save_window_positions)
2020                 {
2021                 hint_mask = GDK_HINT_USER_POS;
2022                 }
2023         else
2024                 {
2025                 hint_mask = 0;
2026                 }
2027
2028         hint.min_width = 32;
2029         hint.min_height = 32;
2030         hint.base_width = 0;
2031         hint.base_height = 0;
2032         gtk_window_set_geometry_hints(GTK_WINDOW(lw->window), NULL, &hint,
2033                                       GDK_HINT_MIN_SIZE | GDK_HINT_BASE_SIZE | hint_mask);
2034
2035         if (options->layout.save_window_positions)
2036                 {
2037                 gtk_window_set_default_size(GTK_WINDOW(lw->window), options->layout.main_window.w, options->layout.main_window.h);
2038                 if (!layout_window_list)
2039                         {
2040                         gtk_window_move(GTK_WINDOW(lw->window), options->layout.main_window.x, options->layout.main_window.y);
2041                         if (options->layout.main_window.maximized) gtk_window_maximize(GTK_WINDOW(lw->window));
2042                         }
2043                 }
2044         else
2045                 {
2046                 gtk_window_set_default_size(GTK_WINDOW(lw->window), MAINWINDOW_DEF_WIDTH, MAINWINDOW_DEF_HEIGHT);
2047                 }
2048
2049         g_signal_connect(G_OBJECT(lw->window), "delete_event",
2050                          G_CALLBACK(layout_delete_cb), lw);
2051
2052         layout_keyboard_init(lw, lw->window);
2053
2054 #ifdef HAVE_LIRC
2055         layout_image_lirc_init(lw);
2056 #endif
2057
2058         lw->main_box = gtk_vbox_new(FALSE, 0);
2059         gtk_container_add(GTK_CONTAINER(lw->window), lw->main_box);
2060         gtk_widget_show(lw->main_box);
2061
2062         layout_grid_setup(lw);
2063         image_top_window_set_sync(lw->image, (lw->tools_float || lw->tools_hidden));
2064
2065         layout_util_sync(lw);
2066         layout_status_update_all(lw);
2067
2068         if (dir_fd)
2069                 {
2070                 layout_set_fd(lw, dir_fd);
2071                 }
2072         else
2073                 {
2074                 GdkPixbuf *pixbuf;
2075
2076                 pixbuf = pixbuf_inline(PIXBUF_INLINE_LOGO);
2077                 
2078                 /* FIXME: the zoom value set here is the value, which is then copied again and again
2079                    in "Leave Zoom at previous setting" mode. This is not ideal.  */
2080                 image_change_pixbuf(lw->image, pixbuf, 0.0, FALSE);
2081                 g_object_unref(pixbuf);
2082                 }
2083
2084         if (geometry)
2085                 {
2086                 if (!gtk_window_parse_geometry(GTK_WINDOW(lw->window), geometry))
2087                         {
2088                         log_printf("%s", _("Invalid geometry\n"));
2089                         }
2090                 }
2091
2092         gtk_widget_show(lw->window);
2093         layout_tools_hide(lw, lw->tools_hidden);
2094
2095         layout_window_list = g_list_append(layout_window_list, lw);
2096
2097         file_data_register_notify_func(layout_image_notify_cb, lw, NOTIFY_PRIORITY_LOW);
2098
2099         return lw;
2100 }
2101
2102 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */