-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWebViewObject.m
263 lines (199 loc) · 9.85 KB
/
WebViewObject.m
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
//
// WebViewObject.m
// MultiWebExplorer
//
// Created by gungor on 6/24/14.
// Copyright (c) 2014 gungor. All rights reserved.
//
#import "WebViewObject.h"
#import "LoadController.h"
#import "TranslationController.h"
#import "Utility.h"
@implementation WebViewObject
ViewController *mainController;
ViewStateController *viewAdjuster;
-(id)initComponent:(ViewController *) controller :(float) x:(float) y:(float) width:(float) height
{
mainController = controller;
[self addComponents:x :y :width :height];
[self attachActions];
return self;
}
-(void)addComponents :(float) x:(float) y:(float) width:(float) height{
UIView *vw = [[UIView alloc] initWithFrame:CGRectMake(x,y, width, height) ];
[[vw layer] setBorderColor:[[UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1] CGColor]];
[[vw layer] setBorderWidth:1];
[vw setBackgroundColor:[UIColor whiteColor]];
[vw setTintColor:[UIColor blackColor]];
self.container = vw;
self.view = vw;
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 30, width, height-30) ];
[[webView layer] setBorderColor:
[[UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1] CGColor]];
[[webView layer] setBorderWidth:1];
[webView setBackgroundColor:[UIColor clearColor]];
webView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight );
webView.opaque = NO;
webView.delegate = self;
self.webView = webView;
[vw addSubview:webView];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, width - 220 , 31) ];
[[textField layer] setBorderColor:
[[UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1] CGColor]];
[[textField layer] setBorderWidth:1];
textField.autoresizingMask = (UIViewAutoresizingFlexibleWidth );
textField.layer.zPosition = 2.0f;
self.textField = textField;
[vw addSubview:textField];
self.autoCompleter = [[AutocompletionTableView alloc] initWithTextField:self.textField :self.container : self.webView ] ;
[self.textField addTarget:self.autoCompleter action:@selector(textFieldValueChanged:) forControlEvents:UIControlEventEditingChanged];
UIButton *goButton = [[UIButton alloc] initWithFrame:CGRectMake(textField.bounds.size.width , 0, 30 , 30) ];
[goButton setTitle:@"Go" forState:UIControlStateNormal];
[goButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
goButton.layer.backgroundColor = [[UIColor colorWithRed:0.0f green:122.0f/255.0f blue:1.0f alpha:1.0f ] CGColor];
goButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
self.goButton = goButton;
[vw addSubview:goButton];
UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(textField.bounds.size.width + goButton.bounds.size.width + 20 , 0, 22 , 30) ];
[backButton setBackgroundImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
backButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
self.backButton = backButton;
[vw addSubview:backButton];
UIButton *forwardButton = [[UIButton alloc] initWithFrame:CGRectMake(textField.bounds.size.width + goButton.bounds.size.width + backButton.bounds.size.width + 40, 0, 22 , 30) ];
[forwardButton setBackgroundImage:[UIImage imageNamed:@"forward.png"] forState:UIControlStateNormal];
forwardButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
self.forwardButton = forwardButton;
[vw addSubview:forwardButton];
UIButton *rotateButtonCW = [[UIButton alloc] initWithFrame:CGRectMake(textField.bounds.size.width + goButton.bounds.size.width + backButton.bounds.size.width + forwardButton.bounds.size.width + 60, 0, 30 , 30) ];
[rotateButtonCW setBackgroundImage:[UIImage imageNamed:@"rotate1.png"] forState:UIControlStateNormal];
rotateButtonCW.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
self.rotateButtonCW = rotateButtonCW;
[vw addSubview:rotateButtonCW];
UIButton *rotateButtonCCW = [[UIButton alloc] initWithFrame:CGRectMake(textField.bounds.size.width + goButton.bounds.size.width + backButton.bounds.size.width + forwardButton.bounds.size.width + 60, 0, 30 , 30) ];
[rotateButtonCCW setBackgroundImage:[UIImage imageNamed:@"rotate2.png"] forState:UIControlStateNormal];
rotateButtonCCW.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
self.rotateButtonCCW = rotateButtonCCW;
rotateButtonCCW.hidden = YES;
[vw addSubview:rotateButtonCCW];
UIButton *hideButton = [[UIButton alloc] initWithFrame:CGRectMake(textField.bounds.size.width + goButton.bounds.size.width + backButton.bounds.size.width + forwardButton.bounds.size.width + rotateButtonCW.bounds.size.width + 80, 0, 30 , 30) ];
[hideButton setBackgroundImage:[UIImage imageNamed:@"hide.png"] forState:UIControlStateNormal];
[hideButton setBackgroundColor:[UIColor whiteColor]];
hideButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
self.hideButton = hideButton;
[vw addSubview:hideButton];
UIButton *multiplyButton = [[UIButton alloc] initWithFrame:CGRectMake(textField.bounds.size.width + goButton.bounds.size.width + backButton.bounds.size.width + forwardButton.bounds.size.width + rotateButtonCW.bounds.size.width + 80, 0, 30 , 30) ];
[multiplyButton setBackgroundImage:[UIImage imageNamed:@"multiply.png"] forState:UIControlStateNormal];
[multiplyButton setBackgroundColor:[UIColor whiteColor]];
multiplyButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
self.multiplyButton = multiplyButton;
multiplyButton.hidden = YES;
[vw addSubview:multiplyButton];
viewAdjuster = [[ViewStateController alloc] init];
}
-(void)attachActions{
[self.rotateButtonCW addTarget:self action:@selector(rotateClockwise:)forControlEvents:UIControlEventTouchUpInside];
[self.rotateButtonCCW addTarget:self action:@selector(rotateCounterClockwise:)forControlEvents:UIControlEventTouchUpInside];
[self.goButton addTarget:self action:@selector(loadUrl:)forControlEvents:UIControlEventTouchUpInside];
[self.textField addTarget:self action:@selector(textTouched:)forControlEvents:UIControlEventTouchDown];
[self.backButton addTarget:self action:@selector(back:)forControlEvents:UIControlEventTouchUpInside];
[self.forwardButton addTarget:self action:@selector(forward:)forControlEvents:UIControlEventTouchUpInside];
[self.hideButton addTarget:mainController action:@selector(hide:)forControlEvents:UIControlEventTouchUpInside];
[self.multiplyButton addTarget:mainController action:@selector(show:)forControlEvents:UIControlEventTouchUpInside];
UILongPressGestureRecognizer* gr = [ [UILongPressGestureRecognizer alloc] initWithTarget: mainController action: @selector( onShowMenu: ) ];
gr.delegate = mainController;
[self.webView addGestureRecognizer: gr];
}
- (IBAction)rotateClockwise:(UIButton *)sender {
[viewAdjuster rotateClockwise: self.container : self.rotateButtonCW : self.rotateButtonCCW ];
}
- (IBAction)rotateCounterClockwise:(UIButton *)sender {
[viewAdjuster rotateCounterClockwise: self.container : self.rotateButtonCW : self.rotateButtonCCW ];
}
- (void)hide
{
[viewAdjuster hide: self.container];
}
- (void)show
{
[viewAdjuster show: self.container];
}
- (IBAction)loadUrl:(id)sender {
[self load: [Utility checkUrl:self.textField.text] : self.webView : self.progress];
}
-(void)load: (NSString *)theString : (UIWebView *)webView : (UIProgressView *)progressView {
[self closeSuggestions];
NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:theString]];
[webView loadRequest:req];
}
- (void)closeSuggestions{
[self.autoCompleter hideOptionsView];
}
- (IBAction)back:(id)sender {
if( [self.webView canGoBack] )
[self.webView goBack];
}
- (IBAction)forward:(id)sender {
if( [self.webView canGoForward] )
[self.webView goForward];
}
- (IBAction)textTouched:(id)sender {
[self.view endEditing:YES];
}
- (IBAction)tapScreen:(id)sender {
[self closeSuggestions];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
// This function is called on all location change :
- (BOOL)webView:(UIWebView *)webView2 shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
// Intercept custom location change, URL begins with "js-call:"
if ([[[request URL] absoluteString] hasPrefix:@"js-call:"]) {
NSArray *components = [[[request URL] absoluteString] componentsSeparatedByString:@":"];
NSString *function = [components objectAtIndex:1];
NSString *sourceString = [function stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[TranslationController instance] showTranslation:sourceString];
return NO;
}
// Accept this location change
return YES;
}
-(UIView *) getView
{
return self.container;
}
-(Boolean) isOwnerOfWebView: (id) child
{
if( [child class] == [UIWebView class] )
{
return self.webView == child;
}
if( [child class] == [UIButton class] )
{
if( self.hideButton == child )
{
return true;
}
if( self.multiplyButton == child )
{
return true;
}
}
return false;
}
- (void)changeToSingleView {
[viewAdjuster changeToSingleView:self.multiplyButton : self.hideButton];
}
- (void)changeToMultipleView {
[viewAdjuster changeToMultipleView:self.multiplyButton : self.hideButton];
}
- (ViewPosition) getViewPosition
{
return [viewAdjuster getPosition];
}
- (void) adjustComponents: (NSMutableArray *) components
{
[viewAdjuster adjustComponents:components];
}
@end