Xaver Xaver - 6 days ago 6
C++ Question

c++ FLTK Image redraw

I am making a simple FLTK application (in linux) that needs to display PNG images in FL_Window. The next code:

Fl_PNG_Image* pngImg;
Fl_Box* boxImage;
boxImage = new Fl_Box(500, 470, 0, 0);
pngImg = new Fl_PNG_Image("main.png");
boxImage->image(pngImg);
boxImage->redraw();`


Draws it OK. But when i like this:
enter image description here
enter image description here
enter image description here

My image destructs. Which callback i need to call to avoid image destruction? How to correctly update box? How to reload image from disk mannualy?

Answer

Try this: (source: http://osdir.com/ml/lib.fltk.general/2004-07/msg00396.html)

Note: As I can't recreate your error I'm not sure it will solve it but it seems plausible that for whatever reason your main window isn't being redrawn when it should be.

Subclass the parent window and get it do manually redraw its contents and nudge FLTK to do it as soon as possible with Fl::check() whenever it is moved so that you have something like

class Mywin : public Fl_Window {
    void resize(int X, int Y, int W, int H) {
        Fl_Window::resize(X,Y,W,H);
        redraw();
        Fl::check();
    }
public:
    Mywin(int x,int y,int w, int h) : Fl_Window(x,y,w,h) {
    }
};

int main() {


    Mywin* win = new Mywin(20,20,800,800);
    Fl_Box* box = new Fl_Box(100,100,300,200);//for example


    Fl_PNG_Image* pngImg = new Fl_PNG_Image("main.png");
    box->image(pngImg);
    box->show();
    win->end();
    win->show();
    return (Fl::run());
}
Comments