xref: /haiku/src/preferences/time/TimeWindow.cpp (revision b46615c55ad2c8fe6de54412055a0713da3d610a)
1 /*
2  * Copyright 2004-2007, 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  *
9  */
10 
11 #include "TimeWindow.h"
12 #include "BaseView.h"
13 #include "DateTimeView.h"
14 #include "TimeMessages.h"
15 #include "TimeSettings.h"
16 #include "ZoneView.h"
17 
18 
19 #include <Application.h>
20 #include <Catalog.h>
21 #include <Message.h>
22 #include <Screen.h>
23 #include <TabView.h>
24 #include <Button.h>
25 
26 #undef B_TRANSLATE_CONTEXT
27 #define B_TRANSLATE_CONTEXT "Time"
28 
29 TTimeWindow::TTimeWindow(BRect rect)
30 	:
31 	BWindow(rect, B_TRANSLATE_SYSTEM_NAME("Time"), B_TITLED_WINDOW,
32 		B_NOT_RESIZABLE | B_NOT_ZOOMABLE)
33 {
34 	_InitWindow();
35 	_AlignWindow();
36 
37 	AddShortcut('A', B_COMMAND_KEY, new BMessage(B_ABOUT_REQUESTED));
38 }
39 
40 
41 TTimeWindow::~TTimeWindow()
42 {
43 }
44 
45 
46 void
47 TTimeWindow::SetRevertStatus()
48 {
49 	fRevertButton->SetEnabled(fDateTimeView->CheckCanRevert()
50 		|| fTimeZoneView->CheckCanRevert());
51 }
52 
53 
54 void
55 TTimeWindow::MessageReceived(BMessage* message)
56 {
57 	switch(message->what) {
58 		case H_USER_CHANGE:
59 			fBaseView->ChangeTime(message);
60 			// To make sure no old time message is in the queue
61 			_SendTimeChangeFinished();
62 			SetRevertStatus();
63 			break;
64 
65 		case B_ABOUT_REQUESTED:
66 			be_app->PostMessage(B_ABOUT_REQUESTED);
67 			break;
68 
69 		case kMsgRevert:
70 			fDateTimeView->MessageReceived(message);
71 			fTimeZoneView->MessageReceived(message);
72 			fRevertButton->SetEnabled(false);
73 			break;
74 
75 		case kRTCUpdate:
76 			fDateTimeView->MessageReceived(message);
77 			fTimeZoneView->MessageReceived(message);
78 			SetRevertStatus();
79 			break;
80 
81 		default:
82 			BWindow::MessageReceived(message);
83 			break;
84 	}
85 }
86 
87 
88 bool
89 TTimeWindow::QuitRequested()
90 {
91 	TimeSettings().SetLeftTop(Frame().LeftTop());
92 
93 	fBaseView->StopWatchingAll(fTimeZoneView);
94 	fBaseView->StopWatchingAll(fDateTimeView);
95 
96 	be_app->PostMessage(B_QUIT_REQUESTED);
97 
98 	return BWindow::QuitRequested();
99 }
100 
101 
102 void
103 TTimeWindow::_InitWindow()
104 {
105 	SetPulseRate(500000);
106 
107 	fDateTimeView = new DateTimeView(Bounds());
108 
109 	BRect bounds = fDateTimeView->Bounds();
110 	fTimeZoneView = new TimeZoneView(bounds);
111 
112 	fBaseView = new TTimeBaseView(bounds, "baseView");
113 	AddChild(fBaseView);
114 
115 	fBaseView->StartWatchingAll(fDateTimeView);
116 	fBaseView->StartWatchingAll(fTimeZoneView);
117 
118 	bounds.OffsetBy(10.0, 10.0);
119 	BTabView* tabView = new BTabView(bounds.InsetByCopy(-5.0, -5.0),
120 		"tabView" , B_WIDTH_AS_USUAL, B_FOLLOW_NONE);
121 
122 	BTab* tab = new BTab();
123 	tabView->AddTab(fDateTimeView, tab);
124 	tab->SetLabel(B_TRANSLATE("Date & Time"));
125 
126 	tab = new BTab();
127 	tabView->AddTab(fTimeZoneView, tab);
128 	tab->SetLabel(B_TRANSLATE("Time zone"));
129 
130 	fBaseView->AddChild(tabView);
131 	tabView->ResizeBy(0.0, tabView->TabHeight());
132 
133 	BRect rect = Bounds();
134 
135 	rect.left = 10;
136 	rect.top = rect.bottom - 10;
137 
138 	fRevertButton = new BButton(rect, "revert", B_TRANSLATE("Revert"),
139 		new BMessage(kMsgRevert), B_FOLLOW_LEFT | B_FOLLOW_BOTTOM, B_WILL_DRAW);
140 
141 	fRevertButton->ResizeToPreferred();
142 	fRevertButton->SetEnabled(false);
143 	float buttonHeight = fRevertButton->Bounds().Height();
144 	fRevertButton->MoveBy(0, -buttonHeight);
145 	fBaseView->AddChild(fRevertButton);
146 	fRevertButton->SetTarget(this);
147 
148 	fBaseView->ResizeTo(tabView->Bounds().Width() + 10.0,
149 		tabView->Bounds().Height() + buttonHeight + 30.0);
150 
151 	ResizeTo(fBaseView->Bounds().Width(), fBaseView->Bounds().Height());
152 }
153 
154 
155 void
156 TTimeWindow::_AlignWindow()
157 {
158 	BPoint pt = TimeSettings().LeftTop();
159 	MoveTo(pt);
160 
161 	BRect frame = Frame();
162 	BRect screen = BScreen().Frame();
163 	if (!frame.Intersects(screen.InsetByCopy(50.0, 50.0))) {
164 		BRect bounds(Bounds());
165 		BPoint leftTop((screen.Width() - bounds.Width()) / 2.0,
166 			(screen.Height() - bounds.Height()) / 2.0);
167 
168 		MoveTo(leftTop);
169 	}
170 }
171 
172 
173 void
174 TTimeWindow::_SendTimeChangeFinished()
175 {
176 	BMessenger messenger(fDateTimeView);
177 	BMessage msg(kChangeTimeFinished);
178 	messenger.SendMessage(&msg);
179 }
180