mrg_ui/src/mtktextbuffer.cxx
//============================================================================
// Apply the given change to this text GUI object. If undo_change is not null,
// save the inverse change there, which can be applied later if needed.
// The offset_diff of the affected text relative to the change record
// must be provided.  Sets original_start and orginal_end according to
// the change record.  Sets new_start and new_end to the location of the
// changed region in this text GUI object.
//
Apply the given change to this text GUI object. If undo_change is not null,
save the inverse change there, which can be applied later if needed.
The offset_diff of the affected text relative to the change record
must be provided.  Sets original_start and orginal_end according to
the change record.  Sets new_start and new_end to the location of the
changed region in this text GUI object.
Apply the given change to this text GUI object . If undo_change is not null save the inverse change there which can be applied later if needed . The offset_diff of the affected text relative to the change record must be provided . Sets original_start and orginal_end according to the change record . Sets new_start and new_end to the location of the changed region in this text GUI object .
void textBuffer::ApplyChange(Change *change, Change *undo_change, int &new_start, 
			 int &new_end, int &original_start, int &original_end,
			 int offset_diff)
{
    if(change->operation == DELETE){
	if(undo_change){
	    char *saved_text       = GetText(  change->offset
							  + offset_diff,
                                                          change->length);
	    undo_change->operation = INSERT;
	    undo_change->offset    = change->offset;
	    undo_change->length    = change->length;
	    undo_change->new_text  = saved_text;
	}
	DeleteText(change->offset + offset_diff, change->length);
	original_start = change->offset;
	original_end   = change->offset + change->length;
	new_start      = change->offset + offset_diff;
	new_end        = new_start;
    } else if(change->operation == INSERT){
	if(undo_change){
	    undo_change->operation = DELETE;
	    undo_change->offset    = change->offset;
	    undo_change->length    = change->new_text.length();
	    undo_change->new_text  = "";
	}
	InsertText(change->offset + offset_diff,
                                (char *)change->new_text);
	original_start = change->offset;
	original_end   = original_start;
	new_start      = change->offset + offset_diff;
	new_end        = new_start + change->new_text.length();
    } else if(change->operation == REPLACE){
	if(undo_change){
	    char *saved_text       = GetText(  change->offset
                                                          + offset_diff,
                                                          change->length);
	    undo_change->operation = REPLACE;
	    undo_change->offset    = change->offset;
	    undo_change->length    = change->new_text.length();
	    undo_change->new_text  = saved_text;
	}
	DeleteText(change->offset + offset_diff, change->length);
	InsertText(change->offset + offset_diff,
                                (char *)change->new_text);
	original_start = change->offset;
	original_end   = change->offset + change->length;
	new_start      = change->offset + offset_diff;
	new_end        = new_start + change->new_text.length();
    }
}
void textBuffer :: ApplyChange ( Change * change , Change * undo_change , int & new_start , int & new_end , int & original_start , int & original_end , int offset_diff ) { if ( change -> operation == DELETE ) { if ( undo_change ) { char * saved_text = GetText ( change -> offset + offset_diff , change -> length ) ; undo_change -> operation = INSERT ; undo_change -> offset = change -> offset ; undo_change -> length = change -> length ; undo_change -> new_text = saved_text ; } DeleteText ( change -> offset + offset_diff , change -> length ) ; original_start = change -> offset ; original_end = change -> offset + change -> length ; new_start = change -> offset + offset_diff ; new_end = new_start ; } else if ( change -> operation == INSERT ) { if ( undo_change ) { undo_change -> operation = DELETE ; undo_change -> offset = change -> offset ; undo_change -> length = change -> new_text . length ( ) ; undo_change -> new_text = " " ; } InsertText ( change -> offset + offset_diff , ( char * ) change -> new_text ) ; original_start = change -> offset ; original_end = original_start ; new_start = change -> offset + offset_diff ; new_end = new_start + change -> new_text . length ( ) ; } else if ( change -> operation == REPLACE ) { if ( undo_change ) { char * saved_text = GetText ( change -> offset + offset_diff , change -> length ) ; undo_change -> operation = REPLACE ; undo_change -> offset = change -> offset ; undo_change -> length = change -> new_text . length ( ) ; undo_change -> new_text = saved_text ; } DeleteText ( change -> offset + offset_diff , change -> length ) ; InsertText ( change -> offset + offset_diff , ( char * ) change -> new_text ) ; original_start = change -> offset ; original_end = change -> offset + change -> length ; new_start = change -> offset + offset_diff ; new_end = new_start + change -> new_text . length ( ) ; } }
Apply the given change to this text GUI object.
Apply the given change to this text GUI object .