ui/win32/text.c

changeset 113
dde28a806552
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/win32/text.c	Mon Nov 10 21:52:51 2025 +0100
@@ -0,0 +1,112 @@
+/*
+ * 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 textfield_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 = CreateWindowEx(
+            WS_EX_CLIENTEDGE,
+            "EDIT",
+            "",
+            WS_VISIBLE | WS_CHILD | ES_LEFT | ES_AUTOHSCROLL,
+            0, 0, width, 25,
+            parent,
+            (HMENU)0,
+            hInstance,
+            NULL);
+    ui_win32_set_ui_font(hwnd);
+
+    W32Widget *widget = w32_widget_create(&textfield_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) {
+    UiTextField *textfield = s->obj;
+
+    if (s->value.free) {
+        s->value.free(s->value.ptr);
+    }
+
+    int len = GetWindowTextLength(textfield->widget.widget.hwnd);
+    s->value.ptr = calloc(len+1, 1);
+    GetWindowText(textfield->widget.widget.hwnd, s->value.ptr, len+1);
+
+    return s->value.ptr;
+}
+
+void ui_textfield_set(UiString *s, const char *value) {
+    UiTextField *textfield = s->obj;
+    if (s->value.free) {
+        s->value.free(s->value.ptr);
+    }
+    s->value.ptr = NULL;
+    SetWindowText(textfield->widget.widget.hwnd, value);
+}

mercurial