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.

238 lines
7.0 KiB

16 years ago
15 years ago
16 years ago
  1. /************/
  2. /* zoom.cpp */
  3. /************/
  4. /*
  5. * Manage zoom, grid step, and auto crop.
  6. */
  7. #include "fctsys.h"
  8. #include "common.h"
  9. #include "macros.h"
  10. #include "bitmaps.h"
  11. #include "id.h"
  12. #include "class_drawpanel.h"
  13. #include "class_base_screen.h"
  14. #include "wxstruct.h"
  15. #include "kicad_device_context.h"
  16. #include "hotkeys_basic.h"
  17. void EDA_DRAW_FRAME::RedrawScreen( const wxPoint& aCenterPoint, bool aWarpPointer )
  18. {
  19. AdjustScrollBars( aCenterPoint );
  20. // Move the mouse cursor to the on grid graphic cursor position
  21. if( aWarpPointer )
  22. DrawPanel->MoveCursorToCrossHair();
  23. DrawPanel->Refresh();
  24. DrawPanel->Update();
  25. }
  26. /** Redraw the screen with best zoom level and the best centering
  27. * that shows all the page or the board
  28. */
  29. void EDA_DRAW_FRAME::Zoom_Automatique( bool aWarpPointer )
  30. {
  31. BASE_SCREEN * screen = GetScreen();
  32. screen->SetZoom( BestZoom() ); // Set the best zoom and get center point.
  33. if( screen->m_FirstRedraw )
  34. screen->SetCrossHairPosition( screen->GetScrollCenterPosition() );
  35. RedrawScreen( screen->GetScrollCenterPosition(), aWarpPointer );
  36. }
  37. /** Compute the zoom factor and the new draw offset to draw the
  38. * selected area (Rect) in full window screen
  39. * @param Rect = selected area to show after zooming
  40. */
  41. void EDA_DRAW_FRAME::Window_Zoom( EDA_RECT& Rect )
  42. {
  43. double scalex, bestscale;
  44. wxSize size;
  45. /* Compute the best zoom */
  46. Rect.Normalize();
  47. size = DrawPanel->GetClientSize();
  48. // Use ceil to at least show the full rect
  49. scalex = (double) Rect.GetSize().x / size.x;
  50. bestscale = (double) Rect.GetSize().y / size.y;
  51. bestscale = MAX( bestscale, scalex );
  52. GetScreen()->SetScalingFactor( bestscale );
  53. RedrawScreen( Rect.Centre(), true );
  54. }
  55. /**
  56. * Function OnZoom
  57. * Called from any zoom event (toolbar , hotkey or popup )
  58. */
  59. void EDA_DRAW_FRAME::OnZoom( wxCommandEvent& event )
  60. {
  61. if( DrawPanel == NULL )
  62. return;
  63. int i;
  64. int id = event.GetId();
  65. bool zoom_at_cursor = false;
  66. BASE_SCREEN* screen = GetScreen();
  67. wxPoint center = screen->GetScrollCenterPosition();
  68. switch( id )
  69. {
  70. case ID_POPUP_ZOOM_IN:
  71. zoom_at_cursor = true;
  72. center = screen->GetCrossHairPosition();
  73. // fall thru
  74. case ID_ZOOM_IN:
  75. if( screen->SetPreviousZoom() )
  76. RedrawScreen( center, zoom_at_cursor );
  77. break;
  78. case ID_POPUP_ZOOM_OUT:
  79. zoom_at_cursor = true;
  80. center = screen->GetCrossHairPosition();
  81. // fall thru
  82. case ID_ZOOM_OUT:
  83. if( screen->SetNextZoom() )
  84. RedrawScreen( center, zoom_at_cursor );
  85. break;
  86. case ID_ZOOM_REDRAW:
  87. DrawPanel->Refresh();
  88. break;
  89. case ID_POPUP_ZOOM_CENTER:
  90. center = screen->GetCrossHairPosition();
  91. RedrawScreen( center, true );
  92. break;
  93. case ID_ZOOM_PAGE:
  94. Zoom_Automatique( false );
  95. break;
  96. case ID_POPUP_ZOOM_SELECT:
  97. break;
  98. case ID_POPUP_CANCEL:
  99. DrawPanel->MoveCursorToCrossHair();
  100. break;
  101. default:
  102. i = id - ID_POPUP_ZOOM_LEVEL_START;
  103. if( ( i < 0 ) || ( (size_t) i >= screen->m_ZoomList.GetCount() ) )
  104. {
  105. wxLogDebug( wxT( "%s %d: index %d is outside the bounds of the zoom list." ),
  106. __TFILE__, __LINE__, i );
  107. return;
  108. }
  109. if( screen->SetZoom( screen->m_ZoomList[i] ) )
  110. RedrawScreen( center, true );
  111. }
  112. UpdateStatusBar();
  113. }
  114. /* add the zoom list menu the the MasterMenu.
  115. * used in OnRightClick(wxMouseEvent& event)
  116. */
  117. void EDA_DRAW_FRAME::AddMenuZoomAndGrid( wxMenu* MasterMenu )
  118. {
  119. int maxZoomIds;
  120. int zoom;
  121. wxString msg;
  122. BASE_SCREEN * screen = DrawPanel->GetScreen();
  123. msg = AddHotkeyName( _( "Center" ), m_HotkeysZoomAndGridList, HK_ZOOM_CENTER );
  124. ADD_MENUITEM( MasterMenu, ID_POPUP_ZOOM_CENTER, msg, zoom_center_on_screen_xpm );
  125. msg = AddHotkeyName( _( "Zoom in" ), m_HotkeysZoomAndGridList, HK_ZOOM_IN );
  126. ADD_MENUITEM( MasterMenu, ID_POPUP_ZOOM_IN, msg, zoom_in_xpm );
  127. msg = AddHotkeyName( _( "Zoom out" ), m_HotkeysZoomAndGridList, HK_ZOOM_OUT );
  128. ADD_MENUITEM( MasterMenu, ID_POPUP_ZOOM_OUT, msg, zoom_out_xpm );
  129. msg = AddHotkeyName( _( "Redraw view" ), m_HotkeysZoomAndGridList, HK_ZOOM_REDRAW );
  130. ADD_MENUITEM( MasterMenu, ID_ZOOM_REDRAW, msg, zoom_redraw_xpm );
  131. msg = AddHotkeyName( _( "Zoom auto" ), m_HotkeysZoomAndGridList, HK_ZOOM_AUTO );
  132. ADD_MENUITEM( MasterMenu, ID_ZOOM_PAGE, msg, zoom_fit_in_page_xpm );
  133. wxMenu* zoom_choice = new wxMenu;
  134. ADD_MENUITEM_WITH_SUBMENU( MasterMenu, zoom_choice,
  135. ID_POPUP_ZOOM_SELECT, _( "Zoom select" ),
  136. zoom_selection_xpm );
  137. zoom = screen->GetZoom();
  138. maxZoomIds = ID_POPUP_ZOOM_LEVEL_END - ID_POPUP_ZOOM_LEVEL_START;
  139. maxZoomIds = ( (size_t) maxZoomIds < screen->m_ZoomList.GetCount() ) ?
  140. maxZoomIds : screen->m_ZoomList.GetCount();
  141. /* Populate zoom submenu. */
  142. for( int i = 0; i < maxZoomIds; i++ )
  143. {
  144. msg.Printf( wxT( "%g" ), screen->m_ZoomList[i] );
  145. zoom_choice->Append( ID_POPUP_ZOOM_LEVEL_START + i, _( "Zoom: " ) + msg,
  146. wxEmptyString, wxITEM_CHECK );
  147. if( zoom == screen->m_ZoomList[i] )
  148. zoom_choice->Check( ID_POPUP_ZOOM_LEVEL_START + i, true );
  149. }
  150. /* Create grid submenu as required. */
  151. if( screen->GetGridCount() )
  152. {
  153. wxMenu* gridMenu = new wxMenu;
  154. ADD_MENUITEM_WITH_SUBMENU( MasterMenu, gridMenu, ID_POPUP_GRID_SELECT,
  155. _( "Grid Select" ), grid_select_xpm );
  156. GRID_TYPE tmp;
  157. wxRealPoint grid = screen->GetGridSize();
  158. for( size_t i = 0; i < screen->GetGridCount(); i++ )
  159. {
  160. tmp = screen->GetGrid( i );
  161. double gridValueInch = To_User_Unit( INCHES, tmp.m_Size.x, m_InternalUnits );
  162. double gridValue_mm = To_User_Unit( MILLIMETRES, tmp.m_Size.x, m_InternalUnits );
  163. if( tmp.m_Id == ID_POPUP_GRID_USER )
  164. {
  165. msg = _( "User Grid" );
  166. }
  167. else
  168. {
  169. switch( g_UserUnit )
  170. {
  171. case INCHES:
  172. msg.Printf( wxT( "%.1f mils, (%.3f mm)" ),
  173. gridValueInch * 1000, gridValue_mm );
  174. break;
  175. case MILLIMETRES:
  176. msg.Printf( wxT( "%.3f mm, (%.1f mils)" ),
  177. gridValue_mm, gridValueInch * 1000 );
  178. break;
  179. case UNSCALED_UNITS:
  180. msg = wxT( "???" );
  181. break;
  182. }
  183. }
  184. gridMenu->Append( tmp.m_Id, msg, wxEmptyString, true );
  185. if( grid == tmp.m_Size )
  186. gridMenu->Check( tmp.m_Id, true );
  187. }
  188. }
  189. MasterMenu->AppendSeparator();
  190. ADD_MENUITEM( MasterMenu, ID_POPUP_CANCEL, _( "Close" ), cancel_xpm );
  191. }