TZDisplay.cpp revision 323ba9b7
1fd7c0c2eSStephan Aßmus/*
2323ba9b7SRyan Leavengood * Copyright 2004-2011, Haiku, Inc. All Rights Reserved.
3fd7c0c2eSStephan Aßmus * Distributed under the terms of the MIT License.
4fd7c0c2eSStephan Aßmus *
5fd7c0c2eSStephan Aßmus * Authors:
600826781SKarsten Heimrich *		Andrew McCall <mccall@@digitalparadise.co.uk>
700826781SKarsten Heimrich *		Mike Berg <mike@berg-net.us>
8fd7c0c2eSStephan Aßmus *		Julun <host.haiku@gmx.de>
9323ba9b7SRyan Leavengood *		Hamish Morrison <hamish@lavabit.com>
10fd7c0c2eSStephan Aßmus */
1113889939SPhil Greenway
1213889939SPhil Greenway#include "TZDisplay.h"
1313889939SPhil Greenway
14323ba9b7SRyan Leavengood#include <stdio.h>
15323ba9b7SRyan Leavengood
16323ba9b7SRyan Leavengood#include <LayoutUtils.h>
1713889939SPhil Greenway
18fd7c0c2eSStephan Aßmus
19fd7c0c2eSStephan Aßmus
20323ba9b7SRyan LeavengoodTTZDisplay::TTZDisplay(const char* name, const char* label)
21323ba9b7SRyan Leavengood	:
22323ba9b7SRyan Leavengood	BView(name, B_WILL_DRAW),
23323ba9b7SRyan Leavengood	fLabel(label),
24323ba9b7SRyan Leavengood	fText(""),
25323ba9b7SRyan Leavengood	fTime("")
2613889939SPhil Greenway{
2713889939SPhil Greenway}
2813889939SPhil Greenway
2913889939SPhil Greenway
3013889939SPhil GreenwayTTZDisplay::~TTZDisplay()
3113889939SPhil Greenway{
3213889939SPhil Greenway}
3313889939SPhil Greenway
3413889939SPhil Greenway
3513889939SPhil Greenwayvoid
3613889939SPhil GreenwayTTZDisplay::AttachedToWindow()
3713889939SPhil Greenway{
3813889939SPhil Greenway	if (Parent())
3913889939SPhil Greenway		SetViewColor(Parent()->ViewColor());
4013889939SPhil Greenway}
4113889939SPhil Greenway
4213889939SPhil Greenway
4313889939SPhil Greenwayvoid
4413889939SPhil GreenwayTTZDisplay::ResizeToPreferred()
4513889939SPhil Greenway{
46323ba9b7SRyan Leavengood	BSize size = _CalcPrefSize();
47323ba9b7SRyan Leavengood	ResizeTo(size.width, size.height);
4813889939SPhil Greenway}
4913889939SPhil Greenway
5013889939SPhil Greenway
5113889939SPhil Greenwayvoid
52323ba9b7SRyan LeavengoodTTZDisplay::Draw(BRect)
5313889939SPhil Greenway{
5413889939SPhil Greenway	SetLowColor(ViewColor());
5585b69a94SKarsten Heimrich
5685b69a94SKarsten Heimrich	BRect bounds = Bounds();
5785b69a94SKarsten Heimrich	FillRect(Bounds(), B_SOLID_LOW);
58323ba9b7SRyan Leavengood
59323ba9b7SRyan Leavengood	font_height height;
60323ba9b7SRyan Leavengood	GetFontHeight(&height);
61323ba9b7SRyan Leavengood	float fontHeight = ceilf(height.descent + height.ascent +
62323ba9b7SRyan Leavengood		height.leading);
636d6408d6SOliver Tappe
64323ba9b7SRyan Leavengood	BPoint pt(bounds.left, ceilf(bounds.top + height.ascent));
6585b69a94SKarsten Heimrich	DrawString(fLabel.String(), pt);
6613889939SPhil Greenway
6785b69a94SKarsten Heimrich	pt.y += fontHeight;
6885b69a94SKarsten Heimrich	DrawString(fText.String(), pt);
696d6408d6SOliver Tappe
7099d2aa98SOliver Tappe	pt.y -= fontHeight;
71323ba9b7SRyan Leavengood	pt.x = bounds.right - StringWidth(fTime.String());
7285b69a94SKarsten Heimrich	DrawString(fTime.String(), pt);
73fd7c0c2eSStephan Aßmus}
74fd7c0c2eSStephan Aßmus
75fd7c0c2eSStephan Aßmus
76fd7c0c2eSStephan Aßmusconst char*
77fd7c0c2eSStephan AßmusTTZDisplay::Label() const
78fd7c0c2eSStephan Aßmus{
79fd7c0c2eSStephan Aßmus	return fLabel.String();
8013889939SPhil Greenway}
8113889939SPhil Greenway
8213889939SPhil Greenway
8313889939SPhil Greenwayvoid
846d6408d6SOliver TappeTTZDisplay::SetLabel(const char* label)
8513889939SPhil Greenway{
86fd7c0c2eSStephan Aßmus	fLabel.SetTo(label);
8738ac8defSOliver Tappe	Invalidate();
88323ba9b7SRyan Leavengood	InvalidateLayout();
8913889939SPhil Greenway}
9013889939SPhil Greenway
915065a785SStefano Ceccherini
92fd7c0c2eSStephan Aßmusconst char*
93fd7c0c2eSStephan AßmusTTZDisplay::Text() const
94fd7c0c2eSStephan Aßmus{
95fd7c0c2eSStephan Aßmus	return fText.String();
96fd7c0c2eSStephan Aßmus}
97fd7c0c2eSStephan Aßmus
98fd7c0c2eSStephan Aßmus
9913889939SPhil Greenwayvoid
1006d6408d6SOliver TappeTTZDisplay::SetText(const char* text)
10113889939SPhil Greenway{
102fd7c0c2eSStephan Aßmus	fText.SetTo(text);
10338ac8defSOliver Tappe	Invalidate();
104323ba9b7SRyan Leavengood	InvalidateLayout();
10513889939SPhil Greenway}
10613889939SPhil Greenway
1075065a785SStefano Ceccherini
108fd7c0c2eSStephan Aßmusconst char*
109fd7c0c2eSStephan AßmusTTZDisplay::Time() const
110fd7c0c2eSStephan Aßmus{
111fd7c0c2eSStephan Aßmus	return fTime.String();
112fd7c0c2eSStephan Aßmus}
113fd7c0c2eSStephan Aßmus
114fd7c0c2eSStephan Aßmus
11513889939SPhil Greenwayvoid
11638ac8defSOliver TappeTTZDisplay::SetTime(const char* time)
11713889939SPhil Greenway{
11838ac8defSOliver Tappe	fTime.SetTo(time);
119fd7c0c2eSStephan Aßmus	Invalidate();
120323ba9b7SRyan Leavengood	InvalidateLayout();
121323ba9b7SRyan Leavengood}
122323ba9b7SRyan Leavengood
123323ba9b7SRyan Leavengood
124323ba9b7SRyan LeavengoodBSize
125323ba9b7SRyan LeavengoodTTZDisplay::MaxSize()
126323ba9b7SRyan Leavengood{
127323ba9b7SRyan Leavengood	BSize size = _CalcPrefSize();
128323ba9b7SRyan Leavengood	size.width = B_SIZE_UNLIMITED;
129323ba9b7SRyan Leavengood
130323ba9b7SRyan Leavengood	return BLayoutUtils::ComposeSize(ExplicitMaxSize(),
131323ba9b7SRyan Leavengood		size);
13226c008b7SPhil Greenway}
13326c008b7SPhil Greenway
134323ba9b7SRyan Leavengood
135323ba9b7SRyan LeavengoodBSize
136323ba9b7SRyan LeavengoodTTZDisplay::MinSize()
137323ba9b7SRyan Leavengood{
138323ba9b7SRyan Leavengood	return BLayoutUtils::ComposeSize(ExplicitMinSize(),
139323ba9b7SRyan Leavengood		_CalcPrefSize());
140323ba9b7SRyan Leavengood}
141323ba9b7SRyan Leavengood
142323ba9b7SRyan Leavengood
143323ba9b7SRyan LeavengoodBSize
144323ba9b7SRyan LeavengoodTTZDisplay::PreferredSize()
145323ba9b7SRyan Leavengood{
146323ba9b7SRyan Leavengood	return BLayoutUtils::ComposeSize(ExplicitPreferredSize(),
147323ba9b7SRyan Leavengood		_CalcPrefSize());
148323ba9b7SRyan Leavengood}
149323ba9b7SRyan Leavengood
150323ba9b7SRyan Leavengood
151323ba9b7SRyan LeavengoodBSize
152323ba9b7SRyan LeavengoodTTZDisplay::_CalcPrefSize()
153323ba9b7SRyan Leavengood{
154323ba9b7SRyan Leavengood	font_height fontHeight;
155323ba9b7SRyan Leavengood	GetFontHeight(&fontHeight);
156323ba9b7SRyan Leavengood
157323ba9b7SRyan Leavengood	BSize size;
158323ba9b7SRyan Leavengood	size.height = 2 * ceilf(fontHeight.ascent + fontHeight.descent +
159323ba9b7SRyan Leavengood		fontHeight.leading);
160323ba9b7SRyan Leavengood
161323ba9b7SRyan Leavengood	float firstLine = ceilf(StringWidth(fLabel.String()) +
162323ba9b7SRyan Leavengood		StringWidth(" ") + StringWidth(fTime.String()));
163323ba9b7SRyan Leavengood	float secondLine = ceilf(StringWidth(fText.String()));
164323ba9b7SRyan Leavengood	size.width = firstLine > secondLine ? firstLine : secondLine;
165323ba9b7SRyan Leavengood	return size;
166323ba9b7SRyan Leavengood}
167