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}, navsize};
109 navbar_ = [[RVNavigationBar alloc] initWithFrame:navrect];
110 [self addSubview:navbar_];
112 [navbar_ setBarStyle:0];
113 [navbar_ setDelegate:self];
115 transition_ = [[UITransitionView alloc] initWithFrame:CGRectMake(
116 bounds.origin.x, bounds.origin.y + navsize.height, bounds.size.width, bounds.size.height - navsize.height
119 [self addSubview:transition_];
123 - (void) setDelegate:(id)delegate {
124 delegate_ = delegate;
127 - (void) setHook:(id)hook {
131 - (void) setPage:(RVPage *)page {
132 if ([pages_ count] != 0)
133 [[pages_ lastObject] setPageActive:NO];
135 [navbar_ disableAnimation];
137 for (unsigned i(0), pages([pages_ count]); i != pages; ++i)
138 [navbar_ popNavigationItem];
141 [self pushPage:page];
142 [navbar_ enableAnimation];
145 - (void) swapPage:(RVPage *)page {
146 if ([pages_ count] == 0)
147 return [self pushPage:page];
149 [[pages_ lastObject] setPageActive:NO];
151 [navbar_ disableAnimation];
153 [navbar_ popNavigationItem];
156 [self pushPage:page animated:NO];
157 [navbar_ enableAnimation];
160 - (void) pushPage:(RVPage *)page animated:(BOOL)animated {
161 NSString *title = [self getTitleForPage:page];
163 NSString *backButtonTitle = [page backButtonTitle];
164 if (backButtonTitle == nil)
165 backButtonTitle = title;
167 UINavigationItem *navitem = [[[UINavigationItem alloc] initWithTitle:title] autorelease];
168 [navitem setBackButtonTitle:backButtonTitle];
169 [navbar_ pushNavigationItem:navitem];
171 [page setFrame:[transition_ bounds]];
172 [transition_ transition:(animated ? 1 : 0) toView:page];
173 [page setPageActive:YES];
175 [pages_ addObject:page];
176 [self reloadButtonsForPage:page];
178 if ([navbar_ respondsToSelector:@selector(setAccessoryView:animate:removeOnPop:)])
179 [navbar_ setAccessoryView:[page accessoryView] animate:animated removeOnPop:NO];
181 [navbar_ setAccessoryView:[page accessoryView] animate:animated];
184 - (void) pushPage:(RVPage *)page {
185 if ([pages_ count] != 0)
186 [[pages_ lastObject] setPageActive:NO];
187 [self pushPage:page animated:([pages_ count] == 0 ? NO : YES)];
190 - (void) pushBook:(RVBook *)book {
191 [delegate_ popUpBook:book];
194 - (void) popPages:(unsigned)pages {
198 [[pages_ lastObject] setPageActive:NO];
201 for (unsigned i(0); i != pages; ++i)
202 [navbar_ popNavigationItem];
205 [self resetViewAnimated:YES toPage:[pages_ lastObject]];
208 - (void) resetViewAnimated:(BOOL)animated {
211 if ([pages_ count] > 1) {
212 [navbar_ disableAnimation];
213 while ([pages_ count] != (animated ? 2 : 1))
214 [navbar_ popNavigationItem];
215 [navbar_ enableAnimation];
217 [navbar_ popNavigationItem];
222 [self resetViewAnimated:animated toPage:[pages_ lastObject]];
225 - (void) resetViewAnimated:(BOOL)animated toPage:(RVPage *)page {
226 [page resetViewAnimated:animated];
227 [page setFrame:[transition_ bounds]];
228 [transition_ transition:(animated ? 2 : 0) toView:page];
229 [page setPageActive:YES];
230 [self reloadButtonsForPage:page];
232 if ([navbar_ respondsToSelector:@selector(setAccessoryView:animate:removeOnPop:)])
233 [navbar_ setAccessoryView:[page accessoryView] animate:animated removeOnPop:NO];
235 [navbar_ setAccessoryView:[page accessoryView] animate:animated];
238 - (void) setBackButtonTitle:(NSString *)title forPage:(RVPage *)page {
239 if ([pages_ count] == 0 || page != [pages_ lastObject])
241 UINavigationItem *navitem = [navbar_ topItem];
242 [navitem setBackButtonTitle:title];
245 - (void) reloadTitleForPage:(RVPage *)page {
246 if ([pages_ count] == 0 || page != [pages_ lastObject])
248 UINavigationItem *navitem = [navbar_ topItem];
249 NSString *title = [self getTitleForPage:page];
250 [navitem setTitle:title];
253 - (void) _leftButtonTitle:(NSString *&)leftButtonTitle style:(UINavigationButtonStyle &)leftButtonStyle forPage:(RVPage *)page {
254 leftButtonTitle = [page leftButtonTitle];
255 leftButtonStyle = [page leftButtonStyle];
258 - (void) reloadButtonsForPage:(RVPage *)page {
259 if ([pages_ count] == 0 || page != [pages_ lastObject])
262 NSString *leftButtonTitle;
263 UINavigationButtonStyle leftButtonStyle;
264 [self _leftButtonTitle:leftButtonTitle style:leftButtonStyle forPage:page];
266 UINavigationButtonStyle rightButtonStyle = [page rightButtonStyle];
267 //[navbar_ showButtonsWithLeftTitle:leftButtonTitle rightTitle:[page rightButtonTitle] leftBack:(leftButtonTitle == nil)];
270 showLeftButton:leftButtonTitle
271 withStyle:leftButtonStyle
272 rightButton:[page rightButtonTitle]
273 withStyle:rightButtonStyle
277 - (NSString *) getTitleForPage:(RVPage *)page {
281 - (void) reloadData {
282 size_t count([pages_ count]);
283 for (size_t i(0); i != count; ++i) {
284 RVPage *page([pages_ objectAtIndex:(count - i - 1)]);
289 RVPage *page([pages_ lastObject]);
290 [self reloadButtonsForPage:page];
294 - (void) reloadButtons {
295 size_t count([pages_ count]);
297 RVPage *page([pages_ lastObject]);
298 [self reloadButtonsForPage:page];
302 - (CGRect) pageBounds {
303 return [transition_ bounds];
308 [hook_ didCloseBook:self];
313 @implementation RVPopUpBook
315 - (void) _leftButtonTitle:(NSString *&)leftButtonTitle style:(UINavigationButtonStyle &)leftButtonStyle forPage:(RVPage *)page {
316 [super _leftButtonTitle:leftButtonTitle style:leftButtonStyle forPage:page];
317 if ((cancel_ = leftButtonTitle == nil && [pages_ count] == 1)) {
318 leftButtonTitle = [[NSBundle mainBundle] localizedStringForKey:@"CANCEL" value:nil table:nil];
319 leftButtonStyle = UINavigationButtonStyleNormal;
323 - (void) navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button {
324 if (button == 1 && cancel_)
327 [super navigationBar:navbar buttonClicked:button];
331 [self popFromSuperviewAnimated:YES];