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 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
|
# ------------------------------------------------------------------------------------ #
# GRADIENTBUTTON wxPython IMPLEMENTATION
#
# Andrea Gavana, @ 07 October 2008
# Latest Revision: 27 Dec 2012, 21.00 GMT
#
#
# TODO List
#
# 1) Anything to do?
#
#
# For all kind of problems, requests of enhancements and bug reports,
# please write to me at:
#
# andrea.gavana@gmail.com
# andrea.gavana@maerskoil.com
#
# Or, obviously, to the wxPython mailing list!!!
#
# Tags: phoenix-port, unittest, documented, py3-port
#
# End Of Comments
# ------------------------------------------------------------------------------------ #
"""`GradientButton` mimics Windows CE mobile gradient buttons.
Description
===========
:class:`GradientButton` is another custom-drawn button class which mimics Windows CE
mobile gradient buttons, using a tri-vertex blended gradient plus some ClearType bold
font (best effect with Tahoma Bold). :class:`GradientButton` supports:
* Triple blended gradient background, with customizable colours;
* Custom colours for the "pressed" state;
* Rounded-corners buttons;
* Text-only or image+text buttons.
And a lot more. Check the demo for an almost complete review of the functionalities.
Usage
=====
Usage example::
import wx
import wx.lib.agw.gradientbutton as GB
class MyFrame(wx.Frame):
def __init__(self, parent: wx.Window | None) -> None:
wx.Frame.__init__(self, parent, -1, "GradientButton Demo")
panel = wx.Panel(self, -1)
# Create a vertical box sizer
sizer = wx.BoxSizer(wx.VERTICAL)
# One button without bitmap
button_size = wx.Size(100, 50)
sizer.Add(
GB.GradientButton(panel, -1, None, "Hello World", size=button_size),
0,
wx.ALL,
10,
)
# One button with bitmap
my_bitmap = wx.Bitmap("my_bitmap.png", wx.BITMAP_TYPE_PNG)
sizer.Add(
GB.GradientButton(panel, -1, my_bitmap, "GradientButton", size=button_size),
0,
wx.ALL,
10,
)
# Set the sizer for the panel
panel.SetSizer(sizer)
# Fit the frame to the sizer
self.Fit()
# our normal wxApp-derived class, as usual
app = wx.App(False)
frame = MyFrame(None)
app.SetTopWindow(frame)
frame.Show()
app.MainLoop()
Supported Platforms
===================
:class:`GradientButton` has been tested on the following platforms:
* Windows (Windows XP).
Window Styles
=============
`No particular window styles are available for this class.`
Events Processing
=================
This class processes the following events:
================= ======================================================================
Event Name Description
================= ======================================================================
``wx.EVT_BUTTON`` Process a `wxEVT_COMMAND_BUTTON_CLICKED` event,
when the button is clicked.
================= ======================================================================
License And Version
===================
:class:`GradientButton` is distributed under the wxPython license.
Latest Revision: Andrea Gavana @ 27 Dec 2012, 21.00 GMT
Version 0.3
"""
from typing import Optional, Union
import wx
HOVER = 1
"""Flag used to indicate that the mouse is hovering on a :class:`GradientButton`."""
CLICK = 2
"""Flag used to indicate that the :class:`GradientButton` is on a pressed state."""
class GradientButtonEvent(wx.PyCommandEvent):
"""Event sent from :class:`GradientButton` when the button is activated.
Args:
eventType (int): the event type;
eventId (int): the event identifier.
"""
def __init__(self, eventType: int, eventId: int) -> None:
wx.PyCommandEvent.__init__(self, eventType, eventId)
self.isDown = False
self.theButton: Union[None, GradientButton] = None
def SetButtonObj(self, btn: "GradientButton") -> None:
"""Set the event object for the event.
Args:
btn ('GradientButton'): The button object.
"""
self.theButton = btn
def GetButtonObj(self) -> Union[None, "GradientButton"]:
"""Return the object associated with this event.
Returns:
Union[None, GradientButton]: The button object associated with this event,
or None if no button is associated.
"""
return self.theButton
class GradientButton(wx.Control):
"""This is the main class implementation of :class:`GradientButton`.
Args:
parent (GradientButton): The :class:`GradientButton` parent.
id (int): Window identifier. A value of -1 indicates a default value.
bitmap (Optional[wx.Bitmap]): The button bitmap (if any).
label (str): The button text label;
pos (wx.Point): The control position.
A value of (-1, -1) indicates a default position, chosen by either the
windowing system or wxPython, depending on platform.
size (wx.Size): The control size.
A value of (-1, -1) indicates a default size, chosen by either the
windowing system or wxPython, depending on platform.
style (int): The button style (unused);
align (int): Text/bitmap alignment. wx.CENTER or wx.LEFT;
validator (wx.Validator): The validator associated to the button;
name (str): the button name.
"""
def __init__(
self,
parent: "GradientButton",
id: int = wx.ID_ANY,
bitmap: Optional[wx.Bitmap] = None,
label: str = "",
pos: wx.Point = wx.DefaultPosition,
size: wx.Size = wx.DefaultSize,
style: int = wx.NO_BORDER,
align: int = wx.CENTER,
validator: wx.Validator = wx.DefaultValidator,
name: str = "gradientbutton",
) -> None:
super().__init__(parent, id, pos, size, style, validator, name)
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Bind(wx.EVT_ERASE_BACKGROUND, lambda event: None)
self.Bind(wx.EVT_SIZE, self.OnSize)
self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
self.Bind(wx.EVT_LEAVE_WINDOW, self.OnMouseLeave)
self.Bind(wx.EVT_ENTER_WINDOW, self.OnMouseEnter)
self.Bind(wx.EVT_SET_FOCUS, self.OnGainFocus)
self.Bind(wx.EVT_KILL_FOCUS, self.OnLoseFocus)
self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
self.Bind(wx.EVT_KEY_UP, self.OnKeyUp)
self.Bind(wx.EVT_LEFT_DCLICK, self.OnLeftDown)
self._mouseAction = None
self._hasFocus = False
self._alignment = align
self.SetBitmapLabel(bitmap)
self.SetLabel(label)
self.InheritAttributes()
self.SetInitialSize(size)
self.SetBaseColours()
def SetBitmapLabel(self, bitmap: Union[None, wx.Bitmap]) -> None:
"""Set the bitmap label for the button.
Args:
bitmap (Union[Non, wx.Bitmap]): the bitmap label to set,.
"""
self._bitmap = bitmap
self.Refresh()
def SetBaseColours(
self,
startcolour: Union[None, wx.Colour] = None,
foregroundcolour: Union[None, wx.Colour] = None,
) -> None:
"""Set the bottom, top, pressed and foreground colour.
Args:
startcolour (wx.Colour): based colour to be used for bottom, top and
pressed.
foregroundcolour (wx.Colour): colour used for the text.
"""
if startcolour is None:
startcolour = wx.BLACK
if foregroundcolour is None:
foregroundcolour = wx.WHITE
self._bottomStartColour = startcolour
rgba = (
self._bottomStartColour.Red(),
self._bottomStartColour.Green(),
self._bottomStartColour.Blue(),
self._bottomStartColour.Alpha(),
)
self._bottomEndColour = self.LightColour(self._bottomStartColour, 20)
self._topStartColour = self.LightColour(self._bottomStartColour, 40)
self._topEndColour = self.LightColour(self._bottomStartColour, 25)
self._pressedTopColour = self.LightColour(self._bottomStartColour, 20)
self._pressedBottomColour = wx.Colour(*rgba)
self.SetForegroundColour(foregroundcolour)
def LightColour(self, colour: wx.Colour, percent: int) -> wx.Colour:
"""Return light contrast of `colour`.
The colour returned is from the scale of `colour` ==> white.
Args:
colour (wx.Colour): the input colour to be brightened;
percent (int): determines how light the colour will be.
`percent` = 100 returns white, `percent` = 0 returns `colour`.
Returns:
wx.Colour: The lightened colour.
"""
end_colour = wx.WHITE
rd = end_colour.Red() - colour.Red()
gd = end_colour.Green() - colour.Green()
bd = end_colour.Blue() - colour.Blue()
high = 100
# We take the percent way of the colour from colour -. white
i = percent
r = colour.Red() + ((i * rd * 100) / high) / 100
g = colour.Green() + ((i * gd * 100) / high) / 100
b = colour.Blue() + ((i * bd * 100) / high) / 100
a = colour.Alpha()
return wx.Colour(int(r), int(g), int(b), int(a))
def OnSize(self, event: wx.SizeEvent) -> None:
"""Handle the ``wx.EVT_SIZE`` event for :class:`GradientButton`.
Args:
event (wx.SizeEvent): a :class:`wx.SizeEvent` event to be processed.
"""
event.Skip()
self.Refresh()
def OnLeftDown(self, event: wx.MouseEvent) -> None:
"""Handle the ``wx.EVT_LEFT_DOWN`` event for :class:`GradientButton`.
Args:
event (wx.MouseEvent): a :class:`MouseEvent` event to be processed.
"""
if not self.IsEnabled():
return
self._mouseAction = CLICK
self.CaptureMouse()
self.Refresh()
event.Skip()
def OnLeftUp(self, event: wx.MouseEvent) -> None:
"""Handle the ``wx.EVT_LEFT_UP`` event for :class:`GradientButton`.
Args:
event (wx.MouseEvent): a :class:`MouseEvent` event to be processed.
"""
if not self.IsEnabled() or not self.HasCapture():
return
pos = event.GetPosition()
rect = self.GetClientRect()
if self.HasCapture():
self.ReleaseMouse()
if rect.Contains(pos):
self._mouseAction = HOVER
self.Notify()
else:
self._mouseAction = None
self.Refresh()
event.Skip()
def OnMouseEnter(self, event: wx.MouseEvent) -> None:
"""Handle the ``wx.EVT_ENTER_WINDOW`` event for :class:`GradientButton`.
Args:
event (wx.MouseEvent): a :class:`MouseEvent` event to be processed.
"""
if not self.IsEnabled():
return
self._mouseAction = HOVER
self.Refresh()
event.Skip()
def OnMouseLeave(self, event: wx.MouseEvent) -> None:
"""Handle the ``wx.EVT_LEAVE_WINDOW`` event for :class:`GradientButton`.
Args:
event (wx.MouseEvent): a :class:`MouseEvent` event to be processed.
"""
self._mouseAction = None
self.Refresh()
event.Skip()
def OnGainFocus(self, event: wx.FocusEvent) -> None:
"""Handle the ``wx.EVT_SET_FOCUS`` event for :class:`GradientButton`.
Args:
event (wx.FocusEvent): a :class:`FocusEvent` event to be processed.
"""
self._hasFocus = True
self.Refresh()
self.Update()
def OnLoseFocus(self, event: wx.FocusEvent) -> None:
"""Handle the ``wx.EVT_KILL_FOCUS`` event for :class:`GradientButton`.
Args:
event (wx.FocusEvent): a :class:`FocusEvent` event to be processed.
"""
self._hasFocus = False
self.Refresh()
self.Update()
def OnKeyDown(self, event: wx.KeyEvent) -> None:
"""Handle the ``wx.EVT_KEY_DOWN`` event for :class:`GradientButton`.
Args:
event (wx.KeyEvent): a :class:`KeyEvent` event to be processed.
"""
if self._hasFocus and event.GetKeyCode() == ord(" "):
self._mouseAction = HOVER
self.Refresh()
event.Skip()
def OnKeyUp(self, event: wx.KeyEvent) -> None:
"""Handle the ``wx.EVT_KEY_UP`` event for :class:`GradientButton`.
Args:
event (wx.KeyEvent): a :class:`KeyEvent` event to be processed.
"""
if self._hasFocus and event.GetKeyCode() == ord(" "):
self._mouseAction = HOVER
self.Notify()
self.Refresh()
event.Skip()
def OnPaint(self, event: wx.PaintEvent) -> None:
"""Handle the ``wx.EVT_PAINT`` event for :class:`GradientButton`.
Args:
event (wx.PaintEvent): a :class:`PaintEvent` event to be processed.
"""
dc = wx.BufferedPaintDC(self)
gc = wx.GraphicsContext.Create(dc)
dc.SetBackground(wx.Brush(self.GetParent().GetBackgroundColour()))
dc.Clear()
clientRect = self.GetClientRect()
gradientRect = wx.Rect(*clientRect)
capture = wx.Window.GetCapture()
x, y, width, height = clientRect
gradientRect.SetHeight(
gradientRect.GetHeight() // 2 + ((capture == self and [1] or [0])[0])
)
if capture != self:
if self._mouseAction == HOVER:
topStart = self.LightColour(self._topStartColour, 10)
topEnd = self.LightColour(self._topEndColour, 10)
else:
topStart, topEnd = self._topStartColour, self._topEndColour
rc1 = wx.Rect(x, y, width, height // 2)
path1 = self.GetPath(gc, rc1, 8)
br1 = gc.CreateLinearGradientBrush(
x, y, x, y + height / 2, topStart, topEnd
)
gc.SetBrush(br1)
gc.FillPath(path1) # draw main
path4 = gc.CreatePath()
path4.AddRectangle(x, y + height / 2 - 8, width, 8)
path4.CloseSubpath()
gc.SetBrush(br1)
gc.FillPath(path4)
else:
rc1 = wx.Rect(x, y, width, height)
path1 = self.GetPath(gc, rc1, 8)
gc.SetPen(wx.Pen(self._pressedTopColour))
gc.SetBrush(wx.Brush(self._pressedTopColour))
gc.FillPath(path1)
gradientRect.Offset((0, gradientRect.GetHeight()))
if capture != self:
if self._mouseAction == HOVER:
bottomStart = self.LightColour(self._bottomStartColour, 10)
bottomEnd = self.LightColour(self._bottomEndColour, 10)
else:
bottomStart, bottomEnd = self._bottomStartColour, self._bottomEndColour
rc3 = wx.Rect(x, y + height // 2, width, height // 2)
path3 = self.GetPath(gc, rc3, 8)
br3 = gc.CreateLinearGradientBrush(
x, y + height / 2, x, y + height, bottomStart, bottomEnd
)
gc.SetBrush(br3)
gc.FillPath(path3) # draw main
path4 = gc.CreatePath()
path4.AddRectangle(x, y + height / 2, width, 8)
path4.CloseSubpath()
gc.SetBrush(br3)
gc.FillPath(path4)
shadowOffset = 0
else:
rc2 = wx.Rect(
x + 1, gradientRect.height // 2, gradientRect.width, gradientRect.height
)
path2 = self.GetPath(gc, rc2, 8)
gc.SetPen(wx.Pen(self._pressedBottomColour))
gc.SetBrush(wx.Brush(self._pressedBottomColour))
gc.FillPath(path2)
shadowOffset = 1
# Create a ClientDC to get the text extent
client_dc = wx.ClientDC(self)
font = self.GetFont()
client_dc.SetFont(font)
label = self.GetLabel()
tw, th = client_dc.GetTextExtent(label)
if self._bitmap:
bw = self._bitmap.GetWidth()
bh = self._bitmap.GetHeight()
else:
bw = bh = 0
# Set default values for pos_x and pos_y
pos_x: float = 0.0
pos_y: float = (height - th) / 2 + shadowOffset
if self._alignment == wx.CENTER:
# adjust for bitmap and text to centre
pos_x = (width - bw - tw) / 2 + shadowOffset
pos_y = (height - bh) / 2 + shadowOffset
if self._bitmap:
# draw bitmap if available
gc.DrawBitmap(self._bitmap, pos_x, pos_y, bw, bh)
pos_x += bw + 2 # extra spacing from bitmap
elif self._alignment == wx.LEFT:
pos_x = 3 # adjust for bitmap and text to left
pos_y = (height - bh) / 2 + shadowOffset
if self._bitmap:
gc.DrawBitmap(
self._bitmap, pos_x, pos_y, bw, bh
) # draw bitmap if available
pos_x += bw + 3 # extra spacing from bitmap
gc.DrawText(label, pos_x + shadowOffset, pos_y)
def GetPath(self, gc: wx.GraphicsContext, rc: wx.Rect, r: int) -> wx.GraphicsPath:
"""Return a rounded :class:`GraphicsPath` rectangle.
Args:
gc (wx.GraphicsContext): an instance of :class:`GraphicsContext`;
rc (wx.Rect): a client rectangle;
r (int): the radius of the rounded part of the rectangle.
Returns:
wx.GraphicsPath: A rounded rectangle path.
"""
x, y, w, h = rc
path = gc.CreatePath()
path.AddRoundedRectangle(x, y, w, h, r)
path.CloseSubpath()
return path
def SetInitialSize(self, size: Optional[wx.Size] = None) -> None:
"""
Given the current font and bezel width settings, calculate and set a good size.
Args:
size (Optional[wx.Size]): an instance of :class:`wx.Size`.
"""
if size is None:
size = wx.DefaultSize
wx.Control.SetInitialSize(self, size)
SetBestSize = SetInitialSize
def AcceptsFocus(self) -> bool:
"""Return True if this window can be given focus by mouse click.
Note:
Overridden from :class:`wx.Control`.
Returns:
bool: True if the window can be given focus by mouse click, False otherwise.
"""
return self.IsShown() and self.IsEnabled()
def GetDefaultAttributes(self) -> wx.VisualAttributes:
"""Overridden base class virtual.
By default we should use the same font/colour attributes as the native
:class:`Button`.
Returns:
wx.VisualAttributes: The default visual attributes for the button.
"""
return wx.Button.GetClassDefaultAttributes()
def ShouldInheritColours(self) -> bool:
"""Overridden base class virtual.
Buttons usually don't inherit the parent's colours.
Note:
Overridden from :class:`wx.Control`.
Returns:
bool: True if the button should inherit the parent's colours,
False otherwise.
"""
return False
def Enable(self, enable: bool = True) -> None:
"""Enable/disable the button.
Args:
enable (bool): ``True`` to enable the button, ``False`` to disable it.
Note:
Overridden from :class:`wx.Control`.
"""
wx.Control.Enable(self, enable)
self.Refresh()
def SetTopStartColour(self, colour: wx.Colour) -> None:
"""Set the top start colour for the gradient shading.
Args:
colour (wx.Colour): a valid :class:`wx.Colour` object.
"""
self._topStartColour = colour
self.Refresh()
def GetTopStartColour(self) -> wx.Colour:
"""Return the top start colour for the gradient shading.
Returns:
wx.Colour: The top start colour for the gradient shading.
"""
return self._topStartColour
def SetTopEndColour(self, colour: wx.Colour) -> None:
"""Set the top end colour for the gradient shading.
Args:
colour (wx.Colour): a valid :class:`wx.Colour` object.
"""
self._topEndColour = colour
self.Refresh()
def GetTopEndColour(self) -> wx.Colour:
"""Return the top end colour for the gradient shading.
Returns:
wx.Colour: The top end colour of the gradient.
"""
return self._topEndColour
def SetBottomStartColour(self, colour: wx.Colour) -> None:
"""Set the top bottom colour for the gradient shading.
Args:
colour (wx.Colour): a valid :class:`wx.Colour` object.
"""
self._bottomStartColour = colour
self.Refresh()
def GetBottomStartColour(self) -> wx.Colour:
"""Return the bottom start colour for the gradient shading.
Returns:
wx.Colour: The bottom start colour for the gradient shading.
"""
return self._bottomStartColour
def SetBottomEndColour(self, colour: wx.Colour) -> None:
"""Set the bottom end colour for the gradient shading.
Args:
colour (wx.Colour): a valid :class:`wx.Colour` object.
"""
self._bottomEndColour = colour
self.Refresh()
def GetBottomEndColour(self) -> wx.Colour:
"""Return the bottom end colour for the gradient shading.
Returns:
wx.Colour: The bottom end colour of the gradient.
"""
return self._bottomEndColour
def SetPressedTopColour(self, colour: wx.Colour) -> None:
"""Set the pressed top start colour for the gradient shading.
Args:
colour (wx.Colour): a valid :class:`wx.Colour` object.
"""
self._pressedTopColour = colour
self.Refresh()
def GetPressedTopColour(self) -> wx.Colour:
"""Return the pressed top start colour for the gradient shading.
Returns:
wx.Colour: The pressed top start colour.
"""
return self._pressedTopColour
def SetPressedBottomColour(self, colour: wx.Colour) -> None:
"""Set the pressed bottom start colour for the gradient shading.
Args:
colour (wx.Colour): a valid :class:`wx.Colour` object.
"""
self._pressedBottomColour: wx.Colour = colour
self.Refresh()
def GetPressedBottomColour(self) -> wx.Colour:
"""Return the pressed bottom start colour for the gradient shading.
Returns:
wx.Colour: The pressed bottom start colour for the gradient shading.
"""
return self._pressedBottomColour
def SetForegroundColour(self, colour: wx.Colour) -> None:
"""Set the :class:`GradientButton` foreground (text) colour.
Args:
colour (wx.Colour): a valid :class:`wx.Colour` object.
Note:
Overridden from :class:`wx.Control`.
"""
wx.Control.SetForegroundColour(self, colour)
self.Refresh()
def DoGetBestSize(self) -> wx.Size:
"""Overridden base class virtual.
Determines the best size of the button based on the label and bezel size.
Note:
Overridden from :class:`wx.Control`.
Returns:
:class:`wx.Size`: The best size for the button.
"""
label = self.GetLabel()
if not label:
return wx.Size(112, 48)
dc = wx.ClientDC(self)
dc.SetFont(self.GetFont())
retWidth, retHeight = dc.GetTextExtent(label)
bmpWidth = 0
bmpHeight = 0
constant = 15
if self._bitmap:
bmpWidth, bmpHeight = self._bitmap.GetWidth() + 10, self._bitmap.GetHeight()
retWidth += bmpWidth
retHeight = max(bmpHeight, retHeight)
constant = 15
return wx.Size(retWidth + constant, retHeight + constant)
def SetDefault(self) -> None:
"""Set the default button."""
tlw = wx.GetTopLevelParent(self)
if isinstance(tlw, (wx.Dialog, wx.Frame)) and hasattr(tlw, "SetDefaultItem"):
tlw.SetDefaultItem(self)
else:
# Fallback: Set the button as the default in a different way if possible
if hasattr(self, "SetDefault"):
self.SetDefault()
def Notify(self) -> None:
"""Actually send a ``wx.EVT_BUTTON`` event to the listener (if any)."""
evt = GradientButtonEvent(wx.wxEVT_COMMAND_BUTTON_CLICKED, self.GetId())
evt.SetButtonObj(self)
evt.SetEventObject(self)
self.GetEventHandler().ProcessEvent(evt)
|