Mercurial > geeqie.yaz
annotate src/view_dir_list.c @ 44:458e396d3f35
Wed May 18 19:36:49 2005 John Ellis <johne@verizon.net>
* utilops.[ch] (file_util_rename_dir): New utility to rename a folder,
does proper checking for existing folder to avoid clobbering an
existing folder.
* view_dir_list.c, view_dir_tree.c: Use new utility above when renaming
a folder to fix possible clobbering of an existing folder with the
same name as the requested name.
##### Note: GQview CVS on sourceforge is not always up to date, please use #####
##### an offical release when making enhancements and translation updates. #####
author | gqview |
---|---|
date | Wed, 18 May 2005 23:52:16 +0000 |
parents | d907d608745f |
children | 04ff0df3ad2f |
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_dir_list.h" | |
14 | |
15 #include "dnd.h" | |
16 #include "dupe.h" | |
17 #include "filelist.h" | |
18 #include "layout.h" | |
19 #include "layout_image.h" | |
20 #include "layout_util.h" | |
21 #include "utilops.h" | |
22 #include "ui_bookmark.h" | |
23 #include "ui_fileops.h" | |
24 #include "ui_menu.h" | |
25 #include "ui_tree_edit.h" | |
26 | |
27 #include <gdk/gdkkeysyms.h> /* for keyboard values */ | |
28 | |
29 | |
30 #define VDLIST_PAD 4 | |
31 | |
32 | |
33 enum { | |
34 DIR_COLUMN_POINTER = 0, | |
35 DIR_COLUMN_ICON, | |
36 DIR_COLUMN_NAME, | |
37 DIR_COLUMN_COLOR, | |
38 DIR_COLUMN_COUNT | |
39 }; | |
40 | |
41 | |
42 static void vdlist_popup_destroy_cb(GtkWidget *widget, gpointer data); | |
43 static gint vdlist_auto_scroll_notify_cb(GtkWidget *widget, gint x, gint y, gpointer data); | |
44 | |
45 /* | |
46 *----------------------------------------------------------------------------- | |
47 * misc | |
48 *----------------------------------------------------------------------------- | |
49 */ | |
50 | |
51 static gint vdlist_find_row(ViewDirList *vdl, FileData *fd, GtkTreeIter *iter) | |
52 { | |
53 GtkTreeModel *store; | |
54 gint valid; | |
55 gint row = 0; | |
56 | |
57 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vdl->listview)); | |
58 valid = gtk_tree_model_get_iter_first(store, iter); | |
59 while (valid) | |
60 { | |
61 FileData *fd_n; | |
62 gtk_tree_model_get(GTK_TREE_MODEL(store), iter, DIR_COLUMN_POINTER, &fd_n, -1); | |
63 if (fd_n == fd) return row; | |
64 | |
65 valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), iter); | |
66 row++; | |
67 } | |
68 | |
69 return -1; | |
70 } | |
71 | |
72 static gint vdlist_rename_row_cb(TreeEditData *td, const gchar *old, const gchar *new, gpointer data) | |
73 { | |
74 ViewDirList *vdl = data; | |
75 GtkTreeModel *store; | |
76 GtkTreeIter iter; | |
77 FileData *fd; | |
78 gchar *old_path; | |
79 gchar *new_path; | |
80 gchar *base; | |
81 | |
82 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vdl->listview)); | |
83 if (!gtk_tree_model_get_iter(store, &iter, td->path)) return FALSE; | |
84 gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &fd, -1); | |
85 if (!fd) return FALSE; | |
86 | |
87 old_path = g_strdup(fd->path); | |
88 | |
89 base = remove_level_from_path(old_path); | |
90 new_path = concat_dir_and_file(base, new); | |
91 g_free(base); | |
92 | |
44
458e396d3f35
Wed May 18 19:36:49 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
93 if (file_util_rename_dir(old_path, new_path, vdl->listview)) |
9 | 94 { |
95 if (vdl->layout && strcmp(vdl->path, old_path) == 0) | |
96 { | |
97 layout_set_path(vdl->layout, new_path); | |
98 } | |
99 else | |
100 { | |
101 vdlist_refresh(vdl); | |
102 } | |
103 } | |
104 | |
105 g_free(old_path); | |
106 g_free(new_path); | |
107 return FALSE; | |
108 } | |
109 | |
110 static void vdlist_rename_by_row(ViewDirList *vdl, FileData *fd) | |
111 { | |
112 GtkTreeModel *store; | |
113 GtkTreePath *tpath; | |
114 GtkTreeIter iter; | |
115 | |
116 if (vdlist_find_row(vdl, fd, &iter) < 0) return; | |
117 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vdl->listview)); | |
118 tpath = gtk_tree_model_get_path(store, &iter); | |
119 | |
120 tree_edit_by_path(GTK_TREE_VIEW(vdl->listview), tpath, 0, fd->name, | |
121 vdlist_rename_row_cb, vdl); | |
122 gtk_tree_path_free(tpath); | |
123 } | |
124 | |
125 static FileData *vdlist_row_by_path(ViewDirList *vdl, const gchar *path, gint *row) | |
126 { | |
127 GList *work; | |
128 gint n; | |
129 | |
130 if (!path) | |
131 { | |
132 if (row) *row = -1; | |
133 return NULL; | |
134 } | |
135 | |
136 n = 0; | |
137 work = vdl->list; | |
138 while (work) | |
139 { | |
140 FileData *fd = work->data; | |
141 if (strcmp(fd->path, path) == 0) | |
142 { | |
143 if (row) *row = n; | |
144 return fd; | |
145 } | |
146 work = work->next; | |
147 n++; | |
148 } | |
149 | |
150 if (row) *row = -1; | |
151 return NULL; | |
152 } | |
153 | |
154 static void vdlist_color_set(ViewDirList *vdl, FileData *fd, gint color_set) | |
155 { | |
156 GtkTreeModel *store; | |
157 GtkTreeIter iter; | |
158 | |
159 if (vdlist_find_row(vdl, fd, &iter) < 0) return; | |
160 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vdl->listview)); | |
161 gtk_list_store_set(GTK_LIST_STORE(store), &iter, DIR_COLUMN_COLOR, color_set, -1); | |
162 } | |
163 | |
164 /* | |
165 *----------------------------------------------------------------------------- | |
166 * drop menu (from dnd) | |
167 *----------------------------------------------------------------------------- | |
168 */ | |
169 | |
170 static void vdlist_drop_menu_copy_cb(GtkWidget *widget, gpointer data) | |
171 { | |
172 ViewDirList *vdl = data; | |
173 const gchar *path; | |
174 GList *list; | |
175 | |
176 if (!vdl->drop_fd) return; | |
177 | |
178 path = vdl->drop_fd->path; | |
179 list = vdl->drop_list; | |
180 vdl->drop_list = NULL; | |
181 | |
182 file_util_copy_simple(list, path); | |
183 } | |
184 | |
185 static void vdlist_drop_menu_move_cb(GtkWidget *widget, gpointer data) | |
186 { | |
187 ViewDirList *vdl = data; | |
188 const gchar *path; | |
189 GList *list; | |
190 | |
191 if (!vdl->drop_fd) return; | |
192 | |
193 path = vdl->drop_fd->path; | |
194 list = vdl->drop_list; | |
195 | |
196 vdl->drop_list = NULL; | |
197 | |
198 file_util_move_simple(list, path); | |
199 } | |
200 | |
201 static GtkWidget *vdlist_drop_menu(ViewDirList *vdl, gint active) | |
202 { | |
203 GtkWidget *menu; | |
204 | |
205 menu = popup_menu_short_lived(); | |
206 g_signal_connect(G_OBJECT(menu), "destroy", | |
207 G_CALLBACK(vdlist_popup_destroy_cb), vdl); | |
208 | |
209 menu_item_add_stock_sensitive(menu, _("_Copy"), GTK_STOCK_COPY, active, | |
210 G_CALLBACK(vdlist_drop_menu_copy_cb), vdl); | |
211 menu_item_add_sensitive(menu, _("_Move"), active, G_CALLBACK(vdlist_drop_menu_move_cb), vdl); | |
212 | |
213 menu_item_add_divider(menu); | |
214 menu_item_add_stock(menu, _("Cancel"), GTK_STOCK_CANCEL, NULL, vdl); | |
215 | |
216 return menu; | |
217 } | |
218 | |
219 /* | |
220 *----------------------------------------------------------------------------- | |
221 * pop-up menu | |
222 *----------------------------------------------------------------------------- | |
223 */ | |
224 | |
225 static void vdlist_pop_menu_up_cb(GtkWidget *widget, gpointer data) | |
226 { | |
227 ViewDirList *vdl = data; | |
228 gchar *path; | |
229 | |
230 if (!vdl->path || strcmp(vdl->path, "/") == 0) return; | |
231 path = remove_level_from_path(vdl->path); | |
232 | |
233 if (vdl->select_func) | |
234 { | |
235 vdl->select_func(vdl, path, vdl->select_data); | |
236 } | |
237 | |
238 g_free(path); | |
239 } | |
240 | |
241 static void vdlist_pop_menu_slide_cb(GtkWidget *widget, gpointer data) | |
242 { | |
243 ViewDirList *vdl = data; | |
244 gchar *path; | |
245 | |
246 if (!vdl->layout || !vdl->click_fd) return; | |
247 | |
248 path = g_strdup(vdl->click_fd->path); | |
249 | |
250 layout_set_path(vdl->layout, path); | |
251 layout_select_none(vdl->layout); | |
252 layout_image_slideshow_stop(vdl->layout); | |
253 layout_image_slideshow_start(vdl->layout); | |
254 | |
255 g_free(path); | |
256 } | |
257 | |
258 static void vdlist_pop_menu_slide_rec_cb(GtkWidget *widget, gpointer data) | |
259 { | |
260 ViewDirList *vdl = data; | |
261 gchar *path; | |
262 GList *list; | |
263 | |
264 if (!vdl->layout || !vdl->click_fd) return; | |
265 | |
266 path = g_strdup(vdl->click_fd->path); | |
267 | |
268 list = path_list_recursive(path); | |
269 | |
270 layout_image_slideshow_stop(vdl->layout); | |
271 layout_image_slideshow_start_from_list(vdl->layout, list); | |
272 | |
273 g_free(path); | |
274 } | |
275 | |
276 static void vdlist_pop_menu_dupe(ViewDirList *vdl, gint recursive) | |
277 { | |
278 DupeWindow *dw; | |
279 const gchar *path; | |
280 GList *list = NULL; | |
281 | |
282 if (!vdl->click_fd) return; | |
283 path = vdl->click_fd->path; | |
284 | |
285 if (recursive) | |
286 { | |
287 list = g_list_append(list, g_strdup(path)); | |
288 } | |
289 else | |
290 { | |
291 path_list(path, &list, NULL); | |
292 list = path_list_filter(list, FALSE); | |
293 } | |
294 | |
295 dw = dupe_window_new(DUPE_MATCH_NAME); | |
296 dupe_window_add_files(dw, list, recursive); | |
297 | |
298 path_list_free(list); | |
299 } | |
300 | |
301 static void vdlist_pop_menu_dupe_cb(GtkWidget *widget, gpointer data) | |
302 { | |
303 ViewDirList *vdl = data; | |
304 vdlist_pop_menu_dupe(vdl, FALSE); | |
305 } | |
306 | |
307 static void vdlist_pop_menu_dupe_rec_cb(GtkWidget *widget, gpointer data) | |
308 { | |
309 ViewDirList *vdl = data; | |
310 vdlist_pop_menu_dupe(vdl, TRUE); | |
311 } | |
312 | |
313 static void vdlist_pop_menu_new_cb(GtkWidget *widget, gpointer data) | |
314 { | |
315 ViewDirList *vdl = data; | |
316 gchar *new_path; | |
317 gchar *buf; | |
318 | |
319 if (!vdl->path) return; | |
320 | |
321 buf = concat_dir_and_file(vdl->path, _("new_folder")); | |
322 new_path = unique_filename(buf, NULL, NULL, FALSE); | |
323 g_free(buf); | |
324 if (!new_path) return; | |
325 | |
326 if (!mkdir_utf8(new_path, 0755)) | |
327 { | |
328 gchar *text; | |
329 | |
330 text = g_strdup_printf(_("Unable to create folder:\n%s"), new_path); | |
331 file_util_warning_dialog(_("Error creating folder"), text, GTK_STOCK_DIALOG_ERROR, vdl->listview); | |
332 g_free(text); | |
333 } | |
334 else | |
335 { | |
336 FileData *fd; | |
337 | |
338 vdlist_refresh(vdl); | |
339 fd = vdlist_row_by_path(vdl, new_path, NULL); | |
340 | |
341 vdlist_rename_by_row(vdl, fd); | |
342 } | |
343 | |
344 g_free(new_path); | |
345 } | |
346 | |
347 static void vdlist_pop_menu_rename_cb(GtkWidget *widget, gpointer data) | |
348 { | |
349 ViewDirList *vdl = data; | |
350 | |
351 vdlist_rename_by_row(vdl, vdl->click_fd); | |
352 } | |
353 | |
354 static void vdlist_pop_menu_tree_cb(GtkWidget *widget, gpointer data) | |
355 { | |
356 ViewDirList *vdl = data; | |
357 | |
358 if (vdl->layout) layout_views_set(vdl->layout, TRUE, vdl->layout->icon_view); | |
359 } | |
360 | |
361 static void vdlist_pop_menu_refresh_cb(GtkWidget *widget, gpointer data) | |
362 { | |
363 ViewDirList *vdl = data; | |
364 | |
365 if (vdl->layout) layout_refresh(vdl->layout); | |
366 } | |
367 | |
368 static GtkWidget *vdlist_pop_menu(ViewDirList *vdl, FileData *fd) | |
369 { | |
370 GtkWidget *menu; | |
371 gint active; | |
372 | |
373 active = (fd != NULL); | |
374 | |
375 menu = popup_menu_short_lived(); | |
376 g_signal_connect(G_OBJECT(menu), "destroy", | |
377 G_CALLBACK(vdlist_popup_destroy_cb), vdl); | |
378 | |
379 menu_item_add_stock_sensitive(menu, _("_Up to parent"), GTK_STOCK_GO_UP, | |
380 (vdl->path && strcmp(vdl->path, "/") != 0), | |
381 G_CALLBACK(vdlist_pop_menu_up_cb), vdl); | |
382 | |
383 menu_item_add_divider(menu); | |
384 menu_item_add_sensitive(menu, _("_Slideshow"), active, | |
385 G_CALLBACK(vdlist_pop_menu_slide_cb), vdl); | |
386 menu_item_add_sensitive(menu, _("Slideshow recursive"), active, | |
387 G_CALLBACK(vdlist_pop_menu_slide_rec_cb), vdl); | |
388 | |
389 menu_item_add_divider(menu); | |
390 menu_item_add_stock_sensitive(menu, _("Find _duplicates..."), GTK_STOCK_FIND, active, | |
391 G_CALLBACK(vdlist_pop_menu_dupe_cb), vdl); | |
392 menu_item_add_stock_sensitive(menu, _("Find duplicates recursive..."), GTK_STOCK_FIND, active, | |
393 G_CALLBACK(vdlist_pop_menu_dupe_rec_cb), vdl); | |
394 | |
395 menu_item_add_divider(menu); | |
396 | |
397 /* check using . (always row 0) */ | |
398 active = (vdl->path && access_file(vdl->path , W_OK | X_OK)); | |
399 menu_item_add_sensitive(menu, _("_New folder..."), active, | |
400 G_CALLBACK(vdlist_pop_menu_new_cb), vdl); | |
401 | |
402 /* ignore .. and . */ | |
403 active = (active && fd && | |
404 strcmp(fd->name, ".") != 0 && | |
405 strcmp(fd->name, "..") != 0 && | |
406 access_file(fd->path, W_OK | X_OK)); | |
407 menu_item_add_sensitive(menu, _("_Rename..."), active, | |
408 G_CALLBACK(vdlist_pop_menu_rename_cb), vdl); | |
409 | |
410 menu_item_add_divider(menu); | |
411 menu_item_add_check(menu, _("View as _tree"), FALSE, | |
412 G_CALLBACK(vdlist_pop_menu_tree_cb), vdl); | |
413 menu_item_add_stock(menu, _("Re_fresh"), GTK_STOCK_REFRESH, | |
414 G_CALLBACK(vdlist_pop_menu_refresh_cb), vdl); | |
415 | |
416 return menu; | |
417 } | |
418 | |
419 static void vdlist_popup_destroy_cb(GtkWidget *widget, gpointer data) | |
420 { | |
421 ViewDirList *vdl = data; | |
422 | |
423 vdlist_color_set(vdl, vdl->click_fd, FALSE); | |
424 vdl->click_fd = NULL; | |
425 vdl->popup = NULL; | |
426 | |
427 vdlist_color_set(vdl, vdl->drop_fd, FALSE); | |
428 path_list_free(vdl->drop_list); | |
429 vdl->drop_list = NULL; | |
430 vdl->drop_fd = NULL; | |
431 } | |
432 | |
433 /* | |
434 *----------------------------------------------------------------------------- | |
435 * dnd | |
436 *----------------------------------------------------------------------------- | |
437 */ | |
438 | |
439 static GtkTargetEntry vdlist_dnd_drop_types[] = { | |
440 { "text/uri-list", 0, TARGET_URI_LIST } | |
441 }; | |
442 static gint vdlist_dnd_drop_types_count = 1; | |
443 | |
444 static void vdlist_dest_set(ViewDirList *vdl, gint enable) | |
445 { | |
446 if (enable) | |
447 { | |
448 gtk_drag_dest_set(vdl->listview, | |
449 GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_DROP, | |
450 vdlist_dnd_drop_types, vdlist_dnd_drop_types_count, | |
451 GDK_ACTION_MOVE | GDK_ACTION_COPY); | |
452 } | |
453 else | |
454 { | |
455 gtk_drag_dest_unset(vdl->listview); | |
456 } | |
457 } | |
458 | |
459 static void vdlist_dnd_get(GtkWidget *widget, GdkDragContext *context, | |
460 GtkSelectionData *selection_data, guint info, | |
461 guint time, gpointer data) | |
462 { | |
463 ViewDirList *vdl = data; | |
464 gchar *path; | |
465 GList *list; | |
466 gchar *text = NULL; | |
467 gint length = 0; | |
468 | |
469 if (!vdl->click_fd) return; | |
470 path = vdl->click_fd->path; | |
471 | |
472 switch (info) | |
473 { | |
474 case TARGET_URI_LIST: | |
475 case TARGET_TEXT_PLAIN: | |
476 list = g_list_prepend(NULL, path); | |
477 text = uri_text_from_list(list, &length, (info == TARGET_TEXT_PLAIN)); | |
478 g_list_free(list); | |
479 break; | |
480 } | |
481 if (text) | |
482 { | |
483 gtk_selection_data_set (selection_data, selection_data->target, | |
484 8, text, length); | |
485 g_free(text); | |
486 } | |
487 } | |
488 | |
489 static void vdlist_dnd_begin(GtkWidget *widget, GdkDragContext *context, gpointer data) | |
490 { | |
491 ViewDirList *vdl = data; | |
492 | |
493 vdlist_color_set(vdl, vdl->click_fd, TRUE); | |
494 vdlist_dest_set(vdl, FALSE); | |
495 } | |
496 | |
497 static void vdlist_dnd_end(GtkWidget *widget, GdkDragContext *context, gpointer data) | |
498 { | |
499 ViewDirList *vdl = data; | |
500 | |
501 vdlist_color_set(vdl, vdl->click_fd, FALSE); | |
502 | |
503 if (context->action == GDK_ACTION_MOVE) | |
504 { | |
505 vdlist_refresh(vdl); | |
506 } | |
507 vdlist_dest_set(vdl, TRUE); | |
508 } | |
509 | |
510 static void vdlist_dnd_drop_receive(GtkWidget *widget, | |
511 GdkDragContext *context, gint x, gint y, | |
512 GtkSelectionData *selection_data, guint info, | |
513 guint time, gpointer data) | |
514 { | |
515 ViewDirList *vdl = data; | |
516 GtkTreePath *tpath; | |
517 GtkTreeIter iter; | |
518 FileData *fd = NULL; | |
519 | |
520 vdl->click_fd = NULL; | |
521 | |
522 if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget), x, y, | |
523 &tpath, NULL, NULL, NULL)) | |
524 { | |
525 GtkTreeModel *store; | |
526 | |
527 store = gtk_tree_view_get_model(GTK_TREE_VIEW(widget)); | |
528 gtk_tree_model_get_iter(store, &iter, tpath); | |
529 gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &fd, -1); | |
530 gtk_tree_path_free(tpath); | |
531 } | |
532 | |
533 if (!fd) return; | |
534 | |
535 if (info == TARGET_URI_LIST) | |
536 { | |
537 GList *list; | |
538 gint active; | |
539 | |
540 list = uri_list_from_text(selection_data->data, TRUE); | |
541 if (!list) return; | |
542 | |
543 active = access_file(fd->path, W_OK | X_OK); | |
544 | |
545 vdlist_color_set(vdl, fd, TRUE); | |
546 vdl->popup = vdlist_drop_menu(vdl, active); | |
547 gtk_menu_popup(GTK_MENU(vdl->popup), NULL, NULL, NULL, NULL, 0, time); | |
548 | |
549 vdl->drop_fd = fd; | |
550 vdl->drop_list = list; | |
551 } | |
552 } | |
553 | |
554 #if 0 | |
555 static gint vdlist_get_row_visibility(ViewDirList *vdl, FileData *fd) | |
556 { | |
557 GtkTreeModel *store; | |
558 GtkTreeViewColumn *column; | |
559 GtkTreePath *tpath; | |
560 GtkTreeIter iter; | |
561 | |
562 GdkRectangle vrect; | |
563 GdkRectangle crect; | |
564 | |
565 if (!fd || vdlist_find_row(vdl, fd, &iter) < 0) return 0; | |
566 | |
567 column = gtk_tree_view_get_column(GTK_TREE_VIEW(vdl->listview), 0); | |
568 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vdl->listview)); | |
569 tpath = gtk_tree_model_get_path(store, &iter); | |
570 | |
571 gtk_tree_view_get_visible_rect(GTK_TREE_VIEW(vdl->listview), &vrect); | |
572 gtk_tree_view_get_cell_area(GTK_TREE_VIEW(vdl->listview), tpath, column, &crect); | |
573 printf("window: %d + %d; cell: %d + %d\n", vrect.y, vrect.height, crect.y, crect.height); | |
574 gtk_tree_path_free(tpath); | |
575 | |
576 if (crect.y + crect.height < vrect.y) return -1; | |
577 if (crect.y > vrect.y + vrect.height) return 1; | |
578 return 0; | |
579 } | |
580 #endif | |
581 | |
582 static void vdlist_scroll_to_row(ViewDirList *vdl, FileData *fd, gfloat y_align) | |
583 { | |
584 GtkTreeIter iter; | |
585 | |
586 if (GTK_WIDGET_REALIZED(vdl->listview) && | |
587 vdlist_find_row(vdl, fd, &iter) >= 0) | |
588 { | |
589 GtkTreeModel *store; | |
590 GtkTreePath *tpath; | |
591 | |
592 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vdl->listview)); | |
593 tpath = gtk_tree_model_get_path(store, &iter); | |
594 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(vdl->listview), tpath, NULL, TRUE, y_align, 0.0); | |
595 gtk_tree_view_set_cursor(GTK_TREE_VIEW(vdl->listview), tpath, NULL, FALSE); | |
596 gtk_tree_path_free(tpath); | |
597 | |
598 if (!GTK_WIDGET_HAS_FOCUS(vdl->listview)) gtk_widget_grab_focus(vdl->listview); | |
599 } | |
600 } | |
601 | |
602 static void vdlist_drop_update(ViewDirList *vdl, gint x, gint y) | |
603 { | |
604 GtkTreePath *tpath; | |
605 GtkTreeIter iter; | |
606 FileData *fd = NULL; | |
607 | |
608 if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(vdl->listview), x, y, | |
609 &tpath, NULL, NULL, NULL)) | |
610 { | |
611 GtkTreeModel *store; | |
612 | |
613 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vdl->listview)); | |
614 gtk_tree_model_get_iter(store, &iter, tpath); | |
615 gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &fd, -1); | |
616 gtk_tree_path_free(tpath); | |
617 } | |
618 | |
619 if (fd != vdl->drop_fd) | |
620 { | |
621 vdlist_color_set(vdl, vdl->drop_fd, FALSE); | |
622 vdlist_color_set(vdl, fd, TRUE); | |
623 } | |
624 | |
625 vdl->drop_fd = fd; | |
626 } | |
627 | |
628 static void vdlist_dnd_drop_scroll_cancel(ViewDirList *vdl) | |
629 { | |
630 if (vdl->drop_scroll_id != -1) g_source_remove(vdl->drop_scroll_id); | |
631 vdl->drop_scroll_id = -1; | |
632 } | |
633 | |
634 static gint vdlist_auto_scroll_idle_cb(gpointer data) | |
635 { | |
636 ViewDirList *vdl = data; | |
637 | |
638 if (vdl->drop_fd) | |
639 { | |
640 GdkWindow *window; | |
641 gint x, y; | |
642 gint w, h; | |
643 | |
644 window = vdl->listview->window; | |
645 gdk_window_get_pointer(window, &x, &y, NULL); | |
646 gdk_drawable_get_size(window, &w, &h); | |
647 if (x >= 0 && x < w && y >= 0 && y < h) | |
648 { | |
649 vdlist_drop_update(vdl, x, y); | |
650 } | |
651 } | |
652 | |
653 vdl->drop_scroll_id = -1; | |
654 return FALSE; | |
655 } | |
656 | |
657 static gint vdlist_auto_scroll_notify_cb(GtkWidget *widget, gint x, gint y, gpointer data) | |
658 { | |
659 ViewDirList *vdl = data; | |
660 | |
661 if (!vdl->drop_fd || vdl->drop_list) return FALSE; | |
662 | |
663 if (vdl->drop_scroll_id == -1) vdl->drop_scroll_id = g_idle_add(vdlist_auto_scroll_idle_cb, vdl); | |
664 | |
665 return TRUE; | |
666 } | |
667 | |
668 static gint vdlist_dnd_drop_motion(GtkWidget *widget, GdkDragContext *context, | |
669 gint x, gint y, guint time, gpointer data) | |
670 { | |
671 ViewDirList *vdl = data; | |
672 | |
673 vdl->click_fd = NULL; | |
674 | |
675 if (gtk_drag_get_source_widget(context) == vdl->listview) | |
676 { | |
677 /* from same window */ | |
678 gdk_drag_status(context, 0, time); | |
679 return TRUE; | |
680 } | |
681 else | |
682 { | |
683 gdk_drag_status(context, context->suggested_action, time); | |
684 } | |
685 | |
686 vdlist_drop_update(vdl, x, y); | |
687 | |
688 if (vdl->drop_fd) | |
689 { | |
690 GtkAdjustment *adj = gtk_tree_view_get_vadjustment(GTK_TREE_VIEW(vdl->listview)); | |
691 widget_auto_scroll_start(vdl->listview, adj, -1, -1, vdlist_auto_scroll_notify_cb, vdl); | |
692 } | |
693 | |
694 return FALSE; | |
695 } | |
696 | |
697 static void vdlist_dnd_drop_leave(GtkWidget *widget, GdkDragContext *context, guint time, gpointer data) | |
698 { | |
699 ViewDirList *vdl = data; | |
700 | |
701 if (vdl->drop_fd != vdl->click_fd) vdlist_color_set(vdl, vdl->drop_fd, FALSE); | |
702 | |
703 vdl->drop_fd = NULL; | |
704 } | |
705 | |
706 static void vdlist_dnd_init(ViewDirList *vdl) | |
707 { | |
708 gtk_drag_source_set(vdl->listview, GDK_BUTTON1_MASK | GDK_BUTTON2_MASK, | |
709 dnd_file_drag_types, dnd_file_drag_types_count, | |
710 GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK); | |
711 g_signal_connect(G_OBJECT(vdl->listview), "drag_data_get", | |
712 G_CALLBACK(vdlist_dnd_get), vdl); | |
713 g_signal_connect(G_OBJECT(vdl->listview), "drag_begin", | |
714 G_CALLBACK(vdlist_dnd_begin), vdl); | |
715 g_signal_connect(G_OBJECT(vdl->listview), "drag_end", | |
716 G_CALLBACK(vdlist_dnd_end), vdl); | |
717 | |
718 vdlist_dest_set(vdl, TRUE); | |
719 g_signal_connect(G_OBJECT(vdl->listview), "drag_data_received", | |
720 G_CALLBACK(vdlist_dnd_drop_receive), vdl); | |
721 g_signal_connect(G_OBJECT(vdl->listview), "drag_motion", | |
722 G_CALLBACK(vdlist_dnd_drop_motion), vdl); | |
723 g_signal_connect(G_OBJECT(vdl->listview), "drag_leave", | |
724 G_CALLBACK(vdlist_dnd_drop_leave), vdl); | |
725 } | |
726 | |
727 /* | |
728 *----------------------------------------------------------------------------- | |
729 * main | |
730 *----------------------------------------------------------------------------- | |
731 */ | |
732 | |
733 static void vdlist_select_row(ViewDirList *vdl, FileData *fd) | |
734 { | |
735 if (fd && vdl->select_func) | |
736 { | |
737 gchar *path; | |
738 | |
739 path = g_strdup(fd->path); | |
740 vdl->select_func(vdl, path, vdl->select_data); | |
741 g_free(path); | |
742 } | |
743 } | |
744 | |
745 const gchar *vdlist_row_get_path(ViewDirList *vdl, gint row) | |
746 { | |
747 FileData *fd; | |
748 | |
749 fd = g_list_nth_data(vdl->list, row); | |
750 | |
751 if (fd) return fd->path; | |
752 | |
753 return NULL; | |
754 } | |
755 | |
756 static void vdlist_populate(ViewDirList *vdl) | |
757 { | |
758 GtkListStore *store; | |
759 GList *work; | |
760 | |
761 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(vdl->listview))); | |
762 gtk_list_store_clear(store); | |
763 | |
764 work = vdl->list; | |
765 while (work) | |
766 { | |
767 FileData *fd; | |
768 GtkTreeIter iter; | |
769 GdkPixbuf *pixbuf; | |
770 | |
771 fd = work->data; | |
772 | |
773 if (access_file(fd->path, R_OK | X_OK) && fd->name) | |
774 { | |
775 if (fd->name[0] == '.' && fd->name[1] == '\0') | |
776 { | |
777 pixbuf = vdl->pf->open; | |
778 } | |
779 else if (fd->name[0] == '.' && fd->name[1] == '.' && fd->name[2] == '\0') | |
780 { | |
781 pixbuf = vdl->pf->parent; | |
782 } | |
783 else | |
784 { | |
785 pixbuf = vdl->pf->close; | |
786 } | |
787 } | |
788 else | |
789 { | |
790 pixbuf = vdl->pf->deny; | |
791 } | |
792 | |
793 gtk_list_store_append(store, &iter); | |
794 gtk_list_store_set(store, &iter, | |
795 DIR_COLUMN_POINTER, fd, | |
796 DIR_COLUMN_ICON, pixbuf, | |
797 DIR_COLUMN_NAME, fd->name, -1); | |
798 | |
799 work = work->next; | |
800 } | |
801 | |
802 vdl->click_fd = NULL; | |
803 vdl->drop_fd = NULL; | |
804 } | |
805 | |
806 gint vdlist_set_path(ViewDirList *vdl, const gchar *path) | |
807 { | |
808 gint ret; | |
809 FileData *fd; | |
810 gchar *old_path = NULL; | |
811 | |
812 if (!path) return FALSE; | |
813 if (vdl->path && strcmp(path, vdl->path) == 0) return TRUE; | |
814 | |
815 if (vdl->path) | |
816 { | |
817 gchar *base; | |
818 | |
819 base = remove_level_from_path(vdl->path); | |
820 if (strcmp(base, path) == 0) | |
821 { | |
822 old_path = g_strdup(filename_from_path(vdl->path)); | |
823 } | |
824 g_free(base); | |
825 } | |
826 | |
827 g_free(vdl->path); | |
828 vdl->path = g_strdup(path); | |
829 | |
830 filelist_free(vdl->list); | |
831 vdl->list = NULL; | |
832 | |
833 ret = filelist_read(vdl->path, NULL, &vdl->list); | |
834 | |
835 vdl->list = filelist_sort(vdl->list, SORT_NAME, TRUE); | |
836 | |
837 /* add . and .. */ | |
838 | |
839 if (strcmp(vdl->path, "/") != 0) | |
840 { | |
841 fd = g_new0(FileData, 1); | |
842 fd->path = remove_level_from_path(vdl->path); | |
843 fd->name = ".."; | |
844 vdl->list = g_list_prepend(vdl->list, fd); | |
845 } | |
846 | |
847 fd = g_new0(FileData, 1); | |
848 fd->path = g_strdup(vdl->path); | |
849 fd->name = "."; | |
850 vdl->list = g_list_prepend(vdl->list, fd); | |
851 | |
852 vdlist_populate(vdl); | |
853 | |
854 if (old_path) | |
855 { | |
856 /* scroll to make last path visible */ | |
857 FileData *found = NULL; | |
858 GList *work; | |
859 | |
860 work = vdl->list; | |
861 while (work && !found) | |
862 { | |
863 FileData *fd = work->data; | |
864 if (strcmp(old_path, fd->name) == 0) found = fd; | |
865 work = work->next; | |
866 } | |
867 | |
868 if (found) vdlist_scroll_to_row(vdl, found, 0.5); | |
869 | |
870 g_free(old_path); | |
871 return ret; | |
872 } | |
873 | |
874 if (GTK_WIDGET_REALIZED(vdl->listview)) | |
875 { | |
876 gtk_tree_view_scroll_to_point(GTK_TREE_VIEW(vdl->listview), 0, 0); | |
877 } | |
878 | |
879 return ret; | |
880 } | |
881 | |
882 void vdlist_refresh(ViewDirList *vdl) | |
883 { | |
884 gchar *path; | |
885 | |
886 path = g_strdup(vdl->path); | |
887 vdl->path = NULL; | |
888 vdlist_set_path(vdl, path); | |
889 g_free(path); | |
890 } | |
891 | |
892 static void vdlist_menu_position_cb(GtkMenu *menu, gint *x, gint *y, gboolean *push_in, gpointer data) | |
893 { | |
894 ViewDirList *vdl = data; | |
895 GtkTreeModel *store; | |
896 GtkTreeIter iter; | |
897 GtkTreePath *tpath; | |
898 gint cw, ch; | |
899 | |
900 if (vdlist_find_row(vdl, vdl->click_fd, &iter) < 0) return; | |
901 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vdl->listview)); | |
902 tpath = gtk_tree_model_get_path(store, &iter); | |
903 tree_view_get_cell_clamped(GTK_TREE_VIEW(vdl->listview), tpath, 0, TRUE, x, y, &cw, &ch); | |
904 gtk_tree_path_free(tpath); | |
905 *y += ch; | |
906 popup_menu_position_clamp(menu, x, y, 0); | |
907 } | |
908 | |
909 static gint vdlist_press_key_cb(GtkWidget *widget, GdkEventKey *event, gpointer data) | |
910 { | |
911 ViewDirList *vdl = data; | |
912 GtkTreePath *tpath; | |
913 | |
914 if (event->keyval != GDK_Menu) return FALSE; | |
915 | |
916 gtk_tree_view_get_cursor(GTK_TREE_VIEW(vdl->listview), &tpath, NULL); | |
917 if (tpath) | |
918 { | |
919 GtkTreeModel *store; | |
920 GtkTreeIter iter; | |
921 | |
922 store = gtk_tree_view_get_model(GTK_TREE_VIEW(widget)); | |
923 gtk_tree_model_get_iter(store, &iter, tpath); | |
924 gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &vdl->click_fd, -1); | |
925 | |
926 gtk_tree_path_free(tpath); | |
927 } | |
928 else | |
929 { | |
930 vdl->click_fd = NULL; | |
931 } | |
932 | |
933 vdlist_color_set(vdl, vdl->click_fd, TRUE); | |
934 | |
935 vdl->popup = vdlist_pop_menu(vdl, vdl->click_fd); | |
936 | |
937 gtk_menu_popup(GTK_MENU(vdl->popup), NULL, NULL, vdlist_menu_position_cb, vdl, 0, GDK_CURRENT_TIME); | |
938 | |
939 return TRUE; | |
940 } | |
941 | |
942 static gint vdlist_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data) | |
943 { | |
944 ViewDirList *vdl = data; | |
945 GtkTreePath *tpath; | |
946 GtkTreeIter iter; | |
947 FileData *fd = NULL; | |
948 | |
949 if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget), bevent->x, bevent->y, | |
950 &tpath, NULL, NULL, NULL)) | |
951 { | |
952 GtkTreeModel *store; | |
953 | |
954 store = gtk_tree_view_get_model(GTK_TREE_VIEW(widget)); | |
955 gtk_tree_model_get_iter(store, &iter, tpath); | |
956 gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &fd, -1); | |
957 gtk_tree_view_set_cursor(GTK_TREE_VIEW(widget), tpath, NULL, FALSE); | |
958 gtk_tree_path_free(tpath); | |
959 } | |
960 | |
961 vdl->click_fd = fd; | |
962 vdlist_color_set(vdl, vdl->click_fd, TRUE); | |
963 | |
964 if (bevent->button == 3) | |
965 { | |
966 vdl->popup = vdlist_pop_menu(vdl, vdl->click_fd); | |
967 gtk_menu_popup(GTK_MENU(vdl->popup), NULL, NULL, NULL, NULL, | |
968 bevent->button, bevent->time); | |
969 } | |
970 | |
971 return TRUE; | |
972 } | |
973 | |
974 static gint vdlist_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data) | |
975 { | |
976 ViewDirList *vdl = data; | |
977 GtkTreePath *tpath; | |
978 GtkTreeIter iter; | |
979 FileData *fd = NULL; | |
980 | |
981 vdlist_color_set(vdl, vdl->click_fd, FALSE); | |
982 | |
983 if (bevent->button != 1) return TRUE; | |
984 | |
985 if ((bevent->x != 0 || bevent->y != 0) && | |
986 gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget), bevent->x, bevent->y, | |
987 &tpath, NULL, NULL, NULL)) | |
988 { | |
989 GtkTreeModel *store; | |
990 | |
991 store = gtk_tree_view_get_model(GTK_TREE_VIEW(widget)); | |
992 gtk_tree_model_get_iter(store, &iter, tpath); | |
993 gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &fd, -1); | |
994 gtk_tree_path_free(tpath); | |
995 } | |
996 | |
997 if (fd && vdl->click_fd == fd) | |
998 { | |
999 vdlist_select_row(vdl, vdl->click_fd); | |
1000 } | |
1001 | |
1002 return TRUE; | |
1003 } | |
1004 | |
1005 static void vdlist_select_cb(GtkTreeView *tview, GtkTreePath *tpath, GtkTreeViewColumn *column, gpointer data) | |
1006 { | |
1007 ViewDirList *vdl = data; | |
1008 GtkTreeModel *store; | |
1009 GtkTreeIter iter; | |
1010 FileData *fd; | |
1011 | |
1012 store = gtk_tree_view_get_model(tview); | |
1013 gtk_tree_model_get_iter(store, &iter, tpath); | |
1014 gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &fd, -1); | |
1015 | |
1016 vdlist_select_row(vdl, fd); | |
1017 } | |
1018 | |
1019 static GdkColor *vdlist_color_shifted(GtkWidget *widget) | |
1020 { | |
1021 static GdkColor color; | |
1022 static GtkWidget *done = NULL; | |
1023 | |
1024 if (done != widget) | |
1025 { | |
1026 GtkStyle *style; | |
1027 | |
1028 style = gtk_widget_get_style(widget); | |
1029 memcpy(&color, &style->base[GTK_STATE_NORMAL], sizeof(color)); | |
1030 shift_color(&color, -1, 0); | |
1031 done = widget; | |
1032 } | |
1033 | |
1034 return &color; | |
1035 } | |
1036 | |
1037 static void vdlist_color_cb(GtkTreeViewColumn *tree_column, GtkCellRenderer *cell, | |
1038 GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data) | |
1039 { | |
1040 ViewDirList *vdl = data; | |
1041 gboolean set; | |
1042 | |
1043 gtk_tree_model_get(tree_model, iter, DIR_COLUMN_COLOR, &set, -1); | |
1044 g_object_set(G_OBJECT(cell), | |
1045 "cell-background-gdk", vdlist_color_shifted(vdl->listview), | |
1046 "cell-background-set", set, NULL); | |
1047 } | |
1048 | |
1049 static void vdlist_destroy_cb(GtkWidget *widget, gpointer data) | |
1050 { | |
1051 ViewDirList *vdl = data; | |
1052 | |
1053 if (vdl->popup) | |
1054 { | |
1055 g_signal_handlers_disconnect_matched(G_OBJECT(vdl->popup), G_SIGNAL_MATCH_DATA, | |
1056 0, 0, 0, NULL, vdl); | |
1057 gtk_widget_destroy(vdl->popup); | |
1058 } | |
1059 | |
1060 vdlist_dnd_drop_scroll_cancel(vdl); | |
1061 widget_auto_scroll_stop(vdl->listview); | |
1062 | |
1063 path_list_free(vdl->drop_list); | |
1064 | |
1065 folder_icons_free(vdl->pf); | |
1066 | |
1067 g_free(vdl->path); | |
1068 filelist_free(vdl->list); | |
1069 g_free(vdl); | |
1070 } | |
1071 | |
1072 ViewDirList *vdlist_new(const gchar *path) | |
1073 { | |
1074 ViewDirList *vdl; | |
1075 GtkListStore *store; | |
1076 GtkTreeSelection *selection; | |
1077 GtkTreeViewColumn *column; | |
1078 GtkCellRenderer *renderer; | |
1079 | |
1080 vdl = g_new0(ViewDirList, 1); | |
1081 | |
1082 vdl->path = NULL; | |
1083 vdl->list = NULL; | |
1084 vdl->click_fd = NULL; | |
1085 | |
1086 vdl->drop_fd = NULL; | |
1087 vdl->drop_list = NULL; | |
1088 | |
1089 vdl->drop_scroll_id = -1; | |
1090 | |
1091 vdl->popup = NULL; | |
1092 | |
1093 vdl->widget = gtk_scrolled_window_new(NULL, NULL); | |
1094 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(vdl->widget), GTK_SHADOW_IN); | |
1095 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(vdl->widget), | |
1096 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS); | |
1097 g_signal_connect(G_OBJECT(vdl->widget), "destroy", | |
1098 G_CALLBACK(vdlist_destroy_cb), vdl); | |
1099 | |
1100 store = gtk_list_store_new(4, G_TYPE_POINTER, GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_BOOLEAN); | |
1101 vdl->listview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store)); | |
1102 g_object_unref(store); | |
1103 | |
1104 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(vdl->listview), FALSE); | |
1105 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(vdl->listview), FALSE); | |
1106 g_signal_connect(G_OBJECT(vdl->listview), "row_activated", | |
1107 | |
1108 G_CALLBACK(vdlist_select_cb), vdl); | |
1109 | |
1110 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(vdl->listview)); | |
1111 gtk_tree_selection_set_mode(selection, GTK_SELECTION_NONE); | |
1112 | |
1113 column = gtk_tree_view_column_new(); | |
1114 gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE); | |
1115 | |
1116 renderer = gtk_cell_renderer_pixbuf_new(); | |
1117 gtk_tree_view_column_pack_start(column, renderer, FALSE); | |
1118 gtk_tree_view_column_add_attribute(column, renderer, "pixbuf", DIR_COLUMN_ICON); | |
1119 gtk_tree_view_column_set_cell_data_func(column, renderer, vdlist_color_cb, vdl, NULL); | |
1120 | |
1121 renderer = gtk_cell_renderer_text_new(); | |
1122 gtk_tree_view_column_pack_start(column, renderer, TRUE); | |
1123 gtk_tree_view_column_add_attribute(column, renderer, "text", DIR_COLUMN_NAME); | |
1124 gtk_tree_view_column_set_cell_data_func(column, renderer, vdlist_color_cb, vdl, NULL); | |
1125 | |
1126 gtk_tree_view_append_column(GTK_TREE_VIEW(vdl->listview), column); | |
1127 | |
1128 g_signal_connect(G_OBJECT(vdl->listview), "key_press_event", | |
1129 G_CALLBACK(vdlist_press_key_cb), vdl); | |
1130 gtk_container_add(GTK_CONTAINER(vdl->widget), vdl->listview); | |
1131 gtk_widget_show(vdl->listview); | |
1132 | |
1133 vdl->pf = folder_icons_new(); | |
1134 | |
1135 vdlist_dnd_init(vdl); | |
1136 | |
1137 g_signal_connect(G_OBJECT(vdl->listview), "button_press_event", | |
1138 G_CALLBACK(vdlist_press_cb), vdl); | |
1139 g_signal_connect(G_OBJECT(vdl->listview), "button_release_event", | |
1140 G_CALLBACK(vdlist_release_cb), vdl); | |
1141 | |
1142 if (path) vdlist_set_path(vdl, path); | |
1143 | |
1144 return vdl; | |
1145 } | |
1146 | |
1147 void vdlist_set_select_func(ViewDirList *vdl, | |
1148 void (*func)(ViewDirList *vdl, const gchar *path, gpointer data), gpointer data) | |
1149 { | |
1150 vdl->select_func = func; | |
1151 vdl->select_data = data; | |
1152 } | |
1153 | |
1154 void vdlist_set_layout(ViewDirList *vdl, LayoutWindow *layout) | |
1155 { | |
1156 vdl->layout = layout; | |
1157 } | |
1158 |