1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Generic print dialogs
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "prntdlgg.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
25 #define WINDOWS_PRINTING (wxTheApp->GetPrintMode() == wxPRINT_WINDOWS)
32 #include "wx/stattext.h"
33 #include "wx/button.h"
34 #include "wx/checkbox.h"
35 #include "wx/textctrl.h"
36 #include "wx/radiobox.h"
37 #include "wx/filedlg.h"
38 #include "wx/choice.h"
42 #include "wx/generic/prntdlgg.h"
43 #include "wx/printdlg.h"
48 #if !USE_SHARED_LIBRARY
49 IMPLEMENT_CLASS(wxGenericPrintDialog
, wxDialog
)
50 IMPLEMENT_CLASS(wxGenericPrintSetupDialog
, wxDialog
)
51 IMPLEMENT_CLASS(wxGenericPageSetupDialog
, wxDialog
)
53 BEGIN_EVENT_TABLE(wxGenericPrintDialog
, wxDialog
)
54 EVT_BUTTON(wxID_OK
, wxGenericPrintDialog::OnOK
)
55 EVT_BUTTON(wxPRINTID_SETUP
, wxGenericPrintDialog::OnSetup
)
56 EVT_RADIOBOX(wxPRINTID_RANGE
, wxGenericPrintDialog::OnRange
)
59 BEGIN_EVENT_TABLE(wxGenericPageSetupDialog
, wxDialog
)
60 EVT_BUTTON(wxPRINTID_SETUP
, wxGenericPageSetupDialog::OnPrinter
)
64 extern wxPrintPaperDatabase
*wxThePrintPaperDatabase
;
67 * Generic print dialog for non-Windows printing use.
71 wxGenericPrintDialog::wxGenericPrintDialog(wxWindow
*parent
, wxPrintData
* data
):
72 wxDialog(parent
, -1, _("Print"), wxPoint(0, 0), wxSize(600, 600), wxDEFAULT_DIALOG_STYLE
|wxDIALOG_MODAL
)
78 int buttonHeight
= 25;
83 wxButton
*okButton
= new wxButton(this, wxID_OK
, _("OK"), wxPoint(5, yPos
), wxSize(buttonWidth
, buttonHeight
));
84 (void) new wxButton(this, wxID_CANCEL
, _("Cancel"), wxPoint(buttonWidth
+ 5 + spacing
, yPos
), wxSize(buttonWidth
, buttonHeight
));
86 setupButton
= new wxButton(this, wxPRINTID_SETUP
, _("Setup..."), wxPoint(buttonWidth
*2 + 5 + 2*spacing
, yPos
), wxSize(buttonWidth
, buttonHeight
));
88 okButton
->SetDefault();
94 choices
[0] = _("All");
95 choices
[1] = _("Pages");
97 fromText
= (wxTextCtrl
*)NULL
;
99 rangeRadioBox
= new wxRadioBox(this, wxPRINTID_RANGE
, _("Print Range"),
100 wxPoint(5, yPos
), wxSize(-1, -1), 2, choices
, 2);
101 rangeRadioBox
->SetSelection(1);
105 int staticWidth
= 45;
109 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("From:"), wxPoint(xPos
, yPos
));
112 fromText
= new wxTextCtrl(this, wxPRINTID_FROM
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
113 xPos
+= spacing
+ textWidth
;
115 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("To:"), wxPoint(xPos
, yPos
));
118 toText
= new wxTextCtrl(this, wxPRINTID_TO
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
119 xPos
+= spacing
+ textWidth
;
121 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Copies:"), wxPoint(xPos
, yPos
));
122 xPos
+= spacing
+ staticWidth
;
124 noCopiesText
= new wxTextCtrl(this, wxPRINTID_COPIES
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
129 printToFileCheckBox
= new wxCheckBox(this, wxPRINTID_PRINTTOFILE
, _("Print to File"), wxPoint(xPos
, yPos
));
134 // Calls wxWindow::OnInitDialog and then wxGenericPrintDialog::TransferDataToWindow
138 int wxGenericPrintDialog::ShowModal(void)
140 if ( printData
.GetSetupDialog() )
142 wxGenericPrintSetupDialog
*genericPrintSetupDialog
=
143 new wxGenericPrintSetupDialog(GetParent(), wxThePrintSetupData
);
144 int ret
= genericPrintSetupDialog
->ShowModal();
145 if ( ret
!= wxID_CANCEL
)
147 *wxThePrintSetupData
= genericPrintSetupDialog
->printData
;
149 genericPrintSetupDialog
->Close(TRUE
);
154 return wxDialog::ShowModal();
158 wxGenericPrintDialog::~wxGenericPrintDialog(void)
162 void wxGenericPrintDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
164 TransferDataFromWindow();
166 // There are some interactions between the global setup data
167 // and the standard print dialog. The global printing 'mode'
168 // is determined by whether the user checks Print to file
170 if (printData
.GetPrintToFile())
172 wxThePrintSetupData
->SetPrinterMode(PS_FILE
);
174 char *f
= wxFileSelector(_("PostScript file"),
175 wxPathOnly(wxThePrintSetupData
->GetPrinterFile()),
176 wxFileNameFromPath(wxThePrintSetupData
->GetPrinterFile()),
177 "ps", "*.ps", 0, this);
179 wxThePrintSetupData
->SetPrinterFile(f
);
184 wxThePrintSetupData
->SetPrinterMode(PS_PRINTER
);
189 void wxGenericPrintDialog::OnRange(wxCommandEvent
& event
)
191 if (!fromText
) return;
193 if (event
.GetInt() == 1)
195 fromText
->Enable(FALSE
);
196 toText
->Enable(FALSE
);
198 else if (event
.GetInt() == 0)
200 fromText
->Enable(TRUE
);
201 toText
->Enable(TRUE
);
205 void wxGenericPrintDialog::OnSetup(wxCommandEvent
& WXUNUSED(event
))
207 wxGenericPrintSetupDialog
*genericPrintSetupDialog
=
208 new wxGenericPrintSetupDialog(this, wxThePrintSetupData
);
209 int ret
= genericPrintSetupDialog
->ShowModal();
210 if ( ret
!= wxID_CANCEL
)
212 *wxThePrintSetupData
= genericPrintSetupDialog
->printData
;
213 printData
.SetOrientation(wxThePrintSetupData
->GetPrinterOrientation());
216 genericPrintSetupDialog
->Close(TRUE
);
219 bool wxGenericPrintDialog::TransferDataToWindow(void)
222 if (printData
.GetEnablePageNumbers())
224 fromText
->Enable(TRUE
);
225 toText
->Enable(TRUE
);
227 sprintf(buf
, "%d", printData
.GetFromPage());
228 fromText
->SetValue(buf
);
229 sprintf(buf
, "%d", printData
.GetToPage());
230 toText
->SetValue(buf
);
232 if (printData
.GetAllPages())
233 rangeRadioBox
->SetSelection(0);
235 rangeRadioBox
->SetSelection(1);
239 fromText
->Enable(FALSE
);
240 toText
->Enable(FALSE
);
241 rangeRadioBox
->SetSelection(0);
242 rangeRadioBox
->wxRadioBox::Enable(1, FALSE
);
244 sprintf(buf
, "%d", printData
.GetNoCopies());
245 noCopiesText
->SetValue(buf
);
247 printToFileCheckBox
->SetValue(printData
.GetPrintToFile());
248 printToFileCheckBox
->Enable(printData
.GetEnablePrintToFile());
252 bool wxGenericPrintDialog::TransferDataFromWindow(void)
254 if (printData
.GetEnablePageNumbers())
256 printData
.SetFromPage(atoi(fromText
->GetValue()));
257 printData
.SetToPage(atoi(toText
->GetValue()));
259 if (rangeRadioBox
->GetSelection() == 0)
260 printData
.SetAllPages(TRUE
);
262 printData
.SetAllPages(FALSE
);
263 printData
.SetNoCopies(atoi(noCopiesText
->GetValue()));
264 printData
.SetPrintToFile(printToFileCheckBox
->GetValue());
269 wxDC
*wxGenericPrintDialog::GetPrintDC(void)
271 return new wxPostScriptDC(wxThePrintSetupData
->GetPrinterFile(), FALSE
, NULL
);
275 * Generic print setup dialog
278 wxGenericPrintSetupDialog::wxGenericPrintSetupDialog(wxWindow
*parent
, wxPrintSetupData
* data
):
279 wxDialog(parent
, -1, _("Print Setup"), wxPoint(0, 0), wxSize(600, 600), wxDEFAULT_DIALOG_STYLE
|wxDIALOG_MODAL
)
284 int buttonWidth
= 65;
285 int buttonHeight
= 25;
290 wxButton
*okButton
= new wxButton(this, wxID_OK
, _("OK"), wxPoint(xPos
, yPos
), wxSize(buttonWidth
, buttonHeight
));
291 xPos
+= buttonWidth
+ spacing
;
292 (void) new wxButton(this, wxID_CANCEL
, _("Cancel"), wxPoint(xPos
, yPos
), wxSize(buttonWidth
, buttonHeight
));
294 okButton
->SetDefault();
295 okButton
->SetFocus();
300 paperTypeChoice
= CreatePaperTypeChoice(&xPos
, &yPos
);
303 choices
[0] = _("Portrait");
304 choices
[1] = _("Landscape");
306 orientationRadioBox
= new wxRadioBox(this, wxPRINTID_ORIENTATION
, _("Orientation"),
307 wxPoint(xPos
, yPos
), wxSize(-1, -1), 2, choices
, 2);
308 orientationRadioBox
->SetSelection(0);
312 colourCheckBox
= new wxCheckBox(this, wxPRINTID_PRINTCOLOUR
, _("Print in colour"), wxPoint(xPos
, yPos
));
317 int staticWidth
= 100;
321 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Printer command:"), wxPoint(xPos
, yPos
));
324 printerCommandText
= new wxTextCtrl(this, wxPRINTID_COMMAND
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
325 xPos
+= textWidth
+ spacing
;
327 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Printer options:"), wxPoint(xPos
, yPos
));
330 printerOptionsText
= new wxTextCtrl(this, wxPRINTID_OPTIONS
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
338 wxGenericPrintSetupDialog::~wxGenericPrintSetupDialog(void)
342 bool wxGenericPrintSetupDialog::TransferDataToWindow(void)
344 if (printerCommandText
&& printData
.GetPrinterCommand())
345 printerCommandText
->SetValue(printData
.GetPrinterCommand());
346 if (printerOptionsText
&& printData
.GetPrinterOptions())
347 printerOptionsText
->SetValue(printData
.GetPrinterOptions());
349 colourCheckBox
->SetValue(printData
.GetColour());
351 if (orientationRadioBox
)
353 if (printData
.GetPrinterOrientation() == PS_PORTRAIT
)
354 orientationRadioBox
->SetSelection(0);
356 orientationRadioBox
->SetSelection(1);
361 bool wxGenericPrintSetupDialog::TransferDataFromWindow(void)
363 if (printerCommandText
)
364 printData
.SetPrinterCommand(WXSTRINGCAST printerCommandText
->GetValue());
365 if (printerOptionsText
)
366 printData
.SetPrinterOptions(WXSTRINGCAST printerOptionsText
->GetValue());
368 printData
.SetColour(colourCheckBox
->GetValue());
369 if (orientationRadioBox
)
371 int sel
= orientationRadioBox
->GetSelection();
373 printData
.SetPrinterOrientation(PS_PORTRAIT
);
375 printData
.SetPrinterOrientation(PS_LANDSCAPE
);
379 wxString
val(paperTypeChoice
->GetStringSelection());
380 if (!val
.IsNull() && val
!= "")
381 printData
.SetPaperName((char *)(const char *)val
);
386 wxChoice
*wxGenericPrintSetupDialog::CreatePaperTypeChoice(int *x
, int *y
)
388 if (!wxThePrintPaperDatabase
)
390 wxThePrintPaperDatabase
= new wxPrintPaperDatabase
;
391 wxThePrintPaperDatabase
->CreateDatabase();
393 int n
= wxThePrintPaperDatabase
->Number();
394 wxString
*choices
= new wxString
[n
];
397 for (i
= 0; i
< n
; i
++)
399 wxPrintPaperType
*paper
= (wxPrintPaperType
*)wxThePrintPaperDatabase
->Nth(i
)->Data();
400 choices
[i
] = paper
->pageName
;
401 if (printData
.GetPaperName() && choices
[i
] == printData
.GetPaperName())
405 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Paper size"), wxPoint(*x
, *y
));
408 wxChoice
*choice
= new wxChoice(this, wxPRINTID_PAPERSIZE
, wxPoint(*x
, *y
), wxSize(300, -1), n
,
413 choice
->SetSelection(sel
);
418 * Generic page setup dialog
421 void wxGenericPageSetupDialog::OnPrinter(wxCommandEvent
& WXUNUSED(event
))
423 if (wxTheApp
->GetPrintMode() == wxPRINT_POSTSCRIPT
)
425 wxGenericPrintSetupDialog
*genericPrintSetupDialog
=
426 new wxGenericPrintSetupDialog(this, wxThePrintSetupData
);
427 int ret
= genericPrintSetupDialog
->ShowModal();
429 *wxThePrintSetupData
= genericPrintSetupDialog
->GetPrintData();
431 genericPrintSetupDialog
->Close(TRUE
);
437 data
.SetSetupDialog(TRUE
);
438 wxPrintDialog
printDialog(this, & data
);
439 printDialog
.Show(TRUE
);
444 wxGenericPageSetupDialog::wxGenericPageSetupDialog(wxWindow
*parent
, wxPageSetupData
* data
):
445 wxDialog(parent
, -1, _("Page Setup"), wxPoint(0, 0), wxSize(600, 600), wxDIALOG_MODAL
|wxDEFAULT_DIALOG_STYLE
)
450 int buttonWidth
= 75;
451 int buttonHeight
= 25;
456 wxButton
*okButton
= new wxButton(this, wxID_OK
, _("OK"), wxPoint(5, yPos
), wxSize(buttonWidth
, buttonHeight
));
457 (void) new wxButton(this, wxID_CANCEL
, _("Cancel"), wxPoint(buttonWidth
+ 5 + spacing
, yPos
), wxSize(buttonWidth
, buttonHeight
));
459 printerButton
= new wxButton(this, wxPRINTID_SETUP
, _("Printer..."), wxPoint(buttonWidth
*2 + 5 + 2*spacing
, yPos
), wxSize(buttonWidth
, buttonHeight
));
461 if ( !pageData
.GetEnablePrinter() )
462 printerButton
->Enable(FALSE
);
464 // if (printData.GetEnableHelp())
465 // wxButton *helpButton = new wxButton(this, (wxFunction)wxGenericPageSetupHelpProc, _("Help"), -1, -1, buttonWidth, buttonHeight);
467 okButton
->SetDefault();
468 okButton
->SetFocus();
473 paperTypeChoice
= CreatePaperTypeChoice(&xPos
, &yPos
);
478 choices
[0] = _("Portrait");
479 choices
[1] = _("Landscape");
480 orientationRadioBox
= new wxRadioBox(this, wxPRINTID_ORIENTATION
, _("Orientation"),
481 wxPoint(xPos
, yPos
), wxSize(-1, -1), 2, choices
, 2);
482 orientationRadioBox
->SetSelection(0);
487 int staticWidth
= 110;
491 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Left margin (mm):"), wxPoint(xPos
, yPos
));
494 marginLeftText
= new wxTextCtrl(this, wxPRINTID_LEFTMARGIN
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
495 xPos
+= textWidth
+ spacing
;
497 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Right margin (mm):"), wxPoint(xPos
, yPos
));
500 marginRightText
= new wxTextCtrl(this, wxPRINTID_RIGHTMARGIN
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
501 xPos
+= textWidth
+ spacing
;
506 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Top margin (mm):"), wxPoint(xPos
, yPos
));
509 marginTopText
= new wxTextCtrl(this, wxPRINTID_TOPMARGIN
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
510 xPos
+= textWidth
+ spacing
;
512 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Bottom margin (mm):"), wxPoint(xPos
, yPos
));
515 marginBottomText
= new wxTextCtrl(this, wxPRINTID_BOTTOMMARGIN
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
523 wxGenericPageSetupDialog::~wxGenericPageSetupDialog(void)
527 bool wxGenericPageSetupDialog::TransferDataToWindow(void)
530 marginLeftText
->SetValue(IntToString((int) pageData
.GetMarginTopLeft().x
));
532 marginTopText
->SetValue(IntToString((int) pageData
.GetMarginTopLeft().y
));
534 marginRightText
->SetValue(IntToString((int) pageData
.GetMarginBottomRight().x
));
535 if (marginBottomText
)
536 marginBottomText
->SetValue(IntToString((int) pageData
.GetMarginBottomRight().y
));
538 if (orientationRadioBox
)
540 if (pageData
.GetOrientation() == wxPORTRAIT
)
541 orientationRadioBox
->SetSelection(0);
543 orientationRadioBox
->SetSelection(1);
548 bool wxGenericPageSetupDialog::TransferDataFromWindow(void)
550 if (marginLeftText
&& marginTopText
)
551 pageData
.SetMarginTopLeft(wxPoint(atoi((const char *)marginLeftText
->GetValue()),atoi((const char *)marginTopText
->GetValue())));
552 if (marginRightText
&& marginBottomText
)
553 pageData
.SetMarginBottomRight(wxPoint(atoi((const char *)marginRightText
->GetValue()),atoi((const char *)marginBottomText
->GetValue())));
555 if (orientationRadioBox
)
557 int sel
= orientationRadioBox
->GetSelection();
560 wxThePrintSetupData
->SetPrinterOrientation(wxPORTRAIT
);
561 pageData
.SetOrientation(wxPORTRAIT
);
565 wxThePrintSetupData
->SetPrinterOrientation(wxLANDSCAPE
);
566 pageData
.SetOrientation(wxLANDSCAPE
);
571 wxString
val(paperTypeChoice
->GetStringSelection());
572 if (!val
.IsNull() && val
!= "")
574 wxPrintPaperType
* paper
= wxThePrintPaperDatabase
->FindPaperType((char*) (const char *)val
);
577 pageData
.SetPaperSize(wxPoint(paper
->widthMM
, paper
->heightMM
));
584 wxChoice
*wxGenericPageSetupDialog::CreatePaperTypeChoice(int *x
, int *y
)
586 if (!wxThePrintPaperDatabase
)
588 wxThePrintPaperDatabase
= new wxPrintPaperDatabase
;
589 wxThePrintPaperDatabase
->CreateDatabase();
591 int n
= wxThePrintPaperDatabase
->Number();
592 wxString
*choices
= new wxString
[n
];
595 for (i
= 0; i
< n
; i
++)
597 wxPrintPaperType
*paper
= (wxPrintPaperType
*)wxThePrintPaperDatabase
->Nth(i
)->Data();
598 choices
[i
] = paper
->pageName
;
599 if (pageData
.GetPaperSize().x
== paper
->widthMM
&& pageData
.GetPaperSize().y
== paper
->heightMM
)
603 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Paper size"), wxPoint(*x
, *y
));
606 wxChoice
*choice
= new wxChoice(this, wxPRINTID_PAPERSIZE
, wxPoint(*x
, *y
), wxSize(300, -1), n
,
611 choice
->SetSelection(sel
);