pystc.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. """
  2. @package gui_core.pystc
  3. @brief Python styled text control widget
  4. Classes:
  5. - pystc::PyStc
  6. (C) 2012-2013 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 Martin Landa <landa.martin gmail.com>
  10. """
  11. import keyword
  12. import wx
  13. from wx import stc
  14. from core.utils import _
  15. class PyStc(stc.StyledTextCtrl):
  16. """Styled Python output (see gmodeler::frame::PythonPanel for
  17. usage)
  18. Based on StyledTextCtrl_2 from wxPython demo
  19. """
  20. def __init__(self, parent, id=wx.ID_ANY, statusbar=None):
  21. stc.StyledTextCtrl.__init__(self, parent, id)
  22. self.parent = parent
  23. self.statusbar = statusbar
  24. self.modified = False # content modified ?
  25. # this is supposed to get monospace
  26. font = wx.Font(
  27. 9,
  28. wx.FONTFAMILY_MODERN,
  29. wx.FONTSTYLE_NORMAL,
  30. wx.FONTWEIGHT_NORMAL)
  31. face = font.GetFaceName()
  32. size = font.GetPointSize()
  33. # setting the monospace here to not mess with the rest of the code
  34. # TODO: review the whole styling
  35. self.faces = {'times': face,
  36. 'mono': face,
  37. 'helv': face,
  38. 'other': face,
  39. 'size': 10,
  40. 'size2': 8,
  41. }
  42. self.CmdKeyAssign(ord('B'), stc.STC_SCMOD_CTRL, stc.STC_CMD_ZOOMIN)
  43. self.CmdKeyAssign(ord('N'), stc.STC_SCMOD_CTRL, stc.STC_CMD_ZOOMOUT)
  44. self.SetLexer(stc.STC_LEX_PYTHON)
  45. self.SetKeyWords(0, " ".join(keyword.kwlist))
  46. self.SetProperty("fold", "1")
  47. self.SetProperty("tab.timmy.whinge.level", "1")
  48. self.SetMargins(0, 0)
  49. self.SetTabWidth(4)
  50. self.SetUseTabs(False)
  51. self.SetEdgeMode(stc.STC_EDGE_BACKGROUND)
  52. self.SetEdgeColumn(78)
  53. # setup a margin to hold fold markers
  54. self.SetMarginType(2, stc.STC_MARGIN_SYMBOL)
  55. self.SetMarginMask(2, stc.STC_MASK_FOLDERS)
  56. self.SetMarginSensitive(2, True)
  57. self.SetMarginWidth(2, 12)
  58. # like a flattened tree control using square headers
  59. self.MarkerDefine(
  60. stc.STC_MARKNUM_FOLDEROPEN,
  61. stc.STC_MARK_BOXMINUS,
  62. "white",
  63. "#808080")
  64. self.MarkerDefine(
  65. stc.STC_MARKNUM_FOLDER,
  66. stc.STC_MARK_BOXPLUS,
  67. "white",
  68. "#808080")
  69. self.MarkerDefine(
  70. stc.STC_MARKNUM_FOLDERSUB,
  71. stc.STC_MARK_VLINE,
  72. "white",
  73. "#808080")
  74. self.MarkerDefine(
  75. stc.STC_MARKNUM_FOLDERTAIL,
  76. stc.STC_MARK_LCORNER,
  77. "white",
  78. "#808080")
  79. self.MarkerDefine(
  80. stc.STC_MARKNUM_FOLDEREND,
  81. stc.STC_MARK_BOXPLUSCONNECTED,
  82. "white",
  83. "#808080")
  84. self.MarkerDefine(
  85. stc.STC_MARKNUM_FOLDEROPENMID,
  86. stc.STC_MARK_BOXMINUSCONNECTED,
  87. "white",
  88. "#808080")
  89. self.MarkerDefine(
  90. stc.STC_MARKNUM_FOLDERMIDTAIL,
  91. stc.STC_MARK_TCORNER,
  92. "white",
  93. "#808080")
  94. self.Bind(stc.EVT_STC_UPDATEUI, self.OnUpdateUI)
  95. self.Bind(stc.EVT_STC_MARGINCLICK, self.OnMarginClick)
  96. self.Bind(wx.EVT_KEY_DOWN, self.OnKeyPressed)
  97. # show whitespace
  98. self.SetViewWhiteSpace(1)
  99. # make the symbols very light gray to be less distracting
  100. self.SetWhitespaceForeground(True, wx.Colour(200, 200, 200))
  101. # Make some styles, the lexer defines what each style is used
  102. # for, we just have to define what each style looks like.
  103. # This set is adapted from Scintilla sample property files.
  104. # global default styles for all languages
  105. self.StyleSetSpec(
  106. stc.STC_STYLE_DEFAULT,
  107. "face:%(helv)s,size:%(size)d" %
  108. self.faces)
  109. self.StyleClearAll() # reset all to be like the default
  110. # global default styles for all languages
  111. self.StyleSetSpec(
  112. stc.STC_STYLE_DEFAULT,
  113. "face:%(helv)s,size:%(size)d" %
  114. self.faces)
  115. self.StyleSetSpec(
  116. stc.STC_STYLE_LINENUMBER,
  117. "back:#C0C0C0,face:%(helv)s,size:%(size2)d" %
  118. self.faces)
  119. self.StyleSetSpec(
  120. stc.STC_STYLE_CONTROLCHAR,
  121. "face:%(other)s" %
  122. self.faces)
  123. self.StyleSetSpec(
  124. stc.STC_STYLE_BRACELIGHT,
  125. "fore:#FFFFFF,back:#0000FF,bold")
  126. self.StyleSetSpec(
  127. stc.STC_STYLE_BRACEBAD,
  128. "fore:#000000,back:#FF0000,bold")
  129. # Python styles
  130. # Default
  131. self.StyleSetSpec(
  132. stc.STC_P_DEFAULT,
  133. "fore:#000000,face:%(helv)s,size:%(size)d" %
  134. self.faces)
  135. # Comments
  136. self.StyleSetSpec(
  137. stc.STC_P_COMMENTLINE,
  138. "fore:#007F00,face:%(other)s,size:%(size)d" %
  139. self.faces)
  140. # Number
  141. self.StyleSetSpec(
  142. stc.STC_P_NUMBER,
  143. "fore:#007F7F,size:%(size)d" %
  144. self.faces)
  145. # String
  146. self.StyleSetSpec(
  147. stc.STC_P_STRING,
  148. "fore:#7F007F,face:%(helv)s,size:%(size)d" %
  149. self.faces)
  150. # Single quoted string
  151. self.StyleSetSpec(
  152. stc.STC_P_CHARACTER,
  153. "fore:#7F007F,face:%(helv)s,size:%(size)d" %
  154. self.faces)
  155. # Keyword
  156. self.StyleSetSpec(
  157. stc.STC_P_WORD,
  158. "fore:#00007F,bold,size:%(size)d" %
  159. self.faces)
  160. # Triple quotes
  161. self.StyleSetSpec(
  162. stc.STC_P_TRIPLE,
  163. "fore:#7F0000,size:%(size)d" %
  164. self.faces)
  165. # Triple double quotes
  166. self.StyleSetSpec(
  167. stc.STC_P_TRIPLEDOUBLE,
  168. "fore:#7F0000,size:%(size)d" %
  169. self.faces)
  170. # Class name definition
  171. self.StyleSetSpec(
  172. stc.STC_P_CLASSNAME,
  173. "fore:#0000FF,bold,underline,size:%(size)d" %
  174. self.faces)
  175. # Function or method name definition
  176. self.StyleSetSpec(
  177. stc.STC_P_DEFNAME,
  178. "fore:#007F7F,bold,size:%(size)d" %
  179. self.faces)
  180. # Operators
  181. self.StyleSetSpec(
  182. stc.STC_P_OPERATOR,
  183. "bold,size:%(size)d" %
  184. self.faces)
  185. # Identifiers
  186. self.StyleSetSpec(
  187. stc.STC_P_IDENTIFIER,
  188. "fore:#000000,face:%(helv)s,size:%(size)d" %
  189. self.faces)
  190. # Comment-blocks
  191. self.StyleSetSpec(
  192. stc.STC_P_COMMENTBLOCK,
  193. "fore:#7F7F7F,size:%(size)d" %
  194. self.faces)
  195. # End of line where string is not closed
  196. self.StyleSetSpec(
  197. stc.STC_P_STRINGEOL,
  198. "fore:#000000,face:%(mono)s,back:#E0C0E0,eol,size:%(size)d" %
  199. self.faces)
  200. self.SetCaretForeground("BLUE")
  201. def OnKeyPressed(self, event):
  202. """Key pressed
  203. .. todo::
  204. implement code completion (see wxPython demo)
  205. """
  206. if not self.modified:
  207. self.modified = True
  208. if self.statusbar:
  209. self.statusbar.SetStatusText(
  210. _('Python script contains local modifications'), 0)
  211. event.Skip()
  212. def OnUpdateUI(self, evt):
  213. # check for matching braces
  214. braceAtCaret = -1
  215. braceOpposite = -1
  216. charBefore = None
  217. caretPos = self.GetCurrentPos()
  218. if caretPos > 0:
  219. charBefore = self.GetCharAt(caretPos - 1)
  220. styleBefore = self.GetStyleAt(caretPos - 1)
  221. # check before
  222. if charBefore and chr(
  223. charBefore) in "[]{}()" and styleBefore == stc.STC_P_OPERATOR:
  224. braceAtCaret = caretPos - 1
  225. # check after
  226. if braceAtCaret < 0:
  227. charAfter = self.GetCharAt(caretPos)
  228. styleAfter = self.GetStyleAt(caretPos)
  229. if charAfter and chr(
  230. charAfter) in "[]{}()" and styleAfter == stc.STC_P_OPERATOR:
  231. braceAtCaret = caretPos
  232. if braceAtCaret >= 0:
  233. braceOpposite = self.BraceMatch(braceAtCaret)
  234. if braceAtCaret != -1 and braceOpposite == -1:
  235. self.BraceBadLight(braceAtCaret)
  236. else:
  237. self.BraceHighlight(braceAtCaret, braceOpposite)
  238. def OnMarginClick(self, evt):
  239. # fold and unfold as needed
  240. if evt.GetMargin() == 2:
  241. if evt.GetShift() and evt.GetControl():
  242. self.FoldAll()
  243. else:
  244. lineClicked = self.LineFromPosition(evt.GetPosition())
  245. if self.GetFoldLevel(
  246. lineClicked) & stc.STC_FOLDLEVELHEADERFLAG:
  247. if evt.GetShift():
  248. self.SetFoldExpanded(lineClicked, True)
  249. self.Expand(lineClicked, True, True, 1)
  250. elif evt.GetControl():
  251. if self.GetFoldExpanded(lineClicked):
  252. self.SetFoldExpanded(lineClicked, False)
  253. self.Expand(lineClicked, False, True, 0)
  254. else:
  255. self.SetFoldExpanded(lineClicked, True)
  256. self.Expand(lineClicked, True, True, 100)
  257. else:
  258. self.ToggleFold(lineClicked)
  259. def FoldAll(self):
  260. lineCount = self.GetLineCount()
  261. expanding = True
  262. # find out if we are folding or unfolding
  263. for lineNum in range(lineCount):
  264. if self.GetFoldLevel(lineNum) & stc.STC_FOLDLEVELHEADERFLAG:
  265. expanding = not self.GetFoldExpanded(lineNum)
  266. break
  267. lineNum = 0
  268. while lineNum < lineCount:
  269. level = self.GetFoldLevel(lineNum)
  270. if level & stc.STC_FOLDLEVELHEADERFLAG and \
  271. (level & stc.STC_FOLDLEVELNUMBERMASK) == stc.STC_FOLDLEVELBASE:
  272. if expanding:
  273. self.SetFoldExpanded(lineNum, True)
  274. lineNum = self.Expand(lineNum, True)
  275. lineNum = lineNum - 1
  276. else:
  277. lastChild = self.GetLastChild(lineNum, -1)
  278. self.SetFoldExpanded(lineNum, False)
  279. if lastChild > lineNum:
  280. self.HideLines(lineNum + 1, lastChild)
  281. lineNum = lineNum + 1
  282. def Expand(self, line, doExpand, force=False, visLevels=0, level=-1):
  283. lastChild = self.GetLastChild(line, level)
  284. line = line + 1
  285. while line <= lastChild:
  286. if force:
  287. if visLevels > 0:
  288. self.ShowLines(line, line)
  289. else:
  290. self.HideLines(line, line)
  291. else:
  292. if doExpand:
  293. self.ShowLines(line, line)
  294. if level == -1:
  295. level = self.GetFoldLevel(line)
  296. if level & stc.STC_FOLDLEVELHEADERFLAG:
  297. if force:
  298. if visLevels > 1:
  299. self.SetFoldExpanded(line, True)
  300. else:
  301. self.SetFoldExpanded(line, False)
  302. line = self.Expand(line, doExpand, force, visLevels - 1)
  303. else:
  304. if doExpand and self.GetFoldExpanded(line):
  305. line = self.Expand(line, True, force, visLevels - 1)
  306. else:
  307. line = self.Expand(line, False, force, visLevels - 1)
  308. else:
  309. line = line + 1
  310. return line