]> git.saurik.com Git - cydia.git/blob - UICaboodle/RVBook.mm
Massive stability and feature improvements to Cydia.
[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 @implementation RVBook
22
23 - (void) dealloc {
24 [navbar_ setDelegate:nil];
25
26 [pages_ release];
27 [navbar_ release];
28 [transition_ release];
29 [super dealloc];
30 }
31
32 - (UINavigationBar *) navigationBar {
33 return navbar_;
34 }
35
36 - (void) navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button {
37 _assert([pages_ count] != 0);
38 RVPage *page = [pages_ lastObject];
39 switch (button) {
40 case 0: [page _rightButtonClicked]; break;
41 case 1: [page _leftButtonClicked]; break;
42 }
43 }
44
45 - (void) navigationBar:(UINavigationBar *)navbar poppedItem:(UINavigationItem *)item {
46 _assert([pages_ count] != 0);
47 if (!resetting_)
48 [[pages_ lastObject] setPageActive:NO];
49 [pages_ removeLastObject];
50 if (!resetting_)
51 [self resetViewAnimated:YES toPage:[pages_ lastObject]];
52 }
53
54 - (id) initWithFrame:(CGRect)frame {
55 if ((self = [super initWithFrame:frame]) != nil) {
56 pages_ = [[NSMutableArray arrayWithCapacity:4] retain];
57
58 struct CGRect bounds = [self bounds];
59 CGSize navsize = [UINavigationBar defaultSize];
60 CGRect navrect = {{0, 0}, navsize};
61
62 navbar_ = [[UINavigationBar alloc] initWithFrame:navrect];
63 [self addSubview:navbar_];
64
65 [navbar_ setBarStyle:0];
66 [navbar_ setDelegate:self];
67
68 transition_ = [[UITransitionView alloc] initWithFrame:CGRectMake(
69 bounds.origin.x, bounds.origin.y + navsize.height, bounds.size.width, bounds.size.height - navsize.height
70 )];
71
72 [self addSubview:transition_];
73 } return self;
74 }
75
76 - (void) setDelegate:(id)delegate {
77 delegate_ = delegate;
78 }
79
80 - (void) setPage:(RVPage *)page {
81 if ([pages_ count] != 0)
82 [[pages_ lastObject] setPageActive:NO];
83
84 [navbar_ disableAnimation];
85 resetting_ = true;
86 for (unsigned i(0), pages([pages_ count]); i != pages; ++i)
87 [navbar_ popNavigationItem];
88 resetting_ = false;
89
90 [self pushPage:page];
91 [navbar_ enableAnimation];
92 }
93
94 - (void) pushPage:(RVPage *)page {
95 if ([pages_ count] != 0)
96 [[pages_ lastObject] setPageActive:NO];
97
98 NSString *title = [self getTitleForPage:page];
99
100 NSString *backButtonTitle = [page backButtonTitle];
101 if (backButtonTitle == nil)
102 backButtonTitle = title;
103
104 UINavigationItem *navitem = [[[UINavigationItem alloc] initWithTitle:title] autorelease];
105 [navitem setBackButtonTitle:backButtonTitle];
106 [navbar_ pushNavigationItem:navitem];
107
108 BOOL animated = [pages_ count] == 0 ? NO : YES;
109 [page setFrame:[transition_ bounds]];
110 [transition_ transition:(animated ? 1 : 0) toView:page];
111 [page setPageActive:YES];
112
113 [pages_ addObject:page];
114 [self reloadButtonsForPage:page];
115
116 [navbar_ setAccessoryView:[page accessoryView] animate:animated removeOnPop:NO];
117 }
118
119 - (void) popPages:(unsigned)pages {
120 if (pages == 0)
121 return;
122
123 [[pages_ lastObject] setPageActive:NO];
124
125 resetting_ = true;
126 for (unsigned i(0); i != pages; ++i)
127 [navbar_ popNavigationItem];
128 resetting_ = false;
129
130 [self resetViewAnimated:YES toPage:[pages_ lastObject]];
131 }
132
133 - (void) resetViewAnimated:(BOOL)animated {
134 resetting_ = true;
135
136 if ([pages_ count] > 1) {
137 [navbar_ disableAnimation];
138 while ([pages_ count] != (animated ? 2 : 1))
139 [navbar_ popNavigationItem];
140 [navbar_ enableAnimation];
141 if (animated)
142 [navbar_ popNavigationItem];
143 }
144
145 resetting_ = false;
146
147 [self resetViewAnimated:animated toPage:[pages_ lastObject]];
148 }
149
150 - (void) resetViewAnimated:(BOOL)animated toPage:(RVPage *)page {
151 [page resetViewAnimated:animated];
152 [page setFrame:[transition_ bounds]];
153 [transition_ transition:(animated ? 2 : 0) toView:page];
154 [page setPageActive:YES];
155 [self reloadButtonsForPage:page];
156
157 [navbar_ setAccessoryView:[page accessoryView] animate:animated removeOnPop:NO];
158 }
159
160 - (void) setTitle:(NSString *)title forPage:(RVPage *)page {
161 if ([pages_ count] == 0 || page != [pages_ lastObject])
162 return;
163 UINavigationItem *navitem = [navbar_ topItem];
164 [navitem setTitle:title];
165 }
166
167 - (void) setBackButtonTitle:(NSString *)title forPage:(RVPage *)page {
168 if ([pages_ count] == 0 || page != [pages_ lastObject])
169 return;
170 UINavigationItem *navitem = [navbar_ topItem];
171 [navitem setBackButtonTitle:title];
172 }
173
174 - (void) reloadButtonsForPage:(RVPage *)page {
175 if ([pages_ count] == 0 || page != [pages_ lastObject])
176 return;
177 NSString *leftButtonTitle([page leftButtonTitle]);
178 UINavigationButtonStyle leftButtonStyle = [page leftButtonStyle];
179 UINavigationButtonStyle rightButtonStyle = [page rightButtonStyle];
180 //[navbar_ showButtonsWithLeftTitle:leftButtonTitle rightTitle:[page rightButtonTitle] leftBack:(leftButtonTitle == nil)];
181
182 [navbar_
183 showLeftButton:leftButtonTitle
184 withStyle:leftButtonStyle
185 rightButton:[page rightButtonTitle]
186 withStyle:rightButtonStyle
187 ];
188 }
189
190 - (NSString *) getTitleForPage:(RVPage *)page {
191 return [page title];
192 }
193
194 - (void) reloadData {
195 for (int i(0), e([pages_ count]); i != e; ++i) {
196 RVPage *page([pages_ objectAtIndex:(e - i - 1)]);
197 [page reloadData];
198 }
199 }
200
201 - (CGRect) pageBounds {
202 return [transition_ bounds];
203 }
204
205 @end