summaryrefslogtreecommitdiff
path: root/test/line_style_docs.cxx
blob: 5bf45fd42c0aea797f938d14acfab5f6ad94d96d (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
//
// Line style image (docs) for the Fast Light Tool Kit (FLTK).
//
// Copyright 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
//

// Notes to devs (and users):
//
// 1. Run this program to create the screenshot for the fl_line_style() docs.
//    Save a screenshot of its original size to documentation/src/fl_line_style.png
// 2. For further tests it's possible to resize the window. Line sizes and widths
//    are adjusted (resized) as well, depending on the window size.
// 3. Some lines may draw outside their boxes in unusual window width/height ratios.
//    These effects are intentionally ignored.

#include <FL/Fl.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Grid.H>
#include <FL/Fl_Box.H>
#include <FL/fl_draw.H>

// constants

static const int sep = 22;    // separation between items
static int width[] = {1, 4};  // line widths (thin + thick/dyn.)

// This class draws a box with one line style inside an Fl_Grid widget.
// Row and column parameters are used to position the box inside the grid.

class StyleBox : public Fl_Box {
  int style;              // line style
public:
  StyleBox(int S, int row, int col) // style, row, column
    : Fl_Box(0, 0, 0, 0) {
    box(FL_FLAT_BOX);
    color(FL_WHITE);
    style = S;
    Fl_Grid *grid = (Fl_Grid *)parent();
    grid->widget(this, row, col, FL_GRID_FILL);
  }
  const char *style_str(int style) {
    switch(style) {
      case FL_SOLID       : return "FL_SOLID"      ;
      case FL_DASH        : return "FL_DASH"       ;
      case FL_DOT         : return "FL_DOT"        ;
      case FL_DASHDOT     : return "FL_DASHDOT"    ;
      case FL_DASHDOTDOT  : return "FL_DASHDOTDOT" ;
      case FL_CAP_FLAT    : return "FL_CAP_FLAT"   ;
      case FL_CAP_ROUND   : return "FL_CAP_ROUND"  ;
      case FL_CAP_SQUARE  : return "FL_CAP_SQUARE" ;
      case FL_JOIN_MITER  : return "FL_JOIN_MITER" ;
      case FL_JOIN_ROUND  : return "FL_JOIN_ROUND" ;
      case FL_JOIN_BEVEL  : return "FL_JOIN_BEVEL" ;
      default             : return "(?)";
    }
  }
  void draw() FL_OVERRIDE {
    draw_box();
    if (style < 0) // draw an empty box
      return;

    // set font and measure widest text
    fl_font(FL_HELVETICA, 12);
    fl_color(FL_BLACK);
    static int text_width = 0;
    if (!text_width) {
      int h = 0; // dummy
      fl_measure("FL_DASHDOTDOT", text_width, h);
    }

    // draw the text
    int X = x() + sep / 2;
    fl_draw(style_str(style), X, y() + h()/2 + fl_height()/2 - 2);

    // calculate dynamic line sizes and widths
    X += text_width + sep / 2;
    int dx = (w() - text_width - 5 * sep) / 4; // horizontal distance
    int dy = h() - sep;
    int Y = y() + sep / 2;
    if (dx >= 80 || dy >= 80)
      width[1] = 9;
    else if (dx >= 60 || dy >= 60)
      width[1] = 8;
    else if (dx >= 40 || dy >= 40)
      width[1] = 7;
    else
      width[1] = 5;

    // draw the lines
    for (int i = 0; i < 2; i++, X += dx + sep) { // thin + thick lines
      fl_line_style(style, width[i]);
      // ___
      //    |
      //    |
      fl_line(X, Y, X + dx, Y, X + dx, Y + dy);
      X += dx + sep;
      // ___
      //   /
      //  /
      fl_line(X, Y, X + dx, Y, X, Y + dy);
    }

    // restore line settings to default
    fl_line_style(FL_SOLID, 0);
  }
};

int main(int argc, char **argv) {
  Fl_Double_Window win(740, 400, "fl_line_style()");
  win.color(FL_WHITE);

  // create grid with a nice white 4px border and a
  // light gray background (color) so margins and gaps show thru
  Fl_Grid grid(4, 4, win.w() - 8, win.h() - 8);
  grid.box(FL_FLAT_BOX);
  grid.color(0xd0d0d000);
  grid.layout(6, 2, 4, 4);  // 6 rows, 2 columns, ...

  // first column
  StyleBox sb00(FL_SOLID,      0, 0);
  StyleBox sb01(FL_DASH,       1, 0);
  StyleBox sb02(FL_DOT,        2, 0);
  StyleBox sb03(FL_DASHDOT,    3, 0);
  StyleBox sb04(FL_DASHDOTDOT, 4, 0);
  StyleBox sb05(-1,            5, 0); // empty box

  // second column
  StyleBox sb10(FL_CAP_FLAT,   0, 1);
  StyleBox sb11(FL_CAP_ROUND,  1, 1);
  StyleBox sb12(FL_CAP_SQUARE, 2, 1);
  StyleBox sb13(FL_JOIN_MITER, 3, 1);
  StyleBox sb14(FL_JOIN_ROUND, 4, 1);
  StyleBox sb15(FL_JOIN_BEVEL, 5, 1);

  grid.end();
  win.end();
  win.resizable(win);
  win.size_range(660, 340); // don't allow to shrink too much
  win.show(argc, argv);
  return Fl::run();
}