Class: Glimmer::Tk::TextProxy

Inherits:
WidgetProxy show all
Defined in:
lib/glimmer/tk/text_proxy.rb

Overview

Proxy for Tk::Text

Follows the Proxy Design Pattern

Constant Summary

Constants inherited from WidgetProxy

WidgetProxy::FONTS_PREDEFINED

Instance Attribute Summary

Attributes inherited from WidgetProxy

#args, #children, #keyword, #on_drag_motion_block, #parent_proxy, #tk

Instance Method Summary collapse

Methods inherited from WidgetProxy

#add_observer, #apply_style, #attribute_setter, #content, create, #drag_source=, #font=, #get_attribute, #grid, #has_attribute?, #has_attributes_attribute?, #has_state?, #image_argument, #initialize, #make_draggable, #make_non_draggable, #method_missing, #on_drag_start_block=, #on_drop_block=, #post_add_content, #post_initialize_child, #respond_to?, #set_attribute, #style=, #textvariable_defined?, tk_widget_class_for, #tk_widget_has_attribute_getter_setter?, #tk_widget_has_attribute_setter?, #widget_attribute_listener_installers, #widget_custom_attribute_mapping, widget_exists?, widget_proxy_class

Constructor Details

This class inherits a constructor from Glimmer::Tk::WidgetProxy

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Glimmer::Tk::WidgetProxy

Instance Method Details

#add_font_format(region_start, region_end, font_option, value) ⇒ Object



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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/glimmer/tk/text_proxy.rb', line 188

def add_font_format(region_start, region_end, font_option, value)
  applied_font_format_tags_and_regions(region_start, region_end).each do |tag, tag_region_start, tag_region_end|
    if tag
      bigger_region_tag = @tk.tag_ranges(tag).any? do |range_start, range_end|
        text_index_less_than_other_text_index?(range_start, tag_region_start) || text_index_greater_than_other_text_index?(range_end, tag_region_end)
      end
      if bigger_region_tag
        @tk.tag_ranges(tag).each do |range_start, range_end|
          if text_index_less_than_other_text_index?(range_start, tag_region_start) && text_index_less_than_or_equal_to_other_text_index?(range_end, tag_region_end) && text_index_greater_than_or_equal_to_other_text_index?(range_end, tag_region_start)
            font = @tk.tag_cget(tag, 'font')
            remove_format(range_start, range_end, 'font', font)
            add_format(range_start, tag_region_start, 'font', font)
            font_clone = clone_font(font)
            font_clone.send("#{font_option}=", value)
            add_format(tag_region_start, tag_region_end, 'font', font_clone)
          elsif text_index_greater_than_other_text_index?(range_end, tag_region_end) && text_index_greater_than_or_equal_to_other_text_index?(range_start, tag_region_start) && text_index_less_than_or_equal_to_other_text_index?(range_start, tag_region_end)
            font = @tk.tag_cget(tag, 'font')
            remove_format(range_start, range_end, 'font', font)
            add_format(tag_region_end, range_end, 'font', font)
            font_clone = clone_font(font)
            font_clone.send("#{font_option}=", value)
            add_format(tag_region_start, tag_region_end, 'font', font_clone)
          elsif text_index_less_than_other_text_index?(range_start, tag_region_start) && text_index_greater_than_other_text_index?(range_end, tag_region_end)
            font = @tk.tag_cget(tag, 'font')
            remove_format(range_start, range_end, 'font', font)
            add_format(range_start, tag_region_start, 'font', font)
            remove_format(range_start, range_end, 'font', font)
            add_format(tag_region_end, range_end, 'font', font)
            font_clone = clone_font(font)
            font_clone.send("#{font_option}=", value)
            add_format(tag_region_start, tag_region_end, 'font', font_clone)
          end
        end
      else
        current_font = @tk.tag_cget(tag, 'font')
        current_font.send("#{font_option}=", value)
      end
    else
      add_format(tag_region_start, tag_region_end, 'font', default_font_attributes.merge(font_option => value))
    end
  end
end

#add_format(region_start, region_end, option, value) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/glimmer/tk/text_proxy.rb', line 110

def add_format(region_start, region_end, option, value)
  @@tag_number = 0 unless defined?(@@tag_number)
  tag = "tag#{@@tag_number += 1}"
  @tk.tag_configure(tag, {option => value})
  @tk.tag_add(tag, region_start, region_end)
  tag
end

#add_selection_font_format(option, value) ⇒ Object



74
75
76
# File 'lib/glimmer/tk/text_proxy.rb', line 74

def add_selection_font_format(option, value)
  process_selection_ranges { |range_start, range_end| add_font_format(range_start, range_end, option, value) }
end

