]> git.saurik.com Git - wxWidgets.git/blobdiff - src/msw/dde.cpp
Use the current font for the DoGetBestSize calculation
[wxWidgets.git] / src / msw / dde.cpp
index cb980679492350f6a9f4de034efbaeff1fbed387..26a13fc678e989e9e765d1bda136dc2fcdda16cb 100644 (file)
@@ -17,7 +17,7 @@
 // headers
 // ----------------------------------------------------------------------------
 
 // headers
 // ----------------------------------------------------------------------------
 
-#ifdef __GNUG__
+#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
     #pragma implementation "dde.h"
 #endif
 
     #pragma implementation "dde.h"
 #endif
 
 #include "wx/msw/private.h"
 
 #include <string.h>
 #include "wx/msw/private.h"
 
 #include <string.h>
-#include <windows.h>
 #include <ddeml.h>
 
 #ifdef __GNUWIN32_OLD__
     #include "wx/msw/gnuwin32/extra.h"
 #endif
 
 #include <ddeml.h>
 
 #ifdef __GNUWIN32_OLD__
     #include "wx/msw/gnuwin32/extra.h"
 #endif
 
-// some compilers headers don't define this one (mingw32)
-#ifndef DMLERR_NO_ERROR
-    #define DMLERR_NO_ERROR (0)
-
-    // this one is also missing from some mingw32 headers, but there is no way
-    // to test for it (I know of) - the test for DMLERR_NO_ERROR works for me,
-    // but is surely not the right thing to do
-    extern "C"
-    HDDEDATA STDCALL DdeClientTransaction(LPBYTE pData,
-                                          DWORD cbData,
-                                          HCONV hConv,
-                                          HSZ hszItem,
-                                          UINT wFmt,
-                                          UINT wType,
-                                          DWORD dwTimeout,
-                                          LPDWORD pdwResult);
-#endif // no DMLERR_NO_ERROR
-
 // ----------------------------------------------------------------------------
 // macros and constants
 // ----------------------------------------------------------------------------
 // ----------------------------------------------------------------------------
 // macros and constants
 // ----------------------------------------------------------------------------
@@ -113,6 +94,7 @@ static HSZ DDEGetAtom(const wxString& string);
 // string handles
 static HSZ DDEAtomFromString(const wxString& s);
 static wxString DDEStringFromAtom(HSZ hsz);
 // string handles
 static HSZ DDEAtomFromString(const wxString& s);
 static wxString DDEStringFromAtom(HSZ hsz);
+static void DDEFreeString(HSZ hsz);
 
 // error handling
 static wxString DDEGetErrorMsg(UINT error);
 
 // error handling
 static wxString DDEGetErrorMsg(UINT error);
