d3dxof: Do not expect a separator when there is no element.
[wine/multimedia.git] / dlls / shell.dll16 / shell.c
blobb8d99fd6bf99d5387c2a2c19434ff462825ab3f3
1 /*
2 * Shell Library Functions
4 * Copyright 1998 Marcus Meissner
5 * Copyright 2000 Juergen Schmied
6 * Copyright 2002 Eric Pouech
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.
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.
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
23 #include "config.h"
24 #include "wine/port.h"
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>
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"
44 #include "wine/winbase16.h"
45 #include "wine/winuser16.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(shell);
51 extern HINSTANCE WINAPI WOWShellExecute(HWND hWnd, LPCSTR lpOperation,LPCSTR lpFile,
52 LPCSTR lpParameters,LPCSTR lpDirectory,
53 INT iShowCmd, void *callback);
55 #define HICON_16(h32) (LOWORD(h32))
56 #define HINSTANCE_32(h16) ((HINSTANCE)(ULONG_PTR)(h16))
57 #define HINSTANCE_16(h32) (LOWORD(h32))
59 typedef struct { /* structure for dropped files */
60 WORD wSize;
61 POINT16 ptMousePos;
62 BOOL16 fInNonClientArea;
63 /* memory block with filenames follows */
64 } DROPFILESTRUCT16, *LPDROPFILESTRUCT16;
66 static const char lpstrMsgWndCreated[] = "OTHERWINDOWCREATED";
67 static const char lpstrMsgWndDestroyed[] = "OTHERWINDOWDESTROYED";
68 static const char lpstrMsgShellActivate[] = "ACTIVATESHELLWINDOW";
70 static HWND SHELL_hWnd = 0;
71 static HHOOK SHELL_hHook = 0;
72 static UINT uMsgWndCreated = 0;
73 static UINT uMsgWndDestroyed = 0;
74 static UINT uMsgShellActivate = 0;
76 static HICON convert_icon_to_32( HICON16 icon16 )
78 CURSORICONINFO *info = GlobalLock16( icon16 );
79 void *and_bits = info + 1;
80 void *xor_bits = (BYTE *)and_bits + info->nHeight * 2 * ((info->nWidth + 15) / 16);
81 HICON ret = CreateIcon( 0, info->nWidth, info->nHeight, info->bPlanes, info->bBitsPerPixel,
82 and_bits, xor_bits );
83 GlobalUnlock16( icon16 );
84 return ret;
87 /***********************************************************************
88 * DllEntryPoint [SHELL.101]
90 * Initialization code for shell.dll. Automatically loads the
91 * 32-bit shell32.dll to allow thunking up to 32-bit code.
93 * RETURNS
94 * Success: TRUE. Initialization completed successfully.
95 * Failure: FALSE.
97 BOOL WINAPI SHELL_DllEntryPoint(DWORD Reason, HINSTANCE16 hInst,
98 WORD ds, WORD HeapSize, DWORD res1, WORD res2)
100 return TRUE;
103 /*************************************************************************
104 * DragAcceptFiles [SHELL.9]
106 void WINAPI DragAcceptFiles16(HWND16 hWnd, BOOL16 b)
108 DragAcceptFiles(HWND_32(hWnd), b);
111 /*************************************************************************
112 * DragQueryFile [SHELL.11]
114 UINT16 WINAPI DragQueryFile16(
115 HDROP16 hDrop,
116 WORD wFile,
117 LPSTR lpszFile,
118 WORD wLength)
120 LPSTR lpDrop;
121 UINT i = 0;
122 LPDROPFILESTRUCT16 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
124 TRACE("(%04x, %x, %p, %u)\n", hDrop,wFile,lpszFile,wLength);
126 if(!lpDropFileStruct) goto end;
128 lpDrop = (LPSTR) lpDropFileStruct + lpDropFileStruct->wSize;
130 while (i++ < wFile)
132 while (*lpDrop++); /* skip filename */
133 if (!*lpDrop)
135 i = (wFile == 0xFFFF) ? i : 0;
136 goto end;
140 i = strlen(lpDrop);
141 if (!lpszFile ) goto end; /* needed buffer size */
142 lstrcpynA (lpszFile, lpDrop, wLength);
143 end:
144 GlobalUnlock16(hDrop);
145 return i;
148 /*************************************************************************
149 * DragFinish [SHELL.12]
151 void WINAPI DragFinish16(HDROP16 h)
153 TRACE("\n");
154 GlobalFree16(h);
158 /*************************************************************************
159 * DragQueryPoint [SHELL.13]
161 BOOL16 WINAPI DragQueryPoint16(HDROP16 hDrop, POINT16 *p)
163 LPDROPFILESTRUCT16 lpDropFileStruct;
164 BOOL16 bRet;
165 TRACE("\n");
166 lpDropFileStruct = (LPDROPFILESTRUCT16) GlobalLock16(hDrop);
168 memcpy(p,&lpDropFileStruct->ptMousePos,sizeof(POINT16));
169 bRet = lpDropFileStruct->fInNonClientArea;
171 GlobalUnlock16(hDrop);
172 return bRet;
175 /*************************************************************************
176 * FindExecutable (SHELL.21)
178 HINSTANCE16 WINAPI FindExecutable16( LPCSTR lpFile, LPCSTR lpDirectory,
179 LPSTR lpResult )
180 { return HINSTANCE_16(FindExecutableA( lpFile, lpDirectory, lpResult ));
183 /*************************************************************************
184 * AboutDlgProc (SHELL.33)
186 BOOL16 WINAPI AboutDlgProc16( HWND16 hWnd, UINT16 msg, WPARAM16 wParam,
187 LPARAM lParam )
189 FIXME( "stub\n" );
190 return FALSE;
194 /*************************************************************************
195 * ShellAbout (SHELL.22)
197 BOOL16 WINAPI ShellAbout16( HWND16 hWnd, LPCSTR szApp, LPCSTR szOtherStuff, HICON16 icon16 )
199 HICON icon = convert_icon_to_32( icon16 );
200 BOOL ret = ShellAboutA( HWND_32(hWnd), szApp, szOtherStuff, icon );
201 DestroyIcon( icon );
202 return ret;
205 /*************************************************************************
206 * InternalExtractIcon [SHELL.39]
208 * This abortion is called directly by Progman
210 HGLOBAL16 WINAPI InternalExtractIcon16(HINSTANCE16 hInstance,
211 LPCSTR lpszExeFileName, UINT16 nIconIndex, WORD n )
213 HGLOBAL16 hRet = 0;
214 HICON16 *RetPtr = NULL;
216 TRACE("(%04x,file %s,start %d,extract %d\n",
217 hInstance, lpszExeFileName, nIconIndex, n);
219 if (!n)
220 return 0;
222 hRet = GlobalAlloc16(GMEM_FIXED | GMEM_ZEROINIT, sizeof(*RetPtr) * n);
223 RetPtr = GlobalLock16(hRet);
225 if (nIconIndex == (UINT16)-1) /* get number of icons */
227 RetPtr[0] = PrivateExtractIconsA(lpszExeFileName, 0, 0, 0, NULL, NULL, 0, LR_DEFAULTCOLOR);
229 else
231 UINT ret;
232 HICON *icons;
234 icons = HeapAlloc(GetProcessHeap(), 0, n * sizeof(*icons));
235 ret = PrivateExtractIconsA(lpszExeFileName, nIconIndex,
236 GetSystemMetrics(SM_CXICON),
237 GetSystemMetrics(SM_CYICON),
238 icons, NULL, n, LR_DEFAULTCOLOR);
239 if ((ret != 0xffffffff) && ret)
241 int i;
242 for (i = 0; i < n; i++) RetPtr[i] = HICON_16(icons[i]);
244 else
246 GlobalFree16(hRet);
247 hRet = 0;
249 HeapFree(GetProcessHeap(), 0, icons);
251 return hRet;
254 /*************************************************************************
255 * ExtractIcon (SHELL.34)
257 HICON16 WINAPI ExtractIcon16( HINSTANCE16 hInstance, LPCSTR lpszExeFileName,
258 UINT16 nIconIndex )
259 { TRACE("\n");
260 return HICON_16(ExtractIconA(HINSTANCE_32(hInstance), lpszExeFileName, nIconIndex));
263 /*************************************************************************
264 * ExtractIconEx (SHELL.40)
266 HICON16 WINAPI ExtractIconEx16(
267 LPCSTR lpszFile, INT16 nIconIndex, HICON16 *phiconLarge,
268 HICON16 *phiconSmall, UINT16 nIcons
270 HICON *ilarge,*ismall;
271 UINT16 ret;
272 int i;
274 if (phiconLarge)
275 ilarge = HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
276 else
277 ilarge = NULL;
278 if (phiconSmall)
279 ismall = HeapAlloc(GetProcessHeap(),0,nIcons*sizeof(HICON));
280 else
281 ismall = NULL;
282 ret = HICON_16(ExtractIconExA(lpszFile,nIconIndex,ilarge,ismall,nIcons));
283 if (ilarge) {
284 for (i=0;i<nIcons;i++)
285 phiconLarge[i]=HICON_16(ilarge[i]);
286 HeapFree(GetProcessHeap(),0,ilarge);
288 if (ismall) {
289 for (i=0;i<nIcons;i++)
290 phiconSmall[i]=HICON_16(ismall[i]);
291 HeapFree(GetProcessHeap(),0,ismall);
293 return ret;
296 /*************************************************************************
297 * ExtractAssociatedIcon [SHELL.36]
299 * Return icon for given file (either from file itself or from associated
300 * executable) and patch parameters if needed.
302 HICON16 WINAPI ExtractAssociatedIcon16(HINSTANCE16 hInst, LPSTR lpIconPath, LPWORD lpiIcon)
304 return HICON_16(ExtractAssociatedIconA(HINSTANCE_32(hInst), lpIconPath,
305 lpiIcon));
308 /*************************************************************************
309 * FindEnvironmentString [SHELL.38]
311 * Returns a pointer into the DOS environment... Ugh.
313 static LPSTR SHELL_FindString(LPSTR lpEnv, LPCSTR entry)
314 { UINT16 l;
316 TRACE("\n");
318 l = strlen(entry);
319 for( ; *lpEnv ; lpEnv+=strlen(lpEnv)+1 )
320 { if( strncasecmp(lpEnv, entry, l) )
321 continue;
322 if( !*(lpEnv+l) )
323 return (lpEnv + l); /* empty entry */
324 else if ( *(lpEnv+l)== '=' )
325 return (lpEnv + l + 1);
327 return NULL;
330 /**********************************************************************/
332 SEGPTR WINAPI FindEnvironmentString16(LPCSTR str)
333 { SEGPTR spEnv;
334 LPSTR lpEnv,lpString;
335 TRACE("\n");
337 spEnv = GetDOSEnvironment16();
339 lpEnv = MapSL(spEnv);
340 lpString = (spEnv)?SHELL_FindString(lpEnv, str):NULL;
342 if( lpString ) /* offset should be small enough */
343 return spEnv + (lpString - lpEnv);
344 return 0;
347 /*************************************************************************
348 * DoEnvironmentSubst [SHELL.37]
350 * Replace %KEYWORD% in the str with the value of variable KEYWORD
351 * from "DOS" environment. If it is not found the %KEYWORD% is left
352 * intact. If the buffer is too small, str is not modified.
354 * PARAMS
355 * str [I] '\0' terminated string with %keyword%.
356 * [O] '\0' terminated string with %keyword% substituted.
357 * length [I] size of str.
359 * RETURNS
360 * str length in the LOWORD and 1 in HIWORD if subst was successful.
362 DWORD WINAPI DoEnvironmentSubst16(LPSTR str,WORD length)
364 LPSTR lpEnv = MapSL(GetDOSEnvironment16());
365 LPSTR lpstr = str;
366 LPSTR lpend;
367 LPSTR lpBuffer = HeapAlloc( GetProcessHeap(), 0, length);
368 WORD bufCnt = 0;
369 WORD envKeyLen;
370 LPSTR lpKey;
371 WORD retStatus = 0;
372 WORD retLength = length;
374 CharToOemA(str,str);
376 TRACE("accept %s\n", str);
378 while( *lpstr && bufCnt <= length - 1 ) {
379 if ( *lpstr != '%' ) {
380 lpBuffer[bufCnt++] = *lpstr++;
381 continue;
384 for( lpend = lpstr + 1; *lpend && *lpend != '%'; lpend++) /**/;
386 envKeyLen = lpend - lpstr - 1;
387 if( *lpend != '%' || envKeyLen == 0)
388 goto err; /* "%\0" or "%%" found; back off and whine */
390 *lpend = '\0';
391 lpKey = SHELL_FindString(lpEnv, lpstr+1);
392 *lpend = '%';
393 if( lpKey ) {
394 int l = strlen(lpKey);
396 if( bufCnt + l > length - 1 )
397 goto err;
399 memcpy(lpBuffer + bufCnt, lpKey, l);
400 bufCnt += l;
401 } else { /* Keyword not found; Leave the %KEYWORD% intact */
402 if( bufCnt + envKeyLen + 2 > length - 1 )
403 goto err;
405 memcpy(lpBuffer + bufCnt, lpstr, envKeyLen + 2);
406 bufCnt += envKeyLen + 2;
409 lpstr = lpend + 1;
412 if (!*lpstr && bufCnt <= length - 1) {
413 memcpy(str,lpBuffer, bufCnt);
414 str[bufCnt] = '\0';
415 retLength = bufCnt + 1;
416 retStatus = 1;
419 err:
420 if (!retStatus)
421 WARN("-- Env subst aborted - string too short or invalid input\n");
422 TRACE("-- return %s\n", str);
424 OemToCharA(str,str);
425 HeapFree( GetProcessHeap(), 0, lpBuffer);
427 return (DWORD)MAKELONG(retLength, retStatus);
430 /*************************************************************************
431 * SHELL_HookProc
433 * 32-bit version of the system-wide WH_SHELL hook.
435 static LRESULT WINAPI SHELL_HookProc(INT code, WPARAM wParam, LPARAM lParam)
437 TRACE("%i, %lx, %08lx\n", code, wParam, lParam );
439 if (SHELL_hWnd)
441 switch( code )
443 case HSHELL_WINDOWCREATED:
444 PostMessageA( SHELL_hWnd, uMsgWndCreated, wParam, 0 );
445 break;
446 case HSHELL_WINDOWDESTROYED:
447 PostMessageA( SHELL_hWnd, uMsgWndDestroyed, wParam, 0 );
448 break;
449 case HSHELL_ACTIVATESHELLWINDOW:
450 PostMessageA( SHELL_hWnd, uMsgShellActivate, wParam, 0 );
451 break;
454 return CallNextHookEx( SHELL_hHook, code, wParam, lParam );
457 /*************************************************************************
458 * ShellHookProc [SHELL.103]
459 * System-wide WH_SHELL hook.
461 LRESULT WINAPI ShellHookProc16(INT16 code, WPARAM16 wParam, LPARAM lParam)
463 return SHELL_HookProc( code, wParam, lParam );
466 /*************************************************************************
467 * RegisterShellHook [SHELL.102]
469 BOOL WINAPI RegisterShellHook16(HWND16 hWnd, UINT16 uAction)
471 TRACE("%04x [%u]\n", hWnd, uAction );
473 switch( uAction )
475 case 2: /* register hWnd as a shell window */
476 if( !SHELL_hHook )
478 SHELL_hHook = SetWindowsHookExA( WH_SHELL, SHELL_HookProc,
479 GetModuleHandleA("shell32.dll"), 0 );
480 if ( SHELL_hHook )
482 uMsgWndCreated = RegisterWindowMessageA( lpstrMsgWndCreated );
483 uMsgWndDestroyed = RegisterWindowMessageA( lpstrMsgWndDestroyed );
484 uMsgShellActivate = RegisterWindowMessageA( lpstrMsgShellActivate );
486 else
487 WARN("-- unable to install ShellHookProc()!\n");
490 if ( SHELL_hHook )
491 return ((SHELL_hWnd = HWND_32(hWnd)) != 0);
492 break;
494 default:
495 WARN("-- unknown code %i\n", uAction );
496 SHELL_hWnd = 0; /* just in case */
498 return FALSE;
502 /***********************************************************************
503 * DriveType (SHELL.262)
505 UINT16 WINAPI DriveType16( UINT16 drive )
507 UINT ret;
508 char path[] = "A:\\";
509 path[0] += drive;
510 ret = GetDriveTypeA(path);
511 switch(ret) /* some values are not supported in Win16 */
513 case DRIVE_CDROM:
514 ret = DRIVE_REMOTE;
515 break;
516 case DRIVE_NO_ROOT_DIR:
517 ret = DRIVE_UNKNOWN;
518 break;
520 return ret;
524 /* 0 and 1 are valid rootkeys in win16 shell.dll and are used by
525 * some programs. Do not remove those cases. -MM
527 static inline void fix_win16_hkey( HKEY *hkey )
529 if (*hkey == 0 || *hkey == (HKEY)1) *hkey = HKEY_CLASSES_ROOT;
532 /******************************************************************************
533 * RegOpenKey [SHELL.1]
535 DWORD WINAPI RegOpenKey16( HKEY hkey, LPCSTR name, PHKEY retkey )
537 fix_win16_hkey( &hkey );
538 return RegOpenKeyA( hkey, name, retkey );
541 /******************************************************************************
542 * RegCreateKey [SHELL.2]
544 DWORD WINAPI RegCreateKey16( HKEY hkey, LPCSTR name, PHKEY retkey )
546 fix_win16_hkey( &hkey );
547 return RegCreateKeyA( hkey, name, retkey );
550 /******************************************************************************
551 * RegCloseKey [SHELL.3]
553 DWORD WINAPI RegCloseKey16( HKEY hkey )
555 fix_win16_hkey( &hkey );
556 return RegCloseKey( hkey );
559 /******************************************************************************
560 * RegDeleteKey [SHELL.4]
562 DWORD WINAPI RegDeleteKey16( HKEY hkey, LPCSTR name )
564 fix_win16_hkey( &hkey );
565 return RegDeleteKeyA( hkey, name );
568 /******************************************************************************
569 * RegSetValue [SHELL.5]
571 DWORD WINAPI RegSetValue16( HKEY hkey, LPCSTR name, DWORD type, LPCSTR data, DWORD count )
573 fix_win16_hkey( &hkey );
574 return RegSetValueA( hkey, name, type, data, count );
577 /******************************************************************************
578 * RegQueryValue [SHELL.6]
580 * NOTES
581 * Is this HACK still applicable?
583 * HACK
584 * The 16bit RegQueryValue doesn't handle selectorblocks anyway, so we just
585 * mask out the high 16 bit. This (not so much incidentally) hopefully fixes
586 * Aldus FH4)
588 DWORD WINAPI RegQueryValue16( HKEY hkey, LPCSTR name, LPSTR data, LPDWORD count
591 fix_win16_hkey( &hkey );
592 if (count) *count &= 0xffff;
593 return RegQueryValueA( hkey, name, data, (LONG*) count );
596 /******************************************************************************
597 * RegEnumKey [SHELL.7]
599 DWORD WINAPI RegEnumKey16( HKEY hkey, DWORD index, LPSTR name, DWORD name_len )
601 fix_win16_hkey( &hkey );
602 return RegEnumKeyA( hkey, index, name, name_len );
605 /*************************************************************************
606 * SHELL_Execute16 [Internal]
608 static UINT_PTR SHELL_Execute16(const WCHAR *lpCmd, WCHAR *env, BOOL shWait,
609 const SHELLEXECUTEINFOW *psei, LPSHELLEXECUTEINFOW psei_out)
611 UINT ret;
612 char sCmd[MAX_PATH];
613 WideCharToMultiByte(CP_ACP, 0, lpCmd, -1, sCmd, MAX_PATH, NULL, NULL);
614 ret = WinExec16(sCmd, (UINT16)psei->nShow);
615 psei_out->hInstApp = HINSTANCE_32(ret);
616 return ret;
619 /*************************************************************************
620 * ShellExecute [SHELL.20]
622 HINSTANCE16 WINAPI ShellExecute16( HWND16 hWnd, LPCSTR lpOperation,
623 LPCSTR lpFile, LPCSTR lpParameters,
624 LPCSTR lpDirectory, INT16 iShowCmd )
626 return HINSTANCE_16( WOWShellExecute( HWND_32(hWnd), lpOperation, lpFile, lpParameters,
627 lpDirectory, iShowCmd, SHELL_Execute16 ));
631 /*************************************************************************
632 * RunDLL_CallEntry16
634 * Only exported from shell32 on Windows, probably imported
635 * from shell through the 16/32 thunks.
637 void WINAPI RunDLL_CallEntry16( DWORD proc, HWND hwnd, HINSTANCE inst, LPCSTR cmdline, INT cmdshow )
639 WORD args[5];
640 SEGPTR cmdline_seg;
642 TRACE( "proc %x hwnd %p inst %p cmdline %s cmdshow %d\n",
643 proc, hwnd, inst, debugstr_a(cmdline), cmdshow );
645 cmdline_seg = MapLS( cmdline );
646 args[4] = HWND_16(hwnd);
647 args[3] = MapHModuleLS(inst);
648 args[2] = SELECTOROF(cmdline_seg);
649 args[1] = OFFSETOF(cmdline_seg);
650 args[0] = cmdshow;
651 WOWCallback16Ex( proc, WCB16_PASCAL, sizeof(args), args, NULL );
652 UnMapLS( cmdline_seg );