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