1 #include "CyteKit/UCPlatform.h"
2 #include "CyteKit/WebViewController.h"
4 #include "CyteKit/MFMailComposeViewController-MailToURL.h"
6 #include "iPhonePrivate.h"
8 #include "CyteKit/Localize.h"
9 #include "CyteKit/WebViewController.h"
10 #include "CyteKit/PerlCompatibleRegEx.hpp"
11 #include "CyteKit/WebThreadLocked.hpp"
13 //#include <QuartzCore/CALayer.h>
14 // XXX: fix the minimum requirement
15 extern NSString * const kCAFilterNearest;
17 #include <WebCore/WebCoreThread.h>
19 #include <WebKit/WebKitErrors.h>
20 #include <WebKit/WebPreferences.h>
22 #include <WebKit/DOMCSSPrimitiveValue.h>
23 #include <WebKit/DOMCSSStyleDeclaration.h>
24 #include <WebKit/DOMDocument.h>
25 #include <WebKit/DOMHTMLBodyElement.h>
26 #include <WebKit/DOMRGBColor.h>
29 #define DefaultTimeout_ 120.0
31 #define ShowInternals 0
35 #define lprintf(args...) fprintf(stderr, args)
37 // XXX: centralize these special class things to some file or mechanism?
38 static Class $MFMailComposeViewController;
40 template <typename Type_>
41 static inline void CYRelease(Type_ &value) {
48 float CYScrollViewDecelerationRateNormal;
50 @interface WebView (Apple)
51 - (void) _setLayoutInterval:(float)interval;
52 - (void) _setAllowsMessaging:(BOOL)allows;
55 @interface WebPreferences (Apple)
56 + (void) _setInitialDefaultTextEncodingToSystemEncoding;
57 - (void) _setLayoutInterval:(NSInteger)interval;
58 - (void) setOfflineWebApplicationCacheEnabled:(BOOL)enabled;
61 /* Indirect Delegate {{{ */
62 @interface IndirectDelegate : NSObject {
63 _transient volatile id delegate_;
66 - (void) setDelegate:(id)delegate;
67 - (id) initWithDelegate:(id)delegate;
70 @implementation IndirectDelegate
72 - (void) setDelegate:(id)delegate {
76 - (id) initWithDelegate:(id)delegate {
81 - (IMP) methodForSelector:(SEL)sel {
82 if (IMP method = [super methodForSelector:sel])
84 fprintf(stderr, "methodForSelector:[%s] == NULL\n", sel_getName(sel));
88 - (BOOL) respondsToSelector:(SEL)sel {
89 if ([super respondsToSelector:sel])
92 // XXX: WebThreadCreateNSInvocation returns nil
95 fprintf(stderr, "[%s]R?%s\n", class_getName(self->isa), sel_getName(sel));
98 return delegate_ == nil ? NO : [delegate_ respondsToSelector:sel];
101 - (NSMethodSignature *) methodSignatureForSelector:(SEL)sel {
102 if (NSMethodSignature *method = [super methodSignatureForSelector:sel])
106 fprintf(stderr, "[%s]S?%s\n", class_getName(self->isa), sel_getName(sel));
109 if (delegate_ != nil)
110 if (NSMethodSignature *sig = [delegate_ methodSignatureForSelector:sel])
113 // XXX: I fucking hate Apple so very very bad
114 return [NSMethodSignature signatureWithObjCTypes:"v@:"];
117 - (void) forwardInvocation:(NSInvocation *)inv {
118 SEL sel = [inv selector];
119 if (delegate_ != nil && [delegate_ respondsToSelector:sel])
120 [inv invokeWithTarget:delegate_];
126 @implementation CyteWebViewController
129 #include "CyteKit/UCInternal.h"
132 + (void) _initialize {
133 [WebPreferences _setInitialDefaultTextEncodingToSystemEncoding];
135 dlopen("/System/Library/Frameworks/MessageUI.framework/MessageUI", RTLD_GLOBAL | RTLD_LAZY);
136 $MFMailComposeViewController = objc_getClass("MFMailComposeViewController");
138 if (float *_UIScrollViewDecelerationRateNormal = reinterpret_cast<float *>(dlsym(RTLD_DEFAULT, "UIScrollViewDecelerationRateNormal")))
139 CYScrollViewDecelerationRateNormal = *_UIScrollViewDecelerationRateNormal;
140 else // XXX: this actually might be fast on some older systems: we should look into this
141 CYScrollViewDecelerationRateNormal = 0.998;
146 NSLog(@"[CyteWebViewController dealloc]");
149 [webview_ setDelegate:nil];
151 [indirect_ setDelegate:nil];
154 if (challenge_ != nil)
155 [challenge_ release];
160 if ([loading_ count] != 0)
161 [delegate_ releaseNetworkActivityIndicator];
164 [reloaditem_ release];
165 [loadingitem_ release];
167 [indicator_ release];
172 - (NSURL *) URLWithURL:(NSURL *)url {
176 - (NSURLRequest *) requestWithURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
178 requestWithURL:[self URLWithURL:url]
180 timeoutInterval:DefaultTimeout_
184 - (void) setURL:(NSURL *)url {
185 _assert(request_ == nil);
186 request_ = [self requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
189 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
190 [self loadRequest:[self requestWithURL:url cachePolicy:policy]];
193 - (void) loadURL:(NSURL *)url {
194 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
197 - (void) loadRequest:(NSURLRequest *)request {
199 NSLog(@"loadRequest:%@", request);
204 WebThreadLocked lock;
205 [webview_ loadRequest:request];
208 - (void) reloadURLWithCache:(BOOL)cache {
212 NSMutableURLRequest *request([request_ mutableCopy]);
213 [request setCachePolicy:(cache ? NSURLRequestUseProtocolCachePolicy : NSURLRequestReloadIgnoringLocalCacheData)];
217 if ([request_ HTTPBody] == nil && [request_ HTTPBodyStream] == nil)
218 [self loadRequest:request_];
220 UIAlertView *alert = [[[UIAlertView alloc]
221 initWithTitle:UCLocalize("RESUBMIT_FORM")
224 cancelButtonTitle:UCLocalize("CANCEL")
226 UCLocalize("SUBMIT"),
230 [alert setContext:@"submit"];
236 [self reloadURLWithCache:YES];
239 - (void) reloadData {
241 [self reloadURLWithCache:YES];
244 - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
247 function_ = function;
249 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
252 - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
255 function_ = function;
257 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
260 - (void) removeButton {
261 custom_ = [NSNull null];
262 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
265 - (void) scrollToBottomAnimated:(NSNumber *)animated {
266 CGSize size([scroller_ contentSize]);
267 CGPoint offset([scroller_ contentOffset]);
268 CGRect frame([scroller_ frame]);
270 if (size.height - offset.y < frame.size.height + 20.f) {
271 CGRect rect = {{0, size.height-1}, {size.width, 1}};
272 [scroller_ scrollRectToVisible:rect animated:[animated boolValue]];
276 - (void) _setViewportWidth {
277 [[webview_ _documentView] setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
280 - (void) setViewportWidth:(float)width {
281 width_ = width != 0 ? width : [[self class] defaultWidth];
282 [self _setViewportWidth];
285 - (void) _setViewportWidthOnMainThread:(NSNumber *)width {
286 [self setViewportWidth:[width floatValue]];
289 - (void) setViewportWidthOnMainThread:(float)width {
290 [self performSelectorOnMainThread:@selector(_setViewportWidthOnMainThread:) withObject:[NSNumber numberWithFloat:width] waitUntilDone:NO];
293 - (void) webViewUpdateViewSettings:(UIWebView *)view {
294 [self _setViewportWidth];
297 - (void) mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
298 [self dismissModalViewControllerAnimated:YES];
301 - (void) _openMailToURL:(NSURL *)url {
302 if ($MFMailComposeViewController != nil && [$MFMailComposeViewController canSendMail]) {
303 MFMailComposeViewController *controller([[[$MFMailComposeViewController alloc] init] autorelease]);
304 [controller setMailComposeDelegate:self];
306 [controller setMailToURL:url];
308 [self presentModalViewController:controller animated:YES];
312 UIApplication *app([UIApplication sharedApplication]);
313 if ([app respondsToSelector:@selector(openURL:asPanel:)])
314 [app openURL:url asPanel:YES];
319 - (bool) _allowJavaScriptPanel {
323 - (bool) allowsNavigationAction {
324 return allowsNavigationAction_;
327 - (void) setAllowsNavigationAction:(bool)value {
328 allowsNavigationAction_ = value;
331 - (void) setAllowsNavigationActionByNumber:(NSNumber *)value {
332 [self setAllowsNavigationAction:[value boolValue]];
335 - (void) popViewControllerWithNumber:(NSNumber *)value {
336 UINavigationController *navigation([self navigationController]);
337 if ([navigation topViewController] == self)
338 [navigation popViewControllerAnimated:[value boolValue]];
341 - (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
342 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
343 [self _didFinishLoading];
345 if ([[error domain] isEqualToString:NSURLErrorDomain] && [error code] == NSURLErrorCancelled)
348 if ([[error domain] isEqualToString:WebKitErrorDomain] && [error code] == WebKitErrorFrameLoadInterruptedByPolicyChange) {
349 request_ = (id) stage2_;
355 if ([frame parentFrame] == nil) {
356 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
357 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
358 [[error localizedDescription] stringByAddingPercentEscapes]
365 - (void) pushRequest:(NSURLRequest *)request asPop:(bool)pop {
366 NSURL *url([request URL]);
368 // XXX: filter to internal usage?
369 CyteViewController *page([delegate_ pageForURL:url forExternal:NO]);
372 CyteWebViewController *browser([[[class_ alloc] init] autorelease]);
373 [browser loadRequest:request];
377 [page setDelegate:delegate_];
380 [[self navigationItem] setTitle:title_];
382 [[self navigationController] pushViewController:page animated:YES];
384 UINavigationController *navigation([[[UINavigationController alloc] initWithRootViewController:page] autorelease]);
386 [navigation setDelegate:delegate_];
388 [[page navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc]
389 initWithTitle:UCLocalize("CLOSE")
390 style:UIBarButtonItemStylePlain
392 action:@selector(close)
395 [delegate_ unloadData];
397 [[self navigationController] presentModalViewController:navigation animated:YES];
401 // CyteWebViewDelegate {{{
402 - (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message {
404 static Pcre irritating("^(?"
405 ":" "The page at .* displayed insecure content from .*\\."
406 "|" "Unsafe JavaScript attempt to access frame with URL .* from frame with URL .*\\. Domains, protocols and ports must match\\."
409 if (NSString *data = [message objectForKey:@"message"])
410 if (irritating(data))
413 NSLog(@"addMessageToConsole:%@", message);
417 - (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
419 NSLog(@"decidePolicyForNavigationAction:%@ request:%@ frame:%@", action, request, frame);
422 if ([frame parentFrame] == nil) {
424 NSURL *url(request == nil ? nil : [request URL]);
426 if (request_ != nil && ![[request_ URL] isEqual:url] && ![self allowsNavigationAction]) {
428 [self pushRequest:request asPop:NO];
435 - (void) webView:(WebView *)view didDecidePolicy:(CYWebPolicyDecision)decision forNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame {
436 if ([frame parentFrame] == nil)
437 if (decision == CYWebPolicyDecisionUse)
439 stage1_ = (id) request_;
444 - (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
446 NSLog(@"decidePolicyForNewWindowAction:%@ request:%@ newFrameName:%@", action, request, frame);
449 NSURL *url([request URL]);
453 if ([frame isEqualToString:@"_open"])
454 [delegate_ openURL:url];
456 NSString *scheme([[url scheme] lowercaseString]);
457 if ([scheme isEqualToString:@"mailto"])
458 [self _openMailToURL:url];
460 [self pushRequest:request asPop:[frame isEqualToString:@"_popup"]];
466 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
469 - (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
471 NSLog(@"didFailLoadWithError:%@ forFrame:%@", error, frame);
474 [self _didFailWithError:error forFrame:frame];
477 - (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
479 NSLog(@"didFailProvisionalLoadWithError:%@ forFrame:%@", error, frame);
482 [self _didFailWithError:error forFrame:frame];
485 - (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame {
486 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
488 if ([frame parentFrame] == nil) {
492 if (DOMDocument *document = [frame DOMDocument])
493 if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"])
494 for (DOMHTMLBodyElement *body in (id) bodies) {
495 DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]);
499 if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
500 if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
501 DOMRGBColor *rgb([color getRGBColorValue]);
503 float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]);
504 float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]);
505 float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]);
506 float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
508 if (red == 0xc7 && green == 0xce && blue == 0xd5)
509 uic = [UIColor pinStripeColor];
512 colorWithRed:(red / 255)
520 [scroller_ setBackgroundColor:(uic ?: [UIColor clearColor])];
525 [self _didFinishLoading];
528 - (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
529 if ([frame parentFrame] != nil)
533 [title_ autorelease];
534 title_ = [title retain];
536 [[self navigationItem] setTitle:title_];
539 - (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame {
540 [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
542 if ([frame parentFrame] == nil) {
548 stage2_ = (id) stage1_;
551 [self setHidesNavigationBar:NO];
553 // XXX: do we still need to do this?
554 [[self navigationItem] setTitle:nil];
557 [self _didStartLoading];
560 - (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
562 NSLog(@"resource:%@ willSendRequest:%@ redirectResponse:%@ fromDataSource:%@", identifier, request, response, source);
568 - (bool) webView:(WebView *)view shouldRunJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
569 return [self _allowJavaScriptPanel];
572 - (bool) webView:(WebView *)view shouldRunJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
573 return [self _allowJavaScriptPanel];
576 - (bool) webView:(WebView *)view shouldRunJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
577 return [self _allowJavaScriptPanel];
580 - (void) webViewClose:(WebView *)view {
586 [[self navigationController] dismissModalViewControllerAnimated:YES];
589 - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button {
590 NSString *context([alert context]);
592 if ([context isEqualToString:@"sensitive"]) {
595 sensitive_ = [NSNumber numberWithBool:YES];
599 sensitive_ = [NSNumber numberWithBool:NO];
603 [alert dismissWithClickedButtonIndex:-1 animated:YES];
604 } else if ([context isEqualToString:@"challenge"]) {
605 id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
609 NSString *username([[alert textFieldAtIndex:0] text]);
610 NSString *password([[alert textFieldAtIndex:1] text]);
612 NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
614 [sender useCredential:credential forAuthenticationChallenge:challenge_];
618 [sender cancelAuthenticationChallenge:challenge_];
624 [challenge_ release];
627 [alert dismissWithClickedButtonIndex:-1 animated:YES];
628 } else if ([context isEqualToString:@"submit"]) {
629 if (button == [alert cancelButtonIndex]) {
630 } else if (button == [alert firstOtherButtonIndex]) {
631 if (request_ != nil) {
632 WebThreadLocked lock;
633 [webview_ loadRequest:request_];
637 [alert dismissWithClickedButtonIndex:-1 animated:YES];
641 - (UIBarButtonItemStyle) rightButtonStyle {
642 if (style_ == nil) normal:
643 return UIBarButtonItemStylePlain;
644 else if ([style_ isEqualToString:@"Normal"])
645 return UIBarButtonItemStylePlain;
646 else if ([style_ isEqualToString:@"Highlighted"])
647 return UIBarButtonItemStyleDone;
651 - (UIBarButtonItem *) customButton {
654 else if (custom_ == [NSNull null])
655 return (UIBarButtonItem *) [NSNull null];
657 return [[[UIBarButtonItem alloc]
658 initWithTitle:static_cast<NSString *>(custom_.operator NSObject *())
659 style:[self rightButtonStyle]
661 action:@selector(customButtonClicked)
665 - (UIBarButtonItem *) leftButton {
666 UINavigationItem *item([self navigationItem]);
667 if ([item backBarButtonItem] != nil && ![item hidesBackButton])
670 if (UINavigationController *navigation = [self navigationController])
671 if ([[navigation parentViewController] modalViewController] == navigation)
672 return [[[UIBarButtonItem alloc]
673 initWithTitle:UCLocalize("CLOSE")
674 style:UIBarButtonItemStylePlain
676 action:@selector(close)
682 - (void) applyLeftButton {
683 [[self navigationItem] setLeftBarButtonItem:[self leftButton]];
686 - (UIBarButtonItem *) rightButton {
690 - (void) applyLoadingTitle {
691 [[self navigationItem] setTitle:UCLocalize("LOADING")];
694 - (void) layoutRightButton {
695 [[loadingitem_ view] addSubview:indicator_];
696 [[loadingitem_ view] bringSubviewToFront:indicator_];
699 - (void) applyRightButton {
700 if ([self isLoading]) {
701 [[self navigationItem] setRightBarButtonItem:loadingitem_ animated:YES];
702 [self performSelector:@selector(layoutRightButton) withObject:nil afterDelay:0];
704 [indicator_ startAnimating];
705 [self applyLoadingTitle];
707 [indicator_ stopAnimating];
709 UIBarButtonItem *button([self customButton]);
711 button = [self rightButton];
712 else if (button == (UIBarButtonItem *) [NSNull null])
715 [[self navigationItem] setRightBarButtonItem:button];
719 - (void) didStartLoading {
720 // Overridden in subclasses.
723 - (void) _didStartLoading {
724 [self applyRightButton];
726 if ([loading_ count] != 1)
729 [delegate_ retainNetworkActivityIndicator];
730 [self didStartLoading];
733 - (void) didFinishLoading {
734 // Overridden in subclasses.
737 - (void) _didFinishLoading {
738 if ([loading_ count] != 0)
741 [self applyRightButton];
742 [[self navigationItem] setTitle:title_];
744 [delegate_ releaseNetworkActivityIndicator];
745 [self didFinishLoading];
749 return [loading_ count] != 0;
752 - (id) initWithWidth:(float)width ofClass:(Class)_class {
753 if ((self = [super init]) != nil) {
754 allowsNavigationAction_ = true;
757 loading_ = [[NSMutableSet alloc] initWithCapacity:5];
759 indirect_ = [[IndirectDelegate alloc] initWithDelegate:self];
761 CGRect bounds([[self view] bounds]);
763 webview_ = [[[CyteWebView alloc] initWithFrame:bounds] autorelease];
764 [webview_ setDelegate:self];
765 [self setView:webview_];
767 if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)])
768 [webview_ setDataDetectorTypes:UIDataDetectorTypeAutomatic];
770 [webview_ setDetectsPhoneNumbers:NO];
772 [webview_ setScalesPageToFit:YES];
774 UIWebDocumentView *document([webview_ _documentView]);
776 // XXX: I think this improves scrolling; the hardcoded-ness sucks
777 [document setTileSize:CGSizeMake(320, 500)];
779 [document setBackgroundColor:[UIColor clearColor]];
781 // XXX: this is terribly (too?) expensive
782 [document setDrawsBackground:NO];
784 WebView *webview([document webView]);
785 WebPreferences *preferences([webview preferences]);
787 // XXX: I have no clue if I actually /want/ this modification
788 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
789 [webview _setLayoutInterval:0];
790 else if ([preferences respondsToSelector:@selector(_setLayoutInterval:)])
791 [preferences _setLayoutInterval:0];
793 [preferences setCacheModel:WebCacheModelDocumentBrowser];
794 [preferences setJavaScriptCanOpenWindowsAutomatically:YES];
795 [preferences setOfflineWebApplicationCacheEnabled:YES];
798 if ([document respondsToSelector:@selector(setAllowsMessaging:)])
799 [document setAllowsMessaging:YES];
800 if ([webview respondsToSelector:@selector(_setAllowsMessaging:)])
801 [webview _setAllowsMessaging:YES];
804 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
805 scroller_ = [webview_ _scrollView];
807 [scroller_ setDirectionalLockEnabled:YES];
808 [scroller_ setDecelerationRate:CYScrollViewDecelerationRateNormal];
809 [scroller_ setDelaysContentTouches:NO];
811 [scroller_ setCanCancelContentTouches:YES];
812 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
813 UIScroller *scroller([webview_ _scroller]);
814 scroller_ = (UIScrollView *) scroller;
816 [scroller setDirectionalScrolling:YES];
817 // XXX: we might be better off /not/ setting this on older systems
818 [scroller setScrollDecelerationFactor:CYScrollViewDecelerationRateNormal]; /* 0.989324 */
819 [scroller setScrollHysteresis:0]; /* 8 */
821 [scroller setThumbDetectionEnabled:NO];
823 // use NO with UIApplicationUseLegacyEvents(YES)
824 [scroller setEventMode:YES];
826 // XXX: this is handled by setBounces, right?
827 //[scroller setAllowsRubberBanding:YES];
830 [scroller_ setFixedBackgroundPattern:YES];
831 [scroller_ setBackgroundColor:[UIColor clearColor]];
832 [scroller_ setClipsSubviews:YES];
834 [scroller_ setBounces:YES];
835 [scroller_ setScrollingEnabled:YES];
836 [scroller_ setShowBackgroundShadow:NO];
838 [self setViewportWidth:width];
840 reloaditem_ = [[UIBarButtonItem alloc]
841 initWithTitle:UCLocalize("RELOAD")
842 style:[self rightButtonStyle]
844 action:@selector(reloadButtonClicked)
847 loadingitem_ = [[UIBarButtonItem alloc]
849 style:UIBarButtonItemStylePlain
851 action:@selector(reloadButtonClicked)
854 indicator_ = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
855 [indicator_ setFrame:CGRectMake(15, 5, [indicator_ frame].size.width, [indicator_ frame].size.height)];
857 UITableView *table([[[UITableView alloc] initWithFrame:bounds style:UITableViewStyleGrouped] autorelease]);
858 [webview_ insertSubview:table atIndex:0];
860 [self applyLeftButton];
861 [self applyRightButton];
863 [table setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
864 [webview_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
865 [indicator_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
869 - (id) initWithWidth:(float)width {
870 return [self initWithWidth:width ofClass:[self class]];
874 return [self initWithWidth:0];
877 - (id) initWithURL:(NSURL *)url {
878 if ((self = [self init]) != nil) {
883 - (void) callFunction:(WebScriptObject *)function {
884 WebThreadLocked lock;
886 WebView *webview([[webview_ _documentView] webView]);
887 WebFrame *frame([webview mainFrame]);
889 JSGlobalContextRef context([frame globalContext]);
890 JSObjectRef object([function JSObject]);
891 JSObjectCallAsFunction(context, object, NULL, 0, NULL, NULL);
894 - (void) reloadButtonClicked {
895 [self reloadURLWithCache:YES];
898 - (void) _customButtonClicked {
899 [self reloadButtonClicked];
902 - (void) customButtonClicked {
904 if (function_ != nil)
905 [self callFunction:function_];
908 [self _customButtonClicked];
911 + (float) defaultWidth {
915 - (void) setNavigationBarStyle:(NSString *)name {
917 if ([name isEqualToString:@"Black"])
918 style = UIBarStyleBlack;
920 style = UIBarStyleDefault;
922 [[[self navigationController] navigationBar] setBarStyle:style];
925 - (void) setNavigationBarTintColor:(UIColor *)color {
926 [[[self navigationController] navigationBar] setTintColor:color];
929 - (void) setBadgeValue:(id)value {
930 [[[self navigationController] tabBarItem] setBadgeValue:value];
933 - (void) setHidesBackButton:(bool)value {
934 [[self navigationItem] setHidesBackButton:value];
935 [self applyLeftButton];
938 - (void) setHidesBackButtonByNumber:(NSNumber *)value {
939 [self setHidesBackButton:[value boolValue]];
942 - (void) dispatchEvent:(NSString *)event {
943 [webview_ dispatchEvent:event];
946 - (bool) hidesNavigationBar {
947 return hidesNavigationBar_;
950 - (void) _setHidesNavigationBar:(bool)value animated:(bool)animated {
952 [[self navigationController] setNavigationBarHidden:(value && [self hidesNavigationBar]) animated:animated];
955 - (void) setHidesNavigationBar:(bool)value {
956 if (hidesNavigationBar_ != value) {
957 hidesNavigationBar_ = value;
958 [self _setHidesNavigationBar:YES animated:YES];
962 - (void) setHidesNavigationBarByNumber:(NSNumber *)value {
963 [self setHidesNavigationBar:[value boolValue]];
966 - (void) viewWillAppear:(BOOL)animated {
969 if ([self hidesNavigationBar])
970 [self _setHidesNavigationBar:YES animated:animated];
972 [self dispatchEvent:@"CydiaViewWillAppear"];
973 [super viewWillAppear:animated];
976 - (void) viewDidAppear:(BOOL)animated {
977 [super viewDidAppear:animated];
978 [self dispatchEvent:@"CydiaViewDidAppear"];
981 - (void) viewWillDisappear:(BOOL)animated {
982 [self dispatchEvent:@"CydiaViewWillDisappear"];
983 [super viewWillDisappear:animated];
985 if ([self hidesNavigationBar])
986 [self _setHidesNavigationBar:NO animated:animated];
991 - (void) viewDidDisappear:(BOOL)animated {
992 [super viewDidDisappear:animated];
993 [self dispatchEvent:@"CydiaViewDidDisappear"];