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