fat: Greatly simplify and clean up dosfs_get_file_map().
[haiku.git] / src / preferences / screensaver / PreviewView.cpp
blobf0ad5a12611a3878bfbeee01fc641b0b8b814559
1 /*
2 * Copyright 2003-2013 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 <iostream>
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>
24 #include "Utility.h"
27 static const rgb_color kWhite = (rgb_color){ 255, 255, 255 };
30 #undef B_TRANSLATION_CONTEXT
31 #define B_TRANSLATION_CONTEXT "PreviewView"
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 };
39 inline BPoint
40 scale2(int x, int y, BRect area)
42 return scale_direct(sampleX[x], sampleY[y], area);
46 inline BRect
47 scale2(int x1, int x2, int y1, int y2, BRect area)
49 return scale_direct(sampleX[x1], sampleX[x2], sampleY[y1], sampleY[y2],
50 area);
54 // #pragma mark - PreviewView
57 PreviewView::PreviewView(const char* name)
59 BView(name, B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE),
60 fSaverView(NULL),
61 fNoPreview(NULL)
63 SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
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);
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);
80 fNoPreview->Hide();
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);
89 fNoPreview->SetHighColor(255, 255, 255);
90 fNoPreview->SetAlignment(B_ALIGN_CENTER);
94 PreviewView::~PreviewView()
99 void
100 PreviewView::Draw(BRect updateRect)
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
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
114 SetHighColor(0, 0, 0);
115 FillRect(scale2(1, 8, 1, 2, Bounds()));
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
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()));
130 BView*
131 PreviewView::AddPreview()
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);
138 float aspectRatio = 4.0f / 3.0f;
139 // 4:3 monitor
140 float previewWidth = 120.0f;
141 float previewHeight = ceilf(previewWidth / aspectRatio);
143 fSaverView->SetExplicitSize(BSize(previewWidth, previewHeight));
144 fSaverView->ResizeTo(previewWidth, previewHeight);
146 fNoPreview->SetExplicitSize(BSize(previewWidth, previewHeight));
147 fNoPreview->ResizeTo(previewWidth, previewHeight);
148 fNoPreview->SetInsets(0, previewHeight / 3, 0 , 0);
150 return fSaverView;
154 BView*
155 PreviewView::RemovePreview()
157 ShowNoPreview();
159 if (fSaverView != NULL)
160 ChildAt(0)->RemoveChild(fSaverView);
162 BView* saverView = fSaverView;
163 fSaverView = NULL;
164 return saverView;
168 BView*
169 PreviewView::SaverView()
171 return fSaverView;
175 void
176 PreviewView::ShowNoPreview() const
178 ((BCardLayout*)ChildAt(0)->GetLayout())->SetVisibleItem((int32)0);
182 void
183 PreviewView::HideNoPreview() const
185 ((BCardLayout*)ChildAt(0)->GetLayout())->SetVisibleItem(1);