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