jverzani / gWidgets2

Rewrite of gWidgets
35 stars 9 forks source link

addHandler fails to return ID #64

Closed landroni closed 10 years ago

landroni commented 10 years ago

It seems that addHandler always fails to return the ID and instead returns NULL (even after https://github.com/jverzani/gWidgets2/commit/0bbeb16676cddaac2d8c320fc3da2f8c8258ca0a and https://github.com/jverzani/gWidgets2RGtk2/commit/f6183e7bb4f695241377a39f7e743b66e373af2e ).

w <- gwindow("notebook example", visible=T)
nb <- gnotebook(container=w)
gbutton("Page one", label="tab 1", container=nb) ## note label argument
h_esc <- addHandlerKeystroke(w, function(h, ...){
    if(h$key=="\033") dispose(w)
})

Then :

> print(addHandler(w))
NULL

This behaviour doesn't allow to block the h_esc handler.

Originally discussed at http://stackoverflow.com/questions/24711308/how-to-bind-esc-key-to-close-a-gwindow-in-gwidgets/24716837?noredirect=1#comment38432017_24716837 .

jverzani commented 10 years ago

Hmm, this works with my ubuntu set up. It also required updating gWidgets2, as the observer code was the issue. Did you do that, or is this a platform inconsistency?

landroni commented 10 years ago

Hmm, well here's exactly what I did. In a new R session:

> install_github('gWidgets2', 'jverzani')
Installing github repo gWidgets2/master from jverzani
Downloading master.zip from https://github.com/jverzani/gWidgets2/archive/master.zip
Installing package from /tmp/RtmpDvbOu6/master.zip
arguments 'minimized' and 'invisible' are for Windows only
Installing gWidgets2
'/usr/lib/R/bin/R' --vanilla CMD INSTALL  \
'/tmp/RtmpDvbOu6/devtools616f7d64bd2f/gWidgets2-master'  \
--library='/home/geek/R/x86_64-pc-linux-gnu-library/3.0' --install-tests 

* installing *source* package 'gWidgets2' ...
** R
** inst
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** installing vignettes
** testing if installed package can be loaded
* DONE (gWidgets2)
> install_github('gWidgets2RGtk2', 'jverzani')
Installing github repo gWidgets2RGtk2/master from jverzani
Downloading master.zip from https://github.com/jverzani/gWidgets2RGtk2/archive/master.zip
Installing package from /tmp/RtmpDvbOu6/master.zip
arguments 'minimized' and 'invisible' are for Windows only
Installing gWidgets2RGtk2
'/usr/lib/R/bin/R' --vanilla CMD INSTALL  \
'/tmp/RtmpDvbOu6/devtools616f79463134/gWidgets2RGtk2-master'  \
--library='/home/geek/R/x86_64-pc-linux-gnu-library/3.0' --install-tests 

* installing *source* package 'gWidgets2RGtk2' ...
** R
** demo
** inst
** tests
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
* DONE (gWidgets2RGtk2)
> library("gWidgets2RGtk2", lib.loc="/home/geek/R/x86_64-pc-linux-gnu-library/3.0")
Loading required package: memoise
Loading required package: RGtk2
Loading required package: gWidgets2
Loading required package: digest
> w <- gwindow("notebook example", visible=T)
> nb <- gnotebook(container=w)
> gbutton("Page one", label="tab 1", container=nb) ## note label argument
Object of class GButton
> h_esc <- addHandlerKeystroke(w, function(h, ...){
    +     if(h$key=="\033") dispose(w)
    + })
> print(addHandler(w))
NULL 

I'm also using Ubuntu Linux 14.04 Trusty:

> sessionInfo()
R version 3.0.2 (2013-09-25)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
    [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8       
[4] LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
[7] LC_PAPER=en_US.UTF-8       LC_NAME=C                  LC_ADDRESS=C              
[10] LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
    [1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
    [1] gWidgets2RGtk2_1.0-3 gWidgets2_1.0-6      digest_0.6.4         RGtk2_2.20.29       
[5] memoise_0.2.1        devtools_1.5        

loaded via a namespace (and not attached):
    [1] evaluate_0.5.5 httr_0.3       parallel_3.0.2 RCurl_1.95-4.1 stringr_0.6.2  tools_3.0.2   
[7] whisker_0.3-2 

Hmm, I would be surprised if it were platform inconsistency. Is it possible that some local changes in your tree failed to make their way to github?

jverzani commented 10 years ago

I thought the change was gWidgets2, but it was here: https://github.com/jverzani/gWidgets2RGtk2/commit/f6183e7bb4f695241377a39f7e743b66e373af2e

This is what I see.

> w <- gwindow()
> hesc <- addHandlerKeystroke(w, function(h,...) print(h))
> print(hesc)
$signal
[1] "key-release-event"

$o
Reference class object of class "Handler"
Field "o":
function(h,...) print(h)
Field "obj":
Object of class GWindow
Field "action":
NULL

In the code I check to see if there is a handler (so addHandler(w) won't return anything). Does your h_esc print empty as well?

landroni commented 10 years ago

OK, please disregard my last comment and let me try again. The code that you used just above works just fine here:

> w <- gwindow()
> hesc <- addHandlerKeystroke(w, function(h,...) print(h))
> print(hesc)
$signal
[1] "key-release-event"

$o
Reference class object of class "Handler"
Field "o":
    function(h,...) print(h)
Field "obj":
    Object of class GWindow
Field "action":
    NULL

$obj
Object of class GWindow

$action
NULL

$key
[1] "\r"

$modifier
[1] "mod2"

$state
[1] 16

$event
<pointer: 0x99c9350>
    attr(,"class")
[1] "GdkEventKey" "GdkEventAny" "GdkEvent"    "RGtkObject" 

However, I'm actually after this:

ID  returned by addHandler. If missing will try to block all handler passed to constructor

So I want to obtain the ID that should be the value of addHandler. But this is what I see instead:

> print(addHandler(hesc))
Error in addHandler.default(hesc) : attempt to apply non-function
> print(addHandler(w))
NULL

So how do I get the ID from hesc? Or if I rephrase that, what is an ID in this context?

landroni commented 10 years ago

OK, I'm making progress here. So it seems that hesc itself is the ID here.

But consider the following:

w <- gwindow()
hesc <- addHandlerKeystroke(w, function(h,...) print(h$event))
addHandlerBlur(w, function(h, ...){
    blockHandler(w, hesc)
})

Now right-click on the WM title bar and you'll see this error:

Error in block_observer(ID) : object 'o' not found

And the hesc handler won't work anymore. Is this error expected?

jverzani commented 10 years ago

There are some errors here. Thanks for digging them up. In this case I need to reference o within id. I'll push that change in a minute.

On Sun, Jul 27, 2014 at 11:02 AM, landroni notifications@github.com wrote:

OK, I'm making progress here. So it seems that hesc itself is the ID here.

But consider the following:

w <- gwindow() hesc <- addHandlerKeystroke(w, function(h,...) print(h$event)) addHandlerBlur(w, function(h, ...){ blockHandler(w, hesc) })

Now right-click on the WM title bar and you'll this error:

Error in block_observer(ID) : object 'o' not found

And the hesc handler won't work anymore. Is this error expected?

— Reply to this email directly or view it on GitHub https://github.com/jverzani/gWidgets2/issues/64#issuecomment-50266625.

John Verzani Chair, Department of Mathematics College of Staten Island, CUNY verzani@math.csi.cuny.edu

jverzani commented 10 years ago

In general, this is the pattern that should work:

id <- addHandlerChanged(obj, handler=function(h,...) {})
blockHandler(obj, id)
unblockHandler(obj, id)

Or, if you want to block all handlers, it is simpler:

blockHandlers(obj)
unblockHandlers(obj)

If that doesn't seem to work, it is definitely a bug.

On Sun, Jul 27, 2014 at 11:12 AM, John Verzani jverzani@gmail.com wrote:

There are some errors here. Thanks for digging them up. In this case I need to reference o within id. I'll push that change in a minute.

On Sun, Jul 27, 2014 at 11:02 AM, landroni notifications@github.com wrote:

OK, I'm making progress here. So it seems that hesc itself is the ID here.

But consider the following:

w <- gwindow() hesc <- addHandlerKeystroke(w, function(h,...) print(h$event)) addHandlerBlur(w, function(h, ...){ blockHandler(w, hesc) })

Now right-click on the WM title bar and you'll this error:

Error in block_observer(ID) : object 'o' not found

And the hesc handler won't work anymore. Is this error expected?

— Reply to this email directly or view it on GitHub https://github.com/jverzani/gWidgets2/issues/64#issuecomment-50266625.

John Verzani Chair, Department of Mathematics College of Staten Island, CUNY verzani@math.csi.cuny.edu

John Verzani Chair, Department of Mathematics College of Staten Island, CUNY verzani@math.csi.cuny.edu

landroni commented 10 years ago

I've installed latest GIT, and now it seems to work and I no longer see the o error.

Would you consider adding the blockHandler and blockHandlers examples above in the docs? That should help avoid any such user confusion. An example involving unblocking on.exit would be nice, too.

jverzani commented 10 years ago

I added something to the example for gbutton. Not sure if that is easy to find. The methods blockHandler are documented, though that could be cleaned up -- a lot. I also ran roxygen to update the docs. Thanks for the feedback!

On Sun, Jul 27, 2014 at 11:42 AM, landroni notifications@github.com wrote:

I've installed latest GIT, and now it seems to work and I no longer see the o error.

Would you consider adding the blockHandler and blockHandlers examples above in the docs? That should help avoid any such user confusion. An example involving unblocking on.exit would be nice, too.

— Reply to this email directly or view it on GitHub https://github.com/jverzani/gWidgets2/issues/64#issuecomment-50273139.

John Verzani Chair, Department of Mathematics College of Staten Island, CUNY verzani@math.csi.cuny.edu

landroni commented 10 years ago

Looks nice.