add nav buttons, update toolkit

Sun, 28 Jan 2024 16:31:34 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 28 Jan 2024 16:31:34 +0100
changeset 3
f154867f54dc
parent 2
fbdfaacc4182
child 4
31137432bea8

add nav buttons, update toolkit

application/application.c file | annotate | diff | comparison | revisions
application/application.h file | annotate | diff | comparison | revisions
application/main.c file | annotate | diff | comparison | revisions
application/window.c file | annotate | diff | comparison | revisions
ui/common/context.c file | annotate | diff | comparison | revisions
ui/common/document.c file | annotate | diff | comparison | revisions
ui/common/menu.c file | annotate | diff | comparison | revisions
ui/common/menu.h file | annotate | diff | comparison | revisions
ui/common/types.c file | annotate | diff | comparison | revisions
ui/ui/button.h file | annotate | diff | comparison | revisions
ui/ui/menu.h file | annotate | diff | comparison | revisions
ui/ui/toolkit.h file | annotate | diff | comparison | revisions
ui/winui/appmenu.cpp file | annotate | diff | comparison | revisions
ui/winui/button.cpp file | annotate | diff | comparison | revisions
ui/winui/label.cpp file | annotate | diff | comparison | revisions
ui/winui/pch.h file | annotate | diff | comparison | revisions
ui/winui/table.cpp file | annotate | diff | comparison | revisions
ui/winui/table.h file | annotate | diff | comparison | revisions
ui/winui/text.cpp file | annotate | diff | comparison | revisions
ui/winui/text.h file | annotate | diff | comparison | revisions
ui/winui/toolkit.cpp file | annotate | diff | comparison | revisions
--- a/application/application.c	Sat Jan 27 17:50:19 2024 +0100
+++ b/application/application.c	Sun Jan 28 16:31:34 2024 +0100
@@ -34,20 +34,45 @@
 #include "window.h"
 
 
+void application_init(void) {
+	application_create_menu();
+}
+
 
 void application_startup(UiEvent* event, void* data) {
-	application_create_menu();
+	UiObject* win = window_create();
 
-	UiObject* win = window_create();
+	DavApp* app = application_create_app_document();
+	UiContext* global = ui_global_context();
+	ui_attach_document(global, app);
+
 	ui_show(win);
 }
 
 void application_create_menu(void) {
+	// menu
 
+
+	// toolbar
+	ui_toolbar_item("Home", .label = "Home", .icon = "Home");
+
+    ui_toolbar_menu("Repo", .label = "Repository") {
+        ui_menu_itemlist(.varname = "repolist");
+    }
+
+	ui_toolbar_add_default("Home", UI_TOOLBAR_LEFT);
+	ui_toolbar_add_default("Repo", UI_TOOLBAR_LEFT);
 }
 
 
 DavApp* application_create_app_document(void) {
 	DavApp* doc = ui_document_new(sizeof(DavApp));
+	UiContext* ctx = ui_document_context(doc);
+	doc->repos = ui_list_new(ctx, "repolist");
+
+	ui_list_append(doc->repos, "test");
+	
+
+
 	return doc;
 }
--- a/application/application.h	Sat Jan 27 17:50:19 2024 +0100
+++ b/application/application.h	Sun Jan 28 16:31:34 2024 +0100
@@ -37,10 +37,13 @@
 
 typedef struct DavApp {
 	DavConfig *dav_config;
+	UiList* repos;
 
 } DavApp;
 
 
+void application_init(void);
+
 /*
  * startup callback for the ui framework
  */
