[RICHED20] Sync with Wine Staging 1.9.4. CORE-10912
[reactos.git] / reactos / dll / win32 / riched20 / editor.c
1 /*
2 * RichEdit - functions dealing with editor object
3 *
4 * Copyright 2004 by Krzysztof Foltman
5 * Copyright 2005 by Cihan Altinay
6 * Copyright 2005 by Phil Krylov
7 * Copyright 2008 Eric Pouech
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24 /*
25 API implementation status:
26
27 Messages (ANSI versions not done yet)
28 + EM_AUTOURLDETECT 2.0
29 + EM_CANPASTE
30 + EM_CANREDO 2.0
31 + EM_CANUNDO
32 + EM_CHARFROMPOS
33 - EM_DISPLAYBAND
34 + EM_EMPTYUNDOBUFFER
35 + EM_EXGETSEL
36 + EM_EXLIMITTEXT
37 + EM_EXLINEFROMCHAR
38 + EM_EXSETSEL
39 + EM_FINDTEXT (only FR_DOWN flag implemented)
40 + EM_FINDTEXTEX (only FR_DOWN flag implemented)
41 - EM_FINDWORDBREAK
42 - EM_FMTLINES
43 - EM_FORMATRANGE
44 + EM_GETAUTOURLDETECT 2.0
45 - EM_GETBIDIOPTIONS 3.0
46 - EM_GETCHARFORMAT (partly done)
47 - EM_GETEDITSTYLE
48 + EM_GETEVENTMASK
49 + EM_GETFIRSTVISIBLELINE (can be optimized if needed)
50 - EM_GETIMECOLOR 1.0asian
51 - EM_GETIMECOMPMODE 2.0
52 - EM_GETIMEOPTIONS 1.0asian
53 - EM_GETIMESTATUS
54 - EM_GETLANGOPTIONS 2.0
55 + EM_GETLIMITTEXT
56 + EM_GETLINE
57 + EM_GETLINECOUNT returns number of rows, not of paragraphs
58 + EM_GETMODIFY
59 + EM_GETOLEINTERFACE
60 + EM_GETOPTIONS
61 + EM_GETPARAFORMAT
62 + EM_GETPASSWORDCHAR 2.0
63 - EM_GETPUNCTUATION 1.0asian
64 + EM_GETRECT
65 - EM_GETREDONAME 2.0
66 + EM_GETSEL
67 + EM_GETSELTEXT (ANSI&Unicode)
68 + EM_GETSCROLLPOS 3.0
69 ! - EM_GETTHUMB
70 + EM_GETTEXTEX 2.0
71 + EM_GETTEXTLENGTHEX (GTL_PRECISE unimplemented)
72 + EM_GETTEXTMODE 2.0
73 ? + EM_GETTEXTRANGE (ANSI&Unicode)
74 - EM_GETTYPOGRAPHYOPTIONS 3.0
75 - EM_GETUNDONAME
76 + EM_GETWORDBREAKPROC
77 - EM_GETWORDBREAKPROCEX
78 - EM_GETWORDWRAPMODE 1.0asian
79 + EM_GETZOOM 3.0
80 + EM_HIDESELECTION
81 + EM_LIMITTEXT (Also called EM_SETLIMITTEXT)
82 + EM_LINEFROMCHAR
83 + EM_LINEINDEX
84 + EM_LINELENGTH
85 + EM_LINESCROLL
86 - EM_PASTESPECIAL
87 + EM_POSFROMCHAR
88 + EM_REDO 2.0
89 + EM_REQUESTRESIZE
90 + EM_REPLACESEL (proper style?) ANSI&Unicode
91 + EM_SCROLL
92 + EM_SCROLLCARET
93 - EM_SELECTIONTYPE
94 - EM_SETBIDIOPTIONS 3.0
95 + EM_SETBKGNDCOLOR
96 + EM_SETCHARFORMAT (partly done, no ANSI)
97 - EM_SETEDITSTYLE
98 + EM_SETEVENTMASK (few notifications supported)
99 + EM_SETFONTSIZE
100 - EM_SETIMECOLOR 1.0asian
101 - EM_SETIMEOPTIONS 1.0asian
102 - EM_SETIMESTATUS
103 - EM_SETLANGOPTIONS 2.0
104 - EM_SETLIMITTEXT
105 - EM_SETMARGINS
106 + EM_SETMODIFY (not sure if implementation is correct)
107 - EM_SETOLECALLBACK
108 + EM_SETOPTIONS (partially implemented)
109 - EM_SETPALETTE 2.0
110 + EM_SETPARAFORMAT
111 + EM_SETPASSWORDCHAR 2.0
112 - EM_SETPUNCTUATION 1.0asian
113 + EM_SETREADONLY no beep on modification attempt
114 + EM_SETRECT
115 + EM_SETRECTNP (EM_SETRECT without repainting)
116 + EM_SETSEL
117 + EM_SETSCROLLPOS 3.0
118 - EM_SETTABSTOPS 3.0
119 - EM_SETTARGETDEVICE (partial)
120 + EM_SETTEXTEX 3.0 (proper style?)
121 - EM_SETTEXTMODE 2.0
122 - EM_SETTYPOGRAPHYOPTIONS 3.0
123 + EM_SETUNDOLIMIT 2.0
124 + EM_SETWORDBREAKPROC (used only for word movement at the moment)
125 - EM_SETWORDBREAKPROCEX
126 - EM_SETWORDWRAPMODE 1.0asian
127 + EM_SETZOOM 3.0
128 + EM_SHOWSCROLLBAR 2.0
129 + EM_STOPGROUPTYPING 2.0
130 + EM_STREAMIN
131 + EM_STREAMOUT
132 + EM_UNDO
133 + WM_CHAR
134 + WM_CLEAR
135 + WM_COPY
136 + WM_CUT
137 + WM_GETDLGCODE (the current implementation is incomplete)
138 + WM_GETTEXT (ANSI&Unicode)
139 + WM_GETTEXTLENGTH (ANSI version sucks)
140 + WM_HSCROLL
141 + WM_PASTE
142 + WM_SETFONT
143 + WM_SETTEXT (resets undo stack !) (proper style?) ANSI&Unicode
144 + WM_STYLECHANGING (seems to do nothing)
145 + WM_STYLECHANGED (seems to do nothing)
146 + WM_UNICHAR
147 + WM_VSCROLL
148
149 Notifications
150
151 * EN_CHANGE (sent from the wrong place)
152 - EN_CORRECTTEXT
153 - EN_DROPFILES
154 - EN_ERRSPACE
155 - EN_HSCROLL
156 - EN_IMECHANGE
157 + EN_KILLFOCUS
158 - EN_LINK
159 - EN_MAXTEXT
160 - EN_MSGFILTER
161 - EN_OLEOPFAILED
162 - EN_PROTECTED
163 + EN_REQUESTRESIZE
164 - EN_SAVECLIPBOARD
165 + EN_SELCHANGE
166 + EN_SETFOCUS
167 - EN_STOPNOUNDO
168 * EN_UPDATE (sent from the wrong place)
169 - EN_VSCROLL
170
171 Styles
172
173 - ES_AUTOHSCROLL
174 - ES_AUTOVSCROLL
175 + ES_CENTER
176 + ES_DISABLENOSCROLL (scrollbar is always visible)
177 - ES_EX_NOCALLOLEINIT
178 + ES_LEFT
179 - ES_MULTILINE (currently single line controls aren't supported)
180 - ES_NOIME
181 - ES_READONLY (I'm not sure if beeping is the proper behaviour)
182 + ES_RIGHT
183 - ES_SAVESEL
184 - ES_SELFIME
185 - ES_SUNKEN
186 - ES_VERTICAL
187 - ES_WANTRETURN (don't know how to do WM_GETDLGCODE part)
188 - WS_SETFONT
189 + WS_HSCROLL
190 + WS_VSCROLL
191 */
192
193 /*
194 * RICHED20 TODO (incomplete):
195 *
196 * - messages/styles/notifications listed above
197 * - add remaining CHARFORMAT/PARAFORMAT fields
198 * - right/center align should strip spaces from the beginning
199 * - pictures/OLE objects (not just smiling faces that lack API support ;-) )
200 * - COM interface (looks like a major pain in the TODO list)
201 * - calculate heights of pictures (half-done)
202 * - hysteresis during wrapping (related to scrollbars appearing/disappearing)
203 * - find/replace
204 * - how to implement EM_FORMATRANGE and EM_DISPLAYBAND ? (Mission Impossible)
205 * - italic caret with italic fonts
206 * - IME
207 * - most notifications aren't sent at all (the most important ones are)
208 * - when should EN_SELCHANGE be sent after text change ? (before/after EN_UPDATE?)
209 * - WM_SETTEXT may use wrong style (but I'm 80% sure it's OK)
210 * - EM_GETCHARFORMAT with SCF_SELECTION may not behave 100% like in original (but very close)
211 * - full justification
212 * - hyphenation
213 * - tables
214 * - ListBox & ComboBox not implemented
215 *
216 * Bugs that are probably fixed, but not so easy to verify:
217 * - EN_UPDATE/EN_CHANGE are handled very incorrectly (should be OK now)
218 * - undo for ME_JoinParagraphs doesn't store paragraph format ? (it does)
219 * - check/fix artificial EOL logic (bCursorAtEnd, hardly logical)
220 * - caret shouldn't be displayed when selection isn't empty
221 * - check refcounting in style management functions (looks perfect now, but no bugs is suspicious)
222 * - undo for setting default format (done, might be buggy)
223 * - styles might be not released properly (looks like they work like charm, but who knows?
224 *
225 */
226
227 #include "editor.h"
228
229 #include <commdlg.h>
230 #include <undocuser.h>
231
232 #include "rtf.h"
233 #include "res.h"
234
235 #define STACK_SIZE_DEFAULT 100
236 #define STACK_SIZE_MAX 1000
237
238 #define TEXT_LIMIT_DEFAULT 32767
239
240 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
241
242 static BOOL ME_RegisterEditorClass(HINSTANCE);
243 static BOOL ME_UpdateLinkAttribute(ME_TextEditor *editor, ME_Cursor *start, int nChars);
244
245 static const WCHAR REListBox20W[] = {'R','E','L','i','s','t','B','o','x','2','0','W', 0};
246 static const WCHAR REComboBox20W[] = {'R','E','C','o','m','b','o','B','o','x','2','0','W', 0};
247 static HCURSOR hLeft;
248
249 BOOL me_debug = FALSE;
250 HANDLE me_heap = NULL;
251
252 static BOOL ME_ListBoxRegistered = FALSE;
253 static BOOL ME_ComboBoxRegistered = FALSE;
254
255 static inline BOOL is_version_nt(void)
256 {
257 return !(GetVersion() & 0x80000000);
258 }
259
260 static ME_TextBuffer *ME_MakeText(void) {
261
262 ME_TextBuffer *buf = ALLOC_OBJ(ME_TextBuffer);
263
264 ME_DisplayItem *p1 = ME_MakeDI(diTextStart);
265 ME_DisplayItem *p2 = ME_MakeDI(diTextEnd);
266
267 p1->prev = NULL;
268 p1->next = p2;
269 p2->prev = p1;
270 p2->next = NULL;
271 p1->member.para.next_para = p2;
272 p2->member.para.prev_para = p1;
273 p2->member.para.nCharOfs = 0;
274
275 buf->pFirst = p1;
276 buf->pLast = p2;
277 buf->pCharStyle = NULL;
278
279 return buf;
280 }
281
282
283 static LRESULT ME_StreamInText(ME_TextEditor *editor, DWORD dwFormat, ME_InStream *stream, ME_Style *style)
284 {
285 WCHAR *pText;
286 LRESULT total_bytes_read = 0;
287 BOOL is_read = FALSE;
288 DWORD cp = CP_ACP, copy = 0;
289 char conv_buf[4 + STREAMIN_BUFFER_SIZE]; /* up to 4 additional UTF-8 bytes */
290
291 static const char bom_utf8[] = {0xEF, 0xBB, 0xBF};
292
293 TRACE("%08x %p\n", dwFormat, stream);
294
295 do {
296 LONG nWideChars = 0;
297 WCHAR wszText[STREAMIN_BUFFER_SIZE+1];
298
299 if (!stream->dwSize)
300 {
301 ME_StreamInFill(stream);
302 if (stream->editstream->dwError)
303 break;
304 if (!stream->dwSize)
305 break;
306 total_bytes_read += stream->dwSize;
307 }
308
309 if (!(dwFormat & SF_UNICODE))
310 {
311 char * buf = stream->buffer;
312 DWORD size = stream->dwSize, end;
313
314 if (!is_read)
315 {
316 is_read = TRUE;
317 if (stream->dwSize >= 3 && !memcmp(stream->buffer, bom_utf8, 3))
318 {
319 cp = CP_UTF8;
320 buf += 3;
321 size -= 3;
322 }
323 }
324
325 if (cp == CP_UTF8)
326 {
327 if (copy)
328 {
329 memcpy(conv_buf + copy, buf, size);
330 buf = conv_buf;
331 size += copy;
332 }
333 end = size;
334 while ((buf[end-1] & 0xC0) == 0x80)
335 {
336 --end;
337 --total_bytes_read; /* strange, but seems to match windows */
338 }
339 if (buf[end-1] & 0x80)
340 {
341 DWORD need = 0;
342 if ((buf[end-1] & 0xE0) == 0xC0)
343 need = 1;
344 if ((buf[end-1] & 0xF0) == 0xE0)
345 need = 2;
346 if ((buf[end-1] & 0xF8) == 0xF0)
347 need = 3;
348
349 if (size - end >= need)
350 {
351 /* we have enough bytes for this sequence */
352 end = size;
353 }
354 else
355 {
356 /* need more bytes, so don't transcode this sequence */
357 --end;
358 }
359 }
360 }
361 else
362 end = size;
363
364 nWideChars = MultiByteToWideChar(cp, 0, buf, end, wszText, STREAMIN_BUFFER_SIZE);
365 pText = wszText;
366
367 if (cp == CP_UTF8)
368 {
369 if (end != size)
370 {
371 memcpy(conv_buf, buf + end, size - end);
372 copy = size - end;
373 }
374 }
375 }
376 else
377 {
378 nWideChars = stream->dwSize >> 1;
379 pText = (WCHAR *)stream->buffer;
380 }
381
382 ME_InsertTextFromCursor(editor, 0, pText, nWideChars, style);
383 if (stream->dwSize == 0)
384 break;
385 stream->dwSize = 0;
386 } while(1);
387 return total_bytes_read;
388 }
389
390 static void ME_ApplyBorderProperties(RTF_Info *info,
391 ME_BorderRect *borderRect,
392 RTFBorder *borderDef)
393 {
394 int i, colorNum;
395 ME_Border *pBorders[] = {&borderRect->top,
396 &borderRect->left,
397 &borderRect->bottom,
398 &borderRect->right};
399 for (i = 0; i < 4; i++)
400 {
401 RTFColor *colorDef = info->colorList;
402 pBorders[i]->width = borderDef[i].width;
403 colorNum = borderDef[i].color;
404 while (colorDef && colorDef->rtfCNum != colorNum)
405 colorDef = colorDef->rtfNextColor;
406 if (colorDef)
407 pBorders[i]->colorRef = RGB(
408 colorDef->rtfCRed >= 0 ? colorDef->rtfCRed : 0,
409 colorDef->rtfCGreen >= 0 ? colorDef->rtfCGreen : 0,
410 colorDef->rtfCBlue >= 0 ? colorDef->rtfCBlue : 0);
411 else
412 pBorders[i]->colorRef = RGB(0, 0, 0);
413 }
414 }
415
416 void ME_RTFCharAttrHook(RTF_Info *info)
417 {
418 CHARFORMAT2W fmt;
419 fmt.cbSize = sizeof(fmt);
420 fmt.dwMask = 0;
421 fmt.dwEffects = 0;
422
423 switch(info->rtfMinor)
424 {
425 case rtfPlain:
426 /* FIXME add more flags once they're implemented */
427 fmt.dwMask = CFM_BOLD | CFM_ITALIC | CFM_UNDERLINETYPE | CFM_STRIKEOUT | CFM_COLOR | CFM_BACKCOLOR | CFM_SIZE | CFM_WEIGHT;
428 fmt.dwEffects = CFE_AUTOCOLOR | CFE_AUTOBACKCOLOR;
429 fmt.yHeight = 12*20; /* 12pt */
430 fmt.wWeight = FW_NORMAL;
431 fmt.bUnderlineType = CFU_UNDERLINENONE;
432 break;
433 case rtfBold:
434 fmt.dwMask = CFM_BOLD | CFM_WEIGHT;
435 fmt.dwEffects = info->rtfParam ? CFE_BOLD : 0;
436 fmt.wWeight = info->rtfParam ? FW_BOLD : FW_NORMAL;
437 break;
438 case rtfItalic:
439 fmt.dwMask = CFM_ITALIC;
440 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
441 break;
442 case rtfUnderline:
443 fmt.dwMask = CFM_UNDERLINETYPE;
444 fmt.bUnderlineType = info->rtfParam ? CFU_CF1UNDERLINE : CFU_UNDERLINENONE;
445 break;
446 case rtfDotUnderline:
447 fmt.dwMask = CFM_UNDERLINETYPE;
448 fmt.bUnderlineType = info->rtfParam ? CFU_UNDERLINEDOTTED : CFU_UNDERLINENONE;
449 break;
450 case rtfDbUnderline:
451 fmt.dwMask = CFM_UNDERLINETYPE;
452 fmt.bUnderlineType = info->rtfParam ? CFU_UNDERLINEDOUBLE : CFU_UNDERLINENONE;
453 break;
454 case rtfWordUnderline:
455 fmt.dwMask = CFM_UNDERLINETYPE;
456 fmt.bUnderlineType = info->rtfParam ? CFU_UNDERLINEWORD : CFU_UNDERLINENONE;
457 break;
458 case rtfNoUnderline:
459 fmt.dwMask = CFM_UNDERLINETYPE;
460 fmt.bUnderlineType = CFU_UNDERLINENONE;
461 break;
462 case rtfStrikeThru:
463 fmt.dwMask = CFM_STRIKEOUT;
464 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
465 break;
466 case rtfSubScript:
467 case rtfSuperScript:
468 case rtfSubScrShrink:
469 case rtfSuperScrShrink:
470 case rtfNoSuperSub:
471 fmt.dwMask = CFM_SUBSCRIPT|CFM_SUPERSCRIPT;
472 if (info->rtfMinor == rtfSubScrShrink) fmt.dwEffects = CFE_SUBSCRIPT;
473 if (info->rtfMinor == rtfSuperScrShrink) fmt.dwEffects = CFE_SUPERSCRIPT;
474 if (info->rtfMinor == rtfNoSuperSub) fmt.dwEffects = 0;
475 break;
476 case rtfInvisible:
477 fmt.dwMask = CFM_HIDDEN;
478 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
479 break;
480 case rtfBackColor:
481 fmt.dwMask = CFM_BACKCOLOR;
482 fmt.dwEffects = 0;
483 if (info->rtfParam == 0)
484 fmt.dwEffects = CFE_AUTOBACKCOLOR;
485 else if (info->rtfParam != rtfNoParam)
486 {
487 RTFColor *c = RTFGetColor(info, info->rtfParam);
488 if (c && c->rtfCBlue >= 0)
489 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
490 else
491 fmt.dwEffects = CFE_AUTOBACKCOLOR;
492 }
493 break;
494 case rtfForeColor:
495 fmt.dwMask = CFM_COLOR;
496 fmt.dwEffects = 0;
497 if (info->rtfParam == 0)
498 fmt.dwEffects = CFE_AUTOCOLOR;
499 else if (info->rtfParam != rtfNoParam)
500 {
501 RTFColor *c = RTFGetColor(info, info->rtfParam);
502 if (c && c->rtfCBlue >= 0)
503 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
504 else {
505 fmt.dwEffects = CFE_AUTOCOLOR;
506 }
507 }
508 break;
509 case rtfFontNum:
510 if (info->rtfParam != rtfNoParam)
511 {
512 RTFFont *f = RTFGetFont(info, info->rtfParam);
513 if (f)
514 {
515 MultiByteToWideChar(CP_ACP, 0, f->rtfFName, -1, fmt.szFaceName, sizeof(fmt.szFaceName)/sizeof(WCHAR));
516 fmt.szFaceName[sizeof(fmt.szFaceName)/sizeof(WCHAR)-1] = '\0';
517 fmt.bCharSet = f->rtfFCharSet;
518 fmt.dwMask = CFM_FACE | CFM_CHARSET;
519 fmt.bPitchAndFamily = f->rtfFPitch | (f->rtfFFamily << 4);
520 }
521 }
522 break;
523 case rtfFontSize:
524 fmt.dwMask = CFM_SIZE;
525 if (info->rtfParam != rtfNoParam)
526 fmt.yHeight = info->rtfParam*10;
527 break;
528 }
529 if (fmt.dwMask) {
530 ME_Style *style2;
531 RTFFlushOutputBuffer(info);
532 /* FIXME too slow ? how come ? */
533 style2 = ME_ApplyStyle(info->editor, info->style, &fmt);
534 ME_ReleaseStyle(info->style);
535 info->style = style2;
536 info->styleChanged = TRUE;
537 }
538 }
539
540 /* FIXME this function doesn't get any information about context of the RTF tag, which is very bad,
541 the same tags mean different things in different contexts */
542 void ME_RTFParAttrHook(RTF_Info *info)
543 {
544 PARAFORMAT2 fmt;
545 fmt.cbSize = sizeof(fmt);
546 fmt.dwMask = 0;
547 fmt.wEffects = 0;
548
549 switch(info->rtfMinor)
550 {
551 case rtfParDef: /* restores default paragraph attributes */
552 if (!info->editor->bEmulateVersion10) /* v4.1 */
553 info->borderType = RTFBorderParaLeft;
554 else /* v1.0 - 3.0 */
555 info->borderType = RTFBorderParaTop;
556 fmt.dwMask = PFM_ALIGNMENT | PFM_BORDER | PFM_LINESPACING | PFM_TABSTOPS |
557 PFM_OFFSET | PFM_RIGHTINDENT | PFM_SPACEAFTER | PFM_SPACEBEFORE |
558 PFM_STARTINDENT;
559 /* TODO: numbering, shading */
560 fmt.wAlignment = PFA_LEFT;
561 fmt.cTabCount = 0;
562 fmt.dxOffset = fmt.dxStartIndent = fmt.dxRightIndent = 0;
563 fmt.wBorderWidth = fmt.wBorders = 0;
564 fmt.wBorderSpace = 0;
565 fmt.bLineSpacingRule = 0;
566 fmt.dySpaceBefore = fmt.dySpaceAfter = 0;
567 fmt.dyLineSpacing = 0;
568 if (!info->editor->bEmulateVersion10) /* v4.1 */
569 {
570 if (info->tableDef && info->tableDef->tableRowStart &&
571 info->tableDef->tableRowStart->member.para.nFlags & MEPF_ROWEND)
572 {
573 ME_Cursor cursor;
574 ME_DisplayItem *para;
575 /* We are just after a table row. */
576 RTFFlushOutputBuffer(info);
577 cursor = info->editor->pCursors[0];
578 para = cursor.pPara;
579 if (para == info->tableDef->tableRowStart->member.para.next_para
580 && !cursor.nOffset && !cursor.pRun->member.run.nCharOfs)
581 {
582 /* Since the table row end, no text has been inserted, and the \intbl
583 * control word has not be used. We can confirm that we are not in a
584 * table anymore.
585 */
586 info->tableDef->tableRowStart = NULL;
587 info->canInheritInTbl = FALSE;
588 }
589 }
590 } else { /* v1.0 - v3.0 */
591 fmt.dwMask |= PFM_TABLE;
592 fmt.wEffects &= ~PFE_TABLE;
593 }
594 break;
595 case rtfNestLevel:
596 if (!info->editor->bEmulateVersion10) /* v4.1 */
597 {
598 while (info->rtfParam > info->nestingLevel) {
599 RTFTable *tableDef = ALLOC_OBJ(RTFTable);
600 ZeroMemory(tableDef, sizeof(RTFTable));
601 tableDef->parent = info->tableDef;
602 info->tableDef = tableDef;
603
604 RTFFlushOutputBuffer(info);
605 if (tableDef->tableRowStart &&
606 tableDef->tableRowStart->member.para.nFlags & MEPF_ROWEND)
607 {
608 ME_DisplayItem *para = tableDef->tableRowStart;
609 para = para->member.para.next_para;
610 para = ME_InsertTableRowStartAtParagraph(info->editor, para);
611 tableDef->tableRowStart = para;
612 } else {
613 ME_Cursor cursor;
614 WCHAR endl = '\r';
615 cursor = info->editor->pCursors[0];
616 if (cursor.nOffset || cursor.pRun->member.run.nCharOfs)
617 ME_InsertTextFromCursor(info->editor, 0, &endl, 1, info->style);
618 tableDef->tableRowStart = ME_InsertTableRowStartFromCursor(info->editor);
619 }
620
621 info->nestingLevel++;
622 }
623 info->canInheritInTbl = FALSE;
624 }
625 break;
626 case rtfInTable:
627 {
628 if (!info->editor->bEmulateVersion10) /* v4.1 */
629 {
630 if (info->nestingLevel < 1)
631 {
632 RTFTable *tableDef;
633 if (!info->tableDef)
634 {
635 info->tableDef = ALLOC_OBJ(RTFTable);
636 ZeroMemory(info->tableDef, sizeof(RTFTable));
637 }
638 tableDef = info->tableDef;
639 RTFFlushOutputBuffer(info);
640 if (tableDef->tableRowStart &&
641 tableDef->tableRowStart->member.para.nFlags & MEPF_ROWEND)
642 {
643 ME_DisplayItem *para = tableDef->tableRowStart;
644 para = para->member.para.next_para;
645 para = ME_InsertTableRowStartAtParagraph(info->editor, para);
646 tableDef->tableRowStart = para;
647 } else {
648 ME_Cursor cursor;
649 WCHAR endl = '\r';
650 cursor = info->editor->pCursors[0];
651 if (cursor.nOffset || cursor.pRun->member.run.nCharOfs)
652 ME_InsertTextFromCursor(info->editor, 0, &endl, 1, info->style);
653 tableDef->tableRowStart = ME_InsertTableRowStartFromCursor(info->editor);
654 }
655 info->nestingLevel = 1;
656 info->canInheritInTbl = TRUE;
657 }
658 return;
659 } else { /* v1.0 - v3.0 */
660 fmt.dwMask |= PFM_TABLE;
661 fmt.wEffects |= PFE_TABLE;
662 }
663 break;
664 }
665 case rtfFirstIndent:
666 ME_GetSelectionParaFormat(info->editor, &fmt);
667 fmt.dwMask = PFM_STARTINDENT | PFM_OFFSET;
668 fmt.dxStartIndent += fmt.dxOffset + info->rtfParam;
669 fmt.dxOffset = -info->rtfParam;
670 break;
671 case rtfLeftIndent:
672 ME_GetSelectionParaFormat(info->editor, &fmt);
673 fmt.dwMask = PFM_STARTINDENT;
674 fmt.dxStartIndent = info->rtfParam - fmt.dxOffset;
675 break;
676 case rtfRightIndent:
677 fmt.dwMask = PFM_RIGHTINDENT;
678 fmt.dxRightIndent = info->rtfParam;
679 break;
680 case rtfQuadLeft:
681 case rtfQuadJust:
682 fmt.dwMask = PFM_ALIGNMENT;
683 fmt.wAlignment = PFA_LEFT;
684 break;
685 case rtfQuadRight:
686 fmt.dwMask = PFM_ALIGNMENT;
687 fmt.wAlignment = PFA_RIGHT;
688 break;
689 case rtfQuadCenter:
690 fmt.dwMask = PFM_ALIGNMENT;
691 fmt.wAlignment = PFA_CENTER;
692 break;
693 case rtfTabPos:
694 ME_GetSelectionParaFormat(info->editor, &fmt);
695 if (!(fmt.dwMask & PFM_TABSTOPS))
696 {
697 fmt.cTabCount = 0;
698 }
699 if (fmt.cTabCount < MAX_TAB_STOPS && info->rtfParam < 0x1000000)
700 fmt.rgxTabs[fmt.cTabCount++] = info->rtfParam;
701 fmt.dwMask = PFM_TABSTOPS;
702 break;
703 case rtfKeep:
704 fmt.dwMask = PFM_KEEP;
705 fmt.wEffects = PFE_KEEP;
706 break;
707 case rtfNoWidowControl:
708 fmt.dwMask = PFM_NOWIDOWCONTROL;
709 fmt.wEffects = PFE_NOWIDOWCONTROL;
710 break;
711 case rtfKeepNext:
712 fmt.dwMask = PFM_KEEPNEXT;
713 fmt.wEffects = PFE_KEEPNEXT;
714 break;
715 case rtfSpaceAfter:
716 fmt.dwMask = PFM_SPACEAFTER;
717 fmt.dySpaceAfter = info->rtfParam;
718 break;
719 case rtfSpaceBefore:
720 fmt.dwMask = PFM_SPACEBEFORE;
721 fmt.dySpaceBefore = info->rtfParam;
722 break;
723 case rtfSpaceBetween:
724 fmt.dwMask = PFM_LINESPACING;
725 if ((int)info->rtfParam > 0)
726 {
727 fmt.dyLineSpacing = info->rtfParam;
728 fmt.bLineSpacingRule = 3;
729 }
730 else
731 {
732 fmt.dyLineSpacing = info->rtfParam;
733 fmt.bLineSpacingRule = 4;
734 }
735 break;
736 case rtfSpaceMultiply:
737 fmt.dwMask = PFM_LINESPACING;
738 fmt.dyLineSpacing = info->rtfParam * 20;
739 fmt.bLineSpacingRule = 5;
740 break;
741 case rtfParBullet:
742 fmt.dwMask = PFM_NUMBERING;
743 fmt.wNumbering = PFN_BULLET;
744 break;
745 case rtfParSimple:
746 fmt.dwMask = PFM_NUMBERING;
747 fmt.wNumbering = 2; /* FIXME: MSDN says it's not used ?? */
748 break;
749 case rtfParNumDecimal:
750 fmt.dwMask = PFM_NUMBERING;
751 fmt.wNumbering = 2; /* FIXME: MSDN says it's not used ?? */
752 break;
753 case rtfParNumIndent:
754 fmt.dwMask = PFM_NUMBERINGTAB;
755 fmt.wNumberingTab = info->rtfParam;
756 break;
757 case rtfParNumStartAt:
758 fmt.dwMask = PFM_NUMBERINGSTART;
759 fmt.wNumberingStart = info->rtfParam;
760 break;
761 case rtfBorderLeft:
762 info->borderType = RTFBorderParaLeft;
763 ME_GetSelectionParaFormat(info->editor, &fmt);
764 if (!(fmt.dwMask & PFM_BORDER))
765 {
766 fmt.wBorderSpace = 0;
767 fmt.wBorderWidth = 1;
768 fmt.wBorders = 0;
769 }
770 fmt.wBorders |= 1;
771 fmt.dwMask = PFM_BORDER;
772 break;
773 case rtfBorderRight:
774 info->borderType = RTFBorderParaRight;
775 ME_GetSelectionParaFormat(info->editor, &fmt);
776 if (!(fmt.dwMask & PFM_BORDER))
777 {
778 fmt.wBorderSpace = 0;
779 fmt.wBorderWidth = 1;
780 fmt.wBorders = 0;
781 }
782 fmt.wBorders |= 2;
783 fmt.dwMask = PFM_BORDER;
784 break;
785 case rtfBorderTop:
786 info->borderType = RTFBorderParaTop;
787 ME_GetSelectionParaFormat(info->editor, &fmt);
788 if (!(fmt.dwMask & PFM_BORDER))
789 {
790 fmt.wBorderSpace = 0;
791 fmt.wBorderWidth = 1;
792 fmt.wBorders = 0;
793 }
794 fmt.wBorders |= 4;
795 fmt.dwMask = PFM_BORDER;
796 break;
797 case rtfBorderBottom:
798 info->borderType = RTFBorderParaBottom;
799 ME_GetSelectionParaFormat(info->editor, &fmt);
800 if (!(fmt.dwMask & PFM_BORDER))
801 {
802 fmt.wBorderSpace = 0;
803 fmt.wBorderWidth = 1;
804 fmt.wBorders = 0;
805 }
806 fmt.wBorders |= 8;
807 fmt.dwMask = PFM_BORDER;
808 break;
809 case rtfBorderSingle:
810 ME_GetSelectionParaFormat(info->editor, &fmt);
811 /* we assume that borders have been created before (RTF spec) */
812 fmt.wBorders &= ~0x700;
813 fmt.wBorders |= 1 << 8;
814 fmt.dwMask = PFM_BORDER;
815 break;
816 case rtfBorderThick:
817 ME_GetSelectionParaFormat(info->editor, &fmt);
818 /* we assume that borders have been created before (RTF spec) */
819 fmt.wBorders &= ~0x700;
820 fmt.wBorders |= 2 << 8;
821 fmt.dwMask = PFM_BORDER;
822 break;
823 case rtfBorderShadow:
824 ME_GetSelectionParaFormat(info->editor, &fmt);
825 /* we assume that borders have been created before (RTF spec) */
826 fmt.wBorders &= ~0x700;
827 fmt.wBorders |= 10 << 8;
828 fmt.dwMask = PFM_BORDER;
829 break;
830 case rtfBorderDouble:
831 ME_GetSelectionParaFormat(info->editor, &fmt);
832 /* we assume that borders have been created before (RTF spec) */
833 fmt.wBorders &= ~0x700;
834 fmt.wBorders |= 7 << 8;
835 fmt.dwMask = PFM_BORDER;
836 break;
837 case rtfBorderDot:
838 ME_GetSelectionParaFormat(info->editor, &fmt);
839 /* we assume that borders have been created before (RTF spec) */
840 fmt.wBorders &= ~0x700;
841 fmt.wBorders |= 11 << 8;
842 fmt.dwMask = PFM_BORDER;
843 break;
844 case rtfBorderWidth:
845 {
846 int borderSide = info->borderType & RTFBorderSideMask;
847 RTFTable *tableDef = info->tableDef;
848 ME_GetSelectionParaFormat(info->editor, &fmt);
849 /* we assume that borders have been created before (RTF spec) */
850 fmt.wBorderWidth |= ((info->rtfParam / 15) & 7) << 8;
851 if ((info->borderType & RTFBorderTypeMask) == RTFBorderTypeCell)
852 {
853 RTFBorder *border;
854 if (!tableDef || tableDef->numCellsDefined >= MAX_TABLE_CELLS)
855 break;
856 border = &tableDef->cells[tableDef->numCellsDefined].border[borderSide];
857 border->width = info->rtfParam;
858 break;
859 }
860 fmt.dwMask = PFM_BORDER;
861 break;
862 }
863 case rtfBorderSpace:
864 ME_GetSelectionParaFormat(info->editor, &fmt);
865 /* we assume that borders have been created before (RTF spec) */
866 fmt.wBorderSpace = info->rtfParam;
867 fmt.dwMask = PFM_BORDER;
868 break;
869 case rtfBorderColor:
870 {
871 RTFTable *tableDef = info->tableDef;
872 int borderSide = info->borderType & RTFBorderSideMask;
873 int borderType = info->borderType & RTFBorderTypeMask;
874 switch(borderType)
875 {
876 case RTFBorderTypePara:
877 if (!info->editor->bEmulateVersion10) /* v4.1 */
878 break;
879 /* v1.0 - 3.0 treat paragraph and row borders the same. */
880 case RTFBorderTypeRow:
881 if (tableDef) {
882 tableDef->border[borderSide].color = info->rtfParam;
883 }
884 break;
885 case RTFBorderTypeCell:
886 if (tableDef && tableDef->numCellsDefined < MAX_TABLE_CELLS) {
887 tableDef->cells[tableDef->numCellsDefined].border[borderSide].color = info->rtfParam;
888 }
889 break;
890 }
891 break;
892 }
893 case rtfRTLPar:
894 fmt.dwMask = PFM_RTLPARA;
895 fmt.wEffects = PFE_RTLPARA;
896 break;
897 case rtfLTRPar:
898 fmt.dwMask = PFM_RTLPARA;
899 fmt.wEffects = 0;
900 break;
901 }
902 if (fmt.dwMask) {
903 RTFFlushOutputBuffer(info);
904 /* FIXME too slow ? how come ?*/
905 ME_SetSelectionParaFormat(info->editor, &fmt);
906 }
907 }
908
909 void ME_RTFTblAttrHook(RTF_Info *info)
910 {
911 switch (info->rtfMinor)
912 {
913 case rtfRowDef:
914 {
915 if (!info->editor->bEmulateVersion10) /* v4.1 */
916 info->borderType = 0; /* Not sure */
917 else /* v1.0 - 3.0 */
918 info->borderType = RTFBorderRowTop;
919 if (!info->tableDef) {
920 info->tableDef = ME_MakeTableDef(info->editor);
921 } else {
922 ME_InitTableDef(info->editor, info->tableDef);
923 }
924 break;
925 }
926 case rtfCellPos:
927 {
928 int cellNum;
929 if (!info->tableDef)
930 {
931 info->tableDef = ME_MakeTableDef(info->editor);
932 }
933 cellNum = info->tableDef->numCellsDefined;
934 if (cellNum >= MAX_TABLE_CELLS)
935 break;
936 info->tableDef->cells[cellNum].rightBoundary = info->rtfParam;
937 if (cellNum < MAX_TAB_STOPS) {
938 /* Tab stops were used to store cell positions before v4.1 but v4.1
939 * still seems to set the tabstops without using them. */
940 ME_DisplayItem *para = info->editor->pCursors[0].pPara;
941 PARAFORMAT2 *pFmt = para->member.para.pFmt;
942 pFmt->rgxTabs[cellNum] &= ~0x00FFFFFF;
943 pFmt->rgxTabs[cellNum] |= 0x00FFFFFF & info->rtfParam;
944 }
945 info->tableDef->numCellsDefined++;
946 break;
947 }
948 case rtfRowBordTop:
949 info->borderType = RTFBorderRowTop;
950 break;
951 case rtfRowBordLeft:
952 info->borderType = RTFBorderRowLeft;
953 break;
954 case rtfRowBordBottom:
955 info->borderType = RTFBorderRowBottom;
956 break;
957 case rtfRowBordRight:
958 info->borderType = RTFBorderRowRight;
959 break;
960 case rtfCellBordTop:
961 info->borderType = RTFBorderCellTop;
962 break;
963 case rtfCellBordLeft:
964 info->borderType = RTFBorderCellLeft;
965 break;
966 case rtfCellBordBottom:
967 info->borderType = RTFBorderCellBottom;
968 break;
969 case rtfCellBordRight:
970 info->borderType = RTFBorderCellRight;
971 break;
972 case rtfRowGapH:
973 if (info->tableDef)
974 info->tableDef->gapH = info->rtfParam;
975 break;
976 case rtfRowLeftEdge:
977 if (info->tableDef)
978 info->tableDef->leftEdge = info->rtfParam;
979 break;
980 }
981 }
982
983 void ME_RTFSpecialCharHook(RTF_Info *info)
984 {
985 RTFTable *tableDef = info->tableDef;
986 switch (info->rtfMinor)
987 {
988 case rtfNestCell:
989 if (info->editor->bEmulateVersion10) /* v1.0 - v3.0 */
990 break;
991 /* else fall through since v4.1 treats rtfNestCell and rtfCell the same */
992 case rtfCell:
993 if (!tableDef)
994 break;
995 RTFFlushOutputBuffer(info);
996 if (!info->editor->bEmulateVersion10) { /* v4.1 */
997 if (tableDef->tableRowStart)
998 {
999 if (!info->nestingLevel &&
1000 tableDef->tableRowStart->member.para.nFlags & MEPF_ROWEND)
1001 {
1002 ME_DisplayItem *para = tableDef->tableRowStart;
1003 para = para->member.para.next_para;
1004 para = ME_InsertTableRowStartAtParagraph(info->editor, para);
1005 tableDef->tableRowStart = para;
1006 info->nestingLevel = 1;
1007 }
1008 ME_InsertTableCellFromCursor(info->editor);
1009 }
1010 } else { /* v1.0 - v3.0 */
1011 ME_DisplayItem *para = info->editor->pCursors[0].pPara;
1012 PARAFORMAT2 *pFmt = para->member.para.pFmt;
1013 if (pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE &&
1014 tableDef->numCellsInserted < tableDef->numCellsDefined)
1015 {
1016 WCHAR tab = '\t';
1017 ME_InsertTextFromCursor(info->editor, 0, &tab, 1, info->style);
1018 tableDef->numCellsInserted++;
1019 }
1020 }
1021 break;
1022 case rtfNestRow:
1023 if (info->editor->bEmulateVersion10) /* v1.0 - v3.0 */
1024 break;
1025 /* else fall through since v4.1 treats rtfNestRow and rtfRow the same */
1026 case rtfRow:
1027 {
1028 ME_DisplayItem *para, *cell, *run;
1029 int i;
1030
1031 if (!tableDef)
1032 break;
1033 RTFFlushOutputBuffer(info);
1034 if (!info->editor->bEmulateVersion10) { /* v4.1 */
1035 if (!tableDef->tableRowStart)
1036 break;
1037 if (!info->nestingLevel &&
1038 tableDef->tableRowStart->member.para.nFlags & MEPF_ROWEND)
1039 {
1040 para = tableDef->tableRowStart;
1041 para = para->member.para.next_para;
1042 para = ME_InsertTableRowStartAtParagraph(info->editor, para);
1043 tableDef->tableRowStart = para;
1044 info->nestingLevel++;
1045 }
1046 para = tableDef->tableRowStart;
1047 cell = ME_FindItemFwd(para, diCell);
1048 assert(cell && !cell->member.cell.prev_cell);
1049 if (tableDef->numCellsDefined < 1)
1050 {
1051 /* 2000 twips appears to be the cell size that native richedit uses
1052 * when no cell sizes are specified. */
1053 const int defaultCellSize = 2000;
1054 int nRightBoundary = defaultCellSize;
1055 cell->member.cell.nRightBoundary = nRightBoundary;
1056 while (cell->member.cell.next_cell) {
1057 cell = cell->member.cell.next_cell;
1058 nRightBoundary += defaultCellSize;
1059 cell->member.cell.nRightBoundary = nRightBoundary;
1060 }
1061 para = ME_InsertTableCellFromCursor(info->editor);
1062 cell = para->member.para.pCell;
1063 cell->member.cell.nRightBoundary = nRightBoundary;
1064 } else {
1065 for (i = 0; i < tableDef->numCellsDefined; i++)
1066 {
1067 RTFCell *cellDef = &tableDef->cells[i];
1068 cell->member.cell.nRightBoundary = cellDef->rightBoundary;
1069 ME_ApplyBorderProperties(info, &cell->member.cell.border,
1070 cellDef->border);
1071 cell = cell->member.cell.next_cell;
1072 if (!cell)
1073 {
1074 para = ME_InsertTableCellFromCursor(info->editor);
1075 cell = para->member.para.pCell;
1076 }
1077 }
1078 /* Cell for table row delimiter is empty */
1079 cell->member.cell.nRightBoundary = tableDef->cells[i-1].rightBoundary;
1080 }
1081
1082 run = ME_FindItemFwd(cell, diRun);
1083 if (info->editor->pCursors[0].pRun != run ||
1084 info->editor->pCursors[0].nOffset)
1085 {
1086 int nOfs, nChars;
1087 /* Delete inserted cells that aren't defined. */
1088 info->editor->pCursors[1].pRun = run;
1089 info->editor->pCursors[1].pPara = ME_GetParagraph(run);
1090 info->editor->pCursors[1].nOffset = 0;
1091 nOfs = ME_GetCursorOfs(&info->editor->pCursors[1]);
1092 nChars = ME_GetCursorOfs(&info->editor->pCursors[0]) - nOfs;
1093 ME_InternalDeleteText(info->editor, &info->editor->pCursors[1],
1094 nChars, TRUE);
1095 }
1096
1097 para = ME_InsertTableRowEndFromCursor(info->editor);
1098 para->member.para.pFmt->dxOffset = abs(info->tableDef->gapH);
1099 para->member.para.pFmt->dxStartIndent = info->tableDef->leftEdge;
1100 ME_ApplyBorderProperties(info, &para->member.para.border,
1101 tableDef->border);
1102 info->nestingLevel--;
1103 if (!info->nestingLevel)
1104 {
1105 if (info->canInheritInTbl) {
1106 tableDef->tableRowStart = para;
1107 } else {
1108 while (info->tableDef) {
1109 tableDef = info->tableDef;
1110 info->tableDef = tableDef->parent;
1111 heap_free(tableDef);
1112 }
1113 }
1114 } else {
1115 info->tableDef = tableDef->parent;
1116 heap_free(tableDef);
1117 }
1118 } else { /* v1.0 - v3.0 */
1119 WCHAR endl = '\r';
1120 ME_DisplayItem *para = info->editor->pCursors[0].pPara;
1121 PARAFORMAT2 *pFmt = para->member.para.pFmt;
1122 pFmt->dxOffset = info->tableDef->gapH;
1123 pFmt->dxStartIndent = info->tableDef->leftEdge;
1124
1125 ME_ApplyBorderProperties(info, &para->member.para.border,
1126 tableDef->border);
1127 while (tableDef->numCellsInserted < tableDef->numCellsDefined)
1128 {
1129 WCHAR tab = '\t';
1130 ME_InsertTextFromCursor(info->editor, 0, &tab, 1, info->style);
1131 tableDef->numCellsInserted++;
1132 }
1133 pFmt->cTabCount = min(tableDef->numCellsDefined, MAX_TAB_STOPS);
1134 if (!tableDef->numCellsDefined)
1135 pFmt->wEffects &= ~PFE_TABLE;
1136 ME_InsertTextFromCursor(info->editor, 0, &endl, 1, info->style);
1137 tableDef->numCellsInserted = 0;
1138 }
1139 break;
1140 }
1141 case rtfTab:
1142 case rtfPar:
1143 if (info->editor->bEmulateVersion10) { /* v1.0 - 3.0 */
1144 ME_DisplayItem *para;
1145 PARAFORMAT2 *pFmt;
1146 RTFFlushOutputBuffer(info);
1147 para = info->editor->pCursors[0].pPara;
1148 pFmt = para->member.para.pFmt;
1149 if (pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE)
1150 {
1151 /* rtfPar is treated like a space within a table. */
1152 info->rtfClass = rtfText;
1153 info->rtfMajor = ' ';
1154 }
1155 else if (info->rtfMinor == rtfPar && tableDef)
1156 tableDef->numCellsInserted = 0;
1157 }
1158 break;
1159 }
1160 }
1161
1162 static BOOL ME_RTFInsertOleObject(RTF_Info *info, HENHMETAFILE hemf, HBITMAP hbmp,
1163 const SIZEL* sz)
1164 {
1165 LPOLEOBJECT lpObject = NULL;
1166 LPSTORAGE lpStorage = NULL;
1167 LPOLECLIENTSITE lpClientSite = NULL;
1168 LPDATAOBJECT lpDataObject = NULL;
1169 LPOLECACHE lpOleCache = NULL;
1170 STGMEDIUM stgm;
1171 FORMATETC fm;
1172 CLSID clsid;
1173 BOOL ret = FALSE;
1174 DWORD conn;
1175
1176 if (hemf)
1177 {
1178 stgm.tymed = TYMED_ENHMF;
1179 stgm.u.hEnhMetaFile = hemf;
1180 fm.cfFormat = CF_ENHMETAFILE;
1181 }
1182 else if (hbmp)
1183 {
1184 stgm.tymed = TYMED_GDI;
1185 stgm.u.hBitmap = hbmp;
1186 fm.cfFormat = CF_BITMAP;
1187 }
1188 stgm.pUnkForRelease = NULL;
1189
1190 fm.ptd = NULL;
1191 fm.dwAspect = DVASPECT_CONTENT;
1192 fm.lindex = -1;
1193 fm.tymed = stgm.tymed;
1194
1195 if (!info->lpRichEditOle)
1196 {
1197 CreateIRichEditOle(NULL, info->editor, (VOID**)&info->lpRichEditOle);
1198 }
1199
1200 if (OleCreateDefaultHandler(&CLSID_NULL, NULL, &IID_IOleObject, (void**)&lpObject) == S_OK &&
1201 IRichEditOle_GetClientSite(info->lpRichEditOle, &lpClientSite) == S_OK &&
1202 IOleObject_SetClientSite(lpObject, lpClientSite) == S_OK &&
1203 IOleObject_GetUserClassID(lpObject, &clsid) == S_OK &&
1204 IOleObject_QueryInterface(lpObject, &IID_IOleCache, (void**)&lpOleCache) == S_OK &&
1205 IOleCache_Cache(lpOleCache, &fm, 0, &conn) == S_OK &&
1206 IOleObject_QueryInterface(lpObject, &IID_IDataObject, (void**)&lpDataObject) == S_OK &&
1207 IDataObject_SetData(lpDataObject, &fm, &stgm, TRUE) == S_OK)
1208 {
1209 REOBJECT reobject;
1210
1211 reobject.cbStruct = sizeof(reobject);
1212 reobject.cp = REO_CP_SELECTION;
1213 reobject.clsid = clsid;
1214 reobject.poleobj = lpObject;
1215 reobject.pstg = lpStorage;
1216 reobject.polesite = lpClientSite;
1217 /* convert from twips to .01 mm */
1218 reobject.sizel.cx = MulDiv(sz->cx, 254, 144);
1219 reobject.sizel.cy = MulDiv(sz->cy, 254, 144);
1220 reobject.dvaspect = DVASPECT_CONTENT;
1221 reobject.dwFlags = 0; /* FIXME */
1222 reobject.dwUser = 0;
1223
1224 ME_InsertOLEFromCursor(info->editor, &reobject, 0);
1225 ret = TRUE;
1226 }
1227
1228 if (lpObject) IOleObject_Release(lpObject);
1229 if (lpClientSite) IOleClientSite_Release(lpClientSite);
1230 if (lpStorage) IStorage_Release(lpStorage);
1231 if (lpDataObject) IDataObject_Release(lpDataObject);
1232 if (lpOleCache) IOleCache_Release(lpOleCache);
1233
1234 return ret;
1235 }
1236
1237 static void ME_RTFReadShpPictGroup( RTF_Info *info )
1238 {
1239 int level = 1;
1240
1241 for (;;)
1242 {
1243 RTFGetToken (info);
1244
1245 if (info->rtfClass == rtfEOF) return;
1246 if (RTFCheckCM( info, rtfGroup, rtfEndGroup ))
1247 {
1248 if (--level == 0) break;
1249 }
1250 else if (RTFCheckCM( info, rtfGroup, rtfBeginGroup ))
1251 {
1252 level++;
1253 }
1254 else
1255 {
1256 RTFRouteToken( info );
1257 if (RTFCheckCM( info, rtfGroup, rtfEndGroup ))
1258 level--;
1259 }
1260 }
1261
1262 RTFRouteToken( info ); /* feed "}" back to router */
1263 return;
1264 }
1265
1266 static DWORD read_hex_data( RTF_Info *info, BYTE **out )
1267 {
1268 DWORD read = 0, size = 1024;
1269 BYTE *buf, val;
1270 BOOL flip;
1271
1272 *out = NULL;
1273
1274 if (info->rtfClass != rtfText)
1275 {
1276 ERR("Called with incorrect token\n");
1277 return 0;
1278 }
1279
1280 buf = HeapAlloc( GetProcessHeap(), 0, size );
1281 if (!buf) return 0;
1282
1283 val = info->rtfMajor;
1284 for (flip = TRUE;; flip = !flip)
1285 {
1286 RTFGetToken( info );
1287 if (info->rtfClass == rtfEOF)
1288 {
1289 HeapFree( GetProcessHeap(), 0, buf );
1290 return 0;
1291 }
1292 if (info->rtfClass != rtfText) break;
1293 if (flip)
1294 {
1295 if (read >= size)
1296 {
1297 size *= 2;
1298 buf = HeapReAlloc( GetProcessHeap(), 0, buf, size );
1299 if (!buf) return 0;
1300 }
1301 buf[read++] = RTFCharToHex(val) * 16 + RTFCharToHex(info->rtfMajor);
1302 }
1303 else
1304 val = info->rtfMajor;
1305 }
1306 if (flip) FIXME("wrong hex string\n");
1307
1308 *out = buf;
1309 return read;
1310 }
1311
1312 static void ME_RTFReadPictGroup(RTF_Info *info)
1313 {
1314 SIZEL sz;
1315 BYTE *buffer = NULL;
1316 DWORD size = 0;
1317 METAFILEPICT mfp;
1318 HENHMETAFILE hemf;
1319 HBITMAP hbmp;
1320 enum gfxkind {gfx_unknown = 0, gfx_enhmetafile, gfx_metafile, gfx_dib} gfx = gfx_unknown;
1321 int level = 1;
1322
1323 mfp.mm = MM_TEXT;
1324 sz.cx = sz.cy = 0;
1325
1326 for (;;)
1327 {
1328 RTFGetToken( info );
1329
1330 if (info->rtfClass == rtfText)
1331 {
1332 if (level == 1)
1333 {
1334 if (!buffer)
1335 size = read_hex_data( info, &buffer );
1336 }
1337 else
1338 {
1339 RTFSkipGroup( info );
1340 }
1341 } /* We potentially have a new token so fall through. */
1342
1343 if (info->rtfClass == rtfEOF) return;
1344
1345 if (RTFCheckCM( info, rtfGroup, rtfEndGroup ))
1346 {
1347 if (--level == 0) break;
1348 continue;
1349 }
1350 if (RTFCheckCM( info, rtfGroup, rtfBeginGroup ))
1351 {
1352 level++;
1353 continue;
1354 }
1355 if (!RTFCheckCM( info, rtfControl, rtfPictAttr ))
1356 {
1357 RTFRouteToken( info );
1358 if (RTFCheckCM( info, rtfGroup, rtfEndGroup ))
1359 level--;
1360 continue;
1361 }
1362
1363 if (RTFCheckMM( info, rtfPictAttr, rtfWinMetafile ))
1364 {
1365 mfp.mm = info->rtfParam;
1366 gfx = gfx_metafile;
1367 }
1368 else if (RTFCheckMM( info, rtfPictAttr, rtfDevIndBitmap ))
1369 {
1370 if (info->rtfParam != 0) FIXME("dibitmap should be 0 (%d)\n", info->rtfParam);
1371 gfx = gfx_dib;
1372 }
1373 else if (RTFCheckMM( info, rtfPictAttr, rtfEmfBlip ))
1374 gfx = gfx_enhmetafile;
1375 else if (RTFCheckMM( info, rtfPictAttr, rtfPicWid ))
1376 mfp.xExt = info->rtfParam;
1377 else if (RTFCheckMM( info, rtfPictAttr, rtfPicHt ))
1378 mfp.yExt = info->rtfParam;
1379 else if (RTFCheckMM( info, rtfPictAttr, rtfPicGoalWid ))
1380 sz.cx = info->rtfParam;
1381 else if (RTFCheckMM( info, rtfPictAttr, rtfPicGoalHt ))
1382 sz.cy = info->rtfParam;
1383 else
1384 FIXME("Non supported attribute: %d %d %d\n", info->rtfClass, info->rtfMajor, info->rtfMinor);
1385 }
1386
1387 if (buffer)
1388 {
1389 switch (gfx)
1390 {
1391 case gfx_enhmetafile:
1392 if ((hemf = SetEnhMetaFileBits( size, buffer )))
1393 ME_RTFInsertOleObject( info, hemf, NULL, &sz );
1394 break;
1395 case gfx_metafile:
1396 if ((hemf = SetWinMetaFileBits( size, buffer, NULL, &mfp )))
1397 ME_RTFInsertOleObject( info, hemf, NULL, &sz );
1398 break;
1399 case gfx_dib:
1400 {
1401 BITMAPINFO *bi = (BITMAPINFO*)buffer;
1402 HDC hdc = GetDC(0);
1403 unsigned nc = bi->bmiHeader.biClrUsed;
1404
1405 /* not quite right, especially for bitfields type of compression */
1406 if (!nc && bi->bmiHeader.biBitCount <= 8)
1407 nc = 1 << bi->bmiHeader.biBitCount;
1408 if ((hbmp = CreateDIBitmap( hdc, &bi->bmiHeader,
1409 CBM_INIT, (char*)(bi + 1) + nc * sizeof(RGBQUAD),
1410 bi, DIB_RGB_COLORS)) )
1411 ME_RTFInsertOleObject( info, NULL, hbmp, &sz );
1412 ReleaseDC( 0, hdc );
1413 break;
1414 }
1415 default:
1416 break;
1417 }
1418 }
1419 HeapFree( GetProcessHeap(), 0, buffer );
1420 RTFRouteToken( info ); /* feed "}" back to router */
1421 return;
1422 }
1423
1424 /* for now, lookup the \result part and use it, whatever the object */
1425 static void ME_RTFReadObjectGroup(RTF_Info *info)
1426 {
1427 for (;;)
1428 {
1429 RTFGetToken (info);
1430 if (info->rtfClass == rtfEOF)
1431 return;
1432 if (RTFCheckCM(info, rtfGroup, rtfEndGroup))
1433 break;
1434 if (RTFCheckCM(info, rtfGroup, rtfBeginGroup))
1435 {
1436 RTFGetToken (info);
1437 if (info->rtfClass == rtfEOF)
1438 return;
1439 if (RTFCheckCMM(info, rtfControl, rtfDestination, rtfObjResult))
1440 {
1441 int level = 1;
1442
1443 while (RTFGetToken (info) != rtfEOF)
1444 {
1445 if (info->rtfClass == rtfGroup)
1446 {
1447 if (info->rtfMajor == rtfBeginGroup) level++;
1448 else if (info->rtfMajor == rtfEndGroup && --level < 0) break;
1449 }
1450 RTFRouteToken(info);
1451 }
1452 }
1453 else RTFSkipGroup(info);
1454 continue;
1455 }
1456 if (!RTFCheckCM (info, rtfControl, rtfObjAttr))
1457 {
1458 FIXME("Non supported attribute: %d %d %d\n", info->rtfClass, info->rtfMajor, info->rtfMinor);
1459 return;
1460 }
1461 }
1462 RTFRouteToken(info); /* feed "}" back to router */
1463 }
1464
1465 static void ME_RTFReadHook(RTF_Info *info)
1466 {
1467 switch(info->rtfClass)
1468 {
1469 case rtfGroup:
1470 switch(info->rtfMajor)
1471 {
1472 case rtfBeginGroup:
1473 if (info->stackTop < maxStack) {
1474 info->stack[info->stackTop].style = info->style;
1475 ME_AddRefStyle(info->style);
1476 info->stack[info->stackTop].codePage = info->codePage;
1477 info->stack[info->stackTop].unicodeLength = info->unicodeLength;
1478 }
1479 info->stackTop++;
1480 info->styleChanged = FALSE;
1481 break;
1482 case rtfEndGroup:
1483 {
1484 RTFFlushOutputBuffer(info);
1485 info->stackTop--;
1486 if (info->stackTop <= 0)
1487 info->rtfClass = rtfEOF;
1488 if (info->stackTop < 0)
1489 return;
1490
1491 ME_ReleaseStyle(info->style);
1492 info->style = info->stack[info->stackTop].style;
1493 info->codePage = info->stack[info->stackTop].codePage;
1494 info->unicodeLength = info->stack[info->stackTop].unicodeLength;
1495 break;
1496 }
1497 }
1498 break;
1499 }
1500 }
1501
1502 void
1503 ME_StreamInFill(ME_InStream *stream)
1504 {
1505 stream->editstream->dwError = stream->editstream->pfnCallback(stream->editstream->dwCookie,
1506 (BYTE *)stream->buffer,
1507 sizeof(stream->buffer),
1508 (LONG *)&stream->dwSize);
1509 stream->dwUsed = 0;
1510 }
1511
1512 static LRESULT ME_StreamIn(ME_TextEditor *editor, DWORD format, EDITSTREAM *stream, BOOL stripLastCR)
1513 {
1514 RTF_Info parser;
1515 ME_Style *style;
1516 int from, to, nUndoMode;
1517 int nEventMask = editor->nEventMask;
1518 ME_InStream inStream;
1519 BOOL invalidRTF = FALSE;
1520 ME_Cursor *selStart, *selEnd;
1521 LRESULT num_read = 0; /* bytes read for SF_TEXT, non-control chars inserted for SF_RTF */
1522
1523 TRACE("stream==%p editor==%p format==0x%X\n", stream, editor, format);
1524 editor->nEventMask = 0;
1525
1526 ME_GetSelectionOfs(editor, &from, &to);
1527 if (format & SFF_SELECTION && editor->mode & TM_RICHTEXT)
1528 {
1529 ME_GetSelection(editor, &selStart, &selEnd);
1530 style = ME_GetSelectionInsertStyle(editor);
1531
1532 ME_InternalDeleteText(editor, selStart, to - from, FALSE);
1533
1534 /* Don't insert text at the end of the table row */
1535 if (!editor->bEmulateVersion10) { /* v4.1 */
1536 ME_DisplayItem *para = editor->pCursors->pPara;
1537 if (para->member.para.nFlags & MEPF_ROWEND)
1538 {
1539 para = para->member.para.next_para;
1540 editor->pCursors[0].pPara = para;
1541 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
1542 editor->pCursors[0].nOffset = 0;
1543 }
1544 if (para->member.para.nFlags & MEPF_ROWSTART)
1545 {
1546 para = para->member.para.next_para;
1547 editor->pCursors[0].pPara = para;
1548 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
1549 editor->pCursors[0].nOffset = 0;
1550 }
1551 editor->pCursors[1] = editor->pCursors[0];
1552 } else { /* v1.0 - 3.0 */
1553 if (editor->pCursors[0].pRun->member.run.nFlags & MERF_ENDPARA &&
1554 ME_IsInTable(editor->pCursors[0].pRun))
1555 return 0;
1556 }
1557 } else {
1558 style = editor->pBuffer->pDefaultStyle;
1559 ME_AddRefStyle(style);
1560 ME_SetSelection(editor, 0, 0);
1561 ME_InternalDeleteText(editor, &editor->pCursors[1],
1562 ME_GetTextLength(editor), FALSE);
1563 from = to = 0;
1564 ME_ClearTempStyle(editor);
1565 ME_SetDefaultParaFormat(editor, editor->pCursors[0].pPara->member.para.pFmt);
1566 }
1567
1568
1569 /* Back up undo mode to a local variable */
1570 nUndoMode = editor->nUndoMode;
1571
1572 /* Only create an undo if SFF_SELECTION is set */
1573 if (!(format & SFF_SELECTION))
1574 editor->nUndoMode = umIgnore;
1575
1576 inStream.editstream = stream;
1577 inStream.editstream->dwError = 0;
1578 inStream.dwSize = 0;
1579 inStream.dwUsed = 0;
1580
1581 if (format & SF_RTF)
1582 {
1583 /* Check if it's really RTF, and if it is not, use plain text */
1584 ME_StreamInFill(&inStream);
1585 if (!inStream.editstream->dwError)
1586 {
1587 if ((!editor->bEmulateVersion10 && strncmp(inStream.buffer, "{\\rtf", 5) && strncmp(inStream.buffer, "{\\urtf", 6))
1588 || (editor->bEmulateVersion10 && *inStream.buffer != '{'))
1589 {
1590 invalidRTF = TRUE;
1591 inStream.editstream->dwError = -16;
1592 }
1593 }
1594 }
1595
1596 if (!invalidRTF && !inStream.editstream->dwError)
1597 {
1598 if (format & SF_RTF) {
1599 from = ME_GetCursorOfs(&editor->pCursors[0]);
1600
1601 /* setup the RTF parser */
1602 memset(&parser, 0, sizeof parser);
1603 RTFSetEditStream(&parser, &inStream);
1604 parser.rtfFormat = format&(SF_TEXT|SF_RTF);
1605 parser.editor = editor;
1606 parser.style = style;
1607 WriterInit(&parser);
1608 RTFInit(&parser);
1609 RTFSetReadHook(&parser, ME_RTFReadHook);
1610 RTFSetDestinationCallback(&parser, rtfShpPict, ME_RTFReadShpPictGroup);
1611 RTFSetDestinationCallback(&parser, rtfPict, ME_RTFReadPictGroup);
1612 RTFSetDestinationCallback(&parser, rtfObject, ME_RTFReadObjectGroup);
1613 if (!parser.editor->bEmulateVersion10) /* v4.1 */
1614 {
1615 RTFSetDestinationCallback(&parser, rtfNoNestTables, RTFSkipGroup);
1616 RTFSetDestinationCallback(&parser, rtfNestTableProps, RTFReadGroup);
1617 }
1618 BeginFile(&parser);
1619
1620 /* do the parsing */
1621 RTFRead(&parser);
1622 RTFFlushOutputBuffer(&parser);
1623 if (!editor->bEmulateVersion10) { /* v4.1 */
1624 if (parser.tableDef && parser.tableDef->tableRowStart &&
1625 (parser.nestingLevel > 0 || parser.canInheritInTbl))
1626 {
1627 /* Delete any incomplete table row at the end of the rich text. */
1628 int nOfs, nChars;
1629 ME_DisplayItem *para;
1630
1631 parser.rtfMinor = rtfRow;
1632 /* Complete the table row before deleting it.
1633 * By doing it this way we will have the current paragraph format set
1634 * properly to reflect that is not in the complete table, and undo items
1635 * will be added for this change to the current paragraph format. */
1636 if (parser.nestingLevel > 0)
1637 {
1638 while (parser.nestingLevel > 1)
1639 ME_RTFSpecialCharHook(&parser); /* Decrements nestingLevel */
1640 para = parser.tableDef->tableRowStart;
1641 ME_RTFSpecialCharHook(&parser);
1642 } else {
1643 para = parser.tableDef->tableRowStart;
1644 ME_RTFSpecialCharHook(&parser);
1645 assert(para->member.para.nFlags & MEPF_ROWEND);
1646 para = para->member.para.next_para;
1647 }
1648
1649 editor->pCursors[1].pPara = para;
1650 editor->pCursors[1].pRun = ME_FindItemFwd(para, diRun);
1651 editor->pCursors[1].nOffset = 0;
1652 nOfs = ME_GetCursorOfs(&editor->pCursors[1]);
1653 nChars = ME_GetCursorOfs(&editor->pCursors[0]) - nOfs;
1654 ME_InternalDeleteText(editor, &editor->pCursors[1], nChars, TRUE);
1655 if (parser.tableDef)
1656 parser.tableDef->tableRowStart = NULL;
1657 }
1658 }
1659 ME_CheckTablesForCorruption(editor);
1660 RTFDestroy(&parser);
1661 if (parser.lpRichEditOle)
1662 IRichEditOle_Release(parser.lpRichEditOle);
1663
1664 if (parser.stackTop > 0)
1665 {
1666 while (--parser.stackTop >= 0)
1667 {
1668 ME_ReleaseStyle(parser.style);
1669 parser.style = parser.stack[parser.stackTop].style;
1670 }
1671 if (!inStream.editstream->dwError)
1672 inStream.editstream->dwError = HRESULT_FROM_WIN32(ERROR_HANDLE_EOF);
1673 }
1674
1675 /* Remove last line break, as mandated by tests. This is not affected by
1676 CR/LF counters, since RTF streaming presents only \para tokens, which
1677 are converted according to the standard rules: \r for 2.0, \r\n for 1.0
1678 */
1679 if (stripLastCR && !(format & SFF_SELECTION)) {
1680 int newto;
1681 ME_GetSelection(editor, &selStart, &selEnd);
1682 newto = ME_GetCursorOfs(selEnd);
1683 if (newto > to + (editor->bEmulateVersion10 ? 1 : 0)) {
1684 WCHAR lastchar[3] = {'\0', '\0'};
1685 int linebreakSize = editor->bEmulateVersion10 ? 2 : 1;
1686 ME_Cursor linebreakCursor = *selEnd;
1687
1688 ME_MoveCursorChars(editor, &linebreakCursor, -linebreakSize);
1689 ME_GetTextW(editor, lastchar, 2, &linebreakCursor, linebreakSize, FALSE, FALSE);
1690 if (lastchar[0] == '\r' && (lastchar[1] == '\n' || lastchar[1] == '\0')) {
1691 ME_InternalDeleteText(editor, &linebreakCursor, linebreakSize, FALSE);
1692 }
1693 }
1694 }
1695 to = ME_GetCursorOfs(&editor->pCursors[0]);
1696 num_read = to - from;
1697
1698 style = parser.style;
1699 }
1700 else if (format & SF_TEXT)
1701 num_read = ME_StreamInText(editor, format, &inStream, style);
1702 else
1703 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
1704 /* put the cursor at the top */
1705 if (!(format & SFF_SELECTION))
1706 ME_SetSelection(editor, 0, 0);
1707 }
1708
1709 /* Restore saved undo mode */
1710 editor->nUndoMode = nUndoMode;
1711
1712 /* even if we didn't add an undo, we need to commit anything on the stack */
1713 ME_CommitUndo(editor);
1714
1715 /* If SFF_SELECTION isn't set, delete any undos from before we started too */
1716 if (!(format & SFF_SELECTION))
1717 ME_EmptyUndoStack(editor);
1718
1719 ME_ReleaseStyle(style);
1720 editor->nEventMask = nEventMask;
1721 ME_UpdateRepaint(editor, FALSE);
1722 if (!(format & SFF_SELECTION)) {
1723 ME_ClearTempStyle(editor);
1724 }
1725 ITextHost_TxShowCaret(editor->texthost, FALSE);
1726 ME_MoveCaret(editor);
1727 ITextHost_TxShowCaret(editor->texthost, TRUE);
1728 ME_SendSelChange(editor);
1729 ME_SendRequestResize(editor, FALSE);
1730
1731 return num_read;
1732 }
1733
1734
1735 typedef struct tagME_RTFStringStreamStruct
1736 {
1737 char *string;
1738 int pos;
1739 int length;
1740 } ME_RTFStringStreamStruct;
1741
1742 static DWORD CALLBACK ME_ReadFromRTFString(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1743 {
1744 ME_RTFStringStreamStruct *pStruct = (ME_RTFStringStreamStruct *)dwCookie;
1745 int count;
1746
1747 count = min(cb, pStruct->length - pStruct->pos);
1748 memmove(lpBuff, pStruct->string + pStruct->pos, count);
1749 pStruct->pos += count;
1750 *pcb = count;
1751 return 0;
1752 }
1753
1754 static void
1755 ME_StreamInRTFString(ME_TextEditor *editor, BOOL selection, char *string)
1756 {
1757 EDITSTREAM es;
1758 ME_RTFStringStreamStruct data;
1759
1760 data.string = string;
1761 data.length = strlen(string);
1762 data.pos = 0;
1763 es.dwCookie = (DWORD_PTR)&data;
1764 es.pfnCallback = ME_ReadFromRTFString;
1765 ME_StreamIn(editor, SF_RTF | (selection ? SFF_SELECTION : 0), &es, TRUE);
1766 }
1767
1768
1769 static int
1770 ME_FindText(ME_TextEditor *editor, DWORD flags, const CHARRANGE *chrg, const WCHAR *text, CHARRANGE *chrgText)
1771 {
1772 const int nLen = lstrlenW(text);
1773 const int nTextLen = ME_GetTextLength(editor);
1774 int nMin, nMax;
1775 ME_Cursor cursor;
1776 WCHAR wLastChar = ' ';
1777
1778 TRACE("flags==0x%08x, chrg->cpMin==%d, chrg->cpMax==%d text==%s\n",
1779 flags, chrg->cpMin, chrg->cpMax, debugstr_w(text));
1780
1781 if (flags & ~(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD))
1782 FIXME("Flags 0x%08x not implemented\n",
1783 flags & ~(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD));
1784
1785 nMin = chrg->cpMin;
1786 if (chrg->cpMax == -1)
1787 nMax = nTextLen;
1788 else
1789 nMax = chrg->cpMax > nTextLen ? nTextLen : chrg->cpMax;
1790
1791 /* In 1.0 emulation, if cpMax reaches end of text, add the FR_DOWN flag */
1792 if (editor->bEmulateVersion10 && nMax == nTextLen)
1793 {
1794 flags |= FR_DOWN;
1795 }
1796
1797 /* In 1.0 emulation, cpMin must always be no greater than cpMax */
1798 if (editor->bEmulateVersion10 && nMax < nMin)
1799 {
1800 if (chrgText)
1801 {
1802 chrgText->cpMin = -1;
1803 chrgText->cpMax = -1;
1804 }
1805 return -1;
1806 }
1807
1808 /* when searching up, if cpMin < cpMax, then instead of searching
1809 * on [cpMin,cpMax], we search on [0,cpMin], otherwise, search on
1810 * [cpMax, cpMin]. The exception is when cpMax is -1, in which
1811 * case, it is always bigger than cpMin.
1812 */
1813 if (!editor->bEmulateVersion10 && !(flags & FR_DOWN))
1814 {
1815 int nSwap = nMax;
1816
1817 nMax = nMin > nTextLen ? nTextLen : nMin;
1818 if (nMin < nSwap || chrg->cpMax == -1)
1819 nMin = 0;
1820 else
1821 nMin = nSwap;
1822 }
1823
1824 if (!nLen || nMin < 0 || nMax < 0 || nMax < nMin)
1825 {
1826 if (chrgText)
1827 chrgText->cpMin = chrgText->cpMax = -1;
1828 return -1;
1829 }
1830
1831 if (flags & FR_DOWN) /* Forward search */
1832 {
1833 /* If possible, find the character before where the search starts */
1834 if ((flags & FR_WHOLEWORD) && nMin)
1835 {
1836 ME_CursorFromCharOfs(editor, nMin - 1, &cursor);
1837 wLastChar = *get_text( &cursor.pRun->member.run, cursor.nOffset );
1838 ME_MoveCursorChars(editor, &cursor, 1);
1839 } else {
1840 ME_CursorFromCharOfs(editor, nMin, &cursor);
1841 }
1842
1843 while (cursor.pRun && ME_GetCursorOfs(&cursor) + nLen <= nMax)
1844 {
1845 ME_DisplayItem *pCurItem = cursor.pRun;
1846 int nCurStart = cursor.nOffset;
1847 int nMatched = 0;
1848
1849 while (pCurItem && ME_CharCompare( *get_text( &pCurItem->member.run, nCurStart + nMatched ), text[nMatched], (flags & FR_MATCHCASE)))
1850 {
1851 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
1852 break;
1853
1854 nMatched++;
1855 if (nMatched == nLen)
1856 {
1857 ME_DisplayItem *pNextItem = pCurItem;
1858 int nNextStart = nCurStart;
1859 WCHAR wNextChar;
1860
1861 /* Check to see if next character is a whitespace */
1862 if (flags & FR_WHOLEWORD)
1863 {
1864 if (nCurStart + nMatched == pCurItem->member.run.len)
1865 {
1866 pNextItem = ME_FindItemFwd(pCurItem, diRun);
1867 nNextStart = -nMatched;
1868 }
1869
1870 if (pNextItem)
1871 wNextChar = *get_text( &pNextItem->member.run, nNextStart + nMatched );
1872 else
1873 wNextChar = ' ';
1874
1875 if (isalnumW(wNextChar))
1876 break;
1877 }
1878
1879 cursor.nOffset += cursor.pPara->member.para.nCharOfs + cursor.pRun->member.run.nCharOfs;
1880 if (chrgText)
1881 {
1882 chrgText->cpMin = cursor.nOffset;
1883 chrgText->cpMax = cursor.nOffset + nLen;
1884 }
1885 TRACE("found at %d-%d\n", cursor.nOffset, cursor.nOffset + nLen);
1886 return cursor.nOffset;
1887 }
1888 if (nCurStart + nMatched == pCurItem->member.run.len)
1889 {
1890 pCurItem = ME_FindItemFwd(pCurItem, diRun);
1891 nCurStart = -nMatched;
1892 }
1893 }
1894 if (pCurItem)
1895 wLastChar = *get_text( &pCurItem->member.run, nCurStart + nMatched );
1896 else
1897 wLastChar = ' ';
1898
1899 cursor.nOffset++;
1900 if (cursor.nOffset == cursor.pRun->member.run.len)
1901 {
1902 ME_NextRun(&cursor.pPara, &cursor.pRun);
1903 cursor.nOffset = 0;
1904 }
1905 }
1906 }
1907 else /* Backward search */
1908 {
1909 /* If possible, find the character after where the search ends */
1910 if ((flags & FR_WHOLEWORD) && nMax < nTextLen - 1)
1911 {
1912 ME_CursorFromCharOfs(editor, nMax + 1, &cursor);
1913 wLastChar = *get_text( &cursor.pRun->member.run, cursor.nOffset );
1914 ME_MoveCursorChars(editor, &cursor, -1);
1915 } else {
1916 ME_CursorFromCharOfs(editor, nMax, &cursor);
1917 }
1918
1919 while (cursor.pRun && ME_GetCursorOfs(&cursor) - nLen >= nMin)
1920 {
1921 ME_DisplayItem *pCurItem = cursor.pRun;
1922 ME_DisplayItem *pCurPara = cursor.pPara;
1923 int nCurEnd = cursor.nOffset;
1924 int nMatched = 0;
1925
1926 if (nCurEnd == 0)
1927 {
1928 ME_PrevRun(&pCurPara, &pCurItem);
1929 nCurEnd = pCurItem->member.run.len;
1930 }
1931
1932 while (pCurItem && ME_CharCompare( *get_text( &pCurItem->member.run, nCurEnd - nMatched - 1 ),
1933 text[nLen - nMatched - 1], (flags & FR_MATCHCASE) ))
1934 {
1935 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
1936 break;
1937
1938 nMatched++;
1939 if (nMatched == nLen)
1940 {
1941 ME_DisplayItem *pPrevItem = pCurItem;
1942 int nPrevEnd = nCurEnd;
1943 WCHAR wPrevChar;
1944 int nStart;
1945
1946 /* Check to see if previous character is a whitespace */
1947 if (flags & FR_WHOLEWORD)
1948 {
1949 if (nPrevEnd - nMatched == 0)
1950 {
1951 pPrevItem = ME_FindItemBack(pCurItem, diRun);
1952 if (pPrevItem)
1953 nPrevEnd = pPrevItem->member.run.len + nMatched;
1954 }
1955
1956 if (pPrevItem)
1957 wPrevChar = *get_text( &pPrevItem->member.run, nPrevEnd - nMatched - 1 );
1958 else
1959 wPrevChar = ' ';
1960
1961 if (isalnumW(wPrevChar))
1962 break;
1963 }
1964
1965 nStart = pCurPara->member.para.nCharOfs
1966 + pCurItem->member.run.nCharOfs + nCurEnd - nMatched;
1967 if (chrgText)
1968 {
1969 chrgText->cpMin = nStart;
1970 chrgText->cpMax = nStart + nLen;
1971 }
1972 TRACE("found at %d-%d\n", nStart, nStart + nLen);
1973 return nStart;
1974 }
1975 if (nCurEnd - nMatched == 0)
1976 {
1977 ME_PrevRun(&pCurPara, &pCurItem);
1978 /* Don't care about pCurItem becoming NULL here; it's already taken
1979 * care of in the exterior loop condition */
1980 nCurEnd = pCurItem->member.run.len + nMatched;
1981 }
1982 }
1983 if (pCurItem)
1984 wLastChar = *get_text( &pCurItem->member.run, nCurEnd - nMatched - 1 );
1985 else
1986 wLastChar = ' ';
1987
1988 cursor.nOffset--;
1989 if (cursor.nOffset < 0)
1990 {
1991 ME_PrevRun(&cursor.pPara, &cursor.pRun);
1992 cursor.nOffset = cursor.pRun->member.run.len;
1993 }
1994 }
1995 }
1996 TRACE("not found\n");
1997 if (chrgText)
1998 chrgText->cpMin = chrgText->cpMax = -1;
1999 return -1;
2000 }
2001
2002 static int ME_GetTextEx(ME_TextEditor *editor, GETTEXTEX *ex, LPARAM pText)
2003 {
2004 int nChars;
2005 ME_Cursor start;
2006
2007 if (!ex->cb || !pText) return 0;
2008
2009 if (ex->flags & ~(GT_SELECTION | GT_USECRLF))
2010 FIXME("GETTEXTEX flags 0x%08x not supported\n", ex->flags & ~(GT_SELECTION | GT_USECRLF));
2011
2012 if (ex->flags & GT_SELECTION)
2013 {
2014 int from, to;
2015 int nStartCur = ME_GetSelectionOfs(editor, &from, &to);
2016 start = editor->pCursors[nStartCur];
2017 nChars = to - from;
2018 }
2019 else
2020 {
2021 ME_SetCursorToStart(editor, &start);
2022 nChars = INT_MAX;
2023 }
2024 if (ex->codepage == CP_UNICODE)
2025 {
2026 return ME_GetTextW(editor, (LPWSTR)pText, ex->cb / sizeof(WCHAR) - 1,
2027 &start, nChars, ex->flags & GT_USECRLF, FALSE);
2028 }
2029 else
2030 {
2031 /* potentially each char may be a CR, why calculate the exact value with O(N) when
2032 we can just take a bigger buffer? :)
2033 The above assumption still holds with CR/LF counters, since CR->CRLF expansion
2034 occurs only in richedit 2.0 mode, in which line breaks have only one CR
2035 */
2036 int crlfmul = (ex->flags & GT_USECRLF) ? 2 : 1;
2037 DWORD buflen;
2038 LPWSTR buffer;
2039 LRESULT rc;
2040
2041 buflen = min(crlfmul * nChars, ex->cb - 1);
2042 buffer = heap_alloc((buflen + 1) * sizeof(WCHAR));
2043
2044 nChars = ME_GetTextW(editor, buffer, buflen, &start, nChars, ex->flags & GT_USECRLF, FALSE);
2045 rc = WideCharToMultiByte(ex->codepage, 0, buffer, nChars + 1,
2046 (LPSTR)pText, ex->cb, ex->lpDefaultChar, ex->lpUsedDefChar);
2047 if (rc) rc--; /* do not count 0 terminator */
2048
2049 heap_free(buffer);
2050 return rc;
2051 }
2052 }
2053
2054 static int ME_GetTextRange(ME_TextEditor *editor, WCHAR *strText,
2055 const ME_Cursor *start, int nLen, BOOL unicode)
2056 {
2057 if (!strText) return 0;
2058 if (unicode) {
2059 return ME_GetTextW(editor, strText, INT_MAX, start, nLen, FALSE, FALSE);
2060 } else {
2061 int nChars;
2062 WCHAR *p = ALLOC_N_OBJ(WCHAR, nLen+1);
2063 if (!p) return 0;
2064 nChars = ME_GetTextW(editor, p, nLen, start, nLen, FALSE, FALSE);
2065 WideCharToMultiByte(CP_ACP, 0, p, nChars+1, (char *)strText,
2066 nLen+1, NULL, NULL);
2067 FREE_OBJ(p);
2068 return nChars;
2069 }
2070 }
2071
2072 static int handle_EM_EXSETSEL( ME_TextEditor *editor, int to, int from )
2073 {
2074 int end;
2075
2076 TRACE("%d - %d\n", to, from );
2077
2078 ME_InvalidateSelection( editor );
2079 end = ME_SetSelection( editor, to, from );
2080 ME_InvalidateSelection( editor );
2081 ITextHost_TxShowCaret( editor->texthost, FALSE );
2082 ME_ShowCaret( editor );
2083 ME_SendSelChange( editor );
2084
2085 return end;
2086 }
2087
2088 typedef struct tagME_GlobalDestStruct
2089 {
2090 HGLOBAL hData;
2091 int nLength;
2092 } ME_GlobalDestStruct;
2093
2094 static DWORD CALLBACK ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
2095 {
2096 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
2097 int i;
2098 WORD *pSrc, *pDest;
2099
2100 cb = cb >> 1;
2101 pDest = (WORD *)lpBuff;
2102 pSrc = GlobalLock(pData->hData);
2103 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
2104 pDest[i] = pSrc[pData->nLength+i];
2105 }
2106 pData->nLength += i;
2107 *pcb = 2*i;
2108 GlobalUnlock(pData->hData);
2109 return 0;
2110 }
2111
2112 static DWORD CALLBACK ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
2113 {
2114 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
2115 int i;
2116 BYTE *pSrc, *pDest;
2117
2118 pDest = lpBuff;
2119 pSrc = GlobalLock(pData->hData);
2120 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
2121 pDest[i] = pSrc[pData->nLength+i];
2122 }
2123 pData->nLength += i;
2124 *pcb = i;
2125 GlobalUnlock(pData->hData);
2126 return 0;
2127 }
2128
2129 static BOOL ME_Paste(ME_TextEditor *editor)
2130 {
2131 DWORD dwFormat = 0;
2132 EDITSTREAM es;
2133 ME_GlobalDestStruct gds;
2134 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
2135 UINT cf = 0;
2136
2137 if (IsClipboardFormatAvailable(nRTFFormat))
2138 cf = nRTFFormat, dwFormat = SF_RTF;
2139 else if (IsClipboardFormatAvailable(CF_UNICODETEXT))
2140 cf = CF_UNICODETEXT, dwFormat = SF_TEXT|SF_UNICODE;
2141 else
2142 return FALSE;
2143
2144 if (!OpenClipboard(editor->hWnd))
2145 return FALSE;
2146 gds.hData = GetClipboardData(cf);
2147 gds.nLength = 0;
2148 es.dwCookie = (DWORD_PTR)&gds;
2149 es.pfnCallback = dwFormat == SF_RTF ? ME_ReadFromHGLOBALRTF : ME_ReadFromHGLOBALUnicode;
2150 ME_StreamIn(editor, dwFormat|SFF_SELECTION, &es, FALSE);
2151
2152 CloseClipboard();
2153 return TRUE;
2154 }
2155
2156 static BOOL ME_Copy(ME_TextEditor *editor, const ME_Cursor *start, int nChars)
2157 {
2158 LPDATAOBJECT dataObj = NULL;
2159 HRESULT hr = S_OK;
2160
2161 if (editor->cPasswordMask)
2162 return FALSE; /* Copying or Cutting masked text isn't allowed */
2163
2164 if(editor->lpOleCallback)
2165 {
2166 CHARRANGE range;
2167 range.cpMin = ME_GetCursorOfs(start);
2168 range.cpMax = range.cpMin + nChars;
2169 hr = IRichEditOleCallback_GetClipboardData(editor->lpOleCallback, &range, RECO_COPY, &dataObj);
2170 }
2171 if(FAILED(hr) || !dataObj)
2172 hr = ME_GetDataObject(editor, start, nChars, &dataObj);
2173 if(SUCCEEDED(hr)) {
2174 hr = OleSetClipboard(dataObj);
2175 IDataObject_Release(dataObj);
2176 }
2177 return SUCCEEDED(hr);
2178 }
2179
2180 /* helper to send a msg filter notification */
2181 static BOOL
2182 ME_FilterEvent(ME_TextEditor *editor, UINT msg, WPARAM* wParam, LPARAM* lParam)
2183 {
2184 MSGFILTER msgf;
2185
2186 if (!editor->hWnd || !editor->hwndParent) return FALSE;
2187 msgf.nmhdr.hwndFrom = editor->hWnd;
2188 msgf.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
2189 msgf.nmhdr.code = EN_MSGFILTER;
2190 msgf.msg = msg;
2191 msgf.wParam = *wParam;
2192 msgf.lParam = *lParam;
2193 if (SendMessageW(editor->hwndParent, WM_NOTIFY, msgf.nmhdr.idFrom, (LPARAM)&msgf))
2194 return FALSE;
2195 *wParam = msgf.wParam;
2196 *lParam = msgf.lParam;
2197 msgf.wParam = *wParam;
2198
2199 return TRUE;
2200 }
2201
2202 static void ME_UpdateSelectionLinkAttribute(ME_TextEditor *editor)
2203 {
2204 ME_DisplayItem *startPara, *endPara;
2205 ME_DisplayItem *prev_para;
2206 ME_Cursor *from, *to;
2207 ME_Cursor start;
2208 int nChars;
2209
2210 if (!editor->AutoURLDetect_bEnable) return;
2211
2212 ME_GetSelection(editor, &from, &to);
2213
2214 /* Find paragraph previous to the one that contains start cursor */
2215 startPara = from->pPara;
2216 prev_para = startPara->member.para.prev_para;
2217 if (prev_para->type == diParagraph) startPara = prev_para;
2218
2219 /* Find paragraph that contains end cursor */
2220 endPara = to->pPara->member.para.next_para;
2221
2222 start.pPara = startPara;
2223 start.pRun = ME_FindItemFwd(startPara, diRun);
2224 start.nOffset = 0;
2225 nChars = endPara->member.para.nCharOfs - startPara->member.para.nCharOfs;
2226
2227 ME_UpdateLinkAttribute(editor, &start, nChars);
2228 }
2229
2230 static BOOL
2231 ME_KeyDown(ME_TextEditor *editor, WORD nKey)
2232 {
2233 BOOL ctrl_is_down = GetKeyState(VK_CONTROL) & 0x8000;
2234 BOOL shift_is_down = GetKeyState(VK_SHIFT) & 0x8000;
2235
2236 if (editor->bMouseCaptured)
2237 return FALSE;
2238 if (nKey != VK_SHIFT && nKey != VK_CONTROL && nKey != VK_MENU)
2239 editor->nSelectionType = stPosition;
2240
2241 switch (nKey)
2242 {
2243 case VK_LEFT:
2244 case VK_RIGHT:
2245 case VK_HOME:
2246 case VK_END:
2247 editor->nUDArrowX = -1;
2248 /* fall through */
2249 case VK_UP:
2250 case VK_DOWN:
2251 case VK_PRIOR:
2252 case VK_NEXT:
2253 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
2254 ME_ArrowKey(editor, nKey, shift_is_down, ctrl_is_down);
2255 return TRUE;
2256 case VK_BACK:
2257 case VK_DELETE:
2258 editor->nUDArrowX = -1;
2259 /* FIXME backspace and delete aren't the same, they act different wrt paragraph style of the merged paragraph */
2260 if (editor->styleFlags & ES_READONLY)
2261 return FALSE;
2262 if (ME_IsSelection(editor))
2263 {
2264 ME_DeleteSelection(editor);
2265 ME_CommitUndo(editor);
2266 }
2267 else if (nKey == VK_DELETE)
2268 {
2269 /* Delete stops group typing.
2270 * (See MSDN remarks on EM_STOPGROUPTYPING message) */
2271 ME_DeleteTextAtCursor(editor, 1, 1);
2272 ME_CommitUndo(editor);
2273 }
2274 else if (ME_ArrowKey(editor, VK_LEFT, FALSE, FALSE))
2275 {
2276 BOOL bDeletionSucceeded;
2277 /* Backspace can be grouped for a single undo */
2278 ME_ContinueCoalescingTransaction(editor);
2279 bDeletionSucceeded = ME_DeleteTextAtCursor(editor, 1, 1);
2280 if (!bDeletionSucceeded && !editor->bEmulateVersion10) { /* v4.1 */
2281 /* Deletion was prevented so the cursor is moved back to where it was.
2282 * (e.g. this happens when trying to delete cell boundaries)
2283 */
2284 ME_ArrowKey(editor, VK_RIGHT, FALSE, FALSE);
2285 }
2286 ME_CommitCoalescingUndo(editor);
2287 }
2288 else
2289 return TRUE;
2290 ME_MoveCursorFromTableRowStartParagraph(editor);
2291 ME_UpdateSelectionLinkAttribute(editor);
2292 ME_UpdateRepaint(editor, FALSE);
2293 ME_SendRequestResize(editor, FALSE);
2294 return TRUE;
2295 case VK_RETURN:
2296 if (editor->bDialogMode)
2297 {
2298 if (ctrl_is_down)
2299 return TRUE;
2300
2301 if (!(editor->styleFlags & ES_WANTRETURN))
2302 {
2303 if (editor->hwndParent)
2304 {
2305 DWORD dw;
2306 dw = SendMessageW(editor->hwndParent, DM_GETDEFID, 0, 0);
2307 if (HIWORD(dw) == DC_HASDEFID)
2308 {
2309 HWND hwDefCtrl = GetDlgItem(editor->hwndParent, LOWORD(dw));
2310 if (hwDefCtrl)
2311 {
2312 SendMessageW(editor->hwndParent, WM_NEXTDLGCTL, (WPARAM)hwDefCtrl, TRUE);
2313 PostMessageW(hwDefCtrl, WM_KEYDOWN, VK_RETURN, 0);
2314 }
2315 }
2316 }
2317 return TRUE;
2318 }
2319 }
2320
2321 if (editor->styleFlags & ES_MULTILINE)
2322 {
2323 ME_Cursor cursor = editor->pCursors[0];
2324 ME_DisplayItem *para = cursor.pPara;
2325 int from, to;
2326 const WCHAR endl = '\r';
2327 const WCHAR endlv10[] = {'\r','\n'};
2328 ME_Style *style;
2329
2330 if (editor->styleFlags & ES_READONLY) {
2331 MessageBeep(MB_ICONERROR);
2332 return TRUE;
2333 }
2334
2335 ME_GetSelectionOfs(editor, &from, &to);
2336 if (editor->nTextLimit > ME_GetTextLength(editor) - (to-from))
2337 {
2338 if (!editor->bEmulateVersion10) { /* v4.1 */
2339 if (para->member.para.nFlags & MEPF_ROWEND) {
2340 /* Add a new table row after this row. */
2341 para = ME_AppendTableRow(editor, para);
2342 para = para->member.para.next_para;
2343 editor->pCursors[0].pPara = para;
2344 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2345 editor->pCursors[0].nOffset = 0;
2346 editor->pCursors[1] = editor->pCursors[0];
2347 ME_CommitUndo(editor);
2348 ME_CheckTablesForCorruption(editor);
2349 ME_UpdateRepaint(editor, FALSE);
2350 return TRUE;
2351 }
2352 else if (para == editor->pCursors[1].pPara &&
2353 cursor.nOffset + cursor.pRun->member.run.nCharOfs == 0 &&
2354 para->member.para.prev_para->member.para.nFlags & MEPF_ROWSTART &&
2355 !para->member.para.prev_para->member.para.nCharOfs)
2356 {
2357 /* Insert a newline before the table. */
2358 para = para->member.para.prev_para;
2359 para->member.para.nFlags &= ~MEPF_ROWSTART;
2360 editor->pCursors[0].pPara = para;
2361 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2362 editor->pCursors[1] = editor->pCursors[0];
2363 ME_InsertTextFromCursor(editor, 0, &endl, 1,
2364 editor->pCursors[0].pRun->member.run.style);
2365 para = editor->pBuffer->pFirst->member.para.next_para;
2366 ME_SetDefaultParaFormat(editor, para->member.para.pFmt);
2367 para->member.para.nFlags = MEPF_REWRAP;
2368 editor->pCursors[0].pPara = para;
2369 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2370 editor->pCursors[1] = editor->pCursors[0];
2371 para->member.para.next_para->member.para.nFlags |= MEPF_ROWSTART;
2372 ME_CommitCoalescingUndo(editor);
2373 ME_CheckTablesForCorruption(editor);
2374 ME_UpdateRepaint(editor, FALSE);
2375 return TRUE;
2376 }
2377 } else { /* v1.0 - 3.0 */
2378 ME_DisplayItem *para = cursor.pPara;
2379 if (ME_IsInTable(para))
2380 {
2381 if (cursor.pRun->member.run.nFlags & MERF_ENDPARA)
2382 {
2383 if (from == to) {
2384 ME_ContinueCoalescingTransaction(editor);
2385 para = ME_AppendTableRow(editor, para);
2386 editor->pCursors[0].pPara = para;
2387 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2388 editor->pCursors[0].nOffset = 0;
2389 editor->pCursors[1] = editor->pCursors[0];
2390 ME_CommitCoalescingUndo(editor);
2391 ME_UpdateRepaint(editor, FALSE);
2392 return TRUE;
2393 }
2394 } else {
2395 ME_ContinueCoalescingTransaction(editor);
2396 if (cursor.pRun->member.run.nCharOfs + cursor.nOffset == 0 &&
2397 !ME_IsInTable(para->member.para.prev_para))
2398 {
2399 /* Insert newline before table */
2400 cursor.pRun = ME_FindItemBack(para, diRun);
2401 if (cursor.pRun) {
2402 editor->pCursors[0].pRun = cursor.pRun;
2403 editor->pCursors[0].pPara = para->member.para.prev_para;
2404 }
2405 editor->pCursors[0].nOffset = 0;
2406 editor->pCursors[1] = editor->pCursors[0];
2407 ME_InsertTextFromCursor(editor, 0, &endl, 1,
2408 editor->pCursors[0].pRun->member.run.style);
2409 } else {
2410 editor->pCursors[1] = editor->pCursors[0];
2411 para = ME_AppendTableRow(editor, para);
2412 editor->pCursors[0].pPara = para;
2413 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2414 editor->pCursors[0].nOffset = 0;
2415 editor->pCursors[1] = editor->pCursors[0];
2416 }
2417 ME_CommitCoalescingUndo(editor);
2418 ME_UpdateRepaint(editor, FALSE);
2419 return TRUE;
2420 }
2421 }
2422 }
2423
2424 style = ME_GetInsertStyle(editor, 0);
2425 ME_SaveTempStyle(editor);
2426 ME_ContinueCoalescingTransaction(editor);
2427 if (shift_is_down)
2428 ME_InsertEndRowFromCursor(editor, 0);
2429 else
2430 if (!editor->bEmulateVersion10)
2431 ME_InsertTextFromCursor(editor, 0, &endl, 1, style);
2432 else
2433 ME_InsertTextFromCursor(editor, 0, endlv10, 2, style);
2434 ME_ReleaseStyle(style);
2435 ME_CommitCoalescingUndo(editor);
2436 SetCursor(NULL);
2437
2438 ME_UpdateSelectionLinkAttribute(editor);
2439 ME_UpdateRepaint(editor, FALSE);
2440 }
2441 return TRUE;
2442 }
2443 break;
2444 case VK_ESCAPE:
2445 if (editor->bDialogMode && editor->hwndParent)
2446 PostMessageW(editor->hwndParent, WM_CLOSE, 0, 0);
2447 return TRUE;
2448 case VK_TAB:
2449 if (editor->bDialogMode && editor->hwndParent)
2450 SendMessageW(editor->hwndParent, WM_NEXTDLGCTL, shift_is_down, 0);
2451 return TRUE;
2452 case 'A':
2453 if (ctrl_is_down)
2454 {
2455 handle_EM_EXSETSEL( editor, 0, -1 );
2456 return TRUE;
2457 }
2458 break;
2459 case 'V':
2460 if (ctrl_is_down)
2461 return ME_Paste(editor);
2462 break;
2463 case 'C':
2464 case 'X':
2465 if (ctrl_is_down)
2466 {
2467 BOOL result;
2468 int nOfs, nChars;
2469 int nStartCur = ME_GetSelectionOfs(editor, &nOfs, &nChars);
2470 ME_Cursor *selStart = &editor->pCursors[nStartCur];
2471
2472 nChars -= nOfs;
2473 result = ME_Copy(editor, selStart, nChars);
2474 if (result && nKey == 'X')
2475 {
2476 ME_InternalDeleteText(editor, selStart, nChars, FALSE);
2477 ME_CommitUndo(editor);
2478 ME_UpdateRepaint(editor, TRUE);
2479 }
2480 return result;
2481 }
2482 break;
2483 case 'Z':
2484 if (ctrl_is_down)
2485 {
2486 ME_Undo(editor);
2487 return TRUE;
2488 }
2489 break;
2490 case 'Y':
2491 if (ctrl_is_down)
2492 {
2493 ME_Redo(editor);
2494 return TRUE;
2495 }
2496 break;
2497
2498 default:
2499 if (nKey != VK_SHIFT && nKey != VK_CONTROL && nKey && nKey != VK_MENU)
2500 editor->nUDArrowX = -1;
2501 if (ctrl_is_down)
2502 {
2503 if (nKey == 'W')
2504 {
2505 CHARFORMAT2W chf;
2506 char buf[2048];
2507 chf.cbSize = sizeof(chf);
2508
2509 ME_GetSelectionCharFormat(editor, &chf);
2510 ME_DumpStyleToBuf(&chf, buf);
2511 MessageBoxA(NULL, buf, "Style dump", MB_OK);
2512 }
2513 if (nKey == 'Q')
2514 {
2515 ME_CheckCharOffsets(editor);
2516 }
2517 }
2518 }
2519 return FALSE;
2520 }
2521
2522 static LRESULT ME_Char(ME_TextEditor *editor, WPARAM charCode,
2523 LPARAM flags, BOOL unicode)
2524 {
2525 WCHAR wstr;
2526
2527 if (editor->bMouseCaptured)
2528 return 0;
2529
2530 if (unicode)
2531 wstr = (WCHAR)charCode;
2532 else
2533 {
2534 CHAR charA = charCode;
2535 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &wstr, 1);
2536 }
2537
2538 if (editor->styleFlags & ES_READONLY) {
2539 MessageBeep(MB_ICONERROR);
2540 return 0; /* FIXME really 0 ? */
2541 }
2542
2543 if ((unsigned)wstr >= ' ' || wstr == '\t')
2544 {
2545 ME_Cursor cursor = editor->pCursors[0];
2546 ME_DisplayItem *para = cursor.pPara;
2547 int from, to;
2548 BOOL ctrl_is_down = GetKeyState(VK_CONTROL) & 0x8000;
2549 ME_GetSelectionOfs(editor, &from, &to);
2550 if (wstr == '\t' &&
2551 /* v4.1 allows tabs to be inserted with ctrl key down */
2552 !(ctrl_is_down && !editor->bEmulateVersion10))
2553 {
2554 ME_DisplayItem *para;
2555 BOOL bSelectedRow = FALSE;
2556
2557 para = cursor.pPara;
2558 if (ME_IsSelection(editor) &&
2559 cursor.pRun->member.run.nCharOfs + cursor.nOffset == 0 &&
2560 to == ME_GetCursorOfs(&editor->pCursors[0]) &&
2561 para->member.para.prev_para->type == diParagraph)
2562 {
2563 para = para->member.para.prev_para;
2564 bSelectedRow = TRUE;
2565 }
2566 if (ME_IsInTable(para))
2567 {
2568 ME_TabPressedInTable(editor, bSelectedRow);
2569 ME_CommitUndo(editor);
2570 return 0;
2571 }
2572 } else if (!editor->bEmulateVersion10) { /* v4.1 */
2573 if (para->member.para.nFlags & MEPF_ROWEND) {
2574 if (from == to) {
2575 para = para->member.para.next_para;
2576 if (para->member.para.nFlags & MEPF_ROWSTART)
2577 para = para->member.para.next_para;
2578 editor->pCursors[0].pPara = para;
2579 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2580 editor->pCursors[0].nOffset = 0;
2581 editor->pCursors[1] = editor->pCursors[0];
2582 }
2583 }
2584 } else { /* v1.0 - 3.0 */
2585 if (ME_IsInTable(cursor.pRun) &&
2586 cursor.pRun->member.run.nFlags & MERF_ENDPARA &&
2587 from == to)
2588 {
2589 /* Text should not be inserted at the end of the table. */
2590 MessageBeep(-1);
2591 return 0;
2592 }
2593 }
2594 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
2595 /* WM_CHAR is restricted to nTextLimit */
2596 if(editor->nTextLimit > ME_GetTextLength(editor) - (to-from))
2597 {
2598 ME_Style *style = ME_GetInsertStyle(editor, 0);
2599 ME_SaveTempStyle(editor);
2600 ME_ContinueCoalescingTransaction(editor);
2601 ME_InsertTextFromCursor(editor, 0, &wstr, 1, style);
2602 ME_ReleaseStyle(style);
2603 ME_CommitCoalescingUndo(editor);
2604 ITextHost_TxSetCursor(editor->texthost, NULL, FALSE);
2605 }
2606
2607 ME_UpdateSelectionLinkAttribute(editor);
2608 ME_UpdateRepaint(editor, FALSE);
2609 }
2610 return 0;
2611 }
2612
2613 /* Process the message and calculate the new click count.
2614 *
2615 * returns: The click count if it is mouse down event, else returns 0. */
2616 static int ME_CalculateClickCount(ME_TextEditor *editor, UINT msg, WPARAM wParam,
2617 LPARAM lParam)
2618 {
2619 static int clickNum = 0;
2620 if (msg < WM_MOUSEFIRST || msg > WM_MOUSELAST)
2621 return 0;
2622
2623 if ((msg == WM_LBUTTONDBLCLK) ||
2624 (msg == WM_RBUTTONDBLCLK) ||
2625 (msg == WM_MBUTTONDBLCLK) ||
2626 (msg == WM_XBUTTONDBLCLK))
2627 {
2628 msg -= (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN);
2629 }
2630
2631 if ((msg == WM_LBUTTONDOWN) ||
2632 (msg == WM_RBUTTONDOWN) ||
2633 (msg == WM_MBUTTONDOWN) ||
2634 (msg == WM_XBUTTONDOWN))
2635 {
2636 static MSG prevClickMsg;
2637 MSG clickMsg;
2638 /* Compare the editor instead of the hwnd so that the this
2639 * can still be done for windowless richedit controls. */
2640 clickMsg.hwnd = (HWND)editor;
2641 clickMsg.message = msg;
2642 clickMsg.wParam = wParam;
2643 clickMsg.lParam = lParam;
2644 clickMsg.time = GetMessageTime();
2645 clickMsg.pt.x = (short)LOWORD(lParam);
2646 clickMsg.pt.y = (short)HIWORD(lParam);
2647 if ((clickNum != 0) &&
2648 (clickMsg.message == prevClickMsg.message) &&
2649 (clickMsg.hwnd == prevClickMsg.hwnd) &&
2650 (clickMsg.wParam == prevClickMsg.wParam) &&
2651 (clickMsg.time - prevClickMsg.time < GetDoubleClickTime()) &&
2652 (abs(clickMsg.pt.x - prevClickMsg.pt.x) < GetSystemMetrics(SM_CXDOUBLECLK)/2) &&
2653 (abs(clickMsg.pt.y - prevClickMsg.pt.y) < GetSystemMetrics(SM_CYDOUBLECLK)/2))
2654 {
2655 clickNum++;
2656 } else {
2657 clickNum = 1;
2658 }
2659 prevClickMsg = clickMsg;
2660 } else {
2661 return 0;
2662 }
2663 return clickNum;
2664 }
2665
2666 static BOOL ME_SetCursor(ME_TextEditor *editor)
2667 {
2668 ME_Cursor cursor;
2669 POINT pt;
2670 BOOL isExact;
2671 SCROLLBARINFO sbi;
2672 DWORD messagePos = GetMessagePos();
2673 pt.x = (short)LOWORD(messagePos);
2674 pt.y = (short)HIWORD(messagePos);
2675
2676 if (editor->hWnd)
2677 {
2678 sbi.cbSize = sizeof(sbi);
2679 GetScrollBarInfo(editor->hWnd, OBJID_HSCROLL, &sbi);
2680 if (!(sbi.rgstate[0] & (STATE_SYSTEM_INVISIBLE|STATE_SYSTEM_OFFSCREEN)) &&
2681 PtInRect(&sbi.rcScrollBar, pt))
2682 {
2683 ITextHost_TxSetCursor(editor->texthost,
2684 LoadCursorW(NULL, (WCHAR*)IDC_ARROW), FALSE);
2685 return TRUE;
2686 }
2687 sbi.cbSize = sizeof(sbi);
2688 GetScrollBarInfo(editor->hWnd, OBJID_VSCROLL, &sbi);
2689 if (!(sbi.rgstate[0] & (STATE_SYSTEM_INVISIBLE|STATE_SYSTEM_OFFSCREEN)) &&
2690 PtInRect(&sbi.rcScrollBar, pt))
2691 {
2692 ITextHost_TxSetCursor(editor->texthost,
2693 LoadCursorW(NULL, (WCHAR*)IDC_ARROW), FALSE);
2694 return TRUE;
2695 }
2696 }
2697 ITextHost_TxScreenToClient(editor->texthost, &pt);
2698
2699 if (editor->nSelectionType == stLine && editor->bMouseCaptured) {
2700 ITextHost_TxSetCursor(editor->texthost, hLeft, FALSE);
2701 return TRUE;
2702 }
2703 if (!editor->bEmulateVersion10 /* v4.1 */ &&
2704 pt.y < editor->rcFormat.top &&
2705 pt.x < editor->rcFormat.left)
2706 {
2707 ITextHost_TxSetCursor(editor->texthost, hLeft, FALSE);
2708 return TRUE;
2709 }
2710 if (pt.y < editor->rcFormat.top || pt.y > editor->rcFormat.bottom)
2711 {
2712 if (editor->bEmulateVersion10) /* v1.0 - 3.0 */
2713 ITextHost_TxSetCursor(editor->texthost,
2714 LoadCursorW(NULL, (WCHAR*)IDC_ARROW), FALSE);
2715 else /* v4.1 */
2716 ITextHost_TxSetCursor(editor->texthost,
2717 LoadCursorW(NULL, (WCHAR*)IDC_IBEAM), TRUE);
2718 return TRUE;
2719 }
2720 if (pt.x < editor->rcFormat.left)
2721 {
2722 ITextHost_TxSetCursor(editor->texthost, hLeft, FALSE);
2723 return TRUE;
2724 }
2725 ME_CharFromPos(editor, pt.x, pt.y, &cursor, &isExact);
2726 if (isExact)
2727 {
2728 ME_Run *run;
2729
2730 run = &cursor.pRun->member.run;
2731 if (run->style->fmt.dwMask & CFM_LINK &&
2732 run->style->fmt.dwEffects & CFE_LINK)
2733 {
2734 ITextHost_TxSetCursor(editor->texthost,
2735 LoadCursorW(NULL, (WCHAR*)IDC_HAND),
2736 FALSE);
2737 return TRUE;
2738 }
2739
2740 if (ME_IsSelection(editor))
2741 {
2742 int selStart, selEnd;
2743 int offset = ME_GetCursorOfs(&cursor);
2744
2745 ME_GetSelectionOfs(editor, &selStart, &selEnd);
2746 if (selStart <= offset && selEnd >= offset) {
2747 ITextHost_TxSetCursor(editor->texthost,
2748 LoadCursorW(NULL, (WCHAR*)IDC_ARROW),
2749 FALSE);
2750 return TRUE;
2751 }
2752 }
2753 }
2754 ITextHost_TxSetCursor(editor->texthost,
2755 LoadCursorW(NULL, (WCHAR*)IDC_IBEAM), TRUE);
2756 return TRUE;
2757 }
2758
2759 static void ME_SetDefaultFormatRect(ME_TextEditor *editor)
2760 {
2761 ITextHost_TxGetClientRect(editor->texthost, &editor->rcFormat);
2762 editor->rcFormat.top += editor->exStyleFlags & WS_EX_CLIENTEDGE ? 1 : 0;
2763 editor->rcFormat.left += 1 + editor->selofs;
2764 editor->rcFormat.right -= 1;
2765 }
2766
2767 static BOOL ME_ShowContextMenu(ME_TextEditor *editor, int x, int y)
2768 {
2769 CHARRANGE selrange;
2770 HMENU menu;
2771 int seltype = 0;
2772 if(!editor->lpOleCallback || !editor->hWnd)
2773 return FALSE;
2774 ME_GetSelectionOfs(editor, &selrange.cpMin, &selrange.cpMax);
2775 if(selrange.cpMin == selrange.cpMax)
2776 seltype |= SEL_EMPTY;
2777 else
2778 {
2779 /* FIXME: Handle objects */
2780 seltype |= SEL_TEXT;
2781 if(selrange.cpMax-selrange.cpMin > 1)
2782 seltype |= SEL_MULTICHAR;
2783 }
2784 if(SUCCEEDED(IRichEditOleCallback_GetContextMenu(editor->lpOleCallback, seltype, NULL, &selrange, &menu)))
2785 {
2786 TrackPopupMenu(menu, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, editor->hwndParent, NULL);
2787 DestroyMenu(menu);
2788 }
2789 return TRUE;
2790 }
2791
2792 ME_TextEditor *ME_MakeEditor(ITextHost *texthost, BOOL bEmulateVersion10, DWORD csStyle)
2793 {
2794 ME_TextEditor *ed = ALLOC_OBJ(ME_TextEditor);
2795 int i;
2796 DWORD props;
2797 LONG selbarwidth;
2798
2799 ed->hWnd = NULL;
2800 ed->hwndParent = NULL;
2801 ed->sizeWindow.cx = ed->sizeWindow.cy = 0;
2802 ed->texthost = texthost;
2803 ed->reOle = NULL;
2804 ed->bEmulateVersion10 = bEmulateVersion10;
2805 ed->styleFlags = 0;
2806 ed->alignStyle = PFA_LEFT;
2807 if (csStyle & ES_RIGHT)
2808 ed->alignStyle = PFA_RIGHT;
2809 if (csStyle & ES_CENTER)
2810 ed->alignStyle = PFA_CENTER;
2811 ITextHost_TxGetPropertyBits(texthost,
2812 (TXTBIT_RICHTEXT|TXTBIT_MULTILINE|
2813 TXTBIT_READONLY|TXTBIT_USEPASSWORD|
2814 TXTBIT_HIDESELECTION|TXTBIT_SAVESELECTION|
2815 TXTBIT_AUTOWORDSEL|TXTBIT_VERTICAL|
2816 TXTBIT_WORDWRAP|TXTBIT_DISABLEDRAG),
2817 &props);
2818 ITextHost_TxGetScrollBars(texthost, &ed->styleFlags);
2819 ed->styleFlags &= (WS_VSCROLL|WS_HSCROLL|ES_AUTOVSCROLL|
2820 ES_AUTOHSCROLL|ES_DISABLENOSCROLL);
2821 ed->pBuffer = ME_MakeText();
2822 ed->nZoomNumerator = ed->nZoomDenominator = 0;
2823 ed->nAvailWidth = 0; /* wrap to client area */
2824 ME_MakeFirstParagraph(ed);
2825 /* The four cursors are for:
2826 * 0 - The position where the caret is shown
2827 * 1 - The anchored end of the selection (for normal selection)
2828 * 2 & 3 - The anchored start and end respectively for word, line,
2829 * or paragraph selection.
2830 */
2831 ed->nCursors = 4;
2832 ed->pCursors = ALLOC_N_OBJ(ME_Cursor, ed->nCursors);
2833 ME_SetCursorToStart(ed, &ed->pCursors[0]);
2834 ed->pCursors[1] = ed->pCursors[0];
2835 ed->pCursors[2] = ed->pCursors[0];
2836 ed->pCursors[3] = ed->pCursors[1];
2837 ed->nLastTotalLength = ed->nTotalLength = 0;
2838 ed->nLastTotalWidth = ed->nTotalWidth = 0;
2839 ed->nUDArrowX = -1;
2840 ed->rgbBackColor = -1;
2841 ed->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
2842 ed->bCaretAtEnd = FALSE;
2843 ed->nEventMask = 0;
2844 ed->nModifyStep = 0;
2845 ed->nTextLimit = TEXT_LIMIT_DEFAULT;
2846 list_init( &ed->undo_stack );
2847 list_init( &ed->redo_stack );
2848 ed->nUndoStackSize = 0;
2849 ed->nUndoLimit = STACK_SIZE_DEFAULT;
2850 ed->nUndoMode = umAddToUndo;
2851 ed->nParagraphs = 1;
2852 ed->nLastSelStart = ed->nLastSelEnd = 0;
2853 ed->pLastSelStartPara = ed->pLastSelEndPara = ed->pCursors[0].pPara;
2854 ed->bHideSelection = FALSE;
2855 ed->pfnWordBreak = NULL;
2856 ed->lpOleCallback = NULL;
2857 ed->mode = TM_MULTILEVELUNDO | TM_MULTICODEPAGE;
2858 ed->mode |= (props & TXTBIT_RICHTEXT) ? TM_RICHTEXT : TM_PLAINTEXT;
2859 ed->AutoURLDetect_bEnable = FALSE;
2860 ed->bHaveFocus = FALSE;
2861 ed->bDialogMode = FALSE;
2862 ed->bMouseCaptured = FALSE;
2863 for (i=0; i<HFONT_CACHE_SIZE; i++)
2864 {
2865 ed->pFontCache[i].nRefs = 0;
2866 ed->pFontCache[i].nAge = 0;
2867 ed->pFontCache[i].hFont = NULL;
2868 }
2869
2870 ME_CheckCharOffsets(ed);
2871 ed->bDefaultFormatRect = TRUE;
2872 ITextHost_TxGetSelectionBarWidth(ed->texthost, &selbarwidth);
2873 if (selbarwidth) {
2874 /* FIXME: Convert selbarwidth from HIMETRIC to pixels */
2875 ed->selofs = SELECTIONBAR_WIDTH;
2876 ed->styleFlags |= ES_SELECTIONBAR;
2877 } else {
2878 ed->selofs = 0;
2879 }
2880 ed->nSelectionType = stPosition;
2881
2882 ed->cPasswordMask = 0;
2883 if (props & TXTBIT_USEPASSWORD)
2884 ITextHost_TxGetPasswordChar(texthost, &ed->cPasswordMask);
2885
2886 if (props & TXTBIT_AUTOWORDSEL)
2887 ed->styleFlags |= ECO_AUTOWORDSELECTION;
2888 if (props & TXTBIT_MULTILINE) {
2889 ed->styleFlags |= ES_MULTILINE;
2890 ed->bWordWrap = (props & TXTBIT_WORDWRAP) != 0;
2891 } else {
2892 ed->bWordWrap = FALSE;
2893 }
2894 if (props & TXTBIT_READONLY)
2895 ed->styleFlags |= ES_READONLY;
2896 if (!(props & TXTBIT_HIDESELECTION))
2897 ed->styleFlags |= ES_NOHIDESEL;
2898 if (props & TXTBIT_SAVESELECTION)
2899 ed->styleFlags |= ES_SAVESEL;
2900 if (props & TXTBIT_VERTICAL)
2901 ed->styleFlags |= ES_VERTICAL;
2902 if (props & TXTBIT_DISABLEDRAG)
2903 ed->styleFlags |= ES_NOOLEDRAGDROP;
2904
2905 ed->notified_cr.cpMin = ed->notified_cr.cpMax = 0;
2906
2907 /* Default scrollbar information */
2908 ed->vert_si.cbSize = sizeof(SCROLLINFO);
2909 ed->vert_si.nMin = 0;
2910 ed->vert_si.nMax = 0;
2911 ed->vert_si.nPage = 0;
2912 ed->vert_si.nPos = 0;
2913
2914 ed->horz_si.cbSize = sizeof(SCROLLINFO);
2915 ed->horz_si.nMin = 0;
2916 ed->horz_si.nMax = 0;
2917 ed->horz_si.nPage = 0;
2918 ed->horz_si.nPos = 0;
2919
2920 ed->wheel_remain = 0;
2921
2922 list_init( &ed->style_list );
2923 OleInitialize(NULL);
2924
2925 return ed;
2926 }
2927
2928 void ME_DestroyEditor(ME_TextEditor *editor)
2929 {
2930 ME_DisplayItem *pFirst = editor->pBuffer->pFirst;
2931 ME_DisplayItem *p = pFirst, *pNext = NULL;
2932 ME_Style *s, *cursor2;
2933 int i;
2934
2935 ME_ClearTempStyle(editor);
2936 ME_EmptyUndoStack(editor);
2937 while(p) {
2938 pNext = p->next;
2939 ME_DestroyDisplayItem(p);
2940 p = pNext;
2941 }
2942
2943 LIST_FOR_EACH_ENTRY_SAFE( s, cursor2, &editor->style_list, ME_Style, entry )
2944 ME_DestroyStyle( s );
2945
2946 ME_ReleaseStyle(editor->pBuffer->pDefaultStyle);
2947 for (i=0; i<HFONT_CACHE_SIZE; i++)
2948 {
2949 if (editor->pFontCache[i].hFont)
2950 DeleteObject(editor->pFontCache[i].hFont);
2951 }
2952 if (editor->rgbBackColor != -1)
2953 DeleteObject(editor->hbrBackground);
2954 if(editor->lpOleCallback)
2955 IRichEditOleCallback_Release(editor->lpOleCallback);
2956 ITextHost_Release(editor->texthost);
2957 if (editor->reOle)
2958 {
2959 IRichEditOle_Release(editor->reOle);
2960 editor->reOle = NULL;
2961 }
2962 OleUninitialize();
2963
2964 FREE_OBJ(editor->pBuffer);
2965 FREE_OBJ(editor->pCursors);
2966
2967 FREE_OBJ(editor);
2968 }
2969
2970 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
2971 {
2972 TRACE("\n");
2973 switch (fdwReason)
2974 {
2975 case DLL_PROCESS_ATTACH:
2976 DisableThreadLibraryCalls(hinstDLL);
2977 me_heap = HeapCreate (0, 0x10000, 0);
2978 if (!ME_RegisterEditorClass(hinstDLL)) return FALSE;
2979 hLeft = LoadCursorW(hinstDLL, MAKEINTRESOURCEW(OCR_REVERSE));
2980 LookupInit();
2981 break;
2982
2983 case DLL_PROCESS_DETACH:
2984 if (lpvReserved) break;
2985 UnregisterClassW(RICHEDIT_CLASS20W, 0);
2986 UnregisterClassW(MSFTEDIT_CLASS, 0);
2987 UnregisterClassA(RICHEDIT_CLASS20A, 0);
2988 UnregisterClassA("RichEdit50A", 0);
2989 if (ME_ListBoxRegistered)
2990 UnregisterClassW(REListBox20W, 0);
2991 if (ME_ComboBoxRegistered)
2992 UnregisterClassW(REComboBox20W, 0);
2993 LookupCleanup();
2994 HeapDestroy (me_heap);
2995 release_typelib();
2996 break;
2997 }
2998 return TRUE;
2999 }
3000
3001 static inline int get_default_line_height( ME_TextEditor *editor )
3002 {
3003 int height = 0;
3004
3005 if (editor->pBuffer && editor->pBuffer->pDefaultStyle)
3006 height = editor->pBuffer->pDefaultStyle->tm.tmHeight;
3007 if (height <= 0) height = 24;
3008
3009 return height;
3010 }
3011
3012 static inline int calc_wheel_change( int *remain, int amount_per_click )
3013 {
3014 int change = amount_per_click * (float)*remain / WHEEL_DELTA;
3015 *remain -= WHEEL_DELTA * change / amount_per_click;
3016 return change;
3017 }
3018
3019 static const char * const edit_messages[] = {
3020 "EM_GETSEL",
3021 "EM_SETSEL",
3022 "EM_GETRECT",
3023 "EM_SETRECT",
3024 "EM_SETRECTNP",
3025 "EM_SCROLL",
3026 "EM_LINESCROLL",
3027 "EM_SCROLLCARET",
3028 "EM_GETMODIFY",
3029 "EM_SETMODIFY",
3030 "EM_GETLINECOUNT",
3031 "EM_LINEINDEX",
3032 "EM_SETHANDLE",
3033 "EM_GETHANDLE",
3034 "EM_GETTHUMB",
3035 "EM_UNKNOWN_BF",
3036 "EM_UNKNOWN_C0",
3037 "EM_LINELENGTH",
3038 "EM_REPLACESEL",
3039 "EM_UNKNOWN_C3",
3040 "EM_GETLINE",
3041 "EM_LIMITTEXT",
3042 "EM_CANUNDO",
3043 "EM_UNDO",
3044 "EM_FMTLINES",
3045 "EM_LINEFROMCHAR",
3046 "EM_UNKNOWN_CA",
3047 "EM_SETTABSTOPS",
3048 "EM_SETPASSWORDCHAR",
3049 "EM_EMPTYUNDOBUFFER",
3050 "EM_GETFIRSTVISIBLELINE",
3051 "EM_SETREADONLY",
3052 "EM_SETWORDBREAKPROC",
3053 "EM_GETWORDBREAKPROC",
3054 "EM_GETPASSWORDCHAR",
3055 "EM_SETMARGINS",
3056 "EM_GETMARGINS",
3057 "EM_GETLIMITTEXT",
3058 "EM_POSFROMCHAR",
3059 "EM_CHARFROMPOS",
3060 "EM_SETIMESTATUS",
3061 "EM_GETIMESTATUS"
3062 };
3063
3064 static const char * const richedit_messages[] = {
3065 "EM_CANPASTE",
3066 "EM_DISPLAYBAND",
3067 "EM_EXGETSEL",
3068 "EM_EXLIMITTEXT",
3069 "EM_EXLINEFROMCHAR",
3070 "EM_EXSETSEL",
3071 "EM_FINDTEXT",
3072 "EM_FORMATRANGE",
3073 "EM_GETCHARFORMAT",
3074 "EM_GETEVENTMASK",
3075 "EM_GETOLEINTERFACE",
3076 "EM_GETPARAFORMAT",
3077 "EM_GETSELTEXT",
3078 "EM_HIDESELECTION",
3079 "EM_PASTESPECIAL",
3080 "EM_REQUESTRESIZE",
3081 "EM_SELECTIONTYPE",
3082 "EM_SETBKGNDCOLOR",
3083 "EM_SETCHARFORMAT",
3084 "EM_SETEVENTMASK",
3085 "EM_SETOLECALLBACK",
3086 "EM_SETPARAFORMAT",
3087 "EM_SETTARGETDEVICE",
3088 "EM_STREAMIN",
3089 "EM_STREAMOUT",
3090 "EM_GETTEXTRANGE",
3091 "EM_FINDWORDBREAK",
3092 "EM_SETOPTIONS",
3093 "EM_GETOPTIONS",
3094 "EM_FINDTEXTEX",
3095 "EM_GETWORDBREAKPROCEX",
3096 "EM_SETWORDBREAKPROCEX",
3097 "EM_SETUNDOLIMIT",
3098 "EM_UNKNOWN_USER_83",
3099 "EM_REDO",
3100 "EM_CANREDO",
3101 "EM_GETUNDONAME",
3102 "EM_GETREDONAME",
3103 "EM_STOPGROUPTYPING",
3104 "EM_SETTEXTMODE",
3105 "EM_GETTEXTMODE",
3106 "EM_AUTOURLDETECT",
3107 "EM_GETAUTOURLDETECT",
3108 "EM_SETPALETTE",
3109 "EM_GETTEXTEX",
3110 "EM_GETTEXTLENGTHEX",
3111 "EM_SHOWSCROLLBAR",
3112 "EM_SETTEXTEX",
3113 "EM_UNKNOWN_USER_98",
3114 "EM_UNKNOWN_USER_99",
3115 "EM_SETPUNCTUATION",
3116 "EM_GETPUNCTUATION",
3117 "EM_SETWORDWRAPMODE",
3118 "EM_GETWORDWRAPMODE",
3119 "EM_SETIMECOLOR",
3120 "EM_GETIMECOLOR",
3121 "EM_SETIMEOPTIONS",
3122 "EM_GETIMEOPTIONS",
3123 "EM_CONVPOSITION",
3124 "EM_UNKNOWN_USER_109",
3125 "EM_UNKNOWN_USER_110",
3126 "EM_UNKNOWN_USER_111",
3127 "EM_UNKNOWN_USER_112",
3128 "EM_UNKNOWN_USER_113",
3129 "EM_UNKNOWN_USER_114",
3130 "EM_UNKNOWN_USER_115",
3131 "EM_UNKNOWN_USER_116",
3132 "EM_UNKNOWN_USER_117",
3133 "EM_UNKNOWN_USER_118",
3134 "EM_UNKNOWN_USER_119",
3135 "EM_SETLANGOPTIONS",
3136 "EM_GETLANGOPTIONS",
3137 "EM_GETIMECOMPMODE",
3138 "EM_FINDTEXTW",
3139 "EM_FINDTEXTEXW",
3140 "EM_RECONVERSION",
3141 "EM_SETIMEMODEBIAS",
3142 "EM_GETIMEMODEBIAS"
3143 };
3144
3145 static const char *
3146 get_msg_name(UINT msg)
3147 {
3148 if (msg >= EM_GETSEL && msg <= EM_CHARFROMPOS)
3149 return edit_messages[msg - EM_GETSEL];
3150 if (msg >= EM_CANPASTE && msg <= EM_GETIMEMODEBIAS)
3151 return richedit_messages[msg - EM_CANPASTE];
3152 return "";
3153 }
3154
3155 static void ME_LinkNotify(ME_TextEditor *editor, UINT msg, WPARAM wParam, LPARAM lParam)
3156 {
3157 int x,y;
3158 BOOL isExact;
3159 ME_Cursor cursor; /* The start of the clicked text. */
3160
3161 ENLINK info;
3162 x = (short)LOWORD(lParam);
3163 y = (short)HIWORD(lParam);
3164 ME_CharFromPos(editor, x, y, &cursor, &isExact);
3165 if (!isExact) return;
3166
3167 if (cursor.pRun->member.run.style->fmt.dwMask & CFM_LINK &&
3168 cursor.pRun->member.run.style->fmt.dwEffects & CFE_LINK)
3169 { /* The clicked run has CFE_LINK set */
3170 ME_DisplayItem *di;
3171
3172 info.nmhdr.hwndFrom = NULL;
3173 info.nmhdr.idFrom = 0;
3174 info.nmhdr.code = EN_LINK;
3175 info.msg = msg;
3176 info.wParam = wParam;
3177 info.lParam = lParam;
3178 cursor.nOffset = 0;
3179
3180 /* find the first contiguous run with CFE_LINK set */
3181 info.chrg.cpMin = ME_GetCursorOfs(&cursor);
3182 for (di = cursor.pRun->prev;
3183 di && di->type == diRun && (di->member.run.style->fmt.dwMask & CFM_LINK) && (di->member.run.style->fmt.dwEffects & CFE_LINK);
3184 di = di->prev)
3185 {
3186 info.chrg.cpMin -= di->member.run.len;
3187 }
3188
3189 /* find the last contiguous run with CFE_LINK set */
3190 info.chrg.cpMax = ME_GetCursorOfs(&cursor) + cursor.pRun->member.run.len;
3191 for (di = cursor.pRun->next;
3192 di && di->type == diRun && (di->member.run.style->fmt.dwMask & CFM_LINK) && (di->member.run.style->fmt.dwEffects & CFE_LINK);
3193 di = di->next)
3194 {
3195 info.chrg.cpMax += di->member.run.len;
3196 }
3197
3198 ITextHost_TxNotify(editor->texthost, info.nmhdr.code, &info);
3199 }
3200 }
3201
3202 void ME_ReplaceSel(ME_TextEditor *editor, BOOL can_undo, const WCHAR *str, int len)
3203 {
3204 int from, to, nStartCursor;
3205 ME_Style *style;
3206
3207 nStartCursor = ME_GetSelectionOfs(editor, &from, &to);
3208 style = ME_GetSelectionInsertStyle(editor);
3209 ME_InternalDeleteText(editor, &editor->pCursors[nStartCursor], to-from, FALSE);
3210 ME_InsertTextFromCursor(editor, 0, str, len, style);
3211 ME_ReleaseStyle(style);
3212 /* drop temporary style if line end */
3213 /*
3214 * FIXME question: does abc\n mean: put abc,
3215 * clear temp style, put \n? (would require a change)
3216 */
3217 if (len>0 && str[len-1] == '\n')
3218 ME_ClearTempStyle(editor);
3219 ME_CommitUndo(editor);
3220 ME_UpdateSelectionLinkAttribute(editor);
3221 if (!can_undo)
3222 ME_EmptyUndoStack(editor);
3223 ME_UpdateRepaint(editor, FALSE);
3224 }
3225
3226 static void ME_SetText(ME_TextEditor *editor, void *text, BOOL unicode)
3227 {
3228 LONG codepage = unicode ? CP_UNICODE : CP_ACP;
3229 int textLen;
3230
3231 LPWSTR wszText = ME_ToUnicode(codepage, text, &textLen);
3232
3233 if (textLen > 0)
3234 {
3235 int len = -1;
3236
3237 /* uses default style! */
3238 if (!(editor->styleFlags & ES_MULTILINE))
3239 {
3240 WCHAR *p = wszText;
3241
3242 while (*p != '\0' && *p != '\r' && *p != '\n') p++;
3243 len = p - wszText;
3244 }
3245 ME_InsertTextFromCursor(editor, 0, wszText, len, editor->pBuffer->pDefaultStyle);
3246 }
3247 ME_EndToUnicode(codepage, wszText);
3248 }
3249
3250 static LRESULT ME_WmCreate(ME_TextEditor *editor, LPARAM lParam, BOOL unicode)
3251 {
3252 CREATESTRUCTW *createW = (CREATESTRUCTW*)lParam;
3253 CREATESTRUCTA *createA = (CREATESTRUCTA*)lParam;
3254 void *text = NULL;
3255 INT max;
3256
3257 if (lParam)
3258 text = unicode ? (void*)createW->lpszName : (void*)createA->lpszName;
3259
3260 ME_SetDefaultFormatRect(editor);
3261
3262 max = (editor->styleFlags & ES_DISABLENOSCROLL) ? 1 : 0;
3263 if (~editor->styleFlags & ES_DISABLENOSCROLL || editor->styleFlags & WS_VSCROLL)
3264 ITextHost_TxSetScrollRange(editor->texthost, SB_VERT, 0, max, TRUE);
3265
3266 if (~editor->styleFlags & ES_DISABLENOSCROLL || editor->styleFlags & WS_HSCROLL)
3267 ITextHost_TxSetScrollRange(editor->texthost, SB_HORZ, 0, max, TRUE);
3268
3269 if (editor->styleFlags & ES_DISABLENOSCROLL)
3270 {
3271 if (editor->styleFlags & WS_VSCROLL)
3272 {
3273 ITextHost_TxEnableScrollBar(editor->texthost, SB_VERT, ESB_DISABLE_BOTH);
3274 ITextHost_TxShowScrollBar(editor->texthost, SB_VERT, TRUE);
3275 }
3276 if (editor->styleFlags & WS_HSCROLL)
3277 {
3278 ITextHost_TxEnableScrollBar(editor->texthost, SB_HORZ, ESB_DISABLE_BOTH);
3279 ITextHost_TxShowScrollBar(editor->texthost, SB_HORZ, TRUE);
3280 }
3281 }
3282
3283 if (text)
3284 {
3285 ME_SetText(editor, text, unicode);
3286 ME_SetCursorToStart(editor, &editor->pCursors[0]);
3287 ME_SetCursorToStart(editor, &editor->pCursors[1]);
3288 }
3289
3290 ME_CommitUndo(editor);
3291 ME_WrapMarkedParagraphs(editor);
3292 ME_MoveCaret(editor);
3293 return 0;
3294 }
3295
3296
3297 #define UNSUPPORTED_MSG(e) \
3298 case e: \
3299 FIXME(#e ": stub\n"); \
3300 *phresult = S_FALSE; \
3301 return 0;
3302
3303 /* Handle messages for windowless and windowed richedit controls.
3304 *
3305 * The LRESULT that is returned is a return value for window procs,
3306 * and the phresult parameter is the COM return code needed by the
3307 * text services interface. */
3308 LRESULT ME_HandleMessage(ME_TextEditor *editor, UINT msg, WPARAM wParam,
3309 LPARAM lParam, BOOL unicode, HRESULT* phresult)
3310 {
3311 *phresult = S_OK;
3312
3313 switch(msg) {
3314
3315 UNSUPPORTED_MSG(EM_DISPLAYBAND)
3316 UNSUPPORTED_MSG(EM_FINDWORDBREAK)
3317 UNSUPPORTED_MSG(EM_FMTLINES)
3318 UNSUPPORTED_MSG(EM_FORMATRANGE)
3319 UNSUPPORTED_MSG(EM_GETBIDIOPTIONS)
3320 UNSUPPORTED_MSG(EM_GETEDITSTYLE)
3321 UNSUPPORTED_MSG(EM_GETIMECOMPMODE)
3322 UNSUPPORTED_MSG(EM_GETIMESTATUS)
3323 UNSUPPORTED_MSG(EM_SETIMESTATUS)
3324 UNSUPPORTED_MSG(EM_GETLANGOPTIONS)
3325 UNSUPPORTED_MSG(EM_GETREDONAME)
3326 UNSUPPORTED_MSG(EM_GETTYPOGRAPHYOPTIONS)
3327 UNSUPPORTED_MSG(EM_GETUNDONAME)
3328 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX)
3329 UNSUPPORTED_MSG(EM_PASTESPECIAL)
3330 UNSUPPORTED_MSG(EM_SELECTIONTYPE)
3331 UNSUPPORTED_MSG(EM_SETBIDIOPTIONS)
3332 UNSUPPORTED_MSG(EM_SETEDITSTYLE)
3333 UNSUPPORTED_MSG(EM_SETLANGOPTIONS)
3334 UNSUPPORTED_MSG(EM_SETMARGINS)
3335 UNSUPPORTED_MSG(EM_SETPALETTE)
3336 UNSUPPORTED_MSG(EM_SETTABSTOPS)
3337 UNSUPPORTED_MSG(EM_SETTYPOGRAPHYOPTIONS)
3338 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX)
3339
3340 /* Messages specific to Richedit controls */
3341
3342 case EM_STREAMIN:
3343 return ME_StreamIn(editor, wParam, (EDITSTREAM*)lParam, TRUE);
3344 case EM_STREAMOUT:
3345 return ME_StreamOut(editor, wParam, (EDITSTREAM *)lParam);
3346 case WM_GETDLGCODE:
3347 {
3348 UINT code = DLGC_WANTCHARS|DLGC_WANTTAB|DLGC_WANTARROWS;
3349
3350 if (lParam)
3351 editor->bDialogMode = TRUE;
3352 if (editor->styleFlags & ES_MULTILINE)
3353 code |= DLGC_WANTMESSAGE;
3354 if (!(editor->styleFlags & ES_SAVESEL))
3355 code |= DLGC_HASSETSEL;
3356 return code;
3357 }
3358 case EM_EMPTYUNDOBUFFER:
3359 ME_EmptyUndoStack(editor);
3360 return 0;
3361 case EM_GETSEL:
3362 {
3363 /* Note: wParam/lParam can be NULL */
3364 UINT from, to;
3365 PUINT pfrom = wParam ? (PUINT)wParam : &from;
3366 PUINT pto = lParam ? (PUINT)lParam : &to;
3367 ME_GetSelectionOfs(editor, (int *)pfrom, (int *)pto);
3368 if ((*pfrom|*pto) & 0xFFFF0000)
3369 return -1;
3370 return MAKELONG(*pfrom,*pto);
3371 }
3372 case EM_EXGETSEL:
3373 {
3374 CHARRANGE *pRange = (CHARRANGE *)lParam;
3375 ME_GetSelectionOfs(editor, &pRange->cpMin, &pRange->cpMax);
3376 TRACE("EM_EXGETSEL = (%d,%d)\n", pRange->cpMin, pRange->cpMax);
3377 return 0;
3378 }
3379 case EM_SETUNDOLIMIT:
3380 {
3381 if ((int)wParam < 0)
3382 editor->nUndoLimit = STACK_SIZE_DEFAULT;
3383 else
3384 editor->nUndoLimit = min(wParam, STACK_SIZE_MAX);
3385 /* Setting a max stack size keeps wine from getting killed
3386 for hogging memory. Windows allocates all this memory at once, so
3387 no program would realistically set a value above our maximum. */
3388 return editor->nUndoLimit;
3389 }
3390 case EM_CANUNDO:
3391 return !list_empty( &editor->undo_stack );
3392 case EM_CANREDO:
3393 return !list_empty( &editor->redo_stack );
3394 case WM_UNDO: /* FIXME: actually not the same */
3395 case EM_UNDO:
3396 return ME_Undo(editor);
3397 case EM_REDO:
3398 return ME_Redo(editor);
3399 case EM_GETOPTIONS:
3400 {
3401 /* these flags are equivalent to the ES_* counterparts */
3402 DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
3403 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN | ECO_SELECTIONBAR;
3404 DWORD settings = editor->styleFlags & mask;
3405
3406 return settings;
3407 }
3408 case EM_SETFONTSIZE:
3409 {
3410 CHARFORMAT2W cf;
3411 LONG tmp_size, size;
3412 BOOL is_increase = ((LONG)wParam > 0);
3413
3414 if (editor->mode & TM_PLAINTEXT)
3415 return FALSE;
3416
3417 cf.cbSize = sizeof(cf);
3418 cf.dwMask = CFM_SIZE;
3419 ME_GetSelectionCharFormat(editor, &cf);
3420 tmp_size = (cf.yHeight / 20) + wParam;
3421
3422 if (tmp_size <= 1)
3423 size = 1;
3424 else if (tmp_size > 12 && tmp_size < 28 && tmp_size % 2)
3425 size = tmp_size + (is_increase ? 1 : -1);
3426 else if (tmp_size > 28 && tmp_size < 36)
3427 size = is_increase ? 36 : 28;
3428 else if (tmp_size > 36 && tmp_size < 48)
3429 size = is_increase ? 48 : 36;
3430 else if (tmp_size > 48 && tmp_size < 72)
3431 size = is_increase ? 72 : 48;
3432 else if (tmp_size > 72 && tmp_size < 80)
3433 size = is_increase ? 80 : 72;
3434 else if (tmp_size > 80 && tmp_size < 1638)
3435 size = 10 * (is_increase ? (tmp_size / 10 + 1) : (tmp_size / 10));
3436 else if (tmp_size >= 1638)
3437 size = 1638;
3438 else
3439 size = tmp_size;
3440
3441 cf.yHeight = size * 20; /* convert twips to points */
3442 ME_SetSelectionCharFormat(editor, &cf);
3443 ME_CommitUndo(editor);
3444 ME_WrapMarkedParagraphs(editor);
3445 ME_UpdateScrollBar(editor);
3446 ME_Repaint(editor);
3447
3448 return TRUE;
3449 }
3450 case EM_SETOPTIONS:
3451 {
3452 /* these flags are equivalent to ES_* counterparts, except for
3453 * ECO_AUTOWORDSELECTION that doesn't have an ES_* counterpart,
3454 * but is still stored in editor->styleFlags. */
3455 const DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
3456 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN |
3457 ECO_SELECTIONBAR | ECO_AUTOWORDSELECTION;
3458 DWORD settings = mask & editor->styleFlags;
3459 DWORD oldSettings = settings;
3460 DWORD changedSettings;
3461
3462 switch(wParam)
3463 {
3464 case ECOOP_SET:
3465 settings = lParam;
3466 break;
3467 case ECOOP_OR:
3468 settings |= lParam;
3469 break;
3470 case ECOOP_AND:
3471 settings &= lParam;
3472 break;
3473 case ECOOP_XOR:
3474 settings ^= lParam;
3475 }
3476 changedSettings = oldSettings ^ settings;
3477
3478 if (changedSettings) {
3479 editor->styleFlags = (editor->styleFlags & ~mask) | (settings & mask);
3480
3481 if (changedSettings & ECO_SELECTIONBAR)
3482 {
3483 ITextHost_TxInvalidateRect(editor->texthost, &editor->rcFormat, TRUE);
3484 if (settings & ECO_SELECTIONBAR) {
3485 assert(!editor->selofs);
3486 editor->selofs = SELECTIONBAR_WIDTH;
3487 editor->rcFormat.left += editor->selofs;
3488 } else {
3489 editor->rcFormat.left -= editor->selofs;
3490 editor->selofs = 0;
3491 }
3492 ME_RewrapRepaint(editor);
3493 }
3494
3495 if (changedSettings & settings & ECO_VERTICAL)
3496 FIXME("ECO_VERTICAL not implemented yet!\n");
3497 if (changedSettings & settings & ECO_AUTOHSCROLL)
3498 FIXME("ECO_AUTOHSCROLL not implemented yet!\n");
3499 if (changedSettings & settings & ECO_AUTOVSCROLL)
3500 FIXME("ECO_AUTOVSCROLL not implemented yet!\n");
3501 if (changedSettings & settings & ECO_NOHIDESEL)
3502 FIXME("ECO_NOHIDESEL not implemented yet!\n");
3503 if (changedSettings & settings & ECO_WANTRETURN)
3504 FIXME("ECO_WANTRETURN not implemented yet!\n");
3505 if (changedSettings & settings & ECO_AUTOWORDSELECTION)
3506 FIXME("ECO_AUTOWORDSELECTION not implemented yet!\n");
3507 }
3508
3509 return settings;
3510 }
3511 case EM_SETSEL:
3512 {
3513 return handle_EM_EXSETSEL( editor, wParam, lParam );
3514 }
3515 case EM_SETSCROLLPOS:
3516 {
3517 POINT *point = (POINT *)lParam;
3518 ME_ScrollAbs(editor, point->x, point->y);
3519 return 0;
3520 }
3521 case EM_AUTOURLDETECT:
3522 {
3523 if (wParam==1 || wParam ==0)
3524 {
3525 editor->AutoURLDetect_bEnable = (BOOL)wParam;
3526 return 0;
3527 }
3528 return E_INVALIDARG;
3529 }
3530 case EM_GETAUTOURLDETECT:
3531 {
3532 return editor->AutoURLDetect_bEnable;
3533 }
3534 case EM_EXSETSEL:
3535 {
3536 CHARRANGE range = *(CHARRANGE *)lParam;
3537
3538 return handle_EM_EXSETSEL( editor, range.cpMin, range.cpMax );
3539 }
3540 case EM_SHOWSCROLLBAR:
3541 {
3542 DWORD flags;
3543
3544 switch (wParam)
3545 {
3546 case SB_HORZ:
3547 flags = WS_HSCROLL;
3548 break;
3549 case SB_VERT:
3550 flags = WS_VSCROLL;
3551 break;
3552 case SB_BOTH:
3553 flags = WS_HSCROLL|WS_VSCROLL;
3554 break;
3555 default:
3556 return 0;
3557 }
3558
3559 if (lParam) {
3560 editor->styleFlags |= flags;
3561 if (flags & WS_HSCROLL)
3562 ITextHost_TxShowScrollBar(editor->texthost, SB_HORZ,
3563 editor->nTotalWidth > editor->sizeWindow.cx);
3564 if (flags & WS_VSCROLL)
3565 ITextHost_TxShowScrollBar(editor->texthost, SB_VERT,
3566 editor->nTotalLength > editor->sizeWindow.cy);
3567 } else {
3568 editor->styleFlags &= ~flags;
3569 ITextHost_TxShowScrollBar(editor->texthost, wParam, FALSE);
3570 }
3571 return 0;
3572 }
3573 case EM_SETTEXTEX:
3574 {
3575 LPWSTR wszText;
3576 SETTEXTEX *pStruct = (SETTEXTEX *)wParam;
3577 int from, to, len;
3578 ME_Style *style;
3579 BOOL bRtf, bUnicode, bSelection, bUTF8;
3580 int oldModify = editor->nModifyStep;
3581 static const char utf8_bom[] = {0xef, 0xbb, 0xbf};
3582
3583 if (!pStruct) return 0;
3584
3585 /* If we detect ascii rtf at the start of the string,
3586 * we know it isn't unicode. */
3587 bRtf = (lParam && (!strncmp((char *)lParam, "{\\rtf", 5) ||
3588 !strncmp((char *)lParam, "{\\urtf", 6)));
3589 bUnicode = !bRtf && pStruct->codepage == CP_UNICODE;
3590 bUTF8 = (lParam && (!strncmp((char *)lParam, utf8_bom, 3)));
3591
3592 TRACE("EM_SETTEXTEX - %s, flags %d, cp %d\n",
3593 bUnicode ? debugstr_w((LPCWSTR)lParam) : debugstr_a((LPCSTR)lParam),
3594 pStruct->flags, pStruct->codepage);
3595
3596 bSelection = (pStruct->flags & ST_SELECTION) != 0;
3597 if (bSelection) {
3598 int nStartCursor = ME_GetSelectionOfs(editor, &from, &to);
3599 style = ME_GetSelectionInsertStyle(editor);
3600 ME_InternalDeleteText(editor, &editor->pCursors[nStartCursor], to - from, FALSE);
3601 } else {
3602 ME_Cursor start;
3603 ME_SetCursorToStart(editor, &start);
3604 ME_InternalDeleteText(editor, &start, ME_GetTextLength(editor), FALSE);
3605 style = editor->pBuffer->pDefaultStyle;
3606 }
3607
3608 if (bRtf) {
3609 ME_StreamInRTFString(editor, bSelection, (char *)lParam);
3610 if (bSelection) {
3611 /* FIXME: The length returned doesn't include the rtf control
3612 * characters, only the actual text. */
3613 len = lParam ? strlen((char *)lParam) : 0;
3614 }
3615 } else {
3616 if (bUTF8 && !bUnicode) {
3617 wszText = ME_ToUnicode(CP_UTF8, (void *)(lParam+3), &len);
3618 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
3619 ME_EndToUnicode(CP_UTF8, wszText);
3620 } else {
3621 wszText = ME_ToUnicode(pStruct->codepage, (void *)lParam, &len);
3622 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
3623 ME_EndToUnicode(pStruct->codepage, wszText);
3624 }
3625 }
3626
3627 if (bSelection) {
3628 ME_ReleaseStyle(style);
3629 ME_UpdateSelectionLinkAttribute(editor);
3630 } else {
3631 ME_Cursor cursor;
3632 len = 1;
3633 ME_SetCursorToStart(editor, &cursor);
3634 ME_UpdateLinkAttribute(editor, &cursor, INT_MAX);
3635 }
3636 ME_CommitUndo(editor);
3637 if (!(pStruct->flags & ST_KEEPUNDO))
3638 {
3639 editor->nModifyStep = oldModify;
3640 ME_EmptyUndoStack(editor);
3641 }
3642 ME_UpdateRepaint(editor, FALSE);
3643 return len;
3644 }
3645 case EM_SETBKGNDCOLOR:
3646 {
3647 LRESULT lColor;
3648 if (editor->rgbBackColor != -1) {
3649 DeleteObject(editor->hbrBackground);
3650 lColor = editor->rgbBackColor;
3651 }
3652 else lColor = ITextHost_TxGetSysColor(editor->texthost, COLOR_WINDOW);
3653
3654 if (wParam)
3655 {
3656 editor->rgbBackColor = -1;
3657 editor->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
3658 }
3659 else
3660 {
3661 editor->rgbBackColor = lParam;
3662 editor->hbrBackground = CreateSolidBrush(editor->rgbBackColor);
3663 }
3664 ITextHost_TxInvalidateRect(editor->texthost, NULL, TRUE);
3665 ITextHost_TxViewChange(editor->texthost, TRUE);
3666 return lColor;
3667 }
3668 case EM_GETMODIFY:
3669 return editor->nModifyStep == 0 ? 0 : -1;
3670 case EM_SETMODIFY:
3671 {
3672 if (wParam)
3673 editor->nModifyStep = 1;
3674 else
3675 editor->nModifyStep = 0;
3676
3677 return 0;
3678 }
3679 case EM_SETREADONLY:
3680 {
3681 if (wParam)
3682 editor->styleFlags |= ES_READONLY;
3683 else
3684 editor->styleFlags &= ~ES_READONLY;
3685 return 1;
3686 }
3687 case EM_SETEVENTMASK:
3688 {
3689 DWORD nOldMask = editor->nEventMask;
3690
3691 editor->nEventMask = lParam;
3692 return nOldMask;
3693 }
3694 case EM_GETEVENTMASK:
3695 return editor->nEventMask;
3696 case EM_SETCHARFORMAT:
3697 {
3698 CHARFORMAT2W buf, *p;
3699 BOOL bRepaint = TRUE;
3700 p = ME_ToCF2W(&buf, (CHARFORMAT2W *)lParam);
3701 if (p == NULL) return 0;
3702 if (wParam & SCF_ALL) {
3703 if (editor->mode & TM_PLAINTEXT) {
3704 ME_SetDefaultCharFormat(editor, p);
3705 } else {
3706 ME_Cursor start;
3707 ME_SetCursorToStart(editor, &start);
3708 ME_SetCharFormat(editor, &start, NULL, p);
3709 editor->nModifyStep = 1;
3710 }
3711 } else if (wParam & SCF_SELECTION) {
3712 if (editor->mode & TM_PLAINTEXT)
3713 return 0;
3714 if (wParam & SCF_WORD) {
3715 ME_Cursor start;
3716 ME_Cursor end = editor->pCursors[0];
3717 ME_MoveCursorWords(editor, &end, +1);
3718 start = end;
3719 ME_MoveCursorWords(editor, &start, -1);
3720 ME_SetCharFormat(editor, &start, &end, p);
3721 }
3722 bRepaint = ME_IsSelection(editor);
3723 ME_SetSelectionCharFormat(editor, p);
3724 if (bRepaint) editor->nModifyStep = 1;
3725 } else { /* SCF_DEFAULT */
3726 ME_SetDefaultCharFormat(editor, p);
3727 }
3728 ME_CommitUndo(editor);
3729 if (bRepaint)
3730 {
3731 ME_WrapMarkedParagraphs(editor);
3732 ME_UpdateScrollBar(editor);
3733 ME_Repaint(editor);
3734 }
3735 return 1;
3736 }
3737 case EM_GETCHARFORMAT:
3738 {
3739 CHARFORMAT2W tmp, *dst = (CHARFORMAT2W *)lParam;
3740 if (dst->cbSize != sizeof(CHARFORMATA) &&
3741 dst->cbSize != sizeof(CHARFORMATW) &&
3742 dst->cbSize != sizeof(CHARFORMAT2A) &&
3743 dst->cbSize != sizeof(CHARFORMAT2W))
3744 return 0;
3745 tmp.cbSize = sizeof(tmp);
3746 if (!wParam)
3747 ME_GetDefaultCharFormat(editor, &tmp);
3748 else
3749 ME_GetSelectionCharFormat(editor, &tmp);
3750 ME_CopyToCFAny(dst, &tmp);
3751 return tmp.dwMask;
3752 }
3753 case EM_SETPARAFORMAT:
3754 {
3755 BOOL result = ME_SetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
3756 ME_WrapMarkedParagraphs(editor);
3757 ME_UpdateScrollBar(editor);
3758 ME_Repaint(editor);
3759 ME_CommitUndo(editor);
3760 return result;
3761 }
3762 case EM_GETPARAFORMAT:
3763 ME_GetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
3764 return ((PARAFORMAT2 *)lParam)->dwMask;
3765 case EM_GETFIRSTVISIBLELINE:
3766 {
3767 ME_DisplayItem *p = editor->pBuffer->pFirst;
3768 int y = editor->vert_si.nPos;
3769 int ypara = 0;
3770 int count = 0;
3771 int ystart, yend;
3772 while(p) {
3773 p = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
3774 if (p->type == diTextEnd)
3775 break;
3776 if (p->type == diParagraph) {
3777 ypara = p->member.para.pt.y;
3778 continue;
3779 }
3780 ystart = ypara + p->member.row.pt.y;
3781 yend = ystart + p->member.row.nHeight;
3782 if (y < yend) {
3783 break;
3784 }
3785 count++;
3786 }
3787 return count;
3788 }
3789 case EM_HIDESELECTION:
3790 {
3791 editor->bHideSelection = (wParam != 0);
3792 ME_InvalidateSelection(editor);
3793 return 0;
3794 }
3795 case EM_LINESCROLL:
3796 {
3797 if (!(editor->styleFlags & ES_MULTILINE))
3798 return FALSE;
3799 ME_ScrollDown( editor, lParam * get_default_line_height( editor ) );
3800 return TRUE;
3801 }
3802 case WM_CLEAR:
3803 {
3804 int from, to;
3805 int nStartCursor = ME_GetSelectionOfs(editor, &from, &to);
3806 ME_InternalDeleteText(editor, &editor->pCursors[nStartCursor], to-from, FALSE);
3807 ME_CommitUndo(editor);
3808 ME_UpdateRepaint(editor, TRUE);
3809 return 0;
3810 }
3811 case EM_REPLACESEL:
3812 {
3813 int len = 0;
3814 LONG codepage = unicode ? CP_UNICODE : CP_ACP;
3815 LPWSTR wszText = ME_ToUnicode(codepage, (void *)lParam, &len);
3816
3817 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText));
3818
3819 ME_ReplaceSel(editor, !!wParam, wszText, len);
3820 ME_EndToUnicode(codepage, wszText);
3821 return len;
3822 }
3823 case EM_SCROLLCARET:
3824 ME_EnsureVisible(editor, &editor->pCursors[0]);
3825 return 0;
3826 case WM_SETFONT:
3827 {
3828 LOGFONTW lf;
3829 CHARFORMAT2W fmt;
3830 HDC hDC;
3831 BOOL bRepaint = LOWORD(lParam);
3832
3833 if (!wParam)
3834 wParam = (WPARAM)GetStockObject(SYSTEM_FONT);
3835
3836 if (!GetObjectW((HGDIOBJ)wParam, sizeof(LOGFONTW), &lf))
3837 return 0;
3838
3839 hDC = ITextHost_TxGetDC(editor->texthost);
3840 ME_CharFormatFromLogFont(hDC, &lf, &fmt);
3841 ITextHost_TxReleaseDC(editor->texthost, hDC);
3842 if (editor->mode & TM_RICHTEXT) {
3843 ME_Cursor start;
3844 ME_SetCursorToStart(editor, &start);
3845 ME_SetCharFormat(editor, &start, NULL, &fmt);
3846 }
3847 ME_SetDefaultCharFormat(editor, &fmt);
3848
3849 ME_CommitUndo(editor);
3850 ME_MarkAllForWrapping(editor);
3851 ME_WrapMarkedParagraphs(editor);
3852 ME_UpdateScrollBar(editor);
3853 if (bRepaint)
3854 ME_Repaint(editor);
3855 return 0;
3856 }
3857 case WM_SETTEXT:
3858 {
3859 ME_Cursor cursor;
3860 ME_SetCursorToStart(editor, &cursor);
3861 ME_InternalDeleteText(editor, &cursor, ME_GetTextLength(editor), FALSE);
3862 if (lParam)
3863 {
3864 TRACE("WM_SETTEXT lParam==%lx\n",lParam);
3865 if (!strncmp((char *)lParam, "{\\rtf", 5) ||
3866 !strncmp((char *)lParam, "{\\urtf", 6))
3867 {
3868 /* Undocumented: WM_SETTEXT supports RTF text */
3869 ME_StreamInRTFString(editor, 0, (char *)lParam);
3870 }
3871 else
3872 ME_SetText(editor, (void*)lParam, unicode);
3873 }
3874 else
3875 TRACE("WM_SETTEXT - NULL\n");
3876 ME_SetCursorToStart(editor, &cursor);
3877 ME_UpdateLinkAttribute(editor, &cursor, INT_MAX);
3878 ME_SetSelection(editor, 0, 0);
3879 editor->nModifyStep = 0;
3880 ME_CommitUndo(editor);
3881 ME_EmptyUndoStack(editor);
3882 ME_UpdateRepaint(editor, FALSE);
3883 return 1;
3884 }
3885 case EM_CANPASTE:
3886 {
3887 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
3888 if (IsClipboardFormatAvailable(nRTFFormat))
3889 return TRUE;
3890 if (IsClipboardFormatAvailable(CF_UNICODETEXT))
3891 return TRUE;
3892 return FALSE;
3893 }
3894 case WM_PASTE:
3895 case WM_MBUTTONDOWN:
3896 ME_Paste(editor);
3897 return 0;
3898 case WM_CUT:
3899 case WM_COPY:
3900 {
3901 int nFrom, nTo, nStartCur = ME_GetSelectionOfs(editor, &nFrom, &nTo);
3902 int nChars = nTo - nFrom;
3903 ME_Cursor *selStart = &editor->pCursors[nStartCur];
3904
3905 if (ME_Copy(editor, selStart, nChars) && msg == WM_CUT)
3906 {
3907 ME_InternalDeleteText(editor, selStart, nChars, FALSE);
3908 ME_CommitUndo(editor);
3909 ME_UpdateRepaint(editor, TRUE);
3910 }
3911 return 0;
3912 }
3913 case WM_GETTEXTLENGTH:
3914 {
3915 GETTEXTLENGTHEX how;
3916
3917 /* CR/LF conversion required in 2.0 mode, verbatim in 1.0 mode */
3918 how.flags = GTL_CLOSE | (editor->bEmulateVersion10 ? 0 : GTL_USECRLF) | GTL_NUMCHARS;
3919 how.codepage = unicode ? CP_UNICODE : CP_ACP;
3920 return ME_GetTextLengthEx(editor, &how);
3921 }
3922 case EM_GETTEXTLENGTHEX:
3923 return ME_GetTextLengthEx(editor, (GETTEXTLENGTHEX *)wParam);
3924 case WM_GETTEXT:
3925 {
3926 GETTEXTEX ex;
3927 ex.cb = wParam * (unicode ? sizeof(WCHAR) : sizeof(CHAR));
3928 ex.flags = GT_USECRLF;
3929 ex.codepage = unicode ? CP_UNICODE : CP_ACP;
3930 ex.lpDefaultChar = NULL;
3931 ex.lpUsedDefChar = NULL;
3932 return ME_GetTextEx(editor, &ex, lParam);
3933 }
3934 case EM_GETTEXTEX:
3935 return ME_GetTextEx(editor, (GETTEXTEX*)wParam, lParam);
3936 case EM_GETSELTEXT:
3937 {
3938 int nFrom, nTo, nStartCur = ME_GetSelectionOfs(editor, &nFrom, &nTo);
3939 ME_Cursor *from = &editor->pCursors[nStartCur];
3940 return ME_GetTextRange(editor, (WCHAR *)lParam, from,
3941 nTo - nFrom, unicode);
3942 }
3943 case EM_GETSCROLLPOS:
3944 {
3945 POINT *point = (POINT *)lParam;
3946 point->x = editor->horz_si.nPos;
3947 point->y = editor->vert_si.nPos;
3948 /* 16-bit scaled value is returned as stored in scrollinfo */
3949 if (editor->horz_si.nMax > 0xffff)
3950 point->x = MulDiv(point->x, 0xffff, editor->horz_si.nMax);
3951 if (editor->vert_si.nMax > 0xffff)
3952 point->y = MulDiv(point->y, 0xffff, editor->vert_si.nMax);
3953 return 1;
3954 }
3955 case EM_GETTEXTRANGE:
3956 {
3957 TEXTRANGEW *rng = (TEXTRANGEW *)lParam;
3958 ME_Cursor start;
3959 int nStart = rng->chrg.cpMin;
3960 int nEnd = rng->chrg.cpMax;
3961 int textlength = ME_GetTextLength(editor);
3962
3963 TRACE("EM_GETTEXTRANGE min=%d max=%d unicode=%d textlength=%d\n",
3964 rng->chrg.cpMin, rng->chrg.cpMax, unicode, textlength);
3965 if (nStart < 0) return 0;
3966 if ((nStart == 0 && nEnd == -1) || nEnd > textlength)
3967 nEnd = textlength;
3968 if (nStart >= nEnd) return 0;
3969
3970 ME_CursorFromCharOfs(editor, nStart, &start);
3971 return ME_GetTextRange(editor, rng->lpstrText, &start, nEnd - nStart, unicode);
3972 }
3973 case EM_GETLINE:
3974 {
3975 ME_DisplayItem *run;
3976 const unsigned int nMaxChars = *(WORD *) lParam;
3977 unsigned int nCharsLeft = nMaxChars;
3978 char *dest = (char *) lParam;
3979 BOOL wroteNull = FALSE;
3980
3981 TRACE("EM_GETLINE: row=%d, nMaxChars=%d (%s)\n", (int) wParam, nMaxChars,
3982 unicode ? "Unicode" : "Ansi");
3983
3984 run = ME_FindRowWithNumber(editor, wParam);
3985 if (run == NULL)
3986 return 0;
3987
3988 while (nCharsLeft && (run = ME_FindItemFwd(run, diRunOrStartRow))
3989 && run->type == diRun)
3990 {
3991 WCHAR *str = get_text( &run->member.run, 0 );
3992 unsigned int nCopy;
3993
3994 nCopy = min(nCharsLeft, run->member.run.len);
3995
3996 if (unicode)
3997 memcpy(dest, str, nCopy * sizeof(WCHAR));
3998 else
3999 nCopy = WideCharToMultiByte(CP_ACP, 0, str, nCopy, dest,
4000 nCharsLeft, NULL, NULL);
4001 dest += nCopy * (unicode ? sizeof(WCHAR) : 1);
4002 nCharsLeft -= nCopy;
4003 }
4004
4005 /* append line termination, space allowing */
4006 if (nCharsLeft > 0)
4007 {
4008 if (unicode)
4009 *((WCHAR *)dest) = '\0';
4010 else
4011 *dest = '\0';
4012 nCharsLeft--;
4013 wroteNull = TRUE;
4014 }
4015
4016 TRACE("EM_GETLINE: got %u characters\n", nMaxChars - nCharsLeft);
4017 return nMaxChars - nCharsLeft - (wroteNull ? 1 : 0);
4018 }
4019 case EM_GETLINECOUNT:
4020 {
4021 ME_DisplayItem *item = editor->pBuffer->pFirst->next;
4022 int nRows = 0;
4023
4024 ME_DisplayItem *prev_para = NULL, *last_para = NULL;
4025
4026 while (item != editor->pBuffer->pLast)
4027 {
4028 assert(item->type == diParagraph);
4029 prev_para = ME_FindItemBack(item, diRun);
4030 if (prev_para) {
4031 assert(prev_para->member.run.nFlags & MERF_ENDPARA);
4032 }
4033 nRows += item->member.para.nRows;
4034 item = item->member.para.next_para;
4035 }
4036 last_para = ME_FindItemBack(item, diRun);
4037 assert(last_para);
4038 assert(last_para->member.run.nFlags & MERF_ENDPARA);
4039 if (editor->bEmulateVersion10 && prev_para &&
4040 last_para->member.run.nCharOfs == 0 &&
4041 prev_para->member.run.len == 1 &&
4042 *get_text( &prev_para->member.run, 0 ) == '\r')
4043 {
4044 /* In 1.0 emulation, the last solitary \r at the very end of the text
4045 (if one exists) is NOT a line break.
4046 FIXME: this is an ugly hack. This should have a more regular model. */
4047 nRows--;
4048 }
4049
4050 TRACE("EM_GETLINECOUNT: nRows==%d\n", nRows);
4051 return max(1, nRows);
4052 }
4053 case EM_LINEFROMCHAR:
4054 {
4055 if (wParam == -1)
4056 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(&editor->pCursors[1]));
4057 else
4058 return ME_RowNumberFromCharOfs(editor, wParam);
4059 }
4060 case EM_EXLINEFROMCHAR:
4061 {
4062 if (lParam == -1)
4063 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(&editor->pCursors[1]));
4064 else
4065 return ME_RowNumberFromCharOfs(editor, lParam);
4066 }
4067 case EM_LINEINDEX:
4068 {
4069 ME_DisplayItem *item, *para;
4070 int nCharOfs;
4071
4072 if (wParam == -1)
4073 item = ME_FindItemBack(editor->pCursors[0].pRun, diStartRow);
4074 else
4075 item = ME_FindRowWithNumber(editor, wParam);
4076 if (!item)
4077 return -1;
4078 para = ME_GetParagraph(item);
4079 item = ME_FindItemFwd(item, diRun);
4080 nCharOfs = para->member.para.nCharOfs + item->member.run.nCharOfs;
4081 TRACE("EM_LINEINDEX: nCharOfs==%d\n", nCharOfs);
4082 return nCharOfs;
4083 }
4084 case EM_LINELENGTH:
4085 {
4086 ME_DisplayItem *item, *item_end;
4087 int nChars = 0, nThisLineOfs = 0, nNextLineOfs = 0;
4088 ME_DisplayItem *para, *run;
4089
4090 if (wParam > ME_GetTextLength(editor))
4091 return 0;
4092 if (wParam == -1)
4093 {
4094 FIXME("EM_LINELENGTH: returning number of unselected characters on lines with selection unsupported.\n");
4095 return 0;
4096 }
4097 ME_RunOfsFromCharOfs(editor, wParam, &para, &run, NULL);
4098 item = ME_RowStart(run);
4099 nThisLineOfs = ME_CharOfsFromRunOfs(editor, para, ME_FindItemFwd(item, diRun), 0);
4100 item_end = ME_FindItemFwd(item, diStartRowOrParagraphOrEnd);
4101 if (item_end->type == diStartRow) {
4102 nNextLineOfs = ME_CharOfsFromRunOfs(editor, para, ME_FindItemFwd(item_end, diRun), 0);
4103 } else {
4104 ME_DisplayItem *endRun = ME_FindItemBack(item_end, diRun);
4105 assert(endRun && endRun->member.run.nFlags & MERF_ENDPARA);
4106 nNextLineOfs = item_end->member.para.nCharOfs - endRun->member.run.len;
4107 }
4108 nChars = nNextLineOfs - nThisLineOfs;
4109 TRACE("EM_LINELENGTH(%ld)==%d\n",wParam, nChars);
4110 return nChars;
4111 }
4112 case EM_EXLIMITTEXT:
4113 {
4114 if ((int)lParam < 0)
4115 return 0;
4116 if (lParam == 0)
4117 editor->nTextLimit = 65536;
4118 else
4119 editor->nTextLimit = (int) lParam;
4120 return 0;
4121 }
4122 case EM_LIMITTEXT:
4123 {
4124 if (wParam == 0)
4125 editor->nTextLimit = 65536;
4126 else
4127 editor->nTextLimit = (int) wParam;
4128 return 0;
4129 }
4130 case EM_GETLIMITTEXT:
4131 {
4132 return editor->nTextLimit;
4133 }
4134 case EM_FINDTEXT:
4135 {
4136 LRESULT r;
4137 if(!unicode){
4138 FINDTEXTA *ft = (FINDTEXTA *)lParam;
4139 int nChars = MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, NULL, 0);
4140 WCHAR *tmp;
4141
4142 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
4143 MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, tmp, nChars);
4144 r = ME_FindText(editor, wParam, &ft->chrg, tmp, NULL);
4145 FREE_OBJ( tmp );
4146 }else{
4147 FINDTEXTW *ft = (FINDTEXTW *)lParam;
4148 r = ME_FindText(editor, wParam, &ft->chrg, ft->lpstrText, NULL);
4149 }
4150 return r;
4151 }
4152 case EM_FINDTEXTEX:
4153 {
4154 LRESULT r;
4155 if(!unicode){
4156 FINDTEXTEXA *ex = (FINDTEXTEXA *)lParam;
4157 int nChars = MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, NULL, 0);
4158 WCHAR *tmp;
4159
4160 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
4161 MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, tmp, nChars);
4162 r = ME_FindText(editor, wParam, &ex->chrg, tmp, &ex->chrgText);
4163 FREE_OBJ( tmp );
4164 }else{
4165 FINDTEXTEXW *ex = (FINDTEXTEXW *)lParam;
4166 r = ME_FindText(editor, wParam, &ex->chrg, ex->lpstrText, &ex->chrgText);
4167 }
4168 return r;
4169 }
4170 case EM_FINDTEXTW:
4171 {
4172 FINDTEXTW *ft = (FINDTEXTW *)lParam;
4173 return ME_FindText(editor, wParam, &ft->chrg, ft->lpstrText, NULL);
4174 }
4175 case EM_FINDTEXTEXW:
4176 {
4177 FINDTEXTEXW *ex = (FINDTEXTEXW *)lParam;
4178 return ME_FindText(editor, wParam, &ex->chrg, ex->lpstrText, &ex->chrgText);
4179 }
4180 case EM_GETZOOM:
4181 if (!wParam || !lParam)
4182 return FALSE;
4183 *(int *)wParam = editor->nZoomNumerator;
4184 *(int *)lParam = editor->nZoomDenominator;
4185 return TRUE;
4186 case EM_SETZOOM:
4187 return ME_SetZoom(editor, wParam, lParam);
4188 case EM_CHARFROMPOS:
4189 {
4190 ME_Cursor cursor;
4191 if (ME_CharFromPos(editor, ((POINTL *)lParam)->x, ((POINTL *)lParam)->y,
4192 &cursor, NULL))
4193 return ME_GetCursorOfs(&cursor);
4194 else
4195 return -1;
4196 }
4197 case EM_POSFROMCHAR:
4198 {
4199 ME_DisplayItem *pPara, *pRun;
4200 int nCharOfs, nOffset, nLength;
4201 POINTL pt = {0,0};
4202
4203 nCharOfs = wParam;
4204 /* detect which API version we're dealing with */
4205 if (wParam >= 0x40000)
4206 nCharOfs = lParam;
4207 nLength = ME_GetTextLength(editor);
4208 nCharOfs = min(nCharOfs, nLength);
4209 nCharOfs = max(nCharOfs, 0);
4210
4211 ME_RunOfsFromCharOfs(editor, nCharOfs, &pPara, &pRun, &nOffset);
4212 assert(pRun->type == diRun);
4213 pt.y = pRun->member.run.pt.y;
4214 pt.x = pRun->member.run.pt.x + ME_PointFromChar(editor, &pRun->member.run, nOffset, TRUE);
4215 pt.y += pPara->member.para.pt.y + editor->rcFormat.top;
4216 pt.x += editor->rcFormat.left;
4217
4218 pt.x -= editor->horz_si.nPos;
4219 pt.y -= editor->vert_si.nPos;
4220
4221 if (wParam >= 0x40000) {
4222 *(POINTL *)wParam = pt;
4223 }
4224 return (wParam >= 0x40000) ? 0 : MAKELONG( pt.x, pt.y );
4225 }
4226 case WM_CREATE:
4227 return ME_WmCreate(editor, lParam, unicode);
4228 case WM_DESTROY:
4229 ME_DestroyEditor(editor);
4230 return 0;
4231 case WM_SETCURSOR:
4232 {
4233 POINT cursor_pos;
4234 if (wParam == (WPARAM)editor->hWnd && GetCursorPos(&cursor_pos) &&
4235 ScreenToClient(editor->hWnd, &cursor_pos))
4236 ME_LinkNotify(editor, msg, 0, MAKELPARAM(cursor_pos.x, cursor_pos.y));
4237 return ME_SetCursor(editor);
4238 }
4239 case WM_LBUTTONDBLCLK:
4240 case WM_LBUTTONDOWN:
4241 {
4242 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
4243 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
4244 !ME_FilterEvent(editor, msg, &wParam, &lParam))
4245 return 0;
4246 ITextHost_TxSetFocus(editor->texthost);
4247 ME_LButtonDown(editor, (short)LOWORD(lParam), (short)HIWORD(lParam),
4248 ME_CalculateClickCount(editor, msg, wParam, lParam));
4249 ITextHost_TxSetCapture(editor->texthost, TRUE);
4250 editor->bMouseCaptured = TRUE;
4251 ME_LinkNotify(editor, msg, wParam, lParam);
4252 if (!ME_SetCursor(editor)) goto do_default;
4253 break;
4254 }
4255 case WM_MOUSEMOVE:
4256 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
4257 !ME_FilterEvent(editor, msg, &wParam, &lParam))
4258 return 0;
4259 if (editor->bMouseCaptured)
4260 ME_MouseMove(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
4261 else
4262 ME_LinkNotify(editor, msg, wParam, lParam);
4263 /* Set cursor if mouse is captured, since WM_SETCURSOR won't be received. */
4264 if (editor->bMouseCaptured)
4265 ME_SetCursor(editor);
4266 break;
4267 case WM_LBUTTONUP:
4268 if (editor->bMouseCaptured) {
4269 ITextHost_TxSetCapture(editor->texthost, FALSE);
4270 editor->bMouseCaptured = FALSE;
4271 }
4272 if (editor->nSelectionType == stDocument)
4273 editor->nSelectionType = stPosition;
4274 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
4275 !ME_FilterEvent(editor, msg, &wParam, &lParam))
4276 return 0;
4277 else
4278 {
4279 ME_SetCursor(editor);
4280 ME_LinkNotify(editor, msg, wParam, lParam);
4281 }
4282 break;
4283 case WM_RBUTTONUP:
4284 case WM_RBUTTONDOWN:
4285 case WM_RBUTTONDBLCLK:
4286 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
4287 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
4288 !ME_FilterEvent(editor, msg, &wParam, &lParam))
4289 return 0;
4290 ME_LinkNotify(editor, msg, wParam, lParam);
4291 goto do_default;
4292 case WM_CONTEXTMENU:
4293 if (!ME_ShowContextMenu(editor, (short)LOWORD(lParam), (short)HIWORD(lParam)))
4294 goto do_default;
4295 break;
4296 case WM_SETFOCUS:
4297 editor->bHaveFocus = TRUE;
4298 ME_ShowCaret(editor);
4299 ME_SendOldNotify(editor, EN_SETFOCUS);
4300 return 0;
4301 case WM_KILLFOCUS:
4302 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
4303 editor->bHaveFocus = FALSE;
4304 editor->wheel_remain = 0;
4305 ME_HideCaret(editor);
4306 ME_SendOldNotify(editor, EN_KILLFOCUS);
4307 return 0;
4308 case WM_COMMAND:
4309 TRACE("editor wnd command = %d\n", LOWORD(wParam));
4310 return 0;
4311 case WM_KEYUP:
4312 if ((editor->nEventMask & ENM_KEYEVENTS) &&
4313 !ME_FilterEvent(editor, msg, &wParam, &lParam))
4314 return 0;
4315 goto do_default;
4316 case WM_KEYDOWN:
4317 if ((editor->nEventMask & ENM_KEYEVENTS) &&
4318 !ME_FilterEvent(editor, msg, &wParam, &lParam))
4319 return 0;
4320 if (ME_KeyDown(editor, LOWORD(wParam)))
4321 return 0;
4322 goto do_default;
4323 case WM_CHAR:
4324 if ((editor->nEventMask & ENM_KEYEVENTS) &&
4325 !ME_FilterEvent(editor, msg, &wParam, &lParam))
4326 return 0;
4327 return ME_Char(editor, wParam, lParam, unicode);
4328 case WM_UNICHAR:
4329 if (unicode)
4330 {
4331 if(wParam == UNICODE_NOCHAR) return TRUE;
4332 if(wParam <= 0x000fffff)
4333 {
4334 if(wParam > 0xffff) /* convert to surrogates */
4335 {
4336 wParam -= 0x10000;
4337 ME_Char(editor, (wParam >> 10) + 0xd800, 0, TRUE);
4338 ME_Char(editor, (wParam & 0x03ff) + 0xdc00, 0, TRUE);
4339 } else {
4340 ME_Char(editor, wParam, 0, TRUE);
4341 }
4342 }
4343 return 0;
4344 }
4345 break;
4346 case EM_STOPGROUPTYPING:
4347 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
4348 return 0;
4349 case WM_HSCROLL:
4350 {
4351 const int scrollUnit = 7;
4352
4353 switch(LOWORD(wParam))
4354 {
4355 case SB_LEFT:
4356 ME_ScrollAbs(editor, 0, 0);
4357 break;
4358 case SB_RIGHT:
4359 ME_ScrollAbs(editor,
4360 editor->horz_si.nMax - (int)editor->horz_si.nPage,
4361 editor->vert_si.nMax - (int)editor->vert_si.nPage);
4362 break;
4363 case SB_LINELEFT:
4364 ME_ScrollLeft(editor, scrollUnit);
4365 break;
4366 case SB_LINERIGHT:
4367 ME_ScrollRight(editor, scrollUnit);
4368 break;
4369 case SB_PAGELEFT:
4370 ME_ScrollLeft(editor, editor->sizeWindow.cx);
4371 break;
4372 case SB_PAGERIGHT:
4373 ME_ScrollRight(editor, editor->sizeWindow.cx);
4374 break;
4375 case SB_THUMBTRACK:
4376 case SB_THUMBPOSITION:
4377 {
4378 int pos = HIWORD(wParam);
4379 if (editor->horz_si.nMax > 0xffff)
4380 pos = MulDiv(pos, editor->horz_si.nMax, 0xffff);
4381 ME_HScrollAbs(editor, pos);
4382 break;
4383 }
4384 }
4385 break;
4386 }
4387 case EM_SCROLL: /* fall through */
4388 case WM_VSCROLL:
4389 {
4390 int origNPos;
4391 int lineHeight = get_default_line_height( editor );
4392
4393 origNPos = editor->vert_si.nPos;
4394
4395 switch(LOWORD(wParam))
4396 {
4397 case SB_TOP:
4398 ME_ScrollAbs(editor, 0, 0);
4399 break;
4400 case SB_BOTTOM:
4401 ME_ScrollAbs(editor,
4402 editor->horz_si.nMax - (int)editor->horz_si.nPage,
4403 editor->vert_si.nMax - (int)editor->vert_si.nPage);
4404 break;
4405 case SB_LINEUP:
4406 ME_ScrollUp(editor,lineHeight);
4407 break;
4408 case SB_LINEDOWN:
4409 ME_ScrollDown(editor,lineHeight);
4410 break;
4411 case SB_PAGEUP:
4412 ME_ScrollUp(editor,editor->sizeWindow.cy);
4413 break;
4414 case SB_PAGEDOWN:
4415 ME_ScrollDown(editor,editor->sizeWindow.cy);
4416 break;
4417 case SB_THUMBTRACK:
4418 case SB_THUMBPOSITION:
4419 {
4420 int pos = HIWORD(wParam);
4421 if (editor->vert_si.nMax > 0xffff)
4422 pos = MulDiv(pos, editor->vert_si.nMax, 0xffff);
4423 ME_VScrollAbs(editor, pos);
4424 break;
4425 }
4426 }
4427 if (msg == EM_SCROLL)
4428 return 0x00010000 | (((editor->vert_si.nPos - origNPos)/lineHeight) & 0xffff);
4429 break;
4430 }
4431 case WM_MOUSEWHEEL:
4432 {
4433 int delta;
4434 BOOL ctrl_is_down;
4435
4436 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
4437 !ME_FilterEvent(editor, msg, &wParam, &lParam))
4438 return 0;
4439
4440 ctrl_is_down = GetKeyState(VK_CONTROL) & 0x8000;
4441
4442 delta = GET_WHEEL_DELTA_WPARAM(wParam);
4443
4444 /* if scrolling changes direction, ignore left overs */
4445 if ((delta < 0 && editor->wheel_remain < 0) ||
4446 (delta > 0 && editor->wheel_remain > 0))
4447 editor->wheel_remain += delta;
4448 else
4449 editor->wheel_remain = delta;
4450
4451 if (editor->wheel_remain)
4452 {
4453 if (ctrl_is_down) {
4454 int numerator;
4455 if (!editor->nZoomNumerator || !editor->nZoomDenominator)
4456 {
4457 numerator = 100;
4458 } else {
4459 numerator = editor->nZoomNumerator * 100 / editor->nZoomDenominator;
4460 }
4461 numerator += calc_wheel_change( &editor->wheel_remain, 10 );
4462 if (numerator >= 10 && numerator <= 500)
4463 ME_SetZoom(editor, numerator, 100);
4464 } else {
4465 UINT max_lines = 3;
4466 int lines = 0;
4467
4468 SystemParametersInfoW( SPI_GETWHEELSCROLLLINES, 0, &max_lines, 0 );
4469 if (max_lines)
4470 lines = calc_wheel_change( &editor->wheel_remain, (int)max_lines );
4471 if (lines)
4472 ME_ScrollDown( editor, -lines * get_default_line_height( editor ) );
4473 }
4474 }
4475 break;
4476 }
4477 case EM_GETRECT:
4478 {
4479 *((RECT *)lParam) = editor->rcFormat;
4480 if (editor->bDefaultFormatRect)
4481 ((RECT *)lParam)->left -= editor->selofs;
4482 return 0;
4483 }
4484 case EM_SETRECT:
4485 case EM_SETRECTNP:
4486 {
4487 if (lParam)
4488 {
4489 int border = 0;
4490 RECT clientRect;
4491 RECT *rc = (RECT *)lParam;
4492
4493 border = editor->exStyleFlags & WS_EX_CLIENTEDGE ? 1 : 0;
4494 ITextHost_TxGetClientRect(editor->texthost, &clientRect);
4495 if (wParam == 0)
4496 {
4497 editor->rcFormat.top = max(0, rc->top - border);
4498 editor->rcFormat.left = max(0, rc->left - border);
4499 editor->rcFormat.bottom = min(clientRect.bottom, rc->bottom);
4500 editor->rcFormat.right = min(clientRect.right, rc->right + border);
4501 } else if (wParam == 1) {
4502 /* MSDN incorrectly says a wParam value of 1 causes the
4503 * lParam rect to be used as a relative offset,
4504 * however, the tests show it just prevents min/max bound
4505 * checking. */
4506 editor->rcFormat.top = rc->top - border;
4507 editor->rcFormat.left = rc->left - border;
4508 editor->rcFormat.bottom = rc->bottom;
4509 editor->rcFormat.right = rc->right + border;
4510 } else {
4511 return 0;
4512 }
4513 editor->bDefaultFormatRect = FALSE;
4514 }
4515 else
4516 {
4517 ME_SetDefaultFormatRect(editor);
4518 editor->bDefaultFormatRect = TRUE;
4519 }
4520 ME_MarkAllForWrapping(editor);
4521 ME_WrapMarkedParagraphs(editor);
4522 ME_UpdateScrollBar(editor);
4523 if (msg != EM_SETRECTNP)
4524 ME_Repaint(editor);
4525 return 0;
4526 }
4527 case EM_REQUESTRESIZE:
4528 ME_SendRequestResize(editor, TRUE);
4529 return 0;
4530 case WM_SETREDRAW:
4531 goto do_default;
4532 case WM_WINDOWPOSCHANGED:
4533 {
4534 RECT clientRect;
4535 WINDOWPOS *winpos = (WINDOWPOS *)lParam;
4536
4537 if (winpos->flags & SWP_NOCLIENTSIZE) goto do_default;
4538 ITextHost_TxGetClientRect(editor->texthost, &clientRect);
4539 if (editor->bDefaultFormatRect) {
4540 ME_SetDefaultFormatRect(editor);
4541 } else {
4542 editor->rcFormat.right += clientRect.right - editor->prevClientRect.right;
4543 editor->rcFormat.bottom += clientRect.bottom - editor->prevClientRect.bottom;
4544 }
4545 editor->prevClientRect = clientRect;
4546 ME_RewrapRepaint(editor);
4547 goto do_default;
4548 }
4549 /* IME messages to make richedit controls IME aware */
4550 case WM_IME_SETCONTEXT:
4551 case WM_IME_CONTROL:
4552 case WM_IME_SELECT:
4553 case WM_IME_COMPOSITIONFULL:
4554 return 0;
4555 case WM_IME_STARTCOMPOSITION:
4556 {
4557 editor->imeStartIndex=ME_GetCursorOfs(&editor->pCursors[0]);
4558 ME_DeleteSelection(editor);
4559 ME_CommitUndo(editor);
4560 ME_UpdateRepaint(editor, FALSE);
4561 return 0;
4562 }
4563 case WM_IME_COMPOSITION:
4564 {
4565 HIMC hIMC;
4566
4567 ME_Style *style = ME_GetInsertStyle(editor, 0);
4568 hIMC = ITextHost_TxImmGetContext(editor->texthost);
4569 ME_DeleteSelection(editor);
4570 ME_SaveTempStyle(editor);
4571 if (lParam & (GCS_RESULTSTR|GCS_COMPSTR))
4572 {
4573 LPWSTR lpCompStr = NULL;
4574 DWORD dwBufLen;
4575 DWORD dwIndex = lParam & GCS_RESULTSTR;
4576 if (!dwIndex)
4577 dwIndex = GCS_COMPSTR;
4578
4579 dwBufLen = ImmGetCompositionStringW(hIMC, dwIndex, NULL, 0);
4580 lpCompStr = HeapAlloc(GetProcessHeap(),0,dwBufLen + sizeof(WCHAR));
4581 ImmGetCompositionStringW(hIMC, dwIndex, lpCompStr, dwBufLen);
4582 lpCompStr[dwBufLen/sizeof(WCHAR)] = 0;
4583 ME_InsertTextFromCursor(editor,0,lpCompStr,dwBufLen/sizeof(WCHAR),style);
4584 HeapFree(GetProcessHeap(), 0, lpCompStr);
4585
4586 if (dwIndex == GCS_COMPSTR)
4587 ME_SetSelection(editor,editor->imeStartIndex,
4588 editor->imeStartIndex + dwBufLen/sizeof(WCHAR));
4589 }
4590 ME_ReleaseStyle(style);
4591 ME_CommitUndo(editor);
4592 ME_UpdateRepaint(editor, FALSE);
4593 return 0;
4594 }
4595 case WM_IME_ENDCOMPOSITION:
4596 {
4597 ME_DeleteSelection(editor);
4598 editor->imeStartIndex=-1;
4599 return 0;
4600 }
4601 case EM_GETOLEINTERFACE:
4602 {
4603 if (!editor->reOle)
4604 if (!CreateIRichEditOle(NULL, editor, (LPVOID *)&editor->reOle))
4605 return 0;
4606 *(LPVOID *)lParam = editor->reOle;
4607 IRichEditOle_AddRef(editor->reOle);
4608 return 1;
4609 }
4610 case EM_GETPASSWORDCHAR:
4611 {
4612 return editor->cPasswordMask;
4613 }
4614 case EM_SETOLECALLBACK:
4615 if(editor->lpOleCallback)
4616 IRichEditOleCallback_Release(editor->lpOleCallback);
4617 editor->lpOleCallback = (IRichEditOleCallback*)lParam;
4618 if(editor->lpOleCallback)
4619 IRichEditOleCallback_AddRef(editor->lpOleCallback);
4620 return TRUE;
4621 case EM_GETWORDBREAKPROC:
4622 return (LRESULT)editor->pfnWordBreak;
4623 case EM_SETWORDBREAKPROC:
4624 {
4625 EDITWORDBREAKPROCW pfnOld = editor->pfnWordBreak;
4626
4627 editor->pfnWordBreak = (EDITWORDBREAKPROCW)lParam;
4628 return (LRESULT)pfnOld;
4629 }
4630 case EM_GETTEXTMODE:
4631 return editor->mode;
4632 case EM_SETTEXTMODE:
4633 {
4634 int mask = 0;
4635 int changes = 0;
4636
4637 if (ME_GetTextLength(editor) ||
4638 !list_empty( &editor->undo_stack ) || !list_empty( &editor->redo_stack ))
4639 return E_UNEXPECTED;
4640
4641 /* Check for mutually exclusive flags in adjacent bits of wParam */
4642 if ((wParam & (TM_RICHTEXT | TM_MULTILEVELUNDO | TM_MULTICODEPAGE)) &
4643 (wParam & (TM_PLAINTEXT | TM_SINGLELEVELUNDO | TM_SINGLECODEPAGE)) << 1)
4644 return E_INVALIDARG;
4645
4646 if (wParam & (TM_RICHTEXT | TM_PLAINTEXT))
4647 {
4648 mask |= TM_RICHTEXT | TM_PLAINTEXT;
4649 changes |= wParam & (TM_RICHTEXT | TM_PLAINTEXT);
4650 if (wParam & TM_PLAINTEXT) {
4651 /* Clear selection since it should be possible to select the
4652 * end of text run for rich text */
4653 ME_InvalidateSelection(editor);
4654 ME_SetCursorToStart(editor, &editor->pCursors[0]);
4655 editor->pCursors[1] = editor->pCursors[0];
4656 /* plain text can only have the default style. */
4657 ME_ClearTempStyle(editor);
4658 ME_AddRefStyle(editor->pBuffer->pDefaultStyle);
4659 ME_ReleaseStyle(editor->pCursors[0].pRun->member.run.style);
4660 editor->pCursors[0].pRun->member.run.style = editor->pBuffer->pDefaultStyle;
4661 }
4662 }
4663 /* FIXME: Currently no support for undo level and code page options */
4664 editor->mode = (editor->mode & ~mask) | changes;
4665 return 0;
4666 }
4667 case EM_SETPASSWORDCHAR:
4668 {
4669 editor->cPasswordMask = wParam;
4670 ME_RewrapRepaint(editor);
4671 return 0;
4672 }
4673 case EM_SETTARGETDEVICE:
4674 if (wParam == 0)
4675 {
4676 BOOL new = (lParam == 0 && (editor->styleFlags & ES_MULTILINE));
4677 if (editor->nAvailWidth || editor->bWordWrap != new)
4678 {
4679 editor->bWordWrap = new;
4680 editor->nAvailWidth = 0; /* wrap to client area */
4681 ME_RewrapRepaint(editor);
4682 }
4683 } else {
4684 int width = max(0, lParam);
4685 if ((editor->styleFlags & ES_MULTILINE) &&
4686 (!editor->bWordWrap || editor->nAvailWidth != width))
4687 {
4688 editor->nAvailWidth = width;
4689 editor->bWordWrap = TRUE;
4690 ME_RewrapRepaint(editor);
4691 }
4692 FIXME("EM_SETTARGETDEVICE doesn't use non-NULL target devices\n");
4693 }
4694 return TRUE;
4695 default:
4696 do_default:
4697 *phresult = S_FALSE;
4698 break;
4699 }
4700 return 0L;
4701 }
4702
4703 static LRESULT RichEditWndProc_common(HWND hWnd, UINT msg, WPARAM wParam,
4704 LPARAM lParam, BOOL unicode)
4705 {
4706 ME_TextEditor *editor;
4707 HRESULT hresult;
4708 LRESULT lresult = 0;
4709
4710 TRACE("enter hwnd %p msg %04x (%s) %lx %lx, unicode %d\n",
4711 hWnd, msg, get_msg_name(msg), wParam, lParam, unicode);
4712
4713 editor = (ME_TextEditor *)GetWindowLongPtrW(hWnd, 0);
4714 if (!editor)
4715 {
4716 if (msg == WM_NCCREATE)
4717 {
4718 CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
4719 ITextHost *texthost;
4720
4721 TRACE("WM_NCCREATE: hWnd %p style 0x%08x\n", hWnd, pcs->style);
4722 texthost = ME_CreateTextHost(hWnd, pcs, FALSE);
4723 return texthost != NULL;
4724 }
4725 else
4726 {
4727 return DefWindowProcW(hWnd, msg, wParam, lParam);
4728 }
4729 }
4730
4731 switch (msg)
4732 {
4733 case WM_PAINT:
4734 {
4735 HDC hDC;
4736 RECT rc;
4737 PAINTSTRUCT ps;
4738
4739 hDC = BeginPaint(editor->hWnd, &ps);
4740 if (!editor->bEmulateVersion10 || (editor->nEventMask & ENM_UPDATE))
4741 ME_SendOldNotify(editor, EN_UPDATE);
4742 /* Erase area outside of the formatting rectangle */
4743 if (ps.rcPaint.top < editor->rcFormat.top)
4744 {
4745 rc = ps.rcPaint;
4746 rc.bottom = editor->rcFormat.top;
4747 FillRect(hDC, &rc, editor->hbrBackground);
4748 ps.rcPaint.top = editor->rcFormat.top;
4749 }
4750 if (ps.rcPaint.bottom > editor->rcFormat.bottom) {
4751 rc = ps.rcPaint;
4752 rc.top = editor->rcFormat.bottom;
4753 FillRect(hDC, &rc, editor->hbrBackground);
4754 ps.rcPaint.bottom = editor->rcFormat.bottom;
4755 }
4756 if (ps.rcPaint.left < editor->rcFormat.left) {
4757 rc = ps.rcPaint;
4758 rc.right = editor->rcFormat.left;
4759 FillRect(hDC, &rc, editor->hbrBackground);
4760 ps.rcPaint.left = editor->rcFormat.left;
4761 }
4762 if (ps.rcPaint.right > editor->rcFormat.right) {
4763 rc = ps.rcPaint;
4764 rc.left = editor->rcFormat.right;
4765 FillRect(hDC, &rc, editor->hbrBackground);
4766 ps.rcPaint.right = editor->rcFormat.right;
4767 }
4768
4769 ME_PaintContent(editor, hDC, &ps.rcPaint);
4770 EndPaint(editor->hWnd, &ps);
4771 return 0;
4772 }
4773 case WM_ERASEBKGND:
4774 {
4775 HDC hDC = (HDC)wParam;
4776 RECT rc;
4777
4778 if (GetUpdateRect(editor->hWnd, &rc, TRUE))
4779 FillRect(hDC, &rc, editor->hbrBackground);
4780 return 1;
4781 }
4782 case EM_SETOPTIONS:
4783 {
4784 DWORD dwStyle;
4785 const DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
4786 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN |
4787 ECO_SELECTIONBAR;
4788 lresult = ME_HandleMessage(editor, msg, wParam, lParam, unicode, &hresult);
4789 dwStyle = GetWindowLongW(hWnd, GWL_STYLE);
4790 dwStyle = (dwStyle & ~mask) | (lresult & mask);
4791 SetWindowLongW(hWnd, GWL_STYLE, dwStyle);
4792 return lresult;
4793 }
4794 case EM_SETREADONLY:
4795 {
4796 DWORD dwStyle;
4797 lresult = ME_HandleMessage(editor, msg, wParam, lParam, unicode, &hresult);
4798 dwStyle = GetWindowLongW(hWnd, GWL_STYLE);
4799 dwStyle &= ~ES_READONLY;
4800 if (wParam)
4801 dwStyle |= ES_READONLY;
4802 SetWindowLongW(hWnd, GWL_STYLE, dwStyle);
4803 return lresult;
4804 }
4805 default:
4806 lresult = ME_HandleMessage(editor, msg, wParam, lParam, unicode, &hresult);
4807 }
4808
4809 if (hresult == S_FALSE)
4810 lresult = DefWindowProcW(hWnd, msg, wParam, lParam);
4811
4812 TRACE("exit hwnd %p msg %04x (%s) %lx %lx, unicode %d -> %lu\n",
4813 hWnd, msg, get_msg_name(msg), wParam, lParam, unicode, lresult);
4814
4815 return lresult;
4816 }
4817
4818 static LRESULT WINAPI RichEditWndProcW(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
4819 {
4820 BOOL unicode = TRUE;
4821
4822 /* Under Win9x RichEdit20W returns ANSI strings, see the tests. */
4823 if (msg == WM_GETTEXT && (GetVersion() & 0x80000000))
4824 unicode = FALSE;
4825
4826 return RichEditWndProc_common(hWnd, msg, wParam, lParam, unicode);
4827 }
4828
4829 static LRESULT WINAPI RichEditWndProcA(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
4830 {
4831 return RichEditWndProc_common(hWnd, msg, wParam, lParam, FALSE);
4832 }
4833
4834 /******************************************************************
4835 * RichEditANSIWndProc (RICHED20.10)
4836 */
4837 LRESULT WINAPI RichEditANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
4838 {
4839 return RichEditWndProcA(hWnd, msg, wParam, lParam);
4840 }
4841
4842 /******************************************************************
4843 * RichEdit10ANSIWndProc (RICHED20.9)
4844 */
4845 LRESULT WINAPI RichEdit10ANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
4846 {
4847 if (msg == WM_NCCREATE && !GetWindowLongPtrW(hWnd, 0))
4848 {
4849 ITextHost *texthost;
4850 CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
4851
4852 TRACE("WM_NCCREATE: hWnd %p style 0x%08x\n", hWnd, pcs->style);
4853 texthost = ME_CreateTextHost(hWnd, pcs, TRUE);
4854 return texthost != NULL;
4855 }
4856 return RichEditANSIWndProc(hWnd, msg, wParam, lParam);
4857 }
4858
4859 void ME_SendOldNotify(ME_TextEditor *editor, int nCode)
4860 {
4861 ITextHost_TxNotify(editor->texthost, nCode, NULL);
4862 }
4863
4864 /* Fill buffer with srcChars unicode characters from the start cursor.
4865 *
4866 * buffer: destination buffer
4867 * buflen: length of buffer in characters excluding the NULL terminator.
4868 * start: start of editor text to copy into buffer.
4869 * srcChars: Number of characters to use from the editor text.
4870 * bCRLF: if true, replaces all end of lines with \r\n pairs.
4871 *
4872 * returns the number of characters written excluding the NULL terminator.
4873 *
4874 * The written text is always NULL terminated.
4875 */
4876 int ME_GetTextW(ME_TextEditor *editor, WCHAR *buffer, int buflen,
4877 const ME_Cursor *start, int srcChars, BOOL bCRLF,
4878 BOOL bEOP)
4879 {
4880 ME_DisplayItem *pRun, *pNextRun;
4881 const WCHAR *pStart = buffer;
4882 const WCHAR cr_lf[] = {'\r', '\n', 0};
4883 const WCHAR *str;
4884 int nLen;
4885
4886 /* bCRLF flag is only honored in 2.0 and up. 1.0 must always return text verbatim */
4887 if (editor->bEmulateVersion10) bCRLF = FALSE;
4888
4889 pRun = start->pRun;
4890 assert(pRun);
4891 pNextRun = ME_FindItemFwd(pRun, diRun);
4892
4893 nLen = pRun->member.run.len - start->nOffset;
4894 str = get_text( &pRun->member.run, start->nOffset );
4895
4896 while (srcChars && buflen && pNextRun)
4897 {
4898 int nFlags = pRun->member.run.nFlags;
4899
4900 if (bCRLF && nFlags & MERF_ENDPARA && ~nFlags & MERF_ENDCELL)
4901 {
4902 if (buflen == 1) break;
4903 /* FIXME: native fails to reduce srcChars here for WM_GETTEXT or
4904 * EM_GETTEXTEX, however, this is done for copying text which
4905 * also uses this function. */
4906 srcChars -= min(nLen, srcChars);
4907 nLen = 2;
4908 str = cr_lf;
4909 } else {
4910 nLen = min(nLen, srcChars);
4911 srcChars -= nLen;
4912 }
4913
4914 nLen = min(nLen, buflen);
4915 buflen -= nLen;
4916
4917 CopyMemory(buffer, str, sizeof(WCHAR) * nLen);
4918
4919 buffer += nLen;
4920
4921 pRun = pNextRun;
4922 pNextRun = ME_FindItemFwd(pRun, diRun);
4923
4924 nLen = pRun->member.run.len;
4925 str = get_text( &pRun->member.run, 0 );
4926 }
4927 /* append '\r' to the last paragraph. */
4928 if (pRun->next->type == diTextEnd && bEOP)
4929 {
4930 *buffer = '\r';
4931 buffer ++;
4932 }
4933 *buffer = 0;
4934 return buffer - pStart;
4935 }
4936
4937 static BOOL ME_RegisterEditorClass(HINSTANCE hInstance)
4938 {
4939 WNDCLASSW wcW;
4940 WNDCLASSA wcA;
4941
4942 wcW.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
4943 wcW.lpfnWndProc = RichEditWndProcW;
4944 wcW.cbClsExtra = 0;
4945 wcW.cbWndExtra = sizeof(ME_TextEditor *);
4946 wcW.hInstance = NULL; /* hInstance would register DLL-local class */
4947 wcW.hIcon = NULL;
4948 wcW.hCursor = LoadCursorW(NULL, (LPWSTR)IDC_IBEAM);
4949 wcW.hbrBackground = GetStockObject(NULL_BRUSH);
4950 wcW.lpszMenuName = NULL;
4951
4952 if (is_version_nt())
4953 {
4954 wcW.lpszClassName = RICHEDIT_CLASS20W;
4955 if (!RegisterClassW(&wcW)) return FALSE;
4956 wcW.lpszClassName = MSFTEDIT_CLASS;
4957 if (!RegisterClassW(&wcW)) return FALSE;
4958 }
4959 else
4960 {
4961 /* WNDCLASSA/W have the same layout */
4962 wcW.lpszClassName = (LPCWSTR)"RichEdit20W";
4963 if (!RegisterClassA((WNDCLASSA *)&wcW)) return FALSE;
4964 wcW.lpszClassName = (LPCWSTR)"RichEdit50W";
4965 if (!RegisterClassA((WNDCLASSA *)&wcW)) return FALSE;
4966 }
4967
4968 wcA.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
4969 wcA.lpfnWndProc = RichEditWndProcA;
4970 wcA.cbClsExtra = 0;
4971 wcA.cbWndExtra = sizeof(ME_TextEditor *);
4972 wcA.hInstance = NULL; /* hInstance would register DLL-local class */
4973 wcA.hIcon = NULL;
4974 wcA.hCursor = LoadCursorW(NULL, (LPWSTR)IDC_IBEAM);
4975 wcA.hbrBackground = GetStockObject(NULL_BRUSH);
4976 wcA.lpszMenuName = NULL;
4977 wcA.lpszClassName = RICHEDIT_CLASS20A;
4978 if (!RegisterClassA(&wcA)) return FALSE;
4979 wcA.lpszClassName = "RichEdit50A";
4980 if (!RegisterClassA(&wcA)) return FALSE;
4981
4982 return TRUE;
4983 }
4984
4985 static LRESULT WINAPI REComboWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
4986 /* FIXME: Not implemented */
4987 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
4988 hWnd, msg, get_msg_name(msg), wParam, lParam);
4989 return DefWindowProcW(hWnd, msg, wParam, lParam);
4990 }
4991
4992 static LRESULT WINAPI REListWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
4993 /* FIXME: Not implemented */
4994 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
4995 hWnd, msg, get_msg_name(msg), wParam, lParam);
4996 return DefWindowProcW(hWnd, msg, wParam, lParam);
4997 }
4998
4999 /******************************************************************
5000 * REExtendedRegisterClass (RICHED20.8)
5001 *
5002 * FIXME undocumented
5003 * Need to check for errors and implement controls and callbacks
5004 */
5005 LRESULT WINAPI REExtendedRegisterClass(void)
5006 {
5007 WNDCLASSW wcW;
5008 UINT result;
5009
5010 FIXME("semi stub\n");
5011
5012 wcW.cbClsExtra = 0;
5013 wcW.cbWndExtra = 4;
5014 wcW.hInstance = NULL;
5015 wcW.hIcon = NULL;
5016 wcW.hCursor = NULL;
5017 wcW.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
5018 wcW.lpszMenuName = NULL;
5019
5020 if (!ME_ListBoxRegistered)
5021 {
5022 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS;
5023 wcW.lpfnWndProc = REListWndProc;
5024 wcW.lpszClassName = REListBox20W;
5025 if (RegisterClassW(&wcW)) ME_ListBoxRegistered = TRUE;
5026 }
5027
5028 if (!ME_ComboBoxRegistered)
5029 {
5030 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
5031 wcW.lpfnWndProc = REComboWndProc;
5032 wcW.lpszClassName = REComboBox20W;
5033 if (RegisterClassW(&wcW)) ME_ComboBoxRegistered = TRUE;
5034 }
5035
5036 result = 0;
5037 if (ME_ListBoxRegistered)
5038 result += 1;
5039 if (ME_ComboBoxRegistered)
5040 result += 2;
5041
5042 return result;
5043 }
5044
5045 static BOOL isurlspecial(WCHAR c)
5046 {
5047 static const WCHAR special_chars[] = {'.','/','%','@','*','|','\\','+','#',0};
5048 return strchrW( special_chars, c ) != NULL;
5049 }
5050
5051 /**
5052 * This proc takes a selection, and scans it forward in order to select the span
5053 * of a possible URL candidate. A possible URL candidate must start with isalnum
5054 * or one of the following special characters: *|/\+%#@ and must consist entirely
5055 * of the characters allowed to start the URL, plus : (colon) which may occur
5056 * at most once, and not at either end.
5057 */
5058 static BOOL ME_FindNextURLCandidate(ME_TextEditor *editor,
5059 const ME_Cursor *start,
5060 int nChars,
5061 ME_Cursor *candidate_min,
5062 ME_Cursor *candidate_max)
5063 {
5064 ME_Cursor cursor = *start;
5065 BOOL foundColon = FALSE;
5066 BOOL candidateStarted = FALSE;
5067 WCHAR lastAcceptedChar = '\0';
5068
5069 while (nChars > 0)
5070 {
5071 WCHAR *strStart = get_text( &cursor.pRun->member.run, 0 );
5072 WCHAR *str = strStart + cursor.nOffset;
5073 int nLen = cursor.pRun->member.run.len - cursor.nOffset;
5074 nChars -= nLen;
5075
5076 if (~cursor.pRun->member.run.nFlags & MERF_ENDPARA)
5077 {
5078 /* Find start of candidate */
5079 if (!candidateStarted)
5080 {
5081 while (nLen)
5082 {
5083 nLen--;
5084 if (isalnumW(*str) || isurlspecial(*str))
5085 {
5086 cursor.nOffset = str - strStart;
5087 *candidate_min = cursor;
5088 candidateStarted = TRUE;
5089 lastAcceptedChar = *str++;
5090 break;
5091 }
5092 str++;
5093 }
5094 }
5095
5096 /* Find end of candidate */
5097 if (candidateStarted) {
5098 while (nLen)
5099 {
5100 nLen--;
5101 if (*str == ':' && !foundColon) {
5102 foundColon = TRUE;
5103 } else if (!isalnumW(*str) && !isurlspecial(*str)) {
5104 cursor.nOffset = str - strStart;
5105 if (lastAcceptedChar == ':')
5106 ME_MoveCursorChars(editor, &cursor, -1);
5107 *candidate_max = cursor;
5108 return TRUE;
5109 }
5110 lastAcceptedChar = *str++;
5111 }
5112 }
5113 } else {
5114 /* End of paragraph: skip it if before candidate span, or terminates
5115 current active span */
5116 if (candidateStarted) {
5117 if (lastAcceptedChar == ':')
5118 ME_MoveCursorChars(editor, &cursor, -1);
5119 *candidate_max = cursor;
5120 return TRUE;
5121 }
5122 }
5123
5124 /* Reaching this point means no span was found, so get next span */
5125 if (!ME_NextRun(&cursor.pPara, &cursor.pRun)) {
5126 if (candidateStarted) {
5127 /* There are no further runs, so take end of text as end of candidate */
5128 cursor.nOffset = str - strStart;
5129 if (lastAcceptedChar == ':')
5130 ME_MoveCursorChars(editor, &cursor, -1);
5131 *candidate_max = cursor;
5132 return TRUE;
5133 }
5134 *candidate_max = *candidate_min = cursor;
5135 return FALSE;
5136 }
5137 cursor.nOffset = 0;
5138 }
5139
5140 if (candidateStarted) {
5141 /* There are no further runs, so take end of text as end of candidate */
5142 if (lastAcceptedChar == ':')
5143 ME_MoveCursorChars(editor, &cursor, -1);
5144 *candidate_max = cursor;
5145 return TRUE;
5146 }
5147 *candidate_max = *candidate_min = cursor;
5148 return FALSE;
5149 }
5150
5151 /**
5152 * This proc evaluates the selection and returns TRUE if it can be considered an URL
5153 */
5154 static BOOL ME_IsCandidateAnURL(ME_TextEditor *editor, const ME_Cursor *start, int nChars)
5155 {
5156 #define MAX_PREFIX_LEN 9
5157 struct prefix_s {
5158 const WCHAR text[MAX_PREFIX_LEN];
5159 int length;
5160 }prefixes[] = {
5161 {{'p','r','o','s','p','e','r','o',':'}, 9},
5162 {{'t','e','l','n','e','t',':'}, 7},
5163 {{'g','o','p','h','e','r',':'}, 7},
5164 {{'m','a','i','l','t','o',':'}, 7},
5165 {{'h','t','t','p','s',':'}, 6},
5166 {{'f','i','l','e',':'}, 5},
5167 {{'n','e','w','s',':'}, 5},
5168 {{'w','a','i','s',':'}, 5},
5169 {{'n','n','t','p',':'}, 5},
5170 {{'h','t','t','p',':'}, 5},
5171 {{'w','w','w','.'}, 4},
5172 {{'f','t','p',':'}, 4},
5173 };
5174 WCHAR bufferW[MAX_PREFIX_LEN + 1];
5175 unsigned int i;
5176
5177 ME_GetTextW(editor, bufferW, MAX_PREFIX_LEN, start, nChars, FALSE, FALSE);
5178 for (i = 0; i < sizeof(prefixes) / sizeof(*prefixes); i++)
5179 {
5180 if (nChars < prefixes[i].length) continue;
5181 if (!memcmp(prefixes[i].text, bufferW, prefixes[i].length * sizeof(WCHAR)))
5182 return TRUE;
5183 }
5184 return FALSE;
5185 #undef MAX_PREFIX_LEN
5186 }
5187
5188 /**
5189 * This proc walks through the indicated selection and evaluates whether each
5190 * section identified by ME_FindNextURLCandidate and in-between sections have
5191 * their proper CFE_LINK attributes set or unset. If the CFE_LINK attribute is
5192 * not what it is supposed to be, this proc sets or unsets it as appropriate.
5193 *
5194 * Since this function can cause runs to be split, do not depend on the value
5195 * of the start cursor at the end of the function.
5196 *
5197 * nChars may be set to INT_MAX to update to the end of the text.
5198 *
5199 * Returns TRUE if at least one section was modified.
5200 */
5201 static BOOL ME_UpdateLinkAttribute(ME_TextEditor *editor, ME_Cursor *start, int nChars)
5202 {
5203 BOOL modified = FALSE;
5204 ME_Cursor startCur = *start;
5205
5206 if (!editor->AutoURLDetect_bEnable) return FALSE;
5207
5208 do
5209 {
5210 CHARFORMAT2W link;
5211 ME_Cursor candidateStart, candidateEnd;
5212
5213 if (ME_FindNextURLCandidate(editor, &startCur, nChars,
5214 &candidateStart, &candidateEnd))
5215 {
5216 /* Section before candidate is not an URL */
5217 int cMin = ME_GetCursorOfs(&candidateStart);
5218 int cMax = ME_GetCursorOfs(&candidateEnd);
5219
5220 if (!ME_IsCandidateAnURL(editor, &candidateStart, cMax - cMin))
5221 candidateStart = candidateEnd;
5222 nChars -= cMax - ME_GetCursorOfs(&startCur);
5223 }
5224 else
5225 {
5226 /* No more candidates until end of selection */
5227 nChars = 0;
5228 }
5229
5230 if (startCur.pRun != candidateStart.pRun ||
5231 startCur.nOffset != candidateStart.nOffset)
5232 {
5233 /* CFE_LINK effect should be consistently unset */
5234 link.cbSize = sizeof(link);
5235 ME_GetCharFormat(editor, &startCur, &candidateStart, &link);
5236 if (!(link.dwMask & CFM_LINK) || (link.dwEffects & CFE_LINK))
5237 {
5238 /* CFE_LINK must be unset from this range */
5239 memset(&link, 0, sizeof(CHARFORMAT2W));
5240 link.cbSize = sizeof(link);
5241 link.dwMask = CFM_LINK;
5242 link.dwEffects = 0;
5243 ME_SetCharFormat(editor, &startCur, &candidateStart, &link);
5244 /* Update candidateEnd since setting character formats may split
5245 * runs, which can cause a cursor to be at an invalid offset within
5246 * a split run. */
5247 while (candidateEnd.nOffset >= candidateEnd.pRun->member.run.len)
5248 {
5249 candidateEnd.nOffset -= candidateEnd.pRun->member.run.len;
5250 candidateEnd.pRun = ME_FindItemFwd(candidateEnd.pRun, diRun);
5251 }
5252 modified = TRUE;
5253 }
5254 }
5255 if (candidateStart.pRun != candidateEnd.pRun ||
5256 candidateStart.nOffset != candidateEnd.nOffset)
5257 {
5258 /* CFE_LINK effect should be consistently set */
5259 link.cbSize = sizeof(link);
5260 ME_GetCharFormat(editor, &candidateStart, &candidateEnd, &link);
5261 if (!(link.dwMask & CFM_LINK) || !(link.dwEffects & CFE_LINK))
5262 {
5263 /* CFE_LINK must be set on this range */
5264 memset(&link, 0, sizeof(CHARFORMAT2W));
5265 link.cbSize = sizeof(link);
5266 link.dwMask = CFM_LINK;
5267 link.dwEffects = CFE_LINK;
5268 ME_SetCharFormat(editor, &candidateStart, &candidateEnd, &link);
5269 modified = TRUE;
5270 }
5271 }
5272 startCur = candidateEnd;
5273 } while (nChars > 0);
5274 return modified;
5275 }