From 6c2c040c8554d42e1ff0e47eea01c12db47db210 Mon Sep 17 00:00:00 2001 From: Olaf Wintermann Date: Tue, 21 Jan 2025 13:36:02 +0100 Subject: [PATCH] implement resize and children layout in MzFilesView --- mizunara/gtk-filesview.c | 59 ++++++++++++++++++++++++++++++++++++++++ mizunara/gtk-filesview.h | 15 ++++++++++ mizunara/window.c | 9 ++++-- 3 files changed, 81 insertions(+), 2 deletions(-) diff --git a/mizunara/gtk-filesview.c b/mizunara/gtk-filesview.c index 0ac84dc..9a80839 100644 --- a/mizunara/gtk-filesview.c +++ b/mizunara/gtk-filesview.c @@ -55,6 +55,8 @@ static void drag_update_cb( static void mz_files_view_class_init(MzFilesViewClass *klass) { printf("mz_files_view_class_init\n"); klass->parent_class.snapshot = mz_files_view_snapshot; + klass->parent_class.measure = mz_files_view_measure; + klass->parent_class.size_allocate = mz_files_view_size_allocate; } static void mz_files_view_init(MzFilesView *self) { @@ -64,6 +66,8 @@ static void mz_files_view_init(MzFilesView *self) { self->items = NULL; self->numitems = 0; + self->current_width = 0; + // event handler GtkGesture *drag = gtk_gesture_drag_new(); g_signal_connect(drag, "drag-begin", G_CALLBACK(drag_begin_cb), self); @@ -83,6 +87,9 @@ void mz_files_view_snapshot(GtkWidget *widget, GtkSnapshot *snapshot) { int width = gtk_widget_get_width(widget); int height = gtk_widget_get_height(widget); + //printf("wh: %d x %d\n", width, height); + + view->current_width = width; int item_width = 80; int item_height = 120; @@ -122,6 +129,57 @@ void mz_files_view_snapshot(GtkWidget *widget, GtkSnapshot *snapshot) { cls->snapshot(widget, snapshot); } +void mz_files_view_measure(GtkWidget *widget, + GtkOrientation orientation, + int for_size, + int *minimum, + int *natural, + int *minimum_baseline, + int *natural_baseline) +{ + //printf("mz_files_view_measure\n"); + //printf("for size: %d - %d %d %d %d\n", for_size, *minimum, *natural, *minimum_baseline, *natural_baseline); + + MzFilesView *view = (MzFilesView*)widget; + + if(view->numitems == 0 || view->current_width == 0) { + return; + } + + if(orientation == GTK_ORIENTATION_VERTICAL) { + int item_width = 80; + int item_height = 120; + int items_per_line = view->current_width / item_width; + + int lines = view->numitems / items_per_line; + int r = lines*items_per_line < view->numitems ? 1 : 0; + //printf("nitems: %d ipl lines: %d: %d\n", (int)view->numitems, items_per_line, lines+r); + + int height = (lines+r) * item_height; + //printf("minimum height: %d\n", height); + + *minimum = height; + *natural = height; + } +} + +static void mz_files_view_size_allocate( + GtkWidget *widget, + int width, + int height, + int baseline) +{ + GtkWidgetClass *parent_class = mz_files_view_parent_class; + if (parent_class->size_allocate) + parent_class->size_allocate(widget, width, height, baseline); + + MzFilesView *view = (MzFilesView *)widget; + if (view->current_width != width) { + view->current_width = width; + gtk_widget_queue_resize(widget); + } +} + /* -------------------------- event handler -------------------------- */ @@ -195,6 +253,7 @@ void mz_update_files_view(MzFilesView *view, FilesSection *sections, size_t nums } } + gtk_widget_queue_resize(GTK_WIDGET(view)); gtk_widget_queue_draw(GTK_WIDGET(view)); } diff --git a/mizunara/gtk-filesview.h b/mizunara/gtk-filesview.h index 54b691b..1bbd085 100644 --- a/mizunara/gtk-filesview.h +++ b/mizunara/gtk-filesview.h @@ -57,6 +57,7 @@ typedef struct MzFilesView { MzIconGadget *items; size_t numitems; + int current_width; MzViewSection *sections; size_t numsections; } MzFilesView; @@ -69,6 +70,20 @@ MzFilesView* mz_files_view_new(void); void mz_files_view_snapshot(GtkWidget *widget, GtkSnapshot *snapshot); +void mz_files_view_measure(GtkWidget *widget, + GtkOrientation orientation, + int for_size, + int *minimum, + int *natural, + int *minimum_baseline, + int *natural_baseline); + +static void mz_files_view_size_allocate( + GtkWidget *widget, + int width, + int height, + int baseline); + void mz_update_files_view(MzFilesView *view, FilesSection *sections, size_t numsections); void mz_files_view_remove_items(MzFilesView *view); diff --git a/mizunara/window.c b/mizunara/window.c index adb29ac..d7fd02a 100644 --- a/mizunara/window.c +++ b/mizunara/window.c @@ -95,15 +95,20 @@ UiObject* window_create(const char *url) { #ifdef GTK_MAJOR_VERSION static UIWIDGET create_filesview(UiObject *obj, UiWidgetArgs args, void *userdata) { + MainWindow *win = userdata; MzFilesView *view = mz_files_view_new(); - return GTK_WIDGET(view); + win->files_gridview = GTK_WIDGET(view); + + GtkWidget *sw = gtk_scrolled_window_new(); + gtk_scrolled_window_set_child(GTK_SCROLLED_WINDOW(sw), GTK_WIDGET(view)); + return sw; } #endif void window_create_browser_view(UiObject *obj, MainWindow *win) { ui_tabview(obj, .tabview = UI_TABVIEW_INVISIBLE, .varname = "view") { ui_tab(obj, "iconview") { - win->files_gridview = ui_customwidget(obj, create_filesview, NULL, .fill = UI_ON); + ui_customwidget(obj, create_filesview, win, .fill = UI_ON); } ui_tab(obj, "listview") { -- 2.43.5