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

#childs, #config, #parent

Instance Method Summary collapse

Methods inherited from Entry

#tk_class

Methods inherited from Base::Widget

#check_match, config, #configure, #create_tk_widget, #find, #find_all, #ids, #manager, manager, modify, #push, #tk_class, #tk_widget

Constructor Details

#initialize(config: {}, childs: []) ⇒ 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: [])
  @min_width = config[:min_width] || 0
  @add_width = config[:add_width] || 0
  super(config: config, childs: childs)
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

#config_for_dummy_labelObject



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

def config_for_dummy_label
  grid_info = TkGrid.info(tk_widget)
  { config: { grid: {
    row: grid_info['row'],
    column: grid_info['column'],
    columnspan: grid_info['columnspan'],
    sticky: 'nw'
  } } }
end

#create_dummy_label_with_same_size(&block) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/widgets/auto_resize_entry.rb', line 39

def create_dummy_label_with_same_size(&block)
  label = TkWrapper::Widgets::Label.new(**config_for_dummy_label)
  label.build(@parent)
  label.tk_widget.text = value
  label.tk_widget.lower
  result = block.call(label)
  label.tk_widget.destroy
  result
end

#resizeObject



63
64
65
66
67
68
69
70
# File 'lib/widgets/auto_resize_entry.rb', line 63

def resize
  max_width = cell_bbox[2]
  text_width = text_width_in_pixel
  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

#text_width_in_pixelObject



49
50
51
52
53
54
# File 'lib/widgets/auto_resize_entry.rb', line 49

def text_width_in_pixel
  create_dummy_label_with_same_size do |label|
    @parent.tk_widget.update
    label.tk_widget.winfo_width
  end
end

#textwidth_and_maxwidth_in_pixelObject



56
57
58
59
60
61
# File 'lib/widgets/auto_resize_entry.rb', line 56

def textwidth_and_maxwidth_in_pixel
  create_dummy_frame_with_same_size_label do |frame, label|
    { text_width: label.tk_widget.winfo_width,
      max_width: frame.tk_widget.winfo_width }
  end
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