#add_selection_format(option, value) ⇒ Object



62
63
64
# File 'lib/glimmer/tk/text_proxy.rb', line 62

def add_selection_format(option, value)
  process_selection_ranges { |range_start, range_end| add_format(range_start, range_end, option, value) }
end

#add_to_text_index(text_index, addition) ⇒ Object



291
292
293
294
295
296
297
# File 'lib/glimmer/tk/text_proxy.rb', line 291

def add_to_text_index(text_index, addition)
  text_index_parts = text_index.split('.')
  line = text_index_parts.first
  char_index = text_index_parts.last
  char_index = char_index.to_i + addition
  "#{line}.#{char_index}"
end

#applied_font_format?(region_start, region_end, font_option, value) ⇒ Boolean

TODO Algorithm for font option formatting for a region, grab all the latest tags for each subregion as well as the widget font for subregions without a tag for each part of the region covered by a tag, augment its font with new font option (or remove if that is what is needed) Once add and remove are implemented, implement toggle Also, there is a need for a method that checks if a font option value applies to an entire region (to decide which way to toggle with toggle method)

Returns:

  • (Boolean)


150
151
152
153
154
155
156
157
158
# File 'lib/glimmer/tk/text_proxy.rb', line 150

def applied_font_format?(region_start, region_end, font_option, value)
  applied_font_format_tags_and_regions(region_start, region_end).all? do |tag, region_start, region_end|
    if tag.nil?
      @tk.font.send(font_option) == value
    else
      @tk.tag_cget(tag, 'font').send(font_option) == value
    end
  end
end

#applied_font_format_tags_and_regions(region_start, region_end) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/glimmer/tk/text_proxy.rb', line 160

def applied_font_format_tags_and_regions(region_start, region_end)
  lines = text.split("\n")
  tags_and_regions = []
  all_tag_names = @tk.tag_names - ['sel']
  (region_start.to_i..region_end.to_i).each do |line_number|
    start_character_index = 0
    start_character_index = region_start.to_s.split('.').last.to_i if line_number == region_start.to_i
    end_character_index = lines[line_number - 1].size
    end_character_index = region_end.to_s.split('.').last.to_i if line_number == region_end.to_i
    (start_character_index...end_character_index).each do |character_index|
      text_index = "#{line_number}.#{character_index}"
      # TODO reimplement the following using @tk.tag_names without arg since passing an arg seems broken and returns inaccurate results
      region_tag = all_tag_names.reverse.find do |tag|
        @tk.tag_cget(tag, 'font') && @tk.tag_ranges(tag).any? do |range_start, range_end|
          text_index_less_than_or_equal_to_other_text_index?(range_start, text_index) && text_index_greater_than_or_equal_to_other_text_index?(range_end, text_index)
        end
      end
      end_text_index = add_to_text_index(text_index, 1)
      if tags_and_regions&.last && region_tag == tags_and_regions.last.first
        tags_and_regions.last[2] = end_text_index
      else
        tags_and_regions << [region_tag, text_index, end_text_index]
      end
    end
  end
  tags_and_regions
end

#applied_format?(region_start, region_end, option, value) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/glimmer/tk/text_proxy.rb', line 94

def applied_format?(region_start, region_end, option, value)
  !applied_format_tags(region_start, region_end, option, value).empty?
end

#applied_format_tags(region_start, region_end, option, value) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/glimmer/tk/text_proxy.rb', line 98

def applied_format_tags(region_start, region_end, option, value)
  tag_names = @tk.tag_names - ['sel']
  
  tag_names.select do |tag_name|
    @tk.tag_ranges(tag_name).any? do |range|
      if text_index_less_than_or_equal_to_other_text_index?(range.first, region_start) && text_index_greater_than_or_equal_to_other_text_index?(range.last, region_end)
        @tk.tag_cget(tag_name, option) == value
      end
    end
  end
end

#clone_font(font) ⇒ Object



335
336
337
# File 'lib/glimmer/tk/text_proxy.rb', line 335

def clone_font(font)
  ::TkFont.new(Hash[font.actual])
end

#default_font_attributesObject



287
288
289
# File 'lib/glimmer/tk/text_proxy.rb', line 287

def default_font_attributes
  Hash[@tk.font.actual]
end

#default_for_font_option(font_option) ⇒ Object



283
284
285
# File 'lib/glimmer/tk/text_proxy.rb', line 283

def default_for_font_option(font_option)
  @tk.font.send(font_option)
end

#handle_listener(listener_name, &listener) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/glimmer/tk/text_proxy.rb', line 30

