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.

326 lines
9.5 KiB

  1. /*************************************************************************/
  2. /* viewlib_frame.cpp - fonctions des classes du type WinEDA_ViewlibFrame */
  3. /*************************************************************************/
  4. #include "fctsys.h"
  5. #include "common.h"
  6. #include "program.h"
  7. #include "libcmp.h"
  8. #include "general.h"
  9. #include "bitmaps.h"
  10. #include "protos.h"
  11. #include "id.h"
  12. /*****************************/
  13. /* class WinEDA_ViewlibFrame */
  14. /*****************************/
  15. BEGIN_EVENT_TABLE( WinEDA_ViewlibFrame, wxFrame )
  16. COMMON_EVENTS_DRAWFRAME EVT_CLOSE( WinEDA_ViewlibFrame::OnCloseWindow )
  17. EVT_SIZE( WinEDA_ViewlibFrame::OnSize )
  18. EVT_ACTIVATE( WinEDA_DrawFrame::OnActivate )
  19. EVT_TOOL_RANGE( ID_LIBVIEW_START_H_TOOL, ID_LIBVIEW_END_H_TOOL,
  20. WinEDA_ViewlibFrame::Process_Special_Functions )
  21. EVT_TOOL_RANGE( ID_ZOOM_IN_BUTT, ID_ZOOM_PAGE_BUTT,
  22. WinEDA_DrawFrame::Process_Zoom )
  23. EVT_TOOL( ID_LIBVIEW_CMP_EXPORT_TO_SCHEMATIC,
  24. WinEDA_ViewlibFrame::ExportToSchematicLibraryPart )
  25. EVT_KICAD_CHOICEBOX( ID_LIBVIEW_SELECT_PART_NUMBER,
  26. WinEDA_ViewlibFrame::Process_Special_Functions )
  27. EVT_LISTBOX( ID_LIBVIEW_LIB_LIST, WinEDA_ViewlibFrame::ClickOnLibList )
  28. EVT_LISTBOX( ID_LIBVIEW_CMP_LIST, WinEDA_ViewlibFrame::ClickOnCmpList )
  29. END_EVENT_TABLE()
  30. /******************************************************************************/
  31. WinEDA_ViewlibFrame::WinEDA_ViewlibFrame( wxWindow* father,
  32. LibraryStruct* Library,
  33. wxSemaphore* semaphore ) :
  34. WinEDA_DrawFrame( father, VIEWER_FRAME, _( "Library browser" ),
  35. wxDefaultPosition, wxDefaultSize )
  36. /******************************************************************************/
  37. {
  38. m_FrameName = wxT( "ViewlibFrame" );
  39. m_Draw_Axis = TRUE; // TRUE to dispaly Axis
  40. m_Draw_Grid = TRUE; // TRUE to display grid
  41. // Give an icon
  42. SetIcon( wxIcon( library_browse_xpm ) );
  43. m_CmpList = NULL;
  44. m_LibList = NULL;
  45. m_Semaphore = semaphore;
  46. if( m_Semaphore )
  47. SetWindowStyle( GetWindowStyle() | wxSTAY_ON_TOP );
  48. SetBaseScreen( new SCH_SCREEN() );
  49. GetScreen()->m_Center = true; // set to true to have the coordinates origine -0,0) centered on screen
  50. if( Library == NULL )
  51. {
  52. m_LibListSize.x = 150; // Width of library list
  53. m_LibListSize.y = -1;
  54. m_LibList = new wxListBox( this, ID_LIBVIEW_LIB_LIST, wxPoint( 0, 0 ),
  55. m_LibListSize, 0, NULL, wxLB_HSCROLL );
  56. m_LibList->SetFont( *g_DialogFont );
  57. m_LibList->SetBackgroundColour( wxColour( 255, 255, 255 ) ); // Library background listbox color (white)
  58. m_LibList->SetForegroundColour( wxColour( 0, 0, 0 ) ); // Library foreground listbox color (black)
  59. }
  60. else
  61. g_CurrentViewLibraryName = Library->m_Name;
  62. m_CmpListSize.x = 150; // Width of component list
  63. m_CmpListSize.y = -1;
  64. m_CmpList = new wxListBox( this, ID_LIBVIEW_CMP_LIST, wxPoint( m_LibListSize.x, 0 ),
  65. m_CmpListSize, 0, NULL, wxLB_HSCROLL );
  66. m_CmpList->SetFont( *g_DialogFont );
  67. m_CmpList->SetBackgroundColour( wxColour( 255, 255, 255 ) ); // Component background listbox color (white)
  68. m_CmpList->SetForegroundColour( wxColour( 0, 0, 0 ) ); // Component foreground listbox color (black)
  69. GetSettings();
  70. SetSize( m_FramePos.x, m_FramePos.y, m_FrameSize.x, m_FrameSize.y );
  71. ReCreateHToolbar();
  72. ReCreateVToolbar();
  73. if( m_LibList )
  74. ReCreateListLib();
  75. DisplayLibInfos();
  76. BestZoom();
  77. Show( TRUE );
  78. }
  79. /*******************************************/
  80. WinEDA_ViewlibFrame::~WinEDA_ViewlibFrame()
  81. /*******************************************/
  82. {
  83. delete GetScreen();
  84. SetBaseScreen( 0 );
  85. WinEDA_SchematicFrame* frame =
  86. (WinEDA_SchematicFrame*) wxGetApp().GetTopWindow();
  87. frame->m_ViewlibFrame = NULL;
  88. }
  89. /*****************************************************************/
  90. void WinEDA_ViewlibFrame::OnCloseWindow( wxCloseEvent& Event )
  91. /*****************************************************************/
  92. {
  93. SaveSettings();
  94. if( m_Semaphore )
  95. m_Semaphore->Post();
  96. Destroy();
  97. }
  98. /*****************************************************/
  99. void WinEDA_ViewlibFrame::OnSize( wxSizeEvent& SizeEv )
  100. /*****************************************************/
  101. {
  102. wxSize size;
  103. wxSize maintoolbar_size;
  104. wxSize Vtoolbar_size;
  105. GetClientSize( &size.x, &size.y );
  106. m_FrameSize = size;
  107. size.y -= m_MsgFrameHeight;
  108. if( m_HToolBar )
  109. {
  110. maintoolbar_size = m_HToolBar->GetSize();
  111. }
  112. if( m_VToolBar )
  113. {
  114. Vtoolbar_size = m_VToolBar->GetSize();
  115. m_VToolBar->SetSize( size.x - maintoolbar_size.y, 0, -1, size.y );
  116. }
  117. if( MsgPanel )
  118. {
  119. MsgPanel->SetSize( 0, size.y, size.x, m_MsgFrameHeight );
  120. }
  121. if( DrawPanel )
  122. {
  123. DrawPanel->SetSize( m_LibListSize.x + m_CmpListSize.x, 0,
  124. size.x - Vtoolbar_size.x - m_LibListSize.x - m_CmpListSize.x,
  125. size.y );
  126. }
  127. if( m_LibList )
  128. {
  129. m_LibListSize.y = size.y;
  130. m_LibList->SetSize( 0, 0, m_LibListSize.x, m_LibListSize.y );
  131. }
  132. if( m_CmpList )
  133. {
  134. m_CmpListSize.y = size.y;
  135. m_CmpList->SetSize( m_LibListSize.x, 0, m_CmpListSize.x, m_CmpListSize.y );
  136. }
  137. }
  138. /***********************************/
  139. int WinEDA_ViewlibFrame::BestZoom()
  140. /***********************************/
  141. {
  142. int bestzoom, ii, jj;
  143. wxSize size, itemsize;
  144. EDA_LibComponentStruct* CurrentLibEntry = NULL;
  145. CurrentLibEntry = FindLibPart( g_CurrentViewComponentName.GetData(),
  146. g_CurrentViewLibraryName.GetData(), FIND_ROOT );
  147. if( CurrentLibEntry == NULL )
  148. {
  149. bestzoom = 16;
  150. GetScreen()->m_Curseur.x = 0;
  151. GetScreen()->m_Curseur.y = 0;
  152. return bestzoom;
  153. }
  154. EDA_Rect BoundaryBox = CurrentLibEntry->GetBoundaryBox( g_ViewUnit, g_ViewConvert );
  155. itemsize = BoundaryBox.GetSize();
  156. size = DrawPanel->GetClientSize();
  157. size -= wxSize( 100, 100 ); // reserve a 100 mils margin
  158. ii = (double) itemsize.x / size.x;
  159. jj = itemsize.y / size.y;
  160. bestzoom = MAX( ii, jj ) + 1;
  161. GetScreen()->m_Curseur = BoundaryBox.Centre();
  162. return bestzoom;
  163. }
  164. /******************************************/
  165. void WinEDA_ViewlibFrame::ReCreateListLib()
  166. /******************************************/
  167. {
  168. const wxChar** ListNames, ** names;
  169. int ii;
  170. bool found = FALSE;
  171. if( m_LibList == NULL )
  172. return;
  173. ListNames = GetLibNames();
  174. m_LibList->Clear();
  175. for( names = ListNames, ii = 0; *names != NULL; names++, ii++ )
  176. {
  177. m_LibList->Append( *names );
  178. if( g_CurrentViewLibraryName.Cmp( *names ) == 0 )
  179. {
  180. m_LibList->SetSelection( ii, TRUE );
  181. found = TRUE;
  182. }
  183. }
  184. free( ListNames );
  185. /* Clear current library because it can be deleted after a config change
  186. */
  187. if( !found )
  188. {
  189. g_CurrentViewLibraryName.Empty();
  190. g_CurrentViewComponentName.Empty();
  191. }
  192. ReCreateListCmp();
  193. ReCreateHToolbar();
  194. DisplayLibInfos();
  195. ReDrawPanel();
  196. }
  197. /***********************************************/
  198. void WinEDA_ViewlibFrame::ReCreateListCmp()
  199. /***********************************************/
  200. {
  201. int ii;
  202. EDA_LibComponentStruct* LibEntry = NULL;
  203. LibraryStruct* Library = FindLibrary( g_CurrentViewLibraryName.GetData() );
  204. m_CmpList->Clear();
  205. ii = 0;
  206. g_CurrentViewComponentName.Empty();
  207. g_ViewConvert = 1; /* Select normal/"de morgan" shape */
  208. g_ViewUnit = 1; /* Selec unit to display for multiple parts per package */
  209. if( Library )
  210. LibEntry = (EDA_LibComponentStruct*) PQFirst( &Library->m_Entries, FALSE );
  211. while( LibEntry )
  212. {
  213. m_CmpList->Append( LibEntry->m_Name.m_Text );
  214. LibEntry = (EDA_LibComponentStruct*) PQNext( Library->m_Entries, LibEntry, NULL );
  215. }
  216. }
  217. /********************************************************************/
  218. void WinEDA_ViewlibFrame::ClickOnLibList( wxCommandEvent& event )
  219. /********************************************************************/
  220. {
  221. int ii = m_LibList->GetSelection();
  222. if( ii < 0 )
  223. return;
  224. wxString name = m_LibList->GetString( ii );
  225. if( g_CurrentViewLibraryName == name )
  226. return;
  227. g_CurrentViewLibraryName = name;
  228. ReCreateListCmp();
  229. ReDrawPanel();
  230. DisplayLibInfos();
  231. ReCreateHToolbar();
  232. }
  233. /****************************************************************/
  234. void WinEDA_ViewlibFrame::ClickOnCmpList( wxCommandEvent& event )
  235. /****************************************************************/
  236. {
  237. int ii = m_CmpList->GetSelection();
  238. if( ii < 0 )
  239. return;
  240. wxString name = m_CmpList->GetString( ii );
  241. g_CurrentViewComponentName = name;
  242. DisplayLibInfos();
  243. g_ViewUnit = 1;
  244. g_ViewConvert = 1;
  245. Zoom_Automatique( FALSE );
  246. ReCreateHToolbar();
  247. ReDrawPanel();
  248. }
  249. /****************************************************************************/
  250. void WinEDA_ViewlibFrame::ExportToSchematicLibraryPart( wxCommandEvent& event )
  251. /****************************************************************************/
  252. /* Export the current component to schematic and close the library browser
  253. */
  254. {
  255. int ii = m_CmpList->GetSelection();
  256. if( ii >= 0 )
  257. g_CurrentViewComponentName = m_CmpList->GetString( ii );
  258. else
  259. g_CurrentViewComponentName.Empty();
  260. Close( TRUE );
  261. }