ui/winui/text.cpp

branch
newapi
changeset 192
bcacd00ea955
child 205
b1ac0dd1d38b
equal deleted inserted replaced
191:6113ed66d258 192:bcacd00ea955
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2023 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 #include "../common/context.h"
32 #include "../common/object.h"
33
34 #include "util.h"
35 #include "container.h"
36
37 using namespace winrt;
38 using namespace Microsoft::UI::Xaml;
39 using namespace Microsoft::UI::Xaml::Controls;
40 using namespace Windows::UI::Xaml::Interop;
41 using namespace winrt::Windows::Foundation;
42 using namespace winrt::Microsoft::UI::Xaml::Controls::Primitives;
43
44
45 UIWIDGET ui_textfield_create(UiObject* obj, UiTextFieldArgs args) {
46 UiObject* current = uic_current_obj(obj);
47
48 // create textbox and toolkit wrapper
49 TextBox textfield = TextBox();
50 UIElement elm = textfield;
51 UiWidget* widget = new UiWidget(elm);
52 ui_context_add_widget_destructor(current->ctx, widget);
53
54 UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.value, args.varname, UI_VAR_STRING);
55 if (var) {
56 UiString* value = (UiString*)var->value;
57 value->obj = widget;
58 value->get = ui_textfield_get;
59 value->set = ui_textfield_set;
60
61 // listener for notifying observers
62 // TODO:
63 }
64
65 // add button to current container
66 UI_APPLY_LAYOUT1(current, args);
67
68 current->container->Add(textfield, false);
69
70 return widget;
71 }
72
73 UIWIDGET ui_frameless_textfield_create(UiObject* obj, UiTextFieldArgs args) {
74 return ui_textfield_create(obj, args);
75 }
76
77 UIWIDGET ui_passwordfield_create(UiObject* obj, UiTextFieldArgs args) {
78 UiObject* current = uic_current_obj(obj);
79
80 // create textbox and toolkit wrapper
81 PasswordBox textfield = PasswordBox();
82 UIElement elm = textfield;
83 UiWidget* widget = new UiWidget(elm);
84 ui_context_add_widget_destructor(current->ctx, widget);
85
86 UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.value, args.varname, UI_VAR_STRING);
87 if (var) {
88 UiString* value = (UiString*)var->value;
89 value->obj = widget;
90 value->get = ui_passwordfield_get;
91 value->set = ui_passwordfield_set;
92
93 // listener for notifying observers
94 // TODO:
95 }
96
97 // add button to current container
98 UI_APPLY_LAYOUT1(current, args);
99
100 current->container->Add(textfield, false);
101
102 return widget;
103 }
104
105
106 // -------------------------- getter/setter for textfield UiString --------------------------
107
108 static char* tf_get(UiString* str, std::wstring &value) {
109 if (str->value.ptr) {
110 str->value.free(str->value.ptr);
111 }
112
113 str->value.ptr = wchar2utf8(value.c_str(), value.length());
114 str->value.free = free;
115
116 return str->value.ptr;
117 }
118
119 static std::wstring tf_set(UiString* str, const char* value) {
120 if (str->value.ptr) {
121 str->value.free(str->value.ptr);
122 }
123
124 str->value.ptr = _strdup(value);
125 str->value.free = free;
126
127 int len;
128 wchar_t* wstr = str2wstr(value, &len);
129 std::wstring s(wstr);
130 free(wstr);
131
132 return s;
133 }
134
135 extern "C" char* ui_textfield_get(UiString * str) {
136 UiWidget* widget = (UiWidget*)str->obj;
137 TextBox box = widget->uielement.as<TextBox>();
138 std::wstring wstr(box.Text());
139 return tf_get(str, wstr);
140 }
141
142 extern "C" void ui_textfield_set(UiString * str, const char* newvalue) {
143 UiWidget* widget = (UiWidget*)str->obj;
144 TextBox box = widget->uielement.as<TextBox>();
145 box.Text(tf_set(str, newvalue));
146 }
147
148
149 extern "C" char* ui_passwordfield_get(UiString * str) {
150 UiWidget* widget = (UiWidget*)str->obj;
151 PasswordBox box = widget->uielement.as<PasswordBox>();
152 std::wstring wstr(box.Password());
153 return tf_get(str, wstr);
154 }
155
156 extern "C" void ui_passwordfield_set(UiString * str, const char* newvalue) {
157 UiWidget* widget = (UiWidget*)str->obj;
158 PasswordBox box = widget->uielement.as<PasswordBox>();
159 box.Password(tf_set(str, newvalue));
160 }

mercurial