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