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