Mercurial > geeqie
annotate src/view_dir_list.c @ 394:4a5e1377f3d7
Merge dirlist/dirview dnd code.
author | zas_ |
---|---|
date | Thu, 17 Apr 2008 14:51:32 +0000 |
parents | b78077f65eff |
children | c359fc2c5a1f |
rev | line source |
---|---|
9 | 1 /* |
196 | 2 * Geeqie |
9 | 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 | |
281 | 12 #include "main.h" |
9 | 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" | |
380
5afe77bb563a
Introduce a new struct ViewDir to handle directory views common
zas_
parents:
373
diff
changeset
|
26 #include "view_dir.h" |
9 | 27 |
28 #include <gdk/gdkkeysyms.h> /* for keyboard values */ | |
29 | |
30 | |
31 #define VDLIST_PAD 4 | |
32 | |
380
5afe77bb563a
Introduce a new struct ViewDir to handle directory views common
zas_
parents:
373
diff
changeset
|
33 #define VDLIST_INFO(_vd_, _part_) (((ViewDirInfoList *)(_vd_->info))->_part_) |
9 | 34 |
35 | |
36 /* | |
37 *----------------------------------------------------------------------------- | |
38 * misc | |
39 *----------------------------------------------------------------------------- | |
40 */ | |
41 | |
383
499d7ba62261
Move some dnd common code from view_dir_list.c and view_dir_tree.c
zas_
parents:
381
diff
changeset
|
42 gint vdlist_find_row(ViewDir *vd, FileData *fd, GtkTreeIter *iter) |
9 | 43 { |
44 GtkTreeModel *store; | |
45 gint valid; | |
46 gint row = 0; | |
47 | |
381 | 48 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view)); |
9 | 49 valid = gtk_tree_model_get_iter_first(store, iter); |
50 while (valid) | |
51 { | |
52 FileData *fd_n; | |
53 gtk_tree_model_get(GTK_TREE_MODEL(store), iter, DIR_COLUMN_POINTER, &fd_n, -1); | |
54 if (fd_n == fd) return row; | |
55 | |
56 valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), iter); | |
57 row++; | |
58 } | |
59 | |
60 return -1; | |
61 } | |
62 | |
63 | |
388
5186f8e38cb8
Make directory view popup menu common and move it to view_dir.{c,h}.
zas_
parents:
385
diff
changeset
|
64 FileData *vdlist_row_by_path(ViewDir *vd, const gchar *path, gint *row) |
9 | 65 { |
66 GList *work; | |
67 gint n; | |
68 | |
69 if (!path) | |
70 { | |
71 if (row) *row = -1; | |
72 return NULL; | |
73 } | |
74 | |
75 n = 0; | |
381 | 76 work = VDLIST_INFO(vd, list); |
9 | 77 while (work) |
78 { | |
79 FileData *fd = work->data; | |
80 if (strcmp(fd->path, path) == 0) | |
81 { | |
82 if (row) *row = n; | |
83 return fd; | |
84 } | |
85 work = work->next; | |
86 n++; | |
87 } | |
88 | |
89 if (row) *row = -1; | |
90 return NULL; | |
91 } | |
92 | |
93 /* | |
94 *----------------------------------------------------------------------------- | |
95 * dnd | |
96 *----------------------------------------------------------------------------- | |
97 */ | |
98 | |
381 | 99 static void vdlist_scroll_to_row(ViewDir *vd, FileData *fd, gfloat y_align) |
9 | 100 { |
101 GtkTreeIter iter; | |
102 | |
394 | 103 if (GTK_WIDGET_REALIZED(vd->view) && vd_find_row(vd, fd, &iter) >= 0) |
9 | 104 { |
105 GtkTreeModel *store; | |
106 GtkTreePath *tpath; | |
107 | |
381 | 108 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view)); |
9 | 109 tpath = gtk_tree_model_get_path(store, &iter); |
381 | 110 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(vd->view), tpath, NULL, TRUE, y_align, 0.0); |
111 gtk_tree_view_set_cursor(GTK_TREE_VIEW(vd->view), tpath, NULL, FALSE); | |
9 | 112 gtk_tree_path_free(tpath); |
113 | |
381 | 114 if (!GTK_WIDGET_HAS_FOCUS(vd->view)) gtk_widget_grab_focus(vd->view); |
9 | 115 } |
116 } | |
117 | |
118 /* | |
119 *----------------------------------------------------------------------------- | |
120 * main | |
121 *----------------------------------------------------------------------------- | |
122 */ | |
123 | |
381 | 124 static void vdlist_select_row(ViewDir *vd, FileData *fd) |
9 | 125 { |
381 | 126 if (fd && vd->select_func) |
9 | 127 { |
128 gchar *path; | |
129 | |
130 path = g_strdup(fd->path); | |
381 | 131 vd->select_func(vd, path, vd->select_data); |
9 | 132 g_free(path); |
133 } | |
134 } | |
135 | |
381 | 136 const gchar *vdlist_row_get_path(ViewDir *vd, gint row) |
9 | 137 { |
138 FileData *fd; | |
139 | |
381 | 140 fd = g_list_nth_data(VDLIST_INFO(vd, list), row); |
9 | 141 |
142 if (fd) return fd->path; | |
143 | |
144 return NULL; | |
145 } | |
146 | |
381 | 147 static void vdlist_populate(ViewDir *vd) |
9 | 148 { |
149 GtkListStore *store; | |
150 GList *work; | |
151 | |
381 | 152 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view))); |
9 | 153 gtk_list_store_clear(store); |
154 | |
381 | 155 work = VDLIST_INFO(vd, list); |
9 | 156 while (work) |
157 { | |
158 FileData *fd; | |
159 GtkTreeIter iter; | |
160 GdkPixbuf *pixbuf; | |
161 | |
162 fd = work->data; | |
163 | |
164 if (access_file(fd->path, R_OK | X_OK) && fd->name) | |
165 { | |
166 if (fd->name[0] == '.' && fd->name[1] == '\0') | |
167 { | |
381 | 168 pixbuf = vd->pf->open; |
9 | 169 } |
170 else if (fd->name[0] == '.' && fd->name[1] == '.' && fd->name[2] == '\0') | |
171 { | |
381 | 172 pixbuf = vd->pf->parent; |
9 | 173 } |
174 else | |
175 { | |
381 | 176 pixbuf = vd->pf->close; |
9 | 177 } |
178 } | |
179 else | |
180 { | |
381 | 181 pixbuf = vd->pf->deny; |
9 | 182 } |
183 | |
184 gtk_list_store_append(store, &iter); | |
185 gtk_list_store_set(store, &iter, | |
186 DIR_COLUMN_POINTER, fd, | |
187 DIR_COLUMN_ICON, pixbuf, | |
188 DIR_COLUMN_NAME, fd->name, -1); | |
189 | |
190 work = work->next; | |
191 } | |
192 | |
381 | 193 vd->click_fd = NULL; |
194 vd->drop_fd = NULL; | |
9 | 195 } |
196 | |
381 | 197 gint vdlist_set_path(ViewDir *vd, const gchar *path) |
9 | 198 { |
199 gint ret; | |
200 FileData *fd; | |
201 gchar *old_path = NULL; | |
138 | 202 gchar *filepath; |
9 | 203 |
204 if (!path) return FALSE; | |
381 | 205 if (vd->path && strcmp(path, vd->path) == 0) return TRUE; |
9 | 206 |
381 | 207 if (vd->path) |
9 | 208 { |
209 gchar *base; | |
210 | |
381 | 211 base = remove_level_from_path(vd->path); |
9 | 212 if (strcmp(base, path) == 0) |
213 { | |
381 | 214 old_path = g_strdup(filename_from_path(vd->path)); |
9 | 215 } |
216 g_free(base); | |
217 } | |
218 | |
381 | 219 g_free(vd->path); |
220 vd->path = g_strdup(path); | |
9 | 221 |
381 | 222 filelist_free(VDLIST_INFO(vd, list)); |
223 VDLIST_INFO(vd, list) = NULL; | |
9 | 224 |
381 | 225 ret = filelist_read(vd->path, NULL, &VDLIST_INFO(vd, list)); |
9 | 226 |
381 | 227 VDLIST_INFO(vd, list) = filelist_sort(VDLIST_INFO(vd, list), SORT_NAME, TRUE); |
9 | 228 |
229 /* add . and .. */ | |
230 | |
381 | 231 if (strcmp(vd->path, "/") != 0) |
9 | 232 { |
381 | 233 filepath = g_strconcat(vd->path, "/", "..", NULL); |
138 | 234 fd = file_data_new_simple(filepath); |
381 | 235 VDLIST_INFO(vd, list) = g_list_prepend(VDLIST_INFO(vd, list), fd); |
138 | 236 g_free(filepath); |
9 | 237 } |
373
61a3c8b05b24
Add a new option in Preferences > Filtering to allow the
zas_
parents:
356
diff
changeset
|
238 |
61a3c8b05b24
Add a new option in Preferences > Filtering to allow the
zas_
parents:
356
diff
changeset
|
239 if (options->file_filter.show_dot_directory) |
61a3c8b05b24
Add a new option in Preferences > Filtering to allow the
zas_
parents:
356
diff
changeset
|
240 { |
381 | 241 filepath = g_strconcat(vd->path, "/", ".", NULL); |
373
61a3c8b05b24
Add a new option in Preferences > Filtering to allow the
zas_
parents:
356
diff
changeset
|
242 fd = file_data_new_simple(filepath); |
381 | 243 VDLIST_INFO(vd, list) = g_list_prepend(VDLIST_INFO(vd, list), fd); |
373
61a3c8b05b24
Add a new option in Preferences > Filtering to allow the
zas_
parents:
356
diff
changeset
|
244 g_free(filepath); |
61a3c8b05b24
Add a new option in Preferences > Filtering to allow the
zas_
parents:
356
diff
changeset
|
245 } |
9 | 246 |
381 | 247 vdlist_populate(vd); |
9 | 248 |
249 if (old_path) | |
250 { | |
251 /* scroll to make last path visible */ | |
252 FileData *found = NULL; | |
253 GList *work; | |
254 | |
381 | 255 work = VDLIST_INFO(vd, list); |
9 | 256 while (work && !found) |
257 { | |
258 FileData *fd = work->data; | |
259 if (strcmp(old_path, fd->name) == 0) found = fd; | |
260 work = work->next; | |
261 } | |
262 | |
381 | 263 if (found) vdlist_scroll_to_row(vd, found, 0.5); |
9 | 264 |
265 g_free(old_path); | |
266 return ret; | |
267 } | |
268 | |
381 | 269 if (GTK_WIDGET_REALIZED(vd->view)) |
9 | 270 { |
381 | 271 gtk_tree_view_scroll_to_point(GTK_TREE_VIEW(vd->view), 0, 0); |
9 | 272 } |
273 | |
274 return ret; | |
275 } | |
276 | |
381 | 277 void vdlist_refresh(ViewDir *vd) |
9 | 278 { |
279 gchar *path; | |
280 | |
381 | 281 path = g_strdup(vd->path); |
282 vd->path = NULL; | |
283 vdlist_set_path(vd, path); | |
9 | 284 g_free(path); |
285 } | |
286 | |
287 static void vdlist_menu_position_cb(GtkMenu *menu, gint *x, gint *y, gboolean *push_in, gpointer data) | |
288 { | |
381 | 289 ViewDir *vd = data; |
9 | 290 GtkTreeModel *store; |
291 GtkTreeIter iter; | |
292 GtkTreePath *tpath; | |
293 gint cw, ch; | |
294 | |
389 | 295 if (vd_find_row(vd, vd->click_fd, &iter) < 0) return; |
381 | 296 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view)); |
9 | 297 tpath = gtk_tree_model_get_path(store, &iter); |
381 | 298 tree_view_get_cell_clamped(GTK_TREE_VIEW(vd->view), tpath, 0, TRUE, x, y, &cw, &ch); |
9 | 299 gtk_tree_path_free(tpath); |
300 *y += ch; | |
301 popup_menu_position_clamp(menu, x, y, 0); | |
302 } | |
303 | |
304 static gint vdlist_press_key_cb(GtkWidget *widget, GdkEventKey *event, gpointer data) | |
305 { | |
381 | 306 ViewDir *vd = data; |
9 | 307 GtkTreePath *tpath; |
308 | |
309 if (event->keyval != GDK_Menu) return FALSE; | |
310 | |
381 | 311 gtk_tree_view_get_cursor(GTK_TREE_VIEW(vd->view), &tpath, NULL); |
9 | 312 if (tpath) |
313 { | |
314 GtkTreeModel *store; | |
315 GtkTreeIter iter; | |
316 | |
317 store = gtk_tree_view_get_model(GTK_TREE_VIEW(widget)); | |
318 gtk_tree_model_get_iter(store, &iter, tpath); | |
381 | 319 gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &vd->click_fd, -1); |
9 | 320 |
321 gtk_tree_path_free(tpath); | |
322 } | |
323 else | |
324 { | |
381 | 325 vd->click_fd = NULL; |
9 | 326 } |
327 | |
383
499d7ba62261
Move some dnd common code from view_dir_list.c and view_dir_tree.c
zas_
parents:
381
diff
changeset
|
328 vd_color_set(vd, vd->click_fd, TRUE); |
9 | 329 |
388
5186f8e38cb8
Make directory view popup menu common and move it to view_dir.{c,h}.
zas_
parents:
385
diff
changeset
|
330 vd->popup = vd_pop_menu(vd, vd->click_fd); |
9 | 331 |
381 | 332 gtk_menu_popup(GTK_MENU(vd->popup), NULL, NULL, vdlist_menu_position_cb, vd, 0, GDK_CURRENT_TIME); |
9 | 333 |
334 return TRUE; | |
335 } | |
336 | |
337 static gint vdlist_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data) | |
338 { | |
381 | 339 ViewDir *vd = data; |
9 | 340 GtkTreePath *tpath; |
341 GtkTreeIter iter; | |
342 FileData *fd = NULL; | |
343 | |
344 if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget), bevent->x, bevent->y, | |
345 &tpath, NULL, NULL, NULL)) | |
346 { | |
347 GtkTreeModel *store; | |
348 | |
349 store = gtk_tree_view_get_model(GTK_TREE_VIEW(widget)); | |
350 gtk_tree_model_get_iter(store, &iter, tpath); | |
351 gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &fd, -1); | |
352 gtk_tree_view_set_cursor(GTK_TREE_VIEW(widget), tpath, NULL, FALSE); | |
353 gtk_tree_path_free(tpath); | |
354 } | |
355 | |
381 | 356 vd->click_fd = fd; |
383
499d7ba62261
Move some dnd common code from view_dir_list.c and view_dir_tree.c
zas_
parents:
381
diff
changeset
|
357 vd_color_set(vd, vd->click_fd, TRUE); |
9 | 358 |
359 if (bevent->button == 3) | |
360 { | |
388
5186f8e38cb8
Make directory view popup menu common and move it to view_dir.{c,h}.
zas_
parents:
385
diff
changeset
|
361 vd->popup = vd_pop_menu(vd, vd->click_fd); |
381 | 362 gtk_menu_popup(GTK_MENU(vd->popup), NULL, NULL, NULL, NULL, |
9 | 363 bevent->button, bevent->time); |
364 } | |
365 | |
366 return TRUE; | |
367 } | |
368 | |
369 static gint vdlist_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data) | |
370 { | |
381 | 371 ViewDir *vd = data; |
9 | 372 GtkTreePath *tpath; |
373 GtkTreeIter iter; | |
374 FileData *fd = NULL; | |
375 | |
383
499d7ba62261
Move some dnd common code from view_dir_list.c and view_dir_tree.c
zas_
parents:
381
diff
changeset
|
376 vd_color_set(vd, vd->click_fd, FALSE); |
9 | 377 |
378 if (bevent->button != 1) return TRUE; | |
379 | |
380 if ((bevent->x != 0 || bevent->y != 0) && | |
381 gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget), bevent->x, bevent->y, | |
382 &tpath, NULL, NULL, NULL)) | |
383 { | |
384 GtkTreeModel *store; | |
385 | |
386 store = gtk_tree_view_get_model(GTK_TREE_VIEW(widget)); | |
387 gtk_tree_model_get_iter(store, &iter, tpath); | |
388 gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &fd, -1); | |
389 gtk_tree_path_free(tpath); | |
390 } | |
391 | |
381 | 392 if (fd && vd->click_fd == fd) |
9 | 393 { |
381 | 394 vdlist_select_row(vd, vd->click_fd); |
9 | 395 } |
396 | |
397 return TRUE; | |
398 } | |
399 | |
400 static void vdlist_select_cb(GtkTreeView *tview, GtkTreePath *tpath, GtkTreeViewColumn *column, gpointer data) | |
401 { | |
381 | 402 ViewDir *vd = data; |
9 | 403 GtkTreeModel *store; |
404 GtkTreeIter iter; | |
405 FileData *fd; | |
406 | |
407 store = gtk_tree_view_get_model(tview); | |
408 gtk_tree_model_get_iter(store, &iter, tpath); | |
409 gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &fd, -1); | |
410 | |
381 | 411 vdlist_select_row(vd, fd); |
9 | 412 } |
413 | |
414 static GdkColor *vdlist_color_shifted(GtkWidget *widget) | |
415 { | |
416 static GdkColor color; | |
417 static GtkWidget *done = NULL; | |
418 | |
419 if (done != widget) | |
420 { | |
421 GtkStyle *style; | |
384
392dd6541d51
Merge parts of view_dir_list/tree constructors/destructors to
zas_
parents:
383
diff
changeset
|
422 |
9 | 423 style = gtk_widget_get_style(widget); |
424 memcpy(&color, &style->base[GTK_STATE_NORMAL], sizeof(color)); | |
425 shift_color(&color, -1, 0); | |
426 done = widget; | |
427 } | |
428 | |
429 return &color; | |
430 } | |
431 | |
432 static void vdlist_color_cb(GtkTreeViewColumn *tree_column, GtkCellRenderer *cell, | |
433 GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data) | |
434 { | |
381 | 435 ViewDir *vd = data; |
9 | 436 gboolean set; |
437 | |
438 gtk_tree_model_get(tree_model, iter, DIR_COLUMN_COLOR, &set, -1); | |
439 g_object_set(G_OBJECT(cell), | |
381 | 440 "cell-background-gdk", vdlist_color_shifted(vd->view), |
9 | 441 "cell-background-set", set, NULL); |
442 } | |
443 | |
444 static void vdlist_destroy_cb(GtkWidget *widget, gpointer data) | |
445 { | |
381 | 446 ViewDir *vd = data; |
9 | 447 |
394 | 448 vd_dnd_drop_scroll_cancel(vd); |
381 | 449 widget_auto_scroll_stop(vd->view); |
9 | 450 |
381 | 451 filelist_free(VDLIST_INFO(vd, list)); |
9 | 452 } |
453 | |
384
392dd6541d51
Merge parts of view_dir_list/tree constructors/destructors to
zas_
parents:
383
diff
changeset
|
454 ViewDir *vdlist_new(ViewDir *vd, const gchar *path) |
9 | 455 { |
456 GtkListStore *store; | |
457 GtkTreeSelection *selection; | |
458 GtkTreeViewColumn *column; | |
459 GtkCellRenderer *renderer; | |
460 | |
381 | 461 vd->info = g_new0(ViewDirInfoList, 1); |
462 vd->type = DIRVIEW_LIST; | |
384
392dd6541d51
Merge parts of view_dir_list/tree constructors/destructors to
zas_
parents:
383
diff
changeset
|
463 vd->widget_destroy_cb = vdlist_destroy_cb; |
9 | 464 |
384
392dd6541d51
Merge parts of view_dir_list/tree constructors/destructors to
zas_
parents:
383
diff
changeset
|
465 VDLIST_INFO(vd, list) = NULL; |
9 | 466 |
467 store = gtk_list_store_new(4, G_TYPE_POINTER, GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_BOOLEAN); | |
381 | 468 vd->view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store)); |
9 | 469 g_object_unref(store); |
470 | |
381 | 471 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(vd->view), FALSE); |
472 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(vd->view), FALSE); | |
473 g_signal_connect(G_OBJECT(vd->view), "row_activated", | |
9 | 474 |
381 | 475 G_CALLBACK(vdlist_select_cb), vd); |
9 | 476 |
381 | 477 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(vd->view)); |
9 | 478 gtk_tree_selection_set_mode(selection, GTK_SELECTION_NONE); |
479 | |
480 column = gtk_tree_view_column_new(); | |
481 gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE); | |
482 | |
483 renderer = gtk_cell_renderer_pixbuf_new(); | |
484 gtk_tree_view_column_pack_start(column, renderer, FALSE); | |
485 gtk_tree_view_column_add_attribute(column, renderer, "pixbuf", DIR_COLUMN_ICON); | |
381 | 486 gtk_tree_view_column_set_cell_data_func(column, renderer, vdlist_color_cb, vd, NULL); |
9 | 487 |
488 renderer = gtk_cell_renderer_text_new(); | |
489 gtk_tree_view_column_pack_start(column, renderer, TRUE); | |
490 gtk_tree_view_column_add_attribute(column, renderer, "text", DIR_COLUMN_NAME); | |
381 | 491 gtk_tree_view_column_set_cell_data_func(column, renderer, vdlist_color_cb, vd, NULL); |
9 | 492 |
381 | 493 gtk_tree_view_append_column(GTK_TREE_VIEW(vd->view), column); |
9 | 494 |
381 | 495 g_signal_connect(G_OBJECT(vd->view), "key_press_event", |
496 G_CALLBACK(vdlist_press_key_cb), vd); | |
497 gtk_container_add(GTK_CONTAINER(vd->widget), vd->view); | |
498 gtk_widget_show(vd->view); | |
9 | 499 |
394 | 500 vd_dnd_init(vd); |
9 | 501 |
381 | 502 g_signal_connect(G_OBJECT(vd->view), "button_press_event", |
503 G_CALLBACK(vdlist_press_cb), vd); | |
504 g_signal_connect(G_OBJECT(vd->view), "button_release_event", | |
505 G_CALLBACK(vdlist_release_cb), vd); | |
9 | 506 |
381 | 507 if (path) vdlist_set_path(vd, path); |
9 | 508 |
381 | 509 return vd; |
9 | 510 } |