|
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 |
|
73 // add button to current container |
|
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()); |
|
97 return ui_string_get(str, wstr); |
|
98 } |
|
99 |
|
100 void ui_label_set(UiString* str, const char* newvalue) { |
|
101 UiWidget* widget = (UiWidget*)str->obj; |
|
102 TextBox box = widget->uielement.as<TextBox>(); |
|
103 box.Text(ui_string_set(str, newvalue)); |
|
104 } |