label.c 2.5 KB

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