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.

470 lines
14 KiB

  1. /****************************************/
  2. /* File: dialog_print_using_printer.cpp */
  3. /****************************************/
  4. // Set this to 1 if you want to test PostScript printing under MSW.
  5. #define wxTEST_POSTSCRIPT_IN_MSW 1
  6. #include "fctsys.h"
  7. #include "appl_wxstruct.h"
  8. #include "gr_basic.h"
  9. #include "common.h"
  10. #include "class_drawpanel.h"
  11. #include "confirm.h"
  12. #include "program.h"
  13. #include "general.h"
  14. #include <wx/dcps.h>
  15. #include "dialog_print_using_printer_base.h"
  16. #define DEFAULT_ORIENTATION_PAPER wxLANDSCAPE // other option is wxPORTRAIT
  17. #define WIDTH_MAX_VALUE 100
  18. #define WIDTH_MIN_VALUE 1
  19. #define PRINTMODECOLOR_KEY wxT("PrintModeColor")
  20. // static print data and page setup data, to remember settings during the session
  21. static wxPrintData* g_PrintData;
  22. // Variables locales
  23. static int s_OptionPrintPage = 0;
  24. static int s_Print_Black_and_White = true;
  25. static bool s_Print_Frame_Ref = true;
  26. /* Dialog to print schematic. Class derived from DIALOG_PRINT_USING_PRINTER_base
  27. * created by wxFormBuilder
  28. */
  29. class DIALOG_PRINT_USING_PRINTER : public DIALOG_PRINT_USING_PRINTER_base
  30. {
  31. private:
  32. WinEDA_DrawFrame* m_Parent;
  33. wxConfig* m_Config;
  34. public:
  35. DIALOG_PRINT_USING_PRINTER( WinEDA_DrawFrame* parent );
  36. ~DIALOG_PRINT_USING_PRINTER() {};
  37. private:
  38. void OnCloseWindow( wxCloseEvent& event );
  39. void OnInitDialog( wxInitDialogEvent& event );
  40. void OnPrintSetup( wxCommandEvent& event );
  41. void OnPrintPreview( wxCommandEvent& event );
  42. void OnPrintButtonClick( wxCommandEvent& event );
  43. void OnButtonCancelClick( wxCommandEvent& event ){ Close(); }
  44. void SetPenWidth();
  45. };
  46. /***************************/
  47. /* Gestion de l'impression */
  48. /***************************/
  49. class EDA_Printout : public wxPrintout
  50. {
  51. public:
  52. bool m_Print_Sheet_Ref;
  53. public:
  54. WinEDA_DrawFrame* m_Parent;
  55. DIALOG_PRINT_USING_PRINTER* m_PrintFrame;
  56. EDA_Printout( DIALOG_PRINT_USING_PRINTER* aPrint_frame,
  57. WinEDA_DrawFrame* aParent,
  58. const wxString& aTitle,
  59. bool aPrint_ref ) :
  60. wxPrintout( aTitle )
  61. {
  62. m_PrintFrame = aPrint_frame;
  63. m_Parent = aParent;
  64. m_Print_Sheet_Ref = aPrint_ref;
  65. }
  66. bool OnPrintPage( int page );
  67. bool HasPage( int page );
  68. bool OnBeginDocument( int startPage, int endPage );
  69. void GetPageInfo( int* minPage, int* maxPage, int* selPageFrom, int* selPageTo );
  70. void DrawPage();
  71. };
  72. /*******************************************************/
  73. void WinEDA_DrawFrame::ToPrinter( wxCommandEvent& event )
  74. /*******************************************************/
  75. /* Virtual function
  76. * Calls the print dialog for Eeschema
  77. */
  78. {
  79. if( g_PrintData == NULL ) // First call. creates print handlers
  80. {
  81. g_PrintData = new wxPrintData();
  82. if( !g_PrintData->Ok() )
  83. {
  84. DisplayError( this, _( "Error Init Printer info" ) );
  85. }
  86. g_PrintData->SetQuality( wxPRINT_QUALITY_HIGH ); // Default resolution = HIGHT;
  87. g_PrintData->SetOrientation( DEFAULT_ORIENTATION_PAPER );
  88. }
  89. DIALOG_PRINT_USING_PRINTER* frame = new DIALOG_PRINT_USING_PRINTER( this );
  90. frame->ShowModal(); frame->Destroy();
  91. }
  92. /*************************************************************************************/
  93. DIALOG_PRINT_USING_PRINTER::DIALOG_PRINT_USING_PRINTER( WinEDA_DrawFrame* parent ) :
  94. DIALOG_PRINT_USING_PRINTER_base( parent )
  95. /*************************************************************************************/
  96. {
  97. m_Parent = parent;
  98. m_Config = wxGetApp().m_EDA_Config;
  99. }
  100. /************************************************************************/
  101. void DIALOG_PRINT_USING_PRINTER::OnInitDialog( wxInitDialogEvent& event )
  102. /************************************************************************/
  103. {
  104. SetFocus();
  105. if( m_Config )
  106. {
  107. m_Config->Read( PRINTMODECOLOR_KEY, &s_Print_Black_and_White );
  108. }
  109. AddUnitSymbol(* m_TextPenWidth, g_DrawDefaultLineThickness );
  110. m_DialogPenWidth->SetValue(
  111. ReturnStringFromValue(g_UnitMetric, g_DrawDefaultLineThickness, m_Parent->m_InternalUnits ) );
  112. m_Print_Sheet_Ref->SetValue( s_Print_Frame_Ref );
  113. m_ModeColorOption->SetSelection( s_Print_Black_and_White ? 1 : 0);
  114. m_Print_Sheet_Ref->SetValue(s_Print_Frame_Ref);
  115. if (GetSizer())
  116. {
  117. GetSizer()->SetSizeHints(this);
  118. }
  119. }
  120. /*********************************************************************/
  121. void DIALOG_PRINT_USING_PRINTER::OnCloseWindow( wxCloseEvent& event )
  122. /*********************************************************************/
  123. {
  124. s_Print_Black_and_White = m_ModeColorOption->GetSelection();
  125. s_Print_Frame_Ref = m_Print_Sheet_Ref->GetValue();
  126. SetPenWidth();
  127. if( m_Config )
  128. {
  129. m_Config->Write( PRINTMODECOLOR_KEY, s_Print_Black_and_White );
  130. }
  131. EndModal( 0 );
  132. }
  133. /****************************************/
  134. void DIALOG_PRINT_USING_PRINTER::SetPenWidth()
  135. /****************************************/
  136. /* Get the new pen width value, and verify min et max value
  137. * NOTE: g_PlotLine_Width is in internal units
  138. */
  139. {
  140. g_DrawDefaultLineThickness = ReturnValueFromTextCtrl( *m_DialogPenWidth, m_Parent->m_InternalUnits );
  141. if( g_DrawDefaultLineThickness > WIDTH_MAX_VALUE )
  142. {
  143. g_DrawDefaultLineThickness = WIDTH_MAX_VALUE;
  144. }
  145. if( g_DrawDefaultLineThickness < WIDTH_MIN_VALUE )
  146. {
  147. g_DrawDefaultLineThickness = WIDTH_MIN_VALUE;
  148. }
  149. m_DialogPenWidth->SetValue(
  150. ReturnStringFromValue(g_UnitMetric, g_DrawDefaultLineThickness, m_Parent->m_InternalUnits ) );
  151. }
  152. /**********************************************************/
  153. void DIALOG_PRINT_USING_PRINTER::OnPrintSetup( wxCommandEvent& event )
  154. /**********************************************************/
  155. /* Open a dialog box for printer setup (printer options, page size ...)
  156. */
  157. {
  158. wxPrintDialogData printDialogData( *g_PrintData );
  159. if( printDialogData.Ok() )
  160. {
  161. wxPrintDialog printerDialog( this, &printDialogData );
  162. printerDialog.ShowModal();
  163. *g_PrintData = printerDialog.GetPrintDialogData().GetPrintData();
  164. }
  165. else
  166. DisplayError( this, _( "Printer Problem!" ) );
  167. }
  168. /***********************************************************************/
  169. void DIALOG_PRINT_USING_PRINTER::OnPrintPreview( wxCommandEvent& event )
  170. /***********************************************************************/
  171. /* Open and display a previewer frame for printing
  172. */
  173. {
  174. wxSize WSize;
  175. wxPoint WPos;
  176. bool print_ref = m_Print_Sheet_Ref->GetValue();
  177. s_Print_Black_and_White = m_ModeColorOption->GetSelection();
  178. SetPenWidth();
  179. s_OptionPrintPage = m_PagesOption->GetSelection();
  180. // Pass two printout objects: for preview, and possible printing.
  181. wxString title = _("Preview");
  182. wxPrintPreview* preview =
  183. new wxPrintPreview( new EDA_Printout( this, m_Parent, title, print_ref ),
  184. new EDA_Printout( this, m_Parent, title, print_ref ), g_PrintData );
  185. if( preview == NULL )
  186. {
  187. DisplayError( this, wxT( "OnPrintPreview() problem" ) );
  188. return;
  189. }
  190. WPos = m_Parent->GetPosition( );
  191. WSize = m_Parent->GetSize();
  192. wxPreviewFrame* frame = new wxPreviewFrame( preview, this,
  193. title, WPos, WSize );
  194. frame->Initialize();
  195. frame->Show( true );
  196. }
  197. /**************************************************************************/
  198. void DIALOG_PRINT_USING_PRINTER::OnPrintButtonClick( wxCommandEvent& event )
  199. /**************************************************************************/
  200. {
  201. bool print_ref = m_Print_Sheet_Ref->GetValue();
  202. s_Print_Black_and_White = m_ModeColorOption->GetSelection();
  203. s_OptionPrintPage = 0;
  204. if( m_PagesOption )
  205. s_OptionPrintPage = m_PagesOption->GetSelection();
  206. SetPenWidth();
  207. wxPrintDialogData printDialogData( *g_PrintData );
  208. wxPrinter printer( &printDialogData );
  209. wxString title = _("Preview");
  210. EDA_Printout printout( this, m_Parent, title, print_ref );
  211. #if !defined(__WINDOWS__) && !wxCHECK_VERSION(2,9,0)
  212. wxDC* dc = printout.GetDC();
  213. ( (wxPostScriptDC*) dc )->SetResolution( 600 ); // Postscript DC resolution is 600 ppi
  214. #endif
  215. if( !printer.Print( this, &printout, true ) )
  216. {
  217. if( wxPrinter::GetLastError() == wxPRINTER_ERROR )
  218. DisplayError( this, _( "There was a problem printing" ) );
  219. return;
  220. }
  221. else
  222. {
  223. *g_PrintData = printer.GetPrintDialogData().GetPrintData();
  224. }
  225. }
  226. /***************************************/
  227. bool EDA_Printout::OnPrintPage( int page )
  228. /***************************************/
  229. {
  230. wxString msg;
  231. msg.Printf( _( "Print page %d" ), page );
  232. m_Parent->Affiche_Message( msg );
  233. WinEDA_SchematicFrame* schframe = (WinEDA_SchematicFrame*) m_Parent;
  234. SCH_SCREEN* screen = schframe->GetScreen();
  235. SCH_SCREEN* oldscreen = screen;
  236. DrawSheetPath* oldsheetpath = schframe->GetSheet();
  237. DrawSheetPath list;
  238. if( s_OptionPrintPage == 1 )
  239. {
  240. /* Print all pages, so when called, the page is not the current page.
  241. * We must select it and setup references and others parameters
  242. * because in complex hierarchies a SCH_SCREEN (a schematic drawings)
  243. * is shared between many sheets
  244. */
  245. EDA_SheetList SheetList( NULL );
  246. DrawSheetPath* sheetpath = SheetList.GetSheet( page - 1 );
  247. if( list.BuildSheetPathInfoFromSheetPathValue( sheetpath->Path() ) )
  248. {
  249. schframe->m_CurrentSheet = &list;
  250. schframe->m_CurrentSheet->UpdateAllScreenReferences();
  251. schframe->SetSheetNumberAndCount();
  252. screen = schframe->m_CurrentSheet->LastScreen();
  253. }
  254. else
  255. screen = NULL;
  256. }
  257. if( screen == NULL )
  258. return false;
  259. ActiveScreen = screen;
  260. DrawPage();
  261. ActiveScreen = oldscreen;
  262. schframe->m_CurrentSheet = oldsheetpath;
  263. schframe->m_CurrentSheet->UpdateAllScreenReferences();
  264. schframe->SetSheetNumberAndCount();
  265. return true;
  266. }
  267. /*********************************************************/
  268. void EDA_Printout::GetPageInfo( int* minPage, int* maxPage,
  269. int* selPageFrom, int* selPageTo )
  270. /*********************************************************/
  271. {
  272. int ii = 1;
  273. *minPage = 1;
  274. *selPageFrom = 1;
  275. if( s_OptionPrintPage == 1 )
  276. {
  277. ii = g_RootSheet->CountSheets();
  278. }
  279. *maxPage = ii;
  280. *selPageTo = ii;
  281. }
  282. /**************************************/
  283. bool EDA_Printout::HasPage( int pageNum )
  284. /**************************************/
  285. {
  286. int pageCount;
  287. pageCount = g_RootSheet->CountSheets();
  288. if( pageCount >= pageNum )
  289. return true;
  290. return false;
  291. }
  292. /*************************************************************/
  293. bool EDA_Printout::OnBeginDocument( int startPage, int endPage )
  294. /*************************************************************/
  295. {
  296. if( !wxPrintout::OnBeginDocument( startPage, endPage ) )
  297. return false;
  298. return true;
  299. }
  300. /********************************/
  301. void EDA_Printout::DrawPage()
  302. /********************************/
  303. /*
  304. * This is the real print function: print the active screen
  305. */
  306. {
  307. int tmpzoom;
  308. wxPoint tmp_startvisu;
  309. wxSize PageSize_in_mm;
  310. wxSize SheetSize; // Page size in internal units
  311. wxSize PlotAreaSize; // plot area size in pixels
  312. double scaleX, scaleY, scale;
  313. wxPoint old_org;
  314. wxPoint DrawOffset; // Offset de trace
  315. double DrawZoom = 1;
  316. wxDC* dc = GetDC();
  317. wxBusyCursor dummy;
  318. GetPageSizeMM( &PageSize_in_mm.x, &PageSize_in_mm.y );
  319. /* Save old draw scale and draw offset */
  320. tmp_startvisu = ActiveScreen->m_StartVisu;
  321. tmpzoom = ActiveScreen->GetZoom();
  322. old_org = ActiveScreen->m_DrawOrg;
  323. /* Change draw scale and offset to draw the whole page */
  324. ActiveScreen->SetScalingFactor( DrawZoom );
  325. ActiveScreen->m_DrawOrg.x = ActiveScreen->m_DrawOrg.y = 0;
  326. ActiveScreen->m_StartVisu.x = ActiveScreen->m_StartVisu.y = 0;
  327. SheetSize = ActiveScreen->m_CurrentSheetDesc->m_Size; // size in 1/1000 inch
  328. SheetSize.x *= m_Parent->m_InternalUnits / 1000;
  329. SheetSize.y *= m_Parent->m_InternalUnits / 1000; // size in pixels
  330. // Get the size of the DC in pixels
  331. dc->GetSize( &PlotAreaSize.x, &PlotAreaSize.y );
  332. // Calculate a suitable scaling factor
  333. scaleX = (double) SheetSize.x / PlotAreaSize.x;
  334. scaleY = (double) SheetSize.y / PlotAreaSize.y;
  335. scale = wxMax( scaleX, scaleY ); // Use x or y scaling factor, whichever fits on the DC
  336. // ajust the real draw scale
  337. dc->SetUserScale( DrawZoom / scale, DrawZoom / scale );
  338. ActiveScreen->m_DrawOrg = DrawOffset;
  339. GRResetPenAndBrush( dc );
  340. if( s_Print_Black_and_White )
  341. GRForceBlackPen( true );
  342. /* set Pen min width (not used now) */
  343. SetPenMinWidth( 1 );
  344. WinEDA_DrawPanel* panel = m_Parent->DrawPanel;
  345. BASE_SCREEN* screen = panel->GetScreen();
  346. EDA_Rect tmp = panel->m_ClipBox;
  347. panel->m_ClipBox.SetOrigin( wxPoint( 0, 0 ) );
  348. panel->m_ClipBox.SetSize( wxSize( 0x7FFFFF0, 0x7FFFFF0 ) );
  349. screen->m_IsPrinting = true;
  350. int bg_color = g_DrawBgColor;
  351. panel->PrintPage( dc, m_Print_Sheet_Ref, 0xFFFFFFFF, false );
  352. g_DrawBgColor = bg_color;
  353. screen->m_IsPrinting = false;
  354. panel->m_ClipBox = tmp;
  355. SetPenMinWidth( 1 );
  356. GRForceBlackPen( false );
  357. ActiveScreen->m_StartVisu = tmp_startvisu;
  358. ActiveScreen->m_DrawOrg = old_org;
  359. ActiveScreen->SetZoom( tmpzoom );
  360. }