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.

560 lines
15 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. LIB_VIEW_FRAME::LIB_VIEW_FRAME( wxWindow* father, CMP_LIBRARY* Library, wxSemaphore* semaphore ) :
  83. SCH_BASE_FRAME( father, VIEWER_FRAME, _( "Library Browser" ),
  84. wxDefaultPosition, wxDefaultSize )
  85. {
  86. wxAcceleratorTable table( ACCEL_TABLE_CNT, accels );
  87. m_FrameName = wxT( "ViewlibFrame" );
  88. m_configPath = wxT( "LibraryViewer" );
  89. // Give an icon
  90. wxIcon icon;
  91. icon.CopyFromBitmap( KiBitmap( library_browse_xpm ) );
  92. SetIcon( icon );
  93. m_HotkeysZoomAndGridList = s_Viewlib_Hokeys_Descr;
  94. m_CmpList = NULL;
  95. m_LibList = NULL;
  96. m_LibListWindow = NULL;
  97. m_CmpListWindow = NULL;
  98. m_Semaphore = semaphore;
  99. m_exportToEeschemaCmpName.Empty();
  100. if( m_Semaphore )
  101. MakeModal(true);
  102. SetScreen( new SCH_SCREEN() );
  103. GetScreen()->m_Center = true; // Center coordinate origins on screen.
  104. LoadSettings();
  105. SetSize( m_FramePos.x, m_FramePos.y, m_FrameSize.x, m_FrameSize.y );
  106. // Initialize grid id to the default value (50 mils):
  107. m_LastGridSizeId = ID_POPUP_GRID_LEVEL_50 - ID_POPUP_GRID_LEVEL_1000;
  108. GetScreen()->SetGrid( ID_POPUP_GRID_LEVEL_1000 + m_LastGridSizeId );
  109. ReCreateHToolbar();
  110. ReCreateVToolbar();
  111. wxSize size = GetClientSize();
  112. size.y -= m_MsgFrameHeight + 2;
  113. m_LibListSize.y = -1;
  114. wxPoint win_pos( 0, 0 );
  115. if( Library == NULL )
  116. {
  117. // Creates the libraries window display
  118. m_LibListWindow =
  119. new wxSashLayoutWindow( this, ID_LIBVIEW_LIBWINDOW, win_pos,
  120. wxDefaultSize, wxCLIP_CHILDREN | wxSW_3D,
  121. wxT( "LibWindow" ) );
  122. m_LibListWindow->SetOrientation( wxLAYOUT_VERTICAL );
  123. m_LibListWindow->SetAlignment( wxLAYOUT_LEFT );
  124. m_LibListWindow->SetSashVisible( wxSASH_RIGHT, true );
  125. m_LibListWindow->SetExtraBorderSize( EXTRA_BORDER_SIZE );
  126. m_LibList = new wxListBox( m_LibListWindow, ID_LIBVIEW_LIB_LIST,
  127. wxPoint( 0, 0 ), wxDefaultSize,
  128. 0, NULL, wxLB_HSCROLL );
  129. }
  130. else
  131. {
  132. m_libraryName = Library->GetName();
  133. m_entryName.Clear();
  134. m_unit = 1;
  135. m_convert = 1;
  136. m_LibListSize.x = 0;
  137. }
  138. // Creates the component window display
  139. m_CmpListSize.y = size.y;
  140. win_pos.x = m_LibListSize.x;
  141. m_CmpListWindow = new wxSashLayoutWindow( this, ID_LIBVIEW_CMPWINDOW,
  142. win_pos, wxDefaultSize,
  143. wxCLIP_CHILDREN | wxSW_3D,
  144. wxT( "CmpWindow" ) );
  145. m_CmpListWindow->SetOrientation( wxLAYOUT_VERTICAL );
  146. m_CmpListWindow->SetSashVisible( wxSASH_RIGHT, true );
  147. m_CmpListWindow->SetExtraBorderSize( EXTRA_BORDER_SIZE );
  148. m_CmpList = new wxListBox( m_CmpListWindow, ID_LIBVIEW_CMP_LIST,
  149. wxPoint( 0, 0 ), wxDefaultSize,
  150. 0, NULL, wxLB_HSCROLL );
  151. if( m_LibList )
  152. ReCreateListLib();
  153. DisplayLibInfos();
  154. if( m_canvas )
  155. m_canvas->SetAcceleratorTable( table );
  156. m_auimgr.SetManagedWindow( this );
  157. EDA_PANEINFO horiz;
  158. horiz.HorizontalToolbarPane();
  159. EDA_PANEINFO vert;
  160. vert.VerticalToolbarPane();
  161. EDA_PANEINFO info;
  162. info.InfoToolbarPane();
  163. EDA_PANEINFO mesg;
  164. mesg.MessageToolbarPane();
  165. // Manage main toolbal
  166. m_auimgr.AddPane( m_mainToolBar,
  167. wxAuiPaneInfo( horiz ).Name( wxT ("m_mainToolBar" ) ).Top().Row( 0 ) );
  168. wxSize minsize( 60, -1 );
  169. // Manage the left window (list of libraries)
  170. if( m_LibListWindow )
  171. m_auimgr.AddPane( m_LibListWindow, wxAuiPaneInfo( info ).Name( wxT( "m_LibList" ) ).
  172. Left().Row( 0 ));
  173. // Manage the list of components)
  174. m_auimgr.AddPane( m_CmpListWindow,
  175. wxAuiPaneInfo( info ).Name( wxT( "m_CmpList" ) ).
  176. Left().Row( 1 ) );
  177. // Manage the draw panel
  178. m_auimgr.AddPane( m_canvas,
  179. wxAuiPaneInfo().Name( wxT( "DrawFrame" ) ).Centre() );
  180. // Manage the message panel
  181. m_auimgr.AddPane( m_messagePanel,
  182. wxAuiPaneInfo( mesg ).Name( wxT( "MsgPanel" ) ).Bottom().Layer(10) );
  183. /* Now the minimum windows are fixed, set library list
  184. * and component list of the previous values from last viewlib use
  185. */
  186. if( m_LibListWindow )
  187. {
  188. wxAuiPaneInfo& pane = m_auimgr.GetPane(m_LibListWindow);
  189. pane.MinSize( wxSize(m_LibListSize.x, -1));
  190. }
  191. wxAuiPaneInfo& pane = m_auimgr.GetPane(m_CmpListWindow);
  192. pane.MinSize(wxSize(m_CmpListSize.x, -1));
  193. m_auimgr.Update();
  194. // Now Drawpanel is sized, we can use BestZoom to show the component (if any)
  195. #ifdef USE_WX_GRAPHICS_CONTEXT
  196. GetScreen()->SetZoom( BestZoom() );
  197. #else
  198. Zoom_Automatique( false );
  199. #endif
  200. Show( true );
  201. }
  202. LIB_VIEW_FRAME::~LIB_VIEW_FRAME()
  203. {
  204. SCH_BASE_FRAME* frame = (SCH_BASE_FRAME*) GetParent();
  205. frame->SetLibraryViewerWindow( NULL );
  206. }
  207. void LIB_VIEW_FRAME::OnCloseWindow( wxCloseEvent& Event )
  208. {
  209. SaveSettings();
  210. if( m_Semaphore )
  211. {
  212. m_Semaphore->Post();
  213. MakeModal(false);
  214. // This window will be destroyed by the calling function,
  215. // to avoid side effects
  216. }
  217. else
  218. {
  219. Destroy();
  220. }
  221. }
  222. void LIB_VIEW_FRAME::OnSashDrag( wxSashEvent& event )
  223. {
  224. if( event.GetDragStatus() == wxSASH_STATUS_OUT_OF_RANGE )
  225. return;
  226. m_LibListSize.y = GetClientSize().y - m_MsgFrameHeight;
  227. m_CmpListSize.y = m_LibListSize.y;
  228. switch( event.GetId() )
  229. {
  230. case ID_LIBVIEW_LIBWINDOW:
  231. if( m_LibListWindow )
  232. {
  233. wxAuiPaneInfo& pane = m_auimgr.GetPane( m_LibListWindow );
  234. m_LibListSize.x = event.GetDragRect().width;
  235. pane.MinSize( m_LibListSize );
  236. m_auimgr.Update();
  237. }
  238. break;
  239. case ID_LIBVIEW_CMPWINDOW:
  240. {
  241. wxAuiPaneInfo& pane = m_auimgr.GetPane( m_CmpListWindow );
  242. m_CmpListSize.x = event.GetDragRect().width;
  243. pane.MinSize( m_CmpListSize );
  244. m_auimgr.Update();
  245. }
  246. break;
  247. }
  248. }
  249. void LIB_VIEW_FRAME::OnSize( wxSizeEvent& SizeEv )
  250. {
  251. if( m_auimgr.GetManagedWindow() )
  252. m_auimgr.Update();
  253. SizeEv.Skip();
  254. }
  255. void LIB_VIEW_FRAME::OnSetRelativeOffset( wxCommandEvent& event )
  256. {
  257. GetScreen()->m_O_Curseur = GetScreen()->GetCrossHairPosition();
  258. UpdateStatusBar();
  259. }
  260. double LIB_VIEW_FRAME::BestZoom()
  261. {
  262. /* Please, note: wxMSW before version 2.9 seems have
  263. * problems with zoom values < 1 ( i.e. userscale > 1) and needs to be patched:
  264. * edit file <wxWidgets>/src/msw/dc.cpp
  265. * search for line static const int VIEWPORT_EXTENT = 1000;
  266. * and replace by static const int VIEWPORT_EXTENT = 10000;
  267. */
  268. LIB_COMPONENT* component = NULL;
  269. CMP_LIBRARY* lib;
  270. double bestzoom = 16.0; // default value for bestzoom
  271. lib = CMP_LIBRARY::FindLibrary( m_libraryName );
  272. if( lib )
  273. component = lib->FindComponent( m_entryName );
  274. if( component == NULL )
  275. {
  276. GetScreen()->SetScrollCenterPosition( wxPoint( 0, 0 ) );
  277. return bestzoom;
  278. }
  279. wxSize size = m_canvas->GetClientSize();
  280. EDA_RECT BoundaryBox = component->GetBoundingBox( m_unit, m_convert );
  281. // Reserve a 10% margin around component bounding box.
  282. double margin_scale_factor = 0.8;
  283. double zx =(double) BoundaryBox.GetWidth() /
  284. ( margin_scale_factor * (double)size.x );
  285. double zy = (double) BoundaryBox.GetHeight() /
  286. ( margin_scale_factor * (double)size.y);
  287. // Calculates the best zoom
  288. bestzoom = MAX( zx, zy );
  289. // keep it >= minimal existing zoom (can happen for very small components
  290. // like small power symbols
  291. if( bestzoom < GetScreen()->m_ZoomList[0] )
  292. bestzoom = GetScreen()->m_ZoomList[0];
  293. GetScreen()->SetScrollCenterPosition( BoundaryBox.Centre() );
  294. return bestzoom;
  295. }
  296. void LIB_VIEW_FRAME::ReCreateListLib()
  297. {
  298. if( m_LibList == NULL )
  299. return;
  300. m_LibList->Clear();
  301. m_LibList->Append( CMP_LIBRARY::GetLibraryNames() );
  302. // Search for a previous selection:
  303. int index = m_LibList->FindString( m_libraryName );
  304. if( index != wxNOT_FOUND )
  305. {
  306. m_LibList->SetSelection( index, true );
  307. }
  308. else
  309. {
  310. /* If not found, clear current library selection because it can be
  311. * deleted after a config change. */
  312. m_libraryName = wxEmptyString;
  313. m_entryName = wxEmptyString;
  314. m_unit = 1;
  315. m_convert = 1;
  316. }
  317. ReCreateListCmp();
  318. ReCreateHToolbar();
  319. DisplayLibInfos();
  320. m_canvas->Refresh();
  321. }
  322. void LIB_VIEW_FRAME::ReCreateListCmp()
  323. {
  324. if( m_CmpList == NULL )
  325. return;
  326. m_CmpList->Clear();
  327. CMP_LIBRARY* Library = CMP_LIBRARY::FindLibrary( m_libraryName );
  328. if( Library == NULL )
  329. {
  330. m_libraryName = wxEmptyString;
  331. m_entryName = wxEmptyString;
  332. m_convert = 1;
  333. m_unit = 1;
  334. return;
  335. }
  336. wxArrayString nameList;
  337. Library->GetEntryNames( nameList );
  338. m_CmpList->Append( nameList );
  339. int index = m_CmpList->FindString( m_entryName );
  340. if( index == wxNOT_FOUND )
  341. {
  342. m_entryName = wxEmptyString;
  343. m_convert = 1;
  344. m_unit = 1;
  345. }
  346. else
  347. {
  348. m_CmpList->SetSelection( index, true );
  349. }
  350. }
  351. void LIB_VIEW_FRAME::ClickOnLibList( wxCommandEvent& event )
  352. {
  353. int ii = m_LibList->GetSelection();
  354. if( ii < 0 )
  355. return;
  356. wxString name = m_LibList->GetString( ii );
  357. if( m_libraryName == name )
  358. return;
  359. m_libraryName = name;
  360. ReCreateListCmp();
  361. m_canvas->Refresh();
  362. DisplayLibInfos();
  363. ReCreateHToolbar();
  364. }
  365. void LIB_VIEW_FRAME::ClickOnCmpList( wxCommandEvent& event )
  366. {
  367. int ii = m_CmpList->GetSelection();
  368. if( ii < 0 )
  369. return;
  370. wxString name = m_CmpList->GetString( ii );
  371. if( m_entryName.CmpNoCase( name ) != 0 )
  372. {
  373. m_entryName = name;
  374. DisplayLibInfos();
  375. m_unit = 1;
  376. m_convert = 1;
  377. Zoom_Automatique( false );
  378. ReCreateHToolbar();
  379. m_canvas->Refresh();
  380. }
  381. }
  382. void LIB_VIEW_FRAME::ExportToSchematicLibraryPart( wxCommandEvent& event )
  383. {
  384. int ii = m_CmpList->GetSelection();
  385. if( ii >= 0 )
  386. m_exportToEeschemaCmpName = m_CmpList->GetString( ii );
  387. else
  388. m_exportToEeschemaCmpName.Empty();
  389. Close( true );
  390. }
  391. #define LIBLIST_WIDTH_KEY wxT( "Liblist_width" )
  392. #define CMPLIST_WIDTH_KEY wxT( "Cmplist_width" )
  393. void LIB_VIEW_FRAME::LoadSettings( )
  394. {
  395. wxConfig* cfg ;
  396. EDA_DRAW_FRAME::LoadSettings();
  397. wxConfigPathChanger cpc( wxGetApp().GetSettings(), m_configPath );
  398. cfg = wxGetApp().GetSettings();
  399. m_LibListSize.x = 150; // default width of libs list
  400. m_CmpListSize.x = 150; // default width of component list
  401. cfg->Read( LIBLIST_WIDTH_KEY, &m_LibListSize.x );
  402. cfg->Read( CMPLIST_WIDTH_KEY, &m_CmpListSize.x );
  403. // Set parameters to a reasonable value.
  404. if ( m_LibListSize.x > m_FrameSize.x/2 )
  405. m_LibListSize.x = m_FrameSize.x/2;
  406. if ( m_CmpListSize.x > m_FrameSize.x/2 )
  407. m_CmpListSize.x = m_FrameSize.x/2;
  408. }
  409. void LIB_VIEW_FRAME::SaveSettings()
  410. {
  411. wxConfig* cfg;
  412. EDA_DRAW_FRAME::SaveSettings();
  413. wxConfigPathChanger cpc( wxGetApp().GetSettings(), m_configPath );
  414. cfg = wxGetApp().GetSettings();
  415. if ( m_LibListSize.x )
  416. cfg->Write( LIBLIST_WIDTH_KEY, m_LibListSize.x );
  417. cfg->Write( CMPLIST_WIDTH_KEY, m_CmpListSize.x );
  418. }
  419. void LIB_VIEW_FRAME::OnActivate( wxActivateEvent& event )
  420. {
  421. EDA_DRAW_FRAME::OnActivate( event );
  422. // Ensure we do not have old selection:
  423. if( m_FrameIsActive )
  424. m_exportToEeschemaCmpName.Empty();
  425. if( m_LibList )
  426. ReCreateListLib();
  427. DisplayLibInfos();
  428. }