EditField.st
changeset 122 04ec3fda7c11
parent 121 4e63bbdb266a
child 125 3ffa271732f7
equal deleted inserted replaced
121:4e63bbdb266a 122:04ec3fda7c11
    11 "
    11 "
    12 
    12 
    13 'From Smalltalk/X, Version:2.10.5 on 4-may-1995 at 8:46:55 am'!
    13 'From Smalltalk/X, Version:2.10.5 on 4-may-1995 at 8:46:55 am'!
    14 
    14 
    15 EditTextView subclass:#EditField
    15 EditTextView subclass:#EditField
    16 	 instanceVariableNames:'leaveAction enabled enableAction alwaysAccept crAction tabAction
    16 	 instanceVariableNames:'leaveAction enabled enableAction crAction tabAction
    17 		converter acceptAction leaveKeys'
    17 		converter acceptAction leaveKeys alwaysAccept acceptOnLeave acceptOnReturn'
    18 	 classVariableNames:'DefaultForegroundColor DefaultBackgroundColor
    18 	 classVariableNames:'DefaultForegroundColor DefaultBackgroundColor
    19 		DefaultSelectionForegroundColor DefaultSelectionBackgroundColor
    19 		DefaultSelectionForegroundColor DefaultSelectionBackgroundColor
    20 		DefaultFont'
    20 		DefaultFont'
    21 	 poolDictionaries:''
    21 	 poolDictionaries:''
    22 	 category:'Views-Text'
    22 	 category:'Views-Text'
    24 
    24 
    25 EditField comment:'
    25 EditField comment:'
    26 COPYRIGHT (c) 1990 by Claus Gittinger
    26 COPYRIGHT (c) 1990 by Claus Gittinger
    27 	      All Rights Reserved
    27 	      All Rights Reserved
    28 
    28 
    29 $Header: /cvs/stx/stx/libwidg/EditField.st,v 1.19 1995-05-06 14:16:58 claus Exp $
    29 $Header: /cvs/stx/stx/libwidg/EditField.st,v 1.20 1995-05-07 00:15:56 claus Exp $
    30 '!
    30 '!
    31 
    31 
    32 !EditField class methodsFor:'documentation'!
    32 !EditField class methodsFor:'documentation'!
    33 
    33 
    34 copyright
    34 copyright
    45 "
    45 "
    46 !
    46 !
    47 
    47 
    48 version
    48 version
    49 "
    49 "
    50 $Header: /cvs/stx/stx/libwidg/EditField.st,v 1.19 1995-05-06 14:16:58 claus Exp $
    50 $Header: /cvs/stx/stx/libwidg/EditField.st,v 1.20 1995-05-07 00:15:56 claus Exp $
    51 "
    51 "
    52 !
    52 !
    53 
    53 
    54 documentation
    54 documentation
    55 "
    55 "
    69 						 field has to be enabled next)
    69 						 field has to be enabled next)
    70 
    70 
    71       enabled        <Boolean>                  if false, input is ignored.
    71       enabled        <Boolean>                  if false, input is ignored.
    72 
    72 
    73       enableAction   <Block | nil>
    73       enableAction   <Block | nil>
       
    74 
       
    75       crAction       <Block | nil>              if non-nil, keyboard input of a cr are not
       
    76 						handled specially, instead this block is evaluated
       
    77 						(however, this block can perform additional checks and send
       
    78 						 a #accept then)
       
    79 
       
    80       tabAction      <Block | nil>              if non-nil, keyboard input of a tab character
       
    81 						is not entered into the text, instead this block
       
    82 						is evaluated.
       
    83 
       
    84       converter      <PrintConverter | nil>     if non-nil, this is supposed to convert between
       
    85 						the object and its printed representation.
       
    86 						Defaults to nil i.e. assume that strings are edited.
       
    87 
       
    88       acceptAction   <Block | nil>              if non-nil, this is performed in addition to
       
    89 						the leaveAction.
       
    90 
       
    91       leaveKeys      <Collection>               keys which are interpreted as 'leving the field'
       
    92 
    74       alwaysAccept   <Boolean>                  if true, every change of the text is immediately
    93       alwaysAccept   <Boolean>                  if true, every change of the text is immediately
    75 						forwardd to the model/acceptBlock.
    94 						forwardd to the model/acceptBlock.
    76 						Default is false i.e. only forward changes
    95 						Default is false i.e. only forward changes
    77 						on accept.
    96 						on accept.
    78 
    97 
    79       crAction       <Block | nil>              if non-nil, keyboard input of a cr are not
    98       acceptOnLeave  <Boolean>                  if true, leaving the field (via cursor keys)
    80 						handled specially, instead this block is evaluated
    99 						automatically accepts the value into the model.
    81 						(however, this block can perform additional checks and send
   100 						Default is false.
    82 						 a #accept then)
   101 
    83 
   102       acceptOnReturn <Boolean>                  if true, leaving the field via return
    84       tabAction      <Block | nil>              if non-nil, keyboard input of a tab character
   103 						automatically accepts the value into the model.
    85 						is not entered into the text, instead this block
   104 						Default is true.
    86 						is evaluated.
       
    87 
       
    88       converter      <PrintConverter | nil>     if non-nil, this is supposed to convert between
       
    89 						the object and its printed representation.
       
    90 						Defaults to nil i.e. assume that strings are edited.
       
    91 
       
    92       acceptAction   <Block | nil>              if non-nil, this is performed in addition to
       
    93 						the leaveAction.
       
    94 
       
    95       leaveKeys      <Collection>               keys which are interpreted as 'leving the field'
       
    96 "
   105 "
    97 !
   106 !
    98 
   107 
    99 examples 
   108 examples 
   100 "
   109 "
   101     basic field:
   110     see more examples in EnterFieldGroup>>examples.
       
   111 
       
   112 
       
   113     basic field in a view:
   102 
   114 
   103 	|top field|
   115 	|top field|
   104 
   116 
   105 	top := StandardSystemView new.
   117 	top := StandardSystemView new.
   106 	top extent:200@100.
   118 	top extent:200@100.
   107 
   119 
   108 	field := EditField origin:0.0@0.0 in:top.
   120 	field := EditField origin:0.0@0.0 in:top.
   109 	field width:1.0.        'let its height as-is'.
   121 	field width:1.0.        'let its height as-is'.
   110 
   122 
   111 	top open
   123 	top open
       
   124 
       
   125 
       
   126     forward input in topView to field:
       
   127 
       
   128 	|top field|
       
   129 
       
   130 	top := StandardSystemView new.
       
   131 	top extent:200@100.
       
   132 
       
   133 	field := EditField origin:0.0@0.0 in:top.
       
   134 	field width:1.0.        'let its height as-is'.
       
   135 
       
   136 	top delegate:(KeyboardForwarder toView:field).
       
   137 	top open
       
   138 
   112 
   139 
   113     just to make it look better: set some inset:
   140     just to make it look better: set some inset:
   114 
   141 
   115 	|top field|
   142 	|top field|
   116 
   143 
   122 	field leftInset:ViewSpacing;
   149 	field leftInset:ViewSpacing;
   123 	      rightInset:ViewSpacing.
   150 	      rightInset:ViewSpacing.
   124 
   151 
   125 	top open
   152 	top open
   126 
   153 
       
   154 
   127     give it an initial contents:
   155     give it an initial contents:
   128 
   156 
   129 	|top field|
   157 	|top field|
   130 
   158 
   131 	top := StandardSystemView new.
   159 	top := StandardSystemView new.
   137 	      rightInset:ViewSpacing.
   165 	      rightInset:ViewSpacing.
   138 	field editValue:'hello world'.
   166 	field editValue:'hello world'.
   139 
   167 
   140 	top open
   168 	top open
   141 
   169 
       
   170 
   142     and have it preselected:
   171     and have it preselected:
   143 
   172 
   144 	|top field|
   173 	|top field|
   145 
   174 
   146 	top := StandardSystemView new.
   175 	top := StandardSystemView new.
   152 	      rightInset:ViewSpacing.
   181 	      rightInset:ViewSpacing.
   153 	field editValue:'hello world' selected:true.
   182 	field editValue:'hello world' selected:true.
   154 
   183 
   155 	top open
   184 	top open
   156 
   185 
       
   186 
   157     have part of it preselected:
   187     have part of it preselected:
   158 
   188 
   159 	|top field|
   189 	|top field|
   160 
   190 
   161 	top := StandardSystemView new.
   191 	top := StandardSystemView new.
   167 	      rightInset:ViewSpacing.
   197 	      rightInset:ViewSpacing.
   168 	field editValue:'hello world';
   198 	field editValue:'hello world';
   169 	      selectFromCharacterPosition:1 to:5.
   199 	      selectFromCharacterPosition:1 to:5.
   170 
   200 
   171 	top open
   201 	top open
       
   202 
   172 
   203 
   173     use a converter:
   204     use a converter:
   174       - numbers:
   205       - numbers:
   175 
   206 
   176 	|top field|
   207 	|top field|
   204 	field converter:(PrintConverter new initForDate).
   235 	field converter:(PrintConverter new initForDate).
   205 	field editValue:Date today.
   236 	field editValue:Date today.
   206 	field acceptAction:[:value | Transcript show:value class name; space; showCr:value].
   237 	field acceptAction:[:value | Transcript show:value class name; space; showCr:value].
   207 	field crAction:[field accept. top destroy].
   238 	field crAction:[field accept. top destroy].
   208 	top open.
   239 	top open.
       
   240 
   209 
   241 
   210     setting alwaysAccept, makes the field update with every key:
   242     setting alwaysAccept, makes the field update with every key:
   211       - numbers:
   243       - numbers:
   212 
   244 
   213 	|top field|
   245 	|top field|
   225 	field editValue:1234.
   257 	field editValue:1234.
   226 	field acceptAction:[:value | Transcript showCr:value].
   258 	field acceptAction:[:value | Transcript showCr:value].
   227 	field crAction:[field accept. top destroy].
   259 	field crAction:[field accept. top destroy].
   228 	top open.
   260 	top open.
   229 
   261 
       
   262 
   230     use a model:
   263     use a model:
   231     (see changing model value in inspector when return is pressed in the field)
   264     (see changing model value in inspector when return is pressed in the field)
   232 
   265 
   233 	|top field model|
   266 	|top field model|
   234 
   267 
   243 	      rightInset:ViewSpacing.
   276 	      rightInset:ViewSpacing.
   244 	field model:model.
   277 	field model:model.
   245 
   278 
   246 	top open.
   279 	top open.
   247 	model inspect.
   280 	model inspect.
       
   281 
   248 
   282 
   249     two views on the same model:
   283     two views on the same model:
   250 
   284 
   251 	|top1 top2 field1 field2 model|
   285 	|top1 top2 field1 field2 model|
   252 
   286 
   267 	field2 width:1.0.
   301 	field2 width:1.0.
   268 	field2 leftInset:ViewSpacing;
   302 	field2 leftInset:ViewSpacing;
   269 	      rightInset:ViewSpacing.
   303 	      rightInset:ViewSpacing.
   270 	field2 model:model.
   304 	field2 model:model.
   271 	top2 open.
   305 	top2 open.
       
   306 
   272 
   307 
   273     just an example; a checkBox and an editField on the same model:
   308     just an example; a checkBox and an editField on the same model:
   274 
   309 
   275 	|top1 top2 field1 box model|
   310 	|top1 top2 field1 box model|
   276 
   311 
   329     enabled := true.
   364     enabled := true.
   330     fixedSize := true.
   365     fixedSize := true.
   331     nFullLinesShown := 1.
   366     nFullLinesShown := 1.
   332     nLinesShown := 1.
   367     nLinesShown := 1.
   333     alwaysAccept := false.
   368     alwaysAccept := false.
       
   369     acceptOnLeave := false.
       
   370     acceptOnReturn := true.
   334     leaveKeys := self class defaultLeaveKeys.
   371     leaveKeys := self class defaultLeaveKeys.
   335     cursorShown := true
   372     cursorShown := true
   336 !
   373 !
   337 
   374 
   338 initStyle
   375 initStyle
   470     ]
   507     ]
   471 !
   508 !
   472 
   509 
   473 contents:someText
   510 contents:someText
   474     "set the contents from a string
   511     "set the contents from a string
   475      - redefined to place the cursor to the end"
   512      - redefined to place the cursor to the end.
       
   513     In your application, please use #editValue:; 
       
   514     it uses a converter (if any) and is compatible to ST-80."
   476 
   515 
   477     super contents:someText.
   516     super contents:someText.
   478     self cursorCol:(someText size + 1).
   517     self cursorCol:(someText size + 1).
   479 !
   518 !
   480 
   519 
   481 contents
   520 contents
   482     "return contents as a string
   521     "return contents as a string
   483      - redefined since EditFields hold only one line of text"
   522      - redefined since EditFields hold only one line of text.
       
   523     In your application, please use #editValue; 
       
   524     it uses a converter (if any) and is compatible to ST-80."
   484 
   525 
   485     list isNil ifTrue:[^ ''].
   526     list isNil ifTrue:[^ ''].
   486     (list size == 0) ifTrue:[^ ''].
   527     (list size == 0) ifTrue:[^ ''].
   487     ^ list at:1
   528     ^ list at:1
   488 !
   529 !
   503     "define an action to be evaluated when field is left by return key"
   544     "define an action to be evaluated when field is left by return key"
   504 
   545 
   505     leaveAction := aBlock
   546     leaveAction := aBlock
   506 !
   547 !
   507 
   548 
       
   549 leaveKeys:aCollectionOfKeySymbols 
       
   550     "define the set of keys which are interpreted as leaveKeys.
       
   551      I.e. those that make the field inactive and accept (if acceptOnLeave is true).
       
   552      The default is a set of #CursorUp, #CursorDown, #Next, #Prior and #Return."
       
   553 
       
   554     leaveKeys := aCollectionOfKeySymbols
       
   555 !
       
   556 
   508 crAction:aBlock
   557 crAction:aBlock
   509     "define an action to be evaluated when the return key is pressed."
   558     "define an action to be evaluated when the return key is pressed."
   510 
   559 
   511     crAction := aBlock
   560     crAction := aBlock
   512 !
   561 !
   522 
   571 
   523     tabAction := aBlock
   572     tabAction := aBlock
   524 !
   573 !
   525 
   574 
   526 alwaysAccept:aBoolean
   575 alwaysAccept:aBoolean
   527     "set/clear the alwaysAccept flag"
   576     "set/clear the alwaysAccept flag. The default is false."
   528 
   577 
   529      alwaysAccept := aBoolean
   578      alwaysAccept := aBoolean
       
   579 !
       
   580 
       
   581 acceptOnReturn:aBoolean
       
   582     "set/clear the acceptOnReturn flag. The default is true."
       
   583 
       
   584      acceptOnReturn := aBoolean
       
   585 !
       
   586 
       
   587 acceptOnLeave:aBoolean
       
   588     "set/clear the acceptOnLeave flag. The default is false."
       
   589 
       
   590      acceptOnLeave := aBoolean
   530 !
   591 !
   531 
   592 
   532 converter
   593 converter
   533     "return the converter (if any)."
   594     "return the converter (if any)."
   534 
   595 
   713     leave ifTrue:[
   774     leave ifTrue:[
   714 	leaveAction notNil ifTrue:[
   775 	leaveAction notNil ifTrue:[
   715 	    leaveAction value:key
   776 	    leaveAction value:key
   716 	].
   777 	].
   717 
   778 
   718 	self accept.
   779 	((key == #Return and:[acceptOnReturn])
       
   780 	or:[key ~~ #Return and:[acceptOnLeave]]) ifTrue:[
       
   781 	    self accept.
       
   782 	].
       
   783 
   719 	x >= 0 ifTrue:[
   784 	x >= 0 ifTrue:[
   720 	    "
   785 	    "
   721 	     let superview know about the leave ...
   786 	     let superview know about the leave ...
   722 	     This is a temporary kludge for the tableWidget -
   787 	     This is a temporary kludge for the tableWidget -
   723 	     it is no clean coding style. Should make the tableWidget
   788 	     it is no clean coding style. Should make the tableWidget