add sidebar (libadwaita) newapi

Thu, 05 Dec 2024 16:44:05 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 05 Dec 2024 16:44:05 +0100
branch
newapi
changeset 409
86fbc80f0be7
parent 408
b06e43f1edd4
child 410
a080c69125d1

add sidebar (libadwaita)

application/main.c file | annotate | diff | comparison | revisions
ui/gtk/container.c file | annotate | diff | comparison | revisions
ui/gtk/window.c file | annotate | diff | comparison | revisions
ui/ui/container.h file | annotate | diff | comparison | revisions
ui/ui/window.h file | annotate | diff | comparison | revisions
--- a/application/main.c	Thu Dec 05 08:40:37 2024 +0100
+++ b/application/main.c	Thu Dec 05 16:44:05 2024 +0100
@@ -268,7 +268,15 @@
     
     
     
-    UiObject *obj = ui_window("Test", NULL);
+    UiObject *obj = ui_sidebar_window("Test", NULL);
+    
+    ui_sidebar(obj, .margin = 8, .spacing = 8) {
+        ui_button(obj, .label = "Button 1", .style_class = "flat");
+        ui_button(obj, .label = "Button 2", .style_class = "flat");
+        ui_button(obj, .label = "Button 3", .style_class = "flat");
+        ui_button(obj, .label = "Button 4", .style_class = "flat");
+        ui_button(obj, .label = "Button 5", .style_class = "flat");
+    }
     
     MyDocument *doc = create_doc();
     ui_attach_document(obj->ctx, doc);
--- a/ui/gtk/container.c	Thu Dec 05 08:40:37 2024 +0100
+++ b/ui/gtk/container.c	Thu Dec 05 16:44:05 2024 +0100
@@ -891,6 +891,28 @@
 
 #endif
 
+/* -------------------- Sidebar -------------------- */
+
+#ifdef UI_LIBADWAITA
+UIWIDGET ui_sidebar_create(UiObject *obj, UiSidebarArgs args) {
+    GtkWidget *sidebar_toolbar_view = g_object_get_data(G_OBJECT(obj->widget), "ui_sidebar");
+    if(!sidebar_toolbar_view) {
+        fprintf(stderr, "Error: window is not configured for sidebar\n");
+        return NULL;
+    }
+    
+    GtkWidget *box = ui_gtk_vbox_new(args.spacing);
+    ui_box_set_margin(box, args.margin);
+    adw_toolbar_view_set_content(ADW_TOOLBAR_VIEW(sidebar_toolbar_view), box);
+    
+    UiObject *newobj = uic_object_new(obj, box);
+    newobj->container = ui_box_container(obj, box, UI_CONTAINER_VBOX);
+    uic_obj_add(obj, newobj);
+    
+    return box;
+}
+#endif
+
 /* -------------------- Splitpane -------------------- */
 
 static GtkWidget* create_paned(UiOrientation orientation) {
--- a/ui/gtk/window.c	Thu Dec 05 08:40:37 2024 +0100
+++ b/ui/gtk/window.c	Thu Dec 05 16:44:05 2024 +0100
@@ -101,7 +101,7 @@
 }
 #endif
 
