forked from HaikuArchives/sanity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreviewView.cpp
More file actions
259 lines (196 loc) · 5.82 KB
/
Copy pathPreviewView.cpp
File metadata and controls
259 lines (196 loc) · 5.82 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include "PreviewView.h"
#include "CheckerBitmap.h"
#include "ScannerWindow.h"
PreviewView::PreviewView(BRect geometry)
: BView("PreviewView", B_WILL_DRAW | B_FRAME_EVENTS | B_FULL_UPDATE_ON_RESIZE)
{
m_geometry = geometry;
m_frame = geometry;
m_image_frame = geometry;
m_mouse_button = false;
m_image = NULL;
m_background = new CheckerBitmap(12);
m_move_cursor = new BCursor(B_CURSOR_ID_CROSS_HAIR);
SetViewColor(B_TRANSPARENT_COLOR);
m_background_color = ui_color(B_PANEL_BACKGROUND_COLOR);
}
PreviewView::~PreviewView()
{
delete m_background;
delete m_move_cursor;
}
void PreviewView::AttachedToWindow(void)
{
if (Parent())
m_background_color = Parent()->ViewColor();
}
void PreviewView::FrameResized(float width, float height)
{
}
void PreviewView::MouseDown(BPoint p)
{
BPoint point;
uint32 buttons;
GetMouse(&point, &buttons);
if ( buttons & B_PRIMARY_MOUSE_BUTTON ) {
m_mouse_point_1 = p;
m_mouse_button = true;
be_app->SetCursor(m_move_cursor, true);
SetMouseEventMask(B_POINTER_EVENTS,B_NO_POINTER_HISTORY);
}
}
void PreviewView::MouseUp(BPoint p)
{
BPoint point;
uint32 buttons;
GetMouse(&point, &buttons);
if ( !buttons & B_PRIMARY_MOUSE_BUTTON ) {
m_mouse_button = false;
be_app->SetCursor(B_CURSOR_SYSTEM_DEFAULT, true);
if ( m_mouse_point_1 == p ) {
BMessage *msg = new BMessage(ScannerWindow::PARAM_CHANGED_MSG);
msg->AddRect("rect", m_geometry);
Window()->PostMessage(msg);
}
}
}
void PreviewView::MouseMoved(BPoint p, uint32 transit,const BMessage *message)
{
if (m_mouse_button) {
BRect r = CenterImage();
float kx = r.Width() / m_geometry.Width();
float ky = r.Height() / m_geometry.Height();
BRect new_frame;
new_frame.left = (m_mouse_point_1.x - r.left) / kx;
new_frame.top = (m_mouse_point_1.y - r.top) / ky;
new_frame.right = (p.x - r.left) / kx;
new_frame.bottom = (p.y - r.top) / ky;
BMessage *msg = new BMessage(ScannerWindow::PARAM_CHANGED_MSG);
msg->AddRect("rect", new_frame);
Window()->PostMessage(msg);
}
}
BRect PreviewView::CenterImage()
{
BRect rect(m_geometry);
float width, height;
width = Bounds().Width();
height = Bounds().Height();
if (width == 0 || height == 0)
return rect;
float ratio = width / (rect.Width()+1.0);
if (ratio * (rect.Height()+1.0) <= height) {
rect.right = width-1;
rect.bottom = (int) (ratio * (rect.Height()+1.0))-1;
// center vertically
rect.OffsetBy(0, (int) ((height - rect.Height()) / 2));
} else {
ratio = height / (rect.Height()+1.0);
rect.right = (int) (ratio * (rect.Width()+1.0))-1;
rect.bottom = height-1;
// center horizontally
rect.OffsetBy((int) ((width - rect.Width()) / 2), 0);
}
return rect;
}
void PreviewView::Draw(BRect invalid)
{
SetDrawingMode(B_OP_ALPHA);
SetBlendingMode(B_PIXEL_ALPHA, B_ALPHA_OVERLAY);
BRect r = CenterImage();
SetLowColor(m_background_color);
DrawBorders(r);
rgb_color darker1 = tint_color(m_background_color, B_DARKEN_1_TINT);
rgb_color darker2 = tint_color(m_background_color, B_DARKEN_3_TINT);
SetHighColor(darker2);
StrokeLine(BPoint(r.left + 1, r.bottom + 1), BPoint(r.right + 1, r.bottom + 1));
StrokeLine(BPoint(r.right + 1, r.bottom + 1), BPoint(r.right + 1, r.top + 1));
SetHighColor(darker1);
StrokeLine(BPoint(r.left + 2, r.bottom + 2), BPoint(r.right + 2, r.bottom + 2));
StrokeLine(BPoint(r.right + 2, r.bottom + 2), BPoint(r.right + 2, r.top + 2));
SetHighColor(0, 0, 0, 255);
StrokeRect(r);
r.InsetBy(1, 1);
float kx = r.Width() / m_geometry.Width();
float ky = r.Height() / m_geometry.Height();
float eps = 1.0 * (m_geometry.Width() / 100.0);
if ( !(fabs(m_frame.left - m_geometry.left) >= eps ||
fabs(m_frame.top - m_geometry.top) >= eps ||
fabs(m_frame.right - m_geometry.right) >= eps ||
fabs(m_frame.bottom - m_geometry.bottom) >= eps) ) {
m_frame = m_geometry;
}
SetHighColor(210, 210, 210, 255);
FillRect(r);
BRect rf(m_frame.left * kx, m_frame.top * ky, m_frame.right * kx, m_frame.bottom * ky);
rf.OffsetBy(r.left, r.top);
SetHighColor(255, 255, 255, 255);
FillRect(rf);
if (m_image) {
BRect rif(m_image_frame.left * kx, m_image_frame.top * ky, m_image_frame.right * kx, m_image_frame.bottom * ky);
rif.OffsetBy(r.left, r.top);
DrawBitmap(m_image, m_image->Bounds(), rif, B_FILTER_BITMAP_BILINEAR);
}
if ( m_frame != m_geometry ) {
SetHighColor(200, 200, 200, 176);
FillRect(BRect(r.left, r.top, rf.left - 1, r.bottom));
FillRect(BRect(rf.right + 1, r.top, r.right, r.bottom));
FillRect(BRect(rf.left, r.top, rf.right, rf.top - 1));
FillRect(BRect(rf.left, rf.bottom + 1, rf.right, r.bottom));
SetHighColor(10, 40, 205, 196);
StrokeRect(rf);
float dotR = 3;
FillEllipse(BPoint(rf.left, rf.top), dotR, dotR);
FillEllipse(BPoint(rf.right - 1, rf.top), dotR, dotR);
FillEllipse(BPoint(rf.left, rf.bottom - 1), dotR, dotR);
FillEllipse(BPoint(rf.right - 1, rf.bottom - 1), dotR, dotR);
}
}
void PreviewView::SetBitmap(BBitmap * bm)
{
m_image = bm;
Invalidate();
}
inline BBitmap * PreviewView::GetBitmap()
{
return m_image;
}
void PreviewView::SetGeometry(BRect geometry)
{
if (geometry == m_geometry)
return;
m_geometry = geometry;
Invalidate();
}
void PreviewView::SetFrame(BRect frame)
{
if (frame == m_frame)
return;
m_frame = frame;
Invalidate();
}
BRect PreviewView::GetGeometry(void)
{
return m_geometry;
}
BRect PreviewView::GetFrame(void)
{
return m_frame;
}
void PreviewView::SetImageFrame(void)
{
m_image_frame = m_frame;
Invalidate();
}
void PreviewView::DrawBorders(BRect border)
{
BRect bounds(Bounds());
// top
FillRect(BRect(0, 0, bounds.right, border.top-1), B_SOLID_LOW);
// left
FillRect(BRect(0, border.top, border.left-1, border.bottom), B_SOLID_LOW);
// right
FillRect(BRect(border.right+1, border.top, bounds.right, border.bottom), B_SOLID_LOW);
// bottom
FillRect(BRect(0, border.bottom+1, bounds.right, bounds.bottom), B_SOLID_LOW);
}