def handle_listener(listener_name, &listener)
  listener_name = listener_name.to_s.downcase
  case listener_name
  when '<<modified>>', '<modified>', 'modified'
    modified_listener = Proc.new do |*args|
      listener.call(*args)
      @tk.modified = false
    end
    bind('<Modified>', modified_listener)
  when '<<selection>>', '<selection>', 'selection'
    bind('<Selection>', listener)
  else
    super
  end
end

#process_selection_ranges(&processor) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/glimmer/tk/text_proxy.rb', line 86

def process_selection_ranges(&processor)
  @tk.tag_ranges('sel').each do |region|
    range_start = region.first
    range_end = region.last
    processor.call(range_start, range_end)
  end
end

#remove_font_format(region_start, region_end, font_option, value) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/glimmer/tk/text_proxy.rb', line 231

def remove_font_format(region_start, region_end, font_option, value)
  applied_font_format_tags_and_regions(region_start, region_end).each do |tag, tag_region_start, tag_region_end|
    if tag
      bigger_region_tag = @tk.tag_ranges(tag).any? do |range_start, range_end|
        text_index_less_than_other_text_index?(range_start, tag_region_start) || text_index_greater_than_other_text_index?(range_end, tag_region_end)
      end
      if bigger_region_tag
        @tk.tag_ranges(tag).each do |range_start, range_end|
          if text_index_less_than_other_text_index?(range_start, tag_region_start) && text_index_less_than_or_equal_to_other_text_index?(range_end, tag_region_end) && text_index_greater_than_or_equal_to_other_text_index?(range_end, tag_region_start)
            font = @tk.tag_cget(tag, 'font')
            remove_format(range_start, range_end, 'font', font)
            add_format(range_start, subtract_from_text_index(tag_region_start, 1), 'font', font)
            font_clone = clone_font(font)
            font_clone.send("#{font_option}=", default_for_font_option(font_option))
            add_format(tag_region_start, tag_region_end, 'font', font_clone)
          elsif text_index_greater_than_other_text_index?(range_end, tag_region_end) && text_index_greater_than_or_equal_to_other_text_index?(range_start, tag_region_start) && text_index_less_than_or_equal_to_other_text_index?(range_start, tag_region_end)
            font = @tk.tag_cget(tag, 'font')
            remove_format(range_start, range_end, 'font', font)
            add_format(add_to_text_index(tag_region_end, 1), range_end, 'font', font)
            font_clone = clone_font(font)
            font_clone.send("#{font_option}=", default_for_font_option(font_option))
            add_format(tag_region_start, tag_region_end, 'font', font_clone)
          elsif text_index_less_than_other_text_index?(range_start, tag_region_start) && text_index_greater_than_other_text_index?(range_end, tag_region_end)
            font = @tk.tag_cget(tag, 'font')
            remove_format(range_start, range_end, 'font', font)
            add_format(range_start, subtract_from_text_index(tag_region_start, 1), 'font', font)
            remove_format(range_start, range_end, 'font', font)
            add_format(add_to_text_index(tag_region_end, 1), range_end, 'font', font)
            font_clone = clone_font(font)
            font_clone.send("#{font_option}=", default_for_font_option(font_option))
            add_format(tag_region_start, tag_region_end, 'font', font_clone)
          end
        end
      else
        current_font = @tk.tag_cget(tag, 'font')
        current_font.send("#{font_option}=", default_for_font_option(font_option))
      end
    else
      add_format(tag_region_start, tag_region_end, 'font', default_font_attributes.merge(font_option => default_for_font_option(font_option)))
    end
  end
end

#remove_format(region_start, region_end, option, value) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/glimmer/tk/text_proxy.rb', line 118

def remove_format(region_start, region_end, option, value)
  partial_intersection_option_applied_tags = tag_names.select do |tag_name|
    @tk.tag_ranges(tag_name).any? do |range|
      if range.first.to_f.between?(region_start.to_f, region_end.to_f) or
         range.last.to_f.between?(region_start.to_f, region_end.to_f) or
         (text_index_less_than_or_equal_to_other_text_index?(range.first, region_start) && text_index_greater_than_or_equal_to_other_text_index?(range.last, region_end))
        @tk.tag_cget(tag_name, option) == value
      end
    end
  end
  
  partial_intersection_option_applied_tags.each do |tag_name|
    @tk.tag_remove(tag_name, region_start, region_end)
  end
  
  nil
end

#remove_selection_font_format(option, value) ⇒ Object



78
79
80
# File 'lib/glimmer/tk/text_proxy.rb', line 78

def remove_selection_font_format(option, value)
  process_selection_ranges { |range_start, range_end| remove_font_format(range_start, range_end, option, value) }
end

#remove_selection_format(option, value) ⇒ Object



66
67
68
# File 'lib/glimmer/tk/text_proxy.rb', line 66

