#include "image_view.h" #include LImageView::LImageView(const char *name, BBitmap *bitmap, BAlignment align) : BView(name, B_SUPPORTS_LAYOUT|B_FRAME_EVENTS|B_WILL_DRAW) { 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(); int iw = bw, ih = bh; mw = Bounds().Width(); mh = Bounds().Height(); float wr = ((float)mw) / ((float)bw); float hr = ((float)mh) / ((float)bh); if (wr < hr) { iw *= wr; ih *= wr; } else { iw *= hr; ih *= hr; } switch (align.Horizontal()) { case B_ALIGN_LEFT: { x = 0; } break; case B_ALIGN_HORIZONTAL_CENTER: { x = (mw - iw) / 2; } break; case B_ALIGN_RIGHT: { x = mw - iw; } break; } switch (align.Vertical()) { case B_ALIGN_TOP: { y = 0; } break; case B_ALIGN_VERTICAL_CENTER: { y = (mh - ih) / 2; } break; case B_ALIGN_BOTTOM: { y = mh - ih; } break; } BRect bitmap_rect(0, 0, bw, bh); BRect draw_rect(x, y, iw + x, ih + 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; printf("Bitmap rect: (%f, %f) -> (%f, %f)\nView rect: (%f, %f) -> (%f, %f)\n", bitmap_rect.left, bitmap_rect.top, bitmap_rect.right, bitmap_rect.bottom, draw_rect.left, draw_rect.top, draw_rect.right, draw_rect.bottom); DrawBitmap(bitmap, bitmap_rect, draw_rect); } void LImageView::GetPreferredSize(float *w, float *h) { if (!bitmap) { *w = 0; *h = 0; return; } *w = bitmap->Bounds().Width(); *h = bitmap->Bounds().Height(); }