implemented grid container (WPF)

Wed, 20 Jan 2016 11:35:01 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 20 Jan 2016 11:35:01 +0100
changeset 101
1c943d43fa81
parent 100
d276306d801f
child 102
2988f00ed9d6

implemented grid container (WPF)

application/main.c file | annotate | diff | comparison | revisions
ui/wpf/UIcore/Container.cs file | annotate | diff | comparison | revisions
ui/wpf/UIcore/Controls.cs file | annotate | diff | comparison | revisions
ui/wpf/UIcore/TextArea.cs file | annotate | diff | comparison | revisions
ui/wpf/UIwrapper/UIwrapper/UIwrapper.vcxproj file | annotate | diff | comparison | revisions
ui/wpf/UIwrapper/UIwrapper/container.cpp file | annotate | diff | comparison | revisions
ui/wpf/UIwrapper/UIwrapper/controls.cpp file | annotate | diff | comparison | revisions
ui/wpf/container.c file | annotate | diff | comparison | revisions
ui/wpf/container.h file | annotate | diff | comparison | revisions
ui/wpf/label.c file | annotate | diff | comparison | revisions
ui/wpf/label.h file | annotate | diff | comparison | revisions
ui/wpf/objs.mk file | annotate | diff | comparison | revisions
ui/wpf/text.c file | annotate | diff | comparison | revisions
ui/wpf/text.h file | annotate | diff | comparison | revisions
--- a/application/main.c	Sun Jan 17 19:19:28 2016 +0100
+++ b/application/main.c	Wed Jan 20 11:35:01 2016 +0100
@@ -43,7 +43,7 @@
     printf("button clicked\n");
     fflush(stdout);
 }
-
+/*
 void draw(UiEvent *event, UiGraphics *g, void *data) {
     int width = g->width;
     int height = g->height;
@@ -63,6 +63,7 @@
     
     ui_text_free(text);
 }
+*/
 
 void click(UiEvent *event, void *data) {
     UiMouseEvent *me = event->eventdata;
@@ -92,22 +93,24 @@
     //ui_mouse_handler(obj, w, click, NULL);
     
     ui_grid_sp(obj, 40, 4, 4);
+    
     ui_button(obj, "OK", NULL, NULL);
     ui_button(obj, "Google", NULL, NULL);
     ui_textfield(obj, NULL);
     ui_newline(obj);
     
     ui_button(obj, "OK", NULL, NULL);
-    ui_space(obj);
-    ui_textfield(obj, NULL);
     ui_newline(obj);
     
     ui_vbox(obj);
     ui_button(obj, "txt", NULL, NULL);
+    ui_textfield(obj, NULL);
+    ui_space(obj);
     ui_end(obj);
     ui_layout_hexpand(obj, TRUE);
     ui_layout_vexpand(obj, TRUE);
     ui_textarea(obj, NULL);
+    ui_button(obj, "BTN1", NULL, NULL);
     
     ui_end(obj);
     
--- a/ui/wpf/UIcore/Container.cs	Sun Jan 17 19:19:28 2016 +0100
+++ b/ui/wpf/UIcore/Container.cs	Wed Jan 20 11:35:01 2016 +0100
@@ -18,10 +18,13 @@
     public class Layout
     {
         public bool? Fill { get; set; }
+        public bool Hexpand { get; set; }
+        public bool Vexpand { get; set; }
+        public bool NewLine { get; set; }
 
         public Layout()
         {
-            Fill = null;
+            Reset();
         }
 
         public bool IsFill(bool fill)
@@ -37,6 +40,9 @@
         public void Reset()
         {
             Fill = null;
+            Hexpand = false;
+            Vexpand = false;
+            NewLine = false;
         }
     }
 
@@ -146,20 +152,76 @@
         }
     }
     
