schedit.cpp 31.7 KB
Newer Older
1
 /*
2 3
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
4 5 6
 * Copyright (C) 2013 Jean-Pierre Charras, jp.charras at wanadoo.fr
 * Copyright (C) 2008-2013 Wayne Stambaugh <stambaughw@verizon.net>
 * Copyright (C) 2004-2013 KiCad Developers, see change_log.txt for contributors.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * 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
 */

26 27 28
/**
 * @file schedit.cpp
 */
plyatov's avatar
plyatov committed
29

30
#include <fctsys.h>
31
#include <kiway.h>
32
#include <gr_basic.h>
33
#include <pgm_base.h>
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#include <class_drawpanel.h>
#include <confirm.h>
#include <eda_doc.h>
#include <wxEeschemaStruct.h>
#include <kicad_device_context.h>
#include <hotkeys_basic.h>

#include <general.h>
#include <eeschema_id.h>
#include <protos.h>
#include <class_library.h>
#include <sch_bus_entry.h>
#include <sch_marker.h>
#include <sch_component.h>
#include <sch_junction.h>
#include <sch_line.h>
#include <sch_sheet.h>
51
#include <sch_sheet_path.h>
plyatov's avatar
plyatov committed
52

53

54
void SCH_EDIT_FRAME::Process_Special_Functions( wxCommandEvent& event )
plyatov's avatar
plyatov committed
55
{
56 57 58
    int         id = event.GetId();
    wxPoint     pos;
    SCH_SCREEN* screen = GetScreen();
59
    SCH_ITEM*   item = screen->GetCurItem();
60 61 62 63 64 65 66 67

    pos = wxGetMousePosition();

    pos.y += 20;

    // If needed, stop the current command and deselect current tool
    switch( id )
    {
68 69 70
    case wxID_CUT:
    case wxID_COPY:
    case ID_POPUP_CANCEL_CURRENT_COMMAND:
71 72
    case ID_POPUP_SCH_ENTRY_SELECT_SLASH:
    case ID_POPUP_SCH_ENTRY_SELECT_ANTISLASH:
73 74
    case ID_POPUP_SCH_BEGIN_WIRE:
    case ID_POPUP_SCH_BEGIN_BUS:
75 76 77 78 79
    case ID_POPUP_END_LINE:
    case ID_POPUP_SCH_SET_SHAPE_TEXT:
    case ID_POPUP_SCH_CLEANUP_SHEET:
    case ID_POPUP_SCH_END_SHEET:
    case ID_POPUP_SCH_RESIZE_SHEET:
charras's avatar
charras committed
80
    case ID_POPUP_IMPORT_GLABEL:
81 82 83 84 85 86 87 88 89 90 91 92 93 94
    case ID_POPUP_SCH_INIT_CMP:
    case ID_POPUP_SCH_DISPLAYDOC_CMP:
    case ID_POPUP_SCH_EDIT_CONVERT_CMP:
    case ID_POPUP_DELETE_BLOCK:
    case ID_POPUP_PLACE_BLOCK:
    case ID_POPUP_ZOOM_BLOCK:
    case ID_POPUP_DRAG_BLOCK:
    case ID_POPUP_COPY_BLOCK:
    case ID_POPUP_SCH_DELETE_NODE:
    case ID_POPUP_SCH_DELETE_CONNECTION:
    case ID_POPUP_SCH_ENTER_SHEET:
    case ID_POPUP_SCH_LEAVE_SHEET:
    case ID_POPUP_SCH_ADD_JUNCTION:
    case ID_POPUP_SCH_ADD_LABEL:
95
    case ID_POPUP_SCH_GETINFO_MARKER:
96

97 98 99
        /* At this point: Do nothing. these commands do not need to stop the
         * current command (mainly a block command) or reset the current state
         * They will be executed later, in next switch structure.
100 101 102
         */
        break;

103
    case ID_POPUP_SCH_DELETE_CMP:
104
    case ID_POPUP_SCH_DELETE:
105

charras's avatar
charras committed
106
        // Stop the current command (if any) but keep the current tool
107
        m_canvas->EndMouseCapture();
108 109
        break;

110
    default:
111

112
        // Stop the current command and deselect the current tool
113
        m_canvas->EndMouseCapture( ID_NO_TOOL_SELECTED, m_canvas->GetDefaultCursor() );
114 115 116
        break;
    }

117
    INSTALL_UNBUFFERED_DC( dc, m_canvas );
118
    item = screen->GetCurItem();    // Can be modified by previous calls.
119

120
    switch( id )
121 122 123
    {
    case ID_HIERARCHY:
        InstallHierarchyFrame( &dc, pos );
124
        SetRepeatItem( NULL );
125 126 127
        break;

    case wxID_CUT:
128
        if( screen->m_BlockLocate.GetCommand() != BLOCK_MOVE )
129
            break;
130

131 132 133
        screen->m_BlockLocate.SetCommand( BLOCK_DELETE );
        screen->m_BlockLocate.SetMessageBlock( this );
        HandleBlockEnd( &dc );
134
        SetRepeatItem( NULL );
135
        SetSheetNumberAndCount();
136 137 138
        break;

    case wxID_PASTE:
139
        HandleBlockBegin( &dc, BLOCK_PASTE, GetCrossHairPosition() );
140 141 142
        break;

    case ID_POPUP_SCH_ENTRY_SELECT_SLASH:
143
        m_canvas->MoveCursorToCrossHair();
144
        SetBusEntryShape( &dc, dynamic_cast<SCH_BUS_ENTRY_BASE*>( item ), '/' );
145 146 147
        break;

    case ID_POPUP_SCH_ENTRY_SELECT_ANTISLASH:
148
        m_canvas->MoveCursorToCrossHair();
149
        SetBusEntryShape( &dc, dynamic_cast<SCH_BUS_ENTRY_BASE*>( item ), '\\' );
150 151 152
        break;

    case ID_POPUP_CANCEL_CURRENT_COMMAND:
153
        if( m_canvas->IsMouseCaptured() )
154
        {
155 156
            m_canvas->EndMouseCapture();
            SetToolID( GetToolId(), m_canvas->GetCurrentCursor(), wxEmptyString );
157 158
        }
        else
159
        {
160
            SetToolID( ID_NO_TOOL_SELECTED, m_canvas->GetDefaultCursor(), wxEmptyString );
161 162
        }

163 164 165
        break;

    case ID_POPUP_END_LINE:
166
        m_canvas->MoveCursorToCrossHair();
167 168 169
        EndSegment( &dc );
        break;

170 171
    case ID_POPUP_SCH_BEGIN_WIRE:
        m_canvas->MoveCursorToCrossHair();
172
        OnLeftClick( &dc, GetCrossHairPosition() );
173
        break;
174

175 176
    case ID_POPUP_SCH_BEGIN_BUS:
        m_canvas->MoveCursorToCrossHair();
177
        OnLeftClick( &dc, GetCrossHairPosition() );
178 179 180
        break;

    case ID_POPUP_SCH_SET_SHAPE_TEXT:
181
        // Not used
182 183 184 185
        break;

    case ID_POPUP_SCH_DELETE_NODE:
    case ID_POPUP_SCH_DELETE_CONNECTION:
186
        m_canvas->MoveCursorToCrossHair();
187
        DeleteConnection( id == ID_POPUP_SCH_DELETE_CONNECTION );
188
        screen->SetCurItem( NULL );
189
        SetRepeatItem( NULL );
190 191
        screen->TestDanglingEnds( m_canvas, &dc );
        m_canvas->Refresh();
192 193 194
        break;

    case ID_POPUP_SCH_BREAK_WIRE:
195
        {
196
            DLIST< SCH_ITEM > oldWires;
197

198 199 200 201
            oldWires.SetOwnership( false );      // Prevent DLIST for deleting items in destructor.
            m_canvas->MoveCursorToCrossHair();
            screen->ExtractWires( oldWires, true );
            screen->BreakSegment( GetCrossHairPosition() );
202

203
            if( oldWires.GetCount() != 0 )
204
            {
205 206 207 208 209 210 211 212 213 214 215
                PICKED_ITEMS_LIST oldItems;

                oldItems.m_Status = UR_WIRE_IMAGE;

                while( oldWires.GetCount() != 0 )
                {
                    ITEM_PICKER picker = ITEM_PICKER( oldWires.PopFront(), UR_WIRE_IMAGE );
                    oldItems.PushItem( picker );
                }

                SaveCopyInUndoList( oldItems, UR_WIRE_IMAGE );
216 217
            }

218
            screen->TestDanglingEnds( m_canvas, &dc );
219
        }
220
        break;
221 222 223

    case ID_POPUP_SCH_DELETE_CMP:
    case ID_POPUP_SCH_DELETE:
224 225
        if( item == NULL )
            break;
226

227
        DeleteItem( item );
228
        screen->SetCurItem( NULL );
229
        SetRepeatItem( NULL );
230
        screen->TestDanglingEnds( m_canvas, &dc );
231 232
        SetSheetNumberAndCount();
        OnModify();
233
        break;
234 235

    case ID_POPUP_SCH_END_SHEET:
236
        m_canvas->MoveCursorToCrossHair();
237
        addCurrentItemToList( &dc );
238 239 240
        break;

    case ID_POPUP_SCH_RESIZE_SHEET:
241
        ReSizeSheet( (SCH_SHEET*) item, &dc );
242
        screen->TestDanglingEnds( m_canvas, &dc );
243 244
        break;

charras's avatar
charras committed
245
    case ID_POPUP_IMPORT_GLABEL:
246
        if( item != NULL && item->Type() == SCH_SHEET_T )
247
            screen->SetCurItem( ImportSheetPin( (SCH_SHEET*) item, &dc ) );
charras's avatar
charras committed
248
        break;
249 250

    case ID_POPUP_SCH_CLEANUP_SHEET:
251
        if( item != NULL && item->Type() == SCH_SHEET_T )
252
        {
253
            SCH_SHEET* sheet = (SCH_SHEET*) item;
254

255
            if( !sheet->HasUndefinedPins() )
256 257
            {
                DisplayInfoMessage( this,
258
                                    _( "There are no undefined labels in this sheet to clean up." ) );
259 260 261
                return;
            }

262
            if( !IsOK( this, _( "Do you wish to cleanup this sheet?" ) ) )
263 264 265 266 267 268
                return;

            /* Save sheet in undo list before cleaning up unreferenced hierarchical labels. */
            SaveCopyInUndoList( sheet, UR_CHANGED );
            sheet->CleanupSheet();
            OnModify();
269
            m_canvas->RefreshDrawingRect( sheet->GetBoundingBox() );
270
        }
271 272 273
        break;

    case ID_POPUP_SCH_INIT_CMP:
274
        m_canvas->MoveCursorToCrossHair();
275 276
        break;

277
    case ID_POPUP_SCH_EDIT_CONVERT_CMP:
278

279
        // Ensure the struct is a component (could be a struct of a component, like Field, text..)
280 281
        if( item && item->Type() == SCH_COMPONENT_T )
        {
282
            m_canvas->MoveCursorToCrossHair();
283 284
            ConvertPart( (SCH_COMPONENT*) item, &dc );
        }
285

286 287 288
        break;

    case ID_POPUP_SCH_DISPLAYDOC_CMP:
289

290
        // Ensure the struct is a component (could be a piece of a component, like Field, text..)
291
        if( item && item->Type() == SCH_COMPONENT_T )
292
        {
293
            if( PART_LIBS* libs = Prj().SchLibs() )
294
            {
295 296 297 298 299
                LIB_ALIAS* entry = libs->FindLibraryEntry( ( (SCH_COMPONENT*) item )->GetPartName() );

                if( entry && !!entry->GetDocFileName() )
                {
                    SEARCH_STACK* lib_search = Prj().SchSearchS();
300

301 302
                    GetAssociatedDocument( this, entry->GetDocFileName(), lib_search );
                }
303
            }
304 305 306 307
        }
        break;

    case ID_POPUP_SCH_ENTER_SHEET:
308

309
        if( item && (item->Type() == SCH_SHEET_T) )
310
        {
311 312
            m_CurrentSheet->Push( (SCH_SHEET*) item );
            DisplayCurrentSheet();
313
        }
314 315

        break;
316 317

    case ID_POPUP_SCH_LEAVE_SHEET:
318 319
        m_CurrentSheet->Pop();
        DisplayCurrentSheet();
320 321 322
        break;

    case wxID_COPY:         // really this is a Save block for paste
323 324 325
        screen->m_BlockLocate.SetCommand( BLOCK_SAVE );
        screen->m_BlockLocate.SetMessageBlock( this );
        HandleBlockEnd( &dc );
326 327 328
        break;

    case ID_POPUP_PLACE_BLOCK:
329
        m_canvas->SetAutoPanRequest( false );
330
        m_canvas->MoveCursorToCrossHair();
331 332 333 334
        HandleBlockPlace( &dc );
        break;

    case ID_POPUP_ZOOM_BLOCK:
335 336 337
        screen->m_BlockLocate.SetCommand( BLOCK_ZOOM );
        screen->m_BlockLocate.SetMessageBlock( this );
        HandleBlockEnd( &dc );
338 339 340
        break;

    case ID_POPUP_DELETE_BLOCK:
341
        m_canvas->MoveCursorToCrossHair();
342 343 344
        screen->m_BlockLocate.SetCommand( BLOCK_DELETE );
        screen->m_BlockLocate.SetMessageBlock( this );
        HandleBlockEnd( &dc );
345
        SetSheetNumberAndCount();
346 347 348
        break;

    case ID_POPUP_COPY_BLOCK:
349
        m_canvas->MoveCursorToCrossHair();
350 351 352
        screen->m_BlockLocate.SetCommand( BLOCK_COPY );
        screen->m_BlockLocate.SetMessageBlock( this );
        HandleBlockEnd( &dc );
353 354 355
        break;

    case ID_POPUP_DRAG_BLOCK:
356
        m_canvas->MoveCursorToCrossHair();
357 358 359
        screen->m_BlockLocate.SetCommand( BLOCK_DRAG );
        screen->m_BlockLocate.SetMessageBlock( this );
        HandleBlockEnd( &dc );
360 361 362
        break;

    case ID_POPUP_SCH_ADD_JUNCTION:
363
        m_canvas->MoveCursorToCrossHair();
364
        screen->SetCurItem( AddJunction( &dc, GetCrossHairPosition(), true ) );
365
        screen->TestDanglingEnds( m_canvas, &dc );
366
        screen->SetCurItem( NULL );
367 368 369 370
        break;

    case ID_POPUP_SCH_ADD_LABEL:
    case ID_POPUP_SCH_ADD_GLABEL:
371 372
        screen->SetCurItem( CreateNewText( &dc, id == ID_POPUP_SCH_ADD_LABEL ?
                                           LAYER_LOCLABEL : LAYER_GLOBLABEL ) );
373 374 375
        item = screen->GetCurItem();

        if( item )
376
            addCurrentItemToList( &dc );
377

378 379
        break;

380
    case ID_POPUP_SCH_GETINFO_MARKER:
381 382
        if( item && item->Type() == SCH_MARKER_T )
            ( (SCH_MARKER*) item )->DisplayMarkerInfo( this );
383

384 385
        break;

386
    default:        // Log error:
387 388
        wxFAIL_MSG( wxString::Format( wxT( "Cannot process command event ID %d" ),
                                      event.GetId() ) );
389 390 391
        break;
    }

392
    // End switch ( id )    (Command execution)
393

394
    if( GetToolId() == ID_NO_TOOL_SELECTED )
395
        SetRepeatItem( NULL );
plyatov's avatar
plyatov committed
396 397 398
}


