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 if(printData
.GetFromPage() != 0)
101 rangeRadioBox
= new wxRadioBox(this, wxPRINTID_RANGE
, _("Print Range"),
102 wxPoint(5, yPos
), wxSize(-1, -1), 2, choices
, 2);
103 rangeRadioBox
->SetSelection(1);
108 int staticWidth
= 45;
112 if(printData
.GetFromPage() != 0)
114 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("From:"), wxPoint(xPos
, yPos
));
117 fromText
= new wxTextCtrl(this, wxPRINTID_FROM
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
118 xPos
+= spacing
+ textWidth
;
120 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("To:"), wxPoint(xPos
, yPos
));
123 toText
= new wxTextCtrl(this, wxPRINTID_TO
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
124 xPos
+= spacing
+ textWidth
;
127 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Copies:"), wxPoint(xPos
, yPos
));
128 xPos
+= spacing
+ staticWidth
;
130 noCopiesText
= new wxTextCtrl(this, wxPRINTID_COPIES
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
135 printToFileCheckBox
= new wxCheckBox(this, wxPRINTID_PRINTTOFILE
, _("Print to File"), wxPoint(xPos
, yPos
));
140 // Calls wxWindow::OnInitDialog and then wxGenericPrintDialog::TransferDataToWindow
144 int wxGenericPrintDialog::ShowModal(void)
146 if ( printData
.GetSetupDialog() )
148 wxGenericPrintSetupDialog
*genericPrintSetupDialog
=
149 new wxGenericPrintSetupDialog(GetParent(), wxThePrintSetupData
);
150 int ret
= genericPrintSetupDialog
->ShowModal();
151 if ( ret
!= wxID_CANCEL
)
153 *wxThePrintSetupData
= genericPrintSetupDialog
->printData
;
155 genericPrintSetupDialog
->Close(TRUE
);
160 return wxDialog::ShowModal();
164 wxGenericPrintDialog::~wxGenericPrintDialog(void)
168 void wxGenericPrintDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
170 TransferDataFromWindow();
172 // There are some interactions between the global setup data
173 // and the standard print dialog. The global printing 'mode'
174 // is determined by whether the user checks Print to file
176 if (printData
.GetPrintToFile())
178 wxThePrintSetupData
->SetPrinterMode(PS_FILE
);
180 char *f
= wxFileSelector(_("PostScript file"),
181 wxPathOnly(wxThePrintSetupData
->GetPrinterFile()),
182 wxFileNameFromPath(wxThePrintSetupData
->GetPrinterFile()),
183 "ps", "*.ps", 0, this);
185 wxThePrintSetupData
->SetPrinterFile(f
);
190 wxThePrintSetupData
->SetPrinterMode(PS_PRINTER
);
195 void wxGenericPrintDialog::OnRange(wxCommandEvent
& event
)
197 if (!fromText
) return;
199 if (event
.GetInt() == 1)
201 fromText
->Enable(FALSE
);
202 toText
->Enable(FALSE
);
204 else if (event
.GetInt() == 0)
206 fromText
->Enable(TRUE
);
207 toText
->Enable(TRUE
);
211 void wxGenericPrintDialog::OnSetup(wxCommandEvent
& WXUNUSED(event
))
213 wxGenericPrintSetupDialog
*genericPrintSetupDialog
=
214 new wxGenericPrintSetupDialog(this, wxThePrintSetupData
);
215 int ret
= genericPrintSetupDialog
->ShowModal();
216 if ( ret
!= wxID_CANCEL
)
218 *wxThePrintSetupData
= genericPrintSetupDialog
->printData
;
219 printData
.SetOrientation(wxThePrintSetupData
->GetPrinterOrientation());
222 genericPrintSetupDialog
->Close(TRUE
);
225 bool wxGenericPrintDialog::TransferDataToWindow(void)
229 if(printData
.GetFromPage() != 0)
231 if (printData
.GetEnablePageNumbers())
233 fromText
->Enable(TRUE
);
234 toText
->Enable(TRUE
);
236 sprintf(buf
, "%d", printData
.GetFromPage());
237 fromText
->SetValue(buf
);
238 sprintf(buf
, "%d", printData
.GetFromPage());
239 toText
->SetValue(buf
);
241 if (printData
.GetAllPages())
242 rangeRadioBox
->SetSelection(0);
244 rangeRadioBox
->SetSelection(1);
248 fromText
->Enable(FALSE
);
249 toText
->Enable(FALSE
);
250 rangeRadioBox
->SetSelection(0);
251 rangeRadioBox
->wxRadioBox::Enable(1, FALSE
);
254 sprintf(buf
, "%d", printData
.GetNoCopies());
255 noCopiesText
->SetValue(buf
);
257 printToFileCheckBox
->SetValue(printData
.GetPrintToFile());
258 printToFileCheckBox
->Enable(printData
.GetEnablePrintToFile());
262 bool wxGenericPrintDialog::TransferDataFromWindow(void)
264 if(printData
.GetFromPage() != -1)
266 if (printData
.GetEnablePageNumbers())
268 printData
.SetFromPage(atoi(fromText
->GetValue()));
269 printData
.SetToPage(atoi(toText
->GetValue()));
271 if (rangeRadioBox
->GetSelection() == 0)
272 printData
.SetAllPages(TRUE
);
274 printData
.SetAllPages(FALSE
);
277 { // continuous printing
278 printData
.SetFromPage(1);
279 printData
.SetToPage(32000);
281 printData
.SetNoCopies(atoi(noCopiesText
->GetValue()));
282 printData
.SetPrintToFile(printToFileCheckBox
->GetValue());
287 wxDC
*wxGenericPrintDialog::GetPrintDC(void)
289 return new wxPostScriptDC(wxThePrintSetupData
->GetPrinterFile(), FALSE
, NULL
);
293 * Generic print setup dialog
296 wxGenericPrintSetupDialog::wxGenericPrintSetupDialog(wxWindow
*parent
, wxPrintSetupData
* data
):
297 wxDialog(parent
, -1, _("Print Setup"), wxPoint(0, 0), wxSize(600, 600), wxDEFAULT_DIALOG_STYLE
|wxDIALOG_MODAL
)
302 int buttonWidth
= 65;
303 int buttonHeight
= 25;
308 wxButton
*okButton
= new wxButton(this, wxID_OK
, _("OK"), wxPoint(xPos
, yPos
), wxSize(buttonWidth
, buttonHeight
));
309 xPos
+= buttonWidth
+ spacing
;
310 (void) new wxButton(this, wxID_CANCEL
, _("Cancel"), wxPoint(xPos
, yPos
), wxSize(buttonWidth
, buttonHeight
));
312 okButton
->SetDefault();
313 okButton
->SetFocus();
318 paperTypeChoice
= CreatePaperTypeChoice(&xPos
, &yPos
);
321 choices
[0] = _("Portrait");
322 choices
[1] = _("Landscape");
324 orientationRadioBox
= new wxRadioBox(this, wxPRINTID_ORIENTATION
, _("Orientation"),
325 wxPoint(xPos
, yPos
), wxSize(-1, -1), 2, choices
, 2);
326 orientationRadioBox
->SetSelection(0);
330 colourCheckBox
= new wxCheckBox(this, wxPRINTID_PRINTCOLOUR
, _("Print in colour"), wxPoint(xPos
, yPos
));
335 int staticWidth
= 100;
339 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Printer command:"), wxPoint(xPos
, yPos
));
342 printerCommandText
= new wxTextCtrl(this, wxPRINTID_COMMAND
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
343 xPos
+= textWidth
+ spacing
;
345 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Printer options:"), wxPoint(xPos
, yPos
));
348 printerOptionsText
= new wxTextCtrl(this, wxPRINTID_OPTIONS
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
356 wxGenericPrintSetupDialog::~wxGenericPrintSetupDialog(void)
360 bool wxGenericPrintSetupDialog::TransferDataToWindow(void)
362 if (printerCommandText
&& printData
.GetPrinterCommand())
363 printerCommandText
->SetValue(printData
.GetPrinterCommand());
364 if (printerOptionsText
&& printData
.GetPrinterOptions())
365 printerOptionsText
->SetValue(printData
.GetPrinterOptions());
367 colourCheckBox
->SetValue(printData
.GetColour());
369 if (orientationRadioBox
)
371 if (printData
.GetPrinterOrientation() == PS_PORTRAIT
)
372 orientationRadioBox
->SetSelection(0);
374 orientationRadioBox
->SetSelection(1);
379 bool wxGenericPrintSetupDialog::TransferDataFromWindow(void)
381 if (printerCommandText
)
382 printData
.SetPrinterCommand(WXSTRINGCAST printerCommandText
->GetValue());
383 if (printerOptionsText
)
384 printData
.SetPrinterOptions(WXSTRINGCAST printerOptionsText
->GetValue());
386 printData
.SetColour(colourCheckBox
->GetValue());
387 if (orientationRadioBox
)
389 int sel
= orientationRadioBox
->GetSelection();
391 printData
.SetPrinterOrientation(PS_PORTRAIT
);
393 printData
.SetPrinterOrientation(PS_LANDSCAPE
);
397 wxString
val(paperTypeChoice
->GetStringSelection());
398 if (!val
.IsNull() && val
!= "")
399 printData
.SetPaperName((char *)(const char *)val
);
404 wxChoice
*wxGenericPrintSetupDialog::CreatePaperTypeChoice(int *x
, int *y
)
406 if (!wxThePrintPaperDatabase
)
408 wxThePrintPaperDatabase
= new wxPrintPaperDatabase
;
409 wxThePrintPaperDatabase
->CreateDatabase();
411 int n
= wxThePrintPaperDatabase
->Number();
412 wxString
*choices
= new wxString
[n
];
415 for (i
= 0; i
< n
; i
++)
417 wxPrintPaperType
*paper
= (wxPrintPaperType
*)wxThePrintPaperDatabase
->Nth(i
)->Data();
418 choices
[i
] = paper
->pageName
;
419 if (printData
.GetPaperName() && choices
[i
] == printData
.GetPaperName())
423 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Paper size"), wxPoint(*x
, *y
));
426 wxChoice
*choice
= new wxChoice(this, wxPRINTID_PAPERSIZE
, wxPoint(*x
, *y
), wxSize(300, -1), n
,
431 choice
->SetSelection(sel
);
436 * Generic page setup dialog
439 void wxGenericPageSetupDialog::OnPrinter(wxCommandEvent
& WXUNUSED(event
))
441 if (wxTheApp
->GetPrintMode() == wxPRINT_POSTSCRIPT
)
443 wxGenericPrintSetupDialog
*genericPrintSetupDialog
=
444 new wxGenericPrintSetupDialog(this, wxThePrintSetupData
);
445 int ret
= genericPrintSetupDialog
->ShowModal();
447 *wxThePrintSetupData
= genericPrintSetupDialog
->GetPrintData();
449 genericPrintSetupDialog
->Close(TRUE
);
455 data
.SetSetupDialog(TRUE
);
456 wxPrintDialog
printDialog(this, & data
);
457 printDialog
.Show(TRUE
);
462 wxGenericPageSetupDialog::wxGenericPageSetupDialog(wxWindow
*parent
, wxPageSetupData
* data
):
463 wxDialog(parent
, -1, _("Page Setup"), wxPoint(0, 0), wxSize(600, 600), wxDIALOG_MODAL
|wxDEFAULT_DIALOG_STYLE
)
468 int buttonWidth
= 75;
469 int buttonHeight
= 25;
474 wxButton
*okButton
= new wxButton(this, wxID_OK
, _("OK"), wxPoint(5, yPos
), wxSize(buttonWidth
, buttonHeight
));
475 (void) new wxButton(this, wxID_CANCEL
, _("Cancel"), wxPoint(buttonWidth
+ 5 + spacing
, yPos
), wxSize(buttonWidth
, buttonHeight
));
477 printerButton
= new wxButton(this, wxPRINTID_SETUP
, _("Printer..."), wxPoint(buttonWidth
*2 + 5 + 2*spacing
, yPos
), wxSize(buttonWidth
, buttonHeight
));
479 if ( !pageData
.GetEnablePrinter() )
480 printerButton
->Enable(FALSE
);
482 // if (printData.GetEnableHelp())
483 // wxButton *helpButton = new wxButton(this, (wxFunction)wxGenericPageSetupHelpProc, _("Help"), -1, -1, buttonWidth, buttonHeight);
485 okButton
->SetDefault();
486 okButton
->SetFocus();
491 paperTypeChoice
= CreatePaperTypeChoice(&xPos
, &yPos
);
496 choices
[0] = _("Portrait");
497 choices
[1] = _("Landscape");
498 orientationRadioBox
= new wxRadioBox(this, wxPRINTID_ORIENTATION
, _("Orientation"),
499 wxPoint(xPos
, yPos
), wxSize(-1, -1), 2, choices
, 2);
500 orientationRadioBox
->SetSelection(0);
505 int staticWidth
= 110;
509 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Left margin (mm):"), wxPoint(xPos
, yPos
));
512 marginLeftText
= new wxTextCtrl(this, wxPRINTID_LEFTMARGIN
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
513 xPos
+= textWidth
+ spacing
;
515 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Right margin (mm):"), wxPoint(xPos
, yPos
));
518 marginRightText
= new wxTextCtrl(this, wxPRINTID_RIGHTMARGIN
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
519 xPos
+= textWidth
+ spacing
;
524 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Top margin (mm):"), wxPoint(xPos
, yPos
));
527 marginTopText
= new wxTextCtrl(this, wxPRINTID_TOPMARGIN
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
528 xPos
+= textWidth
+ spacing
;
530 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Bottom margin (mm):"), wxPoint(xPos
, yPos
));
533 marginBottomText
= new wxTextCtrl(this, wxPRINTID_BOTTOMMARGIN
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
541 wxGenericPageSetupDialog::~wxGenericPageSetupDialog(void)
545 bool wxGenericPageSetupDialog::TransferDataToWindow(void)
548 marginLeftText
->SetValue(IntToString((int) pageData
.GetMarginTopLeft().x
));
550 marginTopText
->SetValue(IntToString((int) pageData
.GetMarginTopLeft().y
));
552 marginRightText
->SetValue(IntToString((int) pageData
.GetMarginBottomRight().x
));
553 if (marginBottomText
)
554 marginBottomText
->SetValue(IntToString((int) pageData
.GetMarginBottomRight().y
));
556 if (orientationRadioBox
)
558 if (pageData
.GetOrientation() == wxPORTRAIT
)
559 orientationRadioBox
->SetSelection(0);
561 orientationRadioBox
->SetSelection(1);
566 bool wxGenericPageSetupDialog::TransferDataFromWindow(void)
568 if (marginLeftText
&& marginTopText
)
569 pageData
.SetMarginTopLeft(wxPoint(atoi((const char *)marginLeftText
->GetValue()),atoi((const char *)marginTopText
->GetValue())));
570 if (marginRightText
&& marginBottomText
)
571 pageData
.SetMarginBottomRight(wxPoint(atoi((const char *)marginRightText
->GetValue()),atoi((const char *)marginBottomText
->GetValue())));
573 if (orientationRadioBox
)
575 int sel
= orientationRadioBox
->GetSelection();
578 wxThePrintSetupData
->SetPrinterOrientation(wxPORTRAIT
);
579 pageData
.SetOrientation(wxPORTRAIT
);
583 wxThePrintSetupData
->SetPrinterOrientation(wxLANDSCAPE
);
584 pageData
.SetOrientation(wxLANDSCAPE
);
589 wxString
val(paperTypeChoice
->GetStringSelection());
590 if (!val
.IsNull() && val
!= "")
592 wxPrintPaperType
* paper
= wxThePrintPaperDatabase
->FindPaperType((char*) (const char *)val
);
595 pageData
.SetPaperSize(wxPoint(paper
->widthMM
, paper
->heightMM
));
602 wxChoice
*wxGenericPageSetupDialog::CreatePaperTypeChoice(int *x
, int *y
)
604 if (!wxThePrintPaperDatabase
)
606 wxThePrintPaperDatabase
= new wxPrintPaperDatabase
;
607 wxThePrintPaperDatabase
->CreateDatabase();
609 int n
= wxThePrintPaperDatabase
->Number();
610 wxString
*choices
= new wxString
[n
];
613 for (i
= 0; i
< n
; i
++)
615 wxPrintPaperType
*paper
= (wxPrintPaperType
*)wxThePrintPaperDatabase
->Nth(i
)->Data();
616 choices
[i
] = paper
->pageName
;
617 if (pageData
.GetPaperSize().x
== paper
->widthMM
&& pageData
.GetPaperSize().y
== paper
->heightMM
)
621 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Paper size"), wxPoint(*x
, *y
));
624 wxChoice
*choice
= new wxChoice(this, wxPRINTID_PAPERSIZE
, wxPoint(*x
, *y
), wxSize(300, -1), n
,
629 choice
->SetSelection(sel
);