forked from phoudoin/sanity
-
Notifications
You must be signed in to change notification settings - Fork 4
/
BitmapView.cpp
122 lines (91 loc) · 2.22 KB
/
BitmapView.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "BitmapView.h"
#include "CheckerBitmap.h"
BitmapView::BitmapView(BRect frame, BBitmap * bm)
: BView(frame, "BitmapView",
B_FOLLOW_ALL_SIDES, B_WILL_DRAW|B_FRAME_EVENTS)
{
SetViewColor(B_TRANSPARENT_COLOR);
m_image = bm;
m_background = NULL;
}
BitmapView::~BitmapView()
{
delete m_background;
}
void BitmapView::AttachedToWindow(void)
{
m_background = new CheckerBitmap(12);
if ( m_background )
SetViewBitmap(m_background);
SetupScrollbars();
}
void BitmapView::FrameResized(float width, float height)
{
SetupScrollbars();
}
void BitmapView::Draw(BRect invalid)
{
if (! m_image)
return;
if (LockLooper()) {
switch ( m_image->ColorSpace() ) {
case B_RGBA32:
case B_RGBA32_BIG:
case B_RGBA15:
case B_RGBA15_BIG:
SetDrawingMode(B_OP_ALPHA);
SetBlendingMode(B_PIXEL_ALPHA, B_ALPHA_OVERLAY);
break;
default:
SetDrawingMode(B_OP_COPY);
break;
};
DrawBitmap(m_image, invalid, invalid);
UnlockLooper();
}
}
void BitmapView::SetBitmap(BBitmap * bm)
{
m_image = bm;
SetupScrollbars();
Invalidate();
}
inline BBitmap * BitmapView::GetBitmap()
{
return m_image;
}
void BitmapView::SetupScrollbars(void)
{
BRect bounds;
BScrollBar * sb;
bounds = Bounds();
float myPixelWidth = bounds.Width();
float myPixelHeight = bounds.Height();
float maxWidth = 1, maxHeight = 1;
if( m_image != NULL && Window()->Lock()) {
// get max size of image
maxWidth = m_image->Bounds().right;
maxHeight = m_image->Bounds().bottom;
Window()->Unlock();
};
float propW = myPixelWidth/maxWidth;
float propH = myPixelHeight/maxHeight;
float rangeW = maxWidth - myPixelWidth;
float rangeH = maxHeight - myPixelHeight;
if(rangeW < 0) rangeW = 0;
if(rangeH < 0) rangeH = 0;
if ((sb=ScrollBar(B_HORIZONTAL))!=NULL) {
sb->SetProportion(propW);
sb->SetRange(0,rangeW);
// Steps are 1/8 visible window for small steps
// and 1/2 visible window for large steps
sb->SetSteps(myPixelWidth / 8.0, myPixelWidth / 2.0);
}
if ((sb=ScrollBar(B_VERTICAL))!=NULL) {
sb->SetProportion(propH);
sb->SetRange(0,rangeH);
// Steps are 1/8 visible window for small steps
// and 1/2 visible window for large steps
sb->SetSteps(myPixelHeight / 8.0, myPixelHeight / 2.0);
}
}