ui/common/types.c

Sat, 24 Jan 2015 19:16:38 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 24 Jan 2015 19:16:38 +0100
changeset 79
052224670baf
parent 39
4e66271541e8
child 116
480354705c2f
permissions
-rw-r--r--

configure script updated

/*
 * 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.
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>

#include "../../ucx/list.h"
#include "../ui/tree.h"
#include "types.h"
#include "context.h"

UiObserver* ui_observer_new(ui_callback f, void *data) {
    UiObserver *observer = malloc(sizeof(UiObserver));
    observer->callback = f;
    observer->data = data;
    observer->next = NULL;
    return observer;
}

UiObserver* ui_obsvlist_add(UiObserver *list, UiObserver *observer) {
    if(!list) {
        return observer;
    } else {
        UiObserver *l = list;
        while(l->next) {
            l = l->next;
        }
        l->next = observer;
        return list;
    }
}

UiObserver* ui_add_observer(UiObserver *list, ui_callback f, void *data) {
    UiObserver *observer = ui_observer_new(f, data);
    return ui_obsvlist_add(list, observer);
}

void ui_notify(UiObserver *observer, void *data) {
    ui_notify_except(observer, NULL, data);
}

void ui_notify_except(UiObserver *observer, UiObserver *exc, void *data) {
    while(observer) {
        if(observer != exc) {
            UiEvent evt;
            evt.obj = NULL;
            evt.window = NULL;
            evt.document = NULL;
            evt.eventdata = data;
            evt.intval = 0;
            observer->callback(&evt, observer->data);
        }
        observer = observer->next;
    }
}

/* --------------------------- UiList --------------------------- */

UiList* ui_list_new() {
    UiList *list = malloc(sizeof(UiList));
    list->first = ui_list_first;
    list->next = ui_list_next;
    list->get = ui_list_get;
    list->count = ui_list_count;
    list->observers = NULL;
    
    list->data = NULL;
    list->iter = NULL;
    
    return list;
}

void ui_list_free(UiList *list) {
    ucx_list_free(list->data);
    free(list);
}

void* ui_list_first(UiList *list) {
    UcxList *elm = list->data;
    list->iter = elm;
    return elm ? elm->data : NULL;
}

void* ui_list_next(UiList *list) {
    UcxList *elm = list->iter;
    if(elm) {
        elm = elm->next;
        if(elm) {
            list->iter = elm;
            return elm->data;
        }
    }
    return NULL;
}

void* ui_list_get(UiList *list, int i) {
    UcxList *elm = ucx_list_get(list->data, i);
    if(elm) {
        list->iter = elm;
        return elm->data;
    } else {
        return NULL;
    }
}

int ui_list_count(UiList *list) {
    UcxList *elm = list->data;
    return (int)ucx_list_size(elm);
}

void ui_list_append(UiList *list, void *data) {
    list->data = ucx_list_append(list->data, data);
}

void ui_list_prepend(UiList *list, void *data) {
    list->data = ucx_list_prepend(list->data, data);
}

void ui_list_addobsv(UiList *list, ui_callback f, void *data) {
    list->observers = ui_add_observer(list->observers, f, data);
}

void ui_list_notify(UiList *list) {
    ui_notify(list->observers, list);
}


typedef struct {
    int  type;
    char *name;
} UiColumn;

UiModelInfo* ui_model_info(UiContext *ctx, ...) {
    UiModelInfo *info = ui_calloc(ctx, 1, sizeof(UiModelInfo));
    
    va_list ap;
    va_start(ap, ctx);
    
    UcxList *cols = NULL;
    int type;
    while((type = va_arg(ap, int)) != -1) {
        char *name = va_arg(ap, char*);
        
        UiColumn *column = malloc(sizeof(UiColumn));
        column->type = type;
        column->name = name;
        
        cols = ucx_list_append(cols, column);
    }
    
    va_end(ap);
    
    size_t len = ucx_list_size(cols);
    info->columns = len;
    info->types = ui_calloc(ctx, len, sizeof(UiModelType));
    info->titles = ui_calloc(ctx, len, sizeof(char*));
    
    int i = 0;
    UCX_FOREACH(elm, cols) {
        UiColumn *c = elm->data;
        info->types[i] = c->type;
        info->titles[i] = c->name;
        free(c);
        i++;
    }
    ucx_list_free(cols);
    
    return info;
}

void ui_model_info_free(UiContext *ctx, UiModelInfo *mi) {
    ucx_mempool_free(ctx->mempool, mi->types);
    ucx_mempool_free(ctx->mempool, mi->titles);
    ucx_mempool_free(ctx->mempool, mi);
}

mercurial