Label.st
changeset 131 208fa92f434d
parent 130 338e856bddc9
child 132 596439fe4efd
equal deleted inserted replaced
130:338e856bddc9 131:208fa92f434d
    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.24 1995-06-06 04:14:14 claus Exp $
    27 $Header: /cvs/stx/stx/libwidg/Label.st,v 1.25 1995-06-27 02:23:15 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.24 1995-06-06 04:14:14 claus Exp $
    48 $Header: /cvs/stx/stx/libwidg/Label.st,v 1.25 1995-06-27 02:23:15 claus Exp $
    49 "
    49 "
    50 !
    50 !
    51 
    51 
    52 documentation
    52 documentation
    53 "
    53 "
   291 	l width:1.0.
   291 	l width:1.0.
   292 
   292 
   293 	top open
   293 	top open
   294 
   294 
   295 
   295 
       
   296     fitting-adjust (resize to see):
       
   297     Warning: #fit is experimental and should not be used.
       
   298 
       
   299 	|top l|
       
   300 
       
   301 	top := StandardSystemView new.
       
   302 	top extent:(200 @ 200).
       
   303 
       
   304 	l := Label origin:0.25 @ 0.25 corner:0.75@0.75 in:top.
       
   305 	l label:'stretch'.
       
   306 	l level:-1.
       
   307 	l adjust:#fit.
       
   308 
       
   309 	top open
       
   310 
       
   311     or:
       
   312     Warning: #fit is experimental and should not be used.
       
   313 
       
   314 	|top l|
       
   315 
       
   316 	top := StandardSystemView new.
       
   317 	top extent:(200 @ 200).
       
   318 
       
   319 	l := Label origin:0.25 @ 0.25 corner:0.75@0.75 in:top.
       
   320 	l label:(Image fromFile:'bitmaps/SBrowser.xbm').
       
   321 	l level:-1.
       
   322 	l adjust:#fit.
       
   323 
       
   324 	top open
       
   325 
       
   326 
   296     just a reminder, that instead of doing placement manually
   327     just a reminder, that instead of doing placement manually
   297     as in ...:    
   328     as in ...:    
   298 
   329 
   299 	|top l|
   330 	|top l|
   300 
   331 
   560 
   591 
   561 form:aForm
   592 form:aForm
   562     "return a new Label showing a form.
   593     "return a new Label showing a form.
   563      OBSOLETE: you should now use #label: for both text and bitmap labels."
   594      OBSOLETE: you should now use #label: for both text and bitmap labels."
   564 
   595 
   565     ^ (self on:Display) form:aForm
   596     ^ (self on:Screen current) form:aForm
   566 !
   597 !
   567 
   598 
   568 form:aForm in:aView
   599 form:aForm in:aView
   569     "return a new Label showing a form.
   600     "return a new Label showing a form.
   570      OBSOLETE: you should now use #label:in: for both text and bitmap labels."
   601      OBSOLETE: you should now use #label:in: for both text and bitmap labels."
   610 
   641 
   611     super sizeChanged:how.
   642     super sizeChanged:how.
   612     prevPosition := labelOriginX.
   643     prevPosition := labelOriginX.
   613     self computeLabelOrigin
   644     self computeLabelOrigin
   614     shown ifTrue:[
   645     shown ifTrue:[
   615 	labelOriginX ~~ prevPosition ifTrue:[
   646 	(adjust == #fit
   616 	    self redraw
   647 	or:[labelOriginX ~~ prevPosition 
       
   648 	or:[how ~~ #smaller]]) ifTrue:[
       
   649 	    "/ self clear.
       
   650 	    self redraw.
       
   651 	    self redrawEdges
   617 	]
   652 	]
   618     ]
   653     ]
   619 ! !
   654 ! !
   620 
   655 
   621 !Label methodsFor:'accessing-mvc'!
   656 !Label methodsFor:'accessing-mvc'!
   654 
   689 
   655 foregroundColor:aColor
   690 foregroundColor:aColor
   656     "set the foreground color"
   691     "set the foreground color"
   657 
   692 
   658     fgColor := aColor on:device.
   693     fgColor := aColor on:device.
   659     self redraw
   694     shown ifTrue:[self redraw]
   660 !
   695 !
   661 
   696 
   662 foregroundColor
   697 foregroundColor
   663     "return the foreground color"
   698     "return the foreground color"
   664 
   699 
   667 
   702 
   668 backgroundColor:aColor
   703 backgroundColor:aColor
   669     "set the background color"
   704     "set the background color"
   670 
   705 
   671     bgColor := aColor on:device.
   706     bgColor := aColor on:device.
   672     self redraw
   707     shown ifTrue:[self redraw]
   673 !
   708 !
   674 
   709 
   675 backgroundColor
   710 backgroundColor
   676     "return the background color"
   711     "return the background color"
   677 
   712 
   681 foregroundColor:fg backgroundColor:bg
   716 foregroundColor:fg backgroundColor:bg
   682     "set the colors to be used for drawing"
   717     "set the colors to be used for drawing"
   683 
   718 
   684     fgColor := fg on:device.
   719     fgColor := fg on:device.
   685     bgColor := bg on:device.
   720     bgColor := bg on:device.
   686     self redraw
   721     shown ifTrue:[self redraw]
   687 !
   722 !
   688 
   723 
   689 labelString:aString
   724 labelString:aString
   690     "for ST-80 compatibility: same as #label:
   725     "for ST-80 compatibility: same as #label:
   691      set the label-string; adjust extent if not already realized and not fixedSize"
   726      set the label-string; adjust extent if not already realized and not fixedSize"
   762 
   797 
   763     ^ labelWidth
   798     ^ labelWidth
   764 !
   799 !
   765 
   800 
   766 layout:how
   801 layout:how
   767     "for protocol compatibility: alias for #adjust:"
   802     "for protocol compatibility: alias for #adjust:.
   768 
   803      Please use #adjust:, since #layout: conflicts with a method
       
   804      in VW (which has a completely different meaning).
       
   805      In future versions of ST/X, #layout: will behave the VW way."
       
   806 
       
   807     self obsoleteMethodWarning:'use #adjust:'.
   769     self adjust:how
   808     self adjust:how
   770 !
   809 !
   771 
   810 
   772 layout
   811 layout
   773     "for protocol compatibility: alias for #adjust"
   812     "for protocol compatibility: alias for #adjust.
   774 
   813      Please use #adjust, since #layout conflicts with a method
       
   814      in VW (which has a completely different meaning).
       
   815      In future versions of ST/X, #layout will behave the VW way."
       
   816 
       
   817     self obsoleteMethodWarning:'use #adjust'.
   775     ^ self adjust
   818     ^ self adjust
   776 !
   819 !
   777 
   820 
   778 adjust:how
   821 adjust:how
   779     "set the adjust, how which must be one of
   822     "set the adjust, how which must be one of
   826     ] ifFalse:[
   869     ] ifFalse:[
   827 	fgColor := Black on:device.
   870 	fgColor := Black on:device.
   828     ]
   871     ]
   829 !
   872 !
   830 
   873 
   831 realize
       
   832     super realize.
       
   833 "/    fgColor := fgColor on:device.
       
   834 "/    bgColor := bgColor on:device.
       
   835 !
       
   836 
       
   837 initialize
   874 initialize
   838     super initialize.
   875     super initialize.
   839 
   876 
   840     font := font on:device.
   877     font := font on:device.
   841     self height:(font height + font descent).
   878     self height:(font height + font descent).
   843     labelOriginX := labelOriginY := 0.
   880     labelOriginX := labelOriginY := 0.
   844     labelWidth := labelHeight := 0.
   881     labelWidth := labelHeight := 0.
   845     logo := nil.
   882     logo := nil.
   846     fixSize := false.
   883     fixSize := false.
   847     hSpace := (self horizontalPixelPerMillimeter:0.5) rounded.
   884     hSpace := (self horizontalPixelPerMillimeter:0.5) rounded.
   848     vSpace := (self verticalPixelPerMillimeter:0.5) rounded
   885     vSpace := (self verticalPixelPerMillimeter:0.25) rounded
   849 !
   886 !
   850 
   887 
   851 recreate
   888 recreate
   852     "after snapin, labels dimensions may have changed due to
   889     "after snapin, labels dimensions may have changed due to
   853      different font parameters"
   890      different font parameters"
   888 
   925 
   889 drawWith:fg and:bg
   926 drawWith:fg and:bg
   890     "redraw my label with fg/bg - this generic method is also used by subclasses
   927     "redraw my label with fg/bg - this generic method is also used by subclasses
   891      (especially Button) to redraw the logo in different colors."
   928      (especially Button) to redraw the logo in different colors."
   892 
   929 
   893     |x y|
   930     |x y logoScaled scaleH scaleV img delta nW nH hSep vSep m2|
   894 
   931 
   895     self clearInsideWith:bg.
   932     self clearInsideWith:bg.
   896 
   933 
   897     logo notNil ifTrue:[
   934     logo notNil ifTrue:[
   898 	self paint:fg on:bg.
   935 	self paint:fg on:bg.
       
   936 
   899 	logo isImageOrForm ifTrue:[
   937 	logo isImageOrForm ifTrue:[
       
   938 	    x := labelOriginX.
       
   939 	    y := labelOriginY.
       
   940 
       
   941 	    adjust == #fit ifTrue:[
       
   942 		hSep := (hSpace*2).
       
   943 		vSep := (vSpace*2).
       
   944 		m2 := margin * 2.
       
   945 
       
   946 		scaleH := ((width - hSep) / (logo width)) asFloat.
       
   947 		scaleV := ((height - vSep) / (logo height)) asFloat.
       
   948 		self scale:(scaleH @ scaleV).
       
   949 		x := transformation applyInverseScaleX:x.
       
   950 		y := transformation applyInverseScaleY:y.
       
   951 	    ].
       
   952 
   900 	    logo := logo on:device.
   953 	    logo := logo on:device.
   901 "/            self background:bg.
   954 "/            self background:bg.
   902 	    self displayOpaqueForm:logo x:labelOriginX y:labelOriginY
   955 	    self displayOpaqueForm:logo x:x y:y 
   903 	] ifFalse:[
   956 	] ifFalse:[
   904 	    x := labelOriginX + hSpace.
   957 	    x := labelOriginX + hSpace.
   905 	    y := labelOriginY + (font ascent) + vSpace.
   958 	    y := labelOriginY + (font ascent) + vSpace.
       
   959 
       
   960 	    adjust == #fit ifTrue:[
       
   961 		scaleH := ((width - m2 - hSep) / (labelWidth - hSpace)) asFloat.
       
   962 		scaleV := ((height - m2 - vSep) / (labelHeight - vSpace)) asFloat.
       
   963 		self scale:(scaleH min: scaleV).
       
   964 		x := transformation applyInverseScaleX:x.
       
   965 		y := y - font ascent + (transformation applyScaleY:font ascent).
       
   966 		y := transformation applyInverseScaleY:y.
       
   967 		scaleH < scaleV ifTrue:[
       
   968 		    nH := labelHeight * scaleH.
       
   969 		    "/ now, center vertically
       
   970 		    y := y + ((height - nH - m2 - vSep) / scaleH / 2).
       
   971 		].
       
   972 		scaleV < scaleH ifTrue:[
       
   973 		    "/ now, center horizontally
       
   974 		    nW := labelWidth * scaleV.
       
   975 		    x := x + ((width - nW - m2 - hSep) / scaleV / 2) 
       
   976 		].
       
   977 	    ].
   906 
   978 
   907 	    logo isString ifTrue:[
   979 	    logo isString ifTrue:[
   908 		self displayString:logo x:x y:y
   980 		self displayString:logo x:x y:y
   909 	    ] ifFalse:[
   981 	    ] ifFalse:[
   910 		logo do:[ :line |
   982 		logo do:[ :line |
   911 		    self displayString:(line printString) x:x y:y.
   983 		    self displayString:(line printString) x:x y:y.
   912 		    y := y + (font height)
   984 		    y := y + (font height)
   913 		]
   985 		]
   914 	    ]
   986 	    ].
   915 	]
   987 
       
   988 	].
       
   989 
       
   990 	adjust == #fit ifTrue:[
       
   991 	    self scale:1
       
   992 	].
       
   993 
   916     ]
   994     ]
   917 !
   995 !
   918 
   996 
   919 redraw
   997 redraw
   920     "redraw my label"
   998     "redraw my label"
   958     "(re)compute the origin of the label whenever label or font changes"
  1036     "(re)compute the origin of the label whenever label or font changes"
   959 
  1037 
   960     |x y|
  1038     |x y|
   961 
  1039 
   962     labelHeight isNil ifTrue:[^ self].
  1040     labelHeight isNil ifTrue:[^ self].
       
  1041 
       
  1042     adjust == #fit ifTrue:[
       
  1043 	labelOriginX := labelOriginY := margin.
       
  1044 	^ self
       
  1045     ].
   963 
  1046 
   964     "if it does not fit, should we make the origin visible,
  1047     "if it does not fit, should we make the origin visible,
   965      or the center (for text, the center seems better. For images,
  1048      or the center (for text, the center seems better. For images,
   966      I dont really know ehich is better ...
  1049      I dont really know ehich is better ...
   967      The commented code below makes the origin visible
  1050      The commented code below makes the origin visible