controls/EnhancedStatusBar.py
changeset 1784 64beb9e9c749
parent 1756 08e4394ff4fb
child 1837 c507c363625e
equal deleted inserted replaced
1729:31e63e25b4cc 1784:64beb9e9c749
    14 # 1. Some Requests/Features To Add?
    14 # 1. Some Requests/Features To Add?
    15 #
    15 #
    16 #
    16 #
    17 # For All Kind Of Problems, Requests Of Enhancements And Bug Reports, Please
    17 # For All Kind Of Problems, Requests Of Enhancements And Bug Reports, Please
    18 # Write To Me At:
    18 # Write To Me At:
    19 # 
    19 #
    20 #
    20 #
    21 # andrea.gavana@agip.it
    21 # andrea.gavana@agip.it
    22 # andrea_gavan@tin.it
    22 # andrea_gavan@tin.it
    23 #
    23 #
    24 # Or, Obviously, To The wxPython Mailing List!!!
    24 # Or, Obviously, To The wxPython Mailing List!!!
    45      ESB_ALIGN_RIGHT;
    45      ESB_ALIGN_RIGHT;
    46    - varticalalignment: This Specify The Vertical Alignment For Your Widget,
    46    - varticalalignment: This Specify The Vertical Alignment For Your Widget,
    47      And Can Be ESB_EXACT_FIT, ESB_ALIGN_CENTER_VERTICAL, ESB_ALIGN_BOTTOM And
    47      And Can Be ESB_EXACT_FIT, ESB_ALIGN_CENTER_VERTICAL, ESB_ALIGN_BOTTOM And
    48      ESB_ALIGN_LEFT;
    48      ESB_ALIGN_LEFT;
    49 
    49 
    50 EnhancedStatusBar Is Freeware And Distributed Under The wxPython License. 
    50 EnhancedStatusBar Is Freeware And Distributed Under The wxPython License.
    51 
    51 
    52 Latest Revision: 21 September 2005, 19.57.20 GMT+2
    52 Latest Revision: 21 September 2005, 19.57.20 GMT+2
    53 Latest Revision before Latest Revision: 21 September 2005, 18.29.35 GMT+2
    53 Latest Revision before Latest Revision: 21 September 2005, 18.29.35 GMT+2
    54 Latest Revision before Latest Revision before Latest Revision: 31 May 2005, 23.17 CET
    54 Latest Revision before Latest Revision before Latest Revision: 31 May 2005, 23.17 CET
    55 
    55 
    76 # ---------------------------------------------------------------
    76 # ---------------------------------------------------------------
    77 # This Is The Main Class Implementation. See The Demo For Details
    77 # This Is The Main Class Implementation. See The Demo For Details
    78 # ---------------------------------------------------------------
    78 # ---------------------------------------------------------------
    79 class EnhancedStatusBarItem(object):
    79 class EnhancedStatusBarItem(object):
    80     def __init__(self, widget, pos, horizontalalignment=ESB_ALIGN_CENTER_HORIZONTAL, verticalalignment=ESB_ALIGN_CENTER_VERTICAL):
    80     def __init__(self, widget, pos, horizontalalignment=ESB_ALIGN_CENTER_HORIZONTAL, verticalalignment=ESB_ALIGN_CENTER_VERTICAL):
    81         self.__dict__.update( locals() )
    81         self.__dict__.update(locals())
       
    82 
    82 
    83 
    83 class EnhancedStatusBar(wx.StatusBar):
    84 class EnhancedStatusBar(wx.StatusBar):
    84 
    85 
    85     def __init__(self, parent, id=wx.ID_ANY, style=wx.ST_SIZEGRIP,
    86     def __init__(self, parent, id=wx.ID_ANY, style=wx.ST_SIZEGRIP,
    86                  name="EnhancedStatusBar"):
    87                  name="EnhancedStatusBar"):
    88 
    89 
    89         EnhancedStatusBar.__init__(self, parent, id=wx.ID_ANY,
    90         EnhancedStatusBar.__init__(self, parent, id=wx.ID_ANY,
    90                                    style=wx.ST_SIZEGRIP,
    91                                    style=wx.ST_SIZEGRIP,
    91                                    name="EnhancedStatusBar")
    92                                    name="EnhancedStatusBar")
    92         """
    93         """
    93         
    94 
    94         wx.StatusBar.__init__(self, parent, id, style, name)
    95         wx.StatusBar.__init__(self, parent, id, style, name)
    95         
    96 
    96         self._items = {}
    97         self._items = {}
    97         self._curPos = 0
    98         self._curPos = 0
    98         self._parent = parent
    99         self._parent = parent
    99         
   100 
   100         wx.EVT_SIZE(self, self.OnSize) 
   101         wx.EVT_SIZE(self, self.OnSize)
   101         wx.CallAfter(self.OnSize, None)
   102         wx.CallAfter(self.OnSize, None)
   102 
   103 
   103             
       
   104     def OnSize(self, event):
   104     def OnSize(self, event):
   105         """Handles The wx.EVT_SIZE Events For The StatusBar.
   105         """Handles The wx.EVT_SIZE Events For The StatusBar.
   106 
   106 
   107         Actually, All The Calculations Linked To HorizontalAlignment And
   107         Actually, All The Calculations Linked To HorizontalAlignment And
   108         VerticalAlignment Are Done In This Function."""
   108         VerticalAlignment Are Done In This Function."""
   109 
   109 
   110         for pos, item in self._items.items():
   110         for pos, item in self._items.items():
   111             widget, horizontalalignment, verticalalignment = item.widget, item.horizontalalignment, item.verticalalignment
   111             widget, horizontalalignment, verticalalignment = item.widget, item.horizontalalignment, item.verticalalignment
   112            
   112 
   113             rect = self.GetFieldRect(pos)
   113             rect = self.GetFieldRect(pos)
   114             widgetpos = widget.GetPosition()
   114             widgetpos = widget.GetPosition()
   115             widgetsize = widget.GetSize()
   115             widgetsize = widget.GetSize()
   116 
   116 
   117             rect = self.GetFieldRect(pos)
   117             rect = self.GetFieldRect(pos)
   118             
   118 
   119             if horizontalalignment == ESB_EXACT_FIT:
   119             if horizontalalignment == ESB_EXACT_FIT:
   120                 
   120 
   121                 if verticalalignment == ESB_EXACT_FIT:
   121                 if verticalalignment == ESB_EXACT_FIT:
   122                     """ 1 September 2015 Fix fit align """
   122                     """ 1 September 2015 Fix fit align """
   123                     widget.SetSize((rect.width-4, rect.height-4))
   123                     widget.SetSize((rect.width-4, rect.height-4))
   124                     widget.SetPosition((rect.x+2, rect.y+2))
   124                     widget.SetPosition((rect.x+2, rect.y+2))
   125                 elif verticalalignment == ESB_ALIGN_CENTER_VERTICAL:
   125                 elif verticalalignment == ESB_ALIGN_CENTER_VERTICAL:
   136                 elif verticalalignment == ESB_ALIGN_BOTTOM:
   136                 elif verticalalignment == ESB_ALIGN_BOTTOM:
   137                     widget.SetSize((rect.width-2, widgetsize[1]))
   137                     widget.SetSize((rect.width-2, widgetsize[1]))
   138                     widget.SetPosition((rect.x-1, rect.height-widgetsize[1]))
   138                     widget.SetPosition((rect.x-1, rect.height-widgetsize[1]))
   139 
   139 
   140             elif horizontalalignment == ESB_ALIGN_LEFT:
   140             elif horizontalalignment == ESB_ALIGN_LEFT:
   141                 
   141 
   142                 xpos = rect.x - 1
   142                 xpos = rect.x - 1
   143                 if verticalalignment == ESB_EXACT_FIT:
   143                 if verticalalignment == ESB_EXACT_FIT:
   144                     widget.SetSize((widgetsize[0], rect.height-2))
   144                     widget.SetSize((widgetsize[0], rect.height-2))
   145                     widget.SetPosition((xpos, rect.y-1))
   145                     widget.SetPosition((xpos, rect.y-1))
   146                 elif verticalalignment == ESB_ALIGN_CENTER_VERTICAL:
   146                 elif verticalalignment == ESB_ALIGN_CENTER_VERTICAL:
   152                         widget.SetPosition((xpos, rect.y-1))
   152                         widget.SetPosition((xpos, rect.y-1))
   153                 elif verticalalignment == ESB_ALIGN_TOP:
   153                 elif verticalalignment == ESB_ALIGN_TOP:
   154                     widget.SetPosition((xpos, rect.y))
   154                     widget.SetPosition((xpos, rect.y))
   155                 elif verticalalignment == ESB_ALIGN_BOTTOM:
   155                 elif verticalalignment == ESB_ALIGN_BOTTOM:
   156                     widget.SetPosition((xpos, rect.height-widgetsize[1]))
   156                     widget.SetPosition((xpos, rect.height-widgetsize[1]))
   157                 
   157 
   158             elif horizontalalignment == ESB_ALIGN_RIGHT:
   158             elif horizontalalignment == ESB_ALIGN_RIGHT:
   159                 
   159 
   160                 xpos = rect.x + rect.width - widgetsize[0] - 1
   160                 xpos = rect.x + rect.width - widgetsize[0] - 1
   161                 if verticalalignment == ESB_EXACT_FIT:
   161                 if verticalalignment == ESB_EXACT_FIT:
   162                     widget.SetSize((widgetsize[0], rect.height-2))
   162                     widget.SetSize((widgetsize[0], rect.height-2))
   163                     widget.SetPosition((xpos, rect.y-1))
   163                     widget.SetPosition((xpos, rect.y-1))
   164                 elif verticalalignment == ESB_ALIGN_CENTER_VERTICAL:
   164                 elif verticalalignment == ESB_ALIGN_CENTER_VERTICAL:
   172                     widget.SetPosition((xpos, rect.y))
   172                     widget.SetPosition((xpos, rect.y))
   173                 elif verticalalignment == ESB_ALIGN_BOTTOM:
   173                 elif verticalalignment == ESB_ALIGN_BOTTOM:
   174                     widget.SetPosition((xpos, rect.height-widgetsize[1]))
   174                     widget.SetPosition((xpos, rect.height-widgetsize[1]))
   175 
   175 
   176             elif horizontalalignment == ESB_ALIGN_CENTER_HORIZONTAL:
   176             elif horizontalalignment == ESB_ALIGN_CENTER_HORIZONTAL:
   177                 
   177 
   178                 xpos = rect.x + (rect.width - widgetsize[0])/2 - 1
   178                 xpos = rect.x + (rect.width - widgetsize[0])/2 - 1
   179                 if verticalalignment == ESB_EXACT_FIT:
   179                 if verticalalignment == ESB_EXACT_FIT:
   180                     widget.SetSize((widgetsize[0], rect.height))
   180                     widget.SetSize((widgetsize[0], rect.height))
   181                     widget.SetPosition((xpos, rect.y))
   181                     widget.SetPosition((xpos, rect.y))
   182                 elif verticalalignment == ESB_ALIGN_CENTER_VERTICAL:
   182                 elif verticalalignment == ESB_ALIGN_CENTER_VERTICAL:
   189                 elif verticalalignment == ESB_ALIGN_TOP:
   189                 elif verticalalignment == ESB_ALIGN_TOP:
   190                     widget.SetPosition((xpos, rect.y))
   190                     widget.SetPosition((xpos, rect.y))
   191                 elif verticalalignment == ESB_ALIGN_BOTTOM:
   191                 elif verticalalignment == ESB_ALIGN_BOTTOM:
   192                     widget.SetPosition((xpos, rect.height-widgetsize[1]))
   192                     widget.SetPosition((xpos, rect.height-widgetsize[1]))
   193 
   193 
   194                 
       
   195         if event is not None:
   194         if event is not None:
   196             event.Skip()
   195             event.Skip()
   197         
   196 
   198         
       
   199     def AddWidget(self, widget, horizontalalignment=ESB_ALIGN_CENTER_HORIZONTAL,
   197     def AddWidget(self, widget, horizontalalignment=ESB_ALIGN_CENTER_HORIZONTAL,
   200                   verticalalignment=ESB_ALIGN_CENTER_VERTICAL, pos = -1):
   198                   verticalalignment=ESB_ALIGN_CENTER_VERTICAL, pos=-1):
   201         """Add A Widget To The EnhancedStatusBar.
   199         """Add A Widget To The EnhancedStatusBar.
   202 
   200 
   203         Parameters:
   201         Parameters:
   204 
   202 
   205         - horizontalalignment: This Can Be One Of:
   203         - horizontalalignment: This Can Be One Of:
   219         """
   217         """
   220 
   218 
   221         if pos == -1:
   219         if pos == -1:
   222             pos = self._curPos
   220             pos = self._curPos
   223             self._curPos += 1
   221             self._curPos += 1
   224         
   222 
   225         if self.GetFieldsCount() <= pos:
   223         if self.GetFieldsCount() <= pos:
   226             raise "\nERROR: EnhancedStatusBar has a max of %d items, you tried to set item #%d" % (self.GetFieldsCount(), pos)
   224             raise "\nERROR: EnhancedStatusBar has a max of %d items, you tried to set item #%d" % (self.GetFieldsCount(), pos)
   227 
   225 
   228         if horizontalalignment not in [ESB_ALIGN_CENTER_HORIZONTAL, ESB_EXACT_FIT,
   226         if horizontalalignment not in [ESB_ALIGN_CENTER_HORIZONTAL, ESB_EXACT_FIT,
   229                                        ESB_ALIGN_LEFT, ESB_ALIGN_RIGHT]:
   227                                        ESB_ALIGN_LEFT, ESB_ALIGN_RIGHT]:
   234         if verticalalignment not in [ESB_ALIGN_CENTER_VERTICAL, ESB_EXACT_FIT,
   232         if verticalalignment not in [ESB_ALIGN_CENTER_VERTICAL, ESB_EXACT_FIT,
   235                                      ESB_ALIGN_TOP, ESB_ALIGN_BOTTOM]:
   233                                      ESB_ALIGN_TOP, ESB_ALIGN_BOTTOM]:
   236             raise '\nERROR: Parameter "verticalalignment" Should Be One Of '\
   234             raise '\nERROR: Parameter "verticalalignment" Should Be One Of '\
   237                   '"ESB_ALIGN_CENTER_VERTICAL", "ESB_ALIGN_TOP", "ESB_ALIGN_BOTTOM"' \
   235                   '"ESB_ALIGN_CENTER_VERTICAL", "ESB_ALIGN_TOP", "ESB_ALIGN_BOTTOM"' \
   238                   '"ESB_EXACT_FIT"'
   236                   '"ESB_EXACT_FIT"'
   239         
       
   240 
   237 
   241         try:
   238         try:
   242             self.RemoveChild(self._items[pos].widget)
   239             self.RemoveChild(self._items[pos].widget)
   243             self._items[pos].widget.Destroy()
   240             self._items[pos].widget.Destroy()
   244         except KeyError: pass
   241         except KeyError:
   245         
   242             pass
       
   243 
   246         self._items[pos] = EnhancedStatusBarItem(widget, pos, horizontalalignment, verticalalignment)
   244         self._items[pos] = EnhancedStatusBarItem(widget, pos, horizontalalignment, verticalalignment)
   247         
   245 
   248         wx.CallAfter(self.OnSize, None)
   246         wx.CallAfter(self.OnSize, None)