Tests:
authorsullivan <sullivan@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 29 Jan 2004 02:16:33 +0000 (02:16 +0000)
committersullivan <sullivan@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 29 Jan 2004 02:16:33 +0000 (02:16 +0000)
        Reviewed by Dave.

        * Blot/blot-tasks.txt:
        Mentioned NSTextView's (and therefore Mail's) option-Escape behavior here
        since we will probably have to implement it also; clarified a comment.

WebCore:

        Reviewed by Dave.

        * khtml/rendering/render_text.cpp:
        (RenderText::paintObject):
        Increment s in the while() clause so it is incremented even
        after a continue statement. This fixes an infinite loop I ran
        into in the printing code path at certain scaled print sizes
        on certain pages.

WebKit:

        More header/footer work: refactored the header/footer code so it could
        be easily reused by other WebDocument classes; used it from WebImageView
        and WebTextView; removed the page count parameters because it's possible
        (though currently nasty, see 3543078) to determine this in the client.

        Reviewed by Dave.

        * Misc.subproj/WebNSPrintOperationExtras.h Added.
        * Misc.subproj/WebNSPrintOperationExtras.m Added.
        (-[NSPrintOperation _web_pageSetupScaleFactor]):
        new convenience method.

        * WebView.subproj/WebUIDelegatePrivate.h:
        Removed page index and page count parameters from delegate methods.

        * WebView.subproj/WebViewPrivate.h:
        New private category for header/footer printing methods so that different
        WebDocument methods can share almost all of the code.

        * WebView.subproj/WebView.m:
        (-[WebView _headerHeight]):
        (-[WebView _footerHeight]):
        (-[WebView _drawHeaderInRect:]):
        (-[WebView _drawFooterInRect:]):
        (-[WebView _adjustPrintingMarginsForHeaderAndFooter]):
        (-[WebView _drawHeaderAndFooter]):
        Moved all of these methods here, formerly in WebHTMLView. Removed the
        page index and page count parameters.

        * WebView.subproj/WebHTMLView.m:
        Removed all the header/footer code that's now in WebView.m, and the
        method that's now -[NSPrintOperation _web_pageSetupScaleFactor]
        (-[WebHTMLView _setPrinting:minimumPageWidth:maximumPageWidth:adjustViewSize:]):
        call methods differently that have now been moved
        (-[WebHTMLView _scaleFactorForPrintOperation:]):
        ditto
        (-[WebHTMLView knowsPageRange:]):
        ditto
        (-[WebHTMLView drawPageBorderWithSize:]):
        now just turns around and calls -[WebView _drawHeaderAndFooter]

        * WebView.subproj/WebImageView.m:
        (-[WebImageView drawPageBorderWithSize:]):
        new method, just calls -[WebView _drawHeaderAndFooter]
        (-[WebImageView beginDocument]):
        now calls -[WebView _adjustPrintMarginsForHeaderAndFooter], also moved in file.
        (-[WebImageView endDocument]):
        just moved in file.

        * WebView.subproj/WebTextView.m:
        (-[WebTextView drawPageBorderWithSize:]):
        new method, just calls -[WebView _drawHeaderAndFooter]
        (-[WebTextView knowsPageRange:]):
        overridden to call -[WebView _adjustPrintMarginsForHeaderAndFooter]

        * WebKit.pbproj/project.pbxproj:
        updated for added files

