UNIXworkcode

1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 * 4 * Copyright 2025 Olaf Wintermann. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include "window.h" 30 #include "container.h" 31 #include "widget.h" 32 33 #include "../common/object.h" 34 35 #include <cx/buffer.h> 36 37 static UiObject* create_window(const char *title, const char *type, UiBool sidebar) { 38 UiObject *obj = uic_object_new_toplevel(); 39 const CxAllocator *a = obj->ctx->allocator; 40 41 UiWindow *window = cxZalloc(a, sizeof(UiWindow)); 42 window->widget.obj = ui_create_server_object(obj->ctx); 43 window->widget.type = cx_str(type); 44 window->widget.children = cxLinkedListCreate(a, NULL, CX_STORE_POINTERS); 45 window->widget.serialize = (ui_serialize_func)ui_window_serialize; 46 window->widget.invisible = TRUE; 47 window->title = cx_strdup_a(a, title); 48 49 obj->widget = (UiWidget*)window; 50 uic_object_push_container(obj, ui_widget_container((UiWidget*)window)); 51 ui_reg_widget(obj->widget); 52 53 return obj; 54 } 55 56 UiObject *ui_window(const char *title) { 57 return create_window(title, "window", FALSE); 58 } 59 60 UiObject *ui_sidebar_window(const char *title) { 61 return create_window(title, "sidebar_window", FALSE); 62 } 63 64 UiObject *ui_splitview_window(const char *title, UiBool sidebar) { 65 return create_window(title, "splitview_window", sidebar); 66 } 67 68 UiObject *ui_simple_window(const char *title) { 69 return create_window(title, "simple_window", FALSE); 70 } 71 72 73 cxmutstr ui_window_serialize(UiWindow *w) { 74 CxBuffer buf; 75 cxBufferInit(&buf, NULL, 1024, NULL, CX_BUFFER_AUTO_EXTEND | CX_BUFFER_FREE_CONTENTS); 76 77 cxBufferPutString(&buf, "{"); 78 ui_serialize_type_obj_id(&w->widget, &buf); 79 80 if(w->title.ptr) { 81 cxBufferPutString(&buf, ",\"title\":\""); 82 cxBufferWrite(w->title.ptr, 1, w->title.length, &buf); 83 cxBufferPutString(&buf, "\""); 84 } 85 86 ui_serialize_children(&w->widget, &buf); 87 88 cxBufferPutString(&buf, "}\n"); 89 90 return cx_mutstrn(buf.space, buf.size); 91 } 92 93 94