rtgui_demo.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * File : application.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. */
  13. #include <rtthread.h>
  14. #include "finsh.h"
  15. #include "rtgui_demo.h"
  16. #define DEBUG
  17. #ifdef DEBUG
  18. #define DEBUG_PRINTF(...) rt_kprintf(__VA_ARGS__)
  19. #else
  20. #define DEBUG_PRINTF(...)
  21. #endif
  22. #ifdef PKG_USING_GUIENGINE
  23. #include <rtgui/rtgui.h>
  24. #include <rtgui/rtgui_system.h>
  25. #include <rtgui/rtgui_app.h>
  26. #include <rtgui/widgets/window.h>
  27. #include <rtgui/dc.h>
  28. struct rtgui_win *main_win;
  29. rt_bool_t dc_event_handler(struct rtgui_object *object, rtgui_event_t *event);
  30. static void rt_gui_demo_entry(void *parameter)
  31. {
  32. struct rtgui_app *app;
  33. //struct rtgui_dc *dc;
  34. DEBUG_PRINTF("gui demo entry\n");
  35. /* create gui app */
  36. app = rtgui_app_create("gui_demo");
  37. if (app == RT_NULL)
  38. {
  39. DEBUG_PRINTF("rtgui_app_create faild\n");
  40. return;
  41. }
  42. /* create main window */
  43. main_win = rtgui_mainwin_create(RT_NULL,
  44. "UiWindow", RTGUI_WIN_STYLE_NO_TITLE | RTGUI_WIN_STYLE_NO_BORDER);
  45. if (main_win == RT_NULL)
  46. {
  47. DEBUG_PRINTF("main_win is null\n");
  48. rtgui_app_destroy(app);
  49. return;
  50. }
  51. rtgui_object_set_event_handler(RTGUI_OBJECT(main_win), dc_event_handler);
  52. DEBUG_PRINTF("rtgui_win_show\n");
  53. rtgui_win_show(main_win, RT_FALSE);
  54. DEBUG_PRINTF("rtgui_app_run\n");
  55. rtgui_app_run(app);
  56. DEBUG_PRINTF("rtgui_win_destroy\n");
  57. rtgui_win_destroy(main_win);
  58. DEBUG_PRINTF("rtgui_app_destroy\n");
  59. rtgui_app_destroy(app);
  60. }
  61. rt_bool_t dc_event_handler(struct rtgui_object *object, rtgui_event_t *event)
  62. {
  63. struct rtgui_widget *widget = RTGUI_WIDGET(object);
  64. if (event->type == RTGUI_EVENT_PAINT)
  65. {
  66. struct rtgui_dc *dc;
  67. rtgui_rect_t rect;
  68. rt_kprintf("\r\n RTGUI_EVENT_PAINT \r\n");
  69. rtgui_win_event_handler(RTGUI_OBJECT(widget), event);
  70. rtgui_widget_get_rect(widget, &rect);
  71. DEBUG_PRINTF("widget react x1: %d, y1: %d, x2: %d, y2: %d\r\n",
  72. rect.x1, rect.y1, rect.x2, rect.y2);
  73. dc = rtgui_dc_begin_drawing(widget);
  74. if(dc == RT_NULL)
  75. {
  76. DEBUG_PRINTF("\r\n dc is null \r\n");
  77. return RT_FALSE;
  78. }
  79. rtgui_dc_draw_line(dc, rect.x1, rect.y1, rect.x2, rect.y2);
  80. rtgui_dc_draw_line(dc, rect.x1, rect.y2, rect.x2, rect.y1);
  81. rect.x1 += (rect.x2 - rect.x1) / 2;
  82. rect.y1 += (rect.y2 - rect.y1) / 2;
  83. rtgui_dc_draw_text_stroke(dc, __DATE__"--"__TIME__, &rect, HIGH_LIGHT, BLUE);
  84. rtgui_dc_end_drawing(dc, RT_TRUE);
  85. }
  86. return RT_FALSE;
  87. }
  88. int rt_gui_demo_init(void)
  89. {
  90. rt_thread_t tid;
  91. tid = rt_thread_create("mygui",
  92. rt_gui_demo_entry, RT_NULL,
  93. 2048, 25, 10);
  94. if (tid != RT_NULL)
  95. rt_thread_startup(tid);
  96. return 0;
  97. }
  98. #endif /* PKG_USING_GUIENGINE */