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];
68 for (RVPage *page in pages_)
73 [transition_ release];
79 - (UINavigationBar *) navigationBar {
83 - (void) navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button {
84 _assert([pages_ count] != 0);
85 RVPage *page = [pages_ lastObject];
87 case 0: [page _rightButtonClicked]; break;
88 case 1: [page _leftButtonClicked]; break;
92 - (void) navigationBar:(UINavigationBar *)navbar poppedItem:(UINavigationItem *)item {
93 _assert([pages_ count] != 0);
95 [[pages_ lastObject] setPageActive:NO];
96 [pages_ removeLastObject];
98 [self resetViewAnimated:YES toPage:[pages_ lastObject]];
101 - (id) initWithFrame:(CGRect)frame {
102 if ((self = [super initWithFrame:frame]) != nil) {
103 pages_ = [[NSMutableArray arrayWithCapacity:4] retain];
105 struct CGRect bounds = [self bounds];
106 CGSize navsize = [UINavigationBar defaultSize];
107 CGRect navrect = {{0, 0}, {bounds.size.width, navsize.height}};
109 navbar_ = [[RVNavigationBar alloc] initWithFrame:navrect];
110 [navbar_ setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
111 [self addSubview:navbar_];
113 [navbar_ setBarStyle:0];
114 [navbar_ setDelegate:self];
116 transition_ = [[UITransitionView alloc] initWithFrame:CGRectMake(
117 bounds.origin.x, bounds.origin.y + navsize.height, bounds.size.width, bounds.size.height - navsize.height
120 [transition_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
122 [self addSubview:transition_];
126 - (void) setDelegate:(id)delegate {
127 delegate_ = delegate;
130 - (void) setHook:(id)hook {
134 - (void) setPage:(RVPage *)page {
135 if ([pages_ count] != 0)
136 [[pages_ lastObject] setPageActive:NO];
138 [navbar_ disableAnimation];
140 for (unsigned i(0), pages([pages_ count]); i != pages; ++i)
141 [navbar_ popNavigationItem];
144 [self pushPage:page];
145 [navbar_ enableAnimation];
148 - (void) swapPage:(RVPage *)page {
149 if ([pages_ count] == 0)
150 return [self pushPage:page];
152 [[pages_ lastObject] setPageActive:NO];
154 [navbar_ disableAnimation];
156 [navbar_ popNavigationItem];
159 [self pushPage:page animated:NO];
160 [navbar_ enableAnimation];
163 - (void) pushPage:(RVPage *)page animated:(BOOL)animated {
164 NSString *title = [self getTitleForPage:page];
166 NSString *backButtonTitle = [page backButtonTitle];
167 if (backButtonTitle == nil)
168 backButtonTitle = title;
170 UINavigationItem *navitem = [[[UINavigationItem alloc] initWithTitle:title] autorelease];
171 [navitem setBackButtonTitle:backButtonTitle];
172 [navbar_ pushNavigationItem:navitem];
174 [page setFrame:[transition_ bounds]];
175 [transition_ transition:(animated ? 1 : 0) toView:page];
176 [page setPageActive:YES];
178 [pages_ addObject:page];
179 [self reloadButtonsForPage:page];
181 if ([navbar_ respondsToSelector:@selector(setAccessoryView:animate:removeOnPop:)])
182 [navbar_ setAccessoryView:[page accessoryView] animate:animated removeOnPop:NO];
184 [navbar_ setAccessoryView:[page accessoryView] animate:animated];
187 - (void) pushPage:(RVPage *)page {
188 if ([pages_ count] != 0)
189 [[pages_ lastObject] setPageActive:NO];
190 [self pushPage:page animated:([pages_ count] == 0 ? NO : YES)];
193 - (void) pushBook:(RVBook *)book {
194 [delegate_ popUpBook:book];
197 - (void) popPages:(unsigned)pages {
201 [[pages_ lastObject] setPageActive:NO];
204 for (unsigned i(0); i != pages; ++i)
205 [navbar_ popNavigationItem];
208 [self resetViewAnimated:YES toPage:[pages_ lastObject]];
211 - (void) resetViewAnimated:(BOOL)animated {
214 if ([pages_ count] > 1) {
215 [navbar_ disableAnimation];
216 while ([pages_ count] != (animated ? 2 : 1))
217 [navbar_ popNavigationItem];
218 [navbar_ enableAnimation];
220 [navbar_ popNavigationItem];
225 [self resetViewAnimated:animated toPage:[pages_ lastObject]];
228 - (void) resetViewAnimated:(BOOL)animated toPage:(RVPage *)page {
229 [page resetViewAnimated:animated];
230 [page setFrame:[transition_ bounds]];
231 [transition_ transition:(animated ? 2 : 0) toView:page];
232 [page setPageActive:YES];
233 [self reloadButtonsForPage:page];
235 if ([navbar_ respondsToSelector:@selector(setAccessoryView:animate:removeOnPop:)])
236 [navbar_ setAccessoryView:[page accessoryView] animate:animated removeOnPop:NO];
238 [navbar_ setAccessoryView:[page accessoryView] animate:animated];
241 - (void) setBackButtonTitle:(NSString *)title forPage:(RVPage *)page {
242 if ([pages_ count] == 0 || page != [pages_ lastObject])
244 UINavigationItem *navitem = [navbar_ topItem];
245 [navitem setBackButtonTitle:title];
248 - (void) reloadTitleForPage:(RVPage *)page {
249 if ([pages_ count] == 0 || page != [pages_ lastObject])
251 UINavigationItem *navitem = [navbar_ topItem];
252 NSString *title = [self getTitleForPage:page];
253 [navitem setTitle:title];
256 - (void) _leftButtonTitle:(NSString *&)leftButtonTitle style:(UINavigationButtonStyle &)leftButtonStyle forPage:(RVPage *)page {
257 leftButtonTitle = [page leftButtonTitle];
258 leftButtonStyle = [page leftButtonStyle];
261 - (void) reloadButtonsForPage:(RVPage *)page {
262 if ([pages_ count] == 0 || page != [pages_ lastObject])
265 NSString *leftButtonTitle;
266 UINavigationButtonStyle leftButtonStyle;
267 [self _leftButtonTitle:leftButtonTitle style:leftButtonStyle forPage:page];
269 UINavigationButtonStyle rightButtonStyle = [page rightButtonStyle];
270 //[navbar_ showButtonsWithLeftTitle:leftButtonTitle rightTitle:[page rightButtonTitle] leftBack:(leftButtonTitle == nil)];
273 showLeftButton:leftButtonTitle
274 withStyle:leftButtonStyle
275 rightButton:[page rightButtonTitle]
276 withStyle:rightButtonStyle
280 - (NSString *) getTitleForPage:(RVPage *)page {
284 - (void) reloadData {
285 size_t count([pages_ count]);
286 for (size_t i(0); i != count; ++i) {
287 RVPage *page([pages_ objectAtIndex:(count - i - 1)]);
292 RVPage *page([pages_ lastObject]);
293 [self reloadButtonsForPage:page];
297 - (void) reloadButtons {
298 size_t count([pages_ count]);
300 RVPage *page([pages_ lastObject]);
301 [self reloadButtonsForPage:page];
305 - (CGRect) pageBounds {
306 return [transition_ bounds];
311 [hook_ didCloseBook:self];
316 @implementation RVPopUpBook
318 - (void) _leftButtonTitle:(NSString *&)leftButtonTitle style:(UINavigationButtonStyle &)leftButtonStyle forPage:(RVPage *)page {
319 [super _leftButtonTitle:leftButtonTitle style:leftButtonStyle forPage:page];
320 if ((cancel_ = leftButtonTitle == nil && [pages_ count] == 1)) {
321 leftButtonTitle = [[NSBundle mainBundle] localizedStringForKey:@"CANCEL" value:nil table:nil];
322 leftButtonStyle = UINavigationButtonStyleNormal;
326 - (void) navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button {
327 if (button == 1 && cancel_)
330 [super navigationBar:navbar buttonClicked:button];
334 [self popFromSuperviewAnimated:YES];