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.

291 lines
7.0 KiB

  1. /**
  2. * @file design_tree_frame.cpp
  3. */
  4. /*
  5. * This program source code file is part of KiCad, a free EDA CAD application.
  6. *
  7. * Copyright (C) 2013 CERN
  8. * @author Jean-Pierre Charras, jp.charras at wanadoo.fr
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version 2
  13. * of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, you may find one here:
  22. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  23. * or you may search the http://www.gnu.org website for the version 2 license,
  24. * or you may write to the Free Software Foundation, Inc.,
  25. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  26. */
  27. #include <wx/imaglist.h>
  28. #include <wx/wupdlock.h>
  29. #include <fctsys.h>
  30. #include <worksheet_shape_builder.h>
  31. #include <worksheet_dataitem.h>
  32. #include <pl_editor_id.h>
  33. #include <design_tree_frame.h>
  34. /* XPM
  35. * This bitmap is used to show item types
  36. */
  37. static const char* root_xpm[] =
  38. {
  39. "12 12 2 1",
  40. " c None",
  41. "x c #008080",
  42. " xxxx ",
  43. " xxx ",
  44. " xxx ",
  45. " xxx ",
  46. "xxxxxxxxxxx ",
  47. "xxxxxxxxxxxx",
  48. "xxxxxxxxxxx ",
  49. " xxx ",
  50. " xxx ",
  51. " xxx ",
  52. " xxxx ",
  53. " "
  54. };
  55. static const char* line_xpm[] =
  56. {
  57. "12 12 2 1",
  58. " c None",
  59. "x c #008080",
  60. "xx ",
  61. "xx ",
  62. "xx ",
  63. "xx ",
  64. "xx ",
  65. "xx ",
  66. "xx ",
  67. "xx ",
  68. "xx ",
  69. "xx ",
  70. "xxxxxxxxxxxx",
  71. "xxxxxxxxxxxx"
  72. };
  73. static const char* rect_xpm[] =
  74. {
  75. "12 12 2 1",
  76. " c None",
  77. "x c #000080",
  78. "xxxxxxxxxxxx",
  79. "xxxxxxxxxxxx",
  80. "xx xx",
  81. "xx xx",
  82. "xx xx",
  83. "xx xx",
  84. "xx xx",
  85. "xx xx",
  86. "xx xx",
  87. "xx xx",
  88. "xxxxxxxxxxxx",
  89. "xxxxxxxxxxxx"
  90. };
  91. static const char* text_xpm[] =
  92. {
  93. "12 12 2 1",
  94. " c None",
  95. "x c #800000",
  96. " xxxxxxxxxx ",
  97. "xxxxxxxxxxxx",
  98. "xx xx xx",
  99. " xx ",
  100. " xx ",
  101. " xx ",
  102. " xx ",
  103. " xx ",
  104. " xx ",
  105. " xx ",
  106. " xxxx ",
  107. " xxxxxx "
  108. };
  109. static const char* poly_xpm[] =
  110. {
  111. "12 12 2 1",
  112. " c None",
  113. "x c #008000",
  114. " xx ",
  115. " xxxx ",
  116. " xxxxxx ",
  117. " xxxxxxxx ",
  118. " xxxxxxxxxx ",
  119. "xxxxxxxxxxxx",
  120. "xxxxxxxxxxxx",
  121. " xxxxxxxxxx ",
  122. " xxxxxxxx ",
  123. " xxxxxx ",
  124. " xxxx ",
  125. " xx "
  126. };
  127. static const char* img_xpm[] =
  128. {
  129. "12 12 2 1",
  130. " c None",
  131. "x c #800000",
  132. " xx ",
  133. " xxxxxx ",
  134. " xx xx ",
  135. "xx xx",
  136. "xx xx",
  137. " xx xx ",
  138. " xxxxxx ",
  139. " xx ",
  140. " xx ",
  141. " xx ",
  142. " xx ",
  143. " xx "
  144. };
  145. // Event table:
  146. DESIGN_TREE_FRAME::DESIGN_TREE_FRAME( PL_EDITOR_FRAME* aParent ) :
  147. wxTreeCtrl( aParent, ID_DESIGN_TREE_FRAME )
  148. {
  149. // icons size is not know (depending on they are built)
  150. // so get it:
  151. wxSize iconsize;
  152. wxBitmap root_bm( root_xpm );
  153. iconsize.x = root_bm.GetWidth();
  154. iconsize.y = root_bm.GetHeight();
  155. // Make an image list containing small icons
  156. m_imageList = new wxImageList( iconsize.x, iconsize.y, true, 6 );
  157. m_imageList->Add( root_bm ); // root symbol
  158. m_imageList->Add( wxBitmap( line_xpm ) ); // line item
  159. m_imageList->Add( wxBitmap( rect_xpm ) ); // rect item
  160. m_imageList->Add( wxBitmap( text_xpm ) ); // text item
  161. m_imageList->Add( wxBitmap( poly_xpm ) ); // poly item
  162. m_imageList->Add( wxBitmap( img_xpm ) ); // bitmap item
  163. SetImageList( m_imageList );
  164. }
  165. DESIGN_TREE_FRAME::~DESIGN_TREE_FRAME()
  166. {
  167. delete m_imageList;
  168. }
  169. wxSize DESIGN_TREE_FRAME::GetMinSize() const
  170. {
  171. return wxSize( 100, -1 );
  172. }
  173. void DESIGN_TREE_FRAME::ReCreateDesignTree()
  174. {
  175. wxWindowUpdateLocker dummy(this); // Avoid flicker when rebuilding the tree
  176. DeleteAllItems();
  177. const WORKSHEET_LAYOUT& pglayout = WORKSHEET_LAYOUT::GetTheInstance();
  178. // root tree:
  179. wxFileName fn( ((PL_EDITOR_FRAME*) GetParent())->GetCurrFileName() );
  180. wxTreeItemId rootitem;
  181. if( fn.GetName().IsEmpty() )
  182. rootitem = AddRoot( wxT( "<default>" ), 0, 0 );
  183. else
  184. rootitem = AddRoot( fn.GetName(), 0, 0 );
  185. SetItemBold( rootitem, true );
  186. // Now adding all current items
  187. for( unsigned ii = 0; ii < pglayout.GetCount(); ii++ )
  188. {
  189. WORKSHEET_DATAITEM* item = pglayout.GetItem( ii );
  190. int img = 0;
  191. switch( item->GetType() )
  192. {
  193. case WORKSHEET_DATAITEM::WS_SEGMENT: img = 1; break;
  194. case WORKSHEET_DATAITEM::WS_RECT: img = 2; break;
  195. case WORKSHEET_DATAITEM::WS_TEXT: img = 3; break;
  196. case WORKSHEET_DATAITEM::WS_POLYPOLYGON: img = 4; break;
  197. case WORKSHEET_DATAITEM::WS_BITMAP: img = 5; break;
  198. }
  199. wxTreeItemId cell= AppendItem( rootitem, item->m_Name, img, img );
  200. DESIGN_TREE_ITEM_DATA* data = new DESIGN_TREE_ITEM_DATA( item );
  201. SetItemData( cell, data );
  202. }
  203. Expand( rootitem );
  204. }
  205. // Select the tree item corresponding to the WORKSHEET_DATAITEM aItem
  206. void DESIGN_TREE_FRAME::SelectCell( WORKSHEET_DATAITEM* aItem )
  207. {
  208. wxTreeItemId rootcell = GetRootItem();
  209. wxTreeItemIdValue cookie;
  210. wxTreeItemId cell = GetFirstChild( rootcell, cookie );
  211. while( cell.IsOk() )
  212. {
  213. DESIGN_TREE_ITEM_DATA* data = (DESIGN_TREE_ITEM_DATA*) GetItemData( cell );
  214. if( data->GetItem() == aItem )
  215. {
  216. SelectItem( cell );
  217. return;
  218. }
  219. cell = GetNextChild( rootcell, cookie );
  220. }
  221. }
  222. //return the page layout item managed by the cell
  223. WORKSHEET_DATAITEM* DESIGN_TREE_FRAME::GetPageLayoutItem( wxTreeItemId aCell ) const
  224. {
  225. DESIGN_TREE_ITEM_DATA* data = (DESIGN_TREE_ITEM_DATA*) GetItemData( aCell );
  226. if( data )
  227. return data->GetItem();
  228. else
  229. return NULL;
  230. }
  231. /* return the page layout item managed by the selected cell (or NULL)
  232. */
  233. WORKSHEET_DATAITEM* DESIGN_TREE_FRAME::GetPageLayoutSelectedItem() const
  234. {
  235. wxTreeItemId cell;
  236. cell = GetSelection();
  237. if( cell.IsOk() )
  238. return GetPageLayoutItem( cell );
  239. return NULL;
  240. }
  241. /* return the page layout item index managed by the selected cell (or -1)
  242. */
  243. int DESIGN_TREE_FRAME::GetSelectedItemIndex()
  244. {
  245. WORKSHEET_DATAITEM*item = GetPageLayoutSelectedItem();
  246. if( item == NULL )
  247. return -1;
  248. WORKSHEET_LAYOUT& pglayout = WORKSHEET_LAYOUT::GetTheInstance();
  249. return pglayout.GetItemIndex( item );
  250. }