diff --git a/rtgui/common/dc.c b/rtgui/common/dc.c index c73b88a5c9..652e6b7fd7 100644 --- a/rtgui/common/dc.c +++ b/rtgui/common/dc.c @@ -41,6 +41,16 @@ void rtgui_dc_draw_point(struct rtgui_dc* dc, int x, int y) dc->draw_point(dc, x, y); } +/* + * draw a color point on dc + */ +void rtgui_dc_draw_color_point(struct rtgui_dc* dc, int x, int y, rtgui_color_t color) +{ + if (dc == RT_NULL) return; + + dc->draw_color_point(dc, x, y, color); +} + /* * draw a vertical line on dc */ @@ -178,7 +188,7 @@ void rtgui_dc_draw_text (struct rtgui_dc* dc, const char* text, struct rtgui_rec RT_ASSERT(dc != RT_NULL); - font = rtgui_dc_get_font(dc); + font = RTGUI_DC_FONT(dc); if (font == RT_NULL) { /* use system default font */ @@ -195,7 +205,7 @@ void rtgui_dc_draw_text (struct rtgui_dc* dc, const char* text, struct rtgui_rec /* text align */ rtgui_font_get_metrics(font, text, &text_rect); - rtgui_rect_moveto_align(rect, &text_rect, rtgui_dc_get_textalign(dc)); + rtgui_rect_moveto_align(rect, &text_rect, RTGUI_DC_TEXTALIGN(dc)); #ifdef RTGUI_USING_FONTHZ while (*text) @@ -261,60 +271,24 @@ void rtgui_dc_draw_word(struct rtgui_dc*dc, int x, int y, int h, const rt_uint8_ } } -void rtgui_dc_set_color(struct rtgui_dc* dc, rtgui_color_t color) +void rtgui_dc_set_gc(struct rtgui_dc* dc, rtgui_gc_t* gc) { if (dc != RT_NULL) { - dc->set_color(dc, color); + dc->set_gc(dc, gc); } } -rtgui_color_t rtgui_dc_get_color(struct rtgui_dc* dc) +rtgui_gc_t *rtgui_dc_get_gc(struct rtgui_dc* dc) { if (dc != RT_NULL) { - return dc->get_color(dc); - } - - return white; -} - -void rtgui_dc_set_font(struct rtgui_dc* dc, rtgui_font_t* font) -{ - if (dc != RT_NULL) - { - dc->set_font(dc, font); - } -} - -rtgui_font_t* rtgui_dc_get_font(struct rtgui_dc* dc) -{ - if (dc != RT_NULL) - { - return dc->get_font(dc); + return dc->get_gc(dc); } return RT_NULL; } -void rtgui_dc_set_textalign(struct rtgui_dc* dc, rt_int32_t align) -{ - if (dc != RT_NULL) - { - dc->set_textalign(dc, align); - } -} - -rt_int32_t rtgui_dc_get_textalign(struct rtgui_dc* dc) -{ - if (dc != RT_NULL) - { - return dc->get_textalign(dc); - } - - return RTGUI_ALIGN_NOT; -} - rt_bool_t rtgui_dc_get_visible(struct rtgui_dc* dc) { if (dc != RT_NULL) @@ -330,11 +304,11 @@ void rtgui_dc_draw_shaded_rect(struct rtgui_dc* dc, rtgui_rect_t* rect, { RT_ASSERT(dc != RT_NULL); - rtgui_dc_set_color(dc, c1); + RTGUI_DC_FC(dc) = c1; rtgui_dc_draw_vline(dc, rect->x1, rect->y1, rect->y2); rtgui_dc_draw_hline(dc, rect->x1 + 1, rect->x2, rect->y1); - rtgui_dc_set_color(dc, c2); + RTGUI_DC_FC(dc) = c2; rtgui_dc_draw_vline(dc, rect->x2, rect->y1, rect->y2); rtgui_dc_draw_hline(dc, rect->x1, rect->x2 + 1, rect->y2); } @@ -347,7 +321,7 @@ void rtgui_dc_draw_border(struct rtgui_dc* dc, rtgui_rect_t* rect, int flag) if (dc == RT_NULL) return ; /* save old color */ - color = rtgui_dc_get_color(dc); + color = RTGUI_DC_FC(dc); r = *rect; switch (flag) @@ -375,12 +349,12 @@ void rtgui_dc_draw_border(struct rtgui_dc* dc, rtgui_rect_t* rect, int flag) break; case RTGUI_BORDER_EXTRA: - rtgui_dc_set_color(dc, light_grey); + RTGUI_DC_FC(dc) = light_grey; rtgui_dc_draw_rect(dc, &r); break; case RTGUI_BORDER_SIMPLE: - rtgui_dc_set_color(dc, black); + RTGUI_DC_FC(dc) = black; rtgui_dc_draw_rect(dc, &r); break; @@ -389,7 +363,7 @@ void rtgui_dc_draw_border(struct rtgui_dc* dc, rtgui_rect_t* rect, int flag) } /* restore color */ - rtgui_dc_set_color(dc, color); + RTGUI_DC_FC(dc) = color; } void rtgui_dc_draw_horizontal_line(struct rtgui_dc* dc, int x1, int x2, int y) @@ -399,18 +373,18 @@ void rtgui_dc_draw_horizontal_line(struct rtgui_dc* dc, int x1, int x2, int y) if (dc == RT_NULL) return ; /* save old color */ - color = rtgui_dc_get_color(dc); + color = RTGUI_DC_FC(dc); - rtgui_dc_set_color(dc, dark_grey); + RTGUI_DC_FC(dc) = dark_grey; rtgui_dc_draw_hline(dc, x1, x2, y); y ++; - rtgui_dc_set_color(dc, high_light); + RTGUI_DC_FC(dc) = high_light; rtgui_dc_draw_hline(dc, x1, x2, y); /* restore color */ - rtgui_dc_set_color(dc, color); + RTGUI_DC_FC(dc) = color; } void rtgui_dc_draw_vertical_line(struct rtgui_dc* dc, int x, int y1, int y2) @@ -420,18 +394,18 @@ void rtgui_dc_draw_vertical_line(struct rtgui_dc* dc, int x, int y1, int y2) if (dc == RT_NULL) return ; /* save old color */ - color = rtgui_dc_get_color(dc); + color = RTGUI_DC_FC(dc); - rtgui_dc_set_color(dc, dark_grey); + RTGUI_DC_FC(dc) = dark_grey; rtgui_dc_draw_hline(dc, x, y1, y2); x ++; - rtgui_dc_set_color(dc, high_light); + RTGUI_DC_FC(dc) = high_light; rtgui_dc_draw_hline(dc, x, y1, y2); /* restore color */ - rtgui_dc_set_color(dc, color); + RTGUI_DC_FC(dc) = color; } void rtgui_dc_draw_arrow(struct rtgui_dc* dc, rtgui_rect_t* rect, int kind) diff --git a/rtgui/common/dc_buffer.c b/rtgui/common/dc_buffer.c index 57aa71becd..2a82ad2b39 100644 --- a/rtgui/common/dc_buffer.c +++ b/rtgui/common/dc_buffer.c @@ -21,6 +21,9 @@ struct rtgui_dc_buffer { struct rtgui_dc parent; + /* graphic context */ + rtgui_gc_t gc; + /* color and font */ rtgui_color_t color; struct rtgui_font* font; @@ -41,17 +44,16 @@ struct rtgui_dc_buffer static rt_bool_t rtgui_dc_buffer_fini(struct rtgui_dc* dc); static void rtgui_dc_buffer_draw_point(struct rtgui_dc* dc, int x, int y); +static void rtgui_dc_buffer_draw_color_point(struct rtgui_dc* dc, int x, int y, rtgui_color_t color); static void rtgui_dc_buffer_draw_vline(struct rtgui_dc* dc, int x, int y1, int y2); static void rtgui_dc_buffer_draw_hline(struct rtgui_dc* dc, int x1, int x2, int y); static void rtgui_dc_buffer_fill_rect (struct rtgui_dc* dc, struct rtgui_rect* rect); static void rtgui_dc_buffer_blit(struct rtgui_dc* self, struct rtgui_point* dc_point, struct rtgui_dc* dest, rtgui_rect_t* rect); -static void rtgui_dc_buffer_set_color (struct rtgui_dc* dc, rtgui_color_t color); -static rtgui_color_t rtgui_dc_buffer_get_color(struct rtgui_dc* dc); -static void rtgui_dc_buffer_set_font(struct rtgui_dc* dc, rtgui_font_t* font); -static rtgui_font_t* rtgui_dc_buffer_get_font(struct rtgui_dc* dc); -static void rtgui_dc_buffer_set_textalign(struct rtgui_dc* dc, rt_int32_t textalign); -static rt_int32_t rtgui_dc_buffer_get_textalign(struct rtgui_dc* dc); + +static void rtgui_dc_buffer_set_gc (struct rtgui_dc* dc, rtgui_gc_t *gc); +static rtgui_gc_t* rtgui_dc_buffer_get_gc(struct rtgui_dc* dc); + static rt_bool_t rtgui_dc_buffer_get_visible(struct rtgui_dc* dc); static void rtgui_dc_buffer_get_rect(struct rtgui_dc* dc, rtgui_rect_t* rect); @@ -61,18 +63,14 @@ static void rtgui_dc_buffer_init(struct rtgui_dc_buffer* dc) dc->parent.type = RTGUI_DC_BUFFER; dc->parent.draw_point = rtgui_dc_buffer_draw_point; + dc->parent.draw_color_point = rtgui_dc_buffer_draw_color_point; dc->parent.draw_hline = rtgui_dc_buffer_draw_hline; dc->parent.draw_vline = rtgui_dc_buffer_draw_vline; dc->parent.fill_rect = rtgui_dc_buffer_fill_rect; dc->parent.blit = rtgui_dc_buffer_blit; - dc->parent.set_color = rtgui_dc_buffer_set_color; - dc->parent.get_color = rtgui_dc_buffer_get_color; - - dc->parent.set_font = rtgui_dc_buffer_set_font; - dc->parent.get_font = rtgui_dc_buffer_get_font; - dc->parent.set_textalign = rtgui_dc_buffer_set_textalign; - dc->parent.get_textalign = rtgui_dc_buffer_get_textalign; + dc->parent.set_gc = rtgui_dc_buffer_set_gc; + dc->parent.get_gc = rtgui_dc_buffer_get_gc; dc->parent.get_visible= rtgui_dc_buffer_get_visible; dc->parent.get_rect = rtgui_dc_buffer_get_rect; @@ -137,6 +135,19 @@ static void rtgui_dc_buffer_draw_point(struct rtgui_dc* self, int x, int y) *ptr = dc->color; } +static void rtgui_dc_buffer_draw_color_point(struct rtgui_dc* self, int x, int y, rtgui_color_t color) +{ + rtgui_color_t* ptr; + struct rtgui_dc_buffer* dc; + + dc = (struct rtgui_dc_buffer*)self; + + /* note: there is no parameter check in this function */ + ptr = (rtgui_color_t*)(dc->pixel + y * dc->pitch + x * sizeof(rtgui_color_t)); + + *ptr = color; +} + static void rtgui_dc_buffer_draw_vline(struct rtgui_dc* self, int x, int y1, int y2) { rtgui_color_t* ptr; @@ -252,7 +263,7 @@ static void rtgui_dc_buffer_blit(struct rtgui_dc* self, struct rtgui_point* dc_p if (dc_point == RT_NULL) dc_point = &rtgui_empty_point; - if ((dest->type == RTGUI_DC_HW) && rtgui_dc_get_visible(dest) == RT_TRUE) + if (dest->type == RTGUI_DC_HW) { rtgui_color_t* pixel; rt_uint8_t *line_ptr; @@ -312,46 +323,19 @@ static void rtgui_dc_buffer_blit(struct rtgui_dc* self, struct rtgui_point* dc_p } } -static void rtgui_dc_buffer_set_color (struct rtgui_dc* self, rtgui_color_t color) + +static void rtgui_dc_buffer_set_gc(struct rtgui_dc* self, rtgui_gc_t *gc) { struct rtgui_dc_buffer* dc = (struct rtgui_dc_buffer*)self; - dc->color = color; + dc->gc = *gc; } -static rtgui_color_t rtgui_dc_buffer_get_color(struct rtgui_dc* self) +static rtgui_gc_t *rtgui_dc_buffer_get_gc(struct rtgui_dc* self) { struct rtgui_dc_buffer* dc = (struct rtgui_dc_buffer*)self; - return dc->color; -} - -static void rtgui_dc_buffer_set_font(struct rtgui_dc* self, rtgui_font_t* font) -{ - struct rtgui_dc_buffer* dc = (struct rtgui_dc_buffer*)self; - - dc->font = font; -} - -static rtgui_font_t* rtgui_dc_buffer_get_font(struct rtgui_dc* self) -{ - struct rtgui_dc_buffer* dc = (struct rtgui_dc_buffer*)self; - - return dc->font; -} - -static void rtgui_dc_buffer_set_textalign(struct rtgui_dc* self, rt_int32_t textalign) -{ - struct rtgui_dc_buffer* dc = (struct rtgui_dc_buffer*)self; - - dc->align = textalign; -} - -static rt_int32_t rtgui_dc_buffer_get_textalign(struct rtgui_dc* self) -{ - struct rtgui_dc_buffer* dc = (struct rtgui_dc_buffer*)self; - - return dc->align; + return &dc->gc; } static rt_bool_t rtgui_dc_buffer_get_visible(struct rtgui_dc* dc) diff --git a/rtgui/common/dc_hw.c b/rtgui/common/dc_hw.c index bc5e1e8b69..bf07cceed3 100644 --- a/rtgui/common/dc_hw.c +++ b/rtgui/common/dc_hw.c @@ -21,16 +21,13 @@ #include static void rtgui_dc_hw_draw_point(struct rtgui_dc* dc, int x, int y); +static void rtgui_dc_hw_draw_color_point(struct rtgui_dc* dc, int x, int y, rtgui_color_t color); static void rtgui_dc_hw_draw_hline(struct rtgui_dc* dc, int x1, int x2, int y); static void rtgui_dc_hw_draw_vline(struct rtgui_dc* dc, int x, int y1, int y2); static void rtgui_dc_hw_fill_rect (struct rtgui_dc* dc, rtgui_rect_t* rect); static void rtgui_dc_hw_blit (struct rtgui_dc* dc, struct rtgui_point* dc_point, struct rtgui_dc* dest, rtgui_rect_t* rect); -static void rtgui_dc_hw_set_color (struct rtgui_dc* dc, rtgui_color_t color); -static rtgui_color_t rtgui_dc_hw_get_color (struct rtgui_dc* dc); -static void rtgui_dc_hw_set_font(struct rtgui_dc* dc, rtgui_font_t* font); -static rtgui_font_t* rtgui_dc_hw_get_font(struct rtgui_dc* dc); -static void rtgui_dc_hw_set_textalign(struct rtgui_dc* dc, rt_int32_t textalign); -static rt_int32_t rtgui_dc_hw_get_textalign(struct rtgui_dc* dc); +static void rtgui_dc_hw_set_gc (struct rtgui_dc* dc, rtgui_gc_t *gc); +static rtgui_gc_t *rtgui_dc_hw_get_gc (struct rtgui_dc* dc); static rt_bool_t rtgui_dc_hw_fini(struct rtgui_dc* dc); static rt_bool_t rtgui_dc_hw_get_visible(struct rtgui_dc* dc); static void rtgui_dc_hw_get_rect(struct rtgui_dc* dc, rtgui_rect_t* rect); @@ -54,18 +51,14 @@ void rtgui_dc_hw_init(struct rtgui_dc_hw* dc) dc->parent.type = RTGUI_DC_HW; dc->parent.draw_point = rtgui_dc_hw_draw_point; + dc->parent.draw_color_point = rtgui_dc_hw_draw_color_point; dc->parent.draw_hline = rtgui_dc_hw_draw_hline; dc->parent.draw_vline = rtgui_dc_hw_draw_vline; dc->parent.fill_rect = rtgui_dc_hw_fill_rect ; dc->parent.blit = rtgui_dc_hw_blit; - dc->parent.set_color = rtgui_dc_hw_set_color; - dc->parent.get_color = rtgui_dc_hw_get_color; - - dc->parent.set_font = rtgui_dc_hw_set_font; - dc->parent.get_font = rtgui_dc_hw_get_font; - dc->parent.set_textalign = rtgui_dc_hw_set_textalign; - dc->parent.get_textalign = rtgui_dc_hw_get_textalign; + dc->parent.set_gc = rtgui_dc_hw_set_gc; + dc->parent.get_gc = rtgui_dc_hw_get_gc; dc->parent.get_visible= rtgui_dc_hw_get_visible; dc->parent.get_rect = rtgui_dc_hw_get_rect; @@ -240,6 +233,24 @@ static void rtgui_dc_hw_draw_point(struct rtgui_dc* self, int x, int y) } } +static void rtgui_dc_hw_draw_color_point(struct rtgui_dc* self, int x, int y, rtgui_color_t color) +{ + struct rtgui_dc_hw* dc; + rtgui_rect_t rect; + + dc = (struct rtgui_dc_hw*)self; + if (dc == RT_NULL || dc->visible != RT_TRUE) return; + + x = x + dc->owner->extent.x1; + y = y + dc->owner->extent.y1; + + if (rtgui_region_contains_point(&(dc->owner->clip), x, y, &rect) == RT_EOK) + { + /* draw this point */ + dc->device->set_pixel(&color, x, y); + } +} + /* * draw a logic vertical line on device */ @@ -376,52 +387,21 @@ static void rtgui_dc_hw_blit(struct rtgui_dc* dc, struct rtgui_point* dc_point, return ; } -static void rtgui_dc_hw_set_color(struct rtgui_dc* self, rtgui_color_t color) +static void rtgui_dc_hw_set_gc(struct rtgui_dc* self, rtgui_gc_t *gc) { struct rtgui_dc_hw* dc = (struct rtgui_dc_hw*)self; if (self != RT_NULL) { - dc->owner->gc.foreground = color; + dc->owner->gc = *gc; } } -static rtgui_color_t rtgui_dc_hw_get_color(struct rtgui_dc* self) +static rtgui_gc_t* rtgui_dc_hw_get_gc(struct rtgui_dc* self) { struct rtgui_dc_hw* dc = (struct rtgui_dc_hw*)self; - return self != RT_NULL? dc->owner->gc.foreground : white; -} - -static void rtgui_dc_hw_set_font(struct rtgui_dc* self, rtgui_font_t* font) -{ - struct rtgui_dc_hw* dc = (struct rtgui_dc_hw*)self; - - if (self != RT_NULL) - { - dc->owner->gc.font = font; - } -} - -static rtgui_font_t* rtgui_dc_hw_get_font(struct rtgui_dc* self) -{ - struct rtgui_dc_hw* dc = (struct rtgui_dc_hw*)self; - - return self != RT_NULL? dc->owner->gc.font : RT_NULL; -} - -static void rtgui_dc_hw_set_textalign(struct rtgui_dc* self, rt_int32_t textalign) -{ - struct rtgui_dc_hw* dc = (struct rtgui_dc_hw*)self; - - dc->owner->gc.textalign = textalign; -} - -static rt_int32_t rtgui_dc_hw_get_textalign(struct rtgui_dc* self) -{ - struct rtgui_dc_hw* dc = (struct rtgui_dc_hw*)self; - - return dc->owner->gc.textalign; + return self != RT_NULL? &(dc->owner->gc) : RT_NULL; } static rt_bool_t rtgui_dc_hw_get_visible(struct rtgui_dc* self) @@ -441,14 +421,14 @@ static void rtgui_dc_hw_get_rect(struct rtgui_dc* self, rtgui_rect_t* rect) void rtgui_dc_hw_draw_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; + + if (dc == RT_NULL || dc->visible != RT_TRUE) return; /* 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; @@ -482,6 +462,6 @@ void rtgui_dc_hw_draw_raw_hline(struct rtgui_dc_hw* dc, rt_uint8_t* raw_ptr, int 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); + dc->device->draw_raw_hline(raw_ptr + (draw_x1 - x1) * dc->device->byte_per_pixel, draw_x1, draw_x2, y); } } diff --git a/rtgui/common/image_jpg.c b/rtgui/common/image_jpg.c index 9a4a9375f5..189db53492 100644 --- a/rtgui/common/image_jpg.c +++ b/rtgui/common/image_jpg.c @@ -365,7 +365,6 @@ static void rtgui_image_jpeg_blit(struct rtgui_image* image, struct rtgui_dc* dc { rt_uint16_t x, y; rtgui_color_t* ptr; - rtgui_color_t foreground; struct rtgui_image_jpeg* jpeg; RT_ASSERT(image != RT_NULL && dc != RT_NULL && rect != RT_NULL); @@ -373,9 +372,6 @@ static void rtgui_image_jpeg_blit(struct rtgui_image* image, struct rtgui_dc* dc jpeg = (struct rtgui_image_jpeg*) image->data; RT_ASSERT(jpeg != RT_NULL); - /* save foreground color */ - foreground = rtgui_dc_get_color(dc); - if (jpeg->pixels != RT_NULL) { ptr = (rtgui_color_t*) jpeg->pixels; @@ -388,8 +384,7 @@ static void rtgui_image_jpeg_blit(struct rtgui_image* image, struct rtgui_dc* dc /* not alpha */ if ((*ptr >> 24) != 255) { - rtgui_dc_set_color(dc, *ptr); - rtgui_dc_draw_point(dc, x + rect->x1, y + rect->y1); + rtgui_dc_draw_color_point(dc, x + rect->x1, y + rect->y1, *ptr); } /* move to next color buffer */ @@ -411,8 +406,7 @@ static void rtgui_image_jpeg_blit(struct rtgui_image* image, struct rtgui_dc* dc /* not alpha */ if ((*ptr >> 24) != 255) { - rtgui_dc_set_color(dc, *ptr); - rtgui_dc_draw_point(dc, x + rect->x1, y + rect->y1); + rtgui_dc_draw_color_point(dc, x + rect->x1, y + rect->y1, *ptr); } /* move to next color buffer */ @@ -420,9 +414,6 @@ static void rtgui_image_jpeg_blit(struct rtgui_image* image, struct rtgui_dc* dc } } } - - /* restore foreground */ - rtgui_dc_set_color(dc, foreground); } static rt_bool_t rtgui_image_jpeg_check(struct rtgui_filerw* file) diff --git a/rtgui/common/image_png.c b/rtgui/common/image_png.c index ae9135c9f7..3560f7a2ff 100644 --- a/rtgui/common/image_png.c +++ b/rtgui/common/image_png.c @@ -42,7 +42,7 @@ static void rtgui_image_png_read_data(png_structp png_ptr, png_bytep data, png_s static rt_bool_t rtgui_image_png_process(png_structp png_ptr, png_infop info_ptr, struct rtgui_image_png* png) { - int x, y; + rt_uint32_t x, y; png_bytep row; png_bytep data; rtgui_color_t *ptr; @@ -229,7 +229,6 @@ static void rtgui_image_png_blit(struct rtgui_image* image, struct rtgui_dc* dc, { rt_uint16_t x, y, w, h; rtgui_color_t* ptr; - rtgui_color_t foreground; struct rtgui_image_png* png; RT_ASSERT(image != RT_NULL && dc != RT_NULL && rect != RT_NULL); @@ -242,9 +241,6 @@ static void rtgui_image_png_blit(struct rtgui_image* image, struct rtgui_dc* dc, if (image->h < rtgui_rect_height(*rect)) h = image->h; else h = rtgui_rect_height(*rect); - /* save foreground color */ - foreground = rtgui_dc_get_color(dc); - if (png->pixels != RT_NULL) { ptr = (rtgui_color_t*)png->pixels; @@ -256,8 +252,7 @@ static void rtgui_image_png_blit(struct rtgui_image* image, struct rtgui_dc* dc, /* not alpha */ if ((*ptr >> 24) != 255) { - rtgui_dc_set_color(dc, *ptr); - rtgui_dc_draw_point(dc, x + rect->x1, y + rect->y1); + rtgui_dc_draw_color_point(dc, x + rect->x1, y + rect->y1, *ptr); } /* move to next color buffer */ @@ -284,8 +279,8 @@ static void rtgui_image_png_blit(struct rtgui_image* image, struct rtgui_dc* dc, data = &(row[x * 4]); if (data[3] != 0) { - rtgui_dc_set_color(dc, RTGUI_ARGB((255 - data[3]), data[0], data[1], data[2])); - rtgui_dc_draw_point(dc, x + rect->x1, y + rect->y1); + rtgui_dc_draw_color_point(dc, x + rect->x1, y + rect->y1, + RTGUI_ARGB((255 - data[3]), data[0], data[1], data[2])); } } } @@ -300,10 +295,10 @@ static void rtgui_image_png_blit(struct rtgui_image* image, struct rtgui_dc* dc, { data = &(row[x]); - rtgui_dc_set_color(dc, RTGUI_ARGB(0, png->info_ptr->palette[data[0]].red, - png->info_ptr->palette[data[0]].green, - png->info_ptr->palette[data[0]].blue)); - rtgui_dc_draw_point(dc, x + rect->x1, y + rect->y1); + rtgui_dc_draw_color_point(dc, x + rect->x1, y + rect->y1, + RTGUI_ARGB(0, png->info_ptr->palette[data[0]].red, + png->info_ptr->palette[data[0]].green, + png->info_ptr->palette[data[0]].blue)); } } @@ -313,9 +308,6 @@ static void rtgui_image_png_blit(struct rtgui_image* image, struct rtgui_dc* dc, rtgui_free(row); } - - /* restore foreground */ - rtgui_dc_set_color(dc, foreground); } void rtgui_image_png_init() diff --git a/rtgui/common/image_xpm.c b/rtgui/common/image_xpm.c index 6200023991..eecf6bb3a3 100644 --- a/rtgui/common/image_xpm.c +++ b/rtgui/common/image_xpm.c @@ -585,16 +585,12 @@ static void rtgui_image_xpm_blit(struct rtgui_image* image, struct rtgui_dc* dc, { rt_uint16_t x, y; rtgui_color_t* ptr; - rtgui_color_t foreground; RT_ASSERT(image != RT_NULL && dc != RT_NULL && rect != RT_NULL); RT_ASSERT(image->data != RT_NULL); ptr = (rtgui_color_t*) image->data; - /* save foreground color */ - foreground = rtgui_dc_get_color(dc); - /* draw each point within dc */ for (y = 0; y < image->h; y ++) { @@ -603,16 +599,12 @@ static void rtgui_image_xpm_blit(struct rtgui_image* image, struct rtgui_dc* dc, /* not alpha */ if ((*ptr >> 24) != 255) { - rtgui_dc_set_color(dc, *ptr); - rtgui_dc_draw_point(dc, x + rect->x1, y + rect->y1); + rtgui_dc_draw_color_point(dc, x + rect->x1, y + rect->y1, *ptr); } /* move to next color buffer */ ptr ++; } } - - /* restore foreground */ - rtgui_dc_set_color(dc, foreground); } diff --git a/rtgui/common/rtgui_theme.c b/rtgui/common/rtgui_theme.c index 392ab67721..42760cf060 100644 --- a/rtgui/common/rtgui_theme.c +++ b/rtgui/common/rtgui_theme.c @@ -505,7 +505,7 @@ void rtgui_theme_draw_radiobox(struct rtgui_radiobox* radiobox) bord_size = item_size; else { - rtgui_font_get_metrics(rtgui_dc_get_font(dc), "H", &item_rect); + rtgui_font_get_metrics(RTGUI_DC_FONT(dc), "H", &item_rect); bord_size = rtgui_rect_height(item_rect); } @@ -529,7 +529,7 @@ void rtgui_theme_draw_radiobox(struct rtgui_radiobox* radiobox) struct rtgui_rect text_rect; /* draw group text */ - rtgui_font_get_metrics(rtgui_dc_get_font(dc), radiobox->text, &text_rect); + rtgui_font_get_metrics(RTGUI_DC_FONT(dc), radiobox->text, &text_rect); rtgui_rect_moveto(&text_rect, rect.x1 + bord_size + 5, rect.y1); rect.x1 -= 5; rect.x2 += 5; rtgui_dc_fill_rect(dc, &text_rect); @@ -804,8 +804,8 @@ void rtgui_theme_draw_selected(struct rtgui_dc* dc, rtgui_rect_t *rect) rtgui_color_t bc; rt_uint16_t index; - bc = rtgui_dc_get_color(dc); - rtgui_dc_set_color(dc, selected_color); + bc = RTGUI_DC_FC(dc); + RTGUI_DC_FC(dc) = selected_color; rtgui_dc_draw_hline(dc, rect->x1 + 3, rect->x2 - 2, rect->y1 + 1); rtgui_dc_draw_hline(dc, rect->x1 + 3, rect->x2 - 2, rect->y2 - 2); @@ -816,7 +816,7 @@ void rtgui_theme_draw_selected(struct rtgui_dc* dc, rtgui_rect_t *rect) for (index = rect->y1 + 1; index < rect->y2 - 2; index ++) rtgui_dc_draw_hline(dc, rect->x1 + 3, rect->x2 - 2, index); - rtgui_dc_set_color(dc, bc); + RTGUI_DC_FC(dc) = bc; } /* get default background color */ diff --git a/rtgui/include/rtgui/color.h b/rtgui/include/rtgui/color.h index 6150b99039..e13f461885 100644 --- a/rtgui/include/rtgui/color.h +++ b/rtgui/include/rtgui/color.h @@ -16,7 +16,6 @@ #include -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))|\ diff --git a/rtgui/include/rtgui/dc.h b/rtgui/include/rtgui/dc.h index ec7d50f461..a4abda7cc3 100644 --- a/rtgui/include/rtgui/dc.h +++ b/rtgui/include/rtgui/dc.h @@ -34,20 +34,15 @@ struct rtgui_dc /* interface */ void (*draw_point)(struct rtgui_dc* dc, int x, int y); + void (*draw_color_point)(struct rtgui_dc* dc, int x, int y, rtgui_color_t color); void (*draw_vline)(struct rtgui_dc* dc, int x, int y1, int y2); void (*draw_hline)(struct rtgui_dc* dc, int x1, int x2, int y); void (*fill_rect )(struct rtgui_dc* dc, rtgui_rect_t* rect); void (*blit )(struct rtgui_dc* dc, struct rtgui_point* dc_point, struct rtgui_dc* dest, rtgui_rect_t* rect); - /* set and get color */ - void (*set_color )(struct rtgui_dc* dc, rtgui_color_t color); - rtgui_color_t (*get_color)(struct rtgui_dc* dc); - - /* set and get font */ - void (*set_font )(struct rtgui_dc* dc, rtgui_font_t* font); - rtgui_font_t* (*get_font)(struct rtgui_dc* dc); - void (*set_textalign)(struct rtgui_dc* dc, rt_int32_t align); - rt_int32_t (*get_textalign)(struct rtgui_dc* dc); + /* set and get graphic context */ + void (*set_gc)(struct rtgui_dc* dc, struct rtgui_gc *gc); + struct rtgui_gc* (*get_gc)(struct rtgui_dc* dc); /* get dc visible */ rt_bool_t (*get_visible)(struct rtgui_dc* dc); @@ -58,27 +53,32 @@ struct rtgui_dc rt_bool_t (*fini )(struct rtgui_dc* dc); }; +#define RTGUI_DC_FC(dc) (rtgui_dc_get_gc(dc)->foreground) +#define RTGUI_DC_BC(dc) (rtgui_dc_get_gc(dc)->background) +#define RTGUI_DC_FONT(dc) (rtgui_dc_get_gc(dc)->font) +#define RTGUI_DC_TEXTALIGN(dc) (rtgui_dc_get_gc(dc)->textalign) + /* create a buffer dc */ struct rtgui_dc* rtgui_dc_buffer_create(int width, int height); rt_uint8_t* rtgui_dc_buffer_get_pixel(struct rtgui_dc* dc); +/* begin and end a drawing */ +struct rtgui_dc* rtgui_dc_begin_drawing(rtgui_widget_t* owner); +void rtgui_dc_end_drawing(struct rtgui_dc* dc); + /* destroy a dc */ void rtgui_dc_destory(struct rtgui_dc* dc); void rtgui_dc_draw_point(struct rtgui_dc* dc, int x, int y); +void rtgui_dc_draw_color_point(struct rtgui_dc* dc, int x, int y, rtgui_color_t color); void rtgui_dc_draw_vline(struct rtgui_dc* dc, int x, int y1, int y2); void rtgui_dc_draw_hline(struct rtgui_dc* dc, int x1, int x2, int y); void rtgui_dc_fill_rect (struct rtgui_dc* dc, struct rtgui_rect* rect); void rtgui_dc_blit(struct rtgui_dc* dc, struct rtgui_point* dc_point, struct rtgui_dc* dest, rtgui_rect_t* rect); -void rtgui_dc_set_color(struct rtgui_dc* dc, rtgui_color_t color); -rtgui_color_t rtgui_dc_get_color(struct rtgui_dc* dc); - -void rtgui_dc_set_font(struct rtgui_dc* dc, rtgui_font_t* font); -rtgui_font_t* rtgui_dc_get_font(struct rtgui_dc* dc); -void rtgui_dc_set_textalign(struct rtgui_dc* dc, rt_int32_t align); -rt_int32_t rtgui_dc_get_textalign(struct rtgui_dc* dc); +void rtgui_dc_set_gc(struct rtgui_dc* dc, rtgui_gc_t* gc); +rtgui_gc_t *rtgui_dc_get_gc(struct rtgui_dc* dc); rt_bool_t rtgui_dc_get_visible(struct rtgui_dc* dc); void rtgui_dc_get_rect(struct rtgui_dc*dc, rtgui_rect_t* rect); diff --git a/rtgui/include/rtgui/dc_hw.h b/rtgui/include/rtgui/dc_hw.h index 870b447aca..e40fce4472 100644 --- a/rtgui/include/rtgui/dc_hw.h +++ b/rtgui/include/rtgui/dc_hw.h @@ -33,8 +33,6 @@ struct rtgui_dc_hw /* create a hardware dc */ struct rtgui_dc* rtgui_dc_hw_create(rtgui_widget_t* owner); -struct rtgui_dc* rtgui_dc_begin_drawing(rtgui_widget_t* owner); -void rtgui_dc_end_drawing(struct rtgui_dc* dc); /* draw a hline with raw pixel data */ void rtgui_dc_hw_draw_raw_hline(struct rtgui_dc_hw* dc, rt_uint8_t* raw_ptr, int x1, int x2, int y); diff --git a/rtgui/include/rtgui/rtgui.h b/rtgui/include/rtgui/rtgui.h index c70be8c55e..d46b83b8d1 100644 --- a/rtgui/include/rtgui/rtgui.h +++ b/rtgui/include/rtgui/rtgui.h @@ -25,6 +25,7 @@ struct rtgui_event; struct rtgui_widget; struct rtgui_win; +struct rtgui_font; typedef struct rtgui_panel rtgui_panel_t; typedef struct rtgui_win rtgui_win_t; @@ -46,6 +47,21 @@ typedef struct rtgui_rect rtgui_rect_t; #define rtgui_rect_width(r) ((r).x2 - (r).x1) #define rtgui_rect_height(r) ((r).y2 - (r).y1) +typedef unsigned long rtgui_color_t; + +struct rtgui_gc +{ + /* foreground and background color */ + rtgui_color_t foreground, background; + + /* text align */ + rt_base_t textalign; + + /* font */ + struct rtgui_font* font; +}; +typedef struct rtgui_gc rtgui_gc_t; + enum RTGUI_MARGIN_STYLE { RTGUI_MARGIN_LEFT = 0x01, @@ -86,7 +102,13 @@ enum RTGUI_ALIGN RTGUI_ALIGN_BOTTOM = 0x04, RTGUI_ALIGN_CENTER_VERTICAL = 0x08, RTGUI_ALIGN_EXPAND = 0x10, - RTGUI_ALIGN_STRETCH = 0x20 + RTGUI_ALIGN_STRETCH = 0x20, +}; + +enum RTGUI_TEXTATTR +{ + RTGUI_TEXTATTR_NORMAL = 0x0000, + RTGUI_TEXTATTR_ }; enum RTGUI_ARRAW diff --git a/rtgui/include/rtgui/widgets/widget.h b/rtgui/include/rtgui/widgets/widget.h index 326595840e..6c561a0051 100644 --- a/rtgui/include/rtgui/widgets/widget.h +++ b/rtgui/include/rtgui/widgets/widget.h @@ -60,19 +60,6 @@ extern "C" { /** Check if the object is a rtgui_widget */ #define RTGUI_IS_WIDGET(obj) (RTGUI_OBJECT_CHECK_TYPE((obj), RTGUI_WIDGET_TYPE)) -struct rtgui_gc -{ - /* foreground and background color */ - rtgui_color_t foreground, background; - - /* text align */ - rt_base_t textalign; - - /* font */ - rtgui_font_t* font; -}; -typedef struct rtgui_gc rtgui_gc_t; - /* * the base widget object */ diff --git a/rtgui/widgets/about_view.c b/rtgui/widgets/about_view.c index f12baa4333..374a6ec43b 100644 --- a/rtgui/widgets/about_view.c +++ b/rtgui/widgets/about_view.c @@ -63,9 +63,9 @@ void rtgui_about_view_ondraw(struct rtgui_about_view* view) if (view->description != RT_NULL) rtgui_dc_draw_text(dc, view->description, &rect); - rect.y1 += rtgui_dc_get_font(dc)->height; + rect.y1 += rtgui_dc_get_gc(dc)->font->height; rtgui_dc_draw_hline(dc, rect.x1 + 3, rect.x2 - 3, rect.y1); - rtgui_dc_set_color(dc, white); + RTGUI_DC_FC(dc) = white; rtgui_dc_draw_hline(dc, rect.x1 + 4, rect.x2 - 2, rect.y1 + 1); rtgui_dc_end_drawing(dc);