Class: Glimmer::Tk::TextProxy
Overview
Proxy for Tk::Text
Follows the Proxy Design Pattern
Instance Attribute Summary
Attributes inherited from WidgetProxy
#args, #children, #keyword, #parent_proxy, #tk
Instance Method Summary
collapse
-
#add_format(region_start, region_end, option, value) ⇒ Object
-
#add_selection_font_format(option, value) ⇒ Object
-
#add_selection_format(option, value) ⇒ Object
-
#applied_format?(region_start, region_end, option, value) ⇒ Boolean
-
#applied_format_tags(region_start, region_end, option, value) ⇒ Object
-
#handle_listener(listener_name, &listener) ⇒ Object
-
#process_selection_ranges(&processor) ⇒ Object
-
#remove_format(region_start, region_end, option, value) ⇒ Object
-
#remove_selection_font_format(option, value) ⇒ Object
-
#remove_selection_format(option, value) ⇒ Object
-
#text ⇒ Object
-
#text=(value) ⇒ Object
-
#toggle_format(region_start, region_end, option, value) ⇒ Object
toggles option/value tag (removes if already applied).
-
#toggle_selection_font_format(option, value) ⇒ Object
-
#toggle_selection_format(option, value) ⇒ Object
Methods inherited from WidgetProxy
#add_observer, #apply_style, #attribute_setter, #content, create, #font=, #get_attribute, #grid, #has_attribute?, #has_attributes_attribute?, #has_state?, #image_argument, #initialize, #method_missing, #post_add_content, #post_initialize_child, #respond_to?, #set_attribute, #style=, 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
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
in the class Glimmer::Tk::WidgetProxy
Instance Method Details
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
|
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
|
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
|
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
|
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 range.first.to_f <= region_start.to_f && range.last.to_f >= region_end.to_f
@tk.tag_cget(tag_name, option) == value
end
end
end
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
|
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
(range.first.to_f <= region_start.to_f && range.last.to_f >= region_end.to_f)
@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
|
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
|
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
|
#text ⇒ Object
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
|
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
|
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
|
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
|