The Ruby WebDialog class allows you to create and interact with DHTML dialog
boxes from Ruby. This is the best way to generate complex, embedded UIs
inside SketchUp, but it does generally require HTML and Javascript expertise.
If your goal is to simply display a website to your users, consider using
UI.getURL, which will show them a web page in their default browser rather
than inside a dialog in SketchUp.
See this blog post for a detailed, step-by-step example:
http://sketchupapi.blogspot.com/2008/02/sharing-data-between-sketchup-ruby-and.html
The add_action_callback method establishes a Ruby callback method that your
web dialog can call to perform some function.
Use the skp:callback_method_name to invoke the callback method from your
webdialog. Your JavaScript in the webdialog will invoke the callback method
with a string representing arguments to the callback method.
Note that you're sending data down to Ruby as a single string that's
passed via the window.location bar. In Internet Explorer on PC, there is
a length limit of 2038 characters for this bar, so if you're
needing to pass large data down you might consider using get_element_value
to pull in a longer string from a hidden input field in the HTML.
Arguments:
Returns:
# In Ruby code...
dlg.add_action_callback("ruby_messagebox") {|dialog, params|
UI.messagebox("You called ruby_messagebox with: " + params.to_s)
}
# JavaScript inside the page loaded into the WebDialog:
# window.location = 'skp:ruby_messagebox@Hello World';By default, actions are only allowed on the host where the webdialog is displayed. The allow_actions_from_host method is used to selectively allow actions to take place on a host remote from the host where the webdialog exists. If the webdialog is local, no remote host is allowed unless you use this method.
Arguments:
Returns:
dialog.allow_actions_from_host("google.com")The bring_to_front method is used to bring the webdialog to the front of all the windows on the desktop. See show_modal for how to ensure that your WedDialogs are on top.
Returns:
dialog.bring_to_front
The close method is used to close the webdialog.
Returns:
dialog.close
The execute_script method is used to execute a JavaScript string on the web dialog.
Arguments:
Returns:
js_command = "document.getElementById('id').innerHTML = 'Hi!'"
dialog.execute_script(js_command)The get_default_dialog_color method is used to get the default dialog color for the web dialog.
Returns:
dialog.get_default_dialog_color #ece9d8
The get_element_value method is used to get a value, with a given element_id, from the web dialog's DOM.
Arguments:
Returns:
# In Ruby code:
dialog.get_element_value("myTextInput")
# In webdialog's HTML:
# The max_height method is used to get the maximum height that the user is allowed to resize the dialog to.
Returns:
max = dialog.max_height
The max_height= method is used to set the maximum height that the user is allowed to resize the dialog to.
Arguments:
Returns:
dialog.max_height = 400
The max_width method is used to get the maximum width that the user is allowed to resize the dialog to.
Returns:
max = dialog.max_width
The max_width= method is used to set the maximum width that the user is allowed to resize the dialog to.
Arguments:
Returns:
dialog.max_width = 500
The min_width method is used to get the minimum height that the user is allowed to resize the dialog to.
Returns:
min = dialog.min_height
The min_height= method is used to set the minimum height that the user is allowed to resize the dialog to.
Arguments:
Returns:
dialog.min_height = 100
The min_width method is used to get the minimum width that the user is allowed to resize the dialog to.
Returns:
min = dialog.min_width
The min_width= method is used to set the minimum width that the user is allowed to resize the dialog to.
Arguments:
Returns:
dialog.min_width = 200
The navigation_buttons_enabled= method is used to set whether the home, next, and back buttons are visible at the top of the WebDialog on the mac. This method has no use on the PC, as these buttons are never displayed.
Arguments:
Returns:
dialog.navigation_buttons_enabled = false
The navigation_buttons_enabled? method is used to get whether the home, next,
and back buttons are visible at the top of the WebDialog on the mac. This
method has no use on the PC, as these buttons are never displayed.
On the mac, this defaults to true for new WebDialogs.
Returns:
nav_buttons = dialog.navigation_buttons_enabled?
The new method is used to create a new webdialog.
Default width is 250. Default height is 250. Default left is 0. Default top
is 0.
Note that the browser which is embedded inside the dialog depends on the
user's OS. On Mac, Safari is embedded, while on the PC whatever version of
Internet Explorer is installed will be embedded.
Arguments:
Returns:
dlg = UI::WebDialog.new("Show Google.com", true,
"ShowGoogleDotCom", 739, 641, 150, 150, true);
dlg.set_url "http://www.google.com"
dlg.showThe post_url method is used to send the data to a url using the HTTP POST method.
Arguments:
Returns:
data = dialog.post_url("http://www.mydomain.com/formchecker.cgi",data)The set_background_color method is used to set the background color for the webdialog.
Arguments:
Returns:
dlg.set_background_color("f3f0f0")The set_file method is used to identify a local HTML file to display in the webdialog.
Arguments:
Returns:
dialog.set_file "c:\\mypage.html"
The set_full_security= method is used to place the WebDialog into a higher security mode where remote URLs and plugins (such as Flash) are not allowed inside the browser. This defaults to false when a new WebDialog is created.
Arguments:
Returns:
nav_buttons = dialog.navigation_buttons_enabled?
The set_html method is used to load a webdialog with a string of provided HTML.
Arguments:
Returns:
html= 'Hello world!' dialog.set_html(html)
The set_on_close method is used to establish one or more activities to perform when the dialog closes (such as saving values stored in the dialog).
Arguments:
Returns:
dialog.set_on_close{ UI.messagebox("Closing the webDialog") }The set_position method is used to set the position of the webdialog relative to the screen, in pixels.
Arguments:
Returns:
dialog.set_position(100,50)
The set_size method is used to set the size of the webdialog, in pixels.
Arguments:
Returns:
dialog.set_size(320,240)
The set_url method is used to load a webdialog with the content at a specific URL. This method allows you to load web sites in a webdialog.
Arguments:
Returns:
dialog.set_url "http://www.google.com"
The show method is used to display a non-modal dialog box.
Arguments:
Returns:
dialog.show {
dialog.execute_script("alert(10)");
}The show_modal method is used to display a modal dialog box. In SketchUp 6 and 7, this behaves differently on Mac vs. PC. On the PC, it shows a truly modal dialog, meaning so long as the WebDialog is visible, no input can be performed elsewhere inside SketchUp. On the Mac, "modal" WebDialogs do not behave this way, but instead are "always on top" of other windows.
Arguments:
Returns:
dialog.show_modal {
dialog.execute_script("alert(10)");
}The visible? method is used to tell if the webdialog is currently shown.
Returns:
vis = dialog.visible?
The write_image method is used to grab a portion of the screen and save the image to the given file path.
Arguments:
dialog.write_image 'c:\\grab.jpg', 0, 0, 100, 100