Applied patch by Philippe Saint-Pierre with some changes by myself:

* Added Revert button and Revert feature. The time at which the preflet
  is started is remembered, the uptime of the preflet is calculated
  via system_time().

TODO: Better placement for the Revert button. Currently it looks like it can
only revert date changes.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@25210 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stephan Aßmus
2008-04-27 12:10:19 +00:00
parent fe2edb706d
commit 1cae077af9
4 changed files with 111 additions and 6 deletions
+91 -3
View File
@@ -6,6 +6,7 @@
* Andrew McCall <mccall@@digitalparadise.co.uk> * Andrew McCall <mccall@@digitalparadise.co.uk>
* Mike Berg <[email protected]> * Mike Berg <[email protected]>
* Julun <[email protected]> * Julun <[email protected]>
* Philippe Saint-Pierre <[email protected]>
*/ */
#include "DateTimeView.h" #include "DateTimeView.h"
@@ -13,8 +14,10 @@
#include "CalendarView.h" #include "CalendarView.h"
#include "DateTimeEdit.h" #include "DateTimeEdit.h"
#include "TimeMessages.h" #include "TimeMessages.h"
#include "DateTime.h"
#include <Button.h>
#include <CheckBox.h> #include <CheckBox.h>
#include <Entry.h> #include <Entry.h>
#include <File.h> #include <File.h>
@@ -25,6 +28,7 @@
#include <String.h> #include <String.h>
#include <StringView.h> #include <StringView.h>
#include <Window.h> #include <Window.h>
#include <time.h>
#ifdef HAIKU_TARGET_PLATFORM_HAIKU #ifdef HAIKU_TARGET_PLATFORM_HAIKU
@@ -39,10 +43,14 @@ DateTimeView::DateTimeView(BRect frame)
: BView(frame, "dateTimeView", B_FOLLOW_NONE, B_WILL_DRAW | B_NAVIGABLE_JUMP), : BView(frame, "dateTimeView", B_FOLLOW_NONE, B_WILL_DRAW | B_NAVIGABLE_JUMP),
fGmtTime(NULL), fGmtTime(NULL),
fUseGmtTime(false), fUseGmtTime(false),
fInitialized(false) fInitialized(false),
fSystemTimeAtStart(system_time())
{ {
_ReadRTCSettings(); _ReadRTCSettings();
_InitView(); _InitView();
// record the current time to enable revert.
time(&fTimeAtStart);
} }
@@ -64,6 +72,7 @@ DateTimeView::AttachedToWindow()
fGmtTime->SetTarget(this); fGmtTime->SetTarget(this);
fLocalTime->SetTarget(this); fLocalTime->SetTarget(this);
fCalendarView->SetTarget(this); fCalendarView->SetTarget(this);
fRevertButton->SetTarget(this);
} }
} }
@@ -116,11 +125,18 @@ DateTimeView::MessageReceived(BMessage *message)
msg.what = H_USER_CHANGE; msg.what = H_USER_CHANGE;
msg.AddBool("time", false); msg.AddBool("time", false);
Window()->PostMessage(&msg); Window()->PostMessage(&msg);
} break; break;
}
case kRTCUpdate: case kRTCUpdate:
fUseGmtTime = !fUseGmtTime; fUseGmtTime = fGmtTime->Value() == B_CONTROL_ON;
_UpdateGmtSettings(); _UpdateGmtSettings();
CheckCanRevert();
break;
case kMsgRevert:
_Revert();
fRevertButton->SetEnabled(false);
break; break;
default: default:
@@ -130,6 +146,62 @@ DateTimeView::MessageReceived(BMessage *message)
} }
void
DateTimeView::CheckCanRevert()
{
// check GMT vs Local setting
bool enable = fUseGmtTime != fOldUseGmtTime;
// check for changed time
time_t unchangedNow = fTimeAtStart + _PrefletUptime();
time_t changedNow;
time(&changedNow);
enable = enable || (changedNow != unchangedNow);
fRevertButton->SetEnabled(enable);
}
void
DateTimeView::_Revert()
{
// Set the clock and calendar as they were at launch time +
// time ellapsed since application launch.
fUseGmtTime = fOldUseGmtTime;
_UpdateGmtSettings();
if (fUseGmtTime)
fGmtTime->SetValue(B_CONTROL_ON);
else
fLocalTime->SetValue(B_CONTROL_ON);
time_t timeNow = fTimeAtStart + _PrefletUptime();
struct tm result;
struct tm* timeInfo;
timeInfo = localtime_r(&timeNow, &result);
BDateTime dateTime = BDateTime::CurrentDateTime(B_LOCAL_TIME);
BTime time = dateTime.Time();
BDate date = dateTime.Date();
time.SetTime(timeInfo->tm_hour, timeInfo->tm_min, timeInfo->tm_sec % 60);
date.SetDate(timeInfo->tm_year + 1900, timeInfo->tm_mon + 1,
timeInfo->tm_mday);
dateTime.SetTime(time);
dateTime.SetDate(date);
set_real_time_clock(dateTime.Time_t());
}
time_t
DateTimeView::_PrefletUptime() const
{
return (time_t)((system_time() - fSystemTimeAtStart) / 1000000);
}
void void
DateTimeView::_InitView() DateTimeView::_InitView()
{ {
@@ -192,6 +264,22 @@ DateTimeView::_InitView()
else else
fLocalTime->SetValue(B_CONTROL_ON); fLocalTime->SetValue(B_CONTROL_ON);
fOldUseGmtTime = fUseGmtTime;
BRect rect = Bounds();
rect.left = 10;
rect.top = rect.bottom - 10;
fRevertButton = new BButton(rect, "revert", "Revert",
new BMessage(kMsgRevert), B_FOLLOW_LEFT | B_FOLLOW_BOTTOM, B_WILL_DRAW);
fRevertButton->ResizeToPreferred();
fRevertButton->SetEnabled(false);
float buttonHeight = fRevertButton->Bounds().Height();
fRevertButton->MoveBy(0, -buttonHeight);
AddChild(fRevertButton);
ResizeTo(fClock->Frame().right + 10.0, fGmtTime->Frame().bottom + 10.0); ResizeTo(fClock->Frame().right + 10.0, fGmtTime->Frame().bottom + 10.0);
} }
+12
View File
@@ -6,6 +6,7 @@
* Andrew McCall <mccall@@digitalparadise.co.uk> * Andrew McCall <mccall@@digitalparadise.co.uk>
* Mike Berg <[email protected]> * Mike Berg <[email protected]>
* Julun <[email protected]> * Julun <[email protected]>
* Philippe Saint-Pierre <[email protected]>
*/ */
#ifndef DATE_TIME_VIEW_H #ifndef DATE_TIME_VIEW_H
#define DATE_TIME_VIEW_H #define DATE_TIME_VIEW_H
@@ -19,6 +20,7 @@ class TTimeEdit;
class BCalendarView; class BCalendarView;
class BRadioButton; class BRadioButton;
class TAnalogClock; class TAnalogClock;
class BButton;
class DateTimeView : public BView { class DateTimeView : public BView {
@@ -30,12 +32,16 @@ class DateTimeView : public BView {
virtual void Draw(BRect updaterect); virtual void Draw(BRect updaterect);
virtual void MessageReceived(BMessage *message); virtual void MessageReceived(BMessage *message);
void CheckCanRevert();
private: private:
void _InitView(); void _InitView();
void _ReadRTCSettings(); void _ReadRTCSettings();
void _WriteRTCSettings(); void _WriteRTCSettings();
void _UpdateGmtSettings(); void _UpdateGmtSettings();
void _UpdateDateTime(BMessage *message); void _UpdateDateTime(BMessage *message);
void _Revert();
time_t _PrefletUptime() const;
private: private:
BRadioButton *fLocalTime; BRadioButton *fLocalTime;
@@ -45,8 +51,14 @@ class DateTimeView : public BView {
BCalendarView *fCalendarView; BCalendarView *fCalendarView;
TAnalogClock *fClock; TAnalogClock *fClock;
BButton *fRevertButton;
bool fUseGmtTime; bool fUseGmtTime;
bool fOldUseGmtTime;
bool fInitialized; bool fInitialized;
time_t fTimeAtStart;
bigtime_t fSystemTimeAtStart;
}; };
#endif // DATE_TIME_VIEW_H #endif // DATE_TIME_VIEW_H
+4
View File
@@ -6,6 +6,7 @@
* Andrew McCall, [email protected] * Andrew McCall, [email protected]
* Mike Berg <[email protected]> * Mike Berg <[email protected]>
* Julun <[email protected]> * Julun <[email protected]>
* Philippe Saint-Pierre <[email protected]>
*/ */
#ifndef TIME_MESSAGES_H #ifndef TIME_MESSAGES_H
#define TIME_MESSAGES_H #define TIME_MESSAGES_H
@@ -40,5 +41,8 @@ const uint32 kWeekStart = '_kws';
// clicked on day in calendar // clicked on day in calendar
const uint32 kDayChanged = '_kdc'; const uint32 kDayChanged = '_kdc';
// clicked on revert button
const uint32 kMsgRevert = 'rvrt';
#endif //TIME_MESSAGES_H #endif //TIME_MESSAGES_H
+1
View File
@@ -43,6 +43,7 @@ TTimeWindow::MessageReceived(BMessage *message)
switch(message->what) { switch(message->what) {
case H_USER_CHANGE: case H_USER_CHANGE:
fBaseView->ChangeTime(message); fBaseView->ChangeTime(message);
fDateTimeView->CheckCanRevert();
break; break;
case B_ABOUT_REQUESTED: case B_ABOUT_REQUESTED: