summaryrefslogtreecommitdiff
path: root/ios/experimental/LibreOffice/LibreOffice/View.m
blob: cb96e48507e3b6378186ac795a3eb09bec345aeb (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
// -*- Mode: ObjC; 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/.

#import "View.h"

#include <touch/touch.h>

@interface View ()
@property const void *documentHandle;
@property CGRect *selectionRectangles;
@property int selectionRectangleCount;
@end

#define HANDLE_BLOB 40
#define HANDLE_STEM_WIDTH 6
#define HANDLE_STEM_HEIGHT 40

@implementation View

- (CGRect) topLeftResizeHandle
{
    if (self.selectionRectangleCount == 0)
        return CGRectNull;

    return CGRectMake(self.selectionRectangles[0].origin.x - HANDLE_STEM_WIDTH/2 - HANDLE_BLOB/2,
                      self.selectionRectangles[0].origin.y - HANDLE_STEM_HEIGHT - HANDLE_BLOB,
                      HANDLE_BLOB, HANDLE_BLOB);
}

- (CGRect) bottomRightResizeHandle
{
    const int N = self.selectionRectangleCount;

    if (N == 0)
        return CGRectNull;

    return CGRectMake(self.selectionRectangles[N-1].origin.x +
                      self.selectionRectangles[N-1].size.width + HANDLE_STEM_WIDTH/2 - HANDLE_BLOB/2,
                      self.selectionRectangles[N-1].origin.y +
                      self.selectionRectangles[N-1].size.height + HANDLE_STEM_HEIGHT,
                      HANDLE_BLOB, HANDLE_BLOB);
}

- (void) requestSelectionRedisplay
{
    if (self.selectionRectangleCount == 0)
        return;

    CGRect r = CGRectNull;
    for (int i = 0; i < self.selectionRectangleCount; i++) {
        r = CGRectUnion(r, self.selectionRectangles[i]);
    }
    r = CGRectUnion(r, [self topLeftResizeHandle]);
    r = CGRectUnion(r, [self bottomRightResizeHandle]);

    [self setNeedsDisplayInRect:r];
}

- (void) drawSelectionIntoContext:(CGContextRef)context
{
    if (self.selectionRectangleCount == 0)
        return;

    const int N = self.selectionRectangleCount;

    CGContextSetFillColorWithColor(context, [[UIColor colorWithRed:0 green:0 blue:1 alpha:0.5] CGColor]);

#if 0
    for (int i = 0; i < N; i++) {
        NSLog(@"UIRectFill: %fx%f@(%f,%f)",
              self.selectionRectangles[i].size.width, self.selectionRectangles[i].size.height,
              self.selectionRectangles[i].origin.x, self.selectionRectangles[i].origin.y);
        UIRectFillUsingBlendMode(CGRectMake(self.selectionRectangles[i].origin.x, self.selectionRectangles[i].origin.y,
                                            self.selectionRectangles[i].size.width, self.selectionRectangles[i].size.height),
                                 kCGBlendModeNormal);
    }
#else
    CGContextSetBlendMode(context, kCGBlendModeNormal);
    CGContextFillRects(context, self.selectionRectangles, self.selectionRectangleCount);
#endif

    CGContextFillRect(context,
                      CGRectMake(self.selectionRectangles[0].origin.x - HANDLE_STEM_WIDTH,
                                 self.selectionRectangles[0].origin.y - HANDLE_STEM_HEIGHT,
                                 HANDLE_STEM_WIDTH, self.selectionRectangles[0].size.height + HANDLE_STEM_HEIGHT));

    CGContextFillRect(context,
                      CGRectMake(self.selectionRectangles[N-1].origin.x +
                                 self.selectionRectangles[N-1].size.width,
                                 self.selectionRectangles[N-1].origin.y,
                                 HANDLE_STEM_WIDTH, self.selectionRectangles[N-1].size.height + HANDLE_STEM_HEIGHT));

    CGContextFillEllipseInRect(context, [self topLeftResizeHandle]);
    CGContextFillEllipseInRect(context, [self bottomRightResizeHandle]);
}

- (void)drawRect:(CGRect)rect
{
    // NSLog(@"View drawRect: %dx%d@(%d,%d)", (int) rect.size.width, (int) rect.size.height, (int) rect.origin.x, (int) rect.origin.y);
    // NSLog(@"statusBarOrientation: %ld", (long)[[UIApplication sharedApplication] statusBarOrientation]);

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);

    switch ([[UIApplication sharedApplication] statusBarOrientation]) {
    case UIInterfaceOrientationPortrait:
        CGContextTranslateCTM(context, 0, self.frame.size.height);
        CGContextScaleCTM(context, 1, -1);
        break;
    case UIInterfaceOrientationLandscapeLeft:
        CGContextTranslateCTM(context, 0, self.frame.size.width);
        CGContextScaleCTM(context, 1, -1);
        break;
    case UIInterfaceOrientationLandscapeRight:
        CGContextTranslateCTM(context, 0, self.frame.size.width);
        CGContextScaleCTM(context, 1, -1);
        break;
    case UIInterfaceOrientationPortraitUpsideDown:
        CGContextTranslateCTM(context, 0, self.frame.size.height);
        CGContextScaleCTM(context, 1, -1);
        break;
    }
    touch_lo_render_windows(context, rect.origin.y, rect.origin.y, rect.size.width, rect.size.height);

    CGContextRestoreGState(context);

    [self drawSelectionIntoContext:context];
}

