-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathno5stream.cpp
More file actions
519 lines (470 loc) · 11 KB
/
Copy pathno5stream.cpp
File metadata and controls
519 lines (470 loc) · 11 KB
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
// no5stream.cpp: implementation of the no5stream class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include <no5tl\colorfader.h>
#include <yahoo\smileymap.h>
#include <yahoo\myutf8.h>
#include <no5tl\mystring.h>
#include "no5stream.h"
#include "no5app.h"
using namespace NO5TL;
using namespace NO5YAHOO;
typedef CSimpleArray<CYahooColor> ColorArray;
CString MakeUnicodeHtmlString(LPCWSTR p);
/******************* CInputStream ******************/
bool CInputStream::IsSmiley(LPCTSTR p,CString &smiley)
{
ATLASSERT(g_app.GetSmileyMap()->IsLoaded());
return g_app.GetSmileyMap()->IsSmiley(p,smiley) ? true : false;
}
/******************* CRichEditStream ******************/
// we well handle alt/fade here
void CRichEditStream::SendToDest()
{
const int count = m_atoms.GetSize();
register int i;
ColorArray colors;
int it = 0;
CTextAtom atom;
TCHAR ch[5] = {0};
// estes valores nao tem a ver com this->m_fade e this->m_alt
// os quais sao usados em AddAtom
bool fade = false,alt = false;
m_pEdit->SetSelEnd();
//GetEdit().SendMessage(WM_SETREDRAW,(WPARAM)FALSE);
for(i=0;i<count;i++){
atom = m_atoms[i];
switch(atom.GetType()){
case ATOM_TEXT:
{
if(!atom.m_pText){
ATLASSERT(0);
break;
}
if(!fade && !alt){
GetEdit().AppendText(*atom.m_pText);
}
else if(fade || alt){
const int len = atom.m_pText->GetLength();
for(int j=0;j<len;j++){
GetEdit().SetTextColor(colors[it++]);
if(it == colors.GetSize())
it = 0;
wsprintf(ch,"%c",atom.m_pText->GetAt(j));
GetEdit().AppendText(ch);
}
}
break;
}
case ATOM_CHAR:
wsprintf(ch,"%c",atom.m_ch);
if(fade || alt){
GetEdit().SetTextColor(colors[it++]);
if(it == colors.GetSize())
it = 0;
}
GetEdit().AppendText(ch);
break;
case ATOM_BREAK:
{
GetEdit().AppendText("\r\n");
}
break;
case ATOM_FADE:
{
CColorFader<CYahooColor> fader;
colors.RemoveAll();
if(atom.m_bSet){
int n = GetFadeLen(i);
if(!atom.m_pColors){
ATLASSERT(0);
break;
}
fader.Fade(*atom.m_pColors,colors,n,m_FadeFactor);
it = 0;
fade = true;
}
else{
// retore default color
GetEdit().SetTextColor(m_FontColor);
fade = false;
}
break;
}
case ATOM_ALT:
{
colors.RemoveAll();
if(atom.m_bSet){
if(!atom.m_pColors){
ATLASSERT(0);
break;
}
NO5TL::CopySimpleArray(colors,*atom.m_pColors);
it = 0;
alt = true;
}
else{
// retore default color
GetEdit().SetTextColor(m_FontColor);
alt = false;
}
break;
}
case ATOM_BOLD:
GetEdit().SetBold(atom.m_bSet);
break;
case ATOM_ITALIC:
GetEdit().SetItalic(atom.m_bSet);
break;
case ATOM_UNDER:
GetEdit().SetUnderline(atom.m_bSet);
break;
case ATOM_TATTOO:
// fall
case ATOM_FONT:
if(!atom.m_bSet){
// restore default stream font
GetEdit().SetTextFontName(m_FontFace);
GetEdit().SetTextHeight(m_FontSize);
}
else{
if(!atom.m_pFont){
ATLASSERT(0);
break;
}
if(!atom.m_pFont->HasInfo()){
if(atom.m_pFont->HasFace()){
GetEdit().SetTextFontName(atom.m_pFont->m_face);
}
if(atom.m_pFont->HasSize()){
int size = max(6,atom.m_pFont->m_size);
size = min(size,24);
GetEdit().SetTextHeight(size);
}
}
}
break;
case ATOM_COLOR:
if(!(fade || alt)){
GetEdit().SetTextColor(atom.m_color);
}
break;
case ATOM_BKCOLOR:
GetEdit().SetTextBkColor(atom.m_color);
break;
case ATOM_CHARSET:
GetEdit().SetCharset(atom.m_charset);
break;
case ATOM_SMILEY:
{
ISmileyMap *psm = g_app.GetSmileyMap();
CString path(g_app.GetSmileysFolder());
CString file;
if(!atom.m_pText){
ATLASSERT(0);
break;
}
if(psm->FindSmiley(*atom.m_pText,file)){
BOOL res;
file.Replace(".gif",".bmp");
path += file;
res = GetEdit().PasteBitmapFromFile(path,LR_SHARED|LR_LOADTRANSPARENT);
if(!res){
GetEdit().AppendText(*atom.m_pText);
}
}
else{
GetEdit().AppendText(*atom.m_pText);
}
}
break;
case ATOM_LINK:
GetEdit().SetLink(atom.m_bSet);
if(atom.m_bSet && atom.m_pText)
GetEdit().AppendText(*atom.m_pText);
break;
default:
break;
} // end of switch
} // end of loop for
//GetEdit().SendMessage(WM_SETREDRAW,(WPARAM)TRUE);
//GetEdit().InvalidateRect(NULL);
}
/******** CHtmlEditStream ***************/
CString CHtmlEditStream::GetFontTag(LPCTSTR name,int size)
{
CString res;
if(size < 0)
size = 1;
if(name && size > 0)
res.Format("<font style=\"font-size: %dpt;font-family: '%s'\">",
size,name);
else if(name)
res.Format("<font style=\"font-family: '%s'\">",name);
else if(size > 0)
res.Format("<font style=\"font-size: %dpt\">",size);
else
res = "</font>";
return res;
}
CString CHtmlEditStream::GetImgTag(LPCTSTR file)
{
CString s;
s.Format("<img src=\"%s\">",file);
return s;
}
CString CHtmlEditStream::GetLinkTag(BOOL bOn,LPCTSTR url,LPCTSTR text,LPCTSTR tip)
{
CString s;
if(bOn){
LPCTSTR _tip = tip ? tip : url;
LPCTSTR _text = text ? text : url;
// the "href" property is modified when we call IHTMLElement::GetAttribute
// for example, if it doesnt contain "http://"
// the A tag requires an href or a name property
s.Format("<a href=\"%s\" name=\"%s\" title=\"%s\" id=\"idlink\">%s",url,url,_tip,_text);
}
else
s = "</a>";
return s;
}
// we well handle alt/fade here
void CHtmlEditStream::SendToDest()
{
CString res;
GetHtml(res);
if(!res.IsEmpty()){
HRESULT hr = GetEdit().AppendHtmlText(res);
ATLASSERT(SUCCEEDED(hr));
}
}
void CHtmlEditStream::GetHtml(CString &res)
{
const int count = m_atoms.GetSize();
register int i;
ColorArray colors;
int it = 0;
CTextAtom atom;
TCHAR ch[10] = {0};
// estes valores nao tem a ver com this->m_fade e this->m_alt
// os quais sao usados em AddAtom
bool fade = false,alt = false;
CString tmp;
bool debug = false;
for(i=0;i<count;i++){
atom = m_atoms[i];
switch(atom.GetType()){
case ATOM_TEXT:
{
if(!atom.m_pText){
ATLASSERT(0);
break;
}
if(! ( fade || alt )){
tmp.Empty();
CString &s = *(atom.m_pText);
if(utf_isValidString(s,s.GetLength())){
LPWSTR pw = NO5TL::UTF8toUNICODE(s);
if(pw){
tmp += MakeUnicodeHtmlString(pw);
delete []pw;
}
else{
tmp += s;
}
}
else
tmp += (s);
tmp.Replace("<","<");
tmp.Replace(">",">");
res += tmp;
}
else if(fade || alt){
const int len = atom.m_pText->GetLength();
for(int j=0;j<len;j++){
res += GetColorTag(colors[it++]);
if(it == colors.GetSize())
it = 0;
if(atom.m_pText->GetAt(j) == '<')
lstrcpy(ch,"<");
else if(atom.m_pText->GetAt(j) == '>')
lstrcpy(ch,">");
else
wsprintf(ch,"%c",atom.m_pText->GetAt(j));
res += ch;
}
}
break;
}
case ATOM_CHAR:
if(atom.m_ch == '<')
lstrcpy(ch,"<");
else if(atom.m_ch == '>')
lstrcpy(ch,">");
else
wsprintf(ch,"%c",atom.m_ch);
if(fade || alt){
res += GetColorTag(colors[it++]);
if(it == colors.GetSize())
it = 0;
}
res += ch;
break;
case ATOM_BREAK:
res += "<br>";
break;
case ATOM_FADE:
{
CColorFader<CYahooColor> fader;
colors.RemoveAll();
if(atom.m_bSet){
int n = GetFadeLen(i);
if(!atom.m_pColors){
ATLASSERT(0);
break;
}
fader.Fade(*atom.m_pColors,colors,n,m_FadeFactor);
it = 0;
fade = true;
}
else{
// retore default color
res += GetColorTag(m_FontColor);
fade = false;
}
break;
}
case ATOM_ALT:
{
colors.RemoveAll();
if(atom.m_bSet){
if(!atom.m_pColors){
ATLASSERT(0);
break;
}
NO5TL::CopySimpleArray(colors,*atom.m_pColors);
it = 0;
alt = true;
}
else{
// retore default color
res += GetColorTag(m_FontColor);
alt = false;
}
break;
}
case ATOM_BOLD:
res += GetBoldTag(atom.m_bSet);
break;
case ATOM_ITALIC:
res += GetItalicTag(atom.m_bSet);
break;
case ATOM_UNDER:
res += GetUnderlineTag(atom.m_bSet);
break;
case ATOM_TATTOO:
// fall
case ATOM_FONT:
if(!atom.m_bSet){
// restore default stream font
res += GetFontTag(m_FontFace,m_FontSize);
}
else{
int size = 0;
if(!atom.m_pFont){
ATLASSERT(0);
break;
}
if(!atom.m_pFont->HasInfo()){
if(atom.m_pFont->HasSize())
size = min(24,max(6,atom.m_pFont->m_size));
res += GetFontTag(atom.m_pFont->m_face,
size);
}
}
break;
case ATOM_COLOR:
if(! ( fade || alt )){
res += GetColorTag(atom.m_color);
}
break;
case ATOM_SMILEY:
{
ISmileyMap *psm = g_app.GetSmileyMap();
CString path(g_app.GetSmileysFolder());
CString file;
if(!atom.m_pText){
ATLASSERT(0);
break;
}
if(psm->FindSmiley(*atom.m_pText,file)){
path += file;
res += GetImgTag(path);
}
else{
res += *atom.m_pText;
}
}
break;
case ATOM_LINK:
{
if(atom.m_bSet && atom.m_pText)
res += GetLinkTag(TRUE,*atom.m_pText,NULL,NULL);
else if(atom.m_bSet)
res += GetLinkTag(TRUE,"","",""); // not used so far
else
res += GetLinkTag(FALSE);
}
break;
case ATOM_HTML:
{
if(atom.m_pText)
res += *atom.m_pText;
}
break;
default:
break;
} // end of switch
} // end of loop for
}
/******** CEditCtlStream ********/
void CEditCtlStream::SendToDest(void)
{
ATLASSERT(m_edit);
m_edit.AppendText(GetPlainText2());
}
void CEditCtlStream::ScrollText(void)
{
SCROLLINFO si = { sizeof(SCROLLINFO) };
int nPos=0;
si.fMask = SIF_RANGE|SIF_PAGE;
::GetScrollInfo(m_edit,SB_VERT,&si);
si.fMask = SIF_POS;
si.nPos = si.nMax;
::SetScrollInfo(m_edit,SB_VERT,&si,TRUE);
if(si.nPage){
nPos = si.nMax - si.nPage;
m_edit.SendMessage(WM_VSCROLL,MAKEWPARAM(SB_THUMBTRACK,nPos),(LPARAM)\
NULL);
}
}
CString MakeUnicodeHtmlString(LPCWSTR p)
{
int len = wcslen(p);
int i;
char buf[10] = {0};
CString res;
for(i=0;i<len;i++){
ZeroMemory(buf,sizeof(buf)/sizeof(buf[0]));
if(p[i] > (unsigned short)127){
wsprintf(buf,"&#x%x;",(int)p[i]);
res += buf;
}
else{
wsprintf(buf,"%c",(char)p[i]);
res += buf;
}
}
return res;
}