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.

581 lines
16 KiB

16 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, jaen-pierre.charras@gipsa-lab.inpg.com
  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 viewlib_frame.cpp
  27. */
  28. #include <fctsys.h>
  29. #include <appl_wxstruct.h>
  30. #include <eeschema_id.h>
  31. #include <class_drawpanel.h>
  32. #include <wxEeschemaStruct.h>
  33. #include <msgpanel.h>
  34. #include <general.h>
  35. #include <viewlib_frame.h>
  36. #include <class_library.h>
  37. #include <hotkeys.h>
  38. #include <dialog_helpers.h>
  39. /**
  40. * Save previous component library viewer state.
  41. */
  42. wxString LIB_VIEW_FRAME::m_libraryName;
  43. wxString LIB_VIEW_FRAME::m_entryName;
  44. int LIB_VIEW_FRAME::m_unit = 1;
  45. int LIB_VIEW_FRAME::m_convert = 1;
  46. /// When the viewer is used to select a component in schematic, the selected component is here.
  47. wxString LIB_VIEW_FRAME::m_exportToEeschemaCmpName;
  48. BEGIN_EVENT_TABLE( LIB_VIEW_FRAME, EDA_DRAW_FRAME )
  49. /* Window events */
  50. EVT_CLOSE( LIB_VIEW_FRAME::OnCloseWindow )
  51. EVT_SIZE( LIB_VIEW_FRAME::OnSize )
  52. EVT_ACTIVATE( LIB_VIEW_FRAME::OnActivate )
  53. /* Sash drag events */
  54. EVT_SASH_DRAGGED( ID_LIBVIEW_LIBWINDOW, LIB_VIEW_FRAME::OnSashDrag )
  55. EVT_SASH_DRAGGED( ID_LIBVIEW_CMPWINDOW, LIB_VIEW_FRAME::OnSashDrag )
  56. /* Toolbar events */
  57. EVT_TOOL_RANGE( ID_LIBVIEW_NEXT, ID_LIBVIEW_DE_MORGAN_CONVERT_BUTT,
  58. LIB_VIEW_FRAME::Process_Special_Functions )
  59. EVT_TOOL( ID_LIBVIEW_CMP_EXPORT_TO_SCHEMATIC, LIB_VIEW_FRAME::ExportToSchematicLibraryPart )
  60. EVT_COMBOBOX( ID_LIBVIEW_SELECT_PART_NUMBER, LIB_VIEW_FRAME::Process_Special_Functions )
  61. /* listbox events */
  62. EVT_LISTBOX( ID_LIBVIEW_LIB_LIST, LIB_VIEW_FRAME::ClickOnLibList )
  63. EVT_LISTBOX( ID_LIBVIEW_CMP_LIST, LIB_VIEW_FRAME::ClickOnCmpList )
  64. EVT_LISTBOX_DCLICK( ID_LIBVIEW_CMP_LIST, LIB_VIEW_FRAME::DClickOnCmpList )
  65. EVT_MENU( ID_SET_RELATIVE_OFFSET, LIB_VIEW_FRAME::OnSetRelativeOffset )
  66. END_EVENT_TABLE()
  67. /*
  68. * This emulates the zoom menu entries found in the other KiCad applications.
  69. * The library viewer does not have any menus so add an accelerator table to
  70. * the main frame.
  71. */
  72. static wxAcceleratorEntry accels[] =
  73. {
  74. wxAcceleratorEntry( wxACCEL_NORMAL, WXK_F1, ID_ZOOM_IN ),
  75. wxAcceleratorEntry( wxACCEL_NORMAL, WXK_F2, ID_ZOOM_OUT ),
  76. wxAcceleratorEntry( wxACCEL_NORMAL, WXK_F3, ID_ZOOM_REDRAW ),
  77. wxAcceleratorEntry( wxACCEL_NORMAL, WXK_F4, ID_POPUP_ZOOM_CENTER ),
  78. wxAcceleratorEntry( wxACCEL_NORMAL, WXK_HOME, ID_ZOOM_PAGE ),
  79. wxAcceleratorEntry( wxACCEL_NORMAL, WXK_SPACE, ID_SET_RELATIVE_OFFSET )
  80. };
  81. #define ACCEL_TABLE_CNT ( sizeof( accels ) / sizeof( wxAcceleratorEntry ) )
  82. #define EXTRA_BORDER_SIZE 2
  83. #define LIB_VIEW_FRAME_NAME wxT( "ViewlibFrame" )
  84. LIB_VIEW_FRAME::LIB_VIEW_FRAME( SCH_BASE_FRAME* aParent, CMP_LIBRARY* aLibrary,
  85. wxSemaphore* aSemaphore, long aStyle ) :
  86. SCH_BASE_FRAME( aParent, VIEWER_FRAME_TYPE, _( "Library Browser" ),
  87. wxDefaultPosition, wxDefaultSize, aStyle, GetLibViewerFrameName() )
  88. {
  89. wxAcceleratorTable table( ACCEL_TABLE_CNT, accels );
  90. m_FrameName = GetLibViewerFrameName();
  91. m_configPath = wxT( "LibraryViewer" );
  92. // Give an icon
  93. wxIcon icon;
  94. icon.CopyFromBitmap( KiBitmap( library_browse_xpm ) );
  95. SetIcon( icon );
  96. m_HotkeysZoomAndGridList = s_Viewlib_Hokeys_Descr;
  97. m_CmpList = NULL;
  98. m_LibList = NULL;
  99. m_LibListWindow = NULL;
  100. m_CmpListWindow = NULL;
  101. m_Semaphore = aSemaphore;
  102. if( m_Semaphore )
  103. SetModalMode( true );
  104. m_exportToEeschemaCmpName.Empty();
  105. SetScreen( new SCH_SCREEN() );
  106. GetScreen()->m_Center = true; // Axis origin centered on screen.
  107. LoadSettings();
  108. SetSize( m_FramePos.x, m_FramePos.y, m_FrameSize.x, m_FrameSize.y );
  109. // Initialize grid id to the default value (50 mils):
  110. m_LastGridSizeId = ID_POPUP_GRID_LEVEL_50 - ID_POPUP_GRID_LEVEL_1000;
  111. GetScreen()->SetGrid( ID_POPUP_GRID_LEVEL_1000 + m_LastGridSizeId );
  112. ReCreateHToolbar();
  113. ReCreateVToolbar();
  114. wxSize size = GetClientSize();
  115. size.y -= m_MsgFrameHeight + 2;
  116. m_LibListSize.y = -1;
  117. wxPoint win_pos( 0, 0 );
  118. if( aLibrary == NULL )
  119. {
  120. // Creates the libraries window display
  121. m_LibListWindow =
  122. new wxSashLayoutWindow( this, ID_LIBVIEW_LIBWINDOW, win_pos,
  123. wxDefaultSize, wxCLIP_CHILDREN | wxSW_3D,
  124. wxT( "LibWindow" ) );
  125. m_LibListWindow->SetOrientation( wxLAYOUT_VERTICAL );
  126. m_LibListWindow->SetAlignment( wxLAYOUT_LEFT );
  127. m_LibListWindow->SetSashVisible( wxSASH_RIGHT, true );
  128. m_LibListWindow->SetExtraBorderSize( EXTRA_BORDER_SIZE );
  129. m_LibList = new wxListBox( m_LibListWindow, ID_LIBVIEW_LIB_LIST,
  130. wxPoint( 0, 0 ), wxDefaultSize,
  131. 0, NULL, wxLB_HSCROLL );
  132. }
  133. else
  134. {
  135. m_libraryName = aLibrary->GetName();
  136. m_entryName.Clear();
  137. m_unit = 1;
  138. m_convert = 1;
  139. m_LibListSize.x = 0;
  140. }
  141. // Creates the component window display
  142. m_CmpListSize.y = size.y;
  143. win_pos.x = m_LibListSize.x;
  144. m_CmpListWindow = new wxSashLayoutWindow( this, ID_LIBVIEW_CMPWINDOW,
  145. win_pos, wxDefaultSize,
  146. wxCLIP_CHILDREN | wxSW_3D,
  147. wxT( "CmpWindow" ) );
  148. m_CmpListWindow->SetOrientation( wxLAYOUT_VERTICAL );
  149. m_CmpListWindow->SetSashVisible( wxSASH_RIGHT, true );
  150. m_CmpListWindow->SetExtraBorderSize( EXTRA_BORDER_SIZE );
  151. m_CmpList = new wxListBox( m_CmpListWindow, ID_LIBVIEW_CMP_LIST,
  152. wxPoint( 0, 0 ), wxDefaultSize,
  153. 0, NULL, wxLB_HSCROLL );
  154. if( m_LibList )
  155. ReCreateListLib();
  156. DisplayLibInfos();
  157. if( m_canvas )
  158. m_canvas->SetAcceleratorTable( table );
  159. m_auimgr.SetManagedWindow( this );
  160. EDA_PANEINFO horiz;
  161. horiz.HorizontalToolbarPane();
  162. EDA_PANEINFO vert;
  163. vert.VerticalToolbarPane();
  164. EDA_PANEINFO info;
  165. info.InfoToolbarPane();
  166. EDA_PANEINFO mesg;
  167. mesg.MessageToolbarPane();
  168. // Manage main toolbal
  169. m_auimgr.AddPane( m_mainToolBar,
  170. wxAuiPaneInfo( horiz ).Name( wxT ("m_mainToolBar" ) ).Top().Row( 0 ) );
  171. wxSize minsize( 60, -1 );
  172. // Manage the left window (list of libraries)
  173. if( m_LibListWindow )
  174. m_auimgr.AddPane( m_LibListWindow, wxAuiPaneInfo( info ).Name( wxT( "m_LibList" ) ).
  175. Left().Row( 0 ));
  176. // Manage the list of components)
  177. m_auimgr.AddPane( m_CmpListWindow,
  178. wxAuiPaneInfo( info ).Name( wxT( "m_CmpList" ) ).
  179. Left().Row( 1 ) );
  180. // Manage the draw panel
  181. m_auimgr.AddPane( m_canvas,
  182. wxAuiPaneInfo().Name( wxT( "DrawFrame" ) ).Centre() );
  183. // Manage the message panel
  184. m_auimgr.AddPane( m_messagePanel,
  185. wxAuiPaneInfo( mesg ).Name( wxT( "MsgPanel" ) ).Bottom().Layer(10) );
  186. /* Now the minimum windows are fixed, set library list
  187. * and component list of the previous values from last viewlib use
  188. */
  189. if( m_LibListWindow )
  190. {
  191. wxAuiPaneInfo& pane = m_auimgr.GetPane(m_LibListWindow);
  192. pane.MinSize( wxSize(m_LibListSize.x, -1));
  193. }
  194. wxAuiPaneInfo& pane = m_auimgr.GetPane(m_CmpListWindow);
  195. pane.MinSize(wxSize(m_CmpListSize.x, -1));
  196. m_auimgr.Update();
  197. // Now Drawpanel is sized, we can use BestZoom to show the component (if any)
  198. #ifdef USE_WX_GRAPHICS_CONTEXT
  199. GetScreen()->SetZoom( BestZoom() );
  200. #else
  201. Zoom_Automatique( false );
  202. #endif
  203. Show( true );
  204. }
  205. LIB_VIEW_FRAME::~LIB_VIEW_FRAME()
  206. {
  207. }
  208. const wxChar* LIB_VIEW_FRAME::GetLibViewerFrameName()
  209. {
  210. return LIB_VIEW_FRAME_NAME;
  211. }
  212. /* return a reference to the current opened Library viewer
  213. * or NULL if no Library viewer currently opened
  214. */
  215. LIB_VIEW_FRAME* LIB_VIEW_FRAME::GetActiveLibraryViewer()
  216. {
  217. return (LIB_VIEW_FRAME*) wxWindow::FindWindowByName(GetLibViewerFrameName());
  218. }
  219. void LIB_VIEW_FRAME::OnCloseWindow( wxCloseEvent& Event )
  220. {
  221. SaveSettings();
  222. if( m_Semaphore )
  223. {
  224. m_Semaphore->Post();
  225. // This window will be destroyed by the calling function,
  226. // if needed
  227. SetModalMode( false );
  228. }
  229. else
  230. {
  231. Destroy();
  232. }
  233. }
  234. void LIB_VIEW_FRAME::OnSashDrag( wxSashEvent& event )
  235. {
  236. if( event.GetDragStatus() == wxSASH_STATUS_OUT_OF_RANGE )
  237. return;
  238. m_LibListSize.y = GetClientSize().y - m_MsgFrameHeight;
  239. m_CmpListSize.y = m_LibListSize.y;
  240. switch( event.GetId() )
  241. {
  242. case ID_LIBVIEW_LIBWINDOW:
  243. if( m_LibListWindow )
  244. {
  245. wxAuiPaneInfo& pane = m_auimgr.GetPane( m_LibListWindow );
  246. m_LibListSize.x = event.GetDragRect().width;
  247. pane.MinSize( m_LibListSize );
  248. m_auimgr.Update();
  249. }
  250. break;
  251. case ID_LIBVIEW_CMPWINDOW:
  252. {
  253. wxAuiPaneInfo& pane = m_auimgr.GetPane( m_CmpListWindow );
  254. m_CmpListSize.x = event.GetDragRect().width;
  255. pane.MinSize( m_CmpListSize );
  256. m_auimgr.Update();
  257. }
  258. break;
  259. }
  260. }
  261. void LIB_VIEW_FRAME::OnSize( wxSizeEvent& SizeEv )
  262. {
  263. if( m_auimgr.GetManagedWindow() )
  264. m_auimgr.Update();
  265. SizeEv.Skip();
  266. }
  267. void LIB_VIEW_FRAME::OnSetRelativeOffset( wxCommandEvent& event )
  268. {
  269. GetScreen()->m_O_Curseur = GetCrossHairPosition();
  270. UpdateStatusBar();
  271. }
  272. double LIB_VIEW_FRAME::BestZoom()
  273. {
  274. /* Please, note: wxMSW before version 2.9 seems have
  275. * problems with zoom values < 1 ( i.e. userscale > 1) and needs to be patched:
  276. * edit file <wxWidgets>/src/msw/dc.cpp
  277. * search for line static const int VIEWPORT_EXTENT = 1000;
  278. * and replace by static const int VIEWPORT_EXTENT = 10000;
  279. */
  280. LIB_COMPONENT* component = NULL;
  281. double bestzoom = 16.0; // default value for bestzoom
  282. CMP_LIBRARY* lib = CMP_LIBRARY::FindLibrary( m_libraryName );
  283. if( lib )
  284. component = lib->FindComponent( m_entryName );
  285. if( component == NULL )
  286. {
  287. SetScrollCenterPosition( wxPoint( 0, 0 ) );
  288. return bestzoom;
  289. }
  290. wxSize size = m_canvas->GetClientSize();
  291. EDA_RECT BoundaryBox = component->GetBoundingBox( m_unit, m_convert );
  292. // Reserve a 10% margin around component bounding box.
  293. double margin_scale_factor = 0.8;
  294. double zx =(double) BoundaryBox.GetWidth() /
  295. ( margin_scale_factor * (double)size.x );
  296. double zy = (double) BoundaryBox.GetHeight() /
  297. ( margin_scale_factor * (double)size.y);
  298. // Calculates the best zoom
  299. bestzoom = std::max( zx, zy );
  300. // keep it >= minimal existing zoom (can happen for very small components
  301. // like small power symbols
  302. if( bestzoom < GetScreen()->m_ZoomList[0] )
  303. bestzoom = GetScreen()->m_ZoomList[0];
  304. SetScrollCenterPosition( BoundaryBox.Centre() );
  305. return bestzoom;
  306. }
  307. void LIB_VIEW_FRAME::ReCreateListLib()
  308. {
  309. if( m_LibList == NULL )
  310. return;
  311. m_LibList->Clear();
  312. m_LibList->Append( CMP_LIBRARY::GetLibraryNames() );
  313. // Search for a previous selection:
  314. int index = m_LibList->FindString( m_libraryName );
  315. if( index != wxNOT_FOUND )
  316. {
  317. m_LibList->SetSelection( index, true );
  318. }
  319. else
  320. {
  321. /* If not found, clear current library selection because it can be
  322. * deleted after a config change. */
  323. m_libraryName = wxEmptyString;
  324. m_entryName = wxEmptyString;
  325. m_unit = 1;
  326. m_convert = 1;
  327. }
  328. ReCreateListCmp();
  329. ReCreateHToolbar();
  330. DisplayLibInfos();
  331. m_canvas->Refresh();
  332. }
  333. void LIB_VIEW_FRAME::ReCreateListCmp()
  334. {
  335. if( m_CmpList == NULL )
  336. return;
  337. m_CmpList->Clear();
  338. CMP_LIBRARY* Library = CMP_LIBRARY::FindLibrary( m_libraryName );
  339. if( Library == NULL )
  340. {
  341. m_libraryName = wxEmptyString;
  342. m_entryName = wxEmptyString;
  343. m_convert = 1;
  344. m_unit = 1;
  345. return;
  346. }
  347. wxArrayString nameList;
  348. Library->GetEntryNames( nameList );
  349. m_CmpList->Append( nameList );
  350. int index = m_CmpList->FindString( m_entryName );
  351. if( index == wxNOT_FOUND )
  352. {
  353. m_entryName = wxEmptyString;
  354. m_convert = 1;
  355. m_unit = 1;
  356. }
  357. else
  358. {
  359. m_CmpList->SetSelection( index, true );
  360. }
  361. }
  362. void LIB_VIEW_FRAME::ClickOnLibList( wxCommandEvent& event )
  363. {
  364. int ii = m_LibList->GetSelection();
  365. if( ii < 0 )
  366. return;
  367. wxString name = m_LibList->GetString( ii );
  368. if( m_libraryName == name )
  369. return;
  370. m_libraryName = name;
  371. ReCreateListCmp();
  372. m_canvas->Refresh();
  373. DisplayLibInfos();
  374. ReCreateHToolbar();
  375. }
  376. void LIB_VIEW_FRAME::ClickOnCmpList( wxCommandEvent& event )
  377. {
  378. int ii = m_CmpList->GetSelection();
  379. if( ii < 0 )
  380. return;
  381. wxString name = m_CmpList->GetString( ii );
  382. if( m_entryName.CmpNoCase( name ) != 0 )
  383. {
  384. m_entryName = name;
  385. DisplayLibInfos();
  386. m_unit = 1;
  387. m_convert = 1;
  388. Zoom_Automatique( false );
  389. ReCreateHToolbar();
  390. m_canvas->Refresh();
  391. }
  392. }
  393. void LIB_VIEW_FRAME::DClickOnCmpList( wxCommandEvent& event )
  394. {
  395. if( m_Semaphore )
  396. {
  397. ExportToSchematicLibraryPart( event );
  398. // Prevent the double click from being as a single click in the parent
  399. // window which would cause the part to be parked rather than staying
  400. // in drag mode.
  401. ((SCH_BASE_FRAME*) GetParent())->SkipNextLeftButtonReleaseEvent();
  402. }
  403. }
  404. void LIB_VIEW_FRAME::ExportToSchematicLibraryPart( wxCommandEvent& event )
  405. {
  406. int ii = m_CmpList->GetSelection();
  407. if( ii >= 0 )
  408. m_exportToEeschemaCmpName = m_CmpList->GetString( ii );
  409. else
  410. m_exportToEeschemaCmpName.Empty();
  411. Close( true );
  412. }
  413. #define LIBLIST_WIDTH_KEY wxT( "Liblist_width" )
  414. #define CMPLIST_WIDTH_KEY wxT( "Cmplist_width" )
  415. void LIB_VIEW_FRAME::LoadSettings( )
  416. {
  417. wxConfig* cfg ;
  418. EDA_DRAW_FRAME::LoadSettings();
  419. wxConfigPathChanger cpc( wxGetApp().GetSettings(), m_configPath );
  420. cfg = wxGetApp().GetSettings();
  421. m_LibListSize.x = 150; // default width of libs list
  422. m_CmpListSize.x = 150; // default width of component list
  423. cfg->Read( LIBLIST_WIDTH_KEY, &m_LibListSize.x );
  424. cfg->Read( CMPLIST_WIDTH_KEY, &m_CmpListSize.x );
  425. // Set parameters to a reasonable value.
  426. if ( m_LibListSize.x > m_FrameSize.x/2 )
  427. m_LibListSize.x = m_FrameSize.x/2;
  428. if ( m_CmpListSize.x > m_FrameSize.x/2 )
  429. m_CmpListSize.x = m_FrameSize.x/2;
  430. }
  431. void LIB_VIEW_FRAME::SaveSettings()
  432. {
  433. wxConfig* cfg;
  434. EDA_DRAW_FRAME::SaveSettings();
  435. wxConfigPathChanger cpc( wxGetApp().GetSettings(), m_configPath );
  436. cfg = wxGetApp().GetSettings();
  437. if ( m_LibListSize.x )
  438. cfg->Write( LIBLIST_WIDTH_KEY, m_LibListSize.x );
  439. cfg->Write( CMPLIST_WIDTH_KEY, m_CmpListSize.x );
  440. }
  441. void LIB_VIEW_FRAME::OnActivate( wxActivateEvent& event )
  442. {
  443. EDA_DRAW_FRAME::OnActivate( event );
  444. // Ensure we do not have old selection:
  445. if( m_FrameIsActive )
  446. m_exportToEeschemaCmpName.Empty();
  447. if( m_LibList )
  448. ReCreateListLib();
  449. DisplayLibInfos();
  450. }