fltk/examples/cairo-draw-x.cxx
Greg Ercolano 196430b016 Issue #358 cont'd: Fl_Cairo_Window coord system docs
Elaborated on Fl_Cairo_Window's use of FLTK style coordinates,
    and how this differs from cairo's default native normalized
    coordinate system, and shows how to switch from one to the other.

    Also, small comment fix to the cairo example regarding the "X" color.
2022-01-16 16:59:49 -08:00

59 lines
2.5 KiB
C++

//
// 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 orange "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)