adds drawingarea (WPF)

Mon, 23 Jan 2017 10:50:22 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 23 Jan 2017 10:50:22 +0100
changeset 137
c9b8b9e0cfe8
parent 136
1df2fb3d079c
child 138
d781436e2490

adds drawingarea (WPF)

.hgignore file | annotate | diff | comparison | revisions
application/main.c file | annotate | diff | comparison | revisions
ui/wpf/UIcore/DrawingArea.cs file | annotate | diff | comparison | revisions
ui/wpf/UIcore/MainToolBar.cs file | annotate | diff | comparison | revisions
ui/wpf/UIcore/Menu.cs file | annotate | diff | comparison | revisions
ui/wpf/UIcore/UIcore.csproj file | annotate | diff | comparison | revisions
ui/wpf/UIcore/Window.cs file | annotate | diff | comparison | revisions
ui/wpf/UIwrapper/UIwrapper/UIwrapper.vcxproj file | annotate | diff | comparison | revisions
ui/wpf/UIwrapper/UIwrapper/UIwrapper.vcxproj.filters file | annotate | diff | comparison | revisions
ui/wpf/UIwrapper/UIwrapper/graphics.cpp file | annotate | diff | comparison | revisions
ui/wpf/UIwrapper/UIwrapper/graphics.h file | annotate | diff | comparison | revisions
ui/wpf/UIwrapper/UIwrapper/toolkit.cpp file | annotate | diff | comparison | revisions
ui/wpf/button.c file | annotate | diff | comparison | revisions
ui/wpf/graphics.c file | annotate | diff | comparison | revisions
ui/wpf/graphics.h file | annotate | diff | comparison | revisions
ui/wpf/objs.mk file | annotate | diff | comparison | revisions
--- a/.hgignore	Sun Jan 22 18:02:45 2017 +0100
+++ b/.hgignore	Mon Jan 23 10:50:22 2017 +0100
@@ -2,9 +2,9 @@
 relre:^config.mk$
 relre:^core$
 relre:^ui/wpf/UIcore/obj
-relre:^ui/wpf/UIWrapper/.vs
-relre:^ui/wpf/UIWrapper/UIwrapper.VC
-relre:^ui/wpf/UIWrapper/UIWrapper/Debug
-relre:^ui/wpf/UIWrapper/UIWrapper/Release
-relre:^ui/wpf/UIWrapper/UIWrapper/x64
+relre:^ui/wpf/UIwrapper/.vs
+relre:^ui/wpf/UIwrapper/UIwrapper.VC
+relre:^ui/wpf/UIwrapper/UIWrapper/Debug
+relre:^ui/wpf/UIwrapper/UIWrapper/Release
+relre:^ui/wpf/UIwrapper/UIWrapper/x64
 relre:^ui/wpf/UIwrapper/ipch
\ No newline at end of file
--- a/application/main.c	Sun Jan 22 18:02:45 2017 +0100
+++ b/application/main.c	Mon Jan 23 10:50:22 2017 +0100
@@ -35,12 +35,32 @@
 
 void action_menu(UiEvent *event, void *data) {
     printf("action_menu\n");
+    fflush(stdout);
 }
 
 void action_button(UiEvent *event, void *data) {
     printf("action_button\n");
+    fflush(stdout);
 }
 
