ui/cocoa/container.m

Sun, 29 Sep 2024 18:51:03 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 29 Sep 2024 18:51:03 +0200
branch
newapi
changeset 315
144c2b4683cb
parent 51
42506e19eb6b
permissions
-rw-r--r--

use GtkFileDialog on newer gtk4 versions

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

#import <stdio.h>
#import <stdlib.h>

#import "container.h"


UiContainer* ui_window_container(UiObject *obj, NSWindow *window) {
    UiContainer *ct = ucx_mempool_malloc(
                                         obj->ctx->mempool,
                                         sizeof(UiContainer));
    ct->widget = [window contentView];
    ct->add = ui_container_add;
    ct->getframe = ui_container_getframe;
    return ct;
}

UIWIDGET ui_sidebar(UiObject *obj) {
    UiContainer *ct = uic_get_current_container(obj);
    NSRect frame = ct->getframe(ct);
    
    // create and add views
    NSSplitView *splitview = [[NSSplitView alloc] initWithFrame:frame];
    [splitview setVertical:YES];
    [splitview setDividerStyle:NSSplitViewDividerStyleThin];
    ct->add(ct, splitview);
    
    NSRect lframe;
    lframe.origin.x = 0;
    lframe.origin.y = 0;
    lframe.size.width = 200;
    lframe.size.height = frame.size.height;
    
    NSRect rframe;
    rframe.origin.x = 0;
    rframe.origin.y = 0;
    rframe.size.width = frame.size.width - 201;
    rframe.size.height = frame.size.height;
    
    NSView *sidebar = [[NSView alloc]initWithFrame:lframe];
    NSView *contentarea = [[NSView alloc]initWithFrame:rframe];
    
    [splitview addSubview:sidebar];
    [splitview addSubview:contentarea];
    
    // add ui objects for the sidebar and contentarea
    // the sidebar is added last, so that new views are added first to it
    UiObject *left = uic_object_new(obj, sidebar);
    UiContainer *ct1 = ucx_mempool_malloc(
            obj->ctx->mempool,
            sizeof(UiContainer));
    ct1->widget = sidebar;
    ct1->add = ui_container_add;
    ct1->getframe = ui_container_getframe;
    left->container = ct1;
    
    UiObject *right = uic_object_new(obj, sidebar);
    UiContainer *ct2 = ucx_mempool_malloc(
            obj->ctx->mempool,
            sizeof(UiContainer));
    ct2->widget = contentarea;
    ct2->add = ui_container_add;
    ct2->getframe = ui_container_getframe;
    right->container = ct2;
    
    uic_obj_add(obj, right);
    uic_obj_add(obj, left);
    
    return splitview;
}


NSRect ui_container_getframe(UiContainer *ct) {
    return [ct->widget frame];
}

void ui_container_add(UiContainer *ct, NSView *view) {
    [ct->widget addSubview: view];
}

mercurial