ui/winui/text.cpp

Thu, 12 Oct 2023 16:03:35 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 12 Oct 2023 16:03:35 +0200
branch
newapi
changeset 213
7e39db525fd9
parent 205
b1ac0dd1d38b
child 219
527a66c0afb2
permissions
-rw-r--r--

add icons (WinUI3)

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2023 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 "text.h"

#include "../common/context.h"
#include "../common/object.h"

#include "util.h"
#include "container.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;


UIWIDGET ui_textfield_create(UiObject* obj, UiTextFieldArgs args) {
    UiObject* current = uic_current_obj(obj);

    // create textbox and toolkit wrapper
    TextBox textfield = TextBox();
    UIElement elm = textfield;
    UiWidget* widget = new UiWidget(elm);
    ui_context_add_widget_destructor(current->ctx, widget);

    UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.value, args.varname, UI_VAR_STRING);
    if (var) {
        UiString* value = (UiString*)var->value;
        value->obj = widget;
        value->get = ui_textfield_get;
        value->set = ui_textfield_set;

        // listener for notifying observers
        // TODO:
    }
    
    // add button to current container
    UI_APPLY_LAYOUT1(current, args);

    current->container->Add(textfield, false);

    return widget;
}

UIWIDGET ui_frameless_textfield_create(UiObject* obj, UiTextFieldArgs args) {
    return ui_textfield_create(obj, args);
}

UIWIDGET ui_passwordfield_create(UiObject* obj, UiTextFieldArgs args) {
    UiObject* current = uic_current_obj(obj);

    // create textbox and toolkit wrapper
    PasswordBox textfield = PasswordBox();
    UIElement elm = textfield;
    UiWidget* widget = new UiWidget(elm);
    ui_context_add_widget_destructor(current->ctx, widget);

    UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.value, args.varname, UI_VAR_STRING);
    if (var) {
        UiString* value = (UiString*)var->value;
        value->obj = widget;
        value->get = ui_passwordfield_get;
        value->set = ui_passwordfield_set;

        // listener for notifying observers
        // TODO:
    }

    // add button to current container
    UI_APPLY_LAYOUT1(current, args);

    current->container->Add(textfield, false);

    return widget;
}


// -------------------------- getter/setter for textfield UiString --------------------------

static char* tf_get(UiString* str, std::wstring &value) {
    if (str->value.ptr) {
        str->value.free(str->value.ptr);
    }

    str->value.ptr = wchar2utf8(value.c_str(), value.length());
    str->value.free = free;

    return str->value.ptr;
}

static std::wstring tf_set(UiString* str, const char* value) {
    if (str->value.ptr) {
        str->value.free(str->value.ptr);
    }

    str->value.ptr = _strdup(value);
    str->value.free = free;

    int len;
    wchar_t* wstr = str2wstr(value, &len);
    std::wstring s(wstr);
    free(wstr);

    return s;
}

extern "C" char* ui_textfield_get(UiString * str) {
    UiWidget* widget = (UiWidget*)str->obj;
    TextBox box = widget->uielement.as<TextBox>();
    std::wstring wstr(box.Text());
    return tf_get(str, wstr);
}

extern "C" void  ui_textfield_set(UiString * str, const char* newvalue) {
    UiWidget* widget = (UiWidget*)str->obj;
    TextBox box = widget->uielement.as<TextBox>();
    box.Text(tf_set(str, newvalue));
}


extern "C" char* ui_passwordfield_get(UiString * str) {
    UiWidget* widget = (UiWidget*)str->obj;
    PasswordBox box = widget->uielement.as<PasswordBox>();
    std::wstring wstr(box.Password());
    return tf_get(str, wstr);
}

extern "C" void  ui_passwordfield_set(UiString * str, const char* newvalue) {
    UiWidget* widget = (UiWidget*)str->obj;
    PasswordBox box = widget->uielement.as<PasswordBox>();
    box.Password(tf_set(str, newvalue));
}

mercurial