]>
Commit | Line | Data |
---|---|---|
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 | ||
11 | #import <Foundation/Foundation.h> | |
12 | #import <CoreGraphics/CGGeometry.h> | |
13 | ||
14 | #include <cstdio> | |
15 | #include <cstdlib> | |
16 | ||
17 | #include <errno.h> | |
18 | ||
19 | #import "RVPage.h" | |
20 | ||
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 | width = adjust; | |
56 | return [super createButtonWithContents:contents width:width barStyle:barStyle buttonStyle:style isRight:right]; | |
57 | } | |
58 | ||
59 | @end | |
60 | ||
61 | @implementation RVBook | |
62 | ||
63 | - (void) dealloc { | |
64 | [navbar_ setDelegate:nil]; | |
65 | if (toolbar_ != nil) | |
66 | [toolbar_ setDelegate:nil]; | |
67 | ||
68 | [pages_ release]; | |
69 | [navbar_ release]; | |
70 | [transition_ release]; | |
71 | if (toolbar_ != nil) | |
72 | [toolbar_ release]; | |
73 | [super dealloc]; | |
74 | } | |
75 | ||
76 | - (UINavigationBar *) navigationBar { | |
77 | return navbar_; | |
78 | } | |
79 | ||
80 | - (void) navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button { | |
81 | _assert([pages_ count] != 0); | |
82 | RVPage *page = [pages_ lastObject]; | |
83 | switch (button) { | |
84 | case 0: [page _rightButtonClicked]; break; | |
85 | case 1: [page _leftButtonClicked]; break; | |
86 | } | |
87 | } | |
88 | ||
89 | - (void) navigationBar:(UINavigationBar *)navbar poppedItem:(UINavigationItem *)item { | |
90 | _assert([pages_ count] != 0); | |
91 | if (!resetting_) | |
92 | [[pages_ lastObject] setPageActive:NO]; | |
93 | [pages_ removeLastObject]; | |
94 | if (!resetting_) | |
95 | [self resetViewAnimated:YES toPage:[pages_ lastObject]]; | |
96 | } | |
97 | ||
98 | - (id) initWithFrame:(CGRect)frame { | |
99 | if ((self = [super initWithFrame:frame]) != nil) { | |
100 | pages_ = [[NSMutableArray arrayWithCapacity:4] retain]; | |
101 | ||
102 | struct CGRect bounds = [self bounds]; | |
103 | CGSize navsize = [UINavigationBar defaultSize]; | |
104 | CGRect navrect = {{0, 0}, navsize}; | |
105 | ||
106 | navbar_ = [[RVNavigationBar alloc] initWithFrame:navrect]; | |
107 | [self addSubview:navbar_]; | |
108 | ||
109 | [navbar_ setBarStyle:0]; | |
110 | [navbar_ setDelegate:self]; | |
111 | ||
112 | transition_ = [[UITransitionView alloc] initWithFrame:CGRectMake( | |
113 | bounds.origin.x, bounds.origin.y + navsize.height, bounds.size.width, bounds.size.height - navsize.height | |
114 | )]; | |
115 | ||
116 | [self addSubview:transition_]; | |
117 | } return self; | |
118 | } | |
119 | ||
120 | - (void) setDelegate:(id)delegate { | |
121 | delegate_ = delegate; | |
122 | } | |
123 | ||
124 | - (void) setHook:(id)hook { | |
125 | hook_ = hook; | |
126 | } | |
127 | ||
128 | - (void) setPage:(RVPage *)page { | |
129 | if ([pages_ count] != 0) | |
130 | [[pages_ lastObject] setPageActive:NO]; | |
131 | ||
132 | [navbar_ disableAnimation]; | |
133 | resetting_ = true; | |
134 | for (unsigned i(0), pages([pages_ count]); i != pages; ++i) | |
135 | [navbar_ popNavigationItem]; | |
136 | resetting_ = false; | |
137 | ||
138 | [self pushPage:page]; | |
139 | [navbar_ enableAnimation]; | |
140 | } | |
141 | ||
142 | - (void) swapPage:(RVPage *)page { | |
143 | if ([pages_ count] == 0) | |
144 | return [self pushPage:page]; | |
145 | ||
146 | [[pages_ lastObject] setPageActive:NO]; | |
147 | ||
148 | [navbar_ disableAnimation]; | |
149 | resetting_ = true; | |
150 | [navbar_ popNavigationItem]; | |
151 | resetting_ = false; | |
152 | ||
153 | [self pushPage:page animated:NO]; | |
154 | [navbar_ enableAnimation]; | |
155 | } | |
156 | ||
157 | - (void) pushPage:(RVPage *)page animated:(BOOL)animated { | |
158 | NSString *title = [self getTitleForPage:page]; | |
159 | ||
160 | NSString *backButtonTitle = [page backButtonTitle]; | |
161 | if (backButtonTitle == nil) | |
162 | backButtonTitle = title; | |
163 | ||
164 | UINavigationItem *navitem = [[[UINavigationItem alloc] initWithTitle:title] autorelease]; | |
165 | [navitem setBackButtonTitle:backButtonTitle]; | |
166 | [navbar_ pushNavigationItem:navitem]; | |
167 | ||
168 | [page setFrame:[transition_ bounds]]; | |
169 | [transition_ transition:(animated ? 1 : 0) toView:page]; | |
170 | [page setPageActive:YES]; | |
171 | ||
172 | [pages_ addObject:page]; | |
173 | [self reloadButtonsForPage:page]; | |
174 | ||
175 | if ([navbar_ respondsToSelector:@selector(setAccessoryView:animate:removeOnPop:)]) | |
176 | [navbar_ setAccessoryView:[page accessoryView] animate:animated removeOnPop:NO]; | |
177 | else | |
178 | [navbar_ setAccessoryView:[page accessoryView] animate:animated]; | |
179 | } | |
180 | ||
181 | - (void) pushPage:(RVPage *)page { | |
182 | if ([pages_ count] != 0) | |
183 | [[pages_ lastObject] setPageActive:NO]; | |
184 | [self pushPage:page animated:([pages_ count] == 0 ? NO : YES)]; | |
185 | } | |
186 | ||
187 | - (void) pushBook:(RVBook *)book { | |
188 | [delegate_ popUpBook:book]; | |
189 | } | |
190 | ||
191 | - (void) popPages:(unsigned)pages { | |
192 | if (pages == 0) | |
193 | return; | |
194 | ||
195 | [[pages_ lastObject] setPageActive:NO]; | |
196 | ||
197 | resetting_ = true; | |
198 | for (unsigned i(0); i != pages; ++i) | |
199 | [navbar_ popNavigationItem]; | |
200 | resetting_ = false; | |
201 | ||
202 | [self resetViewAnimated:YES toPage:[pages_ lastObject]]; | |
203 | } | |
204 | ||
205 | - (void) resetViewAnimated:(BOOL)animated { | |
206 | resetting_ = true; | |
207 | ||
208 | if ([pages_ count] > 1) { | |
209 | [navbar_ disableAnimation]; | |
210 | while ([pages_ count] != (animated ? 2 : 1)) | |
211 | [navbar_ popNavigationItem]; | |
212 | [navbar_ enableAnimation]; | |
213 | if (animated) | |
214 | [navbar_ popNavigationItem]; | |
215 | } | |
216 | ||
217 | resetting_ = false; | |
218 | ||
219 | [self resetViewAnimated:animated toPage:[pages_ lastObject]]; | |
220 | } | |
221 | ||
222 | - (void) resetViewAnimated:(BOOL)animated toPage:(RVPage *)page { | |
223 | [page resetViewAnimated:animated]; | |
224 | [page setFrame:[transition_ bounds]]; | |
225 | [transition_ transition:(animated ? 2 : 0) toView:page]; | |
226 | [page setPageActive:YES]; | |
227 | [self reloadButtonsForPage:page]; | |
228 | ||
229 | if ([navbar_ respondsToSelector:@selector(setAccessoryView:animate:removeOnPop:)]) | |
230 | [navbar_ setAccessoryView:[page accessoryView] animate:animated removeOnPop:NO]; | |
231 | else | |
232 | [navbar_ setAccessoryView:[page accessoryView] animate:animated]; | |
233 | } | |
234 | ||
235 | - (void) setBackButtonTitle:(NSString *)title forPage:(RVPage *)page { | |
236 | if ([pages_ count] == 0 || page != [pages_ lastObject]) | |
237 | return; | |
238 | UINavigationItem *navitem = [navbar_ topItem]; | |
239 | [navitem setBackButtonTitle:title]; | |
240 | } | |
241 | ||
242 | - (void) reloadTitleForPage:(RVPage *)page { | |
243 | if ([pages_ count] == 0 || page != [pages_ lastObject]) | |
244 | return; | |
245 | UINavigationItem *navitem = [navbar_ topItem]; | |
246 | NSString *title = [self getTitleForPage:page]; | |
247 | [navitem setTitle:title]; | |
248 | } | |
249 | ||
250 | - (void) _leftButtonTitle:(NSString *&)leftButtonTitle style:(UINavigationButtonStyle &)leftButtonStyle forPage:(RVPage *)page { | |
251 | leftButtonTitle = [page leftButtonTitle]; | |
252 | leftButtonStyle = [page leftButtonStyle]; | |
253 | } | |
254 | ||
255 | - (void) reloadButtonsForPage:(RVPage *)page { | |
256 | if ([pages_ count] == 0 || page != [pages_ lastObject]) | |
257 | return; | |
258 | ||
259 | NSString *leftButtonTitle; | |
260 | UINavigationButtonStyle leftButtonStyle; | |
261 | [self _leftButtonTitle:leftButtonTitle style:leftButtonStyle forPage:page]; | |
262 | ||
263 | UINavigationButtonStyle rightButtonStyle = [page rightButtonStyle]; | |
264 | //[navbar_ showButtonsWithLeftTitle:leftButtonTitle rightTitle:[page rightButtonTitle] leftBack:(leftButtonTitle == nil)]; | |
265 | ||
266 | [navbar_ | |
267 | showLeftButton:leftButtonTitle | |
268 | withStyle:leftButtonStyle | |
269 | rightButton:[page rightButtonTitle] | |
270 | withStyle:rightButtonStyle | |
271 | ]; | |
272 | } | |
273 | ||
274 | - (NSString *) getTitleForPage:(RVPage *)page { | |
275 | return [page title]; | |
276 | } | |
277 | ||
278 | - (void) reloadData { | |
279 | size_t count([pages_ count]); | |
280 | for (size_t i(0); i != count; ++i) { | |
281 | RVPage *page([pages_ objectAtIndex:(count - i - 1)]); | |
282 | [page reloadData]; | |
283 | } | |
284 | ||
285 | if (count != 0) { | |
286 | RVPage *page([pages_ lastObject]); | |
287 | [self reloadButtonsForPage:page]; | |
288 | } | |
289 | } | |
290 | ||
291 | - (void) reloadButtons { | |
292 | size_t count([pages_ count]); | |
293 | if (count != 0) { | |
294 | RVPage *page([pages_ lastObject]); | |
295 | [self reloadButtonsForPage:page]; | |
296 | } | |
297 | } | |
298 | ||
299 | - (CGRect) pageBounds { | |
300 | return [transition_ bounds]; | |
301 | } | |
302 | ||
303 | - (void) close { | |
304 | if (hook_ != nil) | |
305 | [hook_ didCloseBook:self]; | |
306 | } | |
307 | ||
308 | @end | |
309 | ||
310 | @implementation RVPopUpBook | |
311 | ||
312 | - (void) _leftButtonTitle:(NSString *&)leftButtonTitle style:(UINavigationButtonStyle &)leftButtonStyle forPage:(RVPage *)page { | |
313 | [super _leftButtonTitle:leftButtonTitle style:leftButtonStyle forPage:page]; | |
314 | if ((cancel_ = leftButtonTitle == nil && [pages_ count] == 1)) { | |
315 | leftButtonTitle = [[NSBundle mainBundle] localizedStringForKey:@"CANCEL" value:nil table:nil]; | |
316 | leftButtonStyle = UINavigationButtonStyleNormal; | |
317 | } | |
318 | } | |
319 | ||
320 | - (void) navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button { | |
321 | if (button == 1 && cancel_) | |
322 | [self close]; | |
323 | else | |
324 | [super navigationBar:navbar buttonClicked:button]; | |
325 | } | |
326 | ||
327 | - (void) close { | |
328 | [self popFromSuperviewAnimated:YES]; | |
329 | [super close]; | |
330 | } | |
331 | ||
332 | @end |