1 # -*- coding: latin-1; -*-
3 # PgWorksheet - PostgreSQL Front End
4 # http://pgworksheet.projects.postgresql.org/
6 # Copyright © 2004-2008 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.4 2008/03/12 20:26:23 hmichelon Exp $
25 # maxium size of the undo stack
30 """A user action : insert or delete"""
32 def __init__(self
, buffer, type, start
, end
, value
):
41 """Records user actions and apply or revert them"""
46 # locking is used to avoid recording
47 # our own undo/redo actions
52 """empty the stacks to restart the undo action"""
57 def text_inserted(self
, buffer, iter, text
, length
):
58 """Called by Gtk when text is inserted in a buffer"""
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
,
67 def text_deleted(self
, buffer, start
, end
):
68 """Called by Gtk when text is deleted from a buffer"""
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
)))
78 """Revert the last action"""
79 if (len(self
.undo_stack
) == 0):
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
),
89 self
.redo_stack
.append(action
)
94 """Apply the last reverted action"""
95 if (len(self
.redo_stack
) == 0):
98 action
= self
.redo_stack
.pop()
99 if (action
.type == "insert"):
100 action
.buffer.insert(action
.buffer.get_iter_at_offset(action
.start
),
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
)