infobar.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. self._unInitLayout()
  38. sizer = wx.BoxSizer(wx.VERTICAL)
  39. subSizerText = wx.BoxSizer(wx.HORIZONTAL)
  40. self.subSizerButtons = wx.BoxSizer(wx.HORIZONTAL)
  41. subSizerText.Add(self._icon, wx.SizerFlags().Centre().Border())
  42. subSizerText.Add(self._text, 1, wx.ALIGN_CENTER_VERTICAL)
  43. self.subSizerButtons.AddStretchSpacer()
  44. self.subSizerButtons.Add(self._button, wx.SizerFlags().Centre().Border())
  45. sizer.Add(subSizerText, wx.SizerFlags().Expand())
  46. sizer.Add(self.subSizerButtons, wx.SizerFlags().Expand())
  47. self.SetSizer(sizer)
  48. def _unInitLayout(self):
  49. sizer = self.GetSizer()
  50. children = sizer.GetChildren()
  51. for i in reversed(range(len(children))):
  52. if children[i].IsSpacer():
  53. sizer.Remove(i)
  54. else:
  55. sizer.Detach(i)
  56. def ShowMessage(self, message, icon, buttons=None):
  57. """Show message with buttons (optional).
  58. Buttons are list of tuples (label, handler)"""
  59. self.Hide()
  60. self.RemoveButtons()
  61. if buttons:
  62. self.SetButtons(buttons)
  63. super().ShowMessage(message, icon)
  64. def AddButton(self, btnid, label):
  65. """
  66. Adds a button to be shown in the info bar.
  67. """
  68. sizer = self.GetSizer()
  69. assert sizer is not None, "Sizer must be created first"
  70. # user-added buttons replace the standard close button so remove it if we
  71. # hadn't done it yet
  72. if sizer.Detach(self._button):
  73. self._button.Hide()
  74. button = wx.Button(self, btnid, label)
  75. button.SetBackgroundColour(self._background_color)
  76. button.SetForegroundColour(self._foreground_color)
  77. if wx.Platform == '__WXMAC__':
  78. # smaller buttons look better in the(narrow)info bar under OS X
  79. button.SetWindowVariant(wx.WINDOW_VARIANT_SMALL)
  80. num_items = self.subSizerButtons.GetItemCount()
  81. if num_items == 1:
  82. self.subSizerButtons.Add(button, wx.SizerFlags().Centre().Border())
  83. self.subSizerButtons.Add(self._button, wx.SizerFlags().Centre().Border())
  84. self._button.Show()
  85. else:
  86. self.subSizerButtons.Insert(num_items - 1, button, wx.SizerFlags().Centre().Border())
  87. if self.IsShown():
  88. self.UpdateParent()
  89. def SetButtons(self, buttons):
  90. """
  91. Sets buttons for notification.
  92. Parameter *buttons* is a list of tuples (button_name, event)
  93. """
  94. for button_name, evt_handler in buttons:
  95. button_id = wx.NewId()
  96. self.button_ids.append(button_id)
  97. self.AddButton(button_id, button_name)
  98. self.Bind(wx.EVT_BUTTON, evt_handler, id=button_id)
  99. def RemoveButtons(self):
  100. """
  101. Removes buttons from info bar.
  102. """
  103. items = self.subSizerButtons.GetChildren()
  104. for item in reversed(items):
  105. if not item.IsSpacer():
  106. window = item.GetWindow()
  107. if window.GetId() in self.button_ids:
  108. self.subSizerButtons.Detach(window)
  109. window.Destroy()