return a value when a button is clicked in shoes ruby - ruby

I have an app using shoes. In the model, I have
def run()
query = get_queries
executeQuery(query)
run()
end
Then this get_queries will append some buttons to stack, say
button "do 1"
button "do 2"
button "do 3"
button "do 4"
If the user clicks on the button, get_queries should return the corresponding query (Sometimes it may ask the user for more info, so basically, it returns an array containing query and info). And then the buttons are removed.
However, I am confused as to how to let the buttons return value for that function. I thought of using a variable and check if the variable is updated by busy waiting, but I don't think that is the efficient/right way to do it.
Any suggestions? Or is there another way to this?

I think you need to make your program work somewhat differently. Make get_queries append the buttons to the stack, as it does now, but move the other things from the run method into another method that is called only after the button receives a click-event.
def on_click(button_number)
executeQuery(button_number)
run()
end
button "do 1" do
on_click 1
end
button "do 2" { on_click 2 }
button "do 3" { on_click 3 }
...

Related

adding events to random buttons of the keyboard

Hello guys i really need your help . I want to make some program in javafx so i need to add listener to random generated button on the keyboard. For example :
i don't want to add some action if user type enter but i want to add action to some random button and nobody would know which is that button . so the user need to click every single button on the keyboard to find out which is that button.How would he knows that this is the correct button ? - > well the program will be executed , when he click on the wrong button -> nothing will happen.
Try something like this:
private void handleKeyPress(KeyEvent ke) {
String text = ke.getText();
KeyCode code = ke.getCode();
if ( ke.isControlDown() || ke.isMetaDown() ) {
//DO something for example
}
}

SketchUp API: How to add a check box to a menu item

I don't see it anywhere in the Ruby API documentation but just in case I'm missing something...
I'm writing a plugin for SketchUp and I'm trying to add some options to the menu bar. One of my options would work best as a checkbox, but right now I have to have two separate buttons. Is there a way to create a checkbox menu item with the Ruby API?
Here's what I had to do instead:
foo = true
UI.menu("Plugins").add_item("Turn foo_option on") { #foo = true }
UI.menu("Plugins").add_item("Turn foo_option off") { #foo = false }
...and then I just use foo to change the options. Is there a cleaner way to do this?
SketchUp can have check marks in menu items. Both menu items and commands can have a validation proc. The documentation for set_validation_proc gives this example:
plugins_menu = UI.menu("Plugins")
item = plugins_menu.add_item("Test") { UI.messagebox "My Test Item"}
status = plugins_menu.set_validation_proc(item) {
if Sketchup.is_pro?
MF_ENABLED
else
MF_GRAYED
end
}
Although for checkmarks you would use the constants MF_CHECKED and MF_UNCHECKED
http://www.sketchup.com/intl/en/developer/docs/ourdoc/menu#set_validation_proc
http://www.sketchup.com/intl/en/developer/docs/ourdoc/command#set_validation_proc
I have not seen a checkbox menu item created from an extensions before, but I'm a beginning user so that's maybe why.
An other approach would be to do it like this:
unless file_loaded?(__FILE__)
plugin_menu = UI.menu("Plugin")
option_menu = plugin_menu.add_submenu("NameOfOption")
option_menu.add_item("OptionA"){ }
option_menu.add_item("OptionB"){ }
file_loaded(__FILE__)
end
The file_loaded?(_ FILE _) makes sure the menu only is created once, instead of every time you load your script. I hope this is helpfull. Maybe some experts now a way to create a checkbox menu.

A Confirmation Box On Exit

I want to have hta code such that:
When the user clicks on the "Close" button, it pops up a confirmation box.
If the user confirms "Yes", then it will quit otherwise it keeps running.
Thanks in advance.
In HTML, you need a close button, it can be an input with button type (but in fact, any part of an HTML page can get a 'onclick' property, like a in a table)
<input type="button" value="Close" onclick='F_closeConfirm'>
then the VB code should contain a function F_closeConfirm with something like this
Public Function F_closeConfirm()
'one way to ask
If MsgBox("Are you sure you want to quit?", vbYesNo, "Confirmation") = vbYes Then
'another way
iAnswer = MsgBox("Like, really quit?", vbYesNo, "Confirmation")
If iAnswer = vbYes Then
'method to close parent windows or tab, can differ but it's another topic
Window.Parent.Close
End If
End If
End Function

call a toolbar button click from another form

In VB6 I need to know how to call a button click event on anther form. The another form part is easy but how to pass the click event the proper method to "click" the right button on the toolbar is the real issue.
Here is the vent on the main form - i need to call the click event case "Copyfrom".
MainForm
Public Sub tbrMain_ButtonClick(ByVal Button As MSComctlLib.Button)
Select Case Button.Index
Case ToolBarItem.tbPrint
'(some code)
Case ToolBarItem.tbSave
'(some code)
Case ToolBarItem.tbCopyFrom
'(some code)
Case ToolBarItem.tbNEW
'(etc)
I tried
Mainform.tbrMain_ButtonClick()
and even tried passing the index number and key - no dice.
The event handler is expecting to receive a reference to an actual toolbar button, so you have to pass the toolbar button itself, not it's Caption or Key, e.g.:
Form1.tbrMain_ButtonClick Form1.tbrMain.Buttons(1)
Or, using the Call statement:
Call Form1.tbrMain_ButtonClick(Form1.tbrMain.Buttons(1))
If you set the Key properties on your toolbar buttons, you can use the Key property of the desired button in place of the (1):
Form1.tbrMain_ButtonClick Form1.tbrMain.Buttons("PrintButton")
Private Sub Toolbar1_ButtonMenuClick(ByVal ButtonMenu As MSComctlLib.ButtonMenu)
If ButtonMenu.Key = "A" Then
MsgBox "Button-1"
ElseIf ButtonMenu.Key = "B" Then
MsgBox "Button -2"
ElseIf ButtonMenu.Key = "C" Then
MsgBox "Button -3"
ElseIf ButtonMenu.Key = "D" Then
MsgBox "Button -4"
ElseIf ButtonMenu.Key = "E" Then
MsgBox "Button -5"
End If
End Sub

Pressing the Enter key instead of Clicking button with Shoes (Ruby)

As the title suggests, I'm just looking for a way of pressing a button in Shoes without clicking it.
I've searched the forum, but unfortunately can't find anything.
Thanks
that won't work in red shoes under windows since the windows controlls steal all the events.
I managed to get this at work in green shoes under windows though.
Here an example
['green_shoes'].each(&method(:require))
Shoes.app{
e = edit_line
button("Click me!"){alert("You clicked me.")}
keypress { |k| alert("You pressed Enter") if k == "\n"}
}
Grtz
I do not have shoes at the moment, but I believe you could trap the keypress and execute its action like so:
button('Click me') { do_something }
keypress { |k| do_something if k == '\n' }
From the manual:
One caveat to all of those rules: normally the Return key gives you a
string "\n". When pressed with modifier keys, however, you end up
with :control_enter, :control_alt_enter, :shift_alt_enter

Resources