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