]> git.saurik.com Git - wxWidgets.git/blob - src/osx/carbon/printdlg.cpp
fixing dangling includes
[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 #if wxOSX_USE_CARBON
81 int wxMacPrintDialog::ShowModal()
82 {
83 m_printDialogData.GetPrintData().ConvertToNative();
84 ((wxOSXPrintData*)m_printDialogData.GetPrintData().GetNativeData())->TransferFrom( &m_printDialogData );
85
86 int result = wxID_CANCEL;
87
88 OSErr err = noErr;
89 Boolean accepted;
90 wxOSXPrintData* nativeData = (wxOSXPrintData*)m_printDialogData.GetPrintData().GetNativeData();
91 err = PMSessionPrintDialog(nativeData->GetPrintSession(), nativeData->GetPrintSettings(),
92 nativeData->GetPageFormat(), &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 ((wxOSXPrintData*)m_printDialogData.GetPrintData().GetNativeData())->TransferTo( &m_printDialogData );
118 }
119 return result;
120 }
121 #endif
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 #if wxOSX_USE_CARBON
156 int wxMacPageSetupDialog::ShowModal()
157 {
158 m_pageSetupData.GetPrintData().ConvertToNative();
159 wxOSXPrintData* nativeData = (wxOSXPrintData*)m_pageSetupData.GetPrintData().GetNativeData();
160 nativeData->TransferFrom( &m_pageSetupData );
161
162 int result = wxID_CANCEL;
163 OSErr err = noErr;
164 Boolean accepted;
165
166 err = PMSessionPageSetupDialog( nativeData->GetPrintSession(), nativeData->GetPageFormat(),
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 nativeData->TransferTo( &m_pageSetupData );
196 }
197 return result;
198 }
199 #endif
200
201 IMPLEMENT_CLASS(wxMacPageMarginsDialog, wxDialog)
202
203 wxMacPageMarginsDialog::wxMacPageMarginsDialog(wxFrame *parent, wxPageSetupData *data) :
204 wxDialog(parent, wxID_ANY, wxString(wxT("Page Margins"))),
205 m_pageSetupDialogData(data)
206 {
207 GetMinMargins();
208 wxBoxSizer *colSizer = new wxBoxSizer(wxVERTICAL);
209 wxFlexGridSizer *gridSizer = new wxFlexGridSizer(4, 5, 5);
210 colSizer->Add(gridSizer, wxSizerFlags().Border(wxALL, 5));
211 gridSizer->Add(new wxStaticText(this, wxID_ANY, wxT("Left (mm):")), wxSizerFlags().Right());
212 gridSizer->Add(m_LeftMargin = new wxTextCtrl(this, wxID_ANY), wxSizerFlags().Left());
213 gridSizer->Add(new wxStaticText(this, wxID_ANY, wxT("Top (mm):")), wxSizerFlags().Right());
214 gridSizer->Add(m_TopMargin = new wxTextCtrl(this, wxID_ANY), wxSizerFlags().Left());
215 gridSizer->Add(new wxStaticText(this, wxID_ANY, wxT("Right (mm):")), wxSizerFlags().Right());
216 gridSizer->Add(m_RightMargin = new wxTextCtrl(this, wxID_ANY), wxSizerFlags().Left());
217 gridSizer->Add(new wxStaticText(this, wxID_ANY, wxT("Bottom (mm):")), wxSizerFlags().Right());
218 gridSizer->Add(m_BottomMargin = new wxTextCtrl(this, wxID_ANY), wxSizerFlags().Left());
219 colSizer->Add(new wxStaticLine(this), wxSizerFlags().Expand().Border(wxTOP|wxBOTTOM, 5));
220 colSizer->Add(CreateButtonSizer(wxOK | wxCANCEL), wxSizerFlags().Expand().Border(wxALL, 5));
221 TransferToWindow();
222 SetSizerAndFit(colSizer);
223 Center(wxBOTH);
224 }
225
226 bool wxMacPageMarginsDialog::TransferToWindow()
227 {
228 wxASSERT(m_pageSetupDialogData);
229 wxPoint topLeft = m_pageSetupDialogData->GetMarginTopLeft();
230 wxPoint bottomRight = m_pageSetupDialogData->GetMarginBottomRight();
231 wxPoint minTopLeft = m_pageSetupDialogData->GetMinMarginTopLeft();
232 wxPoint minBottomRight = m_pageSetupDialogData->GetMinMarginBottomRight();
233 m_LeftMargin->SetValue(wxString::Format(wxT("%d"), wxMax(topLeft.x, minTopLeft.x)));
234 m_LeftMargin->SetSelection(-1, -1);
235 m_TopMargin->SetValue(wxString::Format(wxT("%d"), wxMax(topLeft.y, minTopLeft.y)));
236 m_TopMargin->SetSelection(-1, -1);
237 m_RightMargin->SetValue(wxString::Format(wxT("%d"), wxMax(bottomRight.x, minBottomRight.x)));
238 m_RightMargin->SetSelection(-1, -1);
239 m_BottomMargin->SetValue(wxString::Format(wxT("%d"), wxMax(bottomRight.y, minBottomRight.y)));
240 m_BottomMargin->SetSelection(-1, -1);
241 m_LeftMargin->SetFocus();
242 return true;
243 }
244
245 bool wxMacPageMarginsDialog::TransferDataFromWindow()
246 {
247 wxPoint topLeft, bottomRight;
248 if (!CheckValue(m_LeftMargin, &topLeft.x, m_MinMarginTopLeft.x, wxT("left margin"))) return false;
249 if (!CheckValue(m_TopMargin, &topLeft.y, m_MinMarginTopLeft.y, wxT("top margin"))) return false;
250 if (!CheckValue(m_RightMargin, &bottomRight.x, m_MinMarginBottomRight.x, wxT("right margin"))) return false;
251 if (!CheckValue(m_BottomMargin, &bottomRight.y, m_MinMarginBottomRight.y, wxT("bottom margin"))) return false;
252 m_pageSetupDialogData->SetMarginTopLeft(topLeft);
253 m_pageSetupDialogData->SetMarginBottomRight(bottomRight);
254 return true;
255 }
256
257 bool wxMacPageMarginsDialog::CheckValue(wxTextCtrl* textCtrl, int *value, int minValue, const wxString& name)
258 {
259 long lvalue;
260 if (!textCtrl->GetValue().ToLong(&lvalue))
261 {
262 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"));
263 return false;
264 }
265 if (lvalue < minValue)
266 {
267 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"));
268 textCtrl->SetValue(wxString::Format(wxT("%d"), minValue));
269 textCtrl->SetSelection(-1, -1);
270 textCtrl->SetFocus();
271 return false;
272 }
273 *value = int(lvalue);
274 return true;
275 }
276
277 void wxMacPageMarginsDialog::GetMinMargins()
278 {
279 m_MinMarginTopLeft = m_pageSetupDialogData->GetMinMarginTopLeft();
280 m_MinMarginBottomRight = m_pageSetupDialogData->GetMinMarginBottomRight();
281 }
282
283
284
285 #endif