TextViewer.py
changeset 231 fc2d6cbb8b39
parent 145 4fb225afddf4
child 235 7b58a3b5b6ec
equal deleted inserted replaced
230:45d70748e45a 231:fc2d6cbb8b39
    39 for i in xrange(26):
    39 for i in xrange(26):
    40     LETTERS.append(chr(ord('a') + i))
    40     LETTERS.append(chr(ord('a') + i))
    41     LETTERS.append(chr(ord('A') + i))
    41     LETTERS.append(chr(ord('A') + i))
    42 
    42 
    43 [STC_PLC_WORD, STC_PLC_COMMENT, STC_PLC_NUMBER, STC_PLC_VARIABLE, 
    43 [STC_PLC_WORD, STC_PLC_COMMENT, STC_PLC_NUMBER, STC_PLC_VARIABLE, 
    44  STC_PLC_FUNCTION, STC_PLC_JUMP] = range(6)
    44  STC_PLC_FUNCTION, STC_PLC_JUMP, STC_PLC_ERROR] = range(7)
    45 [SPACE, WORD, NUMBER, COMMENT] = range(4)
    45 [SPACE, WORD, NUMBER, COMMENT] = range(4)
    46 
    46 
    47 [ID_TEXTVIEWER,
    47 [ID_TEXTVIEWER,
    48 ] = [wx.NewId() for _init_ctrls in range(1)]
    48 ] = [wx.NewId() for _init_ctrls in range(1)]
    49 
    49 
   138         self.StyleSetSpec(STC_PLC_VARIABLE, "fore:#7F0000,size:%(size)d" % faces)
   138         self.StyleSetSpec(STC_PLC_VARIABLE, "fore:#7F0000,size:%(size)d" % faces)
   139         self.StyleSetSpec(STC_PLC_FUNCTION, "fore:#7F7F00,size:%(size)d" % faces)
   139         self.StyleSetSpec(STC_PLC_FUNCTION, "fore:#7F7F00,size:%(size)d" % faces)
   140         self.StyleSetSpec(STC_PLC_COMMENT, "fore:#7F7F7F,size:%(size)d" % faces)
   140         self.StyleSetSpec(STC_PLC_COMMENT, "fore:#7F7F7F,size:%(size)d" % faces)
   141         self.StyleSetSpec(STC_PLC_NUMBER, "fore:#007F7F,size:%(size)d" % faces)
   141         self.StyleSetSpec(STC_PLC_NUMBER, "fore:#007F7F,size:%(size)d" % faces)
   142         self.StyleSetSpec(STC_PLC_JUMP, "fore:#007F00,size:%(size)d" % faces)
   142         self.StyleSetSpec(STC_PLC_JUMP, "fore:#007F00,size:%(size)d" % faces)
       
   143         self.StyleSetSpec(STC_PLC_ERROR, "fore:#FF0000,back:#FFFF00,size:%(size)d" % faces)
   143         
   144         
   144         # Indicators styles
   145         # Indicators styles
   145         self.IndicatorSetStyle(0, wx.stc.STC_INDIC_SQUIGGLE)
   146         self.IndicatorSetStyle(0, wx.stc.STC_INDIC_SQUIGGLE)
   146         if window and controler:
   147         if window and controler:
   147             self.IndicatorSetForeground(0, wx.RED)
   148             self.IndicatorSetForeground(0, wx.RED)
   163         self.EnumeratedValues = []
   164         self.EnumeratedValues = []
   164         self.DisableEvents = True
   165         self.DisableEvents = True
   165         self.TextSyntax = "ST"
   166         self.TextSyntax = "ST"
   166         self.CurrentAction = None
   167         self.CurrentAction = None
   167         self.TagName = tagname
   168         self.TagName = tagname
       
   169         self.Errors = []
   168         
   170         
   169         self.ParentWindow = window
   171         self.ParentWindow = window
   170         self.Controler = controler
   172         self.Controler = controler
   171 
   173 
   172         self.SetModEventMask(wx.stc.STC_MOD_BEFOREINSERT|wx.stc.STC_MOD_BEFOREDELETE)
   174         self.SetModEventMask(wx.stc.STC_MOD_BEFOREINSERT|wx.stc.STC_MOD_BEFOREDELETE)
   292         self.EnumeratedValues = []
   294         self.EnumeratedValues = []
   293         for value in self.Controler.GetEnumeratedDataValues():
   295         for value in self.Controler.GetEnumeratedDataValues():
   294             self.EnumeratedValues.append(value.upper())
   296             self.EnumeratedValues.append(value.upper())
   295         
   297         
   296         self.Colourise(0, -1)
   298         self.Colourise(0, -1)
   297     
   299         
   298     def RefreshScaling(self, refresh=True):
   300     def RefreshScaling(self, refresh=True):
   299         pass
   301         pass
   300     
   302     
   301     def OnStyleNeeded(self, event):
   303     def OnStyleNeeded(self, event):
   302         self.TextChanged = True
   304         self.TextChanged = True
   303         line = self.LineFromPosition(self.GetEndStyled())
   305         line = self.LineFromPosition(self.GetEndStyled())
   304         if line == 0:
   306         if line == 0:
   305             start_pos = 0
   307             start_pos = last_styled_pos = 0
   306         else:
   308         else:
   307             start_pos = self.GetLineEndPosition(line - 1) + 1
   309             start_pos = last_styled_pos = self.GetLineEndPosition(line - 1) + 1
   308         end_pos = event.GetPosition()
   310         end_pos = event.GetPosition()
   309         self.StartStyling(start_pos, 0xff)
   311         self.StartStyling(start_pos, 0xff)
   310         
   312         
   311         i = start_pos
   313         current_pos = last_styled_pos
   312         state = SPACE
   314         state = SPACE
   313         line = ""
   315         line = ""
   314         word = ""
   316         word = ""
   315         while i < end_pos:
   317         while current_pos < end_pos:
   316             char = chr(self.GetCharAt(i)).upper()
   318             char = chr(self.GetCharAt(current_pos)).upper()
   317             line += char
   319             line += char
   318             if char == NEWLINE:
   320             if char == NEWLINE:
   319                 if state == COMMENT:
   321                 if state == COMMENT:
   320                     self.SetStyling(i - start_pos + 1, STC_PLC_COMMENT)
   322                     self.SetStyling(current_pos - last_styled_pos + 1, STC_PLC_COMMENT)
   321                 elif state == NUMBER:
   323                 elif state == NUMBER:
   322                     self.SetStyling(i - start_pos, STC_PLC_NUMBER)
   324                     self.SetStyling(current_pos - last_styled_pos, STC_PLC_NUMBER)
   323                 elif state == WORD:
   325                 elif state == WORD:
   324                     if word in self.Keywords:
   326                     if word in self.Keywords:
   325                         self.SetStyling(i - start_pos, STC_PLC_WORD)
   327                         self.SetStyling(current_pos - last_styled_pos, STC_PLC_WORD)
   326                     elif word in self.Variables:
   328                     elif word in self.Variables:
   327                         self.SetStyling(i - start_pos, STC_PLC_VARIABLE)
   329                         self.SetStyling(current_pos - last_styled_pos, STC_PLC_VARIABLE)
   328                     elif word in self.Functions:
   330                     elif word in self.Functions:
   329                         self.SetStyling(i - start_pos, STC_PLC_FUNCTION)
   331                         self.SetStyling(current_pos - last_styled_pos, STC_PLC_FUNCTION)
   330                     elif word in self.Jumps:
   332                     elif word in self.Jumps:
   331                         self.SetStyling(i - start_pos, STC_PLC_JUMP)
   333                         self.SetStyling(current_pos - last_styled_pos, STC_PLC_JUMP)
   332                     elif word in self.EnumeratedValues:
   334                     elif word in self.EnumeratedValues:
   333                         self.SetStyling(i - start_pos, STC_PLC_NUMBER)
   335                         self.SetStyling(current_pos - last_styled_pos, STC_PLC_NUMBER)
   334                     else:
   336                     else:
   335                         self.SetStyling(i - start_pos, 31)
   337                         self.SetStyling(current_pos - last_styled_pos, 31)
   336                         if self.GetCurrentPos() < start_pos or self.GetCurrentPos() > i:
   338                         if self.GetCurrentPos() < last_styled_pos or self.GetCurrentPos() > current_pos:
   337                             self.StartStyling(start_pos, wx.stc.STC_INDICS_MASK)
   339                             self.StartStyling(last_styled_pos, wx.stc.STC_INDICS_MASK)
   338                             self.SetStyling(i - start_pos, wx.stc.STC_INDIC0_MASK)
   340                             self.SetStyling(current_pos - last_styled_pos, wx.stc.STC_INDIC0_MASK)
   339                             self.StartStyling(i, 0xff)    
   341                             self.StartStyling(current_pos, 0xff)
   340                 else:
   342                 else:
   341                     self.SetStyling(i - start_pos, 31)
   343                     self.SetStyling(current_pos - last_styled_pos, 31)
   342                 start_pos = i
   344                 last_styled_pos = current_pos
   343                 state = SPACE
   345                 state = SPACE
   344                 line = ""
   346                 line = ""
   345             elif line.endswith("(*") and state != COMMENT:
   347             elif line.endswith("(*") and state != COMMENT:
   346                 self.SetStyling(i - start_pos - 1, 31)
   348                 self.SetStyling(current_pos - last_styled_pos - 1, 31)
   347                 start_pos = i
   349                 last_styled_pos = current_pos
   348                 state = COMMENT
   350                 state = COMMENT
   349             elif state == COMMENT:
   351             elif state == COMMENT:
   350                 if line.endswith("*)"):
   352                 if line.endswith("*)"):
   351                     self.SetStyling(i - start_pos + 2, STC_PLC_COMMENT)
   353                     self.SetStyling(current_pos - last_styled_pos + 2, STC_PLC_COMMENT)
   352                     start_pos = i + 1
   354                     last_styled_pos = current_pos + 1
   353                     state = SPACE
   355                     state = SPACE
   354             elif char in LETTERS:
   356             elif char in LETTERS:
   355                 if state == NUMBER:
   357                 if state == NUMBER:
   356                     word = "#"
   358                     word = "#"
   357                     state = WORD
   359                     state = WORD
   358                 elif state == SPACE:
   360                 elif state == SPACE:
   359                     self.SetStyling(i - start_pos, 31)
   361                     self.SetStyling(current_pos - last_styled_pos, 31)
   360                     word = char
   362                     word = char
   361                     start_pos = i
   363                     last_styled_pos = current_pos
   362                     state = WORD
   364                     state = WORD
   363                 else:
   365                 else:
   364                     word += char
   366                     word += char
   365             elif char in NUMBERS or char == '.' and state != WORD:
   367             elif char in NUMBERS or char == '.' and state != WORD:
   366                 if state == SPACE:
   368                 if state == SPACE:
   367                     self.SetStyling(i - start_pos, 31)
   369                     self.SetStyling(current_pos - last_styled_pos, 31)
   368                     start_pos = i
   370                     last_styled_pos = current_pos
   369                     state = NUMBER
   371                     state = NUMBER
   370                 if state == WORD and char != '.':
   372                 if state == WORD and char != '.':
   371                     word += char
   373                     word += char
   372             else:
   374             else:
   373                 if state == WORD:
   375                 if state == WORD:
   374                     if word in self.Keywords:
   376                     if word in self.Keywords:
   375                         self.SetStyling(i - start_pos, STC_PLC_WORD)
   377                         self.SetStyling(current_pos - last_styled_pos, STC_PLC_WORD)
   376                     elif word in self.Variables:
   378                     elif word in self.Variables:
   377                         self.SetStyling(i - start_pos, STC_PLC_VARIABLE)
   379                         self.SetStyling(current_pos - last_styled_pos, STC_PLC_VARIABLE)
   378                     elif word in self.Functions:
   380                     elif word in self.Functions:
   379                         self.SetStyling(i - start_pos, STC_PLC_FUNCTION)
   381                         self.SetStyling(current_pos - last_styled_pos, STC_PLC_FUNCTION)
   380                     elif word in self.Jumps:
   382                     elif word in self.Jumps:
   381                         self.SetStyling(i - start_pos, STC_PLC_JUMP)
   383                         self.SetStyling(current_pos - last_styled_pos, STC_PLC_JUMP)
   382                     elif word in self.EnumeratedValues:
   384                     elif word in self.EnumeratedValues:
   383                         self.SetStyling(i - start_pos, STC_PLC_NUMBER)
   385                         self.SetStyling(current_pos - last_styled_pos, STC_PLC_NUMBER)
   384                     else:
   386                     else:
   385                         self.SetStyling(i - start_pos, 31)
   387                         self.SetStyling(current_pos - last_styled_pos, 31)
   386                         if self.GetCurrentPos() < start_pos or self.GetCurrentPos() > i:
   388                         if self.GetCurrentPos() < last_styled_pos or self.GetCurrentPos() > current_pos:
   387                             self.StartStyling(start_pos, wx.stc.STC_INDICS_MASK)
   389                             self.StartStyling(last_styled_pos, wx.stc.STC_INDICS_MASK)
   388                             self.SetStyling(i - start_pos, wx.stc.STC_INDIC0_MASK)
   390                             self.SetStyling(current_pos - last_styled_pos, wx.stc.STC_INDIC0_MASK)
   389                             self.StartStyling(i, 0xff)
   391                             self.StartStyling(current_pos, 0xff)
   390                     word = ""
   392                     word = ""
   391                     start_pos = i
   393                     last_styled_pos = current_pos
   392                     state = SPACE
   394                     state = SPACE
   393                 elif state == NUMBER:
   395                 elif state == NUMBER:
   394                     self.SetStyling(i - start_pos, STC_PLC_NUMBER)
   396                     self.SetStyling(current_pos - last_styled_pos, STC_PLC_NUMBER)
   395                     start_pos = i
   397                     last_styled_pos = current_pos
   396                     state = SPACE
   398                     state = SPACE
   397             i += 1
   399             current_pos += 1
   398         if state == COMMENT:
   400         if state == COMMENT:
   399             self.SetStyling(i - start_pos + 2, STC_PLC_COMMENT)
   401             self.SetStyling(current_pos - last_styled_pos + 2, STC_PLC_COMMENT)
   400         elif state == NUMBER:
   402         elif state == NUMBER:
   401             self.SetStyling(i - start_pos, STC_PLC_NUMBER)
   403             self.SetStyling(current_pos - last_styled_pos, STC_PLC_NUMBER)
   402         elif state == WORD:
   404         elif state == WORD:
   403             if word in self.Keywords:
   405             if word in self.Keywords:
   404                 self.SetStyling(i - start_pos, STC_PLC_WORD)
   406                 self.SetStyling(current_pos - last_styled_pos, STC_PLC_WORD)
   405             elif word in self.Variables:
   407             elif word in self.Variables:
   406                 self.SetStyling(i - start_pos, STC_PLC_VARIABLE)
   408                 self.SetStyling(current_pos - last_styled_pos, STC_PLC_VARIABLE)
   407             elif word in self.Functions:
   409             elif word in self.Functions:
   408                 self.SetStyling(i - start_pos, STC_PLC_FUNCTION)
   410                 self.SetStyling(current_pos - last_styled_pos, STC_PLC_FUNCTION)
   409             elif word in self.Jumps:
   411             elif word in self.Jumps:
   410                 self.SetStyling(i - start_pos, STC_PLC_JUMP)
   412                 self.SetStyling(current_pos - last_styled_pos, STC_PLC_JUMP)
   411             elif word in self.EnumeratedValues:
   413             elif word in self.EnumeratedValues:
   412                 self.SetStyling(i - start_pos, STC_PLC_NUMBER)
   414                 self.SetStyling(current_pos - last_styled_pos, STC_PLC_NUMBER)
   413             else:
   415             else:
   414                 self.SetStyling(i - start_pos, 31)
   416                 self.SetStyling(current_pos - last_styled_pos, 31)
   415         else:
   417         else:
   416             self.SetStyling(i - start_pos, 31)
   418             self.SetStyling(current_pos - start_pos, 31)
       
   419         self.ShowErrors(start_pos, end_pos)
   417         event.Skip()
   420         event.Skip()
   418     
   421     
   419     def Cut(self):
   422     def Cut(self):
   420         self.ResetBuffer()
   423         self.ResetBuffer()
   421         self.CmdKeyExecute(wx.stc.STC_CMD_CUT)
   424         self.CmdKeyExecute(wx.stc.STC_CMD_CUT)
   478 
   481 
   479     def OnKillFocus(self, event):
   482     def OnKillFocus(self, event):
   480         self.AutoCompCancel()
   483         self.AutoCompCancel()
   481         event.Skip()
   484         event.Skip()
   482 
   485 
       
   486 #-------------------------------------------------------------------------------
       
   487 #                        Errors showing functions
       
   488 #-------------------------------------------------------------------------------
       
   489 
       
   490     def ClearErrors(self):
       
   491         self.Errors = []
       
   492         self.RefreshView()
       
   493 
       
   494     def AddShownError(self, infos, start, end):
       
   495         if infos[0] == "body":
       
   496             self.Errors.append((infos[1], start, end))
       
   497 
       
   498     def ShowErrors(self, start_pos, end_pos):
       
   499         for indent, start, end in self.Errors:
       
   500             if start[0] == 0:
       
   501                 error_start_pos = start[1] - indent
       
   502             else:
       
   503                 error_start_pos = self.GetLineEndPosition(start[0] - 1) + start[1] - indent + 1
       
   504             if end[0] == 0:
       
   505                 error_end_pos = end[1] - indent + 1
       
   506             else:
       
   507                 error_end_pos = self.GetLineEndPosition(end[0] - 1) + end[1] - indent + 2
       
   508             if start_pos <= error_start_pos <= end_pos or start_pos <= error_end_pos <= end_pos:
       
   509                 self.StartStyling(error_start_pos, 0xff)
       
   510                 self.SetStyling(error_end_pos - error_start_pos, STC_PLC_ERROR)