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