1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
|
/*
* This program source code file is part of KICAD, a free EDA CAD application.
*
* Copyright (C) 2016 Anil8735(https://stackoverflow.com/users/3659387/anil8753)
* from https://stackoverflow.com/a/37274011
* Copyright The 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 <widgets/split_button.h>
#include <wx/button.h>
#include <wx/dcclient.h>
#include <wx/dcmemory.h>
#include <wx/menu.h>
#include <wx/renderer.h>
#include <wx/settings.h>
#include <wx/version.h>
#include <kiplatform/ui.h>
SPLIT_BUTTON::SPLIT_BUTTON( wxWindow* aParent, wxWindowID aId, const wxString& aLabel,
const wxPoint& aPos, const wxSize& aSize ) :
wxPanel( aParent, aId, aPos, aSize, wxBORDER_NONE | wxTAB_TRAVERSAL,
wxS( "DropDownButton" ) ),
m_label( aLabel )
{
m_arrowButtonWidth = FromDIP( 20 ); // just a fixed eyeballed constant width
m_widthPadding = FromDIP( 10 );
if( aSize == wxDefaultSize )
{
wxSize defaultSize = wxButton::GetDefaultSize( aParent );
wxSize textSize = GetTextExtent( m_label );
SetMinSize( wxSize( std::max( textSize.GetWidth(), defaultSize.GetWidth() + 1 ),
defaultSize.GetHeight() + 1 ) );
}
Bind( wxEVT_PAINT, &SPLIT_BUTTON::OnPaint, this );
Bind( wxEVT_LEFT_UP, &SPLIT_BUTTON::OnLeftButtonUp, this );
Bind( wxEVT_LEFT_DOWN, &SPLIT_BUTTON::OnLeftButtonDown, this );
Bind( wxEVT_KILL_FOCUS, &SPLIT_BUTTON::OnKillFocus, this );
Bind( wxEVT_LEAVE_WINDOW, &SPLIT_BUTTON::OnMouseLeave, this );
Bind( wxEVT_ENTER_WINDOW, &SPLIT_BUTTON::OnMouseEnter, this );
Bind( wxEVT_SYS_COLOUR_CHANGED, wxSysColourChangedEventHandler( SPLIT_BUTTON::onThemeChanged ),
this );
m_pMenu = new wxMenu();
}
SPLIT_BUTTON::~SPLIT_BUTTON()
{
delete m_pMenu;
m_pMenu = nullptr;
}
void SPLIT_BUTTON::onThemeChanged( wxSysColourChangedEvent &aEvent )
{
Refresh();
}
void SPLIT_BUTTON::SetMinSize( const wxSize& aSize )
{
m_unadjustedMinSize = aSize;
wxPanel::SetMinSize( wxSize( aSize.GetWidth() + m_arrowButtonWidth + m_widthPadding,
aSize.GetHeight() ) );
}
void SPLIT_BUTTON::SetWidthPadding( int aPadding )
{
m_widthPadding = aPadding;
SetMinSize( m_unadjustedMinSize );
}
void SPLIT_BUTTON::SetBitmap( const wxBitmapBundle& aBmp )
{
m_bitmap = aBmp;
#ifndef __WXMSW__
SetMinSize( m_bitmap.GetDefaultSize() );
#else
SetMinSize( m_bitmap.GetPreferredBitmapSizeFor( this ) );
#endif
}
void SPLIT_BUTTON::SetLabel( const wxString& aLabel )
{
if( m_label != aLabel )
{
m_label = aLabel;
Refresh();
}
}
wxMenu* SPLIT_BUTTON::GetSplitButtonMenu()
{
return m_pMenu;
}
void SPLIT_BUTTON::OnKillFocus( wxFocusEvent& aEvent )
{
if( m_stateButton != 0 || m_stateMenu != 0 )
{
m_stateButton = 0;
m_stateMenu = 0;
Refresh();
}
aEvent.Skip();
}
void SPLIT_BUTTON::OnMouseLeave( wxMouseEvent& aEvent )
{
if( m_stateButton != 0 || m_stateMenu != 0 )
{
m_stateButton = 0;
m_stateMenu = 0;
Refresh();
}
aEvent.Skip();
}
void SPLIT_BUTTON::OnMouseEnter( wxMouseEvent& aEvent )
{
if( m_stateButton != wxCONTROL_CURRENT || m_stateMenu != wxCONTROL_CURRENT )
{
m_stateButton = wxCONTROL_CURRENT;
m_stateMenu = wxCONTROL_CURRENT;
Refresh();
}
aEvent.Skip();
}
void SPLIT_BUTTON::OnLeftButtonUp( wxMouseEvent& aEvent )
{
m_stateButton = 0;
m_stateMenu = 0;
Refresh();
int x = -1;
int y = -1;
aEvent.GetPosition( &x, &y );
if( x < ( GetSize().GetWidth() - m_arrowButtonWidth ) )
{
wxEvtHandler* pEventHandler = GetEventHandler();
wxASSERT( pEventHandler );
pEventHandler->CallAfter(
[this]()
{
wxCommandEvent evt( wxEVT_BUTTON, GetId() );
evt.SetEventObject( this );
GetEventHandler()->ProcessEvent( evt );
} );
}
m_bLButtonDown = false;
aEvent.Skip();
}
void SPLIT_BUTTON::OnLeftButtonDown( wxMouseEvent& aEvent )
{
m_bLButtonDown = true;
int x = -1;
int y = -1;
aEvent.GetPosition( &x, &y );
if( x >= ( GetSize().GetWidth() - m_arrowButtonWidth ) )
{
m_stateButton = 0;
m_stateMenu = wxCONTROL_PRESSED;
Refresh();
wxSize size = GetSize();
wxPoint position;
position.x = 0;
position.y = size.GetHeight();
PopupMenu( m_pMenu, position );
m_stateMenu = 0;
Refresh();
}
else
{
m_stateButton = wxCONTROL_PRESSED;
m_stateMenu = wxCONTROL_PRESSED;
Refresh();
}
aEvent.Skip();
}
void SPLIT_BUTTON::OnPaint( wxPaintEvent& WXUNUSED( aEvent ) )
{
wxPaintDC dc( this );
wxSize size = GetSize();
const int width = size.GetWidth() - m_arrowButtonWidth;
#ifdef __WXMAC__
auto drawBackground =
[&]( wxRect aRect )
{
// wxWidgets doesn't have much support for dark mode on OSX; none of the
// system colours return the right values, nor does wxRendererNative draw
// the borders correctly. So we add some empirically chosen hacks here.
// NOTE: KEEP THESE HACKS IN SYNC WITH STD_BITMAP_BUTTON
wxColor fg = wxSystemSettings::GetColour( wxSYS_COLOUR_BTNTEXT );
wxColor bg = wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE );
aRect.width += 1;
aRect.height += 1;
if( KIPLATFORM::UI::IsDarkTheme() )
{
bg = bg.ChangeLightness( m_bIsEnable ? 130 : 120 );
dc.SetBrush( bg );
dc.SetPen( bg );
}
else
{
bg = bg.ChangeLightness( m_bIsEnable ? 200 : 160 );
dc.SetBrush( bg );
fg = fg.ChangeLightness( 180 );
dc.SetPen( fg );
}
dc.DrawRoundedRectangle( aRect, aRect.height / 4.0 );
};
#endif
// Draw first part of button
wxRect r1;
r1.x = 0;
r1.y = 0;
r1.width = width;
r1.height = size.GetHeight();
#ifdef __WXMAC__
// wxRendereNative doesn't handle dark mode on OSX.
drawBackground( r1 );
#else
#ifdef _WXMSW_
r1.width += 2;
#endif
wxRendererNative::Get().DrawPushButton( this, dc, r1, m_stateButton );
#endif
SetForegroundColour( m_bIsEnable ? wxSystemSettings::GetColour( wxSYS_COLOUR_BTNTEXT )
: wxSystemSettings::GetColour( wxSYS_COLOUR_GRAYTEXT ) );
if( m_bitmap.IsOk() )
{
#ifndef __WXMSW__
wxSize bmpSize = m_bitmap.GetDefaultSize();
#else
wxSize bmpSize = m_bitmap.GetPreferredBitmapSizeFor( this );
#endif
wxBitmap bmp = m_bitmap.GetBitmapFor( this );
wxMemoryDC mdc( bmp );
r1.x = ( width - bmpSize.GetWidth() ) / 2;
if( r1.x < 0 )
r1.x = 0;
r1.y += ( size.GetHeight() - bmpSize.GetHeight() ) / 2;
dc.Blit( wxPoint( r1.x, r1.y ), bmpSize, &mdc, wxPoint( 0, 0 ), wxCOPY, true );
}
else
{
r1.y += ( ( size.GetHeight() - GetCharHeight() ) / 2 ) - 1;
dc.DrawLabel( m_label, r1, wxALIGN_CENTER_HORIZONTAL );
}
// Draw second part of button
wxRect r2;
r2.x = width;
r2.y = 0;
r2.width = m_arrowButtonWidth;
r2.height = size.GetHeight();
#ifdef __WXMAC__
// wxRendereNative doesn't handle dark mode on OSX.
drawBackground( r2 );
#else
r2.x -= 2;
wxRendererNative::Get().DrawPushButton( this, dc, r2, m_stateMenu );
#endif
wxRendererNative::Get().DrawDropArrow( this, dc, r2, m_stateMenu );
}
bool SPLIT_BUTTON::Enable( bool aEnable )
{
m_bIsEnable = aEnable;
wxPanel::Enable( m_bIsEnable );
if( m_bIsEnable
&& ( m_stateButton == wxCONTROL_DISABLED || m_stateMenu == wxCONTROL_DISABLED ) )
{
m_stateButton = 0;
m_stateMenu = 0;
Refresh();
}
if( !m_bIsEnable
&& ( m_stateButton != wxCONTROL_DISABLED || m_stateMenu != wxCONTROL_DISABLED ) )
{
m_stateButton = wxCONTROL_DISABLED;
m_stateMenu = wxCONTROL_DISABLED;
Refresh();
}
return aEnable;
}
|