label.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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(RTGUI_WIDGET(label), rtgui_label_event_handler);
  22. /* set field */
  23. label->text = RT_NULL;
  24. }
  25. static void _rtgui_label_destructor(rtgui_label_t *label)
  26. {
  27. /* release text memory */
  28. rt_free(label->text);
  29. label->text = RT_NULL;
  30. }
  31. rtgui_type_t *rtgui_label_type_get(void)
  32. {
  33. static rtgui_type_t *label_type = RT_NULL;
  34. if (!label_type)
  35. {
  36. label_type = rtgui_type_create("label", RTGUI_WIDGET_TYPE,
  37. sizeof(rtgui_label_t),
  38. RTGUI_CONSTRUCTOR(_rtgui_label_constructor),
  39. RTGUI_DESTRUCTOR(_rtgui_label_destructor));
  40. }
  41. return label_type;
  42. }
  43. rt_bool_t rtgui_label_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
  44. {
  45. struct rtgui_label* label;
  46. RT_ASSERT(widget != RT_NULL);
  47. label = (struct rtgui_label*) widget;
  48. switch (event->type)
  49. {
  50. case RTGUI_EVENT_PAINT:
  51. rtgui_theme_draw_label(label);
  52. break;
  53. }
  54. return RT_FALSE;
  55. }
  56. rtgui_label_t* rtgui_label_create(const char* text)
  57. {
  58. struct rtgui_label* label;
  59. label = (struct rtgui_label*) rtgui_widget_create(RTGUI_LABEL_TYPE);
  60. if (label != RT_NULL)
  61. {
  62. rtgui_rect_t rect;
  63. /* set default rect */
  64. rtgui_font_get_metrics(rtgui_font_default(), text, &rect);
  65. rect.x2 += (RTGUI_BORDER_DEFAULT_WIDTH << 1);
  66. rect.y2 += (RTGUI_BORDER_DEFAULT_WIDTH << 1);
  67. rtgui_widget_set_rect(RTGUI_WIDGET(label), &rect);
  68. /* set text */
  69. label->text = (char*)rt_strdup((const char*)text);
  70. }
  71. return label;
  72. }
  73. void rtgui_label_destroy(rtgui_label_t* label)
  74. {
  75. rtgui_widget_destroy(RTGUI_WIDGET(label));
  76. }
  77. char* rtgui_label_get_text(rtgui_label_t* label)
  78. {
  79. RT_ASSERT(label != RT_NULL);
  80. return label->text;
  81. }
  82. void rtgui_label_set_text(rtgui_label_t* label, const char* text)
  83. {
  84. RT_ASSERT(label != RT_NULL);
  85. if (label->text != RT_NULL)
  86. {
  87. /* release old text memory */
  88. rt_free(label->text);
  89. }
  90. if (text != RT_NULL) label->text = (char*)rt_strdup((const char*)text);
  91. else label->text = RT_NULL;
  92. /* update widget */
  93. rtgui_theme_draw_label(label);
  94. }