Mercurial > geeqie
annotate src/view_dir_tree.c @ 1666:fa942b3f0ef5
created Files and Folders submenu
author | nadvornik |
---|---|
date | Sat, 27 Jun 2009 17:54:31 +0000 |
parents | 2349fa90226d |
children | 956aab097ea7 |
rev | line source |
---|---|
9 | 1 /* |
196 | 2 * Geeqie |
112
b15d4c18168f
Fri Nov 17 19:06:19 2006 John Ellis <johne@verizon.net>
gqview
parents:
64
diff
changeset
|
3 * (C) 2006 John Ellis |
1284 | 4 * Copyright (C) 2008 - 2009 The Geeqie Team |
9 | 5 * |
6 * Author: John Ellis | |
7 * | |
8 * This software is released under the GNU General Public License (GNU GPL). | |
9 * Please read the included file COPYING for more information. | |
10 * This software comes with no warranty of any kind, use at your own risk! | |
11 */ | |
12 | |
281 | 13 #include "main.h" |
9 | 14 #include "view_dir_tree.h" |
15 | |
16 | |
17 #include "dnd.h" | |
18 #include "dupe.h" | |
586 | 19 #include "filedata.h" |
9 | 20 #include "layout.h" |
21 #include "layout_image.h" | |
22 #include "layout_util.h" | |
23 #include "utilops.h" | |
24 #include "ui_fileops.h" | |
25 #include "ui_menu.h" | |
26 #include "ui_tree_edit.h" | |
380
5afe77bb563a
Introduce a new struct ViewDir to handle directory views common
zas_
parents:
356
diff
changeset
|
27 #include "view_dir.h" |
9 | 28 |
29 #include <gdk/gdkkeysyms.h> /* for keyboard values */ | |
30 | |
31 | |
979 | 32 #define VDTREE(_vd_) ((ViewDirInfoTree *)(_vd_->info)) |
9 | 33 |
34 | |
35 typedef struct _PathData PathData; | |
36 struct _PathData | |
37 { | |
38 gchar *name; | |
39 FileData *node; | |
40 }; | |
41 | |
42 | |
1630
a5c1930c4437
do not call signal handler from vdtree_row_expanded
nadvornik
parents:
1629
diff
changeset
|
43 static void vdtree_row_expanded(GtkTreeView *treeview, GtkTreeIter *iter, GtkTreePath *tpath, gpointer data); |
9 | 44 |
45 | |
46 /* | |
47 *---------------------------------------------------------------------------- | |
48 * utils | |
49 *---------------------------------------------------------------------------- | |
50 */ | |
51 | |
52 static void set_cursor(GtkWidget *widget, GdkCursorType cursor_type) | |
53 { | |
54 GdkCursor *cursor = NULL; | |
55 | |
56 if (!widget || !widget->window) return; | |
57 | |
512
f9bf33be53ff
Remove whitespace between function name and first parenthesis for the sake of consistency.
zas_
parents:
475
diff
changeset
|
58 if (cursor_type > -1) cursor = gdk_cursor_new(cursor_type); |
f9bf33be53ff
Remove whitespace between function name and first parenthesis for the sake of consistency.
zas_
parents:
475
diff
changeset
|
59 gdk_window_set_cursor(widget->window, cursor); |
9 | 60 if (cursor) gdk_cursor_unref(cursor); |
61 gdk_flush(); | |
62 } | |
63 | |
382 | 64 static void vdtree_busy_push(ViewDir *vd) |
9 | 65 { |
978
9ff64efe11eb
Replace macros VDLIST_INFO() and VDTREE_INFO() by shorter VDLIST() and VDTREE(). VDLIST_INFO(vd, part) becomes VDLIST(vd)->part.
zas_
parents:
943
diff
changeset
|
66 if (VDTREE(vd)->busy_ref == 0) set_cursor(vd->view, GDK_WATCH); |
9ff64efe11eb
Replace macros VDLIST_INFO() and VDTREE_INFO() by shorter VDLIST() and VDTREE(). VDLIST_INFO(vd, part) becomes VDLIST(vd)->part.
zas_
parents:
943
diff
changeset
|
67 VDTREE(vd)->busy_ref++; |
9 | 68 } |
69 | |
382 | 70 static void vdtree_busy_pop(ViewDir *vd) |
9 | 71 { |
978
9ff64efe11eb
Replace macros VDLIST_INFO() and VDTREE_INFO() by shorter VDLIST() and VDTREE(). VDLIST_INFO(vd, part) becomes VDLIST(vd)->part.
zas_
parents:
943
diff
changeset
|
72 if (VDTREE(vd)->busy_ref == 1) set_cursor(vd->view, -1); |
9ff64efe11eb
Replace macros VDLIST_INFO() and VDTREE_INFO() by shorter VDLIST() and VDTREE(). VDLIST_INFO(vd, part) becomes VDLIST(vd)->part.
zas_
parents:
943
diff
changeset
|
73 if (VDTREE(vd)->busy_ref > 0) VDTREE(vd)->busy_ref--; |
9 | 74 } |
75 | |
1501 | 76 gboolean vdtree_find_row(ViewDir *vd, FileData *fd, GtkTreeIter *iter, GtkTreeIter *parent) |
9 | 77 { |
78 GtkTreeModel *store; | |
1452 | 79 gboolean valid; |
9 | 80 |
382 | 81 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view)); |
9 | 82 if (parent) |
83 { | |
84 valid = gtk_tree_model_iter_children(store, iter, parent); | |
85 } | |
86 else | |
87 { | |
88 valid = gtk_tree_model_get_iter_first(store, iter); | |
89 } | |
90 while (valid) | |
91 { | |
92 NodeData *nd; | |
93 GtkTreeIter found; | |
94 | |
95 gtk_tree_model_get(GTK_TREE_MODEL(store), iter, DIR_COLUMN_POINTER, &nd, -1); | |
96 if (nd->fd == fd) return TRUE; | |
97 | |
382 | 98 if (vdtree_find_row(vd, fd, &found, iter)) |
9 | 99 { |
100 memcpy(iter, &found, sizeof(found)); | |
101 return TRUE; | |
102 } | |
103 | |
104 valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), iter); | |
105 } | |
106 | |
107 return FALSE; | |
108 } | |
109 | |
382 | 110 static void vdtree_icon_set_by_iter(ViewDir *vd, GtkTreeIter *iter, GdkPixbuf *pixbuf) |
9 | 111 { |
112 GtkTreeModel *store; | |
113 GdkPixbuf *old; | |
114 | |
382 | 115 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view)); |
9 | 116 gtk_tree_model_get(store, iter, DIR_COLUMN_ICON, &old, -1); |
382 | 117 if (old != vd->pf->deny) |
9 | 118 { |
119 gtk_tree_store_set(GTK_TREE_STORE(store), iter, DIR_COLUMN_ICON, pixbuf, -1); | |
120 } | |
121 } | |
122 | |
1452 | 123 static void vdtree_expand_by_iter(ViewDir *vd, GtkTreeIter *iter, gboolean expand) |
9 | 124 { |
125 GtkTreeModel *store; | |
126 GtkTreePath *tpath; | |
127 | |
382 | 128 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view)); |
9 | 129 tpath = gtk_tree_model_get_path(store, iter); |
1630
a5c1930c4437
do not call signal handler from vdtree_row_expanded
nadvornik
parents:
1629
diff
changeset
|
130 |
9 | 131 if (expand) |
132 { | |
1630
a5c1930c4437
do not call signal handler from vdtree_row_expanded
nadvornik
parents:
1629
diff
changeset
|
133 /* block signal handler, icon is set here, the caller of vdtree_expand_by_iter must make sure |
a5c1930c4437
do not call signal handler from vdtree_row_expanded
nadvornik
parents:
1629
diff
changeset
|
134 that the iter is populated */ |
a5c1930c4437
do not call signal handler from vdtree_row_expanded
nadvornik
parents:
1629
diff
changeset
|
135 g_signal_handlers_block_by_func(G_OBJECT(vd->view), vdtree_row_expanded, vd); |
382 | 136 gtk_tree_view_expand_row(GTK_TREE_VIEW(vd->view), tpath, FALSE); |
137 vdtree_icon_set_by_iter(vd, iter, vd->pf->open); | |
1630
a5c1930c4437
do not call signal handler from vdtree_row_expanded
nadvornik
parents:
1629
diff
changeset
|
138 g_signal_handlers_unblock_by_func(G_OBJECT(vd->view), vdtree_row_expanded, vd); |
9 | 139 } |
140 else | |
141 { | |
1630
a5c1930c4437
do not call signal handler from vdtree_row_expanded
nadvornik
parents:
1629
diff
changeset
|
142 /* signal handler vdtree_row_collapsed is called, it updates the icon */ |
382 | 143 gtk_tree_view_collapse_row(GTK_TREE_VIEW(vd->view), tpath); |
9 | 144 } |
145 gtk_tree_path_free(tpath); | |
146 } | |
147 | |
1452 | 148 static void vdtree_expand_by_data(ViewDir *vd, FileData *fd, gboolean expand) |
9 | 149 { |
150 GtkTreeIter iter; | |
151 | |
389 | 152 if (vd_find_row(vd, fd, &iter)) |
9 | 153 { |
382 | 154 vdtree_expand_by_iter(vd, &iter, expand); |
9 | 155 } |
156 } | |
157 | |
158 static void vdtree_node_free(NodeData *nd) | |
159 { | |
160 if (!nd) return; | |
161 | |
138 | 162 file_data_unref(nd->fd); |
9 | 163 g_free(nd); |
164 } | |
165 | |
166 /* | |
167 *---------------------------------------------------------------------------- | |
168 * dnd | |
169 *---------------------------------------------------------------------------- | |
170 */ | |
171 | |
1452 | 172 static gboolean vdtree_dnd_drop_expand_cb(gpointer data) |
9 | 173 { |
382 | 174 ViewDir *vd = data; |
9 | 175 GtkTreeIter iter; |
176 | |
394 | 177 if (vd->drop_fd && vd_find_row(vd, vd->drop_fd, &iter)) |
9 | 178 { |
783 | 179 vdtree_populate_path_by_iter(vd, &iter, FALSE, vd->dir_fd); |
382 | 180 vdtree_expand_by_data(vd, vd->drop_fd, TRUE); |
9 | 181 } |
182 | |
1523 | 183 VDTREE(vd)->drop_expand_id = 0; |
9 | 184 return FALSE; |
185 } | |
186 | |
407 | 187 static void vdtree_dnd_drop_expand_cancel(ViewDir *vd) |
9 | 188 { |
1523 | 189 if (VDTREE(vd)->drop_expand_id) |
190 { | |
191 g_source_remove(VDTREE(vd)->drop_expand_id); | |
192 VDTREE(vd)->drop_expand_id = 0; | |
193 } | |
9 | 194 } |
195 | |
407 | 196 static void vdtree_dnd_drop_expand(ViewDir *vd) |
9 | 197 { |
382 | 198 vdtree_dnd_drop_expand_cancel(vd); |
978
9ff64efe11eb
Replace macros VDLIST_INFO() and VDTREE_INFO() by shorter VDLIST() and VDTREE(). VDLIST_INFO(vd, part) becomes VDLIST(vd)->part.
zas_
parents:
943
diff
changeset
|
199 VDTREE(vd)->drop_expand_id = g_timeout_add(1000, vdtree_dnd_drop_expand_cb, vd); |
9 | 200 } |
201 | |
202 /* | |
203 *---------------------------------------------------------------------------- | |
204 * parts lists | |
205 *---------------------------------------------------------------------------- | |
206 */ | |
207 | |
208 static GList *parts_list(const gchar *path) | |
209 { | |
210 GList *list = NULL; | |
211 const gchar *strb, *strp; | |
212 gint l; | |
213 | |
214 strp = path; | |
215 | |
726 | 216 if (*strp != G_DIR_SEPARATOR) return NULL; |
9 | 217 |
218 strp++; | |
219 strb = strp; | |
220 l = 0; | |
221 | |
222 while (*strp != '\0') | |
223 { | |
726 | 224 if (*strp == G_DIR_SEPARATOR) |
9 | 225 { |
226 if (l > 0) list = g_list_prepend(list, g_strndup(strb, l)); | |
227 strp++; | |
228 strb = strp; | |
229 l = 0; | |
230 } | |
231 else | |
232 { | |
233 strp++; | |
234 l++; | |
235 } | |
236 } | |
237 if (l > 0) list = g_list_prepend(list, g_strndup(strb, l)); | |
238 | |
239 list = g_list_reverse(list); | |
240 | |
725 | 241 list = g_list_prepend(list, g_strdup(G_DIR_SEPARATOR_S)); |
9 | 242 |
243 return list; | |
244 } | |
245 | |
246 static void parts_list_free(GList *list) | |
247 { | |
248 GList *work = list; | |
249 while (work) | |
250 { | |
251 PathData *pd = work->data; | |
252 g_free(pd->name); | |
253 g_free(pd); | |
254 work = work->next; | |
255 } | |
256 | |
257 g_list_free(list); | |
258 } | |
259 | |
382 | 260 static GList *parts_list_add_node_points(ViewDir *vd, GList *list) |
9 | 261 { |
262 GList *work; | |
263 GtkTreeModel *store; | |
264 GtkTreeIter iter; | |
1452 | 265 gboolean valid; |
9 | 266 |
382 | 267 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view)); |
9 | 268 valid = gtk_tree_model_get_iter_first(store, &iter); |
269 | |
270 work = list; | |
271 while (work) | |
272 { | |
273 PathData *pd; | |
274 FileData *fd = NULL; | |
275 | |
276 pd = g_new0(PathData, 1); | |
277 pd->name = work->data; | |
278 | |
279 while (valid && !fd) | |
280 { | |
281 NodeData *nd; | |
282 | |
283 gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &nd, -1); | |
284 if (strcmp(nd->fd->name, pd->name) == 0) | |
285 { | |
286 fd = nd->fd; | |
287 } | |
288 else | |
289 { | |
290 valid = gtk_tree_model_iter_next(store, &iter); | |
291 } | |
292 } | |
293 | |
294 pd->node = fd; | |
295 work->data = pd; | |
296 | |
297 if (fd) | |
298 { | |
299 GtkTreeIter parent; | |
300 memcpy(&parent, &iter, sizeof(parent)); | |
301 valid = gtk_tree_model_iter_children(store, &iter, &parent); | |
302 } | |
303 | |
304 work = work->next; | |
305 } | |
306 | |
307 return list; | |
308 } | |
309 | |
310 /* | |
311 *---------------------------------------------------------------------------- | |
312 * misc | |
313 *---------------------------------------------------------------------------- | |
314 */ | |
315 | |
316 #if 0 | |
317 static void vdtree_row_deleted_cb(GtkTreeModel *tree_model, GtkTreePath *tpath, gpointer data) | |
318 { | |
319 GtkTreeIter iter; | |
320 NodeData *nd; | |
321 | |
322 gtk_tree_model_get_iter(tree_model, &iter, tpath); | |
323 gtk_tree_model_get(tree_model, &iter, DIR_COLUMN_POINTER, &nd, -1); | |
324 | |
325 if (!nd) return; | |
326 | |
138 | 327 file_data_unref(nd->fd); |
9 | 328 g_free(nd); |
329 } | |
330 #endif | |
331 | |
332 /* | |
333 *---------------------------------------------------------------------------- | |
334 * node traversal, management | |
335 *---------------------------------------------------------------------------- | |
336 */ | |
337 | |
1452 | 338 static gboolean vdtree_find_iter_by_data(ViewDir *vd, GtkTreeIter *parent, NodeData *nd, GtkTreeIter *iter) |
9 | 339 { |
340 GtkTreeModel *store; | |
341 | |
382 | 342 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view)); |
9 | 343 if (!nd || !gtk_tree_model_iter_children(store, iter, parent)) return -1; |
344 do { | |
345 NodeData *cnd; | |
346 | |
347 gtk_tree_model_get(store, iter, DIR_COLUMN_POINTER, &cnd, -1); | |
348 if (cnd == nd) return TRUE; | |
349 } while (gtk_tree_model_iter_next(store, iter)); | |
350 | |
351 return FALSE; | |
352 } | |
353 | |
382 | 354 static NodeData *vdtree_find_iter_by_name(ViewDir *vd, GtkTreeIter *parent, const gchar *name, GtkTreeIter *iter) |
9 | 355 { |
356 GtkTreeModel *store; | |
357 | |
382 | 358 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view)); |
9 | 359 if (!name || !gtk_tree_model_iter_children(store, iter, parent)) return NULL; |
360 do { | |
361 NodeData *nd; | |
362 | |
363 gtk_tree_model_get(store, iter, DIR_COLUMN_POINTER, &nd, -1); | |
364 if (nd && strcmp(nd->fd->name, name) == 0) return nd; | |
365 } while (gtk_tree_model_iter_next(store, iter)); | |
366 | |
367 return NULL; | |
368 } | |
369 | |
1232 | 370 static NodeData *vdtree_find_iter_by_fd(ViewDir *vd, GtkTreeIter *parent, FileData *fd, GtkTreeIter *iter) |
371 { | |
372 GtkTreeModel *store; | |
373 | |
374 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view)); | |
375 if (!fd || !gtk_tree_model_iter_children(store, iter, parent)) return NULL; | |
376 do { | |
377 NodeData *nd; | |
378 | |
379 gtk_tree_model_get(store, iter, DIR_COLUMN_POINTER, &nd, -1); | |
380 if (nd && nd->fd == fd) return nd; | |
381 } while (gtk_tree_model_iter_next(store, iter)); | |
382 | |
383 return NULL; | |
384 } | |
385 | |
382 | 386 static void vdtree_add_by_data(ViewDir *vd, FileData *fd, GtkTreeIter *parent) |
9 | 387 { |
388 GtkTreeStore *store; | |
389 GtkTreeIter child; | |
390 NodeData *nd; | |
391 GdkPixbuf *pixbuf; | |
392 NodeData *end; | |
393 GtkTreeIter empty; | |
394 | |
395 if (!fd) return; | |
396 | |
397 if (access_file(fd->path, R_OK | X_OK)) | |
398 { | |
382 | 399 pixbuf = vd->pf->close; |
9 | 400 } |
401 else | |
402 { | |
382 | 403 pixbuf = vd->pf->deny; |
9 | 404 } |
405 | |
406 nd = g_new0(NodeData, 1); | |
407 nd->fd = fd; | |
907 | 408 nd->version = fd->version; |
9 | 409 nd->expanded = FALSE; |
410 nd->last_update = time(NULL); | |
411 | |
382 | 412 store = GTK_TREE_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view))); |
9 | 413 gtk_tree_store_append(store, &child, parent); |
414 gtk_tree_store_set(store, &child, DIR_COLUMN_POINTER, nd, | |
415 DIR_COLUMN_ICON, pixbuf, | |
416 DIR_COLUMN_NAME, nd->fd->name, | |
417 DIR_COLUMN_COLOR, FALSE, -1); | |
418 | |
419 /* all nodes are created with an "empty" node, so that the expander is shown | |
420 * this is removed when the child is populated */ | |
421 end = g_new0(NodeData, 1); | |
138 | 422 end->fd = file_data_new_simple(""); |
9 | 423 end->expanded = TRUE; |
424 | |
425 gtk_tree_store_append(store, &empty, &child); | |
426 gtk_tree_store_set(store, &empty, DIR_COLUMN_POINTER, end, | |
427 DIR_COLUMN_NAME, "empty", -1); | |
428 | |
429 if (parent) | |
430 { | |
431 NodeData *pnd; | |
432 GtkTreePath *tpath; | |
433 | |
434 gtk_tree_model_get(GTK_TREE_MODEL(store), parent, DIR_COLUMN_POINTER, &pnd, -1); | |
435 tpath = gtk_tree_model_get_path(GTK_TREE_MODEL(store), parent); | |
320 | 436 if (options->tree_descend_subdirs && |
382 | 437 gtk_tree_view_row_expanded(GTK_TREE_VIEW(vd->view), tpath) && |
9 | 438 !nd->expanded) |
439 { | |
783 | 440 vdtree_populate_path_by_iter(vd, &child, FALSE, vd->dir_fd); |
9 | 441 } |
442 gtk_tree_path_free(tpath); | |
443 } | |
444 } | |
445 | |
1452 | 446 gboolean vdtree_populate_path_by_iter(ViewDir *vd, GtkTreeIter *iter, gboolean force, FileData *target_fd) |
9 | 447 { |
448 GtkTreeModel *store; | |
449 GList *list; | |
450 GList *work; | |
451 GList *old; | |
452 time_t current_time; | |
453 GtkTreeIter child; | |
454 NodeData *nd; | |
455 | |
382 | 456 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view)); |
9 | 457 gtk_tree_model_get(store, iter, DIR_COLUMN_POINTER, &nd, -1); |
458 | |
459 if (!nd) return FALSE; | |
460 | |
461 current_time = time(NULL); | |
442 | 462 |
9 | 463 if (nd->expanded) |
464 { | |
465 if (!isdir(nd->fd->path)) | |
466 { | |
382 | 467 if (vd->click_fd == nd->fd) vd->click_fd = NULL; |
468 if (vd->drop_fd == nd->fd) vd->drop_fd = NULL; | |
9 | 469 gtk_tree_store_remove(GTK_TREE_STORE(store), iter); |
470 vdtree_node_free(nd); | |
471 return FALSE; | |
472 } | |
995 | 473 if (!force && current_time - nd->last_update < 2) |
943 | 474 { |
475 DEBUG_1("Too frequent update of %s", nd->fd->path); | |
476 return TRUE; | |
477 } | |
478 if (nd->fd->version == nd->version) return TRUE; | |
9 | 479 } |
480 | |
382 | 481 vdtree_busy_push(vd); |
9 | 482 |
783 | 483 filelist_read(nd->fd, NULL, &list); |
9 | 484 |
485 /* when hidden files are not enabled, and the user enters a hidden path, | |
486 * allow the tree to display that path by specifically inserting the hidden entries | |
487 */ | |
356 | 488 if (!options->file_filter.show_hidden_files && |
783 | 489 target_fd && |
490 strncmp(nd->fd->path, target_fd->path, strlen(nd->fd->path)) == 0) | |
9 | 491 { |
492 gint n; | |
493 | |
494 n = strlen(nd->fd->path); | |
783 | 495 if (target_fd->path[n] == G_DIR_SEPARATOR && target_fd->path[n+1] == '.') |
9 | 496 { |
497 gchar *name8; | |
498 struct stat sbuf; | |
499 | |
500 n++; | |
501 | |
783 | 502 while (target_fd->path[n] != '\0' && target_fd->path[n] != G_DIR_SEPARATOR) n++; |
503 name8 = g_strndup(target_fd->path, n); | |
9 | 504 |
505 if (stat_utf8(name8, &sbuf)) | |
506 { | |
145
8be2cc687304
fixed grouping sidecar files and made it configurable via config file
nadvornik
parents:
138
diff
changeset
|
507 list = g_list_prepend(list, file_data_new_simple(name8)); |
9 | 508 } |
509 | |
510 g_free(name8); | |
511 } | |
512 } | |
513 | |
514 old = NULL; | |
515 if (gtk_tree_model_iter_children(store, &child, iter)) | |
516 { | |
517 do { | |
518 NodeData *cnd; | |
519 | |
520 gtk_tree_model_get(store, &child, DIR_COLUMN_POINTER, &cnd, -1); | |
521 old = g_list_prepend(old, cnd); | |
522 } while (gtk_tree_model_iter_next(store, &child)); | |
523 } | |
524 | |
525 work = list; | |
526 while (work) | |
527 { | |
528 FileData *fd; | |
529 | |
530 fd = work->data; | |
531 work = work->next; | |
532 | |
533 if (strcmp(fd->name, ".") == 0 || strcmp(fd->name, "..") == 0) | |
534 { | |
138 | 535 file_data_unref(fd); |
9 | 536 } |
537 else | |
538 { | |
539 NodeData *cnd; | |
540 | |
1232 | 541 cnd = vdtree_find_iter_by_fd(vd, iter, fd, &child); |
9 | 542 if (cnd) |
543 { | |
1232 | 544 if (cnd->expanded && cnd->version != fd->version) |
9 | 545 { |
1232 | 546 vdtree_populate_path_by_iter(vd, &child, FALSE, target_fd); |
9 | 547 } |
548 | |
1232 | 549 gtk_tree_store_set(GTK_TREE_STORE(store), &child, DIR_COLUMN_NAME, fd->name, -1); |
550 cnd->version = fd->version; | |
551 old = g_list_remove(old, cnd); | |
138 | 552 file_data_unref(fd); |
9 | 553 } |
554 else | |
555 { | |
382 | 556 vdtree_add_by_data(vd, fd, iter); |
9 | 557 } |
558 } | |
559 } | |
560 | |
561 work = old; | |
562 while (work) | |
563 { | |
564 NodeData *cnd = work->data; | |
565 work = work->next; | |
566 | |
382 | 567 if (vd->click_fd == cnd->fd) vd->click_fd = NULL; |
568 if (vd->drop_fd == cnd->fd) vd->drop_fd = NULL; | |
9 | 569 |
382 | 570 if (vdtree_find_iter_by_data(vd, iter, cnd, &child)) |
9 | 571 { |
572 gtk_tree_store_remove(GTK_TREE_STORE(store), &child); | |
573 vdtree_node_free(cnd); | |
574 } | |
575 } | |
576 | |
577 g_list_free(old); | |
578 g_list_free(list); | |
579 | |
382 | 580 vdtree_busy_pop(vd); |
9 | 581 |
582 nd->expanded = TRUE; | |
583 nd->last_update = current_time; | |
584 | |
585 return TRUE; | |
586 } | |
587 | |
1452 | 588 FileData *vdtree_populate_path(ViewDir *vd, FileData *target_fd, gboolean expand, gboolean force) |
9 | 589 { |
590 GList *list; | |
591 GList *work; | |
592 FileData *fd = NULL; | |
593 | |
783 | 594 if (!target_fd) return NULL; |
9 | 595 |
382 | 596 vdtree_busy_push(vd); |
9 | 597 |
783 | 598 list = parts_list(target_fd->path); |
382 | 599 list = parts_list_add_node_points(vd, list); |
9 | 600 |
601 work = list; | |
602 while (work) | |
603 { | |
604 PathData *pd = work->data; | |
605 if (pd->node == NULL) | |
606 { | |
607 PathData *parent_pd; | |
608 GtkTreeIter parent_iter; | |
609 GtkTreeIter iter; | |
610 NodeData *nd; | |
611 | |
612 if (work == list) | |
613 { | |
614 /* should not happen */ | |
673
fbebf5cf4a55
Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents:
586
diff
changeset
|
615 log_printf("vdtree warning, root node not found\n"); |
9 | 616 parts_list_free(list); |
382 | 617 vdtree_busy_pop(vd); |
9 | 618 return NULL; |
619 } | |
620 | |
621 parent_pd = work->prev->data; | |
622 | |
389 | 623 if (!vd_find_row(vd, parent_pd->node, &parent_iter) || |
783 | 624 !vdtree_populate_path_by_iter(vd, &parent_iter, force, target_fd) || |
382 | 625 (nd = vdtree_find_iter_by_name(vd, &parent_iter, pd->name, &iter)) == NULL) |
9 | 626 { |
673
fbebf5cf4a55
Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents:
586
diff
changeset
|
627 log_printf("vdtree warning, aborted at %s\n", parent_pd->name); |
9 | 628 parts_list_free(list); |
382 | 629 vdtree_busy_pop(vd); |
9 | 630 return NULL; |
631 } | |
632 | |
633 pd->node = nd->fd; | |
634 | |
635 if (pd->node) | |
636 { | |
637 if (expand) | |
638 { | |
382 | 639 vdtree_expand_by_iter(vd, &parent_iter, TRUE); |
640 vdtree_expand_by_iter(vd, &iter, TRUE); | |
9 | 641 } |
783 | 642 vdtree_populate_path_by_iter(vd, &iter, force, target_fd); |
9 | 643 } |
644 } | |
645 else | |
646 { | |
647 GtkTreeIter iter; | |
648 | |
389 | 649 if (vd_find_row(vd, pd->node, &iter)) |
9 | 650 { |
382 | 651 if (expand) vdtree_expand_by_iter(vd, &iter, TRUE); |
783 | 652 vdtree_populate_path_by_iter(vd, &iter, force, target_fd); |
9 | 653 } |
654 } | |
655 | |
656 work = work->next; | |
657 } | |
658 | |
659 work = g_list_last(list); | |
660 if (work) | |
661 { | |
662 PathData *pd = work->data; | |
663 fd = pd->node; | |
664 } | |
665 parts_list_free(list); | |
666 | |
382 | 667 vdtree_busy_pop(vd); |
9 | 668 |
669 return fd; | |
670 } | |
671 | |
672 /* | |
673 *---------------------------------------------------------------------------- | |
674 * access | |
675 *---------------------------------------------------------------------------- | |
676 */ | |
677 | |
1437 | 678 static gboolean selection_is_ok = FALSE; |
9 | 679 |
680 static gboolean vdtree_select_cb(GtkTreeSelection *selection, GtkTreeModel *store, GtkTreePath *tpath, | |
442 | 681 gboolean path_currently_selected, gpointer data) |
9 | 682 { |
683 return selection_is_ok; | |
684 } | |
685 | |
1452 | 686 gboolean vdtree_set_fd(ViewDir *vd, FileData *dir_fd) |
9 | 687 { |
688 FileData *fd; | |
689 GtkTreeIter iter; | |
690 | |
783 | 691 if (!dir_fd) return FALSE; |
692 if (vd->dir_fd == dir_fd) return TRUE; | |
9 | 693 |
783 | 694 file_data_unref(vd->dir_fd); |
695 vd->dir_fd = file_data_ref(dir_fd);; | |
9 | 696 |
783 | 697 fd = vdtree_populate_path(vd, vd->dir_fd, TRUE, FALSE); |
9 | 698 |
699 if (!fd) return FALSE; | |
700 | |
389 | 701 if (vd_find_row(vd, fd, &iter)) |
9 | 702 { |
703 GtkTreeModel *store; | |
1629
0a72cb859b49
reduced jumping of directory tree when user clicks on folders
nadvornik
parents:
1628
diff
changeset
|
704 GtkTreePath *tpath, *old_tpath; |
1628 | 705 GtkTreeSelection *selection; |
9 | 706 |
382 | 707 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view)); |
9 | 708 |
1628 | 709 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(vd->view)); |
710 | |
711 /* hack, such that selection is only allowed to be changed from here */ | |
712 selection_is_ok = TRUE; | |
713 gtk_tree_selection_select_iter(selection, &iter); | |
714 selection_is_ok = FALSE; | |
715 | |
1629
0a72cb859b49
reduced jumping of directory tree when user clicks on folders
nadvornik
parents:
1628
diff
changeset
|
716 gtk_tree_view_get_cursor(GTK_TREE_VIEW(vd->view), &old_tpath, NULL); |
0a72cb859b49
reduced jumping of directory tree when user clicks on folders
nadvornik
parents:
1628
diff
changeset
|
717 tpath = gtk_tree_model_get_path(store, &iter); |
0a72cb859b49
reduced jumping of directory tree when user clicks on folders
nadvornik
parents:
1628
diff
changeset
|
718 |
0a72cb859b49
reduced jumping of directory tree when user clicks on folders
nadvornik
parents:
1628
diff
changeset
|
719 if (!old_tpath || gtk_tree_path_compare(tpath, old_tpath) != 0) |
0a72cb859b49
reduced jumping of directory tree when user clicks on folders
nadvornik
parents:
1628
diff
changeset
|
720 { |
0a72cb859b49
reduced jumping of directory tree when user clicks on folders
nadvornik
parents:
1628
diff
changeset
|
721 /* setting the cursor scrolls the view; do not do that unless it is necessary */ |
0a72cb859b49
reduced jumping of directory tree when user clicks on folders
nadvornik
parents:
1628
diff
changeset
|
722 gtk_tree_view_set_cursor(GTK_TREE_VIEW(vd->view), tpath, NULL, FALSE); |
1631
2349fa90226d
better implementation of tree_view_row_get_visibility,
nadvornik
parents:
1630
diff
changeset
|
723 |
2349fa90226d
better implementation of tree_view_row_get_visibility,
nadvornik
parents:
1630
diff
changeset
|
724 /* gtk_tree_view_set_cursor scrolls the window itself, but it sometimes |
2349fa90226d
better implementation of tree_view_row_get_visibility,
nadvornik
parents:
1630
diff
changeset
|
725 does not work (switch from dir_list to dir_tree) */ |
2349fa90226d
better implementation of tree_view_row_get_visibility,
nadvornik
parents:
1630
diff
changeset
|
726 tree_view_row_make_visible(GTK_TREE_VIEW(vd->view), &iter, TRUE); |
1629
0a72cb859b49
reduced jumping of directory tree when user clicks on folders
nadvornik
parents:
1628
diff
changeset
|
727 } |
0a72cb859b49
reduced jumping of directory tree when user clicks on folders
nadvornik
parents:
1628
diff
changeset
|
728 gtk_tree_path_free(tpath); |
0a72cb859b49
reduced jumping of directory tree when user clicks on folders
nadvornik
parents:
1628
diff
changeset
|
729 gtk_tree_path_free(old_tpath); |
9 | 730 } |
731 | |
732 return TRUE; | |
733 } | |
734 | |
735 #if 0 | |
382 | 736 const gchar *vdtree_get_path(ViewDir *vd) |
9 | 737 { |
382 | 738 return vd->path; |
9 | 739 } |
740 #endif | |
741 | |
382 | 742 void vdtree_refresh(ViewDir *vd) |
9 | 743 { |
783 | 744 vdtree_populate_path(vd, vd->dir_fd, FALSE, TRUE); |
9 | 745 } |
746 | |
382 | 747 const gchar *vdtree_row_get_path(ViewDir *vd, gint row) |
9 | 748 { |
673
fbebf5cf4a55
Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents:
586
diff
changeset
|
749 log_printf("FIXME: no get row path\n"); |
9 | 750 return NULL; |
751 } | |
752 | |
753 /* | |
754 *---------------------------------------------------------------------------- | |
755 * callbacks | |
756 *---------------------------------------------------------------------------- | |
757 */ | |
758 | |
1452 | 759 gboolean vdtree_press_key_cb(GtkWidget *widget, GdkEventKey *event, gpointer data) |
9 | 760 { |
382 | 761 ViewDir *vd = data; |
9 | 762 GtkTreePath *tpath; |
763 GtkTreeIter iter; | |
764 FileData *fd = NULL; | |
765 | |
382 | 766 gtk_tree_view_get_cursor(GTK_TREE_VIEW(vd->view), &tpath, NULL); |
9 | 767 if (tpath) |
768 { | |
769 GtkTreeModel *store; | |
770 NodeData *nd; | |
771 | |
772 store = gtk_tree_view_get_model(GTK_TREE_VIEW(widget)); | |
773 gtk_tree_model_get_iter(store, &iter, tpath); | |
774 gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &nd, -1); | |
775 | |
776 gtk_tree_path_free(tpath); | |
777 | |
778 fd = (nd) ? nd->fd : NULL; | |
779 } | |
780 | |
781 switch (event->keyval) | |
782 { | |
783 case GDK_Menu: | |
382 | 784 vd->click_fd = fd; |
383
499d7ba62261
Move some dnd common code from view_dir_list.c and view_dir_tree.c
zas_
parents:
382
diff
changeset
|
785 vd_color_set(vd, vd->click_fd, TRUE); |
9 | 786 |
388
5186f8e38cb8
Make directory view popup menu common and move it to view_dir.{c,h}.
zas_
parents:
384
diff
changeset
|
787 vd->popup = vd_pop_menu(vd, vd->click_fd); |
395 | 788 gtk_menu_popup(GTK_MENU(vd->popup), NULL, NULL, vd_menu_position_cb, vd, 0, GDK_CURRENT_TIME); |
9 | 789 |
790 return TRUE; | |
791 break; | |
792 case GDK_plus: | |
793 case GDK_Right: | |
794 case GDK_KP_Add: | |
795 if (fd) | |
796 { | |
783 | 797 vdtree_populate_path_by_iter(vd, &iter, FALSE, vd->dir_fd); |
382 | 798 vdtree_icon_set_by_iter(vd, &iter, vd->pf->open); |
9 | 799 } |
800 break; | |
801 } | |
802 | |
803 return FALSE; | |
804 } | |
805 | |
1452 | 806 static gboolean vdtree_clicked_on_expander(GtkTreeView *treeview, GtkTreePath *tpath, |
807 GtkTreeViewColumn *column, gint x, gint y, gint *left_of_expander) | |
9 | 808 { |
809 gint depth; | |
810 gint size; | |
811 gint sep; | |
812 gint exp_width; | |
813 | |
814 if (column != gtk_tree_view_get_expander_column(treeview)) return FALSE; | |
815 | |
816 gtk_widget_style_get(GTK_WIDGET(treeview), "expander-size", &size, "horizontal-separator", &sep, NULL); | |
817 depth = gtk_tree_path_get_depth(tpath); | |
818 | |
819 exp_width = sep + size + sep; | |
820 | |
821 if (x <= depth * exp_width) | |
822 { | |
823 if (left_of_expander) *left_of_expander = !(x >= (depth - 1) * exp_width); | |
824 return TRUE; | |
825 } | |
826 | |
827 return FALSE; | |
828 } | |
829 | |
1452 | 830 gboolean vdtree_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data) |
9 | 831 { |
382 | 832 ViewDir *vd = data; |
9 | 833 GtkTreePath *tpath; |
834 GtkTreeViewColumn *column; | |
835 GtkTreeIter iter; | |
836 NodeData *nd = NULL; | |
837 | |
838 if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget), bevent->x, bevent->y, | |
839 &tpath, &column, NULL, NULL)) | |
840 { | |
841 GtkTreeModel *store; | |
842 gint left_of_expander; | |
843 | |
844 store = gtk_tree_view_get_model(GTK_TREE_VIEW(widget)); | |
845 gtk_tree_model_get_iter(store, &iter, tpath); | |
846 gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &nd, -1); | |
847 gtk_tree_view_set_cursor(GTK_TREE_VIEW(widget), tpath, NULL, FALSE); | |
848 | |
849 if (vdtree_clicked_on_expander(GTK_TREE_VIEW(widget), tpath, column, bevent->x, bevent->y, &left_of_expander)) | |
850 { | |
382 | 851 vd->click_fd = NULL; |
9 | 852 |
853 /* clicking this region should automatically reveal an expander, if necessary | |
854 * treeview bug: the expander will not expand until a button_motion_event highlights it. | |
855 */ | |
448
a73cc0fa14d0
Use explicit names for mouse buttons instead of numbers.
zas_
parents:
442
diff
changeset
|
856 if (bevent->button == MOUSE_BUTTON_LEFT && |
9 | 857 !left_of_expander && |
382 | 858 !gtk_tree_view_row_expanded(GTK_TREE_VIEW(vd->view), tpath)) |
9 | 859 { |
783 | 860 vdtree_populate_path_by_iter(vd, &iter, FALSE, vd->dir_fd); |
382 | 861 vdtree_icon_set_by_iter(vd, &iter, vd->pf->open); |
9 | 862 } |
863 | |
864 gtk_tree_path_free(tpath); | |
865 return FALSE; | |
866 } | |
867 | |
868 gtk_tree_path_free(tpath); | |
869 } | |
870 | |
382 | 871 vd->click_fd = (nd) ? nd->fd : NULL; |
383
499d7ba62261
Move some dnd common code from view_dir_list.c and view_dir_tree.c
zas_
parents:
382
diff
changeset
|
872 vd_color_set(vd, vd->click_fd, TRUE); |
9 | 873 |
448
a73cc0fa14d0
Use explicit names for mouse buttons instead of numbers.
zas_
parents:
442
diff
changeset
|
874 if (bevent->button == MOUSE_BUTTON_RIGHT) |
9 | 875 { |
388
5186f8e38cb8
Make directory view popup menu common and move it to view_dir.{c,h}.
zas_
parents:
384
diff
changeset
|
876 vd->popup = vd_pop_menu(vd, vd->click_fd); |
382 | 877 gtk_menu_popup(GTK_MENU(vd->popup), NULL, NULL, NULL, NULL, |
9 | 878 bevent->button, bevent->time); |
879 } | |
880 | |
448
a73cc0fa14d0
Use explicit names for mouse buttons instead of numbers.
zas_
parents:
442
diff
changeset
|
881 return (bevent->button != MOUSE_BUTTON_LEFT); |
9 | 882 } |
883 | |
884 static void vdtree_row_expanded(GtkTreeView *treeview, GtkTreeIter *iter, GtkTreePath *tpath, gpointer data) | |
885 { | |
382 | 886 ViewDir *vd = data; |
9 | 887 |
382 | 888 vdtree_populate_path_by_iter(vd, iter, FALSE, NULL); |
889 vdtree_icon_set_by_iter(vd, iter, vd->pf->open); | |
9 | 890 } |
891 | |
892 static void vdtree_row_collapsed(GtkTreeView *treeview, GtkTreeIter *iter, GtkTreePath *tpath, gpointer data) | |
893 { | |
382 | 894 ViewDir *vd = data; |
9 | 895 |
382 | 896 vdtree_icon_set_by_iter(vd, iter, vd->pf->close); |
9 | 897 } |
898 | |
899 static gint vdtree_sort_cb(GtkTreeModel *store, GtkTreeIter *a, GtkTreeIter *b, gpointer data) | |
900 { | |
901 NodeData *nda; | |
902 NodeData *ndb; | |
903 | |
904 gtk_tree_model_get(store, a, DIR_COLUMN_POINTER, &nda, -1); | |
905 gtk_tree_model_get(store, b, DIR_COLUMN_POINTER, &ndb, -1); | |
906 | |
785 | 907 if (options->file_sort.case_sensitive) |
819 | 908 return strcmp(nda->fd->collate_key_name, ndb->fd->collate_key_name); |
785 | 909 else |
819 | 910 return strcmp(nda->fd->collate_key_name_nocase, ndb->fd->collate_key_name_nocase); |
9 | 911 } |
912 | |
913 /* | |
914 *---------------------------------------------------------------------------- | |
915 * core | |
916 *---------------------------------------------------------------------------- | |
917 */ | |
918 | |
382 | 919 static void vdtree_setup_root(ViewDir *vd) |
9 | 920 { |
725 | 921 const gchar *path = G_DIR_SEPARATOR_S; |
9 | 922 FileData *fd; |
923 | |
138 | 924 |
925 fd = file_data_new_simple(path); | |
382 | 926 vdtree_add_by_data(vd, fd, NULL); |
9 | 927 |
382 | 928 vdtree_expand_by_data(vd, fd, TRUE); |
783 | 929 vdtree_populate_path(vd, fd, FALSE, FALSE); |
9 | 930 } |
931 | |
932 static gboolean vdtree_destroy_node_cb(GtkTreeModel *store, GtkTreePath *tpath, GtkTreeIter *iter, gpointer data) | |
933 { | |
934 NodeData *nd; | |
935 | |
936 gtk_tree_model_get(store, iter, DIR_COLUMN_POINTER, &nd, -1); | |
937 vdtree_node_free(nd); | |
938 | |
939 return FALSE; | |
940 } | |
941 | |
401
0a2e1b130a25
Add some wrappers in view_dir.c and simplify even more.
zas_
parents:
397
diff
changeset
|
942 void vdtree_destroy_cb(GtkWidget *widget, gpointer data) |
9 | 943 { |
382 | 944 ViewDir *vd = data; |
9 | 945 GtkTreeModel *store; |
946 | |
382 | 947 vdtree_dnd_drop_expand_cancel(vd); |
394 | 948 vd_dnd_drop_scroll_cancel(vd); |
382 | 949 widget_auto_scroll_stop(vd->view); |
9 | 950 |
382 | 951 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view)); |
952 gtk_tree_model_foreach(store, vdtree_destroy_node_cb, vd); | |
9 | 953 } |
954 | |
783 | 955 ViewDir *vdtree_new(ViewDir *vd, FileData *dir_fd) |
9 | 956 { |
957 GtkTreeStore *store; | |
958 GtkTreeSelection *selection; | |
959 GtkTreeViewColumn *column; | |
960 GtkCellRenderer *renderer; | |
961 | |
382 | 962 vd->info = g_new0(ViewDirInfoTree, 1); |
1365
249bf204004a
When g_new0() is used, drop redundant initializations to NULL, FALSE or 0.
zas_
parents:
1284
diff
changeset
|
963 |
382 | 964 vd->type = DIRVIEW_TREE; |
442 | 965 |
407 | 966 vd->dnd_drop_leave_func = vdtree_dnd_drop_expand_cancel; |
967 vd->dnd_drop_update_func = vdtree_dnd_drop_expand; | |
9 | 968 |
969 store = gtk_tree_store_new(4, G_TYPE_POINTER, GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_INT); | |
382 | 970 vd->view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store)); |
9 | 971 g_object_unref(store); |
972 | |
382 | 973 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(vd->view), FALSE); |
974 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(vd->view), FALSE); | |
975 gtk_tree_sortable_set_default_sort_func(GTK_TREE_SORTABLE(store), vdtree_sort_cb, vd, NULL); | |
9 | 976 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store), |
977 GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID, GTK_SORT_ASCENDING); | |
978 | |
382 | 979 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(vd->view)); |
9 | 980 gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE); |
382 | 981 gtk_tree_selection_set_select_function(selection, vdtree_select_cb, vd, NULL); |
9 | 982 |
983 column = gtk_tree_view_column_new(); | |
984 gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_GROW_ONLY); | |
985 | |
986 renderer = gtk_cell_renderer_pixbuf_new(); | |
987 gtk_tree_view_column_pack_start(column, renderer, FALSE); | |
988 gtk_tree_view_column_add_attribute(column, renderer, "pixbuf", DIR_COLUMN_ICON); | |
396 | 989 gtk_tree_view_column_set_cell_data_func(column, renderer, vd_color_cb, vd, NULL); |
9 | 990 |
991 renderer = gtk_cell_renderer_text_new(); | |
992 gtk_tree_view_column_pack_start(column, renderer, TRUE); | |
993 gtk_tree_view_column_add_attribute(column, renderer, "text", DIR_COLUMN_NAME); | |
396 | 994 gtk_tree_view_column_set_cell_data_func(column, renderer, vd_color_cb, vd, NULL); |
9 | 995 |
382 | 996 gtk_tree_view_append_column(GTK_TREE_VIEW(vd->view), column); |
9 | 997 |
382 | 998 vdtree_setup_root(vd); |
9 | 999 |
405 | 1000 g_signal_connect(G_OBJECT(vd->view), "row_expanded", |
1001 G_CALLBACK(vdtree_row_expanded), vd); | |
1002 g_signal_connect(G_OBJECT(vd->view), "row_collapsed", | |
1003 G_CALLBACK(vdtree_row_collapsed), vd); | |
9 | 1004 |
382 | 1005 return vd; |
9 | 1006 } |
1055
1646720364cf
Adding a vim modeline to all files - patch by Klaus Ethgen
nadvornik
parents:
995
diff
changeset
|
1007 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */ |