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