Class: TkWrapper::Widgets::AutoResizeEntry

Inherits:
Entry show all
Defined in:
lib/widgets/auto_resize_entry.rb

Instance Attribute Summary collapse

Attributes inherited from Base::Widget

#cell, #childs, #config, #ids, #parent

Instance Method Summary collapse

Methods inherited from Entry

#tk_class

Methods inherited from Base::Widget

#add_ids, config, #configure, #create_tk_widget, #each, manager, #manager, modify, #modify, #modify_each, #push, #tk_class, #tk_widget

Constructor Details

#initialize(config: {}, childs: [], id: nil) ⇒ AutoResizeEntry

Returns a new instance of AutoResizeEntry.



7
8
9
10
11
# File 'lib/widgets/auto_resize_entry.rb', line 7

def initialize(config: {}, childs: [], id: nil)
  @min_width = config[:min_width] || 0
  @add_width = config[:add_width] || 0
  super(config: config, childs: childs, id: id)
end

Instance Attribute Details

#add_widthObject

auto resizes on user input, only works if in the grid geometry manager of tk



5
6
7
# File 'lib/widgets/auto_resize_entry.rb', line 5

def add_width
  @add_width
end

#min_widthObject

auto resizes on user input, only works if in the grid geometry manager of tk



5
6
7
# File 'lib/widgets/auto_resize_entry.rb', line 5

def min_width
  @min_width
end

Instance Method Details

#build(parent, configure: true) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/widgets/auto_resize_entry.rb', line 13

def build(parent, configure: true)
  super(parent, configure: configure)
  parent.tk_widget.bind('Configure') { resize }
  tk_widget.textvariable = TkVariable.new unless tk_widget.textvariable
  tk_widget.textvariable.trace('write') { resize }
  resize
end

#resizeObject



29
30
31
32
33
34
35
36
# File 'lib/widgets/auto_resize_entry.rb', line 29

def resize
  max_width = @cell.bbox[2]
  text_width = @font.measure(value)
  new_width = [[@min_width, text_width + @add_width].max, max_width].min
  tk_widget.width = 0
  tk_widget.grid(ipadx: new_width / 2.0)
  @parent.tk_widget.update
end

#valueObject



25
26
27
# File 'lib/widgets/auto_resize_entry.rb', line 25

def value
  tk_widget.textvariable.value
end

#value=(value) ⇒ Object



21
22
23
# File 'lib/widgets/auto_resize_entry.rb', line 21

def value=(value)
  tk_widget.textvariable.value = value
end