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/generic/dcpsg.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
)
77 (void)new wxStaticBox( this, -1, _( "Printer options" ), wxPoint( 5, 5), wxSize( 300, 60 ) );
79 printToFileCheckBox
= new wxCheckBox(this, wxPRINTID_PRINTTOFILE
, _("Print to File"), wxPoint(20, 25) );
81 setupButton
= new wxButton(this, wxPRINTID_SETUP
, _("Setup..."), wxPoint(160, 25), wxSize(100, -1));
83 wxString
*choices
= new wxString
[2];
84 choices
[0] = _("All");
85 choices
[1] = _("Pages");
87 fromText
= (wxTextCtrl
*)NULL
;
89 if(printData
.GetFromPage() != 0)
91 rangeRadioBox
= new wxRadioBox(this, wxPRINTID_RANGE
, _("Print Range"),
92 wxPoint(5, 80), wxSize(-1, -1), 2, choices
, 1, wxRA_VERTICAL
);
93 rangeRadioBox
->SetSelection(1);
96 if(printData
.GetFromPage() != 0)
98 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("From:"), wxPoint(5, 135));
100 fromText
= new wxTextCtrl(this, wxPRINTID_FROM
, "", wxPoint(45, 130), wxSize(40, -1));
102 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("To:"), wxPoint(100, 135));
104 toText
= new wxTextCtrl(this, wxPRINTID_TO
, "", wxPoint(133, 130), wxSize(40, -1));
107 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Copies:"), wxPoint(200, 135));
109 noCopiesText
= new wxTextCtrl(this, wxPRINTID_COPIES
, "", wxPoint(252, 130), wxSize(40, -1));
111 wxButton
*okButton
= new wxButton(this, wxID_OK
, _("OK"), wxPoint(40, 180), wxSize(100, -1));
112 (void) new wxButton(this, wxID_CANCEL
, _("Cancel"), wxPoint(180, 180), wxSize(100, -1));
114 okButton
->SetDefault();
115 okButton
->SetFocus();
119 // Calls wxWindow::OnInitDialog and then wxGenericPrintDialog::TransferDataToWindow
124 int wxGenericPrintDialog::ShowModal(void)
126 if ( printData
.GetSetupDialog() )
128 wxGenericPrintSetupDialog
*genericPrintSetupDialog
=
129 new wxGenericPrintSetupDialog(GetParent(), wxThePrintSetupData
);
130 int ret
= genericPrintSetupDialog
->ShowModal();
131 if ( ret
!= wxID_CANCEL
)
133 *wxThePrintSetupData
= genericPrintSetupDialog
->printData
;
135 genericPrintSetupDialog
->Close(TRUE
);
140 return wxDialog::ShowModal();
144 wxGenericPrintDialog::~wxGenericPrintDialog(void)
148 void wxGenericPrintDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
150 TransferDataFromWindow();
152 // There are some interactions between the global setup data
153 // and the standard print dialog. The global printing 'mode'
154 // is determined by whether the user checks Print to file
156 if (printData
.GetPrintToFile())
158 wxThePrintSetupData
->SetPrinterMode(PS_FILE
);
160 wxString f
= wxFileSelector(_("PostScript file"),
161 wxPathOnly(wxThePrintSetupData
->GetPrinterFile()),
162 wxFileNameFromPath(wxThePrintSetupData
->GetPrinterFile()),
163 "ps", "*.ps", 0, this);
167 wxThePrintSetupData
->SetPrinterFile(f
);
170 wxThePrintSetupData
->SetPrinterMode(PS_PRINTER
);
175 void wxGenericPrintDialog::OnRange(wxCommandEvent
& event
)
177 if (!fromText
) return;
179 if (event
.GetInt() == 0)
181 fromText
->Enable(FALSE
);
182 toText
->Enable(FALSE
);
184 else if (event
.GetInt() == 1)
186 fromText
->Enable(TRUE
);
187 toText
->Enable(TRUE
);
191 void wxGenericPrintDialog::OnSetup(wxCommandEvent
& WXUNUSED(event
))
193 wxGenericPrintSetupDialog
*genericPrintSetupDialog
=
194 new wxGenericPrintSetupDialog(this, wxThePrintSetupData
);
195 int ret
= genericPrintSetupDialog
->ShowModal();
196 if ( ret
!= wxID_CANCEL
)
198 *wxThePrintSetupData
= genericPrintSetupDialog
->printData
;
199 printData
.SetOrientation(wxThePrintSetupData
->GetPrinterOrientation());
202 genericPrintSetupDialog
->Close(TRUE
);
205 bool wxGenericPrintDialog::TransferDataToWindow(void)
209 if(printData
.GetFromPage() != 0)
211 if (printData
.GetEnablePageNumbers())
213 fromText
->Enable(TRUE
);
214 toText
->Enable(TRUE
);
216 sprintf(buf
, "%d", printData
.GetFromPage());
217 fromText
->SetValue(buf
);
218 sprintf(buf
, "%d", printData
.GetToPage());
219 toText
->SetValue(buf
);
221 if (printData
.GetAllPages())
222 rangeRadioBox
->SetSelection(0);
224 rangeRadioBox
->SetSelection(1);
228 fromText
->Enable(FALSE
);
229 toText
->Enable(FALSE
);
230 rangeRadioBox
->SetSelection(0);
231 rangeRadioBox
->wxRadioBox::Enable(1, FALSE
);
234 sprintf(buf
, "%d", printData
.GetNoCopies());
235 noCopiesText
->SetValue(buf
);
237 printToFileCheckBox
->SetValue(printData
.GetPrintToFile());
238 printToFileCheckBox
->Enable(printData
.GetEnablePrintToFile());
242 bool wxGenericPrintDialog::TransferDataFromWindow(void)
244 if(printData
.GetFromPage() != -1)
246 if (printData
.GetEnablePageNumbers())
248 printData
.SetFromPage(atoi(fromText
->GetValue()));
249 printData
.SetToPage(atoi(toText
->GetValue()));
251 if (rangeRadioBox
->GetSelection() == 0)
252 printData
.SetAllPages(TRUE
);
254 printData
.SetAllPages(FALSE
);
257 { // continuous printing
258 printData
.SetFromPage(1);
259 printData
.SetToPage(32000);
261 printData
.SetNoCopies(atoi(noCopiesText
->GetValue()));
262 printData
.SetPrintToFile(printToFileCheckBox
->GetValue());
267 wxDC
*wxGenericPrintDialog::GetPrintDC(void)
269 return new wxPostScriptDC(wxThePrintSetupData
->GetPrinterFile(), FALSE
, (wxWindow
*) NULL
);
273 * Generic print setup dialog
276 wxGenericPrintSetupDialog::wxGenericPrintSetupDialog(wxWindow
*parent
, wxPrintSetupData
* data
):
277 wxDialog(parent
, -1, _("Print Setup"), wxPoint(0, 0), wxSize(600, 600), wxDEFAULT_DIALOG_STYLE
|wxDIALOG_MODAL
)
282 (void) new wxStaticBox(this, wxPRINTID_STATIC
, _("Paper size"), wxPoint(10, 10), wxSize(200,60) );
286 paperTypeChoice
= CreatePaperTypeChoice(&xPos
, &yPos
);
288 wxString
*choices
= new wxString
[2];
289 choices
[0] = _("Portrait");
290 choices
[1] = _("Landscape");
292 orientationRadioBox
= new wxRadioBox(this, wxPRINTID_ORIENTATION
, _("Orientation"),
293 wxPoint(10, 80), wxSize(-1, -1), 2, choices
, 1, wxRA_VERTICAL
);
294 orientationRadioBox
->SetSelection(0);
296 (void) new wxStaticBox(this, wxPRINTID_STATIC
, _("Options"), wxPoint(10, 130), wxSize(200,50) );
298 int colourXPos
= 145;
304 colourCheckBox
= new wxCheckBox(this, wxPRINTID_PRINTCOLOUR
, _("Print in colour"), wxPoint(15, colourXPos
));
307 (void) new wxStaticBox(this, wxPRINTID_STATIC
, _("Print spooling"), wxPoint(230, 10), wxSize(200,170) );
309 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Printer command:"), wxPoint(240, 30));
311 printerCommandText
= new wxTextCtrl(this, wxPRINTID_COMMAND
, "", wxPoint(260, 55), wxSize(150, -1));
313 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Printer options:"), wxPoint(240, 110));
315 printerOptionsText
= new wxTextCtrl(this, wxPRINTID_OPTIONS
, "", wxPoint(260, 135), wxSize(150, -1));
317 wxButton
*okButton
= new wxButton(this, wxID_OK
, _("OK"), wxPoint(80, 200), wxSize(100, -1));
318 (void) new wxButton(this, wxID_CANCEL
, _("Cancel"), wxPoint(270, 200), wxSize(100, -1));
320 okButton
->SetDefault();
321 okButton
->SetFocus();
330 wxGenericPrintSetupDialog::~wxGenericPrintSetupDialog(void)
334 bool wxGenericPrintSetupDialog::TransferDataToWindow(void)
336 if (printerCommandText
&& printData
.GetPrinterCommand())
337 printerCommandText
->SetValue(printData
.GetPrinterCommand());
338 if (printerOptionsText
&& printData
.GetPrinterOptions())
339 printerOptionsText
->SetValue(printData
.GetPrinterOptions());
341 colourCheckBox
->SetValue(printData
.GetColour());
343 if (orientationRadioBox
)
345 if (printData
.GetPrinterOrientation() == PS_PORTRAIT
)
346 orientationRadioBox
->SetSelection(0);
348 orientationRadioBox
->SetSelection(1);
353 bool wxGenericPrintSetupDialog::TransferDataFromWindow(void)
355 if (printerCommandText
)
356 printData
.SetPrinterCommand(WXSTRINGCAST printerCommandText
->GetValue());
357 if (printerOptionsText
)
358 printData
.SetPrinterOptions(WXSTRINGCAST printerOptionsText
->GetValue());
360 printData
.SetColour(colourCheckBox
->GetValue());
361 if (orientationRadioBox
)
363 int sel
= orientationRadioBox
->GetSelection();
365 printData
.SetPrinterOrientation(PS_PORTRAIT
);
367 printData
.SetPrinterOrientation(PS_LANDSCAPE
);
371 wxString
val(paperTypeChoice
->GetStringSelection());
372 if (!val
.IsNull() && val
!= "")
373 printData
.SetPaperName((char *)(const char *)val
);
375 *wxThePrintSetupData
= GetPrintData();
379 wxChoice
*wxGenericPrintSetupDialog::CreatePaperTypeChoice(int *x
, int *y
)
381 if (!wxThePrintPaperDatabase
)
383 wxThePrintPaperDatabase
= new wxPrintPaperDatabase
;
384 wxThePrintPaperDatabase
->CreateDatabase();
386 int n
= wxThePrintPaperDatabase
->Number();
387 wxString
*choices
= new wxString
[n
];
390 for (i
= 0; i
< n
; i
++)
392 wxPrintPaperType
*paper
= (wxPrintPaperType
*)wxThePrintPaperDatabase
->Nth(i
)->Data();
393 choices
[i
] = paper
->pageName
;
394 if (printData
.GetPaperName() && choices
[i
] == printData
.GetPaperName())
403 wxChoice
*choice
= new wxChoice(this, wxPRINTID_PAPERSIZE
, wxPoint(*x
, *y
), wxSize(width
, -1), n
,
408 choice
->SetSelection(sel
);
413 * Generic page setup dialog
416 void wxGenericPageSetupDialog::OnPrinter(wxCommandEvent
& WXUNUSED(event
))
418 // We no longer query GetPrintMode, so we can eliminate the need
419 // to call SetPrintMode.
420 // This has the limitation that we can't explicitly call the PostScript
421 // print setup dialog from the generic Page Setup dialog under Windows,
422 // but since this choice would only happen when trying to do PostScript
423 // printing under Windows (and only in 16-bit Windows which
424 // doesn't have a Windows-specific page setup dialog) it's worth it.
427 data
.SetSetupDialog(TRUE
);
428 wxPrintDialog
*printDialog
= new wxPrintDialog(this, & data
);
429 printDialog
->ShowModal();
431 printDialog
->Destroy();
434 if (wxTheApp
->GetPrintMode() == wxPRINT_POSTSCRIPT
)
436 wxGenericPrintSetupDialog
*genericPrintSetupDialog
=
437 new wxGenericPrintSetupDialog(this, wxThePrintSetupData
);
438 int ret
= genericPrintSetupDialog
->ShowModal();
440 *wxThePrintSetupData
= genericPrintSetupDialog
->GetPrintData();
442 genericPrintSetupDialog
->Close(TRUE
);
448 data
.SetSetupDialog(TRUE
);
449 wxPrintDialog
printDialog(this, & data
);
450 printDialog
.ShowModal();
457 wxGenericPageSetupDialog::wxGenericPageSetupDialog(wxWindow
*parent
, wxPageSetupData
* data
):
458 wxDialog(parent
, -1, _("Page Setup"), wxPoint(0, 0), wxSize(600, 600), wxDIALOG_MODAL
|wxDEFAULT_DIALOG_STYLE
)
463 int buttonWidth
= 75;
464 int buttonHeight
= 25;
473 wxButton
*okButton
= new wxButton(this, wxID_OK
, _("OK"), wxPoint(5, yPos
), wxSize(buttonWidth
, buttonHeight
));
474 (void) new wxButton(this, wxID_CANCEL
, _("Cancel"), wxPoint(buttonWidth
+ 5 + spacing
, yPos
), wxSize(buttonWidth
, buttonHeight
));
476 printerButton
= new wxButton(this, wxPRINTID_SETUP
, _("Printer..."), wxPoint(buttonWidth
*2 + 5 + 2*spacing
, yPos
), wxSize(buttonWidth
, buttonHeight
));
478 if ( !pageData
.GetEnablePrinter() )
479 printerButton
->Enable(FALSE
);
481 // if (printData.GetEnableHelp())
482 // wxButton *helpButton = new wxButton(this, (wxFunction)wxGenericPageSetupHelpProc, _("Help"), -1, -1, buttonWidth, buttonHeight);
484 okButton
->SetDefault();
485 okButton
->SetFocus();
494 paperTypeChoice
= CreatePaperTypeChoice(&xPos
, &yPos
);
498 wxString
*choices
= new wxString
[2];
499 choices
[0] = _("Portrait");
500 choices
[1] = _("Landscape");
501 orientationRadioBox
= new wxRadioBox(this, wxPRINTID_ORIENTATION
, _("Orientation"),
502 wxPoint(xPos
, yPos
), wxSize(-1, -1), 2, choices
, 2);
503 orientationRadioBox
->SetSelection(0);
508 int staticWidth
= 110;
516 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Left margin (mm):"), wxPoint(xPos
, yPos
));
519 marginLeftText
= new wxTextCtrl(this, wxPRINTID_LEFTMARGIN
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
520 xPos
+= textWidth
+ spacing
;
522 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Right margin (mm):"), wxPoint(xPos
, yPos
));
525 marginRightText
= new wxTextCtrl(this, wxPRINTID_RIGHTMARGIN
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
526 xPos
+= textWidth
+ spacing
;
531 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Top margin (mm):"), wxPoint(xPos
, yPos
));
534 marginTopText
= new wxTextCtrl(this, wxPRINTID_TOPMARGIN
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
535 xPos
+= textWidth
+ spacing
;
537 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Bottom margin (mm):"), wxPoint(xPos
, yPos
));
540 marginBottomText
= new wxTextCtrl(this, wxPRINTID_BOTTOMMARGIN
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
549 wxGenericPageSetupDialog::~wxGenericPageSetupDialog(void)
553 bool wxGenericPageSetupDialog::TransferDataToWindow(void)
556 marginLeftText
->SetValue(IntToString((int) pageData
.GetMarginTopLeft().x
));
558 marginTopText
->SetValue(IntToString((int) pageData
.GetMarginTopLeft().y
));
560 marginRightText
->SetValue(IntToString((int) pageData
.GetMarginBottomRight().x
));
561 if (marginBottomText
)
562 marginBottomText
->SetValue(IntToString((int) pageData
.GetMarginBottomRight().y
));
564 if (orientationRadioBox
)
566 if (pageData
.GetOrientation() == wxPORTRAIT
)
567 orientationRadioBox
->SetSelection(0);
569 orientationRadioBox
->SetSelection(1);
574 bool wxGenericPageSetupDialog::TransferDataFromWindow(void)
576 if (marginLeftText
&& marginTopText
)
577 pageData
.SetMarginTopLeft(wxPoint(atoi((const char *)marginLeftText
->GetValue()),atoi((const char *)marginTopText
->GetValue())));
578 if (marginRightText
&& marginBottomText
)
579 pageData
.SetMarginBottomRight(wxPoint(atoi((const char *)marginRightText
->GetValue()),atoi((const char *)marginBottomText
->GetValue())));
581 if (orientationRadioBox
)
583 int sel
= orientationRadioBox
->GetSelection();
586 wxThePrintSetupData
->SetPrinterOrientation(wxPORTRAIT
);
587 pageData
.SetOrientation(wxPORTRAIT
);
591 wxThePrintSetupData
->SetPrinterOrientation(wxLANDSCAPE
);
592 pageData
.SetOrientation(wxLANDSCAPE
);
597 wxString
val(paperTypeChoice
->GetStringSelection());
598 if (!val
.IsNull() && val
!= "")
600 wxPrintPaperType
* paper
= wxThePrintPaperDatabase
->FindPaperType((char*) (const char *)val
);
603 pageData
.SetPaperSize(wxPoint(paper
->widthMM
, paper
->heightMM
));
611 wxChoice
*wxGenericPageSetupDialog::CreatePaperTypeChoice(int *x
, int *y
)
613 if (!wxThePrintPaperDatabase
)
615 wxThePrintPaperDatabase
= new wxPrintPaperDatabase
;
616 wxThePrintPaperDatabase
->CreateDatabase();
618 int n
= wxThePrintPaperDatabase
->Number();
619 wxString
*choices
= new wxString
[n
];
622 for (i
= 0; i
< n
; i
++)
624 wxPrintPaperType
*paper
= (wxPrintPaperType
*)wxThePrintPaperDatabase
->Nth(i
)->Data();
625 choices
[i
] = paper
->pageName
;
626 if (pageData
.GetPaperSize().x
== paper
->widthMM
&& pageData
.GetPaperSize().y
== paper
->heightMM
)
630 (void) new wxStaticText(this, wxPRINTID_STATIC
, _("Paper size"), wxPoint(*x
, *y
));
633 wxChoice
*choice
= new wxChoice(this, wxPRINTID_PAPERSIZE
, wxPoint(*x
, *y
), wxSize(300, -1), n
,
638 choice
->SetSelection(sel
);