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.

570 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 <general.h>
  34. //#include <protos.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_MENU( ID_SET_RELATIVE_OFFSET, LIB_VIEW_FRAME::OnSetRelativeOffset )
  65. END_EVENT_TABLE()
  66. /*
  67. * This emulates the zoom menu entries found in the other KiCad applications.
  68. * The library viewer does not have any menus so add an accelerator table to
  69. * the main frame.
  70. */
  71. static wxAcceleratorEntry accels[] =
  72. {
  73. wxAcceleratorEntry( wxACCEL_NORMAL, WXK_F1, ID_ZOOM_IN ),
  74. wxAcceleratorEntry( wxACCEL_NORMAL, WXK_F2, ID_ZOOM_OUT ),
  75. wxAcceleratorEntry( wxACCEL_NORMAL, WXK_F3, ID_ZOOM_REDRAW ),
  76. wxAcceleratorEntry( wxACCEL_NORMAL, WXK_F4, ID_POPUP_ZOOM_CENTER ),
  77. wxAcceleratorEntry( wxACCEL_NORMAL, WXK_HOME, ID_ZOOM_PAGE ),
  78. wxAcceleratorEntry( wxACCEL_NORMAL, WXK_SPACE, ID_SET_RELATIVE_OFFSET )
  79. };
  80. #define ACCEL_TABLE_CNT ( sizeof( accels ) / sizeof( wxAcceleratorEntry ) )
  81. #define EXTRA_BORDER_SIZE 2
  82. #define LIB_VIEW_FRAME_NAME wxT( "ViewlibFrame" )
  83. LIB_VIEW_FRAME::LIB_VIEW_FRAME( wxWindow* father, CMP_LIBRARY* Library,
  84. wxSemaphore* semaphore, long style ) :
  85. SCH_BASE_FRAME( father, VIEWER_FRAME_TYPE, _( "Library Browser" ),
  86. wxDefaultPosition, wxDefaultSize, style, GetLibViewerFrameName() )
  87. {
  88. wxAcceleratorTable table( ACCEL_TABLE_CNT, accels );
  89. m_FrameName = GetLibViewerFrameName();
  90. m_configPath = wxT( "LibraryViewer" );
  91. // Give an icon
  92. wxIcon icon;
  93. icon.CopyFromBitmap( KiBitmap( library_browse_xpm ) );
  94. SetIcon( icon );
  95. m_HotkeysZoomAndGridList = s_Viewlib_Hokeys_Descr;
  96. m_CmpList = NULL;
  97. m_LibList = NULL;
  98. m_LibListWindow = NULL;
  99. m_CmpListWindow = NULL;
  100. m_Semaphore = semaphore;
  101. if( m_Semaphore )
  102. SetModalMode( true );
  103. m_exportToEeschemaCmpName.Empty();
  104. SetScreen( new SCH_SCREEN() );
  105. GetScreen()->m_Center = true; // Axis origin centered on screen.
  106. LoadSettings();
  107. SetSize( m_FramePos.x, m_FramePos.y, m_FrameSize.x, m_FrameSize.y );
  108. // Initialize grid id to the default value (50 mils):
  109. m_LastGridSizeId = ID_POPUP_GRID_LEVEL_50 - ID_POPUP_GRID_LEVEL_1000;
  110. GetScreen()->SetGrid( ID_POPUP_GRID_LEVEL_1000 + m_LastGridSizeId );
  111. ReCreateHToolbar();
  112. ReCreateVToolbar();
  113. wxSize size = GetClientSize();
  114. size.y -= m_MsgFrameHeight + 2;
  115. m_LibListSize.y = -1;
  116. wxPoint win_pos( 0, 0 );
  117. if( Library == NULL )
  118. {
  119. // Creates the libraries window display
  120. m_LibListWindow =
  121. new wxSashLayoutWindow( this, ID_LIBVIEW_LIBWINDOW, win_pos,
  122. wxDefaultSize, wxCLIP_CHILDREN | wxSW_3D,
  123. wxT( "LibWindow" ) );
  124. m_LibListWindow->SetOrientation( wxLAYOUT_VERTICAL );
  125. m_LibListWindow->SetAlignment( wxLAYOUT_LEFT );
  126. m_LibListWindow->SetSashVisible( wxSASH_RIGHT, true );
  127. m_LibListWindow->SetExtraBorderSize( EXTRA_BORDER_SIZE );
  128. m_LibList = new wxListBox( m_LibListWindow, ID_LIBVIEW_LIB_LIST,
  129. wxPoint( 0, 0 ), wxDefaultSize,
  130. 0, NULL, wxLB_HSCROLL );
  131. }
  132. else
  133. {
  134. m_libraryName = Library->GetName();
  135. m_entryName.Clear();
  136. m_unit = 1;
  137. m_convert = 1;
  138. m_LibListSize.x = 0;
  139. }
  140. // Creates the component window display
  141. m_CmpListSize.y = size.y;
  142. win_pos.x = m_LibListSize.x;
  143. m_CmpListWindow = new wxSashLayoutWindow( this, ID_LIBVIEW_CMPWINDOW,
  144. win_pos, wxDefaultSize,
  145. wxCLIP_CHILDREN | wxSW_3D,
  146. wxT( "CmpWindow" ) );
  147. m_CmpListWindow->SetOrientation( wxLAYOUT_VERTICAL );
  148. m_CmpListWindow->SetSashVisible( wxSASH_RIGHT, true );
  149. m_CmpListWindow->SetExtraBorderSize( EXTRA_BORDER_SIZE );
  150. m_CmpList = new wxListBox( m_CmpListWindow, ID_LIBVIEW_CMP_LIST,
  151. wxPoint( 0, 0 ), wxDefaultSize,
  152. 0, NULL, wxLB_HSCROLL );
  153. if( m_LibList )
  154. ReCreateListLib();
  155. DisplayLibInfos();
  156. if( m_canvas )
  157. m_canvas->SetAcceleratorTable( table );
  158. m_auimgr.SetManagedWindow( this );
  159. EDA_PANEINFO horiz;
  160. horiz.HorizontalToolbarPane();
  161. EDA_PANEINFO vert;
  162. vert.VerticalToolbarPane();
  163. EDA_PANEINFO info;
  164. info.InfoToolbarPane();
  165. EDA_PANEINFO mesg;
  166. mesg.MessageToolbarPane();
  167. // Manage main toolbal
  168. m_auimgr.AddPane( m_mainToolBar,
  169. wxAuiPaneInfo( horiz ).Name( wxT ("m_mainToolBar" ) ).Top().Row( 0 ) );
  170. wxSize minsize( 60, -1 );
  171. // Manage the left window (list of libraries)
  172. if( m_LibListWindow )
  173. m_auimgr.AddPane( m_LibListWindow, wxAuiPaneInfo( info ).Name( wxT( "m_LibList" ) ).
  174. Left().Row( 0 ));
  175. // Manage the list of components)
  176. m_auimgr.AddPane( m_CmpListWindow,
  177. wxAuiPaneInfo( info ).Name( wxT( "m_CmpList" ) ).
  178. Left().Row( 1 ) );
  179. // Manage the draw panel
  180. m_auimgr.AddPane( m_canvas,
  181. wxAuiPaneInfo().Name( wxT( "DrawFrame" ) ).Centre() );
  182. // Manage the message panel
  183. m_auimgr.AddPane( m_messagePanel,
  184. wxAuiPaneInfo( mesg ).Name( wxT( "MsgPanel" ) ).Bottom().Layer(10) );
  185. /* Now the minimum windows are fixed, set library list
  186. * and component list of the previous values from last viewlib use
  187. */
  188. if( m_LibListWindow )
  189. {
  190. wxAuiPaneInfo& pane = m_auimgr.GetPane(m_LibListWindow);
  191. pane.MinSize( wxSize(m_LibListSize.x, -1));
  192. }
  193. wxAuiPaneInfo& pane = m_auimgr.GetPane(m_CmpListWindow);
  194. pane.MinSize(wxSize(m_CmpListSize.x, -1));
  195. m_auimgr.Update();
  196. // Now Drawpanel is sized, we can use BestZoom to show the component (if any)
  197. #ifdef USE_WX_GRAPHICS_CONTEXT
  198. GetScreen()->SetZoom( BestZoom() );
  199. #else
  200. Zoom_Automatique( false );
  201. #endif
  202. Show( true );
  203. }
  204. LIB_VIEW_FRAME::~LIB_VIEW_FRAME()
  205. {
  206. }
  207. const wxChar* LIB_VIEW_FRAME::GetLibViewerFrameName()
  208. {
  209. return LIB_VIEW_FRAME_NAME;
  210. }
  211. /* return a reference to the current opened Library viewer
  212. * or NULL if no Library viewer currently opened
  213. */
  214. LIB_VIEW_FRAME* LIB_VIEW_FRAME::GetActiveLibraryViewer()
  215. {
  216. return (LIB_VIEW_FRAME*) wxWindow::FindWindowByName(GetLibViewerFrameName());
  217. }
  218. void LIB_VIEW_FRAME::OnCloseWindow( wxCloseEvent& Event )
  219. {
  220. SaveSettings();
  221. if( m_Semaphore )
  222. {
  223. m_Semaphore->Post();
  224. // This window will be destroyed by the calling function,
  225. // if needed
  226. SetModalMode( false );
  227. }
  228. else
  229. {
  230. Destroy();
  231. }
  232. }
  233. void LIB_VIEW_FRAME::OnSashDrag( wxSashEvent& event )
  234. {
  235. if( event.GetDragStatus() == wxSASH_STATUS_OUT_OF_RANGE )
  236. return;
  237. m_LibListSize.y = GetClientSize().y - m_MsgFrameHeight;
  238. m_CmpListSize.y = m_LibListSize.y;
  239. switch( event.GetId() )
  240. {
  241. case ID_LIBVIEW_LIBWINDOW:
  242. if( m_LibListWindow )
  243. {
  244. wxAuiPaneInfo& pane = m_auimgr.GetPane( m_LibListWindow );
  245. m_LibListSize.x = event.GetDragRect().width;
  246. pane.MinSize( m_LibListSize );
  247. m_auimgr.Update();
  248. }
  249. break;
  250. case ID_LIBVIEW_CMPWINDOW:
  251. {
  252. wxAuiPaneInfo& pane = m_auimgr.GetPane( m_CmpListWindow );
  253. m_CmpListSize.x = event.GetDragRect().width;
  254. pane.MinSize( m_CmpListSize );
  255. m_auimgr.Update();
  256. }
  257. break;
  258. }
  259. }
  260. void LIB_VIEW_FRAME::OnSize( wxSizeEvent& SizeEv )
  261. {
  262. if( m_auimgr.GetManagedWindow() )
  263. m_auimgr.Update();
  264. SizeEv.Skip();
  265. }
  266. void LIB_VIEW_FRAME::OnSetRelativeOffset( wxCommandEvent& event )
  267. {
  268. GetScreen()->m_O_Curseur = GetScreen()->GetCrossHairPosition();
  269. UpdateStatusBar();
  270. }
  271. double LIB_VIEW_FRAME::BestZoom()
  272. {
  273. /* Please, note: wxMSW before version 2.9 seems have
  274. * problems with zoom values < 1 ( i.e. userscale > 1) and needs to be patched:
  275. * edit file <wxWidgets>/src/msw/dc.cpp
  276. * search for line static const int VIEWPORT_EXTENT = 1000;
  277. * and replace by static const int VIEWPORT_EXTENT = 10000;
  278. */
  279. LIB_COMPONENT* component = NULL;
  280. CMP_LIBRARY* lib;
  281. double bestzoom = 16.0; // default value for bestzoom
  282. lib = CMP_LIBRARY::FindLibrary( m_libraryName );
  283. if( lib )
  284. component = lib->FindComponent( m_entryName );
  285. if( component == NULL )
  286. {
  287. GetScreen()->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. GetScreen()->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::ExportToSchematicLibraryPart( wxCommandEvent& event )
  394. {
  395. int ii = m_CmpList->GetSelection();
  396. if( ii >= 0 )
  397. m_exportToEeschemaCmpName = m_CmpList->GetString( ii );
  398. else
  399. m_exportToEeschemaCmpName.Empty();
  400. Close( true );
  401. }
  402. #define LIBLIST_WIDTH_KEY wxT( "Liblist_width" )
  403. #define CMPLIST_WIDTH_KEY wxT( "Cmplist_width" )
  404. void LIB_VIEW_FRAME::LoadSettings( )
  405. {
  406. wxConfig* cfg ;
  407. EDA_DRAW_FRAME::LoadSettings();
  408. wxConfigPathChanger cpc( wxGetApp().GetSettings(), m_configPath );
  409. cfg = wxGetApp().GetSettings();
  410. m_LibListSize.x = 150; // default width of libs list
  411. m_CmpListSize.x = 150; // default width of component list
  412. cfg->Read( LIBLIST_WIDTH_KEY, &m_LibListSize.x );
  413. cfg->Read( CMPLIST_WIDTH_KEY, &m_CmpListSize.x );
  414. // Set parameters to a reasonable value.
  415. if ( m_LibListSize.x > m_FrameSize.x/2 )
  416. m_LibListSize.x = m_FrameSize.x/2;
  417. if ( m_CmpListSize.x > m_FrameSize.x/2 )
  418. m_CmpListSize.x = m_FrameSize.x/2;
  419. }
  420. void LIB_VIEW_FRAME::SaveSettings()
  421. {
  422. wxConfig* cfg;
  423. EDA_DRAW_FRAME::SaveSettings();
  424. wxConfigPathChanger cpc( wxGetApp().GetSettings(), m_configPath );
  425. cfg = wxGetApp().GetSettings();
  426. if ( m_LibListSize.x )
  427. cfg->Write( LIBLIST_WIDTH_KEY, m_LibListSize.x );
  428. cfg->Write( CMPLIST_WIDTH_KEY, m_CmpListSize.x );
  429. }
  430. void LIB_VIEW_FRAME::OnActivate( wxActivateEvent& event )
  431. {
  432. EDA_DRAW_FRAME::OnActivate( event );
  433. // Ensure we do not have old selection:
  434. if( m_FrameIsActive )
  435. m_exportToEeschemaCmpName.Empty();
  436. if( m_LibList )
  437. ReCreateListLib();
  438. DisplayLibInfos();
  439. }