refactors architecture (WPF)

Sun, 22 Jan 2017 17:23:20 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 22 Jan 2017 17:23:20 +0100
changeset 135
b9dc9cdfa23a
parent 134
69e8e0936858
child 136
1df2fb3d079c

refactors architecture (WPF)

.hgignore file | annotate | diff | comparison | revisions
ui/wpf/UIcore/Application.cs 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/UIcore/Window.cs 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/UIwrapper/UIwrapper/toolkit.cpp file | annotate | diff | comparison | revisions
ui/wpf/UIwrapper/UIwrapper/toolkit.h file | annotate | diff | comparison | revisions
ui/wpf/UIwrapper/UIwrapper/window.cpp file | annotate | diff | comparison | revisions
ui/wpf/toolkit.c file | annotate | diff | comparison | revisions
ui/wpf/toolkit.h file | annotate | diff | comparison | revisions
--- a/.hgignore	Sun Jan 22 11:48:50 2017 +0100
+++ b/.hgignore	Sun Jan 22 17:23:20 2017 +0100
@@ -1,3 +1,9 @@
 relre:^build/.*$
 relre:^config.mk$
 relre:^core$
+relre:^ui/wpf/UIcore/obj
+relre:^ui/wpf/UIWrapper/.vs
+relre:^ui/wpf/UIWrapper/Debug
+relre:^ui/wpf/UIWrapper/Release
+relre:^ui/wpf/UIWrapper/x64
+relre:^ui/wpf/UIWrapper/UIwrapper.VC
\ No newline at end of file
--- a/ui/wpf/UIcore/Application.cs	Sun Jan 22 11:48:50 2017 +0100
+++ b/ui/wpf/UIcore/Application.cs	Sun Jan 22 17:23:20 2017 +0100
@@ -15,21 +15,19 @@
         private System.Windows.Application application;
 
         private Thread thread;
-        private Queue<Action> queue = new Queue<Action>();
-        private object sync = new object();
-        private object result = new object();
-        private Boolean main = false;
 
         public String Name;
+
+        public IApplicationCallbacks callbacks;
+
         public List<Window> Windows = new List<Window>();
         public ApplicationMenu Menu = new ApplicationMenu();
         public MainToolBar ToolBar = new MainToolBar();
         
         private Application() : base()
         {
-            thread = new Thread(() => Run());
+            thread = new Thread(() => RunApplication());
             thread.SetApartmentState(ApartmentState.STA);
-            thread.Start();
         }
 
         public static Application GetInstance()
@@ -44,86 +42,22 @@
 
         public Thread Start()
         {
-            lock(sync)
-            {
-                queue.Enqueue(() => RunApplication());
-                Monitor.Pulse(sync);
-            }
+            thread.Start();
             return thread;
         }
 
         private void RunApplication()
         {
             application = new System.Windows.Application();
-            main = true;
-            application.Run();
-        }
 
-        public void Run()
-        {
-            lock(sync)
-            {
-                for (;;)
-                {
-                    Monitor.Wait(sync);
-                    Action action = queue.Dequeue();
-                    action.Invoke();
-                    lock (result)
-                    {
-                        Monitor.Pulse(result);
-                    }
-                    if (main)
-                    {
-                        // end loop after shutdown
-                        break;
-                    }
-                }
-            }
-        }
-
-        public T Exec<T>(Func<T> func)
-        {
-            ResultExec<T> e = new ResultExec<T>();
-            e.Func = func;
-
-            if (Thread.CurrentThread.GetApartmentState() == ApartmentState.STA)
+            if(callbacks != null)
             {
-                e.Exec();
-                return e.Result;
-            }
-            else
-            {
-                lock (sync)
-                {
-                    queue.Enqueue(() => e.Exec());
-                    Monitor.Pulse(sync);
-                }
-                lock (result)
-                {
-                    Monitor.Wait(result);
-                }
-
-                return e.Result;
+                callbacks.OnStartup();
             }
-        }
-
-        public void Exec(Action action)
-        {
-            if (Thread.CurrentThread.GetApartmentState() == ApartmentState.STA)
-            {
-                action.Invoke();
-            }
-            else
+            application.Run();
+            if(callbacks != null)
             {
-                lock (sync)
-                {
-                    queue.Enqueue(action);
-                    Monitor.Pulse(sync);
-                }
-                lock (result)
-                {
-                    Monitor.Wait(result);
-                }
+                callbacks.OnExit();
             }
         }
 
@@ -162,4 +96,11 @@
             Action.Invoke();
         }
     }
+
+    public interface IApplicationCallbacks
+    {
+        void OnStartup();
+        void OnOpen();
+        void OnExit();
+    }
 }
--- a/ui/wpf/UIcore/Container.cs	Sun Jan 22 11:48:50 2017 +0100
+++ b/ui/wpf/UIcore/Container.cs	Sun Jan 22 17:23:20 2017 +0100
@@ -145,11 +145,6 @@
 
             Layout.Reset();
         }
-
-        public static BoxContainer CreateBoxContainer(Container parent, BoxOrientation orientation)
-        {
-            return Application.GetInstance().Exec<BoxContainer>(() => new BoxContainer(parent, orientation));
-        }
     }
     
     public class GridContainer : Grid, Container
@@ -252,10 +247,5 @@
             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 22 11:48:50 2017 +0100
+++ b/ui/wpf/UIcore/Controls.cs	Sun Jan 22 17:23:20 2017 +0100
@@ -10,37 +10,8 @@
 {  
     public class Controls
     {
-
         public static Button Button(Container container, String label, RoutedEventHandler e)
         {
-            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 Separator Separator(Container container)
-        {
-            return Application.GetInstance().Exec<Separator>(() => Controls.CreateSeparator(container));
-        }
-
-        public static Button CreateButton(Container container, String label, RoutedEventHandler e)
-        {
             Button button = new Button();
             button.Content = label;
             container.Add(button, false);
@@ -50,17 +21,31 @@
             return button;
         }
 
-        public static Label CreateLabel(Container container, String str, HorizontalAlignment alignment)
+        public static Label Label(Container container, String str, 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;
+            }
+
             Label label = new Label();
-            label.HorizontalAlignment = alignment;
+            label.HorizontalAlignment = a;
             label.Content = str;
             container.Add(label, false);
 
             return label;
         }
 
-        public static Separator CreateSeparator(Container container)
+        public static Label Space(Container container)
+        {
+            return Label(container, null, 2);
+        }
+
+        public static Separator Separator(Container container)
         {
             Separator separator = new Separator();
             container.Add(separator, false);
--- a/ui/wpf/UIcore/TextArea.cs	Sun Jan 22 11:48:50 2017 +0100
+++ b/ui/wpf/UIcore/TextArea.cs	Sun Jan 22 17:23:20 2017 +0100
@@ -33,27 +33,17 @@
             container.Add(this, fill);
         }
 
-        public static TextArea CreateTextArea(Container container, String 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));
-        }
-
 
         // ------------------ UiText methods ------------------
 
         public void SetText(String str)
         {
-            Application.GetInstance().Exec(() => Text = str);
+            Text = str;
         }
 
         public String GetText()
         {
-            return Application.GetInstance().Exec<String>(() => Text);
+            return Text;
         }
 
         public String GetSubString(int begin, int end)
@@ -68,7 +58,7 @@
 
         public int Position()
         {
-            return Application.GetInstance().Exec<int>(() => CaretIndex);
+            return CaretIndex;
         }
 
         public int Selection()
@@ -78,7 +68,7 @@
 
         public int Length()
         {
-            return Application.GetInstance().Exec<int>(() => Text.Length);
+            return Text.Length;
         }
 
         public void Remove(int begin, int end)
--- a/ui/wpf/UIcore/Window.cs	Sun Jan 22 11:48:50 2017 +0100
+++ b/ui/wpf/UIcore/Window.cs	Sun Jan 22 17:23:20 2017 +0100
@@ -85,14 +85,9 @@
             Closed += CloseEvent;
         }
 
-        public static MainWindow CreateMainWindow(String title, IntPtr uiobj)
-        {
-            return Application.GetInstance().Exec<MainWindow>(() => new MainWindow(title, uiobj));
-        }
-
         public void ShowWindow()
         {
-            Application.GetInstance().Exec(() => this.Show());
+            this.Show();
             Application.GetInstance().AddWindow(this);
         }
 
--- a/ui/wpf/UIwrapper/UIwrapper/container.cpp	Sun Jan 22 11:48:50 2017 +0100
+++ b/ui/wpf/UIwrapper/UIwrapper/container.cpp	Sun Jan 22 17:23:20 2017 +0100
@@ -8,21 +8,21 @@
 #using "UIcore.dll"
 
 UI_EXPORT void* __stdcall UIvbox(gcroot<UI::Container^> *parent) {
-	UI::BoxContainer ^vbox = UI::BoxContainer::CreateBoxContainer(*parent, UI::BoxOrientation::VERTICAL);
+	UI::BoxContainer ^vbox = gcnew UI::BoxContainer(*parent, UI::BoxOrientation::VERTICAL);
 	gcroot<UI::BoxContainer^> *container = new gcroot<UI::BoxContainer^>();
 	*container = vbox;
 	return container;
 }
 
 UI_EXPORT void* __stdcall UIhbox(gcroot<UI::Container^> *parent) {
-	UI::BoxContainer ^hbox = UI::BoxContainer::CreateBoxContainer(*parent, UI::BoxOrientation::HORIZONTAL);
+	UI::BoxContainer ^hbox = gcnew UI::BoxContainer(*parent, UI::BoxOrientation::HORIZONTAL);
 	gcroot<UI::BoxContainer^> *container = new gcroot<UI::BoxContainer^>();
 	*container = hbox;
 	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);
+	UI::GridContainer ^grid = gcnew UI::GridContainer(*parent, margin, columnspacing, rowspacing);
 	gcroot<UI::GridContainer^> *container = new gcroot<UI::GridContainer^>();
 	*container = grid;
 	return container;
--- a/ui/wpf/UIwrapper/UIwrapper/controls.cpp	Sun Jan 22 11:48:50 2017 +0100
+++ b/ui/wpf/UIwrapper/UIwrapper/controls.cpp	Sun Jan 22 17:23:20 2017 +0100
@@ -51,7 +51,7 @@
 	}
 	
 	gcroot<UI::TextArea^> *textarea = new gcroot<UI::TextArea^>();
-	*textarea = UI::TextArea::CreateTextArea(*container, str);
+	*textarea = gcnew UI::TextArea(*container, str, true);
 
 	return textarea;
 }
@@ -104,7 +104,7 @@
 	}
 
 	gcroot<UI::TextArea^> *textfield = new gcroot<UI::TextArea^>();
-	*textfield = UI::TextArea::CreateTextField(*container, str);
+	*textfield = gcnew UI::TextArea(*container, str, false);
 
 	return textfield;
 }
\ No newline at end of file
--- a/ui/wpf/UIwrapper/UIwrapper/toolkit.cpp	Sun Jan 22 11:48:50 2017 +0100
+++ b/ui/wpf/UIwrapper/UIwrapper/toolkit.cpp	Sun Jan 22 17:23:20 2017 +0100
@@ -7,6 +7,39 @@
 
 #using "UIcore.dll"
 
+static UIcallback startup_func;
+void              *startup_data;
+static UIcallback open_func;
+void              *open_data;
+static UIcallback exit_func;
+void              *exit_data;
+
+public ref class AppCallbacks : public UI::IApplicationCallbacks {
+public:
+	UIcallback startupFunc = NULL;
+	void       *startupData = NULL;
+	UIcallback openFunc = NULL;
+	void       *openData = NULL;
+	UIcallback exitFunc = NULL;
+	void       *exitData = NULL;
+
+	virtual void __clrcall OnStartup() {
+		if (startupFunc) {
+			startupFunc(NULL, startupData);
+		}
+	}
+	virtual void __clrcall OnOpen() {
+		if (openFunc) {
+			openFunc(NULL, openData);
+		}
+	}
+	virtual void __clrcall OnExit() {
+		if (exitFunc) {
+			exitFunc(NULL, exitData);
+		}
+	}
+};
+
 
 void* ObjectToPtr(Object ^obj) {
 	GCHandle handle = GCHandle::Alloc(obj);
@@ -58,8 +91,34 @@
 	app->Name = gcnew String(appname);
 }
 
+UI_EXPORT void __stdcall UIonstartup(UIcallback f, void *userdata) {
+	startup_func = f;
+	startup_data = userdata;
+}
+
+UI_EXPORT void __stdcall UIonopen(UIcallback f, void *userdata) {
+	open_func = f;
+	open_data = userdata;
+}
+
+UI_EXPORT void __stdcall UIonexit(UIcallback f, void *userdata) {
+	exit_func = f;
+	exit_data = userdata;
+}
+
 UI_EXPORT void __stdcall UImain() {
-	Thread ^thread = UI::Application::GetInstance()->Start();
+	AppCallbacks ^ac = gcnew AppCallbacks();
+	ac->startupFunc = startup_func;
+	ac->startupData = startup_data;
+	ac->openFunc = open_func;
+	ac->openData = open_data;
+	ac->exitFunc = exit_func;
+	ac->exitData = exit_data;
+	
+	UI::Application ^app = UI::Application::GetInstance();
+	app->callbacks = ac;
+
+	Thread ^thread = app->Start();
 	thread->Join();
 }
 
--- a/ui/wpf/UIwrapper/UIwrapper/toolkit.h	Sun Jan 22 11:48:50 2017 +0100
+++ b/ui/wpf/UIwrapper/UIwrapper/toolkit.h	Sun Jan 22 17:23:20 2017 +0100
@@ -39,3 +39,4 @@
 	EventWrapper(UIcallback callback, void *eventdata);
 	void Callback(Object ^sender, RoutedEventArgs ^e);
 };
+
--- a/ui/wpf/UIwrapper/UIwrapper/window.cpp	Sun Jan 22 11:48:50 2017 +0100
+++ b/ui/wpf/UIwrapper/UIwrapper/window.cpp	Sun Jan 22 17:23:20 2017 +0100
@@ -8,7 +8,7 @@
 #using "UIcore.dll"
 
 UI_EXPORT void* __stdcall UIwindow(char *title, void *uiobj) {
-	UI::MainWindow ^window = UI::MainWindow::CreateMainWindow(gcnew String(title), IntPtr(uiobj));
+	UI::MainWindow ^window = gcnew UI::MainWindow(gcnew String(title), IntPtr(uiobj));
 	gcroot<UI::MainWindow^> *ptr = new gcroot<UI::MainWindow^>();
 	*ptr = window;
 	return ptr;
--- a/ui/wpf/toolkit.c	Sun Jan 22 11:48:50 2017 +0100
+++ b/ui/wpf/toolkit.c	Sun Jan 22 17:23:20 2017 +0100
@@ -37,6 +37,18 @@
     UIinit(appname);
 }
 
+void ui_onstartup(ui_callback f, void *userdata) {
+    UIonstartup(f, userdata);
+}
+
+void ui_onopen(ui_callback f, void *userdata) {
+    UIonopen(f, userdata);
+}
+
+void ui_onexit(ui_callback f, void *userdata) {
+    UIonexit(f, userdata);
+}
+
 void ui_main() {
     UImain();
 }
--- a/ui/wpf/toolkit.h	Sun Jan 22 11:48:50 2017 +0100
+++ b/ui/wpf/toolkit.h	Sun Jan 22 17:23:20 2017 +0100
@@ -51,6 +51,10 @@
 typedef void(*UIcallback)(void*,void*);
 
 UI_IMPORT void __stdcall UIinit(char *appname);
+
+UI_IMPORT void __stdcall UIonstartup(ui_callback f, void *userdata);
+UI_IMPORT void __stdcall UIonopen(ui_callback f, void *userdata);
+UI_IMPORT void __stdcall UIonexit(ui_callback f, void *userdata);
 UI_IMPORT void __stdcall UImain();
 UI_IMPORT void __stdcall UIshow(UIWIDGET widget);
 

mercurial