KeyboardProcessor.st
author Claus Gittinger <cg@exept.de>
Thu, 07 Oct 1999 13:13:13 +0200
changeset 1251 2b4223c2e092
parent 1160 4bd4457b1d90
child 1310 6ea1bca9654c
permissions -rw-r--r--
added global accelerator

"
 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.
"





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 methodsFor:'ST80 - mimicri'!

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 topView app view action|

    view := event view.

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

        "/ how about global accelerators ?
        (globalAccelerators notNil
        and:[(action := globalAccelerators at:key ifAbsent:nil) notNil])
        ifTrue:[
            action isSymbol 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
        ].

        topView isModal ifTrue:[
            topView windowGroup explicitFocusView isNil ifTrue:[
                (returnIsOKInDialog and:[key == #Return]) ifTrue:[
                    self requestGlobalAutoAccept ifFalse:[^ true].
                    app doAccept.
                    ^ true
                ].
            ].

            (escapeIsCancelInDialog and:[key == #Escape]) ifTrue:[
                app doCancel.
                ^ true
            ].
        ].

        "/ how about menu-shortkeys ?
        (menuBar notNil and:[(menuBar processShortcutKeyEventInMenuBar:event)]) ifTrue:[
            ^ true
        ]
    ].

    ^ false

    "/ let view dispatch it.



!

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
    ].
    ^ false

! !

!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.9 1999-10-07 11:13:13 cg Exp $'
! !