magiblot / tvision

A modern port of Turbo Vision 2.0, the classical framework for text-based user interfaces. Now cross-platform and with Unicode support.
Other
1.99k stars 150 forks source link

Fire Event when TDialog Window is closed on the Close-Button-Character #142

Closed paule32 closed 7 months ago

paule32 commented 7 months ago

Hello, I have a TDialog, but I can not trace the "Close" Action, when I click the left top Corner, to close the TDialog. I have add flags, but the handleEvent Event-Handler shows me not the MessageBox.

options   |= ofSelectable | ofTopSelect;
        flags     |= wfMove
                  |  wfGrow
                  |  wfClose
                  |  wfZoom;
...
void handleEvent(TEvent & event ){
    TWindow::handleEvent( event );
    if (event.what == evCommand) {
        switch (event.message.command) {
            case cmClose:
            messageBox("blaa blaa",mfInformation|mfOKButton);
                close();
                clearEvent(event);
                break;
        }
    }
}

How can I make Action's when Windows/Dialogs are closed ?

magiblot commented 7 months ago

That's because the cmClose command is also handled by TWindow::eventHandle. So, in this case, you should check for the cmClose command before calling TWindow::eventHandle.

However, you must take into account that the close() method destroys the current object, so you must make sure that you do not interact with it after calling close(): do not call clearEvent nor TWindow::handleEvent if you called close(). For example:

void handleEvent(TEvent & event ){
    // Handle cmClose first to override the handling in TWindow.
    if (event.what == evCommand && event.message.command == cmClose)
    {
        messageBox("blaa blaa",mfInformation|mfOKButton);
        clearEvent(event);
        close();
        // Don't do anything after close().
        return;
    }
    TWindow::handleEvent( event );
    // Handle other events...
}
magiblot commented 7 months ago

Or even better, override the close method:

void close() override {
    messageBox("blaa blaa",mfInformation|mfOKButton);
    TWindow::close();
}

Then you won't have to do anything special in your handleEvent.

paule32 commented 7 months ago

Thank You. This was a great Solution !