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