You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

592 lines
17 KiB

18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2007 Jean-Pierre Charras, jean-pierre.charras@gipsa-lab.inpg.fr
  5. * Copyright (C) 2011 Wayne Stambaugh <stambaughw@verizon.net>
  6. * Copyright (C) 1992-2011 KiCad Developers, see AUTHORS.txt for contributors.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, you may find one here:
  20. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  21. * or you may search the http://www.gnu.org website for the version 2 license,
  22. * or you may write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  24. */
  25. /**
  26. * @file pcbnew/onleftclick.cpp
  27. * @brief Functions called when the left button is clicked or double clicked.
  28. */
  29. #include <fctsys.h>
  30. #include <class_drawpanel.h>
  31. #include <confirm.h>
  32. #include <wxPcbStruct.h>
  33. #include <msgpanel.h>
  34. #include <class_board.h>
  35. #include <class_zone.h>
  36. #include <class_pcb_text.h>
  37. #include <pcbnew.h>
  38. #include <pcbnew_id.h>
  39. #include <menus_helpers.h>
  40. /* Handle the left button mouse click, when a tool is active
  41. */
  42. void PCB_EDIT_FRAME::OnLeftClick( wxDC* aDC, const wxPoint& aPosition )
  43. {
  44. BOARD_ITEM* DrawStruct = GetCurItem();
  45. bool exit = false;
  46. bool no_tool = GetToolId() == ID_NO_TOOL_SELECTED;
  47. if( no_tool || ( DrawStruct && DrawStruct->GetFlags() ) )
  48. {
  49. m_canvas->SetAutoPanRequest( false );
  50. if( DrawStruct && DrawStruct->GetFlags() ) // Command in progress
  51. {
  52. m_canvas->SetIgnoreMouseEvents( true );
  53. m_canvas->CrossHairOff( aDC );
  54. switch( DrawStruct->Type() )
  55. {
  56. case PCB_ZONE_AREA_T:
  57. if( DrawStruct->IsNew() )
  58. {
  59. m_canvas->SetAutoPanRequest( true );
  60. Begin_Zone( aDC );
  61. }
  62. else
  63. {
  64. End_Move_Zone_Corner_Or_Outlines( aDC, (ZONE_CONTAINER*) DrawStruct );
  65. }
  66. exit = true;
  67. break;
  68. case PCB_TRACE_T:
  69. case PCB_VIA_T:
  70. if( DrawStruct->IsDragging() )
  71. {
  72. PlaceDraggedOrMovedTrackSegment( (TRACK*) DrawStruct, aDC );
  73. exit = true;
  74. }
  75. break;
  76. case PCB_TEXT_T:
  77. Place_Texte_Pcb( (TEXTE_PCB*) DrawStruct, aDC );
  78. exit = true;
  79. break;
  80. case PCB_MODULE_TEXT_T:
  81. PlaceTexteModule( (TEXTE_MODULE*) DrawStruct, aDC );
  82. exit = true;
  83. break;
  84. case PCB_PAD_T:
  85. PlacePad( (D_PAD*) DrawStruct, aDC );
  86. exit = true;
  87. break;
  88. case PCB_MODULE_T:
  89. PlaceModule( (MODULE*) DrawStruct, aDC );
  90. exit = true;
  91. break;
  92. case PCB_TARGET_T:
  93. PlaceTarget( (PCB_TARGET*) DrawStruct, aDC );
  94. exit = true;
  95. break;
  96. case PCB_LINE_T:
  97. if( no_tool ) // when no tools: existing item moving.
  98. {
  99. Place_DrawItem( (DRAWSEGMENT*) DrawStruct, aDC );
  100. exit = true;
  101. }
  102. break;
  103. case PCB_DIMENSION_T:
  104. if( ! DrawStruct->IsNew() )
  105. { // We are moving the text of an existing dimension. Place it
  106. PlaceDimensionText( (DIMENSION*) DrawStruct, aDC );
  107. exit = true;
  108. }
  109. break;
  110. default:
  111. DisplayError( this,
  112. wxT( "PCB_EDIT_FRAME::OnLeftClick() err: DrawType %d m_Flags != 0" ),
  113. DrawStruct->Type() );
  114. exit = true;
  115. break;
  116. }
  117. m_canvas->SetIgnoreMouseEvents( false );
  118. m_canvas->CrossHairOn( aDC );
  119. if( exit )
  120. return;
  121. }
  122. else if( !wxGetKeyState( WXK_SHIFT ) && !wxGetKeyState( WXK_ALT )
  123. && !wxGetKeyState( WXK_CONTROL ) )
  124. {
  125. DrawStruct = PcbGeneralLocateAndDisplay();
  126. if( DrawStruct )
  127. SendMessageToEESCHEMA( DrawStruct );
  128. }
  129. }
  130. if( DrawStruct ) // display netclass info for zones, tracks and pads
  131. {
  132. switch( DrawStruct->Type() )
  133. {
  134. case PCB_ZONE_AREA_T:
  135. case PCB_TRACE_T:
  136. case PCB_VIA_T:
  137. case PCB_PAD_T:
  138. GetBoard()->SetCurrentNetClass(
  139. ((BOARD_CONNECTED_ITEM*)DrawStruct)->GetNetClassName() );
  140. updateTraceWidthSelectBox();
  141. updateViaSizeSelectBox();
  142. break;
  143. default:
  144. break;
  145. }
  146. }
  147. switch( GetToolId() )
  148. {
  149. case ID_MAIN_MENUBAR:
  150. case ID_NO_TOOL_SELECTED:
  151. break;
  152. case ID_PCB_MUWAVE_TOOL_SELF_CMD:
  153. case ID_PCB_MUWAVE_TOOL_GAP_CMD:
  154. case ID_PCB_MUWAVE_TOOL_STUB_CMD:
  155. case ID_PCB_MUWAVE_TOOL_STUB_ARC_CMD:
  156. case ID_PCB_MUWAVE_TOOL_FUNCTION_SHAPE_CMD:
  157. MuWaveCommand( aDC, aPosition );
  158. break;
  159. case ID_PCB_HIGHLIGHT_BUTT:
  160. {
  161. int netcode = SelectHighLight( aDC );
  162. if( netcode < 0 )
  163. SetMsgPanel( GetBoard() );
  164. else
  165. {
  166. NETINFO_ITEM* net = GetBoard()->FindNet( netcode );
  167. if( net )
  168. {
  169. MSG_PANEL_ITEMS items;
  170. net->GetMsgPanelInfo( items );
  171. SetMsgPanel( items );
  172. }
  173. }
  174. }
  175. break;
  176. case ID_PCB_SHOW_1_RATSNEST_BUTT:
  177. DrawStruct = PcbGeneralLocateAndDisplay();
  178. Show_1_Ratsnest( DrawStruct, aDC );
  179. if( DrawStruct )
  180. SendMessageToEESCHEMA( DrawStruct );
  181. break;
  182. case ID_PCB_MIRE_BUTT:
  183. if( (DrawStruct == NULL) || (DrawStruct->GetFlags() == 0) )
  184. {
  185. SetCurItem( (BOARD_ITEM*) CreateTarget( aDC ) );
  186. m_canvas->MoveCursorToCrossHair();
  187. }
  188. else if( DrawStruct->Type() == PCB_TARGET_T )
  189. {
  190. PlaceTarget( (PCB_TARGET*) DrawStruct, aDC );
  191. }
  192. else
  193. {
  194. DisplayError( this, wxT( "OnLeftClick err: not a PCB_TARGET_T" ) );
  195. }
  196. break;
  197. case ID_PCB_CIRCLE_BUTT:
  198. case ID_PCB_ARC_BUTT:
  199. case ID_PCB_ADD_LINE_BUTT:
  200. {
  201. STROKE_T shape = S_SEGMENT;
  202. if( GetToolId() == ID_PCB_CIRCLE_BUTT )
  203. shape = S_CIRCLE;
  204. if( GetToolId() == ID_PCB_ARC_BUTT )
  205. shape = S_ARC;
  206. if( IsCopperLayer( getActiveLayer() ) )
  207. {
  208. DisplayError( this, _( "Graphic not allowed on Copper layers" ) );
  209. break;
  210. }
  211. if( (DrawStruct == NULL) || (DrawStruct->GetFlags() == 0) )
  212. {
  213. DrawStruct = (BOARD_ITEM*) Begin_DrawSegment( NULL, shape, aDC );
  214. SetCurItem( DrawStruct );
  215. m_canvas->SetAutoPanRequest( true );
  216. }
  217. else if( DrawStruct
  218. && (DrawStruct->Type() == PCB_LINE_T)
  219. && DrawStruct->IsNew() )
  220. {
  221. DrawStruct = (BOARD_ITEM*) Begin_DrawSegment( (DRAWSEGMENT*) DrawStruct, shape, aDC );
  222. SetCurItem( DrawStruct );
  223. m_canvas->SetAutoPanRequest( true );
  224. }
  225. }
  226. break;
  227. case ID_TRACK_BUTT:
  228. if( !IsCopperLayer( getActiveLayer() ) )
  229. {
  230. DisplayError( this, _( "Tracks on Copper layers only " ) );
  231. break;
  232. }
  233. if( (DrawStruct == NULL) || (DrawStruct->GetFlags() == 0) )
  234. {
  235. DrawStruct = (BOARD_ITEM*) Begin_Route( NULL, aDC );
  236. SetCurItem( DrawStruct );
  237. if( DrawStruct )
  238. m_canvas->SetAutoPanRequest( true );
  239. }
  240. else if( DrawStruct && DrawStruct->IsNew() )
  241. {
  242. TRACK* track = Begin_Route( (TRACK*) DrawStruct, aDC );
  243. // SetCurItem() must not write to the msg panel
  244. // because a track info is displayed while moving the mouse cursor
  245. if( track ) // A new segment was created
  246. SetCurItem( DrawStruct = (BOARD_ITEM*) track, false );
  247. m_canvas->SetAutoPanRequest( true );
  248. }
  249. break;
  250. case ID_PCB_ZONES_BUTT:
  251. case ID_PCB_KEEPOUT_AREA_BUTT:
  252. /* ZONE or KEEPOUT Tool is selected. Determine action for a left click:
  253. * this can be start a new zone or select and move an existing zone outline corner
  254. * if found near the mouse cursor
  255. */
  256. if( (DrawStruct == NULL) || (DrawStruct->GetFlags() == 0) )
  257. {
  258. if( Begin_Zone( aDC ) )
  259. {
  260. m_canvas->SetAutoPanRequest( true );
  261. DrawStruct = GetBoard()->m_CurrentZoneContour;
  262. GetScreen()->SetCurItem( DrawStruct );
  263. }
  264. }
  265. else if( DrawStruct && (DrawStruct->Type() == PCB_ZONE_AREA_T) && DrawStruct->IsNew() )
  266. { // Add a new corner to the current outline being created:
  267. m_canvas->SetAutoPanRequest( true );
  268. Begin_Zone( aDC );
  269. DrawStruct = GetBoard()->m_CurrentZoneContour;
  270. GetScreen()->SetCurItem( DrawStruct );
  271. }
  272. else
  273. {
  274. DisplayError( this, wxT( "PCB_EDIT_FRAME::OnLeftClick() zone internal error" ) );
  275. }
  276. break;
  277. case ID_PCB_ADD_TEXT_BUTT:
  278. if( IsLayerInList( EDGE_LAYER, getActiveLayer() ) )
  279. {
  280. DisplayError( this,
  281. _( "Texts not allowed on Edge Cut layer" ) );
  282. break;
  283. }
  284. if( (DrawStruct == NULL) || (DrawStruct->GetFlags() == 0) )
  285. {
  286. SetCurItem( CreateTextePcb( aDC ) );
  287. m_canvas->MoveCursorToCrossHair();
  288. m_canvas->SetAutoPanRequest( true );
  289. }
  290. else if( DrawStruct->Type() == PCB_TEXT_T )
  291. {
  292. Place_Texte_Pcb( (TEXTE_PCB*) DrawStruct, aDC );
  293. m_canvas->SetAutoPanRequest( false );
  294. }
  295. else
  296. {
  297. DisplayError( this, wxT( "OnLeftClick err: not a PCB_TEXT_T" ) );
  298. }
  299. break;
  300. case ID_PCB_MODULE_BUTT:
  301. if( (DrawStruct == NULL) || (DrawStruct->GetFlags() == 0) )
  302. {
  303. m_canvas->MoveCursorToCrossHair();
  304. DrawStruct = (BOARD_ITEM*) LoadModuleFromLibrary( wxEmptyString, m_footprintLibTable,
  305. true, aDC );
  306. SetCurItem( DrawStruct );
  307. if( DrawStruct )
  308. StartMoveModule( (MODULE*) DrawStruct, aDC, false );
  309. }
  310. else if( DrawStruct->Type() == PCB_MODULE_T )
  311. {
  312. PlaceModule( (MODULE*) DrawStruct, aDC );
  313. m_canvas->SetAutoPanRequest( false );
  314. }
  315. else
  316. {
  317. DisplayError( this, wxT( "Internal err: Struct not PCB_MODULE_T" ) );
  318. }
  319. break;
  320. case ID_PCB_DIMENSION_BUTT:
  321. if( IsLayerInList( EDGE_LAYER|ALL_CU_LAYERS, getActiveLayer() ) )
  322. {
  323. DisplayError( this,
  324. _( "Dimension not allowed on Copper or Edge Cut layers" ) );
  325. break;
  326. }
  327. if( (DrawStruct == NULL) || (DrawStruct->GetFlags() == 0) )
  328. {
  329. DrawStruct = (BOARD_ITEM*) EditDimension( NULL, aDC );
  330. SetCurItem( DrawStruct );
  331. m_canvas->SetAutoPanRequest( true );
  332. }
  333. else if( DrawStruct && (DrawStruct->Type() == PCB_DIMENSION_T) && DrawStruct->IsNew() )
  334. {
  335. DrawStruct = (BOARD_ITEM*) EditDimension( (DIMENSION*) DrawStruct, aDC );
  336. SetCurItem( DrawStruct );
  337. m_canvas->SetAutoPanRequest( true );
  338. }
  339. else
  340. {
  341. DisplayError( this,
  342. wxT( "PCB_EDIT_FRAME::OnLeftClick() error item is not a DIMENSION" ) );
  343. }
  344. break;
  345. case ID_PCB_DELETE_ITEM_BUTT:
  346. if( !DrawStruct || !DrawStruct->GetFlags() )
  347. {
  348. DrawStruct = PcbGeneralLocateAndDisplay();
  349. if( DrawStruct && (DrawStruct->GetFlags() == 0) )
  350. {
  351. RemoveStruct( DrawStruct, aDC );
  352. SetCurItem( DrawStruct = NULL );
  353. }
  354. }
  355. break;
  356. case ID_PCB_PLACE_OFFSET_COORD_BUTT:
  357. m_canvas->DrawAuxiliaryAxis( aDC, GR_XOR );
  358. SetAuxOrigin( GetCrossHairPosition() );
  359. m_canvas->DrawAuxiliaryAxis( aDC, GR_COPY );
  360. OnModify();
  361. break;
  362. case ID_PCB_PLACE_GRID_COORD_BUTT:
  363. m_canvas->DrawGridAxis( aDC, GR_XOR, GetBoard()->GetGridOrigin() );
  364. SetGridOrigin( GetCrossHairPosition() );
  365. m_canvas->DrawGridAxis( aDC, GR_COPY, GetBoard()->GetGridOrigin() );
  366. break;
  367. default:
  368. DisplayError( this, wxT( "PCB_EDIT_FRAME::OnLeftClick() id error" ) );
  369. SetToolID( ID_NO_TOOL_SELECTED, m_canvas->GetDefaultCursor(), wxEmptyString );
  370. break;
  371. }
  372. }
  373. /* handle the double click on the mouse left button
  374. */
  375. void PCB_EDIT_FRAME::OnLeftDClick( wxDC* aDC, const wxPoint& aPosition )
  376. {
  377. BOARD_ITEM* DrawStruct = GetCurItem();
  378. switch( GetToolId() )
  379. {
  380. case ID_NO_TOOL_SELECTED:
  381. if( (DrawStruct == NULL) || (DrawStruct->GetFlags() == 0) )
  382. {
  383. DrawStruct = PcbGeneralLocateAndDisplay();
  384. }
  385. if( (DrawStruct == NULL) || (DrawStruct->GetFlags() != 0) )
  386. break;
  387. SendMessageToEESCHEMA( DrawStruct );
  388. // An item is found
  389. SetCurItem( DrawStruct );
  390. switch( DrawStruct->Type() )
  391. {
  392. case PCB_TRACE_T:
  393. case PCB_VIA_T:
  394. if( DrawStruct->IsNew() )
  395. {
  396. if( End_Route( (TRACK*) DrawStruct, aDC ) )
  397. m_canvas->SetAutoPanRequest( false );
  398. }
  399. else if( DrawStruct->GetFlags() == 0 )
  400. {
  401. Edit_TrackSegm_Width( aDC, (TRACK*) DrawStruct );
  402. }
  403. break;
  404. case PCB_TEXT_T:
  405. case PCB_PAD_T:
  406. case PCB_MODULE_T:
  407. case PCB_TARGET_T:
  408. case PCB_DIMENSION_T:
  409. case PCB_MODULE_TEXT_T:
  410. OnEditItemRequest( aDC, DrawStruct );
  411. m_canvas->MoveCursorToCrossHair();
  412. break;
  413. case PCB_LINE_T:
  414. OnEditItemRequest( aDC, DrawStruct );
  415. break;
  416. case PCB_ZONE_AREA_T:
  417. if( DrawStruct->GetFlags() )
  418. break;
  419. OnEditItemRequest( aDC, DrawStruct );
  420. break;
  421. default:
  422. break;
  423. }
  424. break; // end case 0
  425. case ID_TRACK_BUTT:
  426. if( DrawStruct && DrawStruct->IsNew() )
  427. {
  428. if( End_Route( (TRACK*) DrawStruct, aDC ) )
  429. m_canvas->SetAutoPanRequest( false );
  430. }
  431. break;
  432. case ID_PCB_ZONES_BUTT:
  433. case ID_PCB_KEEPOUT_AREA_BUTT:
  434. if( End_Zone( aDC ) )
  435. {
  436. m_canvas->SetAutoPanRequest( false );
  437. SetCurItem( NULL );
  438. }
  439. break;
  440. case ID_PCB_ADD_LINE_BUTT:
  441. case ID_PCB_ARC_BUTT:
  442. case ID_PCB_CIRCLE_BUTT:
  443. if( DrawStruct == NULL )
  444. break;
  445. if( DrawStruct->Type() != PCB_LINE_T )
  446. {
  447. DisplayError( this, wxT( "DrawStruct Type error" ) );
  448. m_canvas->SetAutoPanRequest( false );
  449. break;
  450. }
  451. if( DrawStruct->IsNew() )
  452. {
  453. End_Edge( (DRAWSEGMENT*) DrawStruct, aDC );
  454. m_canvas->SetAutoPanRequest( false );
  455. SetCurItem( NULL );
  456. }
  457. break;
  458. }
  459. }
  460. void PCB_EDIT_FRAME::OnEditItemRequest( wxDC* aDC, BOARD_ITEM* aItem )
  461. {
  462. switch( aItem->Type() )
  463. {
  464. case PCB_TRACE_T:
  465. case PCB_VIA_T:
  466. Edit_TrackSegm_Width( aDC, (TRACK*) aItem );
  467. break;
  468. case PCB_TEXT_T:
  469. InstallTextPCBOptionsFrame( (TEXTE_PCB*) aItem, aDC );
  470. break;
  471. case PCB_PAD_T:
  472. InstallPadOptionsFrame( (D_PAD*) aItem );
  473. break;
  474. case PCB_MODULE_T:
  475. InstallModuleOptionsFrame( (MODULE*) aItem, aDC );
  476. break;
  477. case PCB_TARGET_T:
  478. ShowTargetOptionsDialog( (PCB_TARGET*) aItem, aDC );
  479. break;
  480. case PCB_DIMENSION_T:
  481. ShowDimensionPropertyDialog( (DIMENSION*) aItem, aDC );
  482. break;
  483. case PCB_MODULE_TEXT_T:
  484. InstallTextModOptionsFrame( (TEXTE_MODULE*) aItem, aDC );
  485. break;
  486. case PCB_LINE_T:
  487. InstallGraphicItemPropertiesDialog( (DRAWSEGMENT*) aItem, aDC );
  488. break;
  489. case PCB_ZONE_AREA_T:
  490. Edit_Zone_Params( aDC, (ZONE_CONTAINER*) aItem );
  491. break;
  492. default:
  493. break;
  494. }
  495. }