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