btrfs: Attempt to fix GCC2 build.
[haiku.git] / src / kits / interface / textview_support / LineBuffer.cpp
blob35c0c0772c5957e5c1eb2ae0b99ac23196518fbf
1 /*
2 * Copyright 2001-2006, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Marc Flerackers (mflerackers@androme.be)
7 */
10 #include "LineBuffer.h"
13 BTextView::LineBuffer::LineBuffer()
14 : _BTextViewSupportBuffer_<STELine>(20, 2)
19 BTextView::LineBuffer::~LineBuffer()
24 void
25 BTextView::LineBuffer::InsertLine(STELine* inLine, int32 index)
27 InsertItemsAt(1, index, inLine);
31 void
32 BTextView::LineBuffer::RemoveLines(int32 index, int32 count)
34 RemoveItemsAt(count, index);
38 void
39 BTextView::LineBuffer::RemoveLineRange(int32 fromOffset, int32 toOffset)
41 int32 fromLine = OffsetToLine(fromOffset);
42 int32 toLine = OffsetToLine(toOffset);
44 int32 count = toLine - fromLine;
45 if (count > 0)
46 RemoveLines(fromLine + 1, count);
48 BumpOffset(fromOffset - toOffset, fromLine + 1);
52 int32
53 BTextView::LineBuffer::OffsetToLine(int32 offset) const
55 int32 minIndex = 0;
56 int32 maxIndex = fItemCount - 1;
57 int32 index = 0;
59 while (minIndex < maxIndex) {
60 index = (minIndex + maxIndex) >> 1;
61 if (offset >= fBuffer[index].offset) {
62 if (offset < fBuffer[index + 1].offset)
63 break;
64 else
65 minIndex = index + 1;
66 } else
67 maxIndex = index;
70 return index;
74 int32
75 BTextView::LineBuffer::PixelToLine(float pixel) const
77 int32 minIndex = 0;
78 int32 maxIndex = fItemCount - 1;
79 int32 index = 0;
81 while (minIndex < maxIndex) {
82 index = (minIndex + maxIndex) >> 1;
83 if (pixel >= fBuffer[index].origin) {
84 if (pixel < fBuffer[index + 1].origin)
85 break;
86 else
87 minIndex = index + 1;
88 } else
89 maxIndex = index;
92 return index;
96 void
97 BTextView::LineBuffer::BumpOrigin(float delta, int32 index)
99 for (long i = index; i < fItemCount; i++)
100 fBuffer[i].origin += delta;
104 void
105 BTextView::LineBuffer::BumpOffset(int32 delta, int32 index)
107 for (long i = index; i < fItemCount; i++)
108 fBuffer[i].offset += delta;
112 float
113 BTextView::LineBuffer::MaxWidth() const
115 if (fItemCount == 0)
116 return 0;
118 float maxWidth = 0;
119 STELine* line = &fBuffer[0];
120 for (int32 i = 0; i < fItemCount; i++) {
121 if (maxWidth < line->width)
122 maxWidth = line->width;
123 line++;
125 return maxWidth;