Initial revision
[wxWidgets.git] / src / generic / prntdlgg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: prntdlgg.cpp
3 // Purpose: Generic print dialogs
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "prntdlgg.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #include "wx/defs.h"
24
25 #define WINDOWS_PRINTING (wxTheApp->GetPrintMode() == wxPRINT_WINDOWS)
26
27 #ifndef WX_PRECOMP
28 #include "wx/utils.h"
29 #include "wx/dc.h"
30 #include "wx/app.h"
31 #include "wx/frame.h"
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"
39 #endif
40
41 #include "wx/generic/prntdlgg.h"
42 #include "wx/printdlg.h"
43
44 #include <stdlib.h>
45 #include <string.h>
46
47 #if !USE_SHARED_LIBRARY
48 IMPLEMENT_CLASS(wxGenericPrintDialog, wxDialog)
49 IMPLEMENT_CLASS(wxGenericPrintSetupDialog, wxDialog)
50 IMPLEMENT_CLASS(wxGenericPageSetupDialog, wxDialog)
51
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)
56 END_EVENT_TABLE()
57
58 BEGIN_EVENT_TABLE(wxGenericPageSetupDialog, wxDialog)
59 EVT_BUTTON(wxPRINTID_SETUP, wxGenericPageSetupDialog::OnPrinter)
60 END_EVENT_TABLE()
61 #endif
62
63 extern wxPrintPaperDatabase *wxThePrintPaperDatabase;
64
65 /*
66 * Generic print dialog for non-Windows printing use.
67 */
68
69
70 wxGenericPrintDialog::wxGenericPrintDialog(wxWindow *parent, wxPrintData* data):
71 wxDialog(parent, -1, "Print", wxPoint(0, 0), wxSize(600, 600), wxDEFAULT_DIALOG_STYLE|wxDIALOG_MODAL)
72 {
73 if ( data )
74 printData = *data;
75
76 int buttonWidth = 65;
77 int buttonHeight = 25;
78 int spacing = 5;
79 int yPos = 5;
80 int xPos = 5;
81
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));
84
85 setupButton = new wxButton(this, wxPRINTID_SETUP, "Setup...", wxPoint(buttonWidth*2 + 5 + 2*spacing, yPos), wxSize(buttonWidth, buttonHeight));
86
87 okButton->SetDefault();
88 okButton->SetFocus();
89
90 yPos += 35;
91
92 wxString choices[2];
93 choices[0] = "All";
94 choices[1] = "Pages";
95
96 rangeRadioBox = new wxRadioBox(this, wxPRINTID_RANGE, "Print Range",
97 wxPoint(5, yPos), wxSize(-1, -1), 2, choices, 2);
98 rangeRadioBox->SetSelection(1);
99
100 yPos += 60;
101 xPos = 5;
102 int staticWidth = 45;
103 int textWidth = 40;
104 spacing = 10;
105
106 (void) new wxStaticText(this, wxPRINTID_STATIC, "From:", wxPoint(xPos, yPos));
107 xPos += staticWidth;
108
109 fromText = new wxTextCtrl(this, wxPRINTID_FROM, "", wxPoint(xPos, yPos), wxSize(textWidth, -1));
110 xPos += spacing + textWidth;
111
112 (void) new wxStaticText(this, wxPRINTID_STATIC, "To:", wxPoint(xPos, yPos));
113 xPos += staticWidth;
114
115 toText = new wxTextCtrl(this, wxPRINTID_TO, "", wxPoint(xPos, yPos), wxSize(textWidth, -1));
116 xPos += spacing + textWidth;
117
118 (void) new wxStaticText(this, wxPRINTID_STATIC, "Copies:", wxPoint(xPos, yPos));
119 xPos += spacing + staticWidth;
120
121 noCopiesText = new wxTextCtrl(this, wxPRINTID_COPIES, "", wxPoint(xPos, yPos), wxSize(textWidth, -1));
122
123 yPos += 30;
124 xPos = 5;
125
126 printToFileCheckBox = new wxCheckBox(this, wxPRINTID_PRINTTOFILE, "Print to File", wxPoint(xPos, yPos));
127
128 Fit();
129 Centre(wxBOTH);
130
131 // Calls wxWindow::OnInitDialog and then wxGenericPrintDialog::TransferDataToWindow
132 InitDialog();
133 }
134
135 int wxGenericPrintDialog::ShowModal(void)
136 {
137 if ( printData.GetSetupDialog() )
138 {
139 wxGenericPrintSetupDialog *genericPrintSetupDialog =
140 new wxGenericPrintSetupDialog(GetParent(), wxThePrintSetupData);
141 int ret = genericPrintSetupDialog->ShowModal();
142 if ( ret != wxID_CANCEL )
143 {
144 *wxThePrintSetupData = genericPrintSetupDialog->printData;
145 }
146 genericPrintSetupDialog->Close(TRUE);
147 return ret;
148 }
149 else
150 {
151 return wxDialog::ShowModal();
152 }
153 }
154
155 wxGenericPrintDialog::~wxGenericPrintDialog(void)
156 {
157 }
158
159 void wxGenericPrintDialog::OnOK(wxCommandEvent& WXUNUSED(event))
160 {
161 TransferDataFromWindow();
162
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
166 // or not.
167 if (printData.GetPrintToFile())
168 {
169 wxThePrintSetupData->SetPrinterMode(PS_FILE);
170
171 char *f = wxFileSelector("PostScript file",
172 wxPathOnly(wxThePrintSetupData->GetPrinterFile()),
173 wxFileNameFromPath(wxThePrintSetupData->GetPrinterFile()),
174 "ps", "*.ps", 0, this);
175 if (f)
176 wxThePrintSetupData->SetPrinterFile(f);
177 else
178 return;
179 }
180 else
181 wxThePrintSetupData->SetPrinterMode(PS_PRINTER);
182
183 EndModal(wxID_OK);
184 }
185
186 void wxGenericPrintDialog::OnRange(wxCommandEvent& event)
187 {
188 if (event.GetInt() == 0)
189 {
190 fromText->Enable(FALSE);
191 toText->Enable(FALSE);
192 }
193 else if (event.GetInt() == 1)
194 {
195 fromText->Enable(TRUE);
196 toText->Enable(TRUE);
197 }
198 }
199
200 void wxGenericPrintDialog::OnSetup(wxCommandEvent& WXUNUSED(event))
201 {
202 wxGenericPrintSetupDialog *genericPrintSetupDialog =
203 new wxGenericPrintSetupDialog(this, wxThePrintSetupData);
204 int ret = genericPrintSetupDialog->ShowModal();
205 if ( ret != wxID_CANCEL )
206 {
207 *wxThePrintSetupData = genericPrintSetupDialog->printData;
208 printData.SetOrientation(wxThePrintSetupData->GetPrinterOrientation());
209 }
210
211 genericPrintSetupDialog->Close(TRUE);
212 }
213
214 bool wxGenericPrintDialog::TransferDataToWindow(void)
215 {
216 char buf[10];
217 if (printData.GetEnablePageNumbers())
218 {
219 fromText->Enable(TRUE);
220 toText->Enable(TRUE);
221
222 sprintf(buf, "%d", printData.GetFromPage());
223 fromText->SetValue(buf);
224 sprintf(buf, "%d", printData.GetToPage());
225 toText->SetValue(buf);
226
227 if (printData.GetAllPages())
228 rangeRadioBox->SetSelection(0);
229 else
230 rangeRadioBox->SetSelection(1);
231 }
232 else
233 {
234 fromText->Enable(FALSE);
235 toText->Enable(FALSE);
236 rangeRadioBox->SetSelection(0);
237 rangeRadioBox->wxRadioBox::Enable(1, FALSE);
238 }
239 sprintf(buf, "%d", printData.GetNoCopies());
240 noCopiesText->SetValue(buf);
241
242 printToFileCheckBox->SetValue(printData.GetPrintToFile());
243 printToFileCheckBox->Enable(printData.GetEnablePrintToFile());
244 return TRUE;
245 }
246
247 bool wxGenericPrintDialog::TransferDataFromWindow(void)
248 {
249 if (printData.GetEnablePageNumbers())
250 {
251 printData.SetFromPage(atoi(fromText->GetValue()));
252 printData.SetToPage(atoi(toText->GetValue()));
253 }
254 if (rangeRadioBox->GetSelection() == 0)
255 printData.SetAllPages(TRUE);
256 else
257 printData.SetAllPages(FALSE);
258 printData.SetNoCopies(atoi(noCopiesText->GetValue()));
259 printData.SetPrintToFile(printToFileCheckBox->GetValue());
260
261 return TRUE;
262 }
263
264 wxDC *wxGenericPrintDialog::GetPrintDC(void)
265 {
266 return new wxPostScriptDC(wxThePrintSetupData->GetPrinterFile(), FALSE, NULL);
267 }
268
269 /*
270 * Generic print setup dialog
271 */
272
273 wxGenericPrintSetupDialog::wxGenericPrintSetupDialog(wxWindow *parent, wxPrintSetupData* data):
274 wxDialog(parent, -1, "Print Setup", wxPoint(0, 0), wxSize(600, 600), wxDEFAULT_DIALOG_STYLE|wxDIALOG_MODAL)
275 {
276 if ( data )
277 printData = *data;
278
279 int buttonWidth = 65;
280 int buttonHeight = 25;
281 int spacing = 5;
282 int yPos = 5;
283 int xPos = 5;
284
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));
288
289 okButton->SetDefault();
290 okButton->SetFocus();
291
292 yPos += 35;
293 xPos = 5;
294
295 paperTypeChoice = CreatePaperTypeChoice(&xPos, &yPos);
296
297 wxString choices[2];
298 choices[0] = "Portrait";
299 choices[1] = "Landscape";
300
301 orientationRadioBox = new wxRadioBox(this, wxPRINTID_ORIENTATION, "Orientation",
302 wxPoint(xPos, yPos), wxSize(-1, -1), 2, choices, 2);
303 orientationRadioBox->SetSelection(0);
304
305 xPos += 200;
306
307 colourCheckBox = new wxCheckBox(this, wxPRINTID_PRINTCOLOUR, "Print in colour", wxPoint(xPos, yPos));
308
309 xPos = 5;
310 yPos += 60;
311
312 int staticWidth = 100;
313 int textWidth = 120;
314 spacing = 10;
315
316 (void) new wxStaticText(this, wxPRINTID_STATIC, "Printer command:", wxPoint(xPos, yPos));
317 xPos += staticWidth;
318
319 printerCommandText = new wxTextCtrl(this, wxPRINTID_COMMAND, "", wxPoint(xPos, yPos), wxSize(textWidth, -1));
320 xPos += textWidth + spacing;
321
322 (void) new wxStaticText(this, wxPRINTID_STATIC, "Printer options:", wxPoint(xPos, yPos));
323 xPos += staticWidth;
324
325 printerOptionsText = new wxTextCtrl(this, wxPRINTID_OPTIONS, "", wxPoint(xPos, yPos), wxSize(textWidth, -1));
326
327 Fit();
328 Centre(wxBOTH);
329
330 InitDialog();
331 }
332
333 wxGenericPrintSetupDialog::~wxGenericPrintSetupDialog(void)
334 {
335 }
336
337 bool wxGenericPrintSetupDialog::TransferDataToWindow(void)
338 {
339 if (printerCommandText && printData.GetPrinterCommand())
340 printerCommandText->SetValue(printData.GetPrinterCommand());
341 if (printerOptionsText && printData.GetPrinterOptions())
342 printerOptionsText->SetValue(printData.GetPrinterOptions());
343 if (colourCheckBox)
344 colourCheckBox->SetValue(printData.GetColour());
345
346 if (orientationRadioBox)
347 {
348 if (printData.GetPrinterOrientation() == PS_PORTRAIT)
349 orientationRadioBox->SetSelection(0);
350 else
351 orientationRadioBox->SetSelection(1);
352 }
353 return TRUE;
354 }
355
356 bool wxGenericPrintSetupDialog::TransferDataFromWindow(void)
357 {
358 if (printerCommandText)
359 printData.SetPrinterCommand(WXSTRINGCAST printerCommandText->GetValue());
360 if (printerOptionsText)
361 printData.SetPrinterOptions(WXSTRINGCAST printerOptionsText->GetValue());
362 if (colourCheckBox)
363 printData.SetColour(colourCheckBox->GetValue());
364 if (orientationRadioBox)
365 {
366 int sel = orientationRadioBox->GetSelection();
367 if (sel == 0)
368 printData.SetPrinterOrientation(PS_PORTRAIT);
369 else
370 printData.SetPrinterOrientation(PS_LANDSCAPE);
371 }
372 if (paperTypeChoice)
373 {
374 wxString val(paperTypeChoice->GetStringSelection());
375 if (!val.IsNull() && val != "")
376 printData.SetPaperName((char *)(const char *)val);
377 }
378 return TRUE;
379 }
380
381 wxChoice *wxGenericPrintSetupDialog::CreatePaperTypeChoice(int *x, int *y)
382 {
383 if (!wxThePrintPaperDatabase)
384 {
385 wxThePrintPaperDatabase = new wxPrintPaperDatabase;
386 wxThePrintPaperDatabase->CreateDatabase();
387 }
388 int n = wxThePrintPaperDatabase->Number();
389 wxString *choices = new wxString [n];
390 int sel = 0;
391 int i;
392 for (i = 0; i < n; i++)
393 {
394 wxPrintPaperType *paper = (wxPrintPaperType *)wxThePrintPaperDatabase->Nth(i)->Data();
395 choices[i] = paper->pageName;
396 if (printData.GetPaperName() && choices[i] == printData.GetPaperName())
397 sel = i;
398 }
399
400 (void) new wxStaticText(this, wxPRINTID_STATIC, "Paper size", wxPoint(*x, *y));
401 *y += 25;
402
403 wxChoice *choice = new wxChoice(this, wxPRINTID_PAPERSIZE, wxPoint(*x, *y), wxSize(300, -1), n,
404 choices);
405 *y += 35;
406 delete[] choices;
407
408 choice->SetSelection(sel);
409 return choice;
410 }
411
412 /*
413 * Generic page setup dialog
414 */
415
416 void wxGenericPageSetupDialog::OnPrinter(wxCommandEvent& WXUNUSED(event))
417 {
418 if (wxTheApp->GetPrintMode() == wxPRINT_POSTSCRIPT)
419 {
420 wxGenericPrintSetupDialog *genericPrintSetupDialog =
421 new wxGenericPrintSetupDialog(this, wxThePrintSetupData);
422 int ret = genericPrintSetupDialog->ShowModal();
423 if (ret == wxID_OK)
424 *wxThePrintSetupData = genericPrintSetupDialog->GetPrintData();
425
426 genericPrintSetupDialog->Close(TRUE);
427 }
428 #ifdef __WINDOWS__
429 else
430 {
431 wxPrintData data;
432 data.SetSetupDialog(TRUE);
433 wxPrintDialog printDialog(this, & data);
434 printDialog.Show(TRUE);
435 }
436 #endif
437 }
438
439 wxGenericPageSetupDialog::wxGenericPageSetupDialog(wxWindow *parent, wxPageSetupData* data):
440 wxDialog(parent, -1, "Page Setup", wxPoint(0, 0), wxSize(600, 600), wxDIALOG_MODAL|wxDEFAULT_DIALOG_STYLE)
441 {
442 if ( data )
443 pageData = *data;
444
445 int buttonWidth = 75;
446 int buttonHeight = 25;
447 int spacing = 5;
448 int yPos = 5;
449 int xPos = 5;
450
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));
453
454 printerButton = new wxButton(this, wxPRINTID_SETUP, "Printer...", wxPoint(buttonWidth*2 + 5 + 2*spacing, yPos), wxSize(buttonWidth, buttonHeight));
455
456 if ( !pageData.GetEnablePrinter() )
457 printerButton->Enable(FALSE);
458
459 // if (printData.GetEnableHelp())
460 // wxButton *helpButton = new wxButton(this, (wxFunction)wxGenericPageSetupHelpProc, "Help", -1, -1, buttonWidth, buttonHeight);
461
462 okButton->SetDefault();
463 okButton->SetFocus();
464
465 xPos = 5;
466 yPos += 35;
467
468 paperTypeChoice = CreatePaperTypeChoice(&xPos, &yPos);
469
470 xPos = 5;
471
472 wxString choices[2];
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);
478
479 xPos = 5;
480 yPos += 60;
481
482 int staticWidth = 110;
483 int textWidth = 60;
484 spacing = 10;
485
486 (void) new wxStaticText(this, wxPRINTID_STATIC, "Left margin (mm):", wxPoint(xPos, yPos));
487 xPos += staticWidth;
488
489 marginLeftText = new wxTextCtrl(this, wxPRINTID_LEFTMARGIN, "", wxPoint(xPos, yPos), wxSize(textWidth, -1));
490 xPos += textWidth + spacing;
491
492 (void) new wxStaticText(this, wxPRINTID_STATIC, "Right margin (mm):", wxPoint(xPos, yPos));
493 xPos += staticWidth;
494
495 marginRightText = new wxTextCtrl(this, wxPRINTID_RIGHTMARGIN, "", wxPoint(xPos, yPos), wxSize(textWidth, -1));
496 xPos += textWidth + spacing;
497
498 yPos += 35;
499 xPos = 5;
500
501 (void) new wxStaticText(this, wxPRINTID_STATIC, "Top margin (mm):", wxPoint(xPos, yPos));
502 xPos += staticWidth;
503
504 marginTopText = new wxTextCtrl(this, wxPRINTID_TOPMARGIN, "", wxPoint(xPos, yPos), wxSize(textWidth, -1));
505 xPos += textWidth + spacing;
506
507 (void) new wxStaticText(this, wxPRINTID_STATIC, "Bottom margin (mm):", wxPoint(xPos, yPos));
508 xPos += staticWidth;
509
510 marginBottomText = new wxTextCtrl(this, wxPRINTID_BOTTOMMARGIN, "", wxPoint(xPos, yPos), wxSize(textWidth, -1));
511
512 Fit();
513 Centre(wxBOTH);
514
515 InitDialog();
516 }
517
518 wxGenericPageSetupDialog::~wxGenericPageSetupDialog(void)
519 {
520 }
521
522 bool wxGenericPageSetupDialog::TransferDataToWindow(void)
523 {
524 if (marginLeftText)
525 marginLeftText->SetValue(IntToString((int) pageData.GetMarginTopLeft().x));
526 if (marginTopText)
527 marginTopText->SetValue(IntToString((int) pageData.GetMarginTopLeft().y));
528 if (marginRightText)
529 marginRightText->SetValue(IntToString((int) pageData.GetMarginBottomRight().x));
530 if (marginBottomText)
531 marginBottomText->SetValue(IntToString((int) pageData.GetMarginBottomRight().y));
532
533 if (orientationRadioBox)
534 {
535 if (pageData.GetOrientation() == wxPORTRAIT)
536 orientationRadioBox->SetSelection(0);
537 else
538 orientationRadioBox->SetSelection(1);
539 }
540 return TRUE;
541 }
542
543 bool wxGenericPageSetupDialog::TransferDataFromWindow(void)
544 {
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())));
549
550 if (orientationRadioBox)
551 {
552 int sel = orientationRadioBox->GetSelection();
553 if (sel == 0)
554 {
555 wxThePrintSetupData->SetPrinterOrientation(wxPORTRAIT);
556 pageData.SetOrientation(wxPORTRAIT);
557 }
558 else
559 {
560 wxThePrintSetupData->SetPrinterOrientation(wxLANDSCAPE);
561 pageData.SetOrientation(wxLANDSCAPE);
562 }
563 }
564 if (paperTypeChoice)
565 {
566 wxString val(paperTypeChoice->GetStringSelection());
567 if (!val.IsNull() && val != "")
568 {
569 wxPrintPaperType* paper = wxThePrintPaperDatabase->FindPaperType((char*) (const char *)val);
570 if ( paper )
571 {
572 pageData.SetPaperSize(wxPoint(paper->widthMM, paper->heightMM));
573 }
574 }
575 }
576 return TRUE;
577 }
578
579 wxChoice *wxGenericPageSetupDialog::CreatePaperTypeChoice(int *x, int *y)
580 {
581 if (!wxThePrintPaperDatabase)
582 {
583 wxThePrintPaperDatabase = new wxPrintPaperDatabase;
584 wxThePrintPaperDatabase->CreateDatabase();
585 }
586 int n = wxThePrintPaperDatabase->Number();
587 wxString *choices = new wxString [n];
588 int sel = 0;
589 int i;
590 for (i = 0; i < n; i++)
591 {
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)
595 sel = i;
596 }
597
598 (void) new wxStaticText(this, wxPRINTID_STATIC, "Paper size", wxPoint(*x, *y));
599 *y += 25;
600
601 wxChoice *choice = new wxChoice(this, wxPRINTID_PAPERSIZE, wxPoint(*x, *y), wxSize(300, -1), n,
602 choices);
603 *y += 35;
604 delete[] choices;
605
606 choice->SetSelection(sel);
607 return choice;
608 }
609
610