| 1 |
"""Custom XML handlers for wxPython file browse buttons. |
| 2 |
|
| 3 |
It supports both standard FileBrowseButton controls and |
| 4 |
FileBrowseButtonWithHistory controls, allowing configuration of properties such |
| 5 |
as labels, tooltips, dialog titles, and file filters. |
| 6 |
""" |
| 7 |
|
| 8 |
import wx |
| 9 |
import wx.lib.filebrowsebutton as filebrowse |
| 10 |
from wx import xrc |
| 11 |
|
| 12 |
from DisplayCAL.log import safe_print |
| 13 |
|
| 14 |
try: |
| 15 |
from DisplayCAL.wx_windows import ( |
| 16 |
FileBrowseBitmapButtonWithChoiceHistory as FileBrowseButtonWithHistory, |
| 17 |
) |
| 18 |
except ImportError: |
| 19 |
FileBrowseButtonWithHistory = filebrowse.FileBrowseButtonWithHistory |
| 20 |
|
| 21 |
|
| 22 |
class FileBrowseButtonXmlHandler(xrc.XmlResourceHandler): |
| 23 |
"""Custom XML resource handler for FileBrowseButton controls.""" |
| 24 |
|
| 25 |
def __init__(self): |
| 26 |
xrc.XmlResourceHandler.__init__(self) |
| 27 |
self._class = filebrowse.FileBrowseButton |
| 28 |
# Standard styles |
| 29 |
self.AddWindowStyles() |
| 30 |
|
| 31 |
def CanHandle(self, node): |
| 32 |
"""Check if the node can be handled by this handler. |
| 33 |
|
| 34 |
Args: |
| 35 |
node (xrc.XmlNode): The XML node to check. |
| 36 |
|
| 37 |
Returns: |
| 38 |
bool: True if the node is of class FileBrowseButton, False otherwise. |
| 39 |
""" |
| 40 |
return self.IsOfClass(node, self._class.__name__) |
| 41 |
|
| 42 |
# Process XML parameters and create the object |
| 43 |
def DoCreateResource(self): |
| 44 |
"""Create the FileBrowseButton control from XML parameters. |
| 45 |
|
| 46 |
Returns: |
| 47 |
FileBrowseButton: The created FileBrowseButton control. |
| 48 |
""" |
| 49 |
w = self._class( |
| 50 |
parent=self.GetParentAsWindow(), |
| 51 |
id=self.GetID(), |
| 52 |
pos=self.GetPosition(), |
| 53 |
size=self.GetSize(), |
| 54 |
style=self.GetStyle(), |
| 55 |
labelText=self.GetText("message") or "File Entry:", |
| 56 |
buttonText=self.GetText("buttonText") or "Browse", |
| 57 |
toolTip=self.GetText("toolTip") |
| 58 |
or "Type filename or click browse to choose file", |
| 59 |
dialogTitle=self.GetText("dialogTitle") or "Choose a file", |
| 60 |
startDirectory=self.GetText("startDirectory") or ".", |
| 61 |
initialValue=self.GetText("initialValue") or "", |
| 62 |
fileMask=self.GetText("wildcard") or "*.*", |
| 63 |
fileMode=self.GetLong("fileMode") or wx.FD_OPEN, |
| 64 |
labelWidth=self.GetLong("labelWidth") or 0, |
| 65 |
name=self.GetName(), |
| 66 |
) |
| 67 |
self.SetupWindow(w) |
| 68 |
if self.GetBool("hidden") and w.Shown: |
| 69 |
safe_print(f"{self.Name} should have been hidden") |
| 70 |
w.Hide() |
| 71 |
return w |
| 72 |
|
| 73 |
|
| 74 |
class FileBrowseButtonWithHistoryXmlHandler(FileBrowseButtonXmlHandler): |
| 75 |
"""Custom XML resource handler for FileBrowseButtonWithHistory controls.""" |
| 76 |
|
| 77 |
def __init__(self): |
| 78 |
FileBrowseButtonXmlHandler.__init__(self) |
| 79 |
self._class = FileBrowseButtonWithHistory |
| 80 |
|
| 81 |
def CanHandle(self, node): |
| 82 |
"""Check if the node can be handled by this handler. |
| 83 |
|
| 84 |
Args: |
| 85 |
node (xrc.XmlNode): The XML node to check. |
| 86 |
|
| 87 |
Returns: |
| 88 |
bool: True if the node is of class FileBrowseButtonWithHistory, |
| 89 |
False otherwise. |
| 90 |
""" |
| 91 |
return self.IsOfClass(node, self._class.__name__) or self.IsOfClass( |
| 92 |
node, "FileBrowseButtonWithHistory" |
| 93 |
) |