-    public class GridContainer : Container
+    public class GridContainer : Grid, Container
     {
         public Layout Layout { get; set; }
-        
-        public Grid Grid;
+
+        private int X = 0;
+        private int Y = 0;
+        private int CurrentWidth = 0;
+        private int CurrentHeight = 0;
 
-        public GridContainer(System.Windows.Controls.Grid grid)
+        public GridContainer(Container parent, int margin, int colspacing, int rowspacing) : base()
         {
-            Grid = grid;
+            Layout = new Layout();
+
+            parent.Add(this, true);
         }
 
         public void Add(UIElement control, bool fill)
         {
+            if(Layout.NewLine)
+            {
+                X = 0;
+                Y++;
+            }
 
+            ColumnDefinition col;
+            RowDefinition row;
+            if(X >= CurrentWidth)
+            {
+                col = new ColumnDefinition();
+                col.Width = GridLength.Auto;
+                ColumnDefinitions.Add(col);
+                CurrentWidth = X + 1;
+            }
+            else
+            {
+                col = ColumnDefinitions.ElementAt(X);
+            }
+
+            if (Y >= CurrentHeight)
+            {
+                row = new RowDefinition();
+                row.Height = GridLength.Auto;
+                RowDefinitions.Add(row);
+                CurrentHeight = Y + 1;
+            }
+            else
+            {
+                row = RowDefinitions.ElementAt(Y);
+            }
+
+            if(Layout.Hexpand)
+            {
+                col.Width = new GridLength(1, GridUnitType.Star);
+            }
+            if(Layout.Vexpand)
+            {
+                row.Height = new GridLength(1, GridUnitType.Star);
+            }
+
+            Grid.SetColumn(control, X);
+            Grid.SetRow(control, Y);
+            Children.Add(control);
+
+            Layout.Reset();
+            X++;
+        }
+
+        public static GridContainer CreateGridContainer(Container parent, int margin, int colspacing, int rowspacing)
+        {
+            return Application.GetInstance().Exec<GridContainer>(() => new GridContainer(parent, margin, colspacing, rowspacing));
         }
     }
 }
--- a/ui/wpf/UIcore/Controls.cs	Sun Jan 17 19:19:28 2016 +0100
+++ b/ui/wpf/UIcore/Controls.cs	Wed Jan 20 11:35:01 2016 +0100
@@ -16,6 +16,24 @@
             return Application.GetInstance().Exec<Button>(() => Controls.CreateButton(container, label, e));
         }
 
+        public static Label Label(Container container, String label, int alignment)
+        {
+            HorizontalAlignment a;
+            switch(alignment)
+            {
+                case 0: a = HorizontalAlignment.Left; break;
+                case 1: a = HorizontalAlignment.Right; break;
+                case 2: a = HorizontalAlignment.Center; break;
+                default: a = HorizontalAlignment.Left; break;
+            }
+            return Application.GetInstance().Exec<Label>(() => Controls.CreateLabel(container, label, a));
+        }
+
+        public static Label Space(Container container)
+        {
+            return Application.GetInstance().Exec<Label>(() => Controls.CreateLabel(container, null, HorizontalAlignment.Center));
+        }
+
         public static Button CreateButton(Container container, String label, RoutedEventHandler e)
         {
             Button button = new Button();
@@ -27,6 +45,14 @@
             return button;
         }
 
+        public static Label CreateLabel(Container container, String str, HorizontalAlignment alignment)
+        {
+            Label label = new Label();
+            label.HorizontalAlignment = alignment;
+            label.Content = str;
+            container.Add(label, false);
 
+            return label;
+        }
     }
 }