WebBrowser:

        Some header/footer cleanup.

        Reviewed by Dave.

        * BrowserWebViewPrinting.m:
        (-[BrowserWebView webView:drawHeaderInRect:]):
        Removed obsolete page index and count parameters.
        Use displayName rather than currentTitle because it works for all
        document types (currentTitle didn't work for plain text pages).
        (-[BrowserWebView webView:drawFooterInRect:]):
        Removed obsolete page index and count parameters.
        Did some AppKit hackery to get the total document page count in a way that
        works for any print operation. Now handles the case where the page count
        is unknown (by printing "Page <x>" instead of "Page <x> of <n>"). Also added
        a 1 pixel offset to the footer's baseline to work around AppKit bug where
        the descenders in the footer were getting slightly clipped at some scale
        factors.

        * English.lproj/Localizable.strings:
        updated for these changes

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@6004 268f45cc-cd09-0410-ab3c-d52691b4dbfc

12 files changed:
WebCore/ChangeLog-2005-08-23
WebCore/khtml/rendering/render_text.cpp
WebKit/ChangeLog
WebKit/Misc.subproj/WebNSPrintOperationExtras.h [new file with mode: 0644]
WebKit/Misc.subproj/WebNSPrintOperationExtras.m [new file with mode: 0644]
WebKit/WebKit.pbproj/project.pbxproj
WebKit/WebView.subproj/WebHTMLView.m
WebKit/WebView.subproj/WebImageView.m
WebKit/WebView.subproj/WebTextView.m
WebKit/WebView.subproj/WebUIDelegatePrivate.h
WebKit/WebView.subproj/WebView.m
WebKit/WebView.subproj/WebViewPrivate.h

index b3b1bcf32642944a20bc1d89719d658979a50404..f3f1cf4bb56a2609b33195ef1bc4f1e9d79a3430 100644 (file)
@@ -1,3 +1,14 @@
+2004-01-28  John Sullivan  <sullivan@apple.com>
+
+        Reviewed by Dave.
+
+        * khtml/rendering/render_text.cpp:
+        (RenderText::paintObject):
+        Increment s in the while() clause so it is incremented even
+        after a continue statement. This fixes an infinite loop I ran
+        into in the printing code path at certain scaled print sizes
+        on certain pages.
+
 2004-01-28  Chris Blumenberg  <cblu@apple.com>
 
        Fixed: <rdar://problem/3543041>: when pasting an image, cursor should be after the image, not before
index 59d89262dc7946cde2fa831536ad647eeff0771d..49535af5e0978fdff67dc96ce4dc98d0c0b868ad 100644 (file)
@@ -776,9 +776,7 @@ void RenderText::paintObject(QPainter *p, int /*x*/, int y, int /*w*/, int h,
         }
 #endif
 
-        s = s->nextTextBox();
-        
-    } while (s && s->checkVerticalPoint(y, ty, h));
+    } while (((s = s->nextTextBox()) != 0) && s->checkVerticalPoint(y, ty, h));
 
 #if APPLE_CHANGES
     } // end of for loop
index dc814e8d7fd5777aa7601846fdaa0454aee3ea0e..320373d2c68cf26bf9f18ae23d441188ef2b6909 100644 (file)
@@ -1,3 +1,64 @@
+2004-01-28  John Sullivan  <sullivan@apple.com>
+
+        More header/footer work: refactored the header/footer code so it could
+        be easily reused by other WebDocument classes; used it from WebImageView
+        and WebTextView; removed the page count parameters because it's possible
+        (though currently nasty, see 3543078) to determine this in the client.
+
+        Reviewed by Dave.
+
+        * Misc.subproj/WebNSPrintOperationExtras.h Added.
+        * Misc.subproj/WebNSPrintOperationExtras.m Added.
+        (-[NSPrintOperation _web_pageSetupScaleFactor]):
+        new convenience method.
+                
+        * WebView.subproj/WebUIDelegatePrivate.h:
+        Removed page index and page count parameters from delegate methods.
+        
+        * WebView.subproj/WebViewPrivate.h:
+        New private category for header/footer printing methods so that different
+        WebDocument methods can share almost all of the code.
+        
+        * WebView.subproj/WebView.m:
+        (-[WebView _headerHeight]):
+        (-[WebView _footerHeight]):
+        (-[WebView _drawHeaderInRect:]):
+        (-[WebView _drawFooterInRect:]):
+        (-[WebView _adjustPrintingMarginsForHeaderAndFooter]):
+        (-[WebView _drawHeaderAndFooter]):
+        Moved all of these methods here, formerly in WebHTMLView. Removed the
+        page index and page count parameters.
+
+        * WebView.subproj/WebHTMLView.m:
+        Removed all the header/footer code that's now in WebView.m, and the
+        method that's now -[NSPrintOperation _web_pageSetupScaleFactor]
+        (-[WebHTMLView _setPrinting:minimumPageWidth:maximumPageWidth:adjustViewSize:]):
+        call methods differently that have now been moved
+        (-[WebHTMLView _scaleFactorForPrintOperation:]):
+        ditto
+        (-[WebHTMLView knowsPageRange:]):
+        ditto
+        (-[WebHTMLView drawPageBorderWithSize:]):
+        now just turns around and calls -[WebView _drawHeaderAndFooter]
+        
+        * WebView.subproj/WebImageView.m:
+        (-[WebImageView drawPageBorderWithSize:]):
+        new method, just calls -[WebView _drawHeaderAndFooter]
+        (-[WebImageView beginDocument]):
+        now calls -[WebView _adjustPrintMarginsForHeaderAndFooter], also moved in file.
+        (-[WebImageView endDocument]):
+        just moved in file.
+
+        * WebView.subproj/WebTextView.m:
+        (-[WebTextView drawPageBorderWithSize:]):
+        new method, just calls -[WebView _drawHeaderAndFooter]        
+        (-[WebTextView knowsPageRange:]):
+        overridden to call -[WebView _adjustPrintMarginsForHeaderAndFooter]
+        
+        * WebKit.pbproj/project.pbxproj:
+        updated for added files
+
+
 2004-01-28  Chris Blumenberg  <cblu@apple.com>
 
        Fixed: <rdar://problem/3197222>: need context menu items for back, forward, refresh.
