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.

654 lines
22 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2004 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
  5. * Copyright (C) 2008-2011 Wayne Stambaugh <stambaughw@verizon.net>
  6. * Copyright (C) 2004-2011 KiCad Developers, see change_log.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 libeditframe.h
  27. * @brief Definition of class LIB_EDIT_FRAME
  28. */
  29. #ifndef LIBEDITFRM_H_
  30. #define LIBEDITFRM_H_
  31. #include <sch_base_frame.h>
  32. #include <class_sch_screen.h>
  33. #include <lib_draw_item.h>
  34. #include <lib_collectors.h>
  35. class SCH_EDIT_FRAME;
  36. class CMP_LIBRARY;
  37. class LIB_COMPONENT;
  38. class LIB_ALIAS;
  39. class LIB_FIELD;
  40. class DIALOG_LIB_EDIT_TEXT;
  41. /**
  42. * The component library editor main window.
  43. */
  44. class LIB_EDIT_FRAME : public SCH_BASE_FRAME
  45. {
  46. LIB_COMPONENT* m_tempCopyComponent; ///< Temporary copy of current component during edit.
  47. LIB_COLLECTOR m_collectedItems; // Used for hit testing.
  48. wxComboBox* m_partSelectBox; // a Box to select a part to edit (if any)
  49. wxComboBox* m_aliasSelectBox; // a box to select the alias to edit (if any)
  50. wxString m_configPath;
  51. wxString m_lastLibImportPath;
  52. wxString m_lastLibExportPath;
  53. /** Convert of the item currently being drawn. */
  54. bool m_drawSpecificConvert;
  55. /**
  56. * Specify which component parts the current draw item applies to.
  57. *
  58. * If true, the item being drawn or edited applies only to the selected
  59. * part. Otherwise it applies to all parts in the component.
  60. */
  61. bool m_drawSpecificUnit;
  62. /**
  63. * Set to true to not synchronize pins at the same position when editing
  64. * components with multiple parts or multiple body styles. Setting this
  65. * to false allows editing each pin per part or body style individually.
  66. * This requires the user to open each part or body style to make changes
  67. * to the pin at the same location.
  68. */
  69. bool m_editPinsPerPartOrConvert;
  70. /** The current draw or edit graphic item fill style. */
  71. static FILL_T m_drawFillStyle;
  72. /** Default line width for drawing or editing graphic items. */
  73. static int m_drawLineWidth;
  74. /** The current active library. NULL if no active library is selected. */
  75. static CMP_LIBRARY* m_library;
  76. /** The current component being edited. NULL if no component is selected. */
  77. static LIB_COMPONENT* m_component;
  78. static LIB_ITEM* m_lastDrawItem;
  79. static LIB_ITEM* m_drawItem;
  80. static wxString m_aliasName;
  81. // The unit number to edit and show
  82. static int m_unit;
  83. // Show the normal shape ( m_convert <= 1 ) or the converted shape
  84. // ( m_convert > 1 )
  85. static int m_convert;
  86. // true to force DeMorgan/normal tools selection enabled.
  87. // They are enabled when the loaded component has
  88. // Graphic items for converted shape
  89. // But under some circumstances (New component created)
  90. // these tools must left enable
  91. static bool m_showDeMorgan;
  92. /// The current text size setting.
  93. static int m_textSize;
  94. /// Current text orientation setting.
  95. static int m_textOrientation;
  96. static wxSize m_clientSize;
  97. friend class DIALOG_LIB_EDIT_TEXT;
  98. LIB_ITEM* locateItem( const wxPoint& aPosition, const KICAD_T aFilterList[] );
  99. public:
  100. LIB_EDIT_FRAME( SCH_EDIT_FRAME* aParent, const wxString& aTitle,
  101. const wxPoint& aPosition, const wxSize& aSize,
  102. long aStyle = KICAD_DEFAULT_DRAWFRAME_STYLE );
  103. ~LIB_EDIT_FRAME();
  104. /**
  105. * Function GetLibEditFrameName (static)
  106. * @return the frame name used when creating the frame
  107. * used to get a reference to this frame, if exists
  108. */
  109. static const wxChar* GetLibEditFrameName();
  110. /**
  111. * Function GetActiveLibraryEditor (static)
  112. * @return a reference to the current opened Library editor
  113. * or NULL if no Library editor currently opened
  114. */
  115. static LIB_EDIT_FRAME* GetActiveLibraryEditor();
  116. void ReCreateMenuBar();
  117. /**
  118. * Function EnsureActiveLibExists
  119. * must be called after the libraries are reloaded
  120. * (for instance after loading a schematic project)
  121. */
  122. static void EnsureActiveLibExists();
  123. /**
  124. * Function SetLanguage
  125. * is called on a language menu selection
  126. */
  127. void SetLanguage( wxCommandEvent& event );
  128. void InstallConfigFrame( wxCommandEvent& event );
  129. void InstallDimensionsDialog( wxCommandEvent& event );
  130. void OnColorConfig( wxCommandEvent& aEvent );
  131. void Process_Config( wxCommandEvent& event );
  132. /**
  133. * Function SycnronizePins
  134. * @return True if the edit pins per part or convert is false and the current
  135. * component has multiple parts or body styles. Otherwise false is
  136. * returned.
  137. */
  138. bool SynchronizePins() const;
  139. /**
  140. * Function OnPlotCurrentComponent
  141. * plot the current component in SVG or PNG format.
  142. */
  143. void OnPlotCurrentComponent( wxCommandEvent& event );
  144. void Process_Special_Functions( wxCommandEvent& event );
  145. void OnSelectTool( wxCommandEvent& aEvent );
  146. /**
  147. * Routine to read one part.
  148. * The format is that of libraries, but it loads only 1 component.
  149. * Or 1 component if there are several.
  150. * If the first component is an alias, it will load the corresponding root.
  151. */
  152. void OnImportPart( wxCommandEvent& event );
  153. /**
  154. * Function OnExportPart
  155. * creates a new library and backup the current component in this library or export
  156. * the component of the current library.
  157. */
  158. void OnExportPart( wxCommandEvent& event );
  159. void OnSelectAlias( wxCommandEvent& event );
  160. void OnSelectPart( wxCommandEvent& event );
  161. /**
  162. * Function DeleteOnePart
  163. * is the command event handler to delete an entry from the current library.
  164. *
  165. * The deleted entry can be an alias or a component. If the entry is an alias,
  166. * it is removed from the component and the list of alias is updated. If the
  167. * entry is a component and the list of aliases is empty, the component and all
  168. * it drawable items are deleted. Otherwise the first alias in the alias list
  169. * becomes the new component name and the other aliases become dependent on
  170. * renamed component.
  171. *
  172. * @note This only deletes the entry in memory. The file does not change.
  173. */
  174. void DeleteOnePart( wxCommandEvent& event );
  175. /**
  176. * Function CreateNewLibraryPart
  177. * is the command event handler to create a new library component.
  178. *
  179. * If an old component is currently in edit, it is deleted.
  180. */
  181. void CreateNewLibraryPart( wxCommandEvent& event );
  182. void OnCreateNewPartFromExisting( wxCommandEvent& event );
  183. void OnEditComponentProperties( wxCommandEvent& event );
  184. void InstallFieldsEditorDialog( wxCommandEvent& event );
  185. /**
  186. * Function LoadOneLibraryPart
  187. * loads a library component from the currently selected library.
  188. *
  189. * If a library is already selected, the user is prompted for the component name
  190. * to load. If there is no current selected library, the user is prompted to select
  191. * a library name and then select component to load.
  192. */
  193. void LoadOneLibraryPart( wxCommandEvent& event );
  194. void OnViewEntryDoc( wxCommandEvent& event );
  195. void OnCheckComponent( wxCommandEvent& event );
  196. void OnSelectBodyStyle( wxCommandEvent& event );
  197. void OnEditPin( wxCommandEvent& event );
  198. void OnSelectItem( wxCommandEvent& aEvent );
  199. void OnUpdateSelectTool( wxUpdateUIEvent& aEvent );
  200. void OnUpdateEditingPart( wxUpdateUIEvent& event );
  201. void OnUpdateNotEditingPart( wxUpdateUIEvent& event );
  202. void OnUpdateUndo( wxUpdateUIEvent& event );
  203. void OnUpdateRedo( wxUpdateUIEvent& event );
  204. void OnUpdateSaveCurrentLib( wxUpdateUIEvent& event );
  205. void OnUpdateViewDoc( wxUpdateUIEvent& event );
  206. void OnUpdatePinByPin( wxUpdateUIEvent& event );
  207. void OnUpdatePartNumber( wxUpdateUIEvent& event );
  208. void OnUpdateDeMorganNormal( wxUpdateUIEvent& event );
  209. void OnUpdateDeMorganConvert( wxUpdateUIEvent& event );
  210. void OnUpdateSelectAlias( wxUpdateUIEvent& event );
  211. void UpdateAliasSelectList();
  212. void UpdatePartSelectList();
  213. /**
  214. * Function DisplayLibInfos
  215. * updates the main window title bar with the current library name and read only status
  216. * of the library.
  217. */
  218. void DisplayLibInfos();
  219. /**
  220. * Function RedrawComponent
  221. * Redraw the current component loaded in library editor
  222. * Display reference like in schematic (a reference U is shown U? or U?A)
  223. * accordint to the current selected unit and De Morgan selection
  224. * although it is stored without ? and part id.
  225. * @param aDC = the current device context
  226. * @param aOffset = a draw offset. usually 0,0 to draw on the screen, but
  227. * can be set to page size / 2 to draw or print in SVG format.
  228. */
  229. void RedrawComponent( wxDC* aDC, wxPoint aOffset );
  230. /**
  231. * Function RedrawActiveWindow
  232. * Redraw the current component loaded in library editor, an axes
  233. * Display reference like in schematic (a reference U is shown U? or U?A)
  234. * update status bar and info shown in the bottom of the window
  235. */
  236. void RedrawActiveWindow( wxDC* DC, bool EraseBg );
  237. void OnCloseWindow( wxCloseEvent& Event );
  238. void ReCreateHToolbar();
  239. void ReCreateVToolbar();
  240. void CreateOptionToolbar();
  241. void OnLeftClick( wxDC* DC, const wxPoint& MousePos );
  242. bool OnRightClick( const wxPoint& MousePos, wxMenu* PopMenu );
  243. double BestZoom(); // Returns the best zoom
  244. void OnLeftDClick( wxDC* DC, const wxPoint& MousePos );
  245. void OnHotKey( wxDC* aDC, int aHotKey, const wxPoint& aPosition, EDA_ITEM* aItem = NULL );
  246. void GeneralControl( wxDC* aDC, const wxPoint& aPosition, int aHotKey = 0 );
  247. /**
  248. * Function LoadSettings
  249. * loads the library editor frame specific configuration settings.
  250. *
  251. * Don't forget to call this method from any derived classes or the settings will not
  252. * get loaded.
  253. */
  254. void LoadSettings();
  255. /**
  256. * Function SaveSettings
  257. * saves the library editor frame specific configuration settings.
  258. *
  259. * Don't forget to call this base method from any derived classes or the settings will
  260. * not get saved.
  261. */
  262. void SaveSettings();
  263. /**
  264. * Function CloseWindow
  265. * triggers the wxCloseEvent, which is handled by the function given
  266. * to EVT_CLOSE() macro:
  267. * <p>
  268. * EVT_CLOSE( LIB_EDIT_FRAME::OnCloseWindow )
  269. */
  270. void CloseWindow( wxCommandEvent& event )
  271. {
  272. // Generate a wxCloseEvent
  273. Close( false );
  274. }
  275. /**
  276. * Function OnModify
  277. * Must be called after a schematic change
  278. * in order to set the "modify" flag of the current screen
  279. */
  280. void OnModify()
  281. {
  282. GetScreen()->SetModify();
  283. }
  284. LIB_COMPONENT* GetComponent( void ) { return m_component; }
  285. CMP_LIBRARY* GetLibrary( void ) { return m_library; }
  286. wxString& GetAliasName( void ) { return m_aliasName; }
  287. int GetUnit( void ) { return m_unit; }
  288. void SetUnit( int unit )
  289. {
  290. wxASSERT( unit >= 1 );
  291. m_unit = unit;
  292. }
  293. int GetConvert( void ) { return m_convert; }
  294. void SetConvert( int convert )
  295. {
  296. wxASSERT( convert >= 0 );
  297. m_convert = convert;
  298. }
  299. LIB_ITEM* GetLastDrawItem( void ) { return m_lastDrawItem; }
  300. void SetLastDrawItem( LIB_ITEM* drawItem )
  301. {
  302. m_lastDrawItem = drawItem;
  303. }
  304. LIB_ITEM* GetDrawItem( void ) { return m_drawItem; }
  305. void SetDrawItem( LIB_ITEM* drawItem );
  306. bool GetShowDeMorgan( void ) { return m_showDeMorgan; }
  307. void SetShowDeMorgan( bool show ) { m_showDeMorgan = show; }
  308. FILL_T GetFillStyle( void ) { return m_drawFillStyle; }
  309. /**
  310. * Function TempCopyComponent
  311. * create a temporary copy of the current edited component
  312. * Used to prepare an Undo ant/or abort command before editing the component
  313. */
  314. void TempCopyComponent();
  315. /**
  316. * Function RestoreComponent
  317. * Restore the current edited component from its temporary copy.
  318. * Used to abort a command
  319. */
  320. void RestoreComponent();
  321. /**
  322. * Function GetTempCopyComponent
  323. * @return the temporary copy of the current component.
  324. */
  325. LIB_COMPONENT* GetTempCopyComponent() { return m_tempCopyComponent; }
  326. /**
  327. * Function ClearTempCopyComponent
  328. * delete temporary copy of the current component and clear pointer
  329. */
  330. void ClearTempCopyComponent();
  331. bool IsEditingDrawItem() { return m_drawItem && m_drawItem->InEditMode(); }
  332. private:
  333. /**
  334. * Function OnActivate
  335. * is called when the frame is activated. Tests if the current library exists.
  336. * The library list can be changed by the schematic editor after reloading a new schematic
  337. * and the current m_library can point a non existent lib.
  338. */
  339. virtual void OnActivate( wxActivateEvent& event );
  340. // General:
  341. /**
  342. * Function SaveOnePartInMemory
  343. * updates the current component being edited in the active library.
  344. *
  345. * Any changes are updated in memory only and NOT to a file. The old component is
  346. * deleted from the library and/or any aliases before the edited component is updated
  347. * in the library.
  348. */
  349. void SaveOnePartInMemory();
  350. /**
  351. * Function SelectActiveLibrary
  352. * sets the current active library to \a aLibrary.
  353. *
  354. * @param aLibrary A pointer to the CMP_LIBRARY object to select. If NULL, then display
  355. * list of available libraries to select from.
  356. */
  357. void SelectActiveLibrary( CMP_LIBRARY* aLibrary = NULL );
  358. /**
  359. * Function OnSaveActiveLibrary
  360. * it the command event handler to save the changes to the current library.
  361. *
  362. * A backup file of the current library is saved with the .bak extension before the
  363. * changes made to the library are saved.
  364. */
  365. void OnSaveActiveLibrary( wxCommandEvent& event );
  366. /**
  367. * Function SaveActiveLibrary
  368. * saves the changes to the current library.
  369. *
  370. * A backup file of the current library is saved with the .bak extension before the
  371. * changes made to the library are saved.
  372. * @param newFile Ask for a new file name to save the library.
  373. * @return True if the library was successfully saved.
  374. */
  375. bool SaveActiveLibrary( bool newFile );
  376. /**
  377. * Function LoadComponentFromCurrentLib
  378. * loads a component from the current active library.
  379. * @param aLibEntry The component to load from \a aLibrary (can be an alias)
  380. * @return true if \a aLibEntry loaded correctly.
  381. */
  382. bool LoadComponentFromCurrentLib( LIB_ALIAS* aLibEntry );
  383. /**
  384. * Function LoadOneLibraryPartAux
  385. * loads a copy of \a aLibEntry from \a aLibrary into memory.
  386. *
  387. * @param aLibEntry A pointer to the LIB_ALIAS object to load.
  388. * @param aLibrary A pointer to the CMP_LIBRARY object to load \a aLibEntry from.
  389. * @return True if a copy of \a aLibEntry was successfully loaded from \a aLibrary.
  390. */
  391. bool LoadOneLibraryPartAux( LIB_ALIAS* aLibEntry, CMP_LIBRARY* aLibrary );
  392. /**
  393. * Function DisplayCmpDoc
  394. * displays the documentation of the selected component.
  395. */
  396. void DisplayCmpDoc();
  397. /**
  398. * Function OnRotateItem
  399. * rotates the current item.
  400. */
  401. void OnRotateItem( wxCommandEvent& aEvent );
  402. /**
  403. * Function deleteItem
  404. * deletes the currently selected draw item.
  405. * @param aDC The device context to draw upon when removing item.
  406. */
  407. void deleteItem( wxDC* aDC );
  408. // General editing
  409. public:
  410. void SaveCopyInUndoList( EDA_ITEM* ItemToCopy, int flag_type_command = 0 );
  411. private:
  412. void GetComponentFromUndoList( wxCommandEvent& event );
  413. void GetComponentFromRedoList( wxCommandEvent& event );
  414. // Editing pins
  415. void CreatePin( wxDC* DC );
  416. void StartMovePin( wxDC* DC );
  417. /**
  418. * Function CreateImagePins
  419. * adds copies of \a aPin for \a aUnit in components with multiple parts and
  420. * \a aConvert for components that have multiple body styles.
  421. *
  422. * @param aPin The pin to copy.
  423. * @param aUnit The unit to add a copy of \a aPin to.
  424. * @param aConvert The alternate body style to add a copy of \a aPin to.
  425. * @param aDeMorgan Flag to indicate if \a aPin should be created for the
  426. * alternate body style.
  427. */
  428. void CreateImagePins( LIB_PIN* aPin, int aUnit, int aConvert, bool aDeMorgan );
  429. /**
  430. * Function PlaceAnchor
  431. * places an anchor reference coordinate for the current component.
  432. * <p>
  433. * All object coordinates are offset to the current cursor position.
  434. * </p>
  435. */
  436. void PlaceAnchor();
  437. // Editing graphic items
  438. LIB_ITEM* CreateGraphicItem( LIB_COMPONENT* LibEntry, wxDC* DC );
  439. void GraphicItemBeginDraw( wxDC* DC );
  440. void StartMoveDrawSymbol( wxDC* DC );
  441. void StartModifyDrawSymbol( wxDC* DC ); //<! Modify the item, adjust size etc.
  442. void EndDrawGraphicItem( wxDC* DC );
  443. /**
  444. * Function LoadOneSymbol
  445. * read a component symbol file (*.sym ) and add graphic items to the current component.
  446. * <p>
  447. * A symbol file *.sym has the same format as a library, and contains only
  448. * one symbol.
  449. * </p>
  450. */
  451. void LoadOneSymbol();
  452. /**
  453. * Function SaveOneSymbol
  454. * saves the current component to a symbol file.
  455. * <p>
  456. * The symbol file format is similar to the standard component library file format, but
  457. * there is only one symbol. Invisible pins are not saved.
  458. */
  459. void SaveOneSymbol();
  460. void EditGraphicSymbol( wxDC* DC, LIB_ITEM* DrawItem );
  461. void EditSymbolText( wxDC* DC, LIB_ITEM* DrawItem );
  462. LIB_ITEM* LocateItemUsingCursor( const wxPoint& aPosition,
  463. const KICAD_T aFilterList[] = LIB_COLLECTOR::AllItems );
  464. void EditField( LIB_FIELD* Field );
  465. public:
  466. /**
  467. * Function LoadComponentAndSelectLib
  468. * selects the current active library.
  469. *
  470. * @param aLibrary The CMP_LIBRARY to select
  471. * @param aLibEntry The component to load from aLibrary (can be an alias).
  472. * @return true if \a aLibEntry was loaded from \a aLibrary.
  473. */
  474. bool LoadComponentAndSelectLib( LIB_ALIAS* aLibEntry, CMP_LIBRARY* aLibrary );
  475. /* Block commands: */
  476. /**
  477. * Function ReturnBlockCommand
  478. * returns the block command (BLOCK_MOVE, BLOCK_COPY...) corresponding to
  479. * the \a aKey (ALT, SHIFT ALT ..)
  480. */
  481. virtual int ReturnBlockCommand( int aKey );
  482. /**
  483. * Function HandleBlockPlace
  484. * handles the block place command.
  485. */
  486. virtual void HandleBlockPlace( wxDC* DC );
  487. /**
  488. * Function HandleBlockEnd
  489. * performs a block end command.
  490. * @return If command finished (zoom, delete ...) false is returned otherwise true
  491. * is returned indicating more processing is required.
  492. */
  493. virtual bool HandleBlockEnd( wxDC* DC );
  494. /**
  495. * Function PlacePin
  496. * Place at cursor location the pin currently moved (i.e. pin pointed by m_drawItem)
  497. * (and the linked pins, if any)
  498. */
  499. void PlacePin();
  500. /**
  501. * Function GlobalSetPins
  502. * @param aMasterPin is the "template" pin
  503. * @param aId is a param to select what should be mofified:
  504. * - aId = ID_POPUP_LIBEDIT_PIN_GLOBAL_CHANGE_PINNAMESIZE_ITEM:
  505. * Change pins text name size
  506. * - aId = ID_POPUP_LIBEDIT_PIN_GLOBAL_CHANGE_PINNUMSIZE_ITEM:
  507. * Change pins text num size
  508. * - aId = ID_POPUP_LIBEDIT_PIN_GLOBAL_CHANGE_PINSIZE_ITEM:
  509. * Change pins length.
  510. *
  511. * If aMasterPin is selected ( .m_flag == IS_SELECTED ),
  512. * only the other selected pins are modified
  513. */
  514. void GlobalSetPins( LIB_PIN* aMasterPin, int aId );
  515. // Automatic placement of pins
  516. void RepeatPinItem( wxDC* DC, LIB_PIN* Pin );
  517. /**
  518. * Function CreatePNGorJPEGFile
  519. * creates an image (screenshot) of the current component in PNG or JPEG format.
  520. * @param aFileName = the full filename
  521. * @param aFmt_jpeg = true to use JPEG file format, false to use PNG file format
  522. */
  523. void CreatePNGorJPEGFile( const wxString& aFileName, bool aFmt_jpeg );
  524. /**
  525. * Virtual function PrintPage
  526. * used to print a page
  527. * @param aDC = wxDC given by the calling print function
  528. * @param aPrintMask = not used here
  529. * @param aPrintMirrorMode = not used here (Set when printing in mirror mode)
  530. * @param aData = a pointer on an auxiliary data (not always used, NULL if not used)
  531. */
  532. virtual void PrintPage( wxDC* aDC, LAYER_MSK aPrintMask,
  533. bool aPrintMirrorMode, void* aData = NULL );
  534. /**
  535. * Function SVG_PlotComponent
  536. * Creates the SVG print file for the current edited component.
  537. * @param aFullFileName = the full filename
  538. */
  539. void SVG_PlotComponent( const wxString& aFullFileName );
  540. DECLARE_EVENT_TABLE()
  541. };
  542. #endif // LIBEDITFRM_H_