label.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. RTGUI_WIDGET_TEXTALIGN(label) = RTGUI_ALIGN_LEFT | RTGUI_ALIGN_CENTER_VERTICAL;
  21. /* init widget and set event handler */
  22. rtgui_object_set_event_handler(RTGUI_OBJECT(label), rtgui_label_event_handler);
  23. /* set field */
  24. label->text = RT_NULL;
  25. }
  26. static void _rtgui_label_destructor(rtgui_label_t *label)
  27. {
  28. /* release text memory */
  29. if (label->text)
  30. rt_free(label->text);
  31. label->text = RT_NULL;
  32. }
  33. DEFINE_CLASS_TYPE(label, "label",
  34. RTGUI_WIDGET_TYPE,
  35. _rtgui_label_constructor,
  36. _rtgui_label_destructor,
  37. sizeof(struct rtgui_label));
  38. rt_bool_t rtgui_label_event_handler(struct rtgui_object *object, struct rtgui_event* event)
  39. {
  40. struct rtgui_label *label;
  41. RTGUI_WIDGET_EVENT_HANDLER_PREPARE
  42. label = RTGUI_LABEL(object);
  43. switch (event->type)
  44. {
  45. case RTGUI_EVENT_PAINT:
  46. rtgui_theme_draw_label(label);
  47. break;
  48. default:
  49. return rtgui_widget_event_handler(object, event);
  50. }
  51. return RT_FALSE;
  52. }
  53. RTM_EXPORT(rtgui_label_event_handler);
  54. rtgui_label_t* rtgui_label_create(const char* text)
  55. {
  56. struct rtgui_label* label;
  57. label = (struct rtgui_label*) rtgui_widget_create(RTGUI_LABEL_TYPE);
  58. if (label != RT_NULL)
  59. {
  60. rtgui_rect_t rect;
  61. /* set default rect */
  62. rtgui_font_get_metrics(rtgui_font_default(), text, &rect);
  63. rect.x2 += (RTGUI_BORDER_DEFAULT_WIDTH << 1);
  64. rect.y2 += (RTGUI_BORDER_DEFAULT_WIDTH << 1);
  65. rtgui_widget_set_rect(RTGUI_WIDGET(label), &rect);
  66. /* set text */
  67. label->text = (char*)rt_strdup((const char*)text);
  68. }
  69. return label;
  70. }
  71. RTM_EXPORT(rtgui_label_create);
  72. void rtgui_label_destroy(rtgui_label_t* label)
  73. {
  74. rtgui_widget_destroy(RTGUI_WIDGET(label));
  75. }
  76. RTM_EXPORT(rtgui_label_destroy);
  77. char* rtgui_label_get_text(rtgui_label_t* label)
  78. {
  79. RT_ASSERT(label != RT_NULL);
  80. return label->text;
  81. }
  82. RTM_EXPORT(rtgui_label_get_text);
  83. void rtgui_label_set_text(rtgui_label_t* label, const char* text)
  84. {
  85. RT_ASSERT(label != RT_NULL);
  86. if (label->text != RT_NULL)
  87. {
  88. /* it's a same text string */
  89. if (rt_strcmp(text, label->text) == 0)
  90. return;
  91. /* release old text memory */
  92. rt_free(label->text);
  93. }
  94. if (text != RT_NULL)
  95. label->text = (char*)rt_strdup((const char*)text);
  96. else
  97. label->text = RT_NULL;
  98. /* update widget */
  99. rtgui_theme_draw_label(label);
  100. }
  101. RTM_EXPORT(rtgui_label_set_text);