Define some extra SHFGI values.
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 "shlguid.h"
41 #include "shlwapi.h"
42
43 #include "undocshell.h"
44 #include "pidl.h"
45 #include "shell32_main.h"
46 #include "version.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     HGLOBAL hargv;
89     LPWSTR  *argv;
90     LPCWSTR cs;
91     LPWSTR arg,s,d;
92     LPWSTR cmdline;
93     int in_quotes,bcount;
94
95     if (*lpCmdline==0) {
96         /* Return the path to the executable */
97         DWORD len, size=16;
98
99         hargv=GlobalAlloc(size, 0);
100         argv=GlobalLock(hargv);
101         for (;;) {
102             len = GetModuleFileNameW(0, (LPWSTR)(argv+1), size-sizeof(LPWSTR));
103             if (!len) {
104                 GlobalFree(hargv);
105                 return NULL;
106             }
107             if (len < size) break;
108             size*=2;
109             hargv=GlobalReAlloc(hargv, size, 0);
110             argv=GlobalLock(hargv);
111         }
112         argv[0]=(LPWSTR)(argv+1);
113         if (numargs)
114             *numargs=2;
115
116         return argv;
117     }
118
119     /* to get a writeable copy */
120     argc=0;
121     bcount=0;
122     in_quotes=0;
123     cs=lpCmdline;
124     while (1) {
125         if (*cs==0 || ((*cs==0x0009 || *cs==0x0020) && !in_quotes)) {
126             /* space */
127             argc++;
128             /* skip the remaining spaces */
129             while (*cs==0x0009 || *cs==0x0020) {
130                 cs++;
131             }
132             if (*cs==0)
133                 break;
134             bcount=0;
135             continue;
136         } else if (*cs==0x005c) {
137             /* '\', count them */
138             bcount++;
139         } else if ((*cs==0x0022) && ((bcount & 1)==0)) {
140             /* unescaped '"' */
141             in_quotes=!in_quotes;
142             bcount=0;
143         } else {
144             /* a regular character */
145             bcount=0;
146         }
147         cs++;
148     }
149     /* Allocate in a single lump, the string array, and the strings that go with it.
150      * This way the caller can make a single GlobalFree call to free both, as per MSDN.
151      */
152     hargv=GlobalAlloc(0, argc*sizeof(LPWSTR)+(strlenW(lpCmdline)+1)*sizeof(WCHAR));
153     argv=GlobalLock(hargv);
154     if (!argv)
155         return NULL;
156     cmdline=(LPWSTR)(argv+argc);
157     strcpyW(cmdline, lpCmdline);
158
159     argc=0;
160     bcount=0;
161     in_quotes=0;
162     arg=d=s=cmdline;
163     while (*s) {
164         if ((*s==0x0009 || *s==0x0020) && !in_quotes) {
165             /* Close the argument and copy it */
166             *d=0;
167             argv[argc++]=arg;
168
169             /* skip the remaining spaces */
170             do {
171                 s++;
172             } while (*s==0x0009 || *s==0x0020);
173
174             /* Start with a new argument */
175             arg=d=s;
176             bcount=0;
177         } else if (*s==0x005c) {
178             /* '\\' */
179             *d++=*s++;
180             bcount++;
181         } else if (*s==0x0022) {
182             /* '"' */
183             if ((bcount & 1)==0) {
184                 /* Preceeded by an even number of '\', this is half that
185                  * number of '\', plus a quote which we erase.
186                  */
187                 d-=bcount/2;
188                 in_quotes=!in_quotes;
189                 s++;
190             } else {
191                 /* Preceeded by an odd number of '\', this is half that
192                  * number of '\' followed by a '"'
193                  */
194                 d=d-bcount/2-1;
195                 *d++='"';
196                 s++;
197             }
198             bcount=0;
199         } else {
200             /* a regular character */
201             *d++=*s++;
202             bcount=0;
203         }
204     }
205     if (*arg) {
206         *d='\0';
207         argv[argc++]=arg;
208     }
209     if (numargs)
210         *numargs=argc;
211
212     return argv;
213 }
214
215 #define SHGFI_KNOWN_FLAGS \
216     (SHGFI_SMALLICON | SHGFI_OPENICON | SHGFI_SHELLICONSIZE | SHGFI_PIDL | \
217      SHGFI_USEFILEATTRIBUTES | SHGFI_ADDOVERLAYS | SHGFI_OVERLAYINDEX | \
218      SHGFI_ICON | SHGFI_DISPLAYNAME | SHGFI_TYPENAME | SHGFI_ATTRIBUTES | \
219      SHGFI_ICONLOCATION | SHGFI_EXETYPE | SHGFI_SYSICONINDEX | \
220      SHGFI_LINKOVERLAY | SHGFI_SELECTED | SHGFI_ATTR_SPECIFIED)
221
222 /*************************************************************************
223  * SHGetFileInfoW                       [SHELL32.@]
224  *
225  */
226
227 DWORD WINAPI SHGetFileInfoW(LPCWSTR path,DWORD dwFileAttributes,
228                               SHFILEINFOW *psfi, UINT sizeofpsfi,
229                               UINT flags )
230 {
231         WCHAR szLocation[MAX_PATH], szFullPath[MAX_PATH];
232         int iIndex;
233         DWORD ret = TRUE, dwAttributes = 0;
234         IShellFolder * psfParent = NULL;
235         IExtractIconW * pei = NULL;
236         LPITEMIDLIST    pidlLast = NULL, pidl = NULL;
237         HRESULT hr = S_OK;
238         BOOL IconNotYetLoaded=TRUE;
239
240         TRACE("%s fattr=0x%lx sfi=%p(attr=0x%08lx) size=0x%x flags=0x%x\n",
241           (flags & SHGFI_PIDL)? "pidl" : debugstr_w(path), dwFileAttributes,
242           psfi, psfi->dwAttributes, sizeofpsfi, flags);
243
244         if ((flags & SHGFI_USEFILEATTRIBUTES) && (flags & (SHGFI_ATTRIBUTES|SHGFI_EXETYPE|SHGFI_PIDL)))
245           return FALSE;
246
247         /* windows initializes this values regardless of the flags */
248         if (psfi != NULL) {
249             psfi->szDisplayName[0] = '\0';
250             psfi->szTypeName[0] = '\0';
251             psfi->iIcon = 0;
252         }
253
254        if (!(flags & SHGFI_PIDL)){
255             /* SHGitFileInfo should work with absolute and relative paths */
256             if (PathIsRelativeW(path)){
257                 GetCurrentDirectoryW(MAX_PATH, szLocation);
258                 PathCombineW(szFullPath, szLocation, path);
259             } else {
260                 lstrcpynW(szFullPath, path, MAX_PATH);
261             }
262         }
263
264         if (flags & SHGFI_EXETYPE) {
265           BOOL status = FALSE;
266           HANDLE hfile;
267           DWORD BinaryType;
268           IMAGE_DOS_HEADER mz_header;
269           IMAGE_NT_HEADERS nt;
270           DWORD len;
271           char magic[4];
272
273           if (flags != SHGFI_EXETYPE) return 0;
274
275          status = GetBinaryTypeW (szFullPath, &BinaryType);
276           if (!status) return 0;
277           if ((BinaryType == SCS_DOS_BINARY)
278                 || (BinaryType == SCS_PIF_BINARY)) return 0x4d5a;
279
280          hfile = CreateFileW( szFullPath, GENERIC_READ, FILE_SHARE_READ,
281                 NULL, OPEN_EXISTING, 0, 0 );
282           if ( hfile == INVALID_HANDLE_VALUE ) return 0;
283
284         /* The next section is adapted from MODULE_GetBinaryType, as we need
285          * to examine the image header to get OS and version information. We
286          * know from calling GetBinaryTypeA that the image is valid and either
287          * an NE or PE, so much error handling can be omitted.
288          * Seek to the start of the file and read the header information.
289          */
290
291           SetFilePointer( hfile, 0, NULL, SEEK_SET );
292           ReadFile( hfile, &mz_header, sizeof(mz_header), &len, NULL );
293
294          SetFilePointer( hfile, mz_header.e_lfanew, NULL, SEEK_SET );
295          ReadFile( hfile, magic, sizeof(magic), &len, NULL );
296          if ( *(DWORD*)magic      == IMAGE_NT_SIGNATURE )
297          {
298              SetFilePointer( hfile, mz_header.e_lfanew, NULL, SEEK_SET );
299              ReadFile( hfile, &nt, sizeof(nt), &len, NULL );
300               CloseHandle( hfile );
301               if (nt.OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_GUI) {
302                  return IMAGE_NT_SIGNATURE
303                         | (nt.OptionalHeader.MajorSubsystemVersion << 24)
304                         | (nt.OptionalHeader.MinorSubsystemVersion << 16);
305               }
306               return IMAGE_NT_SIGNATURE;
307           }
308          else if ( *(WORD*)magic == IMAGE_OS2_SIGNATURE )
309          {
310              IMAGE_OS2_HEADER ne;
311              SetFilePointer( hfile, mz_header.e_lfanew, NULL, SEEK_SET );
312              ReadFile( hfile, &ne, sizeof(ne), &len, NULL );
313               CloseHandle( hfile );
314              if (ne.ne_exetyp == 2) return IMAGE_OS2_SIGNATURE
315                         | (ne.ne_expver << 16);
316               return 0;
317           }
318           CloseHandle( hfile );
319           return 0;
320       }
321
322       /* psfi is NULL normally to query EXE type. If it is NULL, none of the
323        * below makes sense anyway. Windows allows this and just returns FALSE */
324       if (psfi == NULL) return FALSE;
325
326         /* translate the path into a pidl only when SHGFI_USEFILEATTRIBUTES
327          * is not specified.
328            The pidl functions fail on not existing file names */
329
330         if (flags & SHGFI_PIDL) {
331             pidl = ILClone((LPCITEMIDLIST)path);
332         } else if (!(flags & SHGFI_USEFILEATTRIBUTES)) {
333            hr = SHILCreateFromPathW(szFullPath, &pidl, &dwAttributes);
334         }
335
336         if ((flags & SHGFI_PIDL) || !(flags & SHGFI_USEFILEATTRIBUTES))
337         {
338            /* get the parent shellfolder */
339            if (pidl) {
340               hr = SHBindToParent(pidl, &IID_IShellFolder, (LPVOID*)&psfParent, (LPCITEMIDLIST*)&pidlLast);
341               ILFree(pidl);
342            } else {
343               ERR("pidl is null!\n");
344               return FALSE;
345            }
346         }
347
348         /* get the attributes of the child */
349         if (SUCCEEDED(hr) && (flags & SHGFI_ATTRIBUTES))
350         {
351           if (!(flags & SHGFI_ATTR_SPECIFIED))
352           {
353             psfi->dwAttributes = 0xffffffff;
354           }
355           IShellFolder_GetAttributesOf(psfParent, 1, (LPCITEMIDLIST*)&pidlLast, &(psfi->dwAttributes));
356         }
357
358         /* get the displayname */
359         if (SUCCEEDED(hr) && (flags & SHGFI_DISPLAYNAME))
360         {
361           if (flags & SHGFI_USEFILEATTRIBUTES)
362           {
363            lstrcpyW (psfi->szDisplayName, PathFindFileNameW(szFullPath));
364           }
365           else
366           {
367             STRRET str;
368             hr = IShellFolder_GetDisplayNameOf(psfParent, pidlLast, SHGDN_INFOLDER, &str);
369             StrRetToStrNW (psfi->szDisplayName, MAX_PATH, &str, pidlLast);
370           }
371         }
372
373         /* get the type name */
374         if (SUCCEEDED(hr) && (flags & SHGFI_TYPENAME))
375         {
376             static const WCHAR szFile[] = { 'F','i','l','e',0 };
377             static const WCHAR szDashFile[] = { '-','f','i','l','e',0 };
378             if (!(flags & SHGFI_USEFILEATTRIBUTES))
379             {
380                 char ftype[80];
381                 _ILGetFileType(pidlLast, ftype, 80);
382                 MultiByteToWideChar(CP_ACP, 0, ftype, -1, psfi->szTypeName, 80 );
383             }
384             else
385             {
386                 if (dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
387                    strcatW (psfi->szTypeName, szFile);
388                 else 
389                 {
390                    WCHAR sTemp[64];
391                    lstrcpyW(sTemp,PathFindExtensionW(szFullPath));
392                    if (!( HCR_MapTypeToValueW(sTemp, sTemp, 64, TRUE)
393                         && HCR_MapTypeToValueW(sTemp, psfi->szTypeName, 80, FALSE )))
394                    {
395                        lstrcpynW (psfi->szTypeName, sTemp, 64);
396                        strcatW (psfi->szTypeName, szDashFile);
397                    }
398                 }
399             }
400         }
401
402         /* ### icons ###*/
403         if (flags & SHGFI_ADDOVERLAYS)
404           FIXME("SHGFI_ADDOVERLAYS unhandled\n");
405
406         if (flags & SHGFI_OVERLAYINDEX)
407           FIXME("SHGFI_OVERLAYINDEX unhandled\n");
408
409         if (flags & SHGFI_LINKOVERLAY)
410           FIXME("set icon to link, stub\n");
411
412         if (flags & SHGFI_SELECTED)
413           FIXME("set icon to selected, stub\n");
414
415         if (flags & SHGFI_SHELLICONSIZE)
416           FIXME("set icon to shell size, stub\n");
417
418         /* get the iconlocation */
419         if (SUCCEEDED(hr) && (flags & SHGFI_ICONLOCATION ))
420         {
421           UINT uDummy,uFlags;
422           hr = IShellFolder_GetUIObjectOf(psfParent, 0, 1, (LPCITEMIDLIST*)&pidlLast, &IID_IExtractIconA, &uDummy, (LPVOID*)&pei);
423
424           if (SUCCEEDED(hr))
425           {
426            hr = IExtractIconW_GetIconLocation(pei, (flags & SHGFI_OPENICON)? GIL_OPENICON : 0,szLocation, MAX_PATH, &iIndex, &uFlags);
427            psfi->iIcon = iIndex;
428
429             if(uFlags != GIL_NOTFILENAME)
430               lstrcpyW (psfi->szDisplayName, szLocation);
431             else
432               ret = FALSE;
433
434             IExtractIconA_Release(pei);
435           }
436         }
437
438         /* get icon index (or load icon)*/
439         if (SUCCEEDED(hr) && (flags & (SHGFI_ICON | SHGFI_SYSICONINDEX)))
440         {
441           if (flags & SHGFI_USEFILEATTRIBUTES)
442           {
443             WCHAR sTemp [MAX_PATH];
444             WCHAR * szExt;
445             DWORD dwNr=0;
446
447             lstrcpynW(sTemp, szFullPath, MAX_PATH);
448
449             if (dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
450                psfi->iIcon = 2;
451             else
452             {
453                static const WCHAR p1W[] = {'%','1',0};
454                psfi->iIcon = 0;
455                szExt = (LPWSTR) PathFindExtensionW(sTemp);
456                if ( szExt && HCR_MapTypeToValueW(szExt, sTemp, MAX_PATH, TRUE)
457                    && HCR_GetDefaultIconW(sTemp, sTemp, MAX_PATH, &dwNr))
458                {
459                   if (!lstrcmpW(p1W,sTemp))            /* icon is in the file */
460                      strcpyW(sTemp, szFullPath);
461
462                   if (flags & SHGFI_SYSICONINDEX) 
463                   {
464                       psfi->iIcon = SIC_GetIconIndex(sTemp,dwNr);
465                       if (psfi->iIcon == -1) psfi->iIcon = 0;
466                   }
467                   else 
468                   {
469                       IconNotYetLoaded=FALSE;
470                       PrivateExtractIconsW(sTemp,dwNr,(flags & SHGFI_SMALLICON) ? 
471                         GetSystemMetrics(SM_CXSMICON) : GetSystemMetrics(SM_CXICON),
472                         (flags & SHGFI_SMALLICON) ? GetSystemMetrics(SM_CYSMICON) :
473                         GetSystemMetrics(SM_CYICON), &psfi->hIcon,0,1,0);
474                       psfi->iIcon = dwNr;
475                   }
476                }
477             }
478           }
479           else
480           {
481             if (!(PidlToSicIndex(psfParent, pidlLast, !(flags & SHGFI_SMALLICON),
482               (flags & SHGFI_OPENICON)? GIL_OPENICON : 0, &(psfi->iIcon))))
483             {
484               ret = FALSE;
485             }
486           }
487           if (ret)
488           {
489             ret = (DWORD) ((flags & SHGFI_SMALLICON) ? ShellSmallIconList : ShellBigIconList);
490           }
491         }
492
493         /* icon handle */
494         if (SUCCEEDED(hr) && (flags & SHGFI_ICON) && IconNotYetLoaded)
495           psfi->hIcon = ImageList_GetIcon((flags & SHGFI_SMALLICON) ? ShellSmallIconList:ShellBigIconList, psfi->iIcon, ILD_NORMAL);
496
497         if (flags & ~SHGFI_KNOWN_FLAGS)
498           FIXME("unknown flags %08x\n", flags & ~SHGFI_KNOWN_FLAGS);
499
500         if (psfParent)
501           IShellFolder_Release(psfParent);
502
503         if (hr != S_OK)
504           ret = FALSE;
505
506         if(pidlLast) SHFree(pidlLast);
507 #ifdef MORE_DEBUG
508         TRACE ("icon=%p index=0x%08x attr=0x%08lx name=%s type=%s ret=0x%08lx\n",
509                 psfi->hIcon, psfi->iIcon, psfi->dwAttributes, debugstr_w(psfi->szDisplayName), debugstr_w(psfi->szTypeName), ret);
510 #endif
511         return ret;
512 }
513
514 /*************************************************************************
515  * SHGetFileInfoA                       [SHELL32.@]
516  */
517
518 DWORD WINAPI SHGetFileInfoA(LPCSTR path,DWORD dwFileAttributes,
519                               SHFILEINFOA *psfi, UINT sizeofpsfi,
520                               UINT flags )
521 {
522         INT len;
523         LPWSTR temppath;
524         DWORD ret;
525         SHFILEINFOW temppsfi;
526
527         if (flags & SHGFI_PIDL) {
528           /* path contains a pidl */
529           temppath = (LPWSTR) path;
530         } else {
531           len = MultiByteToWideChar(CP_ACP, 0, path, -1, NULL, 0);
532           temppath = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
533           MultiByteToWideChar(CP_ACP, 0, path, -1, temppath, len);
534         }
535
536         if(psfi && (flags & SHGFI_ATTR_SPECIFIED))
537                temppsfi.dwAttributes=psfi->dwAttributes;
538
539         ret = SHGetFileInfoW(temppath, dwFileAttributes, (psfi == NULL)? NULL : &temppsfi, sizeof(temppsfi), flags);
540
541         if (psfi)
542         {
543             if(flags & SHGFI_ICON)
544                 psfi->hIcon=temppsfi.hIcon;
545             if(flags & (SHGFI_SYSICONINDEX|SHGFI_ICON|SHGFI_ICONLOCATION))
546                 psfi->iIcon=temppsfi.iIcon;
547             if(flags & SHGFI_ATTRIBUTES)
548                 psfi->dwAttributes=temppsfi.dwAttributes;
549             if(flags & (SHGFI_DISPLAYNAME|SHGFI_ICONLOCATION))
550                 WideCharToMultiByte(CP_ACP, 0, temppsfi.szDisplayName, -1, psfi->szDisplayName, sizeof(psfi->szDisplayName), NULL, NULL);
551             if(flags & SHGFI_TYPENAME)
552                 WideCharToMultiByte(CP_ACP, 0, temppsfi.szTypeName, -1, psfi->szTypeName, sizeof(psfi->szTypeName), NULL, NULL);
553         }
554         if(!(flags & SHGFI_PIDL)) HeapFree(GetProcessHeap(), 0, temppath);
555         return ret;
556 }
557
558 /*************************************************************************
559  * DuplicateIcon                        [SHELL32.@]
560  */
561 HICON WINAPI DuplicateIcon( HINSTANCE hInstance, HICON hIcon)
562 {
563     ICONINFO IconInfo;
564     HICON hDupIcon = 0;
565
566     TRACE("(%p, %p)\n", hInstance, hIcon);
567
568     if(GetIconInfo(hIcon, &IconInfo))
569     {
570         hDupIcon = CreateIconIndirect(&IconInfo);
571
572         /* clean up hbmMask and hbmColor */
573         DeleteObject(IconInfo.hbmMask);
574         DeleteObject(IconInfo.hbmColor);
575     }
576
577     return hDupIcon;
578 }
579
580 /*************************************************************************
581  * ExtractIconA                         [SHELL32.@]
582  */
583 HICON WINAPI ExtractIconA(HINSTANCE hInstance, LPCSTR lpszFile, UINT nIconIndex)
584 {   
585   HICON ret;
586   INT len = MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, NULL, 0);
587   LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
588
589   TRACE("%p %s %d\n", hInstance, lpszFile, nIconIndex);
590
591   MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, lpwstrFile, len);
592   ret = ExtractIconW(hInstance, lpwstrFile, nIconIndex);
593   HeapFree(GetProcessHeap(), 0, lpwstrFile);
594   return ret;
595 }
596
597 /*************************************************************************
598  * ExtractIconW                         [SHELL32.@]
599  */
600 HICON WINAPI ExtractIconW(HINSTANCE hInstance, LPCWSTR lpszFile, UINT nIconIndex)
601 {
602         HICON  hIcon = NULL;
603         UINT ret;
604         UINT cx = GetSystemMetrics(SM_CXICON), cy = GetSystemMetrics(SM_CYICON);
605
606         TRACE("%p %s %d\n", hInstance, debugstr_w(lpszFile), nIconIndex);
607
608         if (nIconIndex == 0xFFFFFFFF) {
609           ret = PrivateExtractIconsW(lpszFile, 0, cx, cy, NULL, NULL, 0, LR_DEFAULTCOLOR);
610           if (ret != 0xFFFFFFFF && ret)
611             return (HICON)ret;
612           return NULL;
613         }
614         else
615           ret = PrivateExtractIconsW(lpszFile, nIconIndex, cx, cy, &hIcon, NULL, 1, LR_DEFAULTCOLOR);
616
617         if (ret == 0xFFFFFFFF)
618           return (HICON)1;
619         else if (ret > 0 && hIcon)
620           return hIcon;
621         return NULL;
622 }
623
624 typedef struct
625 {
626     LPCWSTR  szApp;
627     LPCWSTR  szOtherStuff;
628     HICON hIcon;
629     HFONT hFont;
630 } ABOUT_INFO;
631
632 #define         IDC_STATIC_TEXT1        100
633 #define         IDC_STATIC_TEXT2        101
634 #define         IDC_LISTBOX             99
635 #define         IDC_WINE_TEXT           98
636
637 #define         DROP_FIELD_TOP          (-15)
638 #define         DROP_FIELD_HEIGHT       15
639
640 static BOOL __get_dropline( HWND hWnd, LPRECT lprect )
641 { HWND hWndCtl = GetDlgItem(hWnd, IDC_WINE_TEXT);
642     if( hWndCtl )
643   { GetWindowRect( hWndCtl, lprect );
644         MapWindowPoints( 0, hWnd, (LPPOINT)lprect, 2 );
645         lprect->bottom = (lprect->top += DROP_FIELD_TOP);
646         return TRUE;
647     }
648     return FALSE;
649 }
650
651 /*************************************************************************
652  * SHAppBarMessage                      [SHELL32.@]
653  */
654 UINT WINAPI SHAppBarMessage(DWORD msg, PAPPBARDATA data)
655 {
656         int width=data->rc.right - data->rc.left;
657         int height=data->rc.bottom - data->rc.top;
658         RECT rec=data->rc;
659         switch (msg)
660         { case ABM_GETSTATE:
661                return ABS_ALWAYSONTOP | ABS_AUTOHIDE;
662           case ABM_GETTASKBARPOS:
663                GetWindowRect(data->hWnd, &rec);
664                data->rc=rec;
665                return TRUE;
666           case ABM_ACTIVATE:
667                SetActiveWindow(data->hWnd);
668                return TRUE;
669           case ABM_GETAUTOHIDEBAR:
670                data->hWnd=GetActiveWindow();
671                return TRUE;
672           case ABM_NEW:
673                SetWindowPos(data->hWnd,HWND_TOP,rec.left,rec.top,
674                                         width,height,SWP_SHOWWINDOW);
675                return TRUE;
676           case ABM_QUERYPOS:
677                GetWindowRect(data->hWnd, &(data->rc));
678                return TRUE;
679           case ABM_REMOVE:
680                FIXME("ABM_REMOVE broken\n");
681                /* FIXME: this is wrong; should it be DestroyWindow instead? */
682                /*CloseHandle(data->hWnd);*/
683                return TRUE;
684           case ABM_SETAUTOHIDEBAR:
685                SetWindowPos(data->hWnd,HWND_TOP,rec.left+1000,rec.top,
686                                        width,height,SWP_SHOWWINDOW);
687                return TRUE;
688           case ABM_SETPOS:
689                data->uEdge=(ABE_RIGHT | ABE_LEFT);
690                SetWindowPos(data->hWnd,HWND_TOP,data->rc.left,data->rc.top,
691                                   width,height,SWP_SHOWWINDOW);
692                return TRUE;
693           case ABM_WINDOWPOSCHANGED:
694                return TRUE;
695           }
696       return FALSE;
697 }
698
699 /*************************************************************************
700  * SHHelpShortcuts_RunDLL               [SHELL32.@]
701  *
702  */
703 DWORD WINAPI SHHelpShortcuts_RunDLL (DWORD dwArg1, DWORD dwArg2, DWORD dwArg3, DWORD dwArg4)
704 { FIXME("(%lx, %lx, %lx, %lx) empty stub!\n",
705         dwArg1, dwArg2, dwArg3, dwArg4);
706
707   return 0;
708 }
709
710 /*************************************************************************
711  * SHLoadInProc                         [SHELL32.@]
712  * Create an instance of specified object class from within
713  * the shell process and release it immediately
714  */
715
716 HRESULT WINAPI SHLoadInProc (REFCLSID rclsid)
717 {
718     void *ptr = NULL;
719
720     TRACE("%s\n", debugstr_guid(rclsid));
721
722     CoCreateInstance(rclsid, NULL, CLSCTX_INPROC_SERVER, &IID_IUnknown,&ptr);
723     if(ptr)
724     {
725         IUnknown * pUnk = ptr;
726         IUnknown_Release(pUnk);
727         return NOERROR;
728     }
729     return DISP_E_MEMBERNOTFOUND;
730 }
731
732 /*************************************************************************
733  * AboutDlgProc                 (internal)
734  */
735 INT_PTR CALLBACK AboutDlgProc( HWND hWnd, UINT msg, WPARAM wParam,
736                               LPARAM lParam )
737 {
738     HWND hWndCtl;
739
740     TRACE("\n");
741
742     switch(msg)
743     {
744     case WM_INITDIALOG:
745         {
746             ABOUT_INFO *info = (ABOUT_INFO *)lParam;
747             WCHAR Template[512], AppTitle[512];
748
749             if (info)
750             {
751                 const char* const *pstr = SHELL_Authors;
752                 SendDlgItemMessageW(hWnd, stc1, STM_SETICON,(WPARAM)info->hIcon, 0);
753                 GetWindowTextW( hWnd, Template, sizeof(Template)/sizeof(WCHAR) );
754                 sprintfW( AppTitle, Template, info->szApp );
755                 SetWindowTextW( hWnd, AppTitle );
756                 SetWindowTextW( GetDlgItem(hWnd, IDC_STATIC_TEXT1), info->szApp );
757                 SetWindowTextW( GetDlgItem(hWnd, IDC_STATIC_TEXT2), info->szOtherStuff );
758                 hWndCtl = GetDlgItem(hWnd, IDC_LISTBOX);
759                 SendMessageW( hWndCtl, WM_SETREDRAW, 0, 0 );
760                 SendMessageW( hWndCtl, WM_SETFONT, (WPARAM)info->hFont, 0 );
761                 while (*pstr)
762                 {
763                     WCHAR name[64];
764                     /* authors list is in iso-8859-1 format */
765                     MultiByteToWideChar( 28591, 0, *pstr, -1, name, sizeof(name)/sizeof(WCHAR) );
766                     SendMessageW( hWndCtl, LB_ADDSTRING, (WPARAM)-1, (LPARAM)name );
767                     pstr++;
768                 }
769                 SendMessageW( hWndCtl, WM_SETREDRAW, 1, 0 );
770             }
771         }
772         return 1;
773
774     case WM_PAINT:
775       { RECT rect;
776             PAINTSTRUCT ps;
777             HDC hDC = BeginPaint( hWnd, &ps );
778
779             if( __get_dropline( hWnd, &rect ) ) {
780                 SelectObject( hDC, GetStockObject( BLACK_PEN ) );
781                 MoveToEx( hDC, rect.left, rect.top, NULL );
782                 LineTo( hDC, rect.right, rect.bottom );
783             }
784             EndPaint( hWnd, &ps );
785         }
786         break;
787
788     case WM_COMMAND:
789         if (wParam == IDOK || wParam == IDCANCEL)
790         {
791             EndDialog(hWnd, TRUE);
792             return TRUE;
793         }
794         break;
795     case WM_CLOSE:
796       EndDialog(hWnd, TRUE);
797       break;
798     }
799
800     return 0;
801 }
802
803
804 /*************************************************************************
805  * ShellAboutA                          [SHELL32.288]
806  */
807 BOOL WINAPI ShellAboutA( HWND hWnd, LPCSTR szApp, LPCSTR szOtherStuff, HICON hIcon )
808 {
809     BOOL ret;
810     LPWSTR appW = NULL, otherW = NULL;
811     int len;
812
813     if (szApp)
814     {
815         len = MultiByteToWideChar(CP_ACP, 0, szApp, -1, NULL, 0);
816         appW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
817         MultiByteToWideChar(CP_ACP, 0, szApp, -1, appW, len);
818     }
819     if (szOtherStuff)
820     {
821         len = MultiByteToWideChar(CP_ACP, 0, szOtherStuff, -1, NULL, 0);
822         otherW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
823         MultiByteToWideChar(CP_ACP, 0, szOtherStuff, -1, otherW, len);
824     }
825
826     ret = ShellAboutW(hWnd, appW, otherW, hIcon);
827
828     HeapFree(GetProcessHeap(), 0, otherW);
829     HeapFree(GetProcessHeap(), 0, appW);
830     return ret;
831 }
832
833
834 /*************************************************************************
835  * ShellAboutW                          [SHELL32.289]
836  */
837 BOOL WINAPI ShellAboutW( HWND hWnd, LPCWSTR szApp, LPCWSTR szOtherStuff,
838                              HICON hIcon )
839 {
840     ABOUT_INFO info;
841     LOGFONTW logFont;
842     HRSRC hRes;
843     LPVOID template;
844     BOOL bRet;
845     static const WCHAR wszSHELL_ABOUT_MSGBOX[] =
846         {'S','H','E','L','L','_','A','B','O','U','T','_','M','S','G','B','O','X',0};
847
848     TRACE("\n");
849
850     if(!(hRes = FindResourceW(shell32_hInstance, wszSHELL_ABOUT_MSGBOX, (LPWSTR)RT_DIALOG)))
851         return FALSE;
852     if(!(template = (LPVOID)LoadResource(shell32_hInstance, hRes)))
853         return FALSE;
854     info.szApp        = szApp;
855     info.szOtherStuff = szOtherStuff;
856     info.hIcon        = hIcon ? hIcon : LoadIconW( 0, (LPWSTR)IDI_WINLOGO );
857
858     SystemParametersInfoW( SPI_GETICONTITLELOGFONT, 0, &logFont, 0 );
859     info.hFont = CreateFontIndirectW( &logFont );
860
861     bRet = DialogBoxIndirectParamW((HINSTANCE)GetWindowLongPtrW( hWnd, GWLP_HINSTANCE ),
862                                    template, hWnd, AboutDlgProc, (LPARAM)&info );
863     DeleteObject(info.hFont);
864     return bRet;
865 }
866
867 /*************************************************************************
868  * FreeIconList (SHELL32.@)
869  */
870 void WINAPI FreeIconList( DWORD dw )
871 { FIXME("(%lx): stub\n",dw);
872 }
873
874
875 /*************************************************************************
876  * ShellDDEInit (SHELL32.@)
877  */
878 void WINAPI ShellDDEInit(BOOL start)
879 {
880     FIXME("stub: %d\n", start);
881 }
882
883 /***********************************************************************
884  * DllGetVersion [SHELL32.@]
885  *
886  * Retrieves version information of the 'SHELL32.DLL'
887  *
888  * PARAMS
889  *     pdvi [O] pointer to version information structure.
890  *
891  * RETURNS
892  *     Success: S_OK
893  *     Failure: E_INVALIDARG
894  *
895  * NOTES
896  *     Returns version of a shell32.dll from IE4.01 SP1.
897  */
898
899 HRESULT WINAPI SHELL32_DllGetVersion (DLLVERSIONINFO *pdvi)
900 {
901     /* FIXME: shouldn't these values come from the version resource? */
902     if (pdvi->cbSize == sizeof(DLLVERSIONINFO) ||
903      pdvi->cbSize == sizeof(DLLVERSIONINFO2))
904     {
905         pdvi->dwMajorVersion = WINE_FILEVERSION_MAJOR;
906         pdvi->dwMinorVersion = WINE_FILEVERSION_MINOR;
907         pdvi->dwBuildNumber = WINE_FILEVERSION_BUILD;
908         pdvi->dwPlatformID = WINE_FILEVERSION_PLATFORMID;
909         if (pdvi->cbSize == sizeof(DLLVERSIONINFO2))
910         {
911             DLLVERSIONINFO2 *pdvi2 = (DLLVERSIONINFO2 *)pdvi;
912
913             pdvi2->dwFlags = 0;
914             pdvi2->ullVersion = MAKEDLLVERULL(WINE_FILEVERSION_MAJOR,
915                                               WINE_FILEVERSION_MINOR,
916                                               WINE_FILEVERSION_BUILD,
917                                               WINE_FILEVERSION_PLATFORMID);
918         }
919         TRACE("%lu.%lu.%lu.%lu\n",
920            pdvi->dwMajorVersion, pdvi->dwMinorVersion,
921            pdvi->dwBuildNumber, pdvi->dwPlatformID);
922         return S_OK;
923     }
924     else
925         {
926         WARN("wrong DLLVERSIONINFO size from app\n");
927         return E_INVALIDARG;
928     }
929 }
930 /*************************************************************************
931  * global variables of the shell32.dll
932  * all are once per process
933  *
934  */
935 HINSTANCE       shell32_hInstance = 0;
936 HIMAGELIST      ShellSmallIconList = 0;
937 HIMAGELIST      ShellBigIconList = 0;
938
939
940 /*************************************************************************
941  * SHELL32 DllMain
942  *
943  * NOTES
944  *  calling oleinitialize here breaks sone apps.
945  */
946
947 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID fImpLoad)
948 {
949         TRACE("%p 0x%lx %p\n", hinstDLL, fdwReason, fImpLoad);
950
951         switch (fdwReason)
952         {
953           case DLL_PROCESS_ATTACH:
954             shell32_hInstance = hinstDLL;
955             DisableThreadLibraryCalls(shell32_hInstance);
956
957             /* get full path to this DLL for IExtractIconW_fnGetIconLocation() */
958             GetModuleFileNameW(hinstDLL, swShell32Name, MAX_PATH);
959             swShell32Name[MAX_PATH - 1] = '\0';
960
961             InitCommonControlsEx(NULL);
962
963             SIC_Initialize();
964             SYSTRAY_Init();
965             InitChangeNotifications();
966             break;
967
968           case DLL_PROCESS_DETACH:
969               shell32_hInstance = 0;
970               SIC_Destroy();
971               FreeChangeNotifications();
972               break;
973         }
974         return TRUE;
975 }
976
977 /*************************************************************************
978  * DllInstall         [SHELL32.@]
979  *
980  * PARAMETERS
981  *
982  *    BOOL bInstall - TRUE for install, FALSE for uninstall
983  *    LPCWSTR pszCmdLine - command line (unused by shell32?)
984  */
985
986 HRESULT WINAPI SHELL32_DllInstall(BOOL bInstall, LPCWSTR cmdline)
987 {
988    FIXME("(%s, %s): stub!\n", bInstall ? "TRUE":"FALSE", debugstr_w(cmdline));
989
990    return S_OK;         /* indicate success */
991 }
992
993 /***********************************************************************
994  *              DllCanUnloadNow (SHELL32.@)
995  */
996 HRESULT WINAPI SHELL32_DllCanUnloadNow(void)
997 {
998     FIXME("(void): stub\n");
999
1000     return S_FALSE;
1001 }