implement imageviewer (WINUI) newapi

Wed, 30 Oct 2024 15:15:36 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 30 Oct 2024 15:15:36 +0100
branch
newapi
changeset 375
af087d0fad9b
parent 374
eae5d6623fd3
child 376
5578494147cb

implement imageviewer (WINUI)

ui/winui/image.cpp file | annotate | diff | comparison | revisions
ui/winui/image.h file | annotate | diff | comparison | revisions
ui/winui/winui.vcxproj file | annotate | diff | comparison | revisions
ui/winui/winui.vcxproj.filters file | annotate | diff | comparison | revisions
--- a/ui/winui/image.cpp	Wed Oct 30 12:23:52 2024 +0100
+++ b/ui/winui/image.cpp	Wed Oct 30 15:15:36 2024 +0100
@@ -34,6 +34,7 @@
 #include "container.h"
 #include "../common/object.h"
 #include "../common/context.h"
+#include "util.h"
 
 using namespace winrt;
 using namespace Microsoft::UI::Xaml;
@@ -41,11 +42,83 @@
 using namespace Windows::UI::Xaml::Interop;
 using namespace winrt::Windows::Foundation;
 using namespace winrt::Microsoft::UI::Xaml::Controls::Primitives;
+using namespace winrt::Microsoft::UI::Xaml::Media::Imaging;
+using namespace winrt::Microsoft::UI::Xaml::Media;
+
+UiImageSource::UiImageSource(winrt::Microsoft::UI::Xaml::Media::ImageSource& src) : imgsrc(src) {}
 
 UIEXPORT UIWIDGET ui_imageviewer_create(UiObject *obj, UiImageViewerArgs args) {
+    UiObject* current = uic_current_obj(obj);
+    
+    Image image = Image();
+    FrameworkElement elm = image;
+    if (args.scrollarea) {
+        ScrollViewer scroll = ScrollViewer();
+        scroll.Content(image);
+        elm = scroll;
+    }
+
+    // create toolkit wrapper object and register destructor
+    UIElement uielm = image;
+    UiWidget* widget = new UiWidget(uielm);
+    ui_context_add_widget_destructor(current->ctx, widget);
+
+    // bind variable
+    UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.value, args.varname, UI_VAR_GENERIC);
+    if (var) {
+        UiGeneric *value = (UiGeneric*)var->value;
+        value->obj = widget;
+        value->get = ui_image_get;
+        value->set = ui_image_set;
+    }
+
+    // add button to current container
+    UI_APPLY_LAYOUT1(current, args);
+
+    current->container->Add(elm, true);
+
+    return widget;
+}
+
+extern "C" void* ui_image_get(UiGeneric *g) {
+
+
     return NULL;
 }
 
-UIEXPORT int ui_image_load_file(UiGeneric *obj, const char *path) {
+extern "C" int ui_image_set(UiGeneric *g, void *data, const char *type) {
+    if(!type || strcmp(type, UI_IMAGE_OBJECT_TYPE)) {
+        return 1;
+    }
+
+    UiImageSource *imgdata = (UiImageSource*)data;
+    if (g->value) {
+        UiImageSource *prevData = (UiImageSource*)g->value;
+        delete prevData;
+    }
+    g->value = imgdata;
+
+    UiWidget* widget = (UiWidget*)g->obj;
+    Image image = widget->uielement.as<Image>();
+    image.Source(imgdata->imgsrc);
+
     return 0;
 }
+
+UIEXPORT int ui_image_load_file(UiGeneric *obj, const char *path) {
+    wchar_t* wpath = str2wstr(path, nullptr);
+    std::wstring wPath = wpath;
+    std::wstring uriPath = L"file:///" + wPath;
+    Uri uri{ uriPath };
+    
+    BitmapImage bitmapImage = BitmapImage();
+    bitmapImage.UriSource(uri);
+    ImageSource src = bitmapImage;
+
+    UiImageSource *imgdata = new UiImageSource(src);
+    obj->set(obj, imgdata, UI_IMAGE_OBJECT_TYPE);
+
+    free(wpath);
+
+    return 0;
+}
--- a/ui/winui/image.h	Wed Oct 30 12:23:52 2024 +0100
+++ b/ui/winui/image.h	Wed Oct 30 15:15:36 2024 +0100
@@ -31,3 +31,13 @@
 #include "../ui/toolkit.h"
 #include "../ui/image.h"
 
+class UiImageSource {
+public:
+    winrt::Microsoft::UI::Xaml::Media::ImageSource imgsrc { nullptr };
+
+    UiImageSource(winrt::Microsoft::UI::Xaml::Media::ImageSource& src);
+};
+
+
+extern "C" void* ui_image_get(UiGeneric *g);
+extern "C" int ui_image_set(UiGeneric *g, void *data, const char *type);
--- a/ui/winui/winui.vcxproj	Wed Oct 30 12:23:52 2024 +0100
+++ b/ui/winui/winui.vcxproj	Wed Oct 30 15:15:36 2024 +0100
@@ -119,6 +119,7 @@
     <ClInclude Include="..\ui\dnd.h" />
     <ClInclude Include="..\ui\entry.h" />
     <ClInclude Include="..\ui\graphics.h" />
+    <ClInclude Include="..\ui\icons.h" />
     <ClInclude Include="..\ui\image.h" />
     <ClInclude Include="..\ui\menu.h" />
     <ClInclude Include="..\ui\properties.h" />
--- a/ui/winui/winui.vcxproj.filters	Wed Oct 30 12:23:52 2024 +0100
+++ b/ui/winui/winui.vcxproj.filters	Wed Oct 30 15:15:36 2024 +0100
@@ -99,6 +99,9 @@
     <ClInclude Include="dnd.h" />
     <ClInclude Include="condvar.h" />
     <ClInclude Include="image.h" />
+    <ClInclude Include="..\ui\icons.h">
+      <Filter>public</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <Image Include="Assets\Wide310x150Logo.scale-200.png">

mercurial