xref: /haiku/src/preferences/time/TimeWindow.cpp (revision f64b502da072e041cba79dd7d2ae764f10c21246)
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_TRANSLATE_CONTEXT
30 #define B_TRANSLATE_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 		case kMsgClockSettingChanged:
96 		{
97 			bool useGMTTime = true;
98 			message->FindBool("UseGMT", &useGMTTime);
99 			if (useGMTTime) {
100 				BMessage show(H_SHOW_PREVIEW);
101 				fTimeZoneView->MessageReceived(&show);
102 			} else {
103 				BMessage hide(H_HIDE_PREVIEW);
104 				fTimeZoneView->MessageReceived(&hide);
105 			}
106 		}
107 
108 		default:
109 			BWindow::MessageReceived(message);
110 			break;
111 	}
112 }
113 
114 
115 void
116 TTimeWindow::_InitWindow()
117 {
118 	SetPulseRate(500000);
119 
120 	fDateTimeView = new DateTimeView(B_TRANSLATE("Date and time"));
121 	fTimeZoneView = new TimeZoneView(B_TRANSLATE("Time zone"));
122 	fNetworkTimeView = new NetworkTimeView(B_TRANSLATE("Network time"));
123 
124 	fBaseView = new TTimeBaseView("baseView");
125 	fBaseView->StartWatchingAll(fDateTimeView);
126 	fBaseView->StartWatchingAll(fTimeZoneView);
127 
128 	BTabView* tabView = new BTabView("tabView");
129 	tabView->AddTab(fDateTimeView);
130 	tabView->AddTab(fTimeZoneView);
131 	tabView->AddTab(fNetworkTimeView);
132 
133 	fBaseView->AddChild(tabView);
134 
135 	fRevertButton = new BButton("revert", B_TRANSLATE("Revert"),
136 		new BMessage(kMsgRevert));
137 	fRevertButton->SetEnabled(false);
138 	fRevertButton->SetTarget(this);
139 	fRevertButton->SetExplicitAlignment(
140 		BAlignment(B_ALIGN_LEFT, B_ALIGN_MIDDLE));
141 
142 	BLayoutBuilder::Group<>(this, B_VERTICAL, 5)
143 		.Add(fBaseView)
144 		.Add(fRevertButton)
145 		.SetInsets(5, 5, 5, 5);
146 }
147 
148 
149 void
150 TTimeWindow::_AlignWindow()
151 {
152 	BPoint pt = TimeSettings().LeftTop();
153 	MoveTo(pt);
154 
155 	BRect frame = Frame();
156 	BRect screen = BScreen().Frame();
157 	if (!frame.Intersects(screen.InsetByCopy(50.0, 50.0))) {
158 		BRect bounds(Bounds());
159 		BPoint leftTop((screen.Width() - bounds.Width()) / 2.0,
160 			(screen.Height() - bounds.Height()) / 2.0);
161 
162 		MoveTo(leftTop);
163 	}
164 }
165 
166 
167 void
168 TTimeWindow::_SendTimeChangeFinished()
169 {
170 	BMessenger messenger(fDateTimeView);
171 	BMessage msg(kChangeTimeFinished);
172 	messenger.SendMessage(&msg);
173 }
174 
175 
176 void
177 TTimeWindow::_SetRevertStatus()
178 {
179 	fRevertButton->SetEnabled(fDateTimeView->CheckCanRevert()
180 		|| fTimeZoneView->CheckCanRevert()
181 		|| fNetworkTimeView->CheckCanRevert());
182 }
183