[MSCTF] Sync with Wine Staging 3.3. CORE-14434
[reactos.git] / dll / win32 / msctf / inputprocessor.c
index 6770a97..fdeba38 100644 (file)
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  */
 
+#include "config.h"
+
+#include <stdarg.h>
+
+#define COBJMACROS
+
+#include "wine/debug.h"
+#include "windef.h"
+#include "winbase.h"
+#include "winreg.h"
+#include "winuser.h"
+#include "shlwapi.h"
+#include "winerror.h"
+#include "objbase.h"
+#include "olectl.h"
+
+#include "wine/unicode.h"
+
+#include "msctf.h"
 #include "msctf_internal.h"
 
+WINE_DEFAULT_DEBUG_CHANNEL(msctf);
+
 static const WCHAR szwLngp[] = {'L','a','n','g','u','a','g','e','P','r','o','f','i','l','e',0};
 static const WCHAR szwEnable[] = {'E','n','a','b','l','e',0};
 static const WCHAR szwTipfmt[] = {'%','s','\\','%','s',0};
@@ -30,15 +51,6 @@ static const WCHAR szwDefault[] = {'D','e','f','a','u','l','t',0};
 static const WCHAR szwProfile[] = {'P','r','o','f','i','l','e',0};
 static const WCHAR szwDefaultFmt[] = {'%','s','\\','%','s','\\','0','x','%','0','8','x','\\','%','s',0};
 
