On 30 Dec 2008, at 17:36, Greg Ercolano wrote:

>       Possibly fl_xid() is what you really want, as IIRC that returns
>       the native API's platform specific 'window handle' that you would
>       use to interact directly with X or WIN32, or whatever.

Yes, if the OP is intent on using AGG to do the rendering, then  
getting a handle to the window via fl_xid() or similar is probably  
the way to go.

I was going to post an example I had of doing that (albeit using  
Cairo rather than AGG for the rendering) but I can't seem to find it  
now... I think I may have posted it on this list in the past, so it  
might show up with a search.

Oh! Found something - this is not the example I was looking for, but  
it does show how to get the window handle on OSX, linux and Win32 and  
then render into that drawing context with Cairo. I guess something  
*similar* could work for AGG.

Cheers,
-- 
Ian

//////////////////////////////////////////////////////////////////////
#include <FL/Fl.H>
#include <FL/x.H>
#include <FL/Fl_Group.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Button.H>
#include <FL/fl_draw.H>

#include <cairo.h>

#ifdef WIN32
#  include <cairo-win32.h>
#elif defined (__APPLE__)
#  include <cairo-quartz.h>
#else
#  include <cairo-xlib.h>
#endif

// You can use this to switch whether the base-class is a Window or a  
Box
// Actually, a box works fine for most uses and is simpler...
#ifdef WIN_BASE
#  warning Fl_Double_Window base
#  define BOX Fl_Double_Window
#else
#  define BOX Fl_Box
#endif

/*************************************************************/
class pg_preview : public BOX
{
        void draw(void); // draw method
        cairo_surface_t *surface;
        cairo_t *cairo_context;
        void set_cairo_cxt(int wo, int ho);
public:
        // constructor
        pg_preview(int x, int y, int w, int h) : BOX(x,y,w,h) {
                surface = NULL; cairo_context = NULL;}
};
/*************************************************************/
void pg_preview::set_cairo_cxt(int wo, int ho)
{
#ifdef WIN32
//#warning win32 mode
        /* Get a Cairo surface for the current DC */
        HDC dc = fl_gc;         /* Exported by fltk */
        surface = cairo_win32_surface_create(dc);
#elif defined (__APPLE__)
//#warning Apple Quartz mode
        /* Get a Cairo surface for the current CG context */
        CGContext *ctx = fl_gc;
        surface = cairo_quartz_surface_create_for_cg_context(ctx, wo, ho);
#else
//#warning X windows mode
        /* Get a Cairo surface for the current display */
        surface = cairo_xlib_surface_create(fl_display, fl_window, fl_visual- 
 >visual, wo, ho);
#endif
        /* Store the cairo context */
        cairo_context = cairo_create(surface);

        /* All Cairo co-ordinates are shifted by 0.5 pixels to correct anti- 
aliasing */
        cairo_translate(cairo_context, 0.5, 0.5);
}
/*************************************************************/
void pg_preview::draw(void) {
#ifdef WIN_BASE
        int xo = 0; // origin is (0,0) for Fl_Window
        int yo = 0;
#else
        int xo = x(); // origin is current window position for Fl_Box
        int yo = y();
#endif
        int wo = w();
        int ho = h();

        // draw the base widget
        fl_push_no_clip(); // remove any clipping region set by the expose  
events...
        fl_push_clip(xo, yo, wo, ho); // reset local clipping

        fl_color(FL_WHITE);
        fl_rectf(xo, yo, wo, ho);
        // setting cairo conext *after* setting clip regions is OK
        set_cairo_cxt(wo, ho);
        // draw a 70% red triangle pointing upwards
        int x1, y1, x2, y2, x3, y3;
        x1 = xo + wo/2; y1 = yo;
        x2 = xo + wo; y2 = yo + ho;
        x3 = xo; y3 = y2;
        cairo_set_source_rgba(cairo_context, 1.0, 0.0, 0.0, 0.7);
        cairo_new_path(cairo_context);
        cairo_move_to(cairo_context, x1, y1);
        cairo_line_to(cairo_context, x2, y2);
        cairo_line_to(cairo_context, x3, y3);
        cairo_line_to(cairo_context, x1, y1);
        cairo_fill(cairo_context);
        // release the cairo context
        cairo_destroy(cairo_context);
        cairo_surface_destroy(surface);
        cairo_context = NULL;

        fl_pop_clip(); // remove the local clip region
        fl_pop_clip(); // remove the "no_clip" region
} // draw method

/*************************************************************/
Fl_Double_Window *preview_win=(Fl_Double_Window *)0;
pg_preview *pv_box=(pg_preview *)0;
/*************************************************************/
static void cb_Quit(Fl_Button*, void*) {
   preview_win->hide();
}
/*************************************************************/
int main(int argc, char **argv) {
        preview_win = new Fl_Double_Window(600, 855, "Page Preview");
        preview_win->begin();

        pv_box = new pg_preview(5, 5, 590, 800); // in points, A4 is  
595x841, so 590x800 is neatly inside that
        pv_box->box(FL_FLAT_BOX);
        
                Fl_Group *btn_grp = new Fl_Group(2, 810, 598, 43);
                btn_grp->begin();
                        Fl_Button* close = new Fl_Button(535, 815, 60, 30, 
"Close");
                        close->box(FL_THIN_UP_BOX);
                        close->callback((Fl_Callback*)cb_Quit);
                btn_grp->end();
                btn_grp->box(FL_THIN_DOWN_BOX);
        preview_win->end();
//      preview_win->resizable(pv_box);

        preview_win->show(argc, argv);
        pv_box->show();
        pv_box->redraw();
        return Fl::run();
}
/* end of file */



_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk

Reply via email to