def __init__(self, editor): self.editor = editor self.cursorItem = CursorLine(editor.canvas) self.timer = Timer(self.timerEvent) self.shown = True self.word = None self.charIx = 0 self.timer.count(cursorOnTime)
class EdCursor: """The cursor marks a particular Word object and a particular character position within that word. """ def __init__(self, editor): self.editor = editor self.cursorItem = CursorLine(editor.canvas) self.timer = Timer(self.timerEvent) self.shown = True self.word = None self.charIx = 0 self.timer.count(cursorOnTime) def stop(self): """Stop the counter """ self.timer.stop() def timerEvent(self): if self.shown: self.cursorItem.hide() self.timer.count(cursorOnTime) self.shown = False else: self.cursorItem.show() self.timer.count(cursorOffTime) self.shown = True def setPos(self, word, cx): """Set the cursor to a particular word/character. It also forces the cursor to be in 'shown' state. """ # Special case: when leaving an empty word that is not the single # word of an empty paragraph, that word should be deleted. if (self.word != word) and self.word and \ (self.word.string == u"") and self.word.tline: wList = self.word.tline.twords if (len(wList) > 1): ix = wList.index(self.word) t = self.word.tline self.word.delete() del(wList[ix]) self.editor.rsubject.renderShortened(wList[0]) self.word = word self.charIx = cx # Get editor view coordinates x, y, h = word.getXYH(cx) self.cursorItem.set(x, y, h) # Force cursor shown self.shown = False # so that the next switch is to 'shown' self.cursorItem.show() self.editor.canvas.ensureVisible(self.cursorItem, 10) # Set gui 'style' buttons according to paragraph style #NOTE: I hope this is the only place where this must be done! self.editor.canvas.signal("currentStyle", self.word.tline.para.align) def getPos(self): """Return cursor position as (word, charIx) pair. """ return (self.word, self.charIx) def step(self, forward): """Move the cursor one character position. """ w = self.word if forward: c = self.charIx + 1 if (c > len(w.string)): # Move to next word wList = w.tline.twords wx = wList.index(w) + 1 if (wx >= len(wList)): # Move to next line tl = self.editor.rsubject.nextLine(w.tline) if not tl: return False w = tl.twords[0] else: w = wList[wx] c = 0 else: # backward c = self.charIx - 1 if (c < 0): # Move to previous word wList = w.tline.twords wx = wList.index(w) - 1 if (wx < 0): # Move to previous line tl = self.editor.rsubject.previousLine(w.tline) if not tl: return False w = tl.twords[-1] else: w = wList[wx] c = len(w.string) self.setPos(w, c) return True def lineStep(self, x, up): """Move the cursor up or down a line. It tries to retain the x-coordinate. """ if up: # Go to previous line tl = self.editor.rsubject.previousLine(self.word.tline) if not tl: return else: # Go to next line tl = self.editor.rsubject.nextLine(self.word.tline) if not tl: return w, c = self.editor.getXPos(tl, x) self.setPos(w, c)
class EdCursor: """The cursor marks a particular Word object and a particular character position within that word. """ def __init__(self, editor): self.editor = editor self.cursorItem = CursorLine(editor.canvas) self.timer = Timer(self.timerEvent) self.shown = True self.word = None self.charIx = 0 self.timer.count(cursorOnTime) def stop(self): """Stop the counter """ self.timer.stop() def timerEvent(self): if self.shown: self.cursorItem.hide() self.timer.count(cursorOnTime) self.shown = False else: self.cursorItem.show() self.timer.count(cursorOffTime) self.shown = True def setPos(self, word, cx): """Set the cursor to a particular word/character. It also forces the cursor to be in 'shown' state. """ # Special case: when leaving an empty word that is not the single # word of an empty paragraph, that word should be deleted. if (self.word != word) and self.word and \ (self.word.string == u"") and self.word.tline: wList = self.word.tline.twords if (len(wList) > 1): ix = wList.index(self.word) t = self.word.tline self.word.delete() del (wList[ix]) self.editor.rsubject.renderShortened(wList[0]) self.word = word self.charIx = cx # Get editor view coordinates x, y, h = word.getXYH(cx) self.cursorItem.set(x, y, h) # Force cursor shown self.shown = False # so that the next switch is to 'shown' self.cursorItem.show() self.editor.canvas.ensureVisible(self.cursorItem, 10) # Set gui 'style' buttons according to paragraph style #NOTE: I hope this is the only place where this must be done! self.editor.canvas.signal("currentStyle", self.word.tline.para.align) def getPos(self): """Return cursor position as (word, charIx) pair. """ return (self.word, self.charIx) def step(self, forward): """Move the cursor one character position. """ w = self.word if forward: c = self.charIx + 1 if (c > len(w.string)): # Move to next word wList = w.tline.twords wx = wList.index(w) + 1 if (wx >= len(wList)): # Move to next line tl = self.editor.rsubject.nextLine(w.tline) if not tl: return False w = tl.twords[0] else: w = wList[wx] c = 0 else: # backward c = self.charIx - 1 if (c < 0): # Move to previous word wList = w.tline.twords wx = wList.index(w) - 1 if (wx < 0): # Move to previous line tl = self.editor.rsubject.previousLine(w.tline) if not tl: return False w = tl.twords[-1] else: w = wList[wx] c = len(w.string) self.setPos(w, c) return True def lineStep(self, x, up): """Move the cursor up or down a line. It tries to retain the x-coordinate. """ if up: # Go to previous line tl = self.editor.rsubject.previousLine(self.word.tline) if not tl: return else: # Go to next line tl = self.editor.rsubject.nextLine(self.word.tline) if not tl: return w, c = self.editor.getXPos(tl, x) self.setPos(w, c)