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{
38f0650dc9Slooncraz	AdoptParentColors();
3913889939SPhil Greenway}
4013889939SPhil Greenway
4113889939SPhil Greenway
4213889939SPhil Greenwayvoid
4313889939SPhil GreenwayTTZDisplay::ResizeToPreferred()
4413889939SPhil Greenway{
45323ba9b7SRyan Leavengood	BSize size = _CalcPrefSize();
46323ba9b7SRyan Leavengood	ResizeTo(size.width, size.height);
4713889939SPhil Greenway}
4813889939SPhil Greenway
4913889939SPhil Greenway
5013889939SPhil Greenwayvoid
51323ba9b7SRyan LeavengoodTTZDisplay::Draw(BRect)
5213889939SPhil Greenway{
5313889939SPhil Greenway	SetLowColor(ViewColor());
5485b69a94SKarsten Heimrich
5585b69a94SKarsten Heimrich	BRect bounds = Bounds();
5685b69a94SKarsten Heimrich	FillRect(Bounds(), B_SOLID_LOW);
57c2f3ee3bSAdrien Destugues
58323ba9b7SRyan Leavengood	font_height height;
59323ba9b7SRyan Leavengood	GetFontHeight(&height);
60323ba9b7SRyan Leavengood	float fontHeight = ceilf(height.descent + height.ascent +
61323ba9b7SRyan Leavengood		height.leading);
626d6408d6SOliver Tappe
63323ba9b7SRyan Leavengood	BPoint pt(bounds.left, ceilf(bounds.top + height.ascent));
6485b69a94SKarsten Heimrich	DrawString(fLabel.String(), pt);
6513889939SPhil Greenway
6685b69a94SKarsten Heimrich	pt.y += fontHeight;
6785b69a94SKarsten Heimrich	DrawString(fText.String(), pt);
686d6408d6SOliver Tappe
6999d2aa98SOliver Tappe	pt.y -= fontHeight;
70323ba9b7SRyan Leavengood	pt.x = bounds.right - StringWidth(fTime.String());
7185b69a94SKarsten Heimrich	DrawString(fTime.String(), pt);
72fd7c0c2eSStephan Aßmus}
73fd7c0c2eSStephan Aßmus
74fd7c0c2eSStephan Aßmus
75fd7c0c2eSStephan Aßmusconst char*
76fd7c0c2eSStephan AßmusTTZDisplay::Label() const
77fd7c0c2eSStephan Aßmus{
78fd7c0c2eSStephan Aßmus	return fLabel.String();
7913889939SPhil Greenway}
8013889939SPhil Greenway
8113889939SPhil Greenway
8213889939SPhil Greenwayvoid
836d6408d6SOliver TappeTTZDisplay::SetLabel(const char* label)
8413889939SPhil Greenway{
85fd7c0c2eSStephan Aßmus	fLabel.SetTo(label);
8638ac8defSOliver Tappe	Invalidate();
87323ba9b7SRyan Leavengood	InvalidateLayout();
8813889939SPhil Greenway}
8913889939SPhil Greenway
905065a785SStefano Ceccherini
91fd7c0c2eSStephan Aßmusconst char*
92fd7c0c2eSStephan AßmusTTZDisplay::Text() const
93fd7c0c2eSStephan Aßmus{
94fd7c0c2eSStephan Aßmus	return fText.String();
95fd7c0c2eSStephan Aßmus}
96fd7c0c2eSStephan Aßmus
97fd7c0c2eSStephan Aßmus
9813889939SPhil Greenwayvoid
996d6408d6SOliver TappeTTZDisplay::SetText(const char* text)
10013889939SPhil Greenway{
101fd7c0c2eSStephan Aßmus	fText.SetTo(text);
10238ac8defSOliver Tappe	Invalidate();
103323ba9b7SRyan Leavengood	InvalidateLayout();
10413889939SPhil Greenway}
10513889939SPhil Greenway
1065065a785SStefano Ceccherini
107fd7c0c2eSStephan Aßmusconst char*
108fd7c0c2eSStephan AßmusTTZDisplay::Time() const
109fd7c0c2eSStephan Aßmus{
110fd7c0c2eSStephan Aßmus	return fTime.String();
111fd7c0c2eSStephan Aßmus}
112fd7c0c2eSStephan Aßmus
113fd7c0c2eSStephan Aßmus
11413889939SPhil Greenwayvoid
11538ac8defSOliver TappeTTZDisplay::SetTime(const char* time)
11613889939SPhil Greenway{
11738ac8defSOliver Tappe	fTime.SetTo(time);
118fd7c0c2eSStephan Aßmus	Invalidate();
119323ba9b7SRyan Leavengood	InvalidateLayout();
120323ba9b7SRyan Leavengood}
121323ba9b7SRyan Leavengood
122323ba9b7SRyan Leavengood
123323ba9b7SRyan LeavengoodBSize
124323ba9b7SRyan LeavengoodTTZDisplay::MaxSize()
125323ba9b7SRyan Leavengood{
126323ba9b7SRyan Leavengood	BSize size = _CalcPrefSize();
127323ba9b7SRyan Leavengood	size.width = B_SIZE_UNLIMITED;
128323ba9b7SRyan Leavengood
129323ba9b7SRyan Leavengood	return BLayoutUtils::ComposeSize(ExplicitMaxSize(),
130323ba9b7SRyan Leavengood		size);
13126c008b7SPhil Greenway}
13226c008b7SPhil Greenway
133323ba9b7SRyan Leavengood
134323ba9b7SRyan LeavengoodBSize
135323ba9b7SRyan LeavengoodTTZDisplay::MinSize()
136323ba9b7SRyan Leavengood{
137323ba9b7SRyan Leavengood	return BLayoutUtils::ComposeSize(ExplicitMinSize(),
138323ba9b7SRyan Leavengood		_CalcPrefSize());
139323ba9b7SRyan Leavengood}
140323ba9b7SRyan Leavengood
141323ba9b7SRyan Leavengood
142323ba9b7SRyan LeavengoodBSize
143323ba9b7SRyan LeavengoodTTZDisplay::PreferredSize()
144323ba9b7SRyan Leavengood{
145323ba9b7SRyan Leavengood	return BLayoutUtils::ComposeSize(ExplicitPreferredSize(),
146323ba9b7SRyan Leavengood		_CalcPrefSize());
147323ba9b7SRyan Leavengood}
148323ba9b7SRyan Leavengood
149323ba9b7SRyan Leavengood
150323ba9b7SRyan LeavengoodBSize
151323ba9b7SRyan LeavengoodTTZDisplay::_CalcPrefSize()
152323ba9b7SRyan Leavengood{
153323ba9b7SRyan Leavengood	font_height fontHeight;
154323ba9b7SRyan Leavengood	GetFontHeight(&fontHeight);
155323ba9b7SRyan Leavengood
156323ba9b7SRyan Leavengood	BSize size;
157323ba9b7SRyan Leavengood	size.height = 2 * ceilf(fontHeight.ascent + fontHeight.descent +
158323ba9b7SRyan Leavengood		fontHeight.leading);
159323ba9b7SRyan Leavengood
1605fb0d3a6Sleavengood	// Add a little padding
1615fb0d3a6Sleavengood	float padding = 10.0;
162323ba9b7SRyan Leavengood	float firstLine = ceilf(StringWidth(fLabel.String()) +
1635fb0d3a6Sleavengood		StringWidth(" ") + StringWidth(fTime.String()) + padding);
1645fb0d3a6Sleavengood	float secondLine = ceilf(StringWidth(fText.String()) + padding);
165323ba9b7SRyan Leavengood	size.width = firstLine > secondLine ? firstLine : secondLine;
166c2f3ee3bSAdrien Destugues
167323ba9b7SRyan Leavengood	return size;
168323ba9b7SRyan Leavengood}
169