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
|
// Feel free to use this code in your own applications.
// The Author does not guarantee anything about this code.
// Author : Yves Maurer
// FormattedTextDraw.cpp : Implementation of CFormattedTextDraw
#include "headers.h"
const IID IID_ITextServices = { // 8d33f740-cf58-11ce-a89d-00aa006cadc5
0x8d33f740,
0xcf58,
0x11ce,
{0xa8, 0x9d, 0x00, 0xaa, 0x00, 0x6c, 0xad, 0xc5}
};
const IID IID_ITextHost = { // c5bdd8d0-d26e-11ce-a89e-00aa006cadc5
0xc5bdd8d0,
0xd26e,
0x11ce,
{0xa8, 0x9e, 0x00, 0xaa, 0x00, 0x6c, 0xad, 0xc5}
};
static const IID IID_ITextDocument = {
0x8CC497C0,
0xA1DF,
0x11CE,
{0x80,0x98,0x00,0xAA,0x00,0x47,0xBE,0x5D}
};
/////////////////////////////////////////////////////////////////////////////
// CallBack functions
DWORD CALLBACK EditStreamInCallback(DWORD_PTR dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
{
COOKIE *pCookie = (COOKIE*)dwCookie;
if (pCookie->isUnicode) {
if ((pCookie->cbSize - pCookie->cbCount)*sizeof(WCHAR) < (size_t)cb)
*pcb = LONG(pCookie->cbSize - pCookie->cbCount)*sizeof(WCHAR);
else
*pcb = cb & ~1UL;
memcpy(pbBuff, pCookie->unicode + pCookie->cbCount, *pcb);
pCookie->cbCount += *pcb / sizeof(WCHAR);
}
else {
if (pCookie->cbSize - pCookie->cbCount < (DWORD)cb)
*pcb = LONG(pCookie->cbSize - pCookie->cbCount);
else
*pcb = cb;
memcpy(pbBuff, pCookie->ansi + pCookie->cbCount, *pcb);
pCookie->cbCount += *pcb;
}
return 0; // callback succeeded - no errors
}
/////////////////////////////////////////////////////////////////////////////
// CFormattedTextDraw
HRESULT CFormattedTextDraw::putRTFTextA(char *newVal)
{
if (!m_spTextServices)
return S_FALSE;
m_editCookie.isUnicode = false;
m_editCookie.ansi = newVal;
m_editCookie.cbSize = mir_strlen(m_editCookie.ansi);
m_editCookie.cbCount = 0;
EDITSTREAM editStream;
editStream.dwCookie = (DWORD_PTR)&m_editCookie;
editStream.dwError = 0;
editStream.pfnCallback = (EDITSTREAMCALLBACK)EditStreamInCallback;
LRESULT lResult = 0;
HRESULT hr = m_spTextServices->TxSendMessage(EM_STREAMIN, (WPARAM)(SF_RTF), (LPARAM)&editStream, &lResult);
return S_OK;
}
HRESULT CFormattedTextDraw::putRTFTextW(WCHAR *newVal)
{
if (!m_spTextServices)
return S_FALSE;
m_editCookie.isUnicode = true;
m_editCookie.unicode = newVal;
m_editCookie.cbSize = mir_wstrlen(m_editCookie.unicode);
m_editCookie.cbCount = 0;
EDITSTREAM editStream;
editStream.dwCookie = (DWORD_PTR)&m_editCookie;
editStream.dwError = 0;
editStream.pfnCallback = (EDITSTREAMCALLBACK)EditStreamInCallback;
LRESULT lResult = 0;
HRESULT hr = m_spTextServices->TxSendMessage(EM_STREAMIN, (WPARAM)(SF_RTF | SF_UNICODE), (LPARAM)&editStream, &lResult);
return S_OK;
}
HRESULT CFormattedTextDraw::putTextA(char *newVal)
{
if (!m_spTextServices)
return S_FALSE;
m_editCookie.isUnicode = false;
m_editCookie.ansi = newVal;
m_editCookie.cbSize = mir_strlen(m_editCookie.ansi);
m_editCookie.cbCount = 0;
EDITSTREAM editStream;
editStream.dwCookie = (DWORD_PTR)&m_editCookie;
editStream.dwError = 0;
editStream.pfnCallback = (EDITSTREAMCALLBACK)EditStreamInCallback;
LRESULT lResult = 0;
HRESULT hr = m_spTextServices->TxSendMessage(EM_STREAMIN, (WPARAM)(SF_TEXT), (LPARAM)&editStream, &lResult);
CHARFORMAT cf;
cf.cbSize = sizeof(cf);
cf.dwMask = CFM_FACE | CFM_BOLD;
cf.dwEffects = 0;
mir_sntprintf(cf.szFaceName, SIZEOF(cf.szFaceName), _T("MS Shell Dlg"));
m_spTextServices->TxSendMessage(EM_SETCHARFORMAT, (WPARAM)(SCF_ALL), (LPARAM)&cf, &lResult);
return S_OK;
}
HRESULT CFormattedTextDraw::putTextW(WCHAR *newVal)
{
if (!m_spTextServices)
return S_FALSE;
m_editCookie.isUnicode = true;
m_editCookie.unicode = newVal;
m_editCookie.cbSize = mir_wstrlen(m_editCookie.unicode);
m_editCookie.cbCount = 0;
EDITSTREAM editStream;
editStream.dwCookie = (DWORD_PTR)&m_editCookie;
editStream.dwError = 0;
editStream.pfnCallback = (EDITSTREAMCALLBACK)EditStreamInCallback;
LRESULT lResult = 0;
HRESULT hr = m_spTextServices->TxSendMessage(EM_STREAMIN, (WPARAM)(SF_TEXT | SF_UNICODE), (LPARAM)&editStream, &lResult);
CHARFORMAT cf;
cf.cbSize = sizeof(cf);
cf.dwMask = CFM_FACE | CFM_BOLD;
cf.dwEffects = 0;
mir_sntprintf(cf.szFaceName, SIZEOF(cf.szFaceName), _T("MS Shell Dlg"));
m_spTextServices->TxSendMessage(EM_SETCHARFORMAT, (WPARAM)(SCF_ALL), (LPARAM)&cf, &lResult);
return S_OK;
}
HRESULT CFormattedTextDraw::Draw(void *hdcDraw, RECT *prc)
{
LOGFONT lf;
GetObject(GetCurrentObject((HDC)hdcDraw, OBJ_FONT), sizeof(lf), &lf);
LRESULT lResult;
CHARFORMAT cf;
cf.cbSize = sizeof(cf);
cf.dwMask = CFM_FACE/*|CFM_COLOR*/ | CFM_CHARSET | CFM_SIZE |
(lf.lfWeight >= FW_BOLD ? CFM_BOLD : 0) |
(lf.lfItalic ? CFM_ITALIC : 0) |
(lf.lfUnderline ? CFM_UNDERLINE : 0) |
(lf.lfStrikeOut ? CFM_STRIKEOUT : 0);
cf.dwEffects = CFE_BOLD | CFE_ITALIC | CFE_STRIKEOUT | CFE_UNDERLINE;
cf.crTextColor = GetTextColor((HDC)hdcDraw);
cf.bCharSet = lf.lfCharSet;
cf.yHeight = 1440 * abs(lf.lfHeight) / GetDeviceCaps((HDC)hdcDraw, LOGPIXELSY);
mir_sntprintf(cf.szFaceName, SIZEOF(cf.szFaceName), lf.lfFaceName);
m_spTextServices->TxSendMessage(EM_SETCHARFORMAT, (WPARAM)(SCF_ALL), (LPARAM)&cf, &lResult);
m_spTextServices->TxDraw(
DVASPECT_CONTENT, // Draw Aspect
0, // Lindex
NULL, // Info for drawing optimization
NULL, // target device information
(HDC)hdcDraw, // Draw device HDC
NULL, // Target device HDC
(RECTL *)prc, // Bounding client rectangle
NULL, // Clipping rectangle for metafiles
(RECT *)NULL, // Update rectangle
NULL, // Call back function
NULL, // Call back parameter
TXTVIEW_INACTIVE); // What view of the object could be TXTVIEW_ACTIVE
return S_OK;
}
HRESULT CFormattedTextDraw::Create()
{
return CreateTextServicesObject();
}
HRESULT CFormattedTextDraw::get_NaturalSize(void *hdcDraw, long *Width, long *Height)
{
LOGFONT lf;
GetObject(GetCurrentObject((HDC)hdcDraw, OBJ_FONT), sizeof(lf), &lf);
LRESULT lResult;
CHARFORMAT cf;
cf.cbSize = sizeof(cf);
cf.dwMask = CFM_FACE/*|CFM_COLOR*/ | CFM_CHARSET | CFM_SIZE |
(lf.lfWeight >= FW_BOLD ? CFM_BOLD : 0) |
(lf.lfItalic ? CFM_ITALIC : 0) |
(lf.lfUnderline ? CFM_UNDERLINE : 0) |
(lf.lfStrikeOut ? CFM_STRIKEOUT : 0);
cf.dwEffects = CFE_BOLD | CFE_ITALIC | CFE_STRIKEOUT | CFE_UNDERLINE;
cf.crTextColor = GetTextColor((HDC)hdcDraw);
cf.bCharSet = lf.lfCharSet;
cf.yHeight = 1440 * abs(lf.lfHeight) / GetDeviceCaps((HDC)hdcDraw, LOGPIXELSY);
mir_sntprintf(cf.szFaceName, SIZEOF(cf.szFaceName), lf.lfFaceName);
if (!m_spTextServices)
return S_FALSE;
m_spTextServices->TxSendMessage(EM_SETCHARFORMAT, (WPARAM)(SCF_ALL), (LPARAM)&cf, &lResult);
*Height = 1;
SIZEL szExtent;
szExtent.cx = *Width;
szExtent.cy = *Height;
if (m_spTextServices->TxGetNaturalSize(DVASPECT_CONTENT, (HDC)hdcDraw, NULL, NULL, TXTNS_FITTOCONTENT, &szExtent, Width, Height) != S_OK)
return S_FALSE;
return S_OK;
}
/////////////////////////////////////////////////////////////////////////////
// ITextHost functions
HDC CFormattedTextDraw::TxGetDC()
{
return NULL;
}
INT CFormattedTextDraw::TxReleaseDC(HDC hdc)
{
return 1;
}
BOOL CFormattedTextDraw::TxShowScrollBar(INT fnBar, BOOL fShow)
{
return FALSE;
}
BOOL CFormattedTextDraw::TxEnableScrollBar(INT fuSBFlags, INT fuArrowflags)
{
return FALSE;
}
BOOL CFormattedTextDraw::TxSetScrollRange(INT fnBar, LONG nMinPos, INT nMaxPos, BOOL fRedraw)
{
return FALSE;
}
BOOL CFormattedTextDraw::TxSetScrollPos(INT fnBar, INT nPos, BOOL fRedraw)
{
return FALSE;
}
void CFormattedTextDraw::TxInvalidateRect(LPCRECT prc, BOOL fMode)
{}
void CFormattedTextDraw::TxViewChange(BOOL fUpdate)
{}
BOOL CFormattedTextDraw::TxCreateCaret(HBITMAP hbmp, INT xWidth, INT yHeight)
{
return FALSE;
}
BOOL CFormattedTextDraw::TxShowCaret(BOOL fShow)
{
return FALSE;
}
BOOL CFormattedTextDraw::TxSetCaretPos(INT x, INT y)
{
return FALSE;
}
BOOL CFormattedTextDraw::TxSetTimer(UINT idTimer, UINT uTimeout)
{
return FALSE;
}
void CFormattedTextDraw::TxKillTimer(UINT idTimer)
{
}
void CFormattedTextDraw::TxScrollWindowEx(INT dx, INT dy, LPCRECT lprcScroll, LPCRECT lprcClip, HRGN hrgnUpdate, LPRECT lprcUpdate, UINT fuScroll)
{
}
void CFormattedTextDraw::TxSetCapture(BOOL fCapture)
{
}
void CFormattedTextDraw::TxSetFocus()
{
}
void CFormattedTextDraw::TxSetCursor(HCURSOR hcur, BOOL fText)
{
if (fText)
SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW)));
else
SetCursor(hcur);
}
BOOL CFormattedTextDraw::TxScreenToClient(LPPOINT lppt)
{
if (!m_hwndParent) return FALSE;
return ScreenToClient(m_hwndParent, lppt);
}
BOOL CFormattedTextDraw::TxClientToScreen(LPPOINT lppt)
{
if (!m_hwndParent) return FALSE;
// BOOL result = ;
// lppt->x -= m_rcClient.left;
// lppt->y -= m_rcClient.left;
return ClientToScreen(m_hwndParent, lppt);
}
HRESULT CFormattedTextDraw::TxActivate(LONG * plOldState)
{
return S_OK;
}
HRESULT CFormattedTextDraw::TxDeactivate(LONG lNewState)
{
return S_OK;
}
HRESULT CFormattedTextDraw::TxGetClientRect(LPRECT prc)
{
*prc = m_rcClient;
return S_OK;
}
HRESULT CFormattedTextDraw::TxGetViewInset(LPRECT prc)
{
*prc = m_rcViewInset;
return S_OK;
}
HRESULT CFormattedTextDraw::TxGetCharFormat(const CHARFORMATW **ppCF)
{
*ppCF = m_pCF;
return S_OK;
}
HRESULT CFormattedTextDraw::TxGetParaFormat(const PARAFORMAT **ppPF)
{
*ppPF = &m_PF;
return S_OK;
}
COLORREF CFormattedTextDraw::TxGetSysColor(int nIndex)
{
return GetSysColor(nIndex);
}
HRESULT CFormattedTextDraw::TxGetBackStyle(TXTBACKSTYLE *pstyle)
{
*pstyle = TXTBACK_TRANSPARENT;
return S_OK;
}
HRESULT CFormattedTextDraw::TxGetMaxLength(DWORD *plength)
{
*plength = m_dwMaxLength;
return S_OK;
}
HRESULT CFormattedTextDraw::TxGetScrollBars(DWORD *pdwScrollBar)
{
*pdwScrollBar = m_dwScrollbar;
return S_OK;
}
HRESULT CFormattedTextDraw::TxGetPasswordChar(TCHAR *pch)
{
return S_FALSE;
}
HRESULT CFormattedTextDraw::TxGetAcceleratorPos(LONG *pcp)
{
*pcp = -1;
return S_OK;
}
HRESULT CFormattedTextDraw::TxGetExtent(LPSIZEL lpExtent)
{
return E_NOTIMPL;
}
HRESULT CFormattedTextDraw::OnTxCharFormatChange(const CHARFORMATW * pcf)
{
memcpy(m_pCF, pcf, pcf->cbSize);
return S_OK;
}
HRESULT CFormattedTextDraw::OnTxParaFormatChange(const PARAFORMAT * ppf)
{
memcpy(&m_PF, ppf, ppf->cbSize);
return S_OK;
}
HRESULT CFormattedTextDraw::TxGetPropertyBits(DWORD dwMask, DWORD *pdwBits)
{
*pdwBits = m_dwPropertyBits;
return S_OK;
}
HRESULT CFormattedTextDraw::TxNotify(DWORD iNotify, void *pv)
{
return S_OK;
}
HIMC CFormattedTextDraw::TxImmGetContext()
{
return NULL;
}
void CFormattedTextDraw::TxImmReleaseContext(HIMC himc)
{}
HRESULT CFormattedTextDraw::TxGetSelectionBarWidth(LONG *lSelBarWidth)
{
*lSelBarWidth = 100;
return S_OK;
}
/////////////////////////////////////////////////////////////////////////////
// custom functions
HRESULT CFormattedTextDraw::CharFormatFromHFONT(CHARFORMAT2W* pCF, HFONT hFont)
// Takes an HFONT and fills in a CHARFORMAT2W structure with the corresponding info
{
// Get LOGFONT for default font
if (!hFont)
hFont = (HFONT)GetStockObject(SYSTEM_FONT);
// Get LOGFONT for passed hfont
LOGFONT lf;
if (!GetObject(hFont, sizeof(LOGFONT), &lf))
return E_FAIL;
// Set CHARFORMAT structure
memset(pCF, 0, sizeof(CHARFORMAT2W));
pCF->cbSize = sizeof(CHARFORMAT2W);
HWND hWnd = GetDesktopWindow();
HDC hDC = GetDC(hWnd);
LONG yPixPerInch = GetDeviceCaps(hDC, LOGPIXELSY);
pCF->yHeight = -lf.lfHeight * LY_PER_INCH / yPixPerInch;
ReleaseDC(hWnd, hDC);
pCF->yOffset = 0;
pCF->crTextColor = 0;
pCF->dwEffects = CFM_EFFECTS | CFE_AUTOBACKCOLOR;
pCF->dwEffects &= ~(CFE_PROTECTED | CFE_LINK | CFE_AUTOCOLOR);
if (lf.lfWeight < FW_BOLD)
pCF->dwEffects &= ~CFE_BOLD;
if (!lf.lfItalic)
pCF->dwEffects &= ~CFE_ITALIC;
if (!lf.lfUnderline)
pCF->dwEffects &= ~CFE_UNDERLINE;
if (!lf.lfStrikeOut)
pCF->dwEffects &= ~CFE_STRIKEOUT;
pCF->dwMask = CFM_ALL | CFM_BACKCOLOR | CFM_STYLE;
pCF->bCharSet = lf.lfCharSet;
pCF->bPitchAndFamily = lf.lfPitchAndFamily;
mir_wstrcpy(pCF->szFaceName, lf.lfFaceName);
return S_OK;
}
HRESULT CFormattedTextDraw::InitDefaultCharFormat()
{
return CharFormatFromHFONT(m_pCF, NULL);
}
HRESULT CFormattedTextDraw::InitDefaultParaFormat()
{
memset(&m_PF, 0, sizeof(PARAFORMAT2));
m_PF.cbSize = sizeof(PARAFORMAT2);
m_PF.dwMask = PFM_ALL;
m_PF.wAlignment = PFA_LEFT;
m_PF.cTabCount = 1;
m_PF.rgxTabs[0] = lDefaultTab;
return S_OK;
}
HRESULT CFormattedTextDraw::CreateTextServicesObject()
{
IUnknown *spUnk;
HRESULT hr = MyCreateTextServices(NULL, static_cast<ITextHost*>(this), &spUnk);
if (hr == S_OK) {
hr = spUnk->QueryInterface(IID_ITextServices, (void**)&m_spTextServices);
hr = spUnk->QueryInterface(IID_ITextDocument, (void**)&m_spTextDocument);
spUnk->Release();
}
return hr;
}
|