]>
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]; | |
bde2d79b JF |
55 | width = adjust; |
56 | return [super createButtonWithContents:contents width:width barStyle:barStyle buttonStyle:style isRight:right]; | |
57 | } | |
58 | ||
59 | @end | |
60 | ||
77f175ac JF |
61 | @implementation RVBook |
62 | ||
63 | - (void) dealloc { | |
64 | [navbar_ setDelegate:nil]; | |
04700693 JF |
65 | if (toolbar_ != nil) |
66 | [toolbar_ setDelegate:nil]; | |
77f175ac JF |
67 | |
68 | [pages_ release]; | |
69 | [navbar_ release]; | |
70 | [transition_ release]; | |
04700693 JF |
71 | if (toolbar_ != nil) |
72 | [toolbar_ release]; | |
77f175ac JF |
73 | [super dealloc]; |
74 | } | |
75 | ||
20b8724d JF |
76 | - (UINavigationBar *) navigationBar { |
77 | return navbar_; | |
78 | } | |
79 | ||
77f175ac JF |
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]; | |
20b8724d | 103 | CGSize navsize = [UINavigationBar defaultSize]; |
77f175ac JF |
104 | CGRect navrect = {{0, 0}, navsize}; |
105 | ||
bde2d79b | 106 | navbar_ = [[RVNavigationBar alloc] initWithFrame:navrect]; |
77f175ac JF |
107 | [self addSubview:navbar_]; |
108 | ||
2a987aa5 | 109 | [navbar_ setBarStyle:0]; |
77f175ac JF |
110 | [navbar_ setDelegate:self]; |
111 | ||
77f175ac JF |
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 | ||
1ce016d4 JF |
124 | - (void) setHook:(id)hook { |
125 | hook_ = hook; | |
126 | } | |
127 | ||
77f175ac JF |
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 | ||
5889429e JF |
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; | |
77f175ac | 152 | |
5889429e JF |
153 | [self pushPage:page animated:NO]; |
154 | [navbar_ enableAnimation]; | |
155 | } | |
156 | ||
157 | - (void) pushPage:(RVPage *)page animated:(BOOL)animated { | |
77f175ac JF |
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 | ||
20b8724d | 168 | [page setFrame:[transition_ bounds]]; |
77f175ac JF |
169 | [transition_ transition:(animated ? 1 : 0) toView:page]; |
170 | [page setPageActive:YES]; | |
171 | ||
172 | [pages_ addObject:page]; | |
173 | [self reloadButtonsForPage:page]; | |
174 | ||
d210b85d | 175 | [navbar_ setAccessoryView:[page accessoryView] animate:animated removeOnPop:NO]; |
77f175ac JF |
176 | } |
177 | ||
5889429e JF |
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)]; | |
182 | } | |
183 | ||
04700693 JF |
184 | - (void) pushBook:(RVBook *)book { |
185 | [delegate_ popUpBook:book]; | |
186 | } | |
187 | ||
77f175ac JF |
188 | - (void) popPages:(unsigned)pages { |
189 | if (pages == 0) | |
190 | return; | |
191 | ||
192 | [[pages_ lastObject] setPageActive:NO]; | |
193 | ||
194 | resetting_ = true; | |
195 | for (unsigned i(0); i != pages; ++i) | |
196 | [navbar_ popNavigationItem]; | |
197 | resetting_ = false; | |
198 | ||
199 | [self resetViewAnimated:YES toPage:[pages_ lastObject]]; | |
200 | } | |
201 | ||
77f175ac JF |
202 | - (void) resetViewAnimated:(BOOL)animated { |
203 | resetting_ = true; | |
204 | ||
205 | if ([pages_ count] > 1) { | |
206 | [navbar_ disableAnimation]; | |
207 | while ([pages_ count] != (animated ? 2 : 1)) | |
208 | [navbar_ popNavigationItem]; | |
209 | [navbar_ enableAnimation]; | |
210 | if (animated) | |
211 | [navbar_ popNavigationItem]; | |
212 | } | |
213 | ||
214 | resetting_ = false; | |
215 | ||
216 | [self resetViewAnimated:animated toPage:[pages_ lastObject]]; | |
217 | } | |
218 | ||
219 | - (void) resetViewAnimated:(BOOL)animated toPage:(RVPage *)page { | |
220 | [page resetViewAnimated:animated]; | |
20b8724d | 221 | [page setFrame:[transition_ bounds]]; |
77f175ac JF |
222 | [transition_ transition:(animated ? 2 : 0) toView:page]; |
223 | [page setPageActive:YES]; | |
224 | [self reloadButtonsForPage:page]; | |
62203f58 | 225 | |
d210b85d | 226 | [navbar_ setAccessoryView:[page accessoryView] animate:animated removeOnPop:NO]; |
77f175ac JF |
227 | } |
228 | ||
5fd870e0 | 229 | - (void) setBackButtonTitle:(NSString *)title forPage:(RVPage *)page { |
77f175ac JF |
230 | if ([pages_ count] == 0 || page != [pages_ lastObject]) |
231 | return; | |
232 | UINavigationItem *navitem = [navbar_ topItem]; | |
5fd870e0 | 233 | [navitem setBackButtonTitle:title]; |
77f175ac JF |
234 | } |
235 | ||
5fd870e0 | 236 | - (void) reloadTitleForPage:(RVPage *)page { |
77f175ac JF |
237 | if ([pages_ count] == 0 || page != [pages_ lastObject]) |
238 | return; | |
239 | UINavigationItem *navitem = [navbar_ topItem]; | |
5fd870e0 JF |
240 | NSString *title = [self getTitleForPage:page]; |
241 | [navitem setTitle:title]; | |
77f175ac JF |
242 | } |
243 | ||
0a7e5478 JF |
244 | - (void) _leftButtonTitle:(NSString *&)leftButtonTitle style:(UINavigationButtonStyle &)leftButtonStyle forPage:(RVPage *)page { |
245 | leftButtonTitle = [page leftButtonTitle]; | |
246 | leftButtonStyle = [page leftButtonStyle]; | |
04700693 JF |
247 | } |
248 | ||
77f175ac JF |
249 | - (void) reloadButtonsForPage:(RVPage *)page { |
250 | if ([pages_ count] == 0 || page != [pages_ lastObject]) | |
251 | return; | |
0a7e5478 JF |
252 | |
253 | NSString *leftButtonTitle; | |
254 | UINavigationButtonStyle leftButtonStyle; | |
255 | [self _leftButtonTitle:leftButtonTitle style:leftButtonStyle forPage:page]; | |
256 | ||
20b8724d | 257 | UINavigationButtonStyle rightButtonStyle = [page rightButtonStyle]; |
faf4eb4f JF |
258 | //[navbar_ showButtonsWithLeftTitle:leftButtonTitle rightTitle:[page rightButtonTitle] leftBack:(leftButtonTitle == nil)]; |
259 | ||
260 | [navbar_ | |
261 | showLeftButton:leftButtonTitle | |
262 | withStyle:leftButtonStyle | |
263 | rightButton:[page rightButtonTitle] | |
264 | withStyle:rightButtonStyle | |
265 | ]; | |
77f175ac JF |
266 | } |
267 | ||
268 | - (NSString *) getTitleForPage:(RVPage *)page { | |
269 | return [page title]; | |
270 | } | |
271 | ||
272 | - (void) reloadData { | |
3ff1504e JF |
273 | size_t count([pages_ count]); |
274 | for (size_t i(0); i != count; ++i) { | |
275 | RVPage *page([pages_ objectAtIndex:(count - i - 1)]); | |
77f175ac JF |
276 | [page reloadData]; |
277 | } | |
3ff1504e JF |
278 | |
279 | if (count != 0) { | |
280 | RVPage *page([pages_ lastObject]); | |
281 | [self reloadButtonsForPage:page]; | |
282 | } | |
77f175ac JF |
283 | } |
284 | ||
285 | - (CGRect) pageBounds { | |
286 | return [transition_ bounds]; | |
287 | } | |
288 | ||
04700693 | 289 | - (void) close { |
1ce016d4 JF |
290 | if (hook_ != nil) |
291 | [hook_ didCloseBook:self]; | |
04700693 JF |
292 | } |
293 | ||
294 | @end | |
295 | ||
296 | @implementation RVPopUpBook | |
297 | ||
0a7e5478 JF |
298 | - (void) _leftButtonTitle:(NSString *&)leftButtonTitle style:(UINavigationButtonStyle &)leftButtonStyle forPage:(RVPage *)page { |
299 | [super _leftButtonTitle:leftButtonTitle style:leftButtonStyle forPage:page]; | |
300 | if ((cancel_ = leftButtonTitle == nil && [pages_ count] == 1)) { | |
9d7f5e2d | 301 | leftButtonTitle = [[NSBundle mainBundle] localizedStringForKey:@"CANCEL" value:nil table:nil]; |
0a7e5478 JF |
302 | leftButtonStyle = UINavigationButtonStyleNormal; |
303 | } | |
04700693 JF |
304 | } |
305 | ||
306 | - (void) navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button { | |
307 | if (button == 1 && cancel_) | |
308 | [self close]; | |
309 | else | |
310 | [super navigationBar:navbar buttonClicked:button]; | |
311 | } | |
312 | ||
313 | - (void) close { | |
314 | [self popFromSuperviewAnimated:YES]; | |
1ce016d4 | 315 | [super close]; |
04700693 JF |
316 | } |
317 | ||
77f175ac | 318 | @end |