add textfield (win32)

Fri, 24 Oct 2025 15:44:31 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 24 Oct 2025 15:44:31 +0200
changeset 873
78853bfc47cf
parent 872
1e34a27e016d
child 874
33618ae398bd

add textfield (win32)

application/demo_bindings.c file | annotate | diff | comparison | revisions
application/main.c file | annotate | diff | comparison | revisions
ui/win32/objs.mk file | annotate | diff | comparison | revisions
ui/win32/text.c file | annotate | diff | comparison | revisions
ui/win32/text.h file | annotate | diff | comparison | revisions
--- a/application/demo_bindings.c	Thu Oct 23 21:22:05 2025 +0200
+++ b/application/demo_bindings.c	Fri Oct 24 15:44:31 2025 +0200
@@ -28,7 +28,11 @@
 
 #include "demo_bindings.h"
 
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 
+#ifndef UI_WIN32
 
 Document* document_create(int id) {
     Document* doc = ui_document_new(sizeof(Document));
@@ -111,6 +115,7 @@
     ui_show(obj);
 }
 
+#endif
 
 #ifndef UI_WIN32
 
--- a/application/main.c	Thu Oct 23 21:22:05 2025 +0200
+++ b/application/main.c	Fri Oct 24 15:44:31 2025 +0200
@@ -1170,6 +1170,8 @@
     ui_grid(obj, .margin = 10, .columnspacing = 10, .rowspacing = 10, .fill = TRUE) {
         ui_button(obj, .label = "Test", .hfill = TRUE, .hexpand = TRUE, .colspan = 3, .margin = 10);
         ui_newline(obj);
+        ui_textfield(obj, .varname = "text1", .hfill = TRUE, .hexpand = TRUE, .colspan = 2);
+        ui_newline(obj);
 
         ui_button(obj, .label = "Test 2-1");
         ui_button(obj, .label = "Test 2-2");
--- a/ui/win32/objs.mk	Thu Oct 23 21:22:05 2025 +0200
+++ b/ui/win32/objs.mk	Fri Oct 24 15:44:31 2025 +0200
@@ -37,6 +37,7 @@
 WIN32OBJ += container.obj
 WIN32OBJ += button.obj
 WIN32OBJ += grid.obj
+WIN32OBJ += text.obj
 
 TOOLKITOBJS += $(WIN32OBJ:%=$(WIN32_OBJPRE)%)
 TOOLKITSOURCE += $(WIN32OBJ:%.obj=win32/%.c)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/win32/text.c	Fri Oct 24 15:44:31 2025 +0200
@@ -0,0 +1,96 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2025 Olaf Wintermann. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "text.h"
+
+static W32WidgetClass button_widget_class = {
+    .eventproc = ui_textfield_eventproc,
+    .enable = w32_widget_default_enable,
+    .show = w32_widget_default_show,
+    .get_preferred_size = ui_textfield_get_preferred_size,
+    .destroy  = w32_widget_default_destroy
+};
+
+UIWIDGET ui_textfield_create(UiObject *obj, UiTextFieldArgs *args) {
+    HINSTANCE hInstance = GetModuleHandle(NULL);
+    UiContainerPrivate *container = ui_obj_container(obj);
+    HWND parent = ui_container_get_parent(container);
+    UiLayout layout = UI_ARGS2LAYOUT(args);
+
+    int width = args->width >= 0 ? args->width : 100;
+
+    HWND hwnd = CreateWindow(
+            "EDIT",
+            "",
+            WS_VISIBLE | WS_CHILD | WS_BORDER | ES_LEFT | ES_AUTOHSCROLL,
+            0, 0, width, 25,
+            parent,
+            (HMENU)0,
+            hInstance,
+            NULL);
+    ui_win32_set_ui_font(hwnd);
+
+    W32Widget *widget = w32_widget_create(&button_widget_class, hwnd, sizeof(UiTextField));
+    ui_container_add(container, widget, &layout);
+
+    UiTextField *textfield = (UiTextField*)widget;
+    textfield->width = width;
+    textfield->widget.var = uic_widget_var(obj->ctx, obj->ctx, args->value, args->varname, UI_VAR_STRING);
+    textfield->widget.callback = args->onchange;
+    textfield->widget.callbackdata = args->onchangedata;
+
+    if (textfield->widget.var) {
+        UiString *s = textfield->widget.var->value;
+
+        if (s->value.ptr) {
+            // TODO: set textfield string
+        }
+        s->obj = widget;
+        s->get = ui_textfield_get;
+        s->set = ui_textfield_set;
+    }
+
+    return widget;
+}
+
+W32Size ui_textfield_get_preferred_size(W32Widget *widget) {
+    UiTextField *textfield = (UiTextField *)widget;
+    return (W32Size){ .width = textfield->width, .height = 32};
+}
+
+void ui_textfield_eventproc(W32Widget *widget, HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
+
+}
+
+char* ui_textfield_get(UiString *s) {
+    return NULL; // TODO
+}
+
+void ui_textfield_set(UiString *s, const char *value) {
+    // TODO
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/win32/text.h	Fri Oct 24 15:44:31 2025 +0200
@@ -0,0 +1,47 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2025 Olaf Wintermann. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef TEXT_H
+#define TEXT_H
+
+#include "../ui/text.h"
+#include "container.h"
+#include "toolkit.h"
+
+typedef struct UiTextField {
+    UiWidget widget;
+    int width;
+} UiTextField;
+
+W32Size ui_textfield_get_preferred_size(W32Widget *widget);
+void ui_textfield_eventproc(W32Widget *widget, HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
+
+char* ui_textfield_get(UiString *s);
+void ui_textfield_set(UiString *s, const char *value);
+
+#endif /* TEXT_H */
\ No newline at end of file

mercurial