ui/common/types.c

Thu, 15 May 2014 17:17:58 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 15 May 2014 17:17:58 +0200
changeset 38
8ccdde37275b
parent 16
a499c8a72c15
child 39
4e66271541e8
permissions
-rw-r--r--

added some allocation functions

/*
 * 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 "../../ucx/list.h"
#include "types.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);
}

mercurial