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