diff --git a/WebKit/Misc.subproj/WebNSPrintOperationExtras.h b/WebKit/Misc.subproj/WebNSPrintOperationExtras.h
new file mode 100644 (file)
index 0000000..903a726
--- /dev/null
@@ -0,0 +1,16 @@
+//
+//  WebNSPrintOperationExtras.h
+//  WebKit
+//
+//  Created by John Sullivan on Wed Jan 28 2004.
+//  Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
+//
+
+#import <Cocoa/Cocoa.h>
+
+
+@interface NSPrintOperation (WebKitExtras)
+
+- (float)_web_pageSetupScaleFactor;
+
+@end
diff --git a/WebKit/Misc.subproj/WebNSPrintOperationExtras.m b/WebKit/Misc.subproj/WebNSPrintOperationExtras.m
new file mode 100644 (file)
index 0000000..39ddfad
--- /dev/null
@@ -0,0 +1,19 @@
+//
+//  WebNSPrintOperationExtras.m
+//  WebKit
+//
+//  Created by John Sullivan on Wed Jan 28 2004.
+//  Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
+//
+
+#import "WebNSPrintOperationExtras.h"
+
+
+@implementation NSPrintOperation (WebKitExtras)
+
+- (float)_web_pageSetupScaleFactor
+{
+    return [[[[self printInfo] dictionary] objectForKey:NSPrintScalingFactor] floatValue];
+}
+
+@end
index e2e3a2a6388ad02ce12c288322184543c69562c1..3e38696ff1cdf428a4f2d57639926b8fefeb3bf6 100644 (file)
                                BE887C01056D3A6E009BB3E7,
                                84723BE5056D719E0044BFEA,
                                830E81850585375700AD0891,
+                               EDD1A5C805C83987008E3150,
                        );
                        isa = PBXHeadersBuildPhase;
                        runOnlyForDeploymentPostprocessing = 0;
                                BE887C02056D3A6E009BB3E7,
                                84723BE6056D719E0044BFEA,
                                830E81860585375700AD0891,
+                               EDD1A5C905C83987008E3150,
                        );
                        isa = PBXSourcesBuildPhase;
                        runOnlyForDeploymentPostprocessing = 0;
                                8398847B03426FB000BC5F5E,
                                ED2B2474033A2DA800C1A526,
                                ED2B2475033A2DA800C1A526,
+                               EDD1A5C605C83987008E3150,
+                               EDD1A5C705C83987008E3150,
                                BE6DC39904C62C4E004D0EF6,
                                BE6DC39A04C62C4E004D0EF6,
                                F508946902B71D59018A9CD4,
                        settings = {
                        };
                };
