Albrecht, Alvin, Ian:

I recoded a bit, in particular I derived a class from
Fl_Double_Window. Now the area of the main window which is covered by
the Fl_File_Chooser when starting to save is dumped as a black region.
If the Fl_File_Chooser doesn't cover at all the window the dump is Ok.
Using fl_begin_offscreen()/fl_end_offscreen() does not alter the
result. I guess that a really quick-and-dirty solution would be to add
a time delay before reading. The code follows.

Rodrigo

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

#include <stdlib.h>
#include <stdio.h>

class MyWindow : public Fl_Double_Window {
    public:
        MyWindow(int x, int y, int w, int h, const char
*l):Fl_Double_Window(x, y, w, h, l){}
        void save_pnm(const char *name){
            Fl_Offscreen off = fl_create_offscreen(w(), h());

            FILE *fp;
            if ((fp = fopen(name, "w+")) == 0) {
                perror("open");
                return;
            }

            uchar *buf = (uchar*)malloc(3*w()*h()*sizeof(uchar));

            make_current();
            //fl_begin_offscreen(off);

            for (int i = 0; i < children(); i++){
                child(i)->damage(FL_DAMAGE_ALL);
                child(i)->redraw();
            }
            redraw();
            Fl::flush();
            fl_read_image(buf, 0, 0, w(), h());
            //fl_end_offscreen();

            fprintf(fp, "P6\n%d %d\n255\n", w(), h());
            fwrite((uchar*)buf, w()*h()*3, 1, fp);
            fclose(fp);

            free(buf);
            fl_delete_offscreen(off);
        return;
    }
};

void wincb(Fl_Widget*, void *w){
    MyWindow *win = (MyWindow*)w;

    win->save_pnm("fig.pnm");

    exit(0);
    return;
}

void btncb(Fl_Widget*, void *w){
    MyWindow *win = (MyWindow*)w;

    Fl_File_Chooser chooser(".", "*.pnm", Fl_File_Chooser::CREATE,
"Save as PNM");
    chooser.show();

    while(chooser.shown()) Fl::wait();

    if (chooser.value() != NULL){
        win->save_pnm(chooser.value());
    }

    return;
}

int main(int argc, char *argv[]){
    MyWindow win(10, 10, 200, 100, "Test");

    Fl_Button btn(10, 10, win.w() - 20, win.h() - 20, "Save as PNM");
    btn.callback(btncb, &win);

    win.end();
    win.callback(wincb, &win);

    win.show();

    return Fl::run();
}

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

Reply via email to