def remove_selection_format(option, value)
  process_selection_ranges { |range_start, range_end| remove_format(range_start, range_end, option, value) }
end

#subtract_from_text_index(text_index, subtraction) ⇒ Object



299
300
301
# File 'lib/glimmer/tk/text_proxy.rb', line 299

def subtract_from_text_index(text_index, subtraction)
  add_to_text_index(text_index, -1 * subtraction)
end

#textObject



58
59
60
# File 'lib/glimmer/tk/text_proxy.rb', line 58

def text
  @text = get("1.0", 'end')
end

#text=(value) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/glimmer/tk/text_proxy.rb', line 46

def text=(value)
  if value != @text
    if @text && value.start_with?(@text)
      insert('end', value[@text.size..-1])
    else
      delete('1.0', 'end')
      insert('end', value)
    end
    @text = value
  end
end

#text_index_greater_than_or_equal_to_other_text_index?(region1, region2) ⇒ Boolean

Returns:

  • (Boolean)


327
328
329
330
331
332
333
# File 'lib/glimmer/tk/text_proxy.rb', line 327

def text_index_greater_than_or_equal_to_other_text_index?(region1, region2)
  region1_parts = region1.to_s.split('.')
  region2_parts = region2.to_s.split('.')
  return true if region1_parts.first.to_i > region2_parts.first.to_i
  return false if region1_parts.first.to_i < region2_parts.first.to_i
  region1_parts.last.to_i >= region2_parts.last.to_i
end

#text_index_greater_than_other_text_index?(region1, region2) ⇒ Boolean

Returns:

  • (Boolean)


319
320
321
322
323
324
325
# File 'lib/glimmer/tk/text_proxy.rb', line 319

def text_index_greater_than_other_text_index?(region1, region2)
  region1_parts = region1.to_s.split('.')
  region2_parts = region2.to_s.split('.')
  return true if region1_parts.first.to_i > region2_parts.first.to_i
  return false if region1_parts.first.to_i < region2_parts.first.to_i
  region1_parts.last.to_i > region2_parts.last.to_i
end

#text_index_less_than_or_equal_to_other_text_index?(region1, region2) ⇒ Boolean

Returns:

  • (Boolean)


311
312
313
314
315
316
317
# File 'lib/glimmer/tk/text_proxy.rb', line 311

def text_index_less_than_or_equal_to_other_text_index?(region1, region2)
  region1_parts = region1.to_s.split('.')
  region2_parts = region2.to_s.split('.')
  return true if region1_parts.first.to_i < region2_parts.first.to_i
  return false if region1_parts.first.to_i > region2_parts.first.to_i
  region1_parts.last.to_i <= region2_parts.last.to_i
end

#text_index_less_than_other_text_index?(region1, region2) ⇒ Boolean

Returns:

  • (Boolean)


303
304
305
306
307
308
309
# File 'lib/glimmer/tk/text_proxy.rb', line 303

def text_index_less_than_other_text_index?(region1, region2)
  region1_parts = region1.to_s.split('.')
  region2_parts = region2.to_s.split('.')
  return true if region1_parts.first.to_i < region2_parts.first.to_i
  return false if region1_parts.first.to_i > region2_parts.first.to_i
  region1_parts.last.to_i < region2_parts.last.to_i
end

#toggle_font_format(region_start, region_end, option, value) ⇒ Object

toggles option/value tag (removes if already applied)



275
276
277
278
279
280
281
# File 'lib/glimmer/tk/text_proxy.rb', line 275

def toggle_font_format(region_start, region_end, option, value)
  if applied_font_format?(region_start, region_end, option, value)
    remove_font_format(region_start, region_end, option, value)
  else
    add_font_format(region_start, region_end, option, value)
  end
end

#toggle_format(region_start, region_end, option, value) ⇒ Object

toggles option/value tag (removes if already applied)



137
138
139
140
141
142
143
# File 'lib/glimmer/tk/text_proxy.rb', line 137

def toggle_format(region_start, region_end, option, value)
  if applied_format?(region_start, region_end, option, value)
    remove_format(region_start, region_end, option, value)
  else
    add_format(region_start, region_end, option, value)
  end
end

#toggle_selection_font_format(option, value) ⇒ Object



82
83
84
# File 'lib/glimmer/tk/text_proxy.rb', line 82

def toggle_selection_font_format(option, value)
  process_selection_ranges { |range_start, range_end| toggle_font_format(range_start, range_end, option, value) }
end

#toggle_selection_format(option, value) ⇒ Object



70
71
72
# File 'lib/glimmer/tk/text_proxy.rb', line 70

def toggle_selection_format(option, value)
  process_selection_ranges { |range_start, range_end| toggle_format(range_start, range_end, option, value) }
end