void rtgui_xml_destroy(rtgui_xml_t* xml) { if(xml) { rtgui_free(xml->buffer); rtgui_free(xml); } }
static rt_bool_t rtgui_image_hdc_load(struct rtgui_image *image, struct rtgui_filerw *file, rt_bool_t load) { rt_uint32_t header[5]; struct rtgui_image_hdc *hdc; hdc = (struct rtgui_image_hdc *) rtgui_malloc(sizeof(struct rtgui_image_hdc)); if (hdc == RT_NULL) return RT_FALSE; hdc->hw_driver = rtgui_graphic_driver_get_default(); if (hdc->hw_driver == RT_NULL) { rtgui_free(hdc); return RT_FALSE; } rtgui_filerw_read(file, (char *)&header, 1, sizeof(header)); /* set image information */ 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; hdc->byte_per_pixel = hdc->hw_driver->bits_per_pixel / 8; hdc->pitch = image->w * hdc->byte_per_pixel; hdc->pixel_offset = rtgui_filerw_tell(file); if (load == RT_TRUE) { /* load all pixels */ hdc->pixels = rtgui_malloc(image->h * hdc->pitch); if (hdc->pixels == RT_NULL) { /* release data */ rtgui_free(hdc); return RT_FALSE; } rtgui_filerw_read(hdc->filerw, hdc->pixels, 1, image->h * hdc->pitch); rtgui_filerw_close(hdc->filerw); hdc->filerw = RT_NULL; hdc->pixel_offset = 0; } else { hdc->pixels = RT_NULL; } return RT_TRUE; }
rt_bool_t rtgui_edit_delete_line(struct rtgui_edit* edit, struct edit_line *line) { RT_ASSERT(edit != RT_NULL); RT_ASSERT(line != RT_NULL); if(edit->max_rows == 0) return RT_FALSE; if(line->prev == RT_NULL) { if(line->next == RT_NULL) { /* only one item */ edit->head = RT_NULL; edit->tail = RT_NULL; } else { /* first item */ line->next->prev = RT_NULL; edit->head = line->next; } } else { if(line->next == RT_NULL) { /* last item */ line->prev->next = RT_NULL; edit->tail = line->prev; } else { /* middle item */ line->prev->next = line->next; line->next->prev = line->prev; } } if(edit->max_rows > 0)edit->max_rows--; if(line->text) { rtgui_free(line->text); line->text = RT_NULL; } rtgui_free(line); line = RT_NULL; return RT_TRUE; }
void rtgui_freetype_font_destroy(rtgui_font_t* font) { struct rtgui_freetype_font* freetype; RT_ASSERT(font != RT_NULL); freetype = (struct rtgui_freetype_font*) font->data; RT_ASSERT(freetype != RT_NULL); rtgui_font_system_remove_font(font); FT_Done_Face (freetype->face); FT_Done_FreeType(freetype->library); rtgui_free(freetype); rtgui_free(font); }
struct rtgui_dc *rtgui_dc_buffer_create_pixformat(rt_uint8_t pixel_format, int w, int h) { struct rtgui_dc_buffer *dc; dc = (struct rtgui_dc_buffer *)rtgui_malloc(sizeof(struct rtgui_dc_buffer)); dc->parent.type = RTGUI_DC_BUFFER; dc->parent.engine = &dc_buffer_engine; dc->gc.foreground = default_foreground; dc->gc.background = default_background; dc->gc.font = rtgui_font_default(); dc->gc.textalign = RTGUI_ALIGN_LEFT | RTGUI_ALIGN_TOP; dc->pixel_format = pixel_format; dc->width = w; dc->height = h; dc->pitch = w * rtgui_color_get_bpp(pixel_format); dc->pixel = rtgui_malloc(h * dc->pitch); if (!dc->pixel) { rtgui_free(dc); return RT_NULL; } rt_memset(dc->pixel, 0, h * dc->pitch); return &(dc->parent); }
void rtgui_anim_destroy(struct rtgui_animation *anim) { /* Only free animation and timer. If you want to free the dc_buffer, * overwrite the on_finish. */ rtgui_timer_destory(anim->timer); rtgui_free(anim); }
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); /* set current position */ box->position = 0; 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; }
struct rtgui_animation* rtgui_anim_create(struct rtgui_widget *parent, int interval) { struct rtgui_animation *anim = rtgui_malloc(sizeof(*anim)); if (anim == RT_NULL) return RT_NULL; anim->timer = rtgui_timer_create(interval, RT_TIMER_FLAG_PERIODIC, _anim_timeout, anim); if (anim->timer == RT_NULL) { rtgui_free(anim); return RT_NULL; } anim->parent = parent; anim->fg_buf = RT_NULL; anim->dc_cnt = 0; anim->tick = 0; anim->tick_interval = interval; anim->max_tick = 0; /* Set default handlers. */ anim->motion = rtgui_anim_motion_linear; anim->engine = RT_NULL; anim->eng_ctx = RT_NULL; anim->on_finish = _animation_default_finish; anim->state = _ANIM_STOPPED; return anim; }
static struct rtgui_image_palette *rtgui_image_bmp_load_palette( struct rtgui_filerw *file, rt_uint32_t colorsUsed, rt_bool_t alpha) { /* Load the palette, if any */ rt_uint32_t i; struct rtgui_image_palette *palette; palette = rtgui_image_palette_create(colorsUsed); if (palette == RT_NULL) { return RT_NULL; } palette->ncolors = colorsUsed; if (alpha) { rt_uint8_t temp[4]; for (i = 0; i < colorsUsed; i++) { if (rtgui_filerw_read(file, (void *)&temp, 1, 4) != 4) { rtgui_free(palette); return RT_NULL; } palette->colors[i] = RTGUI_ARGB(temp[3], temp[2], temp[1], temp[0]); } } else { rt_uint8_t temp[3]; for (i = 0; i < colorsUsed; i++) { if (rtgui_filerw_read(file, (void *)&temp, 1, 3) != 3) { rtgui_free(palette); return RT_NULL; } palette->colors[i] = RTGUI_RGB(temp[2], temp[1], temp[0]); } } return palette; }
static void _rtgui_textbox_deconstructor(rtgui_textbox_t *textbox) { if (textbox->text != RT_NULL) { rtgui_free(textbox->text); textbox->text = RT_NULL; } if (textbox->caret_timer != RT_NULL) { rtgui_timer_destory(textbox->caret_timer); textbox->caret_timer = RT_NULL; } if (textbox->caret != RT_NULL) { rtgui_free(textbox->caret); textbox->caret = RT_NULL; } }
static void rtgui_image_png_unload(struct rtgui_image *image) { struct rtgui_image_png *png; if (image != RT_NULL) { png = (struct rtgui_image_png *) image->data; /* destroy png struct */ png_destroy_info_struct(png->png_ptr, &png->info_ptr); png_destroy_read_struct(&png->png_ptr, RT_NULL, RT_NULL); if (png->pixels != RT_NULL) rtgui_free(png->pixels); /* release data */ rtgui_free(png); } }
void rtgui_textbox_ondraw(rtgui_textbox_t *box) { /* draw button */ rtgui_rect_t rect; struct rtgui_dc *dc; rtgui_color_t fc; RT_ASSERT(box != RT_NULL); /* 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); fc = RTGUI_WIDGET_FOREGROUND(box); rtgui_rect_inflate(&rect, -1); /* fill widget rect with white color */ RTGUI_WIDGET_BACKGROUND(box) = white; rtgui_dc_fill_rect(dc, &rect); rtgui_rect_inflate(&rect, 1); /* draw border */ RTGUI_WIDGET_FOREGROUND(box) = RTGUI_RGB(123, 158, 189); rtgui_dc_draw_rect(dc, &rect); /* draw text */ RTGUI_WIDGET_FOREGROUND(box) = fc; if (box->text != RT_NULL) { rect.x1 += RTGUI_WIDGET_DEFAULT_MARGIN; /* draw single text */ if (box->flag & RTGUI_TEXTBOX_MASK) { /* draw mask char */ rt_size_t len = rt_strlen(box->text); if (len > 0) { char *text_mask = rtgui_malloc(len + 1); rt_memset(text_mask, box->mask_char, len + 1); text_mask[len] = 0; rtgui_dc_draw_text(dc, text_mask+box->first_pos, &rect); rtgui_free(text_mask); } } else { rtgui_dc_draw_text(dc, box->text+box->first_pos, &rect); } } rtgui_dc_end_drawing(dc); }
static void rtgui_image_hdc_unload(struct rtgui_image *image) { struct rtgui_image_hdc *hdc; if (image != RT_NULL) { hdc = (struct rtgui_image_hdc *) image->data; if (hdc->pixels != RT_NULL) rtgui_free(hdc->pixels); if (hdc->filerw != RT_NULL) { rtgui_filerw_close(hdc->filerw); hdc->filerw = RT_NULL; } /* release data */ rtgui_free(hdc); } }
void _rtgui_edit_deconstructor(struct rtgui_edit *edit) { if(edit->max_rows > 0) { while(edit->max_rows > 0) rtgui_edit_delete_line(edit, edit->head); edit->max_rows = 0; } if(edit->caret_timer != RT_NULL) rtgui_timer_destory(edit->caret_timer); edit->caret_timer = RT_NULL; if(edit->caret != RT_NULL) rtgui_free(edit->caret); edit->caret = RT_NULL; if(edit->update_buf != RT_NULL) rtgui_free(edit->update_buf); rtgui_dc_destory(edit->dbl_buf); }
void load_img_test(char *fname) { struct rtgui_filerw *filerw; struct rtgui_image image; struct rtgui_rect rect; struct rtgui_dc_hw *dc; struct rtgui_image_engine *img_eng; printf_syn("fun:%s, line:%d, fn:%s\n", __FUNCTION__, __LINE__, fname); filerw = rtgui_filerw_create_file(fname, "rb"); if (NULL == filerw) { printf_syn("fun:%s, line:%d, fn:%s\n", __FUNCTION__, __LINE__, fname); return; } img_eng = &rtgui_image_hdc_engine; if (RT_TRUE != img_eng->image_check(filerw)) { printf_syn("fun:%s, line:%d\n", __FUNCTION__, __LINE__); return; } if (RT_TRUE != img_eng->image_load(&image, filerw, RT_FALSE)) { printf_syn("fun:%s, line:%d\n", __FUNCTION__, __LINE__); return; } dc = (struct rtgui_dc_hw*) rtgui_malloc(sizeof(struct rtgui_dc_hw)); dc->parent.type = RTGUI_DC_HW; dc->parent.engine = &dc_hw_engine; dc->owner = NULL; dc->hw_driver = rtgui_graphic_driver_get_default(); rect.x1 = 0; rect.y1 = 0; rect.x2 = rtgui_graphic_driver_get_default()->width; rect.y2 = rtgui_graphic_driver_get_default()->height; printf_syn("fun:%s, line:%d, 0x%x\n", __FUNCTION__, __LINE__, img_eng->image_blit); img_eng->image_blit(&image, (struct rtgui_dc*)dc, &rect); rt_thread_delay(get_ticks_of_ms(5000)); printf_syn("fun:%s, line:%d, fn:%s\n", __FUNCTION__, __LINE__, fname); img_eng->image_unload(&image); printf_syn("fun:%s, line:%d, fn:%s\n", __FUNCTION__, __LINE__, fname); //rect.x1 = rtgui_filerw_close(filerw); /* unload 已close */ printf_syn("fun:%s, line:%d, ret:%d, fn:%s\n", __FUNCTION__, __LINE__, rect.x1, fname); rtgui_free(dc); return; }
static int mem_close(struct rtgui_filerw *context) { struct rtgui_filerw_mem* mem = (struct rtgui_filerw_mem*)context; if (mem != RT_NULL) { rtgui_free(mem); return 0; } return -1; }
static rt_bool_t rtgui_dc_buffer_fini(struct rtgui_dc *dc) { struct rtgui_dc_buffer *buffer = (struct rtgui_dc_buffer *)dc; if (dc->type != RTGUI_DC_BUFFER) return RT_FALSE; rtgui_free(buffer->pixel); buffer->pixel = RT_NULL; return RT_TRUE; }
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; struct rtgui_image_hdc *hdc; RT_ASSERT(image != RT_NULL || dc != RT_NULL || dst_rect != RT_NULL); /* this dc is not visible */ if (rtgui_dc_get_visible(dc) != RT_TRUE) return; hdc = (struct rtgui_image_hdc *) image->data; RT_ASSERT(hdc != RT_NULL); /* the minimum rect */ 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) { rt_uint8_t *ptr; /* get pixel pointer */ ptr = hdc->pixels; for (y = 0; y < h; y ++) { dc->engine->blit_line(dc, dst_rect->x1, dst_rect->x1 + w, dst_rect->y1 + y, ptr); ptr += hdc->pitch; } } else { 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); for (y = 0; y < h; y ++) { /* read pixel data */ if (rtgui_filerw_read(hdc->filerw, ptr, 1, hdc->pitch) != hdc->pitch) break; /* read data failed */ dc->engine->blit_line(dc, dst_rect->x1, dst_rect->x1 + w, dst_rect->y1 + y, ptr); } rtgui_free(ptr); } }
static void rtgui_image_png_unload(struct rtgui_image *image) { rt_uint8_t *pixels; if (image != RT_NULL) { pixels = (rt_uint8_t*) image->data; /* release data */ rtgui_free(pixels); } }
void rtgui_timer_destory(rtgui_timer_t* timer) { RT_ASSERT(timer != RT_NULL); /* stop timer firstly */ rtgui_timer_stop(timer); /* detach rt-thread timer */ rt_timer_detach(&(timer->timer)); rtgui_free(timer); }
rt_bool_t rtgui_edit_readin_file(struct rtgui_edit *edit, const char *filename) { int fd, num=0, read_bytes, size ,len=0; char *text ,ch; fd = open(filename, O_RDONLY, 0); if (fd < 0) { return RT_FALSE; } while(edit->max_rows > 0) rtgui_edit_delete_line(edit, edit->head); edit->max_rows = 0; size = edit->bzsize; text = rtgui_malloc(size); if(text == RT_NULL) return RT_FALSE; do { if ( (read_bytes = read(fd, &ch, 1)) > 0 ) { if(num >= size - 1) text = rt_realloc(text, rtgui_edit_alloc_len(size, num)); if(ch == 0x09) { len = edit->tabsize - num%edit->tabsize; while(len--) *(text + num++) = ' '; } else *(text + num++) = ch; if(ch == 0x0A) { rtgui_edit_append_line(edit, text); num = 0; } } else if(num > 0) { /* last line does not exist the end operator */ *(text + num) = '\0'; rtgui_edit_append_line(edit, text); } } while(read_bytes); close(fd); rtgui_free(text); rtgui_edit_ondraw(edit); return RT_TRUE; }
static int stdio_close(struct rtgui_filerw *context) { struct rtgui_filerw_stdio* stdio_filerw = (struct rtgui_filerw_stdio *)context; if (stdio_filerw) { close(stdio_filerw->fd); rtgui_free(stdio_filerw); return 0; } return -1; }
static void rtgui_freetype_font_get_metrics(struct rtgui_font *font, const char *text, rtgui_rect_t *rect) { int index = 0, len; FT_Error err = 0; rt_uint16_t w = 0, h = 0; rt_uint16_t *text_short, *text_ptr; struct rtgui_freetype_font *freetype; RT_ASSERT(font != RT_NULL); RT_ASSERT(rect != RT_NULL); freetype = (struct rtgui_freetype_font *) font->data; RT_ASSERT(freetype != RT_NULL); len = strlen(text); memset(rect, 0, sizeof(struct rtgui_rect)); /* allocate unicode buffer */ text_short = (rt_uint16_t *)rtgui_malloc((len + 1) * 2); if (text_short == RT_NULL) return ; /* out of memory */ /* convert gbk to unicode */ gbk_to_unicode(text_short, text, len); text_ptr = text_short; while (*text_ptr) { index = FT_Get_Char_Index(freetype->face, *text_ptr); err = FT_Load_Glyph(freetype->face, index, FT_LOAD_DEFAULT); if (err == 0) { w += freetype->face->glyph->bitmap.width; if (freetype->face->glyph->bitmap.rows > h) { h = freetype->face->glyph->bitmap.rows; } } text_ptr ++; } rect->x1 = 0; rect->y1 = 0; rect->x2 = w; rect->y2 = h; /* release unicode buffer */ rtgui_free(text_short); }
static void _rtgui_notebook_destructor(struct rtgui_notebook *notebook) { int index; if (notebook->childs != RT_NULL) { for (index = 0; index < notebook->count; index ++) { rtgui_widget_destroy(notebook->childs[index].widget); rt_free(notebook->childs[index].title); } rtgui_free(notebook->childs); } }
/** * @brief Destroys the object: it first sets the weak-pointers to RT_NULL, emits the "destroyed" signal, and then * queues the object in the list of objects to free. Thus, the destructors will only be called at the beginning of the * next main loop iteration (from the destructor of the more derived class to the destructor of the ultimate base class). * @param object the object to destroy * @warning You should not assume that this function will call directly the destructors of the object! */ void rtgui_object_destroy(rtgui_object_t *object) { if (!object || object->is_static == RT_TRUE) return; #ifdef RTGUI_OBJECT_TRACE obj_info.objs_number --; obj_info.allocated_size -= object->type->size; #endif /* call destructor */ RT_ASSERT(object->type != RT_NULL); rtgui_type_destructors_call(object->type, object); /* release object */ rtgui_free(object); }
void rtgui_thread_deregister(rt_thread_t tid) { struct rtgui_thread* thread; /* find rtgui_thread */ thread = (struct rtgui_thread*) (tid->user_data); if (thread != RT_NULL) { /* remove rtgui_thread */ tid->user_data = 0; /* free rtgui_thread */ rtgui_free(thread); } }
static void rtgui_freetype_font_draw_text(struct rtgui_font* font, struct rtgui_dc* dc, const char* text, rt_ubase_t len, struct rtgui_rect* rect) { int index = 0; FT_Error err = 0; rt_uint16_t *text_short, *text_ptr; struct rtgui_freetype_font* freetype; RT_ASSERT(font != RT_NULL); freetype = (struct rtgui_freetype_font*) font->data; RT_ASSERT(freetype != RT_NULL); /* allocate unicode buffer */ text_short = (rt_uint16_t*)rtgui_malloc((len + 1)* 2); if (text_short == RT_NULL) return ; /* out of memory */ /* convert gbk to unicode */ gbk_to_unicode(text_short, text, len); text_ptr = text_short; while (*text_ptr) { index = FT_Get_Char_Index(freetype->face, *text_ptr); err = FT_Load_Glyph(freetype->face, index, FT_LOAD_DEFAULT|FT_LOAD_RENDER); if (err == 0) { int rows, x; rt_uint8_t* ptr; /* render font */ ptr = (rt_uint8_t*)freetype->face->glyph->bitmap.buffer; for (rows = 0; rows < freetype->face->glyph->bitmap.rows; rows ++) for (x = 0; x < freetype->face->glyph->bitmap.width; x++) { if (*ptr > 0) rtgui_dc_draw_color_point(dc, rect->x1 + x, rect->y1 + rows, RTGUI_RGB(0xff - *ptr, 0xff - *ptr, 0xff - *ptr)); ptr ++; } } text_ptr ++; rect->x1 += freetype->face->glyph->bitmap.width; } /* release unicode buffer */ rtgui_free(text_short); }
void rtgui_notebook_remove(struct rtgui_notebook* notebook, rt_uint16_t index) { struct rtgui_notebook_tab tab; rt_bool_t need_update = RT_FALSE; RT_ASSERT(notebook != RT_NULL); if (index < notebook->count) { if (notebook->count == 1) { tab = notebook->childs[0]; rtgui_free(notebook->childs); notebook->childs = RT_NULL; notebook->count = 0; } else { if (notebook->current == index) need_update = RT_TRUE; tab = notebook->childs[index]; for (;index < notebook->count - 1; index++) { notebook->childs[index] = notebook->childs[index + 1]; } notebook->count -= 1; notebook->childs = (struct rtgui_notebook_tab*) rtgui_realloc(notebook->childs, sizeof(struct rtgui_notebook_tab) * notebook->count); } rt_free(tab.title); if (need_update) { if (notebook->current > notebook->count - 1) notebook->current = notebook->count - 1; rtgui_widget_hide(tab.widget); rtgui_widget_show(notebook->childs[notebook->current].widget); rtgui_widget_update(RTGUI_WIDGET(notebook)); rtgui_widget_set_parent(tab.widget, RT_NULL); } } }
static void _graphic_driver_vmode_init(void) { if (_vfb_driver.width != _driver.width || _vfb_driver.height != _driver.height) { if (_vfb_driver.framebuffer != RT_NULL) rtgui_free((void*)_vfb_driver.framebuffer); _vfb_driver.device = RT_NULL; _vfb_driver.pixel_format = RTGUI_VFB_PIXEL_FMT; _vfb_driver.bits_per_pixel = rtgui_color_get_bits(RTGUI_VFB_PIXEL_FMT); _vfb_driver.width = _driver.width; _vfb_driver.height = _driver.height; _vfb_driver.pitch = _driver.width * _UI_BITBYTES(_vfb_driver.bits_per_pixel); _vfb_driver.framebuffer = rtgui_malloc(_vfb_driver.height * _vfb_driver.pitch); _vfb_driver.ext_ops = RT_NULL; _vfb_driver.ops = rtgui_framebuffer_get_ops(_vfb_driver.pixel_format); } }
void rtgui_timer_destory(rtgui_timer_t *timer) { RT_ASSERT(timer != RT_NULL); /* stop timer firstly */ rtgui_timer_stop(timer); if (timer->pending_cnt != 0 && timer->app->ref_count != 0) { timer->state = RTGUI_TIMER_ST_DESTROY_PENDING; } else { /* detach rt-thread timer */ rt_timer_detach(&(timer->timer)); rtgui_free(timer); } }