Murga-Projects Forums
Response from Message Box - Printable Version

+- Murga-Projects Forums (
+-- Forum: Project Forums (/forumdisplay.php?fid=1)
+--- Forum: MurgaLua - General (/forumdisplay.php?fid=2)
+--- Thread: Response from Message Box (/showthread.php?tid=415)

Response from Message Box - ceeuu - 07-29-2011 06:24 AM

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?



RE: Response from Message Box - mikshaw - 07-30-2011 12:15 AM

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.

RE: Response from Message Box - ceeuu - 07-30-2011 03:04 AM

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 )


RE: Response from Message Box - mikshaw - 07-31-2011 01:11 AM

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

RE: Response from Message Box - ceeuu - 07-31-2011 11:09 AM

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).


RE: Response from Message Box - roydobbins - 08-05-2011 11:37 AM

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