push c6fcfc519a04d046be60ec60e33d075a2146cc03
[wine/hacks.git] / dlls / riched20 / editor.c
blobb67ca6045dd8611dc4528c557364d9cd9a711ecc
1 /*
2 * RichEdit - functions dealing with editor object
4 * Copyright 2004 by Krzysztof Foltman
5 * Copyright 2005 by Cihan Altinay
6 * Copyright 2005 by Phil Krylov
7 * Copyright 2008 Eric Pouech
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.
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.
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
24 /*
25 API implementation status:
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
145 - WM_STYLECHANGED (things like read-only flag)
146 + WM_UNICHAR
147 + WM_VSCROLL
149 Notifications
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
171 Styles
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
194 * RICHED20 TODO (incomplete):
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
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?
227 #include "editor.h"
228 #include "commdlg.h"
229 #include "winreg.h"
230 #define NO_SHLWAPI_STREAM
231 #include "shlwapi.h"
232 #include "rtf.h"
233 #include "imm.h"
234 #include "res.h"
236 #define STACK_SIZE_DEFAULT 100
237 #define STACK_SIZE_MAX 1000
239 #define TEXT_LIMIT_DEFAULT 32767
241 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
243 static BOOL ME_RegisterEditorClass(HINSTANCE);
244 static BOOL ME_UpdateLinkAttribute(ME_TextEditor *editor, int sel_min, int sel_max);
246 static const WCHAR RichEdit20W[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '2', '0', 'W', 0};
247 static const WCHAR RichEdit50W[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '5', '0', 'W', 0};
248 static const WCHAR REListBox20W[] = {'R','E','L','i','s','t','B','o','x','2','0','W', 0};
249 static const WCHAR REComboBox20W[] = {'R','E','C','o','m','b','o','B','o','x','2','0','W', 0};
250 static HCURSOR hLeft;
252 int me_debug = 0;
253 HANDLE me_heap = NULL;
255 static BOOL ME_ListBoxRegistered = FALSE;
256 static BOOL ME_ComboBoxRegistered = FALSE;
258 static inline int is_version_nt(void)
260 return !(GetVersion() & 0x80000000);
263 static ME_TextBuffer *ME_MakeText(void) {
265 ME_TextBuffer *buf = ALLOC_OBJ(ME_TextBuffer);
267 ME_DisplayItem *p1 = ME_MakeDI(diTextStart);
268 ME_DisplayItem *p2 = ME_MakeDI(diTextEnd);
270 p1->prev = NULL;
271 p1->next = p2;
272 p2->prev = p1;
273 p2->next = NULL;
274 p1->member.para.next_para = p2;
275 p2->member.para.prev_para = p1;
276 p2->member.para.nCharOfs = 0;
278 buf->pFirst = p1;
279 buf->pLast = p2;
280 buf->pCharStyle = NULL;
282 return buf;
286 static LRESULT ME_StreamInText(ME_TextEditor *editor, DWORD dwFormat, ME_InStream *stream, ME_Style *style)
288 WCHAR wszText[STREAMIN_BUFFER_SIZE+1];
289 WCHAR *pText;
291 TRACE("%08x %p\n", dwFormat, stream);
293 do {
294 long nWideChars = 0;
296 if (!stream->dwSize)
298 ME_StreamInFill(stream);
299 if (stream->editstream->dwError)
300 break;
301 if (!stream->dwSize)
302 break;
305 if (!(dwFormat & SF_UNICODE))
307 /* FIXME? this is doomed to fail on true MBCS like UTF-8, luckily they're unlikely to be used as CP_ACP */
308 nWideChars = MultiByteToWideChar(CP_ACP, 0, stream->buffer, stream->dwSize, wszText, STREAMIN_BUFFER_SIZE);
309 pText = wszText;
311 else
313 nWideChars = stream->dwSize >> 1;
314 pText = (WCHAR *)stream->buffer;
317 ME_InsertTextFromCursor(editor, 0, pText, nWideChars, style);
318 if (stream->dwSize == 0)
319 break;
320 stream->dwSize = 0;
321 } while(1);
322 ME_CommitUndo(editor);
323 ME_UpdateRepaint(editor);
324 return 0;
327 static void ME_ApplyBorderProperties(RTF_Info *info,
328 ME_BorderRect *borderRect,
329 RTFBorder *borderDef)
331 int i, colorNum;
332 ME_Border *pBorders[] = {&borderRect->top,
333 &borderRect->left,
334 &borderRect->bottom,
335 &borderRect->right};
336 for (i = 0; i < 4; i++)
338 RTFColor *colorDef = info->colorList;
339 pBorders[i]->width = borderDef[i].width;
340 colorNum = borderDef[i].color;
341 while (colorDef && colorDef->rtfCNum != colorNum)
342 colorDef = colorDef->rtfNextColor;
343 if (colorDef)
344 pBorders[i]->colorRef = RGB(
345 colorDef->rtfCRed >= 0 ? colorDef->rtfCRed : 0,
346 colorDef->rtfCGreen >= 0 ? colorDef->rtfCGreen : 0,
347 colorDef->rtfCBlue >= 0 ? colorDef->rtfCBlue : 0);
348 else
349 pBorders[i]->colorRef = RGB(0, 0, 0);
353 void ME_RTFCharAttrHook(RTF_Info *info)
355 CHARFORMAT2W fmt;
356 fmt.cbSize = sizeof(fmt);
357 fmt.dwMask = 0;
358 fmt.dwEffects = 0;
360 switch(info->rtfMinor)
362 case rtfPlain:
363 /* FIXME add more flags once they're implemented */
364 fmt.dwMask = CFM_BOLD | CFM_ITALIC | CFM_UNDERLINETYPE | CFM_STRIKEOUT | CFM_COLOR | CFM_BACKCOLOR | CFM_SIZE | CFM_WEIGHT;
365 fmt.dwEffects = CFE_AUTOCOLOR | CFE_AUTOBACKCOLOR;
366 fmt.yHeight = 12*20; /* 12pt */
367 fmt.wWeight = FW_NORMAL;
368 fmt.bUnderlineType = CFU_UNDERLINENONE;
369 break;
370 case rtfBold:
371 fmt.dwMask = CFM_BOLD | CFM_WEIGHT;
372 fmt.dwEffects = info->rtfParam ? CFE_BOLD : 0;
373 fmt.wWeight = info->rtfParam ? FW_BOLD : FW_NORMAL;
374 break;
375 case rtfItalic:
376 fmt.dwMask = CFM_ITALIC;
377 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
378 break;
379 case rtfUnderline:
380 fmt.dwMask = CFM_UNDERLINETYPE;
381 fmt.bUnderlineType = info->rtfParam ? CFU_CF1UNDERLINE : CFU_UNDERLINENONE;
382 break;
383 case rtfDotUnderline:
384 fmt.dwMask = CFM_UNDERLINETYPE;
385 fmt.bUnderlineType = info->rtfParam ? CFU_UNDERLINEDOTTED : CFU_UNDERLINENONE;
386 break;
387 case rtfDbUnderline:
388 fmt.dwMask = CFM_UNDERLINETYPE;
389 fmt.bUnderlineType = info->rtfParam ? CFU_UNDERLINEDOUBLE : CFU_UNDERLINENONE;
390 break;
391 case rtfWordUnderline:
392 fmt.dwMask = CFM_UNDERLINETYPE;
393 fmt.bUnderlineType = info->rtfParam ? CFU_UNDERLINEWORD : CFU_UNDERLINENONE;
394 break;
395 case rtfNoUnderline:
396 fmt.dwMask = CFM_UNDERLINETYPE;
397 fmt.bUnderlineType = CFU_UNDERLINENONE;
398 break;
399 case rtfStrikeThru:
400 fmt.dwMask = CFM_STRIKEOUT;
401 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
402 break;
403 case rtfSubScript:
404 case rtfSuperScript:
405 case rtfSubScrShrink:
406 case rtfSuperScrShrink:
407 case rtfNoSuperSub:
408 fmt.dwMask = CFM_SUBSCRIPT|CFM_SUPERSCRIPT;
409 if (info->rtfMinor == rtfSubScrShrink) fmt.dwEffects = CFE_SUBSCRIPT;
410 if (info->rtfMinor == rtfSuperScrShrink) fmt.dwEffects = CFE_SUPERSCRIPT;
411 if (info->rtfMinor == rtfNoSuperSub) fmt.dwEffects = 0;
412 break;
413 case rtfInvisible:
414 fmt.dwMask = CFM_HIDDEN;
415 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
416 break;
417 case rtfBackColor:
418 fmt.dwMask = CFM_BACKCOLOR;
419 fmt.dwEffects = 0;
420 if (info->rtfParam == 0)
421 fmt.dwEffects = CFE_AUTOBACKCOLOR;
422 else if (info->rtfParam != rtfNoParam)
424 RTFColor *c = RTFGetColor(info, info->rtfParam);
425 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
427 break;
428 case rtfForeColor:
429 fmt.dwMask = CFM_COLOR;
430 fmt.dwEffects = 0;
431 if (info->rtfParam == 0)
432 fmt.dwEffects = CFE_AUTOCOLOR;
433 else if (info->rtfParam != rtfNoParam)
435 RTFColor *c = RTFGetColor(info, info->rtfParam);
436 if (c)
437 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
438 else
439 fmt.crTextColor = 0;
441 break;
442 case rtfFontNum:
443 if (info->rtfParam != rtfNoParam)
445 RTFFont *f = RTFGetFont(info, info->rtfParam);
446 if (f)
448 MultiByteToWideChar(CP_ACP, 0, f->rtfFName, -1, fmt.szFaceName, sizeof(fmt.szFaceName)/sizeof(WCHAR));
449 fmt.szFaceName[sizeof(fmt.szFaceName)/sizeof(WCHAR)-1] = '\0';
450 fmt.bCharSet = f->rtfFCharSet;
451 fmt.dwMask = CFM_FACE | CFM_CHARSET;
452 fmt.bPitchAndFamily = f->rtfFPitch | (f->rtfFFamily << 4);
455 break;
456 case rtfFontSize:
457 fmt.dwMask = CFM_SIZE;
458 if (info->rtfParam != rtfNoParam)
459 fmt.yHeight = info->rtfParam*10;
460 break;
462 if (fmt.dwMask) {
463 ME_Style *style2;
464 RTFFlushOutputBuffer(info);
465 /* FIXME too slow ? how come ? */
466 style2 = ME_ApplyStyle(info->style, &fmt);
467 ME_ReleaseStyle(info->style);
468 info->style = style2;
469 info->styleChanged = TRUE;
473 /* FIXME this function doesn't get any information about context of the RTF tag, which is very bad,
474 the same tags mean different things in different contexts */
475 void ME_RTFParAttrHook(RTF_Info *info)
477 PARAFORMAT2 fmt;
478 fmt.cbSize = sizeof(fmt);
479 fmt.dwMask = 0;
481 switch(info->rtfMinor)
483 case rtfParDef: /* restores default paragraph attributes */
484 if (!info->editor->bEmulateVersion10) /* v4.1 */
485 info->borderType = RTFBorderParaLeft;
486 else /* v1.0 - 3.0 */
487 info->borderType = RTFBorderParaTop;
488 fmt.dwMask = PFM_ALIGNMENT | PFM_BORDER | PFM_LINESPACING | PFM_TABSTOPS |
489 PFM_OFFSET | PFM_RIGHTINDENT | PFM_SPACEAFTER | PFM_SPACEBEFORE |
490 PFM_STARTINDENT;
491 /* TODO: numbering, shading */
492 fmt.wAlignment = PFA_LEFT;
493 fmt.cTabCount = 0;
494 fmt.dxOffset = fmt.dxStartIndent = fmt.dxRightIndent = 0;
495 fmt.wBorderWidth = fmt.wBorders = 0;
496 fmt.wBorderSpace = 0;
497 fmt.bLineSpacingRule = 0;
498 fmt.dySpaceBefore = fmt.dySpaceAfter = 0;
499 fmt.dyLineSpacing = 0;
500 if (!info->editor->bEmulateVersion10) /* v4.1 */
502 if (info->tableDef && info->tableDef->tableRowStart &&
503 info->tableDef->tableRowStart->member.para.nFlags & MEPF_ROWEND)
505 ME_Cursor cursor;
506 ME_DisplayItem *para;
507 /* We are just after a table row. */
508 RTFFlushOutputBuffer(info);
509 cursor = info->editor->pCursors[0];
510 para = ME_GetParagraph(cursor.pRun);
511 if (para == info->tableDef->tableRowStart->member.para.next_para
512 && !cursor.nOffset && !cursor.pRun->member.run.nCharOfs)
514 /* Since the table row end, no text has been inserted, and the \intbl
515 * control word has not be used. We can confirm that we are not in a
516 * table anymore.
518 info->tableDef->tableRowStart = NULL;
519 info->canInheritInTbl = FALSE;
522 } else { /* v1.0 - v3.0 */
523 fmt.dwMask |= PFM_TABLE;
524 fmt.wEffects &= ~PFE_TABLE;
526 break;
527 case rtfNestLevel:
528 if (!info->editor->bEmulateVersion10) /* v4.1 */
530 while (info->rtfParam > info->nestingLevel) {
531 RTFTable *tableDef = ALLOC_OBJ(RTFTable);
532 ZeroMemory(tableDef, sizeof(RTFTable));
533 tableDef->parent = info->tableDef;
534 info->tableDef = tableDef;
536 RTFFlushOutputBuffer(info);
537 if (tableDef->tableRowStart &&
538 tableDef->tableRowStart->member.para.nFlags & MEPF_ROWEND)
540 ME_DisplayItem *para = tableDef->tableRowStart;
541 para = para->member.para.next_para;
542 para = ME_InsertTableRowStartAtParagraph(info->editor, para);
543 tableDef->tableRowStart = para;
544 } else {
545 ME_Cursor cursor;
546 WCHAR endl = '\r';
547 cursor = info->editor->pCursors[0];
548 if (cursor.nOffset || cursor.pRun->member.run.nCharOfs)
549 ME_InsertTextFromCursor(info->editor, 0, &endl, 1, info->style);
550 tableDef->tableRowStart = ME_InsertTableRowStartFromCursor(info->editor);
553 info->nestingLevel++;
555 info->canInheritInTbl = FALSE;
557 break;
558 case rtfInTable:
560 if (!info->editor->bEmulateVersion10) /* v4.1 */
562 if (info->nestingLevel < 1)
564 RTFTable *tableDef;
565 if (!info->tableDef)
567 info->tableDef = ALLOC_OBJ(RTFTable);
568 ZeroMemory(info->tableDef, sizeof(RTFTable));
570 tableDef = info->tableDef;
571 RTFFlushOutputBuffer(info);
572 if (tableDef->tableRowStart &&
573 tableDef->tableRowStart->member.para.nFlags & MEPF_ROWEND)
575 ME_DisplayItem *para = tableDef->tableRowStart;
576 para = para->member.para.next_para;
577 para = ME_InsertTableRowStartAtParagraph(info->editor, para);
578 tableDef->tableRowStart = para;
579 } else {
580 ME_Cursor cursor;
581 WCHAR endl = '\r';
582 cursor = info->editor->pCursors[0];
583 if (cursor.nOffset || cursor.pRun->member.run.nCharOfs)
584 ME_InsertTextFromCursor(info->editor, 0, &endl, 1, info->style);
585 tableDef->tableRowStart = ME_InsertTableRowStartFromCursor(info->editor);
587 info->nestingLevel = 1;
588 info->canInheritInTbl = TRUE;
590 return;
591 } else { /* v1.0 - v3.0 */
592 fmt.dwMask |= PFM_TABLE;
593 fmt.wEffects |= PFE_TABLE;
595 break;
597 case rtfFirstIndent:
598 ME_GetSelectionParaFormat(info->editor, &fmt);
599 fmt.dwMask = PFM_STARTINDENT | PFM_OFFSET;
600 fmt.dxStartIndent += fmt.dxOffset + info->rtfParam;
601 fmt.dxOffset = -info->rtfParam;
602 break;
603 case rtfLeftIndent:
604 ME_GetSelectionParaFormat(info->editor, &fmt);
605 fmt.dwMask = PFM_STARTINDENT;
606 fmt.dxStartIndent = info->rtfParam - fmt.dxOffset;
607 break;
608 case rtfRightIndent:
609 fmt.dwMask = PFM_RIGHTINDENT;
610 fmt.dxRightIndent = info->rtfParam;
611 break;
612 case rtfQuadLeft:
613 case rtfQuadJust:
614 fmt.dwMask = PFM_ALIGNMENT;
615 fmt.wAlignment = PFA_LEFT;
616 break;
617 case rtfQuadRight:
618 fmt.dwMask = PFM_ALIGNMENT;
619 fmt.wAlignment = PFA_RIGHT;
620 break;
621 case rtfQuadCenter:
622 fmt.dwMask = PFM_ALIGNMENT;
623 fmt.wAlignment = PFA_CENTER;
624 break;
625 case rtfTabPos:
626 ME_GetSelectionParaFormat(info->editor, &fmt);
627 if (!(fmt.dwMask & PFM_TABSTOPS))
629 fmt.cTabCount = 0;
631 if (fmt.cTabCount < MAX_TAB_STOPS && info->rtfParam < 0x1000000)
632 fmt.rgxTabs[fmt.cTabCount++] = info->rtfParam;
633 fmt.dwMask = PFM_TABSTOPS;
634 break;
635 case rtfKeep:
636 fmt.dwMask = PFM_KEEP;
637 fmt.wEffects = PFE_KEEP;
638 break;
639 case rtfNoWidowControl:
640 fmt.dwMask = PFM_NOWIDOWCONTROL;
641 fmt.wEffects = PFE_NOWIDOWCONTROL;
642 break;
643 case rtfKeepNext:
644 fmt.dwMask = PFM_KEEPNEXT;
645 fmt.wEffects = PFE_KEEPNEXT;
646 break;
647 case rtfSpaceAfter:
648 fmt.dwMask = PFM_SPACEAFTER;
649 fmt.dySpaceAfter = info->rtfParam;
650 break;
651 case rtfSpaceBefore:
652 fmt.dwMask = PFM_SPACEBEFORE;
653 fmt.dySpaceBefore = info->rtfParam;
654 break;
655 case rtfSpaceBetween:
656 fmt.dwMask = PFM_LINESPACING;
657 if ((int)info->rtfParam > 0)
659 fmt.dyLineSpacing = info->rtfParam;
660 fmt.bLineSpacingRule = 3;
662 else
664 fmt.dyLineSpacing = info->rtfParam;
665 fmt.bLineSpacingRule = 4;
667 case rtfSpaceMultiply:
668 fmt.dwMask = PFM_LINESPACING;
669 fmt.dyLineSpacing = info->rtfParam * 20;
670 fmt.bLineSpacingRule = 5;
671 break;
672 case rtfParBullet:
673 fmt.dwMask = PFM_NUMBERING;
674 fmt.wNumbering = PFN_BULLET;
675 break;
676 case rtfParSimple:
677 fmt.dwMask = PFM_NUMBERING;
678 fmt.wNumbering = 2; /* FIXME: MSDN says it's not used ?? */
679 break;
680 case rtfParNumDecimal:
681 fmt.dwMask = PFM_NUMBERING;
682 fmt.wNumbering = 2; /* FIXME: MSDN says it's not used ?? */
683 break;
684 case rtfParNumIndent:
685 fmt.dwMask = PFM_NUMBERINGTAB;
686 fmt.wNumberingTab = info->rtfParam;
687 break;
688 case rtfParNumStartAt:
689 fmt.dwMask = PFM_NUMBERINGSTART;
690 fmt.wNumberingStart = info->rtfParam;
691 break;
692 case rtfBorderLeft:
693 info->borderType = RTFBorderParaLeft;
694 ME_GetSelectionParaFormat(info->editor, &fmt);
695 if (!(fmt.dwMask & PFM_BORDER))
697 fmt.wBorderSpace = 0;
698 fmt.wBorderWidth = 1;
699 fmt.wBorders = 0;
701 fmt.wBorders |= 1;
702 fmt.dwMask = PFM_BORDER;
703 break;
704 case rtfBorderRight:
705 info->borderType = RTFBorderParaRight;
706 ME_GetSelectionParaFormat(info->editor, &fmt);
707 if (!(fmt.dwMask & PFM_BORDER))
709 fmt.wBorderSpace = 0;
710 fmt.wBorderWidth = 1;
711 fmt.wBorders = 0;
713 fmt.wBorders |= 2;
714 fmt.dwMask = PFM_BORDER;
715 break;
716 case rtfBorderTop:
717 info->borderType = RTFBorderParaTop;
718 ME_GetSelectionParaFormat(info->editor, &fmt);
719 if (!(fmt.dwMask & PFM_BORDER))
721 fmt.wBorderSpace = 0;
722 fmt.wBorderWidth = 1;
723 fmt.wBorders = 0;
725 fmt.wBorders |= 4;
726 fmt.dwMask = PFM_BORDER;
727 break;
728 case rtfBorderBottom:
729 info->borderType = RTFBorderParaBottom;
730 ME_GetSelectionParaFormat(info->editor, &fmt);
731 if (!(fmt.dwMask & PFM_BORDER))
733 fmt.wBorderSpace = 0;
734 fmt.wBorderWidth = 1;
735 fmt.wBorders = 0;
737 fmt.wBorders |= 8;
738 fmt.dwMask = PFM_BORDER;
739 break;
740 case rtfBorderSingle:
741 ME_GetSelectionParaFormat(info->editor, &fmt);
742 /* we assume that borders have been created before (RTF spec) */
743 fmt.wBorders &= ~0x700;
744 fmt.wBorders |= 1 << 8;
745 fmt.dwMask = PFM_BORDER;
746 break;
747 case rtfBorderThick:
748 ME_GetSelectionParaFormat(info->editor, &fmt);
749 /* we assume that borders have been created before (RTF spec) */
750 fmt.wBorders &= ~0x700;
751 fmt.wBorders |= 2 << 8;
752 fmt.dwMask = PFM_BORDER;
753 break;
754 case rtfBorderShadow:
755 ME_GetSelectionParaFormat(info->editor, &fmt);
756 /* we assume that borders have been created before (RTF spec) */
757 fmt.wBorders &= ~0x700;
758 fmt.wBorders |= 10 << 8;
759 fmt.dwMask = PFM_BORDER;
760 break;
761 case rtfBorderDouble:
762 ME_GetSelectionParaFormat(info->editor, &fmt);
763 /* we assume that borders have been created before (RTF spec) */
764 fmt.wBorders &= ~0x700;
765 fmt.wBorders |= 7 << 8;
766 fmt.dwMask = PFM_BORDER;
767 break;
768 case rtfBorderDot:
769 ME_GetSelectionParaFormat(info->editor, &fmt);
770 /* we assume that borders have been created before (RTF spec) */
771 fmt.wBorders &= ~0x700;
772 fmt.wBorders |= 11 << 8;
773 fmt.dwMask = PFM_BORDER;
774 break;
775 case rtfBorderWidth:
777 int borderSide = info->borderType & RTFBorderSideMask;
778 RTFTable *tableDef = info->tableDef;
779 ME_GetSelectionParaFormat(info->editor, &fmt);
780 /* we assume that borders have been created before (RTF spec) */
781 fmt.wBorderWidth |= ((info->rtfParam / 15) & 7) << 8;
782 if ((info->borderType & RTFBorderTypeMask) == RTFBorderTypeCell)
784 RTFBorder *border;
785 if (!tableDef || tableDef->numCellsDefined >= MAX_TABLE_CELLS)
786 break;
787 border = &tableDef->cells[tableDef->numCellsDefined].border[borderSide];
788 border->width = info->rtfParam;
789 break;
791 fmt.dwMask = PFM_BORDER;
792 break;
794 case rtfBorderSpace:
795 ME_GetSelectionParaFormat(info->editor, &fmt);
796 /* we assume that borders have been created before (RTF spec) */
797 fmt.wBorderSpace = info->rtfParam;
798 fmt.dwMask = PFM_BORDER;
799 break;
800 case rtfBorderColor:
802 RTFTable *tableDef = info->tableDef;
803 int borderSide = info->borderType & RTFBorderSideMask;
804 int borderType = info->borderType & RTFBorderTypeMask;
805 switch(borderType)
807 case RTFBorderTypePara:
808 if (!info->editor->bEmulateVersion10) /* v4.1 */
809 break;
810 /* v1.0 - 3.0 treat paragraph and row borders the same. */
811 case RTFBorderTypeRow:
812 if (tableDef) {
813 tableDef->border[borderSide].color = info->rtfParam;
815 break;
816 case RTFBorderTypeCell:
817 if (tableDef && tableDef->numCellsDefined < MAX_TABLE_CELLS) {
818 tableDef->cells[tableDef->numCellsDefined].border[borderSide].color = info->rtfParam;
820 break;
822 break;
825 if (fmt.dwMask) {
826 RTFFlushOutputBuffer(info);
827 /* FIXME too slow ? how come ?*/
828 ME_SetSelectionParaFormat(info->editor, &fmt);
832 void ME_RTFTblAttrHook(RTF_Info *info)
834 switch (info->rtfMinor)
836 case rtfRowDef:
838 if (!info->editor->bEmulateVersion10) /* v4.1 */
839 info->borderType = 0; /* Not sure */
840 else /* v1.0 - 3.0 */
841 info->borderType = RTFBorderRowTop;
842 if (!info->tableDef) {
843 info->tableDef = ME_MakeTableDef(info->editor);
844 } else {
845 ME_InitTableDef(info->editor, info->tableDef);
847 break;
849 case rtfCellPos:
851 int cellNum;
852 if (!info->tableDef)
854 info->tableDef = ME_MakeTableDef(info->editor);
856 cellNum = info->tableDef->numCellsDefined;
857 if (cellNum >= MAX_TABLE_CELLS)
858 break;
859 info->tableDef->cells[cellNum].rightBoundary = info->rtfParam;
860 if (cellNum < MAX_TAB_STOPS) {
861 /* Tab stops were used to store cell positions before v4.1 but v4.1
862 * still seems to set the tabstops without using them. */
863 ME_DisplayItem *para = ME_GetParagraph(info->editor->pCursors[0].pRun);
864 PARAFORMAT2 *pFmt = para->member.para.pFmt;
865 pFmt->rgxTabs[cellNum] &= ~0x00FFFFFF;
866 pFmt->rgxTabs[cellNum] = 0x00FFFFFF & info->rtfParam;
868 info->tableDef->numCellsDefined++;
869 break;
871 case rtfRowBordTop:
872 info->borderType = RTFBorderRowTop;
873 break;
874 case rtfRowBordLeft:
875 info->borderType = RTFBorderRowLeft;
876 break;
877 case rtfRowBordBottom:
878 info->borderType = RTFBorderRowBottom;
879 break;
880 case rtfRowBordRight:
881 info->borderType = RTFBorderRowRight;
882 break;
883 case rtfCellBordTop:
884 info->borderType = RTFBorderCellTop;
885 break;
886 case rtfCellBordLeft:
887 info->borderType = RTFBorderCellLeft;
888 break;
889 case rtfCellBordBottom:
890 info->borderType = RTFBorderCellBottom;
891 break;
892 case rtfCellBordRight:
893 info->borderType = RTFBorderCellRight;
894 break;
895 case rtfRowGapH:
896 if (info->tableDef)
897 info->tableDef->gapH = info->rtfParam;
898 break;
899 case rtfRowLeftEdge:
900 if (info->tableDef)
901 info->tableDef->leftEdge = info->rtfParam;
902 break;
906 void ME_RTFSpecialCharHook(RTF_Info *info)
908 RTFTable *tableDef = info->tableDef;
909 switch (info->rtfMinor)
911 case rtfNestCell:
912 if (info->editor->bEmulateVersion10) /* v1.0 - v3.0 */
913 break;
914 /* else fall through since v4.1 treats rtfNestCell and rtfCell the same */
915 case rtfCell:
916 if (!tableDef)
917 break;
918 RTFFlushOutputBuffer(info);
919 if (!info->editor->bEmulateVersion10) { /* v4.1 */
920 if (tableDef->tableRowStart)
922 if (!info->nestingLevel &&
923 tableDef->tableRowStart->member.para.nFlags & MEPF_ROWEND)
925 ME_DisplayItem *para = tableDef->tableRowStart;
926 para = para->member.para.next_para;
927 para = ME_InsertTableRowStartAtParagraph(info->editor, para);
928 tableDef->tableRowStart = para;
929 info->nestingLevel = 1;
931 ME_InsertTableCellFromCursor(info->editor);
933 } else { /* v1.0 - v3.0 */
934 ME_DisplayItem *para = ME_GetParagraph(info->editor->pCursors[0].pRun);
935 PARAFORMAT2 *pFmt = para->member.para.pFmt;
936 if (pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE &&
937 tableDef->numCellsInserted < tableDef->numCellsDefined)
939 WCHAR tab = '\t';
940 ME_InsertTextFromCursor(info->editor, 0, &tab, 1, info->style);
941 tableDef->numCellsInserted++;
944 break;
945 case rtfNestRow:
946 if (info->editor->bEmulateVersion10) /* v1.0 - v3.0 */
947 break;
948 /* else fall through since v4.1 treats rtfNestRow and rtfRow the same */
949 case rtfRow:
951 ME_DisplayItem *para, *cell, *run;
952 int i;
954 if (!tableDef)
955 break;
956 RTFFlushOutputBuffer(info);
957 if (!info->editor->bEmulateVersion10) { /* v4.1 */
958 if (!tableDef->tableRowStart)
959 break;
960 if (!info->nestingLevel &&
961 tableDef->tableRowStart->member.para.nFlags & MEPF_ROWEND)
963 para = tableDef->tableRowStart;
964 para = para->member.para.next_para;
965 para = ME_InsertTableRowStartAtParagraph(info->editor, para);
966 tableDef->tableRowStart = para;
967 info->nestingLevel++;
969 para = tableDef->tableRowStart;
970 cell = ME_FindItemFwd(para, diCell);
971 assert(cell && !cell->member.cell.prev_cell);
972 if (tableDef->numCellsDefined < 1)
974 /* 2000 twips appears to be the cell size that native richedit uses
975 * when no cell sizes are specified. */
976 const int defaultCellSize = 2000;
977 int nRightBoundary = defaultCellSize;
978 cell->member.cell.nRightBoundary = nRightBoundary;
979 while (cell->member.cell.next_cell) {
980 cell = cell->member.cell.next_cell;
981 nRightBoundary += defaultCellSize;
982 cell->member.cell.nRightBoundary = nRightBoundary;
984 para = ME_InsertTableCellFromCursor(info->editor);
985 cell = para->member.para.pCell;
986 cell->member.cell.nRightBoundary = nRightBoundary;
987 } else {
988 for (i = 0; i < tableDef->numCellsDefined; i++)
990 RTFCell *cellDef = &tableDef->cells[i];
991 cell->member.cell.nRightBoundary = cellDef->rightBoundary;
992 ME_ApplyBorderProperties(info, &cell->member.cell.border,
993 cellDef->border);
994 cell = cell->member.cell.next_cell;
995 if (!cell)
997 para = ME_InsertTableCellFromCursor(info->editor);
998 cell = para->member.para.pCell;
1001 /* Cell for table row delimiter is empty */
1002 cell->member.cell.nRightBoundary = tableDef->cells[i-1].rightBoundary;
1005 run = ME_FindItemFwd(cell, diRun);
1006 if (info->editor->pCursors[0].pRun != run ||
1007 info->editor->pCursors[0].nOffset)
1009 int nOfs, nChars;
1010 /* Delete inserted cells that aren't defined. */
1011 info->editor->pCursors[1].pRun = run;
1012 info->editor->pCursors[1].nOffset = 0;
1013 nOfs = ME_GetCursorOfs(info->editor, 1);
1014 nChars = ME_GetCursorOfs(info->editor, 0) - nOfs;
1015 ME_InternalDeleteText(info->editor, nOfs, nChars, TRUE);
1018 para = ME_InsertTableRowEndFromCursor(info->editor);
1019 para->member.para.pFmt->dxOffset = abs(info->tableDef->gapH);
1020 para->member.para.pFmt->dxStartIndent = info->tableDef->leftEdge;
1021 ME_ApplyBorderProperties(info, &para->member.para.border,
1022 tableDef->border);
1023 info->nestingLevel--;
1024 if (!info->nestingLevel)
1026 if (info->canInheritInTbl) {
1027 tableDef->tableRowStart = para;
1028 } else {
1029 while (info->tableDef) {
1030 tableDef = info->tableDef;
1031 info->tableDef = tableDef->parent;
1032 heap_free(tableDef);
1035 } else {
1036 info->tableDef = tableDef->parent;
1037 heap_free(tableDef);
1039 } else { /* v1.0 - v3.0 */
1040 WCHAR endl = '\r';
1041 ME_DisplayItem *para = ME_GetParagraph(info->editor->pCursors[0].pRun);
1042 PARAFORMAT2 *pFmt = para->member.para.pFmt;
1043 pFmt->dxOffset = info->tableDef->gapH;
1044 pFmt->dxStartIndent = info->tableDef->leftEdge;
1046 para = ME_GetParagraph(info->editor->pCursors[0].pRun);
1047 ME_ApplyBorderProperties(info, &para->member.para.border,
1048 tableDef->border);
1049 while (tableDef->numCellsInserted < tableDef->numCellsDefined)
1051 WCHAR tab = '\t';
1052 ME_InsertTextFromCursor(info->editor, 0, &tab, 1, info->style);
1053 tableDef->numCellsInserted++;
1055 pFmt->cTabCount = min(tableDef->numCellsDefined, MAX_TAB_STOPS);
1056 if (!tableDef->numCellsDefined)
1057 pFmt->wEffects &= ~PFE_TABLE;
1058 ME_InsertTextFromCursor(info->editor, 0, &endl, 1, info->style);
1059 tableDef->numCellsInserted = 0;
1061 break;
1063 case rtfTab:
1064 case rtfPar:
1065 if (info->editor->bEmulateVersion10) { /* v1.0 - 3.0 */
1066 ME_DisplayItem *para;
1067 PARAFORMAT2 *pFmt;
1068 RTFFlushOutputBuffer(info);
1069 para = ME_GetParagraph(info->editor->pCursors[0].pRun);
1070 pFmt = para->member.para.pFmt;
1071 if (pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE)
1073 /* rtfPar is treated like a space within a table. */
1074 info->rtfClass = rtfText;
1075 info->rtfMajor = ' ';
1077 else if (info->rtfMinor == rtfPar && tableDef)
1078 tableDef->numCellsInserted = 0;
1080 break;
1084 static BOOL ME_RTFInsertOleObject(RTF_Info *info, HENHMETAFILE hemf, HBITMAP hbmp,
1085 const SIZEL* sz)
1087 LPOLEOBJECT lpObject = NULL;
1088 LPSTORAGE lpStorage = NULL;
1089 LPOLECLIENTSITE lpClientSite = NULL;
1090 LPDATAOBJECT lpDataObject = NULL;
1091 LPOLECACHE lpOleCache = NULL;
1092 STGMEDIUM stgm;
1093 FORMATETC fm;
1094 CLSID clsid;
1095 BOOL ret = FALSE;
1096 DWORD conn;
1098 if (hemf)
1100 stgm.tymed = TYMED_ENHMF;
1101 stgm.u.hEnhMetaFile = hemf;
1102 fm.cfFormat = CF_ENHMETAFILE;
1104 else if (hbmp)
1106 stgm.tymed = TYMED_GDI;
1107 stgm.u.hBitmap = hbmp;
1108 fm.cfFormat = CF_BITMAP;
1110 stgm.pUnkForRelease = NULL;
1112 fm.ptd = NULL;
1113 fm.dwAspect = DVASPECT_CONTENT;
1114 fm.lindex = -1;
1115 fm.tymed = stgm.tymed;
1117 if (!info->lpRichEditOle)
1119 CreateIRichEditOle(info->editor, (VOID**)&info->lpRichEditOle);
1122 if (OleCreateDefaultHandler(&CLSID_NULL, NULL, &IID_IOleObject, (void**)&lpObject) == S_OK &&
1123 #if 0
1124 /* FIXME: enable it when rich-edit properly implements this method */
1125 IRichEditOle_GetClientSite(info->lpRichEditOle, &lpClientSite) == S_OK &&
1126 IOleObject_SetClientSite(lpObject, lpClientSite) == S_OK &&
1127 #endif
1128 IOleObject_GetUserClassID(lpObject, &clsid) == S_OK &&
1129 IOleObject_QueryInterface(lpObject, &IID_IOleCache, (void**)&lpOleCache) == S_OK &&
1130 IOleCache_Cache(lpOleCache, &fm, 0, &conn) == S_OK &&
1131 IOleObject_QueryInterface(lpObject, &IID_IDataObject, (void**)&lpDataObject) == S_OK &&
1132 IDataObject_SetData(lpDataObject, &fm, &stgm, TRUE) == S_OK)
1134 REOBJECT reobject;
1136 reobject.cbStruct = sizeof(reobject);
1137 reobject.cp = REO_CP_SELECTION;
1138 reobject.clsid = clsid;
1139 reobject.poleobj = lpObject;
1140 reobject.pstg = lpStorage;
1141 reobject.polesite = lpClientSite;
1142 /* convert from twips to .01 mm */
1143 reobject.sizel.cx = MulDiv(sz->cx, 254, 144);
1144 reobject.sizel.cy = MulDiv(sz->cy, 254, 144);
1145 reobject.dvaspect = DVASPECT_CONTENT;
1146 reobject.dwFlags = 0; /* FIXME */
1147 reobject.dwUser = 0;
1149 /* FIXME: could be simpler */
1150 ret = IRichEditOle_InsertObject(info->lpRichEditOle, &reobject) == S_OK;
1153 if (lpObject) IOleObject_Release(lpObject);
1154 if (lpClientSite) IOleClientSite_Release(lpClientSite);
1155 if (lpStorage) IStorage_Release(lpStorage);
1156 if (lpDataObject) IDataObject_Release(lpDataObject);
1157 if (lpOleCache) IOleCache_Release(lpOleCache);
1159 return ret;
1162 static void ME_RTFReadPictGroup(RTF_Info *info)
1164 SIZEL sz;
1165 BYTE* buffer = NULL;
1166 unsigned bufsz, bufidx;
1167 BOOL flip;
1168 BYTE val;
1169 METAFILEPICT mfp;
1170 HENHMETAFILE hemf;
1171 HBITMAP hbmp;
1172 enum gfxkind {gfx_unknown = 0, gfx_enhmetafile, gfx_metafile, gfx_dib} gfx = gfx_unknown;
1174 RTFGetToken (info);
1175 if (info->rtfClass == rtfEOF)
1176 return;
1177 mfp.mm = MM_TEXT;
1178 /* fetch picture type */
1179 if (RTFCheckMM (info, rtfPictAttr, rtfWinMetafile))
1181 mfp.mm = info->rtfParam;
1182 gfx = gfx_metafile;
1184 else if (RTFCheckMM (info, rtfPictAttr, rtfDevIndBitmap))
1186 if (info->rtfParam != 0) FIXME("dibitmap should be 0 (%d)\n", info->rtfParam);
1187 gfx = gfx_dib;
1189 else if (RTFCheckMM (info, rtfPictAttr, rtfEmfBlip))
1191 gfx = gfx_enhmetafile;
1193 else
1195 FIXME("%d %d\n", info->rtfMajor, info->rtfMinor);
1196 goto skip_group;
1198 sz.cx = sz.cy = 0;
1199 /* fetch picture attributes */
1200 for (;;)
1202 RTFGetToken (info);
1203 if (info->rtfClass == rtfEOF)
1204 return;
1205 if (info->rtfClass == rtfText)
1206 break;
1207 if (!RTFCheckCM (info, rtfControl, rtfPictAttr))
1209 ERR("Expected picture attribute (%d %d)\n",
1210 info->rtfClass, info->rtfMajor);
1211 goto skip_group;
1213 else if (RTFCheckMM (info, rtfPictAttr, rtfPicWid))
1215 if (gfx == gfx_metafile) mfp.xExt = info->rtfParam;
1217 else if (RTFCheckMM (info, rtfPictAttr, rtfPicHt))
1219 if (gfx == gfx_metafile) mfp.yExt = info->rtfParam;
1221 else if (RTFCheckMM (info, rtfPictAttr, rtfPicGoalWid))
1222 sz.cx = info->rtfParam;
1223 else if (RTFCheckMM (info, rtfPictAttr, rtfPicGoalHt))
1224 sz.cy = info->rtfParam;
1225 else
1226 FIXME("Non supported attribute: %d %d %d\n", info->rtfClass, info->rtfMajor, info->rtfMinor);
1228 /* fetch picture data */
1229 bufsz = 1024;
1230 bufidx = 0;
1231 buffer = HeapAlloc(GetProcessHeap(), 0, bufsz);
1232 val = info->rtfMajor;
1233 for (flip = TRUE;; flip = !flip)
1235 RTFGetToken (info);
1236 if (info->rtfClass == rtfEOF)
1238 HeapFree(GetProcessHeap(), 0, buffer);
1239 return; /* Warn ?? */
1241 if (RTFCheckCM(info, rtfGroup, rtfEndGroup))
1242 break;
1243 if (info->rtfClass != rtfText) goto skip_group;
1244 if (flip)
1246 if (bufidx >= bufsz &&
1247 !(buffer = HeapReAlloc(GetProcessHeap(), 0, buffer, bufsz += 1024)))
1248 goto skip_group;
1249 buffer[bufidx++] = RTFCharToHex(val) * 16 + RTFCharToHex(info->rtfMajor);
1251 else
1252 val = info->rtfMajor;
1254 if (flip) FIXME("wrong hex string\n");
1256 switch (gfx)
1258 case gfx_enhmetafile:
1259 if ((hemf = SetEnhMetaFileBits(bufidx, buffer)))
1260 ME_RTFInsertOleObject(info, hemf, NULL, &sz);
1261 break;
1262 case gfx_metafile:
1263 if ((hemf = SetWinMetaFileBits(bufidx, buffer, NULL, &mfp)))
1264 ME_RTFInsertOleObject(info, hemf, NULL, &sz);
1265 break;
1266 case gfx_dib:
1268 BITMAPINFO* bi = (BITMAPINFO*)buffer;
1269 HDC hdc = GetDC(0);
1270 unsigned nc = bi->bmiHeader.biClrUsed;
1272 /* not quite right, especially for bitfields type of compression */
1273 if (!nc && bi->bmiHeader.biBitCount <= 8)
1274 nc = 1 << bi->bmiHeader.biBitCount;
1275 if ((hbmp = CreateDIBitmap(hdc, &bi->bmiHeader,
1276 CBM_INIT, (char*)(bi + 1) + nc * sizeof(RGBQUAD),
1277 bi, DIB_RGB_COLORS)))
1278 ME_RTFInsertOleObject(info, NULL, hbmp, &sz);
1279 ReleaseDC(0, hdc);
1281 break;
1282 default:
1283 break;
1285 HeapFree(GetProcessHeap(), 0, buffer);
1286 RTFRouteToken (info); /* feed "}" back to router */
1287 return;
1288 skip_group:
1289 HeapFree(GetProcessHeap(), 0, buffer);
1290 RTFSkipGroup(info);
1291 RTFRouteToken(info); /* feed "}" back to router */
1294 /* for now, lookup the \result part and use it, whatever the object */
1295 static void ME_RTFReadObjectGroup(RTF_Info *info)
1297 for (;;)
1299 RTFGetToken (info);
1300 if (info->rtfClass == rtfEOF)
1301 return;
1302 if (RTFCheckCM(info, rtfGroup, rtfEndGroup))
1303 break;
1304 if (RTFCheckCM(info, rtfGroup, rtfBeginGroup))
1306 RTFGetToken (info);
1307 if (info->rtfClass == rtfEOF)
1308 return;
1309 if (RTFCheckCMM(info, rtfControl, rtfDestination, rtfObjResult))
1311 int level = 1;
1313 while (RTFGetToken (info) != rtfEOF)
1315 if (info->rtfClass == rtfGroup)
1317 if (info->rtfMajor == rtfBeginGroup) level++;
1318 else if (info->rtfMajor == rtfEndGroup && --level < 0) break;
1320 RTFRouteToken(info);
1323 else RTFSkipGroup(info);
1324 continue;
1326 if (!RTFCheckCM (info, rtfControl, rtfObjAttr))
1328 FIXME("Non supported attribute: %d %d %d\n", info->rtfClass, info->rtfMajor, info->rtfMinor);
1329 return;
1332 RTFRouteToken(info); /* feed "}" back to router */
1335 static void ME_RTFReadHook(RTF_Info *info)
1337 switch(info->rtfClass)
1339 case rtfGroup:
1340 switch(info->rtfMajor)
1342 case rtfBeginGroup:
1343 if (info->stackTop < maxStack) {
1344 info->stack[info->stackTop].fmt = info->style->fmt;
1345 info->stack[info->stackTop].codePage = info->codePage;
1346 info->stack[info->stackTop].unicodeLength = info->unicodeLength;
1348 info->stackTop++;
1349 info->styleChanged = FALSE;
1350 break;
1351 case rtfEndGroup:
1353 ME_Style *s;
1354 RTFFlushOutputBuffer(info);
1355 info->stackTop--;
1356 if (info->stackTop<=0) {
1357 info->rtfClass = rtfEOF;
1358 return;
1360 assert(info->stackTop >= 0);
1361 if (info->styleChanged)
1363 /* FIXME too slow ? how come ? */
1364 s = ME_ApplyStyle(info->style, &info->stack[info->stackTop].fmt);
1365 ME_ReleaseStyle(info->style);
1366 info->style = s;
1367 info->codePage = info->stack[info->stackTop].codePage;
1368 info->unicodeLength = info->stack[info->stackTop].unicodeLength;
1370 break;
1373 break;
1377 void
1378 ME_StreamInFill(ME_InStream *stream)
1380 stream->editstream->dwError = stream->editstream->pfnCallback(stream->editstream->dwCookie,
1381 (BYTE *)stream->buffer,
1382 sizeof(stream->buffer),
1383 (LONG *)&stream->dwSize);
1384 stream->dwUsed = 0;
1387 static LRESULT ME_StreamIn(ME_TextEditor *editor, DWORD format, EDITSTREAM *stream, BOOL stripLastCR)
1389 RTF_Info parser;
1390 ME_Style *style;
1391 int from, to, to2, nUndoMode;
1392 int nEventMask = editor->nEventMask;
1393 ME_InStream inStream;
1394 BOOL invalidRTF = FALSE;
1396 TRACE("stream==%p hWnd==%p format==0x%X\n", stream, editor->hWnd, format);
1397 editor->nEventMask = 0;
1399 ME_GetSelection(editor, &from, &to);
1400 if ((format & SFF_SELECTION) && (editor->mode & TM_RICHTEXT)) {
1401 style = ME_GetSelectionInsertStyle(editor);
1403 ME_InternalDeleteText(editor, from, to-from, FALSE);
1405 /* Don't insert text at the end of the table row */
1406 if (!editor->bEmulateVersion10) { /* v4.1 */
1407 ME_DisplayItem *para = ME_GetParagraph(editor->pCursors->pRun);
1408 if (para->member.para.nFlags & MEPF_ROWEND)
1410 para = para->member.para.next_para;
1411 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
1412 editor->pCursors[0].nOffset = 0;
1414 if (para->member.para.nFlags & MEPF_ROWSTART)
1416 para = para->member.para.next_para;
1417 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
1418 editor->pCursors[0].nOffset = 0;
1420 editor->pCursors[1] = editor->pCursors[0];
1421 } else { /* v1.0 - 3.0 */
1422 if (editor->pCursors[0].pRun->member.run.nFlags & MERF_ENDPARA &&
1423 ME_IsInTable(editor->pCursors[0].pRun))
1424 return 0;
1427 else {
1428 ME_DisplayItem *para_item;
1429 style = editor->pBuffer->pDefaultStyle;
1430 ME_AddRefStyle(style);
1431 ME_SetSelection(editor, 0, 0);
1432 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor), FALSE);
1433 from = to = 0;
1434 ME_ClearTempStyle(editor);
1436 para_item = ME_GetParagraph(editor->pCursors[0].pRun);
1437 ME_SetDefaultParaFormat(para_item->member.para.pFmt);
1441 /* Back up undo mode to a local variable */
1442 nUndoMode = editor->nUndoMode;
1444 /* Only create an undo if SFF_SELECTION is set */
1445 if (!(format & SFF_SELECTION))
1446 editor->nUndoMode = umIgnore;
1448 inStream.editstream = stream;
1449 inStream.editstream->dwError = 0;
1450 inStream.dwSize = 0;
1451 inStream.dwUsed = 0;
1453 if (format & SF_RTF)
1455 /* Check if it's really RTF, and if it is not, use plain text */
1456 ME_StreamInFill(&inStream);
1457 if (!inStream.editstream->dwError)
1459 if ((!editor->bEmulateVersion10 && strncmp(inStream.buffer, "{\\rtf", 5) && strncmp(inStream.buffer, "{\\urtf", 6))
1460 || (editor->bEmulateVersion10 && *inStream.buffer != '{'))
1462 invalidRTF = TRUE;
1463 inStream.editstream->dwError = -16;
1468 if (!invalidRTF && !inStream.editstream->dwError)
1470 if (format & SF_RTF) {
1471 /* setup the RTF parser */
1472 memset(&parser, 0, sizeof parser);
1473 RTFSetEditStream(&parser, &inStream);
1474 parser.rtfFormat = format&(SF_TEXT|SF_RTF);
1475 parser.editor = editor;
1476 parser.style = style;
1477 WriterInit(&parser);
1478 RTFInit(&parser);
1479 RTFSetReadHook(&parser, ME_RTFReadHook);
1480 RTFSetDestinationCallback(&parser, rtfPict, ME_RTFReadPictGroup);
1481 RTFSetDestinationCallback(&parser, rtfObject, ME_RTFReadObjectGroup);
1482 if (!parser.editor->bEmulateVersion10) /* v4.1 */
1484 RTFSetDestinationCallback(&parser, rtfNoNestTables, RTFSkipGroup);
1485 RTFSetDestinationCallback(&parser, rtfNestTableProps, RTFReadGroup);
1487 BeginFile(&parser);
1489 /* do the parsing */
1490 RTFRead(&parser);
1491 RTFFlushOutputBuffer(&parser);
1492 if (!editor->bEmulateVersion10) { /* v4.1 */
1493 if (parser.tableDef && parser.tableDef->tableRowStart &&
1494 (parser.nestingLevel > 0 || parser.canInheritInTbl))
1496 /* Delete any incomplete table row at the end of the rich text. */
1497 int nOfs, nChars;
1498 ME_DisplayItem *pCell;
1499 ME_DisplayItem *para;
1501 parser.rtfMinor = rtfRow;
1502 /* Complete the table row before deleting it.
1503 * By doing it this way we will have the current paragraph format set
1504 * properly to reflect that is not in the complete table, and undo items
1505 * will be added for this change to the current paragraph format. */
1506 if (parser.nestingLevel > 0)
1508 while (parser.nestingLevel > 1)
1509 ME_RTFSpecialCharHook(&parser); /* Decrements nestingLevel */
1510 para = parser.tableDef->tableRowStart;
1511 ME_RTFSpecialCharHook(&parser);
1512 } else {
1513 para = parser.tableDef->tableRowStart;
1514 ME_RTFSpecialCharHook(&parser);
1515 assert(para->member.para.nFlags & MEPF_ROWEND);
1516 para = para->member.para.next_para;
1518 pCell = para->member.para.pCell;
1520 editor->pCursors[1].pRun = ME_FindItemFwd(para, diRun);
1521 editor->pCursors[1].nOffset = 0;
1522 nOfs = ME_GetCursorOfs(editor, 1);
1523 nChars = ME_GetCursorOfs(editor, 0) - nOfs;
1524 ME_InternalDeleteText(editor, nOfs, nChars, TRUE);
1525 if (parser.tableDef)
1526 parser.tableDef->tableRowStart = NULL;
1529 ME_CheckTablesForCorruption(editor);
1530 RTFDestroy(&parser);
1531 if (parser.lpRichEditOle)
1532 IRichEditOle_Release(parser.lpRichEditOle);
1534 if (!inStream.editstream->dwError && parser.stackTop > 0)
1535 inStream.editstream->dwError = HRESULT_FROM_WIN32(ERROR_HANDLE_EOF);
1537 /* Remove last line break, as mandated by tests. This is not affected by
1538 CR/LF counters, since RTF streaming presents only \para tokens, which
1539 are converted according to the standard rules: \r for 2.0, \r\n for 1.0
1541 if (stripLastCR) {
1542 int newfrom, newto;
1543 ME_GetSelection(editor, &newfrom, &newto);
1544 if (newto > to + (editor->bEmulateVersion10 ? 1 : 0)) {
1545 WCHAR lastchar[3] = {'\0', '\0'};
1546 int linebreakSize = editor->bEmulateVersion10 ? 2 : 1;
1548 ME_GetTextW(editor, lastchar, newto - linebreakSize, linebreakSize, 0);
1549 if (lastchar[0] == '\r' && (lastchar[1] == '\n' || lastchar[1] == '\0')) {
1550 ME_InternalDeleteText(editor, newto - linebreakSize, linebreakSize, FALSE);
1555 style = parser.style;
1557 else if (format & SF_TEXT)
1558 ME_StreamInText(editor, format, &inStream, style);
1559 else
1560 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
1561 ME_GetSelection(editor, &to, &to2);
1562 /* put the cursor at the top */
1563 if (!(format & SFF_SELECTION))
1564 ME_SetSelection(editor, 0, 0);
1567 /* Restore saved undo mode */
1568 editor->nUndoMode = nUndoMode;
1570 /* even if we didn't add an undo, we need to commit anything on the stack */
1571 ME_CommitUndo(editor);
1573 /* If SFF_SELECTION isn't set, delete any undos from before we started too */
1574 if (!(format & SFF_SELECTION))
1575 ME_EmptyUndoStack(editor);
1577 ME_ReleaseStyle(style);
1578 editor->nEventMask = nEventMask;
1579 ME_UpdateRepaint(editor);
1580 if (!(format & SFF_SELECTION)) {
1581 ME_ClearTempStyle(editor);
1583 HideCaret(editor->hWnd);
1584 ME_MoveCaret(editor);
1585 ShowCaret(editor->hWnd);
1586 ME_SendSelChange(editor);
1587 ME_SendRequestResize(editor, FALSE);
1589 return 0;
1593 typedef struct tagME_RTFStringStreamStruct
1595 char *string;
1596 int pos;
1597 int length;
1598 } ME_RTFStringStreamStruct;
1600 static DWORD CALLBACK ME_ReadFromRTFString(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1602 ME_RTFStringStreamStruct *pStruct = (ME_RTFStringStreamStruct *)dwCookie;
1603 int count;
1605 count = min(cb, pStruct->length - pStruct->pos);
1606 memmove(lpBuff, pStruct->string + pStruct->pos, count);
1607 pStruct->pos += count;
1608 *pcb = count;
1609 return 0;
1612 static void
1613 ME_StreamInRTFString(ME_TextEditor *editor, BOOL selection, char *string)
1615 EDITSTREAM es;
1616 ME_RTFStringStreamStruct data;
1618 data.string = string;
1619 data.length = strlen(string);
1620 data.pos = 0;
1621 es.dwCookie = (DWORD_PTR)&data;
1622 es.pfnCallback = ME_ReadFromRTFString;
1623 ME_StreamIn(editor, SF_RTF | (selection ? SFF_SELECTION : 0), &es, FALSE);
1627 ME_DisplayItem *
1628 ME_FindItemAtOffset(ME_TextEditor *editor, ME_DIType nItemType, int nOffset, int *nItemOffset)
1630 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
1631 int runLength;
1633 while (item && item->member.para.next_para->member.para.nCharOfs <= nOffset)
1634 item = ME_FindItemFwd(item, diParagraph);
1636 if (!item)
1637 return item;
1639 nOffset -= item->member.para.nCharOfs;
1640 if (nItemType == diParagraph) {
1641 if (nItemOffset)
1642 *nItemOffset = nOffset;
1643 return item;
1646 do {
1647 item = ME_FindItemFwd(item, diRun);
1648 runLength = ME_StrLen(item->member.run.strText);
1649 if (item->member.run.nFlags & MERF_ENDPARA)
1650 runLength = item->member.run.nCR + item->member.run.nLF;
1651 } while (item && (item->member.run.nCharOfs + runLength <= nOffset));
1652 if (item) {
1653 nOffset -= item->member.run.nCharOfs;
1655 /* Special case: nOffset may not point exactly at the division between the
1656 \r and the \n in 1.0 emulation. If such a case happens, it is sent
1657 into the next run, if one exists
1659 if ( item->member.run.nFlags & MERF_ENDPARA
1660 && nOffset == item->member.run.nCR
1661 && item->member.run.nLF > 0) {
1662 ME_DisplayItem *nextItem;
1663 nextItem = ME_FindItemFwd(item, diRun);
1664 if (nextItem) {
1665 nOffset = 0;
1666 item = nextItem;
1669 if (nItemOffset)
1670 *nItemOffset = nOffset;
1672 return item;
1676 static int
1677 ME_FindText(ME_TextEditor *editor, DWORD flags, const CHARRANGE *chrg, const WCHAR *text, CHARRANGE *chrgText)
1679 const int nLen = lstrlenW(text);
1680 const int nTextLen = ME_GetTextLength(editor);
1681 int nStart, nEnd;
1682 int nMin, nMax;
1683 ME_DisplayItem *item;
1684 ME_DisplayItem *para;
1685 WCHAR wLastChar = ' ';
1687 TRACE("flags==0x%08x, chrg->cpMin==%d, chrg->cpMax==%d text==%s\n",
1688 flags, chrg->cpMin, chrg->cpMax, debugstr_w(text));
1690 if (flags & ~(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD))
1691 FIXME("Flags 0x%08x not implemented\n",
1692 flags & ~(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD));
1694 nMin = chrg->cpMin;
1695 if (chrg->cpMax == -1)
1696 nMax = nTextLen;
1697 else
1698 nMax = chrg->cpMax > nTextLen ? nTextLen : chrg->cpMax;
1700 /* In 1.0 emulation, if cpMax reaches end of text, add the FR_DOWN flag */
1701 if (editor->bEmulateVersion10 && nMax == nTextLen)
1703 flags |= FR_DOWN;
1706 /* In 1.0 emulation, cpMin must always be no greater than cpMax */
1707 if (editor->bEmulateVersion10 && nMax < nMin)
1709 if (chrgText)
1711 chrgText->cpMin = -1;
1712 chrgText->cpMax = -1;
1714 return -1;
1717 /* when searching up, if cpMin < cpMax, then instead of searching
1718 * on [cpMin,cpMax], we search on [0,cpMin], otherwise, search on
1719 * [cpMax, cpMin]. The exception is when cpMax is -1, in which
1720 * case, it is always bigger than cpMin.
1722 if (!editor->bEmulateVersion10 && !(flags & FR_DOWN))
1724 int nSwap = nMax;
1726 nMax = nMin > nTextLen ? nTextLen : nMin;
1727 if (nMin < nSwap || chrg->cpMax == -1)
1728 nMin = 0;
1729 else
1730 nMin = nSwap;
1733 if (!nLen || nMin < 0 || nMax < 0 || nMax < nMin)
1735 if (chrgText)
1736 chrgText->cpMin = chrgText->cpMax = -1;
1737 return -1;
1740 if (flags & FR_DOWN) /* Forward search */
1742 /* If possible, find the character before where the search starts */
1743 if ((flags & FR_WHOLEWORD) && nMin)
1745 nStart = nMin - 1;
1746 item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
1747 if (!item)
1749 if (chrgText)
1750 chrgText->cpMin = chrgText->cpMax = -1;
1751 return -1;
1753 wLastChar = item->member.run.strText->szData[nStart];
1756 nStart = nMin;
1757 item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
1758 if (!item)
1760 if (chrgText)
1761 chrgText->cpMin = chrgText->cpMax = -1;
1762 return -1;
1765 para = ME_GetParagraph(item);
1766 while (item
1767 && para->member.para.nCharOfs + item->member.run.nCharOfs + nStart + nLen <= nMax)
1769 ME_DisplayItem *pCurItem = item;
1770 int nCurStart = nStart;
1771 int nMatched = 0;
1773 while (pCurItem && ME_CharCompare(pCurItem->member.run.strText->szData[nCurStart + nMatched], text[nMatched], (flags & FR_MATCHCASE)))
1775 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
1776 break;
1778 nMatched++;
1779 if (nMatched == nLen)
1781 ME_DisplayItem *pNextItem = pCurItem;
1782 int nNextStart = nCurStart;
1783 WCHAR wNextChar;
1785 /* Check to see if next character is a whitespace */
1786 if (flags & FR_WHOLEWORD)
1788 if (nCurStart + nMatched == ME_StrLen(pCurItem->member.run.strText))
1790 pNextItem = ME_FindItemFwd(pCurItem, diRun);
1791 nNextStart = -nMatched;
1794 if (pNextItem)
1795 wNextChar = pNextItem->member.run.strText->szData[nNextStart + nMatched];
1796 else
1797 wNextChar = ' ';
1799 if (isalnumW(wNextChar))
1800 break;
1803 nStart += para->member.para.nCharOfs + item->member.run.nCharOfs;
1804 if (chrgText)
1806 chrgText->cpMin = nStart;
1807 chrgText->cpMax = nStart + nLen;
1809 TRACE("found at %d-%d\n", nStart, nStart + nLen);
1810 return nStart;
1812 if (nCurStart + nMatched == ME_StrLen(pCurItem->member.run.strText))
1814 pCurItem = ME_FindItemFwd(pCurItem, diRun);
1815 para = ME_GetParagraph(pCurItem);
1816 nCurStart = -nMatched;
1819 if (pCurItem)
1820 wLastChar = pCurItem->member.run.strText->szData[nCurStart + nMatched];
1821 else
1822 wLastChar = ' ';
1824 nStart++;
1825 if (nStart == ME_StrLen(item->member.run.strText))
1827 item = ME_FindItemFwd(item, diRun);
1828 para = ME_GetParagraph(item);
1829 nStart = 0;
1833 else /* Backward search */
1835 /* If possible, find the character after where the search ends */
1836 if ((flags & FR_WHOLEWORD) && nMax < nTextLen - 1)
1838 nEnd = nMax + 1;
1839 item = ME_FindItemAtOffset(editor, diRun, nEnd, &nEnd);
1840 if (!item)
1842 if (chrgText)
1843 chrgText->cpMin = chrgText->cpMax = -1;
1844 return -1;
1846 wLastChar = item->member.run.strText->szData[nEnd];
1849 nEnd = nMax;
1850 item = ME_FindItemAtOffset(editor, diRun, nEnd, &nEnd);
1851 if (!item)
1853 if (chrgText)
1854 chrgText->cpMin = chrgText->cpMax = -1;
1855 return -1;
1858 para = ME_GetParagraph(item);
1860 while (item
1861 && para->member.para.nCharOfs + item->member.run.nCharOfs + nEnd - nLen >= nMin)
1863 ME_DisplayItem *pCurItem = item;
1864 int nCurEnd = nEnd;
1865 int nMatched = 0;
1867 if (nCurEnd - nMatched == 0)
1869 pCurItem = ME_FindItemBack(pCurItem, diRun);
1870 para = ME_GetParagraph(pCurItem);
1871 nCurEnd = ME_StrLen(pCurItem->member.run.strText) + nMatched;
1874 while (pCurItem && ME_CharCompare(pCurItem->member.run.strText->szData[nCurEnd - nMatched - 1], text[nLen - nMatched - 1], (flags & FR_MATCHCASE)))
1876 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
1877 break;
1879 nMatched++;
1880 if (nMatched == nLen)
1882 ME_DisplayItem *pPrevItem = pCurItem;
1883 int nPrevEnd = nCurEnd;
1884 WCHAR wPrevChar;
1886 /* Check to see if previous character is a whitespace */
1887 if (flags & FR_WHOLEWORD)
1889 if (nPrevEnd - nMatched == 0)
1891 pPrevItem = ME_FindItemBack(pCurItem, diRun);
1892 if (pPrevItem)
1893 nPrevEnd = ME_StrLen(pPrevItem->member.run.strText) + nMatched;
1896 if (pPrevItem)
1897 wPrevChar = pPrevItem->member.run.strText->szData[nPrevEnd - nMatched - 1];
1898 else
1899 wPrevChar = ' ';
1901 if (isalnumW(wPrevChar))
1902 break;
1905 nStart = para->member.para.nCharOfs + pCurItem->member.run.nCharOfs + nCurEnd - nMatched;
1906 if (chrgText)
1908 chrgText->cpMin = nStart;
1909 chrgText->cpMax = nStart + nLen;
1911 TRACE("found at %d-%d\n", nStart, nStart + nLen);
1912 return nStart;
1914 if (nCurEnd - nMatched == 0)
1916 pCurItem = ME_FindItemBack(pCurItem, diRun);
1917 /* Don't care about pCurItem becoming NULL here; it's already taken
1918 * care of in the exterior loop condition */
1919 para = ME_GetParagraph(pCurItem);
1920 nCurEnd = ME_StrLen(pCurItem->member.run.strText) + nMatched;
1923 if (pCurItem)
1924 wLastChar = pCurItem->member.run.strText->szData[nCurEnd - nMatched - 1];
1925 else
1926 wLastChar = ' ';
1928 nEnd--;
1929 if (nEnd < 0)
1931 item = ME_FindItemBack(item, diRun);
1932 para = ME_GetParagraph(item);
1933 nEnd = ME_StrLen(item->member.run.strText);
1937 TRACE("not found\n");
1938 if (chrgText)
1939 chrgText->cpMin = chrgText->cpMax = -1;
1940 return -1;
1943 static int ME_GetTextEx(ME_TextEditor *editor, GETTEXTEX *ex, LPARAM pText)
1945 int nStart, nCount; /* in chars */
1947 if (ex->flags & ~(GT_SELECTION | GT_USECRLF))
1948 FIXME("GETTEXTEX flags 0x%08x not supported\n", ex->flags & ~(GT_SELECTION | GT_USECRLF));
1950 if (ex->flags & GT_SELECTION)
1952 ME_GetSelection(editor, &nStart, &nCount);
1953 nCount -= nStart;
1955 else
1957 nStart = 0;
1958 nCount = 0x7fffffff;
1960 if (ex->codepage == 1200)
1962 nCount = min(nCount, ex->cb / sizeof(WCHAR) - 1);
1963 return ME_GetTextW(editor, (LPWSTR)pText, nStart, nCount, ex->flags & GT_USECRLF);
1965 else
1967 /* potentially each char may be a CR, why calculate the exact value with O(N) when
1968 we can just take a bigger buffer? :)
1969 The above assumption still holds with CR/LF counters, since CR->CRLF expansion
1970 occurs only in richedit 2.0 mode, in which line breaks have only one CR
1972 int crlfmul = (ex->flags & GT_USECRLF) ? 2 : 1;
1973 LPWSTR buffer;
1974 DWORD buflen = ex->cb;
1975 LRESULT rc;
1976 DWORD flags = 0;
1978 nCount = min(nCount, ex->cb - 1);
1979 buffer = heap_alloc((crlfmul*nCount + 1) * sizeof(WCHAR));
1981 buflen = ME_GetTextW(editor, buffer, nStart, nCount, ex->flags & GT_USECRLF);
1982 rc = WideCharToMultiByte(ex->codepage, flags, buffer, buflen+1,
1983 (LPSTR)pText, ex->cb, ex->lpDefaultChar, ex->lpUsedDefChar);
1984 if (rc) rc--; /* do not count 0 terminator */
1986 heap_free(buffer);
1987 return rc;
1991 static int ME_GetTextRange(ME_TextEditor *editor, TEXTRANGEW *rng, BOOL unicode)
1993 if (unicode)
1994 return ME_GetTextW(editor, rng->lpstrText, rng->chrg.cpMin,
1995 rng->chrg.cpMax-rng->chrg.cpMin, 0);
1996 else
1998 int nLen = rng->chrg.cpMax-rng->chrg.cpMin;
1999 WCHAR *p = ALLOC_N_OBJ(WCHAR, nLen+1);
2000 int nChars = ME_GetTextW(editor, p, rng->chrg.cpMin, nLen, 0);
2001 /* FIXME this is a potential security hole (buffer overrun)
2002 if you know more about wchar->mbyte conversion please explain
2004 WideCharToMultiByte(CP_ACP, 0, p, nChars+1, (char *)rng->lpstrText, nLen+1, NULL, NULL);
2005 FREE_OBJ(p);
2006 return nChars;
2010 typedef struct tagME_GlobalDestStruct
2012 HGLOBAL hData;
2013 int nLength;
2014 } ME_GlobalDestStruct;
2016 static DWORD CALLBACK ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
2018 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
2019 int i;
2020 WORD *pSrc, *pDest;
2022 cb = cb >> 1;
2023 pDest = (WORD *)lpBuff;
2024 pSrc = GlobalLock(pData->hData);
2025 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
2026 pDest[i] = pSrc[pData->nLength+i];
2028 pData->nLength += i;
2029 *pcb = 2*i;
2030 GlobalUnlock(pData->hData);
2031 return 0;
2034 static DWORD CALLBACK ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
2036 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
2037 int i;
2038 BYTE *pSrc, *pDest;
2040 pDest = lpBuff;
2041 pSrc = GlobalLock(pData->hData);
2042 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
2043 pDest[i] = pSrc[pData->nLength+i];
2045 pData->nLength += i;
2046 *pcb = i;
2047 GlobalUnlock(pData->hData);
2048 return 0;
2051 static BOOL ME_Paste(ME_TextEditor *editor)
2053 DWORD dwFormat = 0;
2054 EDITSTREAM es;
2055 ME_GlobalDestStruct gds;
2056 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
2057 UINT cf = 0;
2059 if (IsClipboardFormatAvailable(nRTFFormat))
2060 cf = nRTFFormat, dwFormat = SF_RTF;
2061 else if (IsClipboardFormatAvailable(CF_UNICODETEXT))
2062 cf = CF_UNICODETEXT, dwFormat = SF_TEXT|SF_UNICODE;
2063 else
2064 return FALSE;
2066 if (!OpenClipboard(editor->hWnd))
2067 return FALSE;
2068 gds.hData = GetClipboardData(cf);
2069 gds.nLength = 0;
2070 es.dwCookie = (DWORD_PTR)&gds;
2071 es.pfnCallback = dwFormat == SF_RTF ? ME_ReadFromHGLOBALRTF : ME_ReadFromHGLOBALUnicode;
2072 ME_StreamIn(editor, dwFormat|SFF_SELECTION, &es, FALSE);
2074 CloseClipboard();
2075 return TRUE;
2078 static BOOL ME_Copy(ME_TextEditor *editor, CHARRANGE *range)
2080 LPDATAOBJECT dataObj = NULL;
2081 HRESULT hr = S_OK;
2083 if (editor->cPasswordMask)
2084 return FALSE; /* Copying or Cutting masked text isn't allowed */
2086 if(editor->lpOleCallback)
2087 hr = IRichEditOleCallback_GetClipboardData(editor->lpOleCallback, range, RECO_COPY, &dataObj);
2088 if(FAILED(hr) || !dataObj)
2089 hr = ME_GetDataObject(editor, range, &dataObj);
2090 if(SUCCEEDED(hr)) {
2091 hr = OleSetClipboard(dataObj);
2092 IDataObject_Release(dataObj);
2094 return SUCCEEDED(hr) != 0;
2097 /* helper to send a msg filter notification */
2098 static BOOL
2099 ME_FilterEvent(ME_TextEditor *editor, UINT msg, WPARAM* wParam, LPARAM* lParam)
2101 MSGFILTER msgf;
2103 msgf.nmhdr.hwndFrom = editor->hWnd;
2104 msgf.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
2105 msgf.nmhdr.code = EN_MSGFILTER;
2106 msgf.msg = msg;
2108 msgf.wParam = *wParam;
2109 msgf.lParam = *lParam;
2110 if (SendMessageW(GetParent(editor->hWnd), WM_NOTIFY, msgf.nmhdr.idFrom, (LPARAM)&msgf))
2111 return FALSE;
2112 *wParam = msgf.wParam;
2113 *lParam = msgf.lParam;
2114 msgf.wParam = *wParam;
2116 return TRUE;
2119 static void ME_UpdateSelectionLinkAttribute(ME_TextEditor *editor)
2121 ME_DisplayItem * startPara, * endPara;
2122 ME_DisplayItem * item;
2123 int dummy;
2124 int from, to;
2126 ME_GetSelection(editor, &from, &to);
2127 if (from > to) from ^= to, to ^=from, from ^= to;
2128 startPara = NULL; endPara = NULL;
2130 /* Find paragraph previous to the one that contains start cursor */
2131 item = ME_FindItemAtOffset(editor, diRun, from, &dummy);
2132 if (item) {
2133 startPara = ME_FindItemBack(item, diParagraph);
2134 item = ME_FindItemBack(startPara, diParagraph);
2135 if (item) startPara = item;
2138 /* Find paragraph that contains end cursor */
2139 item = ME_FindItemAtOffset(editor, diRun, to, &dummy);
2140 if (item) {
2141 endPara = ME_FindItemFwd(item, diParagraph);
2144 if (startPara && endPara) {
2145 ME_UpdateLinkAttribute(editor,
2146 startPara->member.para.nCharOfs,
2147 endPara->member.para.nCharOfs);
2148 } else if (startPara) {
2149 ME_UpdateLinkAttribute(editor,
2150 startPara->member.para.nCharOfs,
2151 -1);
2155 static BOOL
2156 ME_KeyDown(ME_TextEditor *editor, WORD nKey)
2158 BOOL ctrl_is_down = GetKeyState(VK_CONTROL) & 0x8000;
2159 BOOL shift_is_down = GetKeyState(VK_SHIFT) & 0x8000;
2161 if (nKey != VK_SHIFT && nKey != VK_CONTROL && nKey != VK_MENU)
2162 editor->nSelectionType = stPosition;
2164 switch (nKey)
2166 case VK_LEFT:
2167 case VK_RIGHT:
2168 case VK_HOME:
2169 case VK_END:
2170 editor->nUDArrowX = -1;
2171 /* fall through */
2172 case VK_UP:
2173 case VK_DOWN:
2174 case VK_PRIOR:
2175 case VK_NEXT:
2176 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
2177 ME_ArrowKey(editor, nKey, shift_is_down, ctrl_is_down);
2178 return TRUE;
2179 case VK_BACK:
2180 case VK_DELETE:
2181 editor->nUDArrowX = -1;
2182 /* FIXME backspace and delete aren't the same, they act different wrt paragraph style of the merged paragraph */
2183 if (editor->styleFlags & ES_READONLY)
2184 return FALSE;
2185 if (ME_IsSelection(editor))
2187 ME_DeleteSelection(editor);
2188 ME_CommitUndo(editor);
2190 else if (nKey == VK_DELETE)
2192 /* Delete stops group typing.
2193 * (See MSDN remarks on EM_STOPGROUPTYPING message) */
2194 ME_DeleteTextAtCursor(editor, 1, 1);
2195 ME_CommitUndo(editor);
2197 else if (ME_ArrowKey(editor, VK_LEFT, FALSE, FALSE))
2199 BOOL bDeletionSucceeded;
2200 /* Backspace can be grouped for a single undo */
2201 ME_ContinueCoalescingTransaction(editor);
2202 bDeletionSucceeded = ME_DeleteTextAtCursor(editor, 1, 1);
2203 if (!bDeletionSucceeded && !editor->bEmulateVersion10) { /* v4.1 */
2204 /* Deletion was prevented so the cursor is moved back to where it was.
2205 * (e.g. this happens when trying to delete cell boundaries)
2207 ME_ArrowKey(editor, VK_RIGHT, FALSE, FALSE);
2209 ME_CommitCoalescingUndo(editor);
2211 else
2212 return TRUE;
2213 ME_MoveCursorFromTableRowStartParagraph(editor);
2214 ME_UpdateSelectionLinkAttribute(editor);
2215 ME_UpdateRepaint(editor);
2216 ME_SendRequestResize(editor, FALSE);
2217 return TRUE;
2218 case VK_RETURN:
2219 if (editor->styleFlags & ES_MULTILINE)
2221 ME_Cursor cursor = editor->pCursors[0];
2222 ME_DisplayItem *para = ME_GetParagraph(cursor.pRun);
2223 int from, to;
2224 const WCHAR endl = '\r';
2225 ME_Style *style;
2227 if (editor->styleFlags & ES_READONLY) {
2228 MessageBeep(MB_ICONERROR);
2229 return TRUE;
2232 ME_GetSelection(editor, &from, &to);
2233 if (editor->nTextLimit > ME_GetTextLength(editor) - (to-from))
2235 if (!editor->bEmulateVersion10) { /* v4.1 */
2236 if (para->member.para.nFlags & MEPF_ROWEND) {
2237 /* Add a new table row after this row. */
2238 para = ME_AppendTableRow(editor, para);
2239 para = para->member.para.next_para;
2240 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2241 editor->pCursors[0].nOffset = 0;
2242 editor->pCursors[1] = editor->pCursors[0];
2243 ME_CommitUndo(editor);
2244 ME_CheckTablesForCorruption(editor);
2245 ME_UpdateRepaint(editor);
2246 return TRUE;
2248 else if (para == ME_GetParagraph(editor->pCursors[1].pRun) &&
2249 cursor.nOffset + cursor.pRun->member.run.nCharOfs == 0 &&
2250 para->member.para.prev_para->member.para.nFlags & MEPF_ROWSTART &&
2251 !para->member.para.prev_para->member.para.nCharOfs)
2253 /* Insert a newline before the table. */
2254 para = para->member.para.prev_para;
2255 para->member.para.nFlags &= ~MEPF_ROWSTART;
2256 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2257 editor->pCursors[1] = editor->pCursors[0];
2258 ME_InsertTextFromCursor(editor, 0, &endl, 1,
2259 editor->pCursors[0].pRun->member.run.style);
2260 para = editor->pBuffer->pFirst->member.para.next_para;
2261 ME_SetDefaultParaFormat(para->member.para.pFmt);
2262 para->member.para.nFlags = MEPF_REWRAP;
2263 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2264 editor->pCursors[1] = editor->pCursors[0];
2265 para->member.para.next_para->member.para.nFlags |= MEPF_ROWSTART;
2266 ME_CommitCoalescingUndo(editor);
2267 ME_CheckTablesForCorruption(editor);
2268 ME_UpdateRepaint(editor);
2269 return TRUE;
2271 } else { /* v1.0 - 3.0 */
2272 ME_DisplayItem *para = ME_GetParagraph(cursor.pRun);
2273 if (ME_IsInTable(para))
2275 if (cursor.pRun->member.run.nFlags & MERF_ENDPARA)
2277 if (from == to) {
2278 ME_ContinueCoalescingTransaction(editor);
2279 para = ME_AppendTableRow(editor, para);
2280 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2281 editor->pCursors[0].nOffset = 0;
2282 editor->pCursors[1] = editor->pCursors[0];
2283 ME_CommitCoalescingUndo(editor);
2284 ME_UpdateRepaint(editor);
2285 return TRUE;
2287 } else {
2288 ME_ContinueCoalescingTransaction(editor);
2289 if (cursor.pRun->member.run.nCharOfs + cursor.nOffset == 0 &&
2290 !ME_IsInTable(para->member.para.prev_para))
2292 /* Insert newline before table */
2293 cursor.pRun = ME_FindItemBack(para, diRun);
2294 if (cursor.pRun)
2295 editor->pCursors[0].pRun = cursor.pRun;
2296 editor->pCursors[0].nOffset = 0;
2297 editor->pCursors[1] = editor->pCursors[0];
2298 ME_InsertTextFromCursor(editor, 0, &endl, 1,
2299 editor->pCursors[0].pRun->member.run.style);
2300 } else {
2301 editor->pCursors[1] = editor->pCursors[0];
2302 para = ME_AppendTableRow(editor, para);
2303 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2304 editor->pCursors[0].nOffset = 0;
2305 editor->pCursors[1] = editor->pCursors[0];
2307 ME_CommitCoalescingUndo(editor);
2308 ME_UpdateRepaint(editor);
2309 return TRUE;
2314 style = ME_GetInsertStyle(editor, 0);
2315 ME_SaveTempStyle(editor);
2316 ME_ContinueCoalescingTransaction(editor);
2317 if (shift_is_down)
2318 ME_InsertEndRowFromCursor(editor, 0);
2319 else
2320 ME_InsertTextFromCursor(editor, 0, &endl, 1, style);
2321 ME_ReleaseStyle(style);
2322 ME_CommitCoalescingUndo(editor);
2323 SetCursor(NULL);
2325 if (editor->AutoURLDetect_bEnable)
2326 ME_UpdateSelectionLinkAttribute(editor);
2328 ME_UpdateRepaint(editor);
2330 return TRUE;
2332 break;
2333 case 'A':
2334 if (ctrl_is_down)
2336 ME_SetSelection(editor, 0, -1);
2337 return TRUE;
2339 break;
2340 case 'V':
2341 if (ctrl_is_down)
2342 return ME_Paste(editor);
2343 break;
2344 case 'C':
2345 case 'X':
2346 if (ctrl_is_down)
2348 CHARRANGE range;
2349 BOOL result;
2351 ME_GetSelection(editor, &range.cpMin, &range.cpMax);
2352 result = ME_Copy(editor, &range);
2353 if (result && nKey == 'X')
2355 ME_InternalDeleteText(editor, range.cpMin, range.cpMax-range.cpMin, FALSE);
2356 ME_CommitUndo(editor);
2357 ME_UpdateRepaint(editor);
2359 return result;
2361 break;
2362 case 'Z':
2363 if (ctrl_is_down)
2365 ME_Undo(editor);
2366 return TRUE;
2368 break;
2369 case 'Y':
2370 if (ctrl_is_down)
2372 ME_Redo(editor);
2373 return TRUE;
2375 break;
2377 default:
2378 if (nKey != VK_SHIFT && nKey != VK_CONTROL && nKey && nKey != VK_MENU)
2379 editor->nUDArrowX = -1;
2380 if (ctrl_is_down)
2382 if (nKey == 'W')
2384 CHARFORMAT2W chf;
2385 char buf[2048];
2386 chf.cbSize = sizeof(chf);
2388 ME_GetSelectionCharFormat(editor, &chf);
2389 ME_DumpStyleToBuf(&chf, buf);
2390 MessageBoxA(NULL, buf, "Style dump", MB_OK);
2392 if (nKey == 'Q')
2394 ME_CheckCharOffsets(editor);
2398 return FALSE;
2401 static LRESULT ME_Char(ME_TextEditor *editor, WPARAM charCode,
2402 LPARAM flags, BOOL unicode)
2404 WCHAR wstr;
2406 if (unicode)
2407 wstr = (WCHAR)charCode;
2408 else
2410 CHAR charA = charCode;
2411 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &wstr, 1);
2414 if (editor->styleFlags & ES_READONLY) {
2415 MessageBeep(MB_ICONERROR);
2416 return 0; /* FIXME really 0 ? */
2419 if ((unsigned)wstr >= ' ' || wstr == '\t')
2421 ME_Cursor cursor = editor->pCursors[0];
2422 ME_DisplayItem *para = ME_GetParagraph(cursor.pRun);
2423 int from, to;
2424 BOOL ctrl_is_down = GetKeyState(VK_CONTROL) & 0x8000;
2425 ME_GetSelection(editor, &from, &to);
2426 if (wstr == '\t' &&
2427 /* v4.1 allows tabs to be inserted with ctrl key down */
2428 !(ctrl_is_down && !editor->bEmulateVersion10))
2430 ME_DisplayItem *para;
2431 BOOL bSelectedRow = FALSE;
2433 para = ME_GetParagraph(cursor.pRun);
2434 if (ME_IsSelection(editor) &&
2435 cursor.pRun->member.run.nCharOfs + cursor.nOffset == 0 &&
2436 to == ME_GetCursorOfs(editor, 0) &&
2437 para->member.para.prev_para->type == diParagraph)
2439 para = para->member.para.prev_para;
2440 bSelectedRow = TRUE;
2442 if (ME_IsInTable(para))
2444 ME_TabPressedInTable(editor, bSelectedRow);
2445 ME_CommitUndo(editor);
2446 return 0;
2448 } else if (!editor->bEmulateVersion10) { /* v4.1 */
2449 if (para->member.para.nFlags & MEPF_ROWEND) {
2450 if (from == to) {
2451 para = para->member.para.next_para;
2452 if (para->member.para.nFlags & MEPF_ROWSTART)
2453 para = para->member.para.next_para;
2454 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2455 editor->pCursors[0].nOffset = 0;
2456 editor->pCursors[1] = editor->pCursors[0];
2459 } else { /* v1.0 - 3.0 */
2460 if (ME_IsInTable(cursor.pRun) &&
2461 cursor.pRun->member.run.nFlags & MERF_ENDPARA &&
2462 from == to)
2464 /* Text should not be inserted at the end of the table. */
2465 MessageBeep(-1);
2466 return 0;
2469 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
2470 /* WM_CHAR is restricted to nTextLimit */
2471 if(editor->nTextLimit > ME_GetTextLength(editor) - (to-from))
2473 ME_Style *style = ME_GetInsertStyle(editor, 0);
2474 ME_SaveTempStyle(editor);
2475 ME_ContinueCoalescingTransaction(editor);
2476 ME_InsertTextFromCursor(editor, 0, &wstr, 1, style);
2477 ME_ReleaseStyle(style);
2478 ME_CommitCoalescingUndo(editor);
2479 SetCursor(NULL);
2482 if (editor->AutoURLDetect_bEnable) ME_UpdateSelectionLinkAttribute(editor);
2484 ME_UpdateRepaint(editor);
2486 return 0;
2489 /* Process the message and calculate the new click count.
2491 * returns: The click count if it is mouse down event, else returns 0. */
2492 static int ME_CalculateClickCount(ME_TextEditor *editor, UINT msg, WPARAM wParam,
2493 LPARAM lParam)
2495 static int clickNum = 0;
2496 if (msg < WM_MOUSEFIRST || msg > WM_MOUSELAST)
2497 return 0;
2499 if ((msg == WM_LBUTTONDBLCLK) ||
2500 (msg == WM_RBUTTONDBLCLK) ||
2501 (msg == WM_MBUTTONDBLCLK) ||
2502 (msg == WM_XBUTTONDBLCLK))
2504 msg -= (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN);
2507 if ((msg == WM_LBUTTONDOWN) ||
2508 (msg == WM_RBUTTONDOWN) ||
2509 (msg == WM_MBUTTONDOWN) ||
2510 (msg == WM_XBUTTONDOWN))
2512 static MSG prevClickMsg;
2513 MSG clickMsg;
2514 /* Compare the editor instead of the hwnd so that the this
2515 * can still be done for windowless richedit controls. */
2516 clickMsg.hwnd = (HWND)editor;
2517 clickMsg.message = msg;
2518 clickMsg.wParam = wParam;
2519 clickMsg.lParam = lParam;
2520 clickMsg.time = GetMessageTime();
2521 clickMsg.pt.x = (short)LOWORD(lParam);
2522 clickMsg.pt.y = (short)HIWORD(lParam);
2523 if ((clickNum != 0) &&
2524 (clickMsg.message == prevClickMsg.message) &&
2525 (clickMsg.hwnd == prevClickMsg.hwnd) &&
2526 (clickMsg.wParam == prevClickMsg.wParam) &&
2527 (clickMsg.time - prevClickMsg.time < GetDoubleClickTime()) &&
2528 (abs(clickMsg.pt.x - prevClickMsg.pt.x) < GetSystemMetrics(SM_CXDOUBLECLK)/2) &&
2529 (abs(clickMsg.pt.y - prevClickMsg.pt.y) < GetSystemMetrics(SM_CYDOUBLECLK)/2))
2531 clickNum++;
2532 } else {
2533 clickNum = 1;
2535 prevClickMsg = clickMsg;
2536 } else {
2537 return 0;
2539 return clickNum;
2542 static BOOL ME_SetCursor(ME_TextEditor *editor)
2544 POINT pt;
2545 BOOL isExact;
2546 int offset;
2547 SCROLLBARINFO sbi;
2548 DWORD messagePos = GetMessagePos();
2549 pt.x = (short)LOWORD(messagePos);
2550 pt.y = (short)HIWORD(messagePos);
2552 sbi.cbSize = sizeof(sbi);
2553 GetScrollBarInfo(editor->hWnd, OBJID_HSCROLL, &sbi);
2554 if (!(sbi.rgstate[0] & (STATE_SYSTEM_INVISIBLE|STATE_SYSTEM_OFFSCREEN)) &&
2555 PtInRect(&sbi.rcScrollBar, pt))
2557 SetCursor(LoadCursorW(NULL, (WCHAR*)IDC_ARROW));
2558 return TRUE;
2560 sbi.cbSize = sizeof(sbi);
2561 GetScrollBarInfo(editor->hWnd, OBJID_VSCROLL, &sbi);
2562 if (!(sbi.rgstate[0] & (STATE_SYSTEM_INVISIBLE|STATE_SYSTEM_OFFSCREEN)) &&
2563 PtInRect(&sbi.rcScrollBar, pt))
2565 SetCursor(LoadCursorW(NULL, (WCHAR*)IDC_ARROW));
2566 return TRUE;
2568 ScreenToClient(editor->hWnd, &pt);
2570 if (editor->nSelectionType == stLine && editor->bMouseCaptured) {
2571 SetCursor(hLeft);
2572 return TRUE;
2574 if (!editor->bEmulateVersion10 /* v4.1 */ &&
2575 pt.y < editor->rcFormat.top &&
2576 pt.x < editor->rcFormat.left)
2578 SetCursor(hLeft);
2579 return TRUE;
2581 if (pt.y < editor->rcFormat.top || pt.y > editor->rcFormat.bottom)
2583 if (editor->bEmulateVersion10) /* v1.0 - 3.0 */
2584 SetCursor(LoadCursorW(NULL, (WCHAR*)IDC_ARROW));
2585 else /* v4.1 */
2586 SetCursor(LoadCursorW(NULL, (WCHAR*)IDC_IBEAM));
2587 return TRUE;
2589 if (pt.x < editor->rcFormat.left)
2591 SetCursor(hLeft);
2592 return TRUE;
2594 offset = ME_CharFromPos(editor, pt.x, pt.y, &isExact);
2595 if (isExact)
2597 if (editor->AutoURLDetect_bEnable)
2599 ME_Cursor cursor;
2600 ME_Run *run;
2601 ME_CursorFromCharOfs(editor, offset, &cursor);
2602 run = &cursor.pRun->member.run;
2603 if (editor->AutoURLDetect_bEnable &&
2604 run->style->fmt.dwMask & CFM_LINK &&
2605 run->style->fmt.dwEffects & CFE_LINK)
2607 SetCursor(LoadCursorW(NULL, (WCHAR*)IDC_HAND));
2608 return TRUE;
2611 if (ME_IsSelection(editor))
2613 int selStart, selEnd;
2614 ME_GetSelection(editor, &selStart, &selEnd);
2615 if (selStart <= offset && selEnd >= offset) {
2616 SetCursor(LoadCursorW(NULL, (WCHAR*)IDC_ARROW));
2617 return TRUE;
2621 SetCursor(LoadCursorW(NULL, (WCHAR*)IDC_IBEAM));
2622 return TRUE;
2625 static void ME_SetDefaultFormatRect(ME_TextEditor *editor)
2627 DWORD exstyle = GetWindowLongW(editor->hWnd, GWL_EXSTYLE);
2629 GetClientRect(editor->hWnd, &editor->rcFormat);
2630 editor->rcFormat.top += (exstyle & WS_EX_CLIENTEDGE ? 1 : 0);
2631 editor->rcFormat.left += 1 + editor->selofs;
2632 editor->rcFormat.right -= 1;
2635 static BOOL ME_ShowContextMenu(ME_TextEditor *editor, int x, int y)
2637 CHARRANGE selrange;
2638 HMENU menu;
2639 int seltype = 0;
2640 if(!editor->lpOleCallback)
2641 return FALSE;
2642 ME_GetSelection(editor, &selrange.cpMin, &selrange.cpMax);
2643 if(selrange.cpMin == selrange.cpMax)
2644 seltype |= SEL_EMPTY;
2645 else
2647 /* FIXME: Handle objects */
2648 seltype |= SEL_TEXT;
2649 if(selrange.cpMax-selrange.cpMin > 1)
2650 seltype |= SEL_MULTICHAR;
2652 if(SUCCEEDED(IRichEditOleCallback_GetContextMenu(editor->lpOleCallback, seltype, NULL, &selrange, &menu)))
2654 TrackPopupMenu(menu, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, GetParent(editor->hWnd), NULL);
2655 DestroyMenu(menu);
2657 return TRUE;
2660 static ME_TextEditor *ME_MakeEditor(HWND hWnd, BOOL bEmulateVersion10)
2662 ME_TextEditor *ed = ALLOC_OBJ(ME_TextEditor);
2663 int i;
2664 ed->hWnd = hWnd;
2665 ed->bEmulateVersion10 = bEmulateVersion10;
2666 ed->styleFlags = GetWindowLongW(hWnd, GWL_STYLE);
2667 if (ed->styleFlags & WS_VSCROLL)
2668 ed->styleFlags |= ES_AUTOVSCROLL;
2669 if (!ed->bEmulateVersion10 && (ed->styleFlags & WS_HSCROLL))
2670 ed->styleFlags |= ES_AUTOHSCROLL;
2671 ed->pBuffer = ME_MakeText();
2672 ed->nZoomNumerator = ed->nZoomDenominator = 0;
2673 ME_MakeFirstParagraph(ed);
2674 /* The four cursors are for:
2675 * 0 - The position where the caret is shown
2676 * 1 - The anchored end of the selection (for normal selection)
2677 * 2 & 3 - The anchored start and end respectively for word, line,
2678 * or paragraph selection.
2680 ed->nCursors = 4;
2681 ed->pCursors = ALLOC_N_OBJ(ME_Cursor, ed->nCursors);
2682 ed->pCursors[0].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
2683 ed->pCursors[0].nOffset = 0;
2684 ed->pCursors[1].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
2685 ed->pCursors[1].nOffset = 0;
2686 ed->pCursors[2] = ed->pCursors[0];
2687 ed->pCursors[3] = ed->pCursors[1];
2688 ed->nLastTotalLength = ed->nTotalLength = 0;
2689 ed->nLastTotalWidth = ed->nTotalWidth = 0;
2690 ed->nUDArrowX = -1;
2691 ed->nSequence = 0;
2692 ed->rgbBackColor = -1;
2693 ed->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
2694 ed->bCaretAtEnd = FALSE;
2695 ed->nEventMask = 0;
2696 ed->nModifyStep = 0;
2697 ed->nTextLimit = TEXT_LIMIT_DEFAULT;
2698 ed->pUndoStack = ed->pRedoStack = ed->pUndoStackBottom = NULL;
2699 ed->nUndoStackSize = 0;
2700 ed->nUndoLimit = STACK_SIZE_DEFAULT;
2701 ed->nUndoMode = umAddToUndo;
2702 ed->nParagraphs = 1;
2703 ed->nLastSelStart = ed->nLastSelEnd = 0;
2704 ed->pLastSelStartPara = ed->pLastSelEndPara = ME_FindItemFwd(ed->pBuffer->pFirst, diParagraph);
2705 ed->bWordWrap = !(ed->styleFlags & ES_AUTOHSCROLL);
2706 ed->bHideSelection = FALSE;
2707 ed->nInvalidOfs = -1;
2708 ed->pfnWordBreak = NULL;
2709 ed->lpOleCallback = NULL;
2710 ed->mode = TM_RICHTEXT | TM_MULTILEVELUNDO | TM_MULTICODEPAGE;
2711 ed->AutoURLDetect_bEnable = FALSE;
2712 ed->bHaveFocus = FALSE;
2713 ed->bMouseCaptured = FALSE;
2714 for (i=0; i<HFONT_CACHE_SIZE; i++)
2716 ed->pFontCache[i].nRefs = 0;
2717 ed->pFontCache[i].nAge = 0;
2718 ed->pFontCache[i].hFont = NULL;
2721 ME_CheckCharOffsets(ed);
2722 if (ed->styleFlags & ES_SELECTIONBAR)
2723 ed->selofs = SELECTIONBAR_WIDTH;
2724 else
2725 ed->selofs = 0;
2726 ed->bDefaultFormatRect = TRUE;
2727 ed->nSelectionType = stPosition;
2729 if (ed->styleFlags & ES_PASSWORD)
2730 ed->cPasswordMask = '*';
2731 else
2732 ed->cPasswordMask = 0;
2734 ed->notified_cr.cpMin = ed->notified_cr.cpMax = 0;
2736 /* Default scrollbar information */
2737 ed->vert_si.cbSize = sizeof(SCROLLINFO);
2738 ed->vert_si.nMin = 0;
2739 ed->vert_si.nMax = 0;
2740 ed->vert_si.nPage = 0;
2741 ed->vert_si.nPos = 0;
2743 ed->horz_si.cbSize = sizeof(SCROLLINFO);
2744 ed->horz_si.nMin = 0;
2745 ed->horz_si.nMax = 0;
2746 ed->horz_si.nPage = 0;
2747 ed->horz_si.nPos = 0;
2749 OleInitialize(NULL);
2751 return ed;
2754 static void ME_DestroyEditor(ME_TextEditor *editor)
2756 ME_DisplayItem *pFirst = editor->pBuffer->pFirst;
2757 ME_DisplayItem *p = pFirst, *pNext = NULL;
2758 int i;
2760 ME_ClearTempStyle(editor);
2761 ME_EmptyUndoStack(editor);
2762 while(p) {
2763 pNext = p->next;
2764 ME_DestroyDisplayItem(p);
2765 p = pNext;
2767 ME_ReleaseStyle(editor->pBuffer->pDefaultStyle);
2768 for (i=0; i<HFONT_CACHE_SIZE; i++)
2770 if (editor->pFontCache[i].hFont)
2771 DeleteObject(editor->pFontCache[i].hFont);
2773 if (editor->rgbBackColor != -1)
2774 DeleteObject(editor->hbrBackground);
2775 if(editor->lpOleCallback)
2776 IUnknown_Release(editor->lpOleCallback);
2777 SetWindowLongPtrW(editor->hWnd, 0, 0);
2778 OleUninitialize();
2780 FREE_OBJ(editor->pBuffer);
2781 FREE_OBJ(editor->pCursors);
2783 FREE_OBJ(editor);
2786 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
2788 TRACE("\n");
2789 switch (fdwReason)
2791 case DLL_PROCESS_ATTACH:
2792 DisableThreadLibraryCalls(hinstDLL);
2793 me_heap = HeapCreate (0, 0x10000, 0);
2794 if (!ME_RegisterEditorClass(hinstDLL)) return FALSE;
2795 hLeft = LoadCursorW(hinstDLL, MAKEINTRESOURCEW(OCR_REVERSE));
2796 LookupInit();
2797 break;
2799 case DLL_PROCESS_DETACH:
2800 UnregisterClassW(RichEdit20W, 0);
2801 UnregisterClassW(RichEdit50W, 0);
2802 UnregisterClassA("RichEdit20A", 0);
2803 UnregisterClassA("RichEdit50A", 0);
2804 if (ME_ListBoxRegistered)
2805 UnregisterClassW(REListBox20W, 0);
2806 if (ME_ComboBoxRegistered)
2807 UnregisterClassW(REComboBox20W, 0);
2808 LookupCleanup();
2809 HeapDestroy (me_heap);
2810 me_heap = NULL;
2811 break;
2813 return TRUE;
2817 static const char * const edit_messages[] = {
2818 "EM_GETSEL",
2819 "EM_SETSEL",
2820 "EM_GETRECT",
2821 "EM_SETRECT",
2822 "EM_SETRECTNP",
2823 "EM_SCROLL",
2824 "EM_LINESCROLL",
2825 "EM_SCROLLCARET",
2826 "EM_GETMODIFY",
2827 "EM_SETMODIFY",
2828 "EM_GETLINECOUNT",
2829 "EM_LINEINDEX",
2830 "EM_SETHANDLE",
2831 "EM_GETHANDLE",
2832 "EM_GETTHUMB",
2833 "EM_UNKNOWN_BF",
2834 "EM_UNKNOWN_C0",
2835 "EM_LINELENGTH",
2836 "EM_REPLACESEL",
2837 "EM_UNKNOWN_C3",
2838 "EM_GETLINE",
2839 "EM_LIMITTEXT",
2840 "EM_CANUNDO",
2841 "EM_UNDO",
2842 "EM_FMTLINES",
2843 "EM_LINEFROMCHAR",
2844 "EM_UNKNOWN_CA",
2845 "EM_SETTABSTOPS",
2846 "EM_SETPASSWORDCHAR",
2847 "EM_EMPTYUNDOBUFFER",
2848 "EM_GETFIRSTVISIBLELINE",
2849 "EM_SETREADONLY",
2850 "EM_SETWORDBREAKPROC",
2851 "EM_GETWORDBREAKPROC",
2852 "EM_GETPASSWORDCHAR",
2853 "EM_SETMARGINS",
2854 "EM_GETMARGINS",
2855 "EM_GETLIMITTEXT",
2856 "EM_POSFROMCHAR",
2857 "EM_CHARFROMPOS",
2858 "EM_SETIMESTATUS",
2859 "EM_GETIMESTATUS"
2862 static const char * const richedit_messages[] = {
2863 "EM_CANPASTE",
2864 "EM_DISPLAYBAND",
2865 "EM_EXGETSEL",
2866 "EM_EXLIMITTEXT",
2867 "EM_EXLINEFROMCHAR",
2868 "EM_EXSETSEL",
2869 "EM_FINDTEXT",
2870 "EM_FORMATRANGE",
2871 "EM_GETCHARFORMAT",
2872 "EM_GETEVENTMASK",
2873 "EM_GETOLEINTERFACE",
2874 "EM_GETPARAFORMAT",
2875 "EM_GETSELTEXT",
2876 "EM_HIDESELECTION",
2877 "EM_PASTESPECIAL",
2878 "EM_REQUESTRESIZE",
2879 "EM_SELECTIONTYPE",
2880 "EM_SETBKGNDCOLOR",
2881 "EM_SETCHARFORMAT",
2882 "EM_SETEVENTMASK",
2883 "EM_SETOLECALLBACK",
2884 "EM_SETPARAFORMAT",
2885 "EM_SETTARGETDEVICE",
2886 "EM_STREAMIN",
2887 "EM_STREAMOUT",
2888 "EM_GETTEXTRANGE",
2889 "EM_FINDWORDBREAK",
2890 "EM_SETOPTIONS",
2891 "EM_GETOPTIONS",
2892 "EM_FINDTEXTEX",
2893 "EM_GETWORDBREAKPROCEX",
2894 "EM_SETWORDBREAKPROCEX",
2895 "EM_SETUNDOLIMIT",
2896 "EM_UNKNOWN_USER_83",
2897 "EM_REDO",
2898 "EM_CANREDO",
2899 "EM_GETUNDONAME",
2900 "EM_GETREDONAME",
2901 "EM_STOPGROUPTYPING",
2902 "EM_SETTEXTMODE",
2903 "EM_GETTEXTMODE",
2904 "EM_AUTOURLDETECT",
2905 "EM_GETAUTOURLDETECT",
2906 "EM_SETPALETTE",
2907 "EM_GETTEXTEX",
2908 "EM_GETTEXTLENGTHEX",
2909 "EM_SHOWSCROLLBAR",
2910 "EM_SETTEXTEX",
2911 "EM_UNKNOWN_USER_98",
2912 "EM_UNKNOWN_USER_99",
2913 "EM_SETPUNCTUATION",
2914 "EM_GETPUNCTUATION",
2915 "EM_SETWORDWRAPMODE",
2916 "EM_GETWORDWRAPMODE",
2917 "EM_SETIMECOLOR",
2918 "EM_GETIMECOLOR",
2919 "EM_SETIMEOPTIONS",
2920 "EM_GETIMEOPTIONS",
2921 "EM_CONVPOSITION",
2922 "EM_UNKNOWN_USER_109",
2923 "EM_UNKNOWN_USER_110",
2924 "EM_UNKNOWN_USER_111",
2925 "EM_UNKNOWN_USER_112",
2926 "EM_UNKNOWN_USER_113",
2927 "EM_UNKNOWN_USER_114",
2928 "EM_UNKNOWN_USER_115",
2929 "EM_UNKNOWN_USER_116",
2930 "EM_UNKNOWN_USER_117",
2931 "EM_UNKNOWN_USER_118",
2932 "EM_UNKNOWN_USER_119",
2933 "EM_SETLANGOPTIONS",
2934 "EM_GETLANGOPTIONS",
2935 "EM_GETIMECOMPMODE",
2936 "EM_FINDTEXTW",
2937 "EM_FINDTEXTEXW",
2938 "EM_RECONVERSION",
2939 "EM_SETIMEMODEBIAS",
2940 "EM_GETIMEMODEBIAS"
2943 static const char *
2944 get_msg_name(UINT msg)
2946 if (msg >= EM_GETSEL && msg <= EM_CHARFROMPOS)
2947 return edit_messages[msg - EM_GETSEL];
2948 if (msg >= EM_CANPASTE && msg <= EM_GETIMEMODEBIAS)
2949 return richedit_messages[msg - EM_CANPASTE];
2950 return "";
2953 void ME_LinkNotify(ME_TextEditor *editor, UINT msg, WPARAM wParam, LPARAM lParam)
2955 int x,y;
2956 ME_Cursor tmpCursor;
2957 BOOL isExact;
2958 int nCharOfs; /* The start of the clicked text. Absolute character offset */
2960 ME_Run *tmpRun;
2962 ENLINK info;
2963 x = (short)LOWORD(lParam);
2964 y = (short)HIWORD(lParam);
2965 nCharOfs = ME_CharFromPos(editor, x, y, &isExact);
2966 if (!isExact) return;
2968 ME_CursorFromCharOfs(editor, nCharOfs, &tmpCursor);
2969 tmpRun = &tmpCursor.pRun->member.run;
2971 if ((tmpRun->style->fmt.dwMask & CFM_LINK)
2972 && (tmpRun->style->fmt.dwEffects & CFE_LINK))
2973 { /* The clicked run has CFE_LINK set */
2974 info.nmhdr.hwndFrom = editor->hWnd;
2975 info.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
2976 info.nmhdr.code = EN_LINK;
2977 info.msg = msg;
2978 info.wParam = wParam;
2979 info.lParam = lParam;
2980 info.chrg.cpMin = ME_CharOfsFromRunOfs(editor,tmpCursor.pRun,0);
2981 info.chrg.cpMax = info.chrg.cpMin + ME_StrVLen(tmpRun->strText);
2982 SendMessageW(GetParent(editor->hWnd), WM_NOTIFY,info.nmhdr.idFrom, (LPARAM)&info);
2986 #define UNSUPPORTED_MSG(e) \
2987 case e: \
2988 FIXME(#e ": stub\n"); \
2989 *phresult = S_FALSE; \
2990 return 0;
2992 /* Handle messages for windowless and windoweded richedit controls.
2994 * The LRESULT that is returned is a return value for window procs,
2995 * and the phresult parameter is the COM return code needed by the
2996 * text services interface. */
2997 static LRESULT ME_HandleMessage(ME_TextEditor *editor, UINT msg, WPARAM wParam,
2998 LPARAM lParam, BOOL unicode, HRESULT* phresult)
3000 *phresult = S_OK;
3002 switch(msg) {
3004 UNSUPPORTED_MSG(EM_DISPLAYBAND)
3005 UNSUPPORTED_MSG(EM_FINDWORDBREAK)
3006 UNSUPPORTED_MSG(EM_FMTLINES)
3007 UNSUPPORTED_MSG(EM_FORMATRANGE)
3008 UNSUPPORTED_MSG(EM_GETBIDIOPTIONS)
3009 UNSUPPORTED_MSG(EM_GETEDITSTYLE)
3010 UNSUPPORTED_MSG(EM_GETIMECOMPMODE)
3011 UNSUPPORTED_MSG(EM_GETIMESTATUS)
3012 UNSUPPORTED_MSG(EM_SETIMESTATUS)
3013 UNSUPPORTED_MSG(EM_GETLANGOPTIONS)
3014 /* UNSUPPORTED_MSG(EM_GETOLEINTERFACE) separate stub */
3015 UNSUPPORTED_MSG(EM_GETREDONAME)
3016 UNSUPPORTED_MSG(EM_GETTEXTMODE)
3017 UNSUPPORTED_MSG(EM_GETTYPOGRAPHYOPTIONS)
3018 UNSUPPORTED_MSG(EM_GETUNDONAME)
3019 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX)
3020 UNSUPPORTED_MSG(EM_PASTESPECIAL)
3021 UNSUPPORTED_MSG(EM_SELECTIONTYPE)
3022 UNSUPPORTED_MSG(EM_SETBIDIOPTIONS)
3023 UNSUPPORTED_MSG(EM_SETEDITSTYLE)
3024 UNSUPPORTED_MSG(EM_SETFONTSIZE)
3025 UNSUPPORTED_MSG(EM_SETLANGOPTIONS)
3026 UNSUPPORTED_MSG(EM_SETMARGINS)
3027 UNSUPPORTED_MSG(EM_SETPALETTE)
3028 UNSUPPORTED_MSG(EM_SETTABSTOPS)
3029 UNSUPPORTED_MSG(EM_SETTYPOGRAPHYOPTIONS)
3030 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX)
3031 UNSUPPORTED_MSG(WM_STYLECHANGING)
3032 UNSUPPORTED_MSG(WM_STYLECHANGED)
3034 /* Messages specific to Richedit controls */
3036 case EM_STREAMIN:
3037 return ME_StreamIn(editor, wParam, (EDITSTREAM*)lParam, TRUE);
3038 case EM_STREAMOUT:
3039 return ME_StreamOut(editor, wParam, (EDITSTREAM *)lParam);
3040 case WM_GETDLGCODE:
3042 UINT code = DLGC_WANTCHARS|DLGC_WANTTAB|DLGC_WANTARROWS|DLGC_HASSETSEL;
3043 if (editor->styleFlags & ES_MULTILINE)
3044 code |= DLGC_WANTMESSAGE;
3045 return code;
3047 case EM_EMPTYUNDOBUFFER:
3048 ME_EmptyUndoStack(editor);
3049 return 0;
3050 case EM_GETSEL:
3052 /* Note: wParam/lParam can be NULL */
3053 UINT from, to;
3054 PUINT pfrom = wParam ? (PUINT)wParam : &from;
3055 PUINT pto = lParam ? (PUINT)lParam : &to;
3056 ME_GetSelection(editor, (int *)pfrom, (int *)pto);
3057 if ((*pfrom|*pto) & 0xFFFF0000)
3058 return -1;
3059 return MAKELONG(*pfrom,*pto);
3061 case EM_EXGETSEL:
3063 CHARRANGE *pRange = (CHARRANGE *)lParam;
3064 ME_GetSelection(editor, &pRange->cpMin, &pRange->cpMax);
3065 TRACE("EM_EXGETSEL = (%d,%d)\n", pRange->cpMin, pRange->cpMax);
3066 return 0;
3068 case EM_SETUNDOLIMIT:
3070 if ((int)wParam < 0)
3071 editor->nUndoLimit = STACK_SIZE_DEFAULT;
3072 else
3073 editor->nUndoLimit = min(wParam, STACK_SIZE_MAX);
3074 /* Setting a max stack size keeps wine from getting killed
3075 for hogging memory. Windows allocates all this memory at once, so
3076 no program would realistically set a value above our maximum. */
3077 return editor->nUndoLimit;
3079 case EM_CANUNDO:
3080 return editor->pUndoStack != NULL;
3081 case EM_CANREDO:
3082 return editor->pRedoStack != NULL;
3083 case WM_UNDO: /* FIXME: actually not the same */
3084 case EM_UNDO:
3085 return ME_Undo(editor);
3086 case EM_REDO:
3087 return ME_Redo(editor);
3088 case EM_GETOPTIONS:
3090 /* these flags are equivalent to the ES_* counterparts */
3091 DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
3092 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN | ECO_SELECTIONBAR;
3093 DWORD settings = editor->styleFlags & mask;
3095 return settings;
3097 case EM_SETOPTIONS:
3099 /* these flags are equivalent to ES_* counterparts
3100 * ECO_READONLY is already implemented in the code, only requires
3101 * setting the bit to work
3103 DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
3104 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN | ECO_SELECTIONBAR;
3105 DWORD settings = mask & editor->styleFlags;
3106 DWORD oldSettings = settings;
3107 DWORD changedSettings;
3109 switch(wParam)
3111 case ECOOP_SET:
3112 settings = lParam;
3113 break;
3114 case ECOOP_OR:
3115 settings |= lParam;
3116 break;
3117 case ECOOP_AND:
3118 settings &= lParam;
3119 break;
3120 case ECOOP_XOR:
3121 settings ^= lParam;
3123 changedSettings = oldSettings ^ settings;
3125 if (settings & ECO_AUTOWORDSELECTION)
3126 FIXME("ECO_AUTOWORDSELECTION not implemented yet!\n");
3128 if (oldSettings ^ settings) {
3129 DWORD dwStyle = GetWindowLongW(editor->hWnd, GWL_STYLE);
3131 editor->styleFlags = (editor->styleFlags & ~mask) | (settings & mask);
3132 SetWindowLongW(editor->hWnd, GWL_STYLE, (dwStyle & ~mask) | (settings & mask));
3134 if (settings & ECO_SELECTIONBAR) {
3135 editor->selofs = SELECTIONBAR_WIDTH;
3136 editor->rcFormat.left += SELECTIONBAR_WIDTH;
3137 } else {
3138 editor->selofs = 0;
3139 editor->rcFormat.left -= SELECTIONBAR_WIDTH;
3141 ME_WrapMarkedParagraphs(editor);
3144 if (settings & ECO_VERTICAL)
3145 FIXME("ECO_VERTICAL not implemented yet!\n");
3146 if (settings & ECO_AUTOHSCROLL)
3147 FIXME("ECO_AUTOHSCROLL not implemented yet!\n");
3148 if (settings & ECO_AUTOVSCROLL)
3149 FIXME("ECO_AUTOVSCROLL not implemented yet!\n");
3150 if (settings & ECO_NOHIDESEL)
3151 FIXME("ECO_NOHIDESEL not implemented yet!\n");
3152 if (settings & ECO_WANTRETURN)
3153 FIXME("ECO_WANTRETURN not implemented yet!\n");
3155 return settings;
3157 case EM_SETSEL:
3159 ME_InvalidateSelection(editor);
3160 ME_SetSelection(editor, wParam, lParam);
3161 ME_InvalidateSelection(editor);
3162 HideCaret(editor->hWnd);
3163 ME_ShowCaret(editor);
3164 ME_SendSelChange(editor);
3165 return 0;
3167 case EM_SETSCROLLPOS:
3169 POINT *point = (POINT *)lParam;
3170 ME_ScrollAbs(editor, point->x, point->y);
3171 return 0;
3173 case EM_AUTOURLDETECT:
3175 if (wParam==1 || wParam ==0)
3177 editor->AutoURLDetect_bEnable = (BOOL)wParam;
3178 return 0;
3180 return E_INVALIDARG;
3182 case EM_GETAUTOURLDETECT:
3184 return editor->AutoURLDetect_bEnable;
3186 case EM_EXSETSEL:
3188 int end;
3189 CHARRANGE range = *(CHARRANGE *)lParam;
3191 TRACE("EM_EXSETSEL (%d,%d)\n", range.cpMin, range.cpMax);
3193 ME_InvalidateSelection(editor);
3194 end = ME_SetSelection(editor, range.cpMin, range.cpMax);
3195 ME_InvalidateSelection(editor);
3196 HideCaret(editor->hWnd);
3197 ME_ShowCaret(editor);
3198 ME_SendSelChange(editor);
3200 return end;
3202 case EM_SHOWSCROLLBAR:
3204 ShowScrollBar(editor->hWnd, wParam, lParam);
3205 return 0;
3207 case EM_SETTEXTEX:
3209 LPWSTR wszText;
3210 SETTEXTEX *pStruct = (SETTEXTEX *)wParam;
3211 size_t len;
3212 int from, to;
3213 ME_Style *style;
3214 int oldModify = editor->nModifyStep;
3216 if (!pStruct) return 0;
3218 TRACE("EM_SETTEXTEX - %s, flags %d, cp %d\n",
3219 pStruct->codepage == 1200 ? debugstr_w((LPCWSTR)lParam) : debugstr_a((LPCSTR)lParam),
3220 pStruct->flags, pStruct->codepage);
3222 /* FIXME: make use of pStruct->codepage in the to unicode translation */
3223 wszText = lParam ? ME_ToUnicode(pStruct->codepage == 1200, (void *)lParam) : NULL;
3224 len = wszText ? lstrlenW(wszText) : 0;
3226 if (pStruct->flags & ST_SELECTION) {
3227 ME_GetSelection(editor, &from, &to);
3228 style = ME_GetSelectionInsertStyle(editor);
3229 ME_InternalDeleteText(editor, from, to - from, FALSE);
3230 if (pStruct->codepage != 1200 && lParam &&
3231 (!strncmp((char *)lParam, "{\\rtf", 5) || !strncmp((char *)lParam, "{\\urtf}", 6)))
3232 ME_StreamInRTFString(editor, 1, (char *)lParam);
3233 else ME_InsertTextFromCursor(editor, 0, wszText, len, style);
3234 ME_ReleaseStyle(style);
3236 if (editor->AutoURLDetect_bEnable) ME_UpdateSelectionLinkAttribute(editor);
3238 else {
3239 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor), FALSE);
3240 if (pStruct->codepage != 1200 && lParam &&
3241 (!strncmp((char *)lParam, "{\\rtf", 5) || !strncmp((char *)lParam, "{\\urtf}", 6)))
3242 ME_StreamInRTFString(editor, 0, (char *)lParam);
3243 else ME_InsertTextFromCursor(editor, 0, wszText, len, editor->pBuffer->pDefaultStyle);
3244 len = 1;
3246 if (editor->AutoURLDetect_bEnable) ME_UpdateLinkAttribute(editor, 0, -1);
3248 ME_CommitUndo(editor);
3249 if (!(pStruct->flags & ST_KEEPUNDO))
3251 editor->nModifyStep = oldModify;
3252 ME_EmptyUndoStack(editor);
3254 ME_UpdateRepaint(editor);
3255 return len;
3257 case EM_SETBKGNDCOLOR:
3259 LRESULT lColor;
3260 if (editor->rgbBackColor != -1) {
3261 DeleteObject(editor->hbrBackground);
3262 lColor = editor->rgbBackColor;
3264 else lColor = GetSysColor(COLOR_WINDOW);
3266 if (wParam)
3268 editor->rgbBackColor = -1;
3269 editor->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
3271 else
3273 editor->rgbBackColor = lParam;
3274 editor->hbrBackground = CreateSolidBrush(editor->rgbBackColor);
3276 InvalidateRect(editor->hWnd, NULL, TRUE);
3277 UpdateWindow(editor->hWnd);
3278 return lColor;
3280 case EM_GETMODIFY:
3281 return editor->nModifyStep == 0 ? 0 : -1;
3282 case EM_SETMODIFY:
3284 if (wParam)
3285 editor->nModifyStep = 1;
3286 else
3287 editor->nModifyStep = 0;
3289 return 0;
3291 case EM_SETREADONLY:
3293 LONG winStyle = GetWindowLongW(editor->hWnd, GWL_STYLE);
3294 if (wParam) {
3295 editor->styleFlags |= ES_READONLY;
3296 winStyle |= ES_READONLY;
3297 } else {
3298 editor->styleFlags &= ~ES_READONLY;
3299 winStyle &= ~ES_READONLY;
3301 SetWindowLongW(editor->hWnd, GWL_STYLE, winStyle);
3302 return 0;
3304 case EM_SETEVENTMASK:
3306 DWORD nOldMask = editor->nEventMask;
3308 editor->nEventMask = lParam;
3309 return nOldMask;
3311 case EM_GETEVENTMASK:
3312 return editor->nEventMask;
3313 case EM_SETCHARFORMAT:
3315 CHARFORMAT2W buf, *p;
3316 BOOL bRepaint = TRUE;
3317 p = ME_ToCF2W(&buf, (CHARFORMAT2W *)lParam);
3318 if (p == NULL) return 0;
3319 if (!wParam)
3320 ME_SetDefaultCharFormat(editor, p);
3321 else if (wParam == (SCF_WORD | SCF_SELECTION)) {
3322 FIXME("EM_SETCHARFORMAT: word selection not supported\n");
3323 return 0;
3324 } else if (wParam == SCF_ALL) {
3325 if (editor->mode & TM_PLAINTEXT)
3326 ME_SetDefaultCharFormat(editor, p);
3327 else {
3328 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), p);
3329 editor->nModifyStep = 1;
3331 } else if (editor->mode & TM_PLAINTEXT) {
3332 return 0;
3333 } else {
3334 int from, to;
3335 ME_GetSelection(editor, &from, &to);
3336 bRepaint = (from != to);
3337 ME_SetSelectionCharFormat(editor, p);
3338 if (from != to) editor->nModifyStep = 1;
3340 ME_CommitUndo(editor);
3341 if (bRepaint)
3343 ME_WrapMarkedParagraphs(editor);
3344 ME_UpdateScrollBar(editor);
3345 ME_Repaint(editor);
3347 return 1;
3349 case EM_GETCHARFORMAT:
3351 CHARFORMAT2W tmp, *dst = (CHARFORMAT2W *)lParam;
3352 if (dst->cbSize != sizeof(CHARFORMATA) &&
3353 dst->cbSize != sizeof(CHARFORMATW) &&
3354 dst->cbSize != sizeof(CHARFORMAT2A) &&
3355 dst->cbSize != sizeof(CHARFORMAT2W))
3356 return 0;
3357 tmp.cbSize = sizeof(tmp);
3358 if (!wParam)
3359 ME_GetDefaultCharFormat(editor, &tmp);
3360 else
3361 ME_GetSelectionCharFormat(editor, &tmp);
3362 ME_CopyToCFAny(dst, &tmp);
3363 return tmp.dwMask;
3365 case EM_SETPARAFORMAT:
3367 BOOL result = ME_SetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
3368 ME_WrapMarkedParagraphs(editor);
3369 ME_UpdateScrollBar(editor);
3370 ME_Repaint(editor);
3371 ME_CommitUndo(editor);
3372 return result;
3374 case EM_GETPARAFORMAT:
3375 ME_GetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
3376 return ((PARAFORMAT2 *)lParam)->dwMask;
3377 case EM_GETFIRSTVISIBLELINE:
3379 ME_DisplayItem *p = editor->pBuffer->pFirst;
3380 int y = editor->vert_si.nPos;
3381 int ypara = 0;
3382 int count = 0;
3383 int ystart, yend;
3384 while(p) {
3385 p = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
3386 if (p->type == diTextEnd)
3387 break;
3388 if (p->type == diParagraph) {
3389 ypara = p->member.para.pt.y;
3390 continue;
3392 ystart = ypara + p->member.row.pt.y;
3393 yend = ystart + p->member.row.nHeight;
3394 if (y < yend) {
3395 break;
3397 count++;
3399 return count;
3401 case EM_HIDESELECTION:
3403 editor->bHideSelection = (wParam != 0);
3404 ME_InvalidateSelection(editor);
3405 return 0;
3407 case EM_LINESCROLL:
3409 ME_ScrollDown(editor, lParam * 8); /* FIXME follow the original */
3410 return TRUE; /* Should return false if a single line richedit control */
3412 case WM_CLEAR:
3414 int from, to;
3415 ME_GetSelection(editor, &from, &to);
3416 ME_InternalDeleteText(editor, from, to-from, FALSE);
3417 ME_CommitUndo(editor);
3418 ME_UpdateRepaint(editor);
3419 return 0;
3421 case EM_REPLACESEL:
3423 int from, to;
3424 ME_Style *style;
3425 LPWSTR wszText = lParam ? ME_ToUnicode(unicode, (void *)lParam) : NULL;
3426 size_t len = wszText ? lstrlenW(wszText) : 0;
3427 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText));
3429 ME_GetSelection(editor, &from, &to);
3430 style = ME_GetSelectionInsertStyle(editor);
3431 ME_InternalDeleteText(editor, from, to-from, FALSE);
3432 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
3433 ME_ReleaseStyle(style);
3434 /* drop temporary style if line end */
3436 * FIXME question: does abc\n mean: put abc,
3437 * clear temp style, put \n? (would require a change)
3439 if (len>0 && wszText[len-1] == '\n')
3440 ME_ClearTempStyle(editor);
3441 ME_EndToUnicode(unicode, wszText);
3442 ME_CommitUndo(editor);
3443 if (editor->AutoURLDetect_bEnable) ME_UpdateSelectionLinkAttribute(editor);
3444 if (!wParam)
3445 ME_EmptyUndoStack(editor);
3446 ME_UpdateRepaint(editor);
3447 return len;
3449 case EM_SCROLLCARET:
3450 ME_EnsureVisible(editor, &editor->pCursors[0]);
3451 return 0;
3452 case WM_SETFONT:
3454 LOGFONTW lf;
3455 CHARFORMAT2W fmt;
3456 HDC hDC;
3457 BOOL bRepaint = LOWORD(lParam);
3459 if (!wParam)
3460 wParam = (WPARAM)GetStockObject(SYSTEM_FONT);
3461 GetObjectW((HGDIOBJ)wParam, sizeof(LOGFONTW), &lf);
3462 hDC = GetDC(editor->hWnd);
3463 ME_CharFormatFromLogFont(hDC, &lf, &fmt);
3464 ReleaseDC(editor->hWnd, hDC);
3465 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), &fmt);
3466 ME_SetDefaultCharFormat(editor, &fmt);
3468 ME_CommitUndo(editor);
3469 if (bRepaint)
3470 ME_RewrapRepaint(editor);
3471 return 0;
3473 case WM_SETTEXT:
3475 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor), FALSE);
3476 if (lParam)
3478 TRACE("WM_SETTEXT lParam==%lx\n",lParam);
3479 if (!unicode && !strncmp((char *)lParam, "{\\rtf", 5))
3481 /* Undocumented: WM_SETTEXT supports RTF text */
3482 ME_StreamInRTFString(editor, 0, (char *)lParam);
3484 else
3486 LPWSTR wszText = ME_ToUnicode(unicode, (void *)lParam);
3487 TRACE("WM_SETTEXT - %s\n", debugstr_w(wszText)); /* debugstr_w() */
3488 if (lstrlenW(wszText) > 0)
3490 int len = -1;
3492 /* uses default style! */
3493 if (!(editor->styleFlags & ES_MULTILINE))
3495 WCHAR * p;
3497 p = wszText;
3498 while (*p != '\0' && *p != '\r' && *p != '\n') p++;
3499 len = p - wszText;
3501 ME_InsertTextFromCursor(editor, 0, wszText, len, editor->pBuffer->pDefaultStyle);
3503 ME_EndToUnicode(unicode, wszText);
3506 else
3507 TRACE("WM_SETTEXT - NULL\n");
3508 if (editor->AutoURLDetect_bEnable)
3510 ME_UpdateLinkAttribute(editor, 0, -1);
3512 ME_SetSelection(editor, 0, 0);
3513 editor->nModifyStep = 0;
3514 ME_CommitUndo(editor);
3515 ME_EmptyUndoStack(editor);
3516 ME_UpdateRepaint(editor);
3517 return 1;
3519 case EM_CANPASTE:
3521 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
3522 if (IsClipboardFormatAvailable(nRTFFormat))
3523 return TRUE;
3524 if (IsClipboardFormatAvailable(CF_UNICODETEXT))
3525 return TRUE;
3526 return FALSE;
3528 case WM_PASTE:
3529 ME_Paste(editor);
3530 return 0;
3531 case WM_CUT:
3532 case WM_COPY:
3534 CHARRANGE range;
3535 ME_GetSelection(editor, &range.cpMin, &range.cpMax);
3537 if (ME_Copy(editor, &range) && msg == WM_CUT)
3539 ME_InternalDeleteText(editor, range.cpMin, range.cpMax-range.cpMin, FALSE);
3540 ME_CommitUndo(editor);
3541 ME_UpdateRepaint(editor);
3543 return 0;
3545 case WM_GETTEXTLENGTH:
3547 GETTEXTLENGTHEX how;
3549 /* CR/LF conversion required in 2.0 mode, verbatim in 1.0 mode */
3550 how.flags = GTL_CLOSE | (editor->bEmulateVersion10 ? 0 : GTL_USECRLF) | GTL_NUMCHARS;
3551 how.codepage = unicode ? 1200 : CP_ACP;
3552 return ME_GetTextLengthEx(editor, &how);
3554 case EM_GETTEXTLENGTHEX:
3555 return ME_GetTextLengthEx(editor, (GETTEXTLENGTHEX *)wParam);
3556 case WM_GETTEXT:
3558 GETTEXTEX ex;
3559 LRESULT rc;
3560 LPSTR bufferA = NULL;
3561 LPWSTR bufferW = NULL;
3563 if (unicode)
3564 bufferW = heap_alloc((wParam + 2) * sizeof(WCHAR));
3565 else
3566 bufferA = heap_alloc(wParam + 2);
3568 ex.cb = (wParam + 2) * (unicode ? sizeof(WCHAR) : sizeof(CHAR));
3569 ex.flags = GT_USECRLF;
3570 ex.codepage = unicode ? 1200 : CP_ACP;
3571 ex.lpDefaultChar = NULL;
3572 ex.lpUsedDefChar = NULL;
3574 rc = ME_GetTextEx(editor, &ex, unicode ? (LPARAM)bufferW : (LPARAM)bufferA);
3576 if (unicode)
3578 memcpy((LPWSTR)lParam, bufferW, wParam * sizeof(WCHAR));
3579 if (strlenW(bufferW) >= wParam) rc = 0;
3581 else
3583 memcpy((LPSTR)lParam, bufferA, wParam);
3584 if (strlen(bufferA) >= wParam) rc = 0;
3586 heap_free(bufferA);
3587 heap_free(bufferW);
3588 return rc;
3590 case EM_GETTEXTEX:
3591 return ME_GetTextEx(editor, (GETTEXTEX*)wParam, lParam);
3592 case EM_GETSELTEXT:
3594 int from, to;
3595 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
3596 ME_GetSelection(editor, &from, &to);
3597 tr.chrg.cpMin = from;
3598 tr.chrg.cpMax = to;
3599 tr.lpstrText = (WCHAR *)lParam;
3600 return ME_GetTextRange(editor, &tr, unicode);
3602 case EM_GETSCROLLPOS:
3604 POINT *point = (POINT *)lParam;
3605 point->x = editor->horz_si.nPos;
3606 point->y = editor->vert_si.nPos;
3607 return 1;
3609 case EM_GETTEXTRANGE:
3611 TEXTRANGEW *rng = (TEXTRANGEW *)lParam;
3612 TRACE("EM_GETTEXTRANGE min=%d max=%d unicode=%d emul1.0=%d length=%d\n",
3613 rng->chrg.cpMin, rng->chrg.cpMax, unicode,
3614 editor->bEmulateVersion10, ME_GetTextLength(editor));
3615 return ME_GetTextRange(editor, rng, unicode);
3617 case EM_GETLINE:
3619 ME_DisplayItem *run;
3620 const unsigned int nMaxChars = *(WORD *) lParam;
3621 unsigned int nCharsLeft = nMaxChars;
3622 char *dest = (char *) lParam;
3623 BOOL wroteNull = FALSE;
3625 TRACE("EM_GETLINE: row=%d, nMaxChars=%d (%s)\n", (int) wParam, nMaxChars,
3626 unicode ? "Unicode" : "Ansi");
3628 run = ME_FindRowWithNumber(editor, wParam);
3629 if (run == NULL)
3630 return 0;
3632 while (nCharsLeft && (run = ME_FindItemFwd(run, diRunOrStartRow))
3633 && !(run->member.run.nFlags & MERF_ENDPARA))
3635 unsigned int nCopy;
3636 ME_String *strText;
3637 if (run->type != diRun)
3638 break;
3639 strText = run->member.run.strText;
3640 nCopy = min(nCharsLeft, strText->nLen);
3642 if (unicode)
3643 memcpy(dest, strText->szData, nCopy * sizeof(WCHAR));
3644 else
3645 nCopy = WideCharToMultiByte(CP_ACP, 0, strText->szData, nCopy, dest,
3646 nCharsLeft, NULL, NULL);
3647 dest += nCopy * (unicode ? sizeof(WCHAR) : 1);
3648 nCharsLeft -= nCopy;
3651 /* append line termination, space allowing */
3652 if (nCharsLeft > 0)
3654 if (run && (run->member.run.nFlags & MERF_ENDPARA))
3656 int i;
3657 /* Write as many \r as encoded in end-of-paragraph, space allowing */
3658 for (i = 0; i < run->member.run.nCR && nCharsLeft > 0; i++, nCharsLeft--)
3660 *((WCHAR *)dest) = '\r';
3661 dest += unicode ? sizeof(WCHAR) : 1;
3663 /* Write as many \n as encoded in end-of-paragraph, space allowing */
3664 for (i = 0; i < run->member.run.nLF && nCharsLeft > 0; i++, nCharsLeft--)
3666 *((WCHAR *)dest) = '\n';
3667 dest += unicode ? sizeof(WCHAR) : 1;
3670 if (nCharsLeft > 0)
3672 if (unicode)
3673 *((WCHAR *)dest) = '\0';
3674 else
3675 *dest = '\0';
3676 nCharsLeft--;
3677 wroteNull = TRUE;
3681 TRACE("EM_GETLINE: got %u characters\n", nMaxChars - nCharsLeft);
3682 return nMaxChars - nCharsLeft - (wroteNull ? 1 : 0);
3684 case EM_GETLINECOUNT:
3686 ME_DisplayItem *item = editor->pBuffer->pFirst->next;
3687 int nRows = 0;
3689 ME_DisplayItem *prev_para = NULL, *last_para = NULL;
3691 while (item != editor->pBuffer->pLast)
3693 assert(item->type == diParagraph);
3694 prev_para = ME_FindItemBack(item, diRun);
3695 if (prev_para) {
3696 assert(prev_para->member.run.nFlags & MERF_ENDPARA);
3698 nRows += item->member.para.nRows;
3699 item = item->member.para.next_para;
3701 last_para = ME_FindItemBack(item, diRun);
3702 assert(last_para);
3703 assert(last_para->member.run.nFlags & MERF_ENDPARA);
3704 if (editor->bEmulateVersion10 && prev_para && last_para->member.run.nCharOfs == 0
3705 && prev_para->member.run.nCR == 1 && prev_para->member.run.nLF == 0)
3707 /* In 1.0 emulation, the last solitary \r at the very end of the text
3708 (if one exists) is NOT a line break.
3709 FIXME: this is an ugly hack. This should have a more regular model. */
3710 nRows--;
3713 TRACE("EM_GETLINECOUNT: nRows==%d\n", nRows);
3714 return max(1, nRows);
3716 case EM_LINEFROMCHAR:
3718 if (wParam == -1)
3719 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(editor, 1));
3720 else
3721 return ME_RowNumberFromCharOfs(editor, wParam);
3723 case EM_EXLINEFROMCHAR:
3725 if (lParam == -1)
3726 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(editor,1));
3727 else
3728 return ME_RowNumberFromCharOfs(editor, lParam);
3730 case EM_LINEINDEX:
3732 ME_DisplayItem *item, *para;
3733 int nCharOfs;
3735 if (wParam == -1)
3736 item = ME_FindItemBack(editor->pCursors[0].pRun, diStartRow);
3737 else
3738 item = ME_FindRowWithNumber(editor, wParam);
3739 if (!item)
3740 return -1;
3741 para = ME_GetParagraph(item);
3742 item = ME_FindItemFwd(item, diRun);
3743 nCharOfs = para->member.para.nCharOfs + item->member.run.nCharOfs;
3744 TRACE("EM_LINEINDEX: nCharOfs==%d\n", nCharOfs);
3745 return nCharOfs;
3747 case EM_LINELENGTH:
3749 ME_DisplayItem *item, *item_end;
3750 int nChars = 0, nThisLineOfs = 0, nNextLineOfs = 0;
3752 if (wParam > ME_GetTextLength(editor))
3753 return 0;
3754 if (wParam == -1)
3756 FIXME("EM_LINELENGTH: returning number of unselected characters on lines with selection unsupported.\n");
3757 return 0;
3759 item = ME_FindItemAtOffset(editor, diRun, wParam, NULL);
3760 item = ME_RowStart(item);
3761 nThisLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item, diRun), 0);
3762 item_end = ME_FindItemFwd(item, diStartRowOrParagraphOrEnd);
3763 if (item_end->type == diStartRow)
3764 nNextLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item_end, diRun), 0);
3765 else
3767 ME_DisplayItem *endPara;
3769 nNextLineOfs = ME_FindItemFwd(item, diParagraphOrEnd)->member.para.nCharOfs;
3770 endPara = ME_FindItemFwd(item, diParagraphOrEnd);
3771 endPara = ME_FindItemBack(endPara, diRun);
3772 assert(endPara);
3773 assert(endPara->type == diRun);
3774 assert(endPara->member.run.nFlags & MERF_ENDPARA);
3775 nNextLineOfs -= endPara->member.run.nCR + endPara->member.run.nLF;
3777 nChars = nNextLineOfs - nThisLineOfs;
3778 TRACE("EM_LINELENGTH(%ld)==%d\n",wParam, nChars);
3779 return nChars;
3781 case EM_EXLIMITTEXT:
3783 if ((int)lParam < 0)
3784 return 0;
3785 if (lParam == 0)
3786 editor->nTextLimit = 65536;
3787 else
3788 editor->nTextLimit = (int) lParam;
3789 return 0;
3791 case EM_LIMITTEXT:
3793 if (wParam == 0)
3794 editor->nTextLimit = 65536;
3795 else
3796 editor->nTextLimit = (int) wParam;
3797 return 0;
3799 case EM_GETLIMITTEXT:
3801 return editor->nTextLimit;
3803 case EM_FINDTEXT:
3805 FINDTEXTA *ft = (FINDTEXTA *)lParam;
3806 int nChars = MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, NULL, 0);
3807 WCHAR *tmp;
3808 LRESULT r;
3810 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
3811 MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, tmp, nChars);
3812 r = ME_FindText(editor, wParam, &ft->chrg, tmp, NULL);
3813 FREE_OBJ( tmp );
3814 return r;
3816 case EM_FINDTEXTEX:
3818 FINDTEXTEXA *ex = (FINDTEXTEXA *)lParam;
3819 int nChars = MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, NULL, 0);
3820 WCHAR *tmp;
3821 LRESULT r;
3823 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
3824 MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, tmp, nChars);
3825 r = ME_FindText(editor, wParam, &ex->chrg, tmp, &ex->chrgText);
3826 FREE_OBJ( tmp );
3827 return r;
3829 case EM_FINDTEXTW:
3831 FINDTEXTW *ft = (FINDTEXTW *)lParam;
3832 return ME_FindText(editor, wParam, &ft->chrg, ft->lpstrText, NULL);
3834 case EM_FINDTEXTEXW:
3836 FINDTEXTEXW *ex = (FINDTEXTEXW *)lParam;
3837 return ME_FindText(editor, wParam, &ex->chrg, ex->lpstrText, &ex->chrgText);
3839 case EM_GETZOOM:
3840 if (!wParam || !lParam)
3841 return FALSE;
3842 *(int *)wParam = editor->nZoomNumerator;
3843 *(int *)lParam = editor->nZoomDenominator;
3844 return TRUE;
3845 case EM_SETZOOM:
3846 return ME_SetZoom(editor, wParam, lParam);
3847 case EM_CHARFROMPOS:
3848 return ME_CharFromPos(editor, ((POINTL *)lParam)->x, ((POINTL *)lParam)->y, NULL);
3849 case EM_POSFROMCHAR:
3851 ME_DisplayItem *pRun;
3852 int nCharOfs, nOffset, nLength;
3853 POINTL pt = {0,0};
3855 nCharOfs = wParam;
3856 /* detect which API version we're dealing with */
3857 if (wParam >= 0x40000)
3858 nCharOfs = lParam;
3859 nLength = ME_GetTextLength(editor);
3860 nCharOfs = min(nCharOfs, nLength);
3861 nCharOfs = max(nCharOfs, 0);
3863 ME_RunOfsFromCharOfs(editor, nCharOfs, &pRun, &nOffset);
3864 assert(pRun->type == diRun);
3865 pt.y = pRun->member.run.pt.y;
3866 pt.x = pRun->member.run.pt.x + ME_PointFromChar(editor, &pRun->member.run, nOffset);
3867 pt.y += ME_GetParagraph(pRun)->member.para.pt.y + editor->rcFormat.top;
3868 pt.x += editor->rcFormat.left;
3870 pt.x -= editor->horz_si.nPos;
3871 pt.y -= editor->vert_si.nPos;
3873 if (wParam >= 0x40000) {
3874 *(POINTL *)wParam = pt;
3876 return (wParam >= 0x40000) ? 0 : MAKELONG( pt.x, pt.y );
3878 case WM_CREATE:
3880 SCROLLINFO si;
3882 ME_SetDefaultFormatRect(editor);
3884 si.cbSize = sizeof(si);
3885 si.fMask = SIF_PAGE | SIF_RANGE;
3886 if (editor->styleFlags & ES_DISABLENOSCROLL)
3887 si.fMask |= SIF_DISABLENOSCROLL;
3888 si.nMax = (si.fMask & SIF_DISABLENOSCROLL) ? 1 : 0;
3889 si.nMin = 0;
3890 si.nPage = 0;
3891 SetScrollInfo(editor->hWnd, SB_VERT, &si, TRUE);
3892 SetScrollInfo(editor->hWnd, SB_HORZ, &si, TRUE);
3894 ME_CommitUndo(editor);
3895 ME_WrapMarkedParagraphs(editor);
3896 ME_MoveCaret(editor);
3897 return 0;
3899 case WM_DESTROY:
3900 ME_DestroyEditor(editor);
3901 return 0;
3902 case WM_SETCURSOR:
3904 return ME_SetCursor(editor);
3906 case WM_LBUTTONDBLCLK:
3907 case WM_LBUTTONDOWN:
3909 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
3910 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
3911 !ME_FilterEvent(editor, msg, &wParam, &lParam))
3912 return 0;
3913 SetFocus(editor->hWnd);
3914 ME_LButtonDown(editor, (short)LOWORD(lParam), (short)HIWORD(lParam),
3915 ME_CalculateClickCount(editor, msg, wParam, lParam));
3916 SetCapture(editor->hWnd);
3917 editor->bMouseCaptured = TRUE;
3918 ME_LinkNotify(editor,msg,wParam,lParam);
3919 if (!ME_SetCursor(editor)) goto do_default;
3920 break;
3922 case WM_MOUSEMOVE:
3923 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
3924 !ME_FilterEvent(editor, msg, &wParam, &lParam))
3925 return 0;
3926 if (editor->bMouseCaptured)
3927 ME_MouseMove(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
3928 ME_LinkNotify(editor,msg,wParam,lParam);
3929 /* Set cursor if mouse is captured, since WM_SETCURSOR won't be received. */
3930 if (editor->bMouseCaptured)
3931 ME_SetCursor(editor);
3932 break;
3933 case WM_LBUTTONUP:
3934 if (editor->bMouseCaptured) {
3935 ReleaseCapture();
3936 editor->bMouseCaptured = FALSE;
3938 if (editor->nSelectionType == stDocument)
3939 editor->nSelectionType = stPosition;
3940 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
3941 !ME_FilterEvent(editor, msg, &wParam, &lParam))
3942 return 0;
3943 else
3945 ME_SetCursor(editor);
3946 ME_LinkNotify(editor,msg,wParam,lParam);
3948 break;
3949 case WM_RBUTTONUP:
3950 case WM_RBUTTONDOWN:
3951 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
3952 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
3953 !ME_FilterEvent(editor, msg, &wParam, &lParam))
3954 return 0;
3955 goto do_default;
3956 case WM_CONTEXTMENU:
3957 if (!ME_ShowContextMenu(editor, (short)LOWORD(lParam), (short)HIWORD(lParam)))
3958 goto do_default;
3959 break;
3960 case WM_PAINT:
3962 HDC hDC;
3963 RECT rc;
3964 PAINTSTRUCT ps;
3966 hDC = BeginPaint(editor->hWnd, &ps);
3967 /* Erase area outside of the formatting rectangle */
3968 if (ps.rcPaint.top < editor->rcFormat.top)
3970 rc = ps.rcPaint;
3971 rc.bottom = editor->rcFormat.top;
3972 FillRect(hDC, &rc, editor->hbrBackground);
3973 ps.rcPaint.top = editor->rcFormat.top;
3975 if (ps.rcPaint.bottom > editor->rcFormat.bottom) {
3976 rc = ps.rcPaint;
3977 rc.top = editor->rcFormat.bottom;
3978 FillRect(hDC, &rc, editor->hbrBackground);
3979 ps.rcPaint.bottom = editor->rcFormat.bottom;
3981 if (ps.rcPaint.left < editor->rcFormat.left) {
3982 rc = ps.rcPaint;
3983 rc.right = editor->rcFormat.left;
3984 FillRect(hDC, &rc, editor->hbrBackground);
3985 ps.rcPaint.left = editor->rcFormat.left;
3987 if (ps.rcPaint.right > editor->rcFormat.right) {
3988 rc = ps.rcPaint;
3989 rc.left = editor->rcFormat.right;
3990 FillRect(hDC, &rc, editor->hbrBackground);
3991 ps.rcPaint.right = editor->rcFormat.right;
3994 ME_PaintContent(editor, hDC, FALSE, &ps.rcPaint);
3995 EndPaint(editor->hWnd, &ps);
3997 break;
3998 case WM_SETFOCUS:
3999 editor->bHaveFocus = TRUE;
4000 ME_ShowCaret(editor);
4001 ME_SendOldNotify(editor, EN_SETFOCUS);
4002 return 0;
4003 case WM_KILLFOCUS:
4004 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
4005 editor->bHaveFocus = FALSE;
4006 ME_HideCaret(editor);
4007 ME_SendOldNotify(editor, EN_KILLFOCUS);
4008 return 0;
4009 case WM_ERASEBKGND:
4011 HDC hDC = (HDC)wParam;
4012 RECT rc;
4013 if (GetUpdateRect(editor->hWnd,&rc,TRUE))
4015 FillRect(hDC, &rc, editor->hbrBackground);
4017 return 1;
4019 case WM_COMMAND:
4020 TRACE("editor wnd command = %d\n", LOWORD(wParam));
4021 return 0;
4022 case WM_KEYUP:
4023 if ((editor->nEventMask & ENM_KEYEVENTS) &&
4024 !ME_FilterEvent(editor, msg, &wParam, &lParam))
4025 return 0;
4026 goto do_default;
4027 case WM_KEYDOWN:
4028 if ((editor->nEventMask & ENM_KEYEVENTS) &&
4029 !ME_FilterEvent(editor, msg, &wParam, &lParam))
4030 return 0;
4031 if (ME_KeyDown(editor, LOWORD(wParam)))
4032 return 0;
4033 goto do_default;
4034 case WM_CHAR:
4035 return ME_Char(editor, wParam, lParam, unicode);
4036 case WM_UNICHAR:
4037 if (unicode)
4039 if(wParam == UNICODE_NOCHAR) return TRUE;
4040 if(wParam <= 0x000fffff)
4042 if(wParam > 0xffff) /* convert to surrogates */
4044 wParam -= 0x10000;
4045 ME_Char(editor, (wParam >> 10) + 0xd800, 0, TRUE);
4046 ME_Char(editor, (wParam & 0x03ff) + 0xdc00, 0, TRUE);
4047 } else {
4048 ME_Char(editor, wParam, 0, TRUE);
4051 return 0;
4053 break;
4054 case EM_STOPGROUPTYPING:
4055 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
4056 return 0;
4057 case WM_HSCROLL:
4059 const int scrollUnit = 7;
4061 switch(LOWORD(wParam))
4063 case SB_LEFT:
4064 ME_ScrollAbs(editor, 0, 0);
4065 break;
4066 case SB_RIGHT:
4067 ME_ScrollAbs(editor,
4068 editor->horz_si.nMax - (int)editor->horz_si.nPage,
4069 editor->vert_si.nMax - (int)editor->vert_si.nPage);
4070 break;
4071 case SB_LINELEFT:
4072 ME_ScrollLeft(editor, scrollUnit);
4073 break;
4074 case SB_LINERIGHT:
4075 ME_ScrollRight(editor, scrollUnit);
4076 break;
4077 case SB_PAGELEFT:
4078 ME_ScrollLeft(editor, editor->sizeWindow.cx);
4079 break;
4080 case SB_PAGERIGHT:
4081 ME_ScrollRight(editor, editor->sizeWindow.cx);
4082 break;
4083 case SB_THUMBTRACK:
4084 case SB_THUMBPOSITION:
4086 SCROLLINFO sbi;
4087 sbi.cbSize = sizeof(sbi);
4088 sbi.fMask = SIF_TRACKPOS;
4089 /* Try to get 32-bit track position value. */
4090 if (!GetScrollInfo(editor->hWnd, SB_HORZ, &sbi))
4091 /* GetScrollInfo failed, settle for 16-bit value in wParam. */
4092 sbi.nTrackPos = HIWORD(wParam);
4094 ME_HScrollAbs(editor, sbi.nTrackPos);
4095 break;
4098 break;
4100 case EM_SCROLL: /* fall through */
4101 case WM_VSCROLL:
4103 int origNPos;
4104 int lineHeight;
4106 origNPos = editor->vert_si.nPos;
4107 lineHeight = 24;
4109 if (editor && editor->pBuffer && editor->pBuffer->pDefaultStyle)
4110 lineHeight = editor->pBuffer->pDefaultStyle->tm.tmHeight;
4111 if (lineHeight <= 0) lineHeight = 24;
4113 switch(LOWORD(wParam))
4115 case SB_TOP:
4116 ME_ScrollAbs(editor, 0, 0);
4117 break;
4118 case SB_BOTTOM:
4119 ME_ScrollAbs(editor,
4120 editor->horz_si.nMax - (int)editor->horz_si.nPage,
4121 editor->vert_si.nMax - (int)editor->vert_si.nPage);
4122 break;
4123 case SB_LINEUP:
4124 ME_ScrollUp(editor,lineHeight);
4125 break;
4126 case SB_LINEDOWN:
4127 ME_ScrollDown(editor,lineHeight);
4128 break;
4129 case SB_PAGEUP:
4130 ME_ScrollUp(editor,editor->sizeWindow.cy);
4131 break;
4132 case SB_PAGEDOWN:
4133 ME_ScrollDown(editor,editor->sizeWindow.cy);
4134 break;
4135 case SB_THUMBTRACK:
4136 case SB_THUMBPOSITION:
4138 SCROLLINFO sbi;
4139 sbi.cbSize = sizeof(sbi);
4140 sbi.fMask = SIF_TRACKPOS;
4141 /* Try to get 32-bit track position value. */
4142 if (!GetScrollInfo(editor->hWnd, SB_VERT, &sbi))
4143 /* GetScrollInfo failed, settle for 16-bit value in wParam. */
4144 sbi.nTrackPos = HIWORD(wParam);
4146 ME_VScrollAbs(editor, sbi.nTrackPos);
4147 break;
4150 if (msg == EM_SCROLL)
4151 return 0x00010000 | (((editor->vert_si.nPos - origNPos)/lineHeight) & 0xffff);
4152 break;
4154 case WM_MOUSEWHEEL:
4156 int gcWheelDelta;
4157 UINT pulScrollLines;
4158 BOOL ctrl_is_down;
4160 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
4161 !ME_FilterEvent(editor, msg, &wParam, &lParam))
4162 return 0;
4164 ctrl_is_down = GetKeyState(VK_CONTROL) & 0x8000;
4166 gcWheelDelta = GET_WHEEL_DELTA_WPARAM(wParam);
4168 if (abs(gcWheelDelta) >= WHEEL_DELTA)
4170 if (ctrl_is_down) {
4171 int numerator;
4172 if (!editor->nZoomNumerator || !editor->nZoomDenominator)
4174 numerator = 100;
4175 } else {
4176 numerator = editor->nZoomNumerator * 100 / editor->nZoomDenominator;
4178 numerator = numerator + (gcWheelDelta / WHEEL_DELTA) * 10;
4179 if (numerator >= 10 && numerator <= 500)
4180 ME_SetZoom(editor, numerator, 100);
4181 } else {
4182 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
4183 /* FIXME follow the original */
4184 if (pulScrollLines)
4185 ME_ScrollDown(editor,pulScrollLines * (-gcWheelDelta / WHEEL_DELTA) * 8);
4188 break;
4190 case EM_GETRECT:
4192 *((RECT *)lParam) = editor->rcFormat;
4193 if (editor->bDefaultFormatRect)
4194 ((RECT *)lParam)->left -= editor->selofs;
4195 return 0;
4197 case EM_SETRECT:
4198 case EM_SETRECTNP:
4200 if (lParam)
4202 DWORD exstyle = GetWindowLongW(editor->hWnd, GWL_EXSTYLE);
4203 int border = (exstyle & WS_EX_CLIENTEDGE) ? 1 : 0;
4204 RECT clientRect;
4205 RECT *rc = (RECT *)lParam;
4207 GetClientRect(editor->hWnd, &clientRect);
4208 if (wParam == 0)
4210 editor->rcFormat.top = max(0, rc->top - border);
4211 editor->rcFormat.left = max(0, rc->left - border);
4212 editor->rcFormat.bottom = min(clientRect.bottom, rc->bottom);
4213 editor->rcFormat.right = min(clientRect.right, rc->right + border);
4214 } else if (wParam == 1) {
4215 /* MSDN incorrectly says a wParam value of 1 causes the
4216 * lParam rect to be used as a relative offset,
4217 * however, the tests show it just prevents min/max bound
4218 * checking. */
4219 editor->rcFormat.top = rc->top - border;
4220 editor->rcFormat.left = rc->left - border;
4221 editor->rcFormat.bottom = rc->bottom;
4222 editor->rcFormat.right = rc->right + border;
4223 } else {
4224 return 0;
4226 editor->bDefaultFormatRect = FALSE;
4228 else
4230 ME_SetDefaultFormatRect(editor);
4231 editor->bDefaultFormatRect = TRUE;
4233 if (msg != EM_SETRECTNP)
4234 ME_RewrapRepaint(editor);
4235 return 0;
4237 case EM_REQUESTRESIZE:
4238 ME_SendRequestResize(editor, TRUE);
4239 return 0;
4240 case WM_SETREDRAW:
4241 goto do_default;
4242 case WM_SIZE:
4244 RECT clientRect;
4246 GetClientRect(editor->hWnd, &clientRect);
4247 if (editor->bDefaultFormatRect) {
4248 ME_SetDefaultFormatRect(editor);
4249 } else {
4250 editor->rcFormat.right += clientRect.right - editor->prevClientRect.right;
4251 editor->rcFormat.bottom += clientRect.bottom - editor->prevClientRect.bottom;
4253 editor->prevClientRect = clientRect;
4254 ME_RewrapRepaint(editor);
4255 goto do_default;
4257 /* IME messages to make richedit controls IME aware */
4258 case WM_IME_SETCONTEXT:
4259 case WM_IME_CONTROL:
4260 case WM_IME_SELECT:
4261 case WM_IME_COMPOSITIONFULL:
4262 return 0;
4263 case WM_IME_STARTCOMPOSITION:
4265 editor->imeStartIndex=ME_GetCursorOfs(editor,0);
4266 ME_DeleteSelection(editor);
4267 ME_CommitUndo(editor);
4268 ME_UpdateRepaint(editor);
4269 return 0;
4271 case WM_IME_COMPOSITION:
4273 HIMC hIMC;
4275 ME_Style *style = ME_GetInsertStyle(editor, 0);
4276 hIMC = ImmGetContext(editor->hWnd);
4277 ME_DeleteSelection(editor);
4278 ME_CommitUndo(editor);
4279 ME_SaveTempStyle(editor);
4280 if (lParam & GCS_RESULTSTR)
4282 LPWSTR lpCompStr = NULL;
4283 DWORD dwBufLen;
4285 dwBufLen = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
4286 lpCompStr = HeapAlloc(GetProcessHeap(),0,dwBufLen + sizeof(WCHAR));
4287 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, lpCompStr, dwBufLen);
4288 lpCompStr[dwBufLen/sizeof(WCHAR)] = 0;
4289 ME_InsertTextFromCursor(editor,0,lpCompStr,dwBufLen/sizeof(WCHAR),style);
4291 else if (lParam & GCS_COMPSTR)
4293 LPWSTR lpCompStr = NULL;
4294 DWORD dwBufLen;
4296 dwBufLen = ImmGetCompositionStringW(hIMC, GCS_COMPSTR, NULL, 0);
4297 lpCompStr = HeapAlloc(GetProcessHeap(),0,dwBufLen + sizeof(WCHAR));
4298 ImmGetCompositionStringW(hIMC, GCS_COMPSTR, lpCompStr, dwBufLen);
4299 lpCompStr[dwBufLen/sizeof(WCHAR)] = 0;
4301 ME_InsertTextFromCursor(editor,0,lpCompStr,dwBufLen/sizeof(WCHAR),style);
4302 ME_SetSelection(editor,editor->imeStartIndex,
4303 editor->imeStartIndex + dwBufLen/sizeof(WCHAR));
4305 ME_ReleaseStyle(style);
4306 ME_UpdateRepaint(editor);
4307 return 0;
4309 case WM_IME_ENDCOMPOSITION:
4311 ME_DeleteSelection(editor);
4312 editor->imeStartIndex=-1;
4313 return 0;
4315 case EM_GETOLEINTERFACE:
4317 LPVOID *ppvObj = (LPVOID*) lParam;
4318 return CreateIRichEditOle(editor, ppvObj);
4320 case EM_GETPASSWORDCHAR:
4322 return editor->cPasswordMask;
4324 case EM_SETOLECALLBACK:
4325 if(editor->lpOleCallback)
4326 IUnknown_Release(editor->lpOleCallback);
4327 editor->lpOleCallback = (LPRICHEDITOLECALLBACK)lParam;
4328 if(editor->lpOleCallback)
4329 IUnknown_AddRef(editor->lpOleCallback);
4330 return TRUE;
4331 case EM_GETWORDBREAKPROC:
4332 return (LRESULT)editor->pfnWordBreak;
4333 case EM_SETWORDBREAKPROC:
4335 EDITWORDBREAKPROCW pfnOld = editor->pfnWordBreak;
4337 editor->pfnWordBreak = (EDITWORDBREAKPROCW)lParam;
4338 return (LRESULT)pfnOld;
4340 case EM_SETTEXTMODE:
4342 LRESULT ret;
4343 int mask = 0;
4344 int changes = 0;
4345 GETTEXTLENGTHEX how;
4347 /* CR/LF conversion required in 2.0 mode, verbatim in 1.0 mode */
4348 how.flags = GTL_CLOSE | (editor->bEmulateVersion10 ? 0 : GTL_USECRLF) | GTL_NUMCHARS;
4349 how.codepage = unicode ? 1200 : CP_ACP;
4350 ret = ME_GetTextLengthEx(editor, &how);
4351 if (!ret)
4353 /*Check for valid wParam*/
4354 if ((((wParam & TM_RICHTEXT) && ((wParam & TM_PLAINTEXT) << 1))) ||
4355 (((wParam & TM_MULTILEVELUNDO) && ((wParam & TM_SINGLELEVELUNDO) << 1))) ||
4356 (((wParam & TM_MULTICODEPAGE) && ((wParam & TM_SINGLECODEPAGE) << 1))))
4357 return 1;
4358 else
4360 if (wParam & (TM_RICHTEXT | TM_PLAINTEXT))
4362 mask |= (TM_RICHTEXT | TM_PLAINTEXT);
4363 changes |= (wParam & (TM_RICHTEXT | TM_PLAINTEXT));
4365 /*FIXME: Currently no support for undo level and code page options*/
4366 editor->mode = (editor->mode & (~mask)) | changes;
4367 return 0;
4370 return ret;
4372 case EM_SETPASSWORDCHAR:
4374 editor->cPasswordMask = wParam;
4375 ME_RewrapRepaint(editor);
4376 return 0;
4378 case EM_SETTARGETDEVICE:
4379 if (wParam == 0)
4381 BOOL new = (lParam == 0);
4382 if (editor->bWordWrap != new)
4384 editor->bWordWrap = new;
4385 ME_RewrapRepaint(editor);
4388 else FIXME("Unsupported yet non NULL device in EM_SETTARGETDEVICE\n");
4389 break;
4390 default:
4391 do_default:
4392 *phresult = S_FALSE;
4393 break;
4395 return 0L;
4398 static LRESULT RichEditWndProc_common(HWND hWnd, UINT msg, WPARAM wParam,
4399 LPARAM lParam, BOOL unicode)
4401 ME_TextEditor *editor;
4402 HRESULT hresult;
4403 LRESULT lresult;
4405 TRACE("enter hwnd %p msg %04x (%s) %lx %lx, unicode %d\n",
4406 hWnd, msg, get_msg_name(msg), wParam, lParam, unicode);
4408 editor = (ME_TextEditor *)GetWindowLongPtrW(hWnd, 0);
4409 if (!editor)
4411 if (msg == WM_NCCREATE)
4413 CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
4414 TRACE("WM_NCCREATE: hWnd %p style 0x%08x\n", hWnd, pcs->style);
4415 editor = ME_MakeEditor(hWnd, FALSE);
4416 SetWindowLongPtrW(hWnd, 0, (LONG_PTR)editor);
4417 return TRUE;
4419 else if (msg != WM_NCDESTROY)
4421 ERR("called with invalid hWnd %p - application bug?\n", hWnd);
4422 return 0;
4426 lresult = ME_HandleMessage(editor, msg, wParam, lParam, unicode, &hresult);
4428 if (hresult == S_FALSE)
4429 lresult = DefWindowProcW(hWnd, msg, wParam, lParam);
4431 TRACE("exit hwnd %p msg %04x (%s) %lx %lx, unicode %d -> %lu\n",
4432 hWnd, msg, get_msg_name(msg), wParam, lParam, unicode, lresult);
4434 return lresult;
4437 static LRESULT WINAPI RichEditWndProcW(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
4439 BOOL unicode = TRUE;
4441 /* Under Win9x RichEdit20W returns ANSI strings, see the tests. */
4442 if (msg == WM_GETTEXT && (GetVersion() & 0x80000000))
4443 unicode = FALSE;
4445 return RichEditWndProc_common(hWnd, msg, wParam, lParam, unicode);
4448 static LRESULT WINAPI RichEditWndProcA(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
4450 return RichEditWndProc_common(hWnd, msg, wParam, lParam, FALSE);
4453 /******************************************************************
4454 * RichEditANSIWndProc (RICHED20.10)
4456 LRESULT WINAPI RichEditANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
4458 return RichEditWndProcA(hWnd, msg, wParam, lParam);
4461 /******************************************************************
4462 * RichEdit10ANSIWndProc (RICHED20.9)
4464 LRESULT WINAPI RichEdit10ANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
4466 if (msg == WM_NCCREATE && !GetWindowLongPtrW(hWnd, 0))
4468 ME_TextEditor *editor;
4469 CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
4471 TRACE("WM_NCCREATE: hWnd %p style 0x%08x\n", hWnd, pcs->style);
4472 editor = ME_MakeEditor(hWnd, TRUE);
4473 SetWindowLongPtrW(hWnd, 0, (LONG_PTR)editor);
4474 return TRUE;
4476 return RichEditANSIWndProc(hWnd, msg, wParam, lParam);
4479 void ME_SendOldNotify(ME_TextEditor *editor, int nCode)
4481 HWND hWnd = editor->hWnd;
4482 SendMessageA(GetParent(hWnd), WM_COMMAND, MAKEWPARAM(GetWindowLongW(hWnd, GWLP_ID), nCode), (LPARAM)hWnd);
4485 int ME_CountParagraphsBetween(ME_TextEditor *editor, int from, int to)
4487 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
4488 int i = 0;
4490 while(item && item->member.para.next_para->member.para.nCharOfs <= from)
4491 item = item->member.para.next_para;
4492 if (!item)
4493 return 0;
4494 while(item && item->member.para.next_para->member.para.nCharOfs <= to) {
4495 item = item->member.para.next_para;
4496 i++;
4498 return i;
4502 int ME_GetTextW(ME_TextEditor *editor, WCHAR *buffer, int nStart, int nChars, int bCRLF)
4504 ME_DisplayItem *item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
4505 int nWritten = 0;
4506 WCHAR *pStart = buffer;
4508 if (!item) {
4509 *buffer = 0;
4510 return 0;
4513 /* bCRLF flag is only honored in 2.0 and up. 1.0 must always return text verbatim */
4514 if (editor->bEmulateVersion10) bCRLF = 0;
4516 if (nStart)
4518 int nLen = ME_StrLen(item->member.run.strText) - nStart;
4519 if (nLen > nChars)
4520 nLen = nChars;
4521 CopyMemory(buffer, item->member.run.strText->szData + nStart, sizeof(WCHAR)*nLen);
4522 nChars -= nLen;
4523 nWritten += nLen;
4524 buffer += nLen;
4525 if (!nChars) {
4526 *buffer = 0;
4527 return nWritten;
4529 nStart = 0;
4530 item = ME_FindItemFwd(item, diRun);
4533 while(nChars && item)
4535 int nLen = ME_StrLen(item->member.run.strText);
4536 if (item->member.run.nFlags & MERF_ENDPARA)
4537 nLen = item->member.run.nCR + item->member.run.nLF;
4538 if (nLen > nChars)
4539 nLen = nChars;
4541 if (item->member.run.nFlags & MERF_ENDCELL &&
4542 item->member.run.nFlags & MERF_ENDPARA)
4544 *buffer = '\t';
4546 else if (item->member.run.nFlags & MERF_ENDPARA)
4548 if (!ME_FindItemFwd(item, diRun))
4549 /* No '\r' is appended to the last paragraph. */
4550 nLen = 0;
4551 else if (bCRLF && nChars == 1) {
4552 nLen = 0;
4553 nChars = 0;
4554 } else {
4555 if (bCRLF)
4557 /* richedit 2.0 case - actual line-break is \r but should report \r\n */
4558 if (ME_GetParagraph(item)->member.para.nFlags & (MEPF_ROWSTART|MEPF_ROWEND))
4559 assert(nLen == 2);
4560 else
4561 assert(nLen == 1);
4562 *buffer++ = '\r';
4563 *buffer = '\n'; /* Later updated by nLen==1 at the end of the loop */
4564 if (nLen == 1)
4565 nWritten++;
4566 else
4567 buffer--;
4569 else
4571 int i, j;
4573 /* richedit 2.0 verbatim has only \r. richedit 1.0 should honor encodings */
4574 i = 0;
4575 while (nChars - i > 0 && i < item->member.run.nCR)
4577 buffer[i] = '\r'; i++;
4579 j = 0;
4580 while (nChars - i - j > 0 && j < item->member.run.nLF)
4582 buffer[i+j] = '\n'; j++;
4587 else
4588 CopyMemory(buffer, item->member.run.strText->szData, sizeof(WCHAR)*nLen);
4589 nChars -= nLen;
4590 nWritten += nLen;
4591 buffer += nLen;
4593 if (!nChars)
4595 TRACE("nWritten=%d, actual=%d\n", nWritten, buffer-pStart);
4596 *buffer = 0;
4597 return nWritten;
4599 item = ME_FindItemFwd(item, diRun);
4601 *buffer = 0;
4602 TRACE("nWritten=%d, actual=%d\n", nWritten, buffer-pStart);
4603 return nWritten;
4606 static BOOL ME_RegisterEditorClass(HINSTANCE hInstance)
4608 WNDCLASSW wcW;
4609 WNDCLASSA wcA;
4611 wcW.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
4612 wcW.lpfnWndProc = RichEditWndProcW;
4613 wcW.cbClsExtra = 0;
4614 wcW.cbWndExtra = sizeof(ME_TextEditor *);
4615 wcW.hInstance = NULL; /* hInstance would register DLL-local class */
4616 wcW.hIcon = NULL;
4617 wcW.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
4618 wcW.hbrBackground = GetStockObject(NULL_BRUSH);
4619 wcW.lpszMenuName = NULL;
4621 if (is_version_nt())
4623 wcW.lpszClassName = RichEdit20W;
4624 if (!RegisterClassW(&wcW)) return FALSE;
4625 wcW.lpszClassName = RichEdit50W;
4626 if (!RegisterClassW(&wcW)) return FALSE;
4628 else
4630 /* WNDCLASSA/W have the same layout */
4631 wcW.lpszClassName = (LPCWSTR)"RichEdit20W";
4632 if (!RegisterClassA((WNDCLASSA *)&wcW)) return FALSE;
4633 wcW.lpszClassName = (LPCWSTR)"RichEdit50W";
4634 if (!RegisterClassA((WNDCLASSA *)&wcW)) return FALSE;
4637 wcA.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
4638 wcA.lpfnWndProc = RichEditWndProcA;
4639 wcA.cbClsExtra = 0;
4640 wcA.cbWndExtra = sizeof(ME_TextEditor *);
4641 wcA.hInstance = NULL; /* hInstance would register DLL-local class */
4642 wcA.hIcon = NULL;
4643 wcA.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
4644 wcA.hbrBackground = GetStockObject(NULL_BRUSH);
4645 wcA.lpszMenuName = NULL;
4646 wcA.lpszClassName = "RichEdit20A";
4647 if (!RegisterClassA(&wcA)) return FALSE;
4648 wcA.lpszClassName = "RichEdit50A";
4649 if (!RegisterClassA(&wcA)) return FALSE;
4651 return TRUE;
4654 LRESULT WINAPI REComboWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
4655 /* FIXME: Not implemented */
4656 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
4657 hWnd, msg, get_msg_name(msg), wParam, lParam);
4658 return DefWindowProcW(hWnd, msg, wParam, lParam);
4661 LRESULT WINAPI REListWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
4662 /* FIXME: Not implemented */
4663 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
4664 hWnd, msg, get_msg_name(msg), wParam, lParam);
4665 return DefWindowProcW(hWnd, msg, wParam, lParam);
4668 /******************************************************************
4669 * REExtendedRegisterClass (RICHED20.8)
4671 * FIXME undocumented
4672 * Need to check for errors and implement controls and callbacks
4674 LRESULT WINAPI REExtendedRegisterClass(void)
4676 WNDCLASSW wcW;
4677 UINT result;
4679 FIXME("semi stub\n");
4681 wcW.cbClsExtra = 0;
4682 wcW.cbWndExtra = 4;
4683 wcW.hInstance = NULL;
4684 wcW.hIcon = NULL;
4685 wcW.hCursor = NULL;
4686 wcW.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
4687 wcW.lpszMenuName = NULL;
4689 if (!ME_ListBoxRegistered)
4691 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS;
4692 wcW.lpfnWndProc = REListWndProc;
4693 wcW.lpszClassName = REListBox20W;
4694 if (RegisterClassW(&wcW)) ME_ListBoxRegistered = TRUE;
4697 if (!ME_ComboBoxRegistered)
4699 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
4700 wcW.lpfnWndProc = REComboWndProc;
4701 wcW.lpszClassName = REComboBox20W;
4702 if (RegisterClassW(&wcW)) ME_ComboBoxRegistered = TRUE;
4705 result = 0;
4706 if (ME_ListBoxRegistered)
4707 result += 1;
4708 if (ME_ComboBoxRegistered)
4709 result += 2;
4711 return result;
4714 static BOOL isurlspecial(WCHAR c)
4716 static const WCHAR special_chars[] = {'.','/','%','@','*','|','\\','+','#',0};
4717 return strchrW( special_chars, c ) != NULL;
4721 * This proc takes a selection, and scans it forward in order to select the span
4722 * of a possible URL candidate. A possible URL candidate must start with isalnum
4723 * or one of the following special characters: *|/\+%#@ and must consist entirely
4724 * of the characters allowed to start the URL, plus : (colon) which may occur
4725 * at most once, and not at either end.
4727 * sel_max == -1 indicates scan to end of text.
4729 static BOOL ME_FindNextURLCandidate(ME_TextEditor *editor, int sel_min, int sel_max,
4730 int * candidate_min, int * candidate_max)
4732 ME_DisplayItem * item;
4733 ME_DisplayItem * para;
4734 int nStart;
4735 BOOL foundColon = FALSE;
4736 WCHAR lastAcceptedChar = '\0';
4738 TRACE("sel_min = %d sel_max = %d\n", sel_min, sel_max);
4740 *candidate_min = *candidate_max = -1;
4741 item = ME_FindItemAtOffset(editor, diRun, sel_min, &nStart);
4742 if (!item) return FALSE;
4743 TRACE("nStart = %d\n", nStart);
4744 para = ME_GetParagraph(item);
4745 if (sel_max == -1) sel_max = ME_GetTextLength(editor);
4746 while (item && para->member.para.nCharOfs + item->member.run.nCharOfs + nStart < sel_max)
4748 ME_DisplayItem * next_item;
4750 if (!(item->member.run.nFlags & MERF_ENDPARA)) {
4751 /* Find start of candidate */
4752 if (*candidate_min == -1) {
4753 while (nStart < ME_StrLen(item->member.run.strText) &&
4754 !(isalnumW(item->member.run.strText->szData[nStart]) ||
4755 isurlspecial(item->member.run.strText->szData[nStart]))) {
4756 nStart++;
4758 if (nStart < ME_StrLen(item->member.run.strText) &&
4759 (isalnumW(item->member.run.strText->szData[nStart]) ||
4760 isurlspecial(item->member.run.strText->szData[nStart]))) {
4761 *candidate_min = para->member.para.nCharOfs + item->member.run.nCharOfs + nStart;
4762 lastAcceptedChar = item->member.run.strText->szData[nStart];
4763 nStart++;
4767 /* Find end of candidate */
4768 if (*candidate_min >= 0) {
4769 while (nStart < ME_StrLen(item->member.run.strText) &&
4770 (isalnumW(item->member.run.strText->szData[nStart]) ||
4771 isurlspecial(item->member.run.strText->szData[nStart]) ||
4772 (!foundColon && item->member.run.strText->szData[nStart] == ':') )) {
4773 if (item->member.run.strText->szData[nStart] == ':') foundColon = TRUE;
4774 lastAcceptedChar = item->member.run.strText->szData[nStart];
4775 nStart++;
4777 if (nStart < ME_StrLen(item->member.run.strText) &&
4778 !(isalnumW(item->member.run.strText->szData[nStart]) ||
4779 isurlspecial(item->member.run.strText->szData[nStart]) )) {
4780 *candidate_max = para->member.para.nCharOfs + item->member.run.nCharOfs + nStart;
4781 nStart++;
4782 if (lastAcceptedChar == ':') (*candidate_max)--;
4783 return TRUE;
4786 } else {
4787 /* End of paragraph: skip it if before candidate span, or terminates
4788 current active span */
4789 if (*candidate_min >= 0) {
4790 *candidate_max = para->member.para.nCharOfs + item->member.run.nCharOfs;
4791 if (lastAcceptedChar == ':') (*candidate_max)--;
4792 return TRUE;
4796 /* Reaching this point means no span was found, so get next span */
4797 next_item = ME_FindItemFwd(item, diRun);
4798 if (!next_item) {
4799 if (*candidate_min >= 0) {
4800 /* There are no further runs, so take end of text as end of candidate */
4801 *candidate_max = para->member.para.nCharOfs + item->member.run.nCharOfs + nStart;
4802 if (lastAcceptedChar == ':') (*candidate_max)--;
4803 return TRUE;
4806 item = next_item;
4807 para = ME_GetParagraph(item);
4808 nStart = 0;
4811 if (item) {
4812 if (*candidate_min >= 0) {
4813 /* There are no further runs, so take end of text as end of candidate */
4814 *candidate_max = para->member.para.nCharOfs + item->member.run.nCharOfs + nStart;
4815 if (lastAcceptedChar == ':') (*candidate_max)--;
4816 return TRUE;
4819 return FALSE;
4823 * This proc evaluates the selection and returns TRUE if it can be considered an URL
4825 static BOOL ME_IsCandidateAnURL(ME_TextEditor *editor, int sel_min, int sel_max)
4827 struct prefix_s {
4828 const char *text;
4829 int length;
4830 } prefixes[12] = {
4831 /* Code below depends on these being in decreasing length order! */
4832 {"prospero:", 10},
4833 {"telnet:", 8},
4834 {"gopher:", 8},
4835 {"mailto:", 8},
4836 {"https:", 7},
4837 {"file:", 6},
4838 {"news:", 6},
4839 {"wais:", 6},
4840 {"nntp:", 6},
4841 {"http:", 5},
4842 {"www.", 5},
4843 {"ftp:", 5},
4845 LPWSTR bufferW = NULL;
4846 WCHAR bufW[32];
4847 unsigned int i;
4849 if (sel_max == -1) sel_max = ME_GetTextLength(editor);
4850 assert(sel_min <= sel_max);
4851 for (i = 0; i < sizeof(prefixes) / sizeof(struct prefix_s); i++)
4853 if (sel_max - sel_min < prefixes[i].length) continue;
4854 if (bufferW == NULL) {
4855 bufferW = heap_alloc((sel_max - sel_min + 1) * sizeof(WCHAR));
4857 ME_GetTextW(editor, bufferW, sel_min, min(sel_max - sel_min, lstrlenA(prefixes[i].text)), 0);
4858 MultiByteToWideChar(CP_ACP, 0, prefixes[i].text, -1, bufW, 32);
4859 if (!lstrcmpW(bufW, bufferW))
4861 heap_free(bufferW);
4862 return TRUE;
4865 heap_free(bufferW);
4866 return FALSE;
4870 * This proc walks through the indicated selection and evaluates whether each
4871 * section identified by ME_FindNextURLCandidate and in-between sections have
4872 * their proper CFE_LINK attributes set or unset. If the CFE_LINK attribute is
4873 * not what it is supposed to be, this proc sets or unsets it as appropriate.
4875 * Returns TRUE if at least one section was modified.
4877 static BOOL ME_UpdateLinkAttribute(ME_TextEditor *editor, int sel_min, int sel_max)
4879 BOOL modified = FALSE;
4880 int cMin, cMax;
4882 if (sel_max == -1) sel_max = ME_GetTextLength(editor);
4885 int beforeURL[2];
4886 int inURL[2];
4887 CHARFORMAT2W link;
4889 if (ME_FindNextURLCandidate(editor, sel_min, sel_max, &cMin, &cMax))
4891 /* Section before candidate is not an URL */
4892 beforeURL[0] = sel_min;
4893 beforeURL[1] = cMin;
4895 if (ME_IsCandidateAnURL(editor, cMin, cMax))
4897 inURL[0] = cMin; inURL[1] = cMax;
4899 else
4901 beforeURL[1] = cMax;
4902 inURL[0] = inURL[1] = -1;
4904 sel_min = cMax;
4906 else
4908 /* No more candidates until end of selection */
4909 beforeURL[0] = sel_min;
4910 beforeURL[1] = sel_max;
4911 inURL[0] = inURL[1] = -1;
4912 sel_min = sel_max;
4915 if (beforeURL[0] < beforeURL[1])
4917 /* CFE_LINK effect should be consistently unset */
4918 link.cbSize = sizeof(link);
4919 ME_GetCharFormat(editor, beforeURL[0], beforeURL[1], &link);
4920 if (!(link.dwMask & CFM_LINK) || (link.dwEffects & CFE_LINK))
4922 /* CFE_LINK must be unset from this range */
4923 memset(&link, 0, sizeof(CHARFORMAT2W));
4924 link.cbSize = sizeof(link);
4925 link.dwMask = CFM_LINK;
4926 link.dwEffects = 0;
4927 ME_SetCharFormat(editor, beforeURL[0], beforeURL[1] - beforeURL[0], &link);
4928 modified = TRUE;
4931 if (inURL[0] < inURL[1])
4933 /* CFE_LINK effect should be consistently set */
4934 link.cbSize = sizeof(link);
4935 ME_GetCharFormat(editor, inURL[0], inURL[1], &link);
4936 if (!(link.dwMask & CFM_LINK) || !(link.dwEffects & CFE_LINK))
4938 /* CFE_LINK must be set on this range */
4939 memset(&link, 0, sizeof(CHARFORMAT2W));
4940 link.cbSize = sizeof(link);
4941 link.dwMask = CFM_LINK;
4942 link.dwEffects = CFE_LINK;
4943 ME_SetCharFormat(editor, inURL[0], inURL[1] - inURL[0], &link);
4944 modified = TRUE;
4947 } while (sel_min < sel_max);
4948 return modified;