When more than one image is selected, Split Horizontal, Vertical and Quad will use...
[geeqie.git] / src / layout_image.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_image.h"
15
16 #include "collect.h"
17 #include "dnd.h"
18 #include "editors.h"
19 #include "filedata.h"
20 #include "fullscreen.h"
21 #include "image.h"
22 #include "image-overlay.h"
23 #include "img-view.h"
24 #include "info.h"
25 #include "layout.h"
26 #include "layout_util.h"
27 #include "menu.h"
28 #include "misc.h"
29 #include "pixbuf_util.h"
30 #include "slideshow.h"
31 #include "ui_fileops.h"
32 #include "ui_menu.h"
33 #include "uri_utils.h"
34 #include "utilops.h"
35
36 #include <gdk/gdkkeysyms.h> /* for keyboard values */
37
38
39 static GtkWidget *layout_image_pop_menu(LayoutWindow *lw);
40 static void layout_image_set_buttons(LayoutWindow *lw);
41
42 /*
43  *----------------------------------------------------------------------------
44  * full screen overlay
45  *----------------------------------------------------------------------------
46  */
47
48 void layout_image_overlay_toggle(LayoutWindow *lw)
49 {
50         if (!lw) return;
51         image_osd_toggle(lw->image);
52 }
53
54 /*
55  *----------------------------------------------------------------------------
56  * full screen
57  *----------------------------------------------------------------------------
58  */
59
60 static void layout_image_full_screen_stop_func(FullScreenData *fs, gpointer data)
61 {
62         LayoutWindow *lw = data;
63
64         /* restore image window */
65         lw->image = fs->normal_imd;
66
67         if (lw->slideshow)
68                 {
69                 lw->slideshow->imd = lw->image;
70                 }
71
72         lw->full_screen = NULL;
73 }
74
75 void layout_image_full_screen_start(LayoutWindow *lw)
76 {
77         if (!layout_valid(&lw)) return;
78
79         if (lw->full_screen) return;
80
81         lw->full_screen = fullscreen_start(lw->window, lw->image,
82                                            layout_image_full_screen_stop_func, lw);
83
84         /* set to new image window */
85         lw->image = lw->full_screen->imd;
86
87         if (lw->slideshow)
88                 {
89                 lw->slideshow->imd = lw->image;
90                 }
91
92         layout_image_set_buttons(lw);
93
94         g_signal_connect(G_OBJECT(lw->full_screen->window), "key_press_event",
95                          G_CALLBACK(layout_key_press_cb), lw);
96
97         layout_actions_add_window(lw, lw->full_screen->window);
98 #if 0
99         gtk_widget_set_sensitive(lw->window, FALSE);
100         if (lw->tools) gtk_widget_set_sensitive(lw->tools, FALSE);
101 #endif
102
103         if (image_osd_get(lw->full_screen->normal_imd) & OSD_SHOW_INFO)
104                 {
105                 image_osd_set(lw->image, image_osd_get(lw->full_screen->normal_imd));
106                 image_osd_set(lw->full_screen->normal_imd, OSD_SHOW_NOTHING);
107                 }
108 }
109
110 void layout_image_full_screen_stop(LayoutWindow *lw)
111 {
112         if (!layout_valid(&lw)) return;
113         if (!lw->full_screen) return;
114
115         if (image_osd_get(lw->full_screen->normal_imd) & OSD_SHOW_INFO)
116                 image_osd_set(lw->full_screen->normal_imd, image_osd_get(lw->image));
117
118         fullscreen_stop(lw->full_screen);
119
120 #if 0
121         gtk_widget_set_sensitive(lw->window, TRUE);
122         if (lw->tools) gtk_widget_set_sensitive(lw->tools, TRUE);
123 #endif
124 }
125
126 void layout_image_full_screen_toggle(LayoutWindow *lw)
127 {
128         if (!layout_valid(&lw)) return;
129         if (lw->full_screen)
130                 {
131                 layout_image_full_screen_stop(lw);
132                 }
133         else
134                 {
135                 layout_image_full_screen_start(lw);
136                 }
137 }
138
139 gint layout_image_full_screen_active(LayoutWindow *lw)
140 {
141         if (!layout_valid(&lw)) return FALSE;
142
143         return (lw->full_screen != NULL);
144 }
145
146 /*
147  *----------------------------------------------------------------------------
148  * slideshow
149  *----------------------------------------------------------------------------
150  */
151
152 static void layout_image_slideshow_next(LayoutWindow *lw)
153 {
154         if (lw->slideshow) slideshow_next(lw->slideshow);
155 }
156
157 static void layout_image_slideshow_prev(LayoutWindow *lw)
158 {
159         if (lw->slideshow) slideshow_prev(lw->slideshow);
160 }
161
162 static void layout_image_slideshow_stop_func(SlideShowData *ss, gpointer data)
163 {
164         LayoutWindow *lw = data;
165
166         lw->slideshow = NULL;
167         layout_status_update_info(lw, NULL);
168 }
169
170 void layout_image_slideshow_start(LayoutWindow *lw)
171 {
172         CollectionData *cd;
173         CollectInfo *info;
174
175         if (!layout_valid(&lw)) return;
176         if (lw->slideshow) return;
177
178         cd = image_get_collection(lw->image, &info);
179
180         if (cd && info)
181                 {
182                 lw->slideshow = slideshow_start_from_collection(lw->image, cd,
183                                 layout_image_slideshow_stop_func, lw, info);
184                 }
185         else
186                 {
187                 lw->slideshow = slideshow_start(lw->image, lw,
188                                 layout_list_get_index(lw, layout_image_get_fd(lw)),
189                                 layout_image_slideshow_stop_func, lw);
190                 }
191
192         layout_status_update_info(lw, NULL);
193 }
194
195 /* note that slideshow will take ownership of the list, do not free it */
196 void layout_image_slideshow_start_from_list(LayoutWindow *lw, GList *list)
197 {
198         if (!layout_valid(&lw)) return;
199
200         if (lw->slideshow || !list)
201                 {
202                 filelist_free(list);
203                 return;
204                 }
205
206         lw->slideshow = slideshow_start_from_filelist(lw->image, list,
207                                                        layout_image_slideshow_stop_func, lw);
208
209         layout_status_update_info(lw, NULL);
210 }
211
212 void layout_image_slideshow_stop(LayoutWindow *lw)
213 {
214         if (!layout_valid(&lw)) return;
215
216         if (!lw->slideshow) return;
217
218         slideshow_free(lw->slideshow);
219         /* the stop_func sets lw->slideshow to NULL for us */
220 }
221
222 void layout_image_slideshow_toggle(LayoutWindow *lw)
223 {
224         if (!layout_valid(&lw)) return;
225
226         if (lw->slideshow)
227                 {
228                 layout_image_slideshow_stop(lw);
229                 }
230         else
231                 {
232                 layout_image_slideshow_start(lw);
233                 }
234 }
235
236 gint layout_image_slideshow_active(LayoutWindow *lw)
237 {
238         if (!layout_valid(&lw)) return FALSE;
239
240         return (lw->slideshow != NULL);
241 }
242
243 gint layout_image_slideshow_pause_toggle(LayoutWindow *lw)
244 {
245         gint ret;
246
247         if (!layout_valid(&lw)) return FALSE;
248
249         ret = slideshow_pause_toggle(lw->slideshow);
250
251         layout_status_update_info(lw, NULL);
252
253         return ret;
254 }
255
256 gint layout_image_slideshow_paused(LayoutWindow *lw)
257 {
258         if (!layout_valid(&lw)) return FALSE;
259
260         return (slideshow_paused(lw->slideshow));
261 }
262
263 static gint layout_image_slideshow_continue_check(LayoutWindow *lw)
264 {
265         if (!lw->slideshow) return FALSE;
266
267         if (!slideshow_should_continue(lw->slideshow))
268                 {
269                 layout_image_slideshow_stop(lw);
270                 return FALSE;
271                 }
272
273         return TRUE;
274 }
275
276 /*
277  *----------------------------------------------------------------------------
278  * pop-up menus
279  *----------------------------------------------------------------------------
280  */
281
282 static void li_pop_menu_zoom_in_cb(GtkWidget *widget, gpointer data)
283 {
284         LayoutWindow *lw = data;
285
286         layout_image_zoom_adjust(lw, get_zoom_increment(), FALSE);
287 }
288
289 static void li_pop_menu_zoom_out_cb(GtkWidget *widget, gpointer data)
290 {
291         LayoutWindow *lw = data;
292         layout_image_zoom_adjust(lw, -get_zoom_increment(), FALSE);
293 }
294
295 static void li_pop_menu_zoom_1_1_cb(GtkWidget *widget, gpointer data)
296 {
297         LayoutWindow *lw = data;
298
299         layout_image_zoom_set(lw, 1.0, FALSE);
300 }
301
302 static void li_pop_menu_zoom_fit_cb(GtkWidget *widget, gpointer data)
303 {
304         LayoutWindow *lw = data;
305
306         layout_image_zoom_set(lw, 0.0, FALSE);
307 }
308
309 static void li_pop_menu_edit_cb(GtkWidget *widget, gpointer data)
310 {
311         LayoutWindow *lw;
312         gint n;
313
314         lw = submenu_item_get_data(widget);
315         n = GPOINTER_TO_INT(data);
316
317         if (!editor_window_flag_set(n))
318                 {
319                 layout_image_full_screen_stop(lw);
320                 }
321         file_util_start_editor_from_file(n, layout_image_get_fd(lw), lw->window);
322 }
323
324 static void li_pop_menu_wallpaper_cb(GtkWidget *widget, gpointer data)
325 {
326         LayoutWindow *lw = data;
327
328         layout_image_to_root(lw);
329 }
330
331 static void li_pop_menu_alter_cb(GtkWidget *widget, gpointer data)
332 {
333         LayoutWindow *lw = data;
334         AlterType type;
335
336         lw = submenu_item_get_data(widget);
337         type = (AlterType)GPOINTER_TO_INT(data);
338
339         image_alter(lw->image, type);
340 }
341
342 static void li_pop_menu_info_cb(GtkWidget *widget, gpointer data)
343 {
344         LayoutWindow *lw = data;
345
346         info_window_new(layout_image_get_fd(lw), NULL, lw->full_screen ? lw->full_screen->window : NULL);
347 }
348
349 static void li_pop_menu_new_cb(GtkWidget *widget, gpointer data)
350 {
351         LayoutWindow *lw = data;
352
353         view_window_new(layout_image_get_fd(lw));
354 }
355
356 static GtkWidget *li_pop_menu_click_parent(GtkWidget *widget, LayoutWindow *lw)
357 {
358         GtkWidget *menu;
359         GtkWidget *parent;
360
361         menu = gtk_widget_get_toplevel(widget);
362         if (!menu) return NULL;
363
364         parent = g_object_get_data(G_OBJECT(menu), "click_parent");
365
366         if (!parent && lw->full_screen)
367                 {
368                 parent = lw->full_screen->imd->widget;
369                 }
370
371         return parent;
372 }
373
374 static void li_pop_menu_copy_cb(GtkWidget *widget, gpointer data)
375 {
376         LayoutWindow *lw = data;
377
378         file_util_copy(layout_image_get_fd(lw), NULL, NULL,
379                        li_pop_menu_click_parent(widget, lw));
380 }
381
382 static void li_pop_menu_copy_path_cb(GtkWidget *widget, gpointer data)
383 {
384         LayoutWindow *lw = data;
385
386         file_util_copy_path_to_clipboard(layout_image_get_fd(lw));
387 }
388
389 static void li_pop_menu_move_cb(GtkWidget *widget, gpointer data)
390 {
391         LayoutWindow *lw = data;
392
393         file_util_move(layout_image_get_fd(lw), NULL, NULL,
394                        li_pop_menu_click_parent(widget, lw));
395 }
396
397 static void li_pop_menu_rename_cb(GtkWidget *widget, gpointer data)
398 {
399         LayoutWindow *lw = data;
400
401         file_util_rename(layout_image_get_fd(lw), NULL,
402                          li_pop_menu_click_parent(widget, lw));
403 }
404
405 static void li_pop_menu_delete_cb(GtkWidget *widget, gpointer data)
406 {
407         LayoutWindow *lw = data;
408
409         file_util_delete(layout_image_get_fd(lw), NULL,
410                          li_pop_menu_click_parent(widget, lw));
411 }
412
413 static void li_pop_menu_slide_start_cb(GtkWidget *widget, gpointer data)
414 {
415         LayoutWindow *lw = data;
416
417         layout_image_slideshow_start(lw);
418 }
419
420 static void li_pop_menu_slide_stop_cb(GtkWidget *widget, gpointer data)
421 {
422         LayoutWindow *lw = data;
423
424         layout_image_slideshow_stop(lw);
425 }
426
427 static void li_pop_menu_slide_pause_cb(GtkWidget *widget, gpointer data)
428 {
429         LayoutWindow *lw = data;
430
431         layout_image_slideshow_pause_toggle(lw);
432 }
433
434 static void li_pop_menu_full_screen_cb(GtkWidget *widget, gpointer data)
435 {
436         LayoutWindow *lw = data;
437
438         layout_image_full_screen_toggle(lw);
439 }
440
441 static void li_pop_menu_hide_cb(GtkWidget *widget, gpointer data)
442 {
443         LayoutWindow *lw = data;
444
445         layout_tools_hide_toggle(lw);
446 }
447
448 static void li_set_layout_path_cb(GtkWidget *widget, gpointer data)
449 {
450         LayoutWindow *lw = data;
451         FileData *fd;
452
453         if (!layout_valid(&lw)) return;
454
455         fd = layout_image_get_fd(lw);
456         if (fd) layout_set_fd(lw, fd);
457 }
458
459 static gint li_check_if_current_path(LayoutWindow *lw, const gchar *path)
460 {
461         gchar *dirname;
462         gint ret;
463
464         if (!path || !layout_valid(&lw) || !lw->dir_fd) return FALSE;
465
466         dirname = g_path_get_dirname(path);
467         ret = (strcmp(lw->dir_fd->path, dirname) == 0);
468         g_free(dirname);
469         return ret;
470 }
471
472 static GtkWidget *layout_image_pop_menu(LayoutWindow *lw)
473 {
474         GtkWidget *menu;
475         GtkWidget *item;
476         GtkWidget *submenu;
477         const gchar *path;
478         gint fullscreen;
479
480         path = layout_image_get_path(lw);
481         fullscreen = layout_image_full_screen_active(lw);
482
483         menu = popup_menu_short_lived();
484
485         menu_item_add_stock(menu, _("Zoom _in"), GTK_STOCK_ZOOM_IN, G_CALLBACK(li_pop_menu_zoom_in_cb), lw);
486         menu_item_add_stock(menu, _("Zoom _out"), GTK_STOCK_ZOOM_OUT, G_CALLBACK(li_pop_menu_zoom_out_cb), lw);
487         menu_item_add_stock(menu, _("Zoom _1:1"), GTK_STOCK_ZOOM_100, G_CALLBACK(li_pop_menu_zoom_1_1_cb), lw);
488         menu_item_add_stock(menu, _("Fit image to _window"), GTK_STOCK_ZOOM_FIT, G_CALLBACK(li_pop_menu_zoom_fit_cb), lw);
489         menu_item_add_divider(menu);
490
491         submenu = submenu_add_edit(menu, &item, G_CALLBACK(li_pop_menu_edit_cb), lw);
492         if (!path) gtk_widget_set_sensitive(item, FALSE);
493         menu_item_add_divider(submenu);
494         menu_item_add(submenu, _("Set as _wallpaper"), G_CALLBACK(li_pop_menu_wallpaper_cb), lw);
495
496         item = submenu_add_alter(menu, G_CALLBACK(li_pop_menu_alter_cb), lw);
497
498         item = menu_item_add_stock(menu, _("_Properties"), GTK_STOCK_PROPERTIES, G_CALLBACK(li_pop_menu_info_cb), lw);
499         if (!path) gtk_widget_set_sensitive(item, FALSE);
500
501         item = menu_item_add_stock(menu, _("View in _new window"), GTK_STOCK_NEW, G_CALLBACK(li_pop_menu_new_cb), lw);
502         if (!path || fullscreen) gtk_widget_set_sensitive(item, FALSE);
503
504         item = menu_item_add(menu, _("_Go to directory view"), G_CALLBACK(li_set_layout_path_cb), lw);
505         if (!path || li_check_if_current_path(lw, path)) gtk_widget_set_sensitive(item, FALSE);
506
507         menu_item_add_divider(menu);
508
509         item = menu_item_add_stock(menu, _("_Copy..."), GTK_STOCK_COPY, G_CALLBACK(li_pop_menu_copy_cb), lw);
510         if (!path) gtk_widget_set_sensitive(item, FALSE);
511         item = menu_item_add(menu, _("_Move..."), G_CALLBACK(li_pop_menu_move_cb), lw);
512         if (!path) gtk_widget_set_sensitive(item, FALSE);
513         item = menu_item_add(menu, _("_Rename..."), G_CALLBACK(li_pop_menu_rename_cb), lw);
514         if (!path) gtk_widget_set_sensitive(item, FALSE);
515         item = menu_item_add_stock(menu, _("_Delete..."), GTK_STOCK_DELETE, G_CALLBACK(li_pop_menu_delete_cb), lw);
516         if (!path) gtk_widget_set_sensitive(item, FALSE);
517         
518         if (options->show_copy_path)
519                 {
520                 item = menu_item_add(menu, _("_Copy path"), G_CALLBACK(li_pop_menu_copy_path_cb), lw);
521                 if (!path) gtk_widget_set_sensitive(item, FALSE);
522         }
523
524         menu_item_add_divider(menu);
525
526         if (layout_image_slideshow_active(lw))
527                 {
528                 menu_item_add(menu, _("_Stop slideshow"), G_CALLBACK(li_pop_menu_slide_stop_cb), lw);
529                 if (layout_image_slideshow_paused(lw))
530                         {
531                         item = menu_item_add(menu, _("Continue slides_how"),
532                                              G_CALLBACK(li_pop_menu_slide_pause_cb), lw);
533                         }
534                 else
535                         {
536                         item = menu_item_add(menu, _("Pause slides_how"),
537                                              G_CALLBACK(li_pop_menu_slide_pause_cb), lw);
538                         }
539                 }
540         else
541                 {
542                 menu_item_add(menu, _("_Start slideshow"), G_CALLBACK(li_pop_menu_slide_start_cb), lw);
543                 item = menu_item_add(menu, _("Pause slides_how"), G_CALLBACK(li_pop_menu_slide_pause_cb), lw);
544                 gtk_widget_set_sensitive(item, FALSE);
545                 }
546
547         if (!fullscreen)
548                 {
549                 menu_item_add(menu, _("_Full screen"), G_CALLBACK(li_pop_menu_full_screen_cb), lw);
550                 }
551         else
552                 {
553                 menu_item_add(menu, _("Exit _full screen"), G_CALLBACK(li_pop_menu_full_screen_cb), lw);
554                 }
555
556         menu_item_add_divider(menu);
557
558         item = menu_item_add_check(menu, _("Hide file _list"), lw->tools_hidden,
559                                    G_CALLBACK(li_pop_menu_hide_cb), lw);
560         if (fullscreen) gtk_widget_set_sensitive(item, FALSE);
561
562         return menu;
563 }
564
565 static void layout_image_menu_pos_cb(GtkMenu *menu, gint *x, gint *y, gboolean *push_in, gpointer data)
566 {
567         LayoutWindow *lw = data;
568
569         gdk_window_get_origin(lw->image->pr->window, x, y);
570         popup_menu_position_clamp(menu, x, y, 0);
571 }
572
573 void layout_image_menu_popup(LayoutWindow *lw)
574 {
575         GtkWidget *menu;
576
577         menu = layout_image_pop_menu(lw);
578         gtk_menu_popup(GTK_MENU(menu), NULL, NULL, layout_image_menu_pos_cb, lw, 0, GDK_CURRENT_TIME);
579 }
580
581 /*
582  *----------------------------------------------------------------------------
583  * dnd
584  *----------------------------------------------------------------------------
585  */
586
587 static void layout_image_dnd_receive(GtkWidget *widget, GdkDragContext *context,
588                                      gint x, gint y,
589                                      GtkSelectionData *selection_data, guint info,
590                                      guint time, gpointer data)
591 {
592         LayoutWindow *lw = data;
593         gint i;
594
595
596         for (i = 0; i < MAX_SPLIT_IMAGES; i++)
597                 {
598                 if (lw->split_images[i] && lw->split_images[i]->pr == widget)
599                         break;
600                 }
601         if (i < MAX_SPLIT_IMAGES)
602                 {
603                 DEBUG_1("dnd image activate %d", i);
604                 layout_image_activate(lw, i);
605                 }
606
607
608         if (info == TARGET_URI_LIST || info == TARGET_APP_COLLECTION_MEMBER)
609                 {
610                 CollectionData *source;
611                 GList *list;
612                 GList *info_list;
613
614                 if (info == TARGET_URI_LIST)
615                         {
616                         list = uri_filelist_from_text((gchar *)selection_data->data, TRUE);
617                         source = NULL;
618                         info_list = NULL;
619                         }
620                 else
621                         {
622                         source = collection_from_dnd_data((gchar *)selection_data->data, &list, &info_list);
623                         }
624
625                 if (list)
626                         {
627                         FileData *fd = list->data;
628
629                         if (isfile(fd->path))
630                                 {
631                                 gchar *base;
632                                 gint row;
633                                 FileData *dir_fd;
634
635                                 base = remove_level_from_path(fd->path);
636                                 dir_fd = file_data_new_simple(base);
637                                 if (dir_fd != lw->dir_fd)
638                                         {
639                                         layout_set_fd(lw, dir_fd);
640                                         }
641                                 file_data_unref(dir_fd);
642                                 g_free(base);
643
644                                 row = layout_list_get_index(lw, fd);
645                                 if (source && info_list)
646                                         {
647                                         layout_image_set_collection(lw, source, info_list->data);
648                                         }
649                                 else if (row == -1)
650                                         {
651                                         layout_image_set_fd(lw, fd);
652                                         }
653                                 else
654                                         {
655                                         layout_image_set_index(lw, row);
656                                         }
657                                 }
658                         else if (isdir(fd->path))
659                                 {
660                                 layout_set_fd(lw, fd);
661                                 layout_image_set_fd(lw, NULL);
662                                 }
663                         }
664
665                 filelist_free(list);
666                 g_list_free(info_list);
667                 }
668 }
669
670 static void layout_image_dnd_get(GtkWidget *widget, GdkDragContext *context,
671                                  GtkSelectionData *selection_data, guint info,
672                                  guint time, gpointer data)
673 {
674         LayoutWindow *lw = data;
675         FileData *fd;
676         gint i;
677
678
679         for (i = 0; i < MAX_SPLIT_IMAGES; i++)
680                 {
681                 if (lw->split_images[i] && lw->split_images[i]->pr == widget)
682                         break;
683                 }
684         if (i < MAX_SPLIT_IMAGES)
685                 {
686                 DEBUG_1("dnd get from %d", i);
687                 fd = image_get_fd(lw->split_images[i]);
688                 }
689         else
690                 fd = layout_image_get_fd(lw);
691
692         if (fd)
693                 {
694                 gchar *text = NULL;
695                 gint len;
696                 gint plain_text;
697                 GList *list;
698
699                 switch (info)
700                         {
701                         case TARGET_URI_LIST:
702                                 plain_text = FALSE;
703                                 break;
704                         case TARGET_TEXT_PLAIN:
705                         default:
706                                 plain_text = TRUE;
707                                 break;
708                         }
709                 list = g_list_append(NULL, fd);
710                 text = uri_text_from_filelist(list, &len, plain_text);
711                 g_list_free(list);
712                 if (text)
713                         {
714                         gtk_selection_data_set(selection_data, selection_data->target,
715                                                8, (guchar *)text, len);
716                         g_free(text);
717                         }
718                 }
719         else
720                 {
721                 gtk_selection_data_set(selection_data, selection_data->target,
722                                        8, NULL, 0);
723                 }
724 }
725
726 static void layout_image_dnd_end(GtkWidget *widget, GdkDragContext *context, gpointer data)
727 {
728         LayoutWindow *lw = data;
729         if (context->action == GDK_ACTION_MOVE)
730                 {
731                 FileData *fd;
732                 gint row;
733
734                 fd = layout_image_get_fd(lw);
735                 row = layout_list_get_index(lw, fd);
736                 if (row < 0) return;
737
738                 if (!isfile(fd->path))
739                         {
740                         if ((guint) row < layout_list_count(lw, NULL) - 1)
741                                 {
742                                 layout_image_next(lw);
743                                 }
744                         else
745                                 {
746                                 layout_image_prev(lw);
747                                 }
748                         }
749                 layout_refresh(lw);
750                 }
751 }
752
753 static void layout_image_dnd_init(LayoutWindow *lw, gint i)
754 {
755         ImageWindow *imd = lw->split_images[i];
756
757         gtk_drag_source_set(imd->pr, GDK_BUTTON2_MASK,
758                             dnd_file_drag_types, dnd_file_drag_types_count,
759                             GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK);
760         g_signal_connect(G_OBJECT(imd->pr), "drag_data_get",
761                          G_CALLBACK(layout_image_dnd_get), lw);
762         g_signal_connect(G_OBJECT(imd->pr), "drag_end",
763                          G_CALLBACK(layout_image_dnd_end), lw);
764
765         gtk_drag_dest_set(imd->pr,
766                           GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_DROP,
767                           dnd_file_drop_types, dnd_file_drop_types_count,
768                           GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK);
769         g_signal_connect(G_OBJECT(imd->pr), "drag_data_received",
770                          G_CALLBACK(layout_image_dnd_receive), lw);
771 }
772
773
774 /*
775  *----------------------------------------------------------------------------
776  * misc
777  *----------------------------------------------------------------------------
778  */
779
780 void layout_image_to_root(LayoutWindow *lw)
781 {
782         image_to_root_window(lw->image, (image_zoom_get(lw->image) == 0));
783 }
784
785 /*
786  *----------------------------------------------------------------------------
787  * manipulation + accessors
788  *----------------------------------------------------------------------------
789  */
790
791 void layout_image_scroll(LayoutWindow *lw, gint x, gint y, gboolean connect_scroll)
792 {
793         gdouble dx, dy;
794         gint width, height, i;
795         if (!layout_valid(&lw)) return;
796
797         image_scroll(lw->image, x, y);
798
799         if (!connect_scroll) return;
800
801         image_get_image_size(lw->image, &width, &height);
802         dx = (gdouble) x / width;
803         dy = (gdouble) y / height;
804         
805         for (i = 0; i < MAX_SPLIT_IMAGES; i++)
806                 {
807                 if (lw->split_images[i] && lw->split_images[i] != lw->image)
808                         {
809                         gdouble sx, sy;
810                         image_get_scroll_center(lw->split_images[i], &sx, &sy);
811                         sx += dx;
812                         sy += dy;
813                         image_set_scroll_center(lw->split_images[i], sx, sy);
814                         }
815                 }
816
817 }
818
819 void layout_image_zoom_adjust(LayoutWindow *lw, gdouble increment, gboolean connect_zoom)
820 {
821         gint i;
822         if (!layout_valid(&lw)) return;
823
824         image_zoom_adjust(lw->image, increment);
825
826         if (!connect_zoom) return;
827
828         for (i = 0; i < MAX_SPLIT_IMAGES; i++)
829                 {
830                 if (lw->split_images[i] && lw->split_images[i] != lw->image)
831                         image_zoom_adjust(lw->split_images[i], increment); ;
832                 }
833 }
834
835 void layout_image_zoom_adjust_at_point(LayoutWindow *lw, gdouble increment, gint x, gint y, gboolean connect_zoom)
836 {
837         gint i;
838         if (!layout_valid(&lw)) return;
839
840         image_zoom_adjust_at_point(lw->image, increment, x, y);
841
842         if (!connect_zoom) return;
843
844         for (i = 0; i < MAX_SPLIT_IMAGES; i++)
845                 {
846                 if (lw->split_images[i] && lw->split_images[i] != lw->image)
847                         image_zoom_adjust_at_point(lw->split_images[i], increment, x, y);
848                 }
849 }
850
851 void layout_image_zoom_set(LayoutWindow *lw, gdouble zoom, gboolean connect_zoom)
852 {
853         gint i;
854         if (!layout_valid(&lw)) return;
855
856         image_zoom_set(lw->image, zoom);
857
858         if (!connect_zoom) return;
859
860         for (i = 0; i < MAX_SPLIT_IMAGES; i++)
861                 {
862                 if (lw->split_images[i] && lw->split_images[i] != lw->image)
863                         image_zoom_set(lw->split_images[i], zoom);
864                 }
865 }
866
867 void layout_image_zoom_set_fill_geometry(LayoutWindow *lw, gint vertical, gboolean connect_zoom)
868 {
869         gint i;
870         if (!layout_valid(&lw)) return;
871
872         image_zoom_set_fill_geometry(lw->image, vertical);
873
874         if (!connect_zoom) return;
875
876         for (i = 0; i < MAX_SPLIT_IMAGES; i++)
877                 {
878                 if (lw->split_images[i] && lw->split_images[i] != lw->image)
879                         image_zoom_set_fill_geometry(lw->split_images[i], vertical);
880                 }
881 }
882
883 void layout_image_alter(LayoutWindow *lw, AlterType type)
884 {
885         if (!layout_valid(&lw)) return;
886
887         image_alter(lw->image, type);
888 }
889
890 const gchar *layout_image_get_path(LayoutWindow *lw)
891 {
892         if (!layout_valid(&lw)) return NULL;
893
894         return image_get_path(lw->image);
895 }
896
897 const gchar *layout_image_get_name(LayoutWindow *lw)
898 {
899         if (!layout_valid(&lw)) return NULL;
900
901         return image_get_name(lw->image);
902 }
903
904 FileData *layout_image_get_fd(LayoutWindow *lw)
905 {
906         if (!layout_valid(&lw)) return NULL;
907
908         return image_get_fd(lw->image);
909 }
910
911 CollectionData *layout_image_get_collection(LayoutWindow *lw, CollectInfo **info)
912 {
913         if (!layout_valid(&lw)) return NULL;
914
915         return image_get_collection(lw->image, info);
916 }
917
918 gint layout_image_get_index(LayoutWindow *lw)
919 {
920         return layout_list_get_index(lw, image_get_fd(lw->image));
921 }
922
923 /*
924  *----------------------------------------------------------------------------
925  * image changers
926  *----------------------------------------------------------------------------
927  */
928
929 void layout_image_set_fd(LayoutWindow *lw, FileData *fd)
930 {
931         if (!layout_valid(&lw)) return;
932
933         image_change_fd(lw->image, fd, image_zoom_get_default(lw->image));
934
935         layout_list_sync_fd(lw, fd);
936         layout_image_slideshow_continue_check(lw);
937         layout_bars_new_image(lw);
938 }
939
940 void layout_image_set_with_ahead(LayoutWindow *lw, FileData *fd, FileData *read_ahead_fd)
941 {
942         if (!layout_valid(&lw)) return;
943
944 /*
945 This should be handled at the caller: in vflist_select_image
946         if (path)
947                 {
948                 const gchar *old_path;
949
950                 old_path = layout_image_get_path(lw);
951                 if (old_path && strcmp(path, old_path) == 0) return;
952                 }
953 */
954         layout_image_set_fd(lw, fd);
955         if (options->image.enable_read_ahead) image_prebuffer_set(lw->image, read_ahead_fd);
956 }
957
958 void layout_image_set_index(LayoutWindow *lw, gint index)
959 {
960         FileData *fd;
961         FileData *read_ahead_fd;
962         gint old;
963
964         if (!layout_valid(&lw)) return;
965
966         old = layout_list_get_index(lw, layout_image_get_fd(lw));
967         fd = layout_list_get_fd(lw, index);
968
969         if (old > index)
970                 {
971                 read_ahead_fd = layout_list_get_fd(lw, index - 1);
972                 }
973         else
974                 {
975                 read_ahead_fd = layout_list_get_fd(lw, index + 1);
976                 }
977
978         if (layout_selection_count(lw, 0) > 1)
979                 {
980                 GList *x = layout_selection_list_by_index(lw);
981                 GList *y;
982                 GList *last;
983
984                 for (last = y = x; y; y = y->next)
985                         last = y;
986                 for (y = x; y && (GPOINTER_TO_INT(y->data)) != index; y = y->next)
987                         ;
988
989                 if (y)
990                         {
991                         gint newindex;
992
993                         if ((index > old && (index != GPOINTER_TO_INT(last->data) || old != GPOINTER_TO_INT(x->data)))
994                             || (old == GPOINTER_TO_INT(last->data) && index == GPOINTER_TO_INT(x->data)))
995                                 {
996                                 if (y->next)
997                                         newindex = GPOINTER_TO_INT(y->next->data);
998                                 else
999                                         newindex = GPOINTER_TO_INT(x->data);
1000                                 }
1001                         else
1002                                 {
1003                                 if (y->prev)
1004                                         newindex = GPOINTER_TO_INT(y->prev->data);
1005                                 else
1006                                         newindex = GPOINTER_TO_INT(last->data);
1007                                 }
1008
1009                         read_ahead_fd = layout_list_get_fd(lw, newindex);
1010                         }
1011
1012                 while (x)
1013                         x = g_list_remove(x, x->data);
1014                 }
1015
1016         layout_image_set_with_ahead(lw, fd, read_ahead_fd);
1017 }
1018
1019 static void layout_image_set_collection_real(LayoutWindow *lw, CollectionData *cd, CollectInfo *info, gint forward)
1020 {
1021         if (!layout_valid(&lw)) return;
1022
1023         image_change_from_collection(lw->image, cd, info, image_zoom_get_default(lw->image));
1024         if (options->image.enable_read_ahead)
1025                 {
1026                 CollectInfo *r_info;
1027                 if (forward)
1028                         {
1029                         r_info = collection_next_by_info(cd, info);
1030                         if (!r_info) r_info = collection_prev_by_info(cd, info);
1031                         }
1032                 else
1033                         {
1034                         r_info = collection_prev_by_info(cd, info);
1035                         if (!r_info) r_info = collection_next_by_info(cd, info);
1036                         }
1037                 if (r_info) image_prebuffer_set(lw->image, r_info->fd);
1038                 }
1039
1040         layout_image_slideshow_continue_check(lw);
1041         layout_bars_new_image(lw);
1042 }
1043
1044 void layout_image_set_collection(LayoutWindow *lw, CollectionData *cd, CollectInfo *info)
1045 {
1046         layout_image_set_collection_real(lw, cd, info, TRUE);
1047         layout_list_sync_fd(lw, layout_image_get_fd(lw));
1048 }
1049
1050 void layout_image_refresh(LayoutWindow *lw)
1051 {
1052         if (!layout_valid(&lw)) return;
1053
1054         image_reload(lw->image);
1055 }
1056
1057 void layout_image_color_profile_set(LayoutWindow *lw,
1058                                     gint input_type, gint screen_type,
1059                                     gint use_image)
1060 {
1061         if (!layout_valid(&lw)) return;
1062
1063         image_color_profile_set(lw->image, input_type, screen_type, use_image);
1064 }
1065
1066 gint layout_image_color_profile_get(LayoutWindow *lw,
1067                                     gint *input_type, gint *screen_type,
1068                                     gint *use_image)
1069 {
1070         if (!layout_valid(&lw)) return FALSE;
1071
1072         return image_color_profile_get(lw->image, input_type, screen_type, use_image);
1073 }
1074
1075 void layout_image_color_profile_set_use(LayoutWindow *lw, gint enable)
1076 {
1077         if (!layout_valid(&lw)) return;
1078
1079         image_color_profile_set_use(lw->image, enable);
1080
1081         if (lw->info_color)
1082                 {
1083 #ifndef HAVE_LCMS
1084                 enable = FALSE;
1085 #endif
1086                 gtk_widget_set_sensitive(GTK_BIN(lw->info_color)->child, enable);
1087                 }
1088 }
1089
1090 gint layout_image_color_profile_get_use(LayoutWindow *lw)
1091 {
1092         if (!layout_valid(&lw)) return FALSE;
1093
1094         return image_color_profile_get_use(lw->image);
1095 }
1096
1097 gint layout_image_color_profile_get_from_image(LayoutWindow *lw)
1098 {
1099         if (!layout_valid(&lw)) return FALSE;
1100
1101         return image_color_profile_get_from_image(lw->image);
1102 }
1103
1104 /*
1105  *----------------------------------------------------------------------------
1106  * list walkers
1107  *----------------------------------------------------------------------------
1108  */
1109
1110 void layout_image_next(LayoutWindow *lw)
1111 {
1112         gint current;
1113         CollectionData *cd;
1114         CollectInfo *info;
1115
1116         if (!layout_valid(&lw)) return;
1117
1118         if (layout_image_slideshow_active(lw))
1119                 {
1120                 layout_image_slideshow_next(lw);
1121                 return;
1122                 }
1123
1124         if (layout_selection_count(lw, 0) > 1)
1125                 {
1126                 GList *x = layout_selection_list_by_index(lw);
1127                 gint old = layout_list_get_index(lw, layout_image_get_fd(lw));
1128                 GList *y;
1129
1130                 for (y = x; y && (GPOINTER_TO_INT(y->data)) != old; y = y->next)
1131                         ;
1132                 if (y)
1133                         {
1134                         if (y->next)
1135                                 layout_image_set_index(lw, GPOINTER_TO_INT(y->next->data));
1136                         else
1137                                 layout_image_set_index(lw, GPOINTER_TO_INT(x->data));
1138                         }
1139                 while (x)
1140                         x = g_list_remove(x, x->data);
1141                 if (y) /* not dereferenced */
1142                         return;
1143                 }
1144
1145         cd = image_get_collection(lw->image, &info);
1146
1147         if (cd && info)
1148                 {
1149                 info = collection_next_by_info(cd, info);
1150                 if (info)
1151                         {
1152                         layout_image_set_collection_real(lw, cd, info, TRUE);
1153                         }
1154                 else
1155                         {
1156                         image_osd_icon(lw->image, IMAGE_OSD_LAST, -1);
1157                         }
1158                 return;
1159                 }
1160
1161         current = layout_image_get_index(lw);
1162
1163         if (current >= 0)
1164                 {
1165                 if ((guint) current < layout_list_count(lw, NULL) - 1)
1166                         {
1167                         layout_image_set_index(lw, current + 1);
1168                         }
1169                 else
1170                         {
1171                         image_osd_icon(lw->image, IMAGE_OSD_LAST, -1);
1172                         }
1173                 }
1174         else
1175                 {
1176                 layout_image_set_index(lw, 0);
1177                 }
1178 }
1179
1180 void layout_image_prev(LayoutWindow *lw)
1181 {
1182         gint current;
1183         CollectionData *cd;
1184         CollectInfo *info;
1185
1186         if (!layout_valid(&lw)) return;
1187
1188         if (layout_image_slideshow_active(lw))
1189                 {
1190                 layout_image_slideshow_prev(lw);
1191                 return;
1192                 }
1193
1194         if (layout_selection_count(lw, 0) > 1)
1195                 {
1196                 GList *x = layout_selection_list_by_index(lw);
1197                 gint old = layout_list_get_index(lw, layout_image_get_fd(lw));
1198                 GList *y;
1199                 GList *last;
1200
1201                 for (last = y = x; y; y = y->next)
1202                         last = y;
1203                 for (y = x; y && (GPOINTER_TO_INT(y->data)) != old; y = y->next)
1204                         ;
1205                 if (y)
1206                         {
1207                         if (y->prev)
1208                                 layout_image_set_index(lw, GPOINTER_TO_INT(y->prev->data));
1209                         else
1210                                 layout_image_set_index(lw, GPOINTER_TO_INT(last->data));
1211                         }
1212                 while (x)
1213                         x = g_list_remove(x, x->data);
1214                 if (y) /* not dereferenced */
1215                         return;
1216                 }
1217
1218         cd = image_get_collection(lw->image, &info);
1219
1220         if (cd && info)
1221                 {
1222                 info = collection_prev_by_info(cd, info);
1223                 if (info)
1224                         {
1225                         layout_image_set_collection_real(lw, cd, info, FALSE);
1226                         }
1227                 else
1228                         {
1229                         image_osd_icon(lw->image, IMAGE_OSD_FIRST, -1);
1230                         }
1231                 return;
1232                 }
1233
1234         current = layout_image_get_index(lw);
1235
1236         if (current >= 0)
1237                 {
1238                 if (current > 0)
1239                         {
1240                         layout_image_set_index(lw, current - 1);
1241                         }
1242                 else
1243                         {
1244                         image_osd_icon(lw->image, IMAGE_OSD_FIRST, -1);
1245                         }
1246                 }
1247         else
1248                 {
1249                 layout_image_set_index(lw, layout_list_count(lw, NULL) - 1);
1250                 }
1251 }
1252
1253 void layout_image_first(LayoutWindow *lw)
1254 {
1255         gint current;
1256         CollectionData *cd;
1257         CollectInfo *info;
1258
1259         if (!layout_valid(&lw)) return;
1260
1261         cd = image_get_collection(lw->image, &info);
1262
1263         if (cd && info)
1264                 {
1265                 CollectInfo *new;
1266                 new = collection_get_first(cd);
1267                 if (new != info) layout_image_set_collection_real(lw, cd, new, TRUE);
1268                 return;
1269                 }
1270
1271         current = layout_image_get_index(lw);
1272         if (current != 0 && layout_list_count(lw, NULL) > 0)
1273                 {
1274                 layout_image_set_index(lw, 0);
1275                 }
1276 }
1277
1278 void layout_image_last(LayoutWindow *lw)
1279 {
1280         gint current;
1281         gint count;
1282         CollectionData *cd;
1283         CollectInfo *info;
1284
1285         if (!layout_valid(&lw)) return;
1286
1287         cd = image_get_collection(lw->image, &info);
1288
1289         if (cd && info)
1290                 {
1291                 CollectInfo *new;
1292                 new = collection_get_last(cd);
1293                 if (new != info) layout_image_set_collection_real(lw, cd, new, FALSE);
1294                 return;
1295                 }
1296
1297         current = layout_image_get_index(lw);
1298         count = layout_list_count(lw, NULL);
1299         if (current != count - 1 && count > 0)
1300                 {
1301                 layout_image_set_index(lw, count - 1);
1302                 }
1303 }
1304
1305 /*
1306  *----------------------------------------------------------------------------
1307  * mouse callbacks
1308  *----------------------------------------------------------------------------
1309  */
1310
1311 static gint image_idx(LayoutWindow *lw, ImageWindow *imd)
1312 {
1313         gint i;
1314
1315         for (i = 0; i < MAX_SPLIT_IMAGES; i++)
1316                 {
1317                 if (lw->split_images[i] == imd)
1318                         break;
1319                 }
1320         if (i < MAX_SPLIT_IMAGES)
1321                 {
1322                 return i;
1323                 }
1324         return -1;
1325 }
1326
1327
1328 static void layout_image_button_cb(ImageWindow *imd, GdkEventButton *event, gpointer data)
1329 {
1330         LayoutWindow *lw = data;
1331         GtkWidget *menu;
1332
1333         switch (event->button)
1334                 {
1335                 case MOUSE_BUTTON_LEFT:
1336                         layout_image_next(lw);
1337                         break;
1338                 case MOUSE_BUTTON_MIDDLE:
1339                         layout_image_prev(lw);
1340                         break;
1341                 case MOUSE_BUTTON_RIGHT:
1342                         menu = layout_image_pop_menu(lw);
1343                         if (imd == lw->image)
1344                                 {
1345                                 g_object_set_data(G_OBJECT(menu), "click_parent", imd->widget);
1346                                 }
1347                         gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, 3, event->time);
1348                         break;
1349                 default:
1350                         break;
1351                 }
1352 }
1353
1354 static void layout_image_scroll_cb(ImageWindow *imd, GdkEventScroll *event, gpointer data)
1355 {
1356         LayoutWindow *lw = data;
1357
1358         gint i = image_idx(lw, imd);
1359
1360         if (i != -1)
1361                 {
1362                 DEBUG_1("image activate scroll %d", i);
1363                 layout_image_activate(lw, i);
1364                 }
1365
1366
1367         if (event->state & GDK_CONTROL_MASK)
1368                 {
1369                 switch (event->direction)
1370                         {
1371                         case GDK_SCROLL_UP:
1372                                 layout_image_zoom_adjust_at_point(lw, get_zoom_increment(), event->x, event->y, event->state & GDK_SHIFT_MASK);
1373                                 break;
1374                         case GDK_SCROLL_DOWN:
1375                                 layout_image_zoom_adjust_at_point(lw, -get_zoom_increment(), event->x, event->y, event->state & GDK_SHIFT_MASK);
1376                                 break;
1377                         default:
1378                                 break;
1379                         }
1380                 }
1381         else if (options->mousewheel_scrolls)
1382                 {
1383                 switch (event->direction)
1384                         {
1385                         case GDK_SCROLL_UP:
1386                                 image_scroll(imd, 0, -MOUSEWHEEL_SCROLL_SIZE);
1387                                 break;
1388                         case GDK_SCROLL_DOWN:
1389                                 image_scroll(imd, 0, MOUSEWHEEL_SCROLL_SIZE);
1390                                 break;
1391                         case GDK_SCROLL_LEFT:
1392                                 image_scroll(imd, -MOUSEWHEEL_SCROLL_SIZE, 0);
1393                                 break;
1394                         case GDK_SCROLL_RIGHT:
1395                                 image_scroll(imd, MOUSEWHEEL_SCROLL_SIZE, 0);
1396                                 break;
1397                         default:
1398                                 break;
1399                         }
1400                 }
1401         else
1402                 {
1403                 switch (event->direction)
1404                         {
1405                         case GDK_SCROLL_UP:
1406                                 layout_image_prev(lw);
1407                                 break;
1408                         case GDK_SCROLL_DOWN:
1409                                 layout_image_next(lw);
1410                                 break;
1411                         default:
1412                                 break;
1413                         }
1414                 }
1415 }
1416
1417 static void layout_image_drag_cb(ImageWindow *imd, GdkEventButton *event, gdouble dx, gdouble dy, gpointer data)
1418 {
1419         gint i;
1420         LayoutWindow *lw = data;
1421
1422         if (!(event->state & GDK_SHIFT_MASK)) return;
1423
1424         for (i = 0; i < MAX_SPLIT_IMAGES; i++)
1425                 {
1426                 if (lw->split_images[i] && lw->split_images[i] != imd)
1427                         {
1428                         gdouble sx, sy;
1429
1430                         if (event->state & GDK_CONTROL_MASK)
1431                                 {
1432                                 image_get_scroll_center(imd, &sx, &sy);
1433                                 }
1434                         else
1435                                 {
1436                                 image_get_scroll_center(lw->split_images[i], &sx, &sy);
1437                                 sx += dx;
1438                                 sy += dy;
1439                                 }
1440                         image_set_scroll_center(lw->split_images[i], sx, sy);
1441                         }
1442                 }
1443 }
1444
1445 static void layout_image_button_inactive_cb(ImageWindow *imd, GdkEventButton *event, gpointer data)
1446 {
1447         LayoutWindow *lw = data;
1448         GtkWidget *menu;
1449         gint i = image_idx(lw, imd);
1450
1451         if (i != -1)
1452                 {
1453                 layout_image_activate(lw, i);
1454                 }
1455
1456         switch (event->button)
1457                 {
1458                 case MOUSE_BUTTON_RIGHT:
1459                         menu = layout_image_pop_menu(lw);
1460                         if (imd == lw->image)
1461                                 {
1462                                 g_object_set_data(G_OBJECT(menu), "click_parent", imd->widget);
1463                                 }
1464                         gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, 3, event->time);
1465                         break;
1466                 default:
1467                         break;
1468                 }
1469
1470 }
1471
1472 static void layout_image_drag_inactive_cb(ImageWindow *imd, GdkEventButton *event, gdouble dx, gdouble dy, gpointer data)
1473 {
1474         LayoutWindow *lw = data;
1475         gint i = image_idx(lw, imd);
1476
1477         if (i != -1)
1478                 {
1479                 layout_image_activate(lw, i);
1480                 }
1481
1482         /* continue as with active image */
1483         layout_image_drag_cb(imd, event, dx, dy, data);
1484 }
1485
1486
1487 static void layout_image_set_buttons(LayoutWindow *lw)
1488 {
1489         image_set_button_func(lw->image, layout_image_button_cb, lw);
1490         image_set_scroll_func(lw->image, layout_image_scroll_cb, lw);
1491 }
1492
1493 static void layout_image_set_buttons_inactive(LayoutWindow *lw, gint i)
1494 {
1495         image_set_button_func(lw->split_images[i], layout_image_button_inactive_cb, lw);
1496         image_set_scroll_func(lw->split_images[i], layout_image_scroll_cb, lw);
1497 }
1498
1499 /*
1500  *----------------------------------------------------------------------------
1501  * setup
1502  *----------------------------------------------------------------------------
1503  */
1504
1505 static void layout_image_update_cb(ImageWindow *imd, gpointer data)
1506 {
1507         LayoutWindow *lw = data;
1508         layout_status_update_image(lw);
1509 }
1510
1511 GtkWidget *layout_image_new(LayoutWindow *lw, gint i)
1512 {
1513         if (!lw->split_image_sizegroup) lw->split_image_sizegroup = gtk_size_group_new(GTK_SIZE_GROUP_BOTH);
1514
1515         if (!lw->split_images[i])
1516                 {
1517                 lw->split_images[i] = image_new(TRUE);
1518
1519 #if GTK_CHECK_VERSION(2,12,0)
1520                 g_object_ref(lw->split_images[i]->widget);
1521 #else
1522                 gtk_widget_ref(lw->split_images[i]->widget);
1523 #endif
1524                 image_background_set_color(lw->split_images[i], options->image.use_custom_border_color ? &options->image.border_color : NULL);
1525
1526                 image_auto_refresh_enable(lw->split_images[i], TRUE);
1527
1528                 layout_image_dnd_init(lw, i);
1529                 image_color_profile_set(lw->split_images[i],
1530                                         options->color_profile.input_type,
1531                                         options->color_profile.screen_type,
1532                                         options->color_profile.use_image);
1533                 image_color_profile_set_use(lw->split_images[i], options->color_profile.enabled);
1534
1535                 gtk_size_group_add_widget(lw->split_image_sizegroup, lw->split_images[i]->widget);
1536                 }
1537
1538         return lw->split_images[i]->widget;
1539 }
1540
1541 void layout_image_deactivate(LayoutWindow *lw, gint i)
1542 {
1543
1544         if (!lw->split_images[i]) return;
1545         image_set_update_func(lw->split_images[i], NULL, NULL);
1546         layout_image_set_buttons_inactive(lw, i);
1547         image_set_drag_func(lw->split_images[i], layout_image_drag_inactive_cb, lw);
1548
1549         image_attach_window(lw->split_images[i], NULL, NULL, NULL, FALSE);
1550         image_select(lw->split_images[i], FALSE);
1551 }
1552
1553
1554 void layout_image_activate(LayoutWindow *lw, gint i)
1555 {
1556         FileData *fd;
1557
1558         if (!lw->split_images[i]) return;
1559
1560         /* deactivate currently active */
1561         if (lw->active_split_image != i)
1562                 layout_image_deactivate(lw, lw->active_split_image);
1563
1564         lw->image = lw->split_images[i];
1565         lw->active_split_image = i;
1566
1567         image_set_update_func(lw->image, layout_image_update_cb, lw);
1568         layout_image_set_buttons(lw);
1569         image_set_drag_func(lw->image, layout_image_drag_cb, lw);
1570
1571         image_attach_window(lw->image, lw->window, NULL, GQ_APPNAME, FALSE);
1572
1573         /* do not hilight selected image in SPLIT_NONE */
1574         /* maybe the image should be selected always and hilight should be controled by
1575            another image option */
1576         if (lw->split_mode != SPLIT_NONE)
1577                 image_select(lw->split_images[i], TRUE);
1578         else
1579                 image_select(lw->split_images[i], FALSE);
1580
1581         fd = image_get_fd(lw->image);
1582
1583         if (fd)
1584                 {
1585 //              layout_list_sync_path(lw, path);
1586                 layout_set_fd(lw, fd);
1587                 }
1588 }
1589
1590
1591 static void layout_image_setup_split_common(LayoutWindow *lw, gint n)
1592 {
1593         gboolean frame = (n == 1) ? (!lw->tools_float && !lw->tools_hidden) : 1;
1594         gint i;
1595
1596         for (i = 0; i < n; i++)
1597                 if (!lw->split_images[i])
1598                         {
1599                         FileData *img_fd = NULL;
1600                         double zoom = 0.0;
1601
1602                         layout_image_new(lw, i);
1603                         image_set_frame(lw->split_images[i], frame);
1604                         image_set_selectable(lw->split_images[i], 1);
1605
1606                         if (layout_selection_count(lw, 0) > 1)
1607                                 {
1608                                 GList *work = layout_selection_list(lw);
1609                                 gint j = 0;
1610
1611                                 while (work && j < i)
1612                                         {
1613                                         FileData *fd = work->data;
1614                                         work = work->next;
1615                                         
1616                                         j++;
1617                                         if (!fd || !*fd->path) continue;
1618                                         img_fd = fd;
1619                                         }
1620                                 }
1621
1622                         if (!img_fd && lw->image)
1623                                 {
1624                                 img_fd = image_get_fd(lw->image);
1625                                 zoom = image_zoom_get(lw->image);
1626                                 }
1627
1628                         if (img_fd)
1629                                 {
1630                                 gdouble sx, sy;
1631                                 image_change_fd(lw->split_images[i], img_fd, zoom);
1632                                 image_get_scroll_center(lw->image, &sx, &sy);
1633                                 image_set_scroll_center(lw->split_images[i], sx, sy);
1634                                 }
1635                         layout_image_deactivate(lw, i);
1636                         }
1637                 else
1638                         {
1639                         image_set_frame(lw->split_images[i], frame);
1640                         image_set_selectable(lw->split_images[i], 1);
1641                         }
1642
1643         for (i = n; i < MAX_SPLIT_IMAGES; i++)
1644                 {
1645                 if (lw->split_images[i])
1646                         {
1647 #if GTK_CHECK_VERSION(2,12,0)
1648                         g_object_unref(lw->split_images[i]->widget);
1649 #else
1650                         gtk_widget_unref(lw->split_images[i]->widget);
1651 #endif
1652                         lw->split_images[i] = NULL;
1653                         }
1654                 }
1655         
1656         if (!lw->image || lw->active_split_image < 0 || lw->active_split_image >= n)
1657                 {
1658                 layout_image_activate(lw, 0);
1659                 }
1660
1661 }
1662
1663 GtkWidget *layout_image_setup_split_none(LayoutWindow *lw)
1664 {
1665         lw->split_mode = SPLIT_NONE;
1666         
1667         layout_image_setup_split_common(lw, 1);
1668
1669         lw->split_image_widget = lw->split_images[0]->widget;
1670
1671         return lw->split_image_widget;
1672 }
1673
1674
1675 GtkWidget *layout_image_setup_split_hv(LayoutWindow *lw, gboolean horizontal)
1676 {
1677         GtkWidget *paned;
1678         
1679         lw->split_mode = horizontal ? SPLIT_HOR : SPLIT_VERT;
1680
1681         layout_image_setup_split_common(lw, 2);
1682
1683         /* horizontal split means vpaned and vice versa */
1684         if (horizontal)
1685                 paned = gtk_vpaned_new();
1686         else
1687                 paned = gtk_hpaned_new();
1688
1689         gtk_paned_pack1(GTK_PANED(paned), lw->split_images[0]->widget, TRUE, TRUE);
1690         gtk_paned_pack2(GTK_PANED(paned), lw->split_images[1]->widget, TRUE, TRUE);
1691
1692         gtk_widget_show(lw->split_images[0]->widget);
1693         gtk_widget_show(lw->split_images[1]->widget);
1694
1695         lw->split_image_widget = paned;
1696
1697         return lw->split_image_widget;
1698
1699 }
1700
1701 GtkWidget *layout_image_setup_split_quad(LayoutWindow *lw)
1702 {
1703         GtkWidget *hpaned;
1704         GtkWidget *vpaned1;
1705         GtkWidget *vpaned2;
1706         gint i;
1707
1708         lw->split_mode = SPLIT_QUAD;
1709
1710         layout_image_setup_split_common(lw, 4);
1711
1712         hpaned = gtk_hpaned_new();
1713         vpaned1 = gtk_vpaned_new();
1714         vpaned2 = gtk_vpaned_new();
1715
1716         gtk_paned_pack1(GTK_PANED(vpaned1), lw->split_images[0]->widget, TRUE, TRUE);
1717         gtk_paned_pack2(GTK_PANED(vpaned1), lw->split_images[2]->widget, TRUE, TRUE);
1718
1719         gtk_paned_pack1(GTK_PANED(vpaned2), lw->split_images[1]->widget, TRUE, TRUE);
1720         gtk_paned_pack2(GTK_PANED(vpaned2), lw->split_images[3]->widget, TRUE, TRUE);
1721
1722         gtk_paned_pack1(GTK_PANED(hpaned), vpaned1, TRUE, TRUE);
1723         gtk_paned_pack2(GTK_PANED(hpaned), vpaned2, TRUE, TRUE);
1724
1725         for (i = 0; i < 4; i++)
1726                 gtk_widget_show(lw->split_images[i]->widget);
1727
1728         gtk_widget_show(vpaned1);
1729         gtk_widget_show(vpaned2);
1730
1731         lw->split_image_widget = hpaned;
1732
1733         return lw->split_image_widget;
1734
1735 }
1736
1737 GtkWidget *layout_image_setup_split(LayoutWindow *lw, ImageSplitMode mode)
1738 {
1739         switch (mode)
1740                 {
1741                 case SPLIT_HOR:
1742                         return layout_image_setup_split_hv(lw, TRUE);
1743                 case SPLIT_VERT:
1744                         return layout_image_setup_split_hv(lw, FALSE);
1745                 case SPLIT_QUAD:
1746                         return layout_image_setup_split_quad(lw);
1747                 case SPLIT_NONE:
1748                 default:
1749                         return layout_image_setup_split_none(lw);
1750                 }
1751 }
1752
1753
1754 /*
1755  *-----------------------------------------------------------------------------
1756  * maintenance (for rename, move, remove)
1757  *-----------------------------------------------------------------------------
1758  */
1759
1760 static void layout_image_maint_renamed(LayoutWindow *lw, FileData *fd)
1761 {
1762         if (fd == layout_image_get_fd(lw))
1763                 {
1764                 image_set_fd(lw->image, fd);
1765                 layout_bars_maint_renamed(lw);
1766                 }
1767 }
1768
1769 static void layout_image_maint_removed(LayoutWindow *lw, FileData *fd)
1770 {
1771         if (fd == layout_image_get_fd(lw))
1772                 {
1773                 CollectionData *cd;
1774                 CollectInfo *info;
1775
1776                 cd = image_get_collection(lw->image, &info);
1777                 if (cd && info)
1778                         {
1779                         CollectInfo *new;
1780
1781                         new = collection_next_by_info(cd, info);
1782                         if (!new) new = collection_prev_by_info(cd, info);
1783
1784                         if (new)
1785                                 {
1786                                 layout_image_set_collection(lw, cd, new);
1787                                 return;
1788                                 }
1789                         }
1790
1791                 layout_image_set_fd(lw, NULL);
1792                 }
1793 }
1794
1795
1796 void layout_image_notify_cb(FileData *fd, NotifyType type, gpointer data)
1797 {
1798         LayoutWindow *lw = data;
1799
1800         if (type != NOTIFY_TYPE_CHANGE || !fd->change) return;
1801         
1802         switch(fd->change->type)
1803                 {
1804                 case FILEDATA_CHANGE_MOVE:
1805                 case FILEDATA_CHANGE_RENAME:
1806                         layout_image_maint_renamed(lw, fd);
1807                         break;
1808                 case FILEDATA_CHANGE_DELETE:
1809                         layout_image_maint_removed(lw, fd);
1810                         break;
1811                 case FILEDATA_CHANGE_COPY:
1812                 case FILEDATA_CHANGE_UNSPECIFIED:
1813                         break;
1814                 }
1815
1816 }
1817 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */