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

call this with a block that alters the appearance or content of the text control - this includes call to set_style(), highlight() as well as changing text content. It reduces flicker and keeps the viewable area positioned correctly.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/weft/wxgui/controls/textcontrols.rb', line 72

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



62
63
64
65
66
# File 'lib/weft/wxgui/controls/textcontrols.rb', line 62

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



55
56
57
# File 'lib/weft/wxgui/controls/textcontrols.rb', line 55

def true_insertion_point()
  true_point( insertion_point )
end

#true_point(point) ⇒ Object

a no-op



28
29
30
# File 'lib/weft/wxgui/controls/textcontrols.rb', line 28

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