Search code examples
c++user-interfacefltk

Fl_Window subclass doesn't work


As titled above, i'm trying to get an extremely simple FLTK 1.3.3 application working. But, even with only a simple Fl_Window and 1 Fl_Button, nothing seems to work. Can anyone help?

class MainEditorWindow : public Fl_Window
{
public:
    MainEditorWindow(int _width, int _height, std::string _title);
    ~MainEditorWindow();

    virtual void draw();
    virtual int handle(int _event);
private:
    Fl_Button* m_btnExit;
};

And here is the Implementation

MainEditorWindow::~MainEditorWindow()
{

}

int MainEditorWindow::handle(int _event)
{
    return 1;
}

void MainEditorWindow::draw()
{
    m_btnExit->redraw();
}

MainEditorWindow::MainEditorWindow(int _width, int _height, std::string _title) : Fl_Window(_width, _height, _title.c_str())
{
    this->begin();
    m_btnExit = new Fl_Button(0, 0, 40, 40, "EXIT");
    m_btnExit->color(FL_RED);
    this->color(FL_WHITE);
    this->end();
}

But when simply running the application like this:

int main(int argc, char** argv)
{
    MainEditorWindow* mw = new MainEditorWindow(800, 600, "SHIP Editor");
    mw->show(argc,argv);
    return Fl::run();
}

The window shows up fine, its resizable movable etc, the draw() - function is being called and all that. But the window itself is just blank. It simply shows nothing, especially not the Fl_Button. Can anybody tell me why this occurs? As far as i can tell, there should be nothing particularily wrong with my code.


Solution

  • You need to call Fl_Window::draw()

        void MainEditorWindow::draw()
        {
          m_btnExit->redraw();
          Fl_Window::draw();
        }
    

    And maybe you want the button is clickable too

        int MainEditorWindow::handle(int _event)
        {
          //return 1;
          return(Fl_Window::handle(_event));
        }