FXRuby usage: Capturing results from an FXDialogBox

Hi all,

I’m working on a script to query some decisions from the user. So
there’s a loop iterating over pairs of alternatively possible choices.

Now, during querying the main program shouldn’t be available, thus some
kind of modal dialog box seems appropriate.

Up to now I wrote the following stuff, which is likely not the most
elegant way, but let’s follow the “Make it parse, make it work, make it
nice, make it fast (IF neccessary” maxime for a moment. (But then I’m
open for any improvements!)

class PriorizeDialog < FXDialogBox

attr_reader :result

include Responder

ID_ITEM1,
ID_ITEM2 = enum( FXMainWindow::ID_LAST , 2 )

def initialize( owner, item1, item2 )

 @result = 0

 FXMAPFUNC( SEL_COMMAND, PriorizeDialog::ID_ITEM1, "onItem1" )
 FXMAPFUNC( SEL_COMMAND, PriorizeDialog::ID_ITEM2, "onItem2" )

 # Invoke base class initialize function first
 super(owner, "Priorize pair", DECOR_TITLE|DECOR_BORDER)

 # Bottom buttons
 buttons = FXHorizontalFrame.new(self, LAYOUT_SIDE_BOTTOM |
   FRAME_NONE | LAYOUT_FILL_X|PACK_UNIFORM_WIDTH, 0, 0, 0, 0, 40, 

40,
20, 20)

 # Select item 2
 FXButton.new(buttons, "&2. #{item2}", nil, self, ID_ITEM2,
   FRAME_RAISED|FRAME_THICK|LAYOUT_RIGHT|LAYOUT_CENTER_Y)

 # Select item 1
 FXButton.new(buttons, "&1. #{item1}", nil, self, ID_ITEM1,
   FRAME_RAISED|FRAME_THICK|LAYOUT_RIGHT|LAYOUT_CENTER_Y)

end

def onItem1( sender, sel, ptr )
@result = 1
end

def onItem2( sender, sel, ptr )
@result = 2
end

end

Now that doen’t work: The dialog will never quit.
My guess is that the script never runs into code that sends something
like ID_ACCEPT/ID_CANCEL to the appropriate object (which is self I think).

What’s the ruby way do accomplish this?
I have the stron feeling that my way of fiddling with @result may not be
too terribly rubyish - is there a better (Ruby) way to do this?

Thanks for any comments.

Stephan

Stephan Kämper wrote:

Now, during querying the main program shouldn’t be available, thus some
kind of modal dialog box seems appropriate.

Yes.

Up to now I wrote the following stuff, which is likely not the most
elegant way, but let’s follow the “Make it parse, make it work, make it
nice, make it fast (IF neccessary” maxime for a moment. (But then I’m
open for any improvements!)

Now that doen’t work: The dialog will never quit.
My guess is that the script never runs into code that sends something
like ID_ACCEPT/ID_CANCEL to the appropriate object (which is self I think).

Your guess is exactly right. Once the dialog is displayed it needs some
hint (such as an ID_ACCEPT or ID_CANCEL message) to tell it to go away.

What’s the ruby way to accomplish this?

For most cases like this you shouldn’t actually need to subclass
FXDialogBox. I would recommend that you take a look at the
imageviewer.rb example program that comes with FXRuby, because it pops
up a few modal dialog boxes here and there to collect inputs from the
user. In particular, take a look at the onCmdScale() and onCmdCrop()
methods in that example.

I have the stron feeling that my way of fiddling with @result may not be
too terribly rubyish - is there a better (Ruby) way to do this?

If you use the technique shown in the ImageViewer example, the user’s
selection should get mapped directly into an FXDataTarget. But another
equally easy approach would be to check the two radio buttons to see
which one is set (by calling FXRadioButton#check).

Hope this helps,

Lyle