Source

CocoaPDF / MyPDFWinController.mm

Full commit
//
//  MyPDFWinController.m
//  cocoapdf
//
//  Created by Quincy Liu on 7/7/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "MyPDFWinController.h"

@implementation MyPDFWinController

@synthesize myPdfDoc = _myPdfDoc;

#pragma mark    NSWindowController functions

- (id)init {
    self = [super initWithWindowNibName:@"MyPDFDocWin"];
    if (self) {
        _myPdfDoc = nil;
    }
    return self;
}

- (void)dealloc 
{
    // No more notifications.
	[[NSNotificationCenter defaultCenter] removeObserver: self];
    
    //do not dealloc _myPdfDoc, since it's just a reference
    [super dealloc];
}

- (void)setDocument:(NSDocument *)document 
{
    [super setDocument:document];
    
    if ([document isKindOfClass:[MyPDFDoc class]])
        _myPdfDoc = (MyPDFDoc*) document;
    
    [self setupDocumentNotifications];
}

- (void) windowDidLoad
{
    [super windowDidLoad];
    
    [_tronPDFView setDocument:_myPdfDoc];
    
    //[_pdfView setDocument:_myPdfDoc.pdfDoc];
    /*
    pdftron::PDF::PDFView * pdfView = [_tronPDFView getPDFView];

    if (pdfView) {
        //NSSize contentSize = [_tronPDFView contentSize];
        pdfView->SetDoc(*(_myPdfDoc.tronPDFDoc));
        pdfView->OnSize(300, 300);
    }
     */
    //    [_tronPDFView setDocument:_myPdfDoc];
    
    //initialize toolbar
    /*

    [self handlePageChange:nil];
    */
    
    /*
	PDFDocument	*pdfDoc;
	//NSRect		visibleScreen;
//	NSSize		pageSize;
//	float		scaleFactor;
	
	// Create PDFDocument.
	pdfDoc = [[PDFDocument alloc] initWithURL: [NSURL fileURLWithPath: [[self document] fileName]]];
	
    NSLog(@"pdfdoc %@", [pdfDoc description]);
    NSLog(@"pdfview %@", [_pdfView description]);
	// Set document.
	[_pdfView setDocument: pdfDoc];
	[pdfDoc release];
    */
	/*
	// Default display mode.
	[_pdfView setAutoScales: YES];
	[_pdfView setDisplaysPageBreaks: NO];
    
	// Get outline (if any).
	_outline = [[[_pdfView document] outlineRoot] retain];
	if (_outline)
	{
		if ([[_pdfView document] isLocked] == NO)
		{
			[_outlineView reloadData];
			[_outlineView setAutoresizesOutlineColumn: NO];
			
			// Expand items.
			if ([_outlineView numberOfRows] == 1)
				[_outlineView expandItem: [_outlineView itemAtRow: 0] expandChildren: NO];
			[self updateOutlineSelection];
			
			// Always open drawer if there is an outline and unencrypted PDF.
			[[[[self window] drawers] objectAtIndex: 0] open];
		}
	}
	
	// How big to create the window?
	// Visible frame for main screen.
	visibleScreen = [[NSScreen mainScreen] visibleFrame];
	
	// Taking into account the toolbars, etc. in the UI.
	visibleScreen.size.width -= kPDFViewXDelta;
	visibleScreen.size.height -= kPDFViewYDelta;
    
	// If continuous and multi-page, subtract space for a vertical scrollbar.
	if ((([_pdfView displayMode] & 0x01) == 0x01) && ([[_pdfView document] pageCount] > 1))
		visibleScreen.size.width -= [NSScroller scrollerWidth];
	
	// Page size.
	pageSize = [_pdfView rowSizeForPage: [_pdfView currentPage]];
	
	// Determine limiting scale factor.
	scaleFactor = visibleScreen.size.width / pageSize.width;
	if (visibleScreen.size.height / pageSize.height < scaleFactor)
		scaleFactor = visibleScreen.size.height / pageSize.height;
	
	// Scale bounds.
	pageSize.width = floorf(pageSize.width * scaleFactor);
	pageSize.height = floorf(pageSize.height * scaleFactor);
	
	// Set the window size.
	[[self window] setContentSize: pageSize];
	
	// Close the search results.
	[self setSearchResultsViewHeight: 0];
	
	// Create toolbar.
	[self setupToolbarForWindow: [self window]];
	
	// Internal notification.
	[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(newActiveAnnotation:) 
                                                 name: @"newActiveAnnotation" object: _pdfView];
	
	// Establish notifications for this document.
	[self setupDocumentNotifications];
	
	// State.
	[self updateLinkTools];
    */
}

