ui/winui/button.cpp

Sun, 24 Sep 2023 12:03:57 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 24 Sep 2023 12:03:57 +0200
branch
newapi
changeset 185
4a8b1a748f09
parent 184
8c9b4b28aaa9
child 186
5db4979bf482
permissions
-rw-r--r--

implement grid container (WinUI)

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
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 UIWIDGET ui_button(UiObject* obj, char* label, ui_callback f, void* data) {
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();
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 if (label) {
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 wchar_t *wlabel = str2wstr(label, nullptr);
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
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 if (f) {
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 widget->obj = obj;
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 widget->event_func = (ui_eventfunc)f;
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 widget->event_data = data;
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);
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 } );
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 }
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78 // add button to current container
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79 current->container->Add(button, false);
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 return widget;
8c9b4b28aaa9 implement ui_button (WinUI)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82 }

mercurial