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 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, see <https://www.gnu.org/licenses/>.
18 */
19
21#include <eda_draw_frame.h>
22
23#ifdef __WXGTK__
24#define LIST_BOX_H_PADDING 20
25#define LIST_BOX_V_PADDING 8
26#elif __WXMAC__
27#define LIST_BOX_H_PADDING 40
28#define LIST_BOX_V_PADDING 5
29#else
30#define LIST_BOX_H_PADDING 10
31#define LIST_BOX_V_PADDING 5
32#endif
33
34#define SHOW_TIME_MS 500
35
36
38 EDA_VIEW_SWITCHER_BASE( aParent ),
39 m_drawFrame( aParent )
40{
41 m_showTimer = new wxTimer( this );
42 Bind( wxEVT_TIMER, [&]( wxTimerEvent& ) { Show( false ); m_drawFrame->GetCanvas()->SetFocus(); },
43 m_showTimer->GetId() );
44
45 // On macOS, we can't change focus to the canvas before sending the event, so the key events
46 // just get discarded via the "don't steal from an input control" logic. So, set this input
47 // control with a special flag because we really do want to steal from it.
49
50#ifdef __WXOSX__
51 m_listBox->Bind( wxEVT_CHAR_HOOK,
52 [=, this]( wxKeyEvent& aEvent )
53 {
54 aEvent.SetEventType( wxEVT_CHAR );
55 m_drawFrame->GetCanvas()->SetFocus();
56 m_drawFrame->GetCanvas()->OnEvent( aEvent );
57 } );
58#endif
59}
60
61
66
67
68void HOTKEY_CYCLE_POPUP::Popup( const wxString& aTitle, const wxArrayString& aItems,
69 int aSelection )
70{
71 m_stTitle->SetLabel( aTitle );
72 m_listBox->Clear();
73 m_listBox->InsertItems( aItems, 0 );
74 m_listBox->SetSelection( std::min( aSelection,
75 static_cast<int>( m_listBox->GetCount() ) - 1 ) );
76
77 int width = m_stTitle->GetTextExtent( aTitle ).x;
78 int height = 0;
79
80 for( const wxString& item : aItems )
81 {
82 wxSize extents = m_listBox->GetTextExtent( item );
83 width = std::max( width, extents.x );
84 height += extents.y + LIST_BOX_V_PADDING;
85 }
86
87 m_listBox->SetMinSize( wxSize( width + LIST_BOX_H_PADDING,
88 height + ( LIST_BOX_V_PADDING * 2 ) ) );
89
90 // this line fixes an issue on Linux Ubuntu using Unity (dialog not shown),
91 // and works fine on all systems
92 GetSizer()->Fit( this );
93
94 if( m_showTimer->IsRunning() )
95 {
96 m_showTimer->StartOnce( SHOW_TIME_MS );
97 SetFocus();
98 return;
99 }
100
101 m_showTimer->StartOnce( SHOW_TIME_MS );
102
103 Show( true );
104 Centre();
105 SetFocus();
106}
107
108
109bool HOTKEY_CYCLE_POPUP::TryBefore( wxEvent& aEvent )
110{
111 if( aEvent.GetEventType() == wxEVT_CHAR || aEvent.GetEventType() == wxEVT_CHAR_HOOK )
112 {
113 aEvent.SetEventType( wxEVT_CHAR );
114 //m_drawFrame->GetCanvas()->SetFocus(); // on GTK causes focus flicker and is not needed
115 m_drawFrame->GetCanvas()->OnEvent( aEvent );
116 return true;
117 }
118
119 return EDA_VIEW_SWITCHER_BASE::TryBefore( aEvent );
120}
bool Show(bool show) override
The base class for create windows for drawing purpose.
EDA_VIEW_SWITCHER_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxString &title=_("View Preset Switcher"), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxSTAY_ON_TOP)
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:46