1c284bb0fSMatt Madia/*
2c284bb0fSMatt Madia * Copyright (c) 1999-2000, Eric Moon.
3c284bb0fSMatt Madia * All rights reserved.
4c284bb0fSMatt Madia *
5c284bb0fSMatt Madia * Redistribution and use in source and binary forms, with or without
6c284bb0fSMatt Madia * modification, are permitted provided that the following conditions
7c284bb0fSMatt Madia * are met:
8c284bb0fSMatt Madia *
9c284bb0fSMatt Madia * 1. Redistributions of source code must retain the above copyright
10c284bb0fSMatt Madia *    notice, this list of conditions, and the following disclaimer.
11c284bb0fSMatt Madia *
12c284bb0fSMatt Madia * 2. Redistributions in binary form must reproduce the above copyright
13c284bb0fSMatt Madia *    notice, this list of conditions, and the following disclaimer in the
14c284bb0fSMatt Madia *    documentation and/or other materials provided with the distribution.
15c284bb0fSMatt Madia *
16c284bb0fSMatt Madia * 3. The name of the author may not be used to endorse or promote products
17c284bb0fSMatt Madia *    derived from this software without specific prior written permission.
18c284bb0fSMatt Madia *
19c284bb0fSMatt Madia * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
20c284bb0fSMatt Madia * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21c284bb0fSMatt Madia * OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22c284bb0fSMatt Madia * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23c284bb0fSMatt Madia * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24c284bb0fSMatt Madia * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25c284bb0fSMatt Madia * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26c284bb0fSMatt Madia * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
27c284bb0fSMatt Madia * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28c284bb0fSMatt Madia * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29c284bb0fSMatt Madia */
30c284bb0fSMatt Madia
31c284bb0fSMatt Madia
32a0795c6fSMarcus Overhagen// TipView.cpp
33a0795c6fSMarcus Overhagen
34a0795c6fSMarcus Overhagen#include "TipView.h"
35a0795c6fSMarcus Overhagen
36a0795c6fSMarcus Overhagen#include <Debug.h>
37a0795c6fSMarcus Overhagen#include <Window.h>
38a0795c6fSMarcus Overhagen#include <cmath>
39a0795c6fSMarcus Overhagen#include <cstring>
40a0795c6fSMarcus Overhagen
4123e67806SIthamar R. Ademausing namespace std;
4223e67806SIthamar R. Adema
43a0795c6fSMarcus Overhagen__USE_CORTEX_NAMESPACE
44a0795c6fSMarcus Overhagen
45a0795c6fSMarcus Overhagen// -------------------------------------------------------- //
46a0795c6fSMarcus Overhagen// constants
47a0795c6fSMarcus Overhagen// -------------------------------------------------------- //
48a0795c6fSMarcus Overhagen
49a0795c6fSMarcus Overhagenconst float TipView::s_xPad = 5.0;
50a0795c6fSMarcus Overhagenconst float TipView::s_yPad = 2.0;
51a0795c6fSMarcus Overhagen
52a0795c6fSMarcus Overhagen// -------------------------------------------------------- //
53a0795c6fSMarcus Overhagen// *** dtor/ctors
54a0795c6fSMarcus Overhagen// -------------------------------------------------------- //
55a0795c6fSMarcus Overhagen
56a0795c6fSMarcus OverhagenTipView::~TipView() {}
57a0795c6fSMarcus OverhagenTipView::TipView() :
58a0795c6fSMarcus Overhagen	BView(
59a0795c6fSMarcus Overhagen		BRect(0,0,0,0),
60a0795c6fSMarcus Overhagen		"TipView",
61a0795c6fSMarcus Overhagen		B_FOLLOW_NONE,
62a0795c6fSMarcus Overhagen		B_WILL_DRAW|B_FRAME_EVENTS),
63a0795c6fSMarcus Overhagen	m_font(be_plain_font) {
64a0795c6fSMarcus Overhagen
65a0795c6fSMarcus Overhagen	_initColors();
66a0795c6fSMarcus Overhagen	_initFont();
67a0795c6fSMarcus Overhagen}
68a0795c6fSMarcus Overhagen
69a0795c6fSMarcus Overhagen// -------------------------------------------------------- //
70a0795c6fSMarcus Overhagen// *** operations
71a0795c6fSMarcus Overhagen// -------------------------------------------------------- //
72a0795c6fSMarcus Overhagen
73a0795c6fSMarcus Overhagenvoid TipView::setText(
74a0795c6fSMarcus Overhagen	const char*							text) {
75a0795c6fSMarcus Overhagen
76a0795c6fSMarcus Overhagen	_setText(text);
77a0795c6fSMarcus Overhagen}
78a0795c6fSMarcus Overhagen
79a0795c6fSMarcus Overhagen// -------------------------------------------------------- //
80a0795c6fSMarcus Overhagen// *** BView
81a0795c6fSMarcus Overhagen// -------------------------------------------------------- //
82a0795c6fSMarcus Overhagen
83a0795c6fSMarcus Overhagen// Adapted from c.lenz' ToolTipView::Draw()
84a0795c6fSMarcus Overhagenvoid TipView::Draw(
85a0795c6fSMarcus Overhagen	BRect										updateRect) {
86a0795c6fSMarcus Overhagen
87a0795c6fSMarcus Overhagen	BRect r = Bounds();
88a0795c6fSMarcus Overhagen
89a0795c6fSMarcus Overhagen	// Draw border and fill
90a0795c6fSMarcus Overhagen	SetDrawingMode(B_OP_ALPHA);
91a0795c6fSMarcus Overhagen	SetHighColor(m_borderLoColor);
92a0795c6fSMarcus Overhagen	StrokeLine(r.LeftBottom(), r.RightBottom());
93a0795c6fSMarcus Overhagen	StrokeLine(r.RightTop(), r.RightBottom());
94a0795c6fSMarcus Overhagen	SetHighColor(m_borderHiColor);
95a0795c6fSMarcus Overhagen	StrokeLine(r.LeftTop(), r.RightTop());
96a0795c6fSMarcus Overhagen	StrokeLine(r.LeftTop(), r.LeftBottom());
97a0795c6fSMarcus Overhagen	SetHighColor(m_viewColor);
98a0795c6fSMarcus Overhagen	SetDrawingMode(B_OP_ALPHA);
99a0795c6fSMarcus Overhagen	r.InsetBy(1.0, 1.0);
100a0795c6fSMarcus Overhagen	FillRect(r);
101a0795c6fSMarcus Overhagen
102a0795c6fSMarcus Overhagen	// Draw text
103a0795c6fSMarcus Overhagen	SetDrawingMode(B_OP_OVER);
104a0795c6fSMarcus Overhagen	SetHighColor(m_textColor);
105a0795c6fSMarcus Overhagen
106a0795c6fSMarcus Overhagen	BPoint p = m_offset;
107a0795c6fSMarcus Overhagen	for(uint32 n = 0; n < m_lineSet.size(); ++n) {
108a0795c6fSMarcus Overhagen
109a0795c6fSMarcus Overhagen		uint32 from = m_lineSet[n];
110a0795c6fSMarcus Overhagen		uint32 to = (n < m_lineSet.size()-1) ? m_lineSet[n+1]-1 :
111a0795c6fSMarcus Overhagen			m_text.Length();
112a0795c6fSMarcus Overhagen
113a0795c6fSMarcus Overhagen		if(to > from)
114a0795c6fSMarcus Overhagen			DrawString(
115a0795c6fSMarcus Overhagen				m_text.String() + from,
116a0795c6fSMarcus Overhagen				to - from,
117a0795c6fSMarcus Overhagen				p);
118a0795c6fSMarcus Overhagen
119a0795c6fSMarcus Overhagen		p.y += (m_fontHeight.ascent + m_fontHeight.descent + m_fontHeight.leading);
120a0795c6fSMarcus Overhagen	}
121a0795c6fSMarcus Overhagen}
122a0795c6fSMarcus Overhagen
123a0795c6fSMarcus Overhagenvoid TipView::FrameResized(
124a0795c6fSMarcus Overhagen	float										width,
125a0795c6fSMarcus Overhagen	float										height) {
126a0795c6fSMarcus Overhagen
127a0795c6fSMarcus Overhagen	_inherited::FrameResized(width, height);
128a0795c6fSMarcus Overhagen	_updateLayout(width, height);
129a0795c6fSMarcus Overhagen	Invalidate();
130a0795c6fSMarcus Overhagen}
131a0795c6fSMarcus Overhagen
132a0795c6fSMarcus Overhagenvoid TipView::GetPreferredSize(
133a0795c6fSMarcus Overhagen	float*									outWidth,
134a0795c6fSMarcus Overhagen	float*									outHeight) {
135a0795c6fSMarcus Overhagen
136a0795c6fSMarcus Overhagen//	*outWidth = ceil(m_font.StringWidth(m_text.String()) + s_xPad*2);
137a0795c6fSMarcus Overhagen	*outWidth = ceil(_maxTextWidth() + s_xPad*2);
138a0795c6fSMarcus Overhagen	*outHeight = ceil(_textHeight() + s_yPad*2);
139a0795c6fSMarcus Overhagen}
140a0795c6fSMarcus Overhagen
141a0795c6fSMarcus Overhagen// -------------------------------------------------------- //
142a0795c6fSMarcus Overhagen// implementation
143a0795c6fSMarcus Overhagen// -------------------------------------------------------- //
144a0795c6fSMarcus Overhagen
145a0795c6fSMarcus Overhagenvoid _make_color(
146a0795c6fSMarcus Overhagen	rgb_color*							outColor,
147a0795c6fSMarcus Overhagen	uint8										red,
148a0795c6fSMarcus Overhagen	uint8										green,
149a0795c6fSMarcus Overhagen	uint8										blue,
150a0795c6fSMarcus Overhagen	uint8										alpha=255);
151a0795c6fSMarcus Overhagenvoid _make_color(
152a0795c6fSMarcus Overhagen	rgb_color*							outColor,
153a0795c6fSMarcus Overhagen	uint8										red,
154a0795c6fSMarcus Overhagen	uint8										green,
155a0795c6fSMarcus Overhagen	uint8										blue,
156a0795c6fSMarcus Overhagen	uint8										alpha) {
157a0795c6fSMarcus Overhagen	outColor->red = red;
158a0795c6fSMarcus Overhagen	outColor->green = green;
159a0795c6fSMarcus Overhagen	outColor->blue = blue;
160a0795c6fSMarcus Overhagen	outColor->alpha = alpha;
161a0795c6fSMarcus Overhagen}
162a0795c6fSMarcus Overhagen
163a0795c6fSMarcus Overhagenvoid TipView::_initColors() {
164a0795c6fSMarcus Overhagen
165a0795c6fSMarcus Overhagen	SetViewColor(B_TRANSPARENT_COLOR);
166a0795c6fSMarcus Overhagen
167a0795c6fSMarcus Overhagen	_make_color(&m_textColor, 0, 0, 0, 255);
168a0795c6fSMarcus Overhagen	_make_color(&m_borderHiColor, 0, 0, 0, 255 /*196*/);
169a0795c6fSMarcus Overhagen	_make_color(&m_borderLoColor, 0, 0, 0, 255 /*196*/);
170a0795c6fSMarcus Overhagen	_make_color(&m_viewColor, 255, 255, 240, 255 /*216*/);
171a0795c6fSMarcus Overhagen}
172a0795c6fSMarcus Overhagen
173a0795c6fSMarcus Overhagenvoid TipView::_initFont() {
174a0795c6fSMarcus Overhagen
175a0795c6fSMarcus Overhagen	SetFont(&m_font);
176a0795c6fSMarcus Overhagen	m_font.GetHeight(&m_fontHeight);
177a0795c6fSMarcus Overhagen}
178a0795c6fSMarcus Overhagen
179a0795c6fSMarcus Overhagenvoid TipView::_updateLayout(
180a0795c6fSMarcus Overhagen	float										width,
181a0795c6fSMarcus Overhagen	float										height) {
182a0795c6fSMarcus Overhagen
183a0795c6fSMarcus Overhagen	// center text
184a0795c6fSMarcus Overhagen	m_offset.x = (width - _maxTextWidth()) / 2;
185a0795c6fSMarcus Overhagen
186a0795c6fSMarcus Overhagen	m_offset.y = (height - _textHeight()) / 2;
187a0795c6fSMarcus Overhagen	m_offset.y += m_fontHeight.ascent;
188a0795c6fSMarcus Overhagen}
189a0795c6fSMarcus Overhagen
190a0795c6fSMarcus Overhagenvoid TipView::_setText(
191a0795c6fSMarcus Overhagen	const char*							text) {
192a0795c6fSMarcus Overhagen
193a0795c6fSMarcus Overhagen	ASSERT(text);
194a0795c6fSMarcus Overhagen
195a0795c6fSMarcus Overhagen	m_lineSet.clear();
196a0795c6fSMarcus Overhagen	m_text = text;
197a0795c6fSMarcus Overhagen
198a0795c6fSMarcus Overhagen	// skip leading newlines
199a0795c6fSMarcus Overhagen	int32 n = 0;
200a0795c6fSMarcus Overhagen	while(n < m_text.Length() && m_text[n] == '\n')
201a0795c6fSMarcus Overhagen		++n;
202a0795c6fSMarcus Overhagen
203a0795c6fSMarcus Overhagen	// mark first line
204a0795c6fSMarcus Overhagen	m_lineSet.push_back(n);
205a0795c6fSMarcus Overhagen
206a0795c6fSMarcus Overhagen	// break following lines
207a0795c6fSMarcus Overhagen	while(n < m_text.Length()) {
208a0795c6fSMarcus Overhagen		int32 nextBreak = m_text.FindFirst('\n', n);
209a0795c6fSMarcus Overhagen		// no more line breaks?
210a0795c6fSMarcus Overhagen		if(nextBreak < n)
211a0795c6fSMarcus Overhagen			break;
212a0795c6fSMarcus Overhagen		n = nextBreak + 1;
213a0795c6fSMarcus Overhagen		m_lineSet.push_back(n);
214a0795c6fSMarcus Overhagen	}
215a0795c6fSMarcus Overhagen}
216a0795c6fSMarcus Overhagen
217a0795c6fSMarcus Overhagenfloat TipView::_maxTextWidth() {
218a0795c6fSMarcus Overhagen	float max = 0.0;
219a0795c6fSMarcus Overhagen	for(uint32 n = 0; n < m_lineSet.size(); ++n) {
220a0795c6fSMarcus Overhagen		uint32 from = m_lineSet[n];
221a0795c6fSMarcus Overhagen		uint32 to = (n < m_lineSet.size()-1) ? m_lineSet[n+1]-1 :
222a0795c6fSMarcus Overhagen			m_text.Length();
223a0795c6fSMarcus Overhagen		float lineWidth = m_font.StringWidth(
224a0795c6fSMarcus Overhagen			m_text.String() + from,
225a0795c6fSMarcus Overhagen			to - from);
226a0795c6fSMarcus Overhagen		if(lineWidth > max)
227a0795c6fSMarcus Overhagen			max = lineWidth;
228a0795c6fSMarcus Overhagen	}
229a0795c6fSMarcus Overhagen	return max;
230a0795c6fSMarcus Overhagen}
231a0795c6fSMarcus Overhagen
232a0795c6fSMarcus Overhagenfloat TipView::_textHeight() {
233a0795c6fSMarcus Overhagen	float height = m_fontHeight.ascent + m_fontHeight.descent;
234a0795c6fSMarcus Overhagen	if(m_lineSet.size() > 1)
235a0795c6fSMarcus Overhagen		height += (m_lineSet.size()-1) *
236a0795c6fSMarcus Overhagen			(m_fontHeight.ascent + m_fontHeight.descent + m_fontHeight.leading);
237a0795c6fSMarcus Overhagen
238a0795c6fSMarcus Overhagen	return height;
239a0795c6fSMarcus Overhagen}
240a0795c6fSMarcus Overhagen
24123e67806SIthamar R. Adema// END -- TipView.cpp --
242