File: xrc_dialog.py

package info (click to toggle)
python-pyface 8.0.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,944 kB
  • sloc: python: 54,107; makefile: 82
file content (123 lines) | stat: -rw-r--r-- 3,898 bytes parent folder | download
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
# (C) Copyright 2005-2023 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!

"""A dialog that is loaded from an XRC resource file.
"""


import os.path

# Major packages.
import wx
import wx.xrc


from traits.api import Instance, Str
import traits.util.resource


from .dialog import Dialog


# ----------------------------------------------------------------------------
# class 'XrcDialog'
# ----------------------------------------------------------------------------


class XrcDialog(Dialog):
    """A dialog that is loaded from an XRC resource file.
    """

    # ------------------------------------------------------------------------
    # Traits
    # ------------------------------------------------------------------------

    # 'XrcDialog' interface --------------------------------------------

    # Path to the xrc file relative to the class's module
    xrc_file = Str()

    # The ID of the dialog in the file
    id = Str("dialog")

    # The resource object
    resource = Instance(wx.xrc.XmlResource)

    # ------------------------------------------------------------------------
    # 'Dialog' interface
    # ------------------------------------------------------------------------

    def _create_control(self, parent):
        """
        Creates the dialog and loads it in from the resource file.
        """
        classpath = traits.util.resource.get_path(self)
        path = os.path.join(classpath, self.xrc_file)

        self.resource = wx.xrc.XmlResource(path)
        return self.resource.LoadDialog(parent, self.id)

    def _create_contents(self, dialog):
        """
        Calls add_handlers.  The actual content is created
        in _create_control by loading a resource file.
        """
        # Wire up the standard buttons
        # We change the ID on OK and CANCEL to the standard ids
        # so we get the default behavior
        okbutton = self.XRCCTRL("OK")
        if okbutton is not None:
            # Change the ID and set the handler
            okbutton.SetId(wx.ID_OK)
            self.control.Bind(wx.EVT_BUTTON, okbutton.GetId(), self._on_ok)
        cancelbutton = self.XRCCTRL("CANCEL")
        if cancelbutton is not None:
            # Change the ID and set the handler
            cancelbutton.SetId(wx.ID_CANCEL)
            self.control.Bind(
                wx.EVT_BUTTON, self._on_cancel, cancelbutton.GetId()
            )
        helpbutton = self.XRCCTRL("HELP")
        if helpbutton is not None:
            self.control.Bind(wx.EVT_BUTTON, self._on_help, helpbutton.GetId())

        self._add_handlers()

    # ------------------------------------------------------------------------
    # 'XrcDialog' interface
    # ------------------------------------------------------------------------

    def XRCID(self, name):
        """
        Returns the numeric widget id for the given name.
        """
        return wx.xrc.XRCID(name)

    def XRCCTRL(self, name):
        """
        Returns the control with the given name.
        """
        return self.control.Window.FindWindowById(self.XRCID(name))

    def set_validator(self, name, validator):
        """
        Sets the validator on the named control.
        """
        self.XRCCTRL(name).SetValidator(validator)

    # ------------------------------------------------------------------------
    # 'XrcDialog' protected interface
    # ------------------------------------------------------------------------

    def _add_handlers(self):
        """
        Override to add event handlers.
        """
        return