pystc.py 12 KB

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