wrap.py 16 KB

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