3 #import <UIKit/UINavigationBar.h>
4 #import <UIKit/UINavigationItem.h>
6 #import <UIKit/UITransitionView.h>
8 #import <UIKit/UIView-Geometry.h>
9 #import <UIKit/UIView-Hierarchy.h>
11 #import <Foundation/Foundation.h>
12 #import <CoreGraphics/CGGeometry.h>
21 @interface NSObject (UICaboodleRVBook)
22 - (float) widthForButtonContents:(float)width;
25 @implementation NSObject (UICaboodleRVBook)
27 - (float) widthForButtonContents:(float)width {
33 @interface UIImage (UICaboodleRVBook)
34 - (float) widthForButtonContents:(float)width;
37 @implementation UIImage (UICaboodleRVBook)
39 - (float) widthForButtonContents:(float)width {
40 return [self size].width + 8;
45 @interface RVNavigationBar : UINavigationBar {
48 - (id) createButtonWithContents:(id)contents width:(float)width barStyle:(int)barStyle buttonStyle:(int)style isRight:(BOOL)right;
51 @implementation RVNavigationBar
53 - (id) createButtonWithContents:(id)contents width:(float)width barStyle:(int)barStyle buttonStyle:(int)style isRight:(BOOL)right {
54 float adjust = [contents widthForButtonContents:width];
55 NSLog(@"cc:%@:%g:%g", contents, width, adjust);
57 return [super createButtonWithContents:contents width:width barStyle:barStyle buttonStyle:style isRight:right];
62 @implementation RVBook
65 [navbar_ setDelegate:nil];
69 [transition_ release];
73 - (UINavigationBar *) navigationBar {
77 - (void) navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button {
78 _assert([pages_ count] != 0);
79 RVPage *page = [pages_ lastObject];
81 case 0: [page _rightButtonClicked]; break;
82 case 1: [page _leftButtonClicked]; break;
86 - (void) navigationBar:(UINavigationBar *)navbar poppedItem:(UINavigationItem *)item {
87 _assert([pages_ count] != 0);
89 [[pages_ lastObject] setPageActive:NO];
90 [pages_ removeLastObject];
92 [self resetViewAnimated:YES toPage:[pages_ lastObject]];
95 - (id) initWithFrame:(CGRect)frame {
96 if ((self = [super initWithFrame:frame]) != nil) {
97 pages_ = [[NSMutableArray arrayWithCapacity:4] retain];
99 struct CGRect bounds = [self bounds];
100 CGSize navsize = [UINavigationBar defaultSize];
101 CGRect navrect = {{0, 0}, navsize};
103 navbar_ = [[RVNavigationBar alloc] initWithFrame:navrect];
104 [self addSubview:navbar_];
106 [navbar_ setBarStyle:0];
107 [navbar_ setDelegate:self];
109 transition_ = [[UITransitionView alloc] initWithFrame:CGRectMake(
110 bounds.origin.x, bounds.origin.y + navsize.height, bounds.size.width, bounds.size.height - navsize.height
113 [self addSubview:transition_];
117 - (void) setDelegate:(id)delegate {
118 delegate_ = delegate;
121 - (void) setPage:(RVPage *)page {
122 if ([pages_ count] != 0)
123 [[pages_ lastObject] setPageActive:NO];
125 [navbar_ disableAnimation];
127 for (unsigned i(0), pages([pages_ count]); i != pages; ++i)
128 [navbar_ popNavigationItem];
131 [self pushPage:page];
132 [navbar_ enableAnimation];
135 - (void) pushPage:(RVPage *)page {
136 if ([pages_ count] != 0)
137 [[pages_ lastObject] setPageActive:NO];
139 NSString *title = [self getTitleForPage:page];
141 NSString *backButtonTitle = [page backButtonTitle];
142 if (backButtonTitle == nil)
143 backButtonTitle = title;
145 UINavigationItem *navitem = [[[UINavigationItem alloc] initWithTitle:title] autorelease];
146 [navitem setBackButtonTitle:backButtonTitle];
147 [navbar_ pushNavigationItem:navitem];
149 BOOL animated = [pages_ count] == 0 ? NO : YES;
150 [page setFrame:[transition_ bounds]];
151 [transition_ transition:(animated ? 1 : 0) toView:page];
152 [page setPageActive:YES];
154 [pages_ addObject:page];
155 [self reloadButtonsForPage:page];
157 [navbar_ setAccessoryView:[page accessoryView] animate:animated removeOnPop:NO];
160 - (void) popPages:(unsigned)pages {
164 [[pages_ lastObject] setPageActive:NO];
167 for (unsigned i(0); i != pages; ++i)
168 [navbar_ popNavigationItem];
171 [self resetViewAnimated:YES toPage:[pages_ lastObject]];
174 - (void) resetViewAnimated:(BOOL)animated {
177 if ([pages_ count] > 1) {
178 [navbar_ disableAnimation];
179 while ([pages_ count] != (animated ? 2 : 1))
180 [navbar_ popNavigationItem];
181 [navbar_ enableAnimation];
183 [navbar_ popNavigationItem];
188 [self resetViewAnimated:animated toPage:[pages_ lastObject]];
191 - (void) resetViewAnimated:(BOOL)animated toPage:(RVPage *)page {
192 [page resetViewAnimated:animated];
193 [page setFrame:[transition_ bounds]];
194 [transition_ transition:(animated ? 2 : 0) toView:page];
195 [page setPageActive:YES];
196 [self reloadButtonsForPage:page];
198 [navbar_ setAccessoryView:[page accessoryView] animate:animated removeOnPop:NO];
201 - (void) setBackButtonTitle:(NSString *)title forPage:(RVPage *)page {
202 if ([pages_ count] == 0 || page != [pages_ lastObject])
204 UINavigationItem *navitem = [navbar_ topItem];
205 [navitem setBackButtonTitle:title];
208 - (void) reloadTitleForPage:(RVPage *)page {
209 if ([pages_ count] == 0 || page != [pages_ lastObject])
211 UINavigationItem *navitem = [navbar_ topItem];
212 NSString *title = [self getTitleForPage:page];
213 [navitem setTitle:title];
216 - (void) reloadButtonsForPage:(RVPage *)page {
217 if ([pages_ count] == 0 || page != [pages_ lastObject])
219 NSString *leftButtonTitle([page leftButtonTitle]);
220 UINavigationButtonStyle leftButtonStyle = [page leftButtonStyle];
221 UINavigationButtonStyle rightButtonStyle = [page rightButtonStyle];
222 //[navbar_ showButtonsWithLeftTitle:leftButtonTitle rightTitle:[page rightButtonTitle] leftBack:(leftButtonTitle == nil)];
225 showLeftButton:leftButtonTitle
226 withStyle:leftButtonStyle
227 rightButton:[page rightButtonTitle]
228 withStyle:rightButtonStyle
232 - (NSString *) getTitleForPage:(RVPage *)page {
236 - (void) reloadData {
237 for (int i(0), e([pages_ count]); i != e; ++i) {
238 RVPage *page([pages_ objectAtIndex:(e - i - 1)]);
243 - (CGRect) pageBounds {
244 return [transition_ bounds];