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