+               EDD1A5C605C83987008E3150 = {
+                       fileEncoding = 4;
+                       isa = PBXFileReference;
+                       lastKnownFileType = sourcecode.c.h;
+                       path = WebNSPrintOperationExtras.h;
+                       refType = 4;
+                       sourceTree = "<group>";
+               };
+               EDD1A5C705C83987008E3150 = {
+                       fileEncoding = 4;
+                       isa = PBXFileReference;
+                       lastKnownFileType = sourcecode.c.objc;
+                       path = WebNSPrintOperationExtras.m;
+                       refType = 4;
+                       sourceTree = "<group>";
+               };
+               EDD1A5C805C83987008E3150 = {
+                       fileRef = EDD1A5C605C83987008E3150;
+                       isa = PBXBuildFile;
+                       settings = {
+                       };
+               };
+               EDD1A5C905C83987008E3150 = {
+                       fileRef = EDD1A5C705C83987008E3150;
+                       isa = PBXBuildFile;
+                       settings = {
+                       };
+               };
 //ED0
 //ED1
 //ED2
index 06e445b456bd9941b57c3e094acdb3c7770e1427..97b305e3dfc1d3ca1f62c5565f5998050dac15dc 100644 (file)
@@ -18,6 +18,7 @@
 #import <WebKit/WebNetscapePluginEmbeddedView.h>
 #import <WebKit/WebKitLogging.h>
 #import <WebKit/WebNSPasteboardExtras.h>
+#import "WebNSPrintOperationExtras.h"
 #import <WebKit/WebNSViewExtras.h>
 #import <WebKit/WebPluginController.h>
 #import <WebKit/WebTextRenderer.h>
@@ -83,10 +84,7 @@ static BOOL forceRealHitTest = NO;
 - (void)_setPrinting:(BOOL)printing minimumPageWidth:(float)minPageWidth maximumPageWidth:(float)maxPageWidth adjustViewSize:(BOOL)adjustViewSize;
 - (void)_updateTextSizeMultiplier;
 - (float)_calculatePrintHeight;
-- (float)_footerHeight;
-- (float)_headerHeight;
 - (float)_scaleFactorForPrintOperation:(NSPrintOperation *)printOperation;
-- (float)_userScaleFactorForPrintOperation:(NSPrintOperation *)printOperation;
 @end
 
 // Any non-zero value will do, but using somethign recognizable might help us debug some day.
@@ -1676,95 +1674,6 @@ static WebHTMLView *lastHitView = nil;
 {
 }
 
