Renamed wxPrintNativeData methods to better match
[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::TransferTo( wxPrintData &data )
117 {
118 return true;
119 }
120
121 bool wxPostScriptPrintNativeData::TransferFrom( 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( this, &m_printDialogData.GetPrintData() );
257 int ret = genericPrintSetupDialog.ShowModal();
258 if (ret != wxID_CANCEL)
259 {
260 // Transfer settings to the print dialog's print data.
261 m_printDialogData.GetPrintData() = genericPrintSetupDialog.GetPrintData();
262 }
263
264 // Restore the wxPrintData settings again (uncomment if any settings become
265 // common to both dialogs)
266 // TransferDataToWindow();
267
268 return ret;
269 }
270 else
271 {
272 return wxDialog::ShowModal();
273 }
274 }
275
276 wxGenericPrintDialog::~wxGenericPrintDialog()
277 {
278 }
279
280 void wxGenericPrintDialog::OnOK(wxCommandEvent& WXUNUSED(event))
281 {
282 TransferDataFromWindow();
283
284 // An empty 'to' field signals printing just the
285 // 'from' page.
286 if (m_printDialogData.GetToPage() < 1)
287 m_printDialogData.SetToPage(m_printDialogData.GetFromPage());
288
289 wxPostScriptPrintNativeData *data =
290 (wxPostScriptPrintNativeData *) m_printDialogData.GetPrintData().GetNativeData();
291
292 // There are some interactions between the global setup data
293 // and the standard print dialog. The global printing 'mode'
294 // is determined by whether the user checks Print to file
295 // or not.
296 if (m_printDialogData.GetPrintToFile())
297 {
298 data->SetPrintMode(wxPRINT_MODE_FILE);
299
300 wxFileName fname( m_printDialogData.GetPrintData().GetFilename() );
301
302 wxFileDialog dialog( this, _("PostScript file"),
303 fname.GetPath(), fname.GetFullName(), wxT("*.ps"), wxOPEN | wxOVERWRITE_PROMPT );
304 if (dialog.ShowModal() != wxID_OK) return;
305
306 m_printDialogData.GetPrintData().SetFilename( dialog.GetPath() );
307 }
308 else
309 {
310 data->SetPrintMode(wxPRINT_MODE_PRINTER);
311 }
312
313 EndModal(wxID_OK);
314 }
315
316 void wxGenericPrintDialog::OnRange(wxCommandEvent& event)
317 {
318 if (!m_fromText) return;
319
320 if (event.GetInt() == 0)
321 {
322 m_fromText->Enable(false);
323 m_toText->Enable(false);
324 }
325 else if (event.GetInt() == 1)
326 {
327 m_fromText->Enable(true);
328 m_toText->Enable(true);
329 }
330 }
331
332 void wxGenericPrintDialog::OnSetup(wxCommandEvent& WXUNUSED(event))
333 {
334 wxGenericPrintSetupDialog dialog( this, &m_printDialogData.GetPrintData() );
335 if (dialog.ShowModal() != wxID_CANCEL)
336 {
337 m_printDialogData = dialog.GetPrintData();
338 }
339 }
340
341 bool wxGenericPrintDialog::TransferDataToWindow()
342 {
343 if(m_printDialogData.GetFromPage() != 0)
344 {
345 if(m_fromText)
346 {
347 if (m_printDialogData.GetEnablePageNumbers())
348 {
349 m_fromText->Enable(true);
350 m_toText->Enable(true);
351 if (m_printDialogData.GetFromPage() > 0)
352 m_fromText->SetValue(wxString::Format(_T("%d"), m_printDialogData.GetFromPage()));
353 if (m_printDialogData.GetToPage() > 0)
354 m_toText->SetValue(wxString::Format(_T("%d"), m_printDialogData.GetToPage()));
355 if(m_rangeRadioBox)
356 if (m_printDialogData.GetAllPages() || m_printDialogData.GetFromPage() == 0)
357 m_rangeRadioBox->SetSelection(0);
358 else
359 m_rangeRadioBox->SetSelection(1);
360 }
361 else
362 {
363 m_fromText->Enable(false);
364 m_toText->Enable(false);
365 if(m_rangeRadioBox)
366 {
367 m_rangeRadioBox->SetSelection(0);
368 m_rangeRadioBox->wxRadioBox::Enable(1, false);
369 }
370 }
371 }
372 }
373 m_noCopiesText->SetValue(
374 wxString::Format(_T("%d"), m_printDialogData.GetNoCopies()));
375
376 m_printToFileCheckBox->SetValue(m_printDialogData.GetPrintToFile());
377 m_printToFileCheckBox->Enable(m_printDialogData.GetEnablePrintToFile());
378 return true;
379 }
380
381 bool wxGenericPrintDialog::TransferDataFromWindow()
382 {
383 long res = 0;
384 if(m_printDialogData.GetFromPage() != -1)
385 {
386 if (m_printDialogData.GetEnablePageNumbers())
387 {
388 if(m_fromText)
389 {
390 wxString value = m_fromText->GetValue();
391 if (value.ToLong( &res ))
392 m_printDialogData.SetFromPage( res );
393 }
394 if(m_toText)
395 {
396 wxString value = m_toText->GetValue();
397 if (value.ToLong( &res ))
398 m_printDialogData.SetToPage( res );
399 }
400 }
401 if(m_rangeRadioBox)
402 {
403 if (m_rangeRadioBox->GetSelection() == 0)
404 m_printDialogData.SetAllPages(true);
405 else
406 m_printDialogData.SetAllPages(false);
407 }
408 }
409 else
410 { // continuous printing
411 m_printDialogData.SetFromPage(1);
412 m_printDialogData.SetToPage(32000);
413 }
414
415 wxString value = m_noCopiesText->GetValue();
416 if (value.ToLong( &res ))
417 m_printDialogData.SetNoCopies( res );
418
419 m_printDialogData.SetPrintToFile(m_printToFileCheckBox->GetValue());
420
421 return true;
422 }
423
424 /*
425 TODO: collate and noCopies should be duplicated across dialog data and print data objects
426 (slightly different semantics on Windows but let's ignore this for a bit).
427 */
428
429 wxDC *wxGenericPrintDialog::GetPrintDC()
430 {
431 // return new wxPostScriptDC(wxThePrintSetupData->GetPrinterFile(), false, (wxWindow *) NULL);
432 return new wxPostScriptDC(GetPrintDialogData().GetPrintData());
433 }
434
435 // ----------------------------------------------------------------------------
436 // Generic print setup dialog
437 // ----------------------------------------------------------------------------
438
439 IMPLEMENT_CLASS(wxGenericPrintSetupDialog, wxDialog)
440
441 wxGenericPrintSetupDialog::wxGenericPrintSetupDialog(wxWindow *parent, wxPrintData* data):
442 wxDialog(parent, wxID_ANY, _("Print Setup"), wxPoint(0, 0), wxSize(600, 600), wxDEFAULT_DIALOG_STYLE|wxTAB_TRAVERSAL)
443 {
444 Init(data);
445 }
446
447 void wxGenericPrintSetupDialog::Init(wxPrintData* data)
448 {
449 if ( data )
450 m_printData = *data;
451
452 int staticBoxWidth = 300;
453
454 (void) new wxStaticBox(this, wxPRINTID_STATIC, _("Paper size"), wxPoint(10, 10), wxSize(staticBoxWidth, 60) );
455
456 int xPos = 20;
457 int yPos = 30;
458 m_paperTypeChoice = CreatePaperTypeChoice(&xPos, &yPos);
459
460 wxString *choices = new wxString[2];
461 choices[0] = _("Portrait");
462 choices[1] = _("Landscape");
463
464 m_orientationRadioBox = new wxRadioBox(this, wxPRINTID_ORIENTATION, _("Orientation"),
465 wxPoint(10, 80), wxDefaultSize, 2, choices, 1, wxRA_VERTICAL );
466 m_orientationRadioBox->SetSelection(0);
467
468 (void) new wxStaticBox(this, wxPRINTID_STATIC, _("Options"), wxPoint(10, 130), wxSize(staticBoxWidth, 50) );
469
470 int colourYPos = 145;
471
472 #ifdef __WXMOTIF__
473 colourYPos = 150;
474 #endif
475
476 m_colourCheckBox = new wxCheckBox(this, wxPRINTID_PRINTCOLOUR, _("Print in colour"), wxPoint(15, colourYPos));
477
478 (void) new wxStaticBox(this, wxPRINTID_STATIC, _("Print spooling"), wxPoint(330, 10), wxSize(200,170) );
479
480 (void) new wxStaticText(this, wxPRINTID_STATIC, _("Printer command:"), wxPoint(340, 30));
481
482 m_printerCommandText = new wxTextCtrl(this, wxPRINTID_COMMAND, wxEmptyString, wxPoint(360, 55), wxSize(150, wxDefaultCoord));
483
484 (void) new wxStaticText(this, wxPRINTID_STATIC, _("Printer options:"), wxPoint(340, 110));
485
486 m_printerOptionsText = new wxTextCtrl(this, wxPRINTID_OPTIONS, wxEmptyString, wxPoint(360, 135), wxSize(150, wxDefaultCoord));
487
488 wxButton *okButton = new wxButton(this, wxID_OK, _("OK"), wxPoint(130, 200), wxSize(80, wxDefaultCoord));
489 (void) new wxButton(this, wxID_CANCEL, _("Cancel"), wxPoint(320, 200), wxSize(80, wxDefaultCoord));
490
491 okButton->SetDefault();
492 okButton->SetFocus();
493
494 Fit();
495 Centre(wxBOTH);
496
497 InitDialog();
498 delete[] choices;
499 }
500
501 wxGenericPrintSetupDialog::~wxGenericPrintSetupDialog()
502 {
503 }
504
505 bool wxGenericPrintSetupDialog::TransferDataToWindow()
506 {
507 wxPostScriptPrintNativeData *data =
508 (wxPostScriptPrintNativeData *) m_printData.GetNativeData();
509
510 if (m_printerCommandText && data->GetPrinterCommand())
511 m_printerCommandText->SetValue(data->GetPrinterCommand());
512 if (m_printerOptionsText && data->GetPrinterOptions())
513 m_printerOptionsText->SetValue(data->GetPrinterOptions());
514 if (m_colourCheckBox)
515 m_colourCheckBox->SetValue(m_printData.GetColour());
516
517 if (m_orientationRadioBox)
518 {
519 if (m_printData.GetOrientation() == wxPORTRAIT)
520 m_orientationRadioBox->SetSelection(0);
521 else
522 m_orientationRadioBox->SetSelection(1);
523 }
524 return true;
525 }
526
527 bool wxGenericPrintSetupDialog::TransferDataFromWindow()
528 {
529 wxPostScriptPrintNativeData *data =
530 (wxPostScriptPrintNativeData *) m_printData.GetNativeData();
531
532 if (m_printerCommandText)
533 data->SetPrinterCommand(m_printerCommandText->GetValue());
534 if (m_printerOptionsText)
535 data->SetPrinterOptions(m_printerOptionsText->GetValue());
536 if (m_colourCheckBox)
537 m_printData.SetColour(m_colourCheckBox->GetValue());
538 if (m_orientationRadioBox)
539 {
540 int sel = m_orientationRadioBox->GetSelection();
541 if (sel == 0)
542 m_printData.SetOrientation(wxPORTRAIT);
543 else
544 m_printData.SetOrientation(wxLANDSCAPE);
545 }
546 if (m_paperTypeChoice)
547 {
548 int selectedItem = m_paperTypeChoice->GetSelection();
549 if (selectedItem != -1)
550 {
551 wxPrintPaperType *paper = wxThePrintPaperDatabase->Item(selectedItem);
552 if (paper != NULL)
553 m_printData.SetPaperId( paper->GetId());
554 }
555 }
556
557 return true;
558 }
559
560 wxComboBox *wxGenericPrintSetupDialog::CreatePaperTypeChoice(int *x, int *y)
561 {
562 /* Should not be necessary
563 if (!wxThePrintPaperDatabase)
564 {
565 wxThePrintPaperDatabase = new wxPrintPaperDatabase;
566 wxThePrintPaperDatabase->CreateDatabase();
567 }
568 */
569 size_t n = wxThePrintPaperDatabase->GetCount();
570 wxString *choices = new wxString [n];
571 size_t sel = 0;
572
573 for (size_t i = 0; i < n; i++)
574 {
575 wxPrintPaperType *paper = wxThePrintPaperDatabase->Item(i);
576 choices[i] = paper->GetName();
577 if (m_printData.GetPaperId() == paper->GetId())
578 sel = i;
579 }
580
581 int width = 250;
582
583 wxComboBox *choice = new wxComboBox( this,
584 wxPRINTID_PAPERSIZE,
585 _("Paper Size"),
586 wxPoint(*x, *y),
587 wxSize(width, wxDefaultCoord),
588 n, choices );
589
590 // SetFont(thisFont);
591
592 delete[] choices;
593
594 choice->SetSelection(sel);
595 return choice;
596 }
597 #endif // wxUSE_POSTSCRIPT
598
599 // ----------------------------------------------------------------------------
600 // Generic page setup dialog
601 // ----------------------------------------------------------------------------
602
603 IMPLEMENT_CLASS(wxGenericPageSetupDialog, wxDialog)
604
605 BEGIN_EVENT_TABLE(wxGenericPageSetupDialog, wxDialog)
606 EVT_BUTTON(wxPRINTID_SETUP, wxGenericPageSetupDialog::OnPrinter)
607 END_EVENT_TABLE()
608
609 void wxGenericPageSetupDialog::OnPrinter(wxCommandEvent& WXUNUSED(event))
610 {
611 // We no longer query GetPrintMode, so we can eliminate the need
612 // to call SetPrintMode.
613 // This has the limitation that we can't explicitly call the PostScript
614 // print setup dialog from the generic Page Setup dialog under Windows,
615 // but since this choice would only happen when trying to do PostScript
616 // printing under Windows (and only in 16-bit Windows which
617 // doesn't have a Windows-specific page setup dialog) it's worth it.
618
619 // First save the current settings, so the wxPrintData object is up to date.
620 TransferDataFromWindow();
621
622 // Transfer the current print settings from this dialog to the page setup dialog.
623 wxPrintDialogData data;
624 data = GetPageSetupData().GetPrintData();
625 data.SetSetupDialog(true);
626 wxPrintDialog printDialog(this, & data);
627 printDialog.ShowModal();
628
629 // Transfer the page setup print settings from the page dialog to this dialog again, in case
630 // the page setup dialog changed something.
631 GetPageSetupData().GetPrintData() = printDialog.GetPrintDialogData().GetPrintData();
632 GetPageSetupData().CalculatePaperSizeFromId(); // Make sure page size reflects the id in wxPrintData
633
634 // Now update the dialog in case the page setup dialog changed some of our settings.
635 TransferDataToWindow();
636 }
637
638 wxGenericPageSetupDialog::wxGenericPageSetupDialog( wxWindow *parent,
639 wxPageSetupData* data)
640 : wxDialog( parent,
641 wxID_ANY,
642 _("Page Setup"),
643 wxPoint(0, 0),
644 wxSize(600, 600),
645 wxDEFAULT_DIALOG_STYLE|wxTAB_TRAVERSAL )
646 {
647 if (data)
648 m_pageData = *data;
649
650 int textWidth = 80;
651
652 wxBoxSizer *mainsizer = new wxBoxSizer( wxVERTICAL );
653
654 // 1) top
655 wxStaticBoxSizer *topsizer = new wxStaticBoxSizer(
656 new wxStaticBox(this,wxPRINTID_STATIC, _("Paper size")), wxHORIZONTAL );
657
658 size_t n = wxThePrintPaperDatabase->GetCount();
659 wxString *choices = new wxString [n];
660
661 for (size_t i = 0; i < n; i++)
662 {
663 wxPrintPaperType *paper = wxThePrintPaperDatabase->Item(i);
664 choices[i] = paper->GetName();
665 }
666
667 m_paperTypeChoice = new wxComboBox( this,
668 wxPRINTID_PAPERSIZE,
669 _("Paper Size"),
670 wxDefaultPosition,
671 wxSize(300, wxDefaultCoord),
672 n, choices );
673 topsizer->Add( m_paperTypeChoice, 1, wxEXPAND|wxALL, 5 );
674 // m_paperTypeChoice->SetSelection(sel);
675
676 mainsizer->Add( topsizer, 0, wxTOP|wxLEFT|wxRIGHT | wxEXPAND, 10 );
677
678 // 2) middle sizer with radio box
679
680 wxString *choices2 = new wxString[2];
681 choices2[0] = _("Portrait");
682 choices2[1] = _("Landscape");
683 m_orientationRadioBox = new wxRadioBox(this, wxPRINTID_ORIENTATION, _("Orientation"),
684 wxDefaultPosition, wxDefaultSize, 2, choices2, 2);
685 m_orientationRadioBox->SetSelection(0);
686
687 mainsizer->Add( m_orientationRadioBox, 0, wxTOP|wxLEFT|wxRIGHT, 10 );
688
689 // 3) margins
690
691 wxBoxSizer *table = new wxBoxSizer( wxHORIZONTAL );
692
693 wxBoxSizer *column1 = new wxBoxSizer( wxVERTICAL );
694 column1->Add( new wxStaticText(this, wxPRINTID_STATIC, _("Left margin (mm):")),1,wxALL|wxALIGN_RIGHT,5 );
695 column1->Add( new wxStaticText(this, wxPRINTID_STATIC, _("Top margin (mm):")),1,wxALL|wxALIGN_RIGHT,5 );
696 table->Add( column1, 0, wxALL | wxEXPAND, 5 );
697
698 wxBoxSizer *column2 = new wxBoxSizer( wxVERTICAL );
699 m_marginLeftText = new wxTextCtrl(this, wxPRINTID_LEFTMARGIN, wxEmptyString, wxDefaultPosition, wxSize(textWidth, wxDefaultCoord));
700 m_marginTopText = new wxTextCtrl(this, wxPRINTID_TOPMARGIN, wxEmptyString, wxDefaultPosition, wxSize(textWidth, wxDefaultCoord));
701 column2->Add( m_marginLeftText, 1, wxALL, 5 );
702 column2->Add( m_marginTopText, 1, wxALL, 5 );
703 table->Add( column2, 0, wxRIGHT|wxTOP|wxBOTTOM | wxEXPAND, 5 );
704
705 wxBoxSizer *column3 = new wxBoxSizer( wxVERTICAL );
706 column3->Add( new wxStaticText(this, wxPRINTID_STATIC, _("Right margin (mm):")),1,wxALL|wxALIGN_RIGHT,5 );
707 column3->Add( new wxStaticText(this, wxPRINTID_STATIC, _("Bottom margin (mm):")),1,wxALL|wxALIGN_RIGHT,5 );
708 table->Add( column3, 0, wxALL | wxEXPAND, 5 );
709
710 wxBoxSizer *column4 = new wxBoxSizer( wxVERTICAL );
711 m_marginRightText = new wxTextCtrl(this, wxPRINTID_RIGHTMARGIN, wxEmptyString, wxDefaultPosition, wxSize(textWidth, wxDefaultCoord));
712 m_marginBottomText = new wxTextCtrl(this, wxPRINTID_BOTTOMMARGIN, wxEmptyString, wxDefaultPosition, wxSize(textWidth, wxDefaultCoord));
713 column4->Add( m_marginRightText, 1, wxALL, 5 );
714 column4->Add( m_marginBottomText, 1, wxALL, 5 );
715 table->Add( column4, 0, wxRIGHT|wxTOP|wxBOTTOM | wxEXPAND, 5 );
716
717 mainsizer->Add( table, 0 );
718
719 #if wxUSE_STATLINE
720 // 5) static line
721 mainsizer->Add( new wxStaticLine( this, wxID_ANY ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 );
722 #endif
723
724 // 6) buttons
725
726 wxSizer* buttonsizer = CreateButtonSizer( wxOK|wxCANCEL);
727 m_printerButton = new wxButton(this, wxPRINTID_SETUP, _("Printer...") );
728 buttonsizer->Add( m_printerButton, 0, wxLEFT|wxRIGHT, 10 );
729 if ( !m_pageData.GetEnablePrinter() )
730 m_printerButton->Enable(false);
731 // if (m_printData.GetEnableHelp())
732 // wxButton *helpButton = new wxButton(this, (wxFunction)wxGenericPageSetupHelpProc, _("Help"), wxDefaultCoord, wxDefaultCoord, buttonWidth, buttonHeight);
733 mainsizer->Add( buttonsizer, 0, wxCENTER|wxALL, 10 );
734
735
736 SetAutoLayout( true );
737 SetSizer( mainsizer );
738
739 mainsizer->Fit( this );
740 Centre(wxBOTH);
741
742 InitDialog();
743
744 delete[] choices;
745 delete [] choices2;
746 }
747
748 wxGenericPageSetupDialog::~wxGenericPageSetupDialog()
749 {
750 }
751
752 bool wxGenericPageSetupDialog::TransferDataToWindow()
753 {
754 if (m_marginLeftText)
755 m_marginLeftText->SetValue(wxString::Format(wxT("%d"), m_pageData.GetMarginTopLeft().x));
756 if (m_marginTopText)
757 m_marginTopText->SetValue(wxString::Format(wxT("%d"), m_pageData.GetMarginTopLeft().y));
758 if (m_marginRightText)
759 m_marginRightText->SetValue(wxString::Format(wxT("%d"), m_pageData.GetMarginBottomRight().x));
760 if (m_marginBottomText)
761 m_marginBottomText->SetValue(wxString::Format(wxT("%d"), m_pageData.GetMarginBottomRight().y));
762
763 if (m_orientationRadioBox)
764 {
765 if (m_pageData.GetPrintData().GetOrientation() == wxPORTRAIT)
766 m_orientationRadioBox->SetSelection(0);
767 else
768 m_orientationRadioBox->SetSelection(1);
769 }
770
771 // Find the paper type from either the current paper size in the wxPageSetupDialogData, or
772 // failing that, the id in the wxPrintData object.
773
774 wxPrintPaperType* type = wxThePrintPaperDatabase->FindPaperType(
775 wxSize(m_pageData.GetPaperSize().x * 10, m_pageData.GetPaperSize().y * 10));
776
777 if (!type && m_pageData.GetPrintData().GetPaperId() != wxPAPER_NONE)
778 type = wxThePrintPaperDatabase->FindPaperType(m_pageData.GetPrintData().GetPaperId());
779
780 if (type)
781 {
782 m_paperTypeChoice->SetStringSelection(type->GetName());
783 }
784
785 return true;
786 }
787
788 bool wxGenericPageSetupDialog::TransferDataFromWindow()
789 {
790 if (m_marginLeftText && m_marginTopText)
791 {
792 int left = wxAtoi( m_marginLeftText->GetValue().c_str() );
793 int top = wxAtoi( m_marginTopText->GetValue().c_str() );
794 m_pageData.SetMarginTopLeft( wxPoint(left,top) );
795 }
796 if (m_marginRightText && m_marginBottomText)
797 {
798 int right = wxAtoi( m_marginRightText->GetValue().c_str() );
799 int bottom = wxAtoi( m_marginBottomText->GetValue().c_str() );
800 m_pageData.SetMarginBottomRight( wxPoint(right,bottom) );
801 }
802
803 if (m_orientationRadioBox)
804 {
805 int sel = m_orientationRadioBox->GetSelection();
806 if (sel == 0)
807 {
808 m_pageData.GetPrintData().SetOrientation(wxPORTRAIT);
809 }
810 else
811 {
812 m_pageData.GetPrintData().SetOrientation(wxLANDSCAPE);
813 }
814 }
815
816 if (m_paperTypeChoice)
817 {
818 int selectedItem = m_paperTypeChoice->GetSelection();
819 if (selectedItem != -1)
820 {
821 wxPrintPaperType *paper = wxThePrintPaperDatabase->Item(selectedItem);
822 if ( paper )
823 {
824 m_pageData.SetPaperSize(wxSize(paper->GetWidth()/10, paper->GetHeight()/10));
825 m_pageData.GetPrintData().SetPaperId(paper->GetId());
826 }
827 }
828 }
829
830 return true;
831 }
832
833 wxComboBox *wxGenericPageSetupDialog::CreatePaperTypeChoice(int *x, int *y)
834 {
835 /*
836 if (!wxThePrintPaperDatabase)
837 {
838 wxThePrintPaperDatabase = new wxPrintPaperDatabase;
839 wxThePrintPaperDatabase->CreateDatabase();
840 }
841 */
842
843 size_t n = wxThePrintPaperDatabase->GetCount();
844 wxString *choices = new wxString [n];
845
846 for (size_t i = 0; i < n; i++)
847 {
848 wxPrintPaperType *paper = wxThePrintPaperDatabase->Item(i);
849 choices[i] = paper->GetName();
850 }
851
852 (void) new wxStaticText(this, wxPRINTID_STATIC, _("Paper size"), wxPoint(*x, *y));
853 *y += 25;
854
855 wxComboBox *choice = new wxComboBox( this,
856 wxPRINTID_PAPERSIZE,
857 _("Paper Size"),
858 wxPoint(*x, *y),
859 wxSize(300, wxDefaultCoord),
860 n, choices );
861 *y += 35;
862 delete[] choices;
863
864 // choice->SetSelection(sel);
865 return choice;
866 }
867
868 #endif
869