ui/motif/range.c

Sun, 16 Jun 2024 11:31:50 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 16 Jun 2024 11:31:50 +0200
branch
newapi
changeset 294
7232a8e68eeb
parent 176
bc63cb601f6d
permissions
-rw-r--r--

fix build

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2016 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 "range.h"
#include "container.h"
#include "../common/context.h"
#include "../common/object.h"


static UIWIDGET ui_scrollbar(UiObject *obj, UiOrientation orientation, UiRange *range, ui_callback f, void *userdata) {
    UiContainer *ct = uic_get_current_container(obj);
    
    int n = 0;
    Arg args[16];
    XtSetArg(args[n], XmNorientation, orientation == UI_HORIZONTAL ? XmHORIZONTAL : XmVERTICAL);
    n++;
    XtSetArg (args[n], XmNmaximum, 10);
    n++;
    XtSetArg (args[n], XmNsliderSize, 1);
    n++;
    Widget parent = ct->prepare(ct, args, &n, FALSE);
    Widget scrollbar = XmCreateScrollBar(parent, "scrollbar", args, n);
    XtManageChild(scrollbar);
    ct->add(ct, scrollbar);
    
    if(range) {
        range->get = ui_scrollbar_get;
        range->set = ui_scrollbar_set;
        range->setrange = ui_scrollbar_setrange;
        range->setextent = ui_scrollbar_setextent;
        range->obj = scrollbar;
    }
    
    if(f) {
        UiEventData *event = cxMalloc(
                obj->ctx->allocator,
                sizeof(UiEventData));
        event->obj = obj;
        event->userdata = userdata;
        event->callback = f;
        event->value = 0;
        XtAddCallback(
                scrollbar,
                XmNvalueChangedCallback,
                (XtCallbackProc)ui_scrollbar_callback,
                event);
    }
    
    return scrollbar;
}

UIWIDGET ui_hscrollbar(UiObject *obj, UiRange *range, ui_callback f, void *userdata) {
    return ui_scrollbar(obj, UI_HORIZONTAL, range, f, userdata);
}

UIWIDGET ui_vscrollbar(UiObject *obj, UiRange *range, ui_callback f, void *userdata) {
    return ui_scrollbar(obj, UI_VERTICAL, range, f, userdata);
}

void ui_scrollbar_callback(Widget scrollbar, UiEventData *event, XtPointer cdata) {
    UiEvent e;
    e.obj = event->obj;
    e.window = event->obj->window;
    e.document = event->obj->ctx->document;
    e.intval = event->value;
    event->callback(&e, event->userdata);
}

double ui_scrollbar_get(UiRange *range) {
    int intval;
    XtVaGetValues(
            range->obj,
            XmNvalue,
            &intval,
            NULL);
    double value = (double)intval / 10;
    range->value = value;
    return value;
}

void   ui_scrollbar_set(UiRange *range, double value) {
    XtVaSetValues(
            range->obj,
            XmNvalue,
            (int)(value * 10),
            NULL);
    range->value = value;
}

void   ui_scrollbar_setrange(UiRange *range, double min, double max) {
    XtVaSetValues(
            range->obj,
            XmNminimum,
            (int)(min * 10),
            XmNmaximum,
            (int)(max * 10),
            NULL);
    range->min = min;
    range->max = max;
}

void   ui_scrollbar_setextent(UiRange *range, double extent) {
    XtVaSetValues(
            range->obj,
            XmNsliderSize,
            (int)(extent * 10),
            NULL);
    range->extent = extent;
}

mercurial