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 #include <objc/runtime.h>
32 #define DefaultTimeout_ 120.0
34 #define ShowInternals 0
38 #define lprintf(args...) fprintf(stderr, args)
40 // XXX: centralize these special class things to some file or mechanism?
41 static Class $MFMailComposeViewController;
43 float CYScrollViewDecelerationRateNormal;
45 @interface WebView (Apple)
46 - (void) _setLayoutInterval:(float)interval;
47 - (void) _setAllowsMessaging:(BOOL)allows;
50 @interface WebPreferences (Apple)
51 + (void) _setInitialDefaultTextEncodingToSystemEncoding;
52 - (void) _setLayoutInterval:(NSInteger)interval;
53 - (void) setOfflineWebApplicationCacheEnabled:(BOOL)enabled;
56 /* Indirect Delegate {{{ */
57 @interface IndirectDelegate : NSObject {
58 _transient volatile id delegate_;
61 - (void) setDelegate:(id)delegate;
62 - (id) initWithDelegate:(id)delegate;
65 @implementation IndirectDelegate
67 - (void) setDelegate:(id)delegate {
71 - (id) initWithDelegate:(id)delegate {
76 - (IMP) methodForSelector:(SEL)sel {
77 if (IMP method = [super methodForSelector:sel])
79 fprintf(stderr, "methodForSelector:[%s] == NULL\n", sel_getName(sel));
83 - (BOOL) respondsToSelector:(SEL)sel {
84 if ([super respondsToSelector:sel])
87 // XXX: WebThreadCreateNSInvocation returns nil
90 fprintf(stderr, "[%s]R?%s\n", class_getName(self->isa), sel_getName(sel));
93 return delegate_ == nil ? NO : [delegate_ respondsToSelector:sel];
96 - (NSMethodSignature *) methodSignatureForSelector:(SEL)sel {
97 if (NSMethodSignature *method = [super methodSignatureForSelector:sel])
101 fprintf(stderr, "[%s]S?%s\n", class_getName(self->isa), sel_getName(sel));
104 if (delegate_ != nil)
105 if (NSMethodSignature *sig = [delegate_ methodSignatureForSelector:sel])
108 // XXX: I fucking hate Apple so very very bad
109 return [NSMethodSignature signatureWithObjCTypes:"v@:"];
112 - (void) forwardInvocation:(NSInvocation *)inv {
113 SEL sel = [inv selector];
114 if (delegate_ != nil && [delegate_ respondsToSelector:sel])
115 [inv invokeWithTarget:delegate_];
121 @implementation CyteWebViewController
124 #include "CyteKit/UCInternal.h"
127 + (void) _initialize {
128 [WebPreferences _setInitialDefaultTextEncodingToSystemEncoding];
130 dlopen("/System/Library/Frameworks/MessageUI.framework/MessageUI", RTLD_GLOBAL | RTLD_LAZY);
131 $MFMailComposeViewController = objc_getClass("MFMailComposeViewController");
133 if (float *_UIScrollViewDecelerationRateNormal = reinterpret_cast<float *>(dlsym(RTLD_DEFAULT, "UIScrollViewDecelerationRateNormal")))
134 CYScrollViewDecelerationRateNormal = *_UIScrollViewDecelerationRateNormal;
135 else // XXX: this actually might be fast on some older systems: we should look into this
136 CYScrollViewDecelerationRateNormal = 0.998;
141 NSLog(@"[CyteWebViewController dealloc]");
144 if ([loading_ count] != 0)
145 [delegate_ releaseNetworkActivityIndicator];
150 - (NSURL *) URLWithURL:(NSURL *)url {
154 - (NSURLRequest *) requestWithURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
156 requestWithURL:[self URLWithURL:url]
158 timeoutInterval:DefaultTimeout_
162 - (void) setURL:(NSURL *)url {
163 _assert(request_ == nil);
164 request_ = [self requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
167 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
168 [self loadRequest:[self requestWithURL:url cachePolicy:policy]];
171 - (void) loadURL:(NSURL *)url {
172 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
175 - (void) loadRequest:(NSURLRequest *)request {
177 NSLog(@"loadRequest:%@", request);
183 WebThreadLocked lock;
184 [webview_ loadRequest:request];
187 - (void) reloadURLWithCache:(BOOL)cache {
191 NSMutableURLRequest *request([request_ mutableCopy]);
192 [request setCachePolicy:(cache ? NSURLRequestUseProtocolCachePolicy : NSURLRequestReloadIgnoringLocalCacheData)];
196 if ([request_ HTTPBody] == nil && [request_ HTTPBodyStream] == nil)
197 [self loadRequest:request_];
199 UIAlertView *alert = [[[UIAlertView alloc]
200 initWithTitle:UCLocalize("RESUBMIT_FORM")
203 cancelButtonTitle:UCLocalize("CANCEL")
205 UCLocalize("SUBMIT"),
209 [alert setContext:@"submit"];
215 [self reloadURLWithCache:YES];
218 - (void) reloadData {
222 [self dispatchEvent:@"CydiaReloadData"];
224 [self reloadURLWithCache:YES];
227 - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
230 function_ = function;
232 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
235 - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
238 function_ = function;
240 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
243 - (void) removeButton {
244 custom_ = [NSNull null];
245 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
248 - (void) scrollToBottomAnimated:(NSNumber *)animated {
249 CGSize size([scroller_ contentSize]);
250 CGPoint offset([scroller_ contentOffset]);
251 CGRect frame([scroller_ frame]);
253 if (size.height - offset.y < frame.size.height + 20.f) {
254 CGRect rect = {{0, size.height-1}, {size.width, 1}};
255 [scroller_ scrollRectToVisible:rect animated:[animated boolValue]];
259 - (void) _setViewportWidth {
260 [[webview_ _documentView] setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
263 - (void) setViewportWidth:(float)width {
264 width_ = width != 0 ? width : [[self class] defaultWidth];
265 [self _setViewportWidth];
268 - (void) _setViewportWidthOnMainThread:(NSNumber *)width {
269 [self setViewportWidth:[width floatValue]];
272 - (void) setViewportWidthOnMainThread:(float)width {
273 [self performSelectorOnMainThread:@selector(_setViewportWidthOnMainThread:) withObject:[NSNumber numberWithFloat:width] waitUntilDone:NO];
276 - (void) webViewUpdateViewSettings:(UIWebView *)view {
277 [self _setViewportWidth];
280 - (void) mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
281 [self dismissModalViewControllerAnimated:YES];
284 - (void) _setupMail:(MFMailComposeViewController *)controller {
287 - (void) _openMailToURL:(NSURL *)url {
288 if ($MFMailComposeViewController != nil && [$MFMailComposeViewController canSendMail]) {
289 MFMailComposeViewController *controller([[[$MFMailComposeViewController alloc] init] autorelease]);
290 [controller setMailComposeDelegate:self];
292 [controller setMailToURL:url];
294 [self _setupMail:controller];
296 [self presentModalViewController:controller animated:YES];
300 UIApplication *app([UIApplication sharedApplication]);
301 if ([app respondsToSelector:@selector(openURL:asPanel:)])
302 [app openURL:url asPanel:YES];
307 - (bool) _allowJavaScriptPanel {
311 - (bool) allowsNavigationAction {
312 return allowsNavigationAction_;
315 - (void) setAllowsNavigationAction:(bool)value {
316 allowsNavigationAction_ = value;
319 - (void) setAllowsNavigationActionByNumber:(NSNumber *)value {
320 [self setAllowsNavigationAction:[value boolValue]];
323 - (void) popViewControllerWithNumber:(NSNumber *)value {
324 UINavigationController *navigation([self navigationController]);
325 if ([navigation topViewController] == self)
326 [navigation popViewControllerAnimated:[value boolValue]];
329 - (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
330 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
331 [self _didFinishLoading];
333 if ([[error domain] isEqualToString:NSURLErrorDomain] && [error code] == NSURLErrorCancelled)
336 if ([[error domain] isEqualToString:WebKitErrorDomain] && [error code] == WebKitErrorFrameLoadInterruptedByPolicyChange) {
343 if ([frame parentFrame] == nil) {
344 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
345 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
346 [[error localizedDescription] stringByAddingPercentEscapes]
353 - (void) pushRequest:(NSURLRequest *)request asPop:(bool)pop {
354 NSURL *url([request URL]);
356 // XXX: filter to internal usage?
357 CyteViewController *page([delegate_ pageForURL:url forExternal:NO]);
360 CyteWebViewController *browser([[[class_ alloc] init] autorelease]);
361 [browser loadRequest:request];
365 [page setDelegate:delegate_];
368 [[self navigationItem] setTitle:title_];
370 [[self navigationController] pushViewController:page animated:YES];
372 UINavigationController *navigation([[[UINavigationController alloc] initWithRootViewController:page] autorelease]);
374 [navigation setDelegate:delegate_];
376 [[page navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc]
377 initWithTitle:UCLocalize("CLOSE")
378 style:UIBarButtonItemStylePlain
380 action:@selector(close)
383 [[self navigationController] presentModalViewController:navigation animated:YES];
387 // CyteWebViewDelegate {{{
388 - (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message {
390 static Pcre irritating("^(?"
391 ":" "The page at .* displayed insecure content from .*\\."
392 "|" "Unsafe JavaScript attempt to access frame with URL .* from frame with URL .*\\. Domains, protocols and ports must match\\."
395 if (NSString *data = [message objectForKey:@"message"])
396 if (irritating(data))
399 NSLog(@"addMessageToConsole:%@", message);
403 - (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
405 NSLog(@"decidePolicyForNavigationAction:%@ request:%@ frame:%@", action, request, frame);
408 if ([frame parentFrame] == nil) {
410 NSURL *url(request == nil ? nil : [request URL]);
412 if (request_ != nil && ![[request_ URL] isEqual:url] && ![self allowsNavigationAction]) {
414 [self pushRequest:request asPop:NO];
421 - (void) webView:(WebView *)view didDecidePolicy:(CYWebPolicyDecision)decision forNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame {
422 if ([frame parentFrame] == nil)
423 if (decision == CYWebPolicyDecisionUse)
430 - (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
432 NSLog(@"decidePolicyForNewWindowAction:%@ request:%@ newFrameName:%@", action, request, frame);
435 NSURL *url([request URL]);
439 if ([frame isEqualToString:@"_open"])
440 [delegate_ openURL:url];
442 NSString *scheme([[url scheme] lowercaseString]);
443 if ([scheme isEqualToString:@"mailto"])
444 [self _openMailToURL:url];
446 [self pushRequest:request asPop:[frame isEqualToString:@"_popup"]];
452 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
455 - (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
457 NSLog(@"didFailLoadWithError:%@ forFrame:%@", error, frame);
460 [self _didFailWithError:error forFrame:frame];
463 - (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
465 NSLog(@"didFailProvisionalLoadWithError:%@ forFrame:%@", error, frame);
468 [self _didFailWithError:error forFrame:frame];
471 - (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame {
472 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
474 if ([frame parentFrame] == nil) {
478 if (DOMDocument *document = [frame DOMDocument])
479 if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"])
480 for (DOMHTMLBodyElement *body in (id) bodies) {
481 DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]);
485 if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
486 if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
487 DOMRGBColor *rgb([color getRGBColorValue]);
489 float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]);
490 float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]);
491 float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]);
492 float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
494 if (red == 0xc7 && green == 0xce && blue == 0xd5)
495 uic = [UIColor pinStripeColor];
498 colorWithRed:(red / 255)
506 [scroller_ setBackgroundColor:(uic ?: [UIColor clearColor])];
511 [self _didFinishLoading];
514 - (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
515 if ([frame parentFrame] != nil)
520 [[self navigationItem] setTitle:title_];
523 - (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame {
525 NSLog(@"didStartProvisionalLoadForFrame:%@", frame);
528 [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
530 if ([frame parentFrame] == nil) {
536 allowsNavigationAction_ = true;
541 [self setHidesNavigationBar:NO];
543 // XXX: do we still need to do this?
544 [[self navigationItem] setTitle:nil];
547 [self _didStartLoading];
550 - (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
552 NSLog(@"resource:%@ willSendRequest:%@ redirectResponse:%@ fromDataSource:%@", identifier, request, response, source);
558 - (bool) webView:(WebView *)view shouldRunJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
559 return [self _allowJavaScriptPanel];
562 - (bool) webView:(WebView *)view shouldRunJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
563 return [self _allowJavaScriptPanel];
566 - (bool) webView:(WebView *)view shouldRunJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
567 return [self _allowJavaScriptPanel];
570 - (void) webViewClose:(WebView *)view {
576 [[self navigationController] dismissModalViewControllerAnimated:YES];
579 - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button {
580 NSString *context([alert context]);
582 if ([context isEqualToString:@"sensitive"]) {
585 sensitive_ = [NSNumber numberWithBool:YES];
589 sensitive_ = [NSNumber numberWithBool:NO];
593 [alert dismissWithClickedButtonIndex:-1 animated:YES];
594 } else if ([context isEqualToString:@"challenge"]) {
595 id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
599 NSString *username([[alert textFieldAtIndex:0] text]);
600 NSString *password([[alert textFieldAtIndex:1] text]);
602 NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
604 [sender useCredential:credential forAuthenticationChallenge:challenge_];
608 [sender cancelAuthenticationChallenge:challenge_];
616 [alert dismissWithClickedButtonIndex:-1 animated:YES];
617 } else if ([context isEqualToString:@"submit"]) {
618 if (button == [alert cancelButtonIndex]) {
619 } else if (button == [alert firstOtherButtonIndex]) {
620 if (request_ != nil) {
621 WebThreadLocked lock;
622 [webview_ loadRequest:request_];
626 [alert dismissWithClickedButtonIndex:-1 animated:YES];
630 - (UIBarButtonItemStyle) rightButtonStyle {
631 if (style_ == nil) normal:
632 return UIBarButtonItemStylePlain;
633 else if ([style_ isEqualToString:@"Normal"])
634 return UIBarButtonItemStylePlain;
635 else if ([style_ isEqualToString:@"Highlighted"])
636 return UIBarButtonItemStyleDone;
640 - (UIBarButtonItem *) customButton {
643 else if (custom_ == [NSNull null])
644 return (UIBarButtonItem *) [NSNull null];
646 return [[[UIBarButtonItem alloc]
647 initWithTitle:static_cast<NSString *>(custom_.operator NSObject *())
648 style:[self rightButtonStyle]
650 action:@selector(customButtonClicked)
654 - (UIBarButtonItem *) leftButton {
655 UINavigationItem *item([self navigationItem]);
656 if ([item backBarButtonItem] != nil && ![item hidesBackButton])
659 if (UINavigationController *navigation = [self navigationController])
660 if ([[navigation parentViewController] modalViewController] == navigation)
661 return [[[UIBarButtonItem alloc]
662 initWithTitle:UCLocalize("CLOSE")
663 style:UIBarButtonItemStylePlain
665 action:@selector(close)
671 - (void) applyLeftButton {
672 [[self navigationItem] setLeftBarButtonItem:[self leftButton]];
675 - (UIBarButtonItem *) rightButton {
679 - (void) applyLoadingTitle {
680 [[self navigationItem] setTitle:UCLocalize("LOADING")];
683 - (void) layoutRightButton {
684 [[loadingitem_ view] addSubview:indicator_];
685 [[loadingitem_ view] bringSubviewToFront:indicator_];
688 - (void) applyRightButton {
689 if ([self isLoading]) {
690 [[self navigationItem] setRightBarButtonItem:loadingitem_ animated:YES];
691 [self performSelector:@selector(layoutRightButton) withObject:nil afterDelay:0];
693 [indicator_ startAnimating];
694 [self applyLoadingTitle];
696 [indicator_ stopAnimating];
698 UIBarButtonItem *button([self customButton]);
700 button = [self rightButton];
701 else if (button == (UIBarButtonItem *) [NSNull null])
704 [[self navigationItem] setRightBarButtonItem:button animated:YES];
708 - (void) didStartLoading {
709 // Overridden in subclasses.
712 - (void) _didStartLoading {
713 [self applyRightButton];
715 if ([loading_ count] != 1)
718 [delegate_ retainNetworkActivityIndicator];
719 [self didStartLoading];
722 - (void) didFinishLoading {
723 // Overridden in subclasses.
726 - (void) _didFinishLoading {
727 if ([loading_ count] != 0)
730 [self applyRightButton];
731 [[self navigationItem] setTitle:title_];
733 [delegate_ releaseNetworkActivityIndicator];
734 [self didFinishLoading];
738 return [loading_ count] != 0;
741 - (id) initWithWidth:(float)width ofClass:(Class)_class {
742 if ((self = [super init]) != nil) {
743 allowsNavigationAction_ = true;
746 loading_ = [NSMutableSet setWithCapacity:5];
748 indirect_ = [[[IndirectDelegate alloc] initWithDelegate:self] autorelease];
750 CGRect bounds([[self view] bounds]);
752 webview_ = [[[CyteWebView alloc] initWithFrame:bounds] autorelease];
753 [webview_ setDelegate:self];
754 [self setView:webview_];
756 if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)])
757 [webview_ setDataDetectorTypes:UIDataDetectorTypeAutomatic];
759 [webview_ setDetectsPhoneNumbers:NO];
761 [webview_ setScalesPageToFit:YES];
763 UIWebDocumentView *document([webview_ _documentView]);
765 // XXX: I think this improves scrolling; the hardcoded-ness sucks
766 [document setTileSize:CGSizeMake(320, 500)];
768 [document setBackgroundColor:[UIColor clearColor]];
770 // XXX: this is terribly (too?) expensive
771 [document setDrawsBackground:NO];
773 WebView *webview([document webView]);
774 WebPreferences *preferences([webview preferences]);
776 // XXX: I have no clue if I actually /want/ this modification
777 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
778 [webview _setLayoutInterval:0];
779 else if ([preferences respondsToSelector:@selector(_setLayoutInterval:)])
780 [preferences _setLayoutInterval:0];
782 [preferences setCacheModel:WebCacheModelDocumentBrowser];
783 [preferences setJavaScriptCanOpenWindowsAutomatically:YES];
784 [preferences setOfflineWebApplicationCacheEnabled:YES];
786 if ([webview respondsToSelector:@selector(setShouldUpdateWhileOffscreen:)])
787 [webview setShouldUpdateWhileOffscreen:NO];
790 if ([document respondsToSelector:@selector(setAllowsMessaging:)])
791 [document setAllowsMessaging:YES];
792 if ([webview respondsToSelector:@selector(_setAllowsMessaging:)])
793 [webview _setAllowsMessaging:YES];
796 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
797 scroller_ = [webview_ _scrollView];
799 [scroller_ setDirectionalLockEnabled:YES];
800 [scroller_ setDecelerationRate:CYScrollViewDecelerationRateNormal];
801 [scroller_ setDelaysContentTouches:NO];
803 [scroller_ setCanCancelContentTouches:YES];
804 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
805 UIScroller *scroller([webview_ _scroller]);
806 scroller_ = (UIScrollView *) scroller;
808 [scroller setDirectionalScrolling:YES];
809 // XXX: we might be better off /not/ setting this on older systems
810 [scroller setScrollDecelerationFactor:CYScrollViewDecelerationRateNormal]; /* 0.989324 */
811 [scroller setScrollHysteresis:0]; /* 8 */
813 [scroller setThumbDetectionEnabled:NO];
815 // use NO with UIApplicationUseLegacyEvents(YES)
816 [scroller setEventMode:YES];
818 // XXX: this is handled by setBounces, right?
819 //[scroller setAllowsRubberBanding:YES];
822 [scroller_ setFixedBackgroundPattern:YES];
823 [scroller_ setBackgroundColor:[UIColor clearColor]];
824 [scroller_ setClipsSubviews:YES];
826 [scroller_ setBounces:YES];
827 [scroller_ setScrollingEnabled:YES];
828 [scroller_ setShowBackgroundShadow:NO];
830 [self setViewportWidth:width];
832 reloaditem_ = [[[UIBarButtonItem alloc]
833 initWithTitle:UCLocalize("RELOAD")
834 style:[self rightButtonStyle]
836 action:@selector(reloadButtonClicked)
839 loadingitem_ = [[[UIBarButtonItem alloc]
841 style:UIBarButtonItemStylePlain
843 action:@selector(reloadButtonClicked)
846 indicator_ = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease];
847 [indicator_ setFrame:CGRectMake(15, 5, [indicator_ frame].size.width, [indicator_ frame].size.height)];
849 UITableView *table([[[UITableView alloc] initWithFrame:bounds style:UITableViewStyleGrouped] autorelease]);
850 [webview_ insertSubview:table atIndex:0];
852 [self applyLeftButton];
853 [self applyRightButton];
855 [table setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
856 [webview_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
857 [indicator_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
861 - (id) initWithWidth:(float)width {
862 return [self initWithWidth:width ofClass:[self class]];
866 return [self initWithWidth:0];
869 - (id) initWithURL:(NSURL *)url {
870 if ((self = [self init]) != nil) {
875 - (void) callFunction:(WebScriptObject *)function {
876 WebThreadLocked lock;
878 WebView *webview([[webview_ _documentView] webView]);
879 WebFrame *frame([webview mainFrame]);
881 JSGlobalContextRef context([frame globalContext]);
882 JSObjectRef object([function JSObject]);
883 JSObjectCallAsFunction(context, object, NULL, 0, NULL, NULL);
886 - (void) reloadButtonClicked {
887 [self reloadURLWithCache:YES];
890 - (void) _customButtonClicked {
891 [self reloadButtonClicked];
894 - (void) customButtonClicked {
896 if (function_ != nil)
897 [self callFunction:function_];
900 [self _customButtonClicked];
903 + (float) defaultWidth {
907 - (void) setNavigationBarStyle:(NSString *)name {
909 if ([name isEqualToString:@"Black"])
910 style = UIBarStyleBlack;
912 style = UIBarStyleDefault;
914 [[[self navigationController] navigationBar] setBarStyle:style];
917 - (void) setNavigationBarTintColor:(UIColor *)color {
918 [[[self navigationController] navigationBar] setTintColor:color];
921 - (void) setBadgeValue:(id)value {
922 [[[self navigationController] tabBarItem] setBadgeValue:value];
925 - (void) setHidesBackButton:(bool)value {
926 [[self navigationItem] setHidesBackButton:value];
927 [self applyLeftButton];
930 - (void) setHidesBackButtonByNumber:(NSNumber *)value {
931 [self setHidesBackButton:[value boolValue]];
934 - (void) dispatchEvent:(NSString *)event {
935 [(CyteWebView *) webview_ dispatchEvent:event];
938 - (bool) hidesNavigationBar {
939 return hidesNavigationBar_;
942 - (void) _setHidesNavigationBar:(bool)value animated:(bool)animated {
944 [[self navigationController] setNavigationBarHidden:(value && [self hidesNavigationBar]) animated:animated];
947 - (void) setHidesNavigationBar:(bool)value {
948 if (hidesNavigationBar_ != value) {
949 hidesNavigationBar_ = value;
950 [self _setHidesNavigationBar:YES animated:YES];
954 - (void) setHidesNavigationBarByNumber:(NSNumber *)value {
955 [self setHidesNavigationBar:[value boolValue]];
958 - (void) viewWillAppear:(BOOL)animated {
961 if ([self hidesNavigationBar])
962 [self _setHidesNavigationBar:YES animated:animated];
964 [self dispatchEvent:@"CydiaViewWillAppear"];
965 [super viewWillAppear:animated];
968 - (void) viewDidAppear:(BOOL)animated {
969 [super viewDidAppear:animated];
970 [self dispatchEvent:@"CydiaViewDidAppear"];
973 - (void) viewWillDisappear:(BOOL)animated {
974 [self dispatchEvent:@"CydiaViewWillDisappear"];
975 [super viewWillDisappear:animated];
977 if ([self hidesNavigationBar])
978 [self _setHidesNavigationBar:NO animated:animated];
983 - (void) viewDidDisappear:(BOOL)animated {
984 [super viewDidDisappear:animated];
985 [self dispatchEvent:@"CydiaViewDidDisappear"];