--- a/application/main.c	Sat Jan 27 17:50:19 2024 +0100
+++ b/application/main.c	Sun Jan 28 16:31:34 2024 +0100
@@ -39,8 +39,10 @@
 
 int idav_main(int argc, char **argv) {
 	ui_init("idav", argc, argv);
+
+	application_init();
 	ui_onstartup(application_startup, NULL);
-
+	
 	ui_main();
 
 	return 0;
--- a/application/window.c	Sat Jan 27 17:50:19 2024 +0100
+++ b/application/window.c	Sun Jan 28 16:31:34 2024 +0100
@@ -35,8 +35,9 @@
 	UiObject* obj = ui_window("iDAV", NULL);
 
 	// navigation bar
-	ui_hbox(obj, .fill = UI_OFF) {
-		// TODO: go back/forward buttons
+	ui_hbox(obj, .fill = UI_OFF, .margin = 8) {
+		ui_button(obj, .icon = "Back");
+		ui_button(obj, .icon = "Forward");
 
 		ui_path_textfield(obj, .fill = UI_ON);
 	}
--- a/ui/common/context.c	Sat Jan 27 17:50:19 2024 +0100
+++ b/ui/common/context.c	Sun Jan 28 16:31:34 2024 +0100
@@ -314,11 +314,15 @@
             break;
         }
         case UI_VAR_LIST: {
-            UiList *f = fromvalue;
-            UiList *t = to->value;
-            if(!f->obj) break;
-            uic_list_copy(f, t);
-            t->update(t, -1);
+            // TODO: not sure how correct this is
+
+            UiVar tmp = *from;
+            *from = *to;
+            *to = tmp;
+
+            UiList* t = to->value;
+            ui_notify(t->observers, NULL);
+            
             break;
         }
         case UI_VAR_RANGE: {
--- a/ui/common/document.c	Sat Jan 27 17:50:19 2024 +0100
+++ b/ui/common/document.c	Sun Jan 28 16:31:34 2024 +0100
@@ -38,7 +38,9 @@
 static CxMap *documents;
 
 void uic_docmgr_init() {
-    documents = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 32);
+    if (!documents) {
+        documents = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 32);
+    }
 }
 
 void ui_set_document(UiObject *obj, void *document) {
@@ -80,7 +82,7 @@
 }
 
 void* ui_document_new(size_t size) {
-    CxMempool *mp = cxBasicMempoolCreate(256);
+    CxMempool *mp = cxMempoolCreate(256, NULL);
     const CxAllocator *a = mp->allocator;
     UiContext *ctx = cxCalloc(a, 1, sizeof(UiContext));
     ctx->attach_document = uic_context_attach_document;
--- a/ui/common/menu.c	Sat Jan 27 17:50:19 2024 +0100
+++ b/ui/common/menu.c	Sun Jan 28 16:31:34 2024 +0100
@@ -211,7 +211,7 @@
     UiMenuItemList* item = malloc(sizeof(UiMenuItemList));
     item->item.prev = NULL;
     item->item.next = NULL;
-    item->item.type = UI_MENU_ITEM_LIST;
+    item->item.type = UI_MENU_CHECKITEM_LIST;
     item->callback = args.onselect;
     item->userdata = args.onselectdata;
     item->varname = nl_strdup(args.varname);
@@ -227,7 +227,7 @@
     UiMenuItemList* item = malloc(sizeof(UiMenuItemList));
     item->item.prev = NULL;
     item->item.next = NULL;
-    item->item.type = UI_MENU_ITEM_LIST;
+    item->item.type = UI_MENU_RADIOITEM_LIST;
     item->callback = args.onselect;
     item->userdata = args.onselectdata;
     item->varname = nl_strdup(args.varname);
@@ -237,6 +237,10 @@
 
 
 void uic_add_menu_to_stack(UiMenu* menu) {
+    if (!current) {
+        current = cxLinkedListCreate(cxDefaultAllocator, NULL, CX_STORE_POINTERS);
+    }
+
     cxListInsert(current, 0, menu);
 }
 
--- a/ui/common/menu.h	Sat Jan 27 17:50:19 2024 +0100
+++ b/ui/common/menu.h	Sun Jan 28 16:31:34 2024 +0100
@@ -110,7 +110,7 @@
     UiMenuItemI    item;
     ui_callback    callback;
     void           *userdata;
-    const char* varname;
+    const char     *varname;
 };
 
 
--- a/ui/common/types.c	Sat Jan 27 17:50:19 2024 +0100
+++ b/ui/common/types.c	Sun Jan 28 16:31:34 2024 +0100
@@ -277,6 +277,66 @@
 }
 
 
+void ui_int_set(UiInteger* i, int64_t value) {
+    if (i && i->set) {
+        i->set(i, value);
+    }
+}
+
+int64_t ui_int_get(UiInteger* i) {
+    if (i) {
+        return i->get ? i->get(i) : i->value;
+    } else {
+        return 0;
+    }
+}
+
+void ui_double_set(UiDouble* d, double value) {
+    if (d && d->set) {
+        d->set(d, value);
+    }
+}
+
+double ui_double_get(UiDouble* d) {
+    if (d) {
+        return d->get ? d->get(d) : d->value;
+    }
+    else {
+        return 0;
+    }
+}
+
+void ui_string_set(UiString* s, const char* value) {
+    if (s && s->set) {
+        s->set(s, value);
+    }
+}
+
+char* ui_string_get(UiString* s) {
+    if (s) {
+        return s->get ? s->get(s) : s->value.ptr;
+    }
+    else {
+        return 0;
+    }
+}
+
+void ui_text_set(UiText* s, const char* value) {
+    if (s && s->set) {
+        s->set(s, value);
+    }
+}
+
+char* ui_text_get(UiText* s) {
+    if (s) {
+        return s->get ? s->get(s) : s->value.ptr;
+    }
+    else {
+        return 0;
+    }
+}
+
+
 // private functions
 void uic_int_copy(UiInteger *from, UiInteger *to) {
     to->get = from->get;
--- a/ui/ui/button.h	Sat Jan 27 17:50:19 2024 +0100
+++ b/ui/ui/button.h	Sun Jan 28 16:31:34 2024 +0100
@@ -44,6 +44,8 @@
 
     const char* label;
     const char* stockid;
+    const char* icon;
+    UiLabelType labeltype;
     ui_callback onclick;
     void* onclickdata;
 } UiButtonArgs;
