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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 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 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
# source("~/research/RGtk2/RGtk2/demo/images.R")
window <- NULL
pixbuf.loader <- NULL
load.timeout <- NULL
image.stream <- NULL
progressive.prepared.callback <- function(loader, data)
{
checkPtrType(data, "GtkWidget")
pixbuf <- loader$getPixbuf()
# Avoid displaying random memory contents, since the pixbuf
# isn't filled in yet.
#
pixbuf$fill("0xaaaaaaff")
data$setFromPixbuf(pixbuf)
}
progressive.updated.callback <- function(loader, x, y, width, height, data)
{
checkPtrType(data, "GtkWidget")
# We know the pixbuf inside the GtkImage has changed, but the image
# itself doesn't know this so queue a redraw. If we wanted to be
# really efficient, we could use a drawing area or something
# instead of a GtkImage, so we could control the exact position of
# the pixbuf on the display, then we could queue a draw for only
# the updated area of the image.
#
data$queueDraw()
}
progressive.timeout <- function(data)
{
checkPtrType(data, "GtkWidget")
# This shows off fully-paranoid error handling, so looks scary.
# You could factor out the error handling code into a nice separate
# function to make things nicer.
#
if (!is.null(image.stream))
{
bytes.read <- try(readBin(image.stream, "integer", 256, 1, FALSE))
if (inherits(bytes.read, "try-error"))
{
dialog <- gtkMessageDialogNew(window, "destroy-with-parent", "error", "close",
"Failure reading image file 'alphatest.png':", bytes.read)
gSignalConnect(dialog, "response", gtkWidgetDestroy)
close(image.stream)
image.stream <<- NULL
load.timeout <<- NULL
return(FALSE) # uninstall the timeout
}
if (length(bytes.read) == 0)
{
close(image.stream)
image.stream <<- NULL
# Errors can happen on close, e.g. if the image
# file was truncated we'll know on close that
# it was incomplete.
#
close.result <- pixbuf.loader$close()
if (!close.result[[1]]) {
dialog <- gtkMessageDialogNew(window, "destroy-with-parent", "error", "close",
"Failed to load image:", close.result$error$message)
gSignalConnect(dialog, "response", gtkWidgetDestroy)
pixbuf.loader <<- NULL
load.timeout <<- NULL
return(FALSE)
}
pixbuf.loader <<- NULL
} else {
write.result <- pixbuf.loader$write(bytes.read, length(bytes.read))
if (!write.result[[1]])
{
dialog <- gtkMessageDialogNew(window, "destroy-with-parent", "error", "close",
"Failed to load image:", write.result$error$message)
gSignalConnect(dialog, "response", gtkWidgetDestroy)
close(image.stream)
image.stream <<- NULL
load.timeout <<- NULL
return(FALSE) # uninstall the timeout
}
}
} else
{
filename <- imagefile("alphatest.png")
if (!file.exists(filename))
{
error.message <- "File 'alphatest.png' does not exist"
} else {
image.stream <<- try(file(filename, "rb"))
if (inherits(image.stream, "try-error")) {
error.message <- paste("Unable to open image file 'alphatest.png':", image.stream)
image.stream <<- NULL
}
}
if (is.null(image.stream))
{
dialog <- gtkMessageDialogNew(window, "destroy-with-parent", "error", "close",
"Failed to load image:", error.message)
gSignalConnect(dialog, "response", gtkWidgetDestroy)
load.timeout <<- NULL
return(FALSE)
}
if (!is.null(pixbuf.loader))
{
pixbuf.loader$close(NULL)
pixbuf.loader <<- NULL
}
pixbuf.loader <<- gdkPixbufLoaderNew()
gSignalConnect(pixbuf.loader, "area_prepared", progressive.prepared.callback, data)
gSignalConnect(pixbuf.loader, "area_updated", progressive.updated.callback, data)
}
# leave timeout installed
return(TRUE)
}
start.progressive.loading <- function(image)
{
# This is obviously totally contrived (we slow down loading
# on purpose to show how incremental loading works).
# The real purpose of incremental loading is the case where
# you are reading data from a slow source such as the network.
# The timeout simply simulates a slow data source by inserting
# pauses in the reading process.
#
load.timeout <<- gTimeoutAdd(150, progressive.timeout, image)
}
cleanup.callback <- function(object, data)
{
if (!is.null(load.timeout))
{
gSourceRemove(load.timeout)
load.timeout <<- NULL
}
if (!is.null(pixbuf.loader))
{
pixbuf.loader$close()
pixbuf.loader <<- NULL
}
if (!is.null(image.stream))
close(image.stream)
image.stream <<- NULL
}
toggle.sensitivity.callback <- function(togglebutton, user.data)
{
container <- user.data
list <- container$getChildren()
sapply(list, function(child) {
# don't disable our toggle
if (!inherits(child, "GtkToggleButton"))
child$setSensitive(!togglebutton$getActive())
})
}
window <- gtkWindowNew("toplevel", show=F)
window$setTitle("Images")
gSignalConnect(window, "destroy", cleanup.callback)
window$setBorderWidth(8)
vbox <- gtkVBoxNew(FALSE, 8)
vbox$setBorderWidth(8)
window$add(vbox)
label <- gtkLabelNew()
label$setMarkup("<u>Image loaded from a file</u>")
vbox$packStart(label, FALSE, FALSE, 0)
frame <- gtkFrameNew()
frame$setShadowType("in")
# The alignment keeps the frame from growing when users resize
# the window
#
align <- gtkAlignmentNew(0.5, 0.5, 0, 0)
align$add(frame)
vbox$packStart(align, FALSE, FALSE, 0)
pixbuf.result <- NULL
pixbuf <- NULL
filename <- imagefile("rgtk-logo.gif")
if (file.exists(filename))
{
pixbuf.result <- gdkPixbufNewFromFile(filename)
pixbuf <- pixbuf.result[[1]]
}
if (is.null(pixbuf.result) || !is.null(pixbuf.result$error))
{
# This code shows off error handling. You can just use
# gtkImageNewFromFile() instead if you don't want to report
# errors to the user. If the file doesn't load when using
# gtkImageNewFromFile(), a "missing image" icon will
# be displayed instead.
#
dialog <- gtkMessageDialogNew(window, "destroy-with-parent", "error", "close",
"Failed to load 'rgtk-logo.gif':", pixbuf.result$error$message)
gSignalConnect(dialog, "response", gtkWidgetDestroy)
}
image <- gtkImageNewFromPixbuf(pixbuf)
frame$add(image)
# Animation #
label <- gtkLabelNew()
label$setMarkup("<u>Animation loaded from a file</u>")
vbox$packStart(label, FALSE, FALSE, 0)
frame <- gtkFrameNew()
frame$setShadowType("in")
align <- gtkAlignmentNew(0.5, 0.5, 0, 0)
align$add(frame)
vbox$packStart(align, FALSE, FALSE, 0)
filename <- "/usr/share/gtk-2.0/demo/floppybuddy.gif"
image <- gtkImageNewFromFile(filename)
frame$add(image)
# Progressive
label <- gtkLabelNew()
label$setMarkup("<u>Progressive image loading</u>")
vbox$packStart(label, FALSE, FALSE, 0)
frame <- gtkFrameNew()
frame$setShadowType("in")
align <- gtkAlignmentNew(0.5, 0.5, 0, 0)
align$add(frame)
vbox$packStart(align, FALSE, FALSE, 0)
# Create an empty image for now the progressive loader
# will create the pixbuf and fill it in.
#
image <- gtkImageNewFromPixbuf(NULL)
frame$add(image)
start.progressive.loading(image)
# Sensitivity control
button <- gtkToggleButtonNewWithMnemonic("_Insensitive")
vbox$packStart(button, FALSE, FALSE, 0)
gSignalConnect(button, "toggled", toggle.sensitivity.callback, vbox)
button <- gtkButtonNewWithLabel("Quit")
vbox$packStart(button, FALSE, FALSE, 0)
gSignalConnect(button, "clicked", function(...) { gtkMainQuit(); window$destroy() })
window$showAll()
gtkMain()
|