rtgui_theme.c 11 KB

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