Example #1
0
/***********************************************************
 * HardWrap
 ***********************************************************/
void
HWrapTextView::GetHardWrapedText(BString &out)
{
	MakeEditable(false);
	
	BTextView *offview = new BTextView(Bounds(),NULL,TextRect(),B_FOLLOW_ALL);
	offview->SetText(Text());
	
	BFont font;
	uint32 propa;
	GetFontAndColor(&font,&propa);
	out = "";
	offview->SetFontAndColor(&font,B_FONT_ALL);
	BString line;
	int32 length = offview->TextLength();
	float view_width = offview->TextRect().Width();
	char c=0;
	bool inserted;
	
	for(int32 i=0;i < length;i++)
	{
		c = offview->ByteAt(i);
		if(c == '\n')
		{
			line = "";
			continue;
		}
		line += c;
		if(font.StringWidth(line.String())>=view_width)
		{
			// Back 1 charactor.
			i--;
			line.Truncate(line.Length()-1);
			// Insert line break.
			inserted = false;
			int32 len = line.Length();
			for(int32 k = 0;k<len;k++)
			{
				if(offview->CanEndLine(i-k))
				{
					offview->Insert(i-k,"\n",1);
					inserted=true;
					i = i-k;
					break;
				}
			}
			// If could not find proper position, add line break to end.
			if(!inserted)
				offview->Insert(i,"\n",1);
			line = "";
		}
	}
	out = offview->Text();
	
	delete offview;
	MakeEditable(true);
}