]>
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 | ||
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 | - (void) navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button { | |
33 | _assert([pages_ count] != 0); | |
34 | RVPage *page = [pages_ lastObject]; | |
35 | switch (button) { | |
36 | case 0: [page _rightButtonClicked]; break; | |
37 | case 1: [page _leftButtonClicked]; break; | |
38 | } | |
39 | } | |
40 | ||
41 | - (void) navigationBar:(UINavigationBar *)navbar poppedItem:(UINavigationItem *)item { | |
42 | _assert([pages_ count] != 0); | |
43 | if (!resetting_) | |
44 | [[pages_ lastObject] setPageActive:NO]; | |
45 | [pages_ removeLastObject]; | |
46 | if (!resetting_) | |
47 | [self resetViewAnimated:YES toPage:[pages_ lastObject]]; | |
48 | } | |
49 | ||
50 | - (id) initWithFrame:(CGRect)frame { | |
51 | if ((self = [super initWithFrame:frame]) != nil) { | |
52 | pages_ = [[NSMutableArray arrayWithCapacity:4] retain]; | |
53 | ||
54 | struct CGRect bounds = [self bounds]; | |
55 | CGSize navsize = [UINavigationBar defaultSizeWithPrompt]; | |
56 | CGRect navrect = {{0, 0}, navsize}; | |
57 | ||
58 | navbar_ = [[UINavigationBar alloc] initWithFrame:navrect]; | |
59 | [self addSubview:navbar_]; | |
60 | ||
2a987aa5 | 61 | [navbar_ setBarStyle:0]; |
77f175ac JF |
62 | [navbar_ setDelegate:self]; |
63 | ||
64 | [navbar_ setPrompt:@""]; | |
65 | ||
66 | transition_ = [[UITransitionView alloc] initWithFrame:CGRectMake( | |
67 | bounds.origin.x, bounds.origin.y + navsize.height, bounds.size.width, bounds.size.height - navsize.height | |
68 | )]; | |
69 | ||
70 | [self addSubview:transition_]; | |
71 | } return self; | |
72 | } | |
73 | ||
74 | - (void) setDelegate:(id)delegate { | |
75 | delegate_ = delegate; | |
76 | } | |
77 | ||
78 | - (void) setPage:(RVPage *)page { | |
79 | if ([pages_ count] != 0) | |
80 | [[pages_ lastObject] setPageActive:NO]; | |
81 | ||
82 | [navbar_ disableAnimation]; | |
83 | resetting_ = true; | |
84 | for (unsigned i(0), pages([pages_ count]); i != pages; ++i) | |
85 | [navbar_ popNavigationItem]; | |
86 | resetting_ = false; | |
87 | ||
88 | [self pushPage:page]; | |
89 | [navbar_ enableAnimation]; | |
90 | } | |
91 | ||
92 | - (void) pushPage:(RVPage *)page { | |
93 | if ([pages_ count] != 0) | |
94 | [[pages_ lastObject] setPageActive:NO]; | |
95 | ||
96 | NSString *title = [self getTitleForPage:page]; | |
97 | ||
98 | NSString *backButtonTitle = [page backButtonTitle]; | |
99 | if (backButtonTitle == nil) | |
100 | backButtonTitle = title; | |
101 | ||
102 | UINavigationItem *navitem = [[[UINavigationItem alloc] initWithTitle:title] autorelease]; | |
103 | [navitem setBackButtonTitle:backButtonTitle]; | |
104 | [navbar_ pushNavigationItem:navitem]; | |
105 | ||
106 | BOOL animated = [pages_ count] == 0 ? NO : YES; | |
107 | [transition_ transition:(animated ? 1 : 0) toView:page]; | |
108 | [page setPageActive:YES]; | |
109 | ||
110 | [pages_ addObject:page]; | |
111 | [self reloadButtonsForPage:page]; | |
112 | ||
d210b85d | 113 | [navbar_ setAccessoryView:[page accessoryView] animate:animated removeOnPop:NO]; |
77f175ac JF |
114 | } |
115 | ||
116 | - (void) popPages:(unsigned)pages { | |
117 | if (pages == 0) | |
118 | return; | |
119 | ||
120 | [[pages_ lastObject] setPageActive:NO]; | |
121 | ||
122 | resetting_ = true; | |
123 | for (unsigned i(0); i != pages; ++i) | |
124 | [navbar_ popNavigationItem]; | |
125 | resetting_ = false; | |
126 | ||
127 | [self resetViewAnimated:YES toPage:[pages_ lastObject]]; | |
128 | } | |
129 | ||
130 | - (void) setPrompt:(NSString *)prompt { | |
131 | [navbar_ setPrompt:prompt]; | |
132 | } | |
133 | ||
134 | - (void) resetViewAnimated:(BOOL)animated { | |
135 | resetting_ = true; | |
136 | ||
137 | if ([pages_ count] > 1) { | |
138 | [navbar_ disableAnimation]; | |
139 | while ([pages_ count] != (animated ? 2 : 1)) | |
140 | [navbar_ popNavigationItem]; | |
141 | [navbar_ enableAnimation]; | |
142 | if (animated) | |
143 | [navbar_ popNavigationItem]; | |
144 | } | |
145 | ||
146 | resetting_ = false; | |
147 | ||
148 | [self resetViewAnimated:animated toPage:[pages_ lastObject]]; | |
149 | } | |
150 | ||
151 | - (void) resetViewAnimated:(BOOL)animated toPage:(RVPage *)page { | |
152 | [page resetViewAnimated:animated]; | |
153 | [transition_ transition:(animated ? 2 : 0) toView:page]; | |
154 | [page setPageActive:YES]; | |
155 | [self reloadButtonsForPage:page]; | |
62203f58 | 156 | |
d210b85d | 157 | [navbar_ setAccessoryView:[page accessoryView] animate:animated removeOnPop:NO]; |
77f175ac JF |
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; | |
faf4eb4f JF |
177 | NSString *leftButtonTitle([page leftButtonTitle]); |
178 | RVUINavBarButtonStyle leftButtonStyle = [page leftButtonStyle]; | |
179 | RVUINavBarButtonStyle 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 | ]; | |
77f175ac JF |
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 |