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