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];
56 return [super createButtonWithContents:contents width:width barStyle:barStyle buttonStyle:style isRight:right];
61 @implementation RVBook
64 [navbar_ setDelegate:nil];
66 [toolbar_ setDelegate:nil];
70 [transition_ release];
76 - (UINavigationBar *) navigationBar {
80 - (void) navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button {
81 _assert([pages_ count] != 0);
82 RVPage *page = [pages_ lastObject];
84 case 0: [page _rightButtonClicked]; break;
85 case 1: [page _leftButtonClicked]; break;
89 - (void) navigationBar:(UINavigationBar *)navbar poppedItem:(UINavigationItem *)item {
90 _assert([pages_ count] != 0);
92 [[pages_ lastObject] setPageActive:NO];
93 [pages_ removeLastObject];
95 [self resetViewAnimated:YES toPage:[pages_ lastObject]];
98 - (id) initWithFrame:(CGRect)frame {
99 if ((self = [super initWithFrame:frame]) != nil) {
100 pages_ = [[NSMutableArray arrayWithCapacity:4] retain];
102 struct CGRect bounds = [self bounds];
103 CGSize navsize = [UINavigationBar defaultSize];
104 CGRect navrect = {{0, 0}, navsize};
106 navbar_ = [[RVNavigationBar alloc] initWithFrame:navrect];
107 [self addSubview:navbar_];
109 [navbar_ setBarStyle:0];
110 [navbar_ setDelegate:self];
112 transition_ = [[UITransitionView alloc] initWithFrame:CGRectMake(
113 bounds.origin.x, bounds.origin.y + navsize.height, bounds.size.width, bounds.size.height - navsize.height
116 [self addSubview:transition_];
120 - (void) setDelegate:(id)delegate {
121 delegate_ = delegate;
124 - (void) setPage:(RVPage *)page {
125 if ([pages_ count] != 0)
126 [[pages_ lastObject] setPageActive:NO];
128 [navbar_ disableAnimation];
130 for (unsigned i(0), pages([pages_ count]); i != pages; ++i)
131 [navbar_ popNavigationItem];
134 [self pushPage:page];
135 [navbar_ enableAnimation];
138 - (void) pushPage:(RVPage *)page {
139 if ([pages_ count] != 0)
140 [[pages_ lastObject] setPageActive:NO];
142 NSString *title = [self getTitleForPage:page];
144 NSString *backButtonTitle = [page backButtonTitle];
145 if (backButtonTitle == nil)
146 backButtonTitle = title;
148 UINavigationItem *navitem = [[[UINavigationItem alloc] initWithTitle:title] autorelease];
149 [navitem setBackButtonTitle:backButtonTitle];
150 [navbar_ pushNavigationItem:navitem];
152 BOOL animated = [pages_ count] == 0 ? NO : YES;
153 [page setFrame:[transition_ bounds]];
154 [transition_ transition:(animated ? 1 : 0) toView:page];
155 [page setPageActive:YES];
157 [pages_ addObject:page];
158 [self reloadButtonsForPage:page];
160 [navbar_ setAccessoryView:[page accessoryView] animate:animated removeOnPop:NO];
163 - (void) pushBook:(RVBook *)book {
164 [delegate_ popUpBook:book];
167 - (void) popPages:(unsigned)pages {
171 [[pages_ lastObject] setPageActive:NO];
174 for (unsigned i(0); i != pages; ++i)
175 [navbar_ popNavigationItem];
178 [self resetViewAnimated:YES toPage:[pages_ lastObject]];
181 - (void) resetViewAnimated:(BOOL)animated {
184 if ([pages_ count] > 1) {
185 [navbar_ disableAnimation];
186 while ([pages_ count] != (animated ? 2 : 1))
187 [navbar_ popNavigationItem];
188 [navbar_ enableAnimation];
190 [navbar_ popNavigationItem];
195 [self resetViewAnimated:animated toPage:[pages_ lastObject]];
198 - (void) resetViewAnimated:(BOOL)animated toPage:(RVPage *)page {
199 [page resetViewAnimated:animated];
200 [page setFrame:[transition_ bounds]];
201 [transition_ transition:(animated ? 2 : 0) toView:page];
202 [page setPageActive:YES];
203 [self reloadButtonsForPage:page];
205 [navbar_ setAccessoryView:[page accessoryView] animate:animated removeOnPop:NO];
208 - (void) setBackButtonTitle:(NSString *)title forPage:(RVPage *)page {
209 if ([pages_ count] == 0 || page != [pages_ lastObject])
211 UINavigationItem *navitem = [navbar_ topItem];
212 [navitem setBackButtonTitle:title];
215 - (void) reloadTitleForPage:(RVPage *)page {
216 if ([pages_ count] == 0 || page != [pages_ lastObject])
218 UINavigationItem *navitem = [navbar_ topItem];
219 NSString *title = [self getTitleForPage:page];
220 [navitem setTitle:title];
223 - (void) _leftButtonTitle:(NSString *&)leftButtonTitle style:(UINavigationButtonStyle &)leftButtonStyle forPage:(RVPage *)page {
224 leftButtonTitle = [page leftButtonTitle];
225 leftButtonStyle = [page leftButtonStyle];
228 - (void) reloadButtonsForPage:(RVPage *)page {
229 if ([pages_ count] == 0 || page != [pages_ lastObject])
232 NSString *leftButtonTitle;
233 UINavigationButtonStyle leftButtonStyle;
234 [self _leftButtonTitle:leftButtonTitle style:leftButtonStyle forPage:page];
236 UINavigationButtonStyle rightButtonStyle = [page rightButtonStyle];
237 //[navbar_ showButtonsWithLeftTitle:leftButtonTitle rightTitle:[page rightButtonTitle] leftBack:(leftButtonTitle == nil)];
240 showLeftButton:leftButtonTitle
241 withStyle:leftButtonStyle
242 rightButton:[page rightButtonTitle]
243 withStyle:rightButtonStyle
247 - (NSString *) getTitleForPage:(RVPage *)page {
251 - (void) reloadData {
252 for (int i(0), e([pages_ count]); i != e; ++i) {
253 RVPage *page([pages_ objectAtIndex:(e - i - 1)]);
258 - (CGRect) pageBounds {
259 return [transition_ bounds];
267 @implementation RVPopUpBook
269 - (void) _leftButtonTitle:(NSString *&)leftButtonTitle style:(UINavigationButtonStyle &)leftButtonStyle forPage:(RVPage *)page {
270 [super _leftButtonTitle:leftButtonTitle style:leftButtonStyle forPage:page];
271 if ((cancel_ = leftButtonTitle == nil && [pages_ count] == 1)) {
272 leftButtonTitle = @"Cancel";
273 leftButtonStyle = UINavigationButtonStyleNormal;
277 - (void) navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button {
278 if (button == 1 && cancel_)
281 [super navigationBar:navbar buttonClicked:button];
285 [self popFromSuperviewAnimated:YES];