home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / PRINTING.PAK / PRINTING.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  5.6 KB  |  224 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1991, 1995 by Borland International, All Rights Reserved
  4. //  This application displays and prints a Ruler using OWL printer classes.
  5. //----------------------------------------------------------------------------
  6. #include <owl/pch.h>
  7. #include <owl/applicat.h>
  8. #include <owl/framewin.h>
  9. #include <owl/dc.h>
  10. #include <owl/printer.h>
  11. #include <owl/editfile.rh>
  12. #include <winsys/system.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15.  
  16. #define IDM_RULERMENU 100
  17.  
  18. //
  19. // class TWindowPrintout
  20. // ~~~~~ ~~~~~~~~~~~~~~~
  21. class TWindowPrintout : public TPrintout {
  22.   public:
  23.     TWindowPrintout(const char* title, TWindow* window);
  24.  
  25.     void GetDialogInfo(int& minPage, int& maxPage,
  26.                        int& selFromPage, int& selToPage);
  27.     void PrintPage(int page, TRect& rect, unsigned flags);
  28.     void SetBanding(bool b)
  29.     {
  30.       Banding = b;
  31.     }
  32.     bool HasPage(int pageNumber)
  33.     {
  34.       return pageNumber == 1;
  35.     }
  36.  
  37.   protected:
  38.     TWindow* Window;
  39.     bool     Scale;
  40. };
  41.  
  42. TWindowPrintout::TWindowPrintout(const char* title, TWindow* window)
  43. :
  44.   TPrintout(title)
  45. {
  46.   Window = window;
  47.   Scale = true;
  48. }
  49.  
  50. void
  51. TWindowPrintout::PrintPage(int, TRect& rect, unsigned)
  52. {
  53.   // Conditionally scale the DC to the window so the printout will
  54.   // resemble the window
  55.   //
  56.   int    prevMode;
  57.   TSize  oldVExt, oldWExt;
  58.   if (Scale) {
  59.     prevMode = DC->SetMapMode(MM_ISOTROPIC);
  60.     TRect windowSize = Window->GetClientRect();
  61.     DC->SetViewportExt(PageSize, &oldVExt);
  62.     DC->SetWindowExt(windowSize.Size(), &oldWExt);
  63.     DC->IntersectClipRect(windowSize);
  64.     DC->DPtoLP(rect, 2);
  65.   }
  66.  
  67.   // Call the window to paint itself
  68.   Window->Paint(*DC, false, rect);
  69.  
  70.   // Restore changes made to the DC
  71.   if (Scale) {
  72.     DC->SetWindowExt(oldWExt);
  73.     DC->SetViewportExt(oldVExt);
  74.     DC->SetMapMode(prevMode);
  75.   }
  76. }
  77.  
  78. // Do not enable page range in the print dialog since only one page is
  79. // available to be printed
  80. //
  81. void
  82. TWindowPrintout::GetDialogInfo(int& minPage, int& maxPage,
  83.                                int& selFromPage, int& selToPage)
  84. {
  85.   minPage = 0;
  86.   maxPage = 0;
  87.   selFromPage = selToPage = 0;
  88. }
  89.  
  90. //----------------------------------------------------------------------------
  91. // TRulerWin
  92.  
  93. class TRulerWin : public TWindow {
  94.   public:
  95.     TRulerWin(TWindow* parent = 0);
  96.    ~TRulerWin();
  97.  
  98.     void Paint(TDC&, bool, TRect&);
  99.  
  100.     void CmFilePageSetup();
  101.     void CmFilePrint();
  102.  
  103.   private:
  104.     TPrinter* Printer;
  105.  
  106.   DECLARE_RESPONSE_TABLE(TRulerWin);
  107. };
  108.  
  109. DEFINE_RESPONSE_TABLE1(TRulerWin, TWindow)
  110.   EV_COMMAND(CM_FILEPAGESETUP, CmFilePageSetup),
  111.   EV_COMMAND(CM_FILEPRINT, CmFilePrint),
  112. END_RESPONSE_TABLE;
  113.  
  114. TRulerWin::TRulerWin(TWindow* parent)
  115. :
  116.   TWindow(parent)
  117. {
  118.   Printer = new TPrinter;
  119. }
  120.  
  121. TRulerWin::~TRulerWin()
  122. {
  123.   delete Printer;
  124. }
  125.  
  126. void
  127. TRulerWin::CmFilePageSetup()    // Execute File:Page-setup command
  128. {
  129.   if (Printer)
  130.     Printer->Setup(this);
  131. }
  132.  
  133. void
  134. TRulerWin::CmFilePrint()          // Execute File:Print command
  135. {
  136.   if (Printer) {
  137.     TWindowPrintout printout("Ruler Test", this);
  138.     //
  139.     // Using OWL's banding support under Win95 _when_ the EXE's marked 4.0
  140.     // (under Options|Project|Linker|General) will cause the print to fail.
  141.     //
  142.     if(!TSystem::IsWin95())
  143.       printout.SetBanding(true);
  144.     Printer->Print(this, printout, true);
  145.   }
  146. }
  147.  
  148. // Paint window's contents on any dc, screen or printer or whatever...
  149. //
  150. void
  151. TRulerWin::Paint(TDC& dc, bool , TRect&)
  152. {
  153.   const int UnitsPerInch = 100;                 // Display scale units per inch
  154.   const int NumInches = 8;                      // Size of ruler in inches
  155.   const int MarkFraction = 4;                   // Power of 2 to use for marks
  156.   const int MarksPerInch = (1 << MarkFraction); // Number of markers for each inch
  157.   const int LargeMarkerSize = UnitsPerInch/3;   // Size of large, labeled markers
  158.   const int SmallMarkerSize = UnitsPerInch/8;   // Size of smallest markers
  159.   const int MarkStep = (LargeMarkerSize-SmallMarkerSize) / MarkFraction;
  160.  
  161.   dc.SaveDC();
  162.   dc.SetMapMode(MM_LOENGLISH);
  163.   int x1 = 0; //0.50 * UnitsPerInch;
  164.   int y1 = x1;
  165.   int x2 = x1 + NumInches * UnitsPerInch;
  166.   int y2 = y1 + 1*UnitsPerInch;
  167.   dc.Rectangle(x1, -y1, x2, -y2);
  168.  
  169.   // Draw marks
  170.   //
  171.   y2 = y1 + SmallMarkerSize;
  172.   for (int marksPerInch = MarksPerInch; marksPerInch > 0; marksPerInch /= 2) {
  173.     for (int i = 0; i <= NumInches*marksPerInch - 1; i++) {
  174.       int x = x1 + (i * UnitsPerInch) / marksPerInch;
  175.       dc.MoveTo(x, -y1);
  176.       dc.LineTo(x, -y2);
  177.     }
  178.     y2 += MarkStep;
  179.   }
  180.  
  181.   // Label the inch marks
  182.   //
  183.   y2 = y1 + LargeMarkerSize; // - text height
  184.   for (int i = 1;  i <= NumInches - 1; i++) {
  185.     int x = x1 + i * UnitsPerInch;  // - ( text width + 20%)
  186.     char s[3];
  187.     itoa(i, s, 10);
  188.     dc.TextOut(TPoint(x, -y2), s, strlen(s));
  189.   }
  190.  
  191.   dc.RestoreDC();
  192. }
  193.  
  194.  
  195. //
  196. // class TRulerApp
  197. // ~~~~~ ~~~~~~~~~
  198. class TRulerApp : public TApplication {
  199.   public:
  200.     TRulerApp()
  201.     :
  202.       TApplication()
  203.     {
  204.     }
  205.  
  206.     void InitMainWindow() {
  207.       EnableCtl3d();
  208.       TFrameWindow* frame = new TFrameWindow(0, "Ruler Printing Demonstration",
  209.         new TRulerWin);
  210.       frame->Attr.X = GetSystemMetrics(SM_CXSCREEN) / 8;
  211.       frame->Attr.Y = GetSystemMetrics(SM_CYSCREEN) / 8;
  212.       frame->Attr.H = frame->Attr.Y * 6;
  213.       frame->Attr.W = frame->Attr.X * 6;
  214.       frame->AssignMenu(IDM_RULERMENU);
  215.       SetMainWindow(frame);
  216.     }
  217. };
  218.  
  219. int
  220. OwlMain(int /*argc*/, char* /*argv*/ [])
  221. {
  222.   return TRulerApp().Run();
  223. }
  224.