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);
182 WebThreadLocked lock;
183 [webview_ loadRequest:request];
186 - (void) reloadURLWithCache:(BOOL)cache {
190 NSMutableURLRequest *request([request_ mutableCopy]);
191 [request setCachePolicy:(cache ? NSURLRequestUseProtocolCachePolicy : NSURLRequestReloadIgnoringLocalCacheData)];
195 if ([request_ HTTPBody] == nil && [request_ HTTPBodyStream] == nil)
196 [self loadRequest:request_];
198 UIAlertView *alert = [[[UIAlertView alloc]
199 initWithTitle:UCLocalize("RESUBMIT_FORM")
202 cancelButtonTitle:UCLocalize("CANCEL")
204 UCLocalize("SUBMIT"),
208 [alert setContext:@"submit"];
214 [self reloadURLWithCache:YES];
217 - (void) reloadData {
219 [self reloadURLWithCache:YES];
222 - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
225 function_ = function;
227 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
230 - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
233 function_ = function;
235 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
238 - (void) removeButton {
239 custom_ = [NSNull null];
240 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
243 - (void) scrollToBottomAnimated:(NSNumber *)animated {
244 CGSize size([scroller_ contentSize]);
245 CGPoint offset([scroller_ contentOffset]);
246 CGRect frame([scroller_ frame]);
248 if (size.height - offset.y < frame.size.height + 20.f) {
249 CGRect rect = {{0, size.height-1}, {size.width, 1}};
250 [scroller_ scrollRectToVisible:rect animated:[animated boolValue]];
254 - (void) _setViewportWidth {
255 [[webview_ _documentView] setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
258 - (void) setViewportWidth:(float)width {
259 width_ = width != 0 ? width : [[self class] defaultWidth];
260 [self _setViewportWidth];
263 - (void) _setViewportWidthOnMainThread:(NSNumber *)width {
264 [self setViewportWidth:[width floatValue]];
267 - (void) setViewportWidthOnMainThread:(float)width {
268 [self performSelectorOnMainThread:@selector(_setViewportWidthOnMainThread:) withObject:[NSNumber numberWithFloat:width] waitUntilDone:NO];
271 - (void) webViewUpdateViewSettings:(UIWebView *)view {
272 [self _setViewportWidth];
275 - (void) mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
276 [self dismissModalViewControllerAnimated:YES];
279 - (void) _openMailToURL:(NSURL *)url {
280 if ($MFMailComposeViewController != nil && [$MFMailComposeViewController canSendMail]) {
281 MFMailComposeViewController *controller([[[$MFMailComposeViewController alloc] init] autorelease]);
282 [controller setMailComposeDelegate:self];
284 [controller setMailToURL:url];
286 [self presentModalViewController:controller animated:YES];
290 UIApplication *app([UIApplication sharedApplication]);
291 if ([app respondsToSelector:@selector(openURL:asPanel:)])
292 [app openURL:url asPanel:YES];
297 - (bool) _allowJavaScriptPanel {
301 - (bool) allowsNavigationAction {
302 return allowsNavigationAction_;
305 - (void) setAllowsNavigationAction:(bool)value {
306 allowsNavigationAction_ = value;
309 - (void) setAllowsNavigationActionByNumber:(NSNumber *)value {
310 [self setAllowsNavigationAction:[value boolValue]];
313 - (void) popViewControllerWithNumber:(NSNumber *)value {
314 UINavigationController *navigation([self navigationController]);
315 if ([navigation topViewController] == self)
316 [navigation popViewControllerAnimated:[value boolValue]];
319 - (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
320 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
321 [self _didFinishLoading];
323 if ([[error domain] isEqualToString:NSURLErrorDomain] && [error code] == NSURLErrorCancelled)
326 if ([[error domain] isEqualToString:WebKitErrorDomain] && [error code] == WebKitErrorFrameLoadInterruptedByPolicyChange) {
333 if ([frame parentFrame] == nil) {
334 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
335 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
336 [[error localizedDescription] stringByAddingPercentEscapes]
343 - (void) pushRequest:(NSURLRequest *)request asPop:(bool)pop {
344 NSURL *url([request URL]);
346 // XXX: filter to internal usage?
347 CyteViewController *page([delegate_ pageForURL:url forExternal:NO]);
350 CyteWebViewController *browser([[[class_ alloc] init] autorelease]);
351 [browser loadRequest:request];
355 [page setDelegate:delegate_];
358 [[self navigationItem] setTitle:title_];
360 [[self navigationController] pushViewController:page animated:YES];
362 UINavigationController *navigation([[[UINavigationController alloc] initWithRootViewController:page] autorelease]);
364 [navigation setDelegate:delegate_];
366 [[page navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc]
367 initWithTitle:UCLocalize("CLOSE")
368 style:UIBarButtonItemStylePlain
370 action:@selector(close)
373 [delegate_ unloadData];
375 [[self navigationController] presentModalViewController:navigation animated:YES];
379 // CyteWebViewDelegate {{{
380 - (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message {
382 static Pcre irritating("^(?"
383 ":" "The page at .* displayed insecure content from .*\\."
384 "|" "Unsafe JavaScript attempt to access frame with URL .* from frame with URL .*\\. Domains, protocols and ports must match\\."
387 if (NSString *data = [message objectForKey:@"message"])
388 if (irritating(data))
391 NSLog(@"addMessageToConsole:%@", message);
395 - (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
397 NSLog(@"decidePolicyForNavigationAction:%@ request:%@ frame:%@", action, request, frame);
400 if ([frame parentFrame] == nil) {
402 NSURL *url(request == nil ? nil : [request URL]);
404 if (request_ != nil && ![[request_ URL] isEqual:url] && ![self allowsNavigationAction]) {
406 [self pushRequest:request asPop:NO];
413 - (void) webView:(WebView *)view didDecidePolicy:(CYWebPolicyDecision)decision forNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame {
414 if ([frame parentFrame] == nil)
415 if (decision == CYWebPolicyDecisionUse)
422 - (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
424 NSLog(@"decidePolicyForNewWindowAction:%@ request:%@ newFrameName:%@", action, request, frame);
427 NSURL *url([request URL]);
431 if ([frame isEqualToString:@"_open"])
432 [delegate_ openURL:url];
434 NSString *scheme([[url scheme] lowercaseString]);
435 if ([scheme isEqualToString:@"mailto"])
436 [self _openMailToURL:url];
438 [self pushRequest:request asPop:[frame isEqualToString:@"_popup"]];
444 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
447 - (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
449 NSLog(@"didFailLoadWithError:%@ forFrame:%@", error, frame);
452 [self _didFailWithError:error forFrame:frame];
455 - (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
457 NSLog(@"didFailProvisionalLoadWithError:%@ forFrame:%@", error, frame);
460 [self _didFailWithError:error forFrame:frame];
463 - (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame {
464 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
466 if ([frame parentFrame] == nil) {
470 if (DOMDocument *document = [frame DOMDocument])
471 if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"])
472 for (DOMHTMLBodyElement *body in (id) bodies) {
473 DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]);
477 if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
478 if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
479 DOMRGBColor *rgb([color getRGBColorValue]);
481 float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]);
482 float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]);
483 float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]);
484 float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
486 if (red == 0xc7 && green == 0xce && blue == 0xd5)
487 uic = [UIColor pinStripeColor];
490 colorWithRed:(red / 255)
498 [scroller_ setBackgroundColor:(uic ?: [UIColor clearColor])];
503 [self _didFinishLoading];
506 - (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
507 if ([frame parentFrame] != nil)
512 [[self navigationItem] setTitle:title_];
515 - (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame {
517 NSLog(@"didStartProvisionalLoadForFrame:%@", frame);
520 [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
522 if ([frame parentFrame] == nil) {
528 allowsNavigationAction_ = true;
533 [self setHidesNavigationBar:NO];
535 // XXX: do we still need to do this?
536 [[self navigationItem] setTitle:nil];
539 [self _didStartLoading];
542 - (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
544 NSLog(@"resource:%@ willSendRequest:%@ redirectResponse:%@ fromDataSource:%@", identifier, request, response, source);
550 - (bool) webView:(WebView *)view shouldRunJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
551 return [self _allowJavaScriptPanel];
554 - (bool) webView:(WebView *)view shouldRunJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
555 return [self _allowJavaScriptPanel];
558 - (bool) webView:(WebView *)view shouldRunJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
559 return [self _allowJavaScriptPanel];
562 - (void) webViewClose:(WebView *)view {
568 [[self navigationController] dismissModalViewControllerAnimated:YES];
571 - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button {
572 NSString *context([alert context]);
574 if ([context isEqualToString:@"sensitive"]) {
577 sensitive_ = [NSNumber numberWithBool:YES];
581 sensitive_ = [NSNumber numberWithBool:NO];
585 [alert dismissWithClickedButtonIndex:-1 animated:YES];
586 } else if ([context isEqualToString:@"challenge"]) {
587 id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
591 NSString *username([[alert textFieldAtIndex:0] text]);
592 NSString *password([[alert textFieldAtIndex:1] text]);
594 NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
596 [sender useCredential:credential forAuthenticationChallenge:challenge_];
600 [sender cancelAuthenticationChallenge:challenge_];
608 [alert dismissWithClickedButtonIndex:-1 animated:YES];
609 } else if ([context isEqualToString:@"submit"]) {
610 if (button == [alert cancelButtonIndex]) {
611 } else if (button == [alert firstOtherButtonIndex]) {
612 if (request_ != nil) {
613 WebThreadLocked lock;
614 [webview_ loadRequest:request_];
618 [alert dismissWithClickedButtonIndex:-1 animated:YES];
622 - (UIBarButtonItemStyle) rightButtonStyle {
623 if (style_ == nil) normal:
624 return UIBarButtonItemStylePlain;
625 else if ([style_ isEqualToString:@"Normal"])
626 return UIBarButtonItemStylePlain;
627 else if ([style_ isEqualToString:@"Highlighted"])
628 return UIBarButtonItemStyleDone;
632 - (UIBarButtonItem *) customButton {
635 else if (custom_ == [NSNull null])
636 return (UIBarButtonItem *) [NSNull null];
638 return [[[UIBarButtonItem alloc]
639 initWithTitle:static_cast<NSString *>(custom_.operator NSObject *())
640 style:[self rightButtonStyle]
642 action:@selector(customButtonClicked)
646 - (UIBarButtonItem *) leftButton {
647 UINavigationItem *item([self navigationItem]);
648 if ([item backBarButtonItem] != nil && ![item hidesBackButton])
651 if (UINavigationController *navigation = [self navigationController])
652 if ([[navigation parentViewController] modalViewController] == navigation)
653 return [[[UIBarButtonItem alloc]
654 initWithTitle:UCLocalize("CLOSE")
655 style:UIBarButtonItemStylePlain
657 action:@selector(close)
663 - (void) applyLeftButton {
664 [[self navigationItem] setLeftBarButtonItem:[self leftButton]];
667 - (UIBarButtonItem *) rightButton {
671 - (void) applyLoadingTitle {
672 [[self navigationItem] setTitle:UCLocalize("LOADING")];
675 - (void) layoutRightButton {
676 [[loadingitem_ view] addSubview:indicator_];
677 [[loadingitem_ view] bringSubviewToFront:indicator_];
680 - (void) applyRightButton {
681 if ([self isLoading]) {
682 [[self navigationItem] setRightBarButtonItem:loadingitem_ animated:YES];
683 [self performSelector:@selector(layoutRightButton) withObject:nil afterDelay:0];
685 [indicator_ startAnimating];
686 [self applyLoadingTitle];
688 [indicator_ stopAnimating];
690 UIBarButtonItem *button([self customButton]);
692 button = [self rightButton];
693 else if (button == (UIBarButtonItem *) [NSNull null])
696 [[self navigationItem] setRightBarButtonItem:button];
700 - (void) didStartLoading {
701 // Overridden in subclasses.
704 - (void) _didStartLoading {
705 [self applyRightButton];
707 if ([loading_ count] != 1)
710 [delegate_ retainNetworkActivityIndicator];
711 [self didStartLoading];
714 - (void) didFinishLoading {
715 // Overridden in subclasses.
718 - (void) _didFinishLoading {
719 if ([loading_ count] != 0)
722 [self applyRightButton];
723 [[self navigationItem] setTitle:title_];
725 [delegate_ releaseNetworkActivityIndicator];
726 [self didFinishLoading];
730 return [loading_ count] != 0;
733 - (id) initWithWidth:(float)width ofClass:(Class)_class {
734 if ((self = [super init]) != nil) {
735 allowsNavigationAction_ = true;
738 loading_ = [NSMutableSet setWithCapacity:5];
740 indirect_ = [[[IndirectDelegate alloc] initWithDelegate:self] autorelease];
742 CGRect bounds([[self view] bounds]);
744 webview_ = [[[CyteWebView alloc] initWithFrame:bounds] autorelease];
745 [webview_ setDelegate:self];
746 [self setView:webview_];
748 if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)])
749 [webview_ setDataDetectorTypes:UIDataDetectorTypeAutomatic];
751 [webview_ setDetectsPhoneNumbers:NO];
753 [webview_ setScalesPageToFit:YES];
755 UIWebDocumentView *document([webview_ _documentView]);
757 // XXX: I think this improves scrolling; the hardcoded-ness sucks
758 [document setTileSize:CGSizeMake(320, 500)];
760 [document setBackgroundColor:[UIColor clearColor]];
762 // XXX: this is terribly (too?) expensive
763 [document setDrawsBackground:NO];
765 WebView *webview([document webView]);
766 WebPreferences *preferences([webview preferences]);
768 // XXX: I have no clue if I actually /want/ this modification
769 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
770 [webview _setLayoutInterval:0];
771 else if ([preferences respondsToSelector:@selector(_setLayoutInterval:)])
772 [preferences _setLayoutInterval:0];
774 [preferences setCacheModel:WebCacheModelDocumentBrowser];
775 [preferences setJavaScriptCanOpenWindowsAutomatically:YES];
776 [preferences setOfflineWebApplicationCacheEnabled:YES];
779 if ([document respondsToSelector:@selector(setAllowsMessaging:)])
780 [document setAllowsMessaging:YES];
781 if ([webview respondsToSelector:@selector(_setAllowsMessaging:)])
782 [webview _setAllowsMessaging:YES];
785 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
786 scroller_ = [webview_ _scrollView];
788 [scroller_ setDirectionalLockEnabled:YES];
789 [scroller_ setDecelerationRate:CYScrollViewDecelerationRateNormal];
790 [scroller_ setDelaysContentTouches:NO];
792 [scroller_ setCanCancelContentTouches:YES];
793 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
794 UIScroller *scroller([webview_ _scroller]);
795 scroller_ = (UIScrollView *) scroller;
797 [scroller setDirectionalScrolling:YES];
798 // XXX: we might be better off /not/ setting this on older systems
799 [scroller setScrollDecelerationFactor:CYScrollViewDecelerationRateNormal]; /* 0.989324 */
800 [scroller setScrollHysteresis:0]; /* 8 */
802 [scroller setThumbDetectionEnabled:NO];
804 // use NO with UIApplicationUseLegacyEvents(YES)
805 [scroller setEventMode:YES];
807 // XXX: this is handled by setBounces, right?
808 //[scroller setAllowsRubberBanding:YES];
811 [scroller_ setFixedBackgroundPattern:YES];
812 [scroller_ setBackgroundColor:[UIColor clearColor]];
813 [scroller_ setClipsSubviews:YES];
815 [scroller_ setBounces:YES];
816 [scroller_ setScrollingEnabled:YES];
817 [scroller_ setShowBackgroundShadow:NO];
819 [self setViewportWidth:width];
821 reloaditem_ = [[[UIBarButtonItem alloc]
822 initWithTitle:UCLocalize("RELOAD")
823 style:[self rightButtonStyle]
825 action:@selector(reloadButtonClicked)
828 loadingitem_ = [[[UIBarButtonItem alloc]
830 style:UIBarButtonItemStylePlain
832 action:@selector(reloadButtonClicked)
835 indicator_ = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease];
836 [indicator_ setFrame:CGRectMake(15, 5, [indicator_ frame].size.width, [indicator_ frame].size.height)];
838 UITableView *table([[[UITableView alloc] initWithFrame:bounds style:UITableViewStyleGrouped] autorelease]);
839 [webview_ insertSubview:table atIndex:0];
841 [self applyLeftButton];
842 [self applyRightButton];
844 [table setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
845 [webview_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
846 [indicator_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
850 - (id) initWithWidth:(float)width {
851 return [self initWithWidth:width ofClass:[self class]];
855 return [self initWithWidth:0];
858 - (id) initWithURL:(NSURL *)url {
859 if ((self = [self init]) != nil) {
864 - (void) callFunction:(WebScriptObject *)function {
865 WebThreadLocked lock;
867 WebView *webview([[webview_ _documentView] webView]);
868 WebFrame *frame([webview mainFrame]);
870 JSGlobalContextRef context([frame globalContext]);
871 JSObjectRef object([function JSObject]);
872 JSObjectCallAsFunction(context, object, NULL, 0, NULL, NULL);
875 - (void) reloadButtonClicked {
876 [self reloadURLWithCache:YES];
879 - (void) _customButtonClicked {
880 [self reloadButtonClicked];
883 - (void) customButtonClicked {
885 if (function_ != nil)
886 [self callFunction:function_];
889 [self _customButtonClicked];
892 + (float) defaultWidth {
896 - (void) setNavigationBarStyle:(NSString *)name {
898 if ([name isEqualToString:@"Black"])
899 style = UIBarStyleBlack;
901 style = UIBarStyleDefault;
903 [[[self navigationController] navigationBar] setBarStyle:style];
906 - (void) setNavigationBarTintColor:(UIColor *)color {
907 [[[self navigationController] navigationBar] setTintColor:color];
910 - (void) setBadgeValue:(id)value {
911 [[[self navigationController] tabBarItem] setBadgeValue:value];
914 - (void) setHidesBackButton:(bool)value {
915 [[self navigationItem] setHidesBackButton:value];
916 [self applyLeftButton];
919 - (void) setHidesBackButtonByNumber:(NSNumber *)value {
920 [self setHidesBackButton:[value boolValue]];
923 - (void) dispatchEvent:(NSString *)event {
924 [(CyteWebView *) webview_ dispatchEvent:event];
927 - (bool) hidesNavigationBar {
928 return hidesNavigationBar_;
931 - (void) _setHidesNavigationBar:(bool)value animated:(bool)animated {
933 [[self navigationController] setNavigationBarHidden:(value && [self hidesNavigationBar]) animated:animated];
936 - (void) setHidesNavigationBar:(bool)value {
937 if (hidesNavigationBar_ != value) {
938 hidesNavigationBar_ = value;
939 [self _setHidesNavigationBar:YES animated:YES];
943 - (void) setHidesNavigationBarByNumber:(NSNumber *)value {
944 [self setHidesNavigationBar:[value boolValue]];
947 - (void) viewWillAppear:(BOOL)animated {
950 if ([self hidesNavigationBar])
951 [self _setHidesNavigationBar:YES animated:animated];
953 [self dispatchEvent:@"CydiaViewWillAppear"];
954 [super viewWillAppear:animated];
957 - (void) viewDidAppear:(BOOL)animated {
958 [super viewDidAppear:animated];
959 [self dispatchEvent:@"CydiaViewDidAppear"];
962 - (void) viewWillDisappear:(BOOL)animated {
963 [self dispatchEvent:@"CydiaViewWillDisappear"];
964 [super viewWillDisappear:animated];
966 if ([self hidesNavigationBar])
967 [self _setHidesNavigationBar:NO animated:animated];
972 - (void) viewDidDisappear:(BOOL)animated {
973 [super viewDidDisappear:animated];
974 [self dispatchEvent:@"CydiaViewDidDisappear"];