added simple window and open/save file dialogs for Qt

Sat, 23 Jan 2016 20:47:07 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 23 Jan 2016 20:47:07 +0100
changeset 106
a4f4123ca12a
parent 105
86d729874ff4
child 107
9aff1dc3990d

added simple window and open/save file dialogs for Qt

application/main.c file | annotate | diff | comparison | revisions
ui/gtk/container.h file | annotate | diff | comparison | revisions
ui/gtk/window.c file | annotate | diff | comparison | revisions
ui/motif/container.c file | annotate | diff | comparison | revisions
ui/motif/container.h file | annotate | diff | comparison | revisions
ui/motif/window.c file | annotate | diff | comparison | revisions
ui/qt/window.cpp file | annotate | diff | comparison | revisions
ui/ui/window.h file | annotate | diff | comparison | revisions
--- a/application/main.c	Sat Jan 23 18:37:30 2016 +0100
+++ b/application/main.c	Sat Jan 23 20:47:07 2016 +0100
@@ -37,6 +37,11 @@
     printf("action_menu test: {%s}\n", data);
     printf("text: {%s}\n", ui_gettext(event->obj, "text"));
     fflush(stdout);
+    
+    char *file = ui_openfiledialog(event->obj);
+    printf("file: %s\n", file);
+    fflush(stdout);
+    free(file);
 }
 
 void action_button(UiEvent *event, void *data) {
--- a/ui/gtk/container.h	Sat Jan 23 18:37:30 2016 +0100
+++ b/ui/gtk/container.h	Sat Jan 23 20:47:07 2016 +0100
@@ -57,8 +57,8 @@
     UiLayoutBool fill;
     UiBool       newline;
     char         *label;
-    UiLayoutBool hexpand;
-    UiLayoutBool vexpand;
+    UiBool       hexpand;
+    UiBool       vexpand;
 };
 
 struct UiContainer {
--- a/ui/gtk/window.c	Sat Jan 23 18:37:30 2016 +0100
+++ b/ui/gtk/window.c	Sat Jan 23 20:47:07 2016 +0100
@@ -57,7 +57,7 @@
     }
 }
 
