label.c 2.5 KB

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