@@ -57,6 +59,8 @@
 
     const char* label;
     const char* stockid;
+    const char* icon;
+    UiLabelType labeltype;
     UiInteger* value;
     const char* varname;
     ui_callback onchange;
--- a/ui/ui/menu.h	Sat Jan 27 17:50:19 2024 +0100
+++ b/ui/ui/menu.h	Sun Jan 28 16:31:34 2024 +0100
@@ -70,6 +70,9 @@
 #define ui_menuitem(...) ui_menuitem_create((UiMenuItemArgs){ __VA_ARGS__ })
 #define ui_menu_toggleitem(...) ui_menu_toggleitem_create((UiMenuToggleItemArgs){ __VA_ARGS__ })
 #define ui_menu_radioitem(...) ui_menu_radioitem_create((UiMenuToggleItemArgs){ __VA_ARGS__ })
+#define ui_menu_itemlist(...) ui_menu_itemlist_create((UiMenuItemListArgs) { __VA_ARGS__ } )
+#define ui_menu_togglelist(...) ui_menu_itemlist_create((UiMenuItemListArgs) { __VA_ARGS} )
+#define ui_menu_radiolist(...) ui_menu_itemlist_create((UiMenuItemListArgs) { __VA_ARGS} )
 
 UIEXPORT void ui_menu_create(const char* label);
 UIEXPORT void ui_menuitem_create(UiMenuItemArgs args);
@@ -97,7 +100,7 @@
 
 UIEXPORT void ui_menuitem_list_deprecated(UiList *items, ui_callback f, void *userdata);
 
-UIEXPORT void ui_menu_end(void);
+UIEXPORT void ui_menu_end(void); // TODO: private
 
 /*
  * widget menu functions
--- a/ui/ui/toolkit.h	Sat Jan 27 17:50:19 2024 +0100
+++ b/ui/ui/toolkit.h	Sun Jan 28 16:31:34 2024 +0100
@@ -69,8 +69,6 @@
 
 #elif UI_WINUI
 
-#include <Windows.h>
-
 #define UIEXPORT __declspec(dllexport) 
 
 #ifdef __cplusplus
@@ -170,9 +168,12 @@
 typedef struct UiTabbedPane UiTabbedPane;
 
 typedef enum UiTri UiTri;
+typedef enum UiLabelType UiLabelType;
 
 enum UiMouseEventType { UI_PRESS = 0, UI_PRESS2 };
 
+enum UiLabelType { UI_LABEL_DEFAULT, UI_LABEL_TEXT, UI_LABEL_ICON, UI_LABEL_TEXT_ICON };
+
 
   
 typedef void(*ui_callback)(UiEvent*, void*); /* event, user data */
@@ -405,6 +406,28 @@
 UIEXPORT UiText* ui_text_new(UiContext *ctx, char *name);
 UIEXPORT UiRange* ui_range_new(UiContext *ctx, char *name);
 
+#define ui_get(v) _Generic(v, \
+    UiInteger*: ui_int_get, \
+    UiDouble*: ui_double_get, \
+    UiString*: ui_string_get, \
+    UiText*:ui_text_get) (v)
+
+#define ui_set(v, n) _Generic(v, \
+    UiInteger*: ui_int_set, \
+    UiDouble*: ui_double_set, \
+    UiString*: ui_string_set, \
+    UiText*:ui_text_set) (v, n)
+
+UIEXPORT void ui_int_set(UiInteger *i, int64_t value);
+UIEXPORT int64_t ui_int_get(UiInteger *i);
+UIEXPORT void ui_double_set(UiDouble *d, double value);
+UIEXPORT double ui_double_get(UiDouble *d);
+UIEXPORT void ui_string_set(UiString *s, const char *value);
+UIEXPORT char* ui_string_get(UiString *s);
+UIEXPORT void ui_text_set(UiText *s, const char* value);
+UIEXPORT char* ui_text_get(UiText *s);
+
+
 UIEXPORT UiObserver* ui_observer_new(ui_callback f, void *data);
 UIEXPORT UiObserver* ui_obsvlist_add(UiObserver *list, UiObserver *observer);
 UIEXPORT UiObserver* ui_add_observer(UiObserver *list, ui_callback f, void *data);
