summaryrefslogtreecommitdiff
path: root/test/cairo_test.cxx
blob: 1fbdb0c9083144616f1a52f0495169184c4301cc (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
//
// Cairo drawing test program for the Fast Light Tool Kit (FLTK).
//
// Copyright 1998-2021 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
//

#include <FL/Fl.H>      // includes <FL/fl_config.h>

#ifdef FLTK_HAVE_CAIRO  // defined in <FL/fl_config.h> since FLTK 1.4.0

#include <FL/Fl_Cairo_Window.H>
#include <FL/Fl_Box.H>
#include <FL/platform.H>
#include <FL/fl_draw.H>
#include <FL/math.h>

#define DEF_WIDTH 0.03

// uncomment the following line to enable Cairo context autolink feature:
// #define AUTOLINK

// draw centered text

static void centered_text(cairo_t *cr, double x0, double y0, double w0, double h0, const char *my_text) {
  cairo_select_font_face(cr, "Sans", CAIRO_FONT_SLANT_OBLIQUE, CAIRO_FONT_WEIGHT_BOLD);
  cairo_set_source_rgba(cr, 0.9, 0.9, 0.4, 0.6);
  cairo_text_extents_t extents;
  cairo_text_extents(cr, my_text, &extents);
  double x = (extents.width / 2 + extents.x_bearing);
  double y = (extents.height / 2 + extents.y_bearing);
  cairo_move_to(cr, x0 + w0 / 2 - x, y0 + h0 / 2 - y);
  cairo_text_path(cr, my_text);
  cairo_fill_preserve(cr);
  cairo_set_source_rgba(cr, 0, 0, 0, 1);
  cairo_set_line_width(cr, 0.004);
  cairo_stroke(cr);
  cairo_set_line_width(cr, DEF_WIDTH);
}

// draw a button object with rounded corners and a label

static void round_button(cairo_t *cr, double x0, double y0,
                         double rect_width, double rect_height, double radius,
                         double r, double g, double b) {
  double x1, y1;
  x1 = x0 + rect_width;
  y1 = y0 + rect_height;
  if (!rect_width || !rect_height)
    return;
  if (rect_width / 2 < radius) {
    if (rect_height / 2 < radius) {
      cairo_move_to(cr, x0, (y0 + y1) / 2);
      cairo_curve_to(cr, x0, y0, x0, y0, (x0 + x1) / 2, y0);
      cairo_curve_to(cr, x1, y0, x1, y0, x1, (y0 + y1) / 2);
      cairo_curve_to(cr, x1, y1, x1, y1, (x1 + x0) / 2, y1);
      cairo_curve_to(cr, x0, y1, x0, y1, x0, (y0 + y1) / 2);
    } else {
      cairo_move_to(cr, x0, y0 + radius);
      cairo_curve_to(cr, x0, y0, x0, y0, (x0 + x1) / 2, y0);
      cairo_curve_to(cr, x1, y0, x1, y0, x1, y0 + radius);
      cairo_line_to(cr, x1, y1 - radius);
      cairo_curve_to(cr, x1, y1, x1, y1, (x1 + x0) / 2, y1);
      cairo_curve_to(cr, x0, y1, x0, y1, x0, y1 - radius);
    }
  } else {
    if (rect_height / 2 < radius) {
      cairo_move_to(cr, x0, (y0 + y1) / 2);
      cairo_curve_to(cr, x0, y0, x0, y0, x0 + radius, y0);
      cairo_line_to(cr, x1 - radius, y0);
      cairo_curve_to(cr, x1, y0, x1, y0, x1, (y0 + y1) / 2);
      cairo_curve_to(cr, x1, y1, x1, y1, x1 - radius, y1);
      cairo_line_to(cr, x0 + radius, y1);
      cairo_curve_to(cr, x0, y1, x0, y1, x0, (y0 + y1) / 2);
    } else {
      cairo_move_to(cr, x0, y0 + radius);
      cairo_curve_to(cr, x0, y0, x0, y0, x0 + radius, y0);
      cairo_line_to(cr, x1 - radius, y0);
      cairo_curve_to(cr, x1, y0, x1, y0, x1, y0 + radius);
      cairo_line_to(cr, x1, y1 - radius);
      cairo_curve_to(cr, x1, y1, x1, y1, x1 - radius, y1);
      cairo_line_to(cr, x0 + radius, y1);
      cairo_curve_to(cr, x0, y1, x0, y1, x0, y1 - radius);
    }
  }
  cairo_close_path(cr);

  cairo_pattern_t *pat =
    // cairo_pattern_create_linear (0.0, 0.0,  0.0, 1.0);
    cairo_pattern_create_radial(0.25, 0.24, 0.11, 0.24, 0.14, 0.35);
  cairo_pattern_set_extend(pat, CAIRO_EXTEND_REFLECT);

  cairo_pattern_add_color_stop_rgba(pat, 1.0, r, g, b, 1);
  cairo_pattern_add_color_stop_rgba(pat, 0.0, 1, 1, 1, 1);
  cairo_set_source(cr, pat);
  cairo_fill_preserve(cr);
  cairo_pattern_destroy(pat);

  // cairo_set_source_rgb (cr, 0.5, 0.5, 1); cairo_fill_preserve (cr);
  cairo_set_source_rgba(cr, 0, 0, 0.5, 0.3);
  cairo_stroke(cr);

  cairo_set_font_size(cr, 0.075);
  centered_text(cr, x0, y0, rect_width, rect_height, "FLTK loves Cairo!");
}

// draw the entire image (3 buttons), scaled to the given width and height

void draw_image(cairo_t *cr, int w, int h) {

  cairo_set_line_width(cr, DEF_WIDTH);
  cairo_scale(cr, w, h);

  round_button(cr, 0.1, 0.1, 0.8, 0.2, 0.4, 1, 0, 0);
  round_button(cr, 0.1, 0.4, 0.8, 0.2, 0.4, 0, 1, 0);
  round_button(cr, 0.1, 0.7, 0.8, 0.2, 0.4, 0, 0, 1);

} // draw_image()

// Cairo rendering cb called during Fl_Cairo_Window::draw()

static void my_cairo_draw_cb(Fl_Cairo_Window *window, cairo_t *cr) {
  draw_image(cr, window->w(), window->h());
  return;
}

int main(int argc, char **argv) {
#ifdef AUTOLINK
  Fl::cairo_autolink_context(true);
#endif
  Fl_Cairo_Window window(300, 300, "FLTK loves Cairo");

  window.resizable(&window);
  window.color(FL_WHITE);
  window.set_draw_cb(my_cairo_draw_cb);
  window.show(argc, argv);

  return Fl::run();
}

#else // (!FLTK_HAVE_CAIRO)

#include <FL/fl_ask.H>

int main(int argc, char **argv) {
  fl_message_title("This program needs a Cairo enabled FLTK library");
  fl_message(
    "Please configure FLTK with Cairo enabled (--enable-cairo or --enable-cairoext)\n"
    "or one of the CMake options OPTION_CAIRO or OPTION_CAIROEXT, respectively.");
  return 0;
}
#endif // (FLTK_HAVE_CAIRO)