-static UiObject* create_window(const char *title, void *window_data, UiBool simple) {
+static UiObject* create_window(const char *title, void *window_data, UiBool sidebar, UiBool simple) {
     CxMempool *mp = cxBasicMempoolCreate(256);
     UiObject *obj = cxCalloc(mp->allocator, 1, sizeof(UiObject));
     obj->ref = 0;
@@ -163,8 +163,24 @@
     GtkWidget *vbox = ui_gtk_vbox_new(0);
 #ifdef UI_LIBADWAITA
     GtkWidget *toolbar_view = adw_toolbar_view_new();
-    adw_application_window_set_content(ADW_APPLICATION_WINDOW(obj->widget), toolbar_view);
     adw_toolbar_view_set_content(ADW_TOOLBAR_VIEW(toolbar_view), vbox);
+    
+    if(sidebar) {
+        GtkWidget *splitview = adw_overlay_split_view_new();
+        adw_application_window_set_content(ADW_APPLICATION_WINDOW(obj->widget), splitview);
+        
+        GtkWidget *sidebar_toolbar_view = adw_toolbar_view_new();
+        adw_overlay_split_view_set_sidebar(ADW_OVERLAY_SPLIT_VIEW(splitview), sidebar_toolbar_view);
+        GtkWidget *sidebar_headerbar = adw_header_bar_new();
+        adw_toolbar_view_add_top_bar(ADW_TOOLBAR_VIEW(sidebar_toolbar_view), sidebar_headerbar);
+        
+        adw_overlay_split_view_set_content(ADW_OVERLAY_SPLIT_VIEW(splitview), toolbar_view);
+        
+        g_object_set_data(G_OBJECT(obj->widget), "ui_sidebar", sidebar_toolbar_view);
+    } else {
+        adw_application_window_set_content(ADW_APPLICATION_WINDOW(obj->widget), toolbar_view);
+    }
+    
 
     GtkWidget *headerbar = adw_header_bar_new();
     adw_toolbar_view_add_top_bar(ADW_TOOLBAR_VIEW(toolbar_view), headerbar);
@@ -223,11 +239,15 @@
 
 
 UiObject* ui_window(const char *title, void *window_data) {
-    return create_window(title, window_data, FALSE);
+    return create_window(title, window_data, FALSE, FALSE);
+}
+
+UiObject *ui_sidebar_window(const char *title, void *window_data) {
+    return create_window(title, window_data, TRUE, FALSE);
 }
 
 UiObject* ui_simple_window(const char *title, void *window_data) {
-    return create_window(title, window_data, TRUE);
+    return create_window(title, window_data, FALSE, TRUE);
 }
 
 void ui_window_size(UiObject *obj, int width, int height) {
--- a/ui/ui/container.h	Thu Dec 05 08:40:37 2024 +0100
+++ b/ui/ui/container.h	Thu Dec 05 16:44:05 2024 +0100
@@ -141,6 +141,13 @@
     int alt_spacing;
 } UiHeaderbarArgs;
 
+typedef struct UiSidebarArgs {
+    const char *name;
+    const char *style_class;
+    int margin;
+    int spacing;
+} UiSidebarArgs;
+
 
 struct UiContainerX {
     void *container;
@@ -160,6 +167,7 @@
 #define ui_scrolledwindow(obj, ...) for(ui_scrolledwindow_create(obj, (UiFrameArgs){ __VA_ARGS__ });ui_container_finish(obj);ui_container_begin_close(obj))
 #define ui_tabview(obj, ...) for(ui_tabview_create(obj, (UiTabViewArgs){ __VA_ARGS__ });ui_container_finish(obj);ui_container_begin_close(obj))
 #define ui_headerbar(obj, ...) for(ui_headerbar_create(obj, (UiHeaderbarArgs){ __VA_ARGS__ });ui_container_finish(obj);ui_container_begin_close(obj))
+#define ui_sidebar(obj, ...) for(ui_sidebar_create(obj, (UiSidebarArgs){ __VA_ARGS__ });ui_container_finish(obj);ui_container_begin_close(obj))
 
 #define ui_vbox0(obj) for(ui_vbox_create(obj, (UiContainerArgs){ 0 });ui_container_finish(obj);ui_container_begin_close(obj))
 #define ui_hbox0(obj) for(ui_hbox_create(obj, (UiContainerArgs){ 0 });ui_container_finish(obj);ui_container_begin_close(obj))
@@ -169,6 +177,7 @@
 #define ui_scrolledwindow0(obj) for(ui_scrolledwindow_create(obj, (UiFrameArgs){ 0 });ui_container_finish(obj);ui_container_begin_close(obj))
 #define ui_tabview0(obj) for(ui_tabview_create(obj, (UiTabViewArgs){ 0 });ui_container_finish(obj);ui_container_begin_close(obj))
 #define ui_headerbar0(obj) for(ui_headerbar_create(obj, (UiHeaderbarArgs){ 0 });ui_container_finish(obj);ui_container_begin_close(obj))
+#define ui_sidebar0(obj) for(ui_sidebar_create(obj, (UiSidebarArgs){ 0 });ui_container_finish(obj);ui_container_begin_close(obj))
 
 #define ui_tab(obj, label) for(ui_tab_create(obj, label);ui_container_finish(obj);ui_container_begin_close(obj))
 
@@ -197,10 +206,8 @@
 UIEXPORT void ui_headerbar_center_create(UiObject *obj);
 UIEXPORT void ui_headerbar_end_create(UiObject *obj);
 
+UIEXPORT UIWIDGET ui_sidebar_create(UiObject *obj, UiSidebarArgs args);
 
-UIEXPORT UIWIDGET ui_scrolledwindow_deprecated(UiObject *obj); // TODO
-
-UIEXPORT UIWIDGET ui_sidebar(UiObject *obj); // TODO
 
 UIEXPORT UIWIDGET ui_hsplitpane(UiObject *obj, int max); // TODO
 UIEXPORT UIWIDGET ui_vsplitpane(UiObject *obj, int max); // TODO
--- a/ui/ui/window.h	Thu Dec 05 08:40:37 2024 +0100
+++ b/ui/ui/window.h	Thu Dec 05 16:44:05 2024 +0100
@@ -72,9 +72,10 @@
     void *onclickdata;
 } UiDialogWindowArgs;
 
-UIEXPORT UiObject* ui_window(const char *title, void *window_data);
-UIEXPORT UiObject* ui_simple_window(const char *title, void *window_data);
-UIEXPORT UiObject* ui_dialog_window_create(UiObject *parent, UiDialogWindowArgs args);
+UIEXPORT UiObject *ui_window(const char *title, void *window_data);
+UIEXPORT UiObject *ui_sidebar_window(const char *title, void *window_data);
+UIEXPORT UiObject *ui_simple_window(const char *title, void *window_data);
+UIEXPORT UiObject *ui_dialog_window_create(UiObject *parent, UiDialogWindowArgs args);
 
 #define ui_dialog_window(parent, ...) ui_dialog_window_create(parent, (UiDialogWindowArgs){ __VA_ARGS__ });
 #define ui_dialog_window0(parent) ui_dialog_window_create(parent, (UiDialogWindowArgs){ 0 });

mercurial