-
Notifications
You must be signed in to change notification settings - Fork 6
/
hello_styled_text.rb
140 lines (127 loc) · 5.64 KB
/
hello_styled_text.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
79
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# Copyright (c) 2007-2024 Andy Maleh
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
require 'glimmer-dsl-swt'
verbiage = <<-MULTI_LINE_STRING
Glimmer DSL for SWT is a native-GUI cross-platform desktop development library written in JRuby,
an OS-threaded faster version of Ruby.
Glimmer's main innovation is a declarative Ruby DSL that enables productive and efficient authoring
of desktop application user-interfaces while relying on the robust Eclipse SWT library.
Glimmer additionally innovates by having built-in data-binding support, which greatly facilitates
synchronizing the GUI with domain models, thus achieving true decoupling of object oriented components
and enabling developers to solve business problems (test-first) without worrying about GUI concerns.
To get started quickly, Glimmer offers scaffolding options for Apps, Gems, and Custom Widgets.
Glimmer also includes native-executable packaging support, sorely lacking in other libraries,
thus enabling the delivery of desktop apps written in Ruby as truly native DMG/PKG/APP files on
the Mac + App Store, MSI/EXE files on Windows, and Gem Packaged Shell Scripts on Linux.
MULTI_LINE_STRING
class StyledTextPresenter
include Glimmer
attr_accessor :text, :caret_offset, :selection_count, :selection, :top_pixel
def line_index_for_offset(line_offset)
text[0..line_offset].split("\n").size
end
end
include Glimmer
@presenter = StyledTextPresenter.new
@presenter.text = verbiage*4
@presenter.caret_offset = 0
@presenter.selection_count = 0
@presenter.selection = Point.new(0, 0)
@presenter.top_pixel = 0
shell {
text 'Hello, Styled Text!'
composite {
@styled_text = styled_text {
layout_data :fill, :fill, true, true
text <=> [@presenter, :text]
left_margin 5
top_margin 5
right_margin 5
bottom_margin 5
# caret offset scrolls text to view when out of page
caret_offset <=> [@presenter, :caret_offset]
# selection_count is not needed if selection is used
selection_count <=> [@presenter, :selection_count]
# selection contains both caret_offset and selection_count, but setting it does not scroll text into view if out of page
selection <=> [@presenter, :selection]
# top_pixel indicates vertically what pixel scrolling is at in a long multi-page text document
top_pixel <=> [@presenter, :top_pixel]
# This demonstrates how to set styles via a listener
on_line_get_style do |line_style_event|
line_offset = line_style_event.lineOffset
if @presenter.line_index_for_offset(line_offset) % 52 < 13
line_size = line_style_event.lineText.size
style_range = StyleRange.new(line_offset, line_size, color(:blue).swt_color, nil, swt(:italic))
style_range.font = Font.new(display.swt_display, 'Times New Roman', 18, swt(:normal))
line_style_event.styles = [style_range]
elsif @presenter.line_index_for_offset(line_offset) % 52 < 26
line_size = line_style_event.lineText.size
style_range = StyleRange.new(line_offset, line_size, color(:dark_green).swt_color, color(:yellow).swt_color, swt(:bold))
line_style_event.styles = [style_range]
elsif @presenter.line_index_for_offset(line_offset) % 52 < 39
line_size = line_style_event.lineText.size
style_range = StyleRange.new(line_offset, line_size, color(:red).swt_color, nil, swt(:normal))
style_range.underline = true
style_range.font = Font.new(display.swt_display, 'Arial', 16, swt(:normal))
line_style_event.styles = [style_range]
else
line_size = line_style_event.lineText.size
style_range = StyleRange.new(line_offset, line_size, color(:dark_magenta).swt_color, color(:cyan).swt_color, swt(:normal))
style_range.strikeout = true
line_style_event.styles = [style_range]
end
end
}
composite {
row_layout :horizontal
label {
text 'Caret Offset:'
}
text {
text <=> [@presenter, :caret_offset, on_read: ->(o) {"%04d" % [o] }]
}
label {
text 'Selection Count:'
}
text {
text <=> [@presenter, :selection_count, on_read: ->(o) {"%04d" % [o] }]
}
label {
text 'Selection Start:'
}
text {
text <=> [@presenter, 'selection.x', on_read: ->(o) {"%04d" % [o] }]
}
label {
text 'Selection End:'
}
text {
text <=> [@presenter, 'selection.y', on_read: ->(o) {"%04d" % [o] }]
}
label {
text 'Top Pixel:'
}
text {
text <=> [@presenter, :top_pixel, on_read: ->(o) {"%04d" % [o] }]
}
}
}
}.open