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 10#include "LineBuffer.h" 11 12 13BTextView::LineBuffer::LineBuffer() 14 : _BTextViewSupportBuffer_<STELine>(20, 2) 15{ 16} 17 18 19BTextView::LineBuffer::~LineBuffer() 20{ 21} 22 23 24void 25BTextView::LineBuffer::InsertLine(STELine* inLine, int32 index) 26{ 27 InsertItemsAt(1, index, inLine); 28} 29 30 31void 32BTextView::LineBuffer::RemoveLines(int32 index, int32 count) 33{ 34 RemoveItemsAt(count, index); 35} 36 37 38void 39BTextView::LineBuffer::RemoveLineRange(int32 fromOffset, int32 toOffset) 40{ 41 int32 fromLine = OffsetToLine(fromOffset); 42 int32 toLine = OffsetToLine(toOffset); 43 44 int32 count = toLine - fromLine; 45 if (count > 0) 46 RemoveLines(fromLine + 1, count); 47 48 BumpOffset(fromOffset - toOffset, fromLine + 1); 49} 50 51 52int32 53BTextView::LineBuffer::OffsetToLine(int32 offset) const 54{ 55 int32 minIndex = 0; 56 int32 maxIndex = fItemCount - 1; 57 int32 index = 0; 58 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; 68 } 69 70 return index; 71} 72 73 74int32 75BTextView::LineBuffer::PixelToLine(float pixel) const 76{ 77 int32 minIndex = 0; 78 int32 maxIndex = fItemCount - 1; 79 int32 index = 0; 80 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; 90 } 91 92 return index; 93} 94 95 96void 97BTextView::LineBuffer::BumpOrigin(float delta, int32 index) 98{ 99 for (long i = index; i < fItemCount; i++) 100 fBuffer[i].origin += delta; 101} 102 103 104void 105BTextView::LineBuffer::BumpOffset(int32 delta, int32 index) 106{ 107 for (long i = index; i < fItemCount; i++) 108 fBuffer[i].offset += delta; 109} 110 111 112float 113BTextView::LineBuffer::MaxWidth() const 114{ 115 if (fItemCount == 0) 116 return 0; 117 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++; 124 } 125 return maxWidth; 126} 127