1 /* 2 * Copyright 2013-214, Stephan Aßmus <superstippi@gmx.de>. 3 * All rights reserved. Distributed under the terms of the MIT License. 4 */ 5 6 #include "FeaturedPackagesView.h" 7 8 #include <stdio.h> 9 10 #include <Catalog.h> 11 #include <Font.h> 12 #include <LayoutBuilder.h> 13 #include <Message.h> 14 #include <ScrollView.h> 15 #include <StringView.h> 16 #include <SpaceLayoutItem.h> 17 18 #include "BitmapView.h" 19 #include "MessagePackageListener.h" 20 #include "RatingView.h" 21 #include "ScrollableGroupView.h" 22 23 24 #undef B_TRANSLATION_CONTEXT 25 #define B_TRANSLATION_CONTEXT "FeaturedPackagesView" 26 27 28 static const rgb_color kLightBlack = (rgb_color){ 60, 60, 60, 255 }; 29 30 31 // #pragma mark - PackageView 32 33 34 class PackageView : public BGroupView { 35 public: 36 PackageView() 37 : 38 BGroupView("package view", B_HORIZONTAL), 39 fPackageListener(new(std::nothrow) MessagePackageListener(this)) 40 { 41 SetViewColor(255, 255, 255); 42 43 fIconView = new BitmapView("package icon view"); 44 fTitleView = new BStringView("package title view", ""); 45 fPublisherView = new BStringView("package publisher view", ""); 46 47 // Title font 48 BFont font; 49 GetFont(&font); 50 font_family family; 51 font_style style; 52 font.SetSize(ceilf(font.Size() * 1.8f)); 53 font.GetFamilyAndStyle(&family, &style); 54 font.SetFamilyAndStyle(family, "Bold"); 55 fTitleView->SetFont(&font); 56 57 // Publisher font 58 GetFont(&font); 59 font.SetSize(std::max(9.0f, floorf(font.Size() * 0.92f))); 60 font.SetFamilyAndStyle(family, "Italic"); 61 fPublisherView->SetFont(&font); 62 fPublisherView->SetHighColor(kLightBlack); 63 64 // slightly bigger font 65 GetFont(&font); 66 font.SetSize(ceilf(font.Size() * 1.2f)); 67 68 // Version info 69 fVersionInfo = new BStringView("package version info", ""); 70 fVersionInfo->SetFont(&font); 71 fVersionInfo->SetHighColor(kLightBlack); 72 73 BLayoutBuilder::Group<>(this) 74 .Add(fIconView) 75 .AddGroup(B_VERTICAL, 1.0f, 2.2f) 76 .Add(fTitleView) 77 .Add(fPublisherView) 78 .SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET)) 79 .End() 80 .AddGlue(0.1f) 81 .AddGroup(B_HORIZONTAL, B_USE_SMALL_SPACING, 2.0f) 82 .Add(fVersionInfo) 83 .AddGlue() 84 .SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET)) 85 .End() 86 .SetInsets(B_USE_WINDOW_INSETS) 87 ; 88 89 Clear(); 90 } 91 92 virtual ~PackageView() 93 { 94 fPackageListener->SetPackage(PackageInfoRef(NULL)); 95 delete fPackageListener; 96 } 97 98 virtual void MessageReceived(BMessage* message) 99 { 100 switch (message->what) { 101 case MSG_UPDATE_PACKAGE: 102 { 103 SetPackage(fPackageListener->Package()); 104 break; 105 } 106 } 107 } 108 109 void SetPackage(const PackageInfoRef& package) 110 { 111 fPackageListener->SetPackage(package); 112 113 if (package->Icon().Get() != NULL) { 114 fIconView->SetBitmap( 115 package->Icon()->Bitmap(SharedBitmap::SIZE_64)); 116 } else 117 fIconView->SetBitmap(NULL); 118 119 fTitleView->SetText(package->Title()); 120 121 BString publisher = package->Publisher().Name(); 122 fPublisherView->SetText(publisher); 123 124 BString version = B_TRANSLATE("%Version%"); 125 version.ReplaceAll("%Version%", package->Version().ToString()); 126 fVersionInfo->SetText(version); 127 128 InvalidateLayout(); 129 Invalidate(); 130 } 131 132 void Clear() 133 { 134 fPackageListener->SetPackage(PackageInfoRef(NULL)); 135 136 fIconView->SetBitmap(NULL); 137 fTitleView->SetText(""); 138 fPublisherView->SetText(""); 139 fVersionInfo->SetText(""); 140 } 141 142 const char* PackageTitle() const 143 { 144 return fTitleView->Text(); 145 } 146 147 private: 148 MessagePackageListener* fPackageListener; 149 150 BitmapView* fIconView; 151 152 BStringView* fTitleView; 153 BStringView* fPublisherView; 154 155 BStringView* fVersionInfo; 156 }; 157 158 159 // #pragma mark - FeaturedPackagesView 160 161 162 FeaturedPackagesView::FeaturedPackagesView() 163 : 164 BView("featured package view", 0) 165 { 166 BGroupLayout* layout = new BGroupLayout(B_VERTICAL); 167 SetLayout(layout); 168 169 ScrollableGroupView* containerView = new ScrollableGroupView(); 170 containerView->SetViewColor(255, 255, 255); 171 fPackageListLayout = containerView->GroupLayout(); 172 173 BScrollView* scrollView = new BScrollView( 174 "featured packages scroll view", containerView, 175 0, false, true, B_FANCY_BORDER); 176 177 BLayoutBuilder::Group<>(this) 178 .Add(scrollView, 1.0f) 179 ; 180 } 181 182 183 FeaturedPackagesView::~FeaturedPackagesView() 184 { 185 } 186 187 188 void 189 FeaturedPackagesView::AddPackage(const PackageInfoRef& package) 190 { 191 PackageView* view = new PackageView(); 192 view->SetPackage(package); 193 194 // Find insertion index (alphabetical) 195 int32 index = 0; 196 for (int32 i = 0; BLayoutItem* item = fPackageListLayout->ItemAt(i); i++) { 197 PackageView* view = dynamic_cast<PackageView*>(item->View()); 198 if (view == NULL) 199 break; 200 201 BString title = view->PackageTitle(); 202 if (title.Compare(package->Title()) >= 0) 203 break; 204 205 index++; 206 } 207 208 fPackageListLayout->AddView(index, view); 209 } 210 211 212 void 213 FeaturedPackagesView::Clear() 214 { 215 for (int32 i = fPackageListLayout->CountItems() - 1; 216 BLayoutItem* item = fPackageListLayout->ItemAt(i); i--) { 217 BView* view = dynamic_cast<PackageView*>(item->View()); 218 if (view != NULL) { 219 view->RemoveSelf(); 220 delete view; 221 } 222 } 223 } 224 225