Debian Codegraph @

DisplayCAL/lib/agw/gradientbutton.py

raw
1 # ------------------------------------------------------------------------------------ #
2 # GRADIENTBUTTON wxPython IMPLEMENTATION
3 #
4 # Andrea Gavana, @ 07 October 2008
5 # Latest Revision: 27 Dec 2012, 21.00 GMT
6 #
7 #
8 # TODO List
9 #
10 # 1) Anything to do?
11 #
12 #
13 # For all kind of problems, requests of enhancements and bug reports,
14 # please write to me at:
15 #
16 # [email protected]
17 # [email protected]
18 #
19 # Or, obviously, to the wxPython mailing list!!!
20 #
21 # Tags: phoenix-port, unittest, documented, py3-port
22 #
23 # End Of Comments
24 # ------------------------------------------------------------------------------------ #
25
26 """`GradientButton` mimics Windows CE mobile gradient buttons.
27
28 Description
29 ===========
30
31 :class:`GradientButton` is another custom-drawn button class which mimics Windows CE
32 mobile gradient buttons, using a tri-vertex blended gradient plus some ClearType bold
33 font (best effect with Tahoma Bold). :class:`GradientButton` supports:
34
35 * Triple blended gradient background, with customizable colours;
36 * Custom colours for the "pressed" state;
37 * Rounded-corners buttons;
38 * Text-only or image+text buttons.
39
40 And a lot more. Check the demo for an almost complete review of the functionalities.
41
42 Usage
43 =====
44
45 Usage example::
46
47 import wx
48 import wx.lib.agw.gradientbutton as GB
49
50
51 class MyFrame(wx.Frame):
52
53 def __init__(self, parent: None | wx.Window) -> None:
54 wx.Frame.__init__(self, parent, -1, "GradientButton Demo")
55
56 panel = wx.Panel(self, -1)
57
58 # Create a vertical box sizer
59 sizer = wx.BoxSizer(wx.VERTICAL)
60
61 # One button without bitmap
62 button_size = wx.Size(100, 50)
63 sizer.Add(
64 GB.GradientButton(panel, -1, None, "Hello World", size=button_size),
65 0,
66 wx.ALL,
67 10,
68 )
69
70 # One button with bitmap
71 my_bitmap = wx.Bitmap("my_bitmap.png", wx.BITMAP_TYPE_PNG)
72 sizer.Add(
73 GB.GradientButton(panel, -1, my_bitmap, "GradientButton", size=button_size),
74 0,
75 wx.ALL,
76 10,
77 )
78
79 # Set the sizer for the panel
80 panel.SetSizer(sizer)
81
82 # Fit the frame to the sizer
83 self.Fit()
84
85
86 # our normal wxApp-derived class, as usual
87
88 app = wx.App(False)
89
90 frame = MyFrame(None)
91 app.SetTopWindow(frame)
92 frame.Show()
93
94 app.MainLoop()
95
96 Supported Platforms
97 ===================
98
99 :class:`GradientButton` has been tested on the following platforms:
100 * Windows (Windows XP).
101
102
103 Window Styles
104 =============
105
106 `No particular window styles are available for this class.`
107
108
109 Events Processing
110 =================
111
112 This class processes the following events:
113
114 ================= ======================================================================
115 Event Name Description
116 ================= ======================================================================
117 ``wx.EVT_BUTTON`` Process a `wxEVT_COMMAND_BUTTON_CLICKED` event,
118 when the button is clicked.
119 ================= ======================================================================
120
121
122 License And Version
123 ===================
124
125 :class:`GradientButton` is distributed under the wxPython license.
126
127 Latest Revision: Andrea Gavana @ 27 Dec 2012, 21.00 GMT
128
129 Version 0.3
130 """
131
132 from __future__ import annotations
133
134 import wx
135
136 HOVER = 1
137 """Flag used to indicate that the mouse is hovering on a :class:`GradientButton`."""
138 CLICK = 2
139 """Flag used to indicate that the :class:`GradientButton` is on a pressed state."""
140
141
142 class GradientButtonEvent(wx.PyCommandEvent):
143 """Event sent from :class:`GradientButton` when the button is activated.
144
145 Args:
146 eventType (int): the event type;
147 eventId (int): the event identifier.
148 """
149
150 def __init__(self, event_type: int, event_id: int) -> None:
151 wx.PyCommandEvent.__init__(self, event_type, event_id)
152 self.isDown = False
153 self.theButton: None | GradientButton = None
154
155 def SetButtonObj(self, btn: GradientButton) -> None: # noqa: N802
156 """Set the event object for the event.
157
158 Args:
159 btn ('GradientButton'): The button object.
160 """
161 self.theButton = btn
162
163 def GetButtonObj(self) -> None | GradientButton: # noqa: N802
164 """Return the object associated with this event.
165
166 Returns:
167 None | GradientButton: The button object associated with this event,
168 or None if no button is associated.
169 """
170 return self.theButton
171
172
173 class GradientButton(wx.Control):
174 """This is the main class implementation of :class:`GradientButton`.
175
176 Args:
177 parent (GradientButton): The :class:`GradientButton` parent.
178 id (int): Window identifier. A value of -1 indicates a default value.
179 bitmap (None | wx.Bitmap): The button bitmap (if any).
180 label (str): The button text label;
181 pos (wx.Point): The control position.
182 A value of (-1, -1) indicates a default position, chosen by either the
183 windowing system or wxPython, depending on platform.
184 size (wx.Size): The control size.
185 A value of (-1, -1) indicates a default size, chosen by either the
186 windowing system or wxPython, depending on platform.
187 style (int): The button style (unused);
188 align (int): Text/bitmap alignment. wx.CENTER or wx.LEFT;
189 validator (wx.Validator): The validator associated to the button;
190 name (str): the button name.
191 """
192
193 def __init__(
194 self,
195 parent: GradientButton,
196 id: int = wx.ID_ANY, # noqa: A002
197 bitmap: None | wx.Bitmap = None,
198 label: str = "",
199 pos: wx.Point = wx.DefaultPosition,
200 size: wx.Size = wx.DefaultSize,
201 style: int = wx.NO_BORDER,
202 align: int = wx.CENTER,
203 validator: wx.Validator = wx.DefaultValidator,
204 name: str = "gradientbutton",
205 ) -> None:
206 super().__init__(parent, id, pos, size, style, validator, name)
207
208 self.Bind(wx.EVT_PAINT, self.OnPaint)
209 self.Bind(wx.EVT_ERASE_BACKGROUND, lambda event: None)
210 self.Bind(wx.EVT_SIZE, self.OnSize)
211 self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
212 self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
213 self.Bind(wx.EVT_LEAVE_WINDOW, self.OnMouseLeave)
214 self.Bind(wx.EVT_ENTER_WINDOW, self.OnMouseEnter)
215 self.Bind(wx.EVT_SET_FOCUS, self.OnGainFocus)
216 self.Bind(wx.EVT_KILL_FOCUS, self.OnLoseFocus)
217 self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
218 self.Bind(wx.EVT_KEY_UP, self.OnKeyUp)
219 self.Bind(wx.EVT_LEFT_DCLICK, self.OnLeftDown)
220
221 self._mouseAction = None
222 self._hasFocus = False
223 self._alignment = align
224 self.SetBitmapLabel(bitmap)
225
226 self.SetLabel(label)
227 self.InheritAttributes()
228 self.SetInitialSize(size)
229
230 self.SetBaseColours()
231
232 def SetBitmapLabel(self, bitmap: None | wx.Bitmap) -> None: # noqa: N802
233 """Set the bitmap label for the button.
234
235 Args:
236 bitmap (None | wx.Bitmap): the bitmap label to set,.
237 """
238 self._bitmap = bitmap
239 self.Refresh()
240
241 def SetBaseColours( # noqa: N802
242 self,
243 startcolour: None | wx.Colour = None,
244 foregroundcolour: None | wx.Colour = None,
245 ) -> None:
246 """Set the bottom, top, pressed and foreground colour.
247
248 Args:
249 startcolour (wx.Colour): based colour to be used for bottom, top and
250 pressed.
251 foregroundcolour (wx.Colour): colour used for the text.
252 """
253 if startcolour is None:
254 startcolour = wx.BLACK
255 if foregroundcolour is None:
256 foregroundcolour = wx.WHITE
257
258 self._bottomStartColour = startcolour
259 rgba = (
260 self._bottomStartColour.Red(),
261 self._bottomStartColour.Green(),
262 self._bottomStartColour.Blue(),
263 self._bottomStartColour.Alpha(),
264 )
265 self._bottomEndColour = self.LightColour(self._bottomStartColour, 20)
266 self._topStartColour = self.LightColour(self._bottomStartColour, 40)
267 self._topEndColour = self.LightColour(self._bottomStartColour, 25)
268 self._pressedTopColour = self.LightColour(self._bottomStartColour, 20)
269 self._pressedBottomColour = wx.Colour(*rgba)
270 self.SetForegroundColour(foregroundcolour)
271
272 def LightColour(self, colour: wx.Colour, percent: int) -> wx.Colour: # noqa: N802
273 """Return light contrast of `colour`.
274
275 The colour returned is from the scale of `colour` ==> white.
276
277 Args:
278 colour (wx.Colour): the input colour to be brightened;
279 percent (int): determines how light the colour will be.
280 `percent` = 100 returns white, `percent` = 0 returns `colour`.
281
282 Returns:
283 wx.Colour: The lightened colour.
284 """
285 end_colour = wx.WHITE
286 rd = end_colour.Red() - colour.Red()
287 gd = end_colour.Green() - colour.Green()
288 bd = end_colour.Blue() - colour.Blue()
289 high = 100
290
291 # We take the percent way of the colour from colour -. white
292 i = percent
293 r = colour.Red() + ((i * rd * 100) / high) / 100
294 g = colour.Green() + ((i * gd * 100) / high) / 100
295 b = colour.Blue() + ((i * bd * 100) / high) / 100
296 a = colour.Alpha()
297
298 return wx.Colour(int(r), int(g), int(b), int(a))
299
300 def OnSize(self, event: wx.SizeEvent) -> None: # noqa: N802
301 """Handle the ``wx.EVT_SIZE`` event for :class:`GradientButton`.
302
303 Args:
304 event (wx.SizeEvent): a :class:`wx.SizeEvent` event to be processed.
305 """
306 event.Skip()
307 self.Refresh()
308
309 def OnLeftDown(self, event: wx.MouseEvent) -> None: # noqa: N802
310 """Handle the ``wx.EVT_LEFT_DOWN`` event for :class:`GradientButton`.
311
312 Args:
313 event (wx.MouseEvent): a :class:`MouseEvent` event to be processed.
314 """
315 if not self.IsEnabled():
316 return
317
318 self._mouseAction = CLICK
319 self.CaptureMouse()
320 self.Refresh()
321 event.Skip()
322
323 def OnLeftUp(self, event: wx.MouseEvent) -> None: # noqa: N802
324 """Handle the ``wx.EVT_LEFT_UP`` event for :class:`GradientButton`.
325
326 Args:
327 event (wx.MouseEvent): a :class:`MouseEvent` event to be processed.
328 """
329 if not self.IsEnabled() or not self.HasCapture():
330 return
331
332 pos = event.GetPosition()
333 rect = self.GetClientRect()
334
335 if self.HasCapture():
336 self.ReleaseMouse()
337
338 if rect.Contains(pos):
339 self._mouseAction = HOVER
340 self.Notify()
341 else:
342 self._mouseAction = None
343
344 self.Refresh()
345 event.Skip()
346
347 def OnMouseEnter(self, event: wx.MouseEvent) -> None: # noqa: N802
348 """Handle the ``wx.EVT_ENTER_WINDOW`` event for :class:`GradientButton`.
349
350 Args:
351 event (wx.MouseEvent): a :class:`MouseEvent` event to be processed.
352 """
353 if not self.IsEnabled():
354 return
355
356 self._mouseAction = HOVER
357 self.Refresh()
358 event.Skip()
359
360 def OnMouseLeave(self, event: wx.MouseEvent) -> None: # noqa: N802
361 """Handle the ``wx.EVT_LEAVE_WINDOW`` event for :class:`GradientButton`.
362
363 Args:
364 event (wx.MouseEvent): a :class:`MouseEvent` event to be processed.
365 """
366 self._mouseAction = None
367 self.Refresh()
368 event.Skip()
369
370 def OnGainFocus(self, event: wx.FocusEvent) -> None: # noqa: N802
371 """Handle the ``wx.EVT_SET_FOCUS`` event for :class:`GradientButton`.
372
373 Args:
374 event (wx.FocusEvent): a :class:`FocusEvent` event to be processed.
375 """
376 self._hasFocus = True
377 self.Refresh()
378 self.Update()
379
380 def OnLoseFocus(self, event: wx.FocusEvent) -> None: # noqa: N802
381 """Handle the ``wx.EVT_KILL_FOCUS`` event for :class:`GradientButton`.
382
383 Args:
384 event (wx.FocusEvent): a :class:`FocusEvent` event to be processed.
385 """
386 self._hasFocus = False
387 self.Refresh()
388 self.Update()
389
390 def OnKeyDown(self, event: wx.KeyEvent) -> None: # noqa: N802
391 """Handle the ``wx.EVT_KEY_DOWN`` event for :class:`GradientButton`.
392
393 Args:
394 event (wx.KeyEvent): a :class:`KeyEvent` event to be processed.
395 """
396 if self._hasFocus and event.GetKeyCode() == ord(" "):
397 self._mouseAction = HOVER
398 self.Refresh()
399 event.Skip()
400
401 def OnKeyUp(self, event: wx.KeyEvent) -> None: # noqa: N802
402 """Handle the ``wx.EVT_KEY_UP`` event for :class:`GradientButton`.
403
404 Args:
405 event (wx.KeyEvent): a :class:`KeyEvent` event to be processed.
406 """
407 if self._hasFocus and event.GetKeyCode() == ord(" "):
408 self._mouseAction = HOVER
409 self.Notify()
410 self.Refresh()
411 event.Skip()
412
413 def OnPaint(self, event: wx.PaintEvent) -> None: # noqa: N802
414 """Handle the ``wx.EVT_PAINT`` event for :class:`GradientButton`.
415
416 Args:
417 event (wx.PaintEvent): a :class:`PaintEvent` event to be processed.
418 """
419 dc = wx.BufferedPaintDC(self)
420 gc = wx.GraphicsContext.Create(dc)
421 dc.SetBackground(wx.Brush(self.GetParent().GetBackgroundColour()))
422 dc.Clear()
423
424 client_rect = self.GetClientRect()
425 gradient_rect = wx.Rect(*client_rect)
426 capture = wx.Window.GetCapture()
427
428 x, y, width, height = client_rect
429
430 gradient_rect.SetHeight(
431 gradient_rect.GetHeight() // 2 + (((capture == self and [1]) or [0])[0])
432 )
433 if capture != self:
434 if self._mouseAction == HOVER:
435 top_start = self.LightColour(self._topStartColour, 10)
436 top_end = self.LightColour(self._topEndColour, 10)
437 else:
438 top_start, top_end = self._topStartColour, self._topEndColour
439
440 rc1 = wx.Rect(x, y, width, height // 2)
441 path1 = self.GetPath(gc, rc1, 8)
442 br1 = gc.CreateLinearGradientBrush(
443 x, y, x, y + height / 2, top_start, top_end
444 )
445 gc.SetBrush(br1)
446 gc.FillPath(path1) # draw main
447
448 path4 = gc.CreatePath()
449 path4.AddRectangle(x, y + height / 2 - 8, width, 8)
450 path4.CloseSubpath()
451 gc.SetBrush(br1)
452 gc.FillPath(path4)
453
454 else:
455 rc1 = wx.Rect(x, y, width, height)
456 path1 = self.GetPath(gc, rc1, 8)
457 gc.SetPen(wx.Pen(self._pressedTopColour))
458 gc.SetBrush(wx.Brush(self._pressedTopColour))
459 gc.FillPath(path1)
460
461 gradient_rect.Offset((0, gradient_rect.GetHeight()))
462
463 if capture != self:
464 if self._mouseAction == HOVER:
465 bottom_start = self.LightColour(self._bottomStartColour, 10)
466 bottom_end = self.LightColour(self._bottomEndColour, 10)
467 else:
468 bottom_start, bottom_end = (
469 self._bottomStartColour,
470 self._bottomEndColour,
471 )
472
473 rc3 = wx.Rect(x, y + height // 2, width, height // 2)
474 path3 = self.GetPath(gc, rc3, 8)
475 br3 = gc.CreateLinearGradientBrush(
476 x, y + height / 2, x, y + height, bottom_start, bottom_end
477 )
478 gc.SetBrush(br3)
479 gc.FillPath(path3) # draw main
480
481 path4 = gc.CreatePath()
482 path4.AddRectangle(x, y + height / 2, width, 8)
483 path4.CloseSubpath()
484 gc.SetBrush(br3)
485 gc.FillPath(path4)
486
487 shadow_offset = 0
488 else:
489 rc2 = wx.Rect(
490 x + 1,
491 gradient_rect.height // 2,
492 gradient_rect.width,
493 gradient_rect.height,
494 )
495 path2 = self.GetPath(gc, rc2, 8)
496 gc.SetPen(wx.Pen(self._pressedBottomColour))
497 gc.SetBrush(wx.Brush(self._pressedBottomColour))
498 gc.FillPath(path2)
499 shadow_offset = 1
500
501 # Create a ClientDC to get the text extent
502 client_dc = wx.ClientDC(self)
503 font = self.GetFont()
504 client_dc.SetFont(font)
505 label = self.GetLabel()
506 tw, th = client_dc.GetTextExtent(label)
507
508 if self._bitmap:
509 bw = self._bitmap.GetWidth()
510 bh = self._bitmap.GetHeight()
511 else:
512 bw = bh = 0
513
514 # Set default values for pos_x and pos_y
515 pos_x: float = 0.0
516 pos_y: float = (height - th) / 2 + shadow_offset
517
518 if self._alignment == wx.CENTER:
519 # adjust for bitmap and text to centre
520 pos_x = (width - bw - tw) / 2 + shadow_offset
521 pos_y = (height - bh) / 2 + shadow_offset
522 if self._bitmap:
523 # draw bitmap if available
524 gc.DrawBitmap(self._bitmap, pos_x, pos_y, bw, bh)
525 pos_x += bw + 2 # extra spacing from bitmap
526 elif self._alignment == wx.LEFT:
527 pos_x = 3 # adjust for bitmap and text to left
528 pos_y = (height - bh) / 2 + shadow_offset
529 if self._bitmap:
530 gc.DrawBitmap(
531 self._bitmap, pos_x, pos_y, bw, bh
532 ) # draw bitmap if available
533 pos_x += bw + 3 # extra spacing from bitmap
534
535 gc.DrawText(label, pos_x + shadow_offset, pos_y)
536
537 def GetPath(self, gc: wx.GraphicsContext, rc: wx.Rect, r: int) -> wx.GraphicsPath: # noqa: N802
538 """Return a rounded :class:`GraphicsPath` rectangle.
539
540 Args:
541 gc (wx.GraphicsContext): an instance of :class:`GraphicsContext`;
542 rc (wx.Rect): a client rectangle;
543 r (int): the radius of the rounded part of the rectangle.
544
545 Returns:
546 wx.GraphicsPath: A rounded rectangle path.
547 """
548 x, y, w, h = rc
549 path = gc.CreatePath()
550 path.AddRoundedRectangle(x, y, w, h, r)
551 path.CloseSubpath()
552 return path
553
554 def SetInitialSize(self, size: None | wx.Size = None) -> None: # noqa: N802
555 """Calculate and set a suitable size based on font and bezel width.
556
557 Args:
558 size (None | wx.Size): A :class:`wx.Size` instance.
559 """
560 if size is None:
561 size = wx.DefaultSize
562 wx.Control.SetInitialSize(self, size)
563
564 SetBestSize = SetInitialSize
565
566 def AcceptsFocus(self) -> bool: # noqa: N802
567 """Return True if this window can be given focus by mouse click.
568
569 Note:
570 Overridden from :class:`wx.Control`.
571
572 Returns:
573 bool: True if the window can be given focus by mouse click, False otherwise.
574 """
575 return self.IsShown() and self.IsEnabled()
576
577 def GetDefaultAttributes(self) -> wx.VisualAttributes: # noqa: N802
578 """Overridden base class virtual.
579
580 By default we should use the same font/colour attributes as the native
581 :class:`Button`.
582
583 Returns:
584 wx.VisualAttributes: The default visual attributes for the button.
585 """
586 return wx.Button.GetClassDefaultAttributes()
587
588 def ShouldInheritColours(self) -> bool: # noqa: N802
589 """Overridden base class virtual.
590
591 Buttons usually don't inherit the parent's colours.
592
593 Note:
594 Overridden from :class:`wx.Control`.
595
596 Returns:
597 bool: True if the button should inherit the parent's colours,
598 False otherwise.
599 """
600 return False
601
602 def Enable(self, enable: bool = True) -> None: # noqa: N802
603 """Enable/disable the button.
604
605 Args:
606 enable (bool): ``True`` to enable the button, ``False`` to disable it.
607
608 Note:
609 Overridden from :class:`wx.Control`.
610 """
611 wx.Control.Enable(self, enable)
612 self.Refresh()
613
614 def SetTopStartColour(self, colour: wx.Colour) -> None: # noqa: N802
615 """Set the top start colour for the gradient shading.
616
617 Args:
618 colour (wx.Colour): a valid :class:`wx.Colour` object.
619 """
620 self._topStartColour = colour
621 self.Refresh()
622
623 def GetTopStartColour(self) -> wx.Colour: # noqa: N802
624 """Return the top start colour for the gradient shading.
625
626 Returns:
627 wx.Colour: The top start colour for the gradient shading.
628 """
629 return self._topStartColour
630
631 def SetTopEndColour(self, colour: wx.Colour) -> None: # noqa: N802
632 """Set the top end colour for the gradient shading.
633
634 Args:
635 colour (wx.Colour): a valid :class:`wx.Colour` object.
636 """
637 self._topEndColour = colour
638 self.Refresh()
639
640 def GetTopEndColour(self) -> wx.Colour: # noqa: N802
641 """Return the top end colour for the gradient shading.
642
643 Returns:
644 wx.Colour: The top end colour of the gradient.
645 """
646 return self._topEndColour
647
648 def SetBottomStartColour(self, colour: wx.Colour) -> None: # noqa: N802
649 """Set the top bottom colour for the gradient shading.
650
651 Args:
652 colour (wx.Colour): a valid :class:`wx.Colour` object.
653 """
654 self._bottomStartColour = colour
655 self.Refresh()
656
657 def GetBottomStartColour(self) -> wx.Colour: # noqa: N802
658 """Return the bottom start colour for the gradient shading.
659
660 Returns:
661 wx.Colour: The bottom start colour for the gradient shading.
662 """
663 return self._bottomStartColour
664
665 def SetBottomEndColour(self, colour: wx.Colour) -> None: # noqa: N802
666 """Set the bottom end colour for the gradient shading.
667
668 Args:
669 colour (wx.Colour): a valid :class:`wx.Colour` object.
670 """
671 self._bottomEndColour = colour
672 self.Refresh()
673
674 def GetBottomEndColour(self) -> wx.Colour: # noqa: N802
675 """Return the bottom end colour for the gradient shading.
676
677 Returns:
678 wx.Colour: The bottom end colour of the gradient.
679 """
680 return self._bottomEndColour
681
682 def SetPressedTopColour(self, colour: wx.Colour) -> None: # noqa: N802
683 """Set the pressed top start colour for the gradient shading.
684
685 Args:
686 colour (wx.Colour): a valid :class:`wx.Colour` object.
687 """
688 self._pressedTopColour = colour
689 self.Refresh()
690
691 def GetPressedTopColour(self) -> wx.Colour: # noqa: N802
692 """Return the pressed top start colour for the gradient shading.
693
694 Returns:
695 wx.Colour: The pressed top start colour.
696 """
697 return self._pressedTopColour
698
699 def SetPressedBottomColour(self, colour: wx.Colour) -> None: # noqa: N802
700 """Set the pressed bottom start colour for the gradient shading.
701
702 Args:
703 colour (wx.Colour): a valid :class:`wx.Colour` object.
704 """
705 self._pressedBottomColour: wx.Colour = colour
706 self.Refresh()
707
708 def GetPressedBottomColour(self) -> wx.Colour: # noqa: N802
709 """Return the pressed bottom start colour for the gradient shading.
710
711 Returns:
712 wx.Colour: The pressed bottom start colour for the gradient shading.
713 """
714 return self._pressedBottomColour
715
716 def SetForegroundColour(self, colour: wx.Colour) -> None: # noqa: N802
717 """Set the :class:`GradientButton` foreground (text) colour.
718
719 Args:
720 colour (wx.Colour): a valid :class:`wx.Colour` object.
721
722 Note:
723 Overridden from :class:`wx.Control`.
724 """
725 wx.Control.SetForegroundColour(self, colour)
726 self.Refresh()
727
728 def DoGetBestSize(self) -> wx.Size: # noqa: N802
729 """Overridden base class virtual.
730
731 Determines the best size of the button based on the label and bezel size.
732
733 Note:
734 Overridden from :class:`wx.Control`.
735
736 Returns:
737 :class:`wx.Size`: The best size for the button.
738 """
739 label = self.GetLabel()
740 if not label:
741 return wx.Size(112, 48)
742
743 dc = wx.ClientDC(self)
744 dc.SetFont(self.GetFont())
745 ret_width, ret_height = dc.GetTextExtent(label)
746
747 bmp_width = 0
748 bmp_height = 0
749 constant = 15
750 if self._bitmap:
751 bmp_width, bmp_height = (
752 self._bitmap.GetWidth() + 10,
753 self._bitmap.GetHeight(),
754 )
755 ret_width += bmp_width
756 ret_height = max(bmp_height, ret_height)
757 constant = 15
758
759 return wx.Size(ret_width + constant, ret_height + constant)
760
761 def SetDefault(self) -> None: # noqa: N802
762 """Set the default button."""
763 tlw = wx.GetTopLevelParent(self)
764 if isinstance(tlw, (wx.Dialog, wx.Frame)) and hasattr(tlw, "SetDefaultItem"):
765 tlw.SetDefaultItem(self)
766 elif hasattr(self, "SetDefault"):
767 # Fallback: Set the button as the default in a different way if possible
768 self.SetDefault()
769
770 def Notify(self) -> None: # noqa: N802
771 """Actually send a ``wx.EVT_BUTTON`` event to the listener (if any)."""
772 evt = GradientButtonEvent(wx.wxEVT_COMMAND_BUTTON_CLICKED, self.GetId())
773 evt.SetButtonObj(self)
774 evt.SetEventObject(self)
775 self.GetEventHandler().ProcessEvent(evt)