Next phase of Print Factory code.
[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 if (factory->HasOwnPrintToFile())
181 m_printToFileCheckBox->Enable( false );
182
183 m_setupButton = new wxButton(this, wxPRINTID_SETUP, _("Setup...") );
184 flex->Add( m_setupButton, 0, wxALIGN_CENTER_VERTICAL|wxALIGN_RIGHT|wxALL, 5 );
185
186 if (!factory->HasPrintSetupDialog())
187 m_setupButton->Enable( false );
188
189 if (factory->HasPrinterLine())
190 {
191 flex->Add( new wxStaticText( this, -1, _("Printer:") ),
192 0, wxALIGN_CENTER_VERTICAL|wxALL, 5 );
193 flex->Add( new wxStaticText( this, -1, factory->CreatePrinterLine() ),
194 0, wxALIGN_CENTER_VERTICAL|wxALL, 5 );
195 }
196
197 if (factory->HasStatusLine())
198 {
199 flex->Add( new wxStaticText( this, -1, _("Status:") ),
200 0, wxALIGN_CENTER_VERTICAL|wxALL-wxTOP, 5 );
201 flex->Add( new wxStaticText( this, -1, factory->CreateStatusLine() ),
202 0, wxALIGN_CENTER_VERTICAL|wxALL-wxTOP, 5 );
203 }
204
205 mainsizer->Add( topsizer, 0, wxLEFT|wxTOP|wxRIGHT|wxGROW, 10 );
206
207 // 2) middle row with radio box
208
209 wxString *choices = new wxString[2];
210 choices[0] = _("All");
211 choices[1] = _("Pages");
212
213 m_fromText = (wxTextCtrl*)NULL;
214 m_toText = (wxTextCtrl*)NULL;
215 m_rangeRadioBox = (wxRadioBox *)NULL;
216
217 if (m_printDialogData.GetFromPage() != 0)
218 {
219 m_rangeRadioBox = new wxRadioBox(this, wxPRINTID_RANGE, _("Print Range"),
220 wxDefaultPosition, wxDefaultSize,
221 2, choices,
222 1, wxRA_VERTICAL);
223 m_rangeRadioBox->SetSelection(1);
224
225 mainsizer->Add( m_rangeRadioBox, 0, wxLEFT|wxTOP|wxRIGHT, 10 );
226 }
227
228 // 3) bottom row
229
230 wxBoxSizer *bottomsizer = new wxBoxSizer( wxHORIZONTAL );
231
232 if (m_printDialogData.GetFromPage() != 0)
233 {
234 bottomsizer->Add( new wxStaticText(this, wxPRINTID_STATIC, _("From:") ), 0, wxCENTER|wxALL, 5 );
235 m_fromText = new wxTextCtrl(this, wxPRINTID_FROM, wxEmptyString, wxDefaultPosition, wxSize(40, wxDefaultCoord));
236 bottomsizer->Add( m_fromText, 1, wxCENTER|wxRIGHT, 10 );
237
238 bottomsizer->Add( new wxStaticText(this, wxPRINTID_STATIC, _("To:") ), 0, wxCENTER|wxALL, 5);
239 m_toText = new wxTextCtrl(this, wxPRINTID_TO, wxEmptyString, wxDefaultPosition, wxSize(40, wxDefaultCoord));
240 bottomsizer->Add( m_toText, 1, wxCENTER|wxRIGHT, 10 );
241 }
242
243 bottomsizer->Add( new wxStaticText(this, wxPRINTID_STATIC, _("Copies:") ), 0, wxCENTER|wxALL, 5 );
244 m_noCopiesText = new wxTextCtrl(this, wxPRINTID_COPIES, wxEmptyString, wxPoint(252, 130), wxSize(40, wxDefaultCoord));
245 bottomsizer->Add( m_noCopiesText, 1, wxCENTER|wxRIGHT, 10 );
246
247 mainsizer->Add( bottomsizer, 0, wxTOP|wxLEFT|wxRIGHT, 12 );
248
249 #if wxUSE_STATLINE
250 // 4) static line
251 mainsizer->Add( new wxStaticLine( this, wxID_ANY ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 );
252 #endif
253
254 // 5) buttons
255
256 mainsizer->Add( CreateButtonSizer( wxOK|wxCANCEL), 0, wxCENTER|wxALL, 10 );
257
258 SetAutoLayout( true );
259 SetSizer( mainsizer );
260
261 mainsizer->Fit( this );
262 Centre(wxBOTH);
263
264 // Calls wxWindow::OnInitDialog and then wxGenericPrintDialog::TransferDataToWindow
265 InitDialog();
266 delete[] choices;
267 }
268
269 int wxGenericPrintDialog::ShowModal()
270 {
271 return wxDialog::ShowModal();
272 }
273
274 wxGenericPrintDialog::~wxGenericPrintDialog()
275 {
276 }
277
278 void wxGenericPrintDialog::OnOK(wxCommandEvent& WXUNUSED(event))
279 {
280 TransferDataFromWindow();
281
282 // An empty 'to' field signals printing just the
283 // 'from' page.
284 if (m_printDialogData.GetToPage() < 1)
285 m_printDialogData.SetToPage(m_printDialogData.GetFromPage());
286
287 // There are some interactions between the global setup data
288 // and the standard print dialog. The global printing 'mode'
289 // is determined by whether the user checks Print to file
290 // or not.
291 if (m_printDialogData.GetPrintToFile())
292 {
293 m_printDialogData.GetPrintData().SetPrintMode(wxPRINT_MODE_FILE);
294
295 wxFileName fname( m_printDialogData.GetPrintData().GetFilename() );
296
297 wxFileDialog dialog( this, _("PostScript file"),
298 fname.GetPath(), fname.GetFullName(), wxT("*.ps"), wxOPEN | wxOVERWRITE_PROMPT );
299 if (dialog.ShowModal() != wxID_OK) return;
300
301 m_printDialogData.GetPrintData().SetFilename( dialog.GetPath() );
302 }
303 else
304 {
305 m_printDialogData.GetPrintData().SetPrintMode(wxPRINT_MODE_PRINTER);
306 }
307
308 EndModal(wxID_OK);
309 }
310
311 void wxGenericPrintDialog::OnRange(wxCommandEvent& event)
312 {
313 if (!m_fromText) return;
314
315 if (event.GetInt() == 0)
316 {
317 m_fromText->Enable(false);
318 m_toText->Enable(false);
319 }
320 else if (event.GetInt() == 1)
321 {
322 m_fromText->Enable(true);
323 m_toText->Enable(true);
324 }
325 }
326
327 void wxGenericPrintDialog::OnSetup(wxCommandEvent& WXUNUSED(event))
328 {
329 wxPrintFactory* factory = wxPrintFactory::GetFactory();
330
331 if (factory->HasPrintSetupDialog())
332 {
333 // The print setup dialog should change the
334 // print data in-place if not cancelled.
335 wxDialog *dialog = factory->CreatePrintSetupDialog( this, &m_printDialogData.GetPrintData() );
336 dialog->ShowModal();
337 dialog->Destroy();
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(GetPrintDialogData().GetPrintData());
432 }
433
434 // ----------------------------------------------------------------------------
435 // Generic print setup dialog
436 // ----------------------------------------------------------------------------
437
438 IMPLEMENT_CLASS(wxGenericPrintSetupDialog, wxDialog)
439
440 wxGenericPrintSetupDialog::wxGenericPrintSetupDialog(wxWindow *parent, wxPrintData* data):
441 wxDialog(parent, wxID_ANY, _("Print Setup"), wxPoint(0, 0), wxSize(600, 600), wxDEFAULT_DIALOG_STYLE|wxTAB_TRAVERSAL)
442 {
443 Init(data);
444 }
445
446 void wxGenericPrintSetupDialog::Init(wxPrintData* data)
447 {
448 if ( data )
449 m_printData = *data;
450
451
452 wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL );
453
454 wxBoxSizer *item1 = new wxBoxSizer( wxHORIZONTAL );
455 main_sizer->Add( item1, 0, wxALL, 5 );
456
457 // printer options (on the left)
458
459 wxBoxSizer *item2 = new wxBoxSizer( wxVERTICAL );
460
461 wxStaticBox *item4 = new wxStaticBox( this, wxPRINTID_STATIC, _("Paper size") );
462 wxStaticBoxSizer *item3 = new wxStaticBoxSizer( item4, wxVERTICAL );
463
464 m_paperTypeChoice = CreatePaperTypeChoice();
465 item3->Add( m_paperTypeChoice, 0, wxALIGN_CENTER|wxALL, 5 );
466
467 item2->Add( item3, 0, wxALIGN_CENTER|wxALL, 5 );
468
469 wxString strs6[] =
470 {
471 _("Portrait"),
472 _("Landscape")
473 };
474 m_orientationRadioBox= new wxRadioBox( this, wxPRINTID_ORIENTATION, _("Orientation"), wxDefaultPosition, wxDefaultSize, 2, strs6, 1, wxRA_SPECIFY_ROWS );
475 item2->Add( m_orientationRadioBox, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
476
477 wxStaticBox *item8 = new wxStaticBox( this, -1, _("Options") );
478 wxStaticBoxSizer *item7 = new wxStaticBoxSizer( item8, wxHORIZONTAL );
479
480 m_colourCheckBox = new wxCheckBox( this, wxPRINTID_PRINTCOLOUR, _("Print in colour") );
481 item7->Add( m_colourCheckBox, 0, wxALIGN_CENTER|wxALL, 5 );
482
483 item2->Add( item7, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
484
485 item1->Add( item2, 0, wxALIGN_CENTER_HORIZONTAL, 5 );
486
487 // spooling options (on the right)
488
489 wxStaticBox *item11 = new wxStaticBox( this, -1, _("Print spooling") );
490 wxStaticBoxSizer *item10 = new wxStaticBoxSizer( item11, wxVERTICAL );
491
492 wxStaticText *item12 = new wxStaticText( this, -1, _("Printer command:") );
493 item10->Add( item12, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5 );
494
495 wxBoxSizer *item13 = new wxBoxSizer( wxHORIZONTAL );
496
497 item13->Add( 20, 20, 0, wxALIGN_CENTER|wxALL, 5 );
498
499 m_printerCommandText = new wxTextCtrl( this, wxPRINTID_COMMAND, wxT(""), wxDefaultPosition, wxSize(160,-1) );
500 item13->Add( m_printerCommandText, 0, wxALIGN_CENTER|wxALL, 5 );
501
502 item10->Add( item13, 0, wxALIGN_CENTER|wxALL, 0 );
503
504 wxStaticText *item15 = new wxStaticText( this, -1, _("Printer options:") );
505 item10->Add( item15, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5 );
506
507 wxBoxSizer *item16 = new wxBoxSizer( wxHORIZONTAL );
508
509 item16->Add( 20, 20, 0, wxALIGN_CENTER|wxALL, 5 );
510
511 m_printerOptionsText = new wxTextCtrl( this, wxPRINTID_OPTIONS, wxT(""), wxDefaultPosition, wxSize(160,-1) );
512 item16->Add( m_printerOptionsText, 0, wxALIGN_CENTER|wxALL, 5 );
513
514 item10->Add( item16, 0, wxALIGN_CENTER|wxALL, 0 );
515
516 item1->Add( item10, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5 );
517
518
519 #if wxUSE_STATLINE
520 // static line
521 main_sizer->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 );
522 #endif
523
524 // buttons
525
526 main_sizer->Add( CreateButtonSizer( wxOK|wxCANCEL), 0, wxCENTER|wxALL, 10 );
527
528 SetAutoLayout( true );
529 SetSizer( main_sizer );
530
531 main_sizer->Fit( this );
532 Centre(wxBOTH);
533
534
535 Fit();
536 Centre(wxBOTH);
537
538 InitDialog();
539 }
540
541 wxGenericPrintSetupDialog::~wxGenericPrintSetupDialog()
542 {
543 }
544
545 bool wxGenericPrintSetupDialog::TransferDataToWindow()
546 {
547 wxPostScriptPrintNativeData *data =
548 (wxPostScriptPrintNativeData *) m_printData.GetNativeData();
549
550 if (m_printerCommandText && data->GetPrinterCommand())
551 m_printerCommandText->SetValue(data->GetPrinterCommand());
552 if (m_printerOptionsText && data->GetPrinterOptions())
553 m_printerOptionsText->SetValue(data->GetPrinterOptions());
554 if (m_colourCheckBox)
555 m_colourCheckBox->SetValue(m_printData.GetColour());
556
557 if (m_orientationRadioBox)
558 {
559 if (m_printData.GetOrientation() == wxPORTRAIT)
560 m_orientationRadioBox->SetSelection(0);
561 else
562 m_orientationRadioBox->SetSelection(1);
563 }
564 return true;
565 }
566
567 bool wxGenericPrintSetupDialog::TransferDataFromWindow()
568 {
569 wxPostScriptPrintNativeData *data =
570 (wxPostScriptPrintNativeData *) m_printData.GetNativeData();
571
572 if (m_printerCommandText)
573 data->SetPrinterCommand(m_printerCommandText->GetValue());
574 if (m_printerOptionsText)
575 data->SetPrinterOptions(m_printerOptionsText->GetValue());
576 if (m_colourCheckBox)
577 m_printData.SetColour(m_colourCheckBox->GetValue());
578 if (m_orientationRadioBox)
579 {
580 int sel = m_orientationRadioBox->GetSelection();
581 if (sel == 0)
582 m_printData.SetOrientation(wxPORTRAIT);
583 else
584 m_printData.SetOrientation(wxLANDSCAPE);
585 }
586 if (m_paperTypeChoice)
587 {
588 int selectedItem = m_paperTypeChoice->GetSelection();
589 if (selectedItem != -1)
590 {
591 wxPrintPaperType *paper = wxThePrintPaperDatabase->Item(selectedItem);
592 if (paper != NULL)
593 m_printData.SetPaperId( paper->GetId());
594 }
595 }
596
597 return true;
598 }
599
600 wxComboBox *wxGenericPrintSetupDialog::CreatePaperTypeChoice()
601 {
602 size_t n = wxThePrintPaperDatabase->GetCount();
603 wxString *choices = new wxString [n];
604 size_t sel = 0;
605
606 for (size_t i = 0; i < n; i++)
607 {
608 wxPrintPaperType *paper = wxThePrintPaperDatabase->Item(i);
609 choices[i] = paper->GetName();
610 if (m_printData.GetPaperId() == paper->GetId())
611 sel = i;
612 }
613
614 int width = 250;
615
616 wxComboBox *choice = new wxComboBox( this,
617 wxPRINTID_PAPERSIZE,
618 _("Paper Size"),
619 wxDefaultPosition,
620 wxSize(width, wxDefaultCoord),
621 n, choices );
622
623 delete[] choices;
624
625 choice->SetSelection(sel);
626 return choice;
627 }
628
629 #endif // wxUSE_POSTSCRIPT
630
631 // ----------------------------------------------------------------------------
632 // Generic page setup dialog
633 // ----------------------------------------------------------------------------
634
635 IMPLEMENT_CLASS(wxGenericPageSetupDialog, wxDialog)
636
637 BEGIN_EVENT_TABLE(wxGenericPageSetupDialog, wxDialog)
638 EVT_BUTTON(wxPRINTID_SETUP, wxGenericPageSetupDialog::OnPrinter)
639 END_EVENT_TABLE()
640
641 void wxGenericPageSetupDialog::OnPrinter(wxCommandEvent& WXUNUSED(event))
642 {
643 // We no longer query GetPrintMode, so we can eliminate the need
644 // to call SetPrintMode.
645 // This has the limitation that we can't explicitly call the PostScript
646 // print setup dialog from the generic Page Setup dialog under Windows,
647 // but since this choice would only happen when trying to do PostScript
648 // printing under Windows (and only in 16-bit Windows which
649 // doesn't have a Windows-specific page setup dialog) it's worth it.
650
651 // First save the current settings, so the wxPrintData object is up to date.
652 TransferDataFromWindow();
653
654 // Transfer the current print settings from this dialog to the page setup dialog.
655 wxPrintDialogData data;
656 data = GetPageSetupData().GetPrintData();
657 data.SetSetupDialog(true);
658 wxPrintDialog printDialog(this, & data);
659 printDialog.ShowModal();
660
661 // Transfer the page setup print settings from the page dialog to this dialog again, in case
662 // the page setup dialog changed something.
663 GetPageSetupData().GetPrintData() = printDialog.GetPrintDialogData().GetPrintData();
664 GetPageSetupData().CalculatePaperSizeFromId(); // Make sure page size reflects the id in wxPrintData
665
666 // Now update the dialog in case the page setup dialog changed some of our settings.
667 TransferDataToWindow();
668 }
669
670 wxGenericPageSetupDialog::wxGenericPageSetupDialog( wxWindow *parent,
671 wxPageSetupData* data)
672 : wxDialog( parent,
673 wxID_ANY,
674 _("Page Setup"),
675 wxPoint(0, 0),
676 wxSize(600, 600),
677 wxDEFAULT_DIALOG_STYLE|wxTAB_TRAVERSAL )
678 {
679 if (data)
680 m_pageData = *data;
681
682 int textWidth = 80;
683
684 wxBoxSizer *mainsizer = new wxBoxSizer( wxVERTICAL );
685
686 // 1) top
687 wxStaticBoxSizer *topsizer = new wxStaticBoxSizer(
688 new wxStaticBox(this,wxPRINTID_STATIC, _("Paper size")), wxHORIZONTAL );
689
690 size_t n = wxThePrintPaperDatabase->GetCount();
691 wxString *choices = new wxString [n];
692
693 for (size_t i = 0; i < n; i++)
694 {
695 wxPrintPaperType *paper = wxThePrintPaperDatabase->Item(i);
696 choices[i] = paper->GetName();
697 }
698
699 m_paperTypeChoice = new wxComboBox( this,
700 wxPRINTID_PAPERSIZE,
701 _("Paper Size"),
702 wxDefaultPosition,
703 wxSize(300, wxDefaultCoord),
704 n, choices );
705 topsizer->Add( m_paperTypeChoice, 1, wxEXPAND|wxALL, 5 );
706 // m_paperTypeChoice->SetSelection(sel);
707
708 mainsizer->Add( topsizer, 0, wxTOP|wxLEFT|wxRIGHT | wxEXPAND, 10 );
709
710 // 2) middle sizer with radio box
711
712 wxString *choices2 = new wxString[2];
713 choices2[0] = _("Portrait");
714 choices2[1] = _("Landscape");
715 m_orientationRadioBox = new wxRadioBox(this, wxPRINTID_ORIENTATION, _("Orientation"),
716 wxDefaultPosition, wxDefaultSize, 2, choices2, 2);
717 m_orientationRadioBox->SetSelection(0);
718
719 mainsizer->Add( m_orientationRadioBox, 0, wxTOP|wxLEFT|wxRIGHT, 10 );
720
721 // 3) margins
722
723 wxBoxSizer *table = new wxBoxSizer( wxHORIZONTAL );
724
725 wxBoxSizer *column1 = new wxBoxSizer( wxVERTICAL );
726 column1->Add( new wxStaticText(this, wxPRINTID_STATIC, _("Left margin (mm):")),1,wxALL|wxALIGN_RIGHT,5 );
727 column1->Add( new wxStaticText(this, wxPRINTID_STATIC, _("Top margin (mm):")),1,wxALL|wxALIGN_RIGHT,5 );
728 table->Add( column1, 0, wxALL | wxEXPAND, 5 );
729
730 wxBoxSizer *column2 = new wxBoxSizer( wxVERTICAL );
731 m_marginLeftText = new wxTextCtrl(this, wxPRINTID_LEFTMARGIN, wxEmptyString, wxDefaultPosition, wxSize(textWidth, wxDefaultCoord));
732 m_marginTopText = new wxTextCtrl(this, wxPRINTID_TOPMARGIN, wxEmptyString, wxDefaultPosition, wxSize(textWidth, wxDefaultCoord));
733 column2->Add( m_marginLeftText, 1, wxALL, 5 );
734 column2->Add( m_marginTopText, 1, wxALL, 5 );
735 table->Add( column2, 0, wxRIGHT|wxTOP|wxBOTTOM | wxEXPAND, 5 );
736
737 wxBoxSizer *column3 = new wxBoxSizer( wxVERTICAL );
738 column3->Add( new wxStaticText(this, wxPRINTID_STATIC, _("Right margin (mm):")),1,wxALL|wxALIGN_RIGHT,5 );
739 column3->Add( new wxStaticText(this, wxPRINTID_STATIC, _("Bottom margin (mm):")),1,wxALL|wxALIGN_RIGHT,5 );
740 table->Add( column3, 0, wxALL | wxEXPAND, 5 );
741
742 wxBoxSizer *column4 = new wxBoxSizer( wxVERTICAL );
743 m_marginRightText = new wxTextCtrl(this, wxPRINTID_RIGHTMARGIN, wxEmptyString, wxDefaultPosition, wxSize(textWidth, wxDefaultCoord));
744 m_marginBottomText = new wxTextCtrl(this, wxPRINTID_BOTTOMMARGIN, wxEmptyString, wxDefaultPosition, wxSize(textWidth, wxDefaultCoord));
745 column4->Add( m_marginRightText, 1, wxALL, 5 );
746 column4->Add( m_marginBottomText, 1, wxALL, 5 );
747 table->Add( column4, 0, wxRIGHT|wxTOP|wxBOTTOM | wxEXPAND, 5 );
748
749 mainsizer->Add( table, 0 );
750
751 #if wxUSE_STATLINE
752 // 5) static line
753 mainsizer->Add( new wxStaticLine( this, wxID_ANY ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 );
754 #endif
755
756 // 6) buttons
757
758 wxSizer* buttonsizer = CreateButtonSizer( wxOK|wxCANCEL);
759 m_printerButton = new wxButton(this, wxPRINTID_SETUP, _("Printer...") );
760 buttonsizer->Add( m_printerButton, 0, wxLEFT|wxRIGHT, 10 );
761 if ( !m_pageData.GetEnablePrinter() )
762 m_printerButton->Enable(false);
763 // if (m_printData.GetEnableHelp())
764 // wxButton *helpButton = new wxButton(this, (wxFunction)wxGenericPageSetupHelpProc, _("Help"), wxDefaultCoord, wxDefaultCoord, buttonWidth, buttonHeight);
765 mainsizer->Add( buttonsizer, 0, wxCENTER|wxALL, 10 );
766
767
768 SetAutoLayout( true );
769 SetSizer( mainsizer );
770
771 mainsizer->Fit( this );
772 Centre(wxBOTH);
773
774 InitDialog();
775
776 delete[] choices;
777 delete [] choices2;
778 }
779
780 wxGenericPageSetupDialog::~wxGenericPageSetupDialog()
781 {
782 }
783
784 bool wxGenericPageSetupDialog::TransferDataToWindow()
785 {
786 if (m_marginLeftText)
787 m_marginLeftText->SetValue(wxString::Format(wxT("%d"), m_pageData.GetMarginTopLeft().x));
788 if (m_marginTopText)
789 m_marginTopText->SetValue(wxString::Format(wxT("%d"), m_pageData.GetMarginTopLeft().y));
790 if (m_marginRightText)
791 m_marginRightText->SetValue(wxString::Format(wxT("%d"), m_pageData.GetMarginBottomRight().x));
792 if (m_marginBottomText)
793 m_marginBottomText->SetValue(wxString::Format(wxT("%d"), m_pageData.GetMarginBottomRight().y));
794
795 if (m_orientationRadioBox)
796 {
797 if (m_pageData.GetPrintData().GetOrientation() == wxPORTRAIT)
798 m_orientationRadioBox->SetSelection(0);
799 else
800 m_orientationRadioBox->SetSelection(1);
801 }
802
803 // Find the paper type from either the current paper size in the wxPageSetupDialogData, or
804 // failing that, the id in the wxPrintData object.
805
806 wxPrintPaperType* type = wxThePrintPaperDatabase->FindPaperType(
807 wxSize(m_pageData.GetPaperSize().x * 10, m_pageData.GetPaperSize().y * 10));
808
809 if (!type && m_pageData.GetPrintData().GetPaperId() != wxPAPER_NONE)
810 type = wxThePrintPaperDatabase->FindPaperType(m_pageData.GetPrintData().GetPaperId());
811
812 if (type)
813 {
814 m_paperTypeChoice->SetStringSelection(type->GetName());
815 }
816
817 return true;
818 }
819
820 bool wxGenericPageSetupDialog::TransferDataFromWindow()
821 {
822 if (m_marginLeftText && m_marginTopText)
823 {
824 int left = wxAtoi( m_marginLeftText->GetValue().c_str() );
825 int top = wxAtoi( m_marginTopText->GetValue().c_str() );
826 m_pageData.SetMarginTopLeft( wxPoint(left,top) );
827 }
828 if (m_marginRightText && m_marginBottomText)
829 {
830 int right = wxAtoi( m_marginRightText->GetValue().c_str() );
831 int bottom = wxAtoi( m_marginBottomText->GetValue().c_str() );
832 m_pageData.SetMarginBottomRight( wxPoint(right,bottom) );
833 }
834
835 if (m_orientationRadioBox)
836 {
837 int sel = m_orientationRadioBox->GetSelection();
838 if (sel == 0)
839 {
840 m_pageData.GetPrintData().SetOrientation(wxPORTRAIT);
841 }
842 else
843 {
844 m_pageData.GetPrintData().SetOrientation(wxLANDSCAPE);
845 }
846 }
847
848 if (m_paperTypeChoice)
849 {
850 int selectedItem = m_paperTypeChoice->GetSelection();
851 if (selectedItem != -1)
852 {
853 wxPrintPaperType *paper = wxThePrintPaperDatabase->Item(selectedItem);
854 if ( paper )
855 {
856 m_pageData.SetPaperSize(wxSize(paper->GetWidth()/10, paper->GetHeight()/10));
857 m_pageData.GetPrintData().SetPaperId(paper->GetId());
858 }
859 }
860 }
861
862 return true;
863 }
864
865 wxComboBox *wxGenericPageSetupDialog::CreatePaperTypeChoice(int *x, int *y)
866 {
867 /*
868 if (!wxThePrintPaperDatabase)
869 {
870 wxThePrintPaperDatabase = new wxPrintPaperDatabase;
871 wxThePrintPaperDatabase->CreateDatabase();
872 }
873 */
874
875 size_t n = wxThePrintPaperDatabase->GetCount();
876 wxString *choices = new wxString [n];
877
878 for (size_t i = 0; i < n; i++)
879 {
880 wxPrintPaperType *paper = wxThePrintPaperDatabase->Item(i);
881 choices[i] = paper->GetName();
882 }
883
884 (void) new wxStaticText(this, wxPRINTID_STATIC, _("Paper size"), wxPoint(*x, *y));
885 *y += 25;
886
887 wxComboBox *choice = new wxComboBox( this,
888 wxPRINTID_PAPERSIZE,
889 _("Paper Size"),
890 wxPoint(*x, *y),
891 wxSize(300, wxDefaultCoord),
892 n, choices );
893 *y += 35;
894 delete[] choices;
895
896 // choice->SetSelection(sel);
897 return choice;
898 }
899
900 #endif
901