You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /* Copyright 2011-2022 Pierre Ossman for Cendio AB
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining
  4. * a copy of this software and associated documentation files (the
  5. * "Software"), to deal in the Software without restriction, including
  6. * without limitation the rights to use, copy, modify, merge, publish,
  7. * distribute, sublicense, and/or sell copies of the Software, and to
  8. * permit persons to whom the Software is furnished to do so, subject to
  9. * the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be
  12. * included in all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  18. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  19. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. */
  23. #ifdef HAVE_CONFIG_H
  24. #include <config.h>
  25. #endif
  26. #include <assert.h>
  27. #ifdef WIN32
  28. #include <windows.h>
  29. #endif
  30. #ifdef __APPLE__
  31. #include <ApplicationServices/ApplicationServices.h>
  32. #endif
  33. #include <FL/Fl.H>
  34. #include <FL/Fl_Widget.H>
  35. #include <FL/fl_ask.H>
  36. #include <FL/fl_draw.H>
  37. #include "theme.h"
  38. const int RADIUS = 4;
  39. /*
  40. * fl_arc() and fl_pie() are broken on Windows, so we need to fudge the
  41. * numbers a bit
  42. */
  43. #ifdef WIN32
  44. static void fixed_arc(int x,int y,int w,int h,double a1,double a2) {
  45. if ((a1 != 0.0) || (a2 != 360.0)) {
  46. a1 -= 10.0;
  47. a2 += 10.0;
  48. }
  49. fl_arc(x, y, w, h, a1, a2);
  50. }
  51. static void fixed_pie(int x,int y,int w,int h,double a1,double a2) {
  52. if ((a1 != 0.0) || (a2 != 360.0)) {
  53. a1 -= 10.0;
  54. a2 += 10.0;
  55. }
  56. fl_pie(x, y, w, h, a1, a2);
  57. }
  58. #define fl_arc fixed_arc
  59. #define fl_pie fixed_pie
  60. #endif
  61. static Fl_Color light_border(Fl_Color c)
  62. {
  63. return fl_color_average(FL_BLACK, c, 0.12);
  64. }
  65. static Fl_Color dark_border(Fl_Color c)
  66. {
  67. return fl_color_average(FL_BLACK, c, 0.33);
  68. }
  69. static void theme_round_frame(bool up, int x, int y, int w, int h,
  70. int r, Fl_Color c)
  71. {
  72. if (r > h/2)
  73. r = h/2;
  74. // Top
  75. if (up)
  76. Fl::set_box_color(light_border(c));
  77. else
  78. Fl::set_box_color(dark_border(c));
  79. fl_xyline(x+r, y, x+w-r-1);
  80. // Top corners
  81. if (!up)
  82. Fl::set_box_color(fl_color_average(light_border(c), dark_border(c), 0.5));
  83. fl_arc(x, y, r*2, r*2, 90.0, 180.0);
  84. fl_arc(x+w-(r*2), y, r*2, r*2, 0, 90.0);
  85. // Left and right
  86. Fl::set_box_color(light_border(c));
  87. fl_yxline(x, y+r, y+h-r-1);
  88. fl_yxline(x+w-1, y+r, y+h-r-1);
  89. // Bottom corners
  90. if (up)
  91. Fl::set_box_color(fl_color_average(light_border(c), dark_border(c), 0.5));
  92. fl_arc(x, y+h-(r*2), r*2, r*2, 180, 270.0);
  93. fl_arc(x+w-(r*2), y+h-(r*2), r*2, r*2, 270, 360.0);
  94. // Bottom
  95. if (up)
  96. Fl::set_box_color(dark_border(c));
  97. else
  98. Fl::set_box_color(light_border(c));
  99. fl_xyline(x+r, y+h-1, x+w-r-1);
  100. }
  101. static void theme_up_frame(int x, int y, int w, int h, Fl_Color c)
  102. {
  103. theme_round_frame(true, x, y, w, h, RADIUS, c);
  104. }
  105. static void theme_down_frame(int x, int y, int w, int h, Fl_Color c)
  106. {
  107. theme_round_frame(false, x, y, w, h, RADIUS, c);
  108. }
  109. static void theme_round_rect(int x, int y, int w, int h, int r,
  110. Fl_Color c, Fl_Color c2=-1)
  111. {
  112. if (r > h/2)
  113. r = h/2;
  114. if (c2 == (Fl_Color)-1)
  115. c2 = c;
  116. // Top
  117. Fl::set_box_color(c);
  118. fl_pie(x, y, r*2, r*2, 90.0, 180.0);
  119. fl_rectf(x+r, y, w-(r*2), r);
  120. fl_pie(x+w-(r*2), y, r*2, r*2, 0, 90.0);
  121. // Middle
  122. const int steps = h - r*2;
  123. for (int i = 0; i < steps; i++) {
  124. Fl::set_box_color(fl_color_average(c2, c, (double)i/steps));
  125. fl_xyline(x, y+r+i, x+w-1);
  126. }
  127. // Bottom
  128. Fl::set_box_color(c2);
  129. fl_pie(x, y+h-(r*2), r*2, r*2, 180, 270.0);
  130. fl_rectf(x+r, y+h-r, w-(r*2), r);
  131. fl_pie(x+w-(r*2), y+h-(r*2), r*2, r*2, 270, 360.0);
  132. }
  133. static void theme_up_box(int x, int y, int w, int h, Fl_Color c)
  134. {
  135. theme_round_rect(x, y, w, h, RADIUS,
  136. fl_color_average(FL_WHITE, c, 0.75));
  137. theme_up_frame(x, y, w, h, c);
  138. }
  139. static void theme_down_box(int x, int y, int w, int h, Fl_Color c)
  140. {
  141. theme_round_rect(x, y, w, h, RADIUS, c);
  142. theme_down_frame(x, y, w, h, c);
  143. }
  144. static void theme_thin_up_box(int x, int y, int w, int h, Fl_Color c)
  145. {
  146. theme_round_rect(x, y, w, h, RADIUS, c);
  147. theme_up_frame(x, y, w, h, c);
  148. }
  149. static void theme_thin_down_box(int x, int y, int w, int h, Fl_Color c)
  150. {
  151. theme_round_rect(x, y, w, h, RADIUS, c);
  152. theme_down_frame(x, y, w, h, c);
  153. }
  154. static void theme_round_up_box(int x, int y, int w, int h, Fl_Color c)
  155. {
  156. // Background
  157. Fl::set_box_color(c);
  158. fl_pie(x, y, w, h, 0.0, 360.0);
  159. // Outline
  160. Fl::set_box_color(light_border(c));
  161. fl_arc(x, y, w, h, 0.0, 180.0);
  162. Fl::set_box_color(dark_border(c));
  163. fl_arc(x, y, w, h, 180.0, 360.0);
  164. Fl::set_box_color(fl_color_average(light_border(c), dark_border(c), 0.5));
  165. fl_arc(x, y, w, h, 225.0, 315.0);
  166. }
  167. static void theme_round_down_box(int x, int y, int w, int h, Fl_Color c)
  168. {
  169. // Background
  170. Fl::set_box_color(c);
  171. fl_pie(x, y, w, h, 0.0, 360.0);
  172. // Outline
  173. Fl::set_box_color(fl_color_average(light_border(c), dark_border(c), 0.5));
  174. fl_arc(x, y, w, h, 0.0, 180.0);
  175. Fl::set_box_color(dark_border(c));
  176. fl_arc(x, y, w, h, 45.0, 135.0);
  177. Fl::set_box_color(light_border(c));
  178. fl_arc(x, y, w, h, 180.0, 360.0);
  179. }
  180. void init_theme()
  181. {
  182. #if defined(WIN32) || defined(__APPLE__)
  183. static char font_name[256];
  184. #endif
  185. // Basic text size (10pt @ 96 dpi => 13px)
  186. FL_NORMAL_SIZE = 13;
  187. // Modern colors based on the default appearance of Windows 11,
  188. // macOS 12 and GNOME 41
  189. // FIXME: Should get these from the system,
  190. // Fl::get_system_colors() is unfortunately not very capable
  191. // FIXME: Should also handle dark mode
  192. #if defined(WIN32)
  193. // Windows 11
  194. Fl::foreground(26, 26, 26);
  195. Fl::background(243, 243, 243);
  196. #elif defined(__APPLE__)
  197. // FIXME: Text is rendered slightly lighter than what we specify here
  198. // for some odd reason. The target is (38, 38, 38).
  199. Fl::foreground(28, 28, 28);
  200. Fl::background(246, 246, 246);
  201. #else
  202. // GNOME
  203. Fl::foreground(46, 52, 54);
  204. Fl::background(246, 245, 244);
  205. #endif
  206. #if defined(WIN32)
  207. // Windows 11 default accent color
  208. Fl::set_color(FL_SELECTION_COLOR, 0, 103, 192);
  209. #elif defined(__APPLE__)
  210. Fl::set_color(FL_SELECTION_COLOR, 0, 122, 255);
  211. #else
  212. // GNOME
  213. Fl::set_color(FL_SELECTION_COLOR, 53, 132, 228);
  214. #endif
  215. // The arrow on Fl_Return_Button gets a invisible, so let's adjust it
  216. // to compensate for our lighter buttons
  217. Fl::set_color(FL_LIGHT3, light_border(fl_color_average(FL_WHITE, FL_BACKGROUND_COLOR, 0.5)));
  218. Fl::set_color(FL_DARK3, dark_border(fl_color_average(FL_WHITE, FL_BACKGROUND_COLOR, 0.5)));
  219. // We will override the box types later, but changing scheme affects
  220. // more things than just those, so we still want to switch from the
  221. // default
  222. Fl::scheme("gtk+");
  223. // Define our box types
  224. const int PX = 2;
  225. const int PY = 2;
  226. // FLTK lacks a bounds check
  227. assert(THEME_ROUND_DOWN_BOX < 256);
  228. Fl::set_boxtype(THEME_UP_FRAME, theme_up_frame, PX, PY, PX*2, PY*2);
  229. Fl::set_boxtype(THEME_DOWN_FRAME, theme_down_frame, PX, PY, PX*2, PY*2);
  230. Fl::set_boxtype(THEME_THIN_UP_FRAME, theme_up_frame, PX, PY, PX*2, PY*2);
  231. Fl::set_boxtype(THEME_THIN_DOWN_FRAME, theme_down_frame, PX, PY, PX*2, PY*2);
  232. Fl::set_boxtype(THEME_UP_BOX, theme_up_box, PX, PY, PX*2, PY*2);
  233. Fl::set_boxtype(THEME_DOWN_BOX, theme_down_box, PX, PY, PX*2, PY*2);
  234. Fl::set_boxtype(THEME_THIN_UP_BOX, theme_thin_up_box, PX, PY, PX*2, PY*2);
  235. Fl::set_boxtype(THEME_THIN_DOWN_BOX, theme_thin_down_box, PX, PY, PX*2, PY*2);
  236. Fl::set_boxtype(THEME_ROUND_UP_BOX, theme_round_up_box, PX, PY, PX*2, PY*2);
  237. Fl::set_boxtype(THEME_ROUND_DOWN_BOX, theme_round_down_box, PX, PY, PX*2, PY*2);
  238. // Make them the default
  239. Fl::set_boxtype(FL_UP_FRAME, THEME_UP_FRAME);
  240. Fl::set_boxtype(FL_DOWN_FRAME, THEME_UP_FRAME);
  241. Fl::set_boxtype(FL_THIN_UP_FRAME, THEME_THIN_UP_FRAME);
  242. Fl::set_boxtype(FL_THIN_DOWN_FRAME, THEME_THIN_DOWN_FRAME);
  243. Fl::set_boxtype(FL_UP_BOX, THEME_UP_BOX);
  244. Fl::set_boxtype(FL_DOWN_BOX, THEME_DOWN_BOX);
  245. Fl::set_boxtype(FL_THIN_UP_BOX, THEME_THIN_UP_BOX);
  246. Fl::set_boxtype(FL_THIN_DOWN_BOX, THEME_THIN_DOWN_BOX);
  247. Fl::set_boxtype(_FL_ROUND_UP_BOX, THEME_ROUND_UP_BOX);
  248. Fl::set_boxtype(_FL_ROUND_DOWN_BOX, THEME_ROUND_DOWN_BOX);
  249. #if defined(WIN32)
  250. NONCLIENTMETRICS metrics;
  251. metrics.cbSize = sizeof(metrics);
  252. if (SystemParametersInfo(SPI_GETNONCLIENTMETRICS,
  253. sizeof(metrics), &metrics, 0)) {
  254. strcpy(font_name, metrics.lfMessageFont.lfFaceName);
  255. Fl::set_font(FL_HELVETICA, font_name);
  256. }
  257. #elif defined(__APPLE__)
  258. CTFontRef font;
  259. CFStringRef name;
  260. font = CTFontCreateUIFontForLanguage(kCTFontSystemFontType, 0.0, NULL);
  261. if (font != NULL) {
  262. name = CTFontCopyFullName(font);
  263. if (name != NULL) {
  264. CFStringGetCString(name, font_name, sizeof(font_name),
  265. kCFStringEncodingUTF8);
  266. Fl::set_font(FL_HELVETICA, font_name);
  267. CFRelease(name);
  268. }
  269. CFRelease(font);
  270. }
  271. #else
  272. // FIXME: Get font from GTK or QT
  273. #endif
  274. }