Programmer's Reference


A complete example

The following example prints two pages of text, diagonal lines, and filled circles. The printer where the pages are being sent is prompted for, and if there are no printers available or if no printer is chosen, no printing is done.

Object subclass: PrintingExample
   instanceVariableNames: 'done gc fontStruct'
   classVariableNames: ''
   poolDictionaries: 'CwConstants CgConstants'
print
"Print two similar pages of text and graphics.
 
NOTE: This call must run in the UI process because it
  runs its own event loop until printing is completed."
 
"To test, execute the following:
   PrintingExample new print"
    | prompter displayName display jobAttributes printerShell |
 
    done := false.
    CgDisplay allPrinterDisplayNames isEmpty
        ifTrue: [^System errorMessage: 'There are no available printers.'].
    prompter := CwPrinterPrompter new.
    (displayName := prompter prompt) isNil
         ifTrue: [^nil].
    jobAttributes := prompter jobAttributes.
    display := CwAppContext default
         openDisplay: displayName.
    display isNil
        ifTrue: [^nil].
    printerShell := CwPrinterShell
         appCreateShell: self class name
         applicationClass: nil
         display: display
         argBlock: [:w | w jobAttributes: jobAttributes].
    printerShell
         addCallback: XmNmapCallback
         receiver: self
         selector: #printerShellMap:clientData:callData:
         clientData: nil;
         addCallback: XmNexposeCallback
         receiver: self
         selector: #printerShellExpose:clientData:callData:
         clientData: nil;
         addCallback: XmNdestroyCallback
         receiver: self
         selector: #printerShellDestroy:clientData:callData:
         clientData: nil.
    printerShell realizeWidget.
"Printing starts on return to the event loop - so force event loop here."
    [done] whileFalse: [CwAppContext default readAndDispatch].
 
printerShellMap: printerShell clientData: clientData callData: callData
"Create a graphics context, load a font, begin the print job, 
 and start the first page."
   | fontNames display |
   gc isNil
       ifTrue: [
            "Create a graphics context."
            gc := printerShell window
            createGC: None
            values: nil.
"Load a font (the first courier font) for use in drawing strings."
            display := printerShell display.
            fontNames := display
                 listFonts: '*courier*'
                 maxnames: 1.
            fontStruct := fontNames isEmpty
                 ifTrue: [display defaultFontStruct]
                 ifFalse: [display loadQueryFont: fontNames first].
         ].
     printerShell
         startJob;
         startPage.
 
 
printerShellDestroy: printerShell clientData: aStream callData: callData
"Free resources allocated for printing."
    done := true.
    gc freeGC.
    fontStruct = printerShell display defaultFontStruct
         ifFalse: [fontStruct freeFont].
    printerShell display close.
 
 
printerShellExpose: printerShell clientData: clientData callData: callData
"Process the current page. If all pages are processed, end the print job
 and destroy the printer shell."
   | pageNum |
   gc setFont: fontStruct fid.
   pageNum := callData pageNumber.
   self printPage: pageNum on: printerShell.
   printerShell endPage.
"This example only prints two pages."
   pageNum < 2
        ifTrue: [
             printerShell startPage.
        ]
        ifFalse: [
             printerShell
                  endJob;
                  destroyWidget.
        ].
 
 
printPage: pageNum on: printerShell
"Print some text and graphics on a printerShell."
    | halfWidth halfHeight fontHeight lineNumber |
 
    halfWidth := printerShell extent x // 2.
    halfHeight := printerShell extent y // 2.
    lineNumber := 1.
    fontHeight := fontStruct height.
    0 to: halfHeight by: 100 do: [:y |
         printerShell window
              drawLine: gc
              x1: 0
              y1: 0
              x2: halfWidth
              y2: y
    ].
    printerShell y + fontHeight to: halfHeight + fontHeight 
                       by: fontHeight do: [:y |
         printerShell window
              drawString: gc
              x: 40
              y: y
              string: 'Line: ', lineNumber printString, ', page: ' , 
                                pageNum printString.
         lineNumber := lineNumber + 1.
    ].
    printerShell window
         fillArc: gc
         x: halfWidth
         y: halfHeight
         width: halfWidth // 2
         height: halfHeight // 2
         angle1: 0 * 64
         angle2: 360 * 64.


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]