WebNodeHighlight.mm 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright (C) 2007, 2008 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. #import "WebNodeHighlight.h"
  29. #import "WebNodeHighlightView.h"
  30. #import "WebNSViewExtras.h"
  31. #import <WebCore/InspectorController.h>
  32. #import <wtf/Assertions.h>
  33. using namespace WebCore;
  34. @interface WebNodeHighlight (FileInternal)
  35. - (NSRect)_computeHighlightWindowFrame;
  36. - (void)_repositionHighlightWindow;
  37. @end
  38. @implementation WebNodeHighlight
  39. - (id)initWithTargetView:(NSView *)targetView inspectorController:(InspectorController*)inspectorController
  40. {
  41. self = [super init];
  42. if (!self)
  43. return nil;
  44. _targetView = [targetView retain];
  45. _inspectorController = inspectorController;
  46. int styleMask = NSBorderlessWindowMask;
  47. NSRect contentRect = [NSWindow contentRectForFrameRect:[self _computeHighlightWindowFrame] styleMask:styleMask];
  48. _highlightWindow = [[NSWindow alloc] initWithContentRect:contentRect styleMask:styleMask backing:NSBackingStoreBuffered defer:NO];
  49. [_highlightWindow setBackgroundColor:[NSColor clearColor]];
  50. [_highlightWindow setOpaque:NO];
  51. [_highlightWindow setIgnoresMouseEvents:YES];
  52. [_highlightWindow setReleasedWhenClosed:NO];
  53. _highlightView = [[WebNodeHighlightView alloc] initWithWebNodeHighlight:self];
  54. [_highlightWindow setContentView:_highlightView];
  55. [_highlightView release];
  56. return self;
  57. }
  58. - (void)dealloc
  59. {
  60. ASSERT(!_highlightWindow);
  61. ASSERT(!_targetView);
  62. ASSERT(!_highlightView);
  63. [super dealloc];
  64. }
  65. - (void)attach
  66. {
  67. ASSERT(_targetView);
  68. ASSERT([_targetView window]);
  69. ASSERT(_highlightWindow);
  70. if (!_highlightWindow || !_targetView || ![_targetView window])
  71. return;
  72. [[_targetView window] addChildWindow:_highlightWindow ordered:NSWindowAbove];
  73. // Observe both frame-changed and bounds-changed notifications because either one could leave
  74. // the highlight incorrectly positioned with respect to the target view. We need to do this for
  75. // the entire superview hierarchy to handle scrolling, bars coming and going, etc.
  76. // (without making concrete assumptions about the view hierarchy).
  77. NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
  78. for (NSView *v = _targetView; v; v = [v superview]) {
  79. [notificationCenter addObserver:self selector:@selector(_repositionHighlightWindow) name:NSViewFrameDidChangeNotification object:v];
  80. [notificationCenter addObserver:self selector:@selector(_repositionHighlightWindow) name:NSViewBoundsDidChangeNotification object:v];
  81. }
  82. if (_delegate && [_delegate respondsToSelector:@selector(didAttachWebNodeHighlight:)])
  83. [_delegate didAttachWebNodeHighlight:self];
  84. }
  85. - (id)delegate
  86. {
  87. return _delegate;
  88. }
  89. - (void)detach
  90. {
  91. if (!_highlightWindow) {
  92. ASSERT(!_targetView);
  93. return;
  94. }
  95. if (_delegate && [_delegate respondsToSelector:@selector(willDetachWebNodeHighlight:)])
  96. [_delegate willDetachWebNodeHighlight:self];
  97. NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
  98. [notificationCenter removeObserver:self name:NSViewFrameDidChangeNotification object:nil];
  99. [notificationCenter removeObserver:self name:NSViewBoundsDidChangeNotification object:nil];
  100. [[_highlightWindow parentWindow] removeChildWindow:_highlightWindow];
  101. [_highlightWindow release];
  102. _highlightWindow = nil;
  103. [_targetView release];
  104. _targetView = nil;
  105. // We didn't retain _highlightView, but we do need to tell it to forget about us, so it doesn't
  106. // try to send our delegate messages after we've been dealloc'ed, e.g.
  107. [_highlightView detachFromWebNodeHighlight];
  108. _highlightView = nil;
  109. }
  110. - (WebNodeHighlightView *)highlightView
  111. {
  112. return _highlightView;
  113. }
  114. - (void)setDelegate:(id)delegate
  115. {
  116. // The delegate is not retained, as usual in Cocoa.
  117. _delegate = delegate;
  118. }
  119. - (void)setNeedsUpdateInTargetViewRect:(NSRect)rect
  120. {
  121. ASSERT(_targetView);
  122. [[_targetView window] disableScreenUpdatesUntilFlush];
  123. // Mark the whole highlight view as needing display since we don't know what areas
  124. // need updated, since the highlight can be larger than the element to show margins.
  125. [_highlightView setNeedsDisplay:YES];
  126. // Redraw highlight view immediately so it updates in sync with the target view.
  127. // This is especially visible when resizing the window, scrolling or with DHTML.
  128. [_highlightView displayIfNeeded];
  129. }
  130. - (NSView *)targetView
  131. {
  132. return _targetView;
  133. }
  134. - (InspectorController*)inspectorController
  135. {
  136. return _inspectorController;
  137. }
  138. @end
  139. @implementation WebNodeHighlight (FileInternal)
  140. - (NSRect)_computeHighlightWindowFrame
  141. {
  142. ASSERT(_targetView);
  143. ASSERT([_targetView window]);
  144. NSRect highlightWindowFrame = [_targetView convertRect:[_targetView visibleRect] toView:nil];
  145. highlightWindowFrame.origin = [[_targetView window] convertBaseToScreen:highlightWindowFrame.origin];
  146. return highlightWindowFrame;
  147. }
  148. - (void)_repositionHighlightWindow
  149. {
  150. // This assertion fires in cases where a new tab is created while the highlight
  151. // is showing (<http://bugs.webkit.org/show_bug.cgi?id=14254>)
  152. ASSERT([_targetView window]);
  153. // Until that bug is fixed, bail out to avoid worse problems where the highlight
  154. // moves to a nonsense location.
  155. if (![_targetView window])
  156. return;
  157. // Disable screen updates so the highlight moves in sync with the view.
  158. [[_targetView window] disableScreenUpdatesUntilFlush];
  159. [_highlightWindow setFrame:[self _computeHighlightWindowFrame] display:YES];
  160. }
  161. @end