home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / Borland / Cplus45 / BC45 / EXPERT.PAK / APXPREV.CPP < prev    next >
C/C++ Source or Header  |  1995-08-29  |  11KB  |  335 lines

  1. ##@QUERY_PRJ_NAME [[Project]]
  2. /*  Project [[Project]]
  3. ##@QUERY_PRJ_COMPANY [[CompanyName]] 2
  4. ##@QUERY_PRJ_COPYRIGHT [[Copyright]] 2
  5. ##\\Copyright Copyright 2
  6.     [[CompanyName]]
  7.     [[Copyright]]
  8.  
  9. ##@QUERY_TARGET_NAME [[Target]]
  10.     SUBSYSTEM:    [[Target]] Application
  11.     FILE:         APXPrev.CPP
  12. ##@QUERY_PRJ_USER [[Author]]
  13.     AUTHOR:       [[Author]]
  14.  
  15.  
  16.     OVERVIEW
  17.     ========
  18.     Source file for implementation of Print Preview.      
  19. */
  20.  
  21.  
  22. #include <owl\owlpch.h>
  23. #pragma hdrstop
  24.  
  25. #include "apxprev.h"
  26.  
  27. ##<<TApplication QUERY_FILENAME_CPP [[FileName]]
  28. #include "[[FileName]].rh"
  29.  
  30.  
  31. //{{PreviewWindow Implementation}}
  32.  
  33. DEFINE_RESPONSE_TABLE1(PreviewWindow, TDecoratedFrame)
  34.     EV_COMMAND_ENABLE(APX_PPR_PREVIOUS, PPR_PreviousEnable),
  35.     EV_COMMAND_ENABLE(APX_PPR_NEXT, PPR_NextEnable),
  36.     EV_COMMAND(APX_PPR_PREVIOUS, PPR_Previous),
  37.     EV_COMMAND(APX_PPR_NEXT, PPR_Next),
  38.     EV_COMMAND(APX_PPR_ONEUP, PPR_OneUp),
  39.     EV_COMMAND_ENABLE(APX_PPR_TWOUP, PPR_TwoUpEnable),
  40.     EV_COMMAND(APX_PPR_TWOUP, PPR_TwoUp),
  41.     EV_COMMAND(APX_PPR_DONE, PPR_Done),
  42. ##@QUERY_APPL_MODEL == VALUE_MDI 2
  43.     EV_COMMAND(CM_FILEPRINT, CmPrint),
  44.     EV_COMMAND_ENABLE(CM_FILEPRINT, CmPrintEnable),
  45. //{{PreviewWindowRSP_TBL_BEGIN}}
  46. //{{PreviewWindowRSP_TBL_END}}
  47. END_RESPONSE_TABLE;
  48.  
  49.  
  50. PreviewWindow::PreviewWindow (TWindow *parentWindow, TPrinter *printer, TWindow* currWindow, const char far* title, TLayoutWindow* client) :
  51.                  TDecoratedFrame(parentWindow, title, client)
  52. {
  53.     CurrWindow = currWindow;
  54.     Printer = printer;
  55.     Client = client;
  56.     Page1 = 0;
  57.     Page2 = 0;
  58.  
  59.     TPrintDialog::TData& data = Printer->GetSetup();
  60.     PrnDC = new TPrintDC(data.GetDriverName(),
  61.                          data.GetDeviceName(),
  62.                          data.GetOutputName(),
  63.                          data.GetDevMode());
  64.  
  65.     PrintExtent = new TSize(PrnDC->GetDeviceCaps(HORZRES), PrnDC->GetDeviceCaps(VERTRES));
  66.     Printout = new APXPrintOut(Printer, "Print Preview", currWindow, true);
  67.  
  68.     SetBkgndColor(::GetSysColor(COLOR_APPWORKSPACE));
  69.  
  70. ##--BEGIN-- @QUERY_APPL_COMMENT == VALUE_VERBOSE
  71.     //
  72.     // Create default toolbar New and associate toolbar buttons with commands.
  73.     //
  74. ##--END-- @QUERY_APPL_COMMENT == VALUE_VERBOSE
  75.     PreviewSpeedBar = new TControlBar(this);
  76.     PreviewSpeedBar->Insert(*new TButtonGadget(APX_PPR_PREVIOUS, APX_PPR_PREVIOUS, TButtonGadget::Command, true));
  77.     PreviewSpeedBar->Insert(*new TButtonGadget(APX_PPR_NEXT, APX_PPR_NEXT, TButtonGadget::Command, true));
  78.     PreviewSpeedBar->Insert(*new TSeparatorGadget(6));
  79.     PreviewSpeedBar->Insert(*new TButtonGadget(APX_PPR_ONEUP, APX_PPR_ONEUP, TButtonGadget::Exclusive, true, TButtonGadget::Down));
  80.     PreviewSpeedBar->Insert(*new TButtonGadget(APX_PPR_TWOUP, APX_PPR_TWOUP, TButtonGadget::Exclusive, true));
  81.     PreviewSpeedBar->Insert(*new TSeparatorGadget(12));
  82.     PreviewSpeedBar->Insert(*new TTextGadget(APX_PPR_CURRPAGE, TGadget::Recessed, TTextGadget::Left, 10, "Page 1"));
  83.     PreviewSpeedBar->Insert(*new TSeparatorGadget(20));
  84.     PreviewSpeedBar->Insert(*new TButtonGadget(CM_FILEPRINT, CM_FILEPRINT, TButtonGadget::Command, true));
  85.     PreviewSpeedBar->Insert(*new TSeparatorGadget(20));
  86.     PreviewSpeedBar->Insert(*new TButtonGadget(APX_PPR_DONE, APX_PPR_DONE, TButtonGadget::Command, true));
  87.     Insert(*PreviewSpeedBar, TDecoratedFrame::Top);
  88.  
  89. ##:@QUERY_APPL_COMMENT == VALUE_VERBOSE
  90.     // We want a window that cannot be sized, maximized, or minimized.
  91.     Attr.Style = (WS_VISIBLE | WS_POPUP);
  92.  
  93. ##:@QUERY_APPL_COMMENT == VALUE_VERBOSE
  94.     // Don't show the border of the preview window.
  95.     Attr.X = 0;
  96.     Attr.Y = -1;
  97.     Attr.W = Parent->GetClientRect().Width();
  98.     Attr.H = Parent->GetClientRect().Height() + 1;
  99.     parentWindow->MapWindowPoints(HWindow, (TPoint *)&(Attr.X), 1);
  100. }
  101.  
  102.  
  103. PreviewWindow::~PreviewWindow ()
  104. {
  105.     delete Page1;
  106.     Page1 = 0;
  107.     delete Page2;
  108.     Page2 = 0;
  109.  
  110.     delete PrnDC;
  111.     PrnDC = 0;
  112.     delete PrintExtent;
  113.     PrintExtent = 0;
  114.     delete Printout;
  115.     Printout = 0;
  116. }
  117.  
  118.  
  119. void PreviewWindow::SetupWindow ()
  120. {
  121.     TDecoratedFrame::SetupWindow();
  122.  
  123.     TPrintDialog::TData& data = Printer->GetSetup();
  124.     Page1 = new TPreviewPage(Client, *Printout, *PrnDC, *PrintExtent, 1);
  125.     Page1->SetPageNumber(1);
  126.     data.FromPage = 1;
  127.     data.ToPage = 1;
  128.     data.MinPage = 1;
  129.     data.MaxPage = 1;
  130.  
  131.     Page2 = 0;
  132.  
  133.     TLayoutMetrics metrics1;
  134.  
  135.     metrics1.X.Set(lmLeft, lmRightOf, lmParent, lmLeft, 15);
  136.     metrics1.Y.Set(lmTop, lmBelow, lmParent, lmTop, 15);
  137.  
  138. ##--BEGIN-- @QUERY_APPL_COMMENT == VALUE_VERBOSE
  139.     //
  140.     // Determine major axis of preview page, have that follow parent size.
  141.     // Make minor axis a percentage (aspect ratio) of the page's major axis
  142.     //
  143. ##--END-- @QUERY_APPL_COMMENT == VALUE_VERBOSE
  144.     TRect r = Client->GetClientRect();
  145.     long ratio;
  146.  
  147.     if (PrintExtent->cx > PrintExtent->cy)
  148.         ratio = ((long)PrintExtent->cy * 100) / PrintExtent->cx;
  149.     else
  150.         ratio = ((long)PrintExtent->cx * 100) / PrintExtent->cy;
  151.  
  152.     bool xMajor = (((r.Width() * ratio) / 100) > r.Height());
  153.     if (xMajor){
  154.         metrics1.Height.Set(lmBottom, lmAbove, lmParent, lmBottom, 15);
  155.         metrics1.Width.PercentOf(Page1, (int)((long)PrintExtent->cx * 95 / PrintExtent->cy), lmHeight);
  156.     } else {
  157.         metrics1.Height.PercentOf(Page1, (int)((long)PrintExtent->cy * 95 / PrintExtent->cx), lmWidth);
  158.         metrics1.Width.Set(lmRight, lmLeftOf, lmParent, lmRight, 15);
  159.     }
  160.  
  161.     Page1->Create();
  162.  
  163.     Client->SetChildLayoutMetrics(*Page1, metrics1);
  164.     Client->Layout();
  165. }
  166.  
  167.  
  168. void PreviewWindow::SpeedBarState ()
  169. {
  170.     TPrintDialog::TData &printerData = Printer->GetSetup();
  171.  
  172. ##:@QUERY_APPL_COMMENT == VALUE_VERBOSE
  173.     // Update the page count.
  174.     TTextGadget *theTGadget = TYPESAFE_DOWNCAST(PreviewSpeedBar->GadgetWithId(APX_PPR_CURRPAGE), TTextGadget);
  175.     if (theTGadget) {
  176.         char    buffer[32];
  177.         if (Page2 && (printerData.FromPage != printerData.ToPage))
  178.             wsprintf(buffer, "Page %d - %d", printerData.FromPage, printerData.ToPage);
  179.         else
  180.             wsprintf(buffer, "Page %d", printerData.FromPage);
  181.         theTGadget->SetText(buffer);
  182.     }
  183. }
  184.  
  185.  
  186. void PreviewWindow::PPR_PreviousEnable (TCommandEnabler &tce)
  187. {
  188. ##:@QUERY_APPL_COMMENT == VALUE_VERBOSE
  189.     // Only have previous on if we're not at the first page.
  190.     TPrintDialog::TData &printerData = Printer->GetSetup();
  191.     tce.Enable(printerData.FromPage != 1);
  192. }
  193.  
  194.  
  195. void PreviewWindow::PPR_NextEnable (TCommandEnabler &tce)
  196. {
  197. ##:@QUERY_APPL_COMMENT == VALUE_VERBOSE
  198.     // Only have next on if we're not at the last page.
  199.     TPrintDialog::TData &printerData = Printer->GetSetup();
  200.     tce.Enable(printerData.ToPage != printerData.MaxPage);
  201. }
  202.  
  203.  
  204. void PreviewWindow::PPR_Previous ()
  205. {
  206.     TPrintDialog::TData &printerData = Printer->GetSetup();
  207.  
  208.     if (printerData.FromPage > printerData.MinPage) { 
  209.         printerData.FromPage--;
  210.         printerData.ToPage--;
  211.  
  212.         Page1->SetPageNumber(printerData.FromPage);
  213.         if (Page2)
  214.             Page2->SetPageNumber(printerData.ToPage);
  215.     }
  216.  
  217.     SpeedBarState();
  218. }
  219.  
  220.  
  221. void PreviewWindow::PPR_Next ()
  222. {
  223.     TPrintDialog::TData &printerData = Printer->GetSetup();
  224.  
  225.     if (printerData.ToPage < printerData.MaxPage) {
  226.         printerData.FromPage++;
  227.         printerData.ToPage++;
  228.  
  229.         Page1->SetPageNumber(printerData.FromPage);
  230.         if (Page2)
  231.             Page2->SetPageNumber(printerData.ToPage);
  232.     }
  233.  
  234.     SpeedBarState();
  235. }
  236.  
  237.  
  238. void PreviewWindow::PPR_OneUp ()
  239. {
  240.     if (Page2) {
  241.         Client->RemoveChildLayoutMetrics(*Page2);
  242.  
  243.         delete Page2;
  244.         Page2 = 0;
  245.  
  246.         Client->Layout();
  247.  
  248.         TPrintDialog::TData &printerData = Printer->GetSetup();
  249.         printerData.ToPage = printerData.FromPage;
  250.  
  251.         SpeedBarState();
  252.     }
  253. }
  254.  
  255.  
  256. void PreviewWindow::PPR_TwoUpEnable (TCommandEnabler &tce)
  257. {
  258. ##:@QUERY_APPL_COMMENT == VALUE_VERBOSE
  259.     // Two up is only available for portrait mode.
  260.     tce.Enable(PrintExtent->cx <= PrintExtent->cy);
  261. }
  262.  
  263.  
  264. void PreviewWindow::PPR_TwoUp ()
  265. {
  266.     if (Page2 == 0) {
  267.         Page2 = new TPreviewPage(Client, *Printout, *PrnDC, *PrintExtent, PageNumber + 1);
  268.         Page2->Create();
  269.  
  270.         TLayoutMetrics metrics2;
  271.  
  272.         metrics2.X.Set(lmLeft, lmRightOf, Page1, lmRight, 30);
  273.         metrics2.Y.SameAs(Page1, lmTop);
  274.  
  275. ##:@QUERY_APPL_COMMENT == VALUE_VERBOSE 2
  276.         // Assume portrait
  277.         //
  278.         metrics2.Width.SameAs(Page1, lmWidth);
  279.         metrics2.Height.SameAs(Page1, lmBottom);
  280.  
  281.         Client->SetChildLayoutMetrics(*Page2, metrics2);
  282.         Client->Layout();
  283.  
  284.         TPrintDialog::TData &printerData = Printer->GetSetup();
  285.  
  286. ##--BEGIN-- @QUERY_APPL_COMMENT == VALUE_VERBOSE
  287.         // Page 2 is the next page.  If the next page is outside of our
  288.         // range then set the first page back one and the 2nd page is
  289.         // the current page.  If the document is only 1 page long then
  290.         // the 2nd page is empty.
  291. ##--END-- @QUERY_APPL_COMMENT == VALUE_VERBOSE
  292.         if (printerData.FromPage == printerData.MaxPage) {
  293.             if (printerData.FromPage > 1) {
  294.                 printerData.FromPage--;
  295.                 printerData.ToPage = printerData.FromPage + 1;
  296.                 Page1->SetPageNumber(printerData.FromPage);
  297.                 Page2->SetPageNumber(printerData.ToPage);
  298.             } else
  299.                 Page2->SetPageNumber(0);
  300.         } else {
  301.             printerData.ToPage = printerData.FromPage + 1;
  302.             Page2->SetPageNumber(printerData.ToPage);
  303.         }
  304.  
  305.         SpeedBarState();
  306.     }
  307. }
  308.  
  309.  
  310. void PreviewWindow::PPR_Done ()
  311. {
  312. ##:@QUERY_APPL_COMMENT == VALUE_VERBOSE
  313.     // Don't call the base class EvClose we do not want PreviewWindow to be destructed.
  314. ##@QUERY_APPL_MODEL == VALUE_SDI
  315.     GetApplication()->GetMainWindow()->SetRedraw(false);
  316.     GetApplication()->EndModal(IDCANCEL);
  317. }
  318. ##--BEGIN-- @QUERY_APPL_MODEL == VALUE_MDI
  319.  
  320.  
  321. void PreviewWindow::CmPrint ()
  322. {
  323.     TWindow *client = GetApplication()->GetMainWindow()->GetClientWindow();
  324.  
  325.     if (client)
  326.         client->SendMessage(WM_COMMAND, CM_FILEPRINT, 0);
  327. }
  328.  
  329.  
  330. void PreviewWindow::CmPrintEnable (TCommandEnabler &tce)
  331. {
  332.     tce.Enable(true);
  333. }
  334. ##--END-- @QUERY_APPL_MODEL == VALUE_MDI
  335.