2 * Copyright (C) 2004 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "view-dir-list.h"
27 #include "ui-fileops.h"
28 #include "ui-tree-edit.h"
31 struct ViewDirInfoList
36 #define VDLIST(_vd_) ((ViewDirInfoList *)((_vd_)->info))
40 *-----------------------------------------------------------------------------
42 *-----------------------------------------------------------------------------
45 gboolean vdlist_find_row(ViewDir *vd, FileData *fd, GtkTreeIter *iter)
50 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view));
51 valid = gtk_tree_model_get_iter_first(store, iter);
55 gtk_tree_model_get(GTK_TREE_MODEL(store), iter, DIR_COLUMN_POINTER, &fd_n, -1);
56 if (fd_n == fd) return TRUE;
58 valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), iter);
65 FileData *vdlist_row_by_path(ViewDir *vd, const gchar *path, gint *row)
77 work = VDLIST(vd)->list;
80 auto fd = static_cast<FileData *>(work->data);
81 if (strcmp(fd->path, path) == 0)
95 *-----------------------------------------------------------------------------
97 *-----------------------------------------------------------------------------
100 static void vdlist_scroll_to_row(ViewDir *vd, FileData *fd, gfloat y_align)
104 if (gtk_widget_get_realized(vd->view) && vd_find_row(vd, fd, &iter))
109 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view));
110 tpath = gtk_tree_model_get_path(store, &iter);
111 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(vd->view), tpath, nullptr, TRUE, y_align, 0.0);
112 gtk_tree_view_set_cursor(GTK_TREE_VIEW(vd->view), tpath, nullptr, FALSE);
113 gtk_tree_path_free(tpath);
115 if (!gtk_widget_has_focus(vd->view)) gtk_widget_grab_focus(vd->view);
120 *-----------------------------------------------------------------------------
122 *-----------------------------------------------------------------------------
125 const gchar *vdlist_row_get_path(ViewDir *vd, gint row)
129 fd = static_cast<FileData *>(g_list_nth_data(VDLIST(vd)->list, row));
131 if (fd) return fd->path;
136 static gboolean vdlist_populate(ViewDir *vd, gboolean clear)
146 SortType sort_type = SORT_NAME;
147 gboolean sort_ascend = TRUE;
148 gboolean sort_case = TRUE;
149 gchar *link = nullptr;
153 sort_type = vd->layout->options.dir_view_list_sort.method;
154 sort_ascend = vd->layout->options.dir_view_list_sort.ascend;
155 sort_case = vd->layout->options.dir_view_list_sort.case_sensitive;
158 old_list = VDLIST(vd)->list;
160 ret = filelist_read(vd->dir_fd, nullptr, &VDLIST(vd)->list);
161 VDLIST(vd)->list = filelist_sort(VDLIST(vd)->list, sort_type, sort_ascend, sort_case);
165 if (options->file_filter.show_parent_directory && strcmp(vd->dir_fd->path, G_DIR_SEPARATOR_S) != 0)
167 filepath = g_build_filename(vd->dir_fd->path, "..", NULL);
168 fd = file_data_new_dir(filepath);
169 VDLIST(vd)->list = g_list_prepend(VDLIST(vd)->list, fd);
173 if (options->file_filter.show_dot_directory)
175 filepath = g_build_filename(vd->dir_fd->path, ".", NULL);
176 fd = file_data_new_dir(filepath);
177 VDLIST(vd)->list = g_list_prepend(VDLIST(vd)->list, fd);
181 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view)));
182 if (clear) gtk_list_store_clear(store);
184 valid = gtk_tree_model_iter_children(GTK_TREE_MODEL(store), &iter, nullptr);
186 work = VDLIST(vd)->list;
191 const gchar *date = "";
192 gboolean done = FALSE;
194 fd = static_cast<FileData *>(work->data);
196 if (access_file(fd->path, R_OK | X_OK) && fd->name)
198 if (islink(fd->path))
200 pixbuf = vd->pf->link;
202 else if (fd->name[0] == '.' && fd->name[1] == '\0')
204 pixbuf = vd->pf->open;
206 else if (fd->name[0] == '.' && fd->name[1] == '.' && fd->name[2] == '\0')
208 pixbuf = vd->pf->parent;
210 else if (!access_file(fd->path, W_OK) )
212 pixbuf = vd->pf->read_only;
216 pixbuf = vd->pf->close;
217 if (vd->layout && vd->layout->options.show_directory_date)
218 date = text_from_time(fd->date);
223 pixbuf = vd->pf->deny;
228 FileData *old_fd = nullptr;
232 gtk_tree_model_get(GTK_TREE_MODEL(store), &iter,
233 DIR_COLUMN_POINTER, &old_fd,
242 match = filelist_sort_compare_filedata_full(fd, old_fd, sort_type, sort_ascend);
244 if (match == 0) g_warning("multiple fd for the same path");
253 if (islink(fd->path))
255 link = realpath(fd->path, nullptr);
264 GtkTreeIter new_iter;
268 gtk_list_store_insert_before(store, &new_iter, &iter);
272 gtk_list_store_append(store, &new_iter);
275 gtk_list_store_set(store, &new_iter,
276 DIR_COLUMN_POINTER, fd,
277 DIR_COLUMN_ICON, pixbuf,
278 DIR_COLUMN_NAME, fd->name,
279 DIR_COLUMN_LINK, link,
280 DIR_COLUMN_DATE, date,
287 valid = gtk_list_store_remove(store, &iter);
291 gtk_list_store_set(store, &iter,
292 DIR_COLUMN_ICON, pixbuf,
293 DIR_COLUMN_NAME, fd->name,
294 DIR_COLUMN_LINK, link,
295 DIR_COLUMN_DATE, date,
298 if (valid) valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
309 gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, DIR_COLUMN_POINTER, &old_fd, -1);
311 valid = gtk_list_store_remove(store, &iter);
315 vd->click_fd = nullptr;
316 vd->drop_fd = nullptr;
318 filelist_free(old_list);
323 gboolean vdlist_set_fd(ViewDir *vd, FileData *dir_fd)
326 gchar *old_path = nullptr; /* Used to store directory for walking up */
328 if (!dir_fd) return FALSE;
329 if (vd->dir_fd == dir_fd) return TRUE;
335 base = remove_level_from_path(vd->dir_fd->path);
336 if (strcmp(base, dir_fd->path) == 0)
338 old_path = g_strdup(filename_from_path(vd->dir_fd->path));
343 file_data_unref(vd->dir_fd);
344 vd->dir_fd = file_data_ref(dir_fd);
346 ret = vdlist_populate(vd, TRUE);
348 /* scroll to make last path visible */
349 FileData *found = nullptr;
352 work = VDLIST(vd)->list;
353 while (work && !found)
355 auto fd = static_cast<FileData *>(work->data);
356 if (!old_path || strcmp(old_path, fd->name) == 0) found = fd;
360 if (found) vdlist_scroll_to_row(vd, found, 0.5);
362 if (old_path) g_free(old_path);
367 void vdlist_refresh(ViewDir *vd)
369 vdlist_populate(vd, FALSE);
372 gboolean vdlist_press_key_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
374 auto vd = static_cast<ViewDir *>(data);
377 if (event->keyval != GDK_KEY_Menu) return FALSE;
379 gtk_tree_view_get_cursor(GTK_TREE_VIEW(vd->view), &tpath, nullptr);
385 store = gtk_tree_view_get_model(GTK_TREE_VIEW(widget));
386 gtk_tree_model_get_iter(store, &iter, tpath);
387 gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &vd->click_fd, -1);
389 gtk_tree_path_free(tpath);
393 vd->click_fd = nullptr;
396 vd_color_set(vd, vd->click_fd, TRUE);
398 vd->popup = vd_pop_menu(vd, vd->click_fd);
400 gtk_menu_popup_at_pointer(GTK_MENU(vd->popup), nullptr);
405 gboolean vdlist_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
407 auto vd = static_cast<ViewDir *>(data);
410 FileData *fd = nullptr;
412 if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget), bevent->x, bevent->y,
413 &tpath, nullptr, nullptr, nullptr))
417 store = gtk_tree_view_get_model(GTK_TREE_VIEW(widget));
418 gtk_tree_model_get_iter(store, &iter, tpath);
419 gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &fd, -1);
420 gtk_tree_view_set_cursor(GTK_TREE_VIEW(widget), tpath, nullptr, FALSE);
421 gtk_tree_path_free(tpath);
426 if (options->view_dir_list_single_click_enter)
427 vd_color_set(vd, vd->click_fd, TRUE);
429 if (bevent->button == MOUSE_BUTTON_RIGHT)
431 vd->popup = vd_pop_menu(vd, vd->click_fd);
432 gtk_menu_popup_at_pointer(GTK_MENU(vd->popup), nullptr);
436 return options->view_dir_list_single_click_enter;
439 void vdlist_destroy_cb(GtkWidget *, gpointer data)
441 auto vd = static_cast<ViewDir *>(data);
443 vd_dnd_drop_scroll_cancel(vd);
444 widget_auto_scroll_stop(vd->view);
446 filelist_free(VDLIST(vd)->list);
449 ViewDir *vdlist_new(ViewDir *vd, FileData *)
452 GtkTreeSelection *selection;
453 GtkTreeViewColumn *column;
454 GtkCellRenderer *renderer;
456 vd->info = g_new0(ViewDirInfoList, 1);
458 vd->type = DIRVIEW_LIST;
460 store = gtk_list_store_new(6, G_TYPE_POINTER, GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_BOOLEAN, G_TYPE_STRING, G_TYPE_STRING);
461 vd->view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
462 g_object_unref(store);
464 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(vd->view), FALSE);
465 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(vd->view), FALSE);
467 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(vd->view));
468 gtk_tree_selection_set_mode(selection, GTK_SELECTION_NONE);
470 column = gtk_tree_view_column_new();
471 gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
473 renderer = gtk_cell_renderer_pixbuf_new();
474 gtk_tree_view_column_pack_start(column, renderer, FALSE);
475 gtk_tree_view_column_add_attribute(column, renderer, "pixbuf", DIR_COLUMN_ICON);
476 gtk_tree_view_column_set_cell_data_func(column, renderer, vd_color_cb, vd, nullptr);
478 renderer = gtk_cell_renderer_text_new();
479 gtk_tree_view_column_pack_start(column, renderer, TRUE);
480 gtk_tree_view_column_add_attribute(column, renderer, "text", DIR_COLUMN_NAME);
481 gtk_tree_view_column_set_cell_data_func(column, renderer, vd_color_cb, vd, nullptr);
483 renderer = gtk_cell_renderer_text_new();
484 gtk_tree_view_column_pack_start(column, renderer, TRUE);
485 gtk_tree_view_column_add_attribute(column, renderer, "text", DIR_COLUMN_DATE);
486 gtk_tree_view_column_set_cell_data_func(column, renderer, vd_color_cb, vd, nullptr);
488 gtk_tree_view_append_column(GTK_TREE_VIEW(vd->view), column);
490 gtk_tree_view_set_tooltip_column(GTK_TREE_VIEW(vd->view), DIR_COLUMN_LINK);
494 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */