kicad/pcbnew/layer_widget.cpp

492 lines
14 KiB
C++
Raw Normal View History

2010-01-07 02:18:25 +00:00
/*
* This program source code file is part of KICAD, a free EDA CAD application.
*
* Copyright (C) 2010 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
* Copyright (C) 2010 Kicad Developers, see change_log.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
/* This source module implements the layer visibility and selection widget
*/
#define STAND_ALONE 1 // define to enable test program for LAYER_WIDGET
// also enable KICAD_AUIMANAGER and KICAD_AUITOOLBAR in ccmake to
// build this test program
#include <wx/wx.h>
2010-01-07 16:33:41 +00:00
#include <wx/statbmp.h>
2010-01-07 02:18:25 +00:00
#include <wx/aui/aui.h>
2010-01-09 15:51:09 +00:00
//#include "fctsys.h"
#include "common.h"
#include "layer_panel_base.h"
#include "colors.h"
/* no external data knowledge needed or wanted
#include "pcbnew.h"
#include "wxPcbStruct.h"
*/
2010-01-07 16:33:41 +00:00
/* XPM */
static const char * clear_xpm[] = {
2010-01-08 01:17:59 +00:00
"28 14 1 1",
2010-01-07 16:33:41 +00:00
" c None",
2010-01-08 01:17:59 +00:00
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" "};
2010-01-07 16:33:41 +00:00
/* XPM */
static const char * rightarrow_xpm[] = {
2010-01-08 01:17:59 +00:00
"28 14 5 1",
2010-01-07 16:33:41 +00:00
" c None",
". c white",
"X c #8080ff",
"o c BLUE",
"O c gray56",
2010-01-08 01:17:59 +00:00
" . .. ",
" .XX ",
" .XXX ",
" .XXXX ",
" ................XXXXX ",
" XXXXXXXXXXXXXXXXXXXXXX ",
" XXXXXXXXXXXXXXXXXXXXXXX ",
" oooooooooooooooooooooooO",
" ooooooooooooooooooooooO ",
" OOOOOOOOOOOOOOOoooooO ",
" ooooO ",
" oooO ",
" ooO ",
" oO "};
2010-01-07 16:33:41 +00:00
2010-01-07 02:18:25 +00:00
/**
* Struct LAYER_SPEC
2010-01-09 15:51:09 +00:00
* provides all the data needed to add a layer row to a LAYER_WIDGET
2010-01-07 02:18:25 +00:00
*/
struct LAYER_SPEC
{
wxString layerName;
2010-01-08 01:17:59 +00:00
int layer;
int colorIndex;
2010-01-07 02:18:25 +00:00
2010-01-08 01:17:59 +00:00
LAYER_SPEC( const wxString& aLayerName, int aLayer, int aColorIndex = 0 )
2010-01-07 02:18:25 +00:00
{
layerName = aLayerName;
2010-01-08 01:17:59 +00:00
layer = aLayer;
2010-01-07 02:18:25 +00:00
colorIndex = aColorIndex;
}
};
/**
2010-01-09 15:51:09 +00:00
* Class LAYER_WIDGET
* is abstract and is derived from a wxFormBuilder maintained class called
* LAYER_PANEL_BASE. It is used to manage a list of layers, with the notion of
* a "current" layer, and layer specific visibility control. You must derive from
* it to use it so you can implement the abstract functions which recieve the
* events. Each layer is given its own color, and that color can be changed
* within the UI provided here. This widget knows nothing of the client code, meaning
* it has no knowledge of a BOARD or anything. To use it you must derive from
* this class and implement the abstract functions:
* <p>
* void ColorChange( int aLayer, int aColor );
* <p>
* bool LayerChange( int aLayer );
2010-01-07 02:18:25 +00:00
*/
2010-01-09 15:51:09 +00:00
class LAYER_WIDGET : public LAYER_PANEL_BASE
2010-01-07 02:18:25 +00:00
{
2010-01-09 15:51:09 +00:00
protected:
2010-01-07 16:33:41 +00:00
wxBitmap* m_BlankBitmap;
wxBitmap* m_RightArrowBitmap;
wxSize m_BitmapSize;
2010-01-08 01:17:59 +00:00
wxStaticBitmap* m_Bitmaps[64];
2010-01-09 15:51:09 +00:00
int m_CurrentRow; ///< visual row of layer list
2010-01-07 16:33:41 +00:00
#define LAYER_COLUMN_COUNT 4
2010-01-07 02:18:25 +00:00
/**
2010-01-09 15:51:09 +00:00
* Function makeColorButton
* creates a wxBitmapButton and assigns it a solid color and a control ID
2010-01-07 02:18:25 +00:00
*/
2010-01-08 01:17:59 +00:00
wxBitmapButton* makeColorButton( int aColorIndex, int aID )
2010-01-07 02:18:25 +00:00
{
2010-01-07 16:33:41 +00:00
const int BUTT_SIZE_X = 32;
const int BUTT_SIZE_Y = 22;
2010-01-07 02:18:25 +00:00
// dynamically make a wxBitMap and brush it with the appropriate color,
// then create a wxBitmapButton from it.
wxBitmap bitmap( BUTT_SIZE_X, BUTT_SIZE_Y );
wxBrush brush;
wxMemoryDC iconDC;
iconDC.SelectObject( bitmap );
brush.SetColour( MakeColour( aColorIndex ) );
brush.SetStyle( wxSOLID );
iconDC.SetBrush( brush );
iconDC.DrawRectangle( 0, 0, BUTT_SIZE_X, BUTT_SIZE_Y );
2010-01-08 01:17:59 +00:00
wxBitmapButton* ret = new wxBitmapButton( m_LayerScrolledWindow, aID, bitmap,
2010-01-07 16:33:41 +00:00
wxDefaultPosition, wxSize(BUTT_SIZE_X, BUTT_SIZE_Y), wxBORDER_RAISED );
2010-01-09 15:51:09 +00:00
ret->Connect( wxEVT_LEFT_DOWN, wxMouseEventHandler( LAYER_WIDGET::OnLeftDownLayers ), NULL, this );
ret->Connect( wxEVT_RIGHT_DOWN, wxMouseEventHandler( LAYER_WIDGET::OnRightDownLayers ), NULL, this );
2010-01-08 01:17:59 +00:00
/* cannot get this event without also the wxEVT_LEFT_DOWN firing first
2010-01-09 15:51:09 +00:00
ret->Connect( wxEVT_LEFT_DCLICK, wxMouseEventHandler( LAYER_WIDGET::OnLeftDClickLayers ), NULL, this );
2010-01-08 01:17:59 +00:00
*/
2010-01-07 16:33:41 +00:00
return ret;
2010-01-07 02:18:25 +00:00
}
2010-01-08 01:17:59 +00:00
2010-01-07 02:18:25 +00:00
void OnLeftDownLayers( wxMouseEvent& event )
{
2010-01-09 15:51:09 +00:00
int row;
2010-01-08 01:17:59 +00:00
wxObject* eventSource = event.GetEventObject();
// if mouse event is coming from the m_LayerScrolledWindow and not one
// of its children, we have to find the row manually based on y coord.
if( eventSource == (wxObject*) m_LayerScrolledWindow )
{
int y = event.GetY();
wxArrayInt heights = m_LayersFlexGridSizer->GetRowHeights();
int height = 0;
int rowCount = GetLayerRowCount();
for( row = 0; row<rowCount; ++row )
{
if( y < height + heights[row] )
break;
height += heights[row];
}
if( row >= rowCount )
row = rowCount - 1;
}
// all nested controls on a given row will have the layer index as their ID
else
{
int layer = ((wxWindow*)eventSource)->GetId();
2010-01-09 15:51:09 +00:00
row = findLayerRow( layer );
}
2010-01-08 01:17:59 +00:00
2010-01-09 15:51:09 +00:00
if( LayerChange( row ) ) // if owner allows this change.
2010-01-08 01:17:59 +00:00
SelectLayerRow( row );
2010-01-07 02:18:25 +00:00
}
2010-01-08 01:17:59 +00:00
2010-01-07 02:18:25 +00:00
void OnRightDownLayers( wxMouseEvent& event )
{
printf("OnRightDownLayers\n");
}
2010-01-08 01:17:59 +00:00
2010-01-07 16:33:41 +00:00
/**
* Function getLayerComp
* returns the component within the m_LayersFlexGridSizer at aSizerNdx.
2010-01-08 01:17:59 +00:00
*
* @param aSizerNdx is the 0 based index into all the wxWindows which have
* been added to the m_LayersFlexGridSizer.
2010-01-07 16:33:41 +00:00
*/
wxWindow* getLayerComp( int aSizerNdx )
{
return m_LayersFlexGridSizer->GetChildren()[aSizerNdx]->GetWindow();
}
2010-01-07 02:18:25 +00:00
2010-01-08 01:17:59 +00:00
/**
* Function findLayerRow
* returns the row index that \a aLayer resides in, or -1 if not found.
*/
int findLayerRow( int aLayer )
{
int count = GetLayerRowCount();
for( int row=0; row<count; ++row )
{
// column 0 in the layer scroll window has a wxStaticBitmap, get its ID.
wxStaticBitmap* bm = (wxStaticBitmap*) getLayerComp( row * LAYER_COLUMN_COUNT + 0 );
if( aLayer == bm->GetId() )
return row;
}
return -1;
}
2010-01-07 02:18:25 +00:00
2010-01-07 16:33:41 +00:00
/**
2010-01-09 15:51:09 +00:00
* Function insertLayerRow
* appends or inserts a new row in the layer portion of the widget.
2010-01-07 16:33:41 +00:00
*/
2010-01-09 15:51:09 +00:00
void insertLayerRow( int aRow, const LAYER_SPEC& aSpec )
2010-01-07 16:33:41 +00:00
{
2010-01-09 15:51:09 +00:00
wxASSERT( aRow >= 0 );
2010-01-07 16:33:41 +00:00
2010-01-09 15:51:09 +00:00
size_t index = aRow * LAYER_COLUMN_COUNT;
2010-01-07 16:33:41 +00:00
2010-01-09 15:51:09 +00:00
wxSizerFlags flags;
2010-01-08 01:17:59 +00:00
2010-01-09 15:51:09 +00:00
flags.Align(wxALIGN_CENTER_HORIZONTAL | wxALIGN_CENTER_VERTICAL);
2010-01-07 16:33:41 +00:00
2010-01-09 15:51:09 +00:00
// column 0
m_Bitmaps[aRow] = new wxStaticBitmap( m_LayerScrolledWindow, aSpec.layer, *m_BlankBitmap,
wxDefaultPosition, m_BitmapSize );
m_Bitmaps[aRow]->Connect( wxEVT_LEFT_DOWN, wxMouseEventHandler( LAYER_WIDGET::OnLeftDownLayers ), NULL, this );
m_LayersFlexGridSizer->Insert( index+0, m_Bitmaps[aRow], wxSizerFlags().Align( wxALIGN_CENTER_VERTICAL ) );
// column 1
wxBitmapButton* bmb = makeColorButton( aSpec.colorIndex, aSpec.layer );
bmb->SetToolTip( _("Right click to change layer color, left click to select layer" ) );
m_LayersFlexGridSizer->Insert( index+1, bmb, flags );
2010-01-07 16:33:41 +00:00
2010-01-09 15:51:09 +00:00
// column 2
wxStaticText* st = new wxStaticText( m_LayerScrolledWindow, aSpec.layer, aSpec.layerName );
st->Connect( wxEVT_LEFT_DOWN, wxMouseEventHandler( LAYER_WIDGET::OnLeftDownLayers ), NULL, this );
st->SetToolTip( _( "Click here to select this layer" ) );
m_LayersFlexGridSizer->Insert( index+2, st,
wxSizerFlags().Align( wxALIGN_CENTER_VERTICAL ) );
2010-01-08 01:17:59 +00:00
2010-01-09 15:51:09 +00:00
// column 3
wxCheckBox* cb = new wxCheckBox( m_LayerScrolledWindow, aSpec.layer, wxEmptyString );
cb->SetToolTip( _( "Enable this for visibility" ) );
m_LayersFlexGridSizer->Insert( index+3, cb, flags );
2010-01-07 16:33:41 +00:00
}
2010-01-09 15:51:09 +00:00
public:
2010-01-07 16:33:41 +00:00
2010-01-07 02:18:25 +00:00
/** Constructor */
2010-01-09 15:51:09 +00:00
LAYER_WIDGET( wxWindow* parent ) :
2010-01-07 02:18:25 +00:00
LAYER_PANEL_BASE( parent )
{
2010-01-07 16:33:41 +00:00
m_CurrentRow = 0;
2010-01-08 01:17:59 +00:00
memset( m_Bitmaps, 0, sizeof(m_Bitmaps) );
2010-01-07 16:33:41 +00:00
m_RightArrowBitmap = new wxBitmap( rightarrow_xpm );
m_BlankBitmap = new wxBitmap( clear_xpm ); // translucent
2010-01-07 02:18:25 +00:00
2010-01-07 16:33:41 +00:00
m_BitmapSize = wxSize(m_BlankBitmap->GetWidth(), m_BlankBitmap->GetHeight());
2010-01-09 15:51:09 +00:00
AppendLayerRow( LAYER_SPEC( wxT("layer 1"), 0, RED ) );
AppendLayerRow( LAYER_SPEC( wxT("layer 2"), 1, GREEN ) );
AppendLayerRow( LAYER_SPEC( wxT("brown_layer"), 2, BROWN ) );
AppendLayerRow( LAYER_SPEC( wxT("layer_4_you"), 3, BLUE ) );
2010-01-07 16:33:41 +00:00
SelectLayerRow( 1 );
2010-01-07 02:18:25 +00:00
}
2010-01-07 16:33:41 +00:00
2010-01-09 15:51:09 +00:00
/**
* Function GetLayerRowCount
* returns the number of rows in the layer tab.
*/
int GetLayerRowCount()
{
int controlCount = m_LayersFlexGridSizer->GetChildren().GetCount();
return controlCount / LAYER_COLUMN_COUNT;
}
/**
* Function AppendLayerRow
* appends a new row in the layer portion of the widget.
*/
void AppendLayerRow( const LAYER_SPEC& aSpec )
{
int nextRow = GetLayerRowCount();
insertLayerRow( nextRow, aSpec );
}
/**
* Function SelectLayerRow
* changes the row selection in the layer list to the given row.
*/
bool SelectLayerRow( int aRow )
{
if( (unsigned) aRow < (unsigned) GetLayerRowCount() )
{
int newNdx = LAYER_COLUMN_COUNT * aRow;
int oldNdx = LAYER_COLUMN_COUNT * m_CurrentRow;
wxStaticBitmap* oldbm = (wxStaticBitmap*) getLayerComp( oldNdx );
wxStaticBitmap* newbm = (wxStaticBitmap*) getLayerComp( newNdx );
oldbm->SetBitmap( *m_BlankBitmap );
newbm->SetBitmap( *m_RightArrowBitmap );
m_CurrentRow = aRow;
return true;
}
return false;
}
/**
* Function SelectLayer
* changes the row selection in the layer list to the given layer.
*/
bool SelectLayer( int aLayer )
{
int row = findLayerRow( aLayer );
return SelectLayerRow( row );
}
/**
* Function ColorChange
* is called whenever the user changes the color of a layer. Derived
* classes will handle this accordingly.
*/
virtual void ColorChange( int aLayer, int aColor ) = 0;
/**
* Function LayerChange
* is called whenever the user selects a different layer. Derived classes
* will handle this accordingly, and can deny the change by returning false.
*/
virtual bool LayerChange( int aLayer ) = 0;
2010-01-07 02:18:25 +00:00
};
/**
* class LAYER_WIDGET : public wxPanel
* {
* };
*/
#if defined(STAND_ALONE)
/**
* Class MYFRAME
* is a test class here to exercise the LAYER_WIDGET and explore use cases.
* @see http://www.kirix.com/labs/wxaui/screenshots.html
* for ideas.
*/
2010-01-09 15:51:09 +00:00
class MYFRAME : public wxFrame
{
class MYLAYERS : public LAYER_WIDGET
{
MYFRAME* frame;
public:
MYLAYERS( wxWindow* aParent, MYFRAME* aFrame ) :
LAYER_WIDGET( aParent ),
frame( aFrame )
{
}
void ColorChange( int aLayer, int aColor )
{
printf("ColorChange( aLayer:%d, aColor:%d )\n", aLayer, aColor );
}
bool LayerChange( int aLayer )
{
printf( "LayerChange( aLayer:%d )\n", aLayer );
return true;
}
};
2010-01-07 02:18:25 +00:00
public:
MYFRAME( wxWindow * parent ) : wxFrame( parent, -1, _( "wxAUI Test" ),
wxDefaultPosition, wxSize( 800, 600 ),
wxDEFAULT_FRAME_STYLE )
{
// notify wxAUI which frame to use
m_mgr.SetManagedWindow( this );
// create several text controls
2010-01-09 15:51:09 +00:00
wxPanel* layerWidget = new MYLAYERS( this, this );
2010-01-07 02:18:25 +00:00
wxTextCtrl* text2 = new wxTextCtrl( this, -1, _( "Pane 2 - sample text" ),
wxDefaultPosition, wxSize( 200, 150 ),
wxNO_BORDER | wxTE_MULTILINE );
wxTextCtrl* text3 = new wxTextCtrl( this, -1, _( "Main content window" ),
wxDefaultPosition, wxSize( 200, 150 ),
wxNO_BORDER | wxTE_MULTILINE );
// add the panes to the manager
m_mgr.AddPane( layerWidget, wxLEFT, wxT( "Layer Visibility" ) );
m_mgr.AddPane( text2, wxBOTTOM, wxT( "Pane Number Two" ) );
m_mgr.AddPane( text3, wxCENTER );
// tell the manager to "commit" all the changes just made
m_mgr.Update();
}
~MYFRAME()
{
// deinitialize the frame manager
m_mgr.UnInit();
}
private:
wxAuiManager m_mgr;
};
// our normal wxApp-derived class, as usual
class MyApp : public wxApp {
public:
bool OnInit()
{
wxFrame* frame = new MYFRAME( NULL );
SetTopWindow( frame );
frame->Show();
return true;
}
};
DECLARE_APP( MyApp );
IMPLEMENT_APP( MyApp );
#endif