rtgui_theme.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. /*
  2. * File : rtgui_theme.c
  3. * This file is part of RTGUI in 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-04 Bernard first version
  13. */
  14. #include <rtgui/rtgui.h>
  15. #include <rtgui/dc.h>
  16. #include <rtgui/widgets/widget.h>
  17. #include <rtgui/widgets/button.h>
  18. #include <rtgui/widgets/label.h>
  19. #include <rtgui/widgets/textbox.h>
  20. #include <rtgui/widgets/iconbox.h>
  21. #include <rtgui/widgets/title.h>
  22. #include <rtgui/rtgui_theme.h>
  23. #include <rtgui/rtgui_server.h>
  24. #include <rtgui/rtgui_system.h>
  25. #define SELECTED_HEIGHT 25
  26. const rtgui_color_t default_foreground = RTGUI_RGB(0x00, 0x00, 0x00);
  27. const rtgui_color_t default_background = RTGUI_RGB(212, 208, 200);
  28. const rtgui_color_t selected_color = RTGUI_RGB(0xc0, 0xc0, 0xc0);
  29. extern struct rtgui_font rtgui_font_asc16;
  30. extern struct rtgui_font rtgui_font_arial16;
  31. extern struct rtgui_font rtgui_font_asc12;
  32. extern struct rtgui_font rtgui_font_arial12;
  33. /* init theme */
  34. void rtgui_system_theme_init()
  35. {
  36. #if RTGUI_DEFAULT_FONT_SIZE == 16
  37. rtgui_font_set_defaut(&rtgui_font_asc16);
  38. #elif RTGUI_DEFAULT_FONT_SIZE == 12
  39. rtgui_font_set_defaut(&rtgui_font_asc12);
  40. #else
  41. rtgui_font_set_defaut(&rtgui_font_asc12);
  42. #endif
  43. }
  44. static const rt_uint8_t close_byte[14] = {0x06, 0x18, 0x03, 0x30, 0x01, 0xE0, 0x00,
  45. 0xC0, 0x01, 0xE0, 0x03, 0x30, 0x06, 0x18
  46. };
  47. /* window drawing */
  48. void rtgui_theme_draw_win(struct rtgui_topwin* win)
  49. {
  50. struct rtgui_dc* dc;
  51. rtgui_rect_t rect;
  52. /* begin drawing */
  53. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(win->title));
  54. if (dc == RT_NULL) return;
  55. /* get rect */
  56. rtgui_widget_get_rect(RTGUI_WIDGET(win->title), &rect);
  57. /* draw border */
  58. if (win->flag & WINTITLE_BORDER)
  59. {
  60. rect.x2 -= 1; rect.y2 -= 1;
  61. RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(win->title)) = RTGUI_RGB(212, 208, 200);
  62. rtgui_dc_draw_hline(dc, rect.x1, rect.x2, rect.y1);
  63. rtgui_dc_draw_vline(dc, rect.x1, rect.y1, rect.y2);
  64. RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(win->title)) = white;
  65. rtgui_dc_draw_hline(dc, rect.x1 + 1, rect.x2 - 1, rect.y1 + 1);
  66. rtgui_dc_draw_vline(dc, rect.x1 + 1, rect.y1 + 1, rect.y2 - 1);
  67. RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(win->title)) = RTGUI_RGB(128, 128, 128);
  68. rtgui_dc_draw_hline(dc, rect.x1 + 1, rect.x2 - 1, rect.y2 - 1);
  69. rtgui_dc_draw_vline(dc, rect.x2 - 1, rect.y1 + 1, rect.y2 - 1);
  70. RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(win->title)) = RTGUI_RGB(64, 64, 64);
  71. rtgui_dc_draw_hline(dc, rect.x1, rect.x2, rect.y2);
  72. rtgui_dc_draw_vline(dc, rect.x2, rect.y1, rect.y2);
  73. /* shrink border */
  74. rtgui_rect_inflate(&rect, -WINTITLE_BORDER_SIZE);
  75. }
  76. /* draw title */
  77. if (!(win->flag & WINTITLE_NO))
  78. {
  79. rt_uint32_t index;
  80. float r, g, b, delta;
  81. if (win->flag & WINTITLE_ACTIVATE)
  82. {
  83. r = 10; g = 36; b = 106;
  84. delta = 150 / (float)(rect.x2 - rect.x1);
  85. }
  86. else
  87. {
  88. r = 128; g = 128; b = 128;
  89. delta = 64 / (float)(rect.x2 - rect.x1);
  90. }
  91. for (index = rect.x1; index < rect.x2; index ++)
  92. {
  93. RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(win->title)) = RTGUI_RGB(r, g, b);
  94. rtgui_dc_draw_vline(dc, index, rect.y1, rect.y2);
  95. r += delta; g += delta; b += delta;
  96. }
  97. if (win->flag & WINTITLE_ACTIVATE)
  98. {
  99. RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(win->title)) = white;
  100. }
  101. else
  102. {
  103. RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(win->title)) = RTGUI_RGB(212, 208, 200);
  104. }
  105. rect.x1 += 4;
  106. rect.y1 += 2; rect.y2 = rect.y1 + WINTITLE_CB_HEIGHT;
  107. rtgui_dc_draw_text(dc, rtgui_wintitle_get_title(win->title), &rect);
  108. if (win->flag & WINTITLE_CLOSEBOX)
  109. {
  110. /* get close button rect */
  111. rtgui_rect_t box_rect = {0, 0, WINTITLE_CB_WIDTH, WINTITLE_CB_HEIGHT};
  112. rtgui_rect_moveto_align(&rect, &box_rect, RTGUI_ALIGN_CENTER_VERTICAL | RTGUI_ALIGN_RIGHT);
  113. box_rect.x1 -= 3; box_rect.x2 -= 3;
  114. rtgui_dc_fill_rect(dc, &box_rect);
  115. /* draw close box */
  116. if (win->flag & WINTITLE_CB_PRESSED)
  117. {
  118. rtgui_dc_draw_border(dc, &box_rect, RTGUI_BORDER_SUNKEN);
  119. RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(win->title)) = red;
  120. rtgui_dc_draw_word(dc, box_rect.x1, box_rect.y1 + 6, 7, close_byte);
  121. }
  122. else
  123. {
  124. rtgui_dc_draw_border(dc, &box_rect, RTGUI_BORDER_RAISE);
  125. RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(win->title)) = black;
  126. rtgui_dc_draw_word(dc, box_rect.x1 - 1, box_rect.y1 + 5, 7, close_byte);
  127. }
  128. }
  129. }
  130. rtgui_dc_end_drawing(dc);
  131. }
  132. /* widget drawing */
  133. void rtgui_theme_draw_button(rtgui_button_t* btn)
  134. {
  135. /* draw button */
  136. struct rtgui_dc* dc;
  137. struct rtgui_rect rect;
  138. rtgui_color_t bc, fc;
  139. /* begin drawing */
  140. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(btn));
  141. if (dc == RT_NULL) return;
  142. /* get widget rect */
  143. rtgui_widget_get_rect(RTGUI_WIDGET(btn), &rect);
  144. /* get foreground color */
  145. bc = RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(btn));
  146. fc = RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(btn));
  147. if (btn->flag & RTGUI_BUTTON_TYPE_PUSH && btn->flag & RTGUI_BUTTON_FLAG_PRESS)
  148. {
  149. /* fill button rect with background color */
  150. rtgui_dc_fill_rect(dc, &rect);
  151. /* draw border */
  152. RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(btn)) = RTGUI_RGB(64, 64, 64);
  153. rtgui_dc_draw_hline(dc, rect.x1, rect.x2, rect.y1);
  154. rtgui_dc_draw_vline(dc, rect.x1, rect.y1, rect.y2);
  155. RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(btn)) = RTGUI_RGB(128, 128, 128);
  156. rtgui_dc_draw_hline(dc, rect.x1, rect.x2 - 1, rect.y1 + 1);
  157. rtgui_dc_draw_vline(dc, rect.x1 + 1, rect.y1 + 1, rect.y2 - 2);
  158. RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(btn)) = RTGUI_RGB(255, 255, 255);
  159. rtgui_dc_draw_hline(dc, rect.x1, rect.x2 + 1, rect.y2 - 1);
  160. rtgui_dc_draw_vline(dc, rect.x2 - 1, rect.y1, rect.y2);
  161. if (btn->pressed_image != RT_NULL)
  162. {
  163. rtgui_rect_t image_rect;
  164. image_rect.x1 = 0; image_rect.y1 = 0;
  165. image_rect.x2 = btn->unpressed_image->w;
  166. image_rect.y2 = btn->unpressed_image->h;
  167. rtgui_rect_moveto_align(&rect, &image_rect, RTGUI_ALIGN_CENTER_HORIZONTAL | RTGUI_ALIGN_CENTER_VERTICAL);
  168. rtgui_image_blit(btn->pressed_image, dc, &image_rect);
  169. }
  170. }
  171. else if (btn->flag & RTGUI_BUTTON_FLAG_PRESS)
  172. {
  173. if (btn->pressed_image != RT_NULL)
  174. {
  175. rtgui_rect_t image_rect;
  176. image_rect.x1 = 0; image_rect.y1 = 0;
  177. image_rect.x2 = btn->unpressed_image->w;
  178. image_rect.y2 = btn->unpressed_image->h;
  179. rtgui_rect_moveto_align(&rect, &image_rect, RTGUI_ALIGN_CENTER_HORIZONTAL | RTGUI_ALIGN_CENTER_VERTICAL);
  180. rtgui_image_blit(btn->pressed_image, dc, &image_rect);
  181. }
  182. else
  183. {
  184. /* fill button rect with background color */
  185. RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(btn)) = RTGUI_RGB(0xff, 0xff, 0xff);
  186. rtgui_dc_fill_rect(dc, &rect);
  187. /* draw border */
  188. RTGUI_WIDGET(btn)->gc.foreground = RTGUI_RGB(0, 0, 0);
  189. rtgui_dc_draw_rect(dc, &rect);
  190. RTGUI_WIDGET(btn)->gc.foreground = RTGUI_RGB(128, 128, 128);
  191. rect.x1 += 1; rect.y1 += 1; rect.x2 -= 1; rect.y2 -= 1;
  192. rtgui_dc_draw_rect(dc, &rect);
  193. }
  194. }
  195. else
  196. {
  197. if (btn->unpressed_image != RT_NULL)
  198. {
  199. rtgui_rect_t image_rect;
  200. image_rect.x1 = 0; image_rect.y1 = 0;
  201. image_rect.x2 = btn->unpressed_image->w;
  202. image_rect.y2 = btn->unpressed_image->h;
  203. rtgui_rect_moveto_align(&rect, &image_rect, RTGUI_ALIGN_CENTER_HORIZONTAL | RTGUI_ALIGN_CENTER_VERTICAL);
  204. rtgui_image_blit(btn->unpressed_image, dc, &image_rect);
  205. }
  206. else
  207. {
  208. /* fill button rect with background color */
  209. rtgui_dc_fill_rect(dc, &rect);
  210. /* draw border */
  211. RTGUI_WIDGET(btn)->gc.foreground = RTGUI_RGB(255, 255, 255);
  212. rtgui_dc_draw_hline(dc, rect.x1, rect.x2, rect.y1);
  213. rtgui_dc_draw_vline(dc, rect.x1, rect.y1, rect.y2);
  214. RTGUI_WIDGET(btn)->gc.foreground = RTGUI_RGB(0, 0, 0);
  215. rtgui_dc_draw_hline(dc, rect.x1, rect.x2 + 1, rect.y2);
  216. rtgui_dc_draw_vline(dc, rect.x2, rect.y1, rect.y2);
  217. RTGUI_WIDGET(btn)->gc.foreground = RTGUI_RGB(128, 128, 128);
  218. rtgui_dc_draw_hline(dc, rect.x1 + 1, rect.x2, rect.y2 - 1);
  219. rtgui_dc_draw_vline(dc, rect.x2 - 1, rect.y1 + 1, rect.y2 - 1);
  220. }
  221. }
  222. if (RTGUI_WIDGET_IS_FOCUSED(RTGUI_WIDGET(btn)))
  223. {
  224. /* re-set foreground and get default rect */
  225. rtgui_widget_get_rect(RTGUI_WIDGET(btn), &rect);
  226. rtgui_rect_inflate(&rect, -2);
  227. RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(btn)) = black;
  228. rtgui_dc_draw_focus_rect(dc, &rect);
  229. }
  230. /* set forecolor */
  231. RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(btn)) = bc;
  232. RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(btn)) = fc;
  233. if (btn->pressed_image == RT_NULL)
  234. {
  235. /* re-set foreground and get default rect */
  236. rtgui_widget_get_rect(RTGUI_WIDGET(btn), &rect);
  237. /* remove border */
  238. rtgui_rect_inflate(&rect, -2);
  239. /* draw text */
  240. rtgui_dc_draw_text(dc, rtgui_label_get_text(RTGUI_LABEL(btn)), &rect);
  241. }
  242. /* end drawing */
  243. rtgui_dc_end_drawing(dc);
  244. }
  245. void rtgui_theme_draw_label(rtgui_label_t* label)
  246. {
  247. /* draw label */
  248. struct rtgui_dc* dc;
  249. struct rtgui_rect rect;
  250. /* begin drawing */
  251. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(label));
  252. if (dc == RT_NULL) return;
  253. rtgui_widget_get_rect(RTGUI_WIDGET(label), &rect);
  254. rtgui_dc_fill_rect(dc, &rect);
  255. /* default left and center draw */
  256. rtgui_dc_draw_text(dc, rtgui_label_get_text(label), &rect);
  257. /* end drawing */
  258. rtgui_dc_end_drawing(dc);
  259. }
  260. #define RTGUI_TEXTBOX_MARGIN 3
  261. void rtgui_theme_draw_textbox(rtgui_textbox_t* box)
  262. {
  263. /* draw button */
  264. struct rtgui_dc* dc;
  265. struct rtgui_rect rect;
  266. rtgui_color_t fc;
  267. /* begin drawing */
  268. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(box));
  269. if (dc == RT_NULL) return;
  270. /* get widget rect */
  271. rtgui_widget_get_rect(RTGUI_WIDGET(box), &rect);
  272. fc = RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(box));
  273. /* fill widget rect with white color */
  274. RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(box)) = white;
  275. rtgui_dc_fill_rect(dc, &rect);
  276. /* draw border */
  277. RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(box)) = RTGUI_RGB(123, 158, 189);
  278. rtgui_dc_draw_rect(dc, &rect);
  279. /* draw text */
  280. RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(box)) = fc;
  281. if (box->text != RT_NULL)
  282. {
  283. rect.x1 += RTGUI_TEXTBOX_MARGIN;
  284. if (box->flag & RTGUI_TEXTBOX_MASK)
  285. {
  286. /* draw '*' */
  287. rt_size_t len = rt_strlen(box->text);
  288. if (len > 0)
  289. {
  290. char *text_mask = rtgui_malloc(len + 1);
  291. rt_memset(text_mask, '*', len + 1);
  292. text_mask[len] = 0;
  293. rtgui_dc_draw_text(dc, text_mask, &rect);
  294. rt_free(text_mask);
  295. }
  296. }
  297. else
  298. {
  299. rtgui_dc_draw_text(dc, box->text, &rect);
  300. }
  301. /* draw caret */
  302. if (box->flag & RTGUI_TEXTBOX_CARET_SHOW)
  303. {
  304. rect.x1 += box->position * box->font_width;
  305. rect.x2 = rect.x1 + box->font_width;
  306. rect.y2 -= 2;
  307. rect.y1 = rect.y2 - 3;
  308. RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(box)) = black;
  309. rtgui_dc_fill_rect(dc, &rect);
  310. }
  311. }
  312. /* end drawing */
  313. rtgui_dc_end_drawing(dc);
  314. }
  315. void rtgui_theme_draw_iconbox(rtgui_iconbox_t* iconbox)
  316. {
  317. struct rtgui_dc* dc;
  318. struct rtgui_rect rect;
  319. /* begin drawing */
  320. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(iconbox));
  321. if (dc == RT_NULL) return;
  322. /* get widget rect */
  323. rtgui_widget_get_rect(RTGUI_WIDGET(iconbox), &rect);
  324. /* draw icon */
  325. rtgui_image_blit(iconbox->image, dc, &rect);
  326. /* draw text */
  327. if (iconbox->text_position == RTGUI_ICONBOX_TEXT_BELOW && iconbox->text != RT_NULL)
  328. {
  329. rect.y1 = iconbox->image->h + RTGUI_WIDGET_DEFAULT_MARGIN;
  330. rtgui_dc_draw_text(dc, iconbox->text, &rect);
  331. }
  332. else if (iconbox->text_position == RTGUI_ICONBOX_TEXT_RIGHT && iconbox->text != RT_NULL)
  333. {
  334. rect.x1 = iconbox->image->w + RTGUI_WIDGET_DEFAULT_MARGIN;
  335. rtgui_dc_draw_text(dc, iconbox->text, &rect);
  336. }
  337. /* end drawing */
  338. rtgui_dc_end_drawing(dc);
  339. }
  340. #define CHECK_BOX_W 13
  341. #define CHECK_BOX_H 13
  342. static const rt_uint8_t checked_byte[7] = {0x02, 0x06, 0x8E, 0xDC, 0xF8, 0x70, 0x20};
  343. void rtgui_theme_draw_checkbox(struct rtgui_checkbox* checkbox)
  344. {
  345. struct rtgui_dc* dc;
  346. struct rtgui_rect rect, box_rect;
  347. rtgui_color_t bc, fc;
  348. fc = RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(checkbox));
  349. bc = RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(checkbox));
  350. /* begin drawing */
  351. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(checkbox));
  352. if (dc == RT_NULL) return;
  353. /* get rect */
  354. rtgui_widget_get_rect(RTGUI_WIDGET(checkbox), &rect);
  355. /* fill rect */
  356. rtgui_dc_fill_rect(dc, &rect);
  357. if (RTGUI_WIDGET_IS_FOCUSED(RTGUI_WIDGET(checkbox)))
  358. {
  359. RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(checkbox)) = black;
  360. /* draw focused border */
  361. rtgui_rect_inflate(&rect, -1);
  362. rtgui_dc_draw_focus_rect(dc, &rect);
  363. rtgui_rect_inflate(&rect, 1);
  364. }
  365. /* draw check box */
  366. box_rect.x1 = 0;
  367. box_rect.y1 = 0;
  368. box_rect.x2 = CHECK_BOX_W;
  369. box_rect.y2 = CHECK_BOX_H;
  370. rtgui_rect_moveto_align(&rect, &box_rect, RTGUI_ALIGN_CENTER_VERTICAL);
  371. box_rect.x1 += 2; box_rect.x2 += 2;
  372. rtgui_dc_draw_border(dc, &box_rect, RTGUI_BORDER_BOX);
  373. rtgui_rect_inflate(&box_rect, -1);
  374. RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(checkbox)) = RTGUI_RGB(247, 247, 246);
  375. rtgui_dc_fill_rect(dc, &box_rect);
  376. if (checkbox->status_down == RTGUI_CHECKBOX_STATUS_CHECKED)
  377. {
  378. RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(checkbox)) = RTGUI_RGB(33, 161, 33);
  379. rtgui_dc_draw_byte(dc, box_rect.x1 + 2, box_rect.y1 + 2, 7, checked_byte);
  380. }
  381. /* restore saved color */
  382. RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(checkbox)) = bc;
  383. RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(checkbox)) = fc;
  384. /* draw text */
  385. rect.x1 += rtgui_rect_height(rect) - 4 + 5;
  386. rtgui_dc_draw_text(dc, rtgui_label_get_text(RTGUI_LABEL(checkbox)), &rect);
  387. /* end drawing */
  388. rtgui_dc_end_drawing(dc);
  389. return;
  390. }
  391. void rtgui_theme_draw_radiobox(struct rtgui_radiobox* radiobox)
  392. {
  393. struct rtgui_dc* dc;
  394. struct rtgui_rect rect, item_rect;
  395. rt_size_t item_size, bord_size, index;
  396. rtgui_color_t fc;
  397. /* begin drawing */
  398. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(radiobox));
  399. if (dc == RT_NULL) return;
  400. /* get widget rect */
  401. rtgui_widget_get_rect(RTGUI_WIDGET(radiobox), &rect);
  402. rtgui_dc_fill_rect(dc, &rect);
  403. item_size = radiobox->item_size;
  404. /* get board size */
  405. if (radiobox->orient == RTGUI_VERTICAL)
  406. bord_size = item_size;
  407. else
  408. {
  409. rtgui_font_get_metrics(rtgui_dc_get_font(dc), "H", &item_rect);
  410. bord_size = rtgui_rect_height(item_rect);
  411. }
  412. /* draw box */
  413. rtgui_rect_inflate(&rect, -bord_size/2);
  414. fc = RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(radiobox));
  415. RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(radiobox)) = white;
  416. rect.x1 ++; rect.y1 ++; rect.x2 ++; rect.y2 ++;
  417. rtgui_dc_draw_rect(dc, &rect);
  418. RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(radiobox)) = RTGUI_RGB(128, 128, 128);
  419. rect.x1 --; rect.y1 --; rect.x2 --; rect.y2 --;
  420. rtgui_dc_draw_rect(dc, &rect);
  421. RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(radiobox)) = fc;
  422. rtgui_rect_inflate(&rect, bord_size/2);
  423. if (radiobox->text != RT_NULL)
  424. {
  425. struct rtgui_rect text_rect;
  426. /* draw group text */
  427. rtgui_font_get_metrics(rtgui_dc_get_font(dc), radiobox->text, &text_rect);
  428. rtgui_rect_moveto(&text_rect, rect.x1 + bord_size + 5, rect.y1);
  429. rect.x1 -= 5; rect.x2 += 5;
  430. rtgui_dc_fill_rect(dc, &text_rect);
  431. rect.x1 += 5; rect.x2 -= 5;
  432. rtgui_dc_draw_text(dc, radiobox->text, &text_rect);
  433. }
  434. /* set init item rect */
  435. item_rect = rect;
  436. rtgui_rect_inflate(&item_rect, - bord_size);
  437. if (radiobox->orient == RTGUI_VERTICAL)
  438. {
  439. /* set the first text rect */
  440. item_rect.y2 = item_rect.y1 + item_size;
  441. /* draw each radio button */
  442. for (index = 0; index < radiobox->item_count; index ++)
  443. {
  444. if (item_rect.y2 > rect.y2 - item_size) break;
  445. /* draw radio */
  446. if (radiobox->item_selection == index)
  447. {
  448. if (RTGUI_WIDGET_IS_FOCUSED(RTGUI_WIDGET(radiobox)))
  449. rtgui_dc_draw_focus_rect(dc, &item_rect);
  450. rtgui_dc_draw_circle(dc, item_rect.x1 + item_size/2 + 2, item_rect.y1 + item_size/2 + 2, item_size/2 - 2);
  451. rtgui_dc_fill_circle(dc, item_rect.x1 + item_size/2 + 2, item_rect.y1 + item_size/2 + 2, item_size/2 - 4);
  452. }
  453. else
  454. {
  455. rtgui_dc_draw_circle(dc, item_rect.x1 + item_size/2 + 2, item_rect.y1 + item_size/2 + 2, item_size/2 - 2);
  456. }
  457. /* draw text */
  458. item_rect.x1 += item_size + 3;
  459. rtgui_dc_draw_text(dc, radiobox->items[index], &item_rect);
  460. item_rect.x1 -= item_size + 3;
  461. item_rect.y1 += item_size;
  462. item_rect.y2 += item_size;
  463. }
  464. }
  465. else
  466. {
  467. /* set the first text rect */
  468. item_rect.x2 = item_rect.x1 + item_size;
  469. item_rect.y2 = item_rect.y1 + bord_size;
  470. /* draw each radio button */
  471. for (index = 0; index < radiobox->item_count; index ++)
  472. {
  473. if (item_rect.x2 > rect.x2 - item_size) break;
  474. /* draw radio */
  475. if (radiobox->item_selection == index)
  476. {
  477. rtgui_dc_draw_focus_rect(dc, &item_rect);
  478. rtgui_dc_draw_circle(dc, item_rect.x1 + bord_size/2 + 2, item_rect.y1 + bord_size/2 + 2, bord_size/2 - 2);
  479. rtgui_dc_fill_circle(dc, item_rect.x1 + bord_size/2 + 2, item_rect.y1 + bord_size/2 + 2, bord_size/2 - 4);
  480. }
  481. else
  482. {
  483. rtgui_dc_draw_circle(dc, item_rect.x1 + bord_size/2 + 2, item_rect.y1 + bord_size/2 + 2, bord_size/2 - 2);
  484. }
  485. /* draw text */
  486. item_rect.x1 += bord_size + 3;
  487. rtgui_dc_draw_text(dc, radiobox->items[index], &item_rect);
  488. item_rect.x1 -= bord_size + 3;
  489. item_rect.x1 += item_size;
  490. item_rect.x2 += item_size;
  491. }
  492. }
  493. /* end drawing */
  494. rtgui_dc_end_drawing(dc);
  495. }
  496. void rtgui_theme_draw_slider(struct rtgui_slider* slider)
  497. {
  498. /* draw button */
  499. struct rtgui_dc* dc;
  500. int i, xsize, x0;
  501. rtgui_rect_t r, focus_rect, slider_rect, slot_rect;
  502. /* begin drawing */
  503. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(slider));
  504. if (dc == RT_NULL) return;
  505. /* get widget rect */
  506. rtgui_widget_get_rect(RTGUI_WIDGET(slider), &focus_rect);
  507. /* fill widget rect with background color */
  508. rtgui_dc_fill_rect(dc, &focus_rect);
  509. r = focus_rect;
  510. if (slider->orient == RTGUI_VERTICAL)
  511. {
  512. rtgui_rect_inflate(&r, -1);
  513. xsize = r.y2 - r.y1 + 1 - slider->thumb_width;
  514. x0 = r.y1 + slider->thumb_width / 2;
  515. /* calculate thumb position */
  516. slider_rect = r;
  517. slider_rect.x1 = 5;
  518. slider_rect.y1 = x0 + xsize * (slider->value - slider->min) / (slider->max - slider->min) - slider->thumb_width/2;
  519. slider_rect.y2 = slider_rect.y1 + slider->thumb_width;
  520. /* calculate slot position */
  521. slot_rect.y1 = x0;
  522. slot_rect.y2 = x0 + xsize;
  523. slot_rect.x1 = (slider_rect.x1 + slider_rect.x2) /2 -1;
  524. slot_rect.x2 = slot_rect.x1 +3;
  525. /* draw slot */
  526. rtgui_dc_draw_border(dc, &slot_rect, RTGUI_BORDER_RAISE);
  527. /* draw the ticks */
  528. for (i = 0; i <= slider->ticks; i++)
  529. {
  530. int x = x0 + xsize * i / slider->ticks;
  531. rtgui_dc_draw_hline(dc, 1, 3, x);
  532. }
  533. /* draw the thumb */
  534. rtgui_dc_fill_rect(dc, &slider_rect);
  535. rtgui_dc_draw_border(dc, &slider_rect, RTGUI_BORDER_RAISE);
  536. }
  537. else
  538. {
  539. rtgui_rect_inflate(&r, -1);
  540. xsize = r.x2 - r.x1 + 1 - slider->thumb_width;
  541. x0 = r.x1 + slider->thumb_width / 2;
  542. /* calculate thumb position */
  543. slider_rect = r;
  544. slider_rect.y1 = 5;
  545. slider_rect.x1 = x0 + xsize * (slider->value - slider->min) / (slider->max - slider->min) - slider->thumb_width/2;
  546. slider_rect.x2 = slider_rect.x1 + slider->thumb_width;
  547. /* calculate slot position */
  548. slot_rect.x1 = x0;
  549. slot_rect.x2 = x0 + xsize;
  550. slot_rect.y1 = (slider_rect.y1 + slider_rect.y2) /2 -1;
  551. slot_rect.y2 = slot_rect.y1 +3;
  552. /* draw slot */
  553. rtgui_dc_draw_border(dc, &slot_rect, RTGUI_BORDER_RAISE);
  554. /* draw the ticks */
  555. for (i = 0; i <= slider->ticks; i++)
  556. {
  557. int x = x0 + xsize * i / slider->ticks;
  558. rtgui_dc_draw_vline(dc, x, 1, 3);
  559. }
  560. /* draw the thumb */
  561. rtgui_dc_fill_rect(dc, &slider_rect);
  562. rtgui_dc_draw_border(dc, &slider_rect, RTGUI_BORDER_RAISE);
  563. }
  564. /* draw focus */
  565. if (RTGUI_WIDGET_IS_FOCUSED(RTGUI_WIDGET(slider)))
  566. {
  567. rtgui_dc_draw_focus_rect(dc, &focus_rect);
  568. }
  569. /* end drawing */
  570. rtgui_dc_end_drawing(dc);
  571. return;
  572. }
  573. void rtgui_theme_draw_progressbar(struct rtgui_progressbar* bar)
  574. {
  575. /* draw progress bar */
  576. struct rtgui_dc* dc;
  577. struct rtgui_rect rect;
  578. int max = bar->range;
  579. int pos = bar->position;
  580. int left;
  581. /* begin drawing */
  582. dc = rtgui_dc_begin_drawing(&(bar->parent));
  583. if (dc == RT_NULL) return;
  584. rtgui_widget_get_rect(&(bar->parent), &rect);
  585. /* fill button rect with background color */
  586. bar->parent.gc.background = RTGUI_RGB(212, 208, 200);
  587. rtgui_dc_fill_rect(dc, &rect);
  588. /* draw border */
  589. bar->parent.gc.foreground = RTGUI_RGB(128, 128, 128);
  590. rtgui_dc_draw_hline(dc, rect.x1, rect.x2 - 1, rect.y1);
  591. rtgui_dc_draw_vline(dc, rect.x1, rect.y1, rect.y2 - 1);
  592. bar->parent.gc.foreground = RTGUI_RGB(64, 64, 64);
  593. rtgui_dc_draw_hline(dc, rect.x1, rect.x2, rect.y1 + 1);
  594. rtgui_dc_draw_vline(dc, rect.x1 + 1, rect.y1, rect.y2);
  595. bar->parent.gc.foreground = RTGUI_RGB(212, 208, 200);
  596. rtgui_dc_draw_hline(dc, rect.x1, rect.x2 + 1, rect.y2);
  597. rtgui_dc_draw_vline(dc, rect.x2, rect.y1, rect.y2);
  598. bar->parent.gc.foreground = RTGUI_RGB(255, 255, 255);
  599. rtgui_dc_draw_hline(dc, rect.x1 + 1, rect.x2, rect.y2 - 1);
  600. rtgui_dc_draw_vline(dc, rect.x2 - 1, rect.y1 + 1, rect.y2 - 1);
  601. /* Nothing to draw */
  602. if (max == 0)
  603. {
  604. rtgui_dc_end_drawing(dc);
  605. return;
  606. }
  607. left = max - pos;
  608. rtgui_rect_inflate(&rect, -2);
  609. bar->parent.gc.background = RTGUI_RGB(0, 0, 255);
  610. if (bar->orientation == RTGUI_VERTICAL)
  611. {
  612. /* Vertical bar grows from bottom to top */
  613. int dy = (rtgui_rect_height(rect) * left) / max;
  614. rect.y1 += dy;
  615. rtgui_dc_fill_rect(dc, &rect);
  616. }
  617. else
  618. {
  619. /* Horizontal bar grows from left to right */
  620. rect.x2 -= (rtgui_rect_width(rect) * left) / max;
  621. rtgui_dc_fill_rect(dc, &rect);
  622. }
  623. /* end drawing */
  624. rtgui_dc_end_drawing(dc);
  625. return;
  626. }
  627. void rtgui_theme_draw_staticline(struct rtgui_staticline* staticline)
  628. {
  629. struct rtgui_dc* dc;
  630. struct rtgui_rect rect;
  631. /* begin drawing */
  632. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(staticline));
  633. if (dc == RT_NULL) return ;
  634. rtgui_widget_get_rect(RTGUI_WIDGET(staticline), &rect);
  635. rtgui_dc_fill_rect(dc, &rect);
  636. if (staticline->orientation == RTGUI_HORIZONTAL)
  637. {
  638. rtgui_dc_draw_horizontal_line(dc, rect.x1, rect.x2, rect.y1);
  639. }
  640. else
  641. {
  642. rtgui_dc_draw_vertical_line(dc, rect.x1, rect.y1, rect.y2);
  643. }
  644. rtgui_dc_end_drawing(dc);
  645. }
  646. rt_uint16_t rtgui_theme_get_selected_height()
  647. {
  648. return SELECTED_HEIGHT;
  649. }
  650. void rtgui_theme_draw_selected(struct rtgui_dc* dc, rtgui_rect_t *rect)
  651. {
  652. rtgui_color_t bc;
  653. rt_uint16_t index;
  654. bc = rtgui_dc_get_color(dc);
  655. rtgui_dc_set_color(dc, selected_color);
  656. rtgui_dc_draw_hline(dc, rect->x1 + 3, rect->x2 - 2, rect->y1 + 1);
  657. rtgui_dc_draw_hline(dc, rect->x1 + 3, rect->x2 - 2, rect->y2 - 2);
  658. rtgui_dc_draw_vline(dc, rect->x1 + 2, rect->y1 + 2, rect->y2 - 2);
  659. rtgui_dc_draw_vline(dc, rect->x2 - 2, rect->y1 + 2, rect->y2 - 2);
  660. for (index = rect->y1 + 1; index < rect->y2 - 2; index ++)
  661. rtgui_dc_draw_hline(dc, rect->x1 + 3, rect->x2 - 2, index);
  662. rtgui_dc_set_color(dc, bc);
  663. }
  664. /* get default background color */
  665. rtgui_color_t rtgui_theme_default_bc()
  666. {
  667. return default_background;
  668. }
  669. /* get default foreground color */
  670. rtgui_color_t rtgui_theme_default_fc()
  671. {
  672. return default_foreground;
  673. }