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.

389 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 "common.h"
  8. #include "class_drawpanel.h"
  9. #include "confirm.h"
  10. #include "class_sch_screen.h"
  11. #include "wxEeschemaStruct.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();
  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. }
  78. SCH_EDIT_FRAME* DIALOG_PRINT_USING_PRINTER::GetParent() const
  79. {
  80. return ( SCH_EDIT_FRAME* ) wxWindow::GetParent();
  81. }
  82. void DIALOG_PRINT_USING_PRINTER::OnInitDialog( wxInitDialogEvent& event )
  83. {
  84. SCH_EDIT_FRAME* parent = GetParent();
  85. if ( GetSizer() )
  86. GetSizer()->SetSizeHints( this );
  87. if( parent->GetPrintDialogPosition() == wxDefaultPosition &&
  88. parent->GetPrintDialogSize() == wxDefaultSize )
  89. {
  90. Center();
  91. }
  92. else
  93. {
  94. SetPosition( parent->GetPrintDialogPosition() );
  95. SetSize( parent->GetPrintDialogSize() );
  96. }
  97. SetFocus();
  98. m_buttonPrint->SetDefault();
  99. }
  100. void DIALOG_PRINT_USING_PRINTER::OnCloseWindow( wxCloseEvent& event )
  101. {
  102. SCH_EDIT_FRAME* parent = GetParent();
  103. if( !IsIconized() )
  104. {
  105. parent->SetPrintDialogPosition( GetPosition() );
  106. parent->SetPrintDialogSize( GetSize() );
  107. }
  108. parent->SetPrintMonochrome( m_checkMonochrome->IsChecked() );
  109. parent->SetPrintSheetReference( m_checkReference->IsChecked() );
  110. EndDialog( wxID_CANCEL );
  111. }
  112. /* Open a dialog box for printer setup (printer options, page size ...)
  113. */
  114. void DIALOG_PRINT_USING_PRINTER::OnPageSetup( wxCommandEvent& event )
  115. {
  116. SCH_EDIT_FRAME* parent = GetParent();
  117. wxPageSetupDialog pageSetupDialog( this, &parent->GetPageSetupData() );
  118. pageSetupDialog.ShowModal();
  119. parent->GetPageSetupData() = pageSetupDialog.GetPageSetupDialogData();
  120. }
  121. /* Open and display a previewer frame for printing
  122. */
  123. void DIALOG_PRINT_USING_PRINTER::OnPrintPreview( wxCommandEvent& event )
  124. {
  125. SCH_EDIT_FRAME* parent = GetParent();
  126. parent->SetPrintMonochrome( m_checkMonochrome->IsChecked() );
  127. parent->SetPrintSheetReference( m_checkReference->IsChecked() );
  128. // Pass two printout objects: for preview, and possible printing.
  129. wxString title = _( "Preview" );
  130. wxPrintPreview* preview = new wxPrintPreview( new SCH_PRINTOUT( this, title ),
  131. new SCH_PRINTOUT( this, title ),
  132. &parent->GetPageSetupData().GetPrintData() );
  133. if( preview == NULL )
  134. {
  135. DisplayError( this, wxT( "Print preview error!" ) );
  136. return;
  137. }
  138. preview->SetZoom( 100 );
  139. SCH_PREVIEW_FRAME* frame = new SCH_PREVIEW_FRAME( preview, this, title,
  140. parent->GetPreviewPosition(),
  141. parent->GetPreviewSize() );
  142. frame->Initialize();
  143. frame->Show( true );
  144. }
  145. void DIALOG_PRINT_USING_PRINTER::OnPrintButtonClick( wxCommandEvent& event )
  146. {
  147. SCH_EDIT_FRAME* parent = GetParent();
  148. parent->SetPrintMonochrome( m_checkMonochrome->IsChecked() );
  149. parent->SetPrintSheetReference( m_checkReference->IsChecked() );
  150. wxPrintDialogData printDialogData( parent->GetPageSetupData().GetPrintData() );
  151. printDialogData.SetMaxPage( g_RootSheet->CountSheets() );
  152. if( g_RootSheet->CountSheets() > 1 )
  153. printDialogData.EnablePageNumbers( true );
  154. wxPrinter printer( &printDialogData );
  155. SCH_PRINTOUT printout( this, _( "Print Schematic" ) );
  156. if( !printer.Print( this, &printout, true ) )
  157. {
  158. if( wxPrinter::GetLastError() == wxPRINTER_ERROR )
  159. wxMessageBox( _( "An error occurred attempting to print the schematic." ),
  160. _( "Printing" ), wxOK );
  161. }
  162. else
  163. {
  164. parent->GetPageSetupData() = printer.GetPrintDialogData().GetPrintData();
  165. }
  166. }
  167. bool SCH_PRINTOUT::OnPrintPage( int page )
  168. {
  169. wxString msg;
  170. SCH_EDIT_FRAME* parent = m_Parent->GetParent();
  171. msg.Printf( _( "Print page %d" ), page );
  172. parent->ClearMsgPanel();
  173. parent->AppendMsgPanel( msg, wxEmptyString, CYAN );
  174. SCH_SCREEN* screen = parent->GetScreen();
  175. SCH_SCREEN* oldscreen = screen;
  176. SCH_SHEET_PATH* oldsheetpath = parent->GetSheet();
  177. SCH_SHEET_PATH list;
  178. SCH_SHEET_LIST SheetList( NULL );
  179. SCH_SHEET_PATH* sheetpath = SheetList.GetSheet( page - 1 );
  180. if( list.BuildSheetPathInfoFromSheetPathValue( sheetpath->Path() ) )
  181. {
  182. parent->m_CurrentSheet = &list;
  183. parent->m_CurrentSheet->UpdateAllScreenReferences();
  184. parent->SetSheetNumberAndCount();
  185. screen = parent->m_CurrentSheet->LastScreen();
  186. }
  187. else
  188. {
  189. screen = NULL;
  190. }
  191. if( screen == NULL )
  192. return false;
  193. ActiveScreen = screen;
  194. DrawPage();
  195. ActiveScreen = oldscreen;
  196. parent->m_CurrentSheet = oldsheetpath;
  197. parent->m_CurrentSheet->UpdateAllScreenReferences();
  198. parent->SetSheetNumberAndCount();
  199. return true;
  200. }
  201. void SCH_PRINTOUT::GetPageInfo( int* minPage, int* maxPage,
  202. int* selPageFrom, int* selPageTo )
  203. {
  204. *minPage = *selPageFrom = 1;
  205. *maxPage = *selPageTo = g_RootSheet->CountSheets();
  206. }
  207. bool SCH_PRINTOUT::HasPage( int pageNum )
  208. {
  209. int pageCount;
  210. pageCount = g_RootSheet->CountSheets();
  211. if( pageCount >= pageNum )
  212. return true;
  213. return false;
  214. }
  215. bool SCH_PRINTOUT::OnBeginDocument( int startPage, int endPage )
  216. {
  217. if( !wxPrintout::OnBeginDocument( startPage, endPage ) )
  218. return false;
  219. #ifdef __WXDEBUG__
  220. SCH_EDIT_FRAME* parent = m_Parent->GetParent();
  221. wxLogDebug( wxT( "Printer name: " ) +
  222. parent->GetPageSetupData().GetPrintData().GetPrinterName() );
  223. wxLogDebug( wxT( "Paper ID: %d" ),
  224. parent->GetPageSetupData().GetPrintData().GetPaperId() );
  225. wxLogDebug( wxT( "Color: %d" ),
  226. (int) parent->GetPageSetupData().GetPrintData().GetColour() );
  227. wxLogDebug( wxT( "Monochrome: %d" ), parent->GetPrintMonochrome() );
  228. wxLogDebug( wxT( "Orientation: %d:" ),
  229. parent->GetPageSetupData().GetPrintData().GetOrientation() );
  230. wxLogDebug( wxT( "Quality: %d"),
  231. parent->GetPageSetupData().GetPrintData().GetQuality() );
  232. #endif
  233. return true;
  234. }
  235. /*
  236. * This is the real print function: print the active screen
  237. */
  238. void SCH_PRINTOUT::DrawPage()
  239. {
  240. int oldZoom;
  241. wxPoint tmp_startvisu;
  242. wxSize SheetSize; // Page size in internal units
  243. wxPoint old_org;
  244. EDA_Rect oldClipBox;
  245. wxRect fitRect;
  246. wxDC* dc = GetDC();
  247. SCH_EDIT_FRAME* parent = m_Parent->GetParent();
  248. EDA_DRAW_PANEL* panel = parent->DrawPanel;
  249. wxBusyCursor dummy;
  250. /* Save current scale factor, offsets, and clip box. */
  251. tmp_startvisu = ActiveScreen->m_StartVisu;
  252. oldZoom = ActiveScreen->GetZoom();
  253. old_org = ActiveScreen->m_DrawOrg;
  254. oldClipBox = panel->m_ClipBox;
  255. /* Change scale factor, offsets, and clip box to print the whole page. */
  256. ActiveScreen->SetScalingFactor( 1.0 );
  257. ActiveScreen->m_DrawOrg.x = ActiveScreen->m_DrawOrg.y = 0;
  258. ActiveScreen->m_StartVisu.x = ActiveScreen->m_StartVisu.y = 0;
  259. panel->m_ClipBox.SetOrigin( wxPoint( 0, 0 ) );
  260. panel->m_ClipBox.SetSize( wxSize( 0x7FFFFF0, 0x7FFFFF0 ) );
  261. bool printReference = parent->GetPrintSheetReference();
  262. if( printReference )
  263. {
  264. /* Draw the page to a memory and let the dc calculate the drawing
  265. * limits.
  266. */
  267. wxBitmap psuedoBitmap( 1, 1 );
  268. wxMemoryDC memDC;
  269. memDC.SelectObject( psuedoBitmap );
  270. ( (SCH_SCREEN*) ActiveScreen )->Draw( panel, &memDC, GR_DEFAULT_DRAWMODE );
  271. parent->TraceWorkSheet( &memDC, ActiveScreen, g_DrawDefaultLineThickness );
  272. wxLogDebug( wxT( "MinX = %d, MaxX = %d, MinY = %d, MaxY = %d" ),
  273. memDC.MinX(), memDC.MaxX(), memDC.MinY(), memDC.MaxY() );
  274. SheetSize.x = memDC.MaxX() - memDC.MinX();
  275. SheetSize.y = memDC.MaxY() - memDC.MinY();
  276. FitThisSizeToPageMargins( SheetSize, parent->GetPageSetupData() );
  277. fitRect = GetLogicalPageMarginsRect( parent->GetPageSetupData() );
  278. }
  279. else
  280. {
  281. SheetSize = ActiveScreen->m_CurrentSheetDesc->m_Size;
  282. FitThisSizeToPaper( SheetSize );
  283. fitRect = GetLogicalPaperRect();
  284. }
  285. wxLogDebug( wxT( "Fit rectangle: %d, %d, %d, %d" ),
  286. fitRect.x, fitRect.y, fitRect.width, fitRect.height );
  287. GRResetPenAndBrush( dc );
  288. if( parent->GetPrintMonochrome() )
  289. GRForceBlackPen( true );
  290. ActiveScreen->m_IsPrinting = true;
  291. int bg_color = g_DrawBgColor;
  292. ( ( SCH_SCREEN* ) ActiveScreen )->Draw( panel, dc, GR_DEFAULT_DRAWMODE );
  293. if( printReference )
  294. parent->TraceWorkSheet( dc, ActiveScreen, g_DrawDefaultLineThickness );
  295. g_DrawBgColor = bg_color;
  296. ActiveScreen->m_IsPrinting = false;
  297. panel->m_ClipBox = oldClipBox;
  298. GRForceBlackPen( false );
  299. ActiveScreen->m_StartVisu = tmp_startvisu;
  300. ActiveScreen->m_DrawOrg = old_org;
  301. ActiveScreen->SetZoom( oldZoom );
  302. }