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