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