-UiObject* ui_window(char *title, void *window_data) {
+static UiObject* create_window(char *title, void *window_data, UiBool simple) {
     UcxMempool *mp = ucx_mempool_new(256);
     UiObject *obj = ucx_mempool_calloc(mp, 1, sizeof(UiObject));  
     obj->widget = gtk_window_new(GTK_WINDOW_TOPLEVEL);
@@ -90,16 +90,18 @@
     GtkWidget *vbox = ui_gtk_vbox_new();
     gtk_container_add(GTK_CONTAINER(obj->widget), vbox);
     
-    // menu
-    GtkWidget *mb = ui_create_menubar(obj);
-    if(mb) {
-        gtk_box_pack_start(GTK_BOX(vbox), mb, FALSE, FALSE, 0);
-    }
-    
-    // toolbar
-    GtkWidget *tb = ui_create_toolbar(obj);
-    if(tb) {
-        gtk_box_pack_start(GTK_BOX(vbox), tb, FALSE, FALSE, 0);
+    if(!simple) {
+        // menu
+        GtkWidget *mb = ui_create_menubar(obj);
+        if(mb) {
+            gtk_box_pack_start(GTK_BOX(vbox), mb, FALSE, FALSE, 0);
+        }
+
+        // toolbar
+        GtkWidget *tb = ui_create_toolbar(obj);
+        if(tb) {
+            gtk_box_pack_start(GTK_BOX(vbox), tb, FALSE, FALSE, 0);
+        }
     }
     
     // window content
@@ -116,6 +118,15 @@
     return obj;
 }
 
+
+UiObject* ui_window(char *title, void *window_data) {
+    return create_window(title, window_data, FALSE);
+}
+
+UiObject* ui_simplewindow(char *title, void *window_data) {
+    return create_window(title, window_data, TRUE);
+}
+
 static char* ui_gtkfilechooser(UiObject *obj, GtkFileChooserAction action) {
     char *button;
     char *title;
--- a/ui/motif/container.c	Sat Jan 23 18:37:30 2016 +0100
+++ b/ui/motif/container.c	Sat Jan 23 20:47:07 2016 +0100
@@ -370,7 +370,7 @@
 }
 
 UIWIDGET ui_grid(UiObject *obj) {
-    return ui_grid_sp(obj, 0, 0);
+    return ui_grid_sp(obj, 0, 0, 0);
 }
 
 UIWIDGET ui_grid_sp(UiObject *obj, int margin, int columnspacing, int rowspacing) {
@@ -725,6 +725,16 @@
     ct->layout.fill = ui_bool2lb(fill);
 }
 
+void ui_layout_hexpand(UiObject *obj, UiBool expand) {
+    UiContainer *ct = uic_get_current_container(obj);
+    ct->layout.hexpand = expand;
+}
+
+void ui_layout_vexpand(UiObject *obj, UiBool expand) {
+    UiContainer *ct = uic_get_current_container(obj);
+    ct->layout.vexpand = expand;
+}
+
 void ui_newline(UiObject *obj) {
     UiContainer *ct = uic_get_current_container(obj);
     ct->layout.newline = TRUE;
--- a/ui/motif/container.h	Sat Jan 23 18:37:30 2016 +0100
+++ b/ui/motif/container.h	Sat Jan 23 20:47:07 2016 +0100
@@ -30,7 +30,7 @@
 #define	CONTAINER_H
 
 #include "../ui/toolkit.h"
-#include <ucx/list.h>
+#include "../../ucx/list.h"
 #include <string.h>
 
 #ifdef	__cplusplus
--- a/ui/motif/window.c	Sat Jan 23 18:37:30 2016 +0100
+++ b/ui/motif/window.c	Sat Jan 23 20:47:07 2016 +0100
@@ -48,7 +48,7 @@
     }
 }
 
-UiObject* ui_window(char *title, void *window_data) {
+static UiObject* create_window(char *title, void *window_data, UiBool simple) {
     UcxMempool *mp = ucx_mempool_new(256);
     UiObject *obj = ucx_mempool_calloc(mp, 1, sizeof(UiObject));
     obj->ctx = uic_context(obj, mp);
@@ -87,24 +87,23 @@
     // TODO: use callback
     ui_set_active_window(toplevel);
     
-    // menu
     Widget window = XtVaCreateManagedWidget(
             title,
             xmMainWindowWidgetClass,
             toplevel,
             NULL);
     obj->widget = window;
-    ui_create_menubar(obj);
-    
-    // toolbar
     Widget form = XtVaCreateManagedWidget(
             "window_form",
             xmFormWidgetClass,
             window,
             NULL);
+    Widget toolbar = NULL;
     
-    Widget toolbar = ui_create_toolbar(obj, form);
-    
+    if(!simple) {
+        ui_create_menubar(obj);
+        toolbar = ui_create_toolbar(obj, form);
+    }
     
     // window content
     XtSetArg(args[0], XmNshadowType, XmSHADOW_ETCHED_OUT);
@@ -134,6 +133,14 @@
     return obj;
 }
 
+UiObject* ui_window(char *title, void *window_data) {
+    return create_window(title, window_data, FALSE);
+}
+
+UiObject* ui_simplewindow(char *title, void *window_data) {
+    return create_window(title, window_data, TRUE);
+}
+
 void ui_close(UiObject *obj) {
     XtDestroyWidget(obj->widget);
     window_close_handler(obj->widget, obj, NULL);
--- a/ui/qt/window.cpp	Sat Jan 23 18:37:30 2016 +0100
+++ b/ui/qt/window.cpp	Sat Jan 23 20:47:07 2016 +0100
@@ -35,8 +35,9 @@
 #include "container.h"
 
 #include <QVBoxLayout>
+#include <QFileDialog>
 
-UiObject* ui_window(char *title, void *window_data) {
+static UiObject* create_window(char *title, void *window_data, bool simple) {
     UcxMempool *mp = ucx_mempool_new(256);
     UiObject *obj = (UiObject*)ucx_mempool_calloc(mp, 1, sizeof(UiObject));
     obj->ctx = uic_context(obj, mp);
@@ -46,10 +47,11 @@
     QMainWindow *window = new QMainWindow();
     obj->widget = window;
     
-    ui_add_menus(obj, window);
-    
-    QToolBar *toolbar = ui_create_toolbar(obj);
-    window->addToolBar(Qt::TopToolBarArea, toolbar);
+    if(!simple) {
+        ui_add_menus(obj, window);
+        QToolBar *toolbar = ui_create_toolbar(obj);
+        window->addToolBar(Qt::TopToolBarArea, toolbar);
+    }
     
     QBoxLayout *box = new QVBoxLayout();
     QWidget *boxWidget = new QWidget();
@@ -60,3 +62,34 @@
     obj->widget = window;
     return obj;
 }
+
+UiObject* ui_window(char *title, void *window_data) {
+    return create_window(title, window_data, FALSE);
+}
+
+UiObject* ui_simplewindow(char *title, void *window_data) {
+    return create_window(title, window_data, TRUE);
+}
+
+
+char* ui_openfiledialog(UiObject *obj) {
+    QString fileName = QFileDialog::getOpenFileName(obj->widget);
+    if(fileName.size() > 0) {
+        QByteArray array = fileName.toLocal8Bit();
+        const char *cstr = array.constData();
+        return strdup(cstr);
+    } else {
+        return NULL;
+    }
+}
+
+char* ui_savefiledialog(UiObject *obj) {
+    QString fileName = QFileDialog::getSaveFileName(obj->widget);
+    if(fileName.size() > 0) {
+        QByteArray array = fileName.toLocal8Bit();
+        const char *cstr = array.constData();
+        return strdup(cstr);
+    } else {
+        return NULL;
+    }
+}
--- a/ui/ui/window.h	Sat Jan 23 18:37:30 2016 +0100
+++ b/ui/ui/window.h	Sat Jan 23 20:47:07 2016 +0100
@@ -36,6 +36,8 @@
 #endif
 
 UiObject* ui_window(char *title, void *window_data);
+UiObject* ui_simplewindow(char *title, void *window_data);
+
 char* ui_openfiledialog(UiObject *obj);
 char* ui_savefiledialog(UiObject *obj);
 

mercurial