HaikuDepot: Moved BitmapView into its own source file
* Added BitmapButton, based on BitmapView, for clickable icons. Incomplete and untested.
This commit is contained in:
parent
a69892cadc
commit
ecbb2c1943
112
src/apps/haiku-depot/BitmapButton.cpp
Normal file
112
src/apps/haiku-depot/BitmapButton.cpp
Normal file
@ -0,0 +1,112 @@
|
||||
/*
|
||||
* Copyright 2013, Stephan Aßmus <superstippi@gmx.de>.
|
||||
* All rights reserved. Distributed under the terms of the MIT License.
|
||||
*/
|
||||
|
||||
|
||||
#include "BitmapButton.h"
|
||||
|
||||
#include <LayoutUtils.h>
|
||||
#include <Window.h>
|
||||
|
||||
|
||||
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();
|
||||
}
|
39
src/apps/haiku-depot/BitmapButton.h
Normal file
39
src/apps/haiku-depot/BitmapButton.h
Normal file
@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright 2013, Stephan Aßmus <superstippi@gmx.de>.
|
||||
* All rights reserved. Distributed under the terms of the MIT License.
|
||||
*/
|
||||
#ifndef BITMAP_BUTTON_H
|
||||
#define BITMAP_BUTTON_H
|
||||
|
||||
|
||||
#include <Invoker.h>
|
||||
|
||||
#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
|
143
src/apps/haiku-depot/BitmapView.cpp
Normal file
143
src/apps/haiku-depot/BitmapView.cpp
Normal file
@ -0,0 +1,143 @@
|
||||
/*
|
||||
* Copyright 2013, Stephan Aßmus <superstippi@gmx.de>.
|
||||
* All rights reserved. Distributed under the terms of the MIT License.
|
||||
*/
|
||||
|
||||
|
||||
#include "BitmapView.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include <Bitmap.h>
|
||||
#include <LayoutUtils.h>
|
||||
|
||||
|
||||
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();
|
||||
}
|
32
src/apps/haiku-depot/BitmapView.h
Normal file
32
src/apps/haiku-depot/BitmapView.h
Normal file
@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright 2013, Stephan Aßmus <superstippi@gmx.de>.
|
||||
* All rights reserved. Distributed under the terms of the MIT License.
|
||||
*/
|
||||
#ifndef BITMAP_VIEW_H
|
||||
#define BITMAP_VIEW_H
|
||||
|
||||
|
||||
#include <View.h>
|
||||
|
||||
|
||||
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
|
@ -14,6 +14,8 @@ for sourceDir in $(sourceDirs) {
|
||||
|
||||
Application HaikuDepot :
|
||||
App.cpp
|
||||
BitmapButton.cpp
|
||||
BitmapView.cpp
|
||||
FilterView.cpp
|
||||
main.cpp
|
||||
MainWindow.cpp
|
||||
|
@ -22,6 +22,8 @@
|
||||
#include <SpaceLayoutItem.h>
|
||||
#include <StringView.h>
|
||||
|
||||
#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 {
|
||||
|
Loading…
Reference in New Issue
Block a user