c_ext/CFileEditor.py
changeset 1096 c9ace6a881c9
parent 1091 5f612651d227
child 1097 233681f2a00e
equal deleted inserted replaced
1095:a73fde048749 1096:c9ace6a881c9
     1 import keyword
       
     2 
     1 
     3 import wx
       
     4 import wx.grid
       
     5 import wx.stc as stc
     2 import wx.stc as stc
     6 import wx.lib.buttons
       
     7 
     3 
     8 from controls import CustomGrid, CustomTable
     4 from controls.CustomStyledTextCtrl import faces
     9 from editors.ConfTreeNodeEditor import ConfTreeNodeEditor, SCROLLBAR_UNIT
     5 from editors.CodeFileEditor import CodeFileEditor, CodeEditor
    10 from util.BitmapLibrary import GetBitmap
       
    11 from controls.CustomStyledTextCtrl import CustomStyledTextCtrl, faces, GetCursorPos
       
    12 
     6 
    13 def AppendMenu(parent, help, id, kind, text):
     7 class CppEditor(CodeEditor):
    14     if wx.VERSION >= (2, 6, 0):
       
    15         parent.Append(help=help, id=id, kind=kind, text=text)
       
    16     else:
       
    17         parent.Append(helpString=help, id=id, kind=kind, item=text)
       
    18 
     8 
    19 
     9     KEYWORDS = ["asm", "auto", "bool", "break", "case", "catch", "char", "class", 
    20 [ID_CPPEDITOR,
    10         "const", "const_cast", "continue", "default", "delete", "do", "double", 
    21 ] = [wx.NewId() for _init_ctrls in range(1)]
    11         "dynamic_cast", "else", "enum", "explicit", "export", "extern", "false", 
    22 
    12         "float", "for", "friend", "goto", "if", "inline", "int", "long", "mutable", 
    23 CPP_KEYWORDS = ["asm", "auto", "bool", "break", "case", "catch", "char", "class", 
    13         "namespace", "new", "operator", "private", "protected", "public", "register", 
    24     "const", "const_cast", "continue", "default", "delete", "do", "double", 
    14         "reinterpret_cast", "return", "short", "signed", "sizeof", "static", 
    25     "dynamic_cast", "else", "enum", "explicit", "export", "extern", "false", 
    15         "static_cast", "struct", "switch", "template", "this", "throw", "true", "try",
    26     "float", "for", "friend", "goto", "if", "inline", "int", "long", "mutable", 
    16         "typedef", "typeid", "typename", "union", "unsigned", "using", "virtual", 
    27     "namespace", "new", "operator", "private", "protected", "public", "register", 
    17         "void", "volatile", "wchar_t", "while"]
    28     "reinterpret_cast", "return", "short", "signed", "sizeof", "static", 
    18     COMMENT_HEADER = "//"
    29     "static_cast", "struct", "switch", "template", "this", "throw", "true", "try",
       
    30     "typedef", "typeid", "typename", "union", "unsigned", "using", "virtual", 
       
    31     "void", "volatile", "wchar_t", "while"]
       
    32 
       
    33 class CppEditor(CustomStyledTextCtrl):
       
    34 
       
    35     fold_symbols = 3
       
    36     
    19     
    37     def __init__(self, parent, name, window, controler):
    20     def SetCodeLexer(self):
    38         CustomStyledTextCtrl.__init__(self, parent, ID_CPPEDITOR, wx.DefaultPosition, 
       
    39                  wx.Size(-1, 300), 0)
       
    40 
       
    41         self.SetMarginType(1, stc.STC_MARGIN_NUMBER)
       
    42         self.SetMarginWidth(1, 25)
       
    43 
       
    44         self.CmdKeyAssign(ord('B'), stc.STC_SCMOD_CTRL, stc.STC_CMD_ZOOMIN)
       
    45         self.CmdKeyAssign(ord('N'), stc.STC_SCMOD_CTRL, stc.STC_CMD_ZOOMOUT)
       
    46 
       
    47         self.SetLexer(stc.STC_LEX_CPP)
    21         self.SetLexer(stc.STC_LEX_CPP)
    48         self.SetKeyWords(0, " ".join(CPP_KEYWORDS))
       
    49 
       
    50         self.SetProperty("fold", "1")
       
    51         self.SetProperty("tab.timmy.whinge.level", "1")
       
    52         self.SetMargins(0,0)
       
    53 
       
    54         self.SetViewWhiteSpace(False)
       
    55         #self.SetBufferedDraw(False)
       
    56         #self.SetViewEOL(True)
       
    57         #self.SetEOLMode(stc.STC_EOL_CRLF)
       
    58         #self.SetUseAntiAliasing(True)
       
    59         
    22         
    60         self.SetEdgeMode(stc.STC_EDGE_BACKGROUND)
       
    61         self.SetEdgeColumn(78)
       
    62 
       
    63         # Setup a margin to hold fold markers
       
    64         #self.SetFoldFlags(16)  ###  WHAT IS THIS VALUE?  WHAT ARE THE OTHER FLAGS?  DOES IT MATTER?
       
    65         self.SetMarginType(2, stc.STC_MARGIN_SYMBOL)
       
    66         self.SetMarginMask(2, stc.STC_MASK_FOLDERS)
       
    67         self.SetMarginSensitive(2, True)
       
    68         self.SetMarginWidth(2, 12)
       
    69 
       
    70         if self.fold_symbols == 0:
       
    71             # Arrow pointing right for contracted folders, arrow pointing down for expanded
       
    72             self.MarkerDefine(stc.STC_MARKNUM_FOLDEROPEN,    stc.STC_MARK_ARROWDOWN, "black", "black")
       
    73             self.MarkerDefine(stc.STC_MARKNUM_FOLDER,        stc.STC_MARK_ARROW, "black", "black")
       
    74             self.MarkerDefine(stc.STC_MARKNUM_FOLDERSUB,     stc.STC_MARK_EMPTY, "black", "black")
       
    75             self.MarkerDefine(stc.STC_MARKNUM_FOLDERTAIL,    stc.STC_MARK_EMPTY, "black", "black")
       
    76             self.MarkerDefine(stc.STC_MARKNUM_FOLDEREND,     stc.STC_MARK_EMPTY,     "white", "black")
       
    77             self.MarkerDefine(stc.STC_MARKNUM_FOLDEROPENMID, stc.STC_MARK_EMPTY,     "white", "black")
       
    78             self.MarkerDefine(stc.STC_MARKNUM_FOLDERMIDTAIL, stc.STC_MARK_EMPTY,     "white", "black")
       
    79             
       
    80         elif self.fold_symbols == 1:
       
    81             # Plus for contracted folders, minus for expanded
       
    82             self.MarkerDefine(stc.STC_MARKNUM_FOLDEROPEN,    stc.STC_MARK_MINUS, "white", "black")
       
    83             self.MarkerDefine(stc.STC_MARKNUM_FOLDER,        stc.STC_MARK_PLUS,  "white", "black")
       
    84             self.MarkerDefine(stc.STC_MARKNUM_FOLDERSUB,     stc.STC_MARK_EMPTY, "white", "black")
       
    85             self.MarkerDefine(stc.STC_MARKNUM_FOLDERTAIL,    stc.STC_MARK_EMPTY, "white", "black")
       
    86             self.MarkerDefine(stc.STC_MARKNUM_FOLDEREND,     stc.STC_MARK_EMPTY, "white", "black")
       
    87             self.MarkerDefine(stc.STC_MARKNUM_FOLDEROPENMID, stc.STC_MARK_EMPTY, "white", "black")
       
    88             self.MarkerDefine(stc.STC_MARKNUM_FOLDERMIDTAIL, stc.STC_MARK_EMPTY, "white", "black")
       
    89 
       
    90         elif self.fold_symbols == 2:
       
    91             # Like a flattened tree control using circular headers and curved joins
       
    92             self.MarkerDefine(stc.STC_MARKNUM_FOLDEROPEN,    stc.STC_MARK_CIRCLEMINUS,          "white", "#404040")
       
    93             self.MarkerDefine(stc.STC_MARKNUM_FOLDER,        stc.STC_MARK_CIRCLEPLUS,           "white", "#404040")
       
    94             self.MarkerDefine(stc.STC_MARKNUM_FOLDERSUB,     stc.STC_MARK_VLINE,                "white", "#404040")
       
    95             self.MarkerDefine(stc.STC_MARKNUM_FOLDERTAIL,    stc.STC_MARK_LCORNERCURVE,         "white", "#404040")
       
    96             self.MarkerDefine(stc.STC_MARKNUM_FOLDEREND,     stc.STC_MARK_CIRCLEPLUSCONNECTED,  "white", "#404040")
       
    97             self.MarkerDefine(stc.STC_MARKNUM_FOLDEROPENMID, stc.STC_MARK_CIRCLEMINUSCONNECTED, "white", "#404040")
       
    98             self.MarkerDefine(stc.STC_MARKNUM_FOLDERMIDTAIL, stc.STC_MARK_TCORNERCURVE,         "white", "#404040")
       
    99 
       
   100         elif self.fold_symbols == 3:
       
   101             # Like a flattened tree control using square headers
       
   102             self.MarkerDefine(stc.STC_MARKNUM_FOLDEROPEN,    stc.STC_MARK_BOXMINUS,          "white", "#808080")
       
   103             self.MarkerDefine(stc.STC_MARKNUM_FOLDER,        stc.STC_MARK_BOXPLUS,           "white", "#808080")
       
   104             self.MarkerDefine(stc.STC_MARKNUM_FOLDERSUB,     stc.STC_MARK_VLINE,             "white", "#808080")
       
   105             self.MarkerDefine(stc.STC_MARKNUM_FOLDERTAIL,    stc.STC_MARK_LCORNER,           "white", "#808080")
       
   106             self.MarkerDefine(stc.STC_MARKNUM_FOLDEREND,     stc.STC_MARK_BOXPLUSCONNECTED,  "white", "#808080")
       
   107             self.MarkerDefine(stc.STC_MARKNUM_FOLDEROPENMID, stc.STC_MARK_BOXMINUSCONNECTED, "white", "#808080")
       
   108             self.MarkerDefine(stc.STC_MARKNUM_FOLDERMIDTAIL, stc.STC_MARK_TCORNER,           "white", "#808080")
       
   109 
       
   110 
       
   111         self.Bind(stc.EVT_STC_UPDATEUI, self.OnUpdateUI)
       
   112         self.Bind(stc.EVT_STC_MARGINCLICK, self.OnMarginClick)
       
   113         self.Bind(wx.EVT_KEY_DOWN, self.OnKeyPressed)
       
   114 
       
   115         # Make some styles,  The lexer defines what each style is used for, we
       
   116         # just have to define what each style looks like.  This set is adapted from
       
   117         # Scintilla sample property files.
       
   118 
       
   119         # Global default styles for all languages
       
   120         self.StyleSetSpec(stc.STC_STYLE_DEFAULT,     "face:%(mono)s,size:%(size)d" % faces)
       
   121         self.StyleClearAll()  # Reset all to be like the default
       
   122 
       
   123         # Global default styles for all languages
       
   124         self.StyleSetSpec(stc.STC_STYLE_DEFAULT,     "face:%(mono)s,size:%(size)d" % faces)
       
   125         self.StyleSetSpec(stc.STC_STYLE_LINENUMBER,  "back:#C0C0C0,face:%(helv)s,size:%(size)d" % faces)
       
   126         self.StyleSetSpec(stc.STC_STYLE_CONTROLCHAR, "face:%(other)s" % faces)
       
   127         self.StyleSetSpec(stc.STC_STYLE_BRACELIGHT,  "fore:#FFFFFF,back:#0000FF,bold")
       
   128         self.StyleSetSpec(stc.STC_STYLE_BRACEBAD,    "fore:#000000,back:#FF0000,bold")
       
   129 
       
   130         self.StyleSetSpec(stc.STC_C_COMMENT, 'fore:#408060,size:%(size)d' % faces)
    23         self.StyleSetSpec(stc.STC_C_COMMENT, 'fore:#408060,size:%(size)d' % faces)
   131         self.StyleSetSpec(stc.STC_C_COMMENTLINE, 'fore:#408060,size:%(size)d' % faces)
    24         self.StyleSetSpec(stc.STC_C_COMMENTLINE, 'fore:#408060,size:%(size)d' % faces)
   132         self.StyleSetSpec(stc.STC_C_COMMENTDOC, 'fore:#408060,size:%(size)d' % faces)
    25         self.StyleSetSpec(stc.STC_C_COMMENTDOC, 'fore:#408060,size:%(size)d' % faces)
   133         self.StyleSetSpec(stc.STC_C_NUMBER, 'fore:#0076AE,size:%(size)d' % faces)
    26         self.StyleSetSpec(stc.STC_C_NUMBER, 'fore:#0076AE,size:%(size)d' % faces)
   134         self.StyleSetSpec(stc.STC_C_WORD, 'bold,fore:#800056,size:%(size)d' % faces)
    27         self.StyleSetSpec(stc.STC_C_WORD, 'bold,fore:#800056,size:%(size)d' % faces)
   135         self.StyleSetSpec(stc.STC_C_STRING, 'fore:#2a00ff,size:%(size)d' % faces)
    28         self.StyleSetSpec(stc.STC_C_STRING, 'fore:#2a00ff,size:%(size)d' % faces)
   136         self.StyleSetSpec(stc.STC_C_PREPROCESSOR, 'bold,fore:#800056,size:%(size)d' % faces)
    29         self.StyleSetSpec(stc.STC_C_PREPROCESSOR, 'bold,fore:#800056,size:%(size)d' % faces)
   137         self.StyleSetSpec(stc.STC_C_OPERATOR, 'bold,size:%(size)d' % faces)
    30         self.StyleSetSpec(stc.STC_C_OPERATOR, 'bold,size:%(size)d' % faces)
   138         self.StyleSetSpec(stc.STC_C_STRINGEOL, 'back:#FFD5FF,size:%(size)d' % faces)
    31         self.StyleSetSpec(stc.STC_C_STRINGEOL, 'back:#FFD5FF,size:%(size)d' % faces)
   139         
       
   140         # register some images for use in the AutoComplete box.
       
   141         #self.RegisterImage(1, images.getSmilesBitmap())
       
   142         self.RegisterImage(1, 
       
   143             wx.ArtProvider.GetBitmap(wx.ART_DELETE, size=(16,16)))
       
   144         self.RegisterImage(2, 
       
   145             wx.ArtProvider.GetBitmap(wx.ART_NEW, size=(16,16)))
       
   146         self.RegisterImage(3, 
       
   147             wx.ArtProvider.GetBitmap(wx.ART_COPY, size=(16,16)))
       
   148 
       
   149         # Indentation size
       
   150         self.SetTabWidth(2)
       
   151         self.SetUseTabs(0)
       
   152 
       
   153         self.Controler = controler
       
   154         self.ParentWindow = window
       
   155         
       
   156         self.DisableEvents = True
       
   157         self.Name = name
       
   158         self.CurrentAction = None
       
   159         
       
   160         self.SetModEventMask(wx.stc.STC_MOD_BEFOREINSERT|wx.stc.STC_MOD_BEFOREDELETE)
       
   161 
       
   162         self.Bind(wx.stc.EVT_STC_DO_DROP, self.OnDoDrop, id=ID_CPPEDITOR)
       
   163         self.Bind(wx.EVT_KILL_FOCUS, self.OnKillFocus)
       
   164         self.Bind(wx.stc.EVT_STC_MODIFIED, self.OnModification, id=ID_CPPEDITOR)
       
   165     
       
   166     def OnModification(self, event):
       
   167         if not self.DisableEvents:
       
   168             mod_type = event.GetModificationType()
       
   169             if not (mod_type&wx.stc.STC_PERFORMED_UNDO or mod_type&wx.stc.STC_PERFORMED_REDO):
       
   170                 if mod_type&wx.stc.STC_MOD_BEFOREINSERT:
       
   171                     if self.CurrentAction == None:
       
   172                         self.StartBuffering()
       
   173                     elif self.CurrentAction[0] != "Add" or self.CurrentAction[1] != event.GetPosition() - 1:
       
   174                         self.Controler.EndBuffering()
       
   175                         self.StartBuffering()
       
   176                     self.CurrentAction = ("Add", event.GetPosition())
       
   177                     wx.CallAfter(self.RefreshModel)
       
   178                 elif mod_type&wx.stc.STC_MOD_BEFOREDELETE:
       
   179                     if self.CurrentAction == None:
       
   180                         self.StartBuffering()
       
   181                     elif self.CurrentAction[0] != "Delete" or self.CurrentAction[1] != event.GetPosition() + 1:
       
   182                         self.Controler.EndBuffering()
       
   183                         self.StartBuffering()
       
   184                     self.CurrentAction = ("Delete", event.GetPosition())
       
   185                     wx.CallAfter(self.RefreshModel)
       
   186         event.Skip()
       
   187     
       
   188     def OnDoDrop(self, event):
       
   189         self.ResetBuffer()
       
   190         wx.CallAfter(self.RefreshModel)
       
   191         event.Skip()
       
   192 
       
   193     # Buffer the last model state
       
   194     def RefreshBuffer(self):
       
   195         self.Controler.BufferCFile()
       
   196         if self.ParentWindow is not None:
       
   197             self.ParentWindow.RefreshTitle()
       
   198             self.ParentWindow.RefreshFileMenu()
       
   199             self.ParentWindow.RefreshEditMenu()
       
   200             self.ParentWindow.RefreshPageTitles()
       
   201     
       
   202     def StartBuffering(self):
       
   203         self.Controler.StartBuffering()
       
   204         if self.ParentWindow is not None:
       
   205             self.ParentWindow.RefreshTitle()
       
   206             self.ParentWindow.RefreshFileMenu()
       
   207             self.ParentWindow.RefreshEditMenu()
       
   208             self.ParentWindow.RefreshPageTitles()
       
   209     
       
   210     def ResetBuffer(self):
       
   211         if self.CurrentAction != None:
       
   212             self.Controler.EndBuffering()
       
   213             self.CurrentAction = None
       
   214 
       
   215     def RefreshView(self):
       
   216         self.ResetBuffer()
       
   217         self.DisableEvents = True
       
   218         old_cursor_pos = self.GetCurrentPos()
       
   219         line = self.GetFirstVisibleLine()
       
   220         column = self.GetXOffset()
       
   221         old_text = self.GetText()
       
   222         new_text = self.Controler.GetPartText(self.Name)
       
   223         self.SetText(new_text)
       
   224         if old_text != new_text:
       
   225             new_cursor_pos = GetCursorPos(old_text, new_text)
       
   226             self.LineScroll(column, line)
       
   227             if new_cursor_pos != None:
       
   228                 self.GotoPos(new_cursor_pos)
       
   229             else:
       
   230                 self.GotoPos(old_cursor_pos)
       
   231             self.EmptyUndoBuffer()
       
   232         self.DisableEvents = False
       
   233         
       
   234         self.Colourise(0, -1)
       
   235 
       
   236     def DoGetBestSize(self):
       
   237         return self.ParentWindow.GetPanelBestSize()
       
   238 
       
   239     def RefreshModel(self):
       
   240         self.Controler.SetPartText(self.Name, self.GetText())
       
   241 
       
   242     def OnKeyPressed(self, event):
       
   243         if self.CallTipActive():
       
   244             self.CallTipCancel()
       
   245         key = event.GetKeyCode()
       
   246 
       
   247         if key == 32 and event.ControlDown():
       
   248             pos = self.GetCurrentPos()
       
   249 
       
   250             # Tips
       
   251             if event.ShiftDown():
       
   252                 pass
       
   253 ##                self.CallTipSetBackground("yellow")
       
   254 ##                self.CallTipShow(pos, 'lots of of text: blah, blah, blah\n\n'
       
   255 ##                                 'show some suff, maybe parameters..\n\n'
       
   256 ##                                 'fubar(param1, param2)')
       
   257             # Code completion
       
   258             else:
       
   259                 self.AutoCompSetIgnoreCase(False)  # so this needs to match
       
   260 
       
   261                 # Images are specified with a appended "?type"
       
   262                 self.AutoCompShow(0, " ".join([word + "?1" for word in CPP_KEYWORDS]))
       
   263         else:
       
   264             event.Skip()
       
   265 
       
   266     def OnKillFocus(self, event):
       
   267         self.AutoCompCancel()
       
   268         event.Skip()
       
   269 
       
   270     def OnUpdateUI(self, evt):
       
   271         # check for matching braces
       
   272         braceAtCaret = -1
       
   273         braceOpposite = -1
       
   274         charBefore = None
       
   275         caretPos = self.GetCurrentPos()
       
   276 
       
   277         if caretPos > 0:
       
   278             charBefore = self.GetCharAt(caretPos - 1)
       
   279             styleBefore = self.GetStyleAt(caretPos - 1)
       
   280 
       
   281         # check before
       
   282         if charBefore and chr(charBefore) in "[]{}()" and styleBefore == stc.STC_P_OPERATOR:
       
   283             braceAtCaret = caretPos - 1
       
   284 
       
   285         # check after
       
   286         if braceAtCaret < 0:
       
   287             charAfter = self.GetCharAt(caretPos)
       
   288             styleAfter = self.GetStyleAt(caretPos)
       
   289 
       
   290             if charAfter and chr(charAfter) in "[]{}()" and styleAfter == stc.STC_P_OPERATOR:
       
   291                 braceAtCaret = caretPos
       
   292 
       
   293         if braceAtCaret >= 0:
       
   294             braceOpposite = self.BraceMatch(braceAtCaret)
       
   295 
       
   296         if braceAtCaret != -1  and braceOpposite == -1:
       
   297             self.BraceBadLight(braceAtCaret)
       
   298         else:
       
   299             self.BraceHighlight(braceAtCaret, braceOpposite)
       
   300             #pt = self.PointFromPosition(braceOpposite)
       
   301             #self.Refresh(True, wxRect(pt.x, pt.y, 5,5))
       
   302             #print pt
       
   303             #self.Refresh(False)
       
   304 
       
   305 
       
   306     def OnMarginClick(self, evt):
       
   307         # fold and unfold as needed
       
   308         if evt.GetMargin() == 2:
       
   309             if evt.GetShift() and evt.GetControl():
       
   310                 self.FoldAll()
       
   311             else:
       
   312                 lineClicked = self.LineFromPosition(evt.GetPosition())
       
   313 
       
   314                 if self.GetFoldLevel(lineClicked) & stc.STC_FOLDLEVELHEADERFLAG:
       
   315                     if evt.GetShift():
       
   316                         self.SetFoldExpanded(lineClicked, True)
       
   317                         self.Expand(lineClicked, True, True, 1)
       
   318                     elif evt.GetControl():
       
   319                         if self.GetFoldExpanded(lineClicked):
       
   320                             self.SetFoldExpanded(lineClicked, False)
       
   321                             self.Expand(lineClicked, False, True, 0)
       
   322                         else:
       
   323                             self.SetFoldExpanded(lineClicked, True)
       
   324                             self.Expand(lineClicked, True, True, 100)
       
   325                     else:
       
   326                         self.ToggleFold(lineClicked)
       
   327 
       
   328 
       
   329     def FoldAll(self):
       
   330         lineCount = self.GetLineCount()
       
   331         expanding = True
       
   332 
       
   333         # find out if we are folding or unfolding
       
   334         for lineNum in range(lineCount):
       
   335             if self.GetFoldLevel(lineNum) & stc.STC_FOLDLEVELHEADERFLAG:
       
   336                 expanding = not self.GetFoldExpanded(lineNum)
       
   337                 break
       
   338 
       
   339         lineNum = 0
       
   340 
       
   341         while lineNum < lineCount:
       
   342             level = self.GetFoldLevel(lineNum)
       
   343             if level & stc.STC_FOLDLEVELHEADERFLAG and \
       
   344                (level & stc.STC_FOLDLEVELNUMBERMASK) == stc.STC_FOLDLEVELBASE:
       
   345 
       
   346                 if expanding:
       
   347                     self.SetFoldExpanded(lineNum, True)
       
   348                     lineNum = self.Expand(lineNum, True)
       
   349                     lineNum = lineNum - 1
       
   350                 else:
       
   351                     lastChild = self.GetLastChild(lineNum, -1)
       
   352                     self.SetFoldExpanded(lineNum, False)
       
   353 
       
   354                     if lastChild > lineNum:
       
   355                         self.HideLines(lineNum+1, lastChild)
       
   356 
       
   357             lineNum = lineNum + 1
       
   358 
       
   359 
       
   360 
       
   361     def Expand(self, line, doExpand, force=False, visLevels=0, level=-1):
       
   362         lastChild = self.GetLastChild(line, level)
       
   363         line = line + 1
       
   364 
       
   365         while line <= lastChild:
       
   366             if force:
       
   367                 if visLevels > 0:
       
   368                     self.ShowLines(line, line)
       
   369                 else:
       
   370                     self.HideLines(line, line)
       
   371             else:
       
   372                 if doExpand:
       
   373                     self.ShowLines(line, line)
       
   374 
       
   375             if level == -1:
       
   376                 level = self.GetFoldLevel(line)
       
   377 
       
   378             if level & stc.STC_FOLDLEVELHEADERFLAG:
       
   379                 if force:
       
   380                     if visLevels > 1:
       
   381                         self.SetFoldExpanded(line, True)
       
   382                     else:
       
   383                         self.SetFoldExpanded(line, False)
       
   384 
       
   385                     line = self.Expand(line, doExpand, force, visLevels-1)
       
   386 
       
   387                 else:
       
   388                     if doExpand and self.GetFoldExpanded(line):
       
   389                         line = self.Expand(line, True, force, visLevels-1)
       
   390                     else:
       
   391                         line = self.Expand(line, False, force, visLevels-1)
       
   392             else:
       
   393                 line = line + 1
       
   394 
       
   395         return line
       
   396 
       
   397     def Cut(self):
       
   398         self.ResetBuffer()
       
   399         self.DisableEvents = True
       
   400         self.CmdKeyExecute(wx.stc.STC_CMD_CUT)
       
   401         self.DisableEvents = False
       
   402         self.RefreshModel()
       
   403         self.RefreshBuffer()
       
   404     
       
   405     def Copy(self):
       
   406         self.CmdKeyExecute(wx.stc.STC_CMD_COPY)
       
   407     
       
   408     def Paste(self):
       
   409         self.ResetBuffer()
       
   410         self.DisableEvents = True
       
   411         self.CmdKeyExecute(wx.stc.STC_CMD_PASTE)
       
   412         self.DisableEvents = False
       
   413         self.RefreshModel()
       
   414         self.RefreshBuffer()
       
   415 
       
   416 
    32 
   417 #-------------------------------------------------------------------------------
    33 #-------------------------------------------------------------------------------
   418 #                         Helper for VariablesGrid values
    34 #                          CFileEditor Main Frame Class
   419 #-------------------------------------------------------------------------------
    35 #-------------------------------------------------------------------------------
   420 
    36 
   421 class VariablesTable(CustomTable):
    37 class CFileEditor(CodeFileEditor):
   422     
    38     
   423     def GetValue(self, row, col):
    39     CONFNODEEDITOR_TABS = CodeFileEditor.CONFNODEEDITOR_TABS + [
   424         if row < self.GetNumberRows():
       
   425             if col == 0:
       
   426                 return row + 1
       
   427             else:
       
   428                 return str(self.data[row].get(self.GetColLabelValue(col, False), ""))
       
   429     
       
   430     def _updateColAttrs(self, grid):
       
   431         """
       
   432         wxGrid -> update the column attributes to add the
       
   433         appropriate renderer given the column name.
       
   434 
       
   435         Otherwise default to the default renderer.
       
   436         """
       
   437         
       
   438         typelist = None
       
   439         accesslist = None
       
   440         for row in range(self.GetNumberRows()):
       
   441             for col in range(self.GetNumberCols()):
       
   442                 editor = None
       
   443                 renderer = None
       
   444                 colname = self.GetColLabelValue(col, False)
       
   445                 
       
   446                 if colname == "Name":
       
   447                     editor = wx.grid.GridCellTextEditor()
       
   448                 elif colname == "Class":
       
   449                     editor = wx.grid.GridCellChoiceEditor()
       
   450                     editor.SetParameters("input,memory,output")
       
   451                 elif colname == "Type":
       
   452                     pass
       
   453                 else:
       
   454                     grid.SetReadOnly(row, col, True)
       
   455                 
       
   456                 grid.SetCellEditor(row, col, editor)
       
   457                 grid.SetCellRenderer(row, col, renderer)
       
   458                 
       
   459                 grid.SetCellBackgroundColour(row, col, wx.WHITE)
       
   460             self.ResizeRow(grid, row)
       
   461 
       
   462 
       
   463 class VariablesEditor(wx.Panel):
       
   464     
       
   465     def __init__(self, parent, window, controler):
       
   466         wx.Panel.__init__(self, parent, style=wx.TAB_TRAVERSAL)
       
   467         
       
   468         main_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=4)
       
   469         main_sizer.AddGrowableCol(0)
       
   470         main_sizer.AddGrowableRow(0)
       
   471         
       
   472         self.VariablesGrid = CustomGrid(self, size=wx.Size(-1, 300), style=wx.VSCROLL)
       
   473         self.VariablesGrid.Bind(wx.grid.EVT_GRID_CELL_CHANGE, self.OnVariablesGridCellChange)
       
   474         self.VariablesGrid.Bind(wx.grid.EVT_GRID_CELL_LEFT_CLICK, self.OnVariablesGridCellLeftClick)
       
   475         self.VariablesGrid.Bind(wx.grid.EVT_GRID_EDITOR_SHOWN, self.OnVariablesGridEditorShown)
       
   476         main_sizer.AddWindow(self.VariablesGrid, flag=wx.GROW)
       
   477         
       
   478         controls_sizer = wx.BoxSizer(wx.HORIZONTAL)
       
   479         main_sizer.AddSizer(controls_sizer, border=5, flag=wx.TOP|wx.ALIGN_RIGHT)
       
   480         
       
   481         for name, bitmap, help in [
       
   482                 ("AddVariableButton", "add_element", _("Add variable")),
       
   483                 ("DeleteVariableButton", "remove_element", _("Remove variable")),
       
   484                 ("UpVariableButton", "up", _("Move variable up")),
       
   485                 ("DownVariableButton", "down", _("Move variable down"))]:
       
   486             button = wx.lib.buttons.GenBitmapButton(self, bitmap=GetBitmap(bitmap), 
       
   487                   size=wx.Size(28, 28), style=wx.NO_BORDER)
       
   488             button.SetToolTipString(help)
       
   489             setattr(self, name, button)
       
   490             controls_sizer.AddWindow(button, border=5, flag=wx.LEFT)
       
   491         
       
   492         self.SetSizer(main_sizer)
       
   493                 
       
   494         self.ParentWindow = window
       
   495         self.Controler = controler
       
   496         
       
   497         self.VariablesDefaultValue = {"Name" : "", "Class" : "input", "Type" : ""}
       
   498         self.Table = VariablesTable(self, [], ["#", "Name", "Class", "Type"])
       
   499         self.ColAlignements = [wx.ALIGN_RIGHT, wx.ALIGN_LEFT, wx.ALIGN_LEFT, wx.ALIGN_LEFT]
       
   500         self.ColSizes = [40, 200, 150, 150]
       
   501         self.VariablesGrid.SetTable(self.Table)
       
   502         self.VariablesGrid.SetButtons({"Add": self.AddVariableButton,
       
   503                                        "Delete": self.DeleteVariableButton,
       
   504                                        "Up": self.UpVariableButton,
       
   505                                        "Down": self.DownVariableButton})
       
   506         
       
   507         def _AddVariable(new_row):
       
   508             self.Table.InsertRow(new_row, self.VariablesDefaultValue.copy())
       
   509             self.RefreshModel()
       
   510             self.RefreshView()
       
   511             return new_row
       
   512         setattr(self.VariablesGrid, "_AddRow", _AddVariable)
       
   513         
       
   514         def _DeleteVariable(row):
       
   515             self.Table.RemoveRow(row)
       
   516             self.RefreshModel()
       
   517             self.RefreshView()
       
   518         setattr(self.VariablesGrid, "_DeleteRow", _DeleteVariable)
       
   519         
       
   520         def _MoveVariable(row, move):
       
   521             new_row = self.Table.MoveRow(row, move)
       
   522             if new_row != row:
       
   523                 self.RefreshModel()
       
   524                 self.RefreshView()
       
   525             return new_row
       
   526         setattr(self.VariablesGrid, "_MoveRow", _MoveVariable)
       
   527         
       
   528         self.VariablesGrid.SetRowLabelSize(0)
       
   529         for col in range(self.Table.GetNumberCols()):
       
   530             attr = wx.grid.GridCellAttr()
       
   531             attr.SetAlignment(self.ColAlignements[col], wx.ALIGN_CENTRE)
       
   532             self.VariablesGrid.SetColAttr(col, attr)
       
   533             self.VariablesGrid.SetColSize(col, self.ColSizes[col])
       
   534         self.Table.ResetView(self.VariablesGrid)
       
   535 
       
   536     def RefreshModel(self):
       
   537         self.Controler.SetVariables(self.Table.GetData())
       
   538         self.RefreshBuffer()
       
   539         
       
   540     # Buffer the last model state
       
   541     def RefreshBuffer(self):
       
   542         self.Controler.BufferCFile()
       
   543         self.ParentWindow.RefreshTitle()
       
   544         self.ParentWindow.RefreshFileMenu()
       
   545         self.ParentWindow.RefreshEditMenu()
       
   546         self.ParentWindow.RefreshPageTitles()
       
   547 
       
   548     def RefreshView(self):
       
   549         self.Table.SetData(self.Controler.GetVariables())
       
   550         self.Table.ResetView(self.VariablesGrid)
       
   551         self.VariablesGrid.RefreshButtons()
       
   552     
       
   553     def DoGetBestSize(self):
       
   554         return self.ParentWindow.GetPanelBestSize()
       
   555     
       
   556     def OnVariablesGridCellChange(self, event):
       
   557         self.RefreshModel()
       
   558         wx.CallAfter(self.RefreshView)
       
   559         event.Skip()
       
   560 
       
   561     def OnVariablesGridEditorShown(self, event):
       
   562         row, col = event.GetRow(), event.GetCol() 
       
   563         if self.Table.GetColLabelValue(col, False) == "Type":
       
   564             type_menu = wx.Menu(title='')
       
   565             base_menu = wx.Menu(title='')
       
   566             for base_type in self.Controler.GetBaseTypes():
       
   567                 new_id = wx.NewId()
       
   568                 AppendMenu(base_menu, help='', id=new_id, kind=wx.ITEM_NORMAL, text=base_type)
       
   569                 self.Bind(wx.EVT_MENU, self.GetVariableTypeFunction(base_type), id=new_id)
       
   570             type_menu.AppendMenu(wx.NewId(), "Base Types", base_menu)
       
   571             datatype_menu = wx.Menu(title='')
       
   572             for datatype in self.Controler.GetDataTypes(basetypes=False, only_locatables=True):
       
   573                 new_id = wx.NewId()
       
   574                 AppendMenu(datatype_menu, help='', id=new_id, kind=wx.ITEM_NORMAL, text=datatype)
       
   575                 self.Bind(wx.EVT_MENU, self.GetVariableTypeFunction(datatype), id=new_id)
       
   576             type_menu.AppendMenu(wx.NewId(), "User Data Types", datatype_menu)
       
   577             rect = self.VariablesGrid.BlockToDeviceRect((row, col), (row, col))
       
   578             
       
   579             self.VariablesGrid.PopupMenuXY(type_menu, rect.x + rect.width, rect.y + self.VariablesGrid.GetColLabelSize())
       
   580             type_menu.Destroy()
       
   581             event.Veto()
       
   582         else:
       
   583             event.Skip()
       
   584 
       
   585     def GetVariableTypeFunction(self, base_type):
       
   586         def VariableTypeFunction(event):
       
   587             row = self.VariablesGrid.GetGridCursorRow()
       
   588             self.Table.SetValueByName(row, "Type", base_type)
       
   589             self.Table.ResetView(self.VariablesGrid)
       
   590             self.RefreshModel()
       
   591             self.RefreshView()
       
   592             event.Skip()
       
   593         return VariableTypeFunction
       
   594 
       
   595     def OnVariablesGridCellLeftClick(self, event):
       
   596         if event.GetCol() == 0:
       
   597             row = event.GetRow()
       
   598             num = 0
       
   599             if self.Table.GetValueByName(row, "Class") == "input":
       
   600                 dir = "%I"
       
   601                 for i in xrange(row):
       
   602                     if self.Table.GetValueByName(i, "Class") == "input":
       
   603                         num += 1
       
   604             elif self.Table.GetValueByName(row, "Class") == "memory":
       
   605                 dir = "%M"
       
   606                 for i in xrange(row):
       
   607                     if self.Table.GetValueByName(i, "Class") == "memory":
       
   608                         num += 1
       
   609             else:
       
   610                 dir = "%Q"
       
   611                 for i in xrange(row):
       
   612                     if self.Table.GetValueByName(i, "Class") == "output":
       
   613                         num += 1
       
   614             data_type = self.Table.GetValueByName(row, "Type")
       
   615             var_name = self.Table.GetValueByName(row, "Name")
       
   616             base_location = ".".join(map(lambda x:str(x), self.Controler.GetCurrentLocation()))
       
   617             location = "%s%s%s.%d"%(dir, self.Controler.GetSizeOfType(data_type), base_location, num)
       
   618             data = wx.TextDataObject(str((location, "location", data_type, var_name, "")))
       
   619             dragSource = wx.DropSource(self.VariablesGrid)
       
   620             dragSource.SetData(data)
       
   621             dragSource.DoDragDrop()
       
   622             return
       
   623         event.Skip()
       
   624     
       
   625 
       
   626 #-------------------------------------------------------------------------------
       
   627 #                          SVGUIEditor Main Frame Class
       
   628 #-------------------------------------------------------------------------------
       
   629 
       
   630 CFILE_PARTS = [
       
   631     ("Includes", CppEditor), 
       
   632     ("Variables", VariablesEditor), 
       
   633     ("Globals", CppEditor), 
       
   634     ("Init", CppEditor), 
       
   635     ("CleanUp", CppEditor), 
       
   636     ("Retrieve", CppEditor), 
       
   637     ("Publish", CppEditor),
       
   638 ]
       
   639 
       
   640 class FoldPanelCaption(wx.lib.buttons.GenBitmapTextToggleButton):
       
   641     
       
   642     def GetBackgroundBrush(self, dc):
       
   643         colBg = self.GetBackgroundColour()
       
   644         brush = wx.Brush(colBg, wx.SOLID)
       
   645         if self.style & wx.BORDER_NONE:
       
   646             myAttr = self.GetDefaultAttributes()
       
   647             parAttr = self.GetParent().GetDefaultAttributes()
       
   648             myDef = colBg == myAttr.colBg
       
   649             parDef = self.GetParent().GetBackgroundColour() == parAttr.colBg
       
   650             if myDef and parDef:
       
   651                 if wx.Platform == "__WXMAC__":
       
   652                     brush.MacSetTheme(1) # 1 == kThemeBrushDialogBackgroundActive
       
   653                 elif wx.Platform == "__WXMSW__":
       
   654                     if self.DoEraseBackground(dc):
       
   655                         brush = None
       
   656             elif myDef and not parDef:
       
   657                 colBg = self.GetParent().GetBackgroundColour()
       
   658                 brush = wx.Brush(colBg, wx.SOLID)
       
   659         return brush
       
   660     
       
   661     def DrawLabel(self, dc, width, height, dx=0, dy=0):
       
   662         bmp = self.bmpLabel
       
   663         if bmp is not None:     # if the bitmap is used
       
   664             if self.bmpDisabled and not self.IsEnabled():
       
   665                 bmp = self.bmpDisabled
       
   666             if self.bmpFocus and self.hasFocus:
       
   667                 bmp = self.bmpFocus
       
   668             if self.bmpSelected and not self.up:
       
   669                 bmp = self.bmpSelected
       
   670             bw,bh = bmp.GetWidth(), bmp.GetHeight()
       
   671             hasMask = bmp.GetMask() is not None
       
   672         else:
       
   673             bw = bh = 0     # no bitmap -> size is zero
       
   674         
       
   675         dc.SetFont(self.GetFont())
       
   676         if self.IsEnabled():
       
   677             dc.SetTextForeground(self.GetForegroundColour())
       
   678         else:
       
   679             dc.SetTextForeground(wx.SystemSettings.GetColour(wx.SYS_COLOUR_GRAYTEXT))
       
   680 
       
   681         label = self.GetLabel()
       
   682         tw, th = dc.GetTextExtent(label)        # size of text
       
   683         
       
   684         if bmp is not None:
       
   685             dc.DrawBitmap(bmp, width - bw - 2, (height-bh)/2, hasMask) # draw bitmap if available
       
   686         
       
   687         dc.DrawText(label, 2, (height-th)/2)      # draw the text
       
   688 
       
   689         dc.SetPen(wx.Pen(self.GetForegroundColour()))
       
   690         dc.SetBrush(wx.TRANSPARENT_BRUSH)
       
   691         dc.DrawRectangle(0, 0, width, height)
       
   692 
       
   693 class CFileEditor(ConfTreeNodeEditor):
       
   694     
       
   695     CONFNODEEDITOR_TABS = [
       
   696         (_("C code"), "_create_CCodeEditor")]
    40         (_("C code"), "_create_CCodeEditor")]
   697     
    41     
   698     def _create_CCodeEditor(self, prnt):
    42     def _create_CCodeEditor(self, prnt):
   699         self.CCodeEditor = wx.ScrolledWindow(prnt, 
    43         self.CCodeEditor = CppEditor(prnt, self.ParentWindow, self.Controler)
   700               style=wx.TAB_TRAVERSAL|wx.HSCROLL|wx.VSCROLL)
       
   701         self.CCodeEditor.Bind(wx.EVT_SIZE, self.OnCCodeEditorResize)
       
   702         
       
   703         self.Panels = {}
       
   704         self.MainSizer = wx.BoxSizer(wx.VERTICAL)
       
   705         
       
   706         for idx, (name, panel_class) in enumerate(CFILE_PARTS):
       
   707             button_id = wx.NewId()
       
   708             button = FoldPanelCaption(id=button_id, name='FoldPanelCaption_%s' % name, 
       
   709                   label=name, bitmap=GetBitmap("CollapsedIconData"), 
       
   710                   parent=self.CCodeEditor, pos=wx.Point(0, 0),
       
   711                   size=wx.Size(0, 20), style=wx.NO_BORDER|wx.ALIGN_LEFT)
       
   712             button.SetBitmapSelected(GetBitmap("ExpandedIconData"))
       
   713             button.Bind(wx.EVT_BUTTON, self.GenPanelButtonCallback(name), id=button_id)
       
   714             self.MainSizer.AddWindow(button, 0, border=0, flag=wx.TOP|wx.GROW)
       
   715             
       
   716             if panel_class == VariablesEditor:
       
   717                 panel = VariablesEditor(self.CCodeEditor, self.ParentWindow, self.Controler)
       
   718             else:
       
   719                 panel = panel_class(self.CCodeEditor, name, self.ParentWindow, self.Controler)
       
   720             self.MainSizer.AddWindow(panel, 0, border=0, flag=wx.BOTTOM|wx.GROW)
       
   721             panel.Hide()
       
   722             
       
   723             self.Panels[name] = {"button": button, "panel": panel, "expanded": False, "row": 2 * idx + 1}
       
   724         
       
   725         self.CCodeEditor.SetSizer(self.MainSizer)
       
   726         
    44         
   727         return self.CCodeEditor
    45         return self.CCodeEditor
   728     
    46 
   729     def __init__(self, parent, controler, window):
    47     def RefreshView(self):
   730         ConfTreeNodeEditor.__init__(self, parent, controler, window)
    48         CodeFileEditor.RefreshView(self)
   731     
       
   732     def GetBufferState(self):
       
   733         return self.Controler.GetBufferState()
       
   734         
    49         
   735     def Undo(self):
    50         self.CCodeEditor.RefreshView()
   736         self.Controler.LoadPrevious()
       
   737         self.RefreshView()
       
   738             
       
   739     def Redo(self):
       
   740         self.Controler.LoadNext()
       
   741         self.RefreshView()
       
   742     
       
   743     def RefreshView(self):
       
   744         ConfTreeNodeEditor.RefreshView(self)
       
   745         
       
   746         for infos in self.Panels.itervalues():
       
   747             infos["panel"].RefreshView()
       
   748         
       
   749         self.RefreshCCodeEditorScrollbars()
       
   750 
       
   751     def GenPanelButtonCallback(self, name):
       
   752         def PanelButtonCallback(event):
       
   753             self.TogglePanel(name)
       
   754         return PanelButtonCallback
       
   755 
       
   756     def ExpandPanel(self, name):
       
   757         infos = self.Panels.get(name, None)
       
   758         if infos is not None and not infos["expanded"]:
       
   759             infos["expanded"] = True
       
   760             infos["button"].SetToggle(True)
       
   761             infos["panel"].Show()
       
   762             
       
   763             self.RefreshSizerLayout()
       
   764     
       
   765     def CollapsePanel(self, name):
       
   766         infos = self.Panels.get(name, None)
       
   767         if infos is not None and infos["expanded"]:
       
   768             infos["expanded"] = False
       
   769             infos["button"].SetToggle(False)
       
   770             infos["panel"].Hide()
       
   771             
       
   772             self.RefreshSizerLayout()
       
   773         
       
   774     def TogglePanel(self, name):
       
   775         infos = self.Panels.get(name, None)
       
   776         if infos is not None:
       
   777             infos["expanded"] = not infos["expanded"]
       
   778             infos["button"].SetToggle(infos["expanded"])
       
   779             if infos["expanded"]:
       
   780                 infos["panel"].Show()
       
   781             else:
       
   782                 infos["panel"].Hide()
       
   783             
       
   784             self.RefreshSizerLayout()
       
   785     
       
   786     def RefreshSizerLayout(self):
       
   787         self.MainSizer.Layout()
       
   788         self.RefreshCCodeEditorScrollbars()
       
   789     
       
   790     def RefreshCCodeEditorScrollbars(self):
       
   791         self.CCodeEditor.GetBestSize()
       
   792         xstart, ystart = self.CCodeEditor.GetViewStart()
       
   793         window_size = self.CCodeEditor.GetClientSize()
       
   794         maxx, maxy = self.MainSizer.GetMinSize()
       
   795         posx = max(0, min(xstart, (maxx - window_size[0]) / SCROLLBAR_UNIT))
       
   796         posy = max(0, min(ystart, (maxy - window_size[1]) / SCROLLBAR_UNIT))
       
   797         self.CCodeEditor.Scroll(posx, posy)
       
   798         self.CCodeEditor.SetScrollbars(SCROLLBAR_UNIT, SCROLLBAR_UNIT, 
       
   799                 maxx / SCROLLBAR_UNIT, maxy / SCROLLBAR_UNIT, posx, posy)
       
   800     
       
   801     def OnCCodeEditorResize(self, event):
       
   802         self.RefreshCCodeEditorScrollbars()
       
   803         event.Skip()
       
   804