Thu, 15 Feb 2024 21:33:08 +0100
port progressbar to new API (GTK)
219 | 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 "pch.h" | |
30 | ||
31 | #include "label.h" | |
32 | #include "text.h" | |
33 | #include "util.h" | |
34 | ||
35 | #include "toolkit.h" | |
36 | #include "container.h" | |
37 | #include "../common/object.h" | |
38 | #include "../common/context.h" | |
39 | ||
40 | using namespace winrt; | |
41 | using namespace Microsoft::UI::Xaml; | |
42 | using namespace Microsoft::UI::Xaml::Controls; | |
43 | using namespace Windows::UI::Xaml::Interop; | |
44 | using namespace winrt::Windows::Foundation; | |
45 | using namespace winrt::Microsoft::UI::Xaml::Controls::Primitives; | |
46 | ||
47 | UIWIDGET ui_label_create(UiObject* obj, UiLabelArgs args) { | |
48 | UiObject* current = uic_current_obj(obj); | |
49 | ||
50 | // create textbox and toolkit wrapper | |
51 | TextBlock label = TextBlock(); | |
52 | if (args.label) { | |
53 | wchar_t* wlabel = str2wstr(args.label, nullptr); | |
54 | label.Text(wlabel); | |
55 | free(wlabel); | |
56 | } | |
57 | ||
58 | UIElement elm = label; | |
59 | UiWidget* widget = new UiWidget(elm); | |
60 | ui_context_add_widget_destructor(current->ctx, widget); | |
61 | ||
62 | UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.value, args.varname, UI_VAR_STRING); | |
63 | if (var) { | |
64 | UiString* value = (UiString*)var->value; | |
65 | value->obj = widget; | |
66 | value->get = ui_label_get; | |
67 | value->set = ui_label_set; | |
68 | ||
69 | // listener for notifying observers | |
70 | // TODO: | |
71 | } | |
72 | ||
220
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
73 | // add label to current container |
219 | 74 | UI_APPLY_LAYOUT1(current, args); |
75 | ||
76 | current->container->Add(label, false); | |
77 | ||
78 | return widget; | |
79 | } | |
80 | ||
81 | UIWIDGET ui_llabel_create(UiObject* obj, UiLabelArgs args) { | |
82 | args.align = UI_ALIGN_LEFT; | |
83 | return ui_label_create(obj, args); | |
84 | } | |
85 | ||
86 | UIWIDGET ui_rlabel_create(UiObject* obj, UiLabelArgs args) { | |
87 | args.align = UI_ALIGN_RIGHT; | |
88 | return ui_label_create(obj, args); | |
89 | } | |
90 | ||
91 | ||
92 | ||
93 | char* ui_label_get(UiString* str) { | |
94 | UiWidget* widget = (UiWidget*)str->obj; | |
95 | TextBlock box = widget->uielement.as<TextBlock>(); | |
96 | std::wstring wstr(box.Text()); | |
231
e160bb392148
add getter/setter for ui number/string types
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
221
diff
changeset
|
97 | return ui_wstring_get(str, wstr); |
219 | 98 | } |
99 | ||
100 | void ui_label_set(UiString* str, const char* newvalue) { | |
101 | UiWidget* widget = (UiWidget*)str->obj; | |
245
06e655d15b17
fix usage of wrong type in ui_label_set (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
231
diff
changeset
|
102 | TextBlock box = widget->uielement.as<TextBlock>(); |
231
e160bb392148
add getter/setter for ui number/string types
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
221
diff
changeset
|
103 | box.Text(ui_wstring_set(str, newvalue)); |
219 | 104 | } |
220
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
105 | |
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
106 | |
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
107 | // -------------------- progressbar ------------------------- |
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
108 | |
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
109 | UIWIDGET ui_progressbar_create(UiObject* obj, UiProgressbarArgs args) { |
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
110 | UiObject* current = uic_current_obj(obj); |
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
111 | |
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
112 | // create textbox and toolkit wrapper |
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
113 | ProgressBar progressbar = ProgressBar(); |
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
114 | progressbar.Minimum(args.min); |
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
115 | progressbar.Maximum(args.max == 0 ? 100 : args.max); |
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
116 | if (args.width > 0) { |
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
117 | progressbar.Width(args.width); |
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
118 | } |
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
119 | |
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
120 | UIElement elm = progressbar; |
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
121 | UiWidget* widget = new UiWidget(elm); |
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
122 | ui_context_add_widget_destructor(current->ctx, widget); |
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
123 | |
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
124 | UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.value, args.varname, UI_VAR_DOUBLE); |
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
125 | if (var) { |
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
126 | UiDouble* value = (UiDouble*)var->value; |
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
127 | value->obj = widget; |
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
128 | value->get = ui_progressbar_get; |
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
129 | value->set = ui_progressbar_set; |
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
130 | |
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
131 | // listener for notifying observers |
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
132 | // TODO: |
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
133 | } |
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
134 | |
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
135 | // add button to current container |
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
136 | UI_APPLY_LAYOUT1(current, args); |
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
137 | |
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
138 | current->container->Add(progressbar, false); |
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
139 | |
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
140 | return widget; |
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
141 | } |
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
142 | |
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
143 | double ui_progressbar_get(UiDouble * d) { |
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
144 | UiWidget* widget = (UiWidget*)d->obj; |
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
145 | ProgressBar progressbar = widget->uielement.as<ProgressBar>(); |
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
146 | d->value = progressbar.Value(); |
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
147 | return d->value; |
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
148 | } |
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
149 | |
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
150 | void ui_progressbar_set(UiDouble * d, double newvalue) { |
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
151 | UiWidget* widget = (UiWidget*)d->obj; |
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
152 | ProgressBar progressbar = widget->uielement.as<ProgressBar>(); |
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
153 | d->value = newvalue; |
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
154 | progressbar.Value(newvalue); |
e377456302df
add progressbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
219
diff
changeset
|
155 | } |
221
a82d9beaa94a
add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
220
diff
changeset
|
156 | |
a82d9beaa94a
add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
220
diff
changeset
|
157 | UIWIDGET ui_progressspinner_create(UiObject* obj, UiProgressbarSpinnerArgs args) { |
a82d9beaa94a
add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
220
diff
changeset
|
158 | UiObject* current = uic_current_obj(obj); |
a82d9beaa94a
add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
220
diff
changeset
|
159 | |
a82d9beaa94a
add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
220
diff
changeset
|
160 | // create textbox and toolkit wrapper |
a82d9beaa94a
add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
220
diff
changeset
|
161 | ProgressRing spinner = ProgressRing(); |
a82d9beaa94a
add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
220
diff
changeset
|
162 | spinner.IsActive(false); |
a82d9beaa94a
add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
220
diff
changeset
|
163 | |
a82d9beaa94a
add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
220
diff
changeset
|
164 | UIElement elm = spinner; |
a82d9beaa94a
add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
220
diff
changeset
|
165 | UiWidget* widget = new UiWidget(elm); |
a82d9beaa94a
add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
220
diff
changeset
|
166 | ui_context_add_widget_destructor(current->ctx, widget); |
a82d9beaa94a
add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
220
diff
changeset
|
167 | |
a82d9beaa94a
add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
220
diff
changeset
|
168 | UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.value, args.varname, UI_VAR_DOUBLE); |
a82d9beaa94a
add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
220
diff
changeset
|
169 | if (var) { |
a82d9beaa94a
add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
220
diff
changeset
|
170 | UiInteger* value = (UiInteger*)var->value; |
a82d9beaa94a
add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
220
diff
changeset
|
171 | value->obj = widget; |
a82d9beaa94a
add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
220
diff
changeset
|
172 | value->get = ui_progressspinner_get; |
a82d9beaa94a
add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
220
diff
changeset
|
173 | value->set = ui_progressspinner_set; |
a82d9beaa94a
add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
220
diff
changeset
|
174 | |
a82d9beaa94a
add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
220
diff
changeset
|
175 | // listener for notifying observers |
a82d9beaa94a
add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
220
diff
changeset
|
176 | // TODO: |
a82d9beaa94a
add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
220
diff
changeset
|
177 | } |
a82d9beaa94a
add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
220
diff
changeset
|
178 | |
a82d9beaa94a
add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
220
diff
changeset
|
179 | // add button to current container |
a82d9beaa94a
add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
220
diff
changeset
|
180 | UI_APPLY_LAYOUT1(current, args); |
a82d9beaa94a
add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
220
diff
changeset
|
181 | |
a82d9beaa94a
add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
220
diff
changeset
|
182 | current->container->Add(spinner, false); |
a82d9beaa94a
add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
220
diff
changeset
|
183 | |
a82d9beaa94a
add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
220
diff
changeset
|
184 | return widget; |
a82d9beaa94a
add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
220
diff
changeset
|
185 | } |
a82d9beaa94a
add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
220
diff
changeset
|
186 | |
a82d9beaa94a
add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
220
diff
changeset
|
187 | int64_t ui_progressspinner_get(UiInteger * i) { |
a82d9beaa94a
add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
220
diff
changeset
|
188 | UiWidget* widget = (UiWidget*)i->obj; |
a82d9beaa94a
add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
220
diff
changeset
|
189 | ProgressRing spinner = widget->uielement.as<ProgressRing>(); |
a82d9beaa94a
add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
220
diff
changeset
|
190 | i->value = spinner.IsActive(); |
a82d9beaa94a
add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
220
diff
changeset
|
191 | return i->value; |
a82d9beaa94a
add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
220
diff
changeset
|
192 | } |
a82d9beaa94a
add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
220
diff
changeset
|
193 | |
a82d9beaa94a
add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
220
diff
changeset
|
194 | void ui_progressspinner_set(UiInteger * i, int64_t newvalue) { |
a82d9beaa94a
add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
220
diff
changeset
|
195 | UiWidget* widget = (UiWidget*)i->obj; |
a82d9beaa94a
add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
220
diff
changeset
|
196 | ProgressRing spinner = widget->uielement.as<ProgressRing>(); |
a82d9beaa94a
add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
220
diff
changeset
|
197 | i->value = newvalue != 0 ? 1 : 0; |
a82d9beaa94a
add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
220
diff
changeset
|
198 | spinner.IsActive(i->value); |
a82d9beaa94a
add semi-functional pathbar (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
220
diff
changeset
|
199 | } |