-//#define DEBUG_HEADER_AND_FOOTER
-
-- (float)_headerHeight
-{
-    // FIXME: headers and footers are only drawn for HTMLView, not for other views
-    // such as plain text or standalone images
-    if ([[[self _webView] UIDelegate] respondsToSelector:@selector(webViewHeaderHeight:)]) {
-        return [[[self _webView] UIDelegate] webViewHeaderHeight:[self _webView]];
-    }
-
-#ifdef DEBUG_HEADER_AND_FOOTER
-    return 25;
-#else
-    return 0;
-#endif
-}
-
-- (float)_footerHeight
-{
-    // FIXME: headers and footers are only drawn for HTMLView, not for other views
-    // such as plain text or standalone images
-    if ([[[self _webView] UIDelegate] respondsToSelector:@selector(webViewFooterHeight:)]) {
-        return [[[self _webView] UIDelegate] webViewFooterHeight:[self _webView]];
-    }
-    
-#ifdef DEBUG_HEADER_AND_FOOTER
-    return 50;
-#else
-    return 0;
-#endif
-}
-
-- (void)_drawHeaderInRect:(NSRect)rect
-{
-#ifdef DEBUG_HEADER_AND_FOOTER
-    NSGraphicsContext *currentContext = [NSGraphicsContext currentContext];
-    [currentContext saveGraphicsState];
-    [[NSColor yellowColor] set];
-    NSRectFill(rect);
-    [currentContext restoreGraphicsState];
-#endif
-    
-    // FIXME: headers and footers are only drawn for HTMLView, not for other views
-    // such as plain text or standalone images
-    if ([[[self _webView] UIDelegate] respondsToSelector:@selector(webView:drawHeaderInRect:forPage:of:)]) {
-        NSGraphicsContext *currentContext = [NSGraphicsContext currentContext];
-        [currentContext saveGraphicsState];
-        NSRectClip(rect);
-        [[[self _webView] UIDelegate] webView:[self _webView] 
-                             drawHeaderInRect:rect 
-                                      forPage:[[NSPrintOperation currentOperation] currentPage] 
-                                           of:[_private->pageRects count]];
-        [currentContext restoreGraphicsState];
-    }
-}
-
-- (void)_drawFooterInRect:(NSRect)rect
-{
-#ifdef DEBUG_HEADER_AND_FOOTER
-    NSGraphicsContext *currentContext = [NSGraphicsContext currentContext];
-    [currentContext saveGraphicsState];
-    [[NSColor cyanColor] set];
-    NSRectFill(rect);
-    [currentContext restoreGraphicsState];
-#endif
-    
-    // FIXME: headers and footers are only drawn for HTMLView, not for other views
-    // such as plain text or standalone images
-    if ([[[self _webView] UIDelegate] respondsToSelector:@selector(webView:drawFooterInRect:forPage:of:)]) {
-        NSGraphicsContext *currentContext = [NSGraphicsContext currentContext];
-        [currentContext saveGraphicsState];
-        NSRectClip(rect);
-        [[[self _webView] UIDelegate] webView:[self _webView] 
-                             drawFooterInRect:rect 
-                                      forPage:[[NSPrintOperation currentOperation] currentPage] 
-                                           of:[_private->pageRects count]];
-        [currentContext restoreGraphicsState];
-    }
-}
-
-- (void)_adjustPrintingMarginsForHeaderAndFooter
-{
-    NSPrintOperation *operation = [NSPrintOperation currentOperation];
-    NSPrintInfo *info = [[NSPrintOperation currentOperation] printInfo];
-    float userScale = [self _userScaleFactorForPrintOperation:operation];
-    [info setTopMargin:[info topMargin] + [self _headerHeight]*userScale];
-    [info setBottomMargin:[info bottomMargin] + [self _footerHeight]*userScale];
-}
-
 // Does setNeedsDisplay:NO as a side effect when printing is ending.
 // pageWidth != 0 implies we will relayout to a new width
 - (void)_setPrinting:(BOOL)printing minimumPageWidth:(float)minPageWidth maximumPageWidth:(float)maxPageWidth adjustViewSize:(BOOL)adjustViewSize
@@ -1789,7 +1698,7 @@ static WebHTMLView *lastHitView = nil;
         [self setNeedsLayout:YES];
         [self layoutToMinimumPageWidth:minPageWidth maximumPageWidth:maxPageWidth adjustingViewSize:adjustViewSize];
         if (printing) {
-            [self _adjustPrintingMarginsForHeaderAndFooter];
+            [[self _webView] _adjustPrintingMarginsForHeaderAndFooter];
         } else {
             // Can't do this when starting printing or nested printing won't work, see 3491427.
             [self setNeedsDisplay:NO];
@@ -1821,11 +1730,6 @@ static WebHTMLView *lastHitView = nil;
     return [printInfo paperSize].width - [printInfo leftMargin] - [printInfo rightMargin];
 }
 
-- (float)_userScaleFactorForPrintOperation:(NSPrintOperation *)printOperation
-{
-       return [[[[printOperation printInfo] dictionary] objectForKey:NSPrintScalingFactor] floatValue];
-}
-
 - (float)_scaleFactorForPrintOperation:(NSPrintOperation *)printOperation
 {
     float viewWidth = NSWidth([self bounds]);
@@ -1834,7 +1738,7 @@ static WebHTMLView *lastHitView = nil;
         return 1.0;
     }
        
-    float userScaleFactor = [self _userScaleFactorForPrintOperation:printOperation];
+    float userScaleFactor = [printOperation _web_pageSetupScaleFactor];
     float maxShrinkToFitScaleFactor = 1/PrintingMaximumShrinkFactor;
     float shrinkToFitScaleFactor = [self _availablePaperWidthForPrintOperation:printOperation]/viewWidth;
     return userScaleFactor * MAX(maxShrinkToFitScaleFactor, shrinkToFitScaleFactor);
