Mercurial > geeqie
annotate src/view_file_list.c @ 90:dc3c77d027e6
Tue Oct 31 18:06:42 2006 John Ellis <johne@verizon.net>
* info.c: Increase default info window size to 600x400.
* po/be.po: Update Belarusian translation,
submitted by Pavel Piatruk <berserker@neolocation.com>.
* gqview.desktop: Add additional formats to MimeType list.
author | gqview |
---|---|
date | Tue, 31 Oct 2006 23:20:48 +0000 |
parents | 04ff0df3ad2f |
children | f1c8f8632e23 |
rev | line source |
---|---|
9 | 1 /* |
2 * GQview | |
3 * (C) 2004 John Ellis | |
4 * | |
5 * Author: John Ellis | |
6 * | |
7 * This software is released under the GNU General Public License (GNU GPL). | |
8 * Please read the included file COPYING for more information. | |
9 * This software comes with no warranty of any kind, use at your own risk! | |
10 */ | |
11 | |
12 #include "gqview.h" | |
13 #include "view_file_list.h" | |
14 | |
15 #include "cache_maint.h" | |
16 #include "dnd.h" | |
17 #include "editors.h" | |
18 #include "img-view.h" | |
19 #include "info.h" | |
20 #include "layout.h" | |
21 #include "layout_image.h" | |
22 #include "menu.h" | |
23 #include "thumb.h" | |
24 #include "utilops.h" | |
25 #include "ui_bookmark.h" | |
26 #include "ui_fileops.h" | |
27 #include "ui_menu.h" | |
28 #include "ui_tree_edit.h" | |
29 | |
30 #include <gdk/gdkkeysyms.h> /* for keyboard values */ | |
31 | |
32 | |
33 enum { | |
34 FILE_COLUMN_POINTER = 0, | |
35 FILE_COLUMN_THUMB, | |
36 FILE_COLUMN_NAME, | |
37 FILE_COLUMN_SIZE, | |
38 FILE_COLUMN_DATE, | |
39 FILE_COLUMN_COLOR, | |
40 FILE_COLUMN_COUNT | |
41 }; | |
42 | |
43 | |
44 static gint vflist_row_is_selected(ViewFileList *vfl, FileData *fd); | |
45 static gint vflist_row_rename_cb(TreeEditData *td, const gchar *old, const gchar *new, gpointer data); | |
46 static void vflist_populate_view(ViewFileList *vfl); | |
47 | |
48 /* | |
49 *----------------------------------------------------------------------------- | |
50 * signals | |
51 *----------------------------------------------------------------------------- | |
52 */ | |
53 | |
54 static void vflist_send_update(ViewFileList *vfl) | |
55 { | |
56 if (vfl->func_status) vfl->func_status(vfl, vfl->data_status); | |
57 } | |
58 | |
59 /* | |
60 *----------------------------------------------------------------------------- | |
61 * misc | |
62 *----------------------------------------------------------------------------- | |
63 */ | |
64 | |
65 static gint vflist_find_row(ViewFileList *vfl, FileData *fd, GtkTreeIter *iter) | |
66 { | |
67 GtkTreeModel *store; | |
68 gint valid; | |
69 gint row = 0; | |
70 | |
71 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vfl->listview)); | |
72 valid = gtk_tree_model_get_iter_first(store, iter); | |
73 while (valid) | |
74 { | |
75 FileData *fd_n; | |
76 gtk_tree_model_get(GTK_TREE_MODEL(store), iter, FILE_COLUMN_POINTER, &fd_n, -1); | |
77 if (fd_n == fd) return row; | |
78 | |
79 valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), iter); | |
80 row++; | |
81 } | |
82 | |
83 return -1; | |
84 } | |
85 | |
86 static void vflist_color_set(ViewFileList *vfl, FileData *fd, gint color_set) | |
87 { | |
88 GtkTreeModel *store; | |
89 GtkTreeIter iter; | |
90 | |
91 if (vflist_find_row(vfl, fd, &iter) < 0) return; | |
92 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vfl->listview)); | |
93 gtk_list_store_set(GTK_LIST_STORE(store), &iter, FILE_COLUMN_COLOR, color_set, -1); | |
94 } | |
95 | |
96 static void vflist_move_cursor(ViewFileList *vfl, GtkTreeIter *iter) | |
97 { | |
98 GtkTreeModel *store; | |
99 GtkTreePath *tpath; | |
100 | |
101 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vfl->listview)); | |
102 | |
103 tpath = gtk_tree_model_get_path(store, iter); | |
104 gtk_tree_view_set_cursor(GTK_TREE_VIEW(vfl->listview), tpath, NULL, FALSE); | |
105 gtk_tree_path_free(tpath); | |
106 } | |
107 | |
108 /* | |
109 *----------------------------------------------------------------------------- | |
110 * dnd | |
111 *----------------------------------------------------------------------------- | |
112 */ | |
113 | |
114 static void vflist_dnd_get(GtkWidget *widget, GdkDragContext *context, | |
115 GtkSelectionData *selection_data, guint info, | |
116 guint time, gpointer data) | |
117 { | |
118 ViewFileList *vfl = data; | |
119 GList *list = NULL; | |
120 gchar *uri_text = NULL; | |
121 gint total; | |
122 | |
123 if (!vfl->click_fd) return; | |
124 | |
125 if (vflist_row_is_selected(vfl, vfl->click_fd)) | |
126 { | |
127 list = vflist_selection_get_list(vfl); | |
128 } | |
129 else | |
130 { | |
131 list = g_list_append(NULL, g_strdup(vfl->click_fd->path)); | |
132 } | |
133 | |
134 if (!list) return; | |
135 | |
136 uri_text = uri_text_from_list(list, &total, (info == TARGET_TEXT_PLAIN)); | |
137 path_list_free(list); | |
138 | |
139 if (debug) printf(uri_text); | |
140 | |
64
04ff0df3ad2f
Mon Aug 15 17:13:57 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
141 gtk_selection_data_set(selection_data, selection_data->target, |
04ff0df3ad2f
Mon Aug 15 17:13:57 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
142 8, (guchar *)uri_text, total); |
9 | 143 g_free(uri_text); |
144 } | |
145 | |
146 static void vflist_dnd_begin(GtkWidget *widget, GdkDragContext *context, gpointer data) | |
147 { | |
148 ViewFileList *vfl = data; | |
149 | |
150 vflist_color_set(vfl, vfl->click_fd, TRUE); | |
151 | |
152 if (vfl->thumbs_enabled && | |
153 vfl->click_fd && vfl->click_fd->pixbuf) | |
154 { | |
155 gint items; | |
156 | |
157 if (vflist_row_is_selected(vfl, vfl->click_fd)) | |
158 items = vflist_selection_count(vfl, NULL); | |
159 else | |
160 items = 1; | |
161 | |
162 dnd_set_drag_icon(widget, context, vfl->click_fd->pixbuf, items); | |
163 } | |
164 } | |
165 | |
166 static void vflist_dnd_end(GtkWidget *widget, GdkDragContext *context, gpointer data) | |
167 { | |
168 ViewFileList *vfl = data; | |
169 | |
170 vflist_color_set(vfl, vfl->click_fd, FALSE); | |
171 | |
172 if (context->action == GDK_ACTION_MOVE) | |
173 { | |
174 vflist_refresh(vfl); | |
175 } | |
176 } | |
177 | |
178 static void vflist_dnd_init(ViewFileList *vfl) | |
179 { | |
180 gtk_drag_source_set(vfl->listview, GDK_BUTTON1_MASK | GDK_BUTTON2_MASK, | |
181 dnd_file_drag_types, dnd_file_drag_types_count, | |
182 GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK); | |
183 g_signal_connect(G_OBJECT(vfl->listview), "drag_data_get", | |
184 G_CALLBACK(vflist_dnd_get), vfl); | |
185 g_signal_connect(G_OBJECT(vfl->listview), "drag_begin", | |
186 G_CALLBACK(vflist_dnd_begin), vfl); | |
187 g_signal_connect(G_OBJECT(vfl->listview), "drag_end", | |
188 G_CALLBACK(vflist_dnd_end), vfl); | |
189 } | |
190 | |
191 /* | |
192 *----------------------------------------------------------------------------- | |
193 * pop-up menu | |
194 *----------------------------------------------------------------------------- | |
195 */ | |
196 | |
197 static GList *vflist_pop_menu_file_list(ViewFileList *vfl) | |
198 { | |
199 if (!vfl->click_fd) return NULL; | |
200 | |
201 if (vflist_row_is_selected(vfl, vfl->click_fd)) | |
202 { | |
203 return vflist_selection_get_list(vfl); | |
204 } | |
205 | |
206 return g_list_append(NULL, g_strdup(vfl->click_fd->path)); | |
207 } | |
208 | |
209 static void vflist_pop_menu_edit_cb(GtkWidget *widget, gpointer data) | |
210 { | |
211 ViewFileList *vfl; | |
212 gint n; | |
213 GList *list; | |
214 | |
215 vfl = submenu_item_get_data(widget); | |
216 n = GPOINTER_TO_INT(data); | |
217 | |
218 if (!vfl) return; | |
219 | |
220 list = vflist_pop_menu_file_list(vfl); | |
221 start_editor_from_path_list(n, list); | |
222 path_list_free(list); | |
223 } | |
224 | |
225 static void vflist_pop_menu_info_cb(GtkWidget *widget, gpointer data) | |
226 { | |
227 ViewFileList *vfl = data; | |
228 | |
229 info_window_new(NULL, vflist_pop_menu_file_list(vfl)); | |
230 } | |
231 | |
232 static void vflist_pop_menu_view_cb(GtkWidget *widget, gpointer data) | |
233 { | |
234 ViewFileList *vfl = data; | |
235 | |
236 if (vflist_row_is_selected(vfl, vfl->click_fd)) | |
237 { | |
238 GList *list; | |
239 | |
240 list = vflist_selection_get_list(vfl); | |
241 view_window_new_from_list(list); | |
242 path_list_free(list); | |
243 } | |
244 else | |
245 { | |
246 const gchar *path; | |
247 | |
248 path = vfl->click_fd->path; | |
249 view_window_new(path); | |
250 } | |
251 } | |
252 | |
253 static void vflist_pop_menu_copy_cb(GtkWidget *widget, gpointer data) | |
254 { | |
255 ViewFileList *vfl = data; | |
256 | |
257 file_util_copy(NULL, vflist_pop_menu_file_list(vfl), NULL, vfl->listview); | |
258 } | |
259 | |
260 static void vflist_pop_menu_move_cb(GtkWidget *widget, gpointer data) | |
261 { | |
262 ViewFileList *vfl = data; | |
263 | |
264 file_util_move(NULL, vflist_pop_menu_file_list(vfl), NULL, vfl->listview); | |
265 } | |
266 | |
267 static void vflist_pop_menu_rename_cb(GtkWidget *widget, gpointer data) | |
268 { | |
269 ViewFileList *vfl = data; | |
270 GList *list; | |
271 | |
272 list = vflist_pop_menu_file_list(vfl); | |
273 if (enable_in_place_rename && | |
274 list && !list->next && vfl->click_fd) | |
275 { | |
276 GtkTreeModel *store; | |
277 GtkTreeIter iter; | |
278 | |
279 path_list_free(list); | |
280 | |
281 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vfl->listview)); | |
282 if (vflist_find_row(vfl, vfl->click_fd, &iter) >= 0) | |
283 { | |
284 GtkTreePath *tpath; | |
285 | |
286 tpath = gtk_tree_model_get_path(store, &iter); | |
287 tree_edit_by_path(GTK_TREE_VIEW(vfl->listview), tpath, | |
288 FILE_COLUMN_NAME -1, vfl->click_fd->name, | |
289 vflist_row_rename_cb, vfl); | |
290 gtk_tree_path_free(tpath); | |
291 } | |
292 return; | |
293 } | |
294 | |
295 file_util_rename(NULL, list, vfl->listview); | |
296 } | |
297 | |
298 static void vflist_pop_menu_delete_cb(GtkWidget *widget, gpointer data) | |
299 { | |
300 ViewFileList *vfl = data; | |
301 | |
302 file_util_delete(NULL, vflist_pop_menu_file_list(vfl), vfl->listview); | |
303 } | |
304 | |
305 static void vflist_pop_menu_sort_cb(GtkWidget *widget, gpointer data) | |
306 { | |
307 ViewFileList *vfl; | |
308 SortType type; | |
309 | |
310 vfl = submenu_item_get_data(widget); | |
311 if (!vfl) return; | |
312 | |
313 type = (SortType)GPOINTER_TO_INT(data); | |
314 | |
315 if (vfl->layout) | |
316 { | |
317 layout_sort_set(vfl->layout, type, vfl->sort_ascend); | |
318 } | |
319 else | |
320 { | |
321 vflist_sort_set(vfl, type, vfl->sort_ascend); | |
322 } | |
323 } | |
324 | |
325 static void vflist_pop_menu_sort_ascend_cb(GtkWidget *widget, gpointer data) | |
326 { | |
327 ViewFileList *vfl = data; | |
328 | |
329 if (vfl->layout) | |
330 { | |
331 layout_sort_set(vfl->layout, vfl->sort_method, !vfl->sort_ascend); | |
332 } | |
333 else | |
334 { | |
335 vflist_sort_set(vfl, vfl->sort_method, !vfl->sort_ascend); | |
336 } | |
337 } | |
338 | |
339 static void vflist_pop_menu_icons_cb(GtkWidget *widget, gpointer data) | |
340 { | |
341 ViewFileList *vfl = data; | |
342 | |
343 if (vfl->layout) layout_views_set(vfl->layout, vfl->layout->tree_view, TRUE); | |
344 } | |
345 | |
346 static void vflist_pop_menu_thumbs_cb(GtkWidget *widget, gpointer data) | |
347 { | |
348 ViewFileList *vfl = data; | |
349 | |
350 vflist_color_set(vfl, vfl->click_fd, FALSE); | |
351 if (vfl->layout) | |
352 { | |
353 layout_thumb_set(vfl->layout, !vfl->thumbs_enabled); | |
354 } | |
355 else | |
356 { | |
357 vflist_thumb_set(vfl, !vfl->thumbs_enabled); | |
358 } | |
359 } | |
360 | |
361 static void vflist_pop_menu_refresh_cb(GtkWidget *widget, gpointer data) | |
362 { | |
363 ViewFileList *vfl = data; | |
364 | |
365 vflist_color_set(vfl, vfl->click_fd, FALSE); | |
366 vflist_refresh(vfl); | |
367 } | |
368 | |
369 static void vflist_popup_destroy_cb(GtkWidget *widget, gpointer data) | |
370 { | |
371 ViewFileList *vfl = data; | |
372 vflist_color_set(vfl, vfl->click_fd, FALSE); | |
373 vfl->click_fd = NULL; | |
374 vfl->popup = NULL; | |
375 } | |
376 | |
377 static GtkWidget *vflist_pop_menu(ViewFileList *vfl, FileData *fd) | |
378 { | |
379 GtkWidget *menu; | |
380 GtkWidget *item; | |
381 GtkWidget *submenu; | |
382 gint active; | |
383 | |
384 vflist_color_set(vfl, fd, TRUE); | |
385 active = (fd != NULL); | |
386 | |
387 menu = popup_menu_short_lived(); | |
388 g_signal_connect(G_OBJECT(menu), "destroy", | |
389 G_CALLBACK(vflist_popup_destroy_cb), vfl); | |
390 | |
391 submenu_add_edit(menu, &item, G_CALLBACK(vflist_pop_menu_edit_cb), vfl); | |
392 gtk_widget_set_sensitive(item, active); | |
393 | |
394 menu_item_add_stock_sensitive(menu, _("_Properties"), GTK_STOCK_PROPERTIES, active, | |
395 G_CALLBACK(vflist_pop_menu_info_cb), vfl); | |
396 menu_item_add_stock_sensitive(menu, _("View in _new window"), GTK_STOCK_NEW, active, | |
397 G_CALLBACK(vflist_pop_menu_view_cb), vfl); | |
398 | |
399 menu_item_add_divider(menu); | |
400 menu_item_add_stock_sensitive(menu, _("_Copy..."), GTK_STOCK_COPY, active, | |
401 G_CALLBACK(vflist_pop_menu_copy_cb), vfl); | |
402 menu_item_add_sensitive(menu, _("_Move..."), active, | |
403 G_CALLBACK(vflist_pop_menu_move_cb), vfl); | |
404 menu_item_add_sensitive(menu, _("_Rename..."), active, | |
405 G_CALLBACK(vflist_pop_menu_rename_cb), vfl); | |
406 menu_item_add_stock_sensitive(menu, _("_Delete..."), GTK_STOCK_DELETE, active, | |
407 G_CALLBACK(vflist_pop_menu_delete_cb), vfl); | |
408 | |
409 menu_item_add_divider(menu); | |
410 | |
411 submenu = submenu_add_sort(NULL, G_CALLBACK(vflist_pop_menu_sort_cb), vfl, | |
412 FALSE, FALSE, TRUE, vfl->sort_method); | |
413 menu_item_add_divider(submenu); | |
414 menu_item_add_check(submenu, _("Ascending"), vfl->sort_ascend, | |
415 G_CALLBACK(vflist_pop_menu_sort_ascend_cb), vfl); | |
416 | |
417 item = menu_item_add(menu, _("_Sort"), NULL, NULL); | |
418 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu); | |
419 | |
420 menu_item_add_check(menu, _("View as _icons"), FALSE, | |
421 G_CALLBACK(vflist_pop_menu_icons_cb), vfl); | |
422 menu_item_add_check(menu, _("Show _thumbnails"), vfl->thumbs_enabled, | |
423 G_CALLBACK(vflist_pop_menu_thumbs_cb), vfl); | |
424 menu_item_add_stock(menu, _("Re_fresh"), GTK_STOCK_REFRESH, G_CALLBACK(vflist_pop_menu_refresh_cb), vfl); | |
425 | |
426 return menu; | |
427 } | |
428 | |
429 /* | |
430 *----------------------------------------------------------------------------- | |
431 * callbacks | |
432 *----------------------------------------------------------------------------- | |
433 */ | |
434 | |
435 static gint vflist_row_rename_cb(TreeEditData *td, const gchar *old, const gchar *new, gpointer data) | |
436 { | |
437 ViewFileList *vfl = data; | |
438 gchar *old_path; | |
439 gchar *new_path; | |
440 | |
441 if (strlen(new) == 0) return FALSE; | |
442 | |
443 old_path = concat_dir_and_file(vfl->path, old); | |
444 new_path = concat_dir_and_file(vfl->path, new); | |
445 | |
446 if (strchr(new, '/') != NULL) | |
447 { | |
448 gchar *text = g_strdup_printf(_("Invalid file name:\n%s"), new); | |
449 file_util_warning_dialog(_("Error renaming file"), text, GTK_STOCK_DIALOG_ERROR, vfl->listview); | |
450 g_free(text); | |
451 } | |
452 else if (isfile(new_path)) | |
453 { | |
454 gchar *text = g_strdup_printf(_("A file with name %s already exists."), new); | |
455 file_util_warning_dialog(_("Error renaming file"), text, GTK_STOCK_DIALOG_ERROR, vfl->listview); | |
456 g_free(text); | |
457 } | |
458 else if (!rename_file(old_path, new_path)) | |
459 { | |
460 gchar *text = g_strdup_printf(_("Unable to rename file:\n%s\nto:\n%s"), old, new); | |
461 file_util_warning_dialog(_("Error renaming file"), text, GTK_STOCK_DIALOG_ERROR, vfl->listview); | |
462 g_free(text); | |
463 } | |
464 else | |
465 { | |
466 file_maint_renamed(old_path, new_path); | |
467 } | |
468 | |
469 g_free(old_path); | |
470 g_free(new_path); | |
471 | |
472 return FALSE; | |
473 } | |
474 | |
475 static void vflist_menu_position_cb(GtkMenu *menu, gint *x, gint *y, gboolean *push_in, gpointer data) | |
476 { | |
477 ViewFileList *vfl = data; | |
478 GtkTreeModel *store; | |
479 GtkTreeIter iter; | |
480 GtkTreePath *tpath; | |
481 gint cw, ch; | |
482 | |
483 if (vflist_find_row(vfl, vfl->click_fd, &iter) < 0) return; | |
484 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vfl->listview)); | |
485 tpath = gtk_tree_model_get_path(store, &iter); | |
486 tree_view_get_cell_clamped(GTK_TREE_VIEW(vfl->listview), tpath, FILE_COLUMN_NAME - 1, TRUE, x, y, &cw, &ch); | |
487 gtk_tree_path_free(tpath); | |
488 *y += ch; | |
489 popup_menu_position_clamp(menu, x, y, 0); | |
490 } | |
491 | |
492 static gint vflist_press_key_cb(GtkWidget *widget, GdkEventKey *event, gpointer data) | |
493 { | |
494 ViewFileList *vfl = data; | |
495 GtkTreePath *tpath; | |
496 | |
497 if (event->keyval != GDK_Menu) return FALSE; | |
498 | |
499 gtk_tree_view_get_cursor(GTK_TREE_VIEW(vfl->listview), &tpath, NULL); | |
500 if (tpath) | |
501 { | |
502 GtkTreeModel *store; | |
503 GtkTreeIter iter; | |
504 | |
505 store = gtk_tree_view_get_model(GTK_TREE_VIEW(widget)); | |
506 gtk_tree_model_get_iter(store, &iter, tpath); | |
507 gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &vfl->click_fd, -1); | |
508 gtk_tree_path_free(tpath); | |
509 } | |
510 else | |
511 { | |
512 vfl->click_fd = NULL; | |
513 } | |
514 | |
515 vfl->popup = vflist_pop_menu(vfl, vfl->click_fd); | |
516 gtk_menu_popup(GTK_MENU(vfl->popup), NULL, NULL, vflist_menu_position_cb, vfl, 0, GDK_CURRENT_TIME); | |
517 | |
518 return TRUE; | |
519 } | |
520 | |
521 static gint vflist_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data) | |
522 { | |
523 ViewFileList *vfl = data; | |
524 GtkTreePath *tpath; | |
525 GtkTreeIter iter; | |
526 FileData *fd = NULL; | |
527 | |
528 if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget), bevent->x, bevent->y, | |
529 &tpath, NULL, NULL, NULL)) | |
530 { | |
531 GtkTreeModel *store; | |
532 | |
533 store = gtk_tree_view_get_model(GTK_TREE_VIEW(widget)); | |
534 gtk_tree_model_get_iter(store, &iter, tpath); | |
535 gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &fd, -1); | |
536 #if 0 | |
537 gtk_tree_view_set_cursor(GTK_TREE_VIEW(widget), tpath, NULL, FALSE); | |
538 #endif | |
539 gtk_tree_path_free(tpath); | |
540 } | |
541 | |
542 vfl->click_fd = fd; | |
543 | |
544 if (bevent->button == 3) | |
545 { | |
546 vfl->popup = vflist_pop_menu(vfl, vfl->click_fd); | |
547 gtk_menu_popup(GTK_MENU(vfl->popup), NULL, NULL, NULL, NULL, | |
548 bevent->button, bevent->time); | |
549 return TRUE; | |
550 } | |
551 | |
552 if (!fd) return FALSE; | |
553 | |
554 if (bevent->button == 2) | |
555 { | |
556 if (!vflist_row_is_selected(vfl, fd)) | |
557 { | |
558 vflist_color_set(vfl, fd, TRUE); | |
559 } | |
560 return TRUE; | |
561 } | |
562 | |
563 | |
564 if (bevent->button == 1 && bevent->type == GDK_BUTTON_PRESS && | |
565 !(bevent->state & GDK_SHIFT_MASK ) && | |
566 !(bevent->state & GDK_CONTROL_MASK ) && | |
567 vflist_row_is_selected(vfl, fd)) | |
568 { | |
569 gtk_widget_grab_focus(widget); | |
570 return TRUE; | |
571 } | |
572 | |
573 #if 0 | |
574 if (bevent->button == 1 && bevent->type == GDK_2BUTTON_PRESS) | |
575 { | |
576 if (vfl->layout) layout_image_full_screen_start(vfl->layout); | |
577 } | |
578 #endif | |
579 | |
580 return FALSE; | |
581 } | |
582 | |
583 static gint vflist_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data) | |
584 { | |
585 ViewFileList *vfl = data; | |
586 GtkTreePath *tpath; | |
587 GtkTreeIter iter; | |
588 FileData *fd = NULL; | |
589 | |
590 if (bevent->button == 2) | |
591 { | |
592 vflist_color_set(vfl, vfl->click_fd, FALSE); | |
593 } | |
594 | |
595 if (bevent->button != 1 && bevent->button != 2) | |
596 { | |
597 return TRUE; | |
598 } | |
599 | |
600 if ((bevent->x != 0 || bevent->y != 0) && | |
601 gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget), bevent->x, bevent->y, | |
602 &tpath, NULL, NULL, NULL)) | |
603 { | |
604 GtkTreeModel *store; | |
605 | |
606 store = gtk_tree_view_get_model(GTK_TREE_VIEW(widget)); | |
607 gtk_tree_model_get_iter(store, &iter, tpath); | |
608 gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &fd, -1); | |
609 gtk_tree_path_free(tpath); | |
610 } | |
611 | |
612 if (bevent->button == 2) | |
613 { | |
614 if (fd && vfl->click_fd == fd) | |
615 { | |
616 GtkTreeSelection *selection; | |
617 | |
618 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget)); | |
619 if (vflist_row_is_selected(vfl, fd)) | |
620 { | |
621 gtk_tree_selection_unselect_iter(selection, &iter); | |
622 } | |
623 else | |
624 { | |
625 gtk_tree_selection_select_iter(selection, &iter); | |
626 } | |
627 } | |
628 return TRUE; | |
629 } | |
630 | |
631 if (fd && vfl->click_fd == fd && | |
632 !(bevent->state & GDK_SHIFT_MASK ) && | |
633 !(bevent->state & GDK_CONTROL_MASK ) && | |
634 vflist_row_is_selected(vfl, fd)) | |
635 { | |
636 GtkTreeSelection *selection; | |
637 | |
638 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(widget)); | |
639 gtk_tree_selection_unselect_all(selection); | |
640 gtk_tree_selection_select_iter(selection, &iter); | |
641 vflist_move_cursor(vfl, &iter); | |
642 return TRUE; | |
643 } | |
644 | |
645 return FALSE; | |
646 } | |
647 | |
648 static void vflist_select_image(ViewFileList *vfl, gint row) | |
649 { | |
650 const gchar *path; | |
651 const gchar *read_ahead_path = NULL; | |
652 | |
653 path = vflist_index_get_path(vfl, row); | |
654 if (!path) return; | |
655 | |
656 if (path && enable_read_ahead) | |
657 { | |
658 FileData *fd; | |
659 if (row > vflist_index_by_path(vfl, layout_image_get_path(vfl->layout)) && | |
660 row + 1 < vflist_count(vfl, NULL)) | |
661 { | |
662 fd = vflist_index_get_data(vfl, row + 1); | |
663 } | |
664 else if (row > 0) | |
665 { | |
666 fd = vflist_index_get_data(vfl, row - 1); | |
667 } | |
668 else | |
669 { | |
670 fd = NULL; | |
671 } | |
672 if (fd) read_ahead_path = fd->path; | |
673 } | |
674 | |
675 layout_image_set_with_ahead(vfl->layout, path, read_ahead_path); | |
676 } | |
677 | |
678 static gint vflist_select_idle_cb(gpointer data) | |
679 { | |
680 ViewFileList *vfl = data; | |
681 | |
682 if (!vfl->layout) | |
683 { | |
684 vfl->select_idle_id = -1; | |
685 return FALSE; | |
686 } | |
687 | |
688 vflist_send_update(vfl); | |
689 | |
690 if (vfl->select_fd) | |
691 { | |
692 vflist_select_image(vfl, g_list_index(vfl->list, vfl->select_fd)); | |
693 vfl->select_fd = NULL; | |
694 } | |
695 | |
696 vfl->select_idle_id = -1; | |
697 return FALSE; | |
698 } | |
699 | |
700 static void vflist_select_idle_cancel(ViewFileList *vfl) | |
701 { | |
702 if (vfl->select_idle_id != -1) g_source_remove(vfl->select_idle_id); | |
703 vfl->select_idle_id = -1; | |
704 } | |
705 | |
706 static gboolean vflist_select_cb(GtkTreeSelection *selection, GtkTreeModel *store, GtkTreePath *tpath, | |
707 gboolean path_currently_selected, gpointer data) | |
708 { | |
709 ViewFileList *vfl = data; | |
710 GtkTreeIter iter; | |
711 | |
712 if (!path_currently_selected && | |
713 gtk_tree_model_get_iter(store, &iter, tpath)) | |
714 { | |
715 gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &vfl->select_fd, -1); | |
716 } | |
717 else | |
718 { | |
719 vfl->select_fd = NULL; | |
720 } | |
721 | |
722 if (vfl->layout && | |
723 vfl->select_idle_id == -1) | |
724 { | |
725 vfl->select_idle_id = g_idle_add(vflist_select_idle_cb, vfl); | |
726 } | |
727 | |
728 return TRUE; | |
729 } | |
730 | |
731 /* | |
732 *----------------------------------------------------------------------------- | |
733 * misc | |
734 *----------------------------------------------------------------------------- | |
735 */ | |
736 | |
737 void vflist_sort_set(ViewFileList *vfl, SortType type, gint ascend) | |
738 { | |
739 GtkListStore *store; | |
740 GList *work; | |
741 | |
742 if (vfl->sort_method == type && vfl->sort_ascend == ascend) return; | |
743 | |
744 vfl->sort_method = type; | |
745 vfl->sort_ascend = ascend; | |
746 | |
747 if (!vfl->list) return; | |
748 | |
749 vfl->list = filelist_sort(vfl->list, vfl->sort_method, vfl->sort_ascend); | |
750 | |
751 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(vfl->listview))); | |
752 | |
753 /* reorder the treeview, maintaining current selection */ | |
754 work = g_list_last(vfl->list); | |
755 while (work) | |
756 { | |
757 FileData *fd; | |
758 GtkTreeIter iter; | |
759 | |
760 fd = work->data; | |
761 if (vflist_find_row(vfl, fd, &iter) >= 0) | |
762 { | |
763 gtk_list_store_move_after(store, &iter, NULL); | |
764 } | |
765 | |
766 work = work->prev; | |
767 } | |
768 } | |
769 | |
770 /* | |
771 *----------------------------------------------------------------------------- | |
772 * thumb updates | |
773 *----------------------------------------------------------------------------- | |
774 */ | |
775 | |
776 static gint vflist_thumb_next(ViewFileList *vfl); | |
777 | |
778 static void vflist_thumb_status(ViewFileList *vfl, gdouble val, const gchar *text) | |
779 { | |
780 if (vfl->func_thumb_status) | |
781 { | |
782 vfl->func_thumb_status(vfl, val, text, vfl->data_thumb_status); | |
783 } | |
784 } | |
785 | |
786 static void vflist_thumb_cleanup(ViewFileList *vfl) | |
787 { | |
788 vflist_thumb_status(vfl, 0.0, NULL); | |
789 | |
790 vfl->thumbs_count = 0; | |
791 vfl->thumbs_running = FALSE; | |
792 | |
793 thumb_loader_free(vfl->thumbs_loader); | |
794 vfl->thumbs_loader = NULL; | |
795 | |
796 vfl->thumbs_filedata = NULL; | |
797 } | |
798 | |
799 static void vflist_thumb_stop(ViewFileList *vfl) | |
800 { | |
801 if (vfl->thumbs_running) vflist_thumb_cleanup(vfl); | |
802 } | |
803 | |
804 static void vflist_thumb_do(ViewFileList *vfl, ThumbLoader *tl, FileData *fd) | |
805 { | |
806 GtkListStore *store; | |
807 GtkTreeIter iter; | |
808 | |
809 if (!fd || vflist_find_row(vfl, fd, &iter) < 0) return; | |
810 | |
811 if (fd->pixbuf) g_object_unref(fd->pixbuf); | |
812 fd->pixbuf = thumb_loader_get_pixbuf(tl, TRUE); | |
813 | |
814 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(vfl->listview))); | |
815 gtk_list_store_set(store, &iter, FILE_COLUMN_THUMB, fd->pixbuf, -1); | |
816 | |
817 vflist_thumb_status(vfl, (gdouble)(vfl->thumbs_count) / g_list_length(vfl->list), _("Loading thumbs...")); | |
818 } | |
819 | |
820 static void vflist_thumb_error_cb(ThumbLoader *tl, gpointer data) | |
821 { | |
822 ViewFileList *vfl = data; | |
823 | |
824 if (vfl->thumbs_filedata && vfl->thumbs_loader == tl) | |
825 { | |
826 vflist_thumb_do(vfl, tl, vfl->thumbs_filedata); | |
827 } | |
828 | |
829 while (vflist_thumb_next(vfl)); | |
830 } | |
831 | |
832 static void vflist_thumb_done_cb(ThumbLoader *tl, gpointer data) | |
833 { | |
834 ViewFileList *vfl = data; | |
835 | |
836 if (vfl->thumbs_filedata && vfl->thumbs_loader == tl) | |
837 { | |
838 vflist_thumb_do(vfl, tl, vfl->thumbs_filedata); | |
839 } | |
840 | |
841 while (vflist_thumb_next(vfl)); | |
842 } | |
843 | |
844 static gint vflist_thumb_next(ViewFileList *vfl) | |
845 { | |
846 GtkTreePath *tpath; | |
847 FileData *fd = NULL; | |
848 | |
849 /* first check the visible files */ | |
850 | |
851 if (GTK_WIDGET_REALIZED(vfl->listview) && | |
852 gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(vfl->listview), 0, 0, &tpath, NULL, NULL, NULL)) | |
853 { | |
854 GtkTreeModel *store; | |
855 GtkTreeIter iter; | |
856 gint valid = TRUE; | |
857 | |
858 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vfl->listview)); | |
859 gtk_tree_model_get_iter(store, &iter, tpath); | |
860 gtk_tree_path_free(tpath); | |
861 | |
862 while (!fd && valid && tree_view_row_get_visibility(GTK_TREE_VIEW(vfl->listview), &iter, FALSE) == 0) | |
863 { | |
864 gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &fd, -1); | |
865 if (fd->pixbuf) fd = NULL; | |
866 | |
867 valid = gtk_tree_model_iter_next(store, &iter); | |
868 } | |
869 } | |
870 | |
871 /* then find first undone */ | |
872 | |
873 if (!fd) | |
874 { | |
875 GList *work = vfl->list; | |
876 while (work && !fd) | |
877 { | |
878 FileData *fd_p = work->data; | |
879 work = work->next; | |
880 | |
881 if (!fd_p->pixbuf) fd = fd_p; | |
882 } | |
883 } | |
884 | |
885 if (!fd) | |
886 { | |
887 /* done */ | |
888 vflist_thumb_cleanup(vfl); | |
889 return FALSE; | |
890 } | |
891 | |
892 vfl->thumbs_count++; | |
893 | |
894 vfl->thumbs_filedata = fd; | |
895 | |
896 thumb_loader_free(vfl->thumbs_loader); | |
897 | |
898 vfl->thumbs_loader = thumb_loader_new(thumb_max_width, thumb_max_height); | |
899 thumb_loader_set_callbacks(vfl->thumbs_loader, | |
900 vflist_thumb_done_cb, | |
901 vflist_thumb_error_cb, | |
902 NULL, | |
903 vfl); | |
904 | |
905 if (!thumb_loader_start(vfl->thumbs_loader, fd->path)) | |
906 { | |
907 /* set icon to unknown, continue */ | |
908 if (debug) printf("thumb loader start failed %s\n", vfl->thumbs_loader->path); | |
909 vflist_thumb_do(vfl, vfl->thumbs_loader, fd); | |
910 | |
911 return TRUE; | |
912 } | |
913 | |
914 return FALSE; | |
915 } | |
916 | |
917 static void vflist_thumb_update(ViewFileList *vfl) | |
918 { | |
919 vflist_thumb_stop(vfl); | |
920 if (!vfl->thumbs_enabled) return; | |
921 | |
922 vflist_thumb_status(vfl, 0.0, _("Loading thumbs...")); | |
923 vfl->thumbs_running = TRUE; | |
924 | |
925 while (vflist_thumb_next(vfl)); | |
926 } | |
927 | |
928 /* | |
929 *----------------------------------------------------------------------------- | |
930 * row stuff | |
931 *----------------------------------------------------------------------------- | |
932 */ | |
933 | |
934 FileData *vflist_index_get_data(ViewFileList *vfl, gint row) | |
935 { | |
936 return g_list_nth_data(vfl->list, row); | |
937 } | |
938 | |
939 gchar *vflist_index_get_path(ViewFileList *vfl, gint row) | |
940 { | |
941 FileData *fd; | |
942 | |
943 fd = g_list_nth_data(vfl->list, row); | |
944 | |
945 return (fd ? fd->path : NULL); | |
946 } | |
947 | |
948 static gint vflist_row_by_path(ViewFileList *vfl, const gchar *path, FileData **fd) | |
949 { | |
950 gint p = 0; | |
951 GList *work; | |
952 | |
953 if (!path) return -1; | |
954 | |
955 work = vfl->list; | |
956 while (work) | |
957 { | |
958 FileData *fd_n = work->data; | |
959 if (strcmp(path, fd_n->path) == 0) | |
960 { | |
961 if (fd) *fd = fd_n; | |
962 return p; | |
963 } | |
964 work = work->next; | |
965 p++; | |
966 } | |
967 | |
968 if (fd) *fd = NULL; | |
969 return -1; | |
970 } | |
971 | |
972 gint vflist_index_by_path(ViewFileList *vfl, const gchar *path) | |
973 { | |
974 return vflist_row_by_path(vfl, path, NULL); | |
975 } | |
976 | |
977 gint vflist_count(ViewFileList *vfl, gint64 *bytes) | |
978 { | |
979 if (bytes) | |
980 { | |
981 gint64 b = 0; | |
982 GList *work; | |
983 | |
984 work = vfl->list; | |
985 while (work) | |
986 { | |
987 FileData *fd = work->data; | |
988 work = work->next; | |
989 b += fd->size; | |
990 } | |
991 | |
992 *bytes = b; | |
993 } | |
994 | |
995 return g_list_length(vfl->list); | |
996 } | |
997 | |
998 GList *vflist_get_list(ViewFileList *vfl) | |
999 { | |
1000 GList *list = NULL; | |
1001 GList *work; | |
1002 | |
1003 work = vfl->list; | |
1004 while (work) | |
1005 { | |
1006 FileData *fd = work->data; | |
1007 work = work->next; | |
1008 | |
1009 list = g_list_prepend(list, g_strdup(fd->path)); | |
1010 } | |
1011 | |
1012 return g_list_reverse(list); | |
1013 } | |
1014 | |
1015 /* | |
1016 *----------------------------------------------------------------------------- | |
1017 * selections | |
1018 *----------------------------------------------------------------------------- | |
1019 */ | |
1020 | |
1021 static gint vflist_row_is_selected(ViewFileList *vfl, FileData *fd) | |
1022 { | |
1023 GtkTreeModel *store; | |
1024 GtkTreeSelection *selection; | |
1025 GList *slist; | |
1026 GList *work; | |
1027 gint found = FALSE; | |
1028 | |
1029 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(vfl->listview)); | |
1030 slist = gtk_tree_selection_get_selected_rows(selection, &store); | |
1031 work = slist; | |
1032 while (!found && work) | |
1033 { | |
1034 GtkTreePath *tpath = work->data; | |
1035 FileData *fd_n; | |
1036 GtkTreeIter iter; | |
1037 | |
1038 gtk_tree_model_get_iter(store, &iter, tpath); | |
1039 gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &fd_n, -1); | |
1040 if (fd_n == fd) found = TRUE; | |
1041 work = work->next; | |
1042 } | |
1043 g_list_foreach(slist, (GFunc)gtk_tree_path_free, NULL); | |
1044 g_list_free(slist); | |
1045 | |
1046 return found; | |
1047 } | |
1048 | |
1049 gint vflist_index_is_selected(ViewFileList *vfl, gint row) | |
1050 { | |
1051 FileData *fd; | |
1052 | |
1053 fd = vflist_index_get_data(vfl, row); | |
1054 return vflist_row_is_selected(vfl, fd); | |
1055 } | |
1056 | |
1057 gint vflist_selection_count(ViewFileList *vfl, gint64 *bytes) | |
1058 { | |
1059 GtkTreeModel *store; | |
1060 GtkTreeSelection *selection; | |
1061 GList *slist; | |
1062 gint count; | |
1063 | |
1064 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(vfl->listview)); | |
1065 slist = gtk_tree_selection_get_selected_rows(selection, &store); | |
1066 | |
1067 if (bytes) | |
1068 { | |
1069 gint64 b = 0; | |
1070 GList *work; | |
1071 | |
1072 work = slist; | |
1073 while (work) | |
1074 { | |
1075 GtkTreePath *tpath = work->data; | |
1076 GtkTreeIter iter; | |
1077 FileData *fd; | |
1078 | |
1079 gtk_tree_model_get_iter(store, &iter, tpath); | |
1080 gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &fd, -1); | |
1081 b += fd->size; | |
1082 | |
1083 work = work->next; | |
1084 } | |
1085 | |
1086 *bytes = b; | |
1087 } | |
1088 | |
1089 count = g_list_length(slist); | |
1090 g_list_foreach(slist, (GFunc)gtk_tree_path_free, NULL); | |
1091 g_list_free(slist); | |
1092 | |
1093 return count; | |
1094 } | |
1095 | |
1096 GList *vflist_selection_get_list(ViewFileList *vfl) | |
1097 { | |
1098 GtkTreeModel *store; | |
1099 GtkTreeSelection *selection; | |
1100 GList *slist; | |
1101 GList *list = NULL; | |
1102 GList *work; | |
1103 | |
1104 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(vfl->listview)); | |
1105 slist = gtk_tree_selection_get_selected_rows(selection, &store); | |
1106 work = slist; | |
1107 while (work) | |
1108 { | |
1109 GtkTreePath *tpath = work->data; | |
1110 FileData *fd; | |
1111 GtkTreeIter iter; | |
1112 | |
1113 gtk_tree_model_get_iter(store, &iter, tpath); | |
1114 gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &fd, -1); | |
1115 | |
1116 list = g_list_prepend(list, g_strdup(fd->path)); | |
1117 | |
1118 work = work->next; | |
1119 } | |
1120 g_list_foreach(slist, (GFunc)gtk_tree_path_free, NULL); | |
1121 g_list_free(slist); | |
1122 | |
1123 return g_list_reverse(list); | |
1124 } | |
1125 | |
1126 GList *vflist_selection_get_list_by_index(ViewFileList *vfl) | |
1127 { | |
1128 GtkTreeModel *store; | |
1129 GtkTreeSelection *selection; | |
1130 GList *slist; | |
1131 GList *list = NULL; | |
1132 GList *work; | |
1133 | |
1134 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(vfl->listview)); | |
1135 slist = gtk_tree_selection_get_selected_rows(selection, &store); | |
1136 work = slist; | |
1137 while (work) | |
1138 { | |
1139 GtkTreePath *tpath = work->data; | |
1140 FileData *fd; | |
1141 GtkTreeIter iter; | |
1142 | |
1143 gtk_tree_model_get_iter(store, &iter, tpath); | |
1144 gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &fd, -1); | |
1145 | |
1146 list = g_list_prepend(list, GINT_TO_POINTER(g_list_index(vfl->list, fd))); | |
1147 | |
1148 work = work->next; | |
1149 } | |
1150 g_list_foreach(slist, (GFunc)gtk_tree_path_free, NULL); | |
1151 g_list_free(slist); | |
1152 | |
1153 return g_list_reverse(list); | |
1154 } | |
1155 | |
1156 void vflist_select_all(ViewFileList *vfl) | |
1157 { | |
1158 GtkTreeSelection *selection; | |
1159 | |
1160 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(vfl->listview)); | |
1161 gtk_tree_selection_select_all(selection); | |
1162 | |
1163 vfl->select_fd = NULL; | |
1164 } | |
1165 | |
1166 void vflist_select_none(ViewFileList *vfl) | |
1167 { | |
1168 GtkTreeSelection *selection; | |
1169 | |
1170 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(vfl->listview)); | |
1171 gtk_tree_selection_unselect_all(selection); | |
1172 } | |
1173 | |
1174 void vflist_select_by_path(ViewFileList *vfl, const gchar *path) | |
1175 { | |
1176 FileData *fd; | |
1177 GtkTreeIter iter; | |
1178 | |
1179 if (vflist_row_by_path(vfl, path, &fd) < 0) return; | |
1180 if (vflist_find_row(vfl, fd, &iter) < 0) return; | |
1181 | |
1182 tree_view_row_make_visible(GTK_TREE_VIEW(vfl->listview), &iter, TRUE); | |
1183 | |
1184 if (!vflist_row_is_selected(vfl, fd)) | |
1185 { | |
1186 GtkTreeSelection *selection; | |
1187 GtkTreeModel *store; | |
1188 GtkTreePath *tpath; | |
1189 | |
1190 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(vfl->listview)); | |
1191 gtk_tree_selection_unselect_all(selection); | |
1192 gtk_tree_selection_select_iter(selection, &iter); | |
1193 vflist_move_cursor(vfl, &iter); | |
1194 | |
1195 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vfl->listview)); | |
1196 tpath = gtk_tree_model_get_path(store, &iter); | |
1197 gtk_tree_view_set_cursor(GTK_TREE_VIEW(vfl->listview), tpath, NULL, FALSE); | |
1198 gtk_tree_path_free(tpath); | |
1199 } | |
1200 } | |
1201 | |
1202 /* | |
1203 *----------------------------------------------------------------------------- | |
1204 * core (population) | |
1205 *----------------------------------------------------------------------------- | |
1206 */ | |
1207 | |
1208 static void vflist_listview_set_height(GtkWidget *listview, gint thumb) | |
1209 { | |
1210 GtkTreeViewColumn *column; | |
1211 GtkCellRenderer *cell; | |
1212 GList *list; | |
1213 | |
1214 column = gtk_tree_view_get_column(GTK_TREE_VIEW(listview), FILE_COLUMN_THUMB - 1); | |
1215 if (!column) return; | |
1216 | |
1217 gtk_tree_view_column_set_fixed_width(column, (thumb) ? thumb_max_width : 4); | |
1218 | |
1219 list = gtk_tree_view_column_get_cell_renderers(column); | |
1220 if (!list) return; | |
1221 cell = list->data; | |
1222 g_list_free(list); | |
1223 | |
1224 g_object_set(G_OBJECT(cell), "height", (thumb) ? thumb_max_height : -1, NULL); | |
1225 gtk_tree_view_columns_autosize(GTK_TREE_VIEW(listview)); | |
1226 } | |
1227 | |
1228 static void vflist_populate_view(ViewFileList *vfl) | |
1229 { | |
1230 GtkListStore *store; | |
1231 GtkTreeIter iter; | |
1232 gint thumbs; | |
1233 GList *work; | |
1234 GtkTreeRowReference *visible_row = NULL; | |
1235 GtkTreePath *tpath; | |
1236 gint valid; | |
1237 | |
1238 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(vfl->listview))); | |
1239 thumbs = vfl->thumbs_enabled; | |
1240 | |
1241 vflist_thumb_stop(vfl); | |
1242 | |
1243 if (!vfl->list) | |
1244 { | |
1245 gtk_list_store_clear(store); | |
1246 vflist_send_update(vfl); | |
1247 return; | |
1248 } | |
1249 | |
1250 if (GTK_WIDGET_REALIZED(vfl->listview) && | |
1251 gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(vfl->listview), 0, 0, &tpath, NULL, NULL, NULL)) | |
1252 { | |
1253 visible_row = gtk_tree_row_reference_new(GTK_TREE_MODEL(store), tpath); | |
1254 gtk_tree_path_free(tpath); | |
1255 } | |
1256 | |
1257 vflist_listview_set_height(vfl->listview, thumbs); | |
1258 | |
1259 valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter); | |
1260 | |
1261 work = vfl->list; | |
1262 while (work) | |
1263 { | |
1264 gint match; | |
1265 FileData *fd = work->data; | |
1266 gint done = FALSE; | |
1267 | |
1268 while (!done) | |
1269 { | |
1270 FileData *old_fd = NULL; | |
1271 | |
1272 if (valid) | |
1273 { | |
1274 gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, FILE_COLUMN_POINTER, &old_fd, -1); | |
1275 match = CASE_SORT(fd->name, old_fd->name); | |
1276 } | |
1277 else | |
1278 { | |
1279 match = -1; | |
1280 } | |
1281 | |
1282 if (match < 0) | |
1283 { | |
1284 GtkTreeIter new; | |
1285 gchar *size; | |
1286 | |
1287 size = text_from_size(fd->size); | |
1288 if (valid) | |
1289 { | |
1290 gtk_list_store_insert_before(store, &new, &iter); | |
1291 } | |
1292 else | |
1293 { | |
1294 gtk_list_store_append(store, &new); | |
1295 } | |
1296 gtk_list_store_set(store, &new, FILE_COLUMN_POINTER, fd, | |
1297 FILE_COLUMN_THUMB, (thumbs) ? fd->pixbuf : NULL, | |
1298 FILE_COLUMN_NAME, fd->name, | |
1299 FILE_COLUMN_SIZE, size, | |
1300 FILE_COLUMN_DATE, text_from_time(fd->date), | |
1301 FILE_COLUMN_COLOR, FALSE, -1); | |
1302 g_free(size); | |
1303 | |
1304 done = TRUE; | |
1305 } | |
1306 else if (match > 0) | |
1307 { | |
1308 valid = gtk_list_store_remove(store, &iter); | |
1309 } | |
1310 else | |
1311 { | |
1312 gtk_list_store_set(store, &iter, FILE_COLUMN_POINTER, fd, -1); | |
1313 if (fd->date != old_fd->date) | |
1314 { | |
1315 gchar *size; | |
1316 | |
1317 /* update, file changed */ | |
1318 size = text_from_size(fd->size); | |
1319 gtk_list_store_set(store, &iter, FILE_COLUMN_SIZE, size, | |
1320 FILE_COLUMN_DATE, text_from_time(fd->date), -1); | |
1321 g_free(size); | |
1322 } | |
1323 else if (fd != old_fd) | |
1324 { | |
1325 /* preserve thumbnail */ | |
1326 if (fd->pixbuf) g_object_unref(fd->pixbuf); | |
1327 fd->pixbuf = old_fd->pixbuf; | |
1328 if (fd->pixbuf) g_object_ref(fd->pixbuf); | |
1329 } | |
1330 | |
1331 gtk_list_store_set(store, &iter, FILE_COLUMN_THUMB, (thumbs) ? fd->pixbuf : NULL, -1); | |
1332 | |
1333 if (vfl->select_fd == old_fd) vfl->select_fd = fd; | |
1334 | |
1335 if (valid) valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter); | |
1336 | |
1337 done = TRUE; | |
1338 } | |
1339 } | |
1340 work = work->next; | |
1341 } | |
1342 | |
1343 while (valid) | |
1344 { | |
1345 valid = gtk_list_store_remove(store, &iter); | |
1346 } | |
1347 | |
1348 if (visible_row) | |
1349 { | |
1350 if (gtk_tree_row_reference_valid(visible_row)) | |
1351 { | |
1352 tpath = gtk_tree_row_reference_get_path(visible_row); | |
1353 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(vfl->listview), tpath, NULL, TRUE, 0.0, 0.0); | |
1354 gtk_tree_path_free(tpath); | |
1355 } | |
1356 gtk_tree_row_reference_free(visible_row); | |
1357 } | |
1358 | |
1359 vflist_send_update(vfl); | |
1360 vflist_thumb_update(vfl); | |
1361 } | |
1362 | |
1363 gint vflist_refresh(ViewFileList *vfl) | |
1364 { | |
1365 GList *old_list; | |
1366 gint ret = TRUE; | |
1367 | |
1368 old_list = vfl->list; | |
1369 vfl->list = NULL; | |
1370 | |
1371 if (vfl->path) | |
1372 { | |
1373 ret = filelist_read(vfl->path, &vfl->list, NULL); | |
1374 } | |
1375 | |
1376 vfl->list = filelist_sort(vfl->list, vfl->sort_method, vfl->sort_ascend); | |
1377 vflist_populate_view(vfl); | |
1378 | |
1379 filelist_free(old_list); | |
1380 | |
1381 return ret; | |
1382 } | |
1383 | |
1384 /* this overrides the low default of a GtkCellRenderer from 100 to CELL_HEIGHT_OVERRIDE, something sane for our purposes */ | |
1385 | |
1386 #define CELL_HEIGHT_OVERRIDE 512 | |
1387 | |
1388 static void cell_renderer_height_override(GtkCellRenderer *renderer) | |
1389 { | |
1390 GParamSpec *spec; | |
1391 | |
1392 spec = g_object_class_find_property(G_OBJECT_GET_CLASS(G_OBJECT(renderer)), "height"); | |
1393 if (spec && G_IS_PARAM_SPEC_INT(spec)) | |
1394 { | |
1395 GParamSpecInt *spec_int; | |
1396 | |
1397 spec_int = G_PARAM_SPEC_INT(spec); | |
1398 if (spec_int->maximum < CELL_HEIGHT_OVERRIDE) spec_int->maximum = CELL_HEIGHT_OVERRIDE; | |
1399 } | |
1400 } | |
1401 | |
1402 static GdkColor *vflist_listview_color_shifted(GtkWidget *widget) | |
1403 { | |
1404 static GdkColor color; | |
1405 static GtkWidget *done = NULL; | |
1406 | |
1407 if (done != widget) | |
1408 { | |
1409 GtkStyle *style; | |
1410 | |
1411 style = gtk_widget_get_style(widget); | |
1412 memcpy(&color, &style->base[GTK_STATE_NORMAL], sizeof(color)); | |
1413 shift_color(&color, -1, 0); | |
1414 done = widget; | |
1415 } | |
1416 | |
1417 return &color; | |
1418 } | |
1419 | |
1420 static void vflist_listview_color_cb(GtkTreeViewColumn *tree_column, GtkCellRenderer *cell, | |
1421 GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data) | |
1422 { | |
1423 ViewFileList *vfl = data; | |
1424 gboolean set; | |
1425 | |
1426 gtk_tree_model_get(tree_model, iter, FILE_COLUMN_COLOR, &set, -1); | |
1427 g_object_set(G_OBJECT(cell), | |
1428 "cell-background-gdk", vflist_listview_color_shifted(vfl->listview), | |
1429 "cell-background-set", set, NULL); | |
1430 } | |
1431 | |
1432 static void vflist_listview_add_column(ViewFileList *vfl, gint n, const gchar *title, gint image, gint right_justify) | |
1433 { | |
1434 GtkTreeViewColumn *column; | |
1435 GtkCellRenderer *renderer; | |
1436 | |
1437 column = gtk_tree_view_column_new(); | |
1438 gtk_tree_view_column_set_title(column, title); | |
1439 gtk_tree_view_column_set_min_width(column, 4); | |
1440 | |
1441 if (!image) | |
1442 { | |
1443 gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_GROW_ONLY); | |
1444 renderer = gtk_cell_renderer_text_new(); | |
1445 if (right_justify) | |
1446 { | |
1447 g_object_set(G_OBJECT(renderer), "xalign", 1.0, NULL); | |
1448 } | |
1449 gtk_tree_view_column_pack_start(column, renderer, TRUE); | |
1450 gtk_tree_view_column_add_attribute(column, renderer, "text", n); | |
1451 } | |
1452 else | |
1453 { | |
1454 gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED); | |
1455 renderer = gtk_cell_renderer_pixbuf_new(); | |
1456 cell_renderer_height_override(renderer); | |
1457 gtk_tree_view_column_pack_start(column, renderer, TRUE); | |
1458 gtk_tree_view_column_add_attribute(column, renderer, "pixbuf", n); | |
1459 } | |
1460 | |
1461 gtk_tree_view_column_set_cell_data_func(column, renderer, vflist_listview_color_cb, vfl, NULL); | |
1462 | |
1463 gtk_tree_view_append_column(GTK_TREE_VIEW(vfl->listview), column); | |
1464 } | |
1465 | |
1466 /* | |
1467 *----------------------------------------------------------------------------- | |
1468 * base | |
1469 *----------------------------------------------------------------------------- | |
1470 */ | |
1471 | |
1472 gint vflist_set_path(ViewFileList *vfl, const gchar *path) | |
1473 { | |
1474 GtkListStore *store; | |
1475 | |
1476 if (!path) return FALSE; | |
1477 if (vfl->path && strcmp(path, vfl->path) == 0) return TRUE; | |
1478 | |
1479 g_free(vfl->path); | |
1480 vfl->path = g_strdup(path); | |
1481 | |
1482 /* force complete reload */ | |
1483 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(vfl->listview))); | |
1484 gtk_list_store_clear(store); | |
1485 | |
1486 filelist_free(vfl->list); | |
1487 vfl->list = NULL; | |
1488 | |
1489 return vflist_refresh(vfl); | |
1490 } | |
1491 | |
1492 static void vflist_destroy_cb(GtkWidget *widget, gpointer data) | |
1493 { | |
1494 ViewFileList *vfl = data; | |
1495 | |
1496 if (vfl->popup) | |
1497 { | |
1498 g_signal_handlers_disconnect_matched(G_OBJECT(vfl->popup), G_SIGNAL_MATCH_DATA, | |
1499 0, 0, 0, NULL, vfl); | |
1500 gtk_widget_destroy(vfl->popup); | |
1501 } | |
1502 | |
1503 vflist_select_idle_cancel(vfl); | |
1504 vflist_thumb_stop(vfl); | |
1505 | |
1506 g_free(vfl->path); | |
1507 filelist_free(vfl->list); | |
1508 g_free(vfl); | |
1509 } | |
1510 | |
1511 ViewFileList *vflist_new(const gchar *path, gint thumbs) | |
1512 { | |
1513 ViewFileList *vfl; | |
1514 GtkListStore *store; | |
1515 GtkTreeSelection *selection; | |
1516 | |
1517 vfl = g_new0(ViewFileList, 1); | |
1518 | |
1519 vfl->path = NULL; | |
1520 vfl->list = NULL; | |
1521 vfl->click_fd = NULL; | |
1522 vfl->select_fd = NULL; | |
1523 vfl->sort_method = SORT_NAME; | |
1524 vfl->sort_ascend = TRUE; | |
1525 vfl->thumbs_enabled = thumbs; | |
1526 | |
1527 vfl->thumbs_running = FALSE; | |
1528 vfl->thumbs_count = 0; | |
1529 vfl->thumbs_loader = NULL; | |
1530 vfl->thumbs_filedata = NULL; | |
1531 | |
1532 vfl->select_idle_id = -1; | |
1533 | |
1534 vfl->popup = NULL; | |
1535 | |
1536 vfl->widget = gtk_scrolled_window_new(NULL, NULL); | |
1537 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(vfl->widget), GTK_SHADOW_IN); | |
1538 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(vfl->widget), | |
1539 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS); | |
1540 g_signal_connect(G_OBJECT(vfl->widget), "destroy", | |
1541 G_CALLBACK(vflist_destroy_cb), vfl); | |
1542 | |
1543 store = gtk_list_store_new(6, G_TYPE_POINTER, GDK_TYPE_PIXBUF, G_TYPE_STRING, | |
1544 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_BOOLEAN); | |
1545 vfl->listview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store)); | |
1546 g_object_unref(store); | |
1547 | |
1548 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(vfl->listview)); | |
1549 gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_MULTIPLE); | |
1550 gtk_tree_selection_set_select_function(selection, vflist_select_cb, vfl, NULL); | |
1551 | |
1552 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(vfl->listview), FALSE); | |
1553 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(vfl->listview), FALSE); | |
1554 | |
1555 vflist_listview_add_column(vfl, FILE_COLUMN_THUMB, "", TRUE, FALSE); | |
1556 vflist_listview_add_column(vfl, FILE_COLUMN_NAME, _("Name"), FALSE, FALSE); | |
1557 vflist_listview_add_column(vfl, FILE_COLUMN_SIZE, _("Size"), FALSE, TRUE); | |
1558 vflist_listview_add_column(vfl, FILE_COLUMN_DATE, _("Date"), FALSE, TRUE); | |
1559 | |
1560 g_signal_connect(G_OBJECT(vfl->listview), "key_press_event", | |
1561 G_CALLBACK(vflist_press_key_cb), vfl); | |
1562 | |
1563 gtk_container_add (GTK_CONTAINER(vfl->widget), vfl->listview); | |
1564 gtk_widget_show(vfl->listview); | |
1565 | |
1566 vflist_dnd_init(vfl); | |
1567 | |
1568 g_signal_connect(G_OBJECT(vfl->listview), "button_press_event", | |
1569 G_CALLBACK(vflist_press_cb), vfl); | |
1570 g_signal_connect(G_OBJECT(vfl->listview), "button_release_event", | |
1571 G_CALLBACK(vflist_release_cb), vfl); | |
1572 | |
1573 if (path) vflist_set_path(vfl, path); | |
1574 | |
1575 return vfl; | |
1576 } | |
1577 | |
1578 void vflist_set_status_func(ViewFileList *vfl, | |
1579 void (*func)(ViewFileList *vfl, gpointer data), gpointer data) | |
1580 { | |
1581 vfl->func_status = func; | |
1582 vfl->data_status = data; | |
1583 } | |
1584 | |
1585 void vflist_set_thumb_status_func(ViewFileList *vfl, | |
1586 void (*func)(ViewFileList *vfl, gdouble val, const gchar *text, gpointer data), | |
1587 gpointer data) | |
1588 { | |
1589 vfl->func_thumb_status = func; | |
1590 vfl->data_thumb_status = data; | |
1591 } | |
1592 | |
1593 void vflist_thumb_set(ViewFileList *vfl, gint enable) | |
1594 { | |
1595 if (vfl->thumbs_enabled == enable) return; | |
1596 | |
1597 vfl->thumbs_enabled = enable; | |
1598 vflist_refresh(vfl); | |
1599 } | |
1600 | |
1601 void vflist_set_layout(ViewFileList *vfl, LayoutWindow *layout) | |
1602 { | |
1603 vfl->layout = layout; | |
1604 } | |
1605 | |
1606 /* | |
1607 *----------------------------------------------------------------------------- | |
1608 * maintenance (for rename, move, remove) | |
1609 *----------------------------------------------------------------------------- | |
1610 */ | |
1611 | |
1612 static gint vflist_maint_find_closest(ViewFileList *vfl, gint row, gint count, GList *ignore_list) | |
1613 { | |
1614 GList *list = NULL; | |
1615 GList *work; | |
1616 gint rev = row - 1; | |
1617 row ++; | |
1618 | |
1619 work = ignore_list; | |
1620 while (work) | |
1621 { | |
1622 gint f = vflist_index_by_path(vfl, work->data); | |
1623 if (f >= 0) list = g_list_prepend(list, GINT_TO_POINTER(f)); | |
1624 work = work->next; | |
1625 } | |
1626 | |
1627 while (list) | |
1628 { | |
1629 gint c = TRUE; | |
1630 work = list; | |
1631 while (work && c) | |
1632 { | |
1633 gpointer p = work->data; | |
1634 work = work->next; | |
1635 if (row == GPOINTER_TO_INT(p)) | |
1636 { | |
1637 row++; | |
1638 c = FALSE; | |
1639 } | |
1640 if (rev == GPOINTER_TO_INT(p)) | |
1641 { | |
1642 rev--; | |
1643 c = FALSE; | |
1644 } | |
1645 if (!c) list = g_list_remove(list, p); | |
1646 } | |
1647 if (c && list) | |
1648 { | |
1649 g_list_free(list); | |
1650 list = NULL; | |
1651 } | |
1652 } | |
1653 if (row > count - 1) | |
1654 { | |
1655 if (rev < 0) | |
1656 return -1; | |
1657 else | |
1658 return rev; | |
1659 } | |
1660 else | |
1661 { | |
1662 return row; | |
1663 } | |
1664 } | |
1665 | |
1666 gint vflist_maint_renamed(ViewFileList *vfl, const gchar *source, const gchar *dest) | |
1667 { | |
1668 gint ret = FALSE; | |
1669 gint row; | |
1670 gchar *source_base; | |
1671 gchar *dest_base; | |
1672 GList *work; | |
1673 FileData *fd; | |
1674 | |
1675 row = vflist_index_by_path(vfl, source); | |
1676 if (row < 0) return FALSE; | |
1677 | |
1678 source_base = remove_level_from_path(source); | |
1679 dest_base = remove_level_from_path(dest); | |
1680 | |
1681 work = g_list_nth(vfl->list, row); | |
1682 fd = work->data; | |
1683 | |
1684 if (strcmp(source_base, dest_base) == 0) | |
1685 { | |
1686 GtkListStore *store; | |
1687 GtkTreeIter iter; | |
1688 GtkTreeIter position; | |
1689 gint old_row; | |
1690 gint n; | |
1691 | |
1692 old_row = g_list_index(vfl->list, fd); | |
1693 | |
1694 vfl->list = g_list_remove(vfl->list, fd); | |
1695 g_free(fd->path); | |
1696 | |
1697 fd->path = g_strdup(dest); | |
1698 fd->name = filename_from_path(fd->path); | |
1699 | |
1700 vfl->list = filelist_insert_sort(vfl->list, fd, vfl->sort_method, vfl->sort_ascend); | |
1701 n = g_list_index(vfl->list, fd); | |
1702 | |
1703 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(vfl->listview))); | |
1704 row = vflist_find_row(vfl, fd, &iter); | |
1705 if (vflist_find_row(vfl, fd, &iter) >= 0 && | |
1706 gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(store), &position, NULL, n)) | |
1707 { | |
1708 if (old_row >= n) | |
1709 { | |
1710 gtk_list_store_move_before(store, &iter, &position); | |
1711 } | |
1712 else | |
1713 { | |
1714 gtk_list_store_move_after(store, &iter, &position); | |
1715 } | |
1716 } | |
1717 gtk_list_store_set(store, &iter, FILE_COLUMN_NAME, fd->name, -1); | |
1718 | |
1719 ret = TRUE; | |
1720 } | |
1721 else | |
1722 { | |
1723 ret = vflist_maint_removed(vfl, source, NULL); | |
1724 } | |
1725 | |
1726 g_free(source_base); | |
1727 g_free(dest_base); | |
1728 | |
1729 return ret; | |
1730 } | |
1731 | |
1732 gint vflist_maint_removed(ViewFileList *vfl, const gchar *path, GList *ignore_list) | |
1733 { | |
1734 GtkTreeIter iter; | |
1735 GList *list; | |
1736 FileData *fd; | |
1737 gint row; | |
1738 gint new_row = -1; | |
1739 | |
1740 row = vflist_index_by_path(vfl, path); | |
1741 if (row < 0) return FALSE; | |
1742 | |
1743 if (vflist_index_is_selected(vfl, row) && | |
1744 layout_image_get_collection(vfl->layout, NULL) == NULL) | |
1745 { | |
1746 gint n; | |
1747 | |
1748 n = vflist_count(vfl, NULL); | |
1749 if (ignore_list) | |
1750 { | |
1751 new_row = vflist_maint_find_closest(vfl, row, n, ignore_list); | |
1752 if (debug) printf("row = %d, closest is %d\n", row, new_row); | |
1753 } | |
1754 else | |
1755 { | |
1756 if (row + 1 < n) | |
1757 { | |
1758 new_row = row + 1; | |
1759 } | |
1760 else if (row > 0) | |
1761 { | |
1762 new_row = row - 1; | |
1763 } | |
1764 } | |
1765 vflist_select_none(vfl); | |
1766 if (new_row >= 0) | |
1767 { | |
1768 fd = vflist_index_get_data(vfl, new_row); | |
1769 if (vflist_find_row(vfl, fd, &iter) >= 0) | |
1770 { | |
1771 GtkTreeSelection *selection; | |
1772 | |
1773 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(vfl->listview)); | |
1774 gtk_tree_selection_select_iter(selection, &iter); | |
1775 vflist_move_cursor(vfl, &iter); | |
1776 } | |
1777 } | |
1778 } | |
1779 | |
1780 fd = vflist_index_get_data(vfl, row); | |
1781 if (vflist_find_row(vfl, fd, &iter) >= 0) | |
1782 { | |
1783 GtkListStore *store; | |
1784 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(vfl->listview))); | |
1785 gtk_list_store_remove(store, &iter); | |
1786 } | |
1787 list = g_list_nth(vfl->list, row); | |
1788 fd = list->data; | |
1789 | |
1790 /* thumbnail loader check */ | |
1791 if (fd == vfl->thumbs_filedata) vfl->thumbs_filedata = NULL; | |
1792 | |
1793 vfl->list = g_list_remove(vfl->list, fd); | |
1794 file_data_free(fd); | |
1795 | |
1796 vflist_send_update(vfl); | |
1797 | |
1798 return TRUE; | |
1799 } | |
1800 | |
1801 gint vflist_maint_moved(ViewFileList *vfl, const gchar *source, const gchar *dest, GList *ignore_list) | |
1802 { | |
1803 gint ret = FALSE; | |
1804 gchar *buf; | |
1805 | |
1806 if (!source || !vfl->path) return FALSE; | |
1807 | |
1808 buf = remove_level_from_path(source); | |
1809 | |
1810 if (strcmp(buf, vfl->path) == 0) | |
1811 { | |
1812 ret = vflist_maint_removed(vfl, source, ignore_list); | |
1813 } | |
1814 | |
1815 g_free(buf); | |
1816 | |
1817 return ret; | |
1818 } | |
1819 |