Class: TT::GUI::Inputbox

Inherits:
ToolWindow show all
Defined in:
TT_Lib2/inputbox.rb

Overview

(i) Alpha stage. Very likely to be subject to change!

Examples:

i = TT::GUI::Inputbox.new
i.add_control( {
  :label => 'FooBar',
  :value => 'Hello'
} )
i.prompt { |results|
  p results
}

Since:

  • 2.4.0

Constant Summary

CT_LIST =

Since:

  • 2.4.0

1
CT_RADIOBOX =

Since:

  • 2.4.0

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

#parent, #theme, #window

Instance Method Summary collapse

Methods inherited from ToolWindow

#show_window

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

TODO:

Escape HTML data (?) Ok on Windows IE.

TODO:

Descriptions (Info, HTML)

TODO:

Slider

TODO:

PickList

TODO:

Events

Creates a new Inputbox instance.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :title (String) — default: "Inputbox"
  • :pref_key (String)

    If present the inputbox will remember it's values and properties between sessions.

  • :save_values (Boolean) — default: true

    Set to false if you do not want the values to be remembered between sessions, but want the window size etc to be remembered. Requires :pref_key to be true

  • :resizable (Boolean) — default: true
  • :width (Integer)
  • :height (Integer)
  • :accept_label (String) — default: "Ok"

    The caption of the accept button.

  • :cancel_label (String) — default: "Cancel"

    The caption of the cancel button.

  • :modal (Boolean) — default: false

    Set to true to prevent the user from interacting with the model while the window is open. It also prevents other modal windows (From TT_Lib) from opening.

  • :true_modal (Boolean) — default: false

    Deprecated Under Windows the window can be made into a true modal window, but since it can't under OSX this key is deprecated.

Raises:

  • (ArgumentError)

Since:

  • 2.4.0



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(options={})
  raise ArgumentError unless options.is_a?( Hash )
  
  # Window options (Sent to Window < Webdialog instance)
  wnd_options = {
    :dialog_title => 'Inputbox',
    :scrollable   => false,
    :resizable    => true
  }
  wnd_options[:dialog_title]    = options[:title]     if options.key?(:title)
  wnd_options[:preferences_key] = options[:pref_key]  if options.key?(:pref_key)
  wnd_options[:resizable]       = options[:resizable] if options.key?(:resizable)
  wnd_options[:width]           = options[:width]     if options.key?(:width)
  wnd_options[:height]          = options[:height]    if options.key?(:height)
  wnd_options[:left]            = options[:left]      if options.key?(:left)
  wnd_options[:top]             = options[:top]       if options.key?(:top)
  wnd_options[:min_width] = 200
  wnd_options[:min_height] = 12
  # Window UI options (Sent to Javascript)
  @html_options = {
    :accept_label => 'Ok',
    :cancel_label => 'Cancel'
  }
  @html_options[:save_values]  = options[:save_values] if options.key?(:save_values)
  @html_options.merge!(options)
  # Internal flags.
  @save_values  = ( options.key?(:save_values) )  ? options[:save_values] : true
  @true_modal   = ( options.key?(:true_modal) )   ? options[:true_modal]  : false
  @modal        = ( options.key?(:modal) )        ? options[: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 options.key?(:pref_key) && @save_values
    @defaults = TT::Settings.new( options[:pref_key] )
  else
    @defaults = nil
  end
  
  # Initialize the parent Window class
  super( wnd_options )
  # 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.

Examples:

Normal Textbox

i = TT::GUI::Inputbox.new( options )
i.add_control( {
  :label => 'Hello',
  :value => 'World'
} )

Natrually Ordered Multi Select List

i = TT::GUI::Inputbox.new( options )
i.add_control( {
  :label         => 'My List',
  :description   => 'Lorem Ipsum Dolor Sit Amet.',
  :value         => ['Hello', 'World'],
  :options       => ['Foo', 'Hello', 'Bar', 'World', 'FooBar'],
  :multiple      => true,
  :order         => 1,
  :natrual_order => true,
  :size          => 5
} )

Parameters:

  • options (Hash)

Options Hash (options):

  • :label (String)

    Required Should be unique in order for persistant values to function.

  • :value (String|Array)

    Required Default value.

  • :key (Symbol)

    Identifying key used in the result hash when the dialog closes. This was introdused in 2.5.0. Before that the results where Arrays.

  • :description (String)

    Explainatory text accosiated to the control.

  • :options (Array)

    Array of valued, used for lists and radiobox options.

  • :order (Integer)

    -1, 0 or 1 - -1 means decending order, 0 no order, 1 accending order.

  • :natrual_order (Boolean)

    If the list is ordered, set this true for a natrual ordering.

  • :size (Integer)

    Set this property for a scrollable list instead of a drop down list.

  • :multiple (Boolean)

    Allows mulitple items to be selected.

  • :type (Integer)

    Possible constants:

    • TT::GUI::Inputbox::CT_LIST

    • TT::GUI::Inputbox::CT_RADIOBOX

  • :no_save (Boolean)

    Prevents the value from being saved if set to true. Added version 2.5.0.

Raises:

  • (ArgumentError)

Since:

  • 2.4.0



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(options)
  options = ( options.is_a?( Hash ) ) ? TT::JSON.new( options ) : options.dup
  raise ArgumentError unless options.is_a?( TT::JSON )
  # Process and prepare the options before sending it to the Webdialog.
  options[: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 options.key?( :key )
    # Ensure key is unique.
    key = options[:key]
    @ib_controls.each { |control|
      raise ArgumentError, 'options[:key] must be uniqe' if control[:key] == key
    }
  else
    options[:key] = options[:id] 
  end
  # Default value.
  if @defaults && !options[:no_save]
    options[:value] = @defaults[ options[:label], options[:value] ]
  end
  @ib_controls << options
  # If controls are added while the inputbox is open it needs to be notified.
  if self.visible?
    self.execute_script("inputbox.add_control(#{options});")
  end
end

#closing?Boolean

Flag indicating if the webdialog is closing.

Returns:

  • (Boolean)

Since:

  • 2.4.0



195
196
197
# File 'TT_Lib2/inputbox.rb', line 195

def closing?
  @closing
end

#controlsObject

attr_accessor( :controls )

Since:

  • 2.5.5



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.

Since:

  • 2.4.0



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.

Since:

  • 2.4.0



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_closeObject

Flag the window as closing to avoid the Modal_Wrapper from also calling close - something which will lead to multiple triggering of this event.

Since:

  • 2.4.0



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.message
    puts e.backtrace.join("\n")
    UI.messagebox("#{e.message}\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.

Since:

  • 2.4.0



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.

Parameters:

  • block (&block)

    the callback that receives the resulting values as a

Returns:

  • (Hash|nil)

    Hash of resulting values if Inputbox is true modal, nil otherwise.

Since:

  • 2.4.0



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