81 lines
1.7 KiB
C++
81 lines
1.7 KiB
C++
|
#include "image_view.h"
|
||
|
|
||
|
|
||
|
LImageView::LImageView(const char *name, BBitmap *bitmap, BAlignment align) : BView(name)
|
||
|
{
|
||
|
SetBitmap(bitmap);
|
||
|
SetAlignment(align);
|
||
|
}
|
||
|
void LImageView::SetBitmap(BBitmap *bitmap) {
|
||
|
this->bitmap = bitmap;
|
||
|
Invalidate();
|
||
|
}
|
||
|
void LImageView::SetAlignment(BAlignment align) {
|
||
|
this->align = align;
|
||
|
Invalidate();
|
||
|
}
|
||
|
BBitmap *LImageView::Bitmap() {
|
||
|
return bitmap;
|
||
|
}
|
||
|
BAlignment LImageView::Alignment() {
|
||
|
return align;
|
||
|
}
|
||
|
void LImageView::Draw(BRect updateRect) {
|
||
|
if (bitmap == NULL) return; // Handle null image.
|
||
|
int x = 0, y = 0;
|
||
|
int mw, mh;
|
||
|
int bw, bh;
|
||
|
bw = bitmap->Bounds().Width();
|
||
|
bh = bitmap->Bounds().Height();
|
||
|
mw = Bounds().Width();
|
||
|
mh = Bounds().Height();
|
||
|
switch (align.Horizontal()) {
|
||
|
case B_ALIGN_LEFT: {
|
||
|
x = 0;
|
||
|
} break;
|
||
|
case B_ALIGN_HORIZONTAL_CENTER: {
|
||
|
x = (mw - bw) / 2;
|
||
|
} break;
|
||
|
case B_ALIGN_RIGHT: {
|
||
|
x = mw - bw;
|
||
|
} break;
|
||
|
}
|
||
|
switch (align.Vertical()) {
|
||
|
case B_ALIGN_TOP: {
|
||
|
y = 0;
|
||
|
} break;
|
||
|
case B_ALIGN_VERTICAL_CENTER: {
|
||
|
y = (mh - bh) / 2;
|
||
|
} break;
|
||
|
case B_ALIGN_BOTTOM: {
|
||
|
y = mh - bh;
|
||
|
} break;
|
||
|
}
|
||
|
BRect bitmap_rect(0, 0, bw, bh);
|
||
|
BRect draw_rect(x, y, bw + x, bh + y);
|
||
|
int left = updateRect.left;
|
||
|
int right = updateRect.right;
|
||
|
int top = updateRect.top;
|
||
|
int bottom = updateRect.bottom;
|
||
|
int x2 = x + bw;
|
||
|
int y2 = y + bh;
|
||
|
if (x > right || x2 < left || y > bottom || y2 < top) return;
|
||
|
if (x < left) {
|
||
|
bitmap_rect.left = left - x;
|
||
|
draw_rect.left = left;
|
||
|
}
|
||
|
if (x2 > right) {
|
||
|
bitmap_rect.right = right - x;
|
||
|
draw_rect.right = right;
|
||
|
}
|
||
|
if (y < top) {
|
||
|
bitmap_rect.top = top - y;
|
||
|
draw_rect.top = top;
|
||
|
}
|
||
|
if (y2 > bottom) {
|
||
|
bitmap_rect.bottom = bottom - y;
|
||
|
draw_rect.bottom = bottom;
|
||
|
}
|
||
|
DrawBitmap(bitmap, bitmap_rect, draw_rect);
|
||
|
}
|