xref: /haiku/src/kits/interface/textview_support/TextGapBuffer.h (revision bab64f65bb775dc23060e276f1f1c4498ab7af6c)
1 /*
2  * Copyright 2001-2006, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Marc Flerackers (mflerackers@androme.be)
7  */
8 #ifndef __TEXTGAPBUFFER_H
9 #define __TEXTGAPBUFFER_H
10 
11 
12 #include <SupportDefs.h>
13 #include <TextView.h>
14 
15 
16 class BFile;
17 
18 
19 namespace BPrivate {
20 
21 
22 class TextGapBuffer {
23 public:
24 								TextGapBuffer();
25 								~TextGapBuffer();
26 
27 			void				InsertText(const char* inText, int32 inNumItems,
28 									int32 inAtIndex);
29 			bool				InsertText(BFile* file, int32 fileOffset,
30 									int32 amount, int32 atIndex);
31 			void				RemoveRange(int32 start, int32 end);
32 
33 			bool				FindChar(char inChar, int32 fromIndex,
34 									int32* ioDelta);
35 
36 			const char*			Text();
37 			const char*			RealText();
38 			int32				Length() const;
39 
40 			const char*			GetString(int32 fromOffset, int32* numBytes);
41 			void				GetString(int32 offset, int32 length,
42 									char* buffer);
43 
44 			char				RealCharAt(int32 offset) const;
45 
46 			bool				PasswordMode() const;
47 			void				SetPasswordMode(bool);
48 
49 private:
50 			void				_MoveGapTo(int32 toIndex);
51 			void				_EnlargeGapTo(int32 inCount);
52 			void				_ShrinkGapTo(int32 inCount);
53 
54 			int32				fItemCount;			// logical count
55 			char*				fBuffer;			// allocated memory
56 			int32				fBufferCount;		// physical count
57 			int32				fGapIndex;			// gap position
58 			int32				fGapCount;			// gap count
59 			char*				fScratchBuffer;		// for GetString
60 			int32				fScratchSize;		// scratch size
61 			bool				fPasswordMode;
62 };
63 
64 
65 inline int32
Length()66 TextGapBuffer::Length() const
67 {
68 	return fItemCount;
69 }
70 
71 
72 inline char
RealCharAt(int32 index)73 TextGapBuffer::RealCharAt(int32 index) const
74 {
75 	if (index < 0 || index >= fItemCount) {
76 		if (index != fItemCount)
77 			debugger("RealCharAt: invalid index supplied");
78 		return 0;
79 	}
80 
81 	return index < fGapIndex ? fBuffer[index] : fBuffer[index + fGapCount];
82 }
83 
84 
85 } // namespace BPrivate
86 
87 
88 #endif //__TEXTGAPBUFFER_H
89