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 41 scale2(int x, int y, BRect area) 42 { 43 return scale_direct(sampleX[x], sampleY[y], area); 44 } 45 46 47 inline BRect 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 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 95 PreviewView::~PreviewView() 96 { 97 } 98 99 100 void 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* 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->SetInsets(0, previewHeight / 3, 0 , 0); 150 151 return fSaverView; 152 } 153 154 155 BView* 156 PreviewView::RemovePreview() 157 { 158 ShowNoPreview(); 159 160 if (fSaverView != NULL) 161 ChildAt(0)->RemoveChild(fSaverView); 162 163 BView* saverView = fSaverView; 164 fSaverView = NULL; 165 return saverView; 166 } 167 168 169 BView* 170 PreviewView::SaverView() 171 { 172 return fSaverView; 173 } 174 175 176 void 177 PreviewView::ShowNoPreview() const 178 { 179 ((BCardLayout*)ChildAt(0)->GetLayout())->SetVisibleItem((int32)0); 180 } 181 182 183 void 184 PreviewView::HideNoPreview() const 185 { 186 ((BCardLayout*)ChildAt(0)->GetLayout())->SetVisibleItem(1); 187 } 188