Added a framework for testing CreateProcess and a few tests.
[wine] / dlls / oleaut32 / propertyframe.c
1 /*
2  * Copyright 2001 TAKESHIMA Hidenori <hidenori@a2.ctktv.ne.jp>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  * FIXME - use PropertySheetW.
19  * FIXME - not tested.
20  */
21
22 #include "config.h"
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wingdi.h"
27 #include "winuser.h"
28 #include "wine/debug.h"
29 #include "ole2.h"
30 #include "olectl.h"
31 #include "oleauto.h"
32 #include "commctrl.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(ole);
35
36 typedef struct CPropertyPageContainerImpl CPropertyPageContainerImpl;
37
38 static const struct
39 {
40         DLGTEMPLATE     templ;
41         WORD    wMenuName;
42         WORD    wClassName;
43         WCHAR   wDummyCaption;
44         BYTE    padding[4];
45 } propsite_dlg =
46 {
47         {
48         WS_VISIBLE | WS_CHILD, /* style */
49         0, /* dwExtendedStyle */
50         0, /* cdit */
51         0, /* x */
52         0, /* y */
53         0, /* cx */
54         0, /* cy */
55         },
56         0, 0, 0,
57 };
58
59 typedef struct CPropertyPageSiteImpl
60 {
61         ICOM_VFIELD(IPropertyPageSite);
62         /* IUnknown fields */
63         ULONG   ref;
64
65         /* IPropertyPageSite fields */
66         CPropertyPageContainerImpl*     pContainer;
67         IPropertyPage*  pPage;
68         HWND    hwnd;
69         BYTE    templ[sizeof(propsite_dlg)];
70         PROPPAGEINFO    info;
71         BOOL    bActivate;
72 } CPropertyPageSiteImpl;
73
74 struct CPropertyPageContainerImpl
75 {
76         ULONG   ref; /* for IUnknown(not used now) */
77         LCID    lcid;
78         DWORD   m_cSites;
79         CPropertyPageSiteImpl** m_ppSites;
80         PROPSHEETPAGEA* m_pPsp;
81         HRESULT m_hr;
82 };
83
84 /* for future use. */
85 #define CPropertyPageContainerImpl_AddRef(pContainer)   (++((pContainer)->ref))
86 #define CPropertyPageContainerImpl_Release(pContainer)  (--((pContainer)->ref))
87
88
89 /***************************************************************************/
90
91 #define PropSiteDlg_Return(a)   do{SetWindowLongA(hwnd,DWL_MSGRESULT,(LONG)a);return TRUE;}while(1)
92 static BOOL CALLBACK PropSiteDlgProc(
93         HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
94 {
95         CPropertyPageSiteImpl*  This = (CPropertyPageSiteImpl*)GetWindowLongA( hwnd, DWL_USER );
96         HRESULT hr;
97         RECT    rc;
98         NMHDR*  pnmh;
99
100         switch ( msg )
101         {
102         case WM_INITDIALOG:
103                 This = (CPropertyPageSiteImpl*)(((PROPSHEETPAGEA*)lParam)->lParam);
104                 SetWindowLongA( hwnd, DWL_USER, (LONG)This );
105                 TRACE("WM_INITDIALOG (%p) hwnd = %08x\n", This, hwnd );
106
107                 This->hwnd = hwnd;
108                 ZeroMemory( &rc, sizeof(rc) );
109                 GetClientRect( hwnd, &rc );
110                 hr = IPropertyPage_Activate(This->pPage,hwnd,&rc,TRUE);
111                 if ( SUCCEEDED(hr) )
112                 {
113                         This->bActivate = TRUE;
114                         hr = IPropertyPage_Show(This->pPage,SW_SHOW);
115                 }
116                 if ( FAILED(hr) )
117                         This->pContainer->m_hr = hr;
118                 break;
119         case WM_DESTROY:
120                 TRACE("WM_DESTROY (%p)\n",This);
121                 if ( This != NULL )
122                 {
123                         if ( This->bActivate )
124                         {
125                                 IPropertyPage_Show(This->pPage,SW_HIDE);
126                                 IPropertyPage_Deactivate(This->pPage);
127                                 This->bActivate = FALSE;
128                         }
129                         This->hwnd = (HWND)NULL;
130                 }
131                 SetWindowLongA( hwnd, DWL_USER, (LONG)0 );
132                 break;
133         case WM_NOTIFY:
134                 pnmh = (NMHDR*)lParam;
135                 switch ( pnmh->code )
136                 {
137                 case PSN_APPLY:
138                         TRACE("PSN_APPLY (%p)\n",This);
139                         hr = IPropertyPage_Apply(This->pPage);
140                         if ( FAILED(hr) )
141                                 PropSiteDlg_Return(PSNRET_INVALID_NOCHANGEPAGE);
142                         PropSiteDlg_Return(PSNRET_NOERROR);
143                 case PSN_QUERYCANCEL:
144                         FIXME("PSN_QUERYCANCEL (%p)\n",This);
145                         PropSiteDlg_Return(FALSE);
146                 case PSN_RESET:
147                         FIXME("PSN_RESET (%p)\n",This);
148                         PropSiteDlg_Return(0);
149                 case PSN_SETACTIVE:
150                         TRACE("PSN_SETACTIVE (%p)\n",This);
151                         PropSiteDlg_Return(0);
152                 case PSN_KILLACTIVE:
153                         TRACE("PSN_KILLACTIVE (%p)\n",This);
154                         PropSiteDlg_Return(FALSE);
155                 }
156                 break;
157         }
158
159         return FALSE;
160 }
161
162 /***************************************************************************/
163
164 static HRESULT WINAPI
165 CPropertyPageSiteImpl_fnQueryInterface(IPropertyPageSite* iface,REFIID riid,LPVOID *ppobj)
166 {
167         ICOM_THIS(CPropertyPageSiteImpl,iface);
168
169         TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
170
171         if ( ppobj == NULL )
172                 return E_POINTER;
173         *ppobj = NULL;
174
175         if ( IsEqualGUID(riid,&IID_IUnknown) ||
176                  IsEqualGUID(riid,&IID_IPropertyPageSite) )
177         {
178                 *ppobj = (LPVOID)This;
179                 IUnknown_AddRef((IUnknown*)(*ppobj));
180                 return S_OK;
181         }
182
183         return E_NOINTERFACE;
184 }
185
186 static ULONG WINAPI
187 CPropertyPageSiteImpl_fnAddRef(IPropertyPageSite* iface)
188 {
189         ICOM_THIS(CPropertyPageSiteImpl,iface);
190
191         TRACE("(%p)->()\n",This);
192
193         return InterlockedExchangeAdd(&(This->ref),1) + 1;
194 }
195
196 static ULONG WINAPI
197 CPropertyPageSiteImpl_fnRelease(IPropertyPageSite* iface)
198 {
199         ICOM_THIS(CPropertyPageSiteImpl,iface);
200         LONG    ref;
201
202         TRACE("(%p)->()\n",This);
203         ref = InterlockedExchangeAdd(&(This->ref),-1) - 1;
204         if ( ref > 0 )
205                 return (ULONG)ref;
206
207         if ( This->pContainer != NULL )
208                 CPropertyPageContainerImpl_Release(This->pContainer);
209         if ( This->pPage != NULL )
210                 IPropertyPage_Release(This->pPage);
211         if ( This->info.pszTitle != NULL )
212                 CoTaskMemFree( This->info.pszTitle );
213         if ( This->info.pszDocString != NULL )
214                 CoTaskMemFree( This->info.pszDocString );
215         if ( This->info.pszHelpFile != NULL )
216                 CoTaskMemFree( This->info.pszHelpFile );
217
218         HeapFree(GetProcessHeap(),0,This);
219
220         return 0;
221 }
222
223 static HRESULT WINAPI
224 CPropertyPageSiteImpl_fnOnStatusChange(IPropertyPageSite* iface,DWORD dwFlags)
225 {
226         ICOM_THIS(CPropertyPageSiteImpl,iface);
227
228         TRACE("(%p,%08lx)\n",This,dwFlags);
229
230         if ( This->hwnd == (HWND)NULL )
231                 return E_UNEXPECTED;
232
233         switch ( dwFlags )
234         {
235         case PROPPAGESTATUS_DIRTY:
236                 /* dirty */
237                 SendMessageA(GetParent(This->hwnd),PSM_CHANGED,(WPARAM)(This->hwnd),0);
238                 break;
239         case PROPPAGESTATUS_VALIDATE:
240                 /* validate */
241                 SendMessageA(GetParent(This->hwnd),PSM_UNCHANGED,(WPARAM)(This->hwnd),0);
242                 break;
243         default:
244                 FIXME("(%p,%08lx) unknown flags\n",This,dwFlags);
245                 return E_INVALIDARG;
246         }
247
248         return NOERROR;
249 }
250
251 static HRESULT WINAPI
252 CPropertyPageSiteImpl_fnGetLocaleID(IPropertyPageSite* iface,LCID* pLocaleID)
253 {
254         ICOM_THIS(CPropertyPageSiteImpl,iface);
255
256         TRACE("(%p,%p)\n",This,pLocaleID);
257
258         if ( pLocaleID == NULL )
259                 return E_POINTER;
260
261         *pLocaleID = This->pContainer->lcid;
262
263         return NOERROR;
264 }
265
266 static HRESULT WINAPI
267 CPropertyPageSiteImpl_fnGetPageContainer(IPropertyPageSite* iface,IUnknown** ppUnk)
268 {
269         ICOM_THIS(CPropertyPageSiteImpl,iface);
270
271         FIXME("(%p,%p) - Win95 returns E_NOTIMPL\n",This,ppUnk);
272
273         if ( ppUnk == NULL )
274                 return E_POINTER;
275
276         *ppUnk = NULL;
277
278         return E_NOTIMPL;
279 }
280
281 static HRESULT WINAPI
282 CPropertyPageSiteImpl_fnTranslateAccelerator(IPropertyPageSite* iface,MSG* pMsg)
283 {
284         ICOM_THIS(CPropertyPageSiteImpl,iface);
285
286         FIXME("(%p,%p) - Win95 returns E_NOTIMPL\n",This,pMsg);
287
288         if ( pMsg == NULL )
289                 return E_POINTER;
290
291         return E_NOTIMPL;
292 }
293
294 static ICOM_VTABLE(IPropertyPageSite) iproppagesite =
295 {
296         ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
297         /* IUnknown fields */
298         CPropertyPageSiteImpl_fnQueryInterface,
299         CPropertyPageSiteImpl_fnAddRef,
300         CPropertyPageSiteImpl_fnRelease,
301         /* IPropertyPageSite fields */
302         CPropertyPageSiteImpl_fnOnStatusChange,
303         CPropertyPageSiteImpl_fnGetLocaleID,
304         CPropertyPageSiteImpl_fnGetPageContainer,
305         CPropertyPageSiteImpl_fnTranslateAccelerator,
306 };
307
308 /***************************************************************************/
309
310 static
311 HRESULT OLEPRO32_CreatePropertyPageSite(
312         CPropertyPageContainerImpl* pContainer,
313         IPropertyPage* pPage,
314         CPropertyPageSiteImpl** ppSite,
315         PROPSHEETPAGEA* pPspReturned )
316 {
317         CPropertyPageSiteImpl*  This = NULL;
318         HRESULT hr;
319         DLGTEMPLATE*    ptempl;
320
321         *ppSite = NULL;
322         ZeroMemory( pPspReturned, sizeof(PROPSHEETPAGEA) );
323
324         This = (CPropertyPageSiteImpl*)HeapAlloc( GetProcessHeap(), 0,
325                 sizeof(CPropertyPageSiteImpl) );
326         if ( This == NULL )
327                 return E_OUTOFMEMORY;
328         ZeroMemory( This, sizeof(CPropertyPageSiteImpl) );
329
330         ICOM_VTBL(This) = &iproppagesite;
331         This->ref = 1;
332         This->pContainer = pContainer; CPropertyPageContainerImpl_AddRef(pContainer);
333         This->pPage = pPage; IPropertyPage_AddRef(pPage);
334         This->hwnd = (HWND)NULL;
335         memcpy( &This->templ, &propsite_dlg, sizeof(propsite_dlg) );
336         This->info.cb = sizeof(PROPPAGEINFO);
337         This->bActivate = FALSE;
338         ptempl = (DLGTEMPLATE*)&This->templ;
339
340         /* construct */
341         hr = IPropertyPage_SetPageSite(pPage,(IPropertyPageSite*)This);
342         if ( FAILED(hr) )
343                 goto end;
344
345         hr = IPropertyPage_GetPageInfo(pPage,&This->info);
346         if ( FAILED(hr) )
347                 goto end;
348
349         ptempl->cx = This->info.size.cx;
350         ptempl->cy = This->info.size.cy;
351
352         pPspReturned->dwSize = sizeof(PROPSHEETPAGEA);
353         pPspReturned->dwFlags = PSP_DLGINDIRECT;
354         pPspReturned->u.pResource = ptempl;
355         if ( This->info.pszTitle != NULL );
356         {
357                 pPspReturned->dwFlags |= PSP_USETITLE;
358                 pPspReturned->pszTitle = "Title";/*FIXME - This->info.pszTitle;*/
359         }
360         pPspReturned->pfnDlgProc = PropSiteDlgProc;
361         pPspReturned->lParam = (LONG)This;
362
363 end:
364         if ( FAILED(hr) )
365         {
366                 IUnknown_Release((IUnknown*)This);
367                 return hr;
368         }
369
370         *ppSite = This;
371         return NOERROR;
372 }
373
374 /***************************************************************************/
375
376 static
377 void OLEPRO32_DestroyPropertyPageContainer(
378         CPropertyPageContainerImpl* This )
379 {
380         DWORD   nIndex;
381
382         if ( This->m_ppSites != NULL )
383         {
384                 for ( nIndex = 0; nIndex < This->m_cSites; nIndex++ )
385                 {
386                         if ( This->m_ppSites[nIndex] != NULL )
387                         {
388                                 IPropertyPage_SetPageSite(This->m_ppSites[nIndex]->pPage,NULL);
389                                 IUnknown_Release((IUnknown*)This->m_ppSites[nIndex]);
390                         }
391                 }
392                 HeapFree( GetProcessHeap(), 0, This->m_ppSites );
393                 This->m_ppSites = NULL;
394         }
395         if ( This->m_pPsp != NULL )
396         {
397                 HeapFree( GetProcessHeap(), 0, This->m_pPsp );
398                 This->m_pPsp = NULL;
399         }
400         HeapFree( GetProcessHeap(), 0, This );
401 }
402
403 static
404 HRESULT OLEPRO32_CreatePropertyPageContainer(
405         CPropertyPageContainerImpl** ppContainer,
406         ULONG cPages, const CLSID* pclsidPages,
407         LCID lcid )
408 {
409         CPropertyPageContainerImpl*     This = NULL;
410         DWORD   nIndex;
411         IPropertyPage*  pPage;
412         HRESULT hr;
413
414         This = (CPropertyPageContainerImpl*)HeapAlloc( GetProcessHeap(), 0,
415                 sizeof(CPropertyPageContainerImpl) );
416         if ( This == NULL )
417                 return E_OUTOFMEMORY;
418         ZeroMemory( This, sizeof(CPropertyPageContainerImpl) );
419         This->ref = 1;
420         This->lcid = lcid;
421         This->m_cSites = cPages;
422         This->m_ppSites = NULL;
423         This->m_pPsp = NULL;
424         This->m_hr = S_OK;
425
426         This->m_ppSites = (CPropertyPageSiteImpl**)HeapAlloc( GetProcessHeap(), 0, sizeof(CPropertyPageSiteImpl*) * cPages );
427         if ( This->m_ppSites == NULL )
428         {
429                 hr = E_OUTOFMEMORY;
430                 goto end;
431         }
432         ZeroMemory( This->m_ppSites, sizeof(CPropertyPageSiteImpl*) * cPages );
433
434         This->m_pPsp = (PROPSHEETPAGEA*)HeapAlloc( GetProcessHeap(), 0, sizeof(PROPSHEETPAGEA) * cPages );
435         if ( This->m_pPsp == NULL )
436         {
437                 hr = E_OUTOFMEMORY;
438                 goto end;
439         }
440         ZeroMemory( This->m_pPsp, sizeof(PROPSHEETPAGEA) * cPages );
441
442         for ( nIndex = 0; nIndex < cPages; nIndex ++ )
443         {
444                 hr = CoCreateInstance(
445                         &pclsidPages[nIndex], NULL, CLSCTX_SERVER,
446                         &IID_IPropertyPage, (void**)&pPage );
447                 if ( FAILED(hr) )
448                         goto end;
449                 hr = OLEPRO32_CreatePropertyPageSite(
450                         This, pPage, &This->m_ppSites[nIndex], &This->m_pPsp[nIndex] );
451                 IPropertyPage_Release(pPage);
452                 if ( FAILED(hr) )
453                         goto end;
454         }
455
456         hr = NOERROR;
457 end:
458         if ( FAILED(hr) )
459         {
460                 OLEPRO32_DestroyPropertyPageContainer( This );
461                 return hr;
462         }
463
464         *ppContainer = This;
465         return NOERROR;
466 }
467
468 static
469 HRESULT OLEPRO32_SetObjectsToPropertyPages(
470         CPropertyPageContainerImpl* This,
471         ULONG cObjects, IUnknown** ppunk )
472 {
473         DWORD   nIndex;
474         HRESULT hr;
475
476         for ( nIndex = 0; nIndex < This->m_cSites; nIndex ++ )
477         {
478                 if ( This->m_ppSites[nIndex] == NULL )
479                         return E_UNEXPECTED;
480                 hr = IPropertyPage_SetObjects(This->m_ppSites[nIndex]->pPage,cObjects,ppunk);
481                 if ( FAILED(hr) )
482                         return hr;
483         }
484
485         return NOERROR;
486 }
487
488
489 /***********************************************************************
490  *
491  * OleCreatePropertyFrameIndirect (OLEAUT32.416)(OLEPRO32.249)
492  *
493  */
494
495 HRESULT WINAPI OleCreatePropertyFrameIndirect( LPOCPFIPARAMS lpParams )
496 {
497         CPropertyPageContainerImpl*     pContainer = NULL;
498         HRESULT hr;
499         PROPSHEETHEADERA        psh;
500         int ret;
501
502         TRACE("(%p)\n",lpParams);
503
504         if ( lpParams == NULL )
505                 return E_POINTER;
506         if ( lpParams->cbStructSize != sizeof(OCPFIPARAMS) )
507         {
508                 FIXME("lpParams->cbStructSize(%lu) != sizeof(OCPFIPARAMS)(%lu)\n",lpParams->cbStructSize,(unsigned long)sizeof(OCPFIPARAMS));
509                 return E_INVALIDARG;
510         }
511
512         hr = OLEPRO32_CreatePropertyPageContainer(
513                 &pContainer,
514                 lpParams->cPages, lpParams->lpPages,
515                 lpParams->lcid );
516         if ( FAILED(hr) )
517         {
518                 TRACE( "OLEPRO32_CreatePropertyPageContainer returns %08lx\n",hr);
519                 return hr;
520         }
521
522         hr = OLEPRO32_SetObjectsToPropertyPages(
523                 pContainer,
524                 lpParams->cObjects, lpParams->lplpUnk );
525         if ( FAILED(hr) )
526         {
527                 TRACE("OLEPRO32_SetObjectsToPropertyPages returns %08lx\n",hr);
528                 OLEPRO32_DestroyPropertyPageContainer(pContainer);
529                 return hr;
530         }
531
532         /* FIXME - use lpParams.x / lpParams.y */
533
534         ZeroMemory( &psh, sizeof(psh) );
535         psh.dwSize = sizeof(PROPSHEETHEADERA);
536         psh.dwFlags = PSH_PROPSHEETPAGE;
537         psh.hwndParent = lpParams->hWndOwner;
538         psh.pszCaption = "Caption"; /* FIXME - lpParams->lpszCaption; */
539         psh.nPages = pContainer->m_cSites;
540         psh.u2.nStartPage = lpParams->dispidInitialProperty;
541         psh.u3.ppsp = pContainer->m_pPsp;
542
543         ret = PropertySheetA( &psh );
544         OLEPRO32_DestroyPropertyPageContainer(pContainer);
545
546         if ( ret == -1 )
547                 return E_FAIL;
548
549         return S_OK;
550 }
551
552
553 /***********************************************************************
554  *
555  * OleCreatePropertyFrame (OLEAUT32.417)(OLEPRO32.250)
556  *
557  */
558
559 HRESULT WINAPI OleCreatePropertyFrame(
560     HWND hwndOwner, UINT x, UINT y, LPCOLESTR lpszCaption,ULONG cObjects,
561     LPUNKNOWN* lplpUnk, ULONG cPages, LPCLSID pPageClsID, LCID lcid, 
562     DWORD dwReserved, LPVOID pvReserved )
563 {
564         OCPFIPARAMS     params;
565
566         TRACE("(%x,%d,%d,%s,%ld,%p,%ld,%p,%x,%ld,%p)\n",
567                 hwndOwner,x,y,debugstr_w(lpszCaption),cObjects,lplpUnk,cPages,
568                 pPageClsID, (int)lcid,dwReserved,pvReserved);
569
570         if ( dwReserved != 0 || pvReserved != NULL )
571         {
572                 FIXME("(%x,%d,%d,%s,%ld,%p,%ld,%p,%x,%ld,%p) - E_INVALIDARG\n",
573                 hwndOwner,x,y,debugstr_w(lpszCaption),cObjects,lplpUnk,cPages,
574                 pPageClsID, (int)lcid,dwReserved,pvReserved);
575                 return E_INVALIDARG;
576         }
577
578         ZeroMemory( &params, sizeof(params) );
579         params.cbStructSize = sizeof(OCPFIPARAMS);
580         params.hWndOwner = hwndOwner;
581         params.x = x;
582         params.y = y;
583         params.lpszCaption = lpszCaption;
584         params.cObjects = cObjects;
585         params.lplpUnk = lplpUnk;
586         params.cPages = cPages;
587         params.lpPages = pPageClsID;
588         params.lcid = lcid;
589         params.dispidInitialProperty = 0;
590
591         return OleCreatePropertyFrameIndirect( &params );
592 }
593