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/statbox.h"
34 #include "wx/button.h"
35 #include "wx/checkbox.h"
36 #include "wx/textctrl.h"
37 #include "wx/radiobox.h"
38 #include "wx/filedlg.h"
39 #include "wx/choice.h"
43 #include "wx/generic/prntdlgg.h"
44 #include "wx/printdlg.h"
49 #if !USE_SHARED_LIBRARY
50 IMPLEMENT_CLASS(wxGenericPrintDialog
, wxDialog
)
51 IMPLEMENT_CLASS(wxGenericPrintSetupDialog
, wxDialog
)
52 IMPLEMENT_CLASS(wxGenericPageSetupDialog
, wxDialog
)
54 BEGIN_EVENT_TABLE(wxGenericPrintDialog
, wxDialog
)
55 EVT_BUTTON(wxID_OK
, wxGenericPrintDialog::OnOK
)
56 EVT_BUTTON(wxPRINTID_SETUP
, wxGenericPrintDialog::OnSetup
)
57 EVT_RADIOBOX(wxPRINTID_RANGE
, wxGenericPrintDialog::OnRange
)
60 BEGIN_EVENT_TABLE(wxGenericPageSetupDialog
, wxDialog
)
61 EVT_BUTTON(wxPRINTID_SETUP
, wxGenericPageSetupDialog::OnPrinter
)
65 extern wxPrintPaperDatabase
*wxThePrintPaperDatabase
;
68 * Generic print dialog for non-Windows printing use.
72 wxGenericPrintDialog::wxGenericPrintDialog(wxWindow
*parent
, wxPrintData
* data
):
73 wxDialog(parent
, -1, _("Print"), wxPoint(0, 0), wxSize(600, 600), wxDEFAULT_DIALOG_STYLE
|wxDIALOG_MODAL
)
78 (void)new wxStaticBox( this, -1, _( "Printer options" ), wxPoint( 5, 5), wxSize( 300, 60 ) );
80 printToFileCheckBox
= new wxCheckBox(this, wxPRINTID_PRINTTOFILE
, _("Print to File"), wxPoint(20, 25) );
82 setupButton
= new wxButton(this, wxPRINTID_SETUP
, _("Setup..."), wxPoint(160, 25), wxSize(100, -1));
84 wxString
*choices
= new wxString
[2];
85 choices
[0] = _("All");
86 choices
[1] = _("Pages");
88 fromText
= (wxTextCtrl
*)NULL
;
90 if(printData
.GetFromPage() != 0)
92 rangeRadioBox
= new wxRadioBox(this, wxPRINTID_RANGE
, _("Print Range"),
93 wxPoint(5, 80), wxSize(-1, -1), 2, choices
, 1, wxRA_VERTICAL
);
94 rangeRadioBox
->SetSelection(1);
97 if(printData
.GetFromPage() != 0)
99 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("From:"), wxPoint(5, 135));
101 fromText
= new wxTextCtrl(this, wxPRINTID_FROM
, "", wxPoint(45, 130), wxSize(40, -1));
103 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("To:"), wxPoint(100, 135));
105 toText
= new wxTextCtrl(this, wxPRINTID_TO
, "", wxPoint(133, 130), wxSize(40, -1));
108 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Copies:"), wxPoint(200, 135));
110 noCopiesText
= new wxTextCtrl(this, wxPRINTID_COPIES
, "", wxPoint(252, 130), wxSize(40, -1));
112 wxButton
*okButton
= new wxButton(this, wxID_OK
, _("OK"), wxPoint(40, 180), wxSize(100, -1));
113 (void) new wxButton(this, wxID_CANCEL
, _("Cancel"), wxPoint(180, 180), wxSize(100, -1));
115 okButton
->SetDefault();
116 okButton
->SetFocus();
120 // Calls wxWindow::OnInitDialog and then wxGenericPrintDialog::TransferDataToWindow
125 int wxGenericPrintDialog::ShowModal(void)
127 if ( printData
.GetSetupDialog() )
129 wxGenericPrintSetupDialog
*genericPrintSetupDialog
=
130 new wxGenericPrintSetupDialog(GetParent(), wxThePrintSetupData
);
131 int ret
= genericPrintSetupDialog
->ShowModal();
132 if ( ret
!= wxID_CANCEL
)
134 *wxThePrintSetupData
= genericPrintSetupDialog
->printData
;
136 genericPrintSetupDialog
->Close(TRUE
);
141 return wxDialog::ShowModal();
145 wxGenericPrintDialog::~wxGenericPrintDialog(void)
149 void wxGenericPrintDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
151 TransferDataFromWindow();
153 // There are some interactions between the global setup data
154 // and the standard print dialog. The global printing 'mode'
155 // is determined by whether the user checks Print to file
157 if (printData
.GetPrintToFile())
159 wxThePrintSetupData
->SetPrinterMode(PS_FILE
);
161 char *f
= wxFileSelector(_("PostScript file"),
162 wxPathOnly(wxThePrintSetupData
->GetPrinterFile()),
163 wxFileNameFromPath(wxThePrintSetupData
->GetPrinterFile()),
164 "ps", "*.ps", 0, this);
166 wxThePrintSetupData
->SetPrinterFile(f
);
171 wxThePrintSetupData
->SetPrinterMode(PS_PRINTER
);
176 void wxGenericPrintDialog::OnRange(wxCommandEvent
& event
)
178 if (!fromText
) return;
180 if (event
.GetInt() == 0)
182 fromText
->Enable(FALSE
);
183 toText
->Enable(FALSE
);
185 else if (event
.GetInt() == 1)
187 fromText
->Enable(TRUE
);
188 toText
->Enable(TRUE
);
192 void wxGenericPrintDialog::OnSetup(wxCommandEvent
& WXUNUSED(event
))
194 wxGenericPrintSetupDialog
*genericPrintSetupDialog
=
195 new wxGenericPrintSetupDialog(this, wxThePrintSetupData
);
196 int ret
= genericPrintSetupDialog
->ShowModal();
197 if ( ret
!= wxID_CANCEL
)
199 *wxThePrintSetupData
= genericPrintSetupDialog
->printData
;
200 printData
.SetOrientation(wxThePrintSetupData
->GetPrinterOrientation());
203 genericPrintSetupDialog
->Close(TRUE
);
206 bool wxGenericPrintDialog::TransferDataToWindow(void)
210 if(printData
.GetFromPage() != 0)
212 if (printData
.GetEnablePageNumbers())
214 fromText
->Enable(TRUE
);
215 toText
->Enable(TRUE
);
217 sprintf(buf
, "%d", printData
.GetFromPage());
218 fromText
->SetValue(buf
);
219 sprintf(buf
, "%d", printData
.GetToPage());
220 toText
->SetValue(buf
);
222 if (printData
.GetAllPages())
223 rangeRadioBox
->SetSelection(0);
225 rangeRadioBox
->SetSelection(1);
229 fromText
->Enable(FALSE
);
230 toText
->Enable(FALSE
);
231 rangeRadioBox
->SetSelection(0);
232 rangeRadioBox
->wxRadioBox::Enable(1, FALSE
);
235 sprintf(buf
, "%d", printData
.GetNoCopies());
236 noCopiesText
->SetValue(buf
);
238 printToFileCheckBox
->SetValue(printData
.GetPrintToFile());
239 printToFileCheckBox
->Enable(printData
.GetEnablePrintToFile());
243 bool wxGenericPrintDialog::TransferDataFromWindow(void)
245 if(printData
.GetFromPage() != -1)
247 if (printData
.GetEnablePageNumbers())
249 printData
.SetFromPage(atoi(fromText
->GetValue()));
250 printData
.SetToPage(atoi(toText
->GetValue()));
252 if (rangeRadioBox
->GetSelection() == 0)
253 printData
.SetAllPages(TRUE
);
255 printData
.SetAllPages(FALSE
);
258 { // continuous printing
259 printData
.SetFromPage(1);
260 printData
.SetToPage(32000);
262 printData
.SetNoCopies(atoi(noCopiesText
->GetValue()));
263 printData
.SetPrintToFile(printToFileCheckBox
->GetValue());
268 wxDC
*wxGenericPrintDialog::GetPrintDC(void)
270 return new wxPostScriptDC(wxThePrintSetupData
->GetPrinterFile(), FALSE
, (wxWindow
*) NULL
);
274 * Generic print setup dialog
277 wxGenericPrintSetupDialog::wxGenericPrintSetupDialog(wxWindow
*parent
, wxPrintSetupData
* data
):
278 wxDialog(parent
, -1, _("Print Setup"), wxPoint(0, 0), wxSize(600, 600), wxDEFAULT_DIALOG_STYLE
|wxDIALOG_MODAL
)
283 (void) new wxStaticBox(this, wxPRINTID_STATIC
, _("Paper size"), wxPoint(10, 10), wxSize(200,60) );
287 paperTypeChoice
= CreatePaperTypeChoice(&xPos
, &yPos
);
289 wxString
*choices
= new wxString
[2];
290 choices
[0] = _("Portrait");
291 choices
[1] = _("Landscape");
293 orientationRadioBox
= new wxRadioBox(this, wxPRINTID_ORIENTATION
, _("Orientation"),
294 wxPoint(10, 80), wxSize(-1, -1), 2, choices
, 1, wxRA_VERTICAL
);
295 orientationRadioBox
->SetSelection(0);
297 (void) new wxStaticBox(this, wxPRINTID_STATIC
, _("Options"), wxPoint(10, 130), wxSize(200,50) );
299 colourCheckBox
= new wxCheckBox(this, wxPRINTID_PRINTCOLOUR
, _("Print in colour"), wxPoint(15, 145));
302 (void) new wxStaticBox(this, wxPRINTID_STATIC
, _("Print spooling"), wxPoint(230, 10), wxSize(200,170) );
304 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Printer command:"), wxPoint(240, 30));
306 printerCommandText
= new wxTextCtrl(this, wxPRINTID_COMMAND
, "", wxPoint(260, 55), wxSize(150, -1));
308 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Printer options:"), wxPoint(240, 110));
310 printerOptionsText
= new wxTextCtrl(this, wxPRINTID_OPTIONS
, "", wxPoint(260, 135), wxSize(150, -1));
312 wxButton
*okButton
= new wxButton(this, wxID_OK
, _("OK"), wxPoint(80, 200), wxSize(100, -1));
313 (void) new wxButton(this, wxID_CANCEL
, _("Cancel"), wxPoint(270, 200), wxSize(100, -1));
315 okButton
->SetDefault();
316 okButton
->SetFocus();
325 wxGenericPrintSetupDialog::~wxGenericPrintSetupDialog(void)
329 bool wxGenericPrintSetupDialog::TransferDataToWindow(void)
331 if (printerCommandText
&& printData
.GetPrinterCommand())
332 printerCommandText
->SetValue(printData
.GetPrinterCommand());
333 if (printerOptionsText
&& printData
.GetPrinterOptions())
334 printerOptionsText
->SetValue(printData
.GetPrinterOptions());
336 colourCheckBox
->SetValue(printData
.GetColour());
338 if (orientationRadioBox
)
340 if (printData
.GetPrinterOrientation() == PS_PORTRAIT
)
341 orientationRadioBox
->SetSelection(0);
343 orientationRadioBox
->SetSelection(1);
348 bool wxGenericPrintSetupDialog::TransferDataFromWindow(void)
350 if (printerCommandText
)
351 printData
.SetPrinterCommand(WXSTRINGCAST printerCommandText
->GetValue());
352 if (printerOptionsText
)
353 printData
.SetPrinterOptions(WXSTRINGCAST printerOptionsText
->GetValue());
355 printData
.SetColour(colourCheckBox
->GetValue());
356 if (orientationRadioBox
)
358 int sel
= orientationRadioBox
->GetSelection();
360 printData
.SetPrinterOrientation(PS_PORTRAIT
);
362 printData
.SetPrinterOrientation(PS_LANDSCAPE
);
366 wxString
val(paperTypeChoice
->GetStringSelection());
367 if (!val
.IsNull() && val
!= "")
368 printData
.SetPaperName((char *)(const char *)val
);
373 wxChoice
*wxGenericPrintSetupDialog::CreatePaperTypeChoice(int *x
, int *y
)
375 if (!wxThePrintPaperDatabase
)
377 wxThePrintPaperDatabase
= new wxPrintPaperDatabase
;
378 wxThePrintPaperDatabase
->CreateDatabase();
380 int n
= wxThePrintPaperDatabase
->Number();
381 wxString
*choices
= new wxString
[n
];
384 for (i
= 0; i
< n
; i
++)
386 wxPrintPaperType
*paper
= (wxPrintPaperType
*)wxThePrintPaperDatabase
->Nth(i
)->Data();
387 choices
[i
] = paper
->pageName
;
388 if (printData
.GetPaperName() && choices
[i
] == printData
.GetPaperName())
392 wxChoice
*choice
= new wxChoice(this, wxPRINTID_PAPERSIZE
, wxPoint(*x
, *y
), wxSize(170, -1), n
,
397 choice
->SetSelection(sel
);
402 * Generic page setup dialog
405 void wxGenericPageSetupDialog::OnPrinter(wxCommandEvent
& WXUNUSED(event
))
407 if (wxTheApp
->GetPrintMode() == wxPRINT_POSTSCRIPT
)
409 wxGenericPrintSetupDialog
*genericPrintSetupDialog
=
410 new wxGenericPrintSetupDialog(this, wxThePrintSetupData
);
411 int ret
= genericPrintSetupDialog
->ShowModal();
413 *wxThePrintSetupData
= genericPrintSetupDialog
->GetPrintData();
415 genericPrintSetupDialog
->Close(TRUE
);
421 data
.SetSetupDialog(TRUE
);
422 wxPrintDialog
printDialog(this, & data
);
423 printDialog
.Show(TRUE
);
428 wxGenericPageSetupDialog::wxGenericPageSetupDialog(wxWindow
*parent
, wxPageSetupData
* data
):
429 wxDialog(parent
, -1, _("Page Setup"), wxPoint(0, 0), wxSize(600, 600), wxDIALOG_MODAL
|wxDEFAULT_DIALOG_STYLE
)
434 int buttonWidth
= 75;
435 int buttonHeight
= 25;
440 wxButton
*okButton
= new wxButton(this, wxID_OK
, _("OK"), wxPoint(5, yPos
), wxSize(buttonWidth
, buttonHeight
));
441 (void) new wxButton(this, wxID_CANCEL
, _("Cancel"), wxPoint(buttonWidth
+ 5 + spacing
, yPos
), wxSize(buttonWidth
, buttonHeight
));
443 printerButton
= new wxButton(this, wxPRINTID_SETUP
, _("Printer..."), wxPoint(buttonWidth
*2 + 5 + 2*spacing
, yPos
), wxSize(buttonWidth
, buttonHeight
));
445 if ( !pageData
.GetEnablePrinter() )
446 printerButton
->Enable(FALSE
);
448 // if (printData.GetEnableHelp())
449 // wxButton *helpButton = new wxButton(this, (wxFunction)wxGenericPageSetupHelpProc, _("Help"), -1, -1, buttonWidth, buttonHeight);
451 okButton
->SetDefault();
452 okButton
->SetFocus();
457 paperTypeChoice
= CreatePaperTypeChoice(&xPos
, &yPos
);
461 wxString
*choices
= new wxString
[2];
462 choices
[0] = _("Portrait");
463 choices
[1] = _("Landscape");
464 orientationRadioBox
= new wxRadioBox(this, wxPRINTID_ORIENTATION
, _("Orientation"),
465 wxPoint(xPos
, yPos
), wxSize(-1, -1), 2, choices
, 2);
466 orientationRadioBox
->SetSelection(0);
471 int staticWidth
= 110;
475 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Left margin (mm):"), wxPoint(xPos
, yPos
));
478 marginLeftText
= new wxTextCtrl(this, wxPRINTID_LEFTMARGIN
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
479 xPos
+= textWidth
+ spacing
;
481 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Right margin (mm):"), wxPoint(xPos
, yPos
));
484 marginRightText
= new wxTextCtrl(this, wxPRINTID_RIGHTMARGIN
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
485 xPos
+= textWidth
+ spacing
;
490 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Top margin (mm):"), wxPoint(xPos
, yPos
));
493 marginTopText
= new wxTextCtrl(this, wxPRINTID_TOPMARGIN
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
494 xPos
+= textWidth
+ spacing
;
496 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Bottom margin (mm):"), wxPoint(xPos
, yPos
));
499 marginBottomText
= new wxTextCtrl(this, wxPRINTID_BOTTOMMARGIN
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
508 wxGenericPageSetupDialog::~wxGenericPageSetupDialog(void)
512 bool wxGenericPageSetupDialog::TransferDataToWindow(void)
515 marginLeftText
->SetValue(IntToString((int) pageData
.GetMarginTopLeft().x
));
517 marginTopText
->SetValue(IntToString((int) pageData
.GetMarginTopLeft().y
));
519 marginRightText
->SetValue(IntToString((int) pageData
.GetMarginBottomRight().x
));
520 if (marginBottomText
)
521 marginBottomText
->SetValue(IntToString((int) pageData
.GetMarginBottomRight().y
));
523 if (orientationRadioBox
)
525 if (pageData
.GetOrientation() == wxPORTRAIT
)
526 orientationRadioBox
->SetSelection(0);
528 orientationRadioBox
->SetSelection(1);
533 bool wxGenericPageSetupDialog::TransferDataFromWindow(void)
535 if (marginLeftText
&& marginTopText
)
536 pageData
.SetMarginTopLeft(wxPoint(atoi((const char *)marginLeftText
->GetValue()),atoi((const char *)marginTopText
->GetValue())));
537 if (marginRightText
&& marginBottomText
)
538 pageData
.SetMarginBottomRight(wxPoint(atoi((const char *)marginRightText
->GetValue()),atoi((const char *)marginBottomText
->GetValue())));
540 if (orientationRadioBox
)
542 int sel
= orientationRadioBox
->GetSelection();
545 wxThePrintSetupData
->SetPrinterOrientation(wxPORTRAIT
);
546 pageData
.SetOrientation(wxPORTRAIT
);
550 wxThePrintSetupData
->SetPrinterOrientation(wxLANDSCAPE
);
551 pageData
.SetOrientation(wxLANDSCAPE
);
556 wxString
val(paperTypeChoice
->GetStringSelection());
557 if (!val
.IsNull() && val
!= "")
559 wxPrintPaperType
* paper
= wxThePrintPaperDatabase
->FindPaperType((char*) (const char *)val
);
562 pageData
.SetPaperSize(wxPoint(paper
->widthMM
, paper
->heightMM
));
569 wxChoice
*wxGenericPageSetupDialog::CreatePaperTypeChoice(int *x
, int *y
)
571 if (!wxThePrintPaperDatabase
)
573 wxThePrintPaperDatabase
= new wxPrintPaperDatabase
;
574 wxThePrintPaperDatabase
->CreateDatabase();
576 int n
= wxThePrintPaperDatabase
->Number();
577 wxString
*choices
= new wxString
[n
];
580 for (i
= 0; i
< n
; i
++)
582 wxPrintPaperType
*paper
= (wxPrintPaperType
*)wxThePrintPaperDatabase
->Nth(i
)->Data();
583 choices
[i
] = paper
->pageName
;
584 if (pageData
.GetPaperSize().x
== paper
->widthMM
&& pageData
.GetPaperSize().y
== paper
->heightMM
)
588 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Paper size"), wxPoint(*x
, *y
));
591 wxChoice
*choice
= new wxChoice(this, wxPRINTID_PAPERSIZE
, wxPoint(*x
, *y
), wxSize(300, -1), n
,
596 choice
->SetSelection(sel
);