Changeset 4166

Show
Ignore:
Timestamp:
06/19/08 18:10:44 (4 months ago)
Author:
ed
Message:

Removed the ReleaseOnEscape? property from dDialog. Added the CancelOnEscape? property to dStandardButtonDialog; when this is True (default), pressing the Escape key will be handled by the appropriate standard button handler.

Re-instituted the ButtonSizerPosition? property.

Deprecated onOK() and onCancel(). The methods that will be used by developers to react to standard buttons being clicked are now named run*; e.g., runOK(), runHelp().

Fixed a bug that has been around in dDialog for years. It used to be that pressing the Escape key destroyed the dialog if ReleaseOnEscape? was True; the code in the class would still try to access the now-destroyed object, throwing an error. Now dialogs are never destroyed in these classes; if you create them, you have to manually call their release() method.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/dabo/ui/uiwx/dDialog.py

    r4164 r4166  
    11# -*- coding: utf-8 -*- 
     2import warnings 
    23import wx 
    34import dabo 
     
    9697    def _onEscape(self, evt): 
    9798        evt.stop() 
    98         if self.ReleaseOnEscape: 
    99             self.release() 
    100             self.Close() 
     99        self.hide() 
    101100 
    102101 
     
    128127     
    129128     
    130     def _setEscapeBehavior(self): 
    131         """Allow subclasses to respond to changes in the ReleaseOnEscape property.""" 
    132         pass 
    133          
    134  
    135129    def _getAutoSize(self): 
    136130        return self._fit 
     
    164158     
    165159 
    166     def _getReleaseOnEscape(self): 
    167         try: 
    168             val = self._releaseOnEscape 
    169         except AttributeError: 
    170             val = True 
    171         return val 
    172  
    173     def _setReleaseOnEscape(self, val): 
    174         self._releaseOnEscape = bool(val) 
    175         self._setEscapeBehavior() 
    176  
    177  
    178160    def _getShowStat(self): 
    179161        # Dialogs cannot have status bars. 
     
    194176            "Determines if the dialog is shown modal (default) or modeless.  (bool)") 
    195177     
    196     ReleaseOnEscape = property(_getReleaseOnEscape, _setReleaseOnEscape, None, 
    197             "Determines if the <Esc> key releases the dialog. Default=True.  (bool)") 
    198  
    199178 
    200179    DynamicAutoSize = makeDynamicProperty(AutoSize) 
     
    218197    you must specify them all; in other words, OK is only assumed if nothing is specified. 
    219198    Then add your custom controls in the addControls() hook method, and respond to 
    220     the pressing of the standard buttons in the on*() handlers, where * is the name of the  
    221     associated property (e.g., onOK(), onNo(), etc.). You can query the Accepted property  
     199    the pressing of the standard buttons in the run*() handlers, where * is the name of the  
     200    associated property (e.g., runOK(), runOo(), etc.). You can query the Accepted property  
    222201    to find out if the user pressed "OK" or "Yes"; if neither of these was pressed,  
    223202    Accepted will be False. 
     
    229208        self._no = self._extractKey((properties, kwargs), "No") 
    230209        self._help = self._extractKey((properties, kwargs), "Help") 
     210        self._cancelOnEscape = True 
    231211        super(dStandardButtonDialog, self).__init__(parent=parent, properties=properties, *args, **kwargs) 
    232212        self._baseClass = dStandardButtonDialog 
     
    267247        # Initialize the button references 
    268248        self.btnOK = self.btnCancel = self.btnYes = self.btnNo = self.btnHelp = None 
    269         self.stdButtonSizer = sbs = self.CreateButtonSizer(flags) 
     249 
     250        # We need a Dabo sizer to wrap the wx sizer. 
     251        self.stdButtonSizer = dabo.ui.dSizer() 
     252        sbs = self.CreateButtonSizer(flags) 
     253        self.stdButtonSizer.append1x(sbs) 
     254 
    270255        btns = [b.GetWindow() for b in sbs.GetChildren() if b.IsWindow()] 
    271256        for btn in btns: 
     
    297282        for pos, btn in enumerate(buttons[1:]): 
    298283            btn.MoveAfterInTabOrder(buttons[pos-1]) 
    299         if cancel or no
     284        if self.CancelOnEscape
    300285            # The default Escape behavior destroys the dialog, so we need to replace 
    301286            # this with out own. 
     
    305290            elif no: 
    306291                self.bindKey("esc", self._onNo) 
     292            elif ok: 
     293                self.bindKey("esc", self._onOK) 
     294            elif yes: 
     295                self.bindKey("esc", self._onYes) 
     296                 
    307297 
    308298        # Let the user add their controls 
     
    310300 
    311301        # Just in case user changed Self.Sizer, update our reference: 
    312         bs = dabo.ui.dSizer("v") 
    313         bs.append((0, sz.DefaultBorder/2)) 
    314         bs.append(sbs, "x") 
    315         bs.append((0, sz.DefaultBorder)) 
    316         sz.append(bs, "x") 
     302        sz = self.Sizer 
     303        if self.ButtonSizerPosition is None: 
     304            # User code didn't add it, so we must. 
     305            bs = dabo.ui.dSizer("v") 
     306            bs.append((0, sz.DefaultBorder/2)) 
     307            bs.append(self.ButtonSizer, "x") 
     308            bs.append((0, sz.DefaultBorder)) 
     309            sz.append(bs, "x") 
     310        self.layout() 
    317311 
    318312    ################################################ 
    319     #  Handlers for the standard buttons 
     313    #    Handlers for the standard buttons.  
     314    ################################################ 
     315    # Note that onOK() and  
     316    # onCancel() are the names of the old event handlers, and  
     317    # code has been written to use these. So as not to break this 
     318    # older code, we issue a deprecation warning and call the 
     319    # old handler. 
    320320    def _onOK(self, evt): 
    321321        self.Accepted = True 
    322         self.onOK() 
     322        try: 
     323            self.onOK() 
     324        except TypeError: 
     325            warnings.warn(_("The onOK() handler is deprecated. Use the runOK() method instead"),  
     326                Warning) 
     327            self.onOK(None) 
     328        except AttributeError: 
     329            # New code should not have onOK 
     330            pass 
     331        self.runOK() 
    323332        self.EndModal(kons.DLG_OK) 
    324333    def _onCancel(self, evt): 
    325         self.onCancel() 
     334        try: 
     335            self.onCancel() 
     336        except TypeError: 
     337            warnings.warn(_("The onCancel() handler is deprecated. Use the runCancel() method instead"),  
     338                Warning) 
     339            self.onCancel(None) 
     340        except AttributeError: 
     341            # New code should not have onCancel 
     342            pass 
     343        self.runCancel() 
    326344        self.EndModal(kons.DLG_CANCEL) 
    327345    def _onYes(self, evt): 
    328346        self.Accepted = True 
    329         self.onYes() 
     347        self.runYes() 
    330348        self.EndModal(kons.DLG_YES) 
    331349    def _onNo(self, evt): 
    332         self.onNo() 
     350        self.runNo() 
    333351        self.EndModal(kons.DLG_NO) 
    334352    def _onHelp(self, evt): 
    335         self.onHelp() 
     353        self.runHelp() 
     354 
    336355    # The following are stub methods that can be overridden when needed. 
    337     def onOK(self): pass 
    338     def onCancel(self): pass 
    339     def onYes(self): pass 
    340     def onNo(self): pass 
    341     def onHelp(self): pass 
     356    def runOK(self): pass 
     357    def runCancel(self): pass 
     358    def runYes(self): pass 
     359    def runNo(self): pass 
     360    def runHelp(self): pass 
    342361    ################################################ 
    343362 
     
    389408 
    390409 
     410    def _getButtonSizerPosition(self): 
     411        return self.ButtonSizer.getPositionInSizer() 
     412 
     413 
    391414    def _getCancelButton(self): 
    392415        return self.btnCancel 
    393416 
    394417 
     418    def _getCancelOnEscape(self): 
     419        return self._cancelOnEscape 
     420 
     421    def _setCancelOnEscape(self, val): 
     422        if self._constructed(): 
     423            self._cancelOnEscape = val 
     424        else: 
     425            self._properties["CancelOnEscape"] = val 
     426 
     427 
    395428    def _getHelpButton(self): 
    396429        return self.btnHelp 
     
    415448            _("Returns a reference to the sizer controlling the Ok/Cancel buttons.  (dSizer)")) 
    416449 
     450    ButtonSizerPosition = property(_getButtonSizerPosition, None, None, 
     451            _("""Returns the position of the Ok/Cancel buttons in the sizer.  (int)""")) 
     452 
    417453    CancelButton = property(_getCancelButton, None, None, 
    418454            _("Reference to the Cancel button on the form, if present  (dButton or None).")) 
    419455     
     456    CancelOnEscape = property(_getCancelOnEscape, _setCancelOnEscape, None, 
     457            _("""When True (default), pressing the Escape key will perform the same action  
     458            as clicking the Cancel button. If no Cancel button is present but there is a No button,  
     459            the No behavior will be executed. If neither button is present, the default button's  
     460            action will be executed  (bool)""")) 
     461 
    420462    HelpButton = property(_getHelpButton, None, None, 
    421463            _("Reference to the Help button on the form, if present  (dButton or None).")) 
     
    438480        super(dOkCancelDialog, self).__init__(parent, properties, *args, **kwargs) 
    439481        self._baseClass = dOkCancelDialog 
    440          
    441482 
    442483class dYesNoDialog(dStandardButtonDialog): 
     
    448489 
    449490 
    450  
    451491if __name__ == "__main__": 
    452492    import test