-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathSelectionWindow.cpp
557 lines (451 loc) · 12.1 KB
/
SelectionWindow.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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/*
* Copyright 2013-2023, Stefano Ceccherini <stefano.ceccherini@gmail.com>
* All rights reserved. Distributed under the terms of the MIT license.
*/
#include "SelectionWindow.h"
#include "Constants.h"
#include "Settings.h"
#include "Utils.h"
#include <Bitmap.h>
#include <Cursor.h>
#include <Screen.h>
#include <String.h>
const char *kInfoRegionMode = "Click and drag to select, press ENTER to confirm";
const char *kInfoWindowMode = "Click to select a window";
const static rgb_color kSelectionColor = { 0, 0, 128, 100 };
const static float kDraggerSize = 16;
const static float kDraggerSpacing = 4;
const static float kDraggerFullSize = kDraggerSize + kDraggerSpacing;
class SelectionView : public BView {
public:
SelectionView(BRect frame, const char *name, const char* text = NULL);
virtual void Draw(BRect updateRect);
virtual BRect SelectionRect() const;
protected:
BString fText;
};
class SelectionViewRegion : public SelectionView {
public:
enum drag_mode {
DRAG_MODE_NONE = 0,
DRAG_MODE_SELECT = 1,
DRAG_MODE_MOVE = 2,
DRAG_MODE_RESIZE_LEFT_TOP = 3,
DRAG_MODE_RESIZE_RIGHT_TOP = 4,
DRAG_MODE_RESIZE_LEFT_BOTTOM = 5,
DRAG_MODE_RESIZE_RIGHT_BOTTOM = 6,
};
enum which_dragger {
DRAGGER_NONE = -1,
DRAGGER_LEFT_TOP = 0,
DRAGGER_RIGHT_TOP = 1,
DRAGGER_LEFT_BOTTOM = 2,
DRAGGER_RIGHT_BOTTOM = 3
};
SelectionViewRegion(BRect frame, const char *name);
virtual ~SelectionViewRegion();
virtual void MouseDown(BPoint where);
virtual void MouseUp(BPoint where);
virtual void MouseMoved(BPoint where, uint32 code, const BMessage *message);
virtual void KeyDown(const char* bytes, int32 numBytes);
virtual void Draw(BRect updateRect);
virtual BRect SelectionRect() const;
BRect LeftTopDragger() const;
BRect RightTopDragger() const;
BRect LeftBottomDragger() const;
BRect RightBottomDragger() const;
private:
void _DrawDraggers();
int _MouseOnDragger(BPoint where) const;
BPoint fSelectionStart;
BPoint fSelectionEnd;
BPoint fCurrentMousePosition;
BRect fStringRect;
int fDragMode;
BCursor* fCursorNWSE;
BCursor* fCursorNESW;
BCursor* fCursorGrab;
BCursor* fCursorSelect;
};
class SelectionViewWindow : public SelectionView {
public:
SelectionViewWindow(BRect frame, const char* name);
virtual void MouseMoved(BPoint where, uint32 code, const BMessage *message);
virtual void MouseUp(BPoint where);
virtual void Draw(BRect updateRect);
virtual BRect SelectionRect() const;
private:
BObjectList<BRect> fFrameList;
BRect fHighlightFrame;
BRect HitTestFrame(BPoint& where);
};
// SelectionView
SelectionView::SelectionView(BRect frame, const char *name, const char* text)
:
BView(frame, name, B_FOLLOW_NONE, B_WILL_DRAW),
fText(text)
{
SetFontSize(30);
}
void
SelectionView::Draw(BRect updateRect)
{
PushState();
SetDrawingMode(B_OP_OVER);
SetHighColor(kRed);
float width = StringWidth(fText.String());
BPoint point;
point.x = (Bounds().Width() - width) / 2;
point.y = Bounds().Height() / 2;
DrawString(fText.String(), point);
PopState();
}
BRect
SelectionView::SelectionRect() const
{
return BRect(0, 0, -1, -1);
}
// SelectionViewRegion
SelectionViewRegion::SelectionViewRegion(BRect frame, const char *name)
:
SelectionView(frame, name, kInfoRegionMode),
fSelectionStart(-1, -1),
fSelectionEnd(-1, -1),
fCursorNWSE(NULL),
fCursorNESW(NULL),
fCursorGrab(NULL),
fCursorSelect(NULL)
{
fDragMode = DRAG_MODE_NONE;
fCursorNWSE = new BCursor(B_CURSOR_ID_RESIZE_NORTH_WEST_SOUTH_EAST);
fCursorNESW = new BCursor(B_CURSOR_ID_RESIZE_NORTH_EAST_SOUTH_WEST);
fCursorGrab = new BCursor(B_CURSOR_ID_GRABBING);
fCursorSelect = new BCursor(B_CURSOR_ID_CROSS_HAIR);
}
SelectionViewRegion::~SelectionViewRegion()
{
delete fCursorNWSE;
delete fCursorNESW;
delete fCursorGrab;
delete fCursorSelect;
}
void
SelectionViewRegion::MouseDown(BPoint where)
{
SelectionView::MouseDown(where);
if (SelectionRect().Contains(where))
fDragMode = DRAG_MODE_MOVE;
else {
switch (_MouseOnDragger(where)) {
case DRAGGER_LEFT_TOP:
fDragMode = DRAG_MODE_RESIZE_LEFT_TOP;
break;
case DRAGGER_RIGHT_TOP:
fDragMode = DRAG_MODE_RESIZE_RIGHT_TOP;
break;
case DRAGGER_LEFT_BOTTOM:
fDragMode = DRAG_MODE_RESIZE_LEFT_BOTTOM;
break;
case DRAGGER_RIGHT_BOTTOM:
fDragMode = DRAG_MODE_RESIZE_RIGHT_BOTTOM;
break;
default:
Invalidate();
fDragMode = DRAG_MODE_SELECT;
fSelectionStart = where;
fSelectionEnd = where;
break;
}
}
}
void
SelectionViewRegion::MouseMoved(BPoint where, uint32 code, const BMessage *message)
{
if (fDragMode != DRAG_MODE_NONE) {
BRect selectionRect = SelectionRect();
float xOffset = where.x - fCurrentMousePosition.x;
float yOffset = where.y - fCurrentMousePosition.y;
switch (fDragMode) {
case DRAG_MODE_SELECT:
{
SetViewCursor(fCursorSelect);
fSelectionEnd = where;
break;
}
case DRAG_MODE_MOVE:
{
SetViewCursor(fCursorGrab);
fSelectionStart.x += xOffset;
fSelectionStart.y += yOffset;
fSelectionEnd.x += xOffset;
fSelectionEnd.y += yOffset;
break;
}
case DRAG_MODE_RESIZE_LEFT_TOP:
{
SetViewCursor(fCursorNWSE);
fSelectionStart.x += xOffset;
fSelectionStart.y += yOffset;
break;
}
case DRAG_MODE_RESIZE_RIGHT_TOP:
{
SetViewCursor(fCursorNESW);
fSelectionEnd.x += xOffset;
fSelectionStart.y += yOffset;
break;
}
case DRAG_MODE_RESIZE_LEFT_BOTTOM:
{
SetViewCursor(fCursorNESW);
fSelectionStart.x += xOffset;
fSelectionEnd.y += yOffset;
break;
}
case DRAG_MODE_RESIZE_RIGHT_BOTTOM:
{
SetViewCursor(fCursorNWSE);
fSelectionEnd.x += xOffset;
fSelectionEnd.y += yOffset;
break;
}
default:
SetViewCursor(B_CURSOR_SYSTEM_DEFAULT);
break;
}
BRect newSelection = SelectionRect();
BRect invalidateRect = (selectionRect | newSelection);
invalidateRect.InsetBySelf(-(kDraggerSize + kDraggerSpacing), -(kDraggerSize + kDraggerSpacing));
Invalidate(invalidateRect);
} else
SetViewCursor(B_CURSOR_SYSTEM_DEFAULT);
SelectionView::MouseMoved(where, code, message);
fCurrentMousePosition = where;
}
void
SelectionViewRegion::MouseUp(BPoint where)
{
SetViewCursor(B_CURSOR_SYSTEM_DEFAULT);
if (fDragMode == DRAG_MODE_SELECT)
fSelectionEnd = where;
fDragMode = DRAG_MODE_NONE;
// Sanitize the selection rect (fSelectionStart < fSelectionEnd)
BRect selectionRect = SelectionRect();
fSelectionStart = selectionRect.LeftTop();
fSelectionEnd = selectionRect.RightBottom();
SelectionView::MouseUp(where);
}
void
SelectionViewRegion::KeyDown(const char* bytes, int32 numBytes)
{
switch (bytes[0]) {
case B_ENTER:
Window()->PostMessage(B_QUIT_REQUESTED);
break;
case B_ESCAPE:
fSelectionStart = BPoint(-1, -1);
fSelectionEnd = BPoint(-1, -1);
Window()->PostMessage(B_QUIT_REQUESTED);
break;
default:
SelectionView::KeyDown(bytes, numBytes);
break;
}
}
void
SelectionViewRegion::Draw(BRect updateRect)
{
SelectionView::Draw(updateRect);
if (SelectionRect().IsValid()) {
BRect selection = SelectionRect();
SetDrawingMode(B_OP_ALPHA);
SetHighColor(kSelectionColor);
FillRect(selection);
BString sizeString;
sizeString << selection.IntegerWidth() << " x " << selection.IntegerHeight();
float stringWidth = StringWidth(sizeString.String());
BPoint position;
position.x = (selection.Width() - stringWidth) / 2;
position.y = selection.Height() / 2;
position += selection.LeftTop();
SetHighColor(kBlack);
SetLowColor(kBlack);
DrawString(sizeString.String(), position);
}
_DrawDraggers();
}
BRect
SelectionViewRegion::SelectionRect() const
{
if (fSelectionStart == BPoint(-1, -1)
&& fSelectionEnd == BPoint(-1, -1))
return BRect(0, 0, -1, -1);
BRect rect;
rect.SetLeftTop(BPoint(min_c(fSelectionStart.x, fSelectionEnd.x),
min_c(fSelectionStart.y, fSelectionEnd.y)));
rect.SetRightBottom(BPoint(max_c(fSelectionStart.x, fSelectionEnd.x),
max_c(fSelectionStart.y, fSelectionEnd.y)));
return rect;
}
BRect
SelectionViewRegion::LeftTopDragger() const
{
BPoint leftTop(min_c(fSelectionStart.x, fSelectionEnd.x),
min_c(fSelectionStart.y, fSelectionEnd.y));
return BRect(leftTop + BPoint(-kDraggerFullSize, -kDraggerFullSize),
leftTop + BPoint(-kDraggerSpacing, -kDraggerSpacing));
}
BRect
SelectionViewRegion::RightTopDragger() const
{
BPoint leftTop(max_c(fSelectionStart.x, fSelectionEnd.x),
min_c(fSelectionStart.y, fSelectionEnd.y));
return BRect(leftTop + BPoint(kDraggerSpacing, -kDraggerFullSize),
leftTop + BPoint(kDraggerFullSize, -kDraggerSpacing));
}
BRect
SelectionViewRegion::LeftBottomDragger() const
{
BPoint leftTop(min_c(fSelectionStart.x, fSelectionEnd.x),
max_c(fSelectionStart.y, fSelectionEnd.y));
return BRect(leftTop + BPoint(-kDraggerFullSize, kDraggerSpacing),
leftTop + BPoint(-kDraggerSpacing, kDraggerFullSize));
}
BRect
SelectionViewRegion::RightBottomDragger() const
{
BPoint leftTop(max_c(fSelectionStart.x, fSelectionEnd.x),
max_c(fSelectionStart.y, fSelectionEnd.y));
return BRect(leftTop + BPoint(kDraggerSpacing, kDraggerSpacing),
leftTop + BPoint(kDraggerFullSize, kDraggerFullSize));
}
void
SelectionViewRegion::_DrawDraggers()
{
if (fSelectionStart == BPoint(-1, -1)
&& fSelectionEnd == BPoint(-1, -1))
return;
PushState();
SetHighColor(0, 0, 0);
StrokeRect(LeftTopDragger(), B_SOLID_HIGH);
StrokeRect(LeftBottomDragger(), B_SOLID_HIGH);
StrokeRect(RightTopDragger(), B_SOLID_HIGH);
StrokeRect(RightBottomDragger(), B_SOLID_HIGH);
PopState();
}
int
SelectionViewRegion::_MouseOnDragger(BPoint point) const
{
if (LeftTopDragger().Contains(point))
return DRAGGER_LEFT_TOP;
else if (RightTopDragger().Contains(point))
return DRAGGER_RIGHT_TOP;
else if (LeftBottomDragger().Contains(point))
return DRAGGER_LEFT_BOTTOM;
else if (RightBottomDragger().Contains(point))
return DRAGGER_RIGHT_BOTTOM;
return DRAGGER_NONE;
}
// SelectionViewWindow
SelectionViewWindow::SelectionViewWindow(BRect frame, const char *name)
:
SelectionView(frame, name, kInfoWindowMode)
{
GetWindowsFrameList(fFrameList, Settings::Current().WindowFrameEdgeSize());
}
void
SelectionViewWindow::MouseMoved(BPoint where, uint32 code, const BMessage *message)
{
BRect frame = HitTestFrame(where);
if (frame.IsValid())
fHighlightFrame = frame;
else
fHighlightFrame.Set(0, 0, -1, -1);
Invalidate();
}
void
SelectionViewWindow::MouseUp(BPoint where)
{
Window()->PostMessage(B_QUIT_REQUESTED);
}
void
SelectionViewWindow::Draw(BRect updateRect)
{
SelectionView::Draw(updateRect);
if (fHighlightFrame.IsValid()) {
SetDrawingMode(B_OP_ALPHA);
SetHighColor(kSelectionColor);
FillRect(fHighlightFrame);
}
}
BRect
SelectionViewWindow::SelectionRect() const
{
return fHighlightFrame;
}
BRect
SelectionViewWindow::HitTestFrame(BPoint& where)
{
int32 count = fFrameList.CountItems();
for (int32 i = 0; i < count; i++) {
BRect *frame = fFrameList.ItemAt(i);
if (frame->Contains(where))
return *frame;
}
return BRect(0, 0, -1, -1);
}
// SelectionWindow
SelectionWindow::SelectionWindow(int mode, BMessenger& target, uint32 command)
:
BWindow(BScreen().Frame(), "Area Window", window_type(1026),
B_ASYNCHRONOUS_CONTROLS|B_NOT_RESIZABLE|B_NOT_CLOSABLE|B_NOT_ZOOMABLE)
{
if (mode == REGION)
fView = new SelectionViewRegion(Bounds(), "Selection view");
else
fView = new SelectionViewWindow(Bounds(), "Selection view");
AddChild(fView);
SetTarget(target);
SetCommand(command);
}
void
SelectionWindow::Show()
{
BBitmap *bitmap = NULL;
BScreen(this).GetBitmap(&bitmap, false);
fView->SetViewBitmap(bitmap);
fView->MakeFocus(true);
delete bitmap;
BWindow::Show();
}
void
SelectionWindow::SetTarget(BMessenger& target)
{
fTarget = target;
}
void
SelectionWindow::SetCommand(uint32 command)
{
fCommand = command;
}
bool
SelectionWindow::QuitRequested()
{
while (!IsHidden()) {
Hide();
snooze(10000);
}
BScreen screen(this);
BMessage message(fCommand);
BBitmap *bitmap = NULL;
BRect selection = fView->SelectionRect();
if (!selection.IsValid())
selection = screen.Frame();
FixRect(selection, screen.Frame());
snooze(10000);
screen.GetBitmap(&bitmap, false, &selection);
message.AddRect("selection", selection);
message.AddPointer("bitmap", bitmap);
fTarget.SendMessage(&message);
return BWindow::QuitRequested();
}