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) setHook:(id)hook {
128 - (void) setPage:(RVPage *)page {
129 if ([pages_ count] != 0)
130 [[pages_ lastObject] setPageActive:NO];
132 [navbar_ disableAnimation];
134 for (unsigned i(0), pages([pages_ count]); i != pages; ++i)
135 [navbar_ popNavigationItem];
138 [self pushPage:page];
139 [navbar_ enableAnimation];
142 - (void) swapPage:(RVPage *)page {
143 if ([pages_ count] == 0)
144 return [self pushPage:page];
146 [[pages_ lastObject] setPageActive:NO];
148 [navbar_ disableAnimation];
150 [navbar_ popNavigationItem];
153 [self pushPage:page animated:NO];
154 [navbar_ enableAnimation];
157 - (void) pushPage:(RVPage *)page animated:(BOOL)animated {
158 NSString *title = [self getTitleForPage:page];
160 NSString *backButtonTitle = [page backButtonTitle];
161 if (backButtonTitle == nil)
162 backButtonTitle = title;
164 UINavigationItem *navitem = [[[UINavigationItem alloc] initWithTitle:title] autorelease];
165 [navitem setBackButtonTitle:backButtonTitle];
166 [navbar_ pushNavigationItem:navitem];
168 [page setFrame:[transition_ bounds]];
169 [transition_ transition:(animated ? 1 : 0) toView:page];
170 [page setPageActive:YES];
172 [pages_ addObject:page];
173 [self reloadButtonsForPage:page];
175 [navbar_ setAccessoryView:[page accessoryView] animate:animated removeOnPop:NO];
178 - (void) pushPage:(RVPage *)page {
179 if ([pages_ count] != 0)
180 [[pages_ lastObject] setPageActive:NO];
181 [self pushPage:page animated:([pages_ count] == 0 ? NO : YES)];
184 - (void) pushBook:(RVBook *)book {
185 [delegate_ popUpBook:book];
188 - (void) popPages:(unsigned)pages {
192 [[pages_ lastObject] setPageActive:NO];
195 for (unsigned i(0); i != pages; ++i)
196 [navbar_ popNavigationItem];
199 [self resetViewAnimated:YES toPage:[pages_ lastObject]];
202 - (void) resetViewAnimated:(BOOL)animated {
205 if ([pages_ count] > 1) {
206 [navbar_ disableAnimation];
207 while ([pages_ count] != (animated ? 2 : 1))
208 [navbar_ popNavigationItem];
209 [navbar_ enableAnimation];
211 [navbar_ popNavigationItem];
216 [self resetViewAnimated:animated toPage:[pages_ lastObject]];
219 - (void) resetViewAnimated:(BOOL)animated toPage:(RVPage *)page {
220 [page resetViewAnimated:animated];
221 [page setFrame:[transition_ bounds]];
222 [transition_ transition:(animated ? 2 : 0) toView:page];
223 [page setPageActive:YES];
224 [self reloadButtonsForPage:page];
226 [navbar_ setAccessoryView:[page accessoryView] animate:animated removeOnPop:NO];
229 - (void) setBackButtonTitle:(NSString *)title forPage:(RVPage *)page {
230 if ([pages_ count] == 0 || page != [pages_ lastObject])
232 UINavigationItem *navitem = [navbar_ topItem];
233 [navitem setBackButtonTitle:title];
236 - (void) reloadTitleForPage:(RVPage *)page {
237 if ([pages_ count] == 0 || page != [pages_ lastObject])
239 UINavigationItem *navitem = [navbar_ topItem];
240 NSString *title = [self getTitleForPage:page];
241 [navitem setTitle:title];
244 - (void) _leftButtonTitle:(NSString *&)leftButtonTitle style:(UINavigationButtonStyle &)leftButtonStyle forPage:(RVPage *)page {
245 leftButtonTitle = [page leftButtonTitle];
246 leftButtonStyle = [page leftButtonStyle];
249 - (void) reloadButtonsForPage:(RVPage *)page {
250 if ([pages_ count] == 0 || page != [pages_ lastObject])
253 NSString *leftButtonTitle;
254 UINavigationButtonStyle leftButtonStyle;
255 [self _leftButtonTitle:leftButtonTitle style:leftButtonStyle forPage:page];
257 UINavigationButtonStyle rightButtonStyle = [page rightButtonStyle];
258 //[navbar_ showButtonsWithLeftTitle:leftButtonTitle rightTitle:[page rightButtonTitle] leftBack:(leftButtonTitle == nil)];
261 showLeftButton:leftButtonTitle
262 withStyle:leftButtonStyle
263 rightButton:[page rightButtonTitle]
264 withStyle:rightButtonStyle
268 - (NSString *) getTitleForPage:(RVPage *)page {
272 - (void) reloadData {
273 for (int i(0), e([pages_ count]); i != e; ++i) {
274 RVPage *page([pages_ objectAtIndex:(e - i - 1)]);
279 - (CGRect) pageBounds {
280 return [transition_ bounds];
285 [hook_ didCloseBook:self];
290 @implementation RVPopUpBook
292 - (void) _leftButtonTitle:(NSString *&)leftButtonTitle style:(UINavigationButtonStyle &)leftButtonStyle forPage:(RVPage *)page {
293 [super _leftButtonTitle:leftButtonTitle style:leftButtonStyle forPage:page];
294 if ((cancel_ = leftButtonTitle == nil && [pages_ count] == 1)) {
295 leftButtonTitle = @"Cancel";
296 leftButtonStyle = UINavigationButtonStyleNormal;
300 - (void) navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button {
301 if (button == 1 && cancel_)
304 [super navigationBar:navbar buttonClicked:button];
308 [self popFromSuperviewAnimated:YES];