ui/common/utils.c

Sun, 07 Dec 2025 12:24:12 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 07 Dec 2025 12:24:12 +0100
changeset 964
c563220d9aea
parent 930
4ce8df2311f0
child 985
93f07ccfd997
permissions
-rw-r--r--

implement ui_listview_destroy (Motif)

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2017 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 "utils.h"
#include "properties.h"

#include <cx/string.h>

UiPathElm* ui_default_pathelm_func(const char* full_path, size_t len, size_t* ret_nelm, void* data) {
    cxstring *pathelms;
    size_t nelm = cx_strsplit_a(cxDefaultAllocator, cx_strn(full_path, len), CX_STR("/"), 4096, &pathelms);

    if (nelm == 0) {
        *ret_nelm = 0;
        return NULL;
    }

    UiPathElm* elms = (UiPathElm*)calloc(nelm, sizeof(UiPathElm));
    size_t n = nelm;
    int j = 0;
    for (int i = 0; i < nelm; i++) {
        cxstring c = pathelms[i];
        if (c.length == 0) {
            if (i == 0) {
                c.length = 1;
            }
            else {
                n--;
                continue;
            }
        }

        cxmutstr m = cx_strdup(c);
        elms[j].name = m.ptr;
        elms[j].name_len = m.length;
        
        size_t elm_path_len = c.ptr + c.length - full_path;
        cxmutstr elm_path = cx_strdup(cx_strn(full_path, elm_path_len));
        elms[j].path = elm_path.ptr;
        elms[j].path_len = elm_path.length;

        j++;
    }
    *ret_nelm = n;

    return elms;
}

void ui_get_window_default_width(int *width, int *height) {
    const char *width_str = ui_get_property("ui.window.width");
    const char *height_str = ui_get_property("ui.window.height");
    if(width_str && height_str) {
        int w = atoi(width_str);
        int h = atoi(height_str);
        if(w > 0 && h > 0) {
            *width = w;
            *height = h;
        }
    }
}

mercurial