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 class UndoTextDelete : UndoTextItem {
23
24  public string             text  { private set; get; }
25  public int                start { private set; get; }
26  public Array<UndoTagInfo> tags  { private set; get; }
27
28  /* Default constructor */
29  public UndoTextDelete( string text, int start, Array<UndoTagInfo> tags, int start_cursor, int end_cursor ) {
30    base( _( "text deletion" ), UndoTextOp.DELETE, start_cursor, end_cursor );
31    this.text  = text;
32    this.start = start;
33    this.tags  = tags;
34  }
35
36  /* Causes the stored item to be put into the before state */
37  public override void undo_text( DrawArea da, CanvasText ct ) {
38    ct.text.insert_text( start, text );
39    ct.text.apply_tags( tags, start );
40    ct.set_cursor_only( start_cursor );
41    da.queue_draw();
42  }
43
44  /* Causes the stored item to be put into the after state */
45  public override void redo_text( DrawArea da, CanvasText ct ) {
46    ct.text.remove_text( start, text.length );
47    ct.set_cursor_only( end_cursor );
48    da.queue_draw();
49  }
50
51  /* Merges the given text item with the current only */
52  public override bool merge( CanvasText ct, UndoTextItem item ) {
53    if( (end_cursor == item.start_cursor) && (item.op == UndoTextOp.DELETE) ) {
54      var delete = item as UndoTextDelete;
55      end_cursor = delete.end_cursor;
56      text       = delete.text + text;
57      start      = delete.start;
58      return( true );
59    }
60    return( false );
61  }
62
63}
64