wrap.py 16 KB

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