# HG changeset patch # User Olaf Wintermann # Date 1485165022 -3600 # Node ID c9b8b9e0cfe8e8d1e1b7533670ffcc2722e49517 # Parent 1df2fb3d079c5954d6fb4b94b163a0468bb2392f adds drawingarea (WPF) diff -r 1df2fb3d079c -r c9b8b9e0cfe8 .hgignore --- 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 diff -r 1df2fb3d079c -r c9b8b9e0cfe8 application/main.c --- 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(); diff -r 1df2fb3d079c -r c9b8b9e0cfe8 ui/wpf/UIcore/DrawingArea.cs --- /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 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); + } + } +} diff -r 1df2fb3d079c -r c9b8b9e0cfe8 ui/wpf/UIcore/MainToolBar.cs --- 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) diff -r 1df2fb3d079c -r c9b8b9e0cfe8 ui/wpf/UIcore/Menu.cs --- 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(); diff -r 1df2fb3d079c -r c9b8b9e0cfe8 ui/wpf/UIcore/UIcore.csproj --- 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 @@ + diff -r 1df2fb3d079c -r c9b8b9e0cfe8 ui/wpf/UIcore/Window.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 diff -r 1df2fb3d079c -r c9b8b9e0cfe8 ui/wpf/UIwrapper/UIwrapper/UIwrapper.vcxproj --- 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 @@ + @@ -165,6 +166,7 @@ + diff -r 1df2fb3d079c -r c9b8b9e0cfe8 ui/wpf/UIwrapper/UIwrapper/UIwrapper.vcxproj.filters --- 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 @@ Headerdateien + + Headerdateien + @@ -65,6 +68,9 @@ Quelldateien + + Quelldateien + diff -r 1df2fb3d079c -r c9b8b9e0cfe8 ui/wpf/UIwrapper/UIwrapper/graphics.cpp --- /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 + +#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(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 *container, UIdrawfunc f, void *data) +{ + gcroot *canvas = new gcroot(); + *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 *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); +} diff -r 1df2fb3d079c -r c9b8b9e0cfe8 ui/wpf/UIwrapper/UIwrapper/graphics.h --- /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 ^action; + + DrawEventWrapper(void *gc, UIdrawfunc callback, void *eventdata); + + void Callback(int width, int height); +}; diff -r 1df2fb3d079c -r c9b8b9e0cfe8 ui/wpf/UIwrapper/UIwrapper/toolkit.cpp --- 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; } diff -r 1df2fb3d079c -r c9b8b9e0cfe8 ui/wpf/button.c --- 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); } diff -r 1df2fb3d079c -r c9b8b9e0cfe8 ui/wpf/graphics.c --- /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 +#include + +#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) { + +} diff -r 1df2fb3d079c -r c9b8b9e0cfe8 ui/wpf/graphics.h --- /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 */ + diff -r 1df2fb3d079c -r c9b8b9e0cfe8 ui/wpf/objs.mk --- 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)