Tuesday, January 20, 2015

Cancellation of a 'Recent Items' Pull-Down Menu

Its been some time since support for 'recent file' operations was first added to the Gnocl sources. At the time I was quite pleased with its inclusion but there was always a annoying snag to it, something which is an inhereted feature from the API: if no selection if made, then the last file URI added to the list is always returned. This is not really a sign of anything lacking in the GTK sources, but the documentation does not suggest how to handle any need to CANCEL the menu by pressing the 'ESC' key.
 A quick gloss over the docs shows that there is a 'cancel' signal for the MENU-SHELL containing the recent items, but this automatically generates a 'selection-done' signal, the very signal that we want to avoid. The obvious solution for this is to set a boolean cancel flag and test for its TRUE or FALSE state during the execution of any 'selection-done' event handler.

For a TCL side solution it was first was necessary to implement a gnocl -onCancel option. So, this could added to any test script:

set cancel 0
set rc_menu [gnocl::menuRecentChooser \
-onCancel {  set cancel 1 } \
-onClicked {
     if { !$cancel } {
        set fname [string range %f 5 end]
        openFile $fname
    set cancel 0

Works nicely, but it's better if this becomes invisible to the scripter.This of course, is not a problem to implement in C and so the relevant handlers have been modified to offer the benefit of this functionality.