@@ -1871,8 +1775,9 @@ static WebHTMLView *lastHitView = nil;
     // you'd simply see the printer fonts on screen. As of this writing, this does not happen with Safari.
 
     range->location = 1;
-    float totalScaleFactor = [self _scaleFactorForPrintOperation:[NSPrintOperation currentOperation]];
-    float userScaleFactor = [self _userScaleFactorForPrintOperation:[NSPrintOperation currentOperation]];
+    NSPrintOperation *printOperation = [NSPrintOperation currentOperation];
+    float totalScaleFactor = [self _scaleFactorForPrintOperation:printOperation];
+    float userScaleFactor = [printOperation _web_pageSetupScaleFactor];
     [_private->pageRects release];
     _private->pageRects = [[[self _bridge] computePageRectsWithPrintWidth:NSWidth([self bounds])/userScaleFactor
                                                              printHeight:[self _calculatePrintHeight]/totalScaleFactor] retain];
@@ -1897,21 +1802,8 @@ static WebHTMLView *lastHitView = nil;
 
 - (void)drawPageBorderWithSize:(NSSize)borderSize
 {
-    ASSERT(NSEqualSizes(borderSize, [[[NSPrintOperation currentOperation] printInfo] paperSize]));
-    
-    // The header and footer rect height scales with the page, but the width is always
-    // all the way across the printed page (inset by printing margins).
-    float userScale = [self _userScaleFactorForPrintOperation:[NSPrintOperation currentOperation]];
-    NSPrintInfo *printInfo = [[NSPrintOperation currentOperation] printInfo];
-    float headerFooterLeft = [printInfo leftMargin]/userScale;
-    float headerFooterWidth = (borderSize.width - ([printInfo leftMargin] + [printInfo rightMargin]))/userScale;
-    NSRect footerRect = NSMakeRect(headerFooterLeft, [printInfo bottomMargin]/userScale - [self _footerHeight] , 
-                                   headerFooterWidth, [self _footerHeight]);
-    NSRect headerRect = NSMakeRect(headerFooterLeft, (borderSize.height - [printInfo topMargin])/userScale, 
-                                   headerFooterWidth, [self _headerHeight]);
-    
-    [self _drawHeaderInRect:headerRect];
-    [self _drawFooterInRect:footerRect];
+    ASSERT(NSEqualSizes(borderSize, [[[NSPrintOperation currentOperation] printInfo] paperSize]));    
+    [[self _webView] _drawHeaderAndFooter];
 }
 
 - (void)endDocument
index b2171cf66f5efd815c9f2b605262254b435983f0..60f9fbff5928c39bed267aa323ce2bae1e1810ca 100644 (file)
@@ -15,6 +15,7 @@
 #import <WebKit/WebNSPasteboardExtras.h>
 #import <WebKit/WebNSViewExtras.h>
 #import <WebKit/WebViewPrivate.h>
+#import <WebKit/WebUIDelegatePrivate.h>
 
 #import <WebCore/WebCoreImageRenderer.h>
 
     needsLayout = NO;
 }
 
-- (void)beginDocument
-{
-    [self adjustFrameSize];
-    [super beginDocument];
-}
-
-- (void)endDocument
-{
-    [super endDocument];
-    [self adjustFrameSize];
-}
-
 - (void)setDataSource:(WebDataSource *)dataSource
 {
     ASSERT(!rep);
     return [rep image];
 }
 
+#pragma mark PRINTING
+
+- (void)drawPageBorderWithSize:(NSSize)borderSize
+{
+    ASSERT(NSEqualSizes(borderSize, [[[NSPrintOperation currentOperation] printInfo] paperSize]));
+    // FIXME: How to determine the number of pages required to print the whole image?
+    [[self webView] _drawHeaderAndFooter];
+}
+
+- (void)beginDocument
+{
+    [self adjustFrameSize];
+    [[self webView] _adjustPrintingMarginsForHeaderAndFooter];
+    [super beginDocument];
+}
+
+- (void)endDocument
+{
+    [super endDocument];
+    [self adjustFrameSize];
+}
+
+
 @end
index bd31412b64173195f524ea91a8e668abf045f19f..008ae8489f9e3ff45cbaa1c51f4ffa69ac3b214e 100644 (file)
     return resign;
 }
 