--- a/ui/winui/appmenu.cpp	Sat Jan 27 17:50:19 2024 +0100
+++ b/ui/winui/appmenu.cpp	Sun Jan 28 16:31:34 2024 +0100
@@ -33,6 +33,9 @@
 #include <cx/linked_list.h>
 #include <cx/array_list.h>
 
+#include "../common/context.h"
+#include "../common/object.h"
+
 #include "util.h"
 
 
@@ -51,17 +54,15 @@
 static void add_checkitem_widget(winrt::Windows::Foundation::Collections::IVector<winrt::Microsoft::UI::Xaml::Controls::MenuFlyoutItemBase> parent, int i, UiMenuItemI* item, UiObject* obj);
 static void add_radioitem_widget(winrt::Windows::Foundation::Collections::IVector<winrt::Microsoft::UI::Xaml::Controls::MenuFlyoutItemBase> parent, int i, UiMenuItemI* item, UiObject* obj);
 static void add_menuitem_list_widget(winrt::Windows::Foundation::Collections::IVector<winrt::Microsoft::UI::Xaml::Controls::MenuFlyoutItemBase> parent, int i, UiMenuItemI* item, UiObject* obj);
-static void add_menucheckitem_list_widget(winrt::Windows::Foundation::Collections::IVector<winrt::Microsoft::UI::Xaml::Controls::MenuFlyoutItemBase> parent, int i, UiMenuItemI* item, UiObject* obj);
-static void add_menuradioitem_list_widget(winrt::Windows::Foundation::Collections::IVector<winrt::Microsoft::UI::Xaml::Controls::MenuFlyoutItemBase> parent, int i, UiMenuItemI* item, UiObject* obj);
 
 static ui_menu_add_f createMenuItem[] = {
     /* UI_MENU                 */ add_menu_widget,
     /* UI_MENU_ITEM            */ add_menuitem_widget,
     /* UI_MENU_CHECK_ITEM      */ add_checkitem_widget,
-    /* UI_MENU_RADIO_ITEM      */ NULL, // TODO
+    /* UI_MENU_RADIO_ITEM      */ add_radioitem_widget,
     /* UI_MENU_ITEM_LIST       */ add_menuitem_list_widget,
-    /* UI_MENU_CHECKITEM_LIST  */ NULL, // TODO
-    /* UI_MENU_RADIOITEM_LIST  */ NULL, // TODO
+    /* UI_MENU_CHECKITEM_LIST  */ add_menuitem_list_widget,
+    /* UI_MENU_RADIOITEM_LIST  */ add_menuitem_list_widget,
     /* UI_MENU_SEPARATOR       */ add_menuseparator_widget
 };
 
@@ -134,28 +135,124 @@
     parent.Append(mi);
 }
 
