RectanglefromUserController.st
changeset 6119 ad406347b834
child 6120 37438a85b476
equal deleted inserted replaced
6118:6cba72e13c30 6119:ad406347b834
       
     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:#RectangleFromUserController
       
    17 	instanceVariableNames:'action startPoint lastX lastY'
       
    18 	classVariableNames:''
       
    19 	poolDictionaries:''
       
    20 	category:'Interface-Support-Controllers'
       
    21 !
       
    22 
       
    23 !RectangleFromUserController 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 := DragRectangleController new.
       
    59     c action:[:rect | Transcript showCR:rect ].
       
    60     v openAndWait.
       
    61     
       
    62     v controller:c.
       
    63 "
       
    64 ! !
       
    65 
       
    66 !RectangleFromUserController class methodsFor:'utilities'!
       
    67 
       
    68 dragRectangleIn:aView thenDo:action
       
    69     "drag a rectangle in aView"
       
    70     
       
    71     |dragController oldController oldCursor|
       
    72     
       
    73     dragController := DragRectangleController new.
       
    74     dragController view:aView.
       
    75     oldController := aView controller.
       
    76     oldCursor := aView cursor.
       
    77 
       
    78     aView cursor:Cursor origin.
       
    79     dragController action:[:rectOrNil |
       
    80         |image|
       
    81 
       
    82         aView controller:oldController.
       
    83         aView cursor:oldCursor.
       
    84         action value:rectOrNil.
       
    85     ].
       
    86     
       
    87     aView controller:dragController.
       
    88 ! !
       
    89 
       
    90 !RectangleFromUserController methodsFor:'accessing'!
       
    91 
       
    92 action:aBlock
       
    93     "set the block which will be called when the rectangle drag is finished.
       
    94      The block will be called with a nil arg, if escape is pressed"
       
    95      
       
    96     action := aBlock
       
    97 !
       
    98 
       
    99 lastMousePoint
       
   100     lastX isNil ifTrue:[^ nil].
       
   101     ^ lastX @ lastY
       
   102 !
       
   103 
       
   104 startPoint
       
   105     ^ startPoint
       
   106 ! !
       
   107 
       
   108 !RectangleFromUserController methodsFor:'event handling'!
       
   109 
       
   110 buttonMotion:buttonState x:x y:y
       
   111     (buttonState == 0 or:[startPoint isNil]) ifTrue:[
       
   112         super buttonMotion:buttonState x:x y:y.
       
   113         ^ self
       
   114     ].
       
   115     
       
   116     lastX notNil ifTrue:[
       
   117         view xoring:[
       
   118             view displayRectangle:(startPoint corner:(lastX@lastY))
       
   119         ].
       
   120     ].
       
   121     
       
   122     lastX := x.
       
   123     lastY := y.
       
   124 
       
   125     startPoint ~= (x@y) ifTrue:[
       
   126         view xoring:[
       
   127             view displayRectangle:(startPoint corner:(lastX@lastY))
       
   128         ].
       
   129     ].
       
   130 !
       
   131 
       
   132 buttonPress:button x:x y:y
       
   133     startPoint := x@y.
       
   134     view cursor:(Cursor corner).
       
   135 !
       
   136 
       
   137 buttonRelease:button x:x y:y
       
   138     |rect|
       
   139     
       
   140     startPoint isNil ifTrue:[
       
   141         super buttonRelease:button x:x y:y.
       
   142         ^ self
       
   143     ].
       
   144     (lastX isNil or:[lastY isNil]) ifTrue:[
       
   145         lastX := x.
       
   146         lastY := y.
       
   147     ].
       
   148     
       
   149     view xoring:[
       
   150         view displayRectangle:(startPoint corner:(lastX@lastY))
       
   151     ].
       
   152     rect := startPoint corner:(lastX@lastY).
       
   153     startPoint := lastX := lastY := nil.
       
   154     action value:rect.
       
   155 
       
   156     "Modified: / 09-02-2017 / 23:18:01 / cg"
       
   157 !
       
   158 
       
   159 keyPress:key x:x y:y
       
   160     startPoint isNil ifTrue:[
       
   161         super keyPress:key x:x y:y.
       
   162         ^ self
       
   163     ].
       
   164 
       
   165     lastX notNil ifTrue:[
       
   166         view xoring:[
       
   167             view displayRectangle:(startPoint corner:(lastX@lastY))
       
   168         ].
       
   169     ].
       
   170 
       
   171     startPoint := nil.
       
   172     action value:nil.
       
   173 ! !
       
   174 
       
   175 !RectangleFromUserController class methodsFor:'documentation'!
       
   176 
       
   177 version
       
   178     ^ '$Header$'
       
   179 !
       
   180 
       
   181 version_CVS
       
   182     ^ '$Header$'
       
   183 ! !
       
   184