improved memory liberation (explicitly set to NULL after delete)
[wxWidgets.git] / src / mac / carbon / printdlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: printdlg.cpp
3 // Purpose: wxPrintDialog, wxPageSetupDialog
4 // Author: AUTHOR
5 // Modified by:
6 // Created: ??/??/98
7 // RCS-ID: $Id$
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "printdlg.h"
14 #endif
15
16 #include "wx/object.h"
17 #include "wx/printdlg.h"
18 #include "wx/dcprint.h"
19 #include "wx/msgdlg.h"
20 #include "wx/mac/uma.h"
21 #ifndef __DARWIN__
22 #include "Printing.h"
23 #endif
24
25 #if defined(TARGET_CARBON) && !defined(__DARWIN__)
26 # if PM_USE_SESSION_APIS
27 # include <PMCore.h>
28 # endif
29 # include <PMApplication.h>
30 #endif
31
32 // Use generic page setup dialog: use your own native one if one exists.
33
34 #if !USE_SHARED_LIBRARY
35 IMPLEMENT_DYNAMIC_CLASS(wxPrintDialog, wxDialog)
36 IMPLEMENT_CLASS(wxPageSetupDialog, wxDialog)
37 #endif
38
39 wxPrintDialog::wxPrintDialog()
40 {
41 m_dialogParent = NULL;
42 m_printerDC = NULL;
43 m_destroyDC = TRUE;
44 }
45
46 wxPrintDialog::wxPrintDialog(wxWindow *p, wxPrintDialogData* data)
47 {
48 Create(p, data);
49 }
50
51 wxPrintDialog::wxPrintDialog(wxWindow *p, wxPrintData* data)
52 {
53 wxPrintDialogData data2;
54 if ( data )
55 data2 = *data;
56
57 Create(p, &data2);
58 }
59
60 bool wxPrintDialog::Create(wxWindow *p, wxPrintDialogData* data)
61 {
62 m_dialogParent = p;
63 m_printerDC = NULL;
64 m_destroyDC = TRUE;
65
66 if ( data )
67 m_printDialogData = *data;
68
69 return TRUE;
70 }
71
72 wxPrintDialog::~wxPrintDialog()
73 {
74 if (m_destroyDC && m_printerDC) {
75 delete m_printerDC;
76 m_printerDC = NULL;
77 }
78 }
79
80 int wxPrintDialog::ShowModal()
81 {
82 int result = wxID_CANCEL ;
83 OSErr err ;
84 wxString message ;
85
86 #if !TARGET_CARBON
87 err = ::UMAPrOpen(NULL) ;
88 if ( err == noErr )
89 {
90 m_printDialogData.ConvertToNative() ;
91 if ( ::PrJobDialog( (THPrint) m_printDialogData.GetPrintData().m_macPrintSettings ) )
92 {
93 m_printDialogData.ConvertFromNative() ;
94 result = wxID_OK ;
95 }
96
97 }
98 else
99 {
100 message.Printf( "Print Error %d", err ) ;
101 wxMessageDialog dialog( NULL , message , "", wxICON_HAND | wxOK) ;
102 dialog.ShowModal();
103 }
104 ::UMAPrClose(NULL) ;
105 #else
106 #if PM_USE_SESSION_APIS
107 PMPrintSession macPrintSession = kPMNoReference;
108 Boolean accepted;
109
110 err = ::UMAPrOpen(&macPrintSession) ;
111 if ( err == noErr )
112 {
113 m_printDialogData.ConvertToNative() ;
114
115 // Set up a valid PageFormat object.
116 if (m_printDialogData.GetPrintData().m_macPageFormat == kPMNoPageFormat)
117 {
118 err = PMCreatePageFormat((PMPageFormat *)&m_printDialogData.GetPrintData().m_macPageFormat);
119
120 // Note that PMPageFormat is not session-specific, but calling
121 // PMSessionDefaultPageFormat assigns values specific to the printer
122 // associated with the current printing session.
123 if ((err == noErr) &&
124 (m_printDialogData.GetPrintData().m_macPageFormat != kPMNoPageFormat))
125 {
126 err = PMSessionDefaultPageFormat((PMPrintSession)macPrintSession,
127 (PMPageFormat)m_printDialogData.GetPrintData().m_macPageFormat);
128 }
129 }
130 else
131 {
132 err = PMSessionValidatePageFormat((PMPrintSession)macPrintSession,
133 (PMPageFormat)m_printDialogData.GetPrintData().m_macPageFormat,
134 kPMDontWantBoolean);
135 }
136
137 // Set up a valid PrintSettings object.
138 if (m_printDialogData.GetPrintData().m_macPrintSettings == kPMNoPrintSettings)
139 {
140 err = PMCreatePrintSettings((PMPrintSettings *)&m_printDialogData.GetPrintData().m_macPrintSettings);
141
142 // Note that PMPrintSettings is not session-specific, but calling
143 // PMSessionDefaultPrintSettings assigns values specific to the printer
144 // associated with the current printing session.
145 if ((err == noErr) &&
146 (m_printDialogData.GetPrintData().m_macPrintSettings != kPMNoPrintSettings))
147 {
148 err = PMSessionDefaultPrintSettings((PMPrintSession)macPrintSession,
149 (PMPrintSettings)m_printDialogData.GetPrintData().m_macPrintSettings);
150 }
151 }
152 else
153 {
154 err = PMSessionValidatePrintSettings((PMPrintSession)macPrintSession,
155 (PMPrintSettings)m_printDialogData.GetPrintData().m_macPrintSettings,
156 kPMDontWantBoolean);
157 }
158 // Set a valid page range before displaying the Print dialog
159 if (err == noErr)
160 {
161 // err = PMSetPageRange(m_printDialogData.GetPrintData().m_macPrintSettings,
162 // minPage, maxPage);
163 }
164
165 // Display the Print dialog.
166 if (err == noErr)
167 {
168 err = PMSessionPrintDialog((PMPrintSession)macPrintSession,
169 (PMPrintSettings)m_printDialogData.GetPrintData().m_macPrintSettings,
170 (PMPageFormat)m_printDialogData.GetPrintData().m_macPageFormat,
171 &accepted);
172 if ((err == noErr) && !accepted)
173 {
174 err = kPMCancel; // user clicked Cancel button
175 }
176 }
177 if ( err == noErr )
178 {
179 m_printDialogData.ConvertFromNative() ;
180 result = wxID_OK ;
181 }
182 }
183 if ((err != noErr) && (err != kPMCancel))
184 {
185 message.Printf( "Print Error %d", err ) ;
186 wxMessageDialog dialog( NULL , message , "", wxICON_HAND | wxOK) ;
187 dialog.ShowModal();
188 }
189 ::UMAPrClose(&macPrintSession) ;
190 #else
191 #pragma warning "TODO: Printing for carbon without session apis"
192 #endif
193 #endif
194 return result ;
195 }
196
197 wxDC *wxPrintDialog::GetPrintDC()
198 {
199 return new wxPrinterDC( m_printDialogData.GetPrintData() ) ;
200 }
201
202 /*
203 * wxPageSetupDialog
204 */
205
206 wxPageSetupDialog::wxPageSetupDialog():
207 wxDialog()
208 {
209 m_dialogParent = NULL;
210 }
211
212 wxPageSetupDialog::wxPageSetupDialog(wxWindow *p, wxPageSetupData *data):
213 wxDialog()
214 {
215 Create(p, data);
216 }
217
218 bool wxPageSetupDialog::Create(wxWindow *p, wxPageSetupData *data)
219 {
220 m_dialogParent = p;
221
222 if (data)
223 m_pageSetupData = (*data);
224
225 return TRUE;
226 }
227
228 wxPageSetupDialog::~wxPageSetupDialog()
229 {
230 }
231
232 int wxPageSetupDialog::ShowModal()
233 {
234 int result = wxID_CANCEL ;
235 OSErr err ;
236 wxString message ;
237
238 #if !TARGET_CARBON
239 err = ::UMAPrOpen(NULL) ;
240 if ( err == noErr )
241 {
242 m_pageSetupData.ConvertToNative() ;
243 if ( ::PrStlDialog( (THPrint) m_pageSetupData.GetPrintData().m_macPrintSettings ) )
244 {
245 m_pageSetupData.ConvertFromNative() ;
246 result = wxID_OK ;
247 }
248
249 }
250 else
251 {
252 message.Printf( "Print Error %d", err ) ;
253 wxMessageDialog dialog( NULL , message , "", wxICON_HAND | wxOK) ;
254 dialog.ShowModal();
255 }
256 ::UMAPrClose(NULL) ;
257 #else
258 #if PM_USE_SESSION_APIS
259 PMPrintSession macPrintSession = kPMNoReference;
260 Boolean accepted;
261
262 err = ::UMAPrOpen(&macPrintSession) ;
263 if ( err == noErr )
264 {
265 m_pageSetupData.ConvertToNative() ;
266
267 // Set up a valid PageFormat object.
268 if (m_pageSetupData.GetPrintData().m_macPageFormat == kPMNoPageFormat)
269 {
270 err = PMCreatePageFormat((PMPageFormat *)&m_pageSetupData.GetPrintData().m_macPageFormat);
271
272 // Note that PMPageFormat is not session-specific, but calling
273 // PMSessionDefaultPageFormat assigns values specific to the printer
274 // associated with the current printing session.
275 if ((err == noErr) &&
276 (m_pageSetupData.GetPrintData().m_macPageFormat != kPMNoPageFormat))
277 {
278 err = PMSessionDefaultPageFormat((PMPrintSession)macPrintSession,
279 (PMPageFormat)m_pageSetupData.GetPrintData().m_macPageFormat);
280 }
281 }
282 else
283 {
284 err = PMSessionValidatePageFormat((PMPrintSession)macPrintSession,
285 (PMPageFormat)m_pageSetupData.GetPrintData().m_macPageFormat,
286 kPMDontWantBoolean);
287 }
288
289 // Display the Page Setup dialog.
290 if (err == noErr)
291 {
292 err = PMSessionPageSetupDialog((PMPrintSession)macPrintSession,
293 (PMPageFormat)m_pageSetupData.GetPrintData().m_macPageFormat,
294 &accepted);
295 if ((err == noErr) && !accepted)
296 {
297 err = kPMCancel; // user clicked Cancel button
298 }
299 }
300
301 // If the user did not cancel, flatten and save the PageFormat object
302 // with our document.
303 if (err == noErr) {
304 // err = FlattenAndSavePageFormat(m_pageSetupData.GetPrintData().m_macPageFormat);
305 m_pageSetupData.ConvertFromNative() ;
306 result = wxID_OK ;
307 }
308 }
309 if ((err != noErr) && (err != kPMCancel))
310 {
311 message.Printf( "Print Error %d", err ) ;
312 wxMessageDialog dialog( NULL , message , "", wxICON_HAND | wxOK) ;
313 dialog.ShowModal();
314 }
315 ::UMAPrClose(&macPrintSession) ;
316 #else
317 #pragma warning "TODO: Printing for carbon without session apis"
318 #endif
319 #endif
320 return result ;
321 }
322