iconbox.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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_object_set_event_handler(RTGUI_OBJECT(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. static void _rtgui_iconbox_destructor(rtgui_iconbox_t *iconbox)
  29. {
  30. if (iconbox->image != RT_NULL)
  31. {
  32. rtgui_image_destroy(iconbox->image);
  33. iconbox->image = RT_NULL;
  34. }
  35. rt_free(iconbox->text);
  36. iconbox->text = RT_NULL;
  37. }
  38. DEFINE_CLASS_TYPE(iconbox, "iconbox",
  39. RTGUI_WIDGET_TYPE,
  40. _rtgui_iconbox_constructor,
  41. _rtgui_iconbox_destructor,
  42. sizeof(struct rtgui_iconbox));
  43. rt_bool_t rtgui_iconbox_event_handler(struct rtgui_object* object, struct rtgui_event* event)
  44. {
  45. struct rtgui_iconbox* iconbox;
  46. RT_ASSERT(object != RT_NULL);
  47. RT_ASSERT(event != RT_NULL);
  48. iconbox = RTGUI_ICONBOX(object);
  49. switch (event->type)
  50. {
  51. case RTGUI_EVENT_PAINT:
  52. #ifndef RTGUI_USING_SMALL_SIZE
  53. if (widget->on_draw != RT_NULL) widget->on_draw(widget, event);
  54. else
  55. #endif
  56. {
  57. rtgui_theme_draw_iconbox(iconbox);
  58. }
  59. break;
  60. }
  61. return RT_FALSE;
  62. }
  63. struct rtgui_iconbox* rtgui_iconbox_create(struct rtgui_image* image,
  64. const char* text,
  65. int position)
  66. {
  67. struct rtgui_iconbox* iconbox;
  68. iconbox = (struct rtgui_iconbox*)rtgui_widget_create(RTGUI_ICONBOX_TYPE);
  69. if (iconbox != RT_NULL)
  70. {
  71. rtgui_rect_t rect = {0, 0, 0, 0}, text_rect;
  72. rect.x2 = image->w;
  73. rect.y2 = image->h;
  74. /* get text rect */
  75. rtgui_font_get_metrics(rtgui_font_default(), text, &text_rect);
  76. if (position == RTGUI_ICONBOX_TEXT_BELOW)
  77. {
  78. rect.y2 += RTGUI_WIDGET_DEFAULT_MARGIN;
  79. if (text_rect.x2 > rect.x2)
  80. {
  81. rect.x2 = text_rect.x2;
  82. }
  83. rect.y2 += text_rect.y2;
  84. }
  85. else if (position == RTGUI_ICONBOX_TEXT_RIGHT)
  86. {
  87. rect.x2 += RTGUI_WIDGET_DEFAULT_MARGIN;
  88. if (text_rect.y2 > rect.y2)
  89. {
  90. rect.y2 = text_rect.y2;
  91. }
  92. rect.x2 += text_rect.x2;
  93. }
  94. /* set widget rect */
  95. rtgui_widget_set_rect(RTGUI_WIDGET(iconbox), &rect);
  96. /* set image and text position */
  97. iconbox->image = image;
  98. iconbox->text = (char*)rt_strdup((const char*)text);
  99. iconbox->text_position = position;
  100. }
  101. return iconbox;
  102. }
  103. void rtgui_iconbox_destroy(struct rtgui_iconbox* iconbox)
  104. {
  105. rtgui_widget_destroy(RTGUI_WIDGET(iconbox));
  106. }
  107. void rtgui_iconbox_set_text_position(struct rtgui_iconbox* iconbox, int position)
  108. {
  109. rtgui_rect_t rect = {0, 0, 0, 0}, text_rect;
  110. RT_ASSERT(iconbox != RT_NULL);
  111. iconbox->text_position = position;
  112. /* set mini width and height */
  113. rect.x2 = iconbox->image->w;
  114. rect.y2 = iconbox->image->h;
  115. /* get text rect */
  116. if (iconbox->text != RT_NULL)
  117. {
  118. rtgui_font_get_metrics(rtgui_font_default(),
  119. iconbox->text, &text_rect);
  120. if (position == RTGUI_ICONBOX_TEXT_BELOW)
  121. {
  122. rect.y2 += RTGUI_WIDGET_DEFAULT_MARGIN;
  123. if (text_rect.x2 > rect.x2)
  124. {
  125. rect.x2 = text_rect.x2;
  126. }
  127. rect.y2 += text_rect.y2;
  128. }
  129. else if (position == RTGUI_ICONBOX_TEXT_RIGHT)
  130. {
  131. rect.x2 += RTGUI_WIDGET_DEFAULT_MARGIN;
  132. if (text_rect.y2 > rect.y2)
  133. {
  134. rect.y2 = text_rect.y2;
  135. }
  136. rect.x2 += text_rect.x2;
  137. }
  138. }
  139. #ifndef RTGUI_USING_SMALL_SIZE
  140. rtgui_widget_set_miniwidth(RTGUI_WIDGET(iconbox), rect.x2);
  141. rtgui_widget_set_miniheight(RTGUI_WIDGET(iconbox), rect.y2);
  142. #endif
  143. }