2 * Copyright 2003-2013 Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
7 * Jérôme Duval, jerome.duval@free.fr
11 #include "PreviewView.h"
15 #include <CardLayout.h>
17 #include <GroupLayout.h>
21 #include <StringView.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 };
40 scale2(int x
, int y
, BRect area
)
42 return scale_direct(sampleX
[x
], sampleY
[y
], area
);
47 scale2(int x1
, int x2
, int y1
, int y2
, BRect area
)
49 return scale_direct(sampleX
[x1
], sampleX
[x2
], sampleY
[y1
], sampleY
[y2
],
54 // #pragma mark - PreviewView
57 PreviewView::PreviewView(const char* name
)
59 BView(name
, B_WILL_DRAW
| B_FULL_UPDATE_ON_RESIZE
),
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);
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);
82 BView
* container
= new BView("preview container", 0);
83 container
->SetLayout(new BCardLayout());
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()
100 PreviewView::Draw(BRect updateRect
)
102 SetHighColor(184, 184, 184);
103 FillRoundRect(scale2(0, 9, 0, 3, Bounds()), 4, 4);
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);
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()));
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
;
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);
155 PreviewView::RemovePreview()
159 if (fSaverView
!= NULL
)
160 ChildAt(0)->RemoveChild(fSaverView
);
162 BView
* saverView
= fSaverView
;
169 PreviewView::SaverView()
176 PreviewView::ShowNoPreview() const
178 ((BCardLayout
*)ChildAt(0)->GetLayout())->SetVisibleItem((int32
)0);
183 PreviewView::HideNoPreview() const
185 ((BCardLayout
*)ChildAt(0)->GetLayout())->SetVisibleItem(1);