1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/printdlg.cpp
3 // Purpose: wxPrintDialog, wxPageSetupDialog
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
14 #if wxUSE_PRINTING_ARCHITECTURE
16 #include "wx/printdlg.h"
19 #include "wx/object.h"
20 #include "wx/dcprint.h"
21 #include "wx/msgdlg.h"
22 #include "wx/textctrl.h"
24 #include "wx/stattext.h"
27 #include "wx/osx/printdlg.h"
28 #include "wx/osx/private/print.h"
29 #include "wx/osx/private.h"
30 #include "wx/statline.h"
33 // Use generic page setup dialog: use your own native one if one exists.
35 IMPLEMENT_DYNAMIC_CLASS(wxMacPrintDialog
, wxPrintDialogBase
)
38 wxMacPrintDialog::wxMacPrintDialog()
40 m_dialogParent
= NULL
;
45 wxMacPrintDialog::wxMacPrintDialog( wxWindow
*p
, wxPrintDialogData
*data
)
50 wxMacPrintDialog::wxMacPrintDialog( wxWindow
*p
, wxPrintData
*data
)
52 wxPrintDialogData data2
;
59 bool wxMacPrintDialog::Create( wxWindow
*p
, wxPrintDialogData
*data
)
66 m_printDialogData
= *data
;
71 wxMacPrintDialog::~wxMacPrintDialog()
73 if (m_destroyDC
&& m_printerDC
)
80 int wxMacPrintDialog::ShowModal()
82 m_printDialogData
.GetPrintData().ConvertToNative();
83 ((wxMacCarbonPrintData
*)m_printDialogData
.GetPrintData().GetNativeData())->TransferFrom( &m_printDialogData
);
85 int result
= wxID_CANCEL
;
90 err
= PMSessionPrintDialog(
91 ((wxMacCarbonPrintData
*)m_printDialogData
.GetPrintData().GetNativeData())->m_macPrintSession
,
92 ((wxMacCarbonPrintData
*)m_printDialogData
.GetPrintData().GetNativeData())->m_macPrintSettings
,
93 ((wxMacCarbonPrintData
*)m_printDialogData
.GetPrintData().GetNativeData())->m_macPageFormat
,
96 if ((err
== noErr
) && !accepted
)
98 // user clicked Cancel button
107 if ((err
!= noErr
) && (err
!= kPMCancel
))
111 message
.Printf( wxT("Print Error %d"), err
);
112 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
);
116 if (result
== wxID_OK
)
118 m_printDialogData
.GetPrintData().ConvertFromNative();
119 ((wxMacCarbonPrintData
*)m_printDialogData
.GetPrintData().GetNativeData())->TransferTo( &m_printDialogData
);
122 // TODO use NSPrintPanel
127 wxDC
*wxMacPrintDialog::GetPrintDC()
129 return new wxPrinterDC( m_printDialogData
.GetPrintData() );
132 IMPLEMENT_CLASS(wxMacPageSetupDialog
, wxPageSetupDialogBase
)
134 wxMacPageSetupDialog::wxMacPageSetupDialog( wxWindow
*p
, wxPageSetupData
*data
)
135 : wxPageSetupDialogBase()
140 bool wxMacPageSetupDialog::Create( wxWindow
*p
, wxPageSetupData
*data
)
145 m_pageSetupData
= (*data
);
150 wxMacPageSetupDialog::~wxMacPageSetupDialog()
154 wxPageSetupData
& wxMacPageSetupDialog::GetPageSetupDialogData()
156 return m_pageSetupData
;
159 int wxMacPageSetupDialog::ShowModal()
161 m_pageSetupData
.GetPrintData().ConvertToNative();
162 ((wxMacCarbonPrintData
*)m_pageSetupData
.GetPrintData().GetNativeData())->TransferFrom( &m_pageSetupData
);
164 int result
= wxID_CANCEL
;
169 err
= PMSessionPageSetupDialog(
170 ((wxMacCarbonPrintData
*)m_pageSetupData
.GetPrintData().GetNativeData())->m_macPrintSession
,
171 ((wxMacCarbonPrintData
*)m_pageSetupData
.GetPrintData().GetNativeData())->m_macPageFormat
,
174 if ((err
== noErr
) && !accepted
)
176 // user clicked Cancel button
180 // If the user did not cancel, flatten and save the PageFormat object
181 // with our document.
187 if ((err
!= noErr
) && (err
!= kPMCancel
))
191 message
.Printf( wxT("Print Error %d"), err
);
192 wxMessageDialog
dialog( NULL
, message
, wxEmptyString
, wxICON_HAND
| wxOK
);
196 if (result
== wxID_OK
)
198 m_pageSetupData
.GetPrintData().ConvertFromNative();
199 m_pageSetupData
.SetPaperSize( m_pageSetupData
.GetPrintData().GetPaperSize() );
200 ((wxMacCarbonPrintData
*)m_pageSetupData
.GetPrintData().GetNativeData())->TransferTo( &m_pageSetupData
);
209 IMPLEMENT_CLASS(wxMacPageMarginsDialog
, wxDialog
)
211 wxMacPageMarginsDialog::wxMacPageMarginsDialog(wxFrame
*parent
, wxPageSetupData
*data
) :
212 wxDialog(parent
, wxID_ANY
, wxString(wxT("Page Margins"))),
213 m_pageSetupDialogData(data
)
216 wxBoxSizer
*colSizer
= new wxBoxSizer(wxVERTICAL
);
217 wxFlexGridSizer
*gridSizer
= new wxFlexGridSizer(4, 5, 5);
218 colSizer
->Add(gridSizer
, wxSizerFlags().Border(wxALL
, 5));
219 gridSizer
->Add(new wxStaticText(this, wxID_ANY
, wxT("Left (mm):")), wxSizerFlags().Right());
220 gridSizer
->Add(m_LeftMargin
= new wxTextCtrl(this, wxID_ANY
), wxSizerFlags().Left());
221 gridSizer
->Add(new wxStaticText(this, wxID_ANY
, wxT("Top (mm):")), wxSizerFlags().Right());
222 gridSizer
->Add(m_TopMargin
= new wxTextCtrl(this, wxID_ANY
), wxSizerFlags().Left());
223 gridSizer
->Add(new wxStaticText(this, wxID_ANY
, wxT("Right (mm):")), wxSizerFlags().Right());
224 gridSizer
->Add(m_RightMargin
= new wxTextCtrl(this, wxID_ANY
), wxSizerFlags().Left());
225 gridSizer
->Add(new wxStaticText(this, wxID_ANY
, wxT("Bottom (mm):")), wxSizerFlags().Right());
226 gridSizer
->Add(m_BottomMargin
= new wxTextCtrl(this, wxID_ANY
), wxSizerFlags().Left());
227 colSizer
->Add(new wxStaticLine(this), wxSizerFlags().Expand().Border(wxTOP
|wxBOTTOM
, 5));
228 colSizer
->Add(CreateButtonSizer(wxOK
| wxCANCEL
), wxSizerFlags().Expand().Border(wxALL
, 5));
230 SetSizerAndFit(colSizer
);
234 bool wxMacPageMarginsDialog::TransferToWindow()
236 wxASSERT(m_pageSetupDialogData
);
237 wxPoint topLeft
= m_pageSetupDialogData
->GetMarginTopLeft();
238 wxPoint bottomRight
= m_pageSetupDialogData
->GetMarginBottomRight();
239 wxPoint minTopLeft
= m_pageSetupDialogData
->GetMinMarginTopLeft();
240 wxPoint minBottomRight
= m_pageSetupDialogData
->GetMinMarginBottomRight();
241 m_LeftMargin
->SetValue(wxString::Format(wxT("%d"), wxMax(topLeft
.x
, minTopLeft
.x
)));
242 m_LeftMargin
->SetSelection(-1, -1);
243 m_TopMargin
->SetValue(wxString::Format(wxT("%d"), wxMax(topLeft
.y
, minTopLeft
.y
)));
244 m_TopMargin
->SetSelection(-1, -1);
245 m_RightMargin
->SetValue(wxString::Format(wxT("%d"), wxMax(bottomRight
.x
, minBottomRight
.x
)));
246 m_RightMargin
->SetSelection(-1, -1);
247 m_BottomMargin
->SetValue(wxString::Format(wxT("%d"), wxMax(bottomRight
.y
, minBottomRight
.y
)));
248 m_BottomMargin
->SetSelection(-1, -1);
249 m_LeftMargin
->SetFocus();
253 bool wxMacPageMarginsDialog::TransferDataFromWindow()
255 wxPoint topLeft
, bottomRight
;
256 if (!CheckValue(m_LeftMargin
, &topLeft
.x
, m_MinMarginTopLeft
.x
, wxT("left margin"))) return false;
257 if (!CheckValue(m_TopMargin
, &topLeft
.y
, m_MinMarginTopLeft
.y
, wxT("top margin"))) return false;
258 if (!CheckValue(m_RightMargin
, &bottomRight
.x
, m_MinMarginBottomRight
.x
, wxT("right margin"))) return false;
259 if (!CheckValue(m_BottomMargin
, &bottomRight
.y
, m_MinMarginBottomRight
.y
, wxT("bottom margin"))) return false;
260 m_pageSetupDialogData
->SetMarginTopLeft(topLeft
);
261 m_pageSetupDialogData
->SetMarginBottomRight(bottomRight
);
265 bool wxMacPageMarginsDialog::CheckValue(wxTextCtrl
* textCtrl
, int *value
, int minValue
, const wxString
& name
)
268 if (!textCtrl
->GetValue().ToLong(&lvalue
))
270 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"));
273 if (lvalue
< minValue
)
275 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"));
276 textCtrl
->SetValue(wxString::Format(wxT("%d"), minValue
));
277 textCtrl
->SetSelection(-1, -1);
278 textCtrl
->SetFocus();
281 *value
= int(lvalue
);
285 void wxMacPageMarginsDialog::GetMinMargins()
287 m_MinMarginTopLeft
= m_pageSetupDialogData
->GetMinMarginTopLeft();
288 m_MinMarginBottomRight
= m_pageSetupDialogData
->GetMinMarginBottomRight();