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 {
541 NSLog(@"didStartProvisionalLoadForFrame:%@", frame);
544 [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
546 if ([frame parentFrame] == nil) {
552 stage2_ = (id) stage1_;
555 [self setHidesNavigationBar:NO];
557 // XXX: do we still need to do this?
558 [[self navigationItem] setTitle:nil];
561 [self _didStartLoading];
564 - (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
566 NSLog(@"resource:%@ willSendRequest:%@ redirectResponse:%@ fromDataSource:%@", identifier, request, response, source);
572 - (bool) webView:(WebView *)view shouldRunJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
573 return [self _allowJavaScriptPanel];
576 - (bool) webView:(WebView *)view shouldRunJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
577 return [self _allowJavaScriptPanel];
580 - (bool) webView:(WebView *)view shouldRunJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
581 return [self _allowJavaScriptPanel];
584 - (void) webViewClose:(WebView *)view {
590 [[self navigationController] dismissModalViewControllerAnimated:YES];
593 - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button {
594 NSString *context([alert context]);
596 if ([context isEqualToString:@"sensitive"]) {
599 sensitive_ = [NSNumber numberWithBool:YES];
603 sensitive_ = [NSNumber numberWithBool:NO];
607 [alert dismissWithClickedButtonIndex:-1 animated:YES];
608 } else if ([context isEqualToString:@"challenge"]) {
609 id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
613 NSString *username([[alert textFieldAtIndex:0] text]);
614 NSString *password([[alert textFieldAtIndex:1] text]);
616 NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
618 [sender useCredential:credential forAuthenticationChallenge:challenge_];
622 [sender cancelAuthenticationChallenge:challenge_];
628 [challenge_ release];
631 [alert dismissWithClickedButtonIndex:-1 animated:YES];
632 } else if ([context isEqualToString:@"submit"]) {
633 if (button == [alert cancelButtonIndex]) {
634 } else if (button == [alert firstOtherButtonIndex]) {
635 if (request_ != nil) {
636 WebThreadLocked lock;
637 [webview_ loadRequest:request_];
641 [alert dismissWithClickedButtonIndex:-1 animated:YES];
645 - (UIBarButtonItemStyle) rightButtonStyle {
646 if (style_ == nil) normal:
647 return UIBarButtonItemStylePlain;
648 else if ([style_ isEqualToString:@"Normal"])
649 return UIBarButtonItemStylePlain;
650 else if ([style_ isEqualToString:@"Highlighted"])
651 return UIBarButtonItemStyleDone;
655 - (UIBarButtonItem *) customButton {
658 else if (custom_ == [NSNull null])
659 return (UIBarButtonItem *) [NSNull null];
661 return [[[UIBarButtonItem alloc]
662 initWithTitle:static_cast<NSString *>(custom_.operator NSObject *())
663 style:[self rightButtonStyle]
665 action:@selector(customButtonClicked)
669 - (UIBarButtonItem *) leftButton {
670 UINavigationItem *item([self navigationItem]);
671 if ([item backBarButtonItem] != nil && ![item hidesBackButton])
674 if (UINavigationController *navigation = [self navigationController])
675 if ([[navigation parentViewController] modalViewController] == navigation)
676 return [[[UIBarButtonItem alloc]
677 initWithTitle:UCLocalize("CLOSE")
678 style:UIBarButtonItemStylePlain
680 action:@selector(close)
686 - (void) applyLeftButton {
687 [[self navigationItem] setLeftBarButtonItem:[self leftButton]];
690 - (UIBarButtonItem *) rightButton {
694 - (void) applyLoadingTitle {
695 [[self navigationItem] setTitle:UCLocalize("LOADING")];
698 - (void) layoutRightButton {
699 [[loadingitem_ view] addSubview:indicator_];
700 [[loadingitem_ view] bringSubviewToFront:indicator_];
703 - (void) applyRightButton {
704 if ([self isLoading]) {
705 [[self navigationItem] setRightBarButtonItem:loadingitem_ animated:YES];
706 [self performSelector:@selector(layoutRightButton) withObject:nil afterDelay:0];
708 [indicator_ startAnimating];
709 [self applyLoadingTitle];
711 [indicator_ stopAnimating];
713 UIBarButtonItem *button([self customButton]);
715 button = [self rightButton];
716 else if (button == (UIBarButtonItem *) [NSNull null])
719 [[self navigationItem] setRightBarButtonItem:button];
723 - (void) didStartLoading {
724 // Overridden in subclasses.
727 - (void) _didStartLoading {
728 [self applyRightButton];
730 if ([loading_ count] != 1)
733 [delegate_ retainNetworkActivityIndicator];
734 [self didStartLoading];
737 - (void) didFinishLoading {
738 // Overridden in subclasses.
741 - (void) _didFinishLoading {
742 if ([loading_ count] != 0)
745 [self applyRightButton];
746 [[self navigationItem] setTitle:title_];
748 [delegate_ releaseNetworkActivityIndicator];
749 [self didFinishLoading];
753 return [loading_ count] != 0;
756 - (id) initWithWidth:(float)width ofClass:(Class)_class {
757 if ((self = [super init]) != nil) {
758 allowsNavigationAction_ = true;
761 loading_ = [[NSMutableSet alloc] initWithCapacity:5];
763 indirect_ = [[IndirectDelegate alloc] initWithDelegate:self];
765 CGRect bounds([[self view] bounds]);
767 webview_ = [[[CyteWebView alloc] initWithFrame:bounds] autorelease];
768 [webview_ setDelegate:self];
769 [self setView:webview_];
771 if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)])
772 [webview_ setDataDetectorTypes:UIDataDetectorTypeAutomatic];
774 [webview_ setDetectsPhoneNumbers:NO];
776 [webview_ setScalesPageToFit:YES];
778 UIWebDocumentView *document([webview_ _documentView]);
780 // XXX: I think this improves scrolling; the hardcoded-ness sucks
781 [document setTileSize:CGSizeMake(320, 500)];
783 [document setBackgroundColor:[UIColor clearColor]];
785 // XXX: this is terribly (too?) expensive
786 [document setDrawsBackground:NO];
788 WebView *webview([document webView]);
789 WebPreferences *preferences([webview preferences]);
791 // XXX: I have no clue if I actually /want/ this modification
792 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
793 [webview _setLayoutInterval:0];
794 else if ([preferences respondsToSelector:@selector(_setLayoutInterval:)])
795 [preferences _setLayoutInterval:0];
797 [preferences setCacheModel:WebCacheModelDocumentBrowser];
798 [preferences setJavaScriptCanOpenWindowsAutomatically:YES];
799 [preferences setOfflineWebApplicationCacheEnabled:YES];
802 if ([document respondsToSelector:@selector(setAllowsMessaging:)])
803 [document setAllowsMessaging:YES];
804 if ([webview respondsToSelector:@selector(_setAllowsMessaging:)])
805 [webview _setAllowsMessaging:YES];
808 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
809 scroller_ = [webview_ _scrollView];
811 [scroller_ setDirectionalLockEnabled:YES];
812 [scroller_ setDecelerationRate:CYScrollViewDecelerationRateNormal];
813 [scroller_ setDelaysContentTouches:NO];
815 [scroller_ setCanCancelContentTouches:YES];
816 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
817 UIScroller *scroller([webview_ _scroller]);
818 scroller_ = (UIScrollView *) scroller;
820 [scroller setDirectionalScrolling:YES];
821 // XXX: we might be better off /not/ setting this on older systems
822 [scroller setScrollDecelerationFactor:CYScrollViewDecelerationRateNormal]; /* 0.989324 */
823 [scroller setScrollHysteresis:0]; /* 8 */
825 [scroller setThumbDetectionEnabled:NO];
827 // use NO with UIApplicationUseLegacyEvents(YES)
828 [scroller setEventMode:YES];
830 // XXX: this is handled by setBounces, right?
831 //[scroller setAllowsRubberBanding:YES];
834 [scroller_ setFixedBackgroundPattern:YES];
835 [scroller_ setBackgroundColor:[UIColor clearColor]];
836 [scroller_ setClipsSubviews:YES];
838 [scroller_ setBounces:YES];
839 [scroller_ setScrollingEnabled:YES];
840 [scroller_ setShowBackgroundShadow:NO];
842 [self setViewportWidth:width];
844 reloaditem_ = [[UIBarButtonItem alloc]
845 initWithTitle:UCLocalize("RELOAD")
846 style:[self rightButtonStyle]
848 action:@selector(reloadButtonClicked)
851 loadingitem_ = [[UIBarButtonItem alloc]
853 style:UIBarButtonItemStylePlain
855 action:@selector(reloadButtonClicked)
858 indicator_ = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
859 [indicator_ setFrame:CGRectMake(15, 5, [indicator_ frame].size.width, [indicator_ frame].size.height)];
861 UITableView *table([[[UITableView alloc] initWithFrame:bounds style:UITableViewStyleGrouped] autorelease]);
862 [webview_ insertSubview:table atIndex:0];
864 [self applyLeftButton];
865 [self applyRightButton];
867 [table setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
868 [webview_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
869 [indicator_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
873 - (id) initWithWidth:(float)width {
874 return [self initWithWidth:width ofClass:[self class]];
878 return [self initWithWidth:0];
881 - (id) initWithURL:(NSURL *)url {
882 if ((self = [self init]) != nil) {
887 - (void) callFunction:(WebScriptObject *)function {
888 WebThreadLocked lock;
890 WebView *webview([[webview_ _documentView] webView]);
891 WebFrame *frame([webview mainFrame]);
893 JSGlobalContextRef context([frame globalContext]);
894 JSObjectRef object([function JSObject]);
895 JSObjectCallAsFunction(context, object, NULL, 0, NULL, NULL);
898 - (void) reloadButtonClicked {
899 [self reloadURLWithCache:YES];
902 - (void) _customButtonClicked {
903 [self reloadButtonClicked];
906 - (void) customButtonClicked {
908 if (function_ != nil)
909 [self callFunction:function_];
912 [self _customButtonClicked];
915 + (float) defaultWidth {
919 - (void) setNavigationBarStyle:(NSString *)name {
921 if ([name isEqualToString:@"Black"])
922 style = UIBarStyleBlack;
924 style = UIBarStyleDefault;
926 [[[self navigationController] navigationBar] setBarStyle:style];
929 - (void) setNavigationBarTintColor:(UIColor *)color {
930 [[[self navigationController] navigationBar] setTintColor:color];
933 - (void) setBadgeValue:(id)value {
934 [[[self navigationController] tabBarItem] setBadgeValue:value];
937 - (void) setHidesBackButton:(bool)value {
938 [[self navigationItem] setHidesBackButton:value];
939 [self applyLeftButton];
942 - (void) setHidesBackButtonByNumber:(NSNumber *)value {
943 [self setHidesBackButton:[value boolValue]];
946 - (void) dispatchEvent:(NSString *)event {
947 [webview_ dispatchEvent:event];
950 - (bool) hidesNavigationBar {
951 return hidesNavigationBar_;
954 - (void) _setHidesNavigationBar:(bool)value animated:(bool)animated {
956 [[self navigationController] setNavigationBarHidden:(value && [self hidesNavigationBar]) animated:animated];
959 - (void) setHidesNavigationBar:(bool)value {
960 if (hidesNavigationBar_ != value) {
961 hidesNavigationBar_ = value;
962 [self _setHidesNavigationBar:YES animated:YES];
966 - (void) setHidesNavigationBarByNumber:(NSNumber *)value {
967 [self setHidesNavigationBar:[value boolValue]];
970 - (void) viewWillAppear:(BOOL)animated {
973 if ([self hidesNavigationBar])
974 [self _setHidesNavigationBar:YES animated:animated];
976 [self dispatchEvent:@"CydiaViewWillAppear"];
977 [super viewWillAppear:animated];
980 - (void) viewDidAppear:(BOOL)animated {
981 [super viewDidAppear:animated];
982 [self dispatchEvent:@"CydiaViewDidAppear"];
985 - (void) viewWillDisappear:(BOOL)animated {
986 [self dispatchEvent:@"CydiaViewWillDisappear"];
987 [super viewWillDisappear:animated];
989 if ([self hidesNavigationBar])
990 [self _setHidesNavigationBar:NO animated:animated];
995 - (void) viewDidDisappear:(BOOL)animated {
996 [super viewDidDisappear:animated];
997 [self dispatchEvent:@"CydiaViewDidDisappear"];