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 UndoNodeInsert : UndoItem {
25
26  private Node? _parent;
27  private Node  _n;
28  private int   _index;
29  private bool  _parent_folded;
30
31  /* Default constructor */
32  public UndoNodeInsert( Node n, int index ) {
33    base( _( "insert node" ) );
34    _n             = n;
35    _index         = index;
36    _parent        = n.parent;
37    _parent_folded = (_parent == null) ? false : _parent.folded;
38  }
39
40  /* Performs an undo operation for this data */
41  public override void undo( DrawArea da ) {
42    if( _parent == null ) {
43      da.remove_root( _index );
44    } else {
45      if( _parent_folded ) {
46        _parent.folded = true;
47      }
48      _n.detach( _n.side );
49    }
50    if( da.get_current_node() == _n ) {
51      da.set_current_node( null );
52    }
53    da.queue_draw();
54    da.auto_save();
55  }
56
57  /* Performs a redo operation */
58  public override void redo( DrawArea da ) {
59    if( _parent == null ) {
60      da.add_root( _n, _index );
61    } else {
62      _parent.folded = _parent_folded;
63      _n.attach( _parent, _index, null );
64    }
65    da.set_current_node( _n );
66    da.queue_draw();
67    da.auto_save();
68  }
69
70}
71