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 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
22#include <wx/utils.h>
23
24
25#ifdef __WXGTK__
26#define LIST_BOX_H_PADDING 20
27#define LIST_BOX_V_PADDING 8
28#else
29#define LIST_BOX_H_PADDING 10
30#define LIST_BOX_V_PADDING 5
31#endif
32
33
34EDA_VIEW_SWITCHER::EDA_VIEW_SWITCHER( wxWindow* aParent, const wxArrayString& aItems,
35 wxKeyCode aCtrlKey ) :
36 EDA_VIEW_SWITCHER_BASE( aParent ),
37 // Start with the tab marked as "up" so the initial ctrl-tab press advances selection.
38 m_tabState( false ),
39 m_receivingEvents( false ),
40 m_ctrlKey( aCtrlKey )
41{
42 m_listBox->InsertItems( aItems, 0 );
43 m_listBox->SetSelection( std::min( 0, (int) m_listBox->GetCount() - 1 ) );
44
45 int width = 0;
46 int height = 0;
47
48 for( const wxString& item : aItems )
49 {
50 wxSize extents = m_listBox->GetTextExtent( item );
51 width = std::max( width, extents.x );
52 height += extents.y + LIST_BOX_V_PADDING;
53 }
54
55 m_listBox->SetMinSize( wxSize( width + LIST_BOX_H_PADDING,
56 height + ( LIST_BOX_V_PADDING * 2 ) ) );
58
59 // this line fixes an issue on Linux Ubuntu using Unity (dialog not shown),
60 // and works fine on all systems
61 GetSizer()->Fit( this );
62
63 Centre();
64}
65
66
67bool EDA_VIEW_SWITCHER::Show( bool aShow )
68{
69 if( !aShow )
70 m_receivingEvents = false;
71
72#ifdef __WXGTK__
73 // On Wayland, window positions cannot be changed after mapping. DIALOG_SHIM::Show() calls
74 // Raise() -> gtk_window_present() before wxDialog::Show(), prematurely mapping the window so
75 // the compositor places it at its default position instead of centered on the parent. Centre
76 // before mapping and bypass DIALOG_SHIM::Show() since this transient popup needs no geometry
77 // save/restore.
78 if( aShow && wxGetEnv( wxT( "WAYLAND_DISPLAY" ), nullptr ) )
79 {
81 Centre();
82 m_receivingEvents = true;
83 return wxDialog::Show( true );
84 }
85#endif
86
87 bool ret = DIALOG_SHIM::Show( aShow );
88
89 if( aShow )
90 {
91 Centre();
92 m_receivingEvents = true;
93 }
94
95 return ret;
96}
97
98
99// OK, this is *really* annoying, but wxWidgets doesn't give us key-down events while the
100// control key is being held down. So we can't use OnKeyDown() or OnCharHook() and instead
101// must rely on watching key states in TryBefore().
102//
103// Just checking the state of the tab key is tempting, but then we'll think it's been hit
104// several times when it's actually just a key-down followed by a redraw or idle event.
105//
106// So we have to keep a state machine of the tab key.
107//
108bool EDA_VIEW_SWITCHER::TryBefore( wxEvent& aEvent )
109{
110 if( !m_receivingEvents )
111 {
112 return DIALOG_SHIM::TryBefore( aEvent );
113 }
114
115 // Check for tab key leading edge
116 if( !m_tabState && wxGetKeyState( WXK_TAB ) )
117 {
118 m_tabState = true;
119
120 int idx = m_listBox->GetSelection();
121
122 if( wxGetKeyState( WXK_SHIFT ) && m_ctrlKey != WXK_SHIFT )
123 {
124 if( --idx < 0 )
125 m_listBox->SetSelection( (int) m_listBox->GetCount() - 1 );
126 else
127 m_listBox->SetSelection( idx );
128 }
129 else
130 {
131 if( ++idx >= (int) m_listBox->GetCount() )
132 m_listBox->SetSelection( 0 );
133 else
134 m_listBox->SetSelection( idx );
135 }
136
137 return true;
138 }
139
140 // Check for tab key trailing edge
141 if( m_tabState && !wxGetKeyState( WXK_TAB ) )
142 {
143 m_tabState = false;
144 }
145
146 // Check for control key trailing edge
147 if( !wxGetKeyState( m_ctrlKey ) )
148 {
149 wxPostEvent( this, wxCommandEvent( wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK ) );
150 }
151 else if( wxGetKeyState( WXK_ESCAPE ) )
152 {
153 wxPostEvent( this, wxCommandEvent( wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL ) );
154 }
155
156 return DIALOG_SHIM::TryBefore( aEvent );
157}
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:79
void clampToWorkArea()
Shrink the dialog's minimum and current size down to the work area of the display it occupies,...
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)
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