blob: a6f53181c35ab8e8b026428c7b16317a1949d061 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
#import "RVBook.h"
#import <UIKit/UINavigationBar.h>
#import <UIKit/UINavigationItem.h>
#import <UIKit/UITransitionView.h>
#import <UIKit/UIView-Geometry.h>
#import <UIKit/UIView-Hierarchy.h>
#import <Foundation/Foundation.h>
#import <CoreGraphics/CGGeometry.h>
#include <cstdio>
#include <cstdlib>
#include <errno.h>
#import "RVPage.h"
@interface NSObject (UICaboodleRVBook)
- (float) widthForButtonContents:(float)width;
@end
@implementation NSObject (UICaboodleRVBook)
- (float) widthForButtonContents:(float)width {
return width;
}
@end
@interface UIImage (UICaboodleRVBook)
- (float) widthForButtonContents:(float)width;
@end
@implementation UIImage (UICaboodleRVBook)
- (float) widthForButtonContents:(float)width {
return [self size].width + 8;
}
@end
@interface RVNavigationBar : UINavigationBar {
}
- (id) createButtonWithContents:(id)contents width:(float)width barStyle:(int)barStyle buttonStyle:(int)style isRight:(BOOL)right;
@end
@implementation RVNavigationBar
- (id) createButtonWithContents:(id)contents width:(float)width barStyle:(int)barStyle buttonStyle:(int)style isRight:(BOOL)right {
float adjust = [contents widthForButtonContents:width];
NSLog(@"cc:%@:%g:%g", contents, width, adjust);
width = adjust;
return [super createButtonWithContents:contents width:width barStyle:barStyle buttonStyle:style isRight:right];
}
@end
@implementation RVBook
- (void) dealloc {
[navbar_ setDelegate:nil];
[pages_ release];
[navbar_ release];
[transition_ release];
[super dealloc];
}
- (UINavigationBar *) navigationBar {
return navbar_;
}
- (void) navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button {
_assert([pages_ count] != 0);
RVPage *page = [pages_ lastObject];
switch (button) {
case 0: [page _rightButtonClicked]; break;
case 1: [page _leftButtonClicked]; break;
}
}
- (void) navigationBar:(UINavigationBar *)navbar poppedItem:(UINavigationItem *)item {
_assert([pages_ count] != 0);
if (!resetting_)
[[pages_ lastObject] setPageActive:NO];
[pages_ removeLastObject];
if (!resetting_)
[self resetViewAnimated:YES toPage:[pages_ lastObject]];
}
- (id) initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame]) != nil) {
pages_ = [[NSMutableArray arrayWithCapacity:4] retain];
struct CGRect bounds = [self bounds];
CGSize navsize = [UINavigationBar defaultSize];
CGRect navrect = {{0, 0}, navsize};
navbar_ = [[RVNavigationBar alloc] initWithFrame:navrect];
[self addSubview:navbar_];
[navbar_ setBarStyle:0];
[navbar_ setDelegate:self];
transition_ = [[UITransitionView alloc] initWithFrame:CGRectMake(
bounds.origin.x, bounds.origin.y + navsize.height, bounds.size.width, bounds.size.height - navsize.height
)];
[self addSubview:transition_];
} return self;
}
- (void) setDelegate:(id)delegate {
delegate_ = delegate;
}
- (void) setPage:(RVPage *)page {
if ([pages_ count] != 0)
[[pages_ lastObject] setPageActive:NO];
[navbar_ disableAnimation];
resetting_ = true;
for (unsigned i(0), pages([pages_ count]); i != pages; ++i)
[navbar_ popNavigationItem];
resetting_ = false;
[self pushPage:page];
[navbar_ enableAnimation];
}
- (void) pushPage:(RVPage *)page {
if ([pages_ count] != 0)
[[pages_ lastObject] setPageActive:NO];
NSString *title = [self getTitleForPage:page];
NSString *backButtonTitle = [page backButtonTitle];
if (backButtonTitle == nil)
backButtonTitle = title;
UINavigationItem *navitem = [[[UINavigationItem alloc] initWithTitle:title] autorelease];
[navitem setBackButtonTitle:backButtonTitle];
[navbar_ pushNavigationItem:navitem];
BOOL animated = [pages_ count] == 0 ? NO : YES;
[page setFrame:[transition_ bounds]];
[transition_ transition:(animated ? 1 : 0) toView:page];
[page setPageActive:YES];
[pages_ addObject:page];
[self reloadButtonsForPage:page];
[navbar_ setAccessoryView:[page accessoryView] animate:animated removeOnPop:NO];
}
- (void) popPages:(unsigned)pages {
if (pages == 0)
return;
[[pages_ lastObject] setPageActive:NO];
resetting_ = true;
for (unsigned i(0); i != pages; ++i)
[navbar_ popNavigationItem];
resetting_ = false;
[self resetViewAnimated:YES toPage:[pages_ lastObject]];
}
- (void) resetViewAnimated:(BOOL)animated {
resetting_ = true;
if ([pages_ count] > 1) {
[navbar_ disableAnimation];
while ([pages_ count] != (animated ? 2 : 1))
[navbar_ popNavigationItem];
[navbar_ enableAnimation];
if (animated)
[navbar_ popNavigationItem];
}
resetting_ = false;
[self resetViewAnimated:animated toPage:[pages_ lastObject]];
}
- (void) resetViewAnimated:(BOOL)animated toPage:(RVPage *)page {
[page resetViewAnimated:animated];
[page setFrame:[transition_ bounds]];
[transition_ transition:(animated ? 2 : 0) toView:page];
[page setPageActive:YES];
[self reloadButtonsForPage:page];
[navbar_ setAccessoryView:[page accessoryView] animate:animated removeOnPop:NO];
}
- (void) setBackButtonTitle:(NSString *)title forPage:(RVPage *)page {
if ([pages_ count] == 0 || page != [pages_ lastObject])
return;
UINavigationItem *navitem = [navbar_ topItem];
[navitem setBackButtonTitle:title];
}
- (void) reloadTitleForPage:(RVPage *)page {
if ([pages_ count] == 0 || page != [pages_ lastObject])
return;
UINavigationItem *navitem = [navbar_ topItem];
NSString *title = [self getTitleForPage:page];
[navitem setTitle:title];
}
- (void) reloadButtonsForPage:(RVPage *)page {
if ([pages_ count] == 0 || page != [pages_ lastObject])
return;
NSString *leftButtonTitle([page leftButtonTitle]);
UINavigationButtonStyle leftButtonStyle = [page leftButtonStyle];
UINavigationButtonStyle rightButtonStyle = [page rightButtonStyle];
//[navbar_ showButtonsWithLeftTitle:leftButtonTitle rightTitle:[page rightButtonTitle] leftBack:(leftButtonTitle == nil)];
[navbar_
showLeftButton:leftButtonTitle
withStyle:leftButtonStyle
rightButton:[page rightButtonTitle]
withStyle:rightButtonStyle
];
}
- (NSString *) getTitleForPage:(RVPage *)page {
return [page title];
}
- (void) reloadData {
for (int i(0), e([pages_ count]); i != e; ++i) {
RVPage *page([pages_ objectAtIndex:(e - i - 1)]);
[page reloadData];
}
}
- (CGRect) pageBounds {
return [transition_ bounds];
}
@end
|