xref: /haiku/src/apps/clock/cl_wind.cpp (revision dd8cf8ddd67f38468fbbb4a2ca197a659a436e34)
152a38012Sejakowatz /*
20064e641SStephan Aßmus  *	Copyright 1999, Be Incorporated. All Rights Reserved.
30064e641SStephan Aßmus  *	This file may be used under the terms of the Be Sample Code License.
452a38012Sejakowatz  */
552a38012Sejakowatz 
652a38012Sejakowatz #include "cl_wind.h"
70c13f74dSKarsten Heimrich #include "cl_view.h"
80064e641SStephan Aßmus 
90064e641SStephan Aßmus #include <Application.h>
1052a38012Sejakowatz #include <FindDirectory.h>
1152a38012Sejakowatz #include <Path.h>
120c13f74dSKarsten Heimrich #include <Screen.h>
130c13f74dSKarsten Heimrich 
140c13f74dSKarsten Heimrich 
1552a38012Sejakowatz #include <fcntl.h>
16*dd8cf8ddSStephan Aßmus #include <unistd.h>
1752a38012Sejakowatz #include <sys/stat.h>
1852a38012Sejakowatz 
190064e641SStephan Aßmus 
TClockWindow(BRect frame,const char * title)200064e641SStephan Aßmus TClockWindow::TClockWindow(BRect frame, const char* title)
210064e641SStephan Aßmus 	: BWindow(frame, title, B_TITLED_WINDOW,
220c13f74dSKarsten Heimrich 		B_NOT_RESIZABLE | B_NOT_ZOOMABLE | B_AVOID_FRONT, B_ALL_WORKSPACES),
230c13f74dSKarsten Heimrich 	  fOnScreenView(NULL)
2452a38012Sejakowatz {
250c13f74dSKarsten Heimrich 	_InitWindow();
2652a38012Sejakowatz }
2752a38012Sejakowatz 
280064e641SStephan Aßmus 
~TClockWindow()290064e641SStephan Aßmus TClockWindow::~TClockWindow()
3052a38012Sejakowatz {
310064e641SStephan Aßmus }
320064e641SStephan Aßmus 
330064e641SStephan Aßmus 
340064e641SStephan Aßmus bool
QuitRequested()350064e641SStephan Aßmus TClockWindow::QuitRequested()
360064e641SStephan Aßmus {
3752a38012Sejakowatz 	BPath path;
3852a38012Sejakowatz 	if (find_directory (B_USER_SETTINGS_DIRECTORY, &path, true) == B_OK) {
3952a38012Sejakowatz 		path.Append("Clock_settings");
400064e641SStephan Aßmus 		int ref = creat(path.Path(), 0777);
4152a38012Sejakowatz 		if (ref >= 0) {
420064e641SStephan Aßmus 			BPoint lefttop = Frame().LeftTop();
4352a38012Sejakowatz 			write(ref, (char *)&lefttop, sizeof(BPoint));
440c13f74dSKarsten Heimrich 			short face = fOnScreenView->ReturnFace();
4552a38012Sejakowatz 			write(ref, (char *)&face, sizeof(short));
460c13f74dSKarsten Heimrich 			bool seconds = fOnScreenView->ReturnSeconds();
4752a38012Sejakowatz 			write(ref, (char *)&seconds, sizeof(bool));
4852a38012Sejakowatz 			close(ref);
4952a38012Sejakowatz 		}
5052a38012Sejakowatz 	}
5152a38012Sejakowatz 	be_app->PostMessage(B_QUIT_REQUESTED);
520064e641SStephan Aßmus 	return true;
5352a38012Sejakowatz }
540c13f74dSKarsten Heimrich 
550c13f74dSKarsten Heimrich 
560c13f74dSKarsten Heimrich void
_InitWindow()570c13f74dSKarsten Heimrich TClockWindow::_InitWindow()
580c13f74dSKarsten Heimrich {
590c13f74dSKarsten Heimrich 	// half second pulse rate
600c13f74dSKarsten Heimrich 	SetPulseRate(500000);
610c13f74dSKarsten Heimrich 
620c13f74dSKarsten Heimrich 	fOnScreenView = new TOnscreenView(BRect(0, 0, 82, 82), "Clock", 22, 15, 41);
630c13f74dSKarsten Heimrich 	AddChild(fOnScreenView);
640c13f74dSKarsten Heimrich 
650c13f74dSKarsten Heimrich 	int ref;
660c13f74dSKarsten Heimrich 	BPath path;
670c13f74dSKarsten Heimrich 	if (find_directory (B_USER_SETTINGS_DIRECTORY, &path) == B_OK) {
680c13f74dSKarsten Heimrich 		path.Append("Clock_settings");
690c13f74dSKarsten Heimrich 		ref = open(path.Path(), O_RDONLY);
700c13f74dSKarsten Heimrich 		if (ref >= 0) {
710c13f74dSKarsten Heimrich 			BPoint leftTop;
720c13f74dSKarsten Heimrich 			read(ref, (char*)&leftTop, sizeof(leftTop));
730c13f74dSKarsten Heimrich 
740c13f74dSKarsten Heimrich 			short face;
750c13f74dSKarsten Heimrich 			read(ref, (char *)&face, sizeof(short));
760c13f74dSKarsten Heimrich 			fOnScreenView->UseFace(face);
770c13f74dSKarsten Heimrich 
780c13f74dSKarsten Heimrich 			bool secs;
790c13f74dSKarsten Heimrich 			read(ref, (char *)&secs, sizeof(bool));
800c13f74dSKarsten Heimrich 			fOnScreenView->ShowSecs(secs);
810c13f74dSKarsten Heimrich 
820c13f74dSKarsten Heimrich 			close(ref);
830c13f74dSKarsten Heimrich 
840c13f74dSKarsten Heimrich 			MoveTo(leftTop);
850c13f74dSKarsten Heimrich 
860c13f74dSKarsten Heimrich 			BRect frame = Frame();
870c13f74dSKarsten Heimrich 			frame.InsetBy(-4, -4);
880c13f74dSKarsten Heimrich 			// it's not visible so reposition. I'm not going to get
890c13f74dSKarsten Heimrich 			// fancy here, just place in the default location
900c13f74dSKarsten Heimrich 			if (!frame.Intersects(BScreen(this).Frame()))
910c13f74dSKarsten Heimrich 				MoveTo(100, 100);
920c13f74dSKarsten Heimrich 		}
930c13f74dSKarsten Heimrich 	}
940c13f74dSKarsten Heimrich }
950c13f74dSKarsten Heimrich 
96