1/*
2* Copyright (c) 2020 (https://github.com/phase1geo/Outliner)
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
22public struct InsertText {
23  int    start;
24  string text;
25}
26
27public class UndoTextMultiInsert : UndoTextItem {
28
29  private Array<InsertText?> _inserts;
30
31  /* Default constructor */
32  public UndoTextMultiInsert( Array<InsertText?> inserts, int start_cursor, int end_cursor ) {
33    base( _( "text insertion" ), UndoTextOp.INSERT, start_cursor, end_cursor );
34    _inserts = inserts;
35  }
36
37  /* Causes the stored item to be put into the before state */
38  public override void undo_text( DrawArea da, CanvasText ct ) {
39    for( int i=0; i<_inserts.length; i++ ) {
40      var insert = _inserts.index( i );
41      ct.text.remove_text( insert.start, insert.text.length );
42    }
43    ct.set_cursor_only( start_cursor );
44    da.queue_draw();
45  }
46
47  /* Causes the stored item to be put into the after state */
48  public override void redo_text( DrawArea da, CanvasText ct ) {
49    for( int i=(int)(_inserts.length - 1); i>=0; i-- ) {
50      var insert = _inserts.index( i );
51      ct.text.insert_text( insert.start, insert.text );
52    }
53    ct.set_cursor_only( end_cursor );
54    da.queue_draw();
55  }
56
57}
58