xref: /haiku/src/preferences/time/TimeWindow.cpp (revision 239222b2369c39dc52df52b0a7cdd6cc0a91bc92)
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 			SetRevertStatus();
73 			break;
74 
75 		default:
76 			BWindow::MessageReceived(message);
77 			break;
78 	}
79 }
80 
81 
82 bool
83 TTimeWindow::QuitRequested()
84 {
85 	TimeSettings().SetLeftTop(Frame().LeftTop());
86 
87 	fBaseView->StopWatchingAll(fTimeZoneView);
88 	fBaseView->StopWatchingAll(fDateTimeView);
89 
90 	be_app->PostMessage(B_QUIT_REQUESTED);
91 
92 	return BWindow::QuitRequested();
93 }
94 
95 
96 void
97 TTimeWindow::_InitWindow()
98 {
99 	SetPulseRate(500000);
100 
101 	fDateTimeView = new DateTimeView(Bounds());
102 
103 	BRect bounds = fDateTimeView->Bounds();
104 	fTimeZoneView = new TimeZoneView(bounds);
105 
106 	fBaseView = new TTimeBaseView(bounds, "baseView");
107 	AddChild(fBaseView);
108 
109 	fBaseView->StartWatchingAll(fDateTimeView);
110 	fBaseView->StartWatchingAll(fTimeZoneView);
111 
112 	bounds.OffsetBy(10.0, 10.0);
113 	BTabView *tabView = new BTabView(bounds.InsetByCopy(-5.0, -5.0),
114 		"tabView" , B_WIDTH_AS_USUAL, B_FOLLOW_NONE);
115 
116 	BTab *tab = new BTab();
117 	tabView->AddTab(fDateTimeView, tab);
118 	tab->SetLabel("Date & Time");
119 
120 	tab = new BTab();
121 	tabView->AddTab(fTimeZoneView, tab);
122 	tab->SetLabel("Timezone");
123 
124 	fBaseView->AddChild(tabView);
125 	tabView->ResizeBy(0.0, tabView->TabHeight());
126 
127 	BRect rect = Bounds();
128 
129 	rect.left = 10;
130 	rect.top = rect.bottom - 10;
131 
132 	fRevertButton = new BButton(rect, "revert", "Revert",
133 		new BMessage(kMsgRevert), B_FOLLOW_LEFT | B_FOLLOW_BOTTOM, B_WILL_DRAW);
134 
135 	fRevertButton->ResizeToPreferred();
136 	fRevertButton->SetEnabled(false);
137 	float buttonHeight = fRevertButton->Bounds().Height();
138 	fRevertButton->MoveBy(0, -buttonHeight);
139 	fBaseView->AddChild(fRevertButton);
140 	fRevertButton->SetTarget(this);
141 
142 	fBaseView->ResizeTo(tabView->Bounds().Width() + 10.0,
143 		tabView->Bounds().Height() + buttonHeight + 30.0);
144 
145 	ResizeTo(fBaseView->Bounds().Width(), fBaseView->Bounds().Height());
146 }
147 
148 
149 void
150 TTimeWindow::_AlignWindow()
151 {
152 	BPoint pt = TimeSettings().LeftTop();
153 	MoveTo(pt);
154 
155 	BRect frame = Frame();
156 	BRect screen = BScreen().Frame();
157 	if (!frame.Intersects(screen.InsetByCopy(50.0, 50.0))) {
158 		BRect bounds(Bounds());
159 		BPoint leftTop((screen.Width() - bounds.Width()) / 2.0,
160 			(screen.Height() - bounds.Height()) / 2.0);
161 
162 		MoveTo(leftTop);
163 	}
164 }
165 
166 
167 void
168 TTimeWindow::_SendTimeChangeFinished()
169 {
170 	BMessenger messenger(fDateTimeView);
171 	BMessage msg(kChangeTimeFinished);
172 	messenger.SendMessage(&msg);
173 }
174