ui/win32/button.c

changeset 112
c3f2f16fa4b8
parent 108
77254bd6dccb
child 113
dde28a806552
--- a/ui/win32/button.c	Sat Oct 04 14:54:25 2025 +0200
+++ b/ui/win32/button.c	Sun Oct 19 21:20:08 2025 +0200
@@ -27,7 +27,74 @@
  */
 
 #include "button.h"
+#include "widget.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <commctrl.h>
+
+static W32WidgetClass button_widget_class = {
+    .eventproc = ui_button_eventproc,
+    .enable = w32_widget_default_enable,
+    .show = w32_widget_default_show,
+    .get_preferred_size = ui_button_get_preferred_size,
+    .destroy  = w32_widget_default_destroy
+};
 
 UIWIDGET ui_button_create(UiObject *obj, UiButtonArgs *args) {
-    return NULL;
+    HINSTANCE hInstance = GetModuleHandle(NULL);
+    UiContainerPrivate *container = ui_obj_container(obj);
+    HWND parent = ui_container_get_parent(container);
+    UiLayout layout = UI_ARGS2LAYOUT(args);
+
+    HWND hwnd = CreateWindow(
+            "BUTTON",
+            args->label,
+            WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
+            0, 0, 100, 30,
+            parent,
+            (HMENU)0,
+            hInstance,
+            NULL);
+    ui_win32_set_ui_font(hwnd);
+
+    W32Widget *widget = w32_widget_create(&button_widget_class, hwnd, sizeof(UiWidget));
+    ui_container_add(container, widget, &layout);
+
+    UiWidget *btn = (UiWidget*)widget;
+    btn->obj = obj;
+    btn->callback = args->onclick;
+    btn->callbackdata = args->onclickdata;
+
+    return widget;
 }
+
+W32Size ui_button_get_preferred_size(W32Widget *widget) {
+    W32Size size;
+    size.width = 100;
+    size.height = 30;
+    SIZE sz;
+    if (Button_GetIdealSize(widget->hwnd, &sz)) {
+        size.width = sz.cx;
+        size.height = sz.cy;
+    }
+    return size;
+}
+
+void ui_button_eventproc(W32Widget *widget, HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
+    UiWidget *w = (UiWidget*)widget;
+
+    UiEvent e;
+    e.obj = w->obj;
+    e.document = e.obj->ctx->document;
+    e.window = e.obj->window;
+    e.eventdata = NULL;
+    e.eventdatatype = 0;
+    e.intval = 0;
+    e.set = ui_get_setop();
+
+    if (w->callback) {
+        w->callback(&e, w->callbackdata);
+    }
+}

mercurial