]>
Commit | Line | Data |
---|---|---|
7e9a36b6 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 | ||
a9543575 JF |
11 | #import <Foundation/Foundation.h> |
12 | #import <CoreGraphics/CGGeometry.h> | |
13 | ||
14 | #include <cstdio> | |
15 | #include <cstdlib> | |
16 | ||
17 | #include <errno.h> | |
18 | ||
7e9a36b6 JF |
19 | #import "RVPage.h" |
20 | ||
22f8bed9 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]; | |
22f8bed9 JF |
55 | width = adjust; |
56 | return [super createButtonWithContents:contents width:width barStyle:barStyle buttonStyle:style isRight:right]; | |
57 | } | |
58 | ||
59 | @end | |
60 | ||
7e9a36b6 JF |
61 | @implementation RVBook |
62 | ||
63 | - (void) dealloc { | |
64 | [navbar_ setDelegate:nil]; | |
aa5d0de7 JF |
65 | if (toolbar_ != nil) |
66 | [toolbar_ setDelegate:nil]; | |
7e9a36b6 JF |
67 | |
68 | [pages_ release]; | |
69 | [navbar_ release]; | |
70 | [transition_ release]; | |
aa5d0de7 JF |
71 | if (toolbar_ != nil) |
72 | [toolbar_ release]; | |
7e9a36b6 JF |
73 | [super dealloc]; |
74 | } | |
75 | ||
facbc56d JF |
76 | - (UINavigationBar *) navigationBar { |
77 | return navbar_; | |
78 | } | |
79 | ||
7e9a36b6 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]; | |
facbc56d | 103 | CGSize navsize = [UINavigationBar defaultSize]; |
7e9a36b6 JF |
104 | CGRect navrect = {{0, 0}, navsize}; |
105 | ||
22f8bed9 | 106 | navbar_ = [[RVNavigationBar alloc] initWithFrame:navrect]; |
7e9a36b6 JF |
107 | [self addSubview:navbar_]; |
108 | ||
2388b078 | 109 | [navbar_ setBarStyle:0]; |
7e9a36b6 JF |
110 | [navbar_ setDelegate:self]; |
111 | ||
7e9a36b6 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 | ||
124 | - (void) setPage:(RVPage *)page { | |
125 | if ([pages_ count] != 0) | |
126 | [[pages_ lastObject] setPageActive:NO]; | |
127 | ||
128 | [navbar_ disableAnimation]; | |
129 | resetting_ = true; | |
130 | for (unsigned i(0), pages([pages_ count]); i != pages; ++i) | |
131 | [navbar_ popNavigationItem]; | |
132 | resetting_ = false; | |
133 | ||
134 | [self pushPage:page]; | |
135 | [navbar_ enableAnimation]; | |
136 | } | |
137 | ||
2f3ef0b1 JF |
138 | - (void) swapPage:(RVPage *)page { |
139 | if ([pages_ count] == 0) | |
140 | return [self pushPage:page]; | |
141 | ||
142 | [[pages_ lastObject] setPageActive:NO]; | |
143 | ||
144 | [navbar_ disableAnimation]; | |
145 | resetting_ = true; | |
146 | [navbar_ popNavigationItem]; | |
147 | resetting_ = false; | |
7e9a36b6 | 148 | |
2f3ef0b1 JF |
149 | [self pushPage:page animated:NO]; |
150 | [navbar_ enableAnimation]; | |
151 | } | |
152 | ||
153 | - (void) pushPage:(RVPage *)page animated:(BOOL)animated { | |
7e9a36b6 JF |
154 | NSString *title = [self getTitleForPage:page]; |
155 | ||
156 | NSString *backButtonTitle = [page backButtonTitle]; | |
157 | if (backButtonTitle == nil) | |
158 | backButtonTitle = title; | |
159 | ||
160 | UINavigationItem *navitem = [[[UINavigationItem alloc] initWithTitle:title] autorelease]; | |
161 | [navitem setBackButtonTitle:backButtonTitle]; | |
162 | [navbar_ pushNavigationItem:navitem]; | |
163 | ||
facbc56d | 164 | [page setFrame:[transition_ bounds]]; |
7e9a36b6 JF |
165 | [transition_ transition:(animated ? 1 : 0) toView:page]; |
166 | [page setPageActive:YES]; | |
167 | ||
168 | [pages_ addObject:page]; | |
169 | [self reloadButtonsForPage:page]; | |
170 | ||
a9543575 | 171 | [navbar_ setAccessoryView:[page accessoryView] animate:animated removeOnPop:NO]; |
7e9a36b6 JF |
172 | } |
173 | ||
2f3ef0b1 JF |
174 | - (void) pushPage:(RVPage *)page { |
175 | if ([pages_ count] != 0) | |
176 | [[pages_ lastObject] setPageActive:NO]; | |
177 | [self pushPage:page animated:([pages_ count] == 0 ? NO : YES)]; | |
178 | } | |
179 | ||
aa5d0de7 JF |
180 | - (void) pushBook:(RVBook *)book { |
181 | [delegate_ popUpBook:book]; | |
182 | } | |
183 | ||
7e9a36b6 JF |
184 | - (void) popPages:(unsigned)pages { |
185 | if (pages == 0) | |
186 | return; | |
187 | ||
188 | [[pages_ lastObject] setPageActive:NO]; | |
189 | ||
190 | resetting_ = true; | |
191 | for (unsigned i(0); i != pages; ++i) | |
192 | [navbar_ popNavigationItem]; | |
193 | resetting_ = false; | |
194 | ||
195 | [self resetViewAnimated:YES toPage:[pages_ lastObject]]; | |
196 | } | |
197 | ||
7e9a36b6 JF |
198 | - (void) resetViewAnimated:(BOOL)animated { |
199 | resetting_ = true; | |
200 | ||
201 | if ([pages_ count] > 1) { | |
202 | [navbar_ disableAnimation]; | |
203 | while ([pages_ count] != (animated ? 2 : 1)) | |
204 | [navbar_ popNavigationItem]; | |
205 | [navbar_ enableAnimation]; | |
206 | if (animated) | |
207 | [navbar_ popNavigationItem]; | |
208 | } | |
209 | ||
210 | resetting_ = false; | |
211 | ||
212 | [self resetViewAnimated:animated toPage:[pages_ lastObject]]; | |
213 | } | |
214 | ||
215 | - (void) resetViewAnimated:(BOOL)animated toPage:(RVPage *)page { | |
216 | [page resetViewAnimated:animated]; | |
facbc56d | 217 | [page setFrame:[transition_ bounds]]; |
7e9a36b6 JF |
218 | [transition_ transition:(animated ? 2 : 0) toView:page]; |
219 | [page setPageActive:YES]; | |
220 | [self reloadButtonsForPage:page]; | |
c15d236a | 221 | |
a9543575 | 222 | [navbar_ setAccessoryView:[page accessoryView] animate:animated removeOnPop:NO]; |
7e9a36b6 JF |
223 | } |
224 | ||
3160e5f0 | 225 | - (void) setBackButtonTitle:(NSString *)title forPage:(RVPage *)page { |
7e9a36b6 JF |
226 | if ([pages_ count] == 0 || page != [pages_ lastObject]) |
227 | return; | |
228 | UINavigationItem *navitem = [navbar_ topItem]; | |
3160e5f0 | 229 | [navitem setBackButtonTitle:title]; |
7e9a36b6 JF |
230 | } |
231 | ||
3160e5f0 | 232 | - (void) reloadTitleForPage:(RVPage *)page { |
7e9a36b6 JF |
233 | if ([pages_ count] == 0 || page != [pages_ lastObject]) |
234 | return; | |
235 | UINavigationItem *navitem = [navbar_ topItem]; | |
3160e5f0 JF |
236 | NSString *title = [self getTitleForPage:page]; |
237 | [navitem setTitle:title]; | |
7e9a36b6 JF |
238 | } |
239 | ||
00e2109e JF |
240 | - (void) _leftButtonTitle:(NSString *&)leftButtonTitle style:(UINavigationButtonStyle &)leftButtonStyle forPage:(RVPage *)page { |
241 | leftButtonTitle = [page leftButtonTitle]; | |
242 | leftButtonStyle = [page leftButtonStyle]; | |
aa5d0de7 JF |
243 | } |
244 | ||
7e9a36b6 JF |
245 | - (void) reloadButtonsForPage:(RVPage *)page { |
246 | if ([pages_ count] == 0 || page != [pages_ lastObject]) | |
247 | return; | |
00e2109e JF |
248 | |
249 | NSString *leftButtonTitle; | |
250 | UINavigationButtonStyle leftButtonStyle; | |
251 | [self _leftButtonTitle:leftButtonTitle style:leftButtonStyle forPage:page]; | |
252 | ||
facbc56d | 253 | UINavigationButtonStyle rightButtonStyle = [page rightButtonStyle]; |
7b0ce2da JF |
254 | //[navbar_ showButtonsWithLeftTitle:leftButtonTitle rightTitle:[page rightButtonTitle] leftBack:(leftButtonTitle == nil)]; |
255 | ||
256 | [navbar_ | |
257 | showLeftButton:leftButtonTitle | |
258 | withStyle:leftButtonStyle | |
259 | rightButton:[page rightButtonTitle] | |
260 | withStyle:rightButtonStyle | |
261 | ]; | |
7e9a36b6 JF |
262 | } |
263 | ||
264 | - (NSString *) getTitleForPage:(RVPage *)page { | |
265 | return [page title]; | |
266 | } | |
267 | ||
268 | - (void) reloadData { | |
269 | for (int i(0), e([pages_ count]); i != e; ++i) { | |
270 | RVPage *page([pages_ objectAtIndex:(e - i - 1)]); | |
271 | [page reloadData]; | |
272 | } | |
273 | } | |
274 | ||
275 | - (CGRect) pageBounds { | |
276 | return [transition_ bounds]; | |
277 | } | |
278 | ||
aa5d0de7 JF |
279 | - (void) close { |
280 | } | |
281 | ||
282 | @end | |
283 | ||
284 | @implementation RVPopUpBook | |
285 | ||
00e2109e JF |
286 | - (void) _leftButtonTitle:(NSString *&)leftButtonTitle style:(UINavigationButtonStyle &)leftButtonStyle forPage:(RVPage *)page { |
287 | [super _leftButtonTitle:leftButtonTitle style:leftButtonStyle forPage:page]; | |
288 | if ((cancel_ = leftButtonTitle == nil && [pages_ count] == 1)) { | |
289 | leftButtonTitle = @"Cancel"; | |
290 | leftButtonStyle = UINavigationButtonStyleNormal; | |
291 | } | |
aa5d0de7 JF |
292 | } |
293 | ||
294 | - (void) navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button { | |
295 | if (button == 1 && cancel_) | |
296 | [self close]; | |
297 | else | |
298 | [super navigationBar:navbar buttonClicked:button]; | |
299 | } | |
300 | ||
301 | - (void) close { | |
302 | [self popFromSuperviewAnimated:YES]; | |
303 | } | |
304 | ||
7e9a36b6 | 305 | @end |