xref: /haiku/src/preferences/time/TimeWindow.cpp (revision 447b7fded10b84c4c072ef1416cdd49b83702543)
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 <Message.h>
21 #include <Screen.h>
22 #include <TabView.h>
23 #include <Button.h>
24 
25 
26 TTimeWindow::TTimeWindow(BRect rect)
27 	: BWindow(rect, "Time", B_TITLED_WINDOW, B_NOT_RESIZABLE | B_NOT_ZOOMABLE)
28 {
29 	_InitWindow();
30 	_AlignWindow();
31 
32 	AddShortcut('A', B_COMMAND_KEY, new BMessage(B_ABOUT_REQUESTED));
33 }
34 
35 
36 TTimeWindow::~TTimeWindow()
37 {
38 }
39 
40 
41 void
42 TTimeWindow::SetRevertStatus()
43 {
44 	fRevertButton->SetEnabled(fDateTimeView->CheckCanRevert()
45 		|| fTimeZoneView->CheckCanRevert());
46 }
47 
48 
49 void
50 TTimeWindow::MessageReceived(BMessage* message)
51 {
52 	switch(message->what) {
53 		case H_USER_CHANGE:
54 			fBaseView->ChangeTime(message);
55 			// To make sure no old time message is in the queue
56 			_SendTimeChangeFinished();
57 			SetRevertStatus();
58 			break;
59 
60 		case B_ABOUT_REQUESTED:
61 			be_app->PostMessage(B_ABOUT_REQUESTED);
62 			break;
63 
64 		case kMsgRevert:
65 			fDateTimeView->MessageReceived(message);
66 			fTimeZoneView->MessageReceived(message);
67 			fRevertButton->SetEnabled(false);
68 			break;
69 
70 		case kRTCUpdate:
71 			fDateTimeView->MessageReceived(message);
72 			fTimeZoneView->MessageReceived(message);
73 			SetRevertStatus();
74 			break;
75 
76 		default:
77 			BWindow::MessageReceived(message);
78 			break;
79 	}
80 }
81 
82 
83 bool
84 TTimeWindow::QuitRequested()
85 {
86 	TimeSettings().SetLeftTop(Frame().LeftTop());
87 
88 	fBaseView->StopWatchingAll(fTimeZoneView);
89 	fBaseView->StopWatchingAll(fDateTimeView);
90 
91 	be_app->PostMessage(B_QUIT_REQUESTED);
92 
93 	return BWindow::QuitRequested();
94 }
95 
96 
97 void
98 TTimeWindow::_InitWindow()
99 {
100 	SetPulseRate(500000);
101 
102 	fDateTimeView = new DateTimeView(Bounds());
103 
104 	BRect bounds = fDateTimeView->Bounds();
105 	fTimeZoneView = new TimeZoneView(bounds);
106 
107 	fBaseView = new TTimeBaseView(bounds, "baseView");
108 	AddChild(fBaseView);
109 
110 	fBaseView->StartWatchingAll(fDateTimeView);
111 	fBaseView->StartWatchingAll(fTimeZoneView);
112 
113 	bounds.OffsetBy(10.0, 10.0);
114 	BTabView* tabView = new BTabView(bounds.InsetByCopy(-5.0, -5.0),
115 		"tabView" , B_WIDTH_AS_USUAL, B_FOLLOW_NONE);
116 
117 	BTab* tab = new BTab();
118 	tabView->AddTab(fDateTimeView, tab);
119 	tab->SetLabel("Date & Time");
120 
121 	tab = new BTab();
122 	tabView->AddTab(fTimeZoneView, tab);
123 	tab->SetLabel("Time zone");
124 
125 	fBaseView->AddChild(tabView);
126 	tabView->ResizeBy(0.0, tabView->TabHeight());
127 
128 	BRect rect = Bounds();
129 
130 	rect.left = 10;
131 	rect.top = rect.bottom - 10;
132 
133 	fRevertButton = new BButton(rect, "revert", "Revert",
134 		new BMessage(kMsgRevert), B_FOLLOW_LEFT | B_FOLLOW_BOTTOM, B_WILL_DRAW);
135 
136 	fRevertButton->ResizeToPreferred();
137 	fRevertButton->SetEnabled(false);
138 	float buttonHeight = fRevertButton->Bounds().Height();
139 	fRevertButton->MoveBy(0, -buttonHeight);
140 	fBaseView->AddChild(fRevertButton);
141 	fRevertButton->SetTarget(this);
142 
143 	fBaseView->ResizeTo(tabView->Bounds().Width() + 10.0,
144 		tabView->Bounds().Height() + buttonHeight + 30.0);
145 
146 	ResizeTo(fBaseView->Bounds().Width(), fBaseView->Bounds().Height());
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