Use menu_item_add_stock_sensitive() and menu_item_add_sensitive().
[geeqie.git] / src / view_file_icon.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 "view_file_icon.h"
15
16 #include "cellrenderericon.h"
17 #include "collect.h"
18 #include "collect-io.h"
19 #include "collect-table.h"
20 #include "dnd.h"
21 #include "editors.h"
22 #include "img-view.h"
23 #include "info.h"
24 #include "filelist.h"
25 #include "layout.h"
26 #include "layout_image.h"
27 #include "menu.h"
28 #include "thumb.h"
29 #include "utilops.h"
30 #include "ui_bookmark.h"
31 #include "ui_fileops.h"
32 #include "ui_menu.h"
33 #include "ui_tree_edit.h"
34
35
36 #include <gdk/gdkkeysyms.h> /* for keyboard values */
37
38 /* between these, the icon width is increased by thumb_max_width / 2 */
39 #define THUMB_MIN_ICON_WIDTH 128
40 #define THUMB_MAX_ICON_WIDTH 150
41
42 #define VFICON_MAX_COLUMNS 32
43 #define THUMB_BORDER_PADDING 2
44
45 #define VFICON_TIP_DELAY 500
46
47 enum {
48         FILE_COLUMN_POINTER = 0,
49         FILE_COLUMN_COUNT
50 };
51
52 typedef enum {
53         SELECTION_NONE          = 0,
54         SELECTION_SELECTED      = 1 << 0,
55         SELECTION_PRELIGHT      = 1 << 1,
56         SELECTION_FOCUS         = 1 << 2
57 } SelectionType;
58
59 typedef struct _IconData IconData;
60 struct _IconData
61 {
62         SelectionType selected;
63         gint row;
64         FileData *fd;
65 };
66
67 static gint vficon_index_by_id(ViewFileIcon *vfi, IconData *in_id);
68
69 static IconData *vficon_icon_data(ViewFileIcon *vfi, FileData *fd)
70 {
71         IconData *id = NULL;
72         GList *work;
73
74         if (!fd) return NULL;
75         work = vfi->list;
76         while (work && !id)
77                 {
78                 IconData *chk = work->data;
79                 work = work->next;
80                 if (chk->fd == fd) id = chk;
81                 }
82         return id;
83 }
84
85
86 static gint iconlist_read(const gchar *path, GList **list)
87 {
88         GList *temp;
89         GList *work;
90
91         if (!filelist_read(path, &temp, NULL)) return FALSE;
92
93         work = temp;
94         while (work)
95                 {
96                 FileData *fd;
97                 IconData *id;
98
99                 fd = work->data;
100                 g_assert(fd->magick == 0x12345678);
101                 id = g_new0(IconData, 1);
102
103                 id->selected = SELECTION_NONE;
104                 id->row = -1;
105                 id->fd = fd;
106
107                 work->data = id;
108                 work = work->next;
109                 }
110
111         *list = temp;
112
113         return TRUE;
114 }
115
116 static void iconlist_free(GList *list)
117 {
118         GList *work = list;
119         while (work)
120                 {
121                 IconData *id = work->data;
122                 file_data_unref(id->fd);
123                 g_free(id);
124                 work = work->next;
125                 }
126
127         g_list_free(list);
128
129 }
130
131 gint iconlist_sort_file_cb(void *a, void *b)
132 {
133         IconData *ida = a;
134         IconData *idb = b;
135         return filelist_sort_compare_filedata(ida->fd, idb->fd);
136 }
137 GList *iconlist_sort(GList *list, SortType method, gint ascend)
138 {
139         return filelist_sort_full(list, method, ascend, (GCompareFunc) iconlist_sort_file_cb);
140 }
141
142 GList *iconlist_insert_sort(GList *list, IconData *id, SortType method, gint ascend)
143 {
144         return filelist_insert_sort_full(list, id, method, ascend, (GCompareFunc) iconlist_sort_file_cb);
145 }
146
147
148 static void vficon_toggle_filenames(ViewFileIcon *vfi);
149 static void vficon_selection_remove(ViewFileIcon *vfi, IconData *id, SelectionType mask, GtkTreeIter *iter);
150 static void vficon_move_focus(ViewFileIcon *vfi, gint row, gint col, gint relative);
151 static void vficon_set_focus(ViewFileIcon *vfi, IconData *id);
152 static void vficon_thumb_update(ViewFileIcon *vfi);
153 static void vficon_populate_at_new_size(ViewFileIcon *vfi, gint w, gint h, gint force);
154
155
156 /*
157  *-----------------------------------------------------------------------------
158  * pop-up menu
159  *-----------------------------------------------------------------------------
160  */
161
162 static GList *vficon_pop_menu_file_list(ViewFileIcon *vfi)
163 {
164         if (!vfi->click_id) return NULL;
165
166         if (vfi->click_id->selected & SELECTION_SELECTED)
167                 {
168                 return vficon_selection_get_list(vfi);
169                 }
170
171         return g_list_append(NULL, file_data_ref(vfi->click_id->fd));
172 }
173
174 static void vficon_pop_menu_edit_cb(GtkWidget *widget, gpointer data)
175 {
176         ViewFileIcon *vfi;
177         gint n;
178         GList *list;
179
180         vfi = submenu_item_get_data(widget);
181         n = GPOINTER_TO_INT(data);
182
183         if (!vfi) return;
184
185         list = vficon_pop_menu_file_list(vfi);
186         start_editor_from_filelist(n, list);
187         filelist_free(list);
188 }
189
190 static void vficon_pop_menu_info_cb(GtkWidget *widget, gpointer data)
191 {
192         ViewFileIcon *vfi = data;
193
194         info_window_new(NULL, vficon_pop_menu_file_list(vfi), NULL);
195 }
196
197 static void vficon_pop_menu_view_cb(GtkWidget *widget, gpointer data)
198 {
199         ViewFileIcon *vfi = data;
200
201         if (!vfi->click_id) return;
202
203         if (vfi->click_id->selected & SELECTION_SELECTED)
204                 {
205                 GList *list;
206
207                 list = vficon_selection_get_list(vfi);
208                 view_window_new_from_list(list);
209                 filelist_free(list);
210                 }
211         else
212                 {
213                 view_window_new(vfi->click_id->fd);
214                 }
215 }
216
217 static void vficon_pop_menu_copy_cb(GtkWidget *widget, gpointer data)
218 {
219         ViewFileIcon *vfi = data;
220
221         file_util_copy(NULL, vficon_pop_menu_file_list(vfi), NULL, vfi->listview);
222 }
223
224 static void vficon_pop_menu_move_cb(GtkWidget *widget, gpointer data)
225 {
226         ViewFileIcon *vfi = data;
227
228         file_util_move(NULL, vficon_pop_menu_file_list(vfi), NULL, vfi->listview);
229 }
230
231 static void vficon_pop_menu_rename_cb(GtkWidget *widget, gpointer data)
232 {
233         ViewFileIcon *vfi = data;
234
235         file_util_rename(NULL, vficon_pop_menu_file_list(vfi), vfi->listview);
236 }
237
238 static void vficon_pop_menu_delete_cb(GtkWidget *widget, gpointer data)
239 {
240         ViewFileIcon *vfi = data;
241
242         file_util_delete(NULL, vficon_pop_menu_file_list(vfi), vfi->listview);
243 }
244
245 static void vficon_pop_menu_copy_path_cb(GtkWidget *widget, gpointer data)
246 {
247         ViewFileIcon *vfi = data;
248
249         file_util_copy_path_list_to_clipboard(vficon_pop_menu_file_list(vfi));
250 }
251
252 static void vficon_pop_menu_sort_cb(GtkWidget *widget, gpointer data)
253 {
254         ViewFileIcon *vfi;
255         SortType type;
256
257         if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) return;
258
259         vfi = submenu_item_get_data(widget);
260         if (!vfi) return;
261
262         type = (SortType)GPOINTER_TO_INT(data);
263
264         if (vfi->layout)
265                 {
266                 layout_sort_set(vfi->layout, type, vfi->sort_ascend);
267                 }
268         else
269                 {
270                 vficon_sort_set(vfi, type, vfi->sort_ascend);
271                 }
272 }
273
274 static void vficon_pop_menu_sort_ascend_cb(GtkWidget *widget, gpointer data)
275 {
276         ViewFileIcon *vfi = data;
277
278         if (vfi->layout)
279                 {
280                 layout_sort_set(vfi->layout, vfi->sort_method, !vfi->sort_ascend);
281                 }
282         else
283                 {
284                 vficon_sort_set(vfi, vfi->sort_method, !vfi->sort_ascend);
285                 }
286 }
287
288 static void vficon_pop_menu_list_cb(GtkWidget *widget, gpointer data)
289 {
290         ViewFileIcon *vfi = data;
291
292         if (vfi->layout) layout_views_set(vfi->layout, vfi->layout->dir_view_type, FALSE);
293 }
294
295 static void vficon_pop_menu_show_names_cb(GtkWidget *widget, gpointer data)
296 {
297         ViewFileIcon *vfi = data;
298
299         vficon_toggle_filenames(vfi);
300 }
301
302 static void vficon_pop_menu_refresh_cb(GtkWidget *widget, gpointer data)
303 {
304         ViewFileIcon *vfi = data;
305
306         vficon_refresh(vfi);
307 }
308
309 static void vficon_popup_destroy_cb(GtkWidget *widget, gpointer data)
310 {
311         ViewFileIcon *vfi = data;
312         vficon_selection_remove(vfi, vfi->click_id, SELECTION_PRELIGHT, NULL);
313         vfi->click_id = NULL;
314         vfi->popup = NULL;
315 }
316
317 static GtkWidget *vficon_pop_menu(ViewFileIcon *vfi, gint active)
318 {
319         GtkWidget *menu;
320         GtkWidget *item;
321         GtkWidget *submenu;
322
323         menu = popup_menu_short_lived();
324
325         g_signal_connect(G_OBJECT(menu), "destroy",
326                          G_CALLBACK(vficon_popup_destroy_cb), vfi);
327
328         submenu_add_edit(menu, &item, G_CALLBACK(vficon_pop_menu_edit_cb), vfi);
329         gtk_widget_set_sensitive(item, active);
330
331         menu_item_add_stock_sensitive(menu, _("_Properties"), GTK_STOCK_PROPERTIES, active,
332                                       G_CALLBACK(vficon_pop_menu_info_cb), vfi);
333
334         menu_item_add_stock_sensitive(menu, _("View in _new window"), GTK_STOCK_NEW, active,
335                                       G_CALLBACK(vficon_pop_menu_view_cb), vfi);
336         menu_item_add_divider(menu);
337
338         menu_item_add_stock_sensitive(menu, _("_Copy..."), GTK_STOCK_COPY, active,
339                                       G_CALLBACK(vficon_pop_menu_copy_cb), vfi);
340         menu_item_add_sensitive(menu, _("_Move..."), active,
341                                 G_CALLBACK(vficon_pop_menu_move_cb), vfi);
342         menu_item_add_sensitive(menu, _("_Rename..."), active,
343                                 G_CALLBACK(vficon_pop_menu_rename_cb), vfi);
344         menu_item_add_stock_sensitive(menu, _("_Delete..."), GTK_STOCK_DELETE, active,
345                                       G_CALLBACK(vficon_pop_menu_delete_cb), vfi);
346         if (options->show_copy_path)
347                 menu_item_add_sensitive(menu, _("_Copy path"), active,
348                                         G_CALLBACK(vficon_pop_menu_copy_path_cb), vfi);
349         menu_item_add_divider(menu);
350
351         submenu = submenu_add_sort(NULL, G_CALLBACK(vficon_pop_menu_sort_cb), vfi,
352                                    FALSE, FALSE, TRUE, vfi->sort_method);
353         menu_item_add_divider(submenu);
354         menu_item_add_check(submenu, _("Ascending"), vfi->sort_ascend,
355                             G_CALLBACK(vficon_pop_menu_sort_ascend_cb), vfi);
356
357         item = menu_item_add(menu, _("_Sort"), NULL, NULL);
358         gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
359
360         menu_item_add_check(menu, _("View as _icons"), TRUE,
361                             G_CALLBACK(vficon_pop_menu_list_cb), vfi);
362         menu_item_add_check(menu, _("Show filename _text"), vfi->show_text,
363                             G_CALLBACK(vficon_pop_menu_show_names_cb), vfi);
364         menu_item_add_stock(menu, _("Re_fresh"), GTK_STOCK_REFRESH, G_CALLBACK(vficon_pop_menu_refresh_cb), vfi);
365
366         return menu;
367 }
368
369 /*
370  *-------------------------------------------------------------------
371  * signals
372  *-------------------------------------------------------------------
373  */
374
375 static void vficon_send_update(ViewFileIcon *vfi)
376 {
377         if (vfi->func_status) vfi->func_status(vfi, vfi->data_status);
378 }
379
380 static void vficon_send_layout_select(ViewFileIcon *vfi, IconData *id)
381 {
382         FileData *read_ahead_fd = NULL;
383         FileData *sel_fd;
384         FileData *cur_fd;
385
386         if (!vfi->layout || !id || !id->fd) return;
387
388         sel_fd = id->fd;
389
390         cur_fd = layout_image_get_fd(vfi->layout);
391         if (sel_fd == cur_fd) return; /* no change */
392
393         if (options->image.enable_read_ahead)
394                 {
395                 gint row;
396
397                 row = g_list_index(vfi->list, id);
398                 if (row > vficon_index_by_fd(vfi, cur_fd) &&
399                     row + 1 < vficon_count(vfi, NULL))
400                         {
401                         read_ahead_fd = vficon_index_get_data(vfi, row + 1);
402                         }
403                 else if (row > 0)
404                         {
405                         read_ahead_fd = vficon_index_get_data(vfi, row - 1);
406                         }
407                 }
408
409         layout_image_set_with_ahead(vfi->layout, sel_fd, read_ahead_fd);
410 }
411
412 static void vficon_toggle_filenames(ViewFileIcon *vfi)
413 {
414         vfi->show_text = !vfi->show_text;
415         options->show_icon_names = vfi->show_text;
416
417         vficon_populate_at_new_size(vfi, vfi->listview->allocation.width, vfi->listview->allocation.height, TRUE);
418 }
419
420 static gint vficon_get_icon_width(ViewFileIcon *vfi)
421 {
422         gint width;
423
424         if (!vfi->show_text) return options->thumbnails.max_width;
425
426         width = options->thumbnails.max_width + options->thumbnails.max_width / 2;
427         if (width < THUMB_MIN_ICON_WIDTH) width = THUMB_MIN_ICON_WIDTH;
428         if (width > THUMB_MAX_ICON_WIDTH) width = options->thumbnails.max_width;
429
430         return width;
431 }
432
433 /*
434  *-------------------------------------------------------------------
435  * misc utils
436  *-------------------------------------------------------------------
437  */
438
439 static gint vficon_find_position(ViewFileIcon *vfi, IconData *id, gint *row, gint *col)
440 {
441         gint n;
442
443         n = g_list_index(vfi->list, id);
444
445         if (n < 0) return FALSE;
446
447         *row = n / vfi->columns;
448         *col = n - (*row * vfi->columns);
449
450         return TRUE;
451 }
452
453 static gint vficon_find_iter(ViewFileIcon *vfi, IconData *id, GtkTreeIter *iter, gint *column)
454 {
455         GtkTreeModel *store;
456         gint row, col;
457
458         store = gtk_tree_view_get_model(GTK_TREE_VIEW(vfi->listview));
459         if (!vficon_find_position(vfi, id, &row, &col)) return FALSE;
460         if (!gtk_tree_model_iter_nth_child(store, iter, NULL, row)) return FALSE;
461         if (column) *column = col;
462
463         return TRUE;
464 }
465
466 static IconData *vficon_find_data(ViewFileIcon *vfi, gint row, gint col, GtkTreeIter *iter)
467 {
468         GtkTreeModel *store;
469         GtkTreeIter p;
470
471         if (row < 0 || col < 0) return NULL;
472
473         store = gtk_tree_view_get_model(GTK_TREE_VIEW(vfi->listview));
474         if (gtk_tree_model_iter_nth_child(store, &p, NULL, row))
475                 {
476                 GList *list;
477
478                 gtk_tree_model_get(store, &p, FILE_COLUMN_POINTER, &list, -1);
479                 if (!list) return NULL;
480
481                 if (iter) *iter = p;
482
483                 return g_list_nth_data(list, col);
484                 }
485
486         return NULL;
487 }
488
489 static IconData *vficon_find_data_by_coord(ViewFileIcon *vfi, gint x, gint y, GtkTreeIter *iter)
490 {
491         GtkTreePath *tpath;
492         GtkTreeViewColumn *column;
493
494         if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(vfi->listview), x, y,
495                                           &tpath, &column, NULL, NULL))
496                 {
497                 GtkTreeModel *store;
498                 GtkTreeIter row;
499                 GList *list;
500                 gint n;
501
502                 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vfi->listview));
503                 gtk_tree_model_get_iter(store, &row, tpath);
504                 gtk_tree_path_free(tpath);
505
506                 gtk_tree_model_get(store, &row, FILE_COLUMN_POINTER, &list, -1);
507
508                 n = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(column), "column_number"));
509                 if (list)
510                         {
511                         if (iter) *iter = row;
512                         return g_list_nth_data(list, n);
513                         }
514                 }
515
516         return NULL;
517 }
518
519 /*
520  *-------------------------------------------------------------------
521  * tooltip type window
522  *-------------------------------------------------------------------
523  */
524
525 static void tip_show(ViewFileIcon *vfi)
526 {
527         GtkWidget *label;
528         gint x, y;
529
530         if (vfi->tip_window) return;
531
532         gdk_window_get_pointer(gtk_tree_view_get_bin_window(GTK_TREE_VIEW(vfi->listview)), &x, &y, NULL);
533
534         vfi->tip_id = vficon_find_data_by_coord(vfi, x, y, NULL);
535         if (!vfi->tip_id) return;
536
537         vfi->tip_window = gtk_window_new(GTK_WINDOW_POPUP);
538         gtk_window_set_resizable(GTK_WINDOW(vfi->tip_window), FALSE);
539         gtk_container_set_border_width(GTK_CONTAINER(vfi->tip_window), 2);
540
541         label = gtk_label_new(vfi->tip_id->fd->name);
542
543         g_object_set_data(G_OBJECT(vfi->tip_window), "tip_label", label);
544         gtk_container_add(GTK_CONTAINER(vfi->tip_window), label);
545         gtk_widget_show(label);
546
547         gdk_window_get_pointer(NULL, &x, &y, NULL);
548
549         if (!GTK_WIDGET_REALIZED(vfi->tip_window)) gtk_widget_realize(vfi->tip_window);
550         gtk_window_move(GTK_WINDOW(vfi->tip_window), x + 16, y + 16);
551         gtk_widget_show(vfi->tip_window);
552 }
553
554 static void tip_hide(ViewFileIcon *vfi)
555 {
556         if (vfi->tip_window) gtk_widget_destroy(vfi->tip_window);
557         vfi->tip_window = NULL;
558 }
559
560 static gint tip_schedule_cb(gpointer data)
561 {
562         ViewFileIcon *vfi = data;
563         GtkWidget *window;
564
565         if (vfi->tip_delay_id == -1) return FALSE;
566
567         window = gtk_widget_get_toplevel(vfi->listview);
568
569         if (GTK_WIDGET_SENSITIVE(window) &&
570             GTK_WINDOW(window)->has_focus)
571                 {
572                 tip_show(vfi);
573                 }
574
575         vfi->tip_delay_id = -1;
576         return FALSE;
577 }
578
579 static void tip_schedule(ViewFileIcon *vfi)
580 {
581         tip_hide(vfi);
582
583         if (vfi->tip_delay_id != -1)
584                 {
585                 g_source_remove(vfi->tip_delay_id);
586                 vfi->tip_delay_id = -1;
587                 }
588
589         if (!vfi->show_text)
590                 {
591                 vfi->tip_delay_id = g_timeout_add(VFICON_TIP_DELAY, tip_schedule_cb, vfi);
592                 }
593 }
594
595 static void tip_unschedule(ViewFileIcon *vfi)
596 {
597         tip_hide(vfi);
598
599         if (vfi->tip_delay_id != -1) g_source_remove(vfi->tip_delay_id);
600         vfi->tip_delay_id = -1;
601 }
602
603 static void tip_update(ViewFileIcon *vfi, IconData *id)
604 {
605         if (vfi->tip_window)
606                 {
607                 gint x, y;
608
609                 gdk_window_get_pointer(NULL, &x, &y, NULL);
610                 gtk_window_move(GTK_WINDOW(vfi->tip_window), x + 16, y + 16);
611
612                 if (id != vfi->tip_id)
613                         {
614                         GtkWidget *label;
615
616                         vfi->tip_id = id;
617
618                         if (!vfi->tip_id)
619                                 {
620                                 tip_hide(vfi);
621                                 tip_schedule(vfi);
622                                 return;
623                                 }
624
625                         label = g_object_get_data(G_OBJECT(vfi->tip_window), "tip_label");
626                         gtk_label_set_text(GTK_LABEL(label), vfi->tip_id->fd->name);
627                         }
628                 }
629         else
630                 {
631                 tip_schedule(vfi);
632                 }
633 }
634
635 /*
636  *-------------------------------------------------------------------
637  * dnd
638  *-------------------------------------------------------------------
639  */
640
641 static void vficon_dnd_get(GtkWidget *widget, GdkDragContext *context,
642                            GtkSelectionData *selection_data, guint info,
643                            guint time, gpointer data)
644 {
645         ViewFileIcon *vfi = data;
646         GList *list = NULL;
647         gchar *uri_text = NULL;
648         gint total;
649
650         if (!vfi->click_id) return;
651
652         if (vfi->click_id->selected & SELECTION_SELECTED)
653                 {
654                 list = vficon_selection_get_list(vfi);
655                 }
656         else
657                 {
658                 list = g_list_append(NULL, file_data_ref(vfi->click_id->fd));
659                 }
660
661         if (!list) return;
662         uri_text = uri_text_from_filelist(list, &total, (info == TARGET_TEXT_PLAIN));
663         filelist_free(list);
664
665         DEBUG_1(uri_text);
666
667         gtk_selection_data_set(selection_data, selection_data->target,
668                                8, (guchar *)uri_text, total);
669         g_free(uri_text);
670 }
671
672 static void vficon_dnd_begin(GtkWidget *widget, GdkDragContext *context, gpointer data)
673 {
674         ViewFileIcon *vfi = data;
675
676         tip_unschedule(vfi);
677
678         if (vfi->click_id && vfi->click_id->fd->pixbuf)
679                 {
680                 gint items;
681
682                 if (vfi->click_id->selected & SELECTION_SELECTED)
683                         items = g_list_length(vfi->selection);
684                 else
685                         items = 1;
686
687                 dnd_set_drag_icon(widget, context, vfi->click_id->fd->pixbuf, items);
688                 }
689 }
690
691 static void vficon_dnd_end(GtkWidget *widget, GdkDragContext *context, gpointer data)
692 {
693         ViewFileIcon *vfi = data;
694
695         vficon_selection_remove(vfi, vfi->click_id, SELECTION_PRELIGHT, NULL);
696
697         if (context->action == GDK_ACTION_MOVE)
698                 {
699                 vficon_refresh(vfi);
700                 }
701
702         tip_unschedule(vfi);
703 }
704
705 static void vficon_dnd_init(ViewFileIcon *vfi)
706 {
707         gtk_drag_source_set(vfi->listview, GDK_BUTTON1_MASK | GDK_BUTTON2_MASK,
708                             dnd_file_drag_types, dnd_file_drag_types_count,
709                             GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK);
710         g_signal_connect(G_OBJECT(vfi->listview), "drag_data_get",
711                          G_CALLBACK(vficon_dnd_get), vfi);
712         g_signal_connect(G_OBJECT(vfi->listview), "drag_begin",
713                          G_CALLBACK(vficon_dnd_begin), vfi);
714         g_signal_connect(G_OBJECT(vfi->listview), "drag_end",
715                          G_CALLBACK(vficon_dnd_end), vfi);
716 }
717
718 /*
719  *-------------------------------------------------------------------
720  * cell updates
721  *-------------------------------------------------------------------
722  */
723
724 static void vficon_selection_set(ViewFileIcon *vfi, IconData *id, SelectionType value, GtkTreeIter *iter)
725 {
726         GtkTreeModel *store;
727         GList *list;
728
729         if (!id) return;
730
731
732         if (id->selected == value) return;
733         id->selected = value;
734
735         store = gtk_tree_view_get_model(GTK_TREE_VIEW(vfi->listview));
736         if (iter)
737                 {
738                 gtk_tree_model_get(store, iter, FILE_COLUMN_POINTER, &list, -1);
739                 if (list) gtk_list_store_set(GTK_LIST_STORE(store), iter, FILE_COLUMN_POINTER, list, -1);
740                 }
741         else
742                 {
743                 GtkTreeIter row;
744
745                 if (vficon_find_iter(vfi, id, &row, NULL))
746                         {
747                         gtk_tree_model_get(store, &row, FILE_COLUMN_POINTER, &list, -1);
748                         if (list) gtk_list_store_set(GTK_LIST_STORE(store), &row, FILE_COLUMN_POINTER, list, -1);
749                         }
750                 }
751 }
752
753 static void vficon_selection_add(ViewFileIcon *vfi, IconData *id, SelectionType mask, GtkTreeIter *iter)
754 {
755         if (!id) return;
756
757         vficon_selection_set(vfi, id, id->selected | mask, iter);
758 }
759
760 static void vficon_selection_remove(ViewFileIcon *vfi, IconData *id, SelectionType mask, GtkTreeIter *iter)
761 {
762         if (!id) return;
763
764         vficon_selection_set(vfi, id, id->selected & ~mask, iter);
765 }
766
767 /*
768  *-------------------------------------------------------------------
769  * selections
770  *-------------------------------------------------------------------
771  */
772
773 static void vficon_verify_selections(ViewFileIcon *vfi)
774 {
775         GList *work;
776
777         work = vfi->selection;
778         while (work)
779                 {
780                 IconData *id = work->data;
781                 work = work->next;
782                 if (vficon_index_by_id(vfi, id) < 0)
783                         {
784                         vfi->selection = g_list_remove(vfi->selection, id);
785                         }
786                 }
787 }
788
789 void vficon_select_all(ViewFileIcon *vfi)
790 {
791         GList *work;
792
793         g_list_free(vfi->selection);
794         vfi->selection = NULL;
795
796         work = vfi->list;
797         while (work)
798                 {
799                 IconData *id = work->data;
800                 vfi->selection = g_list_append(vfi->selection, id);
801                 vficon_selection_add(vfi, work->data, SELECTION_SELECTED, NULL);
802                 work = work->next;
803                 }
804
805         vficon_send_update(vfi);
806 }
807
808 void vficon_select_none(ViewFileIcon *vfi)
809 {
810         GList *work;
811
812         work = vfi->selection;
813         while (work)
814                 {
815                 vficon_selection_remove(vfi, work->data, SELECTION_SELECTED, NULL);
816                 work = work->next;
817                 }
818
819         g_list_free(vfi->selection);
820         vfi->selection = NULL;
821
822         vficon_send_update(vfi);
823 }
824
825 static void vficon_select(ViewFileIcon *vfi, IconData *id)
826 {
827         vfi->prev_selection = id;
828
829         if (!id || id->selected & SELECTION_SELECTED) return;
830
831         vfi->selection = g_list_append(vfi->selection, id);
832         vficon_selection_add(vfi, id, SELECTION_SELECTED, NULL);
833
834         vficon_send_update(vfi);
835 }
836
837 static void vficon_unselect(ViewFileIcon *vfi, IconData *id)
838 {
839         vfi->prev_selection = id;
840
841         if (!id || !(id->selected & SELECTION_SELECTED) ) return;
842
843         vfi->selection = g_list_remove(vfi->selection, id);
844         vficon_selection_remove(vfi, id, SELECTION_SELECTED, NULL);
845
846         vficon_send_update(vfi);
847 }
848
849 static void vficon_select_util(ViewFileIcon *vfi, IconData *id, gint select)
850 {
851         if (select)
852                 {
853                 vficon_select(vfi, id);
854                 }
855         else
856                 {
857                 vficon_unselect(vfi, id);
858                 }
859 }
860
861 static void vficon_select_region_util(ViewFileIcon *vfi, IconData *start, IconData *end, gint select)
862 {
863         gint row1, col1;
864         gint row2, col2;
865         gint t;
866         gint i, j;
867
868         if (!vficon_find_position(vfi, start, &row1, &col1) ||
869             !vficon_find_position(vfi, end, &row2, &col2) ) return;
870
871         vfi->prev_selection = end;
872
873         if (!options->collections.rectangular_selection)
874                 {
875                 GList *work;
876                 IconData *id;
877
878                 if (g_list_index(vfi->list, start) > g_list_index(vfi->list, end))
879                         {
880                         id = start;
881                         start = end;
882                         end = id;
883                         }
884
885                 work = g_list_find(vfi->list, start);
886                 while (work)
887                         {
888                         id = work->data;
889                         vficon_select_util(vfi, id, select);
890
891                         if (work->data != end)
892                                 work = work->next;
893                         else
894                                 work = NULL;
895                         }
896                 return;
897                 }
898
899         if (row2 < row1)
900                 {
901                 t = row1;
902                 row1 = row2;
903                 row2 = t;
904                 }
905         if (col2 < col1)
906                 {
907                 t = col1;
908                 col1 = col2;
909                 col2 = t;
910                 }
911
912         DEBUG_1("table: %d x %d to %d x %d\n", row1, col1, row2, col2);
913
914         for (i = row1; i <= row2; i++)
915                 {
916                 for (j = col1; j <= col2; j++)
917                         {
918                         IconData *id = vficon_find_data(vfi, i, j, NULL);
919                         if (id) vficon_select_util(vfi, id, select);
920                         }
921                 }
922 }
923
924 gint vficon_index_is_selected(ViewFileIcon *vfi, gint row)
925 {
926         IconData *id = g_list_nth_data(vfi->list, row);
927
928         if (!id) return FALSE;
929
930         return (id->selected & SELECTION_SELECTED);
931 }
932
933 gint vficon_selection_count(ViewFileIcon *vfi, gint64 *bytes)
934 {
935         if (bytes)
936                 {
937                 gint64 b = 0;
938                 GList *work;
939
940                 work = vfi->selection;
941                 while (work)
942                         {
943                         IconData *id = work->data;
944                         FileData *fd = id->fd;
945                         g_assert(fd->magick == 0x12345678);
946                         b += fd->size;
947
948                         work = work->next;
949                         }
950
951                 *bytes = b;
952                 }
953
954         return g_list_length(vfi->selection);
955 }
956
957 GList *vficon_selection_get_list(ViewFileIcon *vfi)
958 {
959         GList *list = NULL;
960         GList *work;
961
962         work = vfi->selection;
963         while (work)
964                 {
965                 IconData *id = work->data;
966                 FileData *fd = id->fd;
967                 g_assert(fd->magick == 0x12345678);
968
969                 list = g_list_prepend(list, file_data_ref(fd));
970
971                 work = work->next;
972                 }
973
974         list = g_list_reverse(list);
975
976         return list;
977 }
978
979 GList *vficon_selection_get_list_by_index(ViewFileIcon *vfi)
980 {
981         GList *list = NULL;
982         GList *work;
983
984         work = vfi->selection;
985         while (work)
986                 {
987                 list = g_list_prepend(list, GINT_TO_POINTER(g_list_index(vfi->list, work->data)));
988                 work = work->next;
989                 }
990
991         return g_list_reverse(list);
992 }
993
994 static void vficon_select_by_id(ViewFileIcon *vfi, IconData *id)
995 {
996         if (!id) return;
997
998         if (!(id->selected & SELECTION_SELECTED))
999                 {
1000                 vficon_select_none(vfi);
1001                 vficon_select(vfi, id);
1002                 }
1003
1004         vficon_set_focus(vfi, id);
1005 }
1006
1007
1008 void vficon_select_by_path(ViewFileIcon *vfi, const gchar *path)
1009 {
1010         IconData *id = NULL;
1011         GList *work;
1012
1013         if (!path) return;
1014
1015         work = vfi->list;
1016         while (work && !id)
1017                 {
1018                 IconData *chk = work->data;
1019                 work = work->next;
1020                 if (strcmp(chk->fd->path, path) == 0) id = chk;
1021                 }
1022         vficon_select_by_id(vfi, id);
1023 }
1024
1025 void vficon_select_by_fd(ViewFileIcon *vfi, FileData *fd)
1026 {
1027         IconData *id = NULL;
1028         GList *work;
1029
1030         if (!fd) return;
1031         work = vfi->list;
1032         while (work && !id)
1033                 {
1034                 IconData *chk = work->data;
1035                 work = work->next;
1036                 if (chk->fd == fd) id = chk;
1037                 }
1038         vficon_select_by_id(vfi, id);
1039 }
1040
1041 void vficon_mark_to_selection(ViewFileIcon *vfi, gint mark, MarkToSelectionMode mode)
1042 {
1043         GList *work;
1044
1045         work = vfi->list;
1046         while (work)
1047                 {
1048                 IconData *id = work->data;
1049                 FileData *fd = id->fd;
1050                 gboolean mark_val, selected;
1051
1052                 g_assert(fd->magick == 0x12345678);
1053
1054                 mark_val = fd->marks[mark];
1055                 selected = (id->selected & SELECTION_SELECTED);
1056
1057                 switch (mode)
1058                         {
1059                         case MTS_MODE_SET: selected = mark_val;
1060                                 break;
1061                         case MTS_MODE_OR: selected = mark_val | selected;
1062                                 break;
1063                         case MTS_MODE_AND: selected = mark_val & selected;
1064                                 break;
1065                         case MTS_MODE_MINUS: selected = !mark_val & selected;
1066                                 break;
1067                         }
1068
1069                 vficon_select_util(vfi, id, selected);
1070
1071                 work = work->next;
1072                 }
1073 }
1074
1075 void vficon_selection_to_mark(ViewFileIcon *vfi, gint mark, SelectionToMarkMode mode)
1076 {
1077         GList *slist;
1078         GList *work;
1079
1080         g_assert(mark >= 0 && mark < FILEDATA_MARKS_SIZE);
1081
1082         slist = vficon_selection_get_list(vfi);
1083         work = slist;
1084         while (work)
1085                 {
1086                 FileData *fd = work->data;
1087
1088                 switch (mode)
1089                         {
1090                         case STM_MODE_SET: fd->marks[mark] = 1;
1091                                 break;
1092                         case STM_MODE_RESET: fd->marks[mark] = 0;
1093                                 break;
1094                         case STM_MODE_TOGGLE: fd->marks[mark] = !fd->marks[mark];
1095                                 break;
1096                         }
1097
1098                 work = work->next;
1099                 }
1100         filelist_free(slist);
1101 }
1102
1103
1104 /*
1105  *-------------------------------------------------------------------
1106  * focus
1107  *-------------------------------------------------------------------
1108  */
1109
1110 static void vficon_move_focus(ViewFileIcon *vfi, gint row, gint col, gint relative)
1111 {
1112         gint new_row;
1113         gint new_col;
1114
1115         if (relative)
1116                 {
1117                 new_row = vfi->focus_row;
1118                 new_col = vfi->focus_column;
1119
1120                 new_row += row;
1121                 if (new_row < 0) new_row = 0;
1122                 if (new_row >= vfi->rows) new_row = vfi->rows - 1;
1123
1124                 while(col != 0)
1125                         {
1126                         if (col < 0)
1127                                 {
1128                                 new_col--;
1129                                 col++;
1130                                 }
1131                         else
1132                                 {
1133                                 new_col++;
1134                                 col--;
1135                                 }
1136
1137                         if (new_col < 0)
1138                                 {
1139                                 if (new_row > 0)
1140                                         {
1141                                         new_row--;
1142                                         new_col = vfi->columns - 1;
1143                                         }
1144                                 else
1145                                         {
1146                                         new_col = 0;
1147                                         }
1148                                 }
1149                         if (new_col >= vfi->columns)
1150                                 {
1151                                 if (new_row < vfi->rows - 1)
1152                                         {
1153                                         new_row++;
1154                                         new_col = 0;
1155                                         }
1156                                 else
1157                                         {
1158                                         new_col = vfi->columns - 1;
1159                                         }
1160                                 }
1161                         }
1162                 }
1163         else
1164                 {
1165                 new_row = row;
1166                 new_col = col;
1167
1168                 if (new_row >= vfi->rows)
1169                         {
1170                         if (vfi->rows > 0)
1171                                 new_row = vfi->rows - 1;
1172                         else
1173                                 new_row = 0;
1174                         new_col = vfi->columns - 1;
1175                         }
1176                 if (new_col >= vfi->columns) new_col = vfi->columns - 1;
1177                 }
1178
1179         if (new_row == vfi->rows - 1)
1180                 {
1181                 gint l;
1182
1183                 /* if we moved beyond the last image, go to the last image */
1184
1185                 l = g_list_length(vfi->list);
1186                 if (vfi->rows > 1) l -= (vfi->rows - 1) * vfi->columns;
1187                 if (new_col >= l) new_col = l - 1;
1188                 }
1189
1190         vficon_set_focus(vfi, vficon_find_data(vfi, new_row, new_col, NULL));
1191 }
1192
1193 static void vficon_set_focus(ViewFileIcon *vfi, IconData *id)
1194 {
1195         GtkTreeIter iter;
1196         gint row, col;
1197
1198         if (g_list_find(vfi->list, vfi->focus_id))
1199                 {
1200                 if (id == vfi->focus_id)
1201                         {
1202                         /* ensure focus row col are correct */
1203                         vficon_find_position(vfi, vfi->focus_id, &vfi->focus_row, &vfi->focus_column);
1204                         return;
1205                         }
1206                 vficon_selection_remove(vfi, vfi->focus_id, SELECTION_FOCUS, NULL);
1207                 }
1208
1209         if (!vficon_find_position(vfi, id, &row, &col))
1210                 {
1211                 vfi->focus_id = NULL;
1212                 vfi->focus_row = -1;
1213                 vfi->focus_column = -1;
1214                 return;
1215                 }
1216
1217         vfi->focus_id = id;
1218         vfi->focus_row = row;
1219         vfi->focus_column = col;
1220         vficon_selection_add(vfi, vfi->focus_id, SELECTION_FOCUS, NULL);
1221
1222         if (vficon_find_iter(vfi, vfi->focus_id, &iter, NULL))
1223                 {
1224                 GtkTreePath *tpath;
1225                 GtkTreeViewColumn *column;
1226                 GtkTreeModel *store;
1227
1228                 tree_view_row_make_visible(GTK_TREE_VIEW(vfi->listview), &iter, FALSE);
1229
1230                 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vfi->listview));
1231                 tpath = gtk_tree_model_get_path(store, &iter);
1232                 /* focus is set to an extra column with 0 width to hide focus, we draw it ourself */
1233                 column = gtk_tree_view_get_column(GTK_TREE_VIEW(vfi->listview), VFICON_MAX_COLUMNS);
1234                 gtk_tree_view_set_cursor(GTK_TREE_VIEW(vfi->listview), tpath, column, FALSE);
1235                 gtk_tree_path_free(tpath);
1236                 }
1237 }
1238
1239 static void vficon_update_focus(ViewFileIcon *vfi)
1240 {
1241         gint new_row = 0;
1242         gint new_col = 0;
1243
1244         if (vfi->focus_id && vficon_find_position(vfi, vfi->focus_id, &new_row, &new_col))
1245                 {
1246                 /* first find the old focus, if it exists and is valid */
1247                 }
1248         else
1249                 {
1250                 /* (try to) stay where we were */
1251                 new_row = vfi->focus_row;
1252                 new_col = vfi->focus_column;
1253                 }
1254
1255         vficon_move_focus(vfi, new_row, new_col, FALSE);
1256 }
1257
1258 /* used to figure the page up/down distances */
1259 static gint page_height(ViewFileIcon *vfi)
1260 {
1261         GtkAdjustment *adj;
1262         gint page_size;
1263         gint row_height;
1264         gint ret;
1265
1266         adj = gtk_tree_view_get_vadjustment(GTK_TREE_VIEW(vfi->listview));
1267         page_size = (gint)adj->page_increment;
1268
1269         row_height = options->thumbnails.max_height + THUMB_BORDER_PADDING * 2;
1270         if (vfi->show_text) row_height += options->thumbnails.max_height / 3;
1271
1272         ret = page_size / row_height;
1273         if (ret < 1) ret = 1;
1274
1275         return ret;
1276 }
1277
1278 /*
1279  *-------------------------------------------------------------------
1280  * keyboard
1281  *-------------------------------------------------------------------
1282  */
1283
1284 static void vfi_menu_position_cb(GtkMenu *menu, gint *x, gint *y, gboolean *push_in, gpointer data)
1285 {
1286         ViewFileIcon *vfi = data;
1287         GtkTreeModel *store;
1288         GtkTreeIter iter;
1289         gint column;
1290         GtkTreePath *tpath;
1291         gint cw, ch;
1292
1293         if (!vficon_find_iter(vfi, vfi->click_id, &iter, &column)) return;
1294         store = gtk_tree_view_get_model(GTK_TREE_VIEW(vfi->listview));
1295         tpath = gtk_tree_model_get_path(store, &iter);
1296         tree_view_get_cell_clamped(GTK_TREE_VIEW(vfi->listview), tpath, column, FALSE, x, y, &cw, &ch);
1297         gtk_tree_path_free(tpath);
1298         *y += ch;
1299         popup_menu_position_clamp(menu, x, y, 0);
1300 }
1301
1302 static gint vficon_press_key_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
1303 {
1304         ViewFileIcon *vfi = data;
1305         gint focus_row = 0;
1306         gint focus_col = 0;
1307         IconData *id;
1308         gint stop_signal;
1309
1310         stop_signal = TRUE;
1311         switch (event->keyval)
1312                 {
1313                 case GDK_Left: case GDK_KP_Left:
1314                         focus_col = -1;
1315                         break;
1316                 case GDK_Right: case GDK_KP_Right:
1317                         focus_col = 1;
1318                         break;
1319                 case GDK_Up: case GDK_KP_Up:
1320                         focus_row = -1;
1321                         break;
1322                 case GDK_Down: case GDK_KP_Down:
1323                         focus_row = 1;
1324                         break;
1325                 case GDK_Page_Up: case GDK_KP_Page_Up:
1326                         focus_row = -page_height(vfi);
1327                         break;
1328                 case GDK_Page_Down: case GDK_KP_Page_Down:
1329                         focus_row = page_height(vfi);
1330                         break;
1331                 case GDK_Home: case GDK_KP_Home:
1332                         focus_row = -vfi->focus_row;
1333                         focus_col = -vfi->focus_column;
1334                         break;
1335                 case GDK_End: case GDK_KP_End:
1336                         focus_row = vfi->rows - 1 - vfi->focus_row;
1337                         focus_col = vfi->columns - 1 - vfi->focus_column;
1338                         break;
1339                 case GDK_space:
1340                         id = vficon_find_data(vfi, vfi->focus_row, vfi->focus_column, NULL);
1341                         if (id)
1342                                 {
1343                                 vfi->click_id = id;
1344                                 if (event->state & GDK_CONTROL_MASK)
1345                                         {
1346                                         gint selected;
1347
1348                                         selected = id->selected & SELECTION_SELECTED;
1349                                         if (selected)
1350                                                 {
1351                                                 vficon_unselect(vfi, id);
1352                                                 }
1353                                         else
1354                                                 {
1355                                                 vficon_select(vfi, id);
1356                                                 vficon_send_layout_select(vfi, id);
1357                                                 }
1358                                         }
1359                                 else
1360                                         {
1361                                         vficon_select_none(vfi);
1362                                         vficon_select(vfi, id);
1363                                         vficon_send_layout_select(vfi, id);
1364                                         }
1365                                 }
1366                         break;
1367                 case GDK_Menu:
1368                         id = vficon_find_data(vfi, vfi->focus_row, vfi->focus_column, NULL);
1369                         vfi->click_id = id;
1370
1371                         vficon_selection_add(vfi, vfi->click_id, SELECTION_PRELIGHT, NULL);
1372                         tip_unschedule(vfi);
1373
1374                         vfi->popup = vficon_pop_menu(vfi, (id != NULL));
1375                         gtk_menu_popup(GTK_MENU(vfi->popup), NULL, NULL, vfi_menu_position_cb, vfi, 0, GDK_CURRENT_TIME);
1376                         break;
1377                 default:
1378                         stop_signal = FALSE;
1379                         break;
1380                 }
1381
1382         if (focus_row != 0 || focus_col != 0)
1383                 {
1384                 IconData *new_id;
1385                 IconData *old_id;
1386
1387                 old_id = vficon_find_data(vfi, vfi->focus_row, vfi->focus_column, NULL);
1388                 vficon_move_focus(vfi, focus_row, focus_col, TRUE);
1389                 new_id = vficon_find_data(vfi, vfi->focus_row, vfi->focus_column, NULL);
1390
1391                 if (new_id != old_id)
1392                         {
1393                         if (event->state & GDK_SHIFT_MASK)
1394                                 {
1395                                 if (!options->collections.rectangular_selection)
1396                                         {
1397                                         vficon_select_region_util(vfi, old_id, new_id, FALSE);
1398                                         }
1399                                 else
1400                                         {
1401                                         vficon_select_region_util(vfi, vfi->click_id, old_id, FALSE);
1402                                         }
1403                                 vficon_select_region_util(vfi, vfi->click_id, new_id, TRUE);
1404                                 vficon_send_layout_select(vfi, new_id);
1405                                 }
1406                         else if (event->state & GDK_CONTROL_MASK)
1407                                 {
1408                                 vfi->click_id = new_id;
1409                                 }
1410                         else
1411                                 {
1412                                 vfi->click_id = new_id;
1413                                 vficon_select_none(vfi);
1414                                 vficon_select(vfi, new_id);
1415                                 vficon_send_layout_select(vfi, new_id);
1416                                 }
1417                         }
1418                 }
1419
1420         if (stop_signal)
1421                 {
1422 #if 0
1423                 g_signal_stop_emission_by_name(GTK_OBJECT(widget), "key_press_event");
1424 #endif
1425                 tip_unschedule(vfi);
1426                 }
1427
1428         return stop_signal;
1429 }
1430
1431 /*
1432  *-------------------------------------------------------------------
1433  * mouse
1434  *-------------------------------------------------------------------
1435  */
1436
1437 static gint vficon_motion_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1438 {
1439         ViewFileIcon *vfi = data;
1440         IconData *id;
1441
1442         id = vficon_find_data_by_coord(vfi, (gint)bevent->x, (gint)bevent->y, NULL);
1443         tip_update(vfi, id);
1444
1445         return FALSE;
1446 }
1447
1448 static gint vficon_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1449 {
1450         ViewFileIcon *vfi = data;
1451         GtkTreeIter iter;
1452         IconData *id;
1453
1454         tip_unschedule(vfi);
1455
1456         id = vficon_find_data_by_coord(vfi, (gint)bevent->x, (gint)bevent->y, &iter);
1457
1458         vfi->click_id = id;
1459         vficon_selection_add(vfi, vfi->click_id, SELECTION_PRELIGHT, &iter);
1460
1461         switch (bevent->button)
1462                 {
1463                 case MOUSE_BUTTON_LEFT:
1464                         if (!GTK_WIDGET_HAS_FOCUS(vfi->listview))
1465                                 {
1466                                 gtk_widget_grab_focus(vfi->listview);
1467                                 }
1468 #if 0
1469                         if (bevent->type == GDK_2BUTTON_PRESS &&
1470                             vfi->layout)
1471                                 {
1472                                 vficon_selection_remove(vfi, vfi->click_id, SELECTION_PRELIGHT, &iter);
1473                                 layout_image_full_screen_start(vfi->layout);
1474                                 }
1475 #endif
1476                         break;
1477                 case MOUSE_BUTTON_RIGHT:
1478                         vfi->popup = vficon_pop_menu(vfi, (id != NULL));
1479                         gtk_menu_popup(GTK_MENU(vfi->popup), NULL, NULL, NULL, NULL, bevent->button, bevent->time);
1480                         break;
1481                 default:
1482                         break;
1483                 }
1484
1485         return TRUE;
1486 }
1487
1488 static gint vficon_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1489 {
1490         ViewFileIcon *vfi = data;
1491         GtkTreeIter iter;
1492         IconData *id = NULL;
1493         gint was_selected = FALSE;
1494
1495         tip_schedule(vfi);
1496
1497         if ((gint)bevent->x != 0 || (gint) bevent->y != 0)
1498                 {
1499                 id = vficon_find_data_by_coord(vfi, (gint)bevent->x, (gint)bevent->y, &iter);
1500                 }
1501
1502         if (vfi->click_id)
1503                 {
1504                 vficon_selection_remove(vfi, vfi->click_id, SELECTION_PRELIGHT, NULL);
1505                 }
1506
1507         if (id) was_selected = (id->selected & SELECTION_SELECTED);
1508
1509         if (bevent->button == MOUSE_BUTTON_LEFT &&
1510             id && vfi->click_id == id)
1511                 {
1512                 vficon_set_focus(vfi, id);
1513
1514                 if (bevent->state & GDK_CONTROL_MASK)
1515                         {
1516                         gint select;
1517
1518                         select = !(id->selected & SELECTION_SELECTED);
1519                         if ((bevent->state & GDK_SHIFT_MASK) && vfi->prev_selection)
1520                                 {
1521                                 vficon_select_region_util(vfi, vfi->prev_selection, id, select);
1522                                 }
1523                         else
1524                                 {
1525                                 vficon_select_util(vfi, id, select);
1526                                 }
1527                         }
1528                 else
1529                         {
1530                         vficon_select_none(vfi);
1531
1532                         if ((bevent->state & GDK_SHIFT_MASK) &&
1533                             vfi->prev_selection)
1534                                 {
1535                                 vficon_select_region_util(vfi, vfi->prev_selection, id, TRUE);
1536                                 }
1537                         else
1538                                 {
1539                                 vficon_select_util(vfi, id, TRUE);
1540                                 was_selected = FALSE;
1541                                 }
1542                         }
1543                 }
1544         else if (bevent->button == MOUSE_BUTTON_MIDDLE &&
1545                  id && vfi->click_id == id)
1546                 {
1547                 vficon_select_util(vfi, id, !(id->selected & SELECTION_SELECTED));
1548                 }
1549
1550         if (id && !was_selected &&
1551             (id->selected & SELECTION_SELECTED))
1552                 {
1553                 vficon_send_layout_select(vfi, id);
1554                 }
1555
1556         return TRUE;
1557 }
1558
1559 static gint vficon_leave_cb(GtkWidget *widget, GdkEventCrossing *event, gpointer data)
1560 {
1561         ViewFileIcon *vfi = data;
1562
1563         tip_unschedule(vfi);
1564         return FALSE;
1565 }
1566
1567 /*
1568  *-------------------------------------------------------------------
1569  * population
1570  *-------------------------------------------------------------------
1571  */
1572
1573 static gboolean vficon_destroy_node_cb(GtkTreeModel *store, GtkTreePath *tpath, GtkTreeIter *iter, gpointer data)
1574 {
1575         GList *list;
1576
1577         gtk_tree_model_get(store, iter, FILE_COLUMN_POINTER, &list, -1);
1578         g_list_free(list);
1579
1580         return FALSE;
1581 }
1582
1583 static void vficon_clear_store(ViewFileIcon *vfi)
1584 {
1585         GtkTreeModel *store;
1586
1587         store = gtk_tree_view_get_model(GTK_TREE_VIEW(vfi->listview));
1588         gtk_tree_model_foreach(store, vficon_destroy_node_cb, NULL);
1589
1590         gtk_list_store_clear(GTK_LIST_STORE(store));
1591 }
1592
1593 static void vficon_set_thumb(ViewFileIcon *vfi, FileData *fd, GdkPixbuf *pb)
1594 {
1595         GtkTreeModel *store;
1596         GtkTreeIter iter;
1597         GList *list;
1598
1599         if (!vficon_find_iter(vfi, vficon_icon_data(vfi, fd), &iter, NULL)) return;
1600
1601         store = gtk_tree_view_get_model(GTK_TREE_VIEW(vfi->listview));
1602
1603         if (pb) g_object_ref(pb);
1604         if (fd->pixbuf) g_object_unref(fd->pixbuf);
1605         fd->pixbuf = pb;
1606
1607         gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &list, -1);
1608         gtk_list_store_set(GTK_LIST_STORE(store), &iter, FILE_COLUMN_POINTER, list, -1);
1609 }
1610
1611 static GList *vficon_add_row(ViewFileIcon *vfi, GtkTreeIter *iter)
1612 {
1613         GtkListStore *store;
1614         GList *list = NULL;
1615         gint i;
1616
1617         for (i = 0; i < vfi->columns; i++) list = g_list_prepend(list, NULL);
1618
1619         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(vfi->listview)));
1620         gtk_list_store_append(store, iter);
1621         gtk_list_store_set(store, iter, FILE_COLUMN_POINTER, list, -1);
1622
1623         return list;
1624 }
1625
1626 static void vficon_populate(ViewFileIcon *vfi, gint resize, gint keep_position)
1627 {
1628         GtkTreeModel *store;
1629         GtkTreePath *tpath;
1630         gint row;
1631         GList *work;
1632         IconData *visible_id = NULL;
1633
1634         vficon_verify_selections(vfi);
1635
1636         store = gtk_tree_view_get_model(GTK_TREE_VIEW(vfi->listview));
1637
1638         if (keep_position && GTK_WIDGET_REALIZED(vfi->listview) &&
1639             gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(vfi->listview), 0, 0, &tpath, NULL, NULL, NULL))
1640                 {
1641                 GtkTreeIter iter;
1642                 GList *list;
1643
1644                 gtk_tree_model_get_iter(store, &iter, tpath);
1645                 gtk_tree_path_free(tpath);
1646
1647                 gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &list, -1);
1648                 if (list) visible_id = list->data;
1649                 }
1650
1651         vficon_clear_store(vfi);
1652
1653         if (resize)
1654                 {
1655                 gint i;
1656                 gint thumb_width;
1657
1658                 thumb_width = vficon_get_icon_width(vfi);
1659
1660                 for (i = 0; i < VFICON_MAX_COLUMNS; i++)
1661                         {
1662                         GtkTreeViewColumn *column;
1663                         GtkCellRenderer *cell;
1664                         GList *list;
1665
1666                         column = gtk_tree_view_get_column(GTK_TREE_VIEW(vfi->listview), i);
1667                         gtk_tree_view_column_set_visible(column, (i < vfi->columns));
1668                         gtk_tree_view_column_set_fixed_width(column, thumb_width + (THUMB_BORDER_PADDING * 6));
1669
1670                         list = gtk_tree_view_column_get_cell_renderers(column);
1671                         cell = (list) ? list->data : NULL;
1672                         g_list_free(list);
1673
1674                         if (cell && GQV_IS_CELL_RENDERER_ICON(cell))
1675                                 {
1676                                 g_object_set(G_OBJECT(cell), "fixed_width", thumb_width,
1677                                                              "fixed_height", options->thumbnails.max_height,
1678                                                              "show_text", vfi->show_text, NULL);
1679                                 }
1680                         }
1681                 if (GTK_WIDGET_REALIZED(vfi->listview)) gtk_tree_view_columns_autosize(GTK_TREE_VIEW(vfi->listview));
1682                 }
1683
1684         row = -1;
1685         work = vfi->list;
1686         while (work)
1687                 {
1688                 GList *list;
1689                 GtkTreeIter iter;
1690
1691                 row++;
1692
1693                 list = vficon_add_row(vfi, &iter);
1694                 while (work && list)
1695                         {
1696                         IconData *id;
1697
1698                         id = work->data;
1699                         id->row = row;
1700
1701                         list->data = work->data;
1702                         list = list->next;
1703                         work = work->next;
1704                         }
1705                 }
1706
1707         if (visible_id &&
1708             gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(vfi->listview), 0, 0, &tpath, NULL, NULL, NULL))
1709                 {
1710                 GtkTreeIter iter;
1711                 GList *list;
1712
1713                 gtk_tree_model_get_iter(store, &iter, tpath);
1714                 gtk_tree_path_free(tpath);
1715
1716                 gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &list, -1);
1717                 if (g_list_find(list, visible_id) == NULL &&
1718                     vficon_find_iter(vfi, visible_id, &iter, NULL))
1719                         {
1720                         tree_view_row_make_visible(GTK_TREE_VIEW(vfi->listview), &iter, FALSE);
1721                         }
1722                 }
1723
1724         vfi->rows = row + 1;
1725
1726         vficon_send_update(vfi);
1727         vficon_thumb_update(vfi);
1728 }
1729
1730 static void vficon_populate_at_new_size(ViewFileIcon *vfi, gint w, gint h, gint force)
1731 {
1732         gint new_cols;
1733         gint thumb_width;
1734
1735         thumb_width = vficon_get_icon_width(vfi);
1736
1737         new_cols = w / (thumb_width + (THUMB_BORDER_PADDING * 6));
1738         if (new_cols < 1) new_cols = 1;
1739
1740         if (!force && new_cols == vfi->columns) return;
1741
1742         vfi->columns = new_cols;
1743
1744         vficon_populate(vfi, TRUE, TRUE);
1745
1746         DEBUG_1("col tab pop cols=%d rows=%d\n", vfi->columns, vfi->rows);
1747 }
1748
1749 static void vficon_sync(ViewFileIcon *vfi)
1750 {
1751         GtkTreeModel *store;
1752         GtkTreeIter iter;
1753         GList *work;
1754         gint r, c;
1755
1756         if (vfi->rows == 0) return;
1757
1758         store = gtk_tree_view_get_model(GTK_TREE_VIEW(vfi->listview));
1759
1760         r = -1;
1761         c = 0;
1762
1763         work = vfi->list;
1764         while (work)
1765                 {
1766                 GList *list;
1767                 r++;
1768                 c = 0;
1769                 if (gtk_tree_model_iter_nth_child(store, &iter, NULL, r))
1770                         {
1771                         gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &list, -1);
1772                         gtk_list_store_set(GTK_LIST_STORE(store), &iter, FILE_COLUMN_POINTER, list, -1);
1773                         }
1774                 else
1775                         {
1776                         list = vficon_add_row(vfi, &iter);
1777                         }
1778
1779                 while (list)
1780                         {
1781                         IconData *id;
1782
1783                         if (work)
1784                                 {
1785                                 id = work->data;
1786                                 work = work->next;
1787                                 c++;
1788
1789                                 id->row = r;
1790                                 }
1791                         else
1792                                 {
1793                                 id = NULL;
1794                                 }
1795
1796                         list->data = id;
1797                         list = list->next;
1798                         }
1799                 }
1800
1801         r++;
1802         while (gtk_tree_model_iter_nth_child(store, &iter, NULL, r))
1803                 {
1804                 GList *list;
1805
1806                 gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &list, -1);
1807                 gtk_list_store_remove(GTK_LIST_STORE(store), &iter);
1808                 g_list_free(list);
1809                 }
1810
1811         vfi->rows = r;
1812
1813         vficon_update_focus(vfi);
1814 }
1815
1816 static gint vficon_sync_idle_cb(gpointer data)
1817 {
1818         ViewFileIcon *vfi = data;
1819
1820         if (vfi->sync_idle_id == -1) return FALSE;
1821         vfi->sync_idle_id = -1;
1822
1823         vficon_sync(vfi);
1824         return FALSE;
1825 }
1826
1827 static void vficon_sync_idle(ViewFileIcon *vfi)
1828 {
1829         if (vfi->sync_idle_id == -1)
1830                 {
1831                 /* high priority, the view needs to be resynced before a redraw
1832                  * may contain invalid pointers at this time
1833                  */
1834                 vfi->sync_idle_id = g_idle_add_full(G_PRIORITY_HIGH, vficon_sync_idle_cb, vfi, NULL);
1835                 }
1836 }
1837
1838 static void vficon_sized_cb(GtkWidget *widget, GtkAllocation *allocation, gpointer data)
1839 {
1840         ViewFileIcon *vfi = data;
1841
1842         vficon_populate_at_new_size(vfi, allocation->width, allocation->height, FALSE);
1843 }
1844
1845 /*
1846  *-----------------------------------------------------------------------------
1847  * misc
1848  *-----------------------------------------------------------------------------
1849  */
1850
1851 void vficon_sort_set(ViewFileIcon *vfi, SortType type, gint ascend)
1852 {
1853         if (vfi->sort_method == type && vfi->sort_ascend == ascend) return;
1854
1855         vfi->sort_method = type;
1856         vfi->sort_ascend = ascend;
1857
1858         if (!vfi->list) return;
1859
1860         vfi->list = iconlist_sort(vfi->list, vfi->sort_method, vfi->sort_ascend);
1861         vficon_sync(vfi);
1862 }
1863
1864 /*
1865  *-----------------------------------------------------------------------------
1866  * thumb updates
1867  *-----------------------------------------------------------------------------
1868  */
1869
1870 static gint vficon_thumb_next(ViewFileIcon *vfi);
1871
1872 static void vficon_thumb_status(ViewFileIcon *vfi, gdouble val, const gchar *text)
1873 {
1874         if (vfi->func_thumb_status)
1875                 {
1876                 vfi->func_thumb_status(vfi, val, text, vfi->data_thumb_status);
1877                 }
1878 }
1879
1880 static void vficon_thumb_cleanup(ViewFileIcon *vfi)
1881 {
1882         vficon_thumb_status(vfi, 0.0, NULL);
1883
1884         g_list_free(vfi->thumbs_list);
1885         vfi->thumbs_list = NULL;
1886         vfi->thumbs_count = 0;
1887         vfi->thumbs_running = FALSE;
1888
1889         thumb_loader_free(vfi->thumbs_loader);
1890         vfi->thumbs_loader = NULL;
1891
1892         vfi->thumbs_fd = NULL;
1893 }
1894
1895 static void vficon_thumb_stop(ViewFileIcon *vfi)
1896 {
1897         if (vfi->thumbs_running) vficon_thumb_cleanup(vfi);
1898 }
1899
1900 static void vficon_thumb_do(ViewFileIcon *vfi, ThumbLoader *tl, FileData *fd)
1901 {
1902         GdkPixbuf *pixbuf;
1903
1904         if (!fd) return;
1905
1906         pixbuf = thumb_loader_get_pixbuf(tl, TRUE);
1907         vficon_set_thumb(vfi, fd, pixbuf);
1908         g_object_unref(pixbuf);
1909
1910         vficon_thumb_status(vfi, (gdouble)(vfi->thumbs_count) / g_list_length(vfi->list), _("Loading thumbs..."));
1911 }
1912
1913 static void vficon_thumb_error_cb(ThumbLoader *tl, gpointer data)
1914 {
1915         ViewFileIcon *vfi = data;
1916
1917         if (vfi->thumbs_fd && vfi->thumbs_loader == tl)
1918                 {
1919                 vficon_thumb_do(vfi, tl, vfi->thumbs_fd);
1920                 }
1921
1922         while (vficon_thumb_next(vfi));
1923 }
1924
1925 static void vficon_thumb_done_cb(ThumbLoader *tl, gpointer data)
1926 {
1927         ViewFileIcon *vfi = data;
1928
1929         if (vfi->thumbs_fd && vfi->thumbs_loader == tl)
1930                 {
1931                 vficon_thumb_do(vfi, tl, vfi->thumbs_fd);
1932                 }
1933
1934         while (vficon_thumb_next(vfi));
1935 }
1936
1937 static gint vficon_thumb_next(ViewFileIcon *vfi)
1938 {
1939         GtkTreePath *tpath;
1940         FileData *fd = NULL;
1941
1942         if (!GTK_WIDGET_REALIZED(vfi->listview))
1943                 {
1944                 vficon_thumb_status(vfi, 0.0, NULL);
1945                 return FALSE;
1946                 }
1947
1948         if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(vfi->listview), 0, 0, &tpath, NULL, NULL, NULL))
1949                 {
1950                 GtkTreeModel *store;
1951                 GtkTreeIter iter;
1952                 gint valid = TRUE;
1953
1954                 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vfi->listview));
1955                 gtk_tree_model_get_iter(store, &iter, tpath);
1956                 gtk_tree_path_free(tpath);
1957
1958                 while (!fd && valid && tree_view_row_get_visibility(GTK_TREE_VIEW(vfi->listview), &iter, FALSE) == 0)
1959                         {
1960                         GList *list;
1961
1962                         gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &list, -1);
1963
1964                         while (!fd && list)
1965                                 {
1966                                 IconData *id = list->data;
1967                                 if (id && !id->fd->pixbuf) fd = id->fd;
1968                                 list = list->next;
1969                                 }
1970
1971                         valid = gtk_tree_model_iter_next(store, &iter);
1972                         }
1973                 }
1974
1975         /* then find first undone */
1976
1977         if (!fd)
1978                 {
1979                 GList *work = vfi->list;
1980                 while (work && !fd)
1981                         {
1982                         IconData *id = work->data;
1983                         FileData *fd_p = id->fd;
1984                         work = work->next;
1985
1986                         if (!fd_p->pixbuf) fd = fd_p;
1987                         }
1988                 }
1989
1990         if (!fd)
1991                 {
1992                 /* done */
1993                 vficon_thumb_cleanup(vfi);
1994                 return FALSE;
1995                 }
1996
1997         vfi->thumbs_count++;
1998
1999         vfi->thumbs_fd = fd;
2000
2001         thumb_loader_free(vfi->thumbs_loader);
2002
2003         vfi->thumbs_loader = thumb_loader_new(options->thumbnails.max_width, options->thumbnails.max_height);
2004         thumb_loader_set_callbacks(vfi->thumbs_loader,
2005                                    vficon_thumb_done_cb,
2006                                    vficon_thumb_error_cb,
2007                                    NULL,
2008                                    vfi);
2009
2010         if (!thumb_loader_start(vfi->thumbs_loader, fd->path))
2011                 {
2012                 /* set icon to unknown, continue */
2013                 DEBUG_1("thumb loader start failed %s\n", vfi->thumbs_loader->path);
2014                 vficon_thumb_do(vfi, vfi->thumbs_loader, fd);
2015
2016                 return TRUE;
2017                 }
2018
2019         return FALSE;
2020 }
2021
2022 static void vficon_thumb_update(ViewFileIcon *vfi)
2023 {
2024         vficon_thumb_stop(vfi);
2025
2026         vficon_thumb_status(vfi, 0.0, _("Loading thumbs..."));
2027         vfi->thumbs_running = TRUE;
2028
2029         while (vficon_thumb_next(vfi));
2030 }
2031
2032 /*
2033  *-----------------------------------------------------------------------------
2034  * row stuff
2035  *-----------------------------------------------------------------------------
2036  */
2037
2038 FileData *vficon_index_get_data(ViewFileIcon *vfi, gint row)
2039 {
2040         IconData *id;
2041
2042         id = g_list_nth_data(vfi->list, row);
2043         return id ? id->fd : NULL;
2044 }
2045
2046 gchar *vficon_index_get_path(ViewFileIcon *vfi, gint row)
2047 {
2048         FileData *fd;
2049         IconData *id;
2050
2051         id = g_list_nth_data(vfi->list, row);
2052         fd = id ? id->fd : NULL;
2053
2054         return (fd ? fd->path : NULL);
2055 }
2056
2057 gint vficon_index_by_path(ViewFileIcon *vfi, const gchar *path)
2058 {
2059         gint p = 0;
2060         GList *work;
2061
2062         if (!path) return -1;
2063
2064         work = vfi->list;
2065         while (work)
2066                 {
2067                 IconData *id = work->data;
2068                 FileData *fd = id->fd;
2069                 if (strcmp(path, fd->path) == 0) return p;
2070                 work = work->next;
2071                 p++;
2072                 }
2073
2074         return -1;
2075 }
2076
2077 gint vficon_index_by_fd(ViewFileIcon *vfi, FileData *in_fd)
2078 {
2079         gint p = 0;
2080         GList *work;
2081
2082         if (!in_fd) return -1;
2083
2084         work = vfi->list;
2085         while (work)
2086                 {
2087                 IconData *id = work->data;
2088                 FileData *fd = id->fd;
2089                 if (fd == in_fd) return p;
2090                 work = work->next;
2091                 p++;
2092                 }
2093
2094         return -1;
2095 }
2096
2097 static gint vficon_index_by_id(ViewFileIcon *vfi, IconData *in_id)
2098 {
2099         gint p = 0;
2100         GList *work;
2101
2102         if (!in_id) return -1;
2103
2104         work = vfi->list;
2105         while (work)
2106                 {
2107                 IconData *id = work->data;
2108                 if (id == in_id) return p;
2109                 work = work->next;
2110                 p++;
2111                 }
2112
2113         return -1;
2114 }
2115
2116 gint vficon_count(ViewFileIcon *vfi, gint64 *bytes)
2117 {
2118         if (bytes)
2119                 {
2120                 gint64 b = 0;
2121                 GList *work;
2122
2123                 work = vfi->list;
2124                 while (work)
2125                         {
2126
2127                         IconData *id = work->data;
2128                         FileData *fd = id->fd;
2129                         work = work->next;
2130                         b += fd->size;
2131                         }
2132
2133                 *bytes = b;
2134                 }
2135
2136         return g_list_length(vfi->list);
2137 }
2138
2139 GList *vficon_get_list(ViewFileIcon *vfi)
2140 {
2141         GList *list = NULL;
2142         GList *work;
2143
2144         work = vfi->list;
2145         while (work)
2146                 {
2147                 IconData *id = work->data;
2148                 FileData *fd = id->fd;
2149                 work = work->next;
2150
2151                 list = g_list_prepend(list, file_data_ref(fd));
2152                 }
2153
2154         return g_list_reverse(list);
2155 }
2156
2157 /*
2158  *-----------------------------------------------------------------------------
2159  *
2160  *-----------------------------------------------------------------------------
2161  */
2162
2163 static gint vficon_refresh_real(ViewFileIcon *vfi, gint keep_position)
2164 {
2165         gint ret = TRUE;
2166         GList *old_list;
2167         GList *work;
2168         IconData *focus_id;
2169
2170         focus_id = vfi->focus_id;
2171
2172         old_list = vfi->list;
2173         vfi->list = NULL;
2174
2175         if (vfi->path)
2176                 {
2177                 ret = iconlist_read(vfi->path, &vfi->list);
2178                 }
2179
2180         /* check for same files from old_list */
2181         work = old_list;
2182         while (work)
2183                 {
2184                 IconData *id;
2185                 FileData *fd;
2186
2187                 GList *needle;
2188
2189                 id = work->data;
2190                 fd = id->fd;
2191
2192                 needle = vfi->list;
2193                 while (needle)
2194                         {
2195                         IconData *idn = needle->data;
2196                         FileData *fdn = idn->fd;
2197                         if (fdn == fd)
2198                                 {
2199                                 /* swap, to retain old thumb, selection */
2200                                 needle->data = id;
2201                                 work->data = idn;
2202                                 needle = NULL;
2203                                 }
2204                         else
2205                                 {
2206                                 needle = needle->next;
2207                                 }
2208                         }
2209
2210                 work = work->next;
2211                 }
2212
2213         vfi->list = iconlist_sort(vfi->list, vfi->sort_method, vfi->sort_ascend);
2214
2215         work = old_list;
2216         while (work)
2217                 {
2218                 IconData *id = work->data;
2219                 work = work->next;
2220
2221                 if (id == vfi->prev_selection) vfi->prev_selection = NULL;
2222                 if (id == vfi->click_id) vfi->click_id = NULL;
2223                 }
2224
2225         vficon_populate(vfi, TRUE, keep_position);
2226
2227         /* attempt to keep focus on same icon when refreshing */
2228         if (focus_id && g_list_find(vfi->list, focus_id))
2229                 {
2230                 vficon_set_focus(vfi, focus_id);
2231                 }
2232
2233         iconlist_free(old_list);
2234
2235         return ret;
2236 }
2237
2238 gint vficon_refresh(ViewFileIcon *vfi)
2239 {
2240         return vficon_refresh_real(vfi, TRUE);
2241 }
2242
2243 /*
2244  *-----------------------------------------------------------------------------
2245  * draw, etc.
2246  *-----------------------------------------------------------------------------
2247  */
2248
2249 typedef struct _ColumnData ColumnData;
2250 struct _ColumnData
2251 {
2252         ViewFileIcon *vfi;
2253         gint number;
2254 };
2255
2256 static void vficon_cell_data_cb(GtkTreeViewColumn *tree_column, GtkCellRenderer *cell,
2257                                 GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
2258 {
2259         ColumnData *cd = data;
2260         ViewFileIcon *vfi;
2261         GtkStyle *style;
2262         GList *list;
2263         GdkColor color_fg;
2264         GdkColor color_bg;
2265         IconData *id;
2266
2267         vfi = cd->vfi;
2268
2269         gtk_tree_model_get(tree_model, iter, FILE_COLUMN_POINTER, &list, -1);
2270
2271         id = g_list_nth_data(list, cd->number);
2272
2273         if (id) g_assert(id->fd->magick == 0x12345678);
2274
2275         style = gtk_widget_get_style(vfi->listview);
2276         if (id && id->selected & SELECTION_SELECTED)
2277                 {
2278                 memcpy(&color_fg, &style->text[GTK_STATE_SELECTED], sizeof(color_fg));
2279                 memcpy(&color_bg, &style->base[GTK_STATE_SELECTED], sizeof(color_bg));
2280                 }
2281         else
2282                 {
2283                 memcpy(&color_fg, &style->text[GTK_STATE_NORMAL], sizeof(color_fg));
2284                 memcpy(&color_bg, &style->base[GTK_STATE_NORMAL], sizeof(color_bg));
2285                 }
2286
2287         if (id && id->selected & SELECTION_PRELIGHT)
2288                 {
2289 #if 0
2290                 shift_color(&color_fg, -1, 0);
2291 #endif
2292                 shift_color(&color_bg, -1, 0);
2293                 }
2294
2295         if (GQV_IS_CELL_RENDERER_ICON(cell))
2296                 {
2297                 if (id)
2298                         {
2299                         g_object_set(cell,      "pixbuf", id->fd->pixbuf,
2300                                                 "text", id->fd->name,
2301                                                 "cell-background-gdk", &color_bg,
2302                                                 "cell-background-set", TRUE,
2303                                                 "foreground-gdk", &color_fg,
2304                                                 "foreground-set", TRUE,
2305                                                 "has-focus", (vfi->focus_id == id), NULL);
2306                         }
2307                 else
2308                         {
2309                         g_object_set(cell,      "pixbuf", NULL,
2310                                                 "text", NULL,
2311                                                 "cell-background-set", FALSE,
2312                                                 "foreground-set", FALSE,
2313                                                 "has-focus", FALSE, NULL);
2314                         }
2315                 }
2316 }
2317
2318 static void vficon_append_column(ViewFileIcon *vfi, gint n)
2319 {
2320         ColumnData *cd;
2321         GtkTreeViewColumn *column;
2322         GtkCellRenderer *renderer;
2323
2324         column = gtk_tree_view_column_new();
2325         gtk_tree_view_column_set_min_width(column, 0);
2326
2327         gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
2328         gtk_tree_view_column_set_alignment(column, 0.5);
2329
2330         renderer = gqv_cell_renderer_icon_new();
2331         gtk_tree_view_column_pack_start(column, renderer, FALSE);
2332         g_object_set(G_OBJECT(renderer), "xpad", THUMB_BORDER_PADDING * 2,
2333                                          "ypad", THUMB_BORDER_PADDING,
2334                                          "mode", GTK_CELL_RENDERER_MODE_ACTIVATABLE, NULL);
2335
2336         g_object_set_data(G_OBJECT(column), "column_number", GINT_TO_POINTER(n));
2337
2338         cd = g_new0(ColumnData, 1);
2339         cd->vfi = vfi;
2340         cd->number = n;
2341         gtk_tree_view_column_set_cell_data_func(column, renderer, vficon_cell_data_cb, cd, g_free);
2342
2343         gtk_tree_view_append_column(GTK_TREE_VIEW(vfi->listview), column);
2344 }
2345
2346 /*
2347  *-----------------------------------------------------------------------------
2348  * base
2349  *-----------------------------------------------------------------------------
2350  */
2351
2352 gint vficon_set_path(ViewFileIcon *vfi, const gchar *path)
2353 {
2354         gint ret;
2355
2356         if (!path) return FALSE;
2357         if (vfi->path && strcmp(path, vfi->path) == 0) return TRUE;
2358
2359         g_free(vfi->path);
2360         vfi->path = g_strdup(path);
2361
2362         g_list_free(vfi->selection);
2363         vfi->selection = NULL;
2364
2365         iconlist_free(vfi->list);
2366         vfi->list = NULL;
2367
2368         /* NOTE: populate will clear the store for us */
2369         ret = vficon_refresh_real(vfi, FALSE);
2370
2371         vfi->focus_id = NULL;
2372         vficon_move_focus(vfi, 0, 0, FALSE);
2373
2374         return ret;
2375 }
2376
2377 static void vficon_destroy_cb(GtkWidget *widget, gpointer data)
2378 {
2379         ViewFileIcon *vfi = data;
2380
2381         if (vfi->popup)
2382                 {
2383                 g_signal_handlers_disconnect_matched(G_OBJECT(vfi->popup), G_SIGNAL_MATCH_DATA,
2384                                                      0, 0, 0, NULL, vfi);
2385                 gtk_widget_destroy(vfi->popup);
2386                 }
2387
2388         if (vfi->sync_idle_id != -1) g_source_remove(vfi->sync_idle_id);
2389
2390         tip_unschedule(vfi);
2391
2392         vficon_thumb_cleanup(vfi);
2393
2394         g_free(vfi->path);
2395
2396         iconlist_free(vfi->list);
2397         g_list_free(vfi->selection);
2398         g_free(vfi);
2399 }
2400
2401 ViewFileIcon *vficon_new(const gchar *path)
2402 {
2403         ViewFileIcon *vfi;
2404         GtkListStore *store;
2405         GtkTreeSelection *selection;
2406         gint i;
2407
2408         vfi = g_new0(ViewFileIcon, 1);
2409
2410         vfi->path = NULL;
2411         vfi->sort_method = SORT_NAME;
2412         vfi->sort_ascend = TRUE;
2413
2414         vfi->selection = NULL;
2415         vfi->prev_selection = NULL;
2416
2417         vfi->tip_window = NULL;
2418         vfi->tip_delay_id = -1;
2419
2420         vfi->focus_row = 0;
2421         vfi->focus_column = 0;
2422         vfi->focus_id = NULL;
2423
2424         vfi->show_text = options->show_icon_names;
2425
2426         vfi->sync_idle_id = -1;
2427
2428         vfi->popup = NULL;
2429
2430         vfi->widget = gtk_scrolled_window_new(NULL, NULL);
2431         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(vfi->widget), GTK_SHADOW_IN);
2432         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(vfi->widget),
2433                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
2434         g_signal_connect(G_OBJECT(vfi->widget), "destroy",
2435                          G_CALLBACK(vficon_destroy_cb), vfi);
2436
2437         store = gtk_list_store_new(1, G_TYPE_POINTER);
2438         vfi->listview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
2439         g_object_unref(store);
2440
2441         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(vfi->listview));
2442         gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_NONE);
2443
2444         gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(vfi->listview), FALSE);
2445         gtk_tree_view_set_enable_search(GTK_TREE_VIEW(vfi->listview), FALSE);
2446
2447         for (i = 0; i < VFICON_MAX_COLUMNS; i++)
2448                 {
2449                 vficon_append_column(vfi, i);
2450                 }
2451
2452         /* zero width column to hide tree view focus, we draw it ourselves */
2453         vficon_append_column(vfi, i);
2454         /* end column to fill white space */
2455         vficon_append_column(vfi, i);
2456
2457         g_signal_connect(G_OBJECT(vfi->listview), "size_allocate",
2458                          G_CALLBACK(vficon_sized_cb), vfi);
2459         g_signal_connect(G_OBJECT(vfi->listview), "key_press_event",
2460                          G_CALLBACK(vficon_press_key_cb), vfi);
2461
2462         gtk_container_add(GTK_CONTAINER(vfi->widget), vfi->listview);
2463         gtk_widget_show(vfi->listview);
2464
2465         vficon_dnd_init(vfi);
2466
2467         gtk_widget_set_events(vfi->listview, GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK |
2468                               GDK_BUTTON_PRESS_MASK | GDK_LEAVE_NOTIFY_MASK);
2469         g_signal_connect(G_OBJECT(vfi->listview), "button_press_event",
2470                          G_CALLBACK(vficon_press_cb), vfi);
2471         g_signal_connect(G_OBJECT(vfi->listview), "button_release_event",
2472                          G_CALLBACK(vficon_release_cb), vfi);
2473         g_signal_connect(G_OBJECT(vfi->listview),"motion_notify_event",
2474                          G_CALLBACK(vficon_motion_cb), vfi);
2475         g_signal_connect(G_OBJECT(vfi->listview), "leave_notify_event",
2476                          G_CALLBACK(vficon_leave_cb), vfi);
2477
2478         /* force vfi->columns to be at least 1 (sane) - this will be corrected in the size_cb */
2479         vficon_populate_at_new_size(vfi, 1, 1, FALSE);
2480
2481         if (path) vficon_set_path(vfi, path);
2482
2483         return vfi;
2484 }
2485
2486 void vficon_set_status_func(ViewFileIcon *vfi,
2487                             void (*func)(ViewFileIcon *vfi, gpointer data), gpointer data)
2488 {
2489         vfi->func_status = func;
2490         vfi->data_status = data;
2491 }
2492
2493 void vficon_set_thumb_status_func(ViewFileIcon *vfi,
2494                                   void (*func)(ViewFileIcon *vfi, gdouble val, const gchar *text, gpointer data),
2495                                   gpointer data)
2496 {
2497         vfi->func_thumb_status = func;
2498         vfi->data_thumb_status = data;
2499 }
2500
2501 void vficon_set_layout(ViewFileIcon *vfi, LayoutWindow *layout)
2502 {
2503         vfi->layout = layout;
2504 }
2505
2506 /*
2507  *-----------------------------------------------------------------------------
2508  * maintenance (for rename, move, remove)
2509  *-----------------------------------------------------------------------------
2510  */
2511
2512 static gint vficon_maint_find_closest(ViewFileIcon *vfi, gint row, gint count, GList *ignore_list)
2513 {
2514         GList *list = NULL;
2515         GList *work;
2516         gint rev = row - 1;
2517         row ++;
2518
2519         work = ignore_list;
2520         while (work)
2521                 {
2522                 FileData *fd = work->data;
2523                 gint f = vficon_index_by_fd(vfi, work->data);
2524                 g_assert(fd->magick == 0x12345678);
2525                 if (f >= 0) list = g_list_prepend(list, GINT_TO_POINTER(f));
2526                 work = work->next;
2527                 }
2528
2529         while (list)
2530                 {
2531                 gint c = TRUE;
2532                 work = list;
2533                 while (work && c)
2534                         {
2535                         gpointer p = work->data;
2536                         work = work->next;
2537                         if (row == GPOINTER_TO_INT(p))
2538                                 {
2539                                 row++;
2540                                 c = FALSE;
2541                                 }
2542                         if (rev == GPOINTER_TO_INT(p))
2543                                 {
2544                                 rev--;
2545                                 c = FALSE;
2546                                 }
2547                         if (!c) list = g_list_remove(list, p);
2548                         }
2549                 if (c && list)
2550                         {
2551                         g_list_free(list);
2552                         list = NULL;
2553                         }
2554                 }
2555         if (row > count - 1)
2556                 {
2557                 if (rev < 0)
2558                         return -1;
2559                 else
2560                         return rev;
2561                 }
2562         else
2563                 {
2564                 return row;
2565                 }
2566 }
2567
2568 gint vficon_maint_renamed(ViewFileIcon *vfi, FileData *fd)
2569 {
2570         gint ret = FALSE;
2571         gint row;
2572         gchar *source_base;
2573         gchar *dest_base;
2574         IconData *id = vficon_icon_data(vfi, fd);
2575
2576         if (!id) return FALSE;
2577
2578         row = vficon_index_by_id(vfi, id);
2579         if (row < 0) return FALSE;
2580
2581         source_base = remove_level_from_path(fd->change->source);
2582         dest_base = remove_level_from_path(fd->change->dest);
2583
2584         if (strcmp(source_base, dest_base) == 0)
2585                 {
2586                 vfi->list = g_list_remove(vfi->list, id);
2587                 vfi->list = iconlist_insert_sort(vfi->list, id, vfi->sort_method, vfi->sort_ascend);
2588
2589                 vficon_sync_idle(vfi);
2590                 ret = TRUE;
2591                 }
2592         else
2593                 {
2594                 ret = vficon_maint_removed(vfi, fd, NULL);
2595                 }
2596
2597         g_free(source_base);
2598         g_free(dest_base);
2599
2600         return ret;
2601 }
2602
2603 gint vficon_maint_removed(ViewFileIcon *vfi, FileData *fd, GList *ignore_list)
2604 {
2605         gint row;
2606         gint new_row = -1;
2607         GtkTreeModel *store;
2608         GtkTreeIter iter;
2609         IconData *id = vficon_icon_data(vfi, fd);
2610
2611         if (!id) return FALSE;
2612
2613         row = g_list_index(vfi->list, id);
2614         if (row < 0) return FALSE;
2615
2616         if ((id->selected & SELECTION_SELECTED) &&
2617             layout_image_get_collection(vfi->layout, NULL) == NULL)
2618                 {
2619                 vficon_unselect(vfi, id);
2620
2621                 if (!vfi->selection)
2622                         {
2623                         gint n;
2624
2625                         n = vficon_count(vfi, NULL);
2626                         if (ignore_list)
2627                                 {
2628                                 new_row = vficon_maint_find_closest(vfi, row, n, ignore_list);
2629                                 DEBUG_1("row = %d, closest is %d\n", row, new_row);
2630                                 }
2631                         else
2632                                 {
2633                                 if (row + 1 < n)
2634                                         {
2635                                         new_row = row + 1;
2636                                         }
2637                                 else if (row > 0)
2638                                         {
2639                                         new_row = row - 1;
2640                                         }
2641                                 }
2642                         }
2643                 else if (ignore_list)
2644                         {
2645                         GList *work;
2646
2647                         work = vfi->selection;
2648                         while (work)
2649                                 {
2650                                 IconData *ignore_id;
2651                                 FileData *ignore_fd;
2652                                 GList *tmp;
2653                                 gint match = FALSE;
2654
2655                                 ignore_id = work->data;
2656                                 ignore_fd = ignore_id->fd;
2657                                 g_assert(ignore_fd->magick == 0x12345678);
2658                                 work = work->next;
2659
2660                                 tmp = ignore_list;
2661                                 while (tmp && !match)
2662                                         {
2663                                         FileData *ignore_list_fd = tmp->data;
2664                                         g_assert(ignore_list_fd->magick == 0x12345678);
2665                                         tmp = tmp->next;
2666
2667                                         if (ignore_list_fd == ignore_fd)
2668                                                 {
2669                                                 match = TRUE;
2670                                                 }
2671                                         }
2672                                 if (!match)
2673                                         {
2674                                         new_row = g_list_index(vfi->list, ignore_id);
2675                                         work = NULL;
2676                                         }
2677                                 }
2678                         if (new_row == -1)
2679                                 {
2680                                 /* selection all ignored, use closest */
2681                                 new_row = vficon_maint_find_closest(vfi, row, vficon_count(vfi, NULL), ignore_list);
2682                                 }
2683                         }
2684                 else
2685                         {
2686                         new_row = g_list_index(vfi->list, vfi->selection->data);
2687                         }
2688                 if (new_row >= 0)
2689                         {
2690                         IconData *idn = g_list_nth_data(vfi->list, new_row);
2691
2692                         vficon_select(vfi, idn);
2693                         vficon_send_layout_select(vfi, idn);
2694                         }
2695                 }
2696
2697         /* Thumb loader check */
2698         if (fd == vfi->thumbs_fd) vfi->thumbs_fd = NULL;
2699         if (vfi->thumbs_count > 0) vfi->thumbs_count--;
2700
2701         if (vfi->prev_selection == id) vfi->prev_selection = NULL;
2702         if (vfi->click_id == id) vfi->click_id = NULL;
2703
2704         /* remove pointer to this fd from grid */
2705         store = gtk_tree_view_get_model(GTK_TREE_VIEW(vfi->listview));
2706         if (id->row >= 0 &&
2707             gtk_tree_model_iter_nth_child(store, &iter, NULL, id->row))
2708                 {
2709                 GList *list;
2710
2711                 gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &list, -1);
2712                 list = g_list_find(list, id);
2713                 if (list) list->data = NULL;
2714                 }
2715
2716         vfi->list = g_list_remove(vfi->list, id);
2717         file_data_unref(fd);
2718         g_free(id);
2719
2720         vficon_sync_idle(vfi);
2721         vficon_send_update(vfi);
2722
2723         return TRUE;
2724 }
2725
2726 gint vficon_maint_moved(ViewFileIcon *vfi, FileData *fd, GList *ignore_list)
2727 {
2728         gint ret = FALSE;
2729         gchar *buf;
2730
2731         if (!fd->change->source || !vfi->path) return FALSE;
2732
2733         buf = remove_level_from_path(fd->change->source);
2734
2735         if (strcmp(buf, vfi->path) == 0)
2736                 {
2737                 ret = vficon_maint_removed(vfi, fd, ignore_list);
2738                 }
2739
2740         g_free(buf);
2741
2742         return ret;
2743 }