KeyboardProcessor.st
author ca
Thu, 17 Oct 2002 18:58:19 +0200
changeset 1636 c9ed206b5d32
parent 1635 f65f19dc255a
child 1637 5745a22735c8
permissions -rw-r--r--
escapeIsCancel/returnIsOK in popup with same app as mainapp (escape in ExtendedComboBox of NewSystemBrowser)

"
 COPYRIGHT (c) 1997 by eXept Software AG
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"





"{ Package: 'stx:libview2' }"

Object subclass:#KeyboardProcessor
	instanceVariableNames:'returnIsOKInDialog escapeIsCancelInDialog menuBar
		autoAcceptListeners globalAccelerators'
	classVariableNames:''
	poolDictionaries:''
	category:'Interface-Support-UI'
!

!KeyboardProcessor class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1997 by eXept Software AG
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"




!

documentation
"
    ST80 compatibility (mimicry) class.

    The class is not completed yet and certainly not bug free.

    Notice: 
        this class was implemented using protocol information
        from alpha testers, literature and by reading public domain code
        - it may not be complete or compatible to
        the corresponding ST-80 class. 
        If you encounter any incompatibilities, please forward a note 
        describing the incompatibility verbal (i.e. no code) to the ST/X team.

    KeyboardProcessor is going to take over the focus control
    mechanism (which are currently located in the windowGroup).
    Currently, it keeps track of inputFields, and allows for
    a global accept to be forced.
    This is especially useful with dialogs, where a global accept
    should be performed on all inputFields, when the OK button
    is pressed.

    [author:]
        Claus Gittinger
"




! !

!KeyboardProcessor class methodsFor:'defaults'!

isKeyEventIgnoredAsShortcut:aKeyEvent
    "returns true if the key event ignored is a shortCut key
    "
    |rawKey|

    rawKey := aKeyEvent rawKey.
    rawKey isSymbol ifFalse:[^ true].
"/    "/ ignore cursor keys
"/    (#(
"/        Up Down Left Right 
"/        CursorUp CursorDown CursorLeft CursorRight
"/    ) includes:rawKey) ifTrue:[^ true].

    "/ ignore some meta keys
    (#( 
        #'Control_L' #'Control_R' #'Control' #'Ctrl' 
        #'CmdMenu' #'BackSpace' #Tab
        #'Cmd_L' #'Cmd_R' #'Cmd'
        #'Shift_L' #'Shift_R' Shift 
        #'Alt_L' #'Alt_R' #'Alt'
        #'Super_R' #'Super_L' #'Super'
    ) includes:aKeyEvent key) ifTrue:[^ true].

    ^ false

    "Created: / 17.1.2001 / 11:57:07 / cg"
    "Modified: / 17.1.2001 / 13:05:46 / cg"
!

isMnemonicKeyEvent:aKeyEvent
    "returns true if the key event is a mnemonic key
    "
    |rawKey|

    rawKey := aKeyEvent rawKey.
  ^ rawKey size == 4 and:[rawKey startsWith:'Cmd']
! !

!KeyboardProcessor methodsFor:'Compatibility - VW'!

keyboardConsumers
    ^ #()

    "Created: 6.3.1997 / 15:16:32 / cg"
!

removeKeyboardReceiver:aController

    "Created: / 31.10.1997 / 01:56:54 / cg"
!

sendKeyboardTo:aController

    "Created: / 31.10.1997 / 01:57:15 / cg"
!

setActive:aWidget

    "Created: 3.3.1997 / 18:31:09 / cg"
! !

!KeyboardProcessor methodsFor:'accessing'!

addAccelerator:aKey action:aSelectorOrBlock
    "add a global accelerator - these are handled even if no corresponding
     menu shortcut is defined"

    globalAccelerators isNil ifTrue:[
        globalAccelerators := Dictionary new
    ].
    globalAccelerators at:aKey put:aSelectorOrBlock
!

escapeIsCancelInDialog:aBoolean
    "set the escapeIsCancel flag.
     If off, Escape is NOT handled as cancel (the builder defaults it to true,)"

    escapeIsCancelInDialog := aBoolean
!

menuBar
    "return the value of the instance variable 'menuBar' (automatically generated)"

    ^ menuBar
!

menuBar:something
    "set the value of the instance variable 'menuBar' (automatically generated)"

    menuBar := something.
!

