Cairo and FLUID

162 views
Skip to first unread message

David Topham

unread,
Feb 10, 2016, 10:55:35 PM2/10/16
to fltk.general
I have been experimenting with using Cairo in an FLTK program and like the results for 2D drawing.

But I have run into a "bug":  If I code the FLTK program myself, including FL/Fl_Cairo_Window.H, then the program builds fine like this:
g++ -I ~/fltk-1.3.3 -I /usr/include/cairo `fltk-config --use-cairo --cxxflags --ldflags`

(since I had built fltk using  -enable-cairo  configure switch in my home directory from source)

However, If I try to use FLUID (and rename the Fl_Double_Window Class to Fl_Cairo_Window), then
it will only build if I post-edit the cxx file created by FLUID to remove the include of FL/Fl.H

Is there a suggested way for me to configure FLUID so that it does not insert the include to Fl.H ?
Or what is the best way to build a Cairo window using FLUID?

Thanks for any suggestions, Dave

David Topham

unread,
Feb 10, 2016, 11:12:27 PM2/10/16
to fltk.general
I guess I should include some sample code here to make it easier to see what I mean:
To build I must delete the line: #include <FL/Fl.H>

in tryit.h   (created by FLUID)
#ifndef tryit_h
#define tryit_h
#include <FL/Fl.H>
#include <config.h>
#include <FL/Fl_Cairo_Window.H>
void cwcb(Fl_Cairo_Window* cw,cairo_t* cr);
extern Fl_Cairo_Window *cw; 
#endif 

in tryit.cxx:
// generated by Fast Light User Interface Designer (fluid) version 1.0303

#include "tryit.h"

void cwcb(Fl_Cairo_Window* cw,cairo_t* cr) {
  printf("Hello, World!\n");
}

Fl_Cairo_Window *cw=(Fl_Cairo_Window *)0;

int main(int argc, char **argv) {
  { cw = new Fl_Cairo_Window(100, 100);
    cw->box(FL_FLAT_BOX);
    cw->color(FL_BACKGROUND_COLOR);
    cw->selection_color(FL_BACKGROUND_COLOR);
    cw->labeltype(FL_NO_LABEL);
    cw->labelfont(0);
    cw->labelsize(14);
    cw->labelcolor(FL_FOREGROUND_COLOR);
    cw->align(Fl_Align(FL_ALIGN_TOP));
    cw->when(FL_WHEN_RELEASE);
    cw->end();
  } // Fl_Cairo_Window* cw
  cw->color(FL_WHITE);
  cw->set_draw_cb(cwcb);
  cw->show(argc, argv);
  return Fl::run();
}

MacArthur, Ian (Finmeccanica, UK)

unread,
Feb 11, 2016, 4:48:53 AM2/11/16
to fltkg...@googlegroups.com

> But I have run into a "bug": If I code the FLTK program myself,
> including FL/Fl_Cairo_Window.H, then the program builds fine like this:
> g++ -I ~/fltk-1.3.3 -I /usr/include/cairo `fltk-config --use-cairo
> --cxxflags --ldflags`

> (since I had built fltk using -enable-cairo configure switch in my
> home directory from source)

> However, If I try to use FLUID (and rename the Fl_Double_Window Class to
> Fl_Cairo_Window), then
> it will only build if I post-edit the cxx file created by FLUID to
> remove the include of FL/Fl.H

> Is there a suggested way for me to configure FLUID so that it does not
> insert the include to Fl.H ?


Hmm, a puzzle. It would be helpful if you could post the actual error message and such, so we can see what it does not like.

All fltk widgets descend from Fl.h (as it were) so it should *never* be wrong to include it in a fltk source file.

We need to see why that is triggering an issue here. It should not.


> Or what is the best way to build a Cairo window using FLUID?

I use Cairo with fltk a fair bit (though, as I have said before, never via Fl_Cairo_Window) and it is not hard and pretty much Just Works.
Don't use it with fluid though.

A trawl through the archives will surely find (eventually) one of my posted examples. If not; ask again and I'll post something here.


