You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

392 lines
11 KiB

  1. /****************************************/
  2. /* File: dialog_print_using_printer.cpp */
  3. /****************************************/
  4. #include <fctsys.h>
  5. #include <appl_wxstruct.h>
  6. #include <gr_basic.h>
  7. #include <class_drawpanel.h>
  8. #include <confirm.h>
  9. #include <class_sch_screen.h>
  10. #include <wxEeschemaStruct.h>
  11. #include <base_units.h>
  12. #include <general.h>
  13. #include <eeschema_config.h>
  14. #include <sch_sheet.h>
  15. #include <sch_sheet_path.h>
  16. #include <dialog_print_using_printer.h>
  17. /**
  18. * Custom print out for printing schematics.
  19. */
  20. class SCH_PRINTOUT : public wxPrintout
  21. {
  22. private:
  23. DIALOG_PRINT_USING_PRINTER* m_Parent;
  24. public:
  25. SCH_PRINTOUT( DIALOG_PRINT_USING_PRINTER* aParent, const wxString& aTitle ) :
  26. wxPrintout( aTitle )
  27. {
  28. wxASSERT( aParent != NULL );
  29. m_Parent = aParent;
  30. }
  31. bool OnPrintPage( int page );
  32. bool HasPage( int page );
  33. bool OnBeginDocument( int startPage, int endPage );
  34. void GetPageInfo( int* minPage, int* maxPage, int* selPageFrom, int* selPageTo );
  35. void DrawPage( SCH_SCREEN* aScreen );
  36. };
  37. /**
  38. * Custom schematic print preview frame.
  39. */
  40. class SCH_PREVIEW_FRAME : public wxPreviewFrame
  41. {
  42. public:
  43. SCH_PREVIEW_FRAME( wxPrintPreview* aPreview, DIALOG_PRINT_USING_PRINTER* aParent,
  44. const wxString& aTitle, const wxPoint& aPos = wxDefaultPosition,
  45. const wxSize& aSize = wxDefaultSize ) :
  46. wxPreviewFrame( aPreview, aParent, aTitle, aPos, aSize )
  47. {
  48. }
  49. DIALOG_PRINT_USING_PRINTER* GetParent()
  50. {
  51. return ( DIALOG_PRINT_USING_PRINTER* )wxWindow::GetParent();
  52. }
  53. void OnCloseWindow( wxCloseEvent& event )
  54. {
  55. if( !IsIconized() )
  56. {
  57. GetParent()->GetParent()->SetPreviewPosition( GetPosition() );
  58. GetParent()->GetParent()->SetPreviewSize( GetSize() );
  59. }
  60. wxPreviewFrame::OnCloseWindow( event );
  61. }
  62. private:
  63. DECLARE_CLASS( SCH_PREVIEW_FRAME )
  64. DECLARE_EVENT_TABLE()
  65. DECLARE_NO_COPY_CLASS( SCH_PREVIEW_FRAME )
  66. };
  67. IMPLEMENT_CLASS( SCH_PREVIEW_FRAME, wxPreviewFrame )
  68. BEGIN_EVENT_TABLE( SCH_PREVIEW_FRAME, wxPreviewFrame )
  69. EVT_CLOSE( SCH_PREVIEW_FRAME::OnCloseWindow )
  70. END_EVENT_TABLE()
  71. DIALOG_PRINT_USING_PRINTER::DIALOG_PRINT_USING_PRINTER( SCH_EDIT_FRAME* aParent ) :
  72. DIALOG_PRINT_USING_PRINTER_BASE( aParent )
  73. {
  74. wxASSERT( aParent != NULL );
  75. m_checkReference->SetValue( aParent->GetPrintSheetReference() );
  76. m_checkMonochrome->SetValue( aParent->GetPrintMonochrome() );
  77. #ifdef __WXMAC__
  78. // Problems with modal on wx-2.9 - Anyway preview is standard for OSX
  79. m_buttonPreview->Hide();
  80. #endif
  81. }
  82. SCH_EDIT_FRAME* DIALOG_PRINT_USING_PRINTER::GetParent() const
  83. {
  84. return ( SCH_EDIT_FRAME* ) wxWindow::GetParent();
  85. }
  86. void DIALOG_PRINT_USING_PRINTER::OnInitDialog( wxInitDialogEvent& event )
  87. {
  88. SCH_EDIT_FRAME* parent = GetParent();
  89. // Initialize page specific print setup dialog settings.
  90. const PAGE_INFO& pageInfo = parent->GetScreen()->GetPageSettings();
  91. wxPageSetupDialogData& pageSetupDialogData = parent->GetPageSetupData();
  92. pageSetupDialogData.SetPaperId( pageInfo.GetPaperId() );
  93. if( pageInfo.IsCustom() )
  94. {
  95. if( pageInfo.IsPortrait() )
  96. pageSetupDialogData.SetPaperSize( wxSize( Mils2mm( pageInfo.GetWidthMils() ),
  97. Mils2mm( pageInfo.GetHeightMils() ) ) );
  98. else
  99. pageSetupDialogData.SetPaperSize( wxSize( Mils2mm( pageInfo.GetHeightMils() ),
  100. Mils2mm( pageInfo.GetWidthMils() ) ) );
  101. }
  102. pageSetupDialogData.GetPrintData().SetOrientation( pageInfo.GetWxOrientation() );
  103. if ( GetSizer() )
  104. GetSizer()->SetSizeHints( this );
  105. if( parent->GetPrintDialogPosition() == wxDefaultPosition &&
  106. parent->GetPrintDialogSize() == wxDefaultSize )
  107. {
  108. Center();
  109. }
  110. else
  111. {
  112. SetPosition( parent->GetPrintDialogPosition() );
  113. SetSize( parent->GetPrintDialogSize() );
  114. }
  115. SetFocus();
  116. m_buttonPrint->SetDefault();
  117. }
  118. void DIALOG_PRINT_USING_PRINTER::OnCloseWindow( wxCloseEvent& event )
  119. {
  120. SCH_EDIT_FRAME* parent = GetParent();
  121. if( !IsIconized() )
  122. {
  123. parent->SetPrintDialogPosition( GetPosition() );
  124. parent->SetPrintDialogSize( GetSize() );
  125. }
  126. parent->SetPrintMonochrome( m_checkMonochrome->IsChecked() );
  127. parent->SetPrintSheetReference( m_checkReference->IsChecked() );
  128. EndDialog( wxID_CANCEL );
  129. }
  130. /* Open a dialog box for printer setup (printer options, page size ...)
  131. */
  132. void DIALOG_PRINT_USING_PRINTER::OnPageSetup( wxCommandEvent& event )
  133. {
  134. SCH_EDIT_FRAME* parent = GetParent();
  135. wxPageSetupDialog pageSetupDialog( this, &parent->GetPageSetupData() );
  136. pageSetupDialog.ShowModal();
  137. parent->GetPageSetupData() = pageSetupDialog.GetPageSetupDialogData();
  138. }
  139. /* Open and display a previewer frame for printing
  140. */
  141. void DIALOG_PRINT_USING_PRINTER::OnPrintPreview( wxCommandEvent& event )
  142. {
  143. SCH_EDIT_FRAME* parent = GetParent();
  144. parent->SetPrintMonochrome( m_checkMonochrome->IsChecked() );
  145. parent->SetPrintSheetReference( m_checkReference->IsChecked() );
  146. // Pass two printout objects: for preview, and possible printing.
  147. wxString title = _( "Preview" );
  148. wxPrintPreview* preview = new wxPrintPreview( new SCH_PRINTOUT( this, title ),
  149. new SCH_PRINTOUT( this, title ),
  150. &parent->GetPageSetupData().GetPrintData() );
  151. if( preview == NULL )
  152. {
  153. DisplayError( this, wxT( "Print preview error!" ) );
  154. return;
  155. }
  156. preview->SetZoom( 100 );
  157. SCH_PREVIEW_FRAME* frame = new SCH_PREVIEW_FRAME( preview, this, title,
  158. parent->GetPreviewPosition(),
  159. parent->GetPreviewSize() );
  160. frame->Initialize();
  161. frame->Show( true );
  162. }
  163. void DIALOG_PRINT_USING_PRINTER::OnPrintButtonClick( wxCommandEvent& event )
  164. {
  165. SCH_EDIT_FRAME* parent = GetParent();
  166. parent->SetPrintMonochrome( m_checkMonochrome->IsChecked() );
  167. parent->SetPrintSheetReference( m_checkReference->IsChecked() );
  168. wxPrintDialogData printDialogData( parent->GetPageSetupData().GetPrintData() );
  169. printDialogData.SetMaxPage( g_RootSheet->CountSheets() );
  170. if( g_RootSheet->CountSheets() > 1 )
  171. printDialogData.EnablePageNumbers( true );
  172. wxPrinter printer( &printDialogData );
  173. SCH_PRINTOUT printout( this, _( "Print Schematic" ) );
  174. if( !printer.Print( this, &printout, true ) )
  175. {
  176. if( wxPrinter::GetLastError() == wxPRINTER_ERROR )
  177. wxMessageBox( _( "An error occurred attempting to print the schematic." ),
  178. _( "Printing" ), wxOK );
  179. }
  180. else
  181. {
  182. parent->GetPageSetupData() = printer.GetPrintDialogData().GetPrintData();
  183. }
  184. }
  185. bool SCH_PRINTOUT::OnPrintPage( int page )
  186. {
  187. wxString msg;
  188. SCH_EDIT_FRAME* parent = m_Parent->GetParent();
  189. msg.Printf( _( "Print page %d" ), page );
  190. parent->ClearMsgPanel();
  191. parent->AppendMsgPanel( msg, wxEmptyString, CYAN );
  192. SCH_SCREEN* screen = parent->GetScreen();
  193. SCH_SHEET_PATH oldsheetpath = parent->GetCurrentSheet();
  194. SCH_SHEET_PATH list;
  195. SCH_SHEET_LIST SheetList( NULL );
  196. SCH_SHEET_PATH* sheetpath = SheetList.GetSheet( page - 1 );
  197. if( list.BuildSheetPathInfoFromSheetPathValue( sheetpath->Path() ) )
  198. {
  199. parent->SetCurrentSheet( list );
  200. parent->GetCurrentSheet().UpdateAllScreenReferences();
  201. parent->SetSheetNumberAndCount();
  202. screen = parent->GetCurrentSheet().LastScreen();
  203. }
  204. else
  205. {
  206. screen = NULL;
  207. }
  208. if( screen == NULL )
  209. return false;
  210. DrawPage( screen );
  211. parent->SetCurrentSheet( oldsheetpath );
  212. parent->GetCurrentSheet().UpdateAllScreenReferences();
  213. parent->SetSheetNumberAndCount();
  214. return true;
  215. }
  216. void SCH_PRINTOUT::GetPageInfo( int* minPage, int* maxPage, int* selPageFrom, int* selPageTo )
  217. {
  218. *minPage = *selPageFrom = 1;
  219. *maxPage = *selPageTo = g_RootSheet->CountSheets();
  220. }
  221. bool SCH_PRINTOUT::HasPage( int pageNum )
  222. {
  223. int pageCount;
  224. pageCount = g_RootSheet->CountSheets();
  225. if( pageCount >= pageNum )
  226. return true;
  227. return false;
  228. }
  229. bool SCH_PRINTOUT::OnBeginDocument( int startPage, int endPage )
  230. {
  231. if( !wxPrintout::OnBeginDocument( startPage, endPage ) )
  232. return false;
  233. #ifdef __WXDEBUG__
  234. SCH_EDIT_FRAME* parent = m_Parent->GetParent();
  235. wxLogDebug( wxT( "Printer name: " ) +
  236. parent->GetPageSetupData().GetPrintData().GetPrinterName() );
  237. wxLogDebug( wxT( "Paper ID: %d" ),
  238. parent->GetPageSetupData().GetPrintData().GetPaperId() );
  239. wxLogDebug( wxT( "Color: %d" ),
  240. (int) parent->GetPageSetupData().GetPrintData().GetColour() );
  241. wxLogDebug( wxT( "Monochrome: %d" ), parent->GetPrintMonochrome() );
  242. wxLogDebug( wxT( "Orientation: %d:" ),
  243. parent->GetPageSetupData().GetPrintData().GetOrientation() );
  244. wxLogDebug( wxT( "Quality: %d"),
  245. parent->GetPageSetupData().GetPrintData().GetQuality() );
  246. #endif
  247. return true;
  248. }
  249. /*
  250. * This is the real print function: print the active screen
  251. */
  252. void SCH_PRINTOUT::DrawPage( SCH_SCREEN* aScreen )
  253. {
  254. int oldZoom;
  255. wxPoint tmp_startvisu;
  256. wxSize pageSizeIU; // Page size in internal units
  257. wxPoint old_org;
  258. EDA_RECT oldClipBox;
  259. wxRect fitRect;
  260. wxDC* dc = GetDC();
  261. SCH_EDIT_FRAME* parent = m_Parent->GetParent();
  262. EDA_DRAW_PANEL* panel = parent->GetCanvas();
  263. wxBusyCursor dummy;
  264. // Save current scale factor, offsets, and clip box.
  265. tmp_startvisu = aScreen->m_StartVisu;
  266. oldZoom = aScreen->GetZoom();
  267. old_org = aScreen->m_DrawOrg;
  268. oldClipBox = *panel->GetClipBox();
  269. // Change clip box to print the whole page.
  270. #define MAX_VALUE (INT_MAX/2) // MAX_VALUE is the max we can use in an integer
  271. // and that allows calculations without overflow
  272. panel->SetClipBox( EDA_RECT( wxPoint( 0, 0 ), wxSize( MAX_VALUE, MAX_VALUE ) ) );
  273. // Change scale factor and offset to print the whole page.
  274. bool printReference = parent->GetPrintSheetReference();
  275. pageSizeIU = aScreen->GetPageSettings().GetSizeIU();
  276. FitThisSizeToPaper( pageSizeIU );
  277. fitRect = GetLogicalPaperRect();
  278. wxLogDebug( wxT( "Fit rectangle: %d, %d, %d, %d" ),
  279. fitRect.x, fitRect.y, fitRect.width, fitRect.height );
  280. int xoffset = ( fitRect.width - pageSizeIU.x ) / 2;
  281. int yoffset = ( fitRect.height - pageSizeIU.y ) / 2;
  282. OffsetLogicalOrigin( xoffset, yoffset );
  283. GRResetPenAndBrush( dc );
  284. if( parent->GetPrintMonochrome() )
  285. GRForceBlackPen( true );
  286. aScreen->m_IsPrinting = true;
  287. int bg_color = g_DrawBgColor;
  288. aScreen->Draw( panel, dc, GR_DEFAULT_DRAWMODE );
  289. if( printReference )
  290. parent->TraceWorkSheet( dc, aScreen, g_DrawDefaultLineThickness, IU_PER_MILS );
  291. g_DrawBgColor = bg_color;
  292. aScreen->m_IsPrinting = false;
  293. panel->SetClipBox( oldClipBox );
  294. GRForceBlackPen( false );
  295. aScreen->m_StartVisu = tmp_startvisu;
  296. aScreen->m_DrawOrg = old_org;
  297. aScreen->SetZoom( oldZoom );
  298. }