399
void SCH_EDIT_FRAME::OnMoveItem( wxCommandEvent& aEvent )
plyatov's avatar
plyatov committed
400
{
401
    SCH_SCREEN* screen = GetScreen();
402
    SCH_ITEM*   item = screen->GetCurItem();
403

404 405 406 407 408 409
    if( screen->m_BlockLocate.GetState() != STATE_NO_BLOCK )
    {
        // trying to move an item when there is a block at the same time is not acceptable
        return;
    }

410
    if( item == NULL )
411 412 413 414 415
    {
        // If we didn't get here by a hot key, then something has gone wrong.
        if( aEvent.GetInt() == 0 )
            return;

416
        EDA_HOTKEY_CLIENT_DATA* data = (EDA_HOTKEY_CLIENT_DATA*) aEvent.GetClientObject();
417

418
        wxCHECK_RET( data != NULL, wxT( "Invalid hot key client object." ) );
419 420 421 422 423 424 425 426

        item = LocateAndShowItem( data->GetPosition(), SCH_COLLECTOR::MovableItems,
                                  aEvent.GetInt() );

        // Exit if no item found at the current location or the item is already being edited.
        if( (item == NULL) || (item->GetFlags() != 0) )
            return;
    }
427

428
    INSTALL_UNBUFFERED_DC( dc, m_canvas );
429 430

    switch( item->Type() )
431
    {
432
    case SCH_LINE_T:
433 434
        break;

435 436
    case SCH_JUNCTION_T:
    case SCH_NO_CONNECT_T:
437 438
    case SCH_BUS_BUS_ENTRY_T:
    case SCH_BUS_WIRE_ENTRY_T:
439 440 441 442
    case SCH_LABEL_T:
    case SCH_GLOBAL_LABEL_T:
    case SCH_HIERARCHICAL_LABEL_T:
    case SCH_TEXT_T:
443
    case SCH_COMPONENT_T:
444
    case SCH_SHEET_PIN_T:
445
    case SCH_FIELD_T:
446
        MoveItem( item, &dc );
447 448
        break;

449 450 451 452
    case SCH_BITMAP_T:
        MoveImage( (SCH_BITMAP*) item, &dc );
        break;

453
    case SCH_SHEET_T:
454
        StartMoveSheet( (SCH_SHEET*) item, &dc );
455 456
        break;

457
    case SCH_MARKER_T:
458
    default:
459 460
        wxFAIL_MSG( wxString::Format( wxT( "Cannot move item type %s" ),
                                      GetChars( item->GetClass() ) ) );
461 462
        break;
    }
463 464

    if( GetToolId() == ID_NO_TOOL_SELECTED )
465
        SetRepeatItem( NULL );
plyatov's avatar
plyatov committed
466
}
467 468 469 470 471 472 473 474


void SCH_EDIT_FRAME::OnCancelCurrentCommand( wxCommandEvent& aEvent )
{
    SCH_SCREEN* screen = GetScreen();

    if( screen->IsBlockActive() )
    {
475
        m_canvas->SetCursor( (wxStockCursor) m_canvas->GetDefaultCursor() );
476 477 478
        screen->ClearBlockCommand();

        // Stop the current command (if any) but keep the current tool
479
        m_canvas->EndMouseCapture();
480 481 482
    }
    else
    {
483 484
        if( m_canvas->IsMouseCaptured() ) // Stop the current command but keep the current tool
            m_canvas->EndMouseCapture();
485
        else                    // Deselect current tool
486
            m_canvas->EndMouseCapture( ID_NO_TOOL_SELECTED, m_canvas->GetDefaultCursor() );
487
     }
488
}
489 490 491 492


void SCH_EDIT_FRAME::OnSelectTool( wxCommandEvent& aEvent )
{
493
    int id = aEvent.GetId();
494 495

    // Stop the current command and deselect the current tool.
496
    m_canvas->EndMouseCapture( ID_NO_TOOL_SELECTED, m_canvas->GetDefaultCursor() );
497 498 499

    switch( id )
    {
500
    case ID_NO_TOOL_SELECTED:
501
        SetToolID( id, m_canvas->GetDefaultCursor(), _( "No tool selected" ) );
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
        break;

    case ID_HIERARCHY_PUSH_POP_BUTT:
        SetToolID( id, wxCURSOR_HAND, _( "Descend or ascend hierarchy" ) );
        break;

    case ID_NOCONN_BUTT:
        SetToolID( id, wxCURSOR_PENCIL, _( "Add no connect" ) );
        break;

    case ID_WIRE_BUTT:
        SetToolID( id, wxCURSOR_PENCIL, _( "Add wire" ) );
        break;

    case ID_BUS_BUTT:
        SetToolID( id, wxCURSOR_PENCIL, _( "Add bus" ) );
        break;

    case ID_LINE_COMMENT_BUTT:
        SetToolID( id, wxCURSOR_PENCIL, _( "Add lines" ) );
        break;

    case ID_JUNCTION_BUTT:
        SetToolID( id, wxCURSOR_PENCIL, _( "Add junction" ) );
        break;

    case ID_LABEL_BUTT:
        SetToolID( id, wxCURSOR_PENCIL, _( "Add label" ) );
        break;

    case ID_GLABEL_BUTT:
        SetToolID( id, wxCURSOR_PENCIL, _( "Add global label" ) );
        break;

    case ID_HIERLABEL_BUTT:
        SetToolID( id, wxCURSOR_PENCIL, _( "Add hierarchical label" ) );
        break;

    case ID_TEXT_COMMENT_BUTT:
        SetToolID( id, wxCURSOR_PENCIL, _( "Add text" ) );
        break;

544 545 546 547
    case ID_ADD_IMAGE_BUTT:
        SetToolID( id, wxCURSOR_PENCIL, _( "Add image" ) );
        break;

548 549 550 551 552 553 554 555 556 557 558 559
    case ID_WIRETOBUS_ENTRY_BUTT:
        SetToolID( id, wxCURSOR_PENCIL, _( "Add wire to bus entry" ) );
        break;

    case ID_BUSTOBUS_ENTRY_BUTT:
        SetToolID( id, wxCURSOR_PENCIL, _( "Add bus to bus entry" ) );
        break;

    case ID_SHEET_SYMBOL_BUTT:
        SetToolID( id, wxCURSOR_PENCIL, _( "Add sheet" ) );
        break;

560
    case ID_SHEET_PIN_BUTT:
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
        SetToolID( id, wxCURSOR_PENCIL, _( "Add sheet pins" ) );
        break;

    case ID_IMPORT_HLABEL_BUTT:
        SetToolID( id, wxCURSOR_PENCIL, _( "Import sheet pins" ) );
        break;

    case ID_SCH_PLACE_COMPONENT:
        SetToolID( id, wxCURSOR_PENCIL, _( "Add component" ) );
        break;

    case ID_PLACE_POWER_BUTT:
        SetToolID( id, wxCURSOR_PENCIL, _( "Add power" ) );
        break;

    case ID_SCHEMATIC_DELETE_ITEM_BUTT:
        SetToolID( id, wxCURSOR_BULLSEYE, _( "Delete item" ) );
        break;

    default:
581
        SetRepeatItem( NULL );
582
    }
583 584

    // Simulate left click event if we got here from a hot key.
585
    if( aEvent.GetClientObject() != NULL )
586
    {
587
        EDA_HOTKEY_CLIENT_DATA* data = (EDA_HOTKEY_CLIENT_DATA*) aEvent.GetClientObject();
588 589 590

        wxPoint pos = data->GetPosition();

591
        INSTALL_UNBUFFERED_DC( dc, m_canvas );
592 593
        OnLeftClick( &dc, pos );
    }
594 595 596 597 598
}


void SCH_EDIT_FRAME::OnUpdateSelectTool( wxUpdateUIEvent& aEvent )
{
599
    if( aEvent.GetEventObject() == m_drawToolBar )
600
        aEvent.Check( GetToolId() == aEvent.GetId() );
601
}
602 603 604 605


void SCH_EDIT_FRAME::DeleteConnection( bool aFullConnection )
{
606 607
    PICKED_ITEMS_LIST   pickList;
    SCH_SCREEN*         screen = GetScreen();
608
    wxPoint             pos = GetCrossHairPosition();
609 610 611

    if( screen->GetConnection( pos, pickList, aFullConnection ) != 0 )
    {
612
        DeleteItemsInList( m_canvas, pickList );
613 614 615 616 617 618 619
        OnModify();
    }
}


bool SCH_EDIT_FRAME::DeleteItemAtCrossHair( wxDC* DC )
{
620
    SCH_ITEM*   item;
621 622
    SCH_SCREEN* screen = GetScreen();

623
    item = LocateItem( GetCrossHairPosition(), SCH_COLLECTOR::ParentItems );
624 625 626 627 628 629 630 631 632 633

    if( item )
    {
        bool itemHasConnections = item->IsConnectable();

        screen->SetCurItem( NULL );
        SetRepeatItem( NULL );
        DeleteItem( item );

        if( itemHasConnections )
634
            screen->TestDanglingEnds( m_canvas, DC );
635 636 637 638 639 640 641

        OnModify();
        return true;
    }

    return false;
}
642 643 644 645 646


static void moveItem( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wxPoint& aPosition, bool aErase )
{
    SCH_SCREEN* screen = (SCH_SCREEN*) aPanel->GetScreen();
647
    SCH_ITEM*   item   = screen->GetCurItem();
648 649 650

    wxCHECK_RET( (item != NULL), wxT( "Cannot move invalid schematic item." ) );

651
#ifndef USE_WX_OVERLAY
652 653 654
    // Erase the current item at its current position.
    if( aErase )
        item->Draw( aPanel, aDC, wxPoint( 0, 0 ), g_XorMode );
655
#endif
656

657
    item->SetPosition( aPanel->GetParent()->GetCrossHairPosition() );
658 659

    // Draw the item item at it's new position.
660
    item->SetWireImage();  // While moving, the item may choose to render differently
661 662 663 664 665 666
    item->Draw( aPanel, aDC, wxPoint( 0, 0 ), g_XorMode );
}


static void abortMoveItem( EDA_DRAW_PANEL* aPanel, wxDC* aDC )
{
667 668
    SCH_SCREEN*     screen = (SCH_SCREEN*) aPanel->GetScreen();
    SCH_ITEM*       item = screen->GetCurItem();
669
    SCH_EDIT_FRAME* parent = (SCH_EDIT_FRAME*) aPanel->GetParent();
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704

    parent->SetRepeatItem( NULL );
    screen->SetCurItem( NULL );

    if( item == NULL )  /* no current item */
        return;

    if( item->IsNew() )
    {
        delete item;
        item = NULL;
    }
    else
    {
        SCH_ITEM* oldItem = parent->GetUndoItem();

        SCH_ITEM* currentItem;

        // Items that are children of other objects are undone by swapping the contents
        // of the parent items.
        if( (item->Type() == SCH_SHEET_PIN_T) || (item->Type() == SCH_FIELD_T) )
        {
            currentItem = (SCH_ITEM*) item->GetParent();
        }
        else
        {
            currentItem = item;
        }

        wxCHECK_RET( oldItem != NULL && currentItem->Type() == oldItem->Type(),
                     wxT( "Cannot restore undefined or bad last schematic item." ) );

        // Never delete existing item, because it can be referenced by an undo/redo command
        // Just restore its data
        currentItem->SwapData( oldItem );
705 706 707

        // Erase the wire representation before the 'normal' view is drawn.
        if ( item->IsWireImage() )
708
            item->Draw( aPanel, aDC, wxPoint( 0, 0 ), g_XorMode );
709

710 711 712 713 714 715 716 717 718 719 720
        item->ClearFlags();
    }

    aPanel->Refresh();
}


void SCH_EDIT_FRAME::MoveItem( SCH_ITEM* aItem, wxDC* aDC )
{
    wxCHECK_RET( aItem != NULL, wxT( "Cannot move invalid schematic item" ) );

721
    SetRepeatItem( NULL );
722 723 724 725 726 727 728 729 730 731

    if( !aItem->IsNew() )
    {
        if( (aItem->Type() == SCH_SHEET_PIN_T) || (aItem->Type() == SCH_FIELD_T) )
            SetUndoItem( (SCH_ITEM*) aItem->GetParent() );
        else
            SetUndoItem( aItem );
    }

    aItem->SetFlags( IS_MOVED );
732 733 734 735
#ifdef USE_WX_OVERLAY
    this->Refresh();
    this->Update();
#endif
736
    m_canvas->CrossHairOff( aDC );
737 738

    if( aItem->Type() != SCH_SHEET_PIN_T )
739
        SetCrossHairPosition( aItem->GetPosition() );
740

741
    m_canvas->MoveCursorToCrossHair();
742 743

    OnModify();
744
    m_canvas->SetMouseCapture( moveItem, abortMoveItem );
745
    GetScreen()->SetCurItem( aItem );
746 747
    moveItem( m_canvas, aDC, wxDefaultPosition, true );
    m_canvas->CrossHairOn( aDC );
748
}
749 750 751 752 753


void SCH_EDIT_FRAME::OnRotate( wxCommandEvent& aEvent )
{
    SCH_SCREEN* screen = GetScreen();
754
    SCH_ITEM*   item = screen->GetCurItem();
755

756
    INSTALL_UNBUFFERED_DC( dc, m_canvas );
757

758 759
    // Allows block rotate operation on hot key.
    if( screen->m_BlockLocate.GetState() != STATE_NO_BLOCK )
760
    {
761 762
        screen->m_BlockLocate.SetCommand( BLOCK_ROTATE );
        HandleBlockEnd( &dc );
763 764
        return;
    }
765

766 767
    if( item == NULL )
    {
768 769 770 771
        // If we didn't get here by a hot key, then something has gone wrong.
        if( aEvent.GetInt() == 0 )
            return;

772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
        EDA_HOTKEY_CLIENT_DATA* data = (EDA_HOTKEY_CLIENT_DATA*) aEvent.GetClientObject();

        wxCHECK_RET( data != NULL, wxT( "Invalid hot key client object." ) );

        item = LocateAndShowItem( data->GetPosition(), SCH_COLLECTOR::RotatableItems,
                                  aEvent.GetInt() );

        // Exit if no item found at the current location or the item is already being edited.
        if( (item == NULL) || (item->GetFlags() != 0) )
            return;
    }

    switch( item->Type() )
    {
    case SCH_COMPONENT_T:
787
        if( aEvent.GetId() == ID_SCH_ROTATE_CLOCKWISE )
788
            OrientComponent( CMP_ROTATE_CLOCKWISE );
789 790 791 792 793 794
        else if( aEvent.GetId() == ID_SCH_ROTATE_COUNTERCLOCKWISE )
            OrientComponent( CMP_ROTATE_COUNTERCLOCKWISE );
        else
            wxFAIL_MSG( wxT( "Unknown rotate item command ID." ) );

        break;
795 796 797 798 799

    case SCH_TEXT_T:
    case SCH_LABEL_T:
    case SCH_GLOBAL_LABEL_T:
    case SCH_HIERARCHICAL_LABEL_T:
800
        m_canvas->MoveCursorToCrossHair();
801 802 803 804
        ChangeTextOrient( (SCH_TEXT*) item, &dc );
        break;

    case SCH_FIELD_T:
805
        m_canvas->MoveCursorToCrossHair();
806 807 808 809 810 811 812 813 814
        RotateField( (SCH_FIELD*) item, &dc );
        break;

    case SCH_BITMAP_T:
        RotateImage( (SCH_BITMAP*) item );
        break;

    case SCH_SHEET_T:           /// @todo allow sheet rotate on hotkey
    default:
815 816 817 818 819 820 821 822 823 824 825 826 827 828
        wxFAIL_MSG( wxString::Format( wxT( "Cannot rotate schematic item type %s." ),
                                      GetChars( item->GetClass() ) ) );
    }

    if( item->GetFlags() == 0 )
        screen->SetCurItem( NULL );
}


void SCH_EDIT_FRAME::OnEditItem( wxCommandEvent& aEvent )
{
    SCH_SCREEN* screen = GetScreen();
    SCH_ITEM* item = screen->GetCurItem();

829
    INSTALL_UNBUFFERED_DC( dc, m_canvas );
830 831 832 833 834 835 836 837 838 839 840

    if( item == NULL )
    {
        // If we didn't get here by a hot key, then something has gone wrong.
        if( aEvent.GetInt() == 0 )
            return;

        EDA_HOTKEY_CLIENT_DATA* data = (EDA_HOTKEY_CLIENT_DATA*) aEvent.GetClientObject();

        wxCHECK_RET( data != NULL, wxT( "Invalid hot key client object." ) );

841 842 843
        // Set the locat filter, according to the edit command
        const KICAD_T* filterList = SCH_COLLECTOR::EditableItems;
        const KICAD_T* filterListAux = NULL;
844

845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
        switch( aEvent.GetId() )
        {
        case ID_SCH_EDIT_COMPONENT_REFERENCE:
            filterList = SCH_COLLECTOR::CmpFieldReferenceOnly;
            filterListAux = SCH_COLLECTOR::ComponentsOnly;
            break;

        case ID_SCH_EDIT_COMPONENT_VALUE:
            filterList = SCH_COLLECTOR::CmpFieldValueOnly;
            filterListAux = SCH_COLLECTOR::ComponentsOnly;
            break;

        case ID_SCH_EDIT_COMPONENT_FOOTPRINT:
            filterList = SCH_COLLECTOR::CmpFieldFootprintOnly;
            filterListAux = SCH_COLLECTOR::ComponentsOnly;
            break;

        default:
            break;
        }
865 866

        item = LocateAndShowItem( data->GetPosition(), filterList, aEvent.GetInt() );
867 868 869

        // If no item found, and if an auxiliary filter exists, try to use it
        if( !item && filterListAux )
870
            item = LocateAndShowItem( data->GetPosition(), filterListAux, aEvent.GetInt() );
871 872 873 874 875 876 877 878 879 880 881 882 883

        // Exit if no item found at the current location or the item is already being edited.
        if( (item == NULL) || (item->GetFlags() != 0) )
            return;
    }

    switch( item->Type() )
    {
    case SCH_COMPONENT_T:
    {
        switch( aEvent.GetId() )
        {
        case ID_SCH_EDIT_COMPONENT_REFERENCE:
884
            EditComponentFieldText( ( (SCH_COMPONENT*) item )->GetField( REFERENCE ) );
885 886 887
            break;

        case ID_SCH_EDIT_COMPONENT_VALUE:
888
            EditComponentFieldText( ( (SCH_COMPONENT*) item )->GetField( VALUE ) );
889 890 891
            break;

        case ID_SCH_EDIT_COMPONENT_FOOTPRINT:
892
            EditComponentFieldText( ( (SCH_COMPONENT*) item )->GetField( FOOTPRINT ) );
893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
            break;

        case ID_SCH_EDIT_ITEM:
            EditComponent( (SCH_COMPONENT*) item );
            break;

        default:
            wxFAIL_MSG( wxString::Format( wxT( "Invalid schematic component edit command ID %d" ),
                                          aEvent.GetId() ) );
        }

        break;
    }

    case SCH_SHEET_T:
        EditSheet( (SCH_SHEET*) item, &dc );
909
        break;
910 911 912 913 914 915 916 917 918 919 920 921 922

    case SCH_SHEET_PIN_T:
        EditSheetPin( (SCH_SHEET_PIN*) item, &dc );
        break;

    case SCH_TEXT_T:
    case SCH_LABEL_T:
    case SCH_GLOBAL_LABEL_T:
    case SCH_HIERARCHICAL_LABEL_T:
        EditSchematicText( (SCH_TEXT*) item );
        break;

    case SCH_FIELD_T:
923
        EditComponentFieldText( (SCH_FIELD*) item );
924 925 926 927 928 929 930 931 932
        break;

    case SCH_BITMAP_T:
        EditImage( (SCH_BITMAP*) item );
        break;

    default:
        wxFAIL_MSG( wxString::Format( wxT( "Cannot edit schematic item type %s." ),
                                      GetChars( item->GetClass() ) ) );
933 934 935 936 937
    }

    if( item->GetFlags() == 0 )
        screen->SetCurItem( NULL );
}
938 939 940 941 942 943 944


void SCH_EDIT_FRAME::OnDragItem( wxCommandEvent& aEvent )
{
    SCH_SCREEN* screen = GetScreen();
    SCH_ITEM* item = screen->GetCurItem();

945 946 947 948 949 950 951 952
    // The easiest way to handle a menu or a hot key drag command
    // is to simulate a block drag command
    //
    // When a drag item is requested, some items use a BLOCK_DRAG_ITEM drag type
    // an some items use a BLOCK_DRAG drag type  (mainly a junction)
    // a BLOCK_DRAG collects all items in a block (here a 2x2 rect centered on the cursor)
    // and BLOCK_DRAG_ITEM drag only the selected item
    BLOCK_COMMAND_T dragType = BLOCK_DRAG_ITEM;
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969

    if( item == NULL )
    {
        // If we didn't get here by a hot key, then something has gone wrong.
        if( aEvent.GetInt() == 0 )
            return;

        EDA_HOTKEY_CLIENT_DATA* data = (EDA_HOTKEY_CLIENT_DATA*) aEvent.GetClientObject();

        wxCHECK_RET( data != NULL, wxT( "Invalid hot key client object." ) );

        item = LocateAndShowItem( data->GetPosition(), SCH_COLLECTOR::DraggableItems,
                                  aEvent.GetInt() );

        // Exit if no item found at the current location or the item is already being edited.
        if( (item == NULL) || (item->GetFlags() != 0) )
            return;
970 971 972 973 974

        // When a junction or a node is found, a BLOCK_DRAG is better
        if( m_collectedItems.IsCorner() || m_collectedItems.IsNode( false )
            || m_collectedItems.IsDraggableJunction() )
            dragType = BLOCK_DRAG;
975 976 977 978
    }

    switch( item->Type() )
    {
Dick Hollenbeck's avatar
Dick Hollenbeck committed
979 980 981 982 983 984 985 986 987 988
    case SCH_BUS_BUS_ENTRY_T:
    case SCH_BUS_WIRE_ENTRY_T:
    case SCH_LINE_T:
    case SCH_JUNCTION_T:
    case SCH_COMPONENT_T:
    case SCH_LABEL_T:
    case SCH_GLOBAL_LABEL_T:
    case SCH_HIERARCHICAL_LABEL_T:
    case SCH_SHEET_T:
        m_canvas->MoveCursorToCrossHair();
989

Dick Hollenbeck's avatar
Dick Hollenbeck committed
990 991 992
        if( screen->m_BlockLocate.GetState() == STATE_NO_BLOCK )
        {
            INSTALL_UNBUFFERED_DC( dc, m_canvas );
993

Dick Hollenbeck's avatar
Dick Hollenbeck committed
994 995
            if( !HandleBlockBegin( &dc, dragType, GetCrossHairPosition() ) )
                break;
996

Dick Hollenbeck's avatar
Dick Hollenbeck committed
997 998 999 1000
            // Give a non null size to the search block:
            screen->m_BlockLocate.Inflate( 1 );
            HandleBlockEnd( &dc );
        }
1001

Dick Hollenbeck's avatar
Dick Hollenbeck committed
1002
        break;
1003

Dick Hollenbeck's avatar
Dick Hollenbeck committed
1004 1005 1006
    default:
        wxFAIL_MSG( wxString::Format( wxT( "Cannot drag schematic item type %s." ),
                                      GetChars( item->GetClass() ) ) );
1007 1008
    }
}
1009 1010 1011 1012 1013


void SCH_EDIT_FRAME::OnOrient( wxCommandEvent& aEvent )
{
    SCH_SCREEN* screen = GetScreen();
1014
    SCH_ITEM*   item   = screen->GetCurItem();
1015

1016
    INSTALL_UNBUFFERED_DC( dc, m_canvas );
1017

1018 1019
    // Allows block rotate operation on hot key.
    if( screen->m_BlockLocate.GetState() != STATE_NO_BLOCK )
1020
    {
1021
        if( aEvent.GetId() == ID_SCH_MIRROR_X )
1022 1023 1024 1025 1026 1027
        {
            m_canvas->MoveCursorToCrossHair();
            screen->m_BlockLocate.SetMessageBlock( this );
            screen->m_BlockLocate.SetCommand( BLOCK_MIRROR_X );
            HandleBlockEnd( &dc );
        }
1028
        else if( aEvent.GetId() == ID_SCH_MIRROR_Y )
1029 1030 1031 1032 1033 1034
        {
            m_canvas->MoveCursorToCrossHair();
            screen->m_BlockLocate.SetMessageBlock( this );
            screen->m_BlockLocate.SetCommand( BLOCK_MIRROR_Y );
            HandleBlockEnd( &dc );
        }
1035
        else
1036
        {
1037
            wxFAIL_MSG( wxT( "Unknown block oriention command ID." ) );
1038
        }
1039

1040 1041
        return;
    }
1042

1043 1044
    if( item == NULL )
    {
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
        // If we didn't get here by a hot key, then something has gone wrong.
        if( aEvent.GetInt() == 0 )
            return;

        EDA_HOTKEY_CLIENT_DATA* data = (EDA_HOTKEY_CLIENT_DATA*) aEvent.GetClientObject();

        wxCHECK_RET( data != NULL, wxT( "Invalid hot key client object." ) );

        item = LocateAndShowItem( data->GetPosition(), SCH_COLLECTOR::OrientableItems,
                                  aEvent.GetInt() );

        // Exit if no item found at the current location or the item is already being edited.
        if( (item == NULL) || (item->GetFlags() != 0) )
            return;
    }


    switch( item->Type() )
    {
    case SCH_COMPONENT_T:
        if( aEvent.GetId() == ID_SCH_MIRROR_X )
            OrientComponent( CMP_MIRROR_X );
        else if( aEvent.GetId() == ID_SCH_MIRROR_Y )
            OrientComponent( CMP_MIRROR_Y );
        else if( aEvent.GetId() == ID_SCH_ORIENT_NORMAL )
            OrientComponent( CMP_NORMAL );
        else
            wxFAIL_MSG( wxT( "Invalid orient schematic component command ID." ) );

        break;

    case SCH_BITMAP_T:
        if( aEvent.GetId() == ID_SCH_MIRROR_X )
            MirrorImage( (SCH_BITMAP*) item, true );
        else if( aEvent.GetId() == ID_SCH_MIRROR_Y )
            MirrorImage( (SCH_BITMAP*) item, false );

        break;

    default:
        wxFAIL_MSG( wxString::Format( wxT( "Schematic object type %s cannot be oriented." ),
                                      GetChars( item->GetClass() ) ) );
    }

    if( item->GetFlags() == 0 )
        screen->SetCurItem( NULL );
}