--- a/ui/wpf/UIcore/TextArea.cs	Sun Jan 17 19:19:28 2016 +0100
+++ b/ui/wpf/UIcore/TextArea.cs	Wed Jan 20 11:35:01 2016 +0100
@@ -8,23 +8,34 @@
 {
     public class TextArea : System.Windows.Controls.TextBox
     {
-        public TextArea(Container container, String text) : base()
+        public TextArea(Container container, String text, bool textarea) : base()
         {
-            AcceptsReturn = true;
-            IsUndoEnabled = false; // we need our own undo stack
+            bool fill = false;
+            if (textarea)
+            {
+                AcceptsReturn = true;
+                IsUndoEnabled = false; // we need our own undo stack
+                VerticalScrollBarVisibility = System.Windows.Controls.ScrollBarVisibility.Auto;
+                HorizontalScrollBarVisibility = System.Windows.Controls.ScrollBarVisibility.Auto;
+                fill = true;
+            }
+
             if (text != null)
             {
                 Text = text;
             }
-            VerticalScrollBarVisibility = System.Windows.Controls.ScrollBarVisibility.Auto;
-            HorizontalScrollBarVisibility = System.Windows.Controls.ScrollBarVisibility.Auto;
-            
-            container.Add(this, true);
+
+            container.Add(this, fill);
         }
 
         public static TextArea CreateTextArea(Container container, String text)
         {
-            return Application.GetInstance().Exec<TextArea>(() => new TextArea(container, text));
+            return Application.GetInstance().Exec<TextArea>(() => new TextArea(container, text, true));
+        }
+
+        public static TextArea CreateTextField(Container container, String text)
+        {
+            return Application.GetInstance().Exec<TextArea>(() => new TextArea(container, text, false));
         }
 
 
--- a/ui/wpf/UIwrapper/UIwrapper/UIwrapper.vcxproj	Sun Jan 17 19:19:28 2016 +0100
+++ b/ui/wpf/UIwrapper/UIwrapper/UIwrapper.vcxproj	Wed Jan 20 11:35:01 2016 +0100
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="utf-8"?>
-<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
   <ItemGroup Label="ProjectConfigurations">
     <ProjectConfiguration Include="Debug|Win32">
       <Configuration>Debug</Configuration>
@@ -20,7 +20,7 @@
   </ItemGroup>
   <PropertyGroup Label="Globals">
     <ProjectGuid>{367C474F-D7EA-44E3-9CB7-A4A35DCE9CC5}</ProjectGuid>
-    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
+    <TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
     <Keyword>ManagedCProj</Keyword>
     <RootNamespace>UIwrapper</RootNamespace>
   </PropertyGroup>
@@ -28,28 +28,28 @@
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
     <ConfigurationType>DynamicLibrary</ConfigurationType>
     <UseDebugLibraries>true</UseDebugLibraries>
-    <PlatformToolset>v120</PlatformToolset>
+    <PlatformToolset>v140</PlatformToolset>
     <CLRSupport>true</CLRSupport>
     <CharacterSet>Unicode</CharacterSet>
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
     <ConfigurationType>DynamicLibrary</ConfigurationType>
     <UseDebugLibraries>true</UseDebugLibraries>
-    <PlatformToolset>v120</PlatformToolset>
+    <PlatformToolset>v140</PlatformToolset>
     <CLRSupport>true</CLRSupport>
     <CharacterSet>Unicode</CharacterSet>
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
     <ConfigurationType>DynamicLibrary</ConfigurationType>
     <UseDebugLibraries>false</UseDebugLibraries>
-    <PlatformToolset>v120</PlatformToolset>
+    <PlatformToolset>v140</PlatformToolset>
     <CLRSupport>true</CLRSupport>
     <CharacterSet>Unicode</CharacterSet>
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
     <ConfigurationType>DynamicLibrary</ConfigurationType>
     <UseDebugLibraries>false</UseDebugLibraries>
-    <PlatformToolset>v120</PlatformToolset>
+    <PlatformToolset>v140</PlatformToolset>
     <CLRSupport>true</CLRSupport>
     <CharacterSet>Unicode</CharacterSet>
   </PropertyGroup>
--- a/ui/wpf/UIwrapper/UIwrapper/container.cpp	Sun Jan 17 19:19:28 2016 +0100
+++ b/ui/wpf/UIwrapper/UIwrapper/container.cpp	Wed Jan 20 11:35:01 2016 +0100
@@ -21,6 +21,13 @@
 	return container;
 }
 
+UI_EXPORT void* __stdcall UIgrid(gcroot<UI::Container^> *parent, int margin, int columnspacing, int rowspacing) {
+	UI::GridContainer ^grid = UI::GridContainer::CreateGridContainer(*parent, margin, columnspacing, rowspacing);
+	gcroot<UI::GridContainer^> *container = new gcroot<UI::GridContainer^>();
+	*container = grid;
+	return container;
+}
+
 
 
 /* ------------------- layout functions ------------------- */
@@ -29,3 +36,19 @@
 	UI::Container ^ct = *container;
 	ct->Layout->Fill = fill != 0;
 }
+
+UI_EXPORT void __stdcall UIlayout_hexpand(gcroot<UI::Container^> *container, int expand) {
+	UI::Container ^ct = *container;
+	ct->Layout->Hexpand = expand != 0;
+}
+
+UI_EXPORT void __stdcall UIlayout_vexpand(gcroot<UI::Container^> *container, int expand) {
+	UI::Container ^ct = *container;
+	ct->Layout->Vexpand = expand != 0;
+}
+
+UI_EXPORT void __stdcall UIlayout_newline(gcroot<UI::Container^> *container) {
+	UI::Container ^ct = *container;
+	ct->Layout->NewLine = true;
+}
+
--- a/ui/wpf/UIwrapper/UIwrapper/controls.cpp	Sun Jan 17 19:19:28 2016 +0100
+++ b/ui/wpf/UIwrapper/UIwrapper/controls.cpp	Wed Jan 20 11:35:01 2016 +0100
@@ -7,6 +7,8 @@
 
 #using "UIcore.dll"
 