returnIsOKInDialog:aBoolean
    "set the returnIsOK flag.
     If off, Return is NOT handled as accept (the builder defaults it to true,)"

    returnIsOKInDialog := aBoolean
! !

!KeyboardProcessor methodsFor:'event handling'!

processEvent:event forModalView:modalTopOrNil
    "process a key-event; return true, if handled & eaten; false if not.
     Here, first, we look for a globalAccelerator,
     then for Return and Escape in modal applications,
     (which lead to Accept & Cancel resp.)
     Finally, menu-shortcuts are handled."

    |key rawKey topView wg app view action focusView|

    view := event view.

    event isKeyPressEvent ifTrue:[
        key := event key.
        rawKey := event rawKey.
        topView := modalTopOrNil ? view topView.

        wg := topView windowGroup.
        app := topView application.

        "/ how about global accelerators ?
        (globalAccelerators notNil
        and:[(action := globalAccelerators at:key ifAbsent:nil) notNil])
        ifTrue:[
            event consumed:true.
            action isSymbol ifTrue:[
                app notNil ifTrue:[
                    action numArgs == 1 ifTrue:[
                        app perform:action with:event
                    ] ifFalse:[
                        app perform:action
                    ].
                ]
            ] ifFalse:[
                action numArgs == 1 ifTrue:[
                    action value:event
                ] ifFalse:[
                    action value
                ]
            ].
            "/ ^ true
            ^ event consumed
        ].

        topView isModal ifTrue:[
            (wg notNil 
            and:[(focusView := wg explicitFocusView) isNil
                  or:[ focusView isKeyboardConsumer not 
                      or:[ focusView isInputField ] ]
            ])  ifTrue:[
                ((returnIsOKInDialog ? true) and:[key == #Return]) ifTrue:[
                    self requestGlobalAutoAccept ifFalse:[^ true].

                    "/ only care for RETURN and ESC if the window is the
                    "/ apps topView (not if its a popup or dialog of it)
                    (app notNil and:[app window == topView]) ifTrue:[
                        app doAccept.
                    ] ifFalse:[
                        "/ oldStyle modalBox - for now, let Box handle it itself
                        ^ false
                    ].
                    ^ true
                ].
            ].

            ((escapeIsCancelInDialog ? true) and:[key == #Escape]) ifTrue:[
                "/ only care for RETURN and ESC if the window is the
                "/ apps topView (not if its a popup or dialog of it)
                (app notNil and:[app window == topView]) ifTrue:[
                    app doCancel.
                ] ifFalse:[
                    "/ oldStyle modalBox - for now, let Box handle it itself
                    ^ false
                ].
                ^ true
            ].
        ].

        wg notNil ifTrue:[
            (self class isMnemonicKeyEvent:event) ifTrue:[
"/Transcript showCR:event rawKey.
"/Transcript showCR:event key.
                (wg processMnemonic:event) ifTrue:[
                    ^ true
                ]
            ].
            (self class isKeyEventIgnoredAsShortcut:event) ifFalse:[
"/Transcript showCR:event rawKey.
"/Transcript showCR:event key.
                (wg processShortcut:event) ifTrue:[
                    ^ true
                ].
            ].
        ].        
    ].

    "/ let view dispatch it.
    ^ false

    "Modified: / 23.12.2001 / 13:12:55 / cg"
!

requestForWindowClose
     "about to close the window."

     ^ true
!

requestGlobalAutoAccept
     "about to close the window via return ok accept.
      Ask all acceptListeners to accept their value and return true, if all of those fields
      did."

    autoAcceptListeners notNil ifTrue:[
        autoAcceptListeners do:[:aListener |
            (aListener requestAutoAccept) ifFalse:[^ false].
        ].
        ^ true
    ].
    "/ if no input field (or comparable widget) is present ...
"/    ^ false     <- wrong.
    ^ true.
! !

!KeyboardProcessor methodsFor:'setup'!

addAutoAcceptListener:aListener
    "add a aListener to my autoAcceptListeners.
     Typically, inputFields add themself, to be notified (via requestForAutoAccept)
     when the dialog is about to be closed with returnIsOK or accept.
     (of course, other listeners are also invited ;-)"

    autoAcceptListeners isNil ifTrue:[
        autoAcceptListeners := IdentitySet new.
    ].
    autoAcceptListeners add:aListener
! !

!KeyboardProcessor class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libview2/KeyboardProcessor.st,v 1.29 2002-10-17 16:58:19 ca Exp $'
! !