Class: QDA::GUI::TrueSelectionTextCtrl

Inherits:
Wx::TextCtrl
  • Object
show all
Defined in:
lib/weft/wxgui/controls/textcontrols.rb

Overview

a text control that is able to relate the selection back to the source text - addresses issues with the cross-platform representation of newlines in multiline text controls.

Direct Known Subclasses

HighlightingTextCtrl

Constant Summary collapse

NEWLINE_CORRECTION_FACTOR =

GTK and Mac OS X

0

Instance Method Summary collapse

Instance Method Details

#save_positionObject

a no-op - scroll_pos always returns 0 on Linux?



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/weft/wxgui/controls/textcontrols.rb', line 29

def save_position()
  freeze()
  saved_pos = get_scroll_pos(Wx::VERTICAL)

  # calculate how many pixels it moves when we nudge the scrollbar
  # down a 'line'
  scroll_lines(1)
  pos_down = saved_pos - get_scroll_pos(Wx::VERTICAL)
  # calculate how many pixels it moves when we nudge the scrollbar
  # up a 'line'
  scroll_lines(-1)
  pos_up = saved_pos - get_scroll_pos(Wx::VERTICAL)

  # how long is a 'line'
  movement = 0 - pos_down + pos_up
  yield
  show_position(0)
  if movement > 0
    scroll_lines(saved_pos / movement)
  end
  thaw()
end

#true_index_to_pos(index) ⇒ Object

given an index within the underlying string, returns the corresponding position within the TextCtrl



94
95
96
97
98
# File 'lib/weft/wxgui/controls/textcontrols.rb', line 94

def true_index_to_pos(index)
  str = get_value()[0, index]
  adjustment = str.count("\n") * NEWLINE_CORRECTION_FACTOR
  index + adjustment
end

#true_insertion_pointObject Also known as: get_true_insertion_point

returns the current insertion point as an index within the underlying string



87
88
89
# File 'lib/weft/wxgui/controls/textcontrols.rb', line 87

def true_insertion_point()
  true_point( insertion_point )
end

#true_point(point) ⇒ Object

a no-op



60
61
62
# File 'lib/weft/wxgui/controls/textcontrols.rb', line 60

def true_point(point)
  point - get_range(0, point).count("\n")
end

#true_selectionObject

returns the start and end of the selection as indexes within the underlying string. This is handled differently on each platform, but the end result is the same.



17
18
19
20
21
22
23
# File 'lib/weft/wxgui/controls/textcontrols.rb', line 17

def true_selection()
  # On windows, we have to correct for the way WxWidgets returns
  # newlines in TextCtrls as two characters.
  lines = get_range(0, get_insertion_point()).count("\n")
  [ get_insertion_point() - lines,
    get_insertion_point() + get_string_selection().length - lines ]
end