carbon printing extended
[wxWidgets.git] / src / mac / carbon / printdlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #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 = noErr ;
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( wxT("Print Error %d"), err ) ;
101 wxMessageDialog dialog( NULL , message , wxT(""), wxICON_HAND | wxOK) ;
102 dialog.ShowModal();
103 }
104 ::UMAPrClose(NULL) ;
105 #else
106 #if PM_USE_SESSION_APIS
107 Boolean accepted;
108
109 {
110 m_printDialogData.ConvertToNative() ;
111 // Display the Print dialog.
112 if (err == noErr)
113 {
114 err = PMSessionPrintDialog((PMPrintSession)m_printDialogData.GetPrintData().m_macPrintSession,
115 (PMPrintSettings)m_printDialogData.GetPrintData().m_macPrintSettings,
116 (PMPageFormat)m_printDialogData.GetPrintData().m_macPageFormat,
117 &accepted);
118 if ((err == noErr) && !accepted)
119 {
120 err = kPMCancel; // user clicked Cancel button
121 }
122 }
123 if ( err == noErr )
124 {
125 m_printDialogData.ConvertFromNative() ;
126 result = wxID_OK ;
127 }
128 }
129 if ((err != noErr) && (err != kPMCancel))
130 {
131 message.Printf( wxT("Print Error %d"), err ) ;
132 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
133 dialog.ShowModal();
134 }
135 #else
136 #pragma warning "TODO: Printing for carbon without session apis"
137 #endif
138 #endif
139 return result ;
140 }
141
142 wxDC *wxPrintDialog::GetPrintDC()
143 {
144 return new wxPrinterDC( m_printDialogData.GetPrintData() ) ;
145 }
146
147 /*
148 * wxPageSetupDialog
149 */
150
151 wxPageSetupDialog::wxPageSetupDialog():
152 wxDialog()
153 {
154 m_dialogParent = NULL;
155 }
156
157 wxPageSetupDialog::wxPageSetupDialog(wxWindow *p, wxPageSetupData *data):
158 wxDialog()
159 {
160 Create(p, data);
161 }
162
163 bool wxPageSetupDialog::Create(wxWindow *p, wxPageSetupData *data)
164 {
165 m_dialogParent = p;
166
167 if (data)
168 m_pageSetupData = (*data);
169
170 return TRUE;
171 }
172
173 wxPageSetupDialog::~wxPageSetupDialog()
174 {
175 }
176
177 int wxPageSetupDialog::ShowModal()
178 {
179 int result = wxID_CANCEL ;
180 OSErr err = noErr ;
181 wxString message ;
182
183 #if !TARGET_CARBON
184 err = ::UMAPrOpen(NULL) ;
185 if ( err == noErr )
186 {
187 m_pageSetupData.ConvertToNative() ;
188 if ( ::PrStlDialog( (THPrint) m_pageSetupData.GetPrintData().m_macPrintSettings ) )
189 {
190 m_pageSetupData.ConvertFromNative() ;
191 result = wxID_OK ;
192 }
193
194 }
195 else
196 {
197 message.Printf( wxT("Print Error %d"), err ) ;
198 wxMessageDialog dialog( NULL , message , wxEmptyString , wxICON_HAND | wxOK) ;
199 dialog.ShowModal();
200 }
201 ::UMAPrClose(NULL) ;
202 #else
203 #if PM_USE_SESSION_APIS
204 Boolean accepted;
205 {
206 m_pageSetupData.ConvertToNative() ;
207
208 // Display the Page Setup dialog.
209 if (err == noErr)
210 {
211 err = PMSessionPageSetupDialog((PMPrintSession)m_pageSetupData.GetPrintData().m_macPrintSession,
212 (PMPageFormat)m_pageSetupData.GetPrintData().m_macPageFormat,
213 &accepted);
214 if ((err == noErr) && !accepted)
215 {
216 err = kPMCancel; // user clicked Cancel button
217 }
218 }
219
220 // If the user did not cancel, flatten and save the PageFormat object
221 // with our document.
222 if (err == noErr) {
223 // err = FlattenAndSavePageFormat(m_pageSetupData.GetPrintData().m_macPageFormat);
224 m_pageSetupData.ConvertFromNative() ;
225 result = wxID_OK ;
226 }
227 }
228 if ((err != noErr) && (err != kPMCancel))
229 {
230 message.Printf( wxT("Print Error %d"), err ) ;
231 wxMessageDialog dialog( NULL , message , wxEmptyString, wxICON_HAND | wxOK) ;
232 dialog.ShowModal();
233 }
234 #else
235 #pragma warning "TODO: Printing for carbon without session apis"
236 #endif
237 #endif
238 return result ;
239 }
240