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.

1006 lines
28 KiB

4 years ago
4 years ago
3 years ago
2 years ago
4 years ago
2 years ago
2 years ago
4 years ago
4 years ago
4 years ago
4 years ago
2 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2014 Henner Zeller <h.zeller@acm.org>
  5. * Copyright (C) 2014-2024 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #include <widgets/lib_tree.h>
  25. #include <widgets/std_bitmap_button.h>
  26. #include <core/kicad_algo.h>
  27. #include <macros.h>
  28. #include <bitmaps.h>
  29. #include <dialogs/eda_reorderable_list_dialog.h>
  30. #include <tool/tool_interactive.h>
  31. #include <tool/tool_manager.h>
  32. #include <tool/action_manager.h>
  33. #include <tool/actions.h>
  34. #include <tool/tool_dispatcher.h>
  35. #include <widgets/wx_dataviewctrl.h>
  36. #include <wx/settings.h>
  37. #include <wx/sizer.h>
  38. #include <wx/srchctrl.h>
  39. #include <wx/popupwin.h>
  40. #include <eda_doc.h> // for GetAssociatedDocument()
  41. #include <pgm_base.h> // for PROJECT
  42. #include <settings/settings_manager.h> // for PROJECT
  43. constexpr int RECENT_SEARCHES_MAX = 10;
  44. std::map<wxString, std::vector<wxString>> g_recentSearches;
  45. LIB_TREE::LIB_TREE( wxWindow* aParent, const wxString& aRecentSearchesKey, LIB_TABLE* aLibTable,
  46. wxObjectDataPtr<LIB_TREE_MODEL_ADAPTER>& aAdapter, int aFlags,
  47. HTML_WINDOW* aDetails ) :
  48. wxPanel( aParent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
  49. wxWANTS_CHARS | wxTAB_TRAVERSAL | wxNO_BORDER ),
  50. m_adapter( aAdapter ),
  51. m_query_ctrl( nullptr ),
  52. m_sort_ctrl( nullptr ),
  53. m_details_ctrl( nullptr ),
  54. m_inTimerEvent( false ),
  55. m_recentSearchesKey( aRecentSearchesKey ),
  56. m_filtersSizer( nullptr ),
  57. m_skipNextRightClick( false ),
  58. m_previewWindow( nullptr ),
  59. m_previewDisabled( false )
  60. {
  61. wxBoxSizer* sizer = new wxBoxSizer( wxVERTICAL );
  62. m_hoverTimer.SetOwner( this );
  63. Bind( wxEVT_TIMER, &LIB_TREE::onHoverTimer, this, m_hoverTimer.GetId() );
  64. // Search text control
  65. if( aFlags & SEARCH )
  66. {
  67. wxBoxSizer* search_sizer = new wxBoxSizer( wxHORIZONTAL );
  68. m_query_ctrl = new wxSearchCtrl( this, wxID_ANY );
  69. m_query_ctrl->ShowCancelButton( true );
  70. #ifdef __WXGTK__
  71. // wxSearchCtrl vertical height is not calculated correctly on some GTK setups
  72. // See https://gitlab.com/kicad/code/kicad/-/issues/9019
  73. m_query_ctrl->SetMinSize( wxSize( -1, GetTextExtent( wxT( "qb" ) ).y + 10 ) );
  74. #endif
  75. m_debounceTimer = new wxTimer( this );
  76. search_sizer->Add( m_query_ctrl, 1, wxEXPAND | wxRIGHT, 5 );
  77. m_sort_ctrl = new STD_BITMAP_BUTTON( this, wxID_ANY, wxNullBitmap, wxDefaultPosition,
  78. wxDefaultSize, wxBU_AUTODRAW|0 );
  79. m_sort_ctrl->SetBitmap( KiBitmapBundle( BITMAPS::small_sort_desc ) );
  80. m_sort_ctrl->Bind( wxEVT_LEFT_DOWN,
  81. [&]( wxMouseEvent& aEvent )
  82. {
  83. // Build a pop menu:
  84. wxMenu menu;
  85. menu.Append( 4201, _( "Sort by Best Match" ), wxEmptyString, wxITEM_CHECK );
  86. menu.Append( 4202, _( "Sort Alphabetically" ), wxEmptyString, wxITEM_CHECK );
  87. menu.AppendSeparator();
  88. menu.Append( 4203, ACTIONS::expandAll.GetMenuItem() );
  89. menu.Append( 4204, ACTIONS::collapseAll.GetMenuItem() );
  90. if( m_adapter->GetSortMode() == LIB_TREE_MODEL_ADAPTER::BEST_MATCH )
  91. menu.Check( 4201, true );
  92. else
  93. menu.Check( 4202, true );
  94. // menu_id is the selected submenu id from the popup menu or wxID_NONE
  95. int menu_id = m_sort_ctrl->GetPopupMenuSelectionFromUser( menu );
  96. if( menu_id == 0 || menu_id == 4201 )
  97. {
  98. m_adapter->SetSortMode( LIB_TREE_MODEL_ADAPTER::BEST_MATCH );
  99. Regenerate( true );
  100. }
  101. else if( menu_id == 1 || menu_id == 4202 )
  102. {
  103. m_adapter->SetSortMode( LIB_TREE_MODEL_ADAPTER::ALPHABETIC );
  104. Regenerate( true );
  105. }
  106. else if( menu_id == 3 || menu_id == 4203 )
  107. {
  108. ExpandAll();
  109. }
  110. else if( menu_id == 4 || menu_id == 4204 )
  111. {
  112. CollapseAll();
  113. }
  114. } );
  115. m_sort_ctrl->Bind( wxEVT_CHAR_HOOK, &LIB_TREE::onTreeCharHook, this );
  116. search_sizer->Add( m_sort_ctrl, 0, wxEXPAND, 5 );
  117. sizer->Add( search_sizer, 0, wxEXPAND | wxBOTTOM, 5 );
  118. m_query_ctrl->Bind( wxEVT_TEXT, &LIB_TREE::onQueryText, this );
  119. m_query_ctrl->Bind( wxEVT_SEARCH_CANCEL, &LIB_TREE::onQueryText, this );
  120. m_query_ctrl->Bind( wxEVT_CHAR_HOOK, &LIB_TREE::onQueryCharHook, this );
  121. m_query_ctrl->Bind( wxEVT_MOTION, &LIB_TREE::onQueryMouseMoved, this );
  122. #if defined( __WXOSX__ ) || wxCHECK_VERSION( 3, 3, 0 ) // Doesn't work properly on other ports
  123. m_query_ctrl->Bind( wxEVT_LEAVE_WINDOW,
  124. [this]( wxMouseEvent& aEvt )
  125. {
  126. SetCursor( wxCURSOR_ARROW );
  127. } );
  128. #endif
  129. m_query_ctrl->Bind( wxEVT_MENU,
  130. [this]( wxCommandEvent& aEvent )
  131. {
  132. wxString search;
  133. size_t idx = aEvent.GetId() - 1;
  134. if( idx < g_recentSearches[ m_recentSearchesKey ].size() )
  135. m_query_ctrl->SetValue( g_recentSearches[ m_recentSearchesKey ][idx] );
  136. },
  137. 1, RECENT_SEARCHES_MAX );
  138. Bind( wxEVT_TIMER, &LIB_TREE::onDebounceTimer, this, m_debounceTimer->GetId() );
  139. }
  140. if( aFlags & FILTERS )
  141. {
  142. m_filtersSizer = new wxBoxSizer( wxVERTICAL );
  143. sizer->Add( m_filtersSizer, 0, wxEXPAND | wxLEFT, 4 );
  144. }
  145. // Tree control
  146. int dvFlags = ( aFlags & MULTISELECT ) ? wxDV_MULTIPLE : wxDV_SINGLE;
  147. m_tree_ctrl = new WX_DATAVIEWCTRL( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, dvFlags );
  148. m_adapter->AttachTo( m_tree_ctrl );
  149. #ifdef __WXGTK__
  150. // The GTK renderer seems to calculate row height incorrectly sometimes; but can be overridden
  151. int rowHeight = FromDIP( 8 ) + GetTextExtent( wxS( "pdI" ) ).y;
  152. m_tree_ctrl->SetRowHeight( rowHeight );
  153. #endif
  154. sizer->Add( m_tree_ctrl, 5, wxEXPAND, 5 );
  155. // Description panel
  156. if( aFlags & DETAILS )
  157. {
  158. if( !aDetails )
  159. {
  160. wxPoint html_size = ConvertDialogToPixels( wxPoint( 80, 80 ) );
  161. m_details_ctrl = new HTML_WINDOW( this, wxID_ANY, wxDefaultPosition,
  162. wxSize( html_size.x, html_size.y ) );
  163. sizer->Add( m_details_ctrl, 2, wxTOP | wxEXPAND, 5 );
  164. }
  165. else
  166. {
  167. m_details_ctrl = aDetails;
  168. }
  169. m_details_ctrl->Bind( wxEVT_HTML_LINK_CLICKED, &LIB_TREE::onDetailsLink, this );
  170. }
  171. SetSizer( sizer );
  172. m_tree_ctrl->Bind( wxEVT_DATAVIEW_ITEM_ACTIVATED, &LIB_TREE::onTreeActivate, this );
  173. m_tree_ctrl->Bind( wxEVT_DATAVIEW_SELECTION_CHANGED, &LIB_TREE::onTreeSelect, this );
  174. m_tree_ctrl->Bind( wxEVT_DATAVIEW_ITEM_CONTEXT_MENU, &LIB_TREE::onItemContextMenu, this );
  175. m_tree_ctrl->Bind( wxEVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, &LIB_TREE::onHeaderContextMenu,
  176. this );
  177. // wxDataViewCtrl eats its mouseMoved events, so we're forced to use idle events to track
  178. // the hover state
  179. Bind( wxEVT_IDLE, &LIB_TREE::onIdle, this );
  180. // Process hotkeys when the tree control has focus:
  181. m_tree_ctrl->Bind( wxEVT_CHAR_HOOK, &LIB_TREE::onTreeCharHook, this );
  182. Bind( EVT_LIBITEM_SELECTED, &LIB_TREE::onPreselect, this );
  183. if( m_query_ctrl )
  184. {
  185. m_query_ctrl->SetDescriptiveText( _( "Filter" ) );
  186. m_query_ctrl->SetFocus();
  187. m_query_ctrl->SetValue( wxEmptyString );
  188. updateRecentSearchMenu();
  189. // Force an update of the adapter with the empty text to ensure preselect is done
  190. Regenerate( false );
  191. }
  192. else
  193. {
  194. // There may be a part preselected in the model. Make sure it is displayed.
  195. // Regenerate does this in the other branch
  196. postPreselectEvent();
  197. }
  198. Layout();
  199. sizer->Fit( this );
  200. #ifdef __WXGTK__
  201. // Scrollbars must be always enabled to prevent an infinite event loop
  202. // more details: http://trac.wxwidgets.org/ticket/18141
  203. if( m_details_ctrl )
  204. m_details_ctrl->ShowScrollbars( wxSHOW_SB_ALWAYS, wxSHOW_SB_ALWAYS );
  205. #endif /* __WXGTK__ */
  206. }
  207. LIB_TREE::~LIB_TREE()
  208. {
  209. Unbind( wxEVT_TIMER, &LIB_TREE::onHoverTimer, this, m_hoverTimer.GetId() );
  210. m_tree_ctrl->Unbind( wxEVT_DATAVIEW_ITEM_ACTIVATED, &LIB_TREE::onTreeActivate, this );
  211. m_tree_ctrl->Unbind( wxEVT_DATAVIEW_SELECTION_CHANGED, &LIB_TREE::onTreeSelect, this );
  212. m_tree_ctrl->Unbind( wxEVT_DATAVIEW_ITEM_CONTEXT_MENU, &LIB_TREE::onItemContextMenu, this );
  213. m_tree_ctrl->Unbind( wxEVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, &LIB_TREE::onHeaderContextMenu,
  214. this );
  215. Unbind( wxEVT_IDLE, &LIB_TREE::onIdle, this );
  216. m_tree_ctrl->Unbind( wxEVT_CHAR_HOOK, &LIB_TREE::onTreeCharHook, this );
  217. Unbind( EVT_LIBITEM_SELECTED, &LIB_TREE::onPreselect, this );
  218. if( m_query_ctrl )
  219. {
  220. m_query_ctrl->Unbind( wxEVT_TEXT, &LIB_TREE::onQueryText, this );
  221. m_query_ctrl->Unbind( wxEVT_SEARCH_CANCEL, &LIB_TREE::onQueryText, this );
  222. m_query_ctrl->Unbind( wxEVT_CHAR_HOOK, &LIB_TREE::onQueryCharHook, this );
  223. m_query_ctrl->Unbind( wxEVT_MOTION, &LIB_TREE::onQueryMouseMoved, this );
  224. }
  225. // Stop the timer during destruction early to avoid potential race conditions (that do happen)]
  226. if( m_debounceTimer )
  227. {
  228. m_debounceTimer->Stop();
  229. Unbind( wxEVT_TIMER, &LIB_TREE::onDebounceTimer, this, m_debounceTimer->GetId() );
  230. }
  231. if( m_details_ctrl )
  232. m_details_ctrl->Unbind( wxEVT_HTML_LINK_CLICKED, &LIB_TREE::onDetailsLink, this );
  233. m_hoverTimer.Stop();
  234. destroyPreview();
  235. }
  236. void LIB_TREE::ShowChangedLanguage()
  237. {
  238. if( m_query_ctrl )
  239. m_query_ctrl->SetDescriptiveText( _( "Filter" ) );
  240. if( m_adapter )
  241. m_adapter->ShowChangedLanguage();
  242. }
  243. LIB_ID LIB_TREE::GetSelectedLibId( int* aUnit ) const
  244. {
  245. wxDataViewItem sel = m_tree_ctrl->GetSelection();
  246. if( !sel )
  247. return LIB_ID();
  248. if( aUnit )
  249. *aUnit = m_adapter->GetUnitFor( sel );
  250. return m_adapter->GetAliasFor( sel );
  251. }
  252. int LIB_TREE::GetSelectedLibIds( std::vector<LIB_ID>& aSelection, std::vector<int>* aUnit ) const
  253. {
  254. wxDataViewItemArray selection;
  255. int count = m_tree_ctrl->GetSelections( selection );
  256. for( const wxDataViewItem& item : selection )
  257. {
  258. aSelection.emplace_back( m_adapter->GetAliasFor( item ) );
  259. if( aUnit )
  260. aUnit->emplace_back( m_adapter->GetUnitFor( item ) );
  261. }
  262. return count;
  263. }
  264. LIB_TREE_NODE* LIB_TREE::GetCurrentTreeNode() const
  265. {
  266. wxDataViewItem sel = m_tree_ctrl->GetSelection();
  267. if( !sel )
  268. return nullptr;
  269. return m_adapter->GetTreeNodeFor( sel );
  270. }
  271. void LIB_TREE::SelectLibId( const LIB_ID& aLibId )
  272. {
  273. selectIfValid( m_adapter->FindItem( aLibId ) );
  274. }
  275. void LIB_TREE::CenterLibId( const LIB_ID& aLibId )
  276. {
  277. centerIfValid( m_adapter->FindItem( aLibId ) );
  278. }
  279. void LIB_TREE::Unselect()
  280. {
  281. m_tree_ctrl->Freeze();
  282. m_tree_ctrl->UnselectAll();
  283. m_tree_ctrl->Thaw();
  284. }
  285. void LIB_TREE::ExpandLibId( const LIB_ID& aLibId )
  286. {
  287. expandIfValid( m_adapter->FindItem( aLibId ) );
  288. }
  289. void LIB_TREE::ExpandAll()
  290. {
  291. m_tree_ctrl->ExpandAll();
  292. }
  293. void LIB_TREE::CollapseAll()
  294. {
  295. m_tree_ctrl->CollapseAll();
  296. }
  297. void LIB_TREE::SetSearchString( const wxString& aSearchString )
  298. {
  299. m_query_ctrl->ChangeValue( aSearchString );
  300. }
  301. wxString LIB_TREE::GetSearchString() const
  302. {
  303. return m_query_ctrl->GetValue();
  304. }
  305. void LIB_TREE::updateRecentSearchMenu()
  306. {
  307. wxString newEntry = GetSearchString();
  308. std::vector<wxString>& recents = g_recentSearches[ m_recentSearchesKey ];
  309. if( !newEntry.IsEmpty() )
  310. {
  311. if( alg::contains( recents, newEntry ) )
  312. alg::delete_matching( recents, newEntry );
  313. if( recents.size() >= RECENT_SEARCHES_MAX )
  314. recents.pop_back();
  315. recents.insert( recents.begin(), newEntry );
  316. }
  317. wxMenu* menu = new wxMenu();
  318. for( const wxString& recent : recents )
  319. menu->Append( menu->GetMenuItemCount() + 1, recent );
  320. if( recents.empty() )
  321. menu->Append( wxID_ANY, _( "recent searches" ) );
  322. m_query_ctrl->SetMenu( menu );
  323. }
  324. void LIB_TREE::Regenerate( bool aKeepState )
  325. {
  326. STATE current;
  327. // Store the state
  328. if( aKeepState )
  329. current = getState();
  330. wxString filter = m_query_ctrl->GetValue();
  331. m_adapter->UpdateSearchString( filter, aKeepState );
  332. postPreselectEvent();
  333. // Restore the state
  334. if( aKeepState )
  335. setState( current );
  336. }
  337. void LIB_TREE::RefreshLibTree()
  338. {
  339. m_adapter->RefreshTree();
  340. }
  341. wxWindow* LIB_TREE::GetFocusTarget()
  342. {
  343. if( m_query_ctrl )
  344. return m_query_ctrl;
  345. else
  346. return m_tree_ctrl;
  347. }
  348. void LIB_TREE::FocusSearchFieldIfExists()
  349. {
  350. if( m_query_ctrl )
  351. m_query_ctrl->SetFocus();
  352. }
  353. void LIB_TREE::toggleExpand( const wxDataViewItem& aTreeId )
  354. {
  355. if( !aTreeId.IsOk() )
  356. return;
  357. if( m_tree_ctrl->IsExpanded( aTreeId ) )
  358. m_tree_ctrl->Collapse( aTreeId );
  359. else
  360. m_tree_ctrl->Expand( aTreeId );
  361. }
  362. void LIB_TREE::selectIfValid( const wxDataViewItem& aTreeId )
  363. {
  364. if( aTreeId.IsOk() )
  365. {
  366. m_tree_ctrl->EnsureVisible( aTreeId );
  367. m_tree_ctrl->UnselectAll();
  368. m_tree_ctrl->Select( aTreeId );
  369. postPreselectEvent();
  370. }
  371. }
  372. void LIB_TREE::centerIfValid( const wxDataViewItem& aTreeId )
  373. {
  374. /*
  375. * This doesn't actually center because the wxWidgets API is poorly suited to that (and
  376. * it might be too noisy as well).
  377. *
  378. * It does try to keep the given item a bit off the top or bottom of the window.
  379. */
  380. if( aTreeId.IsOk() )
  381. {
  382. LIB_TREE_NODE* node = m_adapter->GetTreeNodeFor( aTreeId );
  383. LIB_TREE_NODE* parent = node->m_Parent;
  384. LIB_TREE_NODE* grandParent = parent ? parent->m_Parent : nullptr;
  385. if( parent )
  386. {
  387. wxDataViewItemArray siblings;
  388. m_adapter->GetChildren( wxDataViewItem( parent ), siblings );
  389. int idx = siblings.Index( aTreeId );
  390. if( idx + 5 < (int) siblings.GetCount() )
  391. {
  392. m_tree_ctrl->EnsureVisible( siblings.Item( idx + 5 ) );
  393. }
  394. else if( grandParent )
  395. {
  396. wxDataViewItemArray parentsSiblings;
  397. m_adapter->GetChildren( wxDataViewItem( grandParent ), parentsSiblings );
  398. int p_idx = parentsSiblings.Index( wxDataViewItem( parent ) );
  399. if( p_idx + 1 < (int) parentsSiblings.GetCount() )
  400. m_tree_ctrl->EnsureVisible( parentsSiblings.Item( p_idx + 1 ) );
  401. }
  402. if( idx - 5 >= 0 )
  403. m_tree_ctrl->EnsureVisible( siblings.Item( idx - 5 ) );
  404. else
  405. m_tree_ctrl->EnsureVisible( wxDataViewItem( parent ) );
  406. }
  407. m_tree_ctrl->EnsureVisible( aTreeId );
  408. }
  409. }
  410. void LIB_TREE::expandIfValid( const wxDataViewItem& aTreeId )
  411. {
  412. if( aTreeId.IsOk() && !m_tree_ctrl->IsExpanded( aTreeId ) )
  413. m_tree_ctrl->Expand( aTreeId );
  414. }
  415. void LIB_TREE::postPreselectEvent()
  416. {
  417. wxCommandEvent event( EVT_LIBITEM_SELECTED );
  418. wxPostEvent( this, event );
  419. }
  420. void LIB_TREE::postSelectEvent()
  421. {
  422. wxCommandEvent event( EVT_LIBITEM_CHOSEN );
  423. wxPostEvent( this, event );
  424. }
  425. LIB_TREE::STATE LIB_TREE::getState() const
  426. {
  427. STATE state;
  428. wxDataViewItemArray items;
  429. m_adapter->GetChildren( wxDataViewItem( nullptr ), items );
  430. for( const wxDataViewItem& item : items )
  431. {
  432. if( m_tree_ctrl->IsExpanded( item ) )
  433. state.expanded.push_back( item );
  434. }
  435. state.selection = GetSelectedLibId();
  436. return state;
  437. }
  438. void LIB_TREE::setState( const STATE& aState )
  439. {
  440. m_tree_ctrl->Freeze();
  441. for( const wxDataViewItem& item : aState.expanded )
  442. m_tree_ctrl->Expand( item );
  443. // wxDataViewCtrl cannot be frozen when a selection
  444. // command is issued, otherwise it selects a random item (Windows)
  445. m_tree_ctrl->Thaw();
  446. if( !aState.selection.GetLibItemName().empty() || !aState.selection.GetLibNickname().empty() )
  447. SelectLibId( aState.selection );
  448. }
  449. void LIB_TREE::onQueryText( wxCommandEvent& aEvent )
  450. {
  451. m_debounceTimer->StartOnce( 200 );
  452. // Required to avoid interaction with SetHint()
  453. // See documentation for wxTextEntry::SetHint
  454. aEvent.Skip();
  455. }
  456. void LIB_TREE::onDebounceTimer( wxTimerEvent& aEvent )
  457. {
  458. m_inTimerEvent = true;
  459. Regenerate( false );
  460. m_inTimerEvent = false;
  461. }
  462. void LIB_TREE::onQueryCharHook( wxKeyEvent& aKeyStroke )
  463. {
  464. int hotkey = aKeyStroke.GetKeyCode();
  465. if( aKeyStroke.GetModifiers() & wxMOD_CONTROL )
  466. hotkey += MD_CTRL;
  467. if( aKeyStroke.GetModifiers() & wxMOD_ALT )
  468. hotkey += MD_ALT;
  469. if( aKeyStroke.GetModifiers() & wxMOD_SHIFT )
  470. hotkey += MD_SHIFT;
  471. if( hotkey == ACTIONS::expandAll.GetHotKey()
  472. || hotkey == ACTIONS::expandAll.GetHotKeyAlt() )
  473. {
  474. m_tree_ctrl->ExpandAll();
  475. return;
  476. }
  477. else if( hotkey == ACTIONS::collapseAll.GetHotKey()
  478. || hotkey == ACTIONS::collapseAll.GetHotKeyAlt() )
  479. {
  480. m_tree_ctrl->CollapseAll();
  481. return;
  482. }
  483. wxDataViewItem sel = m_tree_ctrl->GetSelection();
  484. if( !sel.IsOk() )
  485. sel = m_adapter->GetCurrentDataViewItem();
  486. LIB_TREE_NODE::TYPE type = sel.IsOk() ? m_adapter->GetTypeFor( sel ) : LIB_TREE_NODE::TYPE::INVALID;
  487. switch( aKeyStroke.GetKeyCode() )
  488. {
  489. case WXK_UP:
  490. updateRecentSearchMenu();
  491. selectIfValid( m_tree_ctrl->GetPrevItem( sel ) );
  492. break;
  493. case WXK_DOWN:
  494. updateRecentSearchMenu();
  495. selectIfValid( m_tree_ctrl->GetNextItem( sel ) );
  496. break;
  497. case WXK_ADD:
  498. updateRecentSearchMenu();
  499. if( type == LIB_TREE_NODE::TYPE::LIBRARY )
  500. m_tree_ctrl->Expand( sel );
  501. break;
  502. case WXK_SUBTRACT:
  503. updateRecentSearchMenu();
  504. if( type == LIB_TREE_NODE::TYPE::LIBRARY )
  505. m_tree_ctrl->Collapse( sel );
  506. break;
  507. case WXK_RETURN:
  508. case WXK_NUMPAD_ENTER:
  509. updateRecentSearchMenu();
  510. if( GetSelectedLibId().IsValid() )
  511. postSelectEvent();
  512. else if( type == LIB_TREE_NODE::TYPE::LIBRARY )
  513. toggleExpand( sel );
  514. break;
  515. default:
  516. aKeyStroke.Skip(); // Any other key: pass on to search box directly.
  517. break;
  518. }
  519. }
  520. void LIB_TREE::onQueryMouseMoved( wxMouseEvent& aEvent )
  521. {
  522. #if defined( __WXOSX__ ) || wxCHECK_VERSION( 3, 3, 0 ) // Doesn't work properly on other ports
  523. wxPoint pos = aEvent.GetPosition();
  524. wxRect ctrlRect = m_query_ctrl->GetScreenRect();
  525. int buttonWidth = ctrlRect.GetHeight(); // Presume buttons are square
  526. if( m_query_ctrl->IsSearchButtonVisible() && pos.x < buttonWidth )
  527. SetCursor( wxCURSOR_ARROW );
  528. else if( m_query_ctrl->IsCancelButtonVisible() && pos.x > ctrlRect.GetWidth() - buttonWidth )
  529. SetCursor( wxCURSOR_ARROW );
  530. else
  531. SetCursor( wxCURSOR_IBEAM );
  532. #endif
  533. }
  534. #define PREVIEW_SIZE wxSize( 240, 200 )
  535. #define HOVER_TIMER_MILLIS 400
  536. void LIB_TREE::showPreview( wxDataViewItem aItem )
  537. {
  538. if( aItem.IsOk() && m_adapter->HasPreview( aItem ) )
  539. {
  540. m_previewItem = aItem;
  541. m_previewItemRect = m_tree_ctrl->GetItemRect( m_previewItem );
  542. wxWindow* topLevelParent = wxGetTopLevelParent( m_parent );
  543. if( !m_previewWindow )
  544. m_previewWindow = new wxPopupWindow( topLevelParent );
  545. m_previewWindow->SetSize( PREVIEW_SIZE );
  546. m_adapter->ShowPreview( m_previewWindow, aItem );
  547. m_previewWindow->SetPosition( wxPoint( m_tree_ctrl->GetScreenRect().GetRight() - 10,
  548. wxGetMousePosition().y - PREVIEW_SIZE.y / 2 ) );
  549. m_previewWindow->Show();
  550. }
  551. }
  552. void LIB_TREE::hidePreview()
  553. {
  554. m_previewItem = wxDataViewItem();
  555. if( m_previewWindow )
  556. m_previewWindow->Hide();
  557. }
  558. void LIB_TREE::destroyPreview()
  559. {
  560. hidePreview();
  561. if( m_previewWindow )
  562. {
  563. m_previewWindow->Destroy();
  564. m_previewWindow = nullptr;
  565. }
  566. }
  567. void LIB_TREE::onIdle( wxIdleEvent& aEvent )
  568. {
  569. // The wxDataViewCtrl won't give us its mouseMoved events so we're forced to use idle
  570. // events to track the hover state
  571. // The dang thing won't give us scroll events either, so we implement a poor-man's
  572. // scroll-checker using the last-known positions of the preview or hover items.
  573. wxWindow* topLevelParent = wxGetTopLevelParent( m_parent );
  574. wxWindow* topLevelFocus = wxGetTopLevelParent( wxWindow::FindFocus() );
  575. bool mouseOverWindow = false;
  576. wxPoint screenPos = wxGetMousePosition();
  577. if( m_tree_ctrl->IsShownOnScreen() )
  578. mouseOverWindow |= m_tree_ctrl->GetScreenRect().Contains( screenPos );
  579. if( m_previewDisabled || topLevelFocus != topLevelParent || !mouseOverWindow )
  580. {
  581. m_hoverTimer.Stop();
  582. hidePreview();
  583. return;
  584. }
  585. wxPoint clientPos = m_tree_ctrl->ScreenToClient( screenPos );
  586. wxDataViewItem item;
  587. wxDataViewColumn* col = nullptr;
  588. m_tree_ctrl->HitTest( clientPos, item, col );
  589. if( m_previewItem.IsOk() )
  590. {
  591. if( item != m_previewItem )
  592. {
  593. #ifdef __WXGTK__
  594. // Hide the preview, because Wayland can't move windows.
  595. if( wxGetDisplayInfo().type == wxDisplayType::wxDisplayWayland )
  596. hidePreview();
  597. #endif
  598. showPreview( item );
  599. }
  600. return;
  601. }
  602. if( m_hoverPos != clientPos )
  603. {
  604. m_hoverPos = clientPos;
  605. m_hoverItem = item;
  606. m_hoverItemRect = m_tree_ctrl->GetItemRect( m_hoverItem );
  607. m_hoverTimer.StartOnce( HOVER_TIMER_MILLIS );
  608. }
  609. }
  610. void LIB_TREE::onHoverTimer( wxTimerEvent& aEvent )
  611. {
  612. hidePreview();
  613. TOOL_DISPATCHER* toolDispatcher = m_adapter->GetToolDispatcher();
  614. if( !m_tree_ctrl->IsShownOnScreen() || m_previewDisabled
  615. || ( toolDispatcher && toolDispatcher->GetCurrentMenu() ) )
  616. return;
  617. wxDataViewItem item;
  618. wxDataViewColumn* col = nullptr;
  619. m_tree_ctrl->HitTest( m_hoverPos, item, col );
  620. if( item == m_hoverItem && m_tree_ctrl->GetItemRect( item ) == m_hoverItemRect )
  621. {
  622. if( item != m_tree_ctrl->GetSelection() )
  623. showPreview( item );
  624. }
  625. else // view must have been scrolled
  626. {
  627. m_hoverItem = item;
  628. m_hoverItemRect = m_tree_ctrl->GetItemRect( m_hoverItem );
  629. m_hoverTimer.StartOnce( HOVER_TIMER_MILLIS );
  630. }
  631. }
  632. void LIB_TREE::onTreeCharHook( wxKeyEvent& aKeyStroke )
  633. {
  634. onQueryCharHook( aKeyStroke );
  635. if( aKeyStroke.GetSkipped() )
  636. {
  637. if( TOOL_INTERACTIVE* tool = m_adapter->GetContextMenuTool() )
  638. {
  639. int hotkey = aKeyStroke.GetKeyCode();
  640. if( aKeyStroke.ShiftDown() )
  641. hotkey |= MD_SHIFT;
  642. if( aKeyStroke.AltDown() )
  643. hotkey |= MD_ALT;
  644. if( aKeyStroke.ControlDown() )
  645. hotkey |= MD_CTRL;
  646. if( tool->GetManager()->GetActionManager()->RunHotKey( hotkey ) )
  647. aKeyStroke.Skip( false );
  648. }
  649. }
  650. }
  651. void LIB_TREE::onTreeSelect( wxDataViewEvent& aEvent )
  652. {
  653. if( m_tree_ctrl->IsFrozen() )
  654. return;
  655. if( !m_inTimerEvent )
  656. updateRecentSearchMenu();
  657. postPreselectEvent();
  658. }
  659. void LIB_TREE::onTreeActivate( wxDataViewEvent& aEvent )
  660. {
  661. hidePreview();
  662. if( !m_inTimerEvent )
  663. updateRecentSearchMenu();
  664. if( !GetSelectedLibId().IsValid() )
  665. toggleExpand( m_tree_ctrl->GetSelection() ); // Expand library/part units subtree
  666. else
  667. postSelectEvent(); // Open symbol/footprint
  668. }
  669. void LIB_TREE::onDetailsLink( wxHtmlLinkEvent& aEvent )
  670. {
  671. const wxHtmlLinkInfo& info = aEvent.GetLinkInfo();
  672. wxString docname = info.GetHref();
  673. PROJECT& prj = Pgm().GetSettingsManager().Prj();
  674. GetAssociatedDocument( this, docname, &prj );
  675. }
  676. void LIB_TREE::onPreselect( wxCommandEvent& aEvent )
  677. {
  678. hidePreview();
  679. if( m_details_ctrl )
  680. {
  681. int unit = 0;
  682. LIB_ID id = GetSelectedLibId( &unit );
  683. if( id.IsValid() )
  684. m_details_ctrl->SetPage( m_adapter->GenerateInfo( id, unit ) );
  685. else
  686. m_details_ctrl->SetPage( wxEmptyString );
  687. }
  688. aEvent.Skip();
  689. }
  690. void LIB_TREE::onItemContextMenu( wxDataViewEvent& aEvent )
  691. {
  692. hidePreview();
  693. if( m_skipNextRightClick )
  694. {
  695. m_skipNextRightClick = false;
  696. return;
  697. }
  698. m_previewDisabled = true;
  699. if( TOOL_INTERACTIVE* tool = m_adapter->GetContextMenuTool() )
  700. {
  701. if( !GetCurrentTreeNode() )
  702. {
  703. wxPoint pos = m_tree_ctrl->ScreenToClient( wxGetMousePosition() );
  704. wxDataViewItem item;
  705. wxDataViewColumn* col;
  706. m_tree_ctrl->HitTest( pos, item, col );
  707. if( item.IsOk() )
  708. {
  709. m_tree_ctrl->SetFocus();
  710. m_tree_ctrl->Select( item );
  711. wxSafeYield();
  712. }
  713. }
  714. tool->Activate();
  715. tool->GetManager()->VetoContextMenuMouseWarp();
  716. tool->GetToolMenu().ShowContextMenu();
  717. TOOL_EVENT evt( TC_MOUSE, TA_MOUSE_CLICK, BUT_RIGHT );
  718. tool->GetManager()->DispatchContextMenu( evt );
  719. }
  720. else
  721. {
  722. LIB_TREE_NODE* current = GetCurrentTreeNode();
  723. if( current && current->m_Type == LIB_TREE_NODE::TYPE::LIBRARY )
  724. {
  725. ACTION_MENU menu( true, nullptr );
  726. if( current->m_Pinned )
  727. {
  728. menu.Add( ACTIONS::unpinLibrary );
  729. if( GetPopupMenuSelectionFromUser( menu ) != wxID_NONE )
  730. m_adapter->UnpinLibrary( current );
  731. }
  732. else
  733. {
  734. menu.Add( ACTIONS::pinLibrary );
  735. if( GetPopupMenuSelectionFromUser( menu ) != wxID_NONE )
  736. m_adapter->PinLibrary( current );
  737. }
  738. }
  739. }
  740. m_previewDisabled = false;
  741. }
  742. void LIB_TREE::onHeaderContextMenu( wxDataViewEvent& aEvent )
  743. {
  744. hidePreview();
  745. m_previewDisabled = true;
  746. ACTION_MENU menu( true, nullptr );
  747. menu.Add( ACTIONS::selectLibTreeColumns );
  748. if( GetPopupMenuSelectionFromUser( menu ) != wxID_NONE )
  749. {
  750. EDA_REORDERABLE_LIST_DIALOG dlg( m_parent, _( "Select Columns" ),
  751. m_adapter->GetAvailableColumns(),
  752. m_adapter->GetShownColumns() );
  753. if( dlg.ShowModal() == wxID_OK )
  754. m_adapter->SetShownColumns( dlg.EnabledList() );
  755. }
  756. m_previewDisabled = false;
  757. }
  758. wxDEFINE_EVENT( EVT_LIBITEM_SELECTED, wxCommandEvent );
  759. wxDEFINE_EVENT( EVT_LIBITEM_CHOSEN, wxCommandEvent );