summaryrefslogtreecommitdiff
path: root/src/fl_oxy.cxx
blob: 6332508d69708c52c4ef24e0a86479e46e346f93 (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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
//
// "Oxy" Scheme drawing routines for the Fast Light Tool Kit (FLTK).
//
// Copyright 2011 by Dmitrij K. aka "kdiman"
// Copyright 2012-2025 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 report all bugs and problems on the following page:
//
//   https://www.fltk.org/str.php
//

#include <FL/Fl.H>
#include <FL/fl_draw.H>
#include <FL/Fl_Rect.H>
#include "fl_oxy.h"

// Note:
//
// Drawing on the X11 platform w/o Cairo can be asymmetric for some "arrows"

#define GROFF 0.45f // gradients offset

#ifndef DEBUG_OXY_ARROW
#define DEBUG_OXY_ARROW 0   // 0 = off, 1 = cross at center position of arrows
#endif

// Draw a single arrow

static void single_arrow(Fl_Rect bb, Fl_Orientation o, Fl_Color col) {

  int x1 = bb.x();
  int y1 = bb.y();
  int w1 = bb.w();
  int h1 = bb.h();

  float angle = int(o) * 45.0f;

  // calculate arrow size
  int dx = (w1 - 3) / 2;
  if (h1 < w1) dx = (h1 - 3) / 2;
  if (dx > 4) dx = 4;
  else if (dx < 2) dx = 2;
  // dx = (dx + 1) & (-2);  // should be even ? not required

  int tx = x1 + w1/2;
  int ty = y1 + h1/2;

  const int lw = 2;         // arrow line width: n+1 pixels (must be even)
  const int dw = 1;         // half the line width

  fl_color(col);
  fl_line_style(FL_SOLID, 1);
  fl_push_matrix();

  fl_translate(tx, ty);     // move to center
  fl_rotate(angle);         // rotate by given angle

  // DEBUG: Draw a two-colored cross at the center of the arrow box
  // This can be used to debug or adjust array alignment (centering)

#if (DEBUG_OXY_ARROW)                   // draw a cross at the center
  int ll;                               // line length
  fl_color(FL_BLUE);                    // "horizontal" line
  ll = (o&2) ? h1/2 - 1 : w1/2 - 1;     // line length
  fl_begin_line();
  fl_vertex(-ll, 0); fl_vertex(ll, 0);
  fl_end_line();
  fl_color(0x22882200);                 // "vertical" line
  ll = (o&2) ? w1/2 - 1 : h1/2 - 1;     // line length
  fl_begin_line();
  fl_vertex(0, -ll); fl_vertex(0, ll);
  fl_end_line();
  fl_color(col);                        // back to original color
#endif

  // Draw the "arrow", similar to '>' at the center of the box

  fl_begin_complex_polygon();
  fl_vertex(-dx + dw,      -dx);
  fl_vertex(  0 + dw,        0);
  fl_vertex(-dx + dw,       dx);
  fl_vertex(-dx + dw + lw,  dx);
  fl_vertex(  0 + dw + lw,   0);
  fl_vertex(-dx + dw + lw, -dx);
  fl_end_complex_polygon();

  fl_pop_matrix();
  fl_line_style(0);
}

/**
  Draw an "arrow" GUI element for the 'oxy' scheme.

  This draws one or two "arrows" depending on the arrow type \p t.

  \param[in]  bb       bounding box
  \param[in]  t        arrow type
  \param[in]  o        orientation
  \param[in]  col      arrow color
*/
void oxy_arrow(Fl_Rect bb, Fl_Arrow_Type t, Fl_Orientation o, Fl_Color col) {

  switch(t) {

    case FL_ARROW_DOUBLE:

      switch (int(o)) {
        case FL_ORIENT_DOWN:
        case FL_ORIENT_UP:
          bb.h(bb.h() - 4);           // reduce size
          single_arrow(bb, o, col);
          bb.y(bb.y() + 4);           // shift down
          single_arrow(bb, o, col);
          break;
        default:
          bb.w(bb.w() - 4);           // reduce size
          single_arrow(bb, o, col);
          bb.x(bb.x() + 4);           // shift right
          single_arrow(bb, o, col);
          break;
      }
      break;

    case FL_ARROW_CHOICE:

      bb.y(bb.y() - 1);               // shift upwards
      bb.h(bb.h() - 4);               // reduce height
      single_arrow(bb, FL_ORIENT_UP, col);
      bb.y(bb.y() + 6);               // shift down
      single_arrow(bb, FL_ORIENT_DOWN, col);
      break;

    default:

      single_arrow(bb, o, col);
      break;
  }
}


// draw gradient from South to North
static void _oxy_up_box_(int x, int y, int w, int h, Fl_Color bg) {
  float groff = GROFF;
  if (groff < 0.0) {
    groff = 0.0f;
  }
  float gradoffset = groff;
  float stepoffset = (1.0f / (float)h);
  int xw = x + w - 1;
  // from bottom to top
  for (int _y = y; _y < y + h; _y++) {
    fl_color(fl_color_average(bg, FL_WHITE, (gradoffset < 1.0f) ? gradoffset : 1.0f));
    fl_xyline(x, _y, xw);
    gradoffset += stepoffset;
  }
}


// draw gradient from North to South
static void _oxy_down_box_(int x, int y, int w, int h, Fl_Color bg) {
  float groff = GROFF;
  if (groff < 0.0) {
    groff = 0.0f;
  }
  float gradoffset = groff;
  float stepoffset = (1.0f / (float)h);
  int xw = x + w - 1;
  // from top to bottom
  for (int _y = y + h - 1; _y >= y; _y--) {
    fl_color(fl_color_average(bg, FL_WHITE, (gradoffset < 1.0f) ? gradoffset : 1.0f));
    fl_xyline(x, _y, xw);
    gradoffset += stepoffset;
  }
}

// draw gradient for button up box
static void _oxy_button_up_box_(int x, int y, int w, int h, Fl_Color bg) {
  int half_h = h / 2;
  float gradoffset = 0.15f;
  float stepoffset = (1.0f / (float)half_h);
  Fl_Color col = fl_color_average(bg, FL_WHITE, 0.5);
  int xw = x + w - 1;
  for (int _y = y; _y <= y + half_h; _y++) {
    fl_color(fl_color_average(col, FL_WHITE, (gradoffset < 1.0f) ? gradoffset : 1.0f));
    fl_xyline(x, _y, xw);
    gradoffset += stepoffset;
  }
  gradoffset = 0.0f;
  col = bg;
  for (int _y = y + h - 1; _y >= y + half_h - 1; _y--) {
    fl_color(fl_color_average(col, FL_WHITE, (gradoffset < 1.0f) ? gradoffset : 1.0f));
    fl_xyline(x, _y, xw);
    gradoffset += stepoffset;
  }
}


// draw gradient for button down box
static void _oxy_button_down_box_(int x, int y, int w, int h, Fl_Color bg) {
  bg = fl_color_average(bg, FL_BLACK, 0.88f);
  int half_h = h / 2, xw = x + w - 1;
  float gradoffset = 0.15f, stepoffset = (1.0f / (float)half_h);
  Fl_Color col = fl_color_average(bg, FL_WHITE, 0.5);
  for (int _y = y; _y <= y + half_h; _y++) {
    fl_color(fl_color_average(col, FL_WHITE, (gradoffset < 1.0f) ? gradoffset : 1.0f));
    fl_xyline(x, _y, xw);
    gradoffset += stepoffset;
  }
  gradoffset = 0.0f;
  col = bg;
  for (int _y = y + h - 1; _y >= y + half_h - 1; _y--) {
    fl_color(fl_color_average(col, FL_WHITE, (gradoffset < 1.0f) ? gradoffset : 1.0f));
    fl_xyline(x, _y, xw);
    gradoffset += stepoffset;
  }
}


// draw rounded box
static void _oxy_rounded_box_(int x, int y, int w, int h, Fl_Color bg) {
  fl_color(bg);
  if (w > h) {
    fl_pie(x, y, h, h, 90.0, 270.0);          // right half of circle
    fl_rectf(x + h / 2, y, w - h + 1, h);     // rectangle between left and right half-circle
    fl_pie(x + w - h, y, h, h, 0.0, 90.0);    // top-left quarter of circle
    fl_pie(x + w - h, y, h, h, 270.0, 360.0); // bottom-left quarter of circle
  } else if (w == h) {
    fl_pie(x, y, w, w, 0.0, 360.0);
  } else {
    fl_pie(x, y, w, w, 0.0, 180.0);           // top half of circle
    fl_rectf(x, y + w / 2, w, h - w + 1);     // rectangle between top and bottom half-circle
    fl_pie(x, y + h - w, w, w, 180.0, 360.0); // bottom half of circle
  }
}


static Fl_Color oxy_color(Fl_Color col) {
  if (Fl::draw_box_active()) {
    return col;
  } else {
    return fl_inactive(col);
  }
}


static void oxy_draw(int x, int y, int w, int h, Fl_Color col, int typebox, bool is_shadow) {

  if (w < 1 || h < 1)
    return;
  int X, Y, W, H, X1, Y1;

  // draw bg
  if (typebox != FL_OXY_UP_FRAME && typebox != FL_OXY_DOWN_FRAME) {

    X = x + 1;
    Y = y + 1;
    W = w - 2;
    H = h - 2;

    switch (typebox) {
      case FL_OXY_UP_BOX: {
        _oxy_up_box_(X, Y, W, H, oxy_color(col));
        break;
      }
      case FL_OXY_DOWN_BOX: {
        _oxy_down_box_(X, Y, W, H, oxy_color(col));
        break;
      }
      case FL_OXY_BUTTON_UP_BOX: {
        _oxy_button_up_box_(X, Y, W, H, oxy_color(col));
        break;
      }
      case FL_OXY_BUTTON_DOWN_BOX: {
        _oxy_button_down_box_(X, Y, W, H, oxy_color(col));
        break;
      }
      case FL_OXY_ROUND_UP_BOX:
      case FL_OXY_ROUND_DOWN_BOX:
        _oxy_rounded_box_(x, y, w, h, oxy_color(fl_color_average(col, FL_WHITE, 0.82f)));
        break;
      default: { break; }
    }
  }

  Fl_Color leftline = col, topline = col, rightline = col, bottomline = col;

  if (typebox == FL_OXY_ROUND_UP_BOX || typebox == FL_OXY_ROUND_DOWN_BOX) {
    leftline = fl_color_average(col, FL_WHITE, 0.88f);
    leftline = topline = rightline = bottomline = fl_color_average(leftline, FL_BLACK, 0.97f);
  }

  else if (typebox == FL_OXY_UP_BOX || typebox == FL_OXY_UP_FRAME) {
    topline = fl_color_average(col, FL_BLACK, 0.95f);
    leftline = fl_color_average(col, FL_BLACK, 0.85f);
    rightline = leftline;
    bottomline = fl_color_average(col, FL_BLACK, 0.88f);
  }

  else if (typebox == FL_OXY_DOWN_BOX || typebox == FL_OXY_DOWN_FRAME) {
    topline = fl_color_average(col, FL_BLACK, 0.88f);
    leftline = fl_color_average(col, FL_BLACK, 0.85f);
    rightline = leftline;
    bottomline = fl_color_average(col, FL_BLACK, 0.95f);
  }

  else if (typebox == FL_OXY_BUTTON_UP_BOX || typebox == FL_OXY_BUTTON_DOWN_BOX) {
    topline = leftline = rightline = bottomline = fl_color_average(col, FL_BLACK, 0.85f);
  }

  // draw border
  if (typebox != FL_OXY_ROUND_UP_BOX && typebox != FL_OXY_ROUND_DOWN_BOX) {
    // bottom side
    fl_color(oxy_color(bottomline));
    fl_line(x + 1, y + h - 1, x + w - 2, y + h - 1);
    // right side
    fl_color(oxy_color(rightline));
    fl_line(x + w - 1, y + 1, x + w - 1, y + h - 2);
    // top side
    fl_color(oxy_color(topline));
    fl_line(x + 1, y, x + w - 2, y);
    // left side
    fl_color(oxy_color(leftline));
    fl_line(x, y + 1, x, y + h - 2);
  }

  // draw shadow
  if (is_shadow) {

    if (typebox == FL_OXY_ROUND_UP_BOX) {
      topline = fl_color_average(col, FL_WHITE, 0.35f);
      bottomline = fl_color_average(col, FL_BLACK, 0.94f);
    }

    else if (typebox == FL_OXY_ROUND_DOWN_BOX) {
      topline = fl_color_average(col, FL_BLACK, 0.94f);
      bottomline = fl_color_average(col, FL_WHITE, 0.35f);
    }

    else if (typebox == FL_OXY_UP_BOX || typebox == FL_OXY_UP_FRAME) {
      topline = fl_color_average(col, FL_WHITE, 0.35f);
      leftline = fl_color_average(col, FL_WHITE, 0.4f);
      rightline = leftline;
      bottomline = fl_color_average(col, FL_BLACK, 0.8f);
    }

    else if (typebox == FL_OXY_DOWN_BOX || typebox == FL_OXY_DOWN_FRAME) {
      topline = fl_color_average(col, FL_BLACK, 0.8f);
      leftline = fl_color_average(col, FL_BLACK, 0.94f);
      rightline = leftline;
      bottomline = fl_color_average(col, FL_WHITE, 0.35f);
    }

    int xw1 = x + w - 1;
    int xw2 = x + w - 2;
    int xw3 = x + w - 3;
    int yh2 = y + h - 2;
    int yh1 = y + h - 1;

    if (typebox == FL_OXY_UP_BOX || typebox == FL_OXY_UP_FRAME) {
      fl_color(oxy_color(topline));
      X = x + 1;
      Y = y + 1;
      X1 = xw2;
      Y1 = y + 1;
      fl_line(X, Y, X1, Y1); // top line

      fl_color(oxy_color(leftline));
      X = x + 1;
      Y = yh2;
      X1 = x + 1;
      Y1 = y + 2;
      fl_line(X, Y, X1, Y1); // left line

      fl_color(oxy_color(rightline));
      X = xw2;
      Y = y + 2;
      X1 = xw2;
      Y1 = yh2;
      fl_line(X, Y, X1, Y1); // right line

      fl_color(oxy_color(bottomline));
      X = xw2;
      Y = yh2;
      X1 = x + 1;
      Y1 = yh2;
      fl_line(X, Y, X1, Y1); // bottom line
    }

    else if (typebox == FL_OXY_DOWN_BOX || typebox == FL_OXY_DOWN_FRAME) {
      fl_color(oxy_color(topline));
      X = x + 1;
      Y = y + 1;
      X1 = xw2;
      Y1 = y + 1;
      fl_line(X, Y, X1, Y1); // top line

      fl_color(oxy_color(leftline));
      X = x + 1;
      Y = yh2;
      X1 = x + 1;
      Y1 = y + 2;
      fl_line(X, Y, X1, Y1); // left line

      fl_color(oxy_color(rightline));
      X = xw2;
      Y = y + 2;
      X1 = xw2;
      Y1 = yh2;
      fl_line(X, Y, X1, Y1); // right line

      fl_color(oxy_color(bottomline));
      X = xw3;
      Y = yh2;
      X1 = x + 2;
      Y1 = yh2;
      fl_line(X, Y, X1, Y1); // bottom line
    }

    else if (typebox == FL_OXY_ROUND_UP_BOX || typebox == FL_OXY_ROUND_DOWN_BOX) {

      int Radius, smooth;
      int r_offset2; // quarter of smooth and half of smooth

      if (w > h) {
        smooth = w;
      } else {
        smooth = h;
      }

      // correcting `smooth'
      if (smooth > 0 && (smooth * 3 > w || smooth * 3 > h)) {
        if (h < w) {
          smooth = h / 3;
        } else {
          smooth = w / 3;
        }
      }

      r_offset2 = smooth / 2;

      Radius = smooth * 3;
      if (Radius == 3) {
        Radius = 4;
      }

      fl_color(oxy_color(topline));
      fl_line(x + 1, yh1 - smooth - r_offset2, x + 1, y + r_offset2 + smooth);  // left side
      fl_arc(x + 1, y + 1, Radius, Radius, 90.0, 180.0);                        // left-top corner
      if (typebox == FL_OXY_ROUND_DOWN_BOX) {
        fl_arc(x + 1, y + 1, Radius + 1, Radius + 1, 90.0, 180.0);
      }                                                                         // left-top corner (DOWN_BOX)
      fl_line(x + smooth + r_offset2, y + 1, xw1 - smooth - r_offset2, y + 1);  // top side
      fl_arc(xw1 - Radius, y + 1, Radius, Radius, 00.0, 90.0);                  // right-top corner
      if (typebox == FL_OXY_ROUND_DOWN_BOX) {
        fl_arc(xw1 - Radius, y + 1, Radius + 1, Radius + 1, 00.0, 90.0);
      }                                                                         // right-top corner (DOWN_BOX)
      fl_line(xw2, y + smooth + r_offset2, xw2, yh1 - smooth - r_offset2);      // right side
      fl_arc(x + 1, yh1 - Radius, Radius, Radius, 180.0, 200.0);                // left-bottom corner
      fl_arc(xw1 - Radius, yh1 - Radius, Radius, Radius, 340.0, 360.0);         // right-bottom
      fl_color(oxy_color(bottomline));
      fl_arc(x + 1, yh1 - Radius, Radius, Radius, 200.0, 270.0);                // left-bottom corner
      if (typebox == FL_OXY_ROUND_UP_BOX) {
        fl_arc(x + 1, yh1 - Radius, Radius + 1, Radius + 1, 200.0, 270.0);
      }                                                                         // left-bottom corner (UP_BOX)
      fl_line(xw1 - smooth - r_offset2, yh2, x + smooth + r_offset2, yh2);      // bottom side
      fl_arc(xw1 - Radius, yh1 - Radius, Radius, Radius, 270.0, 340.0);         // right-bottom corner
      if (typebox == FL_OXY_ROUND_UP_BOX) {
        fl_arc(xw1 - Radius, yh1 - Radius, Radius + 1, Radius + 1, 270.0, 340.0);
      } // right-bottom corner
    }

  } // end `if (is_shadow)'

} // end `static void oxy_draw(...)'


void fl_oxy_button_up_box(int x, int y, int w, int h, Fl_Color col) {
  oxy_draw(x, y, w, h, col, FL_OXY_BUTTON_UP_BOX, true);
}
void fl_oxy_button_down_box(int x, int y, int w, int h, Fl_Color col) {
  oxy_draw(x, y, w, h, col, FL_OXY_BUTTON_DOWN_BOX, true);
}
void fl_oxy_up_box(int x, int y, int w, int h, Fl_Color col) {
  oxy_draw(x, y, w, h, col, FL_OXY_UP_BOX, true);
}
void fl_oxy_down_box(int x, int y, int w, int h, Fl_Color col) {
  oxy_draw(x, y, w, h, col, FL_OXY_DOWN_BOX, true);
}
void fl_oxy_thin_up_box(int x, int y, int w, int h, Fl_Color col) {
  oxy_draw(x, y, w, h, col, FL_OXY_UP_BOX, false);
}
void fl_oxy_thin_down_box(int x, int y, int w, int h, Fl_Color col) {
  oxy_draw(x, y, w, h, col, FL_OXY_DOWN_BOX, false);
}
void fl_oxy_up_frame(int x, int y, int w, int h, Fl_Color col) {
  oxy_draw(x, y, w, h, col, FL_OXY_UP_FRAME, true);
}
void fl_oxy_down_frame(int x, int y, int w, int h, Fl_Color col) {
  oxy_draw(x, y, w, h, col, FL_OXY_DOWN_FRAME, true);
}
void fl_oxy_thin_up_frame(int x, int y, int w, int h, Fl_Color col) {
  oxy_draw(x, y, w, h, col, FL_OXY_UP_FRAME, false);
}
void fl_oxy_thin_down_frame(int x, int y, int w, int h, Fl_Color col) {
  oxy_draw(x, y, w, h, col, FL_OXY_DOWN_FRAME, false);
}
void fl_oxy_round_up_box(int x, int y, int w, int h, Fl_Color col) {
  oxy_draw(x, y, w, h, col, FL_OXY_ROUND_UP_BOX, true);
}
void fl_oxy_round_down_box(int x, int y, int w, int h, Fl_Color col) {
  oxy_draw(x, y, w, h, col, FL_OXY_ROUND_DOWN_BOX, true);
}