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.

403 lines
11 KiB

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) 2009 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
  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 eeschema/onleftclick.cpp
  27. */
  28. #include <fctsys.h>
  29. #include <eeschema_id.h>
  30. #include <class_drawpanel.h>
  31. #include <confirm.h>
  32. #include <wxEeschemaStruct.h>
  33. #include <menus_helpers.h>
  34. #include <sch_bus_entry.h>
  35. #include <sch_text.h>
  36. #include <sch_marker.h>
  37. #include <sch_junction.h>
  38. #include <sch_line.h>
  39. #include <sch_no_connect.h>
  40. #include <sch_component.h>
  41. #include <sch_sheet.h>
  42. #include <sch_sheet_path.h>
  43. #include <sch_bitmap.h>
  44. // TODO(hzeller): These pairs of elmenets should be represented by an object, but don't want
  45. // to refactor too much right now to not get in the way with other code changes.
  46. static wxArrayString s_CmpNameList;
  47. static int s_CmpLastUnit;
  48. static wxArrayString s_PowerNameList;
  49. static int s_LastPowerUnit;
  50. void SCH_EDIT_FRAME::OnLeftClick( wxDC* aDC, const wxPoint& aPosition )
  51. {
  52. SCH_ITEM* item = GetScreen()->GetCurItem();
  53. wxPoint gridPosition = GetGridPosition( aPosition );
  54. if( ( GetToolId() == ID_NO_TOOL_SELECTED ) || ( item && item->GetFlags() ) )
  55. {
  56. m_canvas->SetAutoPanRequest( false );
  57. SetRepeatItem( NULL );
  58. if( item && item->GetFlags() )
  59. {
  60. switch( item->Type() )
  61. {
  62. case SCH_LABEL_T:
  63. case SCH_GLOBAL_LABEL_T:
  64. case SCH_HIERARCHICAL_LABEL_T:
  65. case SCH_TEXT_T:
  66. case SCH_SHEET_PIN_T:
  67. case SCH_SHEET_T:
  68. case SCH_BUS_WIRE_ENTRY_T:
  69. case SCH_BUS_BUS_ENTRY_T:
  70. case SCH_JUNCTION_T:
  71. case SCH_COMPONENT_T:
  72. case SCH_FIELD_T:
  73. case SCH_BITMAP_T:
  74. case SCH_NO_CONNECT_T:
  75. addCurrentItemToList( aDC );
  76. return;
  77. case SCH_LINE_T: // May already be drawing segment.
  78. break;
  79. default:
  80. wxFAIL_MSG( wxT( "SCH_EDIT_FRAME::OnLeftClick error. Item type <" ) +
  81. item->GetClass() + wxT( "> is already being edited." ) );
  82. item->ClearFlags();
  83. break;
  84. }
  85. }
  86. else
  87. {
  88. item = LocateAndShowItem( aPosition );
  89. }
  90. }
  91. switch( GetToolId() )
  92. {
  93. case ID_NO_TOOL_SELECTED:
  94. break;
  95. case ID_HIERARCHY_PUSH_POP_BUTT:
  96. if( ( item && item->GetFlags() ) || ( g_RootSheet->CountSheets() == 0 ) )
  97. break;
  98. item = LocateAndShowItem( aPosition, SCH_COLLECTOR::SheetsOnly );
  99. if( item )
  100. {
  101. m_CurrentSheet->Push( (SCH_SHEET*) item );
  102. DisplayCurrentSheet();
  103. }
  104. else
  105. {
  106. wxCHECK_RET( m_CurrentSheet->Last() != g_RootSheet,
  107. wxT( "Cannot leave root sheet. Bad Programmer!" ) );
  108. m_CurrentSheet->Pop();
  109. DisplayCurrentSheet();
  110. }
  111. break;
  112. case ID_NOCONN_BUTT:
  113. if( ( item == NULL ) || ( item->GetFlags() == 0 ) )
  114. {
  115. if( false == GetScreen()->GetItem( gridPosition, 0, SCH_NO_CONNECT_T ) )
  116. {
  117. SCH_NO_CONNECT* no_connect = AddNoConnect( aDC, gridPosition );
  118. SetRepeatItem( no_connect );
  119. GetScreen()->SetCurItem( no_connect );
  120. m_canvas->SetAutoPanRequest( true );
  121. }
  122. }
  123. else
  124. {
  125. addCurrentItemToList( aDC );
  126. }
  127. break;
  128. case ID_JUNCTION_BUTT:
  129. if( ( item == NULL ) || ( item->GetFlags() == 0 ) )
  130. {
  131. if( false == GetScreen()->GetItem( gridPosition, 0, SCH_JUNCTION_T ) )
  132. {
  133. SCH_JUNCTION* junction = AddJunction( aDC, gridPosition, true );
  134. SetRepeatItem( junction );
  135. GetScreen()->SetCurItem( junction );
  136. m_canvas->SetAutoPanRequest( true );
  137. }
  138. }
  139. else
  140. {
  141. addCurrentItemToList( aDC );
  142. }
  143. break;
  144. case ID_WIRETOBUS_ENTRY_BUTT:
  145. if( ( item == NULL ) || ( item->GetFlags() == 0 ) )
  146. {
  147. CreateBusWireEntry( aDC );
  148. m_canvas->SetAutoPanRequest( true );
  149. }
  150. else
  151. {
  152. addCurrentItemToList( aDC );
  153. }
  154. break;
  155. case ID_BUSTOBUS_ENTRY_BUTT:
  156. if( ( item == NULL ) || ( item->GetFlags() == 0 ) )
  157. {
  158. CreateBusBusEntry( aDC );
  159. m_canvas->SetAutoPanRequest( true );
  160. }
  161. else
  162. {
  163. addCurrentItemToList( aDC );
  164. }
  165. break;
  166. case ID_SCHEMATIC_DELETE_ITEM_BUTT:
  167. DeleteItemAtCrossHair( aDC );
  168. break;
  169. case ID_WIRE_BUTT:
  170. BeginSegment( aDC, LAYER_WIRE );
  171. m_canvas->SetAutoPanRequest( true );
  172. break;
  173. case ID_BUS_BUTT:
  174. BeginSegment( aDC, LAYER_BUS );
  175. m_canvas->SetAutoPanRequest( true );
  176. break;
  177. case ID_LINE_COMMENT_BUTT:
  178. BeginSegment( aDC, LAYER_NOTES );
  179. m_canvas->SetAutoPanRequest( true );
  180. break;
  181. case ID_TEXT_COMMENT_BUTT:
  182. if( ( item == NULL ) || ( item->GetFlags() == 0 ) )
  183. {
  184. GetScreen()->SetCurItem( CreateNewText( aDC, LAYER_NOTES ) );
  185. m_canvas->SetAutoPanRequest( true );
  186. }
  187. else
  188. {
  189. addCurrentItemToList( aDC );
  190. }
  191. break;
  192. case ID_ADD_IMAGE_BUTT:
  193. if( ( item == NULL ) || ( item->GetFlags() == 0 ) )
  194. {
  195. GetScreen()->SetCurItem( CreateNewImage( aDC ) );
  196. m_canvas->SetAutoPanRequest( true );
  197. }
  198. else
  199. {
  200. addCurrentItemToList( aDC );
  201. }
  202. break;
  203. case ID_LABEL_BUTT:
  204. if( ( item == NULL ) || ( item->GetFlags() == 0 ) )
  205. {
  206. GetScreen()->SetCurItem( CreateNewText( aDC, LAYER_LOCLABEL ) );
  207. m_canvas->SetAutoPanRequest( true );
  208. }
  209. else
  210. {
  211. addCurrentItemToList( aDC );
  212. }
  213. break;
  214. case ID_GLABEL_BUTT:
  215. case ID_HIERLABEL_BUTT:
  216. if( (item == NULL) || (item->GetFlags() == 0) )
  217. {
  218. if( GetToolId() == ID_GLABEL_BUTT )
  219. GetScreen()->SetCurItem( CreateNewText( aDC, LAYER_GLOBLABEL ) );
  220. if( GetToolId() == ID_HIERLABEL_BUTT )
  221. GetScreen()->SetCurItem( CreateNewText( aDC, LAYER_HIERLABEL ) );
  222. m_canvas->SetAutoPanRequest( true );
  223. }
  224. else
  225. {
  226. addCurrentItemToList( aDC );
  227. }
  228. break;
  229. case ID_SHEET_SYMBOL_BUTT:
  230. if( ( item == NULL ) || ( item->GetFlags() == 0 ) )
  231. {
  232. item = CreateSheet( aDC );
  233. if( item != NULL )
  234. {
  235. GetScreen()->SetCurItem( item );
  236. m_canvas->SetAutoPanRequest( true );
  237. }
  238. }
  239. else
  240. {
  241. addCurrentItemToList( aDC );
  242. }
  243. break;
  244. case ID_IMPORT_HLABEL_BUTT:
  245. case ID_SHEET_PIN_BUTT:
  246. if( ( item == NULL ) || ( item->GetFlags() == 0 ) )
  247. item = LocateAndShowItem( aPosition, SCH_COLLECTOR::SheetsAndSheetLabels );
  248. if( item == NULL )
  249. break;
  250. if( (item->Type() == SCH_SHEET_T) && (item->GetFlags() == 0) )
  251. {
  252. if( GetToolId() == ID_IMPORT_HLABEL_BUTT )
  253. GetScreen()->SetCurItem( ImportSheetPin( (SCH_SHEET*) item, aDC ) );
  254. else
  255. GetScreen()->SetCurItem( CreateSheetPin( (SCH_SHEET*) item, aDC ) );
  256. }
  257. else if( (item->Type() == SCH_SHEET_PIN_T) && (item->GetFlags() != 0) )
  258. {
  259. addCurrentItemToList( aDC );
  260. }
  261. break;
  262. case ID_SCH_PLACE_COMPONENT:
  263. if( (item == NULL) || (item->GetFlags() == 0) )
  264. {
  265. GetScreen()->SetCurItem( Load_Component( aDC, wxEmptyString,
  266. s_CmpNameList, s_CmpLastUnit, true ) );
  267. m_canvas->SetAutoPanRequest( true );
  268. }
  269. else
  270. {
  271. addCurrentItemToList( aDC );
  272. }
  273. break;
  274. case ID_PLACE_POWER_BUTT:
  275. if( ( item == NULL ) || ( item->GetFlags() == 0 ) )
  276. {
  277. GetScreen()->SetCurItem( Load_Component( aDC, wxT( "power" ),
  278. s_PowerNameList, s_LastPowerUnit, false ) );
  279. m_canvas->SetAutoPanRequest( true );
  280. }
  281. else
  282. {
  283. addCurrentItemToList( aDC );
  284. }
  285. break;
  286. default:
  287. SetToolID( ID_NO_TOOL_SELECTED, m_canvas->GetDefaultCursor(), wxEmptyString );
  288. wxFAIL_MSG( wxT( "SCH_EDIT_FRAME::OnLeftClick invalid tool ID <" ) +
  289. wxString::Format( wxT( "%d> selected." ), GetToolId() ) );
  290. }
  291. }
  292. /**
  293. * Function OnLeftDClick
  294. * called on a double click event from the drawpanel mouse handler
  295. * if an editable item is found (text, component)
  296. * Call the suitable dialog editor.
  297. * Id a create command is in progress:
  298. * validate and finish the command
  299. */
  300. void SCH_EDIT_FRAME::OnLeftDClick( wxDC* aDC, const wxPoint& aPosition )
  301. {
  302. EDA_ITEM* item = GetScreen()->GetCurItem();
  303. switch( GetToolId() )
  304. {
  305. case ID_NO_TOOL_SELECTED:
  306. if( ( item == NULL ) || ( item->GetFlags() == 0 ) )
  307. {
  308. item = LocateAndShowItem( aPosition );
  309. }
  310. if( ( item == NULL ) || ( item->GetFlags() != 0 ) )
  311. break;
  312. switch( item->Type() )
  313. {
  314. case SCH_SHEET_T:
  315. m_CurrentSheet->Push( (SCH_SHEET*) item );
  316. DisplayCurrentSheet();
  317. break;
  318. case SCH_COMPONENT_T:
  319. EditComponent( (SCH_COMPONENT*) item );
  320. m_canvas->MoveCursorToCrossHair();
  321. break;
  322. case SCH_TEXT_T:
  323. case SCH_LABEL_T:
  324. case SCH_GLOBAL_LABEL_T:
  325. case SCH_HIERARCHICAL_LABEL_T:
  326. EditSchematicText( (SCH_TEXT*) item );
  327. break;
  328. case SCH_BITMAP_T:
  329. EditImage( (SCH_BITMAP*) item );
  330. break;
  331. case SCH_FIELD_T:
  332. EditComponentFieldText( (SCH_FIELD*) item );
  333. m_canvas->MoveCursorToCrossHair();
  334. break;
  335. case SCH_MARKER_T:
  336. ( (SCH_MARKER*) item )->DisplayMarkerInfo( this );
  337. break;
  338. default:
  339. break;
  340. }
  341. break;
  342. case ID_BUS_BUTT:
  343. case ID_WIRE_BUTT:
  344. case ID_LINE_COMMENT_BUTT:
  345. if( item && item->IsNew() )
  346. EndSegment( aDC );
  347. break;
  348. }
  349. }