- (void) setupDocumentNotifications 
{
    /*
    // Document saving progress notifications.
	[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(documentBeginWrite:) 
                                                 name: @"PDFDidBeginDocumentWrite" object: [_pdfView document]];
	[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(documentEndWrite:) 
                                                 name: @"PDFDidEndDocumentWrite" object: [_pdfView document]];
     
	[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(documentEndPageWrite:) 
    
    */
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handlePageChange:) 
                                                 name:TronPDFViewPageChangedNotification
                                               object:_tronPDFView];
    /*
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(handleScrollPositionChange:) 
                                                 name:TronPDFViewScrollChangedNotification
                                               object:_tronPDFView];
     */
}

#pragma mark    actions

- (IBAction)doGoToNextPage:(id)sender
{
    NSLog(@"in doGoToNextPage");
    [_tronPDFView goToNextPage:sender];
    //[_pdfScrollView setScroll:0.4 verticalPosition:0.5];
    //[_pdfView goToNextPage:sender];
    
}

- (IBAction)doGoToPreviousPage:(id)sender
{
    NSLog(@"in doGoToPreviousPage");
    [_tronPDFView goToPreviousPage:sender];
    //[_pdfView goToPreviousPage:sender];
}

- (IBAction)doGoToPagePanel:(id)sender
{
    NSLog(@"in doGoToPagePanel...");    
    /*
    // Specify page range.
	[_goTopagePanelRangeLabel setStringValue: [NSString stringWithFormat:  
                                            NSLocalizedString(@"Go to page (%@ to %@):", NULL), [[[_pdfView document] pageAtIndex: 0] label], 
                                            [[[_pdfView document] pageAtIndex: [[_pdfView document] pageCount] - 1] label]]];
	
	// Populate initially with current page label.
	[_goToPagePanelPageLabel setStringValue: [[_pdfView currentPage] label]];
	
	// Bring up the page number panel as a sheet.
	[NSApp beginSheet: _goToPagePanel modalForWindow: [self window] modalDelegate: self 
       didEndSelector: @selector(goToPagePanelDidEnd: returnCode: contextInfo:) contextInfo: NULL];
     */
}

- (IBAction)doGoToPageNumberEntered:(id)sender
{
    /*
    int index = [self getIndexFromLabel:[_goToPagePanelPageLabel stringValue] pdfDoc:[_pdfView document]];
    [NSApp endSheet: _goToPagePanel returnCode: index];
     */
}

- (IBAction)doGoToPagePanelCancel:(id)sender
{
    [NSApp endSheet: _goToPagePanel returnCode: 0];
}

- (void) goToPagePanelDidEnd: (NSWindow *) sheet returnCode: (int) returnCode contextInfo: (void *) contextInfo
{
    /*
	[_goToPagePanel close];
	
	// Make sure page number entered is valid.
	if ((returnCode < 1) || (returnCode > [[_pdfView document] pageCount]))
	{
		// Zero may indicate user canceled, don't beep in that case.
		if (returnCode != 0)
			NSBeep();
		
		return;
	}
	
	// Go to that page.
	[_pdfView goToPage: [[_pdfView document] pageAtIndex: returnCode - 1]];
     */
}

- (IBAction)removeCurrentPage:(id)sender
{
    /*
    PDFDocument * pdfDoc = _myPdfDoc.pdfDoc;
    PDFPage * current_page = [_pdfView currentPage];
    [pdfDoc removePageAtIndex:[pdfDoc indexForPage:current_page]];
    [_pdfView setNeedsDisplay:YES];
    [_myPdfDoc updateChangeCount:NSChangeDone];
     */
}