Note that; whilst I quite like Cairo, I do find it to be *quite slow* for a lot of things. My code usually ifdef's it out on OSX for example, since the stock OSX rendering is nicely anti-aliased and a good deal faster. This is increasingly the case on other host systems too, indeed, so...

> Thanks for any suggestions, Dave
Selex ES Ltd
Registered Office: Sigma House, Christopher Martin Road, Basildon, Essex SS14 3EL
A company registered in England & Wales. Company no. 02426132
********************************************************************
This email and any attachments are confidential to the intended
recipient and may also be privileged. If you are not the intended
recipient please delete it from your system and notify the sender.
You should not copy it or use it for any purpose nor disclose or
distribute its contents to any other person.
********************************************************************

MacArthur, Ian (Finmeccanica, UK)

unread,
Feb 11, 2016, 4:52:13 AM2/11/16
to fltkg...@googlegroups.com

> I guess I should include some sample code here to make it easier to see what
> I mean:
> To build I must delete the line: #include <FL/Fl.H>

> in tryit.h (created by FLUID)
> #ifndef tryit_h
> #define tryit_h
> #include <FL/Fl.H>
> #include <config.h>
> #include <FL/Fl_Cairo_Window.H>
> void cwcb(Fl_Cairo_Window* cw,cairo_t* cr);
> extern Fl_Cairo_Window *cw;
> #endif


That's odd - what is the #include <config.h> doing in there? I don't think config.h should ever be in user generated code.

David Topham

unread,
Feb 12, 2016, 2:49:40 AM2/12/16
to fltkg...@googlegroups.com
Thanks Ian, for taking the time to help with this...I added the include of config.h in a FLUID declaration because without it, the program does not build.  I also need to to remove the include of FL/Fl.H in an editor after FLUID creates the files.

Here is the output from trying to build with FL/Fl.H and config.h included  (in tryit.h created by FLUID)


(is method appropriate to posting error messages?)

These are the errors if I do not include config.h


But, without FL/Fl.H and with config.h, it builds fine using this

fltk-config  --use-cairo --compile tryit.cxx

-Dave

--
You received this message because you are subscribed to a topic in the Google Groups "fltk.general" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/fltkgeneral/_C1OJhMLQl0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to fltkgeneral...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Albrecht Schlosser

unread,
Feb 12, 2016, 7:02:16 AM2/12/16
to fltkg...@googlegroups.com
On 12.02.2016 08:49 David Topham wrote:
> Thanks Ian, for taking the time to help with this...I added the include
> of config.h in a FLUID declaration because without it, the program does
> not build. I also need to to remove the include of FL/Fl.H in an editor
> after FLUID creates the files.
>
> Here is the output from trying to build with FL/Fl.H and config.h
> included (in tryit.h created by FLUID)
>
> http://ix.io/olk
>
> (is method appropriate to posting error messages?)

It would be better to attach a text file (not an image/screenshot) to
the message you are posting so we have the message available even if the
external reference will be removed later. An attachment is preferred
over inlined messages because it keeps formatting intact.

> These are the errors if I do not include config.h
>
> http://ix.io/oln
>
> But, without FL/Fl.H and with config.h, it builds fine using this
>
> fltk-config --use-cairo --compile tryit.cxx

Okay, that's fine (somewhat), but doesn't help for larger programs with
multiple source files.

However, I found a solution - or better - workaround. The culprit lies
in the FLTK header files that require config.h, but this should never be
included in user source files, because it will not be available as an
installed header file (as Ian mentioned).

Here is my modified tryit.h file:

#ifndef tryit_h
#define tryit_h

#if 0
// Solution #1 (bad) - #include <config.h> BEFORE FL/Fl.H
// #include <config.h>
#else
// Solution #2 (still bad, but better)
#define FLTK_HAVE_CAIRO
#define USE_X11
#endif

#include <FL/Fl.H>
#include <FL/Fl_Cairo_Window.H>
void cwcb(Fl_Cairo_Window* cw,cairo_t* cr);
extern Fl_Cairo_Window *cw;

