pystc.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. # Make some styles, the lexer defines what each style is used
  66. # for, we just have to define what each style looks like.
  67. # This set is adapted from Scintilla sample property files.
  68. # global default styles for all languages
  69. self.StyleSetSpec(stc.STC_STYLE_DEFAULT, "face:%(helv)s,size:%(size)d" % self.faces)
  70. self.StyleClearAll() # reset all to be like the default
  71. # global default styles for all languages
  72. self.StyleSetSpec(stc.STC_STYLE_DEFAULT, "face:%(helv)s,size:%(size)d" % self.faces)
  73. self.StyleSetSpec(stc.STC_STYLE_LINENUMBER, "back:#C0C0C0,face:%(helv)s,size:%(size2)d" % self.faces)
  74. self.StyleSetSpec(stc.STC_STYLE_CONTROLCHAR, "face:%(other)s" % self.faces)
  75. self.StyleSetSpec(stc.STC_STYLE_BRACELIGHT, "fore:#FFFFFF,back:#0000FF,bold")
  76. self.StyleSetSpec(stc.STC_STYLE_BRACEBAD, "fore:#000000,back:#FF0000,bold")
  77. # Python styles
  78. # Default
  79. self.StyleSetSpec(stc.STC_P_DEFAULT, "fore:#000000,face:%(helv)s,size:%(size)d" % self.faces)
  80. # Comments
  81. self.StyleSetSpec(stc.STC_P_COMMENTLINE, "fore:#007F00,face:%(other)s,size:%(size)d" % self.faces)
  82. # Number
  83. self.StyleSetSpec(stc.STC_P_NUMBER, "fore:#007F7F,size:%(size)d" % self.faces)
  84. # String
  85. self.StyleSetSpec(stc.STC_P_STRING, "fore:#7F007F,face:%(helv)s,size:%(size)d" % self.faces)
  86. # Single quoted string
  87. self.StyleSetSpec(stc.STC_P_CHARACTER, "fore:#7F007F,face:%(helv)s,size:%(size)d" % self.faces)
  88. # Keyword
  89. self.StyleSetSpec(stc.STC_P_WORD, "fore:#00007F,bold,size:%(size)d" % self.faces)
  90. # Triple quotes
  91. self.StyleSetSpec(stc.STC_P_TRIPLE, "fore:#7F0000,size:%(size)d" % self.faces)
  92. # Triple double quotes
  93. self.StyleSetSpec(stc.STC_P_TRIPLEDOUBLE, "fore:#7F0000,size:%(size)d" % self.faces)
  94. # Class name definition
  95. self.StyleSetSpec(stc.STC_P_CLASSNAME, "fore:#0000FF,bold,underline,size:%(size)d" % self.faces)
  96. # Function or method name definition
  97. self.StyleSetSpec(stc.STC_P_DEFNAME, "fore:#007F7F,bold,size:%(size)d" % self.faces)
  98. # Operators
  99. self.StyleSetSpec(stc.STC_P_OPERATOR, "bold,size:%(size)d" % self.faces)
  100. # Identifiers
  101. self.StyleSetSpec(stc.STC_P_IDENTIFIER, "fore:#000000,face:%(helv)s,size:%(size)d" % self.faces)
  102. # Comment-blocks
  103. self.StyleSetSpec(stc.STC_P_COMMENTBLOCK, "fore:#7F7F7F,size:%(size)d" % self.faces)
  104. # End of line where string is not closed
  105. self.StyleSetSpec(stc.STC_P_STRINGEOL, "fore:#000000,face:%(mono)s,back:#E0C0E0,eol,size:%(size)d" % self.faces)
  106. self.SetCaretForeground("BLUE")
  107. def OnKeyPressed(self, event):
  108. """Key pressed
  109. .. todo::
  110. implement code completion (see wxPython demo)
  111. """
  112. if not self.modified:
  113. self.modified = True
  114. if self.statusbar:
  115. self.statusbar.SetStatusText(_('Python script contains local modifications'), 0)
  116. event.Skip()
  117. def OnUpdateUI(self, evt):
  118. # check for matching braces
  119. braceAtCaret = -1
  120. braceOpposite = -1
  121. charBefore = None
  122. caretPos = self.GetCurrentPos()
  123. if caretPos > 0:
  124. charBefore = self.GetCharAt(caretPos - 1)
  125. styleBefore = self.GetStyleAt(caretPos - 1)
  126. # check before
  127. if charBefore and chr(charBefore) in "[]{}()" and styleBefore == stc.STC_P_OPERATOR:
  128. braceAtCaret = caretPos - 1
  129. # check after
  130. if braceAtCaret < 0:
  131. charAfter = self.GetCharAt(caretPos)
  132. styleAfter = self.GetStyleAt(caretPos)
  133. if charAfter and chr(charAfter) in "[]{}()" and styleAfter == stc.STC_P_OPERATOR:
  134. braceAtCaret = caretPos
  135. if braceAtCaret >= 0:
  136. braceOpposite = self.BraceMatch(braceAtCaret)
  137. if braceAtCaret != -1 and braceOpposite == -1:
  138. self.BraceBadLight(braceAtCaret)
  139. else:
  140. self.BraceHighlight(braceAtCaret, braceOpposite)
  141. def OnMarginClick(self, evt):
  142. # fold and unfold as needed
  143. if evt.GetMargin() == 2:
  144. if evt.GetShift() and evt.GetControl():
  145. self.FoldAll()
  146. else:
  147. lineClicked = self.LineFromPosition(evt.GetPosition())
  148. if self.GetFoldLevel(lineClicked) & stc.STC_FOLDLEVELHEADERFLAG:
  149. if evt.GetShift():
  150. self.SetFoldExpanded(lineClicked, True)
  151. self.Expand(lineClicked, True, True, 1)
  152. elif evt.GetControl():
  153. if self.GetFoldExpanded(lineClicked):
  154. self.SetFoldExpanded(lineClicked, False)
  155. self.Expand(lineClicked, False, True, 0)
  156. else:
  157. self.SetFoldExpanded(lineClicked, True)
  158. self.Expand(lineClicked, True, True, 100)
  159. else:
  160. self.ToggleFold(lineClicked)
  161. def FoldAll(self):
  162. lineCount = self.GetLineCount()
  163. expanding = True
  164. # find out if we are folding or unfolding
  165. for lineNum in range(lineCount):
  166. if self.GetFoldLevel(lineNum) & stc.STC_FOLDLEVELHEADERFLAG:
  167. expanding = not self.GetFoldExpanded(lineNum)
  168. break
  169. lineNum = 0
  170. while lineNum < lineCount:
  171. level = self.GetFoldLevel(lineNum)
  172. if level & stc.STC_FOLDLEVELHEADERFLAG and \
  173. (level & stc.STC_FOLDLEVELNUMBERMASK) == stc.STC_FOLDLEVELBASE:
  174. if expanding:
  175. self.SetFoldExpanded(lineNum, True)
  176. lineNum = self.Expand(lineNum, True)
  177. lineNum = lineNum - 1
  178. else:
  179. lastChild = self.GetLastChild(lineNum, -1)
  180. self.SetFoldExpanded(lineNum, False)
  181. if lastChild > lineNum:
  182. self.HideLines(lineNum+1, lastChild)
  183. lineNum = lineNum + 1
  184. def Expand(self, line, doExpand, force=False, visLevels=0, level=-1):
  185. lastChild = self.GetLastChild(line, level)
  186. line = line + 1
  187. while line <= lastChild:
  188. if force:
  189. if visLevels > 0:
  190. self.ShowLines(line, line)
  191. else:
  192. self.HideLines(line, line)
  193. else:
  194. if doExpand:
  195. self.ShowLines(line, line)
  196. if level == -1:
  197. level = self.GetFoldLevel(line)
  198. if level & stc.STC_FOLDLEVELHEADERFLAG:
  199. if force:
  200. if visLevels > 1:
  201. self.SetFoldExpanded(line, True)
  202. else:
  203. self.SetFoldExpanded(line, False)
  204. line = self.Expand(line, doExpand, force, visLevels-1)
  205. else:
  206. if doExpand and self.GetFoldExpanded(line):
  207. line = self.Expand(line, True, force, visLevels-1)
  208. else:
  209. line = self.Expand(line, False, force, visLevels-1)
  210. else:
  211. line = line + 1
  212. return line