summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorManoloFLTK <41016272+ManoloFLTK@users.noreply.github.com>2022-04-12 09:20:34 +0200
committerManoloFLTK <41016272+ManoloFLTK@users.noreply.github.com>2022-04-12 09:20:34 +0200
commitfad867a5d37b5162255a338f5f5218ff852f8852 (patch)
treeab4bda71568da79af50fc8699a7a2fb2b41cbc94 /test
parent0d59e32b848953be121a86ccd068847dfa28d8fc (diff)
Modify test/cairo_test.cxx to show roles of OPTION_CAIRO and OPTION_CAIROEXT.
Also make clear that these options require, for now, Fl_Double_Window to work cross-platform.
Diffstat (limited to 'test')
-rw-r--r--test/cairo_test.cxx68
1 files changed, 60 insertions, 8 deletions
diff --git a/test/cairo_test.cxx b/test/cairo_test.cxx
index 1fbdb0c90..a30a65334 100644
--- a/test/cairo_test.cxx
+++ b/test/cairo_test.cxx
@@ -1,7 +1,7 @@
//
// Cairo drawing test program for the Fast Light Tool Kit (FLTK).
//
-// Copyright 1998-2021 by Bill Spitzak and others.
+// 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
@@ -26,8 +26,28 @@
#define DEF_WIDTH 0.03
-// uncomment the following line to enable Cairo context autolink feature:
-// #define AUTOLINK
+// 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
+// of main(), any overriden draw() function gets access to an adequate
+// Cairo context with Fl::cairo_cc() without having to call
+// Fl::cairo_make_current(Fl_Window*).
+
+
+#define USE_FL_CAIRO_WINDOW 1
// draw centered text
@@ -126,18 +146,49 @@ void draw_image(cairo_t *cr, int w, int h) {
} // draw_image()
-// Cairo rendering cb called during Fl_Cairo_Window::draw()
-static void my_cairo_draw_cb(Fl_Cairo_Window *window, cairo_t *cr) {
+#if USE_FL_CAIRO_WINDOW && !defined(FLTK_HAVE_CAIROEXT)
+
+typedef Fl_Cairo_Window cairo_using_window;
+
+#else // !USE_FL_CAIRO_WINDOW
+
+class cairo_using_window : public Fl_Double_Window {
+ void (*draw_with_cairo_)(cairo_using_window*, cairo_t*);
+public:
+ 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,
+ "use Cairo and the FLTK API in Fl_Double_Window");
+ box->labelfont(FL_TIMES_BOLD);
+ box->labelsize(12);
+ box->labelcolor(FL_BLUE);
+ }
+ void draw() {
+ 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
+ draw_with_cairo_(this, cc); // draw in this window using Cairo
+ }
+ 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) {
draw_image(cr, window->w(), window->h());
- return;
}
int main(int argc, char **argv) {
-#ifdef AUTOLINK
+#ifdef FLTK_HAVE_CAIROEXT
Fl::cairo_autolink_context(true);
#endif
- Fl_Cairo_Window window(300, 300, "FLTK loves Cairo");
+ cairo_using_window window(300, 300, "FLTK loves Cairo");
window.resizable(&window);
window.color(FL_WHITE);
@@ -159,3 +210,4 @@ int main(int argc, char **argv) {
return 0;
}
#endif // (FLTK_HAVE_CAIRO)
+