]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/printdlg.cpp
WX_PRECOMP, not WXPRECOMP
[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 OSErr err = noErr;
86 Boolean accepted;
87
88 err = PMSessionPrintDialog(
89 ((wxMacCarbonPrintData*)m_printDialogData.GetPrintData().GetNativeData())->m_macPrintSession,
90 ((wxMacCarbonPrintData*)m_printDialogData.GetPrintData().GetNativeData())->m_macPrintSettings,
91 ((wxMacCarbonPrintData*)m_printDialogData.GetPrintData().GetNativeData())->m_macPageFormat,
92 &accepted );
93
94 if ((err == noErr) && !accepted)
95 {
96 // user clicked Cancel button
97 err = kPMCancel;
98 }
99
100 if (err == noErr)
101 {
102 result = wxID_OK;
103 }
104
105 if ((err != noErr) && (err != kPMCancel))
106 {
107 wxString message;
108
109 message.Printf( wxT("Print Error %d"), err );
110 wxMessageDialog dialog( NULL, message, wxEmptyString, wxICON_HAND | wxOK );
111 dialog.ShowModal();
112 }
113
114 if (result == wxID_OK)
115 {
116 m_printDialogData.GetPrintData().ConvertFromNative();
117 ((wxMacCarbonPrintData*)m_printDialogData.GetPrintData().GetNativeData())->TransferTo( &m_printDialogData );
118 }
119
120 return result;
121 }
122
123 wxDC *wxMacPrintDialog::GetPrintDC()
124 {
125 return new wxPrinterDC( m_printDialogData.GetPrintData() );
126 }
127
128 IMPLEMENT_CLASS(wxMacPageSetupDialog, wxPageSetupDialogBase)
129
130 wxMacPageSetupDialog::wxMacPageSetupDialog( wxWindow *p, wxPageSetupData *data )
131 : wxPageSetupDialogBase()
132 {
133 Create( p, data );
134 }
135
136 bool wxMacPageSetupDialog::Create( wxWindow *p, wxPageSetupData *data )
137 {
138 m_dialogParent = p;
139
140 if (data != NULL)
141 m_pageSetupData = (*data);
142
143 return true;
144 }
145
146 wxMacPageSetupDialog::~wxMacPageSetupDialog()
147 {
148 }
149
150 wxPageSetupData& wxMacPageSetupDialog::GetPageSetupDialogData()
151 {
152 return m_pageSetupData;
153 }
154
155 int wxMacPageSetupDialog::ShowModal()
156 {
157 m_pageSetupData.GetPrintData().ConvertToNative();
158 ((wxMacCarbonPrintData*)m_pageSetupData.GetPrintData().GetNativeData())->TransferFrom( &m_pageSetupData );
159
160 int result = wxID_CANCEL;
161 OSErr err = noErr;
162 Boolean accepted;
163
164 err = PMSessionPageSetupDialog(
165 ((wxMacCarbonPrintData*)m_pageSetupData.GetPrintData().GetNativeData())->m_macPrintSession,
166 ((wxMacCarbonPrintData*)m_pageSetupData.GetPrintData().GetNativeData())->m_macPageFormat,
167 &accepted );
168
169 if ((err == noErr) && !accepted)
170 {
171 // user clicked Cancel button
172 err = kPMCancel;
173 }
174
175 // If the user did not cancel, flatten and save the PageFormat object
176 // with our document.
177 if (err == noErr)
178 {
179 result = wxID_OK;
180 }
181
182 if ((err != noErr) && (err != kPMCancel))
183 {
184 wxString message;
185
186 message.Printf( wxT("Print Error %d"), err );
187 wxMessageDialog dialog( NULL, message, wxEmptyString, wxICON_HAND | wxOK );
188 dialog.ShowModal();
189 }
190
191 if (result == wxID_OK)
192 {
193 m_pageSetupData.GetPrintData().ConvertFromNative();
194 m_pageSetupData.SetPaperSize( m_pageSetupData.GetPrintData().GetPaperSize() );
195 ((wxMacCarbonPrintData*)m_pageSetupData.GetPrintData().GetNativeData())->TransferTo( &m_pageSetupData );
196 }
197
198 return result;
199 }
200
201
202 IMPLEMENT_CLASS(wxMacPageMarginsDialog, wxDialog)
203
204 wxMacPageMarginsDialog::wxMacPageMarginsDialog(wxFrame *parent, wxPageSetupData *data) :
205 wxDialog(parent, wxID_ANY, wxString(wxT("Page Margins"))),
206 m_pageSetupDialogData(data)
207 {
208 GetMinMargins();
209 wxBoxSizer *colSizer = new wxBoxSizer(wxVERTICAL);
210 wxFlexGridSizer *gridSizer = new wxFlexGridSizer(4, 5, 5);
211 colSizer->Add(gridSizer, wxSizerFlags().Border(wxALL, 5));
212 gridSizer->Add(new wxStaticText(this, wxID_ANY, wxT("Left (mm):")), wxSizerFlags().Right());
213 gridSizer->Add(m_LeftMargin = new wxTextCtrl(this, wxID_ANY), wxSizerFlags().Left());
214 gridSizer->Add(new wxStaticText(this, wxID_ANY, wxT("Top (mm):")), wxSizerFlags().Right());
215 gridSizer->Add(m_TopMargin = new wxTextCtrl(this, wxID_ANY), wxSizerFlags().Left());
216 gridSizer->Add(new wxStaticText(this, wxID_ANY, wxT("Right (mm):")), wxSizerFlags().Right());
217 gridSizer->Add(m_RightMargin = new wxTextCtrl(this, wxID_ANY), wxSizerFlags().Left());
218 gridSizer->Add(new wxStaticText(this, wxID_ANY, wxT("Bottom (mm):")), wxSizerFlags().Right());
219 gridSizer->Add(m_BottomMargin = new wxTextCtrl(this, wxID_ANY), wxSizerFlags().Left());
220 colSizer->Add(new wxStaticLine(this), wxSizerFlags().Expand().Border(wxTOP|wxBOTTOM, 5));
221 colSizer->Add(CreateButtonSizer(wxOK | wxCANCEL), wxSizerFlags().Expand().Border(wxALL, 5));
222 TransferToWindow();
223 SetSizerAndFit(colSizer);
224 Center(wxBOTH);
225 }
226
227 bool wxMacPageMarginsDialog::TransferToWindow()
228 {
229 wxASSERT(m_pageSetupDialogData);
230 wxPoint topLeft = m_pageSetupDialogData->GetMarginTopLeft();
231 wxPoint bottomRight = m_pageSetupDialogData->GetMarginBottomRight();
232 wxPoint minTopLeft = m_pageSetupDialogData->GetMinMarginTopLeft();
233 wxPoint minBottomRight = m_pageSetupDialogData->GetMinMarginBottomRight();
234 m_LeftMargin->SetValue(wxString::Format(wxT("%d"), wxMax(topLeft.x, minTopLeft.x)));
235 m_LeftMargin->SetSelection(-1, -1);
236 m_TopMargin->SetValue(wxString::Format(wxT("%d"), wxMax(topLeft.y, minTopLeft.y)));
237 m_TopMargin->SetSelection(-1, -1);
238 m_RightMargin->SetValue(wxString::Format(wxT("%d"), wxMax(bottomRight.x, minBottomRight.x)));
239 m_RightMargin->SetSelection(-1, -1);
240 m_BottomMargin->SetValue(wxString::Format(wxT("%d"), wxMax(bottomRight.y, minBottomRight.y)));
241 m_BottomMargin->SetSelection(-1, -1);
242 m_LeftMargin->SetFocus();
243 return true;
244 }
245
246 bool wxMacPageMarginsDialog::TransferDataFromWindow()
247 {
248 wxPoint topLeft, bottomRight;
249 if (!CheckValue(m_LeftMargin, &topLeft.x, m_MinMarginTopLeft.x, wxT("left margin"))) return false;
250 if (!CheckValue(m_TopMargin, &topLeft.y, m_MinMarginTopLeft.y, wxT("top margin"))) return false;
251 if (!CheckValue(m_RightMargin, &bottomRight.x, m_MinMarginBottomRight.x, wxT("right margin"))) return false;
252 if (!CheckValue(m_BottomMargin, &bottomRight.y, m_MinMarginBottomRight.y, wxT("bottom margin"))) return false;
253 m_pageSetupDialogData->SetMarginTopLeft(topLeft);
254 m_pageSetupDialogData->SetMarginBottomRight(bottomRight);
255 return true;
256 }
257
258 bool wxMacPageMarginsDialog::CheckValue(wxTextCtrl* textCtrl, int *value, int minValue, const wxString& name)
259 {
260 long lvalue;
261 if (!textCtrl->GetValue().ToLong(&lvalue))
262 {
263 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"));
264 return false;
265 }
266 if (lvalue < minValue)
267 {
268 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"));
269 textCtrl->SetValue(wxString::Format(wxT("%d"), minValue));
270 textCtrl->SetSelection(-1, -1);
271 textCtrl->SetFocus();
272 return false;
273 }
274 *value = int(lvalue);
275 return true;
276 }
277
278 void wxMacPageMarginsDialog::GetMinMargins()
279 {
280 m_MinMarginTopLeft = m_pageSetupDialogData->GetMinMarginTopLeft();
281 m_MinMarginBottomRight = m_pageSetupDialogData->GetMinMarginBottomRight();
282 }
283
284
285
286 #endif