label.c 2.7 KB

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