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 ),
47 m_listBox( aListBox )
48{
49 m_listBox.Connect( wxEVT_RIGHT_DOWN, wxMouseEventHandler( LISTBOX_TRICKS::OnListBoxRDown ),
50 nullptr, this );
51 m_listBox.Connect( wxEVT_KEY_DOWN, wxKeyEventHandler( LISTBOX_TRICKS::OnListBoxKeyDown ),
52 nullptr, this );
53
54 Connect( EDA_EVT_LISTBOX_DELETE, wxCommandEventHandler( LISTBOX_TRICKS::OnListBoxDelete ) );
55 Connect( EDA_EVT_LISTBOX_COPY, wxCommandEventHandler( LISTBOX_TRICKS::OnListBoxCopy ) );
56 Connect( EDA_EVT_LISTBOX_CUT, wxCommandEventHandler( LISTBOX_TRICKS::OnListBoxCut ) );
57 Connect( EDA_EVT_LISTBOX_PASTE, wxCommandEventHandler( LISTBOX_TRICKS::OnListBoxPaste ) );
58 Connect( EDA_EVT_LISTBOX_DUPLICATE, wxCommandEventHandler( LISTBOX_TRICKS::OnListBoxDuplicate ) );
59}
60
61
63{
64 m_listBox.Disconnect( wxEVT_RIGHT_DOWN, wxMouseEventHandler( LISTBOX_TRICKS::OnListBoxRDown ),
65 nullptr, this );
66 m_listBox.Disconnect( wxEVT_KEY_DOWN, wxKeyEventHandler( LISTBOX_TRICKS::OnListBoxKeyDown ),
67 nullptr, this );
68
69 Disconnect( EDA_EVT_LISTBOX_DELETE, wxCommandEventHandler( LISTBOX_TRICKS::OnListBoxDelete ) );
70 Disconnect( EDA_EVT_LISTBOX_COPY, wxCommandEventHandler( LISTBOX_TRICKS::OnListBoxCopy ) );
71 Disconnect( EDA_EVT_LISTBOX_CUT, wxCommandEventHandler( LISTBOX_TRICKS::OnListBoxCut ) );
72 Disconnect( EDA_EVT_LISTBOX_PASTE, wxCommandEventHandler( LISTBOX_TRICKS::OnListBoxPaste ) );
73 Disconnect( EDA_EVT_LISTBOX_DUPLICATE, wxCommandEventHandler( LISTBOX_TRICKS::OnListBoxDuplicate ) );
74}
75
76
78{
79 wxArrayInt selections;
80 m_listBox.GetSelections( selections );
81
82 if( selections.GetCount() == 0 ) // Nothing to remove
83 return selections;
84
85 std::sort( selections.begin(), selections.end() );
86
87 for( int ii = selections.GetCount() - 1; ii >= 0; ii-- )
88 m_listBox.Delete( selections[ii] );
89
90 m_listBox.SetSelection( wxNOT_FOUND );
91
92 if( m_listBox.GetCount() > 0 )
93 m_listBox.SetSelection( std::max( 0, selections[0] - 1 ) );
94
95 wxPostEvent( &m_listBox, wxCommandEvent( EDA_EVT_LISTBOX_CHANGED ) );
96 return selections;
97}
98
99
101{
102 wxArrayInt selections;
103 m_listBox.GetSelections( selections );
104
105 wxArrayString result;
106
107 for( size_t ii = 0; ii < selections.GetCount(); ii++ )
108 result.Add( m_listBox.GetString( selections[ii] ) );
109
110 return result;
111}
112
113
115{
116 wxArrayInt selections;
117 m_listBox.GetSelections( selections );
118
119 int insertAt = selections.GetCount() > 0 ? selections.back() + 1 : m_listBox.GetCount();
120
121 m_listBox.SetSelection( wxNOT_FOUND );
122
123 for( size_t ii = 0; ii < selections.GetCount(); ii++ )
124 {
125 wxString filter = m_listBox.GetString( selections[ii] );
126 m_listBox.Insert( filter, insertAt );
127 m_listBox.SetSelection( insertAt );
128 insertAt++;
129 }
130
131 wxPostEvent( &m_listBox, wxCommandEvent( EDA_EVT_LISTBOX_CHANGED ) );
132}
133
134
136{
137 wxArrayString filters = listBoxGetSelected();
138 wxString result;
139
140 for( const wxString& filter : filters )
141 result += filter + wxT( "\n" );
142
143 if( wxTheClipboard->Open() )
144 {
145 wxTheClipboard->SetData( new wxTextDataObject( result ) );
146 wxTheClipboard->Close();
147 }
148}
149
150
152{
153 wxArrayString lines;
154
155 if( wxTheClipboard->Open() )
156 {
157 wxTextDataObject data;
158 wxTheClipboard->GetData( data );
159
160 wxString text = data.GetText();
161 text.Trim( false );
162 text.Trim( true );
163 lines = wxSplit( text, '\n' );
164
165 wxTheClipboard->Close();
166 }
167
168 if( lines.IsEmpty() )
169 return;
170
171 wxArrayInt selections;
172 m_listBox.GetSelections( selections );
173 int insertAt = selections.GetCount() > 0 ? (int) selections.back() + 1 : (int) m_listBox.GetCount();
174
175 for( wxString& line : lines )
176 {
177 line.Trim( false );
178 line.Trim( true );
179 }
180
181 m_listBox.InsertItems( lines, insertAt );
182
183 m_listBox.SetSelection( wxNOT_FOUND );
184
185 for( size_t ii = insertAt; ii < insertAt + lines.GetCount(); ii++ )
186 m_listBox.SetSelection( ii );
187
188 wxPostEvent( &m_listBox, wxCommandEvent( EDA_EVT_LISTBOX_CHANGED ) );
189}
190
191
193{
194 listBoxCopy();
195 wxArrayInt deleted = listBoxDeleteSelected();
196
197 size_t select = deleted.GetCount() > 0 ? deleted[0] : m_listBox.GetCount();
198
199 m_listBox.SetSelection( wxNOT_FOUND );
200 m_listBox.SetSelection( std::min( select, (size_t) m_listBox.GetCount() - 1 ) );
201}
202
203
204void LISTBOX_TRICKS::OnListBoxRDown( wxMouseEvent& aEvent )
205{
206 wxMenu menu;
207
208 KIUI::AddMenuItem( &menu, ID_COPY, _( "Copy" ) + "\tCtrl+C", KiBitmap( BITMAPS::copy ) );
209 KIUI::AddMenuItem( &menu, ID_CUT, _( "Cut" ) + "\tCtrl+X", KiBitmap( BITMAPS::cut ) );
210 KIUI::AddMenuItem( &menu, ID_PASTE, _( "Paste" ) + "\tCtrl+V", KiBitmap( BITMAPS::paste ) );
211 KIUI::AddMenuItem( &menu, ID_DUPLICATE, _( "Duplicate" ) + "\tCtrl+D", KiBitmap( BITMAPS::duplicate ) );
212 KIUI::AddMenuItem( &menu, ID_DELETE, _( "Delete" ) + "\tDel", KiBitmap( BITMAPS::trash ) );
213
214 menu.Bind( wxEVT_COMMAND_MENU_SELECTED,
215 [&]( wxCommandEvent& aCmd )
216 {
217 switch( aEvent.GetId() )
218 {
219 case ID_COPY: listBoxCopy(); break;
220 case ID_PASTE: listBoxPaste(); break;
221 case ID_CUT: listBoxCut(); break;
222 case ID_DELETE: listBoxDeleteSelected(); break;
223 case ID_DUPLICATE: listBoxDuplicateSelected(); break;
224 default: aEvent.Skip();
225 }
226 } );
227
228 m_parent.PopupMenu( &menu );
229}
230
231
232void LISTBOX_TRICKS::OnListBoxKeyDown( wxKeyEvent& aEvent )
233{
234 if( aEvent.GetKeyCode() == WXK_DELETE )
235 {
237 }
238 else
239 {
240 if( aEvent.ControlDown() )
241 {
242 switch( aEvent.GetKeyCode() )
243 {
244 case 'C': listBoxCopy(); break;
245 case 'V': listBoxPaste(); break;
246 case 'X': listBoxCut(); break;
247 case 'D': listBoxDuplicateSelected(); break;
248 default: aEvent.Skip();
249 }
250 }
251 else
252 {
253 aEvent.Skip();
254 }
255 }
256}
257
258
259void LISTBOX_TRICKS::OnListBoxDelete( wxCommandEvent& aEvent )
260{
262}
263
264
265void LISTBOX_TRICKS::OnListBoxCopy( wxCommandEvent& aEvent )
266{
267 listBoxCopy();
268}
269
270
271void LISTBOX_TRICKS::OnListBoxCut( wxCommandEvent& aEvent )
272{
273 listBoxCut();
274}
275
276
277void LISTBOX_TRICKS::OnListBoxPaste( wxCommandEvent& aEvent )
278{
279 listBoxPaste();
280}
281
282
283void LISTBOX_TRICKS::OnListBoxDuplicate( wxCommandEvent& aEvent )
284{
286}
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)
wxArrayInt listBoxDeleteSelected()
Delete the selected filters.
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:382
Functions to provide common constants and other functions to assist in making a consistent UI.