jscript: Make RegExp contructor implementation more generic.
[wine] / dlls / shell32 / shell32_main.c
1 /*
2  *                 Shell basics
3  *
4  * Copyright 1998 Marcus Meissner
5  * Copyright 1998 Juergen Schmied (jsch)  *  <juergen.schmied@metronet.de>
6  *
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.
11  *
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.
16  *
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "config.h"
23
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28
29 #define COBJMACROS
30
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winerror.h"
34 #include "winreg.h"
35 #include "dlgs.h"
36 #include "shellapi.h"
37 #include "winuser.h"
38 #include "wingdi.h"
39 #include "shlobj.h"
40 #include "shlwapi.h"
41
42 #include "undocshell.h"
43 #include "pidl.h"
44 #include "shell32_main.h"
45 #include "version.h"
46 #include "shresdef.h"
47
48 #include "wine/debug.h"
49 #include "wine/unicode.h"
50
51 WINE_DEFAULT_DEBUG_CHANNEL(shell);
52
53 extern const char * const SHELL_Authors[];
54
55 #define MORE_DEBUG 1
56 /*************************************************************************
57  * CommandLineToArgvW            [SHELL32.@]
58  *
59  * We must interpret the quotes in the command line to rebuild the argv
60  * array correctly:
61  * - arguments are separated by spaces or tabs
62  * - quotes serve as optional argument delimiters
63  *   '"a b"'   -> 'a b'
64  * - escaped quotes must be converted back to '"'
65  *   '\"'      -> '"'
66  * - an odd number of '\'s followed by '"' correspond to half that number
67  *   of '\' followed by a '"' (extension of the above)
68  *   '\\\"'    -> '\"'
69  *   '\\\\\"'  -> '\\"'
70  * - an even number of '\'s followed by a '"' correspond to half that number
71  *   of '\', plus a regular quote serving as an argument delimiter (which
72  *   means it does not appear in the result)
73  *   'a\\"b c"'   -> 'a\b c'
74  *   'a\\\\"b c"' -> 'a\\b c'
75  * - '\' that are not followed by a '"' are copied literally
76  *   'a\b'     -> 'a\b'
77  *   'a\\b'    -> 'a\\b'
78  *
79  * Note:
80  * '\t' == 0x0009
81  * ' '  == 0x0020
82  * '"'  == 0x0022
83  * '\\' == 0x005c
84  */
85 LPWSTR* WINAPI CommandLineToArgvW(LPCWSTR lpCmdline, int* numargs)
86 {
87     DWORD argc;
88     LPWSTR  *argv;
89     LPCWSTR cs;
90     LPWSTR arg,s,d;
91     LPWSTR cmdline;
92     int in_quotes,bcount;
93
94     if (*lpCmdline==0)
95     {
96         /* Return the path to the executable */
97         DWORD len, size=16;
98
99         argv=LocalAlloc(LMEM_FIXED, size);
100         for (;;)
101         {
102             len = GetModuleFileNameW(0, (LPWSTR)(argv+1), (size-sizeof(LPWSTR))/sizeof(WCHAR));
103             if (!len)
104             {
105                 LocalFree(argv);
106                 return NULL;
107             }
108             if (len < size) break;
109             size*=2;
110             argv=LocalReAlloc(argv, size, 0);
111         }
112         argv[0]=(LPWSTR)(argv+1);
113         if (numargs)
114             *numargs=1;
115
116         return argv;
117     }
118
119     /* to get a writable copy */
120     argc=0;
121     bcount=0;
122     in_quotes=0;
123     cs=lpCmdline;
124     while (1)
125     {
126         if (*cs==0 || ((*cs==0x0009 || *cs==0x0020) && !in_quotes))
127         {
128             /* space */
129             argc++;
130             /* skip the remaining spaces */
131             while (*cs==0x0009 || *cs==0x0020) {
132                 cs++;
133             }
134             if (*cs==0)
135                 break;
136             bcount=0;
137             continue;
138         }
139         else if (*cs==0x005c)
140         {
141             /* '\', count them */
142             bcount++;
143         }
144         else if ((*cs==0x0022) && ((bcount & 1)==0))
145         {
146             /* unescaped '"' */
147             in_quotes=!in_quotes;
148             bcount=0;
149         }
150         else
151         {
152             /* a regular character */
153             bcount=0;
154         }
155         cs++;
156     }
157     /* Allocate in a single lump, the string array, and the strings that go with it.
158      * This way the caller can make a single GlobalFree call to free both, as per MSDN.
159      */
160     argv=LocalAlloc(LMEM_FIXED, argc*sizeof(LPWSTR)+(strlenW(lpCmdline)+1)*sizeof(WCHAR));
161     if (!argv)
162         return NULL;
163     cmdline=(LPWSTR)(argv+argc);
164     strcpyW(cmdline, lpCmdline);
165
166     argc=0;
167     bcount=0;
168     in_quotes=0;
169     arg=d=s=cmdline;
170     while (*s)
171     {
172         if ((*s==0x0009 || *s==0x0020) && !in_quotes)
173         {
174             /* Close the argument and copy it */
175             *d=0;
176             argv[argc++]=arg;
177
178             /* skip the remaining spaces */
179             do {
180                 s++;
181             } while (*s==0x0009 || *s==0x0020);
182
183             /* Start with a new argument */
184             arg=d=s;
185             bcount=0;
186         }
187         else if (*s==0x005c)
188         {
189             /* '\\' */
190             *d++=*s++;
191             bcount++;
192         }
193         else if (*s==0x0022)
194         {
195             /* '"' */
196             if ((bcount & 1)==0)
197             {
198                 /* Preceded by an even number of '\', this is half that
199                  * number of '\', plus a quote which we erase.
200                  */
201                 d-=bcount/2;
202                 in_quotes=!in_quotes;
203                 s++;
204             }
205             else
206             {
207                 /* Preceded by an odd number of '\', this is half that
208                  * number of '\' followed by a '"'
209                  */
210                 d=d-bcount/2-1;
211                 *d++='"';
212                 s++;
213             }
214             bcount=0;
215         }
216         else
217         {
218             /* a regular character */
219             *d++=*s++;
220             bcount=0;
221         }
222     }
223     if (*arg)
224     {
225         *d='\0';
226         argv[argc++]=arg;
227     }
228     if (numargs)
229         *numargs=argc;
230
231     return argv;
232 }
233
234 static DWORD shgfi_get_exe_type(LPCWSTR szFullPath)
235 {
236     BOOL status = FALSE;
237     HANDLE hfile;
238     DWORD BinaryType;
239     IMAGE_DOS_HEADER mz_header;
240     IMAGE_NT_HEADERS nt;
241     DWORD len;
242     char magic[4];
243
244     status = GetBinaryTypeW (szFullPath, &BinaryType);
245     if (!status)
246         return 0;
247     if (BinaryType == SCS_DOS_BINARY || BinaryType == SCS_PIF_BINARY)
248         return 0x4d5a;
249
250     hfile = CreateFileW( szFullPath, GENERIC_READ, FILE_SHARE_READ,
251                          NULL, OPEN_EXISTING, 0, 0 );
252     if ( hfile == INVALID_HANDLE_VALUE )
253         return 0;
254
255     /*
256      * The next section is adapted from MODULE_GetBinaryType, as we need
257      * to examine the image header to get OS and version information. We
258      * know from calling GetBinaryTypeA that the image is valid and either
259      * an NE or PE, so much error handling can be omitted.
260      * Seek to the start of the file and read the header information.
261      */
262
263     SetFilePointer( hfile, 0, NULL, SEEK_SET );
264     ReadFile( hfile, &mz_header, sizeof(mz_header), &len, NULL );
265
266     SetFilePointer( hfile, mz_header.e_lfanew, NULL, SEEK_SET );
267     ReadFile( hfile, magic, sizeof(magic), &len, NULL );
268     if ( *(DWORD*)magic == IMAGE_NT_SIGNATURE )
269     {
270         SetFilePointer( hfile, mz_header.e_lfanew, NULL, SEEK_SET );
271         ReadFile( hfile, &nt, sizeof(nt), &len, NULL );
272         CloseHandle( hfile );
273         /* DLL files are not executable and should return 0 */
274         if (nt.FileHeader.Characteristics & IMAGE_FILE_DLL)
275             return 0;
276         if (nt.OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_GUI)
277         {
278              return IMAGE_NT_SIGNATURE | 
279                    (nt.OptionalHeader.MajorSubsystemVersion << 24) |
280                    (nt.OptionalHeader.MinorSubsystemVersion << 16);
281         }
282         return IMAGE_NT_SIGNATURE;
283     }
284     else if ( *(WORD*)magic == IMAGE_OS2_SIGNATURE )
285     {
286         IMAGE_OS2_HEADER ne;
287         SetFilePointer( hfile, mz_header.e_lfanew, NULL, SEEK_SET );
288         ReadFile( hfile, &ne, sizeof(ne), &len, NULL );
289         CloseHandle( hfile );
290         if (ne.ne_exetyp == 2)
291             return IMAGE_OS2_SIGNATURE | (ne.ne_expver << 16);
292         return 0;
293     }
294     CloseHandle( hfile );
295     return 0;
296 }
297
298 /*************************************************************************
299  * SHELL_IsShortcut             [internal]
300  *
301  * Decide if an item id list points to a shell shortcut
302  */
303 BOOL SHELL_IsShortcut(LPCITEMIDLIST pidlLast)
304 {
305     char szTemp[MAX_PATH];
306     HKEY keyCls;
307     BOOL ret = FALSE;
308
309     if (_ILGetExtension(pidlLast, szTemp, MAX_PATH) &&
310           HCR_MapTypeToValueA(szTemp, szTemp, MAX_PATH, TRUE))
311     {
312         if (ERROR_SUCCESS == RegOpenKeyExA(HKEY_CLASSES_ROOT, szTemp, 0, KEY_QUERY_VALUE, &keyCls))
313         {
314           if (ERROR_SUCCESS == RegQueryValueExA(keyCls, "IsShortcut", NULL, NULL, NULL, NULL))
315             ret = TRUE;
316
317           RegCloseKey(keyCls);
318         }
319     }
320
321     return ret;
322 }
323
324 #define SHGFI_KNOWN_FLAGS \
325     (SHGFI_SMALLICON | SHGFI_OPENICON | SHGFI_SHELLICONSIZE | SHGFI_PIDL | \
326      SHGFI_USEFILEATTRIBUTES | SHGFI_ADDOVERLAYS | SHGFI_OVERLAYINDEX | \
327      SHGFI_ICON | SHGFI_DISPLAYNAME | SHGFI_TYPENAME | SHGFI_ATTRIBUTES | \
328      SHGFI_ICONLOCATION | SHGFI_EXETYPE | SHGFI_SYSICONINDEX | \
329      SHGFI_LINKOVERLAY | SHGFI_SELECTED | SHGFI_ATTR_SPECIFIED)
330
331 /*************************************************************************
332  * SHGetFileInfoW            [SHELL32.@]
333  *
334  */
335 DWORD_PTR WINAPI SHGetFileInfoW(LPCWSTR path,DWORD dwFileAttributes,
336                                 SHFILEINFOW *psfi, UINT sizeofpsfi, UINT flags )
337 {
338     WCHAR szLocation[MAX_PATH], szFullPath[MAX_PATH];
339     int iIndex;
340     DWORD_PTR ret = TRUE;
341     DWORD dwAttributes = 0;
342     IShellFolder * psfParent = NULL;
343     IExtractIconW * pei = NULL;
344     LPITEMIDLIST    pidlLast = NULL, pidl = NULL;
345     HRESULT hr = S_OK;
346     BOOL IconNotYetLoaded=TRUE;
347     UINT uGilFlags = 0;
348
349     TRACE("%s fattr=0x%x sfi=%p(attr=0x%08x) size=0x%x flags=0x%x\n",
350           (flags & SHGFI_PIDL)? "pidl" : debugstr_w(path), dwFileAttributes,
351           psfi, psfi->dwAttributes, sizeofpsfi, flags);
352
353     if (!path)
354         return FALSE;
355
356     /* windows initializes these values regardless of the flags */
357     if (psfi != NULL)
358     {
359         psfi->szDisplayName[0] = '\0';
360         psfi->szTypeName[0] = '\0';
361         psfi->iIcon = 0;
362     }
363
364     if (!(flags & SHGFI_PIDL))
365     {
366         /* SHGetFileInfo should work with absolute and relative paths */
367         if (PathIsRelativeW(path))
368         {
369             GetCurrentDirectoryW(MAX_PATH, szLocation);
370             PathCombineW(szFullPath, szLocation, path);
371         }
372         else
373         {
374             lstrcpynW(szFullPath, path, MAX_PATH);
375         }
376     }
377
378     if (flags & SHGFI_EXETYPE)
379     {
380         if (flags != SHGFI_EXETYPE)
381             return 0;
382         return shgfi_get_exe_type(szFullPath);
383     }
384
385     /*
386      * psfi is NULL normally to query EXE type. If it is NULL, none of the
387      * below makes sense anyway. Windows allows this and just returns FALSE
388      */
389     if (psfi == NULL)
390         return FALSE;
391
392     /*
393      * translate the path into a pidl only when SHGFI_USEFILEATTRIBUTES
394      * is not specified.
395      * The pidl functions fail on not existing file names
396      */
397
398     if (flags & SHGFI_PIDL)
399     {
400         pidl = ILClone((LPCITEMIDLIST)path);
401     }
402     else if (!(flags & SHGFI_USEFILEATTRIBUTES))
403     {
404         hr = SHILCreateFromPathW(szFullPath, &pidl, &dwAttributes);
405     }
406
407     if ((flags & SHGFI_PIDL) || !(flags & SHGFI_USEFILEATTRIBUTES))
408     {
409         /* get the parent shellfolder */
410         if (pidl)
411         {
412             hr = SHBindToParent( pidl, &IID_IShellFolder, (LPVOID*)&psfParent,
413                                 (LPCITEMIDLIST*)&pidlLast );
414             if (SUCCEEDED(hr))
415                 pidlLast = ILClone(pidlLast);
416             ILFree(pidl);
417         }
418         else
419         {
420             ERR("pidl is null!\n");
421             return FALSE;
422         }
423     }
424
425     /* get the attributes of the child */
426     if (SUCCEEDED(hr) && (flags & SHGFI_ATTRIBUTES))
427     {
428         if (!(flags & SHGFI_ATTR_SPECIFIED))
429         {
430             psfi->dwAttributes = 0xffffffff;
431         }
432         if (psfParent)
433             IShellFolder_GetAttributesOf( psfParent, 1, (LPCITEMIDLIST*)&pidlLast,
434                                       &(psfi->dwAttributes) );
435     }
436
437     /* get the displayname */
438     if (SUCCEEDED(hr) && (flags & SHGFI_DISPLAYNAME))
439     {
440         if (flags & SHGFI_USEFILEATTRIBUTES)
441         {
442             lstrcpyW (psfi->szDisplayName, PathFindFileNameW(szFullPath));
443         }
444         else
445         {
446             STRRET str;
447             hr = IShellFolder_GetDisplayNameOf( psfParent, pidlLast,
448                                                 SHGDN_INFOLDER, &str);
449             StrRetToStrNW (psfi->szDisplayName, MAX_PATH, &str, pidlLast);
450         }
451     }
452
453     /* get the type name */
454     if (SUCCEEDED(hr) && (flags & SHGFI_TYPENAME))
455     {
456         static const WCHAR szFile[] = { 'F','i','l','e',0 };
457         static const WCHAR szDashFile[] = { '-','f','i','l','e',0 };
458
459         if (!(flags & SHGFI_USEFILEATTRIBUTES))
460         {
461             char ftype[80];
462
463             _ILGetFileType(pidlLast, ftype, 80);
464             MultiByteToWideChar(CP_ACP, 0, ftype, -1, psfi->szTypeName, 80 );
465         }
466         else
467         {
468             if (dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
469                 strcatW (psfi->szTypeName, szFile);
470             else 
471             {
472                 WCHAR sTemp[64];
473
474                 lstrcpyW(sTemp,PathFindExtensionW(szFullPath));
475                 if (!( HCR_MapTypeToValueW(sTemp, sTemp, 64, TRUE) &&
476                     HCR_MapTypeToValueW(sTemp, psfi->szTypeName, 80, FALSE )))
477                 {
478                     lstrcpynW (psfi->szTypeName, sTemp, 64);
479                     strcatW (psfi->szTypeName, szDashFile);
480                 }
481             }
482         }
483     }
484
485     /* ### icons ###*/
486     if (flags & SHGFI_OPENICON)
487         uGilFlags |= GIL_OPENICON;
488
489     if (flags & SHGFI_LINKOVERLAY)
490         uGilFlags |= GIL_FORSHORTCUT;
491     else if ((flags&SHGFI_ADDOVERLAYS) ||
492              (flags&(SHGFI_ICON|SHGFI_SMALLICON))==SHGFI_ICON)
493     {
494         if (SHELL_IsShortcut(pidlLast))
495             uGilFlags |= GIL_FORSHORTCUT;
496     }
497
498     if (flags & SHGFI_OVERLAYINDEX)
499         FIXME("SHGFI_OVERLAYINDEX unhandled\n");
500
501     if (flags & SHGFI_SELECTED)
502         FIXME("set icon to selected, stub\n");
503
504     if (flags & SHGFI_SHELLICONSIZE)
505         FIXME("set icon to shell size, stub\n");
506
507     /* get the iconlocation */
508     if (SUCCEEDED(hr) && (flags & SHGFI_ICONLOCATION ))
509     {
510         UINT uDummy,uFlags;
511
512         if (flags & SHGFI_USEFILEATTRIBUTES)
513         {
514             if (dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
515             {
516                 lstrcpyW(psfi->szDisplayName, swShell32Name);
517                 psfi->iIcon = -IDI_SHELL_FOLDER;
518             }
519             else
520             {
521                 WCHAR* szExt;
522                 static const WCHAR p1W[] = {'%','1',0};
523                 WCHAR sTemp [MAX_PATH];
524
525                 szExt = PathFindExtensionW(szFullPath);
526                 TRACE("szExt=%s\n", debugstr_w(szExt));
527                 if ( szExt &&
528                      HCR_MapTypeToValueW(szExt, sTemp, MAX_PATH, TRUE) &&
529                      HCR_GetDefaultIconW(sTemp, sTemp, MAX_PATH, &psfi->iIcon))
530                 {
531                     if (lstrcmpW(p1W, sTemp))
532                         strcpyW(psfi->szDisplayName, sTemp);
533                     else
534                     {
535                         /* the icon is in the file */
536                         strcpyW(psfi->szDisplayName, szFullPath);
537                     }
538                 }
539                 else
540                     ret = FALSE;
541             }
542         }
543         else
544         {
545             hr = IShellFolder_GetUIObjectOf(psfParent, 0, 1,
546                 (LPCITEMIDLIST*)&pidlLast, &IID_IExtractIconW,
547                 &uDummy, (LPVOID*)&pei);
548             if (SUCCEEDED(hr))
549             {
550                 hr = IExtractIconW_GetIconLocation(pei, uGilFlags,
551                     szLocation, MAX_PATH, &iIndex, &uFlags);
552
553                 if (uFlags & GIL_NOTFILENAME)
554                     ret = FALSE;
555                 else
556                 {
557                     lstrcpyW (psfi->szDisplayName, szLocation);
558                     psfi->iIcon = iIndex;
559                 }
560                 IExtractIconW_Release(pei);
561             }
562         }
563     }
564
565     /* get icon index (or load icon)*/
566     if (SUCCEEDED(hr) && (flags & (SHGFI_ICON | SHGFI_SYSICONINDEX)))
567     {
568         if (flags & SHGFI_USEFILEATTRIBUTES && !(flags & SHGFI_PIDL))
569         {
570             WCHAR sTemp [MAX_PATH];
571             WCHAR * szExt;
572             int icon_idx=0;
573
574             lstrcpynW(sTemp, szFullPath, MAX_PATH);
575
576             if (dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
577                 psfi->iIcon = SIC_GetIconIndex(swShell32Name, -IDI_SHELL_FOLDER, 0);
578             else
579             {
580                 static const WCHAR p1W[] = {'%','1',0};
581
582                 psfi->iIcon = 0;
583                 szExt = PathFindExtensionW(sTemp);
584                 if ( szExt &&
585                      HCR_MapTypeToValueW(szExt, sTemp, MAX_PATH, TRUE) &&
586                      HCR_GetDefaultIconW(sTemp, sTemp, MAX_PATH, &icon_idx))
587                 {
588                     if (!lstrcmpW(p1W,sTemp))            /* icon is in the file */
589                         strcpyW(sTemp, szFullPath);
590
591                     if (flags & SHGFI_SYSICONINDEX) 
592                     {
593                         psfi->iIcon = SIC_GetIconIndex(sTemp,icon_idx,0);
594                         if (psfi->iIcon == -1)
595                             psfi->iIcon = 0;
596                     }
597                     else 
598                     {
599                         UINT ret;
600                         if (flags & SHGFI_SMALLICON)
601                             ret = PrivateExtractIconsW( sTemp,icon_idx,
602                                 GetSystemMetrics( SM_CXSMICON ),
603                                 GetSystemMetrics( SM_CYSMICON ),
604                                 &psfi->hIcon, 0, 1, 0);
605                         else
606                             ret = PrivateExtractIconsW( sTemp, icon_idx,
607                                 GetSystemMetrics( SM_CXICON),
608                                 GetSystemMetrics( SM_CYICON),
609                                 &psfi->hIcon, 0, 1, 0);
610                         if (ret != 0 && ret != 0xFFFFFFFF)
611                         {
612                             IconNotYetLoaded=FALSE;
613                             psfi->iIcon = icon_idx;
614                         }
615                     }
616                 }
617             }
618         }
619         else
620         {
621             if (!(PidlToSicIndex(psfParent, pidlLast, !(flags & SHGFI_SMALLICON),
622                 uGilFlags, &(psfi->iIcon))))
623             {
624                 ret = FALSE;
625             }
626         }
627         if (ret && (flags & SHGFI_SYSICONINDEX))
628         {
629             if (flags & SHGFI_SMALLICON)
630                 ret = (DWORD_PTR) ShellSmallIconList;
631             else
632                 ret = (DWORD_PTR) ShellBigIconList;
633         }
634     }
635
636     /* icon handle */
637     if (SUCCEEDED(hr) && (flags & SHGFI_ICON) && IconNotYetLoaded)
638     {
639         if (flags & SHGFI_SMALLICON)
640             psfi->hIcon = ImageList_GetIcon( ShellSmallIconList, psfi->iIcon, ILD_NORMAL);
641         else
642             psfi->hIcon = ImageList_GetIcon( ShellBigIconList, psfi->iIcon, ILD_NORMAL);
643     }
644
645     if (flags & ~SHGFI_KNOWN_FLAGS)
646         FIXME("unknown flags %08x\n", flags & ~SHGFI_KNOWN_FLAGS);
647
648     if (psfParent)
649         IShellFolder_Release(psfParent);
650
651     if (hr != S_OK)
652         ret = FALSE;
653
654     SHFree(pidlLast);
655
656 #ifdef MORE_DEBUG
657     TRACE ("icon=%p index=0x%08x attr=0x%08x name=%s type=%s ret=0x%08lx\n",
658            psfi->hIcon, psfi->iIcon, psfi->dwAttributes,
659            debugstr_w(psfi->szDisplayName), debugstr_w(psfi->szTypeName), ret);
660 #endif
661
662     return ret;
663 }
664
665 /*************************************************************************
666  * SHGetFileInfoA            [SHELL32.@]
667  *
668  * Note:
669  *    MSVBVM60.__vbaNew2 expects this function to return a value in range
670  *    1 .. 0x7fff when the function succeeds and flags does not contain
671  *    SHGFI_EXETYPE or SHGFI_SYSICONINDEX (see bug 7701)
672  */
673 DWORD_PTR WINAPI SHGetFileInfoA(LPCSTR path,DWORD dwFileAttributes,
674                                 SHFILEINFOA *psfi, UINT sizeofpsfi,
675                                 UINT flags )
676 {
677     INT len;
678     LPWSTR temppath = NULL;
679     LPCWSTR pathW;
680     DWORD ret;
681     SHFILEINFOW temppsfi;
682
683     if (flags & SHGFI_PIDL)
684     {
685         /* path contains a pidl */
686         pathW = (LPCWSTR)path;
687     }
688     else
689     {
690         len = MultiByteToWideChar(CP_ACP, 0, path, -1, NULL, 0);
691         temppath = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
692         MultiByteToWideChar(CP_ACP, 0, path, -1, temppath, len);
693         pathW = temppath;
694     }
695
696     if (psfi && (flags & SHGFI_ATTR_SPECIFIED))
697         temppsfi.dwAttributes=psfi->dwAttributes;
698
699     if (psfi == NULL)
700         ret = SHGetFileInfoW(pathW, dwFileAttributes, NULL, sizeof(temppsfi), flags);
701     else
702         ret = SHGetFileInfoW(pathW, dwFileAttributes, &temppsfi, sizeof(temppsfi), flags);
703
704     if (psfi)
705     {
706         if(flags & SHGFI_ICON)
707             psfi->hIcon=temppsfi.hIcon;
708         if(flags & (SHGFI_SYSICONINDEX|SHGFI_ICON|SHGFI_ICONLOCATION))
709             psfi->iIcon=temppsfi.iIcon;
710         if(flags & SHGFI_ATTRIBUTES)
711             psfi->dwAttributes=temppsfi.dwAttributes;
712         if(flags & (SHGFI_DISPLAYNAME|SHGFI_ICONLOCATION))
713         {
714             WideCharToMultiByte(CP_ACP, 0, temppsfi.szDisplayName, -1,
715                   psfi->szDisplayName, sizeof(psfi->szDisplayName), NULL, NULL);
716         }
717         if(flags & SHGFI_TYPENAME)
718         {
719             WideCharToMultiByte(CP_ACP, 0, temppsfi.szTypeName, -1,
720                   psfi->szTypeName, sizeof(psfi->szTypeName), NULL, NULL);
721         }
722     }
723
724     HeapFree(GetProcessHeap(), 0, temppath);
725
726     return ret;
727 }
728
729 /*************************************************************************
730  * DuplicateIcon            [SHELL32.@]
731  */
732 HICON WINAPI DuplicateIcon( HINSTANCE hInstance, HICON hIcon)
733 {
734     ICONINFO IconInfo;
735     HICON hDupIcon = 0;
736
737     TRACE("%p %p\n", hInstance, hIcon);
738
739     if (GetIconInfo(hIcon, &IconInfo))
740     {
741         hDupIcon = CreateIconIndirect(&IconInfo);
742
743         /* clean up hbmMask and hbmColor */
744         DeleteObject(IconInfo.hbmMask);
745         DeleteObject(IconInfo.hbmColor);
746     }
747
748     return hDupIcon;
749 }
750
751 /*************************************************************************
752  * ExtractIconA                [SHELL32.@]
753  */
754 HICON WINAPI ExtractIconA(HINSTANCE hInstance, LPCSTR lpszFile, UINT nIconIndex)
755 {   
756     HICON ret;
757     INT len = MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, NULL, 0);
758     LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
759
760     TRACE("%p %s %d\n", hInstance, lpszFile, nIconIndex);
761
762     MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, lpwstrFile, len);
763     ret = ExtractIconW(hInstance, lpwstrFile, nIconIndex);
764     HeapFree(GetProcessHeap(), 0, lpwstrFile);
765
766     return ret;
767 }
768
769 /*************************************************************************
770  * ExtractIconW                [SHELL32.@]
771  */
772 HICON WINAPI ExtractIconW(HINSTANCE hInstance, LPCWSTR lpszFile, UINT nIconIndex)
773 {
774     HICON  hIcon = NULL;
775     UINT ret;
776     UINT cx = GetSystemMetrics(SM_CXICON), cy = GetSystemMetrics(SM_CYICON);
777
778     TRACE("%p %s %d\n", hInstance, debugstr_w(lpszFile), nIconIndex);
779
780     if (nIconIndex == 0xFFFFFFFF)
781     {
782         ret = PrivateExtractIconsW(lpszFile, 0, cx, cy, NULL, NULL, 0, LR_DEFAULTCOLOR);
783         if (ret != 0xFFFFFFFF && ret)
784             return (HICON)(UINT_PTR)ret;
785         return NULL;
786     }
787     else
788         ret = PrivateExtractIconsW(lpszFile, nIconIndex, cx, cy, &hIcon, NULL, 1, LR_DEFAULTCOLOR);
789
790     if (ret == 0xFFFFFFFF)
791         return (HICON)1;
792     else if (ret > 0 && hIcon)
793         return hIcon;
794
795     return NULL;
796 }
797
798 /*************************************************************************
799  * Printer_LoadIconsW        [SHELL32.205]
800  */
801 VOID WINAPI Printer_LoadIconsW(LPCWSTR wsPrinterName, HICON * pLargeIcon, HICON * pSmallIcon)
802 {
803     INT iconindex=IDI_SHELL_PRINTER;
804
805     TRACE("(%s, %p, %p)\n", debugstr_w(wsPrinterName), pLargeIcon, pSmallIcon);
806
807     /* We should check if wsPrinterName is
808        1. the Default Printer or not
809        2. connected or not
810        3. a Local Printer or a Network-Printer
811        and use different Icons
812     */
813     if((wsPrinterName != NULL) && (wsPrinterName[0] != 0))
814     {
815         FIXME("(select Icon by PrinterName %s not implemented)\n", debugstr_w(wsPrinterName));
816     }
817
818     if(pLargeIcon != NULL)
819         *pLargeIcon = LoadImageW(shell32_hInstance,
820                                  (LPCWSTR) MAKEINTRESOURCE(iconindex), IMAGE_ICON,
821                                  0, 0, LR_DEFAULTCOLOR|LR_DEFAULTSIZE);
822
823     if(pSmallIcon != NULL)
824         *pSmallIcon = LoadImageW(shell32_hInstance,
825                                  (LPCWSTR) MAKEINTRESOURCE(iconindex), IMAGE_ICON,
826                                  16, 16, LR_DEFAULTCOLOR);
827 }
828
829 /*************************************************************************
830  * Printers_RegisterWindowW        [SHELL32.213]
831  * used by "printui.dll":
832  * find the Window of the given Type for the specific Printer and 
833  * return the already existent hwnd or open a new window
834  */
835 BOOL WINAPI Printers_RegisterWindowW(LPCWSTR wsPrinter, DWORD dwType,
836             HANDLE * phClassPidl, HWND * phwnd)
837 {
838     FIXME("(%s, %x, %p (%p), %p (%p)) stub!\n", debugstr_w(wsPrinter), dwType,
839                 phClassPidl, (phClassPidl != NULL) ? *(phClassPidl) : NULL,
840                 phwnd, (phwnd != NULL) ? *(phwnd) : NULL);
841
842     return FALSE;
843
844
845 /*************************************************************************
846  * Printers_UnregisterWindow      [SHELL32.214]
847  */
848 VOID WINAPI Printers_UnregisterWindow(HANDLE hClassPidl, HWND hwnd)
849 {
850     FIXME("(%p, %p) stub!\n", hClassPidl, hwnd);
851
852
853 /*************************************************************************/
854
855 typedef struct
856 {
857     LPCWSTR  szApp;
858     LPCWSTR  szOtherStuff;
859     HICON hIcon;
860     HFONT hFont;
861 } ABOUT_INFO;
862
863 #define DROP_FIELD_TOP    (-12)
864
865 static void paint_dropline( HDC hdc, HWND hWnd )
866 {
867     HWND hWndCtl = GetDlgItem(hWnd, IDC_ABOUT_WINE_TEXT);
868     RECT rect;
869
870     if (!hWndCtl) return;
871     GetWindowRect( hWndCtl, &rect );
872     MapWindowPoints( 0, hWnd, (LPPOINT)&rect, 2 );
873     rect.top += DROP_FIELD_TOP;
874     rect.bottom = rect.top + 2;
875     DrawEdge( hdc, &rect, BDR_SUNKENOUTER, BF_RECT );
876 }
877
878 /*************************************************************************
879  * SHHelpShortcuts_RunDLLA        [SHELL32.@]
880  *
881  */
882 DWORD WINAPI SHHelpShortcuts_RunDLLA(DWORD dwArg1, DWORD dwArg2, DWORD dwArg3, DWORD dwArg4)
883 {
884     FIXME("(%x, %x, %x, %x) stub!\n", dwArg1, dwArg2, dwArg3, dwArg4);
885     return 0;
886 }
887
888 /*************************************************************************
889  * SHHelpShortcuts_RunDLLA        [SHELL32.@]
890  *
891  */
892 DWORD WINAPI SHHelpShortcuts_RunDLLW(DWORD dwArg1, DWORD dwArg2, DWORD dwArg3, DWORD dwArg4)
893 {
894     FIXME("(%x, %x, %x, %x) stub!\n", dwArg1, dwArg2, dwArg3, dwArg4);
895     return 0;
896 }
897
898 /*************************************************************************
899  * SHLoadInProc                [SHELL32.@]
900  * Create an instance of specified object class from within
901  * the shell process and release it immediately
902  */
903 HRESULT WINAPI SHLoadInProc (REFCLSID rclsid)
904 {
905     void *ptr = NULL;
906
907     TRACE("%s\n", debugstr_guid(rclsid));
908
909     CoCreateInstance(rclsid, NULL, CLSCTX_INPROC_SERVER, &IID_IUnknown,&ptr);
910     if(ptr)
911     {
912         IUnknown * pUnk = ptr;
913         IUnknown_Release(pUnk);
914         return NOERROR;
915     }
916     return DISP_E_MEMBERNOTFOUND;
917 }
918
919 /*************************************************************************
920  * AboutDlgProc            (internal)
921  */
922 static INT_PTR CALLBACK AboutDlgProc( HWND hWnd, UINT msg, WPARAM wParam,
923                                       LPARAM lParam )
924 {
925     HWND hWndCtl;
926
927     TRACE("\n");
928
929     switch(msg)
930     {
931     case WM_INITDIALOG:
932         {
933             ABOUT_INFO *info = (ABOUT_INFO *)lParam;
934             WCHAR template[512], buffer[512], version[64];
935             extern const char *wine_get_build_id(void);
936
937             if (info)
938             {
939                 const char* const *pstr = SHELL_Authors;
940                 SendDlgItemMessageW(hWnd, stc1, STM_SETICON,(WPARAM)info->hIcon, 0);
941                 GetWindowTextW( hWnd, template, sizeof(template)/sizeof(WCHAR) );
942                 sprintfW( buffer, template, info->szApp );
943                 SetWindowTextW( hWnd, buffer );
944                 SetWindowTextW( GetDlgItem(hWnd, IDC_ABOUT_STATIC_TEXT1), info->szApp );
945                 SetWindowTextW( GetDlgItem(hWnd, IDC_ABOUT_STATIC_TEXT2), info->szOtherStuff );
946                 GetWindowTextW( GetDlgItem(hWnd, IDC_ABOUT_STATIC_TEXT3),
947                                 template, sizeof(template)/sizeof(WCHAR) );
948                 MultiByteToWideChar( CP_UTF8, 0, wine_get_build_id(), -1,
949                                      version, sizeof(version)/sizeof(WCHAR) );
950                 sprintfW( buffer, template, version );
951                 SetWindowTextW( GetDlgItem(hWnd, IDC_ABOUT_STATIC_TEXT3), buffer );
952                 hWndCtl = GetDlgItem(hWnd, IDC_ABOUT_LISTBOX);
953                 SendMessageW( hWndCtl, WM_SETREDRAW, 0, 0 );
954                 SendMessageW( hWndCtl, WM_SETFONT, (WPARAM)info->hFont, 0 );
955                 while (*pstr)
956                 {
957                     /* authors list is in utf-8 format */
958                     MultiByteToWideChar( CP_UTF8, 0, *pstr, -1, buffer, sizeof(buffer)/sizeof(WCHAR) );
959                     SendMessageW( hWndCtl, LB_ADDSTRING, -1, (LPARAM)buffer );
960                     pstr++;
961                 }
962                 SendMessageW( hWndCtl, WM_SETREDRAW, 1, 0 );
963             }
964         }
965         return 1;
966
967     case WM_PAINT:
968         {
969             PAINTSTRUCT ps;
970             HDC hDC = BeginPaint( hWnd, &ps );
971             paint_dropline( hDC, hWnd );
972             EndPaint( hWnd, &ps );
973         }
974     break;
975
976     case WM_COMMAND:
977         if (wParam == IDOK || wParam == IDCANCEL)
978         {
979             EndDialog(hWnd, TRUE);
980             return TRUE;
981         }
982         if (wParam == IDC_ABOUT_LICENSE)
983         {
984             MSGBOXPARAMSW params;
985
986             params.cbSize = sizeof(params);
987             params.hwndOwner = hWnd;
988             params.hInstance = shell32_hInstance;
989             params.lpszText = MAKEINTRESOURCEW(IDS_LICENSE);
990             params.lpszCaption = MAKEINTRESOURCEW(IDS_LICENSE_CAPTION);
991             params.dwStyle = MB_ICONINFORMATION | MB_OK;
992             params.lpszIcon = 0;
993             params.dwContextHelpId = 0;
994             params.lpfnMsgBoxCallback = NULL;
995             params.dwLanguageId = LANG_NEUTRAL;
996             MessageBoxIndirectW( &params );
997         }
998         break;
999     case WM_CLOSE:
1000       EndDialog(hWnd, TRUE);
1001       break;
1002     }
1003
1004     return 0;
1005 }
1006
1007
1008 /*************************************************************************
1009  * ShellAboutA                [SHELL32.288]
1010  */
1011 BOOL WINAPI ShellAboutA( HWND hWnd, LPCSTR szApp, LPCSTR szOtherStuff, HICON hIcon )
1012 {
1013     BOOL ret;
1014     LPWSTR appW = NULL, otherW = NULL;
1015     int len;
1016
1017     if (szApp)
1018     {
1019         len = MultiByteToWideChar(CP_ACP, 0, szApp, -1, NULL, 0);
1020         appW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1021         MultiByteToWideChar(CP_ACP, 0, szApp, -1, appW, len);
1022     }
1023     if (szOtherStuff)
1024     {
1025         len = MultiByteToWideChar(CP_ACP, 0, szOtherStuff, -1, NULL, 0);
1026         otherW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1027         MultiByteToWideChar(CP_ACP, 0, szOtherStuff, -1, otherW, len);
1028     }
1029
1030     ret = ShellAboutW(hWnd, appW, otherW, hIcon);
1031
1032     HeapFree(GetProcessHeap(), 0, otherW);
1033     HeapFree(GetProcessHeap(), 0, appW);
1034     return ret;
1035 }
1036
1037
1038 /*************************************************************************
1039  * ShellAboutW                [SHELL32.289]
1040  */
1041 BOOL WINAPI ShellAboutW( HWND hWnd, LPCWSTR szApp, LPCWSTR szOtherStuff,
1042                              HICON hIcon )
1043 {
1044     ABOUT_INFO info;
1045     LOGFONTW logFont;
1046     BOOL bRet;
1047     static const WCHAR wszSHELL_ABOUT_MSGBOX[] =
1048         {'S','H','E','L','L','_','A','B','O','U','T','_','M','S','G','B','O','X',0};
1049
1050     TRACE("\n");
1051
1052     if (!hIcon) hIcon = LoadImageW( 0, (LPWSTR)IDI_WINLOGO, IMAGE_ICON, 48, 48, LR_SHARED );
1053     info.szApp        = szApp;
1054     info.szOtherStuff = szOtherStuff;
1055     info.hIcon        = hIcon;
1056
1057     SystemParametersInfoW( SPI_GETICONTITLELOGFONT, 0, &logFont, 0 );
1058     info.hFont = CreateFontIndirectW( &logFont );
1059
1060     bRet = DialogBoxParamW( shell32_hInstance, wszSHELL_ABOUT_MSGBOX, hWnd, AboutDlgProc, (LPARAM)&info );
1061     DeleteObject(info.hFont);
1062     return bRet;
1063 }
1064
1065 /*************************************************************************
1066  * FreeIconList (SHELL32.@)
1067  */
1068 void WINAPI FreeIconList( DWORD dw )
1069 {
1070     FIXME("%x: stub\n",dw);
1071 }
1072
1073 /*************************************************************************
1074  * SHLoadNonloadedIconOverlayIdentifiers (SHELL32.@)
1075  */
1076 HRESULT WINAPI SHLoadNonloadedIconOverlayIdentifiers( VOID )
1077 {
1078     FIXME("stub\n");
1079     return S_OK;
1080 }
1081
1082 /***********************************************************************
1083  * DllGetVersion [SHELL32.@]
1084  *
1085  * Retrieves version information of the 'SHELL32.DLL'
1086  *
1087  * PARAMS
1088  *     pdvi [O] pointer to version information structure.
1089  *
1090  * RETURNS
1091  *     Success: S_OK
1092  *     Failure: E_INVALIDARG
1093  *
1094  * NOTES
1095  *     Returns version of a shell32.dll from IE4.01 SP1.
1096  */
1097
1098 HRESULT WINAPI DllGetVersion (DLLVERSIONINFO *pdvi)
1099 {
1100     /* FIXME: shouldn't these values come from the version resource? */
1101     if (pdvi->cbSize == sizeof(DLLVERSIONINFO) ||
1102         pdvi->cbSize == sizeof(DLLVERSIONINFO2))
1103     {
1104         pdvi->dwMajorVersion = WINE_FILEVERSION_MAJOR;
1105         pdvi->dwMinorVersion = WINE_FILEVERSION_MINOR;
1106         pdvi->dwBuildNumber = WINE_FILEVERSION_BUILD;
1107         pdvi->dwPlatformID = WINE_FILEVERSION_PLATFORMID;
1108         if (pdvi->cbSize == sizeof(DLLVERSIONINFO2))
1109         {
1110             DLLVERSIONINFO2 *pdvi2 = (DLLVERSIONINFO2 *)pdvi;
1111
1112             pdvi2->dwFlags = 0;
1113             pdvi2->ullVersion = MAKEDLLVERULL(WINE_FILEVERSION_MAJOR,
1114                                               WINE_FILEVERSION_MINOR,
1115                                               WINE_FILEVERSION_BUILD,
1116                                               WINE_FILEVERSION_PLATFORMID);
1117         }
1118         TRACE("%u.%u.%u.%u\n",
1119               pdvi->dwMajorVersion, pdvi->dwMinorVersion,
1120               pdvi->dwBuildNumber, pdvi->dwPlatformID);
1121         return S_OK;
1122     }
1123     else
1124     {
1125         WARN("wrong DLLVERSIONINFO size from app\n");
1126         return E_INVALIDARG;
1127     }
1128 }
1129
1130 /*************************************************************************
1131  * global variables of the shell32.dll
1132  * all are once per process
1133  *
1134  */
1135 HINSTANCE    shell32_hInstance = 0;
1136 HIMAGELIST   ShellSmallIconList = 0;
1137 HIMAGELIST   ShellBigIconList = 0;
1138
1139
1140 /*************************************************************************
1141  * SHELL32 DllMain
1142  *
1143  * NOTES
1144  *  calling oleinitialize here breaks sone apps.
1145  */
1146 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID fImpLoad)
1147 {
1148     TRACE("%p 0x%x %p\n", hinstDLL, fdwReason, fImpLoad);
1149
1150     switch (fdwReason)
1151     {
1152     case DLL_PROCESS_ATTACH:
1153         shell32_hInstance = hinstDLL;
1154         DisableThreadLibraryCalls(shell32_hInstance);
1155
1156         /* get full path to this DLL for IExtractIconW_fnGetIconLocation() */
1157         GetModuleFileNameW(hinstDLL, swShell32Name, MAX_PATH);
1158         swShell32Name[MAX_PATH - 1] = '\0';
1159
1160         InitCommonControlsEx(NULL);
1161
1162         SIC_Initialize();
1163         InitChangeNotifications();
1164         break;
1165
1166     case DLL_PROCESS_DETACH:
1167         shell32_hInstance = 0;
1168         SIC_Destroy();
1169         FreeChangeNotifications();
1170         break;
1171     }
1172     return TRUE;
1173 }
1174
1175 /*************************************************************************
1176  * DllInstall         [SHELL32.@]
1177  *
1178  * PARAMETERS
1179  *
1180  *    BOOL bInstall - TRUE for install, FALSE for uninstall
1181  *    LPCWSTR pszCmdLine - command line (unused by shell32?)
1182  */
1183
1184 HRESULT WINAPI DllInstall(BOOL bInstall, LPCWSTR cmdline)
1185 {
1186     FIXME("%s %s: stub\n", bInstall ? "TRUE":"FALSE", debugstr_w(cmdline));
1187     return S_OK;        /* indicate success */
1188 }
1189
1190 /***********************************************************************
1191  *              DllCanUnloadNow (SHELL32.@)
1192  */
1193 HRESULT WINAPI DllCanUnloadNow(void)
1194 {
1195     return S_FALSE;
1196 }
1197
1198 /***********************************************************************
1199  *              ExtractVersionResource16W (SHELL32.@)
1200  */
1201 BOOL WINAPI ExtractVersionResource16W(LPWSTR s, DWORD d)
1202 {
1203     FIXME("(%s %x) stub!\n", debugstr_w(s), d);
1204     return FALSE;
1205 }
1206
1207 /***********************************************************************
1208  *              InitNetworkAddressControl (SHELL32.@)
1209  */
1210 BOOL WINAPI InitNetworkAddressControl(void)
1211 {
1212     FIXME("stub\n");
1213     return FALSE;
1214 }
1215
1216 /***********************************************************************
1217  *              ShellHookProc (SHELL32.@)
1218  */
1219 LRESULT CALLBACK ShellHookProc(DWORD a, DWORD b, DWORD c)
1220 {
1221     FIXME("Stub\n");
1222     return 0;
1223 }