| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: printwin.cpp |
| 3 | // Purpose: wxMacPrinter framework |
| 4 | // Author: Julian Smart |
| 5 | // Modified by: |
| 6 | // Created: 04/01/98 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) Julian Smart and Markus Holzem |
| 9 | // Licence: wxWindows license |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | #ifdef __GNUG__ |
| 13 | #pragma implementation "printwin.h" |
| 14 | #endif |
| 15 | |
| 16 | // For compilers that support precompilation, includes "wx.h". |
| 17 | #include "wx/wxprec.h" |
| 18 | |
| 19 | #ifdef __BORLANDC__ |
| 20 | #pragma hdrstop |
| 21 | #endif |
| 22 | |
| 23 | #include "wx/defs.h" |
| 24 | |
| 25 | #ifndef WX_PRECOMP |
| 26 | #include "wx/utils.h" |
| 27 | #include "wx/dc.h" |
| 28 | #include "wx/app.h" |
| 29 | #include "wx/msgdlg.h" |
| 30 | #endif |
| 31 | |
| 32 | #include "wx/mac/private.h" |
| 33 | |
| 34 | #include "wx/mac/printmac.h" |
| 35 | #include "wx/dcprint.h" |
| 36 | #include "wx/printdlg.h" |
| 37 | |
| 38 | #include <stdlib.h> |
| 39 | |
| 40 | #if !USE_SHARED_LIBRARY |
| 41 | IMPLEMENT_DYNAMIC_CLASS(wxMacPrinter, wxPrinterBase) |
| 42 | IMPLEMENT_CLASS(wxMacPrintPreview, wxPrintPreviewBase) |
| 43 | #endif |
| 44 | |
| 45 | /* |
| 46 | * Printer |
| 47 | */ |
| 48 | |
| 49 | wxMacPrinter::wxMacPrinter(wxPrintDialogData *data): |
| 50 | wxPrinterBase(data) |
| 51 | { |
| 52 | } |
| 53 | |
| 54 | wxMacPrinter::~wxMacPrinter(void) |
| 55 | { |
| 56 | } |
| 57 | |
| 58 | bool wxMacPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt) |
| 59 | { |
| 60 | sm_abortIt = FALSE; |
| 61 | sm_abortWindow = NULL; |
| 62 | |
| 63 | if (!printout) |
| 64 | return FALSE; |
| 65 | |
| 66 | printout->SetIsPreview(FALSE); |
| 67 | printout->OnPreparePrinting(); |
| 68 | |
| 69 | // Get some parameters from the printout, if defined |
| 70 | int fromPage, toPage; |
| 71 | int minPage, maxPage; |
| 72 | printout->GetPageInfo(&minPage, &maxPage, &fromPage, &toPage); |
| 73 | |
| 74 | if (maxPage == 0) |
| 75 | return FALSE; |
| 76 | |
| 77 | m_printDialogData.SetMinPage(minPage); |
| 78 | m_printDialogData.SetMaxPage(maxPage); |
| 79 | if (fromPage != 0) |
| 80 | m_printDialogData.SetFromPage(fromPage); |
| 81 | if (toPage != 0) |
| 82 | m_printDialogData.SetToPage(toPage); |
| 83 | |
| 84 | if (minPage != 0) |
| 85 | { |
| 86 | m_printDialogData.EnablePageNumbers(TRUE); |
| 87 | if (m_printDialogData.GetFromPage() < m_printDialogData.GetMinPage()) |
| 88 | m_printDialogData.SetFromPage(m_printDialogData.GetMinPage()); |
| 89 | else if (m_printDialogData.GetFromPage() > m_printDialogData.GetMaxPage()) |
| 90 | m_printDialogData.SetFromPage(m_printDialogData.GetMaxPage()); |
| 91 | if (m_printDialogData.GetToPage() > m_printDialogData.GetMaxPage()) |
| 92 | m_printDialogData.SetToPage(m_printDialogData.GetMaxPage()); |
| 93 | else if (m_printDialogData.GetToPage() < m_printDialogData.GetMinPage()) |
| 94 | m_printDialogData.SetToPage(m_printDialogData.GetMinPage()); |
| 95 | } |
| 96 | else |
| 97 | m_printDialogData.EnablePageNumbers(FALSE); |
| 98 | |
| 99 | // Create a suitable device context |
| 100 | wxDC *dc = NULL; |
| 101 | if (prompt) |
| 102 | { |
| 103 | wxPrintDialog dialog(parent, & m_printDialogData); |
| 104 | if (dialog.ShowModal() == wxID_OK) |
| 105 | { |
| 106 | dc = dialog.GetPrintDC(); |
| 107 | m_printDialogData = dialog.GetPrintData(); |
| 108 | } |
| 109 | } |
| 110 | else |
| 111 | { |
| 112 | dc = new wxPrinterDC( m_printDialogData.GetPrintData() ) ; |
| 113 | } |
| 114 | |
| 115 | |
| 116 | // May have pressed cancel. |
| 117 | if (!dc || !dc->Ok()) |
| 118 | { |
| 119 | if (dc) delete dc; |
| 120 | return FALSE; |
| 121 | } |
| 122 | |
| 123 | // on the mac we have always pixels as addressing mode with 72 dpi |
| 124 | |
| 125 | printout->SetPPIScreen(72, 72); |
| 126 | printout->SetPPIPrinter(72, 72); |
| 127 | |
| 128 | // Set printout parameters |
| 129 | printout->SetDC(dc); |
| 130 | |
| 131 | int w, h; |
| 132 | wxCoord ww, hh; |
| 133 | dc->GetSize(&w, &h); |
| 134 | printout->SetPageSizePixels((int)w, (int)h); |
| 135 | dc->GetSizeMM(&ww, &hh); |
| 136 | printout->SetPageSizeMM((int)ww, (int)hh); |
| 137 | |
| 138 | // Create an abort window |
| 139 | wxBeginBusyCursor(); |
| 140 | |
| 141 | wxWindow *win = CreateAbortWindow(parent, printout); |
| 142 | wxYield(); |
| 143 | |
| 144 | if (!win) |
| 145 | { |
| 146 | wxEndBusyCursor(); |
| 147 | wxMessageBox("Sorry, could not create an abort dialog.", "Print Error", wxOK, parent); |
| 148 | delete dc; |
| 149 | return FALSE; |
| 150 | } |
| 151 | sm_abortWindow = win; |
| 152 | sm_abortWindow->Show(TRUE); |
| 153 | wxYield(); |
| 154 | |
| 155 | printout->OnBeginPrinting(); |
| 156 | |
| 157 | bool keepGoing = TRUE; |
| 158 | |
| 159 | int copyCount; |
| 160 | for (copyCount = 1; copyCount <= m_printDialogData.GetNoCopies(); copyCount ++) |
| 161 | { |
| 162 | if (!printout->OnBeginDocument(m_printDialogData.GetFromPage(), m_printDialogData.GetToPage())) |
| 163 | { |
| 164 | wxEndBusyCursor(); |
| 165 | wxMessageBox("Could not start printing.", "Print Error", wxOK, parent); |
| 166 | break; |
| 167 | } |
| 168 | if (sm_abortIt) |
| 169 | break; |
| 170 | |
| 171 | int pn; |
| 172 | for (pn = m_printDialogData.GetFromPage(); keepGoing && (pn <= m_printDialogData.GetToPage()) && printout->HasPage(pn); |
| 173 | pn++) |
| 174 | { |
| 175 | if (sm_abortIt) |
| 176 | { |
| 177 | keepGoing = FALSE; |
| 178 | break; |
| 179 | } |
| 180 | else |
| 181 | { |
| 182 | GrafPtr thePort ; |
| 183 | GetPort( &thePort ) ; |
| 184 | wxYield() ; |
| 185 | SetPort( thePort ) ; |
| 186 | |
| 187 | dc->StartPage(); |
| 188 | keepGoing = printout->OnPrintPage(pn); |
| 189 | dc->EndPage(); |
| 190 | } |
| 191 | } |
| 192 | printout->OnEndDocument(); |
| 193 | } |
| 194 | |
| 195 | printout->OnEndPrinting(); |
| 196 | |
| 197 | if (sm_abortWindow) |
| 198 | { |
| 199 | sm_abortWindow->Show(FALSE); |
| 200 | delete sm_abortWindow; |
| 201 | sm_abortWindow = NULL; |
| 202 | } |
| 203 | |
| 204 | wxEndBusyCursor(); |
| 205 | |
| 206 | delete dc; |
| 207 | |
| 208 | return TRUE; |
| 209 | } |
| 210 | |
| 211 | wxDC* wxMacPrinter::PrintDialog(wxWindow *parent) |
| 212 | { |
| 213 | wxDC* dc = (wxDC*) NULL; |
| 214 | |
| 215 | wxPrintDialog dialog(parent, & m_printDialogData); |
| 216 | int ret = dialog.ShowModal(); |
| 217 | |
| 218 | if (ret == wxID_OK) |
| 219 | { |
| 220 | dc = dialog.GetPrintDC(); |
| 221 | m_printDialogData = dialog.GetPrintDialogData(); |
| 222 | } |
| 223 | |
| 224 | return dc; |
| 225 | } |
| 226 | |
| 227 | bool wxMacPrinter::Setup(wxWindow *parent) |
| 228 | { |
| 229 | wxPrintDialog dialog(parent, & m_printDialogData); |
| 230 | dialog.GetPrintDialogData().SetSetupDialog(TRUE); |
| 231 | |
| 232 | int ret = dialog.ShowModal(); |
| 233 | |
| 234 | if (ret == wxID_OK) |
| 235 | { |
| 236 | m_printDialogData = dialog.GetPrintDialogData(); |
| 237 | } |
| 238 | |
| 239 | return (ret == wxID_OK); |
| 240 | } |
| 241 | |
| 242 | /* |
| 243 | * Print preview |
| 244 | */ |
| 245 | |
| 246 | wxMacPrintPreview::wxMacPrintPreview(wxPrintout *printout, |
| 247 | wxPrintout *printoutForPrinting, |
| 248 | wxPrintDialogData *data) |
| 249 | : wxPrintPreviewBase(printout, printoutForPrinting, data) |
| 250 | { |
| 251 | DetermineScaling(); |
| 252 | } |
| 253 | |
| 254 | wxMacPrintPreview::wxMacPrintPreview(wxPrintout *printout, wxPrintout *printoutForPrinting, wxPrintData *data): |
| 255 | wxPrintPreviewBase(printout, printoutForPrinting, data) |
| 256 | { |
| 257 | DetermineScaling(); |
| 258 | } |
| 259 | |
| 260 | wxMacPrintPreview::~wxMacPrintPreview(void) |
| 261 | { |
| 262 | } |
| 263 | |
| 264 | bool wxMacPrintPreview::Print(bool interactive) |
| 265 | { |
| 266 | if (!m_printPrintout) |
| 267 | return FALSE; |
| 268 | wxMacPrinter printer(&m_printDialogData); |
| 269 | return printer.Print(m_previewFrame, m_printPrintout, interactive); |
| 270 | } |
| 271 | |
| 272 | void wxMacPrintPreview::DetermineScaling(void) |
| 273 | { |
| 274 | int screenWidth , screenHeight ; |
| 275 | wxDisplaySize( &screenWidth , &screenHeight ) ; |
| 276 | |
| 277 | m_previewPrintout->SetPPIScreen( 72 , 72 ) ; |
| 278 | m_previewPrintout->SetPPIPrinter( 72 , 72 ) ; |
| 279 | m_previewPrintout->SetPageSizeMM( 8 * 25.6 , 11 * 25.6 ) ; |
| 280 | m_previewPrintout->SetPageSizePixels( 8 * 72 , 11 * 72 ) ; |
| 281 | m_pageWidth = 8 * 72 ; |
| 282 | m_pageHeight = 11 * 72 ; |
| 283 | m_previewScale = 1 ; |
| 284 | |
| 285 | // Get a device context for the currently selected printer |
| 286 | wxPrinterDC printerDC(m_printDialogData.GetPrintData()); |
| 287 | if (printerDC.Ok()) |
| 288 | { |
| 289 | int x , y ; |
| 290 | wxCoord ww, hh; |
| 291 | printerDC.GetSizeMM(&ww, &hh); |
| 292 | printerDC.GetSize( &x , &y ) ; |
| 293 | m_previewPrintout->SetPageSizeMM((int)ww, (int)hh); |
| 294 | m_previewPrintout->SetPageSizePixels( x , y) ; |
| 295 | m_pageWidth = x ; |
| 296 | m_pageHeight = y ; |
| 297 | m_isOk = true ; |
| 298 | |
| 299 | } |
| 300 | // At 100%, the page should look about page-size on the screen. |
| 301 | // m_previewScale = (float)((float)screenWidth/(float)printerWidth); |
| 302 | // m_previewScale = m_previewScale * (float)((float)screenXRes/(float)printerXRes); |
| 303 | |
| 304 | m_previewScale = 1 ; |
| 305 | } |