pystc.py 11 KB

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