wrap.py 16 KB

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