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