Ruler.st
changeset 6107 b2a3ee58d40c
parent 2348 6a671b60f274
equal deleted inserted replaced
6106:78049c3f344d 6107:b2a3ee58d40c
     7  inclusion of the above copyright notice.   This software may not
     7  inclusion of the above copyright notice.   This software may not
     8  be provided or otherwise made available to, or used by, any
     8  be provided or otherwise made available to, or used by, any
     9  other person.  No title to or ownership of the software is
     9  other person.  No title to or ownership of the software is
    10  hereby transferred.
    10  hereby transferred.
    11 "
    11 "
    12 
       
    13 "{ Package: 'stx:libwidg2' }"
    12 "{ Package: 'stx:libwidg2' }"
       
    13 
       
    14 "{ NameSpace: Smalltalk }"
    14 
    15 
    15 SimpleView subclass:#Ruler
    16 SimpleView subclass:#Ruler
    16 	instanceVariableNames:'fgColor metric paperWidth paperHeight scale showUnit orientation'
    17 	instanceVariableNames:'fgColor metric paperWidth paperHeight scale showUnit orientation'
    17 	classVariableNames:''
    18 	classVariableNames:''
    18 	poolDictionaries:''
    19 	poolDictionaries:''
   212 
   213 
   213     viewBackground := styleSheet colorAt:#rulerBackgroundColor default:viewBackground.
   214     viewBackground := styleSheet colorAt:#rulerBackgroundColor default:viewBackground.
   214     fgColor := styleSheet colorAt:#rulerForegroundColor.
   215     fgColor := styleSheet colorAt:#rulerForegroundColor.
   215     fgColor isNil ifTrue:[
   216     fgColor isNil ifTrue:[
   216         fgColor := styleSheet colorAt:#foregroundColor.
   217         fgColor := styleSheet colorAt:#foregroundColor.
   217     ].
   218         fgColor isNil ifTrue:[
   218     fgColor isNil ifTrue:[
   219             viewBackground brightness > 0.5 ifTrue:[
   219         viewBackground brightness > 0.5 ifTrue:[
   220                 fgColor := Color black.
   220             fgColor := Black.
   221             ] ifFalse:[
   221         ] ifFalse:[
   222                 fgColor := Color white
   222             fgColor := White
   223             ].
   223         ].
   224         ].
   224     ].
   225     ].
   225     fgColor := fgColor onDevice:device.
   226     fgColor := fgColor onDevice:device.
   226 
   227 
   227     bitGravity := #NorthWest.
   228     bitGravity := #NorthWest.
   228 
   229 
   229     self height:(font height + (2 * font descent)). 
   230     self height:(self font height + (2 * self font descent)). 
   230 
   231 
   231     (Smalltalk language == #english) ifTrue:[
   232     (Smalltalk language == #english) ifTrue:[
   232         metric := #inch
   233         metric := #inch
   233     ] ifFalse:[
   234     ] ifFalse:[
   234         metric := #mm
   235         metric := #mm
   243     paperHeight := 11.
   244     paperHeight := 11.
   244 
   245 
   245     "
   246     "
   246      take a smaller font
   247      take a smaller font
   247     "
   248     "
   248     font := (Font family:(font family)
   249     self font:(self font asSize:8).
   249                     face:(font face)
       
   250                    style:(font style)
       
   251                     size:8) onDevice:device.
       
   252 
   250 
   253     "
   251     "
   254      Ruler new open
   252      Ruler new open
   255     "
   253     "
   256 !
       
   257 
       
   258 reinitialize
       
   259     super reinitialize.
       
   260     font := font on:device.
       
   261 ! !
   254 ! !
   262 
   255 
   263 !Ruler methodsFor:'metric conversions'!
   256 !Ruler methodsFor:'metric conversions'!
   264 
   257 
   265 inchToPixel:inches
   258 inchToPixel:inches
   298 
   291 
   299     paperWidthMM := UnitConverter inchToMillimeter:paperWidth.
   292     paperWidthMM := UnitConverter inchToMillimeter:paperWidth.
   300     paperHeightMM := UnitConverter inchToMillimeter:paperHeight.
   293     paperHeightMM := UnitConverter inchToMillimeter:paperHeight.
   301     pixelPerMM := (self millimeterToPixel:1) * scale.
   294     pixelPerMM := (self millimeterToPixel:1) * scale.
   302 
   295 
   303     fontHeight := font height.
   296     fontHeight := self font height.
   304     fontAscent := font ascent.
   297     fontAscent := self font ascent.
   305 
   298 
   306     orientation == #horizontal ifTrue:[
   299     orientation == #horizontal ifTrue:[
   307         (xOrigin + width > paperWidthPixel) ifTrue:[
   300         (xOrigin + width > paperWidthPixel) ifTrue:[
   308             self paint:(Color darkGrey).
   301             self paint:(Color darkGrey).
   309             self 
   302             self 
   327         (metric == #mm) ifTrue:[
   320         (metric == #mm) ifTrue:[
   328             "centimeter - long blibs every centimeter; short ones every half"
   321             "centimeter - long blibs every centimeter; short ones every half"
   329 
   322 
   330             pos := 5.
   323             pos := 5.
   331             showUnit ifTrue:[
   324             showUnit ifTrue:[
   332                 labelRight := stringRight := marg + (font widthOf:'cm') + 3 + xOrigin.
   325                 labelRight := stringRight := marg + (self font widthOf:'cm') + 3 + xOrigin.
   333             ] ifFalse:[
   326             ] ifFalse:[
   334                 labelRight := stringRight := xOrigin.
   327                 labelRight := stringRight := xOrigin.
   335             ].
   328             ].
   336 
   329 
   337             x := (pixelPerMM * pos) rounded.
   330             x := (pixelPerMM * pos) rounded.
   350                 ((x < stringRight) or:[mod == 1]) ifFalse:[
   343                 ((x < stringRight) or:[mod == 1]) ifFalse:[
   351                     self displayString:(pos // 10) printString
   344                     self displayString:(pos // 10) printString
   352                                      x:(x + marg)
   345                                      x:(x + marg)
   353                                      y:charY.
   346                                      y:charY.
   354                     stringRight := x + marg 
   347                     stringRight := x + marg 
   355                                     + (font widthOf:(pos // 10) printString)
   348                                     + (self font widthOf:(pos // 10) printString)
   356                 ].
   349                 ].
   357                 mod := (mod + 1) \\ 2.
   350                 mod := (mod + 1) \\ 2.
   358                 pos := pos + 5.
   351                 pos := pos + 5.
   359                 x := (pixelPerMM * pos) rounded 
   352                 x := (pixelPerMM * pos) rounded 
   360             ].
   353             ].
   367              short ones every quarter"
   360              short ones every quarter"
   368 
   361 
   369             pixelPerInch := (self inchToPixel:1) * scale.
   362             pixelPerInch := (self inchToPixel:1) * scale.
   370             pos := 0.25.
   363             pos := 0.25.
   371             showUnit ifTrue:[
   364             showUnit ifTrue:[
   372                 labelRight := marg + (font widthOf:'inch') + 3 + xOrigin.
   365                 labelRight := marg + (self font widthOf:'inch') + 3 + xOrigin.
   373             ] ifFalse:[
   366             ] ifFalse:[
   374                 labelRight := xOrigin
   367                 labelRight := xOrigin
   375             ].
   368             ].
   376 
   369 
   377             x := (pixelPerInch * pos) rounded.
   370             x := (pixelPerInch * pos) rounded.
   419         ].
   412         ].
   420 
   413 
   421         self paint:fgColor.
   414         self paint:fgColor.
   422 
   415 
   423         left := 0. "width - (font widthOf:'WW')"
   416         left := 0. "width - (font widthOf:'WW')"
   424         longLen := font widthOf:'WW'.
   417         longLen := self font widthOf:'WW'.
   425         shortLen := longLen // 2.
   418         shortLen := longLen // 2.
   426         charX := left + shortLen.
   419         charX := left + shortLen.
   427         mod := 1.
   420         mod := 1.
   428         marg := 3. "character shift"
   421         marg := 3. "character shift"
   429 
   422 
   502             ]
   495             ]
   503         ].
   496         ].
   504     ].
   497     ].
   505 
   498 
   506     self redrawEdges
   499     self redrawEdges
   507 
       
   508 
       
   509 ! !
   500 ! !
   510 
   501 
   511 !Ruler class methodsFor:'documentation'!
   502 !Ruler class methodsFor:'documentation'!
   512 
   503 
   513 version
   504 version
   514     ^ '$Header: /cvs/stx/stx/libwidg2/Ruler.st,v 1.28 2002-10-31 21:49:29 cg Exp $'
   505     ^ '$Header$'
   515 ! !
   506 ! !
       
   507