简体   繁体   中英

FLTK Closing window

I am using FLTK. I have a window with a variety of buttons the user can click to perform some action. In my int main() i have a switch statement to handle all of these. When the user clicks exit the switch statement is setup like so:

case Exit_program:
    cout << "save files and exit\n";
    do_save_exit(sw);

This goes to the do_save_exit function that creates a exit confirmation window with two buttons yes (exit) and no (don't exit). I got the yes button to work, exits the program, but the no button mean i should just hide the confirmation window. This is the follow functions:

void yes(Address addr, Address)
{
    exit(0);
}
void no(Address addr, Address)
{

}
void do_save_exit(Window& w)
{
    Window quit(Point(w.x()+100, w.y()+100), 250, 55, "Exit confirmation");
    Text conf(Point(15,15),"Do you really want to save and exit?");
    Button yes(Point(60, 20),35,30,"Yes",yes);
    Button no(Point(140, 20),35,30,"No",no);
    quit.attach(conf);
    quit.attach(yes);
    quit.attach(no);
    wait_for_main_window_click();
}

The problem is, when i click the no button it goes to void no, but I can't go anywhere from there. I just want to do quit.hide() but the no function doesn't have sight of the quit window (out of scope). How should I proceed? Thank you

PS: I have thought about using a pointer to the quit window and then using the pointer to quit the window in the no function but am not sure how to do that exactly.

The Fl_Window callback is called when an attempt is made to close the window. The default callback hides the window (and if all windows are hidden, your application ends). If you set your own window callback, you can override this behaviour, so as not to hide the window:

// This window callback allows the user to save & exit, don't save, or cancel.
static void window_cb (Fl_Widget *widget, void *) 
{
    Fl_Window *window = (Fl_Window *)widget;

    // fl_choice presents a modal dialog window with up to three choices.
    int result = fl_choice("Do you want to save before quitting?", 
                           "Don't Save",  // 0
                           "Save",        // 1
                           "Cancel"       // 2
                           );
    if (result == 0) {  // Close without saving
        window->hide();
    } else if (result == 1) {  // Save and close
        save();
        window->hide();
    } else if (result == 2) {  // Cancel / don't close
        // don't do anything
    }
}

Set your window's callback wherever you set up your Fl_Window , eg in your main function:

window->callback( win_cb );

You probably need to look at using a modal (ie, dialog) window. Look at <FL/fl_ask.h>

if (fl_ask("Do you really want to save and exit?"))
    save_and_exit();

The header also has functions for the popup's font, title, etc.

No need to use hide(). You can simply use exit(0); in a callback.

When you build you don't get an error or warning? The problem is probably that you have both global functions names yes and no and also local variables called just the same. Rename either the functions of the variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM