From d1af5210e02bd2de25b603684431b3db91f52cc0 Mon Sep 17 00:00:00 2001 From: Seth Hillbrand Date: Wed, 4 Apr 2018 10:19:17 -0700 Subject: [PATCH] Libedit: Fix a few places where item could be NULL Fixes: lp:1761058 * https://bugs.launchpad.net/kicad/+bug/1761058 --- eeschema/hotkeys.cpp | 18 ++++++++++++------ eeschema/lib_edit_frame.cpp | 21 ++++++++++----------- eeschema/lib_edit_frame.h | 15 +++++++++++---- eeschema/libedit_onleftclick.cpp | 3 +-- eeschema/pinedit.cpp | 14 ++++---------- eeschema/symbdraw.cpp | 20 ++++++++------------ 6 files changed, 46 insertions(+), 45 deletions(-) diff --git a/eeschema/hotkeys.cpp b/eeschema/hotkeys.cpp index bba439b6ee..8ca6753fa7 100644 --- a/eeschema/hotkeys.cpp +++ b/eeschema/hotkeys.cpp @@ -804,7 +804,7 @@ bool LIB_EDIT_FRAME::OnHotKey( wxDC* aDC, int aHotKey, const wxPoint& aPosition, break; case HK_LIBEDIT_MOVE_GRAPHIC_ITEM: - if( !itemInEdit ) + if( !itemInEdit && !blocInProgress ) { SetDrawItem( LocateItemUsingCursor( aPosition ) ); @@ -817,7 +817,7 @@ bool LIB_EDIT_FRAME::OnHotKey( wxDC* aDC, int aHotKey, const wxPoint& aPosition, break; case HK_DRAG: - if( !itemInEdit ) + if( !itemInEdit && !blocInProgress ) { SetDrawItem( LocateItemUsingCursor( aPosition ) ); @@ -833,16 +833,22 @@ bool LIB_EDIT_FRAME::OnHotKey( wxDC* aDC, int aHotKey, const wxPoint& aPosition, if( !itemInEdit ) SetDrawItem( LocateItemUsingCursor( aPosition ) ); - cmd.SetId( ID_LIBEDIT_MIRROR_Y ); - GetEventHandler()->ProcessEvent( cmd ); + if( GetDrawItem() ) + { + cmd.SetId( ID_LIBEDIT_MIRROR_Y ); + GetEventHandler()->ProcessEvent( cmd ); + } break; case HK_MIRROR_X: // Mirror X if( !itemInEdit ) SetDrawItem( LocateItemUsingCursor( aPosition ) ); - cmd.SetId( ID_LIBEDIT_MIRROR_X ); - GetEventHandler()->ProcessEvent( cmd ); + if( GetDrawItem() ) + { + cmd.SetId( ID_LIBEDIT_MIRROR_X ); + GetEventHandler()->ProcessEvent( cmd ); + } break; } diff --git a/eeschema/lib_edit_frame.cpp b/eeschema/lib_edit_frame.cpp index 0ddeb9e83b..042120852c 100644 --- a/eeschema/lib_edit_frame.cpp +++ b/eeschema/lib_edit_frame.cpp @@ -835,7 +835,7 @@ void LIB_EDIT_FRAME::Process_Special_Functions( wxCommandEvent& event ) case ID_POPUP_LIBEDIT_DELETE_ITEM: if( item ) - deleteItem( &dc ); + deleteItem( &dc, item ); break; @@ -844,9 +844,9 @@ void LIB_EDIT_FRAME::Process_Special_Functions( wxCommandEvent& event ) break; if( item->Type() == LIB_PIN_T ) - StartMovePin( &dc ); + StartMovePin( item ); else - StartMoveDrawSymbol( &dc ); + StartMoveDrawSymbol( &dc, item ); break; case ID_POPUP_LIBEDIT_MODIFY_ITEM: @@ -861,7 +861,7 @@ void LIB_EDIT_FRAME::Process_Special_Functions( wxCommandEvent& event ) || item->Type() == LIB_ARC_T ) { - StartModifyDrawSymbol( &dc ); + StartModifyDrawSymbol( &dc, item ); } break; @@ -1377,11 +1377,10 @@ LIB_ITEM* LIB_EDIT_FRAME::locateItem( const wxPoint& aPosition, const KICAD_T aF } -void LIB_EDIT_FRAME::deleteItem( wxDC* aDC ) +void LIB_EDIT_FRAME::deleteItem( wxDC* aDC, LIB_ITEM* aItem ) { - LIB_ITEM* item = GetDrawItem(); - - wxCHECK_RET( item != NULL, "No drawing item selected to delete." ); + if( !aItem ) + return; m_canvas->CrossHairOff( aDC ); @@ -1389,9 +1388,9 @@ void LIB_EDIT_FRAME::deleteItem( wxDC* aDC ) SaveCopyInUndoList( part ); - if( item->Type() == LIB_PIN_T ) + if( aItem->Type() == LIB_PIN_T ) { - LIB_PIN* pin = static_cast( item ); + LIB_PIN* pin = static_cast( aItem ); wxPoint pos = pin->GetPosition(); part->RemoveDrawItem( (LIB_ITEM*) pin, m_canvas, aDC ); @@ -1428,7 +1427,7 @@ void LIB_EDIT_FRAME::deleteItem( wxDC* aDC ) } else { - part->RemoveDrawItem( item, m_canvas, aDC ); + part->RemoveDrawItem( aItem, m_canvas, aDC ); m_canvas->Refresh(); } } diff --git a/eeschema/lib_edit_frame.h b/eeschema/lib_edit_frame.h index 17cbd69b45..44331d76d6 100644 --- a/eeschema/lib_edit_frame.h +++ b/eeschema/lib_edit_frame.h @@ -551,8 +551,9 @@ private: * Deletes the currently selected draw item. * * @param aDC The device context to draw upon when removing item. + * @param aItem The item to delete */ - void deleteItem( wxDC* aDC ); + void deleteItem( wxDC* aDC, LIB_ITEM* aItem ); // General editing public: @@ -570,7 +571,13 @@ private: // Editing pins void CreatePin( wxDC* DC ); - void StartMovePin( wxDC* DC ); + + /** + * Prepare the displacement of a pin + * + * @param aPin the pin to prepare for movement. + */ + void StartMovePin( LIB_ITEM* aPin ); /** * Adds copies of \a aPin in components with multiple units in all units @@ -590,8 +597,8 @@ private: // Editing graphic items LIB_ITEM* CreateGraphicItem( LIB_PART* LibEntry, wxDC* DC ); void GraphicItemBeginDraw( wxDC* DC ); - void StartMoveDrawSymbol( wxDC* DC ); - void StartModifyDrawSymbol( wxDC* DC ); //GetFlags() & IS_CANCELLED ) { - deleteItem( DC ); + deleteItem( DC, pin ); } else { diff --git a/eeschema/symbdraw.cpp b/eeschema/symbdraw.cpp index 72f9e7a2d5..b30a856182 100644 --- a/eeschema/symbdraw.cpp +++ b/eeschema/symbdraw.cpp @@ -292,11 +292,9 @@ static void RedrawWhileMovingCursor( EDA_DRAW_PANEL* aPanel, wxDC* aDC, const wx } -void LIB_EDIT_FRAME::StartMoveDrawSymbol( wxDC* DC ) +void LIB_EDIT_FRAME::StartMoveDrawSymbol( wxDC* DC, LIB_ITEM* aItem ) { - LIB_ITEM* item = GetDrawItem(); - - if( item == NULL ) + if( aItem == NULL ) return; SetCursor( wxCURSOR_HAND ); @@ -305,25 +303,23 @@ void LIB_EDIT_FRAME::StartMoveDrawSymbol( wxDC* DC ) // For fields only, move the anchor point of the field // to the cursor position to allow user to see the text justification - if( item->Type() == LIB_FIELD_T ) - item->BeginEdit( IS_MOVED, item->GetPosition() ); + if( aItem->Type() == LIB_FIELD_T ) + aItem->BeginEdit( IS_MOVED, aItem->GetPosition() ); else - item->BeginEdit( IS_MOVED, GetCrossHairPosition( true ) ); + aItem->BeginEdit( IS_MOVED, GetCrossHairPosition( true ) ); m_canvas->SetMouseCapture( RedrawWhileMovingCursor, AbortSymbolTraceOn ); m_canvas->CallMouseCapture( DC, wxDefaultPosition, true ); } -void LIB_EDIT_FRAME::StartModifyDrawSymbol( wxDC* DC ) +void LIB_EDIT_FRAME::StartModifyDrawSymbol( wxDC* DC, LIB_ITEM* aItem ) { - LIB_ITEM* item = GetDrawItem(); - - if( item == NULL ) + if( aItem == NULL ) return; TempCopyComponent(); - item->BeginEdit( IS_RESIZED, GetCrossHairPosition( true ) ); + aItem->BeginEdit( IS_RESIZED, GetCrossHairPosition( true ) ); m_canvas->SetMouseCapture( SymbolDisplayDraw, AbortSymbolTraceOn ); m_canvas->CallMouseCapture( DC, wxDefaultPosition, true ); }