wrap.py 16 KB

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