wrap.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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 Panel(wx.Panel):
  57. """Wrapper around wx.Panel to have more control
  58. over the widget on different platforms/wxpython versions"""
  59. def __init__(self, *args, **kwargs):
  60. wx.Panel.__init__(self, *args, **kwargs)
  61. def SetToolTip(self, tip):
  62. if wxPythonPhoenix:
  63. wx.Panel.SetToolTip(self, tipString=tip)
  64. else:
  65. wx.Panel.SetToolTipString(self, tip)
  66. class SpinCtrl(wx.SpinCtrl):
  67. """Wrapper around wx.SpinCtrl to have more control
  68. over the widget on different platforms"""
  69. gtk3MinSize = 130
  70. def __init__(self, *args, **kwargs):
  71. if gtk3:
  72. if 'size' in kwargs:
  73. kwargs['size'] = wx.Size(max(self.gtk3MinSize, kwargs['size'][0]), kwargs['size'][1])
  74. else:
  75. kwargs['size'] = wx.Size(self.gtk3MinSize, -1)
  76. wx.SpinCtrl.__init__(self, *args, **kwargs)
  77. class Button(wx.Button):
  78. """Wrapper around wx.Button to have more control
  79. over the widget on different platforms/wxpython versions"""
  80. def __init__(self, *args, **kwargs):
  81. wx.Button.__init__(self, *args, **kwargs)
  82. def SetToolTip(self, tip):
  83. if wxPythonPhoenix:
  84. wx.Button.SetToolTip(self, tipString=tip)
  85. else:
  86. wx.Button.SetToolTipString(self, tip)
  87. class RadioButton(wx.RadioButton):
  88. """Wrapper around wx.RadioButton to have more control
  89. over the widget on different platforms/wxpython versions"""
  90. def __init__(self, *args, **kwargs):
  91. wx.RadioButton.__init__(self, *args, **kwargs)
  92. def SetToolTip(self, tip):
  93. if wxPythonPhoenix:
  94. wx.RadioButton.SetToolTip(self, tipString=tip)
  95. else:
  96. wx.RadioButton.SetToolTipString(self, tip)
  97. class BitmapButton(wx.BitmapButton):
  98. """Wrapper around wx.BitmapButton to have more control
  99. over the widget on different platforms/wxpython versions"""
  100. def __init__(self, *args, **kwargs):
  101. wx.BitmapButton.__init__(self, *args, **kwargs)
  102. def SetToolTip(self, tip):
  103. if wxPythonPhoenix:
  104. wx.BitmapButton.SetToolTip(self, tipString=tip)
  105. else:
  106. wx.BitmapButton.SetToolTipString(self, tip)
  107. class GenBitmapButton(buttons.GenBitmapButton):
  108. """Wrapper around GenBitmapButton to have more control
  109. over the widget on different platforms/wxpython versions"""
  110. def __init__(self, *args, **kwargs):
  111. buttons.GenBitmapButton.__init__(self, *args, **kwargs)
  112. def SetToolTip(self, tip):
  113. if wxPythonPhoenix:
  114. buttons.GenBitmapButton.SetToolTip(self, tipString=tip)
  115. else:
  116. buttons.GenBitmapButton.SetToolTipString(self, tip)
  117. class ToggleButton(wx.ToggleButton):
  118. """Wrapper around wx.ToggleButton to have more control
  119. over the widget on different platforms/wxpython versions"""
  120. def __init__(self, *args, **kwargs):
  121. wx.ToggleButton.__init__(self, *args, **kwargs)
  122. def SetToolTip(self, tip):
  123. if wxPythonPhoenix:
  124. wx.ToggleButton.SetToolTip(self, tipString=tip)
  125. else:
  126. wx.ToggleButton.SetToolTipString(self, tip)
  127. class StaticText(wx.StaticText):
  128. """Wrapper around wx.StaticText to have more control
  129. over the widget on different platforms/wxpython versions"""
  130. def __init__(self, *args, **kwargs):
  131. wx.StaticText.__init__(self, *args, **kwargs)
  132. def SetToolTip(self, tip):
  133. if wxPythonPhoenix:
  134. wx.StaticText.SetToolTip(self, tipString=tip)
  135. else:
  136. wx.StaticText.SetToolTipString(self, tip)
  137. class StaticBox(wx.StaticBox):
  138. """Wrapper around wx.StaticBox to have more control
  139. over the widget on different platforms/wxpython versions"""
  140. def __init__(self, *args, **kwargs):
  141. wx.StaticBox.__init__(self, *args, **kwargs)
  142. def SetToolTip(self, tip):
  143. if wxPythonPhoenix:
  144. wx.StaticBox.SetToolTip(self, tipString=tip)
  145. else:
  146. wx.StaticBox.SetToolTipString(self, tip)
  147. class CheckListBox(wx.CheckListBox):
  148. """Wrapper around wx.CheckListBox to have more control
  149. over the widget on different platforms/wxpython versions"""
  150. def __init__(self, *args, **kwargs):
  151. wx.CheckListBox.__init__(self, *args, **kwargs)
  152. def SetToolTip(self, tip):
  153. if wxPythonPhoenix:
  154. wx.CheckListBox.SetToolTip(self, tipString=tip)
  155. else:
  156. wx.CheckListBox.SetToolTipString(self, tip)
  157. class TextCtrl(wx.TextCtrl):
  158. """Wrapper around wx.TextCtrl to have more control
  159. over the widget on different platforms/wxpython versions"""
  160. def __init__(self, *args, **kwargs):
  161. wx.TextCtrl.__init__(self, *args, **kwargs)
  162. def SetToolTip(self, tip):
  163. if wxPythonPhoenix:
  164. wx.TextCtrl.SetToolTip(self, tipString=tip)
  165. else:
  166. wx.TextCtrl.SetToolTipString(self, tip)
  167. class SearchCtrl(wx.SearchCtrl):
  168. """Wrapper around wx.SearchCtrl to have more control
  169. over the widget on different platforms/wxpython versions"""
  170. def __init__(self, *args, **kwargs):
  171. wx.SearchCtrl.__init__(self, *args, **kwargs)
  172. def SetToolTip(self, tip):
  173. if wxPythonPhoenix:
  174. wx.SearchCtrl.SetToolTip(self, tipString=tip)
  175. else:
  176. wx.SearchCtrl.SetToolTipString(self, tip)
  177. class ListCtrl(wx.ListCtrl):
  178. """Wrapper around wx.ListCtrl to have more control
  179. over the widget on different platforms/wxpython versions"""
  180. def __init__(self, *args, **kwargs):
  181. wx.ListCtrl.__init__(self, *args, **kwargs)
  182. def InsertStringItem(self, index, label, imageIndex=-1):
  183. if wxPythonPhoenix:
  184. return wx.ListCtrl.InsertItem(self, index=index, label=label, imageIndex=imageIndex)
  185. else:
  186. return wx.ListCtrl.InsertStringItem(self, index=index, label=label, imageIndex=imageIndex)
  187. def SetStringItem(self, index, column, label, imageId=-1):
  188. if wxPythonPhoenix:
  189. return wx.ListCtrl.SetItem(self, index=index, column=column, label=label, imageId=imageId)
  190. else:
  191. return wx.ListCtrl.SetStringItem(self, index=index, col=column, label=label, imageId=imageId)
  192. class TreeCtrl(wx.TreeCtrl):
  193. """Wrapper around wx.TreeCtrl to have more control
  194. over the widget on different platforms/wxpython versions"""
  195. def __init__(self, *args, **kwargs):
  196. wx.TreeCtrl.__init__(self, *args, **kwargs)
  197. def AppendItem(self, parent, text, image=-1, selImage=-1, data=None):
  198. if wxPythonPhoenix:
  199. return wx.TreeCtrl.AppendItem(self, parent, text, image, selImage, data)
  200. else:
  201. return wx.TreeCtrl.AppendItem(self, parent, text, image, selImage, wx.TreeItemData(data))
  202. def GetItemData(self, item):
  203. if wxPythonPhoenix:
  204. return wx.TreeCtrl.GetItemData(self, item)
  205. else:
  206. return wx.TreeCtrl.GetPyData(self, item)
  207. class CustomTreeCtrl(CT.CustomTreeCtrl):
  208. """Wrapper around wx.lib.agw.customtreectrl to have more control
  209. over the widget on different platforms/wxpython versions"""
  210. def __init__(self, *args, **kwargs):
  211. CT.CustomTreeCtrl.__init__(self, *args, **kwargs)
  212. def SetToolTip(self, tip):
  213. if wxPythonPhoenix:
  214. CT.CustomTreeCtrl.SetToolTip(self, tipString=tip)
  215. else:
  216. CT.CustomTreeCtrl.SetToolTipString(self, tip)
  217. class ToolBar(wx.ToolBar):
  218. """Wrapper around wx.ToolBar to have more control
  219. over the widget on different platforms/wxpython versions"""
  220. def __init__(self, *args, **kwargs):
  221. wx.ToolBar.__init__(self, *args, **kwargs)
  222. def AddLabelTool(self, toolId, label, bitmap, bmpDisabled=wx.NullBitmap, kind=0,
  223. shortHelpString='', longHelpString='', clientData=None):
  224. if wxPythonPhoenix:
  225. return wx.ToolBar.AddTool(self, toolId=toolId, label=label, bitmap=bitmap, bmpDisabled=bmpDisabled,
  226. kind=kind, shortHelp=shortHelpString, longHelp=longHelpString,
  227. clientData=clientData)
  228. else:
  229. return wx.ToolBar.AddLabelTool(self, toolId, label, bitmap, bmpDisabled, kind,
  230. shortHelpString, longHelpString, clientData)
  231. def InsertLabelTool(self, pos, toolId, label, bitmap, bmpDisabled=wx.NullBitmap, kind=0,
  232. shortHelpString='', longHelpString='', clientData=None):
  233. if wxPythonPhoenix:
  234. return wx.ToolBar.InsertTool(self, pos, toolId=toolId, label=label, bitmap=bitmap, bmpDisabled=bmpDisabled,
  235. kind=kind, shortHelp=shortHelpString, longHelp=longHelpString,
  236. clientData=clientData)
  237. else:
  238. return wx.ToolBar.InsertLabelTool(self, pos, toolId, label, bitmap, bmpDisabled, kind,
  239. shortHelpString, longHelpString, clientData)
  240. class Menu(wx.Menu):
  241. """Wrapper around wx.Menu to have more control
  242. over the widget on different platforms/wxpython versions"""
  243. def __init__(self, *args, **kwargs):
  244. wx.Menu.__init__(self, *args, **kwargs)
  245. def AppendItem(self, menuItem):
  246. if wxPythonPhoenix:
  247. wx.Menu.Append(self, menuItem=menuItem)
  248. else:
  249. wx.Menu.AppendItem(self, menuItem)
  250. def AppendMenu(self, id, text, submenu, help=""):
  251. if wxPythonPhoenix:
  252. wx.Menu.Append(self, id=id, item=text, subMenu=submenu, helpString=help)
  253. else:
  254. wx.Menu.AppendMenu(self, id=id, text=text, submenu=submenu, help=help)
  255. class DragImage(wx.GenericDragImage if wxPythonPhoenix else wx.DragImage):
  256. """Wrapper around wx.DragImage to have more control
  257. over the widget on different platforms/wxpython versions"""
  258. def __init__(self, *args, **kwargs):
  259. super(DragImage, self).__init__(*args, **kwargs)
  260. class PseudoDC(wx.adv.PseudoDC if wxPythonPhoenix else wx.PseudoDC):
  261. """Wrapper around wx.PseudoDC to have more control
  262. over the widget on different platforms/wxpython versions"""
  263. def __init__(self, *args, **kwargs):
  264. super(PseudoDC, self).__init__(*args, **kwargs)
  265. def DrawLinePoint(self, pt1, pt2):
  266. if wxPythonPhoenix:
  267. super(PseudoDC, self).DrawLine(pt1, pt2)
  268. else:
  269. super(PseudoDC, self).DrawLinePoint(pt1, pt2)
  270. def DrawRectangleRect(self, rect):
  271. if wxPythonPhoenix:
  272. super(PseudoDC, self).DrawRectangle(rect=rect)
  273. else:
  274. super(PseudoDC, self).DrawRectangleRect(rect)
  275. def BeginDrawing(self):
  276. if not wxPythonPhoenix:
  277. super(PseudoDC, self).BeginDrawing()
  278. def EndDrawing(self):
  279. if not wxPythonPhoenix:
  280. super(PseudoDC, self).EndDrawing()
  281. class ClientDC(wx.ClientDC):
  282. """Wrapper around wx.ClientDC to have more control
  283. over the widget on different platforms/wxpython versions"""
  284. def __init__(self, *args, **kwargs):
  285. super(ClientDC, self).__init__(*args, **kwargs)
  286. def GetFullMultiLineTextExtent(self, string, font=None):
  287. if wxPythonPhoenix:
  288. return super(ClientDC, self).GetFullMultiLineTextExtent(string, font)
  289. else:
  290. return super(ClientDC, self).GetMultiLineTextExtent(string, font)
  291. class Rect(wx.Rect):
  292. """Wrapper around wx.Rect to have more control
  293. over the widget on different platforms/wxpython versions"""
  294. def __init__(self, *args, **kwargs):
  295. wx.Rect.__init__(self, *args, **kwargs)
  296. def ContainsXY(self, x, y):
  297. if wxPythonPhoenix:
  298. return wx.Rect.Contains(self, x=x, y=y)
  299. else:
  300. return wx.Rect.ContainsXY(self, x, y)
  301. def ContainsRect(self, rect):
  302. if wxPythonPhoenix:
  303. return wx.Rect.Contains(self, rect=rect)
  304. else:
  305. return wx.Rect.ContainsRect(self, rect)
  306. def OffsetXY(self, dx, dy):
  307. if wxPythonPhoenix:
  308. return wx.Rect.Offset(self, dx, dy)
  309. else:
  310. return wx.Rect.OffsetXY(self, dx, dy)
  311. class CheckBox(wx.CheckBox):
  312. """Wrapper around wx.CheckBox to have more control
  313. over the widget on different platforms/wxpython versions"""
  314. def __init__(self, *args, **kwargs):
  315. wx.CheckBox.__init__(self, *args, **kwargs)
  316. def SetToolTip(self, tip):
  317. if wxPythonPhoenix:
  318. wx.CheckBox.SetToolTip(self, tipString=tip)
  319. else:
  320. wx.CheckBox.SetToolTipString(self, tip)
  321. class Choice(wx.Choice):
  322. """Wrapper around wx.Choice to have more control
  323. over the widget on different platforms/wxpython versions"""
  324. def __init__(self, *args, **kwargs):
  325. wx.Choice.__init__(self, *args, **kwargs)
  326. def SetToolTip(self, tip):
  327. if wxPythonPhoenix:
  328. wx.Choice.SetToolTip(self, tipString=tip)
  329. else:
  330. wx.Choice.SetToolTipString(self, tip)