label.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * File : label.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2009, 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. * 2009-10-16 Bernard first version
  13. */
  14. #include <rtgui/dc.h>
  15. #include <rtgui/widgets/label.h>
  16. #include <rtgui/rtgui_system.h>
  17. #include <rtgui/rtgui_theme.h>
  18. static void _rtgui_label_constructor(rtgui_label_t *label)
  19. {
  20. /* init widget and set event handler */
  21. rtgui_widget_set_event_handler(label, rtgui_label_event_handler);
  22. RTGUI_WIDGET_FOREGROUND(label) = black;
  23. RTGUI_WIDGET_BACKGROUND(label) = default_background;
  24. RTGUI_WIDGET_TEXTALIGN(label) = RTGUI_ALIGN_CENTER_VERTICAL;
  25. /* set field */
  26. label->text = RT_NULL;
  27. }
  28. static void _rtgui_label_destructor(rtgui_label_t *label)
  29. {
  30. /* release text memory */
  31. if(label->text != RT_NULL)
  32. {
  33. rt_free(label->text);
  34. label->text = RT_NULL;
  35. }
  36. }
  37. rtgui_type_t *rtgui_label_type_get(void)
  38. {
  39. static rtgui_type_t *label_type = RT_NULL;
  40. if(!label_type)
  41. {
  42. label_type = rtgui_type_create("label", RTGUI_WIDGET_TYPE,
  43. sizeof(rtgui_label_t),
  44. RTGUI_CONSTRUCTOR(_rtgui_label_constructor),
  45. RTGUI_DESTRUCTOR(_rtgui_label_destructor));
  46. }
  47. return label_type;
  48. }
  49. rt_bool_t rtgui_label_event_handler(PVOID wdt, rtgui_event_t* event)
  50. {
  51. rtgui_widget_t *widget = (rtgui_widget_t*)wdt;
  52. rtgui_label_t* label;
  53. RT_ASSERT(widget != RT_NULL);
  54. label = (rtgui_label_t*) widget;
  55. switch (event->type)
  56. {
  57. case RTGUI_EVENT_PAINT:
  58. rtgui_theme_draw_label(label);
  59. break;
  60. default:
  61. break;
  62. }
  63. return RT_FALSE;
  64. }
  65. //parent必须是一个容器类控件
  66. rtgui_label_t* rtgui_label_create(PVOID parent, const char* text,int left, int top, int w, int h)
  67. {
  68. rtgui_label_t* label;
  69. RT_ASSERT(parent != RT_NULL);
  70. label = rtgui_widget_create(RTGUI_LABEL_TYPE);
  71. if(label != RT_NULL)
  72. {
  73. rtgui_rect_t rect;
  74. rtgui_widget_get_rect(parent, &rect);
  75. rtgui_widget_rect_to_device(parent,&rect);
  76. rect.x1 += left;
  77. rect.y1 += top;
  78. rect.x2 = rect.x1+w;
  79. rect.y2 = rect.y1+h;
  80. rtgui_widget_set_rect(label, &rect);
  81. label->text = rt_strdup(text);
  82. rtgui_container_add_child(parent, label);
  83. }
  84. return label;
  85. }
  86. void rtgui_label_destroy(rtgui_label_t* label)
  87. {
  88. rtgui_widget_destroy(label);
  89. }
  90. char* rtgui_label_get_text(rtgui_label_t* label)
  91. {
  92. if(label == RT_NULL)return RT_NULL;
  93. return label->text;
  94. }
  95. void rtgui_label_set_text(rtgui_label_t* label, const char* text)
  96. {
  97. if(label == RT_NULL)return;
  98. if(strcmp(text,label->text)==0)return;
  99. if(label->text != RT_NULL)
  100. {
  101. /* release old text memory */
  102. rt_free(label->text);
  103. label->text = RT_NULL;
  104. }
  105. if(text != RT_NULL)
  106. label->text = rt_strdup(text);
  107. else
  108. label->text = RT_NULL;
  109. /* update widget */
  110. rtgui_theme_draw_label(label);//该句似乎是多余的,会在左上角(0,0)绘制一个字符串
  111. }