Windows build fixes
[WebKit-https.git] / WebKitTools / DumpRenderTree / win / EditingDelegate.cpp
1 /*
2  * Copyright (C) 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
29 #include "DumpRenderTree.h"
30 #include "EditingDelegate.h"
31
32 #include "LayoutTestController.h"
33 #include <WebCore/COMPtr.h>
34 #include <wtf/Platform.h>
35 #include <JavaScriptCore/Assertions.h>
36 #include <string>
37 #include <tchar.h>
38
39 using std::wstring;
40
41 EditingDelegate::EditingDelegate()
42     : m_refCount(1)
43     , m_acceptsEditing(true)
44 {
45 }
46
47 // IUnknown
48 HRESULT STDMETHODCALLTYPE EditingDelegate::QueryInterface(REFIID riid, void** ppvObject)
49 {
50     *ppvObject = 0;
51     if (IsEqualGUID(riid, IID_IUnknown))
52         *ppvObject = static_cast<IWebEditingDelegate*>(this);
53     else if (IsEqualGUID(riid, IID_IWebEditingDelegate))
54         *ppvObject = static_cast<IWebEditingDelegate*>(this);
55     else
56         return E_NOINTERFACE;
57
58     AddRef();
59     return S_OK;
60 }
61
62 ULONG STDMETHODCALLTYPE EditingDelegate::AddRef(void)
63 {
64     return ++m_refCount;
65 }
66
67 ULONG STDMETHODCALLTYPE EditingDelegate::Release(void)
68 {
69     ULONG newRef = --m_refCount;
70     if (!newRef)
71         delete this;
72
73     return newRef;
74 }
75
76 static wstring dumpPath(IDOMNode* node)
77 {
78     ASSERT(node);
79
80     wstring result;
81
82     BSTR name;
83     if (FAILED(node->nodeName(&name)))
84         return result;
85     result.assign(name, SysStringLen(name));
86     SysFreeString(name);
87
88     COMPtr<IDOMNode> parent;
89     if (SUCCEEDED(node->parentNode(parent.adoptionPointer())))
90         result += TEXT(" > ") + dumpPath(parent.get());
91
92     return result;
93 }
94
95 static wstring dump(IDOMRange* range)
96 {
97     ASSERT(range);
98
99     int startOffset;
100     if (FAILED(range->startOffset(&startOffset)))
101         return 0;
102
103     int endOffset;
104     if (FAILED(range->endOffset(&endOffset)))
105         return 0;
106
107     COMPtr<IDOMNode> startContainer;
108     if (FAILED(range->startContainer(startContainer.adoptionPointer())))
109         return 0;
110
111     COMPtr<IDOMNode> endContainer;
112     if (FAILED(range->endContainer(endContainer.adoptionPointer())))
113         return 0;
114
115     wchar_t buffer[1024];
116     _snwprintf(buffer, ARRAYSIZE(buffer), L"range from %ld of %s to %ld of %s", startOffset, dumpPath(startContainer.get()), endOffset, dumpPath(endContainer.get()));
117     return buffer;
118 }
119
120 HRESULT STDMETHODCALLTYPE EditingDelegate::shouldBeginEditingInDOMRange( 
121     /* [in] */ IWebView* webView,
122     /* [in] */ IDOMRange* range,
123     /* [retval][out] */ BOOL* result)
124 {
125     if (!result) {
126         ASSERT_NOT_REACHED();
127         return E_POINTER;
128     }
129
130     if (::layoutTestController->dumpEditingCallbacks() && !done)
131         _tprintf(TEXT("EDITING DELEGATE: shouldBeginEditingInDOMRange:%s\n"), dump(range));
132
133     *result = m_acceptsEditing;
134     return S_OK;
135 }
136
137 HRESULT STDMETHODCALLTYPE EditingDelegate::shouldEndEditingInDOMRange( 
138     /* [in] */ IWebView* webView,
139     /* [in] */ IDOMRange* range,
140     /* [retval][out] */ BOOL* result)
141 {
142     if (!result) {
143         ASSERT_NOT_REACHED();
144         return E_POINTER;
145     }
146
147     if (::layoutTestController->dumpEditingCallbacks() && !done)
148         _tprintf(TEXT("EDITING DELEGATE: shouldEndEditingInDOMRange:%s\n"), dump(range));
149
150     *result = m_acceptsEditing;
151     return S_OK;
152 }
153
154 HRESULT STDMETHODCALLTYPE EditingDelegate::shouldInsertNode( 
155     /* [in] */ IWebView* webView,
156     /* [in] */ IDOMNode* node,
157     /* [in] */ IDOMRange* range,
158     /* [in] */ WebViewInsertAction action)
159 {
160     static LPCTSTR insertactionstring[] = {
161         TEXT("WebViewInsertActionTyped"),
162         TEXT("WebViewInsertActionPasted"),
163         TEXT("WebViewInsertActionDropped"),
164     };
165
166     if (::layoutTestController->dumpEditingCallbacks() && !done)
167         _tprintf(TEXT("EDITING DELEGATE: shouldInsertNode:%s replacingDOMRange:%s givenAction:%s\n"), dumpPath(node), dump(range), insertactionstring[action]);
168
169     return S_OK;
170 }
171
172 HRESULT STDMETHODCALLTYPE EditingDelegate::shouldInsertText( 
173     /* [in] */ IWebView* webView,
174     /* [in] */ BSTR text,
175     /* [in] */ IDOMRange* range,
176     /* [in] */ WebViewInsertAction action,
177     /* [retval][out] */ BOOL* result)
178 {
179     if (!result) {
180         ASSERT_NOT_REACHED();
181         return E_POINTER;
182     }
183
184     static LPCTSTR insertactionstring[] = {
185         TEXT("WebViewInsertActionTyped"),
186         TEXT("WebViewInsertActionPasted"),
187         TEXT("WebViewInsertActionDropped"),
188     };
189
190     if (::layoutTestController->dumpEditingCallbacks() && !done)
191         _tprintf(TEXT("EDITING DELEGATE: shouldInsertText:%s replacingDOMRange:%s givenAction:%s\n"), text ? text : TEXT(""), dump(range), insertactionstring[action]);
192
193     *result = m_acceptsEditing;
194     return S_OK;
195 }
196
197 HRESULT STDMETHODCALLTYPE EditingDelegate::shouldDeleteDOMRange( 
198     /* [in] */ IWebView* webView,
199     /* [in] */ IDOMRange* range,
200     /* [retval][out] */ BOOL* result)
201 {
202     if (!result) {
203         ASSERT_NOT_REACHED();
204         return E_POINTER;
205     }
206
207     if (::layoutTestController->dumpEditingCallbacks() && !done)
208         _tprintf(TEXT("EDITING DELEGATE: shouldDeleteDOMRange:%s\n"), dump(range));
209
210     *result = m_acceptsEditing;
211     return S_OK;
212 }
213
214 HRESULT STDMETHODCALLTYPE EditingDelegate::shouldChangeSelectedDOMRange( 
215     /* [in] */ IWebView* webView,
216     /* [in] */ IDOMRange* currentRange,
217     /* [in] */ IDOMRange* proposedRange,
218     /* [in] */ WebSelectionAffinity selectionAffinity,
219     /* [in] */ BOOL stillSelecting,
220     /* [retval][out] */ BOOL* result)
221 {
222     if (!result) {
223         ASSERT_NOT_REACHED();
224         return E_POINTER;
225     }
226
227     static LPCTSTR affinitystring[] = {
228         TEXT("NSSelectionAffinityUpstream"),
229         TEXT("NSSelectionAffinityDownstream")
230     };
231     static LPCTSTR boolstring[] = {
232         TEXT("FALSE"),
233         TEXT("TRUE")
234     };
235
236     if (::layoutTestController->dumpEditingCallbacks() && !done)
237         _tprintf(TEXT("EDITING DELEGATE: shouldChangeSelectedDOMRange:%s toDOMRange:%s affinity:%s stillSelecting:%s\n"), dump(currentRange), dump(proposedRange), affinitystring[selectionAffinity], boolstring[stillSelecting]);
238
239     *result = m_acceptsEditing;
240     return S_OK;
241 }
242
243 HRESULT STDMETHODCALLTYPE EditingDelegate::shouldApplyStyle( 
244     /* [in] */ IWebView* webView,
245     /* [in] */ IDOMCSSStyleDeclaration* style,
246     /* [in] */ IDOMRange* range,
247     /* [retval][out] */ BOOL* result)
248 {
249     if (!result) {
250         ASSERT_NOT_REACHED();
251         return E_POINTER;
252     }
253
254     if (::layoutTestController->dumpEditingCallbacks() && !done)
255         _tprintf(TEXT("EDITING DELEGATE: shouldApplyStyle:%s toElementsInDOMRange:%s\n"), TEXT("'style description'")/*[[style description] UTF8String]*/, dump(range));
256
257     *result = m_acceptsEditing;
258     return S_OK;
259 }
260
261 HRESULT STDMETHODCALLTYPE EditingDelegate::shouldChangeTypingStyle( 
262     /* [in] */ IWebView* webView,
263     /* [in] */ IDOMCSSStyleDeclaration* currentStyle,
264     /* [in] */ IDOMCSSStyleDeclaration* proposedStyle,
265     /* [retval][out] */ BOOL* result)
266 {
267     if (!result) {
268         ASSERT_NOT_REACHED();
269         return E_POINTER;
270     }
271
272     if (::layoutTestController->dumpEditingCallbacks() && !done)
273         _tprintf(TEXT("EDITING DELEGATE: shouldChangeTypingStyle:%s toStyle:%s\n"), TEXT("'currentStyle description'"), TEXT("'proposedStyle description'"));
274
275     *result = m_acceptsEditing;
276     return S_OK;
277 }
278
279 HRESULT STDMETHODCALLTYPE EditingDelegate::doPlatformCommand( 
280     /* [in] */ IWebView *webView,
281     /* [in] */ BSTR command,
282     /* [retval][out] */ BOOL *result)
283 {
284     if (!result) {
285         ASSERT_NOT_REACHED();
286         return E_POINTER;
287     }
288
289     if (::layoutTestController->dumpEditingCallbacks() && !done)
290         _tprintf(TEXT("EDITING DELEGATE: doPlatformCommand:%s\n"), command ? command : TEXT(""));
291
292     *result = m_acceptsEditing;
293     return S_OK;
294 }
295
296 HRESULT STDMETHODCALLTYPE EditingDelegate::webViewDidBeginEditing( 
297     /* [in] */ IWebNotification* notification)
298 {
299     if (::layoutTestController->dumpEditingCallbacks() && !done) {
300         BSTR name;
301         notification->name(&name);
302         _tprintf(TEXT("EDITING DELEGATE: webViewDidBeginEditing:%s\n"), name ? name : TEXT(""));
303         SysFreeString(name);
304     }
305     return S_OK;
306 }
307
308 HRESULT STDMETHODCALLTYPE EditingDelegate::webViewDidChange( 
309     /* [in] */ IWebNotification *notification)
310 {
311     if (::layoutTestController->dumpEditingCallbacks() && !done) {
312         BSTR name;
313         notification->name(&name);
314         _tprintf(TEXT("EDITING DELEGATE: webViewDidBeginEditing:%s\n"), name ? name : TEXT(""));
315         SysFreeString(name);
316     }
317     return S_OK;
318 }
319
320 HRESULT STDMETHODCALLTYPE EditingDelegate::webViewDidEndEditing( 
321     /* [in] */ IWebNotification *notification)
322 {
323     if (::layoutTestController->dumpEditingCallbacks() && !done) {
324         BSTR name;
325         notification->name(&name);
326         _tprintf(TEXT("EDITING DELEGATE: webViewDidEndEditing:%s\n"), name ? name : TEXT(""));
327         SysFreeString(name);
328     }
329     return S_OK;
330 }
331
332 HRESULT STDMETHODCALLTYPE EditingDelegate::webViewDidChangeTypingStyle( 
333     /* [in] */ IWebNotification *notification)
334 {
335     if (::layoutTestController->dumpEditingCallbacks() && !done) {
336         BSTR name;
337         notification->name(&name);
338         _tprintf(TEXT("EDITING DELEGATE: webViewDidChangeTypingStyle:%s\n"), name ? name : TEXT(""));
339         SysFreeString(name);
340     }
341     return S_OK;
342 }
343
344 HRESULT STDMETHODCALLTYPE EditingDelegate::webViewDidChangeSelection( 
345     /* [in] */ IWebNotification *notification)
346 {
347     if (::layoutTestController->dumpEditingCallbacks() && !done) {
348         BSTR name;
349         notification->name(&name);
350         _tprintf(TEXT("EDITING DELEGATE: webViewDidChangeSelection:%s\n"), name ? name : TEXT(""));
351         SysFreeString(name);
352     }
353     return S_OK;
354 }