Example #1
0
int32_t
PTextViewCanEndLine(void *pobject, void *in, void *out, void *extraData)
{
	if (!pobject || !in || !out)
		return B_ERROR;

	PView *parent = static_cast<PView*>(pobject);
	if (!parent)
		return B_BAD_TYPE;
	
	BTextView *backend = (BTextView*)parent->GetView();


	PArgs *inArgs = static_cast<PArgs*>(in);

	PArgs *outArgs = static_cast<PArgs*>(out);

	int32 offset;
	if (inArgs->FindInt32("offset", &offset) != B_OK)
		return B_ERROR;

	if (backend->Window())
		backend->Window()->Lock();

	bool outValue1;

	outValue1 = backend->ByteAt(offset);

	if (backend->Window())
		backend->Window()->Unlock();

	outArgs->MakeEmpty();

	return B_OK;
}
Example #2
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);
}