From ecbb2c19432752c0d1ba2551d78896e215d4427e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20A=C3=9Fmus?= Date: Sun, 18 Aug 2013 12:59:10 +0200 Subject: [PATCH] HaikuDepot: Moved BitmapView into its own source file * Added BitmapButton, based on BitmapView, for clickable icons. Incomplete and untested. --- src/apps/haiku-depot/BitmapButton.cpp | 112 +++++++++++++++++ src/apps/haiku-depot/BitmapButton.h | 39 ++++++ src/apps/haiku-depot/BitmapView.cpp | 143 ++++++++++++++++++++++ src/apps/haiku-depot/BitmapView.h | 32 +++++ src/apps/haiku-depot/Jamfile | 2 + src/apps/haiku-depot/PackageInfoView.cpp | 146 +---------------------- 6 files changed, 330 insertions(+), 144 deletions(-) create mode 100644 src/apps/haiku-depot/BitmapButton.cpp create mode 100644 src/apps/haiku-depot/BitmapButton.h create mode 100644 src/apps/haiku-depot/BitmapView.cpp create mode 100644 src/apps/haiku-depot/BitmapView.h diff --git a/src/apps/haiku-depot/BitmapButton.cpp b/src/apps/haiku-depot/BitmapButton.cpp new file mode 100644 index 0000000000..342bf0bcf8 --- /dev/null +++ b/src/apps/haiku-depot/BitmapButton.cpp @@ -0,0 +1,112 @@ +/* + * Copyright 2013, Stephan Aßmus . + * All rights reserved. Distributed under the terms of the MIT License. + */ + + +#include "BitmapButton.h" + +#include +#include + + +BitmapButton::BitmapButton(const char* name) + : + BitmapView(name), + fMouseInside(false), + fMouseDown(false) +{ +} + + +BitmapButton::~BitmapButton() +{ +} + + +void +BitmapButton::AttachedToWindow() +{ + // TODO: Init fMouseInside + BitmapView::AttachedToWindow(); +} + +void +BitmapButton::MouseMoved(BPoint where, uint32 transit, + const BMessage* dragMessage) +{ + int32 buttons = 0; + if (Window() != NULL && Window()->CurrentMessage() != NULL) + Window()->CurrentMessage()->FindInt32("buttons", &buttons); + + bool ignoreEvent = dragMessage != NULL || (!fMouseDown && buttons != 0); + + _SetMouseInside(!ignoreEvent && transit == B_INSIDE_VIEW); +} + + +void +BitmapButton::MouseDown(BPoint where) +{ + _SetMouseDown(true); +} + + +void +BitmapButton::MouseUp(BPoint where) +{ + _SetMouseDown(false); +} + + +BSize +BitmapButton::MinSize() +{ + BSize size = BitmapView::MinSize(); + return BLayoutUtils::ComposeSize(ExplicitMinSize(), size); +} + + +BSize +BitmapButton::PreferredSize() +{ + BSize size = MinSize(); + return BLayoutUtils::ComposeSize(ExplicitPreferredSize(), size); +} + + +BSize +BitmapButton::MaxSize() +{ + BSize size = MinSize(); + return BLayoutUtils::ComposeSize(ExplicitMaxSize(), size); +} + + +// #pragma mark - + + +void +BitmapButton::_SetMouseInside(bool inside) +{ + if (fMouseInside == inside) + return; + + fMouseInside = inside; + + if (Message() != NULL) + Invalidate(); +} + + +void +BitmapButton::_SetMouseDown(bool down) +{ + if (fMouseDown == down) + return; + + fMouseDown = down; + + if (Message() != NULL) + Invalidate(); +} diff --git a/src/apps/haiku-depot/BitmapButton.h b/src/apps/haiku-depot/BitmapButton.h new file mode 100644 index 0000000000..8c63128967 --- /dev/null +++ b/src/apps/haiku-depot/BitmapButton.h @@ -0,0 +1,39 @@ +/* + * Copyright 2013, Stephan Aßmus . + * All rights reserved. Distributed under the terms of the MIT License. + */ +#ifndef BITMAP_BUTTON_H +#define BITMAP_BUTTON_H + + +#include + +#include "BitmapView.h" + + +class BitmapButton : public BitmapView, public BInvoker { +public: + BitmapButton(const char* name); + virtual ~BitmapButton(); + + virtual void AttachedToWindow(); + + virtual void MouseMoved(BPoint where, uint32 transit, + const BMessage* dragMessage); + virtual void MouseDown(BPoint where); + virtual void MouseUp(BPoint where); + + virtual BSize MinSize(); + virtual BSize PreferredSize(); + virtual BSize MaxSize(); + +private: + void _SetMouseInside(bool inside); + void _SetMouseDown(bool down); + +private: + bool fMouseInside; + bool fMouseDown; +}; + +#endif // BITMAP_VIEW_H diff --git a/src/apps/haiku-depot/BitmapView.cpp b/src/apps/haiku-depot/BitmapView.cpp new file mode 100644 index 0000000000..bc4a897130 --- /dev/null +++ b/src/apps/haiku-depot/BitmapView.cpp @@ -0,0 +1,143 @@ +/* + * Copyright 2013, Stephan Aßmus . + * All rights reserved. Distributed under the terms of the MIT License. + */ + + +#include "BitmapView.h" + +#include + +#include +#include + + +BitmapView::BitmapView(const char* name) + : + BView(name, B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE), + fBitmap(NULL) +{ + SetViewColor(B_TRANSPARENT_COLOR); + SetLowColor(ui_color(B_PANEL_BACKGROUND_COLOR)); + SetDrawingMode(B_OP_OVER); +} + + +BitmapView::~BitmapView() +{ +} + + +void +BitmapView::AttachedToWindow() +{ + BView* parent = Parent(); + if (parent != NULL) { + rgb_color color = parent->ViewColor(); + if (color == B_TRANSPARENT_COLOR) + color = parent->LowColor(); + SetLowColor(color); + } +} + + +void +BitmapView::Draw(BRect updateRect) +{ + BRect bounds(Bounds()); + FillRect(updateRect, B_SOLID_LOW); + + if (fBitmap == NULL) + return; + + BRect bitmapBounds = fBitmap->Bounds(); + if (bitmapBounds.Width() <= 0.0f || bitmapBounds.Height() <= 0.0f) + return; + + float hScale = bounds.Width() / bitmapBounds.Width(); + float vScale = bounds.Height() / bitmapBounds.Height(); + + float scale = std::min(hScale, vScale); + + float width = bitmapBounds.Width() * scale; + float height = bitmapBounds.Height() * scale; + + switch (LayoutAlignment().horizontal) { + case B_ALIGN_LEFT: + break; + case B_ALIGN_RIGHT: + bounds.left = floorf(bounds.right - width); + break; + default: + case B_ALIGN_HORIZONTAL_CENTER: + bounds.left = floorf(bounds.left + + (bounds.Width() - width) / 2.0f); + break; + } + switch (LayoutAlignment().vertical) { + case B_ALIGN_TOP: + break; + case B_ALIGN_BOTTOM: + bounds.top = floorf(bounds.bottom - height); + break; + default: + case B_ALIGN_VERTICAL_CENTER: + bounds.top = floorf(bounds.top + + (bounds.Height() - height) / 2.0f); + break; + } + + bounds.right = ceilf(bounds.left + width); + bounds.bottom = ceilf(bounds.top + height); + + DrawBitmap(fBitmap, bitmapBounds, bounds, B_FILTER_BITMAP_BILINEAR); +} + + +BSize +BitmapView::MinSize() +{ + BSize size(0.0f, 0.0f); + + if (fBitmap != NULL) { + BRect bounds = fBitmap->Bounds(); + size.width = bounds.Width(); + size.height = bounds.Height(); + } + + return BLayoutUtils::ComposeSize(ExplicitMinSize(), size); +} + + +BSize +BitmapView::PreferredSize() +{ + BSize size = MinSize(); + return BLayoutUtils::ComposeSize(ExplicitPreferredSize(), size); +} + + +BSize +BitmapView::MaxSize() +{ + BSize size = MinSize(); + return BLayoutUtils::ComposeSize(ExplicitMaxSize(), size); +} + + +void +BitmapView::SetBitmap(const BBitmap* bitmap) +{ + if (bitmap == fBitmap) + return; + + BSize size = MinSize(); + + fBitmap = bitmap; + + BSize newSize = MinSize(); + if (size != newSize) + InvalidateLayout(); + + Invalidate(); +} diff --git a/src/apps/haiku-depot/BitmapView.h b/src/apps/haiku-depot/BitmapView.h new file mode 100644 index 0000000000..1452bfb8f6 --- /dev/null +++ b/src/apps/haiku-depot/BitmapView.h @@ -0,0 +1,32 @@ +/* + * Copyright 2013, Stephan Aßmus . + * All rights reserved. Distributed under the terms of the MIT License. + */ +#ifndef BITMAP_VIEW_H +#define BITMAP_VIEW_H + + +#include + + +class BitmapView : public BView { +public: + BitmapView(const char* name); + + virtual ~BitmapView(); + + virtual void AttachedToWindow(); + virtual void Draw(BRect updateRect); + + virtual BSize MinSize(); + virtual BSize PreferredSize(); + virtual BSize MaxSize(); + + void SetBitmap(const BBitmap* bitmap); + +private: + const BBitmap* fBitmap; +}; + + +#endif // BITMAP_VIEW_H diff --git a/src/apps/haiku-depot/Jamfile b/src/apps/haiku-depot/Jamfile index 4fca5680d0..61db429d97 100644 --- a/src/apps/haiku-depot/Jamfile +++ b/src/apps/haiku-depot/Jamfile @@ -14,6 +14,8 @@ for sourceDir in $(sourceDirs) { Application HaikuDepot : App.cpp + BitmapButton.cpp + BitmapView.cpp FilterView.cpp main.cpp MainWindow.cpp diff --git a/src/apps/haiku-depot/PackageInfoView.cpp b/src/apps/haiku-depot/PackageInfoView.cpp index 959c39d66a..213482b0a7 100644 --- a/src/apps/haiku-depot/PackageInfoView.cpp +++ b/src/apps/haiku-depot/PackageInfoView.cpp @@ -22,6 +22,8 @@ #include #include +#include "BitmapButton.h" +#include "BitmapView.h" #include "PackageManager.h" #include "TextView.h" @@ -34,150 +36,6 @@ static const rgb_color kLightBlack = (rgb_color){ 60, 60, 60, 255 }; static const float kContentTint = (B_NO_TINT + B_LIGHTEN_1_TINT) / 2.0f; -class BitmapView : public BView { -public: - BitmapView(const char* name) - : - BView(name, B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE), - fBitmap(NULL) - { - SetViewColor(B_TRANSPARENT_COLOR); - SetLowColor(ui_color(B_PANEL_BACKGROUND_COLOR)); - SetDrawingMode(B_OP_OVER); - } - - virtual ~BitmapView() - { - } - - virtual void AttachedToWindow() - { - BView* parent = Parent(); - if (parent != NULL) - SetLowColor(parent->ViewColor()); - } - - virtual void Draw(BRect updateRect) - { - BRect bounds(Bounds()); - FillRect(updateRect, B_SOLID_LOW); - - if (fBitmap == NULL) - return; - - BRect bitmapBounds = fBitmap->Bounds(); - if (bitmapBounds.Width() <= 0.0f || bitmapBounds.Height() <= 0.0f) - return; - - float hScale = bounds.Width() / bitmapBounds.Width(); - float vScale = bounds.Height() / bitmapBounds.Height(); - - float scale = std::min(hScale, vScale); - - float width = bitmapBounds.Width() * scale; - float height = bitmapBounds.Height() * scale; - - switch (LayoutAlignment().horizontal) { - case B_ALIGN_LEFT: - break; - case B_ALIGN_RIGHT: - bounds.left = floorf(bounds.right - width); - break; - default: - case B_ALIGN_HORIZONTAL_CENTER: - bounds.left = floorf(bounds.left - + (bounds.Width() - width) / 2.0f); - break; - } - switch (LayoutAlignment().vertical) { - case B_ALIGN_TOP: - break; - case B_ALIGN_BOTTOM: - bounds.top = floorf(bounds.bottom - height); - break; - default: - case B_ALIGN_VERTICAL_CENTER: - bounds.top = floorf(bounds.top - + (bounds.Height() - height) / 2.0f); - break; - } - - bounds.right = ceilf(bounds.left + width); - bounds.bottom = ceilf(bounds.top + height); - - DrawBitmap(fBitmap, bitmapBounds, bounds, B_FILTER_BITMAP_BILINEAR); - } - - virtual BSize MinSize() - { - BSize size(0.0f, 0.0f); - - if (fBitmap != NULL) { - BRect bounds = fBitmap->Bounds(); - size.width = bounds.Width(); - size.height = bounds.Height(); - } - - return BLayoutUtils::ComposeSize(ExplicitMinSize(), size); - } - - virtual BSize PreferredSize() - { - BSize size = MinSize(); - return BLayoutUtils::ComposeSize(ExplicitPreferredSize(), size); - } - - virtual BSize MaxSize() - { - BSize size = MinSize(); - return BLayoutUtils::ComposeSize(ExplicitMaxSize(), size); - } - -// virtual bool HasHeightForWidth() -// { -// return true; -// } -// -// virtual void GetHeightForWidth(float width, float* min, float* max, -// float* preferred) -// { -// float height = width; -// -// if (fBitmap != NULL) { -// BRect bounds = fBitmap->Bounds(); -// if (bounds.Width() > 0.0f && bounds.Height() > 0.0f) -// height = (width / bounds.Width()) * bounds.Height(); -// } -// -// if (min != NULL) -// *min = height; -// if (max != NULL) -// *max = height; -// if (preferred != NULL) -// *preferred = height; -// } - - void SetBitmap(const BBitmap* bitmap) - { - if (bitmap == fBitmap) - return; - - BSize size = MinSize(); - - fBitmap = bitmap; - - BSize newSize = MinSize(); - if (size != newSize) - InvalidateLayout(); - - Invalidate(); - } - -private: - const BBitmap* fBitmap; -}; - - //! Layouts the scrollbar so it looks nice with no border and the document // window look. class CustomScrollView : public BScrollView {