home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / msysjour / vol04 / 02b / project / classes / printer.cls < prev    next >
Text File  |  1988-09-01  |  5KB  |  219 lines

  1. /* General Printer Class to output to a printer
  2.    Mark Solinksi, The Whitewater Group.
  3. */!!
  4.  
  5. inherit(Object, #Printer, #(hPrintDC  /* handle to the printer display context */
  6. docName /* name of document */
  7. device  /* current printer */
  8. driver  /* driver file for printer */
  9. port    /* Printer port */
  10. ), 2, nil)!!
  11.  
  12. now(PrinterClass)!!
  13.  
  14. /* new send an init to instance of self */
  15. Def new(self)
  16. { ^init(new(self:Behavior))
  17. }!!
  18.  
  19. now(Printer)!!
  20.  
  21. /* Change the current printer position to be the 
  22.   receiver point using the printer handle to a device 
  23.   context. */
  24. Def moveTo(self, thePoint)
  25. { moveTo(thePoint, hPrintDC);
  26. }!!
  27.  
  28. /* Draw a line from the current position up to, but 
  29.   not including, self, using the printer handle to a 
  30.   display context. Reset the current printer position 
  31.   to be the receiver Point. */
  32. Def lineTo(self, thePoint)
  33. { lineTo(thePoint, hPrintDC);
  34. }!!
  35.  
  36. /* End of File. Return a nil if this command is 
  37.   unsuccessful */
  38. Def eof(self)
  39. { if newPage(self) > 0
  40.   then endDoc(self);
  41.     ^deleteContext(self);
  42.   else ^nil;
  43.   endif;
  44. }!!
  45.  
  46. /* generic escape which returns a point */
  47. Def escapePoint(self, prtEscape | ans, lpPt, pt, struct)
  48. { ans := escape(self, prtEscape, 0, 0,
  49.     lpPt:= lP(struct := "    "));
  50.   pt := pointAt(lpPt);
  51.   freeHandle(struct);
  52.   if ans > 0
  53.   then ^pt
  54.   endif;
  55.   ^nil
  56. }!!
  57.  
  58. /* Turn draft mode off */
  59. Def draftModeOff(self | ans, aStruct)
  60. { aStruct := new(Struct, 2);
  61.   putWord(aStruct, 0, 0);
  62.   ans := escape(self, DRAFTMODE, 2, lP(aStruct), 0);
  63.   freeHandle(aStruct);
  64.   ^ans;
  65. }!!
  66.  
  67. /* Turn draft mode on */
  68. Def draftModeOn(self | ans, aStruct)
  69. { aStruct := new(Struct, 2);
  70.   putWord(aStruct, 1, 0);
  71.   ans := escape(self, DRAFTMODE, 2, lP(aStruct), 0);
  72.   freeHandle(aStruct);
  73.   ^ans;
  74. }!!
  75.  
  76. /* create a new device context for the printer */
  77. Def  createDC(self)
  78. { hPrintDC := Call CreateDC(lP(driver), lP(device), lP(port), 0);
  79.   freeHandle(driver);
  80.   freeHandle(device);
  81.   freeHandle(port);
  82.   if hPrintDC > 0
  83.   then ^hPrintDC
  84.   else ^hPrintDC := nil
  85.   endif;
  86. }!!
  87.  
  88. /* restore the Device Context. */
  89. Def  restoreDC(self, nSavedDC)
  90. { ^Call RestoreDC(hPrintDC, nSavedDC);
  91. }!!
  92.  
  93. /* save the DC */
  94. Def  saveDC(self)
  95. { ^Call SaveDC(hPrintDC);
  96. }!!
  97.  
  98. /* Printer Escape QUERYESCSUPPORT */
  99. Def  queryEscSupport(self, escNum | ans, aStruct)
  100. { aStruct := new(Struct, 2);
  101.   putWord(aStruct, escNum, 0);
  102.   ans := escape(self, QUERYESCSUPPORT, 2, lP(aStruct), 0);
  103.   freeHandle(aStruct);
  104.   if not(ans = 0)
  105.   then ^true
  106.   endif;
  107.   ^nil
  108. }!!
  109.  
  110. /* Printer Escape NEXTBAND */
  111. Def  nextBand(self | rect, ans, bandRect)
  112. { ans := escape(self, NEXTBAND, 0, 0, lP(rect := &(0, 0, 0, 0)));
  113.   bandRect := getData(rect);
  114.   freeHandle(rect);
  115.   if ans > 0
  116.   then ^bandRect
  117.   endif;
  118.   ^nil
  119. }!!
  120.  
  121. /* Printer Escape GETSCALINGFACTOR */
  122. Def  getScalingFactor(self)
  123. { ^escapePoint(self, GETSCALINGFACTOR);
  124. }!!
  125.  
  126. /* Printer Escape NEWFRAME */
  127. Def newFrame(self)
  128. { ^escape(self, NEWFRAME, 0, 0, 0);
  129. }!!
  130.  
  131. /* Printer Escape GETPRINTINGOFFSET */
  132. Def  getPrintingOffset(self)
  133. { ^escapePoint(self, GETPRINTINGOFFSET);
  134. }!!
  135.  
  136. /* delete the printer device context */
  137. Def deleteContext(self)
  138. { ^Call DeleteDC(hPrintDC);
  139. }!!
  140.  
  141. /* Get the printer parameters from the WIN.INI file */
  142. Def getPrinterParms(self | aStr, aStrm, topic, subject, extra)
  143. { aStr := getProfileString(self, "windows","device", "", 64);
  144.   if size(aStr) > 0
  145.   then aStrm := streamOver(aStr);
  146.     device := leftJustify(word(aStrm,','));
  147.     driver := leftJustify(word(aStrm,','));
  148.     port := leftJustify(word(aStrm,','));
  149.     ^device;
  150.   endif;
  151.   ^nil
  152. }!!
  153.  
  154. /* Return the printer display context for self. */
  155. Def getContext(self)
  156. { ^hPrintDC;
  157. }!!
  158.  
  159. /* Printer Escape GETPHYSPAGESIZE */
  160. Def  getPhysPageSize(self)
  161. { ^escapePoint(self, GETPHYSPAGESIZE);
  162. }!!
  163.  
  164. /* Printer Escape STARTDOC */
  165. Def  startDoc(self | dName, ans)
  166. { ans := escape(self, STARTDOC, size(docName), lP(docName), 0);
  167.   freeHandle(docName);
  168.   if ans > 0
  169.   then ^ans
  170.   endif;
  171.   ^nil
  172. }!!
  173.  
  174. /* set the document name to the string */
  175. Def  setDocName(self, aName)
  176. { docName := aName;
  177. }!!
  178.  
  179. /* Escape GETCOLORTABLE procedure.  Returns a long 
  180.   integer value corresponding to the RGB color value 
  181.   for the given table entry or nil if unsuccessful */
  182. Def  getColorTable(self, colorTableIndex | idx, rgbStruct, ans, rgb)
  183. { idx := new(Struct, 2);
  184.   putWord(idx, colorTableIndex, 0);
  185.   rgbStruct := new(Struct, 4);
  186.   ans := escape(self, GETCOLORTABLE, 0, lP(idx), lP(rgbStruct));
  187.   freeHandle(idx);
  188.   rgb := getData(rgbStruct);
  189.   freeHandle(rgbStruct);
  190.   if ans > 0
  191.   then ^longAt(rgb, 0);
  192.   endif;
  193.   ^nil;
  194. }!!
  195.  
  196. /* Escape FLUSHOUTPUT procedure */
  197. Def  flushOutput(self)
  198. { ^escape(self, FLUSHOUTPUT, 0, 0, 0);
  199. }!!
  200.  
  201. /* Escape ENDDOC procedure */
  202. Def  endDoc(self)
  203. { ^escape(self, ENDDOC, 0, 0, 0);
  204. }!!
  205.  
  206. /* Escape ABORTDOC procedure */
  207. Def  abortDoc(self)
  208. { ^escape(self, ABORTDOC, 0, 0, 0);
  209. }!!
  210.  
  211. /* Escape Code call to Windows */
  212. Def  escape(self, nEscCode, count, inData, outData | ret)
  213. { ret := Call Escape(errorIfNil(hPrintDC, #printerError), nEscCode, count, inData, outData);
  214.   if ret = 0
  215.   then ^nil
  216.   endif;
  217.   ^ret
  218. }!!
  219.