2010-05-07 04:47:10 -05:00
|
|
|
/*
|
2010-05-27 20:08:18 -05:00
|
|
|
* Copyright 2009-2010, Pier Luigi Fiorini. All rights reserved.
|
2010-05-07 04:47:10 -05:00
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Pier Luigi Fiorini, pierluigi.fiorini@gmail.com
|
|
|
|
*/
|
|
|
|
|
2010-06-15 12:09:31 -05:00
|
|
|
#include <new>
|
|
|
|
|
2010-05-07 04:47:10 -05:00
|
|
|
#include <Bitmap.h>
|
2010-05-20 13:11:32 -05:00
|
|
|
#include <LayoutUtils.h>
|
2010-05-07 04:47:10 -05:00
|
|
|
#include <TranslationUtils.h>
|
|
|
|
|
|
|
|
#include "BitmapView.h"
|
|
|
|
|
2010-05-27 20:08:18 -05:00
|
|
|
const float kMinWidth = 32.0f;
|
|
|
|
const float kMinHeight = 32.0f;
|
2010-05-20 13:11:32 -05:00
|
|
|
|
2010-05-07 04:47:10 -05:00
|
|
|
|
|
|
|
BitmapView::BitmapView(const char* name, uint32 flags)
|
2010-05-20 13:11:32 -05:00
|
|
|
:
|
|
|
|
BView(name, flags | B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE),
|
2010-05-07 04:47:10 -05:00
|
|
|
fBitmap(NULL),
|
2021-07-27 20:48:03 -05:00
|
|
|
fIsSquare(false),
|
2010-05-20 13:11:32 -05:00
|
|
|
fWidth(kMinWidth),
|
|
|
|
fHeight(kMinHeight)
|
2010-05-07 04:47:10 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BitmapView::~BitmapView()
|
|
|
|
{
|
|
|
|
delete fBitmap;
|
|
|
|
fBitmap = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-29 23:24:53 -05:00
|
|
|
void
|
|
|
|
BitmapView::AttachedToWindow()
|
|
|
|
{
|
|
|
|
// Set view color to parent's view color
|
2012-11-28 09:20:17 -06:00
|
|
|
if (Parent() != NULL)
|
|
|
|
SetViewColor(Parent()->ViewColor());
|
|
|
|
else
|
|
|
|
SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
|
2010-05-29 23:24:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-07 04:47:10 -05:00
|
|
|
status_t
|
|
|
|
BitmapView::InitCheck()
|
|
|
|
{
|
|
|
|
if (fBitmap != NULL)
|
|
|
|
return B_OK;
|
|
|
|
return B_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-20 13:11:32 -05:00
|
|
|
BBitmap*
|
|
|
|
BitmapView::Bitmap() const
|
|
|
|
{
|
|
|
|
return fBitmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-15 12:09:31 -05:00
|
|
|
status_t
|
2010-05-27 19:47:00 -05:00
|
|
|
BitmapView::SetBitmap(const char* filename)
|
|
|
|
{
|
|
|
|
delete fBitmap;
|
2010-06-15 12:09:31 -05:00
|
|
|
|
2010-05-27 19:47:00 -05:00
|
|
|
fBitmap = BTranslationUtils::GetBitmap(filename);
|
2010-06-15 12:09:31 -05:00
|
|
|
if (fBitmap == NULL)
|
|
|
|
return B_ERROR;
|
|
|
|
|
|
|
|
return B_OK;
|
2010-05-27 19:47:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-15 12:09:31 -05:00
|
|
|
status_t
|
|
|
|
BitmapView::SetBitmap(const BBitmap* bitmap)
|
2010-05-07 04:47:10 -05:00
|
|
|
{
|
|
|
|
delete fBitmap;
|
2010-06-15 12:09:31 -05:00
|
|
|
fBitmap = NULL;
|
2010-05-07 04:47:10 -05:00
|
|
|
|
2010-06-15 12:09:31 -05:00
|
|
|
if (bitmap != NULL) {
|
|
|
|
fBitmap = new(std::nothrow) BBitmap(bitmap);
|
|
|
|
if (fBitmap == NULL)
|
|
|
|
return B_NO_MEMORY;
|
|
|
|
if (fBitmap->InitCheck() != B_OK)
|
|
|
|
return fBitmap->InitCheck();
|
2010-05-07 04:47:10 -05:00
|
|
|
|
2010-06-15 12:09:31 -05:00
|
|
|
fWidth = fBitmap->Bounds().Width();
|
|
|
|
fHeight = fBitmap->Bounds().Height();
|
2010-05-20 13:11:32 -05:00
|
|
|
Invalidate();
|
2010-05-07 04:47:10 -05:00
|
|
|
}
|
2010-06-15 12:09:31 -05:00
|
|
|
|
|
|
|
return B_OK;
|
2010-05-07 04:47:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-27 20:48:03 -05:00
|
|
|
void
|
|
|
|
BitmapView::SetSquare(bool isSquare)
|
|
|
|
{
|
|
|
|
fIsSquare = isSquare;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-07 04:47:10 -05:00
|
|
|
BSize
|
|
|
|
BitmapView::MinSize()
|
|
|
|
{
|
2010-05-20 13:11:32 -05:00
|
|
|
return BLayoutUtils::ComposeSize(ExplicitMinSize(),
|
|
|
|
BSize(kMinWidth, kMinHeight));
|
2010-05-07 04:47:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BSize
|
|
|
|
BitmapView::MaxSize()
|
|
|
|
{
|
2010-05-20 13:11:32 -05:00
|
|
|
return BLayoutUtils::ComposeSize(ExplicitMaxSize(),
|
|
|
|
BSize(fWidth, fHeight));
|
2010-05-07 04:47:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BSize
|
|
|
|
BitmapView::PreferredSize()
|
|
|
|
{
|
2010-05-20 13:11:32 -05:00
|
|
|
return BLayoutUtils::ComposeSize(ExplicitPreferredSize(),
|
|
|
|
BSize(fWidth, fHeight));
|
2010-05-07 04:47:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
BitmapView::Draw(BRect frame)
|
|
|
|
{
|
2010-06-15 12:09:31 -05:00
|
|
|
if (fBitmap == NULL)
|
2010-05-20 13:11:32 -05:00
|
|
|
return;
|
|
|
|
|
2010-05-07 04:47:10 -05:00
|
|
|
SetDrawingMode(B_OP_ALPHA);
|
|
|
|
SetBlendingMode(B_PIXEL_ALPHA, B_ALPHA_OVERLAY);
|
|
|
|
|
2010-05-20 13:11:32 -05:00
|
|
|
DrawBitmap(fBitmap, fBitmap->Bounds(),
|
2021-07-27 20:48:03 -05:00
|
|
|
_ViewBounds(), B_FILTER_BITMAP_BILINEAR);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BRect
|
|
|
|
BitmapView::_ViewBounds()
|
|
|
|
{
|
|
|
|
BRect bounds = Bounds();
|
|
|
|
if (fIsSquare == false || bounds.Height() == bounds.Width())
|
|
|
|
return bounds;
|
|
|
|
|
|
|
|
BPoint lt = bounds.LeftTop();
|
|
|
|
BPoint rb = bounds.RightBottom();
|
|
|
|
float diff = 0.0;
|
|
|
|
|
|
|
|
if (bounds.Height() > bounds.Width()) {
|
|
|
|
diff = bounds.Height() - bounds.Width();
|
|
|
|
lt -= BPoint(0.0, diff / 2);
|
|
|
|
rb += BPoint(0.0, diff / 2);
|
|
|
|
}
|
|
|
|
else if (bounds.Width() > bounds.Height()) {
|
|
|
|
diff = bounds.Height() - bounds.Width();
|
|
|
|
lt -= BPoint(diff / 2, 0.0);
|
|
|
|
rb += BPoint(diff / 2, 0.0);
|
|
|
|
}
|
|
|
|
return BRect(lt, rb);
|
2010-05-07 04:47:10 -05:00
|
|
|
}
|