msctf: Add ITfThreadMgrEventSink to ThreadMgr.
[wine] / dlls / shell32 / shell.c
1 /*
2  *                              Shell Library Functions
3  *
4  * Copyright 1998 Marcus Meissner
5  * Copyright 2000 Juergen Schmied
6  * Copyright 2002 Eric Pouech
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include "config.h"
24 #include "wine/port.h"
25
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #ifdef HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif
32 #include <ctype.h>
33
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winreg.h"
37 #include "wownt32.h"
38 #include "shellapi.h"
39 #include "winuser.h"
40 #include "wingdi.h"
41 #include "shlobj.h"
42 #include "shlwapi.h"
43
44 #include "wine/winbase16.h"
45 #include "shell32_main.h"
46
47 #include "wine/debug.h"
48
49 WINE_DEFAULT_DEBUG_CHANNEL(shell);
50
51
52 typedef struct {     /* structure for dropped files */
53  WORD     wSize;
54  POINT16  ptMousePos;
55  BOOL16   fInNonClientArea;
56  /* memory block with filenames follows */
57 } DROPFILESTRUCT16, *LPDROPFILESTRUCT16;
58
59 static const char lpstrMsgWndCreated[] = "OTHERWINDOWCREATED";
60 static const char lpstrMsgWndDestroyed[] = "OTHERWINDOWDESTROYED";
61 static const char lpstrMsgShellActivate[] = "ACTIVATESHELLWINDOW";
62
63 static HWND     SHELL_hWnd = 0;
64 static HHOOK    SHELL_hHook = 0;
65 static UINT     uMsgWndCreated = 0;
66 static UINT     uMsgWndDestroyed = 0;
67 static UINT     uMsgShellActivate = 0;
68
69 /***********************************************************************
70  * DllEntryPoint [SHELL.101]
71  *
72  * Initialization code for shell.dll. Automatically loads the
73  * 32-bit shell32.dll to allow thunking up to 32-bit code.
74  *
75  * RETURNS
76  *  Success: TRUE. Initialization completed successfully.
77  *  Failure: FALSE.
78  */
79 BOOL WINAPI SHELL_DllEntryPoint(DWORD Reason, HINSTANCE16 hInst,
80                                 WORD ds, WORD HeapSize, DWORD res1, WORD res2)
81 {
82     return TRUE;
83 }
84
85 /*************************************************************************
86  *                              DragAcceptFiles         [SHELL.9]
87  */
88 void WINAPI DragAcceptFiles16(HWND16 hWnd, BOOL16 b)
89 {
90   DragAcceptFiles(HWND_32(hWnd), b);
91 }
92
93 /*************************************************************************
94  *                              DragQueryFile           [SHELL.11]
95  */
96 UINT16 WINAPI DragQueryFile16(
97         HDROP16 hDrop,
98         WORD wFile,
99         LPSTR lpszFile,
100         WORD wLength)
101 {
102         LPSTR lpDrop;
103         UINT i = 0;
104         LPDROPFILESTRUCT16 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
105
106         TRACE("(%04x, %x, %p, %u)\n", hDrop,wFile,lpszFile,wLength);
107
108         if(!lpDropFileStruct) goto end;
109
110         lpDrop = (LPSTR) lpDropFileStruct + lpDropFileStruct->wSize;
111
112         while (i++ < wFile)
113         {
114           while (*lpDrop++); /* skip filename */
115           if (!*lpDrop)
116           {
117             i = (wFile == 0xFFFF) ? i : 0;
118             goto end;
119           }
120         }
121
122         i = strlen(lpDrop);
123         if (!lpszFile ) goto end;   /* needed buffer size */
124         lstrcpynA (lpszFile, lpDrop, wLength);
125 end:
126         GlobalUnlock16(hDrop);
127         return i;
128 }
129
130 /*************************************************************************
131  *                              DragFinish              [SHELL.12]
132  */
133 void WINAPI DragFinish16(HDROP16 h)
134 {
135     TRACE("\n");
136     GlobalFree16(h);
137 }
138
139
140 /*************************************************************************
141  *                              DragQueryPoint          [SHELL.13]
142  */
143 BOOL16 WINAPI DragQueryPoint16(HDROP16 hDrop, POINT16 *p)
144 {
145   LPDROPFILESTRUCT16 lpDropFileStruct;
146   BOOL16           bRet;
147   TRACE("\n");
148   lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
149
150   memcpy(p,&lpDropFileStruct->ptMousePos,sizeof(POINT16));
151   bRet = lpDropFileStruct->fInNonClientArea;
152
153   GlobalUnlock16(hDrop);
154   return bRet;
155 }
156
157 /*************************************************************************
158  *             FindExecutable   (SHELL.21)
159  */
160 HINSTANCE16 WINAPI FindExecutable16( LPCSTR lpFile, LPCSTR lpDirectory,
161                                      LPSTR lpResult )
162 { return HINSTANCE_16(FindExecutableA( lpFile, lpDirectory, lpResult ));
163 }
164
165 /*************************************************************************
166  *             AboutDlgProc   (SHELL.33)
167  */
168 BOOL16 WINAPI AboutDlgProc16( HWND16 hWnd, UINT16 msg, WPARAM16 wParam,
169                                LPARAM lParam )
170 { return (BOOL16)AboutDlgProc( HWND_32(hWnd), msg, wParam, lParam );
171 }
172
173
174 /*************************************************************************
175  *             ShellAbout   (SHELL.22)
176  */
177 BOOL16 WINAPI ShellAbout16( HWND16 hWnd, LPCSTR szApp, LPCSTR szOtherStuff,
178                             HICON16 hIcon )
179 { return ShellAboutA( HWND_32(hWnd), szApp, szOtherStuff, HICON_32(hIcon) );
180 }
181
182 /*************************************************************************
183  *                      InternalExtractIcon             [SHELL.39]
184  *
185  * This abortion is called directly by Progman
186  */
187 HGLOBAL16 WINAPI InternalExtractIcon16(HINSTANCE16 hInstance,
188                                      LPCSTR lpszExeFileName, UINT16 nIconIndex, WORD n )
189 {
190     HGLOBAL16 hRet = 0;
191     HICON16 *RetPtr = NULL;
192
193         TRACE("(%04x,file %s,start %d,extract %d\n",
194                        hInstance, lpszExeFileName, nIconIndex, n);
195
196         if (!n)
197           return 0;
198
199         hRet = GlobalAlloc16(GMEM_FIXED | GMEM_ZEROINIT, sizeof(*RetPtr) * n);
200         RetPtr = GlobalLock16(hRet);
201
202         if (nIconIndex == (UINT16)-1)  /* get number of icons */
203         {
204           RetPtr[0] = PrivateExtractIconsA(lpszExeFileName, 0, 0, 0, NULL, NULL, 0, LR_DEFAULTCOLOR);
205         }
206         else
207         {
208           UINT ret;
209           HICON *icons;
210
211           icons = HeapAlloc(GetProcessHeap(), 0, n * sizeof(*icons));
212           ret = PrivateExtractIconsA(lpszExeFileName, nIconIndex,
213                                      GetSystemMetrics(SM_CXICON),
214                                      GetSystemMetrics(SM_CYICON),
215                                      icons, NULL, n, LR_DEFAULTCOLOR);
216           if ((ret != 0xffffffff) && ret)
217           {
218             int i;
219             for (i = 0; i < n; i++) RetPtr[i] = HICON_16(icons[i]);
220           }
221           else
222           {
223             GlobalFree16(hRet);
224             hRet = 0;
225           }
226           HeapFree(GetProcessHeap(), 0, icons);
227         }
228         return hRet;
229 }
230
231 /*************************************************************************
232  *             ExtractIcon   (SHELL.34)
233  */
234 HICON16 WINAPI ExtractIcon16( HINSTANCE16 hInstance, LPCSTR lpszExeFileName,
235         UINT16 nIconIndex )
236 {   TRACE("\n");
237     return HICON_16(ExtractIconA(HINSTANCE_32(hInstance), lpszExeFileName, nIconIndex));
238 }
239
240 /*************************************************************************
241  *             ExtractIconEx   (SHELL.40)
242  */
243 HICON16 WINAPI ExtractIconEx16(
244         LPCSTR lpszFile, INT16 nIconIndex, HICON16 *phiconLarge,
245         HICON16 *phiconSmall, UINT16 nIcons
246 ) {
247     HICON       *ilarge,*ismall;
248     UINT16      ret;
249     int         i;
250
251     if (phiconLarge)
252         ilarge = HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
253     else
254         ilarge = NULL;
255     if (phiconSmall)
256         ismall = HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
257     else
258         ismall = NULL;
259     ret = HICON_16(ExtractIconExA(lpszFile,nIconIndex,ilarge,ismall,nIcons));
260     if (ilarge) {
261         for (i=0;i<nIcons;i++)
262             phiconLarge[i]=HICON_16(ilarge[i]);
263         HeapFree(GetProcessHeap(),0,ilarge);
264     }
265     if (ismall) {
266         for (i=0;i<nIcons;i++)
267             phiconSmall[i]=HICON_16(ismall[i]);
268         HeapFree(GetProcessHeap(),0,ismall);
269     }
270     return ret;
271 }
272
273 /*************************************************************************
274  *                              ExtractAssociatedIcon   [SHELL.36]
275  *
276  * Return icon for given file (either from file itself or from associated
277  * executable) and patch parameters if needed.
278  */
279 HICON16 WINAPI ExtractAssociatedIcon16(HINSTANCE16 hInst, LPSTR lpIconPath, LPWORD lpiIcon)
280 {
281     return HICON_16(ExtractAssociatedIconA(HINSTANCE_32(hInst), lpIconPath,
282                     lpiIcon));
283 }
284
285 /*************************************************************************
286  *                              FindEnvironmentString   [SHELL.38]
287  *
288  * Returns a pointer into the DOS environment... Ugh.
289  */
290 static LPSTR SHELL_FindString(LPSTR lpEnv, LPCSTR entry)
291 { UINT16 l;
292
293   TRACE("\n");
294
295   l = strlen(entry);
296   for( ; *lpEnv ; lpEnv+=strlen(lpEnv)+1 )
297   { if( strncasecmp(lpEnv, entry, l) )
298       continue;
299         if( !*(lpEnv+l) )
300             return (lpEnv + l);                 /* empty entry */
301         else if ( *(lpEnv+l)== '=' )
302             return (lpEnv + l + 1);
303     }
304     return NULL;
305 }
306
307 /**********************************************************************/
308
309 SEGPTR WINAPI FindEnvironmentString16(LPCSTR str)
310 { SEGPTR  spEnv;
311   LPSTR lpEnv,lpString;
312   TRACE("\n");
313
314   spEnv = GetDOSEnvironment16();
315
316   lpEnv = MapSL(spEnv);
317   lpString = (spEnv)?SHELL_FindString(lpEnv, str):NULL;
318
319     if( lpString )              /*  offset should be small enough */
320         return spEnv + (lpString - lpEnv);
321     return 0;
322 }
323
324 /*************************************************************************
325  *                              DoEnvironmentSubst      [SHELL.37]
326  *
327  * Replace %KEYWORD% in the str with the value of variable KEYWORD
328  * from "DOS" environment. If it is not found the %KEYWORD% is left
329  * intact. If the buffer is too small, str is not modified.
330  *
331  * PARAMS
332  *  str        [I] '\0' terminated string with %keyword%.
333  *             [O] '\0' terminated string with %keyword% substituted.
334  *  length     [I] size of str.
335  *
336  * RETURNS
337  *  str length in the LOWORD and 1 in HIWORD if subst was successful.
338  */
339 DWORD WINAPI DoEnvironmentSubst16(LPSTR str,WORD length)
340 {
341   LPSTR   lpEnv = MapSL(GetDOSEnvironment16());
342   LPSTR   lpstr = str;
343   LPSTR   lpend;
344   LPSTR   lpBuffer = HeapAlloc( GetProcessHeap(), 0, length);
345   WORD    bufCnt = 0;
346   WORD    envKeyLen;
347   LPSTR   lpKey;
348   WORD    retStatus = 0;
349   WORD    retLength = length;
350
351   CharToOemA(str,str);
352
353   TRACE("accept %s\n", str);
354
355   while( *lpstr && bufCnt <= length - 1 ) {
356      if ( *lpstr != '%' ) {
357         lpBuffer[bufCnt++] = *lpstr++;
358         continue;
359      }
360
361      for( lpend = lpstr + 1; *lpend && *lpend != '%'; lpend++) /**/;
362
363      envKeyLen = lpend - lpstr - 1;
364      if( *lpend != '%' || envKeyLen == 0)
365         goto err; /* "%\0" or "%%" found; back off and whine */
366
367      *lpend = '\0';
368      lpKey = SHELL_FindString(lpEnv, lpstr+1);
369      *lpend = '%';
370      if( lpKey ) {
371          int l = strlen(lpKey);
372
373          if( bufCnt + l > length - 1 )
374                 goto err;
375
376         memcpy(lpBuffer + bufCnt, lpKey, l);
377         bufCnt += l;
378      } else { /* Keyword not found; Leave the %KEYWORD% intact */
379         if( bufCnt + envKeyLen + 2 > length - 1 )
380             goto err;
381
382          memcpy(lpBuffer + bufCnt, lpstr, envKeyLen + 2);
383         bufCnt += envKeyLen + 2;
384      }
385
386      lpstr = lpend + 1;
387   }
388
389   if (!*lpstr && bufCnt <= length - 1) {
390       memcpy(str,lpBuffer, bufCnt);
391       str[bufCnt] = '\0';
392       retLength = bufCnt + 1;
393       retStatus = 1;
394   }
395
396   err:
397   if (!retStatus)
398       WARN("-- Env subst aborted - string too short or invalid input\n");
399   TRACE("-- return %s\n", str);
400
401   OemToCharA(str,str);
402   HeapFree( GetProcessHeap(), 0, lpBuffer);
403
404   return (DWORD)MAKELONG(retLength, retStatus);
405 }
406
407 /*************************************************************************
408  *                              SHELL_HookProc
409  *
410  * 32-bit version of the system-wide WH_SHELL hook.
411  */
412 static LRESULT WINAPI SHELL_HookProc(INT code, WPARAM wParam, LPARAM lParam)
413 {
414     TRACE("%i, %lx, %08lx\n", code, wParam, lParam );
415
416     if (SHELL_hWnd)
417     {
418         switch( code )
419         {
420         case HSHELL_WINDOWCREATED:
421             PostMessageA( SHELL_hWnd, uMsgWndCreated, wParam, 0 );
422             break;
423         case HSHELL_WINDOWDESTROYED:
424             PostMessageA( SHELL_hWnd, uMsgWndDestroyed, wParam, 0 );
425             break;
426         case HSHELL_ACTIVATESHELLWINDOW:
427             PostMessageA( SHELL_hWnd, uMsgShellActivate, wParam, 0 );
428             break;
429         }
430     }
431     return CallNextHookEx( SHELL_hHook, code, wParam, lParam );
432 }
433
434 /*************************************************************************
435  *                              ShellHookProc           [SHELL.103]
436  * System-wide WH_SHELL hook.
437  */
438 LRESULT WINAPI ShellHookProc16(INT16 code, WPARAM16 wParam, LPARAM lParam)
439 {
440     return SHELL_HookProc( code, wParam, lParam );
441 }
442
443 /*************************************************************************
444  *                              RegisterShellHook       [SHELL.102]
445  */
446 BOOL WINAPI RegisterShellHook16(HWND16 hWnd, UINT16 uAction)
447 {
448     TRACE("%04x [%u]\n", hWnd, uAction );
449
450     switch( uAction )
451     {
452     case 2:  /* register hWnd as a shell window */
453         if( !SHELL_hHook )
454         {
455             SHELL_hHook = SetWindowsHookExA( WH_SHELL, SHELL_HookProc,
456                                              GetModuleHandleA("shell32.dll"), 0 );
457             if ( SHELL_hHook )
458             {
459                 uMsgWndCreated = RegisterWindowMessageA( lpstrMsgWndCreated );
460                 uMsgWndDestroyed = RegisterWindowMessageA( lpstrMsgWndDestroyed );
461                 uMsgShellActivate = RegisterWindowMessageA( lpstrMsgShellActivate );
462             }
463             else
464                 WARN("-- unable to install ShellHookProc()!\n");
465         }
466
467         if ( SHELL_hHook )
468             return ((SHELL_hWnd = HWND_32(hWnd)) != 0);
469         break;
470
471     default:
472         WARN("-- unknown code %i\n", uAction );
473         SHELL_hWnd = 0; /* just in case */
474     }
475     return FALSE;
476 }
477
478
479 /***********************************************************************
480  *           DriveType   (SHELL.262)
481  */
482 UINT16 WINAPI DriveType16( UINT16 drive )
483 {
484     UINT ret;
485     char path[] = "A:\\";
486     path[0] += drive;
487     ret = GetDriveTypeA(path);
488     switch(ret)  /* some values are not supported in Win16 */
489     {
490     case DRIVE_CDROM:
491         ret = DRIVE_REMOTE;
492         break;
493     case DRIVE_NO_ROOT_DIR:
494         ret = DRIVE_UNKNOWN;
495         break;
496     }
497     return ret;
498 }
499
500
501 /* 0 and 1 are valid rootkeys in win16 shell.dll and are used by
502  * some programs. Do not remove those cases. -MM
503  */
504 static inline void fix_win16_hkey( HKEY *hkey )
505 {
506     if (*hkey == 0 || *hkey == (HKEY)1) *hkey = HKEY_CLASSES_ROOT;
507 }
508
509 /******************************************************************************
510  *           RegOpenKey   [SHELL.1]
511  */
512 DWORD WINAPI RegOpenKey16( HKEY hkey, LPCSTR name, PHKEY retkey )
513 {
514     fix_win16_hkey( &hkey );
515     return RegOpenKeyA( hkey, name, retkey );
516 }
517
518 /******************************************************************************
519  *           RegCreateKey   [SHELL.2]
520  */
521 DWORD WINAPI RegCreateKey16( HKEY hkey, LPCSTR name, PHKEY retkey )
522 {
523     fix_win16_hkey( &hkey );
524     return RegCreateKeyA( hkey, name, retkey );
525 }
526
527 /******************************************************************************
528  *           RegCloseKey   [SHELL.3]
529  */
530 DWORD WINAPI RegCloseKey16( HKEY hkey )
531 {
532     fix_win16_hkey( &hkey );
533     return RegCloseKey( hkey );
534 }
535
536 /******************************************************************************
537  *           RegDeleteKey   [SHELL.4]
538  */
539 DWORD WINAPI RegDeleteKey16( HKEY hkey, LPCSTR name )
540 {
541     fix_win16_hkey( &hkey );
542     return RegDeleteKeyA( hkey, name );
543 }
544
545 /******************************************************************************
546  *           RegSetValue   [SHELL.5]
547  */
548 DWORD WINAPI RegSetValue16( HKEY hkey, LPCSTR name, DWORD type, LPCSTR data, DWORD count )
549 {
550     fix_win16_hkey( &hkey );
551     return RegSetValueA( hkey, name, type, data, count );
552 }
553
554 /******************************************************************************
555  *           RegQueryValue   [SHELL.6]
556  *
557  * NOTES
558  *    Is this HACK still applicable?
559  *
560  * HACK
561  *    The 16bit RegQueryValue doesn't handle selectorblocks anyway, so we just
562  *    mask out the high 16 bit.  This (not so much incidentally) hopefully fixes
563  *    Aldus FH4)
564  */
565 DWORD WINAPI RegQueryValue16( HKEY hkey, LPCSTR name, LPSTR data, LPDWORD count
566 )
567 {
568     fix_win16_hkey( &hkey );
569     if (count) *count &= 0xffff;
570     return RegQueryValueA( hkey, name, data, (LONG*) count );
571 }
572
573 /******************************************************************************
574  *           RegEnumKey   [SHELL.7]
575  */
576 DWORD WINAPI RegEnumKey16( HKEY hkey, DWORD index, LPSTR name, DWORD name_len )
577 {
578     fix_win16_hkey( &hkey );
579     return RegEnumKeyA( hkey, index, name, name_len );
580 }
581
582 /*************************************************************************
583  *           SHELL_Execute16 [Internal]
584  */
585 static UINT_PTR SHELL_Execute16(const WCHAR *lpCmd, WCHAR *env, BOOL shWait,
586                             const SHELLEXECUTEINFOW *psei, LPSHELLEXECUTEINFOW psei_out)
587 {
588     UINT ret;
589     char sCmd[MAX_PATH];
590     WideCharToMultiByte(CP_ACP, 0, lpCmd, -1, sCmd, MAX_PATH, NULL, NULL);
591     ret = WinExec16(sCmd, (UINT16)psei->nShow);
592     psei_out->hInstApp = HINSTANCE_32(ret);
593     return ret;
594 }
595
596 /*************************************************************************
597  *                              ShellExecute            [SHELL.20]
598  */
599 HINSTANCE16 WINAPI ShellExecute16( HWND16 hWnd, LPCSTR lpOperation,
600                                    LPCSTR lpFile, LPCSTR lpParameters,
601                                    LPCSTR lpDirectory, INT16 iShowCmd )
602 {
603     SHELLEXECUTEINFOW seiW;
604     WCHAR *wVerb = NULL, *wFile = NULL, *wParameters = NULL, *wDirectory = NULL;
605     HANDLE hProcess = 0;
606
607     seiW.lpVerb = lpOperation ? __SHCloneStrAtoW(&wVerb, lpOperation) : NULL;
608     seiW.lpFile = lpFile ? __SHCloneStrAtoW(&wFile, lpFile) : NULL;
609     seiW.lpParameters = lpParameters ? __SHCloneStrAtoW(&wParameters, lpParameters) : NULL;
610     seiW.lpDirectory = lpDirectory ? __SHCloneStrAtoW(&wDirectory, lpDirectory) : NULL;
611
612     seiW.cbSize = sizeof(seiW);
613     seiW.fMask = 0;
614     seiW.hwnd = HWND_32(hWnd);
615     seiW.nShow = iShowCmd;
616     seiW.lpIDList = 0;
617     seiW.lpClass = 0;
618     seiW.hkeyClass = 0;
619     seiW.dwHotKey = 0;
620     seiW.hProcess = hProcess;
621
622     SHELL_execute( &seiW, SHELL_Execute16 );
623
624     SHFree(wVerb);
625     SHFree(wFile);
626     SHFree(wParameters);
627     SHFree(wDirectory);
628
629     return HINSTANCE_16(seiW.hInstApp);
630 }