HIViewAdapter.m 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Copyright (C) 2005, 2007 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
  14. * its contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  18. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  21. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #ifndef __LP64__
  29. #import "HIViewAdapter.h"
  30. #import "QuickDrawCompatibility.h"
  31. #import "WebNSObjectExtras.h"
  32. #import <wtf/Assertions.h>
  33. static void SetViewNeedsDisplay(HIViewRef inView, RgnHandle inRegion, Boolean inNeedsDisplay);
  34. #define WATCH_INVALIDATION 0
  35. @interface NSView(ShhhhDontTell)
  36. - (NSRect)_dirtyRect;
  37. @end
  38. @implementation HIViewAdapter
  39. static CFMutableDictionaryRef sViewMap;
  40. static IMP oldNSViewSetNeedsDisplayIMP;
  41. static IMP oldNSViewSetNeedsDisplayInRectIMP;
  42. static IMP oldNSViewNextValidKeyViewIMP;
  43. static void _webkit_NSView_setNeedsDisplay(id self, SEL _cmd, BOOL flag);
  44. static void _webkit_NSView_setNeedsDisplayInRect(id self, SEL _cmd, NSRect invalidRect);
  45. static NSView *_webkit_NSView_nextValidKeyView(id self, SEL _cmd);
  46. + (void)bindHIViewToNSView:(HIViewRef)hiView nsView:(NSView*)nsView
  47. {
  48. if (sViewMap == NULL) {
  49. sViewMap = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
  50. // Override -[NSView setNeedsDisplay:]
  51. Method setNeedsDisplayMethod = class_getInstanceMethod(objc_getClass("NSView"), @selector(setNeedsDisplay:));
  52. ASSERT(setNeedsDisplayMethod);
  53. ASSERT(!oldNSViewSetNeedsDisplayIMP);
  54. oldNSViewSetNeedsDisplayIMP = method_setImplementation(setNeedsDisplayMethod, (IMP)_webkit_NSView_setNeedsDisplay);
  55. // Override -[NSView setNeedsDisplayInRect:]
  56. Method setNeedsDisplayInRectMethod = class_getInstanceMethod(objc_getClass("NSView"), @selector(setNeedsDisplayInRect:));
  57. ASSERT(setNeedsDisplayInRectMethod);
  58. ASSERT(!oldNSViewSetNeedsDisplayInRectIMP);
  59. oldNSViewSetNeedsDisplayInRectIMP = method_setImplementation(setNeedsDisplayInRectMethod, (IMP)_webkit_NSView_setNeedsDisplayInRect);
  60. // Override -[NSView nextValidKeyView]
  61. Method nextValidKeyViewMethod = class_getInstanceMethod(objc_getClass("NSView"), @selector(nextValidKeyView));
  62. ASSERT(nextValidKeyViewMethod);
  63. ASSERT(!oldNSViewNextValidKeyViewIMP);
  64. oldNSViewNextValidKeyViewIMP = method_setImplementation(nextValidKeyViewMethod, (IMP)_webkit_NSView_nextValidKeyView);
  65. }
  66. CFDictionaryAddValue(sViewMap, nsView, hiView);
  67. }
  68. + (HIViewRef)getHIViewForNSView:(NSView*)inView
  69. {
  70. return sViewMap ? (HIViewRef)CFDictionaryGetValue(sViewMap, inView) : NULL;
  71. }
  72. + (void)unbindNSView:(NSView*)inView
  73. {
  74. CFDictionaryRemoveValue(sViewMap, inView);
  75. }
  76. static void _webkit_NSView_setNeedsDisplay(id self, SEL _cmd, BOOL flag)
  77. {
  78. oldNSViewSetNeedsDisplayIMP(self, _cmd, flag);
  79. if (!flag) {
  80. HIViewRef hiView = NULL;
  81. NSRect targetBounds = [self visibleRect];
  82. NSRect validRect = targetBounds;
  83. NSView *view = self;
  84. while (view) {
  85. targetBounds = [view visibleRect];
  86. if ((hiView = [HIViewAdapter getHIViewForNSView:view]) != NULL)
  87. break;
  88. validRect = [view convertRect:validRect toView:[view superview]];
  89. view = [view superview];
  90. }
  91. if (hiView) {
  92. // Flip rect here and convert to region
  93. HIRect rect;
  94. rect.origin.x = validRect.origin.x;
  95. rect.origin.y = targetBounds.size.height - NSMaxY(validRect);
  96. rect.size.height = validRect.size.height;
  97. rect.size.width = validRect.size.width;
  98. // For now, call the region-based API.
  99. RgnHandle rgn = NewRgn();
  100. if (rgn) {
  101. Rect qdRect;
  102. qdRect.top = (SInt16)rect.origin.y;
  103. qdRect.left = (SInt16)rect.origin.x;
  104. qdRect.bottom = CGRectGetMaxY(rect);
  105. qdRect.right = CGRectGetMaxX(rect);
  106. RectRgn(rgn, &qdRect);
  107. SetViewNeedsDisplay(hiView, rgn, false);
  108. DisposeRgn(rgn);
  109. }
  110. }
  111. }
  112. }
  113. static void _webkit_NSView_setNeedsDisplayInRect(id self, SEL _cmd, NSRect invalidRect)
  114. {
  115. invalidRect = NSUnionRect(invalidRect, [self _dirtyRect]);
  116. oldNSViewSetNeedsDisplayInRectIMP(self, _cmd, invalidRect);
  117. if (!NSIsEmptyRect(invalidRect)) {
  118. HIViewRef hiView = NULL;
  119. NSRect targetBounds = [(NSView *)self bounds];
  120. NSView *view = self;
  121. while (view) {
  122. targetBounds = [view bounds];
  123. if ((hiView = [HIViewAdapter getHIViewForNSView:view]) != NULL)
  124. break;
  125. invalidRect = [view convertRect:invalidRect toView:[view superview]];
  126. view = [view superview];
  127. }
  128. if (hiView) {
  129. if (NSWidth(invalidRect) > 0 && NSHeight(invalidRect)) {
  130. // Flip rect here and convert to region
  131. HIRect rect;
  132. rect.origin.x = invalidRect.origin.x;
  133. rect.origin.y = targetBounds.size.height - NSMaxY(invalidRect);
  134. rect.size.height = invalidRect.size.height;
  135. rect.size.width = invalidRect.size.width;
  136. // For now, call the region-based API.
  137. RgnHandle rgn = NewRgn();
  138. if (rgn) {
  139. Rect qdRect;
  140. qdRect.top = (SInt16)rect.origin.y;
  141. qdRect.left = (SInt16)rect.origin.x;
  142. qdRect.bottom = CGRectGetMaxY(rect);
  143. qdRect.right = CGRectGetMaxX(rect);
  144. RectRgn(rgn, &qdRect);
  145. SetViewNeedsDisplay(hiView, rgn, true);
  146. DisposeRgn(rgn);
  147. }
  148. }
  149. } else
  150. [[self window] setViewsNeedDisplay:YES];
  151. }
  152. }
  153. static NSView *_webkit_NSView_nextValidKeyView(id self, SEL _cmd)
  154. {
  155. if ([HIViewAdapter getHIViewForNSView:self])
  156. return [[self window] contentView];
  157. else
  158. return oldNSViewNextValidKeyViewIMP(self, _cmd);
  159. }
  160. @end
  161. static void SetViewNeedsDisplay(HIViewRef inHIView, RgnHandle inRegion, Boolean inNeedsDisplay)
  162. {
  163. WindowAttributes attrs;
  164. GetWindowAttributes(GetControlOwner(inHIView), &attrs);
  165. if (attrs & kWindowCompositingAttribute) {
  166. #if WATCH_INVALIDATION
  167. Rect bounds;
  168. GetRegionBounds(inRegion, &bounds);
  169. printf("%s: rect on input %d %d %d %d\n", inNeedsDisplay ? "INVALIDATE" : "VALIDATE",
  170. bounds.top, bounds.left, bounds.bottom, bounds.right);
  171. #endif
  172. HIViewSetNeedsDisplayInRegion(inHIView, inRegion, inNeedsDisplay);
  173. } else {
  174. Rect bounds, cntlBounds;
  175. GrafPtr port, savePort;
  176. Rect portBounds;
  177. #if WATCH_INVALIDATION
  178. printf("%s: rect on input %d %d %d %d\n", inNeedsDisplay ? "INVALIDATE" : "VALIDATE",
  179. bounds.top, bounds.left, bounds.bottom, bounds.right);
  180. #endif
  181. GetControlBounds(inHIView, &cntlBounds);
  182. #if WATCH_INVALIDATION
  183. printf("%s: control bounds are %d %d %d %d\n", inNeedsDisplay ? "INVALIDATE" : "VALIDATE",
  184. cntlBounds.top, cntlBounds.left, cntlBounds.bottom, cntlBounds.right);
  185. #endif
  186. port = GetWindowPort(GetControlOwner(inHIView));
  187. GetPort(&savePort);
  188. SetPort(port);
  189. GetPortBounds(port, &portBounds);
  190. SetOrigin(0, 0);
  191. OffsetRgn(inRegion, cntlBounds.left, cntlBounds.top);
  192. GetRegionBounds(inRegion, &bounds);
  193. #if WATCH_INVALIDATION
  194. printf("%s: rect in port coords %d %d %d %d\n", inNeedsDisplay ? "INVALIDATE" : "VALIDATE",
  195. bounds.top, bounds.left, bounds.bottom, bounds.right);
  196. #endif
  197. if (inNeedsDisplay)
  198. InvalWindowRgn(GetControlOwner(inHIView), inRegion);
  199. else
  200. ValidWindowRgn(GetControlOwner(inHIView), inRegion);
  201. SetOrigin(portBounds.left, portBounds.top);
  202. SetPort(savePort);
  203. }
  204. }
  205. #endif