ui/winui/image.cpp

Sun, 10 Nov 2024 15:30:46 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 10 Nov 2024 15:30:46 +0100
changeset 76
641dcc79e0ef
permissions
-rw-r--r--

update toolkit

76
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2024 Olaf Wintermann. All rights reserved.
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
641dcc79e0ef update toolkit
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
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include "pch.h"
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 #include "image.h"
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 #include "toolkit.h"
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 #include "container.h"
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 #include "../common/object.h"
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 #include "../common/context.h"
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 #include "util.h"
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 using namespace winrt;
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 using namespace Microsoft::UI::Xaml;
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 using namespace Microsoft::UI::Xaml::Controls;
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 using namespace Windows::UI::Xaml::Interop;
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 using namespace winrt::Windows::Foundation;
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 using namespace winrt::Microsoft::UI::Xaml::Controls::Primitives;
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 using namespace winrt::Microsoft::UI::Xaml::Media::Imaging;
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 using namespace winrt::Microsoft::UI::Xaml::Media;
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 UiImageSource::UiImageSource(winrt::Microsoft::UI::Xaml::Media::ImageSource& src) : imgsrc(src) {}
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 UIEXPORT UIWIDGET ui_imageviewer_create(UiObject *obj, UiImageViewerArgs args) {
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 UiObject* current = uic_current_obj(obj);
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 Image image = Image();
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 FrameworkElement elm = image;
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 if (args.scrollarea) {
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 ScrollViewer scroll = ScrollViewer();
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 scroll.Content(image);
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 elm = scroll;
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 }
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 // create toolkit wrapper object and register destructor
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 UIElement uielm = image;
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 UiWidget* widget = new UiWidget(uielm);
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 ui_context_add_widget_destructor(current->ctx, widget);
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 // bind variable
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.value, args.varname, UI_VAR_GENERIC);
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 if (var) {
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 UiGeneric *value = (UiGeneric*)var->value;
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 value->obj = widget;
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71 value->get = ui_image_get;
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 value->set = ui_image_set;
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73 }
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 // add button to current container
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 UI_APPLY_LAYOUT1(current, args);
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78 current->container->Add(elm, true);
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 return widget;
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 }
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 extern "C" void* ui_image_get(UiGeneric *g) {
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86 return NULL;
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87 }
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 extern "C" int ui_image_set(UiGeneric *g, void *data, const char *type) {
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 if(!type || strcmp(type, UI_IMAGE_OBJECT_TYPE)) {
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 return 1;
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 }
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94 UiImageSource *imgdata = (UiImageSource*)data;
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95 if (g->value) {
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96 UiImageSource *prevData = (UiImageSource*)g->value;
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97 delete prevData;
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98 }
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99 g->value = imgdata;
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101 UiWidget* widget = (UiWidget*)g->obj;
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102 Image image = widget->uielement.as<Image>();
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103 image.Source(imgdata->imgsrc);
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105 return 0;
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106 }
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108 UIEXPORT int ui_image_load_file(UiGeneric *obj, const char *path) {
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 wchar_t* wpath = str2wstr(path, nullptr);
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110 std::wstring wPath = wpath;
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111 std::wstring uriPath = L"file:///" + wPath;
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112 Uri uri{ uriPath };
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114 BitmapImage bitmapImage = BitmapImage();
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115 bitmapImage.UriSource(uri);
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
116 ImageSource src = bitmapImage;
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
117
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
118 UiImageSource *imgdata = new UiImageSource(src);
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119 obj->set(obj, imgdata, UI_IMAGE_OBJECT_TYPE);
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
120
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
121 free(wpath);
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123 return 0;
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
124 }

mercurial