added event handler for menus (WPF)

Tue, 27 Jan 2015 09:59:32 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 27 Jan 2015 09:59:32 +0100
changeset 82
0cdb8089a29f
parent 81
5eb765a7a793
child 83
a38aec91bd66

added event handler for menus (WPF)

application/main.c file | annotate | diff | comparison | revisions
ui/wpf/UIcore/Menu.cs file | annotate | diff | comparison | revisions
ui/wpf/UIcore/Toolkit.cs file | annotate | diff | comparison | revisions
ui/wpf/UIcore/Window.cs file | annotate | diff | comparison | revisions
ui/wpf/UIwrapper/UIwrapper.v12.suo file | annotate | diff | comparison | revisions
ui/wpf/UIwrapper/UIwrapper/menu.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/menu.c file | annotate | diff | comparison | revisions
ui/wpf/menu.h file | annotate | diff | comparison | revisions
ui/wpf/toolkit.h file | annotate | diff | comparison | revisions
--- a/application/main.c	Sun Jan 25 15:01:04 2015 +0100
+++ b/application/main.c	Tue Jan 27 09:59:32 2015 +0100
@@ -92,11 +92,24 @@
     //ui_select_tab(tabview, 0);
 }
 */
+
+void action_menu(UiEvent *event, void *data) {
+    printf("action_menu test: {%s}\n", data);
+    fflush(stdout);
+}
+
 int main(int argc, char** argv) { 
     ui_init("app1", argc, argv);
     
     ui_menu("File");
-    ui_menuitem("Hello", NULL, NULL);
+    ui_menuitem("Hello", action_menu, "hello");
+    ui_submenu("Submenu1");
+    ui_submenu("Submenu2");
+    ui_menuitem("item2", NULL, NULL);
+    ui_submenu_end();
+    ui_menuitem("item3", NULL, NULL);
+    ui_submenu_end();
+    ui_menuitem("item4", NULL, NULL);
     
     UiObject *obj = ui_window("Test", NULL);
     ui_show(obj);
--- a/ui/wpf/UIcore/Menu.cs	Sun Jan 25 15:01:04 2015 +0100
+++ b/ui/wpf/UIcore/Menu.cs	Tue Jan 27 09:59:32 2015 +0100
@@ -34,12 +34,12 @@
             current.Add(menu);
         }
 
-        public void EndMenu()
+        public void EndSubMenu()
         {
             current.Remove(current.Last());
         }
 
-        public void AddMenuItem(String label, Action action)
+        public void AddMenuItem(String label, Action<IntPtr> action)
         {
             if(current.Count != 0)
             {
@@ -48,14 +48,14 @@
             }
         }
 
-        public System.Windows.Controls.Menu CreateMenu()
+        public System.Windows.Controls.Menu CreateMenu(IntPtr uiobj)
         {
             System.Windows.Controls.Menu menu = new System.Windows.Controls.Menu();
             foreach (Menu m in Menus)
             {
                 System.Windows.Controls.MenuItem i = new System.Windows.Controls.MenuItem();
                 i.Header = m.Label;
-                m.AddItems(i);
+                m.AddItems(i, uiobj);
                 menu.Items.Add(i);
             }
             return menu;
@@ -64,7 +64,7 @@
 
     public interface IMenuItem
     {
-        void AddTo(System.Windows.Controls.MenuItem menu);
+        void AddTo(System.Windows.Controls.MenuItem menu, IntPtr uiobj);
     }
 
     public class Menu : IMenuItem
@@ -77,19 +77,19 @@
             Label = label;
         }
 
-        public void AddItems(System.Windows.Controls.MenuItem i)
+        public void AddItems(System.Windows.Controls.MenuItem i, IntPtr uiobj)
         {
             foreach (IMenuItem item in Items)
             {
-                item.AddTo(i);
+                item.AddTo(i, uiobj);
             }
         }
 
-        void IMenuItem.AddTo(System.Windows.Controls.MenuItem menu)
+        void IMenuItem.AddTo(System.Windows.Controls.MenuItem menu, IntPtr uiobj)
         {
             System.Windows.Controls.MenuItem i = new System.Windows.Controls.MenuItem();
             i.Header = Label;
-            AddItems(i);
+            AddItems(i, uiobj);
             menu.Items.Add(i);
         }
     }
