2008-09-25 22:26:33 +04:00
|
|
|
//
|
2019-02-12 17:09:42 +03:00
|
|
|
// Cairo drawing test program for the Fast Light Tool Kit (FLTK).
|
2008-09-25 22:26:33 +04:00
|
|
|
//
|
2023-03-14 20:46:56 +03:00
|
|
|
// Copyright 1998-2023 by Bill Spitzak and others.
|
2008-09-25 22:26:33 +04:00
|
|
|
//
|
2011-07-19 08:49:30 +04:00
|
|
|
// 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:
|
|
|
|
//
|
2019-02-12 17:09:42 +03:00
|
|
|
// https://www.fltk.org/COPYING.php
|
2008-09-25 22:26:33 +04:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// Please see the following page on how to report bugs and issues:
|
2010-04-28 07:15:47 +04:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// https://www.fltk.org/bugs.php
|
2008-09-25 22:26:33 +04:00
|
|
|
//
|
|
|
|
|
2021-12-19 00:42:30 +03:00
|
|
|
#include <FL/Fl.H> // includes <FL/fl_config.h>
|
2008-09-25 22:26:33 +04:00
|
|
|
|
2021-12-19 00:42:30 +03:00
|
|
|
#ifdef FLTK_HAVE_CAIRO // defined in <FL/fl_config.h> since FLTK 1.4.0
|
2008-09-25 22:26:33 +04:00
|
|
|
|
|
|
|
#include <FL/Fl_Cairo_Window.H>
|
|
|
|
#include <FL/Fl_Box.H>
|
2018-02-01 00:17:17 +03:00
|
|
|
#include <FL/platform.H>
|
2008-09-25 22:26:33 +04:00
|
|
|
#include <FL/fl_draw.H>
|
|
|
|
#include <FL/math.h>
|
2021-12-19 00:42:30 +03:00
|
|
|
|
2008-09-25 22:26:33 +04:00
|
|
|
#define DEF_WIDTH 0.03
|
|
|
|
|
2022-04-12 10:20:34 +03:00
|
|
|
// This demo program can be used in 3 modes. All 3 modes require configure
|
|
|
|
// option --enable-cairo or CMake OPTION_CAIRO.
|
|
|
|
//
|
|
|
|
// 1) using class Fl_Cairo_Window useful when all the content of a window
|
|
|
|
// is drawn with Cairo.
|
|
|
|
// This is achieved setting #define USE_FL_CAIRO_WINDOW 1 below
|
|
|
|
// or
|
|
|
|
// 2) showing how to draw in an Fl_Double_Window using both Cairo and
|
|
|
|
// the FLTK drawing API.
|
|
|
|
// This is achieved setting #define USE_FL_CAIRO_WINDOW 0 below
|
|
|
|
// or
|
|
|
|
// 3) showing how to use "cairo extended use".
|
|
|
|
// This is achieved when FLTK was built with one more option
|
|
|
|
// (configure --enable-cairoext or CMake OPTION_CAIROEXT)
|
|
|
|
// which defines the preprocessor variable FLTK_HAVE_CAIROEXT.
|
|
|
|
// If Fl::cairo_autolink_context(true); is called at the beginning
|
2023-03-09 20:16:43 +03:00
|
|
|
// of main(), any overridden draw() function gets access to an adequate
|
2022-04-12 10:20:34 +03:00
|
|
|
// Cairo context with Fl::cairo_cc() without having to call
|
|
|
|
// Fl::cairo_make_current(Fl_Window*).
|
|
|
|
|
|
|
|
|
|
|
|
#define USE_FL_CAIRO_WINDOW 1
|
2008-09-25 22:26:33 +04:00
|
|
|
|
2019-11-21 17:03:14 +03:00
|
|
|
// draw centered text
|
2019-02-12 16:55:43 +03:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2008-09-25 22:26:33 +04:00
|
|
|
|
2019-11-21 17:03:14 +03:00
|
|
|
// draw a button object with rounded corners and a label
|
|
|
|
|
2019-02-12 16:55:43 +03:00
|
|
|
static void round_button(cairo_t *cr, double x0, double y0,
|
2020-07-01 19:03:10 +03:00
|
|
|
double rect_width, double rect_height, double radius,
|
|
|
|
double r, double g, double b) {
|
2019-02-12 16:55:43 +03:00
|
|
|
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);
|
2008-09-25 22:26:33 +04:00
|
|
|
} else {
|
2019-02-12 16:55:43 +03:00
|
|
|
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);
|
2008-09-25 22:26:33 +04:00
|
|
|
}
|
2019-02-12 16:55:43 +03:00
|
|
|
} 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 =
|
2019-11-21 17:03:14 +03:00
|
|
|
// 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);
|
2019-02-12 16:55:43 +03:00
|
|
|
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);
|
|
|
|
|
2019-11-21 17:03:14 +03:00
|
|
|
cairo_set_font_size(cr, 0.075);
|
2019-02-12 16:55:43 +03:00
|
|
|
centered_text(cr, x0, y0, rect_width, rect_height, "FLTK loves Cairo!");
|
2008-09-25 22:26:33 +04:00
|
|
|
}
|
2019-02-12 16:55:43 +03:00
|
|
|
|
2019-11-21 17:03:14 +03:00
|
|
|
// draw the entire image (3 buttons), scaled to the given width and height
|
2019-02-12 16:55:43 +03:00
|
|
|
|
2019-11-21 17:03:14 +03:00
|
|
|
void draw_image(cairo_t *cr, int w, int h) {
|
2019-02-12 16:55:43 +03:00
|
|
|
|
|
|
|
cairo_set_line_width(cr, DEF_WIDTH);
|
|
|
|
cairo_scale(cr, w, h);
|
|
|
|
|
2019-11-21 17:03:14 +03:00
|
|
|
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()
|
|
|
|
|
|
|
|
|
2022-04-12 10:20:34 +03:00
|
|
|
#if USE_FL_CAIRO_WINDOW && !defined(FLTK_HAVE_CAIROEXT)
|
|
|
|
|
|
|
|
typedef Fl_Cairo_Window cairo_using_window;
|
|
|
|
|
2023-03-12 17:49:00 +03:00
|
|
|
#else // !USE_FL_CAIRO_WINDOW || defined(FLTK_HAVE_CAIROEXT)
|
2022-04-12 10:20:34 +03:00
|
|
|
|
|
|
|
class cairo_using_window : public Fl_Double_Window {
|
|
|
|
void (*draw_with_cairo_)(cairo_using_window*, cairo_t*);
|
2023-03-14 20:46:56 +03:00
|
|
|
|
2022-04-12 10:20:34 +03:00
|
|
|
public:
|
2023-03-14 20:46:56 +03:00
|
|
|
|
2022-04-12 10:20:34 +03:00
|
|
|
cairo_using_window(int w, int h, const char *title) : Fl_Double_Window(w, h, title) {
|
|
|
|
Fl_Box *box = new Fl_Box(FL_NO_BOX, 0, 0, w, 25,
|
2023-03-14 20:46:56 +03:00
|
|
|
"Cairo and FLTK API in Fl_Double_Window");
|
2022-04-12 10:20:34 +03:00
|
|
|
box->labelfont(FL_TIMES_BOLD);
|
|
|
|
box->labelsize(12);
|
|
|
|
box->labelcolor(FL_BLUE);
|
|
|
|
}
|
2023-03-14 20:46:56 +03:00
|
|
|
void draw() FL_OVERRIDE {
|
2022-04-12 10:20:34 +03:00
|
|
|
Fl_Window::draw(); // perform drawings with the FLTK API
|
|
|
|
#ifndef FLTK_HAVE_CAIROEXT
|
|
|
|
Fl::cairo_make_current(this); // announce Cairo will be used in this window
|
|
|
|
#endif
|
|
|
|
cairo_t *cc = Fl::cairo_cc(); // get the adequate Cairo context
|
2023-03-12 17:49:00 +03:00
|
|
|
draw_with_cairo_(this, cc); // draw in this window using Cairo
|
|
|
|
|
|
|
|
// flush Cairo drawings: necessary at least for Windows
|
2023-03-14 20:46:56 +03:00
|
|
|
Fl::cairo_flush(cc);
|
2022-04-12 10:20:34 +03:00
|
|
|
}
|
2023-03-14 20:46:56 +03:00
|
|
|
|
2022-04-12 10:20:34 +03:00
|
|
|
void set_draw_cb( void (*cb)(cairo_using_window*, cairo_t*)) {
|
|
|
|
draw_with_cairo_ = cb;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // USE_FL_CAIRO_WINDOW
|
|
|
|
|
|
|
|
// Cairo rendering cb called during Fl_Cairo_Window::draw()
|
|
|
|
// or cairo_using_window::draw().
|
|
|
|
static void my_cairo_draw_cb(cairo_using_window *window, cairo_t *cr) {
|
2019-11-21 17:03:14 +03:00
|
|
|
draw_image(cr, window->w(), window->h());
|
2008-09-25 22:26:33 +04:00
|
|
|
}
|
|
|
|
|
2019-02-12 16:55:43 +03:00
|
|
|
int main(int argc, char **argv) {
|
2022-04-12 10:20:34 +03:00
|
|
|
#ifdef FLTK_HAVE_CAIROEXT
|
2008-09-25 22:26:33 +04:00
|
|
|
Fl::cairo_autolink_context(true);
|
|
|
|
#endif
|
2023-03-14 20:46:56 +03:00
|
|
|
cairo_using_window window(350, 350, "FLTK loves Cairo");
|
2018-07-27 12:05:12 +03:00
|
|
|
|
|
|
|
window.resizable(&window);
|
|
|
|
window.color(FL_WHITE);
|
|
|
|
window.set_draw_cb(my_cairo_draw_cb);
|
2019-02-12 16:55:43 +03:00
|
|
|
window.show(argc, argv);
|
2011-01-06 16:17:21 +03:00
|
|
|
|
2018-07-27 12:05:12 +03:00
|
|
|
return Fl::run();
|
2008-09-25 22:26:33 +04:00
|
|
|
}
|
2021-12-19 00:42:30 +03:00
|
|
|
|
|
|
|
#else // (!FLTK_HAVE_CAIRO)
|
|
|
|
|
2008-09-25 22:26:33 +04:00
|
|
|
#include <FL/fl_ask.H>
|
|
|
|
|
2019-02-12 16:55:43 +03:00
|
|
|
int main(int argc, char **argv) {
|
2019-02-12 17:09:42 +03:00
|
|
|
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.");
|
2008-09-25 22:26:33 +04:00
|
|
|
return 0;
|
|
|
|
}
|
2021-12-19 00:42:30 +03:00
|
|
|
#endif // (FLTK_HAVE_CAIRO)
|
2022-04-12 10:20:34 +03:00
|
|
|
|