iconbox.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. #include <rtgui/font.h>
  18. static rt_bool_t rtgui_iconbox_onfocus(PVOID wdt, rtgui_event_t* event);
  19. static rt_bool_t rtgui_iconbox_onunfocus(PVOID wdt, rtgui_event_t* event);
  20. static void _rtgui_iconbox_constructor(rtgui_iconbox_t *iconbox)
  21. {
  22. /* init widget and set event handler */
  23. rtgui_widget_set_event_handler(iconbox, rtgui_iconbox_event_handler);
  24. RTGUI_WIDGET_FLAG(iconbox) |= RTGUI_WIDGET_FLAG_TRANSPARENT;
  25. /* set proper of control */
  26. iconbox->image = RT_NULL;
  27. iconbox->selected = RT_FALSE;
  28. iconbox->text = RT_NULL;
  29. iconbox->text_position = RTGUI_ICONBOX_TEXT_BELOW;
  30. iconbox->call = RT_NULL;
  31. rtgui_widget_set_onfocus(iconbox,rtgui_iconbox_onfocus);
  32. rtgui_widget_set_onunfocus(iconbox,rtgui_iconbox_onunfocus);
  33. }
  34. static void _rtgui_iconbox_destructor(rtgui_iconbox_t *iconbox)
  35. {
  36. if(iconbox->image != RT_NULL)
  37. {
  38. rtgui_image_destroy(iconbox->image);
  39. iconbox->image = RT_NULL;
  40. }
  41. if(iconbox->text != RT_NULL)
  42. {
  43. rt_free(iconbox->text);
  44. iconbox->text = RT_NULL;
  45. }
  46. }
  47. rtgui_type_t *rtgui_iconbox_type_get(void)
  48. {
  49. static rtgui_type_t *iconbox_type = RT_NULL;
  50. if(!iconbox_type)
  51. {
  52. iconbox_type = rtgui_type_create("iconbox", RTGUI_WIDGET_TYPE,
  53. sizeof(rtgui_iconbox_t), RTGUI_CONSTRUCTOR(_rtgui_iconbox_constructor),
  54. RTGUI_DESTRUCTOR(_rtgui_iconbox_destructor));
  55. }
  56. return iconbox_type;
  57. }
  58. rt_bool_t rtgui_iconbox_event_handler(PVOID wdt, rtgui_event_t* event)
  59. {
  60. rtgui_widget_t *widget = (rtgui_widget_t*)wdt;
  61. rtgui_iconbox_t* iconbox = (rtgui_iconbox_t*)widget;
  62. switch (event->type)
  63. {
  64. case RTGUI_EVENT_PAINT:
  65. if(widget->on_draw != RT_NULL)
  66. widget->on_draw(widget, event);
  67. else
  68. {
  69. rtgui_theme_draw_iconbox(iconbox);
  70. }
  71. break;
  72. case RTGUI_EVENT_MOUSE_BUTTON:
  73. rtgui_widget_focus(wdt);
  74. if(iconbox->call)
  75. iconbox->call();
  76. break;
  77. }
  78. return RT_FALSE;
  79. }
  80. rtgui_iconbox_t* rtgui_iconbox_create(PVOID parent, rtgui_image_t* image,const char* text,int position)
  81. {
  82. rtgui_iconbox_t* iconbox;
  83. RT_ASSERT(parent != RT_NULL);
  84. iconbox = rtgui_widget_create(RTGUI_ICONBOX_TYPE);
  85. if(iconbox != RT_NULL)
  86. {
  87. rtgui_rect_t rect, text_rect;
  88. rect.x2 = image->w;
  89. rect.y2 = image->h;
  90. /* get text rect */
  91. rtgui_font_get_string_rect(RTGUI_WIDGET_FONT(iconbox), text, &text_rect);
  92. if(position == RTGUI_ICONBOX_TEXT_BELOW)
  93. {
  94. rect.y2 += RTGUI_WIDGET_DEFAULT_MARGIN;
  95. if(text_rect.x2 > rect.x2)
  96. {
  97. rect.x2 = text_rect.x2;
  98. }
  99. rect.y2 += text_rect.y2;
  100. }
  101. else if(position == RTGUI_ICONBOX_TEXT_RIGHT)
  102. {
  103. rect.x2 += RTGUI_WIDGET_DEFAULT_MARGIN;
  104. if(text_rect.y2 > rect.y2)
  105. {
  106. rect.y2 = text_rect.y2;
  107. }
  108. rect.x2 += text_rect.x2;
  109. }
  110. /* set widget rect */
  111. rtgui_widget_set_rect(iconbox, &rect);
  112. /* set image and text position */
  113. iconbox->image = image;
  114. iconbox->text = rt_strdup(text);
  115. iconbox->text_position = position;
  116. rtgui_container_add_child(parent, iconbox);
  117. }
  118. return iconbox;
  119. }
  120. void rtgui_iconbox_destroy(rtgui_iconbox_t* iconbox)
  121. {
  122. rtgui_widget_destroy(iconbox);
  123. }
  124. void rtgui_iconbox_set_text_position(rtgui_iconbox_t* iconbox, int position)
  125. {
  126. rtgui_rect_t rect = {0, 0, 0, 0}, text_rect;
  127. RT_ASSERT(iconbox != RT_NULL);
  128. iconbox->text_position = position;
  129. /* set mini width and height */
  130. rect.x2 = iconbox->image->w;
  131. rect.y2 = iconbox->image->h;
  132. /* get text rect */
  133. if(iconbox->text != RT_NULL)
  134. {
  135. rtgui_font_get_string_rect(RTGUI_WIDGET_FONT(iconbox),
  136. iconbox->text, &text_rect);
  137. if(position == RTGUI_ICONBOX_TEXT_BELOW)
  138. {
  139. rect.y2 += RTGUI_WIDGET_DEFAULT_MARGIN;
  140. if(text_rect.x2 > rect.x2)
  141. {
  142. rect.x2 = text_rect.x2;
  143. }
  144. rect.y2 += text_rect.y2;
  145. }
  146. else if(position == RTGUI_ICONBOX_TEXT_RIGHT)
  147. {
  148. rect.x2 += RTGUI_WIDGET_DEFAULT_MARGIN;
  149. if(text_rect.y2 > rect.y2)
  150. {
  151. rect.y2 = text_rect.y2;
  152. }
  153. rect.x2 += text_rect.x2;
  154. }
  155. }
  156. }
  157. static rt_bool_t rtgui_iconbox_onfocus(PVOID wdt, rtgui_event_t* event)
  158. {
  159. rtgui_rect_t rect;
  160. rtgui_dc_t *dc;
  161. RT_ASSERT(wdt != RT_NULL);
  162. dc = rtgui_dc_begin_drawing(wdt);
  163. if(dc == RT_NULL)return RT_FALSE;
  164. rtgui_widget_get_rect(wdt,&rect);
  165. rtgui_dc_draw_focus_rect(dc,&rect);
  166. rtgui_dc_end_drawing(dc);
  167. return RT_TRUE;
  168. }
  169. static rt_bool_t rtgui_iconbox_onunfocus(PVOID wdt, rtgui_event_t* event)
  170. {
  171. rtgui_rect_t rect;
  172. rtgui_dc_t *dc;
  173. RT_ASSERT(wdt != RT_NULL);
  174. dc = rtgui_dc_begin_drawing(wdt);
  175. if(dc == RT_NULL)return RT_FALSE;
  176. rtgui_widget_get_rect(wdt,&rect);
  177. RTGUI_DC_FC(dc) = RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(wdt)->parent);
  178. rtgui_dc_draw_focus_rect(dc,&rect);
  179. rtgui_dc_end_drawing(dc);
  180. return RT_TRUE;
  181. }