repository_infos: Enable automatic updates on the main Haiku repostiory.
[haiku.git] / src / apps / haikudepot / ui_generic / BitmapView.cpp
blobe00e1227013a1242e9bd7a932bc17aee3a514eef
1 /*
2 * Copyright 2013, Stephan Aßmus <superstippi@gmx.de>.
3 * All rights reserved. Distributed under the terms of the MIT License.
4 */
7 #include "BitmapView.h"
9 #include <algorithm>
11 #include <Bitmap.h>
12 #include <LayoutUtils.h>
15 BitmapView::BitmapView(const char* name)
17 BView(name, B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE),
18 fBitmap(NULL),
19 fScaleBitmap(true)
24 BitmapView::~BitmapView()
29 void
30 BitmapView::AllAttached()
32 AdoptParentColors();
36 void
37 BitmapView::Draw(BRect updateRect)
39 BRect bounds(Bounds());
40 DrawBackground(bounds, updateRect);
42 if (fBitmap == NULL)
43 return;
45 BRect bitmapBounds = fBitmap->Bounds();
46 if (bitmapBounds.Width() <= 0.0f || bitmapBounds.Height() <= 0.0f)
47 return;
49 float scale = 1.0f;
51 if (fScaleBitmap) {
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) {
62 case B_ALIGN_LEFT:
63 break;
64 case B_ALIGN_RIGHT:
65 bounds.left = floorf(bounds.right - width);
66 break;
67 default:
68 case B_ALIGN_HORIZONTAL_CENTER:
69 bounds.left = floorf(bounds.left
70 + (bounds.Width() - width) / 2.0f);
71 break;
73 switch (LayoutAlignment().vertical) {
74 case B_ALIGN_TOP:
75 break;
76 case B_ALIGN_BOTTOM:
77 bounds.top = floorf(bounds.bottom - height);
78 break;
79 default:
80 case B_ALIGN_VERTICAL_CENTER:
81 bounds.top = floorf(bounds.top
82 + (bounds.Height() - height) / 2.0f);
83 break;
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);
94 BSize
95 BitmapView::MinSize()
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);
109 BSize
110 BitmapView::PreferredSize()
112 BSize size = MinSize();
113 return BLayoutUtils::ComposeSize(ExplicitPreferredSize(), size);
117 BSize
118 BitmapView::MaxSize()
120 BSize size = MinSize();
121 return BLayoutUtils::ComposeSize(ExplicitMaxSize(), size);
125 void
126 BitmapView::SetBitmap(SharedBitmap* bitmap, SharedBitmap::Size bitmapSize)
128 if (bitmap == fReference && bitmapSize == fBitmapSize)
129 return;
131 BSize size = MinSize();
133 fReference.SetTo(bitmap);
134 fBitmapSize = bitmapSize;
135 fBitmap = bitmap->Bitmap(bitmapSize);
137 BSize newSize = MinSize();
138 if (size != newSize)
139 InvalidateLayout();
141 Invalidate();
145 void
146 BitmapView::UnsetBitmap()
148 if (fReference.Get() == NULL)
149 return;
151 fBitmap = NULL;
152 fReference.Unset();
154 InvalidateLayout();
155 Invalidate();
159 void
160 BitmapView::SetScaleBitmap(bool scaleBitmap)
162 if (scaleBitmap == fScaleBitmap)
163 return;
165 fScaleBitmap = scaleBitmap;
167 Invalidate();
171 void
172 BitmapView::DrawBackground(BRect& bounds, BRect updateRect)
174 FillRect(updateRect, B_SOLID_LOW);