1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
|
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright The KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "widgets/listbox_tricks.h"
#include <wx/clipbrd.h>
#include <wx/listbox.h>
#include <wx/menu.h>
#include <wx/window.h>
#include <bitmaps.h>
#include <bitmaps/bitmaps_list.h>
#include <widgets/ui_common.h>
wxDEFINE_EVENT( EDA_EVT_LISTBOX_COPY, wxCommandEvent );
wxDEFINE_EVENT( EDA_EVT_LISTBOX_CUT, wxCommandEvent );
wxDEFINE_EVENT( EDA_EVT_LISTBOX_PASTE, wxCommandEvent );
wxDEFINE_EVENT( EDA_EVT_LISTBOX_DELETE, wxCommandEvent );
wxDEFINE_EVENT( EDA_EVT_LISTBOX_DUPLICATE, wxCommandEvent );
wxDEFINE_EVENT( EDA_EVT_LISTBOX_CHANGED, wxCommandEvent );
LISTBOX_TRICKS::LISTBOX_TRICKS( wxWindow& aParent, wxListBox& aListBox ) :
m_parent( aParent ), m_listBox( aListBox )
{
// Init default menu labels
m_menuStrings = { {
{ ID_COPY, _( "Copy" ) },
{ ID_PASTE, _( "Paste" ) },
{ ID_CUT, _( "Cut" ) },
{ ID_DUPLICATE, _( "Duplicate" ) },
{ ID_DELETE, _( "Delete" ) },
} };
m_listBox.Connect( wxEVT_RIGHT_DOWN, wxMouseEventHandler( LISTBOX_TRICKS::OnListBoxRDown ),
nullptr, this );
m_listBox.Connect( wxEVT_KEY_DOWN, wxKeyEventHandler( LISTBOX_TRICKS::OnListBoxKeyDown ),
nullptr, this );
Connect( EDA_EVT_LISTBOX_DELETE, wxCommandEventHandler( LISTBOX_TRICKS::OnListBoxDelete ) );
Connect( EDA_EVT_LISTBOX_COPY, wxCommandEventHandler( LISTBOX_TRICKS::OnListBoxCopy ) );
Connect( EDA_EVT_LISTBOX_CUT, wxCommandEventHandler( LISTBOX_TRICKS::OnListBoxCut ) );
Connect( EDA_EVT_LISTBOX_PASTE, wxCommandEventHandler( LISTBOX_TRICKS::OnListBoxPaste ) );
Connect( EDA_EVT_LISTBOX_DUPLICATE,
wxCommandEventHandler( LISTBOX_TRICKS::OnListBoxDuplicate ) );
}
LISTBOX_TRICKS::~LISTBOX_TRICKS()
{
m_listBox.Disconnect( wxEVT_RIGHT_DOWN, wxMouseEventHandler( LISTBOX_TRICKS::OnListBoxRDown ),
nullptr, this );
m_listBox.Disconnect( wxEVT_KEY_DOWN, wxKeyEventHandler( LISTBOX_TRICKS::OnListBoxKeyDown ),
nullptr, this );
Disconnect( EDA_EVT_LISTBOX_DELETE, wxCommandEventHandler( LISTBOX_TRICKS::OnListBoxDelete ) );
Disconnect( EDA_EVT_LISTBOX_COPY, wxCommandEventHandler( LISTBOX_TRICKS::OnListBoxCopy ) );
Disconnect( EDA_EVT_LISTBOX_CUT, wxCommandEventHandler( LISTBOX_TRICKS::OnListBoxCut ) );
Disconnect( EDA_EVT_LISTBOX_PASTE, wxCommandEventHandler( LISTBOX_TRICKS::OnListBoxPaste ) );
Disconnect( EDA_EVT_LISTBOX_DUPLICATE,
wxCommandEventHandler( LISTBOX_TRICKS::OnListBoxDuplicate ) );
}
void LISTBOX_TRICKS::SetMenuLabels( const std::map<MENU_ID, wxString>& aItems )
{
for( const auto& [id, string] : aItems )
{
m_menuStrings[id] = string;
}
}
wxArrayInt LISTBOX_TRICKS::listBoxDeleteSelected()
{
wxArrayInt selections;
m_listBox.GetSelections( selections );
if( selections.GetCount() == 0 ) // Nothing to remove
return selections;
std::sort( selections.begin(), selections.end() );
for( int ii = selections.GetCount() - 1; ii >= 0; ii-- )
m_listBox.Delete( selections[ii] );
m_listBox.SetSelection( wxNOT_FOUND );
if( m_listBox.GetCount() > 0 )
m_listBox.SetSelection( std::max( 0, selections[0] - 1 ) );
wxPostEvent( &m_listBox, wxCommandEvent( EDA_EVT_LISTBOX_CHANGED ) );
return selections;
}
wxArrayString LISTBOX_TRICKS::listBoxGetSelected() const
{
wxArrayInt selections;
m_listBox.GetSelections( selections );
wxArrayString result;
for( size_t ii = 0; ii < selections.GetCount(); ii++ )
result.Add( m_listBox.GetString( selections[ii] ) );
return result;
}
void LISTBOX_TRICKS::listBoxDuplicateSelected()
{
wxArrayInt selections;
m_listBox.GetSelections( selections );
int insertAt = selections.GetCount() > 0 ? selections.back() + 1 : m_listBox.GetCount();
m_listBox.SetSelection( wxNOT_FOUND );
for( size_t ii = 0; ii < selections.GetCount(); ii++ )
{
wxString filter = m_listBox.GetString( selections[ii] );
m_listBox.Insert( filter, insertAt );
m_listBox.SetSelection( insertAt );
insertAt++;
}
wxPostEvent( &m_listBox, wxCommandEvent( EDA_EVT_LISTBOX_CHANGED ) );
}
void LISTBOX_TRICKS::listBoxCopy()
{
wxArrayString filters = listBoxGetSelected();
wxString result;
for( const wxString& filter : filters )
{
result += filter + wxT( "\n" );
}
if( wxTheClipboard->Open() )
{
wxTheClipboard->SetData( new wxTextDataObject( result ) );
wxTheClipboard->Close();
}
}
void LISTBOX_TRICKS::listBoxPaste()
{
wxArrayString lines;
if( wxTheClipboard->Open() )
{
wxTextDataObject data;
wxTheClipboard->GetData( data );
wxString text = data.GetText();
text.Trim( false );
text.Trim( true );
lines = wxSplit( text, '\n' );
wxTheClipboard->Close();
}
wxArrayInt selections;
m_listBox.GetSelections( selections );
int insertAt = selections.GetCount() > 0 ? selections.back() + 1 : m_listBox.GetCount();
for( wxString& line : lines )
{
line.Trim( false );
line.Trim( true );
}
m_listBox.InsertItems( lines, insertAt );
m_listBox.SetSelection( wxNOT_FOUND );
for( size_t ii = insertAt; ii < insertAt + lines.GetCount(); ii++ )
m_listBox.SetSelection( ii );
wxPostEvent( &m_listBox, wxCommandEvent( EDA_EVT_LISTBOX_CHANGED ) );
}
void LISTBOX_TRICKS::listBoxCut()
{
listBoxCopy();
wxArrayInt deleted = listBoxDeleteSelected();
size_t select = deleted.GetCount() > 0 ? deleted[0] : m_listBox.GetCount();
m_listBox.SetSelection( wxNOT_FOUND );
m_listBox.SetSelection( std::min( select, (size_t) m_listBox.GetCount() - 1 ) );
}
void LISTBOX_TRICKS::OnListBoxRDown( wxMouseEvent& aEvent )
{
wxMenu menu;
const auto mstr = [&]( const MENU_ID& id ) -> const wxString&
{
return m_menuStrings[id];
};
// clang-format off
KIUI::AddMenuItem( &menu, ID_COPY,
mstr( ID_COPY ) + "\tCtrl+C",
KiBitmap( BITMAPS::copy ) );
KIUI::AddMenuItem( &menu, ID_CUT,
mstr( ID_CUT ) + "\tCtrl+X",
KiBitmap( BITMAPS::cut ) );
KIUI::AddMenuItem( &menu, ID_PASTE,
mstr( ID_PASTE ) + "\tCtrl+V",
KiBitmap( BITMAPS::paste ) );
KIUI::AddMenuItem( &menu, ID_DUPLICATE,
mstr( ID_DUPLICATE ) + "\tCtrl+D",
KiBitmap( BITMAPS::duplicate ) );
KIUI::AddMenuItem( &menu, ID_DELETE,
mstr( ID_DELETE ) + "\tDel",
KiBitmap( BITMAPS::trash ) );
// clang-format on
menu.Bind( wxEVT_COMMAND_MENU_SELECTED,
[&]( wxCommandEvent& aCmd )
{
switch( aEvent.GetId() )
{
case ID_COPY: listBoxCopy(); break;
case ID_PASTE: listBoxPaste(); break;
case ID_CUT: listBoxCut(); break;
case ID_DELETE: listBoxDeleteSelected(); break;
case ID_DUPLICATE: listBoxDuplicateSelected(); break;
default: aEvent.Skip();
}
} );
m_parent.PopupMenu( &menu );
}
void LISTBOX_TRICKS::OnListBoxKeyDown( wxKeyEvent& aEvent )
{
if( aEvent.GetKeyCode() == WXK_DELETE )
{
listBoxDeleteSelected();
}
else
{
if( aEvent.ControlDown() )
{
switch( aEvent.GetKeyCode() )
{
case 'C': listBoxCopy(); break;
case 'V': listBoxPaste(); break;
case 'X': listBoxCut(); break;
case 'D': listBoxDuplicateSelected(); break;
default: aEvent.Skip();
}
}
else
{
aEvent.Skip();
}
}
}
void LISTBOX_TRICKS::OnListBoxDelete( wxCommandEvent& aEvent )
{
listBoxDeleteSelected();
}
void LISTBOX_TRICKS::OnListBoxCopy( wxCommandEvent& aEvent )
{
listBoxCopy();
}
void LISTBOX_TRICKS::OnListBoxCut( wxCommandEvent& aEvent )
{
listBoxCut();
}
void LISTBOX_TRICKS::OnListBoxPaste( wxCommandEvent& aEvent )
{
listBoxPaste();
}
void LISTBOX_TRICKS::OnListBoxDuplicate( wxCommandEvent& aEvent )
{
listBoxDuplicateSelected();
}
|