1/*
2 * Copyright 2007-2009, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 *		Ryan Leavengood, leavengood@gmail.com
7 */
8
9
10#include <Catalog.h>
11#include <Font.h>
12#include <ScreenSaver.h>
13#include <StringView.h>
14#include <View.h>
15
16#include <BuildScreenSaverDefaultSettingsView.h>
17
18#undef B_TRANSLATION_CONTEXT
19#define B_TRANSLATION_CONTEXT "Screensaver DebugNow"
20
21
22const rgb_color kMediumBlue = {0, 0, 100};
23const rgb_color kWhite = {255, 255, 255};
24
25
26// Inspired by the classic BeOS BuyNow screensaver, of course
27class DebugNow : public BScreenSaver
28{
29	public:
30							DebugNow(BMessage *archive, image_id);
31		void				Draw(BView *view, int32 frame);
32		void				StartConfig(BView *view);
33		status_t			StartSaver(BView *view, bool preview);
34
35	private:
36		const char* 		fLine1;
37		const char*			fLine2;
38		BPoint				fLine1Start;
39		BPoint				fLine2Start;
40		escapement_delta	fDelta;
41};
42
43
44BScreenSaver* instantiate_screen_saver(BMessage *msg, image_id image)
45{
46	return new DebugNow(msg, image);
47}
48
49
50DebugNow::DebugNow(BMessage *archive, image_id id)
51	:
52	BScreenSaver(archive, id),
53	fLine1("DEBUG"),
54	fLine2("NOW")
55{
56}
57
58
59void
60DebugNow::StartConfig(BView *view)
61{
62	BPrivate::BuildScreenSaverDefaultSettingsView(view, "DEBUG NOW",
63		B_TRANSLATE("by Ryan Leavengood"));
64}
65
66
67status_t
68DebugNow::StartSaver(BView *view, bool preview)
69{
70	float viewWidth = view->Bounds().Width();
71	float viewHeight = view->Bounds().Height();
72
73	BFont font;
74	view->GetFont(&font);
75	font.SetSize(viewHeight / 2.5);
76	view->SetFont(&font);
77
78	fDelta.nonspace = 0;
79	fDelta.space = 0;
80	BRect stringRect;
81	font.GetBoundingBoxesForStrings(&fLine1, 1, B_SCREEN_METRIC, &fDelta,
82		&stringRect);
83	float y = ((viewHeight - (stringRect.Height() * 2 + viewHeight / 10)) / 2)
84		+ stringRect.Height();
85	fLine1Start.Set(((viewWidth - stringRect.Width()) / 2) - stringRect.left, y);
86	font.GetBoundingBoxesForStrings(&fLine2, 1, B_SCREEN_METRIC, &fDelta,
87		&stringRect);
88	fLine2Start.Set(((viewWidth - stringRect.Width()) / 2) - stringRect.left,
89		y + stringRect.Height() + viewHeight / 10);
90
91	// Set tick size to 500,000 microseconds = 0.5 second
92	SetTickSize(500000);
93
94	return B_OK;
95}
96
97
98void
99DebugNow::Draw(BView *view, int32 frame)
100{
101	// On first frame set the low color to make the text rendering correct
102	if (frame == 0)
103		view->SetLowColor(kMediumBlue);
104
105	// Draw the background color every frame
106	view->SetHighColor(kMediumBlue);
107	view->FillRect(view->Bounds());
108
109	// Draw the text every other frame to make the it blink
110	if (frame % 2 == 1) {
111		view->SetHighColor(kWhite);
112		view->DrawString(fLine1, fLine1Start, &fDelta);
113		view->DrawString(fLine2, fLine2Start, &fDelta);
114	}
115}
116
117