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