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