]> git.saurik.com Git - cydia.git/blobdiff - UICaboodle/BrowserView.mm
Better Queue UI. Discoverable. WebKit JS/objc bridge.
[cydia.git] / UICaboodle / BrowserView.mm
index caa2ba28e4e58da653ee3cdcdf60a69074bf9743..7c123f9796cd49851c166b7ba29b1bc3f6ca8e06 100644 (file)
@@ -148,19 +148,43 @@ static Class $UIWebBrowserView;
 
 @end
 
+@interface BrowserView : UIView {
+@private
+    UIWebDocumentView *documentView;
+}
+@property (nonatomic, retain) UIWebDocumentView *documentView;
+@end
+
+@implementation BrowserView
+
+@synthesize documentView;
+
+- (void)dealloc {
+    [documentView release];
+    [super dealloc];
+}
+
+- (void)layoutSubviews {
+    [super layoutSubviews];
+    if ([documentView respondsToSelector:@selector(setMinimumSize:)])
+        [documentView setMinimumSize:documentView.bounds.size];
+}
+
+@end
+
 #define ShowInternals 0
-#define LogBrowser 0
+#define LogBrowser 1
 
 #define lprintf(args...) fprintf(stderr, args)
 
-@implementation BrowserView
+@implementation BrowserController
 
 #if ShowInternals
 #include "UICaboodle/UCInternal.h"
 #endif
 
 + (void) _initialize {
-    [WebView enableWebThread];
+    //[WebView enableWebThread];
 
     WebPreferences *preferences([WebPreferences standardPreferences]);
     [preferences setCacheModel:WebCacheModelDocumentBrowser];
@@ -183,7 +207,7 @@ static Class $UIWebBrowserView;
 
 - (void) dealloc {
 #if LogBrowser
-    NSLog(@"[BrowserView dealloc]");
+    NSLog(@"[BrowserController dealloc]");
 #endif
 
     if (challenge_ != nil)
@@ -255,6 +279,11 @@ static Class $UIWebBrowserView;
         [sensitive_ release];
     if (title_ != nil)
         [title_ release];
+    if (reloaditem_ != nil)
+        [reloaditem_ release];
+    if (loadingitem_ != nil)
+        [loadingitem_ release];
+        
     [super dealloc];
 }
 
@@ -311,7 +340,7 @@ static Class $UIWebBrowserView;
 
 /* XXX: WebThreadLock? */
 - (void) _fixScroller:(CGRect)bounds {
-    float extra;
+       float extra;
 
     if (!editing_ || $UIFormAssistant == nil)
         extra = 0;
@@ -360,10 +389,10 @@ static Class $UIWebBrowserView;
     [self view:sender didSetFrame:frame];
 }
 
-- (void) pushPage:(RVPage *)page {
+- (void) pushPage:(UCViewController *)page {
     [page setDelegate:delegate_];
-    [self setBackButtonTitle:title_];
-    [book_ pushPage:page];
+    [[self navigationItem] setTitle:title_];
+    [[self navigationController] pushViewController:page animated:YES];
 }
 
 - (void) _pushPage {
@@ -371,15 +400,14 @@ static Class $UIWebBrowserView;
         return;
     // WTR: [self autorelease];
     pushed_ = true;
-    [book_ pushPage:self];
+    [[self navigationController] pushViewController:self animated:YES];
 }
 
-- (void) swapPage:(RVPage *)page {
+- (void) swapPage:(UCViewController *)page {
     [page setDelegate:delegate_];
-    if (pushed_)
-        [book_ swapPage:page];
-    else
-        [book_ pushPage:page];
+    if (pushed_) [[self navigationController] popViewControllerAnimated:NO];
+               
+       [[self navigationController] pushViewController:page animated:NO];
 }
 
 - (BOOL) getSpecial:(NSURL *)url swap:(BOOL)swap {
@@ -387,7 +415,7 @@ static Class $UIWebBrowserView;
     NSLog(@"getSpecial:%@", url);
 #endif
 
-    if (RVPage *page = [delegate_ pageForURL:url hasTag:NULL]) {
+    if (UCViewController *page = [delegate_ pageForURL:url hasTag:NULL]) {
         if (swap)
             [self swapPage:page];
         else
@@ -532,7 +560,7 @@ static Class $UIWebBrowserView;
         [function_ autorelease];
     function_ = function == nil ? nil : [function retain];
 
-    [self reloadButtons];
+       [self applyRightButton];
 }
 
 - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
@@ -548,7 +576,7 @@ static Class $UIWebBrowserView;
         [function_ autorelease];
     function_ = function == nil ? nil : [function retain];
 
-    [self reloadButtons];
+       [self applyRightButton];
 }
 
 - (void) setFinishHook:(id)function {
@@ -581,11 +609,11 @@ static Class $UIWebBrowserView;
 }
 
 - (void) webViewClose:(WebView *)sender {
-    [book_ close];
+       [self close];
 }
 
 - (void) close {
-    [book_ close];
+    [[self navigationController] dismissModalViewControllerAnimated:YES];
 }
 
 - (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
@@ -615,25 +643,32 @@ static Class $UIWebBrowserView;
             if ([scheme isEqualToString:@"mailto"])
                 [self _openMailToURL:url];
             else {
-                RVBook *book([[[RVPopUpBook alloc] initWithFrame:[delegate_ popUpBounds]] autorelease]);
-                [book setHook:indirect_];
+                UCNavigationController *navigation([[[UCNavigationController alloc] init] autorelease]);
+                [navigation setHook:indirect_];
 
-                RVPage *page([delegate_ pageForURL:url hasTag:NULL]);
+                UCViewController *page([delegate_ pageForURL:url hasTag:NULL]);
                 if (page == nil) {
                     /* XXX: call createWebViewWithRequest instead? */
 
-                    [self setBackButtonTitle:title_];
-
-                    BrowserView *browser([[[class_ alloc] initWithBook:book] autorelease]);
+                    BrowserController *browser([[[class_ alloc] init] autorelease]);
                     [browser loadURL:url];
                     page = browser;
                 }
 
-                [book setDelegate:delegate_];
+                [navigation setDelegate:delegate_];
                 [page setDelegate:delegate_];
 
-                [book setPage:page];
-                [book_ pushBook:book];
+                [navigation setViewControllers:[NSArray arrayWithObject:page]];
+                               UIBarButtonItem *closeItem = [[UIBarButtonItem alloc]
+                               initWithTitle:UCLocalize("CLOSE")
+                                       style:UIBarButtonItemStylePlain
+                               target:page
+                               action:@selector(close)
+                           ];
+                           [[page navigationItem] setLeftBarButtonItem:closeItem];
+                           [closeItem release];
+                       
+                [[self navigationController] presentModalViewController:navigation animated:YES];
             }
         } else goto unknown;
 
@@ -881,25 +916,34 @@ static Class $UIWebBrowserView;
     NSNumber *value([features objectForKey:@"width"]);
     float width(value == nil ? 0 : [value floatValue]);
 
-    RVBook *book(!popup_ ? book_ : [[[RVPopUpBook alloc] initWithFrame:[delegate_ popUpBounds]] autorelease]);
+    UCNavigationController *navigation(!popup_ ? [self navigationController] : [[[UCNavigationController alloc] init] autorelease]);
 
     /* XXX: deal with cydia:// pages */
-    BrowserView *browser([[[class_ alloc] initWithBook:book forWidth:width] autorelease]);
+    BrowserController *browser([[[class_ alloc] initWithWidth:width] autorelease]);
 
     if (features != nil && popup_) {
-        [book setDelegate:delegate_];
-        [book setHook:indirect_];
+        [navigation setDelegate:delegate_];
+        [navigation setHook:indirect_];
         [browser setDelegate:delegate_];
 
         [browser loadRequest:request];
 
-        [book setPage:browser];
-        [book_ pushBook:book];
-    } else if (request == nil) {
-        [self setBackButtonTitle:title_];
+        [navigation setViewControllers:[NSArray arrayWithObject:browser]];
+               UIBarButtonItem *closeItem = [[UIBarButtonItem alloc]
+               initWithTitle:UCLocalize("CLOSE")
+                       style:UIBarButtonItemStylePlain
+               target:browser
+               action:@selector(close)
+           ];
+           [[browser navigationItem] setLeftBarButtonItem:closeItem];
+           [closeItem release];
+       
+        [[self navigationController] presentModalViewController:navigation animated:YES];
+    } /*else if (request == nil) {
+        [[self navigationItem] setTitle:title_];
         [browser setDelegate:delegate_];
         [browser retain];
-    } else {
+    }*/ else {
         [self pushPage:browser];
         [browser loadRequest:request];
     }
@@ -917,7 +961,7 @@ static Class $UIWebBrowserView;
         return;
 
     title_ = [title retain];
-    [book_ reloadTitleForPage:self];
+    [[self navigationItem] setTitle:title_];
 }
 
 - (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
@@ -965,7 +1009,7 @@ static Class $UIWebBrowserView;
             special_ = nil;
         }
 
-        [book_ reloadTitleForPage:self];
+        [[self navigationItem] setTitle:title_];
 
         if (Wildcat_) {
             CGRect webrect = [scroller_ bounds];
@@ -992,7 +1036,52 @@ static Class $UIWebBrowserView;
         }
     }
 
-    [self reloadButtons];
+       [self _startLoading];
+}
+
+- (UINavigationButtonStyle) rightButtonStyle {
+    if (style_ == nil) normal:
+        return UINavigationButtonStyleNormal;
+    else if ([style_ isEqualToString:@"Normal"])
+        return UINavigationButtonStyleNormal;
+    else if ([style_ isEqualToString:@"Back"])
+        return UINavigationButtonStyleBack;
+    else if ([style_ isEqualToString:@"Highlighted"])
+        return UINavigationButtonStyleHighlighted;
+    else if ([style_ isEqualToString:@"Destructive"])
+        return UINavigationButtonStyleDestructive;
+    else goto normal;
+}
+
+- (UIBarButtonItem *) customButton {
+    UIBarButtonItem *customItem = [[UIBarButtonItem alloc]
+               initWithTitle:button_
+               style:[self rightButtonStyle]
+               target:self
+               action:@selector(customButtonClicked)
+       ];
+       
+    return [customItem autorelease];
+}
+
+- (void) applyLoadingTitle {
+    [[self navigationItem] setTitle:UCLocalize("LOADING")];
+}
+
+- (void) applyRightButton {
+       if ([self isLoading]) {
+           [[self navigationItem] setRightBarButtonItem:loadingitem_ animated:YES];
+           [[loadingitem_ view] addSubview:indicator_];
+        [self applyLoadingTitle];
+    } else if (button_) {
+        [[self navigationItem] setRightBarButtonItem:[self customButton] animated:YES];
+    } else {
+        [[self navigationItem] setRightBarButtonItem:reloaditem_ animated:YES];
+       }
+}
+
+- (void) _startLoading {
+    [self applyRightButton];
 }
 
 - (void) _finishLoading {
@@ -1003,21 +1092,15 @@ static Class $UIWebBrowserView;
         return;
     if (finish_ != nil)
         [self callFunction:finish_];
-    [self reloadButtons];
+
+       [self applyRightButton];
+       if (![self isLoading]) [[self navigationItem] setTitle:title_];
 }
 
 - (bool) isLoading {
     return [loading_ count] != 0;
 }
 
-- (void) reloadButtons {
-    if ([self isLoading])
-        [indicator_ startAnimation];
-    else
-        [indicator_ stopAnimation];
-    [super reloadButtons];
-}
-
 - (BOOL) webView:(WebView *)sender shouldScrollToPoint:(struct CGPoint)point forFrame:(WebFrame *)frame {
     return [document_ webView:sender shouldScrollToPoint:point forFrame:frame];
 }
@@ -1242,16 +1325,19 @@ static Class $UIWebBrowserView;
     [self _setTileDrawingEnabled:YES];
 }
 
