ui/server/window.c

Thu, 11 Dec 2025 19:52:44 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 11 Dec 2025 19:52:44 +0100
changeset 985
93f07ccfd997
parent 984
2cf5e6d55013
child 986
6f7600c2b9e1
permissions
-rw-r--r--

add button (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 "../common/object.h"

#include <cx/buffer.h>

static UiObject* create_window(const char *title, UiBool simple, UiBool sidebar, UiBool splitview) {
    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.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, void *window_data) {
    return create_window(title, FALSE, FALSE, FALSE);
}

UiObject *ui_sidebar_window(const char *title, void *window_data) {
    return create_window(title, FALSE, TRUE, FALSE);
}

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

UiObject *ui_simple_window(const char *title, void *window_data) {
    return create_window(title, TRUE, FALSE, FALSE);
}


cxmutstr ui_window_serialize(UiWindow *w) {
    CxBuffer buf;
    cxBufferInit(&buf, NULL, 1024, NULL, CX_BUFFER_AUTO_EXTEND | CX_BUFFER_FREE_CONTENTS);
    
    cxBufferPutString(&buf, "{\"type\":");
    switch(w->type) {
        case UI_WINDOW_MAIN: cxBufferPutString(&buf, "\"window\""); break;
        case UI_WINDOW_SIMPLE: cxBufferPutString(&buf, "\"simple_window\""); break;
        case UI_WINDOW_SIDEBAR: cxBufferPutString(&buf, "\"sidebar_window\""); break;
        case UI_WINDOW_SPLITVIEW: cxBufferPutString(&buf, "\"splitview_window\""); break;
    }
    
    cxmutstr obj_id = w->widget.obj->id;
    cxmutstr id = w->widget.id;
    
    cxBufferPutString(&buf, ", \"obj\":\"");
    cxBufferWrite(obj_id.ptr, 1, obj_id.length, &buf);
    cxBufferPutString(&buf, "\",");
    
    cxBufferPutString(&buf, " \"id\":\"");
    cxBufferWrite(id.ptr, 1, id.length, &buf);
    cxBufferPutString(&buf, "\", ");
    
    if(w->title.ptr) {
        cxBufferPutString(&buf, " \"title\":\"");
        cxBufferWrite(w->title.ptr, 1, w->title.length, &buf);
        cxBufferPutString(&buf, "\"");
    }
    
    size_t numchildren = cxListSize(w->widget.children);
    if(numchildren > 0) {
        cxBufferPutString(&buf, ",\"children\":[");
        CxIterator i = cxListIterator(w->widget.children);
        cx_foreach(UiWidget *, child, i) {
            cxmutstr child_str = child->serialize(child);
            cxBufferWrite(child_str.ptr, 1, child_str.length, &buf);
            if(i.index+1 < numchildren) {
                cxBufferPut(&buf, ',');
            }
        }
        cxBufferPutString(&buf, "]");
    }
    
    cxBufferPutString(&buf, "}\n");
    
    return cx_mutstrn(buf.space, buf.size);
}

mercurial