-
Notifications
You must be signed in to change notification settings - Fork 4
/
gtext.R
213 lines (186 loc) · 9.88 KB
/
gtext.R
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
141
142
143
144
145
146
147
148
149
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
177
178
179
180
181
182
183
184
185
186
187
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
##' @include GWidget.R
NULL
## Make a tag table to be shared. We use memoise as we only need to make this once.
make_tag_table <- memoise(gtkTextTagTableNew)
##' toolkit implementation
##'
##' @export
##' @rdname gWidgets2RGtk2-undocumented
##' @method .gtext guiWidgetsToolkitRGtk2
## @export .gtext guiWidgetsToolkitRGtk2
.gtext.guiWidgetsToolkitRGtk2 <- function(toolkit,
text = NULL, width = NULL, height = 300, font.attr = NULL,
wrap = TRUE,
handler = NULL, action = NULL, container = NULL,... ) {
GText$new(toolkit,
text = text, width = width, height = height,
font.attr = font.attr, wrap = wrap,
handler = handler, action = action, container = container, ...
)
}
GText <- setRefClass("GText",
contains="GWidget",
fields=list(
buffer="ANY",
tag_table="ANY",
font_attr="list"
),
methods=list(
initialize=function(toolkit=NULL,
text = NULL, width = NULL, height = 300,
font.attr = NULL, wrap = TRUE,
handler=NULL, action=NULL, container=NULL, ...) {
tag_table <<- make_tag_table()
buffer <<- gtkTextBufferNew(tag_table)
widget <<- gtkTextViewNewWithBuffer(buffer)
widget$SetLeftMargin(10)
widget$SetRightMargin(10)
if(wrap)
widget$SetWrapMode(GtkWrapMode['word'])
else
widget$SetWrapMode(GtkWrapMode['none'])
block <<- gtkScrolledWindowNew()
block$SetPolicy("GTK_POLICY_AUTOMATIC","GTK_POLICY_AUTOMATIC")
if(!is.null(width))
block$SetSizeRequest(width,height)
block$add(widget)
widget$show()
font_attr <<- getWithDefault(font.attr, list())
insert_text(text, where="beginning", do.newline=FALSE)
add_to_parent(container, .self, ...)
handler_id <<- add_handler_changed(handler, action)
callSuper(toolkit)
},
get_value=function(drop=FALSE, ...) {
"Return text, or selected text if drop=TRUE"
if(is.null(drop) || drop == FALSE) {
start <- buffer$GetStartIter()$iter
end <- buffer$GetEndIter()$iter
} else {
## return only **selected** text
## if drop==TRUE
bounds <- buffer$GetSelectionBounds()
if(bounds$retval == FALSE)
return("") # no selectin
start <- bounds$start
end <- bounds$end
}
buffer$GetText(start, end) # has embedded "\n"
},
set_value=function(value, ...) {
"Replace all text, pasted together with newline"
args <- list(...)
drop <- getWithDefault(args$drop, FALSE)
bounds <- buffer$GetSelectionBounds()
if (drop && bounds$retval) {
## replace selection with new string
buffer$insert(bounds$start, value, -1)
new_bounds <- buffer$GetSelectionBounds()
buffer$delete(new_bounds$start, new_bounds$end)
} else {
value <- paste(value, collapse="\n")
buffer$setText(value)
}
},
get_index = function(...) {
stop("Not defined")
},
set_index = function(value,...) {
stop("Not defined")
},
get_items = function(i, j, ..., drop=TRUE) {
stop("Not defined")
},
set_items = function(value, i, j, ...) {
stop("Not defined")
},
get_tag_name=function(name, value) {
"Return tag table name for passing to function"
value <- switch(name,
"weight" = PangoWeight[value],
"style" = PangoStyle[value],
"size" = if(is.numeric(value)) {
as.integer(value)
} else {
PangoScale[value] * 12
},
"scale" = PangoScale[value],
value)
name <- switch(name,
"color"="foreground",
"size" = "size-points",
name)
nm <- sprintf("%s-%s", name, value)
if(is.null(tag_table$lookup(nm))) {
tt <- gtkTextTagNew(nm) # family-Monospace, say
tt[name] <- value
tag_table$add(tt)
}
nm
},
set_font = function(font.attr) {
"Set font for selection or entire buffer if no selection"
font.attr <- sapply(font.attr, identity, simplify=FALSE)
if(length(font.attr) == 0)
return()
bounds <- buffer$GetSelectionBounds()
if(bounds$retval == FALSE) {
## if no text selected, we set for entire buffer
## change entire buffer -- new as of 0.64
start <- buffer$GetStartIter()$iter
end <- buffer$GetEndIter()$iter
buffer$removeAllTags(start, end) # remove, the reset below
} else {
start <- bounds$start
end <- bounds$end
}
for(i in names(font.attr)) {
tag_nm <- get_tag_name(i, tolower(font.attr[i]))
buffer$ApplyTagByName(tag_nm, start, end)
}
},
insert_text=function(value, where, font.attr=NULL, do.newline, ...) {
"Insert text into buffer. Font.attr is a list with named quantities"
if(is_empty(value))
return()
iter <- switch(where,
"end"=buffer$GetEndIter()$iter,
"beginning"=buffer$GetStartIter()$iter,
buffer$getIterAtMark(buffer$getInsert())$iter)
value <- paste(c(value,""), collapse=ifelse(do.newline, "\n", ""))
arg_list <- list(object=buffer, iter=iter, text=value)
if(is.null(font.attr)) {
font.attr <- font_attr
} else {
font.attr <- sapply(font.attr, identity, simplify=FALSE)
font.attr <- gWidgets2:::merge.list(font_attr, font.attr)
}
if(length(font.attr) > 0) {
for(i in names(font.attr)) {
arg_list[[length(arg_list) + 1]] <- get_tag_name(i, font.attr[[i]])
}
}
do.call("gtkTextBufferInsertWithTagsByName",arg_list)
## scroll to end -- if appended to end
if(where == "end") {
gdkWindowProcessAllUpdates()
while (gtkEventsPending())
gtkMainIterationDo(blocking=FALSE)
end <- buffer$getEndIter()$iter
widget$scrollToIter(end, within.margin = 0, use.align=TRUE)
}
},
## editable
get_editable=function(...) {
widget$getEditable()
},
set_editable=function(value,...) {
widget$setEditable(as.logical(value))
},
add_handler_changed=function(handler, action=NULL, ...) {
add_handler_keystroke(handler, action=action, ...)
},
add_handler_selection_changed=function(handler, action=NULL, ...) {
## connect to buffer's `mark_set` and grab the selection bounds
}
))