summaryrefslogtreecommitdiff
path: root/ios/shared/ios_sharedlo/objective_c/view_controllers/selection/MLOSelectionViewController.m
blob: 4e042b5e779ddfa5c18ccccfc8880ba0f43a09be (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
// -*- 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 "MLOMainViewController_Friend.h"
#import "MLOSelectionViewController_Impl.h"
#import "MLOContextualMenuFocus.h"
#import "MLOSelectionHandle.h"
#import "MLOMagnifier.h"
#import "MLORenderManager.h"
#import "MLOThesaurusViewController.h"
#import "mlo_utils.h"
#import "mlo_uno.h"
#include <touch/touch.h>

@interface MLOSelectionViewController ()
@property MLOContextualMenuFocus * focus;
@property MLOSelectionHandle * topLeft, * bottomRight;
@property MLOMagnifier * magnifier;
@property BOOL menuShown,magnifierShown;
@end

static const CGFloat FOCUS_DIM=20.0f;

@implementation MLOSelectionViewController

-(id) initWithMainViewController:(MLOMainViewController *)mainViewController{
    self = [super init];
    if(self){
        self.mainViewController = mainViewController;
        self.buffer = [NSMutableString stringWithString:@""];
        self.focus = [[MLOContextualMenuFocus alloc] initWithSelectionViewController:self];
        self.topLeft = [[MLOSelectionHandle alloc] initWithType: TOP_LEFT selection:self];
        self.bottomRight = [[MLOSelectionHandle alloc] initWithType: BOTTOM_RIGHT selection:self];
        self.magnifier = [[MLOMagnifier alloc] initWithMainViewController:mainViewController];
        self.menuShown =NO;
        self.magnifierShown =NO;
        self.finger = CGPointMake(0,0);
    }
    
    return self;
}
-(void) addToMainViewController{
    [_mainViewController.canvas addSubview:_focus];
    [_topLeft addToMainViewController];
    [_bottomRight addToMainViewController];
    [_magnifier addToMainViewController];
}

-(void) reset{
    [self hide:NO];
}
-(UIMenuItem *)copyItem{
    return [[UIMenuItem alloc]
            initWithTitle:  @"Copy"
            action:         @selector(loCopyToClipboard:)];
}
-(UIMenuItem *)selectAllItem{
    return [[UIMenuItem alloc]
            initWithTitle: @"Select All"
            action:        @selector(loSelectAll:)];
}

-(UIMenuItem *)defineItem{
    return [[UIMenuItem alloc]
            initWithTitle:  @"Define"
            action:         @selector(defineBuffer:)];
}

-(void) showMenuAtCenter:(CGPoint)center{
    [self showAtCenter:center selectAll:YES];
}

-(void) showPostSelectAll{

    [self hideTouchTrackers];
    [self showAtCenter:CGPointMake(_mainViewController.canvas.frame.size.width / 2.0f, FOCUS_DIM) selectAll:NO];
}

-(void) showAtTopLeft:(CGPoint) topLeft bottomRight:(CGPoint) bottomRight showMenu:(BOOL) showMenu{
    [_topLeft showAt:topLeft];
    [_bottomRight showAt:bottomRight];
    if(showMenu){
        [self hideMagnifier];
        [self showMenuAtCenter:CGPointMake( (bottomRight.x + bottomRight.x) /2.0f,
                                    (bottomRight.y + bottomRight.y) /2.0f)];
        self.menuShown = YES;
    }else{
        [self hideMenu:YES];
        [_magnifier showAt:self.finger];
        self.magnifierShown=YES;
    }
    
    [_mainViewController.renderManager renderNow];
}

-(void) showBetweenFinger:(CGPoint) finger andPreviousPoint:(CGPoint) previousPoint showMenu:(BOOL) showMenu{
    self.finger = finger;
    [self showBetweenPointA:finger andPointB:previousPoint showMenu:showMenu];
}

-(void) showBetweenPointA:(CGPoint) pointA andPointB:(CGPoint) pointB showMenu:(BOOL) showMenu{

    if((pointA.y < pointB.y) ||
       ((pointA.y == pointB.y) && (pointA.x < pointB.x))){
        
        [self showAtTopLeft:pointA bottomRight:pointB showMenu:showMenu];
    }else{
        [self showAtTopLeft:pointB bottomRight:pointA showMenu:showMenu];
    }
}

-(void)showAtCenter:(CGPoint) center selectAll:(BOOL) isSelectAll{
    
    CGFloat width = _mainViewController.canvas.frame.size.width;
    CGFloat height = _mainViewController.canvas.frame.size.height;

    self.focus.frame = CGRectMake( min(max(0,center.x - FOCUS_DIM),width - FOCUS_DIM),
                             min(max(0,center.y - FOCUS_DIM),height - FOCUS_DIM),
                             FOCUS_DIM,
                             FOCUS_DIM);

    [self.focus becomeFirstResponder];

    UIMenuController *menuController = [UIMenuController sharedMenuController];

    [menuController setMenuItems:[self getMenuItems:isSelectAll]];
    
    [menuController setTargetRect:self.focus.frame
                           inView:_mainViewController.canvas];
    [menuController setMenuVisible:YES animated:YES];
}

-(NSArray *)getMenuItems:(BOOL) isSelectAll{

    mlo_get_selection(_buffer);

    NSLog(@"Copied LO selection to keyboard: %@",_buffer);

    if(isSelectAll){

        if([UIReferenceLibraryViewController dictionaryHasDefinitionForTerm:_buffer]){
            NSLog(@"Creating contextual menu with Copy, Select All, and Define");

            return @[[self copyItem], [self selectAllItem], [self defineItem]];
            
        }
         NSLog(@"Creating contextual menu with Copy and Select All");
        
         return @[[self copyItem],[self selectAllItem]];
    }
    
    NSLog(@"Creating contextual menu with Copy only");

    return @[[self copyItem]];
 }

-(NSString *)description{
    return @"MLO contextual menu view controller";
}

-(void) hide{
    [self hide:YES];
}

-(void) hideTouchTrackers{
    [_topLeft hide];
    [_bottomRight hide];
    [self hideMagnifier];
}

-(void) hideMenu:(BOOL) animate{
    if(self.menuShown){
        self.menuShown =NO;
        [[UIMenuController sharedMenuController] setMenuVisible:NO animated:animate];
    }
}

-(void) hideMagnifier{
    if(self.magnifierShown){
        self.magnifierShown =NO;
        [_magnifier hide];
    }
}

-(void) hide:(BOOL) animate{

    [self hideTouchTrackers];

    [self hideMenu:animate];
    
    [self.focus resignFirstResponder];
    self.focus.frame =CGRECT_ZERO;
}

-(void)reselect:(BOOL) showMenu{

    CGPoint pointA = [_topLeft getPivot];
    CGPoint pointB = [_bottomRight getPivot];

    touch_lo_mouse_drag(pointA.x, pointA.y, DOWN);

    touch_lo_mouse_drag(pointB.x, pointB.y, MOVE);

    touch_lo_mouse_drag(pointB.x, pointB.y, UP);

    [self showBetweenPointA:pointA andPointB:pointB showMenu:showMenu];
}

-(void) onRotate{
    [self reset];

}

-(BOOL)handleMoveAtPoint:(CGPoint) center gestureState:(UIGestureRecognizerState) state{
    return  (self.focus.frame.origin.x != 0) &&
            ([_topLeft handleMoveAtPoint:center gestureState:state] ||
            [_bottomRight handleMoveAtPoint:center gestureState:state]);
}


-(void)defineBuffer{
    [[[MLOThesaurusViewController alloc] initWithSelectionViewController:self] show];
    
}

@end