diff options
| author | Greg Ercolano <erco@seriss.com> | 2022-01-16 15:22:16 -0800 |
|---|---|---|
| committer | Greg Ercolano <erco@seriss.com> | 2022-01-16 15:22:16 -0800 |
| commit | 6546814a23ac8653916ca9a5e97f2110889ade3d (patch) | |
| tree | 5d702fd7841f3e60c2383d7a67dc3e8ff1e78567 /examples/cairo-draw-x.cxx | |
| parent | 4041608e9487bb59e14593c52aca91054ff79cdf (diff) | |
For issue #358 - adds examples/cairo-draw-x.cxx
Since this is the first cairo example in the examples directory,
it necessarily involved changes to the Makefile and to fltk-config
to properly handle the absence/existance of the cairo libs.
TBD: Add docs to the cario widget describing coordinate system
and how it differs from the default cairo normalized coordinate system.
Diffstat (limited to 'examples/cairo-draw-x.cxx')
| -rw-r--r-- | examples/cairo-draw-x.cxx | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/examples/cairo-draw-x.cxx b/examples/cairo-draw-x.cxx new file mode 100644 index 000000000..8129e607d --- /dev/null +++ b/examples/cairo-draw-x.cxx @@ -0,0 +1,58 @@ +// +// Simple demo of drawing an "X" in Cairo (antialiased lines) +// +// Copyright 1998-2022 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 // Builds example code only if cairo enabled +#include <FL/Fl_Cairo_Window.H> + +// Cairo rendering cb called during Fl_Cairo_Window::draw() +static void my_cairo_draw_cb(Fl_Cairo_Window *window, cairo_t *cr) { + const double xmax = (window->w() - 1); + const double ymax = (window->h() - 1); + + // Draw green "X" + // Draws an X to four corners of resizable window. + // See Fl_Cairo_Window docs for more info. + // + cairo_set_antialias(cr, CAIRO_ANTIALIAS_BEST); // use best antialiasing + cairo_set_line_width(cr, 1.00); // line width for drawing + cairo_set_source_rgb(cr, 1.0, 0.5, 0.0); // orange + cairo_move_to(cr, 0.0, 0.0); cairo_line_to(cr, xmax, ymax); // draw diagonal "\" + cairo_move_to(cr, 0.0, ymax); cairo_line_to(cr, xmax, 0.0); // draw diagonal "/" + cairo_stroke(cr); // stroke the lines +} + +int main(int argc, char **argv) { + Fl_Cairo_Window window(300, 300, "Cairo Draw 'X'"); + window.color(FL_BLACK); // cairo window's default bg color + window.size_range(50,50,-1,-1); // allow resize 50,50 and up + window.resizable(&window); // allow window to be resized + window.set_draw_cb(my_cairo_draw_cb); // draw callback for cairo drawing + window.show(argc, argv); + return Fl::run(); +} + +// The code that follows just allows the example to build even if cairo wasn't enabled. +#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) |
