Label.st
changeset 125 3ffa271732f7
parent 118 3ee5ea99d0e2
child 128 06a050529335
equal deleted inserted replaced
124:7abd3a234296 125:3ffa271732f7
    22 
    22 
    23 Label comment:'
    23 Label comment:'
    24 COPYRIGHT (c) 1989 by Claus Gittinger
    24 COPYRIGHT (c) 1989 by Claus Gittinger
    25 	      All Rights Reserved
    25 	      All Rights Reserved
    26 
    26 
    27 $Header: /cvs/stx/stx/libwidg/Label.st,v 1.21 1995-05-03 00:29:53 claus Exp $
    27 $Header: /cvs/stx/stx/libwidg/Label.st,v 1.22 1995-05-09 01:56:01 claus Exp $
    28 '!
    28 '!
    29 
    29 
    30 !Label class methodsFor:'documentation'!
    30 !Label class methodsFor:'documentation'!
    31 
    31 
    32 copyright
    32 copyright
    43 "
    43 "
    44 !
    44 !
    45 
    45 
    46 version
    46 version
    47 "
    47 "
    48 $Header: /cvs/stx/stx/libwidg/Label.st,v 1.21 1995-05-03 00:29:53 claus Exp $
    48 $Header: /cvs/stx/stx/libwidg/Label.st,v 1.22 1995-05-09 01:56:01 claus Exp $
    49 "
    49 "
    50 !
    50 !
    51 
    51 
    52 documentation
    52 documentation
    53 "
    53 "
    70     This can be used, if resizing of the label is not wanted.
    70     This can be used, if resizing of the label is not wanted.
    71     However, in this case you have to make certain that the size is big enough
    71     However, in this case you have to make certain that the size is big enough
    72     to hold any changed logos later. (usually, you create the label first with
    72     to hold any changed logos later. (usually, you create the label first with
    73     the longest string first to have it compute its size, then set the fixSize 
    73     the longest string first to have it compute its size, then set the fixSize 
    74     attribute to avoid resizing later).
    74     attribute to avoid resizing later).
       
    75     Be careful when placing self-resizing labels into panels - by default,
       
    76     panels do not react on the size change - leading to ugly looking geometry.
       
    77     (but you can tell the panel to watch for changes with #elementsCHangeSize:)
    75 
    78 
    76     The placement of the contents within the label is controlled by
    79     The placement of the contents within the label is controlled by
    77     the adjust attribute, it can be set with:
    80     the adjust attribute, it can be set with:
    78 
    81 
    79 	aLabel adjust:how
    82 	aLabel adjust:how
   106       Having a labelSymbol different from the aspectSymbol allows for two labels
   109       Having a labelSymbol different from the aspectSymbol allows for two labels
   107       to react on the same aspect-change, but use different messages when asking
   110       to react on the same aspect-change, but use different messages when asking
   108       the model for a new label contents. By default, the labelMsg is nil,
   111       the model for a new label contents. By default, the labelMsg is nil,
   109       so the label does NOT update its shown contents.
   112       so the label does NOT update its shown contents.
   110       The aspectMsg defaults to #value.
   113       The aspectMsg defaults to #value.
       
   114 
   111 
   115 
   112     Instance variables:
   116     Instance variables:
   113 
   117 
   114 	logo                <Object>        the logo, can be a Form, String or Text
   118 	logo                <Object>        the logo, can be a Form, String or Text
   115 	labelWidth          <Integer>       the width of the logo in device units
   119 	labelWidth          <Integer>       the width of the logo in device units
   366 	l origin:50@100.
   370 	l origin:50@100.
   367 
   371 
   368 	top open
   372 	top open
   369 
   373 
   370 
   374 
   371      MVC operation (model provides the label):
   375      MVC operation 
   372      (have to use a plug to simulate a model which responds to
   376        model provides the label):
   373       the #someAspect message):
   377        (have to use a plug to simulate a model which responds to
       
   378 	the #someAspect message):
   374 
   379 
   375 	|top l model|
   380 	|top l model|
   376 
   381 
   377 	model := Plug new.
   382 	model := Plug new.
   378 	model respondTo:#someAspect with:['models labelString'].
   383 	model respondTo:#someAspect with:['models labelString'].
   389 	model changed:#someAspect
   394 	model changed:#someAspect
   390 	...
   395 	...
   391 
   396 
   392 
   397 
   393     concrete example (track a counters value):
   398     concrete example (track a counters value):
   394     (here, the default aspect #value is used both to notify the label about
   399       (here, the default aspect #value is used both to notify the label about
   395     changes and to aquire a new value from the model).
   400        changes and to aquire a new value from the model).
   396 
   401 
   397 	|top l model|
   402 	|top l model|
   398 
   403 
   399 	model := ValueHolder new.
   404 	model := ValueHolder new.
   400 	model value:0.
   405 	model value:0.
   408 
   413 
   409 	top := StandardSystemView new.
   414 	top := StandardSystemView new.
   410 	top extent:(200 @ 200).
   415 	top extent:(200 @ 200).
   411 
   416 
   412 	l := Label in:top.
   417 	l := Label in:top.
       
   418 	l level:-1.
   413 	l model:model; labelMessage:#value.
   419 	l model:model; labelMessage:#value.
   414 
   420 
   415 	top open
   421 	top open
   416 
   422 
   417 
   423 
   418      MVC operation (model changes aspect after a while; 
   424        model changes aspect after a while; two labels on the same model:
   419      two labels on the same model):
       
   420 
   425 
   421 	|top l model|
   426 	|top l model|
   422 
   427 
   423 	model := Plug new.
   428 	model := Plug new.
   424 	model respondTo:#labelValue1 with:['models labelString1'].
   429 	model respondTo:#labelValue1 with:['models labelString1'].
   439 	model respondTo:#labelValue2 with:['new string2'].
   444 	model respondTo:#labelValue2 with:['new string2'].
   440 
   445 
   441 	model changed:#someAspect 
   446 	model changed:#someAspect 
   442 
   447 
   443 
   448 
   444      plugged MVC operation (getBlock returns the label): 
   449       plugged MVC operation (getBlock returns the label): 
   445 
   450 
   446 	|top l model|
   451 	|top l model|
   447 
   452 
   448 	model := PluggableAdaptor new
   453 	model := PluggableAdaptor new
   449 			getBlock:[:m | 'hello']
   454 			getBlock:[:m | 'hello']
   455 
   460 
   456 	l := Label origin:0.0@0.0 corner:1.0@0.5 in:top.
   461 	l := Label origin:0.0@0.0 corner:1.0@0.5 in:top.
   457 	l model:model; labelMessage:#value.
   462 	l model:model; labelMessage:#value.
   458 
   463 
   459 	top open.
   464 	top open.
       
   465 
       
   466 
       
   467       use different label-selectors to access fields of a complex model:
       
   468 
       
   469 	|top panel model|
       
   470 
       
   471 	model := Plug new.
       
   472 	model respondTo:#field1 with:['value1'].
       
   473 	model respondTo:#field2 with:['value2'].
       
   474 	model respondTo:#field3 with:['value3'].
       
   475 	model respondTo:#field4 with:['value4'].
       
   476 
       
   477 	top := StandardSystemView new.
       
   478 
       
   479 	panel := VerticalPanelView origin:0.0@0.0 corner:1.0@1.0 in:top.
       
   480 	panel elementsChangeSize:true.
       
   481 
       
   482 	panel addSubView:((Label on:model) labelMessage:#field1).
       
   483 	panel addSubView:((Label on:model) labelMessage:#field2).
       
   484 	panel addSubView:((Label on:model) labelMessage:#field3).
       
   485 	panel addSubView:((Label on:model) labelMessage:#field4).
       
   486 
       
   487 	top extent:(200 @ 200).
       
   488 	top open.
       
   489 
       
   490 	(Delay forSeconds:5) wait.
       
   491 
       
   492 	model respondTo:#field2 with:['new value2'].
       
   493 	model changed:#value  
       
   494 
       
   495 
       
   496       same as above, using default aspects in the label, and an adaptor
       
   497       to translate aspects:
       
   498 
       
   499 	|top panel model v1|
       
   500 
       
   501 	model := Plug new.
       
   502 	model respondTo:#field1 with:[v1].
       
   503 	model respondTo:#field1: with:[:arg | v1 := arg. model changed:#field1].
       
   504 	model respondTo:#field2 with:['value2'].
       
   505 	model respondTo:#field2: with:[:arg |].
       
   506 	model respondTo:#field3 with:['value3'].
       
   507 	model respondTo:#field3: with:[:arg |].
       
   508 	model respondTo:#field4 with:['value4'].
       
   509 	model respondTo:#field4: with:[:arg |].
       
   510 
       
   511 	top := StandardSystemView new.
       
   512 
       
   513 	panel := VerticalPanelView origin:0.0@0.0 corner:1.0@1.0 in:top.
       
   514 	panel elementsChangeSize:true.
       
   515 
       
   516 	panel addSubView:((Label on:((AspectAdaptor subject:model) forAspect:#field1)) labelMessage:#value).
       
   517 	panel addSubView:((Label on:((AspectAdaptor subject:model) forAspect:#field2)) labelMessage:#value).
       
   518 	panel addSubView:((Label on:((AspectAdaptor subject:model) forAspect:#field3)) labelMessage:#value).
       
   519 	panel addSubView:((Label on:((AspectAdaptor subject:model) forAspect:#field4)) labelMessage:#value).
       
   520 
       
   521 	top extent:(200 @ 200).
       
   522 	top open.
       
   523 
       
   524 	(Delay forSeconds:5) wait.
       
   525 
       
   526 	model field1:'new value1'.
       
   527 
       
   528 
       
   529       use an adapter to access fields of a complex model:
       
   530 
       
   531 	|top l panel model|
       
   532 
       
   533 	model := #('one' 'two' 'three') asValue.
       
   534 
       
   535 	top := StandardSystemView new.
       
   536 
       
   537 	panel := VerticalPanelView origin:0.0@0.0 corner:1.0@1.0 in:top.
       
   538 	panel elementsChangeSize:true.
       
   539 
       
   540 	panel addSubView:((Label on:(ProtocolAdaptor
       
   541 					subjectChannel:model
       
   542 					accessPath:#(1))) labelMessage:#value).
       
   543 	panel addSubView:((Label on:(ProtocolAdaptor
       
   544 					subjectChannel:model
       
   545 					accessPath:#(2))) labelMessage:#value).
       
   546 	panel addSubView:((Label on:(ProtocolAdaptor
       
   547 					subjectChannel:model
       
   548 					accessPath:#(3))) labelMessage:#value).
       
   549 
       
   550 	top extent:(200 @ 200).
       
   551 	top open.
       
   552 
       
   553 	(Delay forSeconds:5) wait.
       
   554 
       
   555 	model value:#('oneone' 'twotwo' 'threethree').
   460 "
   556 "
   461 ! !
   557 ! !
   462 
   558 
   463 !Label class methodsFor:'instance creation'!
   559 !Label class methodsFor:'instance creation'!
   464 
   560 
   520 	    self redraw
   616 	    self redraw
   521 	]
   617 	]
   522     ]
   618     ]
   523 ! !
   619 ! !
   524 
   620 
       
   621 !
       
   622 
       
   623 !Label methodsFor:'accessing-mvc'!
       
   624 
       
   625 model:aModel
       
   626     super model:aModel.
       
   627     self getLabelFromModel.
       
   628 !
       
   629 
       
   630 labelMessage 
       
   631     "return the symbol used to aquire the labelString/image from the model
       
   632      when the aspect changes.
       
   633      The default is nil, which means: leave the label unchanged."
       
   634 
       
   635     ^ labelMsg
       
   636 !
       
   637 
       
   638 labelMessage:aSymbol 
       
   639     "set the symbol used to aquire the labelString/image from the model.
       
   640      The default is nil, which means: leave the label unchanged."
       
   641 
       
   642     labelMsg ~~ aSymbol ifTrue:[
       
   643 	labelMsg := aSymbol.
       
   644 	self getLabelFromModel
       
   645     ]
       
   646 !
       
   647 
       
   648 addModelInterfaceTo:aDictionary
       
   649     "see comment in View>>modelInterface"
       
   650 
       
   651     super addModelInterfaceTo:aDictionary.
       
   652     aDictionary at:#labelMessage put:labelMsg
       
   653 ! !
       
   654 
   525 !Label methodsFor:'accessing'!
   655 !Label methodsFor:'accessing'!
   526 
   656 
   527 foregroundColor:aColor
   657 foregroundColor:aColor
   528     "set the foreground color"
   658     "set the foreground color"
   529 
   659 
   554     "set the colors to be used for drawing"
   684     "set the colors to be used for drawing"
   555 
   685 
   556     fgColor := fg on:device.
   686     fgColor := fg on:device.
   557     bgColor := bg on:device.
   687     bgColor := bg on:device.
   558     self redraw
   688     self redraw
   559 !
       
   560 
       
   561 labelMessage:aSymbol 
       
   562     "set the symbol used to aquire the labelString/image from the model.
       
   563      The default is nil, which means: leave the label unchanged."
       
   564 
       
   565     labelMsg := aSymbol
       
   566 !
   689 !
   567 
   690 
   568 labelString:aString
   691 labelString:aString
   569     "for ST-80 compatibility: same as #label:
   692     "for ST-80 compatibility: same as #label:
   570      set the label-string; adjust extent if not already realized and not fixedSize"
   693      set the label-string; adjust extent if not already realized and not fixedSize"
   637     "return the logos width in pixels"
   760     "return the logos width in pixels"
   638 
   761 
   639     ^ labelWidth
   762     ^ labelWidth
   640 !
   763 !
   641 
   764 
       
   765 layout:how
       
   766     "for protocol compatibility: alias for #adjust:"
       
   767 
       
   768     self adjust:how
       
   769 !
       
   770 
       
   771 layout
       
   772     "for protocol compatibility: alias for #adjust"
       
   773 
       
   774     ^ self adjust
       
   775 !
       
   776 
   642 adjust:how
   777 adjust:how
   643     "set the adjust, how which must be one of
   778     "set the adjust, how which must be one of
   644 
   779 
   645      #left        -> left adjust logo
   780      #left        -> left adjust logo
   646      #right       -> right adjust logo
   781      #right       -> right adjust logo
   657 	adjust := how.
   792 	adjust := how.
   658 	self newLayout
   793 	self newLayout
   659     ]
   794     ]
   660 !
   795 !
   661 
   796 
       
   797 adjust
       
   798     "return the adjust symbol"
       
   799 
       
   800     ^ adjust
       
   801 !
       
   802 
   662 font:aFont
   803 font:aFont
   663     "set the font - if I'm not realized and not fixedSize, adjust my size"
   804     "set the font - if I'm not realized and not fixedSize, adjust my size"
   664 
   805 
   665     (aFont ~~ font) ifTrue:[
   806     (aFont ~~ font) ifTrue:[
   666 	super font:(aFont on:device).
   807 	super font:(aFont on:device).
   688 
   829 
   689 realize
   830 realize
   690     super realize.
   831     super realize.
   691 "/    fgColor := fgColor on:device.
   832 "/    fgColor := fgColor on:device.
   692 "/    bgColor := bgColor on:device.
   833 "/    bgColor := bgColor on:device.
   693     (model notNil 
       
   694     and:[aspectMsg notNil]) ifTrue:[
       
   695 	self getLabelFromModel.
       
   696     ]
       
   697 !
   834 !
   698 
   835 
   699 initialize
   836 initialize
   700     super initialize.
   837     super initialize.
   701 
   838 
   793      Here, we use labelMsg. 
   930      Here, we use labelMsg. 
   794      This allows multiple labels to react on the same aspect, 
   931      This allows multiple labels to react on the same aspect, 
   795      but show different labels when changed (also, constant labels
   932      but show different labels when changed (also, constant labels
   796      which have a nil labelMsg will not try to aquire a labelString)."
   933      which have a nil labelMsg will not try to aquire a labelString)."
   797 
   934 
   798     |sym|
   935     (model notNil 
   799 
   936     and:[labelMsg notNil]) ifTrue:[
   800     model notNil ifTrue:[
   937 	self label:(model perform:labelMsg) printString.
   801 	sym := labelMsg.
       
   802 "/        sym isNil ifTrue:[sym := aspect<sg].
       
   803 	sym notNil ifTrue:[
       
   804 	    self label:(model perform:sym) printString.
       
   805 	]
       
   806     ].
   938     ].
   807 !
   939 !
   808 
   940 
   809 newLayout
   941 newLayout
   810     "recompute position/size after a change
   942     "recompute position/size after a change
   935 update:something with:aParameter from:changedObject
  1067 update:something with:aParameter from:changedObject
   936     "the MVC way of changing the label ..."
  1068     "the MVC way of changing the label ..."
   937 
  1069 
   938     changedObject == model ifTrue:[
  1070     changedObject == model ifTrue:[
   939 	something == aspectMsg ifTrue:[
  1071 	something == aspectMsg ifTrue:[
   940 	    labelMsg notNil ifTrue:[
  1072 	    self getLabelFromModel.
   941 		self getLabelFromModel.
       
   942 	    ].
       
   943 	    ^ self.
  1073 	    ^ self.
   944 	]
  1074 	]
   945     ].
  1075     ].
   946     super update:something
  1076     ^ super update:something with:aParameter from:changedObject
   947 ! !
  1077 ! !
   948 
  1078 
   949 !Label methodsFor:'queries'!
  1079 !Label methodsFor:'queries'!
   950 
  1080 
   951 preferedExtent
  1081 preferedExtent