Class: Glimmer::Tk::WidgetProxy
- Inherits:
-
Object
- Object
- Glimmer::Tk::WidgetProxy
- Defined in:
- lib/glimmer/tk/widget_proxy.rb,
lib/glimmer/tk/drag_and_drop_extension.rb
Overview
Proxy for Tk Widget objects
Follows the Proxy Design Pattern
Direct Known Subclasses
ButtonProxy, CheckbuttonProxy, ComboboxProxy, EntryProxy, FrameProxy, LabelProxy, ListProxy, NotebookProxy, RadiobuttonProxy, SpinboxProxy, TextProxy, ToplevelProxy, TreeviewProxy
Defined Under Namespace
Classes: DragAndDropEvent
Constant Summary collapse
- FONTS_PREDEFINED =
%w[default text fixed menu heading caption small_caption icon tooltip]
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#keyword ⇒ Object
readonly
Returns the value of attribute keyword.
-
#on_drag_motion_block ⇒ Object
Returns the value of attribute on_drag_motion_block.
-
#parent_proxy ⇒ Object
readonly
Returns the value of attribute parent_proxy.
-
#tk ⇒ Object
readonly
Returns the value of attribute tk.
Class Method Summary collapse
- .create(keyword, parent, args, &block) ⇒ Object
-
.tk_widget_class_for(underscored_widget_name) ⇒ Object
This supports widgets in and out of basic Tk.
- .widget_exists?(underscored_widget_name) ⇒ Boolean
- .widget_proxy_class(keyword) ⇒ Object
Instance Method Summary collapse
- #add_observer(observer, attribute) ⇒ Object
-
#ancestor_proxies ⇒ Object
returns list of ancestors ordered from direct parent to root parent.
- #apply_style(options) ⇒ Object
- #attribute_setter(attribute) ⇒ Object
- #content(&block) ⇒ Object
- #destroy ⇒ Object
- #drag_source=(value) ⇒ Object
- #font=(value) ⇒ Object
- #get_attribute(attribute) ⇒ Object
- #grid(options = {}) ⇒ Object
- #handle_listener(listener_name, &listener) ⇒ Object
- #has_attribute?(attribute, *args) ⇒ Boolean
- #has_attributes_attribute?(attribute) ⇒ Boolean
- #has_state?(attribute) ⇒ Boolean
- #image_argument(args) ⇒ Object
-
#initialize(underscored_widget_name, parent_proxy, args, &block) ⇒ WidgetProxy
constructor
Initializes a new Tk Widget.
- #make_draggable ⇒ Object
- #make_non_draggable ⇒ Object
- #method_missing(method, *args, &block) ⇒ Object
- #on(listener_name, &listener) ⇒ Object
- #on_drag_start_block=(block) ⇒ Object
- #on_drop_block=(value) ⇒ Object
-
#post_add_content ⇒ Object
Subclasses may override to perform post add_content work.
-
#post_initialize_child(child) ⇒ Object
Subclasses may override to perform post initialization work on an added child.
- #respond_to?(method, *args, super_only: false, &block) ⇒ Boolean
- #root_parent_proxy ⇒ Object
- #set_attribute(attribute, *args) ⇒ Object
- #style=(styles) ⇒ Object
- #textvariable_defined? ⇒ Boolean
- #tk_widget_has_attribute_getter_setter?(attribute) ⇒ Boolean
- #tk_widget_has_attribute_setter?(attribute) ⇒ Boolean
- #toplevel_parent_proxy ⇒ Object
- #widget_attribute_listener_installers ⇒ Object
- #widget_custom_attribute_mapping ⇒ Object
Constructor Details
#initialize(underscored_widget_name, parent_proxy, args, &block) ⇒ WidgetProxy
Initializes a new Tk Widget
Styles is a comma separate list of symbols representing Tk styles in lower case
75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 75 def initialize(, parent_proxy, args, &block) @parent_proxy = parent_proxy @args = args @keyword = @block = block = self.class.() @tk = .new(@parent_proxy.tk, *args) # a common widget initializer @parent_proxy.post_initialize_child(self) initialize_defaults post_add_content if @block.nil? end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
483 484 485 486 487 488 489 490 491 492 493 494 495 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 483 def method_missing(method, *args, &block) method = method.to_s if args.empty? && block.nil? && (([tk.class] && [tk.class][method]) || has_state?(method) || has_attributes_attribute?(method)) get_attribute(method) elsif method.end_with?('=') && block.nil? && (([tk.class] && [tk.class][method.sub(/=$/, '')]) || has_state?(method) || has_attributes_attribute?(method)) set_attribute(method.sub(/=$/, ''), *args) else tk.send(method, *args, &block) end rescue => e Glimmer::Config.logger.debug {"Neither WidgetProxy nor #{tk.class.name} can handle the method ##{method}"} super(method.to_sym, *args, &block) end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
70 71 72 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 70 def args @args end |
#children ⇒ Object (readonly)
Returns the value of attribute children.
70 71 72 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 70 def children @children end |
#keyword ⇒ Object (readonly)
Returns the value of attribute keyword.
70 71 72 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 70 def keyword @keyword end |
#on_drag_motion_block ⇒ Object
Returns the value of attribute on_drag_motion_block.
6 7 8 |
# File 'lib/glimmer/tk/drag_and_drop_extension.rb', line 6 def on_drag_motion_block @on_drag_motion_block end |
#parent_proxy ⇒ Object (readonly)
Returns the value of attribute parent_proxy.
70 71 72 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 70 def parent_proxy @parent_proxy end |
#tk ⇒ Object (readonly)
Returns the value of attribute tk.
70 71 72 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 70 def tk @tk end |
Class Method Details
.create(keyword, parent, args, &block) ⇒ Object
31 32 33 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 31 def create(keyword, parent, args, &block) (keyword).new(keyword, parent, args, &block) end |
.tk_widget_class_for(underscored_widget_name) ⇒ Object
This supports widgets in and out of basic Tk
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 47 def () = .camelcase(:upper) = [ "::Tk::Tile::#{}", "::Tk::#{}", "::Tk#{}", "::Glimmer::Tk::#{}Proxy", ] = nil .each do || begin = eval() break rescue RuntimeError, SyntaxError, NameError => e Glimmer::Config.logger.debug {e.} end end if .respond_to?(:new) end |
.widget_exists?(underscored_widget_name) ⇒ Boolean
123 124 125 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 123 def self.() !!() end |
.widget_proxy_class(keyword) ⇒ Object
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 35 def (keyword) begin class_name = "#{keyword.camelcase(:upper)}Proxy".to_sym Glimmer::Tk.const_get(class_name) rescue => e Glimmer::Config.logger.debug {"Unable to instantiate custom class name for #{keyword} ... defaulting to Glimmer::Tk::WidgetProxy"} Glimmer::Config.logger.debug {e.} Glimmer::Tk::WidgetProxy end end |
Instance Method Details
#add_observer(observer, attribute) ⇒ Object
446 447 448 449 450 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 446 def add_observer(observer, attribute) attribute_listener_installers = @tk.class.ancestors.map {|ancestor| [ancestor]}.compact = attribute_listener_installers.map{|installer| installer[attribute.to_s]}.compact if !attribute_listener_installers.empty? .to_a.first&.call(observer) end |
#ancestor_proxies ⇒ Object
returns list of ancestors ordered from direct parent to root parent
99 100 101 102 103 104 105 106 107 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 99 def ancestor_proxies ancestors = [] current = self while current.parent_proxy ancestors << current.parent_proxy current = current.parent_proxy end ancestors end |
#apply_style(options) ⇒ Object
292 293 294 295 296 297 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 292 def apply_style() @@style_number = 0 unless defined?(@@style_number) style = "style#{@@style_number += 1}.#{@tk.class.name.split('::').last}" ::Tk::Tile::Style.configure(style, ) @tk.style = style end |
#attribute_setter(attribute) ⇒ Object
241 242 243 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 241 def attribute_setter(attribute) "#{attribute}=" end |
#content(&block) ⇒ Object
479 480 481 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 479 def content(&block) Glimmer::DSL::Engine.add_content(self, Glimmer::DSL::Tk::WidgetExpression.new, keyword, *args, &block) end |
#destroy ⇒ Object
287 288 289 290 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 287 def destroy @tk.destroy @on_destroy_procs&.each {|p| p.call(@tk)} end |
#drag_source=(value) ⇒ Object
8 9 10 11 12 13 14 15 |
# File 'lib/glimmer/tk/drag_and_drop_extension.rb', line 8 def drag_source=(value) @drag_source = value if @drag_source make_draggable else make_non_draggable end end |
#font=(value) ⇒ Object
275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 275 def font=(value) if (value.is_a?(Symbol) || value.is_a?(String)) && FONTS_PREDEFINED.include?(value.to_s.downcase) @tk.font = "tk_#{value}_font".camelcase(:upper) else @tk.font = value.is_a?(TkFont) ? value : TkFont.new(value) end rescue => e Glimmer::Config.logger.debug {"Failed to set attribute #{attribute} with args #{args.inspect}. Attempting to set through style instead..."} Glimmer::Config.logger.debug {e.} apply_style({"font" => value}) end |
#get_attribute(attribute) ⇒ Object
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 222 def get_attribute(attribute) = [tk.class] && [tk.class][attribute.to_s] if respond_to?(attribute, super_only: true) send(attribute) elsif [:getter][:invoker].call(@tk, args) elsif (attribute) @tk.send(attribute) elsif has_state?(attribute) @tk.tile_instate(attribute.sub(/\?$/, '')) elsif has_attributes_attribute?(attribute) result = @tk.attributes[attribute.sub(/\?$/, '')] result = result == 1 if result.is_a?(Integer) result else send(attribute) end end |
#grid(options = {}) ⇒ Object
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 251 def grid( = {}) = .stringify_keys index_in_parent = @parent_proxy&.children&.index(self) ['rowspan'] = .delete('row_span') if .keys.include?('row_span') ['columnspan'] = .delete('column_span') if .keys.include?('column_span') ['rowweight'] = .delete('row_weight') if .keys.include?('row_weight') ['columnweight'] = .delete('column_weight') if .keys.include?('column_weight') ['columnweight'] = ['rowweight'] = .delete('weight') if .keys.include?('weight') ['rowminsize'] = .delete('row_minsize') if .keys.include?('row_minsize') ['rowminsize'] = .delete('minheight') if .keys.include?('minheight') ['rowminsize'] = .delete('min_height') if .keys.include?('min_height') ['columnminsize'] = .delete('column_minsize') if .keys.include?('column_minsize') ['columnminsize'] = .delete('minwidth') if .keys.include?('minwidth') ['columnminsize'] = .delete('min_width') if .keys.include?('min_width') ['columnminsize'] = ['rowminsize'] = .delete('minsize') if .keys.include?('minsize') if index_in_parent TkGrid.rowconfigure(@parent_proxy.tk, index_in_parent, 'weight'=> .delete('rowweight')) if .keys.include?('rowweight') TkGrid.rowconfigure(@parent_proxy.tk, index_in_parent, 'minsize'=> .delete('rowminsize')) if .keys.include?('rowminsize') TkGrid.columnconfigure(@parent_proxy.tk, index_in_parent, 'weight'=> .delete('columnweight')) if .keys.include?('columnweight') TkGrid.columnconfigure(@parent_proxy.tk, index_in_parent, 'minsize'=> .delete('columnminsize')) if .keys.include?('columnminsize') end @tk.grid() end |
#handle_listener(listener_name, &listener) ⇒ Object
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 452 def handle_listener(listener_name, &listener) listener_name = listener_name.to_s if listener_name == 'destroy' # 'destroy' is a more reliable alternative listener binding to '<Destroy>' @on_destroy_procs ||= [] listener.singleton_class.include(Glimmer::DataBinding::Tk::OneTimeObserver) unless listener.is_a?(Glimmer::DataBinding::Tk::OneTimeObserver) @on_destroy_procs << listener @tk.bind('<Destroy>', listener) parent_proxy.handle_listener(listener_name, &listener) if parent_proxy # TODO return a listener registration object that has a deregister method else begin @tk.bind(listener_name, &listener) rescue => e Glimmer::Config.logger.debug {"Unable to bind to #{listener_name} .. attempting to surround with <>"} Glimmer::Config.logger.debug {e.} listener_name = "<#{listener_name}" if !listener_name.start_with?('<') listener_name = "#{listener_name}>" if !listener_name.end_with?('>') @tk.bind(listener_name, &listener) end end end |
#has_attribute?(attribute, *args) ⇒ Boolean
172 173 174 175 176 177 178 179 180 181 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 172 def has_attribute?(attribute, *args) ([tk.class] and [tk.class][attribute.to_s]) or (attribute) or (attribute) or has_state?(attribute) or has_attributes_attribute?(attribute) or respond_to?(attribute_setter(attribute), args) or respond_to?(attribute_setter(attribute), *args, super_only: true) or respond_to?(attribute, *args, super_only: true) end |
#has_attributes_attribute?(attribute) ⇒ Boolean
167 168 169 170 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 167 def has_attributes_attribute?(attribute) attribute = attribute.sub(/\?$/, '').sub(/=$/, '') @tk.respond_to?(:attributes) && @tk.attributes.keys.include?(attribute.to_s) end |
#has_state?(attribute) ⇒ Boolean
152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 152 def has_state?(attribute) attribute = attribute.sub(/\?$/, '').sub(/=$/, '') if @tk.respond_to?(:tile_state) begin @tk.tile_instate(attribute) true rescue => e Glimmer::Config.logger.debug { "No tk state for #{attribute}" } false end else false end end |
#image_argument(args) ⇒ Object
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 430 def image_argument(args) if args.first.is_a?(::TkPhotoImage) args.first else image_args = {} image_args.merge!(file: args.first.to_s) if args.first.is_a?(String) the_image = ::TkPhotoImage.new(image_args) if args.last.is_a?(Hash) processed_image = ::TkPhotoImage.new processed_image.copy(the_image, args.last) the_image = processed_image end the_image end end |
#make_draggable ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/glimmer/tk/drag_and_drop_extension.rb', line 38 def make_draggable drag_event = nil bind("<DropAcceptedEvent>", proc { |event| drag_event.drop_accepted = true }) bind("B1-Motion", proc { |tk_event| if drag_event.nil? tooltip = TkToplevel.new(root).overrideredirect(1) #create tooltip window to display dragged data tooltip.geometry("+#{tk_event.x_root + 10}+#{tk_event.y_root - 2}") drag_event = DragAndDropEvent.new(self.tk, nil, tooltip, tk_event.x_root, tk_event.y_root, nil, false) if @drag_source tk_event..configure(:cursor => "hand2") # Default data to drag is text drag_event.data = if textvariable_defined? then tk.textvariable.value elsif has_attribute?(:text) then tk.text end TkLabel.new(tooltip) { text drag_event.data }.pack elsif !@on_drag_start_block.nil? @on_drag_start_block.call(drag_event) TkLabel.new(tooltip) { text drag_event.data }.pack if tooltip.winfo_children().length == 0 end else drag_event.x_root, drag_event.y_root = tk_event.x_root, tk_event.y_root drag_event.drop_accepted = false = tk_event..winfo_containing(tk_event.x_root, tk_event.y_root) drag_event.target = .event_generate("<DropCheckEvent>", :data => drag_event.to_json) if @on_drag_motion_block.nil? # Default motion behavior: # 1.Change cursor to show whether text can be dropped. # 2.Move tooltip with dragged data. if drag_event.drop_accepted tk_event..configure(:cursor => "hand1") else tk_event..configure(:cursor => "hand2") end drag_event.tooltip.geometry("+#{tk_event.x_root + 10}+#{tk_event.y_root - 2}") else @on_drag_motion_block.call(drag_event) end end }) bind("ButtonRelease-1", proc { |tk_event| if drag_event drag_event.target = tk_event..winfo_containing(tk_event.x_root, tk_event.y_root) drag_event.source.configure(:cursor => "") drag_event.target.event_generate("<DropEvent>", :data => drag_event.to_json) drag_event.tooltip.destroy drag_event = nil end }) end |
#make_non_draggable ⇒ Object
87 88 89 90 91 |
# File 'lib/glimmer/tk/drag_and_drop_extension.rb', line 87 def make_non_draggable @tk.bind_remove("B1-Motion") @tk.bind_remove("ButtonRelease-1") @tk.bind_remove("<DropAcceptedEvent>") end |
#on(listener_name, &listener) ⇒ Object
475 476 477 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 475 def on(listener_name, &listener) handle_listener(listener_name, &listener) end |
#on_drag_start_block=(block) ⇒ Object
17 18 19 20 |
# File 'lib/glimmer/tk/drag_and_drop_extension.rb', line 17 def on_drag_start_block=(block) @on_drag_start_block = block make_draggable end |
#on_drop_block=(value) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/glimmer/tk/drag_and_drop_extension.rb', line 22 def on_drop_block=(value) @on_drop_block = value self.tk.bind("<DropEvent>", proc { |tk_event| drop_event = DragAndDropEvent.json_create(JSON.parse("{" + tk_event.detail + "}")) @on_drop_block.call(drop_event) if self.tk == drop_event.target }) self.tk.bind("<DropCheckEvent>", proc { |tk_event| drop_check_event = DragAndDropEvent.json_create(JSON.parse("{" + tk_event.detail + "}")) drop_check_event.source.event_generate("<DropAcceptedEvent>") }) end |
#post_add_content ⇒ Object
Subclasses may override to perform post add_content work
119 120 121 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 119 def post_add_content # No Op by default end |
#post_initialize_child(child) ⇒ Object
Subclasses may override to perform post initialization work on an added child
114 115 116 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 114 def post_initialize_child(child) children << child end |
#respond_to?(method, *args, super_only: false, &block) ⇒ Boolean
497 498 499 500 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 497 def respond_to?(method, *args, super_only: false, &block) super(method, true) || !super_only && tk.respond_to?(method, *args, &block) end |
#root_parent_proxy ⇒ Object
88 89 90 91 92 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 88 def root_parent_proxy current = self current = current.parent_proxy while current.parent_proxy current end |
#set_attribute(attribute, *args) ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 183 def set_attribute(attribute, *args) begin = [tk.class] && [tk.class][attribute.to_s] if respond_to?(attribute_setter(attribute), super_only: true) send(attribute_setter(attribute), *args) elsif respond_to?(attribute, super_only: true) && self.class.instance_method(attribute).parameters.size > 0 send(attribute, *args) elsif [:setter][:invoker].call(@tk, args) elsif (attribute) unless args.size == 1 && @tk.send(attribute) == args.first if args.size == 1 @tk.send(attribute_setter(attribute), *args) else @tk.send(attribute_setter(attribute), args) end end elsif (attribute) @tk.send(attribute, *args) elsif has_state?(attribute) attribute = attribute.sub(/=$/, '') if !!args.first @tk.tile_state(attribute) else @tk.tile_state("!#{attribute}") end elsif has_attributes_attribute?(attribute) attribute = attribute.sub(/=$/, '') @tk.attributes(attribute, args.first) else raise "#{self} cannot handle attribute #{attribute} with args #{args.inspect}" end rescue => e Glimmer::Config.logger.debug {"Failed to set attribute #{attribute} with args #{args.inspect}. Attempting to set through style instead..."} Glimmer::Config.logger.debug {e.} apply_style(attribute => args.first) end end |
#style=(styles) ⇒ Object
245 246 247 248 249 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 245 def style=(styles) styles.each do |attribute, value| apply_style(attribute => value) end end |
#textvariable_defined? ⇒ Boolean
34 35 36 |
# File 'lib/glimmer/tk/drag_and_drop_extension.rb', line 34 def textvariable_defined? (:textvariable) && !tk.textvariable.nil? end |
#tk_widget_has_attribute_getter_setter?(attribute) ⇒ Boolean
141 142 143 144 145 146 147 148 149 150 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 141 def (attribute) begin # TK Widget currently doesn't support respond_to? properly, so I have to resort to this trick for now @tk.send(attribute) true rescue => e Glimmer::Config.logger.debug { "No tk attribute getter setter for #{attribute}" } false end end |
#tk_widget_has_attribute_setter?(attribute) ⇒ Boolean
127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 127 def (attribute) result = nil begin # TK Widget currently doesn't support respond_to? properly, so I have to resort to this trick for now @tk.send(attribute_setter(attribute), @tk.send(attribute)) result = true rescue => e Glimmer::Config.logger.debug { "No tk attribute setter for #{attribute}" } Glimmer::Config.logger.debug { e. } result = false end result end |
#toplevel_parent_proxy ⇒ Object
94 95 96 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 94 def toplevel_parent_proxy ancestor_proxies.find {|| .is_a?(ToplevelProxy)} end |
#widget_attribute_listener_installers ⇒ Object
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 369 def # TODO consider extracting to modules/subclasses @tk_widget_attribute_listener_installers ||= { ::Tk::Tile::TCheckbutton => { 'variable' => lambda do |observer| @tk.command { observer.call(@tk.variable.value.to_s == @tk.onvalue.to_s) } end, }, ::Tk::Tile::TCombobox => { 'text' => lambda do |observer| if observer.is_a?(Glimmer::DataBinding::ModelBinding) model = observer.model = observer.property_name + '_options' @tk.values = model.send() if model.respond_to?() end @tk.bind('<ComboboxSelected>') { observer.call(@tk.textvariable.value) } end, }, ::Tk::Tile::TEntry => { 'text' => lambda do |observer| @tk.textvariable.trace('write') { observer.call(@tk.textvariable.value) } end, }, ::Tk::Tile::TSpinbox => { 'text' => lambda do |observer| @tk.command { observer.call(@tk.textvariable&.value) } @tk.validate('key') @tk.validatecommand { |validate_args| observer.call(validate_args.value) new_icursor = validate_args.index new_icursor += validate_args.string.size if validate_args.action == 1 @tk.icursor = new_icursor true } end, }, ::Tk::Text => { 'value' => lambda do |observer| handle_listener('modified') do observer.call(value) end end, }, ::Tk::Tile::TRadiobutton => { 'variable' => lambda do |observer| @tk.command { observer.call(@tk.variable.value == @tk.value) } end, }, } end |
#widget_custom_attribute_mapping ⇒ Object
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 |
# File 'lib/glimmer/tk/widget_proxy.rb', line 299 def # TODO consider extracting to modules/subclasses @widget_custom_attribute_mapping ||= { ::Tk::Tile::TButton => { 'image' => { getter: {name: 'image', invoker: lambda { |, args| @tk.image }}, setter: {name: 'image=', invoker: lambda { |, args| @tk.image = image_argument(args) }}, }, }, ::Tk::Tile::TCheckbutton => { 'image' => { getter: {name: 'image', invoker: lambda { |, args| @tk.image }}, setter: {name: 'image=', invoker: lambda { |, args| @tk.image = image_argument(args) }}, }, 'variable' => { getter: {name: 'variable', invoker: lambda { |, args| @tk.variable&.value.to_s == @tk.onvalue.to_s }}, setter: {name: 'variable=', invoker: lambda { |, args| @tk.variable&.value = args.first.is_a?(Integer) ? args.first : (args.first ? 1 : 0) }}, }, }, ::Tk::Tile::TRadiobutton => { 'image' => { getter: {name: 'image', invoker: lambda { |, args| @tk.image }}, setter: {name: 'image=', invoker: lambda { |, args| @tk.image = image_argument(args) }}, }, 'text' => { getter: {name: 'text', invoker: lambda { |, args| @tk.text }}, setter: {name: 'text=', invoker: lambda { |, args| @tk.value = @tk.text = args.first }}, }, 'variable' => { getter: {name: 'variable', invoker: lambda { |, args| @tk.variable&.value == @tk.value }}, setter: {name: 'variable=', invoker: lambda { |, args| @tk.variable&.value = args.first ? @tk.value : @tk.variable&.value }}, }, }, ::Tk::Tile::TCombobox => { 'text' => { getter: {name: 'text', invoker: lambda { |, args| @tk.textvariable&.value }}, setter: {name: 'text=', invoker: lambda { |, args| @tk.textvariable&.value = args.first }}, }, }, ::Tk::Tile::TLabel => { 'text' => { getter: {name: 'text', invoker: lambda { |, args| @tk.textvariable&.value }}, setter: {name: 'text=', invoker: lambda { |, args| @tk.textvariable&.value = args.first }}, }, 'image' => { getter: {name: 'image', invoker: lambda { |, args| @tk.image }}, setter: {name: 'image=', invoker: lambda { |, args| @tk.image = image_argument(args) }}, }, }, ::Tk::Tile::TEntry => { 'text' => { getter: {name: 'text', invoker: lambda { |, args| @tk.textvariable&.value }}, setter: {name: 'text=', invoker: lambda { |, args| @tk.textvariable&.value = args.first }}, }, }, ::Tk::Tile::TSpinbox => { 'text' => { getter: {name: 'text', invoker: lambda { |, args| @tk.textvariable&.value }}, setter: {name: 'text=', invoker: lambda { |, args| @tk.textvariable&.value = args.first }}, }, }, ::Tk::Root => { 'text' => { getter: {name: 'text', invoker: lambda { |, args| @tk.title }}, setter: {name: 'text=', invoker: lambda { |, args| @tk.title = args.first }}, }, }, } end |