infobar.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. """
  2. @package gui_core.infobar
  3. @brief Wrapper around wx.InfoBar
  4. Classes:
  5. - gui_core::InfoBar
  6. (C) 2020 by the GRASS Development Team
  7. This program is free software under the GNU General Public License
  8. (>=v2). Read the file COPYING that comes with GRASS for details.
  9. @author Linda Kladivova
  10. @author Anna Petrasova <kratochanna gmail.com>
  11. @author Vaclav Petras <wenzeslaus gmail.com>
  12. """
  13. import wx
  14. import wx.aui
  15. try:
  16. import wx.lib.agw.infobar as IB
  17. except ImportError:
  18. import wx.lib.infobar as IB
  19. class InfoBar(IB.InfoBar):
  20. """A customized and specialized info bar to used by default"""
  21. def __init__(self, parent):
  22. IB.InfoBar.__init__(self, parent)
  23. self.button_ids = []
  24. # some system themes have alpha, remove it
  25. self._background_color = wx.SystemSettings.GetColour(
  26. wx.SYS_COLOUR_HIGHLIGHT
  27. ).Get(False)
  28. self._foreground_color = wx.SystemSettings.GetColour(
  29. wx.SYS_COLOUR_HIGHLIGHTTEXT
  30. ).Get(False)
  31. self.SetBackgroundColour(self._background_color)
  32. self.SetForegroundColour(self._foreground_color)
  33. self._text.SetBackgroundColour(self._background_color)
  34. self._text.SetForegroundColour(self._foreground_color)
  35. self._button.SetBackgroundColour(self._background_color)
  36. # LAYOUT
  37. sizer = wx.BoxSizer(wx.VERTICAL)
  38. self.subSizerText = wx.BoxSizer(wx.HORIZONTAL)
  39. self.subSizerButtons = wx.BoxSizer(wx.HORIZONTAL)
  40. self.subSizerText.Add(self._icon, wx.SizerFlags().Centre().Border())
  41. self.subSizerText.Add(self._text, 1, wx.ALIGN_CENTER_VERTICAL)
  42. self.subSizerButtons.AddStretchSpacer()
  43. self.subSizerButtons.Add(self._button, wx.SizerFlags().Centre().Border())
  44. sizer.Add(self.subSizerText, wx.SizerFlags().Expand())
  45. sizer.Add(self.subSizerButtons, wx.SizerFlags().Expand())
  46. self.SetSizer(sizer)
  47. def ShowMessage(self, message, icon, buttons=None):
  48. """Show message with buttons (optional).
  49. Buttons are list of tuples (label, handler)"""
  50. self.Hide()
  51. self.RemoveButtons()
  52. if buttons:
  53. self.SetButtons(buttons)
  54. super().ShowMessage(message, icon)
  55. def AddButton(self, btnid, label):
  56. """
  57. Adds a button to be shown in the info bar.
  58. """
  59. sizer = self.GetSizer()
  60. assert sizer is not None, "Sizer must be created first"
  61. # user-added buttons replace the standard close button so remove it if we
  62. # hadn't done it yet
  63. if sizer.Detach(self._button):
  64. self._button.Hide()
  65. button = wx.Button(self, btnid, label)
  66. button.SetBackgroundColour(self._background_color)
  67. button.SetForegroundColour(self._foreground_color)
  68. if wx.Platform == '__WXMAC__':
  69. # smaller buttons look better in the(narrow)info bar under OS X
  70. button.SetWindowVariant(wx.WINDOW_VARIANT_SMALL)
  71. num_items = self.subSizerButtons.GetItemCount()
  72. if num_items == 1:
  73. self.subSizerButtons.Add(button, wx.SizerFlags().Centre().Border())
  74. self.subSizerButtons.Add(self._button, wx.SizerFlags().Centre().Border())
  75. self._button.Show()
  76. else:
  77. self.subSizerButtons.Insert(num_items - 1, button, wx.SizerFlags().Centre().Border())
  78. if self.IsShown():
  79. self.UpdateParent()
  80. def SetButtons(self, buttons):
  81. """
  82. Sets buttons for notification.
  83. Parameter *buttons* is a list of tuples (button_name, event)
  84. """
  85. for button_name, evt_handler in buttons:
  86. button_id = wx.NewId()
  87. self.button_ids.append(button_id)
  88. self.AddButton(button_id, button_name)
  89. self.Bind(wx.EVT_BUTTON, evt_handler, id=button_id)
  90. def RemoveButtons(self):
  91. """
  92. Removes buttons from info bar.
  93. """
  94. items = self.subSizerButtons.GetChildren()
  95. for item in reversed(items):
  96. if not item.IsSpacer():
  97. window = item.GetWindow()
  98. if window.GetId() in self.button_ids:
  99. self.subSizerButtons.Detach(window)
  100. window.Destroy()