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.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/watobo/gui/text_viewer.rb', line 101

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.



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

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



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/watobo/gui/text_viewer.rb', line 150

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



34
35
36
# File 'lib/watobo/gui/text_viewer.rb', line 34

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

#highlight_code(pattern = nil) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/watobo/gui/text_viewer.rb', line 64

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



38
39
40
# File 'lib/watobo/gui/text_viewer.rb', line 38

def matchIndex()
  @match_index
end

#numMatchesObject



42
43
44
# File 'lib/watobo/gui/text_viewer.rb', line 42

def numMatches()
  @pattern_matches.length
end

#reset_textObject

reset() this function removes all previous selections and highlightings



180
181
182
# File 'lib/watobo/gui/text_viewer.rb', line 180

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

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



56
57
58
59
60
61
62
# File 'lib/watobo/gui/text_viewer.rb', line 56

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



141
142
143
144
# File 'lib/watobo/gui/text_viewer.rb', line 141

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.



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/watobo/gui/text_viewer.rb', line 188

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



46
47
48
49
# File 'lib/watobo/gui/text_viewer.rb', line 46

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

#showPrevMatchObject



51
52
53
54
# File 'lib/watobo/gui/text_viewer.rb', line 51

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

#subscribe(event, &callback) ⇒ Object



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

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