msvcrt/tests: Remove a space before a '\n'.
[wine/gsoc-2012-control.git] / dlls / riched20 / editor.c
blob574118aede427fa719746dba8d1b8571a9d39ef9
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 editor==%p format==0x%X\n", stream, editor, 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 ITextHost_TxShowCaret(editor->texthost, FALSE);
1584 ME_MoveCaret(editor);
1585 ITextHost_TxShowCaret(editor->texthost, TRUE);
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 static int
1628 ME_FindText(ME_TextEditor *editor, DWORD flags, const CHARRANGE *chrg, const WCHAR *text, CHARRANGE *chrgText)
1630 const int nLen = lstrlenW(text);
1631 const int nTextLen = ME_GetTextLength(editor);
1632 int nStart, nEnd;
1633 int nMin, nMax;
1634 ME_DisplayItem *item;
1635 ME_DisplayItem *para;
1636 WCHAR wLastChar = ' ';
1638 TRACE("flags==0x%08x, chrg->cpMin==%d, chrg->cpMax==%d text==%s\n",
1639 flags, chrg->cpMin, chrg->cpMax, debugstr_w(text));
1641 if (flags & ~(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD))
1642 FIXME("Flags 0x%08x not implemented\n",
1643 flags & ~(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD));
1645 nMin = chrg->cpMin;
1646 if (chrg->cpMax == -1)
1647 nMax = nTextLen;
1648 else
1649 nMax = chrg->cpMax > nTextLen ? nTextLen : chrg->cpMax;
1651 /* In 1.0 emulation, if cpMax reaches end of text, add the FR_DOWN flag */
1652 if (editor->bEmulateVersion10 && nMax == nTextLen)
1654 flags |= FR_DOWN;
1657 /* In 1.0 emulation, cpMin must always be no greater than cpMax */
1658 if (editor->bEmulateVersion10 && nMax < nMin)
1660 if (chrgText)
1662 chrgText->cpMin = -1;
1663 chrgText->cpMax = -1;
1665 return -1;
1668 /* when searching up, if cpMin < cpMax, then instead of searching
1669 * on [cpMin,cpMax], we search on [0,cpMin], otherwise, search on
1670 * [cpMax, cpMin]. The exception is when cpMax is -1, in which
1671 * case, it is always bigger than cpMin.
1673 if (!editor->bEmulateVersion10 && !(flags & FR_DOWN))
1675 int nSwap = nMax;
1677 nMax = nMin > nTextLen ? nTextLen : nMin;
1678 if (nMin < nSwap || chrg->cpMax == -1)
1679 nMin = 0;
1680 else
1681 nMin = nSwap;
1684 if (!nLen || nMin < 0 || nMax < 0 || nMax < nMin)
1686 if (chrgText)
1687 chrgText->cpMin = chrgText->cpMax = -1;
1688 return -1;
1691 if (flags & FR_DOWN) /* Forward search */
1693 /* If possible, find the character before where the search starts */
1694 if ((flags & FR_WHOLEWORD) && nMin)
1696 nStart = nMin - 1;
1697 ME_RunOfsFromCharOfs(editor, nStart, &item, &nStart);
1698 if (!item)
1700 if (chrgText)
1701 chrgText->cpMin = chrgText->cpMax = -1;
1702 return -1;
1704 wLastChar = item->member.run.strText->szData[nStart];
1707 nStart = nMin;
1708 ME_RunOfsFromCharOfs(editor, nStart, &item, &nStart);
1709 if (!item)
1711 if (chrgText)
1712 chrgText->cpMin = chrgText->cpMax = -1;
1713 return -1;
1716 para = ME_GetParagraph(item);
1717 while (item
1718 && para->member.para.nCharOfs + item->member.run.nCharOfs + nStart + nLen <= nMax)
1720 ME_DisplayItem *pCurItem = item;
1721 int nCurStart = nStart;
1722 int nMatched = 0;
1724 while (pCurItem && ME_CharCompare(pCurItem->member.run.strText->szData[nCurStart + nMatched], text[nMatched], (flags & FR_MATCHCASE)))
1726 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
1727 break;
1729 nMatched++;
1730 if (nMatched == nLen)
1732 ME_DisplayItem *pNextItem = pCurItem;
1733 int nNextStart = nCurStart;
1734 WCHAR wNextChar;
1736 /* Check to see if next character is a whitespace */
1737 if (flags & FR_WHOLEWORD)
1739 if (nCurStart + nMatched == ME_StrLen(pCurItem->member.run.strText))
1741 pNextItem = ME_FindItemFwd(pCurItem, diRun);
1742 nNextStart = -nMatched;
1745 if (pNextItem)
1746 wNextChar = pNextItem->member.run.strText->szData[nNextStart + nMatched];
1747 else
1748 wNextChar = ' ';
1750 if (isalnumW(wNextChar))
1751 break;
1754 nStart += para->member.para.nCharOfs + item->member.run.nCharOfs;
1755 if (chrgText)
1757 chrgText->cpMin = nStart;
1758 chrgText->cpMax = nStart + nLen;
1760 TRACE("found at %d-%d\n", nStart, nStart + nLen);
1761 return nStart;
1763 if (nCurStart + nMatched == ME_StrLen(pCurItem->member.run.strText))
1765 pCurItem = ME_FindItemFwd(pCurItem, diRun);
1766 para = ME_GetParagraph(pCurItem);
1767 nCurStart = -nMatched;
1770 if (pCurItem)
1771 wLastChar = pCurItem->member.run.strText->szData[nCurStart + nMatched];
1772 else
1773 wLastChar = ' ';
1775 nStart++;
1776 if (nStart == ME_StrLen(item->member.run.strText))
1778 item = ME_FindItemFwd(item, diRun);
1779 para = ME_GetParagraph(item);
1780 nStart = 0;
1784 else /* Backward search */
1786 /* If possible, find the character after where the search ends */
1787 if ((flags & FR_WHOLEWORD) && nMax < nTextLen - 1)
1789 nEnd = nMax + 1;
1790 ME_RunOfsFromCharOfs(editor, nEnd, &item, &nEnd);
1791 if (!item)
1793 if (chrgText)
1794 chrgText->cpMin = chrgText->cpMax = -1;
1795 return -1;
1797 wLastChar = item->member.run.strText->szData[nEnd];
1800 nEnd = nMax;
1801 ME_RunOfsFromCharOfs(editor, nEnd, &item, &nEnd);
1802 if (!item)
1804 if (chrgText)
1805 chrgText->cpMin = chrgText->cpMax = -1;
1806 return -1;
1809 para = ME_GetParagraph(item);
1811 while (item
1812 && para->member.para.nCharOfs + item->member.run.nCharOfs + nEnd - nLen >= nMin)
1814 ME_DisplayItem *pCurItem = item;
1815 int nCurEnd = nEnd;
1816 int nMatched = 0;
1818 if (nCurEnd - nMatched == 0)
1820 pCurItem = ME_FindItemBack(pCurItem, diRun);
1821 para = ME_GetParagraph(pCurItem);
1822 nCurEnd = ME_StrLen(pCurItem->member.run.strText) + nMatched;
1825 while (pCurItem && ME_CharCompare(pCurItem->member.run.strText->szData[nCurEnd - nMatched - 1], text[nLen - nMatched - 1], (flags & FR_MATCHCASE)))
1827 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
1828 break;
1830 nMatched++;
1831 if (nMatched == nLen)
1833 ME_DisplayItem *pPrevItem = pCurItem;
1834 int nPrevEnd = nCurEnd;
1835 WCHAR wPrevChar;
1837 /* Check to see if previous character is a whitespace */
1838 if (flags & FR_WHOLEWORD)
1840 if (nPrevEnd - nMatched == 0)
1842 pPrevItem = ME_FindItemBack(pCurItem, diRun);
1843 if (pPrevItem)
1844 nPrevEnd = ME_StrLen(pPrevItem->member.run.strText) + nMatched;
1847 if (pPrevItem)
1848 wPrevChar = pPrevItem->member.run.strText->szData[nPrevEnd - nMatched - 1];
1849 else
1850 wPrevChar = ' ';
1852 if (isalnumW(wPrevChar))
1853 break;
1856 nStart = para->member.para.nCharOfs + pCurItem->member.run.nCharOfs + nCurEnd - nMatched;
1857 if (chrgText)
1859 chrgText->cpMin = nStart;
1860 chrgText->cpMax = nStart + nLen;
1862 TRACE("found at %d-%d\n", nStart, nStart + nLen);
1863 return nStart;
1865 if (nCurEnd - nMatched == 0)
1867 pCurItem = ME_FindItemBack(pCurItem, diRun);
1868 /* Don't care about pCurItem becoming NULL here; it's already taken
1869 * care of in the exterior loop condition */
1870 para = ME_GetParagraph(pCurItem);
1871 nCurEnd = ME_StrLen(pCurItem->member.run.strText) + nMatched;
1874 if (pCurItem)
1875 wLastChar = pCurItem->member.run.strText->szData[nCurEnd - nMatched - 1];
1876 else
1877 wLastChar = ' ';
1879 nEnd--;
1880 if (nEnd < 0)
1882 item = ME_FindItemBack(item, diRun);
1883 para = ME_GetParagraph(item);
1884 nEnd = ME_StrLen(item->member.run.strText);
1888 TRACE("not found\n");
1889 if (chrgText)
1890 chrgText->cpMin = chrgText->cpMax = -1;
1891 return -1;
1894 static int ME_GetTextEx(ME_TextEditor *editor, GETTEXTEX *ex, LPARAM pText)
1896 int nStart, nCount; /* in chars */
1898 if (ex->flags & ~(GT_SELECTION | GT_USECRLF))
1899 FIXME("GETTEXTEX flags 0x%08x not supported\n", ex->flags & ~(GT_SELECTION | GT_USECRLF));
1901 if (ex->flags & GT_SELECTION)
1903 ME_GetSelection(editor, &nStart, &nCount);
1904 nCount -= nStart;
1906 else
1908 nStart = 0;
1909 nCount = 0x7fffffff;
1911 if (ex->codepage == 1200)
1913 nCount = min(nCount, ex->cb / sizeof(WCHAR) - 1);
1914 return ME_GetTextW(editor, (LPWSTR)pText, nStart, nCount, ex->flags & GT_USECRLF);
1916 else
1918 /* potentially each char may be a CR, why calculate the exact value with O(N) when
1919 we can just take a bigger buffer? :)
1920 The above assumption still holds with CR/LF counters, since CR->CRLF expansion
1921 occurs only in richedit 2.0 mode, in which line breaks have only one CR
1923 int crlfmul = (ex->flags & GT_USECRLF) ? 2 : 1;
1924 LPWSTR buffer;
1925 DWORD buflen = ex->cb;
1926 LRESULT rc;
1927 DWORD flags = 0;
1929 nCount = min(nCount, ex->cb - 1);
1930 buffer = heap_alloc((crlfmul*nCount + 1) * sizeof(WCHAR));
1932 buflen = ME_GetTextW(editor, buffer, nStart, nCount, ex->flags & GT_USECRLF);
1933 rc = WideCharToMultiByte(ex->codepage, flags, buffer, buflen+1,
1934 (LPSTR)pText, ex->cb, ex->lpDefaultChar, ex->lpUsedDefChar);
1935 if (rc) rc--; /* do not count 0 terminator */
1937 heap_free(buffer);
1938 return rc;
1942 static int ME_GetTextRange(ME_TextEditor *editor, TEXTRANGEW *rng, BOOL unicode)
1944 if (unicode)
1945 return ME_GetTextW(editor, rng->lpstrText, rng->chrg.cpMin,
1946 rng->chrg.cpMax-rng->chrg.cpMin, 0);
1947 else
1949 int nLen = rng->chrg.cpMax-rng->chrg.cpMin;
1950 WCHAR *p = ALLOC_N_OBJ(WCHAR, nLen+1);
1951 int nChars = ME_GetTextW(editor, p, rng->chrg.cpMin, nLen, 0);
1952 /* FIXME this is a potential security hole (buffer overrun)
1953 if you know more about wchar->mbyte conversion please explain
1955 WideCharToMultiByte(CP_ACP, 0, p, nChars+1, (char *)rng->lpstrText, nLen+1, NULL, NULL);
1956 FREE_OBJ(p);
1957 return nChars;
1961 typedef struct tagME_GlobalDestStruct
1963 HGLOBAL hData;
1964 int nLength;
1965 } ME_GlobalDestStruct;
1967 static DWORD CALLBACK ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1969 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
1970 int i;
1971 WORD *pSrc, *pDest;
1973 cb = cb >> 1;
1974 pDest = (WORD *)lpBuff;
1975 pSrc = GlobalLock(pData->hData);
1976 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
1977 pDest[i] = pSrc[pData->nLength+i];
1979 pData->nLength += i;
1980 *pcb = 2*i;
1981 GlobalUnlock(pData->hData);
1982 return 0;
1985 static DWORD CALLBACK ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1987 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
1988 int i;
1989 BYTE *pSrc, *pDest;
1991 pDest = lpBuff;
1992 pSrc = GlobalLock(pData->hData);
1993 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
1994 pDest[i] = pSrc[pData->nLength+i];
1996 pData->nLength += i;
1997 *pcb = i;
1998 GlobalUnlock(pData->hData);
1999 return 0;
2002 static BOOL ME_Paste(ME_TextEditor *editor)
2004 DWORD dwFormat = 0;
2005 EDITSTREAM es;
2006 ME_GlobalDestStruct gds;
2007 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
2008 UINT cf = 0;
2010 if (IsClipboardFormatAvailable(nRTFFormat))
2011 cf = nRTFFormat, dwFormat = SF_RTF;
2012 else if (IsClipboardFormatAvailable(CF_UNICODETEXT))
2013 cf = CF_UNICODETEXT, dwFormat = SF_TEXT|SF_UNICODE;
2014 else
2015 return FALSE;
2017 if (!OpenClipboard(editor->hWnd))
2018 return FALSE;
2019 gds.hData = GetClipboardData(cf);
2020 gds.nLength = 0;
2021 es.dwCookie = (DWORD_PTR)&gds;
2022 es.pfnCallback = dwFormat == SF_RTF ? ME_ReadFromHGLOBALRTF : ME_ReadFromHGLOBALUnicode;
2023 ME_StreamIn(editor, dwFormat|SFF_SELECTION, &es, FALSE);
2025 CloseClipboard();
2026 return TRUE;
2029 static BOOL ME_Copy(ME_TextEditor *editor, CHARRANGE *range)
2031 LPDATAOBJECT dataObj = NULL;
2032 HRESULT hr = S_OK;
2034 if (editor->cPasswordMask)
2035 return FALSE; /* Copying or Cutting masked text isn't allowed */
2037 if(editor->lpOleCallback)
2038 hr = IRichEditOleCallback_GetClipboardData(editor->lpOleCallback, range, RECO_COPY, &dataObj);
2039 if(FAILED(hr) || !dataObj)
2040 hr = ME_GetDataObject(editor, range, &dataObj);
2041 if(SUCCEEDED(hr)) {
2042 hr = OleSetClipboard(dataObj);
2043 IDataObject_Release(dataObj);
2045 return SUCCEEDED(hr) != 0;
2048 /* helper to send a msg filter notification */
2049 static BOOL
2050 ME_FilterEvent(ME_TextEditor *editor, UINT msg, WPARAM* wParam, LPARAM* lParam)
2052 MSGFILTER msgf;
2054 msgf.nmhdr.code = EN_MSGFILTER;
2055 msgf.msg = msg;
2056 msgf.wParam = *wParam;
2057 msgf.lParam = *lParam;
2058 if (ITextHost_TxNotify(editor->texthost, msgf.nmhdr.code, &msgf) == S_OK)
2059 return FALSE;
2060 *wParam = msgf.wParam;
2061 *lParam = msgf.lParam;
2062 msgf.wParam = *wParam;
2064 return TRUE;
2067 static void ME_UpdateSelectionLinkAttribute(ME_TextEditor *editor)
2069 ME_DisplayItem * startPara, * endPara;
2070 ME_DisplayItem * item;
2071 ME_Cursor cursor;
2072 int from, to;
2074 ME_GetSelection(editor, &from, &to);
2075 if (from > to) from ^= to, to ^=from, from ^= to;
2076 startPara = NULL; endPara = NULL;
2078 /* Find paragraph previous to the one that contains start cursor */
2079 ME_CursorFromCharOfs(editor, from, &cursor);
2080 item = cursor.pRun;
2081 if (item) {
2082 startPara = ME_FindItemBack(item, diParagraph);
2083 item = startPara->member.para.prev_para;
2084 if (item && item->type == diParagraph) startPara = item;
2087 /* Find paragraph that contains end cursor */
2088 ME_CursorFromCharOfs(editor, to, &cursor);
2089 item = cursor.pRun;
2090 if (item) {
2091 endPara = ME_FindItemFwd(item, diParagraph);
2094 if (startPara && endPara) {
2095 ME_UpdateLinkAttribute(editor,
2096 startPara->member.para.nCharOfs,
2097 endPara->member.para.nCharOfs);
2098 } else if (startPara) {
2099 ME_UpdateLinkAttribute(editor,
2100 startPara->member.para.nCharOfs,
2101 -1);
2105 static BOOL
2106 ME_KeyDown(ME_TextEditor *editor, WORD nKey)
2108 BOOL ctrl_is_down = GetKeyState(VK_CONTROL) & 0x8000;
2109 BOOL shift_is_down = GetKeyState(VK_SHIFT) & 0x8000;
2111 if (editor->bMouseCaptured)
2112 return FALSE;
2113 if (nKey != VK_SHIFT && nKey != VK_CONTROL && nKey != VK_MENU)
2114 editor->nSelectionType = stPosition;
2116 switch (nKey)
2118 case VK_LEFT:
2119 case VK_RIGHT:
2120 case VK_HOME:
2121 case VK_END:
2122 editor->nUDArrowX = -1;
2123 /* fall through */
2124 case VK_UP:
2125 case VK_DOWN:
2126 case VK_PRIOR:
2127 case VK_NEXT:
2128 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
2129 ME_ArrowKey(editor, nKey, shift_is_down, ctrl_is_down);
2130 return TRUE;
2131 case VK_BACK:
2132 case VK_DELETE:
2133 editor->nUDArrowX = -1;
2134 /* FIXME backspace and delete aren't the same, they act different wrt paragraph style of the merged paragraph */
2135 if (editor->styleFlags & ES_READONLY)
2136 return FALSE;
2137 if (ME_IsSelection(editor))
2139 ME_DeleteSelection(editor);
2140 ME_CommitUndo(editor);
2142 else if (nKey == VK_DELETE)
2144 /* Delete stops group typing.
2145 * (See MSDN remarks on EM_STOPGROUPTYPING message) */
2146 ME_DeleteTextAtCursor(editor, 1, 1);
2147 ME_CommitUndo(editor);
2149 else if (ME_ArrowKey(editor, VK_LEFT, FALSE, FALSE))
2151 BOOL bDeletionSucceeded;
2152 /* Backspace can be grouped for a single undo */
2153 ME_ContinueCoalescingTransaction(editor);
2154 bDeletionSucceeded = ME_DeleteTextAtCursor(editor, 1, 1);
2155 if (!bDeletionSucceeded && !editor->bEmulateVersion10) { /* v4.1 */
2156 /* Deletion was prevented so the cursor is moved back to where it was.
2157 * (e.g. this happens when trying to delete cell boundaries)
2159 ME_ArrowKey(editor, VK_RIGHT, FALSE, FALSE);
2161 ME_CommitCoalescingUndo(editor);
2163 else
2164 return TRUE;
2165 ME_MoveCursorFromTableRowStartParagraph(editor);
2166 ME_UpdateSelectionLinkAttribute(editor);
2167 ME_UpdateRepaint(editor);
2168 ME_SendRequestResize(editor, FALSE);
2169 return TRUE;
2170 case VK_RETURN:
2171 if (editor->styleFlags & ES_MULTILINE)
2173 ME_Cursor cursor = editor->pCursors[0];
2174 ME_DisplayItem *para = ME_GetParagraph(cursor.pRun);
2175 int from, to;
2176 const WCHAR endl = '\r';
2177 ME_Style *style;
2179 if (editor->styleFlags & ES_READONLY) {
2180 MessageBeep(MB_ICONERROR);
2181 return TRUE;
2184 ME_GetSelection(editor, &from, &to);
2185 if (editor->nTextLimit > ME_GetTextLength(editor) - (to-from))
2187 if (!editor->bEmulateVersion10) { /* v4.1 */
2188 if (para->member.para.nFlags & MEPF_ROWEND) {
2189 /* Add a new table row after this row. */
2190 para = ME_AppendTableRow(editor, para);
2191 para = para->member.para.next_para;
2192 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2193 editor->pCursors[0].nOffset = 0;
2194 editor->pCursors[1] = editor->pCursors[0];
2195 ME_CommitUndo(editor);
2196 ME_CheckTablesForCorruption(editor);
2197 ME_UpdateRepaint(editor);
2198 return TRUE;
2200 else if (para == ME_GetParagraph(editor->pCursors[1].pRun) &&
2201 cursor.nOffset + cursor.pRun->member.run.nCharOfs == 0 &&
2202 para->member.para.prev_para->member.para.nFlags & MEPF_ROWSTART &&
2203 !para->member.para.prev_para->member.para.nCharOfs)
2205 /* Insert a newline before the table. */
2206 para = para->member.para.prev_para;
2207 para->member.para.nFlags &= ~MEPF_ROWSTART;
2208 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2209 editor->pCursors[1] = editor->pCursors[0];
2210 ME_InsertTextFromCursor(editor, 0, &endl, 1,
2211 editor->pCursors[0].pRun->member.run.style);
2212 para = editor->pBuffer->pFirst->member.para.next_para;
2213 ME_SetDefaultParaFormat(para->member.para.pFmt);
2214 para->member.para.nFlags = MEPF_REWRAP;
2215 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2216 editor->pCursors[1] = editor->pCursors[0];
2217 para->member.para.next_para->member.para.nFlags |= MEPF_ROWSTART;
2218 ME_CommitCoalescingUndo(editor);
2219 ME_CheckTablesForCorruption(editor);
2220 ME_UpdateRepaint(editor);
2221 return TRUE;
2223 } else { /* v1.0 - 3.0 */
2224 ME_DisplayItem *para = ME_GetParagraph(cursor.pRun);
2225 if (ME_IsInTable(para))
2227 if (cursor.pRun->member.run.nFlags & MERF_ENDPARA)
2229 if (from == to) {
2230 ME_ContinueCoalescingTransaction(editor);
2231 para = ME_AppendTableRow(editor, para);
2232 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2233 editor->pCursors[0].nOffset = 0;
2234 editor->pCursors[1] = editor->pCursors[0];
2235 ME_CommitCoalescingUndo(editor);
2236 ME_UpdateRepaint(editor);
2237 return TRUE;
2239 } else {
2240 ME_ContinueCoalescingTransaction(editor);
2241 if (cursor.pRun->member.run.nCharOfs + cursor.nOffset == 0 &&
2242 !ME_IsInTable(para->member.para.prev_para))
2244 /* Insert newline before table */
2245 cursor.pRun = ME_FindItemBack(para, diRun);
2246 if (cursor.pRun)
2247 editor->pCursors[0].pRun = cursor.pRun;
2248 editor->pCursors[0].nOffset = 0;
2249 editor->pCursors[1] = editor->pCursors[0];
2250 ME_InsertTextFromCursor(editor, 0, &endl, 1,
2251 editor->pCursors[0].pRun->member.run.style);
2252 } else {
2253 editor->pCursors[1] = editor->pCursors[0];
2254 para = ME_AppendTableRow(editor, para);
2255 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2256 editor->pCursors[0].nOffset = 0;
2257 editor->pCursors[1] = editor->pCursors[0];
2259 ME_CommitCoalescingUndo(editor);
2260 ME_UpdateRepaint(editor);
2261 return TRUE;
2266 style = ME_GetInsertStyle(editor, 0);
2267 ME_SaveTempStyle(editor);
2268 ME_ContinueCoalescingTransaction(editor);
2269 if (shift_is_down)
2270 ME_InsertEndRowFromCursor(editor, 0);
2271 else
2272 ME_InsertTextFromCursor(editor, 0, &endl, 1, style);
2273 ME_ReleaseStyle(style);
2274 ME_CommitCoalescingUndo(editor);
2275 SetCursor(NULL);
2277 if (editor->AutoURLDetect_bEnable)
2278 ME_UpdateSelectionLinkAttribute(editor);
2280 ME_UpdateRepaint(editor);
2282 return TRUE;
2284 break;
2285 case 'A':
2286 if (ctrl_is_down)
2288 ME_SetSelection(editor, 0, -1);
2289 return TRUE;
2291 break;
2292 case 'V':
2293 if (ctrl_is_down)
2294 return ME_Paste(editor);
2295 break;
2296 case 'C':
2297 case 'X':
2298 if (ctrl_is_down)
2300 CHARRANGE range;
2301 BOOL result;
2303 ME_GetSelection(editor, &range.cpMin, &range.cpMax);
2304 result = ME_Copy(editor, &range);
2305 if (result && nKey == 'X')
2307 ME_InternalDeleteText(editor, range.cpMin, range.cpMax-range.cpMin, FALSE);
2308 ME_CommitUndo(editor);
2309 ME_UpdateRepaint(editor);
2311 return result;
2313 break;
2314 case 'Z':
2315 if (ctrl_is_down)
2317 ME_Undo(editor);
2318 return TRUE;
2320 break;
2321 case 'Y':
2322 if (ctrl_is_down)
2324 ME_Redo(editor);
2325 return TRUE;
2327 break;
2329 default:
2330 if (nKey != VK_SHIFT && nKey != VK_CONTROL && nKey && nKey != VK_MENU)
2331 editor->nUDArrowX = -1;
2332 if (ctrl_is_down)
2334 if (nKey == 'W')
2336 CHARFORMAT2W chf;
2337 char buf[2048];
2338 chf.cbSize = sizeof(chf);
2340 ME_GetSelectionCharFormat(editor, &chf);
2341 ME_DumpStyleToBuf(&chf, buf);
2342 MessageBoxA(NULL, buf, "Style dump", MB_OK);
2344 if (nKey == 'Q')
2346 ME_CheckCharOffsets(editor);
2350 return FALSE;
2353 static LRESULT ME_Char(ME_TextEditor *editor, WPARAM charCode,
2354 LPARAM flags, BOOL unicode)
2356 WCHAR wstr;
2358 if (editor->bMouseCaptured)
2359 return 0;
2361 if (unicode)
2362 wstr = (WCHAR)charCode;
2363 else
2365 CHAR charA = charCode;
2366 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &wstr, 1);
2369 if (editor->styleFlags & ES_READONLY) {
2370 MessageBeep(MB_ICONERROR);
2371 return 0; /* FIXME really 0 ? */
2374 if ((unsigned)wstr >= ' ' || wstr == '\t')
2376 ME_Cursor cursor = editor->pCursors[0];
2377 ME_DisplayItem *para = ME_GetParagraph(cursor.pRun);
2378 int from, to;
2379 BOOL ctrl_is_down = GetKeyState(VK_CONTROL) & 0x8000;
2380 ME_GetSelection(editor, &from, &to);
2381 if (wstr == '\t' &&
2382 /* v4.1 allows tabs to be inserted with ctrl key down */
2383 !(ctrl_is_down && !editor->bEmulateVersion10))
2385 ME_DisplayItem *para;
2386 BOOL bSelectedRow = FALSE;
2388 para = ME_GetParagraph(cursor.pRun);
2389 if (ME_IsSelection(editor) &&
2390 cursor.pRun->member.run.nCharOfs + cursor.nOffset == 0 &&
2391 to == ME_GetCursorOfs(editor, 0) &&
2392 para->member.para.prev_para->type == diParagraph)
2394 para = para->member.para.prev_para;
2395 bSelectedRow = TRUE;
2397 if (ME_IsInTable(para))
2399 ME_TabPressedInTable(editor, bSelectedRow);
2400 ME_CommitUndo(editor);
2401 return 0;
2403 } else if (!editor->bEmulateVersion10) { /* v4.1 */
2404 if (para->member.para.nFlags & MEPF_ROWEND) {
2405 if (from == to) {
2406 para = para->member.para.next_para;
2407 if (para->member.para.nFlags & MEPF_ROWSTART)
2408 para = para->member.para.next_para;
2409 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2410 editor->pCursors[0].nOffset = 0;
2411 editor->pCursors[1] = editor->pCursors[0];
2414 } else { /* v1.0 - 3.0 */
2415 if (ME_IsInTable(cursor.pRun) &&
2416 cursor.pRun->member.run.nFlags & MERF_ENDPARA &&
2417 from == to)
2419 /* Text should not be inserted at the end of the table. */
2420 MessageBeep(-1);
2421 return 0;
2424 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
2425 /* WM_CHAR is restricted to nTextLimit */
2426 if(editor->nTextLimit > ME_GetTextLength(editor) - (to-from))
2428 ME_Style *style = ME_GetInsertStyle(editor, 0);
2429 ME_SaveTempStyle(editor);
2430 ME_ContinueCoalescingTransaction(editor);
2431 ME_InsertTextFromCursor(editor, 0, &wstr, 1, style);
2432 ME_ReleaseStyle(style);
2433 ME_CommitCoalescingUndo(editor);
2434 ITextHost_TxSetCursor(editor->texthost, NULL, FALSE);
2437 if (editor->AutoURLDetect_bEnable) ME_UpdateSelectionLinkAttribute(editor);
2439 ME_UpdateRepaint(editor);
2441 return 0;
2444 /* Process the message and calculate the new click count.
2446 * returns: The click count if it is mouse down event, else returns 0. */
2447 static int ME_CalculateClickCount(ME_TextEditor *editor, UINT msg, WPARAM wParam,
2448 LPARAM lParam)
2450 static int clickNum = 0;
2451 if (msg < WM_MOUSEFIRST || msg > WM_MOUSELAST)
2452 return 0;
2454 if ((msg == WM_LBUTTONDBLCLK) ||
2455 (msg == WM_RBUTTONDBLCLK) ||
2456 (msg == WM_MBUTTONDBLCLK) ||
2457 (msg == WM_XBUTTONDBLCLK))
2459 msg -= (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN);
2462 if ((msg == WM_LBUTTONDOWN) ||
2463 (msg == WM_RBUTTONDOWN) ||
2464 (msg == WM_MBUTTONDOWN) ||
2465 (msg == WM_XBUTTONDOWN))
2467 static MSG prevClickMsg;
2468 MSG clickMsg;
2469 /* Compare the editor instead of the hwnd so that the this
2470 * can still be done for windowless richedit controls. */
2471 clickMsg.hwnd = (HWND)editor;
2472 clickMsg.message = msg;
2473 clickMsg.wParam = wParam;
2474 clickMsg.lParam = lParam;
2475 clickMsg.time = GetMessageTime();
2476 clickMsg.pt.x = (short)LOWORD(lParam);
2477 clickMsg.pt.y = (short)HIWORD(lParam);
2478 if ((clickNum != 0) &&
2479 (clickMsg.message == prevClickMsg.message) &&
2480 (clickMsg.hwnd == prevClickMsg.hwnd) &&
2481 (clickMsg.wParam == prevClickMsg.wParam) &&
2482 (clickMsg.time - prevClickMsg.time < GetDoubleClickTime()) &&
2483 (abs(clickMsg.pt.x - prevClickMsg.pt.x) < GetSystemMetrics(SM_CXDOUBLECLK)/2) &&
2484 (abs(clickMsg.pt.y - prevClickMsg.pt.y) < GetSystemMetrics(SM_CYDOUBLECLK)/2))
2486 clickNum++;
2487 } else {
2488 clickNum = 1;
2490 prevClickMsg = clickMsg;
2491 } else {
2492 return 0;
2494 return clickNum;
2497 static BOOL ME_SetCursor(ME_TextEditor *editor)
2499 POINT pt;
2500 BOOL isExact;
2501 int offset;
2502 SCROLLBARINFO sbi;
2503 DWORD messagePos = GetMessagePos();
2504 pt.x = (short)LOWORD(messagePos);
2505 pt.y = (short)HIWORD(messagePos);
2507 if (editor->hWnd)
2509 sbi.cbSize = sizeof(sbi);
2510 GetScrollBarInfo(editor->hWnd, OBJID_HSCROLL, &sbi);
2511 if (!(sbi.rgstate[0] & (STATE_SYSTEM_INVISIBLE|STATE_SYSTEM_OFFSCREEN)) &&
2512 PtInRect(&sbi.rcScrollBar, pt))
2514 ITextHost_TxSetCursor(editor->texthost,
2515 LoadCursorW(NULL, (WCHAR*)IDC_ARROW), FALSE);
2516 return TRUE;
2518 sbi.cbSize = sizeof(sbi);
2519 GetScrollBarInfo(editor->hWnd, OBJID_VSCROLL, &sbi);
2520 if (!(sbi.rgstate[0] & (STATE_SYSTEM_INVISIBLE|STATE_SYSTEM_OFFSCREEN)) &&
2521 PtInRect(&sbi.rcScrollBar, pt))
2523 ITextHost_TxSetCursor(editor->texthost,
2524 LoadCursorW(NULL, (WCHAR*)IDC_ARROW), FALSE);
2525 return TRUE;
2528 ITextHost_TxScreenToClient(editor->texthost, &pt);
2530 if (editor->nSelectionType == stLine && editor->bMouseCaptured) {
2531 ITextHost_TxSetCursor(editor->texthost, hLeft, FALSE);
2532 return TRUE;
2534 if (!editor->bEmulateVersion10 /* v4.1 */ &&
2535 pt.y < editor->rcFormat.top &&
2536 pt.x < editor->rcFormat.left)
2538 ITextHost_TxSetCursor(editor->texthost, hLeft, FALSE);
2539 return TRUE;
2541 if (pt.y < editor->rcFormat.top || pt.y > editor->rcFormat.bottom)
2543 if (editor->bEmulateVersion10) /* v1.0 - 3.0 */
2544 ITextHost_TxSetCursor(editor->texthost,
2545 LoadCursorW(NULL, (WCHAR*)IDC_ARROW), FALSE);
2546 else /* v4.1 */
2547 ITextHost_TxSetCursor(editor->texthost,
2548 LoadCursorW(NULL, (WCHAR*)IDC_IBEAM), TRUE);
2549 return TRUE;
2551 if (pt.x < editor->rcFormat.left)
2553 ITextHost_TxSetCursor(editor->texthost, hLeft, FALSE);
2554 return TRUE;
2556 offset = ME_CharFromPos(editor, pt.x, pt.y, &isExact);
2557 if (isExact)
2559 if (editor->AutoURLDetect_bEnable)
2561 ME_Cursor cursor;
2562 ME_Run *run;
2563 ME_CursorFromCharOfs(editor, offset, &cursor);
2564 run = &cursor.pRun->member.run;
2565 if (editor->AutoURLDetect_bEnable &&
2566 run->style->fmt.dwMask & CFM_LINK &&
2567 run->style->fmt.dwEffects & CFE_LINK)
2569 ITextHost_TxSetCursor(editor->texthost,
2570 LoadCursorW(NULL, (WCHAR*)IDC_HAND),
2571 FALSE);
2572 return TRUE;
2575 if (ME_IsSelection(editor))
2577 int selStart, selEnd;
2578 ME_GetSelection(editor, &selStart, &selEnd);
2579 if (selStart <= offset && selEnd >= offset) {
2580 ITextHost_TxSetCursor(editor->texthost,
2581 LoadCursorW(NULL, (WCHAR*)IDC_ARROW),
2582 FALSE);
2583 return TRUE;
2587 ITextHost_TxSetCursor(editor->texthost,
2588 LoadCursorW(NULL, (WCHAR*)IDC_IBEAM), TRUE);
2589 return TRUE;
2592 static void ME_SetDefaultFormatRect(ME_TextEditor *editor)
2594 ITextHost_TxGetClientRect(editor->texthost, &editor->rcFormat);
2595 editor->rcFormat.top += editor->exStyleFlags & WS_EX_CLIENTEDGE ? 1 : 0;
2596 editor->rcFormat.left += 1 + editor->selofs;
2597 editor->rcFormat.right -= 1;
2600 static BOOL ME_ShowContextMenu(ME_TextEditor *editor, int x, int y)
2602 CHARRANGE selrange;
2603 HMENU menu;
2604 int seltype = 0;
2605 if(!editor->lpOleCallback || !editor->hWnd)
2606 return FALSE;
2607 ME_GetSelection(editor, &selrange.cpMin, &selrange.cpMax);
2608 if(selrange.cpMin == selrange.cpMax)
2609 seltype |= SEL_EMPTY;
2610 else
2612 /* FIXME: Handle objects */
2613 seltype |= SEL_TEXT;
2614 if(selrange.cpMax-selrange.cpMin > 1)
2615 seltype |= SEL_MULTICHAR;
2617 if(SUCCEEDED(IRichEditOleCallback_GetContextMenu(editor->lpOleCallback, seltype, NULL, &selrange, &menu)))
2619 TrackPopupMenu(menu, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, GetParent(editor->hWnd), NULL);
2620 DestroyMenu(menu);
2622 return TRUE;
2625 ME_TextEditor *ME_MakeEditor(ITextHost *texthost, BOOL bEmulateVersion10)
2627 ME_TextEditor *ed = ALLOC_OBJ(ME_TextEditor);
2628 int i;
2629 DWORD props;
2630 LONG selbarwidth;
2632 ed->hWnd = NULL;
2633 ed->texthost = texthost;
2634 ed->bEmulateVersion10 = bEmulateVersion10;
2635 ITextHost_TxGetPropertyBits(texthost,
2636 (TXTBIT_RICHTEXT|TXTBIT_MULTILINE|
2637 TXTBIT_READONLY|TXTBIT_USEPASSWORD|
2638 TXTBIT_HIDESELECTION|TXTBIT_SAVESELECTION|
2639 TXTBIT_AUTOWORDSEL|TXTBIT_VERTICAL|
2640 TXTBIT_WORDWRAP|TXTBIT_DISABLEDRAG),
2641 &props);
2642 ITextHost_TxGetScrollBars(texthost, &ed->styleFlags);
2643 ed->styleFlags &= (WS_VSCROLL|WS_HSCROLL|ES_AUTOVSCROLL|
2644 ES_AUTOHSCROLL|ES_DISABLENOSCROLL);
2645 ed->pBuffer = ME_MakeText();
2646 ed->nZoomNumerator = ed->nZoomDenominator = 0;
2647 ME_MakeFirstParagraph(ed);
2648 /* The four cursors are for:
2649 * 0 - The position where the caret is shown
2650 * 1 - The anchored end of the selection (for normal selection)
2651 * 2 & 3 - The anchored start and end respectively for word, line,
2652 * or paragraph selection.
2654 ed->nCursors = 4;
2655 ed->pCursors = ALLOC_N_OBJ(ME_Cursor, ed->nCursors);
2656 ed->pCursors[0].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
2657 ed->pCursors[0].nOffset = 0;
2658 ed->pCursors[1].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
2659 ed->pCursors[1].nOffset = 0;
2660 ed->pCursors[2] = ed->pCursors[0];
2661 ed->pCursors[3] = ed->pCursors[1];
2662 ed->nLastTotalLength = ed->nTotalLength = 0;
2663 ed->nLastTotalWidth = ed->nTotalWidth = 0;
2664 ed->nUDArrowX = -1;
2665 ed->nSequence = 0;
2666 ed->rgbBackColor = -1;
2667 ed->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
2668 ed->bCaretAtEnd = FALSE;
2669 ed->nEventMask = 0;
2670 ed->nModifyStep = 0;
2671 ed->nTextLimit = TEXT_LIMIT_DEFAULT;
2672 ed->pUndoStack = ed->pRedoStack = ed->pUndoStackBottom = NULL;
2673 ed->nUndoStackSize = 0;
2674 ed->nUndoLimit = STACK_SIZE_DEFAULT;
2675 ed->nUndoMode = umAddToUndo;
2676 ed->nParagraphs = 1;
2677 ed->nLastSelStart = ed->nLastSelEnd = 0;
2678 ed->pLastSelStartPara = ed->pLastSelEndPara = ME_FindItemFwd(ed->pBuffer->pFirst, diParagraph);
2679 ed->bWordWrap = (props & TXTBIT_WORDWRAP) != 0;
2680 ed->bHideSelection = FALSE;
2681 ed->pfnWordBreak = NULL;
2682 ed->lpOleCallback = NULL;
2683 ed->mode = TM_RICHTEXT | TM_MULTILEVELUNDO | TM_MULTICODEPAGE;
2684 ed->mode |= (props & TXTBIT_RICHTEXT) ? TM_RICHTEXT : TM_PLAINTEXT;
2685 ed->AutoURLDetect_bEnable = FALSE;
2686 ed->bHaveFocus = FALSE;
2687 ed->bMouseCaptured = FALSE;
2688 for (i=0; i<HFONT_CACHE_SIZE; i++)
2690 ed->pFontCache[i].nRefs = 0;
2691 ed->pFontCache[i].nAge = 0;
2692 ed->pFontCache[i].hFont = NULL;
2695 ME_CheckCharOffsets(ed);
2696 ed->bDefaultFormatRect = TRUE;
2697 ITextHost_TxGetSelectionBarWidth(ed->texthost, &selbarwidth);
2698 /* FIXME: Convert selbarwidth from HIMETRIC to pixels */
2699 ed->selofs = selbarwidth ? SELECTIONBAR_WIDTH : 0;
2700 ed->nSelectionType = stPosition;
2702 ed->cPasswordMask = 0;
2703 if (props & TXTBIT_USEPASSWORD)
2704 ITextHost_TxGetPasswordChar(texthost, &ed->cPasswordMask);
2706 if (props & TXTBIT_AUTOWORDSEL)
2707 ed->styleFlags |= ECO_AUTOWORDSELECTION;
2708 if (props & TXTBIT_MULTILINE)
2709 ed->styleFlags |= ES_MULTILINE;
2710 if (props & TXTBIT_READONLY)
2711 ed->styleFlags |= ES_READONLY;
2712 if (!(props & TXTBIT_HIDESELECTION))
2713 ed->styleFlags |= ES_NOHIDESEL;
2714 if (props & TXTBIT_SAVESELECTION)
2715 ed->styleFlags |= ES_SAVESEL;
2716 if (props & TXTBIT_VERTICAL)
2717 ed->styleFlags |= ES_VERTICAL;
2718 if (props & TXTBIT_DISABLEDRAG)
2719 ed->styleFlags |= ES_NOOLEDRAGDROP;
2721 ed->notified_cr.cpMin = ed->notified_cr.cpMax = 0;
2723 /* Default scrollbar information */
2724 ed->vert_si.cbSize = sizeof(SCROLLINFO);
2725 ed->vert_si.nMin = 0;
2726 ed->vert_si.nMax = 0;
2727 ed->vert_si.nPage = 0;
2728 ed->vert_si.nPos = 0;
2730 ed->horz_si.cbSize = sizeof(SCROLLINFO);
2731 ed->horz_si.nMin = 0;
2732 ed->horz_si.nMax = 0;
2733 ed->horz_si.nPage = 0;
2734 ed->horz_si.nPos = 0;
2736 OleInitialize(NULL);
2738 return ed;
2741 static void ME_DestroyEditor(ME_TextEditor *editor)
2743 ME_DisplayItem *pFirst = editor->pBuffer->pFirst;
2744 ME_DisplayItem *p = pFirst, *pNext = NULL;
2745 int i;
2747 ME_ClearTempStyle(editor);
2748 ME_EmptyUndoStack(editor);
2749 while(p) {
2750 pNext = p->next;
2751 ME_DestroyDisplayItem(p);
2752 p = pNext;
2754 ME_ReleaseStyle(editor->pBuffer->pDefaultStyle);
2755 for (i=0; i<HFONT_CACHE_SIZE; i++)
2757 if (editor->pFontCache[i].hFont)
2758 DeleteObject(editor->pFontCache[i].hFont);
2760 if (editor->rgbBackColor != -1)
2761 DeleteObject(editor->hbrBackground);
2762 if(editor->lpOleCallback)
2763 IUnknown_Release(editor->lpOleCallback);
2764 IUnknown_Release(editor->texthost);
2765 OleUninitialize();
2767 FREE_OBJ(editor->pBuffer);
2768 FREE_OBJ(editor->pCursors);
2770 FREE_OBJ(editor);
2773 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
2775 TRACE("\n");
2776 switch (fdwReason)
2778 case DLL_PROCESS_ATTACH:
2779 DisableThreadLibraryCalls(hinstDLL);
2780 me_heap = HeapCreate (0, 0x10000, 0);
2781 if (!ME_RegisterEditorClass(hinstDLL)) return FALSE;
2782 hLeft = LoadCursorW(hinstDLL, MAKEINTRESOURCEW(OCR_REVERSE));
2783 LookupInit();
2784 break;
2786 case DLL_PROCESS_DETACH:
2787 UnregisterClassW(RichEdit20W, 0);
2788 UnregisterClassW(RichEdit50W, 0);
2789 UnregisterClassA("RichEdit20A", 0);
2790 UnregisterClassA("RichEdit50A", 0);
2791 if (ME_ListBoxRegistered)
2792 UnregisterClassW(REListBox20W, 0);
2793 if (ME_ComboBoxRegistered)
2794 UnregisterClassW(REComboBox20W, 0);
2795 LookupCleanup();
2796 HeapDestroy (me_heap);
2797 me_heap = NULL;
2798 break;
2800 return TRUE;
2804 static const char * const edit_messages[] = {
2805 "EM_GETSEL",
2806 "EM_SETSEL",
2807 "EM_GETRECT",
2808 "EM_SETRECT",
2809 "EM_SETRECTNP",
2810 "EM_SCROLL",
2811 "EM_LINESCROLL",
2812 "EM_SCROLLCARET",
2813 "EM_GETMODIFY",
2814 "EM_SETMODIFY",
2815 "EM_GETLINECOUNT",
2816 "EM_LINEINDEX",
2817 "EM_SETHANDLE",
2818 "EM_GETHANDLE",
2819 "EM_GETTHUMB",
2820 "EM_UNKNOWN_BF",
2821 "EM_UNKNOWN_C0",
2822 "EM_LINELENGTH",
2823 "EM_REPLACESEL",
2824 "EM_UNKNOWN_C3",
2825 "EM_GETLINE",
2826 "EM_LIMITTEXT",
2827 "EM_CANUNDO",
2828 "EM_UNDO",
2829 "EM_FMTLINES",
2830 "EM_LINEFROMCHAR",
2831 "EM_UNKNOWN_CA",
2832 "EM_SETTABSTOPS",
2833 "EM_SETPASSWORDCHAR",
2834 "EM_EMPTYUNDOBUFFER",
2835 "EM_GETFIRSTVISIBLELINE",
2836 "EM_SETREADONLY",
2837 "EM_SETWORDBREAKPROC",
2838 "EM_GETWORDBREAKPROC",
2839 "EM_GETPASSWORDCHAR",
2840 "EM_SETMARGINS",
2841 "EM_GETMARGINS",
2842 "EM_GETLIMITTEXT",
2843 "EM_POSFROMCHAR",
2844 "EM_CHARFROMPOS",
2845 "EM_SETIMESTATUS",
2846 "EM_GETIMESTATUS"
2849 static const char * const richedit_messages[] = {
2850 "EM_CANPASTE",
2851 "EM_DISPLAYBAND",
2852 "EM_EXGETSEL",
2853 "EM_EXLIMITTEXT",
2854 "EM_EXLINEFROMCHAR",
2855 "EM_EXSETSEL",
2856 "EM_FINDTEXT",
2857 "EM_FORMATRANGE",
2858 "EM_GETCHARFORMAT",
2859 "EM_GETEVENTMASK",
2860 "EM_GETOLEINTERFACE",
2861 "EM_GETPARAFORMAT",
2862 "EM_GETSELTEXT",
2863 "EM_HIDESELECTION",
2864 "EM_PASTESPECIAL",
2865 "EM_REQUESTRESIZE",
2866 "EM_SELECTIONTYPE",
2867 "EM_SETBKGNDCOLOR",
2868 "EM_SETCHARFORMAT",
2869 "EM_SETEVENTMASK",
2870 "EM_SETOLECALLBACK",
2871 "EM_SETPARAFORMAT",
2872 "EM_SETTARGETDEVICE",
2873 "EM_STREAMIN",
2874 "EM_STREAMOUT",
2875 "EM_GETTEXTRANGE",
2876 "EM_FINDWORDBREAK",
2877 "EM_SETOPTIONS",
2878 "EM_GETOPTIONS",
2879 "EM_FINDTEXTEX",
2880 "EM_GETWORDBREAKPROCEX",
2881 "EM_SETWORDBREAKPROCEX",
2882 "EM_SETUNDOLIMIT",
2883 "EM_UNKNOWN_USER_83",
2884 "EM_REDO",
2885 "EM_CANREDO",
2886 "EM_GETUNDONAME",
2887 "EM_GETREDONAME",
2888 "EM_STOPGROUPTYPING",
2889 "EM_SETTEXTMODE",
2890 "EM_GETTEXTMODE",
2891 "EM_AUTOURLDETECT",
2892 "EM_GETAUTOURLDETECT",
2893 "EM_SETPALETTE",
2894 "EM_GETTEXTEX",
2895 "EM_GETTEXTLENGTHEX",
2896 "EM_SHOWSCROLLBAR",
2897 "EM_SETTEXTEX",
2898 "EM_UNKNOWN_USER_98",
2899 "EM_UNKNOWN_USER_99",
2900 "EM_SETPUNCTUATION",
2901 "EM_GETPUNCTUATION",
2902 "EM_SETWORDWRAPMODE",
2903 "EM_GETWORDWRAPMODE",
2904 "EM_SETIMECOLOR",
2905 "EM_GETIMECOLOR",
2906 "EM_SETIMEOPTIONS",
2907 "EM_GETIMEOPTIONS",
2908 "EM_CONVPOSITION",
2909 "EM_UNKNOWN_USER_109",
2910 "EM_UNKNOWN_USER_110",
2911 "EM_UNKNOWN_USER_111",
2912 "EM_UNKNOWN_USER_112",
2913 "EM_UNKNOWN_USER_113",
2914 "EM_UNKNOWN_USER_114",
2915 "EM_UNKNOWN_USER_115",
2916 "EM_UNKNOWN_USER_116",
2917 "EM_UNKNOWN_USER_117",
2918 "EM_UNKNOWN_USER_118",
2919 "EM_UNKNOWN_USER_119",
2920 "EM_SETLANGOPTIONS",
2921 "EM_GETLANGOPTIONS",
2922 "EM_GETIMECOMPMODE",
2923 "EM_FINDTEXTW",
2924 "EM_FINDTEXTEXW",
2925 "EM_RECONVERSION",
2926 "EM_SETIMEMODEBIAS",
2927 "EM_GETIMEMODEBIAS"
2930 static const char *
2931 get_msg_name(UINT msg)
2933 if (msg >= EM_GETSEL && msg <= EM_CHARFROMPOS)
2934 return edit_messages[msg - EM_GETSEL];
2935 if (msg >= EM_CANPASTE && msg <= EM_GETIMEMODEBIAS)
2936 return richedit_messages[msg - EM_CANPASTE];
2937 return "";
2940 void ME_LinkNotify(ME_TextEditor *editor, UINT msg, WPARAM wParam, LPARAM lParam)
2942 int x,y;
2943 ME_Cursor tmpCursor;
2944 BOOL isExact;
2945 int nCharOfs; /* The start of the clicked text. Absolute character offset */
2947 ME_Run *tmpRun;
2949 ENLINK info;
2950 x = (short)LOWORD(lParam);
2951 y = (short)HIWORD(lParam);
2952 nCharOfs = ME_CharFromPos(editor, x, y, &isExact);
2953 if (!isExact) return;
2955 ME_CursorFromCharOfs(editor, nCharOfs, &tmpCursor);
2956 tmpRun = &tmpCursor.pRun->member.run;
2958 if ((tmpRun->style->fmt.dwMask & CFM_LINK)
2959 && (tmpRun->style->fmt.dwEffects & CFE_LINK))
2960 { /* The clicked run has CFE_LINK set */
2961 info.nmhdr.hwndFrom = editor->hWnd;
2962 info.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
2963 info.nmhdr.code = EN_LINK;
2964 info.msg = msg;
2965 info.wParam = wParam;
2966 info.lParam = lParam;
2967 info.chrg.cpMin = ME_CharOfsFromRunOfs(editor,tmpCursor.pRun,0);
2968 info.chrg.cpMax = info.chrg.cpMin + ME_StrVLen(tmpRun->strText);
2969 SendMessageW(GetParent(editor->hWnd), WM_NOTIFY,info.nmhdr.idFrom, (LPARAM)&info);
2973 #define UNSUPPORTED_MSG(e) \
2974 case e: \
2975 FIXME(#e ": stub\n"); \
2976 *phresult = S_FALSE; \
2977 return 0;
2979 /* Handle messages for windowless and windoweded richedit controls.
2981 * The LRESULT that is returned is a return value for window procs,
2982 * and the phresult parameter is the COM return code needed by the
2983 * text services interface. */
2984 LRESULT ME_HandleMessage(ME_TextEditor *editor, UINT msg, WPARAM wParam,
2985 LPARAM lParam, BOOL unicode, HRESULT* phresult)
2987 *phresult = S_OK;
2989 switch(msg) {
2991 UNSUPPORTED_MSG(EM_DISPLAYBAND)
2992 UNSUPPORTED_MSG(EM_FINDWORDBREAK)
2993 UNSUPPORTED_MSG(EM_FMTLINES)
2994 UNSUPPORTED_MSG(EM_FORMATRANGE)
2995 UNSUPPORTED_MSG(EM_GETBIDIOPTIONS)
2996 UNSUPPORTED_MSG(EM_GETEDITSTYLE)
2997 UNSUPPORTED_MSG(EM_GETIMECOMPMODE)
2998 UNSUPPORTED_MSG(EM_GETIMESTATUS)
2999 UNSUPPORTED_MSG(EM_SETIMESTATUS)
3000 UNSUPPORTED_MSG(EM_GETLANGOPTIONS)
3001 UNSUPPORTED_MSG(EM_GETREDONAME)
3002 UNSUPPORTED_MSG(EM_GETTEXTMODE)
3003 UNSUPPORTED_MSG(EM_GETTYPOGRAPHYOPTIONS)
3004 UNSUPPORTED_MSG(EM_GETUNDONAME)
3005 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX)
3006 UNSUPPORTED_MSG(EM_PASTESPECIAL)
3007 UNSUPPORTED_MSG(EM_SELECTIONTYPE)
3008 UNSUPPORTED_MSG(EM_SETBIDIOPTIONS)
3009 UNSUPPORTED_MSG(EM_SETEDITSTYLE)
3010 UNSUPPORTED_MSG(EM_SETFONTSIZE)
3011 UNSUPPORTED_MSG(EM_SETLANGOPTIONS)
3012 UNSUPPORTED_MSG(EM_SETMARGINS)
3013 UNSUPPORTED_MSG(EM_SETPALETTE)
3014 UNSUPPORTED_MSG(EM_SETTABSTOPS)
3015 UNSUPPORTED_MSG(EM_SETTYPOGRAPHYOPTIONS)
3016 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX)
3017 UNSUPPORTED_MSG(WM_STYLECHANGING)
3018 UNSUPPORTED_MSG(WM_STYLECHANGED)
3020 /* Messages specific to Richedit controls */
3022 case EM_STREAMIN:
3023 return ME_StreamIn(editor, wParam, (EDITSTREAM*)lParam, TRUE);
3024 case EM_STREAMOUT:
3025 return ME_StreamOut(editor, wParam, (EDITSTREAM *)lParam);
3026 case WM_GETDLGCODE:
3028 UINT code = DLGC_WANTCHARS|DLGC_WANTTAB|DLGC_WANTARROWS|DLGC_HASSETSEL;
3029 if (editor->styleFlags & ES_MULTILINE)
3030 code |= DLGC_WANTMESSAGE;
3031 return code;
3033 case EM_EMPTYUNDOBUFFER:
3034 ME_EmptyUndoStack(editor);
3035 return 0;
3036 case EM_GETSEL:
3038 /* Note: wParam/lParam can be NULL */
3039 UINT from, to;
3040 PUINT pfrom = wParam ? (PUINT)wParam : &from;
3041 PUINT pto = lParam ? (PUINT)lParam : &to;
3042 ME_GetSelection(editor, (int *)pfrom, (int *)pto);
3043 if ((*pfrom|*pto) & 0xFFFF0000)
3044 return -1;
3045 return MAKELONG(*pfrom,*pto);
3047 case EM_EXGETSEL:
3049 CHARRANGE *pRange = (CHARRANGE *)lParam;
3050 ME_GetSelection(editor, &pRange->cpMin, &pRange->cpMax);
3051 TRACE("EM_EXGETSEL = (%d,%d)\n", pRange->cpMin, pRange->cpMax);
3052 return 0;
3054 case EM_SETUNDOLIMIT:
3056 if ((int)wParam < 0)
3057 editor->nUndoLimit = STACK_SIZE_DEFAULT;
3058 else
3059 editor->nUndoLimit = min(wParam, STACK_SIZE_MAX);
3060 /* Setting a max stack size keeps wine from getting killed
3061 for hogging memory. Windows allocates all this memory at once, so
3062 no program would realistically set a value above our maximum. */
3063 return editor->nUndoLimit;
3065 case EM_CANUNDO:
3066 return editor->pUndoStack != NULL;
3067 case EM_CANREDO:
3068 return editor->pRedoStack != NULL;
3069 case WM_UNDO: /* FIXME: actually not the same */
3070 case EM_UNDO:
3071 return ME_Undo(editor);
3072 case EM_REDO:
3073 return ME_Redo(editor);
3074 case EM_GETOPTIONS:
3076 /* these flags are equivalent to the ES_* counterparts */
3077 DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
3078 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN | ECO_SELECTIONBAR;
3079 DWORD settings = editor->styleFlags & mask;
3081 return settings;
3083 case EM_SETOPTIONS:
3085 /* these flags are equivalent to ES_* counterparts, except for
3086 * ECO_AUTOWORDSELECTION that doesn't have an ES_* counterpart,
3087 * but is still stored in editor->styleFlags. */
3088 const DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
3089 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN |
3090 ECO_SELECTIONBAR | ECO_AUTOWORDSELECTION;
3091 DWORD settings = mask & editor->styleFlags;
3092 DWORD oldSettings = settings;
3093 DWORD changedSettings;
3095 switch(wParam)
3097 case ECOOP_SET:
3098 settings = lParam;
3099 break;
3100 case ECOOP_OR:
3101 settings |= lParam;
3102 break;
3103 case ECOOP_AND:
3104 settings &= lParam;
3105 break;
3106 case ECOOP_XOR:
3107 settings ^= lParam;
3109 changedSettings = oldSettings ^ settings;
3111 if (oldSettings ^ settings) {
3112 editor->styleFlags = (editor->styleFlags & ~mask) | (settings & mask);
3114 if (settings & ECO_SELECTIONBAR) {
3115 editor->selofs = SELECTIONBAR_WIDTH;
3116 editor->rcFormat.left += SELECTIONBAR_WIDTH;
3117 } else {
3118 editor->selofs = 0;
3119 editor->rcFormat.left -= SELECTIONBAR_WIDTH;
3121 ME_WrapMarkedParagraphs(editor);
3124 if (settings & ECO_VERTICAL)
3125 FIXME("ECO_VERTICAL not implemented yet!\n");
3126 if (settings & ECO_AUTOHSCROLL)
3127 FIXME("ECO_AUTOHSCROLL not implemented yet!\n");
3128 if (settings & ECO_AUTOVSCROLL)
3129 FIXME("ECO_AUTOVSCROLL not implemented yet!\n");
3130 if (settings & ECO_NOHIDESEL)
3131 FIXME("ECO_NOHIDESEL not implemented yet!\n");
3132 if (settings & ECO_WANTRETURN)
3133 FIXME("ECO_WANTRETURN not implemented yet!\n");
3134 if (settings & ECO_AUTOWORDSELECTION)
3135 FIXME("ECO_AUTOWORDSELECTION not implemented yet!\n");
3137 return settings;
3139 case EM_SETSEL:
3141 ME_InvalidateSelection(editor);
3142 ME_SetSelection(editor, wParam, lParam);
3143 ME_InvalidateSelection(editor);
3144 ITextHost_TxShowCaret(editor->texthost, FALSE);
3145 ME_ShowCaret(editor);
3146 ME_SendSelChange(editor);
3147 return 0;
3149 case EM_SETSCROLLPOS:
3151 POINT *point = (POINT *)lParam;
3152 ME_ScrollAbs(editor, point->x, point->y);
3153 return 0;
3155 case EM_AUTOURLDETECT:
3157 if (wParam==1 || wParam ==0)
3159 editor->AutoURLDetect_bEnable = (BOOL)wParam;
3160 return 0;
3162 return E_INVALIDARG;
3164 case EM_GETAUTOURLDETECT:
3166 return editor->AutoURLDetect_bEnable;
3168 case EM_EXSETSEL:
3170 int end;
3171 CHARRANGE range = *(CHARRANGE *)lParam;
3173 TRACE("EM_EXSETSEL (%d,%d)\n", range.cpMin, range.cpMax);
3175 ME_InvalidateSelection(editor);
3176 end = ME_SetSelection(editor, range.cpMin, range.cpMax);
3177 ME_InvalidateSelection(editor);
3178 ITextHost_TxShowCaret(editor->texthost, FALSE);
3179 ME_ShowCaret(editor);
3180 ME_SendSelChange(editor);
3182 return end;
3184 case EM_SHOWSCROLLBAR:
3186 DWORD flags;
3188 switch (wParam)
3190 case SB_HORZ:
3191 flags = WS_HSCROLL;
3192 break;
3193 case SB_VERT:
3194 flags = WS_VSCROLL;
3195 break;
3196 case SB_BOTH:
3197 flags = WS_HSCROLL|WS_VSCROLL;
3198 break;
3199 default:
3200 return 0;
3203 if (lParam)
3204 editor->styleFlags |= flags;
3205 else
3206 editor->styleFlags &= flags;
3208 ITextHost_TxShowScrollBar(editor->texthost, wParam, lParam);
3209 return 0;
3211 case EM_SETTEXTEX:
3213 LPWSTR wszText;
3214 SETTEXTEX *pStruct = (SETTEXTEX *)wParam;
3215 size_t len = 0;
3216 int from, to;
3217 ME_Style *style;
3218 BOOL bRtf, bUnicode, bSelection;
3219 int oldModify = editor->nModifyStep;
3221 if (!pStruct) return 0;
3223 /* If we detect ascii rtf at the start of the string,
3224 * we know it isn't unicode. */
3225 bRtf = (lParam && (!strncmp((char *)lParam, "{\\rtf", 5) ||
3226 !strncmp((char *)lParam, "{\\urtf}", 6)));
3227 bUnicode = !bRtf && pStruct->codepage == 1200;
3229 TRACE("EM_SETTEXTEX - %s, flags %d, cp %d\n",
3230 bUnicode ? debugstr_w((LPCWSTR)lParam) : debugstr_a((LPCSTR)lParam),
3231 pStruct->flags, pStruct->codepage);
3233 bSelection = (pStruct->flags & ST_SELECTION) != 0;
3234 if (bSelection) {
3235 ME_GetSelection(editor, &from, &to);
3236 style = ME_GetSelectionInsertStyle(editor);
3237 ME_InternalDeleteText(editor, from, to - from, FALSE);
3238 } else {
3239 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor), FALSE);
3240 style = editor->pBuffer->pDefaultStyle;
3243 if (bRtf) {
3244 ME_StreamInRTFString(editor, bSelection, (char *)lParam);
3245 if (bSelection) {
3246 /* FIXME: The length returned is doesn't include the rtf control
3247 * characters, only the actual text. */
3248 len = lParam ? strlen((char *)lParam) : 0;
3250 } else {
3251 /* FIXME: make use of pStruct->codepage in the to unicode translation */
3252 wszText = lParam ? ME_ToUnicode(bUnicode, (void *)lParam) : NULL;
3253 len = wszText ? lstrlenW(wszText) : 0;
3254 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
3257 if (bSelection) {
3258 ME_ReleaseStyle(style);
3259 if (editor->AutoURLDetect_bEnable)
3260 ME_UpdateSelectionLinkAttribute(editor);
3261 } else {
3262 len = 1;
3263 if (editor->AutoURLDetect_bEnable)
3264 ME_UpdateLinkAttribute(editor, 0, -1);
3266 ME_CommitUndo(editor);
3267 if (!(pStruct->flags & ST_KEEPUNDO))
3269 editor->nModifyStep = oldModify;
3270 ME_EmptyUndoStack(editor);
3272 ME_UpdateRepaint(editor);
3273 return len;
3275 case EM_SETBKGNDCOLOR:
3277 LRESULT lColor;
3278 if (editor->rgbBackColor != -1) {
3279 DeleteObject(editor->hbrBackground);
3280 lColor = editor->rgbBackColor;
3282 else lColor = ITextHost_TxGetSysColor(editor->texthost, COLOR_WINDOW);
3284 if (wParam)
3286 editor->rgbBackColor = -1;
3287 editor->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
3289 else
3291 editor->rgbBackColor = lParam;
3292 editor->hbrBackground = CreateSolidBrush(editor->rgbBackColor);
3294 ITextHost_TxInvalidateRect(editor->texthost, NULL, TRUE);
3295 ITextHost_TxViewChange(editor->texthost, TRUE);
3296 return lColor;
3298 case EM_GETMODIFY:
3299 return editor->nModifyStep == 0 ? 0 : -1;
3300 case EM_SETMODIFY:
3302 if (wParam)
3303 editor->nModifyStep = 1;
3304 else
3305 editor->nModifyStep = 0;
3307 return 0;
3309 case EM_SETREADONLY:
3311 if (wParam)
3312 editor->styleFlags |= ES_READONLY;
3313 else
3314 editor->styleFlags &= ~ES_READONLY;
3315 return 0;
3317 case EM_SETEVENTMASK:
3319 DWORD nOldMask = editor->nEventMask;
3321 editor->nEventMask = lParam;
3322 return nOldMask;
3324 case EM_GETEVENTMASK:
3325 return editor->nEventMask;
3326 case EM_SETCHARFORMAT:
3328 CHARFORMAT2W buf, *p;
3329 BOOL bRepaint = TRUE;
3330 p = ME_ToCF2W(&buf, (CHARFORMAT2W *)lParam);
3331 if (p == NULL) return 0;
3332 if (!wParam)
3333 ME_SetDefaultCharFormat(editor, p);
3334 else if (wParam == (SCF_WORD | SCF_SELECTION)) {
3335 FIXME("EM_SETCHARFORMAT: word selection not supported\n");
3336 return 0;
3337 } else if (wParam == SCF_ALL) {
3338 if (editor->mode & TM_PLAINTEXT)
3339 ME_SetDefaultCharFormat(editor, p);
3340 else {
3341 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), p);
3342 editor->nModifyStep = 1;
3344 } else if (editor->mode & TM_PLAINTEXT) {
3345 return 0;
3346 } else {
3347 int from, to;
3348 ME_GetSelection(editor, &from, &to);
3349 bRepaint = (from != to);
3350 ME_SetSelectionCharFormat(editor, p);
3351 if (from != to) editor->nModifyStep = 1;
3353 ME_CommitUndo(editor);
3354 if (bRepaint)
3356 ME_WrapMarkedParagraphs(editor);
3357 ME_UpdateScrollBar(editor);
3358 ME_Repaint(editor);
3360 return 1;
3362 case EM_GETCHARFORMAT:
3364 CHARFORMAT2W tmp, *dst = (CHARFORMAT2W *)lParam;
3365 if (dst->cbSize != sizeof(CHARFORMATA) &&
3366 dst->cbSize != sizeof(CHARFORMATW) &&
3367 dst->cbSize != sizeof(CHARFORMAT2A) &&
3368 dst->cbSize != sizeof(CHARFORMAT2W))
3369 return 0;
3370 tmp.cbSize = sizeof(tmp);
3371 if (!wParam)
3372 ME_GetDefaultCharFormat(editor, &tmp);
3373 else
3374 ME_GetSelectionCharFormat(editor, &tmp);
3375 ME_CopyToCFAny(dst, &tmp);
3376 return tmp.dwMask;
3378 case EM_SETPARAFORMAT:
3380 BOOL result = ME_SetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
3381 ME_WrapMarkedParagraphs(editor);
3382 ME_UpdateScrollBar(editor);
3383 ME_Repaint(editor);
3384 ME_CommitUndo(editor);
3385 return result;
3387 case EM_GETPARAFORMAT:
3388 ME_GetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
3389 return ((PARAFORMAT2 *)lParam)->dwMask;
3390 case EM_GETFIRSTVISIBLELINE:
3392 ME_DisplayItem *p = editor->pBuffer->pFirst;
3393 int y = editor->vert_si.nPos;
3394 int ypara = 0;
3395 int count = 0;
3396 int ystart, yend;
3397 while(p) {
3398 p = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
3399 if (p->type == diTextEnd)
3400 break;
3401 if (p->type == diParagraph) {
3402 ypara = p->member.para.pt.y;
3403 continue;
3405 ystart = ypara + p->member.row.pt.y;
3406 yend = ystart + p->member.row.nHeight;
3407 if (y < yend) {
3408 break;
3410 count++;
3412 return count;
3414 case EM_HIDESELECTION:
3416 editor->bHideSelection = (wParam != 0);
3417 ME_InvalidateSelection(editor);
3418 return 0;
3420 case EM_LINESCROLL:
3422 ME_ScrollDown(editor, lParam * 8); /* FIXME follow the original */
3423 return TRUE; /* Should return false if a single line richedit control */
3425 case WM_CLEAR:
3427 int from, to;
3428 ME_GetSelection(editor, &from, &to);
3429 ME_InternalDeleteText(editor, from, to-from, FALSE);
3430 ME_CommitUndo(editor);
3431 ME_UpdateRepaint(editor);
3432 return 0;
3434 case EM_REPLACESEL:
3436 int from, to;
3437 ME_Style *style;
3438 LPWSTR wszText = lParam ? ME_ToUnicode(unicode, (void *)lParam) : NULL;
3439 size_t len = wszText ? lstrlenW(wszText) : 0;
3440 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText));
3442 ME_GetSelection(editor, &from, &to);
3443 style = ME_GetSelectionInsertStyle(editor);
3444 ME_InternalDeleteText(editor, from, to-from, FALSE);
3445 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
3446 ME_ReleaseStyle(style);
3447 /* drop temporary style if line end */
3449 * FIXME question: does abc\n mean: put abc,
3450 * clear temp style, put \n? (would require a change)
3452 if (len>0 && wszText[len-1] == '\n')
3453 ME_ClearTempStyle(editor);
3454 ME_EndToUnicode(unicode, wszText);
3455 ME_CommitUndo(editor);
3456 if (editor->AutoURLDetect_bEnable) ME_UpdateSelectionLinkAttribute(editor);
3457 if (!wParam)
3458 ME_EmptyUndoStack(editor);
3459 ME_UpdateRepaint(editor);
3460 return len;
3462 case EM_SCROLLCARET:
3463 ME_EnsureVisible(editor, &editor->pCursors[0]);
3464 return 0;
3465 case WM_SETFONT:
3467 LOGFONTW lf;
3468 CHARFORMAT2W fmt;
3469 HDC hDC;
3470 BOOL bRepaint = LOWORD(lParam);
3472 if (!wParam)
3473 wParam = (WPARAM)GetStockObject(SYSTEM_FONT);
3474 GetObjectW((HGDIOBJ)wParam, sizeof(LOGFONTW), &lf);
3475 hDC = ITextHost_TxGetDC(editor->texthost);
3476 ME_CharFormatFromLogFont(hDC, &lf, &fmt);
3477 ITextHost_TxReleaseDC(editor->texthost, hDC);
3478 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), &fmt);
3479 ME_SetDefaultCharFormat(editor, &fmt);
3481 ME_CommitUndo(editor);
3482 if (bRepaint)
3483 ME_RewrapRepaint(editor);
3484 return 0;
3486 case WM_SETTEXT:
3488 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor), FALSE);
3489 if (lParam)
3491 TRACE("WM_SETTEXT lParam==%lx\n",lParam);
3492 if (!unicode && !strncmp((char *)lParam, "{\\rtf", 5))
3494 /* Undocumented: WM_SETTEXT supports RTF text */
3495 ME_StreamInRTFString(editor, 0, (char *)lParam);
3497 else
3499 LPWSTR wszText = ME_ToUnicode(unicode, (void *)lParam);
3500 TRACE("WM_SETTEXT - %s\n", debugstr_w(wszText)); /* debugstr_w() */
3501 if (lstrlenW(wszText) > 0)
3503 int len = -1;
3505 /* uses default style! */
3506 if (!(editor->styleFlags & ES_MULTILINE))
3508 WCHAR * p;
3510 p = wszText;
3511 while (*p != '\0' && *p != '\r' && *p != '\n') p++;
3512 len = p - wszText;
3514 ME_InsertTextFromCursor(editor, 0, wszText, len, editor->pBuffer->pDefaultStyle);
3516 ME_EndToUnicode(unicode, wszText);
3519 else
3520 TRACE("WM_SETTEXT - NULL\n");
3521 if (editor->AutoURLDetect_bEnable)
3523 ME_UpdateLinkAttribute(editor, 0, -1);
3525 ME_SetSelection(editor, 0, 0);
3526 editor->nModifyStep = 0;
3527 ME_CommitUndo(editor);
3528 ME_EmptyUndoStack(editor);
3529 ME_UpdateRepaint(editor);
3530 return 1;
3532 case EM_CANPASTE:
3534 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
3535 if (IsClipboardFormatAvailable(nRTFFormat))
3536 return TRUE;
3537 if (IsClipboardFormatAvailable(CF_UNICODETEXT))
3538 return TRUE;
3539 return FALSE;
3541 case WM_PASTE:
3542 ME_Paste(editor);
3543 return 0;
3544 case WM_CUT:
3545 case WM_COPY:
3547 CHARRANGE range;
3548 ME_GetSelection(editor, &range.cpMin, &range.cpMax);
3550 if (ME_Copy(editor, &range) && msg == WM_CUT)
3552 ME_InternalDeleteText(editor, range.cpMin, range.cpMax-range.cpMin, FALSE);
3553 ME_CommitUndo(editor);
3554 ME_UpdateRepaint(editor);
3556 return 0;
3558 case WM_GETTEXTLENGTH:
3560 GETTEXTLENGTHEX how;
3562 /* CR/LF conversion required in 2.0 mode, verbatim in 1.0 mode */
3563 how.flags = GTL_CLOSE | (editor->bEmulateVersion10 ? 0 : GTL_USECRLF) | GTL_NUMCHARS;
3564 how.codepage = unicode ? 1200 : CP_ACP;
3565 return ME_GetTextLengthEx(editor, &how);
3567 case EM_GETTEXTLENGTHEX:
3568 return ME_GetTextLengthEx(editor, (GETTEXTLENGTHEX *)wParam);
3569 case WM_GETTEXT:
3571 GETTEXTEX ex;
3572 LRESULT rc;
3573 LPSTR bufferA = NULL;
3574 LPWSTR bufferW = NULL;
3576 if (unicode)
3577 bufferW = heap_alloc((wParam + 2) * sizeof(WCHAR));
3578 else
3579 bufferA = heap_alloc(wParam + 2);
3581 ex.cb = (wParam + 2) * (unicode ? sizeof(WCHAR) : sizeof(CHAR));
3582 ex.flags = GT_USECRLF;
3583 ex.codepage = unicode ? 1200 : CP_ACP;
3584 ex.lpDefaultChar = NULL;
3585 ex.lpUsedDefChar = NULL;
3587 rc = ME_GetTextEx(editor, &ex, unicode ? (LPARAM)bufferW : (LPARAM)bufferA);
3589 if (unicode)
3591 memcpy((LPWSTR)lParam, bufferW, wParam * sizeof(WCHAR));
3592 if (strlenW(bufferW) >= wParam) rc = 0;
3594 else
3596 memcpy((LPSTR)lParam, bufferA, wParam);
3597 if (strlen(bufferA) >= wParam) rc = 0;
3599 heap_free(bufferA);
3600 heap_free(bufferW);
3601 return rc;
3603 case EM_GETTEXTEX:
3604 return ME_GetTextEx(editor, (GETTEXTEX*)wParam, lParam);
3605 case EM_GETSELTEXT:
3607 int from, to;
3608 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
3609 ME_GetSelection(editor, &from, &to);
3610 tr.chrg.cpMin = from;
3611 tr.chrg.cpMax = to;
3612 tr.lpstrText = (WCHAR *)lParam;
3613 return ME_GetTextRange(editor, &tr, unicode);
3615 case EM_GETSCROLLPOS:
3617 POINT *point = (POINT *)lParam;
3618 point->x = editor->horz_si.nPos;
3619 point->y = editor->vert_si.nPos;
3620 return 1;
3622 case EM_GETTEXTRANGE:
3624 TEXTRANGEW *rng = (TEXTRANGEW *)lParam;
3625 TRACE("EM_GETTEXTRANGE min=%d max=%d unicode=%d emul1.0=%d length=%d\n",
3626 rng->chrg.cpMin, rng->chrg.cpMax, unicode,
3627 editor->bEmulateVersion10, ME_GetTextLength(editor));
3628 return ME_GetTextRange(editor, rng, unicode);
3630 case EM_GETLINE:
3632 ME_DisplayItem *run;
3633 const unsigned int nMaxChars = *(WORD *) lParam;
3634 unsigned int nCharsLeft = nMaxChars;
3635 char *dest = (char *) lParam;
3636 BOOL wroteNull = FALSE;
3638 TRACE("EM_GETLINE: row=%d, nMaxChars=%d (%s)\n", (int) wParam, nMaxChars,
3639 unicode ? "Unicode" : "Ansi");
3641 run = ME_FindRowWithNumber(editor, wParam);
3642 if (run == NULL)
3643 return 0;
3645 while (nCharsLeft && (run = ME_FindItemFwd(run, diRunOrStartRow))
3646 && run->type == diRun)
3648 unsigned int nCopy;
3649 ME_String *strText;
3651 strText = run->member.run.strText;
3652 nCopy = min(nCharsLeft, strText->nLen);
3654 if (unicode)
3655 memcpy(dest, strText->szData, nCopy * sizeof(WCHAR));
3656 else
3657 nCopy = WideCharToMultiByte(CP_ACP, 0, strText->szData, nCopy, dest,
3658 nCharsLeft, NULL, NULL);
3659 dest += nCopy * (unicode ? sizeof(WCHAR) : 1);
3660 nCharsLeft -= nCopy;
3663 /* append line termination, space allowing */
3664 if (nCharsLeft > 0)
3666 if (unicode)
3667 *((WCHAR *)dest) = '\0';
3668 else
3669 *dest = '\0';
3670 nCharsLeft--;
3671 wroteNull = TRUE;
3674 TRACE("EM_GETLINE: got %u characters\n", nMaxChars - nCharsLeft);
3675 return nMaxChars - nCharsLeft - (wroteNull ? 1 : 0);
3677 case EM_GETLINECOUNT:
3679 ME_DisplayItem *item = editor->pBuffer->pFirst->next;
3680 int nRows = 0;
3682 ME_DisplayItem *prev_para = NULL, *last_para = NULL;
3684 while (item != editor->pBuffer->pLast)
3686 assert(item->type == diParagraph);
3687 prev_para = ME_FindItemBack(item, diRun);
3688 if (prev_para) {
3689 assert(prev_para->member.run.nFlags & MERF_ENDPARA);
3691 nRows += item->member.para.nRows;
3692 item = item->member.para.next_para;
3694 last_para = ME_FindItemBack(item, diRun);
3695 assert(last_para);
3696 assert(last_para->member.run.nFlags & MERF_ENDPARA);
3697 if (editor->bEmulateVersion10 && prev_para &&
3698 last_para->member.run.nCharOfs == 0 &&
3699 prev_para->member.run.strText->nLen == 1 &&
3700 prev_para->member.run.strText->szData[0] == '\r')
3702 /* In 1.0 emulation, the last solitary \r at the very end of the text
3703 (if one exists) is NOT a line break.
3704 FIXME: this is an ugly hack. This should have a more regular model. */
3705 nRows--;
3708 TRACE("EM_GETLINECOUNT: nRows==%d\n", nRows);
3709 return max(1, nRows);
3711 case EM_LINEFROMCHAR:
3713 if (wParam == -1)
3714 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(editor, 1));
3715 else
3716 return ME_RowNumberFromCharOfs(editor, wParam);
3718 case EM_EXLINEFROMCHAR:
3720 if (lParam == -1)
3721 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(editor,1));
3722 else
3723 return ME_RowNumberFromCharOfs(editor, lParam);
3725 case EM_LINEINDEX:
3727 ME_DisplayItem *item, *para;
3728 int nCharOfs;
3730 if (wParam == -1)
3731 item = ME_FindItemBack(editor->pCursors[0].pRun, diStartRow);
3732 else
3733 item = ME_FindRowWithNumber(editor, wParam);
3734 if (!item)
3735 return -1;
3736 para = ME_GetParagraph(item);
3737 item = ME_FindItemFwd(item, diRun);
3738 nCharOfs = para->member.para.nCharOfs + item->member.run.nCharOfs;
3739 TRACE("EM_LINEINDEX: nCharOfs==%d\n", nCharOfs);
3740 return nCharOfs;
3742 case EM_LINELENGTH:
3744 ME_DisplayItem *item, *item_end;
3745 int nChars = 0, nThisLineOfs = 0, nNextLineOfs = 0;
3746 ME_Cursor cursor;
3748 if (wParam > ME_GetTextLength(editor))
3749 return 0;
3750 if (wParam == -1)
3752 FIXME("EM_LINELENGTH: returning number of unselected characters on lines with selection unsupported.\n");
3753 return 0;
3755 ME_CursorFromCharOfs(editor, wParam, &cursor);
3756 item = ME_RowStart(cursor.pRun);
3757 nThisLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item, diRun), 0);
3758 item_end = ME_FindItemFwd(item, diStartRowOrParagraphOrEnd);
3759 if (item_end->type == diStartRow)
3760 nNextLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item_end, diRun), 0);
3761 else
3763 ME_DisplayItem *endPara;
3765 nNextLineOfs = ME_FindItemFwd(item, diParagraphOrEnd)->member.para.nCharOfs;
3766 endPara = ME_FindItemFwd(item, diParagraphOrEnd);
3767 endPara = ME_FindItemBack(endPara, diRun);
3768 assert(endPara);
3769 assert(endPara->type == diRun);
3770 assert(endPara->member.run.nFlags & MERF_ENDPARA);
3771 nNextLineOfs -= endPara->member.run.strText->nLen;
3773 nChars = nNextLineOfs - nThisLineOfs;
3774 TRACE("EM_LINELENGTH(%ld)==%d\n",wParam, nChars);
3775 return nChars;
3777 case EM_EXLIMITTEXT:
3779 if ((int)lParam < 0)
3780 return 0;
3781 if (lParam == 0)
3782 editor->nTextLimit = 65536;
3783 else
3784 editor->nTextLimit = (int) lParam;
3785 return 0;
3787 case EM_LIMITTEXT:
3789 if (wParam == 0)
3790 editor->nTextLimit = 65536;
3791 else
3792 editor->nTextLimit = (int) wParam;
3793 return 0;
3795 case EM_GETLIMITTEXT:
3797 return editor->nTextLimit;
3799 case EM_FINDTEXT:
3801 FINDTEXTA *ft = (FINDTEXTA *)lParam;
3802 int nChars = MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, NULL, 0);
3803 WCHAR *tmp;
3804 LRESULT r;
3806 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
3807 MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, tmp, nChars);
3808 r = ME_FindText(editor, wParam, &ft->chrg, tmp, NULL);
3809 FREE_OBJ( tmp );
3810 return r;
3812 case EM_FINDTEXTEX:
3814 FINDTEXTEXA *ex = (FINDTEXTEXA *)lParam;
3815 int nChars = MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, NULL, 0);
3816 WCHAR *tmp;
3817 LRESULT r;
3819 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
3820 MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, tmp, nChars);
3821 r = ME_FindText(editor, wParam, &ex->chrg, tmp, &ex->chrgText);
3822 FREE_OBJ( tmp );
3823 return r;
3825 case EM_FINDTEXTW:
3827 FINDTEXTW *ft = (FINDTEXTW *)lParam;
3828 return ME_FindText(editor, wParam, &ft->chrg, ft->lpstrText, NULL);
3830 case EM_FINDTEXTEXW:
3832 FINDTEXTEXW *ex = (FINDTEXTEXW *)lParam;
3833 return ME_FindText(editor, wParam, &ex->chrg, ex->lpstrText, &ex->chrgText);
3835 case EM_GETZOOM:
3836 if (!wParam || !lParam)
3837 return FALSE;
3838 *(int *)wParam = editor->nZoomNumerator;
3839 *(int *)lParam = editor->nZoomDenominator;
3840 return TRUE;
3841 case EM_SETZOOM:
3842 return ME_SetZoom(editor, wParam, lParam);
3843 case EM_CHARFROMPOS:
3844 return ME_CharFromPos(editor, ((POINTL *)lParam)->x, ((POINTL *)lParam)->y, NULL);
3845 case EM_POSFROMCHAR:
3847 ME_DisplayItem *pRun;
3848 int nCharOfs, nOffset, nLength;
3849 POINTL pt = {0,0};
3851 nCharOfs = wParam;
3852 /* detect which API version we're dealing with */
3853 if (wParam >= 0x40000)
3854 nCharOfs = lParam;
3855 nLength = ME_GetTextLength(editor);
3856 nCharOfs = min(nCharOfs, nLength);
3857 nCharOfs = max(nCharOfs, 0);
3859 ME_RunOfsFromCharOfs(editor, nCharOfs, &pRun, &nOffset);
3860 assert(pRun->type == diRun);
3861 pt.y = pRun->member.run.pt.y;
3862 pt.x = pRun->member.run.pt.x + ME_PointFromChar(editor, &pRun->member.run, nOffset);
3863 pt.y += ME_GetParagraph(pRun)->member.para.pt.y + editor->rcFormat.top;
3864 pt.x += editor->rcFormat.left;
3866 pt.x -= editor->horz_si.nPos;
3867 pt.y -= editor->vert_si.nPos;
3869 if (wParam >= 0x40000) {
3870 *(POINTL *)wParam = pt;
3872 return (wParam >= 0x40000) ? 0 : MAKELONG( pt.x, pt.y );
3874 case WM_CREATE:
3876 SCROLLINFO si;
3878 ME_SetDefaultFormatRect(editor);
3880 si.cbSize = sizeof(si);
3881 si.fMask = SIF_PAGE | SIF_RANGE;
3882 if (editor->styleFlags & ES_DISABLENOSCROLL)
3883 si.fMask |= SIF_DISABLENOSCROLL;
3884 si.nMax = (si.fMask & SIF_DISABLENOSCROLL) ? 1 : 0;
3885 si.nMin = 0;
3886 si.nPage = 0;
3887 if (editor->hWnd) {
3888 SetScrollInfo(editor->hWnd, SB_VERT, &si, TRUE);
3889 SetScrollInfo(editor->hWnd, SB_HORZ, &si, TRUE);
3890 } else {
3891 ITextHost_TxSetScrollRange(editor->texthost, SB_VERT, si.nMin, si.nMax, TRUE);
3892 ITextHost_TxSetScrollRange(editor->texthost, SB_HORZ, si.nMin, si.nMax, TRUE);
3895 ME_CommitUndo(editor);
3896 ME_WrapMarkedParagraphs(editor);
3897 ME_MoveCaret(editor);
3898 return 0;
3900 case WM_DESTROY:
3901 ME_DestroyEditor(editor);
3902 return 0;
3903 case WM_SETCURSOR:
3905 return ME_SetCursor(editor);
3907 case WM_LBUTTONDBLCLK:
3908 case WM_LBUTTONDOWN:
3910 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
3911 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
3912 !ME_FilterEvent(editor, msg, &wParam, &lParam))
3913 return 0;
3914 ITextHost_TxSetFocus(editor->texthost);
3915 ME_LButtonDown(editor, (short)LOWORD(lParam), (short)HIWORD(lParam),
3916 ME_CalculateClickCount(editor, msg, wParam, lParam));
3917 ITextHost_TxSetCapture(editor->texthost, TRUE);
3918 editor->bMouseCaptured = TRUE;
3919 ME_LinkNotify(editor,msg,wParam,lParam);
3920 if (!ME_SetCursor(editor)) goto do_default;
3921 break;
3923 case WM_MOUSEMOVE:
3924 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
3925 !ME_FilterEvent(editor, msg, &wParam, &lParam))
3926 return 0;
3927 if (editor->bMouseCaptured)
3928 ME_MouseMove(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
3929 ME_LinkNotify(editor,msg,wParam,lParam);
3930 /* Set cursor if mouse is captured, since WM_SETCURSOR won't be received. */
3931 if (editor->bMouseCaptured)
3932 ME_SetCursor(editor);
3933 break;
3934 case WM_LBUTTONUP:
3935 if (editor->bMouseCaptured) {
3936 ITextHost_TxSetCapture(editor->texthost, FALSE);
3937 editor->bMouseCaptured = FALSE;
3939 if (editor->nSelectionType == stDocument)
3940 editor->nSelectionType = stPosition;
3941 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
3942 !ME_FilterEvent(editor, msg, &wParam, &lParam))
3943 return 0;
3944 else
3946 ME_SetCursor(editor);
3947 ME_LinkNotify(editor,msg,wParam,lParam);
3949 break;
3950 case WM_RBUTTONUP:
3951 case WM_RBUTTONDOWN:
3952 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
3953 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
3954 !ME_FilterEvent(editor, msg, &wParam, &lParam))
3955 return 0;
3956 goto do_default;
3957 case WM_CONTEXTMENU:
3958 if (!ME_ShowContextMenu(editor, (short)LOWORD(lParam), (short)HIWORD(lParam)))
3959 goto do_default;
3960 break;
3961 case WM_SETFOCUS:
3962 editor->bHaveFocus = TRUE;
3963 ME_ShowCaret(editor);
3964 ME_SendOldNotify(editor, EN_SETFOCUS);
3965 return 0;
3966 case WM_KILLFOCUS:
3967 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
3968 editor->bHaveFocus = FALSE;
3969 ME_HideCaret(editor);
3970 ME_SendOldNotify(editor, EN_KILLFOCUS);
3971 return 0;
3972 case WM_COMMAND:
3973 TRACE("editor wnd command = %d\n", LOWORD(wParam));
3974 return 0;
3975 case WM_KEYUP:
3976 if ((editor->nEventMask & ENM_KEYEVENTS) &&
3977 !ME_FilterEvent(editor, msg, &wParam, &lParam))
3978 return 0;
3979 goto do_default;
3980 case WM_KEYDOWN:
3981 if ((editor->nEventMask & ENM_KEYEVENTS) &&
3982 !ME_FilterEvent(editor, msg, &wParam, &lParam))
3983 return 0;
3984 if (ME_KeyDown(editor, LOWORD(wParam)))
3985 return 0;
3986 goto do_default;
3987 case WM_CHAR:
3988 return ME_Char(editor, wParam, lParam, unicode);
3989 case WM_UNICHAR:
3990 if (unicode)
3992 if(wParam == UNICODE_NOCHAR) return TRUE;
3993 if(wParam <= 0x000fffff)
3995 if(wParam > 0xffff) /* convert to surrogates */
3997 wParam -= 0x10000;
3998 ME_Char(editor, (wParam >> 10) + 0xd800, 0, TRUE);
3999 ME_Char(editor, (wParam & 0x03ff) + 0xdc00, 0, TRUE);
4000 } else {
4001 ME_Char(editor, wParam, 0, TRUE);
4004 return 0;
4006 break;
4007 case EM_STOPGROUPTYPING:
4008 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
4009 return 0;
4010 case WM_HSCROLL:
4012 const int scrollUnit = 7;
4014 switch(LOWORD(wParam))
4016 case SB_LEFT:
4017 ME_ScrollAbs(editor, 0, 0);
4018 break;
4019 case SB_RIGHT:
4020 ME_ScrollAbs(editor,
4021 editor->horz_si.nMax - (int)editor->horz_si.nPage,
4022 editor->vert_si.nMax - (int)editor->vert_si.nPage);
4023 break;
4024 case SB_LINELEFT:
4025 ME_ScrollLeft(editor, scrollUnit);
4026 break;
4027 case SB_LINERIGHT:
4028 ME_ScrollRight(editor, scrollUnit);
4029 break;
4030 case SB_PAGELEFT:
4031 ME_ScrollLeft(editor, editor->sizeWindow.cx);
4032 break;
4033 case SB_PAGERIGHT:
4034 ME_ScrollRight(editor, editor->sizeWindow.cx);
4035 break;
4036 case SB_THUMBTRACK:
4037 case SB_THUMBPOSITION:
4039 SCROLLINFO sbi;
4040 sbi.cbSize = sizeof(sbi);
4041 sbi.fMask = SIF_TRACKPOS;
4042 /* Try to get 32-bit track position value. */
4043 if (!GetScrollInfo(editor->hWnd, SB_HORZ, &sbi))
4044 /* GetScrollInfo failed, settle for 16-bit value in wParam. */
4045 sbi.nTrackPos = HIWORD(wParam);
4047 ME_HScrollAbs(editor, sbi.nTrackPos);
4048 break;
4051 break;
4053 case EM_SCROLL: /* fall through */
4054 case WM_VSCROLL:
4056 int origNPos;
4057 int lineHeight;
4059 origNPos = editor->vert_si.nPos;
4060 lineHeight = 24;
4062 if (editor->pBuffer && editor->pBuffer->pDefaultStyle)
4063 lineHeight = editor->pBuffer->pDefaultStyle->tm.tmHeight;
4064 if (lineHeight <= 0) lineHeight = 24;
4066 switch(LOWORD(wParam))
4068 case SB_TOP:
4069 ME_ScrollAbs(editor, 0, 0);
4070 break;
4071 case SB_BOTTOM:
4072 ME_ScrollAbs(editor,
4073 editor->horz_si.nMax - (int)editor->horz_si.nPage,
4074 editor->vert_si.nMax - (int)editor->vert_si.nPage);
4075 break;
4076 case SB_LINEUP:
4077 ME_ScrollUp(editor,lineHeight);
4078 break;
4079 case SB_LINEDOWN:
4080 ME_ScrollDown(editor,lineHeight);
4081 break;
4082 case SB_PAGEUP:
4083 ME_ScrollUp(editor,editor->sizeWindow.cy);
4084 break;
4085 case SB_PAGEDOWN:
4086 ME_ScrollDown(editor,editor->sizeWindow.cy);
4087 break;
4088 case SB_THUMBTRACK:
4089 case SB_THUMBPOSITION:
4091 SCROLLINFO sbi;
4092 sbi.cbSize = sizeof(sbi);
4093 sbi.fMask = SIF_TRACKPOS;
4094 /* Try to get 32-bit track position value. */
4095 if (!GetScrollInfo(editor->hWnd, SB_VERT, &sbi))
4096 /* GetScrollInfo failed, settle for 16-bit value in wParam. */
4097 sbi.nTrackPos = HIWORD(wParam);
4099 ME_VScrollAbs(editor, sbi.nTrackPos);
4100 break;
4103 if (msg == EM_SCROLL)
4104 return 0x00010000 | (((editor->vert_si.nPos - origNPos)/lineHeight) & 0xffff);
4105 break;
4107 case WM_MOUSEWHEEL:
4109 int gcWheelDelta;
4110 UINT pulScrollLines;
4111 BOOL ctrl_is_down;
4113 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
4114 !ME_FilterEvent(editor, msg, &wParam, &lParam))
4115 return 0;
4117 ctrl_is_down = GetKeyState(VK_CONTROL) & 0x8000;
4119 gcWheelDelta = GET_WHEEL_DELTA_WPARAM(wParam);
4121 if (abs(gcWheelDelta) >= WHEEL_DELTA)
4123 if (ctrl_is_down) {
4124 int numerator;
4125 if (!editor->nZoomNumerator || !editor->nZoomDenominator)
4127 numerator = 100;
4128 } else {
4129 numerator = editor->nZoomNumerator * 100 / editor->nZoomDenominator;
4131 numerator = numerator + (gcWheelDelta / WHEEL_DELTA) * 10;
4132 if (numerator >= 10 && numerator <= 500)
4133 ME_SetZoom(editor, numerator, 100);
4134 } else {
4135 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
4136 /* FIXME follow the original */
4137 if (pulScrollLines)
4138 ME_ScrollDown(editor,pulScrollLines * (-gcWheelDelta / WHEEL_DELTA) * 8);
4141 break;
4143 case EM_GETRECT:
4145 *((RECT *)lParam) = editor->rcFormat;
4146 if (editor->bDefaultFormatRect)
4147 ((RECT *)lParam)->left -= editor->selofs;
4148 return 0;
4150 case EM_SETRECT:
4151 case EM_SETRECTNP:
4153 if (lParam)
4155 int border = 0;
4156 RECT clientRect;
4157 RECT *rc = (RECT *)lParam;
4159 border = editor->exStyleFlags & WS_EX_CLIENTEDGE ? 1 : 0;
4160 ITextHost_TxGetClientRect(editor->texthost, &clientRect);
4161 if (wParam == 0)
4163 editor->rcFormat.top = max(0, rc->top - border);
4164 editor->rcFormat.left = max(0, rc->left - border);
4165 editor->rcFormat.bottom = min(clientRect.bottom, rc->bottom);
4166 editor->rcFormat.right = min(clientRect.right, rc->right + border);
4167 } else if (wParam == 1) {
4168 /* MSDN incorrectly says a wParam value of 1 causes the
4169 * lParam rect to be used as a relative offset,
4170 * however, the tests show it just prevents min/max bound
4171 * checking. */
4172 editor->rcFormat.top = rc->top - border;
4173 editor->rcFormat.left = rc->left - border;
4174 editor->rcFormat.bottom = rc->bottom;
4175 editor->rcFormat.right = rc->right + border;
4176 } else {
4177 return 0;
4179 editor->bDefaultFormatRect = FALSE;
4181 else
4183 ME_SetDefaultFormatRect(editor);
4184 editor->bDefaultFormatRect = TRUE;
4186 if (msg != EM_SETRECTNP)
4187 ME_RewrapRepaint(editor);
4188 return 0;
4190 case EM_REQUESTRESIZE:
4191 ME_SendRequestResize(editor, TRUE);
4192 return 0;
4193 case WM_SETREDRAW:
4194 goto do_default;
4195 case WM_SIZE:
4197 RECT clientRect;
4199 ITextHost_TxGetClientRect(editor->texthost, &clientRect);
4200 if (editor->bDefaultFormatRect) {
4201 ME_SetDefaultFormatRect(editor);
4202 } else {
4203 editor->rcFormat.right += clientRect.right - editor->prevClientRect.right;
4204 editor->rcFormat.bottom += clientRect.bottom - editor->prevClientRect.bottom;
4206 editor->prevClientRect = clientRect;
4207 ME_RewrapRepaint(editor);
4208 goto do_default;
4210 /* IME messages to make richedit controls IME aware */
4211 case WM_IME_SETCONTEXT:
4212 case WM_IME_CONTROL:
4213 case WM_IME_SELECT:
4214 case WM_IME_COMPOSITIONFULL:
4215 return 0;
4216 case WM_IME_STARTCOMPOSITION:
4218 editor->imeStartIndex=ME_GetCursorOfs(editor,0);
4219 ME_DeleteSelection(editor);
4220 ME_CommitUndo(editor);
4221 ME_UpdateRepaint(editor);
4222 return 0;
4224 case WM_IME_COMPOSITION:
4226 HIMC hIMC;
4228 ME_Style *style = ME_GetInsertStyle(editor, 0);
4229 hIMC = ITextHost_TxImmGetContext(editor->texthost);
4230 ME_DeleteSelection(editor);
4231 ME_CommitUndo(editor);
4232 ME_SaveTempStyle(editor);
4233 if (lParam & GCS_RESULTSTR)
4235 LPWSTR lpCompStr = NULL;
4236 DWORD dwBufLen;
4238 dwBufLen = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
4239 lpCompStr = HeapAlloc(GetProcessHeap(),0,dwBufLen + sizeof(WCHAR));
4240 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, lpCompStr, dwBufLen);
4241 lpCompStr[dwBufLen/sizeof(WCHAR)] = 0;
4242 ME_InsertTextFromCursor(editor,0,lpCompStr,dwBufLen/sizeof(WCHAR),style);
4244 else if (lParam & GCS_COMPSTR)
4246 LPWSTR lpCompStr = NULL;
4247 DWORD dwBufLen;
4249 dwBufLen = ImmGetCompositionStringW(hIMC, GCS_COMPSTR, NULL, 0);
4250 lpCompStr = HeapAlloc(GetProcessHeap(),0,dwBufLen + sizeof(WCHAR));
4251 ImmGetCompositionStringW(hIMC, GCS_COMPSTR, lpCompStr, dwBufLen);
4252 lpCompStr[dwBufLen/sizeof(WCHAR)] = 0;
4254 ME_InsertTextFromCursor(editor,0,lpCompStr,dwBufLen/sizeof(WCHAR),style);
4255 ME_SetSelection(editor,editor->imeStartIndex,
4256 editor->imeStartIndex + dwBufLen/sizeof(WCHAR));
4258 ME_ReleaseStyle(style);
4259 ME_UpdateRepaint(editor);
4260 return 0;
4262 case WM_IME_ENDCOMPOSITION:
4264 ME_DeleteSelection(editor);
4265 editor->imeStartIndex=-1;
4266 return 0;
4268 case EM_GETOLEINTERFACE:
4270 LPVOID *ppvObj = (LPVOID*) lParam;
4271 return CreateIRichEditOle(editor, ppvObj);
4273 case EM_GETPASSWORDCHAR:
4275 return editor->cPasswordMask;
4277 case EM_SETOLECALLBACK:
4278 if(editor->lpOleCallback)
4279 IUnknown_Release(editor->lpOleCallback);
4280 editor->lpOleCallback = (LPRICHEDITOLECALLBACK)lParam;
4281 if(editor->lpOleCallback)
4282 IUnknown_AddRef(editor->lpOleCallback);
4283 return TRUE;
4284 case EM_GETWORDBREAKPROC:
4285 return (LRESULT)editor->pfnWordBreak;
4286 case EM_SETWORDBREAKPROC:
4288 EDITWORDBREAKPROCW pfnOld = editor->pfnWordBreak;
4290 editor->pfnWordBreak = (EDITWORDBREAKPROCW)lParam;
4291 return (LRESULT)pfnOld;
4293 case EM_SETTEXTMODE:
4295 LRESULT ret;
4296 int mask = 0;
4297 int changes = 0;
4298 GETTEXTLENGTHEX how;
4300 /* CR/LF conversion required in 2.0 mode, verbatim in 1.0 mode */
4301 how.flags = GTL_CLOSE | (editor->bEmulateVersion10 ? 0 : GTL_USECRLF) | GTL_NUMCHARS;
4302 how.codepage = unicode ? 1200 : CP_ACP;
4303 ret = ME_GetTextLengthEx(editor, &how);
4304 if (!ret)
4306 /*Check for valid wParam*/
4307 if ((((wParam & TM_RICHTEXT) && ((wParam & TM_PLAINTEXT) << 1))) ||
4308 (((wParam & TM_MULTILEVELUNDO) && ((wParam & TM_SINGLELEVELUNDO) << 1))) ||
4309 (((wParam & TM_MULTICODEPAGE) && ((wParam & TM_SINGLECODEPAGE) << 1))))
4310 return 1;
4311 else
4313 if (wParam & (TM_RICHTEXT | TM_PLAINTEXT))
4315 mask |= (TM_RICHTEXT | TM_PLAINTEXT);
4316 changes |= (wParam & (TM_RICHTEXT | TM_PLAINTEXT));
4318 /*FIXME: Currently no support for undo level and code page options*/
4319 editor->mode = (editor->mode & (~mask)) | changes;
4320 return 0;
4323 return ret;
4325 case EM_SETPASSWORDCHAR:
4327 editor->cPasswordMask = wParam;
4328 ME_RewrapRepaint(editor);
4329 return 0;
4331 case EM_SETTARGETDEVICE:
4332 if (wParam == 0)
4334 BOOL new = (lParam == 0);
4335 if (editor->bWordWrap != new)
4337 editor->bWordWrap = new;
4338 ME_RewrapRepaint(editor);
4341 else FIXME("Unsupported yet non NULL device in EM_SETTARGETDEVICE\n");
4342 break;
4343 default:
4344 do_default:
4345 *phresult = S_FALSE;
4346 break;
4348 return 0L;
4351 static LRESULT RichEditWndProc_common(HWND hWnd, UINT msg, WPARAM wParam,
4352 LPARAM lParam, BOOL unicode)
4354 ME_TextEditor *editor;
4355 HRESULT hresult;
4356 LRESULT lresult = 0;
4358 TRACE("enter hwnd %p msg %04x (%s) %lx %lx, unicode %d\n",
4359 hWnd, msg, get_msg_name(msg), wParam, lParam, unicode);
4361 editor = (ME_TextEditor *)GetWindowLongPtrW(hWnd, 0);
4362 if (!editor)
4364 if (msg == WM_NCCREATE)
4366 CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
4367 ITextHost *texthost;
4369 TRACE("WM_NCCREATE: hWnd %p style 0x%08x\n", hWnd, pcs->style);
4370 texthost = ME_CreateTextHost(hWnd, FALSE);
4371 return texthost != NULL;
4373 else if (msg != WM_NCDESTROY)
4375 ERR("called with invalid hWnd %p - application bug?\n", hWnd);
4376 return 0;
4380 switch (msg)
4382 case WM_PAINT:
4384 HDC hDC;
4385 RECT rc;
4386 PAINTSTRUCT ps;
4388 hDC = BeginPaint(editor->hWnd, &ps);
4389 /* Erase area outside of the formatting rectangle */
4390 if (ps.rcPaint.top < editor->rcFormat.top)
4392 rc = ps.rcPaint;
4393 rc.bottom = editor->rcFormat.top;
4394 FillRect(hDC, &rc, editor->hbrBackground);
4395 ps.rcPaint.top = editor->rcFormat.top;
4397 if (ps.rcPaint.bottom > editor->rcFormat.bottom) {
4398 rc = ps.rcPaint;
4399 rc.top = editor->rcFormat.bottom;
4400 FillRect(hDC, &rc, editor->hbrBackground);
4401 ps.rcPaint.bottom = editor->rcFormat.bottom;
4403 if (ps.rcPaint.left < editor->rcFormat.left) {
4404 rc = ps.rcPaint;
4405 rc.right = editor->rcFormat.left;
4406 FillRect(hDC, &rc, editor->hbrBackground);
4407 ps.rcPaint.left = editor->rcFormat.left;
4409 if (ps.rcPaint.right > editor->rcFormat.right) {
4410 rc = ps.rcPaint;
4411 rc.left = editor->rcFormat.right;
4412 FillRect(hDC, &rc, editor->hbrBackground);
4413 ps.rcPaint.right = editor->rcFormat.right;
4416 ME_PaintContent(editor, hDC, FALSE, &ps.rcPaint);
4417 EndPaint(editor->hWnd, &ps);
4418 return 0;
4420 case WM_ERASEBKGND:
4422 HDC hDC = (HDC)wParam;
4423 RECT rc;
4425 if (GetUpdateRect(editor->hWnd, &rc, TRUE))
4426 FillRect(hDC, &rc, editor->hbrBackground);
4427 return 1;
4429 case EM_SETOPTIONS:
4431 DWORD dwStyle;
4432 const DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
4433 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN |
4434 ECO_SELECTIONBAR;
4435 lresult = ME_HandleMessage(editor, msg, wParam, lParam, unicode, &hresult);
4436 dwStyle = GetWindowLongW(hWnd, GWL_STYLE);
4437 dwStyle = (dwStyle & ~mask) | (lresult & mask);
4438 SetWindowLongW(hWnd, GWL_STYLE, dwStyle);
4439 return lresult;
4441 case EM_SETREADONLY:
4443 DWORD dwStyle;
4444 lresult = ME_HandleMessage(editor, msg, wParam, lParam, unicode, &hresult);
4445 dwStyle = GetWindowLongW(hWnd, GWL_STYLE);
4446 dwStyle &= ~ES_READONLY;
4447 if (wParam)
4448 dwStyle |= ES_READONLY;
4449 SetWindowLongW(hWnd, GWL_STYLE, dwStyle);
4450 return lresult;
4452 default:
4453 lresult = ME_HandleMessage(editor, msg, wParam, lParam, unicode, &hresult);
4456 if (hresult == S_FALSE)
4457 lresult = DefWindowProcW(hWnd, msg, wParam, lParam);
4459 TRACE("exit hwnd %p msg %04x (%s) %lx %lx, unicode %d -> %lu\n",
4460 hWnd, msg, get_msg_name(msg), wParam, lParam, unicode, lresult);
4462 return lresult;
4465 static LRESULT WINAPI RichEditWndProcW(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
4467 BOOL unicode = TRUE;
4469 /* Under Win9x RichEdit20W returns ANSI strings, see the tests. */
4470 if (msg == WM_GETTEXT && (GetVersion() & 0x80000000))
4471 unicode = FALSE;
4473 return RichEditWndProc_common(hWnd, msg, wParam, lParam, unicode);
4476 static LRESULT WINAPI RichEditWndProcA(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
4478 return RichEditWndProc_common(hWnd, msg, wParam, lParam, FALSE);
4481 /******************************************************************
4482 * RichEditANSIWndProc (RICHED20.10)
4484 LRESULT WINAPI RichEditANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
4486 return RichEditWndProcA(hWnd, msg, wParam, lParam);
4489 /******************************************************************
4490 * RichEdit10ANSIWndProc (RICHED20.9)
4492 LRESULT WINAPI RichEdit10ANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
4494 if (msg == WM_NCCREATE && !GetWindowLongPtrW(hWnd, 0))
4496 ITextHost *texthost;
4497 CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
4499 TRACE("WM_NCCREATE: hWnd %p style 0x%08x\n", hWnd, pcs->style);
4500 texthost = ME_CreateTextHost(hWnd, TRUE);
4501 return texthost != NULL;
4503 return RichEditANSIWndProc(hWnd, msg, wParam, lParam);
4506 void ME_SendOldNotify(ME_TextEditor *editor, int nCode)
4508 ITextHost_TxNotify(editor->texthost, nCode, NULL);
4511 int ME_CountParagraphsBetween(ME_TextEditor *editor, int from, int to)
4513 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
4514 int i = 0;
4516 while(item && item->member.para.next_para->member.para.nCharOfs <= from)
4517 item = item->member.para.next_para;
4518 if (!item)
4519 return 0;
4520 while(item && item->member.para.next_para->member.para.nCharOfs <= to) {
4521 item = item->member.para.next_para;
4522 i++;
4524 return i;
4528 int ME_GetTextW(ME_TextEditor *editor, WCHAR *buffer, int nStart,
4529 int nChars, int bCRLF)
4531 ME_DisplayItem *pRun;
4532 int nOffset, nWritten = 0;
4533 WCHAR *pStart = buffer;
4535 ME_RunOfsFromCharOfs(editor, nStart, &pRun, &nOffset);
4537 /* bCRLF flag is only honored in 2.0 and up. 1.0 must always return text verbatim */
4538 if (editor->bEmulateVersion10) bCRLF = 0;
4540 while (nChars && pRun)
4542 int nLen;
4544 if (pRun->member.run.nFlags & MERF_ENDCELL &&
4545 pRun->member.run.nFlags & MERF_ENDPARA)
4547 *buffer = '\t';
4548 nLen = 1;
4549 } else if (pRun->member.run.nFlags & MERF_ENDPARA) {
4550 if (!ME_FindItemFwd(pRun, diRun)) {
4551 /* No '\r' is appended to the last paragraph. */
4552 nLen = 0;
4553 } else if (bCRLF && nChars == 1) {
4554 nLen = 0;
4555 nChars = 0;
4556 } else {
4557 WCHAR cr_lf[] = {'\r', '\n', 0};
4558 WCHAR *szData;
4560 if (bCRLF)
4562 nLen = 2;
4563 szData = cr_lf;
4564 } else {
4565 nLen = pRun->member.run.strText->nLen;
4566 szData = pRun->member.run.strText->szData;
4568 nLen = min(nChars, nLen - nOffset);
4569 CopyMemory(buffer, szData + nOffset, sizeof(WCHAR) * nLen);
4571 } else {
4572 nLen = min(nChars, pRun->member.run.strText->nLen - nOffset);
4573 CopyMemory(buffer, pRun->member.run.strText->szData + nOffset,
4574 sizeof(WCHAR) * nLen);
4576 nChars -= nLen;
4577 nWritten += nLen;
4578 buffer += nLen;
4579 nOffset = 0;
4581 pRun = ME_FindItemFwd(pRun, diRun);
4583 *buffer = 0;
4584 TRACE("nWritten=%d, actual=%d\n", nWritten, buffer-pStart);
4585 return nWritten;
4588 static BOOL ME_RegisterEditorClass(HINSTANCE hInstance)
4590 WNDCLASSW wcW;
4591 WNDCLASSA wcA;
4593 wcW.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
4594 wcW.lpfnWndProc = RichEditWndProcW;
4595 wcW.cbClsExtra = 0;
4596 wcW.cbWndExtra = sizeof(ME_TextEditor *);
4597 wcW.hInstance = NULL; /* hInstance would register DLL-local class */
4598 wcW.hIcon = NULL;
4599 wcW.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
4600 wcW.hbrBackground = GetStockObject(NULL_BRUSH);
4601 wcW.lpszMenuName = NULL;
4603 if (is_version_nt())
4605 wcW.lpszClassName = RichEdit20W;
4606 if (!RegisterClassW(&wcW)) return FALSE;
4607 wcW.lpszClassName = RichEdit50W;
4608 if (!RegisterClassW(&wcW)) return FALSE;
4610 else
4612 /* WNDCLASSA/W have the same layout */
4613 wcW.lpszClassName = (LPCWSTR)"RichEdit20W";
4614 if (!RegisterClassA((WNDCLASSA *)&wcW)) return FALSE;
4615 wcW.lpszClassName = (LPCWSTR)"RichEdit50W";
4616 if (!RegisterClassA((WNDCLASSA *)&wcW)) return FALSE;
4619 wcA.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
4620 wcA.lpfnWndProc = RichEditWndProcA;
4621 wcA.cbClsExtra = 0;
4622 wcA.cbWndExtra = sizeof(ME_TextEditor *);
4623 wcA.hInstance = NULL; /* hInstance would register DLL-local class */
4624 wcA.hIcon = NULL;
4625 wcA.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
4626 wcA.hbrBackground = GetStockObject(NULL_BRUSH);
4627 wcA.lpszMenuName = NULL;
4628 wcA.lpszClassName = "RichEdit20A";
4629 if (!RegisterClassA(&wcA)) return FALSE;
4630 wcA.lpszClassName = "RichEdit50A";
4631 if (!RegisterClassA(&wcA)) return FALSE;
4633 return TRUE;
4636 LRESULT WINAPI REComboWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
4637 /* FIXME: Not implemented */
4638 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
4639 hWnd, msg, get_msg_name(msg), wParam, lParam);
4640 return DefWindowProcW(hWnd, msg, wParam, lParam);
4643 LRESULT WINAPI REListWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
4644 /* FIXME: Not implemented */
4645 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
4646 hWnd, msg, get_msg_name(msg), wParam, lParam);
4647 return DefWindowProcW(hWnd, msg, wParam, lParam);
4650 /******************************************************************
4651 * REExtendedRegisterClass (RICHED20.8)
4653 * FIXME undocumented
4654 * Need to check for errors and implement controls and callbacks
4656 LRESULT WINAPI REExtendedRegisterClass(void)
4658 WNDCLASSW wcW;
4659 UINT result;
4661 FIXME("semi stub\n");
4663 wcW.cbClsExtra = 0;
4664 wcW.cbWndExtra = 4;
4665 wcW.hInstance = NULL;
4666 wcW.hIcon = NULL;
4667 wcW.hCursor = NULL;
4668 wcW.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
4669 wcW.lpszMenuName = NULL;
4671 if (!ME_ListBoxRegistered)
4673 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS;
4674 wcW.lpfnWndProc = REListWndProc;
4675 wcW.lpszClassName = REListBox20W;
4676 if (RegisterClassW(&wcW)) ME_ListBoxRegistered = TRUE;
4679 if (!ME_ComboBoxRegistered)
4681 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
4682 wcW.lpfnWndProc = REComboWndProc;
4683 wcW.lpszClassName = REComboBox20W;
4684 if (RegisterClassW(&wcW)) ME_ComboBoxRegistered = TRUE;
4687 result = 0;
4688 if (ME_ListBoxRegistered)
4689 result += 1;
4690 if (ME_ComboBoxRegistered)
4691 result += 2;
4693 return result;
4696 static BOOL isurlspecial(WCHAR c)
4698 static const WCHAR special_chars[] = {'.','/','%','@','*','|','\\','+','#',0};
4699 return strchrW( special_chars, c ) != NULL;
4703 * This proc takes a selection, and scans it forward in order to select the span
4704 * of a possible URL candidate. A possible URL candidate must start with isalnum
4705 * or one of the following special characters: *|/\+%#@ and must consist entirely
4706 * of the characters allowed to start the URL, plus : (colon) which may occur
4707 * at most once, and not at either end.
4709 * sel_max == -1 indicates scan to end of text.
4711 static BOOL ME_FindNextURLCandidate(ME_TextEditor *editor, int sel_min, int sel_max,
4712 int * candidate_min, int * candidate_max)
4714 ME_DisplayItem * item;
4715 ME_DisplayItem * para;
4716 int nStart;
4717 BOOL foundColon = FALSE;
4718 WCHAR lastAcceptedChar = '\0';
4720 TRACE("sel_min = %d sel_max = %d\n", sel_min, sel_max);
4722 *candidate_min = *candidate_max = -1;
4723 ME_RunOfsFromCharOfs(editor, sel_min, &item, &nStart);
4724 if (!item) return FALSE;
4725 TRACE("nStart = %d\n", nStart);
4726 para = ME_GetParagraph(item);
4727 if (sel_max == -1) sel_max = ME_GetTextLength(editor);
4728 while (item && para->member.para.nCharOfs + item->member.run.nCharOfs + nStart < sel_max)
4730 ME_DisplayItem * next_item;
4732 if (!(item->member.run.nFlags & MERF_ENDPARA)) {
4733 /* Find start of candidate */
4734 if (*candidate_min == -1) {
4735 while (nStart < ME_StrLen(item->member.run.strText) &&
4736 !(isalnumW(item->member.run.strText->szData[nStart]) ||
4737 isurlspecial(item->member.run.strText->szData[nStart]))) {
4738 nStart++;
4740 if (nStart < ME_StrLen(item->member.run.strText) &&
4741 (isalnumW(item->member.run.strText->szData[nStart]) ||
4742 isurlspecial(item->member.run.strText->szData[nStart]))) {
4743 *candidate_min = para->member.para.nCharOfs + item->member.run.nCharOfs + nStart;
4744 lastAcceptedChar = item->member.run.strText->szData[nStart];
4745 nStart++;
4749 /* Find end of candidate */
4750 if (*candidate_min >= 0) {
4751 while (nStart < ME_StrLen(item->member.run.strText) &&
4752 (isalnumW(item->member.run.strText->szData[nStart]) ||
4753 isurlspecial(item->member.run.strText->szData[nStart]) ||
4754 (!foundColon && item->member.run.strText->szData[nStart] == ':') )) {
4755 if (item->member.run.strText->szData[nStart] == ':') foundColon = TRUE;
4756 lastAcceptedChar = item->member.run.strText->szData[nStart];
4757 nStart++;
4759 if (nStart < ME_StrLen(item->member.run.strText) &&
4760 !(isalnumW(item->member.run.strText->szData[nStart]) ||
4761 isurlspecial(item->member.run.strText->szData[nStart]) )) {
4762 *candidate_max = para->member.para.nCharOfs + item->member.run.nCharOfs + nStart;
4763 nStart++;
4764 if (lastAcceptedChar == ':') (*candidate_max)--;
4765 return TRUE;
4768 } else {
4769 /* End of paragraph: skip it if before candidate span, or terminates
4770 current active span */
4771 if (*candidate_min >= 0) {
4772 *candidate_max = para->member.para.nCharOfs + item->member.run.nCharOfs;
4773 if (lastAcceptedChar == ':') (*candidate_max)--;
4774 return TRUE;
4778 /* Reaching this point means no span was found, so get next span */
4779 next_item = ME_FindItemFwd(item, diRun);
4780 if (!next_item) {
4781 if (*candidate_min >= 0) {
4782 /* There are no further runs, so take end of text as end of candidate */
4783 *candidate_max = para->member.para.nCharOfs + item->member.run.nCharOfs + nStart;
4784 if (lastAcceptedChar == ':') (*candidate_max)--;
4785 return TRUE;
4788 item = next_item;
4789 para = ME_GetParagraph(item);
4790 nStart = 0;
4793 if (item) {
4794 if (*candidate_min >= 0) {
4795 /* There are no further runs, so take end of text as end of candidate */
4796 *candidate_max = para->member.para.nCharOfs + item->member.run.nCharOfs + nStart;
4797 if (lastAcceptedChar == ':') (*candidate_max)--;
4798 return TRUE;
4801 return FALSE;
4805 * This proc evaluates the selection and returns TRUE if it can be considered an URL
4807 static BOOL ME_IsCandidateAnURL(ME_TextEditor *editor, int sel_min, int sel_max)
4809 struct prefix_s {
4810 const char *text;
4811 int length;
4812 } prefixes[12] = {
4813 /* Code below depends on these being in decreasing length order! */
4814 {"prospero:", 10},
4815 {"telnet:", 8},
4816 {"gopher:", 8},
4817 {"mailto:", 8},
4818 {"https:", 7},
4819 {"file:", 6},
4820 {"news:", 6},
4821 {"wais:", 6},
4822 {"nntp:", 6},
4823 {"http:", 5},
4824 {"www.", 5},
4825 {"ftp:", 5},
4827 LPWSTR bufferW = NULL;
4828 WCHAR bufW[32];
4829 unsigned int i;
4831 if (sel_max == -1) sel_max = ME_GetTextLength(editor);
4832 assert(sel_min <= sel_max);
4833 for (i = 0; i < sizeof(prefixes) / sizeof(struct prefix_s); i++)
4835 if (sel_max - sel_min < prefixes[i].length) continue;
4836 if (bufferW == NULL) {
4837 bufferW = heap_alloc((sel_max - sel_min + 1) * sizeof(WCHAR));
4839 ME_GetTextW(editor, bufferW, sel_min, min(sel_max - sel_min, lstrlenA(prefixes[i].text)), 0);
4840 MultiByteToWideChar(CP_ACP, 0, prefixes[i].text, -1, bufW, 32);
4841 if (!lstrcmpW(bufW, bufferW))
4843 heap_free(bufferW);
4844 return TRUE;
4847 heap_free(bufferW);
4848 return FALSE;
4852 * This proc walks through the indicated selection and evaluates whether each
4853 * section identified by ME_FindNextURLCandidate and in-between sections have
4854 * their proper CFE_LINK attributes set or unset. If the CFE_LINK attribute is
4855 * not what it is supposed to be, this proc sets or unsets it as appropriate.
4857 * Returns TRUE if at least one section was modified.
4859 static BOOL ME_UpdateLinkAttribute(ME_TextEditor *editor, int sel_min, int sel_max)
4861 BOOL modified = FALSE;
4862 int cMin, cMax;
4864 if (sel_max == -1) sel_max = ME_GetTextLength(editor);
4867 int beforeURL[2];
4868 int inURL[2];
4869 CHARFORMAT2W link;
4871 if (ME_FindNextURLCandidate(editor, sel_min, sel_max, &cMin, &cMax))
4873 /* Section before candidate is not an URL */
4874 beforeURL[0] = sel_min;
4875 beforeURL[1] = cMin;
4877 if (ME_IsCandidateAnURL(editor, cMin, cMax))
4879 inURL[0] = cMin; inURL[1] = cMax;
4881 else
4883 beforeURL[1] = cMax;
4884 inURL[0] = inURL[1] = -1;
4886 sel_min = cMax;
4888 else
4890 /* No more candidates until end of selection */
4891 beforeURL[0] = sel_min;
4892 beforeURL[1] = sel_max;
4893 inURL[0] = inURL[1] = -1;
4894 sel_min = sel_max;
4897 if (beforeURL[0] < beforeURL[1])
4899 /* CFE_LINK effect should be consistently unset */
4900 link.cbSize = sizeof(link);
4901 ME_GetCharFormat(editor, beforeURL[0], beforeURL[1], &link);
4902 if (!(link.dwMask & CFM_LINK) || (link.dwEffects & CFE_LINK))
4904 /* CFE_LINK must be unset from this range */
4905 memset(&link, 0, sizeof(CHARFORMAT2W));
4906 link.cbSize = sizeof(link);
4907 link.dwMask = CFM_LINK;
4908 link.dwEffects = 0;
4909 ME_SetCharFormat(editor, beforeURL[0], beforeURL[1] - beforeURL[0], &link);
4910 modified = TRUE;
4913 if (inURL[0] < inURL[1])
4915 /* CFE_LINK effect should be consistently set */
4916 link.cbSize = sizeof(link);
4917 ME_GetCharFormat(editor, inURL[0], inURL[1], &link);
4918 if (!(link.dwMask & CFM_LINK) || !(link.dwEffects & CFE_LINK))
4920 /* CFE_LINK must be set on this range */
4921 memset(&link, 0, sizeof(CHARFORMAT2W));
4922 link.cbSize = sizeof(link);
4923 link.dwMask = CFM_LINK;
4924 link.dwEffects = CFE_LINK;
4925 ME_SetCharFormat(editor, inURL[0], inURL[1] - inURL[0], &link);
4926 modified = TRUE;
4929 } while (sel_min < sel_max);
4930 return modified;