ui/motif/window.c

Sun, 15 Dec 2024 11:59:30 +0100

author
Olaf Winermann <olaf.wintermann@gmail.com>
date
Sun, 15 Dec 2024 11:59:30 +0100
branch
newapi
changeset 416
89ad8467c39f
parent 406
0ebf9d7b23e8
permissions
-rw-r--r--

implement motif menu/menu item

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2024 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 <stdio.h>
#include <stdlib.h>

#include "toolkit.h"
#include "menu.h"
#include "toolbar.h"
#include "container.h"
#include "../ui/window.h"
#include "../common/context.h"

#include "Grid.h"

#include <cx/mempool.h>

static int nwindows = 0;

static int window_default_width = 600;
static int window_default_height = 500;

static void window_close_handler(Widget window, void *udata, void *cdata) {
    UiObject *obj = udata;
    uic_object_destroy(obj);
    
    nwindows--;
    if(nwindows == 0) {
        ui_exit_mainloop();
    }
}


static UiObject* create_window(const char *title, void *window_data, Boolean simple) {
    CxMempool *mp = cxBasicMempoolCreate(256);
    const CxAllocator *a = mp->allocator;
    UiObject *obj = cxCalloc(a, 1, sizeof(UiObject));
    obj->ctx = uic_context(obj, mp);
    obj->window = window_data;
    
    Arg args[16];
    int n = 0;
    XtSetArg(args[n], XmNtitle, title); n++;
    XtSetArg(args[n], XmNminWidth, 100); n++;
    XtSetArg(args[n], XmNminHeight, 50); n++;
    XtSetArg(args[n], XmNwidth, window_default_width); n++;
    XtSetArg(args[n], XmNheight, window_default_height); n++;
    
    Widget toplevel = XtAppCreateShell(
            ui_appname(),
            "mainwindow",
            //applicationShellWidgetClass,
            vendorShellWidgetClass,
            ui_motif_get_display(),
            args,
            n);
    
    Atom wm_delete_window;
    wm_delete_window = XmInternAtom(
            XtDisplay(toplevel),
            "WM_DELETE_WINDOW",
            0);
    XmAddWMProtocolCallback(
            toplevel,
            wm_delete_window,
            window_close_handler,
            obj);
    
    Widget window = XtVaCreateManagedWidget(
            title,
            xmMainWindowWidgetClass,
            toplevel,
            NULL);
    
    // menu
    if(!simple) {
        ui_create_menubar(obj, window);
    }
    
    // content frame
    n = 0;
    Widget frame = XmCreateFrame(window, "window_frame", args, n);
    XtManageChild(frame);
    
    Widget vbox = XtCreateManagedWidget("window_vbox", gridClass, frame, NULL, 0);
    UiContainerX *container = ui_box_container(obj, vbox, UI_BOX_VERTICAL);
    uic_object_push_container(obj, container);
    
    obj->widget = toplevel;
    nwindows++;
    return obj;
} 

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

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

mercurial