Class: Watobo::Gui::TextView2

Inherits:
FXText
  • Object
show all
Defined in:
lib/watobo/gui/text_viewer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner, opts) ⇒ TextView2

Returns a new instance of TextView2.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/watobo/gui/text_viewer.rb', line 80

def initialize(owner, opts)

  @pattern_matches = []
  @raw_text = ""
  @match_index = 0
  @event_dispatcher_listeners = Hash.new
  @parse_code = true
  @code_pattern = "%%"
  @code_style = 1

  super(owner, opts)

  # Construct some hilite styles
  hs_green = FXHiliteStyle.new
  hs_green.normalForeColor = FXRGBA(255,255,255,255) #FXColor::Red
  hs_green.normalBackColor = FXRGBA(0,255,0,1)   # FXColor::White
  hs_green.style = FXText::STYLE_BOLD

  hs_red = FXHiliteStyle.new
  hs_red.normalForeColor = FXRGBA(255,255,255,255) #FXColor::Red
  hs_red.normalBackColor = FXRGBA(255,0,0,1)   # FXColor::White
  hs_red.style = FXText::STYLE_BOLD

  # Enable the style buffer for this text widget
  self.styled = true
  # Set the styles
  self.hiliteStyles = [ hs_green, hs_red]

  self.editable = false

  self.textStyle |= TEXT_WORDWRAP
  
  self.connect(SEL_CHANGED){
    if @parse_code
      highlight_code()
    end
  }

end

Instance Attribute Details

#max_lenObject

Returns the value of attribute max_len.



6
7
8
# File 'lib/watobo/gui/text_viewer.rb', line 6

def max_len
  @max_len
end

Instance Method Details

#applyFilter(pattern, prefs = {}) ⇒ Object

applies a specific filter (string or regex).

It returns an array containing [pos, len] pairs of each match



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/watobo/gui/text_viewer.rb', line 129

def applyFilter(pattern, prefs={})
  cprefs = { :highlight => true,
    :style_index => 2}
  cprefs.update(prefs) if prefs.is_a? Hash

  dummy = self.to_s
  #remove previous highlighting
  self.killSelection()
  self.setText(dummy)
  @match_index = 0
  @pattern_matches = matchPattern(pattern)
  if cprefs[:highlight] == true
    # puts "* found pattern #{pattern} #{@pattern_matches.length} times"
    @pattern_matches.each do |start, len|
      begin
        self.changeStyle(start, len, cprefs[:style_index])
      rescue => bang
        puts "outch"
        puts bang
      end
    end
  end

  # now re-highlight input and set cursor to last pos

  return @pattern_matches
end

#clearEvents(event) ⇒ Object



13
14
15
# File 'lib/watobo/gui/text_viewer.rb', line 13

def clearEvents(event)
  @event_dispatcher_listener[event].clear
end

#highlight_code(pattern = nil) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/watobo/gui/text_viewer.rb', line 43

def highlight_code(pattern=nil)
  # remove all styles
  self.changeStyle(0, self.length-1, 0)
  mark = pattern.nil? ? @code_pattern : pattern
  sindex = nil
  pos = 0
  match = []
  text = self.to_s
  mark = "%%"

  loop do
    sindex = text.index(mark, pos)
    nli = text.index("\n", pos)
    break if sindex.nil?
    puts pos
    
    unless nli.nil?
      match = [] if nli < sindex
    end
    
    match << sindex
    
    if match.length == 2          
      start = match[0]
      len = match[1] - match[0] + mark.length
      
      self.changeStyle(start, len, @code_style)
      match = []
      pos = sindex + mark.length - 1
    else
      pos = sindex + mark.length            
    end
    
    break if pos >= self.length-1 + mark.length
  end
end

#matchIndexObject



17
18
19
# File 'lib/watobo/gui/text_viewer.rb', line 17

def matchIndex()
  @match_index
end

#numMatchesObject



21
22
23
# File 'lib/watobo/gui/text_viewer.rb', line 21

def numMatches()
  @pattern_matches.length
end

#reset_textObject

reset() this function removes all previous selections and highlightings



159
160
161
# File 'lib/watobo/gui/text_viewer.rb', line 159

def reset_text()
  self.setText(self.to_s)
end

#setFont(font_type = nil, size = nil) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/watobo/gui/text_viewer.rb', line 35

def setFont(font_type=nil, size=nil)
  new_size = size.nil? ? GUI_REGULAR_FONT_SIZE : size
  new_font_type = font_type.nil? ? "helvetica" : font_type
  new_font = FXFont.new(getApp(), new_font_type, new_size)
  new_font.create
  self.font = new_font
end

#setPrintable(text, prefs = {}) ⇒ Object



120
121
122
123
# File 'lib/watobo/gui/text_viewer.rb', line 120

def setPrintable(text, prefs={})
  @raw_text = text
  self.setText(normalizeText(@raw_text))
end

#showMatch(match_index = 0, prefs = {}) ⇒ Object

showMatch(index=0) this function makes a specific match visible in the text field. The default index value is 0. The function returns the index of the current match index.



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/watobo/gui/text_viewer.rb', line 167

def showMatch(match_index=0, prefs={})
  cprefs = { :select_match => false }
    
  cprefs.update prefs
  
  return @match_index if @pattern_matches.empty?
  return @match_index if match_index > ( @pattern_matches.length - 1 )
  return @match_index if match_index < 0
  
  if @pattern_matches[match_index] then
    @match_index = match_index
    pos = @pattern_matches[match_index][0]
    len =@pattern_matches[match_index][1]

    self.setCenterLine(pos)

    #   @textbox.makePositionVisible(pos + len)
    self.makePositionVisible(self.lineEnd(pos))
    self.makePositionVisible(pos)

    self.setCursorPos(pos)

    self.killSelection()
    self.setSelection(pos, len) if cprefs[:select_match] == true
  end
  return @match_index
end

#showNextMatchObject



25
26
27
28
# File 'lib/watobo/gui/text_viewer.rb', line 25

def showNextMatch()
 # puts "* showNextMatch -> #{@match_index+1}"
  showMatch(@match_index + 1)
end

#showPrevMatchObject



30
31
32
33
# File 'lib/watobo/gui/text_viewer.rb', line 30

def showPrevMatch()
 # puts "* showPrevMatch -> #{@match_index-1}"
  showMatch(@match_index - 1)
end

#subscribe(event, &callback) ⇒ Object



9
10
11
# File 'lib/watobo/gui/text_viewer.rb', line 9

def subscribe(event, &callback)
  (@event_dispatcher_listeners[event] ||= []) << callback
end