summaryrefslogtreecommitdiff
path: root/fw/fe310/eos/eve/screen/form.c
blob: 3e5b97c90b77d7c41708c8ddf7e09d8820700d34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <stdlib.h>
#include <string.h>

#include "eve.h"
#include "eve_kbd.h"
#include "eve_font.h"

#include "window.h"
#include "page.h"
#include "form.h"

#define MIN(X, Y)               (((X) < (Y)) ? (X) : (Y))
#define MAX(X, Y)               (((X) > (Y)) ? (X) : (Y))

static void widgets_destroy(EVEWidget *widget, uint16_t widget_size) {
    int i;

    for (i=0; i<widget_size; i++) {
        if (widget->label) eve_free(widget->label);
        eve_widget_destroy(widget);
        widget = eve_widget_next(widget);
    }
}

EVEPage *eve_form_create(EVEWindow *window, EVEViewStack *stack, EVEFormSpec spec[], uint16_t spec_size, eve_page_uievt_t uievt, eve_page_destructor_t destructor) {
    EVEWidget *widgets;
    EVEWidget *widget;
    EVELabel *label;
    EVEPage *page;
    int w_size = 0;
    int i, r;

    if (uievt == NULL) uievt = eve_form_uievt;
    if (destructor == NULL) destructor = eve_form_destroy;
    page = eve_malloc(sizeof(EVEPage));
    if (page == NULL) {
        return NULL;
    }
    eve_form_init(page, window, stack, NULL, 0, uievt, destructor);

    for (i=0; i<spec_size; i++) {
        w_size += eve_widget_size(spec[i].widget.type);
    }
    widgets = eve_malloc(w_size);
    if (widgets == NULL) {
        eve_free(page);
        return NULL;
    }

    widget = widgets;
    for (i=0; i<spec_size; i++) {
        r = eve_widget_create(widget, &spec[i].widget, page);
        if (r) {
            widgets_destroy(widgets, i);
            eve_free(widgets);
            eve_free(page);
            return NULL;
        }
        if (spec[i].label.title) {
            label = eve_malloc(sizeof(EVELabel));
            if (label == NULL) {
                eve_widget_destroy(widget);
                widgets_destroy(widgets, i);
                eve_free(widgets);
                eve_free(page);
                return NULL;
            }
            eve_label_create(label, &spec[i].label, page);
            eve_widget_set_label(widget, label);
        }
        if (widget->g.w == 0) widget->g.w = window->g.w - (widget->label ? widget->label->g.w : 0);
        widget = eve_widget_next(widget);
    }
    eve_form_set_widget(page, widgets, spec_size);

    return page;
}

void eve_form_init(EVEPage *page, EVEWindow *window, EVEViewStack *stack, EVEWidget *widget, uint16_t widget_size, eve_page_uievt_t uievt, eve_page_destructor_t destructor) {
    eve_page_init(page, window, stack, NULL, 0, EVE_PAGE_OPT_SCROLL_Y | EVE_PAGE_OPT_SCROLL_BACK | EVE_PAGE_OPT_TRACK_EXT_Y, eve_page_draw, eve_page_touch, uievt, destructor);
    if (widget) eve_form_set_widget(page, widget, widget_size);
}

void eve_form_set_widget(EVEPage *page, EVEWidget *widget, uint16_t widget_size) {
    eve_page_set_widget(page, widget, widget_size);
    eve_form_update_g(page, NULL);
}

void eve_form_destroy(EVEPage *page) {
    widgets_destroy(page->widget, page->widget_size);
    eve_free(page->widget);
    eve_free(page);
}

int eve_form_uievt(EVEPage *page, uint16_t evt, void *param) {
    switch (evt) {
        case EVE_UIEVT_WIDGET_UPDATE_G:
            eve_form_update_g(page, (EVEWidget *)param);
            break;

        case EVE_UIEVT_WIDGET_FOCUS_IN:
            break;

        case EVE_UIEVT_WIDGET_FOCUS_OUT:
            break;

        case EVE_UIEVT_GEST_SCROLL_START:
            break;

        case EVE_UIEVT_GEST_SCROLL_STOP:
            break;

        case EVE_UIEVT_GEST_TRACK_START:
            break;

        case EVE_UIEVT_GEST_TRACK_STOP: {
            EVEUIEvtTouch *touch_p = (EVEUIEvtTouch *)param;

            if (touch_p->touch->eevt & EVE_TOUCH_EETYPE_TRACK_RIGHT) {
                eve_page_close(page);
                return 1;
            }
            break;
        }
    }
    return 0;
}

void eve_form_update_g(EVEPage *page, EVEWidget *_widget) {
    EVEWidget *widget = page->widget;
    EVEWidget *lastw = NULL;
    int i;
    uint16_t w = 0;
    uint16_t h = 0;
    uint16_t l_h = 0;

    for (i=0; i<page->widget_size; i++) {
        if (widget->flags & EVE_WIDGET_FLAG_HIDDEN) goto update_g_nextw;
        if (widget->label) {
            h += l_h;
            w = widget->label->g.w;
            l_h = widget->label->g.h;
            widget->label->g.x = 0;
            widget->label->g.y = h;
        }
        if (w + widget->g.w > page->v.window->g.w) {
            h += l_h;
            w = 0;
            l_h = 0;
        }
        widget->g.x = w;
        widget->g.y = h;

        w += widget->g.w;
        l_h = MAX(l_h, widget->g.h);

        lastw = widget;
update_g_nextw:
        widget = eve_widget_next(widget);
    }
    page->g.w = page->v.window->g.w;
    page->g.h = h + l_h;

    if (lastw && eve_page_oob(page, NULL, NULL)) {
        eve_page_show_rect(page, &lastw->g);
    }
}