home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / VISUAL_B / PROGRAMS / EM_EDIT / SAMPLES.ZIP / OWL / APXPRINT.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-05  |  5.3 KB  |  174 lines

  1. /*  Main emedit
  2.     Early Morning Software
  3.     Copyright ⌐ 1994. All Rights Reserved.
  4.  
  5.     SUBSYSTEM:    emedit.exe Application
  6.     FILE:         APXPrint.CPP
  7.     AUTHOR:       Ted Stockwell
  8.  
  9.  
  10.     OVERVIEW
  11.     ========
  12.     Source file for implementation of Printing.
  13. */
  14.  
  15.  
  16. #include <owl\owlpch.h>
  17. #include <owl\listbox.h>
  18. #include <owl\preview.h>
  19. #pragma hdrstop
  20.  
  21. #include <mdtchldc.h>
  22. #include "apxprint.h"
  23.  
  24.  
  25. // Do not enable page range in the print dialog since only one page is
  26. // available to be printed
  27. //
  28. void APXPrintOut::GetDialogInfo (int& minPage, int& maxPage, int& selFromPage, int& selToPage)
  29. {
  30.     minPage = maxPage = 0;
  31.     selFromPage = selToPage = 0;
  32. }
  33.  
  34.  
  35. void APXPrintOut::BeginPrinting ()
  36. {
  37.     TRect clientR;
  38.  
  39.     BeginPage(clientR);
  40.  
  41.     TFrameWindow *fWindow = TYPESAFE_DOWNCAST(Window, TFrameWindow);
  42.  
  43.     HFONT   hFont = (HFONT)fWindow->GetClientWindow()->GetWindowFont();
  44.     TFont   font("Arial", -12);
  45.     if (hFont == 0)
  46.       DC->SelectObject(font);
  47.     else
  48.       DC->SelectObject(TFont(hFont));
  49.  
  50.     TEXTMETRIC  tm;
  51.     int fHeight = (DC->GetTextMetrics(tm) == TRUE) ? tm.tmHeight + tm.tmExternalLeading : 10;
  52.  
  53.     DC->RestoreFont();
  54.  
  55.     // How many lines of this font can we fit on a page.
  56.     int linesPerPage = MulDiv(clientR.Height(), 1, fHeight);
  57.  
  58.     TPrintDialog::TData &printerData = Printer->GetSetup();
  59.  
  60.     int maxPg = 1;
  61.  
  62.     // Get the client class window (this is the contents we're going to print).
  63.     { long numlines;
  64.       emeditChildClient* cClient = TYPESAFE_DOWNCAST(fWindow->GetClientWindow(), emeditChildClient);
  65.       cClient->editor->GetPropCount( numlines );
  66.       maxPg = ((numlines / linesPerPage) + 1.0);
  67.     }
  68.  
  69.     // Compute the number of pages to print.
  70.     printerData.MinPage = 1;
  71.     printerData.MaxPage = maxPg;
  72.  
  73.     EndPage();
  74.  
  75.     TPrintout::BeginPrinting();
  76. }
  77.  
  78.  
  79. void APXPrintOut::BeginPage (TRect &clientR)
  80. {
  81.     TScreenDC screenDC;
  82.     TSize screenRes(screenDC.GetDeviceCaps(LOGPIXELSX),
  83.                     screenDC.GetDeviceCaps(LOGPIXELSY));
  84.     TSize printRes(DC->GetDeviceCaps(LOGPIXELSX),
  85.                    DC->GetDeviceCaps(LOGPIXELSY));
  86.  
  87.     // Temporarily change the window size (so any WM_PAINT queries on the total window size (GetClientRect) is
  88.     // the window size for the WM_PAINT of the window and the printer page size when Paint is called from
  89.     // PrintPage. Notice, we don't use AdjustWindowRect because its harder and not accurate.  Instead we
  90.     // compute the difference (in pixels) between the client window and the frame window.  This difference
  91.     // is then added to the clientRect to compute the new frame window size for SetWindowPos.
  92.     clientR = Window->GetClientRect();
  93.     Window->MapWindowPoints(HWND_DESKTOP, (TPoint*)&clientR, 2);
  94.  
  95.     // Compute extra X and Y pixels to bring a client window dimensions to equal the frame window.
  96.     OrgR = Window->GetWindowRect();
  97.     int adjX = OrgR.Width() - clientR.Width();
  98.     int adjY = OrgR.Height() - clientR.Height();
  99.     
  100.     // Conditionally scale the DC to the window so the printout will resemble the window.
  101.     if (Scale) {
  102.         clientR = Window->GetClientRect();
  103.         PrevMode = DC->SetMapMode(MapMode);
  104.         DC->SetViewportExt(PageSize, &OldVExt);
  105.  
  106.         // Scale window to logical page size (assumes left & top are 0)
  107.         clientR.right = MulDiv(PageSize.cx, screenRes.cx, printRes.cx);
  108.         clientR.bottom = MulDiv(PageSize.cy, screenRes.cy, printRes.cy);
  109.  
  110.         DC->SetWindowExt(clientR.Size(), &OldWExt);
  111.     }
  112.  
  113.     // Compute the new size of the window based on the printer DC dimensions.    
  114.     // Resize the window, notice position, order, and redraw are not done the window size changes but the user
  115.     // doesn't see any visible change to the window.
  116.     Window->SetRedraw(FALSE);
  117.     Window->SetWindowPos(0, 0, 0, clientR.Width() + adjX, clientR.Height() + adjY,
  118.                          SWP_NOMOVE | SWP_NOREDRAW | SWP_NOZORDER| SWP_NOACTIVATE);
  119. }
  120.  
  121.  
  122. void APXPrintOut::PrintPage (int page, TRect& bandRect, unsigned)
  123. {
  124.     TRect clientR;
  125.  
  126.     BeginPage(clientR);
  127.  
  128.     if (Scale)
  129.         DC->DPtoLP(bandRect, 2);
  130.  
  131.     // Change the printer range to this current page.
  132.     TPrintDialog::TData& printerData = Printer->GetSetup();
  133.     int fromPg = printerData.FromPage;
  134.     int toPg = printerData.ToPage;
  135.  
  136.     printerData.FromPage = page;
  137.     printerData.ToPage = page;
  138.  
  139.     // Call the window to paint itself to the printer DC.
  140.     Window->Paint(*DC, FALSE, bandRect);
  141.  
  142.     printerData.FromPage = fromPg;
  143.     printerData.ToPage = toPg;
  144.  
  145.     if (Scale)
  146.         DC->LPtoDP(bandRect, 2);
  147.  
  148.     EndPage();
  149. }
  150.  
  151.  
  152. void APXPrintOut::EndPage ()
  153. {
  154.     // Resize to original window size, no one's the wiser.
  155.     Window->SetWindowPos(0, 0, 0, OrgR.Width(), OrgR.Height(),
  156.                          SWP_NOMOVE | SWP_NOREDRAW | SWP_NOZORDER| SWP_NOACTIVATE);
  157.     Window->SetRedraw(TRUE);
  158.  
  159.     // Restore changes made to the DC
  160.     if (Scale) {
  161.         DC->SetWindowExt(OldWExt);
  162.         DC->SetViewportExt(OldVExt);
  163.         DC->SetMapMode(PrevMode);
  164.     }
  165. }
  166.  
  167.  
  168. BOOL APXPrintOut::HasPage (int pageNumber)
  169. {
  170.     TPrintDialog::TData &printerData = Printer->GetSetup();
  171.  
  172.     return (pageNumber >= printerData.MinPage) && (pageNumber <= printerData.MaxPage);
  173. }
  174.