Class: TT::GUI::Inputbox
- Inherits:
-
ToolWindow
- Object
- UI::WebDialog
- WebDialogPatch
- Window
- ToolWindow
- TT::GUI::Inputbox
- Defined in:
- TT_Lib2/inputbox.rb
Overview
(i) Alpha stage. Very likely to be subject to change!
Constant Summary
- CT_LIST =
1
- CT_RADIOBOX =
2
Constants inherited from Window
Window::EVENT_CALLBACK, Window::EVENT_OPEN_URL, Window::EVENT_WINDOW_READY, Window::THEME_DEFAULT, Window::THEME_GRAPHITE
Instance Attribute Summary
Attributes inherited from Window
Instance Method Summary collapse
-
#add_control(options) ⇒ Object
Adds a new input control.
-
#closing? ⇒ Boolean
Flag indicating if the webdialog is closing.
-
#controls ⇒ Object
attr_accessor( :controls ).
-
#event_inputbox_accept(window, params) ⇒ Object
When the user accepts the values in the Inputbox, collect the data and convert them back from string into their input types.
-
#event_inputbox_cancel(window, params) ⇒ Object
User cancels the inputbox, either by using the Cancel button, the Close button or the Right-Click menu.
-
#event_inputbox_close ⇒ Object
Flag the window as closing to avoid the Modal_Wrapper from also calling close - something which will lead to multiple triggering of this event.
-
#event_inputbox_ready(window, params) ⇒ Object
When the webdialog reports it's ready, call function to start building the UI with the given user options.
-
#initialize(options = {}) ⇒ Inputbox
constructor
Creates a new Inputbox instance.
-
#prompt(&block) ⇒ Hash|nil
Note As of 2.5.0 the returned argument of the block is a Hash instead of an Array.
Methods inherited from ToolWindow
Methods inherited from Window
#add_script, #add_style, #call_script, #get_checkbox_state, #get_checked_state, #get_client_size, #get_control_value, #get_html, #get_text, #inspect, #local_path, #on_ready, #set_client_size, #show_window, #title
Methods included from ContainerElement
#get_control_by_name, #get_control_by_ui_id, #release!, #remove_control
Methods inherited from WebDialogPatch
#set_file, #set_html, #set_url
Constructor Details
#initialize(options = {}) ⇒ Inputbox
Escape HTML data (?) Ok on Windows IE.
Descriptions (Info, HTML)
Slider
PickList
Events
Creates a new Inputbox instance.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'TT_Lib2/inputbox.rb', line 68 def initialize(={}) raise ArgumentError unless .is_a?( Hash ) # Window options (Sent to Window < Webdialog instance) = { :dialog_title => 'Inputbox', :scrollable => false, :resizable => true } [:dialog_title] = [:title] if .key?(:title) [:preferences_key] = [:pref_key] if .key?(:pref_key) [:resizable] = [:resizable] if .key?(:resizable) [:width] = [:width] if .key?(:width) [:height] = [:height] if .key?(:height) [:left] = [:left] if .key?(:left) [:top] = [:top] if .key?(:top) [:min_width] = 200 [:min_height] = 12 # Window UI options (Sent to Javascript) @html_options = { :accept_label => 'Ok', :cancel_label => 'Cancel' } @html_options[:save_values] = [:save_values] if .key?(:save_values) @html_options.merge!() # Internal flags. @save_values = ( .key?(:save_values) ) ? [:save_values] : true @true_modal = ( .key?(:true_modal) ) ? [:true_modal] : false @modal = ( .key?(:modal) ) ? [:modal] : false # Array of the controls and their values. @ib_controls = [] @values = nil # Flag indicating if the window is closing. @closing = false # Initate the ModalWrapper if !@true_modal && @modal @modal_window = TT::GUI::ModalWrapper.new( self ) end # Control's label is the section key for settings if .key?(:pref_key) && @save_values @defaults = TT::Settings.new( [:pref_key] ) else @defaults = nil end # Initialize the parent Window class super( ) # Using relative paths, relying on the BASE element seem to fail on some # computers running various versions of Windows. Not sure why - maybe # different security settings..? wpath = File.join( TT::Lib.path, 'webdialog' ) add_script( local_path( File.join(wpath, 'js', 'inputbox.js') ) ) add_style( local_path( File.join(wpath, 'css', 'inputbox.css') ) ) add_action_callback( 'Inputbox_ready', &method(:event_inputbox_ready) ) add_action_callback( 'Inputbox_accept', &method(:event_inputbox_accept) ) add_action_callback( 'Inputbox_cancel', &method(:event_inputbox_cancel) ) set_on_close( &method(:event_inputbox_close) ) end |
Instance Method Details
#add_control(options) ⇒ Object
Adds a new input control.
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'TT_Lib2/inputbox.rb', line 247 def add_control() = ( .is_a?( Hash ) ) ? TT::JSON.new( ) : .dup raise ArgumentError unless .is_a?( TT::JSON ) # Process and prepare the options before sending it to the Webdialog. [:id] = "Inputbox_control#{@ib_controls.size}" # Ensure the options are JSON object which can be sent to the Webdialog. # Hashes will aumatically be converted to JSON objects. if .key?( :key ) # Ensure key is unique. key = [:key] @ib_controls.each { |control| raise ArgumentError, 'options[:key] must be uniqe' if control[:key] == key } else [:key] = [:id] end # Default value. if @defaults && ![:no_save] [:value] = @defaults[ [:label], [:value] ] end @ib_controls << # If controls are added while the inputbox is open it needs to be notified. if self.visible? self.execute_script("inputbox.add_control(#{});") end end |
#closing? ⇒ Boolean
Flag indicating if the webdialog is closing.
195 196 197 |
# File 'TT_Lib2/inputbox.rb', line 195 def closing? @closing end |
#controls ⇒ Object
attr_accessor( :controls )
36 |
# File 'TT_Lib2/inputbox.rb', line 36 def controls; @ib_controls; end |
#event_inputbox_accept(window, params) ⇒ Object
When the user accepts the values in the Inputbox, collect the data and convert them back from string into their input types. (!) Build Hash with keys and values.
153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'TT_Lib2/inputbox.rb', line 153 def event_inputbox_accept( window, params ) TT.debug '>> Input Accept' @values = {} @ib_controls.each { |control| value = window.send( :get_value, control[:id] ) type = ( control[:options] && control[:multiple] ) ? Array : control[:value] value = TT::Locale.cast_string(value, type, '||') @defaults[ control[:label] ] = value if @save_values @values[ control[:key] ] = value control[:value] = value } window.close end |
#event_inputbox_cancel(window, params) ⇒ Object
User cancels the inputbox, either by using the Cancel button, the Close button or the Right-Click menu.
170 171 172 173 |
# File 'TT_Lib2/inputbox.rb', line 170 def event_inputbox_cancel( window, params ) TT.debug '>> Input Cancel' window.close end |
#event_inputbox_close ⇒ Object
Flag the window as closing to avoid the Modal_Wrapper from also calling close - something which will lead to multiple triggering of this event.
178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'TT_Lib2/inputbox.rb', line 178 def event_inputbox_close TT.debug '>> Window Closed' @closing = true @modal_window.close if @modal_window begin @block.call(@values) # (?) Delay with timer to ensure window closes? rescue => e puts e. puts e.backtrace.join("\n") UI.("#{e.}\n\n#{e.backtrace.join("\n")}", MB_MULTILINE) end end |
#event_inputbox_ready(window, params) ⇒ Object
When the webdialog reports it's ready, call function to start building the UI with the given user options.
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'TT_Lib2/inputbox.rb', line 132 def event_inputbox_ready( window, params ) TT.debug '>> Input Ready' # Window settings o = TT::JSON.new( @html_options ) window.execute_script("inputbox.init_html(#{o});") # Add controls @ib_controls.each { |control| c = control.clone if c[:value].is_a?( Float ) && !c[:value].is_a?( Length ) c[:value] = TT::Locale.float_to_string( c[:value] ) elsif c[:value].is_a?( Length ) c[:value] = c[:value].to_s end window.execute_script("inputbox.add_control(#{c});") } end |
#prompt(&block) ⇒ Hash|nil
Note As of 2.5.0 the returned argument of the block is a Hash instead of an Array.
Hash
when the inputbox closes. Only used if the inputbox is
not true modal.
283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'TT_Lib2/inputbox.rb', line 283 def prompt(&block) @values = nil @block = block if @true_modal show_window(@true_modal) @values elsif @modal @modal_window.show nil else self.show_window nil end end |