2 * Copyright 2003-2010, Haiku, Inc. All Rights Reserved.
3 * Copyright 2004-2005 yellowTAB GmbH. All Rights Reserverd.
4 * Copyright 2006 Bernd Korz. All Rights Reserved
5 * Distributed under the terms of the MIT License.
8 * Fernando Francisco de Oliveira
14 * Stephan Aßmus <superstippi@gmx.de>
18 #include "SelectionBox.h"
24 #include "ShowImageView.h"
27 SelectionBox::SelectionBox()
35 SelectionBox::~SelectionBox()
41 SelectionBox::SetBounds(ShowImageView
* view
, BRect bounds
)
43 view
->ConstrainToImage(bounds
);
45 if (fBounds
== bounds
)
48 BRect dirtyOld
= _RectInView(view
);
52 BRect dirtyNew
= _RectInView(view
);
54 if (dirtyOld
.IsValid() && dirtyNew
.IsValid())
55 view
->Invalidate(dirtyOld
| dirtyNew
);
56 else if (dirtyOld
.IsValid())
57 view
->Invalidate(dirtyOld
);
58 else if (dirtyNew
.IsValid())
59 view
->Invalidate(dirtyNew
);
64 SelectionBox::Bounds() const
71 SelectionBox::MouseDown(ShowImageView
* view
, BPoint where
)
73 // TODO: Allow to re-adjust corners.
74 where
= view
->ViewToImage(where
);
75 SetBounds(view
, BRect(where
, where
));
80 SelectionBox::MouseMoved(ShowImageView
* view
, BPoint where
)
82 // TODO: Allow to re-adjust corners.
83 where
= view
->ViewToImage(where
);
85 BRect
bounds(fBounds
);
87 if (where
.x
>= bounds
.left
)
88 bounds
.right
= where
.x
;
90 bounds
.left
= where
.x
;
92 if (where
.y
>= bounds
.top
)
93 bounds
.bottom
= where
.y
;
97 SetBounds(view
, bounds
);
102 SelectionBox::MouseUp(ShowImageView
* view
, BPoint where
)
108 SelectionBox::Animate()
111 uchar p
= fPatternUp
.data
[0];
112 for (int i
= 0; i
<= 6; i
++)
113 fPatternUp
.data
[i
] = fPatternUp
.data
[i
+ 1];
114 fPatternUp
.data
[7] = p
;
117 p
= fPatternDown
.data
[7];
118 for (int i
= 7; i
>= 1; i
--)
119 fPatternDown
.data
[i
] = fPatternDown
.data
[i
- 1];
120 fPatternDown
.data
[0] = p
;
123 p
= fPatternLeft
.data
[0];
124 bool set
= (p
& 0x80) != 0;
129 memset(fPatternLeft
.data
, p
, 8);
132 p
= fPatternRight
.data
[0];
137 memset(fPatternRight
.data
, p
, 8);
142 SelectionBox::Draw(ShowImageView
* view
, const BRect
& updateRect
) const
144 BRect r
= _RectInView(view
);
145 if (!r
.IsValid() || !updateRect
.Intersects(r
))
150 view
->SetLowColor(255, 255, 255);
151 view
->StrokeLine(BPoint(r
.left
, r
.top
), BPoint(r
.right
, r
.top
),
153 view
->StrokeLine(BPoint(r
.right
, r
.top
+ 1), BPoint(r
.right
, r
.bottom
- 1),
155 view
->StrokeLine(BPoint(r
.left
, r
.bottom
), BPoint(r
.right
, r
.bottom
),
157 view
->StrokeLine(BPoint(r
.left
, r
.top
+ 1), BPoint(r
.left
, r
.bottom
- 1),
168 SelectionBox::_InitPatterns()
173 for (int i
= 0; i
<= 7; i
++) {
174 fPatternLeft
.data
[i
] = p1
;
175 fPatternRight
.data
[i
] = p2
;
176 if ((i
/ 2) % 2 == 0)
180 fPatternUp
.data
[i
] = p
;
181 fPatternDown
.data
[i
] = ~p
;
187 SelectionBox::_RectInView(ShowImageView
* view
) const
190 if (fBounds
.Height() <= 0.0 || fBounds
.Width() <= 0.0)
194 // Layout selection rect in view coordinate space
195 view
->ConstrainToImage(r
);
196 r
= view
->ImageToView(r
);
197 // draw selection box *around* selection