#if 0
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"===> View touchesBegan!");
}
#endif

- (void)tapGesture:(UITapGestureRecognizer *)gestureRecognizer
{
    if ([gestureRecognizer state] == UIGestureRecognizerStateEnded) {
        CGPoint location = [gestureRecognizer locationInView:self];

        // NSLog(@"tapGesture: at: (%d,%d)", (int)location.x, (int)location.y);

        touch_lo_tap(location.x, location.y);
    } else {
        // NSLog(@"tapGesture: %@", gestureRecognizer);
    }
}

- (void)panGesture:(UIPanGestureRecognizer *)gestureRecognizer
{
    static enum { NONE, TOPLEFT, BOTTOMRIGHT } draggedHandle = NONE;
    static CGFloat previousX, previousY;

    CGPoint location = [gestureRecognizer locationInView:self];
    CGPoint translation = [gestureRecognizer translationInView:self];

    if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
        previousX = previousY = 0;
    }

    CGPoint delta;
    delta.x = translation.x - previousX;
    delta.y = translation.y - previousY;

    // NSLog(@"location: (%f,%f) , drag: (%f,%f)", location.x, location.y, delta.x, delta.y);

    previousX = translation.x;
    previousY = translation.y;

    if (gestureRecognizer.state == UIGestureRecognizerStateBegan &&
        gestureRecognizer.numberOfTouches == 1) {
        if (CGRectContainsPoint([self topLeftResizeHandle], location)) {
            NSLog(@"===> dragging TOPLEFT handle");
            draggedHandle = TOPLEFT;
        } else if (CGRectContainsPoint([self bottomRightResizeHandle], location)) {
            NSLog(@"===> dragging BOTTOMRIGHT handle");
            draggedHandle = BOTTOMRIGHT;
        }
    }

    if (draggedHandle == TOPLEFT) {
        const int N = self.selectionRectangleCount;

        CGPoint old = self.selectionRectangles[0].origin;

        self.selectionRectangles[0].origin = location;
        self.selectionRectangles[0].size.width -= (location.x - old.x);
        self.selectionRectangles[0].size.height -= (location.y - old.y);

#if 0
        touch_lo_selection_attempt_resize(self.documentHandle,
                                          self.selectionRectangles,
                                          self.selectionRectangleCount);
#else
        touch_lo_tap((self.selectionRectangles[0].origin.x + self.selectionRectangles[N-1].origin.x) / 2,
                     (self.selectionRectangles[0].origin.y + self.selectionRectangles[N-1].origin.y) / 2);

        touch_lo_mouse(self.selectionRectangles[0].origin.x,
                       self.selectionRectangles[0].origin.y,
                       DOWN, NONE);
        touch_lo_mouse(self.selectionRectangles[N-1].origin.x +
                       self.selectionRectangles[N-1].size.width,
                       self.selectionRectangles[N-1].origin.y +
                       self.selectionRectangles[N-1].size.height,
                       UP, NONE);
#endif
        if (gestureRecognizer.state == UIGestureRecognizerStateEnded)
            draggedHandle = NONE;
        return;
    } else if (draggedHandle == BOTTOMRIGHT) {

        touch_lo_selection_end_move(self.documentHandle, location.x, location.y);

        if (gestureRecognizer.state == UIGestureRecognizerStateEnded)
            draggedHandle = NONE;
        return;
    }

    if (gestureRecognizer.state != UIGestureRecognizerStateBegan) {
        touch_lo_pan(delta.x, delta.y);
    }
}

- (void)pinchGesture:(UIPinchGestureRecognizer *)gestureRecognizer
{
    CGPoint location = [gestureRecognizer locationInView:self];
    CGFloat scale = gestureRecognizer.scale;

    // NSLog(@"pinchGesture: pinch: (%f) cords (%d,%d)", (float)scale, (int)location.x, (int)location.y );

    touch_lo_zoom((int)location.x, (int)location.y, (float)scale);

    // to reset the gesture scaling
    if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
        touch_lo_zoom(1, 1, 0.0f);
    }
}

- (void)longPressGesture:(UILongPressGestureRecognizer *)gestureRecognizer
{
    CGPoint point = [gestureRecognizer locationInView:self];

    if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
        touch_lo_tap(point.x, point.y);
        touch_lo_tap(point.x, point.y);
    }
}

static int compare_rects(const void *a, const void *b)
{
    const CGRect *ra = a;
    const CGRect *rb = b;

    if (ra->origin.y != rb->origin.y)
        return ra->origin.y - rb->origin.y;
    else
        return ra->origin.x - rb->origin.x;
}

- (void)startSelectionOfType:(MLOSelectionKind)kind withNumber:(int)number ofRectangles:(CGRect *)rects forDocument:(const void *)document
{
    (void) kind;

    // First request the old selection area to be redisplayed
    [self requestSelectionRedisplay];

    free(self.selectionRectangles);
    self.selectionRectangles = NULL;
    self.selectionRectangleCount = 0;
    self.documentHandle = NULL;

    if (number == 0)
        return;

    self.selectionRectangles = rects;
    self.selectionRectangleCount = number;
    self.documentHandle = document;

    // The selection rectangle provided by LO are not sorted in any sane way
    qsort(self.selectionRectangles, self.selectionRectangleCount, sizeof(self.selectionRectangles[0]), compare_rects);

    [self requestSelectionRedisplay];
}

@end

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