[R] RGtk2 - retrieve ggraphics mouse coordinates during drag-and-drop event
Mark Heckmann
mark.heckmann at gmx.de
Tue Dec 22 02:42:57 CET 2009
Hi all,
I have a gtable and a ggraphic widget.
I want to drag an element from the table onto the graphic.
When the drag object is released over the ggraphic widget, I want the
mouse coordinates inside the ggraphic to be returned.
Right now I do not know how to get the mouse ccordinates of the
ggraphic widget at any given time.
How can I access the ggraphics mouse coordinates?
Below you find my sample code (windows only due to ggraphics).
TIA,
Mark
library(gWidgets)
library(RGtk2)
options("guiToolkit"="RGtk2")
w = gwindow("ggraphics example")
table <- gtable(1:10, container=w)
g = ggraphics(cont=w, expand=T)
size(g) <- c(500,500)
Sys.sleep(1)
plot(rnorm(20), col="red")
## convert from "plt" coordinates to more familiar "usr"
pltToUsr = function(x,y) {
plt = par("plt"); usr = par("usr")
c( (usr[2]-usr[1])/(plt[2]-plt[1])*(x - plt[1]) + usr[1],
(usr[4] - usr[3])/(plt[4] - plt[3])*(y - plt[3]) + usr[3])
}
addHandlerFocus(g, handler = function(h,...) {
print(c("focus g"))
})
addHandlerClicked(g, handler = function(h,...) {
x <- h$x; y <- h$y
xStart <<- x; yStart <<- y
pressed <<- TRUE
print(c("pressed at:", c(x,y)))
})
da <- g at widget@widget
callbackID <- gSignalConnect(da,"button-release-event", function
(w,e,...) {
allocation = w$GetAllocation()
xclick = e$GetX()
yclick = e$GetY()
x = xclick/allocation$width
y = (allocation$height - yclick)/allocation$height
xyCoordsRelease <- pltToUsr(x,y)
print(c("released at:", pltToUsr(x,y)))
xMove <- xyCoordsRelease[1] - xStart
yMove <- xyCoordsRelease[2] - yStart
print(c("xMove", round(xMove, 3), "yMove", round(yMove, 3)))
plot(rnorm(20), col="red")
#print(qplot(rnorm(100), geom="histogram"))
pressed <<- FALSE
return(TRUE)
})
adddropsource(table) #, handler = defHandlerSource)
#defHandlerSource = function(h,...) print("source")
adddropmotion(g, handler=function(h, ...) {
print("motion")
})
adddroptarget(g, targetType="object", handler = defHandlerTarget, g)
defHandlerTarget = function(h, ...){
print(h$dropdata)
}
–––––––––––––––––––––––––––––––––––––––
Mark Heckmann
Dipl. Wirt.-Ing. cand. Psych.
Vorstraße 93 B01
28359 Bremen
Blog: www.markheckmann.de
R-Blog: http://ryouready.wordpress.com
More information about the R-help
mailing list