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