xref: /haiku/src/preferences/screensaver/PreviewView.cpp (revision 850f2d1e58cc443f77353c7fc0ce0c158c1fd328)
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 
13 #include <iostream>
14 
15 #include <CardLayout.h>
16 #include <Catalog.h>
17 #include <GroupLayout.h>
18 #include <Point.h>
19 #include <Rect.h>
20 #include <Size.h>
21 #include <StringView.h>
22 #include <TextView.h>
23 
24 #include "Utility.h"
25 
26 
27 static const rgb_color kWhite = (rgb_color){ 255, 255, 255 };
28 
29 
30 #undef B_TRANSLATION_CONTEXT
31 #define B_TRANSLATION_CONTEXT "PreviewView"
32 
33 
34 static float sampleX[]
35 	= { 0, 0.05, 0.15, 0.7, 0.725, 0.8, 0.825, 0.85, 0.950, 1.0 };
36 static float sampleY[] = { 0, 0.05, 0.90, 0.95, 0.966, 0.975, 1.0 };
37 
38 
39 inline BPoint
40 scale2(int x, int y, BRect area)
41 {
42 	return scale_direct(sampleX[x], sampleY[y], area);
43 }
44 
45 
46 inline BRect
47 scale2(int x1, int x2, int y1, int y2, BRect area)
48 {
49 	return scale_direct(sampleX[x1], sampleX[x2], sampleY[y1], sampleY[y2],
50 		area);
51 }
52 
53 
54 //	#pragma mark - PreviewView
55 
56 
57 PreviewView::PreviewView(const char* name)
58 	:
59 	BView(name, B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE),
60 	fSaverView(NULL),
61 	fNoPreview(NULL)
62 {
63 	SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
64 
65 	BGroupLayout* layout = new BGroupLayout(B_VERTICAL);
66 	// We draw the "monitor" around the preview, hence the strange insets.
67 	layout->SetInsets(7, 6, 8, 12);
68 	SetLayout(layout);
69 
70 	// A BStringView would be enough, if only it handled word wrapping.
71 	fNoPreview = new BTextView("no preview");
72 	fNoPreview->SetText(B_TRANSLATE("No preview available"));
73 	fNoPreview->SetFontAndColor(be_plain_font, B_FONT_ALL, &kWhite);
74 	fNoPreview->MakeEditable(false);
75 	fNoPreview->MakeResizable(false);
76 	fNoPreview->MakeSelectable(false);
77 	fNoPreview->SetViewColor(0, 0, 0);
78 	fNoPreview->SetLowColor(0, 0, 0);
79 
80 	fNoPreview->Hide();
81 
82 	BView* container = new BView("preview container", 0);
83 	container->SetLayout(new BCardLayout());
84 	AddChild(container);
85 	container->SetViewColor(0, 0, 0);
86 	container->SetLowColor(0, 0, 0);
87 	container->AddChild(fNoPreview);
88 
89 	fNoPreview->SetHighColor(255, 255, 255);
90 	fNoPreview->SetAlignment(B_ALIGN_CENTER);
91 }
92 
93 
94 PreviewView::~PreviewView()
95 {
96 }
97 
98 
99 void
100 PreviewView::Draw(BRect updateRect)
101 {
102 	SetHighColor(184, 184, 184);
103 	FillRoundRect(scale2(0, 9, 0, 3, Bounds()), 4, 4);
104 		// outer shape
105 	FillRoundRect(scale2(2, 7, 3, 6, Bounds()), 2, 2);
106 		// control console outline
107 
108 	SetHighColor(96, 96, 96);
109 	StrokeRoundRect(scale2(2, 7, 3, 6, Bounds()), 2, 2);
110 		// control console outline
111 	StrokeRoundRect(scale2(0, 9, 0, 3, Bounds()), 4, 4);
112 		// outline outer shape
113 
114 	SetHighColor(0, 0, 0);
115 	FillRect(scale2(1, 8, 1, 2, Bounds()));
116 
117 	SetHighColor(184, 184, 184);
118 	BRect outerShape = scale2(2, 7, 2, 6, Bounds());
119 	outerShape.InsetBy(1, 1);
120 	FillRoundRect(outerShape, 4, 4);
121 		// outer shape
122 
123 	SetHighColor(0, 255, 0);
124 	FillRect(scale2(3, 4, 4, 5, Bounds()));
125 	SetHighColor(96, 96, 96);
126 	FillRect(scale2(5, 6, 4, 5, Bounds()));
127 }
128 
129 
130 BView*
131 PreviewView::AddPreview()
132 {
133 	fSaverView = new BView("preview", B_WILL_DRAW);
134 	fSaverView->SetViewColor(0, 0, 0);
135 	fSaverView->SetLowColor(0, 0, 0);
136 	ChildAt(0)->AddChild(fSaverView);
137 
138 	float aspectRatio = 4.0f / 3.0f;
139 		// 4:3 monitor
140 	float previewWidth = 120.0f;
141 	float previewHeight = ceilf(previewWidth / aspectRatio);
142 
143 	fSaverView->SetExplicitSize(BSize(previewWidth, previewHeight));
144 	fSaverView->ResizeTo(previewWidth, previewHeight);
145 
146 	fNoPreview->SetExplicitSize(BSize(previewWidth, previewHeight));
147 	fNoPreview->ResizeTo(previewWidth, previewHeight);
148 	fNoPreview->SetInsets(0, previewHeight / 3, 0 , 0);
149 
150 	return fSaverView;
151 }
152 
153 
154 BView*
155 PreviewView::RemovePreview()
156 {
157 	ShowNoPreview();
158 
159 	if (fSaverView != NULL)
160 		ChildAt(0)->RemoveChild(fSaverView);
161 
162 	BView* saverView = fSaverView;
163 	fSaverView = NULL;
164 	return saverView;
165 }
166 
167 
168 BView*
169 PreviewView::SaverView()
170 {
171 	return fSaverView;
172 }
173 
174 
175 void
176 PreviewView::ShowNoPreview() const
177 {
178 	((BCardLayout*)ChildAt(0)->GetLayout())->SetVisibleItem((int32)0);
179 }
180 
181 
182 void
183 PreviewView::HideNoPreview() const
184 {
185 	((BCardLayout*)ChildAt(0)->GetLayout())->SetVisibleItem(1);
186 }
187