Murga-Projects Forums

Full Version: Response from Message Box
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have an app with a main window that I need to display a sort of Message Box to the user to confirm whether to continue with a particular operation. The Message Box is created in a function that is called, the function defines the window and its components, populates a text box from the contents of a function argument, and awaits the user to select one of two buttons..

At the end of the function a call to 'window:show()' is used to display the Message Box... but control returns to the calling code immediately. The Message Box window is defined as being modal, which helps to keep the user from clicking off it and trying to continue without responding... that's good, however, I am unable to return to the calling code which button the user chose.

Is there a recommended way to do this?


I'm not super clear on what you're doing (caffeine hasn't kicked in), but I will say that I've had very little success using modal. It might be an OS-specific thing, or a window manager-specific thing, but for me it tends to steal focus and never return it. So I have stayed away from modal.
Here is a little example to see what I am trying to do.. The code doesn't run without the return at the bottom of the function (or similar).. what I would want is to somehow block on the call to the function until a button is pressed, or a return statement is encountered...

function displayMsg( msg )
local object = fltk:Fl_Double_Window( 200, 110, "Message" );
msg_window = object;
msg_window:box( fltk.Fl_UP_BOX );
do msg_box = fltk:Fl_Box( 15, 15, 170, 40, msg );
msg_box:box( fltk.FL_DOWN_BOX );
do continue_button = fltk:Fl_Button( 15, 70, 70, 24, "Continue" );
function( button_cb )
print( "Continue button Selected" );
return 1;
end )
do cancel_button = fltk:Fl_Button( 130, 70, 55, 24, "Cancel" );
function( button_cb )
print( "Cancel button Selected" );
return 2;
end )
-- execution falls through and returns to calling code,
-- how to remain in function until button is pressed ???
-- adding a return here to allow sample code to work...
return 3;

local object = fltk:Fl_Double_Window( 220, 200, "Msg Test" );
main_window = object;
main_window:box( fltk.Fl_UP_BOX );
do my_button = fltk:Fl_Button( 160, 160, 50, 25, "Push" );
function( button_cb )
print( "button press.." );
print( "calling displayMsg.." );
rc = displayMsg( "Continue or Not?" );
print( "return from displayMsg.." );
print( "return code: "..rc );
end )

Are you aware that Fltk has a built-in dialog that can be used in this way? The fl_choice dialog prevents access to its parent window until a choice is made, either by selection of one of the choices or by escaping out of the dialog.

In this example fl_choice is used as an exit confirmation. If "yes" is not chosen, the dialog closes and nothing happens. You could easily enough add a default condition, in case the user simply closes the dialog.

function my_exit()
  my_confirm=fltk.fl_choice("sure you want to quit?","no","yes",NULL)
  if my_confirm == 1 then os.exit(0) end

I wasn't aware of that call, I'll try it out and see if it will do what I am after (looks like it will).


ceeuu Wrote:
I wasn't aware of that call, I'll try it out and see if it will do what I am after (looks like it will).


In case you want to pursue this further, here is the general approach that i have used for this type of problem:

--launch the dialog, as in your example, setting it modal
--as part of calling the dialog function, supply a callback function as a parameter
--when user clicks a button in the dialog, invoke the callback function, including any data you want to pass back

the caller is still active while the dialog is open, and can process any callbacks

user needs to exit the dialog before main app can continue

this is an asynchronous model, is very flexible and can be used in many situations

hope this helps

Reference URL's