summaryrefslogtreecommitdiffstats
path: root/android/source/src/java/org/libreoffice/overlay/DocumentOverlayView.java
blob: 086108cd903f94bd931275df818d978ea377f3ef (plain)
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
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package org.libreoffice.overlay;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import org.libreoffice.LibreOfficeMainActivity;
import org.libreoffice.R;
import org.libreoffice.canvas.AdjustLengthLine;
import org.libreoffice.canvas.CalcSelectionBox;
import org.libreoffice.canvas.Cursor;
import org.libreoffice.canvas.GraphicSelection;
import org.libreoffice.canvas.PageNumberRect;
import org.libreoffice.canvas.SelectionHandle;
import org.libreoffice.canvas.SelectionHandleEnd;
import org.libreoffice.canvas.SelectionHandleMiddle;
import org.libreoffice.canvas.SelectionHandleStart;
import org.mozilla.gecko.gfx.ImmutableViewportMetrics;
import org.mozilla.gecko.gfx.LayerView;
import org.mozilla.gecko.gfx.RectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Document overlay view is responsible for showing the client drawn overlay
 * elements like cursor, selection and graphic selection, and manipulate them.
 */
public class DocumentOverlayView extends View implements View.OnTouchListener {
    private static final String LOGTAG = DocumentOverlayView.class.getSimpleName();

    private static final int CURSOR_BLINK_TIME = 500;

    private boolean mInitialized = false;

    private List<RectF> mSelections = new ArrayList<RectF>();
    private List<RectF> mScaledSelections = new ArrayList<RectF>();
    private Paint mSelectionPaint = new Paint();
    private boolean mSelectionsVisible;

    private GraphicSelection mGraphicSelection;

    private boolean mGraphicSelectionMove = false;

    private LayerView mLayerView;

    private SelectionHandle mHandleMiddle;
    private SelectionHandle mHandleStart;
    private SelectionHandle mHandleEnd;

    private Cursor mCursor;

    private SelectionHandle mDragHandle = null;

    private List<RectF> mPartPageRectangles;
    private PageNumberRect mPageNumberRect;
    private boolean mPageNumberAvailable = false;
    private int previousIndex = 0; // previous page number, used to compare with the current
    private CalcHeadersController mCalcHeadersController;

    private CalcSelectionBox mCalcSelectionBox;
    private boolean mCalcSelectionBoxDragging;
    private AdjustLengthLine mAdjustLengthLine;
    private boolean mAdjustLengthLineDragging;

    public DocumentOverlayView(Context context) {
        super(context);
    }

    public DocumentOverlayView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DocumentOverlayView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * Initialize the selection and cursor view.
     */
    public void initialize(LayerView layerView) {
        if (!mInitialized) {
            setOnTouchListener(this);
            mLayerView = layerView;

            mCursor = new Cursor();
            mCursor.setVisible(false);

            mSelectionPaint.setColor(Color.BLUE);
            mSelectionPaint.setAlpha(50);
            mSelectionsVisible = false;

            mGraphicSelection = new GraphicSelection((LibreOfficeMainActivity) getContext());
            mGraphicSelection.setVisible(false);

            postDelayed(cursorAnimation, CURSOR_BLINK_TIME);

            mHandleMiddle = new SelectionHandleMiddle((LibreOfficeMainActivity) getContext());
            mHandleStart = new SelectionHandleStart((LibreOfficeMainActivity) getContext());
            mHandleEnd = new SelectionHandleEnd((LibreOfficeMainActivity) getContext());

            mInitialized = true;
        }
    }

    /**
     * Change the cursor position.
     * @param position - new position of the cursor
     */
    public void changeCursorPosition(RectF position) {
        if (RectUtils.fuzzyEquals(mCursor.mPosition, position)) {
            return;
        }
        mCursor.mPosition = position;

        ImmutableViewportMetrics metrics = mLayerView.getViewportMetrics();
        repositionWithViewport(metrics.viewportRectLeft, metrics.viewportRectTop, metrics.zoomFactor);
    }

    /**
     * Change the text selection rectangles.
     * @param selectionRects - list of text selection rectangles
     */
    public void changeSelections(List<RectF> selectionRects) {
        mSelections = selectionRects;

        ImmutableViewportMetrics metrics = mLayerView.getViewportMetrics();
        repositionWithViewport(metrics.viewportRectLeft, metrics.viewportRectTop, metrics.zoomFactor);
    }

    /**
     * Change the graphic selection rectangle.
     * @param rectangle - new graphic selection rectangle
     */
    public void changeGraphicSelection(RectF rectangle) {
        if (RectUtils.fuzzyEquals(mGraphicSelection.mRectangle, rectangle)) {
            return;
        }

        mGraphicSelection.mRectangle = rectangle;

        ImmutableViewportMetrics metrics = mLayerView.getViewportMetrics();
        repositionWithViewport(metrics.viewportRectLeft, metrics.viewportRectTop, metrics.zoomFactor);
    }

    public void repositionWithViewport(float x, float y, float zoom) {
        RectF rect = convertToScreen(mCursor.mPosition, x, y, zoom);
        mCursor.reposition(rect);

        rect = convertToScreen(mHandleMiddle.mDocumentPosition, x, y, zoom);
        mHandleMiddle.reposition(rect.left, rect.bottom);

        rect = convertToScreen(mHandleStart.mDocumentPosition, x, y, zoom);
        mHandleStart.reposition(rect.left, rect.bottom);

        rect = convertToScreen(mHandleEnd.mDocumentPosition, x, y, zoom);
        mHandleEnd.reposition(rect.left, rect.bottom);

        mScaledSelections.clear();
        for (RectF selection : mSelections) {
            RectF scaledSelection = convertToScreen(selection, x, y, zoom);
            mScaledSelections.add(scaledSelection);
        }

        if (mCalcSelectionBox != null) {
            rect = convertToScreen(mCalcSelectionBox.mDocumentPosition, x, y, zoom);
            mCalcSelectionBox.reposition(rect);
        }

        if (mGraphicSelection != null && mGraphicSelection.mRectangle != null) {
            RectF scaledGraphicSelection = convertToScreen(mGraphicSelection.mRectangle, x, y, zoom);
            mGraphicSelection.reposition(scaledGraphicSelection);
        }

        invalidate();
    }

    /**
     * Convert the input rectangle from document to screen coordinates
     * according to current viewport data (x, y, zoom).
     */
    private static RectF convertToScreen(RectF inputRect, float x, float y, float zoom) {
        RectF rect = RectUtils.scale(inputRect, zoom);
        rect.offset(-x, -y);
        return rect;
    }

    /**
     * Set part page rectangles and initialize a page number rectangle object
     * (canvas element).
     */
    public void setPartPageRectangles (List<RectF> rectangles) {
        mPartPageRectangles = rectangles;
        mPageNumberRect = new PageNumberRect();
        mPageNumberAvailable = true;
    }

    /**
     * Drawing on canvas.
     */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        mCursor.draw(canvas);

        if (mPageNumberAvailable) {
            mPageNumberRect.draw(canvas);
        }

        mHandleMiddle.draw(canvas);
        mHandleStart.draw(canvas);
        mHandleEnd.draw(canvas);

        if (mSelectionsVisible) {
            for (RectF selection : mScaledSelections) {
                canvas.drawRect(selection, mSelectionPaint);
            }
        }

        if (mCalcSelectionBox != null) {
            mCalcSelectionBox.draw(canvas);
        }

        mGraphicSelection.draw(canvas);

        if (mCalcHeadersController != null) {
            mCalcHeadersController.showHeaders();
        }

