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