andrej@1731: # --------------------------------------------------------------------------- #
andrej@1731: # ENHANCEDSTATUSBAR wxPython IMPLEMENTATION
andrej@1731: # Python Code By:
andrej@1731: #
andrej@1731: # Andrea Gavana, @ 31 May 2005
andrej@1731: # Nitro, @ 21 September 2005
andrej@1731: # Latest Revision: 21 September 2005, 19.57.20 GMT+2
andrej@1731: # Latest Revision before Latest Revision: 21 September 2005, 18.29.35 GMT+2
andrej@1731: # Latest Revision before Latest Revision before Latest Revision: 31 May 2005, 23.17 CET
andrej@1731: #
andrej@1731: #
andrej@1731: # TODO List/Caveats
andrej@1731: #
andrej@1731: # 1. Some Requests/Features To Add?
andrej@1731: #
andrej@1731: #
andrej@1731: # For All Kind Of Problems, Requests Of Enhancements And Bug Reports, Please
andrej@1731: # Write To Me At:
andrej@1731: #
andrej@1731: #
andrej@1731: # andrea.gavana@agip.it
andrej@1731: # andrea_gavan@tin.it
andrej@1731: #
andrej@1731: # Or, Obviously, To The wxPython Mailing List!!!
andrej@1731: #
andrej@1731: #
andrej@1731: # licensed under wxWidgets License (GPL compatible)
andrej@1731: # End Of Comments
andrej@1731: # --------------------------------------------------------------------------- #
andrej@1731: 
andrej@1731: """ Description:
andrej@1731: 
andrej@1731: EnhancedStatusBar Is A Slight Modification (Actually A Subclassing) Of wx.StatusBar.
andrej@1731: It Allows You To Add Almost Any Widget You Like To The wx.StatusBar Of Your Main
andrej@1731: Frame Application And Also To Layout Them Properly.
andrej@1731: 
andrej@1731: 
andrej@1731: What It Can Do:
andrej@1731: 
andrej@1731: 1) Almost All The Functionalities Of wx.StatusBar Are Still Present;
andrej@1731: 2) You Can Add Different Kind Of Widgets Into Every Field Of The EnhancedStatusBar;
andrej@1731: 3) The AddWidget() Methods Accepts 2 Layout Inputs:
andrej@1731:    - horizontalalignment: This Specify The Horizontal Alignment For Your Widget,
andrej@1731:      And Can Be ESB_EXACT_FIT, ESB_ALIGN_CENTER_HORIZONTAL, ESB_ALIGN_LEFT And
andrej@1731:      ESB_ALIGN_RIGHT;
andrej@1731:    - varticalalignment: This Specify The Vertical Alignment For Your Widget,
andrej@1731:      And Can Be ESB_EXACT_FIT, ESB_ALIGN_CENTER_VERTICAL, ESB_ALIGN_BOTTOM And
andrej@1731:      ESB_ALIGN_LEFT;
andrej@1731: 
andrej@1731: EnhancedStatusBar Is Freeware And Distributed Under The wxPython License.
andrej@1731: 
andrej@1731: Latest Revision: 21 September 2005, 19.57.20 GMT+2
andrej@1731: Latest Revision before Latest Revision: 21 September 2005, 18.29.35 GMT+2
andrej@1731: Latest Revision before Latest Revision before Latest Revision: 31 May 2005, 23.17 CET
andrej@1731: 
andrej@1731: """
andrej@1731: 
andrej@1881: from __future__ import absolute_import
andrej@2437: from __future__ import division
andrej@1731: import wx
andrej@1731: 
andrej@1731: # Horizontal Alignment Constants
andrej@1731: ESB_ALIGN_CENTER_VERTICAL = 1
andrej@1731: ESB_ALIGN_TOP = 2
andrej@1731: ESB_ALIGN_BOTTOM = 3
andrej@1731: 
andrej@1731: # Vertical Alignment Constants
andrej@1731: ESB_ALIGN_CENTER_HORIZONTAL = 11
andrej@1731: ESB_ALIGN_LEFT = 12
andrej@1731: ESB_ALIGN_RIGHT = 13
andrej@1731: 
andrej@1731: # Exact Fit (Either Horizontal Or Vertical Or Both) Constant
andrej@1731: ESB_EXACT_FIT = 20
andrej@1731: 
andrej@1731: 
andrej@1731: # ---------------------------------------------------------------
andrej@1731: # Class EnhancedStatusBar
andrej@1731: # ---------------------------------------------------------------
andrej@1731: # This Is The Main Class Implementation. See The Demo For Details
andrej@1731: # ---------------------------------------------------------------
andrej@1731: class EnhancedStatusBarItem(object):
andrej@1731:     def __init__(self, widget, pos, horizontalalignment=ESB_ALIGN_CENTER_HORIZONTAL, verticalalignment=ESB_ALIGN_CENTER_VERTICAL):
andrej@1746:         self.__dict__.update(locals())
andrej@1731: 
andrej@1736: 
andrej@1731: class EnhancedStatusBar(wx.StatusBar):
andrej@1731: 
andrej@1731:     def __init__(self, parent, id=wx.ID_ANY, style=wx.ST_SIZEGRIP,
andrej@1731:                  name="EnhancedStatusBar"):
andrej@1731:         """Default Class Constructor.
andrej@1731: 
andrej@1731:         EnhancedStatusBar.__init__(self, parent, id=wx.ID_ANY,
andrej@1731:                                    style=wx.ST_SIZEGRIP,
andrej@1731:                                    name="EnhancedStatusBar")
andrej@1731:         """
andrej@1731: 
andrej@1731:         wx.StatusBar.__init__(self, parent, id, style, name)
andrej@1731: 
andrej@1731:         self._items = {}
andrej@1731:         self._curPos = 0
andrej@1731:         self._parent = parent
andrej@1731: 
andrej@1731:         wx.EVT_SIZE(self, self.OnSize)
andrej@1731:         wx.CallAfter(self.OnSize, None)
andrej@1731: 
andrej@1731:     def OnSize(self, event):
andrej@1731:         """Handles The wx.EVT_SIZE Events For The StatusBar.
andrej@1731: 
andrej@1731:         Actually, All The Calculations Linked To HorizontalAlignment And
andrej@1731:         VerticalAlignment Are Done In This Function."""
andrej@1731: 
andrej@1731:         for pos, item in self._items.items():
andrej@1731:             widget, horizontalalignment, verticalalignment = item.widget, item.horizontalalignment, item.verticalalignment
andrej@1731: 
andrej@1731:             rect = self.GetFieldRect(pos)
andrej@1731:             widgetsize = widget.GetSize()
andrej@1731: 
andrej@1731:             rect = self.GetFieldRect(pos)
andrej@1731: 
andrej@1731:             if horizontalalignment == ESB_EXACT_FIT:
andrej@1731: 
andrej@1731:                 if verticalalignment == ESB_EXACT_FIT:
andrej@1837:                     # 1 September 2015 Fix fit align
andrej@1731:                     widget.SetSize((rect.width-4, rect.height-4))
andrej@1731:                     widget.SetPosition((rect.x+2, rect.y+2))
andrej@1731:                 elif verticalalignment == ESB_ALIGN_CENTER_VERTICAL:
andrej@1731:                     if widgetsize[1] < rect.width - 1:
andrej@1731:                         diffs = (rect.height - widgetsize[1])/2
andrej@1731:                         widget.SetSize((rect.width-2, widgetsize[1]))
andrej@1731:                         widget.SetPosition((rect.x-1, rect.y+diffs))
andrej@1731:                     else:
andrej@1731:                         widget.SetSize((rect.width-2, widgetsize[1]))
andrej@1731:                         widget.SetPosition((rect.x-1, rect.y-1))
andrej@1731:                 elif verticalalignment == ESB_ALIGN_TOP:
andrej@1731:                     widget.SetSize((rect.width-2, widgetsize[1]))
andrej@1731:                     widget.SetPosition((rect.x-1, rect.y))
andrej@1731:                 elif verticalalignment == ESB_ALIGN_BOTTOM:
andrej@1731:                     widget.SetSize((rect.width-2, widgetsize[1]))
andrej@1731:                     widget.SetPosition((rect.x-1, rect.height-widgetsize[1]))
andrej@1731: 
andrej@1731:             elif horizontalalignment == ESB_ALIGN_LEFT:
andrej@1731: 
andrej@1731:                 xpos = rect.x - 1
andrej@1731:                 if verticalalignment == ESB_EXACT_FIT:
andrej@1731:                     widget.SetSize((widgetsize[0], rect.height-2))
andrej@1731:                     widget.SetPosition((xpos, rect.y-1))
andrej@1731:                 elif verticalalignment == ESB_ALIGN_CENTER_VERTICAL:
andrej@1731:                     if widgetsize[1] < rect.height - 1:
andrej@2437:                         diffs = (rect.height - widgetsize[1]) // 2
andrej@1731:                         widget.SetPosition((xpos, rect.y+diffs))
andrej@1731:                     else:
andrej@1731:                         widget.SetSize((widgetsize[0], rect.height-2))
andrej@1731:                         widget.SetPosition((xpos, rect.y-1))
andrej@1731:                 elif verticalalignment == ESB_ALIGN_TOP:
andrej@1731:                     widget.SetPosition((xpos, rect.y))
andrej@1731:                 elif verticalalignment == ESB_ALIGN_BOTTOM:
andrej@1731:                     widget.SetPosition((xpos, rect.height-widgetsize[1]))
andrej@1731: 
andrej@1731:             elif horizontalalignment == ESB_ALIGN_RIGHT:
andrej@1731: 
andrej@1731:                 xpos = rect.x + rect.width - widgetsize[0] - 1
andrej@1731:                 if verticalalignment == ESB_EXACT_FIT:
andrej@1731:                     widget.SetSize((widgetsize[0], rect.height-2))
andrej@1731:                     widget.SetPosition((xpos, rect.y-1))
andrej@1731:                 elif verticalalignment == ESB_ALIGN_CENTER_VERTICAL:
andrej@1731:                     if widgetsize[1] < rect.height - 1:
andrej@2437:                         diffs = (rect.height - widgetsize[1]) // 2
andrej@1731:                         widget.SetPosition((xpos, rect.y+diffs))
andrej@1731:                     else:
andrej@1731:                         widget.SetSize((widgetsize[0], rect.height-2))
andrej@1731:                         widget.SetPosition((xpos, rect.y-1))
andrej@1731:                 elif verticalalignment == ESB_ALIGN_TOP:
andrej@1731:                     widget.SetPosition((xpos, rect.y))
andrej@1731:                 elif verticalalignment == ESB_ALIGN_BOTTOM:
andrej@1731:                     widget.SetPosition((xpos, rect.height-widgetsize[1]))
andrej@1731: 
andrej@1731:             elif horizontalalignment == ESB_ALIGN_CENTER_HORIZONTAL:
andrej@1731: 
andrej@2437:                 xpos = rect.x + (rect.width - widgetsize[0]) // 2 - 1
andrej@1731:                 if verticalalignment == ESB_EXACT_FIT:
andrej@1731:                     widget.SetSize((widgetsize[0], rect.height))
andrej@1731:                     widget.SetPosition((xpos, rect.y))
andrej@1731:                 elif verticalalignment == ESB_ALIGN_CENTER_VERTICAL:
andrej@1731:                     if widgetsize[1] < rect.height - 1:
andrej@2437:                         diffs = (rect.height - widgetsize[1]) // 2
andrej@1731:                         widget.SetPosition((xpos, rect.y+diffs))
andrej@1731:                     else:
andrej@1731:                         widget.SetSize((widgetsize[0], rect.height-1))
andrej@1731:                         widget.SetPosition((xpos, rect.y+1))
andrej@1731:                 elif verticalalignment == ESB_ALIGN_TOP:
andrej@1731:                     widget.SetPosition((xpos, rect.y))
andrej@1731:                 elif verticalalignment == ESB_ALIGN_BOTTOM:
andrej@1731:                     widget.SetPosition((xpos, rect.height-widgetsize[1]))
andrej@1731: 
andrej@1731:         if event is not None:
andrej@1731:             event.Skip()
andrej@1731: 
andrej@1731:     def AddWidget(self, widget, horizontalalignment=ESB_ALIGN_CENTER_HORIZONTAL,
andrej@1744:                   verticalalignment=ESB_ALIGN_CENTER_VERTICAL, pos=-1):
andrej@1731:         """Add A Widget To The EnhancedStatusBar.
andrej@1731: 
andrej@1731:         Parameters:
andrej@1731: 
andrej@1731:         - horizontalalignment: This Can Be One Of:
andrej@1731:           a) ESB_EXACT_FIT: The Widget Will Fit Horizontally The StatusBar Field Width;
andrej@1731:           b) ESB_ALIGN_CENTER_HORIZONTAL: The Widget Will Be Centered Horizontally In
andrej@1731:              The StatusBar Field;
andrej@1731:           c) ESB_ALIGN_LEFT: The Widget Will Be Left Aligned In The StatusBar Field;
andrej@1731:           d) ESB_ALIGN_RIGHT: The Widget Will Be Right Aligned In The StatusBar Field;
andrej@1731: 
andrej@1731:         - verticalalignment:
andrej@1731:           a) ESB_EXACT_FIT: The Widget Will Fit Vertically The StatusBar Field Height;
andrej@1731:           b) ESB_ALIGN_CENTER_VERTICAL: The Widget Will Be Centered Vertically In
andrej@1731:              The StatusBar Field;
andrej@1731:           c) ESB_ALIGN_BOTTOM: The Widget Will Be Bottom Aligned In The StatusBar Field;
andrej@1731:           d) ESB_ALIGN_TOP: The Widget Will Be TOP Aligned In The StatusBar Field;
andrej@1731: 
andrej@1731:         """
andrej@1731: 
andrej@1731:         if pos == -1:
andrej@1731:             pos = self._curPos
andrej@1731:             self._curPos += 1
andrej@1731: 
andrej@1731:         if self.GetFieldsCount() <= pos:
andrej@2453:             raise ValueError("\nERROR: EnhancedStatusBar has a max of %d items, you tried to set item #%d" %
andrej@2453:                              (self.GetFieldsCount(), pos))
andrej@1731: 
andrej@1731:         if horizontalalignment not in [ESB_ALIGN_CENTER_HORIZONTAL, ESB_EXACT_FIT,
andrej@1731:                                        ESB_ALIGN_LEFT, ESB_ALIGN_RIGHT]:
andrej@2453:             raise ValueError('\nERROR: Parameter "horizontalalignment" Should Be One Of '
andrej@2453:                              '"ESB_ALIGN_CENTER_HORIZONTAL", "ESB_ALIGN_LEFT", "ESB_ALIGN_RIGHT"'
andrej@2453:                              '"ESB_EXACT_FIT"')
andrej@1731: 
andrej@1731:         if verticalalignment not in [ESB_ALIGN_CENTER_VERTICAL, ESB_EXACT_FIT,
andrej@1731:                                      ESB_ALIGN_TOP, ESB_ALIGN_BOTTOM]:
andrej@2453:             raise ValueError('\nERROR: Parameter "verticalalignment" Should Be One Of '
andrej@2453:                              '"ESB_ALIGN_CENTER_VERTICAL", "ESB_ALIGN_TOP", "ESB_ALIGN_BOTTOM"'
andrej@2453:                              '"ESB_EXACT_FIT"')
andrej@1731: 
andrej@1731:         try:
andrej@1731:             self.RemoveChild(self._items[pos].widget)
andrej@1731:             self._items[pos].widget.Destroy()
andrej@1756:         except KeyError:
andrej@1756:             pass
andrej@1731: 
andrej@1731:         self._items[pos] = EnhancedStatusBarItem(widget, pos, horizontalalignment, verticalalignment)
andrej@1731: 
andrej@1731:         wx.CallAfter(self.OnSize, None)