        if (mAdjustLengthLine != null) {
            mAdjustLengthLine.draw(canvas);
        }
    }

    /**
     * Cursor animation function. Switch the alpha between opaque and fully transparent.
     */
    private Runnable cursorAnimation = new Runnable() {
        public void run() {
            if (mCursor.isVisible()) {
                mCursor.cycleAlpha();
                invalidate();
            }
            postDelayed(cursorAnimation, CURSOR_BLINK_TIME);
        }
    };

    /**
     * Show the cursor on the view.
     */
    public void showCursor() {
        if (!mCursor.isVisible()) {
            mCursor.setVisible(true);
            invalidate();
        }
    }

    /**
     * Hide the cursor.
     */
    public void hideCursor() {
        if (mCursor.isVisible()) {
            mCursor.setVisible(false);
            invalidate();
        }
    }

    /**
     * Calculate and show page number according to current viewport position.
     * In particular, this function compares the middle point of the
     * view port with page rectangles and finds out which page the user
     * is currently on. It does not update the associated canvas element
     * unless there is a change of page number.
     */
    public void showPageNumberRect() {
        if (null == mPartPageRectangles) return;
        PointF midPoint = mLayerView.getLayerClient().convertViewPointToLayerPoint(new PointF(getWidth()/2f, getHeight()/2f));
        int index = previousIndex;
        // search which page the user in currently on. can enhance the search algorithm to binary search if necessary
        for (RectF page : mPartPageRectangles) {
            if (page.top < midPoint.y && midPoint.y < page.bottom) {
                index = mPartPageRectangles.indexOf(page) + 1;
                break;
            }
        }
        // index == 0 applies to non-text document, i.e. don't show page info on non-text docs
        if (index == 0) {
            return;
        }
        // if page rectangle canvas element is not visible or the page number is changed, show
        if (!mPageNumberRect.isVisible() || index != previousIndex) {
            previousIndex = index;
            String pageNumberString = getContext().getString(R.string.page) + " " + index + "/" + mPartPageRectangles.size();
            mPageNumberRect.setPageNumberString(pageNumberString);
            mPageNumberRect.setVisible(true);
            invalidate();
        }
    }

    /**
     * Hide page number rectangle canvas element.
     */
    public void hidePageNumberRect() {
        if (null == mPageNumberRect) return;
        if (mPageNumberRect.isVisible()) {
            mPageNumberRect.setVisible(false);
            invalidate();
        }
    }

    /**
     * Show text selection rectangles.
     */
    public void showSelections() {
        if (!mSelectionsVisible) {
            mSelectionsVisible = true;
            invalidate();
        }
    }

    /**
     * Hide text selection rectangles.
     */
    public void hideSelections() {
        if (mSelectionsVisible) {
            mSelectionsVisible = false;
            invalidate();
        }
    }

    /**
     * Show the graphic selection on the view.
     */
    public void showGraphicSelection() {
        if (!mGraphicSelection.isVisible()) {
            mGraphicSelectionMove = false;
            mGraphicSelection.reset();
            mGraphicSelection.setVisible(true);
            invalidate();
        }
    }

    /**
     * Hide the graphic selection.
     */
    public void hideGraphicSelection() {
        if (mGraphicSelection.isVisible()) {
            mGraphicSelection.setVisible(false);
            invalidate();
        }
    }

    /**
     * Handle the triggered touch event.
     */
    @Override
    public boolean onTouch(View view, MotionEvent event) {
        PointF point = new PointF(event.getX(), event.getY());
        switch (event.getActionMasked()) {
            case MotionEvent.ACTION_DOWN: {
                if (mAdjustLengthLine != null && !mAdjustLengthLine.contains(point.x, point.y)) {
                    mAdjustLengthLine.setVisible(false);
                    invalidate();
                }
                if (mGraphicSelection.isVisible()) {
                    // Check if inside graphic selection was hit
                    if (mGraphicSelection.contains(point.x, point.y)) {
                        mGraphicSelectionMove = true;
                        mGraphicSelection.dragStart(point);
                        invalidate();
                        return true;
                    }
                } else {
                    if (mHandleStart.contains(point.x, point.y)) {
                        mHandleStart.dragStart(point);
                        mDragHandle = mHandleStart;
                        return true;
                    } else if (mHandleEnd.contains(point.x, point.y)) {
                        mHandleEnd.dragStart(point);
                        mDragHandle = mHandleEnd;
                        return true;
                    } else if (mHandleMiddle.contains(point.x, point.y)) {
                        mHandleMiddle.dragStart(point);
                        mDragHandle = mHandleMiddle;
                        return true;
                    } else if (mCalcSelectionBox != null &&
                            mCalcSelectionBox.contains(point.x, point.y) &&
                            !mHandleStart.isVisible()) {
                        mCalcSelectionBox.dragStart(point);
                        mCalcSelectionBoxDragging = true;
                        return true;
                    } else if (mAdjustLengthLine != null &&
                            mAdjustLengthLine.contains(point.x, point.y)) {
                        mAdjustLengthLine.dragStart(point);
                        mAdjustLengthLineDragging = true;
                        return true;
                    }
                }
            }
            case MotionEvent.ACTION_UP: {
                if (mGraphicSelection.isVisible() && mGraphicSelectionMove) {
                    mGraphicSelection.dragEnd(point);
                    mGraphicSelectionMove = false;
                    invalidate();
                    return true;
                } else if (mDragHandle != null) {
                    mDragHandle.dragEnd(point);
                    mDragHandle = null;
                } else if (mCalcSelectionBoxDragging) {
                    mCalcSelectionBox.dragEnd(point);
                    mCalcSelectionBoxDragging = false;
                } else if (mAdjustLengthLineDragging) {
                    mAdjustLengthLine.dragEnd(point);
                    mAdjustLengthLineDragging = false;
                    invalidate();
                }
            }
            case MotionEvent.ACTION_MOVE: {
                if (mGraphicSelection.isVisible() && mGraphicSelectionMove) {
                    mGraphicSelection.dragging(point);
                    invalidate();
                    return true;
                } else if (mDragHandle != null) {
                    mDragHandle.dragging(point);
                } else if (mCalcSelectionBoxDragging) {
                    mCalcSelectionBox.dragging(point);
                } else if (mAdjustLengthLineDragging) {
                    mAdjustLengthLine.dragging(point);
                    invalidate();
                }
            }
        }
        return false;
    }

    /**
     * Change the handle document position.
     * @param type - the type of the handle
     * @param position - the new document position
     */
    public void positionHandle(SelectionHandle.HandleType type, RectF position) {
        SelectionHandle handle = getHandleForType(type);
        if (RectUtils.fuzzyEquals(handle.mDocumentPosition, position)) {
            return;
        }

        RectUtils.assign(handle.mDocumentPosition, position);

        ImmutableViewportMetrics metrics = mLayerView.getViewportMetrics();
        repositionWithViewport(metrics.viewportRectLeft, metrics.viewportRectTop, metrics.zoomFactor);
    }

    /**
     * Hide the handle.
     * @param type - type of the handle
     */
    public void hideHandle(SelectionHandle.HandleType type) {
        SelectionHandle handle = getHandleForType(type);
        if (handle.isVisible()) {
            handle.setVisible(false);
            invalidate();
        }
    }

    /**
     * Show the handle.
     * @param type - type of the handle
     */
    public void showHandle(SelectionHandle.HandleType type) {
        SelectionHandle handle = getHandleForType(type);
        if (!handle.isVisible()) {
            handle.setVisible(true);
            invalidate();
        }
    }

    /**
     * Returns the handle instance for the input type.
     */
    private SelectionHandle getHandleForType(SelectionHandle.HandleType type) {
        switch(type) {
            case START:
                return mHandleStart;
            case END:
                return mHandleEnd;
            case MIDDLE:
                return mHandleMiddle;
        }
        return null;
    }

    public RectF getCurrentCursorPosition() {
        return mCursor.mPosition;
    }

    public void setCalcHeadersController(CalcHeadersController calcHeadersController) {
        mCalcHeadersController = calcHeadersController;
        mCalcSelectionBox = new CalcSelectionBox((LibreOfficeMainActivity) getContext());
    }

    public void showCellSelection(RectF cellCursorRect) {
        if (mCalcHeadersController == null || mCalcSelectionBox == null) return;
        if (RectUtils.fuzzyEquals(mCalcSelectionBox.mDocumentPosition, cellCursorRect)) {
            return;
        }

        // show selection on main GL view (i.e. in the document)
        RectUtils.assign(mCalcSelectionBox.mDocumentPosition, cellCursorRect);
        mCalcSelectionBox.setVisible(true);

        ImmutableViewportMetrics metrics = mLayerView.getViewportMetrics();
        repositionWithViewport(metrics.viewportRectLeft, metrics.viewportRectTop, metrics.zoomFactor);

        // show selection on headers
        if (!mCalcHeadersController.pendingRowOrColumnSelectionToShowUp()) {
            showHeaderSelection(cellCursorRect);
        } else {
            mCalcHeadersController.setPendingRowOrColumnSelectionToShowUp(false);
        }
    }

    public void showHeaderSelection(RectF rect) {
        if (mCalcHeadersController == null) return;
        mCalcHeadersController.showHeaderSelection(rect);
    }

    public void showAdjustLengthLine(boolean isRow, final CalcHeadersView view) {
        mAdjustLengthLine = new AdjustLengthLine((LibreOfficeMainActivity) getContext(), view, isRow, getWidth(), getHeight());
        ImmutableViewportMetrics metrics = mLayerView.getViewportMetrics();
        RectF position = convertToScreen(mCalcSelectionBox.mDocumentPosition, metrics.viewportRectLeft, metrics.viewportRectTop, metrics.zoomFactor);
        mAdjustLengthLine.setScreenRect(position);
        mAdjustLengthLine.setVisible(true);
        invalidate();
    }
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */