00001
00038 #include <stdint.h>
00039 #include <stdlib.h>
00040 #include <assert.h>
00041 #include <membag.h>
00042 #include <string.h>
00043 #include <gfx/wtk.h>
00044
00060 struct wtk_basic_frame {
00062 struct win_window *win;
00064 wtk_basic_frame_command_handler_t frame_handler;
00066 void *custom_data;
00068 wtk_basic_frame_draw_handler_t draw_handler;
00069 };
00070
00081 struct win_window *wtk_basic_frame_as_child(struct wtk_basic_frame *basic_frame)
00082 {
00083 assert(basic_frame);
00084 return basic_frame->win;
00085 }
00086
00097 void *wtk_basic_frame_get_custom_data(const struct wtk_basic_frame *basic_frame)
00098 {
00099 assert(basic_frame);
00100 return basic_frame->custom_data;
00101 }
00102
00115 static bool wtk_basic_frame_handler(struct win_window *win,
00116 enum win_event_type type, const void *data)
00117 {
00118
00119 struct wtk_basic_frame *frame;
00120 bool should_destroy;
00121 struct win_clip_region const *clip;
00122
00123 frame = win_get_custom_data(win);
00124
00125 switch (type) {
00126 case WIN_EVENT_DRAW:
00127
00128
00129
00130 clip = (struct win_clip_region const *)data;
00131
00132 if (win == frame->win) {
00133 if (frame->draw_handler) {
00134
00135 frame->draw_handler(win, clip);
00136 }
00137 }
00138
00139
00140
00141 return true;
00142
00143 case WIN_EVENT_POINTER:
00144
00145
00146
00147
00148 return true;
00149
00150 case WIN_EVENT_DESTROY:
00151
00152
00153
00154 if (win == frame->win) {
00155
00156
00157
00158
00159 membag_free(frame);
00160 }
00161
00162
00163
00164
00165 return true;
00166
00167 case WIN_EVENT_COMMAND:
00168
00169
00170
00171
00172 if (win == frame->win) {
00173 if (frame->frame_handler) {
00174
00175
00176
00177
00178
00179 should_destroy = frame->frame_handler(frame,
00180 (win_command_t)data);
00181
00182
00183
00184
00185
00186
00187
00188 if (should_destroy) {
00189 win_destroy(frame->win);
00190 }
00191
00192
00193
00194
00195 return true;
00196 }
00197 }
00198
00199
00200
00201
00202 return false;
00203
00204 default:
00205
00206 return false;
00207
00208 }
00209 }
00210
00236 struct wtk_basic_frame *wtk_basic_frame_create(struct win_window *parent,
00237 const struct win_area *area, struct gfx_bitmap *background,
00238 wtk_basic_frame_draw_handler_t draw_handler,
00239 wtk_basic_frame_command_handler_t frame_handler, void *custom_data)
00240 {
00241 struct win_attributes attr;
00242 struct wtk_basic_frame *basic_frame;
00243
00244 assert(area);
00245 assert(parent);
00246
00247
00248 basic_frame = membag_alloc(sizeof(struct wtk_basic_frame));
00249 if (!basic_frame) {
00250 goto outofmem_basic_frame;
00251 }
00252
00253
00254 basic_frame->frame_handler = frame_handler;
00255 basic_frame->custom_data = custom_data;
00256 basic_frame->draw_handler = draw_handler;
00257
00258
00259 attr.area = *area;
00260 attr.event_handler = wtk_basic_frame_handler;
00261 attr.custom = basic_frame;
00262
00263
00264 if (background) {
00265 attr.background = background;
00266 attr.behavior = 0;
00267 } else {
00268 attr.background = NULL;
00269 attr.behavior = WIN_BEHAVIOR_REDRAW_PARENT;
00270 }
00271
00272
00273 basic_frame->win = win_create(parent, &attr);
00274 if (!basic_frame->win) {
00275 goto outofmem_win;
00276 }
00277
00278 return basic_frame;
00279
00280 outofmem_win:
00281 membag_free(basic_frame);
00282
00283 outofmem_basic_frame:
00284 return NULL;
00285 }
00286