blob: 29e9513bf2ce9ab2e804476374a47d633f61163a (
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
#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];
width = adjust;
return [super createButtonWithContents:contents width:width barStyle:barStyle buttonStyle:style isRight:right];
}
@end
@implementation RVBook
- (void) dealloc {
[navbar_ setDelegate:nil];
if (toolbar_ != nil)
[toolbar_ setDelegate:nil];
[pages_ release];
[navbar_ release];
[transition_ release];
if (toolbar_ != nil)
[toolbar_ 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) setHook:(id)hook {
hook_ = hook;
}
- (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) swapPage:(RVPage *)page {
if ([pages_ count] == 0)
return [self pushPage:page];
[[pages_ lastObject] setPageActive:NO];
[navbar_ disableAnimation];
resetting_ = true;
[navbar_ popNavigationItem];
resetting_ = false;
[self pushPage:page animated:NO];
[navbar_ enableAnimation];
}
- (void) pushPage:(RVPage *)page animated:(BOOL)animated {
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];
[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) pushPage:(RVPage *)page {
if ([pages_ count] != 0)
[[pages_ lastObject] setPageActive:NO];
[self pushPage:page animated:([pages_ count] == 0 ? NO : YES)];
}
- (void) pushBook:(RVBook *)book {
[delegate_ popUpBook:book];
}
- (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) _leftButtonTitle:(NSString *&)leftButtonTitle style:(UINavigationButtonStyle &)leftButtonStyle forPage:(RVPage *)page {
leftButtonTitle = [page leftButtonTitle];
leftButtonStyle = [page leftButtonStyle];
}
- (void) reloadButtonsForPage:(RVPage *)page {
if ([pages_ count] == 0 || page != [pages_ lastObject])
return;
NSString *leftButtonTitle;
UINavigationButtonStyle leftButtonStyle;
[self _leftButtonTitle:leftButtonTitle style:leftButtonStyle forPage:page];
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 {
size_t count([pages_ count]);
for (size_t i(0); i != count; ++i) {
RVPage *page([pages_ objectAtIndex:(count - i - 1)]);
[page reloadData];
}
if (count != 0) {
RVPage *page([pages_ lastObject]);
[self reloadButtonsForPage:page];
}
}
- (CGRect) pageBounds {
return [transition_ bounds];
}
- (void) close {
if (hook_ != nil)
[hook_ didCloseBook:self];
}
@end
@implementation RVPopUpBook
- (void) _leftButtonTitle:(NSString *&)leftButtonTitle style:(UINavigationButtonStyle &)leftButtonStyle forPage:(RVPage *)page {
[super _leftButtonTitle:leftButtonTitle style:leftButtonStyle forPage:page];
if ((cancel_ = leftButtonTitle == nil && [pages_ count] == 1)) {
leftButtonTitle = [[NSBundle mainBundle] localizedStringForKey:@"CANCEL" value:nil table:nil];
leftButtonStyle = UINavigationButtonStyleNormal;
}
}
- (void) navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button {
if (button == 1 && cancel_)
[self close];
else
[super navigationBar:navbar buttonClicked:button];
}
- (void) close {
[self popFromSuperviewAnimated:YES];
[super close];
}
@end
|