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.
This commit is contained in:
Greg Ercolano 2022-01-16 15:22:16 -08:00
parent 4041608e94
commit 6546814a23
4 changed files with 85 additions and 9 deletions

View File

@ -6,6 +6,7 @@ SHELL = /bin/sh
# Executables
ALL = browser-simple$(EXEEXT) \
cairo-draw-x$(EXEEXT) \
chart-simple$(EXEEXT) \
draggable-group$(EXEEXT) \
howto-add_fd-and-popen$(EXEEXT) \
@ -44,6 +45,14 @@ ALL = browser-simple$(EXEEXT) \
# default target -- build everything
default all: $(ALL)
# Special rules for building cairo app
cairo-draw-x.o: cairo-draw-x.cxx
@echo "*** Compile $<..."
$(CXX) -I.. $(CXXFLAGS_CAIRO) -c $< -o $@
cairo-draw-x$(EXEEXT): cairo-draw-x.o
@echo "*** Link $<..."
$(CXX) $< $(LINKFLTK) $(LINKFLTK_CAIRO) -o $@
# clean everything
clean:
$(RM) $(ALL)

View File

@ -16,13 +16,15 @@ ifeq '$(OS)' "Windows_NT"
EXEEXT = .exe
endif
FLTKCONFIG = ../fltk-config
CXX = $(shell $(FLTKCONFIG) --cxx)
CXXFLAGS = $(shell $(FLTKCONFIG) --cxxflags) -Wall -I.
LINKFLTK = $(shell $(FLTKCONFIG) --ldstaticflags)
LINKFLTK_GL = $(shell $(FLTKCONFIG) --use-gl --ldstaticflags)
LINKFLTK_IMG = $(shell $(FLTKCONFIG) --use-images --ldstaticflags)
LINKFLTK_ALL = $(shell $(FLTKCONFIG) --use-images --use-gl --ldstaticflags)
FLTKCONFIG = ../fltk-config
CXX = $(shell $(FLTKCONFIG) --cxx)
CXXFLAGS = $(shell $(FLTKCONFIG) --cxxflags) -Wall -I.
LINKFLTK = $(shell $(FLTKCONFIG) --ldstaticflags)
LINKFLTK_GL = $(shell $(FLTKCONFIG) --use-gl --ldstaticflags)
LINKFLTK_IMG = $(shell $(FLTKCONFIG) --use-images --ldstaticflags)
LINKFLTK_ALL = $(shell $(FLTKCONFIG) --use-images --use-gl --ldstaticflags)
CXXFLAGS_CAIRO = $(shell $(FLTKCONFIG) --use-cairo --cxxflags)
LINKFLTK_CAIRO = $(shell $(FLTKCONFIG) --use-cairo --ldstaticflags)
.SUFFIXES: .cxx .h .fl .o $(EXEEXT)
# HOW TO COMPILE

58
examples/cairo-draw-x.cxx Normal file
View File

@ -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)

View File

@ -49,6 +49,13 @@ LDLIBS="@LIBS@"
OPTIM="@OPTIM@"
CAIROFLAGS="@CAIROFLAGS@"
# Config
if grep -q '^#define FLTK_HAVE_CAIRO 1' $selfdir/FL/fl_config.h; then
FLTK_HAVE_CAIRO=1
else
FLTK_HAVE_CAIRO=0
fi
# Check for local invocation, and update paths accordingly...
if test -f "$selfdir/FL/Fl_Window.H"; then
includedir="$selfdir"
@ -75,7 +82,7 @@ if test -d $includedir/FL/images; then
CXXFLAGS="-I$includedir/FL/images $CXXFLAGS"
fi
if test -f "$libdir/libfltk_cairo.a"; then
if [ $FLTK_HAVE_CAIRO = 1 ]; then
CFLAGS="$CAIROFLAGS $CFLAGS"
CXXFLAGS="$CAIROFLAGS $CXXFLAGS"
fi
@ -249,7 +256,7 @@ if test x$use_images = xyes; then
LDSTATIC="$libdir/libfltk_images.a $STATICIMAGELIBS $LDSTATIC"
fi
if test x$use_cairo = xyes; then
if test x$use_cairo = xyes -a $FLTK_HAVE_CAIRO = 1; then
LDLIBS="-lfltk_cairo$SHAREDSUFFIX $CAIROLIBS $LDLIBS"
LDSTATIC="$libdir/libfltk_cairo.a $CAIROLIBS $LDSTATIC"
fi