@@ -195,8 +177,10 @@ extern void wxDDEInitialize()
 
 void wxDDECleanUp()
 {
 
 void wxDDECleanUp()
 {
-    WX_CLEAR_LIST(wxDDEClientList, wxDDEClientObjects);
-    WX_CLEAR_LIST(wxDDEServerList, wxDDEServerObjects);
+    // deleting them later won't work as DDE won't be initialized any more
+    wxASSERT_MSG( wxDDEServerObjects.empty() &&
+                    wxDDEClientObjects.empty(),
+                    _T("all DDE objects should be deleted by now") );
 
     wxAtomTable.clear();
 
 
     wxAtomTable.clear();
 
@@ -300,26 +284,45 @@ bool wxDDEServer::Create(const wxString& server)
 {
     m_serviceName = server;
 
 {
     m_serviceName = server;
 
-    if ( !DdeNameService(DDEIdInst, DDEAtomFromString(server), (HSZ)NULL, DNS_REGISTER) )
-    {
-        DDELogError(wxString::Format(_("Failed to register DDE server '%s'"),
-                                     server.c_str()));
+    HSZ hsz = DDEAtomFromString(server);
 
 
+    if ( !hsz )
+    {
         return false;
     }
 
         return false;
     }
 
-    return true;
+
+    bool success = (DdeNameService(DDEIdInst, hsz, (HSZ) NULL, DNS_REGISTER)
+        != NULL);
+
+    if (!success)
+    {
+        DDELogError(wxString::Format(_("Failed to register DDE server '%s'"),
+            server.c_str()));
+    }
+
+    DDEFreeString(hsz);
+
+    return success;
 }
 
 wxDDEServer::~wxDDEServer()
 {
 }
 
 wxDDEServer::~wxDDEServer()
 {
-    if ( !!m_serviceName )
+    if ( !m_serviceName.IsEmpty() )
     {
     {
-        if ( !DdeNameService(DDEIdInst, DDEAtomFromString(m_serviceName),
-                             (HSZ)NULL, DNS_UNREGISTER) )
+        HSZ hsz = DDEAtomFromString(m_serviceName);
+
+        if (hsz)
         {
         {
-            DDELogError(wxString::Format(_("Failed to unregister DDE server '%s'"),
-                                         m_serviceName.c_str()));
+            if ( !DdeNameService(DDEIdInst, hsz,
+                (HSZ) NULL, DNS_UNREGISTER) )
+            {
+                DDELogError(wxString::Format(
+                    _("Failed to unregister DDE server '%s'"),
+                    m_serviceName.c_str()));
+            }
+
+            DDEFreeString(hsz);
         }
     }
 
         }
     }
 
@@ -417,12 +420,35 @@ wxConnectionBase *wxDDEClient::MakeConnection(const wxString& WXUNUSED(host),
                                               const wxString& server,
                                               const wxString& topic)
 {
                                               const wxString& server,
                                               const wxString& topic)
 {
-    HCONV hConv = DdeConnect(DDEIdInst, DDEAtomFromString(server), DDEAtomFromString(topic),
-                             (PCONVCONTEXT)NULL);
+    HSZ hszServer = DDEAtomFromString(server);
+
+    if ( !hszServer )
+    {
+        return (wxConnectionBase*) NULL;
+    }
+
+
+    HSZ hszTopic = DDEAtomFromString(topic);
+
+    if ( !hszTopic )
+    {
+        DDEFreeString(hszServer);
+        return (wxConnectionBase*) NULL;
+    }
+
+
+    HCONV hConv = ::DdeConnect(DDEIdInst, hszServer, hszTopic,
+        (PCONVCONTEXT) NULL);
+
+    DDEFreeString(hszServer);
+    DDEFreeString(hszTopic);
+
+
     if ( !hConv )
     {
     if ( !hConv )
     {
-        DDELogError(wxString::Format(_("Failed to create connection to server '%s' on topic '%s'"),
-                                     server.c_str(), topic.c_str()));
+        DDELogError( wxString::Format(
+            _("Failed to create connection to server '%s' on topic '%s'"),
+            server.c_str(), topic.c_str()) );
     }
     else
     {
     }
     else
     {
@@ -535,7 +561,8 @@ bool wxDDEConnection::Execute(const wxChar *data, int size, wxIPCFormat format)
         size = wxStrlen(data) + 1;
     }
 
         size = wxStrlen(data) + 1;
     }
 
-    bool ok = DdeClientTransaction((LPBYTE)data, size,
+    bool ok = DdeClientTransaction((LPBYTE)data,
+                                    size * sizeof(wxChar),
                                     GetHConv(),
                                     NULL,
                                     format,
                                     GetHConv(),
                                     NULL,
                                     format,
@@ -572,15 +599,15 @@ wxChar *wxDDEConnection::Request(const wxString& item, int *size, wxIPCFormat fo
 
     DWORD len = DdeGetData(returned_data, NULL, 0, 0);
 
 
     DWORD len = DdeGetData(returned_data, NULL, 0, 0);
 
-    wxChar *data = GetBufferAtLeast( len );
+    wxChar *data = GetBufferAtLeast( len/sizeof(wxChar) );
     wxASSERT_MSG(data != NULL,
                  _T("Buffer too small in wxDDEConnection::Request") );
     wxASSERT_MSG(data != NULL,
                  _T("Buffer too small in wxDDEConnection::Request") );
-    DdeGetData(returned_data, (LPBYTE)data, len, 0);
+    (void) DdeGetData(returned_data, (LPBYTE)data, len, 0);
 
 
-    DdeFreeDataHandle(returned_data);
+    (void) DdeFreeDataHandle(returned_data);
 
     if (size)
 
     if (size)
-        *size = (int)len;
+        *size = (int)len/sizeof(wxChar);
 
     return data;
 }
 
     return data;
 }
@@ -594,7 +621,8 @@ bool wxDDEConnection::Poke(const wxString& item, wxChar *data, int size, wxIPCFo
     }
 
     HSZ item_atom = DDEGetAtom(item);
     }
 
     HSZ item_atom = DDEGetAtom(item);
-    bool ok = DdeClientTransaction((LPBYTE)data, size,
+    bool ok = DdeClientTransaction((LPBYTE)data,
+                                   size * sizeof(wxChar),
                                    GetHConv(),
                                    item_atom, format,
                                    XTYP_POKE,
                                    GetHConv(),
                                    item_atom, format,
                                    XTYP_POKE,
@@ -752,7 +780,7 @@ _DDECallback(WORD wType,
                 {
                     DWORD len = DdeGetData(hData, NULL, 0, 0);
 
                 {
                     DWORD len = DdeGetData(hData, NULL, 0, 0);
 
-                    wxChar *data = connection->GetBufferAtLeast( len );
+                    wxChar *data = connection->GetBufferAtLeast( len/sizeof(wxChar) );
                     wxASSERT_MSG(data != NULL,
                                  _T("Buffer too small in _DDECallback (XTYP_EXECUTE)") );
 
                     wxASSERT_MSG(data != NULL,
                                  _T("Buffer too small in _DDECallback (XTYP_EXECUTE)") );
 
@@ -762,7 +790,7 @@ _DDECallback(WORD wType,
 
                     if ( connection->OnExecute(connection->m_topicName,
                                                data,
 
                     if ( connection->OnExecute(connection->m_topicName,
                                                data,
-                                               (int)len,
+                                               (int)len/sizeof(wxChar),
                                                (wxIPCFormat) wFmt) )
                     {
                         return (DDERETURN)(DWORD)DDE_FACK;
                                                (wxIPCFormat) wFmt) )
                     {
                         return (DDERETURN)(DWORD)DDE_FACK;
@@ -792,7 +820,7 @@ _DDECallback(WORD wType,
 
                         HDDEDATA handle = DdeCreateDataHandle(DDEIdInst,
                                                               (LPBYTE)data,
 
                         HDDEDATA handle = DdeCreateDataHandle(DDEIdInst,
                                                               (LPBYTE)data,
-                                                              user_size,
+                                                              user_size*sizeof(wxChar),
                                                               0,
                                                               hsz2,
                                                               wFmt,
                                                               0,
                                                               hsz2,
                                                               wFmt,
@@ -813,7 +841,7 @@ _DDECallback(WORD wType,
 
                     DWORD len = DdeGetData(hData, NULL, 0, 0);
 
 
                     DWORD len = DdeGetData(hData, NULL, 0, 0);
 
-                    wxChar *data = connection->GetBufferAtLeast( len );
+                    wxChar *data = connection->GetBufferAtLeast( len/sizeof(wxChar) );
                     wxASSERT_MSG(data != NULL,
                                  _T("Buffer too small in _DDECallback (XTYP_EXECUTE)") );
 
                     wxASSERT_MSG(data != NULL,
                                  _T("Buffer too small in _DDECallback (XTYP_EXECUTE)") );
 
@@ -824,7 +852,7 @@ _DDECallback(WORD wType,
                     connection->OnPoke(connection->m_topicName,
                                        item_name,
                                        data,
                     connection->OnPoke(connection->m_topicName,
                                        item_name,
                                        data,
-                                       (int)len,
+                                       (int)len/sizeof(wxChar),
                                        (wxIPCFormat) wFmt);
 
                     return (DDERETURN)DDE_FACK;
                                        (wxIPCFormat) wFmt);
 
                     return (DDERETURN)DDE_FACK;
@@ -875,7 +903,7 @@ _DDECallback(WORD wType,
                                     (
                                         DDEIdInst,
                                         (LPBYTE)connection->m_sendingData,
                                     (
                                         DDEIdInst,
                                         (LPBYTE)connection->m_sendingData,
-                                        connection->m_dataSize,
+                                        connection->m_dataSize*sizeof(wxChar),
                                         0,
                                         hsz2,
                                         connection->m_dataType,
                                         0,
                                         hsz2,
                                         connection->m_dataType,
@@ -900,7 +928,7 @@ _DDECallback(WORD wType,
 
                     DWORD len = DdeGetData(hData, NULL, 0, 0);
 
 
                     DWORD len = DdeGetData(hData, NULL, 0, 0);
 
-                    wxChar *data = connection->GetBufferAtLeast( len );
+                    wxChar *data = connection->GetBufferAtLeast( len/sizeof(wxChar) );
                     wxASSERT_MSG(data != NULL,
                                  _T("Buffer too small in _DDECallback (XTYP_ADVDATA)") );
 
                     wxASSERT_MSG(data != NULL,
                                  _T("Buffer too small in _DDECallback (XTYP_ADVDATA)") );
 
@@ -910,7 +938,7 @@ _DDECallback(WORD wType,
                     if ( connection->OnAdvise(connection->m_topicName,
                                               item_name,
                                               data,
                     if ( connection->OnAdvise(connection->m_topicName,
                                               item_name,
                                               data,
-                                              (int)len,
+                                              (int)len/sizeof(wxChar),
                                               (wxIPCFormat) wFmt) )
                     {
                         return (DDERETURN)(DWORD)DDE_FACK;
                                               (wxIPCFormat) wFmt) )
                     {
                         return (DDERETURN)(DWORD)DDE_FACK;
@@ -946,7 +974,10 @@ static HSZ DDEGetAtom(const wxString& str)
     return DDEAddAtom(str);
 }
 
     return DDEAddAtom(str);
 }
 
-// atom <-> strings
+/* atom <-> strings
+The returned handle has to be freed by the caller (using
+(static) DDEFreeString).
+*/
 static HSZ DDEAtomFromString(const wxString& s)
 {
     wxASSERT_MSG( DDEIdInst, _T("DDE not initialized") );
 static HSZ DDEAtomFromString(const wxString& s)
 {
     wxASSERT_MSG( DDEIdInst, _T("DDE not initialized") );
@@ -966,12 +997,20 @@ static wxString DDEStringFromAtom(HSZ hsz)
     static const size_t len = 256;
 
     wxString s;
     static const size_t len = 256;
 
     wxString s;
-    (void)DdeQueryString(DDEIdInst, hsz, s.GetWriteBuf(len), len, DDE_CP);
-    s.UngetWriteBuf();
+    (void)DdeQueryString(DDEIdInst, hsz, wxStringBuffer(s, len), len, DDE_CP);
 
     return s;
 }
 
 
     return s;
 }
 
+static void DDEFreeString(HSZ hsz)
+{
+    // DS: Failure to free a string handle might indicate there's
+    // some other severe error.
+    bool ok = (::DdeFreeStringHandle(DDEIdInst, hsz) != 0);
+    wxASSERT_MSG( ok, wxT("Failed to free DDE string handle") );
+    wxUnusedVar(ok);
+}
+
 // ----------------------------------------------------------------------------
 // error handling
 // ----------------------------------------------------------------------------
 // ----------------------------------------------------------------------------
 // error handling
 // ----------------------------------------------------------------------------