1 /* 2 * Copyright 2003-2013 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 <iostream> 14 15 #include <Catalog.h> 16 #include <Point.h> 17 #include <Rect.h> 18 #include <Size.h> 19 #include <TextView.h> 20 21 #include "Utility.h" 22 23 24 static const rgb_color kWhite = (rgb_color){ 255, 255, 255 }; 25 26 27 #undef B_TRANSLATION_CONTEXT 28 #define B_TRANSLATION_CONTEXT "PreviewView" 29 30 31 static float sampleX[] 32 = { 0, 0.05, 0.15, 0.7, 0.725, 0.8, 0.825, 0.85, 0.950, 1.0 }; 33 static float sampleY[] = { 0, 0.05, 0.90, 0.95, 0.966, 0.975, 1.0 }; 34 35 36 inline BPoint 37 scale2(int x, int y, BRect area) 38 { 39 return scale_direct(sampleX[x], sampleY[y], area); 40 } 41 42 43 inline BRect 44 scale2(int x1, int x2, int y1, int y2, BRect area) 45 { 46 return scale_direct(sampleX[x1], sampleX[x2], sampleY[y1], sampleY[y2], 47 area); 48 } 49 50 51 // #pragma mark - PreviewView 52 53 54 PreviewView::PreviewView(const char* name) 55 : 56 BView(name, B_WILL_DRAW), 57 fSaverView(NULL), 58 fNoPreview(NULL) 59 { 60 SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); 61 62 float aspectRatio = 4.0f / 3.0f; 63 // 4:3 monitor 64 float previewWidth = 160.0f; 65 float previewHeight = ceilf(previewWidth / aspectRatio); 66 67 SetExplicitSize(BSize(previewWidth, previewHeight)); 68 } 69 70 71 PreviewView::~PreviewView() 72 { 73 } 74 75 76 void 77 PreviewView::Draw(BRect updateRect) 78 { 79 SetHighColor(184, 184, 184); 80 FillRoundRect(scale2(0, 9, 0, 3, Bounds()), 4, 4); 81 // outer shape 82 FillRoundRect(scale2(2, 7, 3, 6, Bounds()), 2, 2); 83 // control console outline 84 85 SetHighColor(96, 96, 96); 86 StrokeRoundRect(scale2(2, 7, 3, 6, Bounds()), 2, 2); 87 // control console outline 88 StrokeRoundRect(scale2(0, 9, 0, 3, Bounds()), 4, 4); 89 // outline outer shape 90 91 SetHighColor(0, 0, 0); 92 FillRect(scale2(1, 8, 1, 2, Bounds())); 93 94 SetHighColor(184, 184, 184); 95 BRect outerShape = scale2(2, 7, 2, 6, Bounds()); 96 outerShape.InsetBy(1, 1); 97 FillRoundRect(outerShape, 4, 4); 98 // outer shape 99 100 SetHighColor(0, 255, 0); 101 FillRect(scale2(3, 4, 4, 5, Bounds())); 102 SetHighColor(96, 96, 96); 103 FillRect(scale2(5, 6, 4, 5, Bounds())); 104 } 105 106 107 BView* 108 PreviewView::AddPreview() 109 { 110 BRect rect(scale2(1, 8, 1, 2, Bounds()).InsetBySelf(1.0f, 1.0f)); 111 fSaverView = new BView(rect, "preview", B_FOLLOW_NONE, B_WILL_DRAW); 112 fSaverView->SetViewColor(0, 0, 0); 113 fSaverView->SetLowColor(0, 0, 0); 114 AddChild(fSaverView); 115 116 BRect textRect(rect); 117 textRect.OffsetTo(-7.0f, 0.0f); 118 textRect.InsetBy(15.0f, 20.0f); 119 fNoPreview = new BTextView(rect, "no preview", textRect, B_FOLLOW_NONE, 120 B_WILL_DRAW); 121 fNoPreview->SetViewColor(0, 0, 0); 122 fNoPreview->SetLowColor(0, 0, 0); 123 fNoPreview->SetFontAndColor(be_plain_font, B_FONT_ALL, &kWhite); 124 fNoPreview->SetText(B_TRANSLATE("No preview available")); 125 fNoPreview->SetAlignment(B_ALIGN_CENTER); 126 fNoPreview->MakeEditable(false); 127 fNoPreview->MakeResizable(false); 128 fNoPreview->MakeSelectable(false); 129 130 fNoPreview->Hide(); 131 fSaverView->AddChild(fNoPreview); 132 133 return fSaverView; 134 } 135 136 137 BView* 138 PreviewView::RemovePreview() 139 { 140 if (fSaverView != NULL) 141 RemoveChild(fSaverView); 142 143 BView* saverView = fSaverView; 144 fSaverView = NULL; 145 return saverView; 146 } 147 148 149 BView* 150 PreviewView::SaverView() 151 { 152 return fSaverView; 153 } 154 155 156 void 157 PreviewView::ShowNoPreview() const 158 { 159 fNoPreview->Show(); 160 } 161 162 163 void 164 PreviewView::HideNoPreview() const 165 { 166 fNoPreview->Hide(); 167 } 168