-
Notifications
You must be signed in to change notification settings - Fork 3
/
DDELink.cpp
295 lines (242 loc) · 7.45 KB
/
DDELink.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/******************************************************************************
** (C) Chris Oldwood
**
** MODULE: DDELINK.CPP
** COMPONENT: Network & Comms Library
** DESCRIPTION: The CDDELink class definition.
**
*******************************************************************************
*/
#include "Common.hpp"
#include "DDELink.hpp"
#include "DDEConv.hpp"
#include <WCL/Clipboard.hpp>
#include <Core/AnsiWide.hpp>
/******************************************************************************
**
** Local variables.
**
*******************************************************************************
*/
static uint s_nLinkFormat = CClipboard::RegisterFormat(TXT("Link"));
/******************************************************************************
** Method: Constructor.
**
** Description: .
**
** Parameters: None.
**
** Returns: Nothing.
**
*******************************************************************************
*/
CDDELink::CDDELink(CDDEConv* pConv, const tchar* pszItem, uint nFormat)
: m_nRefCount(0)
, m_pConv(pConv)
, m_strItem(pszItem)
, m_nFormat(nFormat)
, m_pAppData(nullptr)
{
}
/******************************************************************************
** Method: CopyLink()
**
** Description: Copy the link to the clipboard.
**
** Parameters: hOwner The owner window.
** pLink The link to copy.
**
** Returns: true or false.
**
*******************************************************************************
*/
bool CDDELink::CopyLink(HWND hOwner, const CDDELink* pLink)
{
ASSERT(::IsWindow(hOwner));
ASSERT(pLink != nullptr);
ASSERT(pLink->Conversation() != nullptr);
CDDEConv* pConv = pLink->Conversation();
return CopyLink(hOwner, pConv->Service(), pConv->Topic(), pLink->Item());
}
/******************************************************************************
** Method: CopyLink()
**
** Description: Copy the link to the clipboard.
**
** Parameters: hOwner The owner window.
** pszService The service name.
** pszTopic The topic name.
** pszItem The item name.
**
** Returns: true or false.
**
*******************************************************************************
*/
bool CDDELink::CopyLink(HWND hOwner, const tchar* pszService, const tchar* pszTopic, const tchar* pszItem)
{
ASSERT(::IsWindow(hOwner));
ASSERT(pszService != nullptr);
ASSERT(pszTopic != nullptr);
ASSERT(pszItem != nullptr);
bool bCopied = false;
// Open the clipboard.
if (::OpenClipboard(hOwner))
{
// Clear existing contents.
if (::EmptyClipboard())
{
// Strings must be ANSI.
std::string strService = T2A(pszService);
std::string strTopic = T2A(pszTopic);
std::string strItem = T2A(pszItem);
// Format is "SERVICE\0TOPIC\0ITEM\0\0".
size_t nChars = strService.length() + strTopic.length() + strItem.length() + 4;
size_t nBytes = Core::numBytes<char>(nChars);
HGLOBAL hLinkData = ::GlobalAlloc(GMEM_MOVEABLE, nBytes);
HGLOBAL hTextData = ::GlobalAlloc(GMEM_MOVEABLE, nBytes);
// Allocated blocks?
if ( (hLinkData != NULL) && (hTextData != NULL) )
{
char* pszLinkData = static_cast<char*>(::GlobalLock(hLinkData));
// Locked "Link" block?
if (pszLinkData != nullptr)
{
char* pszData = pszLinkData;
// Copy to clipboard buffer.
pszData = strcpy(pszData, strService.c_str());
pszData = strcpy(pszData + strlen(pszData) + 1, strTopic.c_str());
pszData = strcpy(pszData + strlen(pszData) + 1, strItem.c_str());
pszData = strcpy(pszData + strlen(pszData) + 1, "");
::GlobalUnlock(hLinkData);
// Copy to clipboard.
if (::SetClipboardData(s_nLinkFormat, hLinkData) != NULL)
bCopied = true;
}
char* pszTextData = static_cast<char*>(::GlobalLock(hTextData));
// Locked "Text" block?
if (pszLinkData != nullptr)
{
char* pszData = pszTextData;
// Copy to clipboard buffer.
strcpy(pszData, strService.c_str());
strcat(pszData, "|");
strcat(pszData, strTopic.c_str());
strcat(pszData, "!");
strcat(pszData, strItem.c_str());
::GlobalUnlock(hTextData);
// Copy to clipboard.
if (::SetClipboardData(CF_TEXT, hTextData) != NULL)
bCopied = true;
}
}
}
// Close it.
::CloseClipboard();
}
return bCopied;
}
/******************************************************************************
** Method: CanPasteLink()
**
** Description: Queries if a link is on the clipboard.
**
** Parameters: None.
**
** Returns: true or false.
**
*******************************************************************************
*/
bool CDDELink::CanPasteLink()
{
return CClipboard::IsFormatAvail(s_nLinkFormat);
}
/******************************************************************************
** Method: PasteLink()
**
** Description: Get a DDE "link" from the clipboard as a single string.
** NB: Returns the link in "SERVICE|TOPIC!ITEM" format.
**
** Parameters: strLink The returned link.
**
** Returns: true or false.
**
*******************************************************************************
*/
bool CDDELink::PasteLink(CString& strLink)
{
CString strService, strTopic, strItem;
// Get raw link.
if (!PasteLink(strService, strTopic, strItem))
return false;
// Format.
strLink.Format(TXT("%s|%s!%s"), strService.c_str(), strTopic.c_str(), strItem.c_str());
return true;
}
/******************************************************************************
** Method: PasteLink()
**
** Description: Get a DDE "link" from the clipboard.
**
** Parameters: strService The service name.
** strTopic The topic name.
** strItem The item name.
**
** Returns: true or false.
**
*******************************************************************************
*/
bool CDDELink::PasteLink(CString& strService, CString& strTopic, CString& strItem)
{
bool bPasted = false;
// Open the clipboard.
if (::OpenClipboard(NULL))
{
HGLOBAL hData = ::GetClipboardData(s_nLinkFormat);
// Got data?
if (hData != NULL)
{
const char* psz = static_cast<const char*>(::GlobalLock(hData));
// Locked block?
if (psz != nullptr)
{
// Format is "SERVICE\0TOPIC\0ITEM\0\0" [ANSI].
const char* pszService = psz;
const char* pszTopic = pszService + strlen(pszService) + 1;
const char* pszItem = pszTopic + strlen(pszTopic) + 1;
// Copy to return buffers.
strService = A2T(pszService);
strTopic = A2T(pszTopic);
strItem = A2T(pszItem);
::GlobalUnlock(hData);
bPasted = true;
}
}
// Close it.
::CloseClipboard();
}
return bPasted;
}
////////////////////////////////////////////////////////////////////////////////
//! Parse the DDE link into its separate parts.
bool CDDELink::ParseLink(const tstring& link, tstring& service, tstring& topic, tstring& item)
{
size_t serviceBegin = 0;
size_t serviceEnd = link.find_first_of('|', serviceBegin);
if ( (serviceEnd == tstring::npos) || (serviceEnd == serviceBegin) )
return false;
size_t topicBegin = serviceEnd + 1;
size_t topicEnd = link.find_first_of('!', topicBegin);
if ( (topicEnd == tstring::npos) || (topicEnd == topicBegin) )
return false;
size_t itemBegin = topicEnd + 1;
size_t itemEnd = link.length();
if (itemEnd == itemBegin)
return false;
service = link.substr(serviceBegin, serviceEnd - serviceBegin);
topic = link.substr(topicBegin, topicEnd - topicBegin);
item = link.substr(itemBegin, itemEnd - itemBegin);
ASSERT(service.length() != 0);
ASSERT(topic.length() != 0);
ASSERT(item.length() != 0);
return true;
}