PointFromUserController.st
changeset 6121 7fb5e8886804
child 6143 5324a89d3400
equal deleted inserted replaced
6120:37438a85b476 6121:7fb5e8886804
       
     1 "
       
     2  COPYRIGHT (c) 2017 by eXept Software AG
       
     3               All Rights Reserved
       
     4 
       
     5  This software is furnished under a license and may be used
       
     6  only in accordance with the terms of that license and with the
       
     7  inclusion of the above copyright notice.   This software may not
       
     8  be provided or otherwise made available to, or used by, any
       
     9  other person.  No title to or ownership of the software is
       
    10  hereby transferred.
       
    11 "
       
    12 "{ Package: 'stx:libwidg' }"
       
    13 
       
    14 "{ NameSpace: Smalltalk }"
       
    15 
       
    16 ButtonController subclass:#PointFromUserController
       
    17 	instanceVariableNames:'action pressPoint'
       
    18 	classVariableNames:''
       
    19 	poolDictionaries:''
       
    20 	category:'Interface-Support-Controllers'
       
    21 !
       
    22 
       
    23 !PointFromUserController class methodsFor:'documentation'!
       
    24 
       
    25 copyright
       
    26 "
       
    27  COPYRIGHT (c) 2017 by eXept Software AG
       
    28               All Rights Reserved
       
    29 
       
    30  This software is furnished under a license and may be used
       
    31  only in accordance with the terms of that license and with the
       
    32  inclusion of the above copyright notice.   This software may not
       
    33  be provided or otherwise made available to, or used by, any
       
    34  other person.  No title to or ownership of the software is
       
    35  hereby transferred.
       
    36 "
       
    37 !
       
    38 
       
    39 documentation
       
    40 "
       
    41     drags a rectangle.
       
    42 
       
    43     An instance of me can be installed temporarily as controller of any view,
       
    44     to let the user select a rectangular area from the view.
       
    45 
       
    46     Use the utility method:
       
    47         dragRectangleIn:aView thenDo:action
       
    48     which does exactly that for your (and cares to restore any original controller)    
       
    49     
       
    50 "
       
    51 !
       
    52 
       
    53 examples
       
    54 "
       
    55     |v c|
       
    56 
       
    57     v := View new openAndWait.
       
    58     c := PointFromuserController new.
       
    59     c action:[:point | Transcript showCR:point ].
       
    60     v openAndWait.
       
    61     
       
    62     v controller:c.
       
    63 "
       
    64 ! !
       
    65 
       
    66 !PointFromUserController class methodsFor:'utilities'!
       
    67 
       
    68 pointFromUserIn:aView thenDo:action
       
    69     "wait for a button press in aView"
       
    70     
       
    71     |pickController oldController oldCursor|
       
    72     
       
    73     pickController := self new.
       
    74     pickController view:aView.
       
    75     oldController := aView controller.
       
    76     oldCursor := aView cursor.
       
    77 
       
    78     aView cursor:Cursor crossHair.
       
    79     pickController action:[:pointOrNil |
       
    80         |image|
       
    81 
       
    82         aView controller:oldController.
       
    83         aView cursor:oldCursor.
       
    84         action value:pointOrNil.
       
    85     ].
       
    86     
       
    87     aView controller:pickController.
       
    88 
       
    89     "Created: / 07-03-2017 / 18:29:28 / cg"
       
    90 ! !
       
    91 
       
    92 !PointFromUserController methodsFor:'accessing'!
       
    93 
       
    94 action:aBlock
       
    95     "set the block which will be called when the point has been picked.
       
    96      The block will be called with a nil arg, if escape is pressed"
       
    97      
       
    98     action := aBlock
       
    99 
       
   100     "Modified (comment): / 07-03-2017 / 18:26:34 / cg"
       
   101 !
       
   102 
       
   103 pressPoint
       
   104     ^ pressPoint
       
   105 
       
   106     "Created: / 07-03-2017 / 18:28:36 / cg"
       
   107 ! !
       
   108 
       
   109 !PointFromUserController methodsFor:'event handling'!
       
   110 
       
   111 buttonPress:button x:x y:y
       
   112     pressPoint := x@y.
       
   113     view cursor:(Cursor normal).
       
   114 
       
   115     "Modified: / 07-03-2017 / 18:27:46 / cg"
       
   116 !
       
   117 
       
   118 buttonRelease:button x:x y:y
       
   119     |point|
       
   120     
       
   121     pressPoint isNil ifTrue:[
       
   122         super buttonRelease:button x:x y:y.
       
   123         ^ self
       
   124     ].
       
   125     point := pressPoint.
       
   126     pressPoint := nil.
       
   127     action value:point.
       
   128 
       
   129     "Modified: / 07-03-2017 / 18:28:24 / cg"
       
   130 !
       
   131 
       
   132 keyPress:key x:x y:y
       
   133     pressPoint isNil ifTrue:[
       
   134         super keyPress:key x:x y:y.
       
   135         ^ self
       
   136     ].
       
   137 
       
   138     pressPoint := nil.
       
   139     action value:nil.
       
   140 
       
   141     "Modified: / 07-03-2017 / 18:27:19 / cg"
       
   142 ! !
       
   143 
       
   144 !PointFromUserController class methodsFor:'documentation'!
       
   145 
       
   146 version
       
   147     ^ '$Header$'
       
   148 !
       
   149 
       
   150 version_CVS
       
   151     ^ '$Header$'
       
   152 ! !
       
   153