Debian Codegraph @

DisplayCAL/xh_bitmapctrls.py

raw
1 """Custom XML resource handlers for wxBitmapButton and wxStaticBitmap.
2
3 It includes functionality to process XML parameters, load bitmaps, and
4 configure the controls.
5 """
6
7 import os
8
9 import wx
10 from wx import xrc
11
12 from DisplayCAL.config import get_bitmap
13 from DisplayCAL.log import safe_print
14
15
16 class BitmapButton(xrc.XmlResourceHandler):
17 """Custom XML resource handler for wxBitmapButton controls."""
18
19 def __init__(self):
20 xrc.XmlResourceHandler.__init__(self)
21 # Standard styles
22 self.AddWindowStyles()
23
24 def CanHandle(self, node):
25 """Check if the node can be handled by this handler.
26
27 Args:
28 node (xrc.XmlNode): The XML node to check.
29
30 Returns:
31 bool: True if the node is of class wxBitmapButton, False otherwise.
32 """
33 return self.IsOfClass(node, "wxBitmapButton")
34
35 # Process XML parameters and create the object
36 def DoCreateResource(self):
37 """Create the BitmapButton control from XML parameters.
38
39 Returns:
40 wx.BitmapButton: The created BitmapButton control.
41 """
42 name = os.path.splitext(self.GetText("bitmap"))[0]
43 if name.startswith("../"):
44 name = name[3:]
45 bitmap = get_bitmap(name)
46 w = wx.BitmapButton(
47 self.GetParentAsWindow(),
48 self.GetID(),
49 bitmap,
50 pos=self.GetPosition(),
51 size=self.GetSize(),
52 style=self.GetStyle(),
53 name=self.GetName(),
54 )
55
56 self.SetupWindow(w)
57 if self.GetBool("hidden") and w.Shown:
58 safe_print(f"{self.Name} should have been hidden")
59 w.Hide()
60 return w
61
62
63 class StaticBitmap(xrc.XmlResourceHandler):
64 """Custom XML resource handler for wxStaticBitmap controls."""
65
66 def __init__(self):
67 xrc.XmlResourceHandler.__init__(self)
68 # Standard styles
69 self.AddWindowStyles()
70
71 def CanHandle(self, node):
72 """Check if the node can be handled by this handler.
73
74 Args:
75 node (xrc.XmlNode): The XML node to check.
76
77 Returns:
78 bool: True if the node is of class wxStaticBitmap, False otherwise.
79 """
80 return self.IsOfClass(node, "wxStaticBitmap")
81
82 # Process XML parameters and create the object
83 def DoCreateResource(self):
84 """Create the StaticBitmap control from XML parameters.
85
86 Returns:
87 wx.StaticBitmap: The created StaticBitmap control.
88 """
89 name = os.path.splitext(self.GetText("bitmap"))[0]
90 if name.startswith("../"):
91 name = name[3:]
92 bitmap = get_bitmap(name)
93 w = wx.StaticBitmap(
94 self.GetParentAsWindow(),
95 self.GetID(),
96 bitmap,
97 pos=self.GetPosition(),
98 size=self.GetSize(),
99 style=self.GetStyle(),
100 name=self.GetName(),
101 )
102
103 self.SetupWindow(w)
104 if self.GetBool("hidden") and w.Shown:
105 safe_print(f"{self.Name} should have been hidden")
106 w.Hide()
107 return w