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