xref: /haiku/src/preferences/screen/ScreenSettings.cpp (revision d3d8b26997fac34a84981e6d2b649521de2cc45a)
1 /*
2  * Copyright 2001-2006, Haiku.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Rafael Romo
7  *		Stefano Ceccherini (burton666@libero.it)
8  *		Axel Dörfler, axeld@pinc-software.de
9  */
10 
11 
12 #include <StorageKit.h>
13 #include <Screen.h>
14 
15 #include "ScreenSettings.h"
16 
17 
18 static const char* kSettingsFileName = "Screen_data";
19 
20 
21 ScreenSettings::ScreenSettings()
22 {
23 	BScreen screen(B_MAIN_SCREEN_ID);
24 	BRect screenFrame = screen.Frame();
25 
26 	fWindowFrame.Set(0, 0, 356, 202);
27 
28 	BPath path;
29 	if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) == B_OK) {
30 		path.Append(kSettingsFileName);
31 
32 		BFile file(path.Path(), B_READ_ONLY);
33 		if (file.InitCheck() == B_OK) {
34 			BPoint point;
35 			file.Read(&point, sizeof(BPoint));
36 			fWindowFrame.OffsetTo(point);
37 
38 			// make sure the window is visible on screen
39 			if (screenFrame.right >= fWindowFrame.left + 40
40 				&& screenFrame.bottom >= fWindowFrame.top + 40
41 				&& screenFrame.left <= fWindowFrame.right - 40
42 				&& screenFrame.top <= fWindowFrame.bottom - 40)
43 				return;
44 		}
45 	}
46 
47 	fWindowFrame.OffsetTo(screenFrame.left + (screenFrame.Width() - fWindowFrame.Width()) / 2,
48 		screenFrame.top + (screenFrame.Height() - fWindowFrame.Height()) /2);
49 }
50 
51 
52 ScreenSettings::~ScreenSettings()
53 {
54 	BPath path;
55 	if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) < B_OK)
56 		return;
57 
58 	path.Append(kSettingsFileName);
59 
60 	BFile file(path.Path(), B_WRITE_ONLY | B_CREATE_FILE);
61 	if (file.InitCheck() == B_OK) {
62 		BPoint point(fWindowFrame.LeftTop());
63 		file.Write(&point, sizeof(BPoint));
64 	}
65 }
66 
67 
68 void
69 ScreenSettings::SetWindowFrame(BRect frame)
70 {
71 	fWindowFrame = frame;
72 }
73