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"
30 #include "wx/stattext.h"
31 #include "wx/statbox.h"
32 #include "wx/button.h"
33 #include "wx/checkbox.h"
34 #include "wx/textctrl.h"
35 #include "wx/radiobox.h"
36 #include "wx/filedlg.h"
37 #include "wx/choice.h"
41 #include "wx/generic/prntdlgg.h"
42 #include "wx/printdlg.h"
47 #if !USE_SHARED_LIBRARY
48 IMPLEMENT_CLASS(wxGenericPrintDialog
, wxDialog
)
49 IMPLEMENT_CLASS(wxGenericPrintSetupDialog
, wxDialog
)
50 IMPLEMENT_CLASS(wxGenericPageSetupDialog
, wxDialog
)
52 BEGIN_EVENT_TABLE(wxGenericPrintDialog
, wxDialog
)
53 EVT_BUTTON(wxID_OK
, wxGenericPrintDialog::OnOK
)
54 EVT_BUTTON(wxPRINTID_SETUP
, wxGenericPrintDialog::OnSetup
)
55 EVT_RADIOBOX(wxPRINTID_RANGE
, wxGenericPrintDialog::OnRange
)
58 BEGIN_EVENT_TABLE(wxGenericPageSetupDialog
, wxDialog
)
59 EVT_BUTTON(wxPRINTID_SETUP
, wxGenericPageSetupDialog::OnPrinter
)
63 extern wxPrintPaperDatabase
*wxThePrintPaperDatabase
;
66 * Generic print dialog for non-Windows printing use.
70 wxGenericPrintDialog::wxGenericPrintDialog(wxWindow
*parent
, wxPrintData
* data
):
71 wxDialog(parent
, -1, _("Print"), wxPoint(0, 0), wxSize(600, 600), wxDEFAULT_DIALOG_STYLE
|wxDIALOG_MODAL
)
76 (void)new wxStaticBox( this, -1, _( "Printer options" ), wxPoint( 5, 5), wxSize( 300, 60 ) );
78 printToFileCheckBox
= new wxCheckBox(this, wxPRINTID_PRINTTOFILE
, _("Print to File"), wxPoint(20, 25) );
80 setupButton
= new wxButton(this, wxPRINTID_SETUP
, _("Setup..."), wxPoint(160, 25), wxSize(100, -1));
82 wxString
*choices
= new wxString
[2];
83 choices
[0] = _("All");
84 choices
[1] = _("Pages");
86 fromText
= (wxTextCtrl
*)NULL
;
88 if(printData
.GetFromPage() != 0)
90 rangeRadioBox
= new wxRadioBox(this, wxPRINTID_RANGE
, _("Print Range"),
91 wxPoint(5, 80), wxSize(-1, -1), 2, choices
, 1, wxRA_VERTICAL
);
92 rangeRadioBox
->SetSelection(1);
95 if(printData
.GetFromPage() != 0)
97 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("From:"), wxPoint(5, 135));
99 fromText
= new wxTextCtrl(this, wxPRINTID_FROM
, "", wxPoint(45, 130), wxSize(40, -1));
101 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("To:"), wxPoint(100, 135));
103 toText
= new wxTextCtrl(this, wxPRINTID_TO
, "", wxPoint(133, 130), wxSize(40, -1));
106 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Copies:"), wxPoint(200, 135));
108 noCopiesText
= new wxTextCtrl(this, wxPRINTID_COPIES
, "", wxPoint(252, 130), wxSize(40, -1));
110 wxButton
*okButton
= new wxButton(this, wxID_OK
, _("OK"), wxPoint(40, 180), wxSize(100, -1));
111 (void) new wxButton(this, wxID_CANCEL
, _("Cancel"), wxPoint(180, 180), wxSize(100, -1));
113 okButton
->SetDefault();
114 okButton
->SetFocus();
118 // Calls wxWindow::OnInitDialog and then wxGenericPrintDialog::TransferDataToWindow
123 int wxGenericPrintDialog::ShowModal(void)
125 if ( printData
.GetSetupDialog() )
127 wxGenericPrintSetupDialog
*genericPrintSetupDialog
=
128 new wxGenericPrintSetupDialog(GetParent(), wxThePrintSetupData
);
129 int ret
= genericPrintSetupDialog
->ShowModal();
130 if ( ret
!= wxID_CANCEL
)
132 *wxThePrintSetupData
= genericPrintSetupDialog
->printData
;
134 genericPrintSetupDialog
->Close(TRUE
);
139 return wxDialog::ShowModal();
143 wxGenericPrintDialog::~wxGenericPrintDialog(void)
147 void wxGenericPrintDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
149 TransferDataFromWindow();
151 // There are some interactions between the global setup data
152 // and the standard print dialog. The global printing 'mode'
153 // is determined by whether the user checks Print to file
155 if (printData
.GetPrintToFile())
157 wxThePrintSetupData
->SetPrinterMode(PS_FILE
);
159 wxString f
= wxFileSelector(_("PostScript file"),
160 wxPathOnly(wxThePrintSetupData
->GetPrinterFile()),
161 wxFileNameFromPath(wxThePrintSetupData
->GetPrinterFile()),
162 "ps", "*.ps", 0, this);
166 wxThePrintSetupData
->SetPrinterFile(f
);
169 wxThePrintSetupData
->SetPrinterMode(PS_PRINTER
);
174 void wxGenericPrintDialog::OnRange(wxCommandEvent
& event
)
176 if (!fromText
) return;
178 if (event
.GetInt() == 0)
180 fromText
->Enable(FALSE
);
181 toText
->Enable(FALSE
);
183 else if (event
.GetInt() == 1)
185 fromText
->Enable(TRUE
);
186 toText
->Enable(TRUE
);
190 void wxGenericPrintDialog::OnSetup(wxCommandEvent
& WXUNUSED(event
))
192 wxGenericPrintSetupDialog
*genericPrintSetupDialog
=
193 new wxGenericPrintSetupDialog(this, wxThePrintSetupData
);
194 int ret
= genericPrintSetupDialog
->ShowModal();
195 if ( ret
!= wxID_CANCEL
)
197 *wxThePrintSetupData
= genericPrintSetupDialog
->printData
;
198 printData
.SetOrientation(wxThePrintSetupData
->GetPrinterOrientation());
201 genericPrintSetupDialog
->Close(TRUE
);
204 bool wxGenericPrintDialog::TransferDataToWindow(void)
208 if(printData
.GetFromPage() != 0)
210 if (printData
.GetEnablePageNumbers())
212 fromText
->Enable(TRUE
);
213 toText
->Enable(TRUE
);
215 sprintf(buf
, "%d", printData
.GetFromPage());
216 fromText
->SetValue(buf
);
217 sprintf(buf
, "%d", printData
.GetToPage());
218 toText
->SetValue(buf
);
220 if (printData
.GetAllPages())
221 rangeRadioBox
->SetSelection(0);
223 rangeRadioBox
->SetSelection(1);
227 fromText
->Enable(FALSE
);
228 toText
->Enable(FALSE
);
229 rangeRadioBox
->SetSelection(0);
230 rangeRadioBox
->wxRadioBox::Enable(1, FALSE
);
233 sprintf(buf
, "%d", printData
.GetNoCopies());
234 noCopiesText
->SetValue(buf
);
236 printToFileCheckBox
->SetValue(printData
.GetPrintToFile());
237 printToFileCheckBox
->Enable(printData
.GetEnablePrintToFile());
241 bool wxGenericPrintDialog::TransferDataFromWindow(void)
243 if(printData
.GetFromPage() != -1)
245 if (printData
.GetEnablePageNumbers())
247 printData
.SetFromPage(atoi(fromText
->GetValue()));
248 printData
.SetToPage(atoi(toText
->GetValue()));
250 if (rangeRadioBox
->GetSelection() == 0)
251 printData
.SetAllPages(TRUE
);
253 printData
.SetAllPages(FALSE
);
256 { // continuous printing
257 printData
.SetFromPage(1);
258 printData
.SetToPage(32000);
260 printData
.SetNoCopies(atoi(noCopiesText
->GetValue()));
261 printData
.SetPrintToFile(printToFileCheckBox
->GetValue());
266 wxDC
*wxGenericPrintDialog::GetPrintDC(void)
268 return new wxPostScriptDC(wxThePrintSetupData
->GetPrinterFile(), FALSE
, (wxWindow
*) NULL
);
272 * Generic print setup dialog
275 wxGenericPrintSetupDialog::wxGenericPrintSetupDialog(wxWindow
*parent
, wxPrintSetupData
* data
):
276 wxDialog(parent
, -1, _("Print Setup"), wxPoint(0, 0), wxSize(600, 600), wxDEFAULT_DIALOG_STYLE
|wxDIALOG_MODAL
)
281 (void) new wxStaticBox(this, wxPRINTID_STATIC
, _("Paper size"), wxPoint(10, 10), wxSize(200,60) );
285 paperTypeChoice
= CreatePaperTypeChoice(&xPos
, &yPos
);
287 wxString
*choices
= new wxString
[2];
288 choices
[0] = _("Portrait");
289 choices
[1] = _("Landscape");
291 orientationRadioBox
= new wxRadioBox(this, wxPRINTID_ORIENTATION
, _("Orientation"),
292 wxPoint(10, 80), wxSize(-1, -1), 2, choices
, 1, wxRA_VERTICAL
);
293 orientationRadioBox
->SetSelection(0);
295 (void) new wxStaticBox(this, wxPRINTID_STATIC
, _("Options"), wxPoint(10, 130), wxSize(200,50) );
297 int colourXPos
= 145;
303 colourCheckBox
= new wxCheckBox(this, wxPRINTID_PRINTCOLOUR
, _("Print in colour"), wxPoint(15, colourXPos
));
306 (void) new wxStaticBox(this, wxPRINTID_STATIC
, _("Print spooling"), wxPoint(230, 10), wxSize(200,170) );
308 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Printer command:"), wxPoint(240, 30));
310 printerCommandText
= new wxTextCtrl(this, wxPRINTID_COMMAND
, "", wxPoint(260, 55), wxSize(150, -1));
312 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Printer options:"), wxPoint(240, 110));
314 printerOptionsText
= new wxTextCtrl(this, wxPRINTID_OPTIONS
, "", wxPoint(260, 135), wxSize(150, -1));
316 wxButton
*okButton
= new wxButton(this, wxID_OK
, _("OK"), wxPoint(80, 200), wxSize(100, -1));
317 (void) new wxButton(this, wxID_CANCEL
, _("Cancel"), wxPoint(270, 200), wxSize(100, -1));
319 okButton
->SetDefault();
320 okButton
->SetFocus();
329 wxGenericPrintSetupDialog::~wxGenericPrintSetupDialog(void)
333 bool wxGenericPrintSetupDialog::TransferDataToWindow(void)
335 if (printerCommandText
&& printData
.GetPrinterCommand())
336 printerCommandText
->SetValue(printData
.GetPrinterCommand());
337 if (printerOptionsText
&& printData
.GetPrinterOptions())
338 printerOptionsText
->SetValue(printData
.GetPrinterOptions());
340 colourCheckBox
->SetValue(printData
.GetColour());
342 if (orientationRadioBox
)
344 if (printData
.GetPrinterOrientation() == PS_PORTRAIT
)
345 orientationRadioBox
->SetSelection(0);
347 orientationRadioBox
->SetSelection(1);
352 bool wxGenericPrintSetupDialog::TransferDataFromWindow(void)
354 if (printerCommandText
)
355 printData
.SetPrinterCommand(WXSTRINGCAST printerCommandText
->GetValue());
356 if (printerOptionsText
)
357 printData
.SetPrinterOptions(WXSTRINGCAST printerOptionsText
->GetValue());
359 printData
.SetColour(colourCheckBox
->GetValue());
360 if (orientationRadioBox
)
362 int sel
= orientationRadioBox
->GetSelection();
364 printData
.SetPrinterOrientation(PS_PORTRAIT
);
366 printData
.SetPrinterOrientation(PS_LANDSCAPE
);
370 wxString
val(paperTypeChoice
->GetStringSelection());
371 if (!val
.IsNull() && val
!= "")
372 printData
.SetPaperName((char *)(const char *)val
);
374 *wxThePrintSetupData
= GetPrintData();
378 wxChoice
*wxGenericPrintSetupDialog::CreatePaperTypeChoice(int *x
, int *y
)
380 if (!wxThePrintPaperDatabase
)
382 wxThePrintPaperDatabase
= new wxPrintPaperDatabase
;
383 wxThePrintPaperDatabase
->CreateDatabase();
385 int n
= wxThePrintPaperDatabase
->Number();
386 wxString
*choices
= new wxString
[n
];
389 for (i
= 0; i
< n
; i
++)
391 wxPrintPaperType
*paper
= (wxPrintPaperType
*)wxThePrintPaperDatabase
->Nth(i
)->Data();
392 choices
[i
] = paper
->pageName
;
393 if (printData
.GetPaperName() && choices
[i
] == printData
.GetPaperName())
402 wxChoice
*choice
= new wxChoice(this, wxPRINTID_PAPERSIZE
, wxPoint(*x
, *y
), wxSize(width
, -1), n
,
407 choice
->SetSelection(sel
);
412 * Generic page setup dialog
415 void wxGenericPageSetupDialog::OnPrinter(wxCommandEvent
& WXUNUSED(event
))
417 // We no longer query GetPrintMode, so we can eliminate the need
418 // to call SetPrintMode.
419 // This has the limitation that we can't explicitly call the PostScript
420 // print setup dialog from the generic Page Setup dialog under Windows,
421 // but since this choice would only happen when trying to do PostScript
422 // printing under Windows (and only in 16-bit Windows which
423 // doesn't have a Windows-specific page setup dialog) it's worth it.
426 data
.SetSetupDialog(TRUE
);
427 wxPrintDialog
*printDialog
= new wxPrintDialog(this, & data
);
428 printDialog
->ShowModal();
430 printDialog
->Destroy();
433 if (wxTheApp
->GetPrintMode() == wxPRINT_POSTSCRIPT
)
435 wxGenericPrintSetupDialog
*genericPrintSetupDialog
=
436 new wxGenericPrintSetupDialog(this, wxThePrintSetupData
);
437 int ret
= genericPrintSetupDialog
->ShowModal();
439 *wxThePrintSetupData
= genericPrintSetupDialog
->GetPrintData();
441 genericPrintSetupDialog
->Close(TRUE
);
447 data
.SetSetupDialog(TRUE
);
448 wxPrintDialog
printDialog(this, & data
);
449 printDialog
.ShowModal();
456 wxGenericPageSetupDialog::wxGenericPageSetupDialog(wxWindow
*parent
, wxPageSetupData
* data
):
457 wxDialog(parent
, -1, _("Page Setup"), wxPoint(0, 0), wxSize(600, 600), wxDIALOG_MODAL
|wxDEFAULT_DIALOG_STYLE
)
462 int buttonWidth
= 75;
463 int buttonHeight
= 25;
472 wxButton
*okButton
= new wxButton(this, wxID_OK
, _("OK"), wxPoint(5, yPos
), wxSize(buttonWidth
, buttonHeight
));
473 (void) new wxButton(this, wxID_CANCEL
, _("Cancel"), wxPoint(buttonWidth
+ 5 + spacing
, yPos
), wxSize(buttonWidth
, buttonHeight
));
475 printerButton
= new wxButton(this, wxPRINTID_SETUP
, _("Printer..."), wxPoint(buttonWidth
*2 + 5 + 2*spacing
, yPos
), wxSize(buttonWidth
, buttonHeight
));
477 if ( !pageData
.GetEnablePrinter() )
478 printerButton
->Enable(FALSE
);
480 // if (printData.GetEnableHelp())
481 // wxButton *helpButton = new wxButton(this, (wxFunction)wxGenericPageSetupHelpProc, _("Help"), -1, -1, buttonWidth, buttonHeight);
483 okButton
->SetDefault();
484 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;
515 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Left margin (mm):"), wxPoint(xPos
, yPos
));
518 marginLeftText
= new wxTextCtrl(this, wxPRINTID_LEFTMARGIN
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
519 xPos
+= textWidth
+ spacing
;
521 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Right margin (mm):"), wxPoint(xPos
, yPos
));
524 marginRightText
= new wxTextCtrl(this, wxPRINTID_RIGHTMARGIN
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
525 xPos
+= textWidth
+ spacing
;
530 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Top margin (mm):"), wxPoint(xPos
, yPos
));
533 marginTopText
= new wxTextCtrl(this, wxPRINTID_TOPMARGIN
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
534 xPos
+= textWidth
+ spacing
;
536 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Bottom margin (mm):"), wxPoint(xPos
, yPos
));
539 marginBottomText
= new wxTextCtrl(this, wxPRINTID_BOTTOMMARGIN
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
548 wxGenericPageSetupDialog::~wxGenericPageSetupDialog(void)
552 bool wxGenericPageSetupDialog::TransferDataToWindow(void)
555 marginLeftText
->SetValue(IntToString((int) pageData
.GetMarginTopLeft().x
));
557 marginTopText
->SetValue(IntToString((int) pageData
.GetMarginTopLeft().y
));
559 marginRightText
->SetValue(IntToString((int) pageData
.GetMarginBottomRight().x
));
560 if (marginBottomText
)
561 marginBottomText
->SetValue(IntToString((int) pageData
.GetMarginBottomRight().y
));
563 if (orientationRadioBox
)
565 if (pageData
.GetOrientation() == wxPORTRAIT
)
566 orientationRadioBox
->SetSelection(0);
568 orientationRadioBox
->SetSelection(1);
573 bool wxGenericPageSetupDialog::TransferDataFromWindow(void)
575 if (marginLeftText
&& marginTopText
)
576 pageData
.SetMarginTopLeft(wxPoint(atoi((const char *)marginLeftText
->GetValue()),atoi((const char *)marginTopText
->GetValue())));
577 if (marginRightText
&& marginBottomText
)
578 pageData
.SetMarginBottomRight(wxPoint(atoi((const char *)marginRightText
->GetValue()),atoi((const char *)marginBottomText
->GetValue())));
580 if (orientationRadioBox
)
582 int sel
= orientationRadioBox
->GetSelection();
585 wxThePrintSetupData
->SetPrinterOrientation(wxPORTRAIT
);
586 pageData
.SetOrientation(wxPORTRAIT
);
590 wxThePrintSetupData
->SetPrinterOrientation(wxLANDSCAPE
);
591 pageData
.SetOrientation(wxLANDSCAPE
);
596 wxString
val(paperTypeChoice
->GetStringSelection());
597 if (!val
.IsNull() && val
!= "")
599 wxPrintPaperType
* paper
= wxThePrintPaperDatabase
->FindPaperType((char*) (const char *)val
);
602 pageData
.SetPaperSize(wxPoint(paper
->widthMM
, paper
->heightMM
));
610 wxChoice
*wxGenericPageSetupDialog::CreatePaperTypeChoice(int *x
, int *y
)
612 if (!wxThePrintPaperDatabase
)
614 wxThePrintPaperDatabase
= new wxPrintPaperDatabase
;
615 wxThePrintPaperDatabase
->CreateDatabase();
617 int n
= wxThePrintPaperDatabase
->Number();
618 wxString
*choices
= new wxString
[n
];
621 for (i
= 0; i
< n
; i
++)
623 wxPrintPaperType
*paper
= (wxPrintPaperType
*)wxThePrintPaperDatabase
->Nth(i
)->Data();
624 choices
[i
] = paper
->pageName
;
625 if (pageData
.GetPaperSize().x
== paper
->widthMM
&& pageData
.GetPaperSize().y
== paper
->heightMM
)
629 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Paper size"), wxPoint(*x
, *y
));
632 wxChoice
*choice
= new wxChoice(this, wxPRINTID_PAPERSIZE
, wxPoint(*x
, *y
), wxSize(300, -1), n
,
637 choice
->SetSelection(sel
);