wrap.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. """
  2. @package gui_core.wrap
  3. @brief Core wrapped wxpython widgets
  4. Classes:
  5. - wrap::GSpinCtrl
  6. (C) 2016 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 Anna Petrasova <kratochanna gmail.com>
  10. """
  11. import wx
  12. from core.globalvar import gtk3, wxPythonPhoenix
  13. if wxPythonPhoenix:
  14. import wx.adv
  15. def BitmapFromImage(image, depth=-1):
  16. if wxPythonPhoenix:
  17. return wx.Bitmap(img=image, depth=depth)
  18. else:
  19. return wx.BitmapFromImage(image, depth=depth)
  20. def EmptyBitmap(width, height, depth=-1):
  21. if wxPythonPhoenix:
  22. return wx.Bitmap(width=width, height=height, depth=depth)
  23. else:
  24. return wx.EmptyBitmap(width=width, height=height, depth=depth)
  25. def EmptyImage(width, height, clear=True):
  26. if wxPythonPhoenix:
  27. return wx.Image(width=width, height=height, clear=clear)
  28. else:
  29. return wx.EmptyImage(width=width, height=height, clear=clear)
  30. def StockCursor(cursorId):
  31. if wxPythonPhoenix:
  32. return wx.Cursor(cursorId=cursorId)
  33. else:
  34. return wx.StockCursor(cursorId)
  35. class SpinCtrl(wx.SpinCtrl):
  36. """Wrapper around wx.SpinCtrl to have more control
  37. over the widget on different platforms"""
  38. gtk3MinSize = 130
  39. def __init__(self, *args, **kwargs):
  40. if gtk3:
  41. if 'size' in kwargs:
  42. kwargs['size'] = wx.Size(max(self.gtk3MinSize, kwargs['size'][0]), kwargs['size'][1])
  43. else:
  44. kwargs['size'] = wx.Size(self.gtk3MinSize, -1)
  45. wx.SpinCtrl.__init__(self, *args, **kwargs)
  46. class Button(wx.Button):
  47. """Wrapper around wx.Button to have more control
  48. over the widget on different platforms/wxpython versions"""
  49. def __init__(self, *args, **kwargs):
  50. wx.Button.__init__(self, *args, **kwargs)
  51. def SetToolTip(self, tip):
  52. if wxPythonPhoenix:
  53. wx.Button.SetToolTip(self, tipString=tip)
  54. else:
  55. wx.Button.SetToolTipString(self, tip)
  56. class ToggleButton(wx.ToggleButton):
  57. """Wrapper around wx.ToggleButton to have more control
  58. over the widget on different platforms/wxpython versions"""
  59. def __init__(self, *args, **kwargs):
  60. wx.ToggleButton.__init__(self, *args, **kwargs)
  61. def SetToolTip(self, tip):
  62. if wxPythonPhoenix:
  63. wx.ToggleButton.SetToolTip(self, tipString=tip)
  64. else:
  65. wx.ToggleButton.SetToolTipString(self, tip)
  66. class StaticText(wx.StaticText):
  67. """Wrapper around wx.StaticText to have more control
  68. over the widget on different platforms/wxpython versions"""
  69. def __init__(self, *args, **kwargs):
  70. wx.StaticText.__init__(self, *args, **kwargs)
  71. def SetToolTip(self, tip):
  72. if wxPythonPhoenix:
  73. wx.StaticText.SetToolTip(self, tipString=tip)
  74. else:
  75. wx.StaticText.SetToolTipString(self, tip)
  76. class StaticBox(wx.StaticBox):
  77. """Wrapper around wx.StaticBox to have more control
  78. over the widget on different platforms/wxpython versions"""
  79. def __init__(self, *args, **kwargs):
  80. wx.StaticBox.__init__(self, *args, **kwargs)
  81. def SetToolTip(self, tip):
  82. if wxPythonPhoenix:
  83. wx.StaticBox.SetToolTip(self, tipString=tip)
  84. else:
  85. wx.StaticBox.SetToolTipString(self, tip)
  86. class TextCtrl(wx.TextCtrl):
  87. """Wrapper around wx.TextCtrl to have more control
  88. over the widget on different platforms/wxpython versions"""
  89. def __init__(self, *args, **kwargs):
  90. wx.TextCtrl.__init__(self, *args, **kwargs)
  91. def SetToolTip(self, tip):
  92. if wxPythonPhoenix:
  93. wx.TextCtrl.SetToolTip(self, tipString=tip)
  94. else:
  95. wx.TextCtrl.SetToolTipString(self, tip)
  96. class ListCtrl(wx.ListCtrl):
  97. """Wrapper around wx.ListCtrl to have more control
  98. over the widget on different platforms/wxpython versions"""
  99. def __init__(self, *args, **kwargs):
  100. wx.ListCtrl.__init__(self, *args, **kwargs)
  101. def InsertStringItem(self, index, label, imageIndex=-1):
  102. if wxPythonPhoenix:
  103. return wx.ListCtrl.InsertItem(self, index=index, label=label, imageIndex=imageIndex)
  104. else:
  105. return wx.ListCtrl.InsertStringItem(self, index=index, label=label, imageIndex=imageIndex)
  106. def SetStringItem(self, index, column, label, imageId=-1):
  107. if wxPythonPhoenix:
  108. return wx.ListCtrl.SetItem(self, index=index, column=column, label=label, imageId=imageId)
  109. else:
  110. return wx.ListCtrl.SetStringItem(self, index=index, col=column, label=label, imageId=imageId)
  111. class TreeCtrl(wx.TreeCtrl):
  112. """Wrapper around wx.TreeCtrl to have more control
  113. over the widget on different platforms/wxpython versions"""
  114. def __init__(self, *args, **kwargs):
  115. wx.TreeCtrl.__init__(self, *args, **kwargs)
  116. def AppendItem(self, parent, text, image=-1, selImage=-1, data=None):
  117. if wxPythonPhoenix:
  118. return wx.TreeCtrl.AppendItem(self, parent, text, image, selImage, data)
  119. else:
  120. return wx.TreeCtrl.AppendItem(self, parent, text, image, selImage, wx.TreeItemData(data))
  121. class ToolBar(wx.ToolBar):
  122. """Wrapper around wx.ToolBar to have more control
  123. over the widget on different platforms/wxpython versions"""
  124. def __init__(self, *args, **kwargs):
  125. wx.ToolBar.__init__(self, *args, **kwargs)
  126. def AddLabelTool(self, toolId, label, bitmap, bmpDisabled=wx.NullBitmap, kind=0,
  127. shortHelpString='', longHelpString='', clientData=None):
  128. if wxPythonPhoenix:
  129. return wx.ToolBar.AddTool(self, toolId=toolId, label=label, bitmap=bitmap, bmpDisabled=bmpDisabled,
  130. kind=kind, shortHelpString=shortHelpString, longHelpString=longHelpString,
  131. clientData=clientData)
  132. else:
  133. return wx.ToolBar.AddLabelTool(self, toolId, label, bitmap, bmpDisabled, kind,
  134. shortHelpString, longHelpString, clientData)
  135. def InsertLabelTool(self, pos, toolId, label, bitmap, bmpDisabled=wx.NullBitmap, kind=0,
  136. shortHelpString='', longHelpString='', clientData=None):
  137. if wxPythonPhoenix:
  138. return wx.ToolBar.InsertTool(self, pos, toolId=toolId, label=label, bitmap=bitmap, bmpDisabled=bmpDisabled,
  139. kind=kind, shortHelp=shortHelpString, longHelp=longHelpString,
  140. clientData=clientData)
  141. else:
  142. return wx.ToolBar.InsertLabelTool(self, pos, toolId, label, bitmap, bmpDisabled, kind,
  143. shortHelpString, longHelpString, clientData)
  144. class Menu(wx.Menu):
  145. """Wrapper around wx.Menu to have more control
  146. over the widget on different platforms/wxpython versions"""
  147. def __init__(self, *args, **kwargs):
  148. wx.Menu.__init__(self, *args, **kwargs)
  149. def AppendItem(self, menuItem):
  150. if wxPythonPhoenix:
  151. wx.Menu.Append(self, menuItem=menuItem)
  152. else:
  153. wx.Menu.AppendItem(self, menuItem)
  154. def AppendMenu(self, id, text, submenu, help=""):
  155. if wxPythonPhoenix:
  156. wx.Menu.Append(self, id=id, item=text, subMenu=submenu, helpString=help)
  157. else:
  158. wx.Menu.AppendMenu(self, id=id, text=text, submenu=submenu, help=help)
  159. class DragImage(wx.GenericDragImage if wxPythonPhoenix else wx.DragImage):
  160. """Wrapper around wx.DragImage to have more control
  161. over the widget on different platforms/wxpython versions"""
  162. def __init__(self, *args, **kwargs):
  163. super(DragImage, self).__init__(*args, **kwargs)
  164. class PseudoDC(wx.adv.PseudoDC if wxPythonPhoenix else wx.PseudoDC):
  165. """Wrapper around wx.PseudoDC to have more control
  166. over the widget on different platforms/wxpython versions"""
  167. def __init__(self, *args, **kwargs):
  168. super(PseudoDC, self).__init__(*args, **kwargs)
  169. def DrawRectangleRect(self, rect):
  170. if wxPythonPhoenix:
  171. super(PseudoDC, self).DrawRectangle(rect=rect)
  172. else:
  173. super(PseudoDC, self).DrawRectangleRect(rect)
  174. def BeginDrawing(self):
  175. if not wxPythonPhoenix:
  176. super(PseudoDC, self).BeginDrawing()
  177. def EndDrawing(self):
  178. if not wxPythonPhoenix:
  179. super(PseudoDC, self).EndDrawing()
  180. class Rect(wx.Rect):
  181. """Wrapper around wx.Rect to have more control
  182. over the widget on different platforms/wxpython versions"""
  183. def __init__(self, *args, **kwargs):
  184. wx.Rect.__init__(self, *args, **kwargs)
  185. def ContainsXY(self, x, y):
  186. if wxPythonPhoenix:
  187. return wx.Rect.Contains(self, x=x, y=y)
  188. else:
  189. return wx.Rect.ContainsXY(self, x, y)
  190. def ContainsRect(self, rect):
  191. if wxPythonPhoenix:
  192. return wx.Rect.Contains(self, rect=rect)
  193. else:
  194. return wx.Rect.ContainsRect(self, rect)