From 7fa83160f89bc5b4c988ba45b07991f8c71e7111 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20A=C3=9Fmus?= Date: Sat, 27 Jul 2013 22:23:25 +0200 Subject: [PATCH] HaikuDepot: PackagesListView BColumnListView derived class with code-duplication from DriveSetup that should be refactored eventually (BIconStringColumn and BColumn implementation to show it). Does nothing except create some columns. --- src/apps/haiku-depot/PackageListView.cpp | 223 +++++++++++++++++++++++ src/apps/haiku-depot/PackageListView.h | 67 +++++++ 2 files changed, 290 insertions(+) create mode 100644 src/apps/haiku-depot/PackageListView.cpp create mode 100644 src/apps/haiku-depot/PackageListView.h diff --git a/src/apps/haiku-depot/PackageListView.cpp b/src/apps/haiku-depot/PackageListView.cpp new file mode 100644 index 0000000000..2877d6f8f0 --- /dev/null +++ b/src/apps/haiku-depot/PackageListView.cpp @@ -0,0 +1,223 @@ +/* + * Copyright 2013, Stephan Aßmus . + * All rights reserved. Distributed under the terms of the MIT License. + */ + +#include "PackageListView.h" + +#include +#include + +#include + + +#undef B_TRANSLATION_CONTEXT +#define B_TRANSLATION_CONTEXT "PackageListView" + + +// #pragma mark - BBitmapStringField + + +// TODO: Code-duplication with DriveSetup PartitionList.cpp +BBitmapStringField::BBitmapStringField(BBitmap* bitmap, const char* string) + : + Inherited(string), + fBitmap(bitmap) +{ +} + + +BBitmapStringField::~BBitmapStringField() +{ + delete fBitmap; +} + + +void +BBitmapStringField::SetBitmap(BBitmap* bitmap) +{ + delete fBitmap; + fBitmap = bitmap; + // TODO: cause a redraw? +} + + +// #pragma mark - PackageColumn + + +// TODO: Code-duplication with DriveSetup PartitionList.cpp + + +float PackageColumn::sTextMargin = 0.0; + + +PackageColumn::PackageColumn(const char* title, float width, float minWidth, + float maxWidth, uint32 truncateMode, alignment align) + : + Inherited(title, width, minWidth, maxWidth, align), + fTruncateMode(truncateMode) +{ + SetWantsEvents(true); +} + + +void +PackageColumn::DrawField(BField* field, BRect rect, BView* parent) +{ + BBitmapStringField* bitmapField + = dynamic_cast(field); + BStringField* stringField = dynamic_cast(field); + + if (bitmapField) { + const BBitmap* bitmap = bitmapField->Bitmap(); + + // figure out the placement + float x = 0.0; + BRect r = bitmap ? bitmap->Bounds() : BRect(0, 0, 15, 15); + float y = rect.top + ((rect.Height() - r.Height()) / 2); + float width = 0.0; + + switch (Alignment()) { + default: + case B_ALIGN_LEFT: + case B_ALIGN_CENTER: + x = rect.left + sTextMargin; + width = rect.right - (x + r.Width()) - (2 * sTextMargin); + r.Set(x + r.Width(), rect.top, rect.right - width, rect.bottom); + break; + + case B_ALIGN_RIGHT: + x = rect.right - sTextMargin - r.Width(); + width = (x - rect.left - (2 * sTextMargin)); + r.Set(rect.left, rect.top, rect.left + width, rect.bottom); + break; + } + + if (width != bitmapField->Width()) { + BString truncatedString(bitmapField->String()); + parent->TruncateString(&truncatedString, fTruncateMode, width + 2); + bitmapField->SetClippedString(truncatedString.String()); + bitmapField->SetWidth(width); + } + + // draw the bitmap + if (bitmap) { + parent->SetDrawingMode(B_OP_ALPHA); + parent->DrawBitmap(bitmap, BPoint(x, y)); + parent->SetDrawingMode(B_OP_OVER); + } + + // draw the string + DrawString(bitmapField->ClippedString(), parent, r); + + } else if (stringField) { + + float width = rect.Width() - (2 * sTextMargin); + + if (width != stringField->Width()) { + BString truncatedString(stringField->String()); + + parent->TruncateString(&truncatedString, fTruncateMode, width + 2); + stringField->SetClippedString(truncatedString.String()); + stringField->SetWidth(width); + } + + DrawString(stringField->ClippedString(), parent, rect); + } +} + + +float +PackageColumn::GetPreferredWidth(BField *_field, BView* parent) const +{ + BBitmapStringField* bitmapField + = dynamic_cast(_field); + BStringField* stringField = dynamic_cast(_field); + + float parentWidth = Inherited::GetPreferredWidth(_field, parent); + float width = 0.0; + + if (bitmapField) { + const BBitmap* bitmap = bitmapField->Bitmap(); + BFont font; + parent->GetFont(&font); + width = font.StringWidth(bitmapField->String()) + 3 * sTextMargin; + if (bitmap) + width += bitmap->Bounds().Width(); + else + width += 16; + } else if (stringField) { + BFont font; + parent->GetFont(&font); + width = font.StringWidth(stringField->String()) + 2 * sTextMargin; + } + return max_c(width, parentWidth); +} + + +bool +PackageColumn::AcceptsField(const BField* field) const +{ + return dynamic_cast(field) != NULL; +} + + +void +PackageColumn::InitTextMargin(BView* parent) +{ + BFont font; + parent->GetFont(&font); + sTextMargin = ceilf(font.Size() * 0.8); +} + + +// #pragma mark - PackageListView + + +enum { + kNameColumn, + kRatingColumn, + kDescriptionColumn, + kSizeColumn, + kStatusColumn, +}; + + +PackageListView::PackageListView() + : + BColumnListView("package list view", 0, B_FANCY_BORDER, true) +{ + AddColumn(new PackageColumn(B_TRANSLATE("Name"), 150, 50, 500, + B_TRUNCATE_MIDDLE), kNameColumn); + AddColumn(new PackageColumn(B_TRANSLATE("Rating"), 100, 50, 500, + B_TRUNCATE_MIDDLE), kRatingColumn); + AddColumn(new PackageColumn(B_TRANSLATE("Description"), 130, 50, 500, + B_TRUNCATE_MIDDLE), kDescriptionColumn); + AddColumn(new PackageColumn(B_TRANSLATE("Size"), 100, 50, 500, + B_TRUNCATE_END), kSizeColumn); + AddColumn(new PackageColumn(B_TRANSLATE("Status"), 100, 50, 500, + B_TRUNCATE_END), kStatusColumn); +} + + +PackageListView::~PackageListView() +{ +} + + +void +PackageListView::AttachedToWindow() +{ + PackageColumn::InitTextMargin(ScrollView()); +} + + +void +PackageListView::MessageReceived(BMessage* message) +{ + switch (message->what) { + default: + BColumnListView::MessageReceived(message); + break; + } +} diff --git a/src/apps/haiku-depot/PackageListView.h b/src/apps/haiku-depot/PackageListView.h new file mode 100644 index 0000000000..ac2388988e --- /dev/null +++ b/src/apps/haiku-depot/PackageListView.h @@ -0,0 +1,67 @@ +/* + * Copyright 2013, Stephan Aßmus . + * All rights reserved. Distributed under the terms of the MIT License. + */ +#ifndef PACKAGE_LIST_VIEW_H +#define PACKAGE_LIST_VIEW_H + + +#include +#include + + +// A field type displaying both a bitmap and a string so that the +// tree display looks nicer (both text and bitmap are indented) +// TODO: Code-duplication with DriveSetup PartitionList.h +class BBitmapStringField : public BStringField { + typedef BStringField Inherited; +public: + BBitmapStringField(BBitmap* bitmap, + const char* string); + virtual ~BBitmapStringField(); + + void SetBitmap(BBitmap* bitmap); + const BBitmap* Bitmap() const + { return fBitmap; } + +private: + BBitmap* fBitmap; +}; + + +// BColumn for PackageListView which knows how to render +// a BBitmapStringField +// TODO: Code-duplication with DriveSetup PartitionList.h +class PackageColumn : public BTitledColumn { + typedef BTitledColumn Inherited; +public: + PackageColumn(const char* title, + float width, float minWidth, + float maxWidth, uint32 truncateMode, + alignment align = B_ALIGN_LEFT); + + virtual void DrawField(BField* field, BRect rect, + BView* parent); + virtual float GetPreferredWidth(BField* field, + BView* parent) const; + + virtual bool AcceptsField(const BField* field) const; + + static void InitTextMargin(BView* parent); + +private: + uint32 fTruncateMode; + static float sTextMargin; +}; + + +class PackageListView : public BColumnListView { +public: + PackageListView(); + virtual ~PackageListView(); + + virtual void AttachedToWindow(); + virtual void MessageReceived(BMessage* message); +}; + +#endif // PACKAGE_LIST_VIEW_H