xref: /haiku/src/preferences/time/TimeWindow.cpp (revision 1026b0a1a76dc88927bb8175c470f638dc5464ee)
1 /*
2  * Copyright 2004-2011, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Andrew McCall <mccall@@digitalparadise.co.uk>
7  *		Julun <host.haiku@gmx.de>
8  *		Hamish Morrison <hamish@lavabit.com>
9  */
10 
11 #include "TimeWindow.h"
12 
13 #include <Application.h>
14 #include <Button.h>
15 #include <Catalog.h>
16 #include <LayoutBuilder.h>
17 #include <Message.h>
18 #include <Screen.h>
19 #include <TabView.h>
20 
21 #include "BaseView.h"
22 #include "DateTimeView.h"
23 #include "NetworkTimeView.h"
24 #include "TimeMessages.h"
25 #include "TimeSettings.h"
26 #include "ZoneView.h"
27 
28 
29 #undef B_TRANSLATION_CONTEXT
30 #define B_TRANSLATION_CONTEXT "Time"
31 
32 TTimeWindow::TTimeWindow()
33 	:
34 	BWindow(BRect(0, 0, 0, 0), B_TRANSLATE_SYSTEM_NAME("Time"), B_TITLED_WINDOW,
35 		B_NOT_RESIZABLE | B_NOT_ZOOMABLE | B_AUTO_UPDATE_SIZE_LIMITS)
36 {
37 	_InitWindow();
38 	_AlignWindow();
39 
40 	AddShortcut('A', B_COMMAND_KEY, new BMessage(B_ABOUT_REQUESTED));
41 }
42 
43 
44 TTimeWindow::~TTimeWindow()
45 {
46 }
47 
48 
49 bool
50 TTimeWindow::QuitRequested()
51 {
52 	TimeSettings().SetLeftTop(Frame().LeftTop());
53 
54 	fBaseView->StopWatchingAll(fTimeZoneView);
55 	fBaseView->StopWatchingAll(fDateTimeView);
56 
57 	be_app->PostMessage(B_QUIT_REQUESTED);
58 
59 	return BWindow::QuitRequested();
60 }
61 
62 
63 void
64 TTimeWindow::MessageReceived(BMessage* message)
65 {
66 	switch(message->what) {
67 		case H_USER_CHANGE:
68 			fBaseView->ChangeTime(message);
69 			// To make sure no old time message is in the queue
70 			_SendTimeChangeFinished();
71 			_SetRevertStatus();
72 			break;
73 
74 		case B_ABOUT_REQUESTED:
75 			be_app->PostMessage(B_ABOUT_REQUESTED);
76 			break;
77 
78 		case kMsgRevert:
79 			fDateTimeView->MessageReceived(message);
80 			fTimeZoneView->MessageReceived(message);
81 			fNetworkTimeView->MessageReceived(message);
82 			fRevertButton->SetEnabled(false);
83 			break;
84 
85 		case kRTCUpdate:
86 			fDateTimeView->MessageReceived(message);
87 			fTimeZoneView->MessageReceived(message);
88 			_SetRevertStatus();
89 			break;
90 
91 		case kMsgChange:
92 			_SetRevertStatus();
93 			break;
94 
95 		default:
96 			BWindow::MessageReceived(message);
97 			break;
98 	}
99 }
100 
101 
102 void
103 TTimeWindow::_InitWindow()
104 {
105 	SetPulseRate(500000);
106 
107 	fDateTimeView = new DateTimeView(B_TRANSLATE("Date and time"));
108 	fTimeZoneView = new TimeZoneView(B_TRANSLATE("Time zone"));
109 	fNetworkTimeView = new NetworkTimeView(B_TRANSLATE("Network time"));
110 
111 	fBaseView = new TTimeBaseView("baseView");
112 	fBaseView->StartWatchingAll(fDateTimeView);
113 	fBaseView->StartWatchingAll(fTimeZoneView);
114 
115 	BTabView* tabView = new BTabView("tabView");
116 	tabView->AddTab(fDateTimeView);
117 	tabView->AddTab(fTimeZoneView);
118 	tabView->AddTab(fNetworkTimeView);
119 
120 	fBaseView->AddChild(tabView);
121 
122 	fRevertButton = new BButton("revert", B_TRANSLATE("Revert"),
123 		new BMessage(kMsgRevert));
124 	fRevertButton->SetEnabled(false);
125 	fRevertButton->SetTarget(this);
126 	fRevertButton->SetExplicitAlignment(
127 		BAlignment(B_ALIGN_LEFT, B_ALIGN_MIDDLE));
128 
129 	BLayoutBuilder::Group<>(this, B_VERTICAL, 5)
130 		.Add(fBaseView)
131 		.Add(fRevertButton)
132 		.SetInsets(5, 5, 5, 5);
133 }
134 
135 
136 void
137 TTimeWindow::_AlignWindow()
138 {
139 	BPoint pt = TimeSettings().LeftTop();
140 	MoveTo(pt);
141 
142 	BRect frame = Frame();
143 	BRect screen = BScreen().Frame();
144 	if (!frame.Intersects(screen.InsetByCopy(50.0, 50.0))) {
145 		BRect bounds(Bounds());
146 		BPoint leftTop((screen.Width() - bounds.Width()) / 2.0,
147 			(screen.Height() - bounds.Height()) / 2.0);
148 
149 		MoveTo(leftTop);
150 	}
151 }
152 
153 
154 void
155 TTimeWindow::_SendTimeChangeFinished()
156 {
157 	BMessenger messenger(fDateTimeView);
158 	BMessage msg(kChangeTimeFinished);
159 	messenger.SendMessage(&msg);
160 }
161 
162 
163 void
164 TTimeWindow::_SetRevertStatus()
165 {
166 	fRevertButton->SetEnabled(fDateTimeView->CheckCanRevert()
167 		|| fTimeZoneView->CheckCanRevert()
168 		|| fNetworkTimeView->CheckCanRevert());
169 }
170