summaryrefslogtreecommitdiff
path: root/src/Fl_get_system_colors.cxx
blob: 51eb06ac117fca55cb46cfd135e6e25d7911cbd4 (plain)
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
//
// System color support for the Fast Light Tool Kit (FLTK).
//
// Copyright 1998-2024 by Bill Spitzak and others.
//
// This library is free software. Distribution and use rights are outlined in
// the file "COPYING" which should have been included with this file.  If this
// file is missing or damaged, see the license at:
//
//     https://www.fltk.org/COPYING.php
//
// Please see the following page on how to report bugs and issues:
//
//     https://www.fltk.org/bugs.php
//

/** \file Fl_get_system_colors.cxx
 \brief System color support
*/

#include <FL/Fl.H>
#include "Fl_Screen_Driver.H"
#include "Fl_System_Driver.H"
#include <FL/fl_draw.H>
#include <FL/platform.H>
#include <FL/math.h>
#include <FL/fl_utf8.h>
#include <FL/fl_string_functions.h>
#include "flstring.h"
#include <stdio.h>
#include <stdlib.h>
#include <FL/Fl_Pixmap.H>
#include <FL/Fl_Tiled_Image.H>
#include "tile.xpm"

/**
    Changes fl_color(FL_BACKGROUND_COLOR) to the given color,
    and changes the gray ramp from 32 to 56 to black to white.  These are
    the colors used as backgrounds by almost all widgets and used to draw
    the edges of all the boxtypes.
*/
void Fl::background(uchar r, uchar g, uchar b) {
  Fl_Screen_Driver::bg_set = 1;

  // replace the gray ramp so that FL_GRAY is this color
  if (!r) r = 1; else if (r==255) r = 254;
  double powr = log(r/255.0)/log((FL_GRAY-FL_GRAY_RAMP)/(FL_NUM_GRAY-1.0));
  if (!g) g = 1; else if (g==255) g = 254;
  double powg = log(g/255.0)/log((FL_GRAY-FL_GRAY_RAMP)/(FL_NUM_GRAY-1.0));
  if (!b) b = 1; else if (b==255) b = 254;
  double powb = log(b/255.0)/log((FL_GRAY-FL_GRAY_RAMP)/(FL_NUM_GRAY-1.0));
  for (int i = 0; i < FL_NUM_GRAY; i++) {
    double gray = i/(FL_NUM_GRAY-1.0);
    Fl::set_color(fl_gray_ramp(i),
                  uchar(pow(gray,powr)*255+.5),
                  uchar(pow(gray,powg)*255+.5),
                  uchar(pow(gray,powb)*255+.5));
  }
}


/** Changes fl_color(FL_FOREGROUND_COLOR). */
void Fl::foreground(uchar r, uchar g, uchar b) {
  Fl_Screen_Driver::fg_set = 1;

  Fl::set_color(FL_FOREGROUND_COLOR,r,g,b);
}


/**
    Changes the alternative background color. This color is used as a
    background by Fl_Input and other text widgets.
    <P>This call may change fl_color(FL_FOREGROUND_COLOR) if it
    does not provide sufficient contrast to FL_BACKGROUND2_COLOR.
*/
void Fl::background2(uchar r, uchar g, uchar b) {
  Fl_Screen_Driver::bg2_set = 1;

  Fl::set_color(FL_BACKGROUND2_COLOR,r,g,b);
  Fl::set_color(FL_FOREGROUND_COLOR,
                get_color(fl_contrast(FL_FOREGROUND_COLOR,FL_BACKGROUND2_COLOR)));
}


// these are set by Fl::args() and override any system colors:
const char *fl_fg = NULL;
const char *fl_bg = NULL;
const char *fl_bg2 = NULL;

/**
 Parse a string containing a description of a color and write r, g, and b.

 This call is used by the Pixmap file format interpreter and by the command
 line arguments parser to set UI colors.

 RGB color triplets usually start with a '#' character, but it can be omitted
 if it does not conflict with the later rules. Color components are defined
 in hexadecimal notation with 1, 2, 3, or four hex digits per component, making
 color triplets 3, 6, 9, or 12 characters long. The interpreter is case
 insensitive. Valid code examples include "FF0000" for red, "#0F0" for green,
 and "000000004444" for a dark blue.

 On the X11 platform, color values can also be given a color name like "red".
 The list of accepted color names is provided by the X11 server.

 If none of the color interpretations work, \ref fl_parse_color returns 0.
 The Pixmap reader interprets those as transparent, and are usually written as
 "None", "#transparent", or "bg".

 \param[in] p a C-string describing the color
 \param[out] r, g, b the color components in the 0...255 range
 \return 0 if the color cannot be interpreted, 1 otherwise
 */
int fl_parse_color(const char* p, uchar& r, uchar& g, uchar& b) {
  return Fl::screen_driver()->parse_color(p, r, g, b);
}


/** \fn Fl::get_system_colors()
    Read the user preference colors from the system and use them to call
    Fl::foreground(), Fl::background(), and Fl::background2().

    This is done by Fl_Window::show(argc,argv) before applying
    the -fg and -bg switches.

    On X this reads some common values from the Xdefaults database.
    KDE users can set these values by running the "krdb" program, and
    newer versions of KDE set this automatically if you check the "apply
    style to other X programs" switch in their control panel.
*/
void Fl::get_system_colors()
{
  Fl::screen_driver()->get_system_colors();
}


//// Simple implementation of 2.0 Fl::scheme() interface...
#define D1 BORDER_WIDTH
#define D2 (BORDER_WIDTH+BORDER_WIDTH)

extern void     fl_up_box(int, int, int, int, Fl_Color);
extern void     fl_down_box(int, int, int, int, Fl_Color);
extern void     fl_thin_up_box(int, int, int, int, Fl_Color);
extern void     fl_thin_down_box(int, int, int, int, Fl_Color);
extern void     fl_round_up_box(int, int, int, int, Fl_Color);
extern void     fl_round_down_box(int, int, int, int, Fl_Color);
extern void     fl_round_focus(Fl_Boxtype, int, int, int, int, Fl_Color, Fl_Color);

extern void     fl_up_frame(int, int, int, int, Fl_Color);
extern void     fl_down_frame(int, int, int, int, Fl_Color);
extern void     fl_thin_up_frame(int, int, int, int, Fl_Color);
extern void     fl_thin_down_frame(int, int, int, int, Fl_Color);

#ifndef FL_DOXYGEN
const char      *Fl::scheme_ = (const char *)0;     // current scheme
Fl_Image        *Fl::scheme_bg_ = (Fl_Image *)0;    // current background image for the scheme
#endif

static Fl_Pixmap        tile(tile_xpm);


/**
  Sets the current widget scheme. NULL will use the scheme defined
  in the FLTK_SCHEME environment variable or the scheme resource
  under X11. Otherwise, any of the following schemes can be used:

    - "none" - This is the default look-n-feel which resembles old
               Windows (95/98/Me/NT/2000) and old GTK/KDE

    - "base" - This is an alias for "none"

    - "plastic" - This scheme is inspired by the Aqua user interface
                  on macOS

    - "gtk+" - This scheme is inspired by the Red Hat Bluecurve theme

    - "gleam" - This scheme is inspired by the Clearlooks Glossy scheme.
                (Colin Jones and Edmanuel Torres).

    - "oxy" - This is a subset of Dmitrij K's oxy scheme (STR 2675, 3477)

  If the given scheme name is unknown, the default scheme will be used.

  Setting the scheme (name) is case insensitive, but the stored scheme name will
  always be lowercase and Fl::scheme() will return this lowercase name or
  \p NULL if no scheme or the default scheme ("none" or "base") was set.

  \param[in]  s   Scheme name of NULL

  \retval     0 if the scheme has not been set or is the default scheme
  \retval     1 if a scheme other than "none"/"base" was set

  \see Fl::scheme() to get the name of the current scheme
  \see Fl::is_scheme(const char*) to test if the specified scheme is set
*/
int Fl::scheme(const char *s) {
  if (!s) {
    s = screen_driver()->get_system_scheme();
  }

  if (s) {
    if (!fl_ascii_strcasecmp(s, "none") || !fl_ascii_strcasecmp(s, "base") || !*s) s = NULL;
    else if (!fl_ascii_strcasecmp(s, "gtk+")) s = fl_strdup("gtk+");
    else if (!fl_ascii_strcasecmp(s, "plastic")) s = fl_strdup("plastic");
    else if (!fl_ascii_strcasecmp(s, "gleam")) s = fl_strdup("gleam");
    else if (!fl_ascii_strcasecmp(s, "oxy")) s = fl_strdup("oxy");
    else s = NULL;
  }
  if (scheme_) free((void*)scheme_);
  scheme_ = s;

  // Save the new scheme in the FLTK_SCHEME env var so that child processes
  // inherit it...
  static char e[1024];
  strcpy(e, "FLTK_SCHEME=");
  if (s) strlcat(e, s, sizeof(e));
  Fl::system_driver()->putenv(e);

  // Load the scheme...
  reload_scheme();
  return (s != NULL);
}

/**
  Called internally when setting a new scheme according to scheme name.
  Loads or reloads the current scheme selection.

  \return   Always 1 (this may change in the future)

  See void Fl::scheme(const char *name)

  \internal
  \note Internal: Should this method be private?
*/
int Fl::reload_scheme() {
  Fl_Window *win;

  if (scheme_ && !fl_ascii_strcasecmp(scheme_, "plastic")) {
    // Update the tile image to match the background color...
    uchar r, g, b;
    int nr, ng, nb;
    int i;
    // static uchar levels[3] = { 0xff, 0xef, 0xe8 };
    // OSX 10.3 and higher use a background with less contrast...
    static uchar levels[3] = { 0xff, 0xf8, 0xf4 };

    get_color(FL_GRAY, r, g, b);

    // printf("FL_GRAY = 0x%02x 0x%02x 0x%02x\n", r, g, b);

    for (i = 0; i < 3; i ++) {
      nr = levels[i] * r / 0xe8;
      if (nr > 255) nr = 255;

      ng = levels[i] * g / 0xe8;
      if (ng > 255) ng = 255;

      nb = levels[i] * b / 0xe8;
      if (nb > 255) nb = 255;

      snprintf(tile_cmap[i], sizeof(tile_cmap[0]), "%c c #%02x%02x%02x", "Oo."[i], nr, ng, nb);
      // puts(tile_cmap[i]);
    }

    tile.uncache();

    if (!scheme_bg_) scheme_bg_ = new Fl_Tiled_Image(&tile, 0, 0);

    // Load plastic buttons, etc...
    set_boxtype(FL_UP_FRAME,        FL_PLASTIC_UP_FRAME);
    set_boxtype(FL_DOWN_FRAME,      FL_PLASTIC_DOWN_FRAME);
    set_boxtype(FL_THIN_UP_FRAME,   FL_PLASTIC_UP_FRAME);
    set_boxtype(FL_THIN_DOWN_FRAME, FL_PLASTIC_DOWN_FRAME);

    set_boxtype(FL_UP_BOX,          FL_PLASTIC_UP_BOX);
    set_boxtype(FL_DOWN_BOX,        FL_PLASTIC_DOWN_BOX);
    set_boxtype(FL_THIN_UP_BOX,     FL_PLASTIC_THIN_UP_BOX);
    set_boxtype(FL_THIN_DOWN_BOX,   FL_PLASTIC_THIN_DOWN_BOX);
    set_boxtype(FL_ROUND_UP_BOX,    FL_PLASTIC_ROUND_UP_BOX);
    set_boxtype(FL_ROUND_DOWN_BOX,  FL_PLASTIC_ROUND_DOWN_BOX);

    // Use standard size scrollbars...
    Fl::scrollbar_size(16);
  } else if (scheme_ && !fl_ascii_strcasecmp(scheme_, "gtk+")) {
    // Use a GTK+ inspired look-n-feel...
    if (scheme_bg_) {
      delete scheme_bg_;
      scheme_bg_ = (Fl_Image *)0;
    }

    set_boxtype(FL_UP_FRAME,        FL_GTK_UP_FRAME);
    set_boxtype(FL_DOWN_FRAME,      FL_GTK_DOWN_FRAME);
    set_boxtype(FL_THIN_UP_FRAME,   FL_GTK_THIN_UP_FRAME);
    set_boxtype(FL_THIN_DOWN_FRAME, FL_GTK_THIN_DOWN_FRAME);

    set_boxtype(FL_UP_BOX,          FL_GTK_UP_BOX);
    set_boxtype(FL_DOWN_BOX,        FL_GTK_DOWN_BOX);
    set_boxtype(FL_THIN_UP_BOX,     FL_GTK_THIN_UP_BOX);
    set_boxtype(FL_THIN_DOWN_BOX,   FL_GTK_THIN_DOWN_BOX);
    set_boxtype(FL_ROUND_UP_BOX,    FL_GTK_ROUND_UP_BOX);
    set_boxtype(FL_ROUND_DOWN_BOX,  FL_GTK_ROUND_DOWN_BOX);

    // Use slightly thinner scrollbars...
    Fl::scrollbar_size(15);
  } else if (scheme_ && !fl_ascii_strcasecmp(scheme_, "gleam")) {
    // Use a GTK+ inspired look-n-feel...
    if (scheme_bg_) {
      delete scheme_bg_;
      scheme_bg_ = (Fl_Image *)0;
    }

    set_boxtype(FL_UP_FRAME,        FL_GLEAM_UP_FRAME);
    set_boxtype(FL_DOWN_FRAME,      FL_GLEAM_DOWN_FRAME);
    set_boxtype(FL_THIN_UP_FRAME,   FL_GLEAM_UP_FRAME);
    set_boxtype(FL_THIN_DOWN_FRAME, FL_GLEAM_DOWN_FRAME);

    set_boxtype(FL_UP_BOX,          FL_GLEAM_UP_BOX);
    set_boxtype(FL_DOWN_BOX,        FL_GLEAM_DOWN_BOX);
    set_boxtype(FL_THIN_UP_BOX,     FL_GLEAM_THIN_UP_BOX);
    set_boxtype(FL_THIN_DOWN_BOX,   FL_GLEAM_THIN_DOWN_BOX);
    set_boxtype(FL_ROUND_UP_BOX,    FL_GLEAM_ROUND_UP_BOX);
    set_boxtype(FL_ROUND_DOWN_BOX,  FL_GLEAM_ROUND_DOWN_BOX);

    // Use slightly thinner scrollbars...
    Fl::scrollbar_size(15);
  } else if (scheme_ && !fl_ascii_strcasecmp(scheme_, "oxy")) {
    // Oxy scheme
    if (scheme_bg_) {
      delete scheme_bg_;
      scheme_bg_ = (Fl_Image *)0;
    }

    set_boxtype(FL_UP_FRAME,        FL_OXY_UP_FRAME);
    set_boxtype(FL_DOWN_FRAME,      FL_OXY_DOWN_FRAME);
    set_boxtype(FL_THIN_UP_FRAME,   FL_OXY_THIN_UP_FRAME);
    set_boxtype(FL_THIN_DOWN_FRAME, FL_OXY_THIN_DOWN_FRAME);

    set_boxtype(FL_UP_BOX,          FL_OXY_UP_BOX);
    set_boxtype(FL_DOWN_BOX,        FL_OXY_DOWN_BOX);
    set_boxtype(FL_THIN_UP_BOX,     FL_OXY_THIN_UP_BOX);
    set_boxtype(FL_THIN_DOWN_BOX,   FL_OXY_THIN_DOWN_BOX);
    set_boxtype(FL_ROUND_UP_BOX,    FL_OXY_ROUND_UP_BOX);
    set_boxtype(FL_ROUND_DOWN_BOX,  FL_OXY_ROUND_DOWN_BOX);

    // Use slightly thinner scrollbars...
    Fl::scrollbar_size(15);
  } else {
    // Use the standard FLTK look-n-feel...
    if (scheme_bg_) {
      delete scheme_bg_;
      scheme_bg_ = (Fl_Image *)0;
    }

    set_boxtype(FL_UP_FRAME,        fl_up_frame, D1, D1, D2, D2);
    set_boxtype(FL_DOWN_FRAME,      fl_down_frame, D1, D1, D2, D2);
    set_boxtype(FL_THIN_UP_FRAME,   fl_thin_up_frame, 1, 1, 2, 2);
    set_boxtype(FL_THIN_DOWN_FRAME, fl_thin_down_frame, 1, 1, 2, 2);

    set_boxtype(FL_UP_BOX,          fl_up_box, D1, D1, D2, D2);
    set_boxtype(FL_DOWN_BOX,        fl_down_box, D1, D1, D2, D2);
    set_boxtype(FL_THIN_UP_BOX,     fl_thin_up_box, 1, 1, 2, 2);
    set_boxtype(FL_THIN_DOWN_BOX,   fl_thin_down_box, 1, 1, 2, 2);
    set_boxtype(FL_ROUND_UP_BOX,    fl_round_up_box, 3, 3, 6, 6, fl_round_focus);
    set_boxtype(FL_ROUND_DOWN_BOX,  fl_round_down_box, 3, 3, 6, 6, fl_round_focus);

    // Use standard size scrollbars...
    Fl::scrollbar_size(16);
  }

  // Set (or clear) the background tile for all windows...

  // FIXME: This makes it impossible to assign a background image
  // and/or a label to a window. IMHO we should be able to assign a
  // background image to a window. Currently (as of FLTK 1.3.3) there
  // is the workaround to use a group inside the window to achieve this.
  // See also STR #3075.
  // AlbrechtS, 01 Mar 2015
  //
  // If there is already an image assigned that is not the scheme_bg_,
  // then don't change the labeltype or assign another image. Will that
  // fix it?

  for (win = first_window(); win; win = next_window(win)) {
    win->labeltype(scheme_bg_ ? FL_NORMAL_LABEL : FL_NO_LABEL);
    win->align(FL_ALIGN_CENTER | FL_ALIGN_INSIDE | FL_ALIGN_CLIP);
    win->image(scheme_bg_);
    win->redraw();
  }

  return 1;
}