diff options
| author | bernard.xiong <[email protected]> | 2009-11-23 23:34:58 +0000 |
|---|---|---|
| committer | bernard.xiong <[email protected]> | 2009-11-23 23:34:58 +0000 |
| commit | 010cec20096bfdc77538071f5e1a14a5fb1c422c (patch) | |
| tree | fdb1f398ef64d92279164e0ece756c76e7f75058 /rtgui | |
| parent | c1e1d638ff5309184f514545cb0bd700088bc732 (diff) | |
add radio player UI
git-svn-id: https://rt-thread.googlecode.com/svn/trunk@171 bbd45198-f89e-11dd-88c7-29a3b14d5316
Diffstat (limited to 'rtgui')
46 files changed, 5190 insertions, 5015 deletions
diff --git a/rtgui/common/asc16font.c b/rtgui/common/asc16font.c index a2998099d8..4af71da2d6 100644 --- a/rtgui/common/asc16font.c +++ b/rtgui/common/asc16font.c @@ -12,7 +12,7 @@ * 2009-10-16 Bernard first version */ #include <rtgui/font.h> -
+ #ifdef RTGUI_USING_FONT16 const unsigned char asc16_font[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, @@ -292,4 +292,4 @@ struct rtgui_font rtgui_font_asc16 = }; /* size = 4096 bytes */ -#endif
+#endif diff --git a/rtgui/common/dc.c b/rtgui/common/dc.c index 1987d08bd3..b8e06bf176 100644 --- a/rtgui/common/dc.c +++ b/rtgui/common/dc.c @@ -12,10 +12,10 @@ * 2009-10-16 Bernard first version */ #include <rtgui/dc.h> -#include <rtgui/rtgui_system.h>
+#include <rtgui/rtgui_system.h> -#include <string.h> /* for strlen */
-#include <stdlib.h> /* fir qsort */
+#include <string.h> /* for strlen */ +#include <stdlib.h> /* fir qsort */ /* for sin/cos etc */ #include <math.h> diff --git a/rtgui/common/dc_buffer.c b/rtgui/common/dc_buffer.c index a04fcb9c78..ebb182c81b 100644 --- a/rtgui/common/dc_buffer.c +++ b/rtgui/common/dc_buffer.c @@ -14,7 +14,7 @@ #include <rtgui/rtgui.h> #include <rtgui/dc.h> #include <rtgui/color.h> -#include <rtgui/rtgui_system.h>
+#include <rtgui/rtgui_system.h> struct rtgui_dc_buffer { diff --git a/rtgui/common/dc_hw.c b/rtgui/common/dc_hw.c index b5d8be3b6a..ab1b885195 100644 --- a/rtgui/common/dc_hw.c +++ b/rtgui/common/dc_hw.c @@ -13,7 +13,7 @@ */ #include <rtgui/dc.h> #include <rtgui/driver.h> -#include <rtgui/rtgui_system.h>
+#include <rtgui/rtgui_system.h> #include <rtgui/widgets/view.h> #include <rtgui/widgets/window.h> #include <rtgui/widgets/workbench.h> diff --git a/rtgui/common/filerw.c b/rtgui/common/filerw.c index a7656d853c..1e03dda764 100644 --- a/rtgui/common/filerw.c +++ b/rtgui/common/filerw.c @@ -15,7 +15,7 @@ #include <rtgui/filerw.h> #include <rtgui/rtgui_system.h> -#ifdef RT_USING_DFS_FILERW +#ifdef RTGUI_USING_DFS_FILERW #include <dfs_posix.h> /* standard file read/write */ @@ -95,7 +95,7 @@ static int stdio_close(struct rtgui_filerw *context) return -1; } -#elif RT_USING_STDIO_FILERW +#elif defined(RTGUI_USING_STDIO_FILERW) #include <stdio.h> /* standard file read/write */ @@ -302,26 +302,26 @@ rt_uint8_t* rtgui_filerw_mem_getdata(struct rtgui_filerw* context) } /* file read/write public interface */ -#ifdef RT_USING_DFS_FILERW
-static int parse_mode(const char *mode)
-{
- int f=0;
-
- for (;;)
- {
- switch (*mode)
- {
- case 0: return f;
- case 'b': break;
- case 'r': f=O_RDONLY; break;
- case 'w': f=O_WRONLY|O_CREAT|O_TRUNC; break;
- case 'a': f=O_WRONLY|O_CREAT|O_APPEND; break;
- case '+': f=(f&(~O_WRONLY))|O_RDWR; break;
- }
-
- ++mode;
- }
-}
+#ifdef RTGUI_USING_DFS_FILERW +static int parse_mode(const char *mode) +{ + int f=0; + + for (;;) + { + switch (*mode) + { + case 0: return f; + case 'b': break; + case 'r': f=O_RDONLY; break; + case 'w': f=O_WRONLY|O_CREAT|O_TRUNC; break; + case 'a': f=O_WRONLY|O_CREAT|O_APPEND; break; + case '+': f=(f&(~O_WRONLY))|O_RDWR; break; + } + + ++mode; + } +} struct rtgui_filerw* rtgui_filerw_create_file(const char* filename, const char* mode) { @@ -352,7 +352,7 @@ struct rtgui_filerw* rtgui_filerw_create_file(const char* filename, const char* return &(rw->parent); } -#elif RT_USING_STDIO_FILERW +#elif defined(RTGUI_USING_STDIO_FILERW) struct rtgui_filerw* rtgui_filerw_create_file(const char* filename, const char* mode) { FILE *fp; diff --git a/rtgui/common/font_hz_file.c b/rtgui/common/font_hz_file.c index 6c2967317d..11ee300f86 100644 --- a/rtgui/common/font_hz_file.c +++ b/rtgui/common/font_hz_file.c @@ -5,6 +5,7 @@ #include <rtgui/font.h> #include <rtgui/tree.h> #include <rtgui/rtgui_system.h> +#ifdef RTGUI_USING_HZ_FILE #include <dfs_posix.h> #define HZ_CACHE_MAX 64 @@ -64,7 +65,7 @@ static rt_uint8_t* _font_cache_get(struct rtgui_hz_file_font* font, rt_uint16_t } /* insert to cache */ - SPLAY_INSERT(cache_tree, &(font->cache_root), cache);
+ SPLAY_INSERT(cache_tree, &(font->cache_root), cache); font->cache_size ++; if (font->cache_size > HZ_CACHE_MAX) @@ -75,7 +76,7 @@ static rt_uint8_t* _font_cache_get(struct rtgui_hz_file_font* font, rt_uint16_t while (SPLAY_LEFT(left, hz_node) != RT_NULL) left = SPLAY_LEFT(left, hz_node); /* remove the left node */ - SPLAY_REMOVE(cache_tree, &(font->cache_root), left);
+ SPLAY_REMOVE(cache_tree, &(font->cache_root), left); font->cache_size --; } @@ -142,3 +143,4 @@ static void rtgui_hz_file_font_get_metrics(struct rtgui_font* font, const rt_uin rect->x2 = (rt_int16_t)(hz_file_font->font_size/2 * rt_strlen((const char*)text)); rect->y2 = hz_file_font->font_size; } +#endif diff --git a/rtgui/common/hz12font.c b/rtgui/common/hz12font.c index e51ed16e47..5ea8bf57ac 100644 --- a/rtgui/common/hz12font.c +++ b/rtgui/common/hz12font.c @@ -1,5 +1,5 @@ #include <rtgui/font.h> -
+ #ifdef RTGUI_USING_FONT12 #ifndef RTGUI_USING_HZ_FILE const unsigned char hz12_font[] = { @@ -12295,21 +12295,21 @@ struct rtgui_font rtgui_font_hz12 = #else const struct rtgui_hz_file_font hz12 = { - {RT_NULL}, /* cache root */ - 0, /* cache_size */ - 12, /* font_size */ - -1, /* fd */ - "/resource/hz12" /* font_fn */ + {RT_NULL}, /* cache root */ + 0, /* cache size */ + 12, /* font size */ + 32, /* font data size */ + -1, /* fd */ + "/resource/hzk12.fnt" /* font_fn */ }; -extern struct rtgui_hz_file_font_engine hz_file_font_engine; struct rtgui_font rtgui_font_hz12 = { "hz", /* family */ 12, /* height */ 1, /* refer count */ - &hz_file_font_engine,/* font engine */ + &rtgui_hz_file_font_engine,/* font engine */ (void*)&hz12, /* font private data */ }; #endif -#endif
+#endif diff --git a/rtgui/common/hz16font.c b/rtgui/common/hz16font.c index 8f316f13fd..07c8e3f888 100644 --- a/rtgui/common/hz16font.c +++ b/rtgui/common/hz16font.c @@ -1,4 +1,4 @@ -#include <rtgui/font.h>
+#include <rtgui/font.h> #ifdef RTGUI_USING_FONT16 #ifndef RTGUI_USING_HZ_FILE @@ -16740,7 +16740,7 @@ const struct rtgui_font_bitmap hz16 = }; extern struct rtgui_font_engine hz_bmp_font_engine; -const struct rtgui_font rtgui_font_hz16 = +struct rtgui_font rtgui_font_hz16 = { "hz", /* family */ 16, /* height */ @@ -16755,7 +16755,7 @@ struct rtgui_hz_file_font hz16 = { {RT_NULL}, /* cache root */ 0, /* cache size */ - 16, /* font size */
+ 16, /* font size */ 32, /* font data size */ -1, /* fd */ "/resource/hzk16.fnt" /* font_fn */ @@ -16770,4 +16770,4 @@ struct rtgui_font rtgui_font_hz16 = (void*)&hz16, /* font private data */ }; #endif -#endif
+#endif diff --git a/rtgui/common/image.c b/rtgui/common/image.c index 817120d7da..64e2c992e2 100644 --- a/rtgui/common/image.c +++ b/rtgui/common/image.c @@ -12,11 +12,11 @@ * 2009-10-16 Bernard first version */ #include <rtthread.h> -#include <rtgui/image.h>
+#include <rtgui/image.h> -#include <rtgui/image_xpm.h>
+#include <rtgui/image_xpm.h> #include <rtgui/image_hdc.h> -#include <rtgui/rtgui_system.h>
+#include <rtgui/rtgui_system.h> #include <string.h> @@ -37,7 +37,7 @@ void rtgui_system_image_init(void) { /* always support XPM image */ rtgui_image_xpm_init(); - rtgui_image_hdc_init();
+ rtgui_image_hdc_init(); #ifdef RTGUI_IMAGE_BMP rtgui_image_bmp_init(); diff --git a/rtgui/common/image_hdc.c b/rtgui/common/image_hdc.c index 310725db4e..db5ad6b2df 100644 --- a/rtgui/common/image_hdc.c +++ b/rtgui/common/image_hdc.c @@ -77,7 +77,7 @@ static rt_bool_t rtgui_image_hdc_load(struct rtgui_image* image, struct rtgui_fi rtgui_filerw_read(file, (char*)&header, 1, sizeof(header)); /* set image information */ - image->w = header[1]; image->h = header[2]; + image->w = (rt_uint16_t)header[1]; image->h = (rt_uint16_t)header[2]; image->engine = &rtgui_image_hdc_engine; image->data = hdc; hdc->filerw = file; @@ -104,8 +104,8 @@ static rt_bool_t rtgui_image_hdc_load(struct rtgui_image* image, struct rtgui_fi else { hdc->pixels = RT_NULL; - }
-
+ } + return RT_TRUE; } @@ -129,18 +129,60 @@ static void rtgui_image_hdc_unload(struct rtgui_image* image) } } +static void rtgui_image_hdc_raw_hline(struct rtgui_dc_hw* dc, rt_uint8_t* raw_ptr, int x1, int x2, int y) +{ + register rt_base_t index; + register rt_base_t bpp; + + /* convert logic to device */ + x1 = x1 + dc->owner->extent.x1; + x2 = x2 + dc->owner->extent.x1; + y = y + dc->owner->extent.y1; + + bpp = dc->device->byte_per_pixel; + if (dc->owner->clip.data == RT_NULL) + { + rtgui_rect_t* prect; + + prect = &(dc->owner->clip.extents); + + /* calculate hline intersect */ + if (prect->y1 > y || prect->y2 <= y ) return; + if (prect->x2 <= x1 || prect->x1 > x2) return; + + if (prect->x1 > x1) x1 = prect->x1; + if (prect->x2 < x2) x2 = prect->x2; + + /* draw raw hline */ + dc->device->draw_raw_hline(raw_ptr, x1, x2, y); + } + else for (index = 0; index < rtgui_region_num_rects(&(dc->owner->clip)); index ++) + { + rtgui_rect_t* prect; + register rt_base_t draw_x1, draw_x2; + + prect = ((rtgui_rect_t *)(dc->owner->clip.data + index + 1)); + draw_x1 = x1; + draw_x2 = x2; + + /* calculate hline clip */ + if (prect->y1 > y || prect->y2 <= y ) continue; + if (prect->x2 <= x1 || prect->x1 > x2) continue; + + if (prect->x1 > x1) draw_x1 = prect->x1; + if (prect->x2 < x2) draw_x2 = prect->x2; + + /* draw raw hline */ + dc->device->draw_raw_hline(raw_ptr + (draw_x1 - x1) * bpp, draw_x1, draw_x2, y); + } +} + static void rtgui_image_hdc_blit(struct rtgui_image* image, struct rtgui_dc* dc, struct rtgui_rect* dst_rect) { rt_uint16_t y, w, h; - rtgui_color_t foreground; struct rtgui_image_hdc* hdc; - rt_uint16_t rect_pitch, hw_pitch; - rtgui_rect_t dc_rect, _rect, *rect; - - RT_ASSERT(image != RT_NULL || dc != RT_NULL || dst_rect != RT_NULL);
- _rect = *dst_rect;
- rect = &_rect;
+ RT_ASSERT(image != RT_NULL || dc != RT_NULL || dst_rect != RT_NULL); /* this dc is not visible */ if (dc->get_visible(dc) != RT_TRUE) return; @@ -148,107 +190,47 @@ static void rtgui_image_hdc_blit(struct rtgui_image* image, struct rtgui_dc* dc, hdc = (struct rtgui_image_hdc*) image->data; RT_ASSERT(hdc != RT_NULL); - /* transfer logic coordinate to physical coordinate */ - if (dc->type == RTGUI_DC_HW) - { - dc_rect = ((struct rtgui_dc_hw*)dc)->owner->extent; - } - else rtgui_dc_get_rect(dc, &dc_rect); - rtgui_rect_moveto(rect, dc_rect.x1, dc_rect.y1); + if (dc->type != RTGUI_DC_HW) return; /* the minimum rect */ - if (image->w < rtgui_rect_width(*rect)) w = image->w; - else w = rtgui_rect_width(*rect); - if (image->h < rtgui_rect_height(*rect)) h = image->h; - else h = rtgui_rect_height(*rect); - - /* get rect pitch */ - rect_pitch = w * hdc->byte_per_pixel; - hw_pitch = hdc->hw_driver->width * hdc->hw_driver->byte_per_pixel; - - /* save foreground color */ - foreground = rtgui_dc_get_color(dc); + if (image->w < rtgui_rect_width(*dst_rect)) w = image->w; + else w = rtgui_rect_width(*dst_rect); + if (image->h < rtgui_rect_height(*dst_rect)) h = image->h; + else h = rtgui_rect_height(*dst_rect); if (hdc->pixels != RT_NULL) { - if (hdc->hw_driver->get_framebuffer() != RT_NULL) - { - rt_uint8_t* rect_ptr; - rt_uint8_t* hw_ptr; + rt_uint8_t* ptr; - /* get pixel pointer */ - hw_ptr = hdc->hw_driver->get_framebuffer(); - rect_ptr = hdc->pixels; + /* get pixel pointer */ + ptr = hdc->pixels; - /* move hardware pixel pointer */ - hw_ptr += rect->y1 * hdc->pitch + rect->x1 * hdc->byte_per_pixel; - - for (y = 0; y < h; y ++) - { - rt_memcpy(hw_ptr, rect_ptr, rect_pitch); - hw_ptr += hw_pitch; - rect_ptr += rect_pitch; - } - } - else + for (y = 0; y < h; y ++) { - rt_uint8_t* rect_ptr; - - /* get pixel pointer */ - rect_ptr = hdc->pixels; - - for (y = 0; y < h; y ++) - { - hdc->hw_driver->draw_raw_hline(rect_ptr, rect->x1, rect->x1 + w, rect->y1 + y); - rect_ptr += hdc->pitch; - } + rtgui_image_hdc_raw_hline((struct rtgui_dc_hw*)dc, ptr, dst_rect->x1, dst_rect->x2, dst_rect->y1 + y); + ptr += hdc->pitch; } } else { - rt_uint8_t* rect_ptr; - rect_ptr = rtgui_malloc(hdc->pitch); - if (rect_ptr == RT_NULL) return; /* no memory */ + rt_uint8_t* ptr; + ptr = rtgui_malloc(hdc->pitch); + if (ptr == RT_NULL) return; /* no memory */ /* seek to the begin of pixel data */ rtgui_filerw_seek(hdc->filerw, hdc->pixel_offset, RTGUI_FILE_SEEK_SET); - if (hdc->hw_driver->get_framebuffer() != RT_NULL) - { - rt_uint8_t* hw_ptr; - - /* get pixel pointer */ - hw_ptr = hdc->hw_driver->get_framebuffer(); - /* move hardware pixel pointer */ - hw_ptr += rect->y1 * hdc->pitch + rect->x1 * hdc->byte_per_pixel; - - for (y = 0; y < h; y ++) - { - /* read pixel data */ - if (rtgui_filerw_read(hdc->filerw, rect_ptr, 1, hdc->pitch) != hdc->pitch) - break; /* read data failed */ - - rt_memcpy(hw_ptr, rect_ptr, rect_pitch); - hw_ptr += hw_pitch; - } - } - else + for (y = 0; y < h; y ++) { - for (y = 0; y < h; y ++) - { - /* read pixel data */ - if (rtgui_filerw_read(hdc->filerw, rect_ptr, 1, hdc->pitch) != hdc->pitch) - break; /* read data failed */ + /* read pixel data */ + if (rtgui_filerw_read(hdc->filerw, ptr, 1, hdc->pitch) != hdc->pitch) + break; /* read data failed */ - hdc->hw_driver->draw_raw_hline(rect_ptr, rect->x1, rect->x1 + w, rect->y1 + y); - } + rtgui_image_hdc_raw_hline((struct rtgui_dc_hw*)dc, ptr, dst_rect->x1, dst_rect->x1 + w, dst_rect->y1 + y); } - rtgui_free(rect_ptr); + rtgui_free(ptr); } - - /* restore foreground */ - rtgui_dc_set_color(dc, foreground); } void rtgui_image_hdc_init() diff --git a/rtgui/common/image_png.c b/rtgui/common/image_png.c index 95dc817509..ae9135c9f7 100644 --- a/rtgui/common/image_png.c +++ b/rtgui/common/image_png.c @@ -199,8 +199,8 @@ static rt_bool_t rtgui_image_png_load(struct rtgui_image* image, struct rtgui_fi else { png->pixels = RT_NULL; - }
-
+ } + return RT_TRUE; } diff --git a/rtgui/common/image_xpm.c b/rtgui/common/image_xpm.c index 4b171fd674..6200023991 100644 --- a/rtgui/common/image_xpm.c +++ b/rtgui/common/image_xpm.c @@ -565,7 +565,7 @@ color_none: } } - free_colorhash(colors_table);
+ free_colorhash(colors_table); rtgui_filerw_close(file); return RT_TRUE; diff --git a/rtgui/common/rtgui_object.c b/rtgui/common/rtgui_object.c index a0f0d71fba..4bbc221c17 100644 --- a/rtgui/common/rtgui_object.c +++ b/rtgui/common/rtgui_object.c @@ -13,7 +13,7 @@ */ #include <rtgui/rtgui_object.h> -#include <rtgui/rtgui_system.h>
+#include <rtgui/rtgui_system.h> static void _rtgui_object_constructor(rtgui_object_t *object) { diff --git a/rtgui/common/rtgui_system.c b/rtgui/common/rtgui_system.c index 60808da44d..3ee1e8ff1d 100644 --- a/rtgui/common/rtgui_system.c +++ b/rtgui/common/rtgui_system.c @@ -15,6 +15,7 @@ #include <rtgui/rtgui.h> #include <rtgui/driver.h> #include <rtgui/image.h> +#include <rtgui/rtgui_theme.h> #include <rtgui/rtgui_system.h> #include <rtgui/rtgui_server.h> #include <rtgui/widgets/window.h> @@ -31,6 +32,9 @@ void rtgui_system_server_init() /* init rtgui_thread */ rtgui_thread_system_init(); + /* init theme */ + rtgui_system_theme_init(); + /* init image */ rtgui_system_image_init(); /* init font */ @@ -257,7 +261,6 @@ rtgui_thread_t* rtgui_thread_register(rt_thread_t tid, rt_mq_t mq) thread->tid = tid; thread->mq = mq; thread->widget = RT_NULL; - thread->is_quit = RT_FALSE; /* take semaphore */ rt_sem_take(&_rtgui_thread_hash_semaphore, RT_WAITING_FOREVER); diff --git a/rtgui/common/rtgui_theme.c b/rtgui/common/rtgui_theme.c index a99030219d..b7d34966c4 100644 --- a/rtgui/common/rtgui_theme.c +++ b/rtgui/common/rtgui_theme.c @@ -1,454 +1,496 @@ -/*
- * File : rtgui_theme.c
- * This file is part of RTGUI in RT-Thread RTOS
- * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team
- *
- * The license and distribution terms for this file may be
- * found in the file LICENSE in this distribution or at
- * http://www.rt-thread.org/license/LICENSE
- *
- * Change Logs:
- * Date Author Notes
- * 2009-10-04 Bernard first version
- */
-
-#include <rtgui/rtgui.h>
-#include <rtgui/dc.h>
-#include <rtgui/widgets/widget.h>
-#include <rtgui/widgets/button.h>
-#include <rtgui/widgets/label.h>
-#include <rtgui/widgets/textbox.h>
-#include <rtgui/widgets/iconbox.h>
-#include <rtgui/widgets/title.h>
-#include <rtgui/rtgui_theme.h>
-#include <rtgui/rtgui_server.h>
-
-#define WINTITLE_CB_WIDTH 14
-#define WINTITLE_CB_HEIGHT 14
-
-static const rt_uint8_t *close_unpressed_xpm[] = {
- "14 14 55 1",
- " c None",
- ". c #DCDFEA",
- "+ c #A4ADD3",
- "@ c #8F9ACA",
- "# c #98A2CD",
- "$ c #D2D6E6",
- "% c #F7F7F7",
- "& c #F6F6F6",
- "* c #B9C1D0",
- "= c #7A8AAA",
- "- c #D6DAE2",
- "; c #D8DCE3",
- "> c #7485A5",
- ", c #455C89",
- "' c #516690",
- ") c #D3D8E0",
- "! c #536891",
- "~ c #6D7FA1",
- "{ c #F5F5F5",
- "] c #D1D6DF",
- "^ c #D2D7DF",
- "/ c #D5D9E1",
- "( c #4E648E",
- "_ c #CFD4DE",
- ": c #F4F4F4",
- "< c #D0D5DE",
- "[ c #CED3DD",
- "} c #F3F3F3",
- "| c #CFD4DD",
- "1 c #CDD2DC",
- "2 c #F2F2F2",
- "3 c #D3D7DF",
- "4 c #526790",
- "5 c #D0D5DD",
- "6 c #F1F1F1",
- "7 c #D2D6DE",
- "8 c #CFD4DC",
- "9 c #F0F0F0",
- "0 c #D1D5DD",
- "a c #C9CED8",
- "b c #CDD2DB",
- "c c #50658F",
- "d c #CED3DB",
- "e c #7283A3",
- "f c #6E80A2",
- "g c #EFEFEF",
- "h c #B2BACA",
- "i c #7081A2",
- "j c #C8CDD7",
- "k c #CCD1DA",
- "l c #ACB5C7",
- "m c #D0D4E2",
- "n c #EEEEEE",
- "o c #D2D5E3",
- "p c #97A1CC",
- ".+@@@@@@@@@@+.",
- "#$%%%%%%%%%%$#",
- "@&*=-&&&&;=*&@",
- "@&>,')&&-!,~&@",
- "@{]','^/!,(_{@",
- "@::<','!,([::@",
- "@}}}|',,(1}}}@",
- "@22234,,'5222@",
- "@6674,(','866@",
- "@904,(abc,cd9@",
- "@9e,(a99bc,f9@",
- "@ghijggggkelg@",
- "#mnnnnnnnnnnm#",
- "op@@@@@@@@@@po"};
-
-static const rt_uint8_t *close_pressed_xpm[] = {
- "14 14 66 1",
- " c None",
- ". c #CED4EE",
- "+ c #7E90DD",
- "@ c #6076D7",
- "# c #6C80D9",
- "$ c #BFC8EA",
- "% c #F2F3F5",
- "& c #F0F2F3",
- "* c #A5B6D7",
- "= c #587ABB",
- "- c #C9D3E4",
- "; c #CBD5E4",
- "> c #EEF0F2",
- ", c #5073B7",
- "' c #1746A3",
- ") c #2551A8",
- "! c #C5CFE2",
- "~ c #C8D1E3",
- "{ c #2853A8",
- "] c #496DB4",
- "^ c #ECEEF1",
- "/ c #C0CCE0",
- "( c #C3CEE1",
- "_ c #C6D0E2",
- ": c #224FA7",
- "< c #BEC9DF",
- "[ c #EAECF0",
- "} c #BFCAE0",
- "| c #2551A7",
- "1 c #224EA7",
- "2 c #BCC8DF",
- "3 c #E8EBEE",
- "4 c #BDCADE",
- "5 c #BAC7DD",
- "6 c #E6E9ED",
- "7 c #C1CBDF",
- "8 c #2753A8",
- "9 c #BECADE",
- "0 c #E4E7EB",
- "a c #BFCADD",
- "b c #224EA6",
- "c c #BDC8DC",
- "d c #E1E5EA",
- "e c #2752A8",
- "f c #B3C0D9",
- "g c #B8C5DB",
- "h c #2451A7",
- "i c #BAC6DB",
- "j c #DFE2E8",
- "k c #4C70B4",
- "l c #B2BED8",
- "m c #B6C2D9",
- "n c #2450A7",
- "o c #486BB3",
- "p c #DCE0E7",
- "q c #96A8CE",
- "r c #496CB3",
- "s c #AFBCD7",
- "t c #B4C1D8",
- "u c #4B6FB4",
- "v c #8EA4CC",
- "w c #6C80D8",
- "x c #B4BEDF",
- "y c #DADEE5",
- "z c #B5BEDE",
- "A c #6A7ED7",
- ".+@@@@@@@@@@+.",
- "#$%%%%%%%%%%$#",
- "@&*=-&&&&;=*&@",
- "@>,')!>>~{']>@",
- "@^/)')(_{':<^@",
- "@[[}|'|{'12[[@",
- "@3334|''15333@",
- "@66678''|9666@",
- "@00a8'b|'|c00@",
- "@dce'bfgh'hid@",
- "@jk'bljjmn'oj@",
- "@pqrspppptuvp@",
- "wxyyyyyyyyyyxw",
- "zA@@@@@@@@@@Az"};
-
-static rtgui_image_t* close_pressed = RT_NULL;
-static rtgui_image_t* close_unpressed = RT_NULL;
-
-/* window drawing */
-void rtgui_theme_draw_win(struct rtgui_topwin* win)
-{
- struct rtgui_dc* dc;
- rtgui_rect_t rect;
-
- /* init close box image */
- if (close_pressed == RT_NULL)
- close_pressed = rtgui_image_create_from_mem("xpm",
- (const rt_uint8_t*)close_pressed_xpm, sizeof(close_pressed_xpm));
- if (close_unpressed == RT_NULL)
- close_unpressed = rtgui_image_create_from_mem("xpm",
- (const rt_uint8_t*)close_unpressed_xpm, sizeof(close_unpressed_xpm));
-
- /* begin drawing */
- dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(win->title));
-
- /* get rect */
- rtgui_widget_get_rect(RTGUI_WIDGET(win->title), &rect);
-
- /* draw border */
- if (win->flag & WINTITLE_BORDER)
- {
- RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(win->title)) = RTGUI_RGB(219, 210, 243);
- rtgui_dc_draw_rect(dc, &rect);
-
- /* shrink border */
- rect.x1 += WINTITLE_BORDER_SIZE;
- rect.y1 += WINTITLE_BORDER_SIZE;
- rect.x2 -= WINTITLE_BORDER_SIZE;
- rect.y2 -= WINTITLE_BORDER_SIZE;
- }
-
- /* draw title */
- if (!(win->flag & WINTITLE_NO))
- {
- if (win->flag & WINTITLE_ACTIVATE)
- {
- RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(win->title)) = RTGUI_RGB(229, 236, 249);
- RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(win->title)) = RTGUI_RGB( 51, 102, 204);
- }
- else
- {
- RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(win->title)) = RTGUI_RGB(242, 245, 252);
- RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(win->title)) = RTGUI_RGB(153, 178, 229);
- }
- rtgui_dc_fill_rect(dc, &rect);
-
- rect.x1 += 4;
- rect.y1 += 2;
- rtgui_dc_draw_text(dc, rtgui_wintitle_get_title(win->title), &rect);
-
- if (win->flag & WINTITLE_CLOSEBOX)
- {
- /* get close button rect */
- rect.x1 = rtgui_rect_width(RTGUI_WIDGET(win->title)->extent) -
- WINTITLE_BORDER_SIZE - WINTITLE_CB_WIDTH - 3;
- rect.y1 = 3;
- rect.x2 = rect.x1 + WINTITLE_CB_WIDTH;
- rect.y2 = rect.y1 + WINTITLE_CB_HEIGHT;
-
- /* draw close box */
- if(win->flag & WINTITLE_CB_PRESSED) rtgui_image_blit(close_pressed, dc, &rect);
- else rtgui_image_blit(close_unpressed, dc, &rect);
- }
- }
-
- rtgui_dc_end_drawing(dc);
-}
-
-/* widget drawing */
-void rtgui_theme_draw_button(rtgui_button_t* btn)
-{
- /* draw button */
- struct rtgui_dc* dc;
- struct rtgui_rect rect;
-
- /* begin drawing */
- dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(btn));
- if (dc == RT_NULL) return;
-
- rtgui_widget_get_rect(RTGUI_WIDGET(btn), &rect);
-
- /* fill button rect with background color */
- // RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(btn)) = RTGUI_RGB(212, 208, 200);
- rtgui_dc_fill_rect(dc, &rect);
-
- if (btn->flag & RTGUI_BUTTON_TYPE_PUSH && btn->flag & RTGUI_BUTTON_FLAG_PRESS)
- {
- /* draw border */
- RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(btn)) = RTGUI_RGB(64, 64, 64);
- rtgui_dc_draw_hline(dc, rect.x1, rect.x2, rect.y1);
- rtgui_dc_draw_vline(dc, rect.x1, rect.y1, rect.y2);
-
- RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(btn)) = RTGUI_RGB(128, 128, 128);
- rtgui_dc_draw_hline(dc, rect.x1, rect.x2 - 1, rect.y1 + 1);
- rtgui_dc_draw_vline(dc, rect.x1 + 1, rect.y1 + 1, rect.y2 - 2);
-
- RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(btn)) = RTGUI_RGB(255, 255, 255);
- rtgui_dc_draw_hline(dc, rect.x1, rect.x2 + 1, rect.y2 - 1);
- rtgui_dc_draw_vline(dc, rect.x2 - 1, rect.y1, rect.y2);
-
- if (btn->pressed_image != RT_NULL)
- {
- rtgui_rect_t image_rect = {0, 0, btn->unpressed_image->w, btn->unpressed_image->h};
- rtgui_rect_moveto_align(&rect, &image_rect, RTGUI_ALIGN_CENTER_HORIZONTAL | RTGUI_ALIGN_CENTER_VERTICAL);
-
- rtgui_image_blit(btn->pressed_image, dc, &image_rect);
- }
- }
- else if (btn->flag & RTGUI_BUTTON_FLAG_PRESS)
- {
- if (btn->pressed_image != RT_NULL)
- {
- rtgui_rect_t image_rect = {0, 0, btn->unpressed_image->w, btn->unpressed_image->h};
- rtgui_rect_moveto_align(&rect, &image_rect, RTGUI_ALIGN_CENTER_HORIZONTAL | RTGUI_ALIGN_CENTER_VERTICAL);
-
- rtgui_image_blit(btn->pressed_image, dc, &image_rect);
- }
- else
- {
- /* draw border */
- RTGUI_WIDGET(btn)->gc.foreground = RTGUI_RGB(0, 0, 0);
- rtgui_dc_draw_rect(dc, &rect);
-
- RTGUI_WIDGET(btn)->gc.foreground = RTGUI_RGB(128, 128, 128);
- rect.x1 += 1; rect.y1 += 1; rect.x2 -= 1; rect.y2 -= 1;
- rtgui_dc_draw_rect(dc, &rect);
- }
- }
- else
- {
- if (btn->unpressed_image != RT_NULL)
- {
- rtgui_rect_t image_rect = {0, 0, btn->unpressed_image->w, btn->unpressed_image->h};
- rtgui_rect_moveto_align(&rect, &image_rect, RTGUI_ALIGN_CENTER_HORIZONTAL | RTGUI_ALIGN_CENTER_VERTICAL);
-
- rtgui_image_blit(btn->unpressed_image, dc, &image_rect);
- }
- else
- {
- /* draw border */
- RTGUI_WIDGET(btn)->gc.foreground = RTGUI_RGB(255, 255, 255);
- rtgui_dc_draw_hline(dc, rect.x1, rect.x2, rect.y1);
- rtgui_dc_draw_vline(dc, rect.x1, rect.y1, rect.y2);
-
- RTGUI_WIDGET(btn)->gc.foreground = RTGUI_RGB(0, 0, 0);
- rtgui_dc_draw_hline(dc, rect.x1, rect.x2 + 1, rect.y2);
- rtgui_dc_draw_vline(dc, rect.x2, rect.y1, rect.y2);
-
- RTGUI_WIDGET(btn)->gc.foreground = RTGUI_RGB(128, 128, 128);
- rtgui_dc_draw_hline(dc, rect.x1 + 1, rect.x2, rect.y2 - 1);
- rtgui_dc_draw_vline(dc, rect.x2 - 1, rect.y1 + 1, rect.y2 - 1);
- }
- }
-
- if (btn->pressed_image == RT_NULL)
- {
- /* re-set foreground and get default rect */
- RTGUI_WIDGET(btn)->gc.foreground = RTGUI_RGB(0, 0, 0);
- rtgui_widget_get_rect(RTGUI_WIDGET(btn), &rect);
-
- /* remove border */
- rtgui_rect_inflate(&rect, -2);
-
- /* draw text */
- rtgui_dc_draw_text(dc, rtgui_label_get_text(RTGUI_LABEL(btn)), &rect);
- }
-
- /* end drawing */
- rtgui_dc_end_drawing(dc);
-}
-
-void rtgui_theme_draw_label(rtgui_label_t* label)
-{
- /* draw label */
- struct rtgui_dc* dc;
- struct rtgui_rect rect;
-
- /* begin drawing */
- dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(label));
- if (dc == RT_NULL) return;
-
- rtgui_widget_get_rect(RTGUI_WIDGET(label), &rect);
- rtgui_dc_fill_rect(dc, &rect);
-
- /* default left and center draw */
- rect.y1 = rect.y1 + (rtgui_rect_height(rect) - 8)/2;
- rtgui_dc_draw_text(dc, rtgui_label_get_text(label), &rect);
-
- /* end drawing */
- rtgui_dc_end_drawing(dc);
-}
-
-#define RTGUI_TEXTBOX_MARGIN 3
-void rtgui_theme_draw_textbox(rtgui_textbox_t* box)
-{
- /* draw button */
- struct rtgui_dc* dc;
- struct rtgui_rect rect;
-
- /* begin drawing */
- dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(box));
- if (dc == RT_NULL) return;
-
- /* get widget rect */
- rtgui_widget_get_rect(RTGUI_WIDGET(box), &rect);
-
- /* fill widget rect with background color */
- rtgui_dc_fill_rect(dc, &rect);
-
- /* draw border */
- rtgui_dc_draw_border(dc, &rect, RTGUI_BORDER_STATIC);
-
- /* draw text */
- if (box->text != RT_NULL)
- {
- rect.x1 += RTGUI_TEXTBOX_MARGIN;
-
- rtgui_dc_draw_text(dc, box->text, &rect);
- }
-
- /* end drawing */
- rtgui_dc_end_drawing(dc);
-}
-
-void rtgui_theme_draw_iconbox(rtgui_iconbox_t* iconbox)
-{
- struct rtgui_dc* dc;
- struct rtgui_rect rect;
-
- /* begin drawing */
- dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(iconbox));
- if (dc == RT_NULL) return;
-
- /* get widget rect */
- rtgui_widget_get_rect(RTGUI_WIDGET(iconbox), &rect);
-
- /* draw icon */
- rtgui_image_blit(iconbox->image, dc, &rect);
-
- /* draw text */
- if (iconbox->text_position == RTGUI_ICONBOX_TEXT_BELOW && iconbox->text != RT_NULL)
- {
- rect.y1 = iconbox->image->h + RTGUI_WIDGET_DEFAULT_MARGIN;
- rtgui_dc_draw_text(dc, iconbox->text, &rect);
- }
- else if (iconbox->text_position == RTGUI_ICONBOX_TEXT_RIGHT && iconbox->text != RT_NULL)
- {
- rect.x1 = iconbox->image->w + RTGUI_WIDGET_DEFAULT_MARGIN;
- rtgui_dc_draw_text(dc, iconbox->text, &rect);
- }
-
- /* end drawing */
- rtgui_dc_end_drawing(dc);
-}
-
-/* get default background color */
-rtgui_color_t rtgui_theme_default_bc()
-{
- return default_background;
-}
-
-/* get default foreground color */
-rtgui_color_t rtgui_theme_default_fc()
-{
- return default_foreground;
-}
+/* + * File : rtgui_theme.c + * This file is part of RTGUI in RT-Thread RTOS + * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rt-thread.org/license/LICENSE + * + * Change Logs: + * Date Author Notes + * 2009-10-04 Bernard first version + */ + +#include <rtgui/rtgui.h> +#include <rtgui/dc.h> +#include <rtgui/widgets/widget.h> +#include <rtgui/widgets/button.h> +#include <rtgui/widgets/label.h> +#include <rtgui/widgets/textbox.h> +#include <rtgui/widgets/iconbox.h> +#include <rtgui/widgets/title.h> +#include <rtgui/rtgui_theme.h> +#include <rtgui/rtgui_server.h> + +#define WINTITLE_CB_WIDTH 14 +#define WINTITLE_CB_HEIGHT 14 +#define SELECTED_HEIGHT 25 + +static const rt_uint8_t *close_unpressed_xpm[] = { + "14 14 55 1", + " c None", + ". c #DCDFEA", + "+ c #A4ADD3", + "@ c #8F9ACA", + "# c #98A2CD", + "$ c #D2D6E6", + "% c #F7F7F7", + "& c #F6F6F6", + "* c #B9C1D0", + "= c #7A8AAA", + "- c #D6DAE2", + "; c #D8DCE3", + "> c #7485A5", + ", c #455C89", + "' c #516690", + ") c #D3D8E0", + "! c #536891", + "~ c #6D7FA1", + "{ c #F5F5F5", + "] c #D1D6DF", + "^ c #D2D7DF", + "/ c #D5D9E1", + "( c #4E648E", + "_ c #CFD4DE", + ": c #F4F4F4", + "< c #D0D5DE", + "[ c #CED3DD", + "} c #F3F3F3", + "| c #CFD4DD", + "1 c #CDD2DC", + "2 c #F2F2F2", + "3 c #D3D7DF", + "4 c #526790", + "5 c #D0D5DD", + "6 c #F1F1F1", + "7 c #D2D6DE", + "8 c #CFD4DC", + "9 c #F0F0F0", + "0 c #D1D5DD", + "a c #C9CED8", + "b c #CDD2DB", + "c c #50658F", + "d c #CED3DB", + "e c #7283A3", + "f c #6E80A2", + "g c #EFEFEF", + "h c #B2BACA", + "i c #7081A2", + "j c #C8CDD7", + "k c #CCD1DA", + "l c #ACB5C7", + "m c #D0D4E2", + "n c #EEEEEE", + "o c #D2D5E3", + "p c #97A1CC", + ".+@@@@@@@@@@+.", + "#$%%%%%%%%%%$#", + "@&*=-&&&&;=*&@", + "@&>,')&&-!,~&@", + "@{]','^/!,(_{@", + "@::<','!,([::@", + "@}}}|',,(1}}}@", + "@22234,,'5222@", + "@6674,(','866@", + "@904,(abc,cd9@", + "@9e,(a99bc,f9@", + "@ghijggggkelg@", + "#mnnnnnnnnnnm#", + "op@@@@@@@@@@po"}; + +static const rt_uint8_t *close_pressed_xpm[] = { + "14 14 66 1", + " c None", + ". c #CED4EE", + "+ c #7E90DD", + "@ c #6076D7", + "# c #6C80D9", + "$ c #BFC8EA", + "% c #F2F3F5", + "& c #F0F2F3", + "* c #A5B6D7", + "= c #587ABB", + "- c #C9D3E4", + "; c #CBD5E4", + "> c #EEF0F2", + ", c #5073B7", + "' c #1746A3", + ") c #2551A8", + "! c #C5CFE2", + "~ c #C8D1E3", + "{ c #2853A8", + "] c #496DB4", + "^ c #ECEEF1", + "/ c #C0CCE0", + "( c #C3CEE1", + "_ c #C6D0E2", + ": c #224FA7", + "< c #BEC9DF", + "[ c #EAECF0", + "} c #BFCAE0", + "| c #2551A7", + "1 c #224EA7", + "2 c #BCC8DF", + "3 c #E8EBEE", + "4 c #BDCADE", + "5 c #BAC7DD", + "6 c #E6E9ED", + "7 c #C1CBDF", + "8 c #2753A8", + "9 c #BECADE", + "0 c #E4E7EB", + "a c #BFCADD", + "b c #224EA6", + "c c #BDC8DC", + "d c #E1E5EA", + "e c #2752A8", + "f c #B3C0D9", + "g c #B8C5DB", + "h c #2451A7", + "i c #BAC6DB", + "j c #DFE2E8", + "k c #4C70B4", + "l c #B2BED8", + "m c #B6C2D9", + "n c #2450A7", + "o c #486BB3", + "p c #DCE0E7", + "q c #96A8CE", + "r c #496CB3", + "s c #AFBCD7", + "t c #B4C1D8", + "u c #4B6FB4", + "v c #8EA4CC", + "w c #6C80D8", + "x c #B4BEDF", + "y c #DADEE5", + "z c #B5BEDE", + "A c #6A7ED7", + ".+@@@@@@@@@@+.", + "#$%%%%%%%%%%$#", + "@&*=-&&&&;=*&@", + "@>,')!>>~{']>@", + "@^/)')(_{':<^@", + "@[[}|'|{'12[[@", + "@3334|''15333@", + "@66678''|9666@", + "@00a8'b|'|c00@", + "@dce'bfgh'hid@", + "@jk'bljjmn'oj@", + "@pqrspppptuvp@", + "wxyyyyyyyyyyxw", + "zA@@@@@@@@@@Az"}; + +static rtgui_image_t* close_pressed = RT_NULL; +static rtgui_image_t* close_unpressed = RT_NULL; + +/* init theme */ +void rtgui_system_theme_init() +{ + close_pressed = rtgui_image_create_from_mem("xpm", + (const rt_uint8_t*)close_pressed_xpm, sizeof(close_pressed_xpm)); + close_unpressed = rtgui_image_create_from_mem("xpm", + (const rt_uint8_t*)close_unpressed_xpm, sizeof(close_unpressed_xpm)); +} + +/* window drawing */ +void rtgui_theme_draw_win(struct rtgui_topwin* win) +{ + struct rtgui_dc* dc; + rtgui_rect_t rect; + + /* init close box image */ + if (close_pressed == RT_NULL) + close_pressed = rtgui_image_create_from_mem("xpm", + (const rt_uint8_t*)close_pressed_xpm, sizeof(close_pressed_xpm)); + if (close_unpressed == RT_NULL) + close_unpressed = rtgui_image_create_from_mem("xpm", + (const rt_uint8_t*)close_unpressed_xpm, sizeof(close_unpressed_xpm)); + + /* begin drawing */ + dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(win->title)); + + /* get rect */ + rtgui_widget_get_rect(RTGUI_WIDGET(win->title), &rect); + + /* draw border */ + if (win->flag & WINTITLE_BORDER) + { + RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(win->title)) = RTGUI_RGB(219, 210, 243); + rtgui_dc_draw_rect(dc, &rect); + + /* shrink border */ + rect.x1 += WINTITLE_BORDER_SIZE; + rect.y1 += WINTITLE_BORDER_SIZE; + rect.x2 -= WINTITLE_BORDER_SIZE; + rect.y2 -= WINTITLE_BORDER_SIZE; + } + + /* draw title */ + if (!(win->flag & WINTITLE_NO)) + { + if (win->flag & WINTITLE_ACTIVATE) + { + RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(win->title)) = RTGUI_RGB(229, 236, 249); + RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(win->title)) = RTGUI_RGB( 51, 102, 204); + } + else + { + RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(win->title)) = RTGUI_RGB(242, 245, 252); + RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(win->title)) = RTGUI_RGB(153, 178, 229); + } + rtgui_dc_fill_rect(dc, &rect); + + rect.x1 += 4; + rect.y1 += 2; + rtgui_dc_draw_text(dc, rtgui_wintitle_get_title(win->title), &rect); + + if (win->flag & WINTITLE_CLOSEBOX) + { + /* get close button rect */ + rect.x1 = rtgui_rect_width(RTGUI_WIDGET(win->title)->extent) - + WINTITLE_BORDER_SIZE - WINTITLE_CB_WIDTH - 3; + rect.y1 = 3; + rect.x2 = rect.x1 + WINTITLE_CB_WIDTH; + rect.y2 = rect.y1 + WINTITLE_CB_HEIGHT; + + /* draw close box */ + if(win->flag & WINTITLE_CB_PRESSED) rtgui_image_blit(close_pressed, dc, &rect); + else rtgui_image_blit(close_unpressed, dc, &rect); + } + } + + rtgui_dc_end_drawing(dc); +} + +/* widget drawing */ +void rtgui_theme_draw_button(rtgui_button_t* btn) +{ + /* draw button */ + struct rtgui_dc* dc; + struct rtgui_rect rect; + + /* begin drawing */ + dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(btn)); + if (dc == RT_NULL) return; + + rtgui_widget_get_rect(RTGUI_WIDGET(btn), &rect); + + /* fill button rect with background color */ + // RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(btn)) = RTGUI_RGB(212, 208, 200); + rtgui_dc_fill_rect(dc, &rect); + + if (btn->flag & RTGUI_BUTTON_TYPE_PUSH && btn->flag & RTGUI_BUTTON_FLAG_PRESS) + { + /* draw border */ + RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(btn)) = RTGUI_RGB(64, 64, 64); + rtgui_dc_draw_hline(dc, rect.x1, rect.x2, rect.y1); + rtgui_dc_draw_vline(dc, rect.x1, rect.y1, rect.y2); + + RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(btn)) = RTGUI_RGB(128, 128, 128); + rtgui_dc_draw_hline(dc, rect.x1, rect.x2 - 1, rect.y1 + 1); + rtgui_dc_draw_vline(dc, rect.x1 + 1, rect.y1 + 1, rect.y2 - 2); + + RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(btn)) = RTGUI_RGB(255, 255, 255); + rtgui_dc_draw_hline(dc, rect.x1, rect.x2 + 1, rect.y2 - 1); + rtgui_dc_draw_vline(dc, rect.x2 - 1, rect.y1, rect.y2); + + if (btn->pressed_image != RT_NULL) + { + rtgui_rect_t image_rect; + image_rect.x1 = 0; image_rect.y1 = 0; + image_rect.x2 = btn->unpressed_image->w; + image_rect.y2 = btn->unpressed_image->h; + rtgui_rect_moveto_align(&rect, &image_rect, RTGUI_ALIGN_CENTER_HORIZONTAL | RTGUI_ALIGN_CENTER_VERTICAL); + + rtgui_image_blit(btn->pressed_image, dc, &image_rect); + } + } + else if (btn->flag & RTGUI_BUTTON_FLAG_PRESS) + { + if (btn->pressed_image != RT_NULL) + { + rtgui_rect_t image_rect; + image_rect.x1 = 0; image_rect.y1 = 0; + image_rect.x2 = btn->unpressed_image->w; + image_rect.y2 = btn->unpressed_image->h; + rtgui_rect_moveto_align(&rect, &image_rect, RTGUI_ALIGN_CENTER_HORIZONTAL | RTGUI_ALIGN_CENTER_VERTICAL); + + rtgui_image_blit(btn->pressed_image, dc, &image_rect); + } + else + { + /* draw border */ + RTGUI_WIDGET(btn)->gc.foreground = RTGUI_RGB(0, 0, 0); + rtgui_dc_draw_rect(dc, &rect); + + RTGUI_WIDGET(btn)->gc.foreground = RTGUI_RGB(128, 128, 128); + rect.x1 += 1; rect.y1 += 1; rect.x2 -= 1; rect.y2 -= 1; + rtgui_dc_draw_rect(dc, &rect); + } + } + else + { + if (btn->unpressed_image != RT_NULL) + { + rtgui_rect_t image_rect; + image_rect.x1 = 0; image_rect.y1 = 0; + image_rect.x2 = btn->unpressed_image->w; + image_rect.y2 = btn->unpressed_image->h; + rtgui_rect_moveto_align(&rect, &image_rect, RTGUI_ALIGN_CENTER_HORIZONTAL | RTGUI_ALIGN_CENTER_VERTICAL); + + rtgui_image_blit(btn->unpressed_image, dc, &image_rect); + } + else + { + /* draw border */ + RTGUI_WIDGET(btn)->gc.foreground = RTGUI_RGB(255, 255, 255); + rtgui_dc_draw_hline(dc, rect.x1, rect.x2, rect.y1); + rtgui_dc_draw_vline(dc, rect.x1, rect.y1, rect.y2); + + RTGUI_WIDGET(btn)->gc.foreground = RTGUI_RGB(0, 0, 0); + rtgui_dc_draw_hline(dc, rect.x1, rect.x2 + 1, rect.y2); + rtgui_dc_draw_vline(dc, rect.x2, rect.y1, rect.y2); + + RTGUI_WIDGET(btn)->gc.foreground = RTGUI_RGB(128, 128, 128); + rtgui_dc_draw_hline(dc, rect.x1 + 1, rect.x2, rect.y2 - 1); + rtgui_dc_draw_vline(dc, rect.x2 - 1, rect.y1 + 1, rect.y2 - 1); + } + } + + if (btn->pressed_image == RT_NULL) + { + /* re-set foreground and get default rect */ + RTGUI_WIDGET(btn)->gc.foreground = RTGUI_RGB(0, 0, 0); + rtgui_widget_get_rect(RTGUI_WIDGET(btn), &rect); + + /* remove border */ + rtgui_rect_inflate(&rect, -2); + + /* draw text */ + rtgui_dc_draw_text(dc, rtgui_label_get_text(RTGUI_LABEL(btn)), &rect); + } + + /* end drawing */ + rtgui_dc_end_drawing(dc); +} + +void rtgui_theme_draw_label(rtgui_label_t* label) +{ + /* draw label */ + struct rtgui_dc* dc; + struct rtgui_rect rect; + + /* begin drawing */ + dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(label)); + if (dc == RT_NULL) return; + + rtgui_widget_get_rect(RTGUI_WIDGET(label), &rect); + rtgui_dc_fill_rect(dc, &rect); + + /* default left and center draw */ + rect.y1 = rect.y1 + (rtgui_rect_height(rect) - 8)/2; + rtgui_dc_draw_text(dc, rtgui_label_get_text(label), &rect); + + /* end drawing */ + rtgui_dc_end_drawing(dc); +} + +#define RTGUI_TEXTBOX_MARGIN 3 +void rtgui_theme_draw_textbox(rtgui_textbox_t* box) +{ + /* draw button */ + struct rtgui_dc* dc; + struct rtgui_rect rect; + + /* begin drawing */ + dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(box)); + if (dc == RT_NULL) return; + + /* get widget rect */ + rtgui_widget_get_rect(RTGUI_WIDGET(box), &rect); + + /* fill widget rect with background color */ + rtgui_dc_fill_rect(dc, &rect); + + /* draw border */ + rtgui_dc_draw_border(dc, &rect, RTGUI_BORDER_STATIC); + + /* draw text */ + if (box->text != RT_NULL) + { + rect.x1 += RTGUI_TEXTBOX_MARGIN; + + rtgui_dc_draw_text(dc, box->text, &rect); + } + + /* end drawing */ + rtgui_dc_end_drawing(dc); +} + +void rtgui_theme_draw_iconbox(rtgui_iconbox_t* iconbox) +{ + struct rtgui_dc* dc; + struct rtgui_rect rect; + + /* begin drawing */ + dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(iconbox)); + if (dc == RT_NULL) return; + + /* get widget rect */ + rtgui_widget_get_rect(RTGUI_WIDGET(iconbox), &rect); + + /* draw icon */ + rtgui_image_blit(iconbox->image, dc, &rect); + + /* draw text */ + if (iconbox->text_position == RTGUI_ICONBOX_TEXT_BELOW && iconbox->text != RT_NULL) + { + rect.y1 = iconbox->image->h + RTGUI_WIDGET_DEFAULT_MARGIN; + rtgui_dc_draw_text(dc, iconbox->text, &rect); + } + else if (iconbox->text_position == RTGUI_ICONBOX_TEXT_RIGHT && iconbox->text != RT_NULL) + { + rect.x1 = iconbox->image->w + RTGUI_WIDGET_DEFAULT_MARGIN; + rtgui_dc_draw_text(dc, iconbox->text, &rect); + } + + /* end drawing */ + rtgui_dc_end_drawing(dc); +} + +rt_uint16_t rtgui_theme_get_selected_height() +{ + return SELECTED_HEIGHT; +} + +void rtgui_theme_draw_selected(struct rtgui_dc* dc, rtgui_rect_t *rect) +{ + rtgui_color_t saved; + rtgui_rect_t focus_rect; + + focus_rect = *rect; + saved = rtgui_dc_get_color(dc); + rtgui_dc_set_color(dc, blue); + + rtgui_rect_inflate(&focus_rect, -1); + rtgui_dc_draw_focus_rect(dc, &focus_rect); + rtgui_rect_inflate(&focus_rect, -1); + rtgui_dc_draw_focus_rect(dc, &focus_rect); + + rtgui_dc_set_color(dc, saved); +} + +/* get default background color */ +rtgui_color_t rtgui_theme_default_bc() +{ + return default_background; +} + +/* get default foreground color */ +rtgui_color_t rtgui_theme_default_fc() +{ + return default_foreground; +} + diff --git a/rtgui/include/rtgui/color.h b/rtgui/include/rtgui/color.h index af45664a16..6150b99039 100644 --- a/rtgui/include/rtgui/color.h +++ b/rtgui/include/rtgui/color.h @@ -18,9 +18,9 @@ typedef unsigned long rtgui_color_t;
#define RTGUI_ARGB(a, r, g, b) \
- ((rtgui_color_t)(((rt_uint8_t)(r)|\ - (((unsigned)(rt_uint8_t)(g))<<8))|\ - (((unsigned long)(rt_uint8_t)(b))<<16)|\ + ((rtgui_color_t)(((rt_uint8_t)(r)|\
+ (((unsigned)(rt_uint8_t)(g))<<8))|\
+ (((unsigned long)(rt_uint8_t)(b))<<16)|\
(((unsigned long)(rt_uint8_t)(a))<<24)))
#define RTGUI_RGB(r, g, b) RTGUI_ARGB(255, (r), (g), (b))
@@ -51,7 +51,7 @@ rt_inline rt_uint16_t rtgui_color_to_565(rtgui_color_t c) {
rt_uint16_t pixel;
- pixel = ((RTGUI_RGB_B(c)>> 3) << 11) | ((RTGUI_RGB_G(c) >> 2) << 5) | (RTGUI_RGB_R(c) >> 3);
+ pixel = (rt_uint16_t)(((RTGUI_RGB_B(c)>> 3) << 11) | ((RTGUI_RGB_G(c) >> 2) << 5) | (RTGUI_RGB_R(c) >> 3));
return pixel;
}
@@ -75,7 +75,7 @@ rt_inline rt_uint16_t rtgui_color_to_565p(rtgui_color_t c) {
rt_uint16_t pixel;
- pixel = ((RTGUI_RGB_R(c) >> 3) << 11) | ((RTGUI_RGB_G(c) >> 2) << 5) | (RTGUI_RGB_B(c)>> 3);
+ pixel = (rt_uint16_t)(((RTGUI_RGB_R(c) >> 3) << 11) | ((RTGUI_RGB_G(c) >> 2) << 5) | (RTGUI_RGB_B(c)>> 3));
return pixel;
}
diff --git a/rtgui/include/rtgui/event.h b/rtgui/include/rtgui/event.h index 0ff94117ce..e1179d8df4 100644 --- a/rtgui/include/rtgui/event.h +++ b/rtgui/include/rtgui/event.h @@ -213,7 +213,7 @@ struct rtgui_event_win_resize #define rtgui_event_win_activate rtgui_event_win
#define rtgui_event_win_deactivate rtgui_event_win
#define rtgui_event_win_close rtgui_event_win
- +
/* window event init */
#define RTGUI_EVENT_WIN_CREATE_INIT(e) RTGUI_EVENT_INIT(&((e)->parent), RTGUI_EVENT_WIN_CREATE)
#define RTGUI_EVENT_WIN_DESTROY_INIT(e) RTGUI_EVENT_INIT(&((e)->parent), RTGUI_EVENT_WIN_DESTROY)
@@ -296,9 +296,9 @@ struct rtgui_event_clip_info /* the number of rects */
rt_uint32_t num_rect;
- struct rtgui_rect rects[0];
+ /* rtgui_rect_t *rects */
};
-#define RTGUI_EVENT_GET_RECT(e, i) (&(e->rects[0]) + i)
+#define RTGUI_EVENT_GET_RECT(e, i) &(((rtgui_rect_t*)(e + 1))[i])
#define RTGUI_EVENT_UPDATE_BEGIN_INIT(e) RTGUI_EVENT_INIT(&((e)->parent), RTGUI_EVENT_UPDATE_BEGIN)
#define RTGUI_EVENT_UPDATE_END_INIT(e) RTGUI_EVENT_INIT(&((e)->parent), RTGUI_EVENT_UPDATE_END)
@@ -420,5 +420,5 @@ struct rtgui_event_resize rt_int16_t w, h;
};
#define RTGUI_EVENT_RESIZE_INIT(e) RTGUI_EVENT_INIT(&((e)->parent), RTGUI_EVENT_RESIZE)
- +
#endif
diff --git a/rtgui/include/rtgui/image_png.h b/rtgui/include/rtgui/image_png.h index 13aff657f9..09454d6a75 100644 --- a/rtgui/include/rtgui/image_png.h +++ b/rtgui/include/rtgui/image_png.h @@ -16,4 +16,6 @@ #include <rtgui/image.h>
+void rtgui_image_png_init(void);
+
#endif
diff --git a/rtgui/include/rtgui/rtgui.h b/rtgui/include/rtgui/rtgui.h index dcc0741dba..50e3b5b59b 100644 --- a/rtgui/include/rtgui/rtgui.h +++ b/rtgui/include/rtgui/rtgui.h @@ -28,7 +28,7 @@ struct rtgui_widget; struct rtgui_win; typedef struct rtgui_panel rtgui_panel_t; -typedef struct rtgui_win rtgui_win_t;
+typedef struct rtgui_win rtgui_win_t; typedef struct rtgui_workbench rtgui_workbench_t; typedef rt_bool_t (*rtgui_event_handler_ptr)(struct rtgui_widget* widget, struct rtgui_event* event); @@ -95,6 +95,13 @@ enum RTGUI_ARRAW RTGUI_ARRAW_RIGHT }; +enum RTGUI_MODAL_CODE +{ + RTGUI_MODAL_OK, + RTGUI_MODAL_CANCEL +}; +typedef enum RTGUI_MODAL_CODE rtgui_modal_code_t; + #include <rtgui/rtgui_object.h> #endif diff --git a/rtgui/include/rtgui/rtgui_config.h b/rtgui/include/rtgui/rtgui_config.h index 4cce06fab4..bc97b028bd 100644 --- a/rtgui/include/rtgui/rtgui_config.h +++ b/rtgui/include/rtgui/rtgui_config.h @@ -22,18 +22,23 @@ /* #define RTGUI_USING_MOUSE_CURSOR */
+/* #define RTGUI_USING_FONT12 */
+
#define RTGUI_USING_FONT16
#define RTGUI_USING_FONTHZ
+#ifdef _WIN32
+#define RTGUI_USING_STDIO_FILERW
+#define RTGUI_IMAGE_PNG
+#define RTGUI_IMAGE_JPG
+#else
+#define RTGUI_USING_DFS_FILERW
#define RTGUI_USING_HZ_FILE
-
-// #define RT_USING_STDIO_FILERW
-#define RT_USING_DFS_FILERW
-// #define RTGUI_IMAGE_PNG
-// #define RTGUI_IMAGE_JPG
+#endif
#define RTGUI_SVR_THREAD_PRIORITY 15
#define RTGUI_SVR_THREAD_TIMESLICE 5
+#define RTGUI_SVR_THREAD_STACK_SIZE 2048
#define RTGUI_APP_THREAD_PRIORITY 25
#define RTGUI_APP_THREAD_TIMESLICE 8
diff --git a/rtgui/include/rtgui/rtgui_object.h b/rtgui/include/rtgui/rtgui_object.h index 9c06137701..bab31f15cd 100644 --- a/rtgui/include/rtgui/rtgui_object.h +++ b/rtgui/include/rtgui/rtgui_object.h @@ -88,7 +88,6 @@ struct rtgui_object /* object type */
rtgui_type_t* type;
- char *name;
rt_bool_t is_static;
};
rtgui_type_t *rtgui_object_type_get(void);
diff --git a/rtgui/include/rtgui/rtgui_system.h b/rtgui/include/rtgui/rtgui_system.h index 9fd76b39c0..e0ceae6f2b 100644 --- a/rtgui/include/rtgui/rtgui_system.h +++ b/rtgui/include/rtgui/rtgui_system.h @@ -31,9 +31,6 @@ struct rtgui_thread /* the owner of thread */
struct rtgui_widget* widget;
-
- /* quit of thread */
- rt_bool_t is_quit;
};
typedef struct rtgui_thread rtgui_thread_t;
struct rtgui_timer;
diff --git a/rtgui/include/rtgui/rtgui_theme.h b/rtgui/include/rtgui/rtgui_theme.h index 28edac0e61..0efde47cad 100644 --- a/rtgui/include/rtgui/rtgui_theme.h +++ b/rtgui/include/rtgui/rtgui_theme.h @@ -26,12 +26,17 @@ extern "C" {
#endif
+void rtgui_system_theme_init(void);
+
void rtgui_theme_draw_win(struct rtgui_topwin* win);
void rtgui_theme_draw_button(rtgui_button_t* btn);
void rtgui_theme_draw_label(rtgui_label_t* label);
void rtgui_theme_draw_textbox(rtgui_textbox_t* box);
void rtgui_theme_draw_iconbox(rtgui_iconbox_t* iconbox);
+rt_uint16_t rtgui_theme_get_selected_height(void);
+void rtgui_theme_draw_selected(struct rtgui_dc* dc, rtgui_rect_t *rect);
+
rtgui_color_t rtgui_theme_default_bc(void);
rtgui_color_t rtgui_theme_default_fc(void);
diff --git a/rtgui/include/rtgui/widgets/container.h b/rtgui/include/rtgui/widgets/container.h index 7f1af7d3d2..4cd1bb0a30 100644 --- a/rtgui/include/rtgui/widgets/container.h +++ b/rtgui/include/rtgui/widgets/container.h @@ -28,6 +28,7 @@ struct rtgui_container /* inherit from widget */
struct rtgui_widget parent;
+ struct rtgui_widget* focused;
rtgui_list_t children;
};
typedef struct rtgui_container rtgui_container_t;
diff --git a/rtgui/include/rtgui/widgets/toplevel.h b/rtgui/include/rtgui/widgets/toplevel.h index 044edf4058..463b7e76d2 100644 --- a/rtgui/include/rtgui/widgets/toplevel.h +++ b/rtgui/include/rtgui/widgets/toplevel.h @@ -37,9 +37,6 @@ struct rtgui_toplevel /* server thread id */
rt_thread_t server;
-
- /* current focus widget */
- rtgui_widget_t* focus;
};
typedef struct rtgui_toplevel rtgui_toplevel_t;
@@ -51,7 +48,4 @@ void rtgui_toplevel_handle_clip(struct rtgui_toplevel* top, struct rtgui_event_clip_info* info);
void rtgui_toplevel_update_clip(rtgui_toplevel_t* top);
-void rtgui_toplevel_set_focus(struct rtgui_toplevel* top, rtgui_widget_t* focus);
-rtgui_widget_t* rtgui_toplevel_get_focus(struct rtgui_toplevel* top);
-
#endif
diff --git a/rtgui/include/rtgui/widgets/view.h b/rtgui/include/rtgui/widgets/view.h index c4033c603e..c1ebfa6913 100644 --- a/rtgui/include/rtgui/widgets/view.h +++ b/rtgui/include/rtgui/widgets/view.h @@ -50,8 +50,9 @@ rt_bool_t rtgui_view_event_handler(struct rtgui_widget* widget, struct rtgui_eve void rtgui_view_set_box(rtgui_view_t* view, rtgui_box_t* box);
-void rtgui_view_show(rtgui_view_t* view);
+rtgui_modal_code_t rtgui_view_show(rtgui_view_t* view, rt_bool_t is_modal);
void rtgui_view_hide(rtgui_view_t* view);
+void rtgui_view_end_modal(rtgui_view_t* view, rtgui_modal_code_t modal_code);
char* rtgui_view_get_title(rtgui_view_t* view);
void rtgui_view_set_title(rtgui_view_t* view, const char* title);
diff --git a/rtgui/include/rtgui/widgets/window.h b/rtgui/include/rtgui/widgets/window.h index 243ab75d26..ea730580ad 100644 --- a/rtgui/include/rtgui/widgets/window.h +++ b/rtgui/include/rtgui/widgets/window.h @@ -27,18 +27,18 @@ /** Checks if the object is an rtgui_win */
#define RTGUI_IS_WIN(obj) (RTGUI_OBJECT_CHECK_TYPE((obj), RTGUI_WIN_TYPE))
-#define RTGUI_WIN_STYLE_MODAL 0x00
-#define RTGUI_WIN_STYLE_MODAL_LESS 0x01
-#define RTGUI_WIN_STYLE_NO_TITLE 0x02
-#define RTGUI_WIN_STYLE_NO_BORDER 0x04
-#define RTGUI_WIN_STYLE_SHOW 0x08
-#define RTGUI_WIN_STYLE_CLOSEBOX 0x10
-#define RTGUI_WIN_STYLE_MINIBOX 0x20
-#define RTGUI_WIN_STYLE_ACTIVATE 0x40
-#define RTGUI_WIN_STYLE_NO_FOCUS 0x80
+#define RTGUI_WIN_STYLE_MODAL 0x01 /* modal mode window */
+#define RTGUI_WIN_STYLE_CLOSED 0x02 /* window is closed */
+#define RTGUI_WIN_STYLE_ACTIVATE 0x04 /* window is activated */
+#define RTGUI_WIN_STYLE_NO_FOCUS 0x08 /* non-focused window */
+
+#define RTGUI_WIN_STYLE_NO_TITLE 0x10 /* no title window */
+#define RTGUI_WIN_STYLE_NO_BORDER 0x20 /* no border window */
+#define RTGUI_WIN_STYLE_CLOSEBOX 0x40 /* window has the close button */
+#define RTGUI_WIN_STYLE_MINIBOX 0x80 /* window has the mini button */
#define RTGUI_WIN_STYLE_DEFAULT (RTGUI_WIN_STYLE_CLOSEBOX | RTGUI_WIN_STYLE_MINIBOX)
- +
struct rtgui_win_title;
struct rtgui_win_area;
@@ -51,18 +51,19 @@ struct rtgui_win rtgui_toplevel_t* parent_toplevel;
/* top window style */
- rt_uint32_t style;
+ rt_uint8_t style;
+ rtgui_modal_code_t modal_code;
/* window title */
char* title;
/* call back */
- rt_bool_t (*on_activate) (struct rtgui_widget* widget, struct rtgui_event* event); + rt_bool_t (*on_activate) (struct rtgui_widget* widget, struct rtgui_event* event);
rt_bool_t (*on_deactivate) (struct rtgui_widget* widget, struct rtgui_event* event);
rt_bool_t (*on_close) (struct rtgui_widget* widget, struct rtgui_event* event);
/* reserved user data */
- rt_uint32_t user_data; + rt_uint32_t user_data;
};
rtgui_type_t *rtgui_win_type_get(void);
@@ -71,8 +72,10 @@ rtgui_win_t* rtgui_win_create(rtgui_toplevel_t* parent_toplevel, const char* tit rtgui_rect_t *rect, rt_uint32_t flag);
void rtgui_win_destroy(rtgui_win_t* win);
-void rtgui_win_show(rtgui_win_t* win);
+rtgui_modal_code_t rtgui_win_show(rtgui_win_t* win, rt_bool_t is_modal);
void rtgui_win_hiden(rtgui_win_t* win);
+void rtgui_win_end_modal(rtgui_win_t* win, rtgui_modal_code_t modal_code);
+
rt_bool_t rtgui_win_is_activated(struct rtgui_win* win);
void rtgui_win_move(struct rtgui_win* win, int x, int y);
diff --git a/rtgui/include/rtgui/widgets/workbench.h b/rtgui/include/rtgui/widgets/workbench.h index dd132757c7..3d8668083a 100644 --- a/rtgui/include/rtgui/widgets/workbench.h +++ b/rtgui/include/rtgui/widgets/workbench.h @@ -22,12 +22,14 @@ #include <rtgui/widgets/view.h>
#include <rtgui/widgets/toplevel.h>
-#define RTGUI_WORKBENCH_FLAG_VISIBLE 0x00
-#define RTGUI_WORKBENCH_FLAG_INVISIBLE 0x01
-#define RTGUI_WORKBENCH_FLAG_FULLSCREEN 0x02
+#define RTGUI_WORKBENCH_FLAG_VISIBLE 0x00 /* workbench is visible */
+#define RTGUI_WORKBENCH_FLAG_INVISIBLE 0x01 /* workbench is invisible */
+#define RTGUI_WORKBENCH_FLAG_FULLSCREEN 0x02 /* workbench is full screen */
+#define RTGUI_WORKBENCH_FLAG_MODAL_MODE 0x04 /* workbench is modal mode showing */
#define RTGUI_WORKBENCH_FLAG_CLOSEBLE 0x00
#define RTGUI_WORKBENCH_FLAG_UNCLOSEBLE 0x10
+#define RTGUI_WORKBENCH_FLAG_CLOSED 0x20
#define RTGUI_WORKBENCH_FLAG_DEFAULT RTGUI_WORKBENCH_FLAG_VISIBLE | RTGUI_WORKBENCH_FLAG_CLOSEBLE
@@ -48,6 +50,7 @@ struct rtgui_workbench /* workbench flag */
rt_uint8_t flag;
+ rtgui_modal_code_t modal_code;
/* workbench title */
unsigned char* title;
@@ -63,7 +66,7 @@ rt_bool_t rtgui_workbench_event_handler(rtgui_widget_t* widget, rtgui_event_t* e void rtgui_workbench_set_flag(rtgui_workbench_t* workbench, rt_uint8_t flag);
-void rtgui_workbench_event_loop(rtgui_workbench_t* workbench);
+rt_bool_t rtgui_workbench_event_loop(rtgui_workbench_t* workbench);
rt_err_t rtgui_workbench_show (rtgui_workbench_t* workbench);
rt_err_t rtgui_workbench_hide (rtgui_workbench_t* workbench);
diff --git a/rtgui/server/driver.c b/rtgui/server/driver.c index 2805ad1e56..6793ea1360 100644 --- a/rtgui/server/driver.c +++ b/rtgui/server/driver.c @@ -1,67 +1,67 @@ -/*
- * File : driver.c
- * This file is part of RTGUI in RT-Thread RTOS
- * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team
- *
- * The license and distribution terms for this file may be
- * found in the file LICENSE in this distribution or at
- * http://www.rt-thread.org/license/LICENSE
- *
- * Change Logs:
- * Date Author Notes
- * 2009-10-04 Bernard first version
- */
-#include <rtgui/driver.h>
-
-struct rtgui_list_node _rtgui_graphic_driver_list = {RT_NULL};
-
-void rtgui_graphic_driver_add(struct rtgui_graphic_driver* driver)
-{
- rtgui_list_insert(&_rtgui_graphic_driver_list, &(driver->list));
-}
-
-void rtgui_graphic_driver_remove(struct rtgui_graphic_driver* driver)
-{
- rtgui_list_remove(&_rtgui_graphic_driver_list, &(driver->list));
-}
-
-struct rtgui_graphic_driver* rtgui_graphic_driver_find(char* name)
-{
- struct rtgui_list_node* node;
- struct rtgui_graphic_driver* driver;
-
- /* search in list */
- rtgui_list_foreach(node, &(_rtgui_graphic_driver_list))
- {
- driver = rtgui_list_entry(node, struct rtgui_graphic_driver, list);
-
- /* find it */
- if (rt_strncmp(driver->name, name, RTGUI_NAME_MAX) == 0)
- {
- return driver;
- }
- }
-
- return RT_NULL;
-}
-
-struct rtgui_graphic_driver* rtgui_graphic_driver_get_default()
-{
- return rtgui_list_entry(_rtgui_graphic_driver_list.next,
- struct rtgui_graphic_driver, list);
-}
-
-void rtgui_graphic_driver_get_rect(struct rtgui_graphic_driver *driver, rtgui_rect_t *rect)
-{
- if (rect == RT_NULL || driver == RT_NULL) return;
-
- rect->x1 = rect->y1 = 0;
- rect->x2 = driver->width;
- rect->y2 = driver->height;
-}
-
-void rtgui_graphic_driver_get_default_rect(rtgui_rect_t *rect)
-{
- /* return default the extent of default driver */
- rtgui_graphic_driver_get_rect(rtgui_graphic_driver_get_default(), rect);
-}
+/* + * File : driver.c + * This file is part of RTGUI in RT-Thread RTOS + * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rt-thread.org/license/LICENSE + * + * Change Logs: + * Date Author Notes + * 2009-10-04 Bernard first version + */ +#include <rtgui/driver.h> + +struct rtgui_list_node _rtgui_graphic_driver_list = {RT_NULL}; + +void rtgui_graphic_driver_add(struct rtgui_graphic_driver* driver) +{ + rtgui_list_insert(&_rtgui_graphic_driver_list, &(driver->list)); +} + +void rtgui_graphic_driver_remove(struct rtgui_graphic_driver* driver) +{ + rtgui_list_remove(&_rtgui_graphic_driver_list, &(driver->list)); +} + +struct rtgui_graphic_driver* rtgui_graphic_driver_find(char* name) +{ + struct rtgui_list_node* node; + struct rtgui_graphic_driver* driver; + + /* search in list */ + rtgui_list_foreach(node, &(_rtgui_graphic_driver_list)) + { + driver = rtgui_list_entry(node, struct rtgui_graphic_driver, list); + + /* find it */ + if (rt_strncmp(driver->name, name, RTGUI_NAME_MAX) == 0) + { + return driver; + } + } + + return RT_NULL; +} + +struct rtgui_graphic_driver* rtgui_graphic_driver_get_default() +{ + return rtgui_list_entry(_rtgui_graphic_driver_list.next, + struct rtgui_graphic_driver, list); +} + +void rtgui_graphic_driver_get_rect(struct rtgui_graphic_driver *driver, rtgui_rect_t *rect) +{ + if (rect == RT_NULL || driver == RT_NULL) return; + + rect->x1 = rect->y1 = 0; + rect->x2 = driver->width; + rect->y2 = driver->height; +} + +void rtgui_graphic_driver_get_default_rect(rtgui_rect_t *rect) +{ + /* return default the extent of default driver */ + rtgui_graphic_driver_get_rect(rtgui_graphic_driver_get_default(), rect); +} diff --git a/rtgui/server/panel.c b/rtgui/server/panel.c index e52858a91f..0d767a3f3d 100644 --- a/rtgui/server/panel.c +++ b/rtgui/server/panel.c @@ -1,306 +1,306 @@ -/*
- * File : panel.c
- * This file is part of RTGUI in RT-Thread RTOS
- * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team
- *
- * The license and distribution terms for this file may be
- * found in the file LICENSE in this distribution or at
- * http://www.rt-thread.org/license/LICENSE
- *
- * Change Logs:
- * Date Author Notes
- * 2009-10-04 Bernard first version
- */
-
-#include "panel.h"
-#include "mouse.h"
-
-#include <rtgui/rtgui_system.h>
-
-/* the global parameter */
-struct rtgui_list_node _rtgui_panel_list;
-
-void rtgui_panel_init()
-{
- rtgui_list_init(&_rtgui_panel_list);
-}
-
-void rtgui_panel_register(char* name, rtgui_rect_t* extent)
-{
- register rt_base_t temp;
- struct rtgui_panel* panel;
-
- panel = rtgui_panel_find(name);
- if (panel != RT_NULL )
- {
- /* there are already a same named panel exist. */
- return;
- }
-
- panel = rtgui_malloc(sizeof(struct rtgui_panel));
- if (panel == RT_NULL)
- {
- /* can't alloc memory */
- return;
- }
-
- /* copy name */
- for (temp = 0; temp < RTGUI_NAME_MAX; temp ++)
- {
- panel->name[temp] = name[temp];
- }
-
- /* copy extent */
- panel->extent = *extent;
-
- panel->wm_thread = RT_NULL;
-
- /* init list */
- rtgui_list_init(&(panel->sibling));
- rtgui_list_init(&(panel->thread_list));
-
- /* add panel to panel list */
- rtgui_list_insert(&_rtgui_panel_list, &(panel->sibling));
-}
-
-void rtgui_panel_deregister(char* name)
-{
- struct rtgui_panel* panel;
-
- panel = rtgui_panel_find(name);
- if (panel != RT_NULL)
- {
- rtgui_list_remove(&_rtgui_panel_list, &(panel->sibling));
-
- /* free pane node */
- rtgui_free(panel);
- }
-}
-
-/* set default focused panel, please use it after registered panel */
-void rtgui_panel_set_default_focused(char* name)
-{
- extern struct rtgui_panel* rtgui_server_focus_panel;
- struct rtgui_panel* panel;
-
- panel = rtgui_panel_find(name);
- if (panel != RT_NULL)
- {
- rtgui_server_focus_panel = panel;
- }
-}
-
-struct rtgui_panel* rtgui_panel_find(char* name)
-{
- struct rtgui_list_node* node;
- struct rtgui_panel* panel;
-
- rtgui_list_foreach(node, &_rtgui_panel_list)
- {
- panel = rtgui_list_entry(node, struct rtgui_panel, sibling);
-
- if (rt_strncmp(panel->name, name, RTGUI_NAME_MAX) == 0)
- {
- return panel;
- }
- }
-
- return RT_NULL;
-}
-
-struct rtgui_panel* rtgui_panel_thread_add(char* name, rt_thread_t tid)
-{
- struct rtgui_panel* panel;
-
- panel = rtgui_panel_find(name);
- if (panel != RT_NULL )
- {
- struct rtgui_panel_thread* thread;
-
- /* allocate panel thread node */
- thread = rtgui_malloc(sizeof(struct rtgui_panel_thread));
- if (thread == RT_NULL)
- {
- return RT_NULL;
- }
-
- /* construct panel thread node */
- thread->tid = tid;
-
- /* init list */
- rtgui_list_init(&(thread->list));
- rtgui_list_init(&(thread->monitor_list));
-
- /* append thread to the list */
- rtgui_list_append(&(panel->thread_list), &(thread->list));
- }
-
- return panel;
-}
-
-void rtgui_panel_thread_remove(rtgui_panel_t* panel, rt_thread_t tid)
-{
- if (panel != RT_NULL )
- {
- struct rtgui_list_node* node;
- struct rtgui_panel_thread* thread;
-
- rtgui_list_foreach(node, &(panel->thread_list))
- {
- thread = rtgui_list_entry(node, struct rtgui_panel_thread, list);
- if (thread->tid == tid)
- {
- /* remove node from list */
- rtgui_list_remove(&(panel->thread_list), &(thread->list));
-
- /* free the panel thread node */
- rtgui_free(thread);
- return;
- }
- }
- }
-}
-
-rt_thread_t rtgui_panel_get_active_thread(rtgui_panel_t* panel)
-{
- if (panel != RT_NULL)
- {
- if (panel->thread_list.next != RT_NULL)
- {
- struct rtgui_panel_thread* thread;
- thread = rtgui_list_entry(panel->thread_list.next, struct rtgui_panel_thread, list);
-
- return thread->tid;
- }
- }
-
- return RT_NULL;
-}
-
-void rtgui_panel_set_active_thread(rtgui_panel_t* panel, rt_thread_t tid)
-{
- /* get old active thread */
- rt_thread_t prev_actived = rtgui_panel_get_active_thread(panel);
- if (prev_actived != tid)
- {
- /* de-active old active workbench */
- struct rtgui_event_panel_hide ehide;
- RTGUI_EVENT_PANEL_HIDE_INIT(&ehide);
-
- ehide.panel = panel;
- ehide.workbench = RT_NULL;
- rtgui_thread_send_urgent(prev_actived, &(ehide.parent), sizeof (ehide));
- }
-
- if (panel != RT_NULL )
- {
- struct rtgui_list_node* node;
- struct rtgui_panel_thread* thread;
-
- rtgui_list_foreach(node, &(panel->thread_list))
- {
- thread = rtgui_list_entry(node, struct rtgui_panel_thread, list);
- if (thread->tid == tid)
- {
- /* remove node from list */
- rtgui_list_remove(&(panel->thread_list), &(thread->list));
-
- /* insert node to the header */
- rtgui_list_insert(&(panel->thread_list), &(thread->list));
- return;
- }
- }
- }
-}
-
-/* deactivate current activated thread -- move it to the end of list */
-void rtgui_panel_deactive_thread(rtgui_panel_t* panel)
-{
- RT_ASSERT(panel == RT_NULL);
-
- if (panel->thread_list.next != RT_NULL)
- {
- struct rtgui_panel_thread* thread;
- thread = rtgui_list_entry(panel->thread_list.next, struct rtgui_panel_thread, list);
-
- /* remove it */
- panel->thread_list.next = thread->list.next;
-
- /* append to the tail of thread list */
- rtgui_list_append(&(panel->thread_list), &(thread->list));
- }
-}
-
-/**
- * get the panel which contains a point(x, y)
- */
-rtgui_panel_t* rtgui_panel_get_contain(int x, int y)
-{
- struct rtgui_list_node* node;
- struct rtgui_panel* panel;
-
- rtgui_list_foreach(node, &(_rtgui_panel_list))
- {
- panel = rtgui_list_entry(node, struct rtgui_panel, sibling);
- if (rtgui_rect_contains_point(&(panel->extent), x, y) == RT_EOK)
- {
- return panel;
- }
- }
-
- return RT_NULL;
-}
-
-/**
- * append a rect to panel mouse monitor rect list
- */
-void rtgui_panel_append_monitor_rect(rtgui_panel_t* panel, rt_thread_t tid, rtgui_rect_t* rect)
-{
- if (panel != RT_NULL )
- {
- struct rtgui_list_node* node;
- struct rtgui_panel_thread* thread;
-
- rtgui_list_foreach(node, &(panel->thread_list))
- {
- thread = rtgui_list_entry(node, struct rtgui_panel_thread, list);
- if (thread->tid == tid)
- {
- /* add the monitor rect to list */
- rtgui_mouse_monitor_append(&(thread->monitor_list), rect);
- return;
- }
- }
- }
-}
-
-/**
- * remove a rect from panel mouse monitor rect list
- */
-void rtgui_panel_remove_monitor_rect(rtgui_panel_t* panel, rt_thread_t tid, rtgui_rect_t* rect)
-{
- if (panel != RT_NULL )
- {
- struct rtgui_list_node* node;
- struct rtgui_panel_thread* thread;
-
- rtgui_list_foreach(node, &(panel->thread_list))
- {
- thread = rtgui_list_entry(node, struct rtgui_panel_thread, list);
- if (thread->tid == tid)
- {
- /* remove the monitor rect from list */
- rtgui_mouse_monitor_remove(&(thread->monitor_list), rect);
- return;
- }
- }
- }
-}
-
-void rtgui_panel_set_wm(rtgui_panel_t* panel, rt_thread_t wm)
-{
- RT_ASSERT(wm != RT_NULL);
- RT_ASSERT(panel != RT_NULL);
-
- panel->wm_thread = wm;
-}
+/* + * File : panel.c + * This file is part of RTGUI in RT-Thread RTOS + * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rt-thread.org/license/LICENSE + * + * Change Logs: + * Date Author Notes + * 2009-10-04 Bernard first version + */ + +#include "panel.h" +#include "mouse.h" + +#include <rtgui/rtgui_system.h> + +/* the global parameter */ +struct rtgui_list_node _rtgui_panel_list; + +void rtgui_panel_init() +{ + rtgui_list_init(&_rtgui_panel_list); +} + +void rtgui_panel_register(char* name, rtgui_rect_t* extent) +{ + register rt_base_t temp; + struct rtgui_panel* panel; + + panel = rtgui_panel_find(name); + if (panel != RT_NULL ) + { + /* there are already a same named panel exist. */ + return; + } + + panel = rtgui_malloc(sizeof(struct rtgui_panel)); + if (panel == RT_NULL) + { + /* can't alloc memory */ + return; + } + + /* copy name */ + for (temp = 0; temp < RTGUI_NAME_MAX; temp ++) + { + panel->name[temp] = name[temp]; + } + + /* copy extent */ + panel->extent = *extent; + + panel->wm_thread = RT_NULL; + + /* init list */ + rtgui_list_init(&(panel->sibling)); + rtgui_list_init(&(panel->thread_list)); + + /* add panel to panel list */ + rtgui_list_insert(&_rtgui_panel_list, &(panel->sibling)); +} + +void rtgui_panel_deregister(char* name) +{ + struct rtgui_panel* panel; + + panel = rtgui_panel_find(name); + if (panel != RT_NULL) + { + rtgui_list_remove(&_rtgui_panel_list, &(panel->sibling)); + + /* free pane node */ + rtgui_free(panel); + } +} + +/* set default focused panel, please use it after registered panel */ +void rtgui_panel_set_default_focused(char* name) +{ + extern struct rtgui_panel* rtgui_server_focus_panel; + struct rtgui_panel* panel; + + panel = rtgui_panel_find(name); + if (panel != RT_NULL) + { + rtgui_server_focus_panel = panel; + } +} + +struct rtgui_panel* rtgui_panel_find(char* name) +{ + struct rtgui_list_node* node; + struct rtgui_panel* panel; + + rtgui_list_foreach(node, &_rtgui_panel_list) + { + panel = rtgui_list_entry(node, struct rtgui_panel, sibling); + + if (rt_strncmp(panel->name, name, RTGUI_NAME_MAX) == 0) + { + return panel; + } + } + + return RT_NULL; +} + +struct rtgui_panel* rtgui_panel_thread_add(char* name, rt_thread_t tid) +{ + struct rtgui_panel* panel; + + panel = rtgui_panel_find(name); + if (panel != RT_NULL ) + { + struct rtgui_panel_thread* thread; + + /* allocate panel thread node */ + thread = rtgui_malloc(sizeof(struct rtgui_panel_thread)); + if (thread == RT_NULL) + { + return RT_NULL; + } + + /* construct panel thread node */ + thread->tid = tid; + + /* init list */ + rtgui_list_init(&(thread->list)); + rtgui_list_init(&(thread->monitor_list)); + + /* append thread to the list */ + rtgui_list_append(&(panel->thread_list), &(thread->list)); + } + + return panel; +} + +void rtgui_panel_thread_remove(rtgui_panel_t* panel, rt_thread_t tid) +{ + if (panel != RT_NULL ) + { + struct rtgui_list_node* node; + struct rtgui_panel_thread* thread; + + rtgui_list_foreach(node, &(panel->thread_list)) + { + thread = rtgui_list_entry(node, struct rtgui_panel_thread, list); + if (thread->tid == tid) + { + /* remove node from list */ + rtgui_list_remove(&(panel->thread_list), &(thread->list)); + + /* free the panel thread node */ + rtgui_free(thread); + return; + } + } + } +} + +rt_thread_t rtgui_panel_get_active_thread(rtgui_panel_t* panel) +{ + if (panel != RT_NULL) + { + if (panel->thread_list.next != RT_NULL) + { + struct rtgui_panel_thread* thread; + thread = rtgui_list_entry(panel->thread_list.next, struct rtgui_panel_thread, list); + + return thread->tid; + } + } + + return RT_NULL; +} + +void rtgui_panel_set_active_thread(rtgui_panel_t* panel, rt_thread_t tid) +{ + /* get old active thread */ + rt_thread_t prev_actived = rtgui_panel_get_active_thread(panel); + if (prev_actived != tid) + { + /* de-active old active workbench */ + struct rtgui_event_panel_hide ehide; + RTGUI_EVENT_PANEL_HIDE_INIT(&ehide); + + ehide.panel = panel; + ehide.workbench = RT_NULL; + rtgui_thread_send_urgent(prev_actived, &(ehide.parent), sizeof (ehide)); + } + + if (panel != RT_NULL ) + { + struct rtgui_list_node* node; + struct rtgui_panel_thread* thread; + + rtgui_list_foreach(node, &(panel->thread_list)) + { + thread = rtgui_list_entry(node, struct rtgui_panel_thread, list); + if (thread->tid == tid) + { + /* remove node from list */ + rtgui_list_remove(&(panel->thread_list), &(thread->list)); + + /* insert node to the header */ + rtgui_list_insert(&(panel->thread_list), &(thread->list)); + return; + } + } + } +} + +/* deactivate current activated thread -- move it to the end of list */ +void rtgui_panel_deactive_thread(rtgui_panel_t* panel) +{ + RT_ASSERT(panel == RT_NULL); + + if (panel->thread_list.next != RT_NULL) + { + struct rtgui_panel_thread* thread; + thread = rtgui_list_entry(panel->thread_list.next, struct rtgui_panel_thread, list); + + /* remove it */ + panel->thread_list.next = thread->list.next; + + /* append to the tail of thread list */ + rtgui_list_append(&(panel->thread_list), &(thread->list)); + } +} + +/** + * get the panel which contains a point(x, y) + */ +rtgui_panel_t* rtgui_panel_get_contain(int x, int y) +{ + struct rtgui_list_node* node; + struct rtgui_panel* panel; + + rtgui_list_foreach(node, &(_rtgui_panel_list)) + { + panel = rtgui_list_entry(node, struct rtgui_panel, sibling); + if (rtgui_rect_contains_point(&(panel->extent), x, y) == RT_EOK) + { + return panel; + } + } + + return RT_NULL; +} + +/** + * append a rect to panel mouse monitor rect list + */ +void rtgui_panel_append_monitor_rect(rtgui_panel_t* panel, rt_thread_t tid, rtgui_rect_t* rect) +{ + if (panel != RT_NULL ) + { + struct rtgui_list_node* node; + struct rtgui_panel_thread* thread; + + rtgui_list_foreach(node, &(panel->thread_list)) + { + thread = rtgui_list_entry(node, struct rtgui_panel_thread, list); + if (thread->tid == tid) + { + /* add the monitor rect to list */ + rtgui_mouse_monitor_append(&(thread->monitor_list), rect); + return; + } + } + } +} + +/** + * remove a rect from panel mouse monitor rect list + */ +void rtgui_panel_remove_monitor_rect(rtgui_panel_t* panel, rt_thread_t tid, rtgui_rect_t* rect) +{ + if (panel != RT_NULL ) + { + struct rtgui_list_node* node; + struct rtgui_panel_thread* thread; + + rtgui_list_foreach(node, &(panel->thread_list)) + { + thread = rtgui_list_entry(node, struct rtgui_panel_thread, list); + if (thread->tid == tid) + { + /* remove the monitor rect from list */ + rtgui_mouse_monitor_remove(&(thread->monitor_list), rect); + return; + } + } + } +} + +void rtgui_panel_set_wm(rtgui_panel_t* panel, rt_thread_t wm) +{ + RT_ASSERT(wm != RT_NULL); + RT_ASSERT(panel != RT_NULL); + + panel->wm_thread = wm; +} diff --git a/rtgui/server/panel.h b/rtgui/server/panel.h index c40f57a5ff..fffaa35dc0 100644 --- a/rtgui/server/panel.h +++ b/rtgui/server/panel.h @@ -1,68 +1,68 @@ -/*
- * File : panel.h
- * This file is part of RTGUI in RT-Thread RTOS
- * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team
- *
- * The license and distribution terms for this file may be
- * found in the file LICENSE in this distribution or at
- * http://www.rt-thread.org/license/LICENSE
- *
- * Change Logs:
- * Date Author Notes
- * 2009-10-04 Bernard first version
- */
-
-#ifndef __RT_PANEL_H__
-#define __RT_PANEL_H__
-
-#include <rtgui/rtgui.h>
-#include <rtgui/list.h>
-#include <rtgui/region.h>
-
-struct rtgui_panel_thread
-{
- /* thread id */
- rt_thread_t tid;
-
- /* the list of thread */
- rtgui_list_t list;
-
- /* monitor rect list */
- rtgui_list_t monitor_list;
-};
-typedef struct rtgui_panel_thread rtgui_panel_thread_list_t;
-
-struct rtgui_panel
-{
- char name[RTGUI_NAME_MAX];
-
- /* the extent of panel */
- rtgui_rect_t extent;
-
- /* the list of panel */
- rtgui_list_t sibling;
-
- /* the thread list in this panel */
- rtgui_list_t thread_list;
-
- /* the workbench manager thread */
- rt_thread_t wm_thread;
-};
-
-/* find panel by name */
-struct rtgui_panel* rtgui_panel_find(char* name);
-
-/* add or remove application thread from specified panel */
-rtgui_panel_t* rtgui_panel_thread_add(char* name, rt_thread_t tid);
-void rtgui_panel_thread_remove(rtgui_panel_t* panel, rt_thread_t tid);
-
-rt_thread_t rtgui_panel_get_active_thread(rtgui_panel_t* panel);
-void rtgui_panel_set_active_thread(rtgui_panel_t* panel, rt_thread_t tid);
-
-rtgui_panel_t* rtgui_panel_get_contain(int x, int y);
-void rtgui_panel_set_wm(rtgui_panel_t* panel, rt_thread_t wm);
-
-void rtgui_panel_append_monitor_rect(rtgui_panel_t* panel, rt_thread_t tid, rtgui_rect_t* rect);
-void rtgui_panel_remove_monitor_rect(rtgui_panel_t* panel, rt_thread_t tid, rtgui_rect_t* rect);
-
-#endif
+/* + * File : panel.h + * This file is part of RTGUI in RT-Thread RTOS + * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rt-thread.org/license/LICENSE + * + * Change Logs: + * Date Author Notes + * 2009-10-04 Bernard first version + */ + +#ifndef __RT_PANEL_H__ +#define __RT_PANEL_H__ + +#include <rtgui/rtgui.h> +#include <rtgui/list.h> +#include <rtgui/region.h> + +struct rtgui_panel_thread +{ + /* thread id */ + rt_thread_t tid; + + /* the list of thread */ + rtgui_list_t list; + + /* monitor rect list */ + rtgui_list_t monitor_list; +}; +typedef struct rtgui_panel_thread rtgui_panel_thread_list_t; + +struct rtgui_panel +{ + char name[RTGUI_NAME_MAX]; + + /* the extent of panel */ + rtgui_rect_t extent; + + /* the list of panel */ + rtgui_list_t sibling; + + /* the thread list in this panel */ + rtgui_list_t thread_list; + + /* the workbench manager thread */ + rt_thread_t wm_thread; +}; + +/* find panel by name */ +struct rtgui_panel* rtgui_panel_find(char* name); + +/* add or remove application thread from specified panel */ +rtgui_panel_t* rtgui_panel_thread_add(char* name, rt_thread_t tid); +void rtgui_panel_thread_remove(rtgui_panel_t* panel, rt_thread_t tid); + +rt_thread_t rtgui_panel_get_active_thread(rtgui_panel_t* panel); +void rtgui_panel_set_active_thread(rtgui_panel_t* panel, rt_thread_t tid); + +rtgui_panel_t* rtgui_panel_get_contain(int x, int y); +void rtgui_panel_set_wm(rtgui_panel_t* panel, rt_thread_t wm); + +void rtgui_panel_append_monitor_rect(rtgui_panel_t* panel, rt_thread_t tid, rtgui_rect_t* rect); +void rtgui_panel_remove_monitor_rect(rtgui_panel_t* panel, rt_thread_t tid, rtgui_rect_t* rect); + +#endif diff --git a/rtgui/server/server.c b/rtgui/server/server.c index c782e0e138..5a7fe08a5e 100644 --- a/rtgui/server/server.c +++ b/rtgui/server/server.c @@ -1,627 +1,619 @@ -/*
- * File : server.c
- * This file is part of RTGUI in RT-Thread RTOS
- * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team
- *
- * The license and distribution terms for this file may be
- * found in the file LICENSE in this distribution or at
- * http://www.rt-thread.org/license/LICENSE
- *
- * Change Logs:
- * Date Author Notes
- * 2009-10-04 Bernard first version
- */
-
-#include <rtgui/rtgui.h>
-#include <rtgui/event.h>
+/* + * File : server.c + * This file is part of RTGUI in RT-Thread RTOS + * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rt-thread.org/license/LICENSE + * + * Change Logs: + * Date Author Notes + * 2009-10-04 Bernard first version + */ + +#include <rtgui/rtgui.h> +#include <rtgui/event.h> #include <rtgui/rtgui_system.h> -#include <rtgui/driver.h>
-
-#include "mouse.h"
-#include "panel.h"
-#include "topwin.h"
-
-static char rtgui_server_stack[2048];
-static struct rt_thread rtgui_server_thread;
-
-static struct rt_messagequeue rtgui_server_mq;
-static char rtgui_server_msg_pool[2048];
-
-extern struct rtgui_topwin* rtgui_server_focus_topwin;
-struct rtgui_panel* rtgui_server_focus_panel = RT_NULL;
-
-void rtgui_server_create_application(struct rtgui_event_panel_attach* event)
-{
- struct rtgui_panel* panel = rtgui_panel_find(event->panel_name);
-
- if (panel != RT_NULL)
- {
- struct rtgui_event_panel_info ep;
- RTGUI_EVENT_PANEL_INFO_INIT(&ep);
-
- if (panel->wm_thread != RT_NULL)
- {
- /* notify to workbench */
- rtgui_thread_send(panel->wm_thread, &(event->parent), sizeof(struct rtgui_event_panel_attach));
- }
-
- /* send the responses - ok */
- rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_OK);
-
- /* send the panel info */
- ep.panel = panel;
- ep.extent = panel->extent;
- rtgui_thread_send(event->parent.sender, (struct rtgui_event*)&ep, sizeof(ep));
-
- rtgui_panel_thread_add(event->panel_name, event->parent.sender);
- }
- else
- {
- /* send the responses - failure */
- rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_NRC);
- }
-}
-
-void rtgui_server_destroy_application(struct rtgui_event_panel_detach* event)
-{
- struct rtgui_panel* panel = event->panel;
-
- if (panel != RT_NULL)
- {
- if (panel->wm_thread != RT_NULL)
- {
- /* notify to workbench */
- rtgui_thread_send(panel->wm_thread, &(event->parent), sizeof(struct rtgui_event_panel_detach));
- }
-
- /* send the responses */
- rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_OK);
-
- rtgui_panel_thread_remove(panel, event->parent.sender);
-
- {
- /* get next thread and active it */
- rt_thread_t tid = rtgui_panel_get_active_thread(panel);
-
- /* let this thread repaint */
- struct rtgui_event_paint epaint;
- RTGUI_EVENT_PAINT_INIT(&epaint);
- epaint.wid = RT_NULL;
- rtgui_thread_send(tid, (struct rtgui_event*)&epaint, sizeof(epaint));
- }
- }
- else
- {
- /* send the responses - failure */
- rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_NRC);
- }
-}
-
-void rtgui_server_thread_panel_show(struct rtgui_event_panel_show* event)
-{
- struct rtgui_panel* panel = event->panel;
-
- if (panel != RT_NULL)
- {
- struct rtgui_event_paint epaint;
-
- /* send the responses */
- rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_OK);
-
- if (panel->wm_thread != RT_NULL)
- {
- /* notify to workbench */
- rtgui_thread_send(panel->wm_thread, &(event->parent), sizeof(struct rtgui_event_panel_show));
- }
-
- rtgui_panel_set_active_thread(panel, event->parent.sender);
-
- /* send all topwin clip info */
- rtgui_topwin_update_clip_to_panel(panel);
-
- /* send paint event */
- RTGUI_EVENT_PAINT_INIT(&epaint);
- epaint.wid = RT_NULL;
- rtgui_thread_send(event->parent.sender, (struct rtgui_event*)&epaint,
- sizeof(epaint));
- }
- else
- {
- /* send failed */
- rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_ERROR);
- }
-}
-
-void rtgui_server_thread_panel_hide(struct rtgui_event_panel_hide* event)
-{
- struct rtgui_panel* panel = event->panel;
-
- if (panel != RT_NULL)
- {
- rt_thread_t tid;
- struct rtgui_event_paint epaint;
-
- /* send the responses */
- rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_OK);
-
- if (panel->thread_list.next != RT_NULL)
- {
- struct rtgui_panel_thread* thread;
- thread = rtgui_list_entry(panel->thread_list.next, struct rtgui_panel_thread, list);
-
- /* remove it */
- panel->thread_list.next = thread->list.next;
-
- /* append to the tail of thread list */
- rtgui_list_append(&(panel->thread_list), &(thread->list));
- }
-
- /* get new active thread */
- tid = rtgui_panel_get_active_thread(panel);
- /* send all topwin clip info */
- rtgui_topwin_update_clip_to_panel(panel);
-
- /* send paint event */
- RTGUI_EVENT_PAINT_INIT(&epaint);
- epaint.wid = RT_NULL;
- rtgui_thread_send(tid, (struct rtgui_event*)&epaint, sizeof(epaint));
- }
- else
- {
- /* send failed */
- rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_ERROR);
- }
-}
-
-void rtgui_server_handle_set_wm(struct rtgui_event_set_wm *event)
-{
- struct rtgui_panel* panel = rtgui_panel_find(event->panel_name);
-
- if (panel != RT_NULL)
- {
- rtgui_panel_set_wm(panel, event->parent.sender);
- }
-}
-
-void rtgui_server_handle_update(struct rtgui_event_update_end* event)
-{
- struct rtgui_graphic_driver* driver = rtgui_graphic_driver_get_default();
- if (driver != RT_NULL)
- {
- driver->screen_update(&(event->rect));
- }
-}
-
-void rtgui_server_handle_monitor_add(struct rtgui_event_monitor* event)
-{
- if (event->panel != RT_NULL)
- {
- /* append monitor rect to panel list */
- rtgui_panel_append_monitor_rect(event->panel, event->parent.sender, &(event->rect));
- }
- else
- {
- /* add monitor rect to top window list */
- rtgui_topwin_append_monitor_rect(event->wid, &(event->rect));
- }
-}
-
-void rtgui_server_handle_monitor_remove(struct rtgui_event_monitor* event)
-{
- if (event->panel != RT_NULL)
- {
- /* add monitor rect to panel list */
- rtgui_panel_remove_monitor_rect(event->panel, event->parent.sender, &(event->rect));
- }
- else
- {
- /* add monitor rect to top window list */
- rtgui_topwin_remove_monitor_rect(event->wid, &(event->rect));
- }
-}
-
-void rtgui_server_handle_mouse_btn(struct rtgui_event_mouse* event)
-{
- struct rtgui_topwin* wnd;
- struct rtgui_panel* panel;
-
- /* re-init to server thread */
- RTGUI_EVENT_MOUSE_BUTTON_INIT(event);
-
- if (rtgui_winrect_is_moved() &&
- event->button & (RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_UP))
- {
- struct rtgui_topwin* topwin;
- rtgui_rect_t rect;
-
- if (rtgui_winrect_moved_done(&rect, &topwin) == RT_TRUE)
- {
- struct rtgui_event_win_move ewin;
-
- /* move window */
- RTGUI_EVENT_WIN_MOVE_INIT(&ewin);
- ewin.wid = topwin->wid;
- if (topwin->title != RT_NULL)
- {
- if (topwin->flag & WINTITLE_BORDER)
- {
- ewin.x = rect.x1 + WINTITLE_BORDER_SIZE;
- ewin.y = rect.y1 + WINTITLE_BORDER_SIZE;
- }
- if (!(topwin->flag & WINTITLE_NO)) ewin.y += WINTITLE_HEIGHT;
- }
- else
- {
- ewin.x = rect.x1;
- ewin.y = rect.y1;
- }
-
- /* send to client thread */
- rtgui_thread_send(topwin->tid, &(ewin.parent), sizeof(ewin));
-
- return;
- }
- }
-
- /* get the wnd which contains the mouse */
- wnd = rtgui_topwin_get_wnd(event->x, event->y);
- if (wnd != RT_NULL)
- {
- event->wid = wnd->wid;
-
- if (rtgui_server_focus_topwin != wnd)
- {
- /* raise this window */
- rtgui_topwin_raise(wnd->wid, wnd->tid);
- rtgui_server_focus_panel = RT_NULL;
- }
-
- if (wnd->title != RT_NULL &&
- rtgui_rect_contains_point(&(RTGUI_WIDGET(wnd->title)->extent), event->x, event->y) == RT_EOK)
- {
- rtgui_topwin_title_onmouse(wnd, event);
- }
- else
- {
- /* send mouse event to thread */
- rtgui_thread_send(wnd->tid, (struct rtgui_event*)event, sizeof(struct rtgui_event_mouse));
- }
- return ;
- }
-
- /* get the panel which contains the mouse */
- panel = rtgui_panel_get_contain(event->x, event->y);
- if (panel != RT_NULL)
- {
- /* deactivate old window */
- if (rtgui_server_focus_topwin != RT_NULL)
- {
- rtgui_topwin_deactivate_win(rtgui_server_focus_topwin);
- }
-
- rtgui_server_focus_panel = panel;
- rtgui_server_focus_topwin = RT_NULL;
-
- /* set destination window to null */
- event->wid = RT_NULL;
-
- /* send mouse event to thread */
- rtgui_thread_send(rtgui_panel_get_active_thread(panel),
- (struct rtgui_event*)event,
- sizeof(struct rtgui_event_mouse));
- }
-}
-
-static struct rtgui_panel* last_monitor_panel = RT_NULL;
-static struct rtgui_topwin* last_monitor_topwin = RT_NULL;
-
-void rtgui_server_handle_mouse_motion(struct rtgui_event_mouse* event)
-{
- /* the topwin contains current mouse */
- struct rtgui_topwin* win = RT_NULL;
- struct rtgui_panel* panel = RT_NULL;
-
- /* re-init mouse event */
- RTGUI_EVENT_MOUSE_MOTION_INIT(event);
-
- /* find the panel or topwin which monitor the mouse motion */
- win = rtgui_topwin_get_wnd(event->x, event->y);
- if (win == RT_NULL)
- {
- /* try to find monitor on the panel */
- panel = rtgui_panel_get_contain(event->x, event->y);
-
- if (panel != RT_NULL)
- {
- struct rtgui_panel_thread* thread;
-
- /* get active panel thread */
- if (panel->thread_list.next != RT_NULL)
- {
- thread = rtgui_list_entry(panel->thread_list.next, struct rtgui_panel_thread, list);
- if (!rtgui_mouse_monitor_contains_point(&(thread->monitor_list),
- event->x, event->y) == RT_TRUE)
- {
- /* no monitor in this panel */
- panel = RT_NULL;
- }
- }
- }
- }
- else if (win->monitor_list.next != RT_NULL)
- {
- /* check whether the monitor exist */
- if (rtgui_mouse_monitor_contains_point(&(win->monitor_list), event->x, event->y) != RT_TRUE)
- {
- win = RT_NULL;
-
- /* try to find monitor on the panel */
- panel = rtgui_panel_get_contain(event->x, event->y);
- if (panel != RT_NULL)
- {
- struct rtgui_panel_thread* thread;
-
- /* get active panel thread */
- if (panel->thread_list.next != RT_NULL)
- {
- thread = rtgui_list_entry(panel->thread_list.next, struct rtgui_panel_thread, list);
- if (!rtgui_mouse_monitor_contains_point(&(thread->monitor_list),
- event->x, event->y) == RT_TRUE)
- {
- /* no monitor in this panel */
- panel = RT_NULL;
- }
- }
- }
- }
- }
- else
- {
- win = RT_NULL;
- }
-
- /* check old panel or window */
- if (last_monitor_panel != RT_NULL)
- {
- rt_thread_t tid = rtgui_panel_get_active_thread(last_monitor_panel);
- event->wid = RT_NULL;
-
- /* send mouse motion event */
- rtgui_thread_send(tid, &(event->parent), sizeof(struct rtgui_event_mouse));
- }
- else if (last_monitor_topwin != RT_NULL)
- {
- event->wid = last_monitor_topwin->wid;
-
- /* send mouse motion event */
- rtgui_thread_send(last_monitor_topwin->tid, &(event->parent), sizeof(struct rtgui_event_mouse));
- }
-
- if (last_monitor_panel != panel)
- {
- last_monitor_panel = panel;
- if (last_monitor_panel != RT_NULL)
- {
- rt_thread_t tid = rtgui_panel_get_active_thread(last_monitor_panel);
- event->wid = RT_NULL;
-
- /* send mouse motion event */
- rtgui_thread_send(tid, &(event->parent), sizeof(struct rtgui_event_mouse));
- }
- }
-
- if (last_monitor_topwin != win)
- {
- last_monitor_topwin = win;
- if (last_monitor_topwin != RT_NULL)
- {
- event->wid = last_monitor_topwin->wid;
-
- /* send mouse motion event */
- rtgui_thread_send(last_monitor_topwin->tid, &(event->parent), sizeof(struct rtgui_event_mouse));
- }
- }
-
- /* move mouse to (x, y) */
- rtgui_mouse_moveto(event->x, event->y);
-}
-
-void rtgui_server_handle_kbd(struct rtgui_event_kbd* event)
-{
- struct rtgui_topwin* wnd;
- struct rtgui_panel* panel;
-
- /* re-init to server thread */
- RTGUI_EVENT_KBD_INIT(event);
-
- /* todo: handle input method and global shortcut */
-
- /* send to focus window or focus panel */
- wnd = rtgui_server_focus_topwin;
- if (wnd != RT_NULL && wnd->flag & WINTITLE_ACTIVATE)
- {
- /* send to focus window */
- event->wid = wnd->wid;
-
- /* send keyboard event to thread */
- rtgui_thread_send(wnd->tid, (struct rtgui_event*)event, sizeof(struct rtgui_event_kbd));
-
- return;
- }
-
- panel = rtgui_server_focus_panel;
- if (panel != RT_NULL)
- {
- /* send to focus panel */
- event->wid = RT_NULL;
-
- /* send keyboard event to thread */
- rtgui_thread_send(rtgui_panel_get_active_thread(panel),
- (struct rtgui_event*)event, sizeof(struct rtgui_event_kbd));
- }
-}
-
-#ifdef __WIN32__
-#include <windows.h>
-#endif
-
-/**
- * rtgui server thread's entry
- */
-static void rtgui_server_entry(void* parameter)
-{
-#ifdef __WIN32__
- /* set the server thread to highest */
- HANDLE hCurrentThread = GetCurrentThread();
- SetThreadPriority(hCurrentThread, THREAD_PRIORITY_HIGHEST);
-#endif
-
- /* init rtgui server msgq */
- rt_mq_init(&rtgui_server_mq,
- "rtgui",
- &rtgui_server_msg_pool[0],
- 256,
- sizeof(rtgui_server_msg_pool),
- RT_IPC_FLAG_FIFO);
-
- /* register rtgui server thread */
- rtgui_thread_register(&rtgui_server_thread, &rtgui_server_mq);
-
- /* init mouse and show */
- rtgui_mouse_init();
-#ifdef RTGUI_USING_MOUSE_CURSOR
- rtgui_mouse_show_cursor();
-#endif
-
- while (1)
- {
- /* the buffer uses to receive event */
- char event_buf[256];
- struct rtgui_event* event = (struct rtgui_event*)&(event_buf[0]);
-
- if (rtgui_thread_recv(event, sizeof(event_buf)) == RT_EOK)
- {
- /* dispatch event */
- switch (event->type)
- {
- /* panel event */
- case RTGUI_EVENT_PANEL_ATTACH:
- /* register an application in panel */
- rtgui_server_create_application((struct rtgui_event_panel_attach*)event);
- break;
-
- case RTGUI_EVENT_PANEL_DETACH:
- /* unregister an application */
- rtgui_server_destroy_application((struct rtgui_event_panel_detach*)event);
- break;
-
- case RTGUI_EVENT_PANEL_SHOW:
- /* handle raise an application */
- rtgui_server_thread_panel_show((struct rtgui_event_panel_show*)event);
- break;
-
- case RTGUI_EVENT_PANEL_HIDE:
- /* handle hide an application */
- rtgui_server_thread_panel_hide((struct rtgui_event_panel_hide*)event);
- break;
-
- case RTGUI_EVENT_SET_WM:
- /* handle set workbench manager event */
- rtgui_server_handle_set_wm((struct rtgui_event_set_wm*)event);
- break;
-
- /* window event */
- case RTGUI_EVENT_WIN_CREATE:
- rtgui_thread_ack(event, RTGUI_STATUS_OK);
- rtgui_topwin_add((struct rtgui_event_win_create*)event);
- break;
-
- case RTGUI_EVENT_WIN_DESTROY:
- if (rtgui_topwin_remove(((struct rtgui_event_win*)event)->wid) == RT_EOK)
- rtgui_thread_ack(event, RTGUI_STATUS_OK);
- else
- rtgui_thread_ack(event, RTGUI_STATUS_ERROR);
- break;
-
- case RTGUI_EVENT_WIN_SHOW:
- rtgui_topwin_show((struct rtgui_event_win*)event);
- break;
-
- case RTGUI_EVENT_WIN_HIDE:
- rtgui_topwin_hide((struct rtgui_event_win*)event);
- break;
-
- case RTGUI_EVENT_WIN_MOVE:
- rtgui_topwin_move((struct rtgui_event_win_move*)event);
- break;
-
- case RTGUI_EVENT_WIN_RESIZE:
- rtgui_topwin_resize(((struct rtgui_event_win_resize*)event)->wid,
- &(((struct rtgui_event_win_resize*)event)->rect));
- break;
-
- /* other event */
- case RTGUI_EVENT_UPDATE_BEGIN:
-#ifdef RTGUI_USING_MOUSE_CURSOR
- /* hide cursor */
- rtgui_mouse_hide_cursor();
-#endif
- break;
-
- case RTGUI_EVENT_UPDATE_END:
- /* handle screen update */
- rtgui_server_handle_update((struct rtgui_event_update_end*)event);
-#ifdef RTGUI_USING_MOUSE_CURSOR
- /* show cursor */
- rtgui_mouse_show_cursor();
-#endif
- break;
-
- case RTGUI_EVENT_MONITOR_ADD:
- /* handle mouse monitor */
- rtgui_server_handle_monitor_add((struct rtgui_event_monitor*)event);
- break;
-
- /* mouse and keyboard event */
- case RTGUI_EVENT_MOUSE_MOTION:
- /* handle mouse motion event */
- rtgui_server_handle_mouse_motion((struct rtgui_event_mouse*)event);
- break;
-
- case RTGUI_EVENT_MOUSE_BUTTON:
- /* handle mouse button */
- rtgui_server_handle_mouse_btn((struct rtgui_event_mouse*)event);
- break;
-
- case RTGUI_EVENT_KBD:
- /* handle keyboard event */
- rtgui_server_handle_kbd((struct rtgui_event_kbd*)event);
- break;
-
- case RTGUI_EVENT_COMMAND:
- break;
- }
- }
- }
-
- /* unregister in rtgui thread */
- // rtgui_thread_deregister(rt_thread_self());
-}
-
-void rtgui_server_post_event(struct rtgui_event* event, rt_size_t size)
-{
- rt_mq_send(&rtgui_server_mq, event, size);
-}
-
-void rtgui_server_init()
-{
- rt_thread_init(&rtgui_server_thread,
- "rtgui",
- rtgui_server_entry, RT_NULL,
- &rtgui_server_stack[0], sizeof(rtgui_server_stack),
- RTGUI_SVR_THREAD_PRIORITY,
- RTGUI_SVR_THREAD_TIMESLICE);
-
- /* start rtgui server thread */
- rt_thread_startup(&rtgui_server_thread);
-}
+#include <rtgui/driver.h> + +#include "mouse.h" +#include "panel.h" +#include "topwin.h" + +static struct rt_thread *rtgui_server_tid; +static struct rt_messagequeue *rtgui_server_mq; + +extern struct rtgui_topwin* rtgui_server_focus_topwin; +struct rtgui_panel* rtgui_server_focus_panel = RT_NULL; + +void rtgui_server_create_application(struct rtgui_event_panel_attach* event) +{ + struct rtgui_panel* panel = rtgui_panel_find(event->panel_name); + + if (panel != RT_NULL) + { + struct rtgui_event_panel_info ep; + RTGUI_EVENT_PANEL_INFO_INIT(&ep); + + if (panel->wm_thread != RT_NULL) + { + /* notify to workbench */ + rtgui_thread_send(panel->wm_thread, &(event->parent), sizeof(struct rtgui_event_panel_attach)); + } + + /* send the responses - ok */ + rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_OK); + + /* send the panel info */ + ep.panel = panel; + ep.extent = panel->extent; + rtgui_thread_send(event->parent.sender, (struct rtgui_event*)&ep, sizeof(ep)); + + rtgui_panel_thread_add(event->panel_name, event->parent.sender); + } + else + { + /* send the responses - failure */ + rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_NRC); + } +} + +void rtgui_server_destroy_application(struct rtgui_event_panel_detach* event) +{ + struct rtgui_panel* panel = event->panel; + + if (panel != RT_NULL) + { + if (panel->wm_thread != RT_NULL) + { + /* notify to workbench */ + rtgui_thread_send(panel->wm_thread, &(event->parent), sizeof(struct rtgui_event_panel_detach)); + } + + /* send the responses */ + rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_OK); + + rtgui_panel_thread_remove(panel, event->parent.sender); + + { + /* get next thread and active it */ + rt_thread_t tid = rtgui_panel_get_active_thread(panel); + + /* let this thread repaint */ + struct rtgui_event_paint epaint; + RTGUI_EVENT_PAINT_INIT(&epaint); + epaint.wid = RT_NULL; + rtgui_thread_send(tid, (struct rtgui_event*)&epaint, sizeof(epaint)); + } + } + else + { + /* send the responses - failure */ + rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_NRC); + } +} + +void rtgui_server_thread_panel_show(struct rtgui_event_panel_show* event) +{ + struct rtgui_panel* panel = event->panel; + + if (panel != RT_NULL) + { + struct rtgui_event_paint epaint; + + /* send the responses */ + rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_OK); + + if (panel->wm_thread != RT_NULL) + { + /* notify to workbench */ + rtgui_thread_send(panel->wm_thread, &(event->parent), sizeof(struct rtgui_event_panel_show)); + } + + rtgui_panel_set_active_thread(panel, event->parent.sender); + + /* send all topwin clip info */ + rtgui_topwin_update_clip_to_panel(panel); + + /* send paint event */ + RTGUI_EVENT_PAINT_INIT(&epaint); + epaint.wid = RT_NULL; + rtgui_thread_send(event->parent.sender, (struct rtgui_event*)&epaint, + sizeof(epaint)); + } + else + { + /* send failed */ + rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_ERROR); + } +} + +void rtgui_server_thread_panel_hide(struct rtgui_event_panel_hide* event) +{ + struct rtgui_panel* panel = event->panel; + + if (panel != RT_NULL) + { + rt_thread_t tid; + struct rtgui_event_paint epaint; + + /* send the responses */ + rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_OK); + + if (panel->thread_list.next != RT_NULL) + { + struct rtgui_panel_thread* thread; + thread = rtgui_list_entry(panel->thread_list.next, struct rtgui_panel_thread, list); + + /* remove it */ + panel->thread_list.next = thread->list.next; + + /* append to the tail of thread list */ + rtgui_list_append(&(panel->thread_list), &(thread->list)); + } + + /* get new active thread */ + tid = rtgui_panel_get_active_thread(panel); + /* send all topwin clip info */ + rtgui_topwin_update_clip_to_panel(panel); + + /* send paint event */ + RTGUI_EVENT_PAINT_INIT(&epaint); + epaint.wid = RT_NULL; + rtgui_thread_send(tid, (struct rtgui_event*)&epaint, sizeof(epaint)); + } + else + { + /* send failed */ + rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_ERROR); + } +} + +void rtgui_server_handle_set_wm(struct rtgui_event_set_wm *event) +{ + struct rtgui_panel* panel = rtgui_panel_find(event->panel_name); + + if (panel != RT_NULL) + { + rtgui_panel_set_wm(panel, event->parent.sender); + } +} + +void rtgui_server_handle_update(struct rtgui_event_update_end* event) +{ + struct rtgui_graphic_driver* driver = rtgui_graphic_driver_get_default(); + if (driver != RT_NULL) + { + driver->screen_update(&(event->rect)); + } +} + +void rtgui_server_handle_monitor_add(struct rtgui_event_monitor* event) +{ + if (event->panel != RT_NULL) + { + /* append monitor rect to panel list */ + rtgui_panel_append_monitor_rect(event->panel, event->parent.sender, &(event->rect)); + } + else + { + /* add monitor rect to top window list */ + rtgui_topwin_append_monitor_rect(event->wid, &(event->rect)); + } +} + +void rtgui_server_handle_monitor_remove(struct rtgui_event_monitor* event) +{ + if (event->panel != RT_NULL) + { + /* add monitor rect to panel list */ + rtgui_panel_remove_monitor_rect(event->panel, event->parent.sender, &(event->rect)); + } + else + { + /* add monitor rect to top window list */ + rtgui_topwin_remove_monitor_rect(event->wid, &(event->rect)); + } +} + +void rtgui_server_handle_mouse_btn(struct rtgui_event_mouse* event) +{ + struct rtgui_topwin* wnd; + struct rtgui_panel* panel; + + /* re-init to server thread */ + RTGUI_EVENT_MOUSE_BUTTON_INIT(event); + + if (rtgui_winrect_is_moved() && + event->button & (RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_UP)) + { + struct rtgui_topwin* topwin; + rtgui_rect_t rect; + + if (rtgui_winrect_moved_done(&rect, &topwin) == RT_TRUE) + { + struct rtgui_event_win_move ewin; + + /* move window */ + RTGUI_EVENT_WIN_MOVE_INIT(&ewin); + ewin.wid = topwin->wid; + if (topwin->title != RT_NULL) + { + if (topwin->flag & WINTITLE_BORDER) + { + ewin.x = rect.x1 + WINTITLE_BORDER_SIZE; + ewin.y = rect.y1 + WINTITLE_BORDER_SIZE; + } + if (!(topwin->flag & WINTITLE_NO)) ewin.y += WINTITLE_HEIGHT; + } + else + { + ewin.x = rect.x1; + ewin.y = rect.y1; + } + + /* send to client thread */ + rtgui_thread_send(topwin->tid, &(ewin.parent), sizeof(ewin)); + + return; + } + } + + /* get the wnd which contains the mouse */ + wnd = rtgui_topwin_get_wnd(event->x, event->y); + if (wnd != RT_NULL) + { + event->wid = wnd->wid; + + if (rtgui_server_focus_topwin != wnd) + { + /* raise this window */ + rtgui_topwin_raise(wnd->wid, wnd->tid); + rtgui_server_focus_panel = RT_NULL; + } + + if (wnd->title != RT_NULL && + rtgui_rect_contains_point(&(RTGUI_WIDGET(wnd->title)->extent), event->x, event->y) == RT_EOK) + { + rtgui_topwin_title_onmouse(wnd, event); + } + else + { + /* send mouse event to thread */ + rtgui_thread_send(wnd->tid, (struct rtgui_event*)event, sizeof(struct rtgui_event_mouse)); + } + return ; + } + + /* get the panel which contains the mouse */ + panel = rtgui_panel_get_contain(event->x, event->y); + if (panel != RT_NULL) + { + /* deactivate old window */ + if (rtgui_server_focus_topwin != RT_NULL) + { + rtgui_topwin_deactivate_win(rtgui_server_focus_topwin); + } + + rtgui_server_focus_panel = panel; + rtgui_server_focus_topwin = RT_NULL; + + /* set destination window to null */ + event->wid = RT_NULL; + + /* send mouse event to thread */ + rtgui_thread_send(rtgui_panel_get_active_thread(panel), + (struct rtgui_event*)event, + sizeof(struct rtgui_event_mouse)); + } +} + +static struct rtgui_panel* last_monitor_panel = RT_NULL; +static struct rtgui_topwin* last_monitor_topwin = RT_NULL; + +void rtgui_server_handle_mouse_motion(struct rtgui_event_mouse* event) +{ + /* the topwin contains current mouse */ + struct rtgui_topwin* win = RT_NULL; + struct rtgui_panel* panel = RT_NULL; + + /* re-init mouse event */ + RTGUI_EVENT_MOUSE_MOTION_INIT(event); + + /* find the panel or topwin which monitor the mouse motion */ + win = rtgui_topwin_get_wnd(event->x, event->y); + if (win == RT_NULL) + { + /* try to find monitor on the panel */ + panel = rtgui_panel_get_contain(event->x, event->y); + + if (panel != RT_NULL) + { + struct rtgui_panel_thread* thread; + + /* get active panel thread */ + if (panel->thread_list.next != RT_NULL) + { + thread = rtgui_list_entry(panel->thread_list.next, struct rtgui_panel_thread, list); + if (!rtgui_mouse_monitor_contains_point(&(thread->monitor_list), + event->x, event->y) == RT_TRUE) + { + /* no monitor in this panel */ + panel = RT_NULL; + } + } + } + } + else if (win->monitor_list.next != RT_NULL) + { + /* check whether the monitor exist */ + if (rtgui_mouse_monitor_contains_point(&(win->monitor_list), event->x, event->y) != RT_TRUE) + { + win = RT_NULL; + + /* try to find monitor on the panel */ + panel = rtgui_panel_get_contain(event->x, event->y); + if (panel != RT_NULL) + { + struct rtgui_panel_thread* thread; + + /* get active panel thread */ + if (panel->thread_list.next != RT_NULL) + { + thread = rtgui_list_entry(panel->thread_list.next, struct rtgui_panel_thread, list); + if (!rtgui_mouse_monitor_contains_point(&(thread->monitor_list), + event->x, event->y) == RT_TRUE) + { + /* no monitor in this panel */ + panel = RT_NULL; + } + } + } + } + } + else + { + win = RT_NULL; + } + + /* check old panel or window */ + if (last_monitor_panel != RT_NULL) + { + rt_thread_t tid = rtgui_panel_get_active_thread(last_monitor_panel); + event->wid = RT_NULL; + + /* send mouse motion event */ + rtgui_thread_send(tid, &(event->parent), sizeof(struct rtgui_event_mouse)); + } + else if (last_monitor_topwin != RT_NULL) + { + event->wid = last_monitor_topwin->wid; + + /* send mouse motion event */ + rtgui_thread_send(last_monitor_topwin->tid, &(event->parent), sizeof(struct rtgui_event_mouse)); + } + + if (last_monitor_panel != panel) + { + last_monitor_panel = panel; + if (last_monitor_panel != RT_NULL) + { + rt_thread_t tid = rtgui_panel_get_active_thread(last_monitor_panel); + event->wid = RT_NULL; + + /* send mouse motion event */ + rtgui_thread_send(tid, &(event->parent), sizeof(struct rtgui_event_mouse)); + } + } + + if (last_monitor_topwin != win) + { + last_monitor_topwin = win; + if (last_monitor_topwin != RT_NULL) + { + event->wid = last_monitor_topwin->wid; + + /* send mouse motion event */ + rtgui_thread_send(last_monitor_topwin->tid, &(event->parent), sizeof(struct rtgui_event_mouse)); + } + } + + /* move mouse to (x, y) */ + rtgui_mouse_moveto(event->x, event->y); +} + +void rtgui_server_handle_kbd(struct rtgui_event_kbd* event) +{ + struct rtgui_topwin* wnd; + struct rtgui_panel* panel; + + /* re-init to server thread */ + RTGUI_EVENT_KBD_INIT(event); + + /* todo: handle input method and global shortcut */ + + /* send to focus window or focus panel */ + wnd = rtgui_server_focus_topwin; + if (wnd != RT_NULL && wnd->flag & WINTITLE_ACTIVATE) + { + /* send to focus window */ + event->wid = wnd->wid; + + /* send keyboard event to thread */ + rtgui_thread_send(wnd->tid, (struct rtgui_event*)event, sizeof(struct rtgui_event_kbd)); + + return; + } + + panel = rtgui_server_focus_panel; + if (panel != RT_NULL) + { + /* send to focus panel */ + event->wid = RT_NULL; + + /* send keyboard event to thread */ + rtgui_thread_send(rtgui_panel_get_active_thread(panel), + (struct rtgui_event*)event, sizeof(struct rtgui_event_kbd)); + } +} + +#ifdef __WIN32__ +#include <windows.h> +#endif + +/** + * rtgui server thread's entry + */ +static void rtgui_server_entry(void* parameter) +{ +#ifdef __WIN32__ + /* set the server thread to highest */ + HANDLE hCurrentThread = GetCurrentThread(); + SetThreadPriority(hCurrentThread, THREAD_PRIORITY_HIGHEST); +#endif + + /* create rtgui server msgq */ + rtgui_server_mq = rt_mq_create("rtgui", + 256, 8, RT_IPC_FLAG_FIFO); + /* register rtgui server thread */ + rtgui_thread_register(rtgui_server_tid, rtgui_server_mq); + + /* init mouse and show */ + rtgui_mouse_init(); +#ifdef RTGUI_USING_MOUSE_CURSOR + rtgui_mouse_show_cursor(); +#endif + + while (1) + { + /* the buffer uses to receive event */ + char event_buf[256]; + struct rtgui_event* event = (struct rtgui_event*)&(event_buf[0]); + + if (rtgui_thread_recv(event, sizeof(event_buf)) == RT_EOK) + { + /* dispatch event */ + switch (event->type) + { + /* panel event */ + case RTGUI_EVENT_PANEL_ATTACH: + /* register an application in panel */ + rtgui_server_create_application((struct rtgui_event_panel_attach*)event); + break; + + case RTGUI_EVENT_PANEL_DETACH: + /* unregister an application */ + rtgui_server_destroy_application((struct rtgui_event_panel_detach*)event); + break; + + case RTGUI_EVENT_PANEL_SHOW: + /* handle raise an application */ + rtgui_server_thread_panel_show((struct rtgui_event_panel_show*)event); + break; + + case RTGUI_EVENT_PANEL_HIDE: + /* handle hide an application */ + rtgui_server_thread_panel_hide((struct rtgui_event_panel_hide*)event); + break; + + case RTGUI_EVENT_SET_WM: + /* handle set workbench manager event */ + rtgui_server_handle_set_wm((struct rtgui_event_set_wm*)event); + break; + + /* window event */ + case RTGUI_EVENT_WIN_CREATE: + rtgui_thread_ack(event, RTGUI_STATUS_OK); + rtgui_topwin_add((struct rtgui_event_win_create*)event); + break; + + case RTGUI_EVENT_WIN_DESTROY: + if (rtgui_topwin_remove(((struct rtgui_event_win*)event)->wid) == RT_EOK) + rtgui_thread_ack(event, RTGUI_STATUS_OK); + else + rtgui_thread_ack(event, RTGUI_STATUS_ERROR); + break; + + case RTGUI_EVENT_WIN_SHOW: + rtgui_topwin_show((struct rtgui_event_win*)event); + break; + + case RTGUI_EVENT_WIN_HIDE: + rtgui_topwin_hide((struct rtgui_event_win*)event); + break; + + case RTGUI_EVENT_WIN_MOVE: + rtgui_topwin_move((struct rtgui_event_win_move*)event); + break; + + case RTGUI_EVENT_WIN_RESIZE: + rtgui_topwin_resize(((struct rtgui_event_win_resize*)event)->wid, + &(((struct rtgui_event_win_resize*)event)->rect)); + break; + + /* other event */ + case RTGUI_EVENT_UPDATE_BEGIN: +#ifdef RTGUI_USING_MOUSE_CURSOR + /* hide cursor */ + rtgui_mouse_hide_cursor(); +#endif + break; + + case RTGUI_EVENT_UPDATE_END: + /* handle screen update */ + rtgui_server_handle_update((struct rtgui_event_update_end*)event); +#ifdef RTGUI_USING_MOUSE_CURSOR + /* show cursor */ + rtgui_mouse_show_cursor(); +#endif + break; + + case RTGUI_EVENT_MONITOR_ADD: + /* handle mouse monitor */ + rtgui_server_handle_monitor_add((struct rtgui_event_monitor*)event); + break; + + /* mouse and keyboard event */ + case RTGUI_EVENT_MOUSE_MOTION: + /* handle mouse motion event */ + rtgui_server_handle_mouse_motion((struct rtgui_event_mouse*)event); + break; + + case RTGUI_EVENT_MOUSE_BUTTON: + /* handle mouse button */ + rtgui_server_handle_mouse_btn((struct rtgui_event_mouse*)event); + break; + + case RTGUI_EVENT_KBD: + /* handle keyboard event */ + rtgui_server_handle_kbd((struct rtgui_event_kbd*)event); + break; + + case RTGUI_EVENT_COMMAND: + break; + } + } + } + + /* unregister in rtgui thread */ + // rtgui_thread_deregister(rt_thread_self()); +} + +void rtgui_server_post_event(struct rtgui_event* event, rt_size_t size) +{ + rt_mq_send(rtgui_server_mq, event, size); +} + +void rtgui_server_init() +{ + rtgui_server_tid = rt_thread_create("rtgui", + rtgui_server_entry, RT_NULL, + RTGUI_SVR_THREAD_STACK_SIZE, + RTGUI_SVR_THREAD_PRIORITY, + RTGUI_SVR_THREAD_TIMESLICE); + + /* start rtgui server thread */ + if (rtgui_server_tid != RT_NULL) + rt_thread_startup(rtgui_server_tid); +} diff --git a/rtgui/server/topwin.c b/rtgui/server/topwin.c index caf374b41f..6f2c723010 100644 --- a/rtgui/server/topwin.c +++ b/rtgui/server/topwin.c @@ -1,873 +1,873 @@ -/*
- * File : topwin.c
- * This file is part of RT-Thread RTOS
- * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team
- *
- * The license and distribution terms for this file may be
- * found in the file LICENSE in this distribution or at
- * http://www.rt-thread.org/license/LICENSE
- *
- * Change Logs:
- * Date Author Notes
- * 2009-10-16 Bernard first version
- */
-#include "panel.h"
-#include "topwin.h"
-#include "mouse.h"
-
-#include <rtgui/event.h>
-#include <rtgui/image.h>
-#include <rtgui/rtgui_theme.h>
-#include <rtgui/rtgui_system.h>
-#include <rtgui/widgets/window.h>
-
-struct rtgui_topwin* rtgui_server_focus_topwin = RT_NULL;
-
-static struct rtgui_list_node _rtgui_topwin_show_list;
-static struct rtgui_list_node _rtgui_topwin_hide_list;
-
-static void rtgui_topwin_update_clip(void);
-static void rtgui_topwin_redraw(struct rtgui_rect* rect);
-
-#define WINTITLE_CB_WIDTH 14
-#define WINTITLE_CB_HEIGHT 14
-
-void rtgui_topwin_init()
-{
- /* init window list */
- rtgui_list_init(&_rtgui_topwin_show_list);
- rtgui_list_init(&_rtgui_topwin_hide_list);
-}
-
-static struct rtgui_topwin*
-rtgui_topwin_search_in_list(struct rtgui_win* wid, struct rtgui_list_node* list)
-{
- struct rtgui_list_node* node;
- struct rtgui_topwin* topwin;
-
- /* search in list */
- rtgui_list_foreach(node, list)
- {
- topwin = rtgui_list_entry(node, struct rtgui_topwin, list);
-
- /* is this node? */
- if (topwin->wid == wid)
- {
- return topwin;
- }
- }
-
- return RT_NULL;
-}
-
-/* add a window to window list[hide] */
-rt_err_t rtgui_topwin_add(struct rtgui_event_win_create* event)
-{
- struct rtgui_topwin* topwin;
-
- topwin = rtgui_malloc(sizeof(struct rtgui_topwin));
- if (topwin == RT_NULL) return -RT_ERROR;
-
- topwin->wid = event->wid;
- topwin->extent = event->extent;
- topwin->tid = event->parent.sender;
- topwin->mask = event->mask;
-
- topwin->flag = 0;
- if (event->flag & RTGUI_WIN_STYLE_NO_TITLE) topwin->flag |= WINTITLE_NO;
- if (event->flag & RTGUI_WIN_STYLE_CLOSEBOX) topwin->flag |= WINTITLE_CLOSEBOX;
- if (!(event->flag & RTGUI_WIN_STYLE_NO_BORDER)) topwin->flag |= WINTITLE_BORDER;
- if (event->flag & RTGUI_WIN_STYLE_NO_FOCUS) topwin->flag |= WINTITLE_NOFOCUS;
-
- if(!(topwin->flag & WINTITLE_NO) || (topwin->flag & WINTITLE_BORDER))
- {
- /* get win extent */
- rtgui_rect_t rect = topwin->extent;
-
- /* add border rect */
- if (topwin->flag & WINTITLE_BORDER)
- {
- rect.x1 -= WINTITLE_BORDER_SIZE;
- rect.y1 -= WINTITLE_BORDER_SIZE;
- rect.x2 += WINTITLE_BORDER_SIZE;
- rect.y2 += WINTITLE_BORDER_SIZE;
- }
-
- /* add title rect */
- if (!(topwin->flag & WINTITLE_NO)) rect.y1 -= WINTITLE_HEIGHT;
-
- topwin->title = rtgui_wintitle_create(event->title);
- rtgui_widget_set_rect(RTGUI_WIDGET(topwin->title), &rect);
-
- /* update clip info */
- rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(topwin->title));
- rtgui_region_subtract_rect(&(RTGUI_WIDGET(topwin->title)->clip),
- &(RTGUI_WIDGET(topwin->title)->clip),
- &(topwin->extent));
- }
- else topwin->title = RT_NULL;
-
- rtgui_list_init(&topwin->list);
- rtgui_list_init(&topwin->monitor_list);
-
- /* add topwin node to the hidden window list */
- rtgui_list_insert(&(_rtgui_topwin_hide_list), &(topwin->list));
-
- return RT_EOK;
-}
-
-rt_err_t rtgui_topwin_remove(struct rtgui_win* wid)
-{
- struct rtgui_topwin* topwin;
-
- /* find the topwin node */
- topwin = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_show_list);
-
- if (topwin)
- {
- /* remove node from list */
- rtgui_list_remove(&_rtgui_topwin_show_list, &(topwin->list));
-
- rtgui_topwin_update_clip();
-
- /* redraw the old rect */
- rtgui_topwin_redraw(&(topwin->extent));
-
- if (rtgui_server_focus_topwin == topwin)
- {
- /* activate the next window */
- if (_rtgui_topwin_show_list.next != RT_NULL)
- {
- struct rtgui_event_win wevent;
- struct rtgui_topwin* wnd;
-
- /* get the topwin */
- wnd = rtgui_list_entry(_rtgui_topwin_show_list.next,
- struct rtgui_topwin, list);
-
- /* activate the window */
- RTGUI_EVENT_WIN_ACTIVATE_INIT(&wevent);
- wevent.wid = wnd->wid;
- rtgui_thread_send(wnd->tid, &(wevent.parent), sizeof(struct rtgui_event_win));
-
- /* set new focus topwin */
- rtgui_server_focus_topwin = wnd;
- }
- else
- {
- /* there is no shown window right now */
- rtgui_server_focus_topwin = RT_NULL;
- }
- }
-
- /* free the monitor rect list, topwin node and title */
- while (topwin->monitor_list.next != RT_NULL)
- {
- struct rtgui_mouse_monitor* monitor = rtgui_list_entry(topwin->monitor_list.next,
- struct rtgui_mouse_monitor, list);
-
- topwin->monitor_list.next = topwin->monitor_list.next->next;
- rtgui_free(monitor);
- }
-
- /* destroy win title */
- rtgui_wintitle_destroy(topwin->title);
- topwin->title = RT_NULL;
-
- rtgui_free(topwin);
-
- return RT_EOK;
- }
-
- topwin = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_hide_list);
- if (topwin)
- {
- /* remove node from list */
- rtgui_list_remove(&_rtgui_topwin_hide_list, &(topwin->list));
-
- /* free the topwin node and title */
- rtgui_wintitle_destroy(topwin->title);
- topwin->title = RT_NULL;
-
- rtgui_free(topwin);
-
- return RT_EOK;
- }
-
- return -RT_ERROR;
-}
-
-/* activate a win
- * - deactivate the old focus win
- * - activate a win
- * - set the focus win to activate win
- * - draw win title
- */
-void rtgui_topwin_activate_win(struct rtgui_topwin* win)
-{
- struct rtgui_event_win event;
-
- /* activate the raised window */
- RTGUI_EVENT_WIN_ACTIVATE_INIT(&event);
- event.wid = win->wid;
- rtgui_thread_send(win->tid, &(event.parent), sizeof(struct rtgui_event_win));
-
- /* redraw title */
- if (win->title != RT_NULL)
- {
- win->flag |= WINTITLE_ACTIVATE;
- rtgui_theme_draw_win(win);
- }
-
- if (rtgui_server_focus_topwin != RT_NULL)
- {
- /* deactivate the old focus win */
- RTGUI_EVENT_WIN_DEACTIVATE_INIT(&event);
- event.wid = rtgui_server_focus_topwin->wid;
- rtgui_thread_send(rtgui_server_focus_topwin->tid,
- &event.parent, sizeof(struct rtgui_event_win));
-
- /* redraw title */
- if (rtgui_server_focus_topwin->title != RT_NULL)
- {
- rtgui_server_focus_topwin->flag &= ~WINTITLE_ACTIVATE;
- rtgui_theme_draw_win(rtgui_server_focus_topwin);
- }
- }
-
- rtgui_server_focus_topwin = win;
-}
-
-/*
- * deactivate a win
- * - deactivate the win
- * - redraw win title
- * - set rtgui_server_focus_topwin
- */
-void rtgui_topwin_deactivate_win(struct rtgui_topwin* win)
-{
- /* deactivate win */
- struct rtgui_event_win event;
- RTGUI_EVENT_WIN_DEACTIVATE_INIT(&event);
- event.wid = win->wid;
- rtgui_thread_send(win->tid,
- &event.parent, sizeof(struct rtgui_event_win));
-
- win->flag &= ~WINTITLE_ACTIVATE;
- rtgui_theme_draw_win(win);
-
- if (rtgui_server_focus_topwin == win)
- {
- rtgui_server_focus_topwin = RT_NULL;
- }
-}
-
-/* raise window to front */
-void rtgui_topwin_raise(struct rtgui_win* wid, rt_thread_t sender)
-{
- struct rtgui_topwin* topwin;
-
- /* find the topwin node */
- topwin = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_show_list);
- if (topwin)
- {
- rt_int32_t count;
- struct rtgui_list_node* node;
- struct rtgui_event_clip_info* eclip;
- struct rtgui_rect* rect;
-
- /* the window is already placed in front */
- if (&(topwin->list) == _rtgui_topwin_show_list.next)
- {
- rtgui_server_focus_topwin = RT_NULL;
- rtgui_topwin_activate_win(topwin);
- return ;
- }
-
- /* update clip info */
- count = 0;
- node = _rtgui_topwin_show_list.next;
- while (node != &(topwin->list))
- {
- count ++;
- node = node->next;
- }
-
- eclip = (struct rtgui_event_clip_info*)rtgui_malloc(sizeof(struct rtgui_event_clip_info)
- + (count + 1)* sizeof(struct rtgui_rect));
-
- /* reset clip info to top window */
- RTGUI_EVENT_CLIP_INFO_INIT(eclip);
- eclip->num_rect = 0;
- eclip->wid = topwin->wid;
- /* send to destination window */
- rtgui_thread_send(topwin->tid, &(eclip->parent), sizeof(struct rtgui_event_clip_info));
-
- /* reset clip info in title */
- rtgui_toplevel_handle_clip(RTGUI_TOPLEVEL(topwin->title), eclip);
- rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(topwin->title));
- rtgui_region_subtract_rect(&(RTGUI_WIDGET(topwin->title)->clip),
- &(RTGUI_WIDGET(topwin->title)->clip),
- &(topwin->extent));
-
- rect = RTGUI_EVENT_GET_RECT(eclip, 0);
- *rect = (topwin->title != RT_NULL)? RTGUI_WIDGET(topwin->title)->extent : topwin->extent;
-
- count = 1;
- for (node = _rtgui_topwin_show_list.next;
- node != &(topwin->list);
- node = node->next)
- {
- struct rtgui_topwin* wnd;
- wnd = rtgui_list_entry(node, struct rtgui_topwin, list);
-
- eclip->num_rect = count;
- eclip->wid = wnd->wid;
-
- /* send to destination window */
- rtgui_thread_send(wnd->tid, &(eclip->parent),
- sizeof(struct rtgui_event_clip_info) + count * sizeof(struct rtgui_rect));
-
- /* reset clip info in title */
- rtgui_toplevel_handle_clip(RTGUI_TOPLEVEL(wnd->title), eclip);
- rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(wnd->title));
- rtgui_region_subtract_rect(&(RTGUI_WIDGET(wnd->title)->clip),
- &(RTGUI_WIDGET(wnd->title)->clip),
- &(wnd->extent));
-
- rect = RTGUI_EVENT_GET_RECT(eclip, count++);
- *rect = (wnd->title != RT_NULL)? RTGUI_WIDGET(wnd->title)->extent : wnd->extent;
- }
-
- /* release clip info event */
- rtgui_free(eclip);
-
- /* remove node from list */
- rtgui_list_remove(&_rtgui_topwin_show_list, &(topwin->list));
- /* add to front */
- rtgui_list_insert(&_rtgui_topwin_show_list, &(topwin->list));
-
- rtgui_topwin_activate_win(topwin);
- }
-}
-
-/* show a window */
-void rtgui_topwin_show(struct rtgui_event_win* event)
-{
- struct rtgui_topwin* topwin;
- struct rtgui_win* wid = event->wid;
- rt_thread_t sender = RTGUI_EVENT(event)->sender;
-
- /* find in hide list */
- topwin = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_hide_list);
-
- /* find it */
- if (topwin != RT_NULL)
- {
- /* remove node from hidden list */
- rtgui_list_remove(&_rtgui_topwin_hide_list, &(topwin->list));
-
- /* add node to show list */
- rtgui_list_insert(&_rtgui_topwin_show_list, &(topwin->list));
-
- /* show window title */
- if (topwin->title != RT_NULL)
- {
- RTGUI_WIDGET_UNHIDE(RTGUI_WIDGET(topwin->title));
- }
-
- /* update clip info */
- rtgui_topwin_update_clip();
-
- rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_OK);
-
- /* activate this window */
- rtgui_topwin_activate_win(topwin);
- }
- else
- {
- /* the wnd is located in show list, raise wnd to front */
- topwin = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_show_list);
- if (topwin != RT_NULL)
- {
- if (_rtgui_topwin_show_list.next != &(topwin->list))
- {
- rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_OK);
-
- /* not the front window, raise it */
- rtgui_topwin_raise(wid, sender);
- }
- }
- else
- {
- /* there is no wnd in wnd list */
- rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_ERROR);
- }
- }
-}
-
-/* send clip info to panel */
-void rtgui_topwin_update_clip_to_panel(struct rtgui_panel* panel)
-{
- rt_uint32_t count;
- rt_thread_t tid;
- struct rtgui_list_node* node;
- struct rtgui_event_clip_info* eclip;
-
- /* get topwin count */
- count = 0;
- node = _rtgui_topwin_show_list.next;
- while (node != RT_NULL)
- {
- count ++;
- node = node->next;
- }
-
- eclip = (struct rtgui_event_clip_info*)rtgui_malloc(sizeof(struct rtgui_event_clip_info)
- + (count + 1)* sizeof(struct rtgui_rect));
- if (eclip == RT_NULL)
- {
- /* no memory */
- return ;
- }
-
- /* reset clip info to top window */
- RTGUI_EVENT_CLIP_INFO_INIT(eclip);
- eclip->num_rect = count; eclip->wid = RT_NULL;
-
- count = 0;
- for (node = _rtgui_topwin_show_list.next; node != RT_NULL; node = node->next)
- {
- struct rtgui_topwin* wnd;
- struct rtgui_rect* rect;
-
- wnd = rtgui_list_entry(node, struct rtgui_topwin, list);
-
- rect = RTGUI_EVENT_GET_RECT(eclip, count++);
- *rect = (wnd->title != RT_NULL)? RTGUI_WIDGET(wnd->title)->extent : wnd->extent;
- }
-
- /* send to the activated thread of panel */
- tid = rtgui_panel_get_active_thread(panel);
- rtgui_thread_send(tid, (struct rtgui_event*)eclip, sizeof(struct rtgui_event_clip_info)
- + count* sizeof(struct rtgui_rect));
-
- /* release clip info event */
- rtgui_free(eclip);
-}
-
-/* hide a window */
-void rtgui_topwin_hide(struct rtgui_event_win* event)
-{
- struct rtgui_topwin* topwin;
- struct rtgui_win* wid = event->wid;
-
- /* find in show list */
- topwin = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_show_list);
-
- /* found it */
- if (topwin)
- {
- /* remove node from show list */
- rtgui_list_remove(&_rtgui_topwin_show_list, &(topwin->list));
-
- /* add node to hidden list */
- rtgui_list_insert(&_rtgui_topwin_hide_list, &(topwin->list));
-
- /* show window title */
- if (topwin->title != RT_NULL)
- {
- RTGUI_WIDGET_HIDE(RTGUI_WIDGET(topwin->title));
- }
-
- /* update clip info */
- rtgui_topwin_update_clip();
-
- /* redraw the old rect */
- rtgui_topwin_redraw(&(topwin->extent));
-
- if (rtgui_server_focus_topwin == topwin)
- {
- /* activate the next window */
- if (_rtgui_topwin_show_list.next != RT_NULL)
- {
- /* get the topwin */
- topwin = rtgui_list_entry(_rtgui_topwin_show_list.next,
- struct rtgui_topwin, list);
-
- rtgui_server_focus_topwin = RT_NULL;
- rtgui_topwin_activate_win(topwin);
- }
- else
- {
- /* there is no shown window right now */
- rtgui_server_focus_topwin = RT_NULL;
- }
- }
- }
- else
- {
- rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_ERROR);
- return;
- }
-
- rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_OK);
-}
-
-/* move top window */
-void rtgui_topwin_move(struct rtgui_event_win_move* event)
-{
- struct rtgui_topwin* topwin;
-
- /* find in show list */
- topwin = rtgui_topwin_search_in_list(event->wid, &_rtgui_topwin_show_list);
- if (topwin != RT_NULL)
- {
- int dx, dy;
- rtgui_rect_t rect; /* the old topwin coverage area */
- struct rtgui_list_node* node;
-
- /* send status ok */
- rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_OK);
-
- /* get the delta move x, y */
- dx = event->x - topwin->extent.x1;
- dy = event->y - topwin->extent.y1;
-
- rect = topwin->extent;
- /* move window rect */
- rtgui_rect_moveto(&(topwin->extent), dx, dy);
-
- /* move window title */
- if (topwin->title != RT_NULL)
- {
- rect = RTGUI_WIDGET(topwin->title)->extent;
- rtgui_widget_move_to_logic(RTGUI_WIDGET(topwin->title), dx, dy);
- }
-
- /* move the monitor rect list */
- rtgui_list_foreach(node, &(topwin->monitor_list))
- {
- struct rtgui_mouse_monitor* monitor = rtgui_list_entry(node,
- struct rtgui_mouse_monitor,
- list);
- rtgui_rect_moveto(&(monitor->rect), dx, dy);
- }
-
- /* update windows clip info */
- rtgui_topwin_update_clip();
-
- /* update top window title */
- if (topwin->title != RT_NULL) rtgui_theme_draw_win(topwin);
- if (rtgui_rect_is_intersect(&rect, &(topwin->extent)) != RT_EOK)
- {
- /*
- * the old rect is not intersect with moved rect,
- * re-paint window
- */
- struct rtgui_event_paint epaint;
- RTGUI_EVENT_PAINT_INIT(&epaint);
- epaint.wid = topwin->wid;
- rtgui_thread_send(topwin->tid, &(epaint.parent), sizeof(epaint));
- }
-
- /* update old window coverage area */
- rtgui_topwin_redraw(&rect);
- }
- else
- {
- rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_ERROR);
- }
-}
-
-/*
- * resize a top win
- * Note: currently, only support resize hidden window
- */
-void rtgui_topwin_resize(struct rtgui_win* wid, rtgui_rect_t* r)
-{
- struct rtgui_topwin* topwin;
-
- /* find in show list */
- topwin = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_hide_list);
- if (topwin)
- {
- topwin->extent = *r;
-
- if (topwin->title != RT_NULL)
- {
- /* get win extent */
- rtgui_rect_t rect = topwin->extent;
-
- /* add border rect */
- if (topwin->flag & WINTITLE_BORDER)
- {
- rect.x1 -= WINTITLE_BORDER_SIZE;
- rect.y1 -= WINTITLE_BORDER_SIZE;
- rect.x2 += WINTITLE_BORDER_SIZE;
- rect.y2 += WINTITLE_BORDER_SIZE;
- }
-
- /* add title rect */
- if (!(topwin->flag & WINTITLE_NO)) rect.y1 -= WINTITLE_HEIGHT;
-
- RTGUI_WIDGET(topwin->title)->extent = rect;
-
- /* update title & border clip info */
- rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(topwin->title));
- rtgui_region_subtract_rect(&(RTGUI_WIDGET(topwin->title)->clip),
- &(RTGUI_WIDGET(topwin->title)->clip),
- &(topwin->extent));
- }
- }
-}
-
-struct rtgui_topwin* rtgui_topwin_get_wnd(int x, int y)
-{
- struct rtgui_list_node* node;
- struct rtgui_topwin* topwin;
-
- /* search in list */
- rtgui_list_foreach(node, &(_rtgui_topwin_show_list))
- {
- topwin = rtgui_list_entry(node, struct rtgui_topwin, list);
-
- /* is this window? */
- if ((topwin->title != RT_NULL) &&
- rtgui_rect_contains_point(&(RTGUI_WIDGET(topwin->title)->extent), x, y) == RT_EOK)
- {
- return topwin;
- }
- else if (rtgui_rect_contains_point(&(topwin->extent), x, y) == RT_EOK)
- {
- return topwin;
- }
- }
-
- return RT_NULL;
-}
-
-extern struct rtgui_list_node _rtgui_panel_list;
-static void rtgui_topwin_update_clip()
-{
- rt_int32_t count = 0;
- struct rtgui_event_clip_info* eclip;
- struct rtgui_list_node* node = _rtgui_topwin_show_list.next;
-
- /* calculate count */
- while (node != RT_NULL)
- {
- count ++;
- node = node->next;
- }
-
- eclip = (struct rtgui_event_clip_info*)rtgui_malloc(sizeof(struct rtgui_event_clip_info)
- + count * sizeof(struct rtgui_rect));
- RTGUI_EVENT_CLIP_INFO_INIT(eclip);
-
- count = 0;
- rtgui_list_foreach(node, &_rtgui_topwin_show_list)
- {
- struct rtgui_rect* rect;
- struct rtgui_topwin* wnd;
- wnd = rtgui_list_entry(node, struct rtgui_topwin, list);
-
- eclip->num_rect = count;
- eclip->wid = wnd->wid;
-
- /* send to destination window */
- rtgui_thread_send(wnd->tid, &(eclip->parent),
- sizeof(struct rtgui_event_clip_info) + count * sizeof(struct rtgui_rect));
-
- /* update clip in win title */
- if (wnd->title != RT_NULL)
- {
- /* reset clip info */
- rtgui_toplevel_handle_clip(RTGUI_TOPLEVEL(wnd->title), eclip);
- rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(wnd->title));
- rtgui_region_subtract_rect(&(RTGUI_WIDGET(wnd->title)->clip),
- &(RTGUI_WIDGET(wnd->title)->clip),
- &(wnd->extent));
- }
-
- rect = RTGUI_EVENT_GET_RECT(eclip, count++);
- *rect = (wnd->title != RT_NULL)? RTGUI_WIDGET(wnd->title)->extent : wnd->extent;
- }
-
- /* send clip info to each panel */
- eclip->wid = RT_NULL;
- eclip->num_rect = count;
-
- rtgui_list_foreach(node, &(_rtgui_panel_list))
- {
- struct rtgui_panel* panel;
- struct rtgui_list_node* panel_node;
-
- panel = rtgui_list_entry(node, struct rtgui_panel, sibling);
-
- rtgui_list_foreach(panel_node, &(panel->thread_list))
- {
- struct rtgui_panel_thread* thread;
- thread = rtgui_list_entry(panel_node, struct rtgui_panel_thread, list);
-
- /* send clip info to panel */
- rtgui_thread_send(thread->tid, &(eclip->parent),
- sizeof(struct rtgui_event_clip_info) + count * sizeof(struct rtgui_rect));
- }
- }
-
- /* release clip info event */
- rtgui_free(eclip);
-}
-
-static void rtgui_topwin_redraw(struct rtgui_rect* rect)
-{
- struct rtgui_list_node* node;
- struct rtgui_event_paint epaint;
- RTGUI_EVENT_PAINT_INIT(&epaint);
-
- rtgui_list_foreach(node, &_rtgui_topwin_show_list)
- {
- struct rtgui_topwin* wnd = rtgui_list_entry(node, struct rtgui_topwin, list);
-
- if (rtgui_rect_is_intersect(rect, &(wnd->extent)) == RT_EOK)
- {
- /* draw window */
- epaint.wid = wnd->wid;
- rtgui_thread_send(wnd->tid, &(epaint.parent), sizeof(epaint));
-
- /* draw title */
- if (wnd->title != RT_NULL)
- {
- rtgui_theme_draw_win(wnd);
- }
- }
- }
-
- /* redraw the panel */
- rtgui_list_foreach(node, &(_rtgui_panel_list))
- {
- struct rtgui_panel* panel;
- panel = rtgui_list_entry(node, struct rtgui_panel, sibling);
-
- if (rtgui_rect_is_intersect(rect, &(panel->extent)) == RT_EOK)
- {
- rt_thread_t tid;
-
- tid = rtgui_panel_get_active_thread(panel);
- if (tid != RT_NULL)
- {
- /* draw panel */
- epaint.wid = RT_NULL;
- rtgui_thread_send(tid, &(epaint.parent), sizeof(epaint));
- }
- }
- }
-}
-
-void rtgui_topwin_title_onmouse(struct rtgui_topwin* win, struct rtgui_event_mouse* event)
-{
- rtgui_rect_t rect;
-
- /* let window to process this mouse event */
- if (rtgui_rect_contains_point(&win->extent, event->x, event->y) == RT_EOK)
- {
- /* send mouse event to thread */
- rtgui_thread_send(win->tid, &(event->parent), sizeof(struct rtgui_event_mouse));
- return;
- }
-
- /* get close button rect (device value) */
- rect.x1 = RTGUI_WIDGET(win->title)->extent.x2 - WINTITLE_BORDER_SIZE - WINTITLE_CB_WIDTH - 3;
- rect.y1 = RTGUI_WIDGET(win->title)->extent.y1 + WINTITLE_BORDER_SIZE + 3;
- rect.x2 = rect.x1 + WINTITLE_CB_WIDTH;
- rect.y2 = rect.y1 + WINTITLE_CB_HEIGHT;
-
- if (event->button & RTGUI_MOUSE_BUTTON_LEFT)
- {
- if (event->button & RTGUI_MOUSE_BUTTON_DOWN)
- {
- if (rtgui_rect_contains_point(&rect, event->x, event->y) == RT_EOK)
- {
- win->flag |= WINTITLE_CB_PRESSED;
- rtgui_theme_draw_win(win);
- }
- else
- {
- /* maybe move window */
- rtgui_winrect_set(win);
- }
- }
- else if (event->button & RTGUI_MOUSE_BUTTON_UP)
- {
- if (rtgui_rect_contains_point(&rect, event->x, event->y) == RT_EOK)
- {
- struct rtgui_event_win event;
-
- win->flag &= ~WINTITLE_CB_PRESSED;
- rtgui_theme_draw_win(win);
-
- /* send close event to window */
- RTGUI_EVENT_WIN_CLOSE_INIT(&event);
- event.wid = win->wid;
- rtgui_thread_send(win->tid, &(event.parent), sizeof(struct rtgui_event_win));
- }
- }
- }
-}
-
-void rtgui_topwin_append_monitor_rect(struct rtgui_win* wid, rtgui_rect_t* rect)
-{
- struct rtgui_topwin* win;
-
- /* parameters check */
- if (wid == RT_NULL || rect == RT_NULL) return;
-
- /* find topwin */
- win = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_show_list);
- if (win == RT_NULL)
- win = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_hide_list);
-
- if (win == RT_NULL) return;
-
- /* append rect to top window monitor rect list */
- rtgui_mouse_monitor_append(&(win->monitor_list), rect);
-}
-
-void rtgui_topwin_remove_monitor_rect(struct rtgui_win* wid, rtgui_rect_t* rect)
-{
- struct rtgui_topwin* win;
-
- /* parameters check */
- if (wid == RT_NULL || rect == RT_NULL) return;
-
- /* find topwin */
- win = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_show_list);
- if (win == RT_NULL)
- win = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_hide_list);
-
- if (win == RT_NULL) return;
-
- /* remove rect from top window monitor rect list */
- rtgui_mouse_monitor_remove(&(win->monitor_list), rect);
-}
-
-void rtgui_topwin_dump()
-{
- struct rtgui_list_node* node;
-
- rtgui_list_foreach(node, &_rtgui_topwin_show_list)
- {
- struct rtgui_topwin* wnd = rtgui_list_entry(node, struct rtgui_topwin, list);
-
- rt_kprintf("wnd at (%d, %d) - (%d, %d)\n",
- wnd->extent.x1, wnd->extent.y1, wnd->extent.x2, wnd->extent.y2);
-
- if (wnd->title != RT_NULL)
- {
- rt_kprintf("title[%s] border (%d, %d) - (%d, %d)\n", wnd->title->title,
- RTGUI_WIDGET(wnd->title)->extent.x1, RTGUI_WIDGET(wnd->title)->extent.y1,
- RTGUI_WIDGET(wnd->title)->extent.x2, RTGUI_WIDGET(wnd->title)->extent.y2);
- }
- }
-}
+/* + * File : topwin.c + * This file is part of RT-Thread RTOS + * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rt-thread.org/license/LICENSE + * + * Change Logs: + * Date Author Notes + * 2009-10-16 Bernard first version + */ +#include "panel.h" +#include "topwin.h" +#include "mouse.h" + +#include <rtgui/event.h> +#include <rtgui/image.h> +#include <rtgui/rtgui_theme.h> +#include <rtgui/rtgui_system.h> +#include <rtgui/widgets/window.h> + +struct rtgui_topwin* rtgui_server_focus_topwin = RT_NULL; + +static struct rtgui_list_node _rtgui_topwin_show_list; +static struct rtgui_list_node _rtgui_topwin_hide_list; + +static void rtgui_topwin_update_clip(void); +static void rtgui_topwin_redraw(struct rtgui_rect* rect); + +#define WINTITLE_CB_WIDTH 14 +#define WINTITLE_CB_HEIGHT 14 + +void rtgui_topwin_init() +{ + /* init window list */ + rtgui_list_init(&_rtgui_topwin_show_list); + rtgui_list_init(&_rtgui_topwin_hide_list); +} + +static struct rtgui_topwin* +rtgui_topwin_search_in_list(struct rtgui_win* wid, struct rtgui_list_node* list) +{ + struct rtgui_list_node* node; + struct rtgui_topwin* topwin; + + /* search in list */ + rtgui_list_foreach(node, list) + { + topwin = rtgui_list_entry(node, struct rtgui_topwin, list); + + /* is this node? */ + if (topwin->wid == wid) + { + return topwin; + } + } + + return RT_NULL; +} + +/* add a window to window list[hide] */ +rt_err_t rtgui_topwin_add(struct rtgui_event_win_create* event) +{ + struct rtgui_topwin* topwin; + + topwin = rtgui_malloc(sizeof(struct rtgui_topwin)); + if (topwin == RT_NULL) return -RT_ERROR; + + topwin->wid = event->wid; + topwin->extent = event->extent; + topwin->tid = event->parent.sender; + topwin->mask = event->mask; + + topwin->flag = 0; + if (event->flag & RTGUI_WIN_STYLE_NO_TITLE) topwin->flag |= WINTITLE_NO; + if (event->flag & RTGUI_WIN_STYLE_CLOSEBOX) topwin->flag |= WINTITLE_CLOSEBOX; + if (!(event->flag & RTGUI_WIN_STYLE_NO_BORDER)) topwin->flag |= WINTITLE_BORDER; + if (event->flag & RTGUI_WIN_STYLE_NO_FOCUS) topwin->flag |= WINTITLE_NOFOCUS; + + if(!(topwin->flag & WINTITLE_NO) || (topwin->flag & WINTITLE_BORDER)) + { + /* get win extent */ + rtgui_rect_t rect = topwin->extent; + + /* add border rect */ + if (topwin->flag & WINTITLE_BORDER) + { + rect.x1 -= WINTITLE_BORDER_SIZE; + rect.y1 -= WINTITLE_BORDER_SIZE; + rect.x2 += WINTITLE_BORDER_SIZE; + rect.y2 += WINTITLE_BORDER_SIZE; + } + + /* add title rect */ + if (!(topwin->flag & WINTITLE_NO)) rect.y1 -= WINTITLE_HEIGHT; + + topwin->title = rtgui_wintitle_create(event->title); + rtgui_widget_set_rect(RTGUI_WIDGET(topwin->title), &rect); + + /* update clip info */ + rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(topwin->title)); + rtgui_region_subtract_rect(&(RTGUI_WIDGET(topwin->title)->clip), + &(RTGUI_WIDGET(topwin->title)->clip), + &(topwin->extent)); + } + else topwin->title = RT_NULL; + + rtgui_list_init(&topwin->list); + rtgui_list_init(&topwin->monitor_list); + + /* add topwin node to the hidden window list */ + rtgui_list_insert(&(_rtgui_topwin_hide_list), &(topwin->list)); + + return RT_EOK; +} + +rt_err_t rtgui_topwin_remove(struct rtgui_win* wid) +{ + struct rtgui_topwin* topwin; + + /* find the topwin node */ + topwin = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_show_list); + + if (topwin) + { + /* remove node from list */ + rtgui_list_remove(&_rtgui_topwin_show_list, &(topwin->list)); + + rtgui_topwin_update_clip(); + + /* redraw the old rect */ + rtgui_topwin_redraw(&(topwin->extent)); + + if (rtgui_server_focus_topwin == topwin) + { + /* activate the next window */ + if (_rtgui_topwin_show_list.next != RT_NULL) + { + struct rtgui_event_win wevent; + struct rtgui_topwin* wnd; + + /* get the topwin */ + wnd = rtgui_list_entry(_rtgui_topwin_show_list.next, + struct rtgui_topwin, list); + + /* activate the window */ + RTGUI_EVENT_WIN_ACTIVATE_INIT(&wevent); + wevent.wid = wnd->wid; + rtgui_thread_send(wnd->tid, &(wevent.parent), sizeof(struct rtgui_event_win)); + + /* set new focus topwin */ + rtgui_server_focus_topwin = wnd; + } + else + { + /* there is no shown window right now */ + rtgui_server_focus_topwin = RT_NULL; + } + } + + /* free the monitor rect list, topwin node and title */ + while (topwin->monitor_list.next != RT_NULL) + { + struct rtgui_mouse_monitor* monitor = rtgui_list_entry(topwin->monitor_list.next, + struct rtgui_mouse_monitor, list); + + topwin->monitor_list.next = topwin->monitor_list.next->next; + rtgui_free(monitor); + } + + /* destroy win title */ + rtgui_wintitle_destroy(topwin->title); + topwin->title = RT_NULL; + + rtgui_free(topwin); + + return RT_EOK; + } + + topwin = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_hide_list); + if (topwin) + { + /* remove node from list */ + rtgui_list_remove(&_rtgui_topwin_hide_list, &(topwin->list)); + + /* free the topwin node and title */ + rtgui_wintitle_destroy(topwin->title); + topwin->title = RT_NULL; + + rtgui_free(topwin); + + return RT_EOK; + } + + return -RT_ERROR; +} + +/* activate a win + * - deactivate the old focus win + * - activate a win + * - set the focus win to activate win + * - draw win title + */ +void rtgui_topwin_activate_win(struct rtgui_topwin* win) +{ + struct rtgui_event_win event; + + /* activate the raised window */ + RTGUI_EVENT_WIN_ACTIVATE_INIT(&event); + event.wid = win->wid; + rtgui_thread_send(win->tid, &(event.parent), sizeof(struct rtgui_event_win)); + + /* redraw title */ + if (win->title != RT_NULL) + { + win->flag |= WINTITLE_ACTIVATE; + rtgui_theme_draw_win(win); + } + + if (rtgui_server_focus_topwin != RT_NULL) + { + /* deactivate the old focus win */ + RTGUI_EVENT_WIN_DEACTIVATE_INIT(&event); + event.wid = rtgui_server_focus_topwin->wid; + rtgui_thread_send(rtgui_server_focus_topwin->tid, + &event.parent, sizeof(struct rtgui_event_win)); + + /* redraw title */ + if (rtgui_server_focus_topwin->title != RT_NULL) + { + rtgui_server_focus_topwin->flag &= ~WINTITLE_ACTIVATE; + rtgui_theme_draw_win(rtgui_server_focus_topwin); + } + } + + rtgui_server_focus_topwin = win; +} + +/* + * deactivate a win + * - deactivate the win + * - redraw win title + * - set rtgui_server_focus_topwin + */ +void rtgui_topwin_deactivate_win(struct rtgui_topwin* win) +{ + /* deactivate win */ + struct rtgui_event_win event; + RTGUI_EVENT_WIN_DEACTIVATE_INIT(&event); + event.wid = win->wid; + rtgui_thread_send(win->tid, + &event.parent, sizeof(struct rtgui_event_win)); + + win->flag &= ~WINTITLE_ACTIVATE; + rtgui_theme_draw_win(win); + + if (rtgui_server_focus_topwin == win) + { + rtgui_server_focus_topwin = RT_NULL; + } +} + +/* raise window to front */ +void rtgui_topwin_raise(struct rtgui_win* wid, rt_thread_t sender) +{ + struct rtgui_topwin* topwin; + + /* find the topwin node */ + topwin = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_show_list); + if (topwin) + { + rt_int32_t count; + struct rtgui_list_node* node; + struct rtgui_event_clip_info* eclip; + struct rtgui_rect* rect; + + /* the window is already placed in front */ + if (&(topwin->list) == _rtgui_topwin_show_list.next) + { + rtgui_server_focus_topwin = RT_NULL; + rtgui_topwin_activate_win(topwin); + return ; + } + + /* update clip info */ + count = 0; + node = _rtgui_topwin_show_list.next; + while (node != &(topwin->list)) + { + count ++; + node = node->next; + } + + eclip = (struct rtgui_event_clip_info*)rtgui_malloc(sizeof(struct rtgui_event_clip_info) + + (count + 1)* sizeof(struct rtgui_rect)); + + /* reset clip info to top window */ + RTGUI_EVENT_CLIP_INFO_INIT(eclip); + eclip->num_rect = 0; + eclip->wid = topwin->wid; + /* send to destination window */ + rtgui_thread_send(topwin->tid, &(eclip->parent), sizeof(struct rtgui_event_clip_info)); + + /* reset clip info in title */ + rtgui_toplevel_handle_clip(RTGUI_TOPLEVEL(topwin->title), eclip); + rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(topwin->title)); + rtgui_region_subtract_rect(&(RTGUI_WIDGET(topwin->title)->clip), + &(RTGUI_WIDGET(topwin->title)->clip), + &(topwin->extent)); + + rect = RTGUI_EVENT_GET_RECT(eclip, 0); + *rect = (topwin->title != RT_NULL)? RTGUI_WIDGET(topwin->title)->extent : topwin->extent; + + count = 1; + for (node = _rtgui_topwin_show_list.next; + node != &(topwin->list); + node = node->next) + { + struct rtgui_topwin* wnd; + wnd = rtgui_list_entry(node, struct rtgui_topwin, list); + + eclip->num_rect = count; + eclip->wid = wnd->wid; + + /* send to destination window */ + rtgui_thread_send(wnd->tid, &(eclip->parent), + sizeof(struct rtgui_event_clip_info) + count * sizeof(struct rtgui_rect)); + + /* reset clip info in title */ + rtgui_toplevel_handle_clip(RTGUI_TOPLEVEL(wnd->title), eclip); + rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(wnd->title)); + rtgui_region_subtract_rect(&(RTGUI_WIDGET(wnd->title)->clip), + &(RTGUI_WIDGET(wnd->title)->clip), + &(wnd->extent)); + + rect = RTGUI_EVENT_GET_RECT(eclip, count++); + *rect = (wnd->title != RT_NULL)? RTGUI_WIDGET(wnd->title)->extent : wnd->extent; + } + + /* release clip info event */ + rtgui_free(eclip); + + /* remove node from list */ + rtgui_list_remove(&_rtgui_topwin_show_list, &(topwin->list)); + /* add to front */ + rtgui_list_insert(&_rtgui_topwin_show_list, &(topwin->list)); + + rtgui_topwin_activate_win(topwin); + } +} + +/* show a window */ +void rtgui_topwin_show(struct rtgui_event_win* event) +{ + struct rtgui_topwin* topwin; + struct rtgui_win* wid = event->wid; + rt_thread_t sender = RTGUI_EVENT(event)->sender; + + /* find in hide list */ + topwin = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_hide_list); + + /* find it */ + if (topwin != RT_NULL) + { + /* remove node from hidden list */ + rtgui_list_remove(&_rtgui_topwin_hide_list, &(topwin->list)); + + /* add node to show list */ + rtgui_list_insert(&_rtgui_topwin_show_list, &(topwin->list)); + + /* show window title */ + if (topwin->title != RT_NULL) + { + RTGUI_WIDGET_UNHIDE(RTGUI_WIDGET(topwin->title)); + } + + /* update clip info */ + rtgui_topwin_update_clip(); + + rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_OK); + + /* activate this window */ + rtgui_topwin_activate_win(topwin); + } + else + { + /* the wnd is located in show list, raise wnd to front */ + topwin = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_show_list); + if (topwin != RT_NULL) + { + if (_rtgui_topwin_show_list.next != &(topwin->list)) + { + rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_OK); + + /* not the front window, raise it */ + rtgui_topwin_raise(wid, sender); + } + } + else + { + /* there is no wnd in wnd list */ + rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_ERROR); + } + } +} + +/* send clip info to panel */ +void rtgui_topwin_update_clip_to_panel(struct rtgui_panel* panel) +{ + rt_uint32_t count; + rt_thread_t tid; + struct rtgui_list_node* node; + struct rtgui_event_clip_info* eclip; + + /* get topwin count */ + count = 0; + node = _rtgui_topwin_show_list.next; + while (node != RT_NULL) + { + count ++; + node = node->next; + } + + eclip = (struct rtgui_event_clip_info*)rtgui_malloc(sizeof(struct rtgui_event_clip_info) + + (count + 1)* sizeof(struct rtgui_rect)); + if (eclip == RT_NULL) + { + /* no memory */ + return ; + } + + /* reset clip info to top window */ + RTGUI_EVENT_CLIP_INFO_INIT(eclip); + eclip->num_rect = count; eclip->wid = RT_NULL; + + count = 0; + for (node = _rtgui_topwin_show_list.next; node != RT_NULL; node = node->next) + { + struct rtgui_topwin* wnd; + struct rtgui_rect* rect; + + wnd = rtgui_list_entry(node, struct rtgui_topwin, list); + + rect = RTGUI_EVENT_GET_RECT(eclip, count++); + *rect = (wnd->title != RT_NULL)? RTGUI_WIDGET(wnd->title)->extent : wnd->extent; + } + + /* send to the activated thread of panel */ + tid = rtgui_panel_get_active_thread(panel); + rtgui_thread_send(tid, (struct rtgui_event*)eclip, sizeof(struct rtgui_event_clip_info) + + count* sizeof(struct rtgui_rect)); + + /* release clip info event */ + rtgui_free(eclip); +} + +/* hide a window */ +void rtgui_topwin_hide(struct rtgui_event_win* event) +{ + struct rtgui_topwin* topwin; + struct rtgui_win* wid = event->wid; + + /* find in show list */ + topwin = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_show_list); + + /* found it */ + if (topwin) + { + /* remove node from show list */ + rtgui_list_remove(&_rtgui_topwin_show_list, &(topwin->list)); + + /* add node to hidden list */ + rtgui_list_insert(&_rtgui_topwin_hide_list, &(topwin->list)); + + /* show window title */ + if (topwin->title != RT_NULL) + { + RTGUI_WIDGET_HIDE(RTGUI_WIDGET(topwin->title)); + } + + /* update clip info */ + rtgui_topwin_update_clip(); + + /* redraw the old rect */ + rtgui_topwin_redraw(&(topwin->extent)); + + if (rtgui_server_focus_topwin == topwin) + { + /* activate the next window */ + if (_rtgui_topwin_show_list.next != RT_NULL) + { + /* get the topwin */ + topwin = rtgui_list_entry(_rtgui_topwin_show_list.next, + struct rtgui_topwin, list); + + rtgui_server_focus_topwin = RT_NULL; + rtgui_topwin_activate_win(topwin); + } + else + { + /* there is no shown window right now */ + rtgui_server_focus_topwin = RT_NULL; + } + } + } + else + { + rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_ERROR); + return; + } + + rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_OK); +} + +/* move top window */ +void rtgui_topwin_move(struct rtgui_event_win_move* event) +{ + struct rtgui_topwin* topwin; + + /* find in show list */ + topwin = rtgui_topwin_search_in_list(event->wid, &_rtgui_topwin_show_list); + if (topwin != RT_NULL) + { + int dx, dy; + rtgui_rect_t rect; /* the old topwin coverage area */ + struct rtgui_list_node* node; + + /* send status ok */ + rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_OK); + + /* get the delta move x, y */ + dx = event->x - topwin->extent.x1; + dy = event->y - topwin->extent.y1; + + rect = topwin->extent; + /* move window rect */ + rtgui_rect_moveto(&(topwin->extent), dx, dy); + + /* move window title */ + if (topwin->title != RT_NULL) + { + rect = RTGUI_WIDGET(topwin->title)->extent; + rtgui_widget_move_to_logic(RTGUI_WIDGET(topwin->title), dx, dy); + } + + /* move the monitor rect list */ + rtgui_list_foreach(node, &(topwin->monitor_list)) + { + struct rtgui_mouse_monitor* monitor = rtgui_list_entry(node, + struct rtgui_mouse_monitor, + list); + rtgui_rect_moveto(&(monitor->rect), dx, dy); + } + + /* update windows clip info */ + rtgui_topwin_update_clip(); + + /* update top window title */ + if (topwin->title != RT_NULL) rtgui_theme_draw_win(topwin); + if (rtgui_rect_is_intersect(&rect, &(topwin->extent)) != RT_EOK) + { + /* + * the old rect is not intersect with moved rect, + * re-paint window + */ + struct rtgui_event_paint epaint; + RTGUI_EVENT_PAINT_INIT(&epaint); + epaint.wid = topwin->wid; + rtgui_thread_send(topwin->tid, &(epaint.parent), sizeof(epaint)); + } + + /* update old window coverage area */ + rtgui_topwin_redraw(&rect); + } + else + { + rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_ERROR); + } +} + +/* + * resize a top win + * Note: currently, only support resize hidden window + */ +void rtgui_topwin_resize(struct rtgui_win* wid, rtgui_rect_t* r) +{ + struct rtgui_topwin* topwin; + + /* find in show list */ + topwin = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_hide_list); + if (topwin) + { + topwin->extent = *r; + + if (topwin->title != RT_NULL) + { + /* get win extent */ + rtgui_rect_t rect = topwin->extent; + + /* add border rect */ + if (topwin->flag & WINTITLE_BORDER) + { + rect.x1 -= WINTITLE_BORDER_SIZE; + rect.y1 -= WINTITLE_BORDER_SIZE; + rect.x2 += WINTITLE_BORDER_SIZE; + rect.y2 += WINTITLE_BORDER_SIZE; + } + + /* add title rect */ + if (!(topwin->flag & WINTITLE_NO)) rect.y1 -= WINTITLE_HEIGHT; + + RTGUI_WIDGET(topwin->title)->extent = rect; + + /* update title & border clip info */ + rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(topwin->title)); + rtgui_region_subtract_rect(&(RTGUI_WIDGET(topwin->title)->clip), + &(RTGUI_WIDGET(topwin->title)->clip), + &(topwin->extent)); + } + } +} + +struct rtgui_topwin* rtgui_topwin_get_wnd(int x, int y) +{ + struct rtgui_list_node* node; + struct rtgui_topwin* topwin; + + /* search in list */ + rtgui_list_foreach(node, &(_rtgui_topwin_show_list)) + { + topwin = rtgui_list_entry(node, struct rtgui_topwin, list); + + /* is this window? */ + if ((topwin->title != RT_NULL) && + rtgui_rect_contains_point(&(RTGUI_WIDGET(topwin->title)->extent), x, y) == RT_EOK) + { + return topwin; + } + else if (rtgui_rect_contains_point(&(topwin->extent), x, y) == RT_EOK) + { + return topwin; + } + } + + return RT_NULL; +} + +extern struct rtgui_list_node _rtgui_panel_list; +static void rtgui_topwin_update_clip() +{ + rt_int32_t count = 0; + struct rtgui_event_clip_info* eclip; + struct rtgui_list_node* node = _rtgui_topwin_show_list.next; + + /* calculate count */ + while (node != RT_NULL) + { + count ++; + node = node->next; + } + + eclip = (struct rtgui_event_clip_info*)rtgui_malloc(sizeof(struct rtgui_event_clip_info) + + count * sizeof(struct rtgui_rect)); + RTGUI_EVENT_CLIP_INFO_INIT(eclip); + + count = 0; + rtgui_list_foreach(node, &_rtgui_topwin_show_list) + { + struct rtgui_rect* rect; + struct rtgui_topwin* wnd; + wnd = rtgui_list_entry(node, struct rtgui_topwin, list); + + eclip->num_rect = count; + eclip->wid = wnd->wid; + + /* send to destination window */ + rtgui_thread_send(wnd->tid, &(eclip->parent), + sizeof(struct rtgui_event_clip_info) + count * sizeof(struct rtgui_rect)); + + /* update clip in win title */ + if (wnd->title != RT_NULL) + { + /* reset clip info */ + rtgui_toplevel_handle_clip(RTGUI_TOPLEVEL(wnd->title), eclip); + rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(wnd->title)); + rtgui_region_subtract_rect(&(RTGUI_WIDGET(wnd->title)->clip), + &(RTGUI_WIDGET(wnd->title)->clip), + &(wnd->extent)); + } + + rect = RTGUI_EVENT_GET_RECT(eclip, count++); + *rect = (wnd->title != RT_NULL)? RTGUI_WIDGET(wnd->title)->extent : wnd->extent; + } + + /* send clip info to each panel */ + eclip->wid = RT_NULL; + eclip->num_rect = count; + + rtgui_list_foreach(node, &(_rtgui_panel_list)) + { + struct rtgui_panel* panel; + struct rtgui_list_node* panel_node; + + panel = rtgui_list_entry(node, struct rtgui_panel, sibling); + + rtgui_list_foreach(panel_node, &(panel->thread_list)) + { + struct rtgui_panel_thread* thread; + thread = rtgui_list_entry(panel_node, struct rtgui_panel_thread, list); + + /* send clip info to panel */ + rtgui_thread_send(thread->tid, &(eclip->parent), + sizeof(struct rtgui_event_clip_info) + count * sizeof(struct rtgui_rect)); + } + } + + /* release clip info event */ + rtgui_free(eclip); +} + +static void rtgui_topwin_redraw(struct rtgui_rect* rect) +{ + struct rtgui_list_node* node; + struct rtgui_event_paint epaint; + RTGUI_EVENT_PAINT_INIT(&epaint); + + rtgui_list_foreach(node, &_rtgui_topwin_show_list) + { + struct rtgui_topwin* wnd = rtgui_list_entry(node, struct rtgui_topwin, list); + + if (rtgui_rect_is_intersect(rect, &(wnd->extent)) == RT_EOK) + { + /* draw window */ + epaint.wid = wnd->wid; + rtgui_thread_send(wnd->tid, &(epaint.parent), sizeof(epaint)); + + /* draw title */ + if (wnd->title != RT_NULL) + { + rtgui_theme_draw_win(wnd); + } + } + } + + /* redraw the panel */ + rtgui_list_foreach(node, &(_rtgui_panel_list)) + { + struct rtgui_panel* panel; + panel = rtgui_list_entry(node, struct rtgui_panel, sibling); + + if (rtgui_rect_is_intersect(rect, &(panel->extent)) == RT_EOK) + { + rt_thread_t tid; + + tid = rtgui_panel_get_active_thread(panel); + if (tid != RT_NULL) + { + /* draw panel */ + epaint.wid = RT_NULL; + rtgui_thread_send(tid, &(epaint.parent), sizeof(epaint)); + } + } + } +} + +void rtgui_topwin_title_onmouse(struct rtgui_topwin* win, struct rtgui_event_mouse* event) +{ + rtgui_rect_t rect; + + /* let window to process this mouse event */ + if (rtgui_rect_contains_point(&win->extent, event->x, event->y) == RT_EOK) + { + /* send mouse event to thread */ + rtgui_thread_send(win->tid, &(event->parent), sizeof(struct rtgui_event_mouse)); + return; + } + + /* get close button rect (device value) */ + rect.x1 = RTGUI_WIDGET(win->title)->extent.x2 - WINTITLE_BORDER_SIZE - WINTITLE_CB_WIDTH - 3; + rect.y1 = RTGUI_WIDGET(win->title)->extent.y1 + WINTITLE_BORDER_SIZE + 3; + rect.x2 = rect.x1 + WINTITLE_CB_WIDTH; + rect.y2 = rect.y1 + WINTITLE_CB_HEIGHT; + + if (event->button & RTGUI_MOUSE_BUTTON_LEFT) + { + if (event->button & RTGUI_MOUSE_BUTTON_DOWN) + { + if (rtgui_rect_contains_point(&rect, event->x, event->y) == RT_EOK) + { + win->flag |= WINTITLE_CB_PRESSED; + rtgui_theme_draw_win(win); + } + else + { + /* maybe move window */ + rtgui_winrect_set(win); + } + } + else if (event->button & RTGUI_MOUSE_BUTTON_UP) + { + if (rtgui_rect_contains_point(&rect, event->x, event->y) == RT_EOK) + { + struct rtgui_event_win event; + + win->flag &= ~WINTITLE_CB_PRESSED; + rtgui_theme_draw_win(win); + + /* send close event to window */ + RTGUI_EVENT_WIN_CLOSE_INIT(&event); + event.wid = win->wid; + rtgui_thread_send(win->tid, &(event.parent), sizeof(struct rtgui_event_win)); + } + } + } +} + +void rtgui_topwin_append_monitor_rect(struct rtgui_win* wid, rtgui_rect_t* rect) +{ + struct rtgui_topwin* win; + + /* parameters check */ + if (wid == RT_NULL || rect == RT_NULL) return; + + /* find topwin */ + win = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_show_list); + if (win == RT_NULL) + win = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_hide_list); + + if (win == RT_NULL) return; + + /* append rect to top window monitor rect list */ + rtgui_mouse_monitor_append(&(win->monitor_list), rect); +} + +void rtgui_topwin_remove_monitor_rect(struct rtgui_win* wid, rtgui_rect_t* rect) +{ + struct rtgui_topwin* win; + + /* parameters check */ + if (wid == RT_NULL || rect == RT_NULL) return; + + /* find topwin */ + win = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_show_list); + if (win == RT_NULL) + win = rtgui_topwin_search_in_list(wid, &_rtgui_topwin_hide_list); + + if (win == RT_NULL) return; + + /* remove rect from top window monitor rect list */ + rtgui_mouse_monitor_remove(&(win->monitor_list), rect); +} + +void rtgui_topwin_dump() +{ + struct rtgui_list_node* node; + + rtgui_list_foreach(node, &_rtgui_topwin_show_list) + { + struct rtgui_topwin* wnd = rtgui_list_entry(node, struct rtgui_topwin, list); + + rt_kprintf("wnd at (%d, %d) - (%d, %d)\n", + wnd->extent.x1, wnd->extent.y1, wnd->extent.x2, wnd->extent.y2); + + if (wnd->title != RT_NULL) + { + rt_kprintf("title[%s] border (%d, %d) - (%d, %d)\n", wnd->title->title, + RTGUI_WIDGET(wnd->title)->extent.x1, RTGUI_WIDGET(wnd->title)->extent.y1, + RTGUI_WIDGET(wnd->title)->extent.x2, RTGUI_WIDGET(wnd->title)->extent.y2); + } + } +} diff --git a/rtgui/server/topwin.h b/rtgui/server/topwin.h index 64d0a8a767..af5d72b3b8 100644 --- a/rtgui/server/topwin.h +++ b/rtgui/server/topwin.h @@ -1,56 +1,56 @@ -/*
- * File : topwin.h
- * This file is part of RT-Thread RTOS
- * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team
- *
- * The license and distribution terms for this file may be
- * found in the file LICENSE in this distribution or at
- * http://www.rt-thread.org/license/LICENSE
- *
- * Change Logs:
- * Date Author Notes
- * 2009-10-16 Bernard first version
- */
-#ifndef __RTGUI_TOPWIN_H__
-#define __RTGUI_TOPWIN_H__
-
-#include <rtgui/rtgui.h>
-#include <rtgui/list.h>
-#include <rtgui/region.h>
-#include <rtgui/event.h>
-#include <rtgui/widgets/title.h>
-#include <rtgui/rtgui_server.h>
-
-/* add or remove a top win */
-rt_err_t rtgui_topwin_add(struct rtgui_event_win_create* event);
-rt_err_t rtgui_topwin_remove(struct rtgui_win* wid);
-
-/* raise window to front */
-void rtgui_topwin_raise(struct rtgui_win* wid, rt_thread_t sender);
-/* update clip info to a panel */
-void rtgui_topwin_update_clip_to_panel(struct rtgui_panel* panel);
-
-/* show a window */
-void rtgui_topwin_show(struct rtgui_event_win* event);
-/* hide a window */
-void rtgui_topwin_hide(struct rtgui_event_win* event);
-/* move a window */
-void rtgui_topwin_move(struct rtgui_event_win_move* event);
-/* resize a window */
-void rtgui_topwin_resize(struct rtgui_win* wid, rtgui_rect_t* r);
-
-/* get window at (x, y) */
-struct rtgui_topwin* rtgui_topwin_get_wnd(int x, int y);
-
-void rtgui_topwin_activate_win(struct rtgui_topwin* win);
-void rtgui_topwin_deactivate_win(struct rtgui_topwin* win);
-
-/* window title */
-void rtgui_topwin_title_ondraw(struct rtgui_topwin* win);
-void rtgui_topwin_title_onmouse(struct rtgui_topwin* win, struct rtgui_event_mouse* event);
-
-/* monitor rect */
-void rtgui_topwin_append_monitor_rect(struct rtgui_win* wid, rtgui_rect_t* rect);
-void rtgui_topwin_remove_monitor_rect(struct rtgui_win* wid, rtgui_rect_t* rect);
-
-#endif
+/* + * File : topwin.h + * This file is part of RT-Thread RTOS + * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rt-thread.org/license/LICENSE + * + * Change Logs: + * Date Author Notes + * 2009-10-16 Bernard first version + */ +#ifndef __RTGUI_TOPWIN_H__ +#define __RTGUI_TOPWIN_H__ + +#include <rtgui/rtgui.h> +#include <rtgui/list.h> +#include <rtgui/region.h> +#include <rtgui/event.h> +#include <rtgui/widgets/title.h> +#include <rtgui/rtgui_server.h> + +/* add or remove a top win */ +rt_err_t rtgui_topwin_add(struct rtgui_event_win_create* event); +rt_err_t rtgui_topwin_remove(struct rtgui_win* wid); + +/* raise window to front */ +void rtgui_topwin_raise(struct rtgui_win* wid, rt_thread_t sender); +/* update clip info to a panel */ +void rtgui_topwin_update_clip_to_panel(struct rtgui_panel* panel); + +/* show a window */ +void rtgui_topwin_show(struct rtgui_event_win* event); +/* hide a window */ +void rtgui_topwin_hide(struct rtgui_event_win* event); +/* move a window */ +void rtgui_topwin_move(struct rtgui_event_win_move* event); +/* resize a window */ +void rtgui_topwin_resize(struct rtgui_win* wid, rtgui_rect_t* r); + +/* get window at (x, y) */ +struct rtgui_topwin* rtgui_topwin_get_wnd(int x, int y); + +void rtgui_topwin_activate_win(struct rtgui_topwin* win); +void rtgui_topwin_deactivate_win(struct rtgui_topwin* win); + +/* window title */ +void rtgui_topwin_title_ondraw(struct rtgui_topwin* win); +void rtgui_topwin_title_onmouse(struct rtgui_topwin* win, struct rtgui_event_mouse* event); + +/* monitor rect */ +void rtgui_topwin_append_monitor_rect(struct rtgui_win* wid, rtgui_rect_t* rect); +void rtgui_topwin_remove_monitor_rect(struct rtgui_win* wid, rtgui_rect_t* rect); + +#endif diff --git a/rtgui/widgets/box.c b/rtgui/widgets/box.c index a9d16311c8..798f6b8171 100644 --- a/rtgui/widgets/box.c +++ b/rtgui/widgets/box.c @@ -1,340 +1,340 @@ -/*
- * File : box.c
- * This file is part of RT-Thread RTOS
- * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team
- *
- * The license and distribution terms for this file may be
- * found in the file LICENSE in this distribution or at
- * http://www.rt-thread.org/license/LICENSE
- *
- * Change Logs:
- * Date Author Notes
- * 2009-10-16 Bernard first version
- */
-#include <rtgui/dc.h>
-#include <rtgui/widgets/box.h>
-
-static void _rtgui_box_constructor(rtgui_box_t *box)
-{
- /* init widget and set event handler */
- rtgui_widget_set_event_handler(RTGUI_WIDGET(box), rtgui_box_event_handler);
-
- RTGUI_WIDGET(box)->flag |= RTGUI_WIDGET_FLAG_TRANSPARENT;
- rtgui_widget_set_event_handler(RTGUI_WIDGET(box), rtgui_box_event_handler);
-
- /* set proper of control */
- box->orientation = RTGUI_HORIZONTAL;
- box->border_size = RTGUI_BORDER_DEFAULT_WIDTH;
-}
-
-rtgui_type_t *rtgui_box_type_get(void)
-{
- static rtgui_type_t *box_type = RT_NULL;
-
- if (!box_type)
- {
- box_type = rtgui_type_create("box", RTGUI_CONTAINER_TYPE,
- sizeof(rtgui_box_t), RTGUI_CONSTRUCTOR(_rtgui_box_constructor), RT_NULL);
- }
-
- return box_type;
-}
-
-rt_bool_t rtgui_box_event_handler(rtgui_widget_t* widget, rtgui_event_t* event)
-{
- struct rtgui_box* box = (struct rtgui_box*)widget;
-
- RT_ASSERT(box != RT_NULL);
-
- switch (event->type)
- {
- case RTGUI_EVENT_RESIZE:
- /* re-layout */
- rtgui_box_layout(box);
- break;
-
- default:
- return rtgui_container_event_handler(RTGUI_WIDGET(box), event);
- }
-
- return RT_FALSE;
-}
-
-struct rtgui_box* rtgui_box_create(int orientation, rtgui_rect_t* rect)
-{
- struct rtgui_box* box;
-
- box = (struct rtgui_box*) rtgui_widget_create (RTGUI_BOX_TYPE);
- if (box != RT_NULL)
- {
- /* set proper of control */
- rtgui_widget_set_rect(RTGUI_WIDGET(box), rect);
- box->orientation = orientation;
- }
-
- return box;
-}
-
-void rtgui_box_append(struct rtgui_box* box, rtgui_widget_t* widget)
-{
- /* put to box's children list */
- rtgui_container_add_child(RTGUI_CONTAINER(box), widget);
-}
-
-static void rtgui_box_layout_vertical(rtgui_box_t* box)
-{
- rtgui_list_t *node;
- rt_int32_t box_width;
- rt_int32_t space_count;
- rt_int32_t next_x, next_y;
- rt_int32_t total_height, space_height;
- struct rtgui_event_resize size_event;
-
- /* prepare the resize event */
- RTGUI_EVENT_RESIZE_INIT(&size_event);
-
- /* find spaces */
- space_count = 0;
- total_height = 0;
- space_height = 0;
-
- rtgui_list_foreach(node, &(RTGUI_CONTAINER(box)->children))
- {
- rtgui_widget_t* widget = rtgui_list_entry(node, struct rtgui_widget, sibling);
- if (widget->align & RTGUI_ALIGN_STRETCH) space_count ++;
- else total_height += widget->mini_height;
- }
-
- /* calculate the height for each spaces */
- if (space_count != 0)
- {
- space_height = (rtgui_rect_height(RTGUI_WIDGET(box)->extent) - total_height - (box->border_size << 1)) / space_count;
- }
-
- /* init (x, y) and box width */
- next_x = RTGUI_WIDGET(box)->extent.x1 + box->border_size;
- next_y = RTGUI_WIDGET(box)->extent.y1 + box->border_size;
- box_width = rtgui_rect_width(RTGUI_WIDGET(box)->extent) - (box->border_size << 1);
-
- /* layout each widget */
- rtgui_list_foreach(node, &(RTGUI_CONTAINER(box)->children))
- {
- rtgui_rect_t *rect;
- rtgui_widget_t* widget = rtgui_list_entry(node, struct rtgui_widget, sibling);
-
- /* get extent of widget */
- rect = &(widget->extent);
-
- /* reset rect */
- rtgui_rect_moveto(rect, -rect->x1, -rect->y1);
- rect->x2 = widget->mini_width;
- rect->y2 = widget->mini_height;
-
- /* left in default */
- rtgui_rect_moveto(rect, next_x, next_y);
-
- if (widget->align & RTGUI_ALIGN_EXPAND)
- {
- /* expand on horizontal */
- rect->x2 = rect->x1 + (rt_int16_t)box_width;
- }
- if (widget->align & RTGUI_ALIGN_CENTER_VERTICAL)
- {
- /* center */
- rt_uint32_t mid;
-
- mid = box_width - rtgui_rect_width(*rect);
- mid = mid /2;
-
- rect->x1 = next_x + mid;
- rect->x2 = next_x + box_width - mid;
- }
- else if (widget->align & RTGUI_ALIGN_RIGHT)
- {
- /* right */
- rect->x1 = next_x + box_width - rtgui_rect_width(*rect);
- rect->x2 = next_x + box_width;
- }
-
- if (widget->align & RTGUI_ALIGN_STRETCH)
- {
- rect->y2 = rect->y1 + space_height;
- }
-
- /* process resize event */
- size_event.x = rect->x1;
- size_event.y = rect->y1;
- size_event.w = rect->x2 - rect->x1;
- size_event.h = rect->y2 - rect->y1;
- widget->event_handler(widget, &size_event.parent);
-
- /* point to next height */
- next_y = rect->y2;
- }
-}
-
-static void rtgui_box_layout_horizontal(rtgui_box_t* box)
-{
- rtgui_list_t *node;
- rt_int32_t box_height;
- rt_int32_t space_count;
- rt_int32_t next_x, next_y;
- rt_int32_t total_width, space_width;
- struct rtgui_event_resize size_event;
-
- /* prepare the resize event */
- RTGUI_EVENT_RESIZE_INIT(&size_event);
-
- /* find spaces */
- space_count = 0;
- total_width = 0;
- space_width = 0;
-
- rtgui_list_foreach(node, &(RTGUI_CONTAINER(box)->children))
- {
- rtgui_widget_t* widget = rtgui_list_entry(node, struct rtgui_widget, sibling);
- if (widget->align & RTGUI_ALIGN_STRETCH) space_count ++;
- else total_width += widget->mini_width;
- }
-
- if (space_count != 0)
- {
- /* calculate the height for each spaces */
- space_width = (rtgui_rect_width(RTGUI_WIDGET(box)->extent) - total_width) / space_count;
- }
-
- /* init (x, y) and box height */
- next_x = RTGUI_WIDGET(box)->extent.x1 + box->border_size;
- next_y = RTGUI_WIDGET(box)->extent.y1 + box->border_size;
- box_height = rtgui_rect_height(RTGUI_WIDGET(box)->extent) - (box->border_size << 1);
-
- /* layout each widget */
- rtgui_list_foreach(node, &(RTGUI_CONTAINER(box)->children))
- {
- rtgui_rect_t *rect;
- rtgui_widget_t* widget = rtgui_list_entry(node, struct rtgui_widget, sibling);
-
- /* get extent of widget */
- rect = &(widget->extent);
-
- /* reset rect */
- rtgui_rect_moveto(rect, -rect->x1, -rect->y1);
- rect->x2 = widget->mini_width;
- rect->y2 = widget->mini_height;
-
- /* top in default */
- rtgui_rect_moveto(rect, next_x, next_y);
-
- if (widget->align & RTGUI_ALIGN_EXPAND)
- {
- /* expand on vertical */
- rect->y2 = rect->y1 + box_height;
- }
- if (widget->align & RTGUI_ALIGN_CENTER_HORIZONTAL)
- {
- /* center */
- rt_uint32_t mid;
-
- mid = box_height - rtgui_rect_height(*rect);
- mid = mid /2;
-
- rect->y1 = next_y + mid;
- rect->y2 = next_y + box_height - mid;
- }
- else if (widget->align & RTGUI_ALIGN_RIGHT)
- {
- /* right */
- rect->y1 = next_y + box_height - rtgui_rect_height(*rect);
- rect->y2 = next_y + box_height;
- }
-
- if (widget->align & RTGUI_ALIGN_STRETCH)
- {
- rect->x2 = rect->x1 + space_width;
- }
-
- /* process resize event */
- size_event.x = rect->x1;
- size_event.y = rect->y1;
- size_event.w = rect->x2 - rect->x1;
- size_event.h = rect->y2 - rect->y1;
- widget->event_handler(widget, &size_event.parent);
-
- /* point to next width */
- next_x = rect->x2;
- }
-}
-
-void rtgui_box_layout(rtgui_box_t* box)
-{
- RT_ASSERT(box != RT_NULL);
-
- if (box->orientation & RTGUI_VERTICAL)
- {
- rtgui_box_layout_vertical(box);
- }
- else
- {
- rtgui_box_layout_horizontal(box);
- }
-
- /* update box and its children clip */
- if (!RTGUI_WIDGET_IS_HIDE(RTGUI_WIDGET(box)))
- {
- rtgui_widget_update_clip(RTGUI_WIDGET(box));
- }
-}
-
-rt_uint32_t rtgui_box_get_width(rtgui_box_t* box)
-{
- rtgui_list_t *node;
- rt_uint32_t width;
-
- width = 0;
- rtgui_list_foreach(node, &(RTGUI_CONTAINER(box)->children))
- {
- rtgui_widget_t* widget = rtgui_list_entry(node, struct rtgui_widget, sibling);
- rt_uint32_t widget_width;
-
- widget_width = rtgui_rect_width(widget->extent);
- if (box->orientation & RTGUI_VERTICAL)
- {
- /* get the max width */
- if (width < widget_width) width = widget_width;
- }
- else
- {
- /* get the total width */
- width += widget_width;
- }
- }
-
- return width;
-}
-
-rt_uint32_t rtgui_box_get_height(rtgui_box_t* box)
-{
- rtgui_list_t *node;
- rt_uint32_t height;
-
- height = 0;
- rtgui_list_foreach(node, &(RTGUI_CONTAINER(box)->children))
- {
- rtgui_widget_t* widget = rtgui_list_entry(node, struct rtgui_widget, sibling);
- rt_uint32_t widget_height;
-
- widget_height = rtgui_rect_height(widget->extent);
- if (box->orientation & RTGUI_HORIZONTAL)
- {
- /* get the max height */
- if (height < widget_height) height = widget_height;
- }
- else
- {
- /* get the total height */
- height += widget_height;
- }
- }
-
- return height;
-}
+/* + * File : box.c + * This file is part of RT-Thread RTOS + * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rt-thread.org/license/LICENSE + * + * Change Logs: + * Date Author Notes + * 2009-10-16 Bernard first version + */ +#include <rtgui/dc.h> +#include <rtgui/widgets/box.h> + +static void _rtgui_box_constructor(rtgui_box_t *box) +{ + /* init widget and set event handler */ + rtgui_widget_set_event_handler(RTGUI_WIDGET(box), rtgui_box_event_handler); + + RTGUI_WIDGET(box)->flag |= RTGUI_WIDGET_FLAG_TRANSPARENT; + rtgui_widget_set_event_handler(RTGUI_WIDGET(box), rtgui_box_event_handler); + + /* set proper of control */ + box->orientation = RTGUI_HORIZONTAL; + box->border_size = RTGUI_BORDER_DEFAULT_WIDTH; +} + +rtgui_type_t *rtgui_box_type_get(void) +{ + static rtgui_type_t *box_type = RT_NULL; + + if (!box_type) + { + box_type = rtgui_type_create("box", RTGUI_CONTAINER_TYPE, + sizeof(rtgui_box_t), RTGUI_CONSTRUCTOR(_rtgui_box_constructor), RT_NULL); + } + + return box_type; +} + +rt_bool_t rtgui_box_event_handler(rtgui_widget_t* widget, rtgui_event_t* event) +{ + struct rtgui_box* box = (struct rtgui_box*)widget; + + RT_ASSERT(box != RT_NULL); + + switch (event->type) + { + case RTGUI_EVENT_RESIZE: + /* re-layout */ + rtgui_box_layout(box); + break; + + default: + return rtgui_container_event_handler(RTGUI_WIDGET(box), event); + } + + return RT_FALSE; +} + +struct rtgui_box* rtgui_box_create(int orientation, rtgui_rect_t* rect) +{ + struct rtgui_box* box; + + box = (struct rtgui_box*) rtgui_widget_create (RTGUI_BOX_TYPE); + if (box != RT_NULL) + { + /* set proper of control */ + rtgui_widget_set_rect(RTGUI_WIDGET(box), rect); + box->orientation = orientation; + } + + return box; +} + +void rtgui_box_append(struct rtgui_box* box, rtgui_widget_t* widget) +{ + /* put to box's children list */ + rtgui_container_add_child(RTGUI_CONTAINER(box), widget); +} + +static void rtgui_box_layout_vertical(rtgui_box_t* box) +{ + rtgui_list_t *node; + rt_int32_t box_width; + rt_int32_t space_count; + rt_int32_t next_x, next_y; + rt_int32_t total_height, space_height; + struct rtgui_event_resize size_event; + + /* prepare the resize event */ + RTGUI_EVENT_RESIZE_INIT(&size_event); + + /* find spaces */ + space_count = 0; + total_height = 0; + space_height = 0; + + rtgui_list_foreach(node, &(RTGUI_CONTAINER(box)->children)) + { + rtgui_widget_t* widget = rtgui_list_entry(node, struct rtgui_widget, sibling); + if (widget->align & RTGUI_ALIGN_STRETCH) space_count ++; + else total_height += widget->mini_height; + } + + /* calculate the height for each spaces */ + if (space_count != 0) + { + space_height = (rtgui_rect_height(RTGUI_WIDGET(box)->extent) - total_height - (box->border_size << 1)) / space_count; + } + + /* init (x, y) and box width */ + next_x = RTGUI_WIDGET(box)->extent.x1 + box->border_size; + next_y = RTGUI_WIDGET(box)->extent.y1 + box->border_size; + box_width = rtgui_rect_width(RTGUI_WIDGET(box)->extent) - (box->border_size << 1); + + /* layout each widget */ + rtgui_list_foreach(node, &(RTGUI_CONTAINER(box)->children)) + { + rtgui_rect_t *rect; + rtgui_widget_t* widget = rtgui_list_entry(node, struct rtgui_widget, sibling); + + /* get extent of widget */ + rect = &(widget->extent); + + /* reset rect */ + rtgui_rect_moveto(rect, -rect->x1, -rect->y1); + rect->x2 = widget->mini_width; + rect->y2 = widget->mini_height; + + /* left in default */ + rtgui_rect_moveto(rect, next_x, next_y); + + if (widget->align & RTGUI_ALIGN_EXPAND) + { + /* expand on horizontal */ + rect->x2 = rect->x1 + (rt_int16_t)box_width; + } + if (widget->align & RTGUI_ALIGN_CENTER_VERTICAL) + { + /* center */ + rt_uint32_t mid; + + mid = box_width - rtgui_rect_width(*rect); + mid = mid /2; + + rect->x1 = next_x + mid; + rect->x2 = next_x + box_width - mid; + } + else if (widget->align & RTGUI_ALIGN_RIGHT) + { + /* right */ + rect->x1 = next_x + box_width - rtgui_rect_width(*rect); + rect->x2 = next_x + box_width; + } + + if (widget->align & RTGUI_ALIGN_STRETCH) + { + rect->y2 = rect->y1 + space_height; + } + + /* process resize event */ + size_event.x = rect->x1; + size_event.y = rect->y1; + size_event.w = rect->x2 - rect->x1; + size_event.h = rect->y2 - rect->y1; + widget->event_handler(widget, &size_event.parent); + + /* point to next height */ + next_y = rect->y2; + } +} + +static void rtgui_box_layout_horizontal(rtgui_box_t* box) +{ + rtgui_list_t *node; + rt_int32_t box_height; + rt_int32_t space_count; + rt_int32_t next_x, next_y; + rt_int32_t total_width, space_width; + struct rtgui_event_resize size_event; + + /* prepare the resize event */ + RTGUI_EVENT_RESIZE_INIT(&size_event); + + /* find spaces */ + space_count = 0; + total_width = 0; + space_width = 0; + + rtgui_list_foreach(node, &(RTGUI_CONTAINER(box)->children)) + { + rtgui_widget_t* widget = rtgui_list_entry(node, struct rtgui_widget, sibling); + if (widget->align & RTGUI_ALIGN_STRETCH) space_count ++; + else total_width += widget->mini_width; + } + + if (space_count != 0) + { + /* calculate the height for each spaces */ + space_width = (rtgui_rect_width(RTGUI_WIDGET(box)->extent) - total_width) / space_count; + } + + /* init (x, y) and box height */ + next_x = RTGUI_WIDGET(box)->extent.x1 + box->border_size; + next_y = RTGUI_WIDGET(box)->extent.y1 + box->border_size; + box_height = rtgui_rect_height(RTGUI_WIDGET(box)->extent) - (box->border_size << 1); + + /* layout each widget */ + rtgui_list_foreach(node, &(RTGUI_CONTAINER(box)->children)) + { + rtgui_rect_t *rect; + rtgui_widget_t* widget = rtgui_list_entry(node, struct rtgui_widget, sibling); + + /* get extent of widget */ + rect = &(widget->extent); + + /* reset rect */ + rtgui_rect_moveto(rect, -rect->x1, -rect->y1); + rect->x2 = widget->mini_width; + rect->y2 = widget->mini_height; + + /* top in default */ + rtgui_rect_moveto(rect, next_x, next_y); + + if (widget->align & RTGUI_ALIGN_EXPAND) + { + /* expand on vertical */ + rect->y2 = rect->y1 + box_height; + } + if (widget->align & RTGUI_ALIGN_CENTER_HORIZONTAL) + { + /* center */ + rt_uint32_t mid; + + mid = box_height - rtgui_rect_height(*rect); + mid = mid /2; + + rect->y1 = next_y + mid; + rect->y2 = next_y + box_height - mid; + } + else if (widget->align & RTGUI_ALIGN_RIGHT) + { + /* right */ + rect->y1 = next_y + box_height - rtgui_rect_height(*rect); + rect->y2 = next_y + box_height; + } + + if (widget->align & RTGUI_ALIGN_STRETCH) + { + rect->x2 = rect->x1 + space_width; + } + + /* process resize event */ + size_event.x = rect->x1; + size_event.y = rect->y1; + size_event.w = rect->x2 - rect->x1; + size_event.h = rect->y2 - rect->y1; + widget->event_handler(widget, &size_event.parent); + + /* point to next width */ + next_x = rect->x2; + } +} + +void rtgui_box_layout(rtgui_box_t* box) +{ + RT_ASSERT(box != RT_NULL); + + if (box->orientation & RTGUI_VERTICAL) + { + rtgui_box_layout_vertical(box); + } + else + { + rtgui_box_layout_horizontal(box); + } + + /* update box and its children clip */ + if (!RTGUI_WIDGET_IS_HIDE(RTGUI_WIDGET(box))) + { + rtgui_widget_update_clip(RTGUI_WIDGET(box)); + } +} + +rt_uint32_t rtgui_box_get_width(rtgui_box_t* box) +{ + rtgui_list_t *node; + rt_uint32_t width; + + width = 0; + rtgui_list_foreach(node, &(RTGUI_CONTAINER(box)->children)) + { + rtgui_widget_t* widget = rtgui_list_entry(node, struct rtgui_widget, sibling); + rt_uint32_t widget_width; + + widget_width = rtgui_rect_width(widget->extent); + if (box->orientation & RTGUI_VERTICAL) + { + /* get the max width */ + if (width < widget_width) width = widget_width; + } + else + { + /* get the total width */ + width += widget_width; + } + } + + return width; +} + +rt_uint32_t rtgui_box_get_height(rtgui_box_t* box) +{ + rtgui_list_t *node; + rt_uint32_t height; + + height = 0; + rtgui_list_foreach(node, &(RTGUI_CONTAINER(box)->children)) + { + rtgui_widget_t* widget = rtgui_list_entry(node, struct rtgui_widget, sibling); + rt_uint32_t widget_height; + + widget_height = rtgui_rect_height(widget->extent); + if (box->orientation & RTGUI_HORIZONTAL) + { + /* get the max height */ + if (height < widget_height) height = widget_height; + } + else + { + /* get the total height */ + height += widget_height; + } + } + + return height; +} diff --git a/rtgui/widgets/button.c b/rtgui/widgets/button.c index f5bd8877d4..1a563f0dbb 100644 --- a/rtgui/widgets/button.c +++ b/rtgui/widgets/button.c @@ -1,187 +1,187 @@ -/*
- * File : button.c
- * This file is part of RT-Thread RTOS
- * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team
- *
- * The license and distribution terms for this file may be
- * found in the file LICENSE in this distribution or at
- * http://www.rt-thread.org/license/LICENSE
- *
- * Change Logs:
- * Date Author Notes
- * 2009-10-16 Bernard first version
- */
-#include <rtgui/dc.h>
-#include <rtgui/widgets/button.h>
-#include <rtgui/rtgui_theme.h>
-
-static void _rtgui_button_constructor(rtgui_button_t *button)
-{
- /* init widget and set event handler */
- RTGUI_WIDGET(button)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE;
- rtgui_widget_set_event_handler(RTGUI_WIDGET(button), rtgui_button_event_handler);
-
- /* un-press button */
- button->flag &= ~RTGUI_BUTTON_FLAG_PRESS;
-
- /* set flag and on_button event handler */
- button->pressed_image = RT_NULL;
- button->unpressed_image = RT_NULL;
- button->on_button = RT_NULL;
-
- /* set gc */
- RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(button)) = default_foreground;
- RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(button)) = RTGUI_RGB(212, 208, 200);
- RTGUI_WIDGET_TEXTALIGN(RTGUI_WIDGET(button)) = RTGUI_ALIGN_CENTER_HORIZONTAL | RTGUI_ALIGN_CENTER_VERTICAL;
-}
-
-static void _rtgui_button_destructor(rtgui_button_t *button)
-{
- if (button->pressed_image != RT_NULL)
- {
- rtgui_image_destroy(button->pressed_image);
- button->pressed_image = RT_NULL;
- }
-
- if (button->unpressed_image != RT_NULL)
- {
- rtgui_image_destroy(button->unpressed_image);
- button->unpressed_image = RT_NULL;
- }
-}
-
-rtgui_type_t *rtgui_button_type_get(void)
-{
- static rtgui_type_t *button_type = RT_NULL;
-
- if (!button_type)
- {
- button_type = rtgui_type_create("button", RTGUI_LABEL_TYPE,
- sizeof(rtgui_button_t),
- RTGUI_CONSTRUCTOR(_rtgui_button_constructor),
- RTGUI_DESTRUCTOR(_rtgui_button_destructor));
- }
-
- return button_type;
-}
-
-rt_bool_t rtgui_button_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
-{
- struct rtgui_button* btn;
-
- RT_ASSERT(widget != RT_NULL);
-
- btn = (struct rtgui_button*) widget;
- switch (event->type)
- {
- case RTGUI_EVENT_PAINT:
- if (widget->on_draw != RT_NULL ) widget->on_draw(widget, event);
- else rtgui_theme_draw_button(btn);
- break;
-
- case RTGUI_EVENT_MOUSE_BUTTON:
- {
- struct rtgui_event_mouse* emouse = (struct rtgui_event_mouse*)event;
-
- if (btn->flag & RTGUI_BUTTON_TYPE_PUSH)
- {
- /* it's a push button */
- if (emouse->button & RTGUI_MOUSE_BUTTON_UP)
- {
- if (btn->flag & RTGUI_BUTTON_FLAG_PRESS)
- btn->flag &= ~RTGUI_BUTTON_FLAG_PRESS;
- else
- btn->flag |= RTGUI_BUTTON_FLAG_PRESS;
-
- /* draw button */
- if (widget->on_draw != RT_NULL ) widget->on_draw(widget, event);
- else rtgui_theme_draw_button(btn);
-
- /* invokes call back */
- if (widget->on_mouseclick != RT_NULL &&
- emouse->button & RTGUI_MOUSE_BUTTON_UP)
- return widget->on_mouseclick(widget, event);
- }
- }
- else
- {
- if (emouse->button & RTGUI_MOUSE_BUTTON_LEFT)
- {
- /* it's a normal button */
- if (emouse->button & RTGUI_MOUSE_BUTTON_DOWN)
- {
- btn->flag |= RTGUI_BUTTON_FLAG_PRESS;
- }
- else
- {
- btn->flag &= ~RTGUI_BUTTON_FLAG_PRESS;
- }
-
- /* draw button */
- if (widget->on_draw != RT_NULL ) widget->on_draw(widget, event);
- else rtgui_theme_draw_button(btn);
-
- /* invokes call back */
- if (widget->on_mouseclick != RT_NULL &&
- emouse->button & RTGUI_MOUSE_BUTTON_UP)
- return widget->on_mouseclick(widget, event);
- }
-
- }
-
- return RT_TRUE;
- }
- }
-
- return RT_FALSE;
-}
-
-rtgui_button_t* rtgui_button_create(unsigned char* text)
-{
- struct rtgui_button* btn;
-
- btn = (struct rtgui_button*) rtgui_widget_create (RTGUI_BUTTON_TYPE);
- if (btn != RT_NULL)
- {
- rtgui_rect_t rect;
-
- /* set default rect */
- rtgui_font_get_metrics(rtgui_font_default(), text, &rect);
- rect.x2 += (RTGUI_BORDER_DEFAULT_WIDTH << 1);
- rect.y2 += (RTGUI_BORDER_DEFAULT_WIDTH << 1);
- rtgui_widget_set_rect(RTGUI_WIDGET(btn), &rect);
- rtgui_label_set_text(RTGUI_LABEL(btn), text);
- }
-
- return btn;
-}
-
-rtgui_button_t* rtgui_pushbutton_create(unsigned char* text)
-{
- rtgui_button_t* btn;
-
- btn = rtgui_button_create(text);
- if (btn != RT_NULL) btn->flag |= RTGUI_BUTTON_TYPE_PUSH;
-
- return btn;
-}
-
-void rtgui_button_destroy(rtgui_button_t* btn)
-{
- rtgui_widget_destroy(RTGUI_WIDGET(btn));
-}
-
-void rtgui_button_set_pressed_image(rtgui_button_t* btn, rtgui_image_t* image)
-{
- RT_ASSERT(btn != RT_NULL);
-
- btn->pressed_image = image;
-}
-
-void rtgui_button_set_unpressed_image(rtgui_button_t* btn, rtgui_image_t* image)
-{
- RT_ASSERT(btn != RT_NULL);
-
- btn->unpressed_image = image;
-}
-
+/* + * File : button.c + * This file is part of RT-Thread RTOS + * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rt-thread.org/license/LICENSE + * + * Change Logs: + * Date Author Notes + * 2009-10-16 Bernard first version + */ +#include <rtgui/dc.h> +#include <rtgui/widgets/button.h> +#include <rtgui/rtgui_theme.h> + +static void _rtgui_button_constructor(rtgui_button_t *button) +{ + /* init widget and set event handler */ + RTGUI_WIDGET(button)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE; + rtgui_widget_set_event_handler(RTGUI_WIDGET(button), rtgui_button_event_handler); + + /* un-press button */ + button->flag &= ~RTGUI_BUTTON_FLAG_PRESS; + + /* set flag and on_button event handler */ + button->pressed_image = RT_NULL; + button->unpressed_image = RT_NULL; + button->on_button = RT_NULL; + + /* set gc */ + RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(button)) = default_foreground; + RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(button)) = RTGUI_RGB(212, 208, 200); + RTGUI_WIDGET_TEXTALIGN(RTGUI_WIDGET(button)) = RTGUI_ALIGN_CENTER_HORIZONTAL | RTGUI_ALIGN_CENTER_VERTICAL; +} + +static void _rtgui_button_destructor(rtgui_button_t *button) +{ + if (button->pressed_image != RT_NULL) + { + rtgui_image_destroy(button->pressed_image); + button->pressed_image = RT_NULL; + } + + if (button->unpressed_image != RT_NULL) + { + rtgui_image_destroy(button->unpressed_image); + button->unpressed_image = RT_NULL; + } +} + +rtgui_type_t *rtgui_button_type_get(void) +{ + static rtgui_type_t *button_type = RT_NULL; + + if (!button_type) + { + button_type = rtgui_type_create("button", RTGUI_LABEL_TYPE, + sizeof(rtgui_button_t), + RTGUI_CONSTRUCTOR(_rtgui_button_constructor), + RTGUI_DESTRUCTOR(_rtgui_button_destructor)); + } + + return button_type; +} + +rt_bool_t rtgui_button_event_handler(struct rtgui_widget* widget, struct rtgui_event* event) +{ + struct rtgui_button* btn; + + RT_ASSERT(widget != RT_NULL); + + btn = (struct rtgui_button*) widget; + switch (event->type) + { + case RTGUI_EVENT_PAINT: + if (widget->on_draw != RT_NULL ) widget->on_draw(widget, event); + else rtgui_theme_draw_button(btn); + break; + + case RTGUI_EVENT_MOUSE_BUTTON: + { + struct rtgui_event_mouse* emouse = (struct rtgui_event_mouse*)event; + + if (btn->flag & RTGUI_BUTTON_TYPE_PUSH) + { + /* it's a push button */ + if (emouse->button & RTGUI_MOUSE_BUTTON_UP) + { + if (btn->flag & RTGUI_BUTTON_FLAG_PRESS) + btn->flag &= ~RTGUI_BUTTON_FLAG_PRESS; + else + btn->flag |= RTGUI_BUTTON_FLAG_PRESS; + + /* draw button */ + if (widget->on_draw != RT_NULL ) widget->on_draw(widget, event); + else rtgui_theme_draw_button(btn); + + /* invokes call back */ + if (widget->on_mouseclick != RT_NULL && + emouse->button & RTGUI_MOUSE_BUTTON_UP) + return widget->on_mouseclick(widget, event); + } + } + else + { + if (emouse->button & RTGUI_MOUSE_BUTTON_LEFT) + { + /* it's a normal button */ + if (emouse->button & RTGUI_MOUSE_BUTTON_DOWN) + { + btn->flag |= RTGUI_BUTTON_FLAG_PRESS; + } + else + { + btn->flag &= ~RTGUI_BUTTON_FLAG_PRESS; + } + + /* draw button */ + if (widget->on_draw != RT_NULL ) widget->on_draw(widget, event); + else rtgui_theme_draw_button(btn); + + /* invokes call back */ + if (widget->on_mouseclick != RT_NULL && + emouse->button & RTGUI_MOUSE_BUTTON_UP) + return widget->on_mouseclick(widget, event); + } + + } + + return RT_TRUE; + } + } + + return RT_FALSE; +} + +rtgui_button_t* rtgui_button_create(unsigned char* text) +{ + struct rtgui_button* btn; + + btn = (struct rtgui_button*) rtgui_widget_create (RTGUI_BUTTON_TYPE); + if (btn != RT_NULL) + { + rtgui_rect_t rect; + + /* set default rect */ + rtgui_font_get_metrics(rtgui_font_default(), text, &rect); + rect.x2 += (RTGUI_BORDER_DEFAULT_WIDTH << 1); + rect.y2 += (RTGUI_BORDER_DEFAULT_WIDTH << 1); + rtgui_widget_set_rect(RTGUI_WIDGET(btn), &rect); + rtgui_label_set_text(RTGUI_LABEL(btn), text); + } + + return btn; +} + +rtgui_button_t* rtgui_pushbutton_create(unsigned char* text) +{ + rtgui_button_t* btn; + + btn = rtgui_button_create(text); + if (btn != RT_NULL) btn->flag |= RTGUI_BUTTON_TYPE_PUSH; + + return btn; +} + +void rtgui_button_destroy(rtgui_button_t* btn) +{ + rtgui_widget_destroy(RTGUI_WIDGET(btn)); +} + +void rtgui_button_set_pressed_image(rtgui_button_t* btn, rtgui_image_t* image) +{ + RT_ASSERT(btn != RT_NULL); + + btn->pressed_image = image; +} + +void rtgui_button_set_unpressed_image(rtgui_button_t* btn, rtgui_image_t* image) +{ + RT_ASSERT(btn != RT_NULL); + + btn->unpressed_image = image; +} + diff --git a/rtgui/widgets/container.c b/rtgui/widgets/container.c index 1a233807c6..8771f70b77 100644 --- a/rtgui/widgets/container.c +++ b/rtgui/widgets/container.c @@ -1,279 +1,291 @@ -/*
- * File : container.c
- * This file is part of RT-Thread RTOS
- * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team
- *
- * The license and distribution terms for this file may be
- * found in the file LICENSE in this distribution or at
- * http://www.rt-thread.org/license/LICENSE
- *
- * Change Logs:
- * Date Author Notes
- * 2009-10-16 Bernard first version
- */
-#include <rtgui/widgets/toplevel.h>
-#include <rtgui/widgets/container.h>
-
-static void _rtgui_container_constructor(rtgui_container_t *container)
-{
- /* set event handler and init field */
- rtgui_widget_set_event_handler(RTGUI_WIDGET(container), rtgui_container_event_handler);
- rtgui_list_init(&(container->children));
-}
-
-static void _rtgui_container_destructor(rtgui_container_t *container)
-{
- /* destroy children of container */
- rtgui_container_destroy_children(container);
-}
-
-static void _rtgui_container_update_toplevel(rtgui_container_t* container)
-{
- struct rtgui_list_node* node;
-
- rtgui_list_foreach(node, &(container->children))
- {
- rtgui_widget_t* child = rtgui_list_entry(node, rtgui_widget_t, sibling);
- /* set child toplevel */
- child->toplevel = rtgui_widget_get_toplevel(RTGUI_WIDGET(container));
-
- if (RTGUI_IS_CONTAINER(child))
- {
- _rtgui_container_update_toplevel(RTGUI_CONTAINER(child));
- }
- }
-}
-
-rtgui_type_t *rtgui_container_type_get(void)
-{
- static rtgui_type_t *container_type = RT_NULL;
-
- if (!container_type)
- {
- container_type = rtgui_type_create("container", RTGUI_WIDGET_TYPE,
- sizeof(rtgui_container_t),
- RTGUI_CONSTRUCTOR(_rtgui_container_constructor),
- RTGUI_DESTRUCTOR(_rtgui_container_destructor));
- }
-
- return container_type;
-}
-
-rt_bool_t rtgui_container_dispatch_event(rtgui_container_t *container, rtgui_event_t* event)
-{
- /* handle in child widget */
- struct rtgui_list_node* node;
-
- rtgui_list_foreach(node, &(container->children))
- {
- struct rtgui_widget* w;
- w = rtgui_list_entry(node, struct rtgui_widget, sibling);
-
- if (w->event_handler(w, event) == RT_TRUE) return RT_TRUE;
- }
-
- return RT_FALSE;
-}
-
-rt_bool_t rtgui_container_dispatch_mouse_event(rtgui_container_t *container, struct rtgui_event_mouse* event)
-{
- /* handle in child widget */
- struct rtgui_list_node* node;
-
- rtgui_list_foreach(node, &(container->children))
- {
- struct rtgui_widget* w;
- w = rtgui_list_entry(node, struct rtgui_widget, sibling);
- if (rtgui_rect_contains_point(&(w->extent), event->x, event->y) == RT_EOK)
- {
- if (w->event_handler(w, (rtgui_event_t*)event) == RT_TRUE) return RT_TRUE;
- }
- }
-
- return RT_FALSE;
-}
-
-rt_bool_t rtgui_container_event_handler(rtgui_widget_t* widget, rtgui_event_t* event)
-{
- rtgui_container_t *container = RTGUI_CONTAINER(widget);
-
- switch (event->type)
- {
- case RTGUI_EVENT_PAINT:
- if (widget->on_draw != RT_NULL)
- {
- return widget->on_draw(widget, event);
- }
-
- rtgui_container_dispatch_event(container, event);
- break;
-
- case RTGUI_EVENT_KBD:
- if (widget->on_key != RT_NULL)
- {
- return widget->on_key(widget, event);
- }
- else
- {
- /* let parent to handle keyboard event */
- if (widget->parent != RT_NULL && widget->parent != widget->toplevel)
- {
- return widget->parent->event_handler(widget->parent, event);
- }
- }
- break;
-
- case RTGUI_EVENT_MOUSE_BUTTON:
- /* handle in child widget */
- if (rtgui_container_dispatch_mouse_event(container,
- (struct rtgui_event_mouse*)event) == RT_FALSE)
- {
- /* handle event in current widget */
- if (widget->on_mouseclick != RT_NULL)
- {
- return widget->on_mouseclick(widget, event);
- }
- }
- else return RT_TRUE;
- break;
-
- case RTGUI_EVENT_MOUSE_MOTION:
-#if 0
- if (rtgui_container_dispatch_mouse_event(container,
- (struct rtgui_event_mouse*)event) == RT_FALSE)
- {
- /* handle event in current widget */
- if (widget->on_mousemotion != RT_NULL)
- {
- return widget->on_mousemotion(widget, event);
- }
- }
- else return RT_TRUE;
-#endif
- break;
-
- case RTGUI_EVENT_COMMAND:
- if (rtgui_container_dispatch_event(container, event) == RT_FALSE)
- {
- if (widget->on_command != RT_NULL)
- {
- return widget->on_command(widget, event);
- }
- }
- else return RT_TRUE;
- break;
-
- case RTGUI_EVENT_RESIZE:
- if (rtgui_container_dispatch_event(container, event) == RT_FALSE)
- {
- if (widget->on_size != RT_NULL)
- return widget->on_size(widget, event);
- }
- else return RT_TRUE;
- break;
-
- default:
- /* call parent widget event handler */
- return rtgui_widget_event_handler(widget, event);
- }
-
- return RT_FALSE;
-}
-
-/*
- * This function will add a child to a container widget
- * Note: this function will not change the widget layout
- * the layout is the responsibility of layout widget, such as box.
- */
-void rtgui_container_add_child(rtgui_container_t *container, rtgui_widget_t* child)
-{
- RT_ASSERT(container != RT_NULL);
- RT_ASSERT(child != RT_NULL);
-
- /* set parent and toplevel widget */
- child->parent = RTGUI_WIDGET(container);
- /* put widget to parent's children list */
- rtgui_list_append(&(container->children), &(child->sibling));
-
- /* update children toplevel */
- if (RTGUI_WIDGET(container)->toplevel != RT_NULL &&
- RTGUI_IS_TOPLEVEL(RTGUI_WIDGET(container)->toplevel))
- {
- child->toplevel = rtgui_widget_get_toplevel(RTGUI_WIDGET(container));
-
- /* update all child toplevel */
- if (RTGUI_IS_CONTAINER(child))
- {
- _rtgui_container_update_toplevel(RTGUI_CONTAINER(child));
- }
- }
-}
-
-/* remove a child to widget */
-void rtgui_container_remove_child(rtgui_container_t *container, rtgui_widget_t* child)
-{
- RT_ASSERT(container != RT_NULL);
- RT_ASSERT(child != RT_NULL);
-
- /* remove widget from parent's children list */
- rtgui_list_remove(&(container->children), &(child->sibling));
-
- /* set parent and toplevel widget */
- child->parent = RT_NULL;
- child->toplevel = RT_NULL;
-}
-
-/* destroy all children of container */
-void rtgui_container_destroy_children(rtgui_container_t *container)
-{
- struct rtgui_list_node* node;
-
- if (container == RT_NULL) return;
-
- node = container->children.next;
- while (node != RT_NULL)
- {
- rtgui_widget_t* child = rtgui_list_entry(node, rtgui_widget_t, sibling);
-
- if (RTGUI_IS_CONTAINER(child))
- {
- /* destroy children of child */
- rtgui_container_destroy_children(RTGUI_CONTAINER(child));
- }
-
- /* destroy object and remove from parent */
- rtgui_object_destroy(RTGUI_OBJECT(child));
-
- node = container->children.next;
- }
-
- container->children.next = RT_NULL;
-
- /* update widget clip */
-#if 0
- rtgui_widget_update_clip(RTGUI_WIDGET(container));
-#else
- /* update toplevel widget clip */
-#if 0
- {
- rtgui_toplevel_t* top;
-
- top = RTGUI_TOPLEVEL(RTGUI_WIDGET(container)->toplevel);
- if (RTGUI_IS_VIEW(top))
- }
-#endif
- rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(RTGUI_WIDGET(container)->toplevel));
-#endif
-}
-
-rtgui_widget_t* rtgui_container_get_first_child(rtgui_container_t* container)
-{
- rtgui_widget_t* child = RT_NULL;
-
- if (container->children.next != RT_NULL)
- {
- child = rtgui_list_entry(container->children.next, rtgui_widget_t, sibling);
- }
-
- return child;
-}
+/* + * File : container.c + * This file is part of RT-Thread RTOS + * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rt-thread.org/license/LICENSE + * + * Change Logs: + * Date Author Notes + * 2009-10-16 Bernard first version + */ +#include <rtgui/widgets/toplevel.h> +#include <rtgui/widgets/container.h> + +static void _rtgui_container_constructor(rtgui_container_t *container) +{ + /* set event handler and init field */ + rtgui_widget_set_event_handler(RTGUI_WIDGET(container), rtgui_container_event_handler); + rtgui_list_init(&(container->children)); + + /* set focused widget to itself */ + container->focused = RTGUI_WIDGET(container); + /* set container as focusable widget */ + RTGUI_WIDGET(container)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE; +} + +static void _rtgui_container_destructor(rtgui_container_t *container) +{ + /* destroy children of container */ + rtgui_container_destroy_children(container); +} + +static void _rtgui_container_update_toplevel(rtgui_container_t* container) +{ + struct rtgui_list_node* node; + + rtgui_list_foreach(node, &(container->children)) + { + rtgui_widget_t* child = rtgui_list_entry(node, rtgui_widget_t, sibling); + /* set child toplevel */ + child->toplevel = rtgui_widget_get_toplevel(RTGUI_WIDGET(container)); + + if (RTGUI_IS_CONTAINER(child)) + { + _rtgui_container_update_toplevel(RTGUI_CONTAINER(child)); + } + } +} + +rtgui_type_t *rtgui_container_type_get(void) +{ + static rtgui_type_t *container_type = RT_NULL; + + if (!container_type) + { + container_type = rtgui_type_create("container", RTGUI_WIDGET_TYPE, + sizeof(rtgui_container_t), + RTGUI_CONSTRUCTOR(_rtgui_container_constructor), + RTGUI_DESTRUCTOR(_rtgui_container_destructor)); + } + + return container_type; +} + +rt_bool_t rtgui_container_dispatch_event(rtgui_container_t *container, rtgui_event_t* event) +{ + /* handle in child widget */ + struct rtgui_list_node* node; + + rtgui_list_foreach(node, &(container->children)) + { + struct rtgui_widget* w; + w = rtgui_list_entry(node, struct rtgui_widget, sibling); + + if (w->event_handler(w, event) == RT_TRUE) return RT_TRUE; + } + + return RT_FALSE; +} + +rt_bool_t rtgui_container_dispatch_mouse_event(rtgui_container_t *container, struct rtgui_event_mouse* event) +{ + /* handle in child widget */ + struct rtgui_list_node* node; + + rtgui_list_foreach(node, &(container->children)) + { + struct rtgui_widget* w; + w = rtgui_list_entry(node, struct rtgui_widget, sibling); + if (rtgui_rect_contains_point(&(w->extent), event->x, event->y) == RT_EOK) + { + if (w->event_handler(w, (rtgui_event_t*)event) == RT_TRUE) return RT_TRUE; + } + } + + return RT_FALSE; +} + +rt_bool_t rtgui_container_event_handler(rtgui_widget_t* widget, rtgui_event_t* event) +{ + rtgui_container_t *container = RTGUI_CONTAINER(widget); + + switch (event->type) + { + case RTGUI_EVENT_PAINT: + if (widget->on_draw != RT_NULL) + { + return widget->on_draw(widget, event); + } + + rtgui_container_dispatch_event(container, event); + break; + + case RTGUI_EVENT_KBD: + if (widget->on_key != RT_NULL) + { + return widget->on_key(widget, event); + } + else + { + /* let parent to handle keyboard event */ + if (widget->parent != RT_NULL && widget->parent != widget->toplevel) + { + return widget->parent->event_handler(widget->parent, event); + } + } + break; + + case RTGUI_EVENT_MOUSE_BUTTON: + /* handle in child widget */ + if (rtgui_container_dispatch_mouse_event(container, + (struct rtgui_event_mouse*)event) == RT_FALSE) + { + /* handle event in current widget */ + if (widget->on_mouseclick != RT_NULL) + { + return widget->on_mouseclick(widget, event); + } + } + else return RT_TRUE; + break; + + case RTGUI_EVENT_MOUSE_MOTION: + if (rtgui_container_dispatch_mouse_event(container, + (struct rtgui_event_mouse*)event) == RT_FALSE) + { +#if 0 + /* handle event in current widget */ + if (widget->on_mousemotion != RT_NULL) + { + return widget->on_mousemotion(widget, event); + } +#endif + } + else return RT_TRUE; + break; + + case RTGUI_EVENT_COMMAND: + if (rtgui_container_dispatch_event(container, event) == RT_FALSE) + { + if (widget->on_command != RT_NULL) + { + return widget->on_command(widget, event); + } + } + else return RT_TRUE; + break; + + case RTGUI_EVENT_RESIZE: + if (rtgui_container_dispatch_event(container, event) == RT_FALSE) + { + if (widget->on_size != RT_NULL) + return widget->on_size(widget, event); + } + else return RT_TRUE; + break; + + default: + /* call parent widget event handler */ + return rtgui_widget_event_handler(widget, event); + } + + return RT_FALSE; +} + +/* + * This function will add a child to a container widget + * Note: this function will not change the widget layout + * the layout is the responsibility of layout widget, such as box. + */ +void rtgui_container_add_child(rtgui_container_t *container, rtgui_widget_t* child) +{ + RT_ASSERT(container != RT_NULL); + RT_ASSERT(child != RT_NULL); + + /* set parent and toplevel widget */ + child->parent = RTGUI_WIDGET(container); + /* put widget to parent's children list */ + rtgui_list_append(&(container->children), &(child->sibling)); + + /* update children toplevel */ + if (RTGUI_WIDGET(container)->toplevel != RT_NULL && + RTGUI_IS_TOPLEVEL(RTGUI_WIDGET(container)->toplevel)) + { + child->toplevel = rtgui_widget_get_toplevel(RTGUI_WIDGET(container)); + + /* update all child toplevel */ + if (RTGUI_IS_CONTAINER(child)) + { + _rtgui_container_update_toplevel(RTGUI_CONTAINER(child)); + } + } +} + +/* remove a child to widget */ +void rtgui_container_remove_child(rtgui_container_t *container, rtgui_widget_t* child) +{ + RT_ASSERT(container != RT_NULL); + RT_ASSERT(child != RT_NULL); + + if (child == container->focused) + { + /* set focused to itself */ + container->focused = RTGUI_WIDGET(container); + + rtgui_widget_focus(RTGUI_WIDGET(container)); + } + + /* remove widget from parent's children list */ + rtgui_list_remove(&(container->children), &(child->sibling)); + + /* set parent and toplevel widget */ + child->parent = RT_NULL; + child->toplevel = RT_NULL; +} + +/* destroy all children of container */ +void rtgui_container_destroy_children(rtgui_container_t *container) +{ + struct rtgui_list_node* node; + + if (container == RT_NULL) return; + + node = container->children.next; + while (node != RT_NULL) + { + rtgui_widget_t* child = rtgui_list_entry(node, rtgui_widget_t, sibling); + + if (RTGUI_IS_CONTAINER(child)) + { + /* break parent firstly */ + child->parent = RT_NULL; + + /* destroy children of child */ + rtgui_container_destroy_children(RTGUI_CONTAINER(child)); + } + + /* remove widget from parent's children list */ + rtgui_list_remove(&(container->children), &(child->sibling)); + + /* set parent and toplevel widget */ + child->parent = RT_NULL; + + /* destroy object and remove from parent */ + rtgui_object_destroy(RTGUI_OBJECT(child)); + + node = container->children.next; + } + + container->children.next = RT_NULL; + container->focused = RTGUI_WIDGET(container); + if (RTGUI_WIDGET(container)->parent != RT_NULL) + rtgui_widget_focus(RTGUI_WIDGET(container)); + + /* update widget clip */ + rtgui_toplevel_update_clip(RTGUI_TOPLEVEL(RTGUI_WIDGET(container)->toplevel)); +} + +rtgui_widget_t* rtgui_container_get_first_child(rtgui_container_t* container) +{ + rtgui_widget_t* child = RT_NULL; + + if (container->children.next != RT_NULL) + { + child = rtgui_list_entry(container->children.next, rtgui_widget_t, sibling); + } + + return child; +} diff --git a/rtgui/widgets/iconbox.c b/rtgui/widgets/iconbox.c index b44cab1e76..c141e81988 100644 --- a/rtgui/widgets/iconbox.c +++ b/rtgui/widgets/iconbox.c @@ -1,167 +1,167 @@ -/*
- * File : iconbox.c
- * This file is part of RT-Thread RTOS
- * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team
- *
- * The license and distribution terms for this file may be
- * found in the file LICENSE in this distribution or at
- * http://www.rt-thread.org/license/LICENSE
- *
- * Change Logs:
- * Date Author Notes
- * 2009-10-16 Bernard first version
- */
-#include <rtgui/dc.h>
-#include <rtgui/widgets/iconbox.h>
-#include <rtgui/rtgui_theme.h>
-
-static void _rtgui_iconbox_constructor(rtgui_iconbox_t *iconbox)
-{
- /* init widget and set event handler */
- RTGUI_WIDGET(iconbox)->flag |= RTGUI_WIDGET_FLAG_TRANSPARENT;
- rtgui_widget_set_event_handler(RTGUI_WIDGET(iconbox), rtgui_iconbox_event_handler);
-
- /* set proper of control */
- iconbox->image = RT_NULL;
- iconbox->selected = RT_FALSE;
- iconbox->text = RT_NULL;
- iconbox->text_position = RTGUI_ICONBOX_TEXT_BELOW;
-}
-
-static void _rtgui_iconbox_destructor(rtgui_iconbox_t *iconbox)
-{
- if (iconbox->image != RT_NULL)
- {
- rtgui_image_destroy(iconbox->image);
- iconbox->image = RT_NULL;
- }
-
- rt_free(iconbox->text);
- iconbox->text = RT_NULL;
-}
-
-rtgui_type_t *rtgui_iconbox_type_get(void)
-{
- static rtgui_type_t *iconbox_type = RT_NULL;
-
- if (!iconbox_type)
- {
- iconbox_type = rtgui_type_create("iconbox", RTGUI_WIDGET_TYPE,
- sizeof(rtgui_iconbox_t), RTGUI_CONSTRUCTOR(_rtgui_iconbox_constructor),
- RTGUI_DESTRUCTOR(_rtgui_iconbox_destructor));
- }
-
- return iconbox_type;
-}
-
-rt_bool_t rtgui_iconbox_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
-{
- struct rtgui_iconbox* iconbox = (struct rtgui_iconbox*)widget;
-
- switch (event->type)
- {
- case RTGUI_EVENT_PAINT:
- if (widget->on_draw != RT_NULL) widget->on_draw(widget, event);
- else
- {
- rtgui_theme_draw_iconbox(iconbox);
- }
-
- break;
- }
-
- return RT_FALSE;
-}
-
-struct rtgui_iconbox* rtgui_iconbox_create(struct rtgui_image* image,
- const unsigned char* text,
- int position)
-{
- struct rtgui_iconbox* iconbox;
-
- iconbox = (struct rtgui_iconbox*)rtgui_widget_create(RTGUI_ICONBOX_TYPE);
- if (iconbox != RT_NULL)
- {
- rtgui_rect_t rect = {0, 0, 0, 0}, text_rect;
-
- rect.x2 = image->w;
- rect.y2 = image->h;
-
- /* get text rect */
- rtgui_font_get_metrics(rtgui_font_default(), text, &text_rect);
- if (position == RTGUI_ICONBOX_TEXT_BELOW)
- {
- rect.y2 += RTGUI_WIDGET_DEFAULT_MARGIN;
- if (text_rect.x2 > rect.x2)
- {
- rect.x2 = text_rect.x2;
- }
- rect.y2 += text_rect.y2;
- }
- else if (position == RTGUI_ICONBOX_TEXT_RIGHT)
- {
- rect.x2 += RTGUI_WIDGET_DEFAULT_MARGIN;
- if (text_rect.y2 > rect.y2)
- {
- rect.y2 = text_rect.y2;
- }
- rect.x2 += text_rect.x2;
- }
-
- /* set widget rect */
- rtgui_widget_set_rect(RTGUI_WIDGET(iconbox), &rect);
-
- /* set image and text position */
- iconbox->image = image;
- iconbox->text = (unsigned char*)rt_strdup((const char*)text);
- iconbox->text_position = position;
- }
-
- return iconbox;
-}
-
-void rtgui_iconbox_destroy(struct rtgui_iconbox* iconbox)
-{
- rtgui_widget_destroy(RTGUI_WIDGET(iconbox));
-}
-
-void rtgui_iconbox_set_text_position(struct rtgui_iconbox* iconbox, int position)
-{
- rtgui_rect_t rect = {0, 0, 0, 0}, text_rect;
-
- RT_ASSERT(iconbox != RT_NULL);
-
- iconbox->text_position = position;
-
- /* set mini width and height */
- rect.x2 = iconbox->image->w;
- rect.y2 = iconbox->image->h;
-
- /* get text rect */
- if (iconbox->text != RT_NULL)
- {
- rtgui_font_get_metrics(rtgui_font_default(),
- iconbox->text, &text_rect);
- if (position == RTGUI_ICONBOX_TEXT_BELOW)
- {
- rect.y2 += RTGUI_WIDGET_DEFAULT_MARGIN;
- if (text_rect.x2 > rect.x2)
- {
- rect.x2 = text_rect.x2;
- }
- rect.y2 += text_rect.y2;
- }
- else if (position == RTGUI_ICONBOX_TEXT_RIGHT)
- {
- rect.x2 += RTGUI_WIDGET_DEFAULT_MARGIN;
- if (text_rect.y2 > rect.y2)
- {
- rect.y2 = text_rect.y2;
- }
- rect.x2 += text_rect.x2;
- }
- }
-
- rtgui_widget_set_miniwidth(RTGUI_WIDGET(iconbox), rect.x2);
- rtgui_widget_set_miniheight(RTGUI_WIDGET(iconbox), rect.y2);
-}
+/* + * File : iconbox.c + * This file is part of RT-Thread RTOS + * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rt-thread.org/license/LICENSE + * + * Change Logs: + * Date Author Notes + * 2009-10-16 Bernard first version + */ +#include <rtgui/dc.h> +#include <rtgui/widgets/iconbox.h> +#include <rtgui/rtgui_theme.h> + +static void _rtgui_iconbox_constructor(rtgui_iconbox_t *iconbox) +{ + /* init widget and set event handler */ + RTGUI_WIDGET(iconbox)->flag |= RTGUI_WIDGET_FLAG_TRANSPARENT; + rtgui_widget_set_event_handler(RTGUI_WIDGET(iconbox), rtgui_iconbox_event_handler); + + /* set proper of control */ + iconbox->image = RT_NULL; + iconbox->selected = RT_FALSE; + iconbox->text = RT_NULL; + iconbox->text_position = RTGUI_ICONBOX_TEXT_BELOW; +} + +static void _rtgui_iconbox_destructor(rtgui_iconbox_t *iconbox) +{ + if (iconbox->image != RT_NULL) + { + rtgui_image_destroy(iconbox->image); + iconbox->image = RT_NULL; + } + + rt_free(iconbox->text); + iconbox->text = RT_NULL; +} + +rtgui_type_t *rtgui_iconbox_type_get(void) +{ + static rtgui_type_t *iconbox_type = RT_NULL; + + if (!iconbox_type) + { + iconbox_type = rtgui_type_create("iconbox", RTGUI_WIDGET_TYPE, + sizeof(rtgui_iconbox_t), RTGUI_CONSTRUCTOR(_rtgui_iconbox_constructor), + RTGUI_DESTRUCTOR(_rtgui_iconbox_destructor)); + } + + return iconbox_type; +} + +rt_bool_t rtgui_iconbox_event_handler(struct rtgui_widget* widget, struct rtgui_event* event) +{ + struct rtgui_iconbox* iconbox = (struct rtgui_iconbox*)widget; + + switch (event->type) + { + case RTGUI_EVENT_PAINT: + if (widget->on_draw != RT_NULL) widget->on_draw(widget, event); + else + { + rtgui_theme_draw_iconbox(iconbox); + } + + break; + } + + return RT_FALSE; +} + +struct rtgui_iconbox* rtgui_iconbox_create(struct rtgui_image* image, + const unsigned char* text, + int position) +{ + struct rtgui_iconbox* iconbox; + + iconbox = (struct rtgui_iconbox*)rtgui_widget_create(RTGUI_ICONBOX_TYPE); + if (iconbox != RT_NULL) + { + rtgui_rect_t rect = {0, 0, 0, 0}, text_rect; + + rect.x2 = image->w; + rect.y2 = image->h; + + /* get text rect */ + rtgui_font_get_metrics(rtgui_font_default(), text, &text_rect); + if (position == RTGUI_ICONBOX_TEXT_BELOW) + { + rect.y2 += RTGUI_WIDGET_DEFAULT_MARGIN; + if (text_rect.x2 > rect.x2) + { + rect.x2 = text_rect.x2; + } + rect.y2 += text_rect.y2; + } + else if (position == RTGUI_ICONBOX_TEXT_RIGHT) + { + rect.x2 += RTGUI_WIDGET_DEFAULT_MARGIN; + if (text_rect.y2 > rect.y2) + { + rect.y2 = text_rect.y2; + } + rect.x2 += text_rect.x2; + } + + /* set widget rect */ + rtgui_widget_set_rect(RTGUI_WIDGET(iconbox), &rect); + + /* set image and text position */ + iconbox->image = image; + iconbox->text = (unsigned char*)rt_strdup((const char*)text); + iconbox->text_position = position; + } + + return iconbox; +} + +void rtgui_iconbox_destroy(struct rtgui_iconbox* iconbox) +{ + rtgui_widget_destroy(RTGUI_WIDGET(iconbox)); +} + +void rtgui_iconbox_set_text_position(struct rtgui_iconbox* iconbox, int position) +{ + rtgui_rect_t rect = {0, 0, 0, 0}, text_rect; + + RT_ASSERT(iconbox != RT_NULL); + + iconbox->text_position = position; + + /* set mini width and height */ + rect.x2 = iconbox->image->w; + rect.y2 = iconbox->image->h; + + /* get text rect */ + if (iconbox->text != RT_NULL) + { + rtgui_font_get_metrics(rtgui_font_default(), + iconbox->text, &text_rect); + if (position == RTGUI_ICONBOX_TEXT_BELOW) + { + rect.y2 += RTGUI_WIDGET_DEFAULT_MARGIN; + if (text_rect.x2 > rect.x2) + { + rect.x2 = text_rect.x2; + } + rect.y2 += text_rect.y2; + } + else if (position == RTGUI_ICONBOX_TEXT_RIGHT) + { + rect.x2 += RTGUI_WIDGET_DEFAULT_MARGIN; + if (text_rect.y2 > rect.y2) + { + rect.y2 = text_rect.y2; + } + rect.x2 += text_rect.x2; + } + } + + rtgui_widget_set_miniwidth(RTGUI_WIDGET(iconbox), rect.x2); + rtgui_widget_set_miniheight(RTGUI_WIDGET(iconbox), rect.y2); +} diff --git a/rtgui/widgets/label.c b/rtgui/widgets/label.c index 992142472d..68e41d0747 100644 --- a/rtgui/widgets/label.c +++ b/rtgui/widgets/label.c @@ -1,113 +1,113 @@ -/*
- * File : label.c
- * This file is part of RT-Thread RTOS
- * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team
- *
- * The license and distribution terms for this file may be
- * found in the file LICENSE in this distribution or at
- * http://www.rt-thread.org/license/LICENSE
- *
- * Change Logs:
- * Date Author Notes
- * 2009-10-16 Bernard first version
- */
-#include <rtgui/dc.h>
-#include <rtgui/widgets/label.h>
-#include <rtgui/rtgui_system.h>
-#include <rtgui/rtgui_theme.h>
-
-static void _rtgui_label_constructor(rtgui_label_t *label)
-{
- /* init widget and set event handler */
- rtgui_widget_set_event_handler(RTGUI_WIDGET(label), rtgui_label_event_handler);
-
- /* set field */
- label->text = RT_NULL;
-}
-
-static void _rtgui_label_destructor(rtgui_label_t *label)
-{
- /* release text memory */
- rt_free(label->text);
- label->text = RT_NULL;
-}
-
-rtgui_type_t *rtgui_label_type_get(void)
-{
- static rtgui_type_t *label_type = RT_NULL;
-
- if (!label_type)
- {
- label_type = rtgui_type_create("label", RTGUI_WIDGET_TYPE,
- sizeof(rtgui_label_t),
- RTGUI_CONSTRUCTOR(_rtgui_label_constructor),
- RTGUI_DESTRUCTOR(_rtgui_label_destructor));
- }
-
- return label_type;
-}
-
-rt_bool_t rtgui_label_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
-{
- struct rtgui_label* label;
-
- RT_ASSERT(widget != RT_NULL);
-
- label = (struct rtgui_label*) widget;
- switch (event->type)
- {
- case RTGUI_EVENT_PAINT:
- rtgui_theme_draw_label(label);
- break;
- }
-
- return RT_FALSE;
-}
-
-rtgui_label_t* rtgui_label_create(const unsigned char* text)
-{
- struct rtgui_label* label;
-
- label = (struct rtgui_label*) rtgui_widget_create(RTGUI_LABEL_TYPE);
- if (label != RT_NULL)
- {
- rtgui_rect_t rect;
-
- /* set default rect */
- rtgui_font_get_metrics(rtgui_font_default(), text, &rect);
- rect.x2 += (RTGUI_BORDER_DEFAULT_WIDTH << 1);
- rect.y2 += (RTGUI_BORDER_DEFAULT_WIDTH << 1);
- rtgui_widget_set_rect(RTGUI_WIDGET(label), &rect);
-
- /* set text */
- label->text = (unsigned char*)rt_strdup((const char*)text);
- }
-
- return label;
-}
-
-void rtgui_label_destroy(rtgui_label_t* label)
-{
- rtgui_widget_destroy(RTGUI_WIDGET(label));
-}
-
-unsigned char* rtgui_label_get_text(rtgui_label_t* label)
-{
- RT_ASSERT(label != RT_NULL);
-
- return label->text;
-}
-
-void rtgui_label_set_text(rtgui_label_t* label, const unsigned char* text)
-{
- RT_ASSERT(label != RT_NULL);
-
- if (label->text != RT_NULL)
- {
- /* release old text memory */
- rt_free(label->text);
- }
-
- if (text != RT_NULL) label->text = (unsigned char*)rt_strdup((const char*)text);
- else label->text = RT_NULL;
-}
+/* + * File : label.c + * This file is part of RT-Thread RTOS + * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rt-thread.org/license/LICENSE + * + * Change Logs: + * Date Author Notes + * 2009-10-16 Bernard first version + */ +#include <rtgui/dc.h> +#include <rtgui/widgets/label.h> +#include <rtgui/rtgui_system.h> +#include <rtgui/rtgui_theme.h> + +static void _rtgui_label_constructor(rtgui_label_t *label) +{ + /* init widget and set event handler */ + rtgui_widget_set_event_handler(RTGUI_WIDGET(label), rtgui_label_event_handler); + + /* set field */ + label->text = RT_NULL; +} + +static void _rtgui_label_destructor(rtgui_label_t *label) +{ + /* release text memory */ + rt_free(label->text); + label->text = RT_NULL; +} + +rtgui_type_t *rtgui_label_type_get(void) +{ + static rtgui_type_t *label_type = RT_NULL; + + if (!label_type) + { + label_type = rtgui_type_create("label", RTGUI_WIDGET_TYPE, + sizeof(rtgui_label_t), + RTGUI_CONSTRUCTOR(_rtgui_label_constructor), + RTGUI_DESTRUCTOR(_rtgui_label_destructor)); + } + + return label_type; +} + +rt_bool_t rtgui_label_event_handler(struct rtgui_widget* widget, struct rtgui_event* event) +{ + struct rtgui_label* label; + + RT_ASSERT(widget != RT_NULL); + + label = (struct rtgui_label*) widget; + switch (event->type) + { + case RTGUI_EVENT_PAINT: + rtgui_theme_draw_label(label); + break; + } + + return RT_FALSE; +} + +rtgui_label_t* rtgui_label_create(const unsigned char* text) +{ + struct rtgui_label* label; + + label = (struct rtgui_label*) rtgui_widget_create(RTGUI_LABEL_TYPE); + if (label != RT_NULL) + { + rtgui_rect_t rect; + + /* set default rect */ + rtgui_font_get_metrics(rtgui_font_default(), text, &rect); + rect.x2 += (RTGUI_BORDER_DEFAULT_WIDTH << 1); + rect.y2 += (RTGUI_BORDER_DEFAULT_WIDTH << 1); + rtgui_widget_set_rect(RTGUI_WIDGET(label), &rect); + + /* set text */ + label->text = (unsigned char*)rt_strdup((const char*)text); + } + + return label; +} + +void rtgui_label_destroy(rtgui_label_t* label) +{ + rtgui_widget_destroy(RTGUI_WIDGET(label)); +} + +unsigned char* rtgui_label_get_text(rtgui_label_t* label) +{ + RT_ASSERT(label != RT_NULL); + + return label->text; +} + +void rtgui_label_set_text(rtgui_label_t* label, const unsigned char* text) +{ + RT_ASSERT(label != RT_NULL); + + if (label->text != RT_NULL) + { + /* release old text memory */ + rt_free(label->text); + } + + if (text != RT_NULL) label->text = (unsigned char*)rt_strdup((const char*)text); + else label->text = RT_NULL; +} diff --git a/rtgui/widgets/textbox.c b/rtgui/widgets/textbox.c index 560ffb914d..2f1e79227b 100644 --- a/rtgui/widgets/textbox.c +++ b/rtgui/widgets/textbox.c @@ -1,348 +1,350 @@ -/*
- * File : textbox.c
- * This file is part of RT-Thread RTOS
- * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team
- *
- * The license and distribution terms for this file may be
- * found in the file LICENSE in this distribution or at
- * http://www.rt-thread.org/license/LICENSE
- *
- * Change Logs:
- * Date Author Notes
- * 2009-10-16 Bernard first version
- */
-#include <rtgui/dc.h>
-#include <rtgui/widgets/textbox.h>
-#include <rtgui/rtgui_theme.h>
-
-#include <ctype.h>
-
-#define RTGUI_TEXTBOX_LINE_MAX 64
-#define RTGUI_TEXTBOX_MARGIN 3
-
-static void rtgui_textbox_onkey(struct rtgui_textbox* box, struct rtgui_event_kbd* event);
-static rt_bool_t rtgui_textbox_onfocus(struct rtgui_widget* widget, struct rtgui_event* event);
-static rt_bool_t rtgui_textbox_onunfocus(struct rtgui_widget* widget, struct rtgui_event* event);
-
-static void _rtgui_textbox_constructor(rtgui_textbox_t *box)
-{
- rtgui_rect_t rect = {0, 0, RTGUI_TEXTBOX_DEFAULT_WIDTH, RTGUI_TEXTBOX_DEFAULT_HEIGHT};
- rtgui_widget_set_rect(RTGUI_WIDGET(box), &rect);
-
- RTGUI_WIDGET(box)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE;
- rtgui_widget_set_event_handler(RTGUI_WIDGET(box), rtgui_textbox_event_handler);
- rtgui_widget_set_onfocus(RTGUI_WIDGET(box), rtgui_textbox_onfocus);
- rtgui_widget_set_onunfocus(RTGUI_WIDGET(box), rtgui_textbox_onunfocus);
-
- /* set default text align */
- RTGUI_WIDGET_TEXTALIGN(RTGUI_WIDGET(box)) = RTGUI_ALIGN_CENTER_VERTICAL;
-
- /* set proper of control */
- box->caret_x = box->caret_y = 0;
- box->caret = rtgui_caret_create(RTGUI_WIDGET(box));
-
- box->line = box->line_begin = box->position = 0;
- box->type = RTGUI_TEXTBOX_SINGLE;
-
- /* allocate default line buffer */
- box->text = RT_NULL;
-
- rtgui_font_get_metrics(RTGUI_WIDGET(box)->gc.font, "h", &rect);
- box->font_width = rtgui_rect_width(rect);
-}
-
-static void _rtgui_textbox_deconstructor(rtgui_textbox_t *box)
-{
- if (box->text != RT_NULL)
- {
- rt_free(box->text);
- box->text = RT_NULL;
- }
-
- if (box->caret != RT_NULL)
- {
- rtgui_caret_destroy(box->caret);
- box->caret = RT_NULL;
- }
-}
-
-rtgui_type_t *rtgui_textbox_type_get(void)
-{
- static rtgui_type_t *textbox_type = RT_NULL;
-
- if (!textbox_type)
- {
- textbox_type = rtgui_type_create("textbox", RTGUI_WIDGET_TYPE,
- sizeof(rtgui_textbox_t),
- RTGUI_CONSTRUCTOR(_rtgui_textbox_constructor),
- RTGUI_DESTRUCTOR(_rtgui_textbox_deconstructor));
- }
-
- return textbox_type;
-}
-
-static void rtgui_textbox_onmouse(struct rtgui_textbox* box, struct rtgui_event_mouse* event)
-{
- rt_size_t length;
-
- RT_ASSERT(box != RT_NULL);
- RT_ASSERT(event != RT_NULL);
-
- length = rt_strlen((const char*)box->text);
-
- if (event->button & RTGUI_MOUSE_BUTTON_LEFT &&
- event->button & RTGUI_MOUSE_BUTTON_DOWN)
- {
- rt_int32_t x;
-
- /* set caret position */
- x = event->x - RTGUI_WIDGET(box)->extent.x1;
- if (x < 0)
- {
- box->position = 0;
- }
- else if (x > length * box->font_width)
- {
- box->position = length;
- }
- else
- {
- box->position = x / box->font_width;
- }
-
- rtgui_caret_set_point(box->caret, RTGUI_TEXTBOX_MARGIN + box->position * box->font_width, 2);
- rtgui_caret_set_box(box->caret, 2, rtgui_rect_height(RTGUI_WIDGET(box)->extent) - 4);
-
- /* set widget focus */
- rtgui_widget_focus(RTGUI_WIDGET(box));
- }
-}
-
-static void rtgui_textbox_onkey(struct rtgui_textbox* box, struct rtgui_event_kbd* event)
-{
- rt_size_t length;
-
- RT_ASSERT(box != RT_NULL);
- RT_ASSERT(event != RT_NULL);
-
- if (event->type != RTGUI_KEYDOWN)
- return ;
-
- length = rt_strlen((const char*)box->text);
- if (event->key == RTGUIK_DELETE)
- {
- if (box->position == length - 1)
- {
- box->text[box->position] = '\0';
- }
- else
- {
- unsigned char *c;
-
- /* remove character */
- for (c = &box->text[box->position]; c[1] != '\0'; c++)
- *c = c[1];
- *c = '\0';
- }
- }
- else if (event->key == RTGUIK_BACKSPACE)
- {
- if (box->position == length - 1)
- {
- box->text[box->position] = '\0';
- box->position --;
- }
- else if (box->position != 0)
- {
- /* remove current character */
- if (box->position != 0)
- {
- unsigned char *c;
-
- /* remove character */
- for (c = &box->text[box->position - 1]; c[1] != '\0'; c++)
- *c = c[1];
- *c = '\0';
- }
- box->position --;
- }
- }
- else if (event->key == RTGUIK_LEFT)
- {
- if (box->position > 0) box->position --;
- }
- else if (event->key == RTGUIK_RIGHT)
- {
- if (box->position < length) box->position ++;
- }
- else if (event->key == RTGUIK_HOME)
- {
- box->position = 0;
- }
- else if (event->key == RTGUIK_END)
- {
- box->position = length;
- }
- else if (event->key == RTGUIK_RETURN)
- {
- if (box->on_enter != RT_NULL)
- {
- box->on_enter(RTGUI_WIDGET(box), RT_NULL);
- }
- }
- else
- {
- if (isprint(event->key) || isdigit(event->key))
- {
- /* no buffer on this line */
- if (length + 1 > box->line_length) return;
-
- if (box->position < length - 1)
- {
- unsigned char* c;
-
- for (c = &box->text[length]; c != &box->text[box->position]; c--)
- *c = *(c-1);
- box->text[length + 1] = '\0';
- }
-
- box->text[box->position] = event->key;
- box->position ++;
- }
- }
-
- /* re-draw text box */
- rtgui_theme_draw_textbox(box);
- rtgui_caret_set_point(box->caret,
- RTGUI_TEXTBOX_MARGIN + box->position * box->font_width , 2);
- rtgui_caret_set_box(box->caret, 2, rtgui_rect_height(RTGUI_WIDGET(box)->extent) - 4);
-}
-
-static rt_bool_t rtgui_textbox_onfocus(struct rtgui_widget* widget, struct rtgui_event* event)
-{
- struct rtgui_textbox* box = (struct rtgui_textbox*)widget;
-
- /* show caret */
- rtgui_caret_show(box->caret, box->caret_x, box->caret_y);
-
- return RT_TRUE;
-}
-
-static rt_bool_t rtgui_textbox_onunfocus(struct rtgui_widget* widget, struct rtgui_event* event)
-{
- struct rtgui_textbox* box = (struct rtgui_textbox*)widget;
-
- /* hide caret */
- rtgui_caret_hide(box->caret);
-
- return RT_TRUE;
-}
-
-rt_bool_t rtgui_textbox_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
-{
- struct rtgui_textbox* box = (struct rtgui_textbox*)widget;
-
- switch (event->type)
- {
- case RTGUI_EVENT_PAINT:
- if (widget->on_draw != RT_NULL) widget->on_draw(widget, event);
- else rtgui_theme_draw_textbox(box);
- break;
-
- case RTGUI_EVENT_MOUSE_BUTTON:
- if (widget->on_mouseclick != RT_NULL) widget->on_mouseclick(widget, event);
- else rtgui_textbox_onmouse(box, (struct rtgui_event_mouse*)event);
- return RT_TRUE;
-
- case RTGUI_EVENT_KBD:
- if (widget->on_key != RT_NULL) widget->on_key(widget, event);
- else rtgui_textbox_onkey(box, (struct rtgui_event_kbd*)event);
- return RT_TRUE;
- }
-
- return RT_FALSE;
-}
-
-struct rtgui_textbox* rtgui_textbox_create(const char* text)
-{
- struct rtgui_textbox* box;
-
- box = (struct rtgui_textbox*) rtgui_widget_create (RTGUI_TEXTBOX_TYPE);
- if (box != RT_NULL)
- {
- rtgui_rect_t rect = {0, 0, RTGUI_TEXTBOX_DEFAULT_WIDTH, RTGUI_TEXTBOX_DEFAULT_HEIGHT};
-
- /* allocate default line buffer */
- rtgui_textbox_set_value(box, text);
-
- rtgui_font_get_metrics(RTGUI_WIDGET(box)->gc.font, "h", &rect);
- box->font_width = rtgui_rect_width(rect);
- }
-
- return box;
-}
-
-void rtgui_textbox_destroy(struct rtgui_textbox* box)
-{
- rtgui_widget_destroy(RTGUI_WIDGET(box));
-}
-
-void rtgui_textbox_set_value(struct rtgui_textbox* box, const char* text)
-{
- if (box->text != RT_NULL)
- {
- if (box->line_length > rt_strlen(text) + 1)
- {
- rt_memcpy(box->text, text, rt_strlen(text) + 1);
- return;
- }
- else
- {
- /* free the old text */
- rtgui_free(box->text);
- box->text = RT_NULL;
- }
- }
-
- box->line_length = RTGUI_TEXTBOX_LINE_MAX > rt_strlen(text) + 1 ?
- RTGUI_TEXTBOX_LINE_MAX : rt_strlen(text) + 1;
-
- /* allocate line buffer */
- box->text = rtgui_malloc(box->line_length);
- rt_memset(box->text, 0, box->line_length);
-
- /* copy text */
- rt_memcpy(box->text, text, rt_strlen(text) + 1);
-
- /* set current position */
- box->position = 0;
-}
-
-const char* rtgui_textbox_get_value(struct rtgui_textbox* box)
-{
- return (const char*)box->text;
-}
-
-void rtgui_widget_set_line_length(struct rtgui_textbox* box, rt_size_t length)
-{
- rt_uint8_t* new_line;
-
- RT_ASSERT(box != RT_NULL);
-
- /* invalid length */
- if (length <= 0) return;
-
- new_line = rtgui_malloc(length);
- if (length < box->line_length)
- {
- rt_memcpy(new_line, box->text, length - 1);
- new_line[length] = '\0';
- }
- else
- {
- rt_memcpy(new_line, (const char*)box->text, rt_strlen((const char*)box->text));
- }
-
- /* set line length */
- box->line_length = length;
-}
+/* + * File : textbox.c + * This file is part of RT-Thread RTOS + * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rt-thread.org/license/LICENSE + * + * Change Logs: + * Date Author Notes + * 2009-10-16 Bernard first version + */ +#include <rtgui/dc.h> +#include <rtgui/widgets/textbox.h> +#include <rtgui/rtgui_theme.h> + +#ifndef _WIN32 +#include <ctype.h> +#endif + +#define RTGUI_TEXTBOX_LINE_MAX 64 +#define RTGUI_TEXTBOX_MARGIN 3 + +static void rtgui_textbox_onkey(struct rtgui_textbox* box, struct rtgui_event_kbd* event); +static rt_bool_t rtgui_textbox_onfocus(struct rtgui_widget* widget, struct rtgui_event* event); +static rt_bool_t rtgui_textbox_onunfocus(struct rtgui_widget* widget, struct rtgui_event* event); + +static void _rtgui_textbox_constructor(rtgui_textbox_t *box) +{ + rtgui_rect_t rect = {0, 0, RTGUI_TEXTBOX_DEFAULT_WIDTH, RTGUI_TEXTBOX_DEFAULT_HEIGHT}; + rtgui_widget_set_rect(RTGUI_WIDGET(box), &rect); + + RTGUI_WIDGET(box)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE; + rtgui_widget_set_event_handler(RTGUI_WIDGET(box), rtgui_textbox_event_handler); + rtgui_widget_set_onfocus(RTGUI_WIDGET(box), rtgui_textbox_onfocus); + rtgui_widget_set_onunfocus(RTGUI_WIDGET(box), rtgui_textbox_onunfocus); + + /* set default text align */ + RTGUI_WIDGET_TEXTALIGN(RTGUI_WIDGET(box)) = RTGUI_ALIGN_CENTER_VERTICAL; + + /* set proper of control */ + box->caret_x = box->caret_y = 0; + box->caret = rtgui_caret_create(RTGUI_WIDGET(box)); + + box->line = box->line_begin = box->position = 0; + box->type = RTGUI_TEXTBOX_SINGLE; + + /* allocate default line buffer */ + box->text = RT_NULL; + + rtgui_font_get_metrics(RTGUI_WIDGET(box)->gc.font, "h", &rect); + box->font_width = rtgui_rect_width(rect); +} + +static void _rtgui_textbox_deconstructor(rtgui_textbox_t *box) +{ + if (box->text != RT_NULL) + { + rt_free(box->text); + box->text = RT_NULL; + } + + if (box->caret != RT_NULL) + { + rtgui_caret_destroy(box->caret); + box->caret = RT_NULL; + } +} + +rtgui_type_t *rtgui_textbox_type_get(void) +{ + static rtgui_type_t *textbox_type = RT_NULL; + + if (!textbox_type) + { + textbox_type = rtgui_type_create("textbox", RTGUI_WIDGET_TYPE, + sizeof(rtgui_textbox_t), + RTGUI_CONSTRUCTOR(_rtgui_textbox_constructor), + RTGUI_DESTRUCTOR(_rtgui_textbox_deconstructor)); + } + + return textbox_type; +} + +static void rtgui_textbox_onmouse(struct rtgui_textbox* box, struct rtgui_event_mouse* event) +{ + rt_size_t length; + + RT_ASSERT(box != RT_NULL); + RT_ASSERT(event != RT_NULL); + + length = rt_strlen((const char*)box->text); + + if (event->button & RTGUI_MOUSE_BUTTON_LEFT && + event->button & RTGUI_MOUSE_BUTTON_DOWN) + { + rt_int32_t x; + + /* set caret position */ + x = event->x - RTGUI_WIDGET(box)->extent.x1; + if (x < 0) + { + box->position = 0; + } + else if (x > length * box->font_width) + { + box->position = length; + } + else + { + box->position = x / box->font_width; + } + + rtgui_caret_set_point(box->caret, RTGUI_TEXTBOX_MARGIN + box->position * box->font_width, 2); + rtgui_caret_set_box(box->caret, 2, rtgui_rect_height(RTGUI_WIDGET(box)->extent) - 4); + + /* set widget focus */ + rtgui_widget_focus(RTGUI_WIDGET(box)); + } +} + +static void rtgui_textbox_onkey(struct rtgui_textbox* box, struct rtgui_event_kbd* event) +{ + rt_size_t length; + + RT_ASSERT(box != RT_NULL); + RT_ASSERT(event != RT_NULL); + + if (event->type != RTGUI_KEYDOWN) + return ; + + length = rt_strlen((const char*)box->text); + if (event->key == RTGUIK_DELETE) + { + if (box->position == length - 1) + { + box->text[box->position] = '\0'; + } + else + { + unsigned char *c; + + /* remove character */ + for (c = &box->text[box->position]; c[1] != '\0'; c++) + *c = c[1]; + *c = '\0'; + } + } + else if (event->key == RTGUIK_BACKSPACE) + { + if (box->position == length - 1) + { + box->text[box->position] = '\0'; + box->position --; + } + else if (box->position != 0) + { + /* remove current character */ + if (box->position != 0) + { + unsigned char *c; + + /* remove character */ + for (c = &box->text[box->position - 1]; c[1] != '\0'; c++) + *c = c[1]; + *c = '\0'; + } + box->position --; + } + } + else if (event->key == RTGUIK_LEFT) + { + if (box->position > 0) box->position --; + } + else if (event->key == RTGUIK_RIGHT) + { + if (box->position < length) box->position ++; + } + else if (event->key == RTGUIK_HOME) + { + box->position = 0; + } + else if (event->key == RTGUIK_END) + { + box->position = length; + } + else if (event->key == RTGUIK_RETURN) + { + if (box->on_enter != RT_NULL) + { + box->on_enter(RTGUI_WIDGET(box), RT_NULL); + } + } + else + { + if (isprint(event->key) || isdigit(event->key)) + { + /* no buffer on this line */ + if (length + 1 > box->line_length) return; + + if (box->position < length - 1) + { + unsigned char* c; + + for (c = &box->text[length]; c != &box->text[box->position]; c--) + *c = *(c-1); + box->text[length + 1] = '\0'; + } + + box->text[box->position] = event->key; + box->position ++; + } + } + + /* re-draw text box */ + rtgui_theme_draw_textbox(box); + rtgui_caret_set_point(box->caret, + RTGUI_TEXTBOX_MARGIN + box->position * box->font_width , 2); + rtgui_caret_set_box(box->caret, 2, rtgui_rect_height(RTGUI_WIDGET(box)->extent) - 4); +} + +static rt_bool_t rtgui_textbox_onfocus(struct rtgui_widget* widget, struct rtgui_event* event) +{ + struct rtgui_textbox* box = (struct rtgui_textbox*)widget; + + /* show caret */ + rtgui_caret_show(box->caret, box->caret_x, box->caret_y); + + return RT_TRUE; +} + +static rt_bool_t rtgui_textbox_onunfocus(struct rtgui_widget* widget, struct rtgui_event* event) +{ + struct rtgui_textbox* box = (struct rtgui_textbox*)widget; + + /* hide caret */ + rtgui_caret_hide(box->caret); + + return RT_TRUE; +} + +rt_bool_t rtgui_textbox_event_handler(struct rtgui_widget* widget, struct rtgui_event* event) +{ + struct rtgui_textbox* box = (struct rtgui_textbox*)widget; + + switch (event->type) + { + case RTGUI_EVENT_PAINT: + if (widget->on_draw != RT_NULL) widget->on_draw(widget, event); + else rtgui_theme_draw_textbox(box); + break; + + case RTGUI_EVENT_MOUSE_BUTTON: + if (widget->on_mouseclick != RT_NULL) widget->on_mouseclick(widget, event); + else rtgui_textbox_onmouse(box, (struct rtgui_event_mouse*)event); + return RT_TRUE; + + case RTGUI_EVENT_KBD: + if (widget->on_key != RT_NULL) widget->on_key(widget, event); + else rtgui_textbox_onkey(box, (struct rtgui_event_kbd*)event); + return RT_TRUE; + } + + return RT_FALSE; +} + +struct rtgui_textbox* rtgui_textbox_create(const char* text) +{ + struct rtgui_textbox* box; + + box = (struct rtgui_textbox*) rtgui_widget_create (RTGUI_TEXTBOX_TYPE); + if (box != RT_NULL) + { + rtgui_rect_t rect = {0, 0, RTGUI_TEXTBOX_DEFAULT_WIDTH, RTGUI_TEXTBOX_DEFAULT_HEIGHT}; + + /* allocate default line buffer */ + rtgui_textbox_set_value(box, text); + + rtgui_font_get_metrics(RTGUI_WIDGET(box)->gc.font, "h", &rect); + box->font_width = rtgui_rect_width(rect); + } + + return box; +} + +void rtgui_textbox_destroy(struct rtgui_textbox* box) +{ + rtgui_widget_destroy(RTGUI_WIDGET(box)); +} + +void rtgui_textbox_set_value(struct rtgui_textbox* box, const char* text) +{ + if (box->text != RT_NULL) + { + if (box->line_length > rt_strlen(text) + 1) + { + rt_memcpy(box->text, text, rt_strlen(text) + 1); + return; + } + else + { + /* free the old text */ + rtgui_free(box->text); + box->text = RT_NULL; + } + } + + box->line_length = RTGUI_TEXTBOX_LINE_MAX > rt_strlen(text) + 1 ? + RTGUI_TEXTBOX_LINE_MAX : rt_strlen(text) + 1; + + /* allocate line buffer */ + box->text = rtgui_malloc(box->line_length); + rt_memset(box->text, 0, box->line_length); + + /* copy text */ + rt_memcpy(box->text, text, rt_strlen(text) + 1); + + /* set current position */ + box->position = 0; +} + +const char* rtgui_textbox_get_value(struct rtgui_textbox* box) +{ + return (const char*)box->text; +} + +void rtgui_widget_set_line_length(struct rtgui_textbox* box, rt_size_t length) +{ + rt_uint8_t* new_line; + + RT_ASSERT(box != RT_NULL); + + /* invalid length */ + if (length <= 0) return; + + new_line = rtgui_malloc(length); + if (length < box->line_length) + { + rt_memcpy(new_line, box->text, length - 1); + new_line[length] = '\0'; + } + else + { + rt_memcpy(new_line, (const char*)box->text, rt_strlen((const char*)box->text)); + } + + /* set line length */ + box->line_length = length; +} diff --git a/rtgui/widgets/title.c b/rtgui/widgets/title.c index 41b8924cb7..2a38c703a5 100644 --- a/rtgui/widgets/title.c +++ b/rtgui/widgets/title.c @@ -1,81 +1,81 @@ -/*
- * File : title.c
- * This file is part of RT-Thread RTOS
- * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team
- *
- * The license and distribution terms for this file may be
- * found in the file LICENSE in this distribution or at
- * http://www.rt-thread.org/license/LICENSE
- *
- * Change Logs:
- * Date Author Notes
- * 2009-10-16 Bernard first version
- */
-#include <rtgui/widgets/title.h>
-#include <rtgui/rtgui_system.h>
-
-/* there is no event handler in wintitle but handle the event on topwin of server */
-static void _rtgui_wintitle_constructor(rtgui_wintitle_t* wintitle)
-{
- wintitle->title = RT_NULL;
- RTGUI_WIDGET(wintitle)->flag = RTGUI_WIDGET_FLAG_DEFAULT;
-}
-
-static void _rtgui_wintitle_deconstructor(rtgui_wintitle_t* wintitle)
-{
- rt_free(wintitle->title);
- wintitle->title = RT_NULL;
-}
-
-rtgui_type_t* rtgui_wintitle_type_get()
-{
- static rtgui_type_t *wintitle_type = RT_NULL;
-
- if (!wintitle_type)
- {
- wintitle_type = rtgui_type_create("wintitle", RTGUI_TOPLEVEL_TYPE,
- sizeof(rtgui_wintitle_t),
- RTGUI_CONSTRUCTOR(_rtgui_wintitle_constructor),
- RTGUI_DESTRUCTOR(_rtgui_wintitle_deconstructor));
- }
-
- return wintitle_type;
-}
-
-rtgui_wintitle_t* rtgui_wintitle_create(const rt_uint8_t* title)
-{
- rtgui_wintitle_t* wintitle;
-
- wintitle = (rtgui_wintitle_t*)rtgui_widget_create(RTGUI_WINTITLE_TYPE);
- if (wintitle != RT_NULL)
- {
- rtgui_wintitle_set_title(wintitle, title);
- }
-
- return wintitle;
-}
-
-void rtgui_wintitle_destroy(rtgui_wintitle_t* wintitle)
-{
- rtgui_widget_destroy(RTGUI_WIDGET(wintitle));
-}
-
-void rtgui_wintitle_set_title(rtgui_wintitle_t* wintitle, const rt_uint8_t* title)
-{
- RT_ASSERT(wintitle != RT_NULL);
-
- if (wintitle->title != RT_NULL)
- {
- rtgui_free(wintitle->title);
- }
-
- if (title != RT_NULL) wintitle->title = (unsigned char*)rt_strdup((const char*)title);
- else wintitle->title = RT_NULL;
-}
-
-rt_uint8_t *rtgui_wintitle_get_title(rtgui_wintitle_t* wintitle)
-{
- RT_ASSERT(wintitle != RT_NULL);
-
- return wintitle->title;
-}
+/* + * File : title.c + * This file is part of RT-Thread RTOS + * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rt-thread.org/license/LICENSE + * + * Change Logs: + * Date Author Notes + * 2009-10-16 Bernard first version + */ +#include <rtgui/widgets/title.h> +#include <rtgui/rtgui_system.h> + +/* there is no event handler in wintitle but handle the event on topwin of server */ +static void _rtgui_wintitle_constructor(rtgui_wintitle_t* wintitle) +{ + wintitle->title = RT_NULL; + RTGUI_WIDGET(wintitle)->flag = RTGUI_WIDGET_FLAG_DEFAULT; +} + +static void _rtgui_wintitle_deconstructor(rtgui_wintitle_t* wintitle) +{ + rt_free(wintitle->title); + wintitle->title = RT_NULL; +} + +rtgui_type_t* rtgui_wintitle_type_get() +{ + static rtgui_type_t *wintitle_type = RT_NULL; + + if (!wintitle_type) + { + wintitle_type = rtgui_type_create("wintitle", RTGUI_TOPLEVEL_TYPE, + sizeof(rtgui_wintitle_t), + RTGUI_CONSTRUCTOR(_rtgui_wintitle_constructor), + RTGUI_DESTRUCTOR(_rtgui_wintitle_deconstructor)); + } + + return wintitle_type; +} + +rtgui_wintitle_t* rtgui_wintitle_create(const rt_uint8_t* title) +{ + rtgui_wintitle_t* wintitle; + + wintitle = (rtgui_wintitle_t*)rtgui_widget_create(RTGUI_WINTITLE_TYPE); + if (wintitle != RT_NULL) + { + rtgui_wintitle_set_title(wintitle, title); + } + + return wintitle; +} + +void rtgui_wintitle_destroy(rtgui_wintitle_t* wintitle) +{ + rtgui_widget_destroy(RTGUI_WIDGET(wintitle)); +} + +void rtgui_wintitle_set_title(rtgui_wintitle_t* wintitle, const rt_uint8_t* title) +{ + RT_ASSERT(wintitle != RT_NULL); + + if (wintitle->title != RT_NULL) + { + rtgui_free(wintitle->title); + } + + if (title != RT_NULL) wintitle->title = (unsigned char*)rt_strdup((const char*)title); + else wintitle->title = RT_NULL; +} + +rt_uint8_t *rtgui_wintitle_get_title(rtgui_wintitle_t* wintitle) +{ + RT_ASSERT(wintitle != RT_NULL); + + return wintitle->title; +} diff --git a/rtgui/widgets/toplevel.c b/rtgui/widgets/toplevel.c index b2089449fd..1c7da78517 100644 --- a/rtgui/widgets/toplevel.c +++ b/rtgui/widgets/toplevel.c @@ -1,194 +1,178 @@ -/*
- * File : toplevel.c
- * This file is part of RT-Thread RTOS
- * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team
- *
- * The license and distribution terms for this file may be
- * found in the file LICENSE in this distribution or at
- * http://www.rt-thread.org/license/LICENSE
- *
- * Change Logs:
- * Date Author Notes
- * 2009-10-16 Bernard first version
- */
-#include <rtgui/rtgui_system.h>
-#include <rtgui/widgets/toplevel.h>
-
-static void _rtgui_toplevel_constructor(rtgui_toplevel_t *toplevel)
-{
- /* set event handler */
- rtgui_widget_set_event_handler(RTGUI_WIDGET(toplevel), rtgui_toplevel_event_handler);
-
- /* set toplevel to self */
- RTGUI_WIDGET(toplevel)->toplevel = RTGUI_WIDGET(toplevel);
-
- /* init toplevel property */
- toplevel->drawing = 0;
- toplevel->external_clip_rect = RT_NULL;
- toplevel->external_clip_size = 0;
- toplevel->focus = RT_NULL;
-
- /* hide toplevel default */
- RTGUI_WIDGET_HIDE(RTGUI_WIDGET(toplevel));
-
- /* set server as RT_NULL (no connected) */
- toplevel->server = RT_NULL;
-}
-
-static void _rtgui_toplevel_destructor(rtgui_toplevel_t* toplevel)
-{
- /* release external clip info */
- rtgui_free(toplevel->external_clip_rect);
-
- toplevel->drawing = 0;
- rtgui_free(toplevel->external_clip_rect);
- toplevel->external_clip_rect = RT_NULL;
- toplevel->external_clip_size = 0;
- toplevel->focus = RT_NULL;
-}
-
-rtgui_type_t *rtgui_toplevel_type_get(void)
-{
- static rtgui_type_t *toplevel_type = RT_NULL;
-
- if (!toplevel_type)
- {
- toplevel_type = rtgui_type_create("toplevel", RTGUI_CONTAINER_TYPE,
- sizeof(rtgui_toplevel_t),
- RTGUI_CONSTRUCTOR(_rtgui_toplevel_constructor),
- RTGUI_DESTRUCTOR(_rtgui_toplevel_destructor));
- }
-
- return toplevel_type;
-}
-
-rt_bool_t rtgui_toplevel_event_handler(rtgui_widget_t* widget, rtgui_event_t* event)
-{
- rtgui_toplevel_t* toplevel = (rtgui_toplevel_t*)widget;
-
- switch (event->type)
- {
- case RTGUI_EVENT_KBD:
- if (toplevel->focus != RT_NULL)
- {
- toplevel->focus->event_handler(toplevel->focus, event);
- }
- break;
-
- case RTGUI_EVENT_CLIP_INFO:
- /* set toplevel external clip info */
- rtgui_toplevel_handle_clip(toplevel, (struct rtgui_event_clip_info*)event);
-
- /* update toplevel clip */
- rtgui_toplevel_update_clip(toplevel);
- break;
-
- case RTGUI_EVENT_TIMER:
- {
- struct rtgui_timer* timer;
- struct rtgui_event_timer* etimer = (struct rtgui_event_timer*) event;
-
- timer = etimer->timer;
- if (timer->timeout != RT_NULL)
- {
- /* call timeout function */
- timer->timeout(timer, timer->user_data);
- }
- }
- break;
-
- case RTGUI_EVENT_COMMAND:
- if (rtgui_container_dispatch_event(RTGUI_CONTAINER(widget), event) != RT_TRUE)
- {
- if (widget->on_command != RT_NULL)
- {
- widget->on_command(widget, event);
- }
- }
- break;
-
- default :
- return rtgui_container_event_handler(widget, event);
- }
-
- return RT_FALSE;
-}
-
-void rtgui_toplevel_handle_clip(struct rtgui_toplevel* top,
- struct rtgui_event_clip_info* info)
-{
- RT_ASSERT(top != RT_NULL);
- RT_ASSERT(info != RT_NULL);
-
- /* release old rect array */
- if (top->external_clip_size != 0)
- {
- rtgui_free(top->external_clip_rect);
- top->external_clip_rect = RT_NULL;
- top->external_clip_size = 0;
- }
-
- /* no rect info */
- if (info->num_rect == 0) return;
-
- top->external_clip_rect = (rtgui_rect_t*) rtgui_malloc(sizeof(rtgui_rect_t) *
- info->num_rect);
- top->external_clip_size = info->num_rect;
-
- /* copy rect array */
- rt_memcpy(top->external_clip_rect, info->rects, sizeof(rtgui_rect_t) * info->num_rect);
-}
-
-#include <rtgui/driver.h> /* to get screen rect */
-
-void rtgui_toplevel_update_clip(rtgui_toplevel_t* top)
-{
- rt_uint32_t idx;
- rtgui_container_t* container;
- struct rtgui_list_node* node;
- rtgui_rect_t screen_rect;
-
- if (top == RT_NULL) return;
-
- /* reset toplevel widget clip to extent */
- rtgui_region_reset(&(RTGUI_WIDGET(top)->clip), &(RTGUI_WIDGET(top)->extent));
- RTGUI_WIDGET(top)->clip_sync ++;
-
- /* subtract the screen rect */
- screen_rect.x1 = screen_rect.y1 = 0;
- screen_rect.x2 = rtgui_graphic_driver_get_default()->width;
- screen_rect.y2 = rtgui_graphic_driver_get_default()->height;
- rtgui_region_intersect_rect(&(RTGUI_WIDGET(top)->clip), &(RTGUI_WIDGET(top)->clip),
- &screen_rect);
-
- /* subtract the external rect */
- for (idx = 0; idx < top->external_clip_size; idx ++)
- {
- rtgui_region_subtract_rect(&(RTGUI_WIDGET(top)->clip), &(RTGUI_WIDGET(top)->clip),
- &(top->external_clip_rect[idx]));
- }
-
- /* update the clip info of each child */
- container = RTGUI_CONTAINER(top);
- rtgui_list_foreach(node, &(container->children))
- {
- rtgui_widget_t* child = rtgui_list_entry(node, rtgui_widget_t, sibling);
-
- rtgui_widget_update_clip(child);
- }
-}
-
-void rtgui_toplevel_set_focus(struct rtgui_toplevel* top, rtgui_widget_t* focus)
-{
- RT_ASSERT(top != RT_NULL);
-
- top->focus = focus;
-}
-
-rtgui_widget_t* rtgui_toplevel_get_focus(struct rtgui_toplevel* top)
-{
- RT_ASSERT(top != RT_NULL);
-
- return top->focus;
-}
-
+/* + * File : toplevel.c + * This file is part of RT-Thread RTOS + * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rt-thread.org/license/LICENSE + * + * Change Logs: + * Date Author Notes + * 2009-10-16 Bernard first version + */ +#include <rtgui/rtgui_system.h> +#include <rtgui/widgets/toplevel.h> + +static void _rtgui_toplevel_constructor(rtgui_toplevel_t *toplevel) +{ + /* set event handler */ + rtgui_widget_set_event_handler(RTGUI_WIDGET(toplevel), rtgui_toplevel_event_handler); + + /* set toplevel to self */ + RTGUI_WIDGET(toplevel)->toplevel = RTGUI_WIDGET(toplevel); + + /* init toplevel property */ + toplevel->drawing = 0; + toplevel->external_clip_rect = RT_NULL; + toplevel->external_clip_size = 0; + + /* hide toplevel default */ + RTGUI_WIDGET_HIDE(RTGUI_WIDGET(toplevel)); + + /* set server as RT_NULL (no connected) */ + toplevel->server = RT_NULL; +} + +static void _rtgui_toplevel_destructor(rtgui_toplevel_t* toplevel) +{ + /* release external clip info */ + rtgui_free(toplevel->external_clip_rect); + + toplevel->drawing = 0; + rtgui_free(toplevel->external_clip_rect); + toplevel->external_clip_rect = RT_NULL; + toplevel->external_clip_size = 0; +} + +rtgui_type_t *rtgui_toplevel_type_get(void) +{ + static rtgui_type_t *toplevel_type = RT_NULL; + + if (!toplevel_type) + { + toplevel_type = rtgui_type_create("toplevel", RTGUI_CONTAINER_TYPE, + sizeof(rtgui_toplevel_t), + RTGUI_CONSTRUCTOR(_rtgui_toplevel_constructor), + RTGUI_DESTRUCTOR(_rtgui_toplevel_destructor)); + } + + return toplevel_type; +} + +rt_bool_t rtgui_toplevel_event_handler(rtgui_widget_t* widget, rtgui_event_t* event) +{ + rtgui_toplevel_t* toplevel = (rtgui_toplevel_t*)widget; + + switch (event->type) + { + case RTGUI_EVENT_KBD: + if (RTGUI_CONTAINER(toplevel)->focused != RT_NULL) + { + RTGUI_CONTAINER(toplevel)->focused->event_handler(RTGUI_CONTAINER(toplevel)->focused, event); + } + break; + + case RTGUI_EVENT_CLIP_INFO: + /* set toplevel external clip info */ + rtgui_toplevel_handle_clip(toplevel, (struct rtgui_event_clip_info*)event); + + /* update toplevel clip */ + rtgui_toplevel_update_clip(toplevel); + break; + + case RTGUI_EVENT_TIMER: + { + struct rtgui_timer* timer; + struct rtgui_event_timer* etimer = (struct rtgui_event_timer*) event; + + timer = etimer->timer; + if (timer->timeout != RT_NULL) + { + /* call timeout function */ + timer->timeout(timer, timer->user_data); + } + } + break; + + case RTGUI_EVENT_COMMAND: + if (rtgui_container_dispatch_event(RTGUI_CONTAINER(widget), event) != RT_TRUE) + { + if (widget->on_command != RT_NULL) + { + widget->on_command(widget, event); + } + } + else return RT_TRUE; + break; + + default : + return rtgui_container_event_handler(widget, event); + } + + return RT_FALSE; +} + +void rtgui_toplevel_handle_clip(struct rtgui_toplevel* top, + struct rtgui_event_clip_info* info) +{ + RT_ASSERT(top != RT_NULL); + RT_ASSERT(info != RT_NULL); + + /* release old rect array */ + if (top->external_clip_size != 0) + { + rtgui_free(top->external_clip_rect); + top->external_clip_rect = RT_NULL; + top->external_clip_size = 0; + } + + /* no rect info */ + if (info->num_rect == 0) return; + + top->external_clip_rect = (rtgui_rect_t*) rtgui_malloc(sizeof(rtgui_rect_t) * + info->num_rect); + top->external_clip_size = info->num_rect; + + /* copy rect array */ + rt_memcpy(top->external_clip_rect, (void*)(info + 1), sizeof(rtgui_rect_t) * info->num_rect); +} + +#include <rtgui/driver.h> /* to get screen rect */ + +void rtgui_toplevel_update_clip(rtgui_toplevel_t* top) +{ + rt_uint32_t idx; + rtgui_container_t* container; + struct rtgui_list_node* node; + rtgui_rect_t screen_rect; + + if (top == RT_NULL) return; + + /* reset toplevel widget clip to extent */ + rtgui_region_reset(&(RTGUI_WIDGET(top)->clip), &(RTGUI_WIDGET(top)->extent)); + RTGUI_WIDGET(top)->clip_sync ++; + + /* subtract the screen rect */ + screen_rect.x1 = screen_rect.y1 = 0; + screen_rect.x2 = rtgui_graphic_driver_get_default()->width; + screen_rect.y2 = rtgui_graphic_driver_get_default()->height; + rtgui_region_intersect_rect(&(RTGUI_WIDGET(top)->clip), &(RTGUI_WIDGET(top)->clip), + &screen_rect); + + /* subtract the external rect */ + for (idx = 0; idx < top->external_clip_size; idx ++) + { + rtgui_region_subtract_rect(&(RTGUI_WIDGET(top)->clip), &(RTGUI_WIDGET(top)->clip), + &(top->external_clip_rect[idx])); + } + + /* update the clip info of each child */ + container = RTGUI_CONTAINER(top); + rtgui_list_foreach(node, &(container->children)) + { + rtgui_widget_t* child = rtgui_list_entry(node, rtgui_widget_t, sibling); + + rtgui_widget_update_clip(child); + } +} diff --git a/rtgui/widgets/view.c b/rtgui/widgets/view.c index 41b433c7ab..59e1b3043d 100644 --- a/rtgui/widgets/view.c +++ b/rtgui/widgets/view.c @@ -1,162 +1,191 @@ -/*
- * File : view.c
- * This file is part of RT-Thread RTOS
- * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team
- *
- * The license and distribution terms for this file may be
- * found in the file LICENSE in this distribution or at
- * http://www.rt-thread.org/license/LICENSE
- *
- * Change Logs:
- * Date Author Notes
- * 2009-10-16 Bernard first version
- */
-#include <rtgui/dc.h>
-#include <rtgui/rtgui_system.h>
-#include <rtgui/widgets/view.h>
-#include <rtgui/widgets/workbench.h>
-
-static void _rtgui_view_constructor(rtgui_view_t *view)
-{
- /* init view */
- rtgui_widget_set_event_handler(RTGUI_WIDGET(view),
- rtgui_view_event_handler);
-
- view->title = RT_NULL;
-}
-
-static void _rtgui_view_destructor(rtgui_view_t *view)
-{
- if (view->title != RT_NULL)
- {
- rt_free(view->title);
- view->title = RT_NULL;
- }
-}
-
-rtgui_type_t *rtgui_view_type_get(void)
-{
- static rtgui_type_t *view_type = RT_NULL;
-
- if (!view_type)
- {
- view_type = rtgui_type_create("view", RTGUI_CONTAINER_TYPE,
- sizeof(rtgui_view_t),
- RTGUI_CONSTRUCTOR(_rtgui_view_constructor),
- RTGUI_DESTRUCTOR(_rtgui_view_destructor));
- }
-
- return view_type;
-}
-
-rt_bool_t rtgui_view_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
-{
- struct rtgui_view* view = (struct rtgui_view*) widget;
- RT_ASSERT(widget != RT_NULL);
-
- switch (event->type)
- {
- case RTGUI_EVENT_PAINT:
- if (widget->on_draw != RT_NULL) widget->on_draw(widget, event);
- else
- {
- struct rtgui_dc* dc;
- struct rtgui_rect rect;
-
- dc = rtgui_dc_begin_drawing(widget);
- if (dc == RT_NULL) return RT_FALSE;
- rtgui_widget_get_rect(widget, &rect);
-
- /* fill view with background */
- rtgui_dc_fill_rect(dc, &rect);
-
- /* paint on each child */
- rtgui_container_dispatch_event(RTGUI_CONTAINER(view), event);
-
- rtgui_dc_end_drawing(dc);
- }
- break;
-
- default:
- return rtgui_container_event_handler(widget, event);
- }
-
- return RT_FALSE;
-}
-
-rtgui_view_t* rtgui_view_create(const char* title)
-{
- struct rtgui_view* view;
-
- /* allocate view */
- view = (struct rtgui_view*) rtgui_widget_create (RTGUI_VIEW_TYPE);
- if (view != RT_NULL)
- {
- if (title != RT_NULL)
- view->title = rt_strdup(title);
- }
-
- return view;
-}
-
-void rtgui_view_destroy(rtgui_view_t* view)
-{
- rtgui_widget_destroy(RTGUI_WIDGET(view));
-}
-
-void rtgui_view_set_box(rtgui_view_t* view, rtgui_box_t* box)
-{
- if (view == RT_NULL ||
- box == RT_NULL) return;
-
- rtgui_container_add_child(RTGUI_CONTAINER(view), RTGUI_WIDGET(box));
- rtgui_widget_set_rect(RTGUI_WIDGET(box), &(RTGUI_WIDGET(view)->extent));
-}
-
-void rtgui_view_show(rtgui_view_t* view)
-{
- if (view == RT_NULL) return;
-
- if (RTGUI_WIDGET(view)->parent == RT_NULL)
- {
- RTGUI_WIDGET_UNHIDE(RTGUI_WIDGET(view));
- return;
- }
-
- rtgui_workbench_show_view((rtgui_workbench_t*)(RTGUI_WIDGET(view)->parent), view);
- if (RTGUI_WIDGET_IS_FOCUSABLE(RTGUI_WIDGET(view)))
- rtgui_widget_focus(RTGUI_WIDGET(view));
-}
-
-void rtgui_view_hide(rtgui_view_t* view)
-{
- if (view == RT_NULL) return;
-
- if (RTGUI_WIDGET(view)->parent == RT_NULL)
- {
- RTGUI_WIDGET_HIDE(RTGUI_WIDGET(view));
- return;
- }
-
- rtgui_workbench_hide_view((rtgui_workbench_t*)(RTGUI_WIDGET(view)->parent), view);
-}
-
-char* rtgui_view_get_title(rtgui_view_t* view)
-{
- RT_ASSERT(view != RT_NULL);
-
- return view->title;
-}
-
-void rtgui_view_set_title(rtgui_view_t* view, const char *title)
-{
- RT_ASSERT(view != RT_NULL);
-
- if (view->title != RT_NULL)
- {
- rtgui_free(view->title);
-
- if (title != RT_NULL) view->title = rt_strdup(title);
- else view->title = RT_NULL;
- }
-}
+/* + * File : view.c + * This file is part of RT-Thread RTOS + * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rt-thread.org/license/LICENSE + * + * Change Logs: + * Date Author Notes + * 2009-10-16 Bernard first version + */ +#include <rtgui/dc.h> +#include <rtgui/rtgui_system.h> +#include <rtgui/widgets/view.h> +#include <rtgui/widgets/workbench.h> + +static void _rtgui_view_constructor(rtgui_view_t *view) +{ + /* init view */ + rtgui_widget_set_event_handler(RTGUI_WIDGET(view), + rtgui_view_event_handler); + + view->title = RT_NULL; +} + +static void _rtgui_view_destructor(rtgui_view_t *view) +{ + if (view->title != RT_NULL) + { + rt_free(view->title); + view->title = RT_NULL; + } +} + +rtgui_type_t *rtgui_view_type_get(void) +{ + static rtgui_type_t *view_type = RT_NULL; + + if (!view_type) + { + view_type = rtgui_type_create("view", RTGUI_CONTAINER_TYPE, + sizeof(rtgui_view_t), + RTGUI_CONSTRUCTOR(_rtgui_view_constructor), + RTGUI_DESTRUCTOR(_rtgui_view_destructor)); + } + + return view_type; +} + +rt_bool_t rtgui_view_event_handler(struct rtgui_widget* widget, struct rtgui_event* event) +{ + struct rtgui_view* view = (struct rtgui_view*) widget; + RT_ASSERT(widget != RT_NULL); + + switch (event->type) + { + case RTGUI_EVENT_PAINT: + if (widget->on_draw != RT_NULL) widget->on_draw(widget, event); + else + { + struct rtgui_dc* dc; + struct rtgui_rect rect; + + dc = rtgui_dc_begin_drawing(widget); + if (dc == RT_NULL) return RT_FALSE; + rtgui_widget_get_rect(widget, &rect); + + /* fill view with background */ + rtgui_dc_fill_rect(dc, &rect); + + /* paint on each child */ + rtgui_container_dispatch_event(RTGUI_CONTAINER(view), event); + + rtgui_dc_end_drawing(dc); + } + break; + + default: + return rtgui_container_event_handler(widget, event); + } + + return RT_FALSE; +} + +rtgui_view_t* rtgui_view_create(const char* title) +{ + struct rtgui_view* view; + + /* allocate view */ + view = (struct rtgui_view*) rtgui_widget_create (RTGUI_VIEW_TYPE); + if (view != RT_NULL) + { + if (title != RT_NULL) + view->title = rt_strdup(title); + } + + return view; +} + +void rtgui_view_destroy(rtgui_view_t* view) +{ + rtgui_widget_destroy(RTGUI_WIDGET(view)); +} + +void rtgui_view_set_box(rtgui_view_t* view, rtgui_box_t* box) +{ + if (view == RT_NULL || + box == RT_NULL) return; + + rtgui_container_add_child(RTGUI_CONTAINER(view), RTGUI_WIDGET(box)); + rtgui_widget_set_rect(RTGUI_WIDGET(box), &(RTGUI_WIDGET(view)->extent)); +} + +rtgui_modal_code_t rtgui_view_show(rtgui_view_t* view, rt_bool_t is_modal) +{ + rtgui_workbench_t* workbench; + + /* parameter check */ + if (view == RT_NULL) return RTGUI_MODAL_CANCEL; + + if (RTGUI_WIDGET(view)->parent == RT_NULL) + { + RTGUI_WIDGET_UNHIDE(RTGUI_WIDGET(view)); + return RTGUI_MODAL_CANCEL; + } + + workbench = RTGUI_WORKBENCH(RTGUI_WIDGET(view)->parent); + rtgui_workbench_show_view(workbench, view); + if (RTGUI_WIDGET_IS_FOCUSABLE(RTGUI_WIDGET(view))) + rtgui_widget_focus(RTGUI_WIDGET(view)); + + if (is_modal == RT_TRUE) + { + /* set modal mode */ + workbench->flag |= RTGUI_WORKBENCH_FLAG_MODAL_MODE; + + /* perform workbench event loop */ + rtgui_workbench_event_loop(workbench); + return workbench->modal_code; + } + + /* no modal mode, always return modal_ok */ + return RTGUI_MODAL_OK; +} + +void rtgui_view_end_modal(rtgui_view_t* view, rtgui_modal_code_t modal_code) +{ + rtgui_workbench_t* workbench; + + /* parameter check */ + if ((view == RT_NULL) || (RTGUI_WIDGET(view)->parent == RT_NULL))return ; + + workbench = RTGUI_WORKBENCH(RTGUI_WIDGET(view)->parent); + workbench->modal_code = modal_code; + workbench->flag &= ~RTGUI_WORKBENCH_FLAG_MODAL_MODE; +} + +void rtgui_view_hide(rtgui_view_t* view) +{ + if (view == RT_NULL) return; + + if (RTGUI_WIDGET(view)->parent == RT_NULL) + { + RTGUI_WIDGET_HIDE(RTGUI_WIDGET(view)); + return; + } + + rtgui_workbench_hide_view((rtgui_workbench_t*)(RTGUI_WIDGET(view)->parent), view); +} + +char* rtgui_view_get_title(rtgui_view_t* view) +{ + RT_ASSERT(view != RT_NULL); + + return view->title; +} + +void rtgui_view_set_title(rtgui_view_t* view, const char *title) +{ + RT_ASSERT(view != RT_NULL); + + if (view->title != RT_NULL) + { + rtgui_free(view->title); + + if (title != RT_NULL) view->title = rt_strdup(title); + else view->title = RT_NULL; + } +} diff --git a/rtgui/widgets/widget.c b/rtgui/widgets/widget.c index 4363baf1c1..f3c876b290 100644 --- a/rtgui/widgets/widget.c +++ b/rtgui/widgets/widget.c @@ -103,10 +103,10 @@ void rtgui_widget_set_rect(rtgui_widget_t* widget, rtgui_rect_t* rect) { if (widget == RT_NULL || rect == RT_NULL) return; - widget->extent = *rect;
-
- /* reset mini width and height */
- widget->mini_width = rtgui_rect_width(widget->extent);
+ widget->extent = *rect; + + /* reset mini width and height */ + widget->mini_width = rtgui_rect_width(widget->extent); widget->mini_height = rtgui_rect_height(widget->extent); /* it's not empty, fini it */ @@ -226,30 +226,45 @@ void rtgui_widget_set_oncommand(rtgui_widget_t* widget, rtgui_event_handler_ptr } /** - * @brief Focuses the widget. The focused widget is the one which receives the keyboard events + * @brief Focuses the widget. The focused widget is the widget which can receive the keyboard events * @param widget a widget * @note The widget has to be attached to a toplevel widget, otherwise it will have no effect */ void rtgui_widget_focus(rtgui_widget_t *widget) { - rtgui_widget_t *focused; + rtgui_widget_t *focused; + rtgui_container_t *parent; + + RT_ASSERT(widget != RT_NULL); - if (!widget || !widget->toplevel || !RTGUI_WIDGET_IS_FOCUSABLE(widget) || !RTGUI_WIDGET_IS_ENABLE(widget)) + if (!widget->parent || !RTGUI_WIDGET_IS_FOCUSABLE(widget) || !RTGUI_WIDGET_IS_ENABLE(widget)) return; - focused = rtgui_toplevel_get_focus(RTGUI_TOPLEVEL(widget->toplevel)); + /* set widget as focused */ + widget->flag |= RTGUI_WIDGET_FLAG_FOCUS; - if ( focused != RT_NULL && widget == focused) - return; + /* get parent container */ + parent = RTGUI_CONTAINER(widget->parent); + + /* get old focused widget */ + focused = parent->focused; + if (focused == widget) return ; /* it's the same focused widget */ if (focused != RT_NULL) - { rtgui_widget_unfocus(focused); - } - rtgui_toplevel_set_focus(RTGUI_TOPLEVEL(widget->toplevel), widget); - widget->flag |= RTGUI_WIDGET_FLAG_FOCUS; + /* set widget as focused widget in parent link */ + parent->focused = widget; + while (RTGUI_WIDGET(parent)->parent != RT_NULL) + { + parent = RTGUI_CONTAINER(RTGUI_WIDGET(parent)->parent); + parent->focused = widget; + /* if the parent is hide, break it */ + if (RTGUI_WIDGET_IS_HIDE(RTGUI_WIDGET(parent))) break; + } + + /* invoke on focus in call back */ if (widget->on_focus_in) widget->on_focus_in(widget, RT_NULL); } @@ -260,16 +275,13 @@ void rtgui_widget_focus(rtgui_widget_t *widget) */ void rtgui_widget_unfocus(rtgui_widget_t *widget) { - if (!widget || !widget->toplevel || !RTGUI_WIDGET_IS_FOCUS(widget)) - return; - - if (rtgui_toplevel_get_focus(RTGUI_TOPLEVEL(widget->toplevel)) == widget) - { - rtgui_toplevel_set_focus(RTGUI_TOPLEVEL(widget->toplevel), RT_NULL); - } + RT_ASSERT(widget != RT_NULL); widget->flag &= ~RTGUI_WIDGET_FLAG_FOCUS; + if (!widget->toplevel || !RTGUI_WIDGET_IS_FOCUS(widget)) + return; + if (widget->on_focus_out) widget->on_focus_out(widget, RT_NULL); } @@ -477,7 +489,7 @@ rtgui_widget_t* rtgui_widget_get_next_sibling(rtgui_widget_t* widget) rtgui_widget_t* rtgui_widget_get_prev_sibling(rtgui_widget_t* widget) { struct rtgui_list_node* node; - rtgui_widget_t *sibling, *parent;
+ rtgui_widget_t *sibling, *parent; node = RT_NULL; sibling = RT_NULL; parent = widget->parent; @@ -494,4 +506,4 @@ rtgui_widget_t* rtgui_widget_get_prev_sibling(rtgui_widget_t* widget) sibling = rtgui_list_entry(node, rtgui_widget_t, sibling); return sibling; -}
+} diff --git a/rtgui/widgets/window.c b/rtgui/widgets/window.c index 01d4bd6057..ae0c708b3c 100644 --- a/rtgui/widgets/window.c +++ b/rtgui/widgets/window.c @@ -1,476 +1,552 @@ -/*
- * File : window.c
- * This file is part of RTGUI in RT-Thread RTOS
- * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team
- *
- * The license and distribution terms for this file may be
- * found in the file LICENSE in this distribution or at
- * http://www.rt-thread.org/license/LICENSE
- *
- * Change Logs:
- * Date Author Notes
- * 2009-10-04 Bernard first version
- */
-#include <rtgui/dc.h>
-#include <rtgui/color.h>
-#include <rtgui/image.h>
-#include <rtgui/rtgui_system.h>
+/* + * File : window.c + * This file is part of RTGUI in RT-Thread RTOS + * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rt-thread.org/license/LICENSE + * + * Change Logs: + * Date Author Notes + * 2009-10-04 Bernard first version + */ +#include <rtgui/dc.h> +#include <rtgui/color.h> +#include <rtgui/image.h> +#include <rtgui/rtgui_system.h> -#include <rtgui/widgets/window.h>
-#include <rtgui/widgets/button.h>
-
-static void _rtgui_win_constructor(rtgui_win_t *win)
-{
- /* init window attribute */
- win->on_activate = RT_NULL;
- win->on_deactivate = RT_NULL;
- win->on_close = RT_NULL;
- win->title = RT_NULL;
-
- /* set window hide */
- RTGUI_WIDGET_HIDE(RTGUI_WIDGET(win));
-
- /* set window style */
- win->style = RTGUI_WIN_STYLE_DEFAULT;
- rtgui_widget_set_event_handler(RTGUI_WIDGET(win), rtgui_win_event_handler);
-
- /* init user data */
- win->user_data = 0;
-}
-
-static void _rtgui_win_destructor(rtgui_win_t* win)
-{
- struct rtgui_event_win_destroy edestroy;
-
- if (RTGUI_TOPLEVEL(win)->server != RT_NULL)
- {
- /* destroy in server */
- RTGUI_EVENT_WIN_DESTROY_INIT(&edestroy);
- edestroy.wid = win;
- if (rtgui_thread_send_sync(RTGUI_TOPLEVEL(win)->server, RTGUI_EVENT(&edestroy),
- sizeof(struct rtgui_event_win_destroy)) != RT_EOK)
- {
- /* destroy in server failed */
- return;
- }
- }
-
- /* release field */
- rt_free(win->title);
-}
-
-static rt_bool_t _rtgui_win_create_in_server(rtgui_win_t* win)
-{
- if (RTGUI_TOPLEVEL(win)->server == RT_NULL)
- {
- rt_thread_t server;
- struct rtgui_event_win_create ecreate;
- RTGUI_EVENT_WIN_CREATE_INIT(&ecreate);
-
- /* get server thread id */
- server = rtgui_thread_get_server();
- if (server == RT_NULL)
- {
- rt_kprintf("RTGUI server is not running...\n");
- return RT_FALSE;
- }
-
- /* send win create event to server */
- ecreate.wid = win;
- ecreate.extent = RTGUI_WIDGET(win)->extent;
- ecreate.flag = win->style;
- ecreate.mask = 0;
- rt_strncpy((char*)ecreate.title, (char*)win->title, RTGUI_NAME_MAX);
-
- if (rtgui_thread_send_sync(server, RTGUI_EVENT(&ecreate),
- sizeof(struct rtgui_event_win_create)) != RT_EOK)
- {
- rt_kprintf("create win: %s failed\n", win->title);
- return RT_FALSE;
- }
-
- /* set server */
- RTGUI_TOPLEVEL(win)->server = server;
- }
-
- return RT_TRUE;
-}
-
-rtgui_type_t *rtgui_win_type_get(void)
-{
- static rtgui_type_t *win_type = RT_NULL;
-
- if (!win_type)
- {
- win_type = rtgui_type_create("win", RTGUI_TOPLEVEL_TYPE,
- sizeof(rtgui_win_t),
- RTGUI_CONSTRUCTOR(_rtgui_win_constructor),
- RTGUI_DESTRUCTOR(_rtgui_win_destructor));
- }
-
- return win_type;
-}
-
-rtgui_win_t* rtgui_win_create(rtgui_toplevel_t* parent_toplevel, const char* title, rtgui_rect_t *rect, rt_uint32_t style)
-{
- struct rtgui_win* win;
-
- /* allocate win memory */
- win = (struct rtgui_win*) rtgui_widget_create (RTGUI_WIN_TYPE);
- if (win != RT_NULL)
- {
- /* set parent toplevel */
- win->parent_toplevel = parent_toplevel;
-
- /* set title, rect and style */
- if (title != RT_NULL) win->title = rt_strdup(title);
- else win->title = RT_NULL;
-
- rtgui_widget_set_rect(RTGUI_WIDGET(win), rect);
- win->style = style;
-
- if (_rtgui_win_create_in_server(win) == RT_FALSE)
- {
- rtgui_widget_destroy(RTGUI_WIDGET(win));
- return RT_NULL;
- }
- }
-
- return win;
-}
-
-void rtgui_win_destroy(struct rtgui_win* win)
-{
- if (win->parent_toplevel == RT_NULL)
- {
- rtgui_thread_t *rtgui_tid;
-
- rtgui_tid = (rtgui_thread_t*) rt_thread_self()->user_data;
- rtgui_tid->is_quit = RT_TRUE;
- }
-
- rtgui_widget_destroy(RTGUI_WIDGET(win));
-}
-
-void rtgui_win_show(struct rtgui_win* win)
-{
- RT_ASSERT(win != RT_NULL);
-
- /* if it does not register into server, create it in server */
- if (RTGUI_TOPLEVEL(win)->server == RT_NULL)
- {
- if (_rtgui_win_create_in_server(win) == RT_FALSE)
- return;
- }
-
- if (RTGUI_WIDGET_IS_HIDE(RTGUI_WIDGET(win)))
- {
- /* send show message to server */
- struct rtgui_event_win_show eshow;
- RTGUI_EVENT_WIN_SHOW_INIT(&eshow);
- eshow.wid = win;
-
- if (rtgui_thread_send_sync(RTGUI_TOPLEVEL(win)->server, RTGUI_EVENT(&eshow),
- sizeof(struct rtgui_event_win_show)) != RT_EOK)
- {
- /* hide window failed */
- return;
- }
-
- /* set window unhidden */
- RTGUI_WIDGET_UNHIDE(RTGUI_WIDGET(win));
- }
- else rtgui_widget_update(RTGUI_WIDGET(win));
-}
-
-void rtgui_win_hiden(struct rtgui_win* win)
-{
- RT_ASSERT(win != RT_NULL);
-
- if (!RTGUI_WIDGET_IS_HIDE(RTGUI_WIDGET(win)) &&
- RTGUI_TOPLEVEL(win)->server != RT_NULL)
- {
- /* send hidden message to server */
- struct rtgui_event_win_hide ehide;
- RTGUI_EVENT_WIN_HIDE_INIT(&ehide);
- ehide.wid = win;
-
- if (rtgui_thread_send_sync(RTGUI_TOPLEVEL(win)->server, RTGUI_EVENT(&ehide),
- sizeof(struct rtgui_event_win_hide)) != RT_EOK)
- {
- rt_kprintf("hide win: %s failed\n", win->title);
- return;
- }
-
- /* set window hide and deactivated */
- RTGUI_WIDGET_HIDE(RTGUI_WIDGET(win));
- win->style &= ~RTGUI_WIN_STYLE_ACTIVATE;
- }
-}
-
-rt_bool_t rtgui_win_is_activated(struct rtgui_win* win)
-{
- RT_ASSERT(win != RT_NULL);
-
- return win->style & RTGUI_WIN_STYLE_ACTIVATE;
-}
-
-void rtgui_win_move(struct rtgui_win* win, int x, int y)
-{
- struct rtgui_event_win_move emove;
- RTGUI_EVENT_WIN_MOVE_INIT(&emove);
-
- if (win == RT_NULL) return;
-
- if (RTGUI_TOPLEVEL(win)->server != RT_NULL)
- {
- /* set win hide firstly */
- RTGUI_WIDGET_HIDE(RTGUI_WIDGET(win));
-
- emove.wid = win;
- emove.x = x;
- emove.y = y;
- if (rtgui_thread_send_sync(RTGUI_TOPLEVEL(win)->server, RTGUI_EVENT(&emove),
- sizeof(struct rtgui_event_win_move)) != RT_EOK)
- {
- return;
- }
- }
-
- /* move window to logic position */
- rtgui_widget_move_to_logic(RTGUI_WIDGET(win),
- x - RTGUI_WIDGET(win)->extent.x1,
- y - RTGUI_WIDGET(win)->extent.y1);
-
- /* set window visible */
- RTGUI_WIDGET_UNHIDE(RTGUI_WIDGET(win));
-
- /* update window */
- // rtgui_widget_update(RTGUI_WIDGET(win));
- return;
-}
-
-static rt_bool_t rtgui_win_ondraw(struct rtgui_win* win)
-{
- struct rtgui_dc* dc;
- struct rtgui_rect rect;
- struct rtgui_event_paint event;
-
- /* begin drawing */
- dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(win));
- if (dc == RT_NULL) return RT_FALSE;
-
- /* get window rect */
- rtgui_widget_get_rect(RTGUI_WIDGET(win), &rect);
- /* fill area */
- rtgui_dc_fill_rect(dc, &rect);
-
- /* paint each widget */
- RTGUI_EVENT_PAINT_INIT(&event);
- event.wid = RT_NULL;
- rtgui_container_dispatch_event(RTGUI_CONTAINER(win), (rtgui_event_t*)&event);
-
- rtgui_dc_end_drawing(dc);
-
- return RT_FALSE;
-}
-
-rt_bool_t rtgui_win_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
-{
- struct rtgui_win* win = (struct rtgui_win*)widget;
-
- RT_ASSERT((win != RT_NULL) && (event != RT_NULL));
-
- switch (event->type)
- {
- case RTGUI_EVENT_WIN_SHOW:
- rtgui_win_show(win);
- break;
-
- case RTGUI_EVENT_WIN_HIDE:
- rtgui_win_hiden(win);
- break;
-
- case RTGUI_EVENT_WIN_CLOSE:
- if (win->on_close != RT_NULL)
- {
- if (win->on_close(widget, event) == RT_FALSE) return RT_TRUE;
- }
-
- /* destroy window */
- rtgui_win_destroy(win);
-
- /* exit event loop */
- return RT_TRUE;
-
- case RTGUI_EVENT_WIN_MOVE:
- {
- struct rtgui_event_win_move* emove = (struct rtgui_event_win_move*)event;
-
- /* move window */
- rtgui_win_move(win, emove->x, emove->y);
- }
- break;
-
- case RTGUI_EVENT_WIN_ACTIVATE:
- if (RTGUI_WIDGET_IS_HIDE(RTGUI_WIDGET(win)))
- {
- rt_kprintf("activate window, but window is hide!\n");
- }
-
- win->style |= RTGUI_WIN_STYLE_ACTIVATE;
- if (widget->on_draw != RT_NULL) widget->on_draw(widget, event);
- else rtgui_win_ondraw(win);
-
- if (win->on_activate != RT_NULL)
- {
- win->on_activate(widget, event);
- }
- break;
-
- case RTGUI_EVENT_WIN_DEACTIVATE:
- win->style &= ~RTGUI_WIN_STYLE_ACTIVATE;
- if (widget->on_draw != RT_NULL) widget->on_draw(widget, event);
- else rtgui_win_ondraw(win);
-
- if (win->on_deactivate != RT_NULL)
- {
- win->on_deactivate(widget, event);
- }
- break;
-
- case RTGUI_EVENT_PAINT:
- if (widget->on_draw != RT_NULL) widget->on_draw(widget, event);
- else rtgui_win_ondraw(win);
- break;
-
- case RTGUI_EVENT_MOUSE_BUTTON:
- if (rtgui_container_dispatch_mouse_event(RTGUI_CONTAINER(win), (struct rtgui_event_mouse*)event) == RT_FALSE)
- {
- if (widget->on_mouseclick != RT_NULL)
- {
- return widget->on_mouseclick(widget, event);
- }
- }
- break;
-
- case RTGUI_EVENT_MOUSE_MOTION:
-#if 0
- if (rtgui_widget_dispatch_mouse_event(widget,
- (struct rtgui_event_mouse*)event) == RT_FALSE)
- {
- /* handle event in current widget */
- if (widget->on_mousemotion != RT_NULL)
- {
- return widget->on_mousemotion(widget, event);
- }
- }
- else return RT_TRUE;
-#endif
- break;
-
- default:
- /* call parent event handler */
- return rtgui_toplevel_event_handler(widget, event);
- }
-
- return RT_FALSE;
-}
-
-/* windows event loop */
-void rtgui_win_event_loop(rtgui_win_t* wnd)
-{
- rtgui_thread_t *rtgui_tid;
- /* the buffer uses to receive event */
- char event_buf[256];
-
- struct rtgui_event* event = (struct rtgui_event*)&event_buf[0];
- rtgui_tid = (rtgui_thread_t*) rt_thread_self()->user_data;
-
- while (rtgui_tid->is_quit == RT_FALSE)
- {
- if (rtgui_thread_recv(event, sizeof(event_buf)) == RT_EOK)
- {
- if (RTGUI_WIDGET(wnd)->event_handler(RTGUI_WIDGET(wnd), event) == RT_TRUE)
- rtgui_tid->is_quit = RT_TRUE;
- }
- }
-}
-
-void rtgui_win_set_rect(rtgui_win_t* win, rtgui_rect_t* rect)
-{
- struct rtgui_event_win_resize event;
-
- if (win == RT_NULL || rect == RT_NULL) return;
-
- RTGUI_WIDGET(win)->extent = *rect;
-
- if (RTGUI_TOPLEVEL(win)->server != RT_NULL)
- {
- /* set window resize event to server */
- RTGUI_EVENT_WIN_RESIZE_INIT(&event);
- event.wid = win;
- event.rect = *rect;
-
- rtgui_thread_send(RTGUI_TOPLEVEL(win)->server, &(event.parent), sizeof(struct rtgui_event_win_resize));
- }
-}
-
-void rtgui_win_set_box(rtgui_win_t* win, rtgui_box_t* box)
-{
- if (win == RT_NULL || box == RT_NULL) return;
-
- rtgui_container_add_child(RTGUI_CONTAINER(win), RTGUI_WIDGET(box));
- rtgui_widget_set_rect(RTGUI_WIDGET(box), &(RTGUI_WIDGET(win)->extent));
-}
-
-void rtgui_win_set_onactivate(rtgui_win_t* win, rtgui_event_handler_ptr handler)
-{
- if (win != RT_NULL)
- {
- win->on_activate = handler;
- }
-}
-
-void rtgui_win_set_ondeactivate(rtgui_win_t* win, rtgui_event_handler_ptr handler)
-{
- if (win != RT_NULL)
- {
- win->on_deactivate = handler;
- }
-}
-
-void rtgui_win_set_onclose(rtgui_win_t* win, rtgui_event_handler_ptr handler)
-{
- if (win != RT_NULL)
- {
- win->on_close = handler;
- }
-}
-
-void rtgui_win_set_title(rtgui_win_t* win, const char *title)
-{
- /* send title to server */
- if (RTGUI_TOPLEVEL(win)->server != RT_NULL)
- {
- }
-
- /* modify in local side */
- if (win->title != RT_NULL)
- {
- rtgui_free(win->title);
- win->title = RT_NULL;
- }
-
- if (title != RT_NULL)
- {
- win->title = rt_strdup(title);
- }
-}
-
-char* rtgui_win_get_title(rtgui_win_t* win)
-{
- RT_ASSERT(win != RT_NULL);
-
- return win->title;
-}
+#include <rtgui/widgets/window.h> +#include <rtgui/widgets/button.h> +#include <rtgui/widgets/workbench.h> + +static void _rtgui_win_constructor(rtgui_win_t *win) +{ + /* init window attribute */ + win->on_activate = RT_NULL; + win->on_deactivate = RT_NULL; + win->on_close = RT_NULL; + win->title = RT_NULL; + + /* set window hide */ + RTGUI_WIDGET_HIDE(RTGUI_WIDGET(win)); + + /* set window style */ + win->style = RTGUI_WIN_STYLE_DEFAULT; + rtgui_widget_set_event_handler(RTGUI_WIDGET(win), rtgui_win_event_handler); + + /* init user data */ + win->user_data = 0; +} + +static void _rtgui_win_destructor(rtgui_win_t* win) +{ + struct rtgui_event_win_destroy edestroy; + + if (RTGUI_TOPLEVEL(win)->server != RT_NULL) + { + /* destroy in server */ + RTGUI_EVENT_WIN_DESTROY_INIT(&edestroy); + edestroy.wid = win; + if (rtgui_thread_send_sync(RTGUI_TOPLEVEL(win)->server, RTGUI_EVENT(&edestroy), + sizeof(struct rtgui_event_win_destroy)) != RT_EOK) + { + /* destroy in server failed */ + return; + } + } + + /* release field */ + rt_free(win->title); +} + +static rt_bool_t _rtgui_win_create_in_server(rtgui_win_t* win) +{ + if (RTGUI_TOPLEVEL(win)->server == RT_NULL) + { + rt_thread_t server; + struct rtgui_event_win_create ecreate; + RTGUI_EVENT_WIN_CREATE_INIT(&ecreate); + + /* get server thread id */ + server = rtgui_thread_get_server(); + if (server == RT_NULL) + { + rt_kprintf("RTGUI server is not running...\n"); + return RT_FALSE; + } + + /* send win create event to server */ + ecreate.wid = win; + ecreate.extent = RTGUI_WIDGET(win)->extent; + ecreate.flag = win->style; + ecreate.mask = 0; + rt_strncpy((char*)ecreate.title, (char*)win->title, RTGUI_NAME_MAX); + + if (rtgui_thread_send_sync(server, RTGUI_EVENT(&ecreate), + sizeof(struct rtgui_event_win_create)) != RT_EOK) + { + rt_kprintf("create win: %s failed\n", win->title); + return RT_FALSE; + } + + /* set server */ + RTGUI_TOPLEVEL(win)->server = server; + } + + return RT_TRUE; +} + +rtgui_type_t *rtgui_win_type_get(void) +{ + static rtgui_type_t *win_type = RT_NULL; + + if (!win_type) + { + win_type = rtgui_type_create("win", RTGUI_TOPLEVEL_TYPE, + sizeof(rtgui_win_t), + RTGUI_CONSTRUCTOR(_rtgui_win_constructor), + RTGUI_DESTRUCTOR(_rtgui_win_destructor)); + } + + return win_type; +} + +rtgui_win_t* rtgui_win_create(rtgui_toplevel_t* parent_toplevel, const char* title, rtgui_rect_t *rect, rt_uint32_t style) +{ + struct rtgui_win* win; + + /* allocate win memory */ + win = (struct rtgui_win*) rtgui_widget_create (RTGUI_WIN_TYPE); + if (win != RT_NULL) + { + /* set parent toplevel */ + win->parent_toplevel = parent_toplevel; + + /* set title, rect and style */ + if (title != RT_NULL) win->title = rt_strdup(title); + else win->title = RT_NULL; + + rtgui_widget_set_rect(RTGUI_WIDGET(win), rect); + win->style = style; + + if (_rtgui_win_create_in_server(win) == RT_FALSE) + { + rtgui_widget_destroy(RTGUI_WIDGET(win)); + return RT_NULL; + } + } + + return win; +} + +void rtgui_win_destroy(struct rtgui_win* win) +{ + if (win->parent_toplevel != RT_NULL) + { + if (win->style & RTGUI_WIN_STYLE_MODAL) + { + /* exit modal mode */ + win->style &= ~RTGUI_WIN_STYLE_MODAL; + /* set style to closed */ + win->style |= RTGUI_WIN_STYLE_CLOSED; + } + else rtgui_widget_destroy(RTGUI_WIDGET(win)); + + } + else if (win->style & RTGUI_WIN_STYLE_CLOSED) + { + rtgui_widget_destroy(RTGUI_WIDGET(win)); + } + else + { + /* exit modal mode */ + win->style &= ~RTGUI_WIN_STYLE_MODAL; + /* set style to closed */ + win->style |= RTGUI_WIN_STYLE_CLOSED; + } +} + +rtgui_modal_code_t rtgui_win_show(struct rtgui_win* win, rt_bool_t is_modal) +{ + rtgui_modal_code_t result; + + RT_ASSERT(win != RT_NULL); + result = RTGUI_MODAL_CANCEL; + + /* if it does not register into server, create it in server */ + if (RTGUI_TOPLEVEL(win)->server == RT_NULL) + { + if (_rtgui_win_create_in_server(win) == RT_FALSE) + return result; + } + + if (RTGUI_WIDGET_IS_HIDE(RTGUI_WIDGET(win))) + { + /* send show message to server */ + struct rtgui_event_win_show eshow; + RTGUI_EVENT_WIN_SHOW_INIT(&eshow); + eshow.wid = win; + + if (rtgui_thread_send_sync(RTGUI_TOPLEVEL(win)->server, RTGUI_EVENT(&eshow), + sizeof(struct rtgui_event_win_show)) != RT_EOK) + { + /* hide window failed */ + return result; + } + + /* set window unhidden */ + RTGUI_WIDGET_UNHIDE(RTGUI_WIDGET(win)); + } + else rtgui_widget_update(RTGUI_WIDGET(win)); + + if (is_modal == RT_TRUE) + { + if (win->parent_toplevel != RT_NULL) + { + rtgui_workbench_t* workbench; + + /* get root toplevel */ + workbench = RTGUI_WORKBENCH(win->parent_toplevel); + workbench->flag |= RTGUI_WORKBENCH_FLAG_MODAL_MODE; + + rtgui_workbench_event_loop(workbench); + result = workbench->modal_code; + workbench->flag &= ~RTGUI_WORKBENCH_FLAG_MODAL_MODE; + } + else + { + /* which is a root window */ + win->style |= RTGUI_WIN_STYLE_MODAL; + rtgui_win_event_loop(win); + + result = win->modal_code; + win->style &= ~RTGUI_WIN_STYLE_MODAL; + } + } + + return result; +} + +void rtgui_win_end_modal(struct rtgui_win* win, rtgui_modal_code_t modal_code) +{ + if (win->parent_toplevel != RT_NULL) + { + rtgui_workbench_t* workbench; + + /* which is shown under workbench */ + workbench = RTGUI_WORKBENCH(win->parent_toplevel); + workbench->modal_code = modal_code; + workbench->flag &= ~RTGUI_WORKBENCH_FLAG_MODAL_MODE; + } + else + { + /* which is a stand alone window */ + win->modal_code = modal_code; + win->style &= ~RTGUI_WIN_STYLE_MODAL; + } +} + +void rtgui_win_hiden(struct rtgui_win* win) +{ + RT_ASSERT(win != RT_NULL); + + if (!RTGUI_WIDGET_IS_HIDE(RTGUI_WIDGET(win)) && + RTGUI_TOPLEVEL(win)->server != RT_NULL) + { + /* send hidden message to server */ + struct rtgui_event_win_hide ehide; + RTGUI_EVENT_WIN_HIDE_INIT(&ehide); + ehide.wid = win; + + if (rtgui_thread_send_sync(RTGUI_TOPLEVEL(win)->server, RTGUI_EVENT(&ehide), + sizeof(struct rtgui_event_win_hide)) != RT_EOK) + { + rt_kprintf("hide win: %s failed\n", win->title); + return; + } + + /* set window hide and deactivated */ + RTGUI_WIDGET_HIDE(RTGUI_WIDGET(win)); + win->style &= ~RTGUI_WIN_STYLE_ACTIVATE; + } +} + +rt_bool_t rtgui_win_is_activated(struct rtgui_win* win) +{ + RT_ASSERT(win != RT_NULL); + + return win->style & RTGUI_WIN_STYLE_ACTIVATE; +} + +void rtgui_win_move(struct rtgui_win* win, int x, int y) +{ + struct rtgui_event_win_move emove; + RTGUI_EVENT_WIN_MOVE_INIT(&emove); + + if (win == RT_NULL) return; + + if (RTGUI_TOPLEVEL(win)->server != RT_NULL) + { + /* set win hide firstly */ + RTGUI_WIDGET_HIDE(RTGUI_WIDGET(win)); + + emove.wid = win; + emove.x = x; + emove.y = y; + if (rtgui_thread_send_sync(RTGUI_TOPLEVEL(win)->server, RTGUI_EVENT(&emove), + sizeof(struct rtgui_event_win_move)) != RT_EOK) + { + return; + } + } + + /* move window to logic position */ + rtgui_widget_move_to_logic(RTGUI_WIDGET(win), + x - RTGUI_WIDGET(win)->extent.x1, + y - RTGUI_WIDGET(win)->extent.y1); + + /* set window visible */ + RTGUI_WIDGET_UNHIDE(RTGUI_WIDGET(win)); + return; +} + +static rt_bool_t rtgui_win_ondraw(struct rtgui_win* win) +{ + struct rtgui_dc* dc; + struct rtgui_rect rect; + struct rtgui_event_paint event; + + /* begin drawing */ + dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(win)); + if (dc == RT_NULL) return RT_FALSE; + + /* get window rect */ + rtgui_widget_get_rect(RTGUI_WIDGET(win), &rect); + /* fill area */ + rtgui_dc_fill_rect(dc, &rect); + + /* paint each widget */ + RTGUI_EVENT_PAINT_INIT(&event); + event.wid = RT_NULL; + rtgui_container_dispatch_event(RTGUI_CONTAINER(win), (rtgui_event_t*)&event); + + rtgui_dc_end_drawing(dc); + + return RT_FALSE; +} + +rt_bool_t rtgui_win_event_handler(struct rtgui_widget* widget, struct rtgui_event* event) +{ + struct rtgui_win* win = (struct rtgui_win*)widget; + + RT_ASSERT((win != RT_NULL) && (event != RT_NULL)); + + switch (event->type) + { + case RTGUI_EVENT_WIN_SHOW: + rtgui_win_show(win, RT_FALSE); + break; + + case RTGUI_EVENT_WIN_HIDE: + rtgui_win_hiden(win); + break; + + case RTGUI_EVENT_WIN_CLOSE: + if (win->on_close != RT_NULL) + { + if (win->on_close(widget, event) == RT_FALSE) return RT_TRUE; + } + + /* destroy window */ + rtgui_win_destroy(win); + + /* exit event loop */ + return RT_TRUE; + + case RTGUI_EVENT_WIN_MOVE: + { + struct rtgui_event_win_move* emove = (struct rtgui_event_win_move*)event; + + /* move window */ + rtgui_win_move(win, emove->x, emove->y); + } + break; + + case RTGUI_EVENT_WIN_ACTIVATE: + if (RTGUI_WIDGET_IS_HIDE(RTGUI_WIDGET(win))) + { + rt_kprintf("activate window, but window is hide!\n"); + } + + win->style |= RTGUI_WIN_STYLE_ACTIVATE; + if (widget->on_draw != RT_NULL) widget->on_draw(widget, event); + else rtgui_win_ondraw(win); + + if (win->on_activate != RT_NULL) + { + win->on_activate(widget, event); + } + break; + + case RTGUI_EVENT_WIN_DEACTIVATE: + win->style &= ~RTGUI_WIN_STYLE_ACTIVATE; + if (widget->on_draw != RT_NULL) widget->on_draw(widget, event); + else rtgui_win_ondraw(win); + + if (win->on_deactivate != RT_NULL) + { + win->on_deactivate(widget, event); + } + break; + + case RTGUI_EVENT_PAINT: + if (widget->on_draw != RT_NULL) widget->on_draw(widget, event); + else rtgui_win_ondraw(win); + break; + + case RTGUI_EVENT_MOUSE_BUTTON: + if (rtgui_container_dispatch_mouse_event(RTGUI_CONTAINER(win), (struct rtgui_event_mouse*)event) == RT_FALSE) + { + if (widget->on_mouseclick != RT_NULL) + { + return widget->on_mouseclick(widget, event); + } + } + break; + + case RTGUI_EVENT_MOUSE_MOTION: +#if 0 + if (rtgui_widget_dispatch_mouse_event(widget, + (struct rtgui_event_mouse*)event) == RT_FALSE) + { + /* handle event in current widget */ + if (widget->on_mousemotion != RT_NULL) + { + return widget->on_mousemotion(widget, event); + } + } + else return RT_TRUE; +#endif + break; + + default: + /* call parent event handler */ + return rtgui_toplevel_event_handler(widget, event); + } + + return RT_FALSE; +} + +/* windows event loop */ +void rtgui_win_event_loop(rtgui_win_t* wnd) +{ + /* the buffer uses to receive event */ + char event_buf[256]; + + struct rtgui_event* event = (struct rtgui_event*)&event_buf[0]; + + if (wnd->style & RTGUI_WIN_STYLE_MODAL) + { + while (wnd->style & RTGUI_WIN_STYLE_MODAL) + { + if (rtgui_thread_recv(event, sizeof(event_buf)) == RT_EOK) + { + /* perform event handler */ + RTGUI_WIDGET(wnd)->event_handler(RTGUI_WIDGET(wnd), event); + } + } + } + else + { + while (!(wnd->style & RTGUI_WIN_STYLE_CLOSED)) + { + if (rtgui_thread_recv(event, sizeof(event_buf)) == RT_EOK) + { + /* perform event handler */ + RTGUI_WIDGET(wnd)->event_handler(RTGUI_WIDGET(wnd), event); + } + } + } + + /* destroy window */ + rtgui_widget_destroy(RTGUI_WIDGET(wnd)); +} + +void rtgui_win_set_rect(rtgui_win_t* win, rtgui_rect_t* rect) +{ + struct rtgui_event_win_resize event; + + if (win == RT_NULL || rect == RT_NULL) return; + + RTGUI_WIDGET(win)->extent = *rect; + + if (RTGUI_TOPLEVEL(win)->server != RT_NULL) + { + /* set window resize event to server */ + RTGUI_EVENT_WIN_RESIZE_INIT(&event); + event.wid = win; + event.rect = *rect; + + rtgui_thread_send(RTGUI_TOPLEVEL(win)->server, &(event.parent), sizeof(struct rtgui_event_win_resize)); + } +} + +void rtgui_win_set_box(rtgui_win_t* win, rtgui_box_t* box) +{ + if (win == RT_NULL || box == RT_NULL) return; + + rtgui_container_add_child(RTGUI_CONTAINER(win), RTGUI_WIDGET(box)); + rtgui_widget_set_rect(RTGUI_WIDGET(box), &(RTGUI_WIDGET(win)->extent)); +} + +void rtgui_win_set_onactivate(rtgui_win_t* win, rtgui_event_handler_ptr handler) +{ + if (win != RT_NULL) + { + win->on_activate = handler; + } +} + +void rtgui_win_set_ondeactivate(rtgui_win_t* win, rtgui_event_handler_ptr handler) +{ + if (win != RT_NULL) + { + win->on_deactivate = handler; + } +} + +void rtgui_win_set_onclose(rtgui_win_t* win, rtgui_event_handler_ptr handler) +{ + if (win != RT_NULL) + { + win->on_close = handler; + } +} + +void rtgui_win_set_title(rtgui_win_t* win, const char *title) +{ + /* send title to server */ + if (RTGUI_TOPLEVEL(win)->server != RT_NULL) + { + } + + /* modify in local side */ + if (win->title != RT_NULL) + { + rtgui_free(win->title); + win->title = RT_NULL; + } + + if (title != RT_NULL) + { + win->title = rt_strdup(title); + } +} + +char* rtgui_win_get_title(rtgui_win_t* win) +{ + RT_ASSERT(win != RT_NULL); + + return win->title; +} diff --git a/rtgui/widgets/workbench.c b/rtgui/widgets/workbench.c index cdeb93232b..239237bb3e 100644 --- a/rtgui/widgets/workbench.c +++ b/rtgui/widgets/workbench.c @@ -23,6 +23,7 @@ static void _rtgui_workbench_constructor(rtgui_workbench_t *workbench) /* set attributes */ workbench->panel = RT_NULL; workbench->flag = RTGUI_WORKBENCH_FLAG_DEFAULT; + workbench->modal_code = RTGUI_MODAL_OK; workbench->current_view = RT_NULL; } @@ -157,10 +158,8 @@ void rtgui_workbench_set_flag(rtgui_workbench_t* workbench, rt_uint8_t flag) workbench->flag = flag; } -void rtgui_workbench_event_loop(rtgui_workbench_t* workbench) +rt_bool_t rtgui_workbench_event_loop(rtgui_workbench_t* workbench) { - int quit = 0; - /* the buffer uses to receive event */ char event_buf[256]; struct rtgui_event* event = (struct rtgui_event*)&event_buf[0]; @@ -168,13 +167,29 @@ void rtgui_workbench_event_loop(rtgui_workbench_t* workbench) /* show workbench firstly */ rtgui_workbench_show(workbench); - while (!quit) + if (workbench->flag & RTGUI_WORKBENCH_FLAG_MODAL_MODE) { - if (rtgui_thread_recv(event, sizeof(event_buf)) == RT_EOK) + /* event loop for modal mode shown view */ + while (workbench->flag & RTGUI_WORKBENCH_FLAG_MODAL_MODE) { - RTGUI_WIDGET(workbench)->event_handler(RTGUI_WIDGET(workbench), event); + if (rtgui_thread_recv(event, sizeof(event_buf)) == RT_EOK) + { + RTGUI_WIDGET(workbench)->event_handler(RTGUI_WIDGET(workbench), event); + } } } + else + { + while (!(workbench->flag & RTGUI_WORKBENCH_FLAG_CLOSED)) + { + if (rtgui_thread_recv(event, sizeof(event_buf)) == RT_EOK) + { + RTGUI_WIDGET(workbench)->event_handler(RTGUI_WIDGET(workbench), event); + } + } + } + + return RT_TRUE; } rt_err_t rtgui_workbench_show(rtgui_workbench_t* workbench) @@ -301,6 +316,13 @@ rt_bool_t rtgui_workbench_event_handler(rtgui_widget_t* widget, rtgui_event_t* e } else { + if (RTGUI_CONTAINER(widget)->focused == widget) + { + /* set focused widget to the current view */ + if (workbench->current_view != RT_NULL) + rtgui_widget_focus(RTGUI_WIDGET(RTGUI_CONTAINER(workbench->current_view)->focused)); + } + return rtgui_toplevel_event_handler(widget, event); } } @@ -465,8 +487,8 @@ void rtgui_workbench_hide_view(rtgui_workbench_t* workbench, rtgui_view_t* view) next_view = RTGUI_VIEW(rtgui_widget_get_prev_sibling(RTGUI_WIDGET(view))); if (next_view != RT_NULL) - {
- rtgui_view_show(next_view); + { + rtgui_view_show(next_view, RT_FALSE); } else { |
