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