/
PreviewView.cpp
148 lines (118 loc) · 2.64 KB
/
PreviewView.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "PreviewView.h"
#include <cstdio>
#include <Alignment.h>
#include <Bitmap.h>
#include <GroupLayout.h>
#include <GroupLayoutBuilder.h>
#include <LayoutBuilder.h>
#include <LayoutUtils.h>
#include <Screen.h>
#include <StringView.h>
class BitmapView : public BView {
public:
BitmapView();
virtual ~BitmapView();
virtual void Draw(BRect update);
virtual BSize MinSize();
};
PreviewView::PreviewView()
:
BView("Rect View", B_WILL_DRAW),
fBitmapView(NULL),
fTop(NULL),
fLeft(NULL),
fRight(NULL),
fBottom(NULL)
{
fCoordRect = BRect(10, 10, 20, 20);
fChanged = true;
SetLayout(new BGroupLayout(B_HORIZONTAL));
SetExplicitAlignment(BAlignment(B_ALIGN_HORIZONTAL_CENTER,
B_ALIGN_VERTICAL_CENTER));
AddChild(BGroupLayoutBuilder(B_VERTICAL)
.AddGroup(B_HORIZONTAL)
.Add(new BStringView("spacer", ""))
.Add(fTop = new BStringView("top", "top"))
.Add(new BStringView("spacer", ""))
.End()
.AddGroup(B_HORIZONTAL)
.Add(fLeft = new BStringView("left", "left"))
.Add(fBitmapView = new BitmapView())
.Add(fRight = new BStringView("right", "right"))
.End()
.AddGroup(B_HORIZONTAL)
.Add(new BStringView("spacer", ""))
.Add(fBottom = new BStringView("bottom", "bottom"))
.Add(new BStringView("spacer", ""))
.End()
);
}
void
PreviewView::AttachedToWindow()
{
BView::AttachedToWindow();
SetViewColor(Parent()->ViewColor());
UpdateBitmap();
}
void
PreviewView::SetRect(BRect rect)
{
if (fCoordRect == rect)
return;
fCoordRect = rect;
char str[16];
snprintf(str, sizeof(str), "%d", (int)rect.left);
fLeft->SetText(str);
snprintf(str, sizeof(str), "%d", (int)rect.top);
fTop->SetText(str);
snprintf(str, sizeof(str), "%d", (int)rect.right);
fRight->SetText(str);
snprintf(str, sizeof(str), "%d", (int)rect.bottom);
fBottom->SetText(str);
fChanged = true;
UpdateBitmap();
}
void
PreviewView::UpdateBitmap()
{
if (Window() == NULL)
printf("window is null\n");
if (fChanged && Window() != NULL) {
BBitmap *bitmap;
fChanged = false;
BScreen screen(Window());
screen.GetBitmap(&bitmap, false, &fCoordRect);
if (bitmap != NULL) {
fBitmapView->SetViewBitmap(bitmap, bitmap->Bounds(),
fBitmapView->Bounds(),
B_FOLLOW_TOP|B_FOLLOW_LEFT, 0);
Invalidate();
}
}
}
BSize
PreviewView::MinSize()
{
return BLayoutUtils::ComposeSize(ExplicitMinSize(), BSize(160, 100));
}
// BitmapView
BitmapView::BitmapView()
:
BView("bitmap view", B_WILL_DRAW)
{
}
BitmapView::~BitmapView()
{
}
void
BitmapView::Draw(BRect rect)
{
BView::Draw(rect);
SetHighColor(0, 0, 0);
StrokeRect(Bounds());
}
BSize
BitmapView::MinSize()
{
return BLayoutUtils::ComposeSize(ExplicitMinSize(), BSize(100, 60));
}