1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
|
#----------------------------------------------------------------------
# Name: SourceViews.py
# Purpose: Views for editing source code
#
# Author: Riaan Booysen
#
# Created: 2000/05/05
# RCS-ID: $Id: SourceViews.py,v 1.22 2004/08/16 13:08:32 riaan Exp $
# Copyright: (c) 1999 - 2004 Riaan Booysen
# Licence: GPL
#----------------------------------------------------------------------
print 'importing Views.SourceViews'
import time, os
from StringIO import StringIO
from wxPython.wx import *
from wxPython.stc import *
from Preferences import keyDefs
import Utils
import EditorViews, Search, Help, Preferences, Utils
from StyledTextCtrls import TextSTCMix, idWord, object_delim
from Explorers import ExplorerNodes
endOfLines = { wxSTC_EOL_CRLF : '\r\n',
wxSTC_EOL_CR : '\r',
wxSTC_EOL_LF : '\n'}
markPlaceMrk, linePtrMrk = (1, 2)
markerCnt = 2
wxID_TEXTVIEW = wxNewId()
[wxID_STC_WS, wxID_STC_EOL, wxID_STC_BUF, wxID_STC_IDNT,
wxID_STC_EOL_MODE, wxID_STC_EOL_CRLF, wxID_STC_EOL_LF, wxID_STC_EOL_CR,
] = Utils.wxNewIds(8)
[wxID_CVT_EOL_LF, wxID_CVT_EOL_CRLF, wxID_CVT_EOL_CR] = Utils.wxNewIds(3)
class EditorStyledTextCtrl(wxStyledTextCtrl, EditorViews.EditorView,
EditorViews.FindResultsAdderMixin):
refreshBmp = 'Images/Editor/Refresh.png'
undoBmp = 'Images/Shared/Undo.png'
redoBmp = 'Images/Shared/Redo.png'
cutBmp = 'Images/Shared/Cut.png'
copyBmp = 'Images/Shared/Copy.png'
pasteBmp = 'Images/Shared/Paste.png'
findBmp = 'Images/Shared/Find.png'
findAgainBmp = 'Images/Shared/FindAgain.png'
printBmp = 'Images/Shared/Print.png'
defaultEOL = os.linesep
def __init__(self, parent, wId, model, actions, defaultAction = -1):
wxStyledTextCtrl.__init__(self, parent, wId, style = wxCLIP_CHILDREN | wxSUNKEN_BORDER)
a = (('Refresh', self.OnRefresh, self.refreshBmp, 'Refresh'),
('-', None, '', ''),
('Undo', self.OnEditUndo, self.undoBmp, ''),
('Redo', self.OnEditRedo, self.redoBmp, ''),
('-', None, '', ''),
('Cut', self.OnEditCut, self.cutBmp, ''),
('Copy', self.OnEditCopy, self.copyBmp, ''),
('Paste', self.OnEditPaste, self.pasteBmp, ''),
('-', None, '', ''),
('Find \ Replace', self.OnFind, self.findBmp, 'Find'),
('Find again', self.OnFindAgain, self.findAgainBmp, 'FindAgain'),
('Print...', self.OnPrint, self.printBmp, ''),
('Mark place', self.OnMarkPlace, '-', 'MarkPlace'),
('Goto line', self.OnGotoLine, '-', 'GotoLine'),
('STC settings...', self.OnSTCSettings, '-', ''),
('Convert...', self.OnConvert, '-', ''),
##('Toggle Record macro', self.OnRecordMacro, '-', ''),
##('Playback macro', self.OnPlaybackMacro, '-', ''),
##('-', None, '-', ''),
##('Translate selection (via HTTP)', self.OnTranslate, '-', ''),
##('Spellcheck selection (via HTTP)', self.OnSpellCheck, '-', ''),
)
EditorViews.EditorView.__init__(self, model, a + actions, defaultAction)
self.eol = None
self.eolsChecked = false
self.pos = 0
self.stepPos = 0
self.nonUserModification = false
self.lastSearchResults = []
self.lastSearchPattern = ''
self.lastMatchPosition = None
## Install the handler for refreshs.
if wxPlatform == '__WXGTK__' and Preferences.edUseCustomSTCPaintEvtHandler:
self.paint_handler = Utils.PaintEventHandler(self)
self.lastStart = 0
self._blockUpdate = false
self._marking = false
markIdnt, markBorder, markCenter = Preferences.STCMarkPlaceMarker
self.MarkerDefine(markPlaceMrk, markIdnt, markBorder, markCenter)
markIdnt, markBorder, markCenter = Preferences.STCLinePointer
self.MarkerDefine(linePtrMrk , markIdnt, markBorder, markCenter)
self._linePtrHdl = None
EVT_STC_MARGINCLICK(self, wId, self.OnMarginClick)
EVT_STC_MACRORECORD(self, wId, self.OnRecordingMacro)
EVT_MENU(self, wxID_STC_WS, self.OnSTCSettingsWhiteSpace)
EVT_MENU(self, wxID_STC_EOL, self.OnSTCSettingsEOL)
EVT_MENU(self, wxID_STC_BUF, self.OnSTCSettingsBufferedDraw)
EVT_MENU(self, wxID_STC_IDNT, self.OnSTCSettingsIndentGuide)
EVT_MENU(self, wxID_STC_EOL_CRLF, self.OnChangeEOLMode)
EVT_MENU(self, wxID_STC_EOL_LF, self.OnChangeEOLMode)
EVT_MENU(self, wxID_STC_EOL_CR, self.OnChangeEOLMode)
EVT_MENU(self, wxID_CVT_EOL_CRLF, self.OnConvertEols)
EVT_MENU(self, wxID_CVT_EOL_LF, self.OnConvertEols)
EVT_MENU(self, wxID_CVT_EOL_CR, self.OnConvertEols)
EVT_MIDDLE_UP(self, self.OnEditPasteSelection)
def getModelData(self):
return self.model.data
def setModelData(self, data):
self.model.data = data
def saveNotification(self):
if not Preferences.neverEmptyUndoBuffer:
self.EmptyUndoBuffer()
def refreshCtrl(self):
self.pos = self.GetCurrentPos()
selection = self.GetSelection()
prevVsblLn = self.GetFirstVisibleLine()
self._blockUpdate = true
try:
newData = self.getModelData()
curData = Utils.stringFromControl(self.GetText())
if newData != curData:
resetUndo = not self.CanUndo() and not curData
ro = self.GetReadOnly()
self.SetReadOnly(false)
self.SetText(Utils.stringToControl(newData))
self.SetReadOnly(ro)
if resetUndo:
self.EmptyUndoBuffer()
self.GotoPos(self.pos)
curVsblLn = self.GetFirstVisibleLine()
self.LineScroll(0, prevVsblLn - curVsblLn)
# XXX not preserving selection
self.SetSelection(*selection)
finally:
self._blockUpdate = false
if self.eol is None:
self.eol = Utils.getEOLMode(newData, self.defaultEOL)
self.SetEOLMode({'\r\n': wxSTC_EOL_CRLF,
'\r': wxSTC_EOL_CR,
'\n': wxSTC_EOL_LF}[self.eol])
if not self.eolsChecked:
if Utils.checkMixedEOLs(newData):
wxLogWarning('Mixed EOLs detected in %s, please use '
'Edit->Convert... to fix this problem.'\
%os.path.basename(self.model.filename))
self.eolsChecked = true
self.SetSavePoint()
self.nonUserModification = false
self.updatePageName()
self.updateFromAttrs()
def updateFromAttrs(self):
if self.model.transport:
self.SetReadOnly(self.model.transport.stdAttrs['read-only'])
def refreshModel(self):
if self.isModified():
self.model.modified = true
self.nonUserModification = false
pos = self.GetCurrentPos()
prevVsblLn = self.GetFirstVisibleLine()
sel = self.GetSelection()
self.setModelData(str(self.GetText()))
self.GotoPos(pos)
self.SetSelection(*sel)
curVsblLn = self.GetFirstVisibleLine()
self.LineScroll(0, prevVsblLn - curVsblLn)
self.SetSavePoint()
if wxPlatform == '__WXGTK__':
# We are updating the model from the editor view.
# this flag is to prevent the model updating the view
self.noredraw = 1
EditorViews.EditorView.refreshModel(self)
self.noredraw = 0
# Remove from modified views list
if self.model.viewsModified.count(self.viewName):
self.model.viewsModified.remove(self.viewName)
self.updateEditor()
def gotoLine(self, lineno, offset = -1):
self.GotoLine(lineno)
vl = self.GetFirstVisibleLine()
self.LineScroll(0, lineno - vl)
if offset != -1: self.SetCurrentPos(self.GetCurrentPos()+offset+1)
def selectSection(self, lineno, start, word):
self.gotoLine(lineno)
length = len(word)
startPos = self.PositionFromLine(lineno) + start
endPos = startPos + length
self.SetSelection(startPos, endPos)
self.SetFocus()
def selectLine(self, lineno):
self.GotoLine(lineno)
sp = self.PositionFromLine(lineno)
# Dont do whole screen selection
ep = max(0, self.PositionFromLine(lineno+1)-1)
self.SetSelection(sp, ep)
def insertCodeBlock(self, text):
cp = self.GetCurrentPos()
ln = self.LineFromPosition(cp)
indent = cp - self.PositionFromLine(ln)
lns = text.split(self.eol)
# XXX adapt for tab mode
text = (self.eol+indent*' ').join(lns)
selTxtPos = text.find('# Your code')
self.InsertText(cp, text)
self.nonUserModification = true
self.updateViewState()
self.SetFocus()
if selTxtPos != -1:
self.SetSelection(cp + selTxtPos, cp + selTxtPos + 11)
def isModified(self):
return self.GetModify() or self.nonUserModification
#---Block commands--------------------------------------------------------------
def reselectSelectionAsBlock(self):
selStartPos, selEndPos = self.GetSelection()
selStartLine = self.LineFromPosition(selStartPos)
startPos = self.PositionFromLine(selStartLine)
selEndLine = self.LineFromPosition(selEndPos-1)#to handle cursor under sel
endPos = self.GetLineEndPosition(selEndLine)
startPos = self.PositionFromLine(selStartLine)
self.SetSelection(startPos, endPos)
return selStartLine, selEndLine
def processSelectionBlock(self, func):
if self.GetUseTabs():
indtBlock = '\t'
else:
indtBlock = self.GetTabWidth()*' '
self.BeginUndoAction()
try:
sls, sle = self.reselectSelectionAsBlock()
lines = StringIO(str(self.GetSelectedText())).readlines()
text = ''.join(func(lines, indtBlock))
self.ReplaceSelection(text)
self.SetSelection(self.PositionFromLine(sls),
self.GetLineEndPosition(sle))
finally:
self.EndUndoAction()
def getSelectionAsLineNumbers(self):
selStartPos, selEndPos = self.GetSelection()
selStartLine = self.LineFromPosition(selStartPos)
selEndLine = self.LineFromPosition(selEndPos)
return range(self.LineFromPosition(selStartPos),
self.LineFromPosition(selEndPos))
#-------------------------------------------------------------------------------
def setLinePtr(self, lineNo):
if self._linePtrHdl:
self.MarkerDeleteHandle(self._linePtrHdl)
self._linePtrHdl = None
if lineNo >= 0:
# XXX temp while handle returns None
self.MarkerDeleteAll(linePtrMrk)
self._linePtrHdl = self.MarkerAdd(lineNo, linePtrMrk)
def gotoBrowseMarker(self, marker):
self.GotoLine(marker)
self.setLinePtr(marker)
EditorViews.EditorView.gotoBrowseMarker(self, marker)
#-------Canned events-----------------------------------------------------------
def OnRefresh(self, event):
self.refreshModel()
def OnEditCut(self, event):
self.Cut()
def OnEditCopy(self, event):
self.Copy()
def OnEditPaste(self, event):
self.Paste()
def OnEditPasteSelection(self, event):
# XXX I'm limiting this to GTK for the moment, too non standard for MSW
# XXX Maybe this should rather be a preference
if wxPlatform == '__WXGTK__':
text = self.GetSelectedText()
pos = self.PositionFromPoint(event.GetPosition())
self.InsertText(pos, text)
self.SetSelection(pos, pos + len(text))
def OnEditUndo(self, event):
self.Undo()
def OnEditRedo(self, event):
self.Redo()
# XXX
def doFind(self, pattern):
self.lastSearchResults = Search.findInText(\
self.GetText().split(self.eol), pattern, false)
self.lastSearchPattern = pattern
if len(self.lastSearchResults):
self.lastMatchPosition = 0
def doNextMatch(self):
if self.lastMatchPosition is not None and \
len(self.lastSearchResults) > self.lastMatchPosition:
pos = self.lastSearchResults[self.lastMatchPosition]
self.model.editor.addBrowseMarker(self.GetCurrentLine())
self.selectSection(pos[0], pos[1], self.lastSearchPattern)
self.lastMatchPosition = self.lastMatchPosition + 1
else:
dlg = wxMessageDialog(self.model.editor,
'No%smatches'% (self.lastMatchPosition is not None and ' further ' or ' '),
'Find in module', wxOK | wxICON_INFORMATION)
dlg.ShowModal()
dlg.Destroy()
self.lastMatchPosition = None
def OnFind(self, event):
import FindReplaceDlg
FindReplaceDlg.find(self, self.model.editor.finder, self)
def OnFindAgain(self, event):
import FindReplaceDlg
FindReplaceDlg.findAgain(self, self.model.editor.finder, self)
def OnMarkPlace(self, event):
if self._marking : return
self._marking = true
try:
lineno = self.LineFromPosition(self.GetCurrentPos())
self.MarkerAdd(lineno, markPlaceMrk)
self.model.editor.addBrowseMarker(lineno)
self.model.editor.setStatus('Code marker added to Browse History', ringBell=true)
# Encourage a redraw
wxYield()
time.sleep(0.125)
self.MarkerDelete(lineno, markPlaceMrk)
finally:
self._marking = false
def OnGotoLine(self, event):
dlg = wxTextEntryDialog(self, 'Enter line number:', 'Goto line', '')
try:
if dlg.ShowModal() == wxID_OK:
if dlg.GetValue():
try:
lineNo = int(dlg.GetValue())
except ValueError:
wxLogError('Integer line number required')
else:
self.GotoLine(lineNo)
finally:
dlg.Destroy()
def OnUpdateUI(self, event):
if hasattr(self, 'pageIdx'):
self.updateViewState()
l, col = self.GetCurLine()
self.model.editor.statusBar.setColumnPos(col)
def OnTranslate(self, event):
# XXX web service no longer works
import TranslateDlg
dlg = TranslateDlg.create(None, self.GetSelectedText())
try:
if dlg.ShowModal() == wxOK and len(dlg.translated) > 1:
self.ReplaceSelection(dlg.translated[1])
finally:
dlg.Destroy()
def OnSpellCheck(self, event):
# XXX web service no longer works
import TranslateDlg
self.model.editor.setStatus('Spell checking...', 'Warning')
wxBeginBusyCursor()
try:
self.ReplaceSelection(TranslateDlg.spellCheck(self.GetSelectedText()))
finally:
wxEndBusyCursor()
self.model.editor.setStatus('Spelling checked', 'Info')
def OnMarginClick(self, event):
pass
#---STC Settings----------------------------------------------------------------
def OnSTCSettings(self, event):
menu = wxMenu()
menu.Append(wxID_STC_WS, 'View Whitespace', '', 1) #checkable
menu.Check(wxID_STC_WS, self.GetViewWhiteSpace())
menu.Append(wxID_STC_BUF, 'Buffered draw', '', 1) #checkable
menu.Check(wxID_STC_BUF, self.GetBufferedDraw())
menu.Append(wxID_STC_IDNT, 'Use indentation guides', '', 1) #checkable
menu.Check(wxID_STC_IDNT, self.GetIndentationGuides())
menu.Append(wxID_STC_EOL, 'View EOL symbols', '', 1) #checkable
menu.Check(wxID_STC_EOL, self.GetViewEOL())
menu.AppendSeparator()
eolModeMenu = wxMenu()
eolModeMenu.Append(wxID_STC_EOL_CRLF, 'CRLF', '', wxITEM_RADIO)
eolModeMenu.Check(wxID_STC_EOL_CRLF, self.GetEOLMode() == wxSTC_EOL_CRLF)
eolModeMenu.Append(wxID_STC_EOL_LF, 'LF', '', wxITEM_RADIO)
eolModeMenu.Check(wxID_STC_EOL_LF, self.GetEOLMode() == wxSTC_EOL_LF)
eolModeMenu.Append(wxID_STC_EOL_CR, 'CR', '', wxITEM_RADIO)
eolModeMenu.Check(wxID_STC_EOL_CR, self.GetEOLMode() == wxSTC_EOL_CR)
menu.AppendMenu(wxID_STC_EOL_MODE, 'EOL mode', eolModeMenu)
s = self.GetClientSize()
self.PopupMenuXY(menu, s.x/2, s.y/2)
menu.Destroy()
def _getEventChecked(self, event):
checked = not event.IsChecked()
if wxPlatform == '__WXGTK__':
return not checked
else:
return checked
def OnSTCSettingsWhiteSpace(self, event):
self.SetViewWhiteSpace(self._getEventChecked(event))
def OnSTCSettingsEOL(self, event):
self.SetViewEOL(self._getEventChecked(event))
def OnSTCSettingsBufferedDraw(self, event):
self.SetBufferedDraw(self._getEventChecked(event))
def OnSTCSettingsIndentGuide(self, event):
self.SetIndentationGuides(self._getEventChecked(event))
def OnChangeEOLMode(self, event):
eol = {wxID_STC_EOL_CRLF: wxSTC_EOL_CRLF,
wxID_STC_EOL_LF: wxSTC_EOL_LF,
wxID_STC_EOL_CR: wxSTC_EOL_CR}[event.GetId()]
self.SetEOLMode(eol)
#-------------------------------------------------------------------------------
def OnConvert(self, event):
menu = wxMenu()
menu.Append(wxID_CVT_EOL_CRLF, 'EOLs to CRLF')
menu.Append(wxID_CVT_EOL_LF, 'EOLs to LF')
menu.Append(wxID_CVT_EOL_CR, 'EOLs to CR')
s = self.GetClientSize()
self.PopupMenuXY(menu, s.x/2, s.y/2)
menu.Destroy()
def OnConvertEols(self, event):
eol = {wxID_CVT_EOL_CRLF: wxSTC_EOL_CRLF,
wxID_CVT_EOL_LF: wxSTC_EOL_LF,
wxID_CVT_EOL_CR: wxSTC_EOL_CR}[event.GetId()]
self.ConvertEOLs(eol)
#---Macro recording/playback----------------------------------------------------
_recordingMacro = false
_recordedMacro = false
stcMacroCmds = ()
def OnRecordMacro(self, event):
if self._recordingMacro:
self.model.editor.setStatus('Macro recorded', ringBell=true)
self.StopRecord()
self._recordedMacro = true
else:
self.model.editor.setStatus('Recording macro...', 'Warning')
self.stcMacroCmds = []
self.StartRecord()
self._recordingMacro = not self._recordingMacro
def OnPlaybackMacro(self, event):
if self._recordedMacro:
for stcMsg, stcLPrm in self.stcMacroCmds:
self.CmdKeyExecute(stcMsg)
self.model.editor.setStatus('Macro executed')
def OnRecordingMacro(self, event):
data = (event.GetMessage(), event.GetLParam())
self.model.editor.setStatus('Recording macro: %s'%str(data), 'Warning')
self.stcMacroCmds.append(data)
def OnPrint(self, event):
import STCPrinting
dlg = STCPrinting.STCPrintDlg(self.model.editor, self, self.model.filename)
dlg.ShowModal()
dlg.Destroy()
class TextView(EditorStyledTextCtrl, TextSTCMix):
viewName = 'Text'
def __init__(self, parent, model, actions=()):
EditorStyledTextCtrl.__init__(self, parent, wxID_TEXTVIEW, model,
actions, -1)
TextSTCMix.__init__(self, wxID_TEXTVIEW)
self.active = true
ExplorerNodes.langStyleInfoReg.append(
('Text', 'text', TextSTCMix, 'stc-styles.rc.cfg') )
|