demo_view_image.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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) strcat(image_type, "png");
  28. if (rt_strstr(path, ".jpg") != RT_NULL) strcat(image_type, "jpeg");
  29. if (rt_strstr(path, ".hdc") != RT_NULL) strcat(image_type, "hdc");
  30. if (image_type[0] != '\0')
  31. image = rtgui_image_create_from_file(image_type, path, RT_TRUE);
  32. }
  33. /* 删除 文件列表 视图 */
  34. rtgui_view_destroy(RTGUI_VIEW(view));
  35. rtgui_view_show(_view, RT_FALSE);
  36. }
  37. static rt_bool_t demo_view_event_handler(rtgui_widget_t* widget, rtgui_event_t *event)
  38. {
  39. rt_bool_t result;
  40. /* 用默认的事件处理函数 */
  41. result = rtgui_view_event_handler(widget, event);
  42. if (event->type == RTGUI_EVENT_PAINT)
  43. {
  44. struct rtgui_dc* dc;
  45. rtgui_rect_t rect;
  46. /* 获得控件所属的DC */
  47. dc = rtgui_dc_begin_drawing(widget);
  48. if (dc == RT_NULL) /* 如果不能正常获得DC,返回(如果控件或父控件是隐藏状态,DC是获取不成功的) */
  49. return RT_FALSE;
  50. /* 获得demo view允许绘图的区域 */
  51. demo_view_get_rect(RTGUI_VIEW(widget), &rect);
  52. /* 获得图像显示区域 */
  53. rect.x1 += 5; rect.x2 -= 5;
  54. rect.y1 += 30;
  55. if (image != RT_NULL)
  56. rtgui_image_blit(image, dc, &rect);
  57. /* 绘图完成 */
  58. rtgui_dc_end_drawing(dc);
  59. }
  60. return result;
  61. }
  62. rtgui_view_t* demo_view_image(rtgui_workbench_t* workbench)
  63. {
  64. rtgui_rect_t rect;
  65. rtgui_button_t* open_btn;
  66. _view = demo_view(workbench, "图像演示");
  67. if (_view != RT_NULL)
  68. rtgui_widget_set_event_handler(RTGUI_WIDGET(_view), demo_view_event_handler);
  69. demo_view_get_rect(_view, &rect);
  70. rect.x1 += 5; rect.x2 = rect.x1 + 120;
  71. rect.y2 = rect.y1 + 20;
  72. open_btn = rtgui_button_create("打开图像文件");
  73. rtgui_container_add_child(RTGUI_CONTAINER(_view), RTGUI_WIDGET(open_btn));
  74. rtgui_widget_set_rect(RTGUI_WIDGET(open_btn), &rect);
  75. rtgui_button_set_onbutton(open_btn, open_btn_onbutton);
  76. return _view;
  77. }