pretty printed & added some documentation and examples
authorClaus Gittinger <cg@exept.de>
Tue, 10 Oct 2006 17:55:30 +0200
changeset 2235 0df6bda4b353
parent 2234 2c03d8e76468
child 2236 76291d984db9
pretty printed & added some documentation and examples
PrinterContext.st
--- a/PrinterContext.st	Thu Oct 05 18:46:23 2006 +0200
+++ b/PrinterContext.st	Tue Oct 10 17:55:30 2006 +0200
@@ -9,7 +9,6 @@
  other person.  No title to or ownership of the software is
  hereby transferred.
 "
-
 "{ Package: 'stx:libview2' }"
 
 GraphicsMedium subclass:#PrinterContext
@@ -33,12 +32,26 @@
  other person.  No title to or ownership of the software is
  hereby transferred.
 "
+!
+
+documentation
+"
+    I am the mediator between the smalltalk printing protocol 
+    (which is the same as the graphics drawing protocol) and the
+    windows printer.
+    When youopena printer, you will typically talk to me, and I will
+    forward the graphics commands to my printer.
+
+    [author:]
+        Felix Madrid (fm@exept.de)
+"
 ! !
 
 !PrinterContext class methodsFor:'instance creation'!
 
 fromPrinterInfo: aPrinterInfo
-        "Private - Answer a Printer object given an hDC"
+    "Private - Answer a Printer object given an hDC"
+
     | aPrinter hDC|     
 
     hDC := aPrinterInfo createDC.
@@ -51,6 +64,7 @@
 
     "Created: / 03-08-2006 / 12:53:52 / fm"
     "Modified: / 04-08-2006 / 12:55:01 / fm"
+    "Modified: / 10-10-2006 / 17:32:59 / cg"
 !
 
 printWith: aRecordingPen
@@ -64,49 +78,54 @@
 !PrinterContext class methodsFor:'accessing'!
 
 default
-        "Answer the default Printer, or nil if none."
-    | defaultName |
-    ^( defaultName := self defaultPrinterName ) isNil
-        ifTrue: [ nil ]
-        ifFalse: [ self named: defaultName ]
+    "Answer the default Printer, or nil if none."
+    
+    |defaultName|
 
+    ^ (defaultName := self defaultPrinterName) isNil 
+        ifTrue:[ nil ]
+        ifFalse:[ self named:defaultName ]
 
-"
-        self default
-"
+    "
+     self default
+    "
 
     "Created: / 27-07-2006 / 17:51:43 / fm"
     "Modified: / 02-08-2006 / 17:27:09 / fm"
+    "Modified: / 10-10-2006 / 17:30:31 / cg"
 !
 
 defaultMargins
-        "Private - answer aPoint containing the default horizontal and vertical
-        margins for a page (units in inches)."
-    ^0.5@0.75
+    "Private - answer aPoint containing the default horizontal and vertical
+     margins for a page (units in inches)."
+    
+    ^ 0.5 @ 0.75
 
     "Created: / 01-08-2006 / 16:09:31 / fm"
+    "Modified: / 10-10-2006 / 17:30:49 / cg"
 !
 
 defaultPrinterName
-        "Answer the name of the default printer, or nil if none."
-    | printerInfo printerName |
-    printerInfo := OperatingSystem getDefaultPrinterName.  " get default "
-    printerName := ( printerInfo
-        copyFrom: 1
-        to: ( printerInfo indexOf: $, ) - 1 )
-            trimBlanks.
-    ^printerName size == 0 ifTrue: [ nil ] ifFalse: [ printerName ]
+    "Answer the name of the default printer, or nil if none."
+    
+    |printerInfo printerName|
+
+    printerInfo := OperatingSystem getDefaultPrinterName.
+    printerName := (printerInfo copyFrom:1 to:(printerInfo indexOf:$,) - 1) trimBlanks.
+    ^ printerName size == 0 ifTrue:[ nil ] ifFalse:[ printerName ]
 
     "
      PrinterContext defaultPrinterName
-"
+    "
 
     "Modified: / 02-08-2006 / 17:26:09 / fm"
+    "Modified: / 10-10-2006 / 17:32:45 / cg"
 !
 
 getPrinterInformation: printerNameString
-        " Answer the printer information for the printer named printerNameString.  If no name is specified,
-        answer the information for the default printer. "
+    " Answer the printer information for the printer named printerNameString.  If no name is specified,
+      answer the information for the default printer. "
+
     | h |
 
      h := OperatingSystem openPrinter: printerNameString.
