]> git.saurik.com Git - cydia.git/blobdiff - UICaboodle/RVBook.mm
20 minutes of typing.
[cydia.git] / UICaboodle / RVBook.mm
index 708e66f381438e662aac38f2c14ac5e380130c76..433de39dedd55dcd4f476d74483d977c71021323 100644 (file)
 
 #import "RVPage.h"
 
+@interface NSObject (UICaboodleRVBook)
+- (float) widthForButtonContents:(float)width;
+@end
+
+@implementation NSObject (UICaboodleRVBook)
+
+- (float) widthForButtonContents:(float)width {
+    return width;
+}
+
+@end
+
+@interface UIImage (UICaboodleRVBook)
+- (float) widthForButtonContents:(float)width;
+@end
+
+@implementation UIImage (UICaboodleRVBook)
+
+- (float) widthForButtonContents:(float)width {
+    return [self size].width + 8;
+}
+
+@end
+
+@interface RVNavigationBar : UINavigationBar {
+}
+
+- (id) createButtonWithContents:(id)contents width:(float)width barStyle:(int)barStyle buttonStyle:(int)style isRight:(BOOL)right;
+@end
+
+@implementation RVNavigationBar
+
+- (id) createButtonWithContents:(id)contents width:(float)width barStyle:(int)barStyle buttonStyle:(int)style isRight:(BOOL)right {
+    float adjust = [contents widthForButtonContents:width];
+    width = adjust;
+    return [super createButtonWithContents:contents width:width barStyle:barStyle buttonStyle:style isRight:right];
+}
+
+@end
+
 @implementation RVBook
 
 - (void) dealloc {
     [navbar_ setDelegate:nil];
+    if (toolbar_ != nil)
+        [toolbar_ setDelegate:nil];
 
     [pages_ release];
     [navbar_ release];
     [transition_ release];
+    if (toolbar_ != nil)
+        [toolbar_ release];
     [super dealloc];
 }
 
         CGSize navsize = [UINavigationBar defaultSize];
         CGRect navrect = {{0, 0}, navsize};
 
-        navbar_ = [[UINavigationBar alloc] initWithFrame:navrect];
+        navbar_ = [[RVNavigationBar alloc] initWithFrame:navrect];
         [self addSubview:navbar_];
 
         [navbar_ setBarStyle:0];
     delegate_ = delegate;
 }
 
+- (void) setHook:(id)hook {
+    hook_ = hook;
+}
+
 - (void) setPage:(RVPage *)page {
     if ([pages_ count] != 0)
         [[pages_ lastObject] setPageActive:NO];
     [navbar_ enableAnimation];
 }
 
