]> git.saurik.com Git - wxWidgets.git/blob - src/osx/carbon/printdlg.cpp
using subclass as impl ptr, common code in macro because mix-in are not possible...
[wxWidgets.git] / src / osx / carbon / printdlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/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/osx/printdlg.h"
28 #include "wx/osx/private/print.h"
29 #include "wx/osx/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 #if wxOSX_USE_CARBON
88 OSErr err = noErr;
89 Boolean accepted;
90 err = PMSessionPrintDialog(
91 ((wxMacCarbonPrintData*)m_printDialogData.GetPrintData().GetNativeData())->m_macPrintSession,
92 ((wxMacCarbonPrintData*)m_printDialogData.GetPrintData().GetNativeData())->m_macPrintSettings,
93 ((wxMacCarbonPrintData*)m_printDialogData.GetPrintData().GetNativeData())->m_macPageFormat,
94 &accepted );
95
96 if ((err == noErr) && !accepted)
97 {
98 // user clicked Cancel button
99 err = kPMCancel;
100 }
101
102 if (err == noErr)
103 {
104 result = wxID_OK;
105 }
106
107 if ((err != noErr) && (err != kPMCancel))
108 {
109 wxString message;
110
111 message.Printf( wxT("Print Error %d"), err );
112 wxMessageDialog dialog( NULL, message, wxEmptyString, wxICON_HAND | wxOK );
113 dialog.ShowModal();
114 }
115
116 if (result == wxID_OK)
117 {
118 m_printDialogData.GetPrintData().ConvertFromNative();
119 ((wxMacCarbonPrintData*)m_printDialogData.GetPrintData().GetNativeData())->TransferTo( &m_printDialogData );
120 }
121 #else
122 // TODO use NSPrintPanel
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 #if wxOSX_USE_CARBON
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 #else
203 // TODO
204 #endif
205 return result;
206 }
207
208
209 IMPLEMENT_CLASS(wxMacPageMarginsDialog, wxDialog)
210
211 wxMacPageMarginsDialog::wxMacPageMarginsDialog(wxFrame *parent, wxPageSetupData *data) :
212 wxDialog(parent, wxID_ANY, wxString(wxT("Page Margins"))),
213 m_pageSetupDialogData(data)
214 {
215 GetMinMargins();
216 wxBoxSizer *colSizer = new wxBoxSizer(wxVERTICAL);
217 wxFlexGridSizer *gridSizer = new wxFlexGridSizer(4, 5, 5);
218 colSizer->Add(gridSizer, wxSizerFlags().Border(wxALL, 5));
219 gridSizer->Add(new wxStaticText(this, wxID_ANY, wxT("Left (mm):")), wxSizerFlags().Right());
220 gridSizer->Add(m_LeftMargin = new wxTextCtrl(this, wxID_ANY), wxSizerFlags().Left());
221 gridSizer->Add(new wxStaticText(this, wxID_ANY, wxT("Top (mm):")), wxSizerFlags().Right());
222 gridSizer->Add(m_TopMargin = new wxTextCtrl(this, wxID_ANY), wxSizerFlags().Left());
223 gridSizer->Add(new wxStaticText(this, wxID_ANY, wxT("Right (mm):")), wxSizerFlags().Right());
224 gridSizer->Add(m_RightMargin = new wxTextCtrl(this, wxID_ANY), wxSizerFlags().Left());
225 gridSizer->Add(new wxStaticText(this, wxID_ANY, wxT("Bottom (mm):")), wxSizerFlags().Right());
226 gridSizer->Add(m_BottomMargin = new wxTextCtrl(this, wxID_ANY), wxSizerFlags().Left());
227 colSizer->Add(new wxStaticLine(this), wxSizerFlags().Expand().Border(wxTOP|wxBOTTOM, 5));
228 colSizer->Add(CreateButtonSizer(wxOK | wxCANCEL), wxSizerFlags().Expand().Border(wxALL, 5));
229 TransferToWindow();
230 SetSizerAndFit(colSizer);
231 Center(wxBOTH);
232 }
233
234 bool wxMacPageMarginsDialog::TransferToWindow()
235 {
236 wxASSERT(m_pageSetupDialogData);
237 wxPoint topLeft = m_pageSetupDialogData->GetMarginTopLeft();
238 wxPoint bottomRight = m_pageSetupDialogData->GetMarginBottomRight();
239 wxPoint minTopLeft = m_pageSetupDialogData->GetMinMarginTopLeft();
240 wxPoint minBottomRight = m_pageSetupDialogData->GetMinMarginBottomRight();
241 m_LeftMargin->SetValue(wxString::Format(wxT("%d"), wxMax(topLeft.x, minTopLeft.x)));
242 m_LeftMargin->SetSelection(-1, -1);
243 m_TopMargin->SetValue(wxString::Format(wxT("%d"), wxMax(topLeft.y, minTopLeft.y)));
244 m_TopMargin->SetSelection(-1, -1);
245 m_RightMargin->SetValue(wxString::Format(wxT("%d"), wxMax(bottomRight.x, minBottomRight.x)));
246 m_RightMargin->SetSelection(-1, -1);
247 m_BottomMargin->SetValue(wxString::Format(wxT("%d"), wxMax(bottomRight.y, minBottomRight.y)));
248 m_BottomMargin->SetSelection(-1, -1);
249 m_LeftMargin->SetFocus();
250 return true;
251 }
252
253 bool wxMacPageMarginsDialog::TransferDataFromWindow()
254 {
255 wxPoint topLeft, bottomRight;
256 if (!CheckValue(m_LeftMargin, &topLeft.x, m_MinMarginTopLeft.x, wxT("left margin"))) return false;
257 if (!CheckValue(m_TopMargin, &topLeft.y, m_MinMarginTopLeft.y, wxT("top margin"))) return false;
258 if (!CheckValue(m_RightMargin, &bottomRight.x, m_MinMarginBottomRight.x, wxT("right margin"))) return false;
259 if (!CheckValue(m_BottomMargin, &bottomRight.y, m_MinMarginBottomRight.y, wxT("bottom margin"))) return false;
260 m_pageSetupDialogData->SetMarginTopLeft(topLeft);
261 m_pageSetupDialogData->SetMarginBottomRight(bottomRight);
262 return true;
263 }
264
265 bool wxMacPageMarginsDialog::CheckValue(wxTextCtrl* textCtrl, int *value, int minValue, const wxString& name)
266 {
267 long lvalue;
268 if (!textCtrl->GetValue().ToLong(&lvalue))
269 {
270 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"));
271 return false;
272 }
273 if (lvalue < minValue)
274 {
275 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"));
276 textCtrl->SetValue(wxString::Format(wxT("%d"), minValue));
277 textCtrl->SetSelection(-1, -1);
278 textCtrl->SetFocus();
279 return false;
280 }
281 *value = int(lvalue);
282 return true;
283 }
284
285 void wxMacPageMarginsDialog::GetMinMargins()
286 {
287 m_MinMarginTopLeft = m_pageSetupDialogData->GetMinMarginTopLeft();
288 m_MinMarginBottomRight = m_pageSetupDialogData->GetMinMarginBottomRight();
289 }
290
291
292
293 #endif