add box container (QT)

6 days ago

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 26 Mar 2025 21:47:04 +0100 (6 days ago)
changeset 519
8884c7fbe4cb
parent 518
45ed276ba7a4
child 520
ea1bba55de44

add box container (QT)

application/main.c file | annotate | diff | comparison | revisions
ui/gtk/container.h file | annotate | diff | comparison | revisions
ui/qt/container.cpp file | annotate | diff | comparison | revisions
ui/qt/container.h file | annotate | diff | comparison | revisions
ui/qt/toolkit.cpp file | annotate | diff | comparison | revisions
ui/qt/window.cpp file | annotate | diff | comparison | revisions
--- a/application/main.c	Wed Mar 26 21:15:20 2025 +0100
+++ b/application/main.c	Wed Mar 26 21:47:04 2025 +0100
@@ -774,7 +774,9 @@
 #ifdef UI_QT
 
 void application_startup(UiEvent *event, void *data) {
-
+    UiObject *obj = ui_window("My Window", NULL);
+    
+    ui_show(obj);
 }
 
 int main(int argc, char **argv) {
--- a/ui/gtk/container.h	Wed Mar 26 21:15:20 2025 +0100
+++ b/ui/gtk/container.h	Wed Mar 26 21:47:04 2025 +0100
@@ -42,7 +42,7 @@
 #ifdef	__cplusplus
 extern "C" {
 #endif
-
+  
 #define ui_reset_layout(layout) memset(&(layout), 0, sizeof(UiLayout))
 #define ui_lb2bool(b) ((b) == UI_LAYOUT_TRUE ? TRUE : FALSE)
 #define ui_bool2lb(b) ((b) ? UI_LAYOUT_TRUE : UI_LAYOUT_FALSE)
@@ -51,15 +51,14 @@
 
 typedef struct UiDocumentView UiDocumentView;
 
-typedef struct UiLayout UiLayout;
-typedef enum UiLayoutBool UiLayoutBool;
 
-enum UiLayoutBool {
+typedef enum UiLayoutBool {
     UI_LAYOUT_UNDEFINED = 0,
     UI_LAYOUT_TRUE,
     UI_LAYOUT_FALSE,
-};
+} UiLayoutBool;
 
+typedef struct UiLayout UiLayout;
 struct UiLayout {
     UiLayoutBool fill;
     UiBool       newline;
--- a/ui/qt/container.cpp	Wed Mar 26 21:15:20 2025 +0100
+++ b/ui/qt/container.cpp	Wed Mar 26 21:47:04 2025 +0100
@@ -28,8 +28,81 @@
 
 #include <stdio.h>
 #include "container.h"
+#include "../common/object.h"
+
+#include <cx/mempool.h>
 
 #include <QSpacerItem>
 #include <QStackedWidget>
 
+static void delete_container(UiContainerPrivate *ct) {
+    delete ct;
+}
 
+static void add_container(UiObject *obj, UiContainerPrivate *ct) {
+    UiContainerX *container = (UiContainerX*)ui_malloc(obj->ctx, sizeof(UiContainerX));
+    container->close = 0;
+    container->container = ct;
+    container->prev = NULL;
+    container->next = NULL;
+    cxMempoolRegister(obj->ctx->mp, ct, (cx_destructor_func)delete_container);
+    uic_object_push_container(obj, container);
+}
+
+/* -------------------- UiBoxContainer -------------------- */
+
+UiBoxContainer::UiBoxContainer(QBoxLayout* box) {
+    this->box = box;
+    box->setContentsMargins(QMargins(0,0,0,0));
+    box->setSpacing(0);
+    
+    ui_reset_layout(layout);
+}
+
+void UiBoxContainer::add(QWidget* widget, bool fill) {
+    if(layout.fill != UI_LAYOUT_UNDEFINED) {
+        fill = ui_lb2bool(layout.fill);
+    }
+    
+    if(hasStretchedWidget && fill) {
+        fill = false;
+        fprintf(stderr, "UiError: container has 2 filled widgets");
+    }
+    
+    box->addWidget(widget, fill);
+    
+    if(!hasStretchedWidget) {
+        QSpacerItem *newspace = new QSpacerItem(0, 0, QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
+        box->removeItem(space);
+        box->addSpacerItem(newspace);
+        space = newspace; 
+    }
+    
+    if(fill) {
+        hasStretchedWidget = true;
+    }
+    ui_reset_layout(layout);
+    current = widget;
+}
+
+UIWIDGET ui_box(UiObject *obj, UiContainerArgs args, QBoxLayout::Direction dir) {
+    UiContainerPrivate *ctn = (UiContainerPrivate*)ui_obj_container(obj);
+    UI_APPLY_LAYOUT(ctn->layout, args);
+    
+    QWidget *widget = new QWidget();
+    QBoxLayout *box = new QBoxLayout(dir);
+    widget->setLayout(box);
+    ctn->add(widget, true);
+    
+    
+    
+    return widget;
+}
+
+UIWIDGET ui_vbox_create(UiObject *obj, UiContainerArgs args) {
+    return ui_box(obj, args, QBoxLayout::TopToBottom);
+}
+
+UIWIDGET ui_hbox_create(UiObject *obj, UiContainerArgs args) {
+    return ui_box(obj, args, QBoxLayout::LeftToRight);
+}
--- a/ui/qt/container.h	Wed Mar 26 21:15:20 2025 +0100
+++ b/ui/qt/container.h	Wed Mar 26 21:47:04 2025 +0100
@@ -30,6 +30,7 @@
 #define	CONTAINER_H
 
 #include "toolkit.h"
+#include "../ui/container.h"
 #include "window.h"
 
 #include <string.h>
@@ -39,7 +40,71 @@
 #include <QStackedWidget>
 #include <QSplitter>
 
+#define UI_APPLY_LAYOUT(layout, args) \
+    layout.fill = args.fill; \
+    layout.hexpand = args.hexpand; \
+    layout.vexpand = args.vexpand; \
+    layout.hfill = args.hfill; \
+    layout.vfill = args.vfill; \
+    layout.override_defaults = args.override_defaults; \
+    layout.colspan = args.colspan; \
+    layout.rowspan = args.rowspan
 
+#define ui_reset_layout(layout) memset(&(layout), 0, sizeof(UiLayout))
+#define ui_lb2bool(b) ((b) == UI_LAYOUT_TRUE ? TRUE : FALSE)
+#define ui_bool2lb(b) ((b) ? UI_LAYOUT_TRUE : UI_LAYOUT_FALSE)
+
+#define ui_obj_container(obj) (UiContainerPrivate*)obj->container_end
+
+typedef enum UiLayoutBool {
+    UI_LAYOUT_UNDEFINED = 0,
+    UI_LAYOUT_TRUE,
+    UI_LAYOUT_FALSE,
+} UiLayoutBool;
+
+typedef struct UiLayout UiLayout;
+struct UiLayout {
+    UiTri        fill;
+    UiBool       newline;
+    char         *label;
+    UiBool       hexpand;
+    UiBool       vexpand;
+    UiBool       hfill;
+    UiBool       vfill;
+    UiBool       override_defaults;
+    int          width;
+    int          colspan;
+    int          rowspan;
+};
+
+struct UiContainerPrivate {
+    UiLayout layout; 
+    UIWIDGET current;
+
+    virtual void add(QWidget *widget, bool fill) = 0;
+};
+
+class UiBoxContainer : public UiContainerPrivate {
+public:
+    QBoxLayout  *box;
+    bool        hasStretchedWidget = false;
+    QSpacerItem *space;
+    
+    UiBoxContainer(QBoxLayout *box);
+    
+    virtual void add(QWidget *widget, bool fill);
+};
+
+class UiGridContainer : public UiContainerPrivate {
+public:
+    QGridLayout *grid;
+    int x = 0;
+    int y = 0;
+    
+    UiGridContainer(QGridLayout *grid, int margin, int columnspacing, int rowspacing);
+    
+    virtual void add(QWidget *widget, bool fill);
+};
 
 
 #endif	/* CONTAINER_H */
--- a/ui/qt/toolkit.cpp	Wed Mar 26 21:15:20 2025 +0100
+++ b/ui/qt/toolkit.cpp	Wed Mar 26 21:47:04 2025 +0100
@@ -45,9 +45,6 @@
 static ui_callback   exit_func;
 void                 *exit_data;
 
-static ui_callback appclose_fnc;
-static void *appclose_udata;
-
 static int is_toplevel_realized = 0;
 
 int app_argc;
@@ -87,10 +84,12 @@
 }
 
 void ui_main() {
+    if(startup_func) {
+        startup_func(NULL, startup_data);
+    }
     application->exec();
-    
-    if(appclose_fnc) {
-        appclose_fnc(NULL, appclose_udata);
+    if(exit_func) {
+        exit_func(NULL, exit_data);
     }
     uic_store_app_properties();
     
@@ -98,7 +97,7 @@
 }
 
 void ui_show(UiObject *obj) {
-    //obj->widget->show();
+    obj->widget->show();
 }
 
 void ui_close(UiObject *obj) {
--- a/ui/qt/window.cpp	Wed Mar 26 21:15:20 2025 +0100
+++ b/ui/qt/window.cpp	Wed Mar 26 21:47:04 2025 +0100
@@ -36,8 +36,9 @@
 
 #include <QVBoxLayout>
 #include <QFileDialog>
+#include <QPushButton>
 
-static UiObject* create_window(char *title, void *window_data, bool simple) {
+static UiObject* create_window(const char *title, void *window_data, bool simple) {
     CxMempool *mp = cxMempoolCreateSimple(256);
     UiObject *obj = (UiObject*)cxCalloc(mp->allocator, 1, sizeof(UiObject));
     obj->ctx = uic_context(obj, mp);
@@ -45,6 +46,7 @@
     obj->next = NULL;
     
     QMainWindow *window = new QMainWindow();
+    window->setWindowTitle(title);
     obj->widget = window;
     
     /*
@@ -64,7 +66,7 @@
     return obj;
 }
 
-UiObject* ui_window(char *title, void *window_data) {
+UiObject* ui_window(const char *title, void *window_data) {
     return create_window(title, window_data, FALSE);
 }
 

mercurial