richedit: Handle NULL buffer or 0 buffer length in ME_GetTextEx.
[wine/multimedia.git] / dlls / riched20 / editor.c
blob2a6f9089799d5083e6b9e19c70c2986c3ae3fe1a
1 /*
2 * RichEdit - functions dealing with editor object
4 * Copyright 2004 by Krzysztof Foltman
5 * Copyright 2005 by Cihan Altinay
6 * Copyright 2005 by Phil Krylov
7 * Copyright 2008 Eric Pouech
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 /*
25 API implementation status:
27 Messages (ANSI versions not done yet)
28 + EM_AUTOURLDETECT 2.0
29 + EM_CANPASTE
30 + EM_CANREDO 2.0
31 + EM_CANUNDO
32 + EM_CHARFROMPOS
33 - EM_DISPLAYBAND
34 + EM_EMPTYUNDOBUFFER
35 + EM_EXGETSEL
36 + EM_EXLIMITTEXT
37 + EM_EXLINEFROMCHAR
38 + EM_EXSETSEL
39 + EM_FINDTEXT (only FR_DOWN flag implemented)
40 + EM_FINDTEXTEX (only FR_DOWN flag implemented)
41 - EM_FINDWORDBREAK
42 - EM_FMTLINES
43 - EM_FORMATRANGE
44 + EM_GETAUTOURLDETECT 2.0
45 - EM_GETBIDIOPTIONS 3.0
46 - EM_GETCHARFORMAT (partly done)
47 - EM_GETEDITSTYLE
48 + EM_GETEVENTMASK
49 + EM_GETFIRSTVISIBLELINE (can be optimized if needed)
50 - EM_GETIMECOLOR 1.0asian
51 - EM_GETIMECOMPMODE 2.0
52 - EM_GETIMEOPTIONS 1.0asian
53 - EM_GETIMESTATUS
54 - EM_GETLANGOPTIONS 2.0
55 + EM_GETLIMITTEXT
56 + EM_GETLINE
57 + EM_GETLINECOUNT returns number of rows, not of paragraphs
58 + EM_GETMODIFY
59 + EM_GETOLEINTERFACE
60 + EM_GETOPTIONS
61 + EM_GETPARAFORMAT
62 + EM_GETPASSWORDCHAR 2.0
63 - EM_GETPUNCTUATION 1.0asian
64 + EM_GETRECT
65 - EM_GETREDONAME 2.0
66 + EM_GETSEL
67 + EM_GETSELTEXT (ANSI&Unicode)
68 + EM_GETSCROLLPOS 3.0
69 ! - EM_GETTHUMB
70 + EM_GETTEXTEX 2.0
71 + EM_GETTEXTLENGTHEX (GTL_PRECISE unimplemented)
72 + EM_GETTEXTMODE 2.0
73 ? + EM_GETTEXTRANGE (ANSI&Unicode)
74 - EM_GETTYPOGRAPHYOPTIONS 3.0
75 - EM_GETUNDONAME
76 + EM_GETWORDBREAKPROC
77 - EM_GETWORDBREAKPROCEX
78 - EM_GETWORDWRAPMODE 1.0asian
79 + EM_GETZOOM 3.0
80 + EM_HIDESELECTION
81 + EM_LIMITTEXT (Also called EM_SETLIMITTEXT)
82 + EM_LINEFROMCHAR
83 + EM_LINEINDEX
84 + EM_LINELENGTH
85 + EM_LINESCROLL
86 - EM_PASTESPECIAL
87 + EM_POSFROMCHAR
88 + EM_REDO 2.0
89 + EM_REQUESTRESIZE
90 + EM_REPLACESEL (proper style?) ANSI&Unicode
91 + EM_SCROLL
92 + EM_SCROLLCARET
93 - EM_SELECTIONTYPE
94 - EM_SETBIDIOPTIONS 3.0
95 + EM_SETBKGNDCOLOR
96 + EM_SETCHARFORMAT (partly done, no ANSI)
97 - EM_SETEDITSTYLE
98 + EM_SETEVENTMASK (few notifications supported)
99 - EM_SETFONTSIZE
100 - EM_SETIMECOLOR 1.0asian
101 - EM_SETIMEOPTIONS 1.0asian
102 - EM_SETIMESTATUS
103 - EM_SETLANGOPTIONS 2.0
104 - EM_SETLIMITTEXT
105 - EM_SETMARGINS
106 + EM_SETMODIFY (not sure if implementation is correct)
107 - EM_SETOLECALLBACK
108 + EM_SETOPTIONS (partially implemented)
109 - EM_SETPALETTE 2.0
110 + EM_SETPARAFORMAT
111 + EM_SETPASSWORDCHAR 2.0
112 - EM_SETPUNCTUATION 1.0asian
113 + EM_SETREADONLY no beep on modification attempt
114 + EM_SETRECT
115 + EM_SETRECTNP (EM_SETRECT without repainting)
116 + EM_SETSEL
117 + EM_SETSCROLLPOS 3.0
118 - EM_SETTABSTOPS 3.0
119 - EM_SETTARGETDEVICE (partial)
120 + EM_SETTEXTEX 3.0 (proper style?)
121 - EM_SETTEXTMODE 2.0
122 - EM_SETTYPOGRAPHYOPTIONS 3.0
123 + EM_SETUNDOLIMIT 2.0
124 + EM_SETWORDBREAKPROC (used only for word movement at the moment)
125 - EM_SETWORDBREAKPROCEX
126 - EM_SETWORDWRAPMODE 1.0asian
127 + EM_SETZOOM 3.0
128 + EM_SHOWSCROLLBAR 2.0
129 + EM_STOPGROUPTYPING 2.0
130 + EM_STREAMIN
131 + EM_STREAMOUT
132 + EM_UNDO
133 + WM_CHAR
134 + WM_CLEAR
135 + WM_COPY
136 + WM_CUT
137 + WM_GETDLGCODE (the current implementation is incomplete)
138 + WM_GETTEXT (ANSI&Unicode)
139 + WM_GETTEXTLENGTH (ANSI version sucks)
140 + WM_HSCROLL
141 + WM_PASTE
142 + WM_SETFONT
143 + WM_SETTEXT (resets undo stack !) (proper style?) ANSI&Unicode
144 - WM_STYLECHANGING
145 - WM_STYLECHANGED (things like read-only flag)
146 + WM_UNICHAR
147 + WM_VSCROLL
149 Notifications
151 * EN_CHANGE (sent from the wrong place)
152 - EN_CORRECTTEXT
153 - EN_DROPFILES
154 - EN_ERRSPACE
155 - EN_HSCROLL
156 - EN_IMECHANGE
157 + EN_KILLFOCUS
158 - EN_LINK
159 - EN_MAXTEXT
160 - EN_MSGFILTER
161 - EN_OLEOPFAILED
162 - EN_PROTECTED
163 + EN_REQUESTRESIZE
164 - EN_SAVECLIPBOARD
165 + EN_SELCHANGE
166 + EN_SETFOCUS
167 - EN_STOPNOUNDO
168 * EN_UPDATE (sent from the wrong place)
169 - EN_VSCROLL
171 Styles
173 - ES_AUTOHSCROLL
174 - ES_AUTOVSCROLL
175 - ES_CENTER
176 + ES_DISABLENOSCROLL (scrollbar is always visible)
177 - ES_EX_NOCALLOLEINIT
178 - ES_LEFT
179 - ES_MULTILINE (currently single line controls aren't supported)
180 - ES_NOIME
181 - ES_READONLY (I'm not sure if beeping is the proper behaviour)
182 - ES_RIGHT
183 - ES_SAVESEL
184 - ES_SELFIME
185 - ES_SUNKEN
186 - ES_VERTICAL
187 - ES_WANTRETURN (don't know how to do WM_GETDLGCODE part)
188 - WS_SETFONT
189 + WS_HSCROLL
190 + WS_VSCROLL
194 * RICHED20 TODO (incomplete):
196 * - messages/styles/notifications listed above
197 * - add remaining CHARFORMAT/PARAFORMAT fields
198 * - right/center align should strip spaces from the beginning
199 * - pictures/OLE objects (not just smiling faces that lack API support ;-) )
200 * - COM interface (looks like a major pain in the TODO list)
201 * - calculate heights of pictures (half-done)
202 * - hysteresis during wrapping (related to scrollbars appearing/disappearing)
203 * - find/replace
204 * - how to implement EM_FORMATRANGE and EM_DISPLAYBAND ? (Mission Impossible)
205 * - italic caret with italic fonts
206 * - IME
207 * - most notifications aren't sent at all (the most important ones are)
208 * - when should EN_SELCHANGE be sent after text change ? (before/after EN_UPDATE?)
209 * - WM_SETTEXT may use wrong style (but I'm 80% sure it's OK)
210 * - EM_GETCHARFORMAT with SCF_SELECTION may not behave 100% like in original (but very close)
211 * - full justification
212 * - hyphenation
213 * - tables
214 * - ListBox & ComboBox not implemented
216 * Bugs that are probably fixed, but not so easy to verify:
217 * - EN_UPDATE/EN_CHANGE are handled very incorrectly (should be OK now)
218 * - undo for ME_JoinParagraphs doesn't store paragraph format ? (it does)
219 * - check/fix artificial EOL logic (bCursorAtEnd, hardly logical)
220 * - caret shouldn't be displayed when selection isn't empty
221 * - check refcounting in style management functions (looks perfect now, but no bugs is suspicious)
222 * - undo for setting default format (done, might be buggy)
223 * - styles might be not released properly (looks like they work like charm, but who knows?
227 #include "editor.h"
228 #include "commdlg.h"
229 #include "winreg.h"
230 #define NO_SHLWAPI_STREAM
231 #include "shlwapi.h"
232 #include "rtf.h"
233 #include "imm.h"
234 #include "res.h"
236 #define STACK_SIZE_DEFAULT 100
237 #define STACK_SIZE_MAX 1000
239 #define TEXT_LIMIT_DEFAULT 32767
241 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
243 static BOOL ME_RegisterEditorClass(HINSTANCE);
244 static BOOL ME_UpdateLinkAttribute(ME_TextEditor *editor, int sel_min, int sel_max);
246 static const WCHAR RichEdit20W[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '2', '0', 'W', 0};
247 static const WCHAR RichEdit50W[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '5', '0', 'W', 0};
248 static const WCHAR REListBox20W[] = {'R','E','L','i','s','t','B','o','x','2','0','W', 0};
249 static const WCHAR REComboBox20W[] = {'R','E','C','o','m','b','o','B','o','x','2','0','W', 0};
250 static HCURSOR hLeft;
252 int me_debug = 0;
253 HANDLE me_heap = NULL;
255 static BOOL ME_ListBoxRegistered = FALSE;
256 static BOOL ME_ComboBoxRegistered = FALSE;
258 static inline int is_version_nt(void)
260 return !(GetVersion() & 0x80000000);
263 static ME_TextBuffer *ME_MakeText(void) {
265 ME_TextBuffer *buf = ALLOC_OBJ(ME_TextBuffer);
267 ME_DisplayItem *p1 = ME_MakeDI(diTextStart);
268 ME_DisplayItem *p2 = ME_MakeDI(diTextEnd);
270 p1->prev = NULL;
271 p1->next = p2;
272 p2->prev = p1;
273 p2->next = NULL;
274 p1->member.para.next_para = p2;
275 p2->member.para.prev_para = p1;
276 p2->member.para.nCharOfs = 0;
278 buf->pFirst = p1;
279 buf->pLast = p2;
280 buf->pCharStyle = NULL;
282 return buf;
286 static LRESULT ME_StreamInText(ME_TextEditor *editor, DWORD dwFormat, ME_InStream *stream, ME_Style *style)
288 WCHAR wszText[STREAMIN_BUFFER_SIZE+1];
289 WCHAR *pText;
291 TRACE("%08x %p\n", dwFormat, stream);
293 do {
294 long nWideChars = 0;
296 if (!stream->dwSize)
298 ME_StreamInFill(stream);
299 if (stream->editstream->dwError)
300 break;
301 if (!stream->dwSize)
302 break;
305 if (!(dwFormat & SF_UNICODE))
307 /* FIXME? this is doomed to fail on true MBCS like UTF-8, luckily they're unlikely to be used as CP_ACP */
308 nWideChars = MultiByteToWideChar(CP_ACP, 0, stream->buffer, stream->dwSize, wszText, STREAMIN_BUFFER_SIZE);
309 pText = wszText;
311 else
313 nWideChars = stream->dwSize >> 1;
314 pText = (WCHAR *)stream->buffer;
317 ME_InsertTextFromCursor(editor, 0, pText, nWideChars, style);
318 if (stream->dwSize == 0)
319 break;
320 stream->dwSize = 0;
321 } while(1);
322 ME_CommitUndo(editor);
323 ME_UpdateRepaint(editor);
324 return 0;
327 static void ME_ApplyBorderProperties(RTF_Info *info,
328 ME_BorderRect *borderRect,
329 RTFBorder *borderDef)
331 int i, colorNum;
332 ME_Border *pBorders[] = {&borderRect->top,
333 &borderRect->left,
334 &borderRect->bottom,
335 &borderRect->right};
336 for (i = 0; i < 4; i++)
338 RTFColor *colorDef = info->colorList;
339 pBorders[i]->width = borderDef[i].width;
340 colorNum = borderDef[i].color;
341 while (colorDef && colorDef->rtfCNum != colorNum)
342 colorDef = colorDef->rtfNextColor;
343 if (colorDef)
344 pBorders[i]->colorRef = RGB(
345 colorDef->rtfCRed >= 0 ? colorDef->rtfCRed : 0,
346 colorDef->rtfCGreen >= 0 ? colorDef->rtfCGreen : 0,
347 colorDef->rtfCBlue >= 0 ? colorDef->rtfCBlue : 0);
348 else
349 pBorders[i]->colorRef = RGB(0, 0, 0);
353 void ME_RTFCharAttrHook(RTF_Info *info)
355 CHARFORMAT2W fmt;
356 fmt.cbSize = sizeof(fmt);
357 fmt.dwMask = 0;
358 fmt.dwEffects = 0;
360 switch(info->rtfMinor)
362 case rtfPlain:
363 /* FIXME add more flags once they're implemented */
364 fmt.dwMask = CFM_BOLD | CFM_ITALIC | CFM_UNDERLINETYPE | CFM_STRIKEOUT | CFM_COLOR | CFM_BACKCOLOR | CFM_SIZE | CFM_WEIGHT;
365 fmt.dwEffects = CFE_AUTOCOLOR | CFE_AUTOBACKCOLOR;
366 fmt.yHeight = 12*20; /* 12pt */
367 fmt.wWeight = FW_NORMAL;
368 fmt.bUnderlineType = CFU_UNDERLINENONE;
369 break;
370 case rtfBold:
371 fmt.dwMask = CFM_BOLD | CFM_WEIGHT;
372 fmt.dwEffects = info->rtfParam ? CFE_BOLD : 0;
373 fmt.wWeight = info->rtfParam ? FW_BOLD : FW_NORMAL;
374 break;
375 case rtfItalic:
376 fmt.dwMask = CFM_ITALIC;
377 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
378 break;
379 case rtfUnderline:
380 fmt.dwMask = CFM_UNDERLINETYPE;
381 fmt.bUnderlineType = info->rtfParam ? CFU_CF1UNDERLINE : CFU_UNDERLINENONE;
382 break;
383 case rtfDotUnderline:
384 fmt.dwMask = CFM_UNDERLINETYPE;
385 fmt.bUnderlineType = info->rtfParam ? CFU_UNDERLINEDOTTED : CFU_UNDERLINENONE;
386 break;
387 case rtfDbUnderline:
388 fmt.dwMask = CFM_UNDERLINETYPE;
389 fmt.bUnderlineType = info->rtfParam ? CFU_UNDERLINEDOUBLE : CFU_UNDERLINENONE;
390 break;
391 case rtfWordUnderline:
392 fmt.dwMask = CFM_UNDERLINETYPE;
393 fmt.bUnderlineType = info->rtfParam ? CFU_UNDERLINEWORD : CFU_UNDERLINENONE;
394 break;
395 case rtfNoUnderline:
396 fmt.dwMask = CFM_UNDERLINETYPE;
397 fmt.bUnderlineType = CFU_UNDERLINENONE;
398 break;
399 case rtfStrikeThru:
400 fmt.dwMask = CFM_STRIKEOUT;
401 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
402 break;
403 case rtfSubScript:
404 case rtfSuperScript:
405 case rtfSubScrShrink:
406 case rtfSuperScrShrink:
407 case rtfNoSuperSub:
408 fmt.dwMask = CFM_SUBSCRIPT|CFM_SUPERSCRIPT;
409 if (info->rtfMinor == rtfSubScrShrink) fmt.dwEffects = CFE_SUBSCRIPT;
410 if (info->rtfMinor == rtfSuperScrShrink) fmt.dwEffects = CFE_SUPERSCRIPT;
411 if (info->rtfMinor == rtfNoSuperSub) fmt.dwEffects = 0;
412 break;
413 case rtfInvisible:
414 fmt.dwMask = CFM_HIDDEN;
415 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
416 break;
417 case rtfBackColor:
418 fmt.dwMask = CFM_BACKCOLOR;
419 fmt.dwEffects = 0;
420 if (info->rtfParam == 0)
421 fmt.dwEffects = CFE_AUTOBACKCOLOR;
422 else if (info->rtfParam != rtfNoParam)
424 RTFColor *c = RTFGetColor(info, info->rtfParam);
425 if (c && c->rtfCBlue >= 0)
426 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
427 else
428 fmt.dwEffects = CFE_AUTOBACKCOLOR;
430 break;
431 case rtfForeColor:
432 fmt.dwMask = CFM_COLOR;
433 fmt.dwEffects = 0;
434 if (info->rtfParam == 0)
435 fmt.dwEffects = CFE_AUTOCOLOR;
436 else if (info->rtfParam != rtfNoParam)
438 RTFColor *c = RTFGetColor(info, info->rtfParam);
439 if (c && c->rtfCBlue >= 0)
440 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
441 else {
442 fmt.dwEffects = CFE_AUTOCOLOR;
445 break;
446 case rtfFontNum:
447 if (info->rtfParam != rtfNoParam)
449 RTFFont *f = RTFGetFont(info, info->rtfParam);
450 if (f)
452 MultiByteToWideChar(CP_ACP, 0, f->rtfFName, -1, fmt.szFaceName, sizeof(fmt.szFaceName)/sizeof(WCHAR));
453 fmt.szFaceName[sizeof(fmt.szFaceName)/sizeof(WCHAR)-1] = '\0';
454 fmt.bCharSet = f->rtfFCharSet;
455 fmt.dwMask = CFM_FACE | CFM_CHARSET;
456 fmt.bPitchAndFamily = f->rtfFPitch | (f->rtfFFamily << 4);
459 break;
460 case rtfFontSize:
461 fmt.dwMask = CFM_SIZE;
462 if (info->rtfParam != rtfNoParam)
463 fmt.yHeight = info->rtfParam*10;
464 break;
466 if (fmt.dwMask) {
467 ME_Style *style2;
468 RTFFlushOutputBuffer(info);
469 /* FIXME too slow ? how come ? */
470 style2 = ME_ApplyStyle(info->style, &fmt);
471 ME_ReleaseStyle(info->style);
472 info->style = style2;
473 info->styleChanged = TRUE;
477 /* FIXME this function doesn't get any information about context of the RTF tag, which is very bad,
478 the same tags mean different things in different contexts */
479 void ME_RTFParAttrHook(RTF_Info *info)
481 PARAFORMAT2 fmt;
482 fmt.cbSize = sizeof(fmt);
483 fmt.dwMask = 0;
485 switch(info->rtfMinor)
487 case rtfParDef: /* restores default paragraph attributes */
488 if (!info->editor->bEmulateVersion10) /* v4.1 */
489 info->borderType = RTFBorderParaLeft;
490 else /* v1.0 - 3.0 */
491 info->borderType = RTFBorderParaTop;
492 fmt.dwMask = PFM_ALIGNMENT | PFM_BORDER | PFM_LINESPACING | PFM_TABSTOPS |
493 PFM_OFFSET | PFM_RIGHTINDENT | PFM_SPACEAFTER | PFM_SPACEBEFORE |
494 PFM_STARTINDENT;
495 /* TODO: numbering, shading */
496 fmt.wAlignment = PFA_LEFT;
497 fmt.cTabCount = 0;
498 fmt.dxOffset = fmt.dxStartIndent = fmt.dxRightIndent = 0;
499 fmt.wBorderWidth = fmt.wBorders = 0;
500 fmt.wBorderSpace = 0;
501 fmt.bLineSpacingRule = 0;
502 fmt.dySpaceBefore = fmt.dySpaceAfter = 0;
503 fmt.dyLineSpacing = 0;
504 if (!info->editor->bEmulateVersion10) /* v4.1 */
506 if (info->tableDef && info->tableDef->tableRowStart &&
507 info->tableDef->tableRowStart->member.para.nFlags & MEPF_ROWEND)
509 ME_Cursor cursor;
510 ME_DisplayItem *para;
511 /* We are just after a table row. */
512 RTFFlushOutputBuffer(info);
513 cursor = info->editor->pCursors[0];
514 para = cursor.pPara;
515 if (para == info->tableDef->tableRowStart->member.para.next_para
516 && !cursor.nOffset && !cursor.pRun->member.run.nCharOfs)
518 /* Since the table row end, no text has been inserted, and the \intbl
519 * control word has not be used. We can confirm that we are not in a
520 * table anymore.
522 info->tableDef->tableRowStart = NULL;
523 info->canInheritInTbl = FALSE;
526 } else { /* v1.0 - v3.0 */
527 fmt.dwMask |= PFM_TABLE;
528 fmt.wEffects &= ~PFE_TABLE;
530 break;
531 case rtfNestLevel:
532 if (!info->editor->bEmulateVersion10) /* v4.1 */
534 while (info->rtfParam > info->nestingLevel) {
535 RTFTable *tableDef = ALLOC_OBJ(RTFTable);
536 ZeroMemory(tableDef, sizeof(RTFTable));
537 tableDef->parent = info->tableDef;
538 info->tableDef = tableDef;
540 RTFFlushOutputBuffer(info);
541 if (tableDef->tableRowStart &&
542 tableDef->tableRowStart->member.para.nFlags & MEPF_ROWEND)
544 ME_DisplayItem *para = tableDef->tableRowStart;
545 para = para->member.para.next_para;
546 para = ME_InsertTableRowStartAtParagraph(info->editor, para);
547 tableDef->tableRowStart = para;
548 } else {
549 ME_Cursor cursor;
550 WCHAR endl = '\r';
551 cursor = info->editor->pCursors[0];
552 if (cursor.nOffset || cursor.pRun->member.run.nCharOfs)
553 ME_InsertTextFromCursor(info->editor, 0, &endl, 1, info->style);
554 tableDef->tableRowStart = ME_InsertTableRowStartFromCursor(info->editor);
557 info->nestingLevel++;
559 info->canInheritInTbl = FALSE;
561 break;
562 case rtfInTable:
564 if (!info->editor->bEmulateVersion10) /* v4.1 */
566 if (info->nestingLevel < 1)
568 RTFTable *tableDef;
569 if (!info->tableDef)
571 info->tableDef = ALLOC_OBJ(RTFTable);
572 ZeroMemory(info->tableDef, sizeof(RTFTable));
574 tableDef = info->tableDef;
575 RTFFlushOutputBuffer(info);
576 if (tableDef->tableRowStart &&
577 tableDef->tableRowStart->member.para.nFlags & MEPF_ROWEND)
579 ME_DisplayItem *para = tableDef->tableRowStart;
580 para = para->member.para.next_para;
581 para = ME_InsertTableRowStartAtParagraph(info->editor, para);
582 tableDef->tableRowStart = para;
583 } else {
584 ME_Cursor cursor;
585 WCHAR endl = '\r';
586 cursor = info->editor->pCursors[0];
587 if (cursor.nOffset || cursor.pRun->member.run.nCharOfs)
588 ME_InsertTextFromCursor(info->editor, 0, &endl, 1, info->style);
589 tableDef->tableRowStart = ME_InsertTableRowStartFromCursor(info->editor);
591 info->nestingLevel = 1;
592 info->canInheritInTbl = TRUE;
594 return;
595 } else { /* v1.0 - v3.0 */
596 fmt.dwMask |= PFM_TABLE;
597 fmt.wEffects |= PFE_TABLE;
599 break;
601 case rtfFirstIndent:
602 ME_GetSelectionParaFormat(info->editor, &fmt);
603 fmt.dwMask = PFM_STARTINDENT | PFM_OFFSET;
604 fmt.dxStartIndent += fmt.dxOffset + info->rtfParam;
605 fmt.dxOffset = -info->rtfParam;
606 break;
607 case rtfLeftIndent:
608 ME_GetSelectionParaFormat(info->editor, &fmt);
609 fmt.dwMask = PFM_STARTINDENT;
610 fmt.dxStartIndent = info->rtfParam - fmt.dxOffset;
611 break;
612 case rtfRightIndent:
613 fmt.dwMask = PFM_RIGHTINDENT;
614 fmt.dxRightIndent = info->rtfParam;
615 break;
616 case rtfQuadLeft:
617 case rtfQuadJust:
618 fmt.dwMask = PFM_ALIGNMENT;
619 fmt.wAlignment = PFA_LEFT;
620 break;
621 case rtfQuadRight:
622 fmt.dwMask = PFM_ALIGNMENT;
623 fmt.wAlignment = PFA_RIGHT;
624 break;
625 case rtfQuadCenter:
626 fmt.dwMask = PFM_ALIGNMENT;
627 fmt.wAlignment = PFA_CENTER;
628 break;
629 case rtfTabPos:
630 ME_GetSelectionParaFormat(info->editor, &fmt);
631 if (!(fmt.dwMask & PFM_TABSTOPS))
633 fmt.cTabCount = 0;
635 if (fmt.cTabCount < MAX_TAB_STOPS && info->rtfParam < 0x1000000)
636 fmt.rgxTabs[fmt.cTabCount++] = info->rtfParam;
637 fmt.dwMask = PFM_TABSTOPS;
638 break;
639 case rtfKeep:
640 fmt.dwMask = PFM_KEEP;
641 fmt.wEffects = PFE_KEEP;
642 break;
643 case rtfNoWidowControl:
644 fmt.dwMask = PFM_NOWIDOWCONTROL;
645 fmt.wEffects = PFE_NOWIDOWCONTROL;
646 break;
647 case rtfKeepNext:
648 fmt.dwMask = PFM_KEEPNEXT;
649 fmt.wEffects = PFE_KEEPNEXT;
650 break;
651 case rtfSpaceAfter:
652 fmt.dwMask = PFM_SPACEAFTER;
653 fmt.dySpaceAfter = info->rtfParam;
654 break;
655 case rtfSpaceBefore:
656 fmt.dwMask = PFM_SPACEBEFORE;
657 fmt.dySpaceBefore = info->rtfParam;
658 break;
659 case rtfSpaceBetween:
660 fmt.dwMask = PFM_LINESPACING;
661 if ((int)info->rtfParam > 0)
663 fmt.dyLineSpacing = info->rtfParam;
664 fmt.bLineSpacingRule = 3;
666 else
668 fmt.dyLineSpacing = info->rtfParam;
669 fmt.bLineSpacingRule = 4;
671 case rtfSpaceMultiply:
672 fmt.dwMask = PFM_LINESPACING;
673 fmt.dyLineSpacing = info->rtfParam * 20;
674 fmt.bLineSpacingRule = 5;
675 break;
676 case rtfParBullet:
677 fmt.dwMask = PFM_NUMBERING;
678 fmt.wNumbering = PFN_BULLET;
679 break;
680 case rtfParSimple:
681 fmt.dwMask = PFM_NUMBERING;
682 fmt.wNumbering = 2; /* FIXME: MSDN says it's not used ?? */
683 break;
684 case rtfParNumDecimal:
685 fmt.dwMask = PFM_NUMBERING;
686 fmt.wNumbering = 2; /* FIXME: MSDN says it's not used ?? */
687 break;
688 case rtfParNumIndent:
689 fmt.dwMask = PFM_NUMBERINGTAB;
690 fmt.wNumberingTab = info->rtfParam;
691 break;
692 case rtfParNumStartAt:
693 fmt.dwMask = PFM_NUMBERINGSTART;
694 fmt.wNumberingStart = info->rtfParam;
695 break;
696 case rtfBorderLeft:
697 info->borderType = RTFBorderParaLeft;
698 ME_GetSelectionParaFormat(info->editor, &fmt);
699 if (!(fmt.dwMask & PFM_BORDER))
701 fmt.wBorderSpace = 0;
702 fmt.wBorderWidth = 1;
703 fmt.wBorders = 0;
705 fmt.wBorders |= 1;
706 fmt.dwMask = PFM_BORDER;
707 break;
708 case rtfBorderRight:
709 info->borderType = RTFBorderParaRight;
710 ME_GetSelectionParaFormat(info->editor, &fmt);
711 if (!(fmt.dwMask & PFM_BORDER))
713 fmt.wBorderSpace = 0;
714 fmt.wBorderWidth = 1;
715 fmt.wBorders = 0;
717 fmt.wBorders |= 2;
718 fmt.dwMask = PFM_BORDER;
719 break;
720 case rtfBorderTop:
721 info->borderType = RTFBorderParaTop;
722 ME_GetSelectionParaFormat(info->editor, &fmt);
723 if (!(fmt.dwMask & PFM_BORDER))
725 fmt.wBorderSpace = 0;
726 fmt.wBorderWidth = 1;
727 fmt.wBorders = 0;
729 fmt.wBorders |= 4;
730 fmt.dwMask = PFM_BORDER;
731 break;
732 case rtfBorderBottom:
733 info->borderType = RTFBorderParaBottom;
734 ME_GetSelectionParaFormat(info->editor, &fmt);
735 if (!(fmt.dwMask & PFM_BORDER))
737 fmt.wBorderSpace = 0;
738 fmt.wBorderWidth = 1;
739 fmt.wBorders = 0;
741 fmt.wBorders |= 8;
742 fmt.dwMask = PFM_BORDER;
743 break;
744 case rtfBorderSingle:
745 ME_GetSelectionParaFormat(info->editor, &fmt);
746 /* we assume that borders have been created before (RTF spec) */
747 fmt.wBorders &= ~0x700;
748 fmt.wBorders |= 1 << 8;
749 fmt.dwMask = PFM_BORDER;
750 break;
751 case rtfBorderThick:
752 ME_GetSelectionParaFormat(info->editor, &fmt);
753 /* we assume that borders have been created before (RTF spec) */
754 fmt.wBorders &= ~0x700;
755 fmt.wBorders |= 2 << 8;
756 fmt.dwMask = PFM_BORDER;
757 break;
758 case rtfBorderShadow:
759 ME_GetSelectionParaFormat(info->editor, &fmt);
760 /* we assume that borders have been created before (RTF spec) */
761 fmt.wBorders &= ~0x700;
762 fmt.wBorders |= 10 << 8;
763 fmt.dwMask = PFM_BORDER;
764 break;
765 case rtfBorderDouble:
766 ME_GetSelectionParaFormat(info->editor, &fmt);
767 /* we assume that borders have been created before (RTF spec) */
768 fmt.wBorders &= ~0x700;
769 fmt.wBorders |= 7 << 8;
770 fmt.dwMask = PFM_BORDER;
771 break;
772 case rtfBorderDot:
773 ME_GetSelectionParaFormat(info->editor, &fmt);
774 /* we assume that borders have been created before (RTF spec) */
775 fmt.wBorders &= ~0x700;
776 fmt.wBorders |= 11 << 8;
777 fmt.dwMask = PFM_BORDER;
778 break;
779 case rtfBorderWidth:
781 int borderSide = info->borderType & RTFBorderSideMask;
782 RTFTable *tableDef = info->tableDef;
783 ME_GetSelectionParaFormat(info->editor, &fmt);
784 /* we assume that borders have been created before (RTF spec) */
785 fmt.wBorderWidth |= ((info->rtfParam / 15) & 7) << 8;
786 if ((info->borderType & RTFBorderTypeMask) == RTFBorderTypeCell)
788 RTFBorder *border;
789 if (!tableDef || tableDef->numCellsDefined >= MAX_TABLE_CELLS)
790 break;
791 border = &tableDef->cells[tableDef->numCellsDefined].border[borderSide];
792 border->width = info->rtfParam;
793 break;
795 fmt.dwMask = PFM_BORDER;
796 break;
798 case rtfBorderSpace:
799 ME_GetSelectionParaFormat(info->editor, &fmt);
800 /* we assume that borders have been created before (RTF spec) */
801 fmt.wBorderSpace = info->rtfParam;
802 fmt.dwMask = PFM_BORDER;
803 break;
804 case rtfBorderColor:
806 RTFTable *tableDef = info->tableDef;
807 int borderSide = info->borderType & RTFBorderSideMask;
808 int borderType = info->borderType & RTFBorderTypeMask;
809 switch(borderType)
811 case RTFBorderTypePara:
812 if (!info->editor->bEmulateVersion10) /* v4.1 */
813 break;
814 /* v1.0 - 3.0 treat paragraph and row borders the same. */
815 case RTFBorderTypeRow:
816 if (tableDef) {
817 tableDef->border[borderSide].color = info->rtfParam;
819 break;
820 case RTFBorderTypeCell:
821 if (tableDef && tableDef->numCellsDefined < MAX_TABLE_CELLS) {
822 tableDef->cells[tableDef->numCellsDefined].border[borderSide].color = info->rtfParam;
824 break;
826 break;
829 if (fmt.dwMask) {
830 RTFFlushOutputBuffer(info);
831 /* FIXME too slow ? how come ?*/
832 ME_SetSelectionParaFormat(info->editor, &fmt);
836 void ME_RTFTblAttrHook(RTF_Info *info)
838 switch (info->rtfMinor)
840 case rtfRowDef:
842 if (!info->editor->bEmulateVersion10) /* v4.1 */
843 info->borderType = 0; /* Not sure */
844 else /* v1.0 - 3.0 */
845 info->borderType = RTFBorderRowTop;
846 if (!info->tableDef) {
847 info->tableDef = ME_MakeTableDef(info->editor);
848 } else {
849 ME_InitTableDef(info->editor, info->tableDef);
851 break;
853 case rtfCellPos:
855 int cellNum;
856 if (!info->tableDef)
858 info->tableDef = ME_MakeTableDef(info->editor);
860 cellNum = info->tableDef->numCellsDefined;
861 if (cellNum >= MAX_TABLE_CELLS)
862 break;
863 info->tableDef->cells[cellNum].rightBoundary = info->rtfParam;
864 if (cellNum < MAX_TAB_STOPS) {
865 /* Tab stops were used to store cell positions before v4.1 but v4.1
866 * still seems to set the tabstops without using them. */
867 ME_DisplayItem *para = info->editor->pCursors[0].pPara;
868 PARAFORMAT2 *pFmt = para->member.para.pFmt;
869 pFmt->rgxTabs[cellNum] &= ~0x00FFFFFF;
870 pFmt->rgxTabs[cellNum] = 0x00FFFFFF & info->rtfParam;
872 info->tableDef->numCellsDefined++;
873 break;
875 case rtfRowBordTop:
876 info->borderType = RTFBorderRowTop;
877 break;
878 case rtfRowBordLeft:
879 info->borderType = RTFBorderRowLeft;
880 break;
881 case rtfRowBordBottom:
882 info->borderType = RTFBorderRowBottom;
883 break;
884 case rtfRowBordRight:
885 info->borderType = RTFBorderRowRight;
886 break;
887 case rtfCellBordTop:
888 info->borderType = RTFBorderCellTop;
889 break;
890 case rtfCellBordLeft:
891 info->borderType = RTFBorderCellLeft;
892 break;
893 case rtfCellBordBottom:
894 info->borderType = RTFBorderCellBottom;
895 break;
896 case rtfCellBordRight:
897 info->borderType = RTFBorderCellRight;
898 break;
899 case rtfRowGapH:
900 if (info->tableDef)
901 info->tableDef->gapH = info->rtfParam;
902 break;
903 case rtfRowLeftEdge:
904 if (info->tableDef)
905 info->tableDef->leftEdge = info->rtfParam;
906 break;
910 void ME_RTFSpecialCharHook(RTF_Info *info)
912 RTFTable *tableDef = info->tableDef;
913 switch (info->rtfMinor)
915 case rtfNestCell:
916 if (info->editor->bEmulateVersion10) /* v1.0 - v3.0 */
917 break;
918 /* else fall through since v4.1 treats rtfNestCell and rtfCell the same */
919 case rtfCell:
920 if (!tableDef)
921 break;
922 RTFFlushOutputBuffer(info);
923 if (!info->editor->bEmulateVersion10) { /* v4.1 */
924 if (tableDef->tableRowStart)
926 if (!info->nestingLevel &&
927 tableDef->tableRowStart->member.para.nFlags & MEPF_ROWEND)
929 ME_DisplayItem *para = tableDef->tableRowStart;
930 para = para->member.para.next_para;
931 para = ME_InsertTableRowStartAtParagraph(info->editor, para);
932 tableDef->tableRowStart = para;
933 info->nestingLevel = 1;
935 ME_InsertTableCellFromCursor(info->editor);
937 } else { /* v1.0 - v3.0 */
938 ME_DisplayItem *para = info->editor->pCursors[0].pPara;
939 PARAFORMAT2 *pFmt = para->member.para.pFmt;
940 if (pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE &&
941 tableDef->numCellsInserted < tableDef->numCellsDefined)
943 WCHAR tab = '\t';
944 ME_InsertTextFromCursor(info->editor, 0, &tab, 1, info->style);
945 tableDef->numCellsInserted++;
948 break;
949 case rtfNestRow:
950 if (info->editor->bEmulateVersion10) /* v1.0 - v3.0 */
951 break;
952 /* else fall through since v4.1 treats rtfNestRow and rtfRow the same */
953 case rtfRow:
955 ME_DisplayItem *para, *cell, *run;
956 int i;
958 if (!tableDef)
959 break;
960 RTFFlushOutputBuffer(info);
961 if (!info->editor->bEmulateVersion10) { /* v4.1 */
962 if (!tableDef->tableRowStart)
963 break;
964 if (!info->nestingLevel &&
965 tableDef->tableRowStart->member.para.nFlags & MEPF_ROWEND)
967 para = tableDef->tableRowStart;
968 para = para->member.para.next_para;
969 para = ME_InsertTableRowStartAtParagraph(info->editor, para);
970 tableDef->tableRowStart = para;
971 info->nestingLevel++;
973 para = tableDef->tableRowStart;
974 cell = ME_FindItemFwd(para, diCell);
975 assert(cell && !cell->member.cell.prev_cell);
976 if (tableDef->numCellsDefined < 1)
978 /* 2000 twips appears to be the cell size that native richedit uses
979 * when no cell sizes are specified. */
980 const int defaultCellSize = 2000;
981 int nRightBoundary = defaultCellSize;
982 cell->member.cell.nRightBoundary = nRightBoundary;
983 while (cell->member.cell.next_cell) {
984 cell = cell->member.cell.next_cell;
985 nRightBoundary += defaultCellSize;
986 cell->member.cell.nRightBoundary = nRightBoundary;
988 para = ME_InsertTableCellFromCursor(info->editor);
989 cell = para->member.para.pCell;
990 cell->member.cell.nRightBoundary = nRightBoundary;
991 } else {
992 for (i = 0; i < tableDef->numCellsDefined; i++)
994 RTFCell *cellDef = &tableDef->cells[i];
995 cell->member.cell.nRightBoundary = cellDef->rightBoundary;
996 ME_ApplyBorderProperties(info, &cell->member.cell.border,
997 cellDef->border);
998 cell = cell->member.cell.next_cell;
999 if (!cell)
1001 para = ME_InsertTableCellFromCursor(info->editor);
1002 cell = para->member.para.pCell;
1005 /* Cell for table row delimiter is empty */
1006 cell->member.cell.nRightBoundary = tableDef->cells[i-1].rightBoundary;
1009 run = ME_FindItemFwd(cell, diRun);
1010 if (info->editor->pCursors[0].pRun != run ||
1011 info->editor->pCursors[0].nOffset)
1013 int nOfs, nChars;
1014 /* Delete inserted cells that aren't defined. */
1015 info->editor->pCursors[1].pRun = run;
1016 info->editor->pCursors[1].pPara = ME_GetParagraph(run);
1017 info->editor->pCursors[1].nOffset = 0;
1018 nOfs = ME_GetCursorOfs(info->editor, 1);
1019 nChars = ME_GetCursorOfs(info->editor, 0) - nOfs;
1020 ME_InternalDeleteText(info->editor, nOfs, nChars, TRUE);
1023 para = ME_InsertTableRowEndFromCursor(info->editor);
1024 para->member.para.pFmt->dxOffset = abs(info->tableDef->gapH);
1025 para->member.para.pFmt->dxStartIndent = info->tableDef->leftEdge;
1026 ME_ApplyBorderProperties(info, &para->member.para.border,
1027 tableDef->border);
1028 info->nestingLevel--;
1029 if (!info->nestingLevel)
1031 if (info->canInheritInTbl) {
1032 tableDef->tableRowStart = para;
1033 } else {
1034 while (info->tableDef) {
1035 tableDef = info->tableDef;
1036 info->tableDef = tableDef->parent;
1037 heap_free(tableDef);
1040 } else {
1041 info->tableDef = tableDef->parent;
1042 heap_free(tableDef);
1044 } else { /* v1.0 - v3.0 */
1045 WCHAR endl = '\r';
1046 ME_DisplayItem *para = info->editor->pCursors[0].pPara;
1047 PARAFORMAT2 *pFmt = para->member.para.pFmt;
1048 pFmt->dxOffset = info->tableDef->gapH;
1049 pFmt->dxStartIndent = info->tableDef->leftEdge;
1051 ME_ApplyBorderProperties(info, &para->member.para.border,
1052 tableDef->border);
1053 while (tableDef->numCellsInserted < tableDef->numCellsDefined)
1055 WCHAR tab = '\t';
1056 ME_InsertTextFromCursor(info->editor, 0, &tab, 1, info->style);
1057 tableDef->numCellsInserted++;
1059 pFmt->cTabCount = min(tableDef->numCellsDefined, MAX_TAB_STOPS);
1060 if (!tableDef->numCellsDefined)
1061 pFmt->wEffects &= ~PFE_TABLE;
1062 ME_InsertTextFromCursor(info->editor, 0, &endl, 1, info->style);
1063 tableDef->numCellsInserted = 0;
1065 break;
1067 case rtfTab:
1068 case rtfPar:
1069 if (info->editor->bEmulateVersion10) { /* v1.0 - 3.0 */
1070 ME_DisplayItem *para;
1071 PARAFORMAT2 *pFmt;
1072 RTFFlushOutputBuffer(info);
1073 para = info->editor->pCursors[0].pPara;
1074 pFmt = para->member.para.pFmt;
1075 if (pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE)
1077 /* rtfPar is treated like a space within a table. */
1078 info->rtfClass = rtfText;
1079 info->rtfMajor = ' ';
1081 else if (info->rtfMinor == rtfPar && tableDef)
1082 tableDef->numCellsInserted = 0;
1084 break;
1088 static BOOL ME_RTFInsertOleObject(RTF_Info *info, HENHMETAFILE hemf, HBITMAP hbmp,
1089 const SIZEL* sz)
1091 LPOLEOBJECT lpObject = NULL;
1092 LPSTORAGE lpStorage = NULL;
1093 LPOLECLIENTSITE lpClientSite = NULL;
1094 LPDATAOBJECT lpDataObject = NULL;
1095 LPOLECACHE lpOleCache = NULL;
1096 STGMEDIUM stgm;
1097 FORMATETC fm;
1098 CLSID clsid;
1099 BOOL ret = FALSE;
1100 DWORD conn;
1102 if (hemf)
1104 stgm.tymed = TYMED_ENHMF;
1105 stgm.u.hEnhMetaFile = hemf;
1106 fm.cfFormat = CF_ENHMETAFILE;
1108 else if (hbmp)
1110 stgm.tymed = TYMED_GDI;
1111 stgm.u.hBitmap = hbmp;
1112 fm.cfFormat = CF_BITMAP;
1114 stgm.pUnkForRelease = NULL;
1116 fm.ptd = NULL;
1117 fm.dwAspect = DVASPECT_CONTENT;
1118 fm.lindex = -1;
1119 fm.tymed = stgm.tymed;
1121 if (!info->lpRichEditOle)
1123 CreateIRichEditOle(info->editor, (VOID**)&info->lpRichEditOle);
1126 if (OleCreateDefaultHandler(&CLSID_NULL, NULL, &IID_IOleObject, (void**)&lpObject) == S_OK &&
1127 #if 0
1128 /* FIXME: enable it when rich-edit properly implements this method */
1129 IRichEditOle_GetClientSite(info->lpRichEditOle, &lpClientSite) == S_OK &&
1130 IOleObject_SetClientSite(lpObject, lpClientSite) == S_OK &&
1131 #endif
1132 IOleObject_GetUserClassID(lpObject, &clsid) == S_OK &&
1133 IOleObject_QueryInterface(lpObject, &IID_IOleCache, (void**)&lpOleCache) == S_OK &&
1134 IOleCache_Cache(lpOleCache, &fm, 0, &conn) == S_OK &&
1135 IOleObject_QueryInterface(lpObject, &IID_IDataObject, (void**)&lpDataObject) == S_OK &&
1136 IDataObject_SetData(lpDataObject, &fm, &stgm, TRUE) == S_OK)
1138 REOBJECT reobject;
1140 reobject.cbStruct = sizeof(reobject);
1141 reobject.cp = REO_CP_SELECTION;
1142 reobject.clsid = clsid;
1143 reobject.poleobj = lpObject;
1144 reobject.pstg = lpStorage;
1145 reobject.polesite = lpClientSite;
1146 /* convert from twips to .01 mm */
1147 reobject.sizel.cx = MulDiv(sz->cx, 254, 144);
1148 reobject.sizel.cy = MulDiv(sz->cy, 254, 144);
1149 reobject.dvaspect = DVASPECT_CONTENT;
1150 reobject.dwFlags = 0; /* FIXME */
1151 reobject.dwUser = 0;
1153 /* FIXME: could be simpler */
1154 ret = IRichEditOle_InsertObject(info->lpRichEditOle, &reobject) == S_OK;
1157 if (lpObject) IOleObject_Release(lpObject);
1158 if (lpClientSite) IOleClientSite_Release(lpClientSite);
1159 if (lpStorage) IStorage_Release(lpStorage);
1160 if (lpDataObject) IDataObject_Release(lpDataObject);
1161 if (lpOleCache) IOleCache_Release(lpOleCache);
1163 return ret;
1166 static void ME_RTFReadPictGroup(RTF_Info *info)
1168 SIZEL sz;
1169 BYTE* buffer = NULL;
1170 unsigned bufsz, bufidx;
1171 BOOL flip;
1172 BYTE val;
1173 METAFILEPICT mfp;
1174 HENHMETAFILE hemf;
1175 HBITMAP hbmp;
1176 enum gfxkind {gfx_unknown = 0, gfx_enhmetafile, gfx_metafile, gfx_dib} gfx = gfx_unknown;
1178 RTFGetToken (info);
1179 if (info->rtfClass == rtfEOF)
1180 return;
1181 mfp.mm = MM_TEXT;
1182 /* fetch picture type */
1183 if (RTFCheckMM (info, rtfPictAttr, rtfWinMetafile))
1185 mfp.mm = info->rtfParam;
1186 gfx = gfx_metafile;
1188 else if (RTFCheckMM (info, rtfPictAttr, rtfDevIndBitmap))
1190 if (info->rtfParam != 0) FIXME("dibitmap should be 0 (%d)\n", info->rtfParam);
1191 gfx = gfx_dib;
1193 else if (RTFCheckMM (info, rtfPictAttr, rtfEmfBlip))
1195 gfx = gfx_enhmetafile;
1197 else
1199 FIXME("%d %d\n", info->rtfMajor, info->rtfMinor);
1200 goto skip_group;
1202 sz.cx = sz.cy = 0;
1203 /* fetch picture attributes */
1204 for (;;)
1206 RTFGetToken (info);
1207 if (info->rtfClass == rtfEOF)
1208 return;
1209 if (info->rtfClass == rtfText)
1210 break;
1211 if (!RTFCheckCM (info, rtfControl, rtfPictAttr))
1213 ERR("Expected picture attribute (%d %d)\n",
1214 info->rtfClass, info->rtfMajor);
1215 goto skip_group;
1217 else if (RTFCheckMM (info, rtfPictAttr, rtfPicWid))
1219 if (gfx == gfx_metafile) mfp.xExt = info->rtfParam;
1221 else if (RTFCheckMM (info, rtfPictAttr, rtfPicHt))
1223 if (gfx == gfx_metafile) mfp.yExt = info->rtfParam;
1225 else if (RTFCheckMM (info, rtfPictAttr, rtfPicGoalWid))
1226 sz.cx = info->rtfParam;
1227 else if (RTFCheckMM (info, rtfPictAttr, rtfPicGoalHt))
1228 sz.cy = info->rtfParam;
1229 else
1230 FIXME("Non supported attribute: %d %d %d\n", info->rtfClass, info->rtfMajor, info->rtfMinor);
1232 /* fetch picture data */
1233 bufsz = 1024;
1234 bufidx = 0;
1235 buffer = HeapAlloc(GetProcessHeap(), 0, bufsz);
1236 val = info->rtfMajor;
1237 for (flip = TRUE;; flip = !flip)
1239 RTFGetToken (info);
1240 if (info->rtfClass == rtfEOF)
1242 HeapFree(GetProcessHeap(), 0, buffer);
1243 return; /* Warn ?? */
1245 if (RTFCheckCM(info, rtfGroup, rtfEndGroup))
1246 break;
1247 if (info->rtfClass != rtfText) goto skip_group;
1248 if (flip)
1250 if (bufidx >= bufsz &&
1251 !(buffer = HeapReAlloc(GetProcessHeap(), 0, buffer, bufsz += 1024)))
1252 goto skip_group;
1253 buffer[bufidx++] = RTFCharToHex(val) * 16 + RTFCharToHex(info->rtfMajor);
1255 else
1256 val = info->rtfMajor;
1258 if (flip) FIXME("wrong hex string\n");
1260 switch (gfx)
1262 case gfx_enhmetafile:
1263 if ((hemf = SetEnhMetaFileBits(bufidx, buffer)))
1264 ME_RTFInsertOleObject(info, hemf, NULL, &sz);
1265 break;
1266 case gfx_metafile:
1267 if ((hemf = SetWinMetaFileBits(bufidx, buffer, NULL, &mfp)))
1268 ME_RTFInsertOleObject(info, hemf, NULL, &sz);
1269 break;
1270 case gfx_dib:
1272 BITMAPINFO* bi = (BITMAPINFO*)buffer;
1273 HDC hdc = GetDC(0);
1274 unsigned nc = bi->bmiHeader.biClrUsed;
1276 /* not quite right, especially for bitfields type of compression */
1277 if (!nc && bi->bmiHeader.biBitCount <= 8)
1278 nc = 1 << bi->bmiHeader.biBitCount;
1279 if ((hbmp = CreateDIBitmap(hdc, &bi->bmiHeader,
1280 CBM_INIT, (char*)(bi + 1) + nc * sizeof(RGBQUAD),
1281 bi, DIB_RGB_COLORS)))
1282 ME_RTFInsertOleObject(info, NULL, hbmp, &sz);
1283 ReleaseDC(0, hdc);
1285 break;
1286 default:
1287 break;
1289 HeapFree(GetProcessHeap(), 0, buffer);
1290 RTFRouteToken (info); /* feed "}" back to router */
1291 return;
1292 skip_group:
1293 HeapFree(GetProcessHeap(), 0, buffer);
1294 RTFSkipGroup(info);
1295 RTFRouteToken(info); /* feed "}" back to router */
1298 /* for now, lookup the \result part and use it, whatever the object */
1299 static void ME_RTFReadObjectGroup(RTF_Info *info)
1301 for (;;)
1303 RTFGetToken (info);
1304 if (info->rtfClass == rtfEOF)
1305 return;
1306 if (RTFCheckCM(info, rtfGroup, rtfEndGroup))
1307 break;
1308 if (RTFCheckCM(info, rtfGroup, rtfBeginGroup))
1310 RTFGetToken (info);
1311 if (info->rtfClass == rtfEOF)
1312 return;
1313 if (RTFCheckCMM(info, rtfControl, rtfDestination, rtfObjResult))
1315 int level = 1;
1317 while (RTFGetToken (info) != rtfEOF)
1319 if (info->rtfClass == rtfGroup)
1321 if (info->rtfMajor == rtfBeginGroup) level++;
1322 else if (info->rtfMajor == rtfEndGroup && --level < 0) break;
1324 RTFRouteToken(info);
1327 else RTFSkipGroup(info);
1328 continue;
1330 if (!RTFCheckCM (info, rtfControl, rtfObjAttr))
1332 FIXME("Non supported attribute: %d %d %d\n", info->rtfClass, info->rtfMajor, info->rtfMinor);
1333 return;
1336 RTFRouteToken(info); /* feed "}" back to router */
1339 static void ME_RTFReadHook(RTF_Info *info)
1341 switch(info->rtfClass)
1343 case rtfGroup:
1344 switch(info->rtfMajor)
1346 case rtfBeginGroup:
1347 if (info->stackTop < maxStack) {
1348 info->stack[info->stackTop].style = info->style;
1349 ME_AddRefStyle(info->style);
1350 info->stack[info->stackTop].codePage = info->codePage;
1351 info->stack[info->stackTop].unicodeLength = info->unicodeLength;
1353 info->stackTop++;
1354 info->styleChanged = FALSE;
1355 break;
1356 case rtfEndGroup:
1358 RTFFlushOutputBuffer(info);
1359 info->stackTop--;
1360 if (info->stackTop<=0) {
1361 info->rtfClass = rtfEOF;
1362 return;
1364 assert(info->stackTop >= 0);
1366 ME_ReleaseStyle(info->style);
1367 info->style = info->stack[info->stackTop].style;
1368 ME_AddRefStyle(info->style);
1369 info->codePage = info->stack[info->stackTop].codePage;
1370 info->unicodeLength = info->stack[info->stackTop].unicodeLength;
1371 break;
1374 break;
1378 void
1379 ME_StreamInFill(ME_InStream *stream)
1381 stream->editstream->dwError = stream->editstream->pfnCallback(stream->editstream->dwCookie,
1382 (BYTE *)stream->buffer,
1383 sizeof(stream->buffer),
1384 (LONG *)&stream->dwSize);
1385 stream->dwUsed = 0;
1388 static LRESULT ME_StreamIn(ME_TextEditor *editor, DWORD format, EDITSTREAM *stream, BOOL stripLastCR)
1390 RTF_Info parser;
1391 ME_Style *style;
1392 int from, to, to2, nUndoMode;
1393 int nEventMask = editor->nEventMask;
1394 ME_InStream inStream;
1395 BOOL invalidRTF = FALSE;
1397 TRACE("stream==%p editor==%p format==0x%X\n", stream, editor, format);
1398 editor->nEventMask = 0;
1400 ME_GetSelection(editor, &from, &to);
1401 if ((format & SFF_SELECTION) && (editor->mode & TM_RICHTEXT)) {
1402 style = ME_GetSelectionInsertStyle(editor);
1404 ME_InternalDeleteText(editor, from, 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;
1430 else {
1431 style = editor->pBuffer->pDefaultStyle;
1432 ME_AddRefStyle(style);
1433 ME_SetSelection(editor, 0, 0);
1434 ME_InternalDeleteText(editor, 0, 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 *pCell;
1499 ME_DisplayItem *para;
1501 parser.rtfMinor = rtfRow;
1502 /* Complete the table row before deleting it.
1503 * By doing it this way we will have the current paragraph format set
1504 * properly to reflect that is not in the complete table, and undo items
1505 * will be added for this change to the current paragraph format. */
1506 if (parser.nestingLevel > 0)
1508 while (parser.nestingLevel > 1)
1509 ME_RTFSpecialCharHook(&parser); /* Decrements nestingLevel */
1510 para = parser.tableDef->tableRowStart;
1511 ME_RTFSpecialCharHook(&parser);
1512 } else {
1513 para = parser.tableDef->tableRowStart;
1514 ME_RTFSpecialCharHook(&parser);
1515 assert(para->member.para.nFlags & MEPF_ROWEND);
1516 para = para->member.para.next_para;
1518 pCell = para->member.para.pCell;
1520 editor->pCursors[1].pPara = para;
1521 editor->pCursors[1].pRun = ME_FindItemFwd(para, diRun);
1522 editor->pCursors[1].nOffset = 0;
1523 nOfs = ME_GetCursorOfs(editor, 1);
1524 nChars = ME_GetCursorOfs(editor, 0) - nOfs;
1525 ME_InternalDeleteText(editor, nOfs, nChars, TRUE);
1526 if (parser.tableDef)
1527 parser.tableDef->tableRowStart = NULL;
1530 ME_CheckTablesForCorruption(editor);
1531 RTFDestroy(&parser);
1532 if (parser.lpRichEditOle)
1533 IRichEditOle_Release(parser.lpRichEditOle);
1535 if (!inStream.editstream->dwError && parser.stackTop > 0)
1536 inStream.editstream->dwError = HRESULT_FROM_WIN32(ERROR_HANDLE_EOF);
1538 /* Remove last line break, as mandated by tests. This is not affected by
1539 CR/LF counters, since RTF streaming presents only \para tokens, which
1540 are converted according to the standard rules: \r for 2.0, \r\n for 1.0
1542 if (stripLastCR) {
1543 int newfrom, newto;
1544 ME_GetSelection(editor, &newfrom, &newto);
1545 if (newto > to + (editor->bEmulateVersion10 ? 1 : 0)) {
1546 WCHAR lastchar[3] = {'\0', '\0'};
1547 int linebreakSize = editor->bEmulateVersion10 ? 2 : 1;
1549 ME_GetTextW(editor, lastchar, newto - linebreakSize, linebreakSize, 0);
1550 if (lastchar[0] == '\r' && (lastchar[1] == '\n' || lastchar[1] == '\0')) {
1551 ME_InternalDeleteText(editor, newto - linebreakSize, linebreakSize, FALSE);
1556 style = parser.style;
1558 else if (format & SF_TEXT)
1559 ME_StreamInText(editor, format, &inStream, style);
1560 else
1561 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
1562 ME_GetSelection(editor, &to, &to2);
1563 /* put the cursor at the top */
1564 if (!(format & SFF_SELECTION))
1565 ME_SetSelection(editor, 0, 0);
1568 /* Restore saved undo mode */
1569 editor->nUndoMode = nUndoMode;
1571 /* even if we didn't add an undo, we need to commit anything on the stack */
1572 ME_CommitUndo(editor);
1574 /* If SFF_SELECTION isn't set, delete any undos from before we started too */
1575 if (!(format & SFF_SELECTION))
1576 ME_EmptyUndoStack(editor);
1578 ME_ReleaseStyle(style);
1579 editor->nEventMask = nEventMask;
1580 ME_UpdateRepaint(editor);
1581 if (!(format & SFF_SELECTION)) {
1582 ME_ClearTempStyle(editor);
1584 ITextHost_TxShowCaret(editor->texthost, FALSE);
1585 ME_MoveCaret(editor);
1586 ITextHost_TxShowCaret(editor->texthost, TRUE);
1587 ME_SendSelChange(editor);
1588 ME_SendRequestResize(editor, FALSE);
1590 return 0;
1594 typedef struct tagME_RTFStringStreamStruct
1596 char *string;
1597 int pos;
1598 int length;
1599 } ME_RTFStringStreamStruct;
1601 static DWORD CALLBACK ME_ReadFromRTFString(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1603 ME_RTFStringStreamStruct *pStruct = (ME_RTFStringStreamStruct *)dwCookie;
1604 int count;
1606 count = min(cb, pStruct->length - pStruct->pos);
1607 memmove(lpBuff, pStruct->string + pStruct->pos, count);
1608 pStruct->pos += count;
1609 *pcb = count;
1610 return 0;
1613 static void
1614 ME_StreamInRTFString(ME_TextEditor *editor, BOOL selection, char *string)
1616 EDITSTREAM es;
1617 ME_RTFStringStreamStruct data;
1619 data.string = string;
1620 data.length = strlen(string);
1621 data.pos = 0;
1622 es.dwCookie = (DWORD_PTR)&data;
1623 es.pfnCallback = ME_ReadFromRTFString;
1624 ME_StreamIn(editor, SF_RTF | (selection ? SFF_SELECTION : 0), &es, FALSE);
1628 static int
1629 ME_FindText(ME_TextEditor *editor, DWORD flags, const CHARRANGE *chrg, const WCHAR *text, CHARRANGE *chrgText)
1631 const int nLen = lstrlenW(text);
1632 const int nTextLen = ME_GetTextLength(editor);
1633 int nStart, nEnd;
1634 int nMin, nMax;
1635 ME_DisplayItem *item;
1636 ME_DisplayItem *para;
1637 WCHAR wLastChar = ' ';
1639 TRACE("flags==0x%08x, chrg->cpMin==%d, chrg->cpMax==%d text==%s\n",
1640 flags, chrg->cpMin, chrg->cpMax, debugstr_w(text));
1642 if (flags & ~(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD))
1643 FIXME("Flags 0x%08x not implemented\n",
1644 flags & ~(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD));
1646 nMin = chrg->cpMin;
1647 if (chrg->cpMax == -1)
1648 nMax = nTextLen;
1649 else
1650 nMax = chrg->cpMax > nTextLen ? nTextLen : chrg->cpMax;
1652 /* In 1.0 emulation, if cpMax reaches end of text, add the FR_DOWN flag */
1653 if (editor->bEmulateVersion10 && nMax == nTextLen)
1655 flags |= FR_DOWN;
1658 /* In 1.0 emulation, cpMin must always be no greater than cpMax */
1659 if (editor->bEmulateVersion10 && nMax < nMin)
1661 if (chrgText)
1663 chrgText->cpMin = -1;
1664 chrgText->cpMax = -1;
1666 return -1;
1669 /* when searching up, if cpMin < cpMax, then instead of searching
1670 * on [cpMin,cpMax], we search on [0,cpMin], otherwise, search on
1671 * [cpMax, cpMin]. The exception is when cpMax is -1, in which
1672 * case, it is always bigger than cpMin.
1674 if (!editor->bEmulateVersion10 && !(flags & FR_DOWN))
1676 int nSwap = nMax;
1678 nMax = nMin > nTextLen ? nTextLen : nMin;
1679 if (nMin < nSwap || chrg->cpMax == -1)
1680 nMin = 0;
1681 else
1682 nMin = nSwap;
1685 if (!nLen || nMin < 0 || nMax < 0 || nMax < nMin)
1687 if (chrgText)
1688 chrgText->cpMin = chrgText->cpMax = -1;
1689 return -1;
1692 if (flags & FR_DOWN) /* Forward search */
1694 /* If possible, find the character before where the search starts */
1695 if ((flags & FR_WHOLEWORD) && nMin)
1697 ME_RunOfsFromCharOfs(editor, nMin - 1, NULL, &item, &nStart);
1698 wLastChar = item->member.run.strText->szData[nStart];
1701 ME_RunOfsFromCharOfs(editor, nMin, &para, &item, &nStart);
1703 while (item
1704 && para->member.para.nCharOfs + item->member.run.nCharOfs + nStart + nLen <= nMax)
1706 ME_DisplayItem *pCurItem = item;
1707 int nCurStart = nStart;
1708 int nMatched = 0;
1710 while (pCurItem && ME_CharCompare(pCurItem->member.run.strText->szData[nCurStart + nMatched], text[nMatched], (flags & FR_MATCHCASE)))
1712 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
1713 break;
1715 nMatched++;
1716 if (nMatched == nLen)
1718 ME_DisplayItem *pNextItem = pCurItem;
1719 int nNextStart = nCurStart;
1720 WCHAR wNextChar;
1722 /* Check to see if next character is a whitespace */
1723 if (flags & FR_WHOLEWORD)
1725 if (nCurStart + nMatched == pCurItem->member.run.strText->nLen)
1727 pNextItem = ME_FindItemFwd(pCurItem, diRun);
1728 nNextStart = -nMatched;
1731 if (pNextItem)
1732 wNextChar = pNextItem->member.run.strText->szData[nNextStart + nMatched];
1733 else
1734 wNextChar = ' ';
1736 if (isalnumW(wNextChar))
1737 break;
1740 nStart += para->member.para.nCharOfs + item->member.run.nCharOfs;
1741 if (chrgText)
1743 chrgText->cpMin = nStart;
1744 chrgText->cpMax = nStart + nLen;
1746 TRACE("found at %d-%d\n", nStart, nStart + nLen);
1747 return nStart;
1749 if (nCurStart + nMatched == pCurItem->member.run.strText->nLen)
1751 pCurItem = ME_FindItemFwd(pCurItem, diRun);
1752 nCurStart = -nMatched;
1755 if (pCurItem)
1756 wLastChar = pCurItem->member.run.strText->szData[nCurStart + nMatched];
1757 else
1758 wLastChar = ' ';
1760 nStart++;
1761 if (nStart == item->member.run.strText->nLen)
1763 item = ME_FindItemFwd(item, diRun);
1764 para = ME_GetParagraph(item);
1765 nStart = 0;
1769 else /* Backward search */
1771 /* If possible, find the character after where the search ends */
1772 if ((flags & FR_WHOLEWORD) && nMax < nTextLen - 1)
1774 ME_RunOfsFromCharOfs(editor, nMax + 1, NULL, &item, &nEnd);
1775 wLastChar = item->member.run.strText->szData[nEnd];
1778 ME_RunOfsFromCharOfs(editor, nMax, &para, &item, &nEnd);
1780 while (item
1781 && para->member.para.nCharOfs + item->member.run.nCharOfs + nEnd - nLen >= nMin)
1783 ME_DisplayItem *pCurItem = item;
1784 int nCurEnd = nEnd;
1785 int nMatched = 0;
1787 if (nCurEnd == 0)
1789 pCurItem = ME_FindItemBack(pCurItem, diRun);
1790 nCurEnd = pCurItem->member.run.strText->nLen + nMatched;
1793 while (pCurItem && ME_CharCompare(pCurItem->member.run.strText->szData[nCurEnd - nMatched - 1], text[nLen - nMatched - 1], (flags & FR_MATCHCASE)))
1795 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
1796 break;
1798 nMatched++;
1799 if (nMatched == nLen)
1801 ME_DisplayItem *pPrevItem = pCurItem;
1802 int nPrevEnd = nCurEnd;
1803 WCHAR wPrevChar;
1805 /* Check to see if previous character is a whitespace */
1806 if (flags & FR_WHOLEWORD)
1808 if (nPrevEnd - nMatched == 0)
1810 pPrevItem = ME_FindItemBack(pCurItem, diRun);
1811 if (pPrevItem)
1812 nPrevEnd = pPrevItem->member.run.strText->nLen + nMatched;
1815 if (pPrevItem)
1816 wPrevChar = pPrevItem->member.run.strText->szData[nPrevEnd - nMatched - 1];
1817 else
1818 wPrevChar = ' ';
1820 if (isalnumW(wPrevChar))
1821 break;
1824 nStart = ME_GetParagraph(pCurItem)->member.para.nCharOfs
1825 + pCurItem->member.run.nCharOfs + nCurEnd - nMatched;
1826 if (chrgText)
1828 chrgText->cpMin = nStart;
1829 chrgText->cpMax = nStart + nLen;
1831 TRACE("found at %d-%d\n", nStart, nStart + nLen);
1832 return nStart;
1834 if (nCurEnd - nMatched == 0)
1836 pCurItem = ME_FindItemBack(pCurItem, diRun);
1837 /* Don't care about pCurItem becoming NULL here; it's already taken
1838 * care of in the exterior loop condition */
1839 nCurEnd = pCurItem->member.run.strText->nLen + nMatched;
1842 if (pCurItem)
1843 wLastChar = pCurItem->member.run.strText->szData[nCurEnd - nMatched - 1];
1844 else
1845 wLastChar = ' ';
1847 nEnd--;
1848 if (nEnd < 0)
1850 item = ME_FindItemBack(item, diRun);
1851 para = ME_GetParagraph(item);
1852 nEnd = item->member.run.strText->nLen;
1856 TRACE("not found\n");
1857 if (chrgText)
1858 chrgText->cpMin = chrgText->cpMax = -1;
1859 return -1;
1862 static int ME_GetTextEx(ME_TextEditor *editor, GETTEXTEX *ex, LPARAM pText)
1864 int nStart, nCount; /* in chars */
1866 if (!ex->cb || !pText) return 0;
1868 if (ex->flags & ~(GT_SELECTION | GT_USECRLF))
1869 FIXME("GETTEXTEX flags 0x%08x not supported\n", ex->flags & ~(GT_SELECTION | GT_USECRLF));
1871 if (ex->flags & GT_SELECTION)
1873 ME_GetSelection(editor, &nStart, &nCount);
1874 nCount -= nStart;
1876 else
1878 nStart = 0;
1879 nCount = 0x7fffffff;
1881 if (ex->codepage == 1200)
1883 nCount = min(nCount, ex->cb / sizeof(WCHAR) - 1);
1884 return ME_GetTextW(editor, (LPWSTR)pText, nStart, nCount, ex->flags & GT_USECRLF);
1886 else
1888 /* potentially each char may be a CR, why calculate the exact value with O(N) when
1889 we can just take a bigger buffer? :)
1890 The above assumption still holds with CR/LF counters, since CR->CRLF expansion
1891 occurs only in richedit 2.0 mode, in which line breaks have only one CR
1893 int crlfmul = (ex->flags & GT_USECRLF) ? 2 : 1;
1894 LPWSTR buffer;
1895 DWORD buflen = ex->cb;
1896 LRESULT rc;
1897 DWORD flags = 0;
1899 nCount = min(nCount, ex->cb - 1);
1900 buffer = heap_alloc((crlfmul*nCount + 1) * sizeof(WCHAR));
1902 buflen = ME_GetTextW(editor, buffer, nStart, nCount, ex->flags & GT_USECRLF);
1903 rc = WideCharToMultiByte(ex->codepage, flags, buffer, buflen+1,
1904 (LPSTR)pText, ex->cb, ex->lpDefaultChar, ex->lpUsedDefChar);
1905 if (rc) rc--; /* do not count 0 terminator */
1907 heap_free(buffer);
1908 return rc;
1912 static int ME_GetTextRange(ME_TextEditor *editor, WCHAR *strText,
1913 int start, int nLen, BOOL unicode)
1915 if (unicode) {
1916 return ME_GetTextW(editor, strText, start, nLen, 0);
1917 } else {
1918 WCHAR *p = ALLOC_N_OBJ(WCHAR, nLen+1);
1919 int nChars = ME_GetTextW(editor, p, start, nLen, 0);
1920 WideCharToMultiByte(CP_ACP, 0, p, nChars+1, (char *)strText,
1921 nLen+1, NULL, NULL);
1922 FREE_OBJ(p);
1923 return nChars;
1927 typedef struct tagME_GlobalDestStruct
1929 HGLOBAL hData;
1930 int nLength;
1931 } ME_GlobalDestStruct;
1933 static DWORD CALLBACK ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1935 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
1936 int i;
1937 WORD *pSrc, *pDest;
1939 cb = cb >> 1;
1940 pDest = (WORD *)lpBuff;
1941 pSrc = GlobalLock(pData->hData);
1942 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
1943 pDest[i] = pSrc[pData->nLength+i];
1945 pData->nLength += i;
1946 *pcb = 2*i;
1947 GlobalUnlock(pData->hData);
1948 return 0;
1951 static DWORD CALLBACK ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1953 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
1954 int i;
1955 BYTE *pSrc, *pDest;
1957 pDest = lpBuff;
1958 pSrc = GlobalLock(pData->hData);
1959 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
1960 pDest[i] = pSrc[pData->nLength+i];
1962 pData->nLength += i;
1963 *pcb = i;
1964 GlobalUnlock(pData->hData);
1965 return 0;
1968 static BOOL ME_Paste(ME_TextEditor *editor)
1970 DWORD dwFormat = 0;
1971 EDITSTREAM es;
1972 ME_GlobalDestStruct gds;
1973 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
1974 UINT cf = 0;
1976 if (IsClipboardFormatAvailable(nRTFFormat))
1977 cf = nRTFFormat, dwFormat = SF_RTF;
1978 else if (IsClipboardFormatAvailable(CF_UNICODETEXT))
1979 cf = CF_UNICODETEXT, dwFormat = SF_TEXT|SF_UNICODE;
1980 else
1981 return FALSE;
1983 if (!OpenClipboard(editor->hWnd))
1984 return FALSE;
1985 gds.hData = GetClipboardData(cf);
1986 gds.nLength = 0;
1987 es.dwCookie = (DWORD_PTR)&gds;
1988 es.pfnCallback = dwFormat == SF_RTF ? ME_ReadFromHGLOBALRTF : ME_ReadFromHGLOBALUnicode;
1989 ME_StreamIn(editor, dwFormat|SFF_SELECTION, &es, FALSE);
1991 CloseClipboard();
1992 return TRUE;
1995 static BOOL ME_Copy(ME_TextEditor *editor, CHARRANGE *range)
1997 LPDATAOBJECT dataObj = NULL;
1998 HRESULT hr = S_OK;
2000 if (editor->cPasswordMask)
2001 return FALSE; /* Copying or Cutting masked text isn't allowed */
2003 if(editor->lpOleCallback)
2004 hr = IRichEditOleCallback_GetClipboardData(editor->lpOleCallback, range, RECO_COPY, &dataObj);
2005 if(FAILED(hr) || !dataObj)
2006 hr = ME_GetDataObject(editor, range, &dataObj);
2007 if(SUCCEEDED(hr)) {
2008 hr = OleSetClipboard(dataObj);
2009 IDataObject_Release(dataObj);
2011 return SUCCEEDED(hr) != 0;
2014 /* helper to send a msg filter notification */
2015 static BOOL
2016 ME_FilterEvent(ME_TextEditor *editor, UINT msg, WPARAM* wParam, LPARAM* lParam)
2018 MSGFILTER msgf;
2020 if (!editor->hWnd) return FALSE;
2021 msgf.nmhdr.hwndFrom = editor->hWnd;
2022 msgf.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
2023 msgf.nmhdr.code = EN_MSGFILTER;
2024 msgf.msg = msg;
2025 msgf.wParam = *wParam;
2026 msgf.lParam = *lParam;
2027 if (SendMessageW(GetParent(editor->hWnd), WM_NOTIFY, msgf.nmhdr.idFrom, (LPARAM)&msgf))
2028 return FALSE;
2029 *wParam = msgf.wParam;
2030 *lParam = msgf.lParam;
2031 msgf.wParam = *wParam;
2033 return TRUE;
2036 static void ME_UpdateSelectionLinkAttribute(ME_TextEditor *editor)
2038 ME_DisplayItem *startPara, *endPara;
2039 ME_DisplayItem *prev_para;
2040 int from, to;
2042 if (!editor->AutoURLDetect_bEnable) return;
2044 ME_GetSelection(editor, &from, &to);
2046 /* Find paragraph previous to the one that contains start cursor */
2047 ME_RunOfsFromCharOfs(editor, from, &startPara, NULL, NULL);
2048 prev_para = startPara->member.para.prev_para;
2049 if (prev_para->type == diParagraph) startPara = prev_para;
2051 /* Find paragraph that contains end cursor */
2052 ME_RunOfsFromCharOfs(editor, to, &endPara, NULL, NULL);
2053 endPara = endPara->member.para.next_para;
2055 ME_UpdateLinkAttribute(editor,
2056 startPara->member.para.nCharOfs,
2057 endPara->member.para.nCharOfs);
2060 static BOOL
2061 ME_KeyDown(ME_TextEditor *editor, WORD nKey)
2063 BOOL ctrl_is_down = GetKeyState(VK_CONTROL) & 0x8000;
2064 BOOL shift_is_down = GetKeyState(VK_SHIFT) & 0x8000;
2066 if (editor->bMouseCaptured)
2067 return FALSE;
2068 if (nKey != VK_SHIFT && nKey != VK_CONTROL && nKey != VK_MENU)
2069 editor->nSelectionType = stPosition;
2071 switch (nKey)
2073 case VK_LEFT:
2074 case VK_RIGHT:
2075 case VK_HOME:
2076 case VK_END:
2077 editor->nUDArrowX = -1;
2078 /* fall through */
2079 case VK_UP:
2080 case VK_DOWN:
2081 case VK_PRIOR:
2082 case VK_NEXT:
2083 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
2084 ME_ArrowKey(editor, nKey, shift_is_down, ctrl_is_down);
2085 return TRUE;
2086 case VK_BACK:
2087 case VK_DELETE:
2088 editor->nUDArrowX = -1;
2089 /* FIXME backspace and delete aren't the same, they act different wrt paragraph style of the merged paragraph */
2090 if (editor->styleFlags & ES_READONLY)
2091 return FALSE;
2092 if (ME_IsSelection(editor))
2094 ME_DeleteSelection(editor);
2095 ME_CommitUndo(editor);
2097 else if (nKey == VK_DELETE)
2099 /* Delete stops group typing.
2100 * (See MSDN remarks on EM_STOPGROUPTYPING message) */
2101 ME_DeleteTextAtCursor(editor, 1, 1);
2102 ME_CommitUndo(editor);
2104 else if (ME_ArrowKey(editor, VK_LEFT, FALSE, FALSE))
2106 BOOL bDeletionSucceeded;
2107 /* Backspace can be grouped for a single undo */
2108 ME_ContinueCoalescingTransaction(editor);
2109 bDeletionSucceeded = ME_DeleteTextAtCursor(editor, 1, 1);
2110 if (!bDeletionSucceeded && !editor->bEmulateVersion10) { /* v4.1 */
2111 /* Deletion was prevented so the cursor is moved back to where it was.
2112 * (e.g. this happens when trying to delete cell boundaries)
2114 ME_ArrowKey(editor, VK_RIGHT, FALSE, FALSE);
2116 ME_CommitCoalescingUndo(editor);
2118 else
2119 return TRUE;
2120 ME_MoveCursorFromTableRowStartParagraph(editor);
2121 ME_UpdateSelectionLinkAttribute(editor);
2122 ME_UpdateRepaint(editor);
2123 ME_SendRequestResize(editor, FALSE);
2124 return TRUE;
2125 case VK_RETURN:
2126 if (editor->styleFlags & ES_MULTILINE)
2128 ME_Cursor cursor = editor->pCursors[0];
2129 ME_DisplayItem *para = cursor.pPara;
2130 int from, to;
2131 const WCHAR endl = '\r';
2132 ME_Style *style;
2134 if (editor->styleFlags & ES_READONLY) {
2135 MessageBeep(MB_ICONERROR);
2136 return TRUE;
2139 ME_GetSelection(editor, &from, &to);
2140 if (editor->nTextLimit > ME_GetTextLength(editor) - (to-from))
2142 if (!editor->bEmulateVersion10) { /* v4.1 */
2143 if (para->member.para.nFlags & MEPF_ROWEND) {
2144 /* Add a new table row after this row. */
2145 para = ME_AppendTableRow(editor, para);
2146 para = para->member.para.next_para;
2147 editor->pCursors[0].pPara = para;
2148 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2149 editor->pCursors[0].nOffset = 0;
2150 editor->pCursors[1] = editor->pCursors[0];
2151 ME_CommitUndo(editor);
2152 ME_CheckTablesForCorruption(editor);
2153 ME_UpdateRepaint(editor);
2154 return TRUE;
2156 else if (para == editor->pCursors[1].pPara &&
2157 cursor.nOffset + cursor.pRun->member.run.nCharOfs == 0 &&
2158 para->member.para.prev_para->member.para.nFlags & MEPF_ROWSTART &&
2159 !para->member.para.prev_para->member.para.nCharOfs)
2161 /* Insert a newline before the table. */
2162 para = para->member.para.prev_para;
2163 para->member.para.nFlags &= ~MEPF_ROWSTART;
2164 editor->pCursors[0].pPara = para;
2165 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2166 editor->pCursors[1] = editor->pCursors[0];
2167 ME_InsertTextFromCursor(editor, 0, &endl, 1,
2168 editor->pCursors[0].pRun->member.run.style);
2169 para = editor->pBuffer->pFirst->member.para.next_para;
2170 ME_SetDefaultParaFormat(para->member.para.pFmt);
2171 para->member.para.nFlags = MEPF_REWRAP;
2172 editor->pCursors[0].pPara = para;
2173 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2174 editor->pCursors[1] = editor->pCursors[0];
2175 para->member.para.next_para->member.para.nFlags |= MEPF_ROWSTART;
2176 ME_CommitCoalescingUndo(editor);
2177 ME_CheckTablesForCorruption(editor);
2178 ME_UpdateRepaint(editor);
2179 return TRUE;
2181 } else { /* v1.0 - 3.0 */
2182 ME_DisplayItem *para = cursor.pPara;
2183 if (ME_IsInTable(para))
2185 if (cursor.pRun->member.run.nFlags & MERF_ENDPARA)
2187 if (from == to) {
2188 ME_ContinueCoalescingTransaction(editor);
2189 para = ME_AppendTableRow(editor, para);
2190 editor->pCursors[0].pPara = para;
2191 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2192 editor->pCursors[0].nOffset = 0;
2193 editor->pCursors[1] = editor->pCursors[0];
2194 ME_CommitCoalescingUndo(editor);
2195 ME_UpdateRepaint(editor);
2196 return TRUE;
2198 } else {
2199 ME_ContinueCoalescingTransaction(editor);
2200 if (cursor.pRun->member.run.nCharOfs + cursor.nOffset == 0 &&
2201 !ME_IsInTable(para->member.para.prev_para))
2203 /* Insert newline before table */
2204 cursor.pRun = ME_FindItemBack(para, diRun);
2205 if (cursor.pRun) {
2206 editor->pCursors[0].pRun = cursor.pRun;
2207 editor->pCursors[0].pPara = para->member.para.prev_para;
2209 editor->pCursors[0].nOffset = 0;
2210 editor->pCursors[1] = editor->pCursors[0];
2211 ME_InsertTextFromCursor(editor, 0, &endl, 1,
2212 editor->pCursors[0].pRun->member.run.style);
2213 } else {
2214 editor->pCursors[1] = editor->pCursors[0];
2215 para = ME_AppendTableRow(editor, para);
2216 editor->pCursors[0].pPara = para;
2217 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2218 editor->pCursors[0].nOffset = 0;
2219 editor->pCursors[1] = editor->pCursors[0];
2221 ME_CommitCoalescingUndo(editor);
2222 ME_UpdateRepaint(editor);
2223 return TRUE;
2228 style = ME_GetInsertStyle(editor, 0);
2229 ME_SaveTempStyle(editor);
2230 ME_ContinueCoalescingTransaction(editor);
2231 if (shift_is_down)
2232 ME_InsertEndRowFromCursor(editor, 0);
2233 else
2234 ME_InsertTextFromCursor(editor, 0, &endl, 1, style);
2235 ME_ReleaseStyle(style);
2236 ME_CommitCoalescingUndo(editor);
2237 SetCursor(NULL);
2239 ME_UpdateSelectionLinkAttribute(editor);
2240 ME_UpdateRepaint(editor);
2242 return TRUE;
2244 break;
2245 case 'A':
2246 if (ctrl_is_down)
2248 ME_SetSelection(editor, 0, -1);
2249 return TRUE;
2251 break;
2252 case 'V':
2253 if (ctrl_is_down)
2254 return ME_Paste(editor);
2255 break;
2256 case 'C':
2257 case 'X':
2258 if (ctrl_is_down)
2260 CHARRANGE range;
2261 BOOL result;
2263 ME_GetSelection(editor, &range.cpMin, &range.cpMax);
2264 result = ME_Copy(editor, &range);
2265 if (result && nKey == 'X')
2267 ME_InternalDeleteText(editor, range.cpMin, range.cpMax-range.cpMin, FALSE);
2268 ME_CommitUndo(editor);
2269 ME_UpdateRepaint(editor);
2271 return result;
2273 break;
2274 case 'Z':
2275 if (ctrl_is_down)
2277 ME_Undo(editor);
2278 return TRUE;
2280 break;
2281 case 'Y':
2282 if (ctrl_is_down)
2284 ME_Redo(editor);
2285 return TRUE;
2287 break;
2289 default:
2290 if (nKey != VK_SHIFT && nKey != VK_CONTROL && nKey && nKey != VK_MENU)
2291 editor->nUDArrowX = -1;
2292 if (ctrl_is_down)
2294 if (nKey == 'W')
2296 CHARFORMAT2W chf;
2297 char buf[2048];
2298 chf.cbSize = sizeof(chf);
2300 ME_GetSelectionCharFormat(editor, &chf);
2301 ME_DumpStyleToBuf(&chf, buf);
2302 MessageBoxA(NULL, buf, "Style dump", MB_OK);
2304 if (nKey == 'Q')
2306 ME_CheckCharOffsets(editor);
2310 return FALSE;
2313 static LRESULT ME_Char(ME_TextEditor *editor, WPARAM charCode,
2314 LPARAM flags, BOOL unicode)
2316 WCHAR wstr;
2318 if (editor->bMouseCaptured)
2319 return 0;
2321 if (unicode)
2322 wstr = (WCHAR)charCode;
2323 else
2325 CHAR charA = charCode;
2326 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &wstr, 1);
2329 if (editor->styleFlags & ES_READONLY) {
2330 MessageBeep(MB_ICONERROR);
2331 return 0; /* FIXME really 0 ? */
2334 if ((unsigned)wstr >= ' ' || wstr == '\t')
2336 ME_Cursor cursor = editor->pCursors[0];
2337 ME_DisplayItem *para = cursor.pPara;
2338 int from, to;
2339 BOOL ctrl_is_down = GetKeyState(VK_CONTROL) & 0x8000;
2340 ME_GetSelection(editor, &from, &to);
2341 if (wstr == '\t' &&
2342 /* v4.1 allows tabs to be inserted with ctrl key down */
2343 !(ctrl_is_down && !editor->bEmulateVersion10))
2345 ME_DisplayItem *para;
2346 BOOL bSelectedRow = FALSE;
2348 para = cursor.pPara;
2349 if (ME_IsSelection(editor) &&
2350 cursor.pRun->member.run.nCharOfs + cursor.nOffset == 0 &&
2351 to == ME_GetCursorOfs(editor, 0) &&
2352 para->member.para.prev_para->type == diParagraph)
2354 para = para->member.para.prev_para;
2355 bSelectedRow = TRUE;
2357 if (ME_IsInTable(para))
2359 ME_TabPressedInTable(editor, bSelectedRow);
2360 ME_CommitUndo(editor);
2361 return 0;
2363 } else if (!editor->bEmulateVersion10) { /* v4.1 */
2364 if (para->member.para.nFlags & MEPF_ROWEND) {
2365 if (from == to) {
2366 para = para->member.para.next_para;
2367 if (para->member.para.nFlags & MEPF_ROWSTART)
2368 para = para->member.para.next_para;
2369 editor->pCursors[0].pPara = para;
2370 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2371 editor->pCursors[0].nOffset = 0;
2372 editor->pCursors[1] = editor->pCursors[0];
2375 } else { /* v1.0 - 3.0 */
2376 if (ME_IsInTable(cursor.pRun) &&
2377 cursor.pRun->member.run.nFlags & MERF_ENDPARA &&
2378 from == to)
2380 /* Text should not be inserted at the end of the table. */
2381 MessageBeep(-1);
2382 return 0;
2385 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
2386 /* WM_CHAR is restricted to nTextLimit */
2387 if(editor->nTextLimit > ME_GetTextLength(editor) - (to-from))
2389 ME_Style *style = ME_GetInsertStyle(editor, 0);
2390 ME_SaveTempStyle(editor);
2391 ME_ContinueCoalescingTransaction(editor);
2392 ME_InsertTextFromCursor(editor, 0, &wstr, 1, style);
2393 ME_ReleaseStyle(style);
2394 ME_CommitCoalescingUndo(editor);
2395 ITextHost_TxSetCursor(editor->texthost, NULL, FALSE);
2398 ME_UpdateSelectionLinkAttribute(editor);
2399 ME_UpdateRepaint(editor);
2401 return 0;
2404 /* Process the message and calculate the new click count.
2406 * returns: The click count if it is mouse down event, else returns 0. */
2407 static int ME_CalculateClickCount(ME_TextEditor *editor, UINT msg, WPARAM wParam,
2408 LPARAM lParam)
2410 static int clickNum = 0;
2411 if (msg < WM_MOUSEFIRST || msg > WM_MOUSELAST)
2412 return 0;
2414 if ((msg == WM_LBUTTONDBLCLK) ||
2415 (msg == WM_RBUTTONDBLCLK) ||
2416 (msg == WM_MBUTTONDBLCLK) ||
2417 (msg == WM_XBUTTONDBLCLK))
2419 msg -= (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN);
2422 if ((msg == WM_LBUTTONDOWN) ||
2423 (msg == WM_RBUTTONDOWN) ||
2424 (msg == WM_MBUTTONDOWN) ||
2425 (msg == WM_XBUTTONDOWN))
2427 static MSG prevClickMsg;
2428 MSG clickMsg;
2429 /* Compare the editor instead of the hwnd so that the this
2430 * can still be done for windowless richedit controls. */
2431 clickMsg.hwnd = (HWND)editor;
2432 clickMsg.message = msg;
2433 clickMsg.wParam = wParam;
2434 clickMsg.lParam = lParam;
2435 clickMsg.time = GetMessageTime();
2436 clickMsg.pt.x = (short)LOWORD(lParam);
2437 clickMsg.pt.y = (short)HIWORD(lParam);
2438 if ((clickNum != 0) &&
2439 (clickMsg.message == prevClickMsg.message) &&
2440 (clickMsg.hwnd == prevClickMsg.hwnd) &&
2441 (clickMsg.wParam == prevClickMsg.wParam) &&
2442 (clickMsg.time - prevClickMsg.time < GetDoubleClickTime()) &&
2443 (abs(clickMsg.pt.x - prevClickMsg.pt.x) < GetSystemMetrics(SM_CXDOUBLECLK)/2) &&
2444 (abs(clickMsg.pt.y - prevClickMsg.pt.y) < GetSystemMetrics(SM_CYDOUBLECLK)/2))
2446 clickNum++;
2447 } else {
2448 clickNum = 1;
2450 prevClickMsg = clickMsg;
2451 } else {
2452 return 0;
2454 return clickNum;
2457 static BOOL ME_SetCursor(ME_TextEditor *editor)
2459 POINT pt;
2460 BOOL isExact;
2461 int offset;
2462 SCROLLBARINFO sbi;
2463 DWORD messagePos = GetMessagePos();
2464 pt.x = (short)LOWORD(messagePos);
2465 pt.y = (short)HIWORD(messagePos);
2467 if (editor->hWnd)
2469 sbi.cbSize = sizeof(sbi);
2470 GetScrollBarInfo(editor->hWnd, OBJID_HSCROLL, &sbi);
2471 if (!(sbi.rgstate[0] & (STATE_SYSTEM_INVISIBLE|STATE_SYSTEM_OFFSCREEN)) &&
2472 PtInRect(&sbi.rcScrollBar, pt))
2474 ITextHost_TxSetCursor(editor->texthost,
2475 LoadCursorW(NULL, (WCHAR*)IDC_ARROW), FALSE);
2476 return TRUE;
2478 sbi.cbSize = sizeof(sbi);
2479 GetScrollBarInfo(editor->hWnd, OBJID_VSCROLL, &sbi);
2480 if (!(sbi.rgstate[0] & (STATE_SYSTEM_INVISIBLE|STATE_SYSTEM_OFFSCREEN)) &&
2481 PtInRect(&sbi.rcScrollBar, pt))
2483 ITextHost_TxSetCursor(editor->texthost,
2484 LoadCursorW(NULL, (WCHAR*)IDC_ARROW), FALSE);
2485 return TRUE;
2488 ITextHost_TxScreenToClient(editor->texthost, &pt);
2490 if (editor->nSelectionType == stLine && editor->bMouseCaptured) {
2491 ITextHost_TxSetCursor(editor->texthost, hLeft, FALSE);
2492 return TRUE;
2494 if (!editor->bEmulateVersion10 /* v4.1 */ &&
2495 pt.y < editor->rcFormat.top &&
2496 pt.x < editor->rcFormat.left)
2498 ITextHost_TxSetCursor(editor->texthost, hLeft, FALSE);
2499 return TRUE;
2501 if (pt.y < editor->rcFormat.top || pt.y > editor->rcFormat.bottom)
2503 if (editor->bEmulateVersion10) /* v1.0 - 3.0 */
2504 ITextHost_TxSetCursor(editor->texthost,
2505 LoadCursorW(NULL, (WCHAR*)IDC_ARROW), FALSE);
2506 else /* v4.1 */
2507 ITextHost_TxSetCursor(editor->texthost,
2508 LoadCursorW(NULL, (WCHAR*)IDC_IBEAM), TRUE);
2509 return TRUE;
2511 if (pt.x < editor->rcFormat.left)
2513 ITextHost_TxSetCursor(editor->texthost, hLeft, FALSE);
2514 return TRUE;
2516 offset = ME_CharFromPos(editor, pt.x, pt.y, &isExact);
2517 if (isExact)
2519 ME_Cursor cursor;
2520 ME_Run *run;
2522 ME_CursorFromCharOfs(editor, offset, &cursor);
2523 run = &cursor.pRun->member.run;
2524 if (run->style->fmt.dwMask & CFM_LINK &&
2525 run->style->fmt.dwEffects & CFE_LINK)
2527 ITextHost_TxSetCursor(editor->texthost,
2528 LoadCursorW(NULL, (WCHAR*)IDC_HAND),
2529 FALSE);
2530 return TRUE;
2533 if (ME_IsSelection(editor))
2535 int selStart, selEnd;
2536 ME_GetSelection(editor, &selStart, &selEnd);
2537 if (selStart <= offset && selEnd >= offset) {
2538 ITextHost_TxSetCursor(editor->texthost,
2539 LoadCursorW(NULL, (WCHAR*)IDC_ARROW),
2540 FALSE);
2541 return TRUE;
2545 ITextHost_TxSetCursor(editor->texthost,
2546 LoadCursorW(NULL, (WCHAR*)IDC_IBEAM), TRUE);
2547 return TRUE;
2550 static void ME_SetDefaultFormatRect(ME_TextEditor *editor)
2552 ITextHost_TxGetClientRect(editor->texthost, &editor->rcFormat);
2553 editor->rcFormat.top += editor->exStyleFlags & WS_EX_CLIENTEDGE ? 1 : 0;
2554 editor->rcFormat.left += 1 + editor->selofs;
2555 editor->rcFormat.right -= 1;
2558 static BOOL ME_ShowContextMenu(ME_TextEditor *editor, int x, int y)
2560 CHARRANGE selrange;
2561 HMENU menu;
2562 int seltype = 0;
2563 if(!editor->lpOleCallback || !editor->hWnd)
2564 return FALSE;
2565 ME_GetSelection(editor, &selrange.cpMin, &selrange.cpMax);
2566 if(selrange.cpMin == selrange.cpMax)
2567 seltype |= SEL_EMPTY;
2568 else
2570 /* FIXME: Handle objects */
2571 seltype |= SEL_TEXT;
2572 if(selrange.cpMax-selrange.cpMin > 1)
2573 seltype |= SEL_MULTICHAR;
2575 if(SUCCEEDED(IRichEditOleCallback_GetContextMenu(editor->lpOleCallback, seltype, NULL, &selrange, &menu)))
2577 TrackPopupMenu(menu, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, GetParent(editor->hWnd), NULL);
2578 DestroyMenu(menu);
2580 return TRUE;
2583 ME_TextEditor *ME_MakeEditor(ITextHost *texthost, BOOL bEmulateVersion10)
2585 ME_TextEditor *ed = ALLOC_OBJ(ME_TextEditor);
2586 int i;
2587 DWORD props;
2588 LONG selbarwidth;
2590 ed->hWnd = NULL;
2591 ed->texthost = texthost;
2592 ed->bEmulateVersion10 = bEmulateVersion10;
2593 ITextHost_TxGetPropertyBits(texthost,
2594 (TXTBIT_RICHTEXT|TXTBIT_MULTILINE|
2595 TXTBIT_READONLY|TXTBIT_USEPASSWORD|
2596 TXTBIT_HIDESELECTION|TXTBIT_SAVESELECTION|
2597 TXTBIT_AUTOWORDSEL|TXTBIT_VERTICAL|
2598 TXTBIT_WORDWRAP|TXTBIT_DISABLEDRAG),
2599 &props);
2600 ITextHost_TxGetScrollBars(texthost, &ed->styleFlags);
2601 ed->styleFlags &= (WS_VSCROLL|WS_HSCROLL|ES_AUTOVSCROLL|
2602 ES_AUTOHSCROLL|ES_DISABLENOSCROLL);
2603 ed->pBuffer = ME_MakeText();
2604 ed->nZoomNumerator = ed->nZoomDenominator = 0;
2605 ME_MakeFirstParagraph(ed);
2606 /* The four cursors are for:
2607 * 0 - The position where the caret is shown
2608 * 1 - The anchored end of the selection (for normal selection)
2609 * 2 & 3 - The anchored start and end respectively for word, line,
2610 * or paragraph selection.
2612 ed->nCursors = 4;
2613 ed->pCursors = ALLOC_N_OBJ(ME_Cursor, ed->nCursors);
2614 ed->pCursors[0].pPara = ed->pBuffer->pFirst->member.para.next_para;
2615 ed->pCursors[0].pRun = ME_FindItemFwd(ed->pCursors[0].pPara, diRun);
2616 ed->pCursors[0].nOffset = 0;
2617 ed->pCursors[1] = ed->pCursors[0];
2618 ed->pCursors[2] = ed->pCursors[0];
2619 ed->pCursors[3] = ed->pCursors[1];
2620 ed->nLastTotalLength = ed->nTotalLength = 0;
2621 ed->nLastTotalWidth = ed->nTotalWidth = 0;
2622 ed->nUDArrowX = -1;
2623 ed->nSequence = 0;
2624 ed->rgbBackColor = -1;
2625 ed->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
2626 ed->bCaretAtEnd = FALSE;
2627 ed->nEventMask = 0;
2628 ed->nModifyStep = 0;
2629 ed->nTextLimit = TEXT_LIMIT_DEFAULT;
2630 ed->pUndoStack = ed->pRedoStack = ed->pUndoStackBottom = NULL;
2631 ed->nUndoStackSize = 0;
2632 ed->nUndoLimit = STACK_SIZE_DEFAULT;
2633 ed->nUndoMode = umAddToUndo;
2634 ed->nParagraphs = 1;
2635 ed->nLastSelStart = ed->nLastSelEnd = 0;
2636 ed->pLastSelStartPara = ed->pLastSelEndPara = ME_FindItemFwd(ed->pBuffer->pFirst, diParagraph);
2637 ed->nAvailWidth = 0; /* wrap to client area */
2638 ed->bHideSelection = FALSE;
2639 ed->pfnWordBreak = NULL;
2640 ed->lpOleCallback = NULL;
2641 ed->mode = TM_MULTILEVELUNDO | TM_MULTICODEPAGE;
2642 ed->mode |= (props & TXTBIT_RICHTEXT) ? TM_RICHTEXT : TM_PLAINTEXT;
2643 ed->AutoURLDetect_bEnable = FALSE;
2644 ed->bHaveFocus = FALSE;
2645 ed->bMouseCaptured = FALSE;
2646 for (i=0; i<HFONT_CACHE_SIZE; i++)
2648 ed->pFontCache[i].nRefs = 0;
2649 ed->pFontCache[i].nAge = 0;
2650 ed->pFontCache[i].hFont = NULL;
2653 ME_CheckCharOffsets(ed);
2654 ed->bDefaultFormatRect = TRUE;
2655 ITextHost_TxGetSelectionBarWidth(ed->texthost, &selbarwidth);
2656 if (selbarwidth) {
2657 /* FIXME: Convert selbarwidth from HIMETRIC to pixels */
2658 ed->selofs = SELECTIONBAR_WIDTH;
2659 ed->styleFlags |= ES_SELECTIONBAR;
2660 } else {
2661 ed->selofs = 0;
2663 ed->nSelectionType = stPosition;
2665 ed->cPasswordMask = 0;
2666 if (props & TXTBIT_USEPASSWORD)
2667 ITextHost_TxGetPasswordChar(texthost, &ed->cPasswordMask);
2669 if (props & TXTBIT_AUTOWORDSEL)
2670 ed->styleFlags |= ECO_AUTOWORDSELECTION;
2671 if (props & TXTBIT_MULTILINE) {
2672 ed->styleFlags |= ES_MULTILINE;
2673 ed->bWordWrap = (props & TXTBIT_WORDWRAP) != 0;
2674 } else {
2675 ed->bWordWrap = FALSE;
2677 if (props & TXTBIT_READONLY)
2678 ed->styleFlags |= ES_READONLY;
2679 if (!(props & TXTBIT_HIDESELECTION))
2680 ed->styleFlags |= ES_NOHIDESEL;
2681 if (props & TXTBIT_SAVESELECTION)
2682 ed->styleFlags |= ES_SAVESEL;
2683 if (props & TXTBIT_VERTICAL)
2684 ed->styleFlags |= ES_VERTICAL;
2685 if (props & TXTBIT_DISABLEDRAG)
2686 ed->styleFlags |= ES_NOOLEDRAGDROP;
2688 ed->notified_cr.cpMin = ed->notified_cr.cpMax = 0;
2690 /* Default scrollbar information */
2691 ed->vert_si.cbSize = sizeof(SCROLLINFO);
2692 ed->vert_si.nMin = 0;
2693 ed->vert_si.nMax = 0;
2694 ed->vert_si.nPage = 0;
2695 ed->vert_si.nPos = 0;
2697 ed->horz_si.cbSize = sizeof(SCROLLINFO);
2698 ed->horz_si.nMin = 0;
2699 ed->horz_si.nMax = 0;
2700 ed->horz_si.nPage = 0;
2701 ed->horz_si.nPos = 0;
2703 OleInitialize(NULL);
2705 return ed;
2708 static void ME_DestroyEditor(ME_TextEditor *editor)
2710 ME_DisplayItem *pFirst = editor->pBuffer->pFirst;
2711 ME_DisplayItem *p = pFirst, *pNext = NULL;
2712 int i;
2714 ME_ClearTempStyle(editor);
2715 ME_EmptyUndoStack(editor);
2716 while(p) {
2717 pNext = p->next;
2718 ME_DestroyDisplayItem(p);
2719 p = pNext;
2721 ME_ReleaseStyle(editor->pBuffer->pDefaultStyle);
2722 for (i=0; i<HFONT_CACHE_SIZE; i++)
2724 if (editor->pFontCache[i].hFont)
2725 DeleteObject(editor->pFontCache[i].hFont);
2727 if (editor->rgbBackColor != -1)
2728 DeleteObject(editor->hbrBackground);
2729 if(editor->lpOleCallback)
2730 IUnknown_Release(editor->lpOleCallback);
2731 IUnknown_Release(editor->texthost);
2732 OleUninitialize();
2734 FREE_OBJ(editor->pBuffer);
2735 FREE_OBJ(editor->pCursors);
2737 FREE_OBJ(editor);
2740 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
2742 TRACE("\n");
2743 switch (fdwReason)
2745 case DLL_PROCESS_ATTACH:
2746 DisableThreadLibraryCalls(hinstDLL);
2747 me_heap = HeapCreate (0, 0x10000, 0);
2748 if (!ME_RegisterEditorClass(hinstDLL)) return FALSE;
2749 hLeft = LoadCursorW(hinstDLL, MAKEINTRESOURCEW(OCR_REVERSE));
2750 LookupInit();
2751 break;
2753 case DLL_PROCESS_DETACH:
2754 UnregisterClassW(RichEdit20W, 0);
2755 UnregisterClassW(RichEdit50W, 0);
2756 UnregisterClassA("RichEdit20A", 0);
2757 UnregisterClassA("RichEdit50A", 0);
2758 if (ME_ListBoxRegistered)
2759 UnregisterClassW(REListBox20W, 0);
2760 if (ME_ComboBoxRegistered)
2761 UnregisterClassW(REComboBox20W, 0);
2762 LookupCleanup();
2763 HeapDestroy (me_heap);
2764 me_heap = NULL;
2765 break;
2767 return TRUE;
2771 static const char * const edit_messages[] = {
2772 "EM_GETSEL",
2773 "EM_SETSEL",
2774 "EM_GETRECT",
2775 "EM_SETRECT",
2776 "EM_SETRECTNP",
2777 "EM_SCROLL",
2778 "EM_LINESCROLL",
2779 "EM_SCROLLCARET",
2780 "EM_GETMODIFY",
2781 "EM_SETMODIFY",
2782 "EM_GETLINECOUNT",
2783 "EM_LINEINDEX",
2784 "EM_SETHANDLE",
2785 "EM_GETHANDLE",
2786 "EM_GETTHUMB",
2787 "EM_UNKNOWN_BF",
2788 "EM_UNKNOWN_C0",
2789 "EM_LINELENGTH",
2790 "EM_REPLACESEL",
2791 "EM_UNKNOWN_C3",
2792 "EM_GETLINE",
2793 "EM_LIMITTEXT",
2794 "EM_CANUNDO",
2795 "EM_UNDO",
2796 "EM_FMTLINES",
2797 "EM_LINEFROMCHAR",
2798 "EM_UNKNOWN_CA",
2799 "EM_SETTABSTOPS",
2800 "EM_SETPASSWORDCHAR",
2801 "EM_EMPTYUNDOBUFFER",
2802 "EM_GETFIRSTVISIBLELINE",
2803 "EM_SETREADONLY",
2804 "EM_SETWORDBREAKPROC",
2805 "EM_GETWORDBREAKPROC",
2806 "EM_GETPASSWORDCHAR",
2807 "EM_SETMARGINS",
2808 "EM_GETMARGINS",
2809 "EM_GETLIMITTEXT",
2810 "EM_POSFROMCHAR",
2811 "EM_CHARFROMPOS",
2812 "EM_SETIMESTATUS",
2813 "EM_GETIMESTATUS"
2816 static const char * const richedit_messages[] = {
2817 "EM_CANPASTE",
2818 "EM_DISPLAYBAND",
2819 "EM_EXGETSEL",
2820 "EM_EXLIMITTEXT",
2821 "EM_EXLINEFROMCHAR",
2822 "EM_EXSETSEL",
2823 "EM_FINDTEXT",
2824 "EM_FORMATRANGE",
2825 "EM_GETCHARFORMAT",
2826 "EM_GETEVENTMASK",
2827 "EM_GETOLEINTERFACE",
2828 "EM_GETPARAFORMAT",
2829 "EM_GETSELTEXT",
2830 "EM_HIDESELECTION",
2831 "EM_PASTESPECIAL",
2832 "EM_REQUESTRESIZE",
2833 "EM_SELECTIONTYPE",
2834 "EM_SETBKGNDCOLOR",
2835 "EM_SETCHARFORMAT",
2836 "EM_SETEVENTMASK",
2837 "EM_SETOLECALLBACK",
2838 "EM_SETPARAFORMAT",
2839 "EM_SETTARGETDEVICE",
2840 "EM_STREAMIN",
2841 "EM_STREAMOUT",
2842 "EM_GETTEXTRANGE",
2843 "EM_FINDWORDBREAK",
2844 "EM_SETOPTIONS",
2845 "EM_GETOPTIONS",
2846 "EM_FINDTEXTEX",
2847 "EM_GETWORDBREAKPROCEX",
2848 "EM_SETWORDBREAKPROCEX",
2849 "EM_SETUNDOLIMIT",
2850 "EM_UNKNOWN_USER_83",
2851 "EM_REDO",
2852 "EM_CANREDO",
2853 "EM_GETUNDONAME",
2854 "EM_GETREDONAME",
2855 "EM_STOPGROUPTYPING",
2856 "EM_SETTEXTMODE",
2857 "EM_GETTEXTMODE",
2858 "EM_AUTOURLDETECT",
2859 "EM_GETAUTOURLDETECT",
2860 "EM_SETPALETTE",
2861 "EM_GETTEXTEX",
2862 "EM_GETTEXTLENGTHEX",
2863 "EM_SHOWSCROLLBAR",
2864 "EM_SETTEXTEX",
2865 "EM_UNKNOWN_USER_98",
2866 "EM_UNKNOWN_USER_99",
2867 "EM_SETPUNCTUATION",
2868 "EM_GETPUNCTUATION",
2869 "EM_SETWORDWRAPMODE",
2870 "EM_GETWORDWRAPMODE",
2871 "EM_SETIMECOLOR",
2872 "EM_GETIMECOLOR",
2873 "EM_SETIMEOPTIONS",
2874 "EM_GETIMEOPTIONS",
2875 "EM_CONVPOSITION",
2876 "EM_UNKNOWN_USER_109",
2877 "EM_UNKNOWN_USER_110",
2878 "EM_UNKNOWN_USER_111",
2879 "EM_UNKNOWN_USER_112",
2880 "EM_UNKNOWN_USER_113",
2881 "EM_UNKNOWN_USER_114",
2882 "EM_UNKNOWN_USER_115",
2883 "EM_UNKNOWN_USER_116",
2884 "EM_UNKNOWN_USER_117",
2885 "EM_UNKNOWN_USER_118",
2886 "EM_UNKNOWN_USER_119",
2887 "EM_SETLANGOPTIONS",
2888 "EM_GETLANGOPTIONS",
2889 "EM_GETIMECOMPMODE",
2890 "EM_FINDTEXTW",
2891 "EM_FINDTEXTEXW",
2892 "EM_RECONVERSION",
2893 "EM_SETIMEMODEBIAS",
2894 "EM_GETIMEMODEBIAS"
2897 static const char *
2898 get_msg_name(UINT msg)
2900 if (msg >= EM_GETSEL && msg <= EM_CHARFROMPOS)
2901 return edit_messages[msg - EM_GETSEL];
2902 if (msg >= EM_CANPASTE && msg <= EM_GETIMEMODEBIAS)
2903 return richedit_messages[msg - EM_CANPASTE];
2904 return "";
2907 static void ME_LinkNotify(ME_TextEditor *editor, UINT msg, WPARAM wParam, LPARAM lParam)
2909 int x,y;
2910 ME_DisplayItem *para, *run;
2911 BOOL isExact;
2912 int nCharOfs; /* The start of the clicked text. Absolute character offset */
2914 ENLINK info;
2915 x = (short)LOWORD(lParam);
2916 y = (short)HIWORD(lParam);
2917 nCharOfs = ME_CharFromPos(editor, x, y, &isExact);
2918 if (!isExact) return;
2920 ME_RunOfsFromCharOfs(editor, nCharOfs, &para, &run, NULL);
2922 if ((run->member.run.style->fmt.dwMask & CFM_LINK)
2923 && (run->member.run.style->fmt.dwEffects & CFE_LINK))
2924 { /* The clicked run has CFE_LINK set */
2925 info.nmhdr.hwndFrom = editor->hWnd;
2926 info.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
2927 info.nmhdr.code = EN_LINK;
2928 info.msg = msg;
2929 info.wParam = wParam;
2930 info.lParam = lParam;
2931 info.chrg.cpMin = ME_CharOfsFromRunOfs(editor, para, run, 0);
2932 info.chrg.cpMax = info.chrg.cpMin + run->member.run.strText->nLen;
2933 SendMessageW(GetParent(editor->hWnd), WM_NOTIFY,info.nmhdr.idFrom, (LPARAM)&info);
2937 #define UNSUPPORTED_MSG(e) \
2938 case e: \
2939 FIXME(#e ": stub\n"); \
2940 *phresult = S_FALSE; \
2941 return 0;
2943 /* Handle messages for windowless and windoweded richedit controls.
2945 * The LRESULT that is returned is a return value for window procs,
2946 * and the phresult parameter is the COM return code needed by the
2947 * text services interface. */
2948 LRESULT ME_HandleMessage(ME_TextEditor *editor, UINT msg, WPARAM wParam,
2949 LPARAM lParam, BOOL unicode, HRESULT* phresult)
2951 *phresult = S_OK;
2953 switch(msg) {
2955 UNSUPPORTED_MSG(EM_DISPLAYBAND)
2956 UNSUPPORTED_MSG(EM_FINDWORDBREAK)
2957 UNSUPPORTED_MSG(EM_FMTLINES)
2958 UNSUPPORTED_MSG(EM_FORMATRANGE)
2959 UNSUPPORTED_MSG(EM_GETBIDIOPTIONS)
2960 UNSUPPORTED_MSG(EM_GETEDITSTYLE)
2961 UNSUPPORTED_MSG(EM_GETIMECOMPMODE)
2962 UNSUPPORTED_MSG(EM_GETIMESTATUS)
2963 UNSUPPORTED_MSG(EM_SETIMESTATUS)
2964 UNSUPPORTED_MSG(EM_GETLANGOPTIONS)
2965 UNSUPPORTED_MSG(EM_GETREDONAME)
2966 UNSUPPORTED_MSG(EM_GETTYPOGRAPHYOPTIONS)
2967 UNSUPPORTED_MSG(EM_GETUNDONAME)
2968 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX)
2969 UNSUPPORTED_MSG(EM_PASTESPECIAL)
2970 UNSUPPORTED_MSG(EM_SELECTIONTYPE)
2971 UNSUPPORTED_MSG(EM_SETBIDIOPTIONS)
2972 UNSUPPORTED_MSG(EM_SETEDITSTYLE)
2973 UNSUPPORTED_MSG(EM_SETFONTSIZE)
2974 UNSUPPORTED_MSG(EM_SETLANGOPTIONS)
2975 UNSUPPORTED_MSG(EM_SETMARGINS)
2976 UNSUPPORTED_MSG(EM_SETPALETTE)
2977 UNSUPPORTED_MSG(EM_SETTABSTOPS)
2978 UNSUPPORTED_MSG(EM_SETTYPOGRAPHYOPTIONS)
2979 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX)
2980 UNSUPPORTED_MSG(WM_STYLECHANGING)
2981 UNSUPPORTED_MSG(WM_STYLECHANGED)
2983 /* Messages specific to Richedit controls */
2985 case EM_STREAMIN:
2986 return ME_StreamIn(editor, wParam, (EDITSTREAM*)lParam, TRUE);
2987 case EM_STREAMOUT:
2988 return ME_StreamOut(editor, wParam, (EDITSTREAM *)lParam);
2989 case WM_GETDLGCODE:
2991 UINT code = DLGC_WANTCHARS|DLGC_WANTTAB|DLGC_WANTARROWS|DLGC_HASSETSEL;
2992 if (editor->styleFlags & ES_MULTILINE)
2993 code |= DLGC_WANTMESSAGE;
2994 return code;
2996 case EM_EMPTYUNDOBUFFER:
2997 ME_EmptyUndoStack(editor);
2998 return 0;
2999 case EM_GETSEL:
3001 /* Note: wParam/lParam can be NULL */
3002 UINT from, to;
3003 PUINT pfrom = wParam ? (PUINT)wParam : &from;
3004 PUINT pto = lParam ? (PUINT)lParam : &to;
3005 ME_GetSelection(editor, (int *)pfrom, (int *)pto);
3006 if ((*pfrom|*pto) & 0xFFFF0000)
3007 return -1;
3008 return MAKELONG(*pfrom,*pto);
3010 case EM_EXGETSEL:
3012 CHARRANGE *pRange = (CHARRANGE *)lParam;
3013 ME_GetSelection(editor, &pRange->cpMin, &pRange->cpMax);
3014 TRACE("EM_EXGETSEL = (%d,%d)\n", pRange->cpMin, pRange->cpMax);
3015 return 0;
3017 case EM_SETUNDOLIMIT:
3019 if ((int)wParam < 0)
3020 editor->nUndoLimit = STACK_SIZE_DEFAULT;
3021 else
3022 editor->nUndoLimit = min(wParam, STACK_SIZE_MAX);
3023 /* Setting a max stack size keeps wine from getting killed
3024 for hogging memory. Windows allocates all this memory at once, so
3025 no program would realistically set a value above our maximum. */
3026 return editor->nUndoLimit;
3028 case EM_CANUNDO:
3029 return editor->pUndoStack != NULL;
3030 case EM_CANREDO:
3031 return editor->pRedoStack != NULL;
3032 case WM_UNDO: /* FIXME: actually not the same */
3033 case EM_UNDO:
3034 return ME_Undo(editor);
3035 case EM_REDO:
3036 return ME_Redo(editor);
3037 case EM_GETOPTIONS:
3039 /* these flags are equivalent to the ES_* counterparts */
3040 DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
3041 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN | ECO_SELECTIONBAR;
3042 DWORD settings = editor->styleFlags & mask;
3044 return settings;
3046 case EM_SETOPTIONS:
3048 /* these flags are equivalent to ES_* counterparts, except for
3049 * ECO_AUTOWORDSELECTION that doesn't have an ES_* counterpart,
3050 * but is still stored in editor->styleFlags. */
3051 const DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
3052 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN |
3053 ECO_SELECTIONBAR | ECO_AUTOWORDSELECTION;
3054 DWORD settings = mask & editor->styleFlags;
3055 DWORD oldSettings = settings;
3056 DWORD changedSettings;
3058 switch(wParam)
3060 case ECOOP_SET:
3061 settings = lParam;
3062 break;
3063 case ECOOP_OR:
3064 settings |= lParam;
3065 break;
3066 case ECOOP_AND:
3067 settings &= lParam;
3068 break;
3069 case ECOOP_XOR:
3070 settings ^= lParam;
3072 changedSettings = oldSettings ^ settings;
3074 if (changedSettings) {
3075 editor->styleFlags = (editor->styleFlags & ~mask) | (settings & mask);
3077 if (changedSettings & ECO_SELECTIONBAR)
3079 ITextHost_TxInvalidateRect(editor->texthost, &editor->rcFormat, TRUE);
3080 if (settings & ECO_SELECTIONBAR) {
3081 assert(!editor->selofs);
3082 editor->selofs = SELECTIONBAR_WIDTH;
3083 editor->rcFormat.left += editor->selofs;
3084 } else {
3085 editor->rcFormat.left -= editor->selofs;
3086 editor->selofs = 0;
3088 ME_RewrapRepaint(editor);
3091 if (changedSettings & settings & ECO_VERTICAL)
3092 FIXME("ECO_VERTICAL not implemented yet!\n");
3093 if (changedSettings & settings & ECO_AUTOHSCROLL)
3094 FIXME("ECO_AUTOHSCROLL not implemented yet!\n");
3095 if (changedSettings & settings & ECO_AUTOVSCROLL)
3096 FIXME("ECO_AUTOVSCROLL not implemented yet!\n");
3097 if (changedSettings & settings & ECO_NOHIDESEL)
3098 FIXME("ECO_NOHIDESEL not implemented yet!\n");
3099 if (changedSettings & settings & ECO_WANTRETURN)
3100 FIXME("ECO_WANTRETURN not implemented yet!\n");
3101 if (changedSettings & settings & ECO_AUTOWORDSELECTION)
3102 FIXME("ECO_AUTOWORDSELECTION not implemented yet!\n");
3105 return settings;
3107 case EM_SETSEL:
3109 ME_InvalidateSelection(editor);
3110 ME_SetSelection(editor, wParam, lParam);
3111 ME_InvalidateSelection(editor);
3112 ITextHost_TxShowCaret(editor->texthost, FALSE);
3113 ME_ShowCaret(editor);
3114 ME_SendSelChange(editor);
3115 return 0;
3117 case EM_SETSCROLLPOS:
3119 POINT *point = (POINT *)lParam;
3120 ME_ScrollAbs(editor, point->x, point->y);
3121 return 0;
3123 case EM_AUTOURLDETECT:
3125 if (wParam==1 || wParam ==0)
3127 editor->AutoURLDetect_bEnable = (BOOL)wParam;
3128 return 0;
3130 return E_INVALIDARG;
3132 case EM_GETAUTOURLDETECT:
3134 return editor->AutoURLDetect_bEnable;
3136 case EM_EXSETSEL:
3138 int end;
3139 CHARRANGE range = *(CHARRANGE *)lParam;
3141 TRACE("EM_EXSETSEL (%d,%d)\n", range.cpMin, range.cpMax);
3143 ME_InvalidateSelection(editor);
3144 end = ME_SetSelection(editor, range.cpMin, range.cpMax);
3145 ME_InvalidateSelection(editor);
3146 ITextHost_TxShowCaret(editor->texthost, FALSE);
3147 ME_ShowCaret(editor);
3148 ME_SendSelChange(editor);
3150 return end;
3152 case EM_SHOWSCROLLBAR:
3154 DWORD flags;
3156 switch (wParam)
3158 case SB_HORZ:
3159 flags = WS_HSCROLL;
3160 break;
3161 case SB_VERT:
3162 flags = WS_VSCROLL;
3163 break;
3164 case SB_BOTH:
3165 flags = WS_HSCROLL|WS_VSCROLL;
3166 break;
3167 default:
3168 return 0;
3171 if (lParam) {
3172 editor->styleFlags |= flags;
3173 if (flags & WS_HSCROLL)
3174 ITextHost_TxShowScrollBar(editor->texthost, SB_HORZ,
3175 editor->nTotalWidth > editor->sizeWindow.cx);
3176 if (flags & WS_VSCROLL)
3177 ITextHost_TxShowScrollBar(editor->texthost, SB_VERT,
3178 editor->nTotalLength > editor->sizeWindow.cy);
3179 } else {
3180 editor->styleFlags &= ~flags;
3181 ITextHost_TxShowScrollBar(editor->texthost, wParam, FALSE);
3183 return 0;
3185 case EM_SETTEXTEX:
3187 LPWSTR wszText;
3188 SETTEXTEX *pStruct = (SETTEXTEX *)wParam;
3189 size_t len = 0;
3190 int from, to;
3191 ME_Style *style;
3192 BOOL bRtf, bUnicode, bSelection;
3193 int oldModify = editor->nModifyStep;
3195 if (!pStruct) return 0;
3197 /* If we detect ascii rtf at the start of the string,
3198 * we know it isn't unicode. */
3199 bRtf = (lParam && (!strncmp((char *)lParam, "{\\rtf", 5) ||
3200 !strncmp((char *)lParam, "{\\urtf", 6)));
3201 bUnicode = !bRtf && pStruct->codepage == 1200;
3203 TRACE("EM_SETTEXTEX - %s, flags %d, cp %d\n",
3204 bUnicode ? debugstr_w((LPCWSTR)lParam) : debugstr_a((LPCSTR)lParam),
3205 pStruct->flags, pStruct->codepage);
3207 bSelection = (pStruct->flags & ST_SELECTION) != 0;
3208 if (bSelection) {
3209 ME_GetSelection(editor, &from, &to);
3210 style = ME_GetSelectionInsertStyle(editor);
3211 ME_InternalDeleteText(editor, from, to - from, FALSE);
3212 } else {
3213 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor), FALSE);
3214 style = editor->pBuffer->pDefaultStyle;
3217 if (bRtf) {
3218 ME_StreamInRTFString(editor, bSelection, (char *)lParam);
3219 if (bSelection) {
3220 /* FIXME: The length returned is doesn't include the rtf control
3221 * characters, only the actual text. */
3222 len = lParam ? strlen((char *)lParam) : 0;
3224 } else {
3225 /* FIXME: make use of pStruct->codepage in the to unicode translation */
3226 wszText = lParam ? ME_ToUnicode(bUnicode, (void *)lParam) : NULL;
3227 len = wszText ? lstrlenW(wszText) : 0;
3228 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
3231 if (bSelection) {
3232 ME_ReleaseStyle(style);
3233 ME_UpdateSelectionLinkAttribute(editor);
3234 } else {
3235 len = 1;
3236 ME_UpdateLinkAttribute(editor, 0, -1);
3238 ME_CommitUndo(editor);
3239 if (!(pStruct->flags & ST_KEEPUNDO))
3241 editor->nModifyStep = oldModify;
3242 ME_EmptyUndoStack(editor);
3244 ME_UpdateRepaint(editor);
3245 return len;
3247 case EM_SETBKGNDCOLOR:
3249 LRESULT lColor;
3250 if (editor->rgbBackColor != -1) {
3251 DeleteObject(editor->hbrBackground);
3252 lColor = editor->rgbBackColor;
3254 else lColor = ITextHost_TxGetSysColor(editor->texthost, COLOR_WINDOW);
3256 if (wParam)
3258 editor->rgbBackColor = -1;
3259 editor->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
3261 else
3263 editor->rgbBackColor = lParam;
3264 editor->hbrBackground = CreateSolidBrush(editor->rgbBackColor);
3266 ITextHost_TxInvalidateRect(editor->texthost, NULL, TRUE);
3267 ITextHost_TxViewChange(editor->texthost, TRUE);
3268 return lColor;
3270 case EM_GETMODIFY:
3271 return editor->nModifyStep == 0 ? 0 : -1;
3272 case EM_SETMODIFY:
3274 if (wParam)
3275 editor->nModifyStep = 1;
3276 else
3277 editor->nModifyStep = 0;
3279 return 0;
3281 case EM_SETREADONLY:
3283 if (wParam)
3284 editor->styleFlags |= ES_READONLY;
3285 else
3286 editor->styleFlags &= ~ES_READONLY;
3287 return 0;
3289 case EM_SETEVENTMASK:
3291 DWORD nOldMask = editor->nEventMask;
3293 editor->nEventMask = lParam;
3294 return nOldMask;
3296 case EM_GETEVENTMASK:
3297 return editor->nEventMask;
3298 case EM_SETCHARFORMAT:
3300 CHARFORMAT2W buf, *p;
3301 BOOL bRepaint = TRUE;
3302 p = ME_ToCF2W(&buf, (CHARFORMAT2W *)lParam);
3303 if (p == NULL) return 0;
3304 if (!wParam)
3305 ME_SetDefaultCharFormat(editor, p);
3306 else if (wParam == (SCF_WORD | SCF_SELECTION)) {
3307 FIXME("EM_SETCHARFORMAT: word selection not supported\n");
3308 return 0;
3309 } else if (wParam == SCF_ALL) {
3310 if (editor->mode & TM_PLAINTEXT)
3311 ME_SetDefaultCharFormat(editor, p);
3312 else {
3313 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), p);
3314 editor->nModifyStep = 1;
3316 } else if (editor->mode & TM_PLAINTEXT) {
3317 return 0;
3318 } else {
3319 int from, to;
3320 ME_GetSelection(editor, &from, &to);
3321 bRepaint = (from != to);
3322 ME_SetSelectionCharFormat(editor, p);
3323 if (from != to) editor->nModifyStep = 1;
3325 ME_CommitUndo(editor);
3326 if (bRepaint)
3328 ME_WrapMarkedParagraphs(editor);
3329 ME_UpdateScrollBar(editor);
3330 ME_Repaint(editor);
3332 return 1;
3334 case EM_GETCHARFORMAT:
3336 CHARFORMAT2W tmp, *dst = (CHARFORMAT2W *)lParam;
3337 if (dst->cbSize != sizeof(CHARFORMATA) &&
3338 dst->cbSize != sizeof(CHARFORMATW) &&
3339 dst->cbSize != sizeof(CHARFORMAT2A) &&
3340 dst->cbSize != sizeof(CHARFORMAT2W))
3341 return 0;
3342 tmp.cbSize = sizeof(tmp);
3343 if (!wParam)
3344 ME_GetDefaultCharFormat(editor, &tmp);
3345 else
3346 ME_GetSelectionCharFormat(editor, &tmp);
3347 ME_CopyToCFAny(dst, &tmp);
3348 return tmp.dwMask;
3350 case EM_SETPARAFORMAT:
3352 BOOL result = ME_SetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
3353 ME_WrapMarkedParagraphs(editor);
3354 ME_UpdateScrollBar(editor);
3355 ME_Repaint(editor);
3356 ME_CommitUndo(editor);
3357 return result;
3359 case EM_GETPARAFORMAT:
3360 ME_GetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
3361 return ((PARAFORMAT2 *)lParam)->dwMask;
3362 case EM_GETFIRSTVISIBLELINE:
3364 ME_DisplayItem *p = editor->pBuffer->pFirst;
3365 int y = editor->vert_si.nPos;
3366 int ypara = 0;
3367 int count = 0;
3368 int ystart, yend;
3369 while(p) {
3370 p = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
3371 if (p->type == diTextEnd)
3372 break;
3373 if (p->type == diParagraph) {
3374 ypara = p->member.para.pt.y;
3375 continue;
3377 ystart = ypara + p->member.row.pt.y;
3378 yend = ystart + p->member.row.nHeight;
3379 if (y < yend) {
3380 break;
3382 count++;
3384 return count;
3386 case EM_HIDESELECTION:
3388 editor->bHideSelection = (wParam != 0);
3389 ME_InvalidateSelection(editor);
3390 return 0;
3392 case EM_LINESCROLL:
3394 if (!(editor->styleFlags & ES_MULTILINE))
3395 return FALSE;
3396 ME_ScrollDown(editor, lParam * 8); /* FIXME follow the original */
3397 return TRUE;
3399 case WM_CLEAR:
3401 int from, to;
3402 ME_GetSelection(editor, &from, &to);
3403 ME_InternalDeleteText(editor, from, to-from, FALSE);
3404 ME_CommitUndo(editor);
3405 ME_UpdateRepaint(editor);
3406 return 0;
3408 case EM_REPLACESEL:
3410 int from, to;
3411 ME_Style *style;
3412 LPWSTR wszText = lParam ? ME_ToUnicode(unicode, (void *)lParam) : NULL;
3413 size_t len = wszText ? lstrlenW(wszText) : 0;
3414 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText));
3416 ME_GetSelection(editor, &from, &to);
3417 style = ME_GetSelectionInsertStyle(editor);
3418 ME_InternalDeleteText(editor, from, to-from, FALSE);
3419 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
3420 ME_ReleaseStyle(style);
3421 /* drop temporary style if line end */
3423 * FIXME question: does abc\n mean: put abc,
3424 * clear temp style, put \n? (would require a change)
3426 if (len>0 && wszText[len-1] == '\n')
3427 ME_ClearTempStyle(editor);
3428 ME_EndToUnicode(unicode, wszText);
3429 ME_CommitUndo(editor);
3430 ME_UpdateSelectionLinkAttribute(editor);
3431 if (!wParam)
3432 ME_EmptyUndoStack(editor);
3433 ME_UpdateRepaint(editor);
3434 return len;
3436 case EM_SCROLLCARET:
3437 ME_EnsureVisible(editor, &editor->pCursors[0]);
3438 return 0;
3439 case WM_SETFONT:
3441 LOGFONTW lf;
3442 CHARFORMAT2W fmt;
3443 HDC hDC;
3444 BOOL bRepaint = LOWORD(lParam);
3446 if (!wParam)
3447 wParam = (WPARAM)GetStockObject(SYSTEM_FONT);
3448 GetObjectW((HGDIOBJ)wParam, sizeof(LOGFONTW), &lf);
3449 hDC = ITextHost_TxGetDC(editor->texthost);
3450 ME_CharFormatFromLogFont(hDC, &lf, &fmt);
3451 ITextHost_TxReleaseDC(editor->texthost, hDC);
3452 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), &fmt);
3453 ME_SetDefaultCharFormat(editor, &fmt);
3455 ME_CommitUndo(editor);
3456 ME_MarkAllForWrapping(editor);
3457 ME_WrapMarkedParagraphs(editor);
3458 ME_UpdateScrollBar(editor);
3459 if (bRepaint)
3460 ME_Repaint(editor);
3461 return 0;
3463 case WM_SETTEXT:
3465 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor), FALSE);
3466 if (lParam)
3468 TRACE("WM_SETTEXT lParam==%lx\n",lParam);
3469 if (!strncmp((char *)lParam, "{\\rtf", 5) ||
3470 !strncmp((char *)lParam, "{\\urtf", 6))
3472 /* Undocumented: WM_SETTEXT supports RTF text */
3473 ME_StreamInRTFString(editor, 0, (char *)lParam);
3475 else
3477 LPWSTR wszText = ME_ToUnicode(unicode, (void *)lParam);
3478 TRACE("WM_SETTEXT - %s\n", debugstr_w(wszText)); /* debugstr_w() */
3479 if (lstrlenW(wszText) > 0)
3481 int len = -1;
3483 /* uses default style! */
3484 if (!(editor->styleFlags & ES_MULTILINE))
3486 WCHAR * p;
3488 p = wszText;
3489 while (*p != '\0' && *p != '\r' && *p != '\n') p++;
3490 len = p - wszText;
3492 ME_InsertTextFromCursor(editor, 0, wszText, len, editor->pBuffer->pDefaultStyle);
3494 ME_EndToUnicode(unicode, wszText);
3497 else
3498 TRACE("WM_SETTEXT - NULL\n");
3499 ME_UpdateLinkAttribute(editor, 0, -1);
3500 ME_SetSelection(editor, 0, 0);
3501 editor->nModifyStep = 0;
3502 ME_CommitUndo(editor);
3503 ME_EmptyUndoStack(editor);
3504 ME_UpdateRepaint(editor);
3505 return 1;
3507 case EM_CANPASTE:
3509 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
3510 if (IsClipboardFormatAvailable(nRTFFormat))
3511 return TRUE;
3512 if (IsClipboardFormatAvailable(CF_UNICODETEXT))
3513 return TRUE;
3514 return FALSE;
3516 case WM_PASTE:
3517 ME_Paste(editor);
3518 return 0;
3519 case WM_CUT:
3520 case WM_COPY:
3522 CHARRANGE range;
3523 ME_GetSelection(editor, &range.cpMin, &range.cpMax);
3525 if (ME_Copy(editor, &range) && msg == WM_CUT)
3527 ME_InternalDeleteText(editor, range.cpMin, range.cpMax-range.cpMin, FALSE);
3528 ME_CommitUndo(editor);
3529 ME_UpdateRepaint(editor);
3531 return 0;
3533 case WM_GETTEXTLENGTH:
3535 GETTEXTLENGTHEX how;
3537 /* CR/LF conversion required in 2.0 mode, verbatim in 1.0 mode */
3538 how.flags = GTL_CLOSE | (editor->bEmulateVersion10 ? 0 : GTL_USECRLF) | GTL_NUMCHARS;
3539 how.codepage = unicode ? 1200 : CP_ACP;
3540 return ME_GetTextLengthEx(editor, &how);
3542 case EM_GETTEXTLENGTHEX:
3543 return ME_GetTextLengthEx(editor, (GETTEXTLENGTHEX *)wParam);
3544 case WM_GETTEXT:
3546 GETTEXTEX ex;
3547 LRESULT rc;
3548 LPSTR bufferA = NULL;
3549 LPWSTR bufferW = NULL;
3551 if (unicode)
3552 bufferW = heap_alloc((wParam + 2) * sizeof(WCHAR));
3553 else
3554 bufferA = heap_alloc(wParam + 2);
3556 ex.cb = (wParam + 2) * (unicode ? sizeof(WCHAR) : sizeof(CHAR));
3557 ex.flags = GT_USECRLF;
3558 ex.codepage = unicode ? 1200 : CP_ACP;
3559 ex.lpDefaultChar = NULL;
3560 ex.lpUsedDefChar = NULL;
3562 rc = ME_GetTextEx(editor, &ex, unicode ? (LPARAM)bufferW : (LPARAM)bufferA);
3564 if (unicode)
3566 memcpy((LPWSTR)lParam, bufferW, wParam * sizeof(WCHAR));
3567 if (strlenW(bufferW) >= wParam) rc = 0;
3569 else
3571 memcpy((LPSTR)lParam, bufferA, wParam);
3572 if (strlen(bufferA) >= wParam) rc = 0;
3574 heap_free(bufferA);
3575 heap_free(bufferW);
3576 return rc;
3578 case EM_GETTEXTEX:
3579 return ME_GetTextEx(editor, (GETTEXTEX*)wParam, lParam);
3580 case EM_GETSELTEXT:
3582 int from, to;
3583 ME_GetSelection(editor, &from, &to);
3584 return ME_GetTextRange(editor, (WCHAR *)lParam, from,
3585 to - from, unicode);
3587 case EM_GETSCROLLPOS:
3589 POINT *point = (POINT *)lParam;
3590 point->x = editor->horz_si.nPos;
3591 point->y = editor->vert_si.nPos;
3592 /* 16-bit scaled value is returned as stored in scrollinfo */
3593 if (editor->horz_si.nMax > 0xffff)
3594 point->x = MulDiv(point->x, 0xffff, editor->horz_si.nMax);
3595 if (editor->vert_si.nMax > 0xffff)
3596 point->y = MulDiv(point->y, 0xffff, editor->vert_si.nMax);
3597 return 1;
3599 case EM_GETTEXTRANGE:
3601 TEXTRANGEW *rng = (TEXTRANGEW *)lParam;
3602 int start = rng->chrg.cpMin;
3603 int end = rng->chrg.cpMax;
3604 int textlength = ME_GetTextLength(editor);
3605 TRACE("EM_GETTEXTRANGE min=%d max=%d unicode=%d emul1.0=%d length=%d\n",
3606 rng->chrg.cpMin, rng->chrg.cpMax, unicode,
3607 editor->bEmulateVersion10, ME_GetTextLength(editor));
3608 if (start < 0) return 0;
3609 if ((start == 0 && end == -1) || end > textlength)
3610 end = textlength;
3611 if (start >= end) return 0;
3612 return ME_GetTextRange(editor, rng->lpstrText, start, end - start, unicode);
3614 case EM_GETLINE:
3616 ME_DisplayItem *run;
3617 const unsigned int nMaxChars = *(WORD *) lParam;
3618 unsigned int nCharsLeft = nMaxChars;
3619 char *dest = (char *) lParam;
3620 BOOL wroteNull = FALSE;
3622 TRACE("EM_GETLINE: row=%d, nMaxChars=%d (%s)\n", (int) wParam, nMaxChars,
3623 unicode ? "Unicode" : "Ansi");
3625 run = ME_FindRowWithNumber(editor, wParam);
3626 if (run == NULL)
3627 return 0;
3629 while (nCharsLeft && (run = ME_FindItemFwd(run, diRunOrStartRow))
3630 && run->type == diRun)
3632 unsigned int nCopy;
3633 ME_String *strText;
3635 strText = run->member.run.strText;
3636 nCopy = min(nCharsLeft, strText->nLen);
3638 if (unicode)
3639 memcpy(dest, strText->szData, nCopy * sizeof(WCHAR));
3640 else
3641 nCopy = WideCharToMultiByte(CP_ACP, 0, strText->szData, nCopy, dest,
3642 nCharsLeft, NULL, NULL);
3643 dest += nCopy * (unicode ? sizeof(WCHAR) : 1);
3644 nCharsLeft -= nCopy;
3647 /* append line termination, space allowing */
3648 if (nCharsLeft > 0)
3650 if (unicode)
3651 *((WCHAR *)dest) = '\0';
3652 else
3653 *dest = '\0';
3654 nCharsLeft--;
3655 wroteNull = TRUE;
3658 TRACE("EM_GETLINE: got %u characters\n", nMaxChars - nCharsLeft);
3659 return nMaxChars - nCharsLeft - (wroteNull ? 1 : 0);
3661 case EM_GETLINECOUNT:
3663 ME_DisplayItem *item = editor->pBuffer->pFirst->next;
3664 int nRows = 0;
3666 ME_DisplayItem *prev_para = NULL, *last_para = NULL;
3668 while (item != editor->pBuffer->pLast)
3670 assert(item->type == diParagraph);
3671 prev_para = ME_FindItemBack(item, diRun);
3672 if (prev_para) {
3673 assert(prev_para->member.run.nFlags & MERF_ENDPARA);
3675 nRows += item->member.para.nRows;
3676 item = item->member.para.next_para;
3678 last_para = ME_FindItemBack(item, diRun);
3679 assert(last_para);
3680 assert(last_para->member.run.nFlags & MERF_ENDPARA);
3681 if (editor->bEmulateVersion10 && prev_para &&
3682 last_para->member.run.nCharOfs == 0 &&
3683 prev_para->member.run.strText->nLen == 1 &&
3684 prev_para->member.run.strText->szData[0] == '\r')
3686 /* In 1.0 emulation, the last solitary \r at the very end of the text
3687 (if one exists) is NOT a line break.
3688 FIXME: this is an ugly hack. This should have a more regular model. */
3689 nRows--;
3692 TRACE("EM_GETLINECOUNT: nRows==%d\n", nRows);
3693 return max(1, nRows);
3695 case EM_LINEFROMCHAR:
3697 if (wParam == -1)
3698 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(editor, 1));
3699 else
3700 return ME_RowNumberFromCharOfs(editor, wParam);
3702 case EM_EXLINEFROMCHAR:
3704 if (lParam == -1)
3705 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(editor,1));
3706 else
3707 return ME_RowNumberFromCharOfs(editor, lParam);
3709 case EM_LINEINDEX:
3711 ME_DisplayItem *item, *para;
3712 int nCharOfs;
3714 if (wParam == -1)
3715 item = ME_FindItemBack(editor->pCursors[0].pRun, diStartRow);
3716 else
3717 item = ME_FindRowWithNumber(editor, wParam);
3718 if (!item)
3719 return -1;
3720 para = ME_GetParagraph(item);
3721 item = ME_FindItemFwd(item, diRun);
3722 nCharOfs = para->member.para.nCharOfs + item->member.run.nCharOfs;
3723 TRACE("EM_LINEINDEX: nCharOfs==%d\n", nCharOfs);
3724 return nCharOfs;
3726 case EM_LINELENGTH:
3728 ME_DisplayItem *item, *item_end;
3729 int nChars = 0, nThisLineOfs = 0, nNextLineOfs = 0;
3730 ME_DisplayItem *para, *run;
3732 if (wParam > ME_GetTextLength(editor))
3733 return 0;
3734 if (wParam == -1)
3736 FIXME("EM_LINELENGTH: returning number of unselected characters on lines with selection unsupported.\n");
3737 return 0;
3739 ME_RunOfsFromCharOfs(editor, wParam, &para, &run, NULL);
3740 item = ME_RowStart(run);
3741 nThisLineOfs = ME_CharOfsFromRunOfs(editor, para, ME_FindItemFwd(item, diRun), 0);
3742 item_end = ME_FindItemFwd(item, diStartRowOrParagraphOrEnd);
3743 if (item_end->type == diStartRow) {
3744 nNextLineOfs = ME_CharOfsFromRunOfs(editor, para, ME_FindItemFwd(item_end, diRun), 0);
3745 } else {
3746 ME_DisplayItem *endRun = ME_FindItemBack(item_end, diRun);
3747 assert(endRun && endRun->member.run.nFlags & MERF_ENDPARA);
3748 nNextLineOfs = item_end->member.para.nCharOfs - endRun->member.run.strText->nLen;
3750 nChars = nNextLineOfs - nThisLineOfs;
3751 TRACE("EM_LINELENGTH(%ld)==%d\n",wParam, nChars);
3752 return nChars;
3754 case EM_EXLIMITTEXT:
3756 if ((int)lParam < 0)
3757 return 0;
3758 if (lParam == 0)
3759 editor->nTextLimit = 65536;
3760 else
3761 editor->nTextLimit = (int) lParam;
3762 return 0;
3764 case EM_LIMITTEXT:
3766 if (wParam == 0)
3767 editor->nTextLimit = 65536;
3768 else
3769 editor->nTextLimit = (int) wParam;
3770 return 0;
3772 case EM_GETLIMITTEXT:
3774 return editor->nTextLimit;
3776 case EM_FINDTEXT:
3778 FINDTEXTA *ft = (FINDTEXTA *)lParam;
3779 int nChars = MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, NULL, 0);
3780 WCHAR *tmp;
3781 LRESULT r;
3783 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
3784 MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, tmp, nChars);
3785 r = ME_FindText(editor, wParam, &ft->chrg, tmp, NULL);
3786 FREE_OBJ( tmp );
3787 return r;
3789 case EM_FINDTEXTEX:
3791 FINDTEXTEXA *ex = (FINDTEXTEXA *)lParam;
3792 int nChars = MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, NULL, 0);
3793 WCHAR *tmp;
3794 LRESULT r;
3796 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
3797 MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, tmp, nChars);
3798 r = ME_FindText(editor, wParam, &ex->chrg, tmp, &ex->chrgText);
3799 FREE_OBJ( tmp );
3800 return r;
3802 case EM_FINDTEXTW:
3804 FINDTEXTW *ft = (FINDTEXTW *)lParam;
3805 return ME_FindText(editor, wParam, &ft->chrg, ft->lpstrText, NULL);
3807 case EM_FINDTEXTEXW:
3809 FINDTEXTEXW *ex = (FINDTEXTEXW *)lParam;
3810 return ME_FindText(editor, wParam, &ex->chrg, ex->lpstrText, &ex->chrgText);
3812 case EM_GETZOOM:
3813 if (!wParam || !lParam)
3814 return FALSE;
3815 *(int *)wParam = editor->nZoomNumerator;
3816 *(int *)lParam = editor->nZoomDenominator;
3817 return TRUE;
3818 case EM_SETZOOM:
3819 return ME_SetZoom(editor, wParam, lParam);
3820 case EM_CHARFROMPOS:
3821 return ME_CharFromPos(editor, ((POINTL *)lParam)->x, ((POINTL *)lParam)->y, NULL);
3822 case EM_POSFROMCHAR:
3824 ME_DisplayItem *pPara, *pRun;
3825 int nCharOfs, nOffset, nLength;
3826 POINTL pt = {0,0};
3828 nCharOfs = wParam;
3829 /* detect which API version we're dealing with */
3830 if (wParam >= 0x40000)
3831 nCharOfs = lParam;
3832 nLength = ME_GetTextLength(editor);
3833 nCharOfs = min(nCharOfs, nLength);
3834 nCharOfs = max(nCharOfs, 0);
3836 ME_RunOfsFromCharOfs(editor, nCharOfs, &pPara, &pRun, &nOffset);
3837 assert(pRun->type == diRun);
3838 pt.y = pRun->member.run.pt.y;
3839 pt.x = pRun->member.run.pt.x + ME_PointFromChar(editor, &pRun->member.run, nOffset);
3840 pt.y += pPara->member.para.pt.y + editor->rcFormat.top;
3841 pt.x += editor->rcFormat.left;
3843 pt.x -= editor->horz_si.nPos;
3844 pt.y -= editor->vert_si.nPos;
3846 if (wParam >= 0x40000) {
3847 *(POINTL *)wParam = pt;
3849 return (wParam >= 0x40000) ? 0 : MAKELONG( pt.x, pt.y );
3851 case WM_CREATE:
3853 INT max;
3855 ME_SetDefaultFormatRect(editor);
3857 max = (editor->styleFlags & ES_DISABLENOSCROLL) ? 1 : 0;
3858 if (~editor->styleFlags & ES_DISABLENOSCROLL || editor->styleFlags & WS_VSCROLL)
3859 ITextHost_TxSetScrollRange(editor->texthost, SB_VERT, 0, max, TRUE);
3861 if (~editor->styleFlags & ES_DISABLENOSCROLL || editor->styleFlags & WS_HSCROLL)
3862 ITextHost_TxSetScrollRange(editor->texthost, SB_HORZ, 0, max, TRUE);
3864 if (editor->styleFlags & ES_DISABLENOSCROLL)
3866 if (editor->styleFlags & WS_VSCROLL)
3868 ITextHost_TxEnableScrollBar(editor->texthost, SB_VERT, ESB_DISABLE_BOTH);
3869 ITextHost_TxShowScrollBar(editor->texthost, SB_VERT, TRUE);
3871 if (editor->styleFlags & WS_HSCROLL)
3873 ITextHost_TxEnableScrollBar(editor->texthost, SB_HORZ, ESB_DISABLE_BOTH);
3874 ITextHost_TxShowScrollBar(editor->texthost, SB_HORZ, TRUE);
3878 ME_CommitUndo(editor);
3879 ME_WrapMarkedParagraphs(editor);
3880 ME_MoveCaret(editor);
3881 return 0;
3883 case WM_DESTROY:
3884 ME_DestroyEditor(editor);
3885 return 0;
3886 case WM_SETCURSOR:
3888 return ME_SetCursor(editor);
3890 case WM_LBUTTONDBLCLK:
3891 case WM_LBUTTONDOWN:
3893 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
3894 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
3895 !ME_FilterEvent(editor, msg, &wParam, &lParam))
3896 return 0;
3897 ITextHost_TxSetFocus(editor->texthost);
3898 ME_LButtonDown(editor, (short)LOWORD(lParam), (short)HIWORD(lParam),
3899 ME_CalculateClickCount(editor, msg, wParam, lParam));
3900 ITextHost_TxSetCapture(editor->texthost, TRUE);
3901 editor->bMouseCaptured = TRUE;
3902 ME_LinkNotify(editor,msg,wParam,lParam);
3903 if (!ME_SetCursor(editor)) goto do_default;
3904 break;
3906 case WM_MOUSEMOVE:
3907 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
3908 !ME_FilterEvent(editor, msg, &wParam, &lParam))
3909 return 0;
3910 if (editor->bMouseCaptured)
3911 ME_MouseMove(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
3912 ME_LinkNotify(editor,msg,wParam,lParam);
3913 /* Set cursor if mouse is captured, since WM_SETCURSOR won't be received. */
3914 if (editor->bMouseCaptured)
3915 ME_SetCursor(editor);
3916 break;
3917 case WM_LBUTTONUP:
3918 if (editor->bMouseCaptured) {
3919 ITextHost_TxSetCapture(editor->texthost, FALSE);
3920 editor->bMouseCaptured = FALSE;
3922 if (editor->nSelectionType == stDocument)
3923 editor->nSelectionType = stPosition;
3924 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
3925 !ME_FilterEvent(editor, msg, &wParam, &lParam))
3926 return 0;
3927 else
3929 ME_SetCursor(editor);
3930 ME_LinkNotify(editor,msg,wParam,lParam);
3932 break;
3933 case WM_RBUTTONUP:
3934 case WM_RBUTTONDOWN:
3935 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
3936 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
3937 !ME_FilterEvent(editor, msg, &wParam, &lParam))
3938 return 0;
3939 goto do_default;
3940 case WM_CONTEXTMENU:
3941 if (!ME_ShowContextMenu(editor, (short)LOWORD(lParam), (short)HIWORD(lParam)))
3942 goto do_default;
3943 break;
3944 case WM_SETFOCUS:
3945 editor->bHaveFocus = TRUE;
3946 ME_ShowCaret(editor);
3947 ME_SendOldNotify(editor, EN_SETFOCUS);
3948 return 0;
3949 case WM_KILLFOCUS:
3950 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
3951 editor->bHaveFocus = FALSE;
3952 ME_HideCaret(editor);
3953 ME_SendOldNotify(editor, EN_KILLFOCUS);
3954 return 0;
3955 case WM_COMMAND:
3956 TRACE("editor wnd command = %d\n", LOWORD(wParam));
3957 return 0;
3958 case WM_KEYUP:
3959 if ((editor->nEventMask & ENM_KEYEVENTS) &&
3960 !ME_FilterEvent(editor, msg, &wParam, &lParam))
3961 return 0;
3962 goto do_default;
3963 case WM_KEYDOWN:
3964 if ((editor->nEventMask & ENM_KEYEVENTS) &&
3965 !ME_FilterEvent(editor, msg, &wParam, &lParam))
3966 return 0;
3967 if (ME_KeyDown(editor, LOWORD(wParam)))
3968 return 0;
3969 goto do_default;
3970 case WM_CHAR:
3971 return ME_Char(editor, wParam, lParam, unicode);
3972 case WM_UNICHAR:
3973 if (unicode)
3975 if(wParam == UNICODE_NOCHAR) return TRUE;
3976 if(wParam <= 0x000fffff)
3978 if(wParam > 0xffff) /* convert to surrogates */
3980 wParam -= 0x10000;
3981 ME_Char(editor, (wParam >> 10) + 0xd800, 0, TRUE);
3982 ME_Char(editor, (wParam & 0x03ff) + 0xdc00, 0, TRUE);
3983 } else {
3984 ME_Char(editor, wParam, 0, TRUE);
3987 return 0;
3989 break;
3990 case EM_STOPGROUPTYPING:
3991 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
3992 return 0;
3993 case WM_HSCROLL:
3995 const int scrollUnit = 7;
3997 switch(LOWORD(wParam))
3999 case SB_LEFT:
4000 ME_ScrollAbs(editor, 0, 0);
4001 break;
4002 case SB_RIGHT:
4003 ME_ScrollAbs(editor,
4004 editor->horz_si.nMax - (int)editor->horz_si.nPage,
4005 editor->vert_si.nMax - (int)editor->vert_si.nPage);
4006 break;
4007 case SB_LINELEFT:
4008 ME_ScrollLeft(editor, scrollUnit);
4009 break;
4010 case SB_LINERIGHT:
4011 ME_ScrollRight(editor, scrollUnit);
4012 break;
4013 case SB_PAGELEFT:
4014 ME_ScrollLeft(editor, editor->sizeWindow.cx);
4015 break;
4016 case SB_PAGERIGHT:
4017 ME_ScrollRight(editor, editor->sizeWindow.cx);
4018 break;
4019 case SB_THUMBTRACK:
4020 case SB_THUMBPOSITION:
4022 int pos = HIWORD(wParam);
4023 if (editor->horz_si.nMax > 0xffff)
4024 pos = MulDiv(pos, editor->horz_si.nMax, 0xffff);
4025 ME_HScrollAbs(editor, pos);
4026 break;
4029 break;
4031 case EM_SCROLL: /* fall through */
4032 case WM_VSCROLL:
4034 int origNPos;
4035 int lineHeight;
4037 origNPos = editor->vert_si.nPos;
4038 lineHeight = 24;
4040 if (editor->pBuffer && editor->pBuffer->pDefaultStyle)
4041 lineHeight = editor->pBuffer->pDefaultStyle->tm.tmHeight;
4042 if (lineHeight <= 0) lineHeight = 24;
4044 switch(LOWORD(wParam))
4046 case SB_TOP:
4047 ME_ScrollAbs(editor, 0, 0);
4048 break;
4049 case SB_BOTTOM:
4050 ME_ScrollAbs(editor,
4051 editor->horz_si.nMax - (int)editor->horz_si.nPage,
4052 editor->vert_si.nMax - (int)editor->vert_si.nPage);
4053 break;
4054 case SB_LINEUP:
4055 ME_ScrollUp(editor,lineHeight);
4056 break;
4057 case SB_LINEDOWN:
4058 ME_ScrollDown(editor,lineHeight);
4059 break;
4060 case SB_PAGEUP:
4061 ME_ScrollUp(editor,editor->sizeWindow.cy);
4062 break;
4063 case SB_PAGEDOWN:
4064 ME_ScrollDown(editor,editor->sizeWindow.cy);
4065 break;
4066 case SB_THUMBTRACK:
4067 case SB_THUMBPOSITION:
4069 int pos = HIWORD(wParam);
4070 if (editor->vert_si.nMax > 0xffff)
4071 pos = MulDiv(pos, editor->vert_si.nMax, 0xffff);
4072 ME_VScrollAbs(editor, pos);
4073 break;
4076 if (msg == EM_SCROLL)
4077 return 0x00010000 | (((editor->vert_si.nPos - origNPos)/lineHeight) & 0xffff);
4078 break;
4080 case WM_MOUSEWHEEL:
4082 int gcWheelDelta;
4083 UINT pulScrollLines;
4084 BOOL ctrl_is_down;
4086 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
4087 !ME_FilterEvent(editor, msg, &wParam, &lParam))
4088 return 0;
4090 ctrl_is_down = GetKeyState(VK_CONTROL) & 0x8000;
4092 gcWheelDelta = GET_WHEEL_DELTA_WPARAM(wParam);
4094 if (abs(gcWheelDelta) >= WHEEL_DELTA)
4096 if (ctrl_is_down) {
4097 int numerator;
4098 if (!editor->nZoomNumerator || !editor->nZoomDenominator)
4100 numerator = 100;
4101 } else {
4102 numerator = editor->nZoomNumerator * 100 / editor->nZoomDenominator;
4104 numerator = numerator + (gcWheelDelta / WHEEL_DELTA) * 10;
4105 if (numerator >= 10 && numerator <= 500)
4106 ME_SetZoom(editor, numerator, 100);
4107 } else {
4108 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
4109 /* FIXME follow the original */
4110 if (pulScrollLines)
4111 ME_ScrollDown(editor,pulScrollLines * (-gcWheelDelta / WHEEL_DELTA) * 8);
4114 break;
4116 case EM_GETRECT:
4118 *((RECT *)lParam) = editor->rcFormat;
4119 if (editor->bDefaultFormatRect)
4120 ((RECT *)lParam)->left -= editor->selofs;
4121 return 0;
4123 case EM_SETRECT:
4124 case EM_SETRECTNP:
4126 if (lParam)
4128 int border = 0;
4129 RECT clientRect;
4130 RECT *rc = (RECT *)lParam;
4132 border = editor->exStyleFlags & WS_EX_CLIENTEDGE ? 1 : 0;
4133 ITextHost_TxGetClientRect(editor->texthost, &clientRect);
4134 if (wParam == 0)
4136 editor->rcFormat.top = max(0, rc->top - border);
4137 editor->rcFormat.left = max(0, rc->left - border);
4138 editor->rcFormat.bottom = min(clientRect.bottom, rc->bottom);
4139 editor->rcFormat.right = min(clientRect.right, rc->right + border);
4140 } else if (wParam == 1) {
4141 /* MSDN incorrectly says a wParam value of 1 causes the
4142 * lParam rect to be used as a relative offset,
4143 * however, the tests show it just prevents min/max bound
4144 * checking. */
4145 editor->rcFormat.top = rc->top - border;
4146 editor->rcFormat.left = rc->left - border;
4147 editor->rcFormat.bottom = rc->bottom;
4148 editor->rcFormat.right = rc->right + border;
4149 } else {
4150 return 0;
4152 editor->bDefaultFormatRect = FALSE;
4154 else
4156 ME_SetDefaultFormatRect(editor);
4157 editor->bDefaultFormatRect = TRUE;
4159 ME_MarkAllForWrapping(editor);
4160 ME_WrapMarkedParagraphs(editor);
4161 ME_UpdateScrollBar(editor);
4162 if (msg != EM_SETRECTNP)
4163 ME_Repaint(editor);
4164 return 0;
4166 case EM_REQUESTRESIZE:
4167 ME_SendRequestResize(editor, TRUE);
4168 return 0;
4169 case WM_SETREDRAW:
4170 goto do_default;
4171 case WM_SIZE:
4173 RECT clientRect;
4175 ITextHost_TxGetClientRect(editor->texthost, &clientRect);
4176 if (editor->bDefaultFormatRect) {
4177 ME_SetDefaultFormatRect(editor);
4178 } else {
4179 editor->rcFormat.right += clientRect.right - editor->prevClientRect.right;
4180 editor->rcFormat.bottom += clientRect.bottom - editor->prevClientRect.bottom;
4182 editor->prevClientRect = clientRect;
4183 ME_RewrapRepaint(editor);
4184 goto do_default;
4186 /* IME messages to make richedit controls IME aware */
4187 case WM_IME_SETCONTEXT:
4188 case WM_IME_CONTROL:
4189 case WM_IME_SELECT:
4190 case WM_IME_COMPOSITIONFULL:
4191 return 0;
4192 case WM_IME_STARTCOMPOSITION:
4194 editor->imeStartIndex=ME_GetCursorOfs(editor,0);
4195 ME_DeleteSelection(editor);
4196 ME_CommitUndo(editor);
4197 ME_UpdateRepaint(editor);
4198 return 0;
4200 case WM_IME_COMPOSITION:
4202 HIMC hIMC;
4204 ME_Style *style = ME_GetInsertStyle(editor, 0);
4205 hIMC = ITextHost_TxImmGetContext(editor->texthost);
4206 ME_DeleteSelection(editor);
4207 ME_CommitUndo(editor);
4208 ME_SaveTempStyle(editor);
4209 if (lParam & GCS_RESULTSTR)
4211 LPWSTR lpCompStr = NULL;
4212 DWORD dwBufLen;
4214 dwBufLen = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
4215 lpCompStr = HeapAlloc(GetProcessHeap(),0,dwBufLen + sizeof(WCHAR));
4216 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, lpCompStr, dwBufLen);
4217 lpCompStr[dwBufLen/sizeof(WCHAR)] = 0;
4218 ME_InsertTextFromCursor(editor,0,lpCompStr,dwBufLen/sizeof(WCHAR),style);
4220 else if (lParam & GCS_COMPSTR)
4222 LPWSTR lpCompStr = NULL;
4223 DWORD dwBufLen;
4225 dwBufLen = ImmGetCompositionStringW(hIMC, GCS_COMPSTR, NULL, 0);
4226 lpCompStr = HeapAlloc(GetProcessHeap(),0,dwBufLen + sizeof(WCHAR));
4227 ImmGetCompositionStringW(hIMC, GCS_COMPSTR, lpCompStr, dwBufLen);
4228 lpCompStr[dwBufLen/sizeof(WCHAR)] = 0;
4230 ME_InsertTextFromCursor(editor,0,lpCompStr,dwBufLen/sizeof(WCHAR),style);
4231 ME_SetSelection(editor,editor->imeStartIndex,
4232 editor->imeStartIndex + dwBufLen/sizeof(WCHAR));
4234 ME_ReleaseStyle(style);
4235 ME_UpdateRepaint(editor);
4236 return 0;
4238 case WM_IME_ENDCOMPOSITION:
4240 ME_DeleteSelection(editor);
4241 editor->imeStartIndex=-1;
4242 return 0;
4244 case EM_GETOLEINTERFACE:
4246 LPVOID *ppvObj = (LPVOID*) lParam;
4247 return CreateIRichEditOle(editor, ppvObj);
4249 case EM_GETPASSWORDCHAR:
4251 return editor->cPasswordMask;
4253 case EM_SETOLECALLBACK:
4254 if(editor->lpOleCallback)
4255 IUnknown_Release(editor->lpOleCallback);
4256 editor->lpOleCallback = (LPRICHEDITOLECALLBACK)lParam;
4257 if(editor->lpOleCallback)
4258 IUnknown_AddRef(editor->lpOleCallback);
4259 return TRUE;
4260 case EM_GETWORDBREAKPROC:
4261 return (LRESULT)editor->pfnWordBreak;
4262 case EM_SETWORDBREAKPROC:
4264 EDITWORDBREAKPROCW pfnOld = editor->pfnWordBreak;
4266 editor->pfnWordBreak = (EDITWORDBREAKPROCW)lParam;
4267 return (LRESULT)pfnOld;
4269 case EM_GETTEXTMODE:
4270 return editor->mode;
4271 case EM_SETTEXTMODE:
4273 LRESULT ret;
4274 int mask = 0;
4275 int changes = 0;
4276 GETTEXTLENGTHEX how;
4278 /* CR/LF conversion required in 2.0 mode, verbatim in 1.0 mode */
4279 how.flags = GTL_CLOSE | (editor->bEmulateVersion10 ? 0 : GTL_USECRLF) | GTL_NUMCHARS;
4280 how.codepage = unicode ? 1200 : CP_ACP;
4281 ret = ME_GetTextLengthEx(editor, &how);
4282 if (!ret)
4284 /*Check for valid wParam*/
4285 if ((((wParam & TM_RICHTEXT) && ((wParam & TM_PLAINTEXT) << 1))) ||
4286 (((wParam & TM_MULTILEVELUNDO) && ((wParam & TM_SINGLELEVELUNDO) << 1))) ||
4287 (((wParam & TM_MULTICODEPAGE) && ((wParam & TM_SINGLECODEPAGE) << 1))))
4288 return 1;
4289 else
4291 if (wParam & (TM_RICHTEXT | TM_PLAINTEXT))
4293 mask |= (TM_RICHTEXT | TM_PLAINTEXT);
4294 changes |= (wParam & (TM_RICHTEXT | TM_PLAINTEXT));
4296 /*FIXME: Currently no support for undo level and code page options*/
4297 editor->mode = (editor->mode & (~mask)) | changes;
4298 return 0;
4301 return ret;
4303 case EM_SETPASSWORDCHAR:
4305 editor->cPasswordMask = wParam;
4306 ME_RewrapRepaint(editor);
4307 return 0;
4309 case EM_SETTARGETDEVICE:
4310 if (wParam == 0)
4312 BOOL new = (lParam == 0 && (editor->styleFlags & ES_MULTILINE));
4313 if (editor->nAvailWidth || editor->bWordWrap != new)
4315 editor->bWordWrap = new;
4316 editor->nAvailWidth = 0; /* wrap to client area */
4317 ME_RewrapRepaint(editor);
4319 } else {
4320 int width = max(0, lParam);
4321 if ((editor->styleFlags & ES_MULTILINE) &&
4322 (!editor->bWordWrap || editor->nAvailWidth != width))
4324 editor->nAvailWidth = width;
4325 editor->bWordWrap = TRUE;
4326 ME_RewrapRepaint(editor);
4328 FIXME("EM_SETTARGETDEVICE doesn't use non-NULL target devices\n");
4330 return TRUE;
4331 default:
4332 do_default:
4333 *phresult = S_FALSE;
4334 break;
4336 return 0L;
4339 static LRESULT RichEditWndProc_common(HWND hWnd, UINT msg, WPARAM wParam,
4340 LPARAM lParam, BOOL unicode)
4342 ME_TextEditor *editor;
4343 HRESULT hresult;
4344 LRESULT lresult = 0;
4346 TRACE("enter hwnd %p msg %04x (%s) %lx %lx, unicode %d\n",
4347 hWnd, msg, get_msg_name(msg), wParam, lParam, unicode);
4349 editor = (ME_TextEditor *)GetWindowLongPtrW(hWnd, 0);
4350 if (!editor)
4352 if (msg == WM_NCCREATE)
4354 CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
4355 ITextHost *texthost;
4357 TRACE("WM_NCCREATE: hWnd %p style 0x%08x\n", hWnd, pcs->style);
4358 texthost = ME_CreateTextHost(hWnd, FALSE);
4359 return texthost != NULL;
4361 else if (msg != WM_NCDESTROY)
4363 ERR("called with invalid hWnd %p - application bug?\n", hWnd);
4364 return 0;
4368 switch (msg)
4370 case WM_PAINT:
4372 HDC hDC;
4373 RECT rc;
4374 PAINTSTRUCT ps;
4376 hDC = BeginPaint(editor->hWnd, &ps);
4377 /* Erase area outside of the formatting rectangle */
4378 if (ps.rcPaint.top < editor->rcFormat.top)
4380 rc = ps.rcPaint;
4381 rc.bottom = editor->rcFormat.top;
4382 FillRect(hDC, &rc, editor->hbrBackground);
4383 ps.rcPaint.top = editor->rcFormat.top;
4385 if (ps.rcPaint.bottom > editor->rcFormat.bottom) {
4386 rc = ps.rcPaint;
4387 rc.top = editor->rcFormat.bottom;
4388 FillRect(hDC, &rc, editor->hbrBackground);
4389 ps.rcPaint.bottom = editor->rcFormat.bottom;
4391 if (ps.rcPaint.left < editor->rcFormat.left) {
4392 rc = ps.rcPaint;
4393 rc.right = editor->rcFormat.left;
4394 FillRect(hDC, &rc, editor->hbrBackground);
4395 ps.rcPaint.left = editor->rcFormat.left;
4397 if (ps.rcPaint.right > editor->rcFormat.right) {
4398 rc = ps.rcPaint;
4399 rc.left = editor->rcFormat.right;
4400 FillRect(hDC, &rc, editor->hbrBackground);
4401 ps.rcPaint.right = editor->rcFormat.right;
4404 ME_PaintContent(editor, hDC, FALSE, &ps.rcPaint);
4405 EndPaint(editor->hWnd, &ps);
4406 return 0;
4408 case WM_ERASEBKGND:
4410 HDC hDC = (HDC)wParam;
4411 RECT rc;
4413 if (GetUpdateRect(editor->hWnd, &rc, TRUE))
4414 FillRect(hDC, &rc, editor->hbrBackground);
4415 return 1;
4417 case EM_SETOPTIONS:
4419 DWORD dwStyle;
4420 const DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
4421 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN |
4422 ECO_SELECTIONBAR;
4423 lresult = ME_HandleMessage(editor, msg, wParam, lParam, unicode, &hresult);
4424 dwStyle = GetWindowLongW(hWnd, GWL_STYLE);
4425 dwStyle = (dwStyle & ~mask) | (lresult & mask);
4426 SetWindowLongW(hWnd, GWL_STYLE, dwStyle);
4427 return lresult;
4429 case EM_SETREADONLY:
4431 DWORD dwStyle;
4432 lresult = ME_HandleMessage(editor, msg, wParam, lParam, unicode, &hresult);
4433 dwStyle = GetWindowLongW(hWnd, GWL_STYLE);
4434 dwStyle &= ~ES_READONLY;
4435 if (wParam)
4436 dwStyle |= ES_READONLY;
4437 SetWindowLongW(hWnd, GWL_STYLE, dwStyle);
4438 return lresult;
4440 default:
4441 lresult = ME_HandleMessage(editor, msg, wParam, lParam, unicode, &hresult);
4444 if (hresult == S_FALSE)
4445 lresult = DefWindowProcW(hWnd, msg, wParam, lParam);
4447 TRACE("exit hwnd %p msg %04x (%s) %lx %lx, unicode %d -> %lu\n",
4448 hWnd, msg, get_msg_name(msg), wParam, lParam, unicode, lresult);
4450 return lresult;
4453 static LRESULT WINAPI RichEditWndProcW(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
4455 BOOL unicode = TRUE;
4457 /* Under Win9x RichEdit20W returns ANSI strings, see the tests. */
4458 if (msg == WM_GETTEXT && (GetVersion() & 0x80000000))
4459 unicode = FALSE;
4461 return RichEditWndProc_common(hWnd, msg, wParam, lParam, unicode);
4464 static LRESULT WINAPI RichEditWndProcA(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
4466 return RichEditWndProc_common(hWnd, msg, wParam, lParam, FALSE);
4469 /******************************************************************
4470 * RichEditANSIWndProc (RICHED20.10)
4472 LRESULT WINAPI RichEditANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
4474 return RichEditWndProcA(hWnd, msg, wParam, lParam);
4477 /******************************************************************
4478 * RichEdit10ANSIWndProc (RICHED20.9)
4480 LRESULT WINAPI RichEdit10ANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
4482 if (msg == WM_NCCREATE && !GetWindowLongPtrW(hWnd, 0))
4484 ITextHost *texthost;
4485 CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
4487 TRACE("WM_NCCREATE: hWnd %p style 0x%08x\n", hWnd, pcs->style);
4488 texthost = ME_CreateTextHost(hWnd, TRUE);
4489 return texthost != NULL;
4491 return RichEditANSIWndProc(hWnd, msg, wParam, lParam);
4494 void ME_SendOldNotify(ME_TextEditor *editor, int nCode)
4496 ITextHost_TxNotify(editor->texthost, nCode, NULL);
4499 int ME_CountParagraphsBetween(ME_TextEditor *editor, int from, int to)
4501 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
4502 int i = 0;
4504 while(item && item->member.para.next_para->member.para.nCharOfs <= from)
4505 item = item->member.para.next_para;
4506 if (!item)
4507 return 0;
4508 while(item && item->member.para.next_para->member.para.nCharOfs <= to) {
4509 item = item->member.para.next_para;
4510 i++;
4512 return i;
4516 int ME_GetTextW(ME_TextEditor *editor, WCHAR *buffer, int nStart,
4517 int nChars, int bCRLF)
4519 ME_DisplayItem *pRun;
4520 int nOffset, nWritten = 0;
4521 WCHAR *pStart = buffer;
4523 ME_RunOfsFromCharOfs(editor, nStart, NULL, &pRun, &nOffset);
4525 /* bCRLF flag is only honored in 2.0 and up. 1.0 must always return text verbatim */
4526 if (editor->bEmulateVersion10) bCRLF = 0;
4528 while (nChars && pRun)
4530 int nLen;
4532 if (pRun->member.run.nFlags & MERF_ENDCELL &&
4533 pRun->member.run.nFlags & MERF_ENDPARA)
4535 *buffer = '\t';
4536 nLen = 1;
4537 } else if (pRun->member.run.nFlags & MERF_ENDPARA) {
4538 if (!ME_FindItemFwd(pRun, diRun)) {
4539 /* No '\r' is appended to the last paragraph. */
4540 nLen = 0;
4541 } else if (bCRLF && nChars == 1) {
4542 nLen = 0;
4543 nChars = 0;
4544 } else {
4545 WCHAR cr_lf[] = {'\r', '\n', 0};
4546 WCHAR *szData;
4548 if (bCRLF)
4550 nLen = 2;
4551 szData = cr_lf;
4552 } else {
4553 nLen = pRun->member.run.strText->nLen;
4554 szData = pRun->member.run.strText->szData;
4556 nLen = min(nChars, nLen - nOffset);
4557 CopyMemory(buffer, szData + nOffset, sizeof(WCHAR) * nLen);
4559 } else {
4560 nLen = min(nChars, pRun->member.run.strText->nLen - nOffset);
4561 CopyMemory(buffer, pRun->member.run.strText->szData + nOffset,
4562 sizeof(WCHAR) * nLen);
4564 nChars -= nLen;
4565 nWritten += nLen;
4566 buffer += nLen;
4567 nOffset = 0;
4569 pRun = ME_FindItemFwd(pRun, diRun);
4571 *buffer = 0;
4572 TRACE("nWritten=%d, actual=%d\n", nWritten, buffer-pStart);
4573 return nWritten;
4576 static BOOL ME_RegisterEditorClass(HINSTANCE hInstance)
4578 WNDCLASSW wcW;
4579 WNDCLASSA wcA;
4581 wcW.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
4582 wcW.lpfnWndProc = RichEditWndProcW;
4583 wcW.cbClsExtra = 0;
4584 wcW.cbWndExtra = sizeof(ME_TextEditor *);
4585 wcW.hInstance = NULL; /* hInstance would register DLL-local class */
4586 wcW.hIcon = NULL;
4587 wcW.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
4588 wcW.hbrBackground = GetStockObject(NULL_BRUSH);
4589 wcW.lpszMenuName = NULL;
4591 if (is_version_nt())
4593 wcW.lpszClassName = RichEdit20W;
4594 if (!RegisterClassW(&wcW)) return FALSE;
4595 wcW.lpszClassName = RichEdit50W;
4596 if (!RegisterClassW(&wcW)) return FALSE;
4598 else
4600 /* WNDCLASSA/W have the same layout */
4601 wcW.lpszClassName = (LPCWSTR)"RichEdit20W";
4602 if (!RegisterClassA((WNDCLASSA *)&wcW)) return FALSE;
4603 wcW.lpszClassName = (LPCWSTR)"RichEdit50W";
4604 if (!RegisterClassA((WNDCLASSA *)&wcW)) return FALSE;
4607 wcA.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
4608 wcA.lpfnWndProc = RichEditWndProcA;
4609 wcA.cbClsExtra = 0;
4610 wcA.cbWndExtra = sizeof(ME_TextEditor *);
4611 wcA.hInstance = NULL; /* hInstance would register DLL-local class */
4612 wcA.hIcon = NULL;
4613 wcA.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
4614 wcA.hbrBackground = GetStockObject(NULL_BRUSH);
4615 wcA.lpszMenuName = NULL;
4616 wcA.lpszClassName = "RichEdit20A";
4617 if (!RegisterClassA(&wcA)) return FALSE;
4618 wcA.lpszClassName = "RichEdit50A";
4619 if (!RegisterClassA(&wcA)) return FALSE;
4621 return TRUE;
4624 static LRESULT WINAPI REComboWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
4625 /* FIXME: Not implemented */
4626 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
4627 hWnd, msg, get_msg_name(msg), wParam, lParam);
4628 return DefWindowProcW(hWnd, msg, wParam, lParam);
4631 static LRESULT WINAPI REListWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
4632 /* FIXME: Not implemented */
4633 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
4634 hWnd, msg, get_msg_name(msg), wParam, lParam);
4635 return DefWindowProcW(hWnd, msg, wParam, lParam);
4638 /******************************************************************
4639 * REExtendedRegisterClass (RICHED20.8)
4641 * FIXME undocumented
4642 * Need to check for errors and implement controls and callbacks
4644 LRESULT WINAPI REExtendedRegisterClass(void)
4646 WNDCLASSW wcW;
4647 UINT result;
4649 FIXME("semi stub\n");
4651 wcW.cbClsExtra = 0;
4652 wcW.cbWndExtra = 4;
4653 wcW.hInstance = NULL;
4654 wcW.hIcon = NULL;
4655 wcW.hCursor = NULL;
4656 wcW.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
4657 wcW.lpszMenuName = NULL;
4659 if (!ME_ListBoxRegistered)
4661 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS;
4662 wcW.lpfnWndProc = REListWndProc;
4663 wcW.lpszClassName = REListBox20W;
4664 if (RegisterClassW(&wcW)) ME_ListBoxRegistered = TRUE;
4667 if (!ME_ComboBoxRegistered)
4669 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
4670 wcW.lpfnWndProc = REComboWndProc;
4671 wcW.lpszClassName = REComboBox20W;
4672 if (RegisterClassW(&wcW)) ME_ComboBoxRegistered = TRUE;
4675 result = 0;
4676 if (ME_ListBoxRegistered)
4677 result += 1;
4678 if (ME_ComboBoxRegistered)
4679 result += 2;
4681 return result;
4684 static BOOL isurlspecial(WCHAR c)
4686 static const WCHAR special_chars[] = {'.','/','%','@','*','|','\\','+','#',0};
4687 return strchrW( special_chars, c ) != NULL;
4691 * This proc takes a selection, and scans it forward in order to select the span
4692 * of a possible URL candidate. A possible URL candidate must start with isalnum
4693 * or one of the following special characters: *|/\+%#@ and must consist entirely
4694 * of the characters allowed to start the URL, plus : (colon) which may occur
4695 * at most once, and not at either end.
4697 * sel_max == -1 indicates scan to end of text.
4699 static BOOL ME_FindNextURLCandidate(ME_TextEditor *editor, int sel_min, int sel_max,
4700 int * candidate_min, int * candidate_max)
4702 ME_DisplayItem * item;
4703 ME_DisplayItem * para;
4704 int nStart;
4705 BOOL foundColon = FALSE;
4706 WCHAR lastAcceptedChar = '\0';
4708 TRACE("sel_min = %d sel_max = %d\n", sel_min, sel_max);
4710 *candidate_min = *candidate_max = -1;
4711 ME_RunOfsFromCharOfs(editor, sel_min, &para, &item, &nStart);
4712 TRACE("nStart = %d\n", nStart);
4713 if (sel_max == -1) sel_max = ME_GetTextLength(editor);
4714 while (item && para->member.para.nCharOfs + item->member.run.nCharOfs + nStart < sel_max)
4716 ME_DisplayItem * next_item;
4718 if (!(item->member.run.nFlags & MERF_ENDPARA)) {
4719 /* Find start of candidate */
4720 if (*candidate_min == -1) {
4721 while (nStart < item->member.run.strText->nLen &&
4722 !(isalnumW(item->member.run.strText->szData[nStart]) ||
4723 isurlspecial(item->member.run.strText->szData[nStart]))) {
4724 nStart++;
4726 if (nStart < item->member.run.strText->nLen &&
4727 (isalnumW(item->member.run.strText->szData[nStart]) ||
4728 isurlspecial(item->member.run.strText->szData[nStart]))) {
4729 *candidate_min = para->member.para.nCharOfs + item->member.run.nCharOfs + nStart;
4730 lastAcceptedChar = item->member.run.strText->szData[nStart];
4731 nStart++;
4735 /* Find end of candidate */
4736 if (*candidate_min >= 0) {
4737 while (nStart < item->member.run.strText->nLen &&
4738 (isalnumW(item->member.run.strText->szData[nStart]) ||
4739 isurlspecial(item->member.run.strText->szData[nStart]) ||
4740 (!foundColon && item->member.run.strText->szData[nStart] == ':') )) {
4741 if (item->member.run.strText->szData[nStart] == ':') foundColon = TRUE;
4742 lastAcceptedChar = item->member.run.strText->szData[nStart];
4743 nStart++;
4745 if (nStart < item->member.run.strText->nLen &&
4746 !(isalnumW(item->member.run.strText->szData[nStart]) ||
4747 isurlspecial(item->member.run.strText->szData[nStart]) )) {
4748 *candidate_max = para->member.para.nCharOfs + item->member.run.nCharOfs + nStart;
4749 nStart++;
4750 if (lastAcceptedChar == ':') (*candidate_max)--;
4751 return TRUE;
4754 } else {
4755 /* End of paragraph: skip it if before candidate span, or terminates
4756 current active span */
4757 if (*candidate_min >= 0) {
4758 *candidate_max = para->member.para.nCharOfs + item->member.run.nCharOfs;
4759 if (lastAcceptedChar == ':') (*candidate_max)--;
4760 return TRUE;
4764 /* Reaching this point means no span was found, so get next span */
4765 next_item = ME_FindItemFwd(item, diRun);
4766 if (!next_item) {
4767 if (*candidate_min >= 0) {
4768 /* There are no further runs, so take end of text as end of candidate */
4769 *candidate_max = para->member.para.nCharOfs + item->member.run.nCharOfs + nStart;
4770 if (lastAcceptedChar == ':') (*candidate_max)--;
4771 return TRUE;
4774 item = next_item;
4775 para = ME_GetParagraph(item);
4776 nStart = 0;
4779 if (item) {
4780 if (*candidate_min >= 0) {
4781 /* There are no further runs, so take end of text as end of candidate */
4782 *candidate_max = para->member.para.nCharOfs + item->member.run.nCharOfs + nStart;
4783 if (lastAcceptedChar == ':') (*candidate_max)--;
4784 return TRUE;
4787 return FALSE;
4791 * This proc evaluates the selection and returns TRUE if it can be considered an URL
4793 static BOOL ME_IsCandidateAnURL(ME_TextEditor *editor, int sel_min, int sel_max)
4795 struct prefix_s {
4796 const char *text;
4797 int length;
4798 } prefixes[12] = {
4799 /* Code below depends on these being in decreasing length order! */
4800 {"prospero:", 10},
4801 {"telnet:", 8},
4802 {"gopher:", 8},
4803 {"mailto:", 8},
4804 {"https:", 7},
4805 {"file:", 6},
4806 {"news:", 6},
4807 {"wais:", 6},
4808 {"nntp:", 6},
4809 {"http:", 5},
4810 {"www.", 5},
4811 {"ftp:", 5},
4813 LPWSTR bufferW = NULL;
4814 WCHAR bufW[32];
4815 unsigned int i;
4817 if (sel_max == -1) sel_max = ME_GetTextLength(editor);
4818 assert(sel_min <= sel_max);
4819 for (i = 0; i < sizeof(prefixes) / sizeof(struct prefix_s); i++)
4821 if (sel_max - sel_min < prefixes[i].length) continue;
4822 if (bufferW == NULL) {
4823 bufferW = heap_alloc((sel_max - sel_min + 1) * sizeof(WCHAR));
4825 ME_GetTextW(editor, bufferW, sel_min, min(sel_max - sel_min, lstrlenA(prefixes[i].text)), 0);
4826 MultiByteToWideChar(CP_ACP, 0, prefixes[i].text, -1, bufW, 32);
4827 if (!lstrcmpW(bufW, bufferW))
4829 heap_free(bufferW);
4830 return TRUE;
4833 heap_free(bufferW);
4834 return FALSE;
4838 * This proc walks through the indicated selection and evaluates whether each
4839 * section identified by ME_FindNextURLCandidate and in-between sections have
4840 * their proper CFE_LINK attributes set or unset. If the CFE_LINK attribute is
4841 * not what it is supposed to be, this proc sets or unsets it as appropriate.
4843 * Returns TRUE if at least one section was modified.
4845 static BOOL ME_UpdateLinkAttribute(ME_TextEditor *editor, int sel_min, int sel_max)
4847 BOOL modified = FALSE;
4848 int cMin, cMax;
4850 if (!editor->AutoURLDetect_bEnable) return FALSE;
4852 if (sel_max == -1) sel_max = ME_GetTextLength(editor);
4855 int beforeURL[2];
4856 int inURL[2];
4857 CHARFORMAT2W link;
4859 if (ME_FindNextURLCandidate(editor, sel_min, sel_max, &cMin, &cMax))
4861 /* Section before candidate is not an URL */
4862 beforeURL[0] = sel_min;
4863 beforeURL[1] = cMin;
4865 if (ME_IsCandidateAnURL(editor, cMin, cMax))
4867 inURL[0] = cMin; inURL[1] = cMax;
4869 else
4871 beforeURL[1] = cMax;
4872 inURL[0] = inURL[1] = -1;
4874 sel_min = cMax;
4876 else
4878 /* No more candidates until end of selection */
4879 beforeURL[0] = sel_min;
4880 beforeURL[1] = sel_max;
4881 inURL[0] = inURL[1] = -1;
4882 sel_min = sel_max;
4885 if (beforeURL[0] < beforeURL[1])
4887 /* CFE_LINK effect should be consistently unset */
4888 link.cbSize = sizeof(link);
4889 ME_GetCharFormat(editor, beforeURL[0], beforeURL[1], &link);
4890 if (!(link.dwMask & CFM_LINK) || (link.dwEffects & CFE_LINK))
4892 /* CFE_LINK must be unset from this range */
4893 memset(&link, 0, sizeof(CHARFORMAT2W));
4894 link.cbSize = sizeof(link);
4895 link.dwMask = CFM_LINK;
4896 link.dwEffects = 0;
4897 ME_SetCharFormat(editor, beforeURL[0], beforeURL[1] - beforeURL[0], &link);
4898 modified = TRUE;
4901 if (inURL[0] < inURL[1])
4903 /* CFE_LINK effect should be consistently set */
4904 link.cbSize = sizeof(link);
4905 ME_GetCharFormat(editor, inURL[0], inURL[1], &link);
4906 if (!(link.dwMask & CFM_LINK) || !(link.dwEffects & CFE_LINK))
4908 /* CFE_LINK must be set on this range */
4909 memset(&link, 0, sizeof(CHARFORMAT2W));
4910 link.cbSize = sizeof(link);
4911 link.dwMask = CFM_LINK;
4912 link.dwEffects = CFE_LINK;
4913 ME_SetCharFormat(editor, inURL[0], inURL[1] - inURL[0], &link);
4914 modified = TRUE;
4917 } while (sel_min < sel_max);
4918 return modified;