ui/winui/image.cpp

branch
newapi
changeset 375
af087d0fad9b
parent 374
eae5d6623fd3
--- 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;
+}

mercurial