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