rtgui_demo.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * File : rtgui_demo.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2008 - 2017, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2017-11-8 Tangyuxin first version
  23. */
  24. #include <rtthread.h>
  25. // #define DEBUG
  26. #ifdef DEBUG
  27. #define DEBUG_PRINTF(...) rt_kprintf(__VA_ARGS__)
  28. #else
  29. #define DEBUG_PRINTF(...)
  30. #endif
  31. #ifdef RT_USING_GUIENGINE
  32. #include <rtgui/rtgui.h>
  33. #include <rtgui/rtgui_system.h>
  34. #include <rtgui/rtgui_app.h>
  35. #include <rtgui/widgets/window.h>
  36. #include <rtgui/dc.h>
  37. struct rtgui_win *main_win;
  38. rt_bool_t dc_event_handler(struct rtgui_object *object, rtgui_event_t *event);
  39. static void rt_gui_demo_entry(void *parameter)
  40. {
  41. struct rtgui_app *app;
  42. DEBUG_PRINTF("gui demo entry\n");
  43. /* create gui app */
  44. app = rtgui_app_create("gui_demo");
  45. if (app == RT_NULL)
  46. {
  47. DEBUG_PRINTF("rtgui_app_create faild\n");
  48. return;
  49. }
  50. /* create main window */
  51. main_win = rtgui_mainwin_create(RT_NULL,
  52. "UiWindow", RTGUI_WIN_STYLE_NO_TITLE | RTGUI_WIN_STYLE_NO_BORDER);
  53. if (main_win == RT_NULL)
  54. {
  55. DEBUG_PRINTF("main_win is null\n");
  56. rtgui_app_destroy(app);
  57. return;
  58. }
  59. rtgui_object_set_event_handler(RTGUI_OBJECT(main_win), dc_event_handler);
  60. DEBUG_PRINTF("rtgui_win_show\n");
  61. rtgui_win_show(main_win, RT_FALSE);
  62. DEBUG_PRINTF("rtgui_app_run\n");
  63. rtgui_app_run(app);
  64. DEBUG_PRINTF("rtgui_win_destroy\n");
  65. rtgui_win_destroy(main_win);
  66. DEBUG_PRINTF("rtgui_app_destroy\n");
  67. rtgui_app_destroy(app);
  68. }
  69. rt_bool_t dc_event_handler(struct rtgui_object *object, rtgui_event_t *event)
  70. {
  71. struct rtgui_widget *widget = RTGUI_WIDGET(object);
  72. if (event->type == RTGUI_EVENT_PAINT)
  73. {
  74. struct rtgui_dc *dc;
  75. rtgui_rect_t rect;
  76. rt_kprintf("\r\n RTGUI_EVENT_PAINT \r\n");
  77. rtgui_win_event_handler(RTGUI_OBJECT(widget), event);
  78. rtgui_widget_get_rect(widget, &rect);
  79. DEBUG_PRINTF("widget react x1: %d, y1: %d, x2: %d, y2: %d\r\n",
  80. rect.x1, rect.y1, rect.x2, rect.y2);
  81. dc = rtgui_dc_begin_drawing(widget);
  82. if(dc == RT_NULL)
  83. {
  84. DEBUG_PRINTF("\r\n dc is null \r\n");
  85. return RT_FALSE;
  86. }
  87. rtgui_dc_draw_line(dc, rect.x1, rect.y1, rect.x2, rect.y2);
  88. rtgui_dc_draw_line(dc, rect.x1, rect.y2, rect.x2, rect.y1);
  89. rect.x1 += (rect.x2 - rect.x1) / 2;
  90. rect.y1 += (rect.y2 - rect.y1) / 2;
  91. rtgui_dc_draw_text_stroke(dc, __DATE__"--"__TIME__, &rect, HIGH_LIGHT, BLUE);
  92. rtgui_dc_end_drawing(dc,RT_TRUE);
  93. }
  94. return RT_FALSE;
  95. }
  96. int rt_gui_demo_init(void)
  97. {
  98. rt_thread_t tid;
  99. rt_device_t device;
  100. rt_err_t err;
  101. device = rt_device_find("lcd");
  102. if(device == RT_NULL)
  103. {
  104. rt_kprintf("Not found LCD driver\n");
  105. return RT_ERROR;
  106. }
  107. err = rt_device_open(device, RT_DEVICE_OFLAG_RDWR);
  108. if (err != RT_EOK)
  109. {
  110. rt_kprintf("Open LCD driver fail\n");
  111. return RT_ERROR;
  112. }
  113. /* set graphic device */
  114. rtgui_graphic_set_device(device);
  115. tid = rt_thread_create("mygui",
  116. rt_gui_demo_entry, RT_NULL,
  117. 2048, 25, 10);
  118. if (tid != RT_NULL)
  119. rt_thread_startup(tid);
  120. return 0;
  121. }
  122. INIT_APP_EXPORT(rt_gui_demo_init);
  123. #endif /* RT_USING_GUIENGINE */