@@ -114,17 +133,20 @@
 
     "Created: / 27-07-2006 / 10:22:32 / fm"
     "Modified: / 01-08-2006 / 16:01:44 / fm"
+    "Modified: / 10-10-2006 / 17:33:07 / cg"
 !
 
 named: aName
-        "Answer a new instance of Printer which represents
-         the printer named aName as specified in the host
-         Control Panel."
+    "Answer a new instance of Printer which represents
+     the printer named aName as specified in the host
+     Control Panel."
+
     aName isNil ifTrue: [ ^self default ].
-    ^super new printerInfoWithName: aName
+    ^self new printerInfoWithName: aName
 
     "Created: / 27-07-2006 / 17:51:27 / fm"
     "Modified: / 02-08-2006 / 17:26:29 / fm"
+    "Modified: / 10-10-2006 / 17:33:29 / cg"
 !
 
 printerNames
@@ -135,288 +157,238 @@
     "Created: / 27-07-2006 / 17:54:45 / fm"
 ! !
 
-!PrinterContext class methodsFor:'printing'!
+!PrinterContext class methodsFor:'testing & examples'!
 
 print: aString font: aFont title: aTitle
-        "Open a print dialog to allow printing of the given string
-        using the given title & font; answer the receiver."
+    "Open a print dialog to allow printing of the given string
+     using the given title & font."
 
     self print: aString font: aFont title: aTitle wordWrap: false     
 
-"
-            PrinterContext print: 'Holaaaa!! (from:  PrinterContext>>print:aString font:aFont title:aTitle)' font: nil title: 'Printing Test'
-            PrinterContext print: self printingTestString font: nil title: 'Printing Test String'
-            PrinterContext print: self printingTestString font: (Font family:'Arial' face:'medium' size:8) title: 'Printing Test String'
-
-"
+   "
+    PrinterContext print: 'Holaaaa!! (from:  PrinterContext>>print:aString font:aFont title:aTitle)' font: nil title: 'Printing Test'
+    PrinterContext print: self printingTestString font: nil title: 'Printing Test String'
+    PrinterContext print: self printingTestString font: (Font family:'Arial' face:'medium' size:8) title: 'Printing Test String'
+   "
 
     "Created: / 27-07-2006 / 17:52:33 / fm"
     "Modified: / 03-08-2006 / 18:52:31 / fm"
+    "Modified: / 10-10-2006 / 17:37:49 / cg"
 !
 
 print: aString font: aFont title: aTitle wordWrap: wordWrap
-        "Open a print dialog to allow printing of the given string
-        using the given title & font; answer the receiver."
-    | printerInfo printer |
-    printerInfo := PrintingDialog getPrinterInfo.
-    printerInfo isNil ifTrue:[^self].
-
-"/    ( hDCPrinter := printerInfo hDCPrinter ) isNil ifTrue: [ ^self ].
-"/    hDCPrinter := printerInfo createDC.
-    printer := self fromPrinterInfo: printerInfo.
-    [ printer
-        print: aString
-        font: aFont
-        title: aTitle 
-        wordWrap: wordWrap
-        marginsRect: nil ] forkAt: 3
-
-"
-            PrinterContext print: 'Holaaaa!! (from:  PrinterContext>>print:aString font:aFont title:aTitle)' font: nil title: 'Printing Test' wordWrap: true
-            PrinterContext print: self printingTestString font: nil title: 'Printing Test String' wordWrap: true
-            PrinterContext print: self printingTestString font: (Font family:'Arial' face:'medium' size:8) title: 'Printing Test String' wordWrap: true
-
-"
-
-    "Created: / 03-08-2006 / 18:51:53 / fm"
-! !
-
-!PrinterContext class methodsFor:'testing'!
-
-printCircles: arrayOfPointsAndRadius
-
-        "Opens a print dialog and prints the given circles"
+    "Open a print dialog to allow printing of the given string
+     using the given title & font."
 
     | printerInfo printer |
+
     printerInfo := PrintingDialog getPrinterInfo.
     printerInfo isNil ifTrue:[^self].
 
     printer := self fromPrinterInfo: printerInfo.
     [ 
-    printer startPrintJob: 'Circles'.
-    arrayOfPointsAndRadius
-        do:[:pointAndRadius |
-            printer displayCircle:(pointAndRadius at:1) 
-                    radius:(pointAndRadius at:2).
-        ].
-    printer endPrintJob.
+        printer
+            print: aString
+            font: aFont
+            title: aTitle 
+            wordWrap: wordWrap
+            marginsRect: nil 
     ] forkAt: 3
 
