del net-oscar
[learning-git.git] / pgworksheet_yvesf / pgw / Undo.py
blobff7b0253820e22159b91eb90c72a86bdf312c954
1 # -*- coding: latin-1; -*-
3 # PgWorksheet - PostgreSQL Front End
4 # http://pgworksheet.projects.postgresql.org/
6 # Copyright © 2004-2005 Henri Michelon & CML http://www.e-cml.org/
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License
10 # as published by the Free Software Foundation; either version 2
11 # of the License, or (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details (read LICENSE.txt).
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 # $Id: Undo.py,v 1.2 2005/10/24 14:16:02 hmichelon Exp $
25 # maxium size of the undo stack
26 UNDO_MAX = 1000
29 class UndoAction:
30 """A user action : insert or delete"""
32 def __init__(self, buffer, type, start, end, value):
33 self.buffer = buffer
34 self.type = type
35 self.start = start
36 self.end = end
37 self.value = value
40 class Undo:
41 """Records user actions and apply or revert them"""
43 def __init__(self):
44 self.undo_stack = []
45 self.redo_stack = []
46 # locking is used to avoid recording
47 # our own undo/redo actions
48 self.lock = False
51 def reset(self):
52 """empty the stacks to restart the undo action"""
53 self.undo_stack = []
54 self.redo_stack = []
57 def text_inserted(self, buffer, iter, text, length):
58 """Called by Gtk when text is inserted in a buffer"""
59 if (not self.lock):
60 if (len(self.undo_stack) >= UNDO_MAX):
61 self.undo_stack.pop(0)
62 self.undo_stack.append(UndoAction(buffer, "insert",
63 iter.get_offset(), iter.get_offset() + length,
64 text))
67 def text_deleted(self, buffer, start, end):
68 """Called by Gtk when text is deleted from a buffer"""
69 if (not self.lock):
70 if (len(self.undo_stack) >= UNDO_MAX):
71 self.undo_stack.pop(0)
72 self.undo_stack.append(UndoAction(buffer, "delete",
73 start.get_offset(), end.get_offset(),
74 buffer.get_text(start, end)))
77 def undo(self):
78 """Revert the last action"""
79 if (len(self.undo_stack) == 0):
80 return
81 self.lock = True
82 action = self.undo_stack.pop()
83 if (action.type == "insert"):
84 action.buffer.delete(action.buffer.get_iter_at_offset(action.start),
85 action.buffer.get_iter_at_offset(action.end))
86 elif (action.type == "delete"):
87 action.buffer.insert(action.buffer.get_iter_at_offset(action.start),
88 action.value)
89 self.redo_stack.append(action)
90 self.lock = False
93 def redo(self):
94 """Apply the last reverted action"""
95 if (len(self.redo_stack) == 0):
96 return
97 self.lock = True
98 action = self.redo_stack.pop()
99 if (action.type == "insert"):
100 action.buffer.insert(action.buffer.get_iter_at_offset(action.start),
101 action.value)
102 elif (action.type == "delete"):
103 action.buffer.delete(action.buffer.get_iter_at_offset(action.start),
104 action.buffer.get_iter_at_offset(action.end))
105 self.undo_stack.append(action)
106 self.lock = False