]> uap-core.de Git - mizunara.git/commitdiff
implement resize and children layout in MzFilesView
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Tue, 21 Jan 2025 12:36:02 +0000 (13:36 +0100)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Tue, 21 Jan 2025 12:36:02 +0000 (13:36 +0100)
mizunara/gtk-filesview.c
mizunara/gtk-filesview.h
mizunara/window.c

index 0ac84dc066ec682bd5cd362cc059e77fe149a081..9a80839bc53be3f17043ba21f6ab34a5848c44fc 100644 (file)
@@ -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));
 }
 
index 54b691b16c86e512c59200e3facdcdd037ad20cc..1bbd0850880c0947fa3d61643048d50f0e0a7797 100644 (file)
@@ -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);
index adb29ac415761d2f678346ececbfffa7b2a60f48..d7fd02a5b74c5213ef754db02abe8fe85404669d 100644 (file)
@@ -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") {