ask_for_element(parent = nil, default_type = nil, value_text = @content)
public
Ask for an element to be appended parent.
Show source
def ask_for_element(parent = nil, default_type = nil, value_text = @content)
type_input = value_input = nil
dialog = Dialog.new(
"New element into #{parent ? parent.type : 'root'}",
nil, nil,
[ Stock::OK, Dialog::RESPONSE_ACCEPT ],
[ Stock::CANCEL, Dialog::RESPONSE_REJECT ]
)
hbox = HBox.new(false, 5)
hbox.pack_start(Label.new("Type:"), false)
hbox.pack_start(type_input = ComboBox.new(true))
default_active = 0
types = parent ? ALL_TYPES : CONTAINER_TYPES
types.each_with_index do |t, i|
type_input.append_text(t)
if t == default_type
default_active = i
end
end
type_input.active = default_active
dialog.vbox.pack_start(hbox, false)
type_input.signal_connect(:changed) do
configure_value(value_input, types[type_input.active])
end
hbox = HBox.new(false, 5)
hbox.pack_start(Label.new("Value:"), false)
hbox.pack_start(value_input = Entry.new)
value_input.width_chars = 60
value_input.text = value_text if value_text
configure_value(value_input, types[type_input.active])
dialog.vbox.pack_start(hbox, false)
dialog.signal_connect(:'key-press-event', &DEFAULT_DIALOG_KEY_PRESS_HANDLER)
dialog.show_all
self.focus = dialog
dialog.run do |response|
if response == Dialog::RESPONSE_ACCEPT
type = types[type_input.active]
@content = case type
when 'Numeric'
if (t = value_input.text) == 'Infinity'
1 / 0.0
else
Integer(t) rescue Float(t) rescue 0
end
else
value_input.text
end.to_s
return type, @content
end
end
return
ensure
dialog.destroy if dialog
end