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"
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
)
77 int buttonHeight
= 25;
82 wxButton
*okButton
= new wxButton(this, wxID_OK
, "OK", wxPoint(5, yPos
), wxSize(buttonWidth
, buttonHeight
));
83 (void) new wxButton(this, wxID_CANCEL
, "Cancel", wxPoint(buttonWidth
+ 5 + spacing
, yPos
), wxSize(buttonWidth
, buttonHeight
));
85 setupButton
= new wxButton(this, wxPRINTID_SETUP
, "Setup...", wxPoint(buttonWidth
*2 + 5 + 2*spacing
, yPos
), wxSize(buttonWidth
, buttonHeight
));
87 okButton
->SetDefault();
96 rangeRadioBox
= new wxRadioBox(this, wxPRINTID_RANGE
, "Print Range",
97 wxPoint(5, yPos
), wxSize(-1, -1), 2, choices
, 2);
98 rangeRadioBox
->SetSelection(1);
102 int staticWidth
= 45;
106 (void) new wxStaticText(this, wxPRINTID_STATIC
, "From:", wxPoint(xPos
, yPos
));
109 fromText
= new wxTextCtrl(this, wxPRINTID_FROM
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
110 xPos
+= spacing
+ textWidth
;
112 (void) new wxStaticText(this, wxPRINTID_STATIC
, "To:", wxPoint(xPos
, yPos
));
115 toText
= new wxTextCtrl(this, wxPRINTID_TO
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
116 xPos
+= spacing
+ textWidth
;
118 (void) new wxStaticText(this, wxPRINTID_STATIC
, "Copies:", wxPoint(xPos
, yPos
));
119 xPos
+= spacing
+ staticWidth
;
121 noCopiesText
= new wxTextCtrl(this, wxPRINTID_COPIES
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
126 printToFileCheckBox
= new wxCheckBox(this, wxPRINTID_PRINTTOFILE
, "Print to File", wxPoint(xPos
, yPos
));
131 // Calls wxWindow::OnInitDialog and then wxGenericPrintDialog::TransferDataToWindow
135 int wxGenericPrintDialog::ShowModal(void)
137 if ( printData
.GetSetupDialog() )
139 wxGenericPrintSetupDialog
*genericPrintSetupDialog
=
140 new wxGenericPrintSetupDialog(GetParent(), wxThePrintSetupData
);
141 int ret
= genericPrintSetupDialog
->ShowModal();
142 if ( ret
!= wxID_CANCEL
)
144 *wxThePrintSetupData
= genericPrintSetupDialog
->printData
;
146 genericPrintSetupDialog
->Close(TRUE
);
151 return wxDialog::ShowModal();
155 wxGenericPrintDialog::~wxGenericPrintDialog(void)
159 void wxGenericPrintDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
161 TransferDataFromWindow();
163 // There are some interactions between the global setup data
164 // and the standard print dialog. The global printing 'mode'
165 // is determined by whether the user checks Print to file
167 if (printData
.GetPrintToFile())
169 wxThePrintSetupData
->SetPrinterMode(PS_FILE
);
171 char *f
= wxFileSelector("PostScript file",
172 wxPathOnly(wxThePrintSetupData
->GetPrinterFile()),
173 wxFileNameFromPath(wxThePrintSetupData
->GetPrinterFile()),
174 "ps", "*.ps", 0, this);
176 wxThePrintSetupData
->SetPrinterFile(f
);
181 wxThePrintSetupData
->SetPrinterMode(PS_PRINTER
);
186 void wxGenericPrintDialog::OnRange(wxCommandEvent
& event
)
188 if (event
.GetInt() == 0)
190 fromText
->Enable(FALSE
);
191 toText
->Enable(FALSE
);
193 else if (event
.GetInt() == 1)
195 fromText
->Enable(TRUE
);
196 toText
->Enable(TRUE
);
200 void wxGenericPrintDialog::OnSetup(wxCommandEvent
& WXUNUSED(event
))
202 wxGenericPrintSetupDialog
*genericPrintSetupDialog
=
203 new wxGenericPrintSetupDialog(this, wxThePrintSetupData
);
204 int ret
= genericPrintSetupDialog
->ShowModal();
205 if ( ret
!= wxID_CANCEL
)
207 *wxThePrintSetupData
= genericPrintSetupDialog
->printData
;
208 printData
.SetOrientation(wxThePrintSetupData
->GetPrinterOrientation());
211 genericPrintSetupDialog
->Close(TRUE
);
214 bool wxGenericPrintDialog::TransferDataToWindow(void)
217 if (printData
.GetEnablePageNumbers())
219 fromText
->Enable(TRUE
);
220 toText
->Enable(TRUE
);
222 sprintf(buf
, "%d", printData
.GetFromPage());
223 fromText
->SetValue(buf
);
224 sprintf(buf
, "%d", printData
.GetToPage());
225 toText
->SetValue(buf
);
227 if (printData
.GetAllPages())
228 rangeRadioBox
->SetSelection(0);
230 rangeRadioBox
->SetSelection(1);
234 fromText
->Enable(FALSE
);
235 toText
->Enable(FALSE
);
236 rangeRadioBox
->SetSelection(0);
237 rangeRadioBox
->wxRadioBox::Enable(1, FALSE
);
239 sprintf(buf
, "%d", printData
.GetNoCopies());
240 noCopiesText
->SetValue(buf
);
242 printToFileCheckBox
->SetValue(printData
.GetPrintToFile());
243 printToFileCheckBox
->Enable(printData
.GetEnablePrintToFile());
247 bool wxGenericPrintDialog::TransferDataFromWindow(void)
249 if (printData
.GetEnablePageNumbers())
251 printData
.SetFromPage(atoi(fromText
->GetValue()));
252 printData
.SetToPage(atoi(toText
->GetValue()));
254 if (rangeRadioBox
->GetSelection() == 0)
255 printData
.SetAllPages(TRUE
);
257 printData
.SetAllPages(FALSE
);
258 printData
.SetNoCopies(atoi(noCopiesText
->GetValue()));
259 printData
.SetPrintToFile(printToFileCheckBox
->GetValue());
264 wxDC
*wxGenericPrintDialog::GetPrintDC(void)
266 return new wxPostScriptDC(wxThePrintSetupData
->GetPrinterFile(), FALSE
, NULL
);
270 * Generic print setup dialog
273 wxGenericPrintSetupDialog::wxGenericPrintSetupDialog(wxWindow
*parent
, wxPrintSetupData
* data
):
274 wxDialog(parent
, -1, "Print Setup", wxPoint(0, 0), wxSize(600, 600), wxDEFAULT_DIALOG_STYLE
|wxDIALOG_MODAL
)
279 int buttonWidth
= 65;
280 int buttonHeight
= 25;
285 wxButton
*okButton
= new wxButton(this, wxID_OK
, "OK", wxPoint(xPos
, yPos
), wxSize(buttonWidth
, buttonHeight
));
286 xPos
+= buttonWidth
+ spacing
;
287 (void) new wxButton(this, wxID_CANCEL
, "Cancel", wxPoint(xPos
, yPos
), wxSize(buttonWidth
, buttonHeight
));
289 okButton
->SetDefault();
290 okButton
->SetFocus();
295 paperTypeChoice
= CreatePaperTypeChoice(&xPos
, &yPos
);
298 choices
[0] = "Portrait";
299 choices
[1] = "Landscape";
301 orientationRadioBox
= new wxRadioBox(this, wxPRINTID_ORIENTATION
, "Orientation",
302 wxPoint(xPos
, yPos
), wxSize(-1, -1), 2, choices
, 2);
303 orientationRadioBox
->SetSelection(0);
307 colourCheckBox
= new wxCheckBox(this, wxPRINTID_PRINTCOLOUR
, "Print in colour", wxPoint(xPos
, yPos
));
312 int staticWidth
= 100;
316 (void) new wxStaticText(this, wxPRINTID_STATIC
, "Printer command:", wxPoint(xPos
, yPos
));
319 printerCommandText
= new wxTextCtrl(this, wxPRINTID_COMMAND
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
320 xPos
+= textWidth
+ spacing
;
322 (void) new wxStaticText(this, wxPRINTID_STATIC
, "Printer options:", wxPoint(xPos
, yPos
));
325 printerOptionsText
= new wxTextCtrl(this, wxPRINTID_OPTIONS
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
333 wxGenericPrintSetupDialog::~wxGenericPrintSetupDialog(void)
337 bool wxGenericPrintSetupDialog::TransferDataToWindow(void)
339 if (printerCommandText
&& printData
.GetPrinterCommand())
340 printerCommandText
->SetValue(printData
.GetPrinterCommand());
341 if (printerOptionsText
&& printData
.GetPrinterOptions())
342 printerOptionsText
->SetValue(printData
.GetPrinterOptions());
344 colourCheckBox
->SetValue(printData
.GetColour());
346 if (orientationRadioBox
)
348 if (printData
.GetPrinterOrientation() == PS_PORTRAIT
)
349 orientationRadioBox
->SetSelection(0);
351 orientationRadioBox
->SetSelection(1);
356 bool wxGenericPrintSetupDialog::TransferDataFromWindow(void)
358 if (printerCommandText
)
359 printData
.SetPrinterCommand(WXSTRINGCAST printerCommandText
->GetValue());
360 if (printerOptionsText
)
361 printData
.SetPrinterOptions(WXSTRINGCAST printerOptionsText
->GetValue());
363 printData
.SetColour(colourCheckBox
->GetValue());
364 if (orientationRadioBox
)
366 int sel
= orientationRadioBox
->GetSelection();
368 printData
.SetPrinterOrientation(PS_PORTRAIT
);
370 printData
.SetPrinterOrientation(PS_LANDSCAPE
);
374 wxString
val(paperTypeChoice
->GetStringSelection());
375 if (!val
.IsNull() && val
!= "")
376 printData
.SetPaperName((char *)(const char *)val
);
381 wxChoice
*wxGenericPrintSetupDialog::CreatePaperTypeChoice(int *x
, int *y
)
383 if (!wxThePrintPaperDatabase
)
385 wxThePrintPaperDatabase
= new wxPrintPaperDatabase
;
386 wxThePrintPaperDatabase
->CreateDatabase();
388 int n
= wxThePrintPaperDatabase
->Number();
389 wxString
*choices
= new wxString
[n
];
392 for (i
= 0; i
< n
; i
++)
394 wxPrintPaperType
*paper
= (wxPrintPaperType
*)wxThePrintPaperDatabase
->Nth(i
)->Data();
395 choices
[i
] = paper
->pageName
;
396 if (printData
.GetPaperName() && choices
[i
] == printData
.GetPaperName())
400 (void) new wxStaticText(this, wxPRINTID_STATIC
, "Paper size", wxPoint(*x
, *y
));
403 wxChoice
*choice
= new wxChoice(this, wxPRINTID_PAPERSIZE
, wxPoint(*x
, *y
), wxSize(300, -1), n
,
408 choice
->SetSelection(sel
);
413 * Generic page setup dialog
416 void wxGenericPageSetupDialog::OnPrinter(wxCommandEvent
& WXUNUSED(event
))
418 if (wxTheApp
->GetPrintMode() == wxPRINT_POSTSCRIPT
)
420 wxGenericPrintSetupDialog
*genericPrintSetupDialog
=
421 new wxGenericPrintSetupDialog(this, wxThePrintSetupData
);
422 int ret
= genericPrintSetupDialog
->ShowModal();
424 *wxThePrintSetupData
= genericPrintSetupDialog
->GetPrintData();
426 genericPrintSetupDialog
->Close(TRUE
);
432 data
.SetSetupDialog(TRUE
);
433 wxPrintDialog
printDialog(this, & data
);
434 printDialog
.Show(TRUE
);
439 wxGenericPageSetupDialog::wxGenericPageSetupDialog(wxWindow
*parent
, wxPageSetupData
* data
):
440 wxDialog(parent
, -1, "Page Setup", wxPoint(0, 0), wxSize(600, 600), wxDIALOG_MODAL
|wxDEFAULT_DIALOG_STYLE
)
445 int buttonWidth
= 75;
446 int buttonHeight
= 25;
451 wxButton
*okButton
= new wxButton(this, wxID_OK
, "OK", wxPoint(5, yPos
), wxSize(buttonWidth
, buttonHeight
));
452 (void) new wxButton(this, wxID_CANCEL
, "Cancel", wxPoint(buttonWidth
+ 5 + spacing
, yPos
), wxSize(buttonWidth
, buttonHeight
));
454 printerButton
= new wxButton(this, wxPRINTID_SETUP
, "Printer...", wxPoint(buttonWidth
*2 + 5 + 2*spacing
, yPos
), wxSize(buttonWidth
, buttonHeight
));
456 if ( !pageData
.GetEnablePrinter() )
457 printerButton
->Enable(FALSE
);
459 // if (printData.GetEnableHelp())
460 // wxButton *helpButton = new wxButton(this, (wxFunction)wxGenericPageSetupHelpProc, "Help", -1, -1, buttonWidth, buttonHeight);
462 okButton
->SetDefault();
463 okButton
->SetFocus();
468 paperTypeChoice
= CreatePaperTypeChoice(&xPos
, &yPos
);
473 choices
[0] = "Portrait";
474 choices
[1] = "Landscape";
475 orientationRadioBox
= new wxRadioBox(this, wxPRINTID_ORIENTATION
, "Orientation",
476 wxPoint(xPos
, yPos
), wxSize(-1, -1), 2, choices
, 2);
477 orientationRadioBox
->SetSelection(0);
482 int staticWidth
= 110;
486 (void) new wxStaticText(this, wxPRINTID_STATIC
, "Left margin (mm):", wxPoint(xPos
, yPos
));
489 marginLeftText
= new wxTextCtrl(this, wxPRINTID_LEFTMARGIN
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
490 xPos
+= textWidth
+ spacing
;
492 (void) new wxStaticText(this, wxPRINTID_STATIC
, "Right margin (mm):", wxPoint(xPos
, yPos
));
495 marginRightText
= new wxTextCtrl(this, wxPRINTID_RIGHTMARGIN
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
496 xPos
+= textWidth
+ spacing
;
501 (void) new wxStaticText(this, wxPRINTID_STATIC
, "Top margin (mm):", wxPoint(xPos
, yPos
));
504 marginTopText
= new wxTextCtrl(this, wxPRINTID_TOPMARGIN
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
505 xPos
+= textWidth
+ spacing
;
507 (void) new wxStaticText(this, wxPRINTID_STATIC
, "Bottom margin (mm):", wxPoint(xPos
, yPos
));
510 marginBottomText
= new wxTextCtrl(this, wxPRINTID_BOTTOMMARGIN
, "", wxPoint(xPos
, yPos
), wxSize(textWidth
, -1));
518 wxGenericPageSetupDialog::~wxGenericPageSetupDialog(void)
522 bool wxGenericPageSetupDialog::TransferDataToWindow(void)
525 marginLeftText
->SetValue(IntToString((int) pageData
.GetMarginTopLeft().x
));
527 marginTopText
->SetValue(IntToString((int) pageData
.GetMarginTopLeft().y
));
529 marginRightText
->SetValue(IntToString((int) pageData
.GetMarginBottomRight().x
));
530 if (marginBottomText
)
531 marginBottomText
->SetValue(IntToString((int) pageData
.GetMarginBottomRight().y
));
533 if (orientationRadioBox
)
535 if (pageData
.GetOrientation() == wxPORTRAIT
)
536 orientationRadioBox
->SetSelection(0);
538 orientationRadioBox
->SetSelection(1);
543 bool wxGenericPageSetupDialog::TransferDataFromWindow(void)
545 if (marginLeftText
&& marginTopText
)
546 pageData
.SetMarginTopLeft(wxPoint(atoi((const char *)marginLeftText
->GetValue()),atoi((const char *)marginTopText
->GetValue())));
547 if (marginRightText
&& marginBottomText
)
548 pageData
.SetMarginBottomRight(wxPoint(atoi((const char *)marginRightText
->GetValue()),atoi((const char *)marginBottomText
->GetValue())));
550 if (orientationRadioBox
)
552 int sel
= orientationRadioBox
->GetSelection();
555 wxThePrintSetupData
->SetPrinterOrientation(wxPORTRAIT
);
556 pageData
.SetOrientation(wxPORTRAIT
);
560 wxThePrintSetupData
->SetPrinterOrientation(wxLANDSCAPE
);
561 pageData
.SetOrientation(wxLANDSCAPE
);
566 wxString
val(paperTypeChoice
->GetStringSelection());
567 if (!val
.IsNull() && val
!= "")
569 wxPrintPaperType
* paper
= wxThePrintPaperDatabase
->FindPaperType((char*) (const char *)val
);
572 pageData
.SetPaperSize(wxPoint(paper
->widthMM
, paper
->heightMM
));
579 wxChoice
*wxGenericPageSetupDialog::CreatePaperTypeChoice(int *x
, int *y
)
581 if (!wxThePrintPaperDatabase
)
583 wxThePrintPaperDatabase
= new wxPrintPaperDatabase
;
584 wxThePrintPaperDatabase
->CreateDatabase();
586 int n
= wxThePrintPaperDatabase
->Number();
587 wxString
*choices
= new wxString
[n
];
590 for (i
= 0; i
< n
; i
++)
592 wxPrintPaperType
*paper
= (wxPrintPaperType
*)wxThePrintPaperDatabase
->Nth(i
)->Data();
593 choices
[i
] = paper
->pageName
;
594 if (pageData
.GetPaperSize().x
== paper
->widthMM
&& pageData
.GetPaperSize().y
== paper
->heightMM
)
598 (void) new wxStaticText(this, wxPRINTID_STATIC
, "Paper size", wxPoint(*x
, *y
));
601 wxChoice
*choice
= new wxChoice(this, wxPRINTID_PAPERSIZE
, wxPoint(*x
, *y
), wxSize(300, -1), n
,
606 choice
->SetSelection(sel
);