1 package org
.libreoffice
.canvas
;
3 import android
.graphics
.Canvas
;
4 import android
.graphics
.Color
;
5 import android
.graphics
.Paint
;
6 import android
.graphics
.PointF
;
7 import android
.graphics
.RectF
;
9 import org
.libreoffice
.LOKitShell
;
10 import org
.libreoffice
.LibreOfficeMainActivity
;
11 import org
.mozilla
.gecko
.gfx
.ImmutableViewportMetrics
;
14 * CalcSelectionBox is the selection frame for the current highlighted area/cells
18 public class CalcSelectionBox
extends CommonCanvasElement
{
19 private static final long MINIMUM_HANDLE_UPDATE_TIME
= 50 * 1000000;
20 private static final float CIRCLE_HANDLE_RADIUS
= 8f
;
22 public RectF mDocumentPosition
;
24 private LibreOfficeMainActivity mContext
;
25 private RectF mScreenPosition
;
26 private long mLastTime
= 0;
28 private Paint mCirclePaint
;
30 public CalcSelectionBox(LibreOfficeMainActivity context
) {
32 mScreenPosition
= new RectF();
33 mDocumentPosition
= new RectF();
35 mPaint
.setStyle(Paint
.Style
.STROKE
);
36 mPaint
.setColor(Color
.BLACK
);
37 mPaint
.setStrokeWidth(2f
);
38 mCirclePaint
= new Paint();
39 mCirclePaint
.setColor(Color
.BLACK
);
40 mCirclePaint
.setStyle(Paint
.Style
.FILL
);
44 * Start of a touch and drag action on the box.
46 public void dragStart(PointF point
) {}
49 * End of a touch and drag action on the box.
51 public void dragEnd(PointF point
) {}
54 * Box has been dragged.
56 public void dragging(PointF point
) {
57 long currentTime
= System
.nanoTime();
58 if (currentTime
- mLastTime
> MINIMUM_HANDLE_UPDATE_TIME
) {
59 mLastTime
= currentTime
;
60 signalHandleMove(point
.x
, point
.y
);
65 * Signal to move the handle to a new position to LO.
67 private void signalHandleMove(float newX
, float newY
) {
68 ImmutableViewportMetrics viewportMetrics
= mContext
.getLayerClient().getViewportMetrics();
69 float zoom
= viewportMetrics
.zoomFactor
;
70 PointF origin
= viewportMetrics
.getOrigin();
72 PointF documentPoint
= new PointF((newX
+origin
.x
)/zoom
, (newY
+origin
.y
)/zoom
);
74 if (documentPoint
.x
< mDocumentPosition
.left
|| documentPoint
.y
< mDocumentPosition
.top
) {
75 LOKitShell
.sendChangeHandlePositionEvent(SelectionHandle
.HandleType
.START
, documentPoint
);
76 } else if (documentPoint
.x
> mDocumentPosition
.right
|| documentPoint
.y
> mDocumentPosition
.bottom
){
77 LOKitShell
.sendChangeHandlePositionEvent(SelectionHandle
.HandleType
.END
, documentPoint
);
82 public boolean onHitTest(float x
, float y
) {
83 return mScreenPosition
.contains(x
, y
);
87 public void onDraw(Canvas canvas
) {
88 canvas
.drawRect(mScreenPosition
, mPaint
);
89 canvas
.drawCircle(mScreenPosition
.left
, mScreenPosition
.top
, CIRCLE_HANDLE_RADIUS
, mCirclePaint
);
90 canvas
.drawCircle(mScreenPosition
.right
, mScreenPosition
.bottom
, CIRCLE_HANDLE_RADIUS
, mCirclePaint
);
93 public void reposition(RectF rect
) {
94 mScreenPosition
= rect
;
98 public boolean contains(float x
, float y
) {
99 // test if in range of the box or the circular handles
100 boolean inRange
= new RectF(mScreenPosition
.left
- CIRCLE_HANDLE_RADIUS
,
101 mScreenPosition
.top
- CIRCLE_HANDLE_RADIUS
,
102 mScreenPosition
.left
+ CIRCLE_HANDLE_RADIUS
,
103 mScreenPosition
.top
+ CIRCLE_HANDLE_RADIUS
).contains(x
, y
)
104 || new RectF(mScreenPosition
.right
- CIRCLE_HANDLE_RADIUS
,
105 mScreenPosition
.bottom
- CIRCLE_HANDLE_RADIUS
,
106 mScreenPosition
.right
+ CIRCLE_HANDLE_RADIUS
,
107 mScreenPosition
.bottom
+ CIRCLE_HANDLE_RADIUS
).contains(x
, y
)
109 return inRange
&& isVisible();