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