#endif

Comments:

I used both "solutions" successfully under Linux.

Solution #1 includes <config.h> before (!) Fl.H. This enables some
conditional code in Fl.H. This is bad, and FLTK should be fixed. This
will likely be addressed in FLTK 1.4


Solution #2 is my recommended workaround for now. You need

#define FLTK_HAVE_CAIRO

(again: before any other FLTK header files) to enable cairo support in
the FLTK headers.

You do also need to define a platform macro so FLTK "knows" which cairo
headers to #include. USE_X11 is needed for all U*x (Linux) platforms,
but again this is a questionable way (IMHO FLTK's fault). You can take a
look at FL/Fl_Cairo.H and see how this is done. I suggest to

#include <whatever-cairo.h>

in your user code so you don't need to define USE_X11, WIN32, or
__APPLE_QUARTZ__ (the platform macros used in the FLTK lib and defined
in config.h), but this might still fail because of that (IMHO wrong)
error message if no applicable platform macro is defined.

So, having said all that, I believe that we need to fix the FLTK headers
and document a proper way to use Fl_Cairo_Window with FLTK. I suggest to
use workaround (solution) #2 meanwhile.

HTH
Albrecht

Albrecht Schlosser

unread,
Feb 12, 2016, 8:12:14 AM2/12/16
to fltkg...@googlegroups.com
On 12.02.2016 13:01 Albrecht Schlosser wrote:

> Here is my modified tryit.h file:

Okay, I started fixing the FLTK headers. Please see attached patch file.
Here is my new version of tryit.h - but you need to apply the attached
patch and rebuild the library to see if it works. Can you please do this
and report success or fail ?

#ifndef tryit_h
#define tryit_h

// Solution #3 (much better - but FLTK needs to be changed)
#define FLTK_HAVE_CAIRO

// optional (will be #included in Fl_Cairo.H anyway)
// #include <cairo.h>

#include <FL/Fl.H>
#include <FL/Fl_Cairo_Window.H>
void cwcb(Fl_Cairo_Window* cw,cairo_t* cr);
extern Fl_Cairo_Window *cw;

#endif

> So, having said all that, I believe that we need to fix the FLTK headers
> and document a proper way to use Fl_Cairo_Window with FLTK. I suggest to
> use workaround (solution) #2 meanwhile.

The attached patch file removes the dependency on <config.h> or any
platform specific macro definition from the FLTK headers. Please apply
the patch using 'patch -p1'. Essentially this replaces the block marked
with '-' in the first column in FL/Fl_Cairo.H with the single statement

# include <cairo.h>

The removed/replaced block has to be added in cairo/Fl_Cairo.cxx instead.

With this patch in place you only need to '#define FLTK_HAVE_CAIRO'
_before_ #include'ing any FLTK headers. This should hopefully work for
you. I'm waiting for your confirmation - prepared to commit this patch
to svn...

Thanks for your report and support to fix this, BTW.

cairo.patch

David Topham

unread,
Feb 12, 2016, 11:52:18 AM2/12/16
to fltkg...@googlegroups.com
Albrecht, I really appreciate you taking time to look at this and to explain the process.  "solution 2" putting these lines at front of tryit.h
#define FLTK_HAVE_CAIRO
#define USE_X11
does work, but I still need a way to reconfigure FLUID then, since it places the include of Fl/H at beginning of tryit.h even if I add those defines at top of FLUID window.

I tried placing exports of the 2 defines on command line (debian bash), but that did not (seem) to work. Or perhaps we can
add a way for fltk-config to accept defines as an option?

I just noticed your 3rd solution, and I will try that (although I have never applied a patch so I need to read up on that process first).   That would still need a way to influence where FLUID puts the define (so it actually is before Fl.H)
-Dave

David Topham

unread,
Feb 12, 2016, 12:14:51 PM2/12/16
to fltkg...@googlegroups.com
I am showing my ignorance here I am sure, but I get this error when applying the patch:

patch -p1 < cairo.patch
-----------------------------
can't find file to patch at input line 5
Perhaps you used the wrong -p or --strip option?
The text leading up to this was:
--------------------------
|diff --git a/FL/Fl_Cairo.H b/FL/Fl_Cairo.H
|index e114400..b0596a5 100644
|--- a/FL/Fl_Cairo.H
|+++ b/FL/Fl_Cairo.H
--------------------------
File to patch: 
Skip this patch? [y] 
Skipping patch.
1 out of 1 hunk ignored
can't find file to patch at input line 26
Perhaps you used the wrong -p or --strip option?
The text leading up to this was:
--------------------------
|diff --git a/cairo/Fl_Cairo.cxx b/cairo/Fl_Cairo.cxx
|index 1cdad67..51d3ada 100644
|--- a/cairo/Fl_Cairo.cxx
|+++ b/cairo/Fl_Cairo.cxx
--------------------------
File to patch: 
Skip this patch? [y] 
Skipping patch.
1 out of 1 hunk ignored

Albrecht Schlosser

unread,
Feb 12, 2016, 12:20:57 PM2/12/16
to fltkg...@googlegroups.com
On 12.02.2016 18:14 David Topham wrote:
> I am showing my ignorance here I am sure, but I get this error when
> applying the patch:

don't worry, you'll be able to do it in a minute.

> patch -p1 < cairo.patch
> -----------------------------
>
> can't find file to patch at input line 5

The command line is fine. You must run this in the FLTK root directory,
then it should work (just tested it myself).

Otherwise see my previous or next post for a simple description how to
do it with a text editor.

Albrecht Schlosser

unread,
Feb 12, 2016, 12:21:13 PM2/12/16
to fltkg...@googlegroups.com
On 12.02.2016 17:52 David Topham wrote:
> Albrecht, I really appreciate you taking time to look at this and to
> explain the process.

You're welcome.

> "solution 2" putting these lines at front of tryit.h
> #define FLTK_HAVE_CAIRO
> #define USE_X11
> does work, but I still need a way to reconfigure FLUID then, since it
> places the include of Fl/H at beginning of tryit.h even if I add those
> defines at top of FLUID window.

Okay, good point. Note that these "solutions" are not meant to be real
solutions - they are more workarounds than solutions. But I hope that we
can break down the real solution to several smaller steps.

I did not yet consider modifying fluid, but to help with the complete
process, please post your fluid (.fl) file as an attachment so we (I)
can see the whole process.

> I tried placing exports of the 2 defines on command line (debian bash),
> but that did not (seem) to work. Or perhaps we can
> add a way for fltk-config to accept defines as an option?

fltk-config "remembers" all flags you specify when configuring the build
process so that it can show them when you use it, for instance ...

CXXFLAGS="-DFLTK_HAVE_CAIRO" ./configure --enable-cairo
make -j3
fltk-config --cxxflags

... but that is not the solution I'd recommend. We should find a better way.

> I just noticed your 3rd solution, and I will try that (although I have
> never applied a patch so I need to read up on that process first).

just run

$ patch -p1 < cairo.patch

in the fltk source directory. If this doesn't work, use the simple
description I provided (move and block from the .H file to the .cxx file
and replace that block in the .H file with "#include <cairo.h>". It's so
simple that you can do it with a text editor. To find the correct
places, look at the line numbers shown in the "--" and "++" lines.

> That would still need a way to influence where FLUID puts the define (so
> it actually is before Fl.H)

Yep - please post your fluid file, then we can see. I'm not a fluid
expert though, but I believe that you can add a code or declaration
block or something like that as the very first item in fluid so that
this should be exported right at the beginning of the header file.

If that doesn't help you can still add -DFLTK_HAVE_CAIRO to your
compiler command line...

Albrecht Schlosser

unread,
Feb 12, 2016, 12:29:08 PM2/12/16
to fltkg...@googlegroups.com
On 12.02.2016 18:20 Albrecht Schlosser wrote:

small correction:

> fltk-config "remembers" all flags you specify when configuring the build
> process so that it can show them when you use it, for instance ...
>
> CXXFLAGS="-DFLTK_HAVE_CAIRO" ./configure --enable-cairo
> make -j3
> fltk-config --cxxflags

the line above should read:

./fltk-config --cxxflags

and you can shorten this procedure: you don't need to run make to see
the effect, so you can simply execute:

CXXFLAGS="-DFLTK_HAVE_CAIRO" ./configure --enable-cairo
./fltk-config --cxxflags

David Topham

unread,
Feb 12, 2016, 12:41:39 PM2/12/16
to fltkg...@googlegroups.com
FLUID always puts the include of Fl.H before anything else, but the light dawned on me from your last suggestion about using fltk-config differently so that I can have multiple cxx files, and pass defines:

g++ -DFLTK_HAVE_CAIRO -DUSE_X11 tryit.cxx `fltk-config --use-cairo --cxxflags --ldflags` -o tryit

This does the trick!
Here is my fl file:


tryit.fl

David Topham

unread,
Feb 13, 2016, 11:01:50 AM2/13/16
to fltkg...@googlegroups.com
Ok, the patch worked...now the define of USE_X11 is no longer needed.

I still need to define FLTK_HAVE_CAIRO on the command line when invoking fltk-config

This puzzles me a little since I thought the purpose of 
./configure --enable-cairo
was to build FLTK so that it would put into place everything needed to include cairo libraries.
i.e. I thought it would change fltk-config so that if you put 
fltk-config --use-cairo
that would already cause any needed defines to be used.

Anyway, this workaround solves my problem!  Thank you!

Albrecht Schlosser

unread,
Feb 13, 2016, 12:15:25 PM2/13/16
to fltkg...@googlegroups.com
On 13.02.2016 17:01 David Topham wrote:
> Ok, the patch worked...now the define of USE_X11 is no longer needed.

Thanks for confirmation.

> I still need to define FLTK_HAVE_CAIRO on the command line when invoking
> fltk-config

That was to be expected, but - as I wrote before - should not be the
final solution.

> This puzzles me a little since I thought the purpose of
> ./configure --enable-cairo
> was to build FLTK so that it would put into place everything needed to
> include cairo libraries.
> i.e. I thought it would change fltk-config so that if you put
> fltk-config --use-cairo
> that would already cause any needed defines to be used.

Yes, it should. Obviously nobody used it this way (or with fluid as you
tried to do), so nobody noticed this. Generally you should be able to
#include <FL/Fl_Cairo_Window.H> w/o the need to define anything.

We need to change that, but this is not going to be done in FLTK 1.3.x,
because this would change the API, at least AFAICT now. The way FL/Fl.H
includes the cairo stuff is ... well, let's say suboptimal. ;-)

configure --enable-cairo includes the use of cairo stuff in the library,
but as you noticed there is a dependency on <config.h> in FL/Fl.H which
should definitely not be. This is the problem we need to fix. I'm going
to commit the patch you used, and this is the best I can do for now
(i.e. in FLTK 1.3.x). I'll also post an STR for FLTK 1.4 so we don't
forget to fix this.

> Anyway, this workaround solves my problem! Thank you!

I'm glad that we found this workaround. That is at least a way to go for
now. I'll commit the patch, and then you'll be able to use FLTK from
svn, the next weekly snapshot (every Friday) from the download page, or
the next release (1.3.4).

Hopefully we'll find a better way for FLTK 1.4, but as mentioned before,
this might not be 100% source compatible in all cases. I think that just
using Fl_Cairo_Window should work w/o code changes though, hence you
could just omit the definition of FLTK_HAVE_CAIRO when you start using
FLTK 1.4. However, the next release will be 1.3.4, before we'll start
developing 1.4.x.

Albrecht Schlosser

unread,
Feb 13, 2016, 3:11:41 PM2/13/16
to fltkg...@googlegroups.com
On 13.02.2016 18:14 Albrecht Schlosser wrote:

> ... I'll commit the patch, and then you'll be able to use FLTK from
> svn, the next weekly snapshot (every Friday) from the download page, or
> the next release (1.3.4).

The patch is in svn r11167.

I also filed this STR for reference:
http://www.fltk.org/str.php?L3284

David Topham

unread,
Nov 15, 2016, 7:05:08 PM11/15/16
to fltkg...@googlegroups.com
I am happy to see release of 1.3.4...thank you...but I didn't see mention of this patch...does that mean it didn't make it into this release?  Or that it was too small to mention?   

--
You received this message because you are subscribed to a topic in the Google Groups "fltk.general" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/fltkgeneral/_C1OJhMLQl0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to fltkgeneral+unsubscribe@googlegroups.com.

Albrecht Schlosser

unread,
Nov 16, 2016, 5:40:44 AM11/16/16
to fltkg...@googlegroups.com
On 16.11.2016 01:05 David Topham wrote:
> I am happy to see release of 1.3.4...thank you...

Welcome.
> but I didn't see
> mention of this patch...does that mean it didn't make it into this
> release? Or that it was too small to mention?

It was likely just forgotten to mention in CHANGES.

> On Sat, Feb 13, 2016 at 12:10 PM, Albrecht Schlosser
> <Albrech...@online.de <mailto:Albrech...@online.de>> wrote:
>
> On 13.02.2016 18:14 Albrecht Schlosser wrote:
>
> ... I'll commit the patch, and then you'll be able to use FLTK from
> svn, the next weekly snapshot (every Friday) from the download
> page, or
> the next release (1.3.4).
>
>
> The patch is in svn r11167.

The patch mentioned above is included, but it doesn't solve the entire
problem. The log says:

------------------------------------------------------------------------
r11167 | AlbrechtS | 2016-02-13 18:35:36 +0100 (Sat, 13 Feb 2016) | 18 lines

[Cairo] Remove part of the dependency of FL/Fl.H on config.h.

To use Fl_Cairo_Window one ddi not only need to configure with
--enable-cairo,
but also needed to #include <config.h> before #include <FL/Fl.H>.

This commit removes the smaller part of the dependency of the public header
file FL/Fl.H on config.h. This needs a better solution in FLTK 1.4.0.

The user still needs to define FLTK_HAVE_CAIRO _before_ #include <FL/Fl.H>.

Note: This is not compatible with fluid, since fluid _always_ includes
FL/Fl.H as the first statement in generated header files.

See also the discussion in fltk.general:

https://groups.google.com/d/msg/fltkgeneral/_C1OJhMLQl0/pHUFtz9SCwAJ

--- end of log ---

> I also filed this STR for reference:
> http://www.fltk.org/str.php?L3284 <http://www.fltk.org/str.php?L3284>

To sum it up: I changed the FLTK header file FL/Fl.H but I didn't change
fluid. This was postponed to FLTK 1.4, sorry for that. So this will not
be solved entirely in FLTK 1.3.

The discussion mentioned above shows some workarounds for use in FLTK
1.3. Since fluid always generates #include <FL/Fl.H> as the first
statement (sorry, I couldn't change this) you will still have to define
one or more compiler macros on the commandline, either with configure
(if you want to use fltk-config --cxxflags later), or in your own build
system, something like the "solution" here:

https://groups.google.com/d/msg/fltkgeneral/_C1OJhMLQl0/B5oZe_NgCwAJ

I think what you'd have to do is to #define FLTK_HAVE_CAIRO on the
command line, like this:

g++ -DFLTK_HAVE_CAIRO tryit.cxx `fltk-config --use-cairo --cxxflags
--ldflags` -o tryit

... and to #include <cairo.h> as needed in files where you use cairo in
your own code (this was previously done in FL/Fl.H).

I think that this will enable you to use cairo, even with source files
generated by fluid.

I hope we can find a better solution in FLTK 1.4 (STR #3284).

If you are logged in to the FLTK website and go to STR #3284 then you
can enter your email address in the "Update Notification" field (and
click the button), and you will get notifications of changes on this
STR. That's the best I can offer for now.

http://www.fltk.org/str.php?L3284

Reply all
Reply to author
Forward
0 new messages