TZDisplay.cpp revision 00826781
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);
22fd7c0c2eSStephan Aßmus		float height = ceil(fontHeight.descent) + ceil(fontHeight.ascent)
23fd7c0c2eSStephan Aßmus			+ ceil(fontHeight.leading);
24fd7c0c2eSStephan Aßmus		return height;
25fd7c0c2eSStephan Aßmus	}
26fd7c0c2eSStephan Aßmus}
27fd7c0c2eSStephan Aßmus
28fd7c0c2eSStephan Aßmus
29fd7c0c2eSStephan AßmusTTZDisplay::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{
54fd7c0c2eSStephan Aßmus	float height = _FontHeight();
5513889939SPhil Greenway	ResizeTo(Bounds().Width(), height *2);
5613889939SPhil Greenway}
5713889939SPhil Greenway
5813889939SPhil Greenway
5913889939SPhil Greenwayvoid
60fd7c0c2eSStephan AßmusTTZDisplay::Draw(BRect /* updateRect */)
6113889939SPhil Greenway{
6213889939SPhil Greenway	BRect bounds(Bounds());
6313889939SPhil Greenway	SetLowColor(ViewColor());
6413889939SPhil Greenway	FillRect(bounds, B_SOLID_LOW);
6513889939SPhil Greenway
66fd7c0c2eSStephan Aßmus	float height = _FontHeight();
6713889939SPhil Greenway
68fd7c0c2eSStephan Aßmus	BPoint drawpt(bounds.left +2, height /2.0 +1);
69fd7c0c2eSStephan Aßmus	DrawString(fLabel.String(), drawpt);
7013889939SPhil Greenway
71fd7c0c2eSStephan Aßmus	drawpt.y += height +2;
72fd7c0c2eSStephan Aßmus	DrawString(fText.String(), drawpt);
7313889939SPhil Greenway
74fd7c0c2eSStephan Aßmus	drawpt.x = bounds.right -be_plain_font->StringWidth(fTime.String()) - 2;
75fd7c0c2eSStephan Aßmus	DrawString(fTime.String(), drawpt);
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
8713889939SPhil GreenwayTTZDisplay::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
10213889939SPhil GreenwayTTZDisplay::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;
12213889939SPhil Greenway
12313889939SPhil Greenway	if (ahour == 0)
12413889939SPhil Greenway		ahour = 12;
12513889939SPhil Greenway
126fd7c0c2eSStephan Aßmus	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