iconbox.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 | RTGUI_WIDGET_FLAG_FOCUSABLE);
  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. if (iconbox->text != RT_NULL)
  36. {
  37. rt_free(iconbox->text);
  38. iconbox->text = RT_NULL;
  39. }
  40. }
  41. DEFINE_CLASS_TYPE(iconbox, "iconbox",
  42. RTGUI_WIDGET_TYPE,
  43. _rtgui_iconbox_constructor,
  44. _rtgui_iconbox_destructor,
  45. sizeof(struct rtgui_iconbox));
  46. rt_bool_t rtgui_iconbox_event_handler(struct rtgui_object *object, struct rtgui_event *event)
  47. {
  48. struct rtgui_iconbox *iconbox;
  49. iconbox = RTGUI_ICONBOX(object);
  50. switch (event->type)
  51. {
  52. case RTGUI_EVENT_PAINT:
  53. rtgui_theme_draw_iconbox(iconbox);
  54. break;
  55. case RTGUI_EVENT_MOUSE_BUTTON:
  56. if (RTGUI_WIDGET_IS_HIDE(object)) return RT_FALSE;
  57. {
  58. struct rtgui_event_mouse *emouse = (struct rtgui_event_mouse *)event;
  59. /* it's not this widget event, clean status */
  60. if (rtgui_rect_contains_point(&(RTGUI_WIDGET(iconbox)->extent),
  61. emouse->x, emouse->y) != RT_EOK)
  62. {
  63. if (iconbox->selected != RT_TRUE)
  64. {
  65. rtgui_iconbox_set_selected(iconbox, RT_TRUE);
  66. rtgui_widget_focus(RTGUI_WIDGET(iconbox));
  67. }
  68. break;
  69. }
  70. }
  71. return RT_TRUE;
  72. default:
  73. return rtgui_widget_event_handler(object, event);
  74. }
  75. return RT_FALSE;
  76. }
  77. struct rtgui_iconbox *rtgui_iconbox_create(struct rtgui_image *image,
  78. const char *text,
  79. int position)
  80. {
  81. struct rtgui_iconbox *iconbox;
  82. iconbox = (struct rtgui_iconbox *)rtgui_widget_create(RTGUI_ICONBOX_TYPE);
  83. if (iconbox != RT_NULL)
  84. {
  85. rtgui_rect_t rect = {0, 0, 0, 0}, text_rect;
  86. rect.x2 = image->w;
  87. rect.y2 = image->h;
  88. /* get text rect */
  89. rtgui_font_get_metrics(rtgui_font_default(), text, &text_rect);
  90. if (position == RTGUI_ICONBOX_TEXT_BELOW)
  91. {
  92. rect.y2 += RTGUI_WIDGET_DEFAULT_MARGIN;
  93. if (text_rect.x2 > rect.x2)
  94. {
  95. rect.x2 = text_rect.x2;
  96. }
  97. rect.y2 += text_rect.y2;
  98. }
  99. else if (position == RTGUI_ICONBOX_TEXT_RIGHT)
  100. {
  101. rect.x2 += RTGUI_WIDGET_DEFAULT_MARGIN;
  102. if (text_rect.y2 > rect.y2)
  103. {
  104. rect.y2 = text_rect.y2;
  105. }
  106. rect.x2 += text_rect.x2;
  107. }
  108. /* set widget rect */
  109. rtgui_widget_set_rect(RTGUI_WIDGET(iconbox), &rect);
  110. /* set image and text position */
  111. iconbox->image = image;
  112. iconbox->text = (char *)rt_strdup((const char *)text);
  113. iconbox->text_position = position;
  114. }
  115. return iconbox;
  116. }
  117. void rtgui_iconbox_destroy(struct rtgui_iconbox *iconbox)
  118. {
  119. rtgui_widget_destroy(RTGUI_WIDGET(iconbox));
  120. }
  121. void rtgui_iconbox_set_text_position(struct rtgui_iconbox *iconbox, int position)
  122. {
  123. struct rtgui_rect rect = {0, 0, 0, 0}, text_rect;
  124. RT_ASSERT(iconbox != RT_NULL);
  125. iconbox->text_position = position;
  126. /* set mini width and height */
  127. rect.x2 = iconbox->image->w;
  128. rect.y2 = iconbox->image->h;
  129. /* get text rect */
  130. if (iconbox->text != RT_NULL)
  131. {
  132. rtgui_font_get_metrics(rtgui_font_default(),
  133. iconbox->text, &text_rect);
  134. if (position == RTGUI_ICONBOX_TEXT_BELOW)
  135. {
  136. rect.y2 += RTGUI_WIDGET_DEFAULT_MARGIN;
  137. if (text_rect.x2 > rect.x2)
  138. {
  139. rect.x2 = text_rect.x2;
  140. }
  141. rect.y2 += text_rect.y2;
  142. }
  143. else if (position == RTGUI_ICONBOX_TEXT_RIGHT)
  144. {
  145. rect.x2 += RTGUI_WIDGET_DEFAULT_MARGIN;
  146. if (text_rect.y2 > rect.y2)
  147. {
  148. rect.y2 = text_rect.y2;
  149. }
  150. rect.x2 += text_rect.x2;
  151. }
  152. }
  153. }
  154. void rtgui_iconbox_set_selected(struct rtgui_iconbox *iconbox, rt_bool_t selected)
  155. {
  156. RT_ASSERT(iconbox != RT_NULL);
  157. iconbox->selected = selected;
  158. }