1
0

demo_view_image.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include "demo_view.h"
  2. #include <rtgui/widgets/button.h>
  3. #include <rtgui/widgets/filelist_view.h>
  4. static rtgui_image_t* image = RT_NULL;
  5. static rtgui_view_t* _view = RT_NULL;
  6. static void open_btn_onbutton(rtgui_widget_t* widget, struct rtgui_event* event)
  7. {
  8. /* create a file list view */
  9. rtgui_filelist_view_t *view;
  10. rtgui_workbench_t *workbench;
  11. rtgui_rect_t rect;
  12. workbench = RTGUI_WORKBENCH(rtgui_widget_get_toplevel(widget));
  13. rtgui_widget_get_rect(RTGUI_WIDGET(workbench), &rect);
  14. #ifdef _WIN32
  15. view = rtgui_filelist_view_create(workbench, "d:\\", "*.*", &rect);
  16. #else
  17. view = rtgui_filelist_view_create(workbench, "/", "*.*", &rect);
  18. #endif
  19. if (rtgui_view_show(RTGUI_VIEW(view), RT_TRUE) == RTGUI_MODAL_OK)
  20. {
  21. char path[32], image_type[8];
  22. /* set label */
  23. rtgui_filelist_get_fullpath(view, path, sizeof(path));
  24. if (image != RT_NULL) rtgui_image_destroy(image);
  25. rt_memset(image_type, 0, sizeof(image_type));
  26. /* 获得图像的类型 */
  27. if (rt_strstr(path, ".png") != RT_NULL ||
  28. rt_strstr(path, ".PNG") != RT_NULL)
  29. strcat(image_type, "png");
  30. if (rt_strstr(path, ".jpg") != RT_NULL ||
  31. rt_strstr(path, ".JPG") != RT_NULL)
  32. strcat(image_type, "jpeg");
  33. if (rt_strstr(path, ".hdc") != RT_NULL ||
  34. rt_strstr(path, ".HDC") != RT_NULL)
  35. strcat(image_type, "hdc");
  36. if (image_type[0] != '\0')
  37. image = rtgui_image_create_from_file(image_type, path, RT_TRUE);
  38. }
  39. /* 删除 文件列表 视图 */
  40. rtgui_view_destroy(RTGUI_VIEW(view));
  41. rtgui_view_show(_view, RT_FALSE);
  42. }
  43. static rt_bool_t demo_view_event_handler(rtgui_widget_t* widget, rtgui_event_t *event)
  44. {
  45. rt_bool_t result;
  46. /* 用默认的事件处理函数 */
  47. result = rtgui_view_event_handler(widget, event);
  48. if (event->type == RTGUI_EVENT_PAINT)
  49. {
  50. struct rtgui_dc* dc;
  51. rtgui_rect_t rect;
  52. /* 获得控件所属的DC */
  53. dc = rtgui_dc_begin_drawing(widget);
  54. if (dc == RT_NULL) /* 如果不能正常获得DC,返回(如果控件或父控件是隐藏状态,DC是获取不成功的) */
  55. return RT_FALSE;
  56. /* 获得demo view允许绘图的区域 */
  57. demo_view_get_rect(RTGUI_VIEW(widget), &rect);
  58. /* 获得图像显示区域 */
  59. rect.x1 += 5; rect.x2 -= 5;
  60. rect.y1 += 30;
  61. if (image != RT_NULL)
  62. rtgui_image_blit(image, dc, &rect);
  63. /* 绘图完成 */
  64. rtgui_dc_end_drawing(dc);
  65. }
  66. return result;
  67. }
  68. rtgui_view_t* demo_view_image(rtgui_workbench_t* workbench)
  69. {
  70. rtgui_rect_t rect;
  71. rtgui_button_t* open_btn;
  72. _view = demo_view(workbench, "图像演示");
  73. if (_view != RT_NULL)
  74. rtgui_widget_set_event_handler(RTGUI_WIDGET(_view), demo_view_event_handler);
  75. demo_view_get_rect(_view, &rect);
  76. rect.x1 += 5; rect.x2 = rect.x1 + 120;
  77. rect.y2 = rect.y1 + 20;
  78. open_btn = rtgui_button_create("打开图像文件");
  79. rtgui_container_add_child(RTGUI_CONTAINER(_view), RTGUI_WIDGET(open_btn));
  80. rtgui_widget_set_rect(RTGUI_WIDGET(open_btn), &rect);
  81. rtgui_button_set_onbutton(open_btn, open_btn_onbutton);
  82. return _view;
  83. }