Mark Heckmann
2009-Dec-22 01:42 UTC
[R] RGtk2 - retrieve ggraphics mouse coordinates during drag-and-drop event
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
john verzani
2009-Dec-28 03:17 UTC
[R] RGtk2 - retrieve ggraphics mouse coordinates during drag-and-drop event
Mark Heckmann <mark.heckmann <at> gmx.de> writes:> > 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? >The handler has h$x and h$y components to do this. In your code (which I clipped to save space) you might look at: defHandlerTarget = function(h, ...){ theArgs <- list(...) w <- theArgs[[1]] allocation = w$GetAllocation() ## h$x, h$y give x,y coords for g x = h$x/allocation$width y = 1 - (allocation$height - h$y)/allocation$height print(c(x,y)) } If that doesn't help, let me know.> Below you find my sample code (windows only due to ggraphics). >(The ggraphics widget works with linux and mac too, although the mac can have issues with some installations.)> TIA, > Mark >..snip..> ??????????????????????????????????????? > Mark Heckmann > Dipl. Wirt.-Ing. cand. Psych. > Vorstra?e 93 B01 > 28359 Bremen > Blog: www.markheckmann.de > R-Blog: http://ryouready.wordpress.com > >