]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/printdlg.cpp
Performance optimization
[wxWidgets.git] / src / mac / carbon / printdlg.cpp
CommitLineData
e9576ca5 1/////////////////////////////////////////////////////////////////////////////
cc224d6e 2// Name: src/mac/carbon/printdlg.cpp
e9576ca5 3// Purpose: wxPrintDialog, wxPageSetupDialog
a31a5f85 4// Author: Stefan Csomor
e9576ca5 5// Modified by:
a31a5f85 6// Created: 1998-01-01
e9576ca5 7// RCS-ID: $Id$
a31a5f85 8// Copyright: (c) Stefan Csomor
8e3f3880 9// Licence: wxWindows licence
e9576ca5
SC
10/////////////////////////////////////////////////////////////////////////////
11
3d1a4878
SC
12#include "wx/wxprec.h"
13
179e085f
RN
14#if wxUSE_PRINTING_ARCHITECTURE
15
246c5004
WS
16#include "wx/printdlg.h"
17
b4f4d3dd 18#ifndef WX_PRECOMP
8e3f3880 19 #include "wx/object.h"
6d50343d 20 #include "wx/dcprint.h"
246c5004 21 #include "wx/msgdlg.h"
286f4413
KH
22 #include "wx/textctrl.h"
23 #include "wx/sizer.h"
24 #include "wx/stattext.h"
8e3f3880
WS
25#endif
26
08680429 27#include "wx/mac/printdlg.h"
746d7582 28#include "wx/mac/private/print.h"
f415cab9 29#include "wx/statline.h"
a3d3d3bf 30
cc224d6e 31
e9576ca5 32// Use generic page setup dialog: use your own native one if one exists.
e9576ca5 33
c061373d 34IMPLEMENT_DYNAMIC_CLASS(wxMacPrintDialog, wxPrintDialogBase)
e9576ca5 35
cc224d6e 36
c061373d 37wxMacPrintDialog::wxMacPrintDialog()
e9576ca5
SC
38{
39 m_dialogParent = NULL;
e7549107 40 m_printerDC = NULL;
cc224d6e 41 m_destroyDC = true;
e9576ca5
SC
42}
43
cc224d6e 44wxMacPrintDialog::wxMacPrintDialog( wxWindow *p, wxPrintDialogData *data )
e9576ca5 45{
cc224d6e 46 Create( p, data );
e9576ca5
SC
47}
48
cc224d6e 49wxMacPrintDialog::wxMacPrintDialog( wxWindow *p, wxPrintData *data )
e7549107
SC
50{
51 wxPrintDialogData data2;
cc224d6e 52 if (data != NULL)
e7549107 53 data2 = *data;
cc224d6e
DS
54
55 Create( p, &data2 );
e7549107
SC
56}
57
cc224d6e 58bool wxMacPrintDialog::Create( wxWindow *p, wxPrintDialogData *data )
e9576ca5
SC
59{
60 m_dialogParent = p;
e7549107 61 m_printerDC = NULL;
cc224d6e
DS
62 m_destroyDC = true;
63
64 if (data != NULL)
e7549107 65 m_printDialogData = *data;
cc224d6e
DS
66
67 return true;
e9576ca5
SC
68}
69
c061373d 70wxMacPrintDialog::~wxMacPrintDialog()
e9576ca5 71{
cc224d6e
DS
72 if (m_destroyDC && m_printerDC)
73 {
e7549107 74 delete m_printerDC;
f5bb2251
GD
75 m_printerDC = NULL;
76 }
e9576ca5
SC
77}
78
c061373d 79int wxMacPrintDialog::ShowModal()
e9576ca5 80{
dc7ccb9c 81 m_printDialogData.GetPrintData().ConvertToNative();
cc224d6e
DS
82 ((wxMacCarbonPrintData*)m_printDialogData.GetPrintData().GetNativeData())->TransferFrom( &m_printDialogData );
83
84 int result = wxID_CANCEL;
1bd568fa
SC
85
86#ifdef __LP64__
87 // TODO use NSPrintPanel
88#else
cc224d6e
DS
89 OSErr err = noErr;
90 Boolean accepted;
cc224d6e
DS
91 err = PMSessionPrintDialog(
92 ((wxMacCarbonPrintData*)m_printDialogData.GetPrintData().GetNativeData())->m_macPrintSession,
dc7ccb9c
SC
93 ((wxMacCarbonPrintData*)m_printDialogData.GetPrintData().GetNativeData())->m_macPrintSettings,
94 ((wxMacCarbonPrintData*)m_printDialogData.GetPrintData().GetNativeData())->m_macPageFormat,
cc224d6e
DS
95 &accepted );
96
dc7ccb9c
SC
97 if ((err == noErr) && !accepted)
98 {
cc224d6e
DS
99 // user clicked Cancel button
100 err = kPMCancel;
dc7ccb9c
SC
101 }
102
cc224d6e 103 if (err == noErr)
dc7ccb9c 104 {
cc224d6e 105 result = wxID_OK;
dc7ccb9c
SC
106 }
107
108 if ((err != noErr) && (err != kPMCancel))
109 {
cc224d6e
DS
110 wxString message;
111
112 message.Printf( wxT("Print Error %d"), err );
113 wxMessageDialog dialog( NULL, message, wxEmptyString, wxICON_HAND | wxOK );
dc7ccb9c
SC
114 dialog.ShowModal();
115 }
116
cc224d6e 117 if (result == wxID_OK)
dc7ccb9c
SC
118 {
119 m_printDialogData.GetPrintData().ConvertFromNative();
cc224d6e 120 ((wxMacCarbonPrintData*)m_printDialogData.GetPrintData().GetNativeData())->TransferTo( &m_printDialogData );
dc7ccb9c 121 }
1bd568fa 122#endif
cc224d6e 123 return result;
e9576ca5
SC
124}
125
f415cab9 126wxDC *wxMacPrintDialog::GetPrintDC()
e9576ca5 127{
cc224d6e 128 return new wxPrinterDC( m_printDialogData.GetPrintData() );
e9576ca5
SC
129}
130
08680429 131IMPLEMENT_CLASS(wxMacPageSetupDialog, wxPageSetupDialogBase)
e9576ca5 132
f415cab9 133wxMacPageSetupDialog::wxMacPageSetupDialog( wxWindow *p, wxPageSetupData *data )
cc224d6e 134 : wxPageSetupDialogBase()
e9576ca5 135{
cc224d6e 136 Create( p, data );
e9576ca5
SC
137}
138
f415cab9 139bool wxMacPageSetupDialog::Create( wxWindow *p, wxPageSetupData *data )
e9576ca5
SC
140{
141 m_dialogParent = p;
cc224d6e
DS
142
143 if (data != NULL)
e9576ca5 144 m_pageSetupData = (*data);
cc224d6e
DS
145
146 return true;
e9576ca5
SC
147}
148
08680429 149wxMacPageSetupDialog::~wxMacPageSetupDialog()
e9576ca5
SC
150{
151}
152
08680429 153wxPageSetupData& wxMacPageSetupDialog::GetPageSetupDialogData()
cc224d6e 154{
08680429
RR
155 return m_pageSetupData;
156}
157
158int wxMacPageSetupDialog::ShowModal()
e9576ca5 159{
dc7ccb9c 160 m_pageSetupData.GetPrintData().ConvertToNative();
cc224d6e 161 ((wxMacCarbonPrintData*)m_pageSetupData.GetPrintData().GetNativeData())->TransferFrom( &m_pageSetupData );
dc7ccb9c 162
cc224d6e 163 int result = wxID_CANCEL;
1bd568fa
SC
164#ifdef __LP64__
165#else
cc224d6e
DS
166 OSErr err = noErr;
167 Boolean accepted;
dc7ccb9c 168
cc224d6e
DS
169 err = PMSessionPageSetupDialog(
170 ((wxMacCarbonPrintData*)m_pageSetupData.GetPrintData().GetNativeData())->m_macPrintSession,
dc7ccb9c 171 ((wxMacCarbonPrintData*)m_pageSetupData.GetPrintData().GetNativeData())->m_macPageFormat,
cc224d6e
DS
172 &accepted );
173
dc7ccb9c
SC
174 if ((err == noErr) && !accepted)
175 {
cc224d6e
DS
176 // user clicked Cancel button
177 err = kPMCancel;
dc7ccb9c
SC
178 }
179
cc224d6e
DS
180 // If the user did not cancel, flatten and save the PageFormat object
181 // with our document.
182 if (err == noErr)
dc7ccb9c 183 {
cc224d6e 184 result = wxID_OK;
dc7ccb9c 185 }
cc224d6e 186
dc7ccb9c
SC
187 if ((err != noErr) && (err != kPMCancel))
188 {
cc224d6e
DS
189 wxString message;
190
191 message.Printf( wxT("Print Error %d"), err );
192 wxMessageDialog dialog( NULL, message, wxEmptyString, wxICON_HAND | wxOK );
dc7ccb9c
SC
193 dialog.ShowModal();
194 }
195
cc224d6e 196 if (result == wxID_OK)
dc7ccb9c
SC
197 {
198 m_pageSetupData.GetPrintData().ConvertFromNative();
cc224d6e
DS
199 m_pageSetupData.SetPaperSize( m_pageSetupData.GetPrintData().GetPaperSize() );
200 ((wxMacCarbonPrintData*)m_pageSetupData.GetPrintData().GetNativeData())->TransferTo( &m_pageSetupData );
201 }
1bd568fa 202#endif
08680429 203 return result;
e9576ca5
SC
204}
205
f415cab9
JS
206
207IMPLEMENT_CLASS(wxMacPageMarginsDialog, wxDialog)
208
209wxMacPageMarginsDialog::wxMacPageMarginsDialog(wxFrame *parent, wxPageSetupData *data) :
210 wxDialog(parent, wxID_ANY, wxString(wxT("Page Margins"))),
211 m_pageSetupDialogData(data)
212 {
213 GetMinMargins();
214 wxBoxSizer *colSizer = new wxBoxSizer(wxVERTICAL);
215 wxFlexGridSizer *gridSizer = new wxFlexGridSizer(4, 5, 5);
216 colSizer->Add(gridSizer, wxSizerFlags().Border(wxALL, 5));
217 gridSizer->Add(new wxStaticText(this, wxID_ANY, wxT("Left (mm):")), wxSizerFlags().Right());
218 gridSizer->Add(m_LeftMargin = new wxTextCtrl(this, wxID_ANY), wxSizerFlags().Left());
219 gridSizer->Add(new wxStaticText(this, wxID_ANY, wxT("Top (mm):")), wxSizerFlags().Right());
220 gridSizer->Add(m_TopMargin = new wxTextCtrl(this, wxID_ANY), wxSizerFlags().Left());
221 gridSizer->Add(new wxStaticText(this, wxID_ANY, wxT("Right (mm):")), wxSizerFlags().Right());
222 gridSizer->Add(m_RightMargin = new wxTextCtrl(this, wxID_ANY), wxSizerFlags().Left());
223 gridSizer->Add(new wxStaticText(this, wxID_ANY, wxT("Bottom (mm):")), wxSizerFlags().Right());
224 gridSizer->Add(m_BottomMargin = new wxTextCtrl(this, wxID_ANY), wxSizerFlags().Left());
225 colSizer->Add(new wxStaticLine(this), wxSizerFlags().Expand().Border(wxTOP|wxBOTTOM, 5));
226 colSizer->Add(CreateButtonSizer(wxOK | wxCANCEL), wxSizerFlags().Expand().Border(wxALL, 5));
227 TransferToWindow();
228 SetSizerAndFit(colSizer);
229 Center(wxBOTH);
230 }
231
232bool wxMacPageMarginsDialog::TransferToWindow()
233 {
234 wxASSERT(m_pageSetupDialogData);
235 wxPoint topLeft = m_pageSetupDialogData->GetMarginTopLeft();
236 wxPoint bottomRight = m_pageSetupDialogData->GetMarginBottomRight();
237 wxPoint minTopLeft = m_pageSetupDialogData->GetMinMarginTopLeft();
238 wxPoint minBottomRight = m_pageSetupDialogData->GetMinMarginBottomRight();
239 m_LeftMargin->SetValue(wxString::Format(wxT("%d"), wxMax(topLeft.x, minTopLeft.x)));
240 m_LeftMargin->SetSelection(-1, -1);
241 m_TopMargin->SetValue(wxString::Format(wxT("%d"), wxMax(topLeft.y, minTopLeft.y)));
242 m_TopMargin->SetSelection(-1, -1);
243 m_RightMargin->SetValue(wxString::Format(wxT("%d"), wxMax(bottomRight.x, minBottomRight.x)));
244 m_RightMargin->SetSelection(-1, -1);
245 m_BottomMargin->SetValue(wxString::Format(wxT("%d"), wxMax(bottomRight.y, minBottomRight.y)));
246 m_BottomMargin->SetSelection(-1, -1);
247 m_LeftMargin->SetFocus();
248 return true;
249 }
250
251bool wxMacPageMarginsDialog::TransferDataFromWindow()
252 {
253 wxPoint topLeft, bottomRight;
254 if (!CheckValue(m_LeftMargin, &topLeft.x, m_MinMarginTopLeft.x, wxT("left margin"))) return false;
255 if (!CheckValue(m_TopMargin, &topLeft.y, m_MinMarginTopLeft.y, wxT("top margin"))) return false;
256 if (!CheckValue(m_RightMargin, &bottomRight.x, m_MinMarginBottomRight.x, wxT("right margin"))) return false;
257 if (!CheckValue(m_BottomMargin, &bottomRight.y, m_MinMarginBottomRight.y, wxT("bottom margin"))) return false;
258 m_pageSetupDialogData->SetMarginTopLeft(topLeft);
259 m_pageSetupDialogData->SetMarginBottomRight(bottomRight);
260 return true;
261 }
262
263bool wxMacPageMarginsDialog::CheckValue(wxTextCtrl* textCtrl, int *value, int minValue, const wxString& name)
264 {
265 long lvalue;
266 if (!textCtrl->GetValue().ToLong(&lvalue))
267 {
268 wxMessageBox(wxString::Format(wxT("Sorry, \"%s\" is not a valid numerical value for the %s"), textCtrl->GetValue().c_str(), name.c_str()), wxT("Page Margin Error"));
269 return false;
270 }
271 if (lvalue < minValue)
272 {
273 wxMessageBox(wxString::Format(wxT("Sorry, \"%s\" is not a valid value for the %s, which must be >= %d"), textCtrl->GetValue().c_str(), name.c_str(), minValue), wxT("Page Margin Error"));
274 textCtrl->SetValue(wxString::Format(wxT("%d"), minValue));
275 textCtrl->SetSelection(-1, -1);
276 textCtrl->SetFocus();
277 return false;
278 }
279 *value = int(lvalue);
280 return true;
281 }
282
283void wxMacPageMarginsDialog::GetMinMargins()
284 {
285 m_MinMarginTopLeft = m_pageSetupDialogData->GetMinMarginTopLeft();
286 m_MinMarginBottomRight = m_pageSetupDialogData->GetMinMarginBottomRight();
287 }
288
289
290
179e085f 291#endif