KiCad PCB EDA Suite
Loading...
Searching...
No Matches
panel_toolbar_customization.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KICAD, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
25
26#include <bitmaps.h>
28#include <tool/actions.h>
33
34#include <magic_enum.hpp>
35#include <wx/listctrl.h>
36#include <wx/menu.h>
37#include <widgets/ui_common.h>
38
39#include <algorithm>
40#include <set>
41
42// Simple IDs for the split button menu
43enum
44{
45 ID_SEPARATOR_MENU = ( wxID_HIGHEST + 5 ),
48};
49
50
51static std::map<TOOLBAR_LOC, wxString> s_toolbarNameMap = {
52 { TOOLBAR_LOC::LEFT, _( "Left" ) },
53 { TOOLBAR_LOC::RIGHT, _( "Right" ) },
54 { TOOLBAR_LOC::TOP_MAIN, _( "Top main" ) },
55 { TOOLBAR_LOC::TOP_AUX, _( "Top auxiliary" ) }
56};
57
58
59class TOOLBAR_TREE_ITEM_DATA : public wxTreeItemData
60{
61public:
63 m_type( TOOLBAR_ITEM_TYPE::SEPARATOR ), // Init m_type to something
64 m_action( nullptr ),
65 m_control( nullptr ),
66 m_size( 0 )
67 { }
68
70 m_type( aType ),
71 m_action( nullptr ),
72 m_control( nullptr ),
73 m_size( 0 )
74 { }
75
77 m_type( aType ),
78 m_action( nullptr ),
79 m_control( nullptr ),
80 m_size( aSize )
81 {
82 wxASSERT( aType == TOOLBAR_ITEM_TYPE::SPACER );
83 }
84
85 TOOLBAR_TREE_ITEM_DATA( TOOLBAR_ITEM_TYPE aType, wxString aName ) :
86 m_type( aType ),
87 m_action( nullptr ),
88 m_control( nullptr ),
89 m_size( 0 ),
90 m_name( aName )
91 {
92 wxASSERT( aType == TOOLBAR_ITEM_TYPE::CONTROL
93 || aType == TOOLBAR_ITEM_TYPE::TB_GROUP );
94 }
95
96 void SetAction( TOOL_ACTION* aAction ) { m_action = aAction; }
98 {
99 wxASSERT( m_type == TOOLBAR_ITEM_TYPE::TOOL );
100 return m_action;
101 }
102
103 void SetControl( ACTION_TOOLBAR_CONTROL* aControl ) { m_control = aControl; }
105 {
106 wxASSERT( m_type == TOOLBAR_ITEM_TYPE::CONTROL );
107 return m_control;
108 }
109
110 void SetName( const wxString& aName ) { m_name = aName; }
111 const wxString& GetName() const { return m_name; }
112
113 void SetSize( int aSize ) { m_size = aSize; }
114 int GetSize() const { return m_size; }
115
116 TOOLBAR_ITEM_TYPE GetType() const { return m_type; }
117
118private:
119 // Item type
121
122 // Tool properties (can be one or the other, but never both)
125
126 // Spacer properties
128
129 // Group/control properties
130 wxString m_name;
131};
132
133
135 TOOLBAR_SETTINGS* aTbSettings, FRAME_T aActionContext,
136 const std::vector<TOOL_ACTION*>& aTools,
137 const std::vector<ACTION_TOOLBAR_CONTROL*>& aControls ) :
139 m_appSettings( aCfg ),
140 m_appTbSettings( aTbSettings ),
142 m_actionContext( aActionContext )
143{
144 // Copy the tools and controls into the internal maps
145 for( auto& tool : aTools )
146 m_availableTools.emplace( tool->GetName(), tool );
147
148 for( auto& control : aControls )
149 m_availableControls.emplace( control->GetName(), control );
150
151 // Configure the Ui elements
156
157 m_insertButton->SetLabel( _( "Insert Separator" ) );
158 //m_insertButton->SetWidthPadding( 4 );
159
160 // Populate the browse library options
161 wxMenu* insertMenu = m_insertButton->GetSplitButtonMenu();
162
163 insertMenu->Append( ID_SPACER_MENU, _( "Insert Spacer" ) );
164 insertMenu->Append( ID_GROUP_MENU, _( "Insert Group" ) );
165
166 insertMenu->Bind( wxEVT_COMMAND_MENU_SELECTED, &PANEL_TOOLBAR_CUSTOMIZATION::onSpacerPress,
167 this, ID_SPACER_MENU );
168 insertMenu->Bind( wxEVT_COMMAND_MENU_SELECTED, &PANEL_TOOLBAR_CUSTOMIZATION::onGroupPress,
169 this, ID_GROUP_MENU );
170
171 // This is the button only press for the browse button instead of the menu
173
174 m_actionFilter->ShowSearchButton( false );
175 m_actionFilter->ShowCancelButton( true );
176 m_actionFilter->SetDescriptiveText( _( "Filter actions" ) );
177
178#ifdef __WXGTK__
179 m_actionFilter->SetMinSize( wxSize( -1, GetTextExtent( wxT( "qb" ) ).y + 10 ) );
180#endif
181
183 m_actionFilter->Bind( wxEVT_SEARCHCTRL_CANCEL_BTN,
186 m_actionsList->Bind( wxEVT_LEAVE_WINDOW, &PANEL_TOOLBAR_CUSTOMIZATION::onActionListMouseMove, this );
187
188 // TODO (ISM): Enable draging
189 m_btnToolMoveDown->Enable( false );
190 m_btnToolMoveUp->Enable( false );
191}
192
193
202
203
205{
206 const std::string& name = aAction.GetName();
207
208 auto hasPrefix = [&]( const char* aPrefix ) -> bool
209 {
210 return name.rfind( aPrefix, 0 ) == 0;
211 };
212
214 {
215 if( hasPrefix( "3DViewer." ) )
216 return true;
217
218 return name == ACTIONS::zoomRedraw.GetName() || name == ACTIONS::zoomInCenter.GetName()
219 || name == ACTIONS::zoomOutCenter.GetName() || name == ACTIONS::zoomFitScreen.GetName();
220 }
221
222 if( hasPrefix( "common." ) )
223 return true;
224
225 switch( m_actionContext )
226 {
227 case FRAME_PCB_EDITOR:
229 case FRAME_FOOTPRINT_VIEWER: return hasPrefix( "pcbnew." );
230
231 case FRAME_SCH:
233 case FRAME_SCH_VIEWER:
234 case FRAME_SIMULATOR: return hasPrefix( "eeschema." );
235
236 case FRAME_GERBER: return hasPrefix( "gerbview." );
237
238 case FRAME_PL_EDITOR: return hasPrefix( "plEditor." );
239
240 default: return false;
241 }
242}
243
244
246{
247 m_toolbars.clear();
248 m_toolbarChoices.clear();
249
250 // Go over every toolbar and initialize things
251 for( auto& tb : magic_enum::enum_values<TOOLBAR_LOC>() )
252 {
253 // Create a shadow toolbar
254 auto tbConfig = m_appTbSettings->DefaultToolbarConfig( tb );
255
256 if( !tbConfig.has_value() )
257 continue;
258
259 m_toolbars[tb] = tbConfig.value();
260 m_toolbarChoices.push_back( tb );
261 }
262
263 if( !m_toolbarChoices.empty() )
264 {
265 m_tbChoice->SetSelection( 0 );
267 }
268
270}
271
272
274{
275 wxArrayString tbChoices;
276
277 m_toolbars.clear();
278 m_toolbarChoices.clear();
279
280 // Go over every toolbar and initialize things
281 for( auto& tb : magic_enum::enum_values<TOOLBAR_LOC>() )
282 {
283 // Create a shadow toolbar
284 auto tbConfig = m_appTbSettings->GetToolbarConfig( tb );
285
286 if( !tbConfig.has_value() )
287 continue;
288
289 m_toolbars.emplace( tb, tbConfig.value() );
290 m_toolbarChoices.push_back( tb );
291
292 // Setup the UI name
293 const auto& tbName = s_toolbarNameMap.find( tb );
294
295 wxASSERT_MSG( tbName != s_toolbarNameMap.end(),
296 wxString::Format( "Unknown toolbar: %s", magic_enum::enum_name( tb ) ) );
297
298 tbChoices.Add( tbName->second );
299 }
300
301 m_tbChoice->Set( tbChoices );
302
303 // Always populate the actions before the toolbars, that way the icons are available
305
306 if( !m_toolbarChoices.empty() )
307 {
308 m_tbChoice->SetSelection( 0 );
310 }
311
313
314 // Sync the enable/disable control
315 enableCustomControls( m_appSettings->m_CustomToolbars );
316 m_customToolbars->SetValue( m_appSettings->m_CustomToolbars );
317
318 return true;
319}
320
321
323{
324 m_appSettings->m_CustomToolbars = m_customToolbars->GetValue();
325
326 // Store the current toolbar
327 std::optional<TOOLBAR_CONFIGURATION> currentTb = parseToolbarTree();
328
329 if( currentTb.has_value() )
330 m_toolbars[m_currentToolbar] = currentTb.value();
331
332 std::set<std::string> seenControls;
333
334 for( auto& [loc, config] : m_toolbars )
335 {
336 auto& items = config.m_toolbarItems;
337
338 items.erase( std::remove_if( items.begin(), items.end(),
339 [&]( const TOOLBAR_ITEM& item )
340 {
341 if( item.m_Type != TOOLBAR_ITEM_TYPE::CONTROL )
342 return false;
343
344 return !seenControls.insert( item.m_ControlName ).second;
345 } ),
346 items.end() );
347 }
348
349 // Write the shadow toolbars with changes back to the app toolbar settings
350 for( const auto& [loc, config] : m_toolbars )
351 m_appTbSettings->SetStoredToolbarConfig( loc, config );
352
353 return true;
354}
355
356std::optional<TOOLBAR_CONFIGURATION> PANEL_TOOLBAR_CUSTOMIZATION::parseToolbarTree()
357{
359
360 wxTreeItemId mainId;
361 wxTreeItemId rootId = m_toolbarTree->GetRootItem();
362 wxTreeItemIdValue mainCookie;
363
364 if( !rootId.IsOk() )
365 return std::nullopt;
366
367 mainId = m_toolbarTree->GetFirstChild( rootId, mainCookie );
368
369 while( mainId.IsOk() )
370 {
371 wxTreeItemData* treeData = m_toolbarTree->GetItemData( mainId );
372
373 TOOLBAR_TREE_ITEM_DATA* tbData = dynamic_cast<TOOLBAR_TREE_ITEM_DATA*>( treeData );
374
375 wxCHECK2( tbData, continue );
376
377 switch( tbData->GetType() )
378 {
380 config.AppendSpacer( tbData->GetSize() );
381 break;
382
384 config.AppendSeparator();
385 break;
386
388 config.AppendControl( tbData->GetControl()->GetName() );
389 break;
390
392 config.AppendAction( *( tbData->GetAction() ) );
393 break;
394
396 TOOLBAR_GROUP_CONFIG grpConfig( tbData->GetName() );
397
398 if( m_toolbarTree->ItemHasChildren( mainId ) )
399 {
400 wxTreeItemIdValue childCookie;
401 wxTreeItemId childId = m_toolbarTree->GetFirstChild( mainId, childCookie );
402
403 while( childId.IsOk() )
404 {
405 wxTreeItemData* childTreeData = m_toolbarTree->GetItemData( childId );
406
407 TOOLBAR_TREE_ITEM_DATA* childTbData = dynamic_cast<TOOLBAR_TREE_ITEM_DATA*>( childTreeData );
408
409 wxCHECK2( childTbData, break );
410
411 switch( childTbData->GetType() )
412 {
417 wxASSERT_MSG( false, "Invalid entry in a group" );
418 break;
419
421 grpConfig.AddAction( *( childTbData->GetAction() ) );
422 break;
423 }
424
425 childId = m_toolbarTree->GetNextChild( mainId, childCookie );
426 }
427 }
428
429 config.AppendGroup( grpConfig );
430 }
431
432 mainId = m_toolbarTree->GetNextChild( rootId, mainCookie );
433 }
434
435 return config;
436}
437
438
440{
441 m_toolbarTree->DeleteAllItems();
443
444 const auto& it = m_toolbars.find( m_currentToolbar );
445
446 if( it == m_toolbars.end() )
447 {
448 // Disable the controls and bail out - no toolbar here
449 enableToolbarControls( false );
450 return;
451 }
452
453 // Ensure the controls are enabled
454 enableToolbarControls( true );
455
456 TOOLBAR_CONFIGURATION toolbar = it->second;
457
458 wxTreeItemId root = m_toolbarTree->AddRoot( "Toolbar" );
459
460 for( const TOOLBAR_ITEM& item : toolbar.GetToolbarItems() )
461 {
462 switch( item.m_Type )
463 {
465 {
466 // Add a separator
468 m_toolbarTree->AppendItem( root, _( "Separator" ), -1, -1, sepTreeItem );
469 break;
470 }
471
473 {
474 // Add a spacer
476 spacerTreeItem->SetSize( item.m_Size );
477 m_toolbarTree->AppendItem( root, wxString::Format( _( "Spacer: %i" ), item.m_Size ), -1, -1,
478 spacerTreeItem );
479 break;
480 }
481
483 {
484 auto controlIter = m_availableControls.find( item.m_ControlName );
485
486 if( controlIter == m_availableControls.end() )
487 {
488 wxASSERT_MSG( false, wxString::Format( "Unable to find control %s", item.m_ControlName ) );
489 continue;
490 }
491
492 // Add a control
494 controlTreeItem->SetControl( controlIter->second );
495 m_toolbarTree->AppendItem( root, controlIter->second->GetUiName(), -1, -1, controlTreeItem );
496 break;
497 }
498
500 {
501 // Add a tool
502 auto toolIter = m_availableTools.find( item.m_ActionName );
503
504 if( toolIter == m_availableTools.end() )
505 {
506 wxASSERT_MSG( false, wxString::Format( "Unable to find tool %s", item.m_ActionName ) );
507 continue;
508 }
509
510 if( !isActionSupported( *toolIter->second ) )
511 continue;
512
514 toolTreeItem->SetAction( toolIter->second );
515
516 int imgIdx = -1;
517 auto imgMap = m_actionImageListMap.find( item.m_ActionName );
518
519 if( imgMap != m_actionImageListMap.end() )
520 imgIdx = imgMap->second;
521
522 m_toolbarTree->AppendItem( root, toolIter->second->GetFriendlyName(), imgIdx, -1, toolTreeItem );
523 break;
524 }
525
527 {
528 // Add a group of items to the toolbar
530 groupTreeItem->SetName( item.m_GroupName );
531
532 wxTreeItemId groupId = m_toolbarTree->AppendItem( root, item.m_GroupName, -1, -1, groupTreeItem );
533 bool haveVisibleGroupItems = false;
534
535 // Add the elements below the group
536 for( const TOOLBAR_ITEM& groupItem : item.m_GroupItems )
537 {
538 auto toolMap = m_availableTools.find( groupItem.m_ActionName );
539
540 if( toolMap == m_availableTools.end() )
541 {
542 wxASSERT_MSG( false, wxString::Format( "Unable to find group tool %s", groupItem.m_ActionName ) );
543 continue;
544 }
545
546 if( !isActionSupported( *toolMap->second ) )
547 continue;
548
550 toolTreeItem->SetAction( toolMap->second );
551
552 int imgIdx = -1;
553 auto imgMap = m_actionImageListMap.find( groupItem.m_ActionName );
554
555 if( imgMap != m_actionImageListMap.end() )
556 imgIdx = imgMap->second;
557
558 m_toolbarTree->AppendItem( groupId, toolMap->second->GetFriendlyName(), imgIdx, -1, toolTreeItem );
559
560 haveVisibleGroupItems = true;
561 }
562
563 if( !haveVisibleGroupItems )
564 m_toolbarTree->Delete( groupId );
565
566 break;
567 }
568 }
569 }
570
571 m_toolbarTree->ExpandAll();
572
573 wxTreeItemIdValue temp;
574 wxTreeItemId firstItem = m_toolbarTree->GetFirstChild( root, temp );
575
576 if( firstItem.IsOk() )
577 {
578 m_toolbarTree->SelectItem( firstItem );
579 m_toolbarTree->EnsureVisible( firstItem );
580 }
581}
582
583
585{
586 const int c_defSize = 24; // Default icon size for toolbar actions
587
588 // Clear all existing information for the actions
589 m_actionImageListMap.clear();
591 m_actionEntries.clear();
592
593 // Prep the control
594 m_actionsList->DeleteAllItems();
595 m_actionsList->DeleteAllColumns();
596 m_actionsList->InsertColumn( 0, "", wxLIST_FORMAT_LEFT, wxLIST_AUTOSIZE );
597
598 for( const auto& [k, tool] : m_availableTools )
599 {
600 if( !isActionSupported( *tool ) )
601 continue;
602
603 if( tool->CheckToolbarState( TOOLBAR_STATE::HIDDEN ) )
604 continue;
605
606 ACTION_LIST_ENTRY entry;
607 entry.label = tool->GetFriendlyName();
608 entry.tooltip = tool->GetDescription(); // falls back to tooltip if no description provided
609 entry.action = tool;
610 entry.search_text = entry.label.Upper() + wxS( " " ) + entry.tooltip.Upper();
611
612 if( tool->GetIcon() != BITMAPS::INVALID_BITMAP )
613 {
614 int imgIdx = m_actionImageBundleVector.size();
615 m_actionImageBundleVector.push_back( KiBitmapBundleDef( tool->GetIcon(), c_defSize ) );
616 m_actionImageListMap.emplace( tool->GetName(), imgIdx );
617 entry.image_index = imgIdx;
618 }
619
620 m_actionEntries.push_back( std::move( entry ) );
621 }
622
623 for( const auto& [k, control] : m_availableControls )
624 {
625 ACTION_LIST_ENTRY entry;
626 entry.label = control->GetUiName();
627 entry.tooltip = control->GetDescription();
628 entry.control = control;
629 entry.search_text = entry.label.Upper() + wxS( " " ) + control->GetDescription().Upper();
630 m_actionEntries.push_back( std::move( entry ) );
631 }
632
633 std::sort( m_actionEntries.begin(), m_actionEntries.end(),
634 []( const ACTION_LIST_ENTRY& a, const ACTION_LIST_ENTRY& b )
635 {
636 return a.label.CmpNoCase( b.label ) < 0;
637 } );
638
639 m_actionsList->SetSmallImages( m_actionImageBundleVector );
641}
642
643
645 const wxString& aFilter ) const
646{
647 if( aFilter.IsEmpty() )
648 return true;
649
650 return aEntry.search_text.Contains( aFilter.Upper() );
651}
652
653
655{
656 wxFont listFont = KIUI::GetInfoFont( this );
657 wxString filter = m_actionFilter->GetValue();
658
660 m_actionsList->UnsetToolTip();
661
662 m_actionsList->DeleteAllItems();
663
664 for( size_t idx = 0; idx < m_actionEntries.size(); ++idx )
665 {
666 const ACTION_LIST_ENTRY& entry = m_actionEntries[idx];
667
668 if( !actionMatchesFilter( entry, filter ) )
669 continue;
670
671 wxListItem item;
672 item.SetId( m_actionsList->GetItemCount() );
673 item.SetText( entry.label );
674 item.SetFont( listFont );
675 item.SetData( static_cast<long>( idx ) );
676 item.SetImage( entry.image_index );
677
678 m_actionsList->InsertItem( item );
679 }
680
681 if( m_actionsList->GetItemCount() > 0 )
682 m_actionsList->SetItemState( 0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
683
684 m_actionsList->SetColumnWidth( 0, wxLIST_AUTOSIZE );
685}
686
687
688void PANEL_TOOLBAR_CUSTOMIZATION::onGroupPress( wxCommandEvent& aEvent )
689{
691
692 wxTreeItemId newItem;
693 wxTreeItemId selItem = m_toolbarTree->GetSelection();
694
695 if( selItem.IsOk() )
696 {
697 // Can't add a group onto a group
698 wxTreeItemId parent = m_toolbarTree->GetItemParent( selItem );
699
700 if( parent.IsOk() )
701 {
702 wxTreeItemId secondParent = m_toolbarTree->GetItemParent( parent );
703
704 if( secondParent.IsOk() )
705 {
706 delete treeItem;
707 return;
708 }
709 }
710
711 newItem = m_toolbarTree->InsertItem( m_toolbarTree->GetRootItem(), selItem, treeItem->GetName(),
712 -1, -1, treeItem );
713 }
714 else
715 {
716 newItem = m_toolbarTree->AppendItem( m_toolbarTree->GetRootItem(), treeItem->GetName(), -1, -1,
717 treeItem );
718 }
719
720 if( newItem.IsOk() )
721 {
722 m_toolbarTree->SelectItem( newItem );
723 m_toolbarTree->EnsureVisible( newItem );
724 }
725}
726
727
728void PANEL_TOOLBAR_CUSTOMIZATION::onSpacerPress( wxCommandEvent& aEvent )
729{
731
732 wxString label = wxString::Format( "Spacer: %i", treeItem->GetSize() );
733
734 wxTreeItemId newItem;
735 wxTreeItemId selItem = m_toolbarTree->GetSelection();
736
737 if( selItem.IsOk() )
738 {
739 // Insert after the current selection at the same level
740 wxTreeItemId parent = m_toolbarTree->GetItemParent( selItem );
741
742 // Can't insert a spacer in a group yet
743 if( parent.IsOk() )
744 {
745 wxTreeItemId secondParent = m_toolbarTree->GetItemParent( parent );
746
747 if( secondParent.IsOk() )
748 {
749 delete treeItem;
750 return;
751 }
752 }
753
754 newItem = m_toolbarTree->InsertItem( parent, selItem, label, -1, -1, treeItem );
755 }
756 else
757 {
758 newItem = m_toolbarTree->AppendItem( m_toolbarTree->GetRootItem(), label, -1, -1, treeItem );
759 }
760
761 if( newItem.IsOk() )
762 {
763 m_toolbarTree->SelectItem( newItem );
764 m_toolbarTree->EnsureVisible( newItem );
765 }
766}
767
768
770{
772
773 wxTreeItemId newItem;
774 wxTreeItemId selItem = m_toolbarTree->GetSelection();
775
776 if( selItem.IsOk() )
777 {
778 // Insert after the current selection at the same level
779 wxTreeItemId parent = m_toolbarTree->GetItemParent( selItem );
780
781 // Can't insert a separator in a group yet
782 if( parent.IsOk() )
783 {
784 wxTreeItemId secondParent = m_toolbarTree->GetItemParent( parent );
785
786 if( secondParent.IsOk() )
787 {
788 delete treeItem;
789 return;
790 }
791 }
792
793 newItem = m_toolbarTree->InsertItem( parent, selItem, _( "Separator" ), -1, -1, treeItem );
794 }
795 else
796 {
797 newItem = m_toolbarTree->AppendItem( m_toolbarTree->GetRootItem(), _( "Separator" ), -1, -1, treeItem );
798 }
799
800 if( newItem.IsOk() )
801 {
802 m_toolbarTree->SelectItem( newItem );
803 m_toolbarTree->EnsureVisible( newItem );
804 }
805}
806
807
809{
810 enableCustomControls( event.IsChecked() );
811}
812
813
815{
816 m_tbChoice->Enable( enable );
817 enableToolbarControls( enable );
818}
819
820
822{
823 m_toolbarTree->Enable( enable );
824 m_btnAddTool->Enable( enable );
825 m_btnToolDelete->Enable( enable );
826
827 m_btnToolMoveDown->Enable( enable );
828 m_btnToolMoveUp->Enable( enable );
829 m_actionsList->Enable( enable );
830 m_actionFilter->Enable( enable );
831 m_insertButton->Enable( enable );
832}
833
834
835void PANEL_TOOLBAR_CUSTOMIZATION::onToolDelete( wxCommandEvent& event )
836{
837 wxTreeItemId item = m_toolbarTree->GetSelection();
838
839 if( !item.IsOk() )
840 return;
841
842 // The tree control defaults to nothing selected if you delete the item
843 // at the last place, so we have to manually get the itme immediately before
844 // the one we will delete, and then select it if nothing is selected.
845 wxTreeItemId prev = m_toolbarTree->GetPrevSibling( item );
846
847 m_toolbarTree->Delete( item );
848
849 item = m_toolbarTree->GetSelection();
850
851 if( !item.IsOk() && prev.IsOk() )
852 {
853 m_toolbarTree->SelectItem( prev );
854 m_toolbarTree->EnsureVisible( prev );
855 }
856}
857
858
859void PANEL_TOOLBAR_CUSTOMIZATION::onToolMoveUp( wxCommandEvent& event )
860{
861 m_toolbarTree->MoveItemUp( m_toolbarTree->GetSelection() );
862}
863
864
866{
867 m_toolbarTree->MoveItemDown( m_toolbarTree->GetSelection() );
868}
869
870
872{
873 // Get the selected item
874 long actionIdx = m_actionsList->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
875
876 // Nothing is selected, bail out
877 if( actionIdx < 0 )
878 return;
879
880 size_t entryIdx = m_actionsList->GetItemData( actionIdx );
881
882 if( entryIdx >= m_actionEntries.size() )
883 return;
884
885 const ACTION_LIST_ENTRY& entry = m_actionEntries[entryIdx];
886 TOOL_ACTION* action = entry.action;
887 ACTION_TOOLBAR_CONTROL* control = entry.control;
888
889 if( control )
890 {
891 removeControlFromOtherToolbars( control->GetName() );
892 removeControlFromCurrentTree( control->GetName() );
893 }
894
895 // Build the item to add
898 toolTreeItem->SetAction( action );
899 toolTreeItem->SetControl( control );
900
901 int imgIdx = -1;
902
903 if( action )
904 {
905 auto imgMap = m_actionImageListMap.find( action->GetName() );
906
907 if( imgMap != m_actionImageListMap.end() )
908 imgIdx = imgMap->second;
909 }
910
911 // Actually add the item
912 wxTreeItemId selItem = m_toolbarTree->GetSelection();
913 wxTreeItemId newItem;
914
915 if( selItem.IsOk() )
916 {
918 dynamic_cast<TOOLBAR_TREE_ITEM_DATA*>( m_toolbarTree->GetItemData( selItem ) );
919
920 if( data && data->GetType() == TOOLBAR_ITEM_TYPE::TB_GROUP )
921 {
922 // Insert into the end of the group
923 newItem = m_toolbarTree->AppendItem( selItem, entry.label, imgIdx, -1, toolTreeItem );
924 }
925 else
926 {
927 // Insert after the current selection at the same level
928 wxTreeItemId parent = m_toolbarTree->GetItemParent( selItem );
929 newItem = m_toolbarTree->InsertItem( parent, selItem, entry.label, imgIdx, -1, toolTreeItem );
930 }
931 }
932 else
933 {
934 // Insert at the root level if there is no selection
935 newItem = m_toolbarTree->AppendItem( m_toolbarTree->GetRootItem(), entry.label, imgIdx, -1, toolTreeItem );
936 }
937
938 if( newItem.IsOk() )
939 {
940 m_toolbarTree->SelectItem( newItem );
941 m_toolbarTree->EnsureVisible( newItem );
942
943 // Move the action to the next available one, to be nice
944 if( ++actionIdx < m_actionsList->GetItemCount() )
945 m_actionsList->SetItemState( actionIdx, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
946 }
947}
948
949
951{
953 aEvent.Skip();
954}
955
956
958{
959 aEvent.Skip();
960
961 if( aEvent.Leaving() )
962 {
964 m_actionsList->UnsetToolTip();
965 return;
966 }
967
968 int flags = 0;
969 long item = m_actionsList->HitTest( aEvent.GetPosition(), flags );
970
971 if( item >= 0 )
972 {
973 long entryIdx = static_cast<long>( m_actionsList->GetItemData( item ) );
974
975 if( entryIdx >= 0 && entryIdx < static_cast<long>( m_actionEntries.size() ) )
976 {
977 if( m_hoveredActionEntry != entryIdx )
978 {
979 m_hoveredActionEntry = entryIdx;
980
981 if( const wxString& tooltip = m_actionEntries[entryIdx].tooltip; tooltip.IsEmpty() )
982 m_actionsList->UnsetToolTip();
983 else
984 m_actionsList->SetToolTip( tooltip );
985 }
986
987 return;
988 }
989 }
990
991 if( m_hoveredActionEntry != -1 )
992 {
994 m_actionsList->UnsetToolTip();
995 }
996}
997
998
1000{
1001 wxTreeItemId id = event.GetItem();
1002
1003 if( id.IsOk() )
1004 {
1005 wxTreeItemData* treeData = m_toolbarTree->GetItemData( id );
1006
1007 if( TOOLBAR_TREE_ITEM_DATA* tbData = dynamic_cast<TOOLBAR_TREE_ITEM_DATA*>( treeData ) )
1008 {
1009 switch( tbData->GetType() )
1010 {
1014 // Don't let these be edited
1015 event.Veto();
1016 break;
1017
1020 // Do nothing here
1021 break;
1022 }
1023 }
1024 }
1025}
1026
1027
1029{
1030
1031}
1032
1033
1035{
1036 // Store the current toolbar
1037 std::optional<TOOLBAR_CONFIGURATION> currentTb = parseToolbarTree();
1038
1039 if( currentTb.has_value() )
1040 m_toolbars[m_currentToolbar] = currentTb.value();
1041
1042 int idx = event.GetInt();
1043
1044 if( idx >= 0 && idx < static_cast<int>( m_toolbarChoices.size() ) )
1045 {
1048 }
1049}
1050
1051
1053{
1054 wxCommandEvent dummy;
1056}
1057
1058
1060{
1061 for( auto& [loc, config] : m_toolbars )
1062 {
1063 if( loc == m_currentToolbar )
1064 continue;
1065
1066 auto& items = config.m_toolbarItems;
1067
1068 items.erase( std::remove_if( items.begin(), items.end(),
1069 [&]( const TOOLBAR_ITEM& item )
1070 {
1071 return item.m_Type == TOOLBAR_ITEM_TYPE::CONTROL
1072 && item.m_ControlName == aControlName;
1073 } ),
1074 items.end() );
1075 }
1076}
1077
1078
1080{
1081 wxTreeItemId rootId = m_toolbarTree->GetRootItem();
1082
1083 if( !rootId.IsOk() )
1084 return;
1085
1086 std::vector<wxTreeItemId> toDelete;
1087 wxTreeItemIdValue cookie;
1088
1089 for( wxTreeItemId id = m_toolbarTree->GetFirstChild( rootId, cookie );
1090 id.IsOk();
1091 id = m_toolbarTree->GetNextChild( rootId, cookie ) )
1092 {
1094 dynamic_cast<TOOLBAR_TREE_ITEM_DATA*>( m_toolbarTree->GetItemData( id ) );
1095
1096 if( data
1098 && data->GetControl()
1099 && data->GetControl()->GetName() == aControlName )
1100 {
1101 toDelete.push_back( id );
1102 }
1103 }
1104
1105 for( const wxTreeItemId& id : toDelete )
1106 m_toolbarTree->Delete( id );
1107}
const char * name
wxBitmapBundle KiBitmapBundleDef(BITMAPS aBitmap, int aDefHeight)
Constructs and returns a bitmap bundle for the given icon ID, with the default bitmap size being aDef...
Definition bitmap.cpp:116
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:110
@ INVALID_BITMAP
static TOOL_ACTION zoomRedraw
Definition actions.h:132
static TOOL_ACTION zoomOutCenter
Definition actions.h:136
static TOOL_ACTION zoomFitScreen
Definition actions.h:142
static TOOL_ACTION zoomInCenter
Definition actions.h:135
Class to hold basic information about controls that can be added to the toolbars.
const std::string & GetName() const
APP_SETTINGS_BASE is a settings class that should be derived for each standalone KiCad application.
PANEL_TOOLBAR_CUSTOMIZATION_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxTAB_TRAVERSAL, const wxString &name=wxEmptyString)
bool actionMatchesFilter(const ACTION_LIST_ENTRY &aEntry, const wxString &aFilter) const
void onSpacerPress(wxCommandEvent &aEvent)
wxVector< wxBitmapBundle > m_actionImageBundleVector
void onActionListMouseMove(wxMouseEvent &event)
void onTbChoiceSelect(wxCommandEvent &event) override
bool isActionSupported(const TOOL_ACTION &aAction) const
void onTreeEndLabelEdit(wxTreeEvent &event) override
void onTreeBeginLabelEdit(wxTreeEvent &event) override
void removeControlFromCurrentTree(const std::string &aControlName)
void onToolDelete(wxCommandEvent &event) override
std::map< TOOLBAR_LOC, TOOLBAR_CONFIGURATION > m_toolbars
void onGroupPress(wxCommandEvent &aEvent)
void onSeparatorPress(wxCommandEvent &aEvent)
void ResetPanel() override
Reset the contents of this panel.
std::vector< TOOLBAR_LOC > m_toolbarChoices
std::map< std::string, int > m_actionImageListMap
void onBtnAddAction(wxCommandEvent &event) override
void removeControlFromOtherToolbars(const std::string &aControlName)
std::map< std::string, TOOL_ACTION * > m_availableTools
std::map< std::string, ACTION_TOOLBAR_CONTROL * > m_availableControls
std::optional< TOOLBAR_CONFIGURATION > parseToolbarTree()
PANEL_TOOLBAR_CUSTOMIZATION(wxWindow *aParent, APP_SETTINGS_BASE *aCfg, TOOLBAR_SETTINGS *aTbSettings, FRAME_T aActionContext, const std::vector< TOOL_ACTION * > &aTools, const std::vector< ACTION_TOOLBAR_CONTROL * > &aControls)
void onCustomizeTbCb(wxCommandEvent &event) override
void onActionFilterText(wxCommandEvent &event)
void onToolMoveDown(wxCommandEvent &event) override
void onListItemActivated(wxListEvent &event) override
void onToolMoveUp(wxCommandEvent &event) override
std::vector< ACTION_LIST_ENTRY > m_actionEntries
std::vector< TOOLBAR_ITEM > GetToolbarItems() const
TOOLBAR_GROUP_CONFIG & AddAction(std::string aActionName)
std::vector< TOOLBAR_ITEM > m_GroupItems
std::string m_ActionName
void SetName(const wxString &aName)
void SetAction(TOOL_ACTION *aAction)
void SetControl(ACTION_TOOLBAR_CONTROL *aControl)
TOOLBAR_TREE_ITEM_DATA(TOOLBAR_ITEM_TYPE aType)
TOOLBAR_TREE_ITEM_DATA(TOOLBAR_ITEM_TYPE aType, int aSize)
TOOLBAR_TREE_ITEM_DATA(TOOLBAR_ITEM_TYPE aType, wxString aName)
TOOLBAR_ITEM_TYPE GetType() const
ACTION_TOOLBAR_CONTROL * GetControl() const
Represent a single user action.
const std::string & GetName() const
Return name of the action.
#define _(s)
FRAME_T
The set of EDA_BASE_FRAME derivatives, typically stored in EDA_BASE_FRAME::m_Ident.
Definition frame_type.h:33
@ FRAME_PCB_EDITOR
Definition frame_type.h:42
@ FRAME_SCH_SYMBOL_EDITOR
Definition frame_type.h:35
@ FRAME_FOOTPRINT_VIEWER
Definition frame_type.h:45
@ FRAME_SCH_VIEWER
Definition frame_type.h:36
@ FRAME_SCH
Definition frame_type.h:34
@ FRAME_SIMULATOR
Definition frame_type.h:38
@ FRAME_PL_EDITOR
Definition frame_type.h:59
@ FRAME_FOOTPRINT_EDITOR
Definition frame_type.h:43
@ FRAME_GERBER
Definition frame_type.h:57
@ FRAME_PCB_DISPLAY3D
Definition frame_type.h:47
KICOMMON_API wxFont GetInfoFont(wxWindow *aWindow)
static std::map< TOOLBAR_LOC, wxString > s_toolbarNameMap
std::vector< FAB_LAYER_COLOR > dummy
TOOL_ACTION * action
wxString label
ACTION_TOOLBAR_CONTROL * control
int image_index
wxString tooltip
wxString search_text
@ HIDDEN
Action is hidden from the toolbar.
Definition tool_action.h:63
@ RIGHT
Toolbar on the right side of the canvas.
@ LEFT
Toolbar on the left side of the canvas.
@ TOP_AUX
Toolbar on the top of the canvas.
@ TOP_MAIN
Toolbar on the top of the canvas.
Functions to provide common constants and other functions to assist in making a consistent UI.
#define SEPARATOR