diff options
author | Kevin Walzer <kw@codebykevin.com> | 2014-08-16 00:52:12 (GMT) |
---|---|---|
committer | Kevin Walzer <kw@codebykevin.com> | 2014-08-16 00:52:12 (GMT) |
commit | 42f946fb1df90d38c04c7b68cf1fd3aca9377d6f (patch) | |
tree | cc764bdecf38c9acc11fb11c0751d6afa7b3c0d2 | |
parent | ab19054c699a4edb7222aafe4595c79a139d4fe4 (diff) | |
download | tk-42f946fb1df90d38c04c7b68cf1fd3aca9377d6f.zip tk-42f946fb1df90d38c04c7b68cf1fd3aca9377d6f.tar.gz tk-42f946fb1df90d38c04c7b68cf1fd3aca9377d6f.tar.bz2 |
Fix for shimmering of buttons embedded when scrolled in text and canvas widgets; improvements in scrolling smoothness in text widget. Thanks to Marc Culler for patches.
-rw-r--r-- | generic/tkTextDisp.c | 11 | ||||
-rw-r--r-- | macosx/tkMacOSXButton.c | 163 | ||||
-rw-r--r-- | macosx/tkMacOSXDraw.c | 132 | ||||
-rw-r--r-- | macosx/tkMacOSXInt.h | 3 |
4 files changed, 213 insertions, 96 deletions
diff --git a/generic/tkTextDisp.c b/generic/tkTextDisp.c index aebdcc6..8ceb3fa 100644 --- a/generic/tkTextDisp.c +++ b/generic/tkTextDisp.c @@ -3970,6 +3970,15 @@ DisplayText( UpdateDisplayInfo(textPtr); dInfoPtr->dLinesInvalidated = 0; +#ifdef MAC_OSX_TK + /* + * Make sure that unmapped subwindows really have been unmapped. + * If the unmap request is pending as an idle request, the window + * can get redrawn on top of the widget. + */ + while (Tcl_DoOneEvent(TCL_IDLE_EVENTS|TCL_DONT_WAIT)) {} +#endif + /* * See if it's possible to bring some parts of the screen up-to-date by * scrolling (copying from other parts of the screen). We have to be @@ -4035,7 +4044,7 @@ DisplayText( */ if ((y + height) > dInfoPtr->maxY) { - height = dInfoPtr->maxY -y; + height = dInfoPtr->maxY - y; } oldY = dlPtr->oldY; if (y < dInfoPtr->y) { diff --git a/macosx/tkMacOSXButton.c b/macosx/tkMacOSXButton.c index 036624d..dd4cca7 100644 --- a/macosx/tkMacOSXButton.c +++ b/macosx/tkMacOSXButton.c @@ -7,6 +7,7 @@ * Copyright (c) 1996-1997 by Sun Microsystems, Inc. * Copyright 2001-2009, Apple Inc. * Copyright (c) 2006-2009 Daniel A. Steffen <das@users.sourceforge.net> + * Copyright 2014 Marc Culler. * * See the file "license.terms" for information on usage and redistribution * of this file, and for a DISCLAIMER OF ALL WARRANTIES. @@ -23,9 +24,59 @@ #endif */ +static NSRect TkMacOSXGetButtonFrame(TkButton *butPtr); + +/* + * A subclass of NSButton with sanity checking: + * NSButtons created by Tk will have their tag set to a pointer to the TkButton + * which manages the NSButton. This allows a TkNSButton to be aware of the + * state of its Tk parent. This subclass overrides the drawRect method + * so that it will not draw itself unless the NSButton frame matches + * the frame which was installed by DisplayButton, and the TkButton is + * mapped. Also, it will not draw anything if the widget is completely + * outside of its container. + */ + +@interface TkNSButton: NSButton + +@end + +@implementation TkNSButton + + - (void)drawRect:(NSRect)dirtyRect + { + NSInteger tag = [self tag]; + if ( tag != -1) { + TkButton *butPtr = (TkButton *)tag; + MacDrawable* macWin = (MacDrawable *)butPtr; + NSRect Tkframe = TkMacOSXGetButtonFrame(butPtr); + Tk_Window tkwin = butPtr->tkwin; + /* Do not draw if the widget is misplaced or unmapped. */ + if ( NSIsEmptyRect(Tkframe) || + ! macWin->winPtr->flags & TK_MAPPED || + ! NSEqualRects(Tkframe, [self frame]) + ) { + return; + } + /* Do not draw if the widget is completely outside of its parent, or within 20 pixels of the lower border; this prevents buttons from being drawn on peer widgets as scrolling occurs. */ + if (tkwin) { + int parent_height = Tk_Height(Tk_Parent(tkwin)); + int widget_height = Tk_Height(tkwin); + int y = Tk_Y(tkwin); + if ( y > parent_height - 20 || y + widget_height < 0 ) { + return; + } + } + [super drawRect:dirtyRect]; + } + } + +@end + + typedef struct MacButton { TkButton info; - NSButton *button; + TkNSButton *button; NSImage *image, *selectImage, *tristateImage; #if TK_MAC_BUTTON_USE_COMPATIBILITY_METRICS int fix; @@ -186,6 +237,45 @@ TkpDisplayButton( /* *---------------------------------------------------------------------- * + * TkpShiftButton -- + * + * Moves the frame of an NSButton (or TkNSButton) and in case the tag is + * set, also adjusts the xOff and yOff of the controlling TkButton's + * MacDrawable. This is used to avoid jitter when scrolling. + * + * Results: + * None + * + * Side effects: + * Moves the NSbutton after adjusting the associated MacDrawable. + * + *---------------------------------------------------------------------- + */ +void +TkpShiftButton( + NSButton *button, + NSPoint delta ) + { + NSPoint origin = [button frame].origin; + NSInteger tag = [button tag]; + if ( tag != -1) { + TkButton* butPtr = (TkButton *)tag; + TkWindow *winPtr = (TkWindow *) (butPtr->tkwin); + if (winPtr) { + MacDrawable *macWin = (MacDrawable *) winPtr->window; + macWin->xOff += delta.x; + macWin->yOff += delta.y; + } + } + origin.x += delta.x; + origin.y -= delta.y; + [button setFrameOrigin:origin]; + } + + +/* + *---------------------------------------------------------------------- + * * TkpComputeButtonGeometry -- * * After changes in a button's text or bitmap, this procedure @@ -218,7 +308,8 @@ TkpComputeButtonGeometry( case TYPE_CHECK_BUTTON: case TYPE_RADIO_BUTTON: if (!macButtonPtr->button) { - NSButton *button = [[NSButton alloc] initWithFrame:NSZeroRect]; + TkNSButton *button = [[TkNSButton alloc] initWithFrame:NSZeroRect]; + [button setTag:(NSInteger)butPtr]; macButtonPtr->button = TkMacOSXMakeUncollectable(button); } ComputeNativeButtonGeometry(butPtr); @@ -266,6 +357,52 @@ TkpButtonSetDefaults() /* *---------------------------------------------------------------------- * + * TkMacOSXGetButtonFrame -- + * + * Computes a frame for an NSButton that will correspond to where + * Tk thinks the button is located. + * + * Results: + * Returns an NSRect describing a frame for an NSButton. + * + * Side effects: + * None + * + *---------------------------------------------------------------------- + */ +NSRect TkMacOSXGetButtonFrame( + TkButton *butPtr) +{ + MacButton *macButtonPtr = (MacButton *) butPtr; + Tk_Window tkwin = butPtr->tkwin; + TkWindow *winPtr = (TkWindow *) tkwin; + if (tkwin) { + MacDrawable *macWin = (MacDrawable *) winPtr->window; + NSView *view = TkMacOSXDrawableView(macWin); + CGFloat viewHeight = [view bounds].size.height; + NSRect frame = NSMakeRect(macWin->xOff, macWin->yOff, + Tk_Width(tkwin), Tk_Height(tkwin)); + +#if TK_MAC_BUTTON_USE_COMPATIBILITY_METRICS + if (tkMacOSXUseCompatibilityMetrics) { + BoundsFix boundsFix = boundsFixes[macButtonPtr->fix]; + frame = NSOffsetRect(frame, boundsFix.offsetX, boundsFix.offsetY); + frame.size.height -= boundsFix.shrinkH + NATIVE_BUTTON_EXTRA_H; + frame = NSInsetRect(frame, boundsFix.inset + NATIVE_BUTTON_INSET, + boundsFix.inset + NATIVE_BUTTON_INSET); + } +#endif + + frame.origin.y = viewHeight - (frame.origin.y + frame.size.height); + return frame; + } else { + return NSZeroRect; + } +} + +/* + *---------------------------------------------------------------------- + * * DisplayNativeButton -- * * This procedure is invoked to display a button widget. It is @@ -286,7 +423,7 @@ DisplayNativeButton( TkButton *butPtr) { MacButton *macButtonPtr = (MacButton *) butPtr; - NSButton *button = macButtonPtr->button; + TkNSButton *button = macButtonPtr->button; Tk_Window tkwin = butPtr->tkwin; TkWindow *winPtr = (TkWindow *) tkwin; MacDrawable *macWin = (MacDrawable *) winPtr->window; @@ -349,21 +486,8 @@ DisplayNativeButton( } else { [button setKeyEquivalent:@""]; } - frame = NSMakeRect(macWin->xOff, macWin->yOff, Tk_Width(tkwin), - Tk_Height(tkwin)); -#if TK_MAC_BUTTON_USE_COMPATIBILITY_METRICS - if (tkMacOSXUseCompatibilityMetrics) { - BoundsFix boundsFix = boundsFixes[macButtonPtr->fix]; - frame = NSOffsetRect(frame, boundsFix.offsetX, boundsFix.offsetY); - frame.size.height -= boundsFix.shrinkH + NATIVE_BUTTON_EXTRA_H; - frame = NSInsetRect(frame, boundsFix.inset + NATIVE_BUTTON_INSET, - boundsFix.inset + NATIVE_BUTTON_INSET); - } -#endif - frame.origin.y = viewHeight - (frame.origin.y + frame.size.height); - if (!NSEqualRects(frame, [button frame])) { - [button setFrame:frame]; - } + frame = TkMacOSXGetButtonFrame(butPtr); + [button setFrame:frame]; [button displayRectIgnoringOpacity:[button bounds]]; TkMacOSXRestoreDrawingContext(&dc); #ifdef TK_MAC_DEBUG_BUTTON @@ -396,7 +520,7 @@ ComputeNativeButtonGeometry( TkButton *butPtr) /* Button whose geometry may have changed. */ { MacButton *macButtonPtr = (MacButton *) butPtr; - NSButton *button = macButtonPtr->button; + TkNSButton *button = macButtonPtr->button; NSButtonCell *cell = [button cell]; NSButtonType type = -1; NSBezelStyle style = 0; @@ -1169,6 +1293,7 @@ ComputeUnixButtonGeometry( Tk_SetInternalBorder(butPtr->tkwin, butPtr->inset); } + /* * Local Variables: * mode: objc diff --git a/macosx/tkMacOSXDraw.c b/macosx/tkMacOSXDraw.c index 3a3e288..ce8bf9b 100644 --- a/macosx/tkMacOSXDraw.c +++ b/macosx/tkMacOSXDraw.c @@ -17,6 +17,7 @@ #include "tkMacOSXPrivate.h" #include "tkMacOSXDebug.h" #include "xbytes.h" +#include "tkButton.h" /* #ifdef TK_MAC_DEBUG @@ -1479,86 +1480,67 @@ TkScrollWindow( TkRegion damageRgn) /* Region to accumulate damage in. */ { Drawable drawable = Tk_WindowId(tkwin); - MacDrawable *macDraw = (MacDrawable *) drawable; + MacDrawable *macDraw = (MacDrawable *) drawable; NSView *view = TkMacOSXDrawableView(macDraw); - CGRect visRect, srcRect, dstRect, scroll_src, scroll_dst; - HIShapeRef dmgRgn = NULL; - NSRect bounds; + CGRect srcRect, dstRect; + HIShapeRef dmgRgn = NULL, extraRgn; + NSRect bounds, visRect, scrollSrc, scrollDst; + NSPoint delta = NSMakePoint(dx, dy); int result; - + if ( view ) { - /* Get the scroll area in NSView coordinates (origin at bottom left). */ - bounds = [view bounds]; - scroll_src = CGRectMake( - macDraw->xOff + x, - bounds.size.height - height - (macDraw->yOff + y), - width, height); - scroll_dst = CGRectOffset(scroll_src, dx, -dy); - /* Limit scrolling to the window content area. */ - visRect = NSRectToCGRect([view visibleRect]); - scroll_src = CGRectIntersection(scroll_src, visRect); - scroll_dst = CGRectIntersection(scroll_dst, visRect); - - if ( !CGRectIsEmpty(scroll_src) && !CGRectIsEmpty(scroll_dst) ) { - - /* - * Mark the difference between source and destination as damaged. - * This region is described in the Tk coordinate system, after shifting by dy. - */ - - srcRect = CGRectMake(x - macDraw->xOff, y - macDraw->yOff, - width, height); - dstRect = CGRectOffset(srcRect, dx, dy); - dmgRgn = HIShapeCreateMutableWithRect(&srcRect); - HIShapeRef dstRgn = HIShapeCreateWithRect(&dstRect); - ChkErr(HIShapeDifference, dmgRgn, dstRgn, (HIMutableShapeRef) dmgRgn); - CFRelease(dstRgn); - - /* Scroll the rectangle. */ - [view scrollRect:NSRectFromCGRect(scroll_src) by:NSMakeSize(dx, -dy)]; - [view displayRect:NSRectFromCGRect(scroll_dst)]; - - /* - * When a Text widget contains embedded images, scrolling generates - * lots of artifacts involving multiple copies of the images - * displayed on top of each other. Extensive experimentation, with - * very little help from the Apple documentation, indicates that - * whenever an image is displayed it gets added as a subview, which - * then gets automatically redisplayed in its original location. - * - * We do two things to combat this. First, each subview that meets - * the scroll area is added as a damage rectangle. Second, we - * redisplay the subviews after the scroll. - */ - - /* - * Step 1: Find any subviews that meet the scroll area and mark - * them as damaged. Use Tk coordinates, shifted to account for the - * future scrolling. - */ - - for (NSView *subview in [view subviews] ) { - NSRect frame = [subview frame]; - CGRect subviewRect = CGRectMake( - frame.origin.x - macDraw->xOff + dx, - (bounds.size.height - frame.origin.y - frame.size.height) - macDraw->yOff + dy, - frame.size.width, frame.size.height); - /* Rectangles with negative coordinates seem to cause trouble. */ - if (subviewRect.origin.y < 0 && subviewRect.origin.y + subviewRect.size.height > 0) { - subviewRect.origin.y = 0; - } - CGRect intersection = CGRectIntersection(srcRect, subviewRect); - if (! CGRectIsEmpty(intersection) ){ - dstRgn = HIShapeCreateWithRect(&subviewRect); - ChkErr(HIShapeUnion, dmgRgn, dstRgn, (HIMutableShapeRef) dmgRgn); - CFRelease(dstRgn); - } - } - } + /* Get the scroll area in NSView coordinates (origin at bottom left). */ + bounds = [view bounds]; + scrollSrc = NSMakeRect( + macDraw->xOff + x, + bounds.size.height - height - (macDraw->yOff + y), + width, height); + scrollDst = NSOffsetRect(scrollSrc, dx, -dy); + /* Limit scrolling to the window content area. */ + visRect = [view visibleRect]; + scrollSrc = NSIntersectionRect(scrollSrc, visRect); + scrollDst = NSIntersectionRect(scrollDst, visRect); + + if ( !NSIsEmptyRect(scrollSrc) && !NSIsEmptyRect(scrollDst) ) { + + /* + * Mark the difference between source and destination as damaged. + * This region is described in the Tk coordinate system. + */ + + srcRect = CGRectMake(x, y, width, height); + dstRect = CGRectOffset(srcRect, dx, dy); + dmgRgn = HIShapeCreateMutableWithRect(&srcRect); + extraRgn = HIShapeCreateWithRect(&dstRect); + ChkErr(HIShapeDifference, dmgRgn, extraRgn, (HIMutableShapeRef) dmgRgn); + CFRelease(extraRgn); + + /* Scroll the rectangle. */ + [view scrollRect:scrollSrc by:NSMakeSize(dx, -dy)]; + + /* + * Adjust the positions of the button subwindows that meet the scroll + * area. + */ + + for (NSView *subview in [view subviews] ) { + if ( [subview isKindOfClass:[NSButton class]] == YES ) { + NSRect subframe = [subview frame]; + if ( NSIntersectsRect(scrollSrc, subframe) || + NSIntersectsRect(scrollDst, subframe) ) { + TkpShiftButton((NSButton *)subview, delta ); + } + } + } + + /* Redisplay the scrolled area. */ + [view displayRect:scrollDst]; + + } } - + if ( dmgRgn == NULL ) { - dmgRgn = HIShapeCreateEmpty(); + dmgRgn = HIShapeCreateEmpty(); } TkMacOSXSetWithNativeRegion(damageRgn, dmgRgn); result = HIShapeIsEmpty(dmgRgn) ? 0 : 1; diff --git a/macosx/tkMacOSXInt.h b/macosx/tkMacOSXInt.h index 813acce..249d5cf 100644 --- a/macosx/tkMacOSXInt.h +++ b/macosx/tkMacOSXInt.h @@ -24,6 +24,7 @@ #ifndef _TKMAC #include "tkMacOSX.h" +#import <Cocoa/Cocoa.h> #endif /* @@ -196,7 +197,7 @@ MODULE_SCOPE void TkpClipDrawableToRect(Display *display, Drawable d, int x, int y, int width, int height); MODULE_SCOPE void TkpRetainRegion(TkRegion r); MODULE_SCOPE void TkpReleaseRegion(TkRegion r); - +MODULE_SCOPE void TkpShiftButton(NSButton *button, NSPoint delta); /* * Include the stubbed internal platform-specific API. */ |