Hi all. I wrote a widget that, when asked, dumps itself, so to speak,
into a PNM. This works fine if I invoke its pnm-save method when the
main window is exiting (by clicking the window's cross or Esc-aping).
If I use first a Fl_File_Chooser (the way I intended to use more
often) or even if I quit the program via an item in a Fl_Menu, all I
got is garbage. I have prepared a little example that replicates this
behaviour. I'm using Ubuntu 8.10 and 9.04, FLTK 1.1.9 (compiled by
me). In Windows a similar problem arises. I even tried show()ing the
window before using fl_read_image, to no avail.
The error is:
X_GetImage: BadDrawable (invalid Pixmap or Window parameter) 0x7400006
To compile this example:
g++ pnmtest.cpp -lfltk -o pnmtest
The code:
// Begin pnmtest.cpp
#include <FL/Fl.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Button.H>
#include <FL/fl_draw.H>
#include <FL/Fl_File_Chooser.H>
#include <stdlib.h>
#include <stdio.h>
int save_pnm(const char *name, Fl_Double_Window *win){
FILE *fp;
if ((fp = fopen(name, "w+")) == 0) {
perror("open");
exit(1);
}
uchar *buf = (uchar*)malloc(3*win->w()*win->h()*sizeof(uchar));
fl_read_image(buf, 0, 0, win->w(), win->h());
fprintf(fp, "P6\n%d %d\n255\n", win->w(), win->h());
fwrite((unsigned char *)buf, win->w() * win->h() * 3, 1, fp);
fclose(fp);
free(buf);
return 0;
}
void wincb(Fl_Widget*, void *w){
Fl_Double_Window *win = (Fl_Double_Window*)w;
save_pnm("fig.pnm", win);
exit(0);
return;
}
void btncb(Fl_Widget*, void *w){
Fl_Double_Window *win = (Fl_Double_Window*)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->show();
Fl::check();
save_pnm(chooser.value(), win);
}
return;
}
int main(int argc, char *argv[]){
Fl_Double_Window 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();
}
// End pnmtest.cpp
Thanks to all,
Rodrigo
_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk