xref: /haiku/src/kits/interface/textview_support/TextGapBuffer.h (revision bab64f65bb775dc23060e276f1f1c4498ab7af6c)
1b31b14e0SStefano Ceccherini /*
2b31b14e0SStefano Ceccherini  * Copyright 2001-2006, Haiku, Inc. All Rights Reserved.
3b31b14e0SStefano Ceccherini  * Distributed under the terms of the MIT License.
4b31b14e0SStefano Ceccherini  *
5b31b14e0SStefano Ceccherini  * Authors:
6b31b14e0SStefano Ceccherini  *		Marc Flerackers (mflerackers@androme.be)
7b31b14e0SStefano Ceccherini  */
8b31b14e0SStefano Ceccherini #ifndef __TEXTGAPBUFFER_H
9b31b14e0SStefano Ceccherini #define __TEXTGAPBUFFER_H
10b31b14e0SStefano Ceccherini 
11c3423856SOliver Tappe 
12b31b14e0SStefano Ceccherini #include <SupportDefs.h>
13a682d981SStephan Aßmus #include <TextView.h>
14b31b14e0SStefano Ceccherini 
15b31b14e0SStefano Ceccherini 
16b31b14e0SStefano Ceccherini class BFile;
17b31b14e0SStefano Ceccherini 
18c3423856SOliver Tappe 
1990b7764dSRene Gollent namespace BPrivate {
20a682d981SStephan Aßmus 
21c3423856SOliver Tappe 
2290b7764dSRene Gollent class TextGapBuffer {
23b31b14e0SStefano Ceccherini public:
24a682d981SStephan Aßmus 								TextGapBuffer();
25c3423856SOliver Tappe 								~TextGapBuffer();
26b31b14e0SStefano Ceccherini 
27a682d981SStephan Aßmus 			void				InsertText(const char* inText, int32 inNumItems,
28a682d981SStephan Aßmus 									int32 inAtIndex);
29*3c857341SAugustin Cavalier 			bool				InsertText(BFile* file, int32 fileOffset,
30c3423856SOliver Tappe 									int32 amount, int32 atIndex);
31b31b14e0SStefano Ceccherini 			void				RemoveRange(int32 start, int32 end);
32b31b14e0SStefano Ceccherini 
33c3423856SOliver Tappe 			bool				FindChar(char inChar, int32 fromIndex,
34c3423856SOliver Tappe 									int32* ioDelta);
35b31b14e0SStefano Ceccherini 
36b31b14e0SStefano Ceccherini 			const char*			Text();
37e94ad1e2SStefano Ceccherini 			const char*			RealText();
38b31b14e0SStefano Ceccherini 			int32				Length() const;
39b31b14e0SStefano Ceccherini 
40de20f0faSStefano Ceccherini 			const char*			GetString(int32 fromOffset, int32* numBytes);
41c3423856SOliver Tappe 			void				GetString(int32 offset, int32 length,
42c3423856SOliver Tappe 									char* buffer);
43b31b14e0SStefano Ceccherini 
44b31b14e0SStefano Ceccherini 			char				RealCharAt(int32 offset) const;
45b31b14e0SStefano Ceccherini 
46b31b14e0SStefano Ceccherini 			bool				PasswordMode() const;
47b31b14e0SStefano Ceccherini 			void				SetPasswordMode(bool);
48b31b14e0SStefano Ceccherini 
497e31a05cSOliver Tappe private:
507e31a05cSOliver Tappe 			void				_MoveGapTo(int32 toIndex);
517e31a05cSOliver Tappe 			void				_EnlargeGapTo(int32 inCount);
527e31a05cSOliver Tappe 			void				_ShrinkGapTo(int32 inCount);
537e31a05cSOliver Tappe 
54b31b14e0SStefano Ceccherini 			int32				fItemCount;			// logical count
55b31b14e0SStefano Ceccherini 			char*				fBuffer;			// allocated memory
56b31b14e0SStefano Ceccherini 			int32				fBufferCount;		// physical count
57b31b14e0SStefano Ceccherini 			int32				fGapIndex;			// gap position
58b31b14e0SStefano Ceccherini 			int32				fGapCount;			// gap count
59b31b14e0SStefano Ceccherini 			char*				fScratchBuffer;		// for GetString
60b31b14e0SStefano Ceccherini 			int32				fScratchSize;		// scratch size
61b31b14e0SStefano Ceccherini 			bool				fPasswordMode;
62b31b14e0SStefano Ceccherini };
63b31b14e0SStefano Ceccherini 
64b31b14e0SStefano Ceccherini 
65b31b14e0SStefano Ceccherini inline int32
Length()6690b7764dSRene Gollent TextGapBuffer::Length() const
67b31b14e0SStefano Ceccherini {
68b31b14e0SStefano Ceccherini 	return fItemCount;
69b31b14e0SStefano Ceccherini }
70b31b14e0SStefano Ceccherini 
71b31b14e0SStefano Ceccherini 
72b31b14e0SStefano Ceccherini inline char
RealCharAt(int32 index)735c4e7641SMichael Lotz TextGapBuffer::RealCharAt(int32 index) const
74b31b14e0SStefano Ceccherini {
755c4e7641SMichael Lotz 	if (index < 0 || index >= fItemCount) {
765c4e7641SMichael Lotz 		if (index != fItemCount)
775c4e7641SMichael Lotz 			debugger("RealCharAt: invalid index supplied");
785c4e7641SMichael Lotz 		return 0;
795c4e7641SMichael Lotz 	}
805c4e7641SMichael Lotz 
815c4e7641SMichael Lotz 	return index < fGapIndex ? fBuffer[index] : fBuffer[index + fGapCount];
82b31b14e0SStefano Ceccherini }
83b31b14e0SStefano Ceccherini 
84c3423856SOliver Tappe 
8590b7764dSRene Gollent } // namespace BPrivate
8690b7764dSRene Gollent 
87c3423856SOliver Tappe 
88b31b14e0SStefano Ceccherini #endif //__TEXTGAPBUFFER_H
89