xref: /haiku/src/apps/clock/cl_wind.cpp (revision dd8cf8ddd67f38468fbbb4a2ca197a659a436e34)
1 /*
2  *	Copyright 1999, Be Incorporated. All Rights Reserved.
3  *	This file may be used under the terms of the Be Sample Code License.
4  */
5 
6 #include "cl_wind.h"
7 #include "cl_view.h"
8 
9 #include <Application.h>
10 #include <FindDirectory.h>
11 #include <Path.h>
12 #include <Screen.h>
13 
14 
15 #include <fcntl.h>
16 #include <unistd.h>
17 #include <sys/stat.h>
18 
19 
TClockWindow(BRect frame,const char * title)20 TClockWindow::TClockWindow(BRect frame, const char* title)
21 	: BWindow(frame, title, B_TITLED_WINDOW,
22 		B_NOT_RESIZABLE | B_NOT_ZOOMABLE | B_AVOID_FRONT, B_ALL_WORKSPACES),
23 	  fOnScreenView(NULL)
24 {
25 	_InitWindow();
26 }
27 
28 
~TClockWindow()29 TClockWindow::~TClockWindow()
30 {
31 }
32 
33 
34 bool
QuitRequested()35 TClockWindow::QuitRequested()
36 {
37 	BPath path;
38 	if (find_directory (B_USER_SETTINGS_DIRECTORY, &path, true) == B_OK) {
39 		path.Append("Clock_settings");
40 		int ref = creat(path.Path(), 0777);
41 		if (ref >= 0) {
42 			BPoint lefttop = Frame().LeftTop();
43 			write(ref, (char *)&lefttop, sizeof(BPoint));
44 			short face = fOnScreenView->ReturnFace();
45 			write(ref, (char *)&face, sizeof(short));
46 			bool seconds = fOnScreenView->ReturnSeconds();
47 			write(ref, (char *)&seconds, sizeof(bool));
48 			close(ref);
49 		}
50 	}
51 	be_app->PostMessage(B_QUIT_REQUESTED);
52 	return true;
53 }
54 
55 
56 void
_InitWindow()57 TClockWindow::_InitWindow()
58 {
59 	// half second pulse rate
60 	SetPulseRate(500000);
61 
62 	fOnScreenView = new TOnscreenView(BRect(0, 0, 82, 82), "Clock", 22, 15, 41);
63 	AddChild(fOnScreenView);
64 
65 	int ref;
66 	BPath path;
67 	if (find_directory (B_USER_SETTINGS_DIRECTORY, &path) == B_OK) {
68 		path.Append("Clock_settings");
69 		ref = open(path.Path(), O_RDONLY);
70 		if (ref >= 0) {
71 			BPoint leftTop;
72 			read(ref, (char*)&leftTop, sizeof(leftTop));
73 
74 			short face;
75 			read(ref, (char *)&face, sizeof(short));
76 			fOnScreenView->UseFace(face);
77 
78 			bool secs;
79 			read(ref, (char *)&secs, sizeof(bool));
80 			fOnScreenView->ShowSecs(secs);
81 
82 			close(ref);
83 
84 			MoveTo(leftTop);
85 
86 			BRect frame = Frame();
87 			frame.InsetBy(-4, -4);
88 			// it's not visible so reposition. I'm not going to get
89 			// fancy here, just place in the default location
90 			if (!frame.Intersects(BScreen(this).Frame()))
91 				MoveTo(100, 100);
92 		}
93 	}
94 }
95 
96