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