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