-static void add_menuitem_st_widget(winrt::Windows::Foundation::Collections::IVector<winrt::Microsoft::UI::Xaml::Controls::MenuFlyoutItemBase> parent, int i, UiMenuItemI* item, UiObject* obj) {
+static void add_menuseparator_widget(
+    winrt::Windows::Foundation::Collections::IVector<winrt::Microsoft::UI::Xaml::Controls::MenuFlyoutItemBase> parent,
+    int i,
+    UiMenuItemI* item,
+    UiObject* obj)
+{
 
 }
 
-static void add_menuseparator_widget(winrt::Windows::Foundation::Collections::IVector<winrt::Microsoft::UI::Xaml::Controls::MenuFlyoutItemBase> parent, int i, UiMenuItemI* item, UiObject* obj) {
+static void add_checkitem_widget(
+    winrt::Windows::Foundation::Collections::IVector<winrt::Microsoft::UI::Xaml::Controls::MenuFlyoutItemBase> parent,
+    int i,
+    UiMenuItemI* item,
+    UiObject* obj)
+{
+
+}
+
+static void add_radioitem_widget(
+    winrt::Windows::Foundation::Collections::IVector<winrt::Microsoft::UI::Xaml::Controls::MenuFlyoutItemBase> parent,
+    int i,
+    UiMenuItemI* item,
+    UiObject* obj)
+{
 
 }
 
-static void add_checkitem_widget(winrt::Windows::Foundation::Collections::IVector<winrt::Microsoft::UI::Xaml::Controls::MenuFlyoutItemBase> parent, int i, UiMenuItemI* item, UiObject* obj) {
+
+class UiMenuList {
+public:
+    winrt::Windows::Foundation::Collections::IVector<winrt::Microsoft::UI::Xaml::Controls::MenuFlyoutItemBase> parent = { nullptr };
+    UiMenuItemType type;
+    int prevSize = 0;
+    int insertPos = 0;
+    UiVar* var = nullptr;
+    ui_callback callback = nullptr;
+    void* userdata = nullptr;
+
+    UiMenuList() {
+
+    }
+
+    void updateItems() {
+        UiList* list = (UiList*)var->value;
 
+        // delete previous items
+        for (int i = 0; i < prevSize; i++) {
+            parent.RemoveAt(insertPos);
+        }
+        
+        // insert new items
+        int count = 0;
+        void* elm = list->first(list);
+        while (elm) {
+
+            MenuFlyoutItem mi = MenuFlyoutItem();
+            wchar_t* wlabel = str2wstr((char*)elm, NULL);
+            mi.Text(wlabel);
+            free(wlabel);
+
+            parent.InsertAt(insertPos + count, mi);
+
+            elm = list->next(list);
+            count++;
+        }
+
+        prevSize = count;
+    }
+};
+
+extern "C" void destroy_ui_menu_list(void* ptr) {
+    UiMenuList* ls = (UiMenuList*)ptr;
+    delete ls;
 }
 
-static void add_checkitemnv_widget(winrt::Windows::Foundation::Collections::IVector<winrt::Microsoft::UI::Xaml::Controls::MenuFlyoutItemBase> parent, int i, UiMenuItemI* item, UiObject* obj) {
+static void ui_context_add_menu_list_destructor(UiContext* ctx, UiMenuList* list) {
+    cxMempoolRegister(ctx->mp, list, destroy_ui_menu_list);
+}
 
+static void ui_menulist_update(UiEvent* event, void* userdata) {
+    UiMenuList* mlist = (UiMenuList*)userdata;
+    mlist->updateItems();
 }
 
-static void add_menuitem_list_widget(winrt::Windows::Foundation::Collections::IVector<winrt::Microsoft::UI::Xaml::Controls::MenuFlyoutItemBase> parent, int i, UiMenuItemI* item, UiObject* obj) {
+static void add_menuitem_list_widget(
+    winrt::Windows::Foundation::Collections::IVector<winrt::Microsoft::UI::Xaml::Controls::MenuFlyoutItemBase> parent,
+    int i,
+    UiMenuItemI* item,
+    UiObject* obj)
+{
+    UiMenuItemList* it = (UiMenuItemList*)item;
+    if (!it->varname) {
+        return;
+    }
+    
+    uint32_t size = parent.Size();
+    
+    UiVar* var = uic_create_var(ui_global_context(), it->varname, UI_VAR_LIST);
 
+    UiMenuList* mlist = new UiMenuList();
+    mlist->parent = parent;
+    mlist->callback = it->callback;
+    mlist->userdata = it->userdata;
+    mlist->prevSize = 0;
+    mlist->insertPos = size;
+    mlist->type = item->type;
+    mlist->var = var;
+    ui_context_add_menu_list_destructor(obj->ctx, mlist);
+
+    UiList* list = (UiList*)var->value;
+    list->observers = ui_add_observer(list->observers, ui_menulist_update, mlist);
+
+    mlist->updateItems();
 }
 
 
 
+
 winrt::Microsoft::UI::Xaml::Controls::MenuFlyout ui_create_menu_flyout(UiObject* obj, UiMenu* menudef) {
     MenuFlyout flyout = MenuFlyout();
 
--- a/ui/winui/button.cpp	Sat Jan 27 17:50:19 2024 +0100
+++ b/ui/winui/button.cpp	Sun Jan 28 16:31:34 2024 +0100
@@ -46,13 +46,39 @@
 
 
 
-static void set_button_label(ButtonBase button, const char* label, const char* stockid) {
-	if (label) {
+static void set_button_label(ButtonBase button, const char* label, const char* stockid, const char *icon, UiLabelType type) {
+	// TODO: stockid
+
+	if (type == UI_LABEL_ICON) {
+		label = NULL;
+	}
+	else if (type == UI_LABEL_TEXT) {
+		icon = NULL;
+	}
+
+	if (label && icon) {
+		StackPanel panel = StackPanel();
+		panel.Orientation(Orientation::Horizontal);
+		panel.Spacing(5);
+		
+		panel.Children().Append(ui_get_icon(icon));
+
+		wchar_t* wlabel = str2wstr(label, nullptr);
+		TextBlock label = TextBlock();
+		label.Text(wlabel);
+		panel.Children().Append(label);
+		free(wlabel);
+
+		button.Content(panel);
+	}
+	else if (label) {
 		wchar_t* wlabel = str2wstr(label, nullptr);
 		button.Content(box_value(wlabel));
 		free(wlabel);
 	}
-	// TODO: stockid
+	else if (icon) {
+		button.Content(ui_get_icon(icon));
+	}
 }
 
 
@@ -61,7 +87,7 @@
 
 	// create button with label
 	Button button = Button();
-	set_button_label(button, args.label, args.stockid);
+	set_button_label(button, args.label, args.stockid, args.icon, args.labeltype);
 
 	// create toolkit wrapper object and register destructor
 	UIElement elm = button;
@@ -149,7 +175,7 @@
 	UiObject* current = uic_current_obj(obj);
 
 	// set label
-	set_button_label(button, args.label, args.stockid);
+	set_button_label(button, args.label, args.stockid, args.icon, args.labeltype);
 	togglebutton_register_callback(button, obj, args);
 
 	// create toolkit wrapper object and register destructor
@@ -236,7 +262,7 @@
 	UiObject* current = uic_current_obj(obj);
 
 	// set label
-	set_button_label(button, args.label, args.stockid);
+	set_button_label(button, args.label, args.stockid, args.icon, args.labeltype);
 	togglebutton_register_callback(button, obj, args);
 
 	// create toolkit wrapper object and register destructor
--- a/ui/winui/label.cpp	Sat Jan 27 17:50:19 2024 +0100
+++ b/ui/winui/label.cpp	Sun Jan 28 16:31:34 2024 +0100
@@ -94,13 +94,13 @@
     UiWidget* widget = (UiWidget*)str->obj;
     TextBlock box = widget->uielement.as<TextBlock>();
     std::wstring wstr(box.Text());
-    return ui_string_get(str, wstr);
+    return ui_wstring_get(str, wstr);
 }
 
 void  ui_label_set(UiString* str, const char* newvalue) {
     UiWidget* widget = (UiWidget*)str->obj;
     TextBox box = widget->uielement.as<TextBox>();
-    box.Text(ui_string_set(str, newvalue));
+    box.Text(ui_wstring_set(str, newvalue));
 }
 
 
--- a/ui/winui/pch.h	Sat Jan 27 17:50:19 2024 +0100
+++ b/ui/winui/pch.h	Sun Jan 28 16:31:34 2024 +0100
@@ -30,7 +30,9 @@
 #include <winrt/Windows.ApplicationModel.DataTransfer.h>
 #include <wil/cppwinrt_helpers.h>
 #include <winrt/Microsoft.UI.Xaml.Input.h>
+#include <winrt/Microsoft.UI.Input.h>
 #include <winrt/Windows.UI.Core.h>
 #include <winrt/Windows.ApplicationModel.h>
 
+
 #include <winrt/Windows.Storage.Streams.h>
--- a/ui/winui/table.cpp	Sat Jan 27 17:50:19 2024 +0100
+++ b/ui/winui/table.cpp	Sun Jan 28 16:31:34 2024 +0100
@@ -59,6 +59,20 @@
 	// TODO:
 }
 
+static void textblock_set_str(TextBlock& t, const char* str) {
+	if (str) {
+		wchar_t* wstr = str2wstr(str, nullptr);
+		t.Text(winrt::hstring(wstr));
+		free(wstr);
+	}
+}
+
+static void textblock_set_int(TextBlock& t, int i) {
+	wchar_t buf[16];
+	swprintf(buf, 16, L"%d", i);
+	t.Text(winrt::hstring(buf));
+}
+
 UIEXPORT UIWIDGET ui_table_create(UiObject* obj, UiListArgs args) {
 	if (!args.model) {
 		return nullptr;
@@ -162,6 +176,9 @@
 	headerRowDef.Height(gl);
 	grid.RowDefinitions().Append(headerRowDef);
 
+	winrt::Windows::UI::Color borderColor = { 63, 0, 0, 0 };
+	SolidColorBrush borderBrush = SolidColorBrush(borderColor);
+
 
 	for (int i = 0; i < model->columns;i++) {
 		char* title = model->titles[i];
@@ -172,44 +189,83 @@
 		colDef.Width(gl);
 		grid.ColumnDefinitions().Append(colDef);
 
-		// add button
-		auto button = Button();
-		wchar_t* wlabel = str2wstr(title, nullptr);
-		button.Content(box_value(wlabel));
-		free(wlabel);
-
-		// some styling for the button
+		// header column border
+		Border headerBorder = Border();
 		Thickness border = { 0,0,1,0 };
-		CornerRadius corner = { 0,0,0,0 };
-		button.BorderThickness(border);
-		button.CornerRadius(corner);
+		headerBorder.BorderThickness(border);
+		headerBorder.BorderBrush(borderBrush);
+
+		// add text
+		auto hLabel = TextBlock();
+		textblock_set_str(hLabel, title);
+		Thickness cellpadding = { 10,4,4,4 };
+		hLabel.Padding(cellpadding);
+		hLabel.VerticalAlignment(VerticalAlignment::Stretch);
 
-		grid.SetColumn(button, i);
-		grid.SetRow(button, 0);
-		grid.Children().Append(button);
+		// event handler for highlighting and column resizing
+		headerBorder.PointerPressed(
+			winrt::Microsoft::UI::Xaml::Input::PointerEventHandler(
+				[=](IInspectable const& sender, winrt::Microsoft::UI::Xaml::Input::PointerRoutedEventArgs const& args) {
+					// the last column doesn't need resize capabilities
+					if (i + 1 < model->columns) {
+						double width = headerBorder.ActualWidth();
+						auto point = args.GetCurrentPoint(headerBorder);
+						auto position = point.Position();
+						if (position.X + 4 >= width) {
+							this->resize = true;
+							this->resizedCol = headerBorder;
+						}
+					}
+				})
+		);
+		headerBorder.PointerReleased(
+			winrt::Microsoft::UI::Xaml::Input::PointerEventHandler(
+				[=](IInspectable const& sender, winrt::Microsoft::UI::Xaml::Input::PointerRoutedEventArgs const& args) {
+					this->resize = false;
+				})
+		);
+		headerBorder.PointerMoved(
+			winrt::Microsoft::UI::Xaml::Input::PointerEventHandler(
+				[=](IInspectable const& sender, winrt::Microsoft::UI::Xaml::Input::PointerRoutedEventArgs const& args) {
+					if (this->resize) {
+						auto point = args.GetCurrentPoint(this->resizedCol);
+						auto position = point.Position();
+						if (position.X > 1) {
+							this->resizedCol.Width(position.X);
+						}
+					}
+				})
+		);
+		headerBorder.PointerEntered(
+			winrt::Microsoft::UI::Xaml::Input::PointerEventHandler(
+				[=](IInspectable const& sender, winrt::Microsoft::UI::Xaml::Input::PointerRoutedEventArgs const& args) {
+					// TODO: background
+				})
+		);
+		headerBorder.PointerExited(
+			winrt::Microsoft::UI::Xaml::Input::PointerEventHandler(
+				[=](IInspectable const& sender, winrt::Microsoft::UI::Xaml::Input::PointerRoutedEventArgs const& args) {
+					// TODO: background
+				})
+		);
+
+
+
+		// add controls
+		headerBorder.Child(hLabel);
+
+		grid.SetColumn(headerBorder, i);
+		grid.SetRow(headerBorder, 0);
+		grid.Children().Append(headerBorder);
 
 		UiTableColumn h;
-		h.header = button;
+		h.header = headerBorder;
 		header.push_back(h);
 	}
 
 	maxrows = 1;
 }
 
-static void textblock_set_str(TextBlock &t, const char *str) {
-	if (str) {
-		wchar_t* wstr = str2wstr(str, nullptr);
-		t.Text(winrt::hstring(wstr));
-		free(wstr);
-	}
-}
-
-static void textblock_set_int(TextBlock& t, int i) {
-	wchar_t buf[16];
-	swprintf(buf, 16, L"%d", i);
-	t.Text(winrt::hstring(buf));
-}
-
 static ULONG64 getsystime() {
 	SYSTEMTIME st;
 	GetSystemTime(&st);
--- a/ui/winui/table.h	Sat Jan 27 17:50:19 2024 +0100
+++ b/ui/winui/table.h	Sun Jan 28 16:31:34 2024 +0100
@@ -36,7 +36,7 @@
 
 
 typedef struct UiTableColumn {
-	winrt::Microsoft::UI::Xaml::Controls::Button header;
+	winrt::Microsoft::UI::Xaml::Controls::Border header;
 
 } UiTableColumn;
 
@@ -47,6 +47,10 @@
 	winrt::Microsoft::UI::Xaml::Media::SolidColorBrush highlightBrush;
 	winrt::Microsoft::UI::Xaml::Media::SolidColorBrush selectedBrush;
 	winrt::Microsoft::UI::Xaml::Media::SolidColorBrush selectedBorderBrush;
+
+	winrt::Microsoft::UI::Xaml::Controls::Border resizedCol{ nullptr };
+	bool resize = false;
+
 	UiObject* obj;
 	ui_callback onactivate;
 	void* onactivatedata;
--- a/ui/winui/text.cpp	Sat Jan 27 17:50:19 2024 +0100
+++ b/ui/winui/text.cpp	Sun Jan 28 16:31:34 2024 +0100
@@ -114,7 +114,7 @@
 
 // -------------------------- getter/setter for textfield UiString --------------------------
 
-char* ui_string_get(UiString* str, std::wstring &value) {
+char* ui_wstring_get(UiString* str, std::wstring &value) {
     if (str->value.ptr) {
         str->value.free(str->value.ptr);
     }
@@ -125,7 +125,7 @@
     return str->value.ptr;
 }
 
-std::wstring ui_string_set(UiString* str, const char* value) {
+std::wstring ui_wstring_set(UiString* str, const char* value) {
     if (str->value.ptr) {
         str->value.free(str->value.ptr);
     }
@@ -145,13 +145,13 @@
     UiWidget* widget = (UiWidget*)str->obj;
     TextBox box = widget->uielement.as<TextBox>();
     std::wstring wstr(box.Text());
-    return ui_string_get(str, wstr);
+    return ui_wstring_get(str, wstr);
 }
 
 void  ui_textfield_set(UiString * str, const char* newvalue) {
     UiWidget* widget = (UiWidget*)str->obj;
     TextBox box = widget->uielement.as<TextBox>();
-    box.Text(ui_string_set(str, newvalue));
+    box.Text(ui_wstring_set(str, newvalue));
 }
 
 
@@ -159,13 +159,13 @@
     UiWidget* widget = (UiWidget*)str->obj;
     PasswordBox box = widget->uielement.as<PasswordBox>();
     std::wstring wstr(box.Password());
-    return ui_string_get(str, wstr);
+    return ui_wstring_get(str, wstr);
 }
 
 void  ui_passwordfield_set(UiString * str, const char* newvalue) {
     UiWidget* widget = (UiWidget*)str->obj;
     PasswordBox box = widget->uielement.as<PasswordBox>();
-    box.Password(ui_string_set(str, newvalue));
+    box.Password(ui_wstring_set(str, newvalue));
 }
 
 
@@ -304,13 +304,13 @@
     UiPathTextField* widget = (UiPathTextField*)str->obj;
     TextBox box = widget->textbox;
     std::wstring wstr(box.Text());
-    return ui_string_get(str, wstr);
+    return ui_wstring_get(str, wstr);
 }
 
 void  ui_path_textfield_set(UiString* str, const char* newvalue) {
     UiPathTextField* widget = (UiPathTextField*)str->obj;
     TextBox box = widget->textbox;
-    box.Text(ui_string_set(str, newvalue));
+    box.Text(ui_wstring_set(str, newvalue));
     ui_pathtextfield_update(widget, newvalue);
 }
 
--- a/ui/winui/text.h	Sat Jan 27 17:50:19 2024 +0100
+++ b/ui/winui/text.h	Sun Jan 28 16:31:34 2024 +0100
@@ -60,8 +60,8 @@
     void* ondropdata;
 };
 
-char* ui_string_get(UiString* str, std::wstring& value);
-std::wstring ui_string_set(UiString* str, const char* value);
+char* ui_wstring_get(UiString* str, std::wstring& value);
+std::wstring ui_wstring_set(UiString* str, const char* value);
 
 extern "C" char* ui_textfield_get(UiString *str);
 extern "C" void  ui_textfield_set(UiString *str, const char *newvalue);
--- a/ui/winui/toolkit.cpp	Sat Jan 27 17:50:19 2024 +0100
+++ b/ui/winui/toolkit.cpp	Sun Jan 28 16:31:34 2024 +0100
@@ -34,6 +34,7 @@
 #include <cx/mempool.h>
 
 #include "../common/context.h"
+#include "../common/document.h"
 #include "../common/toolbar.h"
 
 #include "icons.h"
@@ -157,6 +158,8 @@
 
 	//ui_appsdk_bootstrap();
 
+	uic_init_global_context();
+	uic_docmgr_init();
 	uic_toolbar_init();
 }
 

mercurial