ui/gtk/container.c

changeset 59
eb6611be50c7
parent 52
25e5390cce41
child 62
70d2aee84432
--- a/ui/gtk/container.c	Sat Aug 30 22:48:46 2014 +0200
+++ b/ui/gtk/container.c	Thu Jan 01 11:23:43 2015 +0100
@@ -33,21 +33,80 @@
 #include "../common/context.h"
 #include "../common/object.h"
 
+static UiBool ui_lb2bool(UiLayoutBool b) {
+    return b == UI_LAYOUT_TRUE ? TRUE : FALSE;
+}
 
+static UiLayoutBool ui_bool2lb(UiBool b) {
+    return b ? UI_LAYOUT_TRUE : UI_LAYOUT_FALSE;
+}
+
+GtkWidget* ui_gtk_vbox_new() {
+#ifdef UI_GTK3
+    return gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
+#else
+    return gtk_vbox_new(FALSE, 0);
+#endif
+}
+
+GtkWidget* ui_gtk_hbox_new() {
+#ifdef UI_GTK3
+    return gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
+#else
+    return gtk_hbox_new(FALSE, 0);
+#endif
+}
+
+/* -------------------- Frame Container (deprecated) -------------------- */
 UiContainer* ui_frame_container(UiObject *obj, GtkWidget *frame) {
-    UiContainer *ct = ucx_mempool_malloc(
+    UiContainer *ct = ucx_mempool_calloc(
             obj->ctx->mempool,
+            1,
             sizeof(UiContainer));
     ct->widget = frame;
     ct->add = ui_frame_container_add;
     return ct;
 }
 
-void ui_frame_container_add(UiContainer *ct, GtkWidget *widget) {
+void ui_frame_container_add(UiContainer *ct, GtkWidget *widget, UiBool fill) {
     gtk_container_add(GTK_CONTAINER(ct->widget), widget);
+    ui_reset_layout(ct->layout);
 }
 
 
+/* -------------------- Box Container -------------------- */
+UiContainer* ui_box_container(UiObject *obj, GtkWidget *box) {
+    UiBoxContainer *ct = ucx_mempool_calloc(
+            obj->ctx->mempool,
+            1,
+            sizeof(UiBoxContainer));
+    ct->container.widget = box;
+    ct->container.add = ui_box_container_add;
+    return (UiContainer*)ct;
+}
+
+void ui_box_container_add(UiContainer *ct, GtkWidget *widget, UiBool fill) {
+    UiBoxContainer *bc = (UiBoxContainer*)ct;
+    if(ct->layout.fill != UI_LAYOUT_UNDEFINED) {
+        fill = ui_lb2bool(ct->layout.fill);
+    }
+    
+    if(bc->has_fill && fill) {
+        fprintf(stderr, "UiError: container has 2 filled widgets");
+        fill = FALSE;
+    }
+    if(fill) {
+        bc->has_fill = TRUE;
+    }
+    
+    UiBool expand = fill;
+    gtk_box_pack_start(GTK_BOX(ct->widget), widget, expand, fill, 0);
+    
+    ui_reset_layout(ct->layout);
+}
+
+
+/* -------------------- Sidebar -------------------- */
 UIWIDGET ui_sidebar(UiObject *obj) {
 #ifdef UI_GTK3
     GtkWidget *paned = gtk_paned_new(GTK_ORIENTATION_HORIZONTAL);
@@ -56,20 +115,11 @@
 #endif
     gtk_paned_set_position(GTK_PANED(paned), 200);
     
-    GtkWidget *sidebar;
-#ifdef UI_GTK3
-    sidebar = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
-#else
-    sidebar = gtk_vbox_new(FALSE, 0);
-#endif
+    GtkWidget *sidebar = ui_gtk_vbox_new();
     gtk_paned_pack1(GTK_PANED(paned), sidebar, TRUE, FALSE);
     
     UiObject *left = uic_object_new(obj, sidebar);
-    UiContainer *ct1 = ucx_mempool_malloc(
-            obj->ctx->mempool,
-            sizeof(UiContainer));
-    ct1->widget = sidebar;
-    ct1->add = ui_box_add;
+    UiContainer *ct1 = ui_box_container(obj, sidebar);
     left->container = ct1;
     
     UiObject *right = uic_object_new(obj, sidebar);
@@ -81,7 +131,7 @@
     right->container = ct2;
     
     UiContainer *ct = uic_get_current_container(obj);
-    ct->add(ct, paned);
+    ct->add(ct, paned, TRUE);
     
     uic_obj_add(obj, right);
     uic_obj_add(obj, left);
@@ -89,22 +139,17 @@
     return sidebar;
 }
 
-void ui_split_container_add1(UiContainer *ct, GtkWidget *widget) {
+void ui_split_container_add1(UiContainer *ct, GtkWidget *widget, UiBool fill) {
+    // TODO: remove
     gtk_paned_pack1(GTK_PANED(ct->widget), widget, TRUE, FALSE);
 }
 
-void ui_split_container_add2(UiContainer *ct, GtkWidget *widget) {
+void ui_split_container_add2(UiContainer *ct, GtkWidget *widget, UiBool fill) {
     gtk_paned_pack2(GTK_PANED(ct->widget), widget, TRUE, FALSE);
 }
 
 
-void ui_box_add(UiContainer *ct, GtkWidget *widget) {
-    gtk_box_pack_start(GTK_BOX(ct->widget), widget, TRUE, TRUE, 0);
-}
-
-
-
-
+/* -------------------- Document Tabview -------------------- */
 static void page_change(GtkNotebook *notebook, GtkWidget *page, guint page_num, gpointer data) {
     GQuark q = g_quark_from_static_string("ui.tab.object");
     UiObject *tab = g_object_get_qdata(G_OBJECT(page), q);
@@ -128,7 +173,7 @@
                 NULL);
     
     UiContainer *ct = uic_get_current_container(obj);
-    ct->add(ct, tabview);
+    ct->add(ct, tabview, TRUE);
     
     UiTabbedPane *tabbedpane = ui_malloc(obj->ctx, sizeof(UiTabbedPane));
     tabbedpane->ctx = uic_current_obj(obj)->ctx;
@@ -172,3 +217,16 @@
 void ui_tab_detach_document(UiContext *ctx, void *document) {
     uic_context_detach_document(ctx->parent, document);
 }
+
+
+/*
+ * -------------------- Layout Functions --------------------
+ * 
+ * functions for setting layout attributes for the current container
+ *
+ */
+
+void ui_layout_fill(UiObject *obj, UiBool fill) {
+    UiContainer *ct = uic_get_current_container(obj);
+    ct->layout.fill = ui_bool2lb(fill);
+}

mercurial