00001
00038 #include <assert.h>
00039 #include <string.h>
00040
00041 #include <gfx/gfx.h>
00042 #include <gfx/win.h>
00043 #include <gfx/wtk.h>
00044 #include <gfx/sysfont.h>
00045
00046 #include "app_clock.h"
00047 #include "app_desktop.h"
00048
00061
00062 #define COLOR_TEXT GFX_COLOR(255, 255, 255)
00063
00064 #define COLOR_BACKGROUND GFX_COLOR(0, 0, 0)
00065
00066 #define COLOR_BORDER GFX_COLOR(150, 150, 150)
00067
00069
00076
00077 #define FRAME_PADDING 40
00078
00079 #define FRAME_HEIGHT (gfx_get_height() - (2 * FRAME_PADDING))
00080
00081 #define FRAME_WIDTH (gfx_get_width() - (2 * FRAME_PADDING))
00082
00083 #define FRAME_POS_X FRAME_PADDING
00084
00085 #define FRAME_POS_Y FRAME_PADDING
00086
00088
00095
00096 #define TEXT_INDENT 15
00097
00098 #define TEXT_PADDING_NEWLINE 4
00099
00101
00105 enum app_clock_button_ids {
00107 BUTTON_EXIT_ID = 1,
00108 };
00109
00113 struct app_clock {
00115 struct wtk_basic_frame *frame;
00117 struct gfx_bitmap background;
00119 struct font prev_sysfont;
00120 };
00121
00129 static struct app_clock *the_clock_app;
00130
00138 static void app_clock_frame_draw_handler(struct win_window *win,
00139 const struct win_clip_region *clip)
00140 {
00141 gfx_coord_t y = FRAME_POS_Y + TEXT_INDENT;
00142
00143 gfx_draw_rect(FRAME_POS_X, FRAME_POS_Y, FRAME_WIDTH, FRAME_HEIGHT,
00144 COLOR_BORDER);
00145
00146 gfx_draw_string("Clock application",
00147 FRAME_POS_X + TEXT_INDENT, y, &sysfont, COLOR_TEXT,
00148 GFX_COLOR_TRANSPARENT);
00149
00150
00151
00152
00153
00154 y += (2 * gfx_font_get_height(&sysfont)) + TEXT_PADDING_NEWLINE;
00155 gfx_draw_string("This application",
00156 FRAME_POS_X + TEXT_INDENT, y, &sysfont, COLOR_TEXT,
00157 GFX_COLOR_TRANSPARENT);
00158
00159 y += gfx_font_get_height(&sysfont) + TEXT_PADDING_NEWLINE;
00160 gfx_draw_string("is left as an",
00161 FRAME_POS_X + TEXT_INDENT, y, &sysfont, COLOR_TEXT,
00162 GFX_COLOR_TRANSPARENT);
00163
00164 y += gfx_font_get_height(&sysfont) + TEXT_PADDING_NEWLINE;
00165 gfx_draw_string("exercise for the",
00166 FRAME_POS_X + TEXT_INDENT, y, &sysfont, COLOR_TEXT,
00167 GFX_COLOR_TRANSPARENT);
00168
00169 y += gfx_font_get_height(&sysfont) + TEXT_PADDING_NEWLINE;
00170 gfx_draw_string("user to implement.",
00171 FRAME_POS_X + TEXT_INDENT, y, &sysfont, COLOR_TEXT,
00172 GFX_COLOR_TRANSPARENT);
00173 }
00174
00185 static bool app_clock_frame_command_handler(struct wtk_basic_frame *frame,
00186 win_command_t command_data)
00187 {
00188 uint8_t command = (uint8_t)(uintptr_t)command_data;
00189
00190 switch (command) {
00191 case BUTTON_EXIT_ID:
00192 memcpy(&sysfont, &the_clock_app->prev_sysfont,
00193 sizeof(struct font));
00194 membag_free(the_clock_app);
00195 app_desktop_restart();
00196 return true;
00197
00198 default:
00199 break;
00200 }
00201
00202 return false;
00203 }
00204
00210 void app_clock_launch(struct workqueue_task *task)
00211 {
00212 struct win_window *win_root;
00213 struct win_window *parent;
00214 struct win_area area;
00215 struct wtk_button *button;
00216
00217 the_clock_app = membag_alloc(sizeof(struct app_clock));
00218 if (!the_clock_app)
00219 goto error_membag_alloc;
00220
00221
00222 memcpy(&the_clock_app->prev_sysfont, &sysfont, sizeof(struct font));
00223 sysfont.scale = 2;
00224
00225
00226 the_clock_app->background.type = BITMAP_SOLID;
00227 the_clock_app->background.data.color = COLOR_BACKGROUND;
00228
00229 win_root = win_get_root();
00230
00231
00232
00233
00234
00235
00236 area.pos.x = FRAME_POS_X;
00237 area.pos.y = FRAME_POS_Y;
00238 area.size.x = FRAME_WIDTH;
00239 area.size.y = FRAME_HEIGHT;
00240
00241 the_clock_app->frame = wtk_basic_frame_create(win_root, &area,
00242 &the_clock_app->background,
00243 app_clock_frame_draw_handler,
00244 app_clock_frame_command_handler, &the_clock_app);
00245 if (!the_clock_app->frame)
00246 goto error_text_frame;
00247
00248 parent = wtk_basic_frame_as_child(the_clock_app->frame);
00249 win_show(parent);
00250
00251
00252 area.size.x = APP_EXIT_BUTTON_SIZE_X;
00253 area.size.y = APP_EXIT_BUTTON_SIZE_Y;
00254
00255 area.pos.x = FRAME_WIDTH - APP_EXIT_BUTTON_SIZE_X - 1;
00256 area.pos.y = FRAME_HEIGHT - APP_EXIT_BUTTON_SIZE_Y - 1;
00257
00258 button = wtk_button_create(parent, &area, APP_EXIT_BUTTON_TEXT,
00259 (win_command_t)BUTTON_EXIT_ID);
00260 if (!button)
00261 goto error_widget;
00262
00263 win_show(wtk_button_as_child(button));
00264
00265 return;
00266
00267 error_widget:
00268 win_destroy(parent);
00269 error_text_frame:
00270 memcpy(&sysfont, &the_clock_app->prev_sysfont, sizeof(struct font));
00271 membag_free(the_clock_app);
00272 error_membag_alloc:
00273 app_desktop_restart();
00274 }
00275