rtgui_theme.c 11 KB

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