TZDisplay.cpp revision 6d6408d6
1fd7c0c2eSStephan Aßmus/*
2fd7c0c2eSStephan Aßmus * Copyright 2004-2007, 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>
9fd7c0c2eSStephan Aßmus *
10fd7c0c2eSStephan Aßmus */
1113889939SPhil Greenway
129c16019fSStefano Ceccherini#include <stdio.h>
139c16019fSStefano Ceccherini
1413889939SPhil Greenway#include "TZDisplay.h"
1513889939SPhil Greenway
1613889939SPhil Greenway
17fd7c0c2eSStephan Aßmusnamespace {
18fd7c0c2eSStephan Aßmus	float _FontHeight()
19fd7c0c2eSStephan Aßmus	{
20fd7c0c2eSStephan Aßmus		font_height fontHeight;
21fd7c0c2eSStephan Aßmus		be_plain_font->GetHeight(&fontHeight);
2285b69a94SKarsten Heimrich		float height = ceil(fontHeight.descent + fontHeight.ascent
2385b69a94SKarsten Heimrich			+ fontHeight.leading);
24fd7c0c2eSStephan Aßmus		return height;
25fd7c0c2eSStephan Aßmus	}
26fd7c0c2eSStephan Aßmus}
27fd7c0c2eSStephan Aßmus
28fd7c0c2eSStephan Aßmus
296d6408d6SOliver TappeTTZDisplay::TTZDisplay(BRect frame, const char* name, const char* label)
30fd7c0c2eSStephan Aßmus	: BView(frame, name, B_FOLLOW_NONE, B_WILL_DRAW),
31fd7c0c2eSStephan Aßmus	  fLabel(label),
32fd7c0c2eSStephan Aßmus	  fText(""),
33fd7c0c2eSStephan Aßmus	  fTime("")
3413889939SPhil Greenway{
3513889939SPhil Greenway}
3613889939SPhil Greenway
3713889939SPhil Greenway
3813889939SPhil GreenwayTTZDisplay::~TTZDisplay()
3913889939SPhil Greenway{
4013889939SPhil Greenway}
4113889939SPhil Greenway
4213889939SPhil Greenway
4313889939SPhil Greenwayvoid
4413889939SPhil GreenwayTTZDisplay::AttachedToWindow()
4513889939SPhil Greenway{
4613889939SPhil Greenway	if (Parent())
4713889939SPhil Greenway		SetViewColor(Parent()->ViewColor());
4813889939SPhil Greenway}
4913889939SPhil Greenway
5013889939SPhil Greenway
5113889939SPhil Greenwayvoid
5213889939SPhil GreenwayTTZDisplay::ResizeToPreferred()
5313889939SPhil Greenway{
5485b69a94SKarsten Heimrich	ResizeTo(Bounds().Width(), _FontHeight() * 2.0 + 4.0);
5513889939SPhil Greenway}
5613889939SPhil Greenway
5713889939SPhil Greenway
5813889939SPhil Greenwayvoid
59fd7c0c2eSStephan AßmusTTZDisplay::Draw(BRect /* updateRect */)
6013889939SPhil Greenway{
6113889939SPhil Greenway	SetLowColor(ViewColor());
6285b69a94SKarsten Heimrich
6385b69a94SKarsten Heimrich	BRect bounds = Bounds();
6485b69a94SKarsten Heimrich	FillRect(Bounds(), B_SOLID_LOW);
656d6408d6SOliver Tappe
6685b69a94SKarsten Heimrich	float fontHeight = _FontHeight();
6713889939SPhil Greenway
6885b69a94SKarsten Heimrich	BPoint pt(bounds.left + 2.0, fontHeight / 2.0 + 2.0);
6985b69a94SKarsten Heimrich	DrawString(fLabel.String(), pt);
7013889939SPhil Greenway
7185b69a94SKarsten Heimrich	pt.y += fontHeight;
7285b69a94SKarsten Heimrich	DrawString(fText.String(), pt);
736d6408d6SOliver Tappe
7485b69a94SKarsten Heimrich	pt.x = bounds.right - StringWidth(fTime.String()) - 2.0;
7585b69a94SKarsten Heimrich	DrawString(fTime.String(), pt);
76fd7c0c2eSStephan Aßmus}
77fd7c0c2eSStephan Aßmus
78fd7c0c2eSStephan Aßmus
79fd7c0c2eSStephan Aßmusconst char*
80fd7c0c2eSStephan AßmusTTZDisplay::Label() const
81fd7c0c2eSStephan Aßmus{
82fd7c0c2eSStephan Aßmus	return fLabel.String();
8313889939SPhil Greenway}
8413889939SPhil Greenway
8513889939SPhil Greenway
8613889939SPhil Greenwayvoid
876d6408d6SOliver TappeTTZDisplay::SetLabel(const char* label)
8813889939SPhil Greenway{
89fd7c0c2eSStephan Aßmus	fLabel.SetTo(label);
9013889939SPhil Greenway	Draw(Bounds());
9113889939SPhil Greenway}
9213889939SPhil Greenway
935065a785SStefano Ceccherini
94fd7c0c2eSStephan Aßmusconst char*
95fd7c0c2eSStephan AßmusTTZDisplay::Text() const
96fd7c0c2eSStephan Aßmus{
97fd7c0c2eSStephan Aßmus	return fText.String();
98fd7c0c2eSStephan Aßmus}
99fd7c0c2eSStephan Aßmus
100fd7c0c2eSStephan Aßmus
10113889939SPhil Greenwayvoid
1026d6408d6SOliver TappeTTZDisplay::SetText(const char* text)
10313889939SPhil Greenway{
104fd7c0c2eSStephan Aßmus	fText.SetTo(text);
10513889939SPhil Greenway	Draw(Bounds());
10613889939SPhil Greenway}
10713889939SPhil Greenway
1085065a785SStefano Ceccherini
109fd7c0c2eSStephan Aßmusconst char*
110fd7c0c2eSStephan AßmusTTZDisplay::Time() const
111fd7c0c2eSStephan Aßmus{
112fd7c0c2eSStephan Aßmus	return fTime.String();
113fd7c0c2eSStephan Aßmus}
114fd7c0c2eSStephan Aßmus
115fd7c0c2eSStephan Aßmus
11613889939SPhil Greenwayvoid
117fd7c0c2eSStephan AßmusTTZDisplay::SetTime(int32 hour, int32 minute)
11813889939SPhil Greenway{
11913889939SPhil Greenway	int32 ahour = hour;
120fd7c0c2eSStephan Aßmus	if (hour > 12)
12113889939SPhil Greenway		ahour = hour -12;
1226d6408d6SOliver Tappe
12313889939SPhil Greenway	if (ahour == 0)
12413889939SPhil Greenway		ahour = 12;
12513889939SPhil Greenway
1266d6408d6SOliver Tappe	const char* ap = "AM";
127fd7c0c2eSStephan Aßmus	if (hour > 11)
12826c008b7SPhil Greenway		ap = "PM";
12926c008b7SPhil Greenway
1309c16019fSStefano Ceccherini	char buffer[32];
1319c16019fSStefano Ceccherini	snprintf(buffer, sizeof(buffer), "%ld:%02ld %s", ahour, minute, ap);
1329c16019fSStefano Ceccherini
1339c16019fSStefano Ceccherini	fTime.SetTo(buffer);
1345065a785SStefano Ceccherini
135fd7c0c2eSStephan Aßmus	Invalidate();
13626c008b7SPhil Greenway}
13726c008b7SPhil Greenway
138