]> git.saurik.com Git - cydia.git/blob - UICaboodle/RVBook.mm
Loading changes, visibility switches, gsc.wildcat issues, remote package catalog...
[cydia.git] / UICaboodle / RVBook.mm
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 for (RVPage *page in pages_)
69 [page setBook:nil];
70
71 [pages_ release];
72 [navbar_ release];
73 [transition_ release];
74 if (toolbar_ != nil)
75 [toolbar_ release];
76 [super dealloc];
77 }
78
79 - (UINavigationBar *) navigationBar {
80 return navbar_;
81 }
82
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];
106 CGSize navsize = [UINavigationBar defaultSize];
107 CGRect navrect = {{0, 0}, {bounds.size.width, navsize.height}};
108
109 navbar_ = [[RVNavigationBar alloc] initWithFrame:navrect];
110 [navbar_ setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
111 [self addSubview:navbar_];
112
113 [navbar_ setBarStyle:0];
114 [navbar_ setDelegate:self];
115
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
120 [transition_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
121
122 [self addSubview:transition_];
123 } return self;
124 }
125
126 - (void) setDelegate:(id)delegate {
127 delegate_ = delegate;
128 }
129
130 - (void) setHook:(id)hook {
131 hook_ = hook;
132 }
133
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
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;
158
159 [self pushPage:page animated:NO];
160 [navbar_ enableAnimation];
161 }
162
163 - (void) pushPage:(RVPage *)page animated:(BOOL)animated {
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
174 [page setFrame:[transition_ bounds]];
175 [transition_ transition:(animated ? 1 : 0) toView:page];
176 [page setPageActive:YES];
177
178 [pages_ addObject:page];
179 [self reloadButtonsForPage:page];
180
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];
185 }
186
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
193 - (void) pushBook:(RVBook *)book {
194 [delegate_ popUpBook:book];
195 }
196
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
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];
230 [page setFrame:[transition_ bounds]];
231 [transition_ transition:(animated ? 2 : 0) toView:page];
232 [page setPageActive:YES];
233 [self reloadButtonsForPage:page];
234
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];
239 }
240
241 - (void) setBackButtonTitle:(NSString *)title forPage:(RVPage *)page {
242 if ([pages_ count] == 0 || page != [pages_ lastObject])
243 return;
244 UINavigationItem *navitem = [navbar_ topItem];
245 [navitem setBackButtonTitle:title];
246 }
247
248 - (void) reloadTitleForPage:(RVPage *)page {
249 if ([pages_ count] == 0 || page != [pages_ lastObject])
250 return;
251 UINavigationItem *navitem = [navbar_ topItem];
252 NSString *title = [self getTitleForPage:page];
253 [navitem setTitle:title];
254 }
255
256 - (void) _leftButtonTitle:(NSString *&)leftButtonTitle style:(UINavigationButtonStyle &)leftButtonStyle forPage:(RVPage *)page {
257 leftButtonTitle = [page leftButtonTitle];
258 leftButtonStyle = [page leftButtonStyle];
259 }
260
261 - (void) reloadButtonsForPage:(RVPage *)page {
262 if ([pages_ count] == 0 || page != [pages_ lastObject])
263 return;
264
265 NSString *leftButtonTitle;
266 UINavigationButtonStyle leftButtonStyle;
267 [self _leftButtonTitle:leftButtonTitle style:leftButtonStyle forPage:page];
268
269 UINavigationButtonStyle rightButtonStyle = [page rightButtonStyle];
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 ];
278 }
279
280 - (NSString *) getTitleForPage:(RVPage *)page {
281 return [page title];
282 }
283
284 - (void) reloadData {
285 size_t count([pages_ count]);
286 for (size_t i(0); i != count; ++i) {
287 RVPage *page([pages_ objectAtIndex:(count - i - 1)]);
288 [page reloadData];
289 }
290
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]);
299 if (count != 0) {
300 RVPage *page([pages_ lastObject]);
301 [self reloadButtonsForPage:page];
302 }
303 }
304
305 - (CGRect) pageBounds {
306 return [transition_ bounds];
307 }
308
309 - (void) close {
310 if (hook_ != nil)
311 [hook_ didCloseBook:self];
312 }
313
314 @end
315
316 @implementation RVPopUpBook
317
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)) {
321 leftButtonTitle = [[NSBundle mainBundle] localizedStringForKey:@"CANCEL" value:nil table:nil];
322 leftButtonStyle = UINavigationButtonStyleNormal;
323 }
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];
335 [super close];
336 }
337
338 @end