xref: /haiku/src/preferences/time/TimeWindow.cpp (revision 1b8f7f13a3dc70e0e903cb94248220b40b732204)
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 #include "TimeWindow.h"
11 #include "BaseView.h"
12 #include "SettingsView.h"
13 #include "TimeMessages.h"
14 #include "ZoneView.h"
15 
16 
17 #include <Application.h>
18 #include <Message.h>
19 #include <Screen.h>
20 #include <TabView.h>
21 
22 
23 #define WINDOW_RIGHT	400
24 #define WINDOW_BOTTOM	227
25 
26 
27 TTimeWindow::TTimeWindow(const BPoint leftTop)
28 	: BWindow(BRect(leftTop, leftTop + BPoint(WINDOW_RIGHT, WINDOW_BOTTOM)),
29 		"Time & Date", B_TITLED_WINDOW, B_NOT_RESIZABLE | B_NOT_ZOOMABLE)
30 {
31 	BRect frame = Frame();
32 	BRect bounds = Bounds();
33 	BRect screenFrame = BScreen().Frame();
34 	// Code to make sure that the window doesn't get drawn off screen...
35 	if (!(screenFrame.right >= frame.right && screenFrame.bottom >= frame.bottom))
36 		MoveTo((screenFrame.right - bounds.right) * 0.5f,
37 			(screenFrame.bottom - bounds.bottom) * 0.5f);
38 
39 	_InitWindow();
40 	SetPulseRate(500000);
41 }
42 
43 
44 void
45 TTimeWindow::MessageReceived(BMessage *message)
46 {
47 	switch(message->what) {
48 		case H_USER_CHANGE:
49 		{
50 			bool isTime;
51 			if (message->FindBool("time", &isTime) == B_OK)
52 				fBaseView->ChangeTime(message);
53 			break;
54 		}
55 
56 		case H_RTC_CHANGE:
57 			fBaseView->SetGMTime(fTimeSettings->GMTime());
58 			break;
59 
60 		default:
61 			BWindow::MessageReceived(message);
62 			break;
63 	}
64 }
65 
66 
67 bool
68 TTimeWindow::QuitRequested()
69 {
70 	BMessage msg(UPDATE_SETTINGS);
71 	msg.AddPoint("LeftTop", Frame().LeftTop());
72 	be_app->PostMessage(&msg);
73 
74 	fBaseView->StopWatchingAll(fTimeSettings);
75 	fBaseView->StopWatchingAll(fTimeZones);
76 
77 	be_app->PostMessage(B_QUIT_REQUESTED);
78 
79 	return BWindow::QuitRequested();
80 }
81 
82 
83 void
84 TTimeWindow::_InitWindow()
85 {
86 	BRect bounds(Bounds());
87 
88 	fBaseView = new TTimeBaseView(bounds, "background view");
89 	AddChild(fBaseView);
90 
91 	bounds.top = 9;
92 	BTabView *tabview = new BTabView(bounds, "tab_view");
93 
94 	bounds = tabview->Bounds();
95 	bounds.InsetBy(4, 6);
96 	bounds.bottom -= tabview->TabHeight();
97 
98 	fTimeSettings = new TSettingsView(bounds);
99 	fBaseView->StartWatchingAll(fTimeSettings);
100 
101 	fTimeZones = new TZoneView(bounds);
102 	fBaseView->StartWatchingAll(fTimeZones);
103 
104 	// add tabs
105 	BTab *tab = new BTab();
106 	tabview->AddTab(fTimeSettings, tab);
107 	tab->SetLabel("Settings");
108 
109 	tab = new BTab();
110 	tabview->AddTab(fTimeZones, tab);
111 	tab->SetLabel("Time Zone");
112 
113 	fBaseView->AddChild(tabview);
114 
115 	float width;
116 	float height;
117 	fTimeSettings->GetPreferredSize(&width, &height);
118 	// width/ height from settingsview + all InsetBy etc..
119 	ResizeTo(width +5, height + tabview->TabHeight() +25);
120 }
121 
122