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