]> git.saurik.com Git - cydia.git/blame - UICaboodle/BrowserView.mm
Use high-resolution icons for purpose badges.
[cydia.git] / UICaboodle / BrowserView.mm
CommitLineData
c21004b9
JF
1#include <UIKit/UIKit.h>
2#include "iPhonePrivate.h"
3
4#include "UCPlatform.h"
5
275d8115
JF
6#include <UICaboodle/BrowserView.h>
7#include <UICaboodle/UCLocalize.h>
43f3d7f6 8
c21004b9 9//#include <QuartzCore/CALayer.h>
43f3d7f6
JF
10// XXX: fix the minimum requirement
11extern NSString * const kCAFilterNearest;
22f8bed9 12
caa427d1 13#include <WebCore/WebCoreThread.h>
c21004b9
JF
14
15#include <WebKit/WebPolicyDelegate.h>
16#include <WebKit/WebPreferences.h>
17
18#include <WebKit/DOMCSSPrimitiveValue.h>
19#include <WebKit/DOMCSSStyleDeclaration.h>
20#include <WebKit/DOMDocument.h>
21#include <WebKit/DOMHTMLBodyElement.h>
22#include <WebKit/DOMRGBColor.h>
23
24//#include <WebCore/Page.h>
25//#include <WebCore/Settings.h>
caa427d1 26
43f3d7f6
JF
27#include "substrate.h"
28
adcb0422 29#define ForSaurik 0
bfc87a4d 30
2634b249
JF
31template <typename Type_>
32static inline void CYRelease(Type_ &value) {
33 if (value != nil) {
34 [value release];
35 value = nil;
36 }
37}
04fe1349 38
9c1605e2
JF
39float CYScrollViewDecelerationRateNormal;
40
2634b249
JF
41@interface WebView (Apple)
42- (void) _setLayoutInterval:(float)interval;
43@end
44
45@interface WebPreferences (Apple)
46+ (void) _setInitialDefaultTextEncodingToSystemEncoding;
47- (void) _setLayoutInterval:(NSInteger)interval;
48- (void) setOfflineWebApplicationCacheEnabled:(BOOL)enabled;
49@end
327624b6 50
aa5d0de7 51/* Indirect Delegate {{{ */
c21004b9
JF
52@interface IndirectDelegate : NSObject <
53 HookProtocol
54> {
aa5d0de7
JF
55 _transient volatile id delegate_;
56}
57
58- (void) setDelegate:(id)delegate;
59- (id) initWithDelegate:(id)delegate;
60@end
61
62@implementation IndirectDelegate
63
64- (void) setDelegate:(id)delegate {
65 delegate_ = delegate;
66}
67
68- (id) initWithDelegate:(id)delegate {
69 delegate_ = delegate;
70 return self;
71}
72
c21004b9
JF
73- (void) didDismissModalViewController {
74 if (delegate_ != nil)
75 return [delegate_ didDismissModalViewController];
76}
77
caa427d1
JF
78- (IMP) methodForSelector:(SEL)sel {
79 if (IMP method = [super methodForSelector:sel])
80 return method;
81 fprintf(stderr, "methodForSelector:[%s] == NULL\n", sel_getName(sel));
82 return NULL;
83}
84
aa5d0de7 85- (BOOL) respondsToSelector:(SEL)sel {
caa427d1
JF
86 if ([super respondsToSelector:sel])
87 return YES;
88 // XXX: WebThreadCreateNSInvocation returns nil
89 //fprintf(stderr, "[%s]R?%s\n", class_getName(self->isa), sel_getName(sel));
90 return delegate_ == nil ? NO : [delegate_ respondsToSelector:sel];
aa5d0de7
JF
91}
92
93- (NSMethodSignature *) methodSignatureForSelector:(SEL)sel {
caa427d1
JF
94 if (NSMethodSignature *method = [super methodSignatureForSelector:sel])
95 return method;
96 //fprintf(stderr, "[%s]S?%s\n", class_getName(self->isa), sel_getName(sel));
aa5d0de7
JF
97 if (delegate_ != nil)
98 if (NSMethodSignature *sig = [delegate_ methodSignatureForSelector:sel])
99 return sig;
100 // XXX: I fucking hate Apple so very very bad
101 return [NSMethodSignature signatureWithObjCTypes:"v@:"];
102}
103
104- (void) forwardInvocation:(NSInvocation *)inv {
105 SEL sel = [inv selector];
106 if (delegate_ != nil && [delegate_ respondsToSelector:sel])
107 [inv invokeWithTarget:delegate_];
108}
109
110@end
111/* }}} */
112
43f3d7f6 113@implementation WebScriptObject (UICaboodle)
caa427d1 114
96f3833b 115- (NSUInteger) count {
caa427d1
JF
116 id length([self valueForKey:@"length"]);
117 if ([length respondsToSelector:@selector(intValue)])
118 return [length intValue];
119 else
120 return 0;
121}
122
123- (id) objectAtIndex:(unsigned)index {
124 return [self webScriptValueAtIndex:index];
125}
126
127@end
128
2634b249
JF
129// CYWebPolicyDecision* {{{
130enum CYWebPolicyDecision {
131 CYWebPolicyDecisionUnknown,
132 CYWebPolicyDecisionDownload,
133 CYWebPolicyDecisionIgnore,
134 CYWebPolicyDecisionUse,
135};
136
137@interface CYWebPolicyDecisionMediator : NSObject <
138 WebPolicyDecisionListener
139> {
140 id<WebPolicyDecisionListener> listener_;
141 CYWebPolicyDecision decision_;
69b82e6a 142}
2634b249
JF
143
144- (id) initWithListener:(id<WebPolicyDecisionListener>)listener;
145
146- (CYWebPolicyDecision) decision;
147- (bool) decided;
148- (bool) decide;
149
69b82e6a
RP
150@end
151
2634b249 152@implementation CYWebPolicyDecisionMediator
69b82e6a 153
2634b249
JF
154- (id) initWithListener:(id<WebPolicyDecisionListener>)listener {
155 if ((self = [super init]) != nil) {
156 listener_ = listener;
157 } return self;
158}
69b82e6a 159
2634b249
JF
160- (CYWebPolicyDecision) decision {
161 return decision_;
162}
163
164- (bool) decided {
165 return decision_ != CYWebPolicyDecisionUnknown;
166}
167
168- (bool) decide {
169 switch (decision_) {
170 case CYWebPolicyDecisionUnknown:
171 default:
0352f238 172 NSLog(@"CYWebPolicyDecisionUnknown");
2634b249
JF
173 return false;
174
175 case CYWebPolicyDecisionDownload: [listener_ download]; break;
176 case CYWebPolicyDecisionIgnore: [listener_ ignore]; break;
177 case CYWebPolicyDecisionUse: [listener_ use]; break;
178 }
179
180 return true;
181}
182
183- (void) download {
184 decision_ = CYWebPolicyDecisionDownload;
69b82e6a
RP
185}
186
2634b249
JF
187- (void) ignore {
188 decision_ = CYWebPolicyDecisionIgnore;
189}
190
191- (void) use {
192 decision_ = CYWebPolicyDecisionUse;
69b82e6a
RP
193}
194
195@end
2634b249 196// }}}
69b82e6a 197
2634b249 198@implementation CYWebView : UIWebView
ea173384 199
2634b249
JF
200- (id) initWithFrame:(CGRect)frame {
201 if ((self = [super initWithFrame:frame]) != nil) {
202 } return self;
203}
ea173384 204
2634b249
JF
205- (void) dealloc {
206 [super dealloc];
207}
22f8bed9 208
2634b249
JF
209- (id<CYWebViewDelegate>) delegate {
210 return (id<CYWebViewDelegate>) [super delegate];
211}
22f8bed9 212
2634b249 213/*- (WebView *) webView:(WebView *)view createWebViewWithRequest:(NSURLRequest *)request {
7880ff90
JF
214 id<CYWebViewDelegate> delegate([self delegate]);
215 WebView *created(nil);
216 if (created == nil && [delegate respondsToSelector:@selector(webView:createWebViewWithRequest:)])
217 created = [delegate webView:view createWebViewWithRequest:request];
d49718a1 218 if (created == nil && [UIWebView instancesRespondToSelector:@selector(webView:createWebViewWithRequest:)])
7880ff90
JF
219 created = [super webView:view createWebViewWithRequest:request];
220 return created;
2634b249 221}*/
ea173384 222
d49718a1 223// webView:decidePolicyForNavigationAction:request:frame:decisionListener: (2.0+) {{{
2634b249 224- (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
d49718a1 225 id<CYWebViewDelegate> delegate([self delegate]);
2634b249 226 CYWebPolicyDecisionMediator *mediator([[[CYWebPolicyDecisionMediator alloc] initWithListener:listener] autorelease]);
d49718a1
JF
227 if (![mediator decided] && [delegate respondsToSelector:@selector(webView:decidePolicyForNavigationAction:request:frame:decisionListener:)])
228 [delegate webView:view decidePolicyForNavigationAction:action request:request frame:frame decisionListener:mediator];
229 if (![mediator decided] && [UIWebView instancesRespondToSelector:@selector(webView:decidePolicyForNavigationAction:request:frame:decisionListener:)])
2634b249
JF
230 [super webView:view decidePolicyForNavigationAction:action request:request frame:frame decisionListener:mediator];
231 [mediator decide];
232}
d49718a1
JF
233// }}}
234// webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener: (3.0+) {{{
235static void $UIWebViewWebViewDelegate$webView$decidePolicyForNewWindowAction$request$newFrameName$decisionListener$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, NSDictionary *action, NSURLRequest *request, NSString *frame, id<WebPolicyDecisionListener> listener) {
236 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
237 if ([uiWebView respondsToSelector:@selector(webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:)])
238 [uiWebView webView:view decidePolicyForNewWindowAction:action request:request newFrameName:frame decisionListener:listener];
239}
ea173384 240
2634b249 241- (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
d49718a1 242 id<CYWebViewDelegate> delegate([self delegate]);
2634b249 243 CYWebPolicyDecisionMediator *mediator([[[CYWebPolicyDecisionMediator alloc] initWithListener:listener] autorelease]);
d49718a1
JF
244 if (![mediator decided] && [delegate respondsToSelector:@selector(webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:)])
245 [delegate webView:view decidePolicyForNewWindowAction:action request:request newFrameName:frame decisionListener:mediator];
246 if (![mediator decided] && [UIWebView instancesRespondToSelector:@selector(webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:)])
2634b249
JF
247 [super webView:view decidePolicyForNewWindowAction:action request:request newFrameName:frame decisionListener:mediator];
248 [mediator decide];
249}
d49718a1 250// }}}
3a1ff609 251// webView:didClearWindowObject:forFrame: (3.2+) {{{
d49718a1
JF
252static void $UIWebViewWebViewDelegate$webView$didClearWindowObject$forFrame$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, WebScriptObject *window, WebFrame *frame) {
253 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
254 if ([uiWebView respondsToSelector:@selector(webView:didClearWindowObject:forFrame:)])
255 [uiWebView webView:view didClearWindowObject:window forFrame:frame];
256}
327624b6 257
2634b249 258- (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
d49718a1
JF
259 id<CYWebViewDelegate> delegate([self delegate]);
260 if ([delegate respondsToSelector:@selector(webView:didClearWindowObject:forFrame:)])
261 [delegate webView:view didClearWindowObject:window forFrame:frame];
262 if ([UIWebView instancesRespondToSelector:@selector(webView:didClearWindowObject:forFrame:)])
2634b249
JF
263 [super webView:view didClearWindowObject:window forFrame:frame];
264}
d49718a1
JF
265// }}}
266// webView:didFailLoadWithError:forFrame: (2.0+) {{{
2634b249 267- (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
d49718a1
JF
268 id<CYWebViewDelegate> delegate([self delegate]);
269 if ([delegate respondsToSelector:@selector(webView:didFailLoadWithError:forFrame:)])
270 [delegate webView:view didFailLoadWithError:error forFrame:frame];
271 if ([UIWebView instancesRespondToSelector:@selector(webView:didFailLoadWithError:forFrame:)])
2634b249 272 [super webView:view didFailLoadWithError:error forFrame:frame];
ea173384 273}
d49718a1
JF
274// }}}
275// webView:didFailProvisionalLoadWithError:forFrame: (2.0+) {{{
2634b249 276- (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
d49718a1
JF
277 id<CYWebViewDelegate> delegate([self delegate]);
278 if ([delegate respondsToSelector:@selector(webView:didFailProvisionalLoadWithError:forFrame:)])
279 [delegate webView:view didFailProvisionalLoadWithError:error forFrame:frame];
280 if ([UIWebView instancesRespondToSelector:@selector(webView:didFailProvisionalLoadWithError:forFrame:)])
2634b249
JF
281 [super webView:view didFailProvisionalLoadWithError:error forFrame:frame];
282}
d49718a1
JF
283// }}}
284// webView:didFinishLoadForFrame: (2.0+) {{{
2634b249 285- (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame {
d49718a1
JF
286 id<CYWebViewDelegate> delegate([self delegate]);
287 if ([delegate respondsToSelector:@selector(webView:didFinishLoadForFrame:)])
288 [delegate webView:view didFinishLoadForFrame:frame];
289 if ([UIWebView instancesRespondToSelector:@selector(webView:didFinishLoadForFrame:)])
2634b249
JF
290 [super webView:view didFinishLoadForFrame:frame];
291}
d49718a1 292// }}}
3a1ff609 293// webView:didReceiveTitle:forFrame: (3.2+) {{{
d49718a1
JF
294static void $UIWebViewWebViewDelegate$webView$didReceiveTitle$forFrame$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, NSString *title, WebFrame *frame) {
295 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
296 if ([uiWebView respondsToSelector:@selector(webView:didReceiveTitle:forFrame:)])
297 [uiWebView webView:view didReceiveTitle:title forFrame:frame];
298}
22f8bed9 299
2634b249 300- (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
d49718a1
JF
301 id<CYWebViewDelegate> delegate([self delegate]);
302 if ([delegate respondsToSelector:@selector(webView:didReceiveTitle:forFrame:)])
303 [delegate webView:view didReceiveTitle:title forFrame:frame];
304 if ([UIWebView instancesRespondToSelector:@selector(webView:didReceiveTitle:forFrame:)])
2634b249
JF
305 [super webView:view didReceiveTitle:title forFrame:frame];
306}
d49718a1
JF
307// }}}
308// webView:didStartProvisionalLoadForFrame: (2.0+) {{{
2634b249 309- (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame {
d49718a1
JF
310 id<CYWebViewDelegate> delegate([self delegate]);
311 if ([delegate respondsToSelector:@selector(webView:didStartProvisionalLoadForFrame:)])
312 [delegate webView:view didStartProvisionalLoadForFrame:frame];
313 if ([UIWebView instancesRespondToSelector:@selector(webView:didStartProvisionalLoadForFrame:)])
2634b249
JF
314 [super webView:view didStartProvisionalLoadForFrame:frame];
315}
d49718a1 316// }}}
3a1ff609 317// webView:resource:willSendRequest:redirectResponse:fromDataSource: (3.2+) {{{
d49718a1
JF
318static NSURLRequest *$UIWebViewWebViewDelegate$webView$resource$willSendRequest$redirectResponse$fromDataSource$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, id identifier, NSURLRequest *request, NSURLResponse *response, WebDataSource *source) {
319 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
320 if ([uiWebView respondsToSelector:@selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:)])
f97db630
JF
321 request = [uiWebView webView:view resource:identifier willSendRequest:request redirectResponse:response fromDataSource:source];
322 return request;
d49718a1 323}
22f8bed9 324
2634b249 325- (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
d49718a1
JF
326 id<CYWebViewDelegate> delegate([self delegate]);
327 if ([UIWebView instancesRespondToSelector:@selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:)])
2634b249 328 request = [super webView:view resource:identifier willSendRequest:request redirectResponse:response fromDataSource:source];
d49718a1
JF
329 if ([delegate respondsToSelector:@selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:)])
330 request = [delegate webView:view resource:identifier willSendRequest:request redirectResponse:response fromDataSource:source];
331 return request;
2634b249 332}
d49718a1 333// }}}
427754d6 334// webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame: (2.1+) {{{
2634b249 335- (void) webView:(WebView *)view runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
d49718a1
JF
336 id<CYWebViewDelegate> delegate([self delegate]);
337 if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:)])
2de07ccc
JF
338 if (
339 ![delegate respondsToSelector:@selector(webView:shouldRunJavaScriptAlertPanelWithMessage:initiatedByFrame:)] ||
340 [delegate webView:view shouldRunJavaScriptAlertPanelWithMessage:message initiatedByFrame:frame]
341 )
2634b249
JF
342 [super webView:view runJavaScriptAlertPanelWithMessage:message initiatedByFrame:frame];
343}
d49718a1 344// }}}
427754d6 345// webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame: (2.1+) {{{
2634b249 346- (BOOL) webView:(WebView *)view runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
d49718a1
JF
347 id<CYWebViewDelegate> delegate([self delegate]);
348 if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame:)])
2de07ccc
JF
349 if (
350 ![delegate respondsToSelector:@selector(webView:shouldRunJavaScriptConfirmPanelWithMessage:initiatedByFrame:)] ||
351 [delegate webView:view shouldRunJavaScriptConfirmPanelWithMessage:message initiatedByFrame:frame]
352 )
2634b249
JF
353 return [super webView:view runJavaScriptConfirmPanelWithMessage:message initiatedByFrame:frame];
354 return NO;
355}
d49718a1 356// }}}
427754d6 357// webView:runJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame: (2.1+) {{{
2634b249 358- (NSString *) webView:(WebView *)view runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
d49718a1
JF
359 id<CYWebViewDelegate> delegate([self delegate]);
360 if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame:)])
2de07ccc
JF
361 if (
362 ![delegate respondsToSelector:@selector(webView:shouldRunJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame:)] ||
363 [delegate webView:view shouldRunJavaScriptTextInputPanelWithPrompt:prompt defaultText:text initiatedByFrame:frame]
364 )
2634b249
JF
365 return [super webView:view runJavaScriptTextInputPanelWithPrompt:prompt defaultText:text initiatedByFrame:frame];
366 return nil;
367}
d49718a1 368// }}}
3a1ff609 369// webViewClose: (3.2+) {{{
d49718a1
JF
370static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view) {
371 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
372 if ([uiWebView respondsToSelector:@selector(webViewClose:)])
373 [uiWebView webViewClose:view];
374}
600d005d 375
2634b249 376- (void) webViewClose:(WebView *)view {
d49718a1
JF
377 id<CYWebViewDelegate> delegate([self delegate]);
378 if ([delegate respondsToSelector:@selector(webViewClose:)])
379 [delegate webViewClose:view];
380 if ([UIWebView instancesRespondToSelector:@selector(webViewClose:)])
2634b249
JF
381 [super webViewClose:view];
382}
d49718a1
JF
383// }}}
384
7e37a676
JF
385- (void) _updateViewSettings {
386 [super _updateViewSettings];
387
388 id<CYWebViewDelegate> delegate([self delegate]);
389 if ([delegate respondsToSelector:@selector(webViewUpdateViewSettings:)])
390 [delegate webViewUpdateViewSettings:self];
391}
392
d49718a1
JF
393+ (void) initialize {
394 if (Class $UIWebViewWebViewDelegate = objc_getClass("UIWebViewWebViewDelegate")) {
395 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:), (IMP) &$UIWebViewWebViewDelegate$webView$decidePolicyForNewWindowAction$request$newFrameName$decisionListener$, "v28@0:4@8@12@16@20@24");
396 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:didClearWindowObject:forFrame:), (IMP) &$UIWebViewWebViewDelegate$webView$didClearWindowObject$forFrame$, "v20@0:4@8@12@16");
397 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:didReceiveTitle:forFrame:), (IMP) &$UIWebViewWebViewDelegate$webView$didReceiveTitle$forFrame$, "v20@0:4@8@12@16");
398 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:), (IMP) &$UIWebViewWebViewDelegate$webView$resource$willSendRequest$redirectResponse$fromDataSource$, "@28@0:4@8@12@16@20@24");
399 class_addMethod($UIWebViewWebViewDelegate, @selector(webViewClose:), (IMP) &$UIWebViewWebViewDelegate$webViewClose$, "v12@0:4@8");
400 }
401}
22f8bed9 402
2634b249 403@end
caa427d1 404
2634b249 405#define ShowInternals 0
652006e7 406#define LogBrowser 0
22f8bed9 407
2634b249 408#define lprintf(args...) fprintf(stderr, args)
22f8bed9 409
2634b249
JF
410@implementation BrowserController
411
412#if ShowInternals
413#include "UICaboodle/UCInternal.h"
22f8bed9
JF
414#endif
415
2634b249
JF
416+ (void) _initialize {
417 [WebPreferences _setInitialDefaultTextEncodingToSystemEncoding];
9c1605e2
JF
418
419 if (float *_UIScrollViewDecelerationRateNormal = reinterpret_cast<float *>(dlsym(RTLD_DEFAULT, "UIScrollViewDecelerationRateNormal")))
420 CYScrollViewDecelerationRateNormal = *_UIScrollViewDecelerationRateNormal;
421 else // XXX: this actually might be fast on some older systems: we should look into this
422 CYScrollViewDecelerationRateNormal = 0.998;
2634b249
JF
423}
424
425- (void) dealloc {
426#if LogBrowser
427 NSLog(@"[BrowserController dealloc]");
428#endif
429
430 [webview_ setDelegate:nil];
431
432 [indirect_ setDelegate:nil];
22f8bed9
JF
433 [indirect_ release];
434
2634b249
JF
435 if (challenge_ != nil)
436 [challenge_ release];
caa427d1 437
3931b718
JF
438 if (request_ != nil)
439 [request_ release];
440
2634b249
JF
441 if (custom_ != nil)
442 [custom_ release];
22f8bed9
JF
443 if (style_ != nil)
444 [style_ release];
2634b249 445
22f8bed9
JF
446 if (function_ != nil)
447 [function_ release];
12b59862
JF
448 if (closer_ != nil)
449 [closer_ release];
22f8bed9 450
22f8bed9
JF
451 if (title_ != nil)
452 [title_ release];
2634b249 453
3931b718
JF
454 [loading_ release];
455
2634b249
JF
456 [reloaditem_ release];
457 [loadingitem_ release];
458
459 [indicator_ release];
bc11cf5b 460
22f8bed9
JF
461 [super dealloc];
462}
463
464- (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
465 [self loadRequest:[NSURLRequest
466 requestWithURL:url
467 cachePolicy:policy
77801ff1 468 timeoutInterval:120.0
22f8bed9
JF
469 ]];
470}
471
472- (void) loadURL:(NSURL *)url {
473 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
474}
475
22f8bed9 476- (void) loadRequest:(NSURLRequest *)request {
0352f238
JF
477#if LogBrowser
478 NSLog(@"loadRequest:%@", request);
479#endif
480
fe468f45 481 error_ = false;
caa427d1
JF
482
483 WebThreadLock();
2634b249 484 [webview_ loadRequest:request];
caa427d1 485 WebThreadUnlock();
22f8bed9
JF
486}
487
488- (void) reloadURL {
22f8bed9
JF
489 if (request_ == nil)
490 return;
491
492 if ([request_ HTTPBody] == nil && [request_ HTTPBodyStream] == nil)
fe468f45 493 [self loadRequest:request_];
22f8bed9 494 else {
79ed082a 495 UIAlertView *alert = [[[UIAlertView alloc]
43f3d7f6 496 initWithTitle:UCLocalize("RESUBMIT_FORM")
79ed082a 497 message:nil
22f8bed9 498 delegate:self
79ed082a
GP
499 cancelButtonTitle:UCLocalize("CANCEL")
500 otherButtonTitles:UCLocalize("SUBMIT"), nil
22f8bed9 501 ] autorelease];
2634b249 502
79ed082a
GP
503 [alert setContext:@"submit"];
504 [alert show];
22f8bed9
JF
505 }
506}
507
22f8bed9 508- (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
2634b249
JF
509 if (custom_ != nil)
510 [custom_ autorelease];
3931b718
JF
511 if (button == nil)
512 custom_ = nil;
513 else
514 custom_ = [[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:button]]] retain];
22f8bed9
JF
515
516 if (style_ != nil)
517 [style_ autorelease];
3931b718
JF
518 if (style == nil)
519 style_ = nil;
520 else
521 style_ = [style retain];
22f8bed9
JF
522
523 if (function_ != nil)
524 [function_ autorelease];
3931b718
JF
525 if (function == nil)
526 function_ = nil;
527 else
528 function_ = [function retain];
12b59862 529
bc11cf5b 530 [self applyRightButton];
22f8bed9
JF
531}
532
533- (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
2634b249
JF
534 if (custom_ != nil)
535 [custom_ autorelease];
3931b718
JF
536 if (button == nil)
537 custom_ = nil;
538 else
539 custom_ = [button retain];
22f8bed9
JF
540
541 if (style_ != nil)
542 [style_ autorelease];
3931b718
JF
543 if (style == nil)
544 style_ = nil;
545 else
546 style_ = [style retain];
22f8bed9
JF
547
548 if (function_ != nil)
549 [function_ autorelease];
3931b718
JF
550 if (function == nil)
551 function_ = nil;
552 else
553 function_ = [function retain];
12b59862 554
bc11cf5b 555 [self applyRightButton];
22f8bed9 556}
22f8bed9 557
caa427d1
JF
558- (void) setPopupHook:(id)function {
559 if (closer_ != nil)
560 [closer_ autorelease];
3931b718
JF
561 if (function == nil)
562 closer_ = nil;
563 else
564 closer_ = [function retain];
caa427d1
JF
565}
566
7e37a676
JF
567- (void) _setViewportWidth {
568 [[webview_ _documentView] setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
569}
570
2634b249
JF
571- (void) setViewportWidth:(float)width {
572 width_ = width != 0 ? width : [[self class] defaultWidth];
7e37a676
JF
573 [self _setViewportWidth];
574}
575
576- (void) webViewUpdateViewSettings:(UIWebView *)view {
577 [self _setViewportWidth];
22f8bed9
JF
578}
579
2634b249
JF
580- (void) _openMailToURL:(NSURL *)url {
581 [[UIApplication sharedApplication] openURL:url];// asPanel:YES];
22f8bed9
JF
582}
583
2634b249
JF
584- (bool) _allowJavaScriptPanel {
585 return true;
22f8bed9
JF
586}
587
2634b249
JF
588- (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
589 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
590 [self _didFinishLoading];
22f8bed9 591
2634b249 592 if ([error code] == NSURLErrorCancelled)
22f8bed9
JF
593 return;
594
caa427d1 595 if ([frame parentFrame] == nil) {
2634b249
JF
596 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
597 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
598 [[error localizedDescription] stringByAddingPercentEscapes]
599 ]]];
22f8bed9 600
2634b249
JF
601 error_ = true;
602 }
603}
caa427d1 604
2634b249
JF
605// CYWebViewDelegate {{{
606- (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
607#if LogBrowser
608 NSLog(@"decidePolicyForNavigationAction:%@ request:%@ frame:%@", action, request, frame);
609#endif
600d005d 610
ee32e123
JF
611 if ([frame parentFrame] == nil) {
612 if (!error_) {
613 if (request_ != nil)
614 [request_ autorelease];
615 if (request == nil)
616 request_ = nil;
617 else
618 request_ = [request retain];
619 }
caa427d1 620 }
22f8bed9
JF
621}
622
2634b249
JF
623- (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
624#if LogBrowser
625 NSLog(@"decidePolicyForNewWindowAction:%@ request:%@ newFrameName:%@", action, request, frame);
626#endif
2e6c1426 627
2634b249
JF
628 NSURL *url([request URL]);
629 if (url == nil)
630 return;
bc11cf5b 631
2634b249
JF
632 if ([frame isEqualToString:@"_open"])
633 [delegate_ openURL:url];
2e6c1426 634
2634b249
JF
635 NSString *scheme([[url scheme] lowercaseString]);
636 if ([scheme isEqualToString:@"mailto"])
637 [self _openMailToURL:url];
a5938ea5 638
2634b249 639 CYViewController *page([delegate_ pageForURL:url hasTag:NULL]);
2fad210a 640
2634b249
JF
641 if (page == nil) {
642 BrowserController *browser([[[class_ alloc] init] autorelease]);
643 [browser loadRequest:request];
644 page = browser;
bc11cf5b 645 }
ce041f4f 646
2634b249 647 [page setDelegate:delegate_];
b5e7eebb 648
2634b249
JF
649 if (![frame isEqualToString:@"_popup"]) {
650 [[self navigationItem] setTitle:title_];
b5e7eebb 651
2634b249
JF
652 [[self navigationController] pushViewController:page animated:YES];
653 } else {
654 UCNavigationController *navigation([[[UCNavigationController alloc] init] autorelease]);
22f8bed9 655
2634b249
JF
656 [navigation setHook:indirect_];
657 [navigation setDelegate:delegate_];
22f8bed9 658
2634b249
JF
659 [navigation setViewControllers:[NSArray arrayWithObject:page]];
660
661 [[page navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc]
662 initWithTitle:UCLocalize("CLOSE")
663 style:UIBarButtonItemStylePlain
664 target:page
665 action:@selector(close)
666 ] autorelease]];
667
668 [[self navigationController] presentModalViewController:navigation animated:YES];
669 }
22f8bed9 670
2634b249 671 [listener ignore];
22f8bed9
JF
672}
673
2634b249 674- (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
22f8bed9
JF
675}
676
2634b249
JF
677- (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
678#if LogBrowser
679 NSLog(@"didFailLoadWithError:%@ forFrame:%@", error, frame);
680#endif
681
682 [self _didFailWithError:error forFrame:frame];
22f8bed9
JF
683}
684
2634b249
JF
685- (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
686#if LogBrowser
687 NSLog(@"didFailProvisionalLoadWithError:%@ forFrame:%@", error, frame);
688#endif
689
690 [self _didFailWithError:error forFrame:frame];
22f8bed9
JF
691}
692
2634b249 693- (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame {
7592e053 694 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
22f8bed9 695
caa427d1 696 if ([frame parentFrame] == nil) {
22f8bed9
JF
697 if (DOMDocument *document = [frame DOMDocument])
698 if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"])
96f3833b 699 for (DOMHTMLBodyElement *body in (id) bodies) {
22f8bed9
JF
700 DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]);
701
702 bool colored(false);
703
704 if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
fe468f45
JF
705 if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
706 DOMRGBColor *rgb([color getRGBColorValue]);
707
708 float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]);
709 float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]);
710 float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]);
711 float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
712
713 UIColor *uic(nil);
714
715 if (red == 0xc7 && green == 0xce && blue == 0xd5)
4f6056df 716 uic = [UIColor groupTableViewBackgroundColor];
fe468f45
JF
717 else if (alpha != 0)
718 uic = [UIColor
719 colorWithRed:(red / 255)
720 green:(green / 255)
721 blue:(blue / 255)
722 alpha:alpha
723 ];
724
725 if (uic != nil) {
726 colored = true;
727 [scroller_ setBackgroundColor:uic];
728 }
22f8bed9
JF
729 }
730 }
731
732 if (!colored)
4f6056df 733 [scroller_ setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
22f8bed9
JF
734 break;
735 }
736 }
737
2634b249 738 [self _didFinishLoading];
22f8bed9
JF
739}
740
2634b249
JF
741- (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
742 if ([frame parentFrame] != nil)
743 return;
caa427d1 744
3931b718
JF
745 if (title_ != nil)
746 [title_ autorelease];
2634b249 747 title_ = [title retain];
3931b718 748
2634b249
JF
749 [[self navigationItem] setTitle:title_];
750}
22f8bed9 751
2634b249
JF
752- (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame {
753 [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
7592e053 754
caa427d1 755 if ([frame parentFrame] == nil) {
2634b249
JF
756 CYRelease(title_);
757 CYRelease(custom_);
758 CYRelease(style_);
759 CYRelease(function_);
760 CYRelease(closer_);
761
762 // XXX: do we still need to do this?
763 [[self navigationItem] setTitle:nil];
caa427d1 764 }
fe468f45 765
2634b249 766 [self _didStartLoading];
caa427d1
JF
767}
768
2634b249 769- (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
0352f238
JF
770#if LogBrowser
771 NSLog(@"resource:%@ willSendRequest:%@ redirectResponse:%@ fromDataSource:%@", identifier, request, response, source);
772#endif
773
2634b249 774 return request;
22f8bed9
JF
775}
776
2634b249
JF
777- (bool) webView:(WebView *)view shouldRunJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
778 return [self _allowJavaScriptPanel];
22f8bed9
JF
779}
780
2634b249
JF
781- (bool) webView:(WebView *)view shouldRunJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
782 return [self _allowJavaScriptPanel];
600d005d
JF
783}
784
2634b249
JF
785- (bool) webView:(WebView *)view shouldRunJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
786 return [self _allowJavaScriptPanel];
600d005d
JF
787}
788
2634b249
JF
789- (void) webViewClose:(WebView *)view {
790 [self close];
600d005d 791}
2634b249 792// }}}
600d005d 793
2634b249
JF
794- (void) close {
795 [[self navigationController] dismissModalViewControllerAnimated:YES];
600d005d
JF
796}
797
2634b249
JF
798- (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button {
799 NSString *context([alert context]);
600d005d 800
2634b249
JF
801 if ([context isEqualToString:@"sensitive"]) {
802 switch (button) {
803 case 1:
804 sensitive_ = [NSNumber numberWithBool:YES];
805 break;
600d005d 806
2634b249
JF
807 case 2:
808 sensitive_ = [NSNumber numberWithBool:NO];
809 break;
810 }
600d005d 811
2634b249
JF
812 [alert dismissWithClickedButtonIndex:-1 animated:YES];
813 } else if ([context isEqualToString:@"challenge"]) {
814 id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
caa427d1 815
2634b249
JF
816 switch (button) {
817 case 1: {
818 NSString *username([[alert textFieldAtIndex:0] text]);
819 NSString *password([[alert textFieldAtIndex:1] text]);
600d005d 820
2634b249 821 NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
600d005d 822
2634b249
JF
823 [sender useCredential:credential forAuthenticationChallenge:challenge_];
824 } break;
600d005d 825
2634b249
JF
826 case 2:
827 [sender cancelAuthenticationChallenge:challenge_];
828 break;
600d005d 829
2634b249
JF
830 _nodefault
831 }
600d005d 832
2634b249
JF
833 [challenge_ release];
834 challenge_ = nil;
600d005d 835
2634b249
JF
836 [alert dismissWithClickedButtonIndex:-1 animated:YES];
837 } else if ([context isEqualToString:@"submit"]) {
838 switch (button) {
839 case 1:
840 break;
600d005d 841
2634b249
JF
842 case 2:
843 if (request_ != nil) {
844 WebThreadLock();
845 [webview_ loadRequest:request_];
846 WebThreadUnlock();
847 }
848 break;
600d005d 849
2634b249
JF
850 _nodefault
851 }
600d005d 852
2634b249
JF
853 [alert dismissWithClickedButtonIndex:-1 animated:YES];
854 }
600d005d
JF
855}
856
2634b249
JF
857- (UIBarButtonItemStyle) rightButtonStyle {
858 if (style_ == nil) normal:
859 return UIBarButtonItemStylePlain;
860 else if ([style_ isEqualToString:@"Normal"])
861 return UIBarButtonItemStylePlain;
862 else if ([style_ isEqualToString:@"Highlighted"])
863 return UIBarButtonItemStyleDone;
864 else goto normal;
600d005d
JF
865}
866
2634b249
JF
867- (UIBarButtonItem *) customButton {
868 return [[[UIBarButtonItem alloc]
869 initWithTitle:custom_
870 style:[self rightButtonStyle]
871 target:self
872 action:@selector(customButtonClicked)
873 ] autorelease];
caa427d1
JF
874}
875
2634b249
JF
876- (UIBarButtonItem *) rightButton {
877 return reloaditem_;
eb35c522
JF
878}
879
2634b249
JF
880- (void) applyLoadingTitle {
881 [[self navigationItem] setTitle:UCLocalize("LOADING")];
caa427d1
JF
882}
883
c83a464d
JF
884- (void) layoutRightButton {
885 [[loadingitem_ view] addSubview:indicator_];
886 [[loadingitem_ view] bringSubviewToFront:indicator_];
887}
888
2634b249
JF
889- (void) applyRightButton {
890 if ([self isLoading]) {
891 [[self navigationItem] setRightBarButtonItem:loadingitem_ animated:YES];
c83a464d 892 [self performSelector:@selector(layoutRightButton) withObject:nil afterDelay:0];
8d603a7d
JF
893
894 [indicator_ startAnimating];
2634b249 895 [self applyLoadingTitle];
2634b249 896 } else {
8d603a7d
JF
897 [indicator_ stopAnimating];
898
899 [[self navigationItem] setRightBarButtonItem:(
900 custom_ != nil ? [self customButton] : [self rightButton]
901 ) animated:YES];
2634b249 902 }
caa427d1
JF
903}
904
2634b249
JF
905- (void) _didStartLoading {
906 [self applyRightButton];
caa427d1
JF
907}
908
2634b249
JF
909- (void) _didFinishLoading {
910 if ([loading_ count] != 0)
911 return;
912
913 [self applyRightButton];
914
915 // XXX: wtf?
916 if (![self isLoading])
917 [[self navigationItem] setTitle:title_];
caa427d1
JF
918}
919
2634b249
JF
920- (bool) isLoading {
921 return [loading_ count] != 0;
caa427d1
JF
922}
923
b5e7eebb
GP
924- (id) initWithWidth:(float)width ofClass:(Class)_class {
925 if ((self = [super init]) != nil) {
245cce8a 926 class_ = _class;
2634b249 927 loading_ = [[NSMutableSet alloc] initWithCapacity:5];
bc11cf5b 928
2634b249 929 indirect_ = [[IndirectDelegate alloc] initWithDelegate:self];
caa427d1 930
2634b249
JF
931 webview_ = [[[CYWebView alloc] initWithFrame:[[self view] bounds]] autorelease];
932 [webview_ setDelegate:self];
933 [self setView:webview_];
22f8bed9 934
2634b249
JF
935 if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)])
936 [webview_ setDataDetectorTypes:UIDataDetectorTypeAutomatic];
937 else
938 [webview_ setDetectsPhoneNumbers:NO];
22f8bed9 939
2634b249 940 [webview_ setScalesPageToFit:YES];
22f8bed9 941
2634b249 942 UIWebDocumentView *document([webview_ _documentView]);
ea173384 943
2634b249
JF
944 // XXX: I think this improves scrolling; the hardcoded-ness sucks
945 [document setTileSize:CGSizeMake(320, 500)];
ea173384 946
2634b249 947 [document setBackgroundColor:[UIColor clearColor]];
14f17703
JF
948
949 // XXX: this is terribly (too?) expensive
2634b249 950 [document setDrawsBackground:NO];
ea173384 951
2634b249
JF
952 WebView *webview([document webView]);
953 WebPreferences *preferences([webview preferences]);
22f8bed9 954
2634b249
JF
955 // XXX: I have no clue if I actually /want/ this modification
956 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
957 [webview _setLayoutInterval:0];
958 else if ([preferences respondsToSelector:@selector(_setLayoutInterval:)])
959 [preferences _setLayoutInterval:0];
22f8bed9 960
2634b249
JF
961 [preferences setCacheModel:WebCacheModelDocumentBrowser];
962 [preferences setOfflineWebApplicationCacheEnabled:YES];
22f8bed9 963
2634b249
JF
964 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
965 scroller_ = [webview_ _scrollView];
22f8bed9 966
2634b249 967 [scroller_ setDirectionalLockEnabled:YES];
9c1605e2 968 [scroller_ setDecelerationRate:CYScrollViewDecelerationRateNormal];
2634b249 969 [scroller_ setDelaysContentTouches:NO];
22f8bed9 970
2634b249
JF
971 [scroller_ setCanCancelContentTouches:YES];
972 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
973 UIScroller *scroller([webview_ _scroller]);
974 scroller_ = (UIScrollView *) scroller;
22f8bed9 975
2634b249 976 [scroller setDirectionalScrolling:YES];
9c1605e2
JF
977 // XXX: we might be better off /not/ setting this on older systems
978 [scroller setScrollDecelerationFactor:CYScrollViewDecelerationRateNormal]; /* 0.989324 */
2634b249 979 [scroller setScrollHysteresis:0]; /* 8 */
3e9c9e85 980
2634b249 981 [scroller setThumbDetectionEnabled:NO];
ea173384 982
2634b249
JF
983 // use NO with UIApplicationUseLegacyEvents(YES)
984 [scroller setEventMode:YES];
ea173384 985
2634b249
JF
986 // XXX: this is handled by setBounces, right?
987 //[scroller setAllowsRubberBanding:YES];
22f8bed9 988 }
bc11cf5b 989
2634b249
JF
990 [scroller_ setFixedBackgroundPattern:YES];
991 [scroller_ setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
992 [scroller_ setClipsSubviews:YES];
caa427d1 993
2634b249
JF
994 [scroller_ setBounces:YES];
995 [scroller_ setScrollingEnabled:YES];
996 [scroller_ setShowBackgroundShadow:NO];
caa427d1 997
2634b249 998 [self setViewportWidth:width];
bc11cf5b
JF
999
1000 reloaditem_ = [[UIBarButtonItem alloc]
1001 initWithTitle:UCLocalize("RELOAD")
1002 style:[self rightButtonStyle]
1003 target:self
1004 action:@selector(reloadButtonClicked)
1005 ];
1006
1007 loadingitem_ = [[UIBarButtonItem alloc]
1008 initWithTitle:@" "
1009 style:UIBarButtonItemStylePlain
1010 target:self
1011 action:@selector(reloadButtonClicked)
1012 ];
2634b249 1013
7a11fbb9
GP
1014 indicator_ = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
1015 [indicator_ setFrame:CGRectMake(15, 5, [indicator_ frame].size.width, [indicator_ frame].size.height)];
22f8bed9 1016
2634b249 1017 [webview_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
04fe1349 1018 [indicator_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
22f8bed9
JF
1019 } return self;
1020}
1021
b5e7eebb
GP
1022- (id) initWithWidth:(float)width {
1023 return [self initWithWidth:width ofClass:[self class]];
245cce8a
JF
1024}
1025
b5e7eebb
GP
1026- (id) init {
1027 return [self initWithWidth:0];
3e9c9e85
JF
1028}
1029
2634b249
JF
1030- (void) didDismissModalViewController {
1031 if (closer_ != nil)
1032 [self callFunction:closer_];
22f8bed9
JF
1033}
1034
12b59862 1035- (void) callFunction:(WebScriptObject *)function {
caa427d1
JF
1036 WebThreadLock();
1037
2634b249 1038 WebView *webview([[webview_ _documentView] webView]);
12b59862 1039 WebFrame *frame([webview mainFrame]);
c21004b9
JF
1040 WebPreferences *preferences([webview preferences]);
1041
1042 bool maybe([preferences javaScriptCanOpenWindowsAutomatically]);
1043 [preferences setJavaScriptCanOpenWindowsAutomatically:NO];
22f8bed9 1044
c21004b9 1045 /*id _private(MSHookIvar<id>(webview, "_private"));
12b59862
JF
1046 WebCore::Page *page(_private == nil ? NULL : MSHookIvar<WebCore::Page *>(_private, "page"));
1047 WebCore::Settings *settings(page == NULL ? NULL : page->settings());
22f8bed9 1048
12b59862
JF
1049 bool no;
1050 if (settings == NULL)
1051 no = 0;
1052 else {
1053 no = settings->JavaScriptCanOpenWindowsAutomatically();
1054 settings->setJavaScriptCanOpenWindowsAutomatically(true);
c21004b9 1055 }*/
12b59862 1056
b5e7eebb 1057 if (UIWindow *window = [[self view] window])
43f3d7f6
JF
1058 if (UIResponder *responder = [window firstResponder])
1059 [responder resignFirstResponder];
1060
12b59862
JF
1061 JSObjectRef object([function JSObject]);
1062 JSGlobalContextRef context([frame globalContext]);
1063 JSObjectCallAsFunction(context, object, NULL, 0, NULL, NULL);
1064
c21004b9
JF
1065 /*if (settings != NULL)
1066 settings->setJavaScriptCanOpenWindowsAutomatically(no);*/
1067
1068 [preferences setJavaScriptCanOpenWindowsAutomatically:maybe];
caa427d1
JF
1069
1070 WebThreadUnlock();
12b59862
JF
1071}
1072
ce041f4f 1073- (void) reloadButtonClicked {
12b59862
JF
1074 [self reloadURL];
1075}
1076
719d6c2f
DH
1077- (void) _customButtonClicked {
1078 [self reloadButtonClicked];
1079}
1080
ce041f4f 1081- (void) customButtonClicked {
caa427d1
JF
1082#if !AlwaysReload
1083 if (function_ != nil)
12b59862 1084 [self callFunction:function_];
caa427d1
JF
1085 else
1086#endif
719d6c2f 1087 [self _customButtonClicked];
22f8bed9
JF
1088}
1089
3e9c9e85
JF
1090+ (float) defaultWidth {
1091 return 980;
1092}
1093
a5938ea5 1094@end