ui/win32/text.c

changeset 113
dde28a806552
equal deleted inserted replaced
112:c3f2f16fa4b8 113:dde28a806552
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2025 Olaf Wintermann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "text.h"
30
31 static W32WidgetClass textfield_widget_class = {
32 .eventproc = ui_textfield_eventproc,
33 .enable = w32_widget_default_enable,
34 .show = w32_widget_default_show,
35 .get_preferred_size = ui_textfield_get_preferred_size,
36 .destroy = w32_widget_default_destroy
37 };
38
39 UIWIDGET ui_textfield_create(UiObject *obj, UiTextFieldArgs *args) {
40 HINSTANCE hInstance = GetModuleHandle(NULL);
41 UiContainerPrivate *container = ui_obj_container(obj);
42 HWND parent = ui_container_get_parent(container);
43 UiLayout layout = UI_ARGS2LAYOUT(args);
44
45 int width = args->width >= 0 ? args->width : 100;
46
47 HWND hwnd = CreateWindowEx(
48 WS_EX_CLIENTEDGE,
49 "EDIT",
50 "",
51 WS_VISIBLE | WS_CHILD | ES_LEFT | ES_AUTOHSCROLL,
52 0, 0, width, 25,
53 parent,
54 (HMENU)0,
55 hInstance,
56 NULL);
57 ui_win32_set_ui_font(hwnd);
58
59 W32Widget *widget = w32_widget_create(&textfield_widget_class, hwnd, sizeof(UiTextField));
60 ui_container_add(container, widget, &layout);
61
62 UiTextField *textfield = (UiTextField*)widget;
63 textfield->width = width;
64 textfield->widget.var = uic_widget_var(obj->ctx, obj->ctx, args->value, args->varname, UI_VAR_STRING);
65 textfield->widget.callback = args->onchange;
66 textfield->widget.callbackdata = args->onchangedata;
67
68 if (textfield->widget.var) {
69 UiString *s = textfield->widget.var->value;
70
71 if (s->value.ptr) {
72 // TODO: set textfield string
73 }
74 s->obj = widget;
75 s->get = ui_textfield_get;
76 s->set = ui_textfield_set;
77 }
78
79 return widget;
80 }
81
82 W32Size ui_textfield_get_preferred_size(W32Widget *widget) {
83 UiTextField *textfield = (UiTextField *)widget;
84 return (W32Size){ .width = textfield->width, .height = 32};
85 }
86
87 void ui_textfield_eventproc(W32Widget *widget, HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
88
89 }
90
91 char* ui_textfield_get(UiString *s) {
92 UiTextField *textfield = s->obj;
93
94 if (s->value.free) {
95 s->value.free(s->value.ptr);
96 }
97
98 int len = GetWindowTextLength(textfield->widget.widget.hwnd);
99 s->value.ptr = calloc(len+1, 1);
100 GetWindowText(textfield->widget.widget.hwnd, s->value.ptr, len+1);
101
102 return s->value.ptr;
103 }
104
105 void ui_textfield_set(UiString *s, const char *value) {
106 UiTextField *textfield = s->obj;
107 if (s->value.free) {
108 s->value.free(s->value.ptr);
109 }
110 s->value.ptr = NULL;
111 SetWindowText(textfield->widget.widget.hwnd, value);
112 }

mercurial