Class: TkWrapper::Widgets::Text

Inherits:
Base::Widget show all
Defined in:
lib/widgets/text.rb

Instance Attribute Summary

Attributes inherited from Base::Widget

#cell, #childs, #config, #font, #ids, #manager, #opts, #parent, #winfo

Instance Method Summary collapse

Methods inherited from Base::Widget

#create_tk_widget, #each, #init_id, #initialize, #initialize_utilities, #normalize_childs, #push, #tk_widget

Constructor Details

This class inherits a constructor from TkWrapper::Widgets::Base::Widget

Instance Method Details

#accumulated_border_and_padding_heightObject



63
64
65
# File 'lib/widgets/text.rb', line 63

def accumulated_border_and_padding_height
  2 * (opts.pady + opts.borderwidth)
end

#accumulated_border_and_padding_widthObject



59
60
61
# File 'lib/widgets/text.rb', line 59

def accumulated_border_and_padding_width
  2 * (opts.padx + opts.borderwidth)
end

#count_linesObject



15
16
17
# File 'lib/widgets/text.rb', line 15

def count_lines
  pos('end')[0] - 1
end

#current_line_nrObject



19
20
21
# File 'lib/widgets/text.rb', line 19

def current_line_nr
  pos('insert')[0]
end

#height_of_lines(num_lines = count_lines) ⇒ Object



47
48
49
50
51
52
# File 'lib/widgets/text.rb', line 47

def height_of_lines(num_lines = count_lines)
  h = num_lines * line_height_including_padding

  # spacing 3: additional space between lines
  h + [num_lines - 1, 0].max * opts.spacing3
end

#line_height_including_paddingObject

includes spacing1 and spacing2 (add. space above and below line) does not include spacing3 (additional space between lines)



40
41
42
43
44
45
# File 'lib/widgets/text.rb', line 40

def line_height_including_padding
  # linespace: max height of characters of the font
  # spacing1: additional space above line
  # spacing3: additional space below line
  @font.linespace + opts.spacing1 + opts.spacing3
end

#lines(start_pos = '1.0', end_pos = 'end') ⇒ Object



28
29
30
31
32
# File 'lib/widgets/text.rb', line 28

def lines(start_pos = '1.0', end_pos = 'end')
  start_pos = start_pos.is_a?(String) ? start_pos : "#{start_pos}.0"
  end_pos   = end_pos.is_a?(String)   ? end_pos   : "#{end_pos}.0"
  tk_widget.get(start_pos, end_pos).split("\n")
end

#longest_line_widthObject



34
35
36
# File 'lib/widgets/text.rb', line 34

def longest_line_width
  lines.map { |line| @font.measure(line) }.max || 0
end

#pos(key) ⇒ Object



10
11
12
13
# File 'lib/widgets/text.rb', line 10

def pos(key)
  match = tk_widget.index(key).match(/(.*)\.(.*)/)
  [match[1].to_i, match[2].to_i]
end

#resize(lines: nil, chars: nil) ⇒ Object



54
55
56
57
# File 'lib/widgets/text.rb', line 54

def resize(lines: nil, chars: nil)
  tk_widget['height'] = lines if lines
  tk_widget['width']  = chars if chars
end

#tk_classObject



6
7
8
# File 'lib/widgets/text.rb', line 6

def tk_class
  TkText
end

#value=(value) ⇒ Object



23
24
25
26
# File 'lib/widgets/text.rb', line 23

def value=(value)
  tk_widget.delete('1.0', 'end')
  tk_widget.insert('1.0', value)
end