#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;
}
UIElement uielm = image;
UiWidget* widget = new UiWidget(uielm);
ui_context_add_widget_destructor(current->ctx, widget);
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;
}
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 =
"file:///"L + 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;
}