+void draw(UiEvent *event, UiGraphics *g, void *data) {
+    printf("draw: %d, %d\n", g->width, g->height);
+    fflush(stdout);
+    
+    ui_graphics_color(g, 200, 240, 240);
+    ui_draw_rect(g, 0, 0, 50, g->height, TRUE);
+    
+    ui_graphics_color(g, 150, 150, 200);
+    ui_draw_rect(g, 50, 0, g->width - 50, g->height, TRUE);
+    
+    ui_graphics_color(g, 0, 0, 0);
+    ui_draw_line(g, 0, 10, 100, 10);
+    ui_draw_line(g, 0, 10, 10, 50);
+    ui_draw_line(g, 10, 50, 50, 50);
+    ui_draw_line(g, 50, 50, 100, 100);
+    
+    ui_draw_rect(g, 15, 15, 80, 80, FALSE);
+}
 
 void application_startup(UiEvent *event, void *data) {
     UiObject *obj = ui_window("Test", NULL);
@@ -57,7 +77,8 @@
     ui_layout_hexpand(obj, TRUE);
     ui_layout_vexpand(obj, TRUE);
     ui_layout_gridwidth(obj, 2);
-    ui_textarea(obj, NULL);
+    
+    ui_drawingarea(obj, draw, NULL);
     
     ui_newline(obj);
     
@@ -84,8 +105,18 @@
     // toolbar
     ui_toolitem("button1", "Test1", action_button, NULL);
     ui_toolitem("button2", "Test2", action_button, NULL);
+    ui_toolitem("button3", "Test3", action_button, NULL);
+    ui_toolitem("button4", "Test4", action_button, NULL);
+    ui_toolitem("button5", "Test5", action_button, NULL);
+    ui_toolitem("button6", "Test6", action_button, NULL);
+    ui_toolitem("button7", "Test7", action_button, NULL);
     ui_toolbar_add_default("button1");
     ui_toolbar_add_default("button2");
+    ui_toolbar_add_default("button3");
+    ui_toolbar_add_default("button4");
+    ui_toolbar_add_default("button5");
+    ui_toolbar_add_default("button6");
+    ui_toolbar_add_default("button7");
     
     ui_main();
     
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/wpf/UIcore/DrawingArea.cs	Mon Jan 23 10:50:22 2017 +0100
@@ -0,0 +1,99 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Media;
+using System.Windows.Shapes;
+using System.Diagnostics;
+
+namespace UI
+{
+    public class DrawingArea : System.Windows.Controls.Canvas
+    {
+        public Action<int,int> resizeCallback;
+
+        public Color Color;
+        private Brush Brush;
+
+        public DrawingArea(Container container) : base()
+        {
+            this.SizeChanged += UpdateSize;
+            ResetGraphics();
+
+            container.Add(this, true);
+        }
+
+        public void UpdateSize(object sender, SizeChangedEventArgs e)
+        {
+            if(resizeCallback != null)
+            {
+                Children.Clear();
+                ResetGraphics();
+
+                Size s = e.NewSize;
+                resizeCallback((int)s.Width, (int)s.Height);
+            }
+        }
+
+        public void Redraw()
+        {
+            if (resizeCallback != null)
+            {
+                Children.Clear();
+                ResetGraphics();
+
+                resizeCallback((int)ActualWidth, (int)ActualHeight);
+            }
+
+        }
+
+        private void ResetGraphics()
+        {
+            Color = Color.FromRgb(0, 0, 0);
+            Brush = System.Windows.Media.Brushes.Black;
+        }
+
+        public void SetColor(int r, int g, int b)
+        {
+            Color = Color.FromRgb((byte)r, (byte)g, (byte)b);
+            Brush = new SolidColorBrush(Color);
+        }
+
+        public void DrawLine(int x1, int y1, int x2, int y2)
+        {
+            Line line = new Line();
+            line.Stroke = Brush;
+            line.StrokeThickness = 1;
+            line.SnapsToDevicePixels = true;
+
+            line.X1 = x1;
+            line.Y1 = y1;
+            line.X2 = x2;
+            line.Y2 = y2;
+
+            Children.Add(line);
+        }
+
+        public void DrawRect(int x, int y, int w, int h, bool fill)
+        {
+            Rectangle rect = new Rectangle();
+            rect.Stroke = Brush;
+            rect.StrokeThickness = 1;
+            rect.SnapsToDevicePixels = true;
+            if(fill)
+            {
+                rect.Fill = Brush;
+            }
+
+            rect.Width = w;
+            rect.Height = h;
+            SetLeft(rect, x);
+            SetTop(rect, y);
+
+            Children.Add(rect);
+        }
+    }
+}
--- a/ui/wpf/UIcore/MainToolBar.cs	Sun Jan 22 18:02:45 2017 +0100
+++ b/ui/wpf/UIcore/MainToolBar.cs	Mon Jan 23 10:50:22 2017 +0100
@@ -5,6 +5,7 @@
 using System.Threading.Tasks;
 using System.Windows;
 using System.Windows.Controls;
+using System.Windows.Media;
 
 namespace UI
 {
@@ -39,7 +40,6 @@
         public ToolBarTray CreateToolBarTray(IntPtr objptr)
         {
             ToolBarTray tray = new ToolBarTray();
-
             ToolBar toolbar = new ToolBar();
             tray.ToolBars.Add(toolbar);
             foreach(string s in Defaults)
--- a/ui/wpf/UIcore/Menu.cs	Sun Jan 22 18:02:45 2017 +0100
+++ b/ui/wpf/UIcore/Menu.cs	Mon Jan 23 10:50:22 2017 +0100
@@ -5,6 +5,7 @@
 using System.Threading.Tasks;
 using System.Windows;
 using System.Windows.Controls;
+using System.Windows.Media;
 
 namespace UI
 {
@@ -51,6 +52,7 @@
         public System.Windows.Controls.Menu CreateMenu(IntPtr uiobj)
         {
             System.Windows.Controls.Menu menu = new System.Windows.Controls.Menu();
+            menu.Background = new SolidColorBrush(Color.FromRgb(255, 255, 255));
             foreach (Menu m in Menus)
             {
                 System.Windows.Controls.MenuItem i = new System.Windows.Controls.MenuItem();
--- a/ui/wpf/UIcore/UIcore.csproj	Sun Jan 22 18:02:45 2017 +0100
+++ b/ui/wpf/UIcore/UIcore.csproj	Mon Jan 23 10:50:22 2017 +0100
@@ -48,6 +48,7 @@
   <ItemGroup>
     <Compile Include="Container.cs" />
     <Compile Include="Controls.cs" />
+    <Compile Include="DrawingArea.cs" />
     <Compile Include="MainToolBar.cs" />
     <Compile Include="Menu.cs" />
     <Compile Include="TextArea.cs" />
--- a/ui/wpf/UIcore/Window.cs	Sun Jan 22 18:02:45 2017 +0100
+++ b/ui/wpf/UIcore/Window.cs	Mon Jan 23 10:50:22 2017 +0100
@@ -42,9 +42,10 @@
 
             // menu
             Application app = Application.GetInstance();
+            System.Windows.Controls.Menu menu = null;
             if (!app.Menu.IsEmpty())
             {
-                System.Windows.Controls.Menu menu = app.Menu.CreateMenu(uiobj);
+                menu = app.Menu.CreateMenu(uiobj);
 
                 RowDefinition menuRow = new RowDefinition();
                 menuRow.Height = GridLength.Auto;
@@ -68,6 +69,11 @@
                 Grid.SetColumn(tray, 0);
                 windowGrid.Children.Add(tray);
                 rowIndex++;
+
+                if(menu != null)
+                {
+                    menu.Background = tray.Background;
+                }
             }
 
             // content
--- a/ui/wpf/UIwrapper/UIwrapper/UIwrapper.vcxproj	Sun Jan 22 18:02:45 2017 +0100
+++ b/ui/wpf/UIwrapper/UIwrapper/UIwrapper.vcxproj	Mon Jan 23 10:50:22 2017 +0100
@@ -155,6 +155,7 @@
   <ItemGroup>
     <ClInclude Include="controls.h" />
     <ClInclude Include="container.h" />
+    <ClInclude Include="graphics.h" />
     <ClInclude Include="menu.h" />
     <ClInclude Include="resource.h" />
     <ClInclude Include="Stdafx.h" />
@@ -165,6 +166,7 @@
   <ItemGroup>
     <ClCompile Include="AssemblyInfo.cpp" />
     <ClCompile Include="controls.cpp" />
+    <ClCompile Include="graphics.cpp" />
     <ClCompile Include="menu.cpp" />
     <ClCompile Include="container.cpp" />
     <ClCompile Include="toolbar.cpp" />
--- a/ui/wpf/UIwrapper/UIwrapper/UIwrapper.vcxproj.filters	Sun Jan 22 18:02:45 2017 +0100
+++ b/ui/wpf/UIwrapper/UIwrapper/UIwrapper.vcxproj.filters	Mon Jan 23 10:50:22 2017 +0100
@@ -39,6 +39,9 @@
     <ClInclude Include="toolbar.h">
       <Filter>Headerdateien</Filter>
     </ClInclude>
+    <ClInclude Include="graphics.h">
+      <Filter>Headerdateien</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="AssemblyInfo.cpp">
@@ -65,6 +68,9 @@
     <ClCompile Include="toolbar.cpp">
       <Filter>Quelldateien</Filter>
     </ClCompile>
+    <ClCompile Include="graphics.cpp">
+      <Filter>Quelldateien</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <Text Include="ReadMe.txt" />
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/wpf/UIwrapper/UIwrapper/graphics.cpp	Mon Jan 23 10:50:22 2017 +0100
@@ -0,0 +1,63 @@
+#include "stdafx.h"
+#include <stdio.h>
+
+#include "graphics.h"
+
+#using "UIcore.dll"
+
+
+DrawEventWrapper::DrawEventWrapper(void *gc, UIdrawfunc callback, void *eventdata) {
+	this->callback = callback;
+	this->eventdata = eventdata;
+	this->gc = gc;
+	action = gcnew Action<int,int>(this, &DrawEventWrapper::Callback);
+}
+
+
+void DrawEventWrapper::Callback(int width, int height)
+{
+	if (callback)
+	{
+		UI::DrawingArea ^d = (UI::DrawingArea^)PtrToObject(gc);
+		callback(gc, eventdata, width, height);
+	}
+}
+
+
+UI_EXPORT void* __stdcall UIdrawingarea(gcroot<UI::Container^> *container, UIdrawfunc f, void *data)
+{
+	gcroot<UI::DrawingArea^> *canvas = new gcroot<UI::DrawingArea^>();
+	*canvas = gcnew UI::DrawingArea(*container);
+
+	DrawEventWrapper ^ev = gcnew DrawEventWrapper(ObjectToPtr(*canvas), f, data);
+	(*canvas)->resizeCallback = ev->action;
+
+	return canvas;
+}
+
+
+UI_EXPORT void __stdcall UIdrawingarea_redraw(gcroot<UI::DrawingArea^> *drawingarea)
+{
+	(*drawingarea)->Redraw();
+}
+
+
+/* ------------------------- drawing functions ------------------------- */
+
+UI_EXPORT void __stdcall UIgraphics_color(void *g, int red, int green, int blue)
+{
+	UI::DrawingArea ^d = (UI::DrawingArea^)PtrToObject(g);
+	d->SetColor(red, green, blue);
+}
+
+UI_EXPORT void __stdcall UIdraw_line(void *g, int x1, int y1, int x2, int y2)
+{
+	UI::DrawingArea ^d = (UI::DrawingArea^)PtrToObject(g);
+	d->DrawLine(x1, y1, x2, y2);
+}
+
+UI_EXPORT void __stdcall UIdraw_rect(void *g, int x, int y, int w, int h, int fill)
+{
+	UI::DrawingArea ^d = (UI::DrawingArea^)PtrToObject(g);
+	d->DrawRect(x, y, w, h, fill ? true : false);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/wpf/UIwrapper/UIwrapper/graphics.h	Mon Jan 23 10:50:22 2017 +0100
@@ -0,0 +1,17 @@
+#pragma once
+
+#include "toolkit.h"
+
+typedef void(*UIdrawfunc)(void *gc, void *event, int width, int height);
+
+public ref class DrawEventWrapper {
+public:
+	UIdrawfunc callback = NULL;
+	void *eventdata = NULL;
+	void *gc;
+	Action<int,int> ^action;
+
+	DrawEventWrapper(void *gc, UIdrawfunc callback, void *eventdata);
+
+	void Callback(int width, int height);
+};
--- a/ui/wpf/UIwrapper/UIwrapper/toolkit.cpp	Sun Jan 22 18:02:45 2017 +0100
+++ b/ui/wpf/UIwrapper/UIwrapper/toolkit.cpp	Mon Jan 23 10:50:22 2017 +0100
@@ -50,7 +50,7 @@
 Object^ PtrToObject(void *ptr) {
 	GCHandle h = GCHandle::FromIntPtr(IntPtr(ptr));
 	Object^ object = h.Target;
-	h.Free();
+	//h.Free();
 	return object;
 }
 
--- a/ui/wpf/button.c	Sun Jan 22 18:02:45 2017 +0100
+++ b/ui/wpf/button.c	Mon Jan 23 10:50:22 2017 +0100
@@ -44,7 +44,6 @@
         callback = (ui_callback)ui_button_callback;
     }
     
-    fflush(stdout);
     UiContainer *container = uic_get_current_container(obj);
     return UIbutton(container, label, callback, event);
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/wpf/graphics.c	Mon Jan 23 10:50:22 2017 +0100
@@ -0,0 +1,74 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "graphics.h"
+#include "container.h"
+#include "../../ucx/mempool.h"
+#include "../common/context.h"
+#include "../common/object.h"
+
+UIWIDGET ui_drawingarea(UiObject *obj, ui_drawfunc f, void *userdata) {
+    UiDrawEvent *eventdata = NULL;
+    ui_draw_callback cb = NULL;
+    if(f) {
+        eventdata = malloc(sizeof(UiDrawEvent));
+        eventdata->obj = obj;
+        eventdata->draw = f;
+        eventdata->userdata = userdata;
+        cb = ui_draw_event;
+    }
+    
+    UiContainer *container = uic_get_current_container(obj);
+    return UIdrawingarea(container, cb, eventdata);
+}
+
+void ui_draw_event(void *gc, UiDrawEvent *event, int width, int height) {
+    UiEvent e;
+    e.obj = event->obj;
+    e.window = e.obj->window;
+    e.document = e.obj->ctx->document;
+    e.eventdata = NULL;
+    e.intval = 0;
+    
+    UiWPFGraphics g;
+    g.g.width = width;
+    g.g.height = height;
+    g.gc = gc;
+    
+    event->draw(&e, &g.g, event->userdata);
+}
+
+
+void ui_drawingarea_mousehandler(UiObject *obj, UIWIDGET widget, ui_callback f, void *u) {
+    
+}
+
+void ui_drawingarea_getsize(UIWIDGET drawingarea, int *width, int *height) {
+    
+}
+
+void ui_drawingarea_redraw(UIWIDGET drawingarea) {
+    UIdrawingarea_redraw(drawingarea);
+}
+
+
+/* ------------------------- drawing functions ------------------------- */
+
+void ui_graphics_color(UiGraphics *g, int red, int green, int blue) {
+    UiWPFGraphics *wg = (UiWPFGraphics*)g;
+    UIgraphics_color(wg->gc, red, green, blue);
+}
+
+void ui_draw_line(UiGraphics *g, int x1, int y1, int x2, int y2) {
+    UiWPFGraphics *wg = (UiWPFGraphics*)g;
+    UIdraw_line(wg->gc, x1, y1, x2, y2);
+}
+
+void ui_draw_rect(UiGraphics *g, int x, int y, int w, int h, int fill) {
+    UiWPFGraphics *wg = (UiWPFGraphics*)g;
+    UIdraw_rect(wg->gc, x, y, w, h, fill);
+}
+
+void ui_draw_text(UiGraphics *g, int x, int y, UiTextLayout *text) {
+    
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/wpf/graphics.h	Mon Jan 23 10:50:22 2017 +0100
@@ -0,0 +1,56 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+/* 
+ * File:   graphics.h
+ * Author: Olaf
+ *
+ * Created on 22. Januar 2017, 18:34
+ */
+
+#ifndef GRAPHICS_H
+#define GRAPHICS_H
+
+#include "toolkit.h"
+#include "../ui/graphics.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+    
+typedef struct UiDrawEvent {
+    UiObject    *obj;
+    ui_drawfunc draw;
+    void        *userdata;
+} UiDrawEvent;
+
+typedef struct UiWPFGraphics {
+    UiGraphics g;
+    void       *gc;
+} UiWPFGraphics;
+
+typedef void(*ui_draw_callback)(void *gc, UiDrawEvent *event, int width, int height);
+    
+UI_IMPORT UIWIDGET __stdcall UIdrawingarea(void *container, ui_draw_callback f, void *userdata);
+
+UI_IMPORT void __stdcall UIdrawingarea_redraw(UIWIDGET drawingarea);
+
+void ui_draw_event(void *gc, UiDrawEvent *event, int width, int height);
+
+// drawing functions
+
+UI_IMPORT void __stdcall UIgraphics_color(UiGraphics *g, int red, int green, int blue);
+UI_IMPORT void __stdcall UIdraw_line(UiGraphics *g, int x1, int y1, int x2, int y2);
+UI_IMPORT void __stdcall UIdraw_rect(UiGraphics *g, int x, int y, int w, int h, int fill);
+//void UIdraw_text(UiGraphics *g, int x, int y, UiTextLayout *text);
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* GRAPHICS_H */
+
--- a/ui/wpf/objs.mk	Sun Jan 22 18:02:45 2017 +0100
+++ b/ui/wpf/objs.mk	Mon Jan 23 10:50:22 2017 +0100
@@ -37,6 +37,7 @@
 WPFOBJ += button.o
 WPFOBJ += label.o
 WPFOBJ += text.o
+WPFOBJ += graphics.o
 
 TOOLKITOBJS += $(WPFOBJ:%=$(WPF_OBJPRE)%)
 TOOLKITSOURCE += $(WPFOBJ:%.o=wpf/%.c)

mercurial