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