KiCad PCB EDA Suite
Loading...
Searching...
No Matches
listbox_tricks.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, 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#include <wx/clipbrd.h>
27#include <wx/listbox.h>
28#include <wx/menu.h>
29#include <wx/window.h>
30
31#include <bitmaps.h>
33#include <widgets/ui_common.h>
34
35
36wxDEFINE_EVENT( EDA_EVT_LISTBOX_COPY, wxCommandEvent );
37wxDEFINE_EVENT( EDA_EVT_LISTBOX_CUT, wxCommandEvent );
38wxDEFINE_EVENT( EDA_EVT_LISTBOX_PASTE, wxCommandEvent );
39wxDEFINE_EVENT( EDA_EVT_LISTBOX_DELETE, wxCommandEvent );
40wxDEFINE_EVENT( EDA_EVT_LISTBOX_DUPLICATE, wxCommandEvent );
41
42wxDEFINE_EVENT( EDA_EVT_LISTBOX_CHANGED, wxCommandEvent );
43
44
45LISTBOX_TRICKS::LISTBOX_TRICKS( wxWindow& aParent, wxListBox& aListBox ) :
46 m_parent( aParent ), m_listBox( aListBox )
47{
48 // Init default menu labels
49 m_menuStrings = { {
50 { ID_COPY, _( "Copy" ) },
51 { ID_PASTE, _( "Paste" ) },
52 { ID_CUT, _( "Cut" ) },
53 { ID_DUPLICATE, _( "Duplicate" ) },
54 { ID_DELETE, _( "Delete" ) },
55 } };
56
57
58 m_listBox.Connect( wxEVT_RIGHT_DOWN, wxMouseEventHandler( LISTBOX_TRICKS::OnListBoxRDown ),
59 nullptr, this );
60 m_listBox.Connect( wxEVT_KEY_DOWN, wxKeyEventHandler( LISTBOX_TRICKS::OnListBoxKeyDown ),
61 nullptr, this );
62
63 Connect( EDA_EVT_LISTBOX_DELETE, wxCommandEventHandler( LISTBOX_TRICKS::OnListBoxDelete ) );
64 Connect( EDA_EVT_LISTBOX_COPY, wxCommandEventHandler( LISTBOX_TRICKS::OnListBoxCopy ) );
65 Connect( EDA_EVT_LISTBOX_CUT, wxCommandEventHandler( LISTBOX_TRICKS::OnListBoxCut ) );
66 Connect( EDA_EVT_LISTBOX_PASTE, wxCommandEventHandler( LISTBOX_TRICKS::OnListBoxPaste ) );
67 Connect( EDA_EVT_LISTBOX_DUPLICATE,
68 wxCommandEventHandler( LISTBOX_TRICKS::OnListBoxDuplicate ) );
69}
70
71
73{
74 m_listBox.Disconnect( wxEVT_RIGHT_DOWN, wxMouseEventHandler( LISTBOX_TRICKS::OnListBoxRDown ),
75 nullptr, this );
76 m_listBox.Disconnect( wxEVT_KEY_DOWN, wxKeyEventHandler( LISTBOX_TRICKS::OnListBoxKeyDown ),
77 nullptr, this );
78
79 Disconnect( EDA_EVT_LISTBOX_DELETE, wxCommandEventHandler( LISTBOX_TRICKS::OnListBoxDelete ) );
80 Disconnect( EDA_EVT_LISTBOX_COPY, wxCommandEventHandler( LISTBOX_TRICKS::OnListBoxCopy ) );
81 Disconnect( EDA_EVT_LISTBOX_CUT, wxCommandEventHandler( LISTBOX_TRICKS::OnListBoxCut ) );
82 Disconnect( EDA_EVT_LISTBOX_PASTE, wxCommandEventHandler( LISTBOX_TRICKS::OnListBoxPaste ) );
83 Disconnect( EDA_EVT_LISTBOX_DUPLICATE,
84 wxCommandEventHandler( LISTBOX_TRICKS::OnListBoxDuplicate ) );
85}
86
87
88void LISTBOX_TRICKS::SetMenuLabels( const std::map<MENU_ID, wxString>& aItems )
89{
90 for( const auto& [id, string] : aItems )
91 {
92 m_menuStrings[id] = string;
93 }
94}
95
96
98{
99 wxArrayInt selections;
100 m_listBox.GetSelections( selections );
101
102 std::sort( selections.begin(), selections.end() );
103
104 for( int ii = selections.GetCount() - 1; ii >= 0; ii-- )
105 m_listBox.Delete( selections[ii] );
106
107 m_listBox.SetSelection( wxNOT_FOUND );
108
109 if( m_listBox.GetCount() > 0 )
110 m_listBox.SetSelection( std::max( 0, selections[0] - 1 ) );
111
112 wxPostEvent( &m_listBox, wxCommandEvent( EDA_EVT_LISTBOX_CHANGED ) );
113 return selections;
114}
115
116
118{
119 wxArrayInt selections;
120 m_listBox.GetSelections( selections );
121
122 wxArrayString result;
123
124 for( size_t ii = 0; ii < selections.GetCount(); ii++ )
125 result.Add( m_listBox.GetString( selections[ii] ) );
126
127 return result;
128}
129
130
132{
133 wxArrayInt selections;
134 m_listBox.GetSelections( selections );
135
136 int insertAt = selections.GetCount() > 0 ? selections.back() + 1 : m_listBox.GetCount();
137
138 m_listBox.SetSelection( wxNOT_FOUND );
139
140 for( size_t ii = 0; ii < selections.GetCount(); ii++ )
141 {
142 wxString filter = m_listBox.GetString( selections[ii] );
143 m_listBox.Insert( filter, insertAt );
144 m_listBox.SetSelection( insertAt );
145 insertAt++;
146 }
147
148 wxPostEvent( &m_listBox, wxCommandEvent( EDA_EVT_LISTBOX_CHANGED ) );
149}
150
151
153{
154 wxArrayString filters = listBoxGetSelected();
155
156 wxString result;
157
158 for( const wxString& filter : filters )
159 {
160 result += filter + wxT( "\n" );
161 }
162
163 if( wxTheClipboard->Open() )
164 {
165 wxTheClipboard->SetData( new wxTextDataObject( result ) );
166 wxTheClipboard->Close();
167 }
168}
169
170
172{
173 wxArrayString lines;
174
175 if( wxTheClipboard->Open() )
176 {
177 wxTextDataObject data;
178 wxTheClipboard->GetData( data );
179
180 wxString text = data.GetText();
181 text.Trim( false );
182 text.Trim( true );
183 lines = wxSplit( text, '\n' );
184
185 wxTheClipboard->Close();
186 }
187
188 wxArrayInt selections;
189 m_listBox.GetSelections( selections );
190 int insertAt = selections.GetCount() > 0 ? selections.back() + 1 : m_listBox.GetCount();
191
192 for( wxString& line : lines )
193 {
194 line.Trim( false );
195 line.Trim( true );
196 }
197
198 m_listBox.InsertItems( lines, insertAt );
199
200 m_listBox.SetSelection( wxNOT_FOUND );
201
202 for( size_t ii = insertAt; ii < insertAt + lines.GetCount(); ii++ )
203 m_listBox.SetSelection( ii );
204
205 wxPostEvent( &m_listBox, wxCommandEvent( EDA_EVT_LISTBOX_CHANGED ) );
206}
207
208
210{
211 listBoxCopy();
212 wxArrayInt deleted = listBoxDeleteSelected();
213
214 size_t select = deleted.GetCount() > 0 ? deleted[0] : m_listBox.GetCount();
215
216 m_listBox.SetSelection( wxNOT_FOUND );
217 m_listBox.SetSelection( std::min( select, (size_t) m_listBox.GetCount() - 1 ) );
218}
219
220
221void LISTBOX_TRICKS::OnListBoxRDown( wxMouseEvent& aEvent )
222{
223 wxMenu menu;
224
225 const auto mstr = [&]( const MENU_ID& id ) -> const wxString&
226 {
227 return m_menuStrings[id];
228 };
229
230 // clang-format off
232 mstr( ID_COPY ) + "\tCtrl+C",
233 KiBitmap( BITMAPS::copy ) );
235 mstr( ID_CUT ) + "\tCtrl+X",
236 KiBitmap( BITMAPS::cut ) );
238 mstr( ID_PASTE ) + "\tCtrl+V",
239 KiBitmap( BITMAPS::paste ) );
241 mstr( ID_DUPLICATE ) + "\tCtrl+D",
242 KiBitmap( BITMAPS::duplicate ) );
244 mstr( ID_DELETE ) + "\tDel",
245 KiBitmap( BITMAPS::trash ) );
246 // clang-format on
247
248 menu.Bind( wxEVT_COMMAND_MENU_SELECTED,
249 [&]( wxCommandEvent& aCmd )
250 {
251 switch( aEvent.GetId() )
252 {
253 case ID_COPY: listBoxCopy(); break;
254 case ID_PASTE: listBoxPaste(); break;
255 case ID_CUT: listBoxCut(); break;
256 case ID_DELETE: listBoxDeleteSelected(); break;
257 case ID_DUPLICATE: listBoxDuplicateSelected(); break;
258 default: aEvent.Skip();
259 }
260 } );
261
262 m_parent.PopupMenu( &menu );
263}
264
265
266void LISTBOX_TRICKS::OnListBoxKeyDown( wxKeyEvent& aEvent )
267{
268 if( aEvent.GetKeyCode() == WXK_DELETE )
269 {
271 }
272 else
273 {
274 if( aEvent.ControlDown() )
275 {
276 switch( aEvent.GetKeyCode() )
277 {
278 case 'C': listBoxCopy(); break;
279 case 'V': listBoxPaste(); break;
280 case 'X': listBoxCut(); break;
281 case 'D': listBoxDuplicateSelected(); break;
282 default: aEvent.Skip();
283 }
284 }
285 else
286 {
287 aEvent.Skip();
288 }
289 }
290}
291
292
293void LISTBOX_TRICKS::OnListBoxDelete( wxCommandEvent& aEvent )
294{
296}
297
298
299void LISTBOX_TRICKS::OnListBoxCopy( wxCommandEvent& aEvent )
300{
301 listBoxCopy();
302}
303
304
305void LISTBOX_TRICKS::OnListBoxCut( wxCommandEvent& aEvent )
306{
307 listBoxCut();
308}
309
310
311void LISTBOX_TRICKS::OnListBoxPaste( wxCommandEvent& aEvent )
312{
313 listBoxPaste();
314}
315
316
317void LISTBOX_TRICKS::OnListBoxDuplicate( wxCommandEvent& aEvent )
318{
320}
wxBitmap KiBitmap(BITMAPS aBitmap, int aHeightTag)
Construct a wxBitmap from an image identifier Returns the image from the active theme if the image ha...
Definition: bitmap.cpp:104
wxListBox & m_listBox
void OnListBoxCut(wxCommandEvent &aEvent)
void listBoxDuplicateSelected()
Duplicate the selected filters.
void OnListBoxDuplicate(wxCommandEvent &aEvent)
LISTBOX_TRICKS(wxWindow &aWindow, wxListBox &aListBox)
void OnListBoxPaste(wxCommandEvent &aEvent)
void OnListBoxCopy(wxCommandEvent &aEvent)
wxArrayString listBoxGetSelected() const
void OnListBoxDelete(wxCommandEvent &aEvent)
void OnListBoxKeyDown(wxKeyEvent &aEvent)
MENU_ID
These are the ids for the menu.
wxArrayInt listBoxDeleteSelected()
Delete the selected filters.
void SetMenuLabels(const std::map< MENU_ID, wxString > &aItems)
std::map< MENU_ID, wxString > m_menuStrings
wxWindow & m_parent
void OnListBoxRDown(wxMouseEvent &aEvent)
#define _(s)
wxDEFINE_EVENT(EDA_EVT_LISTBOX_COPY, wxCommandEvent)
KICOMMON_API wxMenuItem * AddMenuItem(wxMenu *aMenu, int aId, const wxString &aText, const wxBitmapBundle &aImage, wxItemKind aType=wxITEM_NORMAL)
Create and insert a menu item with an icon into aMenu.
Definition: ui_common.cpp:374
Functions to provide common constants and other functions to assist in making a consistent UI.