ShellExecuteEx, ExtractIconEx, SHFileOperation, SHGetFileInfo,
[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 /*************************************************************************
216  * SHGetFileInfoA                       [SHELL32.@]
217  *
218  */
219
220 DWORD WINAPI SHGetFileInfoW(LPCWSTR path,DWORD dwFileAttributes,
221                               SHFILEINFOW *psfi, UINT sizeofpsfi,
222                               UINT flags )
223 {
224         WCHAR szLocation[MAX_PATH], szFullPath[MAX_PATH];
225         int iIndex;
226         DWORD ret = TRUE, dwAttributes = 0;
227         IShellFolder * psfParent = NULL;
228         IExtractIconW * pei = NULL;
229         LPITEMIDLIST    pidlLast = NULL, pidl = NULL;
230         HRESULT hr = S_OK;
231         BOOL IconNotYetLoaded=TRUE;
232
233         TRACE("(%s fattr=0x%lx sfi=%p(attr=0x%08lx) size=0x%x flags=0x%x)\n",
234           (flags & SHGFI_PIDL)? "pidl" : debugstr_w(path), dwFileAttributes, psfi, psfi->dwAttributes, sizeofpsfi, flags);
235
236         if ((flags & SHGFI_USEFILEATTRIBUTES) && (flags & (SHGFI_ATTRIBUTES|SHGFI_EXETYPE|SHGFI_PIDL)))
237           return FALSE;
238
239         /* windows initializes this values regardless of the flags */
240         if (psfi != NULL) {
241             psfi->szDisplayName[0] = '\0';
242             psfi->szTypeName[0] = '\0';
243             psfi->iIcon = 0;
244         }
245
246        if (!(flags & SHGFI_PIDL)){
247             /* SHGitFileInfo should work with absolute and relative paths */
248             if (PathIsRelativeW(path)){
249                 GetCurrentDirectoryW(MAX_PATH, szLocation);
250                 PathCombineW(szFullPath, szLocation, path);
251             } else {
252                 lstrcpynW(szFullPath, path, MAX_PATH);
253             }
254         }
255
256         if (flags & SHGFI_EXETYPE) {
257           BOOL status = FALSE;
258           HANDLE hfile;
259           DWORD BinaryType;
260           IMAGE_DOS_HEADER mz_header;
261           IMAGE_NT_HEADERS nt;
262           DWORD len;
263           char magic[4];
264
265           if (flags != SHGFI_EXETYPE) return 0;
266
267          status = GetBinaryTypeW (szFullPath, &BinaryType);
268           if (!status) return 0;
269           if ((BinaryType == SCS_DOS_BINARY)
270                 || (BinaryType == SCS_PIF_BINARY)) return 0x4d5a;
271
272          hfile = CreateFileW( szFullPath, GENERIC_READ, FILE_SHARE_READ,
273                 NULL, OPEN_EXISTING, 0, 0 );
274           if ( hfile == INVALID_HANDLE_VALUE ) return 0;
275
276         /* The next section is adapted from MODULE_GetBinaryType, as we need
277          * to examine the image header to get OS and version information. We
278          * know from calling GetBinaryTypeA that the image is valid and either
279          * an NE or PE, so much error handling can be omitted.
280          * Seek to the start of the file and read the header information.
281          */
282
283           SetFilePointer( hfile, 0, NULL, SEEK_SET );
284           ReadFile( hfile, &mz_header, sizeof(mz_header), &len, NULL );
285
286          SetFilePointer( hfile, mz_header.e_lfanew, NULL, SEEK_SET );
287          ReadFile( hfile, magic, sizeof(magic), &len, NULL );
288          if ( *(DWORD*)magic      == IMAGE_NT_SIGNATURE )
289          {
290              SetFilePointer( hfile, mz_header.e_lfanew, NULL, SEEK_SET );
291              ReadFile( hfile, &nt, sizeof(nt), &len, NULL );
292               CloseHandle( hfile );
293               if (nt.OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_GUI) {
294                  return IMAGE_NT_SIGNATURE
295                         | (nt.OptionalHeader.MajorSubsystemVersion << 24)
296                         | (nt.OptionalHeader.MinorSubsystemVersion << 16);
297               }
298               return IMAGE_NT_SIGNATURE;
299           }
300          else if ( *(WORD*)magic == IMAGE_OS2_SIGNATURE )
301          {
302              IMAGE_OS2_HEADER ne;
303              SetFilePointer( hfile, mz_header.e_lfanew, NULL, SEEK_SET );
304              ReadFile( hfile, &ne, sizeof(ne), &len, NULL );
305               CloseHandle( hfile );
306              if (ne.ne_exetyp == 2) return IMAGE_OS2_SIGNATURE
307                         | (ne.ne_expver << 16);
308               return 0;
309           }
310           CloseHandle( hfile );
311           return 0;
312       }
313
314       /* psfi is NULL normally to query EXE type. If it is NULL, none of the
315        * below makes sense anyway. Windows allows this and just returns FALSE */
316       if (psfi == NULL) return FALSE;
317
318         /* translate the path into a pidl only when SHGFI_USEFILEATTRIBUTES
319          * is not specified.
320            The pidl functions fail on not existing file names */
321
322         if (flags & SHGFI_PIDL) {
323             pidl = ILClone((LPCITEMIDLIST)path);
324         } else if (!(flags & SHGFI_USEFILEATTRIBUTES)) {
325            hr = SHILCreateFromPathW(szFullPath, &pidl, &dwAttributes);
326         }
327
328         if ((flags & SHGFI_PIDL) || !(flags & SHGFI_USEFILEATTRIBUTES))
329         {
330            /* get the parent shellfolder */
331            if (pidl) {
332               hr = SHBindToParent(pidl, &IID_IShellFolder, (LPVOID*)&psfParent, (LPCITEMIDLIST*)&pidlLast);
333               ILFree(pidl);
334            } else {
335               ERR("pidl is null!\n");
336               return FALSE;
337            }
338         }
339
340         /* get the attributes of the child */
341         if (SUCCEEDED(hr) && (flags & SHGFI_ATTRIBUTES))
342         {
343           if (!(flags & SHGFI_ATTR_SPECIFIED))
344           {
345             psfi->dwAttributes = 0xffffffff;
346           }
347           IShellFolder_GetAttributesOf(psfParent, 1, (LPCITEMIDLIST*)&pidlLast, &(psfi->dwAttributes));
348         }
349
350         /* get the displayname */
351         if (SUCCEEDED(hr) && (flags & SHGFI_DISPLAYNAME))
352         {
353           if (flags & SHGFI_USEFILEATTRIBUTES)
354           {
355            lstrcpyW (psfi->szDisplayName, PathFindFileNameW(szFullPath));
356           }
357           else
358           {
359             STRRET str;
360             hr = IShellFolder_GetDisplayNameOf(psfParent, pidlLast, SHGDN_INFOLDER, &str);
361             StrRetToStrNW (psfi->szDisplayName, MAX_PATH, &str, pidlLast);
362           }
363         }
364
365         /* get the type name */
366         if (SUCCEEDED(hr) && (flags & SHGFI_TYPENAME))
367         {
368             static const WCHAR szFile[] = { 'F','i','l','e',0 };
369             static const WCHAR szDashFile[] = { '-','f','i','l','e',0 };
370             if (!(flags & SHGFI_USEFILEATTRIBUTES))
371             {
372                 char ftype[80];
373                 _ILGetFileType(pidlLast, ftype, 80);
374                 MultiByteToWideChar(CP_ACP, 0, ftype, -1, psfi->szTypeName, 80 );
375             }
376             else
377             {
378                 if (dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
379                    strcatW (psfi->szTypeName, szFile);
380                 else 
381                 {
382                    WCHAR sTemp[64];
383                    lstrcpyW(sTemp,PathFindExtensionW(szFullPath));
384                    if (!( HCR_MapTypeToValueW(sTemp, sTemp, 64, TRUE)
385                         && HCR_MapTypeToValueW(sTemp, psfi->szTypeName, 80, FALSE )))
386                    {
387                        lstrcpynW (psfi->szTypeName, sTemp, 64);
388                        strcatW (psfi->szTypeName, szDashFile);
389                    }
390                 }
391             }
392         }
393
394         /* ### icons ###*/
395         if (flags & SHGFI_LINKOVERLAY)
396           FIXME("set icon to link, stub\n");
397
398         if (flags & SHGFI_SELECTED)
399           FIXME("set icon to selected, stub\n");
400
401         if (flags & SHGFI_SHELLICONSIZE)
402           FIXME("set icon to shell size, stub\n");
403
404         /* get the iconlocation */
405         if (SUCCEEDED(hr) && (flags & SHGFI_ICONLOCATION ))
406         {
407           UINT uDummy,uFlags;
408           hr = IShellFolder_GetUIObjectOf(psfParent, 0, 1, (LPCITEMIDLIST*)&pidlLast, &IID_IExtractIconA, &uDummy, (LPVOID*)&pei);
409
410           if (SUCCEEDED(hr))
411           {
412            hr = IExtractIconW_GetIconLocation(pei, (flags & SHGFI_OPENICON)? GIL_OPENICON : 0,szLocation, MAX_PATH, &iIndex, &uFlags);
413            psfi->iIcon = iIndex;
414
415             if(uFlags != GIL_NOTFILENAME)
416               lstrcpyW (psfi->szDisplayName, szLocation);
417             else
418               ret = FALSE;
419
420             IExtractIconA_Release(pei);
421           }
422         }
423
424         /* get icon index (or load icon)*/
425         if (SUCCEEDED(hr) && (flags & (SHGFI_ICON | SHGFI_SYSICONINDEX)))
426         {
427
428           if (flags & SHGFI_USEFILEATTRIBUTES)
429           {
430             WCHAR sTemp [MAX_PATH];
431             WCHAR * szExt;
432             DWORD dwNr=0;
433
434             lstrcpynW(sTemp, szFullPath, MAX_PATH);
435
436             if (dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
437                psfi->iIcon = 2;
438             else
439             {
440                static const WCHAR p1W[] = {'%','1',0};
441                psfi->iIcon = 0;
442                szExt = (LPWSTR) PathFindExtensionW(sTemp);
443                if ( szExt && HCR_MapTypeToValueW(szExt, sTemp, MAX_PATH, TRUE)
444                    && HCR_GetDefaultIconW(sTemp, sTemp, MAX_PATH, &dwNr))
445                {
446                   if (!lstrcmpW(p1W,sTemp))            /* icon is in the file */
447                      strcpyW(sTemp, szFullPath);
448
449                   if (flags & SHGFI_SYSICONINDEX) 
450                   {
451                       psfi->iIcon = SIC_GetIconIndex(sTemp,dwNr);
452                       if (psfi->iIcon == -1) psfi->iIcon = 0;
453                   }
454                   else 
455                   {
456                       IconNotYetLoaded=FALSE;
457                       PrivateExtractIconsW(sTemp,dwNr,(flags & SHGFI_SMALLICON) ? 
458                         GetSystemMetrics(SM_CXSMICON) : GetSystemMetrics(SM_CXICON),
459                         (flags & SHGFI_SMALLICON) ? GetSystemMetrics(SM_CYSMICON) :
460                         GetSystemMetrics(SM_CYICON), &psfi->hIcon,0,1,0);
461                       psfi->iIcon = dwNr;
462                   }
463                }
464             }
465           }
466           else
467           {
468             if (!(PidlToSicIndex(psfParent, pidlLast, !(flags & SHGFI_SMALLICON),
469               (flags & SHGFI_OPENICON)? GIL_OPENICON : 0, &(psfi->iIcon))))
470             {
471               ret = FALSE;
472             }
473           }
474           if (ret)
475           {
476             ret = (DWORD) ((flags & SHGFI_SMALLICON) ? ShellSmallIconList : ShellBigIconList);
477           }
478         }
479
480         /* icon handle */
481         if (SUCCEEDED(hr) && (flags & SHGFI_ICON) && IconNotYetLoaded)
482           psfi->hIcon = ImageList_GetIcon((flags & SHGFI_SMALLICON) ? ShellSmallIconList:ShellBigIconList, psfi->iIcon, ILD_NORMAL);
483
484         if (flags & (SHGFI_UNKNOWN1 | SHGFI_UNKNOWN2 | SHGFI_UNKNOWN3))
485           FIXME("unknown attribute!\n");
486
487         if (psfParent)
488           IShellFolder_Release(psfParent);
489
490         if (hr != S_OK)
491           ret = FALSE;
492
493         if(pidlLast) SHFree(pidlLast);
494 #ifdef MORE_DEBUG
495         TRACE ("icon=%p index=0x%08x attr=0x%08lx name=%s type=%s ret=0x%08lx\n",
496                 psfi->hIcon, psfi->iIcon, psfi->dwAttributes, debugstr_w(psfi->szDisplayName), debugstr_w(psfi->szTypeName), ret);
497 #endif
498         return ret;
499 }
500
501 /*************************************************************************
502  * SHGetFileInfoW                       [SHELL32.@]
503  */
504
505 DWORD WINAPI SHGetFileInfoA(LPCSTR path,DWORD dwFileAttributes,
506                               SHFILEINFOA *psfi, UINT sizeofpsfi,
507                               UINT flags )
508 {
509         INT len;
510         LPWSTR temppath;
511         DWORD ret;
512         SHFILEINFOW temppsfi;
513
514         if (flags & SHGFI_PIDL) {
515           /* path contains a pidl */
516           temppath = (LPWSTR) path;
517         } else {
518           len = MultiByteToWideChar(CP_ACP, 0, path, -1, NULL, 0);
519           temppath = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
520           MultiByteToWideChar(CP_ACP, 0, path, -1, temppath, len);
521         }
522
523         if(psfi && (flags & SHGFI_ATTR_SPECIFIED))
524                temppsfi.dwAttributes=psfi->dwAttributes;
525
526         ret = SHGetFileInfoW(temppath, dwFileAttributes, (psfi == NULL)? NULL : &temppsfi, sizeof(temppsfi), flags);
527
528         if (psfi)
529         {
530             if(flags & SHGFI_ICON)
531                 psfi->hIcon=temppsfi.hIcon;
532             if(flags & (SHGFI_SYSICONINDEX|SHGFI_ICON|SHGFI_ICONLOCATION))
533                 psfi->iIcon=temppsfi.iIcon;
534             if(flags & SHGFI_ATTRIBUTES)
535                 psfi->dwAttributes=temppsfi.dwAttributes;
536             if(flags & (SHGFI_DISPLAYNAME|SHGFI_ICONLOCATION))
537                 WideCharToMultiByte(CP_ACP, 0, temppsfi.szDisplayName, -1, psfi->szDisplayName, sizeof(psfi->szDisplayName), NULL, NULL);
538             if(flags & SHGFI_TYPENAME)
539                 WideCharToMultiByte(CP_ACP, 0, temppsfi.szTypeName, -1, psfi->szTypeName, sizeof(psfi->szTypeName), NULL, NULL);
540         }
541         if(!(flags & SHGFI_PIDL)) HeapFree(GetProcessHeap(), 0, temppath);
542         return ret;
543 }
544
545 /*************************************************************************
546  * SHGetFileInfo                        [SHELL32.@]
547  */
548 DWORD WINAPI SHGetFileInfoAW(
549         LPCVOID path,
550         DWORD dwFileAttributes,
551         LPVOID psfi,
552         UINT sizeofpsfi,
553         UINT flags)
554 {
555         if(SHELL_OsIsUnicode())
556           return SHGetFileInfoW(path, dwFileAttributes, psfi, sizeofpsfi, flags );
557         return SHGetFileInfoA(path, dwFileAttributes, psfi, sizeofpsfi, flags );
558 }
559
560 /*************************************************************************
561  * DuplicateIcon                        [SHELL32.@]
562  */
563 HICON WINAPI DuplicateIcon( HINSTANCE hInstance, HICON hIcon)
564 {
565     ICONINFO IconInfo;
566     HICON hDupIcon = 0;
567
568     TRACE("(%p, %p)\n", hInstance, hIcon);
569
570     if(GetIconInfo(hIcon, &IconInfo))
571     {
572         hDupIcon = CreateIconIndirect(&IconInfo);
573
574         /* clean up hbmMask and hbmColor */
575         DeleteObject(IconInfo.hbmMask);
576         DeleteObject(IconInfo.hbmColor);
577     }
578
579     return hDupIcon;
580 }
581
582 /*************************************************************************
583  * ExtractIconA                         [SHELL32.@]
584  */
585 HICON WINAPI ExtractIconA(HINSTANCE hInstance, LPCSTR lpszFile, UINT nIconIndex)
586 {   
587   HICON ret;
588   INT len = MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, NULL, 0);
589   LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
590
591   TRACE("%p %s %d\n", hInstance, lpszFile, nIconIndex);
592
593   MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, lpwstrFile, len);
594   ret = ExtractIconW(hInstance, lpwstrFile, nIconIndex);
595   HeapFree(GetProcessHeap(), 0, lpwstrFile);
596   return ret;
597 }
598
599 /*************************************************************************
600  * ExtractIconW                         [SHELL32.@]
601  */
602 HICON WINAPI ExtractIconW(HINSTANCE hInstance, LPCWSTR lpszFile, UINT nIconIndex)
603 {
604         HICON  hIcon = NULL;
605         UINT ret;
606         UINT cx = GetSystemMetrics(SM_CXICON), cy = GetSystemMetrics(SM_CYICON);
607
608         TRACE("%p %s %d\n", hInstance, debugstr_w(lpszFile), nIconIndex);
609
610         if (nIconIndex == 0xFFFFFFFF) {
611           ret = PrivateExtractIconsW(lpszFile, 0, cx, cy, NULL, NULL, 0, LR_DEFAULTCOLOR);
612           if (ret != 0xFFFFFFFF && ret)
613             return (HICON)ret;
614           return NULL;
615         }
616         else
617           ret = PrivateExtractIconsW(lpszFile, nIconIndex, cx, cy, &hIcon, NULL, 1, LR_DEFAULTCOLOR);
618
619         if (ret == 0xFFFFFFFF)
620           return (HICON)1;
621         else if (ret > 0 && hIcon)
622           return hIcon;
623         return NULL;
624 }
625
626 typedef struct
627 {
628     LPCWSTR  szApp;
629     LPCWSTR  szOtherStuff;
630     HICON hIcon;
631     HFONT hFont;
632 } ABOUT_INFO;
633
634 #define         IDC_STATIC_TEXT1        100
635 #define         IDC_STATIC_TEXT2        101
636 #define         IDC_LISTBOX             99
637 #define         IDC_WINE_TEXT           98
638
639 #define         DROP_FIELD_TOP          (-15)
640 #define         DROP_FIELD_HEIGHT       15
641
642 static BOOL __get_dropline( HWND hWnd, LPRECT lprect )
643 { HWND hWndCtl = GetDlgItem(hWnd, IDC_WINE_TEXT);
644     if( hWndCtl )
645   { GetWindowRect( hWndCtl, lprect );
646         MapWindowPoints( 0, hWnd, (LPPOINT)lprect, 2 );
647         lprect->bottom = (lprect->top += DROP_FIELD_TOP);
648         return TRUE;
649     }
650     return FALSE;
651 }
652
653 /*************************************************************************
654  * SHAppBarMessage                      [SHELL32.@]
655  */
656 UINT WINAPI SHAppBarMessage(DWORD msg, PAPPBARDATA data)
657 {
658         int width=data->rc.right - data->rc.left;
659         int height=data->rc.bottom - data->rc.top;
660         RECT rec=data->rc;
661         switch (msg)
662         { case ABM_GETSTATE:
663                return ABS_ALWAYSONTOP | ABS_AUTOHIDE;
664           case ABM_GETTASKBARPOS:
665                GetWindowRect(data->hWnd, &rec);
666                data->rc=rec;
667                return TRUE;
668           case ABM_ACTIVATE:
669                SetActiveWindow(data->hWnd);
670                return TRUE;
671           case ABM_GETAUTOHIDEBAR:
672                data->hWnd=GetActiveWindow();
673                return TRUE;
674           case ABM_NEW:
675                SetWindowPos(data->hWnd,HWND_TOP,rec.left,rec.top,
676                                         width,height,SWP_SHOWWINDOW);
677                return TRUE;
678           case ABM_QUERYPOS:
679                GetWindowRect(data->hWnd, &(data->rc));
680                return TRUE;
681           case ABM_REMOVE:
682                FIXME("ABM_REMOVE broken\n");
683                /* FIXME: this is wrong; should it be DestroyWindow instead? */
684                /*CloseHandle(data->hWnd);*/
685                return TRUE;
686           case ABM_SETAUTOHIDEBAR:
687                SetWindowPos(data->hWnd,HWND_TOP,rec.left+1000,rec.top,
688                                        width,height,SWP_SHOWWINDOW);
689                return TRUE;
690           case ABM_SETPOS:
691                data->uEdge=(ABE_RIGHT | ABE_LEFT);
692                SetWindowPos(data->hWnd,HWND_TOP,data->rc.left,data->rc.top,
693                                   width,height,SWP_SHOWWINDOW);
694                return TRUE;
695           case ABM_WINDOWPOSCHANGED:
696                return TRUE;
697           }
698       return FALSE;
699 }
700
701 /*************************************************************************
702  * SHHelpShortcuts_RunDLL               [SHELL32.@]
703  *
704  */
705 DWORD WINAPI SHHelpShortcuts_RunDLL (DWORD dwArg1, DWORD dwArg2, DWORD dwArg3, DWORD dwArg4)
706 { FIXME("(%lx, %lx, %lx, %lx) empty stub!\n",
707         dwArg1, dwArg2, dwArg3, dwArg4);
708
709   return 0;
710 }
711
712 /*************************************************************************
713  * SHLoadInProc                         [SHELL32.@]
714  * Create an instance of specified object class from within
715  * the shell process and release it immediately
716  */
717
718 HRESULT WINAPI SHLoadInProc (REFCLSID rclsid)
719 {
720     void *ptr = NULL;
721
722     TRACE("%s\n", debugstr_guid(rclsid));
723
724     CoCreateInstance(rclsid, NULL, CLSCTX_INPROC_SERVER, &IID_IUnknown,&ptr);
725     if(ptr)
726     {
727         IUnknown * pUnk = ptr;
728         IUnknown_Release(pUnk);
729         return NOERROR;
730     }
731     return DISP_E_MEMBERNOTFOUND;
732 }
733
734 /*************************************************************************
735  * AboutDlgProc                 (internal)
736  */
737 INT_PTR CALLBACK AboutDlgProc( HWND hWnd, UINT msg, WPARAM wParam,
738                               LPARAM lParam )
739 {
740     HWND hWndCtl;
741
742     TRACE("\n");
743
744     switch(msg)
745     {
746     case WM_INITDIALOG:
747         {
748             ABOUT_INFO *info = (ABOUT_INFO *)lParam;
749             WCHAR Template[512], AppTitle[512];
750
751             if (info)
752             {
753                 const char* const *pstr = SHELL_Authors;
754                 SendDlgItemMessageW(hWnd, stc1, STM_SETICON,(WPARAM)info->hIcon, 0);
755                 GetWindowTextW( hWnd, Template, sizeof(Template)/sizeof(WCHAR) );
756                 sprintfW( AppTitle, Template, info->szApp );
757                 SetWindowTextW( hWnd, AppTitle );
758                 SetWindowTextW( GetDlgItem(hWnd, IDC_STATIC_TEXT1), info->szApp );
759                 SetWindowTextW( GetDlgItem(hWnd, IDC_STATIC_TEXT2), info->szOtherStuff );
760                 hWndCtl = GetDlgItem(hWnd, IDC_LISTBOX);
761                 SendMessageW( hWndCtl, WM_SETREDRAW, 0, 0 );
762                 SendMessageW( hWndCtl, WM_SETFONT, (WPARAM)info->hFont, 0 );
763                 while (*pstr)
764                 {
765                     WCHAR name[64];
766                     /* authors list is in iso-8859-1 format */
767                     MultiByteToWideChar( 28591, 0, *pstr, -1, name, sizeof(name)/sizeof(WCHAR) );
768                     SendMessageW( hWndCtl, LB_ADDSTRING, (WPARAM)-1, (LPARAM)name );
769                     pstr++;
770                 }
771                 SendMessageW( hWndCtl, WM_SETREDRAW, 1, 0 );
772             }
773         }
774         return 1;
775
776     case WM_PAINT:
777       { RECT rect;
778             PAINTSTRUCT ps;
779             HDC hDC = BeginPaint( hWnd, &ps );
780
781             if( __get_dropline( hWnd, &rect ) ) {
782                 SelectObject( hDC, GetStockObject( BLACK_PEN ) );
783                 MoveToEx( hDC, rect.left, rect.top, NULL );
784                 LineTo( hDC, rect.right, rect.bottom );
785             }
786             EndPaint( hWnd, &ps );
787         }
788         break;
789
790     case WM_COMMAND:
791         if (wParam == IDOK || wParam == IDCANCEL)
792         {
793             EndDialog(hWnd, TRUE);
794             return TRUE;
795         }
796         break;
797     case WM_CLOSE:
798       EndDialog(hWnd, TRUE);
799       break;
800     }
801
802     return 0;
803 }
804
805
806 /*************************************************************************
807  * ShellAboutA                          [SHELL32.288]
808  */
809 BOOL WINAPI ShellAboutA( HWND hWnd, LPCSTR szApp, LPCSTR szOtherStuff, HICON hIcon )
810 {
811     BOOL ret;
812     LPWSTR appW = NULL, otherW = NULL;
813     int len;
814
815     if (szApp)
816     {
817         len = MultiByteToWideChar(CP_ACP, 0, szApp, -1, NULL, 0);
818         appW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
819         MultiByteToWideChar(CP_ACP, 0, szApp, -1, appW, len);
820     }
821     if (szOtherStuff)
822     {
823         len = MultiByteToWideChar(CP_ACP, 0, szOtherStuff, -1, NULL, 0);
824         otherW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
825         MultiByteToWideChar(CP_ACP, 0, szOtherStuff, -1, otherW, len);
826     }
827
828     ret = ShellAboutW(hWnd, appW, otherW, hIcon);
829
830     if (otherW) HeapFree(GetProcessHeap(), 0, otherW);
831     if (appW) HeapFree(GetProcessHeap(), 0, appW);
832     return ret;
833 }
834
835
836 /*************************************************************************
837  * ShellAboutW                          [SHELL32.289]
838  */
839 BOOL WINAPI ShellAboutW( HWND hWnd, LPCWSTR szApp, LPCWSTR szOtherStuff,
840                              HICON hIcon )
841 {
842     ABOUT_INFO info;
843     LOGFONTW logFont;
844     HRSRC hRes;
845     LPVOID template;
846     BOOL bRet;
847
848     TRACE("\n");
849
850     if(!(hRes = FindResourceA(shell32_hInstance, "SHELL_ABOUT_MSGBOX", (LPSTR)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 }