]> git.saurik.com Git - wxWidgets.git/blob - src/generic/prntdlgg.cpp
new renderer architecture
[wxWidgets.git] / src / generic / prntdlgg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/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 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_PRINTING_ARCHITECTURE && (!defined(__WXMSW__) || wxUSE_POSTSCRIPT_ARCHITECTURE_IN_MSW)
28
29 #ifndef WX_PRECOMP
30 #include "wx/utils.h"
31 #include "wx/dc.h"
32 #include "wx/stattext.h"
33 #include "wx/statbox.h"
34 #include "wx/button.h"
35 #include "wx/checkbox.h"
36 #include "wx/textctrl.h"
37 #include "wx/radiobox.h"
38 #include "wx/filedlg.h"
39 #include "wx/combobox.h"
40 #include "wx/intl.h"
41 #include "wx/sizer.h"
42 #include "wx/cmndata.h"
43 #endif
44
45 #if wxUSE_STATLINE
46 #include "wx/statline.h"
47 #endif
48
49 #include "wx/generic/prntdlgg.h"
50
51 #if wxUSE_POSTSCRIPT
52 #include "wx/generic/dcpsg.h"
53 #endif
54
55 #include "wx/prntbase.h"
56 #include "wx/printdlg.h"
57 #include "wx/paper.h"
58 #include "wx/filename.h"
59 #include "wx/tokenzr.h"
60 #include "wx/imaglist.h"
61
62 #include <stdlib.h>
63 #include <string.h>
64
65 #if wxUSE_LIBGNOMEPRINT
66 #include "wx/html/forcelnk.h"
67 FORCE_LINK(gnome_print)
68 #endif
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 = wxEmptyString;
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 = wxEmptyString;
96 m_afmPath = wxT("c:\\windows\\system\\");
97 #endif
98
99 #if !defined(__VMS__) && !defined(__WXMSW__)
100 m_printerCommand = wxT("lpr");
101 m_printerOptions = wxEmptyString;
102 m_afmPath = wxEmptyString;
103 #endif
104
105 m_printerScaleX = 1.0;
106 m_printerScaleY = 1.0;
107 m_printerTranslateX = 0;
108 m_printerTranslateY = 0;
109 }
110
111 wxPostScriptPrintNativeData::~wxPostScriptPrintNativeData()
112 {
113 }
114
115 bool wxPostScriptPrintNativeData::TransferTo( wxPrintData &WXUNUSED(data) )
116 {
117 return true;
118 }
119
120 bool wxPostScriptPrintNativeData::TransferFrom( const wxPrintData &WXUNUSED(data) )
121 {
122 return true;
123 }
124
125 // ----------------------------------------------------------------------------
126 // Generic print dialog for non-Windows printing use.
127 // ----------------------------------------------------------------------------
128
129 IMPLEMENT_CLASS(wxGenericPrintDialog, wxPrintDialogBase)
130
131 BEGIN_EVENT_TABLE(wxGenericPrintDialog, wxPrintDialogBase)
132 EVT_BUTTON(wxID_OK, wxGenericPrintDialog::OnOK)
133 EVT_BUTTON(wxPRINTID_SETUP, wxGenericPrintDialog::OnSetup)
134 EVT_RADIOBOX(wxPRINTID_RANGE, wxGenericPrintDialog::OnRange)
135 END_EVENT_TABLE()
136
137 wxGenericPrintDialog::wxGenericPrintDialog(wxWindow *parent,
138 wxPrintDialogData* data)
139 : wxPrintDialogBase(parent, wxID_ANY, _("Print"),
140 wxPoint(0,0), wxSize(600, 600),
141 wxDEFAULT_DIALOG_STYLE |
142 wxTAB_TRAVERSAL)
143 {
144 if ( data )
145 m_printDialogData = *data;
146
147 Init(parent);
148 }
149
150 wxGenericPrintDialog::wxGenericPrintDialog(wxWindow *parent,
151 wxPrintData* data)
152 : wxPrintDialogBase(parent, wxID_ANY, _("Print"),
153 wxPoint(0,0), wxSize(600, 600),
154 wxDEFAULT_DIALOG_STYLE |
155 wxTAB_TRAVERSAL)
156 {
157 if ( data )
158 m_printDialogData = *data;
159
160 Init(parent);
161 }
162
163 void wxGenericPrintDialog::Init(wxWindow * WXUNUSED(parent))
164 {
165 // wxDialog::Create(parent, wxID_ANY, _("Print"), wxPoint(0,0), wxSize(600, 600),
166 // wxDEFAULT_DIALOG_STYLE | wxTAB_TRAVERSAL);
167
168 wxBoxSizer *mainsizer = new wxBoxSizer( wxVERTICAL );
169
170 // 1) top row
171
172 wxPrintFactory* factory = wxPrintFactory::GetFactory();
173
174 wxStaticBoxSizer *topsizer = new wxStaticBoxSizer(
175 new wxStaticBox( this, wxID_ANY, _( "Printer options" ) ), wxHORIZONTAL );
176 wxFlexGridSizer *flex = new wxFlexGridSizer( 2 );
177 flex->AddGrowableCol( 1 );
178 topsizer->Add( flex, 1, wxGROW );
179
180 m_printToFileCheckBox = new wxCheckBox( this, wxPRINTID_PRINTTOFILE, _("Print to File") );
181 flex->Add( m_printToFileCheckBox, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5 );
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, wxID_ANY, _("Printer:") ),
192 0, wxALIGN_CENTER_VERTICAL|wxALL, 5 );
193 flex->Add( new wxStaticText( this, wxID_ANY, factory->CreatePrinterLine() ),
194 0, wxALIGN_CENTER_VERTICAL|wxALL, 5 );
195 }
196
197 if (factory->HasStatusLine())
198 {
199 flex->Add( new wxStaticText( this, wxID_ANY, _("Status:") ),
200 0, wxALIGN_CENTER_VERTICAL|wxALL-wxTOP, 5 );
201 flex->Add( new wxStaticText( this, wxID_ANY, 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 // 4) buttons
250
251 wxSizer *sizerBtn = CreateSeparatedButtonSizer( wxOK|wxCANCEL);
252 if ( sizerBtn )
253 mainsizer->Add(sizerBtn, 0, wxEXPAND|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"), wxFD_SAVE | wxFD_OVERWRITE_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 {
402 m_printDialogData.SetAllPages(true);
403
404 // This means all pages, more or less
405 m_printDialogData.SetFromPage(1);
406 m_printDialogData.SetToPage(32000);
407 }
408 else
409 m_printDialogData.SetAllPages(false);
410 }
411 }
412 else
413 { // continuous printing
414 m_printDialogData.SetFromPage(1);
415 m_printDialogData.SetToPage(32000);
416 }
417
418 wxString value = m_noCopiesText->GetValue();
419 if (value.ToLong( &res ))
420 m_printDialogData.SetNoCopies( res );
421
422 m_printDialogData.SetPrintToFile(m_printToFileCheckBox->GetValue());
423
424 return true;
425 }
426
427 wxDC *wxGenericPrintDialog::GetPrintDC()
428 {
429 return new wxPostScriptDC(GetPrintDialogData().GetPrintData());
430 }
431
432 // ----------------------------------------------------------------------------
433 // Generic print setup dialog
434 // ----------------------------------------------------------------------------
435
436 IMPLEMENT_CLASS(wxGenericPrintSetupDialog, wxDialog)
437
438 BEGIN_EVENT_TABLE(wxGenericPrintSetupDialog, wxDialog)
439 EVT_LIST_ITEM_ACTIVATED(wxPRINTID_PRINTER, wxGenericPrintSetupDialog::OnPrinter)
440 END_EVENT_TABLE()
441
442 wxGenericPrintSetupDialog::wxGenericPrintSetupDialog(wxWindow *parent, wxPrintData* data):
443 wxDialog(parent, wxID_ANY, _("Print Setup"), wxPoint(0,0), wxSize(600, 600), wxDEFAULT_DIALOG_STYLE|wxTAB_TRAVERSAL)
444 {
445 Init(data);
446 }
447
448 /* XPM */
449 static const char * check_xpm[] = {
450 /* width height ncolors chars_per_pixel */
451 "16 16 3 1",
452 /* colors */
453 " s None c None",
454 "X c #000000",
455 ". c #808080",
456 /* pixels */
457 " ",
458 " ",
459 " ",
460 " .. ",
461 " XX ",
462 " XX. ",
463 " .XX ",
464 " XX ",
465 " X XX. ",
466 " XX .XX ",
467 " XX XX ",
468 " XXXX. ",
469 " XX. ",
470 " . ",
471 " ",
472 " "
473 };
474
475
476 void wxGenericPrintSetupDialog::Init(wxPrintData* data)
477 {
478 if ( data )
479 m_printData = *data;
480
481 m_targetData = data;
482
483 wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL );
484
485 // printer selection
486
487 wxStaticBoxSizer *printer_sizer = new wxStaticBoxSizer( new wxStaticBox( this, wxID_ANY, _("Printer") ), wxVERTICAL );
488 main_sizer->Add( printer_sizer, 0, wxALL|wxGROW, 10 );
489
490 m_printerListCtrl = new wxListCtrl( this, wxPRINTID_PRINTER,
491 wxDefaultPosition, wxSize(wxDefaultCoord,100), wxLC_REPORT|wxLC_SINGLE_SEL|wxSUNKEN_BORDER );
492 wxImageList *image_list = new wxImageList;
493 image_list->Add( wxBitmap(check_xpm) );
494 m_printerListCtrl->AssignImageList( image_list, wxIMAGE_LIST_SMALL );
495
496 m_printerListCtrl->InsertColumn( 0, wxT(" "), wxLIST_FORMAT_LEFT, 20 );
497 m_printerListCtrl->InsertColumn( 1, wxT("Printer"), wxLIST_FORMAT_LEFT, 150 );
498 m_printerListCtrl->InsertColumn( 2, wxT("Device"), wxLIST_FORMAT_LEFT, 150 );
499 m_printerListCtrl->InsertColumn( 3, wxT("Status"), wxLIST_FORMAT_LEFT, 80 );
500
501 wxListItem item;
502 item.SetMask( wxLIST_MASK_TEXT );
503 item.SetColumn( 1 );
504 item.SetText( _("Default printer") );
505 item.SetId( m_printerListCtrl->InsertItem( item ) );
506
507 if (data->GetPrinterName().empty())
508 {
509 wxListItem item2;
510 item2.SetId( item.GetId() );
511 item2.SetMask( wxLIST_MASK_IMAGE );
512 item2.SetImage( 0 );
513 m_printerListCtrl->SetItem( item2 );
514 // also select item
515 m_printerListCtrl->SetItemState( item.GetId(), wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
516 }
517
518 item.SetId( 1+ item.GetId() );
519
520 wxArrayString errors;
521 wxArrayString output;
522 long res = wxExecute( wxT("lpstat -v"), output, errors, wxEXEC_NODISABLE );
523 if (res >= 0 && errors.GetCount() == 0)
524 {
525 size_t i;
526 for (i = 0; i < output.GetCount(); i++)
527 {
528 wxStringTokenizer tok( output[i], wxT(" ") );
529 wxString tmp = tok.GetNextToken(); // "device"
530 if (tmp != wxT("device"))
531 break; // the lpstat syntax must have changed.
532 tmp = tok.GetNextToken(); // "for"
533 if (tmp != wxT("for"))
534 break; // the lpstat syntax must have changed.
535 tmp = tok.GetNextToken(); // "hp_deskjet930c:"
536 if (tmp[tmp.length()-1] == wxT(':'))
537 tmp.Remove(tmp.length()-1,1);
538 wxString name = tmp;
539 item.SetText( name );
540 item.SetId( m_printerListCtrl->InsertItem( item ) );
541 tmp = tok.GetNextToken(); // "parallel:/dev/lp0"
542 item.SetColumn( 2 );
543 item.SetText( tmp );
544 m_printerListCtrl->SetItem( item );
545 if (data->GetPrinterName() == name)
546 {
547 wxListItem item2;
548 item2.SetId( item.GetId() );
549 item2.SetMask( wxLIST_MASK_IMAGE );
550 item2.SetImage( 0 );
551 m_printerListCtrl->SetItem( item2 );
552 // also select item
553 m_printerListCtrl->SetItemState( item.GetId(), wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
554 }
555
556 wxString command = wxT("lpstat -p ");
557 command += name;
558 wxArrayString errors2;
559 wxArrayString output2;
560 res = wxExecute( command, output2, errors2, wxEXEC_NODISABLE );
561 if (res >= 0 && errors2.GetCount() == 0 && output2.GetCount() > 0)
562 {
563 tmp = output2[0]; // "printer hp_deskjet930c is idle. enable since ..."
564 int pos = tmp.Find( wxT('.') );
565 if (pos != wxNOT_FOUND)
566 tmp.Remove( (size_t)pos, tmp.length()-(size_t)pos );
567 wxStringTokenizer tok2( tmp, wxT(" ") );
568 tmp = tok2.GetNextToken(); // "printer"
569 tmp = tok2.GetNextToken(); // "hp_deskjet930c"
570 tmp = wxEmptyString;
571 while (tok2.HasMoreTokens())
572 {
573 tmp += tok2.GetNextToken();
574 tmp += wxT(" ");
575 }
576 item.SetColumn( 3 );
577 item.SetText( tmp );
578 m_printerListCtrl->SetItem( item );
579 }
580
581 item.SetColumn( 1 );
582 item.SetId( 1+ item.GetId() );
583 }
584 }
585
586
587 printer_sizer->Add( m_printerListCtrl, 0, wxALL|wxGROW, 5 );
588
589 wxBoxSizer *item1 = new wxBoxSizer( wxHORIZONTAL );
590 main_sizer->Add( item1, 0, wxALL, 5 );
591
592 // printer options (on the left)
593
594 wxBoxSizer *item2 = new wxBoxSizer( wxVERTICAL );
595
596 wxStaticBox *item4 = new wxStaticBox( this, wxPRINTID_STATIC, _("Paper size") );
597 wxStaticBoxSizer *item3 = new wxStaticBoxSizer( item4, wxVERTICAL );
598
599 m_paperTypeChoice = CreatePaperTypeChoice();
600 item3->Add( m_paperTypeChoice, 0, wxALIGN_CENTER|wxALL, 5 );
601
602 item2->Add( item3, 0, wxALIGN_CENTER|wxALL, 5 );
603
604 wxString strs6[] =
605 {
606 _("Portrait"),
607 _("Landscape")
608 };
609 m_orientationRadioBox= new wxRadioBox( this, wxPRINTID_ORIENTATION, _("Orientation"), wxDefaultPosition, wxDefaultSize, 2, strs6, 1, wxRA_SPECIFY_ROWS );
610 item2->Add( m_orientationRadioBox, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
611
612 wxStaticBox *item8 = new wxStaticBox( this, wxID_ANY, _("Options") );
613 wxStaticBoxSizer *item7 = new wxStaticBoxSizer( item8, wxHORIZONTAL );
614
615 m_colourCheckBox = new wxCheckBox( this, wxPRINTID_PRINTCOLOUR, _("Print in colour") );
616 item7->Add( m_colourCheckBox, 0, wxALIGN_CENTER|wxALL, 5 );
617
618 item2->Add( item7, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5 );
619
620 item1->Add( item2, 0, wxALIGN_CENTER_HORIZONTAL, 5 );
621
622 // spooling options (on the right)
623
624 wxStaticBox *item11 = new wxStaticBox( this, wxID_ANY, _("Print spooling") );
625 wxStaticBoxSizer *item10 = new wxStaticBoxSizer( item11, wxVERTICAL );
626
627 wxStaticText *item12 = new wxStaticText( this, wxID_ANY, _("Printer command:") );
628 item10->Add( item12, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5 );
629
630 wxBoxSizer *item13 = new wxBoxSizer( wxHORIZONTAL );
631
632 item13->Add( 20, 20, 0, wxALIGN_CENTER|wxALL, 5 );
633
634 m_printerCommandText = new wxTextCtrl( this, wxPRINTID_COMMAND, wxEmptyString, wxDefaultPosition, wxSize(160,wxDefaultCoord) );
635 item13->Add( m_printerCommandText, 0, wxALIGN_CENTER|wxALL, 5 );
636
637 item10->Add( item13, 0, wxALIGN_CENTER|wxALL, 0 );
638
639 wxStaticText *item15 = new wxStaticText( this, wxID_ANY, _("Printer options:") );
640 item10->Add( item15, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5 );
641
642 wxBoxSizer *item16 = new wxBoxSizer( wxHORIZONTAL );
643
644 item16->Add( 20, 20, 0, wxALIGN_CENTER|wxALL, 5 );
645
646 m_printerOptionsText = new wxTextCtrl( this, wxPRINTID_OPTIONS, wxEmptyString, wxDefaultPosition, wxSize(160,wxDefaultCoord) );
647 item16->Add( m_printerOptionsText, 0, wxALIGN_CENTER|wxALL, 5 );
648
649 item10->Add( item16, 0, wxALIGN_CENTER|wxALL, 0 );
650
651 item1->Add( item10, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5 );
652
653
654 #if wxUSE_STATLINE
655 // static line
656 main_sizer->Add( new wxStaticLine( this, wxID_ANY ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 );
657 #endif
658
659 // buttons
660
661 main_sizer->Add( CreateButtonSizer( wxOK|wxCANCEL), 0, wxEXPAND|wxALL, 10 );
662
663 SetAutoLayout( true );
664 SetSizer( main_sizer );
665
666 main_sizer->Fit( this );
667 Centre(wxBOTH);
668
669
670 Fit();
671 Centre(wxBOTH);
672
673 InitDialog();
674 }
675
676 wxGenericPrintSetupDialog::~wxGenericPrintSetupDialog()
677 {
678 }
679
680 void wxGenericPrintSetupDialog::OnPrinter(wxListEvent& event)
681 {
682 // Delete check mark
683 for (long item = 0; item < m_printerListCtrl->GetItemCount(); item++)
684 m_printerListCtrl->SetItemImage( item, -1 );
685
686 m_printerListCtrl->SetItemImage( event.GetIndex(), 0 );
687
688 if (event.GetIndex() == 0)
689 {
690 m_printerCommandText->SetValue( wxT("lpr") );
691 }
692 else
693 {
694 wxListItem li;
695 li.SetColumn( 1 );
696 li.SetMask( wxLIST_MASK_TEXT );
697 li.SetId( event.GetIndex() );
698 m_printerListCtrl->GetItem( li );
699 m_printerCommandText->SetValue( _T("lpr -P") + li.GetText() );
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( wxEmptyString );
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, wxPageSetupDialogBase)
813
814 BEGIN_EVENT_TABLE(wxGenericPageSetupDialog, wxPageSetupDialogBase)
815 EVT_BUTTON(wxPRINTID_SETUP, wxGenericPageSetupDialog::OnPrinter)
816 END_EVENT_TABLE()
817
818 wxGenericPageSetupDialog::wxGenericPageSetupDialog( wxWindow *parent,
819 wxPageSetupDialogData* data)
820 : wxPageSetupDialogBase( parent,
821 wxID_ANY,
822 _("Page Setup"),
823 wxPoint(0,0),
824 wxSize(600, 600),
825 wxDEFAULT_DIALOG_STYLE|wxTAB_TRAVERSAL )
826 {
827 if (data)
828 m_pageData = *data;
829
830 int textWidth = 80;
831
832 wxBoxSizer *mainsizer = new wxBoxSizer( wxVERTICAL );
833
834 // 1) top
835 wxStaticBoxSizer *topsizer = new wxStaticBoxSizer(
836 new wxStaticBox(this,wxPRINTID_STATIC, _("Paper size")), wxHORIZONTAL );
837
838 size_t n = wxThePrintPaperDatabase->GetCount();
839 wxString *choices = new wxString [n];
840
841 for (size_t i = 0; i < n; i++)
842 {
843 wxPrintPaperType *paper = wxThePrintPaperDatabase->Item(i);
844 choices[i] = paper->GetName();
845 }
846
847 m_paperTypeChoice = new wxComboBox( this,
848 wxPRINTID_PAPERSIZE,
849 _("Paper Size"),
850 wxDefaultPosition,
851 wxSize(300, wxDefaultCoord),
852 n, choices );
853 topsizer->Add( m_paperTypeChoice, 1, wxEXPAND|wxALL, 5 );
854 // m_paperTypeChoice->SetSelection(sel);
855
856 mainsizer->Add( topsizer, 0, wxTOP|wxLEFT|wxRIGHT | wxEXPAND, 10 );
857
858 // 2) middle sizer with radio box
859
860 wxString *choices2 = new wxString[2];
861 choices2[0] = _("Portrait");
862 choices2[1] = _("Landscape");
863 m_orientationRadioBox = new wxRadioBox(this, wxPRINTID_ORIENTATION, _("Orientation"),
864 wxDefaultPosition, wxDefaultSize, 2, choices2, 2);
865 m_orientationRadioBox->SetSelection(0);
866
867 mainsizer->Add( m_orientationRadioBox, 0, wxTOP|wxLEFT|wxRIGHT, 10 );
868
869 // 3) margins
870
871 wxBoxSizer *table = new wxBoxSizer( wxHORIZONTAL );
872
873 wxBoxSizer *column1 = new wxBoxSizer( wxVERTICAL );
874 column1->Add( new wxStaticText(this, wxPRINTID_STATIC, _("Left margin (mm):")),1,wxALL|wxALIGN_RIGHT,5 );
875 column1->Add( new wxStaticText(this, wxPRINTID_STATIC, _("Top margin (mm):")),1,wxALL|wxALIGN_RIGHT,5 );
876 table->Add( column1, 0, wxALL | wxEXPAND, 5 );
877
878 wxBoxSizer *column2 = new wxBoxSizer( wxVERTICAL );
879 m_marginLeftText = new wxTextCtrl(this, wxPRINTID_LEFTMARGIN, wxEmptyString, wxDefaultPosition, wxSize(textWidth, wxDefaultCoord));
880 m_marginTopText = new wxTextCtrl(this, wxPRINTID_TOPMARGIN, wxEmptyString, wxDefaultPosition, wxSize(textWidth, wxDefaultCoord));
881 column2->Add( m_marginLeftText, 1, wxALL, 5 );
882 column2->Add( m_marginTopText, 1, wxALL, 5 );
883 table->Add( column2, 0, wxRIGHT|wxTOP|wxBOTTOM | wxEXPAND, 5 );
884
885 wxBoxSizer *column3 = new wxBoxSizer( wxVERTICAL );
886 column3->Add( new wxStaticText(this, wxPRINTID_STATIC, _("Right margin (mm):")),1,wxALL|wxALIGN_RIGHT,5 );
887 column3->Add( new wxStaticText(this, wxPRINTID_STATIC, _("Bottom margin (mm):")),1,wxALL|wxALIGN_RIGHT,5 );
888 table->Add( column3, 0, wxALL | wxEXPAND, 5 );
889
890 wxBoxSizer *column4 = new wxBoxSizer( wxVERTICAL );
891 m_marginRightText = new wxTextCtrl(this, wxPRINTID_RIGHTMARGIN, wxEmptyString, wxDefaultPosition, wxSize(textWidth, wxDefaultCoord));
892 m_marginBottomText = new wxTextCtrl(this, wxPRINTID_BOTTOMMARGIN, wxEmptyString, wxDefaultPosition, wxSize(textWidth, wxDefaultCoord));
893 column4->Add( m_marginRightText, 1, wxALL, 5 );
894 column4->Add( m_marginBottomText, 1, wxALL, 5 );
895 table->Add( column4, 0, wxRIGHT|wxTOP|wxBOTTOM | wxEXPAND, 5 );
896
897 mainsizer->Add( table, 0 );
898
899 #if wxUSE_STATLINE
900 // 5) static line
901 mainsizer->Add( new wxStaticLine( this, wxID_ANY ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 );
902 #endif
903
904 // 6) buttons
905
906 wxSizer* buttonsizer = CreateButtonSizer( wxOK|wxCANCEL);
907
908 if (wxPrintFactory::GetFactory()->HasPrintSetupDialog())
909 {
910 m_printerButton = new wxButton(this, wxPRINTID_SETUP, _("Printer...") );
911 buttonsizer->Add( m_printerButton, 0, wxLEFT|wxRIGHT, 10 );
912 if ( !m_pageData.GetEnablePrinter() )
913 m_printerButton->Enable(false);
914 }
915 else
916 {
917 m_printerButton = NULL;
918 }
919
920 // if (m_printData.GetEnableHelp())
921 // wxButton *helpButton = new wxButton(this, (wxFunction)wxGenericPageSetupHelpProc, _("Help"), wxDefaultCoord, wxDefaultCoord, buttonWidth, buttonHeight);
922 mainsizer->Add( buttonsizer, 0, wxEXPAND|wxALL, 10 );
923
924
925 SetAutoLayout( true );
926 SetSizer( mainsizer );
927
928 mainsizer->Fit( this );
929 Centre(wxBOTH);
930
931 InitDialog();
932
933 delete[] choices;
934 delete [] choices2;
935 }
936
937 wxGenericPageSetupDialog::~wxGenericPageSetupDialog()
938 {
939 }
940
941 wxPageSetupDialogData& wxGenericPageSetupDialog::GetPageSetupDialogData()
942 {
943 return m_pageData;
944 }
945
946 bool wxGenericPageSetupDialog::TransferDataToWindow()
947 {
948 if (m_marginLeftText)
949 m_marginLeftText->SetValue(wxString::Format(wxT("%d"), m_pageData.GetMarginTopLeft().x));
950 if (m_marginTopText)
951 m_marginTopText->SetValue(wxString::Format(wxT("%d"), m_pageData.GetMarginTopLeft().y));
952 if (m_marginRightText)
953 m_marginRightText->SetValue(wxString::Format(wxT("%d"), m_pageData.GetMarginBottomRight().x));
954 if (m_marginBottomText)
955 m_marginBottomText->SetValue(wxString::Format(wxT("%d"), m_pageData.GetMarginBottomRight().y));
956
957 if (m_orientationRadioBox)
958 {
959 if (m_pageData.GetPrintData().GetOrientation() == wxPORTRAIT)
960 m_orientationRadioBox->SetSelection(0);
961 else
962 m_orientationRadioBox->SetSelection(1);
963 }
964
965 // Find the paper type from either the current paper size in the wxPageSetupDialogData, or
966 // failing that, the id in the wxPrintData object.
967
968 wxPrintPaperType* type = wxThePrintPaperDatabase->FindPaperType(
969 wxSize(m_pageData.GetPaperSize().x * 10, m_pageData.GetPaperSize().y * 10));
970
971 if (!type && m_pageData.GetPrintData().GetPaperId() != wxPAPER_NONE)
972 type = wxThePrintPaperDatabase->FindPaperType(m_pageData.GetPrintData().GetPaperId());
973
974 if (type)
975 {
976 m_paperTypeChoice->SetStringSelection(type->GetName());
977 }
978
979 return true;
980 }
981
982 bool wxGenericPageSetupDialog::TransferDataFromWindow()
983 {
984 if (m_marginLeftText && m_marginTopText)
985 {
986 int left = wxAtoi( m_marginLeftText->GetValue().c_str() );
987 int top = wxAtoi( m_marginTopText->GetValue().c_str() );
988 m_pageData.SetMarginTopLeft( wxPoint(left,top) );
989 }
990 if (m_marginRightText && m_marginBottomText)
991 {
992 int right = wxAtoi( m_marginRightText->GetValue().c_str() );
993 int bottom = wxAtoi( m_marginBottomText->GetValue().c_str() );
994 m_pageData.SetMarginBottomRight( wxPoint(right,bottom) );
995 }
996
997 if (m_orientationRadioBox)
998 {
999 int sel = m_orientationRadioBox->GetSelection();
1000 if (sel == 0)
1001 {
1002 m_pageData.GetPrintData().SetOrientation(wxPORTRAIT);
1003 }
1004 else
1005 {
1006 m_pageData.GetPrintData().SetOrientation(wxLANDSCAPE);
1007 }
1008 }
1009
1010 if (m_paperTypeChoice)
1011 {
1012 int selectedItem = m_paperTypeChoice->GetSelection();
1013 if (selectedItem != -1)
1014 {
1015 wxPrintPaperType *paper = wxThePrintPaperDatabase->Item(selectedItem);
1016 if ( paper )
1017 {
1018 m_pageData.SetPaperSize(wxSize(paper->GetWidth()/10, paper->GetHeight()/10));
1019 m_pageData.GetPrintData().SetPaperId(paper->GetId());
1020 }
1021 }
1022 }
1023
1024 return true;
1025 }
1026
1027 wxComboBox *wxGenericPageSetupDialog::CreatePaperTypeChoice(int *x, int *y)
1028 {
1029 /*
1030 if (!wxThePrintPaperDatabase)
1031 {
1032 wxThePrintPaperDatabase = new wxPrintPaperDatabase;
1033 wxThePrintPaperDatabase->CreateDatabase();
1034 }
1035 */
1036
1037 size_t n = wxThePrintPaperDatabase->GetCount();
1038 wxString *choices = new wxString [n];
1039
1040 for (size_t i = 0; i < n; i++)
1041 {
1042 wxPrintPaperType *paper = wxThePrintPaperDatabase->Item(i);
1043 choices[i] = paper->GetName();
1044 }
1045
1046 (void) new wxStaticText(this, wxPRINTID_STATIC, _("Paper size"), wxPoint(*x, *y));
1047 *y += 25;
1048
1049 wxComboBox *choice = new wxComboBox( this,
1050 wxPRINTID_PAPERSIZE,
1051 _("Paper Size"),
1052 wxPoint(*x, *y),
1053 wxSize(300, wxDefaultCoord),
1054 n, choices );
1055 *y += 35;
1056 delete[] choices;
1057
1058 // choice->SetSelection(sel);
1059 return choice;
1060 }
1061
1062 void wxGenericPageSetupDialog::OnPrinter(wxCommandEvent& WXUNUSED(event))
1063 {
1064 // We no longer query GetPrintMode, so we can eliminate the need
1065 // to call SetPrintMode.
1066 // This has the limitation that we can't explicitly call the PostScript
1067 // print setup dialog from the generic Page Setup dialog under Windows,
1068 // but since this choice would only happen when trying to do PostScript
1069 // printing under Windows (and only in 16-bit Windows which
1070 // doesn't have a Windows-specific page setup dialog) it's worth it.
1071
1072 // First save the current settings, so the wxPrintData object is up to date.
1073 TransferDataFromWindow();
1074
1075 // Transfer the current print settings from this dialog to the page setup dialog.
1076
1077 #if 0
1078 // Use print factory later
1079
1080 wxPrintDialogData data;
1081 data = GetPageSetupData().GetPrintData();
1082 data.SetSetupDialog(true);
1083 wxPrintDialog printDialog(this, & data);
1084 printDialog.ShowModal();
1085
1086 // Transfer the page setup print settings from the page dialog to this dialog again, in case
1087 // the page setup dialog changed something.
1088 GetPageSetupData().GetPrintData() = printDialog.GetPrintDialogData().GetPrintData();
1089 GetPageSetupData().CalculatePaperSizeFromId(); // Make sure page size reflects the id in wxPrintData
1090
1091 // Now update the dialog in case the page setup dialog changed some of our settings.
1092 TransferDataToWindow();
1093 #endif
1094 }
1095
1096 #endif