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