iconbox.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * File : iconbox.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/iconbox.h>
  16. #include <rtgui/rtgui_theme.h>
  17. static void _rtgui_iconbox_constructor(rtgui_iconbox_t *iconbox)
  18. {
  19. /* init widget and set event handler */
  20. RTGUI_WIDGET(iconbox)->flag |= RTGUI_WIDGET_FLAG_TRANSPARENT;
  21. rtgui_widget_set_event_handler(RTGUI_WIDGET(iconbox), rtgui_iconbox_event_handler);
  22. /* set proper of control */
  23. iconbox->image = RT_NULL;
  24. iconbox->selected = RT_FALSE;
  25. iconbox->text = RT_NULL;
  26. iconbox->text_position = RTGUI_ICONBOX_TEXT_BELOW;
  27. }
  28. rtgui_type_t *rtgui_iconbox_type_get(void)
  29. {
  30. static rtgui_type_t *iconbox_type = RT_NULL;
  31. if (!iconbox_type)
  32. {
  33. iconbox_type = rtgui_type_create("iconbox", RTGUI_WIDGET_TYPE,
  34. sizeof(rtgui_iconbox_t), RTGUI_CONSTRUCTOR(_rtgui_iconbox_constructor), RT_NULL);
  35. }
  36. return iconbox_type;
  37. }
  38. rt_bool_t rtgui_iconbox_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
  39. {
  40. struct rtgui_iconbox* iconbox = (struct rtgui_iconbox*)widget;
  41. switch (event->type)
  42. {
  43. case RTGUI_EVENT_PAINT:
  44. if (widget->on_draw != RT_NULL) widget->on_draw(widget, event);
  45. else
  46. {
  47. rtgui_theme_draw_iconbox(iconbox);
  48. }
  49. break;
  50. }
  51. return RT_FALSE;
  52. }
  53. struct rtgui_iconbox* rtgui_iconbox_create(struct rtgui_image* image,
  54. const unsigned char* text,
  55. int position)
  56. {
  57. struct rtgui_iconbox* iconbox;
  58. iconbox = (struct rtgui_iconbox*)rtgui_widget_create(RTGUI_ICONBOX_TYPE);
  59. if (iconbox != RT_NULL)
  60. {
  61. rtgui_rect_t rect = {0, 0, 0, 0}, text_rect;
  62. rect.x2 = image->w;
  63. rect.y2 = image->h;
  64. /* get text rect */
  65. rtgui_font_get_metrics(rtgui_font_default(), text, &text_rect);
  66. if (position == RTGUI_ICONBOX_TEXT_BELOW)
  67. {
  68. rect.y2 += RTGUI_WIDGET_DEFAULT_MARGIN;
  69. if (text_rect.x2 > rect.x2)
  70. {
  71. rect.x2 = text_rect.x2;
  72. }
  73. rect.y2 += text_rect.y2;
  74. }
  75. else if (position == RTGUI_ICONBOX_TEXT_RIGHT)
  76. {
  77. rect.x2 += RTGUI_WIDGET_DEFAULT_MARGIN;
  78. if (text_rect.y2 > rect.y2)
  79. {
  80. rect.y2 = text_rect.y2;
  81. }
  82. rect.x2 += text_rect.x2;
  83. }
  84. /* set widget rect */
  85. rtgui_widget_set_rect(RTGUI_WIDGET(iconbox), &rect);
  86. /* set image and text position */
  87. iconbox->image = image;
  88. iconbox->text = (unsigned char*)rt_strdup((const char*)text);
  89. iconbox->text_position = position;
  90. }
  91. return iconbox;
  92. }
  93. void rtgui_iconbox_destroy(struct rtgui_iconbox* iconbox)
  94. {
  95. rtgui_widget_destroy(RTGUI_WIDGET(iconbox));
  96. }
  97. void rtgui_iconbox_set_text_position(struct rtgui_iconbox* iconbox, int position)
  98. {
  99. rtgui_rect_t rect = {0, 0, 0, 0}, text_rect;
  100. RT_ASSERT(iconbox != RT_NULL);
  101. iconbox->text_position = position;
  102. /* set mini width and height */
  103. rect.x2 = iconbox->image->w;
  104. rect.y2 = iconbox->image->h;
  105. /* get text rect */
  106. if (iconbox->text != RT_NULL)
  107. {
  108. rtgui_font_get_metrics(rtgui_font_default(),
  109. iconbox->text, &text_rect);
  110. if (position == RTGUI_ICONBOX_TEXT_BELOW)
  111. {
  112. rect.y2 += RTGUI_WIDGET_DEFAULT_MARGIN;
  113. if (text_rect.x2 > rect.x2)
  114. {
  115. rect.x2 = text_rect.x2;
  116. }
  117. rect.y2 += text_rect.y2;
  118. }
  119. else if (position == RTGUI_ICONBOX_TEXT_RIGHT)
  120. {
  121. rect.x2 += RTGUI_WIDGET_DEFAULT_MARGIN;
  122. if (text_rect.y2 > rect.y2)
  123. {
  124. rect.y2 = text_rect.y2;
  125. }
  126. rect.x2 += text_rect.x2;
  127. }
  128. }
  129. rtgui_widget_set_miniwidth(RTGUI_WIDGET(iconbox), rect.x2);
  130. rtgui_widget_set_miniheight(RTGUI_WIDGET(iconbox), rect.y2);
  131. }