diff options
author | bamse16 <marius@marius.me.uk> | 2009-04-11 09:14:42 +0000 |
---|---|---|
committer | bamse16 <marius@marius.me.uk> | 2009-04-11 09:14:42 +0000 |
commit | 1824ae6360c9ce1897e75404163d39df08ee5fbf (patch) | |
tree | 0eca1f6f4cb42e08f25e44a4683aecfb5881aac2 /Source/TableDocument.m | |
parent | 41f8cde09ff77996339cabc71517496976beee2e (diff) | |
download | sequelpro-1824ae6360c9ce1897e75404163d39df08ee5fbf.tar.gz sequelpro-1824ae6360c9ce1897e75404163d39df08ee5fbf.tar.bz2 sequelpro-1824ae6360c9ce1897e75404163d39df08ee5fbf.zip |
Added printing support via WebKit WebView
Diffstat (limited to 'Source/TableDocument.m')
-rw-r--r-- | Source/TableDocument.m | 122 |
1 files changed, 113 insertions, 9 deletions
diff --git a/Source/TableDocument.m b/Source/TableDocument.m index 19825570..49962fcc 100644 --- a/Source/TableDocument.m +++ b/Source/TableDocument.m @@ -42,6 +42,10 @@ #import "CMMCPConnection.h" #import "CMMCPResult.h" +//used for printing +#import "MGTemplateEngine.h" +#import "ICUTemplateMatcher.h" + NSString *TableDocumentFavoritesControllerSelectionIndexDidChange = @"TableDocumentFavoritesControllerSelectionIndexDidChange"; NSString *TableDocumentFavoritesControllerFavoritesDidChange = @"TableDocumentFavoritesControllerFavoritesDidChange"; @@ -60,7 +64,8 @@ NSString *TableDocumentFavoritesControllerFavoritesDidChange = @"TableDocum chooseDatabaseButton = nil; chooseDatabaseToolbarItem = nil; } - + printWebView = [[WebView alloc] init]; + [printWebView setFrameLoadDelegate:self]; return self; } @@ -97,14 +102,116 @@ NSString *TableDocumentFavoritesControllerFavoritesDidChange = @"TableDocum [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; } -- (NSPrintOperation *)printOperationWithSettings:(NSDictionary *)ps error:(NSError **)e -{ +- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame +{ + //because I need the webFrame loaded (for preview), I've moved the actuall printing here. NSPrintInfo *printInfo = [self printInfo]; [printInfo setHorizontalPagination:NSFitPagination]; [printInfo setVerticalPagination:NSAutoPagination]; - NSPrintOperation *printOp = [NSPrintOperation printOperationWithView:[[tableTabView selectedTabViewItem] view] printInfo:printInfo]; - return printOp; + [printInfo setVerticallyCentered:NO]; + [printInfo setTopMargin:30]; + [printInfo setBottomMargin:30]; + [printInfo setLeftMargin:10]; + [printInfo setRightMargin:10]; + + NSPrintOperation *op = [NSPrintOperation + printOperationWithView:[[[printWebView mainFrame] frameView] documentView] + printInfo:printInfo]; + + //add ability to select orientation to print panel + NSPrintPanel *printPanel = [op printPanel]; + [printPanel setOptions:[printPanel options] + NSPrintPanelShowsOrientation + NSPrintPanelShowsScaling]; + [op setPrintPanel:printPanel]; + + [op runOperationModalForWindow:tableWindow + delegate:self + didRunSelector: + @selector(printOperationDidRun:success:contextInfo:) + contextInfo:NULL]; + +} + +- (IBAction)printDocument:(id)sender +{ + //here load the printing document. The actual printing is done in the doneLoading delegate. + [[printWebView mainFrame] loadHTMLString:[self getHTMLforPrint] baseURL:nil]; +} + +- (void)printOperationDidRun:(NSPrintOperation *)printOperation + success:(BOOL)success + contextInfo:(void *)info +{ + //selector for print... maybe we can get rid of this? +} + +- (NSString *)getHTMLforPrint +{ + // Set up template engine with your chosen matcher. + MGTemplateEngine *engine = [MGTemplateEngine templateEngine]; + [engine setMatcher:[ICUTemplateMatcher matcherWithTemplateEngine:engine]]; + + NSString *versionForPrint = [NSString stringWithFormat:@"%@ %@ (build %@)", + [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"], + [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"], + [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"] + ]; + + NSMutableDictionary *connection = [[NSMutableDictionary alloc] init]; + if([[self user] length]) + [connection setValue:[self user] forKey:@"username"]; + [connection setValue:[self host] forKey:@"hostname"]; + if([[portField stringValue] length]) + [connection setValue:[portField stringValue] forKey:@"port"]; + [connection setValue:selectedDatabase forKey:@"database"]; + [connection setValue:versionForPrint forKey:@"version"]; + + NSArray *columns, *rows; + columns = rows = [[NSArray alloc] init]; + + if ( [tableTabView indexOfTabViewItem:[tableTabView selectedTabViewItem]] == 0 ){ + if([[tableSourceInstance tableStructureForPrint] count] > 0) + columns = [[NSArray alloc] initWithArray:[[tableSourceInstance tableStructureForPrint] objectAtIndex:0] copyItems:YES]; + if([[tableSourceInstance tableStructureForPrint] count] > 1) + rows = [[NSArray alloc] initWithArray: + [[tableSourceInstance tableStructureForPrint] objectsAtIndexes: + [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, [[tableSourceInstance tableStructureForPrint] count]-1)] + ] + ]; + } + else if ( [tableTabView indexOfTabViewItem:[tableTabView selectedTabViewItem]] == 1 ){ + if([[tableContentInstance currentResult] count] > 0) + columns = [[NSArray alloc] initWithArray:[[tableContentInstance currentResult] objectAtIndex:0] copyItems:YES]; + if([[tableContentInstance currentResult] count] > 1) + rows = [[NSArray alloc] initWithArray: + [[tableContentInstance currentResult] objectsAtIndexes: + [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, [[tableContentInstance currentResult] count]-1)] + ] + ]; + [connection setValue:[tableContentInstance usedQuery] forKey:@"query"]; + } + else if ( [tableTabView indexOfTabViewItem:[tableTabView selectedTabViewItem]] == 2 ){ + if([[customQueryInstance currentResult] count] > 0) + columns = [[NSArray alloc] initWithArray:[[customQueryInstance currentResult] objectAtIndex:0] copyItems:YES]; + if([[customQueryInstance currentResult] count] > 1) + rows = [[NSArray alloc] initWithArray: + [[customQueryInstance currentResult] objectsAtIndexes: + [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, [[customQueryInstance currentResult] count]-1)] + ] + ]; + [connection setValue:[customQueryInstance usedQuery] forKey:@"query"]; + } + + [engine setObject:connection forKey:@"c"]; + // Get path to template. + NSString *templatePath = [[NSBundle mainBundle] pathForResource:@"sequel-pro-print-template" ofType:@"html"]; + NSDictionary *print_data = [NSDictionary dictionaryWithObjectsAndKeys: + columns, @"columns", + rows, @"rows", + nil]; + // Process the template and display the results. + NSString *result = [engine processTemplateInFileAtPath:templatePath withVariables:print_data]; + return result; } - (CMMCPConnection *)sharedConnection @@ -1740,10 +1847,7 @@ NSString *TableDocumentFavoritesControllerFavoritesDidChange = @"TableDocum * Invoked when the document window is about to close */ - (void)windowWillClose:(NSNotification *)aNotification -{ - //reset print settings, so we're not prompted about saving them - [self setPrintInfo:[NSPrintInfo sharedPrintInfo]]; - +{ if ([mySQLConnection isConnected]) [self closeConnection]; if ([[[SPQueryConsole sharedQueryConsole] window] isVisible]) [self toggleConsole:self]; [[NSNotificationCenter defaultCenter] removeObserver:self]; |