-- (id) initWithBook:(RVBook *)book forWidth:(float)width ofClass:(Class)_class {
-    if ((self = [super initWithBook:book]) != nil) {
+- (id) initWithWidth:(float)width ofClass:(Class)_class {
+    if ((self = [super init]) != nil) {
         class_ = _class;
         loading_ = [[NSMutableSet alloc] initWithCapacity:3];
         popup_ = false;
 
-        struct CGRect bounds = [self bounds];
+        BrowserView *actualView = [[BrowserView alloc] initWithFrame:CGRectZero];
+        [self setView:actualView];
+        
+        struct CGRect bounds = [[self view] bounds];
 
         scroller_ = [[objc_getClass(Wildcat_ ? "UIScrollView" : "UIScroller") alloc] initWithFrame:bounds];
-        [self addSubview:scroller_];
+        [[self view] addSubview:scroller_];
 
         [scroller_ setFixedBackgroundPattern:YES];
         [scroller_ setBackgroundColor:[UIColor pinStripeColor]];
@@ -1367,6 +1453,9 @@ static Class $UIWebBrowserView;
             else
                 [preferences _setLayoutInterval:0];
         }
+        
+        actualView.documentView = document_;
+        [actualView release];
 
         [self setViewportWidth:width];
 
@@ -1395,26 +1484,41 @@ static Class $UIWebBrowserView;
         WebThreadUnlock();
 
         CGSize indsize = [UIProgressIndicator defaultSizeForStyle:UIProgressIndicatorStyleMediumWhite];
-        indicator_ = [[UIProgressIndicator alloc] initWithFrame:CGRectMake(bounds.size.width - 39, 12, indsize.width, indsize.height)];
+        indicator_ = [[UIProgressIndicator alloc] initWithFrame:CGRectMake(15, 5, indsize.width, indsize.height)];
         [indicator_ setStyle:UIProgressIndicatorStyleMediumWhite];
+               [indicator_ startAnimation];
+               
+               reloaditem_ = [[UIBarButtonItem alloc]
+                       initWithTitle:UCLocalize("RELOAD")
+                       style:[self rightButtonStyle]
+                       target:self
+                       action:@selector(reloadButtonClicked)
+               ];
+               
+           loadingitem_ = [[UIBarButtonItem alloc]
+               initWithTitle:@" "
+               style:UIBarButtonItemStylePlain
+               target:self
+               action:@selector(reloadButtonClicked)
+           ];
+               [[loadingitem_ view] addSubview:indicator_];
 
-        [self setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
         [scroller_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
         [indicator_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
-        [document_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
+        [document_ setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
 
-        /*UIWebView *test([[[UIWebView alloc] initWithFrame:[self bounds]] autorelease]);
+        /*UIWebView *test([[[UIWebView alloc] initWithFrame:[[self view] bounds]] autorelease]);
         [test loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.saurik.com/"]]];
-        [self addSubview:test];*/
+        [[self view] addSubview:test];*/
     } return self;
 }
 
-- (id) initWithBook:(RVBook *)book forWidth:(float)width {
-    return [self initWithBook:book forWidth:width ofClass:[self class]];
+- (id) initWithWidth:(float)width {
+    return [self initWithWidth:width ofClass:[self class]];
 }
 
-- (id) initWithBook:(RVBook *)book {
-    return [self initWithBook:book forWidth:0];
+- (id) init {
+    return [self initWithWidth:0];
 }
 
 - (NSString *) stringByEvaluatingJavaScriptFromString:(NSString *)script {
@@ -1443,7 +1547,7 @@ static Class $UIWebBrowserView;
         settings->setJavaScriptCanOpenWindowsAutomatically(true);
     }
 
-    if (UIWindow *window = [self window])
+    if (UIWindow *window = [[self view] window])
         if (UIResponder *responder = [window firstResponder])
             [responder resignFirstResponder];
 
@@ -1457,60 +1561,30 @@ static Class $UIWebBrowserView;
     WebThreadUnlock();
 }
 
-- (void) didCloseBook:(RVBook *)book {
+- (void) didDismissModalViewController {
     if (closer_ != nil)
         [self callFunction:closer_];
 }
 
-- (void) __rightButtonClicked {
+- (void) reloadButtonClicked {
     reloading_ = true;
     [self reloadURL];
 }
 
-- (void) _rightButtonClicked {
+- (void) customButtonClicked {
 #if !AlwaysReload
     if (function_ != nil)
         [self callFunction:function_];
     else
 #endif
-        [self __rightButtonClicked];
-}
-
-- (id) _rightButtonTitle {
-    return UCLocalize("RELOAD");
-}
-
-- (id) rightButtonTitle {
-    return [self isLoading] ? @"" : button_ != nil ? button_ : [self _rightButtonTitle];
-}
-
-- (UINavigationButtonStyle) rightButtonStyle {
-    if (style_ == nil) normal:
-        return UINavigationButtonStyleNormal;
-    else if ([style_ isEqualToString:@"Normal"])
-        return UINavigationButtonStyleNormal;
-    else if ([style_ isEqualToString:@"Back"])
-        return UINavigationButtonStyleBack;
-    else if ([style_ isEqualToString:@"Highlighted"])
-        return UINavigationButtonStyleHighlighted;
-    else if ([style_ isEqualToString:@"Destructive"])
-        return UINavigationButtonStyleDestructive;
-    else goto normal;
-}
-
-- (NSString *) title {
-    return title_ == nil ? UCLocalize("LOADING") : title_;
-}
-
-- (NSString *) backButtonTitle {
-    return UCLocalize("BROWSER");
+               [self reloadButtonClicked];
 }
 
 - (void) setPageActive:(BOOL)active {
     if (!active)
         [indicator_ removeFromSuperview];
     else
-        [[book_ navigationBar] addSubview:indicator_];
+               [[[[self navigationItem] rightBarButtonItem] view] addSubview:indicator_];
 }
 
 - (void) resetViewAnimated:(BOOL)animated {
@@ -1524,4 +1598,4 @@ static Class $UIWebBrowserView;
     return 980;
 }
 
-@end
+@end
\ No newline at end of file