4 #include "KUndoBuffer.h"
7 KUndoItem::KUndoItem(const char* redo_text
, int32 length
, int32 offset
,
8 undo_type history
, int32 cursor_pos
)
13 CursorPos
= cursor_pos
;
15 if (redo_text
!= NULL
) {
16 RedoText
= (char*)malloc(length
);
17 memcpy(RedoText
, redo_text
, length
);
26 KUndoItem::~KUndoItem()
33 KUndoItem::InitCheck()
40 KUndoItem::Merge(const char* text
, int32 length
)
42 RedoText
= (char*)realloc(RedoText
, Length
+ length
);
43 memcpy(&RedoText
[Length
], text
, length
);
48 KUndoBuffer::KUndoBuffer():BList(1024)
56 KUndoBuffer::~KUndoBuffer()
63 KUndoBuffer::AddItem(KUndoItem
* item
, int32 index
)
65 for (int32 i
= CountItems() - 1; i
>= index
; i
--)
73 KUndoBuffer::AddItem(KUndoItem
* item
)
75 return BList::AddItem(item
);
80 KUndoBuffer::MakeEmpty(void)
82 for (int32 i
= CountItems() - 1; i
>= 0; i
--)
88 KUndoBuffer::RemoveItem(int32 index
)
90 if (fIndex
>= CountItems())
92 delete this->ItemAt(index
);
93 return (KUndoItem
*)BList::RemoveItem(index
);
98 KUndoBuffer::ItemAt(int32 index
) const
100 return (KUndoItem
*)BList::ItemAt(index
);
119 KUndoBuffer::NewUndo(const char* text
, int32 length
, int32 offset
,
120 undo_type history
, int32 cursor_pos
)
122 KUndoItem
* NewUndoItem
= new KUndoItem(text
, length
, offset
, history
,
125 status_t status
= NewUndoItem
->InitCheck();
126 if (status
!= B_OK
) {
130 AddItem(NewUndoItem
, fIndex
);
137 KUndoBuffer::AddUndo(const char* text
, int32 length
, int32 offset
,
138 undo_type history
, int32 cursor_pos
)
143 status_t status
= B_OK
;
145 if (fNewItem
|| fIndex
< CountItems() || CountItems() == 0) {
146 status
= NewUndo(text
, length
, offset
, history
, cursor_pos
);
149 KUndoItem
* CurrentUndoItem
;
150 CurrentUndoItem
= ItemAt(fIndex
- 1);
151 if (CurrentUndoItem
!= NULL
) {
152 int32 c_length
= CurrentUndoItem
->Length
;
153 int32 c_offset
= CurrentUndoItem
->Offset
;
154 undo_type c_history
= CurrentUndoItem
->History
;
155 if (c_history
== history
) {
159 if ((c_offset
+ c_length
) == offset
)
160 CurrentUndoItem
->Merge(text
, length
);
162 status
= NewUndo(text
, length
, offset
, history
,
167 status
= NewUndo(text
, length
, offset
, history
,
172 status
= NewUndo(text
, length
, offset
, history
, cursor_pos
);
181 KUndoBuffer::MakeNewUndoItem()
183 if (fIndex
>= CountItems()) {
192 KUndoBuffer::Undo(char** text
, int32
* length
, int32
* offset
,
193 undo_type
* history
, int32
* cursor_pos
)
196 status_t status
= B_ERROR
;
199 undoItem
= ItemAt(fIndex
- 1);
200 if (undoItem
!= NULL
) {
201 *text
= undoItem
->RedoText
;
202 *length
= undoItem
->Length
;
203 *offset
= undoItem
->Offset
;
204 *history
= undoItem
->History
;
205 *cursor_pos
= undoItem
->CursorPos
+ undoItem
->Length
;
215 KUndoBuffer::Redo(char** text
, int32
* length
, int32
* offset
,
216 undo_type
* history
, int32
* cursor_pos
, bool* replaced
)
219 status_t status
= B_ERROR
;
221 if (fIndex
< CountItems()) {
222 undoItem
= ItemAt(fIndex
);
223 if (undoItem
!= NULL
) {
224 *text
= undoItem
->RedoText
;
225 *length
= undoItem
->Length
;
226 *offset
= undoItem
->Offset
;
227 *history
= undoItem
->History
;
228 *cursor_pos
= undoItem
->CursorPos
;
229 if (fIndex
+ 1 < CountItems())
230 *replaced
= ItemAt(fIndex
+ 1)->History
== K_REPLACED
;
242 KUndoBuffer::PrintToStream()
244 for (int32 i
= 0; i
< CountItems(); i
++) {
245 KUndoItem
* item
= ItemAt(i
);
246 printf("%3.3d ", (int)i
);
247 switch (item
->History
) {
258 printf("Offset = %d ", (int)item
->Offset
);
259 printf("Length = %d ", (int)item
->Length
);
260 printf("CursorPos = %d ", (int)item
->CursorPos
);
261 printf("RedoText = '");
262 for (int32 j
= 0; j
< item
->Length
; j
++) {
263 uchar c
= (uchar
)item
->RedoText
[j
];