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