EnhancedStatusBar.py
changeset 1583 d91356480df9
parent 1582 dee51e22a9aa
child 1584 431f4ef34bde
equal deleted inserted replaced
1582:dee51e22a9aa 1583:d91356480df9
     1 # --------------------------------------------------------------------------- #
       
     2 # ENHANCEDSTATUSBAR wxPython IMPLEMENTATION
       
     3 # Python Code By:
       
     4 #
       
     5 # Andrea Gavana, @ 31 May 2005
       
     6 # Nitro, @ 21 September 2005
       
     7 # Latest Revision: 21 September 2005, 19.57.20 GMT+2
       
     8 # Latest Revision before Latest Revision: 21 September 2005, 18.29.35 GMT+2
       
     9 # Latest Revision before Latest Revision before Latest Revision: 31 May 2005, 23.17 CET
       
    10 #
       
    11 #
       
    12 # TODO List/Caveats
       
    13 #
       
    14 # 1. Some Requests/Features To Add?
       
    15 #
       
    16 #
       
    17 # For All Kind Of Problems, Requests Of Enhancements And Bug Reports, Please
       
    18 # Write To Me At:
       
    19 # 
       
    20 #
       
    21 # andrea.gavana@agip.it
       
    22 # andrea_gavan@tin.it
       
    23 #
       
    24 # Or, Obviously, To The wxPython Mailing List!!!
       
    25 #
       
    26 #
       
    27 # licensed under wxWidgets License (GPL compatible)
       
    28 # End Of Comments
       
    29 # --------------------------------------------------------------------------- #
       
    30 
       
    31 """ Description:
       
    32 
       
    33 EnhancedStatusBar Is A Slight Modification (Actually A Subclassing) Of wx.StatusBar.
       
    34 It Allows You To Add Almost Any Widget You Like To The wx.StatusBar Of Your Main
       
    35 Frame Application And Also To Layout Them Properly.
       
    36 
       
    37 
       
    38 What It Can Do:
       
    39 
       
    40 1) Almost All The Functionalities Of wx.StatusBar Are Still Present;
       
    41 2) You Can Add Different Kind Of Widgets Into Every Field Of The EnhancedStatusBar;
       
    42 3) The AddWidget() Methods Accepts 2 Layout Inputs:
       
    43    - horizontalalignment: This Specify The Horizontal Alignment For Your Widget,
       
    44      And Can Be ESB_EXACT_FIT, ESB_ALIGN_CENTER_HORIZONTAL, ESB_ALIGN_LEFT And
       
    45      ESB_ALIGN_RIGHT;
       
    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
       
    48      ESB_ALIGN_LEFT;
       
    49 
       
    50 EnhancedStatusBar Is Freeware And Distributed Under The wxPython License. 
       
    51 
       
    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
       
    54 Latest Revision before Latest Revision before Latest Revision: 31 May 2005, 23.17 CET
       
    55 
       
    56 """
       
    57 
       
    58 import wx
       
    59 
       
    60 # Horizontal Alignment Constants
       
    61 ESB_ALIGN_CENTER_VERTICAL = 1
       
    62 ESB_ALIGN_TOP = 2
       
    63 ESB_ALIGN_BOTTOM = 3
       
    64 
       
    65 # Vertical Alignment Constants
       
    66 ESB_ALIGN_CENTER_HORIZONTAL = 11
       
    67 ESB_ALIGN_LEFT = 12
       
    68 ESB_ALIGN_RIGHT = 13
       
    69 
       
    70 # Exact Fit (Either Horizontal Or Vertical Or Both) Constant
       
    71 ESB_EXACT_FIT = 20
       
    72 
       
    73 
       
    74 # ---------------------------------------------------------------
       
    75 # Class EnhancedStatusBar
       
    76 # ---------------------------------------------------------------
       
    77 # This Is The Main Class Implementation. See The Demo For Details
       
    78 # ---------------------------------------------------------------
       
    79 class EnhancedStatusBarItem(object):
       
    80     def __init__(self, widget, pos, horizontalalignment=ESB_ALIGN_CENTER_HORIZONTAL, verticalalignment=ESB_ALIGN_CENTER_VERTICAL):
       
    81         self.__dict__.update( locals() )
       
    82 
       
    83 class EnhancedStatusBar(wx.StatusBar):
       
    84 
       
    85     def __init__(self, parent, id=wx.ID_ANY, style=wx.ST_SIZEGRIP,
       
    86                  name="EnhancedStatusBar"):
       
    87         """Default Class Constructor.
       
    88 
       
    89         EnhancedStatusBar.__init__(self, parent, id=wx.ID_ANY,
       
    90                                    style=wx.ST_SIZEGRIP,
       
    91                                    name="EnhancedStatusBar")
       
    92         """
       
    93         
       
    94         wx.StatusBar.__init__(self, parent, id, style, name)
       
    95         
       
    96         self._items = {}
       
    97         self._curPos = 0
       
    98         self._parent = parent
       
    99         
       
   100         wx.EVT_SIZE(self, self.OnSize) 
       
   101         wx.CallAfter(self.OnSize, None)
       
   102 
       
   103             
       
   104     def OnSize(self, event):
       
   105         """Handles The wx.EVT_SIZE Events For The StatusBar.
       
   106 
       
   107         Actually, All The Calculations Linked To HorizontalAlignment And
       
   108         VerticalAlignment Are Done In This Function."""
       
   109 
       
   110         for pos, item in self._items.items():
       
   111             widget, horizontalalignment, verticalalignment = item.widget, item.horizontalalignment, item.verticalalignment
       
   112            
       
   113             rect = self.GetFieldRect(pos)
       
   114             widgetpos = widget.GetPosition()
       
   115             widgetsize = widget.GetSize()
       
   116 
       
   117             rect = self.GetFieldRect(pos)
       
   118             
       
   119             if horizontalalignment == ESB_EXACT_FIT:
       
   120                 
       
   121                 if verticalalignment == ESB_EXACT_FIT:
       
   122                     """ 1 September 2015 Fix fit align """
       
   123                     widget.SetSize((rect.width-4, rect.height-4))
       
   124                     widget.SetPosition((rect.x+2, rect.y+2))
       
   125                 elif verticalalignment == ESB_ALIGN_CENTER_VERTICAL:
       
   126                     if widgetsize[1] < rect.width - 1:
       
   127                         diffs = (rect.height - widgetsize[1])/2
       
   128                         widget.SetSize((rect.width-2, widgetsize[1]))
       
   129                         widget.SetPosition((rect.x-1, rect.y+diffs))
       
   130                     else:
       
   131                         widget.SetSize((rect.width-2, widgetsize[1]))
       
   132                         widget.SetPosition((rect.x-1, rect.y-1))
       
   133                 elif verticalalignment == ESB_ALIGN_TOP:
       
   134                     widget.SetSize((rect.width-2, widgetsize[1]))
       
   135                     widget.SetPosition((rect.x-1, rect.y))
       
   136                 elif verticalalignment == ESB_ALIGN_BOTTOM:
       
   137                     widget.SetSize((rect.width-2, widgetsize[1]))
       
   138                     widget.SetPosition((rect.x-1, rect.height-widgetsize[1]))
       
   139 
       
   140             elif horizontalalignment == ESB_ALIGN_LEFT:
       
   141                 
       
   142                 xpos = rect.x - 1
       
   143                 if verticalalignment == ESB_EXACT_FIT:
       
   144                     widget.SetSize((widgetsize[0], rect.height-2))
       
   145                     widget.SetPosition((xpos, rect.y-1))
       
   146                 elif verticalalignment == ESB_ALIGN_CENTER_VERTICAL:
       
   147                     if widgetsize[1] < rect.height - 1:
       
   148                         diffs = (rect.height - widgetsize[1])/2
       
   149                         widget.SetPosition((xpos, rect.y+diffs))
       
   150                     else:
       
   151                         widget.SetSize((widgetsize[0], rect.height-2))
       
   152                         widget.SetPosition((xpos, rect.y-1))
       
   153                 elif verticalalignment == ESB_ALIGN_TOP:
       
   154                     widget.SetPosition((xpos, rect.y))
       
   155                 elif verticalalignment == ESB_ALIGN_BOTTOM:
       
   156                     widget.SetPosition((xpos, rect.height-widgetsize[1]))
       
   157                 
       
   158             elif horizontalalignment == ESB_ALIGN_RIGHT:
       
   159                 
       
   160                 xpos = rect.x + rect.width - widgetsize[0] - 1
       
   161                 if verticalalignment == ESB_EXACT_FIT:
       
   162                     widget.SetSize((widgetsize[0], rect.height-2))
       
   163                     widget.SetPosition((xpos, rect.y-1))
       
   164                 elif verticalalignment == ESB_ALIGN_CENTER_VERTICAL:
       
   165                     if widgetsize[1] < rect.height - 1:
       
   166                         diffs = (rect.height - widgetsize[1])/2
       
   167                         widget.SetPosition((xpos, rect.y+diffs))
       
   168                     else:
       
   169                         widget.SetSize((widgetsize[0], rect.height-2))
       
   170                         widget.SetPosition((xpos, rect.y-1))
       
   171                 elif verticalalignment == ESB_ALIGN_TOP:
       
   172                     widget.SetPosition((xpos, rect.y))
       
   173                 elif verticalalignment == ESB_ALIGN_BOTTOM:
       
   174                     widget.SetPosition((xpos, rect.height-widgetsize[1]))
       
   175 
       
   176             elif horizontalalignment == ESB_ALIGN_CENTER_HORIZONTAL:
       
   177                 
       
   178                 xpos = rect.x + (rect.width - widgetsize[0])/2 - 1
       
   179                 if verticalalignment == ESB_EXACT_FIT:
       
   180                     widget.SetSize((widgetsize[0], rect.height))
       
   181                     widget.SetPosition((xpos, rect.y))
       
   182                 elif verticalalignment == ESB_ALIGN_CENTER_VERTICAL:
       
   183                     if widgetsize[1] < rect.height - 1:
       
   184                         diffs = (rect.height - widgetsize[1])/2
       
   185                         widget.SetPosition((xpos, rect.y+diffs))
       
   186                     else:
       
   187                         widget.SetSize((widgetsize[0], rect.height-1))
       
   188                         widget.SetPosition((xpos, rect.y+1))
       
   189                 elif verticalalignment == ESB_ALIGN_TOP:
       
   190                     widget.SetPosition((xpos, rect.y))
       
   191                 elif verticalalignment == ESB_ALIGN_BOTTOM:
       
   192                     widget.SetPosition((xpos, rect.height-widgetsize[1]))
       
   193 
       
   194                 
       
   195         if event is not None:
       
   196             event.Skip()
       
   197         
       
   198         
       
   199     def AddWidget(self, widget, horizontalalignment=ESB_ALIGN_CENTER_HORIZONTAL,
       
   200                   verticalalignment=ESB_ALIGN_CENTER_VERTICAL, pos = -1):
       
   201         """Add A Widget To The EnhancedStatusBar.
       
   202 
       
   203         Parameters:
       
   204 
       
   205         - horizontalalignment: This Can Be One Of:
       
   206           a) ESB_EXACT_FIT: The Widget Will Fit Horizontally The StatusBar Field Width;
       
   207           b) ESB_ALIGN_CENTER_HORIZONTAL: The Widget Will Be Centered Horizontally In
       
   208              The StatusBar Field;
       
   209           c) ESB_ALIGN_LEFT: The Widget Will Be Left Aligned In The StatusBar Field;
       
   210           d) ESB_ALIGN_RIGHT: The Widget Will Be Right Aligned In The StatusBar Field;
       
   211 
       
   212         - verticalalignment:
       
   213           a) ESB_EXACT_FIT: The Widget Will Fit Vertically The StatusBar Field Height;
       
   214           b) ESB_ALIGN_CENTER_VERTICAL: The Widget Will Be Centered Vertically In
       
   215              The StatusBar Field;
       
   216           c) ESB_ALIGN_BOTTOM: The Widget Will Be Bottom Aligned In The StatusBar Field;
       
   217           d) ESB_ALIGN_TOP: The Widget Will Be TOP Aligned In The StatusBar Field;
       
   218 
       
   219         """
       
   220 
       
   221         if pos == -1:
       
   222             pos = self._curPos
       
   223             self._curPos += 1
       
   224         
       
   225         if self.GetFieldsCount() <= pos:
       
   226             raise "\nERROR: EnhancedStatusBar has a max of %d items, you tried to set item #%d" % (self.GetFieldsCount(), pos)
       
   227 
       
   228         if horizontalalignment not in [ESB_ALIGN_CENTER_HORIZONTAL, ESB_EXACT_FIT,
       
   229                                        ESB_ALIGN_LEFT, ESB_ALIGN_RIGHT]:
       
   230             raise '\nERROR: Parameter "horizontalalignment" Should Be One Of '\
       
   231                   '"ESB_ALIGN_CENTER_HORIZONTAL", "ESB_ALIGN_LEFT", "ESB_ALIGN_RIGHT"' \
       
   232                   '"ESB_EXACT_FIT"'
       
   233 
       
   234         if verticalalignment not in [ESB_ALIGN_CENTER_VERTICAL, ESB_EXACT_FIT,
       
   235                                      ESB_ALIGN_TOP, ESB_ALIGN_BOTTOM]:
       
   236             raise '\nERROR: Parameter "verticalalignment" Should Be One Of '\
       
   237                   '"ESB_ALIGN_CENTER_VERTICAL", "ESB_ALIGN_TOP", "ESB_ALIGN_BOTTOM"' \
       
   238                   '"ESB_EXACT_FIT"'
       
   239         
       
   240 
       
   241         try:
       
   242             self.RemoveChild(self._items[pos].widget)
       
   243             self._items[pos].widget.Destroy()
       
   244         except KeyError: pass
       
   245         
       
   246         self._items[pos] = EnhancedStatusBarItem(widget, pos, horizontalalignment, verticalalignment)
       
   247         
       
   248         wx.CallAfter(self.OnSize, None)