-"
-            PrinterContext printCircles:  (Array with: (Array with: 800@800 with: 600)
-                                                 with: (Array with: 1500@1500 with: 1000)
-                                                 with: (Array with: 4000@2500 with: 2000))
-"
+    "
+     PrinterContext print: 'Holaaaa!! (from:  PrinterContext>>print:aString font:aFont title:aTitle)' font: nil title: 'Printing Test' wordWrap: true
+     PrinterContext print: (PrinterContext class sourceCodeAt:#'print:font:title:wordWrap:') font:nil title:'Printing Test String' wordWrap:true
+     PrinterContext print: (PrinterContext class sourceCodeAt:#'print:font:title:wordWrap:') font: (Font family:'Arial' face:'medium' size:8) title: 'Printing Test String' wordWrap: true
+    "
 
-    "Created: / 07-08-2006 / 11:46:52 / fm"
+    "Created: / 03-08-2006 / 18:51:53 / fm"
+    "Modified: / 10-10-2006 / 17:42:06 / cg"
 !
 
-printCirclesIn: rectangles
-
-        "Opens a print dialog and prints the given circles"
+printCircles: arrayOfPointsAndRadius
+    "Opens a print dialog and prints the given circles"
 
     | printerInfo printer |
+
     printerInfo := PrintingDialog getPrinterInfo.
     printerInfo isNil ifTrue:[^self].
 
     printer := self fromPrinterInfo: printerInfo.
     [ 
-    printer startPrintJob: 'Circles In Rectangles'.
-    rectangles
-        do:[:rectangle |
-            printer displayCircleIn: rectangle.
-        ].
-    printer endPrintJob.
+        printer startPrintJob: 'Circles'.
+        arrayOfPointsAndRadius
+            do:[:pointAndRadius |
+                printer displayCircle:(pointAndRadius at:1) 
+                        radius:(pointAndRadius at:2).
+            ].
+        printer endPrintJob.
     ] forkAt: 3
 
-"
-            PrinterContext printCirclesIn:  (Array with: (Rectangle left:20 top:20 width:400 height:600)
-                                                             with: (Rectangle left:40 top:40 width:600 height:400)    )
+    "
+     PrinterContext printCircles:  
+        (Array with: (Array with: 800@800 with: 600)
+               with: (Array with: 1500@1500 with: 1000)
+               with: (Array with: 4000@2500 with: 2000))
+    "
 
-"
-
-    "Created: / 07-08-2006 / 11:48:46 / fm"
+    "Created: / 07-08-2006 / 11:46:52 / fm"
+    "Modified: / 10-10-2006 / 17:36:23 / cg"
 !
 
-printLines: pairOfPointsArray
-
-        "Opens a print dialog and prints the given lines"
+printCirclesIn: rectangles
+    "Opens a print dialog and prints the given circles"
 
     | printerInfo printer |
+
     printerInfo := PrintingDialog getPrinterInfo.
     printerInfo isNil ifTrue:[^self].
 
     printer := self fromPrinterInfo: printerInfo.
     [ 
-    printer startPrintJob: 'Lines'.
-    printer foreground:Color black background:Color white.
-    pairOfPointsArray
-        do:[:pairOfPoints |                 
-             printer displayLineFrom: (pairOfPoints at:1)  to: (pairOfPoints at:2).
-        ].
-    printer endPrintJob.
+        printer startPrintJob: 'Circles In Rectangles'.
+        rectangles
+           do:[:rectangle |
+               printer displayCircleIn: rectangle.
+           ].
+       printer endPrintJob.
     ] forkAt: 3
 
-"
-    PrinterContext printLines:  
-        (Array with: (Array with:10@10 with:100@10)
-               with: (Array with:10@10 with:35@200))
-"
+    "
+     PrinterContext printCirclesIn:  
+        (Array with: (Rectangle left:20 top:20 width:400 height:600)
+               with: (Rectangle left:40 top:40 width:600 height:400)    
+        )
+    "
 
-    "Created: / 07-08-2006 / 12:09:48 / fm"
-    "Modified: / 07-08-2006 / 14:11:17 / fm"
+    "Created: / 07-08-2006 / 11:48:46 / fm"
+    "Modified: / 10-10-2006 / 17:38:28 / cg"
 !
 
-printPolygons: polygons
-
-        "Opens a print dialog and prints the given polygons"
+printLines: pairOfPointsArray
+    "Opens a print dialog and prints the given lines"
 
     | printerInfo printer |
+
     printerInfo := PrintingDialog getPrinterInfo.
     printerInfo isNil ifTrue:[^self].
 
     printer := self fromPrinterInfo: printerInfo.
     [ 
-    printer startPrintJob: 'Polygons'.
-    printer foreground:Color black background:Color white.
-    polygons
-        do:[:aPolygon |
-             aPolygon displayStrokedOn: printer.
-        ].
-    printer endPrintJob.
+        printer startPrintJob: 'Lines'.
+        printer foreground:Color black background:Color white.
+        pairOfPointsArray
+            do:[:pairOfPoints |                 
+                 printer displayLineFrom: (pairOfPoints at:1)  to: (pairOfPoints at:2).
+            ].
+        printer endPrintJob.
     ] forkAt: 3
 
-"
-    PrinterContext printPolygons:  
-        (Array with: (Polygon vertices:(
-                                Array
-                                    with:10@10
-                                    with:60@10
-                                    with:35@60)))
-"
+    "
+     PrinterContext printLines:  
+        (Array with: (Array with:10@10 with:100@10)
+               with: (Array with:10@10 with:35@200))
+    "
 
     "Created: / 07-08-2006 / 12:09:48 / fm"
     "Modified: / 07-08-2006 / 14:11:17 / fm"
+    "Modified: / 10-10-2006 / 17:38:44 / cg"
 !
 
-printRectangles: rectangles
-
-        "Opens a print dialog and prints the given rectangles"
+printPolygons: polygons
+    "Opens a print dialog and prints the given polygons"
 
     | printerInfo printer |
+
     printerInfo := PrintingDialog getPrinterInfo.
     printerInfo isNil ifTrue:[^self].
 
     printer := self fromPrinterInfo: printerInfo.
     [ 
-    printer startPrintJob: 'Rectangles'.
-    rectangles 
-        do:[:rectangle |
-            printer displayRectangleX: rectangle origin x 
-                    y: rectangle origin y 
-                    width: rectangle width 
-                    height: rectangle height.
-        ].
-    printer endPrintJob.
+        printer startPrintJob: 'Polygons'.
+        printer foreground:Color black background:Color white.
+        polygons
+            do:[:aPolygon |
+                 aPolygon displayStrokedOn: printer.
+            ].
+        printer endPrintJob.
     ] forkAt: 3
 
-"
-            PrinterContext printRectangles:  (Array with: (Rectangle left:20 top:20 width:400 height:600)
-                                                    with: (Rectangle left:40 top:40 width:600 height:400)    )
+    "
+     PrinterContext printPolygons:  
+        (Array with: (Polygon vertices:(
+                                Array
+                                    with:10@10
+                                    with:60@10
+                                    with:35@60)))
+    "
 
-"
-
-    "Created: / 07-08-2006 / 11:40:48 / fm"
+    "Created: / 07-08-2006 / 12:09:48 / fm"
+    "Modified: / 07-08-2006 / 14:11:17 / fm"
+    "Modified: / 10-10-2006 / 17:39:23 / cg"
 !
 
-printStrings: stringAndPositionsArray
-
-        "Opens a print dialog and prints the given strings"
+printRectangles: rectangles
+    "Opens a print dialog and prints the given rectangles"
 
     | printerInfo printer |
+
     printerInfo := PrintingDialog getPrinterInfo.
     printerInfo isNil ifTrue:[^self].
 
     printer := self fromPrinterInfo: printerInfo.
     [ 
-    printer startPrintJob: 'Strings with Position'.
-    printer foreground:Color black background:Color white.
-    stringAndPositionsArray
-        do:[:pairOfPointsAndPosition |                 
-             printer displayString:(pairOfPointsAndPosition at: 1) 
-                        x:(pairOfPointsAndPosition at: 2) x
-                        y:(pairOfPointsAndPosition at: 2) y
-        ].
-    printer endPrintJob.
+        printer startPrintJob: 'Rectangles'.
+        rectangles 
+            do:[:rectangle |
+                printer displayRectangleX: rectangle origin x 
+                        y: rectangle origin y 
+                        width: rectangle width 
+                        height: rectangle height.
+            ].
+        printer endPrintJob.
     ] forkAt: 3
 
-"
-    PrinterContext printStrings:  
+    "
+     PrinterContext printRectangles:  
+        (Array with: (Rectangle left:20 top:20 width:400 height:600)
+               with: (Rectangle left:40 top:40 width:600 height:400)    
+        )
+    "
+
+    "Created: / 07-08-2006 / 11:40:48 / fm"
+    "Modified: / 10-10-2006 / 17:39:51 / cg"
+!
+
+printStrings: stringAndPositionsArray
+    "Opens a print dialog and prints the given strings"
+
+    | printerInfo printer |
+
+    printerInfo := PrintingDialog getPrinterInfo.
+    printerInfo isNil ifTrue:[^self].
+
+    printer := self fromPrinterInfo: printerInfo.
+    [ 
+        printer startPrintJob: 'Strings with Position'.
+        printer foreground:Color black background:Color white.
+        stringAndPositionsArray
+            do:[:pairOfPointsAndPosition |                 
+                 printer displayString:(pairOfPointsAndPosition at: 1) 
+                            x:(pairOfPointsAndPosition at: 2) x
+                            y:(pairOfPointsAndPosition at: 2) y
+            ].
+        printer endPrintJob.
+    ] forkAt: 3
+
+    "
+     PrinterContext printStrings:  
         (Array with: (Array with:'Testing printing with standart method' with:10@10)
                with: (Array with:'Another test string to print' with:80@200))
-"
+    "
 
     "Created: / 07-08-2006 / 12:09:48 / fm"
     "Modified: / 07-08-2006 / 14:11:17 / fm"
-!
-
-printingTestString
-
-^'GetTextExtentPoint Note  
-
-The GetTextExtentPoint function computes the width and height of the specified string of text.
-
-Note  This function is provided only for compatibility with 16-bit versions of Windows. Applications should call the GetTextExtentPoint32 function, which provides more accurate results.
-
-BOOL GetTextExtentPoint(
-  HDC hdc,           // handle to DC
-  LPCTSTR lpString,  // text string
-  int cbString,      // number of characters in string
-  LPSIZE lpSize      // string size
-);
-
-Parameters
-
-hdc
-    [in] Handle to the device context. 
-lpString
-    [in] Pointer to the string that specifies the text. The string does not need to be zero-terminated, since cbString specifies the length of the string. 
-cbString
-    [in] Specifies the length of the string pointed to by lpString.
-
-    Windows 95/98/Me: This value may not exceed 8192. 
-lpSize
-    [out] Pointer to a SIZE structure that receives the dimensions of the string, in logical units. 
-
-Return Values
-
-If the function succeeds, the return value is nonzero.
-
-If the function fails, the return value is zero.
-
-Windows NT/2000/XP: To get extended error information, call GetLastError.
-Remarks
-
-The GetTextExtentPoint function uses the currently selected font to compute the dimensions of the string. The width and height, in logical units, are computed without considering any clipping. Also, this function assumes that the text is horizontal, that is, that the escapement is always 0. This is true for both the horizontal and vertical measurements of the text. Even if using a font specifying a nonzero escapement, this function will not use the angle while computing the text extentthe application must convert it explicitly.
-
-Because some devices kern characters, the sum of the extents of the characters in a string may not be equal to the extent of the string.
-
-The calculated string width takes into account the intercharacter spacing set by the SetTextCharacterExtra function.
-
-Windows 95/98/Me: For compatibility with Windows 3.1, GetTextExtentPoint adds an extra pixel for bold simulation fonts. However, the bold Tahoma font is not simulated, so it does not have any extra pixel. To correct for the extra pixel in a bold simulation font, decrease the length of every substring passed to GetTextExtentPoint by one pixel and add an extra pixel for the entire string.
-
-Windows 95/98/Me: Although GetTextExtentPointW exists on Windows 95/98/Me, it is supported by the Microsoft Layer for Unicode to give more consistent behavior across all Windows operating systems. To use this, you must add certain files to your application, as outlined in Microsoft Layer for Unicode on Windows 95/98/Me Systems.
-Requirements
-
-  Windows NT/2000/XP/Vista: Included in Windows NT 3.1 and later.
-  Windows 95/98/Me: Included in Windows 95 and later.
-  Header: Declared in Wingdi.h; include Windows.h.
-  Library: Use Gdi32.lib.
-  Unicode: Implemented as Unicode and ANSI versions on all platforms. Also supported by Microsoft Layer for Unicode.
-See Also'
-
-    "Created: / 03-08-2006 / 13:22:14 / fm"
-    "Modified: / 07-08-2006 / 14:28:16 / fm"
+    "Modified: / 10-10-2006 / 17:40:07 / cg"
 ! !
 
 !PrinterContext methodsFor:'abort handling'!
@@ -1078,5 +1050,5 @@
 !PrinterContext class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libview2/PrinterContext.st,v 1.4 2006-08-25 10:52:49 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libview2/PrinterContext.st,v 1.5 2006-10-10 15:55:30 cg Exp $'
 ! !