3rdparty/licenseReport: Add seperate LGPL checks
[haiku.git] / src / add-ons / screen_savers / haiku / ScreenSaver.cpp
blob4bd9590f2a4a3cb89132819ffff00bf166d4d56c
1 /*
2 **
3 ** A simple screensaver, displays the text "Haiku" at random locations.
4 **
5 ** Version: 3.0
6 **
7 ** Copyright (c) 2002, 2005 Marcus Overhagen. All Rights Reserved.
8 ** This file may be used under the terms of the MIT License.
9 */
12 #include <stdlib.h>
14 #include <Catalog.h>
15 #include <DefaultSettingsView.h>
16 #include <Font.h>
17 #include <ScreenSaver.h>
18 #include <StringView.h>
19 #include <View.h>
22 #undef B_TRANSLATION_CONTEXT
23 #define B_TRANSLATION_CONTEXT "Screensaver Haiku"
26 class ScreenSaver : public BScreenSaver
28 public:
29 ScreenSaver(BMessage *archive, image_id);
30 void Draw(BView *view, int32 frame);
31 void StartConfig(BView *view);
32 status_t StartSaver(BView *view, bool preview);
34 private:
35 const char *fText;
36 float fX;
37 float fY;
38 float fSizeX;
39 float fSizeY;
40 float fTextHeight;
41 float fTextWith;
42 bool fIsPreview;
46 BScreenSaver *instantiate_screen_saver(BMessage *msg, image_id image)
48 return new ScreenSaver(msg, image);
52 ScreenSaver::ScreenSaver(BMessage *archive, image_id id)
53 : BScreenSaver(archive, id)
54 , fText("Haiku")
55 , fX(0)
56 , fY(0)
61 void
62 ScreenSaver::StartConfig(BView *view)
64 BPrivate::BuildDefaultSettingsView(view, "Haiku",
65 B_TRANSLATE("by Marcus Overhagen"));
69 status_t
70 ScreenSaver::StartSaver(BView *view, bool preview)
72 // save view dimensions and preview mode
73 fIsPreview = preview;
74 fSizeX = view->Bounds().Width();
75 fSizeY = view->Bounds().Height();
77 // set a new font, about 1/8th of view height, and bold
78 BFont font;
79 view->GetFont(&font);
80 font.SetSize(fSizeY / 8);
81 font.SetFace(B_BOLD_FACE);
82 view->SetFont(&font);
84 // find out space needed for text display
85 BRect rect;
86 escapement_delta delta;
87 delta.nonspace = 0;
88 delta.space = 0;
89 font.GetBoundingBoxesForStrings(&fText, 1, B_SCREEN_METRIC, &delta, &rect);
90 fTextHeight = rect.Height();
91 fTextWith = rect.Width();
93 // seed the random number generator
94 srand((int)system_time());
96 return B_OK;
100 void
101 ScreenSaver::Draw(BView *view, int32 frame)
103 if (frame == 0) {
104 // fill with black on first frame
105 view->SetLowColor(0, 0, 0);
106 view->FillRect(view->Bounds(), B_SOLID_LOW);
107 } else {
108 // erase old text on all other frames
109 view->SetHighColor(0, 0, 0);
110 view->DrawString(fText, BPoint(fX, fY));
113 // find some new text coordinates
114 fX = rand() % int(fSizeX - fTextWith);
115 fY = rand() % int(fSizeY - fTextHeight - (fIsPreview ? 2 : 20)) + fTextHeight;
117 // draw new text
118 view->SetHighColor(0, 255, 0);
119 view->DrawString(fText, BPoint(fX, fY));
121 // randomize time until next update (preview mode is faster)
122 SetTickSize(((rand() % 4) + 1) * (fIsPreview ? 300000 : 1000000));