Tests for recent variant changes.
[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 "wine/winuser16.h"
43 #include "authors.h"
44 #include "heap.h"
45 #include "pidl.h"
46 #include "shell32_main.h"
47
48 #include "wine/debug.h"
49 #include "wine/unicode.h"
50
51 WINE_DEFAULT_DEBUG_CHANNEL(shell);
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;
96
97         hargv=0;
98         size=16;
99         do {
100             size*=2;
101             hargv=GlobalReAlloc(hargv, size, 0);
102             argv=GlobalLock(hargv);
103         } while (GetModuleFileNameW(0, (LPWSTR)(argv+1), size-sizeof(LPWSTR)) == 0);
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 { LPCSTR  szApp;
613     LPCSTR  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 {   HWND hWndCtl;
724     char Template[512], AppTitle[512];
725
726     TRACE("\n");
727
728     switch(msg)
729     { case WM_INITDIALOG:
730       { ABOUT_INFO *info = (ABOUT_INFO *)lParam;
731             if (info)
732         { const char* const *pstr = SHELL_People;
733                 SendDlgItemMessageA(hWnd, stc1, STM_SETICON,(WPARAM)info->hIcon, 0);
734                 GetWindowTextA( hWnd, Template, sizeof(Template) );
735                 sprintf( AppTitle, Template, info->szApp );
736                 SetWindowTextA( hWnd, AppTitle );
737                 SetWindowTextA( GetDlgItem(hWnd, IDC_STATIC_TEXT),
738                                   info->szOtherStuff );
739                 hWndCtl = GetDlgItem(hWnd, IDC_LISTBOX);
740                 SendMessageA( hWndCtl, WM_SETREDRAW, 0, 0 );
741                 if (!hIconTitleFont)
742                 {
743                     LOGFONTA logFont;
744                     SystemParametersInfoA( SPI_GETICONTITLELOGFONT, 0, &logFont, 0 );
745                     hIconTitleFont = CreateFontIndirectA( &logFont );
746                 }
747                 SendMessageA( hWndCtl, WM_SETFONT, HICON_16(hIconTitleFont), 0 );
748                 while (*pstr)
749           { SendMessageA( hWndCtl, LB_ADDSTRING, (WPARAM)-1, (LPARAM)*pstr );
750                     pstr++;
751                 }
752                 SendMessageA( hWndCtl, WM_SETREDRAW, 1, 0 );
753             }
754         }
755         return 1;
756
757     case WM_PAINT:
758       { RECT rect;
759             PAINTSTRUCT ps;
760             HDC hDC = BeginPaint( hWnd, &ps );
761
762             if( __get_dropline( hWnd, &rect ) ) {
763                 SelectObject( hDC, GetStockObject( BLACK_PEN ) );
764                 MoveToEx( hDC, rect.left, rect.top, NULL );
765                 LineTo( hDC, rect.right, rect.bottom );
766             }
767             EndPaint( hWnd, &ps );
768         }
769         break;
770
771 #if 0  /* FIXME: should use DoDragDrop */
772     case WM_LBTRACKPOINT:
773         hWndCtl = GetDlgItem(hWnd, IDC_LISTBOX);
774         if( (INT16)GetKeyState( VK_CONTROL ) < 0 )
775       { if( DragDetect( hWndCtl, *((LPPOINT)&lParam) ) )
776         { INT idx = SendMessageA( hWndCtl, LB_GETCURSEL, 0, 0 );
777                 if( idx != -1 )
778           { INT length = SendMessageA( hWndCtl, LB_GETTEXTLEN, (WPARAM)idx, 0 );
779                     HGLOBAL16 hMemObj = GlobalAlloc16( GMEM_MOVEABLE, length + 1 );
780                     char* pstr = (char*)GlobalLock16( hMemObj );
781
782                     if( pstr )
783             { HCURSOR hCursor = LoadCursorA( 0, MAKEINTRESOURCEA(OCR_DRAGOBJECT) );
784                         SendMessageA( hWndCtl, LB_GETTEXT, (WPARAM)idx, (LPARAM)pstr );
785                         SendMessageA( hWndCtl, LB_DELETESTRING, (WPARAM)idx, 0 );
786                         UpdateWindow( hWndCtl );
787                         if( !DragObject16((HWND16)hWnd, (HWND16)hWnd, DRAGOBJ_DATA, 0, (WORD)hMemObj, hCursor) )
788                             SendMessageA( hWndCtl, LB_ADDSTRING, (WPARAM)-1, (LPARAM)pstr );
789                     }
790             if( hMemObj )
791               GlobalFree16( hMemObj );
792                 }
793             }
794         }
795         break;
796 #endif
797
798     case WM_QUERYDROPOBJECT:
799         if( wParam == 0 )
800       { LPDRAGINFO16 lpDragInfo = MapSL((SEGPTR)lParam);
801             if( lpDragInfo && lpDragInfo->wFlags == DRAGOBJ_DATA )
802         { RECT rect;
803                 if( __get_dropline( hWnd, &rect ) )
804           { POINT pt;
805             pt.x=lpDragInfo->pt.x;
806             pt.x=lpDragInfo->pt.y;
807                     rect.bottom += DROP_FIELD_HEIGHT;
808                     if( PtInRect( &rect, pt ) )
809             { SetWindowLongA( hWnd, DWL_MSGRESULT, 1 );
810                         return TRUE;
811                     }
812                 }
813             }
814         }
815         break;
816
817     case WM_DROPOBJECT:
818         if( wParam == (WPARAM)hWnd )
819       { LPDRAGINFO16 lpDragInfo = MapSL((SEGPTR)lParam);
820             if( lpDragInfo && lpDragInfo->wFlags == DRAGOBJ_DATA && lpDragInfo->hList )
821         { char* pstr = (char*)GlobalLock16( (HGLOBAL16)(lpDragInfo->hList) );
822                 if( pstr )
823           { static char __appendix_str[] = " with";
824
825                     hWndCtl = GetDlgItem( hWnd, IDC_WINE_TEXT );
826                     SendMessageA( hWndCtl, WM_GETTEXT, 512, (LPARAM)Template );
827                     if( !strncmp( Template, "WINE", 4 ) )
828                         SetWindowTextA( GetDlgItem(hWnd, IDC_STATIC_TEXT), Template );
829                     else
830           { char* pch = Template + strlen(Template) - strlen(__appendix_str);
831                         *pch = '\0';
832                         SendMessageA( GetDlgItem(hWnd, IDC_LISTBOX), LB_ADDSTRING,
833                                         (WPARAM)-1, (LPARAM)Template );
834                     }
835
836                     strcpy( Template, pstr );
837                     strcat( Template, __appendix_str );
838                     SetWindowTextA( hWndCtl, Template );
839                     SetWindowLongA( hWnd, DWL_MSGRESULT, 1 );
840                     return TRUE;
841                 }
842             }
843         }
844         break;
845
846     case WM_COMMAND:
847         if (wParam == IDOK || wParam == IDCANCEL)
848         {
849             EndDialog(hWnd, TRUE);
850             return TRUE;
851         }
852         break;
853     case WM_CLOSE:
854       EndDialog(hWnd, TRUE);
855       break;
856     }
857
858     return 0;
859 }
860
861
862 /*************************************************************************
863  * ShellAboutA                          [SHELL32.288]
864  */
865 BOOL WINAPI ShellAboutA( HWND hWnd, LPCSTR szApp, LPCSTR szOtherStuff,
866                              HICON hIcon )
867 {   ABOUT_INFO info;
868     HRSRC hRes;
869     LPVOID template;
870     TRACE("\n");
871
872     if(!(hRes = FindResourceA(shell32_hInstance, "SHELL_ABOUT_MSGBOX", (LPSTR)RT_DIALOG)))
873         return FALSE;
874     if(!(template = (LPVOID)LoadResource(shell32_hInstance, hRes)))
875         return FALSE;
876
877     info.szApp        = szApp;
878     info.szOtherStuff = szOtherStuff;
879     info.hIcon        = hIcon;
880     if (!hIcon) info.hIcon = LoadIconA( 0, (LPSTR)IDI_WINLOGO );
881     return DialogBoxIndirectParamA( (HINSTANCE)GetWindowLongA( hWnd, GWL_HINSTANCE ),
882                                       template, hWnd, AboutDlgProc, (LPARAM)&info );
883 }
884
885
886 /*************************************************************************
887  * ShellAboutW                          [SHELL32.289]
888  */
889 BOOL WINAPI ShellAboutW( HWND hWnd, LPCWSTR szApp, LPCWSTR szOtherStuff,
890                              HICON hIcon )
891 {   BOOL ret;
892     ABOUT_INFO info;
893     HRSRC hRes;
894     LPVOID template;
895
896     TRACE("\n");
897
898     if(!(hRes = FindResourceA(shell32_hInstance, "SHELL_ABOUT_MSGBOX", (LPSTR)RT_DIALOG)))
899         return FALSE;
900     if(!(template = (LPVOID)LoadResource(shell32_hInstance, hRes)))
901         return FALSE;
902
903     info.szApp        = HEAP_strdupWtoA( GetProcessHeap(), 0, szApp );
904     info.szOtherStuff = HEAP_strdupWtoA( GetProcessHeap(), 0, szOtherStuff );
905     info.hIcon        = hIcon;
906     if (!hIcon) info.hIcon = LoadIconA( 0, (LPSTR)IDI_WINLOGO );
907     ret = DialogBoxIndirectParamA((HINSTANCE)GetWindowLongA( hWnd, GWL_HINSTANCE ),
908                                    template, hWnd, AboutDlgProc, (LPARAM)&info );
909     HeapFree( GetProcessHeap(), 0, (LPSTR)info.szApp );
910     HeapFree( GetProcessHeap(), 0, (LPSTR)info.szOtherStuff );
911     return ret;
912 }
913
914 /*************************************************************************
915  * FreeIconList (SHELL32.@)
916  */
917 void WINAPI FreeIconList( DWORD dw )
918 { FIXME("(%lx): stub\n",dw);
919 }
920
921 /***********************************************************************
922  * DllGetVersion [SHELL32.@]
923  *
924  * Retrieves version information of the 'SHELL32.DLL'
925  *
926  * PARAMS
927  *     pdvi [O] pointer to version information structure.
928  *
929  * RETURNS
930  *     Success: S_OK
931  *     Failure: E_INVALIDARG
932  *
933  * NOTES
934  *     Returns version of a shell32.dll from IE4.01 SP1.
935  */
936
937 HRESULT WINAPI SHELL32_DllGetVersion (DLLVERSIONINFO *pdvi)
938 {
939         if (pdvi->cbSize != sizeof(DLLVERSIONINFO))
940         {
941           WARN("wrong DLLVERSIONINFO size from app\n");
942           return E_INVALIDARG;
943         }
944
945         pdvi->dwMajorVersion = 4;
946         pdvi->dwMinorVersion = 72;
947         pdvi->dwBuildNumber = 3110;
948         pdvi->dwPlatformID = 1;
949
950         TRACE("%lu.%lu.%lu.%lu\n",
951            pdvi->dwMajorVersion, pdvi->dwMinorVersion,
952            pdvi->dwBuildNumber, pdvi->dwPlatformID);
953
954         return S_OK;
955 }
956 /*************************************************************************
957  * global variables of the shell32.dll
958  * all are once per process
959  *
960  */
961 void    (WINAPI *pDLLInitComctl)(LPVOID);
962
963 static HINSTANCE        hComctl32;
964
965 HINSTANCE       shell32_hInstance = 0;
966 HIMAGELIST      ShellSmallIconList = 0;
967 HIMAGELIST      ShellBigIconList = 0;
968
969
970 /*************************************************************************
971  * SHELL32 DllMain
972  *
973  * NOTES
974  *  calling oleinitialize here breaks sone apps.
975  */
976
977 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID fImpLoad)
978 {
979         TRACE("%p 0x%lx %p\n", hinstDLL, fdwReason, fImpLoad);
980
981         switch (fdwReason)
982         {
983           case DLL_PROCESS_ATTACH:
984             shell32_hInstance = hinstDLL;
985             hComctl32 = GetModuleHandleA("COMCTL32.DLL");
986             DisableThreadLibraryCalls(shell32_hInstance);
987
988             if (!hComctl32)
989             {
990               ERR("P A N I C SHELL32 loading failed\n");
991               return FALSE;
992             }
993
994             /* comctl32 */
995             pDLLInitComctl=(void*)GetProcAddress(hComctl32,"InitCommonControlsEx");
996             /* initialize the common controls */
997             if (pDLLInitComctl)
998             {
999               pDLLInitComctl(NULL);
1000             }
1001
1002             SIC_Initialize();
1003             SYSTRAY_Init();
1004             InitChangeNotifications();
1005             SHInitRestricted(NULL, NULL);
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 }