+#pragma mark PRINTING
+
+- (void)drawPageBorderWithSize:(NSSize)borderSize
+{
+    ASSERT(NSEqualSizes(borderSize, [[[NSPrintOperation currentOperation] printInfo] paperSize]));
+    [[[self _web_parentWebFrameView] _webView] _drawHeaderAndFooter];
+}
+
+- (BOOL)knowsPageRange:(NSRangePointer)range {
+    // Waiting for beginDocument to adjust the printing margins is too late.
+    [[[self _web_parentWebFrameView] _webView] _adjustPrintingMarginsForHeaderAndFooter];
+    return [super knowsPageRange:range];
+}
+
 @end
 
 @implementation WebTextView (TextSizing)
index a70569d32bfec6cf8ca698fd83b5332c52e69066..2dc7b4d96eb42030cc51016b0699a446e71ea666 100644 (file)
@@ -11,7 +11,7 @@
 
 - (float)webViewHeaderHeight:(WebView *)sender;
 - (float)webViewFooterHeight:(WebView *)sender;
-- (void)webView:(WebView *)sender drawHeaderInRect:(NSRect)rect forPage:(unsigned)pageIndex of:(unsigned)pageCount;
-- (void)webView:(WebView *)sender drawFooterInRect:(NSRect)rect forPage:(unsigned)pageIndex of:(unsigned)pageCount;
+- (void)webView:(WebView *)sender drawHeaderInRect:(NSRect)rect;
+- (void)webView:(WebView *)sender drawFooterInRect:(NSRect)rect;
 
 @end
index bcd49e5060e2f4fb3621e7f9a2805f1553112735..4bc1ddb14ec77665d0b08006f3d82ec24a88d17a 100644 (file)
@@ -30,6 +30,7 @@
 #import <WebKit/WebKitLogging.h>
 #import <WebKit/WebKitStatisticsPrivate.h>
 #import <WebKit/WebNSPasteboardExtras.h>
+#import "WebNSPrintOperationExtras.h"
 #import <WebKit/WebNSURLExtras.h>
 #import <WebKit/WebNSViewExtras.h>
 #import <WebKit/WebPluginDatabase.h>
@@ -40,6 +41,7 @@
 #import <WebKit/WebTextRepresentation.h>
 #import <WebKit/WebTextRenderer.h>
 #import <WebKit/WebUIDelegate.h>
+#import <WebKit/WebUIDelegatePrivate.h>
 
 #import <WebCore/WebCoreEncodings.h>
 #import <WebCore/WebCoreSettings.h>
@@ -1956,6 +1958,101 @@ static WebFrame *incrementFrame(WebFrame *curr, BOOL forward, BOOL wrapFlag)
 
 @end
 
