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