- (IBAction)goToSpecifiedPage:(id)sender
{
    /*
    NSLog(@"goToSpecifiedPage");
    NSString * pages = [_pagesTextField stringValue];
    int page_num = [self getIndexFromLabel:pages pdfDoc:[_myPdfDoc pdfDoc]];
    [_pdfView goToPage:[[_pdfView document] pageAtIndex:page_num - 1]];
     */
}

#pragma mark notification handlers
- (void)handlePageChange:(NSNotification*) notification
{
    [_pagesTextField setStringValue:[NSString stringWithFormat:@"%d", 
                                     [_tronPDFView currPageNum]]];
 
/*    
    [_previousNextPageSegCtrl setEnabled:[_pdfView canGoToPreviousPage] forSegment:0];
    [_previousNextPageSegCtrl setEnabled:[_pdfView canGoToNextPage] forSegment:1];
     */
}

/*
- (void)handleScrollPositionChange:(NSNotification*) notification
{
    [_pdfScrollView setScroll:[_tronPDFView currentHorizontalScrollPosition] 
             verticalPosition:[_tronPDFView currentVerticalScrollPosition]];
}
*/
- (IBAction)goPreviousNextPage:(id)sender
{
    NSLog(@"goPreviousNextPage");
    NSInteger seg_index = [_previousNextPageSegCtrl selectedSegment];
    if (seg_index == 0) {
        //[_tronPDFView goToPreviousPage:sender];
        [self doGoToPreviousPage:sender];
    }
    else if (seg_index == 1) {
        //[_tronPDFView goToNextPage:sender];
        [self doGoToNextPage:sender];
    }
}

- (IBAction)doZoom:(id)sender
{
    NSLog(@"doZoom");
    NSInteger seg_index = [_zoomSegCtrl selectedSegment];
    if (seg_index == 0) {
        //[_pdfView zoomOut:sender];
    } 
    else if (seg_index == 1) {
        //[_pdfView zoomIn:sender];
    }
}

#pragma mark -------- save progress
// --------------------------------------------------------------------------------------------------- documentBeginWrite

- (void) documentBeginWrite: (NSNotification *) notification
{
	// Establish maximum and current value for progress bar.
	//[_saveProgressBar setMaxValue: (double)[[_pdfView document] pageCount]];
	[_saveProgressBar setDoubleValue: 0.0];
	
	// Bring up the save panel as a sheet.
	[NSApp beginSheet: _saveProgressPanel modalForWindow: [self window] modalDelegate: self 
       didEndSelector: @selector(saveProgressSheetDidEnd: returnCode: contextInfo:) contextInfo: NULL];
}

// ----------------------------------------------------------------------------------------------------- documentEndWrite

- (void) documentEndWrite: (NSNotification *) notification
{
	[NSApp endSheet: _saveProgressPanel];
}

// ------------------------------------------------------------------------------------------------- documentEndPageWrite

- (void) documentEndPageWrite: (NSNotification *) notification
{
	[_saveProgressBar setDoubleValue: [[[notification userInfo] objectForKey: @"PDFDocumentPageIndex"] floatValue]];
	[_saveProgressBar displayIfNeeded];
}

// ---------------------------------------------------------------------------------------------- saveProgressSheetDidEnd

- (void) saveProgressSheetDidEnd: (NSWindow *) sheet returnCode: (int) returnCode contextInfo: (void *) contextInfo
{
	[_saveProgressPanel close];
}

#pragma mark    utilities

- (int) getIndexFromLabel:(NSString *)label pdfDoc: (PDFDocument *) doc 
{
	int			index = -1;
	NSInteger	count;
	int			i;
	
	// Handle empty string.
	if ([label length] < 1)
		return index;
	
	// Walk through all pages and compare the page label against 'label'.
	count = [doc pageCount];
	for (i = 0; i < count; i++)
	{
		if ([[[doc pageAtIndex: i] label] isEqualToString: label])
		{
			index = i + 1;
			break;
		}
	}
	
	return index;
}

@end