display undo/redo actions in statusbar
[booh] / lib / booh / UndoHandler.rb
1 #!/usr/bin/ruby
2 #
3 #                         *  BOOH  *
4 #
5 # A.k.a `Best web-album Of the world, Or your money back, Humerus'.
6 #
7 # The acronyn sucks, however this is a tribute to Dragon Ball by
8 # Akira Toriyama, where the last enemy beaten by heroes of Dragon
9 # Ball is named "Boo". But there was already a free software project
10 # called Boo, so this one will be it "Booh". Or whatever.
11 #
12 #
13 # Copyright (c) 2004 Guillaume Cottenceau <gc3 at bluewin.ch>
14 #
15 # This software may be freely redistributed under the terms of the GNU
16 # public license version 2.
17 #
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., 675 Mass Ave, Cambridge, MA 02139, USA.
21
22 require 'gettext'
23 include GetText
24 bindtextdomain("booh")
25
26 module UndoHandler
27
28     @undo_actions = []
29     @redo_actions = []
30
31     module_function
32
33     def save_undo(name, closure, params)
34         @undo_actions << { :name => name, :closure => closure, :params => params }
35     end
36
37     def undo(statusbar)
38         todo = @undo_actions.pop
39         redo_closure = todo[:closure].call(*todo[:params])
40         statusbar.pop(0)
41         statusbar.push(0, utf8(_("Undo %s.") % todo[:name]))
42         @redo_actions << { :name => todo[:name], :redo => redo_closure, :undo => todo }
43         return !@undo_actions.empty?
44     end
45
46     def redo(statusbar)
47         redo_item = @redo_actions.pop
48         redo_item[:redo].call
49         statusbar.pop(0)
50         statusbar.push(0, utf8(_("Redo %s.") % redo_item[:name]))
51         @undo_actions << redo_item[:undo]
52         return !@redo_actions.empty?
53     end
54
55     def cleanup
56         @undo_actions.clear
57         @redo_actions.clear
58     end
59
60 end