KiCad PCB EDA Suite
Loading...
Searching...
No Matches
eda_view_switcher.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) 2020-2021 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
27#ifdef __WXGTK__
28#define LIST_BOX_H_PADDING 20
29#define LIST_BOX_V_PADDING 8
30#else
31#define LIST_BOX_H_PADDING 10
32#define LIST_BOX_V_PADDING 5
33#endif
34
35
36EDA_VIEW_SWITCHER::EDA_VIEW_SWITCHER( wxWindow* aParent, const wxArrayString& aItems,
37 wxKeyCode aCtrlKey ) :
38 EDA_VIEW_SWITCHER_BASE( aParent ),
39 m_tabState( true ),
40 m_receivingEvents( false ),
41 m_ctrlKey( aCtrlKey )
42{
43 m_listBox->InsertItems( aItems, 0 );
44 m_listBox->SetSelection( std::min( 1, (int) m_listBox->GetCount() - 1 ) );
45
46 int width = 0;
47 int height = 0;
48
49 for( const wxString& item : aItems )
50 {
51 wxSize extents = m_listBox->GetTextExtent( item );
52 width = std::max( width, extents.x );
53 height += extents.y + LIST_BOX_V_PADDING;
54 }
55
56 m_listBox->SetMinSize( wxSize( width + LIST_BOX_H_PADDING,
57 height + ( LIST_BOX_V_PADDING * 2 ) ) );
59
60 // this line fixes an issue on Linux Ubuntu using Unity (dialog not shown),
61 // and works fine on all systems
62 GetSizer()->Fit( this );
63
64 Centre();
65}
66
67
68bool EDA_VIEW_SWITCHER::Show( bool aShow )
69{
70 if( !aShow )
71 m_receivingEvents = false;
72
73 bool ret = DIALOG_SHIM::Show( aShow );
74
75 if( aShow )
76 {
77 m_receivingEvents = true;
78
79 // Force the dialog to always be centered over the window
80 Centre();
81 }
82
83 return ret;
84}
85
86
87// OK, this is *really* annoying, but wxWidgets doesn't give us key-down events while the
88// control key is being held down. So we can't use OnKeyDown() or OnCharHook() and instead
89// must rely on watching key states in TryBefore().
90//
91// Just checking the state of the tab key is tempting, but then we'll think it's been hit
92// several times when it's actually just a key-down followed by a redraw or idle event.
93//
94// So we have to keep a state machine of the tab key.
95//
96bool EDA_VIEW_SWITCHER::TryBefore( wxEvent& aEvent )
97{
99 {
100 return DIALOG_SHIM::TryBefore( aEvent );
101 }
102
103 // Check for tab key leading edge
104 if( !m_tabState && wxGetKeyState( WXK_TAB ) )
105 {
106 m_tabState = true;
107
108 int idx = m_listBox->GetSelection();
109
110 if( wxGetKeyState( WXK_SHIFT ) && m_ctrlKey != WXK_SHIFT )
111 {
112 if( --idx < 0 )
113 m_listBox->SetSelection( (int) m_listBox->GetCount() - 1 );
114 else
115 m_listBox->SetSelection( idx );
116 }
117 else
118 {
119 if( ++idx >= (int) m_listBox->GetCount() )
120 m_listBox->SetSelection( 0 );
121 else
122 m_listBox->SetSelection( idx );
123 }
124
125 return true;
126 }
127
128 // Check for tab key trailing edge
129 if( m_tabState && !wxGetKeyState( WXK_TAB ) )
130 {
131 m_tabState = false;
132 }
133
134 // Check for control key trailing edge
135 if( !wxGetKeyState( m_ctrlKey ) )
136 {
137 wxPostEvent( this, wxCommandEvent( wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK ) );
138 }
139 else if( wxGetKeyState( WXK_ESCAPE ) )
140 {
141 wxPostEvent( this, wxCommandEvent( wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL ) );
142 }
143
144 return DIALOG_SHIM::TryBefore( aEvent );
145}
bool Show(bool show) override
void SetInitialFocus(wxWindow *aWindow)
Sets the window (usually a wxTextCtrl) that should be focused when the dialog is shown.
Definition: dialog_shim.h:102
Class EDA_VIEW_SWITCHER_BASE.
EDA_VIEW_SWITCHER(wxWindow *aParent, const wxArrayString &aItems, wxKeyCode aCtrlKey)
bool Show(bool show) override
bool TryBefore(wxEvent &aEvent) override
#define LIST_BOX_V_PADDING
#define LIST_BOX_H_PADDING