2 * Copyright 2004-2011, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
6 * Andrew McCall <mccall@@digitalparadise.co.uk>
7 * Mike Berg <mike@berg-net.us>
8 * Julun <host.haiku@gmx.de>
9 * Philippe Saint-Pierre <stpere@gmail.com>
10 * Hamish Morrison <hamish@lavabit.com>
13 #include "DateTimeView.h"
19 #include <CalendarView.h>
22 #include <ControlLook.h>
26 #include <FindDirectory.h>
29 #include <StringView.h>
32 #include "AnalogClock.h"
33 #include "DateTimeEdit.h"
34 #include "TimeMessages.h"
35 #include "TimeWindow.h"
38 #undef B_TRANSLATION_CONTEXT
39 #define B_TRANSLATION_CONTEXT "Time"
42 using BPrivate::BCalendarView
;
43 using BPrivate::BDateTime
;
44 using BPrivate::B_LOCAL_TIME
;
47 DateTimeView::DateTimeView(const char* name
)
49 BGroupView(name
, B_HORIZONTAL
, 5),
51 fSystemTimeAtStart(system_time())
55 // record the current time to enable revert.
60 DateTimeView::~DateTimeView()
66 DateTimeView::AttachedToWindow()
69 SetViewColor(Parent()->ViewColor());
74 fCalendarView
->SetTarget(this);
80 DateTimeView::MessageReceived(BMessage
* message
)
83 switch (message
->what
) {
84 case B_OBSERVER_NOTICE_CHANGE
:
85 message
->FindInt32(B_OBSERVE_WHAT_CHANGE
, &change
);
88 _UpdateDateTime(message
);
92 BView::MessageReceived(message
);
99 BMessage
msg(*message
);
100 msg
.what
= H_USER_CHANGE
;
101 msg
.AddBool("time", false);
102 Window()->PostMessage(&msg
);
110 case kChangeTimeFinished
:
111 if (fClock
->IsChangingTime())
112 fTimeEdit
->MakeFocus(false);
113 fClock
->ChangeTimeFinished();
120 BView::MessageReceived(message
);
127 DateTimeView::CheckCanRevert()
129 // check for changed time
130 time_t unchangedNow
= fTimeAtStart
+ _PrefletUptime();
134 return changedNow
!= unchangedNow
;
139 DateTimeView::_Revert()
141 // Set the clock and calendar as they were at launch time +
142 // time elapsed since application launch.
144 time_t timeNow
= fTimeAtStart
+ _PrefletUptime();
147 timeInfo
= localtime_r(&timeNow
, &result
);
149 BDateTime dateTime
= BDateTime::CurrentDateTime(B_LOCAL_TIME
);
150 BTime time
= dateTime
.Time();
151 BDate date
= dateTime
.Date();
152 time
.SetTime(timeInfo
->tm_hour
, timeInfo
->tm_min
, timeInfo
->tm_sec
% 60);
153 date
.SetDate(timeInfo
->tm_year
+ 1900, timeInfo
->tm_mon
+ 1,
155 dateTime
.SetTime(time
);
156 dateTime
.SetDate(date
);
158 set_real_time_clock(dateTime
.Time_t());
163 DateTimeView::_PrefletUptime() const
165 return (time_t)((system_time() - fSystemTimeAtStart
) / 1000000);
170 DateTimeView::_InitView()
172 fCalendarView
= new BCalendarView("calendar");
173 fCalendarView
->SetWeekNumberHeaderVisible(false);
174 fCalendarView
->SetSelectionMessage(new BMessage(kDayChanged
));
175 fCalendarView
->SetInvocationMessage(new BMessage(kDayChanged
));
177 fDateEdit
= new TDateEdit("dateEdit", 3);
178 fTimeEdit
= new TTimeEdit("timeEdit", 5);
179 fClock
= new TAnalogClock("analogClock");
181 BTime
time(BTime::CurrentTime(B_LOCAL_TIME
));
182 fClock
->SetTime(time
.Hour(), time
.Minute(), time
.Second());
184 BBox
* divider
= new BBox(BRect(0, 0, 1, 1),
185 B_EMPTY_STRING
, B_FOLLOW_ALL_SIDES
,
186 B_WILL_DRAW
| B_FRAME_EVENTS
, B_FANCY_BORDER
);
187 divider
->SetExplicitMaxSize(BSize(1, B_SIZE_UNLIMITED
));
189 const float kInset
= be_control_look
->DefaultItemSpacing();
190 BLayoutBuilder::Group
<>(this)
191 .AddGroup(B_VERTICAL
, kInset
/ 2)
196 .AddGroup(B_VERTICAL
, 0)
200 .SetInsets(kInset
, kInset
, kInset
, kInset
);
205 DateTimeView::_UpdateDateTime(BMessage
* message
)
210 if (message
->FindInt32("month", &month
) == B_OK
211 && message
->FindInt32("day", &day
) == B_OK
212 && message
->FindInt32("year", &year
) == B_OK
) {
213 static int32 lastDay
;
214 static int32 lastMonth
;
215 static int32 lastYear
;
216 if (day
!= lastDay
|| month
!= lastMonth
|| year
!= lastYear
) {
217 fDateEdit
->SetDate(year
, month
, day
);
218 fCalendarView
->SetDate(year
, month
, day
);
228 if (message
->FindInt32("hour", &hour
) == B_OK
229 && message
->FindInt32("minute", &minute
) == B_OK
230 && message
->FindInt32("second", &second
) == B_OK
) {
231 fClock
->SetTime(hour
, minute
, second
);
232 fTimeEdit
->SetTime(hour
, minute
, second
);