Browse Source
Add a new BITMAP_BUTTON widget and use it in the appearances panel
Add a new BITMAP_BUTTON widget and use it in the appearances panel
This button behaves like an AUI button, so it is more suited to being on panels without a border.pull/16/head
7 changed files with 352 additions and 46 deletions
-
3common/CMakeLists.txt
-
200common/widgets/bitmap_button.cpp
-
121include/widgets/bitmap_button.h
-
8pcbnew/widgets/appearance_controls.cpp
-
7pcbnew/widgets/appearance_controls_base.cpp
-
52pcbnew/widgets/appearance_controls_base.fbp
-
7pcbnew/widgets/appearance_controls_base.h
@ -0,0 +1,200 @@ |
|||
/*
|
|||
* This program source code file is part of KICAD, a free EDA CAD application. |
|||
* |
|||
* Copyright (C) 2020 Ian McInerney <ian.s.mcinerney at ieee dot org> |
|||
* Copyright (C) 2020 Kicad Developers, see AUTHORS.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 |
|||
*/ |
|||
|
|||
#include <kiplatform/ui.h>
|
|||
#include <widgets/bitmap_button.h>
|
|||
#include <wx/button.h>
|
|||
#include <wx/dcclient.h>
|
|||
#include <wx/renderer.h>
|
|||
#include <wx/settings.h>
|
|||
|
|||
|
|||
BITMAP_BUTTON::BITMAP_BUTTON( wxWindow* aParent, wxWindowID aId, const wxPoint& aPos, |
|||
const wxSize& aSize, int aStyles ) : |
|||
wxPanel( aParent, aId, aPos, aSize, aStyles ), |
|||
m_buttonState( 0 ), |
|||
m_padding( 0 ) |
|||
{ |
|||
if( aSize == wxDefaultSize ) |
|||
SetMinSize( wxButton::GetDefaultSize() ); |
|||
|
|||
Bind( wxEVT_PAINT, &BITMAP_BUTTON::OnPaint, this ); |
|||
Bind( wxEVT_LEFT_UP, &BITMAP_BUTTON::OnLeftButtonUp, this ); |
|||
Bind( wxEVT_LEFT_DOWN, &BITMAP_BUTTON::OnLeftButtonDown, this ); |
|||
Bind( wxEVT_LEAVE_WINDOW, &BITMAP_BUTTON::OnMouseLeave, this ); |
|||
Bind( wxEVT_ENTER_WINDOW, &BITMAP_BUTTON::OnMouseEnter, this ); |
|||
} |
|||
|
|||
|
|||
BITMAP_BUTTON::~BITMAP_BUTTON() |
|||
{ |
|||
} |
|||
|
|||
|
|||
void BITMAP_BUTTON::SetPadding( int aPadding ) |
|||
{ |
|||
m_padding = aPadding; |
|||
SetMinSize( m_unadjustedMinSize + wxSize( aPadding * 2, aPadding * 2) ); |
|||
} |
|||
|
|||
|
|||
void BITMAP_BUTTON::SetBitmap( const wxBitmap& aBmp ) |
|||
{ |
|||
m_normalBitmap = aBmp; |
|||
m_unadjustedMinSize = aBmp.GetSize(); |
|||
|
|||
SetMinSize( wxSize( aBmp.GetWidth() + ( m_padding * 2 ), aBmp.GetHeight() + ( m_padding * 2 ) ) ); |
|||
} |
|||
|
|||
|
|||
void BITMAP_BUTTON::SetDisabledBitmap( const wxBitmap& aBmp ) |
|||
{ |
|||
m_disabledBitmap = aBmp; |
|||
} |
|||
|
|||
|
|||
void BITMAP_BUTTON::OnMouseLeave( wxMouseEvent& aEvent ) |
|||
{ |
|||
clearFlag( wxCONTROL_CURRENT ); |
|||
Refresh(); |
|||
|
|||
aEvent.Skip(); |
|||
} |
|||
|
|||
|
|||
void BITMAP_BUTTON::OnMouseEnter( wxMouseEvent& aEvent ) |
|||
{ |
|||
setFlag( wxCONTROL_CURRENT ); |
|||
Refresh(); |
|||
|
|||
aEvent.Skip(); |
|||
} |
|||
|
|||
|
|||
void BITMAP_BUTTON::OnLeftButtonUp( wxMouseEvent& aEvent ) |
|||
{ |
|||
// Only create a button event when the control is enabled
|
|||
if( !hasFlag( wxCONTROL_DISABLED ) ) |
|||
{ |
|||
wxEvtHandler* pEventHandler = GetEventHandler(); |
|||
wxASSERT( pEventHandler ); |
|||
|
|||
pEventHandler->CallAfter( |
|||
[=]() |
|||
{ |
|||
wxCommandEvent evt( wxEVT_BUTTON, this->GetId() ); |
|||
evt.SetEventObject( this ); |
|||
GetEventHandler()->ProcessEvent( evt ); |
|||
} ); |
|||
} |
|||
|
|||
clearFlag( wxCONTROL_PRESSED ); |
|||
Refresh(); |
|||
|
|||
aEvent.Skip(); |
|||
} |
|||
|
|||
|
|||
void BITMAP_BUTTON::OnLeftButtonDown( wxMouseEvent& aEvent ) |
|||
{ |
|||
setFlag( wxCONTROL_PRESSED ); |
|||
Refresh(); |
|||
|
|||
aEvent.Skip(); |
|||
} |
|||
|
|||
|
|||
void BITMAP_BUTTON::OnPaint( wxPaintEvent& aEvent ) |
|||
{ |
|||
bool darkMode = KIPLATFORM::UI::IsDarkTheme(); |
|||
wxColor highlightColor = wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ); |
|||
|
|||
// The drawing rectangle
|
|||
wxRect rect( wxPoint( 0, 0 ), GetSize() ); |
|||
wxPaintDC dc( this ); |
|||
|
|||
// This drawing is done so the button looks the same as an AUI toolbar button
|
|||
if( !hasFlag( wxCONTROL_DISABLED ) ) |
|||
{ |
|||
if( hasFlag( wxCONTROL_PRESSED ) ) |
|||
{ |
|||
dc.SetPen( wxPen( highlightColor ) ); |
|||
dc.SetBrush( wxBrush( highlightColor.ChangeLightness( darkMode ? 20 : 150 ) ) ); |
|||
dc.DrawRectangle( rect ); |
|||
} |
|||
else if( hasFlag( wxCONTROL_CURRENT ) ) |
|||
{ |
|||
dc.SetPen( wxPen( highlightColor ) ); |
|||
dc.SetBrush( wxBrush( highlightColor.ChangeLightness( darkMode ? 40 : 170 ) ) ); |
|||
|
|||
// Checked items need a lighter hover rectangle
|
|||
if( hasFlag( wxCONTROL_CHECKED ) ) |
|||
dc.SetBrush( wxBrush( highlightColor.ChangeLightness( darkMode ? 50 : 180 ) ) ); |
|||
|
|||
dc.DrawRectangle( rect ); |
|||
} |
|||
else if( hasFlag( wxCONTROL_CHECKED ) ) |
|||
{ |
|||
dc.SetPen( wxPen( highlightColor ) ); |
|||
dc.SetBrush( wxBrush( highlightColor.ChangeLightness( darkMode ? 40 : 170 ) ) ); |
|||
dc.DrawRectangle( rect ); |
|||
} |
|||
} |
|||
|
|||
const wxBitmap& bmp = hasFlag( wxCONTROL_DISABLED ) ? m_disabledBitmap : m_normalBitmap; |
|||
|
|||
// Draw the bitmap with the upper-left corner offset by the padding
|
|||
if( bmp.IsOk() ) |
|||
dc.DrawBitmap( bmp, m_padding, m_padding, true ); |
|||
} |
|||
|
|||
|
|||
bool BITMAP_BUTTON::Enable( bool aEnable ) |
|||
{ |
|||
// If the requested state is already the current state, don't do anything
|
|||
if( aEnable != hasFlag( wxCONTROL_DISABLED ) ) |
|||
return false; |
|||
|
|||
wxPanel::Enable( aEnable ); |
|||
|
|||
if( aEnable ) |
|||
clearFlag( wxCONTROL_DISABLED ); |
|||
else |
|||
setFlag( wxCONTROL_DISABLED ); |
|||
|
|||
Refresh(); |
|||
|
|||
return true; |
|||
} |
|||
|
|||
|
|||
void BITMAP_BUTTON::Check( bool aCheck ) |
|||
{ |
|||
if( aCheck ) |
|||
clearFlag( wxCONTROL_CHECKED ); |
|||
else |
|||
setFlag( wxCONTROL_CHECKED ); |
|||
|
|||
Refresh(); |
|||
} |
@ -0,0 +1,121 @@ |
|||
/* |
|||
* This program source code file is part of KICAD, a free EDA CAD application. |
|||
* |
|||
* Copyright (C) 2020 Ian McInerney <ian.s.mcinerney at ieee dot org> |
|||
* Copyright (C) 2020 Kicad Developers, see AUTHORS.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 |
|||
*/ |
|||
|
|||
#ifndef BITMAP_BUTTON_H_ |
|||
#define BITMAP_BUTTON_H_ |
|||
|
|||
#include <wx/bitmap.h> |
|||
#include <wx/panel.h> |
|||
|
|||
|
|||
/** |
|||
* A bitmap button widget that behaves like an AUI toolbar item's button when it is drawn. |
|||
* Specifically: |
|||
* * It has no border |
|||
* * It has a rectangle highlight when the mouse is hovering/pressed |
|||
* * It has the ability to be checked/toggled |
|||
*/ |
|||
class BITMAP_BUTTON : public wxPanel |
|||
{ |
|||
public: |
|||
BITMAP_BUTTON( wxWindow* aParent, wxWindowID aId, const wxPoint& aPos = wxDefaultPosition, |
|||
const wxSize& aSize = wxDefaultSize, int aStyles = wxBORDER_NONE | wxTAB_TRAVERSAL ); |
|||
|
|||
~BITMAP_BUTTON(); |
|||
|
|||
/** |
|||
* Set the amount of padding present on each side of the bitmap. |
|||
* |
|||
* @param aPadding is the amount in px of padding for each side |
|||
*/ |
|||
void SetPadding( int aPadding ); |
|||
|
|||
/** |
|||
* Set the bitmap shown when the button is enabled. |
|||
* |
|||
* @param aBmp is the enabled bitmap |
|||
*/ |
|||
void SetBitmap( const wxBitmap& aBmp ); |
|||
|
|||
/** |
|||
* Set the bitmap shown when the button is disabled. |
|||
* |
|||
* @param aBmp is the disabled bitmap |
|||
*/ |
|||
void SetDisabledBitmap( const wxBitmap& aBmp ); |
|||
|
|||
/** |
|||
* Enable the button. |
|||
* |
|||
* @param aEnable is true to enable, false to disable |
|||
*/ |
|||
bool Enable( bool aEnable = true ) override; |
|||
|
|||
/** |
|||
* Check the control. This is the equivalent to toggling a toolbar button. |
|||
* |
|||
* @param aCheck is true to check, false to uncheck |
|||
*/ |
|||
void Check( bool aCheck = true ); |
|||
|
|||
protected: |
|||
void OnMouseLeave( wxMouseEvent& aEvent ); |
|||
void OnMouseEnter( wxMouseEvent& aEvent ); |
|||
void OnLeftButtonUp( wxMouseEvent& aEvent ); |
|||
void OnLeftButtonDown( wxMouseEvent& aEvent ); |
|||
void OnPaint( wxPaintEvent& aEvent ); |
|||
|
|||
void setFlag( int aFlag) |
|||
{ |
|||
m_buttonState |= aFlag; |
|||
} |
|||
|
|||
void clearFlag( int aFlag ) |
|||
{ |
|||
m_buttonState &= ~aFlag; |
|||
} |
|||
|
|||
bool hasFlag( int aFlag ) |
|||
{ |
|||
return m_buttonState & aFlag; |
|||
} |
|||
|
|||
private: |
|||
///> Bitmap shown when button is enabled |
|||
wxBitmap m_normalBitmap; |
|||
|
|||
///> Bitmap shown when button is disabled |
|||
wxBitmap m_disabledBitmap; |
|||
|
|||
///> Current state of the button |
|||
int m_buttonState; |
|||
|
|||
///> Padding on each side of the bitmap |
|||
int m_padding; |
|||
|
|||
///> Size without the padding |
|||
wxSize m_unadjustedMinSize; |
|||
}; |
|||
|
|||
#endif /*BITMAP_BUTTON_H_*/ |
Write
Preview
Loading…
Cancel
Save
Reference in new issue