msi: Only insert entries into listbox if property value matches.
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 writable 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 static DWORD shgfi_get_exe_type(LPCWSTR szFullPath)
240 {
241     BOOL status = FALSE;
242     HANDLE hfile;
243     DWORD BinaryType;
244     IMAGE_DOS_HEADER mz_header;
245     IMAGE_NT_HEADERS nt;
246     DWORD len;
247     char magic[4];
248
249     status = GetBinaryTypeW (szFullPath, &BinaryType);
250     if (!status)
251         return 0;
252     if (BinaryType == SCS_DOS_BINARY || BinaryType == SCS_PIF_BINARY)
253         return 0x4d5a;
254
255     hfile = CreateFileW( szFullPath, GENERIC_READ, FILE_SHARE_READ,
256                          NULL, OPEN_EXISTING, 0, 0 );
257     if ( hfile == INVALID_HANDLE_VALUE )
258         return 0;
259
260     /*
261      * The next section is adapted from MODULE_GetBinaryType, as we need
262      * to examine the image header to get OS and version information. We
263      * know from calling GetBinaryTypeA that the image is valid and either
264      * an NE or PE, so much error handling can be omitted.
265      * Seek to the start of the file and read the header information.
266      */
267
268     SetFilePointer( hfile, 0, NULL, SEEK_SET );
269     ReadFile( hfile, &mz_header, sizeof(mz_header), &len, NULL );
270
271     SetFilePointer( hfile, mz_header.e_lfanew, NULL, SEEK_SET );
272     ReadFile( hfile, magic, sizeof(magic), &len, NULL );
273     if ( *(DWORD*)magic == IMAGE_NT_SIGNATURE )
274     {
275         SetFilePointer( hfile, mz_header.e_lfanew, NULL, SEEK_SET );
276         ReadFile( hfile, &nt, sizeof(nt), &len, NULL );
277         CloseHandle( hfile );
278         if (nt.OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_GUI)
279         {
280              return IMAGE_NT_SIGNATURE | 
281                    (nt.OptionalHeader.MajorSubsystemVersion << 24) |
282                    (nt.OptionalHeader.MinorSubsystemVersion << 16);
283         }
284         return IMAGE_NT_SIGNATURE;
285     }
286     else if ( *(WORD*)magic == IMAGE_OS2_SIGNATURE )
287     {
288         IMAGE_OS2_HEADER ne;
289         SetFilePointer( hfile, mz_header.e_lfanew, NULL, SEEK_SET );
290         ReadFile( hfile, &ne, sizeof(ne), &len, NULL );
291         CloseHandle( hfile );
292         if (ne.ne_exetyp == 2)
293             return IMAGE_OS2_SIGNATURE | (ne.ne_expver << 16);
294         return 0;
295     }
296     CloseHandle( hfile );
297     return 0;
298 }
299
300 /*************************************************************************
301  * SHELL_IsShortcut             [internal]
302  *
303  * Decide if an item id list points to a shell shortcut
304  */
305 BOOL SHELL_IsShortcut(LPCITEMIDLIST pidlLast)
306 {
307     char szTemp[MAX_PATH];
308     HKEY keyCls;
309     BOOL ret = FALSE;
310
311     if (_ILGetExtension(pidlLast, szTemp, MAX_PATH) &&
312           HCR_MapTypeToValueA(szTemp, szTemp, MAX_PATH, TRUE))
313     {
314         if (ERROR_SUCCESS == RegOpenKeyExA(HKEY_CLASSES_ROOT, szTemp, 0, KEY_QUERY_VALUE, &keyCls))
315         {
316           if (ERROR_SUCCESS == RegQueryValueExA(keyCls, "IsShortcut", NULL, NULL, NULL, NULL))
317             ret = TRUE;
318
319           RegCloseKey(keyCls);
320         }
321     }
322
323     return ret;
324 }
325
326 #define SHGFI_KNOWN_FLAGS \
327     (SHGFI_SMALLICON | SHGFI_OPENICON | SHGFI_SHELLICONSIZE | SHGFI_PIDL | \
328      SHGFI_USEFILEATTRIBUTES | SHGFI_ADDOVERLAYS | SHGFI_OVERLAYINDEX | \
329      SHGFI_ICON | SHGFI_DISPLAYNAME | SHGFI_TYPENAME | SHGFI_ATTRIBUTES | \
330      SHGFI_ICONLOCATION | SHGFI_EXETYPE | SHGFI_SYSICONINDEX | \
331      SHGFI_LINKOVERLAY | SHGFI_SELECTED | SHGFI_ATTR_SPECIFIED)
332
333 /*************************************************************************
334  * SHGetFileInfoW            [SHELL32.@]
335  *
336  */
337 DWORD_PTR WINAPI SHGetFileInfoW(LPCWSTR path,DWORD dwFileAttributes,
338                                 SHFILEINFOW *psfi, UINT sizeofpsfi, UINT flags )
339 {
340     WCHAR szLocation[MAX_PATH], szFullPath[MAX_PATH];
341     int iIndex;
342     DWORD_PTR ret = TRUE;
343     DWORD dwAttributes = 0;
344     IShellFolder * psfParent = NULL;
345     IExtractIconW * pei = NULL;
346     LPITEMIDLIST    pidlLast = NULL, pidl = NULL;
347     HRESULT hr = S_OK;
348     BOOL IconNotYetLoaded=TRUE;
349     UINT uGilFlags = 0;
350
351     TRACE("%s fattr=0x%x sfi=%p(attr=0x%08x) size=0x%x flags=0x%x\n",
352           (flags & SHGFI_PIDL)? "pidl" : debugstr_w(path), dwFileAttributes,
353           psfi, psfi->dwAttributes, sizeofpsfi, flags);
354
355     if ( (flags & SHGFI_USEFILEATTRIBUTES) && 
356          (flags & (SHGFI_ATTRIBUTES|SHGFI_EXETYPE|SHGFI_PIDL)))
357         return FALSE;
358
359     /* windows initializes these values regardless of the flags */
360     if (psfi != NULL)
361     {
362         psfi->szDisplayName[0] = '\0';
363         psfi->szTypeName[0] = '\0';
364         psfi->iIcon = 0;
365     }
366
367     if (!(flags & SHGFI_PIDL))
368     {
369         /* SHGetFileInfo should work with absolute and relative paths */
370         if (PathIsRelativeW(path))
371         {
372             GetCurrentDirectoryW(MAX_PATH, szLocation);
373             PathCombineW(szFullPath, szLocation, path);
374         }
375         else
376         {
377             lstrcpynW(szFullPath, path, MAX_PATH);
378         }
379     }
380
381     if (flags & SHGFI_EXETYPE)
382     {
383         if (flags != SHGFI_EXETYPE)
384             return 0;
385         return shgfi_get_exe_type(szFullPath);
386     }
387
388     /*
389      * psfi is NULL normally to query EXE type. If it is NULL, none of the
390      * below makes sense anyway. Windows allows this and just returns FALSE
391      */
392     if (psfi == NULL)
393         return FALSE;
394
395     /*
396      * translate the path into a pidl only when SHGFI_USEFILEATTRIBUTES
397      * is not specified.
398      * The pidl functions fail on not existing file names
399      */
400
401     if (flags & SHGFI_PIDL)
402     {
403         pidl = ILClone((LPCITEMIDLIST)path);
404     }
405     else if (!(flags & SHGFI_USEFILEATTRIBUTES))
406     {
407         hr = SHILCreateFromPathW(szFullPath, &pidl, &dwAttributes);
408     }
409
410     if ((flags & SHGFI_PIDL) || !(flags & SHGFI_USEFILEATTRIBUTES))
411     {
412         /* get the parent shellfolder */
413         if (pidl)
414         {
415             hr = SHBindToParent( pidl, &IID_IShellFolder, (LPVOID*)&psfParent,
416                                 (LPCITEMIDLIST*)&pidlLast );
417             if (SUCCEEDED(hr))
418                 pidlLast = ILClone(pidlLast);
419             ILFree(pidl);
420         }
421         else
422         {
423             ERR("pidl is null!\n");
424             return FALSE;
425         }
426     }
427
428     /* get the attributes of the child */
429     if (SUCCEEDED(hr) && (flags & SHGFI_ATTRIBUTES))
430     {
431         if (!(flags & SHGFI_ATTR_SPECIFIED))
432         {
433             psfi->dwAttributes = 0xffffffff;
434         }
435         IShellFolder_GetAttributesOf( psfParent, 1, (LPCITEMIDLIST*)&pidlLast,
436                                       &(psfi->dwAttributes) );
437     }
438
439     /* get the displayname */
440     if (SUCCEEDED(hr) && (flags & SHGFI_DISPLAYNAME))
441     {
442         if (flags & SHGFI_USEFILEATTRIBUTES)
443         {
444             lstrcpyW (psfi->szDisplayName, PathFindFileNameW(szFullPath));
445         }
446         else
447         {
448             STRRET str;
449             hr = IShellFolder_GetDisplayNameOf( psfParent, pidlLast,
450                                                 SHGDN_INFOLDER, &str);
451             StrRetToStrNW (psfi->szDisplayName, MAX_PATH, &str, pidlLast);
452         }
453     }
454
455     /* get the type name */
456     if (SUCCEEDED(hr) && (flags & SHGFI_TYPENAME))
457     {
458         static const WCHAR szFile[] = { 'F','i','l','e',0 };
459         static const WCHAR szDashFile[] = { '-','f','i','l','e',0 };
460
461         if (!(flags & SHGFI_USEFILEATTRIBUTES))
462         {
463             char ftype[80];
464
465             _ILGetFileType(pidlLast, ftype, 80);
466             MultiByteToWideChar(CP_ACP, 0, ftype, -1, psfi->szTypeName, 80 );
467         }
468         else
469         {
470             if (dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
471                 strcatW (psfi->szTypeName, szFile);
472             else 
473             {
474                 WCHAR sTemp[64];
475
476                 lstrcpyW(sTemp,PathFindExtensionW(szFullPath));
477                 if (!( HCR_MapTypeToValueW(sTemp, sTemp, 64, TRUE) &&
478                     HCR_MapTypeToValueW(sTemp, psfi->szTypeName, 80, FALSE )))
479                 {
480                     lstrcpynW (psfi->szTypeName, sTemp, 64);
481                     strcatW (psfi->szTypeName, szDashFile);
482                 }
483             }
484         }
485     }
486
487     /* ### icons ###*/
488     if (flags & SHGFI_OPENICON)
489         uGilFlags |= GIL_OPENICON;
490
491     if (flags & SHGFI_LINKOVERLAY)
492         uGilFlags |= GIL_FORSHORTCUT;
493     else if ((flags&SHGFI_ADDOVERLAYS) ||
494              (flags&(SHGFI_ICON|SHGFI_SMALLICON))==SHGFI_ICON)
495     {
496         if (SHELL_IsShortcut(pidlLast))
497             uGilFlags |= GIL_FORSHORTCUT;
498     }
499
500     if (flags & SHGFI_OVERLAYINDEX)
501         FIXME("SHGFI_OVERLAYINDEX unhandled\n");
502
503     if (flags & SHGFI_SELECTED)
504         FIXME("set icon to selected, stub\n");
505
506     if (flags & SHGFI_SHELLICONSIZE)
507         FIXME("set icon to shell size, stub\n");
508
509     /* get the iconlocation */
510     if (SUCCEEDED(hr) && (flags & SHGFI_ICONLOCATION ))
511     {
512         UINT uDummy,uFlags;
513
514         if (flags & SHGFI_USEFILEATTRIBUTES)
515         {
516             if (dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
517             {
518                 lstrcpyW(psfi->szDisplayName, swShell32Name);
519                 psfi->iIcon = -IDI_SHELL_FOLDER;
520             }
521             else
522             {
523                 WCHAR* szExt;
524                 static const WCHAR p1W[] = {'%','1',0};
525                 WCHAR sTemp [MAX_PATH];
526
527                 szExt = (LPWSTR) PathFindExtensionW(szFullPath);
528                 TRACE("szExt=%s\n", debugstr_w(szExt));
529                 if ( szExt &&
530                      HCR_MapTypeToValueW(szExt, sTemp, MAX_PATH, TRUE) &&
531                      HCR_GetDefaultIconW(sTemp, sTemp, MAX_PATH, &psfi->iIcon))
532                 {
533                     if (lstrcmpW(p1W, sTemp))
534                         strcpyW(psfi->szDisplayName, sTemp);
535                     else
536                     {
537                         /* the icon is in the file */
538                         strcpyW(psfi->szDisplayName, szFullPath);
539                     }
540                 }
541                 else
542                     ret = FALSE;
543             }
544         }
545         else
546         {
547             hr = IShellFolder_GetUIObjectOf(psfParent, 0, 1,
548                 (LPCITEMIDLIST*)&pidlLast, &IID_IExtractIconW,
549                 &uDummy, (LPVOID*)&pei);
550             if (SUCCEEDED(hr))
551             {
552                 hr = IExtractIconW_GetIconLocation(pei, uGilFlags,
553                     szLocation, MAX_PATH, &iIndex, &uFlags);
554
555                 if (uFlags & GIL_NOTFILENAME)
556                     ret = FALSE;
557                 else
558                 {
559                     lstrcpyW (psfi->szDisplayName, szLocation);
560                     psfi->iIcon = iIndex;
561                 }
562                 IExtractIconW_Release(pei);
563             }
564         }
565     }
566
567     /* get icon index (or load icon)*/
568     if (SUCCEEDED(hr) && (flags & (SHGFI_ICON | SHGFI_SYSICONINDEX)))
569     {
570         if (flags & SHGFI_USEFILEATTRIBUTES)
571         {
572             WCHAR sTemp [MAX_PATH];
573             WCHAR * szExt;
574             int icon_idx=0;
575
576             lstrcpynW(sTemp, szFullPath, MAX_PATH);
577
578             if (dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
579                 psfi->iIcon = SIC_GetIconIndex(swShell32Name, -IDI_SHELL_FOLDER, 0);
580             else
581             {
582                 static const WCHAR p1W[] = {'%','1',0};
583
584                 psfi->iIcon = 0;
585                 szExt = (LPWSTR) PathFindExtensionW(sTemp);
586                 if ( szExt &&
587                      HCR_MapTypeToValueW(szExt, sTemp, MAX_PATH, TRUE) &&
588                      HCR_GetDefaultIconW(sTemp, sTemp, MAX_PATH, &icon_idx))
589                 {
590                     if (!lstrcmpW(p1W,sTemp))            /* icon is in the file */
591                         strcpyW(sTemp, szFullPath);
592
593                     if (flags & SHGFI_SYSICONINDEX) 
594                     {
595                         psfi->iIcon = SIC_GetIconIndex(sTemp,icon_idx,0);
596                         if (psfi->iIcon == -1)
597                             psfi->iIcon = 0;
598                     }
599                     else 
600                     {
601                         IconNotYetLoaded=FALSE;
602                         if (flags & SHGFI_SMALLICON)
603                             PrivateExtractIconsW( sTemp,icon_idx,
604                                 GetSystemMetrics( SM_CXSMICON ),
605                                 GetSystemMetrics( SM_CYSMICON ),
606                                 &psfi->hIcon, 0, 1, 0);
607                         else
608                             PrivateExtractIconsW( sTemp, icon_idx,
609                                 GetSystemMetrics( SM_CXICON),
610                                 GetSystemMetrics( SM_CYICON),
611                                 &psfi->hIcon, 0, 1, 0);
612                         psfi->iIcon = icon_idx;
613                     }
614                 }
615             }
616         }
617         else
618         {
619             if (!(PidlToSicIndex(psfParent, pidlLast, !(flags & SHGFI_SMALLICON),
620                 uGilFlags, &(psfi->iIcon))))
621             {
622                 ret = FALSE;
623             }
624         }
625         if (ret)
626         {
627             if (flags & SHGFI_SMALLICON)
628                 ret = (DWORD_PTR) ShellSmallIconList;
629             else
630                 ret = (DWORD_PTR) ShellBigIconList;
631         }
632     }
633
634     /* icon handle */
635     if (SUCCEEDED(hr) && (flags & SHGFI_ICON) && IconNotYetLoaded)
636     {
637         if (flags & SHGFI_SMALLICON)
638             psfi->hIcon = ImageList_GetIcon( ShellSmallIconList, psfi->iIcon, ILD_NORMAL);
639         else
640             psfi->hIcon = ImageList_GetIcon( ShellBigIconList, psfi->iIcon, ILD_NORMAL);
641     }
642
643     if (flags & ~SHGFI_KNOWN_FLAGS)
644         FIXME("unknown flags %08x\n", flags & ~SHGFI_KNOWN_FLAGS);
645
646     if (psfParent)
647         IShellFolder_Release(psfParent);
648
649     if (hr != S_OK)
650         ret = FALSE;
651
652     SHFree(pidlLast);
653
654 #ifdef MORE_DEBUG
655     TRACE ("icon=%p index=0x%08x attr=0x%08x name=%s type=%s ret=0x%08lx\n",
656            psfi->hIcon, psfi->iIcon, psfi->dwAttributes,
657            debugstr_w(psfi->szDisplayName), debugstr_w(psfi->szTypeName), ret);
658 #endif
659
660     return ret;
661 }
662
663 /*************************************************************************
664  * SHGetFileInfoA            [SHELL32.@]
665  */
666 DWORD_PTR WINAPI SHGetFileInfoA(LPCSTR path,DWORD dwFileAttributes,
667                                 SHFILEINFOA *psfi, UINT sizeofpsfi,
668                                 UINT flags )
669 {
670     INT len;
671     LPWSTR temppath = NULL;
672     LPCWSTR pathW;
673     DWORD ret;
674     SHFILEINFOW temppsfi;
675
676     if (flags & SHGFI_PIDL)
677     {
678         /* path contains a pidl */
679         pathW = (LPCWSTR)path;
680     }
681     else
682     {
683         len = MultiByteToWideChar(CP_ACP, 0, path, -1, NULL, 0);
684         temppath = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
685         MultiByteToWideChar(CP_ACP, 0, path, -1, temppath, len);
686         pathW = temppath;
687     }
688
689     if (psfi && (flags & SHGFI_ATTR_SPECIFIED))
690         temppsfi.dwAttributes=psfi->dwAttributes;
691
692     if (psfi == NULL)
693         ret = SHGetFileInfoW(pathW, dwFileAttributes, NULL, sizeof(temppsfi), flags);
694     else
695         ret = SHGetFileInfoW(pathW, dwFileAttributes, &temppsfi, sizeof(temppsfi), flags);
696
697     if (psfi)
698     {
699         if(flags & SHGFI_ICON)
700             psfi->hIcon=temppsfi.hIcon;
701         if(flags & (SHGFI_SYSICONINDEX|SHGFI_ICON|SHGFI_ICONLOCATION))
702             psfi->iIcon=temppsfi.iIcon;
703         if(flags & SHGFI_ATTRIBUTES)
704             psfi->dwAttributes=temppsfi.dwAttributes;
705         if(flags & (SHGFI_DISPLAYNAME|SHGFI_ICONLOCATION))
706         {
707             WideCharToMultiByte(CP_ACP, 0, temppsfi.szDisplayName, -1,
708                   psfi->szDisplayName, sizeof(psfi->szDisplayName), NULL, NULL);
709         }
710         if(flags & SHGFI_TYPENAME)
711         {
712             WideCharToMultiByte(CP_ACP, 0, temppsfi.szTypeName, -1,
713                   psfi->szTypeName, sizeof(psfi->szTypeName), NULL, NULL);
714         }
715     }
716
717     HeapFree(GetProcessHeap(), 0, temppath);
718
719     return ret;
720 }
721
722 /*************************************************************************
723  * DuplicateIcon            [SHELL32.@]
724  */
725 HICON WINAPI DuplicateIcon( HINSTANCE hInstance, HICON hIcon)
726 {
727     ICONINFO IconInfo;
728     HICON hDupIcon = 0;
729
730     TRACE("%p %p\n", hInstance, hIcon);
731
732     if (GetIconInfo(hIcon, &IconInfo))
733     {
734         hDupIcon = CreateIconIndirect(&IconInfo);
735
736         /* clean up hbmMask and hbmColor */
737         DeleteObject(IconInfo.hbmMask);
738         DeleteObject(IconInfo.hbmColor);
739     }
740
741     return hDupIcon;
742 }
743
744 /*************************************************************************
745  * ExtractIconA                [SHELL32.@]
746  */
747 HICON WINAPI ExtractIconA(HINSTANCE hInstance, LPCSTR lpszFile, UINT nIconIndex)
748 {   
749     HICON ret;
750     INT len = MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, NULL, 0);
751     LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
752
753     TRACE("%p %s %d\n", hInstance, lpszFile, nIconIndex);
754
755     MultiByteToWideChar(CP_ACP, 0, lpszFile, -1, lpwstrFile, len);
756     ret = ExtractIconW(hInstance, lpwstrFile, nIconIndex);
757     HeapFree(GetProcessHeap(), 0, lpwstrFile);
758
759     return ret;
760 }
761
762 /*************************************************************************
763  * ExtractIconW                [SHELL32.@]
764  */
765 HICON WINAPI ExtractIconW(HINSTANCE hInstance, LPCWSTR lpszFile, UINT nIconIndex)
766 {
767     HICON  hIcon = NULL;
768     UINT ret;
769     UINT cx = GetSystemMetrics(SM_CXICON), cy = GetSystemMetrics(SM_CYICON);
770
771     TRACE("%p %s %d\n", hInstance, debugstr_w(lpszFile), nIconIndex);
772
773     if (nIconIndex == 0xFFFFFFFF)
774     {
775         ret = PrivateExtractIconsW(lpszFile, 0, cx, cy, NULL, NULL, 0, LR_DEFAULTCOLOR);
776         if (ret != 0xFFFFFFFF && ret)
777             return (HICON)(UINT_PTR)ret;
778         return NULL;
779     }
780     else
781         ret = PrivateExtractIconsW(lpszFile, nIconIndex, cx, cy, &hIcon, NULL, 1, LR_DEFAULTCOLOR);
782
783     if (ret == 0xFFFFFFFF)
784         return (HICON)1;
785     else if (ret > 0 && hIcon)
786         return hIcon;
787
788     return NULL;
789 }
790
791 /*************************************************************************
792  * Printer_LoadIconsW        [SHELL32.205]
793  */
794 VOID WINAPI Printer_LoadIconsW(LPCWSTR wsPrinterName, HICON * pLargeIcon, HICON * pSmallIcon)
795 {
796     INT iconindex=IDI_SHELL_PRINTER;
797
798     TRACE("(%s, %p, %p)\n", debugstr_w(wsPrinterName), pLargeIcon, pSmallIcon);
799
800     /* We should check if wsPrinterName is
801        1. the Default Printer or not
802        2. connected or not
803        3. a Local Printer or a Network-Printer
804        and use different Icons
805     */
806     if((wsPrinterName != NULL) && (wsPrinterName[0] != 0))
807     {
808         FIXME("(select Icon by PrinterName %s not implemented)\n", debugstr_w(wsPrinterName));
809     }
810
811     if(pLargeIcon != NULL)
812         *pLargeIcon = LoadImageW(shell32_hInstance,
813                                  (LPCWSTR) MAKEINTRESOURCE(iconindex), IMAGE_ICON,
814                                  0, 0, LR_DEFAULTCOLOR|LR_DEFAULTSIZE);
815
816     if(pSmallIcon != NULL)
817         *pSmallIcon = LoadImageW(shell32_hInstance,
818                                  (LPCWSTR) MAKEINTRESOURCE(iconindex), IMAGE_ICON,
819                                  16, 16, LR_DEFAULTCOLOR);
820 }
821
822 /*************************************************************************
823  * Printers_RegisterWindowW        [SHELL32.213]
824  * used by "printui.dll":
825  * find the Window of the given Type for the specific Printer and 
826  * return the already existent hwnd or open a new window
827  */
828 BOOL WINAPI Printers_RegisterWindowW(LPCWSTR wsPrinter, DWORD dwType,
829             HANDLE * phClassPidl, HWND * phwnd)
830 {
831     FIXME("(%s, %x, %p (%p), %p (%p)) stub!\n", debugstr_w(wsPrinter), dwType,
832                 phClassPidl, (phClassPidl != NULL) ? *(phClassPidl) : NULL,
833                 phwnd, (phwnd != NULL) ? *(phwnd) : NULL);
834
835     return FALSE;
836
837
838 /*************************************************************************
839  * Printers_UnregisterWindow      [SHELL32.214]
840  */
841 VOID WINAPI Printers_UnregisterWindow(HANDLE hClassPidl, HWND hwnd)
842 {
843     FIXME("(%p, %p) stub!\n", hClassPidl, hwnd);
844
845
846 /*************************************************************************/
847
848 typedef struct
849 {
850     LPCWSTR  szApp;
851     LPCWSTR  szOtherStuff;
852     HICON hIcon;
853     HFONT hFont;
854 } ABOUT_INFO;
855
856 #define IDC_STATIC_TEXT1   100
857 #define IDC_STATIC_TEXT2   101
858 #define IDC_LISTBOX        99
859 #define IDC_WINE_TEXT      98
860
861 #define DROP_FIELD_TOP    (-15)
862 #define DROP_FIELD_HEIGHT  15
863
864 static BOOL __get_dropline( HWND hWnd, LPRECT lprect )
865 {
866     HWND hWndCtl = GetDlgItem(hWnd, IDC_WINE_TEXT);
867
868     if( hWndCtl )
869     {
870         GetWindowRect( hWndCtl, lprect );
871         MapWindowPoints( 0, hWnd, (LPPOINT)lprect, 2 );
872         lprect->bottom = (lprect->top += DROP_FIELD_TOP);
873         return TRUE;
874     }
875     return FALSE;
876 }
877
878 /*************************************************************************
879  * SHAppBarMessage            [SHELL32.@]
880  */
881 UINT WINAPI SHAppBarMessage(DWORD msg, PAPPBARDATA data)
882 {
883     int width=data->rc.right - data->rc.left;
884     int height=data->rc.bottom - data->rc.top;
885     RECT rec=data->rc;
886
887     switch (msg)
888     {
889     case ABM_GETSTATE:
890         return ABS_ALWAYSONTOP | ABS_AUTOHIDE;
891     case ABM_GETTASKBARPOS:
892         GetWindowRect(data->hWnd, &rec);
893         data->rc=rec;
894         return TRUE;
895     case ABM_ACTIVATE:
896         SetActiveWindow(data->hWnd);
897         return TRUE;
898     case ABM_GETAUTOHIDEBAR:
899         data->hWnd=GetActiveWindow();
900         return TRUE;
901     case ABM_NEW:
902         /* cbSize, hWnd, and uCallbackMessage are used. All other ignored */
903         SetWindowPos(data->hWnd,HWND_TOP,0,0,0,0,SWP_SHOWWINDOW|SWP_NOMOVE|SWP_NOSIZE);
904         return TRUE;
905     case ABM_QUERYPOS:
906         GetWindowRect(data->hWnd, &(data->rc));
907         return TRUE;
908     case ABM_REMOVE:
909         FIXME("ABM_REMOVE broken\n");
910         /* FIXME: this is wrong; should it be DestroyWindow instead? */
911         /*CloseHandle(data->hWnd);*/
912         return TRUE;
913     case ABM_SETAUTOHIDEBAR:
914         SetWindowPos(data->hWnd,HWND_TOP,rec.left+1000,rec.top,
915                          width,height,SWP_SHOWWINDOW);
916         return TRUE;
917     case ABM_SETPOS:
918         data->uEdge=(ABE_RIGHT | ABE_LEFT);
919         SetWindowPos(data->hWnd,HWND_TOP,data->rc.left,data->rc.top,
920                      width,height,SWP_SHOWWINDOW);
921         return TRUE;
922     case ABM_WINDOWPOSCHANGED:
923         return TRUE;
924     }
925     return FALSE;
926 }
927
928 /*************************************************************************
929  * SHHelpShortcuts_RunDLLA        [SHELL32.@]
930  *
931  */
932 DWORD WINAPI SHHelpShortcuts_RunDLLA(DWORD dwArg1, DWORD dwArg2, DWORD dwArg3, DWORD dwArg4)
933 {
934     FIXME("(%x, %x, %x, %x) stub!\n", dwArg1, dwArg2, dwArg3, dwArg4);
935     return 0;
936 }
937
938 /*************************************************************************
939  * SHHelpShortcuts_RunDLLA        [SHELL32.@]
940  *
941  */
942 DWORD WINAPI SHHelpShortcuts_RunDLLW(DWORD dwArg1, DWORD dwArg2, DWORD dwArg3, DWORD dwArg4)
943 {
944     FIXME("(%x, %x, %x, %x) stub!\n", dwArg1, dwArg2, dwArg3, dwArg4);
945     return 0;
946 }
947
948 /*************************************************************************
949  * SHLoadInProc                [SHELL32.@]
950  * Create an instance of specified object class from within
951  * the shell process and release it immediately
952  */
953 HRESULT WINAPI SHLoadInProc (REFCLSID rclsid)
954 {
955     void *ptr = NULL;
956
957     TRACE("%s\n", debugstr_guid(rclsid));
958
959     CoCreateInstance(rclsid, NULL, CLSCTX_INPROC_SERVER, &IID_IUnknown,&ptr);
960     if(ptr)
961     {
962         IUnknown * pUnk = ptr;
963         IUnknown_Release(pUnk);
964         return NOERROR;
965     }
966     return DISP_E_MEMBERNOTFOUND;
967 }
968
969 /*************************************************************************
970  * AboutDlgProc            (internal)
971  */
972 INT_PTR CALLBACK AboutDlgProc( HWND hWnd, UINT msg, WPARAM wParam,
973                               LPARAM lParam )
974 {
975     HWND hWndCtl;
976
977     TRACE("\n");
978
979     switch(msg)
980     {
981     case WM_INITDIALOG:
982         {
983             ABOUT_INFO *info = (ABOUT_INFO *)lParam;
984             WCHAR Template[512], AppTitle[512];
985
986             if (info)
987             {
988                 const char* const *pstr = SHELL_Authors;
989                 SendDlgItemMessageW(hWnd, stc1, STM_SETICON,(WPARAM)info->hIcon, 0);
990                 GetWindowTextW( hWnd, Template, sizeof(Template)/sizeof(WCHAR) );
991                 sprintfW( AppTitle, Template, info->szApp );
992                 SetWindowTextW( hWnd, AppTitle );
993                 SetWindowTextW( GetDlgItem(hWnd, IDC_STATIC_TEXT1), info->szApp );
994                 SetWindowTextW( GetDlgItem(hWnd, IDC_STATIC_TEXT2), info->szOtherStuff );
995                 hWndCtl = GetDlgItem(hWnd, IDC_LISTBOX);
996                 SendMessageW( hWndCtl, WM_SETREDRAW, 0, 0 );
997                 SendMessageW( hWndCtl, WM_SETFONT, (WPARAM)info->hFont, 0 );
998                 while (*pstr)
999                 {
1000                     WCHAR name[64];
1001                     /* authors list is in utf-8 format */
1002                     MultiByteToWideChar( CP_UTF8, 0, *pstr, -1, name, sizeof(name)/sizeof(WCHAR) );
1003                     SendMessageW( hWndCtl, LB_ADDSTRING, (WPARAM)-1, (LPARAM)name );
1004                     pstr++;
1005                 }
1006                 SendMessageW( hWndCtl, WM_SETREDRAW, 1, 0 );
1007             }
1008         }
1009         return 1;
1010
1011     case WM_PAINT:
1012         {
1013             RECT rect;
1014             PAINTSTRUCT ps;
1015             HDC hDC = BeginPaint( hWnd, &ps );
1016
1017             if (__get_dropline( hWnd, &rect ))
1018             {
1019                 SelectObject( hDC, GetStockObject( BLACK_PEN ) );
1020                 MoveToEx( hDC, rect.left, rect.top, NULL );
1021                 LineTo( hDC, rect.right, rect.bottom );
1022             }
1023             EndPaint( hWnd, &ps );
1024         }
1025     break;
1026
1027     case WM_COMMAND:
1028         if (wParam == IDOK || wParam == IDCANCEL)
1029         {
1030             EndDialog(hWnd, TRUE);
1031             return TRUE;
1032         }
1033         break;
1034     case WM_CLOSE:
1035       EndDialog(hWnd, TRUE);
1036       break;
1037     }
1038
1039     return 0;
1040 }
1041
1042
1043 /*************************************************************************
1044  * ShellAboutA                [SHELL32.288]
1045  */
1046 BOOL WINAPI ShellAboutA( HWND hWnd, LPCSTR szApp, LPCSTR szOtherStuff, HICON hIcon )
1047 {
1048     BOOL ret;
1049     LPWSTR appW = NULL, otherW = NULL;
1050     int len;
1051
1052     if (szApp)
1053     {
1054         len = MultiByteToWideChar(CP_ACP, 0, szApp, -1, NULL, 0);
1055         appW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1056         MultiByteToWideChar(CP_ACP, 0, szApp, -1, appW, len);
1057     }
1058     if (szOtherStuff)
1059     {
1060         len = MultiByteToWideChar(CP_ACP, 0, szOtherStuff, -1, NULL, 0);
1061         otherW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1062         MultiByteToWideChar(CP_ACP, 0, szOtherStuff, -1, otherW, len);
1063     }
1064
1065     ret = ShellAboutW(hWnd, appW, otherW, hIcon);
1066
1067     HeapFree(GetProcessHeap(), 0, otherW);
1068     HeapFree(GetProcessHeap(), 0, appW);
1069     return ret;
1070 }
1071
1072
1073 /*************************************************************************
1074  * ShellAboutW                [SHELL32.289]
1075  */
1076 BOOL WINAPI ShellAboutW( HWND hWnd, LPCWSTR szApp, LPCWSTR szOtherStuff,
1077                              HICON hIcon )
1078 {
1079     ABOUT_INFO info;
1080     LOGFONTW logFont;
1081     HRSRC hRes;
1082     LPVOID template;
1083     BOOL bRet;
1084     static const WCHAR wszSHELL_ABOUT_MSGBOX[] =
1085         {'S','H','E','L','L','_','A','B','O','U','T','_','M','S','G','B','O','X',0};
1086
1087     TRACE("\n");
1088
1089     if(!(hRes = FindResourceW(shell32_hInstance, wszSHELL_ABOUT_MSGBOX, (LPWSTR)RT_DIALOG)))
1090         return FALSE;
1091     if(!(template = (LPVOID)LoadResource(shell32_hInstance, hRes)))
1092         return FALSE;
1093     info.szApp        = szApp;
1094     info.szOtherStuff = szOtherStuff;
1095     info.hIcon        = hIcon ? hIcon : LoadIconW( 0, (LPWSTR)IDI_WINLOGO );
1096
1097     SystemParametersInfoW( SPI_GETICONTITLELOGFONT, 0, &logFont, 0 );
1098     info.hFont = CreateFontIndirectW( &logFont );
1099
1100     bRet = DialogBoxIndirectParamW((HINSTANCE)GetWindowLongPtrW( hWnd, GWLP_HINSTANCE ),
1101                                    template, hWnd, AboutDlgProc, (LPARAM)&info );
1102     DeleteObject(info.hFont);
1103     return bRet;
1104 }
1105
1106 /*************************************************************************
1107  * FreeIconList (SHELL32.@)
1108  */
1109 void WINAPI FreeIconList( DWORD dw )
1110 {
1111     FIXME("%x: stub\n",dw);
1112 }
1113
1114 /*************************************************************************
1115  * SHLoadNonloadedIconOverlayIdentifiers (SHELL32.@)
1116  */
1117 HRESULT WINAPI SHLoadNonloadedIconOverlayIdentifiers( VOID )
1118 {
1119     FIXME("stub\n");
1120     return S_OK;
1121 }
1122
1123 /***********************************************************************
1124  * DllGetVersion [SHELL32.@]
1125  *
1126  * Retrieves version information of the 'SHELL32.DLL'
1127  *
1128  * PARAMS
1129  *     pdvi [O] pointer to version information structure.
1130  *
1131  * RETURNS
1132  *     Success: S_OK
1133  *     Failure: E_INVALIDARG
1134  *
1135  * NOTES
1136  *     Returns version of a shell32.dll from IE4.01 SP1.
1137  */
1138
1139 HRESULT WINAPI DllGetVersion (DLLVERSIONINFO *pdvi)
1140 {
1141     /* FIXME: shouldn't these values come from the version resource? */
1142     if (pdvi->cbSize == sizeof(DLLVERSIONINFO) ||
1143         pdvi->cbSize == sizeof(DLLVERSIONINFO2))
1144     {
1145         pdvi->dwMajorVersion = WINE_FILEVERSION_MAJOR;
1146         pdvi->dwMinorVersion = WINE_FILEVERSION_MINOR;
1147         pdvi->dwBuildNumber = WINE_FILEVERSION_BUILD;
1148         pdvi->dwPlatformID = WINE_FILEVERSION_PLATFORMID;
1149         if (pdvi->cbSize == sizeof(DLLVERSIONINFO2))
1150         {
1151             DLLVERSIONINFO2 *pdvi2 = (DLLVERSIONINFO2 *)pdvi;
1152
1153             pdvi2->dwFlags = 0;
1154             pdvi2->ullVersion = MAKEDLLVERULL(WINE_FILEVERSION_MAJOR,
1155                                               WINE_FILEVERSION_MINOR,
1156                                               WINE_FILEVERSION_BUILD,
1157                                               WINE_FILEVERSION_PLATFORMID);
1158         }
1159         TRACE("%u.%u.%u.%u\n",
1160               pdvi->dwMajorVersion, pdvi->dwMinorVersion,
1161               pdvi->dwBuildNumber, pdvi->dwPlatformID);
1162         return S_OK;
1163     }
1164     else
1165     {
1166         WARN("wrong DLLVERSIONINFO size from app\n");
1167         return E_INVALIDARG;
1168     }
1169 }
1170
1171 /*************************************************************************
1172  * global variables of the shell32.dll
1173  * all are once per process
1174  *
1175  */
1176 HINSTANCE    shell32_hInstance = 0;
1177 HIMAGELIST   ShellSmallIconList = 0;
1178 HIMAGELIST   ShellBigIconList = 0;
1179
1180
1181 /*************************************************************************
1182  * SHELL32 DllMain
1183  *
1184  * NOTES
1185  *  calling oleinitialize here breaks sone apps.
1186  */
1187 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID fImpLoad)
1188 {
1189     TRACE("%p 0x%x %p\n", hinstDLL, fdwReason, fImpLoad);
1190
1191     switch (fdwReason)
1192     {
1193     case DLL_PROCESS_ATTACH:
1194         shell32_hInstance = hinstDLL;
1195         DisableThreadLibraryCalls(shell32_hInstance);
1196
1197         /* get full path to this DLL for IExtractIconW_fnGetIconLocation() */
1198         GetModuleFileNameW(hinstDLL, swShell32Name, MAX_PATH);
1199         swShell32Name[MAX_PATH - 1] = '\0';
1200
1201         InitCommonControlsEx(NULL);
1202
1203         SIC_Initialize();
1204         InitChangeNotifications();
1205         break;
1206
1207     case DLL_PROCESS_DETACH:
1208         shell32_hInstance = 0;
1209         SIC_Destroy();
1210         FreeChangeNotifications();
1211         break;
1212     }
1213     return TRUE;
1214 }
1215
1216 /*************************************************************************
1217  * DllInstall         [SHELL32.@]
1218  *
1219  * PARAMETERS
1220  *
1221  *    BOOL bInstall - TRUE for install, FALSE for uninstall
1222  *    LPCWSTR pszCmdLine - command line (unused by shell32?)
1223  */
1224
1225 HRESULT WINAPI DllInstall(BOOL bInstall, LPCWSTR cmdline)
1226 {
1227     FIXME("%s %s: stub\n", bInstall ? "TRUE":"FALSE", debugstr_w(cmdline));
1228     return S_OK;        /* indicate success */
1229 }
1230
1231 /***********************************************************************
1232  *              DllCanUnloadNow (SHELL32.@)
1233  */
1234 HRESULT WINAPI DllCanUnloadNow(void)
1235 {
1236     FIXME("stub\n");
1237     return S_FALSE;
1238 }