wrap.py 13 KB

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