def paste(self, event=None): try: clipboard = self.selection_get(selection='CLIPBOARD') except: return ypos = self.yview()[0] # paste them after the last selected item # bid,lid push them to self so it can be accessed from addLines() # python3 might fix this with the inner scope try: self._bid, self._lid = self._items[self.curselection()[-1]] except: try: self._bid, self._lid = self._items[-1] except: self._bid = 0 self._lid = None selitems = [] undoinfo = [] def addLines(lines): for line in lines.splitlines(): # Create a new block if self._lid is None: self._bid += 1 if self._bid > len(self.gcode.blocks): self._bid = len(self.gcode.blocks) self._lid = MAXINT block = Block() undoinfo.append(self.gcode.addBlockUndo(self._bid,block)) selitems.append((self._bid, None)) else: block = self.gcode.blocks[self._bid] if self._lid == MAXINT: self._lid = len(block) selitems.append((self._bid, len(block))) else: self._lid += 1 selitems.append((self._bid, self._lid)) undoinfo.append(self.gcode.insLineUndo(self._bid, self._lid, line)) try: # try to unpickle it unpickler = pickle.Unpickler(StringIO(clipboard)) try: while True: obj = unpickler.load() if isinstance(obj,tuple): block = Block.load(obj) self._bid += 1 undoinfo.append(self.gcode.addBlockUndo(self._bid, block)) selitems.append((self._bid,None)) self._lid = None else: addLines(obj) except EOFError: pass except pickle.UnpicklingError: # Paste as text addLines(clipboard) if not undoinfo: return self.gcode.addUndo(undoinfo) self.selection_clear(0,END) self.fill() self.yview_moveto(ypos) self.select(selitems, clear=True) #self.selection_set(ACTIVE) #self.see(ACTIVE) self.winfo_toplevel().event_generate("<<Modified>>")
def paste(self, event=None): try: clipboard = self.selection_get(selection='CLIPBOARD') except: return ypos = self.yview()[0] # paste them after the last selected item # bid,lid push them to self so it can be accessed from addLines() # python3 might fix this with the inner scope try: self.__bid, self.__lid = self._items[self.curselection()[-1]] except: try: self.__bid, self.__lid = self._items[-1] except: self.__bid = 0 self.__lid = None selitems = [] undoinfo = [] def addLines(lines): for line in lines.splitlines(): # Create a new block if self.__lid is None: self.__bid += 1 self.__lid = sys.maxint block = Block() undoinfo.append(self.gcode.addBlockUndo(self.__bid,block)) selitems.append((self.__bid, None)) else: block = self.gcode.blocks[self.__bid] if self.__lid == sys.maxint: selitems.append((self.__bid, len(block))) else: self.__lid += 1 selitems.append((self.__bid, self.__lid)) undoinfo.append(self.gcode.insLineUndo(self.__bid, self.__lid, line)) try: # try to unpickle it unpickler = pickle.Unpickler(StringIO(clipboard)) try: while True: obj = unpickler.load() if isinstance(obj,tuple): block = Block.load(obj) self.__bid += 1 undoinfo.append(self.gcode.addBlockUndo(self.__bid, block)) selitems.append((self.__bid,None)) self.__lid = None else: addLines(obj) except EOFError: pass except pickle.UnpicklingError: # Paste as text addLines(clipboard) if not undoinfo: return self.gcode.addUndo(undoinfo) self.selection_clear(0,END) self.fill() self.yview_moveto(ypos) self.select(selitems, clear=True) #self.selection_set(ACTIVE) #self.see(ACTIVE) self.app.event_generate("<<Modified>>")