-typedef struct tagInputProcessorProfilesSink {
-    struct list         entry;
-    union {
-        /* InputProcessorProfile Sinks */
-        IUnknown            *pIUnknown;
-        ITfLanguageProfileNotifySink *pITfLanguageProfileNotifySink;
-    } interfaces;
-} InputProcessorProfilesSink;
-
 typedef struct tagInputProcessorProfiles {
     ITfInputProcessorProfiles ITfInputProcessorProfiles_iface;
     ITfSource ITfSource_iface;
@@ -81,7 +93,7 @@ typedef struct {
 } EnumTfInputProcessorProfiles;
 
 static HRESULT ProfilesEnumGuid_Constructor(IEnumGUID **ppOut);
-static HRESULT EnumTfLanguageProfiles_Constructor(LANGID langid, IEnumTfLanguageProfiles **ppOut);
+static HRESULT EnumTfLanguageProfiles_Constructor(LANGID langid, EnumTfLanguageProfiles **out);
 
 static inline EnumTfInputProcessorProfiles *impl_from_IEnumTfInputProcessorProfiles(IEnumTfInputProcessorProfiles *iface)
 {
@@ -196,25 +208,11 @@ static inline EnumTfLanguageProfiles *impl_from_IEnumTfLanguageProfiles(IEnumTfL
     return CONTAINING_RECORD(iface, EnumTfLanguageProfiles, IEnumTfLanguageProfiles_iface);
 }
 
-static void free_sink(InputProcessorProfilesSink *sink)
-{
-        IUnknown_Release(sink->interfaces.pIUnknown);
-        HeapFree(GetProcessHeap(),0,sink);
-}
-
 static void InputProcessorProfiles_Destructor(InputProcessorProfiles *This)
 {
-    struct list *cursor, *cursor2;
     TRACE("destroying %p\n", This);
 
-    /* free sinks */
-    LIST_FOR_EACH_SAFE(cursor, cursor2, &This->LanguageProfileNotifySink)
-    {
-        InputProcessorProfilesSink* sink = LIST_ENTRY(cursor,InputProcessorProfilesSink,entry);
-        list_remove(cursor);
-        free_sink(sink);
-    }
-
+    free_sinks(&This->LanguageProfileNotifySink);
     HeapFree(GetProcessHeap(),0,This);
 }
 
@@ -328,8 +326,8 @@ static HRESULT WINAPI InputProcessorProfiles_Unregister(
     StringFromGUID2(rclsid, buf, 39);
     sprintfW(fullkey,szwTipfmt,szwSystemTIPKey,buf);
 
-    SHDeleteKeyW(HKEY_LOCAL_MACHINE, fullkey);
-    SHDeleteKeyW(HKEY_CURRENT_USER, fullkey);
+    RegDeleteTreeW(HKEY_LOCAL_MACHINE, fullkey);
+    RegDeleteTreeW(HKEY_CURRENT_USER, fullkey);
 
     return S_OK;
 }
@@ -527,10 +525,9 @@ static HRESULT WINAPI InputProcessorProfiles_ActivateLanguageProfile(
     LanguageProfile.clsid = *rclsid;
     LanguageProfile.langid = langid;
     LanguageProfile.guidProfile = *guidProfiles;
+    LanguageProfile.fActive = TRUE;
 
-    hr = add_active_textservice(&LanguageProfile);
-
-    return hr;
+    return add_active_textservice(&LanguageProfile);
 }
 
 static HRESULT WINAPI InputProcessorProfiles_GetActiveLanguageProfile(
@@ -585,18 +582,18 @@ static HRESULT WINAPI InputProcessorProfiles_ChangeCurrentLanguage(
         ITfInputProcessorProfiles *iface, LANGID langid)
 {
     InputProcessorProfiles *This = impl_from_ITfInputProcessorProfiles(iface);
+    ITfLanguageProfileNotifySink *sink;
     struct list *cursor;
     BOOL accept;
 
     FIXME("STUB:(%p)\n",This);
 
-    LIST_FOR_EACH(cursor, &This->LanguageProfileNotifySink)
+    SINK_FOR_EACH(cursor, &This->LanguageProfileNotifySink, ITfLanguageProfileNotifySink, sink)
     {
-        InputProcessorProfilesSink* sink = LIST_ENTRY(cursor,InputProcessorProfilesSink,entry);
         accept = TRUE;
-        ITfLanguageProfileNotifySink_OnLanguageChange(sink->interfaces.pITfLanguageProfileNotifySink, langid, &accept);
+        ITfLanguageProfileNotifySink_OnLanguageChange(sink, langid, &accept);
         if (!accept)
-            return  E_FAIL;
+            return E_FAIL;
     }
 
     /* TODO:  On successful language change call OnLanguageChanged sink */
@@ -619,8 +616,17 @@ static HRESULT WINAPI InputProcessorProfiles_EnumLanguageProfiles(
         IEnumTfLanguageProfiles **ppEnum)
 {
     InputProcessorProfiles *This = impl_from_ITfInputProcessorProfiles(iface);
+    EnumTfLanguageProfiles *profenum;
+    HRESULT hr;
+
     TRACE("(%p) %x %p\n",This,langid,ppEnum);
-    return EnumTfLanguageProfiles_Constructor(langid, ppEnum);
+
+    if (!ppEnum)
+        return E_INVALIDARG;
+    hr = EnumTfLanguageProfiles_Constructor(langid, &profenum);
+    *ppEnum = &profenum->IEnumTfLanguageProfiles_iface;
+
+    return hr;
 }
 
 static HRESULT WINAPI InputProcessorProfiles_EnableLanguageProfile(
@@ -908,7 +914,6 @@ static HRESULT WINAPI IPPSource_AdviseSink(ITfSource *iface,
         REFIID riid, IUnknown *punk, DWORD *pdwCookie)
 {
     InputProcessorProfiles *This = impl_from_ITfSource(iface);
-    InputProcessorProfilesSink *ipps;
 
     TRACE("(%p) %s %p %p\n",This,debugstr_guid(riid),punk,pdwCookie);
 
@@ -916,47 +921,23 @@ static HRESULT WINAPI IPPSource_AdviseSink(ITfSource *iface,
         return E_INVALIDARG;
 
     if (IsEqualIID(riid, &IID_ITfLanguageProfileNotifySink))
-    {
-        ipps = HeapAlloc(GetProcessHeap(),0,sizeof(InputProcessorProfilesSink));
-        if (!ipps)
-            return E_OUTOFMEMORY;
-        if (FAILED(IUnknown_QueryInterface(punk, riid, (LPVOID *)&ipps->interfaces.pITfLanguageProfileNotifySink)))
-        {
-            HeapFree(GetProcessHeap(),0,ipps);
-            return CONNECT_E_CANNOTCONNECT;
-        }
-        list_add_head(&This->LanguageProfileNotifySink,&ipps->entry);
-        *pdwCookie = generate_Cookie(COOKIE_MAGIC_IPPSINK, ipps);
-    }
-    else
-    {
-        FIXME("(%p) Unhandled Sink: %s\n",This,debugstr_guid(riid));
-        return E_NOTIMPL;
-    }
-
-    TRACE("cookie %x\n",*pdwCookie);
+        return advise_sink(&This->LanguageProfileNotifySink, &IID_ITfLanguageProfileNotifySink,
+                           COOKIE_MAGIC_IPPSINK, punk, pdwCookie);
 
-    return S_OK;
+    FIXME("(%p) Unhandled Sink: %s\n",This,debugstr_guid(riid));
+    return E_NOTIMPL;
 }
 
 static HRESULT WINAPI IPPSource_UnadviseSink(ITfSource *iface, DWORD pdwCookie)
 {
     InputProcessorProfiles *This = impl_from_ITfSource(iface);
-    InputProcessorProfilesSink *sink;
 
     TRACE("(%p) %x\n",This,pdwCookie);
 
     if (get_Cookie_magic(pdwCookie)!=COOKIE_MAGIC_IPPSINK)
         return E_INVALIDARG;
 
-    sink = remove_Cookie(pdwCookie);
-    if (!sink)
-        return CONNECT_E_NOCONNECTION;
-
-    list_remove(&sink->entry);
-    free_sink(sink);
-
-    return S_OK;
+    return unadvise_sink(pdwCookie);
 }
 
 static const ITfSourceVtbl InputProcessorProfilesSourceVtbl =
@@ -1316,16 +1297,16 @@ static HRESULT WINAPI EnumTfLanguageProfiles_Clone( IEnumTfLanguageProfiles *ifa
     IEnumTfLanguageProfiles **ppenum)
 {
     EnumTfLanguageProfiles *This = impl_from_IEnumTfLanguageProfiles(iface);
+    EnumTfLanguageProfiles *new_This;
     HRESULT res;
 
     TRACE("(%p)\n",This);
 
     if (ppenum == NULL) return E_POINTER;
 
-    res = EnumTfLanguageProfiles_Constructor(This->langid, ppenum);
+    res = EnumTfLanguageProfiles_Constructor(This->langid, &new_This);
     if (SUCCEEDED(res))
     {
-        EnumTfLanguageProfiles *new_This = (EnumTfLanguageProfiles *)*ppenum;
         new_This->tip_index = This->tip_index;
         lstrcpynW(new_This->szwCurrentClsid,This->szwCurrentClsid,39);
 
@@ -1338,6 +1319,7 @@ static HRESULT WINAPI EnumTfLanguageProfiles_Clone( IEnumTfLanguageProfiles *ifa
             res = RegOpenKeyExW(new_This->tipkey, fullkey, 0, KEY_READ | KEY_WRITE, &This->langkey);
             new_This->lang_index = This->lang_index;
         }
+        *ppenum = &new_This->IEnumTfLanguageProfiles_iface;
     }
     return res;
 }
@@ -1353,7 +1335,7 @@ static const IEnumTfLanguageProfilesVtbl EnumTfLanguageProfilesVtbl =
     EnumTfLanguageProfiles_Skip
 };
 
-static HRESULT EnumTfLanguageProfiles_Constructor(LANGID langid, IEnumTfLanguageProfiles **ppOut)
+static HRESULT EnumTfLanguageProfiles_Constructor(LANGID langid, EnumTfLanguageProfiles **out)
 {
     HRESULT hr;
     EnumTfLanguageProfiles *This;
@@ -1380,7 +1362,7 @@ static HRESULT EnumTfLanguageProfiles_Constructor(LANGID langid, IEnumTfLanguage
         return E_FAIL;
     }
 
-    *ppOut = &This->IEnumTfLanguageProfiles_iface;
-    TRACE("returning %p\n", *ppOut);
+    *out = This;
+    TRACE("returning %p\n", *out);
     return S_OK;
 }