push 8724397e7ede0f147b6e05994a72142d44d4fecc
[wine/hacks.git] / dlls / riched20 / editor.c
blob09feb960da445295e93ca480a73ef011301c6c6b
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 = cursor.pPara;
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 = info->editor->pCursors[0].pPara;
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 = info->editor->pCursors[0].pPara;
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].pPara = ME_GetParagraph(run);
1013 info->editor->pCursors[1].nOffset = 0;
1014 nOfs = ME_GetCursorOfs(info->editor, 1);
1015 nChars = ME_GetCursorOfs(info->editor, 0) - nOfs;
1016 ME_InternalDeleteText(info->editor, nOfs, nChars, TRUE);
1019 para = ME_InsertTableRowEndFromCursor(info->editor);
1020 para->member.para.pFmt->dxOffset = abs(info->tableDef->gapH);
1021 para->member.para.pFmt->dxStartIndent = info->tableDef->leftEdge;
1022 ME_ApplyBorderProperties(info, &para->member.para.border,
1023 tableDef->border);
1024 info->nestingLevel--;
1025 if (!info->nestingLevel)
1027 if (info->canInheritInTbl) {
1028 tableDef->tableRowStart = para;
1029 } else {
1030 while (info->tableDef) {
1031 tableDef = info->tableDef;
1032 info->tableDef = tableDef->parent;
1033 heap_free(tableDef);
1036 } else {
1037 info->tableDef = tableDef->parent;
1038 heap_free(tableDef);
1040 } else { /* v1.0 - v3.0 */
1041 WCHAR endl = '\r';
1042 ME_DisplayItem *para = info->editor->pCursors[0].pPara;
1043 PARAFORMAT2 *pFmt = para->member.para.pFmt;
1044 pFmt->dxOffset = info->tableDef->gapH;
1045 pFmt->dxStartIndent = info->tableDef->leftEdge;
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 = info->editor->pCursors[0].pPara;
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 = editor->pCursors->pPara;
1408 if (para->member.para.nFlags & MEPF_ROWEND)
1410 para = para->member.para.next_para;
1411 editor->pCursors[0].pPara = para;
1412 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
1413 editor->pCursors[0].nOffset = 0;
1415 if (para->member.para.nFlags & MEPF_ROWSTART)
1417 para = para->member.para.next_para;
1418 editor->pCursors[0].pPara = para;
1419 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
1420 editor->pCursors[0].nOffset = 0;
1422 editor->pCursors[1] = editor->pCursors[0];
1423 } else { /* v1.0 - 3.0 */
1424 if (editor->pCursors[0].pRun->member.run.nFlags & MERF_ENDPARA &&
1425 ME_IsInTable(editor->pCursors[0].pRun))
1426 return 0;
1429 else {
1430 style = editor->pBuffer->pDefaultStyle;
1431 ME_AddRefStyle(style);
1432 ME_SetSelection(editor, 0, 0);
1433 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor), FALSE);
1434 from = to = 0;
1435 ME_ClearTempStyle(editor);
1436 ME_SetDefaultParaFormat(editor->pCursors[0].pPara->member.para.pFmt);
1440 /* Back up undo mode to a local variable */
1441 nUndoMode = editor->nUndoMode;
1443 /* Only create an undo if SFF_SELECTION is set */
1444 if (!(format & SFF_SELECTION))
1445 editor->nUndoMode = umIgnore;
1447 inStream.editstream = stream;
1448 inStream.editstream->dwError = 0;
1449 inStream.dwSize = 0;
1450 inStream.dwUsed = 0;
1452 if (format & SF_RTF)
1454 /* Check if it's really RTF, and if it is not, use plain text */
1455 ME_StreamInFill(&inStream);
1456 if (!inStream.editstream->dwError)
1458 if ((!editor->bEmulateVersion10 && strncmp(inStream.buffer, "{\\rtf", 5) && strncmp(inStream.buffer, "{\\urtf", 6))
1459 || (editor->bEmulateVersion10 && *inStream.buffer != '{'))
1461 invalidRTF = TRUE;
1462 inStream.editstream->dwError = -16;
1467 if (!invalidRTF && !inStream.editstream->dwError)
1469 if (format & SF_RTF) {
1470 /* setup the RTF parser */
1471 memset(&parser, 0, sizeof parser);
1472 RTFSetEditStream(&parser, &inStream);
1473 parser.rtfFormat = format&(SF_TEXT|SF_RTF);
1474 parser.editor = editor;
1475 parser.style = style;
1476 WriterInit(&parser);
1477 RTFInit(&parser);
1478 RTFSetReadHook(&parser, ME_RTFReadHook);
1479 RTFSetDestinationCallback(&parser, rtfPict, ME_RTFReadPictGroup);
1480 RTFSetDestinationCallback(&parser, rtfObject, ME_RTFReadObjectGroup);
1481 if (!parser.editor->bEmulateVersion10) /* v4.1 */
1483 RTFSetDestinationCallback(&parser, rtfNoNestTables, RTFSkipGroup);
1484 RTFSetDestinationCallback(&parser, rtfNestTableProps, RTFReadGroup);
1486 BeginFile(&parser);
1488 /* do the parsing */
1489 RTFRead(&parser);
1490 RTFFlushOutputBuffer(&parser);
1491 if (!editor->bEmulateVersion10) { /* v4.1 */
1492 if (parser.tableDef && parser.tableDef->tableRowStart &&
1493 (parser.nestingLevel > 0 || parser.canInheritInTbl))
1495 /* Delete any incomplete table row at the end of the rich text. */
1496 int nOfs, nChars;
1497 ME_DisplayItem *pCell;
1498 ME_DisplayItem *para;
1500 parser.rtfMinor = rtfRow;
1501 /* Complete the table row before deleting it.
1502 * By doing it this way we will have the current paragraph format set
1503 * properly to reflect that is not in the complete table, and undo items
1504 * will be added for this change to the current paragraph format. */
1505 if (parser.nestingLevel > 0)
1507 while (parser.nestingLevel > 1)
1508 ME_RTFSpecialCharHook(&parser); /* Decrements nestingLevel */
1509 para = parser.tableDef->tableRowStart;
1510 ME_RTFSpecialCharHook(&parser);
1511 } else {
1512 para = parser.tableDef->tableRowStart;
1513 ME_RTFSpecialCharHook(&parser);
1514 assert(para->member.para.nFlags & MEPF_ROWEND);
1515 para = para->member.para.next_para;
1517 pCell = para->member.para.pCell;
1519 editor->pCursors[1].pPara = para;
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 ME_RunOfsFromCharOfs(editor, nMin - 1, NULL, &item, &nStart);
1697 wLastChar = item->member.run.strText->szData[nStart];
1700 ME_RunOfsFromCharOfs(editor, nMin, &para, &item, &nStart);
1702 while (item
1703 && para->member.para.nCharOfs + item->member.run.nCharOfs + nStart + nLen <= nMax)
1705 ME_DisplayItem *pCurItem = item;
1706 int nCurStart = nStart;
1707 int nMatched = 0;
1709 while (pCurItem && ME_CharCompare(pCurItem->member.run.strText->szData[nCurStart + nMatched], text[nMatched], (flags & FR_MATCHCASE)))
1711 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
1712 break;
1714 nMatched++;
1715 if (nMatched == nLen)
1717 ME_DisplayItem *pNextItem = pCurItem;
1718 int nNextStart = nCurStart;
1719 WCHAR wNextChar;
1721 /* Check to see if next character is a whitespace */
1722 if (flags & FR_WHOLEWORD)
1724 if (nCurStart + nMatched == pCurItem->member.run.strText->nLen)
1726 pNextItem = ME_FindItemFwd(pCurItem, diRun);
1727 nNextStart = -nMatched;
1730 if (pNextItem)
1731 wNextChar = pNextItem->member.run.strText->szData[nNextStart + nMatched];
1732 else
1733 wNextChar = ' ';
1735 if (isalnumW(wNextChar))
1736 break;
1739 nStart += para->member.para.nCharOfs + item->member.run.nCharOfs;
1740 if (chrgText)
1742 chrgText->cpMin = nStart;
1743 chrgText->cpMax = nStart + nLen;
1745 TRACE("found at %d-%d\n", nStart, nStart + nLen);
1746 return nStart;
1748 if (nCurStart + nMatched == pCurItem->member.run.strText->nLen)
1750 pCurItem = ME_FindItemFwd(pCurItem, diRun);
1751 nCurStart = -nMatched;
1754 if (pCurItem)
1755 wLastChar = pCurItem->member.run.strText->szData[nCurStart + nMatched];
1756 else
1757 wLastChar = ' ';
1759 nStart++;
1760 if (nStart == item->member.run.strText->nLen)
1762 item = ME_FindItemFwd(item, diRun);
1763 para = ME_GetParagraph(item);
1764 nStart = 0;
1768 else /* Backward search */
1770 /* If possible, find the character after where the search ends */
1771 if ((flags & FR_WHOLEWORD) && nMax < nTextLen - 1)
1773 ME_RunOfsFromCharOfs(editor, nMax + 1, NULL, &item, &nEnd);
1774 wLastChar = item->member.run.strText->szData[nEnd];
1777 ME_RunOfsFromCharOfs(editor, nMax, &para, &item, &nEnd);
1779 while (item
1780 && para->member.para.nCharOfs + item->member.run.nCharOfs + nEnd - nLen >= nMin)
1782 ME_DisplayItem *pCurItem = item;
1783 int nCurEnd = nEnd;
1784 int nMatched = 0;
1786 if (nCurEnd == 0)
1788 pCurItem = ME_FindItemBack(pCurItem, diRun);
1789 nCurEnd = pCurItem->member.run.strText->nLen + nMatched;
1792 while (pCurItem && ME_CharCompare(pCurItem->member.run.strText->szData[nCurEnd - nMatched - 1], text[nLen - nMatched - 1], (flags & FR_MATCHCASE)))
1794 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
1795 break;
1797 nMatched++;
1798 if (nMatched == nLen)
1800 ME_DisplayItem *pPrevItem = pCurItem;
1801 int nPrevEnd = nCurEnd;
1802 WCHAR wPrevChar;
1804 /* Check to see if previous character is a whitespace */
1805 if (flags & FR_WHOLEWORD)
1807 if (nPrevEnd - nMatched == 0)
1809 pPrevItem = ME_FindItemBack(pCurItem, diRun);
1810 if (pPrevItem)
1811 nPrevEnd = pPrevItem->member.run.strText->nLen + nMatched;
1814 if (pPrevItem)
1815 wPrevChar = pPrevItem->member.run.strText->szData[nPrevEnd - nMatched - 1];
1816 else
1817 wPrevChar = ' ';
1819 if (isalnumW(wPrevChar))
1820 break;
1823 nStart = ME_GetParagraph(pCurItem)->member.para.nCharOfs
1824 + pCurItem->member.run.nCharOfs + nCurEnd - nMatched;
1825 if (chrgText)
1827 chrgText->cpMin = nStart;
1828 chrgText->cpMax = nStart + nLen;
1830 TRACE("found at %d-%d\n", nStart, nStart + nLen);
1831 return nStart;
1833 if (nCurEnd - nMatched == 0)
1835 pCurItem = ME_FindItemBack(pCurItem, diRun);
1836 /* Don't care about pCurItem becoming NULL here; it's already taken
1837 * care of in the exterior loop condition */
1838 nCurEnd = pCurItem->member.run.strText->nLen + nMatched;
1841 if (pCurItem)
1842 wLastChar = pCurItem->member.run.strText->szData[nCurEnd - nMatched - 1];
1843 else
1844 wLastChar = ' ';
1846 nEnd--;
1847 if (nEnd < 0)
1849 item = ME_FindItemBack(item, diRun);
1850 para = ME_GetParagraph(item);
1851 nEnd = item->member.run.strText->nLen;
1855 TRACE("not found\n");
1856 if (chrgText)
1857 chrgText->cpMin = chrgText->cpMax = -1;
1858 return -1;
1861 static int ME_GetTextEx(ME_TextEditor *editor, GETTEXTEX *ex, LPARAM pText)
1863 int nStart, nCount; /* in chars */
1865 if (ex->flags & ~(GT_SELECTION | GT_USECRLF))
1866 FIXME("GETTEXTEX flags 0x%08x not supported\n", ex->flags & ~(GT_SELECTION | GT_USECRLF));
1868 if (ex->flags & GT_SELECTION)
1870 ME_GetSelection(editor, &nStart, &nCount);
1871 nCount -= nStart;
1873 else
1875 nStart = 0;
1876 nCount = 0x7fffffff;
1878 if (ex->codepage == 1200)
1880 nCount = min(nCount, ex->cb / sizeof(WCHAR) - 1);
1881 return ME_GetTextW(editor, (LPWSTR)pText, nStart, nCount, ex->flags & GT_USECRLF);
1883 else
1885 /* potentially each char may be a CR, why calculate the exact value with O(N) when
1886 we can just take a bigger buffer? :)
1887 The above assumption still holds with CR/LF counters, since CR->CRLF expansion
1888 occurs only in richedit 2.0 mode, in which line breaks have only one CR
1890 int crlfmul = (ex->flags & GT_USECRLF) ? 2 : 1;
1891 LPWSTR buffer;
1892 DWORD buflen = ex->cb;
1893 LRESULT rc;
1894 DWORD flags = 0;
1896 nCount = min(nCount, ex->cb - 1);
1897 buffer = heap_alloc((crlfmul*nCount + 1) * sizeof(WCHAR));
1899 buflen = ME_GetTextW(editor, buffer, nStart, nCount, ex->flags & GT_USECRLF);
1900 rc = WideCharToMultiByte(ex->codepage, flags, buffer, buflen+1,
1901 (LPSTR)pText, ex->cb, ex->lpDefaultChar, ex->lpUsedDefChar);
1902 if (rc) rc--; /* do not count 0 terminator */
1904 heap_free(buffer);
1905 return rc;
1909 static int ME_GetTextRange(ME_TextEditor *editor, TEXTRANGEW *rng, BOOL unicode)
1911 if (unicode)
1912 return ME_GetTextW(editor, rng->lpstrText, rng->chrg.cpMin,
1913 rng->chrg.cpMax-rng->chrg.cpMin, 0);
1914 else
1916 int nLen = rng->chrg.cpMax-rng->chrg.cpMin;
1917 WCHAR *p = ALLOC_N_OBJ(WCHAR, nLen+1);
1918 int nChars = ME_GetTextW(editor, p, rng->chrg.cpMin, nLen, 0);
1919 /* FIXME this is a potential security hole (buffer overrun)
1920 if you know more about wchar->mbyte conversion please explain
1922 WideCharToMultiByte(CP_ACP, 0, p, nChars+1, (char *)rng->lpstrText, nLen+1, NULL, NULL);
1923 FREE_OBJ(p);
1924 return nChars;
1928 typedef struct tagME_GlobalDestStruct
1930 HGLOBAL hData;
1931 int nLength;
1932 } ME_GlobalDestStruct;
1934 static DWORD CALLBACK ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1936 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
1937 int i;
1938 WORD *pSrc, *pDest;
1940 cb = cb >> 1;
1941 pDest = (WORD *)lpBuff;
1942 pSrc = GlobalLock(pData->hData);
1943 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
1944 pDest[i] = pSrc[pData->nLength+i];
1946 pData->nLength += i;
1947 *pcb = 2*i;
1948 GlobalUnlock(pData->hData);
1949 return 0;
1952 static DWORD CALLBACK ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1954 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
1955 int i;
1956 BYTE *pSrc, *pDest;
1958 pDest = lpBuff;
1959 pSrc = GlobalLock(pData->hData);
1960 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
1961 pDest[i] = pSrc[pData->nLength+i];
1963 pData->nLength += i;
1964 *pcb = i;
1965 GlobalUnlock(pData->hData);
1966 return 0;
1969 static BOOL ME_Paste(ME_TextEditor *editor)
1971 DWORD dwFormat = 0;
1972 EDITSTREAM es;
1973 ME_GlobalDestStruct gds;
1974 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
1975 UINT cf = 0;
1977 if (IsClipboardFormatAvailable(nRTFFormat))
1978 cf = nRTFFormat, dwFormat = SF_RTF;
1979 else if (IsClipboardFormatAvailable(CF_UNICODETEXT))
1980 cf = CF_UNICODETEXT, dwFormat = SF_TEXT|SF_UNICODE;
1981 else
1982 return FALSE;
1984 if (!OpenClipboard(editor->hWnd))
1985 return FALSE;
1986 gds.hData = GetClipboardData(cf);
1987 gds.nLength = 0;
1988 es.dwCookie = (DWORD_PTR)&gds;
1989 es.pfnCallback = dwFormat == SF_RTF ? ME_ReadFromHGLOBALRTF : ME_ReadFromHGLOBALUnicode;
1990 ME_StreamIn(editor, dwFormat|SFF_SELECTION, &es, FALSE);
1992 CloseClipboard();
1993 return TRUE;
1996 static BOOL ME_Copy(ME_TextEditor *editor, CHARRANGE *range)
1998 LPDATAOBJECT dataObj = NULL;
1999 HRESULT hr = S_OK;
2001 if (editor->cPasswordMask)
2002 return FALSE; /* Copying or Cutting masked text isn't allowed */
2004 if(editor->lpOleCallback)
2005 hr = IRichEditOleCallback_GetClipboardData(editor->lpOleCallback, range, RECO_COPY, &dataObj);
2006 if(FAILED(hr) || !dataObj)
2007 hr = ME_GetDataObject(editor, range, &dataObj);
2008 if(SUCCEEDED(hr)) {
2009 hr = OleSetClipboard(dataObj);
2010 IDataObject_Release(dataObj);
2012 return SUCCEEDED(hr) != 0;
2015 /* helper to send a msg filter notification */
2016 static BOOL
2017 ME_FilterEvent(ME_TextEditor *editor, UINT msg, WPARAM* wParam, LPARAM* lParam)
2019 MSGFILTER msgf;
2021 msgf.nmhdr.code = EN_MSGFILTER;
2022 msgf.msg = msg;
2023 msgf.wParam = *wParam;
2024 msgf.lParam = *lParam;
2025 if (ITextHost_TxNotify(editor->texthost, msgf.nmhdr.code, &msgf) == S_OK)
2026 return FALSE;
2027 *wParam = msgf.wParam;
2028 *lParam = msgf.lParam;
2029 msgf.wParam = *wParam;
2031 return TRUE;
2034 static void ME_UpdateSelectionLinkAttribute(ME_TextEditor *editor)
2036 ME_DisplayItem *startPara, *endPara;
2037 ME_DisplayItem *prev_para;
2038 int from, to;
2040 ME_GetSelection(editor, &from, &to);
2042 /* Find paragraph previous to the one that contains start cursor */
2043 ME_RunOfsFromCharOfs(editor, from, &startPara, NULL, NULL);
2044 prev_para = startPara->member.para.prev_para;
2045 if (prev_para->type == diParagraph) startPara = prev_para;
2047 /* Find paragraph that contains end cursor */
2048 ME_RunOfsFromCharOfs(editor, to, &endPara, NULL, NULL);
2049 endPara = endPara->member.para.next_para;
2051 ME_UpdateLinkAttribute(editor,
2052 startPara->member.para.nCharOfs,
2053 endPara->member.para.nCharOfs);
2056 static BOOL
2057 ME_KeyDown(ME_TextEditor *editor, WORD nKey)
2059 BOOL ctrl_is_down = GetKeyState(VK_CONTROL) & 0x8000;
2060 BOOL shift_is_down = GetKeyState(VK_SHIFT) & 0x8000;
2062 if (editor->bMouseCaptured)
2063 return FALSE;
2064 if (nKey != VK_SHIFT && nKey != VK_CONTROL && nKey != VK_MENU)
2065 editor->nSelectionType = stPosition;
2067 switch (nKey)
2069 case VK_LEFT:
2070 case VK_RIGHT:
2071 case VK_HOME:
2072 case VK_END:
2073 editor->nUDArrowX = -1;
2074 /* fall through */
2075 case VK_UP:
2076 case VK_DOWN:
2077 case VK_PRIOR:
2078 case VK_NEXT:
2079 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
2080 ME_ArrowKey(editor, nKey, shift_is_down, ctrl_is_down);
2081 return TRUE;
2082 case VK_BACK:
2083 case VK_DELETE:
2084 editor->nUDArrowX = -1;
2085 /* FIXME backspace and delete aren't the same, they act different wrt paragraph style of the merged paragraph */
2086 if (editor->styleFlags & ES_READONLY)
2087 return FALSE;
2088 if (ME_IsSelection(editor))
2090 ME_DeleteSelection(editor);
2091 ME_CommitUndo(editor);
2093 else if (nKey == VK_DELETE)
2095 /* Delete stops group typing.
2096 * (See MSDN remarks on EM_STOPGROUPTYPING message) */
2097 ME_DeleteTextAtCursor(editor, 1, 1);
2098 ME_CommitUndo(editor);
2100 else if (ME_ArrowKey(editor, VK_LEFT, FALSE, FALSE))
2102 BOOL bDeletionSucceeded;
2103 /* Backspace can be grouped for a single undo */
2104 ME_ContinueCoalescingTransaction(editor);
2105 bDeletionSucceeded = ME_DeleteTextAtCursor(editor, 1, 1);
2106 if (!bDeletionSucceeded && !editor->bEmulateVersion10) { /* v4.1 */
2107 /* Deletion was prevented so the cursor is moved back to where it was.
2108 * (e.g. this happens when trying to delete cell boundaries)
2110 ME_ArrowKey(editor, VK_RIGHT, FALSE, FALSE);
2112 ME_CommitCoalescingUndo(editor);
2114 else
2115 return TRUE;
2116 ME_MoveCursorFromTableRowStartParagraph(editor);
2117 ME_UpdateSelectionLinkAttribute(editor);
2118 ME_UpdateRepaint(editor);
2119 ME_SendRequestResize(editor, FALSE);
2120 return TRUE;
2121 case VK_RETURN:
2122 if (editor->styleFlags & ES_MULTILINE)
2124 ME_Cursor cursor = editor->pCursors[0];
2125 ME_DisplayItem *para = cursor.pPara;
2126 int from, to;
2127 const WCHAR endl = '\r';
2128 ME_Style *style;
2130 if (editor->styleFlags & ES_READONLY) {
2131 MessageBeep(MB_ICONERROR);
2132 return TRUE;
2135 ME_GetSelection(editor, &from, &to);
2136 if (editor->nTextLimit > ME_GetTextLength(editor) - (to-from))
2138 if (!editor->bEmulateVersion10) { /* v4.1 */
2139 if (para->member.para.nFlags & MEPF_ROWEND) {
2140 /* Add a new table row after this row. */
2141 para = ME_AppendTableRow(editor, para);
2142 para = para->member.para.next_para;
2143 editor->pCursors[0].pPara = para;
2144 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2145 editor->pCursors[0].nOffset = 0;
2146 editor->pCursors[1] = editor->pCursors[0];
2147 ME_CommitUndo(editor);
2148 ME_CheckTablesForCorruption(editor);
2149 ME_UpdateRepaint(editor);
2150 return TRUE;
2152 else if (para == editor->pCursors[1].pPara &&
2153 cursor.nOffset + cursor.pRun->member.run.nCharOfs == 0 &&
2154 para->member.para.prev_para->member.para.nFlags & MEPF_ROWSTART &&
2155 !para->member.para.prev_para->member.para.nCharOfs)
2157 /* Insert a newline before the table. */
2158 para = para->member.para.prev_para;
2159 para->member.para.nFlags &= ~MEPF_ROWSTART;
2160 editor->pCursors[0].pPara = para;
2161 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2162 editor->pCursors[1] = editor->pCursors[0];
2163 ME_InsertTextFromCursor(editor, 0, &endl, 1,
2164 editor->pCursors[0].pRun->member.run.style);
2165 para = editor->pBuffer->pFirst->member.para.next_para;
2166 ME_SetDefaultParaFormat(para->member.para.pFmt);
2167 para->member.para.nFlags = MEPF_REWRAP;
2168 editor->pCursors[0].pPara = para;
2169 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2170 editor->pCursors[1] = editor->pCursors[0];
2171 para->member.para.next_para->member.para.nFlags |= MEPF_ROWSTART;
2172 ME_CommitCoalescingUndo(editor);
2173 ME_CheckTablesForCorruption(editor);
2174 ME_UpdateRepaint(editor);
2175 return TRUE;
2177 } else { /* v1.0 - 3.0 */
2178 ME_DisplayItem *para = cursor.pPara;
2179 if (ME_IsInTable(para))
2181 if (cursor.pRun->member.run.nFlags & MERF_ENDPARA)
2183 if (from == to) {
2184 ME_ContinueCoalescingTransaction(editor);
2185 para = ME_AppendTableRow(editor, para);
2186 editor->pCursors[0].pPara = para;
2187 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2188 editor->pCursors[0].nOffset = 0;
2189 editor->pCursors[1] = editor->pCursors[0];
2190 ME_CommitCoalescingUndo(editor);
2191 ME_UpdateRepaint(editor);
2192 return TRUE;
2194 } else {
2195 ME_ContinueCoalescingTransaction(editor);
2196 if (cursor.pRun->member.run.nCharOfs + cursor.nOffset == 0 &&
2197 !ME_IsInTable(para->member.para.prev_para))
2199 /* Insert newline before table */
2200 cursor.pRun = ME_FindItemBack(para, diRun);
2201 if (cursor.pRun) {
2202 editor->pCursors[0].pRun = cursor.pRun;
2203 editor->pCursors[0].pPara = para->member.para.prev_para;
2205 editor->pCursors[0].nOffset = 0;
2206 editor->pCursors[1] = editor->pCursors[0];
2207 ME_InsertTextFromCursor(editor, 0, &endl, 1,
2208 editor->pCursors[0].pRun->member.run.style);
2209 } else {
2210 editor->pCursors[1] = editor->pCursors[0];
2211 para = ME_AppendTableRow(editor, para);
2212 editor->pCursors[0].pPara = para;
2213 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2214 editor->pCursors[0].nOffset = 0;
2215 editor->pCursors[1] = editor->pCursors[0];
2217 ME_CommitCoalescingUndo(editor);
2218 ME_UpdateRepaint(editor);
2219 return TRUE;
2224 style = ME_GetInsertStyle(editor, 0);
2225 ME_SaveTempStyle(editor);
2226 ME_ContinueCoalescingTransaction(editor);
2227 if (shift_is_down)
2228 ME_InsertEndRowFromCursor(editor, 0);
2229 else
2230 ME_InsertTextFromCursor(editor, 0, &endl, 1, style);
2231 ME_ReleaseStyle(style);
2232 ME_CommitCoalescingUndo(editor);
2233 SetCursor(NULL);
2235 if (editor->AutoURLDetect_bEnable)
2236 ME_UpdateSelectionLinkAttribute(editor);
2238 ME_UpdateRepaint(editor);
2240 return TRUE;
2242 break;
2243 case 'A':
2244 if (ctrl_is_down)
2246 ME_SetSelection(editor, 0, -1);
2247 return TRUE;
2249 break;
2250 case 'V':
2251 if (ctrl_is_down)
2252 return ME_Paste(editor);
2253 break;
2254 case 'C':
2255 case 'X':
2256 if (ctrl_is_down)
2258 CHARRANGE range;
2259 BOOL result;
2261 ME_GetSelection(editor, &range.cpMin, &range.cpMax);
2262 result = ME_Copy(editor, &range);
2263 if (result && nKey == 'X')
2265 ME_InternalDeleteText(editor, range.cpMin, range.cpMax-range.cpMin, FALSE);
2266 ME_CommitUndo(editor);
2267 ME_UpdateRepaint(editor);
2269 return result;
2271 break;
2272 case 'Z':
2273 if (ctrl_is_down)
2275 ME_Undo(editor);
2276 return TRUE;
2278 break;
2279 case 'Y':
2280 if (ctrl_is_down)
2282 ME_Redo(editor);
2283 return TRUE;
2285 break;
2287 default:
2288 if (nKey != VK_SHIFT && nKey != VK_CONTROL && nKey && nKey != VK_MENU)
2289 editor->nUDArrowX = -1;
2290 if (ctrl_is_down)
2292 if (nKey == 'W')
2294 CHARFORMAT2W chf;
2295 char buf[2048];
2296 chf.cbSize = sizeof(chf);
2298 ME_GetSelectionCharFormat(editor, &chf);
2299 ME_DumpStyleToBuf(&chf, buf);
2300 MessageBoxA(NULL, buf, "Style dump", MB_OK);
2302 if (nKey == 'Q')
2304 ME_CheckCharOffsets(editor);
2308 return FALSE;
2311 static LRESULT ME_Char(ME_TextEditor *editor, WPARAM charCode,
2312 LPARAM flags, BOOL unicode)
2314 WCHAR wstr;
2316 if (editor->bMouseCaptured)
2317 return 0;
2319 if (unicode)
2320 wstr = (WCHAR)charCode;
2321 else
2323 CHAR charA = charCode;
2324 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &wstr, 1);
2327 if (editor->styleFlags & ES_READONLY) {
2328 MessageBeep(MB_ICONERROR);
2329 return 0; /* FIXME really 0 ? */
2332 if ((unsigned)wstr >= ' ' || wstr == '\t')
2334 ME_Cursor cursor = editor->pCursors[0];
2335 ME_DisplayItem *para = cursor.pPara;
2336 int from, to;
2337 BOOL ctrl_is_down = GetKeyState(VK_CONTROL) & 0x8000;
2338 ME_GetSelection(editor, &from, &to);
2339 if (wstr == '\t' &&
2340 /* v4.1 allows tabs to be inserted with ctrl key down */
2341 !(ctrl_is_down && !editor->bEmulateVersion10))
2343 ME_DisplayItem *para;
2344 BOOL bSelectedRow = FALSE;
2346 para = cursor.pPara;
2347 if (ME_IsSelection(editor) &&
2348 cursor.pRun->member.run.nCharOfs + cursor.nOffset == 0 &&
2349 to == ME_GetCursorOfs(editor, 0) &&
2350 para->member.para.prev_para->type == diParagraph)
2352 para = para->member.para.prev_para;
2353 bSelectedRow = TRUE;
2355 if (ME_IsInTable(para))
2357 ME_TabPressedInTable(editor, bSelectedRow);
2358 ME_CommitUndo(editor);
2359 return 0;
2361 } else if (!editor->bEmulateVersion10) { /* v4.1 */
2362 if (para->member.para.nFlags & MEPF_ROWEND) {
2363 if (from == to) {
2364 para = para->member.para.next_para;
2365 if (para->member.para.nFlags & MEPF_ROWSTART)
2366 para = para->member.para.next_para;
2367 editor->pCursors[0].pPara = para;
2368 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2369 editor->pCursors[0].nOffset = 0;
2370 editor->pCursors[1] = editor->pCursors[0];
2373 } else { /* v1.0 - 3.0 */
2374 if (ME_IsInTable(cursor.pRun) &&
2375 cursor.pRun->member.run.nFlags & MERF_ENDPARA &&
2376 from == to)
2378 /* Text should not be inserted at the end of the table. */
2379 MessageBeep(-1);
2380 return 0;
2383 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
2384 /* WM_CHAR is restricted to nTextLimit */
2385 if(editor->nTextLimit > ME_GetTextLength(editor) - (to-from))
2387 ME_Style *style = ME_GetInsertStyle(editor, 0);
2388 ME_SaveTempStyle(editor);
2389 ME_ContinueCoalescingTransaction(editor);
2390 ME_InsertTextFromCursor(editor, 0, &wstr, 1, style);
2391 ME_ReleaseStyle(style);
2392 ME_CommitCoalescingUndo(editor);
2393 ITextHost_TxSetCursor(editor->texthost, NULL, FALSE);
2396 if (editor->AutoURLDetect_bEnable) ME_UpdateSelectionLinkAttribute(editor);
2398 ME_UpdateRepaint(editor);
2400 return 0;
2403 /* Process the message and calculate the new click count.
2405 * returns: The click count if it is mouse down event, else returns 0. */
2406 static int ME_CalculateClickCount(ME_TextEditor *editor, UINT msg, WPARAM wParam,
2407 LPARAM lParam)
2409 static int clickNum = 0;
2410 if (msg < WM_MOUSEFIRST || msg > WM_MOUSELAST)
2411 return 0;
2413 if ((msg == WM_LBUTTONDBLCLK) ||
2414 (msg == WM_RBUTTONDBLCLK) ||
2415 (msg == WM_MBUTTONDBLCLK) ||
2416 (msg == WM_XBUTTONDBLCLK))
2418 msg -= (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN);
2421 if ((msg == WM_LBUTTONDOWN) ||
2422 (msg == WM_RBUTTONDOWN) ||
2423 (msg == WM_MBUTTONDOWN) ||
2424 (msg == WM_XBUTTONDOWN))
2426 static MSG prevClickMsg;
2427 MSG clickMsg;
2428 /* Compare the editor instead of the hwnd so that the this
2429 * can still be done for windowless richedit controls. */
2430 clickMsg.hwnd = (HWND)editor;
2431 clickMsg.message = msg;
2432 clickMsg.wParam = wParam;
2433 clickMsg.lParam = lParam;
2434 clickMsg.time = GetMessageTime();
2435 clickMsg.pt.x = (short)LOWORD(lParam);
2436 clickMsg.pt.y = (short)HIWORD(lParam);
2437 if ((clickNum != 0) &&
2438 (clickMsg.message == prevClickMsg.message) &&
2439 (clickMsg.hwnd == prevClickMsg.hwnd) &&
2440 (clickMsg.wParam == prevClickMsg.wParam) &&
2441 (clickMsg.time - prevClickMsg.time < GetDoubleClickTime()) &&
2442 (abs(clickMsg.pt.x - prevClickMsg.pt.x) < GetSystemMetrics(SM_CXDOUBLECLK)/2) &&
2443 (abs(clickMsg.pt.y - prevClickMsg.pt.y) < GetSystemMetrics(SM_CYDOUBLECLK)/2))
2445 clickNum++;
2446 } else {
2447 clickNum = 1;
2449 prevClickMsg = clickMsg;
2450 } else {
2451 return 0;
2453 return clickNum;
2456 static BOOL ME_SetCursor(ME_TextEditor *editor)
2458 POINT pt;
2459 BOOL isExact;
2460 int offset;
2461 SCROLLBARINFO sbi;
2462 DWORD messagePos = GetMessagePos();
2463 pt.x = (short)LOWORD(messagePos);
2464 pt.y = (short)HIWORD(messagePos);
2466 if (editor->hWnd)
2468 sbi.cbSize = sizeof(sbi);
2469 GetScrollBarInfo(editor->hWnd, OBJID_HSCROLL, &sbi);
2470 if (!(sbi.rgstate[0] & (STATE_SYSTEM_INVISIBLE|STATE_SYSTEM_OFFSCREEN)) &&
2471 PtInRect(&sbi.rcScrollBar, pt))
2473 ITextHost_TxSetCursor(editor->texthost,
2474 LoadCursorW(NULL, (WCHAR*)IDC_ARROW), FALSE);
2475 return TRUE;
2477 sbi.cbSize = sizeof(sbi);
2478 GetScrollBarInfo(editor->hWnd, OBJID_VSCROLL, &sbi);
2479 if (!(sbi.rgstate[0] & (STATE_SYSTEM_INVISIBLE|STATE_SYSTEM_OFFSCREEN)) &&
2480 PtInRect(&sbi.rcScrollBar, pt))
2482 ITextHost_TxSetCursor(editor->texthost,
2483 LoadCursorW(NULL, (WCHAR*)IDC_ARROW), FALSE);
2484 return TRUE;
2487 ITextHost_TxScreenToClient(editor->texthost, &pt);
2489 if (editor->nSelectionType == stLine && editor->bMouseCaptured) {
2490 ITextHost_TxSetCursor(editor->texthost, hLeft, FALSE);
2491 return TRUE;
2493 if (!editor->bEmulateVersion10 /* v4.1 */ &&
2494 pt.y < editor->rcFormat.top &&
2495 pt.x < editor->rcFormat.left)
2497 ITextHost_TxSetCursor(editor->texthost, hLeft, FALSE);
2498 return TRUE;
2500 if (pt.y < editor->rcFormat.top || pt.y > editor->rcFormat.bottom)
2502 if (editor->bEmulateVersion10) /* v1.0 - 3.0 */
2503 ITextHost_TxSetCursor(editor->texthost,
2504 LoadCursorW(NULL, (WCHAR*)IDC_ARROW), FALSE);
2505 else /* v4.1 */
2506 ITextHost_TxSetCursor(editor->texthost,
2507 LoadCursorW(NULL, (WCHAR*)IDC_IBEAM), TRUE);
2508 return TRUE;
2510 if (pt.x < editor->rcFormat.left)
2512 ITextHost_TxSetCursor(editor->texthost, hLeft, FALSE);
2513 return TRUE;
2515 offset = ME_CharFromPos(editor, pt.x, pt.y, &isExact);
2516 if (isExact)
2518 if (editor->AutoURLDetect_bEnable)
2520 ME_Cursor cursor;
2521 ME_Run *run;
2522 ME_CursorFromCharOfs(editor, offset, &cursor);
2523 run = &cursor.pRun->member.run;
2524 if (editor->AutoURLDetect_bEnable &&
2525 run->style->fmt.dwMask & CFM_LINK &&
2526 run->style->fmt.dwEffects & CFE_LINK)
2528 ITextHost_TxSetCursor(editor->texthost,
2529 LoadCursorW(NULL, (WCHAR*)IDC_HAND),
2530 FALSE);
2531 return TRUE;
2534 if (ME_IsSelection(editor))
2536 int selStart, selEnd;
2537 ME_GetSelection(editor, &selStart, &selEnd);
2538 if (selStart <= offset && selEnd >= offset) {
2539 ITextHost_TxSetCursor(editor->texthost,
2540 LoadCursorW(NULL, (WCHAR*)IDC_ARROW),
2541 FALSE);
2542 return TRUE;
2546 ITextHost_TxSetCursor(editor->texthost,
2547 LoadCursorW(NULL, (WCHAR*)IDC_IBEAM), TRUE);
2548 return TRUE;
2551 static void ME_SetDefaultFormatRect(ME_TextEditor *editor)
2553 ITextHost_TxGetClientRect(editor->texthost, &editor->rcFormat);
2554 editor->rcFormat.top += editor->exStyleFlags & WS_EX_CLIENTEDGE ? 1 : 0;
2555 editor->rcFormat.left += 1 + editor->selofs;
2556 editor->rcFormat.right -= 1;
2559 static BOOL ME_ShowContextMenu(ME_TextEditor *editor, int x, int y)
2561 CHARRANGE selrange;
2562 HMENU menu;
2563 int seltype = 0;
2564 if(!editor->lpOleCallback || !editor->hWnd)
2565 return FALSE;
2566 ME_GetSelection(editor, &selrange.cpMin, &selrange.cpMax);
2567 if(selrange.cpMin == selrange.cpMax)
2568 seltype |= SEL_EMPTY;
2569 else
2571 /* FIXME: Handle objects */
2572 seltype |= SEL_TEXT;
2573 if(selrange.cpMax-selrange.cpMin > 1)
2574 seltype |= SEL_MULTICHAR;
2576 if(SUCCEEDED(IRichEditOleCallback_GetContextMenu(editor->lpOleCallback, seltype, NULL, &selrange, &menu)))
2578 TrackPopupMenu(menu, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, GetParent(editor->hWnd), NULL);
2579 DestroyMenu(menu);
2581 return TRUE;
2584 ME_TextEditor *ME_MakeEditor(ITextHost *texthost, BOOL bEmulateVersion10)
2586 ME_TextEditor *ed = ALLOC_OBJ(ME_TextEditor);
2587 int i;
2588 DWORD props;
2589 LONG selbarwidth;
2591 ed->hWnd = NULL;
2592 ed->texthost = texthost;
2593 ed->bEmulateVersion10 = bEmulateVersion10;
2594 ITextHost_TxGetPropertyBits(texthost,
2595 (TXTBIT_RICHTEXT|TXTBIT_MULTILINE|
2596 TXTBIT_READONLY|TXTBIT_USEPASSWORD|
2597 TXTBIT_HIDESELECTION|TXTBIT_SAVESELECTION|
2598 TXTBIT_AUTOWORDSEL|TXTBIT_VERTICAL|
2599 TXTBIT_WORDWRAP|TXTBIT_DISABLEDRAG),
2600 &props);
2601 ITextHost_TxGetScrollBars(texthost, &ed->styleFlags);
2602 ed->styleFlags &= (WS_VSCROLL|WS_HSCROLL|ES_AUTOVSCROLL|
2603 ES_AUTOHSCROLL|ES_DISABLENOSCROLL);
2604 ed->pBuffer = ME_MakeText();
2605 ed->nZoomNumerator = ed->nZoomDenominator = 0;
2606 ME_MakeFirstParagraph(ed);
2607 /* The four cursors are for:
2608 * 0 - The position where the caret is shown
2609 * 1 - The anchored end of the selection (for normal selection)
2610 * 2 & 3 - The anchored start and end respectively for word, line,
2611 * or paragraph selection.
2613 ed->nCursors = 4;
2614 ed->pCursors = ALLOC_N_OBJ(ME_Cursor, ed->nCursors);
2615 ed->pCursors[0].pPara = ed->pBuffer->pFirst->member.para.next_para;
2616 ed->pCursors[0].pRun = ME_FindItemFwd(ed->pCursors[0].pPara, diRun);
2617 ed->pCursors[0].nOffset = 0;
2618 ed->pCursors[1] = ed->pCursors[0];
2619 ed->pCursors[2] = ed->pCursors[0];
2620 ed->pCursors[3] = ed->pCursors[1];
2621 ed->nLastTotalLength = ed->nTotalLength = 0;
2622 ed->nLastTotalWidth = ed->nTotalWidth = 0;
2623 ed->nUDArrowX = -1;
2624 ed->nSequence = 0;
2625 ed->rgbBackColor = -1;
2626 ed->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
2627 ed->bCaretAtEnd = FALSE;
2628 ed->nEventMask = 0;
2629 ed->nModifyStep = 0;
2630 ed->nTextLimit = TEXT_LIMIT_DEFAULT;
2631 ed->pUndoStack = ed->pRedoStack = ed->pUndoStackBottom = NULL;
2632 ed->nUndoStackSize = 0;
2633 ed->nUndoLimit = STACK_SIZE_DEFAULT;
2634 ed->nUndoMode = umAddToUndo;
2635 ed->nParagraphs = 1;
2636 ed->nLastSelStart = ed->nLastSelEnd = 0;
2637 ed->pLastSelStartPara = ed->pLastSelEndPara = ME_FindItemFwd(ed->pBuffer->pFirst, diParagraph);
2638 ed->nAvailWidth = 0; /* wrap to client area */
2639 ed->bHideSelection = FALSE;
2640 ed->pfnWordBreak = NULL;
2641 ed->lpOleCallback = NULL;
2642 ed->mode = TM_MULTILEVELUNDO | TM_MULTICODEPAGE;
2643 ed->mode |= (props & TXTBIT_RICHTEXT) ? TM_RICHTEXT : TM_PLAINTEXT;
2644 ed->AutoURLDetect_bEnable = FALSE;
2645 ed->bHaveFocus = FALSE;
2646 ed->bMouseCaptured = FALSE;
2647 for (i=0; i<HFONT_CACHE_SIZE; i++)
2649 ed->pFontCache[i].nRefs = 0;
2650 ed->pFontCache[i].nAge = 0;
2651 ed->pFontCache[i].hFont = NULL;
2654 ME_CheckCharOffsets(ed);
2655 ed->bDefaultFormatRect = TRUE;
2656 ITextHost_TxGetSelectionBarWidth(ed->texthost, &selbarwidth);
2657 if (selbarwidth) {
2658 /* FIXME: Convert selbarwidth from HIMETRIC to pixels */
2659 ed->selofs = SELECTIONBAR_WIDTH;
2660 ed->styleFlags |= ES_SELECTIONBAR;
2661 } else {
2662 ed->selofs = 0;
2664 ed->nSelectionType = stPosition;
2666 ed->cPasswordMask = 0;
2667 if (props & TXTBIT_USEPASSWORD)
2668 ITextHost_TxGetPasswordChar(texthost, &ed->cPasswordMask);
2670 if (props & TXTBIT_AUTOWORDSEL)
2671 ed->styleFlags |= ECO_AUTOWORDSELECTION;
2672 if (props & TXTBIT_MULTILINE) {
2673 ed->styleFlags |= ES_MULTILINE;
2674 ed->bWordWrap = (props & TXTBIT_WORDWRAP) != 0;
2675 } else {
2676 ed->bWordWrap = FALSE;
2678 if (props & TXTBIT_READONLY)
2679 ed->styleFlags |= ES_READONLY;
2680 if (!(props & TXTBIT_HIDESELECTION))
2681 ed->styleFlags |= ES_NOHIDESEL;
2682 if (props & TXTBIT_SAVESELECTION)
2683 ed->styleFlags |= ES_SAVESEL;
2684 if (props & TXTBIT_VERTICAL)
2685 ed->styleFlags |= ES_VERTICAL;
2686 if (props & TXTBIT_DISABLEDRAG)
2687 ed->styleFlags |= ES_NOOLEDRAGDROP;
2689 ed->notified_cr.cpMin = ed->notified_cr.cpMax = 0;
2691 /* Default scrollbar information */
2692 ed->vert_si.cbSize = sizeof(SCROLLINFO);
2693 ed->vert_si.nMin = 0;
2694 ed->vert_si.nMax = 0;
2695 ed->vert_si.nPage = 0;
2696 ed->vert_si.nPos = 0;
2698 ed->horz_si.cbSize = sizeof(SCROLLINFO);
2699 ed->horz_si.nMin = 0;
2700 ed->horz_si.nMax = 0;
2701 ed->horz_si.nPage = 0;
2702 ed->horz_si.nPos = 0;
2704 OleInitialize(NULL);
2706 return ed;
2709 static void ME_DestroyEditor(ME_TextEditor *editor)
2711 ME_DisplayItem *pFirst = editor->pBuffer->pFirst;
2712 ME_DisplayItem *p = pFirst, *pNext = NULL;
2713 int i;
2715 ME_ClearTempStyle(editor);
2716 ME_EmptyUndoStack(editor);
2717 while(p) {
2718 pNext = p->next;
2719 ME_DestroyDisplayItem(p);
2720 p = pNext;
2722 ME_ReleaseStyle(editor->pBuffer->pDefaultStyle);
2723 for (i=0; i<HFONT_CACHE_SIZE; i++)
2725 if (editor->pFontCache[i].hFont)
2726 DeleteObject(editor->pFontCache[i].hFont);
2728 if (editor->rgbBackColor != -1)
2729 DeleteObject(editor->hbrBackground);
2730 if(editor->lpOleCallback)
2731 IUnknown_Release(editor->lpOleCallback);
2732 IUnknown_Release(editor->texthost);
2733 OleUninitialize();
2735 FREE_OBJ(editor->pBuffer);
2736 FREE_OBJ(editor->pCursors);
2738 FREE_OBJ(editor);
2741 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
2743 TRACE("\n");
2744 switch (fdwReason)
2746 case DLL_PROCESS_ATTACH:
2747 DisableThreadLibraryCalls(hinstDLL);
2748 me_heap = HeapCreate (0, 0x10000, 0);
2749 if (!ME_RegisterEditorClass(hinstDLL)) return FALSE;
2750 hLeft = LoadCursorW(hinstDLL, MAKEINTRESOURCEW(OCR_REVERSE));
2751 LookupInit();
2752 break;
2754 case DLL_PROCESS_DETACH:
2755 UnregisterClassW(RichEdit20W, 0);
2756 UnregisterClassW(RichEdit50W, 0);
2757 UnregisterClassA("RichEdit20A", 0);
2758 UnregisterClassA("RichEdit50A", 0);
2759 if (ME_ListBoxRegistered)
2760 UnregisterClassW(REListBox20W, 0);
2761 if (ME_ComboBoxRegistered)
2762 UnregisterClassW(REComboBox20W, 0);
2763 LookupCleanup();
2764 HeapDestroy (me_heap);
2765 me_heap = NULL;
2766 break;
2768 return TRUE;
2772 static const char * const edit_messages[] = {
2773 "EM_GETSEL",
2774 "EM_SETSEL",
2775 "EM_GETRECT",
2776 "EM_SETRECT",
2777 "EM_SETRECTNP",
2778 "EM_SCROLL",
2779 "EM_LINESCROLL",
2780 "EM_SCROLLCARET",
2781 "EM_GETMODIFY",
2782 "EM_SETMODIFY",
2783 "EM_GETLINECOUNT",
2784 "EM_LINEINDEX",
2785 "EM_SETHANDLE",
2786 "EM_GETHANDLE",
2787 "EM_GETTHUMB",
2788 "EM_UNKNOWN_BF",
2789 "EM_UNKNOWN_C0",
2790 "EM_LINELENGTH",
2791 "EM_REPLACESEL",
2792 "EM_UNKNOWN_C3",
2793 "EM_GETLINE",
2794 "EM_LIMITTEXT",
2795 "EM_CANUNDO",
2796 "EM_UNDO",
2797 "EM_FMTLINES",
2798 "EM_LINEFROMCHAR",
2799 "EM_UNKNOWN_CA",
2800 "EM_SETTABSTOPS",
2801 "EM_SETPASSWORDCHAR",
2802 "EM_EMPTYUNDOBUFFER",
2803 "EM_GETFIRSTVISIBLELINE",
2804 "EM_SETREADONLY",
2805 "EM_SETWORDBREAKPROC",
2806 "EM_GETWORDBREAKPROC",
2807 "EM_GETPASSWORDCHAR",
2808 "EM_SETMARGINS",
2809 "EM_GETMARGINS",
2810 "EM_GETLIMITTEXT",
2811 "EM_POSFROMCHAR",
2812 "EM_CHARFROMPOS",
2813 "EM_SETIMESTATUS",
2814 "EM_GETIMESTATUS"
2817 static const char * const richedit_messages[] = {
2818 "EM_CANPASTE",
2819 "EM_DISPLAYBAND",
2820 "EM_EXGETSEL",
2821 "EM_EXLIMITTEXT",
2822 "EM_EXLINEFROMCHAR",
2823 "EM_EXSETSEL",
2824 "EM_FINDTEXT",
2825 "EM_FORMATRANGE",
2826 "EM_GETCHARFORMAT",
2827 "EM_GETEVENTMASK",
2828 "EM_GETOLEINTERFACE",
2829 "EM_GETPARAFORMAT",
2830 "EM_GETSELTEXT",
2831 "EM_HIDESELECTION",
2832 "EM_PASTESPECIAL",
2833 "EM_REQUESTRESIZE",
2834 "EM_SELECTIONTYPE",
2835 "EM_SETBKGNDCOLOR",
2836 "EM_SETCHARFORMAT",
2837 "EM_SETEVENTMASK",
2838 "EM_SETOLECALLBACK",
2839 "EM_SETPARAFORMAT",
2840 "EM_SETTARGETDEVICE",
2841 "EM_STREAMIN",
2842 "EM_STREAMOUT",
2843 "EM_GETTEXTRANGE",
2844 "EM_FINDWORDBREAK",
2845 "EM_SETOPTIONS",
2846 "EM_GETOPTIONS",
2847 "EM_FINDTEXTEX",
2848 "EM_GETWORDBREAKPROCEX",
2849 "EM_SETWORDBREAKPROCEX",
2850 "EM_SETUNDOLIMIT",
2851 "EM_UNKNOWN_USER_83",
2852 "EM_REDO",
2853 "EM_CANREDO",
2854 "EM_GETUNDONAME",
2855 "EM_GETREDONAME",
2856 "EM_STOPGROUPTYPING",
2857 "EM_SETTEXTMODE",
2858 "EM_GETTEXTMODE",
2859 "EM_AUTOURLDETECT",
2860 "EM_GETAUTOURLDETECT",
2861 "EM_SETPALETTE",
2862 "EM_GETTEXTEX",
2863 "EM_GETTEXTLENGTHEX",
2864 "EM_SHOWSCROLLBAR",
2865 "EM_SETTEXTEX",
2866 "EM_UNKNOWN_USER_98",
2867 "EM_UNKNOWN_USER_99",
2868 "EM_SETPUNCTUATION",
2869 "EM_GETPUNCTUATION",
2870 "EM_SETWORDWRAPMODE",
2871 "EM_GETWORDWRAPMODE",
2872 "EM_SETIMECOLOR",
2873 "EM_GETIMECOLOR",
2874 "EM_SETIMEOPTIONS",
2875 "EM_GETIMEOPTIONS",
2876 "EM_CONVPOSITION",
2877 "EM_UNKNOWN_USER_109",
2878 "EM_UNKNOWN_USER_110",
2879 "EM_UNKNOWN_USER_111",
2880 "EM_UNKNOWN_USER_112",
2881 "EM_UNKNOWN_USER_113",
2882 "EM_UNKNOWN_USER_114",
2883 "EM_UNKNOWN_USER_115",
2884 "EM_UNKNOWN_USER_116",
2885 "EM_UNKNOWN_USER_117",
2886 "EM_UNKNOWN_USER_118",
2887 "EM_UNKNOWN_USER_119",
2888 "EM_SETLANGOPTIONS",
2889 "EM_GETLANGOPTIONS",
2890 "EM_GETIMECOMPMODE",
2891 "EM_FINDTEXTW",
2892 "EM_FINDTEXTEXW",
2893 "EM_RECONVERSION",
2894 "EM_SETIMEMODEBIAS",
2895 "EM_GETIMEMODEBIAS"
2898 static const char *
2899 get_msg_name(UINT msg)
2901 if (msg >= EM_GETSEL && msg <= EM_CHARFROMPOS)
2902 return edit_messages[msg - EM_GETSEL];
2903 if (msg >= EM_CANPASTE && msg <= EM_GETIMEMODEBIAS)
2904 return richedit_messages[msg - EM_CANPASTE];
2905 return "";
2908 void ME_LinkNotify(ME_TextEditor *editor, UINT msg, WPARAM wParam, LPARAM lParam)
2910 int x,y;
2911 ME_DisplayItem *para, *run;
2912 BOOL isExact;
2913 int nCharOfs; /* The start of the clicked text. Absolute character offset */
2915 ENLINK info;
2916 x = (short)LOWORD(lParam);
2917 y = (short)HIWORD(lParam);
2918 nCharOfs = ME_CharFromPos(editor, x, y, &isExact);
2919 if (!isExact) return;
2921 ME_RunOfsFromCharOfs(editor, nCharOfs, &para, &run, NULL);
2923 if ((run->member.run.style->fmt.dwMask & CFM_LINK)
2924 && (run->member.run.style->fmt.dwEffects & CFE_LINK))
2925 { /* The clicked run has CFE_LINK set */
2926 info.nmhdr.hwndFrom = editor->hWnd;
2927 info.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
2928 info.nmhdr.code = EN_LINK;
2929 info.msg = msg;
2930 info.wParam = wParam;
2931 info.lParam = lParam;
2932 info.chrg.cpMin = ME_CharOfsFromRunOfs(editor, para, run, 0);
2933 info.chrg.cpMax = info.chrg.cpMin + run->member.run.strText->nLen;
2934 SendMessageW(GetParent(editor->hWnd), WM_NOTIFY,info.nmhdr.idFrom, (LPARAM)&info);
2938 #define UNSUPPORTED_MSG(e) \
2939 case e: \
2940 FIXME(#e ": stub\n"); \
2941 *phresult = S_FALSE; \
2942 return 0;
2944 /* Handle messages for windowless and windoweded richedit controls.
2946 * The LRESULT that is returned is a return value for window procs,
2947 * and the phresult parameter is the COM return code needed by the
2948 * text services interface. */
2949 LRESULT ME_HandleMessage(ME_TextEditor *editor, UINT msg, WPARAM wParam,
2950 LPARAM lParam, BOOL unicode, HRESULT* phresult)
2952 *phresult = S_OK;
2954 switch(msg) {
2956 UNSUPPORTED_MSG(EM_DISPLAYBAND)
2957 UNSUPPORTED_MSG(EM_FINDWORDBREAK)
2958 UNSUPPORTED_MSG(EM_FMTLINES)
2959 UNSUPPORTED_MSG(EM_FORMATRANGE)
2960 UNSUPPORTED_MSG(EM_GETBIDIOPTIONS)
2961 UNSUPPORTED_MSG(EM_GETEDITSTYLE)
2962 UNSUPPORTED_MSG(EM_GETIMECOMPMODE)
2963 UNSUPPORTED_MSG(EM_GETIMESTATUS)
2964 UNSUPPORTED_MSG(EM_SETIMESTATUS)
2965 UNSUPPORTED_MSG(EM_GETLANGOPTIONS)
2966 UNSUPPORTED_MSG(EM_GETREDONAME)
2967 UNSUPPORTED_MSG(EM_GETTYPOGRAPHYOPTIONS)
2968 UNSUPPORTED_MSG(EM_GETUNDONAME)
2969 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX)
2970 UNSUPPORTED_MSG(EM_PASTESPECIAL)
2971 UNSUPPORTED_MSG(EM_SELECTIONTYPE)
2972 UNSUPPORTED_MSG(EM_SETBIDIOPTIONS)
2973 UNSUPPORTED_MSG(EM_SETEDITSTYLE)
2974 UNSUPPORTED_MSG(EM_SETFONTSIZE)
2975 UNSUPPORTED_MSG(EM_SETLANGOPTIONS)
2976 UNSUPPORTED_MSG(EM_SETMARGINS)
2977 UNSUPPORTED_MSG(EM_SETPALETTE)
2978 UNSUPPORTED_MSG(EM_SETTABSTOPS)
2979 UNSUPPORTED_MSG(EM_SETTYPOGRAPHYOPTIONS)
2980 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX)
2981 UNSUPPORTED_MSG(WM_STYLECHANGING)
2982 UNSUPPORTED_MSG(WM_STYLECHANGED)
2984 /* Messages specific to Richedit controls */
2986 case EM_STREAMIN:
2987 return ME_StreamIn(editor, wParam, (EDITSTREAM*)lParam, TRUE);
2988 case EM_STREAMOUT:
2989 return ME_StreamOut(editor, wParam, (EDITSTREAM *)lParam);
2990 case WM_GETDLGCODE:
2992 UINT code = DLGC_WANTCHARS|DLGC_WANTTAB|DLGC_WANTARROWS|DLGC_HASSETSEL;
2993 if (editor->styleFlags & ES_MULTILINE)
2994 code |= DLGC_WANTMESSAGE;
2995 return code;
2997 case EM_EMPTYUNDOBUFFER:
2998 ME_EmptyUndoStack(editor);
2999 return 0;
3000 case EM_GETSEL:
3002 /* Note: wParam/lParam can be NULL */
3003 UINT from, to;
3004 PUINT pfrom = wParam ? (PUINT)wParam : &from;
3005 PUINT pto = lParam ? (PUINT)lParam : &to;
3006 ME_GetSelection(editor, (int *)pfrom, (int *)pto);
3007 if ((*pfrom|*pto) & 0xFFFF0000)
3008 return -1;
3009 return MAKELONG(*pfrom,*pto);
3011 case EM_EXGETSEL:
3013 CHARRANGE *pRange = (CHARRANGE *)lParam;
3014 ME_GetSelection(editor, &pRange->cpMin, &pRange->cpMax);
3015 TRACE("EM_EXGETSEL = (%d,%d)\n", pRange->cpMin, pRange->cpMax);
3016 return 0;
3018 case EM_SETUNDOLIMIT:
3020 if ((int)wParam < 0)
3021 editor->nUndoLimit = STACK_SIZE_DEFAULT;
3022 else
3023 editor->nUndoLimit = min(wParam, STACK_SIZE_MAX);
3024 /* Setting a max stack size keeps wine from getting killed
3025 for hogging memory. Windows allocates all this memory at once, so
3026 no program would realistically set a value above our maximum. */
3027 return editor->nUndoLimit;
3029 case EM_CANUNDO:
3030 return editor->pUndoStack != NULL;
3031 case EM_CANREDO:
3032 return editor->pRedoStack != NULL;
3033 case WM_UNDO: /* FIXME: actually not the same */
3034 case EM_UNDO:
3035 return ME_Undo(editor);
3036 case EM_REDO:
3037 return ME_Redo(editor);
3038 case EM_GETOPTIONS:
3040 /* these flags are equivalent to the ES_* counterparts */
3041 DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
3042 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN | ECO_SELECTIONBAR;
3043 DWORD settings = editor->styleFlags & mask;
3045 return settings;
3047 case EM_SETOPTIONS:
3049 /* these flags are equivalent to ES_* counterparts, except for
3050 * ECO_AUTOWORDSELECTION that doesn't have an ES_* counterpart,
3051 * but is still stored in editor->styleFlags. */
3052 const DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
3053 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN |
3054 ECO_SELECTIONBAR | ECO_AUTOWORDSELECTION;
3055 DWORD settings = mask & editor->styleFlags;
3056 DWORD oldSettings = settings;
3057 DWORD changedSettings;
3059 switch(wParam)
3061 case ECOOP_SET:
3062 settings = lParam;
3063 break;
3064 case ECOOP_OR:
3065 settings |= lParam;
3066 break;
3067 case ECOOP_AND:
3068 settings &= lParam;
3069 break;
3070 case ECOOP_XOR:
3071 settings ^= lParam;
3073 changedSettings = oldSettings ^ settings;
3075 if (changedSettings) {
3076 editor->styleFlags = (editor->styleFlags & ~mask) | (settings & mask);
3078 if (changedSettings & ECO_SELECTIONBAR)
3080 ITextHost_TxInvalidateRect(editor->texthost, &editor->rcFormat, TRUE);
3081 if (settings & ECO_SELECTIONBAR) {
3082 assert(!editor->selofs);
3083 editor->selofs = SELECTIONBAR_WIDTH;
3084 editor->rcFormat.left += editor->selofs;
3085 } else {
3086 editor->rcFormat.left -= editor->selofs;
3087 editor->selofs = 0;
3089 ME_RewrapRepaint(editor);
3092 if (changedSettings & settings & ECO_VERTICAL)
3093 FIXME("ECO_VERTICAL not implemented yet!\n");
3094 if (changedSettings & settings & ECO_AUTOHSCROLL)
3095 FIXME("ECO_AUTOHSCROLL not implemented yet!\n");
3096 if (changedSettings & settings & ECO_AUTOVSCROLL)
3097 FIXME("ECO_AUTOVSCROLL not implemented yet!\n");
3098 if (changedSettings & settings & ECO_NOHIDESEL)
3099 FIXME("ECO_NOHIDESEL not implemented yet!\n");
3100 if (changedSettings & settings & ECO_WANTRETURN)
3101 FIXME("ECO_WANTRETURN not implemented yet!\n");
3102 if (changedSettings & settings & ECO_AUTOWORDSELECTION)
3103 FIXME("ECO_AUTOWORDSELECTION not implemented yet!\n");
3106 return settings;
3108 case EM_SETSEL:
3110 ME_InvalidateSelection(editor);
3111 ME_SetSelection(editor, wParam, lParam);
3112 ME_InvalidateSelection(editor);
3113 ITextHost_TxShowCaret(editor->texthost, FALSE);
3114 ME_ShowCaret(editor);
3115 ME_SendSelChange(editor);
3116 return 0;
3118 case EM_SETSCROLLPOS:
3120 POINT *point = (POINT *)lParam;
3121 ME_ScrollAbs(editor, point->x, point->y);
3122 return 0;
3124 case EM_AUTOURLDETECT:
3126 if (wParam==1 || wParam ==0)
3128 editor->AutoURLDetect_bEnable = (BOOL)wParam;
3129 return 0;
3131 return E_INVALIDARG;
3133 case EM_GETAUTOURLDETECT:
3135 return editor->AutoURLDetect_bEnable;
3137 case EM_EXSETSEL:
3139 int end;
3140 CHARRANGE range = *(CHARRANGE *)lParam;
3142 TRACE("EM_EXSETSEL (%d,%d)\n", range.cpMin, range.cpMax);
3144 ME_InvalidateSelection(editor);
3145 end = ME_SetSelection(editor, range.cpMin, range.cpMax);
3146 ME_InvalidateSelection(editor);
3147 ITextHost_TxShowCaret(editor->texthost, FALSE);
3148 ME_ShowCaret(editor);
3149 ME_SendSelChange(editor);
3151 return end;
3153 case EM_SHOWSCROLLBAR:
3155 DWORD flags;
3157 switch (wParam)
3159 case SB_HORZ:
3160 flags = WS_HSCROLL;
3161 break;
3162 case SB_VERT:
3163 flags = WS_VSCROLL;
3164 break;
3165 case SB_BOTH:
3166 flags = WS_HSCROLL|WS_VSCROLL;
3167 break;
3168 default:
3169 return 0;
3172 if (lParam) {
3173 editor->styleFlags |= flags;
3174 if (flags & WS_HSCROLL)
3175 ITextHost_TxShowScrollBar(editor->texthost, SB_HORZ,
3176 editor->nTotalWidth > editor->sizeWindow.cx);
3177 if (flags & WS_VSCROLL)
3178 ITextHost_TxShowScrollBar(editor->texthost, SB_VERT,
3179 editor->nTotalLength > editor->sizeWindow.cy);
3180 } else {
3181 editor->styleFlags &= ~flags;
3182 ITextHost_TxShowScrollBar(editor->texthost, wParam, FALSE);
3184 return 0;
3186 case EM_SETTEXTEX:
3188 LPWSTR wszText;
3189 SETTEXTEX *pStruct = (SETTEXTEX *)wParam;
3190 size_t len = 0;
3191 int from, to;
3192 ME_Style *style;
3193 BOOL bRtf, bUnicode, bSelection;
3194 int oldModify = editor->nModifyStep;
3196 if (!pStruct) return 0;
3198 /* If we detect ascii rtf at the start of the string,
3199 * we know it isn't unicode. */
3200 bRtf = (lParam && (!strncmp((char *)lParam, "{\\rtf", 5) ||
3201 !strncmp((char *)lParam, "{\\urtf}", 6)));
3202 bUnicode = !bRtf && pStruct->codepage == 1200;
3204 TRACE("EM_SETTEXTEX - %s, flags %d, cp %d\n",
3205 bUnicode ? debugstr_w((LPCWSTR)lParam) : debugstr_a((LPCSTR)lParam),
3206 pStruct->flags, pStruct->codepage);
3208 bSelection = (pStruct->flags & ST_SELECTION) != 0;
3209 if (bSelection) {
3210 ME_GetSelection(editor, &from, &to);
3211 style = ME_GetSelectionInsertStyle(editor);
3212 ME_InternalDeleteText(editor, from, to - from, FALSE);
3213 } else {
3214 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor), FALSE);
3215 style = editor->pBuffer->pDefaultStyle;
3218 if (bRtf) {
3219 ME_StreamInRTFString(editor, bSelection, (char *)lParam);
3220 if (bSelection) {
3221 /* FIXME: The length returned is doesn't include the rtf control
3222 * characters, only the actual text. */
3223 len = lParam ? strlen((char *)lParam) : 0;
3225 } else {
3226 /* FIXME: make use of pStruct->codepage in the to unicode translation */
3227 wszText = lParam ? ME_ToUnicode(bUnicode, (void *)lParam) : NULL;
3228 len = wszText ? lstrlenW(wszText) : 0;
3229 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
3232 if (bSelection) {
3233 ME_ReleaseStyle(style);
3234 if (editor->AutoURLDetect_bEnable)
3235 ME_UpdateSelectionLinkAttribute(editor);
3236 } else {
3237 len = 1;
3238 if (editor->AutoURLDetect_bEnable)
3239 ME_UpdateLinkAttribute(editor, 0, -1);
3241 ME_CommitUndo(editor);
3242 if (!(pStruct->flags & ST_KEEPUNDO))
3244 editor->nModifyStep = oldModify;
3245 ME_EmptyUndoStack(editor);
3247 ME_UpdateRepaint(editor);
3248 return len;
3250 case EM_SETBKGNDCOLOR:
3252 LRESULT lColor;
3253 if (editor->rgbBackColor != -1) {
3254 DeleteObject(editor->hbrBackground);
3255 lColor = editor->rgbBackColor;
3257 else lColor = ITextHost_TxGetSysColor(editor->texthost, COLOR_WINDOW);
3259 if (wParam)
3261 editor->rgbBackColor = -1;
3262 editor->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
3264 else
3266 editor->rgbBackColor = lParam;
3267 editor->hbrBackground = CreateSolidBrush(editor->rgbBackColor);
3269 ITextHost_TxInvalidateRect(editor->texthost, NULL, TRUE);
3270 ITextHost_TxViewChange(editor->texthost, TRUE);
3271 return lColor;
3273 case EM_GETMODIFY:
3274 return editor->nModifyStep == 0 ? 0 : -1;
3275 case EM_SETMODIFY:
3277 if (wParam)
3278 editor->nModifyStep = 1;
3279 else
3280 editor->nModifyStep = 0;
3282 return 0;
3284 case EM_SETREADONLY:
3286 if (wParam)
3287 editor->styleFlags |= ES_READONLY;
3288 else
3289 editor->styleFlags &= ~ES_READONLY;
3290 return 0;
3292 case EM_SETEVENTMASK:
3294 DWORD nOldMask = editor->nEventMask;
3296 editor->nEventMask = lParam;
3297 return nOldMask;
3299 case EM_GETEVENTMASK:
3300 return editor->nEventMask;
3301 case EM_SETCHARFORMAT:
3303 CHARFORMAT2W buf, *p;
3304 BOOL bRepaint = TRUE;
3305 p = ME_ToCF2W(&buf, (CHARFORMAT2W *)lParam);
3306 if (p == NULL) return 0;
3307 if (!wParam)
3308 ME_SetDefaultCharFormat(editor, p);
3309 else if (wParam == (SCF_WORD | SCF_SELECTION)) {
3310 FIXME("EM_SETCHARFORMAT: word selection not supported\n");
3311 return 0;
3312 } else if (wParam == SCF_ALL) {
3313 if (editor->mode & TM_PLAINTEXT)
3314 ME_SetDefaultCharFormat(editor, p);
3315 else {
3316 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), p);
3317 editor->nModifyStep = 1;
3319 } else if (editor->mode & TM_PLAINTEXT) {
3320 return 0;
3321 } else {
3322 int from, to;
3323 ME_GetSelection(editor, &from, &to);
3324 bRepaint = (from != to);
3325 ME_SetSelectionCharFormat(editor, p);
3326 if (from != to) editor->nModifyStep = 1;
3328 ME_CommitUndo(editor);
3329 if (bRepaint)
3331 ME_WrapMarkedParagraphs(editor);
3332 ME_UpdateScrollBar(editor);
3333 ME_Repaint(editor);
3335 return 1;
3337 case EM_GETCHARFORMAT:
3339 CHARFORMAT2W tmp, *dst = (CHARFORMAT2W *)lParam;
3340 if (dst->cbSize != sizeof(CHARFORMATA) &&
3341 dst->cbSize != sizeof(CHARFORMATW) &&
3342 dst->cbSize != sizeof(CHARFORMAT2A) &&
3343 dst->cbSize != sizeof(CHARFORMAT2W))
3344 return 0;
3345 tmp.cbSize = sizeof(tmp);
3346 if (!wParam)
3347 ME_GetDefaultCharFormat(editor, &tmp);
3348 else
3349 ME_GetSelectionCharFormat(editor, &tmp);
3350 ME_CopyToCFAny(dst, &tmp);
3351 return tmp.dwMask;
3353 case EM_SETPARAFORMAT:
3355 BOOL result = ME_SetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
3356 ME_WrapMarkedParagraphs(editor);
3357 ME_UpdateScrollBar(editor);
3358 ME_Repaint(editor);
3359 ME_CommitUndo(editor);
3360 return result;
3362 case EM_GETPARAFORMAT:
3363 ME_GetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
3364 return ((PARAFORMAT2 *)lParam)->dwMask;
3365 case EM_GETFIRSTVISIBLELINE:
3367 ME_DisplayItem *p = editor->pBuffer->pFirst;
3368 int y = editor->vert_si.nPos;
3369 int ypara = 0;
3370 int count = 0;
3371 int ystart, yend;
3372 while(p) {
3373 p = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
3374 if (p->type == diTextEnd)
3375 break;
3376 if (p->type == diParagraph) {
3377 ypara = p->member.para.pt.y;
3378 continue;
3380 ystart = ypara + p->member.row.pt.y;
3381 yend = ystart + p->member.row.nHeight;
3382 if (y < yend) {
3383 break;
3385 count++;
3387 return count;
3389 case EM_HIDESELECTION:
3391 editor->bHideSelection = (wParam != 0);
3392 ME_InvalidateSelection(editor);
3393 return 0;
3395 case EM_LINESCROLL:
3397 if (!(editor->styleFlags & ES_MULTILINE))
3398 return FALSE;
3399 ME_ScrollDown(editor, lParam * 8); /* FIXME follow the original */
3400 return TRUE;
3402 case WM_CLEAR:
3404 int from, to;
3405 ME_GetSelection(editor, &from, &to);
3406 ME_InternalDeleteText(editor, from, to-from, FALSE);
3407 ME_CommitUndo(editor);
3408 ME_UpdateRepaint(editor);
3409 return 0;
3411 case EM_REPLACESEL:
3413 int from, to;
3414 ME_Style *style;
3415 LPWSTR wszText = lParam ? ME_ToUnicode(unicode, (void *)lParam) : NULL;
3416 size_t len = wszText ? lstrlenW(wszText) : 0;
3417 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText));
3419 ME_GetSelection(editor, &from, &to);
3420 style = ME_GetSelectionInsertStyle(editor);
3421 ME_InternalDeleteText(editor, from, to-from, FALSE);
3422 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
3423 ME_ReleaseStyle(style);
3424 /* drop temporary style if line end */
3426 * FIXME question: does abc\n mean: put abc,
3427 * clear temp style, put \n? (would require a change)
3429 if (len>0 && wszText[len-1] == '\n')
3430 ME_ClearTempStyle(editor);
3431 ME_EndToUnicode(unicode, wszText);
3432 ME_CommitUndo(editor);
3433 if (editor->AutoURLDetect_bEnable) ME_UpdateSelectionLinkAttribute(editor);
3434 if (!wParam)
3435 ME_EmptyUndoStack(editor);
3436 ME_UpdateRepaint(editor);
3437 return len;
3439 case EM_SCROLLCARET:
3440 ME_EnsureVisible(editor, &editor->pCursors[0]);
3441 return 0;
3442 case WM_SETFONT:
3444 LOGFONTW lf;
3445 CHARFORMAT2W fmt;
3446 HDC hDC;
3447 BOOL bRepaint = LOWORD(lParam);
3449 if (!wParam)
3450 wParam = (WPARAM)GetStockObject(SYSTEM_FONT);
3451 GetObjectW((HGDIOBJ)wParam, sizeof(LOGFONTW), &lf);
3452 hDC = ITextHost_TxGetDC(editor->texthost);
3453 ME_CharFormatFromLogFont(hDC, &lf, &fmt);
3454 ITextHost_TxReleaseDC(editor->texthost, hDC);
3455 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), &fmt);
3456 ME_SetDefaultCharFormat(editor, &fmt);
3458 ME_CommitUndo(editor);
3459 ME_MarkAllForWrapping(editor);
3460 ME_WrapMarkedParagraphs(editor);
3461 ME_UpdateScrollBar(editor);
3462 if (bRepaint)
3463 ME_Repaint(editor);
3464 return 0;
3466 case WM_SETTEXT:
3468 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor), FALSE);
3469 if (lParam)
3471 TRACE("WM_SETTEXT lParam==%lx\n",lParam);
3472 if (!unicode && !strncmp((char *)lParam, "{\\rtf", 5))
3474 /* Undocumented: WM_SETTEXT supports RTF text */
3475 ME_StreamInRTFString(editor, 0, (char *)lParam);
3477 else
3479 LPWSTR wszText = ME_ToUnicode(unicode, (void *)lParam);
3480 TRACE("WM_SETTEXT - %s\n", debugstr_w(wszText)); /* debugstr_w() */
3481 if (lstrlenW(wszText) > 0)
3483 int len = -1;
3485 /* uses default style! */
3486 if (!(editor->styleFlags & ES_MULTILINE))
3488 WCHAR * p;
3490 p = wszText;
3491 while (*p != '\0' && *p != '\r' && *p != '\n') p++;
3492 len = p - wszText;
3494 ME_InsertTextFromCursor(editor, 0, wszText, len, editor->pBuffer->pDefaultStyle);
3496 ME_EndToUnicode(unicode, wszText);
3499 else
3500 TRACE("WM_SETTEXT - NULL\n");
3501 if (editor->AutoURLDetect_bEnable)
3503 ME_UpdateLinkAttribute(editor, 0, -1);
3505 ME_SetSelection(editor, 0, 0);
3506 editor->nModifyStep = 0;
3507 ME_CommitUndo(editor);
3508 ME_EmptyUndoStack(editor);
3509 ME_UpdateRepaint(editor);
3510 return 1;
3512 case EM_CANPASTE:
3514 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
3515 if (IsClipboardFormatAvailable(nRTFFormat))
3516 return TRUE;
3517 if (IsClipboardFormatAvailable(CF_UNICODETEXT))
3518 return TRUE;
3519 return FALSE;
3521 case WM_PASTE:
3522 ME_Paste(editor);
3523 return 0;
3524 case WM_CUT:
3525 case WM_COPY:
3527 CHARRANGE range;
3528 ME_GetSelection(editor, &range.cpMin, &range.cpMax);
3530 if (ME_Copy(editor, &range) && msg == WM_CUT)
3532 ME_InternalDeleteText(editor, range.cpMin, range.cpMax-range.cpMin, FALSE);
3533 ME_CommitUndo(editor);
3534 ME_UpdateRepaint(editor);
3536 return 0;
3538 case WM_GETTEXTLENGTH:
3540 GETTEXTLENGTHEX how;
3542 /* CR/LF conversion required in 2.0 mode, verbatim in 1.0 mode */
3543 how.flags = GTL_CLOSE | (editor->bEmulateVersion10 ? 0 : GTL_USECRLF) | GTL_NUMCHARS;
3544 how.codepage = unicode ? 1200 : CP_ACP;
3545 return ME_GetTextLengthEx(editor, &how);
3547 case EM_GETTEXTLENGTHEX:
3548 return ME_GetTextLengthEx(editor, (GETTEXTLENGTHEX *)wParam);
3549 case WM_GETTEXT:
3551 GETTEXTEX ex;
3552 LRESULT rc;
3553 LPSTR bufferA = NULL;
3554 LPWSTR bufferW = NULL;
3556 if (unicode)
3557 bufferW = heap_alloc((wParam + 2) * sizeof(WCHAR));
3558 else
3559 bufferA = heap_alloc(wParam + 2);
3561 ex.cb = (wParam + 2) * (unicode ? sizeof(WCHAR) : sizeof(CHAR));
3562 ex.flags = GT_USECRLF;
3563 ex.codepage = unicode ? 1200 : CP_ACP;
3564 ex.lpDefaultChar = NULL;
3565 ex.lpUsedDefChar = NULL;
3567 rc = ME_GetTextEx(editor, &ex, unicode ? (LPARAM)bufferW : (LPARAM)bufferA);
3569 if (unicode)
3571 memcpy((LPWSTR)lParam, bufferW, wParam * sizeof(WCHAR));
3572 if (strlenW(bufferW) >= wParam) rc = 0;
3574 else
3576 memcpy((LPSTR)lParam, bufferA, wParam);
3577 if (strlen(bufferA) >= wParam) rc = 0;
3579 heap_free(bufferA);
3580 heap_free(bufferW);
3581 return rc;
3583 case EM_GETTEXTEX:
3584 return ME_GetTextEx(editor, (GETTEXTEX*)wParam, lParam);
3585 case EM_GETSELTEXT:
3587 int from, to;
3588 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
3589 ME_GetSelection(editor, &from, &to);
3590 tr.chrg.cpMin = from;
3591 tr.chrg.cpMax = to;
3592 tr.lpstrText = (WCHAR *)lParam;
3593 return ME_GetTextRange(editor, &tr, unicode);
3595 case EM_GETSCROLLPOS:
3597 POINT *point = (POINT *)lParam;
3598 point->x = editor->horz_si.nPos;
3599 point->y = editor->vert_si.nPos;
3600 return 1;
3602 case EM_GETTEXTRANGE:
3604 TEXTRANGEW *rng = (TEXTRANGEW *)lParam;
3605 TRACE("EM_GETTEXTRANGE min=%d max=%d unicode=%d emul1.0=%d length=%d\n",
3606 rng->chrg.cpMin, rng->chrg.cpMax, unicode,
3607 editor->bEmulateVersion10, ME_GetTextLength(editor));
3608 return ME_GetTextRange(editor, rng, unicode);
3610 case EM_GETLINE:
3612 ME_DisplayItem *run;
3613 const unsigned int nMaxChars = *(WORD *) lParam;
3614 unsigned int nCharsLeft = nMaxChars;
3615 char *dest = (char *) lParam;
3616 BOOL wroteNull = FALSE;
3618 TRACE("EM_GETLINE: row=%d, nMaxChars=%d (%s)\n", (int) wParam, nMaxChars,
3619 unicode ? "Unicode" : "Ansi");
3621 run = ME_FindRowWithNumber(editor, wParam);
3622 if (run == NULL)
3623 return 0;
3625 while (nCharsLeft && (run = ME_FindItemFwd(run, diRunOrStartRow))
3626 && run->type == diRun)
3628 unsigned int nCopy;
3629 ME_String *strText;
3631 strText = run->member.run.strText;
3632 nCopy = min(nCharsLeft, strText->nLen);
3634 if (unicode)
3635 memcpy(dest, strText->szData, nCopy * sizeof(WCHAR));
3636 else
3637 nCopy = WideCharToMultiByte(CP_ACP, 0, strText->szData, nCopy, dest,
3638 nCharsLeft, NULL, NULL);
3639 dest += nCopy * (unicode ? sizeof(WCHAR) : 1);
3640 nCharsLeft -= nCopy;
3643 /* append line termination, space allowing */
3644 if (nCharsLeft > 0)
3646 if (unicode)
3647 *((WCHAR *)dest) = '\0';
3648 else
3649 *dest = '\0';
3650 nCharsLeft--;
3651 wroteNull = TRUE;
3654 TRACE("EM_GETLINE: got %u characters\n", nMaxChars - nCharsLeft);
3655 return nMaxChars - nCharsLeft - (wroteNull ? 1 : 0);
3657 case EM_GETLINECOUNT:
3659 ME_DisplayItem *item = editor->pBuffer->pFirst->next;
3660 int nRows = 0;
3662 ME_DisplayItem *prev_para = NULL, *last_para = NULL;
3664 while (item != editor->pBuffer->pLast)
3666 assert(item->type == diParagraph);
3667 prev_para = ME_FindItemBack(item, diRun);
3668 if (prev_para) {
3669 assert(prev_para->member.run.nFlags & MERF_ENDPARA);
3671 nRows += item->member.para.nRows;
3672 item = item->member.para.next_para;
3674 last_para = ME_FindItemBack(item, diRun);
3675 assert(last_para);
3676 assert(last_para->member.run.nFlags & MERF_ENDPARA);
3677 if (editor->bEmulateVersion10 && prev_para &&
3678 last_para->member.run.nCharOfs == 0 &&
3679 prev_para->member.run.strText->nLen == 1 &&
3680 prev_para->member.run.strText->szData[0] == '\r')
3682 /* In 1.0 emulation, the last solitary \r at the very end of the text
3683 (if one exists) is NOT a line break.
3684 FIXME: this is an ugly hack. This should have a more regular model. */
3685 nRows--;
3688 TRACE("EM_GETLINECOUNT: nRows==%d\n", nRows);
3689 return max(1, nRows);
3691 case EM_LINEFROMCHAR:
3693 if (wParam == -1)
3694 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(editor, 1));
3695 else
3696 return ME_RowNumberFromCharOfs(editor, wParam);
3698 case EM_EXLINEFROMCHAR:
3700 if (lParam == -1)
3701 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(editor,1));
3702 else
3703 return ME_RowNumberFromCharOfs(editor, lParam);
3705 case EM_LINEINDEX:
3707 ME_DisplayItem *item, *para;
3708 int nCharOfs;
3710 if (wParam == -1)
3711 item = ME_FindItemBack(editor->pCursors[0].pRun, diStartRow);
3712 else
3713 item = ME_FindRowWithNumber(editor, wParam);
3714 if (!item)
3715 return -1;
3716 para = ME_GetParagraph(item);
3717 item = ME_FindItemFwd(item, diRun);
3718 nCharOfs = para->member.para.nCharOfs + item->member.run.nCharOfs;
3719 TRACE("EM_LINEINDEX: nCharOfs==%d\n", nCharOfs);
3720 return nCharOfs;
3722 case EM_LINELENGTH:
3724 ME_DisplayItem *item, *item_end;
3725 int nChars = 0, nThisLineOfs = 0, nNextLineOfs = 0;
3726 ME_DisplayItem *para, *run;
3728 if (wParam > ME_GetTextLength(editor))
3729 return 0;
3730 if (wParam == -1)
3732 FIXME("EM_LINELENGTH: returning number of unselected characters on lines with selection unsupported.\n");
3733 return 0;
3735 ME_RunOfsFromCharOfs(editor, wParam, &para, &run, NULL);
3736 item = ME_RowStart(run);
3737 nThisLineOfs = ME_CharOfsFromRunOfs(editor, para, ME_FindItemFwd(item, diRun), 0);
3738 item_end = ME_FindItemFwd(item, diStartRowOrParagraphOrEnd);
3739 if (item_end->type == diStartRow) {
3740 nNextLineOfs = ME_CharOfsFromRunOfs(editor, para, ME_FindItemFwd(item_end, diRun), 0);
3741 } else {
3742 ME_DisplayItem *endRun = ME_FindItemBack(item_end, diRun);
3743 assert(endRun && endRun->member.run.nFlags & MERF_ENDPARA);
3744 nNextLineOfs = item_end->member.para.nCharOfs - endRun->member.run.strText->nLen;
3746 nChars = nNextLineOfs - nThisLineOfs;
3747 TRACE("EM_LINELENGTH(%ld)==%d\n",wParam, nChars);
3748 return nChars;
3750 case EM_EXLIMITTEXT:
3752 if ((int)lParam < 0)
3753 return 0;
3754 if (lParam == 0)
3755 editor->nTextLimit = 65536;
3756 else
3757 editor->nTextLimit = (int) lParam;
3758 return 0;
3760 case EM_LIMITTEXT:
3762 if (wParam == 0)
3763 editor->nTextLimit = 65536;
3764 else
3765 editor->nTextLimit = (int) wParam;
3766 return 0;
3768 case EM_GETLIMITTEXT:
3770 return editor->nTextLimit;
3772 case EM_FINDTEXT:
3774 FINDTEXTA *ft = (FINDTEXTA *)lParam;
3775 int nChars = MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, NULL, 0);
3776 WCHAR *tmp;
3777 LRESULT r;
3779 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
3780 MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, tmp, nChars);
3781 r = ME_FindText(editor, wParam, &ft->chrg, tmp, NULL);
3782 FREE_OBJ( tmp );
3783 return r;
3785 case EM_FINDTEXTEX:
3787 FINDTEXTEXA *ex = (FINDTEXTEXA *)lParam;
3788 int nChars = MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, NULL, 0);
3789 WCHAR *tmp;
3790 LRESULT r;
3792 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
3793 MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, tmp, nChars);
3794 r = ME_FindText(editor, wParam, &ex->chrg, tmp, &ex->chrgText);
3795 FREE_OBJ( tmp );
3796 return r;
3798 case EM_FINDTEXTW:
3800 FINDTEXTW *ft = (FINDTEXTW *)lParam;
3801 return ME_FindText(editor, wParam, &ft->chrg, ft->lpstrText, NULL);
3803 case EM_FINDTEXTEXW:
3805 FINDTEXTEXW *ex = (FINDTEXTEXW *)lParam;
3806 return ME_FindText(editor, wParam, &ex->chrg, ex->lpstrText, &ex->chrgText);
3808 case EM_GETZOOM:
3809 if (!wParam || !lParam)
3810 return FALSE;
3811 *(int *)wParam = editor->nZoomNumerator;
3812 *(int *)lParam = editor->nZoomDenominator;
3813 return TRUE;
3814 case EM_SETZOOM:
3815 return ME_SetZoom(editor, wParam, lParam);
3816 case EM_CHARFROMPOS:
3817 return ME_CharFromPos(editor, ((POINTL *)lParam)->x, ((POINTL *)lParam)->y, NULL);
3818 case EM_POSFROMCHAR:
3820 ME_DisplayItem *pPara, *pRun;
3821 int nCharOfs, nOffset, nLength;
3822 POINTL pt = {0,0};
3824 nCharOfs = wParam;
3825 /* detect which API version we're dealing with */
3826 if (wParam >= 0x40000)
3827 nCharOfs = lParam;
3828 nLength = ME_GetTextLength(editor);
3829 nCharOfs = min(nCharOfs, nLength);
3830 nCharOfs = max(nCharOfs, 0);
3832 ME_RunOfsFromCharOfs(editor, nCharOfs, &pPara, &pRun, &nOffset);
3833 assert(pRun->type == diRun);
3834 pt.y = pRun->member.run.pt.y;
3835 pt.x = pRun->member.run.pt.x + ME_PointFromChar(editor, &pRun->member.run, nOffset);
3836 pt.y += pPara->member.para.pt.y + editor->rcFormat.top;
3837 pt.x += editor->rcFormat.left;
3839 pt.x -= editor->horz_si.nPos;
3840 pt.y -= editor->vert_si.nPos;
3842 if (wParam >= 0x40000) {
3843 *(POINTL *)wParam = pt;
3845 return (wParam >= 0x40000) ? 0 : MAKELONG( pt.x, pt.y );
3847 case WM_CREATE:
3849 SCROLLINFO si;
3851 ME_SetDefaultFormatRect(editor);
3853 si.cbSize = sizeof(si);
3854 si.fMask = SIF_PAGE | SIF_RANGE;
3855 if (editor->styleFlags & ES_DISABLENOSCROLL)
3856 si.fMask |= SIF_DISABLENOSCROLL;
3857 si.nMax = (si.fMask & SIF_DISABLENOSCROLL) ? 1 : 0;
3858 si.nMin = 0;
3859 si.nPage = 0;
3860 if (editor->hWnd) {
3861 SetScrollInfo(editor->hWnd, SB_VERT, &si, TRUE);
3862 SetScrollInfo(editor->hWnd, SB_HORZ, &si, TRUE);
3863 } else {
3864 ITextHost_TxSetScrollRange(editor->texthost, SB_VERT, si.nMin, si.nMax, TRUE);
3865 ITextHost_TxSetScrollRange(editor->texthost, SB_HORZ, si.nMin, si.nMax, TRUE);
3868 ME_CommitUndo(editor);
3869 ME_WrapMarkedParagraphs(editor);
3870 ME_MoveCaret(editor);
3871 return 0;
3873 case WM_DESTROY:
3874 ME_DestroyEditor(editor);
3875 return 0;
3876 case WM_SETCURSOR:
3878 return ME_SetCursor(editor);
3880 case WM_LBUTTONDBLCLK:
3881 case WM_LBUTTONDOWN:
3883 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
3884 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
3885 !ME_FilterEvent(editor, msg, &wParam, &lParam))
3886 return 0;
3887 ITextHost_TxSetFocus(editor->texthost);
3888 ME_LButtonDown(editor, (short)LOWORD(lParam), (short)HIWORD(lParam),
3889 ME_CalculateClickCount(editor, msg, wParam, lParam));
3890 ITextHost_TxSetCapture(editor->texthost, TRUE);
3891 editor->bMouseCaptured = TRUE;
3892 ME_LinkNotify(editor,msg,wParam,lParam);
3893 if (!ME_SetCursor(editor)) goto do_default;
3894 break;
3896 case WM_MOUSEMOVE:
3897 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
3898 !ME_FilterEvent(editor, msg, &wParam, &lParam))
3899 return 0;
3900 if (editor->bMouseCaptured)
3901 ME_MouseMove(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
3902 ME_LinkNotify(editor,msg,wParam,lParam);
3903 /* Set cursor if mouse is captured, since WM_SETCURSOR won't be received. */
3904 if (editor->bMouseCaptured)
3905 ME_SetCursor(editor);
3906 break;
3907 case WM_LBUTTONUP:
3908 if (editor->bMouseCaptured) {
3909 ITextHost_TxSetCapture(editor->texthost, FALSE);
3910 editor->bMouseCaptured = FALSE;
3912 if (editor->nSelectionType == stDocument)
3913 editor->nSelectionType = stPosition;
3914 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
3915 !ME_FilterEvent(editor, msg, &wParam, &lParam))
3916 return 0;
3917 else
3919 ME_SetCursor(editor);
3920 ME_LinkNotify(editor,msg,wParam,lParam);
3922 break;
3923 case WM_RBUTTONUP:
3924 case WM_RBUTTONDOWN:
3925 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
3926 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
3927 !ME_FilterEvent(editor, msg, &wParam, &lParam))
3928 return 0;
3929 goto do_default;
3930 case WM_CONTEXTMENU:
3931 if (!ME_ShowContextMenu(editor, (short)LOWORD(lParam), (short)HIWORD(lParam)))
3932 goto do_default;
3933 break;
3934 case WM_SETFOCUS:
3935 editor->bHaveFocus = TRUE;
3936 ME_ShowCaret(editor);
3937 ME_SendOldNotify(editor, EN_SETFOCUS);
3938 return 0;
3939 case WM_KILLFOCUS:
3940 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
3941 editor->bHaveFocus = FALSE;
3942 ME_HideCaret(editor);
3943 ME_SendOldNotify(editor, EN_KILLFOCUS);
3944 return 0;
3945 case WM_COMMAND:
3946 TRACE("editor wnd command = %d\n", LOWORD(wParam));
3947 return 0;
3948 case WM_KEYUP:
3949 if ((editor->nEventMask & ENM_KEYEVENTS) &&
3950 !ME_FilterEvent(editor, msg, &wParam, &lParam))
3951 return 0;
3952 goto do_default;
3953 case WM_KEYDOWN:
3954 if ((editor->nEventMask & ENM_KEYEVENTS) &&
3955 !ME_FilterEvent(editor, msg, &wParam, &lParam))
3956 return 0;
3957 if (ME_KeyDown(editor, LOWORD(wParam)))
3958 return 0;
3959 goto do_default;
3960 case WM_CHAR:
3961 return ME_Char(editor, wParam, lParam, unicode);
3962 case WM_UNICHAR:
3963 if (unicode)
3965 if(wParam == UNICODE_NOCHAR) return TRUE;
3966 if(wParam <= 0x000fffff)
3968 if(wParam > 0xffff) /* convert to surrogates */
3970 wParam -= 0x10000;
3971 ME_Char(editor, (wParam >> 10) + 0xd800, 0, TRUE);
3972 ME_Char(editor, (wParam & 0x03ff) + 0xdc00, 0, TRUE);
3973 } else {
3974 ME_Char(editor, wParam, 0, TRUE);
3977 return 0;
3979 break;
3980 case EM_STOPGROUPTYPING:
3981 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
3982 return 0;
3983 case WM_HSCROLL:
3985 const int scrollUnit = 7;
3987 switch(LOWORD(wParam))
3989 case SB_LEFT:
3990 ME_ScrollAbs(editor, 0, 0);
3991 break;
3992 case SB_RIGHT:
3993 ME_ScrollAbs(editor,
3994 editor->horz_si.nMax - (int)editor->horz_si.nPage,
3995 editor->vert_si.nMax - (int)editor->vert_si.nPage);
3996 break;
3997 case SB_LINELEFT:
3998 ME_ScrollLeft(editor, scrollUnit);
3999 break;
4000 case SB_LINERIGHT:
4001 ME_ScrollRight(editor, scrollUnit);
4002 break;
4003 case SB_PAGELEFT:
4004 ME_ScrollLeft(editor, editor->sizeWindow.cx);
4005 break;
4006 case SB_PAGERIGHT:
4007 ME_ScrollRight(editor, editor->sizeWindow.cx);
4008 break;
4009 case SB_THUMBTRACK:
4010 case SB_THUMBPOSITION:
4012 SCROLLINFO sbi;
4013 sbi.cbSize = sizeof(sbi);
4014 sbi.fMask = SIF_TRACKPOS;
4015 /* Try to get 32-bit track position value. */
4016 if (!GetScrollInfo(editor->hWnd, SB_HORZ, &sbi))
4017 /* GetScrollInfo failed, settle for 16-bit value in wParam. */
4018 sbi.nTrackPos = HIWORD(wParam);
4020 ME_HScrollAbs(editor, sbi.nTrackPos);
4021 break;
4024 break;
4026 case EM_SCROLL: /* fall through */
4027 case WM_VSCROLL:
4029 int origNPos;
4030 int lineHeight;
4032 origNPos = editor->vert_si.nPos;
4033 lineHeight = 24;
4035 if (editor->pBuffer && editor->pBuffer->pDefaultStyle)
4036 lineHeight = editor->pBuffer->pDefaultStyle->tm.tmHeight;
4037 if (lineHeight <= 0) lineHeight = 24;
4039 switch(LOWORD(wParam))
4041 case SB_TOP:
4042 ME_ScrollAbs(editor, 0, 0);
4043 break;
4044 case SB_BOTTOM:
4045 ME_ScrollAbs(editor,
4046 editor->horz_si.nMax - (int)editor->horz_si.nPage,
4047 editor->vert_si.nMax - (int)editor->vert_si.nPage);
4048 break;
4049 case SB_LINEUP:
4050 ME_ScrollUp(editor,lineHeight);
4051 break;
4052 case SB_LINEDOWN:
4053 ME_ScrollDown(editor,lineHeight);
4054 break;
4055 case SB_PAGEUP:
4056 ME_ScrollUp(editor,editor->sizeWindow.cy);
4057 break;
4058 case SB_PAGEDOWN:
4059 ME_ScrollDown(editor,editor->sizeWindow.cy);
4060 break;
4061 case SB_THUMBTRACK:
4062 case SB_THUMBPOSITION:
4064 SCROLLINFO sbi;
4065 sbi.cbSize = sizeof(sbi);
4066 sbi.fMask = SIF_TRACKPOS;
4067 /* Try to get 32-bit track position value. */
4068 if (!GetScrollInfo(editor->hWnd, SB_VERT, &sbi))
4069 /* GetScrollInfo failed, settle for 16-bit value in wParam. */
4070 sbi.nTrackPos = HIWORD(wParam);
4072 ME_VScrollAbs(editor, sbi.nTrackPos);
4073 break;
4076 if (msg == EM_SCROLL)
4077 return 0x00010000 | (((editor->vert_si.nPos - origNPos)/lineHeight) & 0xffff);
4078 break;
4080 case WM_MOUSEWHEEL:
4082 int gcWheelDelta;
4083 UINT pulScrollLines;
4084 BOOL ctrl_is_down;
4086 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
4087 !ME_FilterEvent(editor, msg, &wParam, &lParam))
4088 return 0;
4090 ctrl_is_down = GetKeyState(VK_CONTROL) & 0x8000;
4092 gcWheelDelta = GET_WHEEL_DELTA_WPARAM(wParam);
4094 if (abs(gcWheelDelta) >= WHEEL_DELTA)
4096 if (ctrl_is_down) {
4097 int numerator;
4098 if (!editor->nZoomNumerator || !editor->nZoomDenominator)
4100 numerator = 100;
4101 } else {
4102 numerator = editor->nZoomNumerator * 100 / editor->nZoomDenominator;
4104 numerator = numerator + (gcWheelDelta / WHEEL_DELTA) * 10;
4105 if (numerator >= 10 && numerator <= 500)
4106 ME_SetZoom(editor, numerator, 100);
4107 } else {
4108 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
4109 /* FIXME follow the original */
4110 if (pulScrollLines)
4111 ME_ScrollDown(editor,pulScrollLines * (-gcWheelDelta / WHEEL_DELTA) * 8);
4114 break;
4116 case EM_GETRECT:
4118 *((RECT *)lParam) = editor->rcFormat;
4119 if (editor->bDefaultFormatRect)
4120 ((RECT *)lParam)->left -= editor->selofs;
4121 return 0;
4123 case EM_SETRECT:
4124 case EM_SETRECTNP:
4126 if (lParam)
4128 int border = 0;
4129 RECT clientRect;
4130 RECT *rc = (RECT *)lParam;
4132 border = editor->exStyleFlags & WS_EX_CLIENTEDGE ? 1 : 0;
4133 ITextHost_TxGetClientRect(editor->texthost, &clientRect);
4134 if (wParam == 0)
4136 editor->rcFormat.top = max(0, rc->top - border);
4137 editor->rcFormat.left = max(0, rc->left - border);
4138 editor->rcFormat.bottom = min(clientRect.bottom, rc->bottom);
4139 editor->rcFormat.right = min(clientRect.right, rc->right + border);
4140 } else if (wParam == 1) {
4141 /* MSDN incorrectly says a wParam value of 1 causes the
4142 * lParam rect to be used as a relative offset,
4143 * however, the tests show it just prevents min/max bound
4144 * checking. */
4145 editor->rcFormat.top = rc->top - border;
4146 editor->rcFormat.left = rc->left - border;
4147 editor->rcFormat.bottom = rc->bottom;
4148 editor->rcFormat.right = rc->right + border;
4149 } else {
4150 return 0;
4152 editor->bDefaultFormatRect = FALSE;
4154 else
4156 ME_SetDefaultFormatRect(editor);
4157 editor->bDefaultFormatRect = TRUE;
4159 ME_MarkAllForWrapping(editor);
4160 ME_WrapMarkedParagraphs(editor);
4161 ME_UpdateScrollBar(editor);
4162 if (msg != EM_SETRECTNP)
4163 ME_Repaint(editor);
4164 return 0;
4166 case EM_REQUESTRESIZE:
4167 ME_SendRequestResize(editor, TRUE);
4168 return 0;
4169 case WM_SETREDRAW:
4170 goto do_default;
4171 case WM_SIZE:
4173 RECT clientRect;
4175 ITextHost_TxGetClientRect(editor->texthost, &clientRect);
4176 if (editor->bDefaultFormatRect) {
4177 ME_SetDefaultFormatRect(editor);
4178 } else {
4179 editor->rcFormat.right += clientRect.right - editor->prevClientRect.right;
4180 editor->rcFormat.bottom += clientRect.bottom - editor->prevClientRect.bottom;
4182 editor->prevClientRect = clientRect;
4183 ME_RewrapRepaint(editor);
4184 goto do_default;
4186 /* IME messages to make richedit controls IME aware */
4187 case WM_IME_SETCONTEXT:
4188 case WM_IME_CONTROL:
4189 case WM_IME_SELECT:
4190 case WM_IME_COMPOSITIONFULL:
4191 return 0;
4192 case WM_IME_STARTCOMPOSITION:
4194 editor->imeStartIndex=ME_GetCursorOfs(editor,0);
4195 ME_DeleteSelection(editor);
4196 ME_CommitUndo(editor);
4197 ME_UpdateRepaint(editor);
4198 return 0;
4200 case WM_IME_COMPOSITION:
4202 HIMC hIMC;
4204 ME_Style *style = ME_GetInsertStyle(editor, 0);
4205 hIMC = ITextHost_TxImmGetContext(editor->texthost);
4206 ME_DeleteSelection(editor);
4207 ME_CommitUndo(editor);
4208 ME_SaveTempStyle(editor);
4209 if (lParam & GCS_RESULTSTR)
4211 LPWSTR lpCompStr = NULL;
4212 DWORD dwBufLen;
4214 dwBufLen = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
4215 lpCompStr = HeapAlloc(GetProcessHeap(),0,dwBufLen + sizeof(WCHAR));
4216 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, lpCompStr, dwBufLen);
4217 lpCompStr[dwBufLen/sizeof(WCHAR)] = 0;
4218 ME_InsertTextFromCursor(editor,0,lpCompStr,dwBufLen/sizeof(WCHAR),style);
4220 else if (lParam & GCS_COMPSTR)
4222 LPWSTR lpCompStr = NULL;
4223 DWORD dwBufLen;
4225 dwBufLen = ImmGetCompositionStringW(hIMC, GCS_COMPSTR, NULL, 0);
4226 lpCompStr = HeapAlloc(GetProcessHeap(),0,dwBufLen + sizeof(WCHAR));
4227 ImmGetCompositionStringW(hIMC, GCS_COMPSTR, lpCompStr, dwBufLen);
4228 lpCompStr[dwBufLen/sizeof(WCHAR)] = 0;
4230 ME_InsertTextFromCursor(editor,0,lpCompStr,dwBufLen/sizeof(WCHAR),style);
4231 ME_SetSelection(editor,editor->imeStartIndex,
4232 editor->imeStartIndex + dwBufLen/sizeof(WCHAR));
4234 ME_ReleaseStyle(style);
4235 ME_UpdateRepaint(editor);
4236 return 0;
4238 case WM_IME_ENDCOMPOSITION:
4240 ME_DeleteSelection(editor);
4241 editor->imeStartIndex=-1;
4242 return 0;
4244 case EM_GETOLEINTERFACE:
4246 LPVOID *ppvObj = (LPVOID*) lParam;
4247 return CreateIRichEditOle(editor, ppvObj);
4249 case EM_GETPASSWORDCHAR:
4251 return editor->cPasswordMask;
4253 case EM_SETOLECALLBACK:
4254 if(editor->lpOleCallback)
4255 IUnknown_Release(editor->lpOleCallback);
4256 editor->lpOleCallback = (LPRICHEDITOLECALLBACK)lParam;
4257 if(editor->lpOleCallback)
4258 IUnknown_AddRef(editor->lpOleCallback);
4259 return TRUE;
4260 case EM_GETWORDBREAKPROC:
4261 return (LRESULT)editor->pfnWordBreak;
4262 case EM_SETWORDBREAKPROC:
4264 EDITWORDBREAKPROCW pfnOld = editor->pfnWordBreak;
4266 editor->pfnWordBreak = (EDITWORDBREAKPROCW)lParam;
4267 return (LRESULT)pfnOld;
4269 case EM_GETTEXTMODE:
4270 return editor->mode;
4271 case EM_SETTEXTMODE:
4273 LRESULT ret;
4274 int mask = 0;
4275 int changes = 0;
4276 GETTEXTLENGTHEX how;
4278 /* CR/LF conversion required in 2.0 mode, verbatim in 1.0 mode */
4279 how.flags = GTL_CLOSE | (editor->bEmulateVersion10 ? 0 : GTL_USECRLF) | GTL_NUMCHARS;
4280 how.codepage = unicode ? 1200 : CP_ACP;
4281 ret = ME_GetTextLengthEx(editor, &how);
4282 if (!ret)
4284 /*Check for valid wParam*/
4285 if ((((wParam & TM_RICHTEXT) && ((wParam & TM_PLAINTEXT) << 1))) ||
4286 (((wParam & TM_MULTILEVELUNDO) && ((wParam & TM_SINGLELEVELUNDO) << 1))) ||
4287 (((wParam & TM_MULTICODEPAGE) && ((wParam & TM_SINGLECODEPAGE) << 1))))
4288 return 1;
4289 else
4291 if (wParam & (TM_RICHTEXT | TM_PLAINTEXT))
4293 mask |= (TM_RICHTEXT | TM_PLAINTEXT);
4294 changes |= (wParam & (TM_RICHTEXT | TM_PLAINTEXT));
4296 /*FIXME: Currently no support for undo level and code page options*/
4297 editor->mode = (editor->mode & (~mask)) | changes;
4298 return 0;
4301 return ret;
4303 case EM_SETPASSWORDCHAR:
4305 editor->cPasswordMask = wParam;
4306 ME_RewrapRepaint(editor);
4307 return 0;
4309 case EM_SETTARGETDEVICE:
4310 if (wParam == 0)
4312 BOOL new = (lParam == 0 && (editor->styleFlags & ES_MULTILINE));
4313 if (editor->nAvailWidth || editor->bWordWrap != new)
4315 editor->bWordWrap = new;
4316 editor->nAvailWidth = 0; /* wrap to client area */
4317 ME_RewrapRepaint(editor);
4319 } else {
4320 int width = max(0, lParam);
4321 if ((editor->styleFlags & ES_MULTILINE) &&
4322 (!editor->bWordWrap || editor->nAvailWidth != width))
4324 editor->nAvailWidth = width;
4325 editor->bWordWrap = TRUE;
4326 ME_RewrapRepaint(editor);
4328 FIXME("EM_SETTARGETDEVICE doesn't use non-NULL target devices\n");
4330 return TRUE;
4331 default:
4332 do_default:
4333 *phresult = S_FALSE;
4334 break;
4336 return 0L;
4339 static LRESULT RichEditWndProc_common(HWND hWnd, UINT msg, WPARAM wParam,
4340 LPARAM lParam, BOOL unicode)
4342 ME_TextEditor *editor;
4343 HRESULT hresult;
4344 LRESULT lresult = 0;
4346 TRACE("enter hwnd %p msg %04x (%s) %lx %lx, unicode %d\n",
4347 hWnd, msg, get_msg_name(msg), wParam, lParam, unicode);
4349 editor = (ME_TextEditor *)GetWindowLongPtrW(hWnd, 0);
4350 if (!editor)
4352 if (msg == WM_NCCREATE)
4354 CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
4355 ITextHost *texthost;
4357 TRACE("WM_NCCREATE: hWnd %p style 0x%08x\n", hWnd, pcs->style);
4358 texthost = ME_CreateTextHost(hWnd, FALSE);
4359 return texthost != NULL;
4361 else if (msg != WM_NCDESTROY)
4363 ERR("called with invalid hWnd %p - application bug?\n", hWnd);
4364 return 0;
4368 switch (msg)
4370 case WM_PAINT:
4372 HDC hDC;
4373 RECT rc;
4374 PAINTSTRUCT ps;
4376 hDC = BeginPaint(editor->hWnd, &ps);
4377 /* Erase area outside of the formatting rectangle */
4378 if (ps.rcPaint.top < editor->rcFormat.top)
4380 rc = ps.rcPaint;
4381 rc.bottom = editor->rcFormat.top;
4382 FillRect(hDC, &rc, editor->hbrBackground);
4383 ps.rcPaint.top = editor->rcFormat.top;
4385 if (ps.rcPaint.bottom > editor->rcFormat.bottom) {
4386 rc = ps.rcPaint;
4387 rc.top = editor->rcFormat.bottom;
4388 FillRect(hDC, &rc, editor->hbrBackground);
4389 ps.rcPaint.bottom = editor->rcFormat.bottom;
4391 if (ps.rcPaint.left < editor->rcFormat.left) {
4392 rc = ps.rcPaint;
4393 rc.right = editor->rcFormat.left;
4394 FillRect(hDC, &rc, editor->hbrBackground);
4395 ps.rcPaint.left = editor->rcFormat.left;
4397 if (ps.rcPaint.right > editor->rcFormat.right) {
4398 rc = ps.rcPaint;
4399 rc.left = editor->rcFormat.right;
4400 FillRect(hDC, &rc, editor->hbrBackground);
4401 ps.rcPaint.right = editor->rcFormat.right;
4404 ME_PaintContent(editor, hDC, FALSE, &ps.rcPaint);
4405 EndPaint(editor->hWnd, &ps);
4406 return 0;
4408 case WM_ERASEBKGND:
4410 HDC hDC = (HDC)wParam;
4411 RECT rc;
4413 if (GetUpdateRect(editor->hWnd, &rc, TRUE))
4414 FillRect(hDC, &rc, editor->hbrBackground);
4415 return 1;
4417 case EM_SETOPTIONS:
4419 DWORD dwStyle;
4420 const DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
4421 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN |
4422 ECO_SELECTIONBAR;
4423 lresult = ME_HandleMessage(editor, msg, wParam, lParam, unicode, &hresult);
4424 dwStyle = GetWindowLongW(hWnd, GWL_STYLE);
4425 dwStyle = (dwStyle & ~mask) | (lresult & mask);
4426 SetWindowLongW(hWnd, GWL_STYLE, dwStyle);
4427 return lresult;
4429 case EM_SETREADONLY:
4431 DWORD dwStyle;
4432 lresult = ME_HandleMessage(editor, msg, wParam, lParam, unicode, &hresult);
4433 dwStyle = GetWindowLongW(hWnd, GWL_STYLE);
4434 dwStyle &= ~ES_READONLY;
4435 if (wParam)
4436 dwStyle |= ES_READONLY;
4437 SetWindowLongW(hWnd, GWL_STYLE, dwStyle);
4438 return lresult;
4440 default:
4441 lresult = ME_HandleMessage(editor, msg, wParam, lParam, unicode, &hresult);
4444 if (hresult == S_FALSE)
4445 lresult = DefWindowProcW(hWnd, msg, wParam, lParam);
4447 TRACE("exit hwnd %p msg %04x (%s) %lx %lx, unicode %d -> %lu\n",
4448 hWnd, msg, get_msg_name(msg), wParam, lParam, unicode, lresult);
4450 return lresult;
4453 static LRESULT WINAPI RichEditWndProcW(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
4455 BOOL unicode = TRUE;
4457 /* Under Win9x RichEdit20W returns ANSI strings, see the tests. */
4458 if (msg == WM_GETTEXT && (GetVersion() & 0x80000000))
4459 unicode = FALSE;
4461 return RichEditWndProc_common(hWnd, msg, wParam, lParam, unicode);
4464 static LRESULT WINAPI RichEditWndProcA(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
4466 return RichEditWndProc_common(hWnd, msg, wParam, lParam, FALSE);
4469 /******************************************************************
4470 * RichEditANSIWndProc (RICHED20.10)
4472 LRESULT WINAPI RichEditANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
4474 return RichEditWndProcA(hWnd, msg, wParam, lParam);
4477 /******************************************************************
4478 * RichEdit10ANSIWndProc (RICHED20.9)
4480 LRESULT WINAPI RichEdit10ANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
4482 if (msg == WM_NCCREATE && !GetWindowLongPtrW(hWnd, 0))
4484 ITextHost *texthost;
4485 CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
4487 TRACE("WM_NCCREATE: hWnd %p style 0x%08x\n", hWnd, pcs->style);
4488 texthost = ME_CreateTextHost(hWnd, TRUE);
4489 return texthost != NULL;
4491 return RichEditANSIWndProc(hWnd, msg, wParam, lParam);
4494 void ME_SendOldNotify(ME_TextEditor *editor, int nCode)
4496 ITextHost_TxNotify(editor->texthost, nCode, NULL);
4499 int ME_CountParagraphsBetween(ME_TextEditor *editor, int from, int to)
4501 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
4502 int i = 0;
4504 while(item && item->member.para.next_para->member.para.nCharOfs <= from)
4505 item = item->member.para.next_para;
4506 if (!item)
4507 return 0;
4508 while(item && item->member.para.next_para->member.para.nCharOfs <= to) {
4509 item = item->member.para.next_para;
4510 i++;
4512 return i;
4516 int ME_GetTextW(ME_TextEditor *editor, WCHAR *buffer, int nStart,
4517 int nChars, int bCRLF)
4519 ME_DisplayItem *pRun;
4520 int nOffset, nWritten = 0;
4521 WCHAR *pStart = buffer;
4523 ME_RunOfsFromCharOfs(editor, nStart, NULL, &pRun, &nOffset);
4525 /* bCRLF flag is only honored in 2.0 and up. 1.0 must always return text verbatim */
4526 if (editor->bEmulateVersion10) bCRLF = 0;
4528 while (nChars && pRun)
4530 int nLen;
4532 if (pRun->member.run.nFlags & MERF_ENDCELL &&
4533 pRun->member.run.nFlags & MERF_ENDPARA)
4535 *buffer = '\t';
4536 nLen = 1;
4537 } else if (pRun->member.run.nFlags & MERF_ENDPARA) {
4538 if (!ME_FindItemFwd(pRun, diRun)) {
4539 /* No '\r' is appended to the last paragraph. */
4540 nLen = 0;
4541 } else if (bCRLF && nChars == 1) {
4542 nLen = 0;
4543 nChars = 0;
4544 } else {
4545 WCHAR cr_lf[] = {'\r', '\n', 0};
4546 WCHAR *szData;
4548 if (bCRLF)
4550 nLen = 2;
4551 szData = cr_lf;
4552 } else {
4553 nLen = pRun->member.run.strText->nLen;
4554 szData = pRun->member.run.strText->szData;
4556 nLen = min(nChars, nLen - nOffset);
4557 CopyMemory(buffer, szData + nOffset, sizeof(WCHAR) * nLen);
4559 } else {
4560 nLen = min(nChars, pRun->member.run.strText->nLen - nOffset);
4561 CopyMemory(buffer, pRun->member.run.strText->szData + nOffset,
4562 sizeof(WCHAR) * nLen);
4564 nChars -= nLen;
4565 nWritten += nLen;
4566 buffer += nLen;
4567 nOffset = 0;
4569 pRun = ME_FindItemFwd(pRun, diRun);
4571 *buffer = 0;
4572 TRACE("nWritten=%d, actual=%d\n", nWritten, buffer-pStart);
4573 return nWritten;
4576 static BOOL ME_RegisterEditorClass(HINSTANCE hInstance)
4578 WNDCLASSW wcW;
4579 WNDCLASSA wcA;
4581 wcW.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
4582 wcW.lpfnWndProc = RichEditWndProcW;
4583 wcW.cbClsExtra = 0;
4584 wcW.cbWndExtra = sizeof(ME_TextEditor *);
4585 wcW.hInstance = NULL; /* hInstance would register DLL-local class */
4586 wcW.hIcon = NULL;
4587 wcW.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
4588 wcW.hbrBackground = GetStockObject(NULL_BRUSH);
4589 wcW.lpszMenuName = NULL;
4591 if (is_version_nt())
4593 wcW.lpszClassName = RichEdit20W;
4594 if (!RegisterClassW(&wcW)) return FALSE;
4595 wcW.lpszClassName = RichEdit50W;
4596 if (!RegisterClassW(&wcW)) return FALSE;
4598 else
4600 /* WNDCLASSA/W have the same layout */
4601 wcW.lpszClassName = (LPCWSTR)"RichEdit20W";
4602 if (!RegisterClassA((WNDCLASSA *)&wcW)) return FALSE;
4603 wcW.lpszClassName = (LPCWSTR)"RichEdit50W";
4604 if (!RegisterClassA((WNDCLASSA *)&wcW)) return FALSE;
4607 wcA.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
4608 wcA.lpfnWndProc = RichEditWndProcA;
4609 wcA.cbClsExtra = 0;
4610 wcA.cbWndExtra = sizeof(ME_TextEditor *);
4611 wcA.hInstance = NULL; /* hInstance would register DLL-local class */
4612 wcA.hIcon = NULL;
4613 wcA.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
4614 wcA.hbrBackground = GetStockObject(NULL_BRUSH);
4615 wcA.lpszMenuName = NULL;
4616 wcA.lpszClassName = "RichEdit20A";
4617 if (!RegisterClassA(&wcA)) return FALSE;
4618 wcA.lpszClassName = "RichEdit50A";
4619 if (!RegisterClassA(&wcA)) return FALSE;
4621 return TRUE;
4624 LRESULT WINAPI REComboWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
4625 /* FIXME: Not implemented */
4626 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
4627 hWnd, msg, get_msg_name(msg), wParam, lParam);
4628 return DefWindowProcW(hWnd, msg, wParam, lParam);
4631 LRESULT WINAPI REListWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
4632 /* FIXME: Not implemented */
4633 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
4634 hWnd, msg, get_msg_name(msg), wParam, lParam);
4635 return DefWindowProcW(hWnd, msg, wParam, lParam);
4638 /******************************************************************
4639 * REExtendedRegisterClass (RICHED20.8)
4641 * FIXME undocumented
4642 * Need to check for errors and implement controls and callbacks
4644 LRESULT WINAPI REExtendedRegisterClass(void)
4646 WNDCLASSW wcW;
4647 UINT result;
4649 FIXME("semi stub\n");
4651 wcW.cbClsExtra = 0;
4652 wcW.cbWndExtra = 4;
4653 wcW.hInstance = NULL;
4654 wcW.hIcon = NULL;
4655 wcW.hCursor = NULL;
4656 wcW.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
4657 wcW.lpszMenuName = NULL;
4659 if (!ME_ListBoxRegistered)
4661 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS;
4662 wcW.lpfnWndProc = REListWndProc;
4663 wcW.lpszClassName = REListBox20W;
4664 if (RegisterClassW(&wcW)) ME_ListBoxRegistered = TRUE;
4667 if (!ME_ComboBoxRegistered)
4669 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
4670 wcW.lpfnWndProc = REComboWndProc;
4671 wcW.lpszClassName = REComboBox20W;
4672 if (RegisterClassW(&wcW)) ME_ComboBoxRegistered = TRUE;
4675 result = 0;
4676 if (ME_ListBoxRegistered)
4677 result += 1;
4678 if (ME_ComboBoxRegistered)
4679 result += 2;
4681 return result;
4684 static BOOL isurlspecial(WCHAR c)
4686 static const WCHAR special_chars[] = {'.','/','%','@','*','|','\\','+','#',0};
4687 return strchrW( special_chars, c ) != NULL;
4691 * This proc takes a selection, and scans it forward in order to select the span
4692 * of a possible URL candidate. A possible URL candidate must start with isalnum
4693 * or one of the following special characters: *|/\+%#@ and must consist entirely
4694 * of the characters allowed to start the URL, plus : (colon) which may occur
4695 * at most once, and not at either end.
4697 * sel_max == -1 indicates scan to end of text.
4699 static BOOL ME_FindNextURLCandidate(ME_TextEditor *editor, int sel_min, int sel_max,
4700 int * candidate_min, int * candidate_max)
4702 ME_DisplayItem * item;
4703 ME_DisplayItem * para;
4704 int nStart;
4705 BOOL foundColon = FALSE;
4706 WCHAR lastAcceptedChar = '\0';
4708 TRACE("sel_min = %d sel_max = %d\n", sel_min, sel_max);
4710 *candidate_min = *candidate_max = -1;
4711 ME_RunOfsFromCharOfs(editor, sel_min, &para, &item, &nStart);
4712 TRACE("nStart = %d\n", nStart);
4713 if (sel_max == -1) sel_max = ME_GetTextLength(editor);
4714 while (item && para->member.para.nCharOfs + item->member.run.nCharOfs + nStart < sel_max)
4716 ME_DisplayItem * next_item;
4718 if (!(item->member.run.nFlags & MERF_ENDPARA)) {
4719 /* Find start of candidate */
4720 if (*candidate_min == -1) {
4721 while (nStart < item->member.run.strText->nLen &&
4722 !(isalnumW(item->member.run.strText->szData[nStart]) ||
4723 isurlspecial(item->member.run.strText->szData[nStart]))) {
4724 nStart++;
4726 if (nStart < item->member.run.strText->nLen &&
4727 (isalnumW(item->member.run.strText->szData[nStart]) ||
4728 isurlspecial(item->member.run.strText->szData[nStart]))) {
4729 *candidate_min = para->member.para.nCharOfs + item->member.run.nCharOfs + nStart;
4730 lastAcceptedChar = item->member.run.strText->szData[nStart];
4731 nStart++;
4735 /* Find end of candidate */
4736 if (*candidate_min >= 0) {
4737 while (nStart < item->member.run.strText->nLen &&
4738 (isalnumW(item->member.run.strText->szData[nStart]) ||
4739 isurlspecial(item->member.run.strText->szData[nStart]) ||
4740 (!foundColon && item->member.run.strText->szData[nStart] == ':') )) {
4741 if (item->member.run.strText->szData[nStart] == ':') foundColon = TRUE;
4742 lastAcceptedChar = item->member.run.strText->szData[nStart];
4743 nStart++;
4745 if (nStart < item->member.run.strText->nLen &&
4746 !(isalnumW(item->member.run.strText->szData[nStart]) ||
4747 isurlspecial(item->member.run.strText->szData[nStart]) )) {
4748 *candidate_max = para->member.para.nCharOfs + item->member.run.nCharOfs + nStart;
4749 nStart++;
4750 if (lastAcceptedChar == ':') (*candidate_max)--;
4751 return TRUE;
4754 } else {
4755 /* End of paragraph: skip it if before candidate span, or terminates
4756 current active span */
4757 if (*candidate_min >= 0) {
4758 *candidate_max = para->member.para.nCharOfs + item->member.run.nCharOfs;
4759 if (lastAcceptedChar == ':') (*candidate_max)--;
4760 return TRUE;
4764 /* Reaching this point means no span was found, so get next span */
4765 next_item = ME_FindItemFwd(item, diRun);
4766 if (!next_item) {
4767 if (*candidate_min >= 0) {
4768 /* There are no further runs, so take end of text as end of candidate */
4769 *candidate_max = para->member.para.nCharOfs + item->member.run.nCharOfs + nStart;
4770 if (lastAcceptedChar == ':') (*candidate_max)--;
4771 return TRUE;
4774 item = next_item;
4775 para = ME_GetParagraph(item);
4776 nStart = 0;
4779 if (item) {
4780 if (*candidate_min >= 0) {
4781 /* There are no further runs, so take end of text as end of candidate */
4782 *candidate_max = para->member.para.nCharOfs + item->member.run.nCharOfs + nStart;
4783 if (lastAcceptedChar == ':') (*candidate_max)--;
4784 return TRUE;
4787 return FALSE;
4791 * This proc evaluates the selection and returns TRUE if it can be considered an URL
4793 static BOOL ME_IsCandidateAnURL(ME_TextEditor *editor, int sel_min, int sel_max)
4795 struct prefix_s {
4796 const char *text;
4797 int length;
4798 } prefixes[12] = {
4799 /* Code below depends on these being in decreasing length order! */
4800 {"prospero:", 10},
4801 {"telnet:", 8},
4802 {"gopher:", 8},
4803 {"mailto:", 8},
4804 {"https:", 7},
4805 {"file:", 6},
4806 {"news:", 6},
4807 {"wais:", 6},
4808 {"nntp:", 6},
4809 {"http:", 5},
4810 {"www.", 5},
4811 {"ftp:", 5},
4813 LPWSTR bufferW = NULL;
4814 WCHAR bufW[32];
4815 unsigned int i;
4817 if (sel_max == -1) sel_max = ME_GetTextLength(editor);
4818 assert(sel_min <= sel_max);
4819 for (i = 0; i < sizeof(prefixes) / sizeof(struct prefix_s); i++)
4821 if (sel_max - sel_min < prefixes[i].length) continue;
4822 if (bufferW == NULL) {
4823 bufferW = heap_alloc((sel_max - sel_min + 1) * sizeof(WCHAR));
4825 ME_GetTextW(editor, bufferW, sel_min, min(sel_max - sel_min, lstrlenA(prefixes[i].text)), 0);
4826 MultiByteToWideChar(CP_ACP, 0, prefixes[i].text, -1, bufW, 32);
4827 if (!lstrcmpW(bufW, bufferW))
4829 heap_free(bufferW);
4830 return TRUE;
4833 heap_free(bufferW);
4834 return FALSE;
4838 * This proc walks through the indicated selection and evaluates whether each
4839 * section identified by ME_FindNextURLCandidate and in-between sections have
4840 * their proper CFE_LINK attributes set or unset. If the CFE_LINK attribute is
4841 * not what it is supposed to be, this proc sets or unsets it as appropriate.
4843 * Returns TRUE if at least one section was modified.
4845 static BOOL ME_UpdateLinkAttribute(ME_TextEditor *editor, int sel_min, int sel_max)
4847 BOOL modified = FALSE;
4848 int cMin, cMax;
4850 if (sel_max == -1) sel_max = ME_GetTextLength(editor);
4853 int beforeURL[2];
4854 int inURL[2];
4855 CHARFORMAT2W link;
4857 if (ME_FindNextURLCandidate(editor, sel_min, sel_max, &cMin, &cMax))
4859 /* Section before candidate is not an URL */
4860 beforeURL[0] = sel_min;
4861 beforeURL[1] = cMin;
4863 if (ME_IsCandidateAnURL(editor, cMin, cMax))
4865 inURL[0] = cMin; inURL[1] = cMax;
4867 else
4869 beforeURL[1] = cMax;
4870 inURL[0] = inURL[1] = -1;
4872 sel_min = cMax;
4874 else
4876 /* No more candidates until end of selection */
4877 beforeURL[0] = sel_min;
4878 beforeURL[1] = sel_max;
4879 inURL[0] = inURL[1] = -1;
4880 sel_min = sel_max;
4883 if (beforeURL[0] < beforeURL[1])
4885 /* CFE_LINK effect should be consistently unset */
4886 link.cbSize = sizeof(link);
4887 ME_GetCharFormat(editor, beforeURL[0], beforeURL[1], &link);
4888 if (!(link.dwMask & CFM_LINK) || (link.dwEffects & CFE_LINK))
4890 /* CFE_LINK must be unset from this range */
4891 memset(&link, 0, sizeof(CHARFORMAT2W));
4892 link.cbSize = sizeof(link);
4893 link.dwMask = CFM_LINK;
4894 link.dwEffects = 0;
4895 ME_SetCharFormat(editor, beforeURL[0], beforeURL[1] - beforeURL[0], &link);
4896 modified = TRUE;
4899 if (inURL[0] < inURL[1])
4901 /* CFE_LINK effect should be consistently set */
4902 link.cbSize = sizeof(link);
4903 ME_GetCharFormat(editor, inURL[0], inURL[1], &link);
4904 if (!(link.dwMask & CFM_LINK) || !(link.dwEffects & CFE_LINK))
4906 /* CFE_LINK must be set on this range */
4907 memset(&link, 0, sizeof(CHARFORMAT2W));
4908 link.cbSize = sizeof(link);
4909 link.dwMask = CFM_LINK;
4910 link.dwEffects = CFE_LINK;
4911 ME_SetCharFormat(editor, inURL[0], inURL[1] - inURL[0], &link);
4912 modified = TRUE;
4915 } while (sel_min < sel_max);
4916 return modified;