wrap.py 11 KB

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