NCBI C++ ToolKit
print_setup_dlg_wx.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 
2 #include <ncbi_pch.hpp>
4 
6 
7 #include <util/image/image.hpp>
8 
9 #include "wx/utils.h"
10 #include "wx/dc.h"
11 #include "wx/stattext.h"
12 #include "wx/statbox.h"
13 #include "wx/button.h"
14 #include "wx/checkbox.h"
15 #include "wx/textctrl.h"
16 #include "wx/radiobox.h"
17 #include "wx/slider.h"
18 #include "wx/combobox.h"
19 #include "wx/intl.h"
20 #include "wx/sizer.h"
21 #include "wx/cmndata.h"
22 
23 #if wxUSE_STATLINE
24  #include "wx/statline.h"
25 #endif
26 
27 #include "wx/prntbase.h"
28 #include "wx/printdlg.h"
29 #include "wx/paper.h"
30 #include "wx/filename.h"
31 #include "wx/tokenzr.h"
32 #include "wx/imaglist.h"
33 
34 #include <stdlib.h>
35 #include <string.h>
36 
37 /*
38 #ifndef __WXUNIVERSAL__
39 
40 #if wxUSE_LIBGNOMEPRINT
41  #include "wx/link.h"
42  wxFORCE_LINK_MODULE(gnome_print)
43 #endif
44 
45 #endif // !__WXUNIVERSAL__
46 */
47 
48 // ----------------------------------------------------------------------------
49 // global vars
50 // ----------------------------------------------------------------------------
51 
52 //extern wxPrintPaperDatabase *wxThePrintPaperDatabase;
53 extern WXDLLEXPORT_DATA(wxPrintPaperDatabase*) wxThePrintPaperDatabase;
54 
55 // ----------------------------------------------------------------------------
56 // Generic page setup dialog
57 // ----------------------------------------------------------------------------
58 
60 
62 
63 BEGIN_EVENT_TABLE(CPrintSetupDlg, CPreviewSetupDlg)
64  EVT_BUTTON(ePrintIDSetup, CPrintSetupDlg::OnPrinter)
65  EVT_SCROLL(CPrintSetupDlg::OnScroll)
66  EVT_RADIOBOX(eMultiPageID, CPrintSetupDlg::OnMultiPage)
67  EVT_RADIOBOX(eGuidesID, CPrintSetupDlg::OnGuides)
69 
71  wxWindow *parent,
72  wxPageSetupDialogData* data)
73  : CPreviewSetupDlg( img, parent)
74  , m_OrientationRadioBox(NULL)
75  , m_MarginLeftText(NULL)
76  , m_MarginTopText(NULL)
77  , m_MarginRightText(NULL)
78  , m_MarginBottomText(NULL)
79  , m_PaperTypeChoice(NULL)
80 {
81  if (data)
82  m_PageData = *data;
83 
84  // Layout all the controls
85  x_InitDialog(img);
86 }
87 
89 {
90 }
91 
92 void CPrintSetupDlg::x_Init(wxBoxSizer* mainsizer, int widget_width)
93 {
94  int text_width = 80;
95 
96  // 1. Paper Choice
97  ///////////////////////////////////////////////////////////////////////////
98  size_t n = wxThePrintPaperDatabase->GetCount();
99  wxString *choices = new wxString [n];
100 
101  for (size_t i = 0; i < n; i++)
102  {
103  wxPrintPaperType *paper = wxThePrintPaperDatabase->Item(i);
104  choices[i] = paper->GetName();
105  }
106 
107 
108  m_PaperTypeChoice = new wxComboBox( this,
110  wxT("Paper"),
111  wxDefaultPosition,
112  wxSize(widget_width, wxDefaultCoord),
113  (int)n, choices );
114 
115  wxStaticBoxSizer *paper_choice_sizer = new wxStaticBoxSizer(
116  new wxStaticBox(this,ePrintIDStatic, wxT("Paper size")),
117  wxHORIZONTAL );
118 
119  paper_choice_sizer->Add( m_PaperTypeChoice, 1, wxEXPAND|wxALL|wxALIGN_BOTTOM, 5 );
120  wxBoxSizer *paper_box = new wxBoxSizer( wxHORIZONTAL );
121  paper_box->Add(paper_choice_sizer, 0, wxALL, 5);
122 
123  wxString *choices2 = new wxString[2];
124  choices2[0] = wxT("Portrait");
125  choices2[1] = wxT("Landscape");
126  m_OrientationRadioBox = new wxRadioBox(this, ePrintIDOrientation, wxT("Paper Orientation"),
127  wxDefaultPosition, wxDefaultSize, 2, choices2, 2);
128  m_OrientationRadioBox->SetSelection(0);
129 
130  paper_box->Add(m_OrientationRadioBox, 0, wxALL|wxALIGN_TOP, 5 );
131 
132  mainsizer->Add( paper_box, 0, wxALL|wxALIGN_CENTER, 5 );
133 
134 
135  // 2. margins
136  ///////////////////////////////////////////////////////////////////////////
137  wxBoxSizer *table = new wxBoxSizer( wxHORIZONTAL );
138 
139  wxBoxSizer *column1 = new wxBoxSizer( wxVERTICAL );
140  column1->Add( new wxStaticText(this, ePrintIDStatic, wxT("Left margin (mm):")),1,wxALL|wxALIGN_CENTER,5 );
141  column1->Add( new wxStaticText(this, ePrintIDStatic, wxT("Top margin (mm):")),1,wxALL|wxALIGN_CENTER,5 );
142  table->Add( column1, 0, wxALL |wxEXPAND, 5 );
143 
144  wxBoxSizer *column2 = new wxBoxSizer( wxVERTICAL );
145  m_MarginLeftText = new wxTextCtrl(this, ePrintIDLeftMargin, wxEmptyString, wxDefaultPosition, wxSize(text_width, wxDefaultCoord));
146  m_MarginTopText = new wxTextCtrl(this, ePrintIDTopMargin, wxEmptyString, wxDefaultPosition, wxSize(text_width, wxDefaultCoord));
147  column2->Add( m_MarginLeftText, 1, wxALL, 5 );
148  column2->Add( m_MarginTopText, 1, wxALL, 5 );
149  table->Add( column2, 0, wxRIGHT|wxTOP|wxBOTTOM |wxEXPAND, 5 );
150 
151  wxBoxSizer *column3 = new wxBoxSizer( wxVERTICAL );
152  column3->Add( new wxStaticText(this, ePrintIDStatic, wxT("Right margin (mm):")),1,wxALL|wxALIGN_CENTER,5 );
153  column3->Add( new wxStaticText(this, ePrintIDStatic, wxT("Bottom margin (mm):")),1,wxALL|wxALIGN_CENTER,5 );
154  table->Add( column3, 0, wxALL|wxEXPAND, 5 );
155 
156  wxBoxSizer *column4 = new wxBoxSizer( wxVERTICAL );
157  m_MarginRightText = new wxTextCtrl(this, ePrintIDRightMargin, wxEmptyString, wxDefaultPosition, wxSize(text_width, wxDefaultCoord));
158  m_MarginBottomText = new wxTextCtrl(this, ePrintIDBottomMargin, wxEmptyString, wxDefaultPosition, wxSize(text_width, wxDefaultCoord));
159  column4->Add( m_MarginRightText, 1, wxALL, 5 );
160  column4->Add( m_MarginBottomText, 1, wxALL, 5 );
161  table->Add( column4, 0, wxRIGHT|wxTOP|wxBOTTOM|wxEXPAND, 5 );
162 
163  mainsizer->Add( table, 0, wxALIGN_CENTER );
164 }
165 
167 {
168  return m_PageData;
169 }
170 
172 {
173  // Don't transfer data until window controls have been created
174  if (!m_MarginLeftText)
175  return false;
176 
178 
179  m_MarginLeftText->SetValue(wxString::Format(wxT("%d"), m_PageData.GetMarginTopLeft().x));
180  m_MarginTopText->SetValue(wxString::Format(wxT("%d"), m_PageData.GetMarginTopLeft().y));
181  m_MarginRightText->SetValue(wxString::Format(wxT("%d"), m_PageData.GetMarginBottomRight().x));
182  m_MarginBottomText->SetValue(wxString::Format(wxT("%d"), m_PageData.GetMarginBottomRight().y));
183 
184  if (m_PageData.GetPrintData().GetOrientation() == wxPORTRAIT)
185  m_OrientationRadioBox->SetSelection(0);
186  else
187  m_OrientationRadioBox->SetSelection(1);
188 
189  // Find the paper type from either the current paper size in the wxPageSetupDialogData, or
190  // failing that, the id in the wxPrintData object.
191 
192  wxPrintPaperType* type = wxThePrintPaperDatabase->FindPaperType(
193  wxSize(m_PageData.GetPaperSize().x * 10, m_PageData.GetPaperSize().y * 10));
194 
195  if (!type && m_PageData.GetPrintData().GetPaperId() != wxPAPER_NONE)
196  type = wxThePrintPaperDatabase->FindPaperType(m_PageData.GetPrintData().GetPaperId());
197 
198  if (type)
199  {
200  m_PaperTypeChoice->SetStringSelection(type->GetName());
201  }
202 
203  return true;
204 }
205 
207 {
209 
211  {
212  int left = wxAtoi( m_MarginLeftText->GetValue().c_str() );
213  int top = wxAtoi( m_MarginTopText->GetValue().c_str() );
214  m_PageData.SetMarginTopLeft( wxPoint(left,top) );
215  }
217  {
218  int right = wxAtoi( m_MarginRightText->GetValue().c_str() );
219  int bottom = wxAtoi( m_MarginBottomText->GetValue().c_str() );
220  m_PageData.SetMarginBottomRight( wxPoint(right,bottom) );
221  }
222 
224  {
225  int sel = m_OrientationRadioBox->GetSelection();
226  if (sel == 0)
227  {
228  m_PageData.GetPrintData().SetOrientation(wxPORTRAIT);
229  }
230  else
231  {
232  m_PageData.GetPrintData().SetOrientation(wxLANDSCAPE);
233  }
234  }
235 
236  if (m_PaperTypeChoice)
237  {
238  int selectedItem = m_PaperTypeChoice->GetSelection();
239  if (selectedItem != -1)
240  {
241  wxPrintPaperType *paper = wxThePrintPaperDatabase->Item(selectedItem);
242  if ( paper )
243  {
244  m_PageData.SetPaperSize(wxSize(paper->GetWidth()/10, paper->GetHeight()/10));
245  m_PageData.GetPrintData().SetPaperId(paper->GetId());
246  }
247  }
248  }
249 
250  return true;
251 }
252 
253 wxComboBox *CPrintSetupDlg::CreatePaperTypeChoice(int *x, int *y)
254 {
255 /*
256  if (!wxThePrintPaperDatabase)
257  {
258  wxThePrintPaperDatabase = new wxPrintPaperDatabase;
259  wxThePrintPaperDatabase->CreateDatabase();
260  }
261 */
262 
263  size_t n = wxThePrintPaperDatabase->GetCount();
264  wxString *choices = new wxString [n];
265 
266  for (size_t i = 0; i < n; i++)
267  {
268  wxPrintPaperType *paper = wxThePrintPaperDatabase->Item(i);
269  choices[i] = paper->GetName();
270  }
271 
272  (void) new wxStaticText(this, ePrintIDStatic, wxT("Paper size"), wxPoint(*x, *y));
273  *y += 25;
274 
275  wxComboBox *choice = new wxComboBox( this,
277  wxT("Paper Size"),
278  wxPoint(*x, *y),
279  wxSize(300, wxDefaultCoord),
280  (int)n, choices );
281  *y += 35;
282  delete[] choices;
283 
284 // choice->SetSelection(sel);
285  return choice;
286 }
287 
289 {
291 
293 
294  // take paper size and margin sizes into account when setting up coordinate system
295  // so that units are "square" (1 unit x is a wide as 1 unit y is high)
296  // (actually, if we don't make it square, on some platforms wx will even resize your
297  // margins to make it so)
298  wxSize paperSize = m_PageData.GetPaperSize(); // in millimeters
299 
300  // still in millimeters
301  float large_side = std::max(paperSize.GetWidth(), paperSize.GetHeight());
302  float small_side = std::min(paperSize.GetWidth(), paperSize.GetHeight());
303 
304  //float large_side_cm = large_side / 10.0f; // in centimeters
305  float small_side_cm = small_side / 10.0f; // in centimeters
306 
307  wxPoint marginTopLeft = m_PageData.GetMarginTopLeft();
308  wxPoint marginBottomRight = m_PageData.GetMarginBottomRight();
309  int m_margin_left = marginTopLeft.x;
310  int m_margin_right = marginBottomRight.x;
311  int m_margin_top = marginTopLeft.y;
312  int m_margin_bottom = marginBottomRight.y;
313 
314  float m_units_per_cm = 30.0f;
315 
316  if (m_PageData.GetPrintData().GetOrientation() == wxPORTRAIT)
317  {
318  float ratio = float(large_side - m_margin_top - m_margin_bottom) /
319  float(small_side - m_margin_left - m_margin_right);
320 
321  m_PrintCoordWidth = (int)((small_side_cm - m_margin_left/10.f -
322  m_margin_right/10.0f)*m_units_per_cm);
323  m_PrintCoordHeight = (int)(((float)m_PrintCoordWidth)*ratio);
324  }
325  else
326  {
327  float ratio = float(large_side - m_margin_left - m_margin_right) /
328  float(small_side - m_margin_top - m_margin_bottom);
329 
330  m_PrintCoordHeight = (int)((small_side_cm - m_margin_top/10.0f -
331  m_margin_bottom/10.0f)* m_units_per_cm);
332  m_PrintCoordWidth = (int)(((float)m_PrintCoordHeight)*ratio);
333  }
334 
335  // If we are printing a single page;
336  int image_width = m_PreviewSetupWidget->GetImageWidth();
337  int image_height = m_PreviewSetupWidget->GetImageHeight();
338  float img_ratio = ((float)image_width)/(float)image_height;
339  float paper_ratio = ((float)m_PrintCoordWidth)/(float)m_PrintCoordHeight;
340 
341  //float xpos, ypos;
342  float xlen, ylen;
343 
344  if (m_MultiPageRadioBox->GetSelection() == 0 ||
345  m_PageCountSlider->GetValue() == 0) {
346 
348 
349  if (paper_ratio > 1.0f && img_ratio < 1.0f) {
351  std::swap(image_width, image_height);
352  img_ratio = 1.0f/img_ratio;
353  }
354  else if (paper_ratio < 1.0f && img_ratio > 1.0f) {
356  std::swap(image_width, image_height);
357  img_ratio = 1.0f/img_ratio;
358  }
359 
360  float wr = ((float)image_width)/(float)m_PrintCoordWidth;
361  float hr = ((float)image_height)/(float)m_PrintCoordHeight;
362 
363  if (wr > hr) {
364  xlen = (float)m_PrintCoordWidth; // = image_width*(paper_width/image_width) = paper_width
365  ylen = ((float)image_height) * (((float)m_PrintCoordWidth)/((float)image_width));
366 
367  m_PrintOffsetX = 0;
368  m_PrintOffsetY = (int) ((((float)m_PrintCoordHeight) - ylen)/2.0f);
369 
371  m_ImageWidthY = (int)ylen;
372  }
373  else {
374  xlen = ((float)image_width) * (((float)m_PrintCoordHeight)/((float)image_height));
375  ylen = (float)m_PrintCoordHeight; //= image_height*(paper_height/image_height) = paper_height
376 
377  m_PrintOffsetX = (int)((((float)m_PrintCoordWidth) - xlen)/2.0f);
378  m_PrintOffsetY = 0;
379 
380  m_ImageWidthX = (int)xlen;
382  }
383  }
384  // If we are printing multiple pages
385  else {
386  }
387 
388 }
389 
390 /*
391 void CPrintSetupDlg::OnScroll(wxScrollEvent& event)
392 {
393  if (event.GetEventObject() == m_PageCountSlider) {
394  int pos = m_PageCountSlider->GetValue();
395 
396  m_PageCount->SetLabel(NStr::IntToString(pos));
397  m_PageCount->Refresh();
398 
399  //int m_PrintCoordWidth;
400  //int m_PrintCoordHeight;
401 
402  m_PreviewSetupWidget->SetPartitions(3, 3);
403  }
404 }
405 
406 void CPrintSetupDlg::OnMultiPage( wxCommandEvent& event )
407 {
408  if (m_MultiPageRadioBox->GetSelection() == 0) {
409  //disable sizing
410  //m_PageCountSlider->SetValue(1);
411  m_PageCountSlider->Disable();
412  m_PageCountSlider->Refresh();
413  //m_PageCount->SetLabel(NStr::IntToString(1));
414  m_PageCount->Disable();
415  m_PageCount->Refresh();
416 
417  //m_PreviewSetupWidget->HideBars();
418  m_PreviewSetupWidget->Refresh();
419  }
420  else {
421  // enable sizing
422  m_PageCountSlider->Enable();
423  m_PageCountSlider->Refresh();
424  m_PageCount->Enable();
425  m_PageCount->Refresh();
426  //m_PageCountSlider->Enable();
427  //m_PreviewSetupWidget->ShowBars();
428  m_PreviewSetupWidget->Refresh();
429  }
430 
431  UpdateMargins();
432 
433 }
434 
435 
436 void CPrintSetupDlg::OnGuides( wxCommandEvent& event )
437 {
438 }
439 */
440 
441 
442 void CPrintSetupDlg::OnPrinter(wxCommandEvent& WXUNUSED(event))
443 {
444  // We no longer query GetPrintMode, so we can eliminate the need
445  // to call SetPrintMode.
446  // This has the limitation that we can't explicitly call the PostScript
447  // print setup dialog from the generic Page Setup dialog under Windows,
448  // but since this choice would only happen when trying to do PostScript
449  // printing under Windows (and only in 16-bit Windows which
450  // doesn't have a Windows-specific page setup dialog) it's worth it.
451 
452  // First save the current settings, so the wxPrintData object is up to date.
454 }
455 
456 
void SetRotated(bool b)
Set to true to rotate the image 90 (to better match paper size)
int GetImageWidth() const
Get width and height of image.
CImage –.
Definition: Image.hpp:66
virtual void UpdateMargins()
virtual void OnScroll(wxScrollEvent &event)
virtual bool TransferDataFromWindow()
wxRadioBox * m_MultiPageRadioBox
wxSlider * m_PageCountSlider
virtual void OnGuides(wxCommandEvent &event)
virtual void OnMultiPage(wxCommandEvent &event)
CGlPreviewSetupWidget * m_PreviewSetupWidget
virtual bool TransferDataToWindow()
wxTextCtrl * m_MarginRightText
virtual wxPageSetupDialogData & GetPageSetupDialogData()
Required as a subclass of wxPageSetupDialogBase.
wxTextCtrl * m_MarginLeftText
void OnPrinter(wxCommandEvent &event)
wxComboBox * m_PaperTypeChoice
virtual bool TransferDataFromWindow()
wxComboBox * CreatePaperTypeChoice(int *x, int *y)
wxPageSetupDialogData m_PageData
virtual void x_Init(wxBoxSizer *mainsizer, int widget_width)
Provide virtual hook for subclasses to add their own controls below the main controls.
virtual bool TransferDataToWindow()
wxRadioBox * m_OrientationRadioBox
wxTextCtrl * m_MarginBottomText
wxTextCtrl * m_MarginTopText
CRef –.
Definition: ncbiobj.hpp:618
IMPLEMENT_CLASS(CFloatingFrame, CFloatingFrameBaseClass) const static long kFloatFrameStyle
CFloatingFrame.
void swap(NCBI_NS_NCBI::pair_base_member< T1, T2 > &pair1, NCBI_NS_NCBI::pair_base_member< T1, T2 > &pair2)
Definition: ncbimisc.hpp:1508
#define NULL
Definition: ncbistd.hpp:225
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
unsigned int
A callback function used to compare two keys in a database.
Definition: types.hpp:1210
<!DOCTYPE HTML >< html > n< header > n< title > PubSeq Gateway Help Page</title > n< style > n table
END_EVENT_TABLE()
int i
yy_size_t n
#define wxT(x)
Definition: muParser.cpp:41
T max(T x_, T y_)
T min(T x_, T y_)
Format
Definition: njn_ioutil.hpp:52
WXDLLEXPORT_DATA(wxPrintPaperDatabase *) wxThePrintPaperDatabase
Definition: type.c:6
Modified on Fri Dec 01 04:45:15 2023 by modify_doxy.py rev. 669887