void _draw_default(struct rtgui_object *object, rtgui_event_t* event) { struct rtgui_widget *widget = RTGUI_WIDGET(object); struct rtgui_dc* dc; rtgui_rect_t rect; /* 因为用的是demo container,上面本身有一部分控件,所以在绘图时先要让demo container先绘图 */ rtgui_container_event_handler(object, event); /* 获得控件所属的DC */ dc = rtgui_dc_begin_drawing(widget); if (dc == RT_NULL) /* 如果不能正常获得DC,返回(如果控件或父控件是隐藏状态,DC是获取不成功的) */ return; /* 获得demo container允许绘图的区域 */ demo_view_get_logic_rect(RTGUI_CONTAINER(widget), &rect); /* 擦除所有 */ RTGUI_WIDGET_BACKGROUND(widget) = default_background; rtgui_dc_fill_rect(dc, &rect); /* 显示提示 */ rtgui_dc_draw_text(dc, "按任意键开始/停止测试...", &rect); /* 绘图完成 */ rtgui_dc_end_drawing(dc); }
/* * view的事件处理函数 */ static rt_bool_t dc_buffer_event_handler(struct rtgui_object* object, rtgui_event_t *event) { struct rtgui_widget *widget = RTGUI_WIDGET(object); /* 仅对PAINT事件进行处理 */ if (event->type == RTGUI_EVENT_PAINT) { struct rtgui_dc* dc; rtgui_rect_t rect; /* * 因为用的是demo view,上面本身有一部分控件,所以在绘图时先要让demo view * 先绘图 */ rtgui_container_event_handler(object, event); /* 获得控件所属的DC */ dc = rtgui_dc_begin_drawing(widget); /* 如果不能正常获得DC,返回(如果控件或父控件是隐藏状态,DC是获取不成功的) */ if (dc == RT_NULL) return RT_FALSE; /* 获得demo view允许绘图的区域 */ demo_view_get_logic_rect(RTGUI_CONTAINER(widget), &rect); rect.x1 += 10; rect.y1 += 10; rtgui_dc_blit(dc_buffer, NULL, dc, &rect); /* 绘图完成 */ rtgui_dc_end_drawing(dc); } else { /* 其他事件,调用默认的事件处理函数 */ return rtgui_container_event_handler(object, event); } return RT_FALSE; }
static rt_bool_t picture_view_event_handler(rtgui_object_t *object, rtgui_event_t *event) { if (event->type == RTGUI_EVENT_PAINT) { struct rtgui_dc* dc; struct rtgui_rect rect; struct rtgui_image* image = RT_NULL; char fn[32]; dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(object)); if (dc == RT_NULL) return RT_FALSE; rtgui_widget_get_rect(RTGUI_WIDGET(object), &rect); /* open image */ rt_snprintf(fn, sizeof(fn), "%s/%s", PICTURE_DIR, current_fn); rt_kprintf("pic fn: %s\n", fn); if (strstr(fn, ".hdc") != RT_NULL || strstr(fn, ".HDC") != RT_NULL) { image = rtgui_image_create_from_file("hdc", fn, RT_FALSE); } else if (strstr(fn, ".bmp") != RT_NULL || strstr(fn, ".BMP") != RT_NULL) { image = rtgui_image_create_from_file("bmp", fn, RT_FALSE); } if (image != RT_NULL) { /* blit image */ rtgui_image_blit(image, dc, &rect); /* destroy image */ rtgui_image_destroy(image); } else { rtgui_dc_fill_rect(dc, &rect); rtgui_dc_draw_text(dc, "没有文件被打开", &rect); } rtgui_dc_end_drawing(dc); return RT_FALSE; } return rtgui_container_event_handler(object, event); }
rt_bool_t benchmark_event_handler(struct rtgui_object *object, rtgui_event_t *event) { if (event->type == RTGUI_EVENT_PAINT) { _draw_default(object, event); } else if (event->type == RTGUI_EVENT_KBD) { struct rtgui_event_kbd *kbd = (struct rtgui_event_kbd*)event; if (kbd->key == RTGUIK_LEFT || kbd->key == RTGUIK_RIGHT) return RT_FALSE; if (RTGUI_KBD_IS_UP(kbd)) { if (running) { /* stop */ rtgui_app_set_onidle(RT_NULL); _draw_default(object, event); } else { /* run */ rtgui_app_set_onidle(_onidle); } running = !running; } return RT_TRUE; } else { /* 调用默认的事件处理函数 */ return rtgui_container_event_handler(object, event); } return RT_FALSE; }
rt_bool_t rtgui_edit_event_handler(pvoid wdt, rtgui_event_t* event) { rtgui_widget_t *widget = RTGUI_WIDGET(wdt); struct rtgui_edit* edit = RTGUI_EDIT(wdt); switch(event->type) { case RTGUI_EVENT_PAINT: if(widget->on_draw != RT_NULL) widget->on_draw(widget, event); else rtgui_edit_ondraw(edit); break; case RTGUI_EVENT_MOUSE_BUTTON: if(widget->on_mouseclick != RT_NULL) widget->on_mouseclick(widget, event); else rtgui_edit_onmouse(edit, (struct rtgui_event_mouse*)event); rtgui_container_dispatch_mouse_event(edit, (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_edit_onkey(widget, event); return RT_TRUE; default: return rtgui_container_event_handler(widget, event); } return RT_FALSE; }
rt_bool_t demo_bitmap_showbox(struct rtgui_object* object, struct rtgui_event* event) { rtgui_container_t *container; rtgui_widget_t *widget; RT_ASSERT(object != RT_NULL); container = RTGUI_CONTAINER(object); widget = RTGUI_WIDGET(object); if(event->type == RTGUI_EVENT_PAINT) { int w, h; rtgui_rect_t rect; struct rtgui_dc *dc; struct rtgui_image *image = bmpdt.showimg; /* 如果从其他标签切换到当前标签, image应该是RT_NULL, 重置它 */ if(image == RT_NULL && bmpdt.image != RT_NULL) { image = bmpdt.image; bmpdt.scale = 1.0f; bmpdt.angle = 0.0f; rtgui_widget_get_rect(RTGUI_WIDGET(bmpdt.showbox), &bmpdt.lastrect); rtgui_rect_inflate(&bmpdt.lastrect, -RTGUI_WIDGET_BORDER(bmpdt.showbox)); } dc = rtgui_dc_begin_drawing(widget); if (dc == RT_NULL) return RT_FALSE; rtgui_widget_get_rect(widget, &rect); /* 在绘制边框后, 再将rect缩小填充背景, 可以降低闪烁现象 */ rtgui_dc_draw_border(dc, &rect, RTGUI_WIDGET_BORDER_STYLE(widget)); rtgui_rect_inflate(&rect, -RTGUI_WIDGET_BORDER(widget)); w = rtgui_rect_width(bmpdt.lastrect); h = rtgui_rect_height(bmpdt.lastrect); if(w > rtgui_rect_width(rect)) w = rtgui_rect_width(rect); if(h > rtgui_rect_height(rect)) h = rtgui_rect_height(rect); /* fill container with background */ /* * 参数lastrect会记录上一次绘图所用区域 * 每次重绘时,只需与lastrect比较,即可得知那些背景区域需要刷新 * 例如当放大图片时,lastrect比当前绘图区小,所有无需更新背景区, * 当缩小图片时, 也仅需要更新绘图区比lastrect大的区域. */ if(image != RT_NULL) { /* 减少不必要的绘图 */ rtgui_rect_t rc; if(w > image->w) { rc.x1 = image->w; rc.y1 = bmpdt.lastrect.y1; rc.x2 = bmpdt.lastrect.x2; rc.y2 = (h > image->h) ? image->h : bmpdt.lastrect.y2; rtgui_dc_fill_rect(dc, &rc); } if(h > image->h) { rc.x1 = bmpdt.lastrect.x1; rc.y1 = image->h; rc.x2 = bmpdt.lastrect.x2; rc.y2 = bmpdt.lastrect.y2; rtgui_dc_fill_rect(dc, &rc); } } else rtgui_dc_fill_rect(dc, &bmpdt.lastrect); /* 将图像数据blit到画布上 */ if (image != RT_NULL) { int value; rtgui_image_blit(image, dc, &rect); bmpdt.lastrect.x1 = bmpdt.lastrect.y1 = RTGUI_WIDGET_BORDER(bmpdt.showbox); if(image->w > rtgui_rect_width(rect)) value = rtgui_rect_width(rect); else value = image->w; bmpdt.lastrect.x2 = bmpdt.lastrect.x1 + value; if(image->h > rtgui_rect_height(rect)) value = rtgui_rect_height(rect); else value = image->h; bmpdt.lastrect.y2 = bmpdt.lastrect.y1 + value; } rtgui_dc_end_drawing(dc); return RT_FALSE; } return rtgui_container_event_handler(object, event); }
rt_bool_t rtgui_fileview_event_handler(pvoid wdt, rtgui_event_t* event) { rtgui_widget_t *widget = RTGUI_WIDGET(wdt); rtgui_fileview_t* fview = RTGUI_FILEVIEW(wdt); switch(event->type) { case RTGUI_EVENT_PAINT: rtgui_fileview_ondraw(fview); return RT_FALSE; case RTGUI_EVENT_MOUSE_BUTTON: { rtgui_rect_t rect; struct rtgui_event_mouse* emouse; emouse = (struct rtgui_event_mouse*)event; rtgui_widget_focus(fview); /* get physical extent information */ rtgui_widget_get_rect(fview, &rect); rtgui_widget_rect_to_device(fview, &rect); if(fview->sbar && !RTGUI_WIDGET_IS_HIDE(fview->sbar)) rect.x2 -= RC_W(fview->sbar->parent.extent); if((rtgui_rect_contains_point(&rect, emouse->x, emouse->y) == RT_EOK) && fview->item_count>0) { rt_uint16_t i; /* set focus */ rtgui_widget_focus(fview); i = (emouse->y - rect.y1) / (2 + RTGUI_SEL_H); if((i < fview->item_count) && (i < fview->item_per_page)) { if(emouse->button & RTGUI_MOUSE_BUTTON_DOWN) { fview->old_item = fview->now_item; fview->now_item = fview->first_item + i; rtgui_fileview_update_current(fview); } else if(emouse->button & RTGUI_MOUSE_BUTTON_UP) { if(fview->now_item==fview->old_item) return RT_FALSE; rtgui_fileview_update_current(fview); } if(fview->sbar && !RTGUI_WIDGET_IS_HIDE(fview)) { if(!RTGUI_WIDGET_IS_HIDE(fview->sbar)) rtgui_scrollbar_set_value(fview->sbar,fview->first_item); } } } rtgui_container_dispatch_mouse_event(fview, emouse); return RT_TRUE; } case RTGUI_EVENT_KBD: { struct rtgui_event_kbd *ekbd = (struct rtgui_event_kbd*)event; if(RTGUI_KBD_IS_DOWN(ekbd)) { switch(ekbd->key) { case RTGUIK_UP: if(fview->now_item > 0) { fview->old_item = fview->now_item; fview->now_item --; if(fview->now_item < fview->first_item) { /* turn up page */ fview->first_item = fview->now_item; rtgui_fileview_ondraw(fview); } else { /* current page */ rtgui_fileview_update_current(fview); } if(fview->sbar && !RTGUI_WIDGET_IS_HIDE(fview)) { if(!RTGUI_WIDGET_IS_HIDE(fview->sbar)) rtgui_scrollbar_set_value(fview->sbar,fview->first_item); } } return RT_TRUE; case RTGUIK_DOWN: if(fview->now_item < fview->item_count-1) { fview->old_item = fview->now_item; fview->now_item ++; if(fview->now_item >= fview->first_item+fview->item_per_page) { /* turn down page */ fview->first_item++; rtgui_fileview_ondraw(fview); } else { /* in current page */ rtgui_fileview_update_current(fview); } if(fview->sbar && !RTGUI_WIDGET_IS_HIDE(fview)) { if(!RTGUI_WIDGET_IS_HIDE(fview->sbar)) rtgui_scrollbar_set_value(fview->sbar,fview->first_item); } } return RT_TRUE; case RTGUIK_LEFT: if(fview->item_count==0)return RT_FALSE; fview->old_item = fview->now_item; fview->now_item -= fview->item_per_page; if(fview->now_item < 0) fview->now_item = 0; if(fview->now_item < fview->first_item) { fview->first_item = fview->now_item; rtgui_fileview_ondraw(fview); } else { rtgui_fileview_update_current(fview); } if(fview->sbar && !RTGUI_WIDGET_IS_HIDE(fview)) { if(!RTGUI_WIDGET_IS_HIDE(fview->sbar)) rtgui_scrollbar_set_value(fview->sbar,fview->first_item); } return RT_TRUE; case RTGUIK_RIGHT: if(fview->item_count==0)return RT_FALSE; fview->old_item = fview->now_item; fview->now_item += fview->item_per_page; if(fview->now_item > fview->item_count-1) fview->now_item = fview->item_count-1; if(fview->now_item >= fview->first_item+fview->item_per_page) { fview->first_item += fview->item_per_page; if(fview->first_item >fview->item_count-fview->item_per_page) { fview->first_item = fview->item_count-fview->item_per_page; fview->now_item = fview->first_item; } rtgui_fileview_ondraw(fview); } else { rtgui_fileview_update_current(fview); } if(fview->sbar && !RTGUI_WIDGET_IS_HIDE(fview)) { if(!RTGUI_WIDGET_IS_HIDE(fview->sbar)) rtgui_scrollbar_set_value(fview->sbar,fview->first_item); } return RT_TRUE; case RTGUIK_RETURN: rtgui_fileview_on_enter(fview, event); return RT_TRUE; case RTGUIK_BACKSPACE: rtgui_fileview_goto_topfolder(fview); return RT_TRUE; default: break; } } } return RT_FALSE; default: return rtgui_container_event_handler(widget, event); } }
/* * container的事件处理函数 */ rt_bool_t instrument_panel_event_handler(struct rtgui_object *object, rtgui_event_t *event) { struct rtgui_widget *widget = RTGUI_WIDGET(object); char ac[4]; int i; int x0 = 120; int y0 = 170; int x, y; int default_color; /* 仅对PAINT事件进行处理 */ if (event->type == RTGUI_EVENT_PAINT) { struct rtgui_dc *dc; rtgui_rect_t rect; const int arrowx[] = {120 + 75, 120 + 75, 120 + 85}; const int arrowy[] = {170 - 5, 170 + 5, 170}; /* * 因为用的是demo container,上面本身有一部分控件,所以在绘图时先要让demo container * 先绘图 */ rtgui_container_event_handler(RTGUI_OBJECT(widget), event); /************************************************************************/ /* 下面的是DC的操作 */ /************************************************************************/ /* 获得控件所属的DC */ dc = rtgui_dc_begin_drawing(widget); /* 如果不能正常获得DC,返回(如果控件或父控件是隐藏状态,DC是获取不成功的) */ if (dc == RT_NULL) return RT_FALSE; /* 获得demo container允许绘图的区域 */ demo_view_get_rect(RTGUI_CONTAINER(widget), &rect); RTGUI_DC_TEXTALIGN(dc) = RTGUI_ALIGN_BOTTOM | RTGUI_ALIGN_CENTER_HORIZONTAL; /* 显示GUI的版本信息 */ #ifdef RTGUI_USING_SMALL_SIZE rtgui_dc_draw_text(dc, "RT-Thread/GUI小型版本", &rect); #else rtgui_dc_draw_text(dc, "RT-Thread/GUI标准版本", &rect); #endif RTGUI_DC_TEXTALIGN(dc) = RTGUI_ALIGN_CENTER_VERTICAL | RTGUI_ALIGN_CENTER_HORIZONTAL; RTGUI_DC_FC(dc) = blue; rect.y2 = 270; rtgui_dc_draw_text(dc, "rtgui-panel", &rect); for (i = 0; i < 6; i++) { rtgui_dc_draw_arc(dc, x0, y0, 117 - i, 150, 30); } RTGUI_DC_FC(dc) = black; RTGUI_DC_TEXTALIGN(dc) = RTGUI_ALIGN_LEFT; for (i = 0; i <= 23; i++) { if (i < 12) { x = x0 + 105 * cos((150 + i * 10) * 3.1415926 / 180); y = y0 + 105 * sin((150 + i * 10) * 3.1415926 / 180); rect.x1 = x; rect.y1 = y; rect.x2 = rect.x1 + 12 * 3; rect.y2 = rect.y1 + 12; rt_sprintf(ac, "%d", 10 * i); rtgui_dc_draw_text(dc, ac, &rect); } else { RTGUI_DC_TEXTALIGN(dc) = RTGUI_ALIGN_RIGHT; x = x0 + 105 * cos((160 + i * 10) * 3.1415926 / 180); y = y0 + 105 * sin((160 + i * 10) * 3.1415926 / 180); rect.x1 = x - 12 * 3; rect.y1 = y; rect.x2 = rect.x1 + 12 * 3; rect.y2 = rect.y1 + 12; rt_sprintf(ac, "%d", 10 * i); rtgui_dc_draw_text(dc, ac, &rect); } x = x0 + 107 * cos((150 + i * 10) * 3.1415926 / 180); y = y0 + 107 * sin((150 + i * 10) * 3.1415926 / 180); rtgui_dc_fill_circle(dc, x, y, 3); } RTGUI_DC_FC(dc) = RTGUI_RGB(166, 0, 166); rtgui_dc_fill_circle(dc, x0, y0, 3); RTGUI_DC_FC(dc) = RTGUI_RGB(120, 141, 30); rtgui_dc_draw_circle(dc, x0, y0, 5); default_color = RTGUI_DC_BC(dc); RTGUI_DC_BC(dc) = red; rect.x1 = x0 + 7; rect.y1 = y0 - 1; rect.x2 = x0 + 75; rect.y2 = y0 + 1; rtgui_dc_fill_rect(dc, &rect); RTGUI_DC_BC(dc) = default_color; rtgui_dc_fill_polygon(dc, arrowx, arrowy, 3); /* 绘图完成 */ rtgui_dc_end_drawing(dc); } else { /* 其他事件,调用默认的事件处理函数 */ return rtgui_container_event_handler(RTGUI_OBJECT(widget), event); } return RT_FALSE; }
rt_bool_t rtgui_list_view_event_handler(struct rtgui_object *widget, struct rtgui_event *event) { struct rtgui_list_view *view = RT_NULL; view = RTGUI_LIST_VIEW(widget); switch (event->type) { case RTGUI_EVENT_PAINT: rtgui_list_view_ondraw(view); return RT_FALSE; case RTGUI_EVENT_RESIZE: { struct rtgui_event_resize *resize; resize = (struct rtgui_event_resize *)event; /* recalculate page items */ view->page_items = resize->h / (2 + rtgui_theme_get_selected_height()); } break; case RTGUI_EVENT_MOUSE_BUTTON: { struct rtgui_event_mouse *emouse; emouse = (struct rtgui_event_mouse *)event; return rtgui_list_view_onmouse(view, emouse); } case RTGUI_EVENT_KBD: { struct rtgui_event_kbd *ekbd = (struct rtgui_event_kbd *)event; if (ekbd->type == RTGUI_KEYDOWN) { rt_uint16_t old_item; if (view->current_item == -1) { /* set a initial item */ if ((view->items_count > 0) && (ekbd->key == RTGUIK_UP || ekbd->key == RTGUIK_DOWN)) { view->current_item = 0; if (view->flag == RTGUI_LIST_VIEW_LIST) rtgui_list_view_update_list(view, -1); else rtgui_list_view_update_icon(view, -1); break; } else return RT_FALSE; } old_item = view->current_item; switch (ekbd->key) { case RTGUIK_LEFT: if (view->flag == RTGUI_LIST_VIEW_LIST) { if (view->current_item - view->page_items >= 0) view->current_item -= view->page_items; rtgui_list_view_update_list(view, old_item); } else if (view->flag == RTGUI_LIST_VIEW_ICON) { if (view->current_item > 0) view->current_item --; rtgui_list_view_update_icon(view, old_item); } return RT_FALSE; case RTGUIK_UP: if (view->flag == RTGUI_LIST_VIEW_LIST) { if (view->current_item > 0) view->current_item --; rtgui_list_view_update_list(view, old_item); } else if (view->flag == RTGUI_LIST_VIEW_ICON) { if (view->current_item >= view->col_items) view->current_item -= view->col_items; else view->current_item = 0; rtgui_list_view_update_icon(view, old_item); } return RT_FALSE; case RTGUIK_RIGHT: if (view->flag == RTGUI_LIST_VIEW_LIST) { if (view->current_item + view->page_items < view->items_count - 1) view->current_item += view->page_items; rtgui_list_view_update_list(view, old_item); } else if (view->flag == RTGUI_LIST_VIEW_ICON) { if (view->current_item < view->items_count - 1) view->current_item ++; rtgui_list_view_update_icon(view, old_item); } return RT_FALSE; case RTGUIK_DOWN: if (view->flag == RTGUI_LIST_VIEW_LIST) { if (view->current_item < view->items_count - 1) view->current_item ++; rtgui_list_view_update_list(view, old_item); } else if (view->flag == RTGUI_LIST_VIEW_ICON) { if (view->current_item + view->col_items <= (view->items_count - 1)) view->current_item += view->col_items; else view->current_item = view->items_count - 1; rtgui_list_view_update_icon(view, old_item); } return RT_FALSE; case RTGUIK_RETURN: if (view->items[view->current_item].action != RT_NULL) { view->items[view->current_item].action(RTGUI_WIDGET(view), view->items[view->current_item].parameter); } return RT_FALSE; default: break; } } } return RT_FALSE; default: /* use view event handler */ return rtgui_container_event_handler(widget, event); } return RT_FALSE; }
rt_bool_t rtgui_filelist_view_event_handler(struct rtgui_object *object, struct rtgui_event *event) { struct rtgui_widget *widget; struct rtgui_filelist_view *view = RT_NULL; widget = RTGUI_WIDGET(object); view = RTGUI_FILELIST_VIEW(widget); switch (event->type) { case RTGUI_EVENT_PAINT: rtgui_filelist_view_ondraw(view); return RT_FALSE; case RTGUI_EVENT_RESIZE: { struct rtgui_event_resize *resize; resize = (struct rtgui_event_resize *)event; /* recalculate page items */ if (file_image != RT_NULL) view->page_items = resize->h / (1 + rtgui_theme_get_selected_height()); else view->page_items = resize->h / (2 + 14); if (view->page_items == 0) view->page_items = 1; } break; case RTGUI_EVENT_MOUSE_BUTTON: { rtgui_rect_t rect; struct rtgui_event_mouse *emouse; emouse = (struct rtgui_event_mouse *)event; /* calculate selected item */ /* get physical extent information */ rtgui_widget_get_rect(widget, &rect); rtgui_widget_rect_to_device(widget, &rect); if (rtgui_rect_contains_point(&rect, emouse->x, emouse->y) == RT_EOK) { rt_uint16_t index; rt_uint16_t current_page; rt_uint16_t old_item; index = (emouse->y - rect.y1) / (2 + rtgui_theme_get_selected_height()); /* get current page */ current_page = view->current_item / view->page_items; old_item = view->current_item; if (index + current_page * view->page_items < view->items_count) { /* set selected item */ view->current_item = index + current_page * view->page_items; if (emouse->button & RTGUI_MOUSE_BUTTON_DOWN) { rtgui_filelist_view_update_current(view, old_item); } else { /* up event */ rtgui_filelist_view_onenturn(view); } } return RT_TRUE; } } break; case RTGUI_EVENT_KBD: { struct rtgui_event_kbd *ekbd = (struct rtgui_event_kbd *)event; if (ekbd->type == RTGUI_KEYDOWN) { rt_uint16_t old_item; old_item = view->current_item; switch (ekbd->key) { case RTGUIK_UP: if (view->current_item > 0) view->current_item --; rtgui_filelist_view_update_current(view, old_item); return RT_TRUE; case RTGUIK_DOWN: if (view->current_item < view->items_count - 1) view->current_item ++; rtgui_filelist_view_update_current(view, old_item); return RT_TRUE; case RTGUIK_LEFT: if (view->current_item - view->page_items >= 0) view->current_item -= view->page_items; rtgui_filelist_view_update_current(view, old_item); return RT_TRUE; case RTGUIK_RIGHT: if (view->current_item + view->page_items < view->items_count - 1) view->current_item += view->page_items; rtgui_filelist_view_update_current(view, old_item); return RT_TRUE; case RTGUIK_RETURN: rtgui_filelist_view_onenturn(view); return RT_TRUE; default: break; } } } return RT_FALSE; } /* use view event handler */ return rtgui_container_event_handler(object, event); }