kicad/pcbnew/controle.cpp

377 lines
11 KiB
C++
Raw Normal View History

/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2009 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
* Copyright (C) 2011 Wayne Stambaugh <stambaughw@verizon.net>
* Copyright (C) 1992-2011 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
*/
/**
* @file pcbnew/controle.cpp
*/
2007-05-06 16:03:28 +00:00
#include "fctsys.h"
#include "class_drawpanel.h"
2009-07-30 11:04:07 +00:00
#include "wxPcbStruct.h"
#include "pcbcommon.h"
#include "macros.h"
#include "pcbnew_id.h"
#include "class_board.h"
#include "class_module.h"
#include "pcbnew.h"
#include "protos.h"
#include "collectors.h"
2007-05-06 16:03:28 +00:00
//external functions used here:
extern bool Magnetize( BOARD* m_Pcb, PCB_EDIT_FRAME* frame,
int aCurrentTool, wxSize grid, wxPoint on_grid, wxPoint* curpos );
2007-05-06 16:03:28 +00:00
2007-09-14 16:15:27 +00:00
/**
* Function AllAreModulesAndReturnSmallestIfSo
* tests that all items in the collection are MODULEs and if so, returns the
* smallest MODULE.
* @return BOARD_ITEM* - The smallest or NULL.
*/
static BOARD_ITEM* AllAreModulesAndReturnSmallestIfSo( GENERAL_COLLECTOR* aCollector )
2007-09-14 16:15:27 +00:00
{
int count = aCollector->GetPrimaryCount(); // try to use preferred layer
if( 0 == count ) count = aCollector->GetCount();
for( int i = 0; i<count; ++i )
2007-09-14 16:15:27 +00:00
{
if( (*aCollector)[i]->Type() != PCB_MODULE_T )
2007-09-14 16:15:27 +00:00
return NULL;
}
2007-09-14 16:15:27 +00:00
// all are modules, now find smallest MODULE
int minDim = 0x7FFFFFFF;
int minNdx = 0;
for( int i = 0; i<count; ++i )
2007-09-14 16:15:27 +00:00
{
MODULE* module = (MODULE*) (*aCollector)[i];
int lx = module->m_BoundaryBox.GetWidth();
int ly = module->m_BoundaryBox.GetHeight();
int lmin = MIN( lx, ly );
if( lmin < minDim )
2007-09-14 16:15:27 +00:00
{
minDim = lmin;
minNdx = i;
}
}
2007-09-14 16:15:27 +00:00
return (*aCollector)[minNdx];
}
BOARD_ITEM* PCB_BASE_FRAME::PcbGeneralLocateAndDisplay( int aHotKeyCode )
{
BOARD_ITEM* item;
GENERAL_COLLECTORS_GUIDE guide = GetCollectorsGuide();
2007-09-26 20:10:12 +00:00
// Assign to scanList the proper item types desired based on tool type
// or hotkey that is in play.
const KICAD_T* scanList = NULL;
2007-09-25 15:10:01 +00:00
if( aHotKeyCode )
{
2007-09-26 20:10:12 +00:00
// @todo: add switch here and add calls to PcbGeneralLocateAndDisplay( int aHotKeyCode )
// when searching is needed from a hotkey handler
2007-09-25 15:10:01 +00:00
}
else if( GetToolId() == ID_NO_TOOL_SELECTED )
{
if( m_mainToolBar->GetToolToggled( ID_TOOLBARH_PCB_MODE_MODULE ) )
scanList = GENERAL_COLLECTOR::ModuleItems;
else
scanList = (DisplayOpt.DisplayZonesMode == 0) ?
GENERAL_COLLECTOR::AllBoardItems :
GENERAL_COLLECTOR::AllButZones;
}
else
{
switch( GetToolId() )
{
case ID_PCB_SHOW_1_RATSNEST_BUTT:
scanList = GENERAL_COLLECTOR::PadsOrModules;
break;
case ID_TRACK_BUTT:
scanList = GENERAL_COLLECTOR::Tracks;
break;
case ID_PCB_MODULE_BUTT:
scanList = GENERAL_COLLECTOR::ModuleItems;
break;
case ID_PCB_ZONES_BUTT:
scanList = GENERAL_COLLECTOR::Zones;
break;
default:
scanList = DisplayOpt.DisplayZonesMode == 0 ?
GENERAL_COLLECTOR::AllBoardItems :
GENERAL_COLLECTOR::AllButZones;
}
}
m_Collector->Collect( m_Pcb, scanList, GetScreen()->RefPos( true ), guide );
#if 0
2007-09-25 15:10:01 +00:00
// debugging: print out the collected items, showing their priority order too.
for( int i = 0; i<m_Collector->GetCount(); ++i )
2007-09-25 15:10:01 +00:00
(*m_Collector)[i]->Show( 0, std::cout );
#endif
/* Remove redundancies: sometime, zones are found twice,
* because zones can be filled by overlapping segments (this is a fill option)
*/
unsigned long timestampzone = 0;
2007-09-26 20:10:12 +00:00
for( int ii = 0; ii < m_Collector->GetCount(); ii++ )
{
item = (*m_Collector)[ii];
if( item->Type() != PCB_ZONE_T )
continue;
/* Found a TYPE ZONE */
2011-12-12 08:37:05 +00:00
if( item->GetTimeStamp() == timestampzone ) // Remove it, redundant, zone already found
{
m_Collector->Remove( ii );
ii--;
}
else
{
2011-12-12 08:37:05 +00:00
timestampzone = item->GetTimeStamp();
}
}
if( m_Collector->GetCount() <= 1 )
{
item = (*m_Collector)[0];
SetCurItem( item );
}
// If the count is 2, and first item is a pad or module text, and the 2nd item is its
// parent module:
else if( m_Collector->GetCount() == 2
&& ( (*m_Collector)[0]->Type() == PCB_PAD_T || (*m_Collector)[0]->Type() ==
PCB_MODULE_TEXT_T )
&& (*m_Collector)[1]->Type() == PCB_MODULE_T && (*m_Collector)[0]->GetParent()==
(*m_Collector)[1] )
{
item = (*m_Collector)[0];
SetCurItem( item );
}
// if all are modules, find the smallest one among the primary choices
else if( ( item = AllAreModulesAndReturnSmallestIfSo( m_Collector ) ) != NULL )
2007-09-14 16:15:27 +00:00
{
SetCurItem( item );
2007-09-14 16:15:27 +00:00
}
2007-09-26 20:10:12 +00:00
else // we can't figure out which item user wants, do popup menu so user can choose
{
wxMenu itemMenu;
2007-10-10 04:45:26 +00:00
/* Give a title to the selection menu. This is also a cancel menu item */
wxMenuItem * item_title = new wxMenuItem( &itemMenu, -1, _( "Selection Clarification" ) );
#ifdef __WINDOWS__
wxFont bold_font( *wxNORMAL_FONT );
bold_font.SetWeight( wxFONTWEIGHT_BOLD );
bold_font.SetStyle( wxFONTSTYLE_ITALIC );
item_title->SetFont( bold_font );
#endif
itemMenu.Append( item_title );
2007-10-10 04:45:26 +00:00
itemMenu.AppendSeparator();
2007-09-20 21:06:49 +00:00
int limit = MIN( MAX_ITEMS_IN_PICKER, m_Collector->GetCount() );
for( int i = 0; i<limit; ++i )
{
wxString text;
item = (*m_Collector)[i];
text = item->GetSelectMenuText();
BITMAP_DEF xpm = item->GetMenuImage();
AddMenuItem( &itemMenu, ID_POPUP_PCB_ITEM_SELECTION_START + i, text, KiBitmap( xpm ) );
}
/* @todo: rather than assignment to true, these should be increment and decrement
* operators throughout _everywhere_.
* That way we can handle nesting.
* But I tried that and found there cases where the assignment to true (converted to
* a m_IgnoreMouseEvents++ )
* was not balanced with the -- (now m_IgnoreMouseEvents=false), so I had to revert.
* Somebody should track down these and make them balanced.
* m_canvas->m_IgnoreMouseEvents = true;
*/
// this menu's handler is void PCB_BASE_FRAME::ProcessItemSelection()
// and it calls SetCurItem() which in turn calls DisplayInfo() on the item.
m_canvas->m_AbortRequest = true; // changed in false if an item is selected
2011-03-10 17:52:36 +00:00
PopupMenu( &itemMenu );
m_canvas->MoveCursorToCrossHair();
// The function ProcessItemSelection() has set the current item, return it.
if( m_canvas->m_AbortRequest ) // Nothing selected
2011-03-10 17:52:36 +00:00
item = NULL;
else
item = GetCurItem();
}
return item;
2007-05-06 16:03:28 +00:00
}
void PCB_EDIT_FRAME::GeneralControl( wxDC* aDC, const wxPoint& aPosition, int aHotKey )
2007-05-06 16:03:28 +00:00
{
wxRealPoint gridSize;
wxPoint oldpos;
wxPoint pos = GetScreen()->GetNearestGridPosition( aPosition );
2007-08-04 01:12:30 +00:00
oldpos = GetScreen()->GetCrossHairPosition();
2007-08-04 01:12:30 +00:00
gridSize = GetScreen()->GetGridSize();
2007-08-04 01:12:30 +00:00
switch( aHotKey )
2007-08-04 01:12:30 +00:00
{
case WXK_NUMPAD8:
2007-08-04 01:12:30 +00:00
case WXK_UP:
pos.y -= wxRound( gridSize.y );
m_canvas->MoveCursor( pos );
2007-08-04 01:12:30 +00:00
break;
case WXK_NUMPAD2:
2007-08-04 01:12:30 +00:00
case WXK_DOWN:
pos.y += wxRound( gridSize.y );
m_canvas->MoveCursor( pos );
2007-08-04 01:12:30 +00:00
break;
case WXK_NUMPAD4:
2007-08-04 01:12:30 +00:00
case WXK_LEFT:
pos.x -= wxRound( gridSize.x );
m_canvas->MoveCursor( pos );
2007-08-04 01:12:30 +00:00
break;
case WXK_NUMPAD6:
2007-08-04 01:12:30 +00:00
case WXK_RIGHT:
pos.x += wxRound( gridSize.x );
m_canvas->MoveCursor( pos );
2007-08-04 01:12:30 +00:00
break;
default:
break;
}
// Put cursor in new position, according to the zoom keys (if any).
GetScreen()->SetCrossHairPosition( pos );
2007-08-04 01:12:30 +00:00
/* Put cursor on grid or a pad centre if requested. If the tool DELETE is active the
* cursor is left off grid this is better to reach items to delete off grid,
2007-08-04 01:12:30 +00:00
*/
bool keep_on_grid = true;
if( GetToolId() == ID_PCB_DELETE_ITEM_BUTT )
keep_on_grid = false;
2007-08-04 01:12:30 +00:00
/* Cursor is left off grid if no block in progress and no moving object */
if( GetScreen()->m_BlockLocate.m_State != STATE_NO_BLOCK )
keep_on_grid = true;
EDA_ITEM* DrawStruct = GetScreen()->GetCurItem();
if( DrawStruct && DrawStruct->GetFlags() )
keep_on_grid = true;
2007-08-04 01:12:30 +00:00
2008-02-27 15:26:16 +00:00
if( keep_on_grid )
{
wxPoint on_grid = GetScreen()->GetNearestGridPosition( pos );
2007-08-04 01:12:30 +00:00
wxSize grid;
grid.x = (int) GetScreen()->GetGridSize().x;
grid.y = (int) GetScreen()->GetGridSize().y;
if( Magnetize( m_Pcb, this, GetToolId(), grid, on_grid, &pos ) )
{
GetScreen()->SetCrossHairPosition( pos, false );
}
2008-02-27 15:26:16 +00:00
else
{
// If there's no intrusion and DRC is active, we pass the cursor
// "as is", and let ShowNewTrackWhenMovingCursor figure out what to do.
if( !Drc_On || !g_CurrentTrackSegment
|| (BOARD_ITEM*)g_CurrentTrackSegment != this->GetCurItem()
|| !LocateIntrusion( m_Pcb->m_Track, g_CurrentTrackSegment,
GetScreen()->m_Active_Layer, GetScreen()->RefPos( true ) ) )
{
GetScreen()->SetCrossHairPosition( on_grid );
}
2008-02-27 15:26:16 +00:00
}
2007-08-04 01:12:30 +00:00
}
if( oldpos != GetScreen()->GetCrossHairPosition() )
2007-08-04 01:12:30 +00:00
{
pos = GetScreen()->GetCrossHairPosition();
GetScreen()->SetCrossHairPosition( oldpos, false );
m_canvas->CrossHairOff( aDC );
GetScreen()->SetCrossHairPosition( pos, false );
m_canvas->CrossHairOn( aDC );
2007-08-04 01:12:30 +00:00
if( m_canvas->IsMouseCaptured() )
2007-08-04 01:12:30 +00:00
{
#ifdef USE_WX_OVERLAY
wxDCOverlay oDC( m_canvas->m_overlay, (wxWindowDC*)aDC );
oDC.Clear();
m_canvas->m_mouseCaptureCallback( m_canvas, aDC, aPosition, false );
#else
m_canvas->m_mouseCaptureCallback( m_canvas, aDC, aPosition, true );
#endif
2007-08-04 01:12:30 +00:00
}
#ifdef USE_WX_OVERLAY
else
{
m_canvas->m_overlay.Reset();
}
#endif
2007-08-04 01:12:30 +00:00
}
if( aHotKey )
2007-08-20 10:55:09 +00:00
{
OnHotKey( aDC, aHotKey, aPosition );
2007-08-20 10:55:09 +00:00
}
UpdateStatusBar(); /* Display new cursor coordinates */
2007-05-06 16:03:28 +00:00
}