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) _openMailToURL:(NSURL *)url {
285 if ($MFMailComposeViewController != nil && [$MFMailComposeViewController canSendMail]) {
286 MFMailComposeViewController *controller([[[$MFMailComposeViewController alloc] init] autorelease]);
287 [controller setMailComposeDelegate:self];
289 [controller setMailToURL:url];
291 [self presentModalViewController:controller animated:YES];
295 UIApplication *app([UIApplication sharedApplication]);
296 if ([app respondsToSelector:@selector(openURL:asPanel:)])
297 [app openURL:url asPanel:YES];
302 - (bool) _allowJavaScriptPanel {
306 - (bool) allowsNavigationAction {
307 return allowsNavigationAction_;
310 - (void) setAllowsNavigationAction:(bool)value {
311 allowsNavigationAction_ = value;
314 - (void) setAllowsNavigationActionByNumber:(NSNumber *)value {
315 [self setAllowsNavigationAction:[value boolValue]];
318 - (void) popViewControllerWithNumber:(NSNumber *)value {
319 UINavigationController *navigation([self navigationController]);
320 if ([navigation topViewController] == self)
321 [navigation popViewControllerAnimated:[value boolValue]];
324 - (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
325 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
326 [self _didFinishLoading];
328 if ([[error domain] isEqualToString:NSURLErrorDomain] && [error code] == NSURLErrorCancelled)
331 if ([[error domain] isEqualToString:WebKitErrorDomain] && [error code] == WebKitErrorFrameLoadInterruptedByPolicyChange) {
338 if ([frame parentFrame] == nil) {
339 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
340 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
341 [[error localizedDescription] stringByAddingPercentEscapes]
348 - (void) pushRequest:(NSURLRequest *)request asPop:(bool)pop {
349 NSURL *url([request URL]);
351 // XXX: filter to internal usage?
352 CyteViewController *page([delegate_ pageForURL:url forExternal:NO]);
355 CyteWebViewController *browser([[[class_ alloc] init] autorelease]);
356 [browser loadRequest:request];
360 [page setDelegate:delegate_];
363 [[self navigationItem] setTitle:title_];
365 [[self navigationController] pushViewController:page animated:YES];
367 UINavigationController *navigation([[[UINavigationController alloc] initWithRootViewController:page] autorelease]);
369 [navigation setDelegate:delegate_];
371 [[page navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc]
372 initWithTitle:UCLocalize("CLOSE")
373 style:UIBarButtonItemStylePlain
375 action:@selector(close)
378 [delegate_ unloadData];
380 [[self navigationController] presentModalViewController:navigation animated:YES];
384 // CyteWebViewDelegate {{{
385 - (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message {
387 static Pcre irritating("^(?"
388 ":" "The page at .* displayed insecure content from .*\\."
389 "|" "Unsafe JavaScript attempt to access frame with URL .* from frame with URL .*\\. Domains, protocols and ports must match\\."
392 if (NSString *data = [message objectForKey:@"message"])
393 if (irritating(data))
396 NSLog(@"addMessageToConsole:%@", message);
400 - (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
402 NSLog(@"decidePolicyForNavigationAction:%@ request:%@ frame:%@", action, request, frame);
405 if ([frame parentFrame] == nil) {
407 NSURL *url(request == nil ? nil : [request URL]);
409 if (request_ != nil && ![[request_ URL] isEqual:url] && ![self allowsNavigationAction]) {
411 [self pushRequest:request asPop:NO];
418 - (void) webView:(WebView *)view didDecidePolicy:(CYWebPolicyDecision)decision forNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame {
419 if ([frame parentFrame] == nil)
420 if (decision == CYWebPolicyDecisionUse)
427 - (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
429 NSLog(@"decidePolicyForNewWindowAction:%@ request:%@ newFrameName:%@", action, request, frame);
432 NSURL *url([request URL]);
436 if ([frame isEqualToString:@"_open"])
437 [delegate_ openURL:url];
439 NSString *scheme([[url scheme] lowercaseString]);
440 if ([scheme isEqualToString:@"mailto"])
441 [self _openMailToURL:url];
443 [self pushRequest:request asPop:[frame isEqualToString:@"_popup"]];
449 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
452 - (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
454 NSLog(@"didFailLoadWithError:%@ forFrame:%@", error, frame);
457 [self _didFailWithError:error forFrame:frame];
460 - (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
462 NSLog(@"didFailProvisionalLoadWithError:%@ forFrame:%@", error, frame);
465 [self _didFailWithError:error forFrame:frame];
468 - (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame {
469 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
471 if ([frame parentFrame] == nil) {
475 if (DOMDocument *document = [frame DOMDocument])
476 if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"])
477 for (DOMHTMLBodyElement *body in (id) bodies) {
478 DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]);
482 if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
483 if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
484 DOMRGBColor *rgb([color getRGBColorValue]);
486 float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]);
487 float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]);
488 float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]);
489 float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
491 if (red == 0xc7 && green == 0xce && blue == 0xd5)
492 uic = [UIColor pinStripeColor];
495 colorWithRed:(red / 255)
503 [scroller_ setBackgroundColor:(uic ?: [UIColor clearColor])];
508 [self _didFinishLoading];
511 - (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
512 if ([frame parentFrame] != nil)
517 [[self navigationItem] setTitle:title_];
520 - (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame {
522 NSLog(@"didStartProvisionalLoadForFrame:%@", frame);
525 [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
527 if ([frame parentFrame] == nil) {
533 allowsNavigationAction_ = true;
538 [self setHidesNavigationBar:NO];
540 // XXX: do we still need to do this?
541 [[self navigationItem] setTitle:nil];
544 [self _didStartLoading];
547 - (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
549 NSLog(@"resource:%@ willSendRequest:%@ redirectResponse:%@ fromDataSource:%@", identifier, request, response, source);
555 - (bool) webView:(WebView *)view shouldRunJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
556 return [self _allowJavaScriptPanel];
559 - (bool) webView:(WebView *)view shouldRunJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
560 return [self _allowJavaScriptPanel];
563 - (bool) webView:(WebView *)view shouldRunJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
564 return [self _allowJavaScriptPanel];
567 - (void) webViewClose:(WebView *)view {
573 [[self navigationController] dismissModalViewControllerAnimated:YES];
576 - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button {
577 NSString *context([alert context]);
579 if ([context isEqualToString:@"sensitive"]) {
582 sensitive_ = [NSNumber numberWithBool:YES];
586 sensitive_ = [NSNumber numberWithBool:NO];
590 [alert dismissWithClickedButtonIndex:-1 animated:YES];
591 } else if ([context isEqualToString:@"challenge"]) {
592 id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
596 NSString *username([[alert textFieldAtIndex:0] text]);
597 NSString *password([[alert textFieldAtIndex:1] text]);
599 NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
601 [sender useCredential:credential forAuthenticationChallenge:challenge_];
605 [sender cancelAuthenticationChallenge:challenge_];
613 [alert dismissWithClickedButtonIndex:-1 animated:YES];
614 } else if ([context isEqualToString:@"submit"]) {
615 if (button == [alert cancelButtonIndex]) {
616 } else if (button == [alert firstOtherButtonIndex]) {
617 if (request_ != nil) {
618 WebThreadLocked lock;
619 [webview_ loadRequest:request_];
623 [alert dismissWithClickedButtonIndex:-1 animated:YES];
627 - (UIBarButtonItemStyle) rightButtonStyle {
628 if (style_ == nil) normal:
629 return UIBarButtonItemStylePlain;
630 else if ([style_ isEqualToString:@"Normal"])
631 return UIBarButtonItemStylePlain;
632 else if ([style_ isEqualToString:@"Highlighted"])
633 return UIBarButtonItemStyleDone;
637 - (UIBarButtonItem *) customButton {
640 else if (custom_ == [NSNull null])
641 return (UIBarButtonItem *) [NSNull null];
643 return [[[UIBarButtonItem alloc]
644 initWithTitle:static_cast<NSString *>(custom_.operator NSObject *())
645 style:[self rightButtonStyle]
647 action:@selector(customButtonClicked)
651 - (UIBarButtonItem *) leftButton {
652 UINavigationItem *item([self navigationItem]);
653 if ([item backBarButtonItem] != nil && ![item hidesBackButton])
656 if (UINavigationController *navigation = [self navigationController])
657 if ([[navigation parentViewController] modalViewController] == navigation)
658 return [[[UIBarButtonItem alloc]
659 initWithTitle:UCLocalize("CLOSE")
660 style:UIBarButtonItemStylePlain
662 action:@selector(close)
668 - (void) applyLeftButton {
669 [[self navigationItem] setLeftBarButtonItem:[self leftButton]];
672 - (UIBarButtonItem *) rightButton {
676 - (void) applyLoadingTitle {
677 [[self navigationItem] setTitle:UCLocalize("LOADING")];
680 - (void) layoutRightButton {
681 [[loadingitem_ view] addSubview:indicator_];
682 [[loadingitem_ view] bringSubviewToFront:indicator_];
685 - (void) applyRightButton {
686 if ([self isLoading]) {
687 [[self navigationItem] setRightBarButtonItem:loadingitem_ animated:YES];
688 [self performSelector:@selector(layoutRightButton) withObject:nil afterDelay:0];
690 [indicator_ startAnimating];
691 [self applyLoadingTitle];
693 [indicator_ stopAnimating];
695 UIBarButtonItem *button([self customButton]);
697 button = [self rightButton];
698 else if (button == (UIBarButtonItem *) [NSNull null])
701 [[self navigationItem] setRightBarButtonItem:button];
705 - (void) didStartLoading {
706 // Overridden in subclasses.
709 - (void) _didStartLoading {
710 [self applyRightButton];
712 if ([loading_ count] != 1)
715 [delegate_ retainNetworkActivityIndicator];
716 [self didStartLoading];
719 - (void) didFinishLoading {
720 // Overridden in subclasses.
723 - (void) _didFinishLoading {
724 if ([loading_ count] != 0)
727 [self applyRightButton];
728 [[self navigationItem] setTitle:title_];
730 [delegate_ releaseNetworkActivityIndicator];
731 [self didFinishLoading];
735 return [loading_ count] != 0;
738 - (id) initWithWidth:(float)width ofClass:(Class)_class {
739 if ((self = [super init]) != nil) {
740 allowsNavigationAction_ = true;
743 loading_ = [NSMutableSet setWithCapacity:5];
745 indirect_ = [[[IndirectDelegate alloc] initWithDelegate:self] autorelease];
747 CGRect bounds([[self view] bounds]);
749 webview_ = [[[CyteWebView alloc] initWithFrame:bounds] autorelease];
750 [webview_ setDelegate:self];
751 [self setView:webview_];
753 if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)])
754 [webview_ setDataDetectorTypes:UIDataDetectorTypeAutomatic];
756 [webview_ setDetectsPhoneNumbers:NO];
758 [webview_ setScalesPageToFit:YES];
760 UIWebDocumentView *document([webview_ _documentView]);
762 // XXX: I think this improves scrolling; the hardcoded-ness sucks
763 [document setTileSize:CGSizeMake(320, 500)];
765 [document setBackgroundColor:[UIColor clearColor]];
767 // XXX: this is terribly (too?) expensive
768 [document setDrawsBackground:NO];
770 WebView *webview([document webView]);
771 WebPreferences *preferences([webview preferences]);
773 // XXX: I have no clue if I actually /want/ this modification
774 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
775 [webview _setLayoutInterval:0];
776 else if ([preferences respondsToSelector:@selector(_setLayoutInterval:)])
777 [preferences _setLayoutInterval:0];
779 [preferences setCacheModel:WebCacheModelDocumentBrowser];
780 [preferences setJavaScriptCanOpenWindowsAutomatically:YES];
781 [preferences setOfflineWebApplicationCacheEnabled:YES];
783 if ([webview respondsToSelector:@selector(setShouldUpdateWhileOffscreen:)])
784 [webview setShouldUpdateWhileOffscreen:NO];
787 if ([document respondsToSelector:@selector(setAllowsMessaging:)])
788 [document setAllowsMessaging:YES];
789 if ([webview respondsToSelector:@selector(_setAllowsMessaging:)])
790 [webview _setAllowsMessaging:YES];
793 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
794 scroller_ = [webview_ _scrollView];
796 [scroller_ setDirectionalLockEnabled:YES];
797 [scroller_ setDecelerationRate:CYScrollViewDecelerationRateNormal];
798 [scroller_ setDelaysContentTouches:NO];
800 [scroller_ setCanCancelContentTouches:YES];
801 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
802 UIScroller *scroller([webview_ _scroller]);
803 scroller_ = (UIScrollView *) scroller;
805 [scroller setDirectionalScrolling:YES];
806 // XXX: we might be better off /not/ setting this on older systems
807 [scroller setScrollDecelerationFactor:CYScrollViewDecelerationRateNormal]; /* 0.989324 */
808 [scroller setScrollHysteresis:0]; /* 8 */
810 [scroller setThumbDetectionEnabled:NO];
812 // use NO with UIApplicationUseLegacyEvents(YES)
813 [scroller setEventMode:YES];
815 // XXX: this is handled by setBounces, right?
816 //[scroller setAllowsRubberBanding:YES];
819 [scroller_ setFixedBackgroundPattern:YES];
820 [scroller_ setBackgroundColor:[UIColor clearColor]];
821 [scroller_ setClipsSubviews:YES];
823 [scroller_ setBounces:YES];
824 [scroller_ setScrollingEnabled:YES];
825 [scroller_ setShowBackgroundShadow:NO];
827 [self setViewportWidth:width];
829 reloaditem_ = [[[UIBarButtonItem alloc]
830 initWithTitle:UCLocalize("RELOAD")
831 style:[self rightButtonStyle]
833 action:@selector(reloadButtonClicked)
836 loadingitem_ = [[[UIBarButtonItem alloc]
838 style:UIBarButtonItemStylePlain
840 action:@selector(reloadButtonClicked)
843 indicator_ = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease];
844 [indicator_ setFrame:CGRectMake(15, 5, [indicator_ frame].size.width, [indicator_ frame].size.height)];
846 UITableView *table([[[UITableView alloc] initWithFrame:bounds style:UITableViewStyleGrouped] autorelease]);
847 [webview_ insertSubview:table atIndex:0];
849 [self applyLeftButton];
850 [self applyRightButton];
852 [table setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
853 [webview_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
854 [indicator_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
858 - (id) initWithWidth:(float)width {
859 return [self initWithWidth:width ofClass:[self class]];
863 return [self initWithWidth:0];
866 - (id) initWithURL:(NSURL *)url {
867 if ((self = [self init]) != nil) {
872 - (void) callFunction:(WebScriptObject *)function {
873 WebThreadLocked lock;
875 WebView *webview([[webview_ _documentView] webView]);
876 WebFrame *frame([webview mainFrame]);
878 JSGlobalContextRef context([frame globalContext]);
879 JSObjectRef object([function JSObject]);
880 JSObjectCallAsFunction(context, object, NULL, 0, NULL, NULL);
883 - (void) reloadButtonClicked {
884 [self reloadURLWithCache:YES];
887 - (void) _customButtonClicked {
888 [self reloadButtonClicked];
891 - (void) customButtonClicked {
893 if (function_ != nil)
894 [self callFunction:function_];
897 [self _customButtonClicked];
900 + (float) defaultWidth {
904 - (void) setNavigationBarStyle:(NSString *)name {
906 if ([name isEqualToString:@"Black"])
907 style = UIBarStyleBlack;
909 style = UIBarStyleDefault;
911 [[[self navigationController] navigationBar] setBarStyle:style];
914 - (void) setNavigationBarTintColor:(UIColor *)color {
915 [[[self navigationController] navigationBar] setTintColor:color];
918 - (void) setBadgeValue:(id)value {
919 [[[self navigationController] tabBarItem] setBadgeValue:value];
922 - (void) setHidesBackButton:(bool)value {
923 [[self navigationItem] setHidesBackButton:value];
924 [self applyLeftButton];
927 - (void) setHidesBackButtonByNumber:(NSNumber *)value {
928 [self setHidesBackButton:[value boolValue]];
931 - (void) dispatchEvent:(NSString *)event {
932 [(CyteWebView *) webview_ dispatchEvent:event];
935 - (bool) hidesNavigationBar {
936 return hidesNavigationBar_;
939 - (void) _setHidesNavigationBar:(bool)value animated:(bool)animated {
941 [[self navigationController] setNavigationBarHidden:(value && [self hidesNavigationBar]) animated:animated];
944 - (void) setHidesNavigationBar:(bool)value {
945 if (hidesNavigationBar_ != value) {
946 hidesNavigationBar_ = value;
947 [self _setHidesNavigationBar:YES animated:YES];
951 - (void) setHidesNavigationBarByNumber:(NSNumber *)value {
952 [self setHidesNavigationBar:[value boolValue]];
955 - (void) viewWillAppear:(BOOL)animated {
958 if ([self hidesNavigationBar])
959 [self _setHidesNavigationBar:YES animated:animated];
961 [self dispatchEvent:@"CydiaViewWillAppear"];
962 [super viewWillAppear:animated];
965 - (void) viewDidAppear:(BOOL)animated {
966 [super viewDidAppear:animated];
967 [self dispatchEvent:@"CydiaViewDidAppear"];
970 - (void) viewWillDisappear:(BOOL)animated {
971 [self dispatchEvent:@"CydiaViewWillDisappear"];
972 [super viewWillDisappear:animated];
974 if ([self hidesNavigationBar])
975 [self _setHidesNavigationBar:NO animated:animated];
980 - (void) viewDidDisappear:(BOOL)animated {
981 [super viewDidDisappear:animated];
982 [self dispatchEvent:@"CydiaViewDidDisappear"];