@@ -97,17 +97,19 @@
     public class MenuItem : IMenuItem
     {
         String Label;
-        Action Action;
+        Action<IntPtr> Action;
 
-        public MenuItem(String label, Action action)
+        public MenuItem(String label, Action<IntPtr> action)
         {
             Label = label;
             Action = action;
         }
 
-        void IMenuItem.AddTo(System.Windows.Controls.MenuItem menu)
+        void IMenuItem.AddTo(System.Windows.Controls.MenuItem menu, IntPtr uiobj)
         {
             System.Windows.Controls.MenuItem i = new System.Windows.Controls.MenuItem();
+            EventCallback cb = new EventCallback(uiobj, Action);
+            i.Click += cb.Callback;
             i.Header = Label;
             menu.Items.Add(i);
         }
--- a/ui/wpf/UIcore/Toolkit.cs	Sun Jan 25 15:01:04 2015 +0100
+++ b/ui/wpf/UIcore/Toolkit.cs	Tue Jan 27 09:59:32 2015 +0100
@@ -4,11 +4,24 @@
 using System.Text;
 using System.Threading;
 using System.Threading.Tasks;
+using System.Windows;
 
 namespace UI
 {
-    public class Toolkit
+    public class EventCallback
     {
-        
+        public IntPtr Object;
+        public Action<IntPtr> Action;
+
+        public EventCallback(IntPtr uiobj, Action<IntPtr> action)
+        {
+            Object = uiobj;
+            Action = action;
+        }
+
+        public void Callback(object sender, RoutedEventArgs a)
+        {
+            Action.Invoke(Object);
+        }
     }
 }
--- a/ui/wpf/UIcore/Window.cs	Sun Jan 25 15:01:04 2015 +0100
+++ b/ui/wpf/UIcore/Window.cs	Tue Jan 27 09:59:32 2015 +0100
@@ -10,10 +10,13 @@
 namespace UI
 {
     public class MainWindow : Window
-    { 
-        public MainWindow(String title)
+    {
+        public IntPtr Object;
+        
+        public MainWindow(String title, IntPtr uiobj)
         {
             Title = title;
+            Object = uiobj;
             Width = 300;
             Height = 300;
 
@@ -21,7 +24,7 @@
             Application app = Application.GetInstance();
             if (!app.AppMenu.IsEmpty())
             {
-                System.Windows.Controls.Menu menu = app.AppMenu.CreateMenu();
+                System.Windows.Controls.Menu menu = app.AppMenu.CreateMenu(uiobj);
                 this.AddChild(menu);
             }
 
@@ -29,9 +32,9 @@
             Closed += CloseEvent;
         }
 
-        public static MainWindow CreateMainWindow(String title)
+        public static MainWindow CreateMainWindow(String title, IntPtr uiobj)
         {
-            return Application.GetInstance().Exec<MainWindow>(() => new MainWindow(title));
+            return Application.GetInstance().Exec<MainWindow>(() => new MainWindow(title, uiobj));
         }
 
         public void ShowWindow()
Binary file ui/wpf/UIwrapper/UIwrapper.v12.suo has changed
--- a/ui/wpf/UIwrapper/UIwrapper/menu.cpp	Sun Jan 25 15:01:04 2015 +0100
+++ b/ui/wpf/UIwrapper/UIwrapper/menu.cpp	Tue Jan 27 09:59:32 2015 +0100
@@ -10,8 +10,17 @@
 	UI::Application::GetInstance()->AppMenu->AddMenu(gcnew String(label));
 }
 
+UI_EXPORT void __stdcall UIsubmenu(char *label) {
+	UI::Application::GetInstance()->AppMenu->AddSubMenu(gcnew String(label));
+}
 
-UI_EXPORT void __stdcall UImenuitem(char *label, UIcallback f, void *userdata) {
-	EventWrapper ^e = gcnew EventWrapper(f, userdata);
+UI_EXPORT void __stdcall UIsubmenu_end() {
+	UI::Application::GetInstance()->AppMenu->EndSubMenu();
+}
+
+
+UI_EXPORT void __stdcall UImenuitem(char *label, UIcallback f, void *eventdata) {
+	EventWrapper ^e = gcnew EventWrapper(f, eventdata);
 	UI::Application::GetInstance()->AppMenu->AddMenuItem(gcnew String(label), e->GetAction());
 }
+
--- a/ui/wpf/UIwrapper/UIwrapper/toolkit.cpp	Sun Jan 25 15:01:04 2015 +0100
+++ b/ui/wpf/UIwrapper/UIwrapper/toolkit.cpp	Tue Jan 27 09:59:32 2015 +0100
@@ -23,18 +23,20 @@
 
 // EventWrapper
 
-EventWrapper::EventWrapper(UIcallback callback, void *userdata) {
+EventWrapper::EventWrapper(UIcallback callback, void *eventdata) {
 	this->callback = callback;
-	this->userdata = userdata;
-	action = gcnew Action(this, &EventWrapper::Callback);
+	this->userdata = eventdata;
+	action = gcnew Action<IntPtr>(this, &EventWrapper::Callback);
 }
 
-Action^ EventWrapper::GetAction() {
+Action<IntPtr>^ EventWrapper::GetAction() {
 	return action;
 }
 
-void EventWrapper::Callback() {
-	callback(NULL, NULL);
+void EventWrapper::Callback(IntPtr uiobj) {
+	if (callback) {
+		callback(uiobj.ToPointer(), userdata);
+	}
 }
 
 UI_EXPORT void __stdcall UIinit(char *appname) {
--- a/ui/wpf/UIwrapper/UIwrapper/toolkit.h	Sun Jan 25 15:01:04 2015 +0100
+++ b/ui/wpf/UIwrapper/UIwrapper/toolkit.h	Tue Jan 27 09:59:32 2015 +0100
@@ -17,12 +17,12 @@
 public ref class EventWrapper {
 	UIcallback callback = NULL;
 	void *userdata = NULL;
-	Action ^action;
+	Action<IntPtr> ^action;
 
 public:
 	EventWrapper(UIcallback callback, void *userdata);
 
-	Action^ GetAction();
+	Action<IntPtr>^ GetAction();
 
-	void Callback();
+	void Callback(IntPtr uiobj);
 };
--- a/ui/wpf/UIwrapper/UIwrapper/window.cpp	Sun Jan 25 15:01:04 2015 +0100
+++ b/ui/wpf/UIwrapper/UIwrapper/window.cpp	Tue Jan 27 09:59:32 2015 +0100
@@ -8,6 +8,6 @@
 #using "UIcore.dll"
 
 UI_EXPORT void* UIwindow(char *title, void *uiobj) {
-	UI::MainWindow ^window = UI::MainWindow::CreateMainWindow(gcnew String(title));
+	UI::MainWindow ^window = UI::MainWindow::CreateMainWindow(gcnew String(title), IntPtr(uiobj));
 	return ObjectToPtr(window);
 }
--- a/ui/wpf/menu.c	Sun Jan 25 15:01:04 2015 +0100
+++ b/ui/wpf/menu.c	Tue Jan 27 09:59:32 2015 +0100
@@ -37,13 +37,36 @@
 }
 
 void ui_submenu(char *label) {
-    
+    UIsubmenu(label);
 }
 
 void ui_submenu_end() {
-    
+    UIsubmenu_end();
 }
 
 void ui_menuitem(char *label, ui_callback f, void *userdata) {
-    UImenuitem(label, f, userdata);
+    ui_callback cb = NULL;
+    void *e = NULL;
+    if (f) {
+        UiEventData *event = malloc(sizeof(UiEventData));
+        event->obj = NULL;
+        event->user_data = userdata;
+        event->callback = f;
+        event->value = 0;
+        cb = (ui_callback)ui_menu_callback;
+        e = event;
+    }
+    
+    UImenuitem(label, cb, e);
 }
+
+
+void ui_menu_callback(UiObject *obj, UiEventData *e) {
+    UiEvent event;
+    event.obj = obj;
+    event.window = obj->window;
+    event.intval = 0;
+    event.eventdata = NULL;
+    event.document = obj->ctx->document;
+    e->callback(&event, e->user_data);
+}
--- a/ui/wpf/menu.h	Sun Jan 25 15:01:04 2015 +0100
+++ b/ui/wpf/menu.h	Tue Jan 27 09:59:32 2015 +0100
@@ -16,10 +16,14 @@
 #endif
 
 UI_IMPORT void __stdcall UImenu(char *label);
-
+UI_IMPORT void __stdcall UIsubmenu(char *label);
+UI_IMPORT void __stdcall UIsubmenu_end();
 UI_IMPORT void __stdcall UImenuitem(char *label, ui_callback f, void *udata);
 
 
+
+void ui_menu_callback(UiObject *obj, UiEventData *e);
+
 #ifdef	__cplusplus
 }
 #endif
--- a/ui/wpf/toolkit.h	Sun Jan 25 15:01:04 2015 +0100
+++ b/ui/wpf/toolkit.h	Tue Jan 27 09:59:32 2015 +0100
@@ -41,6 +41,13 @@
 #define UI_IMPORT __declspec(dllimport)
 __declspec(dllimport) int  __stdcall myfunc(char *str);
 
+typedef struct UiEventData {
+    UiObject    *obj;
+    ui_callback callback;
+    void        *user_data;
+    int         value;
+} UiEventData;
+
 UI_IMPORT void __stdcall UIinit(char *appname);
 UI_IMPORT void __stdcall UImain();
 UI_IMPORT void __stdcall UIshow(UIWIDGET widget);

mercurial