PointFromUserController.st
author Claus Gittinger <cg@exept.de>
Thu, 09 Nov 2017 20:09:30 +0100
changeset 6225 0122e4e6c587
parent 6143 5324a89d3400
permissions -rw-r--r--
#FEATURE by cg class: GenericToolbarIconLibrary class added: #hideFilter16x16Icon

"
 COPYRIGHT (c) 2017 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:libwidg' }"

"{ NameSpace: Smalltalk }"

ButtonController subclass:#PointFromUserController
	instanceVariableNames:'action pressPoint'
	classVariableNames:''
	poolDictionaries:''
	category:'Interface-Support-Controllers'
!

!PointFromUserController class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 2017 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
"
    drags a rectangle.

    An instance of me can be installed temporarily as controller of any view,
    to let the user select a rectangular area from the view.

    Use the utility method:
        dragRectangleIn:aView thenDo:action
    which does exactly that for your (and cares to restore any original controller)    
    
"
!

examples
"
    |v c|

    v := View new openAndWait.
    c := PointFromuserController new.
    c action:[:point | Transcript showCR:point ].
    v openAndWait.
    
    v controller:c.
"
! !

!PointFromUserController class methodsFor:'utilities'!

pointFromUserIn:aView thenDo:action
    "wait for a button press in aView"
    
    |pickController oldController oldCursor|
    
    pickController := self new.
    pickController view:aView.
    oldController := aView controller.
    oldCursor := aView cursor.

    aView cursor:Cursor crossHair.
    pickController action:[:pointOrNil |
        |image|

        aView controller:oldController.
        aView cursor:oldCursor.
        action value:pointOrNil.
    ].
    
    aView controller:pickController.

    "Created: / 07-03-2017 / 18:29:28 / cg"
! !

!PointFromUserController methodsFor:'accessing'!

action:aBlock
    "set the block which will be called when the point has been picked.
     The block will be called with a nil arg, if escape is pressed"
     
    action := aBlock

    "Modified (comment): / 07-03-2017 / 18:26:34 / cg"
!

pressPoint
    ^ pressPoint

    "Created: / 07-03-2017 / 18:28:36 / cg"
! !

!PointFromUserController methodsFor:'event handling'!

buttonPress:button x:x y:y
    pressPoint := x@y.
    view cursor:(Cursor normal).

    "Modified: / 07-03-2017 / 18:27:46 / cg"
!

buttonRelease:button x:x y:y
    |point|
    
    pressPoint isNil ifTrue:[
        super buttonRelease:button x:x y:y.
        ^ self
    ].
    
    point := pressPoint.
    pressPoint := nil.
    action value:point.

    "Modified: / 07-03-2017 / 18:28:24 / cg"
    "Modified (format): / 10-04-2017 / 08:41:13 / cg"
!

keyPress:key x:x y:y
    "actually, this is for the escape key (to abort a drag operation)"

    pressPoint isNil ifTrue:[
        super keyPress:key x:x y:y.
        ^ self
    ].

    pressPoint := nil.
    action value:nil.

    "Modified: / 07-03-2017 / 18:27:19 / cg"
    "Modified (comment): / 10-04-2017 / 08:41:08 / cg"
! !

!PointFromUserController class methodsFor:'documentation'!

version
    ^ '$Header$'
!

version_CVS
    ^ '$Header$'
! !