xref: /haiku/src/kits/interface/textview_support/LineBuffer.cpp (revision 1acbe440b8dd798953bec31d18ee589aa3f71b73)
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 
9 #include "LineBuffer.h"
10 
11 
12 _BLineBuffer_::_BLineBuffer_()
13 	:	_BTextViewSupportBuffer_<STELine>(20, 2)
14 {
15 }
16 
17 
18 _BLineBuffer_::~_BLineBuffer_()
19 {
20 }
21 
22 
23 void
24 _BLineBuffer_::InsertLine(STELine *inLine, int32 index)
25 {
26 	InsertItemsAt(1, index, inLine);
27 }
28 
29 
30 void
31 _BLineBuffer_::RemoveLines(int32 index, int32 count)
32 {
33 	RemoveItemsAt(count, index);
34 }
35 
36 
37 void
38 _BLineBuffer_::RemoveLineRange(int32 fromOffset, int32 toOffset)
39 {
40 	int32 fromLine = OffsetToLine(fromOffset);
41 	int32 toLine = OffsetToLine(toOffset);
42 
43 	int32 count = toLine - fromLine;
44 	if (count > 0)
45 		RemoveLines(fromLine + 1, count);
46 
47 	BumpOffset(fromOffset - toOffset, fromLine + 1);
48 }
49 
50 
51 int32
52 _BLineBuffer_::OffsetToLine(int32 offset) const
53 {
54 	int32 minIndex = 0;
55 	int32 maxIndex = fItemCount - 1;
56 	int32 index = 0;
57 
58 	while (minIndex < maxIndex) {
59 		index = (minIndex + maxIndex) >> 1;
60 		if (offset >= fBuffer[index].offset) {
61 			if (offset < fBuffer[index + 1].offset)
62 				break;
63 			else
64 				minIndex = index + 1;
65 		} else
66 			maxIndex = index;
67 	}
68 
69 	return index;
70 }
71 
72 
73 int32
74 _BLineBuffer_::PixelToLine(float pixel) const
75 {
76 	int32 minIndex = 0;
77 	int32 maxIndex = fItemCount - 1;
78 	int32 index = 0;
79 
80 	while (minIndex < maxIndex) {
81 		index = (minIndex + maxIndex) >> 1;
82 		if (pixel >= fBuffer[index].origin) {
83 			if (pixel < fBuffer[index + 1].origin)
84 				break;
85 			else
86 				minIndex = index + 1;
87 		} else
88 			maxIndex = index;
89 	}
90 
91 	return index;
92 }
93 
94 
95 void
96 _BLineBuffer_::BumpOrigin(float delta, long index)
97 {
98 	for (long i = index; i < fItemCount; i++)
99 		fBuffer[i].origin += delta;
100 }
101 
102 
103 void
104 _BLineBuffer_::BumpOffset(int32 delta, int32 index)
105 {
106 	for (long i = index; i < fItemCount; i++)
107 		fBuffer[i].offset += delta;
108 }
109