ui/server/window.c

Fri, 12 Dec 2025 12:00:34 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 12 Dec 2025 12:00:34 +0100
changeset 991
ab3125bd8b5f
parent 990
38a48b756d80
permissions
-rw-r--r--

move some common serialization code to functions (Server)

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2025 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 "window.h"
#include "container.h"
#include "widget.h"

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

#include <cx/buffer.h>

static UiObject* create_window(const char *title, const char *type, UiBool sidebar) {
    UiObject *obj = uic_object_new_toplevel();
    const CxAllocator *a = obj->ctx->allocator;
    
    UiWindow *window = cxZalloc(a, sizeof(UiWindow));
    window->widget.obj = ui_create_server_object(obj->ctx);
    window->widget.type = cx_str(type);
    window->widget.children = cxLinkedListCreate(a, NULL, CX_STORE_POINTERS);
    window->widget.serialize = (ui_serialize_func)ui_window_serialize;
    window->widget.invisible = TRUE;
    window->title = cx_strdup_a(a, title);
    
    obj->widget = (UiWidget*)window;
    uic_object_push_container(obj, ui_widget_container((UiWidget*)window));
    ui_reg_widget(obj->widget);
    
    return obj;
}

UiObject *ui_window(const char *title) {
    return create_window(title, "window", FALSE);
}

UiObject *ui_sidebar_window(const char *title) {
    return create_window(title, "sidebar_window", FALSE);
}

UiObject *ui_splitview_window(const char *title, UiBool sidebar) {
    return create_window(title, "splitview_window", sidebar);
}

UiObject *ui_simple_window(const char *title) {
    return create_window(title, "simple_window", FALSE);
}


cxmutstr ui_window_serialize(UiWindow *w) {
    CxBuffer buf;
    cxBufferInit(&buf, NULL, 1024, NULL, CX_BUFFER_AUTO_EXTEND | CX_BUFFER_FREE_CONTENTS);
    
    cxBufferPutString(&buf, "{");
    ui_serialize_type_obj_id(&w->widget, &buf);
    
    if(w->title.ptr) {
        cxBufferPutString(&buf, ",\"title\":\"");
        cxBufferWrite(w->title.ptr, 1, w->title.length, &buf);
        cxBufferPutString(&buf, "\"");
    }
    
    ui_serialize_children(&w->widget, &buf);
    
    cxBufferPutString(&buf, "}\n");
    
    return cx_mutstrn(buf.space, buf.size);
}

mercurial