Add wxPrintout::SetUp() to reuse the same code in all ports

MSW, GTK, OSX and Postscript implementations all did almost exactly the
same thing to initialize wxPrintout, so extract this common code into a
new wxPrintout method and just call it instead.

There should be no changes in behaviour.
This commit is contained in:
Vadim Zeitlin
2018-05-23 17:52:31 +02:00
parent b6f0693a41
commit 048b7f44ec
6 changed files with 37 additions and 65 deletions

View File

@@ -281,6 +281,10 @@ public:
virtual wxString GetTitle() const { return m_printoutTitle; } virtual wxString GetTitle() const { return m_printoutTitle; }
// Port-specific code should call this function to initialize this object
// with everything it needs, instead of using individual accessors below.
bool SetUp(wxDC& dc);
wxDC *GetDC() const { return m_printoutDC; } wxDC *GetDC() const { return m_printoutDC; }
void SetDC(wxDC *dc) { m_printoutDC = dc; } void SetDC(wxDC *dc) { m_printoutDC = dc; }

View File

@@ -638,6 +638,33 @@ void wxPrintout::GetPageInfo(int *minPage, int *maxPage, int *fromPage, int *toP
*toPage = 1; *toPage = 1;
} }
bool wxPrintout::SetUp(wxDC& dc)
{
SetPPIScreen(wxGetDisplayPPI());
// We need to know printer PPI. In most ports, this can be retrieved from
// the printer DC, but in others it is computed (probably for legacy
// reasons) outside of wxDC code, so don't override it if it had been
// already set.
if ( !m_PPIPrinterX || !m_PPIPrinterY )
{
SetPPIPrinter(dc.GetPPI());
if ( !m_PPIPrinterX || !m_PPIPrinterY )
{
// But if we couldn't get it in any way, we can't continue.
return false;
}
}
SetDC(&dc);
dc.GetSize(&m_pageWidthPixels, &m_pageHeightPixels);
m_paperRectPixels = wxRect(0, 0, m_pageWidthPixels, m_pageHeightPixels);
dc.GetSizeMM(&m_pageWidthMM, &m_pageHeightMM);
return true;
}
void wxPrintout::FitThisSizeToPaper(const wxSize& imageSize) void wxPrintout::FitThisSizeToPaper(const wxSize& imageSize)
{ {
// Set the DC scale and origin so that the given image size fits within the // Set the DC scale and origin so that the given image size fits within the

View File

@@ -105,24 +105,8 @@ bool wxPostScriptPrinter::Print(wxWindow *parent, wxPrintout *printout, bool pro
return false; return false;
} }
wxSize ScreenPixels = wxGetDisplaySize();
wxSize ScreenMM = wxGetDisplaySizeMM();
printout->SetPPIScreen( (int) ((ScreenPixels.GetWidth() * 25.4) / ScreenMM.GetWidth()),
(int) ((ScreenPixels.GetHeight() * 25.4) / ScreenMM.GetHeight()) );
printout->SetPPIPrinter( dc->GetResolution(),
dc->GetResolution() );
// Set printout parameters // Set printout parameters
printout->SetDC(dc); printout->SetUp(*dc);
int w, h;
dc->GetSize(&w, &h);
printout->SetPageSizePixels((int)w, (int)h);
printout->SetPaperRectPixels(wxRect(0, 0, w, h));
int mw, mh;
dc->GetSizeMM(&mw, &mh);
printout->SetPageSizeMM((int)mw, (int)mh);
// Create an abort window // Create an abort window
wxBeginBusyCursor(); wxBeginBusyCursor();

View File

@@ -1024,19 +1024,8 @@ void wxGtkPrinter::BeginPrint(wxPrintout *printout, GtkPrintOperation *operation
return; return;
} }
printout->SetPPIScreen(wxGetDisplayPPI()); printout->SetUp(*m_dc);
printout->SetPPIPrinter( printDC->GetResolution(),
printDC->GetResolution() );
printout->SetDC(m_dc);
int w, h;
m_dc->GetSize(&w, &h);
printout->SetPageSizePixels((int)w, (int)h);
printout->SetPaperRectPixels(wxRect(0, 0, w, h));
int mw, mh;
m_dc->GetSizeMM(&mw, &mh);
printout->SetPageSizeMM((int)mw, (int)mh);
printout->OnPreparePrinting(); printout->OnPreparePrinting();
// Get some parameters from the printout, if defined. // Get some parameters from the printout, if defined.

View File

@@ -115,36 +115,14 @@ bool wxWindowsPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt
return false; return false;
} }
wxPrinterDCImpl *impl = (wxPrinterDCImpl*) dc->GetImpl(); // Set printout parameters
if (!printout->SetUp(*dc))
HDC hdc = ::GetDC(NULL);
int logPPIScreenX = ::GetDeviceCaps(hdc, LOGPIXELSX);
int logPPIScreenY = ::GetDeviceCaps(hdc, LOGPIXELSY);
::ReleaseDC(NULL, hdc);
int logPPIPrinterX = ::GetDeviceCaps((HDC) impl->GetHDC(), LOGPIXELSX);
int logPPIPrinterY = ::GetDeviceCaps((HDC) impl->GetHDC(), LOGPIXELSY);
if (logPPIPrinterX == 0 || logPPIPrinterY == 0)
{ {
delete dc; delete dc;
sm_lastError = wxPRINTER_ERROR; sm_lastError = wxPRINTER_ERROR;
return false; return false;
} }
printout->SetPPIScreen(logPPIScreenX, logPPIScreenY);
printout->SetPPIPrinter(logPPIPrinterX, logPPIPrinterY);
// Set printout parameters
printout->SetDC(dc);
int w, h;
dc->GetSize(&w, &h);
printout->SetPageSizePixels((int)w, (int)h);
printout->SetPaperRectPixels(dc->GetPaperRect());
dc->GetSizeMM(&w, &h);
printout->SetPageSizeMM((int)w, (int)h);
// Create an abort window // Create an abort window
wxBusyCursor busyCursor; wxBusyCursor busyCursor;
@@ -169,7 +147,7 @@ bool wxWindowsPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt
wxPrintAbortDialog *win = CreateAbortWindow(parent, printout); wxPrintAbortDialog *win = CreateAbortWindow(parent, printout);
wxYield(); wxYield();
::SetAbortProc(GetHdcOf(*impl), wxAbortProc); ::SetAbortProc(GetHdcOf(*dc), wxAbortProc);
if (!win) if (!win)
{ {

View File

@@ -585,8 +585,6 @@ bool wxMacPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt)
return false; return false;
} }
printout->SetPPIScreen(wxGetDisplayPPI());
PMResolution res; PMResolution res;
PMPrinter printer; PMPrinter printer;
wxOSXPrintData* nativeData = (wxOSXPrintData*) wxOSXPrintData* nativeData = (wxOSXPrintData*)
@@ -607,15 +605,7 @@ bool wxMacPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt)
printout->SetPPIPrinter(int(res.hRes), int(res.vRes)); printout->SetPPIPrinter(int(res.hRes), int(res.vRes));
// Set printout parameters // Set printout parameters
printout->SetDC(dc); printout->SetUp(*dc);
int w, h;
dc->GetSize(&w, &h);
printout->SetPageSizePixels((int)w, (int)h);
printout->SetPaperRectPixels(dc->GetPaperRect());
wxCoord mw, mh;
dc->GetSizeMM(&mw, &mh);
printout->SetPageSizeMM((int)mw, (int)mh);
// Create an abort window // Create an abort window
wxBeginBusyCursor(); wxBeginBusyCursor();