BTRFS: Implement BTree::Path and change _Find.
[haiku.git] / src / preferences / screensaver / PreviewView.cpp
blob8301089a0ee4d44a444c00ad718328b3a20664fc
1 /*
2 * Copyright 2003-2015 Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Michael Phipps
7 * Jérôme Duval, jerome.duval@free.fr
8 */
11 #include "PreviewView.h"
13 #include <algorithm>
14 #include <iostream>
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>
25 #include "Utility.h"
28 static const rgb_color kWhite = (rgb_color){ 255, 255, 255 };
31 #undef B_TRANSLATION_CONTEXT
32 #define B_TRANSLATION_CONTEXT "PreviewView"
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 };
40 inline BPoint
41 scale2(int x, int y, BRect area)
43 return scale_direct(sampleX[x], sampleY[y], area);
47 inline BRect
48 scale2(int x1, int x2, int y1, int y2, BRect area)
50 return scale_direct(sampleX[x1], sampleX[x2], sampleY[y1], sampleY[y2],
51 area);
55 // #pragma mark - PreviewView
58 PreviewView::PreviewView(const char* name)
60 BView(name, B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE),
61 fSaverView(NULL),
62 fNoPreview(NULL)
64 SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
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);
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);
81 fNoPreview->Hide();
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);
90 fNoPreview->SetHighColor(255, 255, 255);
91 fNoPreview->SetAlignment(B_ALIGN_CENTER);
95 PreviewView::~PreviewView()
100 void
101 PreviewView::Draw(BRect updateRect)
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
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
115 SetHighColor(0, 0, 0);
116 FillRect(scale2(1, 8, 1, 2, Bounds()));
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
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()));
131 BView*
132 PreviewView::AddPreview()
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);
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);
144 fSaverView->SetExplicitSize(BSize(previewWidth, previewHeight));
145 fSaverView->ResizeTo(previewWidth, previewHeight);
147 fNoPreview->SetExplicitSize(BSize(previewWidth, previewHeight));
148 fNoPreview->ResizeTo(previewWidth, previewHeight);
149 fNoPreview->SetInsets(0, previewHeight / 3, 0 , 0);
151 return fSaverView;
155 BView*
156 PreviewView::RemovePreview()
158 ShowNoPreview();
160 if (fSaverView != NULL)
161 ChildAt(0)->RemoveChild(fSaverView);
163 BView* saverView = fSaverView;
164 fSaverView = NULL;
165 return saverView;
169 BView*
170 PreviewView::SaverView()
172 return fSaverView;
176 void
177 PreviewView::ShowNoPreview() const
179 ((BCardLayout*)ChildAt(0)->GetLayout())->SetVisibleItem((int32)0);
183 void
184 PreviewView::HideNoPreview() const
186 ((BCardLayout*)ChildAt(0)->GetLayout())->SetVisibleItem(1);