First step at moving the print (dialog) native
[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
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "prntdlgg.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #if wxUSE_PRINTING_ARCHITECTURE && (!defined(__WXMSW__) || wxUSE_POSTSCRIPT_ARCHITECTURE_IN_MSW)
32
33 #ifndef WX_PRECOMP
34 #include "wx/utils.h"
35 #include "wx/dc.h"
36 #include "wx/stattext.h"
37 #include "wx/statbox.h"
38 #include "wx/button.h"
39 #include "wx/checkbox.h"
40 #include "wx/textctrl.h"
41 #include "wx/radiobox.h"
42 #include "wx/filedlg.h"
43 #include "wx/combobox.h"
44 #include "wx/intl.h"
45 #include "wx/sizer.h"
46 #include "wx/cmndata.h"
47 #endif
48
49 #if wxUSE_STATLINE
50 #include "wx/statline.h"
51 #endif
52
53 #include "wx/generic/prntdlgg.h"
54
55 #if wxUSE_POSTSCRIPT
56 #include "wx/generic/dcpsg.h"
57 #endif
58
59 #include "wx/prntbase.h"
60 #include "wx/printdlg.h"
61 #include "wx/paper.h"
62 #include "wx/filename.h"
63
64 // For print paper things
65 #include "wx/prntbase.h"
66
67 #include <stdlib.h>
68 #include <string.h>
69
70 // ----------------------------------------------------------------------------
71 // global vars
72 // ----------------------------------------------------------------------------
73
74 extern wxPrintPaperDatabase *wxThePrintPaperDatabase;
75
76 #if wxUSE_POSTSCRIPT
77
78 //----------------------------------------------------------------------------
79 // wxPostScriptNativeData
80 //----------------------------------------------------------------------------
81
82 IMPLEMENT_CLASS(wxPostScriptPrintNativeData, wxPrintNativeDataBase)
83
84 wxPostScriptPrintNativeData::wxPostScriptPrintNativeData()
85 {
86 m_previewCommand = wxT("");
87 #ifdef __VMS__
88 m_printerCommand = wxT("print");
89 m_printerOptions = wxT("/nonotify/queue=psqueue");
90 m_afmPath = wxT("sys$ps_font_metrics:");
91 #endif
92
93 #ifdef __WXMSW__
94 m_printerCommand = wxT("print");
95 m_printerOptions = wxT("");
96 m_afmPath = wxT("c:\\windows\\system\\");
97 #endif
98
99 #if !defined(__VMS__) && !defined(__WXMSW__)
100 m_printerCommand = wxT("lpr");
101 m_printerOptions = wxT("");
102 m_afmPath = wxT("");
103 #endif
104
105 m_printerScaleX = 1.0;
106 m_printerScaleY = 1.0;
107 m_printerTranslateX = 0;
108 m_printerTranslateY = 0;
109 m_printMode = wxPRINT_MODE_FILE;
110 }
111
112 wxPostScriptPrintNativeData::~wxPostScriptPrintNativeData()
113 {
114 }
115
116 bool wxPostScriptPrintNativeData::ConvertTo( wxPrintData &data )
117 {
118 return true;
119 }
120
121 bool wxPostScriptPrintNativeData::ConvertFrom( const wxPrintData &data )
122 {
123 return true;
124 }
125
126 // ----------------------------------------------------------------------------
127 // Generic print dialog for non-Windows printing use.
128 // ----------------------------------------------------------------------------
129
130 IMPLEMENT_CLASS(wxGenericPrintDialog, wxPrintDialogBase)
131
132 BEGIN_EVENT_TABLE(wxGenericPrintDialog, wxPrintDialogBase)
133 EVT_BUTTON(wxID_OK, wxGenericPrintDialog::OnOK)
134 EVT_BUTTON(wxPRINTID_SETUP, wxGenericPrintDialog::OnSetup)
135 EVT_RADIOBOX(wxPRINTID_RANGE, wxGenericPrintDialog::OnRange)
136 END_EVENT_TABLE()
137
138 wxGenericPrintDialog::wxGenericPrintDialog(wxWindow *parent,
139 wxPrintDialogData* data)
140 : wxPrintDialogBase(parent, wxID_ANY, _("Print"),
141 wxPoint(0, 0), wxSize(600, 600),
142 wxDEFAULT_DIALOG_STYLE |
143 wxTAB_TRAVERSAL)
144 {
145 if ( data )
146 m_printDialogData = *data;
147
148 Init(parent);
149 }
150
151 wxGenericPrintDialog::wxGenericPrintDialog(wxWindow *parent,
152 wxPrintData* data)
153 : wxPrintDialogBase(parent, wxID_ANY, _("Print"),
154 wxPoint(0, 0), wxSize(600, 600),
155 wxDEFAULT_DIALOG_STYLE |
156 wxTAB_TRAVERSAL)
157 {
158 if ( data )
159 m_printDialogData = *data;
160
161 Init(parent);
162 }
163
164 void wxGenericPrintDialog::Init(wxWindow * WXUNUSED(parent))
165 {
166 // wxDialog::Create(parent, wxID_ANY, _("Print"), wxPoint(0, 0), wxSize(600, 600),
167 // wxDEFAULT_DIALOG_STYLE | wxTAB_TRAVERSAL);
168
169 wxBoxSizer *mainsizer = new wxBoxSizer( wxVERTICAL );
170
171 // 1) top row
172
173 wxStaticBoxSizer *topsizer = new wxStaticBoxSizer(
174 new wxStaticBox( this, wxID_ANY, _( "Printer options" ) ), wxHORIZONTAL );
175 m_printToFileCheckBox = new wxCheckBox( this, wxPRINTID_PRINTTOFILE, _("Print to File") );
176 topsizer->Add( m_printToFileCheckBox, 0, wxCENTER|wxALL, 5 );
177
178 topsizer->Add( 60,2,1 );
179
180 m_setupButton = new wxButton(this, wxPRINTID_SETUP, _("Setup...") );
181 topsizer->Add( m_setupButton, 0, wxCENTER|wxALL, 5 );
182
183 mainsizer->Add( topsizer, 0, wxLEFT|wxTOP|wxRIGHT, 10 );
184
185 // 2) middle row with radio box
186
187 wxString *choices = new wxString[2];
188 choices[0] = _("All");
189 choices[1] = _("Pages");
190
191 m_fromText = (wxTextCtrl*)NULL;
192 m_toText = (wxTextCtrl*)NULL;
193 m_rangeRadioBox = (wxRadioBox *)NULL;
194
195 if (m_printDialogData.GetFromPage() != 0)
196 {
197 m_rangeRadioBox = new wxRadioBox(this, wxPRINTID_RANGE, _("Print Range"),
198 wxDefaultPosition, wxDefaultSize,
199 2, choices,
200 1, wxRA_VERTICAL);
201 m_rangeRadioBox->SetSelection(1);
202
203 mainsizer->Add( m_rangeRadioBox, 0, wxLEFT|wxTOP|wxRIGHT, 10 );
204 }
205
206 // 3) bottom row
207
208 wxBoxSizer *bottomsizer = new wxBoxSizer( wxHORIZONTAL );
209
210 if (m_printDialogData.GetFromPage() != 0)
211 {
212 bottomsizer->Add( new wxStaticText(this, wxPRINTID_STATIC, _("From:") ), 0, wxCENTER|wxALL, 5 );
213 m_fromText = new wxTextCtrl(this, wxPRINTID_FROM, wxEmptyString, wxDefaultPosition, wxSize(40, wxDefaultCoord));
214 bottomsizer->Add( m_fromText, 1, wxCENTER|wxRIGHT, 10 );
215
216 bottomsizer->Add( new wxStaticText(this, wxPRINTID_STATIC, _("To:") ), 0, wxCENTER|wxALL, 5);
217 m_toText = new wxTextCtrl(this, wxPRINTID_TO, wxEmptyString, wxDefaultPosition, wxSize(40, wxDefaultCoord));
218 bottomsizer->Add( m_toText, 1, wxCENTER|wxRIGHT, 10 );
219 }
220
221 bottomsizer->Add( new wxStaticText(this, wxPRINTID_STATIC, _("Copies:") ), 0, wxCENTER|wxALL, 5 );
222 m_noCopiesText = new wxTextCtrl(this, wxPRINTID_COPIES, wxEmptyString, wxPoint(252, 130), wxSize(40, wxDefaultCoord));
223 bottomsizer->Add( m_noCopiesText, 1, wxCENTER|wxRIGHT, 10 );
224
225 mainsizer->Add( bottomsizer, 0, wxTOP|wxLEFT|wxRIGHT, 12 );
226
227 #if wxUSE_STATLINE
228 // 4) static line
229 mainsizer->Add( new wxStaticLine( this, wxID_ANY ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 );
230 #endif
231
232 // 5) buttons
233
234 mainsizer->Add( CreateButtonSizer( wxOK|wxCANCEL), 0, wxCENTER|wxALL, 10 );
235
236 SetAutoLayout( true );
237 SetSizer( mainsizer );
238
239 mainsizer->Fit( this );
240 Centre(wxBOTH);
241
242 // Calls wxWindow::OnInitDialog and then wxGenericPrintDialog::TransferDataToWindow
243 InitDialog();
244 delete[] choices;
245 }
246
247 int wxGenericPrintDialog::ShowModal()
248 {
249 if ( m_printDialogData.GetSetupDialog() )
250 {
251 // Make sure wxPrintData object reflects the settings now, in case the setup dialog
252 // changes it. In fact there aren't any common settings at
253 // present, but there might be in future.
254 // TransferDataFromWindow();
255
256 wxGenericPrintSetupDialog *genericPrintSetupDialog =
257 new wxGenericPrintSetupDialog(this, & m_printDialogData.GetPrintData());
258 int ret = genericPrintSetupDialog->ShowModal();
259 if ( ret != wxID_CANCEL )
260 {
261 // Transfer settings to the print dialog's print data.
262 m_printDialogData.GetPrintData() = genericPrintSetupDialog->GetPrintData();
263 }
264 // genericPrintSetupDialog->Destroy();
265
266 // Restore the wxPrintData settings again (uncomment if any settings become common
267 // to both dialogs)
268 // TransferDataToWindow();
269
270 return ret;
271 }
272 else
273 {
274 return wxDialog::ShowModal();
275 }
276 }
277
278 wxGenericPrintDialog::~wxGenericPrintDialog()
279 {
280 }
281
282 void wxGenericPrintDialog::OnOK(wxCommandEvent& WXUNUSED(event))
283 {
284 TransferDataFromWindow();
285
286 // An empty 'to' field signals printing just the
287 // 'from' page.
288 if (m_printDialogData.GetToPage() < 1)
289 m_printDialogData.SetToPage(m_printDialogData.GetFromPage());
290
291 wxPostScriptPrintNativeData *data =
292 (wxPostScriptPrintNativeData *) m_printDialogData.GetPrintData().GetNativeData();
293
294 // There are some interactions between the global setup data
295 // and the standard print dialog. The global printing 'mode'
296 // is determined by whether the user checks Print to file
297 // or not.
298 if (m_printDialogData.GetPrintToFile())
299 {
300 data->SetPrintMode(wxPRINT_MODE_FILE);
301
302 wxFileName fname( m_printDialogData.GetPrintData().GetFilename() );
303
304 wxFileDialog dialog( this, _("PostScript file"),
305 fname.GetPath(), fname.GetFullName(), wxT("*.ps"), wxOPEN | wxOVERWRITE_PROMPT );
306 if (dialog.ShowModal() != wxID_OK) return;
307
308 m_printDialogData.GetPrintData().SetFilename( dialog.GetPath() );
309 }
310 else
311 {
312 data->SetPrintMode(wxPRINT_MODE_PRINTER);
313 }
314
315 EndModal(wxID_OK);
316 }
317
318 void wxGenericPrintDialog::OnRange(wxCommandEvent& event)
319 {
320 if (!m_fromText) return;
321
322 if (event.GetInt() == 0)
323 {
324 m_fromText->Enable(false);
325 m_toText->Enable(false);
326 }
327 else if (event.GetInt() == 1)
328 {
329 m_fromText->Enable(true);
330 m_toText->Enable(true);
331 }
332 }
333
334 void wxGenericPrintDialog::OnSetup(wxCommandEvent& WXUNUSED(event))
335 {
336 wxGenericPrintSetupDialog dialog( this, &m_printDialogData.GetPrintData() );
337 if (dialog.ShowModal() != wxID_CANCEL)
338 {
339 m_printDialogData = dialog.GetPrintData();
340 }
341 }
342
343 bool wxGenericPrintDialog::TransferDataToWindow()
344 {
345 if(m_printDialogData.GetFromPage() != 0)
346 {
347 if(m_fromText)
348 {
349 if (m_printDialogData.GetEnablePageNumbers())
350 {
351 m_fromText->Enable(true);
352 m_toText->Enable(true);
353 if (m_printDialogData.GetFromPage() > 0)
354 m_fromText->SetValue(wxString::Format(_T("%d"), m_printDialogData.GetFromPage()));
355 if (m_printDialogData.GetToPage() > 0)
356 m_toText->SetValue(wxString::Format(_T("%d"), m_printDialogData.GetToPage()));
357 if(m_rangeRadioBox)
358 if (m_printDialogData.GetAllPages() || m_printDialogData.GetFromPage() == 0)
359 m_rangeRadioBox->SetSelection(0);
360 else
361 m_rangeRadioBox->SetSelection(1);
362 }
363 else
364 {
365 m_fromText->Enable(false);
366 m_toText->Enable(false);
367 if(m_rangeRadioBox)
368 {
369 m_rangeRadioBox->SetSelection(0);
370 m_rangeRadioBox->wxRadioBox::Enable(1, false);
371 }
372 }
373 }
374 }
375 m_noCopiesText->SetValue(
376 wxString::Format(_T("%d"), m_printDialogData.GetNoCopies()));
377
378 m_printToFileCheckBox->SetValue(m_printDialogData.GetPrintToFile());
379 m_printToFileCheckBox->Enable(m_printDialogData.GetEnablePrintToFile());
380 return true;
381 }
382
383 bool wxGenericPrintDialog::TransferDataFromWindow()
384 {
385 long res = 0;
386 if(m_printDialogData.GetFromPage() != -1)
387 {
388 if (m_printDialogData.GetEnablePageNumbers())
389 {
390 if(m_fromText)
391 {
392 wxString value = m_fromText->GetValue();
393 if (value.ToLong( &res ))
394 m_printDialogData.SetFromPage( res );
395 }
396 if(m_toText)
397 {
398 wxString value = m_toText->GetValue();
399 if (value.ToLong( &res ))
400 m_printDialogData.SetToPage( res );
401 }
402 }
403 if(m_rangeRadioBox)
404 {
405 if (m_rangeRadioBox->GetSelection() == 0)
406 m_printDialogData.SetAllPages(true);
407 else
408 m_printDialogData.SetAllPages(false);
409 }
410 }
411 else
412 { // continuous printing
413 m_printDialogData.SetFromPage(1);
414 m_printDialogData.SetToPage(32000);
415 }
416
417 wxString value = m_noCopiesText->GetValue();
418 if (value.ToLong( &res ))
419 m_printDialogData.SetNoCopies( res );
420
421 m_printDialogData.SetPrintToFile(m_printToFileCheckBox->GetValue());
422
423 return true;
424 }
425
426 /*
427 TODO: collate and noCopies should be duplicated across dialog data and print data objects
428 (slightly different semantics on Windows but let's ignore this for a bit).
429 */
430
431 wxDC *wxGenericPrintDialog::GetPrintDC()
432 {
433 // return new wxPostScriptDC(wxThePrintSetupData->GetPrinterFile(), false, (wxWindow *) NULL);
434 return new wxPostScriptDC(GetPrintDialogData().GetPrintData());
435 }
436
437 // ----------------------------------------------------------------------------
438 // Generic print setup dialog
439 // ----------------------------------------------------------------------------
440
441 IMPLEMENT_CLASS(wxGenericPrintSetupDialog, wxDialog)
442
443 wxGenericPrintSetupDialog::wxGenericPrintSetupDialog(wxWindow *parent, wxPrintData* data):
444 wxDialog(parent, wxID_ANY, _("Print Setup"), wxPoint(0, 0), wxSize(600, 600), wxDEFAULT_DIALOG_STYLE|wxTAB_TRAVERSAL)
445 {
446 Init(data);
447 }
448
449 void wxGenericPrintSetupDialog::Init(wxPrintData* data)
450 {
451 if ( data )
452 m_printData = *data;
453
454 int staticBoxWidth = 300;
455
456 (void) new wxStaticBox(this, wxPRINTID_STATIC, _("Paper size"), wxPoint(10, 10), wxSize(staticBoxWidth, 60) );
457
458 int xPos = 20;
459 int yPos = 30;
460 m_paperTypeChoice = CreatePaperTypeChoice(&xPos, &yPos);
461
462 wxString *choices = new wxString[2];
463 choices[0] = _("Portrait");
464 choices[1] = _("Landscape");
465
466 m_orientationRadioBox = new wxRadioBox(this, wxPRINTID_ORIENTATION, _("Orientation"),
467 wxPoint(10, 80), wxDefaultSize, 2, choices, 1, wxRA_VERTICAL );
468 m_orientationRadioBox->SetSelection(0);
469
470 (void) new wxStaticBox(this, wxPRINTID_STATIC, _("Options"), wxPoint(10, 130), wxSize(staticBoxWidth, 50) );
471
472 int colourYPos = 145;
473
474 #ifdef __WXMOTIF__
475 colourYPos = 150;
476 #endif
477
478 m_colourCheckBox = new wxCheckBox(this, wxPRINTID_PRINTCOLOUR, _("Print in colour"), wxPoint(15, colourYPos));
479
480 (void) new wxStaticBox(this, wxPRINTID_STATIC, _("Print spooling"), wxPoint(330, 10), wxSize(200,170) );
481
482 (void) new wxStaticText(this, wxPRINTID_STATIC, _("Printer command:"), wxPoint(340, 30));
483
484 m_printerCommandText = new wxTextCtrl(this, wxPRINTID_COMMAND, wxEmptyString, wxPoint(360, 55), wxSize(150, wxDefaultCoord));
485
486 (void) new wxStaticText(this, wxPRINTID_STATIC, _("Printer options:"), wxPoint(340, 110));
487
488 m_printerOptionsText = new wxTextCtrl(this, wxPRINTID_OPTIONS, wxEmptyString, wxPoint(360, 135), wxSize(150, wxDefaultCoord));
489
490 wxButton *okButton = new wxButton(this, wxID_OK, _("OK"), wxPoint(130, 200), wxSize(80, wxDefaultCoord));
491 (void) new wxButton(this, wxID_CANCEL, _("Cancel"), wxPoint(320, 200), wxSize(80, wxDefaultCoord));
492
493 okButton->SetDefault();
494 okButton->SetFocus();
495
496 Fit();
497 Centre(wxBOTH);
498
499 InitDialog();
500 delete[] choices;
501 }
502
503 wxGenericPrintSetupDialog::~wxGenericPrintSetupDialog()
504 {
505 }
506
507 bool wxGenericPrintSetupDialog::TransferDataToWindow()
508 {
509 wxPostScriptPrintNativeData *data =
510 (wxPostScriptPrintNativeData *) m_printData.GetNativeData();
511
512 if (m_printerCommandText && data->GetPrinterCommand())
513 m_printerCommandText->SetValue(data->GetPrinterCommand());
514 if (m_printerOptionsText && data->GetPrinterOptions())
515 m_printerOptionsText->SetValue(data->GetPrinterOptions());
516 if (m_colourCheckBox)
517 m_colourCheckBox->SetValue(m_printData.GetColour());
518
519 if (m_orientationRadioBox)
520 {
521 if (m_printData.GetOrientation() == wxPORTRAIT)
522 m_orientationRadioBox->SetSelection(0);
523 else
524 m_orientationRadioBox->SetSelection(1);
525 }
526 return true;
527 }
528
529 bool wxGenericPrintSetupDialog::TransferDataFromWindow()
530 {
531 wxPostScriptPrintNativeData *data =
532 (wxPostScriptPrintNativeData *) m_printData.GetNativeData();
533
534 if (m_printerCommandText)
535 data->SetPrinterCommand(m_printerCommandText->GetValue());
536 if (m_printerOptionsText)
537 data->SetPrinterOptions(m_printerOptionsText->GetValue());
538 if (m_colourCheckBox)
539 m_printData.SetColour(m_colourCheckBox->GetValue());
540 if (m_orientationRadioBox)
541 {
542 int sel = m_orientationRadioBox->GetSelection();
543 if (sel == 0)
544 m_printData.SetOrientation(wxPORTRAIT);
545 else
546 m_printData.SetOrientation(wxLANDSCAPE);
547 }
548 if (m_paperTypeChoice)
549 {
550 int selectedItem = m_paperTypeChoice->GetSelection();
551 if (selectedItem != -1)
552 {
553 wxPrintPaperType *paper = wxThePrintPaperDatabase->Item(selectedItem);
554 if (paper != NULL)
555 m_printData.SetPaperId( paper->GetId());
556 }
557 }
558
559 return true;
560 }
561
562 wxComboBox *wxGenericPrintSetupDialog::CreatePaperTypeChoice(int *x, int *y)
563 {
564 /* Should not be necessary
565 if (!wxThePrintPaperDatabase)
566 {
567 wxThePrintPaperDatabase = new wxPrintPaperDatabase;
568 wxThePrintPaperDatabase->CreateDatabase();
569 }
570 */
571 size_t n = wxThePrintPaperDatabase->GetCount();
572 wxString *choices = new wxString [n];
573 size_t sel = 0;
574
575 for (size_t i = 0; i < n; i++)
576 {
577 wxPrintPaperType *paper = wxThePrintPaperDatabase->Item(i);
578 choices[i] = paper->GetName();
579 if (m_printData.GetPaperId() == paper->GetId())
580 sel = i;
581 }
582
583 int width = 250;
584
585 wxComboBox *choice = new wxComboBox( this,
586 wxPRINTID_PAPERSIZE,
587 _("Paper Size"),
588 wxPoint(*x, *y),
589 wxSize(width, wxDefaultCoord),
590 n, choices );
591
592 // SetFont(thisFont);
593
594 delete[] choices;
595
596 choice->SetSelection(sel);
597 return choice;
598 }
599 #endif // wxUSE_POSTSCRIPT
600
601 // ----------------------------------------------------------------------------
602 // Generic page setup dialog
603 // ----------------------------------------------------------------------------
604
605 IMPLEMENT_CLASS(wxGenericPageSetupDialog, wxDialog)
606
607 BEGIN_EVENT_TABLE(wxGenericPageSetupDialog, wxDialog)
608 EVT_BUTTON(wxPRINTID_SETUP, wxGenericPageSetupDialog::OnPrinter)
609 END_EVENT_TABLE()
610
611 void wxGenericPageSetupDialog::OnPrinter(wxCommandEvent& WXUNUSED(event))
612 {
613 // We no longer query GetPrintMode, so we can eliminate the need
614 // to call SetPrintMode.
615 // This has the limitation that we can't explicitly call the PostScript
616 // print setup dialog from the generic Page Setup dialog under Windows,
617 // but since this choice would only happen when trying to do PostScript
618 // printing under Windows (and only in 16-bit Windows which
619 // doesn't have a Windows-specific page setup dialog) it's worth it.
620
621 // First save the current settings, so the wxPrintData object is up to date.
622 TransferDataFromWindow();
623
624 // Transfer the current print settings from this dialog to the page setup dialog.
625 wxPrintDialogData data;
626 data = GetPageSetupData().GetPrintData();
627 data.SetSetupDialog(true);
628 wxPrintDialog printDialog(this, & data);
629 printDialog.ShowModal();
630
631 // Transfer the page setup print settings from the page dialog to this dialog again, in case
632 // the page setup dialog changed something.
633 GetPageSetupData().GetPrintData() = printDialog.GetPrintDialogData().GetPrintData();
634 GetPageSetupData().CalculatePaperSizeFromId(); // Make sure page size reflects the id in wxPrintData
635
636 // Now update the dialog in case the page setup dialog changed some of our settings.
637 TransferDataToWindow();
638 }
639
640 wxGenericPageSetupDialog::wxGenericPageSetupDialog( wxWindow *parent,
641 wxPageSetupData* data)
642 : wxDialog( parent,
643 wxID_ANY,
644 _("Page Setup"),
645 wxPoint(0, 0),
646 wxSize(600, 600),
647 wxDEFAULT_DIALOG_STYLE|wxTAB_TRAVERSAL )
648 {
649 if (data)
650 m_pageData = *data;
651
652 int textWidth = 80;
653
654 wxBoxSizer *mainsizer = new wxBoxSizer( wxVERTICAL );
655
656 // 1) top
657 wxStaticBoxSizer *topsizer = new wxStaticBoxSizer(
658 new wxStaticBox(this,wxPRINTID_STATIC, _("Paper size")), wxHORIZONTAL );
659
660 size_t n = wxThePrintPaperDatabase->GetCount();
661 wxString *choices = new wxString [n];
662
663 for (size_t i = 0; i < n; i++)
664 {
665 wxPrintPaperType *paper = wxThePrintPaperDatabase->Item(i);
666 choices[i] = paper->GetName();
667 }
668
669 m_paperTypeChoice = new wxComboBox( this,
670 wxPRINTID_PAPERSIZE,
671 _("Paper Size"),
672 wxDefaultPosition,
673 wxSize(300, wxDefaultCoord),
674 n, choices );
675 topsizer->Add( m_paperTypeChoice, 1, wxEXPAND|wxALL, 5 );
676 // m_paperTypeChoice->SetSelection(sel);
677
678 mainsizer->Add( topsizer, 0, wxTOP|wxLEFT|wxRIGHT | wxEXPAND, 10 );
679
680 // 2) middle sizer with radio box
681
682 wxString *choices2 = new wxString[2];
683 choices2[0] = _("Portrait");
684 choices2[1] = _("Landscape");
685 m_orientationRadioBox = new wxRadioBox(this, wxPRINTID_ORIENTATION, _("Orientation"),
686 wxDefaultPosition, wxDefaultSize, 2, choices2, 2);
687 m_orientationRadioBox->SetSelection(0);
688
689 mainsizer->Add( m_orientationRadioBox, 0, wxTOP|wxLEFT|wxRIGHT, 10 );
690
691 // 3) margins
692
693 wxBoxSizer *table = new wxBoxSizer( wxHORIZONTAL );
694
695 wxBoxSizer *column1 = new wxBoxSizer( wxVERTICAL );
696 column1->Add( new wxStaticText(this, wxPRINTID_STATIC, _("Left margin (mm):")),1,wxALL|wxALIGN_RIGHT,5 );
697 column1->Add( new wxStaticText(this, wxPRINTID_STATIC, _("Top margin (mm):")),1,wxALL|wxALIGN_RIGHT,5 );
698 table->Add( column1, 0, wxALL | wxEXPAND, 5 );
699
700 wxBoxSizer *column2 = new wxBoxSizer( wxVERTICAL );
701 m_marginLeftText = new wxTextCtrl(this, wxPRINTID_LEFTMARGIN, wxEmptyString, wxDefaultPosition, wxSize(textWidth, wxDefaultCoord));
702 m_marginTopText = new wxTextCtrl(this, wxPRINTID_TOPMARGIN, wxEmptyString, wxDefaultPosition, wxSize(textWidth, wxDefaultCoord));
703 column2->Add( m_marginLeftText, 1, wxALL, 5 );
704 column2->Add( m_marginTopText, 1, wxALL, 5 );
705 table->Add( column2, 0, wxRIGHT|wxTOP|wxBOTTOM | wxEXPAND, 5 );
706
707 wxBoxSizer *column3 = new wxBoxSizer( wxVERTICAL );
708 column3->Add( new wxStaticText(this, wxPRINTID_STATIC, _("Right margin (mm):")),1,wxALL|wxALIGN_RIGHT,5 );
709 column3->Add( new wxStaticText(this, wxPRINTID_STATIC, _("Bottom margin (mm):")),1,wxALL|wxALIGN_RIGHT,5 );
710 table->Add( column3, 0, wxALL | wxEXPAND, 5 );
711
712 wxBoxSizer *column4 = new wxBoxSizer( wxVERTICAL );
713 m_marginRightText = new wxTextCtrl(this, wxPRINTID_RIGHTMARGIN, wxEmptyString, wxDefaultPosition, wxSize(textWidth, wxDefaultCoord));
714 m_marginBottomText = new wxTextCtrl(this, wxPRINTID_BOTTOMMARGIN, wxEmptyString, wxDefaultPosition, wxSize(textWidth, wxDefaultCoord));
715 column4->Add( m_marginRightText, 1, wxALL, 5 );
716 column4->Add( m_marginBottomText, 1, wxALL, 5 );
717 table->Add( column4, 0, wxRIGHT|wxTOP|wxBOTTOM | wxEXPAND, 5 );
718
719 mainsizer->Add( table, 0 );
720
721 #if wxUSE_STATLINE
722 // 5) static line
723 mainsizer->Add( new wxStaticLine( this, wxID_ANY ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 );
724 #endif
725
726 // 6) buttons
727
728 wxSizer* buttonsizer = CreateButtonSizer( wxOK|wxCANCEL);
729 m_printerButton = new wxButton(this, wxPRINTID_SETUP, _("Printer...") );
730 buttonsizer->Add( m_printerButton, 0, wxLEFT|wxRIGHT, 10 );
731 if ( !m_pageData.GetEnablePrinter() )
732 m_printerButton->Enable(false);
733 // if (m_printData.GetEnableHelp())
734 // wxButton *helpButton = new wxButton(this, (wxFunction)wxGenericPageSetupHelpProc, _("Help"), wxDefaultCoord, wxDefaultCoord, buttonWidth, buttonHeight);
735 mainsizer->Add( buttonsizer, 0, wxCENTER|wxALL, 10 );
736
737
738 SetAutoLayout( true );
739 SetSizer( mainsizer );
740
741 mainsizer->Fit( this );
742 Centre(wxBOTH);
743
744 InitDialog();
745
746 delete[] choices;
747 delete [] choices2;
748 }
749
750 wxGenericPageSetupDialog::~wxGenericPageSetupDialog()
751 {
752 }
753
754 bool wxGenericPageSetupDialog::TransferDataToWindow()
755 {
756 if (m_marginLeftText)
757 m_marginLeftText->SetValue(wxString::Format(wxT("%d"), m_pageData.GetMarginTopLeft().x));
758 if (m_marginTopText)
759 m_marginTopText->SetValue(wxString::Format(wxT("%d"), m_pageData.GetMarginTopLeft().y));
760 if (m_marginRightText)
761 m_marginRightText->SetValue(wxString::Format(wxT("%d"), m_pageData.GetMarginBottomRight().x));
762 if (m_marginBottomText)
763 m_marginBottomText->SetValue(wxString::Format(wxT("%d"), m_pageData.GetMarginBottomRight().y));
764
765 if (m_orientationRadioBox)
766 {
767 if (m_pageData.GetPrintData().GetOrientation() == wxPORTRAIT)
768 m_orientationRadioBox->SetSelection(0);
769 else
770 m_orientationRadioBox->SetSelection(1);
771 }
772
773 // Find the paper type from either the current paper size in the wxPageSetupDialogData, or
774 // failing that, the id in the wxPrintData object.
775
776 wxPrintPaperType* type = wxThePrintPaperDatabase->FindPaperType(
777 wxSize(m_pageData.GetPaperSize().x * 10, m_pageData.GetPaperSize().y * 10));
778
779 if (!type && m_pageData.GetPrintData().GetPaperId() != wxPAPER_NONE)
780 type = wxThePrintPaperDatabase->FindPaperType(m_pageData.GetPrintData().GetPaperId());
781
782 if (type)
783 {
784 m_paperTypeChoice->SetStringSelection(type->GetName());
785 }
786
787 return true;
788 }
789
790 bool wxGenericPageSetupDialog::TransferDataFromWindow()
791 {
792 if (m_marginLeftText && m_marginTopText)
793 {
794 int left = wxAtoi( m_marginLeftText->GetValue().c_str() );
795 int top = wxAtoi( m_marginTopText->GetValue().c_str() );
796 m_pageData.SetMarginTopLeft( wxPoint(left,top) );
797 }
798 if (m_marginRightText && m_marginBottomText)
799 {
800 int right = wxAtoi( m_marginRightText->GetValue().c_str() );
801 int bottom = wxAtoi( m_marginBottomText->GetValue().c_str() );
802 m_pageData.SetMarginBottomRight( wxPoint(right,bottom) );
803 }
804
805 if (m_orientationRadioBox)
806 {
807 int sel = m_orientationRadioBox->GetSelection();
808 if (sel == 0)
809 {
810 m_pageData.GetPrintData().SetOrientation(wxPORTRAIT);
811 }
812 else
813 {
814 m_pageData.GetPrintData().SetOrientation(wxLANDSCAPE);
815 }
816 }
817
818 if (m_paperTypeChoice)
819 {
820 int selectedItem = m_paperTypeChoice->GetSelection();
821 if (selectedItem != -1)
822 {
823 wxPrintPaperType *paper = wxThePrintPaperDatabase->Item(selectedItem);
824 if ( paper )
825 {
826 m_pageData.SetPaperSize(wxSize(paper->GetWidth()/10, paper->GetHeight()/10));
827 m_pageData.GetPrintData().SetPaperId(paper->GetId());
828 }
829 }
830 }
831
832 return true;
833 }
834
835 wxComboBox *wxGenericPageSetupDialog::CreatePaperTypeChoice(int *x, int *y)
836 {
837 /*
838 if (!wxThePrintPaperDatabase)
839 {
840 wxThePrintPaperDatabase = new wxPrintPaperDatabase;
841 wxThePrintPaperDatabase->CreateDatabase();
842 }
843 */
844
845 size_t n = wxThePrintPaperDatabase->GetCount();
846 wxString *choices = new wxString [n];
847
848 for (size_t i = 0; i < n; i++)
849 {
850 wxPrintPaperType *paper = wxThePrintPaperDatabase->Item(i);
851 choices[i] = paper->GetName();
852 }
853
854 (void) new wxStaticText(this, wxPRINTID_STATIC, _("Paper size"), wxPoint(*x, *y));
855 *y += 25;
856
857 wxComboBox *choice = new wxComboBox( this,
858 wxPRINTID_PAPERSIZE,
859 _("Paper Size"),
860 wxPoint(*x, *y),
861 wxSize(300, wxDefaultCoord),
862 n, choices );
863 *y += 35;
864 delete[] choices;
865
866 // choice->SetSelection(sel);
867 return choice;
868 }
869
870 #endif
871