xref: /haiku/src/preferences/time/TimeWindow.cpp (revision 21258e2674226d6aa732321b6f8494841895af5f)
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 <LocaleRoster.h>
18 #include <Message.h>
19 #include <Screen.h>
20 #include <SeparatorView.h>
21 #include <TabView.h>
22 
23 #include "BaseView.h"
24 #include "ClockView.h"
25 #include "DateTimeView.h"
26 #include "NetworkTimeView.h"
27 #include "TimeMessages.h"
28 #include "TimeSettings.h"
29 #include "ZoneView.h"
30 
31 
32 #undef B_TRANSLATION_CONTEXT
33 #define B_TRANSLATION_CONTEXT "Time"
34 
35 TTimeWindow::TTimeWindow()
36 	:
37 	BWindow(BRect(0, 0, 0, 0), B_TRANSLATE_SYSTEM_NAME("Time"), B_TITLED_WINDOW,
38 		B_NOT_RESIZABLE | B_NOT_ZOOMABLE | B_AUTO_UPDATE_SIZE_LIMITS)
39 {
40 	_InitWindow();
41 	_AlignWindow();
42 
43 	AddShortcut('A', B_COMMAND_KEY, new BMessage(B_ABOUT_REQUESTED));
44 }
45 
46 
47 TTimeWindow::~TTimeWindow()
48 {
49 }
50 
51 
52 bool
53 TTimeWindow::QuitRequested()
54 {
55 	TimeSettings().SetLeftTop(Frame().LeftTop());
56 
57 	fBaseView->StopWatchingAll(fTimeZoneView);
58 	fBaseView->StopWatchingAll(fDateTimeView);
59 
60 	be_app->PostMessage(B_QUIT_REQUESTED);
61 
62 	return BWindow::QuitRequested();
63 }
64 
65 
66 void
67 TTimeWindow::MessageReceived(BMessage* message)
68 {
69 	switch(message->what) {
70 		case H_USER_CHANGE:
71 			fBaseView->ChangeTime(message);
72 			// To make sure no old time message is in the queue
73 			_SendTimeChangeFinished();
74 			_SetRevertStatus();
75 			break;
76 
77 		case B_ABOUT_REQUESTED:
78 			be_app->PostMessage(B_ABOUT_REQUESTED);
79 			break;
80 
81 		case B_LOCALE_CHANGED:
82 		{
83 			BLocaleRoster::Default()->Refresh();
84 			fDateTimeView->MessageReceived(message);
85 			break;
86 		}
87 
88 		case kMsgRevert:
89 			fDateTimeView->MessageReceived(message);
90 			fTimeZoneView->MessageReceived(message);
91 			fNetworkTimeView->MessageReceived(message);
92 			fClockView->MessageReceived(message);
93 			fRevertButton->SetEnabled(false);
94 			break;
95 
96 		case kRTCUpdate:
97 			fDateTimeView->MessageReceived(message);
98 			fTimeZoneView->MessageReceived(message);
99 			_SetRevertStatus();
100 			break;
101 
102 		case kMsgChange:
103 			_SetRevertStatus();
104 			break;
105 
106 		case kSelectClockTab:
107 			// focus the clock tab (last one)
108 			fTabView->Select(fTabView->CountTabs() - 1);
109 			break;
110 
111 		case kShowHideTime:
112 			fClockView->MessageReceived(message);
113 			break;
114 
115 		default:
116 			BWindow::MessageReceived(message);
117 			break;
118 	}
119 }
120 
121 
122 void
123 TTimeWindow::_InitWindow()
124 {
125 	SetPulseRate(500000);
126 
127 	fDateTimeView = new DateTimeView(B_TRANSLATE("Date and time"));
128 	fTimeZoneView = new TimeZoneView(B_TRANSLATE("Time zone"));
129 	fNetworkTimeView = new NetworkTimeView(B_TRANSLATE("Network time"));
130 	fClockView = new ClockView(B_TRANSLATE("Clock"));
131 
132 	fBaseView = new TTimeBaseView("baseView");
133 	fBaseView->StartWatchingAll(fDateTimeView);
134 	fBaseView->StartWatchingAll(fTimeZoneView);
135 
136 	fTabView = new BTabView("tabView", B_WIDTH_FROM_WIDEST);
137 	fTabView->AddTab(fDateTimeView);
138 	fTabView->AddTab(fTimeZoneView);
139 	fTabView->AddTab(fNetworkTimeView);
140 	fTabView->AddTab(fClockView);
141 	fTabView->SetBorder(B_NO_BORDER);
142 
143 	fBaseView->AddChild(fTabView);
144 
145 	fRevertButton = new BButton("revert", B_TRANSLATE("Revert"),
146 		new BMessage(kMsgRevert));
147 	fRevertButton->SetEnabled(false);
148 	fRevertButton->SetTarget(this);
149 	fRevertButton->SetExplicitAlignment(
150 		BAlignment(B_ALIGN_LEFT, B_ALIGN_MIDDLE));
151 
152 	BLayoutBuilder::Group<>(this, B_VERTICAL, 0)
153 		.SetInsets(0, B_USE_DEFAULT_SPACING, 0, 0)
154 		.Add(fBaseView)
155 		.Add(new BSeparatorView(B_HORIZONTAL))
156 		.AddGroup(B_HORIZONTAL)
157 			.Add(fRevertButton)
158 			.SetInsets(B_USE_WINDOW_SPACING, B_USE_DEFAULT_SPACING,
159 				B_USE_DEFAULT_SPACING, B_USE_WINDOW_SPACING);
160 }
161 
162 
163 void
164 TTimeWindow::_AlignWindow()
165 {
166 	MoveTo(TimeSettings().LeftTop());
167 	MoveOnScreen();
168 }
169 
170 
171 void
172 TTimeWindow::_SendTimeChangeFinished()
173 {
174 	BMessenger messenger(fDateTimeView);
175 	BMessage msg(kChangeTimeFinished);
176 	messenger.SendMessage(&msg);
177 }
178 
179 
180 void
181 TTimeWindow::_SetRevertStatus()
182 {
183 	fRevertButton->SetEnabled(fDateTimeView->CheckCanRevert()
184 		|| fTimeZoneView->CheckCanRevert()
185 		|| fNetworkTimeView->CheckCanRevert()
186 		|| fClockView->CheckCanRevert());
187 }
188