| 1 | # Based on iwidgets2.2.0/dialog.itk and iwidgets2.2.0/dialogshell.itk code. |
| 2 | |
| 3 | # Convention: |
| 4 | # Each dialog window should have one of these as the rightmost button: |
| 5 | # Close Close a window which only displays information. |
| 6 | # Cancel Close a window which may be used to change the state of |
| 7 | # the application. |
| 8 | |
| 9 | import sys |
| 10 | import types |
| 11 | import Tkinter |
| 12 | import Pmw |
| 13 | |
| 14 | # A Toplevel with a ButtonBox and child site. |
| 15 | |
| 16 | class Dialog(Pmw.MegaToplevel): |
| 17 | def __init__(self, parent = None, **kw): |
| 18 | |
| 19 | # Define the megawidget options. |
| 20 | INITOPT = Pmw.INITOPT |
| 21 | optiondefs = ( |
| 22 | ('buttonbox_hull_borderwidth', 1, None), |
| 23 | ('buttonbox_hull_relief', 'raised', None), |
| 24 | ('buttonboxpos', 's', INITOPT), |
| 25 | ('buttons', ('OK',), self._buttons), |
| 26 | ('command', None, None), |
| 27 | ('dialogchildsite_borderwidth', 1, None), |
| 28 | ('dialogchildsite_relief', 'raised', None), |
| 29 | ('defaultbutton', None, self._defaultButton), |
| 30 | ('master', 'parent', None), |
| 31 | ('separatorwidth', 0, INITOPT), |
| 32 | ) |
| 33 | self.defineoptions(kw, optiondefs) |
| 34 | |
| 35 | # Initialise the base class (after defining the options). |
| 36 | Pmw.MegaToplevel.__init__(self, parent) |
| 37 | |
| 38 | # Create the components. |
| 39 | |
| 40 | oldInterior = Pmw.MegaToplevel.interior(self) |
| 41 | |
| 42 | # Set up pack options according to the position of the button box. |
| 43 | pos = self['buttonboxpos'] |
| 44 | if pos not in 'nsew': |
| 45 | raise ValueError, \ |
| 46 | 'bad buttonboxpos option "%s": should be n, s, e, or w' \ |
| 47 | % pos |
| 48 | |
| 49 | if pos in 'ns': |
| 50 | orient = 'horizontal' |
| 51 | fill = 'x' |
| 52 | if pos == 'n': |
| 53 | side = 'top' |
| 54 | else: |
| 55 | side = 'bottom' |
| 56 | else: |
| 57 | orient = 'vertical' |
| 58 | fill = 'y' |
| 59 | if pos == 'w': |
| 60 | side = 'left' |
| 61 | else: |
| 62 | side = 'right' |
| 63 | |
| 64 | # Create the button box. |
| 65 | self._buttonBox = self.createcomponent('buttonbox', |
| 66 | (), None, |
| 67 | Pmw.ButtonBox, (oldInterior,), orient = orient) |
| 68 | self._buttonBox.pack(side = side, fill = fill) |
| 69 | |
| 70 | # Create the separating line. |
| 71 | width = self['separatorwidth'] |
| 72 | if width > 0: |
| 73 | self._separator = self.createcomponent('separator', |
| 74 | (), None, |
| 75 | Tkinter.Frame, (oldInterior,), relief = 'sunken', |
| 76 | height = width, width = width, borderwidth = width / 2) |
| 77 | self._separator.pack(side = side, fill = fill) |
| 78 | |
| 79 | # Create the child site. |
| 80 | self.__dialogChildSite = self.createcomponent('dialogchildsite', |
| 81 | (), None, |
| 82 | Tkinter.Frame, (oldInterior,)) |
| 83 | self.__dialogChildSite.pack(side=side, fill='both', expand=1) |
| 84 | |
| 85 | self.oldButtons = () |
| 86 | self.oldDefault = None |
| 87 | |
| 88 | self.bind('<Return>', self._invokeDefault) |
| 89 | self.userdeletefunc(self._doCommand) |
| 90 | self.usermodaldeletefunc(self._doCommand) |
| 91 | |
| 92 | # Check keywords and initialise options. |
| 93 | self.initialiseoptions() |
| 94 | |
| 95 | def interior(self): |
| 96 | return self.__dialogChildSite |
| 97 | |
| 98 | def invoke(self, index = Pmw.DEFAULT): |
| 99 | return self._buttonBox.invoke(index) |
| 100 | |
| 101 | def _invokeDefault(self, event): |
| 102 | try: |
| 103 | self._buttonBox.index(Pmw.DEFAULT) |
| 104 | except ValueError: |
| 105 | return |
| 106 | self._buttonBox.invoke() |
| 107 | |
| 108 | def _doCommand(self, name = None): |
| 109 | if name is not None and self.active() and \ |
| 110 | Pmw.grabstacktopwindow() != self.component('hull'): |
| 111 | # This is a modal dialog but is not on the top of the grab |
| 112 | # stack (ie: should not have the grab), so ignore this |
| 113 | # event. This seems to be a bug in Tk and may occur in |
| 114 | # nested modal dialogs. |
| 115 | # |
| 116 | # An example is the PromptDialog demonstration. To |
| 117 | # trigger the problem, start the demo, then move the mouse |
| 118 | # to the main window, hit <TAB> and then <TAB> again. The |
| 119 | # highlight border of the "Show prompt dialog" button |
| 120 | # should now be displayed. Now hit <SPACE>, <RETURN>, |
| 121 | # <RETURN> rapidly several times. Eventually, hitting the |
| 122 | # return key invokes the password dialog "OK" button even |
| 123 | # though the confirm dialog is active (and therefore |
| 124 | # should have the keyboard focus). Observed under Solaris |
| 125 | # 2.5.1, python 1.5.2 and Tk8.0. |
| 126 | |
| 127 | # TODO: Give focus to the window on top of the grabstack. |
| 128 | return |
| 129 | |
| 130 | command = self['command'] |
| 131 | if callable(command): |
| 132 | return command(name) |
| 133 | else: |
| 134 | if self.active(): |
| 135 | self.deactivate(name) |
| 136 | else: |
| 137 | self.withdraw() |
| 138 | |
| 139 | def _buttons(self): |
| 140 | buttons = self['buttons'] |
| 141 | if type(buttons) != types.TupleType and type(buttons) != types.ListType: |
| 142 | raise ValueError, \ |
| 143 | 'bad buttons option "%s": should be a tuple' % str(buttons) |
| 144 | if self.oldButtons == buttons: |
| 145 | return |
| 146 | |
| 147 | self.oldButtons = buttons |
| 148 | |
| 149 | for index in range(self._buttonBox.numbuttons()): |
| 150 | self._buttonBox.delete(0) |
| 151 | for name in buttons: |
| 152 | self._buttonBox.add(name, |
| 153 | command=lambda self=self, name=name: self._doCommand(name)) |
| 154 | |
| 155 | if len(buttons) > 0: |
| 156 | defaultbutton = self['defaultbutton'] |
| 157 | if defaultbutton is None: |
| 158 | self._buttonBox.setdefault(None) |
| 159 | else: |
| 160 | try: |
| 161 | self._buttonBox.index(defaultbutton) |
| 162 | except ValueError: |
| 163 | pass |
| 164 | else: |
| 165 | self._buttonBox.setdefault(defaultbutton) |
| 166 | self._buttonBox.alignbuttons() |
| 167 | |
| 168 | def _defaultButton(self): |
| 169 | defaultbutton = self['defaultbutton'] |
| 170 | if self.oldDefault == defaultbutton: |
| 171 | return |
| 172 | |
| 173 | self.oldDefault = defaultbutton |
| 174 | |
| 175 | if len(self['buttons']) > 0: |
| 176 | if defaultbutton is None: |
| 177 | self._buttonBox.setdefault(None) |
| 178 | else: |
| 179 | try: |
| 180 | self._buttonBox.index(defaultbutton) |
| 181 | except ValueError: |
| 182 | pass |
| 183 | else: |
| 184 | self._buttonBox.setdefault(defaultbutton) |