]>
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 | ||
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 | ||
138 | - (void) pushPage:(RVPage *)page { | |
139 | if ([pages_ count] != 0) | |
140 | [[pages_ lastObject] setPageActive:NO]; | |
141 | ||
142 | NSString *title = [self getTitleForPage:page]; | |
143 | ||
144 | NSString *backButtonTitle = [page backButtonTitle]; | |
145 | if (backButtonTitle == nil) | |
146 | backButtonTitle = title; | |
147 | ||
148 | UINavigationItem *navitem = [[[UINavigationItem alloc] initWithTitle:title] autorelease]; | |
149 | [navitem setBackButtonTitle:backButtonTitle]; | |
150 | [navbar_ pushNavigationItem:navitem]; | |
151 | ||
152 | BOOL animated = [pages_ count] == 0 ? NO : YES; | |
20b8724d | 153 | [page setFrame:[transition_ bounds]]; |
77f175ac JF |
154 | [transition_ transition:(animated ? 1 : 0) toView:page]; |
155 | [page setPageActive:YES]; | |
156 | ||
157 | [pages_ addObject:page]; | |
158 | [self reloadButtonsForPage:page]; | |
159 | ||
d210b85d | 160 | [navbar_ setAccessoryView:[page accessoryView] animate:animated removeOnPop:NO]; |
77f175ac JF |
161 | } |
162 | ||
04700693 JF |
163 | - (void) pushBook:(RVBook *)book { |
164 | [delegate_ popUpBook:book]; | |
165 | } | |
166 | ||
77f175ac JF |
167 | - (void) popPages:(unsigned)pages { |
168 | if (pages == 0) | |
169 | return; | |
170 | ||
171 | [[pages_ lastObject] setPageActive:NO]; | |
172 | ||
173 | resetting_ = true; | |
174 | for (unsigned i(0); i != pages; ++i) | |
175 | [navbar_ popNavigationItem]; | |
176 | resetting_ = false; | |
177 | ||
178 | [self resetViewAnimated:YES toPage:[pages_ lastObject]]; | |
179 | } | |
180 | ||
77f175ac JF |
181 | - (void) resetViewAnimated:(BOOL)animated { |
182 | resetting_ = true; | |
183 | ||
184 | if ([pages_ count] > 1) { | |
185 | [navbar_ disableAnimation]; | |
186 | while ([pages_ count] != (animated ? 2 : 1)) | |
187 | [navbar_ popNavigationItem]; | |
188 | [navbar_ enableAnimation]; | |
189 | if (animated) | |
190 | [navbar_ popNavigationItem]; | |
191 | } | |
192 | ||
193 | resetting_ = false; | |
194 | ||
195 | [self resetViewAnimated:animated toPage:[pages_ lastObject]]; | |
196 | } | |
197 | ||
198 | - (void) resetViewAnimated:(BOOL)animated toPage:(RVPage *)page { | |
199 | [page resetViewAnimated:animated]; | |
20b8724d | 200 | [page setFrame:[transition_ bounds]]; |
77f175ac JF |
201 | [transition_ transition:(animated ? 2 : 0) toView:page]; |
202 | [page setPageActive:YES]; | |
203 | [self reloadButtonsForPage:page]; | |
62203f58 | 204 | |
d210b85d | 205 | [navbar_ setAccessoryView:[page accessoryView] animate:animated removeOnPop:NO]; |
77f175ac JF |
206 | } |
207 | ||
5fd870e0 | 208 | - (void) setBackButtonTitle:(NSString *)title forPage:(RVPage *)page { |
77f175ac JF |
209 | if ([pages_ count] == 0 || page != [pages_ lastObject]) |
210 | return; | |
211 | UINavigationItem *navitem = [navbar_ topItem]; | |
5fd870e0 | 212 | [navitem setBackButtonTitle:title]; |
77f175ac JF |
213 | } |
214 | ||
5fd870e0 | 215 | - (void) reloadTitleForPage:(RVPage *)page { |
77f175ac JF |
216 | if ([pages_ count] == 0 || page != [pages_ lastObject]) |
217 | return; | |
218 | UINavigationItem *navitem = [navbar_ topItem]; | |
5fd870e0 JF |
219 | NSString *title = [self getTitleForPage:page]; |
220 | [navitem setTitle:title]; | |
77f175ac JF |
221 | } |
222 | ||
0a7e5478 JF |
223 | - (void) _leftButtonTitle:(NSString *&)leftButtonTitle style:(UINavigationButtonStyle &)leftButtonStyle forPage:(RVPage *)page { |
224 | leftButtonTitle = [page leftButtonTitle]; | |
225 | leftButtonStyle = [page leftButtonStyle]; | |
04700693 JF |
226 | } |
227 | ||
77f175ac JF |
228 | - (void) reloadButtonsForPage:(RVPage *)page { |
229 | if ([pages_ count] == 0 || page != [pages_ lastObject]) | |
230 | return; | |
0a7e5478 JF |
231 | |
232 | NSString *leftButtonTitle; | |
233 | UINavigationButtonStyle leftButtonStyle; | |
234 | [self _leftButtonTitle:leftButtonTitle style:leftButtonStyle forPage:page]; | |
235 | ||
20b8724d | 236 | UINavigationButtonStyle rightButtonStyle = [page rightButtonStyle]; |
faf4eb4f JF |
237 | //[navbar_ showButtonsWithLeftTitle:leftButtonTitle rightTitle:[page rightButtonTitle] leftBack:(leftButtonTitle == nil)]; |
238 | ||
239 | [navbar_ | |
240 | showLeftButton:leftButtonTitle | |
241 | withStyle:leftButtonStyle | |
242 | rightButton:[page rightButtonTitle] | |
243 | withStyle:rightButtonStyle | |
244 | ]; | |
77f175ac JF |
245 | } |
246 | ||
247 | - (NSString *) getTitleForPage:(RVPage *)page { | |
248 | return [page title]; | |
249 | } | |
250 | ||
251 | - (void) reloadData { | |
252 | for (int i(0), e([pages_ count]); i != e; ++i) { | |
253 | RVPage *page([pages_ objectAtIndex:(e - i - 1)]); | |
254 | [page reloadData]; | |
255 | } | |
256 | } | |
257 | ||
258 | - (CGRect) pageBounds { | |
259 | return [transition_ bounds]; | |
260 | } | |
261 | ||
04700693 JF |
262 | - (void) close { |
263 | } | |
264 | ||
265 | @end | |
266 | ||
267 | @implementation RVPopUpBook | |
268 | ||
0a7e5478 JF |
269 | - (void) _leftButtonTitle:(NSString *&)leftButtonTitle style:(UINavigationButtonStyle &)leftButtonStyle forPage:(RVPage *)page { |
270 | [super _leftButtonTitle:leftButtonTitle style:leftButtonStyle forPage:page]; | |
271 | if ((cancel_ = leftButtonTitle == nil && [pages_ count] == 1)) { | |
272 | leftButtonTitle = @"Cancel"; | |
273 | leftButtonStyle = UINavigationButtonStyleNormal; | |
274 | } | |
04700693 JF |
275 | } |
276 | ||
277 | - (void) navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button { | |
278 | if (button == 1 && cancel_) | |
279 | [self close]; | |
280 | else | |
281 | [super navigationBar:navbar buttonClicked:button]; | |
282 | } | |
283 | ||
284 | - (void) close { | |
285 | [self popFromSuperviewAnimated:YES]; | |
286 | } | |
287 | ||
77f175ac | 288 | @end |