Sync with trunk revision 63128.
[reactos.git] / dll / win32 / msctf / compartmentmgr.c
index e356bb6..17dc2d6 100644 (file)
@@ -30,7 +30,7 @@ typedef struct tagCompartmentValue {
 } CompartmentValue;
 
 typedef struct tagCompartmentMgr {
-    const ITfCompartmentMgrVtbl *CompartmentMgrVtbl;
+    ITfCompartmentMgr ITfCompartmentMgr_iface;
     LONG refCount;
 
     IUnknown *pUnkOuter;
@@ -39,7 +39,7 @@ typedef struct tagCompartmentMgr {
 } CompartmentMgr;
 
 typedef struct tagCompartmentEnumGuid {
-    const IEnumGUIDVtbl *Vtbl;
+    IEnumGUID IEnumGUID_iface;
     LONG refCount;
 
     struct list *values;
@@ -56,8 +56,8 @@ typedef struct tagCompartmentSink {
 } CompartmentSink;
 
 typedef struct tagCompartment {
-    const ITfCompartmentVtbl *Vtbl;
-    const ITfSourceVtbl *SourceVtbl;
+    ITfCompartment ITfCompartment_iface;
+    ITfSource ITfSource_iface;
     LONG refCount;
 
     /* Only VT_I4, VT_UNKNOWN and VT_BSTR data types are allowed */
@@ -69,14 +69,29 @@ typedef struct tagCompartment {
 static HRESULT CompartmentEnumGuid_Constructor(struct list* values, IEnumGUID **ppOut);
 static HRESULT Compartment_Constructor(CompartmentValue *value, ITfCompartment **ppOut);
 
-static inline Compartment *impl_from_ITfSourceVtbl(ITfSource *iface)
+static inline CompartmentMgr *impl_from_ITfCompartmentMgr(ITfCompartmentMgr *iface)
 {
-    return (Compartment *)((char *)iface - FIELD_OFFSET(Compartment,SourceVtbl));
+    return CONTAINING_RECORD(iface, CompartmentMgr, ITfCompartmentMgr_iface);
+}
+
+static inline Compartment *impl_from_ITfCompartment(ITfCompartment *iface)
+{
+    return CONTAINING_RECORD(iface, Compartment, ITfCompartment_iface);
+}
+
+static inline Compartment *impl_from_ITfSource(ITfSource *iface)
+{
+    return CONTAINING_RECORD(iface, Compartment, ITfSource_iface);
+}
+
+static inline CompartmentEnumGuid *impl_from_IEnumGUID(IEnumGUID *iface)
+{
+    return CONTAINING_RECORD(iface, CompartmentEnumGuid, IEnumGUID_iface);
 }
 
 HRESULT CompartmentMgr_Destructor(ITfCompartmentMgr *iface)
 {
-    CompartmentMgr *This = (CompartmentMgr *)iface;
+    CompartmentMgr *This = impl_from_ITfCompartmentMgr(iface);
     struct list *cursor, *cursor2;
 
     LIST_FOR_EACH_SAFE(cursor, cursor2, &This->values)
@@ -96,16 +111,16 @@ HRESULT CompartmentMgr_Destructor(ITfCompartmentMgr *iface)
  *****************************************************/
 static HRESULT WINAPI CompartmentMgr_QueryInterface(ITfCompartmentMgr *iface, REFIID iid, LPVOID *ppvOut)
 {
-    CompartmentMgr *This = (CompartmentMgr *)iface;
+    CompartmentMgr *This = impl_from_ITfCompartmentMgr(iface);
     if (This->pUnkOuter)
-        return IUnknown_QueryInterface(This->pUnkOuter, iid, *ppvOut);
+        return IUnknown_QueryInterface(This->pUnkOuter, iid, ppvOut);
     else
     {
         *ppvOut = NULL;
 
         if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfCompartmentMgr))
         {
-            *ppvOut = This;
+            *ppvOut = &This->ITfCompartmentMgr_iface;
         }
 
         if (*ppvOut)
@@ -121,7 +136,7 @@ static HRESULT WINAPI CompartmentMgr_QueryInterface(ITfCompartmentMgr *iface, RE
 
 static ULONG WINAPI CompartmentMgr_AddRef(ITfCompartmentMgr *iface)
 {
-    CompartmentMgr *This = (CompartmentMgr *)iface;
+    CompartmentMgr *This = impl_from_ITfCompartmentMgr(iface);
     if (This->pUnkOuter)
         return IUnknown_AddRef(This->pUnkOuter);
     else
@@ -130,7 +145,7 @@ static ULONG WINAPI CompartmentMgr_AddRef(ITfCompartmentMgr *iface)
 
 static ULONG WINAPI CompartmentMgr_Release(ITfCompartmentMgr *iface)
 {
-    CompartmentMgr *This = (CompartmentMgr *)iface;
+    CompartmentMgr *This = impl_from_ITfCompartmentMgr(iface);
     if (This->pUnkOuter)
         return IUnknown_Release(This->pUnkOuter);
     else
@@ -147,7 +162,7 @@ static ULONG WINAPI CompartmentMgr_Release(ITfCompartmentMgr *iface)
 static HRESULT WINAPI CompartmentMgr_GetCompartment(ITfCompartmentMgr *iface,
         REFGUID rguid, ITfCompartment **ppcomp)
 {
-    CompartmentMgr *This = (CompartmentMgr *)iface;
+    CompartmentMgr *This = impl_from_ITfCompartmentMgr(iface);
     CompartmentValue* value;
     struct list *cursor;
     HRESULT hr;
@@ -186,8 +201,9 @@ static HRESULT WINAPI CompartmentMgr_GetCompartment(ITfCompartmentMgr *iface,
 static HRESULT WINAPI CompartmentMgr_ClearCompartment(ITfCompartmentMgr *iface,
     TfClientId tid, REFGUID rguid)
 {
+    CompartmentMgr *This = impl_from_ITfCompartmentMgr(iface);
     struct list *cursor;
-    CompartmentMgr *This = (CompartmentMgr *)iface;
+
     TRACE("(%p) %i %s\n",This,tid,debugstr_guid(rguid));
 
     LIST_FOR_EACH(cursor, &This->values)
@@ -210,19 +226,19 @@ static HRESULT WINAPI CompartmentMgr_ClearCompartment(ITfCompartmentMgr *iface,
 static HRESULT WINAPI CompartmentMgr_EnumCompartments(ITfCompartmentMgr *iface,
  IEnumGUID **ppEnum)
 {
-    CompartmentMgr *This = (CompartmentMgr *)iface;
+    CompartmentMgr *This = impl_from_ITfCompartmentMgr(iface);
+
     TRACE("(%p) %p\n",This,ppEnum);
     if (!ppEnum)
         return E_INVALIDARG;
     return CompartmentEnumGuid_Constructor(&This->values, ppEnum);
 }
 
-static const ITfCompartmentMgrVtbl CompartmentMgr_CompartmentMgrVtbl =
+static const ITfCompartmentMgrVtbl CompartmentMgrVtbl =
 {
     CompartmentMgr_QueryInterface,
     CompartmentMgr_AddRef,
     CompartmentMgr_Release,
-
     CompartmentMgr_GetCompartment,
     CompartmentMgr_ClearCompartment,
     CompartmentMgr_EnumCompartments
@@ -242,20 +258,20 @@ HRESULT CompartmentMgr_Constructor(IUnknown *pUnkOuter, REFIID riid, IUnknown **
     if (This == NULL)
         return E_OUTOFMEMORY;
 
-    This->CompartmentMgrVtbl = &CompartmentMgr_CompartmentMgrVtbl;
+    This->ITfCompartmentMgr_iface.lpVtbl = &CompartmentMgrVtbl;
     This->pUnkOuter = pUnkOuter;
     list_init(&This->values);
 
     if (pUnkOuter)
     {
-        TRACE("returning %p\n", This);
-        *ppOut = (IUnknown*)This;
+        *ppOut = (IUnknown*)&This->ITfCompartmentMgr_iface;
+        TRACE("returning %p\n", *ppOut);
         return S_OK;
     }
     else
     {
         HRESULT hr;
-        hr = IUnknown_QueryInterface((IUnknown*)This, riid, (LPVOID*)ppOut);
+        hr = ITfCompartmentMgr_QueryInterface(&This->ITfCompartmentMgr_iface, riid, (void**)ppOut);
         if (FAILED(hr))
             HeapFree(GetProcessHeap(),0,This);
         return hr;
@@ -273,12 +289,12 @@ static void CompartmentEnumGuid_Destructor(CompartmentEnumGuid *This)
 
 static HRESULT WINAPI CompartmentEnumGuid_QueryInterface(IEnumGUID *iface, REFIID iid, LPVOID *ppvOut)
 {
-    CompartmentEnumGuid *This = (CompartmentEnumGuid *)iface;
+    CompartmentEnumGuid *This = impl_from_IEnumGUID(iface);
     *ppvOut = NULL;
 
     if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_IEnumGUID))
     {
-        *ppvOut = This;
+        *ppvOut = &This->IEnumGUID_iface;
     }
 
     if (*ppvOut)
@@ -293,13 +309,13 @@ static HRESULT WINAPI CompartmentEnumGuid_QueryInterface(IEnumGUID *iface, REFII
 
 static ULONG WINAPI CompartmentEnumGuid_AddRef(IEnumGUID *iface)
 {
-    CompartmentEnumGuid *This = (CompartmentEnumGuid*)iface;
+    CompartmentEnumGuid *This = impl_from_IEnumGUID(iface);
     return InterlockedIncrement(&This->refCount);
 }
 
 static ULONG WINAPI CompartmentEnumGuid_Release(IEnumGUID *iface)
 {
-    CompartmentEnumGuid *This = (CompartmentEnumGuid *)iface;
+    CompartmentEnumGuid *This = impl_from_IEnumGUID(iface);
     ULONG ret;
 
     ret = InterlockedDecrement(&This->refCount);
@@ -311,10 +327,10 @@ static ULONG WINAPI CompartmentEnumGuid_Release(IEnumGUID *iface)
 /*****************************************************
  * IEnumGuid functions
  *****************************************************/
-static HRESULT WINAPI CompartmentEnumGuid_Next( LPENUMGUID iface,
+static HRESULT WINAPI CompartmentEnumGuid_Next(IEnumGUID *iface,
     ULONG celt, GUID *rgelt, ULONG *pceltFetched)
 {
-    CompartmentEnumGuid *This = (CompartmentEnumGuid *)iface;
+    CompartmentEnumGuid *This = impl_from_IEnumGUID(iface);
     ULONG fetched = 0;
 
     TRACE("(%p)\n",This);
@@ -338,27 +354,27 @@ static HRESULT WINAPI CompartmentEnumGuid_Next( LPENUMGUID iface,
     return fetched == celt ? S_OK : S_FALSE;
 }
 
-static HRESULT WINAPI CompartmentEnumGuid_Skip( LPENUMGUID iface, ULONG celt)
+static HRESULT WINAPI CompartmentEnumGuid_Skip(IEnumGUID *iface, ULONG celt)
 {
-    CompartmentEnumGuid *This = (CompartmentEnumGuid *)iface;
+    CompartmentEnumGuid *This = impl_from_IEnumGUID(iface);
     TRACE("(%p)\n",This);
 
     This->cursor = list_next(This->values,This->cursor);
     return S_OK;
 }
 
-static HRESULT WINAPI CompartmentEnumGuid_Reset( LPENUMGUID iface)
+static HRESULT WINAPI CompartmentEnumGuid_Reset(IEnumGUID *iface)
 {
-    CompartmentEnumGuid *This = (CompartmentEnumGuid *)iface;
+    CompartmentEnumGuid *This = impl_from_IEnumGUID(iface);
     TRACE("(%p)\n",This);
     This->cursor = list_head(This->values);
     return S_OK;
 }
 
-static HRESULT WINAPI CompartmentEnumGuid_Clone( LPENUMGUID iface,
+static HRESULT WINAPI CompartmentEnumGuid_Clone(IEnumGUID *iface,
     IEnumGUID **ppenum)
 {
-    CompartmentEnumGuid *This = (CompartmentEnumGuid *)iface;
+    CompartmentEnumGuid *This = impl_from_IEnumGUID(iface);
     HRESULT res;
 
     TRACE("(%p)\n",This);
@@ -368,17 +384,17 @@ static HRESULT WINAPI CompartmentEnumGuid_Clone( LPENUMGUID iface,
     res = CompartmentEnumGuid_Constructor(This->values, ppenum);
     if (SUCCEEDED(res))
     {
-        CompartmentEnumGuid *new_This = (CompartmentEnumGuid *)*ppenum;
+        CompartmentEnumGuid *new_This = impl_from_IEnumGUID(*ppenum);
         new_This->cursor = This->cursor;
     }
     return res;
 }
 
-static const IEnumGUIDVtbl IEnumGUID_Vtbl ={
+static const IEnumGUIDVtbl EnumGUIDVtbl =
+{
     CompartmentEnumGuid_QueryInterface,
     CompartmentEnumGuid_AddRef,
     CompartmentEnumGuid_Release,
-
     CompartmentEnumGuid_Next,
     CompartmentEnumGuid_Skip,
     CompartmentEnumGuid_Reset,
@@ -393,14 +409,14 @@ static HRESULT CompartmentEnumGuid_Constructor(struct list *values, IEnumGUID **
     if (This == NULL)
         return E_OUTOFMEMORY;
 
-    This->Vtbl= &IEnumGUID_Vtbl;
+    This->IEnumGUID_iface.lpVtbl= &EnumGUIDVtbl;
     This->refCount = 1;
 
     This->values = values;
     This->cursor = list_head(values);
 
-    TRACE("returning %p\n", This);
-    *ppOut = (IEnumGUID*)This;
+    *ppOut = &This->IEnumGUID_iface;
+    TRACE("returning %p\n", *ppOut);
     return S_OK;
 }
 
@@ -429,16 +445,17 @@ static void Compartment_Destructor(Compartment *This)
 
 static HRESULT WINAPI Compartment_QueryInterface(ITfCompartment *iface, REFIID iid, LPVOID *ppvOut)
 {
-    Compartment *This = (Compartment *)iface;
+    Compartment *This = impl_from_ITfCompartment(iface);
+
     *ppvOut = NULL;
 
     if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfCompartment))
     {
-        *ppvOut = This;
+        *ppvOut = &This->ITfCompartment_iface;
     }
     else if (IsEqualIID(iid, &IID_ITfSource))
     {
-        *ppvOut = &This->SourceVtbl;
+        *ppvOut = &This->ITfSource_iface;
     }
 
     if (*ppvOut)
@@ -453,13 +470,13 @@ static HRESULT WINAPI Compartment_QueryInterface(ITfCompartment *iface, REFIID i
 
 static ULONG WINAPI Compartment_AddRef(ITfCompartment *iface)
 {
-    Compartment *This = (Compartment*)iface;
+    Compartment *This = impl_from_ITfCompartment(iface);
     return InterlockedIncrement(&This->refCount);
 }
 
 static ULONG WINAPI Compartment_Release(ITfCompartment *iface)
 {
-    Compartment *This = (Compartment *)iface;
+    Compartment *This = impl_from_ITfCompartment(iface);
     ULONG ret;
 
     ret = InterlockedDecrement(&This->refCount);
@@ -471,7 +488,7 @@ static ULONG WINAPI Compartment_Release(ITfCompartment *iface)
 static HRESULT WINAPI Compartment_SetValue(ITfCompartment *iface,
     TfClientId tid, const VARIANT *pvarValue)
 {
-    Compartment *This = (Compartment *)iface;
+    Compartment *This = impl_from_ITfCompartment(iface);
     struct list *cursor;
 
     TRACE("(%p) %i %p\n",This,tid,pvarValue);
@@ -509,7 +526,7 @@ static HRESULT WINAPI Compartment_SetValue(ITfCompartment *iface,
 static HRESULT WINAPI Compartment_GetValue(ITfCompartment *iface,
     VARIANT *pvarValue)
 {
-    Compartment *This = (Compartment *)iface;
+    Compartment *This = impl_from_ITfCompartment(iface);
     TRACE("(%p) %p\n",This, pvarValue);
 
     if (!pvarValue)
@@ -520,11 +537,11 @@ static HRESULT WINAPI Compartment_GetValue(ITfCompartment *iface,
     return VariantCopy(pvarValue,&This->variant);
 }
 
-static const ITfCompartmentVtbl ITfCompartment_Vtbl ={
+static const ITfCompartmentVtbl CompartmentVtbl =
+{
     Compartment_QueryInterface,
     Compartment_AddRef,
     Compartment_Release,
-
     Compartment_SetValue,
     Compartment_GetValue
 };
@@ -533,29 +550,29 @@ static const ITfCompartmentVtbl ITfCompartment_Vtbl ={
  * ITfSource functions
  *****************************************************/
 
-static HRESULT WINAPI Source_QueryInterface(ITfSource *iface, REFIID iid, LPVOID *ppvOut)
+static HRESULT WINAPI CompartmentSource_QueryInterface(ITfSource *iface, REFIID iid, LPVOID *ppvOut)
 {
-    Compartment *This = impl_from_ITfSourceVtbl(iface);
-    return Compartment_QueryInterface((ITfCompartment *)This, iid, *ppvOut);
+    Compartment *This = impl_from_ITfSource(iface);
+    return ITfCompartment_QueryInterface(&This->ITfCompartment_iface, iid, ppvOut);
 }
 
-static ULONG WINAPI Source_AddRef(ITfSource *iface)
+static ULONG WINAPI CompartmentSource_AddRef(ITfSource *iface)
 {
-    Compartment *This = impl_from_ITfSourceVtbl(iface);
-    return Compartment_AddRef((ITfCompartment*)This);
+    Compartment *This = impl_from_ITfSource(iface);
+    return ITfCompartment_AddRef(&This->ITfCompartment_iface);
 }
 
-static ULONG WINAPI Source_Release(ITfSource *iface)
+static ULONG WINAPI CompartmentSource_Release(ITfSource *iface)
 {
-    Compartment *This = impl_from_ITfSourceVtbl(iface);
-    return Compartment_Release((ITfCompartment *)This);
+    Compartment *This = impl_from_ITfSource(iface);
+    return ITfCompartment_Release(&This->ITfCompartment_iface);
 }
 
 static HRESULT WINAPI CompartmentSource_AdviseSink(ITfSource *iface,
         REFIID riid, IUnknown *punk, DWORD *pdwCookie)
 {
+    Compartment *This = impl_from_ITfSource(iface);
     CompartmentSink *cs;
-    Compartment *This = impl_from_ITfSourceVtbl(iface);
 
     TRACE("(%p) %s %p %p\n",This,debugstr_guid(riid),punk,pdwCookie);
 
@@ -588,8 +605,8 @@ static HRESULT WINAPI CompartmentSource_AdviseSink(ITfSource *iface,
 
 static HRESULT WINAPI CompartmentSource_UnadviseSink(ITfSource *iface, DWORD pdwCookie)
 {
+    Compartment *This = impl_from_ITfSource(iface);
     CompartmentSink *sink;
-    Compartment *This = impl_from_ITfSourceVtbl(iface);
 
     TRACE("(%p) %x\n",This,pdwCookie);
 
@@ -606,12 +623,11 @@ static HRESULT WINAPI CompartmentSource_UnadviseSink(ITfSource *iface, DWORD pdw
     return S_OK;
 }
 
-static const ITfSourceVtbl Compartment_SourceVtbl =
+static const ITfSourceVtbl CompartmentSourceVtbl =
 {
-    Source_QueryInterface,
-    Source_AddRef,
-    Source_Release,
-
+    CompartmentSource_QueryInterface,
+    CompartmentSource_AddRef,
+    CompartmentSource_Release,
     CompartmentSource_AdviseSink,
     CompartmentSource_UnadviseSink,
 };
@@ -624,8 +640,8 @@ static HRESULT Compartment_Constructor(CompartmentValue *valueData, ITfCompartme
     if (This == NULL)
         return E_OUTOFMEMORY;
 
-    This->Vtbl= &ITfCompartment_Vtbl;
-    This->SourceVtbl = &Compartment_SourceVtbl;
+    This->ITfCompartment_iface.lpVtbl= &CompartmentVtbl;
+    This->ITfSource_iface.lpVtbl = &CompartmentSourceVtbl;
     This->refCount = 1;
 
     This->valueData = valueData;
@@ -633,7 +649,7 @@ static HRESULT Compartment_Constructor(CompartmentValue *valueData, ITfCompartme
 
     list_init(&This->CompartmentEventSink);
 
-    TRACE("returning %p\n", This);
-    *ppOut = (ITfCompartment*)This;
+    *ppOut = &This->ITfCompartment_iface;
+    TRACE("returning %p\n", *ppOut);
     return S_OK;
 }