-- (void) pushPage:(RVPage *)page {
-    if ([pages_ count] != 0)
-        [[pages_ lastObject] setPageActive:NO];
+- (void) swapPage:(RVPage *)page {
+    if ([pages_ count] == 0)
+        return [self pushPage:page];
+
+    [[pages_ lastObject] setPageActive:NO];
+
+    [navbar_ disableAnimation];
+    resetting_ = true;
+    [navbar_ popNavigationItem];
+    resetting_ = false;
 
+    [self pushPage:page animated:NO];
+    [navbar_ enableAnimation];
+}
+
+- (void) pushPage:(RVPage *)page animated:(BOOL)animated {
     NSString *title = [self getTitleForPage:page];
 
     NSString *backButtonTitle = [page backButtonTitle];
     [navitem setBackButtonTitle:backButtonTitle];
     [navbar_ pushNavigationItem:navitem];
 
-    BOOL animated = [pages_ count] == 0 ? NO : YES;
     [page setFrame:[transition_ bounds]];
     [transition_ transition:(animated ? 1 : 0) toView:page];
     [page setPageActive:YES];
     [pages_ addObject:page];
     [self reloadButtonsForPage:page];
 
-    [navbar_ setAccessoryView:[page accessoryView] animate:animated removeOnPop:NO];
+    if ([navbar_ respondsToSelector:@selector(setAccessoryView:animate:removeOnPop:)])
+        [navbar_ setAccessoryView:[page accessoryView] animate:animated removeOnPop:NO];
+    else
+        [navbar_ setAccessoryView:[page accessoryView] animate:animated];
+}
+
+- (void) pushPage:(RVPage *)page {
+    if ([pages_ count] != 0)
+        [[pages_ lastObject] setPageActive:NO];
+    [self pushPage:page animated:([pages_ count] == 0 ? NO : YES)];
+}
+
+- (void) pushBook:(RVBook *)book {
+    [delegate_ popUpBook:book];
 }
 
 - (void) popPages:(unsigned)pages {
     [page setPageActive:YES];
     [self reloadButtonsForPage:page];
 
-    [navbar_ setAccessoryView:[page accessoryView] animate:animated removeOnPop:NO];
+    if ([navbar_ respondsToSelector:@selector(setAccessoryView:animate:removeOnPop:)])
+        [navbar_ setAccessoryView:[page accessoryView] animate:animated removeOnPop:NO];
+    else
+        [navbar_ setAccessoryView:[page accessoryView] animate:animated];
 }
 
 - (void) setBackButtonTitle:(NSString *)title forPage:(RVPage *)page {
     [navitem setTitle:title];
 }
 
+- (void) _leftButtonTitle:(NSString *&)leftButtonTitle style:(UINavigationButtonStyle &)leftButtonStyle forPage:(RVPage *)page {
+    leftButtonTitle = [page leftButtonTitle];
+    leftButtonStyle = [page leftButtonStyle];
+}
+
 - (void) reloadButtonsForPage:(RVPage *)page {
     if ([pages_ count] == 0 || page != [pages_ lastObject])
         return;
-    NSString *leftButtonTitle([page leftButtonTitle]);
-    UINavigationButtonStyle leftButtonStyle = [page leftButtonStyle];
+
+    NSString *leftButtonTitle;
+    UINavigationButtonStyle leftButtonStyle;
+    [self _leftButtonTitle:leftButtonTitle style:leftButtonStyle forPage:page];
+
     UINavigationButtonStyle rightButtonStyle = [page rightButtonStyle];
     //[navbar_ showButtonsWithLeftTitle:leftButtonTitle rightTitle:[page rightButtonTitle] leftBack:(leftButtonTitle == nil)];
 
 }
 
 - (void) reloadData {
-    for (int i(0), e([pages_ count]); i != e; ++i) {
-        RVPage *page([pages_ objectAtIndex:(e - i - 1)]);
+    size_t count([pages_ count]);
+    for (size_t i(0); i != count; ++i) {
+        RVPage *page([pages_ objectAtIndex:(count - i - 1)]);
         [page reloadData];
     }
+
+    if (count != 0) {
+        RVPage *page([pages_ lastObject]);
+        [self reloadButtonsForPage:page];
+    }
+}
+
+- (void) reloadButtons {
+    size_t count([pages_ count]);
+    if (count != 0) {
+        RVPage *page([pages_ lastObject]);
+        [self reloadButtonsForPage:page];
+    }
 }
 
 - (CGRect) pageBounds {
     return [transition_ bounds];
 }
 
+- (void) close {
+    if (hook_ != nil)
+        [hook_ didCloseBook:self];
+}
+
+@end
+
+@implementation RVPopUpBook
+
+- (void) _leftButtonTitle:(NSString *&)leftButtonTitle style:(UINavigationButtonStyle &)leftButtonStyle forPage:(RVPage *)page {
+    [super _leftButtonTitle:leftButtonTitle style:leftButtonStyle forPage:page];
+    if ((cancel_ = leftButtonTitle == nil && [pages_ count] == 1)) {
+        leftButtonTitle = [[NSBundle mainBundle] localizedStringForKey:@"CANCEL" value:nil table:nil];
+        leftButtonStyle = UINavigationButtonStyleNormal;
+    }
+}
+
+- (void) navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button {
+    if (button == 1 && cancel_)
+        [self close];
+    else
+        [super navigationBar:navbar buttonClicked:button];
+}
+
+- (void) close {
+    [self popFromSuperviewAnimated:YES];
+    [super close];
+}
+
 @end