+@implementation WebView (WebViewPrintingPrivate)
+
+- (float)_headerHeight
+{
+    if ([[self UIDelegate] respondsToSelector:@selector(webViewHeaderHeight:)]) {
+        return [[self UIDelegate] webViewHeaderHeight:self];
+    }
+    
+#ifdef DEBUG_HEADER_AND_FOOTER
+    return 25;
+#else
+    return 0;
+#endif
+}
+
+- (float)_footerHeight
+{
+    if ([[self UIDelegate] respondsToSelector:@selector(webViewFooterHeight:)]) {
+        return [[self UIDelegate] webViewFooterHeight:self];
+    }
+    
+#ifdef DEBUG_HEADER_AND_FOOTER
+    return 50;
+#else
+    return 0;
+#endif
+}
+
+- (void)_drawHeaderInRect:(NSRect)rect
+{
+#ifdef DEBUG_HEADER_AND_FOOTER
+    NSGraphicsContext *currentContext = [NSGraphicsContext currentContext];
+    [currentContext saveGraphicsState];
+    [[NSColor yellowColor] set];
+    NSRectFill(rect);
+    [currentContext restoreGraphicsState];
+#endif
+    
+    if ([[self UIDelegate] respondsToSelector:@selector(webView:drawHeaderInRect:)]) {
+        NSGraphicsContext *currentContext = [NSGraphicsContext currentContext];
+        [currentContext saveGraphicsState];
+        NSRectClip(rect);
+        [[self UIDelegate] webView:self drawHeaderInRect:rect]; 
+        [currentContext restoreGraphicsState];
+    }
+}
+
+- (void)_drawFooterInRect:(NSRect)rect
+{
+#ifdef DEBUG_HEADER_AND_FOOTER
+    NSGraphicsContext *currentContext = [NSGraphicsContext currentContext];
+    [currentContext saveGraphicsState];
+    [[NSColor cyanColor] set];
+    NSRectFill(rect);
+    [currentContext restoreGraphicsState];
+#endif
+    
+    if ([[self UIDelegate] respondsToSelector:@selector(webView:drawFooterInRect:)]) {
+        NSGraphicsContext *currentContext = [NSGraphicsContext currentContext];
+        [currentContext saveGraphicsState];
+        NSRectClip(rect);
+        [[self UIDelegate] webView:self drawFooterInRect:rect];
+        [currentContext restoreGraphicsState];
+    }
+}
+
+- (void)_adjustPrintingMarginsForHeaderAndFooter
+{
+    NSPrintOperation *op = [NSPrintOperation currentOperation];
+    NSPrintInfo *info = [op printInfo];
+    float scale = [op _web_pageSetupScaleFactor];
+    [info setTopMargin:[info topMargin] + [self _headerHeight]*scale];
+    [info setBottomMargin:[info bottomMargin] + [self _footerHeight]*scale];
+}
+
+- (void)_drawHeaderAndFooter
+{
+    // The header and footer rect height scales with the page, but the width is always
+    // all the way across the printed page (inset by printing margins).
+    NSPrintOperation *op = [NSPrintOperation currentOperation];
+    float scale = [op _web_pageSetupScaleFactor];
+    NSPrintInfo *printInfo = [op printInfo];
+    NSSize paperSize = [printInfo paperSize];
+    float headerFooterLeft = [printInfo leftMargin]/scale;
+    float headerFooterWidth = (paperSize.width - ([printInfo leftMargin] + [printInfo rightMargin]))/scale;
+    NSRect footerRect = NSMakeRect(headerFooterLeft, [printInfo bottomMargin]/scale - [self _footerHeight] , 
+                                   headerFooterWidth, [self _footerHeight]);
+    NSRect headerRect = NSMakeRect(headerFooterLeft, (paperSize.height - [printInfo topMargin])/scale, 
+                                   headerFooterWidth, [self _headerHeight]);
+    
+    [self _drawHeaderInRect:headerRect];
+    [self _drawFooterInRect:footerRect];
+}
+@end
+
 @implementation WebView (WebDebugBinding)
 
 - (void)addObserver:(NSObject *)anObserver forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context
index 643293297bf0bbefac2f53b6ed9752cb6f7ccad6..5920d5018aa7416f327dc8fa71cfc5d3123cc794 100644 (file)
@@ -264,9 +264,31 @@ Could be worth adding to the API.
 
 @end
 
+@interface WebView (WebViewPrintingPrivate)
+/*!
+    @method _adjustPrintingMarginsForHeaderAndFooter:
+    @abstract Increase the top and bottom margins for the current print operation to
+    account for the header and footer height. 
+    @discussion Called by <WebDocument> implementors once when a print job begins. If the
+    <WebDocument> implementor implements knowsPageRange:, this should be called from there.
+    Otherwise this should be called from beginDocument. The <WebDocument> implementors need
+    to also call _drawHeaderAndFooter.
+*/
+- (void)_adjustPrintingMarginsForHeaderAndFooter;
+
+/*!
+    @method _drawHeaderAndFooter
+    @abstract Gives the WebView's UIDelegate a chance to draw a header and footer on the
+    printed page. 
+    @discussion This should be called by <WebDocument> implementors from an override of
+    drawPageBorderWithSize:.
+*/
+- (void)_drawHeaderAndFooter;
+@end
+
 @interface _WebSafeForwarder : NSObject
 {
-    id target; // Non-retainted.  Don't retain delegates;
+    id target; // Non-retained.  Don't retain delegates;
     id defaultTarget;
     Class templateClass;
 }