UNIXworkcode

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 label 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_wstring_get(str, wstr); 98 } 99 100 void ui_label_set(UiString* str, const char* newvalue) { 101 UiWidget* widget = (UiWidget*)str->obj; 102 TextBlock box = widget->uielement.as<TextBlock>(); 103 box.Text(ui_wstring_set(str, newvalue)); 104 } 105 106 107 // -------------------- progressbar ------------------------- 108 109 UIWIDGET ui_progressbar_create(UiObject* obj, UiProgressbarArgs args) { 110 UiObject* current = uic_current_obj(obj); 111 112 // create textbox and toolkit wrapper 113 ProgressBar progressbar = ProgressBar(); 114 progressbar.Minimum(args.min); 115 progressbar.Maximum(args.max == 0 ? 100 : args.max); 116 if (args.width > 0) { 117 progressbar.Width(args.width); 118 } 119 120 UIElement elm = progressbar; 121 UiWidget* widget = new UiWidget(elm); 122 ui_context_add_widget_destructor(current->ctx, widget); 123 124 UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.value, args.varname, UI_VAR_DOUBLE); 125 if (var) { 126 UiDouble* value = (UiDouble*)var->value; 127 value->obj = widget; 128 value->get = ui_progressbar_get; 129 value->set = ui_progressbar_set; 130 131 // listener for notifying observers 132 // TODO: 133 } 134 135 // add button to current container 136 UI_APPLY_LAYOUT1(current, args); 137 138 current->container->Add(progressbar, false); 139 140 return widget; 141 } 142 143 double ui_progressbar_get(UiDouble * d) { 144 UiWidget* widget = (UiWidget*)d->obj; 145 ProgressBar progressbar = widget->uielement.as<ProgressBar>(); 146 d->value = progressbar.Value(); 147 return d->value; 148 } 149 150 void ui_progressbar_set(UiDouble * d, double newvalue) { 151 UiWidget* widget = (UiWidget*)d->obj; 152 ProgressBar progressbar = widget->uielement.as<ProgressBar>(); 153 d->value = newvalue; 154 progressbar.Value(newvalue); 155 } 156 157 UIWIDGET ui_progressspinner_create(UiObject* obj, UiProgressbarSpinnerArgs args) { 158 UiObject* current = uic_current_obj(obj); 159 160 // create textbox and toolkit wrapper 161 ProgressRing spinner = ProgressRing(); 162 spinner.IsActive(false); 163 164 UIElement elm = spinner; 165 UiWidget* widget = new UiWidget(elm); 166 ui_context_add_widget_destructor(current->ctx, widget); 167 168 UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.value, args.varname, UI_VAR_DOUBLE); 169 if (var) { 170 UiInteger* value = (UiInteger*)var->value; 171 value->obj = widget; 172 value->get = ui_progressspinner_get; 173 value->set = ui_progressspinner_set; 174 175 // listener for notifying observers 176 // TODO: 177 } 178 179 // add button to current container 180 UI_APPLY_LAYOUT1(current, args); 181 182 current->container->Add(spinner, false); 183 184 return widget; 185 } 186 187 int64_t ui_progressspinner_get(UiInteger * i) { 188 UiWidget* widget = (UiWidget*)i->obj; 189 ProgressRing spinner = widget->uielement.as<ProgressRing>(); 190 i->value = spinner.IsActive(); 191 return i->value; 192 } 193 194 void ui_progressspinner_set(UiInteger * i, int64_t newvalue) { 195 UiWidget* widget = (UiWidget*)i->obj; 196 ProgressRing spinner = widget->uielement.as<ProgressRing>(); 197 i->value = newvalue != 0 ? 1 : 0; 198 spinner.IsActive(i->value); 199 } 200