1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
/*
* Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#import "WebNodeHighlight.h"
#import "WebNodeHighlightView.h"
#import "WebNSViewExtras.h"
#import <WebCore/InspectorController.h>
#import <wtf/Assertions.h>
using namespace WebCore;
@interface WebNodeHighlight (FileInternal)
- (NSRect)_computeHighlightWindowFrame;
- (void)_repositionHighlightWindow;
@end
@implementation WebNodeHighlight
- (id)initWithTargetView:(NSView *)targetView inspectorController:(InspectorController*)inspectorController
{
self = [super init];
if (!self)
return nil;
_targetView = [targetView retain];
_inspectorController = inspectorController;
int styleMask = NSBorderlessWindowMask;
NSRect contentRect = [NSWindow contentRectForFrameRect:[self _computeHighlightWindowFrame] styleMask:styleMask];
_highlightWindow = [[NSWindow alloc] initWithContentRect:contentRect styleMask:styleMask backing:NSBackingStoreBuffered defer:NO];
[_highlightWindow setBackgroundColor:[NSColor clearColor]];
[_highlightWindow setOpaque:NO];
[_highlightWindow setIgnoresMouseEvents:YES];
[_highlightWindow setReleasedWhenClosed:NO];
_highlightView = [[WebNodeHighlightView alloc] initWithWebNodeHighlight:self];
[_highlightWindow setContentView:_highlightView];
[_highlightView release];
return self;
}
- (void)dealloc
{
ASSERT(!_highlightWindow);
ASSERT(!_targetView);
ASSERT(!_highlightView);
[super dealloc];
}
- (void)attach
{
ASSERT(_targetView);
ASSERT([_targetView window]);
ASSERT(_highlightWindow);
if (!_highlightWindow || !_targetView || ![_targetView window])
return;
[[_targetView window] addChildWindow:_highlightWindow ordered:NSWindowAbove];
// Observe both frame-changed and bounds-changed notifications because either one could leave
// the highlight incorrectly positioned with respect to the target view. We need to do this for
// the entire superview hierarchy to handle scrolling, bars coming and going, etc.
// (without making concrete assumptions about the view hierarchy).
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
for (NSView *v = _targetView; v; v = [v superview]) {
[notificationCenter addObserver:self selector:@selector(_repositionHighlightWindow) name:NSViewFrameDidChangeNotification object:v];
[notificationCenter addObserver:self selector:@selector(_repositionHighlightWindow) name:NSViewBoundsDidChangeNotification object:v];
}
if (_delegate && [_delegate respondsToSelector:@selector(didAttachWebNodeHighlight:)])
[_delegate didAttachWebNodeHighlight:self];
}
- (id)delegate
{
return _delegate;
}
- (void)detach
{
if (!_highlightWindow) {
ASSERT(!_targetView);
return;
}
if (_delegate && [_delegate respondsToSelector:@selector(willDetachWebNodeHighlight:)])
[_delegate willDetachWebNodeHighlight:self];
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter removeObserver:self name:NSViewFrameDidChangeNotification object:nil];
[notificationCenter removeObserver:self name:NSViewBoundsDidChangeNotification object:nil];
[[_highlightWindow parentWindow] removeChildWindow:_highlightWindow];
[_highlightWindow release];
_highlightWindow = nil;
[_targetView release];
_targetView = nil;
// We didn't retain _highlightView, but we do need to tell it to forget about us, so it doesn't
// try to send our delegate messages after we've been dealloc'ed, e.g.
[_highlightView detachFromWebNodeHighlight];
_highlightView = nil;
}
- (WebNodeHighlightView *)highlightView
{
return _highlightView;
}
- (void)setDelegate:(id)delegate
{
// The delegate is not retained, as usual in Cocoa.
_delegate = delegate;
}
- (void)setNeedsUpdateInTargetViewRect:(NSRect)rect
{
ASSERT(_targetView);
[[_targetView window] disableScreenUpdatesUntilFlush];
// Mark the whole highlight view as needing display since we don't know what areas
// need updated, since the highlight can be larger than the element to show margins.
[_highlightView setNeedsDisplay:YES];
// Redraw highlight view immediately so it updates in sync with the target view.
// This is especially visible when resizing the window, scrolling or with DHTML.
[_highlightView displayIfNeeded];
}
- (NSView *)targetView
{
return _targetView;
}
- (InspectorController*)inspectorController
{
return _inspectorController;
}
@end
@implementation WebNodeHighlight (FileInternal)
- (NSRect)_computeHighlightWindowFrame
{
ASSERT(_targetView);
ASSERT([_targetView window]);
NSRect highlightWindowFrame = [_targetView convertRect:[_targetView visibleRect] toView:nil];
highlightWindowFrame.origin = [[_targetView window] convertBaseToScreen:highlightWindowFrame.origin];
return highlightWindowFrame;
}
- (void)_repositionHighlightWindow
{
// This assertion fires in cases where a new tab is created while the highlight
// is showing (<http://bugs.webkit.org/show_bug.cgi?id=14254>)
ASSERT([_targetView window]);
// Until that bug is fixed, bail out to avoid worse problems where the highlight
// moves to a nonsense location.
if (![_targetView window])
return;
// Disable screen updates so the highlight moves in sync with the view.
[[_targetView window] disableScreenUpdatesUntilFlush];
[_highlightWindow setFrame:[self _computeHighlightWindowFrame] display:YES];
}
@end
|