Standard dialog.
MessageBox is class for quickly building ordianary dialogs with applied layout. This means following: it will resize to fit larger text input or it will resize added buttons (to fit their labels), applying that to whole window too.
This class is not meant to be used to construct complicated dialogs nor to construct dialogs with different elements or layout.
So how to be used? MessageBox is by default MSGBOX_PLAIN, which means that is ordianary dialog without any input. And here is the code:
mb.set_text("This is sample text");
mb.add_button("&Close");
mb.run_plain();
MessageBox(MessageBoxType t=MSGBOX_PLAIN)
This sample will run dialog with provided text and one 'Close' button. By default, this button will not have any callback attached to it, so adding is done like:
...
...
@ MSGBOX_BUTTON_PLAIN
Ordinary button (Fl_Button)
Definition: MessageBox.h:59
Now, clicking on 'Close' button, it will call some_callback_func. Passing data to callback function is done via fltk style, like:
add_button() can accept previously declared Fl_Button with already set callback or properties so you can add it like:
...
Fl_Button* b = new Fl_Button(...);
b->callback(...);
mb.add_button(b);
If you want to add Fl_Return_Button (button with 'enter' as shortcut), using MSGBOX_BUTTON_RETURN parameter will do the job, like:
...
Fl_Return_Button* b = new Fl_Return_Button(...);
@ MSGBOX_BUTTON_RETURN
Button with 'enter' shortcut (Fl_Return_Button)
Definition: MessageBox.h:60
- Note
- If you added pre-allocated Fl_Button or Fl_Return_Button, make sure it is not deleted somewhere in the code since MessageBox will do that. What this means ? This means that added Fl_Button or Fl_Return_Button must not be inside begin()/end() functions, nor added to some group via add() or data will be deleted twice, crashing program probably.
Adding more buttons is done via calling add_button() multiple times. You can add max 4 buttons to dialog. This is dialog with 'Yes' and 'No' buttons:
mb.set_text("Would you like to quit");
mb.add_button("&No", ...);
mb.add_button("&Yes", ...);
mb.run_plain();
When multiple buttons are added, they should be added in reverse order, which means that first added button will be at right edge of dialog and any further added will be placed toward left edge.
If you want dialog with input field (max. 1 input field is allowed), this is how:
mb.set_text("Please input something");
mb.add_button("&Close me", ...);
mb.run_plain();
printf("You entered %s\n", mb.get_input());
@ MSGBOX_INPUT
Dialog with input field.
Definition: MessageBox.h:49
get_input() will return NULL if nothing was entered or if MSGBOX_PLAIN was set.
Here is full sample of dialog requesting some password, where typed data is hidden with asterisks:
void close_cb(Fl_Widget*, void* b) {
b->hide();
}
mb.set_text("Please enter password");
mb.run_plain();
const char* ret = mb.get_input();
if(ret)
printf("You entered %s\n", ret);
else
printf("Nothing was entered");
@ MSGBOX_INPUT_SECRET
Dialog with secret input field.
Definition: MessageBox.h:50
Setting callbacks each time just to get some status can be cumbersome, so there is a run() function which is a shortcut for run_plain() with callbacks attached to each button. This function will close dialog and return number of pressed button (starting from most right and 0); in case dialog was closed without pressing on any button (like calling hide() or closing it via window manager) it will return -1.