From 93d0aaf7fe6345967acc9bbe6b40526a88ef00a5 Mon Sep 17 00:00:00 2001 From: Olaf Wintermann Date: Wed, 29 Jan 2025 21:15:02 +0100 Subject: [PATCH] selection only works when not clicking on the activation area of items (MzFilesView, gtk4) --- mizunara/gtk-filesview.c | 45 ++++++++++++++++++++++++++++------------ mizunara/gtk-filesview.h | 5 ++++- 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/mizunara/gtk-filesview.c b/mizunara/gtk-filesview.c index fa7e820..c035167 100644 --- a/mizunara/gtk-filesview.c +++ b/mizunara/gtk-filesview.c @@ -77,7 +77,7 @@ static void mz_files_view_init(MzFilesView *self) { self->highlight = FALSE; self->highlight_col = -1; self->highlight_row = -1; - + // event handler GtkGesture *drag = gtk_gesture_drag_new(); g_signal_connect(drag, "drag-begin", G_CALLBACK(drag_begin_cb), self); @@ -108,13 +108,15 @@ MzFilesView* mz_files_view_new(void) { #define MZ_GRID_VIEW_SELECTION_WIDTH 165 #define MZ_GRID_VIEW_SELECTION_HEIGHT 165 -static int point2item(MzFilesView *view, int x, int y, int *out_col, int *out_row) { +static int point2item(MzFilesView *view, int x, int y, int *out_col, int *out_row, size_t *index) { int width = view->current_width; width -= MZ_GRID_VIEW_PADDING_LEFT + MZ_GRID_VIEW_PADDING_RIGHT; - + int item_width = 180; int item_height = 170; + int items_per_line = width / item_width; + int highlight_width = MZ_GRID_VIEW_SELECTION_WIDTH; int highlight_height = MZ_GRID_VIEW_SELECTION_HEIGHT; int highlight_rect_offset_x = (item_width - highlight_width) / 2; @@ -143,6 +145,9 @@ static int point2item(MzFilesView *view, int x, int y, int *out_col, int *out_ro if(col >= 0 && row >= 0) { *out_col = col; *out_row = row; + if(index) { + *index = row * items_per_line + col; + } return 1; } return 0; @@ -425,20 +430,28 @@ static void drag_begin_cb( printf("drag_begin_cb: %f\n", start_x); // update selection - int item_selection_width = 160; - int item_selection_height = 160; - int selection_offset_x = 5; - int selection_offset_y = 5; int item_width = 180; int item_height = 170; int col; + int row; + size_t item_index; + if(point2item(view, start_x, start_y, &col, &row, &item_index)) { + if(view->numsections > 0 && item_index < cxListSize(view->sections[0].section.files)) { + FileInfo *file = cxListAt(view->sections[0].section.files, item_index); + printf("drag file: %s\n", file->name); + view->drag_file = file; + view->drag_item = TRUE; + return; + } + } + if(start_x <= MZ_GRID_VIEW_PADDING_LEFT) { col = -1; } else { col = (start_x - MZ_GRID_VIEW_PADDING_LEFT) / item_width; } - int row = start_y / item_height; + row = start_y / item_height; view->drag = TRUE; view->drag_start_x = start_x; @@ -461,6 +474,8 @@ static void drag_end_cb( { MzFilesView *view = user_data; view->drag = FALSE; + view->drag_item = FALSE; + view->drag_file = NULL; view->update_selection = 1; gtk_widget_queue_draw(GTK_WIDGET(view)); @@ -473,14 +488,14 @@ static void drag_update_cb( gpointer user_data) { MzFilesView *view = user_data; + if(view->drag_file) { + return; + } view->drag_width = x; view->drag_height = y; + // update selection - int item_selection_width = 160; - int item_selection_height = 160; - int selection_offset_x = 5; - int selection_offset_y = 5; int item_width = 180; int item_height = 170; @@ -506,9 +521,13 @@ static void motion_cb( gpointer user_data) { MzFilesView *view = user_data; + if(view->drag_file) { + + return; + } int col, row; int update = FALSE; - if(point2item(view, x, y, &col, &row)) { + if(point2item(view, x, y, &col, &row, NULL)) { if(view->highlight_col != col || view->highlight_row != row) { update = TRUE; view->highlight = TRUE; diff --git a/mizunara/gtk-filesview.h b/mizunara/gtk-filesview.h index d5a6ee4..423f778 100644 --- a/mizunara/gtk-filesview.h +++ b/mizunara/gtk-filesview.h @@ -59,10 +59,12 @@ typedef struct MzFilesView { MzIconGadget *items; size_t numitems; + FileInfo *drag_file; + int current_width; MzViewSection *sections; size_t numsections; - + int highlight_col; int highlight_row; @@ -75,6 +77,7 @@ typedef struct MzFilesView { int drag_col; int drag_row; bool drag; + bool drag_item; bool highlight; int update_selection; // 0: no update, 1: set selection, 2: add selection -- 2.43.5