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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
|
/*
Miranda SmileyAdd Plugin
Copyright (C) 2005 - 2011 Boris Krasnovskiy
Copyright (C) 2003 - 2004 Rein-Peter de Boer
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation version 2
of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "general.h"
ISmileyBase* CreateSmileyObject(SmileyType* sml);
ISmileyBase* CreateAniSmileyObject(SmileyType* sml, COLORREF clr, bool ishpp);
bool g_HiddenTextSupported = true;
// {8CC497C0-A1DF-11CE-8098-00AA0047BE5D}
const GUID IID_ITextDocument =
{ 0x8CC497C0, 0xA1DF, 0x11CE, { 0x80,0x98,0x00,0xAA,0x00,0x47,0xBE,0x5D } };
void LookupAllSmileys(SmileyPackType* smileyPack, SmileyPackCType* smileyCPack, const TCHAR* lpstrText,
SmileysQueueType& smllist, const bool firstOnly)
{
if (lpstrText == NULL || *lpstrText == 0) return;
SmileyPackType::SmileyLookupType* sml = smileyPack ? smileyPack->GetSmileyLookup() : NULL;
SmileyPackCType::SmileyLookupType* smlc = smileyCPack ? &smileyCPack->GetSmileyLookup() : NULL;
// Precompute number of smileys
int smlszo = sml ? sml->getCount() : 0;
int smlszc = smlc ? smlc->getCount() : 0;
int smlsz = smlszo + smlszc;
if (smlsz == 0) return;
// All possible smileys
SmileyLookup::SmileyLocVecType* smileys = new SmileyLookup::SmileyLocVecType [smlsz];
// Find all possible smileys
CMString tmpstr(lpstrText);
int i = 0;
if (sml)
for (int j=0; j < sml->getCount(); j++) {
(*sml)[j].Find(tmpstr, smileys[i], false);
i++;
}
if (smlc)
for (int j=0; j < smlc->getCount(); j++) {
(*smlc)[j].Find(tmpstr, smileys[i], false);
i++;
}
int *csmlit = (int*)alloca(smlsz * sizeof(int));
memset(csmlit, 0, smlsz * sizeof(int));
long numCharsSoFar = 0;
size_t smloff = 0;
while (true) {
int firstSml = -1;
int firstSmlRef = -1;
SmileyLookup::SmileyLocVecType* smlf = NULL;
for (int csml=0; csml < smlsz; csml++) {
SmileyLookup::SmileyLocVecType& smlv = smileys[csml];
int tsml;
for (tsml = csmlit[csml]; tsml < smlv.getCount(); tsml++) {
if (smlv[tsml].pos >= smloff) {
if (firstSmlRef == -1 || smlv[tsml].pos < (*smlf)[firstSmlRef].pos ||
(smlv[tsml].pos == (*smlf)[firstSmlRef].pos && smlv[tsml].len > (*smlf)[firstSmlRef].len))
{
firstSmlRef = tsml;
firstSml = csml;
smlf = &smileys[csml];
}
break;
}
}
csmlit[csml] = tsml;
}
// // Nothing to parse, exiting
if (firstSml == -1)
break;
ReplaceSmileyType *dat = new ReplaceSmileyType;
const TCHAR* textToSearch = lpstrText + smloff;
const TCHAR* textSmlStart = lpstrText + (*smlf)[firstSmlRef].pos;
const TCHAR* textSmlEnd = textSmlStart + (*smlf)[firstSmlRef].len;
// check if leading space exist
const TCHAR* prech = _tcsdec(textToSearch, textSmlStart);
dat->ldspace = prech != NULL ? _istspace(*prech) != 0 : smloff == 0;
// check if trailing space exist
dat->trspace = *textSmlEnd == 0 || _istspace(*textSmlEnd);
// compute text location in RichEdit
dat->loc.cpMin = (long)_tcsnccnt(textToSearch, (*smlf)[firstSmlRef].pos - smloff) + numCharsSoFar;
dat->loc.cpMax = numCharsSoFar = (long)_tcsnccnt(textSmlStart, (*smlf)[firstSmlRef].len) + dat->loc.cpMin;
if (!opt.EnforceSpaces || (dat->ldspace && dat->trspace)) {
dat->ldspace |= !opt.SurroundSmileyWithSpaces;
dat->trspace |= !opt.SurroundSmileyWithSpaces;
if (firstSml < smlszo) {
dat->sml = smileyPack->GetSmiley((*sml)[firstSml].GetIndex());
dat->smlc = NULL;
}
else {
dat->smlc = smileyCPack->GetSmiley((*smlc)[firstSml-smlszo].GetIndex());
dat->sml = NULL;
}
if (dat->sml != NULL || dat->smlc != NULL) {
// First smiley found record it
smllist.insert(dat);
if (firstOnly) break;
}
else delete dat;
}
else delete dat;
// Advance string pointer to search for the next smiley
smloff = int((*smlf)[firstSmlRef].pos + (*smlf)[firstSmlRef].len);
csmlit[firstSml]++;
}
delete[] smileys;
}
void FindSmileyInText(SmileyPackType* smp, const TCHAR* str,
unsigned& first, unsigned& size, SmileyType** sml)
{
SmileysQueueType smllist;
LookupAllSmileys(smp, NULL, str, smllist, true);
if (smllist.getCount() == 0) {
size = 0;
*sml = NULL;
}
else {
first = smllist[0].loc.cpMin;
size = smllist[0].loc.cpMax - smllist[0].loc.cpMin;
*sml = smllist[0].sml;
}
}
SmileyType* FindButtonSmiley(SmileyPackType* smp)
{
unsigned start, size;
SmileyType* sml;
FindSmileyInText(smp, smp->GetButtonSmiley(), start, size, &sml);
return sml;
}
void UpdateSelection(CHARRANGE& sel, int pos, int dif)
{
if (sel.cpMax == sel.cpMin) {
if (sel.cpMax < LONG_MAX && sel.cpMax > pos) {
sel.cpMax += dif;
sel.cpMin += dif;
}
}
else {
if (sel.cpMax >= pos && sel.cpMax < LONG_MAX) sel.cpMax += dif;
if (sel.cpMin > pos) sel.cpMin += dif;
}
}
void ReplaceSmileys(HWND hwnd, SmileyPackType* smp, SmileyPackCType* smcp, const CHARRANGE& sel,
bool useHidden, bool ignoreLast, bool unFreeze, bool fireView)
{
IRichEditOle* RichEditOle = NULL;
if (SendMessage(hwnd, EM_GETOLEINTERFACE, 0, (LPARAM)&RichEditOle) == 0)
return;
if (RichEditOle == NULL)
return;
ITextDocument* TextDocument = NULL;
if (RichEditOle->QueryInterface(IID_ITextDocument, (void**)&TextDocument) != S_OK) {
RichEditOle->Release();
return;
}
long cnt;
if (smp == NULL && smcp == NULL) {
if (unFreeze) TextDocument->Unfreeze(&cnt);
TextDocument->Release();
RichEditOle->Release();
return;
}
// retrieve text range
ITextRange* TextRange;
if (TextDocument->Range(sel.cpMin, sel.cpMax, &TextRange) != S_OK) {
TextDocument->Release();
RichEditOle->Release();
return;
}
// retrieve text to parse for smileys
BSTR btxt = 0;
if (TextRange->GetText(&btxt) != S_OK) {
TextRange->Release();
TextDocument->Release();
RichEditOle->Release();
return;
}
TextRange->Release();
SmileysQueueType smllist;
LookupAllSmileys(smp, smcp, W2T_SM(btxt), smllist, false);
SysFreeString(btxt);
if (smllist.getCount() != 0) {
// disable screen updates
TextDocument->Freeze(&cnt);
TCHAR classname[20];
GetClassName(hwnd, classname, _countof(classname));
bool ishpp = (_tcsncmp(classname, _T("THppRichEdit"), 12) == 0) || fireView;
SetRichCallback(hwnd, NULL, false, true);
bool rdo = (GetWindowLongPtr(hwnd, GWL_STYLE) & ES_READONLY) != 0;
if (rdo) SendMessage(hwnd, EM_SETREADONLY, FALSE, 0);
ITextSelection* TextSelection;
TextDocument->GetSelection(&TextSelection);
ITextFont *TextFont;
TextSelection->GetFont(&TextFont);
//save selection
CHARRANGE oldSel;
TextSelection->GetStart(&oldSel.cpMin);
TextSelection->GetEnd(&oldSel.cpMax);
CHARFORMAT2 chf;
chf.cbSize = sizeof(chf);
chf.dwMask = CFM_ALL2;
// Determine background color
// This logic trying to minimize number of background color changes
static COLORREF bkgColor = GetSysColor(COLOR_WINDOW);
COLORREF bkgColorPv = (COLORREF)SendMessage(hwnd, EM_SETBKGNDCOLOR, 0, bkgColor);
if (bkgColorPv != bkgColor) {
bkgColor = bkgColorPv;
SendMessage(hwnd, EM_SETBKGNDCOLOR, 0, bkgColor);
}
HDC hdc = GetDC(hwnd);
int sclX = GetDeviceCaps(hdc, LOGPIXELSX);
int sclY = GetDeviceCaps(hdc, LOGPIXELSY);
unsigned numBTBSm = 0;
BSTR spaceb = SysAllocString(_T(" "));
// Replace smileys specified in the list in RichEdit
for (int j = smllist.getCount()-1; j >= 0; j--) {
CHARRANGE& smlpos = smllist[j].loc;
if (ignoreLast && oldSel.cpMax == smlpos.cpMax)
continue;
smlpos.cpMin += sel.cpMin;
smlpos.cpMax += sel.cpMin;
// Find all back to back smileys and for propper hidden text detection
if (numBTBSm == 0) {
CHARRANGE lastPos = smlpos;
for (int jn = j; jn--; ) {
if (jn != j && smllist[jn].loc.cpMax != lastPos.cpMin)
break;
++numBTBSm;
lastPos.cpMin = smllist[jn].loc.cpMin;
}
TextSelection->SetRange(lastPos.cpMin, lastPos.cpMax);
long hid;
TextFont->GetHidden(&hid);
if (hid == tomFalse) numBTBSm = 0;
}
if (numBTBSm != 0) {
--numBTBSm;
continue;
}
SmileyType* sml = smllist[j].sml;
SmileyCType* smlc = smllist[j].smlc;
if (sml == NULL && smlc == NULL) continue;
// Select text analyze
TextSelection->SetRange(smlpos.cpMin, smlpos.cpMax);
BSTR btxt = NULL;
if (smlc == NULL && sml->IsText()) {
btxt = SysAllocString(T2W_SM(sml->GetToolText().c_str()));
TextSelection->SetText(btxt);
}
else {
TextSelection->GetText(&btxt);
// Get font properties
SendMessage(hwnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM)&chf);
//do not look for smileys in hyperlinks
if ((chf.dwEffects & (CFE_LINK | CFE_HIDDEN)) != 0)
continue;
SIZE osize;
if (sml)
sml->GetSize(osize);
else
smlc->GetSize(osize);
if (osize.cx == 0 || osize.cy == 0)
continue;
int sizeX, sizeY;
if (opt.ScaleToTextheight) {
sizeY = CalculateTextHeight(hdc, &chf);
sizeX = osize.cx * sizeY / osize.cy;
int dx = osize.cx - sizeX;
sizeX += dx & 1;
int dy = osize.cy - sizeY;
sizeY += dy & 1;
}
else {
sizeX = osize.cx;
sizeY = osize.cy;
}
if (smlc != NULL && opt.MaxCustomSmileySize && (unsigned)sizeY > opt.MaxCustomSmileySize) {
sizeY = opt.MaxCustomSmileySize;
sizeX = osize.cx * sizeY / osize.cy;
int dx = osize.cx - sizeX;
sizeX += dx & 1;
int dy = osize.cy - opt.MaxCustomSmileySize;
sizeY += dy & 1;
}
if (opt.MinSmileySize && (unsigned)sizeY < opt.MinSmileySize) {
sizeY = opt.MinSmileySize;
sizeX = osize.cx * sizeY / osize.cy;
int dx = osize.cx - sizeX;
sizeX += dx & 1;
int dy = osize.cy - opt.MinSmileySize;
sizeY += dy & 1;
}
// Convert pixel to HIMETRIC
SIZEL sizehm;
sizehm.cx = (2540 * (sizeX+1) + (sclX >> 1)) / sclX;
sizehm.cy = (2540 * (sizeY+1) + (sclY >> 1)) / sclY;
// If font does not have designated background use control background
if (chf.dwEffects & CFE_AUTOBACKCOLOR) chf.crBackColor = bkgColor;
// insert space after
if (!smllist[j].trspace && useHidden) {
TextSelection->SetStart(smlpos.cpMax);
TextSelection->TypeText(spaceb);
UpdateSelection(oldSel, smlpos.cpMax , 1);
// Restore selection
TextSelection->SetRange(smlpos.cpMin, smlpos.cpMax);
}
if (g_HiddenTextSupported && useHidden) {
TextFont->SetHidden(tomTrue);
TextSelection->SetEnd(smlpos.cpMin);
UpdateSelection(oldSel, smlpos.cpMin , 1);
}
else UpdateSelection(oldSel, smlpos.cpMin, -(int)SysStringLen(btxt)+1);
ISmileyBase* smileyBase = CreateAniSmileyObject(smlc ? smlc : sml, chf.crBackColor, ishpp);
if (smileyBase == NULL) continue;
smileyBase->SetExtent(DVASPECT_CONTENT, &sizehm);
smileyBase->SetHint(W2T_SM(btxt));
smileyBase->SetPosition(hwnd, NULL);
// Get the RichEdit container site
IOleClientSite *pOleClientSite;
RichEditOle->GetClientSite(&pOleClientSite);
// Now Add the object to the RichEdit
REOBJECT reobject = { sizeof(REOBJECT) };
reobject.cp = REO_CP_SELECTION;
reobject.dvaspect = DVASPECT_CONTENT;
reobject.poleobj = smileyBase;
reobject.polesite = pOleClientSite;
reobject.dwFlags = REO_BELOWBASELINE | REO_BLANK;
reobject.dwUser = (DWORD)smileyBase;
// Insert the bitmap at the current location in the richedit control
RichEditOle->InsertObject(&reobject);
smileyBase->Release();
// insert space before
if (!smllist[j].ldspace && useHidden) {
TextSelection->SetRange(smlpos.cpMin, smlpos.cpMin);
TextSelection->TypeText(spaceb);
UpdateSelection(oldSel, smlpos.cpMin , 1);
}
}
SysFreeString(btxt);
}
SysFreeString(spaceb);
TextSelection->SetRange(oldSel.cpMin, oldSel.cpMax);
if (rdo) SendMessage(hwnd, EM_SETREADONLY, TRUE, 0);
TextFont->Release();
TextSelection->Release();
ReleaseDC(hwnd, hdc);
TextDocument->Unfreeze(&cnt);
if (cnt == 0) UpdateWindow(hwnd);
}
if (unFreeze) {
TextDocument->Unfreeze(&cnt);
if (cnt == 0) UpdateWindow(hwnd);
}
TextDocument->Release();
RichEditOle->Release();
}
void ReplaceSmileysWithText(HWND hwnd, CHARRANGE& sel, bool keepFrozen)
{
IRichEditOle* RichEditOle = NULL;
if (SendMessage(hwnd, EM_GETOLEINTERFACE, 0, (LPARAM)&RichEditOle) == 0)
return;
if (RichEditOle == NULL)
return;
ITextDocument* TextDocument;
if (RichEditOle->QueryInterface(IID_ITextDocument, (void**)&TextDocument) != S_OK) {
RichEditOle->Release();
return;
}
// retrieve text range
ITextRange* TextRange;
if (TextDocument->Range(0, 0, &TextRange) != S_OK) {
TextDocument->Release();
RichEditOle->Release();
return;
}
long cnt;
TextDocument->Freeze(&cnt);
bool rdo = (GetWindowLongPtr(hwnd, GWL_STYLE) & ES_READONLY) != 0;
if (rdo) SendMessage(hwnd, EM_SETREADONLY, FALSE, 0);
CHARRANGE oldSel;
SendMessage(hwnd, EM_EXGETSEL, 0, (LPARAM)&oldSel);
int objectCount = RichEditOle->GetObjectCount();
for (int i = objectCount-1; i >= 0; i--) {
REOBJECT reObj = {0};
reObj.cbStruct = sizeof(REOBJECT);
HRESULT hr = RichEditOle->GetObject(i, &reObj, REO_GETOBJ_POLEOBJ);
if (FAILED(hr))
continue;
if (reObj.cp < sel.cpMin) {
reObj.poleobj->Release();
break;
}
ISmileyBase *igsc = NULL;
if (reObj.cp < sel.cpMax && reObj.clsid == CLSID_NULL)
reObj.poleobj->QueryInterface(IID_ISmileyAddSmiley, (void**) &igsc);
reObj.poleobj->Release();
if (igsc == NULL)
continue;
TextRange->SetRange(reObj.cp, reObj.cp + 1);
BSTR bstr = NULL;
igsc->GetTooltip(&bstr);
TextRange->SetText(bstr);
unsigned int len = SysStringLen(bstr);
UpdateSelection(oldSel, reObj.cp, len-1);
UpdateSelection(sel, reObj.cp, len-1);
SysFreeString(bstr);
igsc->Release();
}
SendMessage(hwnd, EM_EXSETSEL, 0, (LPARAM)&oldSel);
if (rdo) SendMessage(hwnd, EM_SETREADONLY, TRUE, 0);
if (!keepFrozen) TextDocument->Unfreeze(&cnt);
TextRange->Release();
TextDocument->Release();
RichEditOle->Release();
}
|