Minor syntactical changes for readability.
[xuni.git] / src / widget / panel.c
blob2a858ff2fb947b35f102179c5df516182c06ba0e
1 /*! \file panel.c
3 */
5 #include "../graphics.h"
6 #include "../loadso.h"
7 #include "../memory.h"
8 #include "widgets.h"
9 #include "panel.h"
11 static func_point_t panel_load_handler(void *object,
12 struct resource_list_t *handler, const char *name);
14 static void free_panel_data(struct xuni_t *xuni, struct widget_t *widget);
15 static void reposition_panel(struct xuni_t *xuni, struct widget_t *widget);
16 static void paint_panel(struct xuni_t *xuni, struct widget_t *widget);
18 void panel_widget_event(struct xuni_t *xuni, struct widget_t *widget,
19 enum widget_event_t event) {
21 static void (*function[])(struct xuni_t *xuni, struct widget_t *widget)
22 = {
24 free_panel_data,
26 paint_panel,
27 reposition_panel,
31 call_widget_event_func(xuni, widget, event, function,
32 sizeof(function) / sizeof(*function));
35 void init_panel(struct widget_t *widget) {
36 widget->type = WIDGET_PANEL;
37 widget->p.panel = xuni_memory_allocate(sizeof(*widget->p.panel));
39 init_panel_data(widget);
41 /*widget->compose = allocate_panel(widget);*/
44 void init_panel_data(struct widget_t *widget) {
45 enum panel_event_type_t x;
47 widget->p.panel->data = 0;
48 widget->p.panel->frameupdate = 0;
50 for(x = 0; x < PANEL_EVENTS; x ++) {
51 widget->p.panel->event[x].type = x;
52 widget->p.panel->event[x].handler = 0;
55 widget->p.panel->nameid = 0;
57 widget->p.panel->accel = 0;
60 void set_panel_data(struct widget_t *widget, void *vdata, int frameupdate) {
61 widget->p.panel->data = vdata;
62 widget->p.panel->frameupdate = frameupdate;
64 widget->p.panel->nameid = 0;
65 widget->p.panel->accel = 0;
68 void set_panel_callback(struct widget_t *widget, enum panel_event_type_t type,
69 panel_event_func_t func) {
71 widget->p.panel->event[type].handler = func;
74 static func_point_t panel_load_handler(void *object,
75 struct resource_list_t *handler, const char *name) {
77 const char *func = first_resource_text(first_resource_tag(handler, name));
79 if(!func || !*func) return 0;
81 /*printf("Loading handler \"%s\"\n", func);*/
83 return xuni_loadso_load_function(object, func);
86 /* !!! */
87 int default_panel_sel(struct xuni_t *xuni, struct panel_data_t *data);
89 void init_panel_from_resource(struct xuni_t *xuni, struct widget_t *widget,
90 struct resource_list_t *list) {
92 struct resource_list_t *handler = first_resource_tag(list, "handler");
93 loadso_t object = xuni_loadso_load_object(xuni->loadso,
94 first_resource_text(first_resource_tag(handler, "file")));
96 widget->type = WIDGET_PANEL;
97 widget->p.panel = xuni_memory_allocate(sizeof(*widget->p.panel));
99 widget->p.panel->data = 0;
100 widget->p.panel->frameupdate = 0;
101 widget->p.panel->event[PANEL_EVENT_INIT].handler
102 = (panel_event_func_t)panel_load_handler(object, handler, "init");
103 widget->p.panel->event[PANEL_EVENT_START].handler
104 = (panel_event_func_t)panel_load_handler(object, handler, "start");
105 widget->p.panel->event[PANEL_EVENT_EVENT].handler
106 = (panel_event_func_t)panel_load_handler(object, handler, "event");
107 widget->p.panel->event[PANEL_EVENT_SEL].handler
108 = (panel_event_func_t)default_panel_sel;
109 widget->p.panel->event[PANEL_EVENT_CLICK].handler
110 = (panel_event_func_t)panel_load_handler(object, handler,
111 "click");
112 widget->p.panel->event[PANEL_EVENT_DEACTIVATE].handler
113 = (panel_event_func_t)panel_load_handler(object, handler,
114 "deactivate");
115 widget->p.panel->event[PANEL_EVENT_PAINT].handler
116 = (panel_event_func_t)panel_load_handler(object, handler, "paint");
117 widget->p.panel->event[PANEL_EVENT_FREE].handler
118 = (panel_event_func_t)panel_load_handler(object, handler, "free");
120 widget->p.panel->nameid = 0;
122 widget->p.panel->accel = 0;
125 static void free_panel_data(struct xuni_t *xuni, struct widget_t *widget) {
126 xuni_memory_free(widget->p.panel->data);
128 if(widget->p.panel->nameid) {
129 xuni_memory_free(widget->p.panel->nameid->widget);
130 xuni_memory_free(widget->p.panel->nameid);
133 if(widget->p.panel->accel) {
134 xuni_memory_free(widget->p.panel->accel->key);
135 xuni_memory_free(widget->p.panel->accel);
138 xuni_memory_free(widget->p.panel);
141 /* !!! This code really needs to be re-written and moved somewhere else --
142 it's a big, messy kludge.
144 #define MOVE_LISTBOX_DATA_DOWN
146 /* !!! could do away with paint_*(), paint_func_t functions */
147 static void paint_panel(struct xuni_t *xuni, struct widget_t *widget) {
148 #ifdef MOVE_LISTBOX_DATA_DOWN
149 struct clip_pos_t *oldclip, clip;
150 int xdiff, ydiff, hasclip;
151 #endif
152 size_t x;
154 if(!widget || !widget->compose) return;
156 for(x = 0; x < widget->compose->widgets; x ++) {
157 #ifdef MOVE_LISTBOX_DATA_DOWN
158 hasclip = (widget->pos->clip != 0);
159 if(hasclip) {
160 oldclip = widget->compose->widget[x]->pos->clip;
161 clip = *widget->pos->clip;
162 widget->compose->widget[x]->pos->clip = &clip;
165 xdiff = (widget->compose->widget[x]->pos->real.x
166 - widget->pos->real.x);
168 /*clip.wclip -= xdiff;
169 clip.wclip += clip.xclip;
170 if(clip.wclip < 0) clip.wclip = 0;*/
172 clip.xclip -= xdiff;
173 if(clip.xclip < 0) clip.xclip = 0;
175 clip.xoff += clip.xclip;
179 ydiff = (widget->compose->widget[x]->pos->real.y
180 - widget->pos->real.y);
182 clip.hclip -= ydiff;
183 clip.hclip += clip.yclip;
184 if(clip.hclip < 0) clip.hclip = 0;
186 clip.yclip -= ydiff;
187 if(clip.yclip < 0) clip.yclip = 0;
189 clip.yoff += clip.yclip;
192 #endif
194 widget_event(xuni, widget->compose->widget[x], WIDGET_EVENT_PAINT);
196 #ifdef MOVE_LISTBOX_DATA_DOWN
197 if(hasclip /*&& widget->compose->widget[x]->pos->clip*/) {
198 widget->compose->widget[x]->pos->clip = oldclip;
200 #endif
204 static void reposition_panel(struct xuni_t *xuni, struct widget_t *widget) {
208 void free_panel(struct xuni_t *xuni, struct panel_t *panel) {
209 size_t x;
211 if(!panel) return;
213 for(x = 0; x < panel->widgets; x ++) {
214 widget_event(xuni, panel->widget[x], WIDGET_EVENT_FREE);
217 xuni_memory_free(panel->widget);
219 panel->widget = 0;
220 panel->widgets = 0;
222 xuni_memory_free(panel);