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

  1. //----------------------------------------------------------------------------
  2. //  Project ApxSdi
  3. //  Borland International
  4. //  Copyright ⌐ 1996. All Rights Reserved.
  5. //
  6. //  SUBSYSTEM:    ApxSdi Application
  7. //  FILE:         apxprint.cpp
  8. //  AUTHOR:
  9. //
  10. //  OVERVIEW
  11. //  ~~~~~~~~
  12. //  Source file for implementation of Printing.
  13. //
  14. //----------------------------------------------------------------------------
  15. #include <owl/pch.h>
  16. #include <owl/editfile.h>
  17. #include "apxprint.h"
  18.  
  19.  
  20.  
  21. void TApxPrintout::GetDialogInfo(int& minPage, int& maxPage,
  22.                                  int& selFromPage, int& selToPage)
  23. {
  24.   // Calculate the total number of pages in the document. TotalPages is
  25.   // initialized in INT_MAX in the event that we cannot determine the
  26.   // number for ourselves.
  27.   //
  28.   TPrintDC* printDC = new TPrintDC( Printer->GetSetup().GetDriverName(),
  29.                                     Printer->GetSetup().GetDeviceName(),
  30.                                     Printer->GetSetup().GetOutputName(),
  31.                                     Printer->GetSetup().GetDevMode() );
  32.  
  33.   TEditFile* efWindow = TYPESAFE_DOWNCAST( Window, TEditFile );
  34.  
  35.   if( printDC && efWindow )  {
  36.     TSize pageSize( printDC->GetDeviceCaps(HORZRES),
  37.                     printDC->GetDeviceCaps(VERTRES) );
  38.  
  39.     TEXTMETRIC tm;
  40.     printDC->GetTextMetrics( tm );
  41.     int TextHeight = tm.tmHeight + tm.tmInternalLeading;
  42.  
  43.     int LinesPerPage = pageSize.cy / TextHeight;
  44.     int TotalLines = efWindow->GetNumLines();
  45.     TotalPages = TotalLines / LinesPerPage + 1; //Add one to account for any roundoff.
  46.     if(TotalPages <= 0)
  47.       TotalPages = 1;
  48.   }
  49.  
  50.   delete printDC;
  51.  
  52.   minPage = 1;
  53.   maxPage = TotalPages;
  54.   selFromPage = 0;
  55.   selToPage = 0;
  56. }
  57.  
  58.  
  59.  
  60. void TApxPrintout::BeginPage(TRect& clientR)
  61. {
  62.   TScreenDC screenDC;
  63.   TSize screenRes(screenDC.GetDeviceCaps(LOGPIXELSX),
  64.           screenDC.GetDeviceCaps(LOGPIXELSY));
  65.   TSize printRes(DC->GetDeviceCaps(LOGPIXELSX),
  66.            DC->GetDeviceCaps(LOGPIXELSY));
  67.  
  68.   // Temporarily change the window size (so any WM_PAINT queries on the total window size (GetClientRect) is
  69.   // the window size for the WM_PAINT of the window and the printer page size when Paint is called from
  70.   // PrintPage. Notice, we don't use AdjustWindowRect because its harder and not accurate.  Instead we
  71.   // compute the difference (in pixels) between the client window and the frame window.  This difference
  72.   // is then added to the clientRect to compute the new frame window size for SetWindowPos.
  73.   //
  74.   clientR = Window->GetClientRect();
  75.   Window->MapWindowPoints(HWND_DESKTOP, (TPoint*)&clientR, 2);
  76.  
  77.   // Compute extra X and Y pixels to bring a client window dimensions to equal the frame window.
  78.   //
  79.   OrgR = Window->GetWindowRect();
  80.   int adjX = OrgR.Width() - clientR.Width();
  81.   int adjY = OrgR.Height() - clientR.Height();
  82.  
  83.   // Conditionally scale the DC to the window so the printout will resemble the window.
  84.   //
  85.   if (Scale) {
  86.     clientR = Window->GetClientRect();
  87.     PrevMode = DC->SetMapMode(MapMode);
  88.     DC->SetViewportExt(PageSize, &OldVExt);
  89.  
  90.     // Scale window to logical page size (assumes left & top are 0)
  91.     //
  92.     clientR.right = MulDiv(PageSize.cx, screenRes.cx, printRes.cx);
  93.     clientR.bottom = MulDiv(PageSize.cy, screenRes.cy, printRes.cy);
  94.  
  95.     DC->SetWindowExt(clientR.Size(), &OldWExt);
  96.   }
  97.  
  98.   // Compute the new size of the window based on the printer DC dimensions.
  99.   // Resize the window, notice position, order, and redraw are not done the window size changes but the user
  100.   // doesn't see any visible change to the window.
  101.   //
  102.   Window->SetRedraw(false);
  103.   Window->SetWindowPos(0, 0, 0, clientR.Width() + adjX, clientR.Height() + adjY,
  104.                        SWP_NOMOVE | SWP_NOREDRAW | SWP_NOZORDER| SWP_NOACTIVATE);
  105. }
  106.  
  107.  
  108. void TApxPrintout::PrintPage(int page, TRect& bandRect, unsigned)
  109. {
  110.   TRect clientR;
  111.  
  112.   BeginPage(clientR);
  113.  
  114.   if (Scale)
  115.     DC->DPtoLP(bandRect, 2);
  116.  
  117.   // Change the printer range to this current page.
  118.   //
  119.   TPrintDialog::TData& printerData = Printer->GetSetup();
  120.   int fromPg = printerData.FromPage;
  121.   int toPg = printerData.ToPage;
  122.  
  123.   printerData.FromPage = page;
  124.   printerData.ToPage = page;
  125.  
  126.   // Call the window to paint itself to the printer DC.
  127.   //
  128.   Window->Paint(*DC, false, bandRect);
  129.  
  130.   printerData.FromPage = fromPg;
  131.   printerData.ToPage = toPg;
  132.  
  133.   if (Scale)
  134.     DC->LPtoDP(bandRect, 2);
  135.  
  136.   EndPage();
  137. }
  138.  
  139.  
  140. void TApxPrintout::EndPage()
  141. {
  142.   // Resize to original window size, no one's the wiser.
  143.   //
  144.   Window->SetWindowPos(0, 0, 0, OrgR.Width(), OrgR.Height(),
  145.                SWP_NOMOVE | SWP_NOREDRAW | SWP_NOZORDER| SWP_NOACTIVATE);
  146.   Window->SetRedraw(true);
  147.  
  148.   // Restore changes made to the DC
  149.   //
  150.   if (Scale) {
  151.     DC->SetWindowExt(OldWExt);
  152.     DC->SetViewportExt(OldVExt);
  153.     DC->SetMapMode(PrevMode);
  154.   }
  155. }
  156.  
  157.  
  158. bool TApxPrintout::HasPage(int pageNumber)
  159. {
  160.   TPrintDialog::TData& printerData = Printer->GetSetup();
  161.  
  162.   return pageNumber >= printerData.MinPage && pageNumber <= printerData.MaxPage;
  163. }
  164.