Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

P.S: 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.

like image 475
Richard Avatar asked Aug 16 '26 02:08

Richard


2 Answers

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, e.g. in your main function:

window->callback( win_cb );
like image 194
Daniel Hanrahan Avatar answered Aug 18 '26 16:08

Daniel Hanrahan


You probably need to look at using a modal (i.e., 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.

like image 39
Brett Hale Avatar answered Aug 18 '26 15:08

Brett Hale



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!