ui/winui/button.cpp

Sun, 01 Oct 2023 09:23:47 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 01 Oct 2023 09:23:47 +0200
branch
newapi
changeset 186
5db4979bf482
parent 184
8c9b4b28aaa9
child 187
24ce2c326d85
permissions
-rw-r--r--

implement new api style for ui_button (WinUI3)

184
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2023 Olaf Wintermann. All rights reserved.
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
8c9b4b28aaa9 implement ui_button (WinUI)
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
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 #include "button.h"
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 #include "util.h"
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 #include "container.h"
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 #include "../common/object.h"
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 #include "../common/context.h"
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 using namespace winrt;
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 using namespace Microsoft::UI::Xaml;
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 using namespace Microsoft::UI::Xaml::Controls;
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 using namespace Windows::UI::Xaml::Interop;
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 using namespace winrt::Windows::Foundation;
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43
186
5db4979bf482 implement new api style for ui_button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 184
diff changeset
44 UIWIDGET ui_button_create(UiObject* obj, UiButtonArgs args) {
184
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 UiObject* current = uic_current_obj(obj);
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 // create button with label
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 Button button = Button();
186
5db4979bf482 implement new api style for ui_button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 184
diff changeset
49 if (args.label) {
5db4979bf482 implement new api style for ui_button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 184
diff changeset
50 wchar_t* wlabel = str2wstr(args.label, nullptr);
184
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 button.Content(box_value(wlabel));
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 free(wlabel);
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 }
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 // create toolkit wrapper object and register destructor
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 UIElement elm = button;
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 UiWidget* widget = new UiWidget(elm);
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 ui_context_add_widget_destructor(current->ctx, widget);
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 // register callback
186
5db4979bf482 implement new api style for ui_button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 184
diff changeset
61 if (args.onclick) {
184
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 widget->obj = obj;
186
5db4979bf482 implement new api style for ui_button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 184
diff changeset
63 widget->event_func = (ui_eventfunc)args.onclick;
5db4979bf482 implement new api style for ui_button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 184
diff changeset
64 widget->event_data = args.onclickdata;
184
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 button.Click([widget](IInspectable const& sender, RoutedEventArgs) {
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 ui_callback cb = (ui_callback)widget->event_func;
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 UiEvent evt;
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 evt.obj = (UiObject*)widget->obj;
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 evt.window = evt.obj->window;
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71 evt.document = evt.obj->ctx->document;
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 evt.eventdata = nullptr;
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73 evt.intval = 0;
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 cb(&evt, widget->event_data);
186
5db4979bf482 implement new api style for ui_button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 184
diff changeset
75 });
184
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 }
186
5db4979bf482 implement new api style for ui_button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 184
diff changeset
77
184
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78 // add button to current container
186
5db4979bf482 implement new api style for ui_button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 184
diff changeset
79 UI_APPLY_LAYOUT1(current, args);
184
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 current->container->Add(button, false);
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82 return widget;
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 }
186
5db4979bf482 implement new api style for ui_button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 184
diff changeset
84
5db4979bf482 implement new api style for ui_button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 184
diff changeset
85 UIWIDGET ui_checkbox_create(UiObject* obj, UiToggleArgs args) {
5db4979bf482 implement new api style for ui_button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 184
diff changeset
86 return nullptr;
5db4979bf482 implement new api style for ui_button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 184
diff changeset
87 }
5db4979bf482 implement new api style for ui_button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 184
diff changeset
88
5db4979bf482 implement new api style for ui_button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 184
diff changeset
89 UIWIDGET ui_radiobutton_create(UiObject* obj, UiToggleArgs) {
5db4979bf482 implement new api style for ui_button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 184
diff changeset
90 return nullptr;
5db4979bf482 implement new api style for ui_button (WinUI3)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 184
diff changeset
91 }

mercurial