2 * Copyright 2013, Stephan Aßmus <superstippi@gmx.de>.
3 * All rights reserved. Distributed under the terms of the MIT License.
7 #include "BitmapView.h"
12 #include <LayoutUtils.h>
15 BitmapView::BitmapView(const char* name
)
17 BView(name
, B_WILL_DRAW
| B_FULL_UPDATE_ON_RESIZE
),
24 BitmapView::~BitmapView()
30 BitmapView::AllAttached()
37 BitmapView::Draw(BRect updateRect
)
39 BRect
bounds(Bounds());
40 DrawBackground(bounds
, updateRect
);
45 BRect bitmapBounds
= fBitmap
->Bounds();
46 if (bitmapBounds
.Width() <= 0.0f
|| bitmapBounds
.Height() <= 0.0f
)
52 float hScale
= bounds
.Width() / bitmapBounds
.Width();
53 float vScale
= bounds
.Height() / bitmapBounds
.Height();
55 scale
= std::min(hScale
, vScale
);
58 float width
= bitmapBounds
.Width() * scale
;
59 float height
= bitmapBounds
.Height() * scale
;
61 switch (LayoutAlignment().horizontal
) {
65 bounds
.left
= floorf(bounds
.right
- width
);
68 case B_ALIGN_HORIZONTAL_CENTER
:
69 bounds
.left
= floorf(bounds
.left
70 + (bounds
.Width() - width
) / 2.0f
);
73 switch (LayoutAlignment().vertical
) {
77 bounds
.top
= floorf(bounds
.bottom
- height
);
80 case B_ALIGN_VERTICAL_CENTER
:
81 bounds
.top
= floorf(bounds
.top
82 + (bounds
.Height() - height
) / 2.0f
);
86 bounds
.right
= ceilf(bounds
.left
+ width
);
87 bounds
.bottom
= ceilf(bounds
.top
+ height
);
89 SetDrawingMode(B_OP_OVER
);
90 DrawBitmap(fBitmap
, bitmapBounds
, bounds
, B_FILTER_BITMAP_BILINEAR
);
97 BSize
size(0.0f
, 0.0f
);
99 if (fBitmap
!= NULL
) {
100 BRect bounds
= fBitmap
->Bounds();
101 size
.width
= bounds
.Width();
102 size
.height
= bounds
.Height();
105 return BLayoutUtils::ComposeSize(ExplicitMinSize(), size
);
110 BitmapView::PreferredSize()
112 BSize size
= MinSize();
113 return BLayoutUtils::ComposeSize(ExplicitPreferredSize(), size
);
118 BitmapView::MaxSize()
120 BSize size
= MinSize();
121 return BLayoutUtils::ComposeSize(ExplicitMaxSize(), size
);
126 BitmapView::SetBitmap(SharedBitmap
* bitmap
, SharedBitmap::Size bitmapSize
)
128 if (bitmap
== fReference
&& bitmapSize
== fBitmapSize
)
131 BSize size
= MinSize();
133 fReference
.SetTo(bitmap
);
134 fBitmapSize
= bitmapSize
;
135 fBitmap
= bitmap
->Bitmap(bitmapSize
);
137 BSize newSize
= MinSize();
146 BitmapView::UnsetBitmap()
148 if (fReference
.Get() == NULL
)
160 BitmapView::SetScaleBitmap(bool scaleBitmap
)
162 if (scaleBitmap
== fScaleBitmap
)
165 fScaleBitmap
= scaleBitmap
;
172 BitmapView::DrawBackground(BRect
& bounds
, BRect updateRect
)
174 FillRect(updateRect
, B_SOLID_LOW
);