ui/winui/image.cpp

Fri, 15 Nov 2024 21:23:16 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 15 Nov 2024 21:23:16 +0100
branch
newapi
changeset 384
8be7fd628fbc
parent 375
af087d0fad9b
permissions
-rw-r--r--

start UiObject ref with 0 and increases ref with ui_show

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

mercurial