Mercurial > geeqie
annotate src/view_dir_list.c @ 664:db373fb1b9d8
Merge vflist_row_by_path() into vflist_index_by_path().
author | zas_ |
---|---|
date | Wed, 14 May 2008 12:59:00 +0000 |
parents | 905688aa2317 |
children | 9973edfd86f6 |
rev | line source |
---|---|
9 | 1 /* |
196 | 2 * Geeqie |
9 | 3 * (C) 2004 John Ellis |
475 | 4 * Copyright (C) 2008 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_list.h" |
15 | |
16 #include "dnd.h" | |
17 #include "dupe.h" | |
586 | 18 #include "filedata.h" |
9 | 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:
373
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:
373
diff
changeset
|
32 #define VDLIST_INFO(_vd_, _part_) (((ViewDirInfoList *)(_vd_->info))->_part_) |
9 | 33 |
34 | |
35 /* | |
36 *----------------------------------------------------------------------------- | |
37 * misc | |
38 *----------------------------------------------------------------------------- | |
39 */ | |
40 | |
383
499d7ba62261
Move some dnd common code from view_dir_list.c and view_dir_tree.c
zas_
parents:
381
diff
changeset
|
41 gint vdlist_find_row(ViewDir *vd, FileData *fd, GtkTreeIter *iter) |
9 | 42 { |
43 GtkTreeModel *store; | |
44 gint valid; | |
45 gint row = 0; | |
46 | |
381 | 47 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view)); |
9 | 48 valid = gtk_tree_model_get_iter_first(store, iter); |
49 while (valid) | |
50 { | |
51 FileData *fd_n; | |
52 gtk_tree_model_get(GTK_TREE_MODEL(store), iter, DIR_COLUMN_POINTER, &fd_n, -1); | |
53 if (fd_n == fd) return row; | |
54 | |
55 valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), iter); | |
56 row++; | |
57 } | |
58 | |
59 return -1; | |
60 } | |
61 | |
62 | |
388
5186f8e38cb8
Make directory view popup menu common and move it to view_dir.{c,h}.
zas_
parents:
385
diff
changeset
|
63 FileData *vdlist_row_by_path(ViewDir *vd, const gchar *path, gint *row) |
9 | 64 { |
65 GList *work; | |
66 gint n; | |
67 | |
68 if (!path) | |
69 { | |
70 if (row) *row = -1; | |
71 return NULL; | |
72 } | |
73 | |
74 n = 0; | |
381 | 75 work = VDLIST_INFO(vd, list); |
9 | 76 while (work) |
77 { | |
78 FileData *fd = work->data; | |
79 if (strcmp(fd->path, path) == 0) | |
80 { | |
81 if (row) *row = n; | |
82 return fd; | |
83 } | |
84 work = work->next; | |
85 n++; | |
86 } | |
87 | |
88 if (row) *row = -1; | |
89 return NULL; | |
90 } | |
91 | |
92 /* | |
93 *----------------------------------------------------------------------------- | |
94 * dnd | |
95 *----------------------------------------------------------------------------- | |
96 */ | |
97 | |
381 | 98 static void vdlist_scroll_to_row(ViewDir *vd, FileData *fd, gfloat y_align) |
9 | 99 { |
100 GtkTreeIter iter; | |
101 | |
394 | 102 if (GTK_WIDGET_REALIZED(vd->view) && vd_find_row(vd, fd, &iter) >= 0) |
9 | 103 { |
104 GtkTreeModel *store; | |
105 GtkTreePath *tpath; | |
106 | |
381 | 107 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view)); |
9 | 108 tpath = gtk_tree_model_get_path(store, &iter); |
381 | 109 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(vd->view), tpath, NULL, TRUE, y_align, 0.0); |
110 gtk_tree_view_set_cursor(GTK_TREE_VIEW(vd->view), tpath, NULL, FALSE); | |
9 | 111 gtk_tree_path_free(tpath); |
112 | |
381 | 113 if (!GTK_WIDGET_HAS_FOCUS(vd->view)) gtk_widget_grab_focus(vd->view); |
9 | 114 } |
115 } | |
116 | |
117 /* | |
118 *----------------------------------------------------------------------------- | |
119 * main | |
120 *----------------------------------------------------------------------------- | |
442 | 121 */ |
9 | 122 |
396 | 123 void vdlist_select_row(ViewDir *vd, FileData *fd) |
9 | 124 { |
381 | 125 if (fd && vd->select_func) |
9 | 126 { |
127 gchar *path; | |
128 | |
129 path = g_strdup(fd->path); | |
381 | 130 vd->select_func(vd, path, vd->select_data); |
9 | 131 g_free(path); |
132 } | |
133 } | |
134 | |
381 | 135 const gchar *vdlist_row_get_path(ViewDir *vd, gint row) |
9 | 136 { |
137 FileData *fd; | |
138 | |
381 | 139 fd = g_list_nth_data(VDLIST_INFO(vd, list), row); |
9 | 140 |
141 if (fd) return fd->path; | |
142 | |
143 return NULL; | |
144 } | |
145 | |
381 | 146 static void vdlist_populate(ViewDir *vd) |
9 | 147 { |
148 GtkListStore *store; | |
149 GList *work; | |
150 | |
381 | 151 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view))); |
9 | 152 gtk_list_store_clear(store); |
153 | |
381 | 154 work = VDLIST_INFO(vd, list); |
9 | 155 while (work) |
156 { | |
157 FileData *fd; | |
158 GtkTreeIter iter; | |
159 GdkPixbuf *pixbuf; | |
160 | |
161 fd = work->data; | |
162 | |
163 if (access_file(fd->path, R_OK | X_OK) && fd->name) | |
164 { | |
165 if (fd->name[0] == '.' && fd->name[1] == '\0') | |
166 { | |
381 | 167 pixbuf = vd->pf->open; |
9 | 168 } |
169 else if (fd->name[0] == '.' && fd->name[1] == '.' && fd->name[2] == '\0') | |
170 { | |
381 | 171 pixbuf = vd->pf->parent; |
9 | 172 } |
173 else | |
174 { | |
381 | 175 pixbuf = vd->pf->close; |
9 | 176 } |
177 } | |
178 else | |
179 { | |
381 | 180 pixbuf = vd->pf->deny; |
9 | 181 } |
182 | |
183 gtk_list_store_append(store, &iter); | |
184 gtk_list_store_set(store, &iter, | |
185 DIR_COLUMN_POINTER, fd, | |
186 DIR_COLUMN_ICON, pixbuf, | |
187 DIR_COLUMN_NAME, fd->name, -1); | |
188 | |
189 work = work->next; | |
190 } | |
191 | |
381 | 192 vd->click_fd = NULL; |
193 vd->drop_fd = NULL; | |
9 | 194 } |
195 | |
381 | 196 gint vdlist_set_path(ViewDir *vd, const gchar *path) |
9 | 197 { |
198 gint ret; | |
199 FileData *fd; | |
200 gchar *old_path = NULL; | |
138 | 201 gchar *filepath; |
9 | 202 |
203 if (!path) return FALSE; | |
381 | 204 if (vd->path && strcmp(path, vd->path) == 0) return TRUE; |
9 | 205 |
381 | 206 if (vd->path) |
9 | 207 { |
208 gchar *base; | |
209 | |
381 | 210 base = remove_level_from_path(vd->path); |
9 | 211 if (strcmp(base, path) == 0) |
212 { | |
381 | 213 old_path = g_strdup(filename_from_path(vd->path)); |
9 | 214 } |
215 g_free(base); | |
216 } | |
217 | |
381 | 218 g_free(vd->path); |
219 vd->path = g_strdup(path); | |
9 | 220 |
381 | 221 filelist_free(VDLIST_INFO(vd, list)); |
222 VDLIST_INFO(vd, list) = NULL; | |
9 | 223 |
381 | 224 ret = filelist_read(vd->path, NULL, &VDLIST_INFO(vd, list)); |
9 | 225 |
381 | 226 VDLIST_INFO(vd, list) = filelist_sort(VDLIST_INFO(vd, list), SORT_NAME, TRUE); |
9 | 227 |
228 /* add . and .. */ | |
229 | |
381 | 230 if (strcmp(vd->path, "/") != 0) |
9 | 231 { |
442 | 232 filepath = g_strconcat(vd->path, "/", "..", NULL); |
138 | 233 fd = file_data_new_simple(filepath); |
381 | 234 VDLIST_INFO(vd, list) = g_list_prepend(VDLIST_INFO(vd, list), fd); |
138 | 235 g_free(filepath); |
9 | 236 } |
442 | 237 |
373
61a3c8b05b24
Add a new option in Preferences > Filtering to allow the
zas_
parents:
356
diff
changeset
|
238 if (options->file_filter.show_dot_directory) |
61a3c8b05b24
Add a new option in Preferences > Filtering to allow the
zas_
parents:
356
diff
changeset
|
239 { |
442 | 240 filepath = g_strconcat(vd->path, "/", ".", NULL); |
373
61a3c8b05b24
Add a new option in Preferences > Filtering to allow the
zas_
parents:
356
diff
changeset
|
241 fd = file_data_new_simple(filepath); |
381 | 242 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
|
243 g_free(filepath); |
61a3c8b05b24
Add a new option in Preferences > Filtering to allow the
zas_
parents:
356
diff
changeset
|
244 } |
9 | 245 |
381 | 246 vdlist_populate(vd); |
9 | 247 |
248 if (old_path) | |
249 { | |
250 /* scroll to make last path visible */ | |
251 FileData *found = NULL; | |
252 GList *work; | |
253 | |
381 | 254 work = VDLIST_INFO(vd, list); |
9 | 255 while (work && !found) |
256 { | |
257 FileData *fd = work->data; | |
258 if (strcmp(old_path, fd->name) == 0) found = fd; | |
259 work = work->next; | |
260 } | |
261 | |
381 | 262 if (found) vdlist_scroll_to_row(vd, found, 0.5); |
9 | 263 |
264 g_free(old_path); | |
265 return ret; | |
266 } | |
267 | |
381 | 268 if (GTK_WIDGET_REALIZED(vd->view)) |
9 | 269 { |
381 | 270 gtk_tree_view_scroll_to_point(GTK_TREE_VIEW(vd->view), 0, 0); |
9 | 271 } |
272 | |
273 return ret; | |
274 } | |
275 | |
381 | 276 void vdlist_refresh(ViewDir *vd) |
9 | 277 { |
278 gchar *path; | |
279 | |
381 | 280 path = g_strdup(vd->path); |
281 vd->path = NULL; | |
282 vdlist_set_path(vd, path); | |
9 | 283 g_free(path); |
284 } | |
285 | |
401
0a2e1b130a25
Add some wrappers in view_dir.c and simplify even more.
zas_
parents:
397
diff
changeset
|
286 gint vdlist_press_key_cb(GtkWidget *widget, GdkEventKey *event, gpointer data) |
9 | 287 { |
381 | 288 ViewDir *vd = data; |
9 | 289 GtkTreePath *tpath; |
442 | 290 |
9 | 291 if (event->keyval != GDK_Menu) return FALSE; |
292 | |
381 | 293 gtk_tree_view_get_cursor(GTK_TREE_VIEW(vd->view), &tpath, NULL); |
9 | 294 if (tpath) |
295 { | |
296 GtkTreeModel *store; | |
297 GtkTreeIter iter; | |
298 | |
299 store = gtk_tree_view_get_model(GTK_TREE_VIEW(widget)); | |
300 gtk_tree_model_get_iter(store, &iter, tpath); | |
381 | 301 gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &vd->click_fd, -1); |
442 | 302 |
9 | 303 gtk_tree_path_free(tpath); |
304 } | |
305 else | |
306 { | |
381 | 307 vd->click_fd = NULL; |
9 | 308 } |
309 | |
383
499d7ba62261
Move some dnd common code from view_dir_list.c and view_dir_tree.c
zas_
parents:
381
diff
changeset
|
310 vd_color_set(vd, vd->click_fd, TRUE); |
9 | 311 |
388
5186f8e38cb8
Make directory view popup menu common and move it to view_dir.{c,h}.
zas_
parents:
385
diff
changeset
|
312 vd->popup = vd_pop_menu(vd, vd->click_fd); |
9 | 313 |
395 | 314 gtk_menu_popup(GTK_MENU(vd->popup), NULL, NULL, vd_menu_position_cb, vd, 0, GDK_CURRENT_TIME); |
9 | 315 |
316 return TRUE; | |
317 } | |
318 | |
401
0a2e1b130a25
Add some wrappers in view_dir.c and simplify even more.
zas_
parents:
397
diff
changeset
|
319 gint vdlist_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data) |
9 | 320 { |
381 | 321 ViewDir *vd = data; |
9 | 322 GtkTreePath *tpath; |
323 GtkTreeIter iter; | |
324 FileData *fd = NULL; | |
325 | |
326 if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget), bevent->x, bevent->y, | |
327 &tpath, NULL, NULL, NULL)) | |
328 { | |
329 GtkTreeModel *store; | |
330 | |
331 store = gtk_tree_view_get_model(GTK_TREE_VIEW(widget)); | |
332 gtk_tree_model_get_iter(store, &iter, tpath); | |
333 gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &fd, -1); | |
334 gtk_tree_view_set_cursor(GTK_TREE_VIEW(widget), tpath, NULL, FALSE); | |
335 gtk_tree_path_free(tpath); | |
336 } | |
337 | |
381 | 338 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
|
339 vd_color_set(vd, vd->click_fd, TRUE); |
9 | 340 |
448
a73cc0fa14d0
Use explicit names for mouse buttons instead of numbers.
zas_
parents:
442
diff
changeset
|
341 if (bevent->button == MOUSE_BUTTON_RIGHT) |
9 | 342 { |
388
5186f8e38cb8
Make directory view popup menu common and move it to view_dir.{c,h}.
zas_
parents:
385
diff
changeset
|
343 vd->popup = vd_pop_menu(vd, vd->click_fd); |
381 | 344 gtk_menu_popup(GTK_MENU(vd->popup), NULL, NULL, NULL, NULL, |
9 | 345 bevent->button, bevent->time); |
346 } | |
347 | |
348 return TRUE; | |
349 } | |
350 | |
401
0a2e1b130a25
Add some wrappers in view_dir.c and simplify even more.
zas_
parents:
397
diff
changeset
|
351 void vdlist_destroy_cb(GtkWidget *widget, gpointer data) |
9 | 352 { |
381 | 353 ViewDir *vd = data; |
9 | 354 |
394 | 355 vd_dnd_drop_scroll_cancel(vd); |
381 | 356 widget_auto_scroll_stop(vd->view); |
9 | 357 |
381 | 358 filelist_free(VDLIST_INFO(vd, list)); |
9 | 359 } |
360 | |
384
392dd6541d51
Merge parts of view_dir_list/tree constructors/destructors to
zas_
parents:
383
diff
changeset
|
361 ViewDir *vdlist_new(ViewDir *vd, const gchar *path) |
9 | 362 { |
363 GtkListStore *store; | |
364 GtkTreeSelection *selection; | |
365 GtkTreeViewColumn *column; | |
366 GtkCellRenderer *renderer; | |
367 | |
381 | 368 vd->info = g_new0(ViewDirInfoList, 1); |
369 vd->type = DIRVIEW_LIST; | |
9 | 370 |
384
392dd6541d51
Merge parts of view_dir_list/tree constructors/destructors to
zas_
parents:
383
diff
changeset
|
371 VDLIST_INFO(vd, list) = NULL; |
9 | 372 |
373 store = gtk_list_store_new(4, G_TYPE_POINTER, GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_BOOLEAN); | |
381 | 374 vd->view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store)); |
9 | 375 g_object_unref(store); |
376 | |
381 | 377 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(vd->view), FALSE); |
378 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(vd->view), FALSE); | |
9 | 379 |
381 | 380 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(vd->view)); |
9 | 381 gtk_tree_selection_set_mode(selection, GTK_SELECTION_NONE); |
382 | |
383 column = gtk_tree_view_column_new(); | |
384 gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE); | |
385 | |
386 renderer = gtk_cell_renderer_pixbuf_new(); | |
387 gtk_tree_view_column_pack_start(column, renderer, FALSE); | |
388 gtk_tree_view_column_add_attribute(column, renderer, "pixbuf", DIR_COLUMN_ICON); | |
396 | 389 gtk_tree_view_column_set_cell_data_func(column, renderer, vd_color_cb, vd, NULL); |
9 | 390 |
391 renderer = gtk_cell_renderer_text_new(); | |
392 gtk_tree_view_column_pack_start(column, renderer, TRUE); | |
393 gtk_tree_view_column_add_attribute(column, renderer, "text", DIR_COLUMN_NAME); | |
396 | 394 gtk_tree_view_column_set_cell_data_func(column, renderer, vd_color_cb, vd, NULL); |
9 | 395 |
381 | 396 gtk_tree_view_append_column(GTK_TREE_VIEW(vd->view), column); |
9 | 397 |
381 | 398 return vd; |
9 | 399 } |