forked from vfr/Reader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReaderMainToolbar.m
More file actions
331 lines (249 loc) · 11.1 KB
/
ReaderMainToolbar.m
File metadata and controls
331 lines (249 loc) · 11.1 KB
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
319
320
321
322
323
324
325
326
327
328
329
330
331
//
// ReaderMainToolbar.m
// Reader v2.6.2
//
// Created by Julius Oklamcak on 2011-07-01.
// Copyright © 2011-2013 Julius Oklamcak. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to
// do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#import "ReaderConstants.h"
#import "ReaderMainToolbar.h"
#import "ReaderDocument.h"
#import <MessageUI/MessageUI.h>
@implementation ReaderMainToolbar
{
UIButton *markButton;
UIImage *markImageN;
UIImage *markImageY;
}
#pragma mark Constants
#define BUTTON_X 8.0f
#define BUTTON_Y 8.0f
#define BUTTON_SPACE 8.0f
#define BUTTON_HEIGHT 30.0f
#define DONE_BUTTON_WIDTH 56.0f
#define THUMBS_BUTTON_WIDTH 40.0f
#define PRINT_BUTTON_WIDTH 40.0f
#define EMAIL_BUTTON_WIDTH 40.0f
#define MARK_BUTTON_WIDTH 40.0f
#define TITLE_HEIGHT 28.0f
#pragma mark Properties
@synthesize delegate;
#pragma mark ReaderMainToolbar instance methods
- (id)initWithFrame:(CGRect)frame
{
return [self initWithFrame:frame document:nil];
}
- (id)initWithFrame:(CGRect)frame document:(ReaderDocument *)object
{
assert(object != nil); // Must have a valid ReaderDocument
if ((self = [super initWithFrame:frame]))
{
CGFloat viewWidth = self.bounds.size.width;
UIImage *imageH = [UIImage imageNamed:@"Reader-Button-H"];
UIImage *imageN = [UIImage imageNamed:@"Reader-Button-N"];
UIImage *buttonH = [imageH stretchableImageWithLeftCapWidth:5 topCapHeight:0];
UIImage *buttonN = [imageN stretchableImageWithLeftCapWidth:5 topCapHeight:0];
CGFloat titleX = BUTTON_X; CGFloat titleWidth = (viewWidth - (titleX + titleX));
CGFloat leftButtonX = BUTTON_X; // Left button start X position
#if (READER_STANDALONE == FALSE) // Option
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(leftButtonX, BUTTON_Y, DONE_BUTTON_WIDTH, BUTTON_HEIGHT);
[doneButton setTitle:NSLocalizedString(@"Done", @"button") forState:UIControlStateNormal];
[doneButton setTitleColor:[UIColor colorWithWhite:0.0f alpha:1.0f] forState:UIControlStateNormal];
[doneButton setTitleColor:[UIColor colorWithWhite:1.0f alpha:1.0f] forState:UIControlStateHighlighted];
[doneButton addTarget:self action:@selector(doneButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[doneButton setBackgroundImage:buttonH forState:UIControlStateHighlighted];
[doneButton setBackgroundImage:buttonN forState:UIControlStateNormal];
doneButton.titleLabel.font = [UIFont systemFontOfSize:14.0f];
doneButton.autoresizingMask = UIViewAutoresizingNone;
doneButton.exclusiveTouch = YES;
[self addSubview:doneButton]; leftButtonX += (DONE_BUTTON_WIDTH + BUTTON_SPACE);
titleX += (DONE_BUTTON_WIDTH + BUTTON_SPACE); titleWidth -= (DONE_BUTTON_WIDTH + BUTTON_SPACE);
#endif // end of READER_STANDALONE Option
#if (READER_ENABLE_THUMBS == TRUE) // Option
UIButton *thumbsButton = [UIButton buttonWithType:UIButtonTypeCustom];
thumbsButton.frame = CGRectMake(leftButtonX, BUTTON_Y, THUMBS_BUTTON_WIDTH, BUTTON_HEIGHT);
[thumbsButton setImage:[UIImage imageNamed:@"Reader-Thumbs"] forState:UIControlStateNormal];
[thumbsButton addTarget:self action:@selector(thumbsButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[thumbsButton setBackgroundImage:buttonH forState:UIControlStateHighlighted];
[thumbsButton setBackgroundImage:buttonN forState:UIControlStateNormal];
thumbsButton.autoresizingMask = UIViewAutoresizingNone;
thumbsButton.exclusiveTouch = YES;
[self addSubview:thumbsButton]; //leftButtonX += (THUMBS_BUTTON_WIDTH + BUTTON_SPACE);
titleX += (THUMBS_BUTTON_WIDTH + BUTTON_SPACE); titleWidth -= (THUMBS_BUTTON_WIDTH + BUTTON_SPACE);
#endif // end of READER_ENABLE_THUMBS Option
#if (READER_BOOKMARKS == TRUE || READER_ENABLE_MAIL == TRUE || READER_ENABLE_PRINT == TRUE)
CGFloat rightButtonX = viewWidth; // Right button start X position
#endif // end of READER_BOOKMARKS || READER_ENABLE_MAIL || READER_ENABLE_PRINT Options
#if (READER_BOOKMARKS == TRUE) // Option
rightButtonX -= (MARK_BUTTON_WIDTH + BUTTON_SPACE);
UIButton *flagButton = [UIButton buttonWithType:UIButtonTypeCustom];
flagButton.frame = CGRectMake(rightButtonX, BUTTON_Y, MARK_BUTTON_WIDTH, BUTTON_HEIGHT);
//[flagButton setImage:[UIImage imageNamed:@"Reader-Mark-N"] forState:UIControlStateNormal];
[flagButton addTarget:self action:@selector(markButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[flagButton setBackgroundImage:buttonH forState:UIControlStateHighlighted];
[flagButton setBackgroundImage:buttonN forState:UIControlStateNormal];
flagButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
flagButton.exclusiveTouch = YES;
[self addSubview:flagButton]; titleWidth -= (MARK_BUTTON_WIDTH + BUTTON_SPACE);
markButton = flagButton; markButton.enabled = NO; markButton.tag = NSIntegerMin;
markImageN = [UIImage imageNamed:@"Reader-Mark-N"]; // N image
markImageY = [UIImage imageNamed:@"Reader-Mark-Y"]; // Y image
#endif // end of READER_BOOKMARKS Option
#if (READER_ENABLE_MAIL == TRUE) // Option
if ([MFMailComposeViewController canSendMail] == YES) // Can email
{
unsigned long long fileSize = [object.fileSize unsignedLongLongValue];
if (fileSize < (unsigned long long)15728640) // Check attachment size limit (15MB)
{
rightButtonX -= (EMAIL_BUTTON_WIDTH + BUTTON_SPACE);
UIButton *emailButton = [UIButton buttonWithType:UIButtonTypeCustom];
emailButton.frame = CGRectMake(rightButtonX, BUTTON_Y, EMAIL_BUTTON_WIDTH, BUTTON_HEIGHT);
[emailButton setImage:[UIImage imageNamed:@"Reader-Email"] forState:UIControlStateNormal];
[emailButton addTarget:self action:@selector(emailButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[emailButton setBackgroundImage:buttonH forState:UIControlStateHighlighted];
[emailButton setBackgroundImage:buttonN forState:UIControlStateNormal];
emailButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
emailButton.exclusiveTouch = YES;
[self addSubview:emailButton]; titleWidth -= (EMAIL_BUTTON_WIDTH + BUTTON_SPACE);
}
}
#endif // end of READER_ENABLE_MAIL Option
#if (READER_ENABLE_PRINT == TRUE) // Option
if (object.password == nil) // We can only print documents without passwords
{
Class printInteractionController = NSClassFromString(@"UIPrintInteractionController");
if ((printInteractionController != nil) && [printInteractionController isPrintingAvailable])
{
rightButtonX -= (PRINT_BUTTON_WIDTH + BUTTON_SPACE);
UIButton *printButton = [UIButton buttonWithType:UIButtonTypeCustom];
printButton.frame = CGRectMake(rightButtonX, BUTTON_Y, PRINT_BUTTON_WIDTH, BUTTON_HEIGHT);
[printButton setImage:[UIImage imageNamed:@"Reader-Print"] forState:UIControlStateNormal];
[printButton addTarget:self action:@selector(printButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[printButton setBackgroundImage:buttonH forState:UIControlStateHighlighted];
[printButton setBackgroundImage:buttonN forState:UIControlStateNormal];
printButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
printButton.exclusiveTouch = YES;
[self addSubview:printButton]; titleWidth -= (PRINT_BUTTON_WIDTH + BUTTON_SPACE);
}
}
#endif // end of READER_ENABLE_PRINT Option
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad)
{
CGRect titleRect = CGRectMake(titleX, BUTTON_Y, titleWidth, TITLE_HEIGHT);
UILabel *titleLabel = [[UILabel alloc] initWithFrame:titleRect];
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.font = [UIFont systemFontOfSize:19.0f];
titleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
titleLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
titleLabel.textColor = [UIColor colorWithWhite:0.0f alpha:1.0f];
titleLabel.shadowColor = [UIColor colorWithWhite:0.65f alpha:1.0f];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.shadowOffset = CGSizeMake(0.0f, 1.0f);
titleLabel.adjustsFontSizeToFitWidth = YES;
titleLabel.minimumScaleFactor = 0.75f;
titleLabel.text = [object.fileName stringByDeletingPathExtension];
[self addSubview:titleLabel];
}
}
return self;
}
- (void)setBookmarkState:(BOOL)state
{
#if (READER_BOOKMARKS == TRUE) // Option
if (state != markButton.tag) // Only if different state
{
if (self.hidden == NO) // Only if toolbar is visible
{
UIImage *image = (state ? markImageY : markImageN);
[markButton setImage:image forState:UIControlStateNormal];
}
markButton.tag = state; // Update bookmarked state tag
}
if (markButton.enabled == NO) markButton.enabled = YES;
#endif // end of READER_BOOKMARKS Option
}
- (void)updateBookmarkImage
{
#if (READER_BOOKMARKS == TRUE) // Option
if (markButton.tag != NSIntegerMin) // Valid tag
{
BOOL state = markButton.tag; // Bookmarked state
UIImage *image = (state ? markImageY : markImageN);
[markButton setImage:image forState:UIControlStateNormal];
}
if (markButton.enabled == NO) markButton.enabled = YES;
#endif // end of READER_BOOKMARKS Option
}
- (void)hideToolbar
{
if (self.hidden == NO)
{
[UIView animateWithDuration:0.25 delay:0.0
options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
animations:^(void)
{
self.alpha = 0.0f;
}
completion:^(BOOL finished)
{
self.hidden = YES;
}
];
}
}
- (void)showToolbar
{
if (self.hidden == YES)
{
[self updateBookmarkImage]; // First
[UIView animateWithDuration:0.25 delay:0.0
options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
animations:^(void)
{
self.hidden = NO;
self.alpha = 1.0f;
}
completion:NULL
];
}
}
#pragma mark UIButton action methods
- (void)doneButtonTapped:(UIButton *)button
{
[delegate tappedInToolbar:self doneButton:button];
}
- (void)thumbsButtonTapped:(UIButton *)button
{
[delegate tappedInToolbar:self thumbsButton:button];
}
- (void)printButtonTapped:(UIButton *)button
{
[delegate tappedInToolbar:self printButton:button];
}
- (void)emailButtonTapped:(UIButton *)button
{
[delegate tappedInToolbar:self emailButton:button];
}
- (void)markButtonTapped:(UIButton *)button
{
[delegate tappedInToolbar:self markButton:button];
}
@end