]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/printdlg.cpp
Performance optimization
[wxWidgets.git] / src / mac / carbon / printdlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/printdlg.cpp
3 // Purpose: wxPrintDialog, wxPageSetupDialog
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #if wxUSE_PRINTING_ARCHITECTURE
15
16 #include "wx/printdlg.h"
17
18 #ifndef WX_PRECOMP
19 #include "wx/object.h"
20 #include "wx/dcprint.h"
21 #include "wx/msgdlg.h"
22 #include "wx/textctrl.h"
23 #include "wx/sizer.h"
24 #include "wx/stattext.h"
25 #endif
26
27 #include "wx/mac/printdlg.h"
28 #include "wx/mac/private/print.h"
29 #include "wx/statline.h"
30
31
32 // Use generic page setup dialog: use your own native one if one exists.
33
34 IMPLEMENT_DYNAMIC_CLASS(wxMacPrintDialog, wxPrintDialogBase)
35
36
37 wxMacPrintDialog::wxMacPrintDialog()
38 {
39 m_dialogParent = NULL;
40 m_printerDC = NULL;
41 m_destroyDC = true;
42 }
43
44 wxMacPrintDialog::wxMacPrintDialog( wxWindow *p, wxPrintDialogData *data )
45 {
46 Create( p, data );
47 }
48
49 wxMacPrintDialog::wxMacPrintDialog( wxWindow *p, wxPrintData *data )
50 {
51 wxPrintDialogData data2;
52 if (data != NULL)
53 data2 = *data;
54
55 Create( p, &data2 );
56 }
57
58 bool wxMacPrintDialog::Create( wxWindow *p, wxPrintDialogData *data )
59 {
60 m_dialogParent = p;
61 m_printerDC = NULL;
62 m_destroyDC = true;
63
64 if (data != NULL)
65 m_printDialogData = *data;
66
67 return true;
68 }
69
70 wxMacPrintDialog::~wxMacPrintDialog()
71 {
72 if (m_destroyDC && m_printerDC)
73 {
74 delete m_printerDC;
75 m_printerDC = NULL;
76 }
77 }
78
79 int wxMacPrintDialog::ShowModal()
80 {
81 m_printDialogData.GetPrintData().ConvertToNative();
82 ((wxMacCarbonPrintData*)m_printDialogData.GetPrintData().GetNativeData())->TransferFrom( &m_printDialogData );
83
84 int result = wxID_CANCEL;
85
86 #ifdef __LP64__
87 // TODO use NSPrintPanel
88 #else
89 OSErr err = noErr;
90 Boolean accepted;
91 err = PMSessionPrintDialog(
92 ((wxMacCarbonPrintData*)m_printDialogData.GetPrintData().GetNativeData())->m_macPrintSession,
93 ((wxMacCarbonPrintData*)m_printDialogData.GetPrintData().GetNativeData())->m_macPrintSettings,
94 ((wxMacCarbonPrintData*)m_printDialogData.GetPrintData().GetNativeData())->m_macPageFormat,
95 &accepted );
96
97 if ((err == noErr) && !accepted)
98 {
99 // user clicked Cancel button
100 err = kPMCancel;
101 }
102
103 if (err == noErr)
104 {
105 result = wxID_OK;
106 }
107
108 if ((err != noErr) && (err != kPMCancel))
109 {
110 wxString message;
111
112 message.Printf( wxT("Print Error %d"), err );
113 wxMessageDialog dialog( NULL, message, wxEmptyString, wxICON_HAND | wxOK );
114 dialog.ShowModal();
115 }
116
117 if (result == wxID_OK)
118 {
119 m_printDialogData.GetPrintData().ConvertFromNative();
120 ((wxMacCarbonPrintData*)m_printDialogData.GetPrintData().GetNativeData())->TransferTo( &m_printDialogData );
121 }
122 #endif
123 return result;
124 }
125
126 wxDC *wxMacPrintDialog::GetPrintDC()
127 {
128 return new wxPrinterDC( m_printDialogData.GetPrintData() );
129 }
130
131 IMPLEMENT_CLASS(wxMacPageSetupDialog, wxPageSetupDialogBase)
132
133 wxMacPageSetupDialog::wxMacPageSetupDialog( wxWindow *p, wxPageSetupData *data )
134 : wxPageSetupDialogBase()
135 {
136 Create( p, data );
137 }
138
139 bool wxMacPageSetupDialog::Create( wxWindow *p, wxPageSetupData *data )
140 {
141 m_dialogParent = p;
142
143 if (data != NULL)
144 m_pageSetupData = (*data);
145
146 return true;
147 }
148
149 wxMacPageSetupDialog::~wxMacPageSetupDialog()
150 {
151 }
152
153 wxPageSetupData& wxMacPageSetupDialog::GetPageSetupDialogData()
154 {
155 return m_pageSetupData;
156 }
157
158 int wxMacPageSetupDialog::ShowModal()
159 {
160 m_pageSetupData.GetPrintData().ConvertToNative();
161 ((wxMacCarbonPrintData*)m_pageSetupData.GetPrintData().GetNativeData())->TransferFrom( &m_pageSetupData );
162
163 int result = wxID_CANCEL;
164 #ifdef __LP64__
165 #else
166 OSErr err = noErr;
167 Boolean accepted;
168
169 err = PMSessionPageSetupDialog(
170 ((wxMacCarbonPrintData*)m_pageSetupData.GetPrintData().GetNativeData())->m_macPrintSession,
171 ((wxMacCarbonPrintData*)m_pageSetupData.GetPrintData().GetNativeData())->m_macPageFormat,
172 &accepted );
173
174 if ((err == noErr) && !accepted)
175 {
176 // user clicked Cancel button
177 err = kPMCancel;
178 }
179
180 // If the user did not cancel, flatten and save the PageFormat object
181 // with our document.
182 if (err == noErr)
183 {
184 result = wxID_OK;
185 }
186
187 if ((err != noErr) && (err != kPMCancel))
188 {
189 wxString message;
190
191 message.Printf( wxT("Print Error %d"), err );
192 wxMessageDialog dialog( NULL, message, wxEmptyString, wxICON_HAND | wxOK );
193 dialog.ShowModal();
194 }
195
196 if (result == wxID_OK)
197 {
198 m_pageSetupData.GetPrintData().ConvertFromNative();
199 m_pageSetupData.SetPaperSize( m_pageSetupData.GetPrintData().GetPaperSize() );
200 ((wxMacCarbonPrintData*)m_pageSetupData.GetPrintData().GetNativeData())->TransferTo( &m_pageSetupData );
201 }
202 #endif
203 return result;
204 }
205
206
207 IMPLEMENT_CLASS(wxMacPageMarginsDialog, wxDialog)
208
209 wxMacPageMarginsDialog::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
232 bool 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
251 bool 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
263 bool 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
283 void wxMacPageMarginsDialog::GetMinMargins()
284 {
285 m_MinMarginTopLeft = m_pageSetupDialogData->GetMinMarginTopLeft();
286 m_MinMarginBottomRight = m_pageSetupDialogData->GetMinMarginBottomRight();
287 }
288
289
290
291 #endif