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
)
79 int buttonHeight
= 25;
84 wxButton
*okButton
= new wxButton(this, wxID_OK
, _("OK"), wxPoint(5, yPos
), wxSize(buttonWidth
, buttonHeight
));
85 (void) new wxButton(this, wxID_CANCEL
, _("Cancel"), wxPoint(buttonWidth
+ 5 + spacing
, yPos
), wxSize(buttonWidth
, buttonHeight
));
87 setupButton
= new wxButton(this, wxPRINTID_SETUP
, _("Setup..."), wxPoint(buttonWidth
*2 + 5 + 2*spacing
, yPos
), wxSize(buttonWidth
, buttonHeight
));
89 okButton
->SetDefault();
94 wxString
*choices
= new wxString
[2];
95 choices
[0] = _("All");
96 choices
[1] = _("Pages");
98 fromText
= (wxTextCtrl
*)NULL
;
100 if(printData
.GetFromPage() != 0)
102 rangeRadioBox
= new wxRadioBox(this, wxPRINTID_RANGE
, _("Print Range"),
103 wxPoint(5, yPos
), wxSize(-1, -1), 2, choices
, 2);
104 rangeRadioBox
->SetSelection(1);
109 int staticWidth
= 45;
113 if(printData
.GetFromPage() != 0)
115 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("From:"), wxPoint(xPos
, yPos
));
118 fromText
= new wxTextCtrl(this, wxPRINTID_FROM
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
119 xPos
+= spacing
+ textWidth
;
121 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("To:"), wxPoint(xPos
, yPos
));
124 toText
= new wxTextCtrl(this, wxPRINTID_TO
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
125 xPos
+= spacing
+ textWidth
;
128 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Copies:"), wxPoint(xPos
, yPos
));
129 xPos
+= spacing
+ staticWidth
;
131 noCopiesText
= new wxTextCtrl(this, wxPRINTID_COPIES
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
136 printToFileCheckBox
= new wxCheckBox(this, wxPRINTID_PRINTTOFILE
, _("Print to File"), wxPoint(xPos
, yPos
));
141 // Calls wxWindow::OnInitDialog and then wxGenericPrintDialog::TransferDataToWindow
146 int wxGenericPrintDialog
::ShowModal(void)
148 if ( printData
.GetSetupDialog() )
150 wxGenericPrintSetupDialog
*genericPrintSetupDialog
=
151 new wxGenericPrintSetupDialog(GetParent(), wxThePrintSetupData
);
152 int ret
= genericPrintSetupDialog
->ShowModal();
153 if ( ret
!= wxID_CANCEL
)
155 *wxThePrintSetupData
= genericPrintSetupDialog
->printData
;
157 genericPrintSetupDialog
->Close(TRUE
);
162 return wxDialog
::ShowModal();
166 wxGenericPrintDialog
::~wxGenericPrintDialog(void)
170 void wxGenericPrintDialog
::OnOK(wxCommandEvent
& WXUNUSED(event
))
172 TransferDataFromWindow();
174 // There are some interactions between the global setup data
175 // and the standard print dialog. The global printing 'mode'
176 // is determined by whether the user checks Print to file
178 if (printData
.GetPrintToFile())
180 wxThePrintSetupData
->SetPrinterMode(PS_FILE
);
182 char *f
= wxFileSelector(_("PostScript file"),
183 wxPathOnly(wxThePrintSetupData
->GetPrinterFile()),
184 wxFileNameFromPath(wxThePrintSetupData
->GetPrinterFile()),
185 "ps", "*.ps", 0, this);
187 wxThePrintSetupData
->SetPrinterFile(f
);
192 wxThePrintSetupData
->SetPrinterMode(PS_PRINTER
);
197 void wxGenericPrintDialog
::OnRange(wxCommandEvent
& event
)
199 if (!fromText
) return;
201 if (event
.GetInt() == 0)
203 fromText
->Enable(FALSE
);
204 toText
->Enable(FALSE
);
206 else if (event
.GetInt() == 1)
208 fromText
->Enable(TRUE
);
209 toText
->Enable(TRUE
);
213 void wxGenericPrintDialog
::OnSetup(wxCommandEvent
& WXUNUSED(event
))
215 wxGenericPrintSetupDialog
*genericPrintSetupDialog
=
216 new wxGenericPrintSetupDialog(this, wxThePrintSetupData
);
217 int ret
= genericPrintSetupDialog
->ShowModal();
218 if ( ret
!= wxID_CANCEL
)
220 *wxThePrintSetupData
= genericPrintSetupDialog
->printData
;
221 printData
.SetOrientation(wxThePrintSetupData
->GetPrinterOrientation());
224 genericPrintSetupDialog
->Close(TRUE
);
227 bool wxGenericPrintDialog
::TransferDataToWindow(void)
231 if(printData
.GetFromPage() != 0)
233 if (printData
.GetEnablePageNumbers())
235 fromText
->Enable(TRUE
);
236 toText
->Enable(TRUE
);
238 sprintf(buf
, "%d", printData
.GetFromPage());
239 fromText
->SetValue(buf
);
240 sprintf(buf
, "%d", printData
.GetToPage());
241 toText
->SetValue(buf
);
243 if (printData
.GetAllPages())
244 rangeRadioBox
->SetSelection(0);
246 rangeRadioBox
->SetSelection(1);
250 fromText
->Enable(FALSE
);
251 toText
->Enable(FALSE
);
252 rangeRadioBox
->SetSelection(0);
253 rangeRadioBox
->wxRadioBox
::Enable(1, FALSE
);
256 sprintf(buf
, "%d", printData
.GetNoCopies());
257 noCopiesText
->SetValue(buf
);
259 printToFileCheckBox
->SetValue(printData
.GetPrintToFile());
260 printToFileCheckBox
->Enable(printData
.GetEnablePrintToFile());
264 bool wxGenericPrintDialog
::TransferDataFromWindow(void)
266 if(printData
.GetFromPage() != -1)
268 if (printData
.GetEnablePageNumbers())
270 printData
.SetFromPage(atoi(fromText
->GetValue()));
271 printData
.SetToPage(atoi(toText
->GetValue()));
273 if (rangeRadioBox
->GetSelection() == 0)
274 printData
.SetAllPages(TRUE
);
276 printData
.SetAllPages(FALSE
);
279 { // continuous printing
280 printData
.SetFromPage(1);
281 printData
.SetToPage(32000);
283 printData
.SetNoCopies(atoi(noCopiesText
->GetValue()));
284 printData
.SetPrintToFile(printToFileCheckBox
->GetValue());
289 wxDC
*wxGenericPrintDialog
::GetPrintDC(void)
291 return new wxPostScriptDC(wxThePrintSetupData
->GetPrinterFile(), FALSE
, (wxWindow
*) NULL
);
295 * Generic print setup dialog
298 wxGenericPrintSetupDialog
::wxGenericPrintSetupDialog(wxWindow
*parent
, wxPrintSetupData
* data
):
299 wxDialog(parent
, -1, _("Print Setup"), wxPoint(0, 0), wxSize(600, 600), wxDEFAULT_DIALOG_STYLE
|wxDIALOG_MODAL
)
304 (void) new wxStaticBox(this, wxPRINTID_STATIC
, _("Paper size"), wxPoint(10, 10), wxSize(200,60) );
308 paperTypeChoice
= CreatePaperTypeChoice(&xPos
, &yPos
);
310 wxString
*choices
= new wxString
[2];
311 choices
[0] = _("Portrait");
312 choices
[1] = _("Landscape");
314 orientationRadioBox
= new wxRadioBox(this, wxPRINTID_ORIENTATION
, _("Orientation"),
315 wxPoint(10, 80), wxSize(-1, -1), 2, choices
, 2);
316 orientationRadioBox
->SetSelection(0);
318 (void) new wxStaticBox(this, wxPRINTID_STATIC
, _("Options"), wxPoint(10, 130), wxSize(200,50) );
320 colourCheckBox
= new wxCheckBox(this, wxPRINTID_PRINTCOLOUR
, _("Print in colour"), wxPoint(15, 145));
323 (void) new wxStaticBox(this, wxPRINTID_STATIC
, _("Print spooling"), wxPoint(230, 10), wxSize(200,170) );
325 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Printer command:"), wxPoint(240, 30));
327 printerCommandText
= new wxTextCtrl(this, wxPRINTID_COMMAND
, "", wxPoint(260, 55), wxSize(150, -1));
329 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Printer options:"), wxPoint(240, 110));
331 printerOptionsText
= new wxTextCtrl(this, wxPRINTID_OPTIONS
, "", wxPoint(260, 135), wxSize(150, -1));
333 wxButton
*okButton
= new wxButton(this, wxID_OK
, _("OK"), wxPoint(240, 200), wxSize(100, -1));
334 (void) new wxButton(this, wxID_CANCEL
, _("Cancel"), wxPoint(360, 200), wxSize(100, -1));
336 okButton
->SetDefault();
337 okButton
->SetFocus();
346 wxGenericPrintSetupDialog
::~wxGenericPrintSetupDialog(void)
350 bool wxGenericPrintSetupDialog
::TransferDataToWindow(void)
352 if (printerCommandText
&& printData
.GetPrinterCommand())
353 printerCommandText
->SetValue(printData
.GetPrinterCommand());
354 if (printerOptionsText
&& printData
.GetPrinterOptions())
355 printerOptionsText
->SetValue(printData
.GetPrinterOptions());
357 colourCheckBox
->SetValue(printData
.GetColour());
359 if (orientationRadioBox
)
361 if (printData
.GetPrinterOrientation() == PS_PORTRAIT
)
362 orientationRadioBox
->SetSelection(0);
364 orientationRadioBox
->SetSelection(1);
369 bool wxGenericPrintSetupDialog
::TransferDataFromWindow(void)
371 if (printerCommandText
)
372 printData
.SetPrinterCommand(WXSTRINGCAST printerCommandText
->GetValue());
373 if (printerOptionsText
)
374 printData
.SetPrinterOptions(WXSTRINGCAST printerOptionsText
->GetValue());
376 printData
.SetColour(colourCheckBox
->GetValue());
377 if (orientationRadioBox
)
379 int sel
= orientationRadioBox
->GetSelection();
381 printData
.SetPrinterOrientation(PS_PORTRAIT
);
383 printData
.SetPrinterOrientation(PS_LANDSCAPE
);
387 wxString
val(paperTypeChoice
->GetStringSelection());
388 if (!val
.IsNull() && val
!= "")
389 printData
.SetPaperName((char *)(const char *)val
);
394 wxChoice
*wxGenericPrintSetupDialog
::CreatePaperTypeChoice(int *x
, int *y
)
396 if (!wxThePrintPaperDatabase
)
398 wxThePrintPaperDatabase
= new wxPrintPaperDatabase
;
399 wxThePrintPaperDatabase
->CreateDatabase();
401 int n
= wxThePrintPaperDatabase
->Number();
402 wxString
*choices
= new wxString
[n
];
405 for (i
= 0; i
< n
; i
++)
407 wxPrintPaperType
*paper
= (wxPrintPaperType
*)wxThePrintPaperDatabase
->Nth(i
)->Data();
408 choices
[i
] = paper
->pageName
;
409 if (printData
.GetPaperName() && choices
[i
] == printData
.GetPaperName())
413 wxChoice
*choice
= new wxChoice(this, wxPRINTID_PAPERSIZE
, wxPoint(*x
, *y
), wxSize(170, -1), n
,
418 choice
->SetSelection(sel
);
423 * Generic page setup dialog
426 void wxGenericPageSetupDialog
::OnPrinter(wxCommandEvent
& WXUNUSED(event
))
428 if (wxTheApp
->GetPrintMode() == wxPRINT_POSTSCRIPT
)
430 wxGenericPrintSetupDialog
*genericPrintSetupDialog
=
431 new wxGenericPrintSetupDialog(this, wxThePrintSetupData
);
432 int ret
= genericPrintSetupDialog
->ShowModal();
434 *wxThePrintSetupData
= genericPrintSetupDialog
->GetPrintData();
436 genericPrintSetupDialog
->Close(TRUE
);
442 data
.SetSetupDialog(TRUE
);
443 wxPrintDialog
printDialog(this, & data
);
444 printDialog
.Show(TRUE
);
449 wxGenericPageSetupDialog
::wxGenericPageSetupDialog(wxWindow
*parent
, wxPageSetupData
* data
):
450 wxDialog(parent
, -1, _("Page Setup"), wxPoint(0, 0), wxSize(600, 600), wxDIALOG_MODAL
|wxDEFAULT_DIALOG_STYLE
)
455 int buttonWidth
= 75;
456 int buttonHeight
= 25;
461 wxButton
*okButton
= new wxButton(this, wxID_OK
, _("OK"), wxPoint(5, yPos
), wxSize(buttonWidth
, buttonHeight
));
462 (void) new wxButton(this, wxID_CANCEL
, _("Cancel"), wxPoint(buttonWidth
+ 5 + spacing
, yPos
), wxSize(buttonWidth
, buttonHeight
));
464 printerButton
= new wxButton(this, wxPRINTID_SETUP
, _("Printer..."), wxPoint(buttonWidth
*2 + 5 + 2*spacing
, yPos
), wxSize(buttonWidth
, buttonHeight
));
466 if ( !pageData
.GetEnablePrinter() )
467 printerButton
->Enable(FALSE
);
469 // if (printData.GetEnableHelp())
470 // wxButton *helpButton = new wxButton(this, (wxFunction)wxGenericPageSetupHelpProc, _("Help"), -1, -1, buttonWidth, buttonHeight);
472 okButton
->SetDefault();
473 okButton
->SetFocus();
478 paperTypeChoice
= CreatePaperTypeChoice(&xPos
, &yPos
);
482 wxString
*choices
= new wxString
[2];
483 choices
[0] = _("Portrait");
484 choices
[1] = _("Landscape");
485 orientationRadioBox
= new wxRadioBox(this, wxPRINTID_ORIENTATION
, _("Orientation"),
486 wxPoint(xPos
, yPos
), wxSize(-1, -1), 2, choices
, 2);
487 orientationRadioBox
->SetSelection(0);
492 int staticWidth
= 110;
496 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Left margin (mm):"), wxPoint(xPos
, yPos
));
499 marginLeftText
= new wxTextCtrl(this, wxPRINTID_LEFTMARGIN
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
500 xPos
+= textWidth
+ spacing
;
502 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Right margin (mm):"), wxPoint(xPos
, yPos
));
505 marginRightText
= new wxTextCtrl(this, wxPRINTID_RIGHTMARGIN
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
506 xPos
+= textWidth
+ spacing
;
511 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Top margin (mm):"), wxPoint(xPos
, yPos
));
514 marginTopText
= new wxTextCtrl(this, wxPRINTID_TOPMARGIN
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
515 xPos
+= textWidth
+ spacing
;
517 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Bottom margin (mm):"), wxPoint(xPos
, yPos
));
520 marginBottomText
= new wxTextCtrl(this, wxPRINTID_BOTTOMMARGIN
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
529 wxGenericPageSetupDialog
::~wxGenericPageSetupDialog(void)
533 bool wxGenericPageSetupDialog
::TransferDataToWindow(void)
536 marginLeftText
->SetValue(IntToString((int) pageData
.GetMarginTopLeft().x
));
538 marginTopText
->SetValue(IntToString((int) pageData
.GetMarginTopLeft().y
));
540 marginRightText
->SetValue(IntToString((int) pageData
.GetMarginBottomRight().x
));
541 if (marginBottomText
)
542 marginBottomText
->SetValue(IntToString((int) pageData
.GetMarginBottomRight().y
));
544 if (orientationRadioBox
)
546 if (pageData
.GetOrientation() == wxPORTRAIT
)
547 orientationRadioBox
->SetSelection(0);
549 orientationRadioBox
->SetSelection(1);
554 bool wxGenericPageSetupDialog
::TransferDataFromWindow(void)
556 if (marginLeftText
&& marginTopText
)
557 pageData
.SetMarginTopLeft(wxPoint(atoi((const char *)marginLeftText
->GetValue()),atoi((const char *)marginTopText
->GetValue())));
558 if (marginRightText
&& marginBottomText
)
559 pageData
.SetMarginBottomRight(wxPoint(atoi((const char *)marginRightText
->GetValue()),atoi((const char *)marginBottomText
->GetValue())));
561 if (orientationRadioBox
)
563 int sel
= orientationRadioBox
->GetSelection();
566 wxThePrintSetupData
->SetPrinterOrientation(wxPORTRAIT
);
567 pageData
.SetOrientation(wxPORTRAIT
);
571 wxThePrintSetupData
->SetPrinterOrientation(wxLANDSCAPE
);
572 pageData
.SetOrientation(wxLANDSCAPE
);
577 wxString
val(paperTypeChoice
->GetStringSelection());
578 if (!val
.IsNull() && val
!= "")
580 wxPrintPaperType
* paper
= wxThePrintPaperDatabase
->FindPaperType((char*) (const char *)val
);
583 pageData
.SetPaperSize(wxPoint(paper
->widthMM
, paper
->heightMM
));
590 wxChoice
*wxGenericPageSetupDialog
::CreatePaperTypeChoice(int *x
, int *y
)
592 if (!wxThePrintPaperDatabase
)
594 wxThePrintPaperDatabase
= new wxPrintPaperDatabase
;
595 wxThePrintPaperDatabase
->CreateDatabase();
597 int n
= wxThePrintPaperDatabase
->Number();
598 wxString
*choices
= new wxString
[n
];
601 for (i
= 0; i
< n
; i
++)
603 wxPrintPaperType
*paper
= (wxPrintPaperType
*)wxThePrintPaperDatabase
->Nth(i
)->Data();
604 choices
[i
] = paper
->pageName
;
605 if (pageData
.GetPaperSize().x
== paper
->widthMM
&& pageData
.GetPaperSize().y
== paper
->heightMM
)
609 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Paper size"), wxPoint(*x
, *y
));
612 wxChoice
*choice
= new wxChoice(this, wxPRINTID_PAPERSIZE
, wxPoint(*x
, *y
), wxSize(300, -1), n
,
617 choice
->SetSelection(sel
);