+/* ------------------------------ Buttons ------------------------------ */
+
 UI_EXPORT void* __stdcall UIbutton(gcroot<UI::Container^> *container, char *label, UIcallback f, void *eventdata) {
 	gcroot<Button^> *button = new gcroot<Button^>();
 
@@ -18,6 +20,29 @@
 }
 
 
+/* ------------------------------ Labels ------------------------------ */
+
+UI_EXPORT void* __stdcall UIlabel(gcroot<UI::Container^> *container, char *label, int alignment) {
+	gcroot<Label^> *control = new gcroot<Label^>();
+	*control = UI::Controls::Label(*container, gcnew String(label), alignment);
+	return control;
+}
+
+UI_EXPORT void* __stdcall UIspace(gcroot<UI::Container^> *container) {
+	gcroot<Label^> *control = new gcroot<Label^>();
+
+	*control = UI::Controls::Space(*container);
+	return control;
+}
+
+UI_EXPORT void* __stdcall UIseparator(gcroot<UI::Container^> *container) {
+	return NULL;
+}
+
+
+
+/* ------------------------------ Textarea ------------------------------ */
+
 UI_EXPORT void* __stdcall UItextarea(gcroot<UI::Container^> *container, char *text) {
 	String ^str = nullptr;
 	if (text) {
@@ -67,3 +92,18 @@
 UI_EXPORT void __stdcall UIfreestr(char *str) {
 	Marshal::FreeHGlobal((IntPtr)(void*)str);
 }
+
+
+/* ------------------------------ Textfield ------------------------------ */
+
+UI_EXPORT void* __stdcall UItextfield(gcroot<UI::Container^> *container, char *text) {
+	String ^str = nullptr;
+	if (text) {
+		str = gcnew String(text);
+	}
+
+	gcroot<UI::TextArea^> *textfield = new gcroot<UI::TextArea^>();
+	*textfield = UI::TextArea::CreateTextField(*container, str);
+
+	return textfield;
+}
\ No newline at end of file
--- a/ui/wpf/container.c	Sun Jan 17 19:19:28 2016 +0100
+++ b/ui/wpf/container.c	Wed Jan 20 11:35:01 2016 +0100
@@ -35,7 +35,7 @@
 UIWIDGET ui_vbox(UiObject *obj) {
     UiContainer *ct = uic_get_current_container(obj);
     
-    UIWIDGET *vbox = UIvbox(ct);
+    UIWIDGET vbox = UIvbox(ct);
     
     UiObject *newobj = uic_object_new(obj, vbox);
     newobj->container = (UiContainer*)vbox;
@@ -47,7 +47,7 @@
 UIWIDGET ui_hbox(UiObject *obj) {
     UiContainer *ct = uic_get_current_container(obj);
     
-    UIWIDGET *hbox = UIhbox(ct);
+    UIWIDGET hbox = UIhbox(ct);
     
     UiObject *newobj = uic_object_new(obj, hbox);
     newobj->container = (UiContainer*)hbox;
@@ -56,6 +56,21 @@
     return hbox;
 }
 
+UIWIDGET ui_grid(UiObject *obj) {
+    return ui_grid_sp(obj, 0, 0, 0);
+}
+
+UIWIDGET ui_grid_sp(UiObject *obj, int margin, int columnspacing, int rowspacing) {
+    UiContainer *ct = uic_get_current_container(obj);
+    
+    UIWIDGET grid = UIgrid(ct, margin, columnspacing, rowspacing);
+    
+    UiObject *newobj = uic_object_new(obj, grid);
+    newobj->container = (UiContainer*)grid;
+    uic_obj_add(obj, newobj);
+    
+    return grid;
+}
 
 /*
  * -------------------- Layout Functions --------------------
@@ -69,7 +84,17 @@
     UIlayout_fill(ct, fill);
 }
 
+void ui_layout_hexpand(UiObject *obj, UiBool expand) {
+    UiContainer *ct = uic_get_current_container(obj);
+    UIlayout_hexpand(ct, expand);
+}
+
+void ui_layout_vexpand(UiObject *obj, UiBool expand) {
+    UiContainer *ct = uic_get_current_container(obj);
+    UIlayout_vexpand(ct, expand);
+}
+
 void ui_newline(UiObject *obj) {
     UiContainer *ct = uic_get_current_container(obj);
-    
+    UIlayout_newline(ct);
 }
\ No newline at end of file
--- a/ui/wpf/container.h	Sun Jan 17 19:19:28 2016 +0100
+++ b/ui/wpf/container.h	Wed Jan 20 11:35:01 2016 +0100
@@ -37,9 +37,14 @@
 
 UI_IMPORT void* __stdcall UIvbox(UiContainer *parent);
 UI_IMPORT void* __stdcall UIhbox(UiContainer *parent);
+UI_IMPORT void* __stdcall UIgrid(UiContainer *parent, int margin, int columnspacing, int rowspacing);
 
 
 UI_IMPORT void __stdcall UIlayout_fill(UiContainer *container, int fill);
+UI_IMPORT void __stdcall UIlayout_hexpand(UiContainer *container, int expand);
+UI_IMPORT void __stdcall UIlayout_vexpand(UiContainer *container, int expand);
+
+UI_IMPORT void __stdcall UIlayout_newline(UiContainer *container);
 
 #ifdef	__cplusplus
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/wpf/label.c	Wed Jan 20 11:35:01 2016 +0100
@@ -0,0 +1,28 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "label.h"
+#include "container.h"
+#include "../../ucx/mempool.h"
+#include "../common/context.h"
+#include "../common/object.h"
+
+UIWIDGET ui_label(UiObject *obj, char *label) {
+    return UIlabel(uic_get_current_container(obj), label, 2);
+}
+
+UIWIDGET ui_llabel(UiObject *obj, char *label) {
+    return UIlabel(uic_get_current_container(obj), label, 0);
+}
+
+UIWIDGET ui_rlabel(UiObject *obj, char *label) {
+    return UIlabel(uic_get_current_container(obj), label, 1);
+}
+
+UIWIDGET ui_space(UiObject *obj) {
+    return UIspace(uic_get_current_container(obj));
+}
+
+UIWIDGET ui_separator(UiObject *obj) {
+    return UIseparator(uic_get_current_container(obj));
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/wpf/label.h	Wed Jan 20 11:35:01 2016 +0100
@@ -0,0 +1,28 @@
+/* 
+ * File:   label.h
+ * Author: Olaf
+ *
+ * Created on 19. Januar 2016, 18:12
+ */
+
+#ifndef LABEL_H
+#define	LABEL_H
+
+#include "toolkit.h"
+
+#ifdef	__cplusplus
+extern "C" {
+#endif
+
+UI_IMPORT UIWIDGET __stdcall UIlabel(void *container, char *label, int alignment);
+
+UI_IMPORT UIWIDGET __stdcall UIspace(void *container);
+
+UI_IMPORT UIWIDGET __stdcall UIseparator(void *container);
+
+#ifdef	__cplusplus
+}
+#endif
+
+#endif	/* LABEL_H */
+
--- a/ui/wpf/objs.mk	Sun Jan 17 19:19:28 2016 +0100
+++ b/ui/wpf/objs.mk	Wed Jan 20 11:35:01 2016 +0100
@@ -35,6 +35,7 @@
 WPFOBJ += menu.o
 WPFOBJ += toolbar.o
 WPFOBJ += button.o
+WPFOBJ += label.o
 WPFOBJ += text.o
 
 TOOLKITOBJS += $(WPFOBJ:%=$(WPF_OBJPRE)%)
--- a/ui/wpf/text.c	Sun Jan 17 19:19:28 2016 +0100
+++ b/ui/wpf/text.c	Wed Jan 20 11:35:01 2016 +0100
@@ -116,3 +116,25 @@
     }
     UItextarea_remove(text->obj, begin, end);
 }
+
+
+UIWIDGET ui_textfield(UiObject *obj, UiString *value) {
+    UiContainer *container = uic_get_current_container(obj); 
+    UIWIDGET textfield = UItextfield(container, value ? value->value : NULL);
+    
+    if(value) {
+        // TODO
+    }
+    return textfield;
+}
+
+UIWIDGET ui_textfield_nv(UiObject *obj, char *varname) {
+    UiVar *var = uic_connect_var(obj->ctx, varname, UI_VAR_STRING);
+    if(var) {
+        UiString *value = var->value;
+        return ui_textfield(obj, value);
+    } else {
+        // TODO: error
+    }
+    return NULL;
+}
--- a/ui/wpf/text.h	Sun Jan 17 19:19:28 2016 +0100
+++ b/ui/wpf/text.h	Wed Jan 20 11:35:01 2016 +0100
@@ -58,6 +58,9 @@
 
 UI_IMPORT void __stdcall UIfreestr(char *str);
 
+
+UI_IMPORT UIWIDGET __stdcall UItextfield(void *container, char *text);
+
 #ifdef	__cplusplus
 }
 #endif

mercurial