KeyboardProcessor.st
author Claus Gittinger <cg@exept.de>
Thu, 18 Jan 2001 16:39:12 +0100
changeset 1458 43c22152a2f5
parent 1457 59014aba97fe
child 1488 9fa3505d2d85
permissions -rw-r--r--
accept autoAccept (RETURN) if no input field widget is present

"
 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
        Shift
    ) includes:rawKey) ifTrue:[^ true].

    "/ ignore some meta keys
    (#( 
        #'Control_L' #'Control_R' #'Control' #'Ctrl' #'CmdMenu' #'BackSpace' 
    ) 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 - ST80'!

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 (however, the default is 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 (however, the default is 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|

    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:[
            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
        ].
        app notNil ifTrue:[
            topView isModal ifTrue:[
                (wg notNil and:[wg explicitFocusView isNil]) ifTrue:[
                    (returnIsOKInDialog and:[key == #Return]) ifTrue:[
                        self requestGlobalAutoAccept ifFalse:[^ true].
                        app doAccept.
                        ^ true
                    ].
                ].

                (escapeIsCancelInDialog and:[key == #Escape]) ifTrue:[
                    app doCancel.
                    ^ 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: / 17.1.2001 / 12:07:45 / 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.21 2001-01-18 15:39:12 cg Exp $'
! !