plugins/python/PythonEditor.py
changeset 366 cd90e4c10261
child 415 339fa2542481
equal deleted inserted replaced
365:a7f58414dea0 366:cd90e4c10261
       
     1 import  wx, wx.grid
       
     2 import  wx.stc  as  stc
       
     3 import keyword
       
     4 
       
     5 if wx.Platform == '__WXMSW__':
       
     6     faces = { 'times': 'Times New Roman',
       
     7               'mono' : 'Courier New',
       
     8               'helv' : 'Arial',
       
     9               'other': 'Comic Sans MS',
       
    10               'size' : 10,
       
    11               'size2': 8,
       
    12              }
       
    13 elif wx.Platform == '__WXMAC__':
       
    14     faces = { 'times': 'Times New Roman',
       
    15               'mono' : 'Monaco',
       
    16               'helv' : 'Arial',
       
    17               'other': 'Comic Sans MS',
       
    18               'size' : 12,
       
    19               'size2': 10,
       
    20              }
       
    21 else:
       
    22     faces = { 'times': 'Times',
       
    23               'mono' : 'Courier',
       
    24               'helv' : 'Helvetica',
       
    25               'other': 'new century schoolbook',
       
    26               'size' : 12,
       
    27               'size2': 10,
       
    28              }
       
    29 
       
    30 def AppendMenu(parent, help, id, kind, text):
       
    31     if wx.VERSION >= (2, 6, 0):
       
    32         parent.Append(help=help, id=id, kind=kind, text=text)
       
    33     else:
       
    34         parent.Append(helpString=help, id=id, kind=kind, item=text)
       
    35 
       
    36 
       
    37 [ID_PYTHONEDITOR,
       
    38 ] = [wx.NewId() for _init_ctrls in range(1)]
       
    39 
       
    40 def GetCursorPos(old, new):
       
    41     old_length = len(old)
       
    42     new_length = len(new)
       
    43     common_length = min(old_length, new_length)
       
    44     i = 0
       
    45     for i in xrange(common_length):
       
    46         if old[i] != new[i]:
       
    47             break
       
    48     if old_length < new_length:
       
    49         if common_length > 0 and old[i] != new[i]:
       
    50             return i + new_length - old_length
       
    51         else:
       
    52             return i + new_length - old_length + 1
       
    53     elif old_length > new_length or i < min(old_length, new_length) - 1:
       
    54         if common_length > 0 and old[i] != new[i]:
       
    55             return i
       
    56         else:
       
    57             return i + 1
       
    58     else:
       
    59         return None
       
    60 
       
    61 class PythonEditor(stc.StyledTextCtrl):
       
    62 
       
    63     fold_symbols = 3
       
    64     
       
    65     def __init__(self, parent, window, controler):
       
    66         stc.StyledTextCtrl.__init__(self, parent, ID_PYTHONEDITOR, wx.DefaultPosition, 
       
    67                  wx.DefaultSize, 0)
       
    68 
       
    69         self.CmdKeyAssign(ord('B'), stc.STC_SCMOD_CTRL, stc.STC_CMD_ZOOMIN)
       
    70         self.CmdKeyAssign(ord('N'), stc.STC_SCMOD_CTRL, stc.STC_CMD_ZOOMOUT)
       
    71 
       
    72         self.SetLexer(stc.STC_LEX_PYTHON)
       
    73         self.SetKeyWords(0, " ".join(keyword.kwlist))
       
    74 
       
    75         self.SetProperty("fold", "1")
       
    76         self.SetProperty("tab.timmy.whinge.level", "1")
       
    77         self.SetMargins(0,0)
       
    78 
       
    79         self.SetViewWhiteSpace(False)
       
    80         #self.SetBufferedDraw(False)
       
    81         #self.SetViewEOL(True)
       
    82         #self.SetEOLMode(stc.STC_EOL_CRLF)
       
    83         #self.SetUseAntiAliasing(True)
       
    84         
       
    85         self.SetEdgeMode(stc.STC_EDGE_BACKGROUND)
       
    86         self.SetEdgeColumn(78)
       
    87 
       
    88         # Set up the numbers in the margin for margin #1
       
    89         self.SetMarginType(1, wx.stc.STC_MARGIN_NUMBER)
       
    90         # Reasonable value for, say, 4-5 digits using a mono font (40 pix)
       
    91         self.SetMarginWidth(1, 40)
       
    92 
       
    93         # Setup a margin to hold fold markers
       
    94         self.SetMarginType(2, stc.STC_MARGIN_SYMBOL)
       
    95         self.SetMarginMask(2, stc.STC_MASK_FOLDERS)
       
    96         self.SetMarginSensitive(2, True)
       
    97         self.SetMarginWidth(2, 12)
       
    98 
       
    99         if self.fold_symbols == 0:
       
   100             # Arrow pointing right for contracted folders, arrow pointing down for expanded
       
   101             self.MarkerDefine(stc.STC_MARKNUM_FOLDEROPEN,    stc.STC_MARK_ARROWDOWN, "black", "black")
       
   102             self.MarkerDefine(stc.STC_MARKNUM_FOLDER,        stc.STC_MARK_ARROW, "black", "black")
       
   103             self.MarkerDefine(stc.STC_MARKNUM_FOLDERSUB,     stc.STC_MARK_EMPTY, "black", "black")
       
   104             self.MarkerDefine(stc.STC_MARKNUM_FOLDERTAIL,    stc.STC_MARK_EMPTY, "black", "black")
       
   105             self.MarkerDefine(stc.STC_MARKNUM_FOLDEREND,     stc.STC_MARK_EMPTY,     "white", "black")
       
   106             self.MarkerDefine(stc.STC_MARKNUM_FOLDEROPENMID, stc.STC_MARK_EMPTY,     "white", "black")
       
   107             self.MarkerDefine(stc.STC_MARKNUM_FOLDERMIDTAIL, stc.STC_MARK_EMPTY,     "white", "black")
       
   108             
       
   109         elif self.fold_symbols == 1:
       
   110             # Plus for contracted folders, minus for expanded
       
   111             self.MarkerDefine(stc.STC_MARKNUM_FOLDEROPEN,    stc.STC_MARK_MINUS, "white", "black")
       
   112             self.MarkerDefine(stc.STC_MARKNUM_FOLDER,        stc.STC_MARK_PLUS,  "white", "black")
       
   113             self.MarkerDefine(stc.STC_MARKNUM_FOLDERSUB,     stc.STC_MARK_EMPTY, "white", "black")
       
   114             self.MarkerDefine(stc.STC_MARKNUM_FOLDERTAIL,    stc.STC_MARK_EMPTY, "white", "black")
       
   115             self.MarkerDefine(stc.STC_MARKNUM_FOLDEREND,     stc.STC_MARK_EMPTY, "white", "black")
       
   116             self.MarkerDefine(stc.STC_MARKNUM_FOLDEROPENMID, stc.STC_MARK_EMPTY, "white", "black")
       
   117             self.MarkerDefine(stc.STC_MARKNUM_FOLDERMIDTAIL, stc.STC_MARK_EMPTY, "white", "black")
       
   118 
       
   119         elif self.fold_symbols == 2:
       
   120             # Like a flattened tree control using circular headers and curved joins
       
   121             self.MarkerDefine(stc.STC_MARKNUM_FOLDEROPEN,    stc.STC_MARK_CIRCLEMINUS,          "white", "#404040")
       
   122             self.MarkerDefine(stc.STC_MARKNUM_FOLDER,        stc.STC_MARK_CIRCLEPLUS,           "white", "#404040")
       
   123             self.MarkerDefine(stc.STC_MARKNUM_FOLDERSUB,     stc.STC_MARK_VLINE,                "white", "#404040")
       
   124             self.MarkerDefine(stc.STC_MARKNUM_FOLDERTAIL,    stc.STC_MARK_LCORNERCURVE,         "white", "#404040")
       
   125             self.MarkerDefine(stc.STC_MARKNUM_FOLDEREND,     stc.STC_MARK_CIRCLEPLUSCONNECTED,  "white", "#404040")
       
   126             self.MarkerDefine(stc.STC_MARKNUM_FOLDEROPENMID, stc.STC_MARK_CIRCLEMINUSCONNECTED, "white", "#404040")
       
   127             self.MarkerDefine(stc.STC_MARKNUM_FOLDERMIDTAIL, stc.STC_MARK_TCORNERCURVE,         "white", "#404040")
       
   128 
       
   129         elif self.fold_symbols == 3:
       
   130             # Like a flattened tree control using square headers
       
   131             self.MarkerDefine(stc.STC_MARKNUM_FOLDEROPEN,    stc.STC_MARK_BOXMINUS,          "white", "#808080")
       
   132             self.MarkerDefine(stc.STC_MARKNUM_FOLDER,        stc.STC_MARK_BOXPLUS,           "white", "#808080")
       
   133             self.MarkerDefine(stc.STC_MARKNUM_FOLDERSUB,     stc.STC_MARK_VLINE,             "white", "#808080")
       
   134             self.MarkerDefine(stc.STC_MARKNUM_FOLDERTAIL,    stc.STC_MARK_LCORNER,           "white", "#808080")
       
   135             self.MarkerDefine(stc.STC_MARKNUM_FOLDEREND,     stc.STC_MARK_BOXPLUSCONNECTED,  "white", "#808080")
       
   136             self.MarkerDefine(stc.STC_MARKNUM_FOLDEROPENMID, stc.STC_MARK_BOXMINUSCONNECTED, "white", "#808080")
       
   137             self.MarkerDefine(stc.STC_MARKNUM_FOLDERMIDTAIL, stc.STC_MARK_TCORNER,           "white", "#808080")
       
   138 
       
   139 
       
   140         self.Bind(stc.EVT_STC_UPDATEUI, self.OnUpdateUI)
       
   141         self.Bind(stc.EVT_STC_MARGINCLICK, self.OnMarginClick)
       
   142         self.Bind(wx.EVT_KEY_DOWN, self.OnKeyPressed)
       
   143 
       
   144         # Global default style
       
   145         if wx.Platform == '__WXMSW__':
       
   146             self.StyleSetSpec(stc.STC_STYLE_DEFAULT, 
       
   147                               'fore:#000000,back:#FFFFFF,face:Courier New')
       
   148         elif wx.Platform == '__WXMAC__':
       
   149             # TODO: if this looks fine on Linux too, remove the Mac-specific case 
       
   150             # and use this whenever OS != MSW.
       
   151             self.StyleSetSpec(stc.STC_STYLE_DEFAULT, 
       
   152                               'fore:#000000,back:#FFFFFF,face:Monaco')
       
   153         else:
       
   154             defsize = wx.SystemSettings.GetFont(wx.SYS_ANSI_FIXED_FONT).GetPointSize()
       
   155             self.StyleSetSpec(stc.STC_STYLE_DEFAULT, 
       
   156                               'fore:#000000,back:#FFFFFF,face:Courier,size:%d'%defsize)
       
   157 
       
   158         # Clear styles and revert to default.
       
   159         self.StyleClearAll()
       
   160 
       
   161         # Following style specs only indicate differences from default.
       
   162         # The rest remains unchanged.
       
   163 
       
   164         # Line numbers in margin
       
   165         self.StyleSetSpec(wx.stc.STC_STYLE_LINENUMBER,'fore:#000000,back:#99A9C2')    
       
   166         # Highlighted brace
       
   167         self.StyleSetSpec(wx.stc.STC_STYLE_BRACELIGHT,'fore:#00009D,back:#FFFF00')
       
   168         # Unmatched brace
       
   169         self.StyleSetSpec(wx.stc.STC_STYLE_BRACEBAD,'fore:#00009D,back:#FF0000')
       
   170         # Indentation guide
       
   171         self.StyleSetSpec(wx.stc.STC_STYLE_INDENTGUIDE, "fore:#CDCDCD")
       
   172 
       
   173         # Python styles
       
   174         self.StyleSetSpec(wx.stc.STC_P_DEFAULT, 'fore:#000000')
       
   175         # Comments
       
   176         self.StyleSetSpec(wx.stc.STC_P_COMMENTLINE,  'fore:#008000,back:#F0FFF0')
       
   177         self.StyleSetSpec(wx.stc.STC_P_COMMENTBLOCK, 'fore:#008000,back:#F0FFF0')
       
   178         # Numbers
       
   179         self.StyleSetSpec(wx.stc.STC_P_NUMBER, 'fore:#008080')
       
   180         # Strings and characters
       
   181         self.StyleSetSpec(wx.stc.STC_P_STRING, 'fore:#800080')
       
   182         self.StyleSetSpec(wx.stc.STC_P_CHARACTER, 'fore:#800080')
       
   183         # Keywords
       
   184         self.StyleSetSpec(wx.stc.STC_P_WORD, 'fore:#000080,bold')
       
   185         # Triple quotes
       
   186         self.StyleSetSpec(wx.stc.STC_P_TRIPLE, 'fore:#800080,back:#FFFFEA')
       
   187         self.StyleSetSpec(wx.stc.STC_P_TRIPLEDOUBLE, 'fore:#800080,back:#FFFFEA')
       
   188         # Class names
       
   189         self.StyleSetSpec(wx.stc.STC_P_CLASSNAME, 'fore:#0000FF,bold')
       
   190         # Function names
       
   191         self.StyleSetSpec(wx.stc.STC_P_DEFNAME, 'fore:#008080,bold')
       
   192         # Operators
       
   193         self.StyleSetSpec(wx.stc.STC_P_OPERATOR, 'fore:#800000,bold')
       
   194         # Identifiers. I leave this as not bold because everything seems
       
   195         # to be an identifier if it doesn't match the above criterae
       
   196         self.StyleSetSpec(wx.stc.STC_P_IDENTIFIER, 'fore:#000000')
       
   197 
       
   198         # Caret color
       
   199         self.SetCaretForeground("BLUE")
       
   200         # Selection background
       
   201         self.SetSelBackground(1, '#66CCFF')
       
   202 
       
   203         self.SetSelBackground(True, wx.SystemSettings_GetColour(wx.SYS_COLOUR_HIGHLIGHT))
       
   204         self.SetSelForeground(True, wx.SystemSettings_GetColour(wx.SYS_COLOUR_HIGHLIGHTTEXT))
       
   205         
       
   206         # register some images for use in the AutoComplete box.
       
   207         #self.RegisterImage(1, images.getSmilesBitmap())
       
   208         self.RegisterImage(1, 
       
   209             wx.ArtProvider.GetBitmap(wx.ART_DELETE, size=(16,16)))
       
   210         self.RegisterImage(2, 
       
   211             wx.ArtProvider.GetBitmap(wx.ART_NEW, size=(16,16)))
       
   212         self.RegisterImage(3, 
       
   213             wx.ArtProvider.GetBitmap(wx.ART_COPY, size=(16,16)))
       
   214 
       
   215         # Indentation and tab stuff
       
   216         self.SetIndent(4)               # Proscribed indent size for wx
       
   217         self.SetIndentationGuides(True) # Show indent guides
       
   218         self.SetBackSpaceUnIndents(True)# Backspace unindents rather than delete 1 space
       
   219         self.SetTabIndents(True)        # Tab key indents
       
   220         self.SetTabWidth(4)             # Proscribed tab size for wx
       
   221         self.SetUseTabs(False)          # Use spaces rather than tabs, or
       
   222                                         # TabTimmy will complain!    
       
   223         # White space
       
   224         self.SetViewWhiteSpace(False)   # Don't view white space
       
   225 
       
   226         # EOL: Since we are loading/saving ourselves, and the
       
   227         # strings will always have \n's in them, set the STC to
       
   228         # edit them that way.            
       
   229         self.SetEOLMode(wx.stc.STC_EOL_LF)
       
   230         self.SetViewEOL(False)
       
   231         
       
   232         # No right-edge mode indicator
       
   233         self.SetEdgeMode(stc.STC_EDGE_NONE)
       
   234 
       
   235         self.Controler = controler
       
   236         self.ParentWindow = window
       
   237         
       
   238         self.DisableEvents = True
       
   239         self.CurrentAction = None
       
   240         
       
   241         self.SetModEventMask(wx.stc.STC_MOD_BEFOREINSERT|wx.stc.STC_MOD_BEFOREDELETE)
       
   242 
       
   243         self.Bind(wx.stc.EVT_STC_DO_DROP, self.OnDoDrop, id=ID_PYTHONEDITOR)
       
   244         self.Bind(wx.EVT_KILL_FOCUS, self.OnKillFocus)
       
   245         self.Bind(wx.stc.EVT_STC_MODIFIED, self.OnModification, id=ID_PYTHONEDITOR)
       
   246     
       
   247     def OnModification(self, event):
       
   248         if not self.DisableEvents:
       
   249             mod_type = event.GetModificationType()
       
   250             if not (mod_type&wx.stc.STC_PERFORMED_UNDO or mod_type&wx.stc.STC_PERFORMED_REDO):
       
   251                 if mod_type&wx.stc.STC_MOD_BEFOREINSERT:
       
   252                     if self.CurrentAction == None:
       
   253                         self.StartBuffering()
       
   254                     elif self.CurrentAction[0] != "Add" or self.CurrentAction[1] != event.GetPosition() - 1:
       
   255                         self.Controler.EndBuffering()
       
   256                         self.StartBuffering()
       
   257                     self.CurrentAction = ("Add", event.GetPosition())
       
   258                 elif mod_type&wx.stc.STC_MOD_BEFOREDELETE:
       
   259                     if self.CurrentAction == None:
       
   260                         self.StartBuffering()
       
   261                     elif self.CurrentAction[0] != "Delete" or self.CurrentAction[1] != event.GetPosition() + 1:
       
   262                         self.Controler.EndBuffering()
       
   263                         self.StartBuffering()
       
   264                     self.CurrentAction = ("Delete", event.GetPosition())
       
   265         event.Skip()
       
   266     
       
   267     def OnDoDrop(self, event):
       
   268         self.ResetBuffer()
       
   269         wx.CallAfter(self.RefreshModel)
       
   270         event.Skip()
       
   271 
       
   272     def IsViewing(self, name):
       
   273         return self.Name == name
       
   274 
       
   275     # Buffer the last model state
       
   276     def RefreshBuffer(self):
       
   277         self.Controler.BufferPython()
       
   278         if self.ParentWindow:
       
   279             self.ParentWindow.RefreshTitle()
       
   280             self.ParentWindow.RefreshEditMenu()
       
   281     
       
   282     def StartBuffering(self):
       
   283         self.Controler.StartBuffering()
       
   284         if self.ParentWindow:
       
   285             self.ParentWindow.RefreshTitle()
       
   286             self.ParentWindow.RefreshEditMenu()
       
   287     
       
   288     def ResetBuffer(self):
       
   289         if self.CurrentAction != None:
       
   290             self.Controler.EndBuffering()
       
   291             self.CurrentAction = None
       
   292 
       
   293     def RefreshView(self):
       
   294         self.ResetBuffer()
       
   295         self.DisableEvents = True
       
   296         old_cursor_pos = self.GetCurrentPos()
       
   297         old_text = self.GetText()
       
   298         new_text = self.Controler.GetPythonCode()
       
   299         self.SetText(new_text)
       
   300         new_cursor_pos = GetCursorPos(old_text, new_text)
       
   301         if new_cursor_pos != None:
       
   302             self.GotoPos(new_cursor_pos)
       
   303         else:
       
   304             self.GotoPos(old_cursor_pos)
       
   305         self.ScrollToColumn(0)
       
   306         self.EmptyUndoBuffer()
       
   307         self.DisableEvents = False
       
   308         
       
   309         self.Colourise(0, -1)
       
   310 
       
   311     def RefreshModel(self):
       
   312         self.Controler.SetPythonCode(self.GetText())
       
   313 
       
   314     def OnKeyPressed(self, event):
       
   315         if self.CallTipActive():
       
   316             self.CallTipCancel()
       
   317         key = event.GetKeyCode()
       
   318 
       
   319         if key == 32 and event.ControlDown():
       
   320             pos = self.GetCurrentPos()
       
   321 
       
   322             # Tips
       
   323             if event.ShiftDown():
       
   324                 pass
       
   325 ##                self.CallTipSetBackground("yellow")
       
   326 ##                self.CallTipShow(pos, 'lots of of text: blah, blah, blah\n\n'
       
   327 ##                                 'show some suff, maybe parameters..\n\n'
       
   328 ##                                 'fubar(param1, param2)')
       
   329             # Code completion
       
   330             else:
       
   331                 self.AutoCompSetIgnoreCase(False)  # so this needs to match
       
   332 
       
   333                 # Images are specified with a appended "?type"
       
   334                 self.AutoCompShow(0, " ".join([word + "?1" for word in keyword.kwlist]))
       
   335         else:
       
   336             wx.CallAfter(self.RefreshModel)
       
   337             event.Skip()
       
   338 
       
   339     def OnKillFocus(self, event):
       
   340         self.AutoCompCancel()
       
   341         event.Skip()
       
   342 
       
   343     def OnUpdateUI(self, evt):
       
   344         # check for matching braces
       
   345         braceAtCaret = -1
       
   346         braceOpposite = -1
       
   347         charBefore = None
       
   348         caretPos = self.GetCurrentPos()
       
   349 
       
   350         if caretPos > 0:
       
   351             charBefore = self.GetCharAt(caretPos - 1)
       
   352             styleBefore = self.GetStyleAt(caretPos - 1)
       
   353 
       
   354         # check before
       
   355         if charBefore and chr(charBefore) in "[]{}()" and styleBefore == stc.STC_P_OPERATOR:
       
   356             braceAtCaret = caretPos - 1
       
   357 
       
   358         # check after
       
   359         if braceAtCaret < 0:
       
   360             charAfter = self.GetCharAt(caretPos)
       
   361             styleAfter = self.GetStyleAt(caretPos)
       
   362 
       
   363             if charAfter and chr(charAfter) in "[]{}()" and styleAfter == stc.STC_P_OPERATOR:
       
   364                 braceAtCaret = caretPos
       
   365 
       
   366         if braceAtCaret >= 0:
       
   367             braceOpposite = self.BraceMatch(braceAtCaret)
       
   368 
       
   369         if braceAtCaret != -1  and braceOpposite == -1:
       
   370             self.BraceBadLight(braceAtCaret)
       
   371         else:
       
   372             self.BraceHighlight(braceAtCaret, braceOpposite)
       
   373             #pt = self.PointFromPosition(braceOpposite)
       
   374             #self.Refresh(True, wxRect(pt.x, pt.y, 5,5))
       
   375             #print pt
       
   376             #self.Refresh(False)
       
   377 
       
   378 
       
   379     def OnMarginClick(self, evt):
       
   380         # fold and unfold as needed
       
   381         if evt.GetMargin() == 2:
       
   382             if evt.GetShift() and evt.GetControl():
       
   383                 self.FoldAll()
       
   384             else:
       
   385                 lineClicked = self.LineFromPosition(evt.GetPosition())
       
   386 
       
   387                 if self.GetFoldLevel(lineClicked) & stc.STC_FOLDLEVELHEADERFLAG:
       
   388                     if evt.GetShift():
       
   389                         self.SetFoldExpanded(lineClicked, True)
       
   390                         self.Expand(lineClicked, True, True, 1)
       
   391                     elif evt.GetControl():
       
   392                         if self.GetFoldExpanded(lineClicked):
       
   393                             self.SetFoldExpanded(lineClicked, False)
       
   394                             self.Expand(lineClicked, False, True, 0)
       
   395                         else:
       
   396                             self.SetFoldExpanded(lineClicked, True)
       
   397                             self.Expand(lineClicked, True, True, 100)
       
   398                     else:
       
   399                         self.ToggleFold(lineClicked)
       
   400 
       
   401 
       
   402     def FoldAll(self):
       
   403         lineCount = self.GetLineCount()
       
   404         expanding = True
       
   405 
       
   406         # find out if we are folding or unfolding
       
   407         for lineNum in range(lineCount):
       
   408             if self.GetFoldLevel(lineNum) & stc.STC_FOLDLEVELHEADERFLAG:
       
   409                 expanding = not self.GetFoldExpanded(lineNum)
       
   410                 break
       
   411 
       
   412         lineNum = 0
       
   413 
       
   414         while lineNum < lineCount:
       
   415             level = self.GetFoldLevel(lineNum)
       
   416             if level & stc.STC_FOLDLEVELHEADERFLAG and \
       
   417                (level & stc.STC_FOLDLEVELNUMBERMASK) == stc.STC_FOLDLEVELBASE:
       
   418 
       
   419                 if expanding:
       
   420                     self.SetFoldExpanded(lineNum, True)
       
   421                     lineNum = self.Expand(lineNum, True)
       
   422                     lineNum = lineNum - 1
       
   423                 else:
       
   424                     lastChild = self.GetLastChild(lineNum, -1)
       
   425                     self.SetFoldExpanded(lineNum, False)
       
   426 
       
   427                     if lastChild > lineNum:
       
   428                         self.HideLines(lineNum+1, lastChild)
       
   429 
       
   430             lineNum = lineNum + 1
       
   431 
       
   432 
       
   433 
       
   434     def Expand(self, line, doExpand, force=False, visLevels=0, level=-1):
       
   435         lastChild = self.GetLastChild(line, level)
       
   436         line = line + 1
       
   437 
       
   438         while line <= lastChild:
       
   439             if force:
       
   440                 if visLevels > 0:
       
   441                     self.ShowLines(line, line)
       
   442                 else:
       
   443                     self.HideLines(line, line)
       
   444             else:
       
   445                 if doExpand:
       
   446                     self.ShowLines(line, line)
       
   447 
       
   448             if level == -1:
       
   449                 level = self.GetFoldLevel(line)
       
   450 
       
   451             if level & stc.STC_FOLDLEVELHEADERFLAG:
       
   452                 if force:
       
   453                     if visLevels > 1:
       
   454                         self.SetFoldExpanded(line, True)
       
   455                     else:
       
   456                         self.SetFoldExpanded(line, False)
       
   457 
       
   458                     line = self.Expand(line, doExpand, force, visLevels-1)
       
   459 
       
   460                 else:
       
   461                     if doExpand and self.GetFoldExpanded(line):
       
   462                         line = self.Expand(line, True, force, visLevels-1)
       
   463                     else:
       
   464                         line = self.Expand(line, False, force, visLevels-1)
       
   465             else:
       
   466                 line = line + 1
       
   467 
       
   468         return line
       
   469    
       
   470 
       
   471 #-------------------------------------------------------------------------------
       
   472 #                          PythonEditor Main Frame Class
       
   473 #-------------------------------------------------------------------------------
       
   474 
       
   475 
       
   476 [ID_PYTHONEDITORFRAME, 
       
   477 ] = [wx.NewId() for _init_ctrls in range(1)]
       
   478 
       
   479 class PythonEditorFrame(wx.Frame):
       
   480     
       
   481     if wx.VERSION < (2, 6, 0):
       
   482         def Bind(self, event, function, id = None):
       
   483             if id is not None:
       
   484                 event(self, id, function)
       
   485             else:
       
   486                 event(self, function)
       
   487     
       
   488     def _init_coll_EditMenu_Items(self, parent):
       
   489         AppendMenu(parent, help='', id=wx.ID_REFRESH,
       
   490               kind=wx.ITEM_NORMAL, text=u'Refresh\tCTRL+R')
       
   491         AppendMenu(parent, help='', id=wx.ID_UNDO,
       
   492               kind=wx.ITEM_NORMAL, text=u'Undo\tCTRL+Z')
       
   493         AppendMenu(parent, help='', id=wx.ID_REDO,
       
   494               kind=wx.ITEM_NORMAL, text=u'Redo\tCTRL+Y')
       
   495         self.Bind(wx.EVT_MENU, self.OnRefreshMenu, id=wx.ID_REFRESH)
       
   496         self.Bind(wx.EVT_MENU, self.OnUndoMenu, id=wx.ID_UNDO)
       
   497         self.Bind(wx.EVT_MENU, self.OnRedoMenu, id=wx.ID_REDO)
       
   498     
       
   499     def _init_coll_MenuBar_Menus(self, parent):
       
   500         parent.Append(menu=self.EditMenu, title=u'&Edit')
       
   501     
       
   502     def _init_utils(self):
       
   503         self.MenuBar = wx.MenuBar()
       
   504 
       
   505         self.EditMenu = wx.Menu(title='')
       
   506         
       
   507         self._init_coll_MenuBar_Menus(self.MenuBar)
       
   508         self._init_coll_EditMenu_Items(self.EditMenu)
       
   509         
       
   510     def _init_ctrls(self, prnt):
       
   511         wx.Frame.__init__(self, id=ID_PYTHONEDITORFRAME, name=u'PythonEditor',
       
   512               parent=prnt, pos=wx.DefaultPosition, size=wx.Size(800, 650),
       
   513               style=wx.DEFAULT_FRAME_STYLE, title=u'PythonEditor')
       
   514         self._init_utils()
       
   515         self.SetClientSize(wx.Size(1000, 600))
       
   516         self.SetMenuBar(self.MenuBar)
       
   517         self.Bind(wx.EVT_CLOSE, self.OnCloseFrame)
       
   518         
       
   519         self.Bind(wx.EVT_MENU, self.OnSaveMenu, id=wx.ID_SAVE)
       
   520         accel = wx.AcceleratorTable([wx.AcceleratorEntry(wx.ACCEL_CTRL, 83, wx.ID_SAVE)])
       
   521         self.SetAcceleratorTable(accel)
       
   522         
       
   523         if wx.VERSION >= (2, 8, 0):
       
   524             self.AUIManager = wx.aui.AuiManager(self)
       
   525             self.AUIManager.SetDockSizeConstraint(0.5, 0.5)
       
   526         
       
   527         self.PythonEdited = PythonEditor(self, self, self.Controler)
       
   528         if wx.VERSION < (2, 8, 0):
       
   529             self.MainSizer = wx.BoxSizer(wx.VERTICAL)
       
   530             self.MainSizer.AddWindow(self.PythonEdited, 0, border=0, flag=wx.GROW)
       
   531             self.SetSizer(self.MainSizer)
       
   532         else:
       
   533             self.AUIManager.AddPane(self.PythonEdited, wx.aui.AuiPaneInfo().CentrePane())
       
   534             
       
   535         self.StatusBar = wx.StatusBar( name='HelpBar',
       
   536               parent=self, style=wx.ST_SIZEGRIP)
       
   537         self.SetStatusBar(self.StatusBar)
       
   538         
       
   539         if wx.VERSION >= (2, 8, 0):
       
   540             self.AUIManager.Update()
       
   541     
       
   542     def __init__(self, parent, controler):
       
   543         self.Controler = controler
       
   544         
       
   545         self._init_ctrls(parent)
       
   546         
       
   547         self.PythonEdited.RefreshView()
       
   548         self.RefreshTitle()
       
   549         self.RefreshEditMenu()
       
   550 
       
   551     def OnCloseFrame(self, event):
       
   552         if wx.VERSION >= (2, 8, 0):
       
   553             self.AUIManager.UnInit()
       
   554         if getattr(self, "_onclose", None) is not None:
       
   555             self._onclose()
       
   556         event.Skip()
       
   557 
       
   558     def OnSaveMenu(self, event):
       
   559         if getattr(self, "_onsave", None) != None:
       
   560             self._onsave()
       
   561         self.RefreshTitle()
       
   562         self.RefreshEditMenu()
       
   563         event.Skip()
       
   564 
       
   565     def RefreshTitle(self):
       
   566         self.SetTitle("PythonEditor - %s"%self.Controler.GetFilename())
       
   567         
       
   568 #-------------------------------------------------------------------------------
       
   569 #                          Edit Project Menu Functions
       
   570 #-------------------------------------------------------------------------------
       
   571 
       
   572     def RefreshEditMenu(self):
       
   573         undo, redo = self.Controler.GetBufferState()
       
   574         self.EditMenu.Enable(wx.ID_UNDO, undo)
       
   575         self.EditMenu.Enable(wx.ID_REDO, redo)
       
   576 
       
   577     def OnRefreshMenu(self, event):
       
   578         self.PythonEdited.RefreshView()
       
   579         event.Skip()
       
   580 
       
   581     def OnUndoMenu(self, event):
       
   582         self.Controler.LoadPrevious()
       
   583         self.PythonEdited.RefreshView()
       
   584         self.RefreshTitle()
       
   585         self.RefreshEditMenu()
       
   586         event.Skip()
       
   587     
       
   588     def OnRedoMenu(self, event):
       
   589         self.Controler.LoadNext()
       
   590         self.PythonEdited.RefreshView()
       
   591         self.RefreshTitle()
       
   592         self.RefreshEditMenu()
       
   593         event.Skip()
       
   594