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, [=]( wxKeyEvent& aEvent )
56 {
57 aEvent.SetEventType( wxEVT_CHAR );
59 m_drawFrame->GetCanvas()->OnEvent( aEvent );
60 } );
61#endif
62}
63
64
66{
67 delete m_showTimer;
68}
69
70
71void HOTKEY_CYCLE_POPUP::Popup( const wxString& aTitle, const wxArrayString& aItems,
72 int aSelection )
73{
74 m_stTitle->SetLabel( aTitle );
75 m_listBox->Clear();
76 m_listBox->InsertItems( aItems, 0 );
77 m_listBox->SetSelection( std::min( aSelection,
78 static_cast<int>( m_listBox->GetCount() ) - 1 ) );
79
80 int width = m_stTitle->GetTextExtent( aTitle ).x;
81 int height = 0;
82
83 for( const wxString& item : aItems )
84 {
85 wxSize extents = m_listBox->GetTextExtent( item );
86 width = std::max( width, extents.x );
87 height += extents.y + LIST_BOX_V_PADDING;
88 }
89
90 m_listBox->SetMinSize( wxSize( width + LIST_BOX_H_PADDING,
91 height + ( LIST_BOX_V_PADDING * 2 ) ) );
92
93 // this line fixes an issue on Linux Ubuntu using Unity (dialog not shown),
94 // and works fine on all systems
95 GetSizer()->Fit( this );
96
97 if( m_showTimer->IsRunning() )
98 {
99 m_showTimer->StartOnce( SHOW_TIME_MS );
100 SetFocus();
101 return;
102 }
103
104 m_showTimer->StartOnce( SHOW_TIME_MS );
105
106 Show( true );
107 Centre();
108 SetFocus();
109}
110
111
112bool HOTKEY_CYCLE_POPUP::TryBefore( wxEvent& aEvent )
113{
114 if( aEvent.GetEventType() == wxEVT_CHAR || aEvent.GetEventType() == wxEVT_CHAR_HOOK )
115 {
116 aEvent.SetEventType( wxEVT_CHAR );
117 //m_drawFrame->GetCanvas()->SetFocus(); // on GTK causes focus flicker and is not needed
118 m_drawFrame->GetCanvas()->OnEvent( aEvent );
119 return true;
120 }
121
122 return EDA_VIEW_SWITCHER_BASE::TryBefore( aEvent );
123}
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:42