9 #---------------------------------------------------------------------- 
  12 ## This version of the editor has been set up to edit Python source 
  13 ## code.  Here is a copy of wxPython/demo/Main.py to play with. 
  18 #---------------------------------------------------------------------- 
  21 if wx
.Platform 
== '__WXMSW__': 
  22     faces 
= { 'times': 'Times New Roman', 
  23               'mono' : 'Courier New', 
  25               'other': 'Comic Sans MS', 
  30     faces 
= { 'times': 'Times', 
  33               'other': 'new century schoolbook', 
  39 #---------------------------------------------------------------------- 
  41 class PythonSTC(stc
.StyledTextCtrl
): 
  45     def __init__(self
, parent
, ID
, 
  46                  pos
=wx
.DefaultPosition
, size
=wx
.DefaultSize
, 
  48         stc
.StyledTextCtrl
.__init
__(self
, parent
, ID
, pos
, size
, style
) 
  50         self
.CmdKeyAssign(ord('B'), stc
.STC_SCMOD_CTRL
, stc
.STC_CMD_ZOOMIN
) 
  51         self
.CmdKeyAssign(ord('N'), stc
.STC_SCMOD_CTRL
, stc
.STC_CMD_ZOOMOUT
) 
  53         self
.SetLexer(stc
.STC_LEX_PYTHON
) 
  54         self
.SetKeyWords(0, " ".join(keyword
.kwlist
)) 
  56         self
.SetProperty("fold", "1") 
  57         self
.SetProperty("tab.timmy.whinge.level", "1") 
  60         self
.SetViewWhiteSpace(False) 
  61         #self.SetBufferedDraw(False) 
  62         #self.SetViewEOL(True) 
  63         #self.SetEOLMode(stc.STC_EOL_CRLF) 
  64         #self.SetUseAntiAliasing(True) 
  66         self
.SetEdgeMode(stc
.STC_EDGE_BACKGROUND
) 
  67         self
.SetEdgeColumn(78) 
  69         # Setup a margin to hold fold markers 
  70         #self.SetFoldFlags(16)  ###  WHAT IS THIS VALUE?  WHAT ARE THE OTHER FLAGS?  DOES IT MATTER? 
  71         self
.SetMarginType(2, stc
.STC_MARGIN_SYMBOL
) 
  72         self
.SetMarginMask(2, stc
.STC_MASK_FOLDERS
) 
  73         self
.SetMarginSensitive(2, True) 
  74         self
.SetMarginWidth(2, 12) 
  76         if self
.fold_symbols 
== 0: 
  77             # Arrow pointing right for contracted folders, arrow pointing down for expanded 
  78             self
.MarkerDefine(stc
.STC_MARKNUM_FOLDEROPEN
,    stc
.STC_MARK_ARROWDOWN
, "black", "black") 
  79             self
.MarkerDefine(stc
.STC_MARKNUM_FOLDER
,        stc
.STC_MARK_ARROW
, "black", "black") 
  80             self
.MarkerDefine(stc
.STC_MARKNUM_FOLDERSUB
,     stc
.STC_MARK_EMPTY
, "black", "black") 
  81             self
.MarkerDefine(stc
.STC_MARKNUM_FOLDERTAIL
,    stc
.STC_MARK_EMPTY
, "black", "black") 
  82             self
.MarkerDefine(stc
.STC_MARKNUM_FOLDEREND
,     stc
.STC_MARK_EMPTY
,     "white", "black") 
  83             self
.MarkerDefine(stc
.STC_MARKNUM_FOLDEROPENMID
, stc
.STC_MARK_EMPTY
,     "white", "black") 
  84             self
.MarkerDefine(stc
.STC_MARKNUM_FOLDERMIDTAIL
, stc
.STC_MARK_EMPTY
,     "white", "black") 
  86         elif self
.fold_symbols 
== 1: 
  87             # Plus for contracted folders, minus for expanded 
  88             self
.MarkerDefine(stc
.STC_MARKNUM_FOLDEROPEN
,    stc
.STC_MARK_MINUS
, "white", "black") 
  89             self
.MarkerDefine(stc
.STC_MARKNUM_FOLDER
,        stc
.STC_MARK_PLUS
,  "white", "black") 
  90             self
.MarkerDefine(stc
.STC_MARKNUM_FOLDERSUB
,     stc
.STC_MARK_EMPTY
, "white", "black") 
  91             self
.MarkerDefine(stc
.STC_MARKNUM_FOLDERTAIL
,    stc
.STC_MARK_EMPTY
, "white", "black") 
  92             self
.MarkerDefine(stc
.STC_MARKNUM_FOLDEREND
,     stc
.STC_MARK_EMPTY
, "white", "black") 
  93             self
.MarkerDefine(stc
.STC_MARKNUM_FOLDEROPENMID
, stc
.STC_MARK_EMPTY
, "white", "black") 
  94             self
.MarkerDefine(stc
.STC_MARKNUM_FOLDERMIDTAIL
, stc
.STC_MARK_EMPTY
, "white", "black") 
  96         elif self
.fold_symbols 
== 2: 
  97             # Like a flattened tree control using circular headers and curved joins 
  98             self
.MarkerDefine(stc
.STC_MARKNUM_FOLDEROPEN
,    stc
.STC_MARK_CIRCLEMINUS
,          "white", "#404040") 
  99             self
.MarkerDefine(stc
.STC_MARKNUM_FOLDER
,        stc
.STC_MARK_CIRCLEPLUS
,           "white", "#404040") 
 100             self
.MarkerDefine(stc
.STC_MARKNUM_FOLDERSUB
,     stc
.STC_MARK_VLINE
,                "white", "#404040") 
 101             self
.MarkerDefine(stc
.STC_MARKNUM_FOLDERTAIL
,    stc
.STC_MARK_LCORNERCURVE
,         "white", "#404040") 
 102             self
.MarkerDefine(stc
.STC_MARKNUM_FOLDEREND
,     stc
.STC_MARK_CIRCLEPLUSCONNECTED
,  "white", "#404040") 
 103             self
.MarkerDefine(stc
.STC_MARKNUM_FOLDEROPENMID
, stc
.STC_MARK_CIRCLEMINUSCONNECTED
, "white", "#404040") 
 104             self
.MarkerDefine(stc
.STC_MARKNUM_FOLDERMIDTAIL
, stc
.STC_MARK_TCORNERCURVE
,         "white", "#404040") 
 106         elif self
.fold_symbols 
== 3: 
 107             # Like a flattened tree control using square headers 
 108             self
.MarkerDefine(stc
.STC_MARKNUM_FOLDEROPEN
,    stc
.STC_MARK_BOXMINUS
,          "white", "#808080") 
 109             self
.MarkerDefine(stc
.STC_MARKNUM_FOLDER
,        stc
.STC_MARK_BOXPLUS
,           "white", "#808080") 
 110             self
.MarkerDefine(stc
.STC_MARKNUM_FOLDERSUB
,     stc
.STC_MARK_VLINE
,             "white", "#808080") 
 111             self
.MarkerDefine(stc
.STC_MARKNUM_FOLDERTAIL
,    stc
.STC_MARK_LCORNER
,           "white", "#808080") 
 112             self
.MarkerDefine(stc
.STC_MARKNUM_FOLDEREND
,     stc
.STC_MARK_BOXPLUSCONNECTED
,  "white", "#808080") 
 113             self
.MarkerDefine(stc
.STC_MARKNUM_FOLDEROPENMID
, stc
.STC_MARK_BOXMINUSCONNECTED
, "white", "#808080") 
 114             self
.MarkerDefine(stc
.STC_MARKNUM_FOLDERMIDTAIL
, stc
.STC_MARK_TCORNER
,           "white", "#808080") 
 117         self
.Bind(stc
.EVT_STC_UPDATEUI
, self
.OnUpdateUI
) 
 118         self
.Bind(stc
.EVT_STC_MARGINCLICK
, self
.OnMarginClick
) 
 119         self
.Bind(wx
.EVT_KEY_DOWN
, self
.OnKeyPressed
) 
 121         # Make some styles,  The lexer defines what each style is used for, we 
 122         # just have to define what each style looks like.  This set is adapted from 
 123         # Scintilla sample property files. 
 125         # Global default styles for all languages 
 126         self
.StyleSetSpec(stc
.STC_STYLE_DEFAULT
,     "face:%(helv)s,size:%(size)d" % faces
) 
 127         self
.StyleClearAll()  # Reset all to be like the default 
 129         # Global default styles for all languages 
 130         self
.StyleSetSpec(stc
.STC_STYLE_DEFAULT
,     "face:%(helv)s,size:%(size)d" % faces
) 
 131         self
.StyleSetSpec(stc
.STC_STYLE_LINENUMBER
,  "back:#C0C0C0,face:%(helv)s,size:%(size2)d" % faces
) 
 132         self
.StyleSetSpec(stc
.STC_STYLE_CONTROLCHAR
, "face:%(other)s" % faces
) 
 133         self
.StyleSetSpec(stc
.STC_STYLE_BRACELIGHT
,  "fore:#FFFFFF,back:#0000FF,bold") 
 134         self
.StyleSetSpec(stc
.STC_STYLE_BRACEBAD
,    "fore:#000000,back:#FF0000,bold") 
 138         self
.StyleSetSpec(stc
.STC_P_DEFAULT
, "fore:#000000,face:%(helv)s,size:%(size)d" % faces
) 
 140         self
.StyleSetSpec(stc
.STC_P_COMMENTLINE
, "fore:#007F00,face:%(other)s,size:%(size)d" % faces
) 
 142         self
.StyleSetSpec(stc
.STC_P_NUMBER
, "fore:#007F7F,size:%(size)d" % faces
) 
 144         self
.StyleSetSpec(stc
.STC_P_STRING
, "fore:#7F007F,face:%(helv)s,size:%(size)d" % faces
) 
 145         # Single quoted string 
 146         self
.StyleSetSpec(stc
.STC_P_CHARACTER
, "fore:#7F007F,face:%(helv)s,size:%(size)d" % faces
) 
 148         self
.StyleSetSpec(stc
.STC_P_WORD
, "fore:#00007F,bold,size:%(size)d" % faces
) 
 150         self
.StyleSetSpec(stc
.STC_P_TRIPLE
, "fore:#7F0000,size:%(size)d" % faces
) 
 151         # Triple double quotes 
 152         self
.StyleSetSpec(stc
.STC_P_TRIPLEDOUBLE
, "fore:#7F0000,size:%(size)d" % faces
) 
 153         # Class name definition 
 154         self
.StyleSetSpec(stc
.STC_P_CLASSNAME
, "fore:#0000FF,bold,underline,size:%(size)d" % faces
) 
 155         # Function or method name definition 
 156         self
.StyleSetSpec(stc
.STC_P_DEFNAME
, "fore:#007F7F,bold,size:%(size)d" % faces
) 
 158         self
.StyleSetSpec(stc
.STC_P_OPERATOR
, "bold,size:%(size)d" % faces
) 
 160         self
.StyleSetSpec(stc
.STC_P_IDENTIFIER
, "fore:#000000,face:%(helv)s,size:%(size)d" % faces
) 
 162         self
.StyleSetSpec(stc
.STC_P_COMMENTBLOCK
, "fore:#7F7F7F,size:%(size)d" % faces
) 
 163         # End of line where string is not closed 
 164         self
.StyleSetSpec(stc
.STC_P_STRINGEOL
, "fore:#000000,face:%(mono)s,back:#E0C0E0,eol,size:%(size)d" % faces
) 
 166         self
.SetCaretForeground("BLUE") 
 169         # register some images for use in the AutoComplete box. 
 170         self
.RegisterImage(1, images
.getSmilesBitmap()) 
 171         self
.RegisterImage(2,  
 172             wx
.ArtProvider
.GetBitmap(wx
.ART_NEW
, size
=(16,16))) 
 173         self
.RegisterImage(3,  
 174             wx
.ArtProvider
.GetBitmap(wx
.ART_COPY
, size
=(16,16))) 
 177     def OnKeyPressed(self
, event
): 
 178         if self
.CallTipActive(): 
 180         key 
= event
.KeyCode() 
 182         if key 
== 32 and event
.ControlDown(): 
 183             pos 
= self
.GetCurrentPos() 
 186             if event
.ShiftDown(): 
 187                 self
.CallTipSetBackground("yellow") 
 188                 self
.CallTipShow(pos
, 'lots of of text: blah, blah, blah\n\n' 
 189                                  'show some suff, maybe parameters..\n\n' 
 190                                  'fubar(param1, param2)') 
 194                 #for x in range(50000): 
 195                 #    lst.append('%05d' % x) 
 198                 #self.AutoCompShow(0, st) 
 200                 kw 
= keyword
.kwlist
[:] 
 201                 kw
.append("zzzzzz?2") 
 203                 kw
.append("__init__?3") 
 204                 kw
.append("zzaaaaa?2") 
 205                 kw
.append("zzbaaaa?2") 
 206                 kw
.append("this_is_a_longer_value") 
 207                 #kw.append("this_is_a_much_much_much_much_much_much_much_longer_value") 
 209                 kw
.sort()  # Python sorts are case sensitive 
 210                 self
.AutoCompSetIgnoreCase(False)  # so this needs to match 
 212                 # Images are specified with a appended "?type" 
 213                 for i 
in range(len(kw
)): 
 214                     if kw
[i
] in keyword
.kwlist
: 
 217                 self
.AutoCompShow(0, " ".join(kw
)) 
 222     def OnUpdateUI(self
, evt
): 
 223         # check for matching braces 
 227         caretPos 
= self
.GetCurrentPos() 
 230             charBefore 
= self
.GetCharAt(caretPos 
- 1) 
 231             styleBefore 
= self
.GetStyleAt(caretPos 
- 1) 
 234         if charBefore 
and chr(charBefore
) in "[]{}()" and styleBefore 
== stc
.STC_P_OPERATOR
: 
 235             braceAtCaret 
= caretPos 
- 1 
 239             charAfter 
= self
.GetCharAt(caretPos
) 
 240             styleAfter 
= self
.GetStyleAt(caretPos
) 
 242             if charAfter 
and chr(charAfter
) in "[]{}()" and styleAfter 
== stc
.STC_P_OPERATOR
: 
 243                 braceAtCaret 
= caretPos
 
 245         if braceAtCaret 
>= 0: 
 246             braceOpposite 
= self
.BraceMatch(braceAtCaret
) 
 248         if braceAtCaret 
!= -1  and braceOpposite 
== -1: 
 249             self
.BraceBadLight(braceAtCaret
) 
 251             self
.BraceHighlight(braceAtCaret
, braceOpposite
) 
 252             #pt = self.PointFromPosition(braceOpposite) 
 253             #self.Refresh(True, wxRect(pt.x, pt.y, 5,5)) 
 258     def OnMarginClick(self
, evt
): 
 259         # fold and unfold as needed 
 260         if evt
.GetMargin() == 2: 
 261             if evt
.GetShift() and evt
.GetControl(): 
 264                 lineClicked 
= self
.LineFromPosition(evt
.GetPosition()) 
 266                 if self
.GetFoldLevel(lineClicked
) & stc
.STC_FOLDLEVELHEADERFLAG
: 
 268                         self
.SetFoldExpanded(lineClicked
, True) 
 269                         self
.Expand(lineClicked
, True, True, 1) 
 270                     elif evt
.GetControl(): 
 271                         if self
.GetFoldExpanded(lineClicked
): 
 272                             self
.SetFoldExpanded(lineClicked
, False) 
 273                             self
.Expand(lineClicked
, False, True, 0) 
 275                             self
.SetFoldExpanded(lineClicked
, True) 
 276                             self
.Expand(lineClicked
, True, True, 100) 
 278                         self
.ToggleFold(lineClicked
) 
 282         lineCount 
= self
.GetLineCount() 
 285         # find out if we are folding or unfolding 
 286         for lineNum 
in range(lineCount
): 
 287             if self
.GetFoldLevel(lineNum
) & stc
.STC_FOLDLEVELHEADERFLAG
: 
 288                 expanding 
= not self
.GetFoldExpanded(lineNum
) 
 293         while lineNum 
< lineCount
: 
 294             level 
= self
.GetFoldLevel(lineNum
) 
 295             if level 
& stc
.STC_FOLDLEVELHEADERFLAG 
and \
 
 296                (level 
& stc
.STC_FOLDLEVELNUMBERMASK
) == stc
.STC_FOLDLEVELBASE
: 
 299                     self
.SetFoldExpanded(lineNum
, True) 
 300                     lineNum 
= self
.Expand(lineNum
, True) 
 301                     lineNum 
= lineNum 
- 1 
 303                     lastChild 
= self
.GetLastChild(lineNum
, -1) 
 304                     self
.SetFoldExpanded(lineNum
, False) 
 306                     if lastChild 
> lineNum
: 
 307                         self
.HideLines(lineNum
+1, lastChild
) 
 309             lineNum 
= lineNum 
+ 1 
 313     def Expand(self
, line
, doExpand
, force
=False, visLevels
=0, level
=-1): 
 314         lastChild 
= self
.GetLastChild(line
, level
) 
 317         while line 
<= lastChild
: 
 320                     self
.ShowLines(line
, line
) 
 322                     self
.HideLines(line
, line
) 
 325                     self
.ShowLines(line
, line
) 
 328                 level 
= self
.GetFoldLevel(line
) 
 330             if level 
& stc
.STC_FOLDLEVELHEADERFLAG
: 
 333                         self
.SetFoldExpanded(line
, True) 
 335                         self
.SetFoldExpanded(line
, False) 
 337                     line 
= self
.Expand(line
, doExpand
, force
, visLevels
-1) 
 340                     if doExpand 
and self
.GetFoldExpanded(line
): 
 341                         line 
= self
.Expand(line
, True, force
, visLevels
-1) 
 343                         line 
= self
.Expand(line
, False, force
, visLevels
-1) 
 350 #---------------------------------------------------------------------- 
 354 def runTest(frame
, nb
, log
): 
 356         ed 
= p 
= PythonSTC(nb
, -1) 
 358         p 
= wx
.Panel(nb
, -1, style 
= wx
.NO_FULL_REPAINT_ON_RESIZE
) 
 359         ed 
= PythonSTC(p
, -1) 
 360         s 
= wx
.BoxSizer(wx
.HORIZONTAL
) 
 361         s
.Add(ed
, 1, wx
.EXPAND
) 
 363         p
.SetAutoLayout(True) 
 366     ed
.SetText(demoText 
+ open('Main.py').read()) 
 370     # line numbers in the margin 
 371     ed
.SetMarginType(1, stc
.STC_MARGIN_NUMBER
) 
 372     ed
.SetMarginWidth(1, 25) 
 378 #---------------------------------------------------------------------- 
 383 Once again, no docs yet.  <b>Sorry.</b>  But <a href="data/stc.h.html">this</a> 
 384 and <a href="http://www.scintilla.org/ScintillaDoc.html">this</a> should 
 390 if __name__ 
== '__main__': 
 393     run
.main(['', os
.path
.basename(sys
.argv
[0])] + sys
.argv
[1:]) 
 399 #---------------------------------------------------------------------- 
 400 #----------------------------------------------------------------------