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.

937 lines
27 KiB

19 years ago
19 years ago
14 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
14 years ago
19 years ago
19 years ago
14 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2004 Jean-Pierre Charras, jean-pierre.charras@gipsa-lab.inpg.fr
  5. * Copyright (C) 2008-2011 Wayne Stambaugh <stambaughw@verizon.net>
  6. * Copyright (C) 2004-2011 KiCad Developers, see change_log.txt for contributors.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, you may find one here:
  20. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  21. * or you may search the http://www.gnu.org website for the version 2 license,
  22. * or you may write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  24. */
  25. /**
  26. * @file drawframe.cpp
  27. */
  28. #include <fctsys.h>
  29. #include <appl_wxstruct.h>
  30. #include <gr_basic.h>
  31. #include <common.h>
  32. #include <bitmaps.h>
  33. #include <macros.h>
  34. #include <id.h>
  35. #include <class_drawpanel.h>
  36. #include <class_base_screen.h>
  37. #include <msgpanel.h>
  38. #include <wxstruct.h>
  39. #include <confirm.h>
  40. #include <kicad_device_context.h>
  41. #include <dialog_helpers.h>
  42. #include <base_units.h>
  43. #include <vector2d.h>
  44. #include <wx/fontdlg.h>
  45. /**
  46. * Definition for enabling and disabling scroll bar setting trace output. See the
  47. * wxWidgets documentation on useing the WXTRACE environment variable.
  48. */
  49. static const wxString traceScrollSettings( wxT( "KicadScrollSettings" ) );
  50. // Configuration entry names.
  51. static const wxString CursorShapeEntryKeyword( wxT( "CursorShape" ) );
  52. static const wxString ShowGridEntryKeyword( wxT( "ShowGrid" ) );
  53. static const wxString GridColorEntryKeyword( wxT( "GridColor" ) );
  54. static const wxString LastGridSizeId( wxT( "_LastGridSize" ) );
  55. BEGIN_EVENT_TABLE( EDA_DRAW_FRAME, EDA_BASE_FRAME )
  56. EVT_MOUSEWHEEL( EDA_DRAW_FRAME::OnMouseEvent )
  57. EVT_MENU_OPEN( EDA_DRAW_FRAME::OnMenuOpen )
  58. EVT_ACTIVATE( EDA_DRAW_FRAME::OnActivate )
  59. EVT_MENU_RANGE( ID_ZOOM_IN, ID_ZOOM_REDRAW, EDA_DRAW_FRAME::OnZoom )
  60. EVT_MENU_RANGE( ID_OFFCENTER_ZOOM_IN, ID_OFFCENTER_ZOOM_OUT, EDA_DRAW_FRAME::OnZoom )
  61. EVT_MENU_RANGE( ID_POPUP_ZOOM_START_RANGE, ID_POPUP_ZOOM_END_RANGE,
  62. EDA_DRAW_FRAME::OnZoom )
  63. EVT_MENU_RANGE( ID_POPUP_GRID_LEVEL_1000, ID_POPUP_GRID_USER,
  64. EDA_DRAW_FRAME::OnSelectGrid )
  65. EVT_TOOL( ID_TB_OPTIONS_SHOW_GRID, EDA_DRAW_FRAME::OnToggleGridState )
  66. EVT_TOOL_RANGE( ID_TB_OPTIONS_SELECT_UNIT_MM, ID_TB_OPTIONS_SELECT_UNIT_INCH,
  67. EDA_DRAW_FRAME::OnSelectUnits )
  68. EVT_TOOL( ID_TB_OPTIONS_SELECT_CURSOR, EDA_DRAW_FRAME::OnToggleCrossHairStyle )
  69. EVT_UPDATE_UI( wxID_UNDO, EDA_DRAW_FRAME::OnUpdateUndo )
  70. EVT_UPDATE_UI( wxID_REDO, EDA_DRAW_FRAME::OnUpdateRedo )
  71. EVT_UPDATE_UI( ID_TB_OPTIONS_SHOW_GRID, EDA_DRAW_FRAME::OnUpdateGrid )
  72. EVT_UPDATE_UI( ID_TB_OPTIONS_SELECT_CURSOR, EDA_DRAW_FRAME::OnUpdateCrossHairStyle )
  73. EVT_UPDATE_UI_RANGE( ID_TB_OPTIONS_SELECT_UNIT_MM, ID_TB_OPTIONS_SELECT_UNIT_INCH,
  74. EDA_DRAW_FRAME::OnUpdateUnits )
  75. END_EVENT_TABLE()
  76. EDA_DRAW_FRAME::EDA_DRAW_FRAME( wxWindow* aParent,
  77. ID_DRAWFRAME_TYPE aFrameType,
  78. const wxString& aTitle,
  79. const wxPoint& aPos, const wxSize& aSize,
  80. long aStyle, const wxString & aFrameName ) :
  81. EDA_BASE_FRAME( aParent, aFrameType, aTitle, aPos, aSize, aStyle, aFrameName )
  82. {
  83. m_drawToolBar = NULL;
  84. m_optionsToolBar = NULL;
  85. m_gridSelectBox = NULL;
  86. m_zoomSelectBox = NULL;
  87. m_HotkeysZoomAndGridList = NULL;
  88. m_canvas = NULL;
  89. m_messagePanel = NULL;
  90. m_currentScreen = NULL;
  91. m_toolId = ID_NO_TOOL_SELECTED;
  92. m_lastDrawToolId = ID_NO_TOOL_SELECTED;
  93. m_showAxis = false; // true to draw axis.
  94. m_showBorderAndTitleBlock = false; // true to display reference sheet.
  95. m_showGridAxis = false; // true to draw the grid axis
  96. m_cursorShape = 0;
  97. m_LastGridSizeId = 0;
  98. m_DrawGrid = true; // hide/Show grid. default = show
  99. m_GridColor = DARKGRAY; // Grid color
  100. m_snapToGrid = true;
  101. m_MsgFrameHeight = EDA_MSG_PANEL::GetRequiredHeight();
  102. //#define ZOOM_DISPLAY_SIZE 60
  103. //#define COORD_DISPLAY_SIZE 165
  104. //#define DELTA_DISPLAY_SIZE 245
  105. //#define UNITS_DISPLAY_SIZE 65
  106. #define FUNCTION_DISPLAY_SIZE 110
  107. CreateStatusBar( 6 );
  108. // set the size of the status bar subwindows:
  109. wxWindow* stsbar = GetStatusBar();
  110. int dims[] = {
  111. // balance of status bar on far left is set to a default or whatever is left over.
  112. -1,
  113. // When using GetTextSize() remember the width of '1' is not the same
  114. // as the width of '0' unless the font is fixed width, and it usually won't be.
  115. // zoom:
  116. GetTextSize( wxT( "Z 762000" ), stsbar ).x + 10,
  117. // cursor coords
  118. GetTextSize( wxT( "X 0234.567890 Y 0234.567890" ), stsbar ).x + 10,
  119. // delta distances
  120. GetTextSize( wxT( "dx 0234.567890 dx 0234.567890 d 0234.567890" ), stsbar ).x + 10,
  121. // units display, Inches is bigger than mm
  122. GetTextSize( _( "Inches" ), stsbar ).x + 10,
  123. FUNCTION_DISPLAY_SIZE,
  124. };
  125. SetStatusWidths( DIM( dims ), dims );
  126. // Create child subwindows.
  127. GetClientSize( &m_FrameSize.x, &m_FrameSize.y );
  128. m_FramePos.x = m_FramePos.y = 0;
  129. m_FrameSize.y -= m_MsgFrameHeight;
  130. m_canvas = new EDA_DRAW_PANEL( this, -1, wxPoint( 0, 0 ), m_FrameSize );
  131. m_messagePanel = new EDA_MSG_PANEL( this, -1, wxPoint( 0, m_FrameSize.y ),
  132. wxSize( m_FrameSize.x, m_MsgFrameHeight ) );
  133. m_messagePanel->SetBackgroundColour( MakeColour( LIGHTGRAY ) );
  134. }
  135. EDA_DRAW_FRAME::~EDA_DRAW_FRAME()
  136. {
  137. SAFE_DELETE( m_currentScreen );
  138. m_auimgr.UnInit();
  139. }
  140. void EDA_DRAW_FRAME::unitsChangeRefresh()
  141. {
  142. UpdateStatusBar();
  143. EDA_ITEM* item = GetScreen()->GetCurItem();
  144. if( item )
  145. SetMsgPanel( item );
  146. }
  147. void EDA_DRAW_FRAME::EraseMsgBox()
  148. {
  149. if( m_messagePanel )
  150. m_messagePanel->EraseMsgBox();
  151. }
  152. void EDA_DRAW_FRAME::OnActivate( wxActivateEvent& event )
  153. {
  154. m_FrameIsActive = event.GetActive();
  155. if( m_canvas )
  156. m_canvas->SetCanStartBlock( -1 );
  157. event.Skip(); // required under wxMAC
  158. }
  159. void EDA_DRAW_FRAME::OnMenuOpen( wxMenuEvent& event )
  160. {
  161. if( m_canvas )
  162. m_canvas->SetCanStartBlock( -1 );
  163. event.Skip();
  164. }
  165. /* function SkipNextLeftButtonReleaseEvent
  166. * after calling this function, if the left mouse button
  167. * is down, the next left mouse button release event will be ignored.
  168. * It is is usefull for instance when closing a dialog on a mouse click,
  169. * to skip the next mouse left button release event
  170. * by the parent window, because the mouse button
  171. * clicked on the dialog is often released in the parent frame,
  172. * and therefore creates a left button released mouse event
  173. * which can be unwanted in some cases
  174. */
  175. void EDA_DRAW_FRAME::SkipNextLeftButtonReleaseEvent()
  176. {
  177. m_canvas->SetIgnoreLeftButtonReleaseEvent( true );
  178. }
  179. void EDA_DRAW_FRAME::OnToggleGridState( wxCommandEvent& aEvent )
  180. {
  181. SetGridVisibility( !IsGridVisible() );
  182. m_canvas->Refresh();
  183. }
  184. void EDA_DRAW_FRAME::OnSelectUnits( wxCommandEvent& aEvent )
  185. {
  186. if( aEvent.GetId() == ID_TB_OPTIONS_SELECT_UNIT_MM && g_UserUnit != MILLIMETRES )
  187. {
  188. g_UserUnit = MILLIMETRES;
  189. unitsChangeRefresh();
  190. }
  191. else if( aEvent.GetId() == ID_TB_OPTIONS_SELECT_UNIT_INCH && g_UserUnit != INCHES )
  192. {
  193. g_UserUnit = INCHES;
  194. unitsChangeRefresh();
  195. }
  196. }
  197. void EDA_DRAW_FRAME::OnToggleCrossHairStyle( wxCommandEvent& aEvent )
  198. {
  199. INSTALL_UNBUFFERED_DC( dc, m_canvas );
  200. m_canvas->CrossHairOff( &dc );
  201. m_cursorShape = !m_cursorShape;
  202. m_canvas->CrossHairOn( &dc );
  203. }
  204. void EDA_DRAW_FRAME::OnUpdateUndo( wxUpdateUIEvent& aEvent )
  205. {
  206. if( GetScreen() )
  207. aEvent.Enable( GetScreen()->GetUndoCommandCount() > 0 );
  208. }
  209. void EDA_DRAW_FRAME::OnUpdateRedo( wxUpdateUIEvent& aEvent )
  210. {
  211. if( GetScreen() )
  212. aEvent.Enable( GetScreen()->GetRedoCommandCount() > 0 );
  213. }
  214. void EDA_DRAW_FRAME::OnUpdateUnits( wxUpdateUIEvent& aEvent )
  215. {
  216. bool enable;
  217. enable = ( ((aEvent.GetId() == ID_TB_OPTIONS_SELECT_UNIT_MM) && (g_UserUnit == MILLIMETRES))
  218. || ((aEvent.GetId() == ID_TB_OPTIONS_SELECT_UNIT_INCH) && (g_UserUnit == INCHES)) );
  219. aEvent.Check( enable );
  220. DisplayUnitsMsg();
  221. }
  222. void EDA_DRAW_FRAME::OnUpdateGrid( wxUpdateUIEvent& aEvent )
  223. {
  224. wxString tool_tip = IsGridVisible() ? _( "Hide grid" ) : _( "Show grid" );
  225. aEvent.Check( IsGridVisible() );
  226. m_optionsToolBar->SetToolShortHelp( ID_TB_OPTIONS_SHOW_GRID, tool_tip );
  227. }
  228. void EDA_DRAW_FRAME::OnUpdateCrossHairStyle( wxUpdateUIEvent& aEvent )
  229. {
  230. aEvent.Check( m_cursorShape );
  231. }
  232. void EDA_DRAW_FRAME::ReCreateAuxiliaryToolbar()
  233. {
  234. }
  235. void EDA_DRAW_FRAME::ReCreateMenuBar()
  236. {
  237. }
  238. void EDA_DRAW_FRAME::OnHotKey( wxDC* aDC, int aHotKey, const wxPoint& aPosition, EDA_ITEM* aItem )
  239. {
  240. }
  241. void EDA_DRAW_FRAME::ToolOnRightClick( wxCommandEvent& event )
  242. {
  243. }
  244. void EDA_DRAW_FRAME::PrintPage( wxDC* aDC,LAYER_MSK aPrintMask, bool aPrintMirrorMode, void* aData )
  245. {
  246. wxMessageBox( wxT("EDA_DRAW_FRAME::PrintPage() error") );
  247. }
  248. void EDA_DRAW_FRAME::OnSelectGrid( wxCommandEvent& event )
  249. {
  250. int* clientData;
  251. int id = ID_POPUP_GRID_LEVEL_100;
  252. if( event.GetEventType() == wxEVT_COMMAND_COMBOBOX_SELECTED )
  253. {
  254. if( m_gridSelectBox == NULL )
  255. return;
  256. /*
  257. * Don't use wxCommandEvent::GetClientData() here. It always
  258. * returns NULL in GTK. This solution is not as elegant but
  259. * it works.
  260. */
  261. int index = m_gridSelectBox->GetSelection();
  262. wxASSERT( index != wxNOT_FOUND );
  263. clientData = (int*) m_gridSelectBox->wxItemContainer::GetClientData( index );
  264. if( clientData != NULL )
  265. id = *clientData;
  266. }
  267. else
  268. {
  269. id = event.GetId();
  270. /* Update the grid select combobox if the grid size was changed
  271. * by menu event.
  272. */
  273. if( m_gridSelectBox != NULL )
  274. {
  275. for( size_t i = 0; i < m_gridSelectBox->GetCount(); i++ )
  276. {
  277. clientData = (int*) m_gridSelectBox->wxItemContainer::GetClientData( i );
  278. if( clientData && id == *clientData )
  279. {
  280. m_gridSelectBox->SetSelection( i );
  281. break;
  282. }
  283. }
  284. }
  285. }
  286. BASE_SCREEN* screen = GetScreen();
  287. if( screen->GetGridId() == id )
  288. return;
  289. /*
  290. * This allows for saving non-sequential command ID offsets used that
  291. * may be used in the grid size combobox. Do not use the selection
  292. * index returned by GetSelection().
  293. */
  294. m_LastGridSizeId = id - ID_POPUP_GRID_LEVEL_1000;
  295. screen->SetGrid( id );
  296. screen->SetCrossHairPosition( screen->RefPos( true ) );
  297. Refresh();
  298. }
  299. void EDA_DRAW_FRAME::OnSelectZoom( wxCommandEvent& event )
  300. {
  301. if( m_zoomSelectBox == NULL )
  302. return; // Should not happen!
  303. int id = m_zoomSelectBox->GetCurrentSelection();
  304. if( id < 0 || !( id < (int)m_zoomSelectBox->GetCount() ) )
  305. return;
  306. if( id == 0 ) // Auto zoom (Fit in Page)
  307. {
  308. Zoom_Automatique( true );
  309. }
  310. else
  311. {
  312. id--;
  313. double selectedZoom = GetScreen()->m_ZoomList[id];
  314. if( GetScreen()->GetZoom() == selectedZoom )
  315. return;
  316. GetScreen()->SetZoom( selectedZoom );
  317. RedrawScreen( GetScreen()->GetScrollCenterPosition(), false );
  318. }
  319. }
  320. double EDA_DRAW_FRAME::GetZoom()
  321. {
  322. return GetScreen()->GetZoom();
  323. }
  324. void EDA_DRAW_FRAME::OnMouseEvent( wxMouseEvent& event )
  325. {
  326. event.Skip();
  327. }
  328. void EDA_DRAW_FRAME::OnLeftDClick( wxDC* DC, const wxPoint& MousePos )
  329. {
  330. }
  331. void EDA_DRAW_FRAME::DisplayToolMsg( const wxString& msg )
  332. {
  333. SetStatusText( msg, 5 );
  334. }
  335. void EDA_DRAW_FRAME::DisplayUnitsMsg()
  336. {
  337. wxString msg;
  338. switch( g_UserUnit )
  339. {
  340. case INCHES:
  341. msg = _( "Inches" );
  342. break;
  343. case MILLIMETRES:
  344. msg = _( "mm" );
  345. break;
  346. default:
  347. msg = _( "Units" );
  348. break;
  349. }
  350. SetStatusText( msg, 4 );
  351. }
  352. void EDA_DRAW_FRAME::OnSize( wxSizeEvent& SizeEv )
  353. {
  354. m_FrameSize = GetClientSize( );
  355. SizeEv.Skip();
  356. }
  357. void EDA_DRAW_FRAME::SetToolID( int aId, int aCursor, const wxString& aToolMsg )
  358. {
  359. // Keep default cursor in toolbars
  360. SetCursor( wxNullCursor );
  361. // Change m_canvas cursor if requested.
  362. if( m_canvas && aCursor >= 0 )
  363. m_canvas->SetCurrentCursor( aCursor );
  364. DisplayToolMsg( aToolMsg );
  365. if( aId < 0 )
  366. return;
  367. wxCHECK2_MSG( aId >= ID_NO_TOOL_SELECTED, aId = ID_NO_TOOL_SELECTED,
  368. wxString::Format( wxT( "Current tool ID cannot be set to %d." ), aId ) );
  369. m_toolId = aId;
  370. }
  371. void EDA_DRAW_FRAME::OnGrid( int grid_type )
  372. {
  373. }
  374. wxPoint EDA_DRAW_FRAME::GetGridPosition( const wxPoint& aPosition ) const
  375. {
  376. wxPoint pos = aPosition;
  377. if( m_currentScreen != NULL && m_snapToGrid )
  378. pos = m_currentScreen->GetNearestGridPosition( aPosition );
  379. return pos;
  380. }
  381. int EDA_DRAW_FRAME::ReturnBlockCommand( int key )
  382. {
  383. return 0;
  384. }
  385. void EDA_DRAW_FRAME::InitBlockPasteInfos()
  386. {
  387. GetScreen()->m_BlockLocate.ClearItemsList();
  388. m_canvas->SetMouseCaptureCallback( NULL );
  389. }
  390. void EDA_DRAW_FRAME::HandleBlockPlace( wxDC* DC )
  391. {
  392. }
  393. bool EDA_DRAW_FRAME::HandleBlockEnd( wxDC* DC )
  394. {
  395. return false;
  396. }
  397. void EDA_DRAW_FRAME::SetLanguage( wxCommandEvent& event )
  398. {
  399. EDA_BASE_FRAME::SetLanguage( event );
  400. }
  401. void EDA_DRAW_FRAME::UpdateStatusBar()
  402. {
  403. wxString Line;
  404. BASE_SCREEN* screen = GetScreen();
  405. if( !screen )
  406. return;
  407. // Display Zoom level: zoom = zoom_coeff/ZoomScalar
  408. Line.Printf( wxT( "Z %g" ), screen->GetZoom() );
  409. SetStatusText( Line, 1 );
  410. // Absolute and relative cursor positions are handled by overloading this function and
  411. // handling the internal to user units conversion at the appropriate level.
  412. // refresh units display
  413. DisplayUnitsMsg();
  414. }
  415. void EDA_DRAW_FRAME::LoadSettings()
  416. {
  417. wxASSERT( wxGetApp().GetSettings() != NULL );
  418. wxConfig* cfg = wxGetApp().GetSettings();
  419. EDA_BASE_FRAME::LoadSettings();
  420. cfg->Read( m_FrameName + CursorShapeEntryKeyword, &m_cursorShape, ( long )0 );
  421. bool btmp;
  422. if ( cfg->Read( m_FrameName + ShowGridEntryKeyword, &btmp ) )
  423. SetGridVisibility( btmp );
  424. int itmp;
  425. if( cfg->Read( m_FrameName + GridColorEntryKeyword, &itmp ) )
  426. SetGridColor( ColorFromInt( itmp ) );
  427. cfg->Read( m_FrameName + LastGridSizeId, &m_LastGridSizeId, 0L );
  428. }
  429. void EDA_DRAW_FRAME::SaveSettings()
  430. {
  431. wxASSERT( wxGetApp().GetSettings() != NULL );
  432. wxConfig* cfg = wxGetApp().GetSettings();
  433. EDA_BASE_FRAME::SaveSettings();
  434. cfg->Write( m_FrameName + CursorShapeEntryKeyword, m_cursorShape );
  435. cfg->Write( m_FrameName + ShowGridEntryKeyword, IsGridVisible() );
  436. cfg->Write( m_FrameName + GridColorEntryKeyword, ( long ) GetGridColor() );
  437. cfg->Write( m_FrameName + LastGridSizeId, ( long ) m_LastGridSizeId );
  438. }
  439. void EDA_DRAW_FRAME::AppendMsgPanel( const wxString& textUpper,
  440. const wxString& textLower,
  441. EDA_COLOR_T color, int pad )
  442. {
  443. if( m_messagePanel == NULL )
  444. return;
  445. m_messagePanel->AppendMessage( textUpper, textLower, color, pad );
  446. }
  447. void EDA_DRAW_FRAME::ClearMsgPanel( void )
  448. {
  449. if( m_messagePanel == NULL )
  450. return;
  451. m_messagePanel->EraseMsgBox();
  452. }
  453. void EDA_DRAW_FRAME::SetMsgPanel( const MSG_PANEL_ITEMS& aList )
  454. {
  455. if( m_messagePanel == NULL && !aList.empty() )
  456. return;
  457. ClearMsgPanel();
  458. for( unsigned i = 0; i < aList.size(); i++ )
  459. m_messagePanel->AppendMessage( aList[i] );
  460. }
  461. void EDA_DRAW_FRAME::SetMsgPanel( EDA_ITEM* aItem )
  462. {
  463. wxCHECK_RET( aItem != NULL, wxT( "Invalid EDA_ITEM pointer. Bad programmer." ) );
  464. MSG_PANEL_ITEMS items;
  465. aItem->GetMsgPanelInfo( items );
  466. SetMsgPanel( items );
  467. }
  468. wxString EDA_DRAW_FRAME::CoordinateToString( int aValue, bool aConvertToMils ) const
  469. {
  470. return ::CoordinateToString( aValue, aConvertToMils );
  471. }
  472. wxString EDA_DRAW_FRAME::LengthDoubleToString( double aValue, bool aConvertToMils ) const
  473. {
  474. return ::LengthDoubleToString( aValue, aConvertToMils );
  475. }
  476. bool EDA_DRAW_FRAME::HandleBlockBegin( wxDC* aDC, int aKey, const wxPoint& aPosition )
  477. {
  478. BLOCK_SELECTOR* Block = &GetScreen()->m_BlockLocate;
  479. if( ( Block->GetCommand() != BLOCK_IDLE ) || ( Block->GetState() != STATE_NO_BLOCK ) )
  480. return false;
  481. Block->SetCommand( (BLOCK_COMMAND_T) ReturnBlockCommand( aKey ) );
  482. if( Block->GetCommand() == 0 )
  483. return false;
  484. switch( Block->GetCommand() )
  485. {
  486. case BLOCK_IDLE:
  487. break;
  488. case BLOCK_MOVE: /* Move */
  489. case BLOCK_DRAG: /* Drag */
  490. case BLOCK_COPY: /* Copy */
  491. case BLOCK_DELETE: /* Delete */
  492. case BLOCK_SAVE: /* Save */
  493. case BLOCK_ROTATE: /* Rotate 90 deg */
  494. case BLOCK_FLIP: /* Flip */
  495. case BLOCK_ZOOM: /* Window Zoom */
  496. case BLOCK_MIRROR_X:
  497. case BLOCK_MIRROR_Y: /* mirror */
  498. case BLOCK_PRESELECT_MOVE: /* Move with preselection list*/
  499. Block->InitData( m_canvas, aPosition );
  500. break;
  501. case BLOCK_PASTE:
  502. Block->InitData( m_canvas, aPosition );
  503. Block->SetLastCursorPosition( wxPoint( 0, 0 ) );
  504. InitBlockPasteInfos();
  505. if( Block->GetCount() == 0 ) /* No data to paste */
  506. {
  507. DisplayError( this, wxT( "No Block to paste" ), 20 );
  508. GetScreen()->m_BlockLocate.SetCommand( BLOCK_IDLE );
  509. m_canvas->SetMouseCaptureCallback( NULL );
  510. return true;
  511. }
  512. if( !m_canvas->IsMouseCaptured() )
  513. {
  514. Block->ClearItemsList();
  515. DisplayError( this,
  516. wxT( "EDA_DRAW_FRAME::HandleBlockBegin() Err: m_mouseCaptureCallback NULL" ) );
  517. return true;
  518. }
  519. Block->SetState( STATE_BLOCK_MOVE );
  520. m_canvas->CallMouseCapture( aDC, aPosition, false );
  521. break;
  522. default:
  523. {
  524. wxString msg;
  525. msg << wxT( "EDA_DRAW_FRAME::HandleBlockBegin() error: Unknown command " ) <<
  526. Block->GetCommand();
  527. DisplayError( this, msg );
  528. }
  529. break;
  530. }
  531. Block->SetMessageBlock( this );
  532. return true;
  533. }
  534. // See comment in classpcb.cpp near line 66
  535. //static const double MAX_AXIS = 1518500251;
  536. // However I am not seeing a problem with this size yet:
  537. static const double MAX_AXIS = INT_MAX - 100;
  538. #define VIRT_MIN (-MAX_AXIS/2.0) ///< min X or Y coordinate in virtual space
  539. #define VIRT_MAX (MAX_AXIS/2.0) ///< max X or Y coordinate in virtual space
  540. void EDA_DRAW_FRAME::AdjustScrollBars( const wxPoint& aCenterPositionIU )
  541. {
  542. BASE_SCREEN* screen = GetScreen();
  543. if( screen == NULL || m_canvas == NULL )
  544. return;
  545. // There are no safety limits on these calculations, so in NANOMETRES build it
  546. // still blows up. This is incomplete work.
  547. double scale = screen->GetScalingFactor();
  548. wxLogTrace( traceScrollSettings, wxT( "Center Position = ( %d, %d ), scale = %.10g" ),
  549. aCenterPositionIU.x, aCenterPositionIU.y, scale );
  550. // Calculate the portion of the drawing that can be displayed in the
  551. // client area at the current zoom level.
  552. // visible viewport in device units ~ pixels
  553. wxSize clientSizeDU = m_canvas->GetClientSize();
  554. // Size of the client window in IU
  555. DSIZE clientSizeIU( clientSizeDU.x / scale, clientSizeDU.y / scale );
  556. // Full drawing or "page" rectangle in internal units
  557. DBOX pageRectIU( 0, 0, GetPageSizeIU().x, GetPageSizeIU().y );
  558. // The upper left corner of the client rectangle in internal units.
  559. double xIU = aCenterPositionIU.x - clientSizeIU.x / 2.0;
  560. double yIU = aCenterPositionIU.y - clientSizeIU.y / 2.0;
  561. // If drawn around the center, adjust the client rectangle accordingly.
  562. if( screen->m_Center )
  563. {
  564. // half page offset.
  565. xIU += pageRectIU.width / 2.0;
  566. yIU += pageRectIU.height / 2.0;
  567. }
  568. DBOX clientRectIU( xIU, yIU, clientSizeIU.x, clientSizeIU.y );
  569. wxPoint centerPositionIU;
  570. #if 1 || defined( USE_PCBNEW_NANOMETRES )
  571. // put "int" limits on the clientRect
  572. if( clientRectIU.GetLeft() < VIRT_MIN )
  573. clientRectIU.MoveLeftTo( VIRT_MIN );
  574. if( clientRectIU.GetTop() < VIRT_MIN )
  575. clientRectIU.MoveTopTo( VIRT_MIN );
  576. if( clientRectIU.GetRight() > VIRT_MAX )
  577. clientRectIU.MoveRightTo( VIRT_MAX );
  578. if( clientRectIU.GetBottom() > VIRT_MAX )
  579. clientRectIU.MoveBottomTo( VIRT_MAX );
  580. #endif
  581. centerPositionIU.x = KiROUND( clientRectIU.x + clientRectIU.width/2 );
  582. centerPositionIU.y = KiROUND( clientRectIU.y + clientRectIU.height/2 );
  583. if( screen->m_Center )
  584. {
  585. centerPositionIU.x -= KiROUND( pageRectIU.width / 2.0 );
  586. centerPositionIU.y -= KiROUND( pageRectIU.height / 2.0 );
  587. }
  588. DSIZE virtualSizeIU;
  589. if( pageRectIU.GetLeft() < clientRectIU.GetLeft() && pageRectIU.GetRight() > clientRectIU.GetRight() )
  590. {
  591. virtualSizeIU.x = pageRectIU.GetSize().x;
  592. }
  593. else
  594. {
  595. double pageCenterX = pageRectIU.x + ( pageRectIU.width / 2 );
  596. double clientCenterX = clientRectIU.x + ( clientRectIU.width / 2 );
  597. if( clientRectIU.width > pageRectIU.width )
  598. {
  599. if( pageCenterX > clientCenterX )
  600. virtualSizeIU.x = ( pageCenterX - clientRectIU.GetLeft() ) * 2;
  601. else if( pageCenterX < clientCenterX )
  602. virtualSizeIU.x = ( clientRectIU.GetRight() - pageCenterX ) * 2;
  603. else
  604. virtualSizeIU.x = clientRectIU.width;
  605. }
  606. else
  607. {
  608. if( pageCenterX > clientCenterX )
  609. virtualSizeIU.x = pageRectIU.width + ( (pageRectIU.GetLeft() - clientRectIU.GetLeft() ) * 2 );
  610. else if( pageCenterX < clientCenterX )
  611. virtualSizeIU.x = pageRectIU.width + ( (clientRectIU.GetRight() - pageRectIU.GetRight() ) * 2 );
  612. else
  613. virtualSizeIU.x = pageRectIU.width;
  614. }
  615. }
  616. if( pageRectIU.GetTop() < clientRectIU.GetTop() && pageRectIU.GetBottom() > clientRectIU.GetBottom() )
  617. {
  618. virtualSizeIU.y = pageRectIU.GetSize().y;
  619. }
  620. else
  621. {
  622. double pageCenterY = pageRectIU.y + ( pageRectIU.height / 2 );
  623. double clientCenterY = clientRectIU.y + ( clientRectIU.height / 2 );
  624. if( clientRectIU.height > pageRectIU.height )
  625. {
  626. if( pageCenterY > clientCenterY )
  627. virtualSizeIU.y = ( pageCenterY - clientRectIU.GetTop() ) * 2;
  628. else if( pageCenterY < clientCenterY )
  629. virtualSizeIU.y = ( clientRectIU.GetBottom() - pageCenterY ) * 2;
  630. else
  631. virtualSizeIU.y = clientRectIU.height;
  632. }
  633. else
  634. {
  635. if( pageCenterY > clientCenterY )
  636. virtualSizeIU.y = pageRectIU.height +
  637. ( ( pageRectIU.GetTop() - clientRectIU.GetTop() ) * 2 );
  638. else if( pageCenterY < clientCenterY )
  639. virtualSizeIU.y = pageRectIU.height +
  640. ( ( clientRectIU.GetBottom() - pageRectIU.GetBottom() ) * 2 );
  641. else
  642. virtualSizeIU.y = pageRectIU.height;
  643. }
  644. }
  645. #if 1 || defined( USE_PCBNEW_NANOMETRES )
  646. // put "int" limits on the virtualSizeIU
  647. virtualSizeIU.x = std::min( virtualSizeIU.x, MAX_AXIS );
  648. virtualSizeIU.y = std::min( virtualSizeIU.y, MAX_AXIS );
  649. #endif
  650. if( screen->m_Center )
  651. {
  652. screen->m_DrawOrg.x = -KiROUND( virtualSizeIU.x / 2.0 );
  653. screen->m_DrawOrg.y = -KiROUND( virtualSizeIU.y / 2.0 );
  654. }
  655. else
  656. {
  657. screen->m_DrawOrg.x = -KiROUND( ( virtualSizeIU.x - pageRectIU.width ) / 2.0 );
  658. screen->m_DrawOrg.y = -KiROUND( ( virtualSizeIU.y - pageRectIU.height ) / 2.0 );
  659. }
  660. /* Always set scrollbar pixels per unit to 1 unless you want the zoom
  661. * around cursor to jump around. This reported problem occurs when the
  662. * zoom point is not on a pixel per unit increment. If you set the
  663. * pixels per unit to 10, you have potential for the zoom point to
  664. * jump around +/-5 pixels from the nearest grid point.
  665. */
  666. screen->m_ScrollPixelsPerUnitX = screen->m_ScrollPixelsPerUnitY = 1;
  667. // Number of scroll bar units for the given zoom level in device units.
  668. double unitsX = virtualSizeIU.x * scale;
  669. double unitsY = virtualSizeIU.y * scale;
  670. // Calculate the scroll bar position in internal units to place the
  671. // center position at the center of client rectangle.
  672. screen->SetScrollCenterPosition( centerPositionIU );
  673. double posX = centerPositionIU.x - clientRectIU.width /2.0 - screen->m_DrawOrg.x;
  674. double posY = centerPositionIU.y - clientRectIU.height/2.0 - screen->m_DrawOrg.y;
  675. // Convert scroll bar position to device units.
  676. posX = KiROUND( posX * scale );
  677. posY = KiROUND( posY * scale );
  678. if( posX < 0 )
  679. {
  680. wxLogTrace( traceScrollSettings, wxT( "Required scroll bar X position %.10g" ), posX );
  681. posX = 0;
  682. }
  683. if( posX > unitsX )
  684. {
  685. wxLogTrace( traceScrollSettings, wxT( "Required scroll bar X position %.10g" ), posX );
  686. posX = unitsX;
  687. }
  688. if( posY < 0 )
  689. {
  690. wxLogTrace( traceScrollSettings, wxT( "Required scroll bar Y position %.10g" ), posY );
  691. posY = 0;
  692. }
  693. if( posY > unitsY )
  694. {
  695. wxLogTrace( traceScrollSettings, wxT( "Required scroll bar Y position %.10g" ), posY );
  696. posY = unitsY;
  697. }
  698. screen->m_ScrollbarPos = wxPoint( KiROUND( posX ), KiROUND( posY ) );
  699. screen->m_ScrollbarNumber = wxSize( KiROUND( unitsX ), KiROUND( unitsY ) );
  700. wxLogTrace( traceScrollSettings,
  701. wxT( "Drawing = (%.10g, %.10g), Client = (%.10g, %.10g), Offset = (%d, %d), SetScrollbars(%d, %d, %d, %d, %d, %d)" ),
  702. virtualSizeIU.x, virtualSizeIU.y, clientSizeIU.x, clientSizeIU.y,
  703. screen->m_DrawOrg.x, screen->m_DrawOrg.y,
  704. screen->m_ScrollPixelsPerUnitX, screen->m_ScrollPixelsPerUnitY,
  705. screen->m_ScrollbarNumber.x, screen->m_ScrollbarNumber.y,
  706. screen->m_ScrollbarPos.x, screen->m_ScrollbarPos.y );
  707. bool noRefresh = true;
  708. m_canvas->SetScrollbars( screen->m_ScrollPixelsPerUnitX,
  709. screen->m_ScrollPixelsPerUnitY,
  710. screen->m_ScrollbarNumber.x,
  711. screen->m_ScrollbarNumber.y,
  712. screen->m_ScrollbarPos.x,
  713. screen->m_ScrollbarPos.y, noRefresh );
  714. }