Ruler.st
changeset 0 0fd7841626f6
child 2 ab6002adaee1
equal deleted inserted replaced
-1:000000000000 0:0fd7841626f6
       
     1 "
       
     2  COPYRIGHT (c) 1991-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 View subclass:#Ruler
       
    14          instanceVariableNames:'fgColor metric paperWidth paperHeight'
       
    15          classVariableNames:''
       
    16          poolDictionaries:''
       
    17          category:'Views-Interactors'
       
    18 !
       
    19 
       
    20 Ruler comment:'
       
    21 
       
    22 COPYRIGHT (c) 1991-93 by Claus Gittinger
       
    23               All Rights Reserved
       
    24 
       
    25 a Ruler for page layout.
       
    26 
       
    27 %W% %E%
       
    28 
       
    29 written oct 91 by claus
       
    30 '!
       
    31 
       
    32 !Ruler methodsFor:'accessing'!
       
    33 
       
    34 metric:aSymbol
       
    35     "set the metric"
       
    36 
       
    37     metric := aSymbol.
       
    38     shown ifTrue:[
       
    39         self redraw
       
    40     ]
       
    41 ! !
       
    42 
       
    43 !Ruler methodsFor:'redrawing'!
       
    44 
       
    45 redraw
       
    46     "redraw scale"
       
    47 
       
    48     |x pixelPerMM pixelPerInch mod pos shortLen veryShortLen longLen charY
       
    49      top paperWidthMM paperWidthPixel|
       
    50 
       
    51     self fill:viewBackground.
       
    52 
       
    53     paperWidthPixel := (self inchToPixel:paperWidth) rounded.
       
    54     (width > paperWidthPixel) ifTrue:[
       
    55         self paint:(Color darkGrey).
       
    56         self fillRectangleX:paperWidthPixel y:0
       
    57                       width:(width - paperWidthPixel) height:height.
       
    58         self paint:fgColor.
       
    59         self displayLineFromX:paperWidthPixel y:0
       
    60                           toX:paperWidthPixel y:height
       
    61     ].
       
    62 
       
    63     self paint:fgColor.
       
    64 
       
    65     top := height - font height - font ascent.
       
    66     longLen := font height.
       
    67     shortLen := longLen // 2.
       
    68     charY := top + (font ascent) + shortLen.
       
    69     mod := 1.
       
    70 
       
    71     (metric == #mm) ifTrue:[
       
    72         "centimeter - long blibs every centimeter; short ones every half"
       
    73 
       
    74         paperWidthMM := self inchToMillimeter:paperWidth.
       
    75         pixelPerMM := self millimeterToPixel:1.
       
    76         pos := 5.
       
    77         x := (pixelPerMM * pos) rounded.
       
    78         self displayString:'cm' x:3 y:charY.
       
    79         [(x < width) and:[pos <= paperWidthMM]] whileTrue:[
       
    80             (mod == 1) ifTrue:[
       
    81                 self displayLineFromX:x y:top
       
    82                                   toX:x y:(top + shortLen)
       
    83             ] ifFalse:[
       
    84                 self displayLineFromX:x y:top
       
    85                                   toX:x y:(top + longLen).
       
    86                 self displayString:(pos // 10) asInteger printString
       
    87                                  x:(x + 3)
       
    88                                  y:charY
       
    89             ].
       
    90             mod := (mod + 1) \\ 2.
       
    91             pos := pos + 5.
       
    92             x := (pixelPerMM * pos) rounded 
       
    93         ]
       
    94     ].
       
    95     (metric == #inch) ifTrue:[
       
    96         "inches - long blibs every inch; short ones every half; very
       
    97          short ones every quarter"
       
    98 
       
    99         pixelPerInch := self inchToPixel:1.
       
   100         pos := 0.25.
       
   101         x := (pixelPerInch * pos) rounded.
       
   102         veryShortLen := longLen // 4.
       
   103         self displayString:'inch' x:3 y:charY.
       
   104         [(x < width) and:[pos <= paperWidth]] whileTrue:[
       
   105             (mod == 0) ifTrue:[
       
   106                 self displayLineFromX:x y:top
       
   107                                   toX:x y:(top + longLen).
       
   108                 self displayString:pos asInteger printString
       
   109                                  x:(x + 3)
       
   110                                  y:charY
       
   111             ] ifFalse:[
       
   112                 (mod == 2) ifTrue:[
       
   113                     self displayLineFromX:x y:top
       
   114                                       toX:x y:(top + shortLen)
       
   115                 ] ifFalse:[
       
   116                     self displayLineFromX:x y:top
       
   117                                       toX:x y:(top + veryShortLen)
       
   118                 ]
       
   119             ].
       
   120             mod := (mod + 1) \\ 4.
       
   121             pos := pos + 0.25.
       
   122             x := (pixelPerInch * pos) rounded
       
   123         ]
       
   124     ].
       
   125     self redrawEdges
       
   126 ! !
       
   127 
       
   128 !Ruler methodsFor:'initialization'!
       
   129 
       
   130 initialize
       
   131     super initialize.
       
   132 
       
   133     fgColor := Black.
       
   134     self height:(font height + font descent + font descent). 
       
   135     (Language == #english) ifTrue:[
       
   136         metric := #inch
       
   137     ] ifFalse:[
       
   138         metric := #mm
       
   139     ].
       
   140     paperWidth := 8.5.
       
   141     paperHeight := 11
       
   142 
       
   143     "Ruler new realize"
       
   144 ! !
       
   145 
       
   146 !Ruler methodsFor:'metric conversions'!
       
   147 
       
   148 inchToMillimeter:inches
       
   149     "convert inches to mm"
       
   150 
       
   151     ^ inches * 25.4
       
   152 !
       
   153 
       
   154 inchToPixel:inches
       
   155     "convert inches to screen pixels"
       
   156 
       
   157     ^ inches * self horizontalPixelPerInch
       
   158 !
       
   159 
       
   160 inchToTwip:inches
       
   161     "convert inches to twips"
       
   162 
       
   163     ^ inches * 1440
       
   164 
       
   165 !
       
   166 
       
   167 millimeterToPixel:mm
       
   168     "convert mms to screen pixels"
       
   169 
       
   170     ^ mm * self horizontalPixelPerMillimeter
       
   171 
       
   172 !
       
   173 
       
   174 millimeterToInch:mm
       
   175     "convert mm to inches"
       
   176 
       
   177     ^ mm / 25.4
       
   178 !
       
   179 
       
   180 pointToTwip:points
       
   181     "convert points to twips"
       
   182 
       
   183     ^ points * 20
       
   184 !
       
   185 
       
   186 twipToInch:twips
       
   187     "convert twips to inches"
       
   188 
       
   189     ^ twips / 1440.0
       
   190 !
       
   191 
       
   192 pixelToInch:pixels
       
   193     "convert pixels to inches"
       
   194 
       
   195     ^ pixels / self horizontalPixelPerInch
       
   196 !
       
   197 
       
   198 twipToPixel:twips
       
   199     "convert twips to screen pixels"
       
   200 
       
   201     ^ (twips / 1440.0) * self horizontalPixelPerInch
       
   202 
       
   203 !
       
   204 
       
   205 twipToPoint:twips
       
   206     "convert twips to points"
       
   207 
       
   208     ^ twips / 20.0
       
   209 ! !