kernel32: Update Italian translation.
[wine/testsucceed.git] / dlls / riched20 / editor.c
blobb4bff4f07d97bde65d6017939a6380f1f3b744c2
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 (seems to do nothing)
145 + WM_STYLECHANGED (seems to do nothing)
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, ME_Cursor *start, int nChars);
246 static const WCHAR REListBox20W[] = {'R','E','L','i','s','t','B','o','x','2','0','W', 0};
247 static const WCHAR REComboBox20W[] = {'R','E','C','o','m','b','o','B','o','x','2','0','W', 0};
248 static HCURSOR hLeft;
250 int me_debug = 0;
251 HANDLE me_heap = NULL;
253 static BOOL ME_ListBoxRegistered = FALSE;
254 static BOOL ME_ComboBoxRegistered = FALSE;
256 static inline int is_version_nt(void)
258 return !(GetVersion() & 0x80000000);
261 static ME_TextBuffer *ME_MakeText(void) {
263 ME_TextBuffer *buf = ALLOC_OBJ(ME_TextBuffer);
265 ME_DisplayItem *p1 = ME_MakeDI(diTextStart);
266 ME_DisplayItem *p2 = ME_MakeDI(diTextEnd);
268 p1->prev = NULL;
269 p1->next = p2;
270 p2->prev = p1;
271 p2->next = NULL;
272 p1->member.para.next_para = p2;
273 p2->member.para.prev_para = p1;
274 p2->member.para.nCharOfs = 0;
276 buf->pFirst = p1;
277 buf->pLast = p2;
278 buf->pCharStyle = NULL;
280 return buf;
284 static LRESULT ME_StreamInText(ME_TextEditor *editor, DWORD dwFormat, ME_InStream *stream, ME_Style *style)
286 WCHAR wszText[STREAMIN_BUFFER_SIZE+1];
287 WCHAR *pText;
289 TRACE("%08x %p\n", dwFormat, stream);
291 do {
292 LONG nWideChars = 0;
294 if (!stream->dwSize)
296 ME_StreamInFill(stream);
297 if (stream->editstream->dwError)
298 break;
299 if (!stream->dwSize)
300 break;
303 if (!(dwFormat & SF_UNICODE))
305 /* FIXME? this is doomed to fail on true MBCS like UTF-8, luckily they're unlikely to be used as CP_ACP */
306 nWideChars = MultiByteToWideChar(CP_ACP, 0, stream->buffer, stream->dwSize, wszText, STREAMIN_BUFFER_SIZE);
307 pText = wszText;
309 else
311 nWideChars = stream->dwSize >> 1;
312 pText = (WCHAR *)stream->buffer;
315 ME_InsertTextFromCursor(editor, 0, pText, nWideChars, style);
316 if (stream->dwSize == 0)
317 break;
318 stream->dwSize = 0;
319 } while(1);
320 ME_CommitUndo(editor);
321 ME_UpdateRepaint(editor);
322 return 0;
325 static void ME_ApplyBorderProperties(RTF_Info *info,
326 ME_BorderRect *borderRect,
327 RTFBorder *borderDef)
329 int i, colorNum;
330 ME_Border *pBorders[] = {&borderRect->top,
331 &borderRect->left,
332 &borderRect->bottom,
333 &borderRect->right};
334 for (i = 0; i < 4; i++)
336 RTFColor *colorDef = info->colorList;
337 pBorders[i]->width = borderDef[i].width;
338 colorNum = borderDef[i].color;
339 while (colorDef && colorDef->rtfCNum != colorNum)
340 colorDef = colorDef->rtfNextColor;
341 if (colorDef)
342 pBorders[i]->colorRef = RGB(
343 colorDef->rtfCRed >= 0 ? colorDef->rtfCRed : 0,
344 colorDef->rtfCGreen >= 0 ? colorDef->rtfCGreen : 0,
345 colorDef->rtfCBlue >= 0 ? colorDef->rtfCBlue : 0);
346 else
347 pBorders[i]->colorRef = RGB(0, 0, 0);
351 void ME_RTFCharAttrHook(RTF_Info *info)
353 CHARFORMAT2W fmt;
354 fmt.cbSize = sizeof(fmt);
355 fmt.dwMask = 0;
356 fmt.dwEffects = 0;
358 switch(info->rtfMinor)
360 case rtfPlain:
361 /* FIXME add more flags once they're implemented */
362 fmt.dwMask = CFM_BOLD | CFM_ITALIC | CFM_UNDERLINETYPE | CFM_STRIKEOUT | CFM_COLOR | CFM_BACKCOLOR | CFM_SIZE | CFM_WEIGHT;
363 fmt.dwEffects = CFE_AUTOCOLOR | CFE_AUTOBACKCOLOR;
364 fmt.yHeight = 12*20; /* 12pt */
365 fmt.wWeight = FW_NORMAL;
366 fmt.bUnderlineType = CFU_UNDERLINENONE;
367 break;
368 case rtfBold:
369 fmt.dwMask = CFM_BOLD | CFM_WEIGHT;
370 fmt.dwEffects = info->rtfParam ? CFE_BOLD : 0;
371 fmt.wWeight = info->rtfParam ? FW_BOLD : FW_NORMAL;
372 break;
373 case rtfItalic:
374 fmt.dwMask = CFM_ITALIC;
375 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
376 break;
377 case rtfUnderline:
378 fmt.dwMask = CFM_UNDERLINETYPE;
379 fmt.bUnderlineType = info->rtfParam ? CFU_CF1UNDERLINE : CFU_UNDERLINENONE;
380 break;
381 case rtfDotUnderline:
382 fmt.dwMask = CFM_UNDERLINETYPE;
383 fmt.bUnderlineType = info->rtfParam ? CFU_UNDERLINEDOTTED : CFU_UNDERLINENONE;
384 break;
385 case rtfDbUnderline:
386 fmt.dwMask = CFM_UNDERLINETYPE;
387 fmt.bUnderlineType = info->rtfParam ? CFU_UNDERLINEDOUBLE : CFU_UNDERLINENONE;
388 break;
389 case rtfWordUnderline:
390 fmt.dwMask = CFM_UNDERLINETYPE;
391 fmt.bUnderlineType = info->rtfParam ? CFU_UNDERLINEWORD : CFU_UNDERLINENONE;
392 break;
393 case rtfNoUnderline:
394 fmt.dwMask = CFM_UNDERLINETYPE;
395 fmt.bUnderlineType = CFU_UNDERLINENONE;
396 break;
397 case rtfStrikeThru:
398 fmt.dwMask = CFM_STRIKEOUT;
399 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
400 break;
401 case rtfSubScript:
402 case rtfSuperScript:
403 case rtfSubScrShrink:
404 case rtfSuperScrShrink:
405 case rtfNoSuperSub:
406 fmt.dwMask = CFM_SUBSCRIPT|CFM_SUPERSCRIPT;
407 if (info->rtfMinor == rtfSubScrShrink) fmt.dwEffects = CFE_SUBSCRIPT;
408 if (info->rtfMinor == rtfSuperScrShrink) fmt.dwEffects = CFE_SUPERSCRIPT;
409 if (info->rtfMinor == rtfNoSuperSub) fmt.dwEffects = 0;
410 break;
411 case rtfInvisible:
412 fmt.dwMask = CFM_HIDDEN;
413 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
414 break;
415 case rtfBackColor:
416 fmt.dwMask = CFM_BACKCOLOR;
417 fmt.dwEffects = 0;
418 if (info->rtfParam == 0)
419 fmt.dwEffects = CFE_AUTOBACKCOLOR;
420 else if (info->rtfParam != rtfNoParam)
422 RTFColor *c = RTFGetColor(info, info->rtfParam);
423 if (c && c->rtfCBlue >= 0)
424 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
425 else
426 fmt.dwEffects = CFE_AUTOBACKCOLOR;
428 break;
429 case rtfForeColor:
430 fmt.dwMask = CFM_COLOR;
431 fmt.dwEffects = 0;
432 if (info->rtfParam == 0)
433 fmt.dwEffects = CFE_AUTOCOLOR;
434 else if (info->rtfParam != rtfNoParam)
436 RTFColor *c = RTFGetColor(info, info->rtfParam);
437 if (c && c->rtfCBlue >= 0)
438 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
439 else {
440 fmt.dwEffects = CFE_AUTOCOLOR;
443 break;
444 case rtfFontNum:
445 if (info->rtfParam != rtfNoParam)
447 RTFFont *f = RTFGetFont(info, info->rtfParam);
448 if (f)
450 MultiByteToWideChar(CP_ACP, 0, f->rtfFName, -1, fmt.szFaceName, sizeof(fmt.szFaceName)/sizeof(WCHAR));
451 fmt.szFaceName[sizeof(fmt.szFaceName)/sizeof(WCHAR)-1] = '\0';
452 fmt.bCharSet = f->rtfFCharSet;
453 fmt.dwMask = CFM_FACE | CFM_CHARSET;
454 fmt.bPitchAndFamily = f->rtfFPitch | (f->rtfFFamily << 4);
457 break;
458 case rtfFontSize:
459 fmt.dwMask = CFM_SIZE;
460 if (info->rtfParam != rtfNoParam)
461 fmt.yHeight = info->rtfParam*10;
462 break;
464 if (fmt.dwMask) {
465 ME_Style *style2;
466 RTFFlushOutputBuffer(info);
467 /* FIXME too slow ? how come ? */
468 style2 = ME_ApplyStyle(info->style, &fmt);
469 ME_ReleaseStyle(info->style);
470 info->style = style2;
471 info->styleChanged = TRUE;
475 /* FIXME this function doesn't get any information about context of the RTF tag, which is very bad,
476 the same tags mean different things in different contexts */
477 void ME_RTFParAttrHook(RTF_Info *info)
479 PARAFORMAT2 fmt;
480 fmt.cbSize = sizeof(fmt);
481 fmt.dwMask = 0;
483 switch(info->rtfMinor)
485 case rtfParDef: /* restores default paragraph attributes */
486 if (!info->editor->bEmulateVersion10) /* v4.1 */
487 info->borderType = RTFBorderParaLeft;
488 else /* v1.0 - 3.0 */
489 info->borderType = RTFBorderParaTop;
490 fmt.dwMask = PFM_ALIGNMENT | PFM_BORDER | PFM_LINESPACING | PFM_TABSTOPS |
491 PFM_OFFSET | PFM_RIGHTINDENT | PFM_SPACEAFTER | PFM_SPACEBEFORE |
492 PFM_STARTINDENT;
493 /* TODO: numbering, shading */
494 fmt.wAlignment = PFA_LEFT;
495 fmt.cTabCount = 0;
496 fmt.dxOffset = fmt.dxStartIndent = fmt.dxRightIndent = 0;
497 fmt.wBorderWidth = fmt.wBorders = 0;
498 fmt.wBorderSpace = 0;
499 fmt.bLineSpacingRule = 0;
500 fmt.dySpaceBefore = fmt.dySpaceAfter = 0;
501 fmt.dyLineSpacing = 0;
502 if (!info->editor->bEmulateVersion10) /* v4.1 */
504 if (info->tableDef && info->tableDef->tableRowStart &&
505 info->tableDef->tableRowStart->member.para.nFlags & MEPF_ROWEND)
507 ME_Cursor cursor;
508 ME_DisplayItem *para;
509 /* We are just after a table row. */
510 RTFFlushOutputBuffer(info);
511 cursor = info->editor->pCursors[0];
512 para = cursor.pPara;
513 if (para == info->tableDef->tableRowStart->member.para.next_para
514 && !cursor.nOffset && !cursor.pRun->member.run.nCharOfs)
516 /* Since the table row end, no text has been inserted, and the \intbl
517 * control word has not be used. We can confirm that we are not in a
518 * table anymore.
520 info->tableDef->tableRowStart = NULL;
521 info->canInheritInTbl = FALSE;
524 } else { /* v1.0 - v3.0 */
525 fmt.dwMask |= PFM_TABLE;
526 fmt.wEffects &= ~PFE_TABLE;
528 break;
529 case rtfNestLevel:
530 if (!info->editor->bEmulateVersion10) /* v4.1 */
532 while (info->rtfParam > info->nestingLevel) {
533 RTFTable *tableDef = ALLOC_OBJ(RTFTable);
534 ZeroMemory(tableDef, sizeof(RTFTable));
535 tableDef->parent = info->tableDef;
536 info->tableDef = tableDef;
538 RTFFlushOutputBuffer(info);
539 if (tableDef->tableRowStart &&
540 tableDef->tableRowStart->member.para.nFlags & MEPF_ROWEND)
542 ME_DisplayItem *para = tableDef->tableRowStart;
543 para = para->member.para.next_para;
544 para = ME_InsertTableRowStartAtParagraph(info->editor, para);
545 tableDef->tableRowStart = para;
546 } else {
547 ME_Cursor cursor;
548 WCHAR endl = '\r';
549 cursor = info->editor->pCursors[0];
550 if (cursor.nOffset || cursor.pRun->member.run.nCharOfs)
551 ME_InsertTextFromCursor(info->editor, 0, &endl, 1, info->style);
552 tableDef->tableRowStart = ME_InsertTableRowStartFromCursor(info->editor);
555 info->nestingLevel++;
557 info->canInheritInTbl = FALSE;
559 break;
560 case rtfInTable:
562 if (!info->editor->bEmulateVersion10) /* v4.1 */
564 if (info->nestingLevel < 1)
566 RTFTable *tableDef;
567 if (!info->tableDef)
569 info->tableDef = ALLOC_OBJ(RTFTable);
570 ZeroMemory(info->tableDef, sizeof(RTFTable));
572 tableDef = info->tableDef;
573 RTFFlushOutputBuffer(info);
574 if (tableDef->tableRowStart &&
575 tableDef->tableRowStart->member.para.nFlags & MEPF_ROWEND)
577 ME_DisplayItem *para = tableDef->tableRowStart;
578 para = para->member.para.next_para;
579 para = ME_InsertTableRowStartAtParagraph(info->editor, para);
580 tableDef->tableRowStart = para;
581 } else {
582 ME_Cursor cursor;
583 WCHAR endl = '\r';
584 cursor = info->editor->pCursors[0];
585 if (cursor.nOffset || cursor.pRun->member.run.nCharOfs)
586 ME_InsertTextFromCursor(info->editor, 0, &endl, 1, info->style);
587 tableDef->tableRowStart = ME_InsertTableRowStartFromCursor(info->editor);
589 info->nestingLevel = 1;
590 info->canInheritInTbl = TRUE;
592 return;
593 } else { /* v1.0 - v3.0 */
594 fmt.dwMask |= PFM_TABLE;
595 fmt.wEffects |= PFE_TABLE;
597 break;
599 case rtfFirstIndent:
600 ME_GetSelectionParaFormat(info->editor, &fmt);
601 fmt.dwMask = PFM_STARTINDENT | PFM_OFFSET;
602 fmt.dxStartIndent += fmt.dxOffset + info->rtfParam;
603 fmt.dxOffset = -info->rtfParam;
604 break;
605 case rtfLeftIndent:
606 ME_GetSelectionParaFormat(info->editor, &fmt);
607 fmt.dwMask = PFM_STARTINDENT;
608 fmt.dxStartIndent = info->rtfParam - fmt.dxOffset;
609 break;
610 case rtfRightIndent:
611 fmt.dwMask = PFM_RIGHTINDENT;
612 fmt.dxRightIndent = info->rtfParam;
613 break;
614 case rtfQuadLeft:
615 case rtfQuadJust:
616 fmt.dwMask = PFM_ALIGNMENT;
617 fmt.wAlignment = PFA_LEFT;
618 break;
619 case rtfQuadRight:
620 fmt.dwMask = PFM_ALIGNMENT;
621 fmt.wAlignment = PFA_RIGHT;
622 break;
623 case rtfQuadCenter:
624 fmt.dwMask = PFM_ALIGNMENT;
625 fmt.wAlignment = PFA_CENTER;
626 break;
627 case rtfTabPos:
628 ME_GetSelectionParaFormat(info->editor, &fmt);
629 if (!(fmt.dwMask & PFM_TABSTOPS))
631 fmt.cTabCount = 0;
633 if (fmt.cTabCount < MAX_TAB_STOPS && info->rtfParam < 0x1000000)
634 fmt.rgxTabs[fmt.cTabCount++] = info->rtfParam;
635 fmt.dwMask = PFM_TABSTOPS;
636 break;
637 case rtfKeep:
638 fmt.dwMask = PFM_KEEP;
639 fmt.wEffects = PFE_KEEP;
640 break;
641 case rtfNoWidowControl:
642 fmt.dwMask = PFM_NOWIDOWCONTROL;
643 fmt.wEffects = PFE_NOWIDOWCONTROL;
644 break;
645 case rtfKeepNext:
646 fmt.dwMask = PFM_KEEPNEXT;
647 fmt.wEffects = PFE_KEEPNEXT;
648 break;
649 case rtfSpaceAfter:
650 fmt.dwMask = PFM_SPACEAFTER;
651 fmt.dySpaceAfter = info->rtfParam;
652 break;
653 case rtfSpaceBefore:
654 fmt.dwMask = PFM_SPACEBEFORE;
655 fmt.dySpaceBefore = info->rtfParam;
656 break;
657 case rtfSpaceBetween:
658 fmt.dwMask = PFM_LINESPACING;
659 if ((int)info->rtfParam > 0)
661 fmt.dyLineSpacing = info->rtfParam;
662 fmt.bLineSpacingRule = 3;
664 else
666 fmt.dyLineSpacing = info->rtfParam;
667 fmt.bLineSpacingRule = 4;
669 case rtfSpaceMultiply:
670 fmt.dwMask = PFM_LINESPACING;
671 fmt.dyLineSpacing = info->rtfParam * 20;
672 fmt.bLineSpacingRule = 5;
673 break;
674 case rtfParBullet:
675 fmt.dwMask = PFM_NUMBERING;
676 fmt.wNumbering = PFN_BULLET;
677 break;
678 case rtfParSimple:
679 fmt.dwMask = PFM_NUMBERING;
680 fmt.wNumbering = 2; /* FIXME: MSDN says it's not used ?? */
681 break;
682 case rtfParNumDecimal:
683 fmt.dwMask = PFM_NUMBERING;
684 fmt.wNumbering = 2; /* FIXME: MSDN says it's not used ?? */
685 break;
686 case rtfParNumIndent:
687 fmt.dwMask = PFM_NUMBERINGTAB;
688 fmt.wNumberingTab = info->rtfParam;
689 break;
690 case rtfParNumStartAt:
691 fmt.dwMask = PFM_NUMBERINGSTART;
692 fmt.wNumberingStart = info->rtfParam;
693 break;
694 case rtfBorderLeft:
695 info->borderType = RTFBorderParaLeft;
696 ME_GetSelectionParaFormat(info->editor, &fmt);
697 if (!(fmt.dwMask & PFM_BORDER))
699 fmt.wBorderSpace = 0;
700 fmt.wBorderWidth = 1;
701 fmt.wBorders = 0;
703 fmt.wBorders |= 1;
704 fmt.dwMask = PFM_BORDER;
705 break;
706 case rtfBorderRight:
707 info->borderType = RTFBorderParaRight;
708 ME_GetSelectionParaFormat(info->editor, &fmt);
709 if (!(fmt.dwMask & PFM_BORDER))
711 fmt.wBorderSpace = 0;
712 fmt.wBorderWidth = 1;
713 fmt.wBorders = 0;
715 fmt.wBorders |= 2;
716 fmt.dwMask = PFM_BORDER;
717 break;
718 case rtfBorderTop:
719 info->borderType = RTFBorderParaTop;
720 ME_GetSelectionParaFormat(info->editor, &fmt);
721 if (!(fmt.dwMask & PFM_BORDER))
723 fmt.wBorderSpace = 0;
724 fmt.wBorderWidth = 1;
725 fmt.wBorders = 0;
727 fmt.wBorders |= 4;
728 fmt.dwMask = PFM_BORDER;
729 break;
730 case rtfBorderBottom:
731 info->borderType = RTFBorderParaBottom;
732 ME_GetSelectionParaFormat(info->editor, &fmt);
733 if (!(fmt.dwMask & PFM_BORDER))
735 fmt.wBorderSpace = 0;
736 fmt.wBorderWidth = 1;
737 fmt.wBorders = 0;
739 fmt.wBorders |= 8;
740 fmt.dwMask = PFM_BORDER;
741 break;
742 case rtfBorderSingle:
743 ME_GetSelectionParaFormat(info->editor, &fmt);
744 /* we assume that borders have been created before (RTF spec) */
745 fmt.wBorders &= ~0x700;
746 fmt.wBorders |= 1 << 8;
747 fmt.dwMask = PFM_BORDER;
748 break;
749 case rtfBorderThick:
750 ME_GetSelectionParaFormat(info->editor, &fmt);
751 /* we assume that borders have been created before (RTF spec) */
752 fmt.wBorders &= ~0x700;
753 fmt.wBorders |= 2 << 8;
754 fmt.dwMask = PFM_BORDER;
755 break;
756 case rtfBorderShadow:
757 ME_GetSelectionParaFormat(info->editor, &fmt);
758 /* we assume that borders have been created before (RTF spec) */
759 fmt.wBorders &= ~0x700;
760 fmt.wBorders |= 10 << 8;
761 fmt.dwMask = PFM_BORDER;
762 break;
763 case rtfBorderDouble:
764 ME_GetSelectionParaFormat(info->editor, &fmt);
765 /* we assume that borders have been created before (RTF spec) */
766 fmt.wBorders &= ~0x700;
767 fmt.wBorders |= 7 << 8;
768 fmt.dwMask = PFM_BORDER;
769 break;
770 case rtfBorderDot:
771 ME_GetSelectionParaFormat(info->editor, &fmt);
772 /* we assume that borders have been created before (RTF spec) */
773 fmt.wBorders &= ~0x700;
774 fmt.wBorders |= 11 << 8;
775 fmt.dwMask = PFM_BORDER;
776 break;
777 case rtfBorderWidth:
779 int borderSide = info->borderType & RTFBorderSideMask;
780 RTFTable *tableDef = info->tableDef;
781 ME_GetSelectionParaFormat(info->editor, &fmt);
782 /* we assume that borders have been created before (RTF spec) */
783 fmt.wBorderWidth |= ((info->rtfParam / 15) & 7) << 8;
784 if ((info->borderType & RTFBorderTypeMask) == RTFBorderTypeCell)
786 RTFBorder *border;
787 if (!tableDef || tableDef->numCellsDefined >= MAX_TABLE_CELLS)
788 break;
789 border = &tableDef->cells[tableDef->numCellsDefined].border[borderSide];
790 border->width = info->rtfParam;
791 break;
793 fmt.dwMask = PFM_BORDER;
794 break;
796 case rtfBorderSpace:
797 ME_GetSelectionParaFormat(info->editor, &fmt);
798 /* we assume that borders have been created before (RTF spec) */
799 fmt.wBorderSpace = info->rtfParam;
800 fmt.dwMask = PFM_BORDER;
801 break;
802 case rtfBorderColor:
804 RTFTable *tableDef = info->tableDef;
805 int borderSide = info->borderType & RTFBorderSideMask;
806 int borderType = info->borderType & RTFBorderTypeMask;
807 switch(borderType)
809 case RTFBorderTypePara:
810 if (!info->editor->bEmulateVersion10) /* v4.1 */
811 break;
812 /* v1.0 - 3.0 treat paragraph and row borders the same. */
813 case RTFBorderTypeRow:
814 if (tableDef) {
815 tableDef->border[borderSide].color = info->rtfParam;
817 break;
818 case RTFBorderTypeCell:
819 if (tableDef && tableDef->numCellsDefined < MAX_TABLE_CELLS) {
820 tableDef->cells[tableDef->numCellsDefined].border[borderSide].color = info->rtfParam;
822 break;
824 break;
827 if (fmt.dwMask) {
828 RTFFlushOutputBuffer(info);
829 /* FIXME too slow ? how come ?*/
830 ME_SetSelectionParaFormat(info->editor, &fmt);
834 void ME_RTFTblAttrHook(RTF_Info *info)
836 switch (info->rtfMinor)
838 case rtfRowDef:
840 if (!info->editor->bEmulateVersion10) /* v4.1 */
841 info->borderType = 0; /* Not sure */
842 else /* v1.0 - 3.0 */
843 info->borderType = RTFBorderRowTop;
844 if (!info->tableDef) {
845 info->tableDef = ME_MakeTableDef(info->editor);
846 } else {
847 ME_InitTableDef(info->editor, info->tableDef);
849 break;
851 case rtfCellPos:
853 int cellNum;
854 if (!info->tableDef)
856 info->tableDef = ME_MakeTableDef(info->editor);
858 cellNum = info->tableDef->numCellsDefined;
859 if (cellNum >= MAX_TABLE_CELLS)
860 break;
861 info->tableDef->cells[cellNum].rightBoundary = info->rtfParam;
862 if (cellNum < MAX_TAB_STOPS) {
863 /* Tab stops were used to store cell positions before v4.1 but v4.1
864 * still seems to set the tabstops without using them. */
865 ME_DisplayItem *para = info->editor->pCursors[0].pPara;
866 PARAFORMAT2 *pFmt = para->member.para.pFmt;
867 pFmt->rgxTabs[cellNum] &= ~0x00FFFFFF;
868 pFmt->rgxTabs[cellNum] = 0x00FFFFFF & info->rtfParam;
870 info->tableDef->numCellsDefined++;
871 break;
873 case rtfRowBordTop:
874 info->borderType = RTFBorderRowTop;
875 break;
876 case rtfRowBordLeft:
877 info->borderType = RTFBorderRowLeft;
878 break;
879 case rtfRowBordBottom:
880 info->borderType = RTFBorderRowBottom;
881 break;
882 case rtfRowBordRight:
883 info->borderType = RTFBorderRowRight;
884 break;
885 case rtfCellBordTop:
886 info->borderType = RTFBorderCellTop;
887 break;
888 case rtfCellBordLeft:
889 info->borderType = RTFBorderCellLeft;
890 break;
891 case rtfCellBordBottom:
892 info->borderType = RTFBorderCellBottom;
893 break;
894 case rtfCellBordRight:
895 info->borderType = RTFBorderCellRight;
896 break;
897 case rtfRowGapH:
898 if (info->tableDef)
899 info->tableDef->gapH = info->rtfParam;
900 break;
901 case rtfRowLeftEdge:
902 if (info->tableDef)
903 info->tableDef->leftEdge = info->rtfParam;
904 break;
908 void ME_RTFSpecialCharHook(RTF_Info *info)
910 RTFTable *tableDef = info->tableDef;
911 switch (info->rtfMinor)
913 case rtfNestCell:
914 if (info->editor->bEmulateVersion10) /* v1.0 - v3.0 */
915 break;
916 /* else fall through since v4.1 treats rtfNestCell and rtfCell the same */
917 case rtfCell:
918 if (!tableDef)
919 break;
920 RTFFlushOutputBuffer(info);
921 if (!info->editor->bEmulateVersion10) { /* v4.1 */
922 if (tableDef->tableRowStart)
924 if (!info->nestingLevel &&
925 tableDef->tableRowStart->member.para.nFlags & MEPF_ROWEND)
927 ME_DisplayItem *para = tableDef->tableRowStart;
928 para = para->member.para.next_para;
929 para = ME_InsertTableRowStartAtParagraph(info->editor, para);
930 tableDef->tableRowStart = para;
931 info->nestingLevel = 1;
933 ME_InsertTableCellFromCursor(info->editor);
935 } else { /* v1.0 - v3.0 */
936 ME_DisplayItem *para = info->editor->pCursors[0].pPara;
937 PARAFORMAT2 *pFmt = para->member.para.pFmt;
938 if (pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE &&
939 tableDef->numCellsInserted < tableDef->numCellsDefined)
941 WCHAR tab = '\t';
942 ME_InsertTextFromCursor(info->editor, 0, &tab, 1, info->style);
943 tableDef->numCellsInserted++;
946 break;
947 case rtfNestRow:
948 if (info->editor->bEmulateVersion10) /* v1.0 - v3.0 */
949 break;
950 /* else fall through since v4.1 treats rtfNestRow and rtfRow the same */
951 case rtfRow:
953 ME_DisplayItem *para, *cell, *run;
954 int i;
956 if (!tableDef)
957 break;
958 RTFFlushOutputBuffer(info);
959 if (!info->editor->bEmulateVersion10) { /* v4.1 */
960 if (!tableDef->tableRowStart)
961 break;
962 if (!info->nestingLevel &&
963 tableDef->tableRowStart->member.para.nFlags & MEPF_ROWEND)
965 para = tableDef->tableRowStart;
966 para = para->member.para.next_para;
967 para = ME_InsertTableRowStartAtParagraph(info->editor, para);
968 tableDef->tableRowStart = para;
969 info->nestingLevel++;
971 para = tableDef->tableRowStart;
972 cell = ME_FindItemFwd(para, diCell);
973 assert(cell && !cell->member.cell.prev_cell);
974 if (tableDef->numCellsDefined < 1)
976 /* 2000 twips appears to be the cell size that native richedit uses
977 * when no cell sizes are specified. */
978 const int defaultCellSize = 2000;
979 int nRightBoundary = defaultCellSize;
980 cell->member.cell.nRightBoundary = nRightBoundary;
981 while (cell->member.cell.next_cell) {
982 cell = cell->member.cell.next_cell;
983 nRightBoundary += defaultCellSize;
984 cell->member.cell.nRightBoundary = nRightBoundary;
986 para = ME_InsertTableCellFromCursor(info->editor);
987 cell = para->member.para.pCell;
988 cell->member.cell.nRightBoundary = nRightBoundary;
989 } else {
990 for (i = 0; i < tableDef->numCellsDefined; i++)
992 RTFCell *cellDef = &tableDef->cells[i];
993 cell->member.cell.nRightBoundary = cellDef->rightBoundary;
994 ME_ApplyBorderProperties(info, &cell->member.cell.border,
995 cellDef->border);
996 cell = cell->member.cell.next_cell;
997 if (!cell)
999 para = ME_InsertTableCellFromCursor(info->editor);
1000 cell = para->member.para.pCell;
1003 /* Cell for table row delimiter is empty */
1004 cell->member.cell.nRightBoundary = tableDef->cells[i-1].rightBoundary;
1007 run = ME_FindItemFwd(cell, diRun);
1008 if (info->editor->pCursors[0].pRun != run ||
1009 info->editor->pCursors[0].nOffset)
1011 int nOfs, nChars;
1012 /* Delete inserted cells that aren't defined. */
1013 info->editor->pCursors[1].pRun = run;
1014 info->editor->pCursors[1].pPara = ME_GetParagraph(run);
1015 info->editor->pCursors[1].nOffset = 0;
1016 nOfs = ME_GetCursorOfs(&info->editor->pCursors[1]);
1017 nChars = ME_GetCursorOfs(&info->editor->pCursors[0]) - nOfs;
1018 ME_InternalDeleteText(info->editor, &info->editor->pCursors[1],
1019 nChars, TRUE);
1022 para = ME_InsertTableRowEndFromCursor(info->editor);
1023 para->member.para.pFmt->dxOffset = abs(info->tableDef->gapH);
1024 para->member.para.pFmt->dxStartIndent = info->tableDef->leftEdge;
1025 ME_ApplyBorderProperties(info, &para->member.para.border,
1026 tableDef->border);
1027 info->nestingLevel--;
1028 if (!info->nestingLevel)
1030 if (info->canInheritInTbl) {
1031 tableDef->tableRowStart = para;
1032 } else {
1033 while (info->tableDef) {
1034 tableDef = info->tableDef;
1035 info->tableDef = tableDef->parent;
1036 heap_free(tableDef);
1039 } else {
1040 info->tableDef = tableDef->parent;
1041 heap_free(tableDef);
1043 } else { /* v1.0 - v3.0 */
1044 WCHAR endl = '\r';
1045 ME_DisplayItem *para = info->editor->pCursors[0].pPara;
1046 PARAFORMAT2 *pFmt = para->member.para.pFmt;
1047 pFmt->dxOffset = info->tableDef->gapH;
1048 pFmt->dxStartIndent = info->tableDef->leftEdge;
1050 ME_ApplyBorderProperties(info, &para->member.para.border,
1051 tableDef->border);
1052 while (tableDef->numCellsInserted < tableDef->numCellsDefined)
1054 WCHAR tab = '\t';
1055 ME_InsertTextFromCursor(info->editor, 0, &tab, 1, info->style);
1056 tableDef->numCellsInserted++;
1058 pFmt->cTabCount = min(tableDef->numCellsDefined, MAX_TAB_STOPS);
1059 if (!tableDef->numCellsDefined)
1060 pFmt->wEffects &= ~PFE_TABLE;
1061 ME_InsertTextFromCursor(info->editor, 0, &endl, 1, info->style);
1062 tableDef->numCellsInserted = 0;
1064 break;
1066 case rtfTab:
1067 case rtfPar:
1068 if (info->editor->bEmulateVersion10) { /* v1.0 - 3.0 */
1069 ME_DisplayItem *para;
1070 PARAFORMAT2 *pFmt;
1071 RTFFlushOutputBuffer(info);
1072 para = info->editor->pCursors[0].pPara;
1073 pFmt = para->member.para.pFmt;
1074 if (pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE)
1076 /* rtfPar is treated like a space within a table. */
1077 info->rtfClass = rtfText;
1078 info->rtfMajor = ' ';
1080 else if (info->rtfMinor == rtfPar && tableDef)
1081 tableDef->numCellsInserted = 0;
1083 break;
1087 static BOOL ME_RTFInsertOleObject(RTF_Info *info, HENHMETAFILE hemf, HBITMAP hbmp,
1088 const SIZEL* sz)
1090 LPOLEOBJECT lpObject = NULL;
1091 LPSTORAGE lpStorage = NULL;
1092 LPOLECLIENTSITE lpClientSite = NULL;
1093 LPDATAOBJECT lpDataObject = NULL;
1094 LPOLECACHE lpOleCache = NULL;
1095 STGMEDIUM stgm;
1096 FORMATETC fm;
1097 CLSID clsid;
1098 BOOL ret = FALSE;
1099 DWORD conn;
1101 if (hemf)
1103 stgm.tymed = TYMED_ENHMF;
1104 stgm.u.hEnhMetaFile = hemf;
1105 fm.cfFormat = CF_ENHMETAFILE;
1107 else if (hbmp)
1109 stgm.tymed = TYMED_GDI;
1110 stgm.u.hBitmap = hbmp;
1111 fm.cfFormat = CF_BITMAP;
1113 stgm.pUnkForRelease = NULL;
1115 fm.ptd = NULL;
1116 fm.dwAspect = DVASPECT_CONTENT;
1117 fm.lindex = -1;
1118 fm.tymed = stgm.tymed;
1120 if (!info->lpRichEditOle)
1122 CreateIRichEditOle(info->editor, (VOID**)&info->lpRichEditOle);
1125 if (OleCreateDefaultHandler(&CLSID_NULL, NULL, &IID_IOleObject, (void**)&lpObject) == S_OK &&
1126 #if 0
1127 /* FIXME: enable it when rich-edit properly implements this method */
1128 IRichEditOle_GetClientSite(info->lpRichEditOle, &lpClientSite) == S_OK &&
1129 IOleObject_SetClientSite(lpObject, lpClientSite) == S_OK &&
1130 #endif
1131 IOleObject_GetUserClassID(lpObject, &clsid) == S_OK &&
1132 IOleObject_QueryInterface(lpObject, &IID_IOleCache, (void**)&lpOleCache) == S_OK &&
1133 IOleCache_Cache(lpOleCache, &fm, 0, &conn) == S_OK &&
1134 IOleObject_QueryInterface(lpObject, &IID_IDataObject, (void**)&lpDataObject) == S_OK &&
1135 IDataObject_SetData(lpDataObject, &fm, &stgm, TRUE) == S_OK)
1137 REOBJECT reobject;
1139 reobject.cbStruct = sizeof(reobject);
1140 reobject.cp = REO_CP_SELECTION;
1141 reobject.clsid = clsid;
1142 reobject.poleobj = lpObject;
1143 reobject.pstg = lpStorage;
1144 reobject.polesite = lpClientSite;
1145 /* convert from twips to .01 mm */
1146 reobject.sizel.cx = MulDiv(sz->cx, 254, 144);
1147 reobject.sizel.cy = MulDiv(sz->cy, 254, 144);
1148 reobject.dvaspect = DVASPECT_CONTENT;
1149 reobject.dwFlags = 0; /* FIXME */
1150 reobject.dwUser = 0;
1152 ME_InsertOLEFromCursor(info->editor, &reobject, 0);
1153 ret = TRUE;
1156 if (lpObject) IOleObject_Release(lpObject);
1157 if (lpClientSite) IOleClientSite_Release(lpClientSite);
1158 if (lpStorage) IStorage_Release(lpStorage);
1159 if (lpDataObject) IDataObject_Release(lpDataObject);
1160 if (lpOleCache) IOleCache_Release(lpOleCache);
1162 return ret;
1165 static void ME_RTFReadPictGroup(RTF_Info *info)
1167 SIZEL sz;
1168 BYTE* buffer = NULL;
1169 unsigned bufsz, bufidx;
1170 BOOL flip;
1171 BYTE val;
1172 METAFILEPICT mfp;
1173 HENHMETAFILE hemf;
1174 HBITMAP hbmp;
1175 enum gfxkind {gfx_unknown = 0, gfx_enhmetafile, gfx_metafile, gfx_dib} gfx = gfx_unknown;
1177 RTFGetToken (info);
1178 if (info->rtfClass == rtfEOF)
1179 return;
1180 mfp.mm = MM_TEXT;
1181 /* fetch picture type */
1182 if (RTFCheckMM (info, rtfPictAttr, rtfWinMetafile))
1184 mfp.mm = info->rtfParam;
1185 gfx = gfx_metafile;
1187 else if (RTFCheckMM (info, rtfPictAttr, rtfDevIndBitmap))
1189 if (info->rtfParam != 0) FIXME("dibitmap should be 0 (%d)\n", info->rtfParam);
1190 gfx = gfx_dib;
1192 else if (RTFCheckMM (info, rtfPictAttr, rtfEmfBlip))
1194 gfx = gfx_enhmetafile;
1196 else
1198 FIXME("%d %d\n", info->rtfMajor, info->rtfMinor);
1199 goto skip_group;
1201 sz.cx = sz.cy = 0;
1202 /* fetch picture attributes */
1203 for (;;)
1205 RTFGetToken (info);
1206 if (info->rtfClass == rtfEOF)
1207 return;
1208 if (info->rtfClass == rtfText)
1209 break;
1210 if (!RTFCheckCM (info, rtfControl, rtfPictAttr))
1212 ERR("Expected picture attribute (%d %d)\n",
1213 info->rtfClass, info->rtfMajor);
1214 goto skip_group;
1216 else if (RTFCheckMM (info, rtfPictAttr, rtfPicWid))
1218 if (gfx == gfx_metafile) mfp.xExt = info->rtfParam;
1220 else if (RTFCheckMM (info, rtfPictAttr, rtfPicHt))
1222 if (gfx == gfx_metafile) mfp.yExt = info->rtfParam;
1224 else if (RTFCheckMM (info, rtfPictAttr, rtfPicGoalWid))
1225 sz.cx = info->rtfParam;
1226 else if (RTFCheckMM (info, rtfPictAttr, rtfPicGoalHt))
1227 sz.cy = info->rtfParam;
1228 else
1229 FIXME("Non supported attribute: %d %d %d\n", info->rtfClass, info->rtfMajor, info->rtfMinor);
1231 /* fetch picture data */
1232 bufsz = 1024;
1233 bufidx = 0;
1234 buffer = HeapAlloc(GetProcessHeap(), 0, bufsz);
1235 val = info->rtfMajor;
1236 for (flip = TRUE;; flip = !flip)
1238 RTFGetToken (info);
1239 if (info->rtfClass == rtfEOF)
1241 HeapFree(GetProcessHeap(), 0, buffer);
1242 return; /* Warn ?? */
1244 if (RTFCheckCM(info, rtfGroup, rtfEndGroup))
1245 break;
1246 if (info->rtfClass != rtfText) goto skip_group;
1247 if (flip)
1249 if (bufidx >= bufsz &&
1250 !(buffer = HeapReAlloc(GetProcessHeap(), 0, buffer, bufsz += 1024)))
1251 goto skip_group;
1252 buffer[bufidx++] = RTFCharToHex(val) * 16 + RTFCharToHex(info->rtfMajor);
1254 else
1255 val = info->rtfMajor;
1257 if (flip) FIXME("wrong hex string\n");
1259 switch (gfx)
1261 case gfx_enhmetafile:
1262 if ((hemf = SetEnhMetaFileBits(bufidx, buffer)))
1263 ME_RTFInsertOleObject(info, hemf, NULL, &sz);
1264 break;
1265 case gfx_metafile:
1266 if ((hemf = SetWinMetaFileBits(bufidx, buffer, NULL, &mfp)))
1267 ME_RTFInsertOleObject(info, hemf, NULL, &sz);
1268 break;
1269 case gfx_dib:
1271 BITMAPINFO* bi = (BITMAPINFO*)buffer;
1272 HDC hdc = GetDC(0);
1273 unsigned nc = bi->bmiHeader.biClrUsed;
1275 /* not quite right, especially for bitfields type of compression */
1276 if (!nc && bi->bmiHeader.biBitCount <= 8)
1277 nc = 1 << bi->bmiHeader.biBitCount;
1278 if ((hbmp = CreateDIBitmap(hdc, &bi->bmiHeader,
1279 CBM_INIT, (char*)(bi + 1) + nc * sizeof(RGBQUAD),
1280 bi, DIB_RGB_COLORS)))
1281 ME_RTFInsertOleObject(info, NULL, hbmp, &sz);
1282 ReleaseDC(0, hdc);
1284 break;
1285 default:
1286 break;
1288 HeapFree(GetProcessHeap(), 0, buffer);
1289 RTFRouteToken (info); /* feed "}" back to router */
1290 return;
1291 skip_group:
1292 HeapFree(GetProcessHeap(), 0, buffer);
1293 RTFSkipGroup(info);
1294 RTFRouteToken(info); /* feed "}" back to router */
1297 /* for now, lookup the \result part and use it, whatever the object */
1298 static void ME_RTFReadObjectGroup(RTF_Info *info)
1300 for (;;)
1302 RTFGetToken (info);
1303 if (info->rtfClass == rtfEOF)
1304 return;
1305 if (RTFCheckCM(info, rtfGroup, rtfEndGroup))
1306 break;
1307 if (RTFCheckCM(info, rtfGroup, rtfBeginGroup))
1309 RTFGetToken (info);
1310 if (info->rtfClass == rtfEOF)
1311 return;
1312 if (RTFCheckCMM(info, rtfControl, rtfDestination, rtfObjResult))
1314 int level = 1;
1316 while (RTFGetToken (info) != rtfEOF)
1318 if (info->rtfClass == rtfGroup)
1320 if (info->rtfMajor == rtfBeginGroup) level++;
1321 else if (info->rtfMajor == rtfEndGroup && --level < 0) break;
1323 RTFRouteToken(info);
1326 else RTFSkipGroup(info);
1327 continue;
1329 if (!RTFCheckCM (info, rtfControl, rtfObjAttr))
1331 FIXME("Non supported attribute: %d %d %d\n", info->rtfClass, info->rtfMajor, info->rtfMinor);
1332 return;
1335 RTFRouteToken(info); /* feed "}" back to router */
1338 static void ME_RTFReadHook(RTF_Info *info)
1340 switch(info->rtfClass)
1342 case rtfGroup:
1343 switch(info->rtfMajor)
1345 case rtfBeginGroup:
1346 if (info->stackTop < maxStack) {
1347 info->stack[info->stackTop].style = info->style;
1348 ME_AddRefStyle(info->style);
1349 info->stack[info->stackTop].codePage = info->codePage;
1350 info->stack[info->stackTop].unicodeLength = info->unicodeLength;
1352 info->stackTop++;
1353 info->styleChanged = FALSE;
1354 break;
1355 case rtfEndGroup:
1357 RTFFlushOutputBuffer(info);
1358 info->stackTop--;
1359 if (info->stackTop <= 0)
1360 info->rtfClass = rtfEOF;
1361 if (info->stackTop < 0)
1362 return;
1364 ME_ReleaseStyle(info->style);
1365 info->style = info->stack[info->stackTop].style;
1366 info->codePage = info->stack[info->stackTop].codePage;
1367 info->unicodeLength = info->stack[info->stackTop].unicodeLength;
1368 break;
1371 break;
1375 void
1376 ME_StreamInFill(ME_InStream *stream)
1378 stream->editstream->dwError = stream->editstream->pfnCallback(stream->editstream->dwCookie,
1379 (BYTE *)stream->buffer,
1380 sizeof(stream->buffer),
1381 (LONG *)&stream->dwSize);
1382 stream->dwUsed = 0;
1385 static LRESULT ME_StreamIn(ME_TextEditor *editor, DWORD format, EDITSTREAM *stream, BOOL stripLastCR)
1387 RTF_Info parser;
1388 ME_Style *style;
1389 int from, to, nUndoMode;
1390 int nEventMask = editor->nEventMask;
1391 ME_InStream inStream;
1392 BOOL invalidRTF = FALSE;
1393 ME_Cursor *selStart, *selEnd;
1395 TRACE("stream==%p editor==%p format==0x%X\n", stream, editor, format);
1396 editor->nEventMask = 0;
1398 ME_GetSelectionOfs(editor, &from, &to);
1399 if (format & SFF_SELECTION && editor->mode & TM_RICHTEXT)
1401 ME_GetSelection(editor, &selStart, &selEnd);
1402 style = ME_GetSelectionInsertStyle(editor);
1404 ME_InternalDeleteText(editor, selStart, to - from, FALSE);
1406 /* Don't insert text at the end of the table row */
1407 if (!editor->bEmulateVersion10) { /* v4.1 */
1408 ME_DisplayItem *para = editor->pCursors->pPara;
1409 if (para->member.para.nFlags & MEPF_ROWEND)
1411 para = para->member.para.next_para;
1412 editor->pCursors[0].pPara = para;
1413 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
1414 editor->pCursors[0].nOffset = 0;
1416 if (para->member.para.nFlags & MEPF_ROWSTART)
1418 para = para->member.para.next_para;
1419 editor->pCursors[0].pPara = para;
1420 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
1421 editor->pCursors[0].nOffset = 0;
1423 editor->pCursors[1] = editor->pCursors[0];
1424 } else { /* v1.0 - 3.0 */
1425 if (editor->pCursors[0].pRun->member.run.nFlags & MERF_ENDPARA &&
1426 ME_IsInTable(editor->pCursors[0].pRun))
1427 return 0;
1429 } else {
1430 style = editor->pBuffer->pDefaultStyle;
1431 ME_AddRefStyle(style);
1432 ME_SetSelection(editor, 0, 0);
1433 ME_InternalDeleteText(editor, &editor->pCursors[1],
1434 ME_GetTextLength(editor), FALSE);
1435 from = to = 0;
1436 ME_ClearTempStyle(editor);
1437 ME_SetDefaultParaFormat(editor->pCursors[0].pPara->member.para.pFmt);
1441 /* Back up undo mode to a local variable */
1442 nUndoMode = editor->nUndoMode;
1444 /* Only create an undo if SFF_SELECTION is set */
1445 if (!(format & SFF_SELECTION))
1446 editor->nUndoMode = umIgnore;
1448 inStream.editstream = stream;
1449 inStream.editstream->dwError = 0;
1450 inStream.dwSize = 0;
1451 inStream.dwUsed = 0;
1453 if (format & SF_RTF)
1455 /* Check if it's really RTF, and if it is not, use plain text */
1456 ME_StreamInFill(&inStream);
1457 if (!inStream.editstream->dwError)
1459 if ((!editor->bEmulateVersion10 && strncmp(inStream.buffer, "{\\rtf", 5) && strncmp(inStream.buffer, "{\\urtf", 6))
1460 || (editor->bEmulateVersion10 && *inStream.buffer != '{'))
1462 invalidRTF = TRUE;
1463 inStream.editstream->dwError = -16;
1468 if (!invalidRTF && !inStream.editstream->dwError)
1470 if (format & SF_RTF) {
1471 /* setup the RTF parser */
1472 memset(&parser, 0, sizeof parser);
1473 RTFSetEditStream(&parser, &inStream);
1474 parser.rtfFormat = format&(SF_TEXT|SF_RTF);
1475 parser.editor = editor;
1476 parser.style = style;
1477 WriterInit(&parser);
1478 RTFInit(&parser);
1479 RTFSetReadHook(&parser, ME_RTFReadHook);
1480 RTFSetDestinationCallback(&parser, rtfPict, ME_RTFReadPictGroup);
1481 RTFSetDestinationCallback(&parser, rtfObject, ME_RTFReadObjectGroup);
1482 if (!parser.editor->bEmulateVersion10) /* v4.1 */
1484 RTFSetDestinationCallback(&parser, rtfNoNestTables, RTFSkipGroup);
1485 RTFSetDestinationCallback(&parser, rtfNestTableProps, RTFReadGroup);
1487 BeginFile(&parser);
1489 /* do the parsing */
1490 RTFRead(&parser);
1491 RTFFlushOutputBuffer(&parser);
1492 if (!editor->bEmulateVersion10) { /* v4.1 */
1493 if (parser.tableDef && parser.tableDef->tableRowStart &&
1494 (parser.nestingLevel > 0 || parser.canInheritInTbl))
1496 /* Delete any incomplete table row at the end of the rich text. */
1497 int nOfs, nChars;
1498 ME_DisplayItem *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;
1518 editor->pCursors[1].pPara = para;
1519 editor->pCursors[1].pRun = ME_FindItemFwd(para, diRun);
1520 editor->pCursors[1].nOffset = 0;
1521 nOfs = ME_GetCursorOfs(&editor->pCursors[1]);
1522 nChars = ME_GetCursorOfs(&editor->pCursors[0]) - nOfs;
1523 ME_InternalDeleteText(editor, &editor->pCursors[1], nChars, TRUE);
1524 if (parser.tableDef)
1525 parser.tableDef->tableRowStart = NULL;
1528 ME_CheckTablesForCorruption(editor);
1529 RTFDestroy(&parser);
1530 if (parser.lpRichEditOle)
1531 IRichEditOle_Release(parser.lpRichEditOle);
1533 if (parser.stackTop > 0)
1535 while (--parser.stackTop >= 0)
1537 ME_ReleaseStyle(parser.style);
1538 parser.style = parser.stack[parser.stackTop].style;
1540 if (!inStream.editstream->dwError)
1541 inStream.editstream->dwError = HRESULT_FROM_WIN32(ERROR_HANDLE_EOF);
1544 /* Remove last line break, as mandated by tests. This is not affected by
1545 CR/LF counters, since RTF streaming presents only \para tokens, which
1546 are converted according to the standard rules: \r for 2.0, \r\n for 1.0
1548 if (stripLastCR) {
1549 int newto;
1550 ME_GetSelection(editor, &selStart, &selEnd);
1551 newto = ME_GetCursorOfs(selEnd);
1552 if (newto > to + (editor->bEmulateVersion10 ? 1 : 0)) {
1553 WCHAR lastchar[3] = {'\0', '\0'};
1554 int linebreakSize = editor->bEmulateVersion10 ? 2 : 1;
1555 ME_Cursor linebreakCursor = *selEnd;
1557 ME_MoveCursorChars(editor, &linebreakCursor, -linebreakSize);
1558 ME_GetTextW(editor, lastchar, 2, &linebreakCursor, linebreakSize, 0);
1559 if (lastchar[0] == '\r' && (lastchar[1] == '\n' || lastchar[1] == '\0')) {
1560 ME_InternalDeleteText(editor, &linebreakCursor, linebreakSize, FALSE);
1565 style = parser.style;
1567 else if (format & SF_TEXT)
1568 ME_StreamInText(editor, format, &inStream, style);
1569 else
1570 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
1571 /* put the cursor at the top */
1572 if (!(format & SFF_SELECTION))
1573 ME_SetSelection(editor, 0, 0);
1576 /* Restore saved undo mode */
1577 editor->nUndoMode = nUndoMode;
1579 /* even if we didn't add an undo, we need to commit anything on the stack */
1580 ME_CommitUndo(editor);
1582 /* If SFF_SELECTION isn't set, delete any undos from before we started too */
1583 if (!(format & SFF_SELECTION))
1584 ME_EmptyUndoStack(editor);
1586 ME_ReleaseStyle(style);
1587 editor->nEventMask = nEventMask;
1588 ME_UpdateRepaint(editor);
1589 if (!(format & SFF_SELECTION)) {
1590 ME_ClearTempStyle(editor);
1592 ITextHost_TxShowCaret(editor->texthost, FALSE);
1593 ME_MoveCaret(editor);
1594 ITextHost_TxShowCaret(editor->texthost, TRUE);
1595 ME_SendSelChange(editor);
1596 ME_SendRequestResize(editor, FALSE);
1598 return 0;
1602 typedef struct tagME_RTFStringStreamStruct
1604 char *string;
1605 int pos;
1606 int length;
1607 } ME_RTFStringStreamStruct;
1609 static DWORD CALLBACK ME_ReadFromRTFString(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1611 ME_RTFStringStreamStruct *pStruct = (ME_RTFStringStreamStruct *)dwCookie;
1612 int count;
1614 count = min(cb, pStruct->length - pStruct->pos);
1615 memmove(lpBuff, pStruct->string + pStruct->pos, count);
1616 pStruct->pos += count;
1617 *pcb = count;
1618 return 0;
1621 static void
1622 ME_StreamInRTFString(ME_TextEditor *editor, BOOL selection, char *string)
1624 EDITSTREAM es;
1625 ME_RTFStringStreamStruct data;
1627 data.string = string;
1628 data.length = strlen(string);
1629 data.pos = 0;
1630 es.dwCookie = (DWORD_PTR)&data;
1631 es.pfnCallback = ME_ReadFromRTFString;
1632 ME_StreamIn(editor, SF_RTF | (selection ? SFF_SELECTION : 0), &es, FALSE);
1636 static int
1637 ME_FindText(ME_TextEditor *editor, DWORD flags, const CHARRANGE *chrg, const WCHAR *text, CHARRANGE *chrgText)
1639 const int nLen = lstrlenW(text);
1640 const int nTextLen = ME_GetTextLength(editor);
1641 int nMin, nMax;
1642 ME_Cursor cursor;
1643 WCHAR wLastChar = ' ';
1645 TRACE("flags==0x%08x, chrg->cpMin==%d, chrg->cpMax==%d text==%s\n",
1646 flags, chrg->cpMin, chrg->cpMax, debugstr_w(text));
1648 if (flags & ~(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD))
1649 FIXME("Flags 0x%08x not implemented\n",
1650 flags & ~(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD));
1652 nMin = chrg->cpMin;
1653 if (chrg->cpMax == -1)
1654 nMax = nTextLen;
1655 else
1656 nMax = chrg->cpMax > nTextLen ? nTextLen : chrg->cpMax;
1658 /* In 1.0 emulation, if cpMax reaches end of text, add the FR_DOWN flag */
1659 if (editor->bEmulateVersion10 && nMax == nTextLen)
1661 flags |= FR_DOWN;
1664 /* In 1.0 emulation, cpMin must always be no greater than cpMax */
1665 if (editor->bEmulateVersion10 && nMax < nMin)
1667 if (chrgText)
1669 chrgText->cpMin = -1;
1670 chrgText->cpMax = -1;
1672 return -1;
1675 /* when searching up, if cpMin < cpMax, then instead of searching
1676 * on [cpMin,cpMax], we search on [0,cpMin], otherwise, search on
1677 * [cpMax, cpMin]. The exception is when cpMax is -1, in which
1678 * case, it is always bigger than cpMin.
1680 if (!editor->bEmulateVersion10 && !(flags & FR_DOWN))
1682 int nSwap = nMax;
1684 nMax = nMin > nTextLen ? nTextLen : nMin;
1685 if (nMin < nSwap || chrg->cpMax == -1)
1686 nMin = 0;
1687 else
1688 nMin = nSwap;
1691 if (!nLen || nMin < 0 || nMax < 0 || nMax < nMin)
1693 if (chrgText)
1694 chrgText->cpMin = chrgText->cpMax = -1;
1695 return -1;
1698 if (flags & FR_DOWN) /* Forward search */
1700 /* If possible, find the character before where the search starts */
1701 if ((flags & FR_WHOLEWORD) && nMin)
1703 ME_CursorFromCharOfs(editor, nMin - 1, &cursor);
1704 wLastChar = cursor.pRun->member.run.strText->szData[cursor.nOffset];
1705 ME_MoveCursorChars(editor, &cursor, 1);
1706 } else {
1707 ME_CursorFromCharOfs(editor, nMin, &cursor);
1710 while (cursor.pRun && ME_GetCursorOfs(&cursor) + nLen <= nMax)
1712 ME_DisplayItem *pCurItem = cursor.pRun;
1713 int nCurStart = cursor.nOffset;
1714 int nMatched = 0;
1716 while (pCurItem && ME_CharCompare(pCurItem->member.run.strText->szData[nCurStart + nMatched], text[nMatched], (flags & FR_MATCHCASE)))
1718 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
1719 break;
1721 nMatched++;
1722 if (nMatched == nLen)
1724 ME_DisplayItem *pNextItem = pCurItem;
1725 int nNextStart = nCurStart;
1726 WCHAR wNextChar;
1728 /* Check to see if next character is a whitespace */
1729 if (flags & FR_WHOLEWORD)
1731 if (nCurStart + nMatched == pCurItem->member.run.strText->nLen)
1733 pNextItem = ME_FindItemFwd(pCurItem, diRun);
1734 nNextStart = -nMatched;
1737 if (pNextItem)
1738 wNextChar = pNextItem->member.run.strText->szData[nNextStart + nMatched];
1739 else
1740 wNextChar = ' ';
1742 if (isalnumW(wNextChar))
1743 break;
1746 cursor.nOffset += cursor.pPara->member.para.nCharOfs + cursor.pRun->member.run.nCharOfs;
1747 if (chrgText)
1749 chrgText->cpMin = cursor.nOffset;
1750 chrgText->cpMax = cursor.nOffset + nLen;
1752 TRACE("found at %d-%d\n", cursor.nOffset, cursor.nOffset + nLen);
1753 return cursor.nOffset;
1755 if (nCurStart + nMatched == pCurItem->member.run.strText->nLen)
1757 pCurItem = ME_FindItemFwd(pCurItem, diRun);
1758 nCurStart = -nMatched;
1761 if (pCurItem)
1762 wLastChar = pCurItem->member.run.strText->szData[nCurStart + nMatched];
1763 else
1764 wLastChar = ' ';
1766 cursor.nOffset++;
1767 if (cursor.nOffset == cursor.pRun->member.run.strText->nLen)
1769 ME_NextRun(&cursor.pPara, &cursor.pRun);
1770 cursor.nOffset = 0;
1774 else /* Backward search */
1776 /* If possible, find the character after where the search ends */
1777 if ((flags & FR_WHOLEWORD) && nMax < nTextLen - 1)
1779 ME_CursorFromCharOfs(editor, nMax + 1, &cursor);
1780 wLastChar = cursor.pRun->member.run.strText->szData[cursor.nOffset];
1781 ME_MoveCursorChars(editor, &cursor, -1);
1782 } else {
1783 ME_CursorFromCharOfs(editor, nMax, &cursor);
1786 while (cursor.pRun && ME_GetCursorOfs(&cursor) - nLen >= nMin)
1788 ME_DisplayItem *pCurItem = cursor.pRun;
1789 ME_DisplayItem *pCurPara = cursor.pPara;
1790 int nCurEnd = cursor.nOffset;
1791 int nMatched = 0;
1793 if (nCurEnd == 0)
1795 ME_PrevRun(&pCurPara, &pCurItem);
1796 nCurEnd = pCurItem->member.run.strText->nLen + nMatched;
1799 while (pCurItem && ME_CharCompare(pCurItem->member.run.strText->szData[nCurEnd - nMatched - 1], text[nLen - nMatched - 1], (flags & FR_MATCHCASE)))
1801 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
1802 break;
1804 nMatched++;
1805 if (nMatched == nLen)
1807 ME_DisplayItem *pPrevItem = pCurItem;
1808 int nPrevEnd = nCurEnd;
1809 WCHAR wPrevChar;
1810 int nStart;
1812 /* Check to see if previous character is a whitespace */
1813 if (flags & FR_WHOLEWORD)
1815 if (nPrevEnd - nMatched == 0)
1817 pPrevItem = ME_FindItemBack(pCurItem, diRun);
1818 if (pPrevItem)
1819 nPrevEnd = pPrevItem->member.run.strText->nLen + nMatched;
1822 if (pPrevItem)
1823 wPrevChar = pPrevItem->member.run.strText->szData[nPrevEnd - nMatched - 1];
1824 else
1825 wPrevChar = ' ';
1827 if (isalnumW(wPrevChar))
1828 break;
1831 nStart = pCurPara->member.para.nCharOfs
1832 + pCurItem->member.run.nCharOfs + nCurEnd - nMatched;
1833 if (chrgText)
1835 chrgText->cpMin = nStart;
1836 chrgText->cpMax = nStart + nLen;
1838 TRACE("found at %d-%d\n", nStart, nStart + nLen);
1839 return nStart;
1841 if (nCurEnd - nMatched == 0)
1843 ME_PrevRun(&pCurPara, &pCurItem);
1844 /* Don't care about pCurItem becoming NULL here; it's already taken
1845 * care of in the exterior loop condition */
1846 nCurEnd = pCurItem->member.run.strText->nLen + nMatched;
1849 if (pCurItem)
1850 wLastChar = pCurItem->member.run.strText->szData[nCurEnd - nMatched - 1];
1851 else
1852 wLastChar = ' ';
1854 cursor.nOffset--;
1855 if (cursor.nOffset < 0)
1857 ME_PrevRun(&cursor.pPara, &cursor.pRun);
1858 cursor.nOffset = cursor.pRun->member.run.strText->nLen;
1862 TRACE("not found\n");
1863 if (chrgText)
1864 chrgText->cpMin = chrgText->cpMax = -1;
1865 return -1;
1868 static int ME_GetTextEx(ME_TextEditor *editor, GETTEXTEX *ex, LPARAM pText)
1870 int nChars;
1871 ME_Cursor start;
1873 if (!ex->cb || !pText) return 0;
1875 if (ex->flags & ~(GT_SELECTION | GT_USECRLF))
1876 FIXME("GETTEXTEX flags 0x%08x not supported\n", ex->flags & ~(GT_SELECTION | GT_USECRLF));
1878 if (ex->flags & GT_SELECTION)
1880 int from, to;
1881 int nStartCur = ME_GetSelectionOfs(editor, &from, &to);
1882 start = editor->pCursors[nStartCur];
1883 nChars = to - from;
1885 else
1887 ME_SetCursorToStart(editor, &start);
1888 nChars = INT_MAX;
1890 if (ex->codepage == 1200)
1892 return ME_GetTextW(editor, (LPWSTR)pText, ex->cb / sizeof(WCHAR) - 1,
1893 &start, nChars, ex->flags & GT_USECRLF);
1895 else
1897 /* potentially each char may be a CR, why calculate the exact value with O(N) when
1898 we can just take a bigger buffer? :)
1899 The above assumption still holds with CR/LF counters, since CR->CRLF expansion
1900 occurs only in richedit 2.0 mode, in which line breaks have only one CR
1902 int crlfmul = (ex->flags & GT_USECRLF) ? 2 : 1;
1903 DWORD buflen;
1904 LPWSTR buffer;
1905 LRESULT rc;
1907 buflen = min(crlfmul * nChars, ex->cb - 1);
1908 buffer = heap_alloc((buflen + 1) * sizeof(WCHAR));
1910 nChars = ME_GetTextW(editor, buffer, buflen, &start, nChars, ex->flags & GT_USECRLF);
1911 rc = WideCharToMultiByte(ex->codepage, 0, buffer, nChars + 1,
1912 (LPSTR)pText, ex->cb, ex->lpDefaultChar, ex->lpUsedDefChar);
1913 if (rc) rc--; /* do not count 0 terminator */
1915 heap_free(buffer);
1916 return rc;
1920 static int ME_GetTextRange(ME_TextEditor *editor, WCHAR *strText,
1921 const ME_Cursor *start, int nLen, BOOL unicode)
1923 if (!strText) return 0;
1924 if (unicode) {
1925 return ME_GetTextW(editor, strText, INT_MAX, start, nLen, 0);
1926 } else {
1927 int nChars;
1928 WCHAR *p = ALLOC_N_OBJ(WCHAR, nLen+1);
1929 if (!p) return 0;
1930 nChars = ME_GetTextW(editor, p, nLen, start, nLen, 0);
1931 WideCharToMultiByte(CP_ACP, 0, p, nChars+1, (char *)strText,
1932 nLen+1, NULL, NULL);
1933 FREE_OBJ(p);
1934 return nChars;
1938 typedef struct tagME_GlobalDestStruct
1940 HGLOBAL hData;
1941 int nLength;
1942 } ME_GlobalDestStruct;
1944 static DWORD CALLBACK ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1946 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
1947 int i;
1948 WORD *pSrc, *pDest;
1950 cb = cb >> 1;
1951 pDest = (WORD *)lpBuff;
1952 pSrc = GlobalLock(pData->hData);
1953 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
1954 pDest[i] = pSrc[pData->nLength+i];
1956 pData->nLength += i;
1957 *pcb = 2*i;
1958 GlobalUnlock(pData->hData);
1959 return 0;
1962 static DWORD CALLBACK ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1964 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
1965 int i;
1966 BYTE *pSrc, *pDest;
1968 pDest = lpBuff;
1969 pSrc = GlobalLock(pData->hData);
1970 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
1971 pDest[i] = pSrc[pData->nLength+i];
1973 pData->nLength += i;
1974 *pcb = i;
1975 GlobalUnlock(pData->hData);
1976 return 0;
1979 static BOOL ME_Paste(ME_TextEditor *editor)
1981 DWORD dwFormat = 0;
1982 EDITSTREAM es;
1983 ME_GlobalDestStruct gds;
1984 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
1985 UINT cf = 0;
1987 if (IsClipboardFormatAvailable(nRTFFormat))
1988 cf = nRTFFormat, dwFormat = SF_RTF;
1989 else if (IsClipboardFormatAvailable(CF_UNICODETEXT))
1990 cf = CF_UNICODETEXT, dwFormat = SF_TEXT|SF_UNICODE;
1991 else
1992 return FALSE;
1994 if (!OpenClipboard(editor->hWnd))
1995 return FALSE;
1996 gds.hData = GetClipboardData(cf);
1997 gds.nLength = 0;
1998 es.dwCookie = (DWORD_PTR)&gds;
1999 es.pfnCallback = dwFormat == SF_RTF ? ME_ReadFromHGLOBALRTF : ME_ReadFromHGLOBALUnicode;
2000 ME_StreamIn(editor, dwFormat|SFF_SELECTION, &es, FALSE);
2002 CloseClipboard();
2003 return TRUE;
2006 static BOOL ME_Copy(ME_TextEditor *editor, const ME_Cursor *start, int nChars)
2008 LPDATAOBJECT dataObj = NULL;
2009 HRESULT hr = S_OK;
2011 if (editor->cPasswordMask)
2012 return FALSE; /* Copying or Cutting masked text isn't allowed */
2014 if(editor->lpOleCallback)
2016 CHARRANGE range;
2017 range.cpMin = ME_GetCursorOfs(start);
2018 range.cpMax = range.cpMin + nChars;
2019 hr = IRichEditOleCallback_GetClipboardData(editor->lpOleCallback, &range, RECO_COPY, &dataObj);
2021 if(FAILED(hr) || !dataObj)
2022 hr = ME_GetDataObject(editor, start, nChars, &dataObj);
2023 if(SUCCEEDED(hr)) {
2024 hr = OleSetClipboard(dataObj);
2025 IDataObject_Release(dataObj);
2027 return SUCCEEDED(hr) != 0;
2030 /* helper to send a msg filter notification */
2031 static BOOL
2032 ME_FilterEvent(ME_TextEditor *editor, UINT msg, WPARAM* wParam, LPARAM* lParam)
2034 MSGFILTER msgf;
2036 if (!editor->hWnd || !editor->hwndParent) return FALSE;
2037 msgf.nmhdr.hwndFrom = editor->hWnd;
2038 msgf.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
2039 msgf.nmhdr.code = EN_MSGFILTER;
2040 msgf.msg = msg;
2041 msgf.wParam = *wParam;
2042 msgf.lParam = *lParam;
2043 if (SendMessageW(editor->hwndParent, WM_NOTIFY, msgf.nmhdr.idFrom, (LPARAM)&msgf))
2044 return FALSE;
2045 *wParam = msgf.wParam;
2046 *lParam = msgf.lParam;
2047 msgf.wParam = *wParam;
2049 return TRUE;
2052 static void ME_UpdateSelectionLinkAttribute(ME_TextEditor *editor)
2054 ME_DisplayItem *startPara, *endPara;
2055 ME_DisplayItem *prev_para;
2056 ME_Cursor *from, *to;
2057 ME_Cursor start;
2058 int nChars;
2060 if (!editor->AutoURLDetect_bEnable) return;
2062 ME_GetSelection(editor, &from, &to);
2064 /* Find paragraph previous to the one that contains start cursor */
2065 startPara = from->pPara;
2066 prev_para = startPara->member.para.prev_para;
2067 if (prev_para->type == diParagraph) startPara = prev_para;
2069 /* Find paragraph that contains end cursor */
2070 endPara = to->pPara->member.para.next_para;
2072 start.pPara = startPara;
2073 start.pRun = ME_FindItemFwd(startPara, diRun);
2074 start.nOffset = 0;
2075 nChars = endPara->member.para.nCharOfs - startPara->member.para.nCharOfs;
2077 ME_UpdateLinkAttribute(editor, &start, nChars);
2080 static BOOL
2081 ME_KeyDown(ME_TextEditor *editor, WORD nKey)
2083 BOOL ctrl_is_down = GetKeyState(VK_CONTROL) & 0x8000;
2084 BOOL shift_is_down = GetKeyState(VK_SHIFT) & 0x8000;
2086 if (editor->bMouseCaptured)
2087 return FALSE;
2088 if (nKey != VK_SHIFT && nKey != VK_CONTROL && nKey != VK_MENU)
2089 editor->nSelectionType = stPosition;
2091 switch (nKey)
2093 case VK_LEFT:
2094 case VK_RIGHT:
2095 case VK_HOME:
2096 case VK_END:
2097 editor->nUDArrowX = -1;
2098 /* fall through */
2099 case VK_UP:
2100 case VK_DOWN:
2101 case VK_PRIOR:
2102 case VK_NEXT:
2103 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
2104 ME_ArrowKey(editor, nKey, shift_is_down, ctrl_is_down);
2105 return TRUE;
2106 case VK_BACK:
2107 case VK_DELETE:
2108 editor->nUDArrowX = -1;
2109 /* FIXME backspace and delete aren't the same, they act different wrt paragraph style of the merged paragraph */
2110 if (editor->styleFlags & ES_READONLY)
2111 return FALSE;
2112 if (ME_IsSelection(editor))
2114 ME_DeleteSelection(editor);
2115 ME_CommitUndo(editor);
2117 else if (nKey == VK_DELETE)
2119 /* Delete stops group typing.
2120 * (See MSDN remarks on EM_STOPGROUPTYPING message) */
2121 ME_DeleteTextAtCursor(editor, 1, 1);
2122 ME_CommitUndo(editor);
2124 else if (ME_ArrowKey(editor, VK_LEFT, FALSE, FALSE))
2126 BOOL bDeletionSucceeded;
2127 /* Backspace can be grouped for a single undo */
2128 ME_ContinueCoalescingTransaction(editor);
2129 bDeletionSucceeded = ME_DeleteTextAtCursor(editor, 1, 1);
2130 if (!bDeletionSucceeded && !editor->bEmulateVersion10) { /* v4.1 */
2131 /* Deletion was prevented so the cursor is moved back to where it was.
2132 * (e.g. this happens when trying to delete cell boundaries)
2134 ME_ArrowKey(editor, VK_RIGHT, FALSE, FALSE);
2136 ME_CommitCoalescingUndo(editor);
2138 else
2139 return TRUE;
2140 ME_MoveCursorFromTableRowStartParagraph(editor);
2141 ME_UpdateSelectionLinkAttribute(editor);
2142 ME_UpdateRepaint(editor);
2143 ME_SendRequestResize(editor, FALSE);
2144 return TRUE;
2145 case VK_RETURN:
2146 if (editor->bDialogMode)
2148 if (ctrl_is_down)
2149 return TRUE;
2151 if (!(editor->styleFlags & ES_WANTRETURN))
2153 if (editor->hwndParent)
2155 DWORD dw;
2156 dw = SendMessageW(editor->hwndParent, DM_GETDEFID, 0, 0);
2157 if (HIWORD(dw) == DC_HASDEFID)
2159 HWND hwDefCtrl = GetDlgItem(editor->hwndParent, LOWORD(dw));
2160 if (hwDefCtrl)
2162 SendMessageW(editor->hwndParent, WM_NEXTDLGCTL, (WPARAM)hwDefCtrl, TRUE);
2163 PostMessageW(hwDefCtrl, WM_KEYDOWN, VK_RETURN, 0);
2167 return TRUE;
2171 if (editor->styleFlags & ES_MULTILINE)
2173 ME_Cursor cursor = editor->pCursors[0];
2174 ME_DisplayItem *para = cursor.pPara;
2175 int from, to;
2176 const WCHAR endl = '\r';
2177 ME_Style *style;
2179 if (editor->styleFlags & ES_READONLY) {
2180 MessageBeep(MB_ICONERROR);
2181 return TRUE;
2184 ME_GetSelectionOfs(editor, &from, &to);
2185 if (editor->nTextLimit > ME_GetTextLength(editor) - (to-from))
2187 if (!editor->bEmulateVersion10) { /* v4.1 */
2188 if (para->member.para.nFlags & MEPF_ROWEND) {
2189 /* Add a new table row after this row. */
2190 para = ME_AppendTableRow(editor, para);
2191 para = para->member.para.next_para;
2192 editor->pCursors[0].pPara = para;
2193 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2194 editor->pCursors[0].nOffset = 0;
2195 editor->pCursors[1] = editor->pCursors[0];
2196 ME_CommitUndo(editor);
2197 ME_CheckTablesForCorruption(editor);
2198 ME_UpdateRepaint(editor);
2199 return TRUE;
2201 else if (para == editor->pCursors[1].pPara &&
2202 cursor.nOffset + cursor.pRun->member.run.nCharOfs == 0 &&
2203 para->member.para.prev_para->member.para.nFlags & MEPF_ROWSTART &&
2204 !para->member.para.prev_para->member.para.nCharOfs)
2206 /* Insert a newline before the table. */
2207 para = para->member.para.prev_para;
2208 para->member.para.nFlags &= ~MEPF_ROWSTART;
2209 editor->pCursors[0].pPara = para;
2210 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2211 editor->pCursors[1] = editor->pCursors[0];
2212 ME_InsertTextFromCursor(editor, 0, &endl, 1,
2213 editor->pCursors[0].pRun->member.run.style);
2214 para = editor->pBuffer->pFirst->member.para.next_para;
2215 ME_SetDefaultParaFormat(para->member.para.pFmt);
2216 para->member.para.nFlags = MEPF_REWRAP;
2217 editor->pCursors[0].pPara = para;
2218 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2219 editor->pCursors[1] = editor->pCursors[0];
2220 para->member.para.next_para->member.para.nFlags |= MEPF_ROWSTART;
2221 ME_CommitCoalescingUndo(editor);
2222 ME_CheckTablesForCorruption(editor);
2223 ME_UpdateRepaint(editor);
2224 return TRUE;
2226 } else { /* v1.0 - 3.0 */
2227 ME_DisplayItem *para = cursor.pPara;
2228 if (ME_IsInTable(para))
2230 if (cursor.pRun->member.run.nFlags & MERF_ENDPARA)
2232 if (from == to) {
2233 ME_ContinueCoalescingTransaction(editor);
2234 para = ME_AppendTableRow(editor, para);
2235 editor->pCursors[0].pPara = para;
2236 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2237 editor->pCursors[0].nOffset = 0;
2238 editor->pCursors[1] = editor->pCursors[0];
2239 ME_CommitCoalescingUndo(editor);
2240 ME_UpdateRepaint(editor);
2241 return TRUE;
2243 } else {
2244 ME_ContinueCoalescingTransaction(editor);
2245 if (cursor.pRun->member.run.nCharOfs + cursor.nOffset == 0 &&
2246 !ME_IsInTable(para->member.para.prev_para))
2248 /* Insert newline before table */
2249 cursor.pRun = ME_FindItemBack(para, diRun);
2250 if (cursor.pRun) {
2251 editor->pCursors[0].pRun = cursor.pRun;
2252 editor->pCursors[0].pPara = para->member.para.prev_para;
2254 editor->pCursors[0].nOffset = 0;
2255 editor->pCursors[1] = editor->pCursors[0];
2256 ME_InsertTextFromCursor(editor, 0, &endl, 1,
2257 editor->pCursors[0].pRun->member.run.style);
2258 } else {
2259 editor->pCursors[1] = editor->pCursors[0];
2260 para = ME_AppendTableRow(editor, para);
2261 editor->pCursors[0].pPara = para;
2262 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2263 editor->pCursors[0].nOffset = 0;
2264 editor->pCursors[1] = editor->pCursors[0];
2266 ME_CommitCoalescingUndo(editor);
2267 ME_UpdateRepaint(editor);
2268 return TRUE;
2273 style = ME_GetInsertStyle(editor, 0);
2274 ME_SaveTempStyle(editor);
2275 ME_ContinueCoalescingTransaction(editor);
2276 if (shift_is_down)
2277 ME_InsertEndRowFromCursor(editor, 0);
2278 else
2279 ME_InsertTextFromCursor(editor, 0, &endl, 1, style);
2280 ME_ReleaseStyle(style);
2281 ME_CommitCoalescingUndo(editor);
2282 SetCursor(NULL);
2284 ME_UpdateSelectionLinkAttribute(editor);
2285 ME_UpdateRepaint(editor);
2287 return TRUE;
2289 break;
2290 case VK_ESCAPE:
2291 if (editor->bDialogMode && editor->hwndParent)
2292 PostMessageW(editor->hwndParent, WM_CLOSE, 0, 0);
2293 return TRUE;
2294 case VK_TAB:
2295 if (editor->bDialogMode && editor->hwndParent)
2296 SendMessageW(editor->hwndParent, WM_NEXTDLGCTL, shift_is_down, 0);
2297 return TRUE;
2298 case 'A':
2299 if (ctrl_is_down)
2301 ME_SetSelection(editor, 0, -1);
2302 return TRUE;
2304 break;
2305 case 'V':
2306 if (ctrl_is_down)
2307 return ME_Paste(editor);
2308 break;
2309 case 'C':
2310 case 'X':
2311 if (ctrl_is_down)
2313 BOOL result;
2314 int nOfs, nChars;
2315 int nStartCur = ME_GetSelectionOfs(editor, &nOfs, &nChars);
2316 ME_Cursor *selStart = &editor->pCursors[nStartCur];
2318 nChars -= nOfs;
2319 result = ME_Copy(editor, selStart, nChars);
2320 if (result && nKey == 'X')
2322 ME_InternalDeleteText(editor, selStart, nChars, FALSE);
2323 ME_CommitUndo(editor);
2324 ME_UpdateRepaint(editor);
2326 return result;
2328 break;
2329 case 'Z':
2330 if (ctrl_is_down)
2332 ME_Undo(editor);
2333 return TRUE;
2335 break;
2336 case 'Y':
2337 if (ctrl_is_down)
2339 ME_Redo(editor);
2340 return TRUE;
2342 break;
2344 default:
2345 if (nKey != VK_SHIFT && nKey != VK_CONTROL && nKey && nKey != VK_MENU)
2346 editor->nUDArrowX = -1;
2347 if (ctrl_is_down)
2349 if (nKey == 'W')
2351 CHARFORMAT2W chf;
2352 char buf[2048];
2353 chf.cbSize = sizeof(chf);
2355 ME_GetSelectionCharFormat(editor, &chf);
2356 ME_DumpStyleToBuf(&chf, buf);
2357 MessageBoxA(NULL, buf, "Style dump", MB_OK);
2359 if (nKey == 'Q')
2361 ME_CheckCharOffsets(editor);
2365 return FALSE;
2368 static LRESULT ME_Char(ME_TextEditor *editor, WPARAM charCode,
2369 LPARAM flags, BOOL unicode)
2371 WCHAR wstr;
2373 if (editor->bMouseCaptured)
2374 return 0;
2376 if (unicode)
2377 wstr = (WCHAR)charCode;
2378 else
2380 CHAR charA = charCode;
2381 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &wstr, 1);
2384 if (editor->styleFlags & ES_READONLY) {
2385 MessageBeep(MB_ICONERROR);
2386 return 0; /* FIXME really 0 ? */
2389 if ((unsigned)wstr >= ' ' || wstr == '\t')
2391 ME_Cursor cursor = editor->pCursors[0];
2392 ME_DisplayItem *para = cursor.pPara;
2393 int from, to;
2394 BOOL ctrl_is_down = GetKeyState(VK_CONTROL) & 0x8000;
2395 ME_GetSelectionOfs(editor, &from, &to);
2396 if (wstr == '\t' &&
2397 /* v4.1 allows tabs to be inserted with ctrl key down */
2398 !(ctrl_is_down && !editor->bEmulateVersion10))
2400 ME_DisplayItem *para;
2401 BOOL bSelectedRow = FALSE;
2403 para = cursor.pPara;
2404 if (ME_IsSelection(editor) &&
2405 cursor.pRun->member.run.nCharOfs + cursor.nOffset == 0 &&
2406 to == ME_GetCursorOfs(&editor->pCursors[0]) &&
2407 para->member.para.prev_para->type == diParagraph)
2409 para = para->member.para.prev_para;
2410 bSelectedRow = TRUE;
2412 if (ME_IsInTable(para))
2414 ME_TabPressedInTable(editor, bSelectedRow);
2415 ME_CommitUndo(editor);
2416 return 0;
2418 } else if (!editor->bEmulateVersion10) { /* v4.1 */
2419 if (para->member.para.nFlags & MEPF_ROWEND) {
2420 if (from == to) {
2421 para = para->member.para.next_para;
2422 if (para->member.para.nFlags & MEPF_ROWSTART)
2423 para = para->member.para.next_para;
2424 editor->pCursors[0].pPara = para;
2425 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2426 editor->pCursors[0].nOffset = 0;
2427 editor->pCursors[1] = editor->pCursors[0];
2430 } else { /* v1.0 - 3.0 */
2431 if (ME_IsInTable(cursor.pRun) &&
2432 cursor.pRun->member.run.nFlags & MERF_ENDPARA &&
2433 from == to)
2435 /* Text should not be inserted at the end of the table. */
2436 MessageBeep(-1);
2437 return 0;
2440 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
2441 /* WM_CHAR is restricted to nTextLimit */
2442 if(editor->nTextLimit > ME_GetTextLength(editor) - (to-from))
2444 ME_Style *style = ME_GetInsertStyle(editor, 0);
2445 ME_SaveTempStyle(editor);
2446 ME_ContinueCoalescingTransaction(editor);
2447 ME_InsertTextFromCursor(editor, 0, &wstr, 1, style);
2448 ME_ReleaseStyle(style);
2449 ME_CommitCoalescingUndo(editor);
2450 ITextHost_TxSetCursor(editor->texthost, NULL, FALSE);
2453 ME_UpdateSelectionLinkAttribute(editor);
2454 ME_UpdateRepaint(editor);
2456 return 0;
2459 /* Process the message and calculate the new click count.
2461 * returns: The click count if it is mouse down event, else returns 0. */
2462 static int ME_CalculateClickCount(ME_TextEditor *editor, UINT msg, WPARAM wParam,
2463 LPARAM lParam)
2465 static int clickNum = 0;
2466 if (msg < WM_MOUSEFIRST || msg > WM_MOUSELAST)
2467 return 0;
2469 if ((msg == WM_LBUTTONDBLCLK) ||
2470 (msg == WM_RBUTTONDBLCLK) ||
2471 (msg == WM_MBUTTONDBLCLK) ||
2472 (msg == WM_XBUTTONDBLCLK))
2474 msg -= (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN);
2477 if ((msg == WM_LBUTTONDOWN) ||
2478 (msg == WM_RBUTTONDOWN) ||
2479 (msg == WM_MBUTTONDOWN) ||
2480 (msg == WM_XBUTTONDOWN))
2482 static MSG prevClickMsg;
2483 MSG clickMsg;
2484 /* Compare the editor instead of the hwnd so that the this
2485 * can still be done for windowless richedit controls. */
2486 clickMsg.hwnd = (HWND)editor;
2487 clickMsg.message = msg;
2488 clickMsg.wParam = wParam;
2489 clickMsg.lParam = lParam;
2490 clickMsg.time = GetMessageTime();
2491 clickMsg.pt.x = (short)LOWORD(lParam);
2492 clickMsg.pt.y = (short)HIWORD(lParam);
2493 if ((clickNum != 0) &&
2494 (clickMsg.message == prevClickMsg.message) &&
2495 (clickMsg.hwnd == prevClickMsg.hwnd) &&
2496 (clickMsg.wParam == prevClickMsg.wParam) &&
2497 (clickMsg.time - prevClickMsg.time < GetDoubleClickTime()) &&
2498 (abs(clickMsg.pt.x - prevClickMsg.pt.x) < GetSystemMetrics(SM_CXDOUBLECLK)/2) &&
2499 (abs(clickMsg.pt.y - prevClickMsg.pt.y) < GetSystemMetrics(SM_CYDOUBLECLK)/2))
2501 clickNum++;
2502 } else {
2503 clickNum = 1;
2505 prevClickMsg = clickMsg;
2506 } else {
2507 return 0;
2509 return clickNum;
2512 static BOOL ME_SetCursor(ME_TextEditor *editor)
2514 ME_Cursor cursor;
2515 POINT pt;
2516 BOOL isExact;
2517 SCROLLBARINFO sbi;
2518 DWORD messagePos = GetMessagePos();
2519 pt.x = (short)LOWORD(messagePos);
2520 pt.y = (short)HIWORD(messagePos);
2522 if (editor->hWnd)
2524 sbi.cbSize = sizeof(sbi);
2525 GetScrollBarInfo(editor->hWnd, OBJID_HSCROLL, &sbi);
2526 if (!(sbi.rgstate[0] & (STATE_SYSTEM_INVISIBLE|STATE_SYSTEM_OFFSCREEN)) &&
2527 PtInRect(&sbi.rcScrollBar, pt))
2529 ITextHost_TxSetCursor(editor->texthost,
2530 LoadCursorW(NULL, (WCHAR*)IDC_ARROW), FALSE);
2531 return TRUE;
2533 sbi.cbSize = sizeof(sbi);
2534 GetScrollBarInfo(editor->hWnd, OBJID_VSCROLL, &sbi);
2535 if (!(sbi.rgstate[0] & (STATE_SYSTEM_INVISIBLE|STATE_SYSTEM_OFFSCREEN)) &&
2536 PtInRect(&sbi.rcScrollBar, pt))
2538 ITextHost_TxSetCursor(editor->texthost,
2539 LoadCursorW(NULL, (WCHAR*)IDC_ARROW), FALSE);
2540 return TRUE;
2543 ITextHost_TxScreenToClient(editor->texthost, &pt);
2545 if (editor->nSelectionType == stLine && editor->bMouseCaptured) {
2546 ITextHost_TxSetCursor(editor->texthost, hLeft, FALSE);
2547 return TRUE;
2549 if (!editor->bEmulateVersion10 /* v4.1 */ &&
2550 pt.y < editor->rcFormat.top &&
2551 pt.x < editor->rcFormat.left)
2553 ITextHost_TxSetCursor(editor->texthost, hLeft, FALSE);
2554 return TRUE;
2556 if (pt.y < editor->rcFormat.top || pt.y > editor->rcFormat.bottom)
2558 if (editor->bEmulateVersion10) /* v1.0 - 3.0 */
2559 ITextHost_TxSetCursor(editor->texthost,
2560 LoadCursorW(NULL, (WCHAR*)IDC_ARROW), FALSE);
2561 else /* v4.1 */
2562 ITextHost_TxSetCursor(editor->texthost,
2563 LoadCursorW(NULL, (WCHAR*)IDC_IBEAM), TRUE);
2564 return TRUE;
2566 if (pt.x < editor->rcFormat.left)
2568 ITextHost_TxSetCursor(editor->texthost, hLeft, FALSE);
2569 return TRUE;
2571 ME_CharFromPos(editor, pt.x, pt.y, &cursor, &isExact);
2572 if (isExact)
2574 ME_Run *run;
2576 run = &cursor.pRun->member.run;
2577 if (run->style->fmt.dwMask & CFM_LINK &&
2578 run->style->fmt.dwEffects & CFE_LINK)
2580 ITextHost_TxSetCursor(editor->texthost,
2581 LoadCursorW(NULL, (WCHAR*)IDC_HAND),
2582 FALSE);
2583 return TRUE;
2586 if (ME_IsSelection(editor))
2588 int selStart, selEnd;
2589 int offset = ME_GetCursorOfs(&cursor);
2591 ME_GetSelectionOfs(editor, &selStart, &selEnd);
2592 if (selStart <= offset && selEnd >= offset) {
2593 ITextHost_TxSetCursor(editor->texthost,
2594 LoadCursorW(NULL, (WCHAR*)IDC_ARROW),
2595 FALSE);
2596 return TRUE;
2600 ITextHost_TxSetCursor(editor->texthost,
2601 LoadCursorW(NULL, (WCHAR*)IDC_IBEAM), TRUE);
2602 return TRUE;
2605 static void ME_SetDefaultFormatRect(ME_TextEditor *editor)
2607 ITextHost_TxGetClientRect(editor->texthost, &editor->rcFormat);
2608 editor->rcFormat.top += editor->exStyleFlags & WS_EX_CLIENTEDGE ? 1 : 0;
2609 editor->rcFormat.left += 1 + editor->selofs;
2610 editor->rcFormat.right -= 1;
2613 static BOOL ME_ShowContextMenu(ME_TextEditor *editor, int x, int y)
2615 CHARRANGE selrange;
2616 HMENU menu;
2617 int seltype = 0;
2618 if(!editor->lpOleCallback || !editor->hWnd)
2619 return FALSE;
2620 ME_GetSelectionOfs(editor, &selrange.cpMin, &selrange.cpMax);
2621 if(selrange.cpMin == selrange.cpMax)
2622 seltype |= SEL_EMPTY;
2623 else
2625 /* FIXME: Handle objects */
2626 seltype |= SEL_TEXT;
2627 if(selrange.cpMax-selrange.cpMin > 1)
2628 seltype |= SEL_MULTICHAR;
2630 if(SUCCEEDED(IRichEditOleCallback_GetContextMenu(editor->lpOleCallback, seltype, NULL, &selrange, &menu)))
2632 TrackPopupMenu(menu, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, editor->hwndParent, NULL);
2633 DestroyMenu(menu);
2635 return TRUE;
2638 ME_TextEditor *ME_MakeEditor(ITextHost *texthost, BOOL bEmulateVersion10)
2640 ME_TextEditor *ed = ALLOC_OBJ(ME_TextEditor);
2641 int i;
2642 DWORD props;
2643 LONG selbarwidth;
2645 ed->hWnd = NULL;
2646 ed->hwndParent = NULL;
2647 ed->sizeWindow.cx = ed->sizeWindow.cy = 0;
2648 ed->texthost = texthost;
2649 ed->bEmulateVersion10 = bEmulateVersion10;
2650 ed->styleFlags = 0;
2651 ITextHost_TxGetPropertyBits(texthost,
2652 (TXTBIT_RICHTEXT|TXTBIT_MULTILINE|
2653 TXTBIT_READONLY|TXTBIT_USEPASSWORD|
2654 TXTBIT_HIDESELECTION|TXTBIT_SAVESELECTION|
2655 TXTBIT_AUTOWORDSEL|TXTBIT_VERTICAL|
2656 TXTBIT_WORDWRAP|TXTBIT_DISABLEDRAG),
2657 &props);
2658 ITextHost_TxGetScrollBars(texthost, &ed->styleFlags);
2659 ed->styleFlags &= (WS_VSCROLL|WS_HSCROLL|ES_AUTOVSCROLL|
2660 ES_AUTOHSCROLL|ES_DISABLENOSCROLL);
2661 ed->pBuffer = ME_MakeText();
2662 ed->nZoomNumerator = ed->nZoomDenominator = 0;
2663 ed->nAvailWidth = 0; /* wrap to client area */
2664 ME_MakeFirstParagraph(ed);
2665 /* The four cursors are for:
2666 * 0 - The position where the caret is shown
2667 * 1 - The anchored end of the selection (for normal selection)
2668 * 2 & 3 - The anchored start and end respectively for word, line,
2669 * or paragraph selection.
2671 ed->nCursors = 4;
2672 ed->pCursors = ALLOC_N_OBJ(ME_Cursor, ed->nCursors);
2673 ME_SetCursorToStart(ed, &ed->pCursors[0]);
2674 ed->pCursors[1] = ed->pCursors[0];
2675 ed->pCursors[2] = ed->pCursors[0];
2676 ed->pCursors[3] = ed->pCursors[1];
2677 ed->nLastTotalLength = ed->nTotalLength = 0;
2678 ed->nLastTotalWidth = ed->nTotalWidth = 0;
2679 ed->nUDArrowX = -1;
2680 ed->nSequence = 0;
2681 ed->rgbBackColor = -1;
2682 ed->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
2683 ed->bCaretAtEnd = FALSE;
2684 ed->nEventMask = 0;
2685 ed->nModifyStep = 0;
2686 ed->nTextLimit = TEXT_LIMIT_DEFAULT;
2687 ed->pUndoStack = ed->pRedoStack = ed->pUndoStackBottom = NULL;
2688 ed->nUndoStackSize = 0;
2689 ed->nUndoLimit = STACK_SIZE_DEFAULT;
2690 ed->nUndoMode = umAddToUndo;
2691 ed->nParagraphs = 1;
2692 ed->nLastSelStart = ed->nLastSelEnd = 0;
2693 ed->pLastSelStartPara = ed->pLastSelEndPara = ed->pCursors[0].pPara;
2694 ed->bHideSelection = FALSE;
2695 ed->pfnWordBreak = NULL;
2696 ed->lpOleCallback = NULL;
2697 ed->mode = TM_MULTILEVELUNDO | TM_MULTICODEPAGE;
2698 ed->mode |= (props & TXTBIT_RICHTEXT) ? TM_RICHTEXT : TM_PLAINTEXT;
2699 ed->AutoURLDetect_bEnable = FALSE;
2700 ed->bHaveFocus = FALSE;
2701 ed->bDialogMode = FALSE;
2702 ed->bMouseCaptured = FALSE;
2703 for (i=0; i<HFONT_CACHE_SIZE; i++)
2705 ed->pFontCache[i].nRefs = 0;
2706 ed->pFontCache[i].nAge = 0;
2707 ed->pFontCache[i].hFont = NULL;
2710 ME_CheckCharOffsets(ed);
2711 ed->bDefaultFormatRect = TRUE;
2712 ITextHost_TxGetSelectionBarWidth(ed->texthost, &selbarwidth);
2713 if (selbarwidth) {
2714 /* FIXME: Convert selbarwidth from HIMETRIC to pixels */
2715 ed->selofs = SELECTIONBAR_WIDTH;
2716 ed->styleFlags |= ES_SELECTIONBAR;
2717 } else {
2718 ed->selofs = 0;
2720 ed->nSelectionType = stPosition;
2722 ed->cPasswordMask = 0;
2723 if (props & TXTBIT_USEPASSWORD)
2724 ITextHost_TxGetPasswordChar(texthost, &ed->cPasswordMask);
2726 if (props & TXTBIT_AUTOWORDSEL)
2727 ed->styleFlags |= ECO_AUTOWORDSELECTION;
2728 if (props & TXTBIT_MULTILINE) {
2729 ed->styleFlags |= ES_MULTILINE;
2730 ed->bWordWrap = (props & TXTBIT_WORDWRAP) != 0;
2731 } else {
2732 ed->bWordWrap = FALSE;
2734 if (props & TXTBIT_READONLY)
2735 ed->styleFlags |= ES_READONLY;
2736 if (!(props & TXTBIT_HIDESELECTION))
2737 ed->styleFlags |= ES_NOHIDESEL;
2738 if (props & TXTBIT_SAVESELECTION)
2739 ed->styleFlags |= ES_SAVESEL;
2740 if (props & TXTBIT_VERTICAL)
2741 ed->styleFlags |= ES_VERTICAL;
2742 if (props & TXTBIT_DISABLEDRAG)
2743 ed->styleFlags |= ES_NOOLEDRAGDROP;
2745 ed->notified_cr.cpMin = ed->notified_cr.cpMax = 0;
2747 /* Default scrollbar information */
2748 ed->vert_si.cbSize = sizeof(SCROLLINFO);
2749 ed->vert_si.nMin = 0;
2750 ed->vert_si.nMax = 0;
2751 ed->vert_si.nPage = 0;
2752 ed->vert_si.nPos = 0;
2754 ed->horz_si.cbSize = sizeof(SCROLLINFO);
2755 ed->horz_si.nMin = 0;
2756 ed->horz_si.nMax = 0;
2757 ed->horz_si.nPage = 0;
2758 ed->horz_si.nPos = 0;
2760 OleInitialize(NULL);
2762 return ed;
2765 static void ME_DestroyEditor(ME_TextEditor *editor)
2767 ME_DisplayItem *pFirst = editor->pBuffer->pFirst;
2768 ME_DisplayItem *p = pFirst, *pNext = NULL;
2769 int i;
2771 ME_ClearTempStyle(editor);
2772 ME_EmptyUndoStack(editor);
2773 while(p) {
2774 pNext = p->next;
2775 ME_DestroyDisplayItem(p);
2776 p = pNext;
2778 ME_ReleaseStyle(editor->pBuffer->pDefaultStyle);
2779 for (i=0; i<HFONT_CACHE_SIZE; i++)
2781 if (editor->pFontCache[i].hFont)
2782 DeleteObject(editor->pFontCache[i].hFont);
2784 if (editor->rgbBackColor != -1)
2785 DeleteObject(editor->hbrBackground);
2786 if(editor->lpOleCallback)
2787 IUnknown_Release(editor->lpOleCallback);
2788 IUnknown_Release(editor->texthost);
2789 OleUninitialize();
2791 FREE_OBJ(editor->pBuffer);
2792 FREE_OBJ(editor->pCursors);
2794 FREE_OBJ(editor);
2797 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
2799 TRACE("\n");
2800 switch (fdwReason)
2802 case DLL_PROCESS_ATTACH:
2803 DisableThreadLibraryCalls(hinstDLL);
2804 me_heap = HeapCreate (0, 0x10000, 0);
2805 if (!ME_RegisterEditorClass(hinstDLL)) return FALSE;
2806 hLeft = LoadCursorW(hinstDLL, MAKEINTRESOURCEW(OCR_REVERSE));
2807 LookupInit();
2808 break;
2810 case DLL_PROCESS_DETACH:
2811 UnregisterClassW(RICHEDIT_CLASS20W, 0);
2812 UnregisterClassW(MSFTEDIT_CLASS, 0);
2813 UnregisterClassA(RICHEDIT_CLASS20A, 0);
2814 UnregisterClassA("RichEdit50A", 0);
2815 if (ME_ListBoxRegistered)
2816 UnregisterClassW(REListBox20W, 0);
2817 if (ME_ComboBoxRegistered)
2818 UnregisterClassW(REComboBox20W, 0);
2819 LookupCleanup();
2820 HeapDestroy (me_heap);
2821 me_heap = NULL;
2822 break;
2824 return TRUE;
2828 static const char * const edit_messages[] = {
2829 "EM_GETSEL",
2830 "EM_SETSEL",
2831 "EM_GETRECT",
2832 "EM_SETRECT",
2833 "EM_SETRECTNP",
2834 "EM_SCROLL",
2835 "EM_LINESCROLL",
2836 "EM_SCROLLCARET",
2837 "EM_GETMODIFY",
2838 "EM_SETMODIFY",
2839 "EM_GETLINECOUNT",
2840 "EM_LINEINDEX",
2841 "EM_SETHANDLE",
2842 "EM_GETHANDLE",
2843 "EM_GETTHUMB",
2844 "EM_UNKNOWN_BF",
2845 "EM_UNKNOWN_C0",
2846 "EM_LINELENGTH",
2847 "EM_REPLACESEL",
2848 "EM_UNKNOWN_C3",
2849 "EM_GETLINE",
2850 "EM_LIMITTEXT",
2851 "EM_CANUNDO",
2852 "EM_UNDO",
2853 "EM_FMTLINES",
2854 "EM_LINEFROMCHAR",
2855 "EM_UNKNOWN_CA",
2856 "EM_SETTABSTOPS",
2857 "EM_SETPASSWORDCHAR",
2858 "EM_EMPTYUNDOBUFFER",
2859 "EM_GETFIRSTVISIBLELINE",
2860 "EM_SETREADONLY",
2861 "EM_SETWORDBREAKPROC",
2862 "EM_GETWORDBREAKPROC",
2863 "EM_GETPASSWORDCHAR",
2864 "EM_SETMARGINS",
2865 "EM_GETMARGINS",
2866 "EM_GETLIMITTEXT",
2867 "EM_POSFROMCHAR",
2868 "EM_CHARFROMPOS",
2869 "EM_SETIMESTATUS",
2870 "EM_GETIMESTATUS"
2873 static const char * const richedit_messages[] = {
2874 "EM_CANPASTE",
2875 "EM_DISPLAYBAND",
2876 "EM_EXGETSEL",
2877 "EM_EXLIMITTEXT",
2878 "EM_EXLINEFROMCHAR",
2879 "EM_EXSETSEL",
2880 "EM_FINDTEXT",
2881 "EM_FORMATRANGE",
2882 "EM_GETCHARFORMAT",
2883 "EM_GETEVENTMASK",
2884 "EM_GETOLEINTERFACE",
2885 "EM_GETPARAFORMAT",
2886 "EM_GETSELTEXT",
2887 "EM_HIDESELECTION",
2888 "EM_PASTESPECIAL",
2889 "EM_REQUESTRESIZE",
2890 "EM_SELECTIONTYPE",
2891 "EM_SETBKGNDCOLOR",
2892 "EM_SETCHARFORMAT",
2893 "EM_SETEVENTMASK",
2894 "EM_SETOLECALLBACK",
2895 "EM_SETPARAFORMAT",
2896 "EM_SETTARGETDEVICE",
2897 "EM_STREAMIN",
2898 "EM_STREAMOUT",
2899 "EM_GETTEXTRANGE",
2900 "EM_FINDWORDBREAK",
2901 "EM_SETOPTIONS",
2902 "EM_GETOPTIONS",
2903 "EM_FINDTEXTEX",
2904 "EM_GETWORDBREAKPROCEX",
2905 "EM_SETWORDBREAKPROCEX",
2906 "EM_SETUNDOLIMIT",
2907 "EM_UNKNOWN_USER_83",
2908 "EM_REDO",
2909 "EM_CANREDO",
2910 "EM_GETUNDONAME",
2911 "EM_GETREDONAME",
2912 "EM_STOPGROUPTYPING",
2913 "EM_SETTEXTMODE",
2914 "EM_GETTEXTMODE",
2915 "EM_AUTOURLDETECT",
2916 "EM_GETAUTOURLDETECT",
2917 "EM_SETPALETTE",
2918 "EM_GETTEXTEX",
2919 "EM_GETTEXTLENGTHEX",
2920 "EM_SHOWSCROLLBAR",
2921 "EM_SETTEXTEX",
2922 "EM_UNKNOWN_USER_98",
2923 "EM_UNKNOWN_USER_99",
2924 "EM_SETPUNCTUATION",
2925 "EM_GETPUNCTUATION",
2926 "EM_SETWORDWRAPMODE",
2927 "EM_GETWORDWRAPMODE",
2928 "EM_SETIMECOLOR",
2929 "EM_GETIMECOLOR",
2930 "EM_SETIMEOPTIONS",
2931 "EM_GETIMEOPTIONS",
2932 "EM_CONVPOSITION",
2933 "EM_UNKNOWN_USER_109",
2934 "EM_UNKNOWN_USER_110",
2935 "EM_UNKNOWN_USER_111",
2936 "EM_UNKNOWN_USER_112",
2937 "EM_UNKNOWN_USER_113",
2938 "EM_UNKNOWN_USER_114",
2939 "EM_UNKNOWN_USER_115",
2940 "EM_UNKNOWN_USER_116",
2941 "EM_UNKNOWN_USER_117",
2942 "EM_UNKNOWN_USER_118",
2943 "EM_UNKNOWN_USER_119",
2944 "EM_SETLANGOPTIONS",
2945 "EM_GETLANGOPTIONS",
2946 "EM_GETIMECOMPMODE",
2947 "EM_FINDTEXTW",
2948 "EM_FINDTEXTEXW",
2949 "EM_RECONVERSION",
2950 "EM_SETIMEMODEBIAS",
2951 "EM_GETIMEMODEBIAS"
2954 static const char *
2955 get_msg_name(UINT msg)
2957 if (msg >= EM_GETSEL && msg <= EM_CHARFROMPOS)
2958 return edit_messages[msg - EM_GETSEL];
2959 if (msg >= EM_CANPASTE && msg <= EM_GETIMEMODEBIAS)
2960 return richedit_messages[msg - EM_CANPASTE];
2961 return "";
2964 static void ME_LinkNotify(ME_TextEditor *editor, UINT msg, WPARAM wParam, LPARAM lParam)
2966 int x,y;
2967 BOOL isExact;
2968 ME_Cursor cursor; /* The start of the clicked text. */
2970 ENLINK info;
2971 x = (short)LOWORD(lParam);
2972 y = (short)HIWORD(lParam);
2973 ME_CharFromPos(editor, x, y, &cursor, &isExact);
2974 if (!isExact) return;
2976 if (cursor.pRun->member.run.style->fmt.dwMask & CFM_LINK &&
2977 cursor.pRun->member.run.style->fmt.dwEffects & CFE_LINK)
2978 { /* The clicked run has CFE_LINK set */
2979 info.nmhdr.hwndFrom = NULL;
2980 info.nmhdr.idFrom = 0;
2981 info.nmhdr.code = EN_LINK;
2982 info.msg = msg;
2983 info.wParam = wParam;
2984 info.lParam = lParam;
2985 cursor.nOffset = 0;
2986 info.chrg.cpMin = ME_GetCursorOfs(&cursor);
2987 info.chrg.cpMax = info.chrg.cpMin + cursor.pRun->member.run.strText->nLen;
2988 ITextHost_TxNotify(editor->texthost, info.nmhdr.code, &info);
2992 #define UNSUPPORTED_MSG(e) \
2993 case e: \
2994 FIXME(#e ": stub\n"); \
2995 *phresult = S_FALSE; \
2996 return 0;
2998 /* Handle messages for windowless and windoweded richedit controls.
3000 * The LRESULT that is returned is a return value for window procs,
3001 * and the phresult parameter is the COM return code needed by the
3002 * text services interface. */
3003 LRESULT ME_HandleMessage(ME_TextEditor *editor, UINT msg, WPARAM wParam,
3004 LPARAM lParam, BOOL unicode, HRESULT* phresult)
3006 *phresult = S_OK;
3008 switch(msg) {
3010 UNSUPPORTED_MSG(EM_DISPLAYBAND)
3011 UNSUPPORTED_MSG(EM_FINDWORDBREAK)
3012 UNSUPPORTED_MSG(EM_FMTLINES)
3013 UNSUPPORTED_MSG(EM_FORMATRANGE)
3014 UNSUPPORTED_MSG(EM_GETBIDIOPTIONS)
3015 UNSUPPORTED_MSG(EM_GETEDITSTYLE)
3016 UNSUPPORTED_MSG(EM_GETIMECOMPMODE)
3017 UNSUPPORTED_MSG(EM_GETIMESTATUS)
3018 UNSUPPORTED_MSG(EM_SETIMESTATUS)
3019 UNSUPPORTED_MSG(EM_GETLANGOPTIONS)
3020 UNSUPPORTED_MSG(EM_GETREDONAME)
3021 UNSUPPORTED_MSG(EM_GETTYPOGRAPHYOPTIONS)
3022 UNSUPPORTED_MSG(EM_GETUNDONAME)
3023 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX)
3024 UNSUPPORTED_MSG(EM_PASTESPECIAL)
3025 UNSUPPORTED_MSG(EM_SELECTIONTYPE)
3026 UNSUPPORTED_MSG(EM_SETBIDIOPTIONS)
3027 UNSUPPORTED_MSG(EM_SETEDITSTYLE)
3028 UNSUPPORTED_MSG(EM_SETFONTSIZE)
3029 UNSUPPORTED_MSG(EM_SETLANGOPTIONS)
3030 UNSUPPORTED_MSG(EM_SETMARGINS)
3031 UNSUPPORTED_MSG(EM_SETPALETTE)
3032 UNSUPPORTED_MSG(EM_SETTABSTOPS)
3033 UNSUPPORTED_MSG(EM_SETTYPOGRAPHYOPTIONS)
3034 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX)
3036 /* Messages specific to Richedit controls */
3038 case EM_STREAMIN:
3039 return ME_StreamIn(editor, wParam, (EDITSTREAM*)lParam, TRUE);
3040 case EM_STREAMOUT:
3041 return ME_StreamOut(editor, wParam, (EDITSTREAM *)lParam);
3042 case WM_GETDLGCODE:
3044 UINT code = DLGC_WANTCHARS|DLGC_WANTTAB|DLGC_WANTARROWS|DLGC_HASSETSEL;
3045 if (lParam)
3046 editor->bDialogMode = TRUE;
3047 if (editor->styleFlags & ES_MULTILINE)
3048 code |= DLGC_WANTMESSAGE;
3049 return code;
3051 case EM_EMPTYUNDOBUFFER:
3052 ME_EmptyUndoStack(editor);
3053 return 0;
3054 case EM_GETSEL:
3056 /* Note: wParam/lParam can be NULL */
3057 UINT from, to;
3058 PUINT pfrom = wParam ? (PUINT)wParam : &from;
3059 PUINT pto = lParam ? (PUINT)lParam : &to;
3060 ME_GetSelectionOfs(editor, (int *)pfrom, (int *)pto);
3061 if ((*pfrom|*pto) & 0xFFFF0000)
3062 return -1;
3063 return MAKELONG(*pfrom,*pto);
3065 case EM_EXGETSEL:
3067 CHARRANGE *pRange = (CHARRANGE *)lParam;
3068 ME_GetSelectionOfs(editor, &pRange->cpMin, &pRange->cpMax);
3069 TRACE("EM_EXGETSEL = (%d,%d)\n", pRange->cpMin, pRange->cpMax);
3070 return 0;
3072 case EM_SETUNDOLIMIT:
3074 if ((int)wParam < 0)
3075 editor->nUndoLimit = STACK_SIZE_DEFAULT;
3076 else
3077 editor->nUndoLimit = min(wParam, STACK_SIZE_MAX);
3078 /* Setting a max stack size keeps wine from getting killed
3079 for hogging memory. Windows allocates all this memory at once, so
3080 no program would realistically set a value above our maximum. */
3081 return editor->nUndoLimit;
3083 case EM_CANUNDO:
3084 return editor->pUndoStack != NULL;
3085 case EM_CANREDO:
3086 return editor->pRedoStack != NULL;
3087 case WM_UNDO: /* FIXME: actually not the same */
3088 case EM_UNDO:
3089 return ME_Undo(editor);
3090 case EM_REDO:
3091 return ME_Redo(editor);
3092 case EM_GETOPTIONS:
3094 /* these flags are equivalent to the ES_* counterparts */
3095 DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
3096 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN | ECO_SELECTIONBAR;
3097 DWORD settings = editor->styleFlags & mask;
3099 return settings;
3101 case EM_SETOPTIONS:
3103 /* these flags are equivalent to ES_* counterparts, except for
3104 * ECO_AUTOWORDSELECTION that doesn't have an ES_* counterpart,
3105 * but is still stored in editor->styleFlags. */
3106 const DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
3107 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN |
3108 ECO_SELECTIONBAR | ECO_AUTOWORDSELECTION;
3109 DWORD settings = mask & editor->styleFlags;
3110 DWORD oldSettings = settings;
3111 DWORD changedSettings;
3113 switch(wParam)
3115 case ECOOP_SET:
3116 settings = lParam;
3117 break;
3118 case ECOOP_OR:
3119 settings |= lParam;
3120 break;
3121 case ECOOP_AND:
3122 settings &= lParam;
3123 break;
3124 case ECOOP_XOR:
3125 settings ^= lParam;
3127 changedSettings = oldSettings ^ settings;
3129 if (changedSettings) {
3130 editor->styleFlags = (editor->styleFlags & ~mask) | (settings & mask);
3132 if (changedSettings & ECO_SELECTIONBAR)
3134 ITextHost_TxInvalidateRect(editor->texthost, &editor->rcFormat, TRUE);
3135 if (settings & ECO_SELECTIONBAR) {
3136 assert(!editor->selofs);
3137 editor->selofs = SELECTIONBAR_WIDTH;
3138 editor->rcFormat.left += editor->selofs;
3139 } else {
3140 editor->rcFormat.left -= editor->selofs;
3141 editor->selofs = 0;
3143 ME_RewrapRepaint(editor);
3146 if (changedSettings & settings & ECO_VERTICAL)
3147 FIXME("ECO_VERTICAL not implemented yet!\n");
3148 if (changedSettings & settings & ECO_AUTOHSCROLL)
3149 FIXME("ECO_AUTOHSCROLL not implemented yet!\n");
3150 if (changedSettings & settings & ECO_AUTOVSCROLL)
3151 FIXME("ECO_AUTOVSCROLL not implemented yet!\n");
3152 if (changedSettings & settings & ECO_NOHIDESEL)
3153 FIXME("ECO_NOHIDESEL not implemented yet!\n");
3154 if (changedSettings & settings & ECO_WANTRETURN)
3155 FIXME("ECO_WANTRETURN not implemented yet!\n");
3156 if (changedSettings & settings & ECO_AUTOWORDSELECTION)
3157 FIXME("ECO_AUTOWORDSELECTION not implemented yet!\n");
3160 return settings;
3162 case EM_SETSEL:
3164 ME_InvalidateSelection(editor);
3165 ME_SetSelection(editor, wParam, lParam);
3166 ME_InvalidateSelection(editor);
3167 ITextHost_TxShowCaret(editor->texthost, FALSE);
3168 ME_ShowCaret(editor);
3169 ME_SendSelChange(editor);
3170 return 0;
3172 case EM_SETSCROLLPOS:
3174 POINT *point = (POINT *)lParam;
3175 ME_ScrollAbs(editor, point->x, point->y);
3176 return 0;
3178 case EM_AUTOURLDETECT:
3180 if (wParam==1 || wParam ==0)
3182 editor->AutoURLDetect_bEnable = (BOOL)wParam;
3183 return 0;
3185 return E_INVALIDARG;
3187 case EM_GETAUTOURLDETECT:
3189 return editor->AutoURLDetect_bEnable;
3191 case EM_EXSETSEL:
3193 int end;
3194 CHARRANGE range = *(CHARRANGE *)lParam;
3196 TRACE("EM_EXSETSEL (%d,%d)\n", range.cpMin, range.cpMax);
3198 ME_InvalidateSelection(editor);
3199 end = ME_SetSelection(editor, range.cpMin, range.cpMax);
3200 ME_InvalidateSelection(editor);
3201 ITextHost_TxShowCaret(editor->texthost, FALSE);
3202 ME_ShowCaret(editor);
3203 ME_SendSelChange(editor);
3205 return end;
3207 case EM_SHOWSCROLLBAR:
3209 DWORD flags;
3211 switch (wParam)
3213 case SB_HORZ:
3214 flags = WS_HSCROLL;
3215 break;
3216 case SB_VERT:
3217 flags = WS_VSCROLL;
3218 break;
3219 case SB_BOTH:
3220 flags = WS_HSCROLL|WS_VSCROLL;
3221 break;
3222 default:
3223 return 0;
3226 if (lParam) {
3227 editor->styleFlags |= flags;
3228 if (flags & WS_HSCROLL)
3229 ITextHost_TxShowScrollBar(editor->texthost, SB_HORZ,
3230 editor->nTotalWidth > editor->sizeWindow.cx);
3231 if (flags & WS_VSCROLL)
3232 ITextHost_TxShowScrollBar(editor->texthost, SB_VERT,
3233 editor->nTotalLength > editor->sizeWindow.cy);
3234 } else {
3235 editor->styleFlags &= ~flags;
3236 ITextHost_TxShowScrollBar(editor->texthost, wParam, FALSE);
3238 return 0;
3240 case EM_SETTEXTEX:
3242 LPWSTR wszText;
3243 SETTEXTEX *pStruct = (SETTEXTEX *)wParam;
3244 size_t len = 0;
3245 int from, to;
3246 ME_Style *style;
3247 BOOL bRtf, bUnicode, bSelection;
3248 int oldModify = editor->nModifyStep;
3250 if (!pStruct) return 0;
3252 /* If we detect ascii rtf at the start of the string,
3253 * we know it isn't unicode. */
3254 bRtf = (lParam && (!strncmp((char *)lParam, "{\\rtf", 5) ||
3255 !strncmp((char *)lParam, "{\\urtf", 6)));
3256 bUnicode = !bRtf && pStruct->codepage == 1200;
3258 TRACE("EM_SETTEXTEX - %s, flags %d, cp %d\n",
3259 bUnicode ? debugstr_w((LPCWSTR)lParam) : debugstr_a((LPCSTR)lParam),
3260 pStruct->flags, pStruct->codepage);
3262 bSelection = (pStruct->flags & ST_SELECTION) != 0;
3263 if (bSelection) {
3264 int nStartCursor = ME_GetSelectionOfs(editor, &from, &to);
3265 style = ME_GetSelectionInsertStyle(editor);
3266 ME_InternalDeleteText(editor, &editor->pCursors[nStartCursor], to - from, FALSE);
3267 } else {
3268 ME_Cursor start;
3269 ME_SetCursorToStart(editor, &start);
3270 ME_InternalDeleteText(editor, &start, ME_GetTextLength(editor), FALSE);
3271 style = editor->pBuffer->pDefaultStyle;
3274 if (bRtf) {
3275 ME_StreamInRTFString(editor, bSelection, (char *)lParam);
3276 if (bSelection) {
3277 /* FIXME: The length returned doesn't include the rtf control
3278 * characters, only the actual text. */
3279 len = lParam ? strlen((char *)lParam) : 0;
3281 } else {
3282 /* FIXME: make use of pStruct->codepage in the to unicode translation */
3283 wszText = lParam ? ME_ToUnicode(bUnicode, (void *)lParam) : NULL;
3284 len = wszText ? lstrlenW(wszText) : 0;
3285 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
3286 ME_EndToUnicode(bUnicode, wszText);
3289 if (bSelection) {
3290 ME_ReleaseStyle(style);
3291 ME_UpdateSelectionLinkAttribute(editor);
3292 } else {
3293 ME_Cursor cursor;
3294 len = 1;
3295 ME_SetCursorToStart(editor, &cursor);
3296 ME_UpdateLinkAttribute(editor, &cursor, INT_MAX);
3298 ME_CommitUndo(editor);
3299 if (!(pStruct->flags & ST_KEEPUNDO))
3301 editor->nModifyStep = oldModify;
3302 ME_EmptyUndoStack(editor);
3304 ME_UpdateRepaint(editor);
3305 return len;
3307 case EM_SETBKGNDCOLOR:
3309 LRESULT lColor;
3310 if (editor->rgbBackColor != -1) {
3311 DeleteObject(editor->hbrBackground);
3312 lColor = editor->rgbBackColor;
3314 else lColor = ITextHost_TxGetSysColor(editor->texthost, COLOR_WINDOW);
3316 if (wParam)
3318 editor->rgbBackColor = -1;
3319 editor->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
3321 else
3323 editor->rgbBackColor = lParam;
3324 editor->hbrBackground = CreateSolidBrush(editor->rgbBackColor);
3326 ITextHost_TxInvalidateRect(editor->texthost, NULL, TRUE);
3327 ITextHost_TxViewChange(editor->texthost, TRUE);
3328 return lColor;
3330 case EM_GETMODIFY:
3331 return editor->nModifyStep == 0 ? 0 : -1;
3332 case EM_SETMODIFY:
3334 if (wParam)
3335 editor->nModifyStep = 1;
3336 else
3337 editor->nModifyStep = 0;
3339 return 0;
3341 case EM_SETREADONLY:
3343 if (wParam)
3344 editor->styleFlags |= ES_READONLY;
3345 else
3346 editor->styleFlags &= ~ES_READONLY;
3347 return 0;
3349 case EM_SETEVENTMASK:
3351 DWORD nOldMask = editor->nEventMask;
3353 editor->nEventMask = lParam;
3354 return nOldMask;
3356 case EM_GETEVENTMASK:
3357 return editor->nEventMask;
3358 case EM_SETCHARFORMAT:
3360 CHARFORMAT2W buf, *p;
3361 BOOL bRepaint = TRUE;
3362 p = ME_ToCF2W(&buf, (CHARFORMAT2W *)lParam);
3363 if (p == NULL) return 0;
3364 if (!wParam)
3365 ME_SetDefaultCharFormat(editor, p);
3366 else if (wParam == (SCF_WORD | SCF_SELECTION)) {
3367 FIXME("EM_SETCHARFORMAT: word selection not supported\n");
3368 return 0;
3369 } else if (wParam == SCF_ALL) {
3370 if (editor->mode & TM_PLAINTEXT)
3371 ME_SetDefaultCharFormat(editor, p);
3372 else {
3373 ME_Cursor start;
3374 ME_SetCursorToStart(editor, &start);
3375 ME_SetCharFormat(editor, &start, NULL, p);
3376 editor->nModifyStep = 1;
3378 } else if (editor->mode & TM_PLAINTEXT) {
3379 return 0;
3380 } else {
3381 bRepaint = ME_IsSelection(editor);
3382 ME_SetSelectionCharFormat(editor, p);
3383 if (bRepaint) editor->nModifyStep = 1;
3385 ME_CommitUndo(editor);
3386 if (bRepaint)
3388 ME_WrapMarkedParagraphs(editor);
3389 ME_UpdateScrollBar(editor);
3390 ME_Repaint(editor);
3392 return 1;
3394 case EM_GETCHARFORMAT:
3396 CHARFORMAT2W tmp, *dst = (CHARFORMAT2W *)lParam;
3397 if (dst->cbSize != sizeof(CHARFORMATA) &&
3398 dst->cbSize != sizeof(CHARFORMATW) &&
3399 dst->cbSize != sizeof(CHARFORMAT2A) &&
3400 dst->cbSize != sizeof(CHARFORMAT2W))
3401 return 0;
3402 tmp.cbSize = sizeof(tmp);
3403 if (!wParam)
3404 ME_GetDefaultCharFormat(editor, &tmp);
3405 else
3406 ME_GetSelectionCharFormat(editor, &tmp);
3407 ME_CopyToCFAny(dst, &tmp);
3408 return tmp.dwMask;
3410 case EM_SETPARAFORMAT:
3412 BOOL result = ME_SetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
3413 ME_WrapMarkedParagraphs(editor);
3414 ME_UpdateScrollBar(editor);
3415 ME_Repaint(editor);
3416 ME_CommitUndo(editor);
3417 return result;
3419 case EM_GETPARAFORMAT:
3420 ME_GetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
3421 return ((PARAFORMAT2 *)lParam)->dwMask;
3422 case EM_GETFIRSTVISIBLELINE:
3424 ME_DisplayItem *p = editor->pBuffer->pFirst;
3425 int y = editor->vert_si.nPos;
3426 int ypara = 0;
3427 int count = 0;
3428 int ystart, yend;
3429 while(p) {
3430 p = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
3431 if (p->type == diTextEnd)
3432 break;
3433 if (p->type == diParagraph) {
3434 ypara = p->member.para.pt.y;
3435 continue;
3437 ystart = ypara + p->member.row.pt.y;
3438 yend = ystart + p->member.row.nHeight;
3439 if (y < yend) {
3440 break;
3442 count++;
3444 return count;
3446 case EM_HIDESELECTION:
3448 editor->bHideSelection = (wParam != 0);
3449 ME_InvalidateSelection(editor);
3450 return 0;
3452 case EM_LINESCROLL:
3454 if (!(editor->styleFlags & ES_MULTILINE))
3455 return FALSE;
3456 ME_ScrollDown(editor, lParam * 8); /* FIXME follow the original */
3457 return TRUE;
3459 case WM_CLEAR:
3461 int from, to;
3462 int nStartCursor = ME_GetSelectionOfs(editor, &from, &to);
3463 ME_InternalDeleteText(editor, &editor->pCursors[nStartCursor], to-from, FALSE);
3464 ME_CommitUndo(editor);
3465 ME_UpdateRepaint(editor);
3466 return 0;
3468 case EM_REPLACESEL:
3470 int from, to, nStartCursor;
3471 ME_Style *style;
3472 LPWSTR wszText = lParam ? ME_ToUnicode(unicode, (void *)lParam) : NULL;
3473 size_t len = wszText ? lstrlenW(wszText) : 0;
3474 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText));
3476 nStartCursor = ME_GetSelectionOfs(editor, &from, &to);
3477 style = ME_GetSelectionInsertStyle(editor);
3478 ME_InternalDeleteText(editor, &editor->pCursors[nStartCursor], to-from, FALSE);
3479 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
3480 ME_ReleaseStyle(style);
3481 /* drop temporary style if line end */
3483 * FIXME question: does abc\n mean: put abc,
3484 * clear temp style, put \n? (would require a change)
3486 if (len>0 && wszText[len-1] == '\n')
3487 ME_ClearTempStyle(editor);
3488 ME_EndToUnicode(unicode, wszText);
3489 ME_CommitUndo(editor);
3490 ME_UpdateSelectionLinkAttribute(editor);
3491 if (!wParam)
3492 ME_EmptyUndoStack(editor);
3493 ME_UpdateRepaint(editor);
3494 return len;
3496 case EM_SCROLLCARET:
3497 ME_EnsureVisible(editor, &editor->pCursors[0]);
3498 return 0;
3499 case WM_SETFONT:
3501 LOGFONTW lf;
3502 CHARFORMAT2W fmt;
3503 HDC hDC;
3504 BOOL bRepaint = LOWORD(lParam);
3506 if (!wParam)
3507 wParam = (WPARAM)GetStockObject(SYSTEM_FONT);
3508 GetObjectW((HGDIOBJ)wParam, sizeof(LOGFONTW), &lf);
3509 hDC = ITextHost_TxGetDC(editor->texthost);
3510 ME_CharFormatFromLogFont(hDC, &lf, &fmt);
3511 ITextHost_TxReleaseDC(editor->texthost, hDC);
3512 if (editor->mode & TM_RICHTEXT) {
3513 ME_Cursor start;
3514 ME_SetCursorToStart(editor, &start);
3515 ME_SetCharFormat(editor, &start, NULL, &fmt);
3517 ME_SetDefaultCharFormat(editor, &fmt);
3519 ME_CommitUndo(editor);
3520 ME_MarkAllForWrapping(editor);
3521 ME_WrapMarkedParagraphs(editor);
3522 ME_UpdateScrollBar(editor);
3523 if (bRepaint)
3524 ME_Repaint(editor);
3525 return 0;
3527 case WM_SETTEXT:
3529 ME_Cursor cursor;
3530 ME_SetCursorToStart(editor, &cursor);
3531 ME_InternalDeleteText(editor, &cursor, ME_GetTextLength(editor), FALSE);
3532 if (lParam)
3534 TRACE("WM_SETTEXT lParam==%lx\n",lParam);
3535 if (!strncmp((char *)lParam, "{\\rtf", 5) ||
3536 !strncmp((char *)lParam, "{\\urtf", 6))
3538 /* Undocumented: WM_SETTEXT supports RTF text */
3539 ME_StreamInRTFString(editor, 0, (char *)lParam);
3541 else
3543 LPWSTR wszText = ME_ToUnicode(unicode, (void *)lParam);
3544 TRACE("WM_SETTEXT - %s\n", debugstr_w(wszText)); /* debugstr_w() */
3545 if (lstrlenW(wszText) > 0)
3547 int len = -1;
3549 /* uses default style! */
3550 if (!(editor->styleFlags & ES_MULTILINE))
3552 WCHAR * p;
3554 p = wszText;
3555 while (*p != '\0' && *p != '\r' && *p != '\n') p++;
3556 len = p - wszText;
3558 ME_InsertTextFromCursor(editor, 0, wszText, len, editor->pBuffer->pDefaultStyle);
3560 ME_EndToUnicode(unicode, wszText);
3563 else
3564 TRACE("WM_SETTEXT - NULL\n");
3565 ME_SetCursorToStart(editor, &cursor);
3566 ME_UpdateLinkAttribute(editor, &cursor, INT_MAX);
3567 ME_SetSelection(editor, 0, 0);
3568 editor->nModifyStep = 0;
3569 ME_CommitUndo(editor);
3570 ME_EmptyUndoStack(editor);
3571 ME_UpdateRepaint(editor);
3572 return 1;
3574 case EM_CANPASTE:
3576 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
3577 if (IsClipboardFormatAvailable(nRTFFormat))
3578 return TRUE;
3579 if (IsClipboardFormatAvailable(CF_UNICODETEXT))
3580 return TRUE;
3581 return FALSE;
3583 case WM_PASTE:
3584 ME_Paste(editor);
3585 return 0;
3586 case WM_CUT:
3587 case WM_COPY:
3589 int nFrom, nTo, nStartCur = ME_GetSelectionOfs(editor, &nFrom, &nTo);
3590 int nChars = nTo - nFrom;
3591 ME_Cursor *selStart = &editor->pCursors[nStartCur];
3593 if (ME_Copy(editor, selStart, nChars) && msg == WM_CUT)
3595 ME_InternalDeleteText(editor, selStart, nChars, FALSE);
3596 ME_CommitUndo(editor);
3597 ME_UpdateRepaint(editor);
3599 return 0;
3601 case WM_GETTEXTLENGTH:
3603 GETTEXTLENGTHEX how;
3605 /* CR/LF conversion required in 2.0 mode, verbatim in 1.0 mode */
3606 how.flags = GTL_CLOSE | (editor->bEmulateVersion10 ? 0 : GTL_USECRLF) | GTL_NUMCHARS;
3607 how.codepage = unicode ? 1200 : CP_ACP;
3608 return ME_GetTextLengthEx(editor, &how);
3610 case EM_GETTEXTLENGTHEX:
3611 return ME_GetTextLengthEx(editor, (GETTEXTLENGTHEX *)wParam);
3612 case WM_GETTEXT:
3614 GETTEXTEX ex;
3615 ex.cb = wParam * (unicode ? sizeof(WCHAR) : sizeof(CHAR));
3616 ex.flags = GT_USECRLF;
3617 ex.codepage = unicode ? 1200 : CP_ACP;
3618 ex.lpDefaultChar = NULL;
3619 ex.lpUsedDefChar = NULL;
3620 return ME_GetTextEx(editor, &ex, lParam);
3622 case EM_GETTEXTEX:
3623 return ME_GetTextEx(editor, (GETTEXTEX*)wParam, lParam);
3624 case EM_GETSELTEXT:
3626 int nFrom, nTo, nStartCur = ME_GetSelectionOfs(editor, &nFrom, &nTo);
3627 ME_Cursor *from = &editor->pCursors[nStartCur];
3628 return ME_GetTextRange(editor, (WCHAR *)lParam, from,
3629 nTo - nFrom, unicode);
3631 case EM_GETSCROLLPOS:
3633 POINT *point = (POINT *)lParam;
3634 point->x = editor->horz_si.nPos;
3635 point->y = editor->vert_si.nPos;
3636 /* 16-bit scaled value is returned as stored in scrollinfo */
3637 if (editor->horz_si.nMax > 0xffff)
3638 point->x = MulDiv(point->x, 0xffff, editor->horz_si.nMax);
3639 if (editor->vert_si.nMax > 0xffff)
3640 point->y = MulDiv(point->y, 0xffff, editor->vert_si.nMax);
3641 return 1;
3643 case EM_GETTEXTRANGE:
3645 TEXTRANGEW *rng = (TEXTRANGEW *)lParam;
3646 ME_Cursor start;
3647 int nStart = rng->chrg.cpMin;
3648 int nEnd = rng->chrg.cpMax;
3649 int textlength = ME_GetTextLength(editor);
3651 TRACE("EM_GETTEXTRANGE min=%d max=%d unicode=%d textlength=%d\n",
3652 rng->chrg.cpMin, rng->chrg.cpMax, unicode, textlength);
3653 if (nStart < 0) return 0;
3654 if ((nStart == 0 && nEnd == -1) || nEnd > textlength)
3655 nEnd = textlength;
3656 if (nStart >= nEnd) return 0;
3658 ME_CursorFromCharOfs(editor, nStart, &start);
3659 return ME_GetTextRange(editor, rng->lpstrText, &start, nEnd - nStart, unicode);
3661 case EM_GETLINE:
3663 ME_DisplayItem *run;
3664 const unsigned int nMaxChars = *(WORD *) lParam;
3665 unsigned int nCharsLeft = nMaxChars;
3666 char *dest = (char *) lParam;
3667 BOOL wroteNull = FALSE;
3669 TRACE("EM_GETLINE: row=%d, nMaxChars=%d (%s)\n", (int) wParam, nMaxChars,
3670 unicode ? "Unicode" : "Ansi");
3672 run = ME_FindRowWithNumber(editor, wParam);
3673 if (run == NULL)
3674 return 0;
3676 while (nCharsLeft && (run = ME_FindItemFwd(run, diRunOrStartRow))
3677 && run->type == diRun)
3679 unsigned int nCopy;
3680 ME_String *strText;
3682 strText = run->member.run.strText;
3683 nCopy = min(nCharsLeft, strText->nLen);
3685 if (unicode)
3686 memcpy(dest, strText->szData, nCopy * sizeof(WCHAR));
3687 else
3688 nCopy = WideCharToMultiByte(CP_ACP, 0, strText->szData, nCopy, dest,
3689 nCharsLeft, NULL, NULL);
3690 dest += nCopy * (unicode ? sizeof(WCHAR) : 1);
3691 nCharsLeft -= nCopy;
3694 /* append line termination, space allowing */
3695 if (nCharsLeft > 0)
3697 if (unicode)
3698 *((WCHAR *)dest) = '\0';
3699 else
3700 *dest = '\0';
3701 nCharsLeft--;
3702 wroteNull = TRUE;
3705 TRACE("EM_GETLINE: got %u characters\n", nMaxChars - nCharsLeft);
3706 return nMaxChars - nCharsLeft - (wroteNull ? 1 : 0);
3708 case EM_GETLINECOUNT:
3710 ME_DisplayItem *item = editor->pBuffer->pFirst->next;
3711 int nRows = 0;
3713 ME_DisplayItem *prev_para = NULL, *last_para = NULL;
3715 while (item != editor->pBuffer->pLast)
3717 assert(item->type == diParagraph);
3718 prev_para = ME_FindItemBack(item, diRun);
3719 if (prev_para) {
3720 assert(prev_para->member.run.nFlags & MERF_ENDPARA);
3722 nRows += item->member.para.nRows;
3723 item = item->member.para.next_para;
3725 last_para = ME_FindItemBack(item, diRun);
3726 assert(last_para);
3727 assert(last_para->member.run.nFlags & MERF_ENDPARA);
3728 if (editor->bEmulateVersion10 && prev_para &&
3729 last_para->member.run.nCharOfs == 0 &&
3730 prev_para->member.run.strText->nLen == 1 &&
3731 prev_para->member.run.strText->szData[0] == '\r')
3733 /* In 1.0 emulation, the last solitary \r at the very end of the text
3734 (if one exists) is NOT a line break.
3735 FIXME: this is an ugly hack. This should have a more regular model. */
3736 nRows--;
3739 TRACE("EM_GETLINECOUNT: nRows==%d\n", nRows);
3740 return max(1, nRows);
3742 case EM_LINEFROMCHAR:
3744 if (wParam == -1)
3745 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(&editor->pCursors[1]));
3746 else
3747 return ME_RowNumberFromCharOfs(editor, wParam);
3749 case EM_EXLINEFROMCHAR:
3751 if (lParam == -1)
3752 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(&editor->pCursors[1]));
3753 else
3754 return ME_RowNumberFromCharOfs(editor, lParam);
3756 case EM_LINEINDEX:
3758 ME_DisplayItem *item, *para;
3759 int nCharOfs;
3761 if (wParam == -1)
3762 item = ME_FindItemBack(editor->pCursors[0].pRun, diStartRow);
3763 else
3764 item = ME_FindRowWithNumber(editor, wParam);
3765 if (!item)
3766 return -1;
3767 para = ME_GetParagraph(item);
3768 item = ME_FindItemFwd(item, diRun);
3769 nCharOfs = para->member.para.nCharOfs + item->member.run.nCharOfs;
3770 TRACE("EM_LINEINDEX: nCharOfs==%d\n", nCharOfs);
3771 return nCharOfs;
3773 case EM_LINELENGTH:
3775 ME_DisplayItem *item, *item_end;
3776 int nChars = 0, nThisLineOfs = 0, nNextLineOfs = 0;
3777 ME_DisplayItem *para, *run;
3779 if (wParam > ME_GetTextLength(editor))
3780 return 0;
3781 if (wParam == -1)
3783 FIXME("EM_LINELENGTH: returning number of unselected characters on lines with selection unsupported.\n");
3784 return 0;
3786 ME_RunOfsFromCharOfs(editor, wParam, &para, &run, NULL);
3787 item = ME_RowStart(run);
3788 nThisLineOfs = ME_CharOfsFromRunOfs(editor, para, ME_FindItemFwd(item, diRun), 0);
3789 item_end = ME_FindItemFwd(item, diStartRowOrParagraphOrEnd);
3790 if (item_end->type == diStartRow) {
3791 nNextLineOfs = ME_CharOfsFromRunOfs(editor, para, ME_FindItemFwd(item_end, diRun), 0);
3792 } else {
3793 ME_DisplayItem *endRun = ME_FindItemBack(item_end, diRun);
3794 assert(endRun && endRun->member.run.nFlags & MERF_ENDPARA);
3795 nNextLineOfs = item_end->member.para.nCharOfs - endRun->member.run.strText->nLen;
3797 nChars = nNextLineOfs - nThisLineOfs;
3798 TRACE("EM_LINELENGTH(%ld)==%d\n",wParam, nChars);
3799 return nChars;
3801 case EM_EXLIMITTEXT:
3803 if ((int)lParam < 0)
3804 return 0;
3805 if (lParam == 0)
3806 editor->nTextLimit = 65536;
3807 else
3808 editor->nTextLimit = (int) lParam;
3809 return 0;
3811 case EM_LIMITTEXT:
3813 if (wParam == 0)
3814 editor->nTextLimit = 65536;
3815 else
3816 editor->nTextLimit = (int) wParam;
3817 return 0;
3819 case EM_GETLIMITTEXT:
3821 return editor->nTextLimit;
3823 case EM_FINDTEXT:
3825 FINDTEXTA *ft = (FINDTEXTA *)lParam;
3826 int nChars = MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, NULL, 0);
3827 WCHAR *tmp;
3828 LRESULT r;
3830 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
3831 MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, tmp, nChars);
3832 r = ME_FindText(editor, wParam, &ft->chrg, tmp, NULL);
3833 FREE_OBJ( tmp );
3834 return r;
3836 case EM_FINDTEXTEX:
3838 FINDTEXTEXA *ex = (FINDTEXTEXA *)lParam;
3839 int nChars = MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, NULL, 0);
3840 WCHAR *tmp;
3841 LRESULT r;
3843 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
3844 MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, tmp, nChars);
3845 r = ME_FindText(editor, wParam, &ex->chrg, tmp, &ex->chrgText);
3846 FREE_OBJ( tmp );
3847 return r;
3849 case EM_FINDTEXTW:
3851 FINDTEXTW *ft = (FINDTEXTW *)lParam;
3852 return ME_FindText(editor, wParam, &ft->chrg, ft->lpstrText, NULL);
3854 case EM_FINDTEXTEXW:
3856 FINDTEXTEXW *ex = (FINDTEXTEXW *)lParam;
3857 return ME_FindText(editor, wParam, &ex->chrg, ex->lpstrText, &ex->chrgText);
3859 case EM_GETZOOM:
3860 if (!wParam || !lParam)
3861 return FALSE;
3862 *(int *)wParam = editor->nZoomNumerator;
3863 *(int *)lParam = editor->nZoomDenominator;
3864 return TRUE;
3865 case EM_SETZOOM:
3866 return ME_SetZoom(editor, wParam, lParam);
3867 case EM_CHARFROMPOS:
3869 ME_Cursor cursor;
3870 if (ME_CharFromPos(editor, ((POINTL *)lParam)->x, ((POINTL *)lParam)->y,
3871 &cursor, NULL))
3872 return ME_GetCursorOfs(&cursor);
3873 else
3874 return -1;
3876 case EM_POSFROMCHAR:
3878 ME_DisplayItem *pPara, *pRun;
3879 int nCharOfs, nOffset, nLength;
3880 POINTL pt = {0,0};
3882 nCharOfs = wParam;
3883 /* detect which API version we're dealing with */
3884 if (wParam >= 0x40000)
3885 nCharOfs = lParam;
3886 nLength = ME_GetTextLength(editor);
3887 nCharOfs = min(nCharOfs, nLength);
3888 nCharOfs = max(nCharOfs, 0);
3890 ME_RunOfsFromCharOfs(editor, nCharOfs, &pPara, &pRun, &nOffset);
3891 assert(pRun->type == diRun);
3892 pt.y = pRun->member.run.pt.y;
3893 pt.x = pRun->member.run.pt.x + ME_PointFromChar(editor, &pRun->member.run, nOffset);
3894 pt.y += pPara->member.para.pt.y + editor->rcFormat.top;
3895 pt.x += editor->rcFormat.left;
3897 pt.x -= editor->horz_si.nPos;
3898 pt.y -= editor->vert_si.nPos;
3900 if (wParam >= 0x40000) {
3901 *(POINTL *)wParam = pt;
3903 return (wParam >= 0x40000) ? 0 : MAKELONG( pt.x, pt.y );
3905 case WM_CREATE:
3907 INT max;
3909 ME_SetDefaultFormatRect(editor);
3911 max = (editor->styleFlags & ES_DISABLENOSCROLL) ? 1 : 0;
3912 if (~editor->styleFlags & ES_DISABLENOSCROLL || editor->styleFlags & WS_VSCROLL)
3913 ITextHost_TxSetScrollRange(editor->texthost, SB_VERT, 0, max, TRUE);
3915 if (~editor->styleFlags & ES_DISABLENOSCROLL || editor->styleFlags & WS_HSCROLL)
3916 ITextHost_TxSetScrollRange(editor->texthost, SB_HORZ, 0, max, TRUE);
3918 if (editor->styleFlags & ES_DISABLENOSCROLL)
3920 if (editor->styleFlags & WS_VSCROLL)
3922 ITextHost_TxEnableScrollBar(editor->texthost, SB_VERT, ESB_DISABLE_BOTH);
3923 ITextHost_TxShowScrollBar(editor->texthost, SB_VERT, TRUE);
3925 if (editor->styleFlags & WS_HSCROLL)
3927 ITextHost_TxEnableScrollBar(editor->texthost, SB_HORZ, ESB_DISABLE_BOTH);
3928 ITextHost_TxShowScrollBar(editor->texthost, SB_HORZ, TRUE);
3932 ME_CommitUndo(editor);
3933 ME_WrapMarkedParagraphs(editor);
3934 ME_MoveCaret(editor);
3935 return 0;
3937 case WM_DESTROY:
3938 ME_DestroyEditor(editor);
3939 return 0;
3940 case WM_SETCURSOR:
3942 return ME_SetCursor(editor);
3944 case WM_LBUTTONDBLCLK:
3945 case WM_LBUTTONDOWN:
3947 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
3948 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
3949 !ME_FilterEvent(editor, msg, &wParam, &lParam))
3950 return 0;
3951 ITextHost_TxSetFocus(editor->texthost);
3952 ME_LButtonDown(editor, (short)LOWORD(lParam), (short)HIWORD(lParam),
3953 ME_CalculateClickCount(editor, msg, wParam, lParam));
3954 ITextHost_TxSetCapture(editor->texthost, TRUE);
3955 editor->bMouseCaptured = TRUE;
3956 ME_LinkNotify(editor,msg,wParam,lParam);
3957 if (!ME_SetCursor(editor)) goto do_default;
3958 break;
3960 case WM_MOUSEMOVE:
3961 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
3962 !ME_FilterEvent(editor, msg, &wParam, &lParam))
3963 return 0;
3964 if (editor->bMouseCaptured)
3965 ME_MouseMove(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
3966 ME_LinkNotify(editor,msg,wParam,lParam);
3967 /* Set cursor if mouse is captured, since WM_SETCURSOR won't be received. */
3968 if (editor->bMouseCaptured)
3969 ME_SetCursor(editor);
3970 break;
3971 case WM_LBUTTONUP:
3972 if (editor->bMouseCaptured) {
3973 ITextHost_TxSetCapture(editor->texthost, FALSE);
3974 editor->bMouseCaptured = FALSE;
3976 if (editor->nSelectionType == stDocument)
3977 editor->nSelectionType = stPosition;
3978 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
3979 !ME_FilterEvent(editor, msg, &wParam, &lParam))
3980 return 0;
3981 else
3983 ME_SetCursor(editor);
3984 ME_LinkNotify(editor,msg,wParam,lParam);
3986 break;
3987 case WM_RBUTTONUP:
3988 case WM_RBUTTONDOWN:
3989 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
3990 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
3991 !ME_FilterEvent(editor, msg, &wParam, &lParam))
3992 return 0;
3993 goto do_default;
3994 case WM_CONTEXTMENU:
3995 if (!ME_ShowContextMenu(editor, (short)LOWORD(lParam), (short)HIWORD(lParam)))
3996 goto do_default;
3997 break;
3998 case WM_SETFOCUS:
3999 editor->bHaveFocus = TRUE;
4000 ME_ShowCaret(editor);
4001 ME_SendOldNotify(editor, EN_SETFOCUS);
4002 return 0;
4003 case WM_KILLFOCUS:
4004 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
4005 editor->bHaveFocus = FALSE;
4006 ME_HideCaret(editor);
4007 ME_SendOldNotify(editor, EN_KILLFOCUS);
4008 return 0;
4009 case WM_COMMAND:
4010 TRACE("editor wnd command = %d\n", LOWORD(wParam));
4011 return 0;
4012 case WM_KEYUP:
4013 if ((editor->nEventMask & ENM_KEYEVENTS) &&
4014 !ME_FilterEvent(editor, msg, &wParam, &lParam))
4015 return 0;
4016 goto do_default;
4017 case WM_KEYDOWN:
4018 if ((editor->nEventMask & ENM_KEYEVENTS) &&
4019 !ME_FilterEvent(editor, msg, &wParam, &lParam))
4020 return 0;
4021 if (ME_KeyDown(editor, LOWORD(wParam)))
4022 return 0;
4023 goto do_default;
4024 case WM_CHAR:
4025 return ME_Char(editor, wParam, lParam, unicode);
4026 case WM_UNICHAR:
4027 if (unicode)
4029 if(wParam == UNICODE_NOCHAR) return TRUE;
4030 if(wParam <= 0x000fffff)
4032 if(wParam > 0xffff) /* convert to surrogates */
4034 wParam -= 0x10000;
4035 ME_Char(editor, (wParam >> 10) + 0xd800, 0, TRUE);
4036 ME_Char(editor, (wParam & 0x03ff) + 0xdc00, 0, TRUE);
4037 } else {
4038 ME_Char(editor, wParam, 0, TRUE);
4041 return 0;
4043 break;
4044 case EM_STOPGROUPTYPING:
4045 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
4046 return 0;
4047 case WM_HSCROLL:
4049 const int scrollUnit = 7;
4051 switch(LOWORD(wParam))
4053 case SB_LEFT:
4054 ME_ScrollAbs(editor, 0, 0);
4055 break;
4056 case SB_RIGHT:
4057 ME_ScrollAbs(editor,
4058 editor->horz_si.nMax - (int)editor->horz_si.nPage,
4059 editor->vert_si.nMax - (int)editor->vert_si.nPage);
4060 break;
4061 case SB_LINELEFT:
4062 ME_ScrollLeft(editor, scrollUnit);
4063 break;
4064 case SB_LINERIGHT:
4065 ME_ScrollRight(editor, scrollUnit);
4066 break;
4067 case SB_PAGELEFT:
4068 ME_ScrollLeft(editor, editor->sizeWindow.cx);
4069 break;
4070 case SB_PAGERIGHT:
4071 ME_ScrollRight(editor, editor->sizeWindow.cx);
4072 break;
4073 case SB_THUMBTRACK:
4074 case SB_THUMBPOSITION:
4076 int pos = HIWORD(wParam);
4077 if (editor->horz_si.nMax > 0xffff)
4078 pos = MulDiv(pos, editor->horz_si.nMax, 0xffff);
4079 ME_HScrollAbs(editor, pos);
4080 break;
4083 break;
4085 case EM_SCROLL: /* fall through */
4086 case WM_VSCROLL:
4088 int origNPos;
4089 int lineHeight;
4091 origNPos = editor->vert_si.nPos;
4092 lineHeight = 24;
4094 if (editor->pBuffer && editor->pBuffer->pDefaultStyle)
4095 lineHeight = editor->pBuffer->pDefaultStyle->tm.tmHeight;
4096 if (lineHeight <= 0) lineHeight = 24;
4098 switch(LOWORD(wParam))
4100 case SB_TOP:
4101 ME_ScrollAbs(editor, 0, 0);
4102 break;
4103 case SB_BOTTOM:
4104 ME_ScrollAbs(editor,
4105 editor->horz_si.nMax - (int)editor->horz_si.nPage,
4106 editor->vert_si.nMax - (int)editor->vert_si.nPage);
4107 break;
4108 case SB_LINEUP:
4109 ME_ScrollUp(editor,lineHeight);
4110 break;
4111 case SB_LINEDOWN:
4112 ME_ScrollDown(editor,lineHeight);
4113 break;
4114 case SB_PAGEUP:
4115 ME_ScrollUp(editor,editor->sizeWindow.cy);
4116 break;
4117 case SB_PAGEDOWN:
4118 ME_ScrollDown(editor,editor->sizeWindow.cy);
4119 break;
4120 case SB_THUMBTRACK:
4121 case SB_THUMBPOSITION:
4123 int pos = HIWORD(wParam);
4124 if (editor->vert_si.nMax > 0xffff)
4125 pos = MulDiv(pos, editor->vert_si.nMax, 0xffff);
4126 ME_VScrollAbs(editor, pos);
4127 break;
4130 if (msg == EM_SCROLL)
4131 return 0x00010000 | (((editor->vert_si.nPos - origNPos)/lineHeight) & 0xffff);
4132 break;
4134 case WM_MOUSEWHEEL:
4136 int gcWheelDelta;
4137 UINT pulScrollLines;
4138 BOOL ctrl_is_down;
4140 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
4141 !ME_FilterEvent(editor, msg, &wParam, &lParam))
4142 return 0;
4144 ctrl_is_down = GetKeyState(VK_CONTROL) & 0x8000;
4146 gcWheelDelta = GET_WHEEL_DELTA_WPARAM(wParam);
4148 if (abs(gcWheelDelta) >= WHEEL_DELTA)
4150 if (ctrl_is_down) {
4151 int numerator;
4152 if (!editor->nZoomNumerator || !editor->nZoomDenominator)
4154 numerator = 100;
4155 } else {
4156 numerator = editor->nZoomNumerator * 100 / editor->nZoomDenominator;
4158 numerator = numerator + (gcWheelDelta / WHEEL_DELTA) * 10;
4159 if (numerator >= 10 && numerator <= 500)
4160 ME_SetZoom(editor, numerator, 100);
4161 } else {
4162 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
4163 /* FIXME follow the original */
4164 if (pulScrollLines)
4165 ME_ScrollDown(editor,pulScrollLines * (-gcWheelDelta / WHEEL_DELTA) * 8);
4168 break;
4170 case EM_GETRECT:
4172 *((RECT *)lParam) = editor->rcFormat;
4173 if (editor->bDefaultFormatRect)
4174 ((RECT *)lParam)->left -= editor->selofs;
4175 return 0;
4177 case EM_SETRECT:
4178 case EM_SETRECTNP:
4180 if (lParam)
4182 int border = 0;
4183 RECT clientRect;
4184 RECT *rc = (RECT *)lParam;
4186 border = editor->exStyleFlags & WS_EX_CLIENTEDGE ? 1 : 0;
4187 ITextHost_TxGetClientRect(editor->texthost, &clientRect);
4188 if (wParam == 0)
4190 editor->rcFormat.top = max(0, rc->top - border);
4191 editor->rcFormat.left = max(0, rc->left - border);
4192 editor->rcFormat.bottom = min(clientRect.bottom, rc->bottom);
4193 editor->rcFormat.right = min(clientRect.right, rc->right + border);
4194 } else if (wParam == 1) {
4195 /* MSDN incorrectly says a wParam value of 1 causes the
4196 * lParam rect to be used as a relative offset,
4197 * however, the tests show it just prevents min/max bound
4198 * checking. */
4199 editor->rcFormat.top = rc->top - border;
4200 editor->rcFormat.left = rc->left - border;
4201 editor->rcFormat.bottom = rc->bottom;
4202 editor->rcFormat.right = rc->right + border;
4203 } else {
4204 return 0;
4206 editor->bDefaultFormatRect = FALSE;
4208 else
4210 ME_SetDefaultFormatRect(editor);
4211 editor->bDefaultFormatRect = TRUE;
4213 ME_MarkAllForWrapping(editor);
4214 ME_WrapMarkedParagraphs(editor);
4215 ME_UpdateScrollBar(editor);
4216 if (msg != EM_SETRECTNP)
4217 ME_Repaint(editor);
4218 return 0;
4220 case EM_REQUESTRESIZE:
4221 ME_SendRequestResize(editor, TRUE);
4222 return 0;
4223 case WM_SETREDRAW:
4224 goto do_default;
4225 case WM_SIZE:
4227 RECT clientRect;
4229 ITextHost_TxGetClientRect(editor->texthost, &clientRect);
4230 if (editor->bDefaultFormatRect) {
4231 ME_SetDefaultFormatRect(editor);
4232 } else {
4233 editor->rcFormat.right += clientRect.right - editor->prevClientRect.right;
4234 editor->rcFormat.bottom += clientRect.bottom - editor->prevClientRect.bottom;
4236 editor->prevClientRect = clientRect;
4237 ME_RewrapRepaint(editor);
4238 goto do_default;
4240 /* IME messages to make richedit controls IME aware */
4241 case WM_IME_SETCONTEXT:
4242 case WM_IME_CONTROL:
4243 case WM_IME_SELECT:
4244 case WM_IME_COMPOSITIONFULL:
4245 return 0;
4246 case WM_IME_STARTCOMPOSITION:
4248 editor->imeStartIndex=ME_GetCursorOfs(&editor->pCursors[0]);
4249 ME_DeleteSelection(editor);
4250 ME_CommitUndo(editor);
4251 ME_UpdateRepaint(editor);
4252 return 0;
4254 case WM_IME_COMPOSITION:
4256 HIMC hIMC;
4258 ME_Style *style = ME_GetInsertStyle(editor, 0);
4259 hIMC = ITextHost_TxImmGetContext(editor->texthost);
4260 ME_DeleteSelection(editor);
4261 ME_SaveTempStyle(editor);
4262 if (lParam & (GCS_RESULTSTR|GCS_COMPSTR))
4264 LPWSTR lpCompStr = NULL;
4265 DWORD dwBufLen;
4266 DWORD dwIndex = lParam & GCS_RESULTSTR;
4267 if (!dwIndex)
4268 dwIndex = GCS_COMPSTR;
4270 dwBufLen = ImmGetCompositionStringW(hIMC, dwIndex, NULL, 0);
4271 lpCompStr = HeapAlloc(GetProcessHeap(),0,dwBufLen + sizeof(WCHAR));
4272 ImmGetCompositionStringW(hIMC, dwIndex, lpCompStr, dwBufLen);
4273 lpCompStr[dwBufLen/sizeof(WCHAR)] = 0;
4274 ME_InsertTextFromCursor(editor,0,lpCompStr,dwBufLen/sizeof(WCHAR),style);
4275 HeapFree(GetProcessHeap(), 0, lpCompStr);
4277 if (dwIndex == GCS_COMPSTR)
4278 ME_SetSelection(editor,editor->imeStartIndex,
4279 editor->imeStartIndex + dwBufLen/sizeof(WCHAR));
4281 ME_ReleaseStyle(style);
4282 ME_CommitUndo(editor);
4283 ME_UpdateRepaint(editor);
4284 return 0;
4286 case WM_IME_ENDCOMPOSITION:
4288 ME_DeleteSelection(editor);
4289 editor->imeStartIndex=-1;
4290 return 0;
4292 case EM_GETOLEINTERFACE:
4294 LPVOID *ppvObj = (LPVOID*) lParam;
4295 return CreateIRichEditOle(editor, ppvObj);
4297 case EM_GETPASSWORDCHAR:
4299 return editor->cPasswordMask;
4301 case EM_SETOLECALLBACK:
4302 if(editor->lpOleCallback)
4303 IUnknown_Release(editor->lpOleCallback);
4304 editor->lpOleCallback = (LPRICHEDITOLECALLBACK)lParam;
4305 if(editor->lpOleCallback)
4306 IUnknown_AddRef(editor->lpOleCallback);
4307 return TRUE;
4308 case EM_GETWORDBREAKPROC:
4309 return (LRESULT)editor->pfnWordBreak;
4310 case EM_SETWORDBREAKPROC:
4312 EDITWORDBREAKPROCW pfnOld = editor->pfnWordBreak;
4314 editor->pfnWordBreak = (EDITWORDBREAKPROCW)lParam;
4315 return (LRESULT)pfnOld;
4317 case EM_GETTEXTMODE:
4318 return editor->mode;
4319 case EM_SETTEXTMODE:
4321 LRESULT ret;
4322 int mask = 0;
4323 int changes = 0;
4324 GETTEXTLENGTHEX how;
4326 /* CR/LF conversion required in 2.0 mode, verbatim in 1.0 mode */
4327 how.flags = GTL_CLOSE | (editor->bEmulateVersion10 ? 0 : GTL_USECRLF) | GTL_NUMCHARS;
4328 how.codepage = unicode ? 1200 : CP_ACP;
4329 ret = ME_GetTextLengthEx(editor, &how);
4330 if (!ret)
4332 /*Check for valid wParam*/
4333 if ((((wParam & TM_RICHTEXT) && ((wParam & TM_PLAINTEXT) << 1))) ||
4334 (((wParam & TM_MULTILEVELUNDO) && ((wParam & TM_SINGLELEVELUNDO) << 1))) ||
4335 (((wParam & TM_MULTICODEPAGE) && ((wParam & TM_SINGLECODEPAGE) << 1))))
4336 return 1;
4337 else
4339 if (wParam & (TM_RICHTEXT | TM_PLAINTEXT))
4341 mask |= (TM_RICHTEXT | TM_PLAINTEXT);
4342 changes |= (wParam & (TM_RICHTEXT | TM_PLAINTEXT));
4344 /*FIXME: Currently no support for undo level and code page options*/
4345 editor->mode = (editor->mode & (~mask)) | changes;
4346 return 0;
4349 return ret;
4351 case EM_SETPASSWORDCHAR:
4353 editor->cPasswordMask = wParam;
4354 ME_RewrapRepaint(editor);
4355 return 0;
4357 case EM_SETTARGETDEVICE:
4358 if (wParam == 0)
4360 BOOL new = (lParam == 0 && (editor->styleFlags & ES_MULTILINE));
4361 if (editor->nAvailWidth || editor->bWordWrap != new)
4363 editor->bWordWrap = new;
4364 editor->nAvailWidth = 0; /* wrap to client area */
4365 ME_RewrapRepaint(editor);
4367 } else {
4368 int width = max(0, lParam);
4369 if ((editor->styleFlags & ES_MULTILINE) &&
4370 (!editor->bWordWrap || editor->nAvailWidth != width))
4372 editor->nAvailWidth = width;
4373 editor->bWordWrap = TRUE;
4374 ME_RewrapRepaint(editor);
4376 FIXME("EM_SETTARGETDEVICE doesn't use non-NULL target devices\n");
4378 return TRUE;
4379 default:
4380 do_default:
4381 *phresult = S_FALSE;
4382 break;
4384 return 0L;
4387 static LRESULT RichEditWndProc_common(HWND hWnd, UINT msg, WPARAM wParam,
4388 LPARAM lParam, BOOL unicode)
4390 ME_TextEditor *editor;
4391 HRESULT hresult;
4392 LRESULT lresult = 0;
4394 TRACE("enter hwnd %p msg %04x (%s) %lx %lx, unicode %d\n",
4395 hWnd, msg, get_msg_name(msg), wParam, lParam, unicode);
4397 editor = (ME_TextEditor *)GetWindowLongPtrW(hWnd, 0);
4398 if (!editor)
4400 if (msg == WM_NCCREATE)
4402 CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
4403 ITextHost *texthost;
4405 TRACE("WM_NCCREATE: hWnd %p style 0x%08x\n", hWnd, pcs->style);
4406 texthost = ME_CreateTextHost(hWnd, pcs, FALSE);
4407 return texthost != NULL;
4409 else if (msg != WM_NCDESTROY)
4411 ERR("called with invalid hWnd %p - application bug?\n", hWnd);
4412 return 0;
4416 switch (msg)
4418 case WM_PAINT:
4420 HDC hDC;
4421 RECT rc;
4422 PAINTSTRUCT ps;
4424 hDC = BeginPaint(editor->hWnd, &ps);
4425 /* Erase area outside of the formatting rectangle */
4426 if (ps.rcPaint.top < editor->rcFormat.top)
4428 rc = ps.rcPaint;
4429 rc.bottom = editor->rcFormat.top;
4430 FillRect(hDC, &rc, editor->hbrBackground);
4431 ps.rcPaint.top = editor->rcFormat.top;
4433 if (ps.rcPaint.bottom > editor->rcFormat.bottom) {
4434 rc = ps.rcPaint;
4435 rc.top = editor->rcFormat.bottom;
4436 FillRect(hDC, &rc, editor->hbrBackground);
4437 ps.rcPaint.bottom = editor->rcFormat.bottom;
4439 if (ps.rcPaint.left < editor->rcFormat.left) {
4440 rc = ps.rcPaint;
4441 rc.right = editor->rcFormat.left;
4442 FillRect(hDC, &rc, editor->hbrBackground);
4443 ps.rcPaint.left = editor->rcFormat.left;
4445 if (ps.rcPaint.right > editor->rcFormat.right) {
4446 rc = ps.rcPaint;
4447 rc.left = editor->rcFormat.right;
4448 FillRect(hDC, &rc, editor->hbrBackground);
4449 ps.rcPaint.right = editor->rcFormat.right;
4452 ME_PaintContent(editor, hDC, FALSE, &ps.rcPaint);
4453 EndPaint(editor->hWnd, &ps);
4454 return 0;
4456 case WM_ERASEBKGND:
4458 HDC hDC = (HDC)wParam;
4459 RECT rc;
4461 if (GetUpdateRect(editor->hWnd, &rc, TRUE))
4462 FillRect(hDC, &rc, editor->hbrBackground);
4463 return 1;
4465 case EM_SETOPTIONS:
4467 DWORD dwStyle;
4468 const DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
4469 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN |
4470 ECO_SELECTIONBAR;
4471 lresult = ME_HandleMessage(editor, msg, wParam, lParam, unicode, &hresult);
4472 dwStyle = GetWindowLongW(hWnd, GWL_STYLE);
4473 dwStyle = (dwStyle & ~mask) | (lresult & mask);
4474 SetWindowLongW(hWnd, GWL_STYLE, dwStyle);
4475 return lresult;
4477 case EM_SETREADONLY:
4479 DWORD dwStyle;
4480 lresult = ME_HandleMessage(editor, msg, wParam, lParam, unicode, &hresult);
4481 dwStyle = GetWindowLongW(hWnd, GWL_STYLE);
4482 dwStyle &= ~ES_READONLY;
4483 if (wParam)
4484 dwStyle |= ES_READONLY;
4485 SetWindowLongW(hWnd, GWL_STYLE, dwStyle);
4486 return lresult;
4488 default:
4489 lresult = ME_HandleMessage(editor, msg, wParam, lParam, unicode, &hresult);
4492 if (hresult == S_FALSE)
4493 lresult = DefWindowProcW(hWnd, msg, wParam, lParam);
4495 TRACE("exit hwnd %p msg %04x (%s) %lx %lx, unicode %d -> %lu\n",
4496 hWnd, msg, get_msg_name(msg), wParam, lParam, unicode, lresult);
4498 return lresult;
4501 static LRESULT WINAPI RichEditWndProcW(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
4503 BOOL unicode = TRUE;
4505 /* Under Win9x RichEdit20W returns ANSI strings, see the tests. */
4506 if (msg == WM_GETTEXT && (GetVersion() & 0x80000000))
4507 unicode = FALSE;
4509 return RichEditWndProc_common(hWnd, msg, wParam, lParam, unicode);
4512 static LRESULT WINAPI RichEditWndProcA(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
4514 return RichEditWndProc_common(hWnd, msg, wParam, lParam, FALSE);
4517 /******************************************************************
4518 * RichEditANSIWndProc (RICHED20.10)
4520 LRESULT WINAPI RichEditANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
4522 return RichEditWndProcA(hWnd, msg, wParam, lParam);
4525 /******************************************************************
4526 * RichEdit10ANSIWndProc (RICHED20.9)
4528 LRESULT WINAPI RichEdit10ANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
4530 if (msg == WM_NCCREATE && !GetWindowLongPtrW(hWnd, 0))
4532 ITextHost *texthost;
4533 CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
4535 TRACE("WM_NCCREATE: hWnd %p style 0x%08x\n", hWnd, pcs->style);
4536 texthost = ME_CreateTextHost(hWnd, pcs, TRUE);
4537 return texthost != NULL;
4539 return RichEditANSIWndProc(hWnd, msg, wParam, lParam);
4542 void ME_SendOldNotify(ME_TextEditor *editor, int nCode)
4544 ITextHost_TxNotify(editor->texthost, nCode, NULL);
4547 /* Fill buffer with srcChars unicode characters from the start cursor.
4549 * buffer: destination buffer
4550 * buflen: length of buffer in characters excluding the NULL terminator.
4551 * start: start of editor text to copy into buffer.
4552 * srcChars: Number of characters to use from the editor text.
4553 * bCRLF: if true, replaces all end of lines with \r\n pairs.
4555 * returns the number of characters written excluding the NULL terminator.
4557 * The written text is always NULL terminated.
4559 int ME_GetTextW(ME_TextEditor *editor, WCHAR *buffer, int buflen,
4560 const ME_Cursor *start, int srcChars, BOOL bCRLF)
4562 ME_DisplayItem *pRun, *pNextRun;
4563 const WCHAR *pStart = buffer;
4564 const WCHAR cr_lf[] = {'\r', '\n', 0};
4565 const WCHAR *str;
4566 int nLen;
4568 /* bCRLF flag is only honored in 2.0 and up. 1.0 must always return text verbatim */
4569 if (editor->bEmulateVersion10) bCRLF = 0;
4571 pRun = start->pRun;
4572 assert(pRun);
4573 pNextRun = ME_FindItemFwd(pRun, diRun);
4575 nLen = pRun->member.run.strText->nLen - start->nOffset;
4576 str = pRun->member.run.strText->szData + start->nOffset;
4578 /* No '\r' is appended to the last paragraph. */
4579 while (srcChars && buflen && pNextRun)
4581 int nFlags = pRun->member.run.nFlags;
4583 if (bCRLF && nFlags & MERF_ENDPARA && ~nFlags & MERF_ENDCELL)
4585 if (buflen == 1) break;
4586 /* FIXME: native fails to reduce srcChars here for WM_GETTEXT or
4587 * EM_GETTEXTEX, however, this is done for copying text which
4588 * also uses this function. */
4589 srcChars -= min(nLen, srcChars);
4590 nLen = 2;
4591 str = cr_lf;
4592 } else {
4593 nLen = min(nLen, srcChars);
4594 srcChars -= nLen;
4597 nLen = min(nLen, buflen);
4598 buflen -= nLen;
4600 CopyMemory(buffer, str, sizeof(WCHAR) * nLen);
4602 buffer += nLen;
4604 pRun = pNextRun;
4605 pNextRun = ME_FindItemFwd(pRun, diRun);
4607 nLen = pRun->member.run.strText->nLen;
4608 str = pRun->member.run.strText->szData;
4610 *buffer = 0;
4611 return buffer - pStart;
4614 static BOOL ME_RegisterEditorClass(HINSTANCE hInstance)
4616 WNDCLASSW wcW;
4617 WNDCLASSA wcA;
4619 wcW.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
4620 wcW.lpfnWndProc = RichEditWndProcW;
4621 wcW.cbClsExtra = 0;
4622 wcW.cbWndExtra = sizeof(ME_TextEditor *);
4623 wcW.hInstance = NULL; /* hInstance would register DLL-local class */
4624 wcW.hIcon = NULL;
4625 wcW.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
4626 wcW.hbrBackground = GetStockObject(NULL_BRUSH);
4627 wcW.lpszMenuName = NULL;
4629 if (is_version_nt())
4631 wcW.lpszClassName = RICHEDIT_CLASS20W;
4632 if (!RegisterClassW(&wcW)) return FALSE;
4633 wcW.lpszClassName = MSFTEDIT_CLASS;
4634 if (!RegisterClassW(&wcW)) return FALSE;
4636 else
4638 /* WNDCLASSA/W have the same layout */
4639 wcW.lpszClassName = (LPCWSTR)"RichEdit20W";
4640 if (!RegisterClassA((WNDCLASSA *)&wcW)) return FALSE;
4641 wcW.lpszClassName = (LPCWSTR)"RichEdit50W";
4642 if (!RegisterClassA((WNDCLASSA *)&wcW)) return FALSE;
4645 wcA.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
4646 wcA.lpfnWndProc = RichEditWndProcA;
4647 wcA.cbClsExtra = 0;
4648 wcA.cbWndExtra = sizeof(ME_TextEditor *);
4649 wcA.hInstance = NULL; /* hInstance would register DLL-local class */
4650 wcA.hIcon = NULL;
4651 wcA.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
4652 wcA.hbrBackground = GetStockObject(NULL_BRUSH);
4653 wcA.lpszMenuName = NULL;
4654 wcA.lpszClassName = RICHEDIT_CLASS20A;
4655 if (!RegisterClassA(&wcA)) return FALSE;
4656 wcA.lpszClassName = "RichEdit50A";
4657 if (!RegisterClassA(&wcA)) return FALSE;
4659 return TRUE;
4662 static LRESULT WINAPI REComboWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
4663 /* FIXME: Not implemented */
4664 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
4665 hWnd, msg, get_msg_name(msg), wParam, lParam);
4666 return DefWindowProcW(hWnd, msg, wParam, lParam);
4669 static LRESULT WINAPI REListWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
4670 /* FIXME: Not implemented */
4671 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
4672 hWnd, msg, get_msg_name(msg), wParam, lParam);
4673 return DefWindowProcW(hWnd, msg, wParam, lParam);
4676 /******************************************************************
4677 * REExtendedRegisterClass (RICHED20.8)
4679 * FIXME undocumented
4680 * Need to check for errors and implement controls and callbacks
4682 LRESULT WINAPI REExtendedRegisterClass(void)
4684 WNDCLASSW wcW;
4685 UINT result;
4687 FIXME("semi stub\n");
4689 wcW.cbClsExtra = 0;
4690 wcW.cbWndExtra = 4;
4691 wcW.hInstance = NULL;
4692 wcW.hIcon = NULL;
4693 wcW.hCursor = NULL;
4694 wcW.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
4695 wcW.lpszMenuName = NULL;
4697 if (!ME_ListBoxRegistered)
4699 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS;
4700 wcW.lpfnWndProc = REListWndProc;
4701 wcW.lpszClassName = REListBox20W;
4702 if (RegisterClassW(&wcW)) ME_ListBoxRegistered = TRUE;
4705 if (!ME_ComboBoxRegistered)
4707 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
4708 wcW.lpfnWndProc = REComboWndProc;
4709 wcW.lpszClassName = REComboBox20W;
4710 if (RegisterClassW(&wcW)) ME_ComboBoxRegistered = TRUE;
4713 result = 0;
4714 if (ME_ListBoxRegistered)
4715 result += 1;
4716 if (ME_ComboBoxRegistered)
4717 result += 2;
4719 return result;
4722 static BOOL isurlspecial(WCHAR c)
4724 static const WCHAR special_chars[] = {'.','/','%','@','*','|','\\','+','#',0};
4725 return strchrW( special_chars, c ) != NULL;
4729 * This proc takes a selection, and scans it forward in order to select the span
4730 * of a possible URL candidate. A possible URL candidate must start with isalnum
4731 * or one of the following special characters: *|/\+%#@ and must consist entirely
4732 * of the characters allowed to start the URL, plus : (colon) which may occur
4733 * at most once, and not at either end.
4735 static BOOL ME_FindNextURLCandidate(ME_TextEditor *editor,
4736 const ME_Cursor *start,
4737 int nChars,
4738 ME_Cursor *candidate_min,
4739 ME_Cursor *candidate_max)
4741 ME_Cursor cursor = *start;
4742 BOOL foundColon = FALSE;
4743 BOOL candidateStarted = FALSE;
4744 WCHAR lastAcceptedChar = '\0';
4746 while (nChars > 0)
4748 WCHAR *strStart = cursor.pRun->member.run.strText->szData;
4749 WCHAR *str = strStart + cursor.nOffset;
4750 int nLen = cursor.pRun->member.run.strText->nLen - cursor.nOffset;
4751 nChars -= nLen;
4753 if (~cursor.pRun->member.run.nFlags & MERF_ENDPARA)
4755 /* Find start of candidate */
4756 if (!candidateStarted)
4758 while (nLen)
4760 nLen--;
4761 if (isalnumW(*str) || isurlspecial(*str))
4763 cursor.nOffset = str - strStart;
4764 *candidate_min = cursor;
4765 candidateStarted = TRUE;
4766 lastAcceptedChar = *str++;
4767 break;
4769 str++;
4773 /* Find end of candidate */
4774 if (candidateStarted) {
4775 while (nLen)
4777 nLen--;
4778 if (*str == ':' && !foundColon) {
4779 foundColon = TRUE;
4780 } else if (!isalnumW(*str) && !isurlspecial(*str)) {
4781 cursor.nOffset = str - strStart;
4782 if (lastAcceptedChar == ':')
4783 ME_MoveCursorChars(editor, &cursor, -1);
4784 *candidate_max = cursor;
4785 return TRUE;
4787 lastAcceptedChar = *str++;
4790 } else {
4791 /* End of paragraph: skip it if before candidate span, or terminates
4792 current active span */
4793 if (candidateStarted) {
4794 if (lastAcceptedChar == ':')
4795 ME_MoveCursorChars(editor, &cursor, -1);
4796 *candidate_max = cursor;
4797 return TRUE;
4801 /* Reaching this point means no span was found, so get next span */
4802 if (!ME_NextRun(&cursor.pPara, &cursor.pRun)) {
4803 if (candidateStarted) {
4804 /* There are no further runs, so take end of text as end of candidate */
4805 cursor.nOffset = str - strStart;
4806 if (lastAcceptedChar == ':')
4807 ME_MoveCursorChars(editor, &cursor, -1);
4808 *candidate_max = cursor;
4809 return TRUE;
4811 *candidate_max = *candidate_min = cursor;
4812 return FALSE;
4814 cursor.nOffset = 0;
4817 if (candidateStarted) {
4818 /* There are no further runs, so take end of text as end of candidate */
4819 if (lastAcceptedChar == ':')
4820 ME_MoveCursorChars(editor, &cursor, -1);
4821 *candidate_max = cursor;
4822 return TRUE;
4824 *candidate_max = *candidate_min = cursor;
4825 return FALSE;
4829 * This proc evaluates the selection and returns TRUE if it can be considered an URL
4831 static BOOL ME_IsCandidateAnURL(ME_TextEditor *editor, const ME_Cursor *start, int nChars)
4833 #define MAX_PREFIX_LEN 9
4834 struct prefix_s {
4835 const WCHAR text[MAX_PREFIX_LEN];
4836 int length;
4837 }prefixes[] = {
4838 {{'p','r','o','s','p','e','r','o',':'}, 9},
4839 {{'t','e','l','n','e','t',':'}, 7},
4840 {{'g','o','p','h','e','r',':'}, 7},
4841 {{'m','a','i','l','t','o',':'}, 7},
4842 {{'h','t','t','p','s',':'}, 6},
4843 {{'f','i','l','e',':'}, 5},
4844 {{'n','e','w','s',':'}, 5},
4845 {{'w','a','i','s',':'}, 5},
4846 {{'n','n','t','p',':'}, 5},
4847 {{'h','t','t','p',':'}, 5},
4848 {{'w','w','w','.'}, 4},
4849 {{'f','t','p',':'}, 4},
4851 WCHAR bufferW[MAX_PREFIX_LEN + 1];
4852 unsigned int i;
4854 ME_GetTextW(editor, bufferW, MAX_PREFIX_LEN, start, nChars, 0);
4855 for (i = 0; i < sizeof(prefixes) / sizeof(*prefixes); i++)
4857 if (nChars < prefixes[i].length) continue;
4858 if (!memcmp(prefixes[i].text, bufferW, prefixes[i].length * sizeof(WCHAR)))
4859 return TRUE;
4861 return FALSE;
4862 #undef MAX_PREFIX_LEN
4866 * This proc walks through the indicated selection and evaluates whether each
4867 * section identified by ME_FindNextURLCandidate and in-between sections have
4868 * their proper CFE_LINK attributes set or unset. If the CFE_LINK attribute is
4869 * not what it is supposed to be, this proc sets or unsets it as appropriate.
4871 * Since this function can cause runs to be split, do not depend on the value
4872 * of the start cursor at the end of the function.
4874 * nChars may be set to INT_MAX to update to the end of the text.
4876 * Returns TRUE if at least one section was modified.
4878 static BOOL ME_UpdateLinkAttribute(ME_TextEditor *editor, ME_Cursor *start, int nChars)
4880 BOOL modified = FALSE;
4881 ME_Cursor startCur = *start;
4883 if (!editor->AutoURLDetect_bEnable) return FALSE;
4887 CHARFORMAT2W link;
4888 ME_Cursor candidateStart, candidateEnd;
4890 if (ME_FindNextURLCandidate(editor, &startCur, nChars,
4891 &candidateStart, &candidateEnd))
4893 /* Section before candidate is not an URL */
4894 int cMin = ME_GetCursorOfs(&candidateStart);
4895 int cMax = ME_GetCursorOfs(&candidateEnd);
4897 if (!ME_IsCandidateAnURL(editor, &candidateStart, cMax - cMin))
4898 candidateStart = candidateEnd;
4899 nChars -= cMax - ME_GetCursorOfs(&startCur);
4901 else
4903 /* No more candidates until end of selection */
4904 nChars = 0;
4907 if (startCur.pRun != candidateStart.pRun ||
4908 startCur.nOffset != candidateStart.nOffset)
4910 /* CFE_LINK effect should be consistently unset */
4911 link.cbSize = sizeof(link);
4912 ME_GetCharFormat(editor, &startCur, &candidateStart, &link);
4913 if (!(link.dwMask & CFM_LINK) || (link.dwEffects & CFE_LINK))
4915 /* CFE_LINK must be unset from this range */
4916 memset(&link, 0, sizeof(CHARFORMAT2W));
4917 link.cbSize = sizeof(link);
4918 link.dwMask = CFM_LINK;
4919 link.dwEffects = 0;
4920 ME_SetCharFormat(editor, &startCur, &candidateStart, &link);
4921 /* Update candidateEnd since setting character formats may split
4922 * runs, which can cause a cursor to be at an invalid offset within
4923 * a split run. */
4924 while (candidateEnd.nOffset >= candidateEnd.pRun->member.run.strText->nLen)
4926 candidateEnd.nOffset -= candidateEnd.pRun->member.run.strText->nLen;
4927 candidateEnd.pRun = ME_FindItemFwd(candidateEnd.pRun, diRun);
4929 modified = TRUE;
4932 if (candidateStart.pRun != candidateEnd.pRun ||
4933 candidateStart.nOffset != candidateEnd.nOffset)
4935 /* CFE_LINK effect should be consistently set */
4936 link.cbSize = sizeof(link);
4937 ME_GetCharFormat(editor, &candidateStart, &candidateEnd, &link);
4938 if (!(link.dwMask & CFM_LINK) || !(link.dwEffects & CFE_LINK))
4940 /* CFE_LINK must be set on this range */
4941 memset(&link, 0, sizeof(CHARFORMAT2W));
4942 link.cbSize = sizeof(link);
4943 link.dwMask = CFM_LINK;
4944 link.dwEffects = CFE_LINK;
4945 ME_SetCharFormat(editor, &candidateStart, &candidateEnd, &link);
4946 modified = TRUE;
4949 startCur = candidateEnd;
4950 } while (nChars > 0);
4951 return modified;