1/*
2* Copyright (c) 2018 (https://github.com/phase1geo/Minder)
3*
4* This program is free software; you can redistribute it and/or
5* modify it under the terms of the GNU General Public
6* License as published by the Free Software Foundation; either
7* version 2 of the License, or (at your option) any later version.
8*
9* This program is distributed in the hope that it will be useful,
10* but WITHOUT ANY WARRANTY; without even the implied warranty of
11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12* General Public License for more details.
13*
14* You should have received a copy of the GNU General Public
15* License along with this program; if not, write to the
16* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17* Boston, MA 02110-1301 USA
18*
19* Authored by: Trevor Williams <phase1geo@gmail.com>
20*/
21
22using Gtk;
23
24public class UndoNodesInsert : UndoItem {
25
26  struct InsertedNode {
27    Node? parent;
28    Node  n;
29    int   index;
30    bool  parent_folded;
31  }
32
33  private Array<InsertedNode?> _nodes;
34
35  /* Default constructor */
36  public UndoNodesInsert( DrawArea da, Array<Node> nodes ) {
37    base( _( "insert nodes" ) );
38    _nodes = new Array<InsertedNode?>();
39    for( int i=0; i<nodes.length; i++ ) {
40      var node = nodes.index( i );
41      if( node.parent == null ) {
42        _nodes.append_val( { null, node, da.root_index( node ), false } );
43      } else {
44        _nodes.append_val( { node.parent, node, node.index(), node.parent.folded } );
45      }
46    }
47  }
48
49  /* Performs an undo operation for this data */
50  public override void undo( DrawArea da ) {
51    for( int i=0; i<_nodes.length; i++ ) {
52      var node = _nodes.index( i );
53      if( node.parent == null ) {
54        da.remove_root( node.index );
55      } else {
56        if( node.parent_folded ) {
57          node.parent.folded = true;
58        }
59        node.n.detach( node.n.side );
60      }
61    }
62    da.set_current_node( null );
63    da.queue_draw();
64    da.auto_save();
65  }
66
67  /* Performs a redo operation */
68  public override void redo( DrawArea da ) {
69    for( int i=0; i<_nodes.length; i++ ) {
70      var node = _nodes.index( i );
71      if( node.parent == null ) {
72        da.add_root( node.n, node.index );
73      } else {
74        node.parent.folded = node.parent_folded;
75        node.n.attach( node.parent, node.index, null );
76      }
77    }
78    da.set_current_node( _nodes.index( 0 ).n );
79    da.queue_draw();
80    da.auto_save();
81  }
82
83}
84