ui/winui/image.cpp

Thu, 14 Nov 2024 23:22:35 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 14 Nov 2024 23:22:35 +0100
changeset 85
44ebbb4c8a13
parent 76
641dcc79e0ef
permissions
-rw-r--r--

add support for secret store credentials when connecting to repositories, resolves #496

/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2024 Olaf Wintermann. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
*   1. Redistributions of source code must retain the above copyright
*      notice, this list of conditions and the following disclaimer.
*
*   2. Redistributions in binary form must reproduce the above copyright
*      notice, this list of conditions and the following disclaimer in the
*      documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#include "pch.h"

#include "image.h"

#include "toolkit.h"
#include "container.h"
#include "../common/object.h"
#include "../common/context.h"
#include "util.h"

using namespace winrt;
using namespace Microsoft::UI::Xaml;
using namespace Microsoft::UI::Xaml::Controls;
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;
}

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