]>
Commit | Line | Data |
---|---|---|
77f175ac JF |
1 | #import "RVBook.h" |
2 | ||
3 | #import <UIKit/UINavigationBar.h> | |
4 | #import <UIKit/UINavigationItem.h> | |
5 | ||
6 | #import <UIKit/UITransitionView.h> | |
7 | ||
8 | #import <UIKit/UIView-Geometry.h> | |
9 | #import <UIKit/UIView-Hierarchy.h> | |
10 | ||
d210b85d JF |
11 | #import <Foundation/Foundation.h> |
12 | #import <CoreGraphics/CGGeometry.h> | |
13 | ||
14 | #include <cstdio> | |
15 | #include <cstdlib> | |
16 | ||
17 | #include <errno.h> | |
18 | ||
77f175ac JF |
19 | #import "RVPage.h" |
20 | ||
bde2d79b JF |
21 | @interface NSObject (UICaboodleRVBook) |
22 | - (float) widthForButtonContents:(float)width; | |
23 | @end | |
24 | ||
25 | @implementation NSObject (UICaboodleRVBook) | |
26 | ||
27 | - (float) widthForButtonContents:(float)width { | |
28 | return width; | |
29 | } | |
30 | ||
31 | @end | |
32 | ||
33 | @interface UIImage (UICaboodleRVBook) | |
34 | - (float) widthForButtonContents:(float)width; | |
35 | @end | |
36 | ||
37 | @implementation UIImage (UICaboodleRVBook) | |
38 | ||
39 | - (float) widthForButtonContents:(float)width { | |
40 | return [self size].width + 8; | |
41 | } | |
42 | ||
43 | @end | |
44 | ||
45 | @interface RVNavigationBar : UINavigationBar { | |
46 | } | |
47 | ||
48 | - (id) createButtonWithContents:(id)contents width:(float)width barStyle:(int)barStyle buttonStyle:(int)style isRight:(BOOL)right; | |
49 | @end | |
50 | ||
51 | @implementation RVNavigationBar | |
52 | ||
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); | |
56 | width = adjust; | |
57 | return [super createButtonWithContents:contents width:width barStyle:barStyle buttonStyle:style isRight:right]; | |
58 | } | |
59 | ||
60 | @end | |
61 | ||
77f175ac JF |
62 | @implementation RVBook |
63 | ||
64 | - (void) dealloc { | |
65 | [navbar_ setDelegate:nil]; | |
66 | ||
67 | [pages_ release]; | |
68 | [navbar_ release]; | |
69 | [transition_ release]; | |
70 | [super dealloc]; | |
71 | } | |
72 | ||
20b8724d JF |
73 | - (UINavigationBar *) navigationBar { |
74 | return navbar_; | |
75 | } | |
76 | ||
77f175ac JF |
77 | - (void) navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button { |
78 | _assert([pages_ count] != 0); | |
79 | RVPage *page = [pages_ lastObject]; | |
80 | switch (button) { | |
81 | case 0: [page _rightButtonClicked]; break; | |
82 | case 1: [page _leftButtonClicked]; break; | |
83 | } | |
84 | } | |
85 | ||
86 | - (void) navigationBar:(UINavigationBar *)navbar poppedItem:(UINavigationItem *)item { | |
87 | _assert([pages_ count] != 0); | |
88 | if (!resetting_) | |
89 | [[pages_ lastObject] setPageActive:NO]; | |
90 | [pages_ removeLastObject]; | |
91 | if (!resetting_) | |
92 | [self resetViewAnimated:YES toPage:[pages_ lastObject]]; | |
93 | } | |
94 | ||
95 | - (id) initWithFrame:(CGRect)frame { | |
96 | if ((self = [super initWithFrame:frame]) != nil) { | |
97 | pages_ = [[NSMutableArray arrayWithCapacity:4] retain]; | |
98 | ||
99 | struct CGRect bounds = [self bounds]; | |
20b8724d | 100 | CGSize navsize = [UINavigationBar defaultSize]; |
77f175ac JF |
101 | CGRect navrect = {{0, 0}, navsize}; |
102 | ||
bde2d79b | 103 | navbar_ = [[RVNavigationBar alloc] initWithFrame:navrect]; |
77f175ac JF |
104 | [self addSubview:navbar_]; |
105 | ||
2a987aa5 | 106 | [navbar_ setBarStyle:0]; |
77f175ac JF |
107 | [navbar_ setDelegate:self]; |
108 | ||
77f175ac JF |
109 | transition_ = [[UITransitionView alloc] initWithFrame:CGRectMake( |
110 | bounds.origin.x, bounds.origin.y + navsize.height, bounds.size.width, bounds.size.height - navsize.height | |
111 | )]; | |
112 | ||
113 | [self addSubview:transition_]; | |
114 | } return self; | |
115 | } | |
116 | ||
117 | - (void) setDelegate:(id)delegate { | |
118 | delegate_ = delegate; | |
119 | } | |
120 | ||
121 | - (void) setPage:(RVPage *)page { | |
122 | if ([pages_ count] != 0) | |
123 | [[pages_ lastObject] setPageActive:NO]; | |
124 | ||
125 | [navbar_ disableAnimation]; | |
126 | resetting_ = true; | |
127 | for (unsigned i(0), pages([pages_ count]); i != pages; ++i) | |
128 | [navbar_ popNavigationItem]; | |
129 | resetting_ = false; | |
130 | ||
131 | [self pushPage:page]; | |
132 | [navbar_ enableAnimation]; | |
133 | } | |
134 | ||
135 | - (void) pushPage:(RVPage *)page { | |
136 | if ([pages_ count] != 0) | |
137 | [[pages_ lastObject] setPageActive:NO]; | |
138 | ||
139 | NSString *title = [self getTitleForPage:page]; | |
140 | ||
141 | NSString *backButtonTitle = [page backButtonTitle]; | |
142 | if (backButtonTitle == nil) | |
143 | backButtonTitle = title; | |
144 | ||
145 | UINavigationItem *navitem = [[[UINavigationItem alloc] initWithTitle:title] autorelease]; | |
146 | [navitem setBackButtonTitle:backButtonTitle]; | |
147 | [navbar_ pushNavigationItem:navitem]; | |
148 | ||
149 | BOOL animated = [pages_ count] == 0 ? NO : YES; | |
20b8724d | 150 | [page setFrame:[transition_ bounds]]; |
77f175ac JF |
151 | [transition_ transition:(animated ? 1 : 0) toView:page]; |
152 | [page setPageActive:YES]; | |
153 | ||
154 | [pages_ addObject:page]; | |
155 | [self reloadButtonsForPage:page]; | |
156 | ||
d210b85d | 157 | [navbar_ setAccessoryView:[page accessoryView] animate:animated removeOnPop:NO]; |
77f175ac JF |
158 | } |
159 | ||
160 | - (void) popPages:(unsigned)pages { | |
161 | if (pages == 0) | |
162 | return; | |
163 | ||
164 | [[pages_ lastObject] setPageActive:NO]; | |
165 | ||
166 | resetting_ = true; | |
167 | for (unsigned i(0); i != pages; ++i) | |
168 | [navbar_ popNavigationItem]; | |
169 | resetting_ = false; | |
170 | ||
171 | [self resetViewAnimated:YES toPage:[pages_ lastObject]]; | |
172 | } | |
173 | ||
77f175ac JF |
174 | - (void) resetViewAnimated:(BOOL)animated { |
175 | resetting_ = true; | |
176 | ||
177 | if ([pages_ count] > 1) { | |
178 | [navbar_ disableAnimation]; | |
179 | while ([pages_ count] != (animated ? 2 : 1)) | |
180 | [navbar_ popNavigationItem]; | |
181 | [navbar_ enableAnimation]; | |
182 | if (animated) | |
183 | [navbar_ popNavigationItem]; | |
184 | } | |
185 | ||
186 | resetting_ = false; | |
187 | ||
188 | [self resetViewAnimated:animated toPage:[pages_ lastObject]]; | |
189 | } | |
190 | ||
191 | - (void) resetViewAnimated:(BOOL)animated toPage:(RVPage *)page { | |
192 | [page resetViewAnimated:animated]; | |
20b8724d | 193 | [page setFrame:[transition_ bounds]]; |
77f175ac JF |
194 | [transition_ transition:(animated ? 2 : 0) toView:page]; |
195 | [page setPageActive:YES]; | |
196 | [self reloadButtonsForPage:page]; | |
62203f58 | 197 | |
d210b85d | 198 | [navbar_ setAccessoryView:[page accessoryView] animate:animated removeOnPop:NO]; |
77f175ac JF |
199 | } |
200 | ||
5fd870e0 | 201 | - (void) setBackButtonTitle:(NSString *)title forPage:(RVPage *)page { |
77f175ac JF |
202 | if ([pages_ count] == 0 || page != [pages_ lastObject]) |
203 | return; | |
204 | UINavigationItem *navitem = [navbar_ topItem]; | |
5fd870e0 | 205 | [navitem setBackButtonTitle:title]; |
77f175ac JF |
206 | } |
207 | ||
5fd870e0 | 208 | - (void) reloadTitleForPage:(RVPage *)page { |
77f175ac JF |
209 | if ([pages_ count] == 0 || page != [pages_ lastObject]) |
210 | return; | |
211 | UINavigationItem *navitem = [navbar_ topItem]; | |
5fd870e0 JF |
212 | NSString *title = [self getTitleForPage:page]; |
213 | [navitem setTitle:title]; | |
77f175ac JF |
214 | } |
215 | ||
216 | - (void) reloadButtonsForPage:(RVPage *)page { | |
217 | if ([pages_ count] == 0 || page != [pages_ lastObject]) | |
218 | return; | |
faf4eb4f | 219 | NSString *leftButtonTitle([page leftButtonTitle]); |
20b8724d JF |
220 | UINavigationButtonStyle leftButtonStyle = [page leftButtonStyle]; |
221 | UINavigationButtonStyle rightButtonStyle = [page rightButtonStyle]; | |
faf4eb4f JF |
222 | //[navbar_ showButtonsWithLeftTitle:leftButtonTitle rightTitle:[page rightButtonTitle] leftBack:(leftButtonTitle == nil)]; |
223 | ||
224 | [navbar_ | |
225 | showLeftButton:leftButtonTitle | |
226 | withStyle:leftButtonStyle | |
227 | rightButton:[page rightButtonTitle] | |
228 | withStyle:rightButtonStyle | |
229 | ]; | |
77f175ac JF |
230 | } |
231 | ||
232 | - (NSString *) getTitleForPage:(RVPage *)page { | |
233 | return [page title]; | |
234 | } | |
235 | ||
236 | - (void) reloadData { | |
237 | for (int i(0), e([pages_ count]); i != e; ++i) { | |
238 | RVPage *page([pages_ objectAtIndex:(e - i - 1)]); | |
239 | [page reloadData]; | |
240 | } | |
241 | } | |
242 | ||
243 | - (CGRect) pageBounds { | |
244 | return [transition_ bounds]; | |
245 | } | |
246 | ||
247 | @end |