00001
00039 #include <led.h>
00040 #include <board.h>
00041 #include <string.h>
00042
00043 #include <gfx/gfx.h>
00044 #include <gfx/win.h>
00045 #include <gfx/wtk.h>
00046 #include <gfx/sysfont.h>
00047 #include <membag.h>
00048 #include <debug.h>
00049 #include <mainloop.h>
00050 #include <string.h>
00051 #include <stream.h>
00052
00053 #include "app_widget.h"
00054
00067 enum app_widget_ids {
00069 SLIDER_ID = 1,
00071 BUTTON_ID,
00072 };
00073
00080
00081 #define APP_BACKGROUND_COLOR GFX_COLOR(50, 50, 50)
00082
00084
00091
00092 #define LABEL_POS_X 10
00093
00094 #define LABEL_POS_Y 10
00095
00097 #define SLIDER_POS_X 10
00098
00099 #define SLIDER_POS_Y 60
00100
00101 #define SLIDER_SIZE_X 80
00102
00103 #define SLIDER_SIZE_Y 40
00104
00106 #define SLIDER_PB_SPACING_X 10
00107
00108 #define PB_SIZE_X SLIDER_SIZE_X
00109
00110 #define PB_SIZE_Y SLIDER_SIZE_Y
00111
00113
00120
00121 #define SLIDER_MAX_VALUE 100
00122
00124
00131
00132 const static char *demo_string = "Demonstrating widgets";
00133
00135
00142
00143 static struct wtk_basic_frame *frame;
00145 static struct wtk_slider *slider;
00147 static struct wtk_progress_bar *progress_bar;
00149 static struct gfx_bitmap frame_background;
00151 static uint8_t counter;
00153 static struct wtk_basic_frame *sub_frame;
00155 static struct gfx_bitmap sub_frame_background;
00156
00158
00166 static bool widget_frame_command_handler(struct wtk_basic_frame *frame,
00167 win_command_t command_data)
00168 {
00169 char command = (char)(uintptr_t)command_data;
00170
00171 switch (command) {
00172 case SLIDER_ID:
00173 wtk_progress_bar_set_value(progress_bar,
00174 wtk_slider_get_value(slider));
00175 break;
00176
00177 case BUTTON_ID:
00179 counter++;
00180 win_redraw(wtk_basic_frame_as_child(sub_frame));
00181 break;
00182 }
00183
00184 return false;
00185 }
00186
00194 static void sub_frame_draw_handler(struct win_window *win,
00195 struct win_clip_region const *clip)
00196 {
00197 char buffer[4];
00198
00199 snprintf(buffer, sizeof(buffer), "%3d", counter);
00204 gfx_draw_string(buffer, clip->origin.x + 30, clip->origin.y + 12,
00205 &sysfont, GFX_COLOR(255, 255, 255),
00206 GFX_COLOR_TRANSPARENT);
00207 }
00208
00216 void app_widget_launch(struct workqueue_task *task) {
00217
00218 struct win_window *win_root;
00219 struct win_window *parent;
00220 struct win_area area;
00221 struct wtk_label *lbl;
00222 struct wtk_button *btn;
00223
00224
00225 sysfont.scale = 2;
00226
00227
00228 win_root = win_get_root();
00229
00230
00231
00232
00233 frame_background.type = BITMAP_SOLID;
00234 frame_background.data.color = APP_BACKGROUND_COLOR;
00235
00236
00237 area.pos.x = 0;
00238 area.pos.y = 0;
00239 area.size.x = gfx_get_width();
00240 area.size.y = gfx_get_height();
00241
00242
00243
00244
00245
00246
00247 frame = wtk_basic_frame_create(win_root, &area,
00248 &frame_background, NULL,
00249 widget_frame_command_handler, NULL);
00250 if (!frame) {
00251 goto error_frame;
00252 }
00253
00254
00255 parent = wtk_basic_frame_as_child(frame);
00256
00257
00258
00259
00260
00261 win_show(parent);
00262
00263
00264 area.pos.x = LABEL_POS_X;
00265 area.pos.y = LABEL_POS_Y;
00266
00267 wtk_label_size_hint(&area.size, demo_string);
00268
00269
00270
00271
00272
00273 lbl = wtk_label_create(parent, &area, demo_string, false);
00274 if (!lbl) {
00275 goto error_widget;
00276 }
00277
00278
00279 win_show(wtk_label_as_child(lbl));
00280
00281
00282 area.pos.x = SLIDER_POS_X;
00283 area.pos.y = SLIDER_POS_Y;
00284 area.size.x = SLIDER_SIZE_X;
00285 area.size.y = SLIDER_SIZE_Y;
00286
00287
00288
00289
00290
00291 slider = wtk_slider_create(parent, &area, SLIDER_MAX_VALUE,
00292 SLIDER_MAX_VALUE / 2, WTK_SLIDER_HORIZONTAL|WTK_SLIDER_CMD_RELEASE,
00293 (win_command_t)SLIDER_ID);
00294 if (!slider) {
00295 goto error_widget;
00296 }
00297
00298
00299 win_show(wtk_slider_as_child(slider));
00300
00301
00302 area.pos.x += area.size.x + SLIDER_PB_SPACING_X;
00303 area.size.x = PB_SIZE_X;
00304 area.size.y = PB_SIZE_Y;
00305
00306
00307
00308
00309
00310 progress_bar = wtk_progress_bar_create(parent, &area, SLIDER_MAX_VALUE,
00311 SLIDER_MAX_VALUE / 2, GFX_COLOR(255, 255, 0),
00312 GFX_COLOR(90, 90, 90), WTK_PROGRESS_BAR_HORIZONTAL);
00313 if (!progress_bar) {
00314 goto error_widget;
00315 }
00316
00317
00318 win_show(wtk_progress_bar_as_child(progress_bar));
00319
00321 area.pos.x = 10;
00322 area.pos.y = 150;
00323 area.size.x = 90;
00324 area.size.y = 40;
00325
00326 btn = wtk_button_create(parent, &area, "Click",
00327 (win_command_t)BUTTON_ID);
00328 if (!btn) {
00329 goto error_widget;
00330 }
00331 win_show(wtk_button_as_child(btn));
00332
00334 area.pos.x += area.size.x + 40;
00335
00336 sub_frame_background.type = BITMAP_SOLID;
00337 sub_frame_background.data.color = GFX_COLOR(127, 0, 0);
00338
00339 sub_frame = wtk_basic_frame_create(parent, &area,
00340 &sub_frame_background, sub_frame_draw_handler,
00341 NULL, NULL);
00342 if (!sub_frame) {
00343 goto error_widget;
00344 }
00345 win_show(wtk_basic_frame_as_child(sub_frame));
00346
00347 return;
00348
00349 error_widget:
00350
00351 win_destroy(wtk_basic_frame_as_child(frame));
00352 error_frame:
00353
00354 while(1);
00355 }
00356