rtgui_theme.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. #define WINTITLE_CB_WIDTH 14
  25. #define WINTITLE_CB_HEIGHT 14
  26. #define SELECTED_HEIGHT 25
  27. static const rt_uint8_t *close_unpressed_xpm[] = {
  28. "14 14 55 1",
  29. " c None",
  30. ". c #DCDFEA",
  31. "+ c #A4ADD3",
  32. "@ c #8F9ACA",
  33. "# c #98A2CD",
  34. "$ c #D2D6E6",
  35. "% c #F7F7F7",
  36. "& c #F6F6F6",
  37. "* c #B9C1D0",
  38. "= c #7A8AAA",
  39. "- c #D6DAE2",
  40. "; c #D8DCE3",
  41. "> c #7485A5",
  42. ", c #455C89",
  43. "' c #516690",
  44. ") c #D3D8E0",
  45. "! c #536891",
  46. "~ c #6D7FA1",
  47. "{ c #F5F5F5",
  48. "] c #D1D6DF",
  49. "^ c #D2D7DF",
  50. "/ c #D5D9E1",
  51. "( c #4E648E",
  52. "_ c #CFD4DE",
  53. ": c #F4F4F4",
  54. "< c #D0D5DE",
  55. "[ c #CED3DD",
  56. "} c #F3F3F3",
  57. "| c #CFD4DD",
  58. "1 c #CDD2DC",
  59. "2 c #F2F2F2",
  60. "3 c #D3D7DF",
  61. "4 c #526790",
  62. "5 c #D0D5DD",
  63. "6 c #F1F1F1",
  64. "7 c #D2D6DE",
  65. "8 c #CFD4DC",
  66. "9 c #F0F0F0",
  67. "0 c #D1D5DD",
  68. "a c #C9CED8",
  69. "b c #CDD2DB",
  70. "c c #50658F",
  71. "d c #CED3DB",
  72. "e c #7283A3",
  73. "f c #6E80A2",
  74. "g c #EFEFEF",
  75. "h c #B2BACA",
  76. "i c #7081A2",
  77. "j c #C8CDD7",
  78. "k c #CCD1DA",
  79. "l c #ACB5C7",
  80. "m c #D0D4E2",
  81. "n c #EEEEEE",
  82. "o c #D2D5E3",
  83. "p c #97A1CC",
  84. ".+@@@@@@@@@@+.",
  85. "#$%%%%%%%%%%$#",
  86. "@&*=-&&&&;=*&@",
  87. "@&>,')&&-!,~&@",
  88. "@{]','^/!,(_{@",
  89. "@::<','!,([::@",
  90. "@}}}|',,(1}}}@",
  91. "@22234,,'5222@",
  92. "@6674,(','866@",
  93. "@904,(abc,cd9@",
  94. "@9e,(a99bc,f9@",
  95. "@ghijggggkelg@",
  96. "#mnnnnnnnnnnm#",
  97. "op@@@@@@@@@@po"};
  98. static const rt_uint8_t *close_pressed_xpm[] = {
  99. "14 14 66 1",
  100. " c None",
  101. ". c #CED4EE",
  102. "+ c #7E90DD",
  103. "@ c #6076D7",
  104. "# c #6C80D9",
  105. "$ c #BFC8EA",
  106. "% c #F2F3F5",
  107. "& c #F0F2F3",
  108. "* c #A5B6D7",
  109. "= c #587ABB",
  110. "- c #C9D3E4",
  111. "; c #CBD5E4",
  112. "> c #EEF0F2",
  113. ", c #5073B7",
  114. "' c #1746A3",
  115. ") c #2551A8",
  116. "! c #C5CFE2",
  117. "~ c #C8D1E3",
  118. "{ c #2853A8",
  119. "] c #496DB4",
  120. "^ c #ECEEF1",
  121. "/ c #C0CCE0",
  122. "( c #C3CEE1",
  123. "_ c #C6D0E2",
  124. ": c #224FA7",
  125. "< c #BEC9DF",
  126. "[ c #EAECF0",
  127. "} c #BFCAE0",
  128. "| c #2551A7",
  129. "1 c #224EA7",
  130. "2 c #BCC8DF",
  131. "3 c #E8EBEE",
  132. "4 c #BDCADE",
  133. "5 c #BAC7DD",
  134. "6 c #E6E9ED",
  135. "7 c #C1CBDF",
  136. "8 c #2753A8",
  137. "9 c #BECADE",
  138. "0 c #E4E7EB",
  139. "a c #BFCADD",
  140. "b c #224EA6",
  141. "c c #BDC8DC",
  142. "d c #E1E5EA",
  143. "e c #2752A8",
  144. "f c #B3C0D9",
  145. "g c #B8C5DB",
  146. "h c #2451A7",
  147. "i c #BAC6DB",
  148. "j c #DFE2E8",
  149. "k c #4C70B4",
  150. "l c #B2BED8",
  151. "m c #B6C2D9",
  152. "n c #2450A7",
  153. "o c #486BB3",
  154. "p c #DCE0E7",
  155. "q c #96A8CE",
  156. "r c #496CB3",
  157. "s c #AFBCD7",
  158. "t c #B4C1D8",
  159. "u c #4B6FB4",
  160. "v c #8EA4CC",
  161. "w c #6C80D8",
  162. "x c #B4BEDF",
  163. "y c #DADEE5",
  164. "z c #B5BEDE",
  165. "A c #6A7ED7",
  166. ".+@@@@@@@@@@+.",
  167. "#$%%%%%%%%%%$#",
  168. "@&*=-&&&&;=*&@",
  169. "@>,')!>>~{']>@",
  170. "@^/)')(_{':<^@",
  171. "@[[}|'|{'12[[@",
  172. "@3334|''15333@",
  173. "@66678''|9666@",
  174. "@00a8'b|'|c00@",
  175. "@dce'bfgh'hid@",
  176. "@jk'bljjmn'oj@",
  177. "@pqrspppptuvp@",
  178. "wxyyyyyyyyyyxw",
  179. "zA@@@@@@@@@@Az"};
  180. static rtgui_image_t* close_pressed = RT_NULL;
  181. static rtgui_image_t* close_unpressed = RT_NULL;
  182. /* init theme */
  183. void rtgui_system_theme_init()
  184. {
  185. close_pressed = rtgui_image_create_from_mem("xpm",
  186. (const rt_uint8_t*)close_pressed_xpm, sizeof(close_pressed_xpm), RT_TRUE);
  187. close_unpressed = rtgui_image_create_from_mem("xpm",
  188. (const rt_uint8_t*)close_unpressed_xpm, sizeof(close_unpressed_xpm), RT_TRUE);
  189. }
  190. /* window drawing */
  191. void rtgui_theme_draw_win(struct rtgui_topwin* win)
  192. {
  193. struct rtgui_dc* dc;
  194. rtgui_rect_t rect;
  195. /* init close box image */
  196. if (close_pressed == RT_NULL)
  197. close_pressed = rtgui_image_create_from_mem("xpm",
  198. (const rt_uint8_t*)close_pressed_xpm, sizeof(close_pressed_xpm), RT_TRUE);
  199. if (close_unpressed == RT_NULL)
  200. close_unpressed = rtgui_image_create_from_mem("xpm",
  201. (const rt_uint8_t*)close_unpressed_xpm, sizeof(close_unpressed_xpm), RT_TRUE);
  202. /* begin drawing */
  203. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(win->title));
  204. /* get rect */
  205. rtgui_widget_get_rect(RTGUI_WIDGET(win->title), &rect);
  206. /* draw border */
  207. if (win->flag & WINTITLE_BORDER)
  208. {
  209. RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(win->title)) = RTGUI_RGB(219, 210, 243);
  210. rtgui_dc_draw_rect(dc, &rect);
  211. /* shrink border */
  212. rect.x1 += WINTITLE_BORDER_SIZE;
  213. rect.y1 += WINTITLE_BORDER_SIZE;
  214. rect.x2 -= WINTITLE_BORDER_SIZE;
  215. rect.y2 -= WINTITLE_BORDER_SIZE;
  216. }
  217. /* draw title */
  218. if (!(win->flag & WINTITLE_NO))
  219. {
  220. if (win->flag & WINTITLE_ACTIVATE)
  221. {
  222. RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(win->title)) = RTGUI_RGB(229, 236, 249);
  223. RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(win->title)) = RTGUI_RGB( 51, 102, 204);
  224. }
  225. else
  226. {
  227. RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(win->title)) = RTGUI_RGB(242, 245, 252);
  228. RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(win->title)) = RTGUI_RGB(153, 178, 229);
  229. }
  230. rtgui_dc_fill_rect(dc, &rect);
  231. rect.x1 += 4;
  232. rect.y1 += 2;
  233. rtgui_dc_draw_text(dc, rtgui_wintitle_get_title(win->title), &rect);
  234. if (win->flag & WINTITLE_CLOSEBOX)
  235. {
  236. /* get close button rect */
  237. rect.x1 = rtgui_rect_width(RTGUI_WIDGET(win->title)->extent) -
  238. WINTITLE_BORDER_SIZE - WINTITLE_CB_WIDTH - 3;
  239. rect.y1 = 3;
  240. rect.x2 = rect.x1 + WINTITLE_CB_WIDTH;
  241. rect.y2 = rect.y1 + WINTITLE_CB_HEIGHT;
  242. /* draw close box */
  243. if(win->flag & WINTITLE_CB_PRESSED) rtgui_image_blit(close_pressed, dc, &rect);
  244. else rtgui_image_blit(close_unpressed, dc, &rect);
  245. }
  246. }
  247. rtgui_dc_end_drawing(dc);
  248. }
  249. /* widget drawing */
  250. void rtgui_theme_draw_button(rtgui_button_t* btn)
  251. {
  252. /* draw button */
  253. struct rtgui_dc* dc;
  254. struct rtgui_rect rect;
  255. /* begin drawing */
  256. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(btn));
  257. if (dc == RT_NULL) return;
  258. rtgui_widget_get_rect(RTGUI_WIDGET(btn), &rect);
  259. /* fill button rect with background color */
  260. // RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(btn)) = RTGUI_RGB(212, 208, 200);
  261. rtgui_dc_fill_rect(dc, &rect);
  262. if (btn->flag & RTGUI_BUTTON_TYPE_PUSH && btn->flag & RTGUI_BUTTON_FLAG_PRESS)
  263. {
  264. /* draw border */
  265. RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(btn)) = RTGUI_RGB(64, 64, 64);
  266. rtgui_dc_draw_hline(dc, rect.x1, rect.x2, rect.y1);
  267. rtgui_dc_draw_vline(dc, rect.x1, rect.y1, rect.y2);
  268. RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(btn)) = RTGUI_RGB(128, 128, 128);
  269. rtgui_dc_draw_hline(dc, rect.x1, rect.x2 - 1, rect.y1 + 1);
  270. rtgui_dc_draw_vline(dc, rect.x1 + 1, rect.y1 + 1, rect.y2 - 2);
  271. RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(btn)) = RTGUI_RGB(255, 255, 255);
  272. rtgui_dc_draw_hline(dc, rect.x1, rect.x2 + 1, rect.y2 - 1);
  273. rtgui_dc_draw_vline(dc, rect.x2 - 1, rect.y1, rect.y2);
  274. if (btn->pressed_image != RT_NULL)
  275. {
  276. rtgui_rect_t image_rect;
  277. image_rect.x1 = 0; image_rect.y1 = 0;
  278. image_rect.x2 = btn->unpressed_image->w;
  279. image_rect.y2 = btn->unpressed_image->h;
  280. rtgui_rect_moveto_align(&rect, &image_rect, RTGUI_ALIGN_CENTER_HORIZONTAL | RTGUI_ALIGN_CENTER_VERTICAL);
  281. rtgui_image_blit(btn->pressed_image, dc, &image_rect);
  282. }
  283. }
  284. else if (btn->flag & RTGUI_BUTTON_FLAG_PRESS)
  285. {
  286. if (btn->pressed_image != RT_NULL)
  287. {
  288. rtgui_rect_t image_rect;
  289. image_rect.x1 = 0; image_rect.y1 = 0;
  290. image_rect.x2 = btn->unpressed_image->w;
  291. image_rect.y2 = btn->unpressed_image->h;
  292. rtgui_rect_moveto_align(&rect, &image_rect, RTGUI_ALIGN_CENTER_HORIZONTAL | RTGUI_ALIGN_CENTER_VERTICAL);
  293. rtgui_image_blit(btn->pressed_image, dc, &image_rect);
  294. }
  295. else
  296. {
  297. /* draw border */
  298. RTGUI_WIDGET(btn)->gc.foreground = RTGUI_RGB(0, 0, 0);
  299. rtgui_dc_draw_rect(dc, &rect);
  300. RTGUI_WIDGET(btn)->gc.foreground = RTGUI_RGB(128, 128, 128);
  301. rect.x1 += 1; rect.y1 += 1; rect.x2 -= 1; rect.y2 -= 1;
  302. rtgui_dc_draw_rect(dc, &rect);
  303. }
  304. }
  305. else
  306. {
  307. if (btn->unpressed_image != RT_NULL)
  308. {
  309. rtgui_rect_t image_rect;
  310. image_rect.x1 = 0; image_rect.y1 = 0;
  311. image_rect.x2 = btn->unpressed_image->w;
  312. image_rect.y2 = btn->unpressed_image->h;
  313. rtgui_rect_moveto_align(&rect, &image_rect, RTGUI_ALIGN_CENTER_HORIZONTAL | RTGUI_ALIGN_CENTER_VERTICAL);
  314. rtgui_image_blit(btn->unpressed_image, dc, &image_rect);
  315. }
  316. else
  317. {
  318. /* draw border */
  319. RTGUI_WIDGET(btn)->gc.foreground = RTGUI_RGB(255, 255, 255);
  320. rtgui_dc_draw_hline(dc, rect.x1, rect.x2, rect.y1);
  321. rtgui_dc_draw_vline(dc, rect.x1, rect.y1, rect.y2);
  322. RTGUI_WIDGET(btn)->gc.foreground = RTGUI_RGB(0, 0, 0);
  323. rtgui_dc_draw_hline(dc, rect.x1, rect.x2 + 1, rect.y2);
  324. rtgui_dc_draw_vline(dc, rect.x2, rect.y1, rect.y2);
  325. RTGUI_WIDGET(btn)->gc.foreground = RTGUI_RGB(128, 128, 128);
  326. rtgui_dc_draw_hline(dc, rect.x1 + 1, rect.x2, rect.y2 - 1);
  327. rtgui_dc_draw_vline(dc, rect.x2 - 1, rect.y1 + 1, rect.y2 - 1);
  328. }
  329. }
  330. if (btn->pressed_image == RT_NULL)
  331. {
  332. /* re-set foreground and get default rect */
  333. RTGUI_WIDGET(btn)->gc.foreground = RTGUI_RGB(0, 0, 0);
  334. rtgui_widget_get_rect(RTGUI_WIDGET(btn), &rect);
  335. /* remove border */
  336. rtgui_rect_inflate(&rect, -2);
  337. /* draw text */
  338. rtgui_dc_draw_text(dc, rtgui_label_get_text(RTGUI_LABEL(btn)), &rect);
  339. }
  340. /* end drawing */
  341. rtgui_dc_end_drawing(dc);
  342. }
  343. void rtgui_theme_draw_label(rtgui_label_t* label)
  344. {
  345. /* draw label */
  346. struct rtgui_dc* dc;
  347. struct rtgui_rect rect;
  348. /* begin drawing */
  349. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(label));
  350. if (dc == RT_NULL) return;
  351. rtgui_widget_get_rect(RTGUI_WIDGET(label), &rect);
  352. rtgui_dc_fill_rect(dc, &rect);
  353. /* default left and center draw */
  354. rect.y1 = rect.y1 + (rtgui_rect_height(rect) - 8)/2;
  355. rtgui_dc_draw_text(dc, rtgui_label_get_text(label), &rect);
  356. /* end drawing */
  357. rtgui_dc_end_drawing(dc);
  358. }
  359. #define RTGUI_TEXTBOX_MARGIN 3
  360. void rtgui_theme_draw_textbox(rtgui_textbox_t* box)
  361. {
  362. /* draw button */
  363. struct rtgui_dc* dc;
  364. struct rtgui_rect rect;
  365. /* begin drawing */
  366. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(box));
  367. if (dc == RT_NULL) return;
  368. /* get widget rect */
  369. rtgui_widget_get_rect(RTGUI_WIDGET(box), &rect);
  370. /* fill widget rect with background color */
  371. rtgui_dc_fill_rect(dc, &rect);
  372. /* draw border */
  373. rtgui_dc_draw_border(dc, &rect, RTGUI_BORDER_STATIC);
  374. /* draw text */
  375. if (box->text != RT_NULL)
  376. {
  377. rect.x1 += RTGUI_TEXTBOX_MARGIN;
  378. rtgui_dc_draw_text(dc, box->text, &rect);
  379. }
  380. /* end drawing */
  381. rtgui_dc_end_drawing(dc);
  382. }
  383. void rtgui_theme_draw_iconbox(rtgui_iconbox_t* iconbox)
  384. {
  385. struct rtgui_dc* dc;
  386. struct rtgui_rect rect;
  387. /* begin drawing */
  388. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(iconbox));
  389. if (dc == RT_NULL) return;
  390. /* get widget rect */
  391. rtgui_widget_get_rect(RTGUI_WIDGET(iconbox), &rect);
  392. /* draw icon */
  393. rtgui_image_blit(iconbox->image, dc, &rect);
  394. /* draw text */
  395. if (iconbox->text_position == RTGUI_ICONBOX_TEXT_BELOW && iconbox->text != RT_NULL)
  396. {
  397. rect.y1 = iconbox->image->h + RTGUI_WIDGET_DEFAULT_MARGIN;
  398. rtgui_dc_draw_text(dc, iconbox->text, &rect);
  399. }
  400. else if (iconbox->text_position == RTGUI_ICONBOX_TEXT_RIGHT && iconbox->text != RT_NULL)
  401. {
  402. rect.x1 = iconbox->image->w + RTGUI_WIDGET_DEFAULT_MARGIN;
  403. rtgui_dc_draw_text(dc, iconbox->text, &rect);
  404. }
  405. /* end drawing */
  406. rtgui_dc_end_drawing(dc);
  407. }
  408. rt_uint16_t rtgui_theme_get_selected_height()
  409. {
  410. return SELECTED_HEIGHT;
  411. }
  412. void rtgui_theme_draw_selected(struct rtgui_dc* dc, rtgui_rect_t *rect)
  413. {
  414. rtgui_color_t saved;
  415. rtgui_rect_t focus_rect;
  416. focus_rect = *rect;
  417. saved = rtgui_dc_get_color(dc);
  418. rtgui_dc_set_color(dc, blue);
  419. rtgui_rect_inflate(&focus_rect, -1);
  420. rtgui_dc_draw_focus_rect(dc, &focus_rect);
  421. rtgui_rect_inflate(&focus_rect, -1);
  422. rtgui_dc_draw_focus_rect(dc, &focus_rect);
  423. rtgui_dc_set_color(dc, saved);
  424. }
  425. /* get default background color */
  426. rtgui_color_t rtgui_theme_default_bc()
  427. {
  428. return default_background;
  429. }
  430. /* get default foreground color */
  431. rtgui_color_t rtgui_theme_default_fc()
  432. {
  433. return default_foreground;
  434. }