126c008b7SPhil Greenway/*
242b3a11fSStephan Aßmus * Copyright 2004-2007, Haiku, Inc. All Rights Reserved.
342b3a11fSStephan Aßmus * Distributed under the terms of the MIT License.
442b3a11fSStephan Aßmus *
542b3a11fSStephan Aßmus * Authors:
600826781SKarsten Heimrich *		Mike Berg <mike@berg-net.us>
742b3a11fSStephan Aßmus *		Julun <host.haiku@gmx.de>
842b3a11fSStephan Aßmus */
9b66afdfaSPhil Greenway
10b66afdfaSPhil Greenway
11323ba9b7SRyan Leavengood#include "BaseView.h"
1213889939SPhil Greenway
1378092ae7SKarsten Heimrich#include <DateTime.h>
1442b3a11fSStephan Aßmus#include <OS.h>
1542b3a11fSStephan Aßmus
16323ba9b7SRyan Leavengood#include "TimeMessages.h"
17323ba9b7SRyan Leavengood
1842b3a11fSStephan Aßmus
19323ba9b7SRyan LeavengoodTTimeBaseView::TTimeBaseView(const char* name)
20323ba9b7SRyan Leavengood	:
21323ba9b7SRyan Leavengood	BGroupView(name, B_VERTICAL, 0),
22323ba9b7SRyan Leavengood	fMessage(H_TIME_UPDATE)
2313889939SPhil Greenway{
24323ba9b7SRyan Leavengood	SetFlags(Flags() | B_PULSE_NEEDED);
25b66afdfaSPhil Greenway}
26b66afdfaSPhil Greenway
2713889939SPhil Greenway
28b66afdfaSPhil GreenwayTTimeBaseView::~TTimeBaseView()
2913889939SPhil Greenway{
3026c008b7SPhil Greenway}
3126c008b7SPhil Greenway
3226c008b7SPhil Greenway
33b66afdfaSPhil Greenwayvoid
34b66afdfaSPhil GreenwayTTimeBaseView::Pulse()
3513889939SPhil Greenway{
3613889939SPhil Greenway	if (IsWatched())
3700826781SKarsten Heimrich		_SendNotices();
3813889939SPhil Greenway}
3913889939SPhil Greenway
4013889939SPhil Greenway
41e44561b2SStefano Ceccherinivoid
42e44561b2SStefano CeccheriniTTimeBaseView::AttachedToWindow()
43e44561b2SStefano Ceccherini{
44f0650dc9Slooncraz	SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
45f0650dc9Slooncraz	SetLowUIColor(ViewUIColor());
46e44561b2SStefano Ceccherini}
47e44561b2SStefano Ceccherini
48e44561b2SStefano Ceccherini
4913889939SPhil Greenwayvoid
506d6408d6SOliver TappeTTimeBaseView::ChangeTime(BMessage* message)
5113889939SPhil Greenway{
5242b3a11fSStephan Aßmus	bool isTime;
5342b3a11fSStephan Aßmus	if (message->FindBool("time", &isTime) != B_OK)
5413889939SPhil Greenway		return;
5513889939SPhil Greenway
5600826781SKarsten Heimrich	BDateTime dateTime = BDateTime::CurrentDateTime(B_LOCAL_TIME);
5700826781SKarsten Heimrich
5842b3a11fSStephan Aßmus	if (isTime) {
5900826781SKarsten Heimrich		BTime time = dateTime.Time();
6000826781SKarsten Heimrich		int32 hour;
6100826781SKarsten Heimrich		if (message->FindInt32("hour", &hour) != B_OK)
6200826781SKarsten Heimrich			hour  = time.Hour();
6300826781SKarsten Heimrich
6400826781SKarsten Heimrich		int32 minute;
6500826781SKarsten Heimrich		if (message->FindInt32("minute", &minute) != B_OK)
6600826781SKarsten Heimrich			minute = time.Minute();
6742b3a11fSStephan Aßmus
6800826781SKarsten Heimrich		int32 second;
6900826781SKarsten Heimrich		if (message->FindInt32("second", &second) != B_OK)
7000826781SKarsten Heimrich			second = time.Second();
7142b3a11fSStephan Aßmus
7200826781SKarsten Heimrich		time.SetTime(hour, minute, second);
7300826781SKarsten Heimrich		dateTime.SetTime(time);
7400826781SKarsten Heimrich	} else {
7500826781SKarsten Heimrich		BDate date = dateTime.Date();
7600826781SKarsten Heimrich		int32 day;
7700826781SKarsten Heimrich		if (message->FindInt32("day", &day) != B_OK)
7800826781SKarsten Heimrich			day = date.Day();
7900826781SKarsten Heimrich
8000826781SKarsten Heimrich		int32 year;
8100826781SKarsten Heimrich		if (message->FindInt32("year", &year) != B_OK)
8200826781SKarsten Heimrich			year = date.Year();
8300826781SKarsten Heimrich
8400826781SKarsten Heimrich		int32 month;
8500826781SKarsten Heimrich		if (message->FindInt32("month", &month) != B_OK)
8600826781SKarsten Heimrich			month = date.Month();
8700826781SKarsten Heimrich
8800826781SKarsten Heimrich		if (year >= 1970 && year <= 2037) {
8900826781SKarsten Heimrich			date.SetDate(year, month, day);
9000826781SKarsten Heimrich			dateTime.SetDate(date);
9100826781SKarsten Heimrich		}
92b66afdfaSPhil Greenway	}
9300826781SKarsten Heimrich
9400826781SKarsten Heimrich	set_real_time_clock(dateTime.Time_t());
9542b3a11fSStephan Aßmus}
9642b3a11fSStephan Aßmus
9742b3a11fSStephan Aßmus
9842b3a11fSStephan Aßmusvoid
9900826781SKarsten HeimrichTTimeBaseView::_SendNotices()
10042b3a11fSStephan Aßmus{
10142b3a11fSStephan Aßmus	fMessage.MakeEmpty();
10278092ae7SKarsten Heimrich
10300826781SKarsten Heimrich	BDate date = BDate::CurrentDate(B_LOCAL_TIME);
10400826781SKarsten Heimrich	fMessage.AddInt32("day", date.Day());
10500826781SKarsten Heimrich	fMessage.AddInt32("year", date.Year());
10600826781SKarsten Heimrich	fMessage.AddInt32("month", date.Month());
10700826781SKarsten Heimrich
10800826781SKarsten Heimrich	BTime time = BTime::CurrentTime(B_LOCAL_TIME);
10900826781SKarsten Heimrich	fMessage.AddInt32("hour", time.Hour());
11000826781SKarsten Heimrich	fMessage.AddInt32("minute", time.Minute());
11100826781SKarsten Heimrich	fMessage.AddInt32("second", time.Second());
11242b3a11fSStephan Aßmus
11342b3a11fSStephan Aßmus	SendNotices(H_TM_CHANGED, &fMessage);
114b66afdfaSPhil Greenway}
11542b3a11fSStephan Aßmus
116