📄 view_file_list.c
字号:
/* * GQview * (C) 2004 John Ellis * * Author: John Ellis * * This software is released under the GNU General Public License (GNU GPL). * Please read the included file COPYING for more information. * This software comes with no warranty of any kind, use at your own risk! */#include "gqview.h"#include "view_file_list.h"#include "cache_maint.h"#include "dnd.h"#include "editors.h"#include "img-view.h"#include "info.h"#include "layout.h"#include "layout_image.h"#include "menu.h"#include "thumb.h"#include "utilops.h"#include "ui_bookmark.h"#include "ui_fileops.h"#include "ui_menu.h"#include "ui_tree_edit.h"#include <gdk/gdkkeysyms.h> /* for keyboard values */enum { FILE_COLUMN_POINTER = 0, FILE_COLUMN_THUMB, FILE_COLUMN_NAME, FILE_COLUMN_SIZE, FILE_COLUMN_DATE, FILE_COLUMN_COLOR, FILE_COLUMN_COUNT};static gint vflist_row_is_selected(ViewFileList *vfl, FileData *fd);static gint vflist_row_rename_cb(TreeEditData *td, const gchar *old, const gchar *new, gpointer data);static void vflist_populate_view(ViewFileList *vfl);/* *----------------------------------------------------------------------------- * signals *----------------------------------------------------------------------------- */static void vflist_send_update(ViewFileList *vfl){ if (vfl->func_status) vfl->func_status(vfl, vfl->data_status);}/* *----------------------------------------------------------------------------- * misc *----------------------------------------------------------------------------- */static gint vflist_find_row(ViewFileList *vfl, FileData *fd, GtkTreeIter *iter){ GtkTreeModel *store; gint valid; gint row = 0; store = gtk_tree_view_get_model(GTK_TREE_VIEW(vfl->listview)); valid = gtk_tree_model_get_iter_first(store, iter); while (valid) { FileData *fd_n; gtk_tree_model_get(GTK_TREE_MODEL(store), iter, FILE_COLUMN_POINTER, &fd_n, -1); if (fd_n == fd) return row; valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), iter); row++; } return -1;}static void vflist_color_set(ViewFileList *vfl, FileData *fd, gint color_set){ GtkTreeModel *store; GtkTreeIter iter; if (vflist_find_row(vfl, fd, &iter) < 0) return; store = gtk_tree_view_get_model(GTK_TREE_VIEW(vfl->listview)); gtk_list_store_set(GTK_LIST_STORE(store), &iter, FILE_COLUMN_COLOR, color_set, -1);}static void vflist_move_cursor(ViewFileList *vfl, GtkTreeIter *iter){ GtkTreeModel *store; GtkTreePath *tpath; store = gtk_tree_view_get_model(GTK_TREE_VIEW(vfl->listview)); tpath = gtk_tree_model_get_path(store, iter); gtk_tree_view_set_cursor(GTK_TREE_VIEW(vfl->listview), tpath, NULL, FALSE); gtk_tree_path_free(tpath);}/* *----------------------------------------------------------------------------- * dnd *----------------------------------------------------------------------------- */static void vflist_dnd_get(GtkWidget *widget, GdkDragContext *context, GtkSelectionData *selection_data, guint info, guint time, gpointer data){ ViewFileList *vfl = data; GList *list = NULL; gchar *uri_text = NULL; gint total; if (!vfl->click_fd) return; if (vflist_row_is_selected(vfl, vfl->click_fd)) { list = vflist_selection_get_list(vfl); } else { list = g_list_append(NULL, g_strdup(vfl->click_fd->path)); } if (!list) return; uri_text = uri_text_from_list(list, &total, (info == TARGET_TEXT_PLAIN)); path_list_free(list); if (debug) printf(uri_text); gtk_selection_data_set(selection_data, selection_data->target, 8, uri_text, total); g_free(uri_text);}static void vflist_dnd_begin(GtkWidget *widget, GdkDragContext *context, gpointer data){ ViewFileList *vfl = data; vflist_color_set(vfl, vfl->click_fd, TRUE); if (vfl->thumbs_enabled && vfl->click_fd && vfl->click_fd->pixbuf) { gint items; if (vflist_row_is_selected(vfl, vfl->click_fd)) items = vflist_selection_count(vfl, NULL); else items = 1; dnd_set_drag_icon(widget, context, vfl->click_fd->pixbuf, items); }}static void vflist_dnd_end(GtkWidget *widget, GdkDragContext *context, gpointer data){ ViewFileList *vfl = data; vflist_color_set(vfl, vfl->click_fd, FALSE); if (context->action == GDK_ACTION_MOVE) { vflist_refresh(vfl); }}static void vflist_dnd_init(ViewFileList *vfl){ gtk_drag_source_set(vfl->listview, GDK_BUTTON1_MASK | GDK_BUTTON2_MASK, dnd_file_drag_types, dnd_file_drag_types_count, GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK); g_signal_connect(G_OBJECT(vfl->listview), "drag_data_get", G_CALLBACK(vflist_dnd_get), vfl); g_signal_connect(G_OBJECT(vfl->listview), "drag_begin", G_CALLBACK(vflist_dnd_begin), vfl); g_signal_connect(G_OBJECT(vfl->listview), "drag_end", G_CALLBACK(vflist_dnd_end), vfl);}/* *----------------------------------------------------------------------------- * pop-up menu *----------------------------------------------------------------------------- */static GList *vflist_pop_menu_file_list(ViewFileList *vfl){ if (!vfl->click_fd) return NULL; if (vflist_row_is_selected(vfl, vfl->click_fd)) { return vflist_selection_get_list(vfl); } return g_list_append(NULL, g_strdup(vfl->click_fd->path));}static void vflist_pop_menu_edit_cb(GtkWidget *widget, gpointer data){ ViewFileList *vfl; gint n; GList *list; vfl = submenu_item_get_data(widget); n = GPOINTER_TO_INT(data); if (!vfl) return; list = vflist_pop_menu_file_list(vfl); start_editor_from_path_list(n, list); path_list_free(list);}static void vflist_pop_menu_info_cb(GtkWidget *widget, gpointer data){ ViewFileList *vfl = data; info_window_new(NULL, vflist_pop_menu_file_list(vfl));}static void vflist_pop_menu_view_cb(GtkWidget *widget, gpointer data){ ViewFileList *vfl = data; if (vflist_row_is_selected(vfl, vfl->click_fd)) { GList *list; list = vflist_selection_get_list(vfl); view_window_new_from_list(list); path_list_free(list); } else { const gchar *path; path = vfl->click_fd->path; view_window_new(path); }}static void vflist_pop_menu_copy_cb(GtkWidget *widget, gpointer data){ ViewFileList *vfl = data; file_util_copy(NULL, vflist_pop_menu_file_list(vfl), NULL, vfl->listview);}static void vflist_pop_menu_move_cb(GtkWidget *widget, gpointer data){ ViewFileList *vfl = data; file_util_move(NULL, vflist_pop_menu_file_list(vfl), NULL, vfl->listview);}static void vflist_pop_menu_rename_cb(GtkWidget *widget, gpointer data){ ViewFileList *vfl = data; GList *list; list = vflist_pop_menu_file_list(vfl); if (enable_in_place_rename && list && !list->next && vfl->click_fd) { GtkTreeModel *store; GtkTreeIter iter; path_list_free(list); store = gtk_tree_view_get_model(GTK_TREE_VIEW(vfl->listview)); if (vflist_find_row(vfl, vfl->click_fd, &iter) >= 0) { GtkTreePath *tpath; tpath = gtk_tree_model_get_path(store, &iter); tree_edit_by_path(GTK_TREE_VIEW(vfl->listview), tpath, FILE_COLUMN_NAME -1, vfl->click_fd->name, vflist_row_rename_cb, vfl); gtk_tree_path_free(tpath); } return; } file_util_rename(NULL, list, vfl->listview);}static void vflist_pop_menu_delete_cb(GtkWidget *widget, gpointer data){ ViewFileList *vfl = data; file_util_delete(NULL, vflist_pop_menu_file_list(vfl), vfl->listview);}static void vflist_pop_menu_sort_cb(GtkWidget *widget, gpointer data){ ViewFileList *vfl; SortType type; vfl = submenu_item_get_data(widget); if (!vfl) return; type = (SortType)GPOINTER_TO_INT(data); if (vfl->layout) { layout_sort_set(vfl->layout, type, vfl->sort_ascend); } else { vflist_sort_set(vfl, type, vfl->sort_ascend); }}static void vflist_pop_menu_sort_ascend_cb(GtkWidget *widget, gpointer data){ ViewFileList *vfl = data; if (vfl->layout) { layout_sort_set(vfl->layout, vfl->sort_method, !vfl->sort_ascend); } else { vflist_sort_set(vfl, vfl->sort_method, !vfl->sort_ascend); }}static void vflist_pop_menu_icons_cb(GtkWidget *widget, gpointer data){ ViewFileList *vfl = data; if (vfl->layout) layout_views_set(vfl->layout, vfl->layout->tree_view, TRUE);}static void vflist_pop_menu_thumbs_cb(GtkWidget *widget, gpointer data){ ViewFileList *vfl = data; vflist_color_set(vfl, vfl->click_fd, FALSE); if (vfl->layout) { layout_thumb_set(vfl->layout, !vfl->thumbs_enabled); } else { vflist_thumb_set(vfl, !vfl->thumbs_enabled); }}static void vflist_pop_menu_refresh_cb(GtkWidget *widget, gpointer data){ ViewFileList *vfl = data; vflist_color_set(vfl, vfl->click_fd, FALSE); vflist_refresh(vfl);}static void vflist_popup_destroy_cb(GtkWidget *widget, gpointer data){ ViewFileList *vfl = data; vflist_color_set(vfl, vfl->click_fd, FALSE); vfl->click_fd = NULL; vfl->popup = NULL;}static GtkWidget *vflist_pop_menu(ViewFileList *vfl, FileData *fd){ GtkWidget *menu; GtkWidget *item; GtkWidget *submenu; gint active; vflist_color_set(vfl, fd, TRUE); active = (fd != NULL); menu = popup_menu_short_lived(); g_signal_connect(G_OBJECT(menu), "destroy", G_CALLBACK(vflist_popup_destroy_cb), vfl); submenu_add_edit(menu, &item, G_CALLBACK(vflist_pop_menu_edit_cb), vfl); gtk_widget_set_sensitive(item, active); menu_item_add_stock_sensitive(menu, _("_Properties"), GTK_STOCK_PROPERTIES, active, G_CALLBACK(vflist_pop_menu_info_cb), vfl); menu_item_add_stock_sensitive(menu, _("View in _new window"), GTK_STOCK_NEW, active, G_CALLBACK(vflist_pop_menu_view_cb), vfl); menu_item_add_divider(menu); menu_item_add_stock_sensitive(menu, _("_Copy..."), GTK_STOCK_COPY, active, G_CALLBACK(vflist_pop_menu_copy_cb), vfl); menu_item_add_sensitive(menu, _("_Move..."), active, G_CALLBACK(vflist_pop_menu_move_cb), vfl); menu_item_add_sensitive(menu, _("_Rename..."), active, G_CALLBACK(vflist_pop_menu_rename_cb), vfl); menu_item_add_stock_sensitive(menu, _("_Delete..."), GTK_STOCK_DELETE, active, G_CALLBACK(vflist_pop_menu_delete_cb), vfl); menu_item_add_divider(menu); submenu = submenu_add_sort(NULL, G_CALLBACK(vflist_pop_menu_sort_cb), vfl, FALSE, FALSE, TRUE, vfl->sort_method); menu_item_add_divider(submenu); menu_item_add_check(submenu, _("Ascending"), vfl->sort_ascend, G_CALLBACK(vflist_pop_menu_sort_ascend_cb), vfl); item = menu_item_add(menu, _("_Sort"), NULL, NULL); gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu); menu_item_add_check(menu, _("View as _icons"), FALSE, G_CALLBACK(vflist_pop_menu_icons_cb), vfl); menu_item_add_check(menu, _("Show _thumbnails"), vfl->thumbs_enabled, G_CALLBACK(vflist_pop_menu_thumbs_cb), vfl); menu_item_add_stock(menu, _("Re_fresh"), GTK_STOCK_REFRESH, G_CALLBACK(vflist_pop_menu_refresh_cb), vfl); return menu;}/* *----------------------------------------------------------------------------- * callbacks *----------------------------------------------------------------------------- */static gint vflist_row_rename_cb(TreeEditData *td, const gchar *old, const gchar *new, gpointer data){ ViewFileList *vfl = data; gchar *old_path; gchar *new_path; if (strlen(new) == 0) return FALSE; old_path = concat_dir_and_file(vfl->path, old); new_path = concat_dir_and_file(vfl->path, new); if (strchr(new, '/') != NULL) { gchar *text = g_strdup_printf(_("Invalid file name:\n%s"), new); file_util_warning_dialog(_("Error renaming file"), text, GTK_STOCK_DIALOG_ERROR, vfl->listview); g_free(text); } else if (isfile(new_path)) { gchar *text = g_strdup_printf(_("A file with name %s already exists."), new); file_util_warning_dialog(_("Error renaming file"), text, GTK_STOCK_DIALOG_ERROR, vfl->listview); g_free(text); } else if (!rename_file(old_path, new_path)) { gchar *text = g_strdup_printf(_("Unable to rename file:\n%s\nto:\n%s"), old, new); file_util_warning_dialog(_("Error renaming file"), text, GTK_STOCK_DIALOG_ERROR, vfl->listview); g_free(text); } else { file_maint_renamed(old_path, new_path); } g_free(old_path); g_free(new_path); return FALSE;}static void vflist_menu_position_cb(GtkMenu *menu, gint *x, gint *y, gboolean *push_in, gpointer data){ ViewFileList *vfl = data; GtkTreeModel *store; GtkTreeIter iter; GtkTreePath *tpath; gint cw, ch; if (vflist_find_row(vfl, vfl->click_fd, &iter) < 0) return; store = gtk_tree_view_get_model(GTK_TREE_VIEW(vfl->listview)); tpath = gtk_tree_model_get_path(store, &iter); tree_view_get_cell_clamped(GTK_TREE_VIEW(vfl->listview), tpath, FILE_COLUMN_NAME - 1, TRUE, x, y, &cw, &ch); gtk_tree_path_free(tpath); *y += ch; popup_menu_position_clamp(menu, x, y, 0);}static gint vflist_press_key_cb(GtkWidget *widget, GdkEventKey *event, gpointer data){ ViewFileList *vfl = data; GtkTreePath *tpath; if (event->keyval != GDK_Menu) return FALSE; gtk_tree_view_get_cursor(GTK_TREE_VIEW(vfl->listview), &tpath, NULL); if (tpath) { GtkTreeModel *store; GtkTreeIter iter; store = gtk_tree_view_get_model(GTK_TREE_VIEW(widget)); gtk_tree_model_get_iter(store, &iter, tpath); gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &vfl->click_fd, -1); gtk_tree_path_free(tpath); } else { vfl->click_fd = NULL; } vfl->popup = vflist_pop_menu(vfl, vfl->click_fd); gtk_menu_popup(GTK_MENU(vfl->popup), NULL, NULL, vflist_menu_position_cb, vfl, 0, GDK_CURRENT_TIME); return TRUE;}static gint vflist_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data){ ViewFileList *vfl = data; GtkTreePath *tpath; GtkTreeIter iter; FileData *fd = NULL; if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget), bevent->x, bevent->y, &tpath, NULL, NULL, NULL)) { GtkTreeModel *store; store = gtk_tree_view_get_model(GTK_TREE_VIEW(widget)); gtk_tree_model_get_iter(store, &iter, tpath); gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &fd, -1);#if 0 gtk_tree_view_set_cursor(GTK_TREE_VIEW(widget), tpath, NULL, FALSE);#endif gtk_tree_path_free(tpath); } vfl->click_fd = fd; if (bevent->button == 3) { vfl->popup = vflist_pop_menu(vfl, vfl->click_fd); gtk_menu_popup(GTK_MENU(vfl->popup), NULL, NULL, NULL, NULL, bevent->button, bevent->time); return TRUE; } if (!fd) return FALSE; if (bevent->button == 2) { if (!vflist_row_is_selected(vfl, fd)) { vflist_color_set(vfl, fd, TRUE); } return TRUE; } if (bevent->button == 1 && bevent->type == GDK_BUTTON_PRESS && !(bevent->state & GDK_SHIFT_MASK ) && !(bevent->state & GDK_CONTROL_MASK ) && vflist_row_is_selected(vfl, fd)) { gtk_widget_grab_focus(widget); return TRUE; }#if 0 if (bevent->button == 1 && bevent->type == GDK_2BUTTON_PRESS) { if (vfl->layout) layout_image_full_screen_start(vfl->layout); }#endif return FALSE;}static gint vflist_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data){ ViewFileList *vfl = data; GtkTreePath *tpath; GtkTreeIter iter; FileData *fd = NULL; if (bevent->button == 2) { vflist_color_set(vfl, vfl->click_fd, FALSE); } if (bevent->button != 1 && bevent->button != 2) { return TRUE; } if ((bevent->x != 0 || bevent->y != 0) && gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget), bevent->x, bevent->y, &tpath, NULL, NULL, NULL)) { GtkTreeModel *store; store = gtk_tree_view_get_model(GTK_TREE_VIEW(widget)); gtk_tree_model_get_iter(store, &iter, tpath); gtk_tree_model_get(store, &iter, FILE_COLUMN_POINTER, &fd, -1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -