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