]>
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 | ||
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 | ||
facbc56d JF |
32 | - (UINavigationBar *) navigationBar { |
33 | return navbar_; | |
34 | } | |
35 | ||
7e9a36b6 JF |
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]; | |
facbc56d | 59 | CGSize navsize = [UINavigationBar defaultSize]; |
7e9a36b6 JF |
60 | CGRect navrect = {{0, 0}, navsize}; |
61 | ||
62 | navbar_ = [[UINavigationBar alloc] initWithFrame:navrect]; | |
63 | [self addSubview:navbar_]; | |
64 | ||
2388b078 | 65 | [navbar_ setBarStyle:0]; |
7e9a36b6 JF |
66 | [navbar_ setDelegate:self]; |
67 | ||
7e9a36b6 JF |
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; | |
facbc56d | 109 | [page setFrame:[transition_ bounds]]; |
7e9a36b6 JF |
110 | [transition_ transition:(animated ? 1 : 0) toView:page]; |
111 | [page setPageActive:YES]; | |
112 | ||
113 | [pages_ addObject:page]; | |
114 | [self reloadButtonsForPage:page]; | |
115 | ||
a9543575 | 116 | [navbar_ setAccessoryView:[page accessoryView] animate:animated removeOnPop:NO]; |
7e9a36b6 JF |
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 | ||
7e9a36b6 JF |
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]; | |
facbc56d | 152 | [page setFrame:[transition_ bounds]]; |
7e9a36b6 JF |
153 | [transition_ transition:(animated ? 2 : 0) toView:page]; |
154 | [page setPageActive:YES]; | |
155 | [self reloadButtonsForPage:page]; | |
c15d236a | 156 | |
a9543575 | 157 | [navbar_ setAccessoryView:[page accessoryView] animate:animated removeOnPop:NO]; |
7e9a36b6 JF |
158 | } |
159 | ||
3160e5f0 | 160 | - (void) setBackButtonTitle:(NSString *)title forPage:(RVPage *)page { |
7e9a36b6 JF |
161 | if ([pages_ count] == 0 || page != [pages_ lastObject]) |
162 | return; | |
163 | UINavigationItem *navitem = [navbar_ topItem]; | |
3160e5f0 | 164 | [navitem setBackButtonTitle:title]; |
7e9a36b6 JF |
165 | } |
166 | ||
3160e5f0 | 167 | - (void) reloadTitleForPage:(RVPage *)page { |
7e9a36b6 JF |
168 | if ([pages_ count] == 0 || page != [pages_ lastObject]) |
169 | return; | |
170 | UINavigationItem *navitem = [navbar_ topItem]; | |
3160e5f0 JF |
171 | NSString *title = [self getTitleForPage:page]; |
172 | [navitem setTitle:title]; | |
7e9a36b6 JF |
173 | } |
174 | ||
175 | - (void) reloadButtonsForPage:(RVPage *)page { | |
176 | if ([pages_ count] == 0 || page != [pages_ lastObject]) | |
177 | return; | |
7b0ce2da | 178 | NSString *leftButtonTitle([page leftButtonTitle]); |
facbc56d JF |
179 | UINavigationButtonStyle leftButtonStyle = [page leftButtonStyle]; |
180 | UINavigationButtonStyle rightButtonStyle = [page rightButtonStyle]; | |
7b0ce2da JF |
181 | //[navbar_ showButtonsWithLeftTitle:leftButtonTitle rightTitle:[page rightButtonTitle] leftBack:(leftButtonTitle == nil)]; |
182 | ||
183 | [navbar_ | |
184 | showLeftButton:leftButtonTitle | |
185 | withStyle:leftButtonStyle | |
186 | rightButton:[page rightButtonTitle] | |
187 | withStyle:rightButtonStyle | |
188 | ]; | |
7e9a36b6 JF |
189 | } |
190 | ||
191 | - (NSString *) getTitleForPage:(RVPage *)page { | |
192 | return [page title]; | |
193 | } | |
194 | ||
195 | - (void) reloadData { | |
196 | for (int i(0), e([pages_ count]); i != e; ++i) { | |
197 | RVPage *page([pages_ objectAtIndex:(e - i - 1)]); | |
198 | [page reloadData]; | |
199 | } | |
200 | } | |
201 | ||
202 | - (CGRect) pageBounds { | |
203 | return [transition_ bounds]; | |
204 | } | |
205 | ||
206 | @end |