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.

981 lines
28 KiB

16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
9 years ago
9 years ago
9 years ago
9 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2010 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
  5. * Copyright (C) 2010-2020 KiCad Developers, see change_log.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. /* This source module implements the layer visibility and selection widget
  25. @todo make bitmap size dependent on the point size.
  26. */
  27. //#define STAND_ALONE 1 // define to enable test program for LAYER_WIDGET
  28. #include <layer_widget.h>
  29. #include <macros.h>
  30. #include <common.h>
  31. #include <widgets/indicator_icon.h>
  32. #include <algorithm>
  33. #include <menus_helpers.h>
  34. const wxEventType LAYER_WIDGET::EVT_LAYER_COLOR_CHANGE = wxNewEventType();
  35. /**
  36. * Function shrinkFont
  37. * reduces the size of the wxFont associated with \a aControl
  38. */
  39. static void shrinkFont( wxWindow* aControl, int aPointSize )
  40. {
  41. wxFont font = aControl->GetFont();
  42. font.SetPointSize( aPointSize );
  43. aControl->SetFont( font ); // need this?
  44. }
  45. int LAYER_WIDGET::encodeId( int aColumn, int aId )
  46. {
  47. int id = aId * LYR_COLUMN_COUNT + aColumn;
  48. return id;
  49. }
  50. LAYER_NUM LAYER_WIDGET::getDecodedId( int aControlId )
  51. {
  52. int id = aControlId / LYR_COLUMN_COUNT; // rounding is OK.
  53. return id;
  54. }
  55. void LAYER_WIDGET::OnLeftDownLayers( wxMouseEvent& event )
  56. {
  57. int row;
  58. LAYER_NUM layer;
  59. wxWindow* eventSource = (wxWindow*) event.GetEventObject();
  60. // if mouse event is coming from the m_LayerScrolledWindow and not one
  61. // of its children, we have to find the row manually based on y coord.
  62. if( eventSource == m_LayerScrolledWindow )
  63. {
  64. int y = event.GetY();
  65. wxArrayInt heights = m_LayersFlexGridSizer->GetRowHeights();
  66. int height = 0;
  67. int rowCount = GetLayerRowCount();
  68. for( row = 0; row<rowCount; ++row )
  69. {
  70. if( y < height + heights[row] )
  71. break;
  72. height += heights[row];
  73. }
  74. if( row >= rowCount )
  75. row = rowCount - 1;
  76. layer = getDecodedId( getLayerComp( row, 0 )->GetId() );
  77. }
  78. else
  79. {
  80. // all nested controls on a given row will have their ID encoded with
  81. // encodeId(), and the corresponding decoding is getDecodedId()
  82. int id = eventSource->GetId();
  83. layer = getDecodedId( id );
  84. row = findLayerRow( layer );
  85. }
  86. if( OnLayerSelect( layer ) ) // if client allows this change.
  87. SelectLayerRow( row );
  88. passOnFocus();
  89. }
  90. void LAYER_WIDGET::OnRightDownLayer( wxMouseEvent& aEvent, COLOR_SWATCH* aColorSwatch, const wxString& aLayerName )
  91. {
  92. wxMenu menu;
  93. AddMenuItem( &menu, ID_CHANGE_LAYER_COLOR,
  94. _( "Change Layer Color for " ) + aLayerName,
  95. KiBitmap( setcolor_copper_xpm ) );
  96. menu.AppendSeparator();
  97. OnLayerRightClick( menu );
  98. menu.Bind( wxEVT_COMMAND_MENU_SELECTED, [aColorSwatch]( wxCommandEvent& event ) {
  99. if ( event.GetId() == ID_CHANGE_LAYER_COLOR ) {
  100. aColorSwatch->GetNewSwatchColor();
  101. } else {
  102. event.Skip();
  103. }
  104. } );
  105. PopupMenu( &menu );
  106. passOnFocus();
  107. }
  108. void LAYER_WIDGET::OnLayerSwatchChanged( wxCommandEvent& aEvent )
  109. {
  110. COLOR_SWATCH* eventSource = static_cast<COLOR_SWATCH*>( aEvent.GetEventObject() );
  111. COLOR4D newColor = eventSource->GetSwatchColor();
  112. LAYER_NUM layer = getDecodedId( eventSource->GetId() );
  113. // tell the client code.
  114. OnLayerColorChange( layer, newColor );
  115. // notify others
  116. wxCommandEvent event( EVT_LAYER_COLOR_CHANGE );
  117. wxPostEvent( this, event );
  118. passOnFocus();
  119. }
  120. void LAYER_WIDGET::OnLayerCheckBox( wxCommandEvent& event )
  121. {
  122. wxCheckBox* eventSource = (wxCheckBox*) event.GetEventObject();
  123. LAYER_NUM layer = getDecodedId( eventSource->GetId() );
  124. OnLayerVisible( layer, eventSource->IsChecked() );
  125. passOnFocus();
  126. }
  127. void LAYER_WIDGET::OnRightDownRender( wxMouseEvent& aEvent, COLOR_SWATCH* aColorSwatch, const wxString& aRenderName )
  128. {
  129. wxMenu menu;
  130. AddMenuItem( &menu, ID_CHANGE_RENDER_COLOR,
  131. _( "Change Render Color for " ) + aRenderName,
  132. KiBitmap( setcolor_board_body_xpm ) );
  133. menu.Bind( wxEVT_COMMAND_MENU_SELECTED,
  134. [aColorSwatch]( wxCommandEvent& event )
  135. {
  136. if( event.GetId() == ID_CHANGE_RENDER_COLOR )
  137. aColorSwatch->GetNewSwatchColor();
  138. else
  139. event.Skip();
  140. } );
  141. PopupMenu( &menu );
  142. passOnFocus();
  143. }
  144. void LAYER_WIDGET::OnRenderSwatchChanged( wxCommandEvent& aEvent )
  145. {
  146. auto eventSource = static_cast<COLOR_SWATCH*>( aEvent.GetEventObject() );
  147. COLOR4D newColor = eventSource->GetSwatchColor();
  148. LAYER_NUM id = getDecodedId( eventSource->GetId() );
  149. if( id == LAYER_PCB_BACKGROUND )
  150. {
  151. // Update all swatch backgrounds
  152. int count = GetLayerRowCount();
  153. int row;
  154. int col = 1; // bitmap button is column 1 in layers tab
  155. for( row = 0; row < count; ++row )
  156. {
  157. COLOR_SWATCH* swatch = dynamic_cast<COLOR_SWATCH*>( getLayerComp( row, col ) );
  158. if( swatch )
  159. swatch->SetSwatchBackground( newColor );
  160. }
  161. count = GetRenderRowCount();
  162. col = 0; // bitmap button is column 0 in render tab
  163. for( row = 0; row < count; ++row )
  164. {
  165. COLOR_SWATCH* swatch = dynamic_cast<COLOR_SWATCH*>( getRenderComp( row, col ) );
  166. if( swatch )
  167. swatch->SetSwatchBackground( newColor );
  168. }
  169. }
  170. // tell the client code.
  171. OnRenderColorChange( id, newColor );
  172. passOnFocus();
  173. }
  174. void LAYER_WIDGET::OnRenderCheckBox( wxCommandEvent& event )
  175. {
  176. wxCheckBox* eventSource = (wxCheckBox*) event.GetEventObject();
  177. LAYER_NUM id = getDecodedId( eventSource->GetId() );
  178. OnRenderEnable( id, eventSource->IsChecked() );
  179. passOnFocus();
  180. }
  181. void LAYER_WIDGET::OnTabChange( wxNotebookEvent& event )
  182. {
  183. // wxFocusEvent event( wxEVT_SET_FOCUS );
  184. // m_FocusOwner->AddPendingEvent( event );
  185. passOnFocus(); // does not work in this context, probably because we have receive control here too early.
  186. }
  187. wxWindow* LAYER_WIDGET::getLayerComp( int aRow, int aColumn ) const
  188. {
  189. unsigned ndx = aRow * LYR_COLUMN_COUNT + aColumn;
  190. if( ndx < m_LayersFlexGridSizer->GetChildren().GetCount() )
  191. return m_LayersFlexGridSizer->GetChildren()[ndx]->GetWindow();
  192. return NULL;
  193. }
  194. int LAYER_WIDGET::findLayerRow( LAYER_NUM aLayer ) const
  195. {
  196. int count = GetLayerRowCount();
  197. for( int row = 0; row < count; ++row )
  198. {
  199. // column 0 in the layer scroll window has a wxStaticBitmap, get its ID.
  200. wxWindow* w = getLayerComp( row, 0 );
  201. wxASSERT( w );
  202. if( aLayer == getDecodedId( w->GetId() ))
  203. return row;
  204. }
  205. return -1;
  206. }
  207. wxWindow* LAYER_WIDGET::getRenderComp( int aRow, int aColumn ) const
  208. {
  209. int ndx = aRow * RND_COLUMN_COUNT + aColumn;
  210. if( (unsigned) ndx < m_RenderFlexGridSizer->GetChildren().GetCount() )
  211. return m_RenderFlexGridSizer->GetChildren()[ndx]->GetWindow();
  212. return NULL;
  213. }
  214. int LAYER_WIDGET::findRenderRow( int aId ) const
  215. {
  216. int count = GetRenderRowCount();
  217. for( int row = 0; row < count; ++row )
  218. {
  219. // column 0 in the layer scroll window has a wxStaticBitmap, get its ID.
  220. wxWindow* w = getRenderComp( row, 0 );
  221. wxASSERT( w );
  222. if( aId == getDecodedId( w->GetId() ))
  223. return row;
  224. }
  225. return -1;
  226. }
  227. void LAYER_WIDGET::insertLayerRow( int aRow, const ROW& aSpec )
  228. {
  229. wxASSERT( aRow >= 0 );
  230. int col;
  231. int index = aRow * LYR_COLUMN_COUNT;
  232. const int flags = wxALIGN_CENTER_VERTICAL | wxALIGN_LEFT;
  233. // column 0
  234. col = COLUMN_ICON_ACTIVE;
  235. auto sbm = new INDICATOR_ICON( m_LayerScrolledWindow, *m_IconProvider,
  236. ROW_ICON_PROVIDER::STATE::OFF, encodeId( col, aSpec.id ) );
  237. sbm->Bind( wxEVT_LEFT_DOWN, &LAYER_WIDGET::OnLeftDownLayers, this );
  238. m_LayersFlexGridSizer->wxSizer::Insert( index+col, sbm, 0, flags );
  239. // column 1 (COLUMN_COLORBM)
  240. col = COLUMN_COLORBM;
  241. auto bmb = new COLOR_SWATCH( m_LayerScrolledWindow, aSpec.color, encodeId( col, aSpec.id ),
  242. getBackgroundLayerColor(), aSpec.defaultColor, SWATCH_SMALL );
  243. bmb->Bind( wxEVT_LEFT_DOWN, &LAYER_WIDGET::OnLeftDownLayers, this );
  244. bmb->Bind( COLOR_SWATCH_CHANGED, &LAYER_WIDGET::OnLayerSwatchChanged, this );
  245. bmb->SetToolTip( _("Left double click or middle click for color change, right click for menu" ) );
  246. m_LayersFlexGridSizer->wxSizer::Insert( index+col, bmb, 0, flags );
  247. // column 2 (COLUMN_COLOR_LYR_CB)
  248. col = COLUMN_COLOR_LYR_CB;
  249. wxCheckBox* cb = new wxCheckBox( m_LayerScrolledWindow, encodeId( col, aSpec.id ), wxEmptyString );
  250. cb->SetValue( aSpec.state );
  251. cb->Bind( wxEVT_COMMAND_CHECKBOX_CLICKED, &LAYER_WIDGET::OnLayerCheckBox, this );
  252. cb->SetToolTip( _( "Enable this for visibility" ) );
  253. m_LayersFlexGridSizer->wxSizer::Insert( index+col, cb, 0, flags );
  254. // column 3 (COLUMN_COLOR_LYRNAME)
  255. col = COLUMN_COLOR_LYRNAME;
  256. wxStaticText* st = new wxStaticText( m_LayerScrolledWindow, encodeId( col, aSpec.id ), aSpec.rowName );
  257. shrinkFont( st, m_PointSize );
  258. st->Bind( wxEVT_LEFT_DOWN, &LAYER_WIDGET::OnLeftDownLayers, this );
  259. st->SetToolTip( aSpec.tooltip );
  260. m_LayersFlexGridSizer->wxSizer::Insert( index+col, st, 0, flags );
  261. // column 4 (COLUMN_ALPHA_INDICATOR)
  262. col = COLUMN_ALPHA_INDICATOR;
  263. sbm = new INDICATOR_ICON( m_LayerScrolledWindow, *m_IconProvider,
  264. ROW_ICON_PROVIDER::STATE::OFF, wxID_ANY );
  265. m_LayersFlexGridSizer->wxSizer::Insert( index+col, sbm, 0, flags );
  266. // Bind right click eventhandler to all columns
  267. wxString layerName( aSpec.rowName );
  268. sbm->Bind( wxEVT_RIGHT_DOWN, [this, bmb, layerName] ( wxMouseEvent& aEvt )
  269. {
  270. OnRightDownLayer( aEvt, bmb, layerName );
  271. } );
  272. bmb->Bind( wxEVT_RIGHT_DOWN, [this, bmb, layerName] ( wxMouseEvent& aEvt )
  273. {
  274. OnRightDownLayer( aEvt, bmb, layerName );
  275. } );
  276. cb->Bind( wxEVT_RIGHT_DOWN, [this, bmb, layerName] ( wxMouseEvent& aEvt )
  277. {
  278. OnRightDownLayer( aEvt, bmb, layerName );
  279. } );
  280. st->Bind( wxEVT_RIGHT_DOWN, [this, bmb, layerName] ( wxMouseEvent& aEvt )
  281. {
  282. OnRightDownLayer( aEvt, bmb, layerName );
  283. } );
  284. }
  285. void LAYER_WIDGET::updateLayerRow( int aRow, const wxString& aName )
  286. {
  287. wxStaticText* label = dynamic_cast<wxStaticText*>( getLayerComp( aRow, COLUMN_COLOR_LYRNAME ) );
  288. if( label )
  289. label->SetLabel( aName );
  290. INDICATOR_ICON* indicator = (INDICATOR_ICON*) getLayerComp( aRow, 0 );
  291. if( indicator )
  292. {
  293. if( aRow == m_CurrentRow )
  294. indicator->SetIndicatorState( ROW_ICON_PROVIDER::STATE::ON );
  295. if( useAlternateBitmap( aRow ) )
  296. indicator->SetIndicatorState( ROW_ICON_PROVIDER::STATE::DIMMED );
  297. else
  298. indicator->SetIndicatorState( ROW_ICON_PROVIDER::STATE::OFF );
  299. }
  300. }
  301. void LAYER_WIDGET::insertRenderRow( int aRow, const ROW& aSpec )
  302. {
  303. wxASSERT( aRow >= 0 );
  304. int col;
  305. int index = aRow * RND_COLUMN_COUNT;
  306. const int flags = wxALIGN_CENTER_VERTICAL | wxALIGN_LEFT;
  307. wxString renderName( aSpec.rowName );
  308. wxCheckBox* cb = nullptr;
  309. // column 1
  310. if( !aSpec.spacer )
  311. {
  312. col = 1;
  313. cb = new wxCheckBox( m_RenderScrolledWindow, encodeId( col, aSpec.id ),
  314. aSpec.rowName, wxDefaultPosition, wxDefaultSize, wxALIGN_LEFT );
  315. shrinkFont( cb, m_PointSize );
  316. cb->SetValue( aSpec.state );
  317. cb->Enable( aSpec.changeable );
  318. cb->Bind( wxEVT_COMMAND_CHECKBOX_CLICKED, &LAYER_WIDGET::OnRenderCheckBox, this );
  319. cb->SetToolTip( aSpec.tooltip );
  320. }
  321. // column 0
  322. col = 0;
  323. if( aSpec.color != COLOR4D::UNSPECIFIED )
  324. {
  325. auto bmb = new COLOR_SWATCH( m_RenderScrolledWindow, aSpec.color, encodeId( col, aSpec.id ),
  326. getBackgroundLayerColor(), aSpec.defaultColor, SWATCH_SMALL );
  327. bmb->Bind( COLOR_SWATCH_CHANGED, &LAYER_WIDGET::OnRenderSwatchChanged, this );
  328. bmb->SetToolTip( _( "Left double click or middle click for color change" ) );
  329. m_RenderFlexGridSizer->wxSizer::Insert( index+col, bmb, 0, flags );
  330. bmb->Bind( wxEVT_RIGHT_DOWN, [this, bmb, renderName] ( wxMouseEvent& aEvt ) {
  331. OnRightDownRender( aEvt, bmb, renderName );
  332. } );
  333. cb->Bind( wxEVT_RIGHT_DOWN, [this, bmb, renderName] ( wxMouseEvent& aEvt ) {
  334. OnRightDownRender( aEvt, bmb, renderName );
  335. } );
  336. // could add a left click handler on the color button that toggles checkbox.
  337. }
  338. else // == -1, no color selection wanted
  339. {
  340. // need a place holder within the sizer to keep grid full.
  341. wxPanel* invisible = new wxPanel( m_RenderScrolledWindow, encodeId( col, aSpec.id ) );
  342. m_RenderFlexGridSizer->wxSizer::Insert( index+col, invisible, 0, flags );
  343. }
  344. // Items have to be inserted in order
  345. col = 1;
  346. if( aSpec.spacer )
  347. {
  348. wxPanel* invisible = new wxPanel( m_RenderScrolledWindow, wxID_ANY );
  349. m_RenderFlexGridSizer->wxSizer::Insert( index+col, invisible, 0, flags );
  350. }
  351. else
  352. {
  353. m_RenderFlexGridSizer->wxSizer::Insert( index+col, cb, 0, flags );
  354. }
  355. }
  356. void LAYER_WIDGET::passOnFocus()
  357. {
  358. m_FocusOwner->SetFocus();
  359. }
  360. //-----<public>-------------------------------------------------------
  361. LAYER_WIDGET::LAYER_WIDGET( wxWindow* aParent, wxWindow* aFocusOwner, wxWindowID id,
  362. const wxPoint& pos, const wxSize& size, long style ) :
  363. wxPanel( aParent, id, pos, size, style )
  364. {
  365. int indicatorSize = ConvertDialogToPixels( wxSize( 6, 6 ) ).x;
  366. m_IconProvider = new ROW_ICON_PROVIDER( indicatorSize );
  367. int pointSize = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT ).GetPointSize();
  368. int screenHeight = wxSystemSettings::GetMetric( wxSYS_SCREEN_Y );
  369. if( screenHeight <= 900 && pointSize >= indicatorSize )
  370. pointSize = pointSize * 8 / 10;
  371. m_PointSize = pointSize;
  372. wxBoxSizer* mainSizer = new wxBoxSizer( wxVERTICAL );
  373. m_notebook = new wxAuiNotebook( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxAUI_NB_TOP );
  374. wxFont font = m_notebook->GetFont();
  375. // change the font size on the notebook's tabs to match aPointSize
  376. font.SetPointSize( pointSize );
  377. m_notebook->SetFont( font );
  378. m_notebook->SetNormalFont( font );
  379. m_notebook->SetSelectedFont( font );
  380. m_notebook->SetMeasuringFont( font );
  381. m_LayerPanel = new wxPanel( m_notebook, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL );
  382. wxBoxSizer* layerPanelSizer;
  383. layerPanelSizer = new wxBoxSizer( wxVERTICAL );
  384. m_LayerScrolledWindow = new wxScrolledWindow( m_LayerPanel, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxNO_BORDER );
  385. m_LayerScrolledWindow->SetScrollRate( 5, 5 );
  386. m_LayersFlexGridSizer = new wxFlexGridSizer( 0, LYR_COLUMN_COUNT, 0, 1 );
  387. m_LayersFlexGridSizer->SetFlexibleDirection( wxHORIZONTAL );
  388. m_LayersFlexGridSizer->SetNonFlexibleGrowMode( wxFLEX_GROWMODE_NONE );
  389. m_LayerScrolledWindow->SetSizer( m_LayersFlexGridSizer );
  390. m_LayerScrolledWindow->Layout();
  391. m_LayersFlexGridSizer->Fit( m_LayerScrolledWindow );
  392. layerPanelSizer->Add( m_LayerScrolledWindow, 1, wxBOTTOM|wxEXPAND|wxLEFT|wxTOP, 2 );
  393. m_LayerPanel->SetSizer( layerPanelSizer );
  394. m_LayerPanel->Layout();
  395. layerPanelSizer->Fit( m_LayerPanel );
  396. m_notebook->AddPage( m_LayerPanel, _( "Layers" ), true );
  397. m_RenderingPanel = new wxPanel( m_notebook, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL );
  398. wxBoxSizer* renderPanelSizer;
  399. renderPanelSizer = new wxBoxSizer( wxVERTICAL );
  400. m_RenderScrolledWindow = new wxScrolledWindow( m_RenderingPanel, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxNO_BORDER );
  401. m_RenderScrolledWindow->SetScrollRate( 5, 5 );
  402. m_RenderFlexGridSizer = new wxFlexGridSizer( 0, RND_COLUMN_COUNT, 0, 1 );
  403. m_RenderFlexGridSizer->SetFlexibleDirection( wxHORIZONTAL );
  404. m_RenderFlexGridSizer->SetNonFlexibleGrowMode( wxFLEX_GROWMODE_NONE );
  405. m_RenderScrolledWindow->SetSizer( m_RenderFlexGridSizer );
  406. m_RenderScrolledWindow->Layout();
  407. m_RenderFlexGridSizer->Fit( m_RenderScrolledWindow );
  408. renderPanelSizer->Add( m_RenderScrolledWindow, 1, wxALL|wxEXPAND, 5 );
  409. m_RenderingPanel->SetSizer( renderPanelSizer );
  410. m_RenderingPanel->Layout();
  411. renderPanelSizer->Fit( m_RenderingPanel );
  412. m_notebook->AddPage( m_RenderingPanel, _( "Items" ), false );
  413. mainSizer->Add( m_notebook, 1, wxEXPAND, 5 );
  414. SetSizer( mainSizer );
  415. m_FocusOwner = aFocusOwner;
  416. m_CurrentRow = -1; // hide the arrow initially
  417. // trap the tab changes so that we can call passOnFocus().
  418. m_notebook->Bind( wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED, &LAYER_WIDGET::OnTabChange, this );
  419. Layout();
  420. }
  421. LAYER_WIDGET::~LAYER_WIDGET()
  422. {
  423. delete m_IconProvider;
  424. }
  425. wxSize LAYER_WIDGET::GetBestSize() const
  426. {
  427. // size of m_LayerScrolledWindow --------------
  428. wxArrayInt widths = m_LayersFlexGridSizer->GetColWidths();
  429. int totWidth = 0;
  430. if( widths.GetCount() )
  431. {
  432. for( int i = 0; i < LYR_COLUMN_COUNT; ++i )
  433. {
  434. totWidth += widths[i] + m_LayersFlexGridSizer->GetHGap();
  435. }
  436. }
  437. // Account for the parent's frame:
  438. totWidth += 15;
  439. /* The minimum height is a small size to properly force computation
  440. * of the panel's scrollbars (otherwise it will assume it *has* all
  441. * this space) */
  442. unsigned totHeight = 32;
  443. wxSize layerz( totWidth, totHeight );
  444. layerz += m_LayerPanel->GetWindowBorderSize();
  445. // size of m_RenderScrolledWindow --------------
  446. widths = m_RenderFlexGridSizer->GetColWidths();
  447. totWidth = 0;
  448. if( widths.GetCount() )
  449. {
  450. for( int i = 0; i < RND_COLUMN_COUNT; ++i )
  451. {
  452. totWidth += widths[i] + m_RenderFlexGridSizer->GetHGap();
  453. }
  454. }
  455. // account for the parent's frame, this one has void space of 10 PLUS a border:
  456. totWidth += 15;
  457. // For totHeight re-use the previous small one
  458. wxSize renderz( totWidth, totHeight );
  459. renderz += m_RenderingPanel->GetWindowBorderSize();
  460. wxSize clientz( std::max(renderz.x,layerz.x), std::max(renderz.y,layerz.y) );
  461. return clientz;
  462. }
  463. int LAYER_WIDGET::GetLayerRowCount() const
  464. {
  465. int controlCount = m_LayersFlexGridSizer->GetChildren().GetCount();
  466. return controlCount / LYR_COLUMN_COUNT;
  467. }
  468. int LAYER_WIDGET::GetRenderRowCount() const
  469. {
  470. int controlCount = m_RenderFlexGridSizer->GetChildren().GetCount();
  471. return controlCount / RND_COLUMN_COUNT;
  472. }
  473. void LAYER_WIDGET::AppendLayerRow( const ROW& aRow )
  474. {
  475. int nextRow = GetLayerRowCount();
  476. insertLayerRow( nextRow, aRow );
  477. }
  478. void LAYER_WIDGET::ClearLayerRows()
  479. {
  480. m_LayersFlexGridSizer->Clear( true );
  481. }
  482. void LAYER_WIDGET::AppendRenderRow( const ROW& aRow )
  483. {
  484. int nextRow = GetRenderRowCount();
  485. insertRenderRow( nextRow, aRow );
  486. }
  487. void LAYER_WIDGET::ClearRenderRows()
  488. {
  489. m_RenderFlexGridSizer->Clear( true );
  490. }
  491. void LAYER_WIDGET::SelectLayerRow( int aRow )
  492. {
  493. // enable the layer tab at index 0
  494. m_notebook->SetSelection( 0 );
  495. INDICATOR_ICON* oldIndicator = (INDICATOR_ICON*) getLayerComp( m_CurrentRow, 0 );
  496. if( oldIndicator )
  497. {
  498. if( useAlternateBitmap( m_CurrentRow ) )
  499. oldIndicator->SetIndicatorState( ROW_ICON_PROVIDER::STATE::DIMMED );
  500. else
  501. oldIndicator->SetIndicatorState( ROW_ICON_PROVIDER::STATE::OFF );
  502. }
  503. INDICATOR_ICON* newIndicator = (INDICATOR_ICON*) getLayerComp( aRow, 0 );
  504. if( newIndicator )
  505. {
  506. newIndicator->SetIndicatorState( ROW_ICON_PROVIDER::STATE::ON );
  507. // Make sure the desired layer row is visible.
  508. // It seems that as of 2.8.2, setting the focus does this.
  509. // I don't expect the scrolling to be needed at all because
  510. // the minimum window size may end up being established so that the
  511. // scroll bars will not be visible.
  512. getLayerComp( aRow, 1 )->SetFocus();
  513. }
  514. m_CurrentRow = aRow;
  515. // give the focus back to the app.
  516. passOnFocus();
  517. }
  518. void LAYER_WIDGET::SelectLayer( LAYER_NUM aLayer )
  519. {
  520. int row = findLayerRow( aLayer );
  521. SelectLayerRow( row );
  522. }
  523. LAYER_NUM LAYER_WIDGET::GetSelectedLayer()
  524. {
  525. wxWindow* w = getLayerComp( m_CurrentRow, 0 );
  526. if( w )
  527. return getDecodedId( w->GetId() );
  528. return UNDEFINED_LAYER;
  529. }
  530. void LAYER_WIDGET::SetLayerVisible( LAYER_NUM aLayer, bool isVisible )
  531. {
  532. setLayerCheckbox( aLayer, isVisible );
  533. OnLayerVisible( aLayer, isVisible );
  534. }
  535. void LAYER_WIDGET::setLayerCheckbox( LAYER_NUM aLayer, bool isVisible )
  536. {
  537. int row = findLayerRow( aLayer );
  538. if( row >= 0 )
  539. {
  540. wxCheckBox* cb = (wxCheckBox*) getLayerComp( row, COLUMN_COLOR_LYR_CB );
  541. wxASSERT( cb );
  542. cb->SetValue( isVisible ); // does not fire an event
  543. }
  544. }
  545. bool LAYER_WIDGET::IsLayerVisible( LAYER_NUM aLayer )
  546. {
  547. int row = findLayerRow( aLayer );
  548. if( row >= 0 )
  549. {
  550. wxCheckBox* cb = (wxCheckBox*) getLayerComp( row, COLUMN_COLOR_LYR_CB );
  551. wxASSERT( cb );
  552. return cb->GetValue();
  553. }
  554. return false;
  555. }
  556. void LAYER_WIDGET::SetLayerColor( LAYER_NUM aLayer, COLOR4D aColor )
  557. {
  558. int row = findLayerRow( aLayer );
  559. if( row >= 0 )
  560. {
  561. int col = 1; // bitmap button is column 1
  562. auto swatch = static_cast<COLOR_SWATCH*>( getLayerComp( row, col ) );
  563. wxASSERT( swatch );
  564. swatch->SetSwatchColor( aColor, false );
  565. }
  566. }
  567. COLOR4D LAYER_WIDGET::GetLayerColor( LAYER_NUM aLayer ) const
  568. {
  569. int row = findLayerRow( aLayer );
  570. if( row >= 0 )
  571. {
  572. int col = 1; // bitmap button is column 1
  573. auto swatch = static_cast<COLOR_SWATCH*>( getLayerComp( row, col ) );
  574. wxASSERT( swatch );
  575. return swatch->GetSwatchColor();
  576. }
  577. return COLOR4D::UNSPECIFIED; // it's caller fault, gave me a bad layer
  578. }
  579. void LAYER_WIDGET::SetRenderState( int aId, bool isSet )
  580. {
  581. int row = findRenderRow( aId );
  582. if( row >= 0 )
  583. {
  584. int col = 1; // checkbox is column 1
  585. wxCheckBox* cb = (wxCheckBox*) getRenderComp( row, col );
  586. wxASSERT( cb );
  587. cb->SetValue( isSet ); // does not fire an event
  588. }
  589. }
  590. bool LAYER_WIDGET::GetRenderState( int aId )
  591. {
  592. int row = findRenderRow( aId );
  593. if( row >= 0 )
  594. {
  595. int col = 1; // checkbox is column 1
  596. wxCheckBox* cb = (wxCheckBox*) getRenderComp( row, col );
  597. wxASSERT( cb );
  598. return cb->GetValue();
  599. }
  600. return false; // the value of a non-existent row
  601. }
  602. void LAYER_WIDGET::UpdateLayouts()
  603. {
  604. m_LayersFlexGridSizer->Layout();
  605. m_RenderFlexGridSizer->Layout();
  606. m_LayerPanel->Layout();
  607. m_RenderingPanel->Layout();
  608. FitInside();
  609. }
  610. void LAYER_WIDGET::UpdateLayerIcons()
  611. {
  612. int rowCount = GetLayerRowCount();
  613. for( int row = 0; row < rowCount ; row++ )
  614. {
  615. INDICATOR_ICON* indicator = (INDICATOR_ICON*) getLayerComp( row, COLUMN_ICON_ACTIVE );
  616. if( indicator )
  617. {
  618. ROW_ICON_PROVIDER::STATE state;
  619. if( row == m_CurrentRow )
  620. state = ROW_ICON_PROVIDER::STATE::ON;
  621. else if( useAlternateBitmap( row ) )
  622. state = ROW_ICON_PROVIDER::STATE::DIMMED;
  623. else
  624. state = ROW_ICON_PROVIDER::STATE::OFF;
  625. indicator->SetIndicatorState( state );
  626. }
  627. }
  628. }
  629. #if defined(STAND_ALONE)
  630. #include <wx/aui/aui.h>
  631. /**
  632. * MYFRAME
  633. * is a test class here to exercise the LAYER_WIDGET and explore use cases.
  634. * @see http://www.kirix.com/labs/wxaui/screenshots.html
  635. * for ideas.
  636. */
  637. class MYFRAME : public wxFrame
  638. {
  639. // example of how to derive from LAYER_WIDGET in order to provide the
  640. // abstract methods.
  641. class MYLAYERS : public LAYER_WIDGET
  642. {
  643. public:
  644. // your constructor could take a BOARD argument. here I leave it
  645. // out because this source module wants to know nothing of BOARDs
  646. // to maximize re-use.
  647. MYLAYERS( wxWindow* aParent ) :
  648. LAYER_WIDGET( aParent, aParent )
  649. {
  650. }
  651. void OnLayerColorChange( int aLayer, COLOR4D aColor )
  652. {
  653. /* a test trigger only
  654. if( aLayer == 2 )
  655. {
  656. ClearLayerRows();
  657. }
  658. */
  659. }
  660. bool OnLayerSelect( LAYER_NUM aLayer )
  661. {
  662. return true;
  663. }
  664. void OnLayerVisible( LAYER_NUM aLayer, bool isVisible, bool isFinal )
  665. {
  666. }
  667. void OnRenderColorChange( int aId, COLOR4D aColor )
  668. {
  669. }
  670. void OnRenderEnable( int aId, bool isEnabled )
  671. {
  672. }
  673. };
  674. public:
  675. MYFRAME( wxWindow * parent ) :
  676. wxFrame( parent, -1, wxT( "wxAUI Test" ), wxDefaultPosition,
  677. wxSize( 800, 600 ), wxDEFAULT_FRAME_STYLE )
  678. {
  679. // notify wxAUI which frame to use
  680. m_mgr.SetManagedWindow( this );
  681. MYLAYERS* lw = new MYLAYERS( this );
  682. // add some layer rows
  683. static const LAYER_WIDGET::ROW layerRows[] = {
  684. LAYER_WIDGET::ROW( wxT("layer 1"), 0, RED, wxT("RED"), false ),
  685. LAYER_WIDGET::ROW( wxT("layer 2"), 1, GREEN, wxT("GREEN"), true ),
  686. LAYER_WIDGET::ROW( wxT("brown_layer"), 2, BROWN, wxT("BROWN"), true ),
  687. LAYER_WIDGET::ROW( wxT("layer_4_you"), 3, BLUE, wxT("BLUE"), false ),
  688. };
  689. lw->AppendLayerRows( layerRows, arrayDim(layerRows) );
  690. // add some render rows
  691. static const LAYER_WIDGET::ROW renderRows[] = {
  692. LAYER_WIDGET::ROW( wxT("With Very Large Ears"), 0, COLOR4D::UNSPECIFIED, wxT("Spock here") ),
  693. LAYER_WIDGET::ROW( wxT("With Legs"), 1, YELLOW ),
  694. LAYER_WIDGET::ROW( wxT("With Oval Eyes"), 1, BROWN, wxT("My eyes are upon you") ),
  695. };
  696. lw->AppendRenderRows( renderRows, arrayDim(renderRows) );
  697. lw->SelectLayerRow( 1 );
  698. wxAuiPaneInfo li;
  699. li.MinSize( lw->GetBestSize() );
  700. li.BestSize( lw->GetBestSize() );
  701. li.Left();
  702. // li.MaximizeButton( true );
  703. // li.MinimizeButton( true );
  704. li.CloseButton( false );
  705. li.Caption( wxT( "Layers" ) );
  706. m_mgr.AddPane( lw, li );
  707. wxTextCtrl* text2 = new wxTextCtrl( this, -1, wxT( "Pane 2 - sample text" ),
  708. wxDefaultPosition, wxSize( 200, 150 ),
  709. wxNO_BORDER | wxTE_MULTILINE );
  710. m_mgr.AddPane( text2, wxBOTTOM, wxT( "Pane Number Two" ) );
  711. wxTextCtrl* text3 = new wxTextCtrl( this, -1, wxT( "Main content window" ),
  712. wxDefaultPosition, wxSize( 200, 150 ),
  713. wxNO_BORDER | wxTE_MULTILINE );
  714. m_mgr.AddPane( text3, wxCENTER );
  715. // tell the manager to "commit" all the changes just made
  716. m_mgr.Update();
  717. }
  718. ~MYFRAME()
  719. {
  720. // deinitialize the frame manager
  721. m_mgr.UnInit();
  722. }
  723. private:
  724. wxAuiManager m_mgr;
  725. };
  726. // our normal wxApp-derived class, as usual
  727. class MyApp : public wxApp
  728. {
  729. public:
  730. bool OnInit()
  731. {
  732. wxFrame* frame = new MYFRAME( NULL );
  733. SetTopWindow( frame );
  734. frame->Show();
  735. return true;
  736. }
  737. };
  738. DECLARE_APP( MyApp );
  739. IMPLEMENT_APP( MyApp );
  740. #endif // STAND_ALONE