KiCad PCB EDA Suite
action_toolbar.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 (C) 2019 CERN
5  * Copyright (C) 2019-2021 KiCad Developers, see CHANGELOG.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 
25 #include <algorithm>
26 #include <advanced_config.h>
27 #include <bitmaps.h>
28 #include <eda_draw_frame.h>
29 #include <functional>
30 #include <kiplatform/ui.h>
31 #include <math/util.h>
32 #include <memory>
33 #include <pgm_base.h>
35 #include <tool/action_toolbar.h>
36 #include <tool/actions.h>
37 #include <tool/tool_action.h>
38 #include <tool/tool_event.h>
39 #include <tool/tool_interactive.h>
40 #include <tool/tool_manager.h>
41 #include <widgets/bitmap_button.h>
43 #include <wx/popupwin.h>
44 #include <wx/renderer.h>
45 #include <wx/sizer.h>
46 #include <wx/dcclient.h>
47 #include <wx/settings.h>
48 
49 
50 wxBitmap MakeDisabledBitmap( const wxBitmap& aSource )
51 {
52  return aSource.ConvertToDisabled( KIPLATFORM::UI::IsDarkTheme() ? 70 : 255 );
53 }
54 
55 
56 ACTION_GROUP::ACTION_GROUP( const std::string& aName,
57  const std::vector<const TOOL_ACTION*>& aActions )
58 {
59  wxASSERT_MSG( aActions.size() > 0, "Action groups must have at least one action" );
60 
61  // The default action is just the first action in the vector
62  m_actions = aActions;
64 
65  m_name = aName;
67 }
68 
69 
71 {
72  return m_id + TOOL_ACTION::GetBaseUIId();
73 }
74 
75 
77 {
78  bool valid = std::any_of( m_actions.begin(), m_actions.end(),
79  [&]( const TOOL_ACTION* aAction ) -> bool
80  {
81  // For some reason, we can't compare the actions directly
82  return aAction->GetId() == aDefault.GetId();
83  } );
84 
85  wxASSERT_MSG( valid, "Action must be present in a group to be the default" );
86 
87  m_defaultAction = &aDefault;
88 }
89 
90 
91 #define PALETTE_BORDER 4 // The border around the palette buttons on all sides
92 #define BUTTON_BORDER 1 // The border on the sides of the buttons that touch other buttons
93 
94 
95 ACTION_TOOLBAR_PALETTE::ACTION_TOOLBAR_PALETTE( wxWindow* aParent, bool aVertical ) :
96  wxPopupTransientWindow( aParent, wxBORDER_NONE ),
97  m_group( nullptr ),
98  m_isVertical( aVertical ),
99  m_panel( nullptr ),
100  m_mainSizer( nullptr ),
101  m_buttonSizer( nullptr )
102 {
103  m_panel = new wxPanel( this, wxID_ANY );
104  m_panel->SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW ) );
105 
106  // This sizer holds the buttons for the actions
107  m_buttonSizer = new wxBoxSizer( aVertical ? wxVERTICAL : wxHORIZONTAL );
108 
109  // This sizer holds the other sizer, so that a consistent border is present on all sides
110  m_mainSizer = new wxBoxSizer( aVertical ? wxVERTICAL : wxHORIZONTAL );
111  m_mainSizer->Add( m_buttonSizer, wxSizerFlags().Border( wxALL, PALETTE_BORDER ) );
112 
113  m_panel->SetSizer( m_mainSizer );
114 
115  Connect( wxEVT_CHAR_HOOK, wxCharEventHandler( ACTION_TOOLBAR_PALETTE::onCharHook ),
116  nullptr, this );
117 }
118 
119 
121 {
122  wxBitmap normalBmp = KiScaledBitmap( aAction.GetIcon(), this );
123  wxBitmap disabledBmp = MakeDisabledBitmap( normalBmp );
124 
125  int padding = ( m_buttonSize.GetWidth() - normalBmp.GetWidth() ) / 2;
126 
127  BITMAP_BUTTON* button = new BITMAP_BUTTON( m_panel, aAction.GetUIId() );
128 
129  button->SetBitmap( normalBmp );
130  button->SetDisabledBitmap( disabledBmp );
131  button->SetPadding( padding );
132  button->SetToolTip( aAction.GetDescription() );
133  button->AcceptDragInAsClick();
134 
135  m_buttons[aAction.GetUIId()] = button;
136 
137  if( m_isVertical )
138  m_buttonSizer->Add( button, wxSizerFlags().Border( wxTOP | wxBOTTOM, BUTTON_BORDER ) );
139  else
140  m_buttonSizer->Add( button, wxSizerFlags().Border( wxLEFT | wxRIGHT, BUTTON_BORDER ) );
141 
142  m_buttonSizer->Layout();
143 }
144 
145 
146 void ACTION_TOOLBAR_PALETTE::EnableAction( const TOOL_ACTION& aAction, bool aEnable )
147 {
148  auto it = m_buttons.find( aAction.GetUIId() );
149 
150  if( it != m_buttons.end() )
151  it->second->Enable( aEnable );
152 }
153 
154 
155 void ACTION_TOOLBAR_PALETTE::CheckAction( const TOOL_ACTION& aAction, bool aCheck )
156 {
157  auto it = m_buttons.find( aAction.GetUIId() );
158 
159  if( it != m_buttons.end() )
160  it->second->Check( aCheck );
161 }
162 
163 
164 void ACTION_TOOLBAR_PALETTE::Popup( wxWindow* aFocus )
165 {
166  m_mainSizer->Fit( m_panel );
167  SetClientSize( m_panel->GetSize() );
168 
169  wxPopupTransientWindow::Popup( aFocus );
170 }
171 
172 
173 void ACTION_TOOLBAR_PALETTE::onCharHook( wxKeyEvent& aEvent )
174 {
175  // Allow the escape key to dismiss this popup
176  if( aEvent.GetKeyCode() == WXK_ESCAPE )
177  Dismiss();
178  else
179  aEvent.Skip();
180 }
181 
182 
183 ACTION_TOOLBAR::ACTION_TOOLBAR( EDA_BASE_FRAME* parent, wxWindowID id, const wxPoint& pos,
184  const wxSize& size, long style ) :
185  wxAuiToolBar( parent, id, pos, size, style ),
186  m_paletteTimer( nullptr ),
187  m_auiManager( nullptr ),
188  m_toolManager( parent->GetToolManager() ),
189  m_palette( nullptr )
190 {
191  m_paletteTimer = new wxTimer( this );
192 
193 #if !wxCHECK_VERSION( 3, 1, 0 )
194  // Custom art provider makes dark mode work on wx < 3.1
195  WX_AUI_TOOLBAR_ART* newArt = new WX_AUI_TOOLBAR_ART();
196  SetArtProvider( newArt );
197 #endif
198 
199  Connect( wxEVT_COMMAND_TOOL_CLICKED, wxAuiToolBarEventHandler( ACTION_TOOLBAR::onToolEvent ),
200  nullptr, this );
201  Connect( wxEVT_AUITOOLBAR_RIGHT_CLICK,
202  wxAuiToolBarEventHandler( ACTION_TOOLBAR::onToolRightClick ),
203  nullptr, this );
204  Connect( wxEVT_AUITOOLBAR_BEGIN_DRAG, wxAuiToolBarEventHandler( ACTION_TOOLBAR::onItemDrag ),
205  nullptr, this );
206  Connect( wxEVT_LEFT_DOWN, wxMouseEventHandler( ACTION_TOOLBAR::onMouseClick ), nullptr, this );
207  Connect( wxEVT_LEFT_UP, wxMouseEventHandler( ACTION_TOOLBAR::onMouseClick ), nullptr, this );
208  Connect( m_paletteTimer->GetId(), wxEVT_TIMER,
209  wxTimerEventHandler( ACTION_TOOLBAR::onTimerDone ), nullptr, this );
210 }
211 
212 
214 {
215  delete m_paletteTimer;
216 
217  // Clear all the maps keeping track of our items on the toolbar
218  m_toolMenus.clear();
219  m_actionGroups.clear();
220  m_toolCancellable.clear();
221  m_toolKinds.clear();
222  m_toolActions.clear();
223 }
224 
225 
226 void ACTION_TOOLBAR::Add( const TOOL_ACTION& aAction, bool aIsToggleEntry, bool aIsCancellable )
227 {
228  wxASSERT( GetParent() );
229  wxASSERT_MSG( !( aIsCancellable && !aIsToggleEntry ), "aIsCancellable requires aIsToggleEntry" );
230 
231  int toolId = aAction.GetUIId();
232  wxBitmap bmp = KiScaledBitmap( aAction.GetIcon(), GetParent() );
233 
234  AddTool( toolId, wxEmptyString, bmp, MakeDisabledBitmap( bmp ),
235  aIsToggleEntry ? wxITEM_CHECK : wxITEM_NORMAL,
236  aAction.GetDescription(), wxEmptyString, nullptr );
237 
238  m_toolKinds[ toolId ] = aIsToggleEntry;
239  m_toolActions[ toolId ] = &aAction;
240  m_toolCancellable[ toolId ] = aIsCancellable;
241 }
242 
243 
245 {
246  int toolId = aAction.GetUIId();
247  wxBitmap bmp = KiScaledBitmap( aAction.GetIcon(), GetParent() );
248 
249  AddTool( toolId, wxEmptyString, bmp, MakeDisabledBitmap( bmp ),
250  wxITEM_NORMAL, aAction.GetDescription(), wxEmptyString, nullptr );
251 
252  m_toolKinds[ toolId ] = false;
253  m_toolActions[ toolId ] = &aAction;
254 }
255 
256 
257 void ACTION_TOOLBAR::AddScaledSeparator( wxWindow* aWindow )
258 {
259  int scale = Pgm().GetCommonSettings()->m_Appearance.icon_scale;
260 
261  if( scale == 0 )
262  scale = KiIconScale( aWindow );
263 
264  if( scale > 4 )
265  AddSpacer( 16 * ( scale - 4 ) / 4 );
266 
267  AddSeparator();
268 
269  if( scale > 4 )
270  AddSpacer( 16 * ( scale - 4 ) / 4 );
271 }
272 
273 
275  std::unique_ptr<ACTION_MENU> aMenu )
276 {
277  int toolId = aAction.GetUIId();
278 
279  m_toolMenus[toolId] = std::move( aMenu );
280 }
281 
282 
283 void ACTION_TOOLBAR::AddGroup( ACTION_GROUP* aGroup, bool aIsToggleEntry )
284 {
285  int groupId = aGroup->GetUIId();
286  const TOOL_ACTION* defaultAction = aGroup->GetDefaultAction();
287  wxBitmap bmp = KiScaledBitmap( defaultAction->GetIcon(), GetParent() );
288 
289  wxASSERT( GetParent() );
290  wxASSERT( defaultAction );
291 
292  m_toolKinds[ groupId ] = aIsToggleEntry;
293  m_toolActions[ groupId ] = defaultAction;
294  m_actionGroups[ groupId ] = aGroup;
295 
296  // Add the main toolbar item representing the group
297  AddTool( groupId, wxEmptyString, bmp, MakeDisabledBitmap( bmp ),
298  aIsToggleEntry ? wxITEM_CHECK : wxITEM_NORMAL,
299  wxEmptyString, wxEmptyString, nullptr );
300 
301  // Select the default action
302  doSelectAction( aGroup, *defaultAction );
303 }
304 
305 
307 {
308  bool valid = std::any_of( aGroup->m_actions.begin(), aGroup->m_actions.end(),
309  [&]( const TOOL_ACTION* action2 ) -> bool
310  {
311  // For some reason, we can't compare the actions directly
312  return aAction.GetId() == action2->GetId();
313  } );
314 
315  if( valid )
316  doSelectAction( aGroup, aAction );
317 }
318 
319 
321 {
322  wxASSERT( GetParent() );
323 
324  int groupId = aGroup->GetUIId();
325 
326  wxAuiToolBarItem* item = FindTool( groupId );
327 
328  if( !item )
329  return;
330 
331  // Update the item information
332  item->SetShortHelp( aAction.GetDescription() );
333  item->SetBitmap( KiScaledBitmap( aAction.GetIcon(), GetParent() ) );
334  item->SetDisabledBitmap( MakeDisabledBitmap( item->GetBitmap() ) );
335 
336  // Register a new handler with the new UI conditions
337  if( m_toolManager )
338  {
339  const ACTION_CONDITIONS* cond = m_toolManager->GetActionManager()->GetCondition( aAction );
340 
341  wxASSERT_MSG( cond, wxString::Format( "Missing UI condition for action %s",
342  aAction.GetName() ) );
343 
346  }
347 
348  // Update the currently selected action
349  m_toolActions[ groupId ] = &aAction;
350 
351  Refresh();
352 }
353 
354 
356 {
357  wxAuiToolBarItem* item = FindTool( aID );
358  wxASSERT_MSG( item, wxString::Format( "No toolbar item found for ID %d", aID ) );
359 
360  // The control on the toolbar is stored inside the window field of the item
361  wxControl* control = dynamic_cast<wxControl*>( item->GetWindow() );
362  wxASSERT_MSG( control, wxString::Format( "No control located in toolbar item with ID %d", aID ) );
363 
364  // Update the size the item has stored using the best size of the control
365  wxSize bestSize = control->GetBestSize();
366  item->SetMinSize( bestSize );
367 
368  // Update the sizer item sizes
369  // This is a bit convoluted because there are actually 2 sizers that need to be updated:
370  // 1. The main sizer that is used for the entire toolbar (this sizer item can be found in the
371  // toolbar item)
372  if( wxSizerItem* szrItem = item->GetSizerItem() )
373  szrItem->SetMinSize( bestSize );
374 
375  // 2. The controls have a second sizer that allows for padding above/below the control with
376  // stretch space, so we also need to update the sizer item for the control in that sizer with
377  // the new size. We let wx do the search for us, since SetItemMinSize is recursive and will
378  // locate the control on that sizer.
379  if( m_sizer )
380  {
381  m_sizer->SetItemMinSize( control, bestSize );
382 
383  // Now actually update the toolbar with the new sizes
384  m_sizer->Layout();
385  }
386 }
387 
388 
390 {
391  // Clear all the maps keeping track of our items on the toolbar
392  m_toolMenus.clear();
393  m_actionGroups.clear();
394  m_toolCancellable.clear();
395  m_toolKinds.clear();
396  m_toolActions.clear();
397 
398  // Remove the actual tools from the toolbar
399  Clear();
400 }
401 
402 
403 void ACTION_TOOLBAR::SetToolBitmap( const TOOL_ACTION& aAction, const wxBitmap& aBitmap )
404 {
405  int toolId = aAction.GetUIId();
406  wxAuiToolBar::SetToolBitmap( toolId, aBitmap );
407 
408  // Set the disabled bitmap: we use the disabled bitmap version of aBitmap.
409  wxAuiToolBarItem* tb_item = wxAuiToolBar::FindTool( toolId );
410 
411  if( tb_item )
412  tb_item->SetDisabledBitmap( MakeDisabledBitmap( aBitmap ) );
413 }
414 
415 
416 void ACTION_TOOLBAR::Toggle( const TOOL_ACTION& aAction, bool aState )
417 {
418  int toolId = aAction.GetUIId();
419 
420  if( m_toolKinds[ toolId ] )
421  ToggleTool( toolId, aState );
422  else
423  EnableTool( toolId, aState );
424 }
425 
426 
427 void ACTION_TOOLBAR::Toggle( const TOOL_ACTION& aAction, bool aEnabled, bool aChecked )
428 {
429  int toolId = aAction.GetUIId();
430 
431  EnableTool( toolId, aEnabled );
432  ToggleTool( toolId, aEnabled && aChecked );
433 }
434 
435 
436 void ACTION_TOOLBAR::onToolEvent( wxAuiToolBarEvent& aEvent )
437 {
438  int id = aEvent.GetId();
439  wxEventType type = aEvent.GetEventType();
440  OPT_TOOL_EVENT evt;
441 
442  bool handled = false;
443 
444  if( m_toolManager && type == wxEVT_COMMAND_TOOL_CLICKED && id >= TOOL_ACTION::GetBaseUIId() )
445  {
446  const auto actionIt = m_toolActions.find( id );
447 
448  // The toolbar item is toggled before the event is sent, so we check for it not being
449  // toggled to see if it was toggled originally
450  if( m_toolCancellable[id] && !GetToolToggled( id ) )
451  {
452  // Send a cancel event
454  handled = true;
455  }
456  else if( actionIt != m_toolActions.end() )
457  {
458  // Dispatch a tool event
459  evt = actionIt->second->MakeEvent();
460  evt->SetHasPosition( false );
461  m_toolManager->ProcessEvent( *evt );
463  handled = true;
464  }
465  }
466 
467  // Skip the event if we don't handle it
468  if( !handled )
469  aEvent.Skip();
470 }
471 
472 
473 void ACTION_TOOLBAR::onToolRightClick( wxAuiToolBarEvent& aEvent )
474 {
475  int toolId = aEvent.GetToolId();
476 
477  // This means the event was not on a button
478  if( toolId == -1 )
479  return;
480 
481  // Ensure that the ID used maps to a proper tool ID.
482  // If right-clicked on a group item, this is needed to get the ID of the currently selected
483  // action, since the event's ID is that of the group.
484  const auto actionIt = m_toolActions.find( toolId );
485 
486  if( actionIt != m_toolActions.end() )
487  toolId = actionIt->second->GetUIId();
488 
489  // Find the menu for the action
490  const auto menuIt = m_toolMenus.find( toolId );
491 
492  if( menuIt == m_toolMenus.end() )
493  return;
494 
495  // Update and show the menu
496  std::unique_ptr<ACTION_MENU>& owningMenu = menuIt->second;
497 
498  // Get the actual menu pointer to show it
499  ACTION_MENU* menu = owningMenu.get();
500  SELECTION dummySel;
501 
502  if( CONDITIONAL_MENU* condMenu = dynamic_cast<CONDITIONAL_MENU*>( menu ) )
503  condMenu->Evaluate( dummySel );
504 
505  menu->UpdateAll();
506  PopupMenu( menu );
507 
508  // Remove hovered item when the menu closes, otherwise it remains hovered even if the
509  // mouse is not on the toolbar
510  SetHoverItem( nullptr );
511 }
512 
513 // The time (in milliseconds) between pressing the left mouse button and opening the palette
514 #define PALETTE_OPEN_DELAY 500
515 
516 
517 void ACTION_TOOLBAR::onMouseClick( wxMouseEvent& aEvent )
518 {
519  wxAuiToolBarItem* item = FindToolByPosition( aEvent.GetX(), aEvent.GetY() );
520 
521  if( item )
522  {
523  // Ensure there is no active palette
524  if( m_palette )
525  {
526  m_palette->Hide();
527  m_palette->Destroy();
528  m_palette = nullptr;
529  }
530 
531  // Start the popup conditions if it is a left mouse click and the tool clicked is a group
532  if( aEvent.LeftDown() && ( m_actionGroups.find( item->GetId() ) != m_actionGroups.end() ) )
533  m_paletteTimer->StartOnce( PALETTE_OPEN_DELAY );
534 
535  // Clear the popup conditions if it is a left up, because that implies a click happened
536  if( aEvent.LeftUp() )
537  m_paletteTimer->Stop();
538  }
539 
540  // Skip the event so wx can continue processing the mouse event
541  aEvent.Skip();
542 }
543 
544 
545 void ACTION_TOOLBAR::onItemDrag( wxAuiToolBarEvent& aEvent )
546 {
547  int toolId = aEvent.GetToolId();
548 
549  if( m_actionGroups.find( toolId ) != m_actionGroups.end() )
550  {
551  wxAuiToolBarItem* item = FindTool( toolId );
552 
553  // Use call after because opening the palette from a mouse handler
554  // creates a weird mouse state that causes problems on OSX.
555  CallAfter( &ACTION_TOOLBAR::popupPalette, item );
556 
557  // Don't skip this event since we are handling it
558  return;
559  }
560 
561  // Skip since we don't care about it
562  aEvent.Skip();
563 }
564 
565 
566 void ACTION_TOOLBAR::onTimerDone( wxTimerEvent& aEvent )
567 {
568  // We need to search for the tool using the client coordinates
569  wxPoint mousePos = ScreenToClient( wxGetMousePosition() );
570 
571  wxAuiToolBarItem* item = FindToolByPosition( mousePos.x, mousePos.y );
572 
573  if( item )
574  popupPalette( item );
575 }
576 
577 
578 void ACTION_TOOLBAR::onPaletteEvent( wxCommandEvent& aEvent )
579 {
580  if( !m_palette )
581  return;
582 
583  OPT_TOOL_EVENT evt;
585 
586  // Find the action corresponding to the button press
587  auto actionIt = std::find_if( group->GetActions().begin(), group->GetActions().end(),
588  [=]( const TOOL_ACTION* aAction )
589  {
590  return aAction->GetUIId() == aEvent.GetId();
591  } );
592 
593  if( actionIt != group->GetActions().end() )
594  {
595  const TOOL_ACTION* action = *actionIt;
596 
597  // Dispatch a tool event
598  evt = action->MakeEvent();
599  evt->SetHasPosition( false );
600  m_toolManager->ProcessEvent( *evt );
602 
603  // Update the main toolbar item with the selected action
604  doSelectAction( group, *action );
605  }
606 
607  // Hide the palette
608  m_palette->Hide();
609  m_palette->Destroy();
610  m_palette = nullptr;
611 }
612 
613 
614 void ACTION_TOOLBAR::popupPalette( wxAuiToolBarItem* aItem )
615 {
616  // Clear all popup conditions
617  m_paletteTimer->Stop();
618 
619  wxWindow* toolParent = dynamic_cast<wxWindow*>( m_toolManager->GetToolHolder() );
620 
621  wxASSERT( GetParent() );
622  wxASSERT( m_auiManager );
623  wxASSERT( toolParent );
624 
625  // Ensure the item we are using for the palette has a group associated with it.
626  const auto it = m_actionGroups.find( aItem->GetId() );
627 
628  if( it == m_actionGroups.end() )
629  return;
630 
631  ACTION_GROUP* group = it->second;
632 
633  wxAuiPaneInfo& pane = m_auiManager->GetPane( this );
634 
635  // We use the size of the toolbar items for our palette buttons
636  wxRect toolRect = GetToolRect( aItem->GetId() );
637 
638  // The position for the palette window must be in screen coordinates
639  wxPoint pos( ClientToScreen( toolRect.GetPosition() ) );
640 
641  // True for vertical buttons, false for horizontal
642  bool dir = true;
643  size_t numActions = group->m_actions.size();
644 
645  // The size of the palette in the long dimension
646  int paletteLongDim = ( 2 * PALETTE_BORDER ) // The border on all sides of the buttons
647  + ( BUTTON_BORDER ) // The border on the start of the buttons
648  + ( numActions * BUTTON_BORDER ) // The other button borders
649  + ( numActions * toolRect.GetHeight() ); // The size of the buttons
650 
651  // Determine the position of the top left corner of the palette window
652  switch( pane.dock_direction )
653  {
654  case wxAUI_DOCK_TOP:
655  // Top toolbars need to shift the palette window down by the toolbar padding
656  dir = true; // Buttons are vertical in the palette
657  pos = ClientToScreen( toolRect.GetBottomLeft() );
658  pos += wxPoint( -PALETTE_BORDER, // Shift left to align the button edges
659  m_bottomPadding ); // Shift down to move away from the toolbar
660  break;
661 
662  case wxAUI_DOCK_BOTTOM:
663  // Bottom toolbars need to shift the palette window up by its height (all buttons +
664  // border + toolbar padding)
665  dir = true; // Buttons are vertical in the palette
666  pos = ClientToScreen( toolRect.GetTopLeft() );
667  pos += wxPoint( -PALETTE_BORDER, // Shift left to align the button
668  // Shift up by the entire length of the palette.
669  -( paletteLongDim + m_topPadding ) );
670  break;
671 
672  case wxAUI_DOCK_LEFT:
673  // Left toolbars need to shift the palette window up by the toolbar padding
674  dir = false; // Buttons are horizontal in the palette
675  pos = ClientToScreen( toolRect.GetTopRight() );
676  pos += wxPoint( m_rightPadding, // Shift right to move away from the toolbar
677  -( PALETTE_BORDER ) ); // Shift up to align the button tops
678  break;
679 
680  case wxAUI_DOCK_RIGHT:
681  // Right toolbars need to shift the palette window left by its width (all buttons +
682  // border + toolbar padding)
683  dir = false; // Buttons are horizontal in the palette
684  pos = ClientToScreen( toolRect.GetTopLeft() );
685 
686  // Shift left by the entire length of the palette.
687  pos += wxPoint( -( paletteLongDim + m_leftPadding ),
688  -( PALETTE_BORDER ) ); // Shift up to align the button
689  break;
690  }
691 
692  m_palette = new ACTION_TOOLBAR_PALETTE( GetParent(), dir );
693 
694  // We handle the button events in the toolbar class, so connect the right handler
696  m_palette->SetButtonSize( toolRect );
697  m_palette->Connect( wxEVT_BUTTON, wxCommandEventHandler( ACTION_TOOLBAR::onPaletteEvent ),
698  nullptr, this );
699 
700 
701  // Add the actions in the group to the palette and update their enabled state
702  // We purposely don't check items in the palette
703  for( const TOOL_ACTION* action : group->m_actions )
704  {
705  wxUpdateUIEvent evt( action->GetUIId() );
706 
707  toolParent->ProcessWindowEvent( evt );
708 
709  m_palette->AddAction( *action );
710 
711  if( evt.GetSetEnabled() )
712  m_palette->EnableAction( *action, evt.GetEnabled() );
713  }
714 
715  // Release the mouse to ensure the first click will be recognized in the palette
716  ReleaseMouse();
717 
718  m_palette->SetPosition( pos );
719  m_palette->Popup();
720 
721  // Clear the mouse state on the toolbar because otherwise wxWidgets gets confused
722  // and won't properly display any highlighted items after the palette is closed.
723  // (This is the equivalent of calling the DoResetMouseState() private function)
724  RefreshOverflowState();
725  SetHoverItem( nullptr );
726  SetPressedItem( nullptr );
727 
728  m_dragging = false;
729  m_tipItem = nullptr;
730  m_actionPos = wxPoint( -1, -1 );
731  m_actionItem = nullptr;
732 }
733 
734 
735 void ACTION_TOOLBAR::OnCustomRender(wxDC& aDc, const wxAuiToolBarItem& aItem, const wxRect& aRect )
736 {
737  auto it = m_actionGroups.find( aItem.GetId() );
738 
739  if( it == m_actionGroups.end() )
740  return;
741 
742  // Choose the color to draw the triangle
743  wxColour clr;
744 
745  if( aItem.GetState() & wxAUI_BUTTON_STATE_DISABLED )
746  clr = wxSystemSettings::GetColour( wxSYS_COLOUR_GRAYTEXT );
747  else
748  clr = wxSystemSettings::GetColour( wxSYS_COLOUR_BTNTEXT );
749 
750  // Must set both the pen (for the outline) and the brush (for the polygon fill)
751  aDc.SetPen( wxPen( clr ) );
752  aDc.SetBrush( wxBrush( clr ) );
753 
754  // Make the side length of the triangle approximately 1/5th of the bitmap
755  int sideLength = KiROUND( aRect.height / 5.0 );
756 
757  // This will create a triangle with its point at the bottom right corner,
758  // and its other two corners along the right and bottom sides
759  wxPoint btmRight = aRect.GetBottomRight();
760  wxPoint topCorner( btmRight.x, btmRight.y - sideLength );
761  wxPoint btmCorner( btmRight.x - sideLength, btmRight.y );
762 
763  wxPointList points;
764  points.Append( &btmRight );
765  points.Append( &topCorner );
766  points.Append( &btmCorner );
767 
768  aDc.DrawPolygon( &points );
769 }
770 
771 
773 {
774  wxClientDC dc( this );
775 
776  if( !dc.IsOk() )
777  return false;
778 
779  // calculate hint sizes for both horizontal and vertical
780  // in the order that leaves toolbar in correct final state
781 
782  // however, skip calculating alternate orientations if we don't need them due to window style
783  bool retval = true;
784 
785  if( m_orientation == wxHORIZONTAL )
786  {
787  if( !( GetWindowStyle() & wxAUI_TB_HORIZONTAL ) )
788  {
789  m_vertHintSize = GetSize();
790  retval = RealizeHelper( dc, false );
791  }
792 
793  if( retval && RealizeHelper( dc, true ) )
794  {
795  m_horzHintSize = GetSize();
796  }
797  else
798  {
799  retval = false;
800  }
801  }
802  else
803  {
804  if( !( GetWindowStyle() & wxAUI_TB_VERTICAL ) )
805  {
806  m_horzHintSize = GetSize();
807  retval = RealizeHelper( dc, true );
808  }
809 
810  if( retval && RealizeHelper( dc, false ) )
811  {
812  m_vertHintSize = GetSize();
813  }
814  else
815  {
816  retval = false;
817  }
818  }
819 
820  Refresh( false );
821  return retval;
822 }
823 
824 
826 {
827  for( const std::pair<int, const TOOL_ACTION*> pair : m_toolActions )
828  {
829  wxAuiToolBarItem* tool = FindTool( pair.first );
830 
831  wxBitmap bmp = KiScaledBitmap( pair.second->GetIcon(), GetParent() );
832 
833  tool->SetBitmap( bmp );
834  tool->SetDisabledBitmap( MakeDisabledBitmap( bmp ) );
835  }
836 
837  Refresh();
838 }
wxBitmap MakeDisabledBitmap(const wxBitmap &aSource)
const TOOL_ACTION * m_defaultAction
The actions that compose the group.
A bitmap button widget that behaves like an AUI toolbar item's button when it is drawn.
Definition: bitmap_button.h:39
void onItemDrag(wxAuiToolBarEvent &aEvent)
The default tool event handler.
void CheckAction(const TOOL_ACTION &aAction, bool aCheck=true)
Check/Toggle the button for an action on the palette.
static int MakeActionId(const std::string &aActionName)
Generate an unique ID from for an action with given name.
int GetUIId() const
Definition: tool_action.h:130
ACTION_GROUP * GetGroup()
Defines the structure of a menu based on ACTIONs.
Definition: action_menu.h:48
void SetButtonSize(wxRect &aSize)
Set the size all the buttons on this palette should be.
void AddToolContextMenu(const TOOL_ACTION &aAction, std::unique_ptr< ACTION_MENU > aMenu)
Add a context menu to a specific tool item on the toolbar.
virtual void RefreshCanvas()
Definition: tools_holder.h:151
void AddScaledSeparator(wxWindow *aWindow)
Add a separator that introduces space on either side to not squash the tools when scaled.
void popupPalette(wxAuiToolBarItem *aItem)
Popup the ACTION_TOOLBAR_PALETTE associated with the ACTION_GROUP of the given toolbar item.
void Add(const TOOL_ACTION &aAction, bool aIsToggleEntry=false, bool aIsCancellable=false)
Add a TOOL_ACTION-based button to the toolbar.
int KiIconScale(wxWindow *aWindow)
Return the automatic scale factor that would be used for a given window by KiScaledBitmap and KiScale...
Definition: bitmap.cpp:122
#define PALETTE_OPEN_DELAY
void onToolRightClick(wxAuiToolBarEvent &aEvent)
Handle the button select inside the palette.
virtual void UnregisterUIUpdateHandler(const TOOL_ACTION &aAction)
Unregister a UI handler for an action that was registered using RegisterUIUpdateHandler.
Definition: tools_holder.h:83
void onTimerDone(wxTimerEvent &aEvent)
Render the triangle in the lower-right corner that represents that an action palette is available for...
bool IsDarkTheme()
Determine if the desktop interface is currently using a dark theme or a light theme.
Definition: gtk/ui.cpp:31
void AddGroup(ACTION_GROUP *aGroup, bool aIsToggleEntry=false)
Add a set of actions to a toolbar as a group.
void UpdateAll()
Run update handlers for the menu and its submenus.
ACTION_TOOLBAR_PALETTE(wxWindow *aParent, bool aVertical)
Create the palette.
void onToolEvent(wxAuiToolBarEvent &aEvent)
Handle a right-click on a menu item.
std::map< int, const TOOL_ACTION * > m_toolActions
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:106
bool KiRealize()
Use this over Realize() to avoid a rendering glitch with fixed orientation toolbars.
static int GetBaseUIId()
Definition: tool_action.h:135
void SetDefaultAction(const TOOL_ACTION &aDefault)
Set the default action to use when first creating the toolbar palette icon.
void onMouseClick(wxMouseEvent &aEvent)
Handler for when a drag event occurs on an item.
A group of actions that will be displayed together on a toolbar palette.
ACTION_GROUP(const std::string &aName, const std::vector< const TOOL_ACTION * > &aActions)
void AddAction(const TOOL_ACTION &aAction)
Add an action to the palette.
void AddButton(const TOOL_ACTION &aAction)
Add a large button such as used in the KiCad Manager Frame's launch bar.
wxWidgets 3.1 has support for dark mode detection, but 3.0 doesn't.
void Refresh()
Update the board display after modifying it by a python script (note: it is automatically called by a...
void doSelectAction(ACTION_GROUP *aGroup, const TOOL_ACTION &aAction)
Update a group toolbar item to look like a specific action.
bool ProcessEvent(const TOOL_EVENT &aEvent)
Propagate an event to tools that requested events of matching type(s).
std::map< int, bool > m_toolCancellable
void UpdateControlWidth(int aID)
Update the toolbar item width of a control using its best size.
void SetGroup(ACTION_GROUP *aGroup)
Set the action group that this palette contains the actions for.
void SetPadding(int aPadding)
Set the amount of padding present on each side of the bitmap.
const BITMAPS GetIcon() const
Return an icon associated with the action.
Definition: tool_action.h:179
void OnCustomRender(wxDC &aDc, const wxAuiToolBarItem &aItem, const wxRect &aRect) override
ACTION_TOOLBAR_PALETTE * m_palette
wxString GetDescription(bool aIncludeHotkey=true) const
Definition: tool_action.cpp:97
std::vector< const TOOL_ACTION * > m_actions
ACTION_TOOLBAR(EDA_BASE_FRAME *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxAUI_TB_DEFAULT_STYLE)
std::map< int, std::unique_ptr< ACTION_MENU > > m_toolMenus
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
wxRect m_buttonSize
True if the palette uses vertical buttons, false for horizontal buttons.
TOOLS_HOLDER * GetToolHolder() const
Definition: tool_manager.h:294
wxAuiManager * m_auiManager
void RefreshBitmaps()
Reload all the bitmaps for the tools (e.g.
const TOOL_ACTION * GetDefaultAction() const
Get the default action to use when first creating this group's toolbar palette icon.
TOOL_MANAGER * m_toolManager
int GetId() const
Return the unique id of the TOOL_ACTION object.
Definition: tool_action.h:121
const int scale
see class PGM_BASE
std::map< int, ACTION_GROUP * > m_actionGroups
void SetBitmap(const wxBitmap &aBmp)
Set the bitmap shown when the button is enabled.
Functors that can be used to figure out how the action controls should be displayed in the UI and if ...
std::map< int, BITMAP_BUTTON * > m_buttons
The base frame for deriving all KiCad main window classes.
Represent a single user action.
Definition: tool_action.h:67
#define PALETTE_BORDER
void SetToolBitmap(const TOOL_ACTION &aAction, const wxBitmap &aBitmap)
Updates the bitmap of a particular tool.
TOOL_EVENT MakeEvent() const
Return the event associated with the action (i.e.
Definition: tool_action.cpp:72
void Toggle(const TOOL_ACTION &aAction, bool aState)
Apply the default toggle action.
constexpr ret_type KiROUND(fp_type v)
Round a floating point number to an integer using "round halfway cases away from zero".
Definition: util.h:70
int GetUIId() const
Get the ID used in the UI to reference this group.
wxBoxSizer * m_buttonSizer
The buttons that act as the toolbar on the palette.
virtual ~ACTION_TOOLBAR()
void AcceptDragInAsClick(bool aAcceptDragIn=true)
Accept mouse-up as click even if mouse-down happened outside of the control.
ACTION_MANAGER * GetActionManager() const
Definition: tool_manager.h:199
void SelectAction(ACTION_GROUP *aGroup, const TOOL_ACTION &aAction)
Select an action inside a group.
const ACTION_CONDITIONS * GetCondition(const TOOL_ACTION &aAction) const
Get the conditions to use for a specific tool action.
void SetDisabledBitmap(const wxBitmap &aBmp)
Set the bitmap shown when the button is disabled.
int m_id
< The action ID for this action group
void Popup(wxWindow *aFocus=nullptr) override
Popup this window.
#define BUTTON_BORDER
virtual void RegisterUIUpdateHandler(const TOOL_ACTION &aAction, const ACTION_CONDITIONS &aConditions)
Register an action's update conditions with the UI layer to allow the UI to appropriately display the...
Definition: tools_holder.h:63
void EnableAction(const TOOL_ACTION &aAction, bool aEnable=true)
Enable the button for an action on the palette.
void onCharHook(wxKeyEvent &aEvent)
std::map< int, bool > m_toolKinds
wxTimer * m_paletteTimer
A popup window that contains a row of toolbar-like buttons for the user to choose from.
void SetHasPosition(bool aHasPosition)
Returns if the action associated with this event should be treated as immediate regardless of the cur...
Definition: tool_event.h:241
void CancelTool()
Send a cancel event to the tool currently at the top of the tool stack.
void onPaletteEvent(wxCommandEvent &aEvent)
Handle the palette timer triggering.
OPT< TOOL_EVENT > OPT_TOOL_EVENT
Definition: tool_event.h:543
wxBitmap KiScaledBitmap(BITMAPS aBitmap, wxWindow *aWindow, int aHeight)
Construct a wxBitmap from a memory record, scaling it if device DPI demands it.
Definition: bitmap.cpp:148
std::string m_name
The default action to display on the toolbar item.
const std::string & GetName() const
Return name of the action.
Definition: tool_action.h:101
void ClearToolbar()
Clear the toolbar and remove all associated menus.