EnterBox.st
changeset 0 e6a541c1c0eb
child 3 9d7eefb5e69f
equal deleted inserted replaced
-1:000000000000 0:e6a541c1c0eb
       
     1 "
       
     2  COPYRIGHT (c) 1990-93 by Claus Gittinger
       
     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 
       
    13 ModalBox subclass:#EnterBox
       
    14        instanceVariableNames:'labelField enterField buttonPanel
       
    15                               okButton abortButton
       
    16                               okAction abortAction'
       
    17        classVariableNames:''
       
    18        poolDictionaries:''
       
    19        category:'Views-Interactors'
       
    20 !
       
    21 
       
    22 EnterBox comment:'
       
    23 
       
    24 COPYRIGHT (c) 1990-93 by Claus Gittinger
       
    25               All Rights Reserved
       
    26 
       
    27 this class implements a pop-up box to enter some string
       
    28 with 2 buttons; one to cancel, another to start some action
       
    29 
       
    30 %W% %E%
       
    31 
       
    32 written Feb 90 by claus
       
    33 '!
       
    34 
       
    35 !EnterBox class methodsFor:'defaults'!
       
    36 
       
    37 defaultExtent
       
    38     ^ (Display pixelPerMillimeter * (60 @ 30)) rounded
       
    39 ! !
       
    40 
       
    41 !EnterBox class methodsFor:'instance creation'!
       
    42 
       
    43 action:aBlock
       
    44     "create and return a new EnterBox 
       
    45      which will evaluate aBlock when 'ok' is pressed"
       
    46 
       
    47     ^ (self new) action:aBlock
       
    48 
       
    49     "(EnterBox action:[:string | Transcript showCr:string]) showAtPointer"
       
    50 !
       
    51 
       
    52 title:titleString action:aBlock
       
    53     "create and return a new EnterBox with title aString,
       
    54      which will evaluate aBlock when 'ok' is pressed"
       
    55 
       
    56     ^ ((self new) title:titleString) action:aBlock
       
    57 !
       
    58 
       
    59 title:titleString okText:okText abortText:abortText action:aBlock
       
    60     "create and return a new EnterBox with title aString, and buttons showing
       
    61      okText and abortText; it will evaluate aBlock when 'ok' is pressed"
       
    62 
       
    63     ^ ((self new) title:titleString 
       
    64                  okText:okText 
       
    65               abortText:abortText) action:aBlock
       
    66 ! !
       
    67 
       
    68 !EnterBox methodsFor:'initialization'!
       
    69 
       
    70 initialize
       
    71     |space2 innerWidth|
       
    72 
       
    73     super initialize.
       
    74 
       
    75     space2 := 2 * ViewSpacing.
       
    76 
       
    77     labelField := Label in:self.
       
    78     labelField label:''.
       
    79     labelField borderWidth:0.
       
    80     labelField adjust:#center.
       
    81 
       
    82     "kludge: preset extent to something useful since other subviews
       
    83      depend on it (extent blocks are not evaluated until view is realized)
       
    84      - avoid visible resizing when realized the first time"
       
    85 
       
    86     innerWidth := width - space2.
       
    87 
       
    88     labelField origin:(ViewSpacing @ ViewSpacing)
       
    89                extent:(innerWidth @ labelField height).
       
    90 
       
    91     enterField := EditField in:self.
       
    92     enterField origin:(ViewSpacing @ (space2 + labelField height))
       
    93                extent:((width - space2 - (enterField borderWidth * 2)) @ enterField height).
       
    94     enterField origin:[ViewSpacing @ (space2 + labelField height)]
       
    95                extent:[(width - space2 - (enterField borderWidth * 2)) @ enterField height].
       
    96     enterField leaveAction:[:key | self okPressed].
       
    97 
       
    98     buttonPanel := HorizontalPanelView in:self.
       
    99     buttonPanel origin:(ViewSpacing @ (height - (font height * 2) - ViewSpacing - (borderWidth * 2)))
       
   100                 extent:((width - space2 - (buttonPanel borderWidth * 2)) 
       
   101                        @ ((font height * 2) + (borderWidth * 2))).
       
   102     buttonPanel origin:[ViewSpacing @ (height - (font height * 2) - ViewSpacing - (borderWidth * 2))]
       
   103                 extent:[(width - space2 - (buttonPanel borderWidth * 2)) 
       
   104                        @ ((font height * 2) + (borderWidth * 2))].
       
   105 
       
   106     buttonPanel layout:"#spread2" #right.
       
   107     buttonPanel borderWidth:0.
       
   108 
       
   109     abortButton := Button label:(Resources at:'abort')
       
   110                          action:[
       
   111                                     abortButton turnOffWithoutRedraw.
       
   112                                     self abortPressed
       
   113                                 ]
       
   114                              in:buttonPanel.
       
   115 
       
   116     okButton := Button label:(Resources at:'ok')
       
   117                       action:[
       
   118                                 okButton turnOffWithoutRedraw.
       
   119                                 self okPressed
       
   120                              ]
       
   121                           in:buttonPanel.
       
   122     okButton isReturnButton:true.
       
   123 
       
   124     self keyboardHandler:enterField
       
   125 
       
   126 !
       
   127 
       
   128 initEvents
       
   129     super initEvents.
       
   130     self enableKeyEvents
       
   131 !
       
   132 
       
   133 reAdjustGeometry
       
   134     "sent late in snapin processing - gives me a chance
       
   135      to resize for new font dimensions"
       
   136 
       
   137     super reAdjustGeometry.
       
   138     labelField resize.
       
   139     okButton resize.
       
   140     abortButton resize.
       
   141     self resize
       
   142 ! !
       
   143 
       
   144 !EnterBox methodsFor:'private'!
       
   145 
       
   146 resize
       
   147     "resize myself to make everything visible"
       
   148 
       
   149     |wWanted hWanted wPanel|
       
   150 
       
   151     wWanted := labelField widthIncludingBorder + ViewSpacing + ViewSpacing.
       
   152     (wWanted > width) ifFalse:[
       
   153         wWanted := width
       
   154     ].
       
   155     wPanel := buttonPanel preferedExtent x + ViewSpacing + ViewSpacing.
       
   156     wPanel > wWanted ifTrue:[
       
   157         wWanted := wPanel
       
   158     ].
       
   159     hWanted := ViewSpacing + labelField height +
       
   160                ViewSpacing + enterField height +
       
   161                (ViewSpacing * 6) + buttonPanel height +
       
   162                ViewSpacing.
       
   163     self extent:(wWanted @ hWanted)
       
   164 ! !
       
   165 
       
   166 !EnterBox methodsFor:'accessing'!
       
   167 
       
   168 title:aString
       
   169     "set the title to be displayed at top of enterBox"
       
   170 
       
   171     labelField label:aString.
       
   172     labelField resize.
       
   173     self resize
       
   174 !
       
   175 
       
   176 title:titleString okText:okString abortText:abortString
       
   177     "set title and texts in the buttons"
       
   178 
       
   179     self title:titleString.
       
   180     okButton label:okString.
       
   181     abortButton label:abortString
       
   182 !
       
   183 
       
   184 title:titleString okText:okString
       
   185     "set title and text in okbutton"
       
   186 
       
   187     self title:titleString.
       
   188     okButton label:okString
       
   189 !
       
   190 
       
   191 okText:aString
       
   192     "set the text to be displayed in the ok-button"
       
   193 
       
   194     okButton label:aString.
       
   195     okButton resize.
       
   196     self resize
       
   197 !
       
   198 
       
   199 abortText:aString
       
   200     "set the text to be displayed in the abort-button"
       
   201 
       
   202     abortButton label:aString.
       
   203     abortButton resize.
       
   204     self resize
       
   205 !
       
   206 
       
   207 okText:okString abortText:abortString
       
   208     "set both texts displayed in the buttons"
       
   209 
       
   210     okButton label:okString.
       
   211     abortButton label:abortString.
       
   212     okButton resize.
       
   213     abortButton resize.
       
   214     self resize
       
   215 !
       
   216 
       
   217 contents
       
   218     "return my contents"
       
   219 
       
   220     ^ enterField contents
       
   221 !
       
   222 
       
   223 initialText:aString
       
   224     "define the initial text in the enterfield. all will be selected initially"
       
   225 
       
   226     enterField initialText:aString
       
   227 !
       
   228 
       
   229 initialText:aString selectFrom:start to:stop
       
   230     "define the initial text in the enterfield, and the part to be selected"
       
   231 
       
   232     enterField initialText:aString.
       
   233     enterField selectFromLine:1 col:start toLine:1 col:stop
       
   234 !
       
   235 
       
   236 action:aBlock
       
   237     "set the action to be performed when user presses ok-button;
       
   238      aBlock must be nil or a block with one argument "
       
   239 
       
   240     okAction := aBlock
       
   241 !
       
   242 
       
   243 abortAction:aBlock
       
   244     "set the action to be performed when user presses abort-button;
       
   245      aBlock must be nil or a block with no arguments"
       
   246 
       
   247     abortAction := aBlock
       
   248 ! !
       
   249 
       
   250 !EnterBox methodsFor:'user interaction'!
       
   251 
       
   252 hideAndEvaluate:aBlock
       
   253     "common processing for all ok-actions (see subclasses);
       
   254      shut down box, fetch entered string and evaluate the action with it"
       
   255 
       
   256     |string|
       
   257 
       
   258     self hide.
       
   259     aBlock notNil ifTrue:[
       
   260         string := self contents.
       
   261         string isNil ifTrue:[
       
   262             string := ''
       
   263         ] ifFalse:[
       
   264             string := string withoutSeparators
       
   265         ].
       
   266         aBlock value:string
       
   267     ]
       
   268 !
       
   269 
       
   270 okPressed
       
   271     "user pressed ok button - hide myself and evaluate okAction"
       
   272 
       
   273     self hideAndEvaluate:okAction
       
   274 !
       
   275 
       
   276 abortPressed
       
   277     "user pressed abort button - hide myself and evaluate okAction"
       
   278 
       
   279     self hideAndEvaluate:abortAction
       
   280 ! !