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 rangeRadioBox
= new wxRadioBox(this, wxPRINTID_RANGE
, _("Print Range"),
98 wxPoint(5, yPos
), wxSize(-1, -1), 2, choices
, 2);
99 rangeRadioBox
->SetSelection(1);
103 int staticWidth
= 45;
107 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("From:"), wxPoint(xPos
, yPos
));
110 fromText
= new wxTextCtrl(this, wxPRINTID_FROM
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
111 xPos
+= spacing
+ textWidth
;
113 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("To:"), wxPoint(xPos
, yPos
));
116 toText
= new wxTextCtrl(this, wxPRINTID_TO
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
117 xPos
+= spacing
+ textWidth
;
119 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Copies:"), wxPoint(xPos
, yPos
));
120 xPos
+= spacing
+ staticWidth
;
122 noCopiesText
= new wxTextCtrl(this, wxPRINTID_COPIES
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
127 printToFileCheckBox
= new wxCheckBox(this, wxPRINTID_PRINTTOFILE
, _("Print to File"), wxPoint(xPos
, yPos
));
132 // Calls wxWindow::OnInitDialog and then wxGenericPrintDialog::TransferDataToWindow
136 int wxGenericPrintDialog::ShowModal(void)
138 if ( printData
.GetSetupDialog() )
140 wxGenericPrintSetupDialog
*genericPrintSetupDialog
=
141 new wxGenericPrintSetupDialog(GetParent(), wxThePrintSetupData
);
142 int ret
= genericPrintSetupDialog
->ShowModal();
143 if ( ret
!= wxID_CANCEL
)
145 *wxThePrintSetupData
= genericPrintSetupDialog
->printData
;
147 genericPrintSetupDialog
->Close(TRUE
);
152 return wxDialog::ShowModal();
156 wxGenericPrintDialog::~wxGenericPrintDialog(void)
160 void wxGenericPrintDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
162 TransferDataFromWindow();
164 // There are some interactions between the global setup data
165 // and the standard print dialog. The global printing 'mode'
166 // is determined by whether the user checks Print to file
168 if (printData
.GetPrintToFile())
170 wxThePrintSetupData
->SetPrinterMode(PS_FILE
);
172 char *f
= wxFileSelector(_("PostScript file"),
173 wxPathOnly(wxThePrintSetupData
->GetPrinterFile()),
174 wxFileNameFromPath(wxThePrintSetupData
->GetPrinterFile()),
175 "ps", "*.ps", 0, this);
177 wxThePrintSetupData
->SetPrinterFile(f
);
182 wxThePrintSetupData
->SetPrinterMode(PS_PRINTER
);
187 void wxGenericPrintDialog::OnRange(wxCommandEvent
& event
)
189 if (event
.GetInt() == 0)
191 fromText
->Enable(FALSE
);
192 toText
->Enable(FALSE
);
194 else if (event
.GetInt() == 1)
196 fromText
->Enable(TRUE
);
197 toText
->Enable(TRUE
);
201 void wxGenericPrintDialog::OnSetup(wxCommandEvent
& WXUNUSED(event
))
203 wxGenericPrintSetupDialog
*genericPrintSetupDialog
=
204 new wxGenericPrintSetupDialog(this, wxThePrintSetupData
);
205 int ret
= genericPrintSetupDialog
->ShowModal();
206 if ( ret
!= wxID_CANCEL
)
208 *wxThePrintSetupData
= genericPrintSetupDialog
->printData
;
209 printData
.SetOrientation(wxThePrintSetupData
->GetPrinterOrientation());
212 genericPrintSetupDialog
->Close(TRUE
);
215 bool wxGenericPrintDialog::TransferDataToWindow(void)
218 if (printData
.GetEnablePageNumbers())
220 fromText
->Enable(TRUE
);
221 toText
->Enable(TRUE
);
223 sprintf(buf
, "%d", printData
.GetFromPage());
224 fromText
->SetValue(buf
);
225 sprintf(buf
, "%d", printData
.GetToPage());
226 toText
->SetValue(buf
);
228 if (printData
.GetAllPages())
229 rangeRadioBox
->SetSelection(0);
231 rangeRadioBox
->SetSelection(1);
235 fromText
->Enable(FALSE
);
236 toText
->Enable(FALSE
);
237 rangeRadioBox
->SetSelection(0);
238 rangeRadioBox
->wxRadioBox::Enable(1, FALSE
);
240 sprintf(buf
, "%d", printData
.GetNoCopies());
241 noCopiesText
->SetValue(buf
);
243 printToFileCheckBox
->SetValue(printData
.GetPrintToFile());
244 printToFileCheckBox
->Enable(printData
.GetEnablePrintToFile());
248 bool wxGenericPrintDialog::TransferDataFromWindow(void)
250 if (printData
.GetEnablePageNumbers())
252 printData
.SetFromPage(atoi(fromText
->GetValue()));
253 printData
.SetToPage(atoi(toText
->GetValue()));
255 if (rangeRadioBox
->GetSelection() == 0)
256 printData
.SetAllPages(TRUE
);
258 printData
.SetAllPages(FALSE
);
259 printData
.SetNoCopies(atoi(noCopiesText
->GetValue()));
260 printData
.SetPrintToFile(printToFileCheckBox
->GetValue());
265 wxDC
*wxGenericPrintDialog::GetPrintDC(void)
267 return new wxPostScriptDC(wxThePrintSetupData
->GetPrinterFile(), FALSE
, NULL
);
271 * Generic print setup dialog
274 wxGenericPrintSetupDialog::wxGenericPrintSetupDialog(wxWindow
*parent
, wxPrintSetupData
* data
):
275 wxDialog(parent
, -1, _("Print Setup"), wxPoint(0, 0), wxSize(600, 600), wxDEFAULT_DIALOG_STYLE
|wxDIALOG_MODAL
)
280 int buttonWidth
= 65;
281 int buttonHeight
= 25;
286 wxButton
*okButton
= new wxButton(this, wxID_OK
, _("OK"), wxPoint(xPos
, yPos
), wxSize(buttonWidth
, buttonHeight
));
287 xPos
+= buttonWidth
+ spacing
;
288 (void) new wxButton(this, wxID_CANCEL
, _("Cancel"), wxPoint(xPos
, yPos
), wxSize(buttonWidth
, buttonHeight
));
290 okButton
->SetDefault();
291 okButton
->SetFocus();
296 paperTypeChoice
= CreatePaperTypeChoice(&xPos
, &yPos
);
299 choices
[0] = _("Portrait");
300 choices
[1] = _("Landscape");
302 orientationRadioBox
= new wxRadioBox(this, wxPRINTID_ORIENTATION
, _("Orientation"),
303 wxPoint(xPos
, yPos
), wxSize(-1, -1), 2, choices
, 2);
304 orientationRadioBox
->SetSelection(0);
308 colourCheckBox
= new wxCheckBox(this, wxPRINTID_PRINTCOLOUR
, _("Print in colour"), wxPoint(xPos
, yPos
));
313 int staticWidth
= 100;
317 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Printer command:"), wxPoint(xPos
, yPos
));
320 printerCommandText
= new wxTextCtrl(this, wxPRINTID_COMMAND
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
321 xPos
+= textWidth
+ spacing
;
323 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Printer options:"), wxPoint(xPos
, yPos
));
326 printerOptionsText
= new wxTextCtrl(this, wxPRINTID_OPTIONS
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
334 wxGenericPrintSetupDialog::~wxGenericPrintSetupDialog(void)
338 bool wxGenericPrintSetupDialog::TransferDataToWindow(void)
340 if (printerCommandText
&& printData
.GetPrinterCommand())
341 printerCommandText
->SetValue(printData
.GetPrinterCommand());
342 if (printerOptionsText
&& printData
.GetPrinterOptions())
343 printerOptionsText
->SetValue(printData
.GetPrinterOptions());
345 colourCheckBox
->SetValue(printData
.GetColour());
347 if (orientationRadioBox
)
349 if (printData
.GetPrinterOrientation() == PS_PORTRAIT
)
350 orientationRadioBox
->SetSelection(0);
352 orientationRadioBox
->SetSelection(1);
357 bool wxGenericPrintSetupDialog::TransferDataFromWindow(void)
359 if (printerCommandText
)
360 printData
.SetPrinterCommand(WXSTRINGCAST printerCommandText
->GetValue());
361 if (printerOptionsText
)
362 printData
.SetPrinterOptions(WXSTRINGCAST printerOptionsText
->GetValue());
364 printData
.SetColour(colourCheckBox
->GetValue());
365 if (orientationRadioBox
)
367 int sel
= orientationRadioBox
->GetSelection();
369 printData
.SetPrinterOrientation(PS_PORTRAIT
);
371 printData
.SetPrinterOrientation(PS_LANDSCAPE
);
375 wxString
val(paperTypeChoice
->GetStringSelection());
376 if (!val
.IsNull() && val
!= "")
377 printData
.SetPaperName((char *)(const char *)val
);
382 wxChoice
*wxGenericPrintSetupDialog::CreatePaperTypeChoice(int *x
, int *y
)
384 if (!wxThePrintPaperDatabase
)
386 wxThePrintPaperDatabase
= new wxPrintPaperDatabase
;
387 wxThePrintPaperDatabase
->CreateDatabase();
389 int n
= wxThePrintPaperDatabase
->Number();
390 wxString
*choices
= new wxString
[n
];
393 for (i
= 0; i
< n
; i
++)
395 wxPrintPaperType
*paper
= (wxPrintPaperType
*)wxThePrintPaperDatabase
->Nth(i
)->Data();
396 choices
[i
] = paper
->pageName
;
397 if (printData
.GetPaperName() && choices
[i
] == printData
.GetPaperName())
401 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Paper size"), wxPoint(*x
, *y
));
404 wxChoice
*choice
= new wxChoice(this, wxPRINTID_PAPERSIZE
, wxPoint(*x
, *y
), wxSize(300, -1), n
,
409 choice
->SetSelection(sel
);
414 * Generic page setup dialog
417 void wxGenericPageSetupDialog::OnPrinter(wxCommandEvent
& WXUNUSED(event
))
419 if (wxTheApp
->GetPrintMode() == wxPRINT_POSTSCRIPT
)
421 wxGenericPrintSetupDialog
*genericPrintSetupDialog
=
422 new wxGenericPrintSetupDialog(this, wxThePrintSetupData
);
423 int ret
= genericPrintSetupDialog
->ShowModal();
425 *wxThePrintSetupData
= genericPrintSetupDialog
->GetPrintData();
427 genericPrintSetupDialog
->Close(TRUE
);
433 data
.SetSetupDialog(TRUE
);
434 wxPrintDialog
printDialog(this, & data
);
435 printDialog
.Show(TRUE
);
440 wxGenericPageSetupDialog::wxGenericPageSetupDialog(wxWindow
*parent
, wxPageSetupData
* data
):
441 wxDialog(parent
, -1, _("Page Setup"), wxPoint(0, 0), wxSize(600, 600), wxDIALOG_MODAL
|wxDEFAULT_DIALOG_STYLE
)
446 int buttonWidth
= 75;
447 int buttonHeight
= 25;
452 wxButton
*okButton
= new wxButton(this, wxID_OK
, _("OK"), wxPoint(5, yPos
), wxSize(buttonWidth
, buttonHeight
));
453 (void) new wxButton(this, wxID_CANCEL
, _("Cancel"), wxPoint(buttonWidth
+ 5 + spacing
, yPos
), wxSize(buttonWidth
, buttonHeight
));
455 printerButton
= new wxButton(this, wxPRINTID_SETUP
, _("Printer..."), wxPoint(buttonWidth
*2 + 5 + 2*spacing
, yPos
), wxSize(buttonWidth
, buttonHeight
));
457 if ( !pageData
.GetEnablePrinter() )
458 printerButton
->Enable(FALSE
);
460 // if (printData.GetEnableHelp())
461 // wxButton *helpButton = new wxButton(this, (wxFunction)wxGenericPageSetupHelpProc, _("Help"), -1, -1, buttonWidth, buttonHeight);
463 okButton
->SetDefault();
464 okButton
->SetFocus();
469 paperTypeChoice
= CreatePaperTypeChoice(&xPos
, &yPos
);
474 choices
[0] = _("Portrait");
475 choices
[1] = _("Landscape");
476 orientationRadioBox
= new wxRadioBox(this, wxPRINTID_ORIENTATION
, _("Orientation"),
477 wxPoint(xPos
, yPos
), wxSize(-1, -1), 2, choices
, 2);
478 orientationRadioBox
->SetSelection(0);
483 int staticWidth
= 110;
487 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Left margin (mm):"), wxPoint(xPos
, yPos
));
490 marginLeftText
= new wxTextCtrl(this, wxPRINTID_LEFTMARGIN
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
491 xPos
+= textWidth
+ spacing
;
493 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Right margin (mm):"), wxPoint(xPos
, yPos
));
496 marginRightText
= new wxTextCtrl(this, wxPRINTID_RIGHTMARGIN
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
497 xPos
+= textWidth
+ spacing
;
502 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Top margin (mm):"), wxPoint(xPos
, yPos
));
505 marginTopText
= new wxTextCtrl(this, wxPRINTID_TOPMARGIN
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
506 xPos
+= textWidth
+ spacing
;
508 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Bottom margin (mm):"), wxPoint(xPos
, yPos
));
511 marginBottomText
= new wxTextCtrl(this, wxPRINTID_BOTTOMMARGIN
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
519 wxGenericPageSetupDialog::~wxGenericPageSetupDialog(void)
523 bool wxGenericPageSetupDialog::TransferDataToWindow(void)
526 marginLeftText
->SetValue(IntToString((int) pageData
.GetMarginTopLeft().x
));
528 marginTopText
->SetValue(IntToString((int) pageData
.GetMarginTopLeft().y
));
530 marginRightText
->SetValue(IntToString((int) pageData
.GetMarginBottomRight().x
));
531 if (marginBottomText
)
532 marginBottomText
->SetValue(IntToString((int) pageData
.GetMarginBottomRight().y
));
534 if (orientationRadioBox
)
536 if (pageData
.GetOrientation() == wxPORTRAIT
)
537 orientationRadioBox
->SetSelection(0);
539 orientationRadioBox
->SetSelection(1);
544 bool wxGenericPageSetupDialog::TransferDataFromWindow(void)
546 if (marginLeftText
&& marginTopText
)
547 pageData
.SetMarginTopLeft(wxPoint(atoi((const char *)marginLeftText
->GetValue()),atoi((const char *)marginTopText
->GetValue())));
548 if (marginRightText
&& marginBottomText
)
549 pageData
.SetMarginBottomRight(wxPoint(atoi((const char *)marginRightText
->GetValue()),atoi((const char *)marginBottomText
->GetValue())));
551 if (orientationRadioBox
)
553 int sel
= orientationRadioBox
->GetSelection();
556 wxThePrintSetupData
->SetPrinterOrientation(wxPORTRAIT
);
557 pageData
.SetOrientation(wxPORTRAIT
);
561 wxThePrintSetupData
->SetPrinterOrientation(wxLANDSCAPE
);
562 pageData
.SetOrientation(wxLANDSCAPE
);
567 wxString
val(paperTypeChoice
->GetStringSelection());
568 if (!val
.IsNull() && val
!= "")
570 wxPrintPaperType
* paper
= wxThePrintPaperDatabase
->FindPaperType((char*) (const char *)val
);
573 pageData
.SetPaperSize(wxPoint(paper
->widthMM
, paper
->heightMM
));
580 wxChoice
*wxGenericPageSetupDialog::CreatePaperTypeChoice(int *x
, int *y
)
582 if (!wxThePrintPaperDatabase
)
584 wxThePrintPaperDatabase
= new wxPrintPaperDatabase
;
585 wxThePrintPaperDatabase
->CreateDatabase();
587 int n
= wxThePrintPaperDatabase
->Number();
588 wxString
*choices
= new wxString
[n
];
591 for (i
= 0; i
< n
; i
++)
593 wxPrintPaperType
*paper
= (wxPrintPaperType
*)wxThePrintPaperDatabase
->Nth(i
)->Data();
594 choices
[i
] = paper
->pageName
;
595 if (pageData
.GetPaperSize().x
== paper
->widthMM
&& pageData
.GetPaperSize().y
== paper
->heightMM
)
599 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Paper size"), wxPoint(*x
, *y
));
602 wxChoice
*choice
= new wxChoice(this, wxPRINTID_PAPERSIZE
, wxPoint(*x
, *y
), wxSize(300, -1), n
,
607 choice
->SetSelection(sel
);