KiCad PCB EDA Suite
Loading...
Searching...
No Matches
hotkey_cycle_popup.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) 2023 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#include <eda_draw_frame.h>
26
27#ifdef __WXGTK__
28#define LIST_BOX_H_PADDING 20
29#define LIST_BOX_V_PADDING 8
30#elif __WXMAC__
31#define LIST_BOX_H_PADDING 40
32#define LIST_BOX_V_PADDING 5
33#else
34#define LIST_BOX_H_PADDING 10
35#define LIST_BOX_V_PADDING 5
36#endif
37
38#define SHOW_TIME_MS 500
39
40
42 EDA_VIEW_SWITCHER_BASE( aParent ),
43 m_drawFrame( aParent )
44{
45 m_showTimer = new wxTimer( this );
46 Bind( wxEVT_TIMER, [&]( wxTimerEvent& ) { Show( false ); m_drawFrame->GetCanvas()->SetFocus(); },
47 m_showTimer->GetId() );
48
49 // On macOS, we can't change focus to the canvas before sending the event, so the key events
50 // just get discarded via the "don't steal from an input control" logic. So, set this input
51 // control with a special flag because we really do want to steal from it.
53
54#ifdef __WXOSX__
55 m_listBox->Bind( wxEVT_CHAR_HOOK,
56 [=, this]( wxKeyEvent& aEvent )
57 {
58 aEvent.SetEventType( wxEVT_CHAR );
60 m_drawFrame->GetCanvas()->OnEvent( aEvent );
61 } );
62#endif
63}
64
65
67{
68 delete m_showTimer;
69}
70
71
72void HOTKEY_CYCLE_POPUP::Popup( const wxString& aTitle, const wxArrayString& aItems,
73 int aSelection )
74{
75 m_stTitle->SetLabel( aTitle );
76 m_listBox->Clear();
77 m_listBox->InsertItems( aItems, 0 );
78 m_listBox->SetSelection( std::min( aSelection,
79 static_cast<int>( m_listBox->GetCount() ) - 1 ) );
80
81 int width = m_stTitle->GetTextExtent( aTitle ).x;
82 int height = 0;
83
84 for( const wxString& item : aItems )
85 {
86 wxSize extents = m_listBox->GetTextExtent( item );
87 width = std::max( width, extents.x );
88 height += extents.y + LIST_BOX_V_PADDING;
89 }
90
91 m_listBox->SetMinSize( wxSize( width + LIST_BOX_H_PADDING,
92 height + ( LIST_BOX_V_PADDING * 2 ) ) );
93
94 // this line fixes an issue on Linux Ubuntu using Unity (dialog not shown),
95 // and works fine on all systems
96 GetSizer()->Fit( this );
97
98 if( m_showTimer->IsRunning() )
99 {
100 m_showTimer->StartOnce( SHOW_TIME_MS );
101 SetFocus();
102 return;
103 }
104
105 m_showTimer->StartOnce( SHOW_TIME_MS );
106
107 Show( true );
108 Centre();
109 SetFocus();
110}
111
112
113bool HOTKEY_CYCLE_POPUP::TryBefore( wxEvent& aEvent )
114{
115 if( aEvent.GetEventType() == wxEVT_CHAR || aEvent.GetEventType() == wxEVT_CHAR_HOOK )
116 {
117 aEvent.SetEventType( wxEVT_CHAR );
118 //m_drawFrame->GetCanvas()->SetFocus(); // on GTK causes focus flicker and is not needed
119 m_drawFrame->GetCanvas()->OnEvent( aEvent );
120 return true;
121 }
122
123 return EDA_VIEW_SWITCHER_BASE::TryBefore( aEvent );
124}
bool Show(bool show) override
The base class for create windows for drawing purpose.
virtual EDA_DRAW_PANEL_GAL * GetCanvas() const
Return a pointer to GAL-based canvas of given EDA draw frame.
void SetFocus() override
void OnEvent(wxEvent &aEvent)
Used to forward events to the canvas from popups, etc.
Class EDA_VIEW_SWITCHER_BASE.
void Popup(const wxString &aTitle, const wxArrayString &aItems, int aSelection)
EDA_DRAW_FRAME * m_drawFrame
HOTKEY_CYCLE_POPUP(EDA_DRAW_FRAME *aParent)
bool TryBefore(wxEvent &aEvent) override
#define LIST_BOX_V_PADDING
#define LIST_BOX_H_PADDING
#define SHOW_TIME_MS
KICOMMON_API const wxString s_FocusStealableInputName
Definition: ui_common.cpp:45