1/*
2 * Copyright 2013, Stephan Aßmus <superstippi@gmx.de>.
3 * All rights reserved. Distributed under the terms of the MIT License.
4 */
5
6#include "TextView.h"
7
8
9TextView::TextView(const char* name)
10	:
11	BView(name, B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE | B_FRAME_EVENTS)
12{
13	BFont font;
14	GetFont(&font);
15
16	::ParagraphStyle style;
17	style.SetLineSpacing(ceilf(font.Size() * 0.2));
18
19	SetParagraphStyle(style);
20}
21
22
23TextView::~TextView()
24{
25}
26
27
28void
29TextView::Draw(BRect updateRect)
30{
31	FillRect(updateRect, B_SOLID_LOW);
32
33	fTextLayout.SetWidth(Bounds().Width());
34	fTextLayout.Draw(this, B_ORIGIN);
35}
36
37
38void
39TextView::AttachedToWindow()
40{
41	AdoptParentColors();
42}
43
44
45void
46TextView::FrameResized(float width, float height)
47{
48	fTextLayout.SetWidth(width);
49}
50
51
52BSize
53TextView::MinSize()
54{
55	return BSize(50.0f, 0.0f);
56}
57
58
59BSize
60TextView::MaxSize()
61{
62	return BSize(B_SIZE_UNLIMITED, B_SIZE_UNLIMITED);
63}
64
65
66BSize
67TextView::PreferredSize()
68{
69	return BSize(B_SIZE_UNLIMITED, B_SIZE_UNLIMITED);
70}
71
72
73bool
74TextView::HasHeightForWidth()
75{
76	return true;
77}
78
79
80void
81TextView::GetHeightForWidth(float width, float* min, float* max,
82	float* preferred)
83{
84	ParagraphLayout layout(fTextLayout);
85	layout.SetWidth(width);
86
87	float height = layout.Height() + 1;
88
89	if (min != NULL)
90		*min = height;
91	if (max != NULL)
92		*max = height;
93	if (preferred != NULL)
94		*preferred = height;
95}
96
97
98void
99TextView::SetText(const BString& text)
100{
101	fText.Clear();
102
103	CharacterStyle regularStyle;
104	fText.Append(TextSpan(text, regularStyle));
105
106	fTextLayout.SetParagraph(fText);
107
108	InvalidateLayout();
109	Invalidate();
110}
111
112
113void
114TextView::SetParagraphStyle(const ::ParagraphStyle& style)
115{
116	fText.SetStyle(style);
117	fTextLayout.SetParagraph(fText);
118
119	InvalidateLayout();
120	Invalidate();
121}
122
123