xref: /haiku/src/preferences/screensaver/PreviewView.cpp (revision 2b76973fa2401f7a5edf68e6470f3d3210cbcff3)
1 /*
2  * Copyright 2003-2013 Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Michael Phipps
7  *		Jérôme Duval, jerome.duval@free.fr
8  */
9 
10 
11 #include "PreviewView.h"
12 #include "Utility.h"
13 
14 #include <Point.h>
15 #include <Rect.h>
16 #include <Screen.h>
17 #include <ScreenSaver.h>
18 #include <Shape.h>
19 #include <String.h>
20 
21 #include <iostream>
22 
23 
24 static float sampleX[]
25 	= { 0, 0.05, 0.15, 0.7, 0.725, 0.8, 0.825, 0.85, 0.950, 1.0 };
26 static float sampleY[] = { 0, 0.05, 0.90, 0.95, 0.966, 0.975, 1.0 };
27 
28 
29 inline BPoint
30 scale2(int x, int y, BRect area)
31 {
32 	return scale_direct(sampleX[x], sampleY[y], area);
33 }
34 
35 
36 inline BRect
37 scale2(int x1, int x2, int y1, int y2, BRect area)
38 {
39 	return scale_direct(sampleX[x1], sampleX[x2], sampleY[y1], sampleY[y2],
40 		area);
41 }
42 
43 
44 PreviewView::PreviewView(const char* name)
45 	:
46 	BView(name, B_WILL_DRAW),
47 	fSaverView(NULL)
48 {
49 	SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
50 
51 	float aspectRatio = 4.0f / 3.0f;
52 		// 4:3 monitor
53 	float previewWidth = 160.0f;
54 	float previewHeight = ceilf(previewWidth / aspectRatio);
55 
56 	SetExplicitSize(BSize(previewWidth, previewHeight));
57 }
58 
59 
60 PreviewView::~PreviewView()
61 {
62 }
63 
64 
65 void
66 PreviewView::Draw(BRect update)
67 {
68 	SetHighColor(184, 184, 184);
69 	FillRoundRect(scale2(0, 9, 0, 3, Bounds()), 4, 4);
70 		// Outer shape
71 	FillRoundRect(scale2(2, 7, 3, 6, Bounds()), 2, 2);
72 		// control console outline
73 
74 	SetHighColor(96, 96, 96);
75 	StrokeRoundRect(scale2(2, 7, 3, 6, Bounds()), 2, 2);
76 		// control console outline
77 	StrokeRoundRect(scale2(0, 9, 0, 3, Bounds()), 4, 4);
78 		// Outline outer shape
79 
80 	SetHighColor(0, 0, 0);
81 	FillRect(scale2(1, 8, 1, 2, Bounds()));
82 
83 	SetHighColor(184, 184, 184);
84 	BRect outerShape = scale2(2, 7, 2, 6, Bounds());
85 	outerShape.InsetBy(1, 1);
86 	FillRoundRect(outerShape, 4, 4);
87 		// Outer shape
88 
89 	SetHighColor(0, 255, 0);
90 	FillRect(scale2(3, 4, 4, 5, Bounds()));
91 	SetHighColor(96, 96, 96);
92 	FillRect(scale2(5, 6, 4, 5, Bounds()));
93 }
94 
95 
96 BView*
97 PreviewView::AddPreview()
98 {
99 	BRect rect = scale2(1, 8, 1, 2, Bounds());
100 	fSaverView = new BView(rect.InsetBySelf(1, 1), "preview", B_FOLLOW_NONE,
101 		B_WILL_DRAW);
102 	fSaverView->SetViewColor(0, 0, 0);
103 	fSaverView->SetLowColor(0, 0, 0);
104 	AddChild(fSaverView);
105 
106 	return fSaverView;
107 }
108 
109 
110 BView*
111 PreviewView::RemovePreview()
112 {
113 	if (fSaverView != NULL)
114 		RemoveChild(fSaverView);
115 
116 	BView* saverView = fSaverView;
117 	fSaverView = NULL;
118 	return saverView;
119 }
120 
121 
122 BView*
123 PreviewView::SaverView()
124 {
125 	return fSaverView;
126 }
127