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();
93 wxString
*choices
= new wxString
[2];
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
145 int wxGenericPrintDialog::ShowModal(void)
147 if ( printData
.GetSetupDialog() )
149 wxGenericPrintSetupDialog
*genericPrintSetupDialog
=
150 new wxGenericPrintSetupDialog(GetParent(), wxThePrintSetupData
);
151 int ret
= genericPrintSetupDialog
->ShowModal();
152 if ( ret
!= wxID_CANCEL
)
154 *wxThePrintSetupData
= genericPrintSetupDialog
->printData
;
156 genericPrintSetupDialog
->Close(TRUE
);
161 return wxDialog::ShowModal();
165 wxGenericPrintDialog::~wxGenericPrintDialog(void)
169 void wxGenericPrintDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
171 TransferDataFromWindow();
173 // There are some interactions between the global setup data
174 // and the standard print dialog. The global printing 'mode'
175 // is determined by whether the user checks Print to file
177 if (printData
.GetPrintToFile())
179 wxThePrintSetupData
->SetPrinterMode(PS_FILE
);
181 char *f
= wxFileSelector(_("PostScript file"),
182 wxPathOnly(wxThePrintSetupData
->GetPrinterFile()),
183 wxFileNameFromPath(wxThePrintSetupData
->GetPrinterFile()),
184 "ps", "*.ps", 0, this);
186 wxThePrintSetupData
->SetPrinterFile(f
);
191 wxThePrintSetupData
->SetPrinterMode(PS_PRINTER
);
196 void wxGenericPrintDialog::OnRange(wxCommandEvent
& event
)
198 if (!fromText
) return;
200 if (event
.GetInt() == 1)
202 fromText
->Enable(FALSE
);
203 toText
->Enable(FALSE
);
205 else if (event
.GetInt() == 0)
207 fromText
->Enable(TRUE
);
208 toText
->Enable(TRUE
);
212 void wxGenericPrintDialog::OnSetup(wxCommandEvent
& WXUNUSED(event
))
214 wxGenericPrintSetupDialog
*genericPrintSetupDialog
=
215 new wxGenericPrintSetupDialog(this, wxThePrintSetupData
);
216 int ret
= genericPrintSetupDialog
->ShowModal();
217 if ( ret
!= wxID_CANCEL
)
219 *wxThePrintSetupData
= genericPrintSetupDialog
->printData
;
220 printData
.SetOrientation(wxThePrintSetupData
->GetPrinterOrientation());
223 genericPrintSetupDialog
->Close(TRUE
);
226 bool wxGenericPrintDialog::TransferDataToWindow(void)
230 if(printData
.GetFromPage() != 0)
232 if (printData
.GetEnablePageNumbers())
234 fromText
->Enable(TRUE
);
235 toText
->Enable(TRUE
);
237 sprintf(buf
, "%d", printData
.GetFromPage());
238 fromText
->SetValue(buf
);
239 sprintf(buf
, "%d", printData
.GetToPage());
240 toText
->SetValue(buf
);
242 if (printData
.GetAllPages())
243 rangeRadioBox
->SetSelection(0);
245 rangeRadioBox
->SetSelection(1);
249 fromText
->Enable(FALSE
);
250 toText
->Enable(FALSE
);
251 rangeRadioBox
->SetSelection(0);
252 rangeRadioBox
->wxRadioBox::Enable(1, FALSE
);
255 sprintf(buf
, "%d", printData
.GetNoCopies());
256 noCopiesText
->SetValue(buf
);
258 printToFileCheckBox
->SetValue(printData
.GetPrintToFile());
259 printToFileCheckBox
->Enable(printData
.GetEnablePrintToFile());
263 bool wxGenericPrintDialog::TransferDataFromWindow(void)
265 if(printData
.GetFromPage() != -1)
267 if (printData
.GetEnablePageNumbers())
269 printData
.SetFromPage(atoi(fromText
->GetValue()));
270 printData
.SetToPage(atoi(toText
->GetValue()));
272 if (rangeRadioBox
->GetSelection() == 0)
273 printData
.SetAllPages(TRUE
);
275 printData
.SetAllPages(FALSE
);
278 { // continuous printing
279 printData
.SetFromPage(1);
280 printData
.SetToPage(32000);
282 printData
.SetNoCopies(atoi(noCopiesText
->GetValue()));
283 printData
.SetPrintToFile(printToFileCheckBox
->GetValue());
288 wxDC
*wxGenericPrintDialog::GetPrintDC(void)
290 return new wxPostScriptDC(wxThePrintSetupData
->GetPrinterFile(), FALSE
, (wxWindow
*) NULL
);
294 * Generic print setup dialog
297 wxGenericPrintSetupDialog::wxGenericPrintSetupDialog(wxWindow
*parent
, wxPrintSetupData
* data
):
298 wxDialog(parent
, -1, _("Print Setup"), wxPoint(0, 0), wxSize(600, 600), wxDEFAULT_DIALOG_STYLE
|wxDIALOG_MODAL
)
303 int buttonWidth
= 65;
304 int buttonHeight
= 25;
309 wxButton
*okButton
= new wxButton(this, wxID_OK
, _("OK"), wxPoint(xPos
, yPos
), wxSize(buttonWidth
, buttonHeight
));
310 xPos
+= buttonWidth
+ spacing
;
311 (void) new wxButton(this, wxID_CANCEL
, _("Cancel"), wxPoint(xPos
, yPos
), wxSize(buttonWidth
, buttonHeight
));
313 okButton
->SetDefault();
314 okButton
->SetFocus();
319 paperTypeChoice
= CreatePaperTypeChoice(&xPos
, &yPos
);
321 wxString
*choices
= new wxString
[2];
322 choices
[0] = _("Portrait");
323 choices
[1] = _("Landscape");
325 orientationRadioBox
= new wxRadioBox(this, wxPRINTID_ORIENTATION
, _("Orientation"),
326 wxPoint(xPos
, yPos
), wxSize(-1, -1), 2, choices
, 2);
327 orientationRadioBox
->SetSelection(0);
331 colourCheckBox
= new wxCheckBox(this, wxPRINTID_PRINTCOLOUR
, _("Print in colour"), wxPoint(xPos
, yPos
));
336 int staticWidth
= 100;
340 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Printer command:"), wxPoint(xPos
, yPos
));
343 printerCommandText
= new wxTextCtrl(this, wxPRINTID_COMMAND
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
344 xPos
+= textWidth
+ spacing
;
346 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Printer options:"), wxPoint(xPos
, yPos
));
349 printerOptionsText
= new wxTextCtrl(this, wxPRINTID_OPTIONS
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
358 wxGenericPrintSetupDialog::~wxGenericPrintSetupDialog(void)
362 bool wxGenericPrintSetupDialog::TransferDataToWindow(void)
364 if (printerCommandText
&& printData
.GetPrinterCommand())
365 printerCommandText
->SetValue(printData
.GetPrinterCommand());
366 if (printerOptionsText
&& printData
.GetPrinterOptions())
367 printerOptionsText
->SetValue(printData
.GetPrinterOptions());
369 colourCheckBox
->SetValue(printData
.GetColour());
371 if (orientationRadioBox
)
373 if (printData
.GetPrinterOrientation() == PS_PORTRAIT
)
374 orientationRadioBox
->SetSelection(0);
376 orientationRadioBox
->SetSelection(1);
381 bool wxGenericPrintSetupDialog::TransferDataFromWindow(void)
383 if (printerCommandText
)
384 printData
.SetPrinterCommand(WXSTRINGCAST printerCommandText
->GetValue());
385 if (printerOptionsText
)
386 printData
.SetPrinterOptions(WXSTRINGCAST printerOptionsText
->GetValue());
388 printData
.SetColour(colourCheckBox
->GetValue());
389 if (orientationRadioBox
)
391 int sel
= orientationRadioBox
->GetSelection();
393 printData
.SetPrinterOrientation(PS_PORTRAIT
);
395 printData
.SetPrinterOrientation(PS_LANDSCAPE
);
399 wxString
val(paperTypeChoice
->GetStringSelection());
400 if (!val
.IsNull() && val
!= "")
401 printData
.SetPaperName((char *)(const char *)val
);
406 wxChoice
*wxGenericPrintSetupDialog::CreatePaperTypeChoice(int *x
, int *y
)
408 if (!wxThePrintPaperDatabase
)
410 wxThePrintPaperDatabase
= new wxPrintPaperDatabase
;
411 wxThePrintPaperDatabase
->CreateDatabase();
413 int n
= wxThePrintPaperDatabase
->Number();
414 wxString
*choices
= new wxString
[n
];
417 for (i
= 0; i
< n
; i
++)
419 wxPrintPaperType
*paper
= (wxPrintPaperType
*)wxThePrintPaperDatabase
->Nth(i
)->Data();
420 choices
[i
] = paper
->pageName
;
421 if (printData
.GetPaperName() && choices
[i
] == printData
.GetPaperName())
425 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Paper size"), wxPoint(*x
, *y
));
428 wxChoice
*choice
= new wxChoice(this, wxPRINTID_PAPERSIZE
, wxPoint(*x
, *y
), wxSize(300, -1), n
,
433 choice
->SetSelection(sel
);
438 * Generic page setup dialog
441 void wxGenericPageSetupDialog::OnPrinter(wxCommandEvent
& WXUNUSED(event
))
443 if (wxTheApp
->GetPrintMode() == wxPRINT_POSTSCRIPT
)
445 wxGenericPrintSetupDialog
*genericPrintSetupDialog
=
446 new wxGenericPrintSetupDialog(this, wxThePrintSetupData
);
447 int ret
= genericPrintSetupDialog
->ShowModal();
449 *wxThePrintSetupData
= genericPrintSetupDialog
->GetPrintData();
451 genericPrintSetupDialog
->Close(TRUE
);
457 data
.SetSetupDialog(TRUE
);
458 wxPrintDialog
printDialog(this, & data
);
459 printDialog
.Show(TRUE
);
464 wxGenericPageSetupDialog::wxGenericPageSetupDialog(wxWindow
*parent
, wxPageSetupData
* data
):
465 wxDialog(parent
, -1, _("Page Setup"), wxPoint(0, 0), wxSize(600, 600), wxDIALOG_MODAL
|wxDEFAULT_DIALOG_STYLE
)
470 int buttonWidth
= 75;
471 int buttonHeight
= 25;
476 wxButton
*okButton
= new wxButton(this, wxID_OK
, _("OK"), wxPoint(5, yPos
), wxSize(buttonWidth
, buttonHeight
));
477 (void) new wxButton(this, wxID_CANCEL
, _("Cancel"), wxPoint(buttonWidth
+ 5 + spacing
, yPos
), wxSize(buttonWidth
, buttonHeight
));
479 printerButton
= new wxButton(this, wxPRINTID_SETUP
, _("Printer..."), wxPoint(buttonWidth
*2 + 5 + 2*spacing
, yPos
), wxSize(buttonWidth
, buttonHeight
));
481 if ( !pageData
.GetEnablePrinter() )
482 printerButton
->Enable(FALSE
);
484 // if (printData.GetEnableHelp())
485 // wxButton *helpButton = new wxButton(this, (wxFunction)wxGenericPageSetupHelpProc, _("Help"), -1, -1, buttonWidth, buttonHeight);
487 okButton
->SetDefault();
488 okButton
->SetFocus();
493 paperTypeChoice
= CreatePaperTypeChoice(&xPos
, &yPos
);
497 wxString
*choices
= new wxString
[2];
498 choices
[0] = _("Portrait");
499 choices
[1] = _("Landscape");
500 orientationRadioBox
= new wxRadioBox(this, wxPRINTID_ORIENTATION
, _("Orientation"),
501 wxPoint(xPos
, yPos
), wxSize(-1, -1), 2, choices
, 2);
502 orientationRadioBox
->SetSelection(0);
507 int staticWidth
= 110;
511 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Left margin (mm):"), wxPoint(xPos
, yPos
));
514 marginLeftText
= new wxTextCtrl(this, wxPRINTID_LEFTMARGIN
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
515 xPos
+= textWidth
+ spacing
;
517 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Right margin (mm):"), wxPoint(xPos
, yPos
));
520 marginRightText
= new wxTextCtrl(this, wxPRINTID_RIGHTMARGIN
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
521 xPos
+= textWidth
+ spacing
;
526 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Top margin (mm):"), wxPoint(xPos
, yPos
));
529 marginTopText
= new wxTextCtrl(this, wxPRINTID_TOPMARGIN
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
530 xPos
+= textWidth
+ spacing
;
532 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Bottom margin (mm):"), wxPoint(xPos
, yPos
));
535 marginBottomText
= new wxTextCtrl(this, wxPRINTID_BOTTOMMARGIN
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
544 wxGenericPageSetupDialog::~wxGenericPageSetupDialog(void)
548 bool wxGenericPageSetupDialog::TransferDataToWindow(void)
551 marginLeftText
->SetValue(IntToString((int) pageData
.GetMarginTopLeft().x
));
553 marginTopText
->SetValue(IntToString((int) pageData
.GetMarginTopLeft().y
));
555 marginRightText
->SetValue(IntToString((int) pageData
.GetMarginBottomRight().x
));
556 if (marginBottomText
)
557 marginBottomText
->SetValue(IntToString((int) pageData
.GetMarginBottomRight().y
));
559 if (orientationRadioBox
)
561 if (pageData
.GetOrientation() == wxPORTRAIT
)
562 orientationRadioBox
->SetSelection(0);
564 orientationRadioBox
->SetSelection(1);
569 bool wxGenericPageSetupDialog::TransferDataFromWindow(void)
571 if (marginLeftText
&& marginTopText
)
572 pageData
.SetMarginTopLeft(wxPoint(atoi((const char *)marginLeftText
->GetValue()),atoi((const char *)marginTopText
->GetValue())));
573 if (marginRightText
&& marginBottomText
)
574 pageData
.SetMarginBottomRight(wxPoint(atoi((const char *)marginRightText
->GetValue()),atoi((const char *)marginBottomText
->GetValue())));
576 if (orientationRadioBox
)
578 int sel
= orientationRadioBox
->GetSelection();
581 wxThePrintSetupData
->SetPrinterOrientation(wxPORTRAIT
);
582 pageData
.SetOrientation(wxPORTRAIT
);
586 wxThePrintSetupData
->SetPrinterOrientation(wxLANDSCAPE
);
587 pageData
.SetOrientation(wxLANDSCAPE
);
592 wxString
val(paperTypeChoice
->GetStringSelection());
593 if (!val
.IsNull() && val
!= "")
595 wxPrintPaperType
* paper
= wxThePrintPaperDatabase
->FindPaperType((char*) (const char *)val
);
598 pageData
.SetPaperSize(wxPoint(paper
->widthMM
, paper
->heightMM
));
605 wxChoice
*wxGenericPageSetupDialog::CreatePaperTypeChoice(int *x
, int *y
)
607 if (!wxThePrintPaperDatabase
)
609 wxThePrintPaperDatabase
= new wxPrintPaperDatabase
;
610 wxThePrintPaperDatabase
->CreateDatabase();
612 int n
= wxThePrintPaperDatabase
->Number();
613 wxString
*choices
= new wxString
[n
];
616 for (i
= 0; i
< n
; i
++)
618 wxPrintPaperType
*paper
= (wxPrintPaperType
*)wxThePrintPaperDatabase
->Nth(i
)->Data();
619 choices
[i
] = paper
->pageName
;
620 if (pageData
.GetPaperSize().x
== paper
->widthMM
&& pageData
.GetPaperSize().y
== paper
->heightMM
)
624 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Paper size"), wxPoint(*x
, *y
));
627 wxChoice
*choice
= new wxChoice(this, wxPRINTID_PAPERSIZE
, wxPoint(*x
, *y
), wxSize(300, -1), n
,
632 choice
->SetSelection(sel
);