123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- //
- // CPDFListView.m
- // compdfkit-tools
- //
- // Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
- //
- // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
- // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
- // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
- // This notice may not be removed from this file.
- //
- #import "CPDFListView.h"
- #import "CPDFListView+Annotation.h"
- #import "CPDFListView+Private.h"
- #import "CPDFColorUtils.h"
- #import "CPDFSlider.h"
- #import "CPDFPageIndicatorView.h"
- NSNotificationName const CPDFListViewToolModeChangeNotification = @"CPDFListViewToolModeChangeNotification";
- NSNotificationName const CPDFListViewAnnotationModeChangeNotification = @"CPDFListViewAnnotationModeChangeNotification";
- NSNotificationName const CPDFListViewActiveAnnotationsChangeNotification = @"CPDFListViewActiveAnnotationsChangeNotification";
- @interface CPDFListView()
- @property (nonatomic, strong) CPDFPageIndicatorView * pageIndicatorView;
- @end
- @implementation CPDFListView
- #pragma mark - Initializers
- - (instancetype)initWithFrame:(CGRect)frame {
- if (self = [super initWithFrame:frame]) {
- self.backgroundColor = [UIColor lightGrayColor];
-
- [self commomInit];
-
- [self addNotification];
-
- }
- return self;
- }
- - (id)initWithCoder:(NSCoder *)decoder {
- self = [super initWithCoder:decoder];
- if (self) {
- self.backgroundColor = [UIColor lightGrayColor];
- [self commomInit];
- [self addNotification];
- }
- return self;
- }
- #pragma mark - Accessors
- - (void)setAnnotationMode:(CPDFViewAnnotationMode)annotationMode {
- _annotationMode = annotationMode;
-
- if (CPDFViewAnnotationModeHighlight == annotationMode ||
- CPDFViewAnnotationModeUnderline == annotationMode ||
- CPDFViewAnnotationModeStrikeout == annotationMode ||
- CPDFViewAnnotationModeSquiggly == annotationMode) {
- self.textSelectionMode = YES;
- } else {
- self.textSelectionMode = NO;
- }
-
- if (CPDFViewAnnotationModeLink == annotationMode) {
- self.scrollEnabled = NO;
- [self endDrawing];
- } else if (CPDFViewAnnotationModeInk == annotationMode) {
- self.scrollEnabled = NO;
- [self beginDrawing];
- } else {
- if (self.activeAnnotation) {
- self.scrollEnabled = NO;
- } else {
- self.scrollEnabled = YES;
- }
- [self endDrawing];
- [self becomeFirstResponder];
- }
-
- if (CPDFViewAnnotationModeNone != annotationMode) {
- CPDFPage *page = self.activeAnnotation.page;
- [self updateActiveAnnotations:@[]];
- [self setNeedsDisplayForPage:page];
- }
-
- dispatch_async(dispatch_get_main_queue(), ^{
- [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewActiveAnnotationsChangeNotification object:self];
-
- if([self.performDelegate respondsToSelector:@selector(PDFListViewChangedAnnotationType:forAnnotationMode:)]) {
- [self.performDelegate PDFListViewChangedAnnotationType:self forAnnotationMode:self.annotationMode];
- }
- });
- }
- -(void)setToolModel:(CToolModel)toolModel {
- _toolModel = toolModel;
-
- dispatch_async(dispatch_get_main_queue(), ^{
- [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewToolModeChangeNotification object:self];
-
- if([self.performDelegate respondsToSelector:@selector(PDFListViewChangedToolMode:forToolMode:)]) {
- [self.performDelegate PDFListViewChangedToolMode:self forToolMode:self.toolModel];
- }
- });
- }
- - (NSMutableArray *)activeAnnotations {
- if(!_activeAnnotations) {
- _activeAnnotations = [[NSMutableArray alloc] init];
- }
- return _activeAnnotations;
- }
- - (CPDFAnnotation *)activeAnnotation {
- return self.activeAnnotations.firstObject;
- }
- #pragma mark - Touch
- - (void)touchBeganAtPoint:(CGPoint)point forPage:(CPDFPage *)page {
- if (CToolModelAnnotation == self.toolModel) {
- [self annotationTouchBeganAtPoint:point forPage:page];
- } else if (CToolModelForm == self.toolModel) {
-
- } else if (CToolModelEdit == self.toolModel) {
-
- } else {
- }
- }
- - (void)touchMovedAtPoint:(CGPoint)point forPage:(CPDFPage *)page {
- if (CToolModelAnnotation == self.toolModel) {
- [self annotationTouchMovedAtPoint:point forPage:page];
- } else if (CToolModelForm == self.toolModel) {
-
- } else if (CToolModelEdit == self.toolModel) {
-
- } else {
- }
-
- }
- - (void)touchEndedAtPoint:(CGPoint)point forPage:(CPDFPage *)page {
- if (CToolModelAnnotation == self.toolModel) {
- [self annotationTouchEndedAtPoint:point forPage:page];
- } else if (CToolModelForm == self.toolModel) {
-
- } else if (CToolModelEdit == self.toolModel) {
-
- } else {
- }
- }
- - (void)touchCancelledAtPoint:(CGPoint)point forPage:(CPDFPage *)page {
- if (CToolModelAnnotation == self.toolModel) {
- [self annotationTouchCancelledAtPoint:point forPage:page];
- } else if (CToolModelForm == self.toolModel) {
-
- } else if (CToolModelEdit == self.toolModel) {
-
- } else {
- }
- }
- - (NSArray<UIMenuItem *> *)menuItemsAtPoint:(CGPoint)point forPage:(CPDFPage *)page {
- if([self.performDelegate respondsToSelector:@selector(PDFListView:customizeMenuForPage:forPagePoint:)])
- return [self.performDelegate PDFListView:self customizeMenuForPage:page forPagePoint:point];
- return [super menuItemsAtPoint:point forPage:page];
- }
- #pragma mark - Private method
- - (void)commomInit {
- _pageSliderView = [[CPDFSlider alloc] initWithPDFView:self];
- _pageSliderView.frame = CGRectMake(self.bounds.size.width-22, 0, 22, self.bounds.size.height);
- _pageSliderView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin;
- [self addSubview:self.pageSliderView];
-
- _pageIndicatorView = [[CPDFPageIndicatorView alloc] init];
-
- __weak typeof(self) weakSelf = self;
- _pageIndicatorView.touchCallBack = ^{
- UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:NSLocalizedString(@"Enter a page number", nil) preferredStyle:UIAlertControllerStyleAlert];
-
- [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
-
- }]];
-
- [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
-
- UITextField *pageTextField = alertController.textFields.firstObject;
- NSInteger pageIndex = [pageTextField.text integerValue] - 1;
-
- if (pageIndex > weakSelf.document.pageCount){
- pageIndex = weakSelf.document.pageCount;
- } else if(pageIndex<0){
- pageIndex = 0;
- } else if(pageTextField.text.length == 0){
- pageIndex = (int)weakSelf.currentPageIndex;
- }
-
- [weakSelf goToPageIndex:pageIndex animated:YES];
- }]];
-
- [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
- NSString *str = [NSString stringWithFormat:@"Page(1/%lu)", weakSelf.document.pageCount];
- textField.placeholder = NSLocalizedString(str, nil);
- textField.keyboardType = UIKeyboardTypeNumberPad;
-
- }];
-
- UIViewController *tRootViewControl = [UIApplication sharedApplication].keyWindow.rootViewController;
- if ([tRootViewControl presentedViewController]) {
- tRootViewControl = [tRootViewControl presentedViewController];
- }
-
- [tRootViewControl presentViewController:alertController animated:true completion:nil];
- };
- }
- - (void)addNotification {
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(documentChangedNotification:) name:CPDFViewDocumentChangedNotification object:nil];
-
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pageChangedNotification:) name:CPDFViewPageChangedNotification object:nil];
- }
- - (void)showPageNumIndicator {
- __weak typeof(self) weakSelf = self;
- if(![self.pageIndicatorView superview])
- [self.pageIndicatorView showInView:weakSelf position:CPDFPageIndicatorViewPositionLeftBottom];
- [self.pageIndicatorView updatePageCount:weakSelf.document.pageCount currentPageIndex:self.currentPageIndex + 1];
- }
- - (NSString *)annotationUserName {
- NSString *annotationUserName = CPDFKitShareConfig.annotationAuthor;
- if (!annotationUserName || [annotationUserName length] <= 0) {
- annotationUserName = [[UIDevice currentDevice] name];
- }
- return annotationUserName ? : @"";
- }
- - (void)updateActiveAnnotations:(NSArray <CPDFAnnotation *> *)activeAnnotations {
- if(activeAnnotations) {
- self.activeAnnotations = [NSMutableArray arrayWithArray:activeAnnotations];
-
- dispatch_async(dispatch_get_main_queue(), ^{
- [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewActiveAnnotationsChangeNotification object:self];
-
- if([self.performDelegate respondsToSelector:@selector(PDFListViewChangeatioActiveAnnotations:forActiveAnnotations:)]) {
- [self.performDelegate PDFListViewChangeatioActiveAnnotations:self forActiveAnnotations:self.activeAnnotations];
- }
- });
- } else if (!activeAnnotations && self.activeAnnotations.count > 0) {
- [self.activeAnnotations removeAllObjects];
-
- dispatch_async(dispatch_get_main_queue(), ^{
- [[NSNotificationCenter defaultCenter] postNotificationName:CPDFListViewActiveAnnotationsChangeNotification object:self];
-
- if([self.performDelegate respondsToSelector:@selector(PDFListViewChangeatioActiveAnnotations:forActiveAnnotations:)]) {
- [self.performDelegate PDFListViewChangeatioActiveAnnotations:self forActiveAnnotations:self.activeAnnotations];
- }
- });
- }
- }
- #pragma mark - NotificationCenter
- - (void)documentChangedNotification:(NSNotification *)notification {
- CPDFView *pdfview = notification.object;
- if (pdfview.document == self.document) {
- [self showPageNumIndicator];
- [self.pageSliderView reloadData];
- }
- }
- - (void)pageChangedNotification:(NSNotification *)notification {
- CPDFView *pdfview = notification.object;
- if (pdfview.document == self.document) {
- [self showPageNumIndicator];
- [self.pageSliderView reloadData];
- }
- }
- #pragma mark - Rendering
- - (void)drawPage:(CPDFPage *)page toContext:(CGContextRef)context {
-
- if (CToolModelAnnotation == self.toolModel) {
- [self annotationDrawPage:page toContext:context];
- } else if (CToolModelForm == self.toolModel) {
-
- } else if (CToolModelEdit == self.toolModel) {
-
- } else {
- }
- }
- @end
|