Clear the internal paint flag in the server before returning a
[wine.git] / dlls / shdocvw / shdocvw_main.c
blob458ad9d67ff864716a874d0aff65e82ea5213b60
1 /*
2 * SHDOCVW - Internet Explorer Web Control
4 * Copyright 2001 John R. Sheets (for CodeWeavers)
5 * Copyright 2004 Mike McCormack (for CodeWeavers)
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "config.h"
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <string.h>
28 #define COBJMACROS
29 #define COM_NO_WINDOWS_H
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winreg.h"
34 #include "winuser.h"
35 #include "winnls.h"
36 #include "ole2.h"
37 #include "shlwapi.h"
39 #include "shdocvw.h"
40 #include "uuids.h"
41 #include "urlmon.h"
43 #include "wine/unicode.h"
44 #include "wine/debug.h"
46 #include "initguid.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(shdocvw);
50 LONG SHDOCVW_refCount = 0;
52 static const WCHAR szMozDlPath[] = {
53 'S','o','f','t','w','a','r','e','\\','W','i','n','e','\\',
54 's','h','d','o','c','v','w',0
57 DEFINE_GUID( CLSID_MozillaBrowser, 0x1339B54C,0x3453,0x11D2,0x93,0xB9,0x00,0x00,0x00,0x00,0x00,0x00);
59 typedef HRESULT (WINAPI *fnGetClassObject)(REFCLSID rclsid, REFIID iid, LPVOID *ppv);
60 typedef HRESULT (WINAPI *fnCanUnloadNow)(void);
62 HINSTANCE shdocvw_hinstance = 0;
63 static HMODULE SHDOCVW_hshell32 = 0;
64 static HMODULE hMozCtl = (HMODULE)~0UL;
67 /* convert a guid to a wide character string */
68 static void SHDOCVW_guid2wstr( const GUID *guid, LPWSTR wstr )
70 char str[40];
72 sprintf(str, "{%08lX-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
73 guid->Data1, guid->Data2, guid->Data3,
74 guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
75 guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7] );
76 MultiByteToWideChar( CP_ACP, 0, str, -1, wstr, 40 );
79 static BOOL SHDOCVW_GetMozctlPath( LPWSTR szPath, DWORD sz )
81 DWORD r, type;
82 BOOL ret = FALSE;
83 HKEY hkey;
84 static const WCHAR szPre[] = {
85 'S','o','f','t','w','a','r','e','\\',
86 'C','l','a','s','s','e','s','\\',
87 'C','L','S','I','D','\\',0 };
88 static const WCHAR szPost[] = {
89 '\\','I','n','p','r','o','c','S','e','r','v','e','r','3','2',0 };
90 WCHAR szRegPath[(sizeof(szPre)+sizeof(szPost))/sizeof(WCHAR)+40];
92 strcpyW( szRegPath, szPre );
93 SHDOCVW_guid2wstr( &CLSID_MozillaBrowser, &szRegPath[strlenW(szRegPath)] );
94 strcatW( szRegPath, szPost );
96 TRACE("key = %s\n", debugstr_w( szRegPath ) );
98 r = RegOpenKeyW( HKEY_LOCAL_MACHINE, szRegPath, &hkey );
99 if( r != ERROR_SUCCESS )
100 return FALSE;
102 r = RegQueryValueExW( hkey, NULL, NULL, &type, (LPBYTE)szPath, &sz );
103 ret = ( r == ERROR_SUCCESS ) && ( type == REG_SZ );
104 RegCloseKey( hkey );
106 return ret;
109 /*************************************************************************
110 * SHDOCVW DllMain
112 BOOL WINAPI DllMain(HINSTANCE hinst, DWORD fdwReason, LPVOID fImpLoad)
114 TRACE("%p 0x%lx %p\n", hinst, fdwReason, fImpLoad);
115 switch (fdwReason)
117 case DLL_PROCESS_ATTACH:
118 shdocvw_hinstance = hinst;
119 break;
120 case DLL_PROCESS_DETACH:
121 if (SHDOCVW_hshell32) FreeLibrary(SHDOCVW_hshell32);
122 if (hMozCtl && hMozCtl != (HMODULE)~0UL) FreeLibrary(hMozCtl);
123 break;
125 return TRUE;
128 /*************************************************************************
129 * DllCanUnloadNow (SHDOCVW.@)
131 HRESULT WINAPI SHDOCVW_DllCanUnloadNow(void)
133 HRESULT moz_can_unload = S_FALSE;
134 fnCanUnloadNow pCanUnloadNow;
136 if (hMozCtl)
138 pCanUnloadNow = (fnCanUnloadNow)
139 GetProcAddress(hMozCtl, "DllCanUnloadNow");
140 moz_can_unload = pCanUnloadNow();
142 else
143 moz_can_unload = S_OK;
145 if (moz_can_unload == S_OK && SHDOCVW_refCount == 0)
146 return S_OK;
148 return S_FALSE;
151 /*************************************************************************
152 * SHDOCVW_TryDownloadMozillaControl
154 typedef struct _IBindStatusCallbackImpl {
155 IBindStatusCallbackVtbl *vtbl;
156 DWORD ref;
157 HWND hDialog;
158 BOOL *pbCancelled;
159 } IBindStatusCallbackImpl;
161 static HRESULT WINAPI
162 dlQueryInterface( IBindStatusCallback* This, REFIID riid, void** ppvObject )
164 if (ppvObject == NULL) return E_POINTER;
166 if( IsEqualIID(riid, &IID_IUnknown) ||
167 IsEqualIID(riid, &IID_IBindStatusCallback))
169 IBindStatusCallback_AddRef( This );
170 *ppvObject = This;
171 return S_OK;
173 return E_NOINTERFACE;
176 static ULONG WINAPI dlAddRef( IBindStatusCallback* iface )
178 IBindStatusCallbackImpl *This = (IBindStatusCallbackImpl *) iface;
180 SHDOCVW_LockModule();
182 return InterlockedIncrement( &This->ref );
185 static ULONG WINAPI dlRelease( IBindStatusCallback* iface )
187 IBindStatusCallbackImpl *This = (IBindStatusCallbackImpl *) iface;
188 DWORD ref = InterlockedDecrement( &This->ref );
190 if( !ref )
192 DestroyWindow( This->hDialog );
193 HeapFree( GetProcessHeap(), 0, This );
196 SHDOCVW_UnlockModule();
198 return ref;
201 static HRESULT WINAPI
202 dlOnStartBinding( IBindStatusCallback* iface, DWORD dwReserved, IBinding* pib)
204 ERR("\n");
205 return S_OK;
208 static HRESULT WINAPI
209 dlGetPriority( IBindStatusCallback* iface, LONG* pnPriority)
211 ERR("\n");
212 return S_OK;
215 static HRESULT WINAPI
216 dlOnLowResource( IBindStatusCallback* iface, DWORD reserved)
218 ERR("\n");
219 return S_OK;
222 static HRESULT WINAPI
223 dlOnProgress( IBindStatusCallback* iface, ULONG ulProgress,
224 ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
226 IBindStatusCallbackImpl *This = (IBindStatusCallbackImpl *) iface;
227 HWND hItem;
228 LONG r;
230 hItem = GetDlgItem( This->hDialog, 1000 );
231 if( hItem && ulProgressMax )
232 SendMessageW(hItem,PBM_SETPOS,(ulProgress*100)/ulProgressMax,0);
234 hItem = GetDlgItem(This->hDialog, 104);
235 if( hItem )
236 SendMessageW(hItem,WM_SETTEXT, 0, (LPARAM) szStatusText);
238 SetLastError(0);
239 r = GetWindowLongPtrW( This->hDialog, GWLP_USERDATA );
240 if( r || GetLastError() )
242 *This->pbCancelled = TRUE;
243 ERR("Cancelled\n");
244 return E_ABORT;
247 return S_OK;
250 static HRESULT WINAPI
251 dlOnStopBinding( IBindStatusCallback* iface, HRESULT hresult, LPCWSTR szError)
253 ERR("\n");
254 return S_OK;
257 static HRESULT WINAPI
258 dlGetBindInfo( IBindStatusCallback* iface, DWORD* grfBINDF, BINDINFO* pbindinfo)
260 ERR("\n");
261 return S_OK;
264 static HRESULT WINAPI
265 dlOnDataAvailable( IBindStatusCallback* iface, DWORD grfBSCF,
266 DWORD dwSize, FORMATETC* pformatetc, STGMEDIUM* pstgmed)
268 ERR("\n");
269 return S_OK;
272 static HRESULT WINAPI
273 dlOnObjectAvailable( IBindStatusCallback* iface, REFIID riid, IUnknown* punk)
275 ERR("\n");
276 return S_OK;
279 struct IBindStatusCallbackVtbl dlVtbl =
281 dlQueryInterface,
282 dlAddRef,
283 dlRelease,
284 dlOnStartBinding,
285 dlGetPriority,
286 dlOnLowResource,
287 dlOnProgress,
288 dlOnStopBinding,
289 dlGetBindInfo,
290 dlOnDataAvailable,
291 dlOnObjectAvailable
294 static IBindStatusCallback* create_dl(HWND dlg, BOOL *pbCancelled)
296 IBindStatusCallbackImpl *This;
298 This = HeapAlloc( GetProcessHeap(), 0, sizeof *This );
299 This->vtbl = &dlVtbl;
300 This->ref = 1;
301 This->hDialog = dlg;
302 This->pbCancelled = pbCancelled;
304 return (IBindStatusCallback*) This;
307 static DWORD WINAPI ThreadFunc( LPVOID info )
309 IBindStatusCallback *dl;
310 static const WCHAR szUrlVal[] = {'M','o','z','i','l','l','a','U','r','l',0};
311 WCHAR path[MAX_PATH], szUrl[MAX_PATH];
312 LPWSTR p;
313 STARTUPINFOW si;
314 PROCESS_INFORMATION pi;
315 HWND hDlg = info;
316 DWORD r, sz, type;
317 HKEY hkey;
318 BOOL bCancelled = FALSE;
320 /* find the name of the thing to download */
321 szUrl[0] = 0;
322 r = RegOpenKeyW( HKEY_LOCAL_MACHINE, szMozDlPath, &hkey );
323 if( r == ERROR_SUCCESS )
325 sz = MAX_PATH;
326 r = RegQueryValueExW( hkey, szUrlVal, NULL, &type, (LPBYTE)szUrl, &sz );
327 RegCloseKey( hkey );
329 if( r != ERROR_SUCCESS )
330 goto end;
332 /* built the path for the download */
333 p = strrchrW( szUrl, '/' );
334 if (!p)
335 goto end;
336 if (!GetTempPathW( MAX_PATH, path ))
337 goto end;
338 strcatW( path, p+1 );
340 /* download it */
341 dl = create_dl(info, &bCancelled);
342 r = URLDownloadToFileW( NULL, szUrl, path, 0, dl );
343 if( dl )
344 IBindStatusCallback_Release( dl );
345 if( (r != S_OK) || bCancelled )
346 goto end;
348 /* run it */
349 memset( &si, 0, sizeof si );
350 si.cb = sizeof si;
351 r = CreateProcessW( path, NULL, NULL, NULL, 0, 0, NULL, NULL, &si, &pi );
352 if( !r )
353 goto end;
354 WaitForSingleObject( pi.hProcess, INFINITE );
356 end:
357 EndDialog( hDlg, 0 );
358 return 0;
361 static INT_PTR CALLBACK
362 dlProc ( HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
364 HANDLE hThread;
365 DWORD ThreadId;
366 HWND hItem;
368 switch (uMsg)
370 case WM_INITDIALOG:
371 SetWindowLongPtrW( hwndDlg, GWLP_USERDATA, 0 );
372 hItem = GetDlgItem(hwndDlg, 1000);
373 if( hItem )
375 SendMessageW(hItem,PBM_SETRANGE,0,MAKELPARAM(0,100));
376 SendMessageW(hItem,PBM_SETPOS,0,0);
378 hThread = CreateThread(NULL,0,ThreadFunc,hwndDlg,0,&ThreadId);
379 if (!hThread)
380 return FALSE;
381 return TRUE;
382 case WM_COMMAND:
383 if( wParam == IDCANCEL )
384 SetWindowLongPtrW( hwndDlg, GWLP_USERDATA, 1 );
385 return FALSE;
386 default:
387 return FALSE;
391 static BOOL SHDOCVW_TryDownloadMozillaControl()
393 DWORD r;
394 WCHAR buf[0x100];
395 static const WCHAR szWine[] = { 'W','i','n','e',0 };
396 HANDLE hsem;
398 SetLastError( ERROR_SUCCESS );
399 hsem = CreateSemaphoreA( NULL, 0, 1, "mozctl_install_semaphore");
400 if( GetLastError() != ERROR_ALREADY_EXISTS )
402 LoadStringW( shdocvw_hinstance, 1001, buf, sizeof buf/sizeof(WCHAR) );
403 r = MessageBoxW(NULL, buf, szWine, MB_YESNO | MB_ICONQUESTION);
404 if( r != IDYES )
405 return FALSE;
407 DialogBoxW(shdocvw_hinstance, MAKEINTRESOURCEW(100), 0, dlProc);
409 else
410 WaitForSingleObject( hsem, INFINITE );
411 ReleaseSemaphore( hsem, 1, NULL );
412 CloseHandle( hsem );
414 return TRUE;
417 static BOOL SHDOCVW_TryLoadMozillaControl()
419 WCHAR szPath[MAX_PATH];
420 BOOL bTried = FALSE;
422 if( hMozCtl != (HMODULE)~0UL )
423 return hMozCtl ? TRUE : FALSE;
425 while( 1 )
427 if( SHDOCVW_GetMozctlPath( szPath, sizeof szPath ) )
429 hMozCtl = LoadLibraryExW(szPath, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
430 if( hMozCtl )
431 return TRUE;
433 if( bTried )
435 MESSAGE("You need to install the Mozilla ActiveX control to\n");
436 MESSAGE("use Wine's builtin CLSID_WebBrowser from SHDOCVW.DLL\n");
437 return FALSE;
439 SHDOCVW_TryDownloadMozillaControl();
440 bTried = TRUE;
444 /*************************************************************************
445 * DllGetClassObject (SHDOCVW.@)
447 HRESULT WINAPI SHDOCVW_DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
449 TRACE("\n");
451 if( IsEqualGUID( &CLSID_WebBrowser, rclsid ) &&
452 SHDOCVW_TryLoadMozillaControl() )
454 HRESULT r;
455 fnGetClassObject pGetClassObject;
457 TRACE("WebBrowser class %s\n", debugstr_guid(rclsid) );
459 pGetClassObject = (fnGetClassObject)
460 GetProcAddress( hMozCtl, "DllGetClassObject" );
462 if( !pGetClassObject )
463 return CLASS_E_CLASSNOTAVAILABLE;
464 r = pGetClassObject( &CLSID_MozillaBrowser, riid, ppv );
466 TRACE("r = %08lx *ppv = %p\n", r, *ppv );
468 return r;
471 if (IsEqualGUID(&IID_IClassFactory, riid))
473 /* Pass back our shdocvw class factory */
474 *ppv = (LPVOID)&SHDOCVW_ClassFactory;
475 IClassFactory_AddRef((IClassFactory*)&SHDOCVW_ClassFactory);
477 return S_OK;
480 return CLASS_E_CLASSNOTAVAILABLE;
483 /***********************************************************************
484 * DllGetVersion (SHDOCVW.@)
486 HRESULT WINAPI SHDOCVW_DllGetVersion(DLLVERSIONINFO *info)
488 if (info->cbSize != sizeof(DLLVERSIONINFO)) FIXME("support DLLVERSIONINFO2\n");
490 /* this is what IE6 on Windows 98 reports */
491 info->dwMajorVersion = 6;
492 info->dwMinorVersion = 0;
493 info->dwBuildNumber = 2600;
494 info->dwPlatformID = DLLVER_PLATFORM_WINDOWS;
496 return NOERROR;
499 /*************************************************************************
500 * DllInstall (SHDOCVW.@)
502 HRESULT WINAPI SHDOCVW_DllInstall(BOOL bInstall, LPCWSTR cmdline)
504 FIXME("(%s, %s): stub!\n", bInstall ? "TRUE":"FALSE", debugstr_w(cmdline));
506 return S_OK;
509 /*************************************************************************
510 * SHDOCVW_LoadShell32
512 * makes sure the handle to shell32 is valid
514 BOOL SHDOCVW_LoadShell32(void)
516 if (SHDOCVW_hshell32)
517 return TRUE;
518 return ((SHDOCVW_hshell32 = LoadLibraryA("shell32.dll")) != NULL);
521 /***********************************************************************
522 * @ (SHDOCVW.110)
524 * Called by Win98 explorer.exe main binary, definitely has 0
525 * parameters.
527 DWORD WINAPI WinList_Init(void)
529 FIXME("(), stub!\n");
530 return 0x0deadfeed;
533 /***********************************************************************
534 * @ (SHDOCVW.118)
536 * Called by Win98 explorer.exe main binary, definitely has only one
537 * parameter.
539 static BOOL (WINAPI *pShellDDEInit)(BOOL start) = NULL;
541 BOOL WINAPI ShellDDEInit(BOOL start)
543 TRACE("(%d)\n", start);
545 if (!pShellDDEInit)
547 if (!SHDOCVW_LoadShell32())
548 return FALSE;
549 pShellDDEInit = GetProcAddress(SHDOCVW_hshell32, (LPCSTR)188);
552 if (pShellDDEInit)
553 return pShellDDEInit(start);
554 else
555 return FALSE;
558 /***********************************************************************
559 * @ (SHDOCVW.125)
561 * Called by Win98 explorer.exe main binary, definitely has 0
562 * parameters.
564 DWORD WINAPI RunInstallUninstallStubs(void)
566 FIXME("(), stub!\n");
567 return 0x0deadbee;
570 /***********************************************************************
571 * SetQueryNetSessionCount (SHDOCVW.@)
573 DWORD WINAPI SetQueryNetSessionCount(DWORD arg)
575 FIXME("(%lu), stub!\n", arg);
576 return 0;