richedit: Make the ME_GetCursorOfs function more flexible.
[wine/wine-gecko.git] / dlls / riched20 / editor.c
blob2a37584e50e1d4effd3d26ab8d0bfd3f691a180e
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 REListBox20W[] = {'R','E','L','i','s','t','B','o','x','2','0','W', 0};
247 static const WCHAR REComboBox20W[] = {'R','E','C','o','m','b','o','B','o','x','2','0','W', 0};
248 static HCURSOR hLeft;
250 int me_debug = 0;
251 HANDLE me_heap = NULL;
253 static BOOL ME_ListBoxRegistered = FALSE;
254 static BOOL ME_ComboBoxRegistered = FALSE;
256 static inline int is_version_nt(void)
258 return !(GetVersion() & 0x80000000);
261 static ME_TextBuffer *ME_MakeText(void) {
263 ME_TextBuffer *buf = ALLOC_OBJ(ME_TextBuffer);
265 ME_DisplayItem *p1 = ME_MakeDI(diTextStart);
266 ME_DisplayItem *p2 = ME_MakeDI(diTextEnd);
268 p1->prev = NULL;
269 p1->next = p2;
270 p2->prev = p1;
271 p2->next = NULL;
272 p1->member.para.next_para = p2;
273 p2->member.para.prev_para = p1;
274 p2->member.para.nCharOfs = 0;
276 buf->pFirst = p1;
277 buf->pLast = p2;
278 buf->pCharStyle = NULL;
280 return buf;
284 static LRESULT ME_StreamInText(ME_TextEditor *editor, DWORD dwFormat, ME_InStream *stream, ME_Style *style)
286 WCHAR wszText[STREAMIN_BUFFER_SIZE+1];
287 WCHAR *pText;
289 TRACE("%08x %p\n", dwFormat, stream);
291 do {
292 long nWideChars = 0;
294 if (!stream->dwSize)
296 ME_StreamInFill(stream);
297 if (stream->editstream->dwError)
298 break;
299 if (!stream->dwSize)
300 break;
303 if (!(dwFormat & SF_UNICODE))
305 /* FIXME? this is doomed to fail on true MBCS like UTF-8, luckily they're unlikely to be used as CP_ACP */
306 nWideChars = MultiByteToWideChar(CP_ACP, 0, stream->buffer, stream->dwSize, wszText, STREAMIN_BUFFER_SIZE);
307 pText = wszText;
309 else
311 nWideChars = stream->dwSize >> 1;
312 pText = (WCHAR *)stream->buffer;
315 ME_InsertTextFromCursor(editor, 0, pText, nWideChars, style);
316 if (stream->dwSize == 0)
317 break;
318 stream->dwSize = 0;
319 } while(1);
320 ME_CommitUndo(editor);
321 ME_UpdateRepaint(editor);
322 return 0;
325 static void ME_ApplyBorderProperties(RTF_Info *info,
326 ME_BorderRect *borderRect,
327 RTFBorder *borderDef)
329 int i, colorNum;
330 ME_Border *pBorders[] = {&borderRect->top,
331 &borderRect->left,
332 &borderRect->bottom,
333 &borderRect->right};
334 for (i = 0; i < 4; i++)
336 RTFColor *colorDef = info->colorList;
337 pBorders[i]->width = borderDef[i].width;
338 colorNum = borderDef[i].color;
339 while (colorDef && colorDef->rtfCNum != colorNum)
340 colorDef = colorDef->rtfNextColor;
341 if (colorDef)
342 pBorders[i]->colorRef = RGB(
343 colorDef->rtfCRed >= 0 ? colorDef->rtfCRed : 0,
344 colorDef->rtfCGreen >= 0 ? colorDef->rtfCGreen : 0,
345 colorDef->rtfCBlue >= 0 ? colorDef->rtfCBlue : 0);
346 else
347 pBorders[i]->colorRef = RGB(0, 0, 0);
351 void ME_RTFCharAttrHook(RTF_Info *info)
353 CHARFORMAT2W fmt;
354 fmt.cbSize = sizeof(fmt);
355 fmt.dwMask = 0;
356 fmt.dwEffects = 0;
358 switch(info->rtfMinor)
360 case rtfPlain:
361 /* FIXME add more flags once they're implemented */
362 fmt.dwMask = CFM_BOLD | CFM_ITALIC | CFM_UNDERLINETYPE | CFM_STRIKEOUT | CFM_COLOR | CFM_BACKCOLOR | CFM_SIZE | CFM_WEIGHT;
363 fmt.dwEffects = CFE_AUTOCOLOR | CFE_AUTOBACKCOLOR;
364 fmt.yHeight = 12*20; /* 12pt */
365 fmt.wWeight = FW_NORMAL;
366 fmt.bUnderlineType = CFU_UNDERLINENONE;
367 break;
368 case rtfBold:
369 fmt.dwMask = CFM_BOLD | CFM_WEIGHT;
370 fmt.dwEffects = info->rtfParam ? CFE_BOLD : 0;
371 fmt.wWeight = info->rtfParam ? FW_BOLD : FW_NORMAL;
372 break;
373 case rtfItalic:
374 fmt.dwMask = CFM_ITALIC;
375 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
376 break;
377 case rtfUnderline:
378 fmt.dwMask = CFM_UNDERLINETYPE;
379 fmt.bUnderlineType = info->rtfParam ? CFU_CF1UNDERLINE : CFU_UNDERLINENONE;
380 break;
381 case rtfDotUnderline:
382 fmt.dwMask = CFM_UNDERLINETYPE;
383 fmt.bUnderlineType = info->rtfParam ? CFU_UNDERLINEDOTTED : CFU_UNDERLINENONE;
384 break;
385 case rtfDbUnderline:
386 fmt.dwMask = CFM_UNDERLINETYPE;
387 fmt.bUnderlineType = info->rtfParam ? CFU_UNDERLINEDOUBLE : CFU_UNDERLINENONE;
388 break;
389 case rtfWordUnderline:
390 fmt.dwMask = CFM_UNDERLINETYPE;
391 fmt.bUnderlineType = info->rtfParam ? CFU_UNDERLINEWORD : CFU_UNDERLINENONE;
392 break;
393 case rtfNoUnderline:
394 fmt.dwMask = CFM_UNDERLINETYPE;
395 fmt.bUnderlineType = CFU_UNDERLINENONE;
396 break;
397 case rtfStrikeThru:
398 fmt.dwMask = CFM_STRIKEOUT;
399 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
400 break;
401 case rtfSubScript:
402 case rtfSuperScript:
403 case rtfSubScrShrink:
404 case rtfSuperScrShrink:
405 case rtfNoSuperSub:
406 fmt.dwMask = CFM_SUBSCRIPT|CFM_SUPERSCRIPT;
407 if (info->rtfMinor == rtfSubScrShrink) fmt.dwEffects = CFE_SUBSCRIPT;
408 if (info->rtfMinor == rtfSuperScrShrink) fmt.dwEffects = CFE_SUPERSCRIPT;
409 if (info->rtfMinor == rtfNoSuperSub) fmt.dwEffects = 0;
410 break;
411 case rtfInvisible:
412 fmt.dwMask = CFM_HIDDEN;
413 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
414 break;
415 case rtfBackColor:
416 fmt.dwMask = CFM_BACKCOLOR;
417 fmt.dwEffects = 0;
418 if (info->rtfParam == 0)
419 fmt.dwEffects = CFE_AUTOBACKCOLOR;
420 else if (info->rtfParam != rtfNoParam)
422 RTFColor *c = RTFGetColor(info, info->rtfParam);
423 if (c && c->rtfCBlue >= 0)
424 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
425 else
426 fmt.dwEffects = CFE_AUTOBACKCOLOR;
428 break;
429 case rtfForeColor:
430 fmt.dwMask = CFM_COLOR;
431 fmt.dwEffects = 0;
432 if (info->rtfParam == 0)
433 fmt.dwEffects = CFE_AUTOCOLOR;
434 else if (info->rtfParam != rtfNoParam)
436 RTFColor *c = RTFGetColor(info, info->rtfParam);
437 if (c && c->rtfCBlue >= 0)
438 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
439 else {
440 fmt.dwEffects = CFE_AUTOCOLOR;
443 break;
444 case rtfFontNum:
445 if (info->rtfParam != rtfNoParam)
447 RTFFont *f = RTFGetFont(info, info->rtfParam);
448 if (f)
450 MultiByteToWideChar(CP_ACP, 0, f->rtfFName, -1, fmt.szFaceName, sizeof(fmt.szFaceName)/sizeof(WCHAR));
451 fmt.szFaceName[sizeof(fmt.szFaceName)/sizeof(WCHAR)-1] = '\0';
452 fmt.bCharSet = f->rtfFCharSet;
453 fmt.dwMask = CFM_FACE | CFM_CHARSET;
454 fmt.bPitchAndFamily = f->rtfFPitch | (f->rtfFFamily << 4);
457 break;
458 case rtfFontSize:
459 fmt.dwMask = CFM_SIZE;
460 if (info->rtfParam != rtfNoParam)
461 fmt.yHeight = info->rtfParam*10;
462 break;
464 if (fmt.dwMask) {
465 ME_Style *style2;
466 RTFFlushOutputBuffer(info);
467 /* FIXME too slow ? how come ? */
468 style2 = ME_ApplyStyle(info->style, &fmt);
469 ME_ReleaseStyle(info->style);
470 info->style = style2;
471 info->styleChanged = TRUE;
475 /* FIXME this function doesn't get any information about context of the RTF tag, which is very bad,
476 the same tags mean different things in different contexts */
477 void ME_RTFParAttrHook(RTF_Info *info)
479 PARAFORMAT2 fmt;
480 fmt.cbSize = sizeof(fmt);
481 fmt.dwMask = 0;
483 switch(info->rtfMinor)
485 case rtfParDef: /* restores default paragraph attributes */
486 if (!info->editor->bEmulateVersion10) /* v4.1 */
487 info->borderType = RTFBorderParaLeft;
488 else /* v1.0 - 3.0 */
489 info->borderType = RTFBorderParaTop;
490 fmt.dwMask = PFM_ALIGNMENT | PFM_BORDER | PFM_LINESPACING | PFM_TABSTOPS |
491 PFM_OFFSET | PFM_RIGHTINDENT | PFM_SPACEAFTER | PFM_SPACEBEFORE |
492 PFM_STARTINDENT;
493 /* TODO: numbering, shading */
494 fmt.wAlignment = PFA_LEFT;
495 fmt.cTabCount = 0;
496 fmt.dxOffset = fmt.dxStartIndent = fmt.dxRightIndent = 0;
497 fmt.wBorderWidth = fmt.wBorders = 0;
498 fmt.wBorderSpace = 0;
499 fmt.bLineSpacingRule = 0;
500 fmt.dySpaceBefore = fmt.dySpaceAfter = 0;
501 fmt.dyLineSpacing = 0;
502 if (!info->editor->bEmulateVersion10) /* v4.1 */
504 if (info->tableDef && info->tableDef->tableRowStart &&
505 info->tableDef->tableRowStart->member.para.nFlags & MEPF_ROWEND)
507 ME_Cursor cursor;
508 ME_DisplayItem *para;
509 /* We are just after a table row. */
510 RTFFlushOutputBuffer(info);
511 cursor = info->editor->pCursors[0];
512 para = cursor.pPara;
513 if (para == info->tableDef->tableRowStart->member.para.next_para
514 && !cursor.nOffset && !cursor.pRun->member.run.nCharOfs)
516 /* Since the table row end, no text has been inserted, and the \intbl
517 * control word has not be used. We can confirm that we are not in a
518 * table anymore.
520 info->tableDef->tableRowStart = NULL;
521 info->canInheritInTbl = FALSE;
524 } else { /* v1.0 - v3.0 */
525 fmt.dwMask |= PFM_TABLE;
526 fmt.wEffects &= ~PFE_TABLE;
528 break;
529 case rtfNestLevel:
530 if (!info->editor->bEmulateVersion10) /* v4.1 */
532 while (info->rtfParam > info->nestingLevel) {
533 RTFTable *tableDef = ALLOC_OBJ(RTFTable);
534 ZeroMemory(tableDef, sizeof(RTFTable));
535 tableDef->parent = info->tableDef;
536 info->tableDef = tableDef;
538 RTFFlushOutputBuffer(info);
539 if (tableDef->tableRowStart &&
540 tableDef->tableRowStart->member.para.nFlags & MEPF_ROWEND)
542 ME_DisplayItem *para = tableDef->tableRowStart;
543 para = para->member.para.next_para;
544 para = ME_InsertTableRowStartAtParagraph(info->editor, para);
545 tableDef->tableRowStart = para;
546 } else {
547 ME_Cursor cursor;
548 WCHAR endl = '\r';
549 cursor = info->editor->pCursors[0];
550 if (cursor.nOffset || cursor.pRun->member.run.nCharOfs)
551 ME_InsertTextFromCursor(info->editor, 0, &endl, 1, info->style);
552 tableDef->tableRowStart = ME_InsertTableRowStartFromCursor(info->editor);
555 info->nestingLevel++;
557 info->canInheritInTbl = FALSE;
559 break;
560 case rtfInTable:
562 if (!info->editor->bEmulateVersion10) /* v4.1 */
564 if (info->nestingLevel < 1)
566 RTFTable *tableDef;
567 if (!info->tableDef)
569 info->tableDef = ALLOC_OBJ(RTFTable);
570 ZeroMemory(info->tableDef, sizeof(RTFTable));
572 tableDef = info->tableDef;
573 RTFFlushOutputBuffer(info);
574 if (tableDef->tableRowStart &&
575 tableDef->tableRowStart->member.para.nFlags & MEPF_ROWEND)
577 ME_DisplayItem *para = tableDef->tableRowStart;
578 para = para->member.para.next_para;
579 para = ME_InsertTableRowStartAtParagraph(info->editor, para);
580 tableDef->tableRowStart = para;
581 } else {
582 ME_Cursor cursor;
583 WCHAR endl = '\r';
584 cursor = info->editor->pCursors[0];
585 if (cursor.nOffset || cursor.pRun->member.run.nCharOfs)
586 ME_InsertTextFromCursor(info->editor, 0, &endl, 1, info->style);
587 tableDef->tableRowStart = ME_InsertTableRowStartFromCursor(info->editor);
589 info->nestingLevel = 1;
590 info->canInheritInTbl = TRUE;
592 return;
593 } else { /* v1.0 - v3.0 */
594 fmt.dwMask |= PFM_TABLE;
595 fmt.wEffects |= PFE_TABLE;
597 break;
599 case rtfFirstIndent:
600 ME_GetSelectionParaFormat(info->editor, &fmt);
601 fmt.dwMask = PFM_STARTINDENT | PFM_OFFSET;
602 fmt.dxStartIndent += fmt.dxOffset + info->rtfParam;
603 fmt.dxOffset = -info->rtfParam;
604 break;
605 case rtfLeftIndent:
606 ME_GetSelectionParaFormat(info->editor, &fmt);
607 fmt.dwMask = PFM_STARTINDENT;
608 fmt.dxStartIndent = info->rtfParam - fmt.dxOffset;
609 break;
610 case rtfRightIndent:
611 fmt.dwMask = PFM_RIGHTINDENT;
612 fmt.dxRightIndent = info->rtfParam;
613 break;
614 case rtfQuadLeft:
615 case rtfQuadJust:
616 fmt.dwMask = PFM_ALIGNMENT;
617 fmt.wAlignment = PFA_LEFT;
618 break;
619 case rtfQuadRight:
620 fmt.dwMask = PFM_ALIGNMENT;
621 fmt.wAlignment = PFA_RIGHT;
622 break;
623 case rtfQuadCenter:
624 fmt.dwMask = PFM_ALIGNMENT;
625 fmt.wAlignment = PFA_CENTER;
626 break;
627 case rtfTabPos:
628 ME_GetSelectionParaFormat(info->editor, &fmt);
629 if (!(fmt.dwMask & PFM_TABSTOPS))
631 fmt.cTabCount = 0;
633 if (fmt.cTabCount < MAX_TAB_STOPS && info->rtfParam < 0x1000000)
634 fmt.rgxTabs[fmt.cTabCount++] = info->rtfParam;
635 fmt.dwMask = PFM_TABSTOPS;
636 break;
637 case rtfKeep:
638 fmt.dwMask = PFM_KEEP;
639 fmt.wEffects = PFE_KEEP;
640 break;
641 case rtfNoWidowControl:
642 fmt.dwMask = PFM_NOWIDOWCONTROL;
643 fmt.wEffects = PFE_NOWIDOWCONTROL;
644 break;
645 case rtfKeepNext:
646 fmt.dwMask = PFM_KEEPNEXT;
647 fmt.wEffects = PFE_KEEPNEXT;
648 break;
649 case rtfSpaceAfter:
650 fmt.dwMask = PFM_SPACEAFTER;
651 fmt.dySpaceAfter = info->rtfParam;
652 break;
653 case rtfSpaceBefore:
654 fmt.dwMask = PFM_SPACEBEFORE;
655 fmt.dySpaceBefore = info->rtfParam;
656 break;
657 case rtfSpaceBetween:
658 fmt.dwMask = PFM_LINESPACING;
659 if ((int)info->rtfParam > 0)
661 fmt.dyLineSpacing = info->rtfParam;
662 fmt.bLineSpacingRule = 3;
664 else
666 fmt.dyLineSpacing = info->rtfParam;
667 fmt.bLineSpacingRule = 4;
669 case rtfSpaceMultiply:
670 fmt.dwMask = PFM_LINESPACING;
671 fmt.dyLineSpacing = info->rtfParam * 20;
672 fmt.bLineSpacingRule = 5;
673 break;
674 case rtfParBullet:
675 fmt.dwMask = PFM_NUMBERING;
676 fmt.wNumbering = PFN_BULLET;
677 break;
678 case rtfParSimple:
679 fmt.dwMask = PFM_NUMBERING;
680 fmt.wNumbering = 2; /* FIXME: MSDN says it's not used ?? */
681 break;
682 case rtfParNumDecimal:
683 fmt.dwMask = PFM_NUMBERING;
684 fmt.wNumbering = 2; /* FIXME: MSDN says it's not used ?? */
685 break;
686 case rtfParNumIndent:
687 fmt.dwMask = PFM_NUMBERINGTAB;
688 fmt.wNumberingTab = info->rtfParam;
689 break;
690 case rtfParNumStartAt:
691 fmt.dwMask = PFM_NUMBERINGSTART;
692 fmt.wNumberingStart = info->rtfParam;
693 break;
694 case rtfBorderLeft:
695 info->borderType = RTFBorderParaLeft;
696 ME_GetSelectionParaFormat(info->editor, &fmt);
697 if (!(fmt.dwMask & PFM_BORDER))
699 fmt.wBorderSpace = 0;
700 fmt.wBorderWidth = 1;
701 fmt.wBorders = 0;
703 fmt.wBorders |= 1;
704 fmt.dwMask = PFM_BORDER;
705 break;
706 case rtfBorderRight:
707 info->borderType = RTFBorderParaRight;
708 ME_GetSelectionParaFormat(info->editor, &fmt);
709 if (!(fmt.dwMask & PFM_BORDER))
711 fmt.wBorderSpace = 0;
712 fmt.wBorderWidth = 1;
713 fmt.wBorders = 0;
715 fmt.wBorders |= 2;
716 fmt.dwMask = PFM_BORDER;
717 break;
718 case rtfBorderTop:
719 info->borderType = RTFBorderParaTop;
720 ME_GetSelectionParaFormat(info->editor, &fmt);
721 if (!(fmt.dwMask & PFM_BORDER))
723 fmt.wBorderSpace = 0;
724 fmt.wBorderWidth = 1;
725 fmt.wBorders = 0;
727 fmt.wBorders |= 4;
728 fmt.dwMask = PFM_BORDER;
729 break;
730 case rtfBorderBottom:
731 info->borderType = RTFBorderParaBottom;
732 ME_GetSelectionParaFormat(info->editor, &fmt);
733 if (!(fmt.dwMask & PFM_BORDER))
735 fmt.wBorderSpace = 0;
736 fmt.wBorderWidth = 1;
737 fmt.wBorders = 0;
739 fmt.wBorders |= 8;
740 fmt.dwMask = PFM_BORDER;
741 break;
742 case rtfBorderSingle:
743 ME_GetSelectionParaFormat(info->editor, &fmt);
744 /* we assume that borders have been created before (RTF spec) */
745 fmt.wBorders &= ~0x700;
746 fmt.wBorders |= 1 << 8;
747 fmt.dwMask = PFM_BORDER;
748 break;
749 case rtfBorderThick:
750 ME_GetSelectionParaFormat(info->editor, &fmt);
751 /* we assume that borders have been created before (RTF spec) */
752 fmt.wBorders &= ~0x700;
753 fmt.wBorders |= 2 << 8;
754 fmt.dwMask = PFM_BORDER;
755 break;
756 case rtfBorderShadow:
757 ME_GetSelectionParaFormat(info->editor, &fmt);
758 /* we assume that borders have been created before (RTF spec) */
759 fmt.wBorders &= ~0x700;
760 fmt.wBorders |= 10 << 8;
761 fmt.dwMask = PFM_BORDER;
762 break;
763 case rtfBorderDouble:
764 ME_GetSelectionParaFormat(info->editor, &fmt);
765 /* we assume that borders have been created before (RTF spec) */
766 fmt.wBorders &= ~0x700;
767 fmt.wBorders |= 7 << 8;
768 fmt.dwMask = PFM_BORDER;
769 break;
770 case rtfBorderDot:
771 ME_GetSelectionParaFormat(info->editor, &fmt);
772 /* we assume that borders have been created before (RTF spec) */
773 fmt.wBorders &= ~0x700;
774 fmt.wBorders |= 11 << 8;
775 fmt.dwMask = PFM_BORDER;
776 break;
777 case rtfBorderWidth:
779 int borderSide = info->borderType & RTFBorderSideMask;
780 RTFTable *tableDef = info->tableDef;
781 ME_GetSelectionParaFormat(info->editor, &fmt);
782 /* we assume that borders have been created before (RTF spec) */
783 fmt.wBorderWidth |= ((info->rtfParam / 15) & 7) << 8;
784 if ((info->borderType & RTFBorderTypeMask) == RTFBorderTypeCell)
786 RTFBorder *border;
787 if (!tableDef || tableDef->numCellsDefined >= MAX_TABLE_CELLS)
788 break;
789 border = &tableDef->cells[tableDef->numCellsDefined].border[borderSide];
790 border->width = info->rtfParam;
791 break;
793 fmt.dwMask = PFM_BORDER;
794 break;
796 case rtfBorderSpace:
797 ME_GetSelectionParaFormat(info->editor, &fmt);
798 /* we assume that borders have been created before (RTF spec) */
799 fmt.wBorderSpace = info->rtfParam;
800 fmt.dwMask = PFM_BORDER;
801 break;
802 case rtfBorderColor:
804 RTFTable *tableDef = info->tableDef;
805 int borderSide = info->borderType & RTFBorderSideMask;
806 int borderType = info->borderType & RTFBorderTypeMask;
807 switch(borderType)
809 case RTFBorderTypePara:
810 if (!info->editor->bEmulateVersion10) /* v4.1 */
811 break;
812 /* v1.0 - 3.0 treat paragraph and row borders the same. */
813 case RTFBorderTypeRow:
814 if (tableDef) {
815 tableDef->border[borderSide].color = info->rtfParam;
817 break;
818 case RTFBorderTypeCell:
819 if (tableDef && tableDef->numCellsDefined < MAX_TABLE_CELLS) {
820 tableDef->cells[tableDef->numCellsDefined].border[borderSide].color = info->rtfParam;
822 break;
824 break;
827 if (fmt.dwMask) {
828 RTFFlushOutputBuffer(info);
829 /* FIXME too slow ? how come ?*/
830 ME_SetSelectionParaFormat(info->editor, &fmt);
834 void ME_RTFTblAttrHook(RTF_Info *info)
836 switch (info->rtfMinor)
838 case rtfRowDef:
840 if (!info->editor->bEmulateVersion10) /* v4.1 */
841 info->borderType = 0; /* Not sure */
842 else /* v1.0 - 3.0 */
843 info->borderType = RTFBorderRowTop;
844 if (!info->tableDef) {
845 info->tableDef = ME_MakeTableDef(info->editor);
846 } else {
847 ME_InitTableDef(info->editor, info->tableDef);
849 break;
851 case rtfCellPos:
853 int cellNum;
854 if (!info->tableDef)
856 info->tableDef = ME_MakeTableDef(info->editor);
858 cellNum = info->tableDef->numCellsDefined;
859 if (cellNum >= MAX_TABLE_CELLS)
860 break;
861 info->tableDef->cells[cellNum].rightBoundary = info->rtfParam;
862 if (cellNum < MAX_TAB_STOPS) {
863 /* Tab stops were used to store cell positions before v4.1 but v4.1
864 * still seems to set the tabstops without using them. */
865 ME_DisplayItem *para = info->editor->pCursors[0].pPara;
866 PARAFORMAT2 *pFmt = para->member.para.pFmt;
867 pFmt->rgxTabs[cellNum] &= ~0x00FFFFFF;
868 pFmt->rgxTabs[cellNum] = 0x00FFFFFF & info->rtfParam;
870 info->tableDef->numCellsDefined++;
871 break;
873 case rtfRowBordTop:
874 info->borderType = RTFBorderRowTop;
875 break;
876 case rtfRowBordLeft:
877 info->borderType = RTFBorderRowLeft;
878 break;
879 case rtfRowBordBottom:
880 info->borderType = RTFBorderRowBottom;
881 break;
882 case rtfRowBordRight:
883 info->borderType = RTFBorderRowRight;
884 break;
885 case rtfCellBordTop:
886 info->borderType = RTFBorderCellTop;
887 break;
888 case rtfCellBordLeft:
889 info->borderType = RTFBorderCellLeft;
890 break;
891 case rtfCellBordBottom:
892 info->borderType = RTFBorderCellBottom;
893 break;
894 case rtfCellBordRight:
895 info->borderType = RTFBorderCellRight;
896 break;
897 case rtfRowGapH:
898 if (info->tableDef)
899 info->tableDef->gapH = info->rtfParam;
900 break;
901 case rtfRowLeftEdge:
902 if (info->tableDef)
903 info->tableDef->leftEdge = info->rtfParam;
904 break;
908 void ME_RTFSpecialCharHook(RTF_Info *info)
910 RTFTable *tableDef = info->tableDef;
911 switch (info->rtfMinor)
913 case rtfNestCell:
914 if (info->editor->bEmulateVersion10) /* v1.0 - v3.0 */
915 break;
916 /* else fall through since v4.1 treats rtfNestCell and rtfCell the same */
917 case rtfCell:
918 if (!tableDef)
919 break;
920 RTFFlushOutputBuffer(info);
921 if (!info->editor->bEmulateVersion10) { /* v4.1 */
922 if (tableDef->tableRowStart)
924 if (!info->nestingLevel &&
925 tableDef->tableRowStart->member.para.nFlags & MEPF_ROWEND)
927 ME_DisplayItem *para = tableDef->tableRowStart;
928 para = para->member.para.next_para;
929 para = ME_InsertTableRowStartAtParagraph(info->editor, para);
930 tableDef->tableRowStart = para;
931 info->nestingLevel = 1;
933 ME_InsertTableCellFromCursor(info->editor);
935 } else { /* v1.0 - v3.0 */
936 ME_DisplayItem *para = info->editor->pCursors[0].pPara;
937 PARAFORMAT2 *pFmt = para->member.para.pFmt;
938 if (pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE &&
939 tableDef->numCellsInserted < tableDef->numCellsDefined)
941 WCHAR tab = '\t';
942 ME_InsertTextFromCursor(info->editor, 0, &tab, 1, info->style);
943 tableDef->numCellsInserted++;
946 break;
947 case rtfNestRow:
948 if (info->editor->bEmulateVersion10) /* v1.0 - v3.0 */
949 break;
950 /* else fall through since v4.1 treats rtfNestRow and rtfRow the same */
951 case rtfRow:
953 ME_DisplayItem *para, *cell, *run;
954 int i;
956 if (!tableDef)
957 break;
958 RTFFlushOutputBuffer(info);
959 if (!info->editor->bEmulateVersion10) { /* v4.1 */
960 if (!tableDef->tableRowStart)
961 break;
962 if (!info->nestingLevel &&
963 tableDef->tableRowStart->member.para.nFlags & MEPF_ROWEND)
965 para = tableDef->tableRowStart;
966 para = para->member.para.next_para;
967 para = ME_InsertTableRowStartAtParagraph(info->editor, para);
968 tableDef->tableRowStart = para;
969 info->nestingLevel++;
971 para = tableDef->tableRowStart;
972 cell = ME_FindItemFwd(para, diCell);
973 assert(cell && !cell->member.cell.prev_cell);
974 if (tableDef->numCellsDefined < 1)
976 /* 2000 twips appears to be the cell size that native richedit uses
977 * when no cell sizes are specified. */
978 const int defaultCellSize = 2000;
979 int nRightBoundary = defaultCellSize;
980 cell->member.cell.nRightBoundary = nRightBoundary;
981 while (cell->member.cell.next_cell) {
982 cell = cell->member.cell.next_cell;
983 nRightBoundary += defaultCellSize;
984 cell->member.cell.nRightBoundary = nRightBoundary;
986 para = ME_InsertTableCellFromCursor(info->editor);
987 cell = para->member.para.pCell;
988 cell->member.cell.nRightBoundary = nRightBoundary;
989 } else {
990 for (i = 0; i < tableDef->numCellsDefined; i++)
992 RTFCell *cellDef = &tableDef->cells[i];
993 cell->member.cell.nRightBoundary = cellDef->rightBoundary;
994 ME_ApplyBorderProperties(info, &cell->member.cell.border,
995 cellDef->border);
996 cell = cell->member.cell.next_cell;
997 if (!cell)
999 para = ME_InsertTableCellFromCursor(info->editor);
1000 cell = para->member.para.pCell;
1003 /* Cell for table row delimiter is empty */
1004 cell->member.cell.nRightBoundary = tableDef->cells[i-1].rightBoundary;
1007 run = ME_FindItemFwd(cell, diRun);
1008 if (info->editor->pCursors[0].pRun != run ||
1009 info->editor->pCursors[0].nOffset)
1011 int nOfs, nChars;
1012 /* Delete inserted cells that aren't defined. */
1013 info->editor->pCursors[1].pRun = run;
1014 info->editor->pCursors[1].pPara = ME_GetParagraph(run);
1015 info->editor->pCursors[1].nOffset = 0;
1016 nOfs = ME_GetCursorOfs(&info->editor->pCursors[1]);
1017 nChars = ME_GetCursorOfs(&info->editor->pCursors[0]) - nOfs;
1018 ME_InternalDeleteText(info->editor, nOfs, nChars, TRUE);
1021 para = ME_InsertTableRowEndFromCursor(info->editor);
1022 para->member.para.pFmt->dxOffset = abs(info->tableDef->gapH);
1023 para->member.para.pFmt->dxStartIndent = info->tableDef->leftEdge;
1024 ME_ApplyBorderProperties(info, &para->member.para.border,
1025 tableDef->border);
1026 info->nestingLevel--;
1027 if (!info->nestingLevel)
1029 if (info->canInheritInTbl) {
1030 tableDef->tableRowStart = para;
1031 } else {
1032 while (info->tableDef) {
1033 tableDef = info->tableDef;
1034 info->tableDef = tableDef->parent;
1035 heap_free(tableDef);
1038 } else {
1039 info->tableDef = tableDef->parent;
1040 heap_free(tableDef);
1042 } else { /* v1.0 - v3.0 */
1043 WCHAR endl = '\r';
1044 ME_DisplayItem *para = info->editor->pCursors[0].pPara;
1045 PARAFORMAT2 *pFmt = para->member.para.pFmt;
1046 pFmt->dxOffset = info->tableDef->gapH;
1047 pFmt->dxStartIndent = info->tableDef->leftEdge;
1049 ME_ApplyBorderProperties(info, &para->member.para.border,
1050 tableDef->border);
1051 while (tableDef->numCellsInserted < tableDef->numCellsDefined)
1053 WCHAR tab = '\t';
1054 ME_InsertTextFromCursor(info->editor, 0, &tab, 1, info->style);
1055 tableDef->numCellsInserted++;
1057 pFmt->cTabCount = min(tableDef->numCellsDefined, MAX_TAB_STOPS);
1058 if (!tableDef->numCellsDefined)
1059 pFmt->wEffects &= ~PFE_TABLE;
1060 ME_InsertTextFromCursor(info->editor, 0, &endl, 1, info->style);
1061 tableDef->numCellsInserted = 0;
1063 break;
1065 case rtfTab:
1066 case rtfPar:
1067 if (info->editor->bEmulateVersion10) { /* v1.0 - 3.0 */
1068 ME_DisplayItem *para;
1069 PARAFORMAT2 *pFmt;
1070 RTFFlushOutputBuffer(info);
1071 para = info->editor->pCursors[0].pPara;
1072 pFmt = para->member.para.pFmt;
1073 if (pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE)
1075 /* rtfPar is treated like a space within a table. */
1076 info->rtfClass = rtfText;
1077 info->rtfMajor = ' ';
1079 else if (info->rtfMinor == rtfPar && tableDef)
1080 tableDef->numCellsInserted = 0;
1082 break;
1086 static BOOL ME_RTFInsertOleObject(RTF_Info *info, HENHMETAFILE hemf, HBITMAP hbmp,
1087 const SIZEL* sz)
1089 LPOLEOBJECT lpObject = NULL;
1090 LPSTORAGE lpStorage = NULL;
1091 LPOLECLIENTSITE lpClientSite = NULL;
1092 LPDATAOBJECT lpDataObject = NULL;
1093 LPOLECACHE lpOleCache = NULL;
1094 STGMEDIUM stgm;
1095 FORMATETC fm;
1096 CLSID clsid;
1097 BOOL ret = FALSE;
1098 DWORD conn;
1100 if (hemf)
1102 stgm.tymed = TYMED_ENHMF;
1103 stgm.u.hEnhMetaFile = hemf;
1104 fm.cfFormat = CF_ENHMETAFILE;
1106 else if (hbmp)
1108 stgm.tymed = TYMED_GDI;
1109 stgm.u.hBitmap = hbmp;
1110 fm.cfFormat = CF_BITMAP;
1112 stgm.pUnkForRelease = NULL;
1114 fm.ptd = NULL;
1115 fm.dwAspect = DVASPECT_CONTENT;
1116 fm.lindex = -1;
1117 fm.tymed = stgm.tymed;
1119 if (!info->lpRichEditOle)
1121 CreateIRichEditOle(info->editor, (VOID**)&info->lpRichEditOle);
1124 if (OleCreateDefaultHandler(&CLSID_NULL, NULL, &IID_IOleObject, (void**)&lpObject) == S_OK &&
1125 #if 0
1126 /* FIXME: enable it when rich-edit properly implements this method */
1127 IRichEditOle_GetClientSite(info->lpRichEditOle, &lpClientSite) == S_OK &&
1128 IOleObject_SetClientSite(lpObject, lpClientSite) == S_OK &&
1129 #endif
1130 IOleObject_GetUserClassID(lpObject, &clsid) == S_OK &&
1131 IOleObject_QueryInterface(lpObject, &IID_IOleCache, (void**)&lpOleCache) == S_OK &&
1132 IOleCache_Cache(lpOleCache, &fm, 0, &conn) == S_OK &&
1133 IOleObject_QueryInterface(lpObject, &IID_IDataObject, (void**)&lpDataObject) == S_OK &&
1134 IDataObject_SetData(lpDataObject, &fm, &stgm, TRUE) == S_OK)
1136 REOBJECT reobject;
1138 reobject.cbStruct = sizeof(reobject);
1139 reobject.cp = REO_CP_SELECTION;
1140 reobject.clsid = clsid;
1141 reobject.poleobj = lpObject;
1142 reobject.pstg = lpStorage;
1143 reobject.polesite = lpClientSite;
1144 /* convert from twips to .01 mm */
1145 reobject.sizel.cx = MulDiv(sz->cx, 254, 144);
1146 reobject.sizel.cy = MulDiv(sz->cy, 254, 144);
1147 reobject.dvaspect = DVASPECT_CONTENT;
1148 reobject.dwFlags = 0; /* FIXME */
1149 reobject.dwUser = 0;
1151 /* FIXME: could be simpler */
1152 ret = IRichEditOle_InsertObject(info->lpRichEditOle, &reobject) == S_OK;
1155 if (lpObject) IOleObject_Release(lpObject);
1156 if (lpClientSite) IOleClientSite_Release(lpClientSite);
1157 if (lpStorage) IStorage_Release(lpStorage);
1158 if (lpDataObject) IDataObject_Release(lpDataObject);
1159 if (lpOleCache) IOleCache_Release(lpOleCache);
1161 return ret;
1164 static void ME_RTFReadPictGroup(RTF_Info *info)
1166 SIZEL sz;
1167 BYTE* buffer = NULL;
1168 unsigned bufsz, bufidx;
1169 BOOL flip;
1170 BYTE val;
1171 METAFILEPICT mfp;
1172 HENHMETAFILE hemf;
1173 HBITMAP hbmp;
1174 enum gfxkind {gfx_unknown = 0, gfx_enhmetafile, gfx_metafile, gfx_dib} gfx = gfx_unknown;
1176 RTFGetToken (info);
1177 if (info->rtfClass == rtfEOF)
1178 return;
1179 mfp.mm = MM_TEXT;
1180 /* fetch picture type */
1181 if (RTFCheckMM (info, rtfPictAttr, rtfWinMetafile))
1183 mfp.mm = info->rtfParam;
1184 gfx = gfx_metafile;
1186 else if (RTFCheckMM (info, rtfPictAttr, rtfDevIndBitmap))
1188 if (info->rtfParam != 0) FIXME("dibitmap should be 0 (%d)\n", info->rtfParam);
1189 gfx = gfx_dib;
1191 else if (RTFCheckMM (info, rtfPictAttr, rtfEmfBlip))
1193 gfx = gfx_enhmetafile;
1195 else
1197 FIXME("%d %d\n", info->rtfMajor, info->rtfMinor);
1198 goto skip_group;
1200 sz.cx = sz.cy = 0;
1201 /* fetch picture attributes */
1202 for (;;)
1204 RTFGetToken (info);
1205 if (info->rtfClass == rtfEOF)
1206 return;
1207 if (info->rtfClass == rtfText)
1208 break;
1209 if (!RTFCheckCM (info, rtfControl, rtfPictAttr))
1211 ERR("Expected picture attribute (%d %d)\n",
1212 info->rtfClass, info->rtfMajor);
1213 goto skip_group;
1215 else if (RTFCheckMM (info, rtfPictAttr, rtfPicWid))
1217 if (gfx == gfx_metafile) mfp.xExt = info->rtfParam;
1219 else if (RTFCheckMM (info, rtfPictAttr, rtfPicHt))
1221 if (gfx == gfx_metafile) mfp.yExt = info->rtfParam;
1223 else if (RTFCheckMM (info, rtfPictAttr, rtfPicGoalWid))
1224 sz.cx = info->rtfParam;
1225 else if (RTFCheckMM (info, rtfPictAttr, rtfPicGoalHt))
1226 sz.cy = info->rtfParam;
1227 else
1228 FIXME("Non supported attribute: %d %d %d\n", info->rtfClass, info->rtfMajor, info->rtfMinor);
1230 /* fetch picture data */
1231 bufsz = 1024;
1232 bufidx = 0;
1233 buffer = HeapAlloc(GetProcessHeap(), 0, bufsz);
1234 val = info->rtfMajor;
1235 for (flip = TRUE;; flip = !flip)
1237 RTFGetToken (info);
1238 if (info->rtfClass == rtfEOF)
1240 HeapFree(GetProcessHeap(), 0, buffer);
1241 return; /* Warn ?? */
1243 if (RTFCheckCM(info, rtfGroup, rtfEndGroup))
1244 break;
1245 if (info->rtfClass != rtfText) goto skip_group;
1246 if (flip)
1248 if (bufidx >= bufsz &&
1249 !(buffer = HeapReAlloc(GetProcessHeap(), 0, buffer, bufsz += 1024)))
1250 goto skip_group;
1251 buffer[bufidx++] = RTFCharToHex(val) * 16 + RTFCharToHex(info->rtfMajor);
1253 else
1254 val = info->rtfMajor;
1256 if (flip) FIXME("wrong hex string\n");
1258 switch (gfx)
1260 case gfx_enhmetafile:
1261 if ((hemf = SetEnhMetaFileBits(bufidx, buffer)))
1262 ME_RTFInsertOleObject(info, hemf, NULL, &sz);
1263 break;
1264 case gfx_metafile:
1265 if ((hemf = SetWinMetaFileBits(bufidx, buffer, NULL, &mfp)))
1266 ME_RTFInsertOleObject(info, hemf, NULL, &sz);
1267 break;
1268 case gfx_dib:
1270 BITMAPINFO* bi = (BITMAPINFO*)buffer;
1271 HDC hdc = GetDC(0);
1272 unsigned nc = bi->bmiHeader.biClrUsed;
1274 /* not quite right, especially for bitfields type of compression */
1275 if (!nc && bi->bmiHeader.biBitCount <= 8)
1276 nc = 1 << bi->bmiHeader.biBitCount;
1277 if ((hbmp = CreateDIBitmap(hdc, &bi->bmiHeader,
1278 CBM_INIT, (char*)(bi + 1) + nc * sizeof(RGBQUAD),
1279 bi, DIB_RGB_COLORS)))
1280 ME_RTFInsertOleObject(info, NULL, hbmp, &sz);
1281 ReleaseDC(0, hdc);
1283 break;
1284 default:
1285 break;
1287 HeapFree(GetProcessHeap(), 0, buffer);
1288 RTFRouteToken (info); /* feed "}" back to router */
1289 return;
1290 skip_group:
1291 HeapFree(GetProcessHeap(), 0, buffer);
1292 RTFSkipGroup(info);
1293 RTFRouteToken(info); /* feed "}" back to router */
1296 /* for now, lookup the \result part and use it, whatever the object */
1297 static void ME_RTFReadObjectGroup(RTF_Info *info)
1299 for (;;)
1301 RTFGetToken (info);
1302 if (info->rtfClass == rtfEOF)
1303 return;
1304 if (RTFCheckCM(info, rtfGroup, rtfEndGroup))
1305 break;
1306 if (RTFCheckCM(info, rtfGroup, rtfBeginGroup))
1308 RTFGetToken (info);
1309 if (info->rtfClass == rtfEOF)
1310 return;
1311 if (RTFCheckCMM(info, rtfControl, rtfDestination, rtfObjResult))
1313 int level = 1;
1315 while (RTFGetToken (info) != rtfEOF)
1317 if (info->rtfClass == rtfGroup)
1319 if (info->rtfMajor == rtfBeginGroup) level++;
1320 else if (info->rtfMajor == rtfEndGroup && --level < 0) break;
1322 RTFRouteToken(info);
1325 else RTFSkipGroup(info);
1326 continue;
1328 if (!RTFCheckCM (info, rtfControl, rtfObjAttr))
1330 FIXME("Non supported attribute: %d %d %d\n", info->rtfClass, info->rtfMajor, info->rtfMinor);
1331 return;
1334 RTFRouteToken(info); /* feed "}" back to router */
1337 static void ME_RTFReadHook(RTF_Info *info)
1339 switch(info->rtfClass)
1341 case rtfGroup:
1342 switch(info->rtfMajor)
1344 case rtfBeginGroup:
1345 if (info->stackTop < maxStack) {
1346 info->stack[info->stackTop].style = info->style;
1347 ME_AddRefStyle(info->style);
1348 info->stack[info->stackTop].codePage = info->codePage;
1349 info->stack[info->stackTop].unicodeLength = info->unicodeLength;
1351 info->stackTop++;
1352 info->styleChanged = FALSE;
1353 break;
1354 case rtfEndGroup:
1356 RTFFlushOutputBuffer(info);
1357 info->stackTop--;
1358 if (info->stackTop <= 0)
1359 info->rtfClass = rtfEOF;
1360 if (info->stackTop < 0)
1361 return;
1363 ME_ReleaseStyle(info->style);
1364 info->style = info->stack[info->stackTop].style;
1365 info->codePage = info->stack[info->stackTop].codePage;
1366 info->unicodeLength = info->stack[info->stackTop].unicodeLength;
1367 break;
1370 break;
1374 void
1375 ME_StreamInFill(ME_InStream *stream)
1377 stream->editstream->dwError = stream->editstream->pfnCallback(stream->editstream->dwCookie,
1378 (BYTE *)stream->buffer,
1379 sizeof(stream->buffer),
1380 (LONG *)&stream->dwSize);
1381 stream->dwUsed = 0;
1384 static LRESULT ME_StreamIn(ME_TextEditor *editor, DWORD format, EDITSTREAM *stream, BOOL stripLastCR)
1386 RTF_Info parser;
1387 ME_Style *style;
1388 int from, to, to2, nUndoMode;
1389 int nEventMask = editor->nEventMask;
1390 ME_InStream inStream;
1391 BOOL invalidRTF = FALSE;
1393 TRACE("stream==%p editor==%p format==0x%X\n", stream, editor, format);
1394 editor->nEventMask = 0;
1396 ME_GetSelection(editor, &from, &to);
1397 if ((format & SFF_SELECTION) && (editor->mode & TM_RICHTEXT)) {
1398 style = ME_GetSelectionInsertStyle(editor);
1400 ME_InternalDeleteText(editor, from, to-from, FALSE);
1402 /* Don't insert text at the end of the table row */
1403 if (!editor->bEmulateVersion10) { /* v4.1 */
1404 ME_DisplayItem *para = editor->pCursors->pPara;
1405 if (para->member.para.nFlags & MEPF_ROWEND)
1407 para = para->member.para.next_para;
1408 editor->pCursors[0].pPara = para;
1409 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
1410 editor->pCursors[0].nOffset = 0;
1412 if (para->member.para.nFlags & MEPF_ROWSTART)
1414 para = para->member.para.next_para;
1415 editor->pCursors[0].pPara = para;
1416 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
1417 editor->pCursors[0].nOffset = 0;
1419 editor->pCursors[1] = editor->pCursors[0];
1420 } else { /* v1.0 - 3.0 */
1421 if (editor->pCursors[0].pRun->member.run.nFlags & MERF_ENDPARA &&
1422 ME_IsInTable(editor->pCursors[0].pRun))
1423 return 0;
1426 else {
1427 style = editor->pBuffer->pDefaultStyle;
1428 ME_AddRefStyle(style);
1429 ME_SetSelection(editor, 0, 0);
1430 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor), FALSE);
1431 from = to = 0;
1432 ME_ClearTempStyle(editor);
1433 ME_SetDefaultParaFormat(editor->pCursors[0].pPara->member.para.pFmt);
1437 /* Back up undo mode to a local variable */
1438 nUndoMode = editor->nUndoMode;
1440 /* Only create an undo if SFF_SELECTION is set */
1441 if (!(format & SFF_SELECTION))
1442 editor->nUndoMode = umIgnore;
1444 inStream.editstream = stream;
1445 inStream.editstream->dwError = 0;
1446 inStream.dwSize = 0;
1447 inStream.dwUsed = 0;
1449 if (format & SF_RTF)
1451 /* Check if it's really RTF, and if it is not, use plain text */
1452 ME_StreamInFill(&inStream);
1453 if (!inStream.editstream->dwError)
1455 if ((!editor->bEmulateVersion10 && strncmp(inStream.buffer, "{\\rtf", 5) && strncmp(inStream.buffer, "{\\urtf", 6))
1456 || (editor->bEmulateVersion10 && *inStream.buffer != '{'))
1458 invalidRTF = TRUE;
1459 inStream.editstream->dwError = -16;
1464 if (!invalidRTF && !inStream.editstream->dwError)
1466 if (format & SF_RTF) {
1467 /* setup the RTF parser */
1468 memset(&parser, 0, sizeof parser);
1469 RTFSetEditStream(&parser, &inStream);
1470 parser.rtfFormat = format&(SF_TEXT|SF_RTF);
1471 parser.editor = editor;
1472 parser.style = style;
1473 WriterInit(&parser);
1474 RTFInit(&parser);
1475 RTFSetReadHook(&parser, ME_RTFReadHook);
1476 RTFSetDestinationCallback(&parser, rtfPict, ME_RTFReadPictGroup);
1477 RTFSetDestinationCallback(&parser, rtfObject, ME_RTFReadObjectGroup);
1478 if (!parser.editor->bEmulateVersion10) /* v4.1 */
1480 RTFSetDestinationCallback(&parser, rtfNoNestTables, RTFSkipGroup);
1481 RTFSetDestinationCallback(&parser, rtfNestTableProps, RTFReadGroup);
1483 BeginFile(&parser);
1485 /* do the parsing */
1486 RTFRead(&parser);
1487 RTFFlushOutputBuffer(&parser);
1488 if (!editor->bEmulateVersion10) { /* v4.1 */
1489 if (parser.tableDef && parser.tableDef->tableRowStart &&
1490 (parser.nestingLevel > 0 || parser.canInheritInTbl))
1492 /* Delete any incomplete table row at the end of the rich text. */
1493 int nOfs, nChars;
1494 ME_DisplayItem *pCell;
1495 ME_DisplayItem *para;
1497 parser.rtfMinor = rtfRow;
1498 /* Complete the table row before deleting it.
1499 * By doing it this way we will have the current paragraph format set
1500 * properly to reflect that is not in the complete table, and undo items
1501 * will be added for this change to the current paragraph format. */
1502 if (parser.nestingLevel > 0)
1504 while (parser.nestingLevel > 1)
1505 ME_RTFSpecialCharHook(&parser); /* Decrements nestingLevel */
1506 para = parser.tableDef->tableRowStart;
1507 ME_RTFSpecialCharHook(&parser);
1508 } else {
1509 para = parser.tableDef->tableRowStart;
1510 ME_RTFSpecialCharHook(&parser);
1511 assert(para->member.para.nFlags & MEPF_ROWEND);
1512 para = para->member.para.next_para;
1514 pCell = para->member.para.pCell;
1516 editor->pCursors[1].pPara = para;
1517 editor->pCursors[1].pRun = ME_FindItemFwd(para, diRun);
1518 editor->pCursors[1].nOffset = 0;
1519 nOfs = ME_GetCursorOfs(&editor->pCursors[1]);
1520 nChars = ME_GetCursorOfs(&editor->pCursors[0]) - nOfs;
1521 ME_InternalDeleteText(editor, nOfs, nChars, TRUE);
1522 if (parser.tableDef)
1523 parser.tableDef->tableRowStart = NULL;
1526 ME_CheckTablesForCorruption(editor);
1527 RTFDestroy(&parser);
1528 if (parser.lpRichEditOle)
1529 IRichEditOle_Release(parser.lpRichEditOle);
1531 if (parser.stackTop > 0)
1533 while (--parser.stackTop >= 0)
1535 ME_ReleaseStyle(parser.style);
1536 parser.style = parser.stack[parser.stackTop].style;
1538 if (!inStream.editstream->dwError)
1539 inStream.editstream->dwError = HRESULT_FROM_WIN32(ERROR_HANDLE_EOF);
1542 /* Remove last line break, as mandated by tests. This is not affected by
1543 CR/LF counters, since RTF streaming presents only \para tokens, which
1544 are converted according to the standard rules: \r for 2.0, \r\n for 1.0
1546 if (stripLastCR) {
1547 int newfrom, newto;
1548 ME_GetSelection(editor, &newfrom, &newto);
1549 if (newto > to + (editor->bEmulateVersion10 ? 1 : 0)) {
1550 WCHAR lastchar[3] = {'\0', '\0'};
1551 int linebreakSize = editor->bEmulateVersion10 ? 2 : 1;
1553 ME_GetTextW(editor, lastchar, newto - linebreakSize, linebreakSize, 0);
1554 if (lastchar[0] == '\r' && (lastchar[1] == '\n' || lastchar[1] == '\0')) {
1555 ME_InternalDeleteText(editor, newto - linebreakSize, linebreakSize, FALSE);
1560 style = parser.style;
1562 else if (format & SF_TEXT)
1563 ME_StreamInText(editor, format, &inStream, style);
1564 else
1565 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
1566 ME_GetSelection(editor, &to, &to2);
1567 /* put the cursor at the top */
1568 if (!(format & SFF_SELECTION))
1569 ME_SetSelection(editor, 0, 0);
1572 /* Restore saved undo mode */
1573 editor->nUndoMode = nUndoMode;
1575 /* even if we didn't add an undo, we need to commit anything on the stack */
1576 ME_CommitUndo(editor);
1578 /* If SFF_SELECTION isn't set, delete any undos from before we started too */
1579 if (!(format & SFF_SELECTION))
1580 ME_EmptyUndoStack(editor);
1582 ME_ReleaseStyle(style);
1583 editor->nEventMask = nEventMask;
1584 ME_UpdateRepaint(editor);
1585 if (!(format & SFF_SELECTION)) {
1586 ME_ClearTempStyle(editor);
1588 ITextHost_TxShowCaret(editor->texthost, FALSE);
1589 ME_MoveCaret(editor);
1590 ITextHost_TxShowCaret(editor->texthost, TRUE);
1591 ME_SendSelChange(editor);
1592 ME_SendRequestResize(editor, FALSE);
1594 return 0;
1598 typedef struct tagME_RTFStringStreamStruct
1600 char *string;
1601 int pos;
1602 int length;
1603 } ME_RTFStringStreamStruct;
1605 static DWORD CALLBACK ME_ReadFromRTFString(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1607 ME_RTFStringStreamStruct *pStruct = (ME_RTFStringStreamStruct *)dwCookie;
1608 int count;
1610 count = min(cb, pStruct->length - pStruct->pos);
1611 memmove(lpBuff, pStruct->string + pStruct->pos, count);
1612 pStruct->pos += count;
1613 *pcb = count;
1614 return 0;
1617 static void
1618 ME_StreamInRTFString(ME_TextEditor *editor, BOOL selection, char *string)
1620 EDITSTREAM es;
1621 ME_RTFStringStreamStruct data;
1623 data.string = string;
1624 data.length = strlen(string);
1625 data.pos = 0;
1626 es.dwCookie = (DWORD_PTR)&data;
1627 es.pfnCallback = ME_ReadFromRTFString;
1628 ME_StreamIn(editor, SF_RTF | (selection ? SFF_SELECTION : 0), &es, FALSE);
1632 static int
1633 ME_FindText(ME_TextEditor *editor, DWORD flags, const CHARRANGE *chrg, const WCHAR *text, CHARRANGE *chrgText)
1635 const int nLen = lstrlenW(text);
1636 const int nTextLen = ME_GetTextLength(editor);
1637 int nStart, nEnd;
1638 int nMin, nMax;
1639 ME_DisplayItem *item;
1640 ME_DisplayItem *para;
1641 WCHAR wLastChar = ' ';
1643 TRACE("flags==0x%08x, chrg->cpMin==%d, chrg->cpMax==%d text==%s\n",
1644 flags, chrg->cpMin, chrg->cpMax, debugstr_w(text));
1646 if (flags & ~(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD))
1647 FIXME("Flags 0x%08x not implemented\n",
1648 flags & ~(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD));
1650 nMin = chrg->cpMin;
1651 if (chrg->cpMax == -1)
1652 nMax = nTextLen;
1653 else
1654 nMax = chrg->cpMax > nTextLen ? nTextLen : chrg->cpMax;
1656 /* In 1.0 emulation, if cpMax reaches end of text, add the FR_DOWN flag */
1657 if (editor->bEmulateVersion10 && nMax == nTextLen)
1659 flags |= FR_DOWN;
1662 /* In 1.0 emulation, cpMin must always be no greater than cpMax */
1663 if (editor->bEmulateVersion10 && nMax < nMin)
1665 if (chrgText)
1667 chrgText->cpMin = -1;
1668 chrgText->cpMax = -1;
1670 return -1;
1673 /* when searching up, if cpMin < cpMax, then instead of searching
1674 * on [cpMin,cpMax], we search on [0,cpMin], otherwise, search on
1675 * [cpMax, cpMin]. The exception is when cpMax is -1, in which
1676 * case, it is always bigger than cpMin.
1678 if (!editor->bEmulateVersion10 && !(flags & FR_DOWN))
1680 int nSwap = nMax;
1682 nMax = nMin > nTextLen ? nTextLen : nMin;
1683 if (nMin < nSwap || chrg->cpMax == -1)
1684 nMin = 0;
1685 else
1686 nMin = nSwap;
1689 if (!nLen || nMin < 0 || nMax < 0 || nMax < nMin)
1691 if (chrgText)
1692 chrgText->cpMin = chrgText->cpMax = -1;
1693 return -1;
1696 if (flags & FR_DOWN) /* Forward search */
1698 /* If possible, find the character before where the search starts */
1699 if ((flags & FR_WHOLEWORD) && nMin)
1701 ME_RunOfsFromCharOfs(editor, nMin - 1, NULL, &item, &nStart);
1702 wLastChar = item->member.run.strText->szData[nStart];
1705 ME_RunOfsFromCharOfs(editor, nMin, &para, &item, &nStart);
1707 while (item
1708 && para->member.para.nCharOfs + item->member.run.nCharOfs + nStart + nLen <= nMax)
1710 ME_DisplayItem *pCurItem = item;
1711 int nCurStart = nStart;
1712 int nMatched = 0;
1714 while (pCurItem && ME_CharCompare(pCurItem->member.run.strText->szData[nCurStart + nMatched], text[nMatched], (flags & FR_MATCHCASE)))
1716 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
1717 break;
1719 nMatched++;
1720 if (nMatched == nLen)
1722 ME_DisplayItem *pNextItem = pCurItem;
1723 int nNextStart = nCurStart;
1724 WCHAR wNextChar;
1726 /* Check to see if next character is a whitespace */
1727 if (flags & FR_WHOLEWORD)
1729 if (nCurStart + nMatched == pCurItem->member.run.strText->nLen)
1731 pNextItem = ME_FindItemFwd(pCurItem, diRun);
1732 nNextStart = -nMatched;
1735 if (pNextItem)
1736 wNextChar = pNextItem->member.run.strText->szData[nNextStart + nMatched];
1737 else
1738 wNextChar = ' ';
1740 if (isalnumW(wNextChar))
1741 break;
1744 nStart += para->member.para.nCharOfs + item->member.run.nCharOfs;
1745 if (chrgText)
1747 chrgText->cpMin = nStart;
1748 chrgText->cpMax = nStart + nLen;
1750 TRACE("found at %d-%d\n", nStart, nStart + nLen);
1751 return nStart;
1753 if (nCurStart + nMatched == pCurItem->member.run.strText->nLen)
1755 pCurItem = ME_FindItemFwd(pCurItem, diRun);
1756 nCurStart = -nMatched;
1759 if (pCurItem)
1760 wLastChar = pCurItem->member.run.strText->szData[nCurStart + nMatched];
1761 else
1762 wLastChar = ' ';
1764 nStart++;
1765 if (nStart == item->member.run.strText->nLen)
1767 item = ME_FindItemFwd(item, diRun);
1768 para = ME_GetParagraph(item);
1769 nStart = 0;
1773 else /* Backward search */
1775 /* If possible, find the character after where the search ends */
1776 if ((flags & FR_WHOLEWORD) && nMax < nTextLen - 1)
1778 ME_RunOfsFromCharOfs(editor, nMax + 1, NULL, &item, &nEnd);
1779 wLastChar = item->member.run.strText->szData[nEnd];
1782 ME_RunOfsFromCharOfs(editor, nMax, &para, &item, &nEnd);
1784 while (item
1785 && para->member.para.nCharOfs + item->member.run.nCharOfs + nEnd - nLen >= nMin)
1787 ME_DisplayItem *pCurItem = item;
1788 int nCurEnd = nEnd;
1789 int nMatched = 0;
1791 if (nCurEnd == 0)
1793 pCurItem = ME_FindItemBack(pCurItem, diRun);
1794 nCurEnd = pCurItem->member.run.strText->nLen + nMatched;
1797 while (pCurItem && ME_CharCompare(pCurItem->member.run.strText->szData[nCurEnd - nMatched - 1], text[nLen - nMatched - 1], (flags & FR_MATCHCASE)))
1799 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
1800 break;
1802 nMatched++;
1803 if (nMatched == nLen)
1805 ME_DisplayItem *pPrevItem = pCurItem;
1806 int nPrevEnd = nCurEnd;
1807 WCHAR wPrevChar;
1809 /* Check to see if previous character is a whitespace */
1810 if (flags & FR_WHOLEWORD)
1812 if (nPrevEnd - nMatched == 0)
1814 pPrevItem = ME_FindItemBack(pCurItem, diRun);
1815 if (pPrevItem)
1816 nPrevEnd = pPrevItem->member.run.strText->nLen + nMatched;
1819 if (pPrevItem)
1820 wPrevChar = pPrevItem->member.run.strText->szData[nPrevEnd - nMatched - 1];
1821 else
1822 wPrevChar = ' ';
1824 if (isalnumW(wPrevChar))
1825 break;
1828 nStart = ME_GetParagraph(pCurItem)->member.para.nCharOfs
1829 + pCurItem->member.run.nCharOfs + nCurEnd - nMatched;
1830 if (chrgText)
1832 chrgText->cpMin = nStart;
1833 chrgText->cpMax = nStart + nLen;
1835 TRACE("found at %d-%d\n", nStart, nStart + nLen);
1836 return nStart;
1838 if (nCurEnd - nMatched == 0)
1840 pCurItem = ME_FindItemBack(pCurItem, diRun);
1841 /* Don't care about pCurItem becoming NULL here; it's already taken
1842 * care of in the exterior loop condition */
1843 nCurEnd = pCurItem->member.run.strText->nLen + nMatched;
1846 if (pCurItem)
1847 wLastChar = pCurItem->member.run.strText->szData[nCurEnd - nMatched - 1];
1848 else
1849 wLastChar = ' ';
1851 nEnd--;
1852 if (nEnd < 0)
1854 item = ME_FindItemBack(item, diRun);
1855 para = ME_GetParagraph(item);
1856 nEnd = item->member.run.strText->nLen;
1860 TRACE("not found\n");
1861 if (chrgText)
1862 chrgText->cpMin = chrgText->cpMax = -1;
1863 return -1;
1866 static int ME_GetTextEx(ME_TextEditor *editor, GETTEXTEX *ex, LPARAM pText)
1868 int nStart, nCount; /* in chars */
1870 if (!ex->cb || !pText) return 0;
1872 if (ex->flags & ~(GT_SELECTION | GT_USECRLF))
1873 FIXME("GETTEXTEX flags 0x%08x not supported\n", ex->flags & ~(GT_SELECTION | GT_USECRLF));
1875 if (ex->flags & GT_SELECTION)
1877 ME_GetSelection(editor, &nStart, &nCount);
1878 nCount -= nStart;
1880 else
1882 nStart = 0;
1883 nCount = 0x7fffffff;
1885 if (ex->codepage == 1200)
1887 nCount = min(nCount, ex->cb / sizeof(WCHAR) - 1);
1888 return ME_GetTextW(editor, (LPWSTR)pText, nStart, nCount, ex->flags & GT_USECRLF);
1890 else
1892 /* potentially each char may be a CR, why calculate the exact value with O(N) when
1893 we can just take a bigger buffer? :)
1894 The above assumption still holds with CR/LF counters, since CR->CRLF expansion
1895 occurs only in richedit 2.0 mode, in which line breaks have only one CR
1897 int crlfmul = (ex->flags & GT_USECRLF) ? 2 : 1;
1898 LPWSTR buffer;
1899 DWORD buflen = ex->cb;
1900 LRESULT rc;
1901 DWORD flags = 0;
1903 nCount = min(nCount, ex->cb - 1);
1904 buffer = heap_alloc((crlfmul*nCount + 1) * sizeof(WCHAR));
1906 buflen = ME_GetTextW(editor, buffer, nStart, nCount, ex->flags & GT_USECRLF);
1907 rc = WideCharToMultiByte(ex->codepage, flags, buffer, buflen+1,
1908 (LPSTR)pText, ex->cb, ex->lpDefaultChar, ex->lpUsedDefChar);
1909 if (rc) rc--; /* do not count 0 terminator */
1911 heap_free(buffer);
1912 return rc;
1916 static int ME_GetTextRange(ME_TextEditor *editor, WCHAR *strText,
1917 int start, int nLen, BOOL unicode)
1919 if (unicode) {
1920 return ME_GetTextW(editor, strText, start, nLen, 0);
1921 } else {
1922 WCHAR *p = ALLOC_N_OBJ(WCHAR, nLen+1);
1923 int nChars = ME_GetTextW(editor, p, start, nLen, 0);
1924 WideCharToMultiByte(CP_ACP, 0, p, nChars+1, (char *)strText,
1925 nLen+1, NULL, NULL);
1926 FREE_OBJ(p);
1927 return nChars;
1931 typedef struct tagME_GlobalDestStruct
1933 HGLOBAL hData;
1934 int nLength;
1935 } ME_GlobalDestStruct;
1937 static DWORD CALLBACK ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1939 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
1940 int i;
1941 WORD *pSrc, *pDest;
1943 cb = cb >> 1;
1944 pDest = (WORD *)lpBuff;
1945 pSrc = GlobalLock(pData->hData);
1946 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
1947 pDest[i] = pSrc[pData->nLength+i];
1949 pData->nLength += i;
1950 *pcb = 2*i;
1951 GlobalUnlock(pData->hData);
1952 return 0;
1955 static DWORD CALLBACK ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1957 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
1958 int i;
1959 BYTE *pSrc, *pDest;
1961 pDest = lpBuff;
1962 pSrc = GlobalLock(pData->hData);
1963 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
1964 pDest[i] = pSrc[pData->nLength+i];
1966 pData->nLength += i;
1967 *pcb = i;
1968 GlobalUnlock(pData->hData);
1969 return 0;
1972 static BOOL ME_Paste(ME_TextEditor *editor)
1974 DWORD dwFormat = 0;
1975 EDITSTREAM es;
1976 ME_GlobalDestStruct gds;
1977 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
1978 UINT cf = 0;
1980 if (IsClipboardFormatAvailable(nRTFFormat))
1981 cf = nRTFFormat, dwFormat = SF_RTF;
1982 else if (IsClipboardFormatAvailable(CF_UNICODETEXT))
1983 cf = CF_UNICODETEXT, dwFormat = SF_TEXT|SF_UNICODE;
1984 else
1985 return FALSE;
1987 if (!OpenClipboard(editor->hWnd))
1988 return FALSE;
1989 gds.hData = GetClipboardData(cf);
1990 gds.nLength = 0;
1991 es.dwCookie = (DWORD_PTR)&gds;
1992 es.pfnCallback = dwFormat == SF_RTF ? ME_ReadFromHGLOBALRTF : ME_ReadFromHGLOBALUnicode;
1993 ME_StreamIn(editor, dwFormat|SFF_SELECTION, &es, FALSE);
1995 CloseClipboard();
1996 return TRUE;
1999 static BOOL ME_Copy(ME_TextEditor *editor, CHARRANGE *range)
2001 LPDATAOBJECT dataObj = NULL;
2002 HRESULT hr = S_OK;
2004 if (editor->cPasswordMask)
2005 return FALSE; /* Copying or Cutting masked text isn't allowed */
2007 if(editor->lpOleCallback)
2008 hr = IRichEditOleCallback_GetClipboardData(editor->lpOleCallback, range, RECO_COPY, &dataObj);
2009 if(FAILED(hr) || !dataObj)
2010 hr = ME_GetDataObject(editor, range, &dataObj);
2011 if(SUCCEEDED(hr)) {
2012 hr = OleSetClipboard(dataObj);
2013 IDataObject_Release(dataObj);
2015 return SUCCEEDED(hr) != 0;
2018 /* helper to send a msg filter notification */
2019 static BOOL
2020 ME_FilterEvent(ME_TextEditor *editor, UINT msg, WPARAM* wParam, LPARAM* lParam)
2022 MSGFILTER msgf;
2024 if (!editor->hWnd) return FALSE;
2025 msgf.nmhdr.hwndFrom = editor->hWnd;
2026 msgf.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
2027 msgf.nmhdr.code = EN_MSGFILTER;
2028 msgf.msg = msg;
2029 msgf.wParam = *wParam;
2030 msgf.lParam = *lParam;
2031 if (SendMessageW(GetParent(editor->hWnd), WM_NOTIFY, msgf.nmhdr.idFrom, (LPARAM)&msgf))
2032 return FALSE;
2033 *wParam = msgf.wParam;
2034 *lParam = msgf.lParam;
2035 msgf.wParam = *wParam;
2037 return TRUE;
2040 static void ME_UpdateSelectionLinkAttribute(ME_TextEditor *editor)
2042 ME_DisplayItem *startPara, *endPara;
2043 ME_DisplayItem *prev_para;
2044 int from, to;
2046 if (!editor->AutoURLDetect_bEnable) return;
2048 ME_GetSelection(editor, &from, &to);
2050 /* Find paragraph previous to the one that contains start cursor */
2051 ME_RunOfsFromCharOfs(editor, from, &startPara, NULL, NULL);
2052 prev_para = startPara->member.para.prev_para;
2053 if (prev_para->type == diParagraph) startPara = prev_para;
2055 /* Find paragraph that contains end cursor */
2056 ME_RunOfsFromCharOfs(editor, to, &endPara, NULL, NULL);
2057 endPara = endPara->member.para.next_para;
2059 ME_UpdateLinkAttribute(editor,
2060 startPara->member.para.nCharOfs,
2061 endPara->member.para.nCharOfs);
2064 static BOOL
2065 ME_KeyDown(ME_TextEditor *editor, WORD nKey)
2067 BOOL ctrl_is_down = GetKeyState(VK_CONTROL) & 0x8000;
2068 BOOL shift_is_down = GetKeyState(VK_SHIFT) & 0x8000;
2070 if (editor->bMouseCaptured)
2071 return FALSE;
2072 if (nKey != VK_SHIFT && nKey != VK_CONTROL && nKey != VK_MENU)
2073 editor->nSelectionType = stPosition;
2075 switch (nKey)
2077 case VK_LEFT:
2078 case VK_RIGHT:
2079 case VK_HOME:
2080 case VK_END:
2081 editor->nUDArrowX = -1;
2082 /* fall through */
2083 case VK_UP:
2084 case VK_DOWN:
2085 case VK_PRIOR:
2086 case VK_NEXT:
2087 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
2088 ME_ArrowKey(editor, nKey, shift_is_down, ctrl_is_down);
2089 return TRUE;
2090 case VK_BACK:
2091 case VK_DELETE:
2092 editor->nUDArrowX = -1;
2093 /* FIXME backspace and delete aren't the same, they act different wrt paragraph style of the merged paragraph */
2094 if (editor->styleFlags & ES_READONLY)
2095 return FALSE;
2096 if (ME_IsSelection(editor))
2098 ME_DeleteSelection(editor);
2099 ME_CommitUndo(editor);
2101 else if (nKey == VK_DELETE)
2103 /* Delete stops group typing.
2104 * (See MSDN remarks on EM_STOPGROUPTYPING message) */
2105 ME_DeleteTextAtCursor(editor, 1, 1);
2106 ME_CommitUndo(editor);
2108 else if (ME_ArrowKey(editor, VK_LEFT, FALSE, FALSE))
2110 BOOL bDeletionSucceeded;
2111 /* Backspace can be grouped for a single undo */
2112 ME_ContinueCoalescingTransaction(editor);
2113 bDeletionSucceeded = ME_DeleteTextAtCursor(editor, 1, 1);
2114 if (!bDeletionSucceeded && !editor->bEmulateVersion10) { /* v4.1 */
2115 /* Deletion was prevented so the cursor is moved back to where it was.
2116 * (e.g. this happens when trying to delete cell boundaries)
2118 ME_ArrowKey(editor, VK_RIGHT, FALSE, FALSE);
2120 ME_CommitCoalescingUndo(editor);
2122 else
2123 return TRUE;
2124 ME_MoveCursorFromTableRowStartParagraph(editor);
2125 ME_UpdateSelectionLinkAttribute(editor);
2126 ME_UpdateRepaint(editor);
2127 ME_SendRequestResize(editor, FALSE);
2128 return TRUE;
2129 case VK_RETURN:
2130 if (editor->styleFlags & ES_MULTILINE)
2132 ME_Cursor cursor = editor->pCursors[0];
2133 ME_DisplayItem *para = cursor.pPara;
2134 int from, to;
2135 const WCHAR endl = '\r';
2136 ME_Style *style;
2138 if (editor->styleFlags & ES_READONLY) {
2139 MessageBeep(MB_ICONERROR);
2140 return TRUE;
2143 ME_GetSelection(editor, &from, &to);
2144 if (editor->nTextLimit > ME_GetTextLength(editor) - (to-from))
2146 if (!editor->bEmulateVersion10) { /* v4.1 */
2147 if (para->member.para.nFlags & MEPF_ROWEND) {
2148 /* Add a new table row after this row. */
2149 para = ME_AppendTableRow(editor, para);
2150 para = para->member.para.next_para;
2151 editor->pCursors[0].pPara = para;
2152 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2153 editor->pCursors[0].nOffset = 0;
2154 editor->pCursors[1] = editor->pCursors[0];
2155 ME_CommitUndo(editor);
2156 ME_CheckTablesForCorruption(editor);
2157 ME_UpdateRepaint(editor);
2158 return TRUE;
2160 else if (para == editor->pCursors[1].pPara &&
2161 cursor.nOffset + cursor.pRun->member.run.nCharOfs == 0 &&
2162 para->member.para.prev_para->member.para.nFlags & MEPF_ROWSTART &&
2163 !para->member.para.prev_para->member.para.nCharOfs)
2165 /* Insert a newline before the table. */
2166 para = para->member.para.prev_para;
2167 para->member.para.nFlags &= ~MEPF_ROWSTART;
2168 editor->pCursors[0].pPara = para;
2169 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2170 editor->pCursors[1] = editor->pCursors[0];
2171 ME_InsertTextFromCursor(editor, 0, &endl, 1,
2172 editor->pCursors[0].pRun->member.run.style);
2173 para = editor->pBuffer->pFirst->member.para.next_para;
2174 ME_SetDefaultParaFormat(para->member.para.pFmt);
2175 para->member.para.nFlags = MEPF_REWRAP;
2176 editor->pCursors[0].pPara = para;
2177 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2178 editor->pCursors[1] = editor->pCursors[0];
2179 para->member.para.next_para->member.para.nFlags |= MEPF_ROWSTART;
2180 ME_CommitCoalescingUndo(editor);
2181 ME_CheckTablesForCorruption(editor);
2182 ME_UpdateRepaint(editor);
2183 return TRUE;
2185 } else { /* v1.0 - 3.0 */
2186 ME_DisplayItem *para = cursor.pPara;
2187 if (ME_IsInTable(para))
2189 if (cursor.pRun->member.run.nFlags & MERF_ENDPARA)
2191 if (from == to) {
2192 ME_ContinueCoalescingTransaction(editor);
2193 para = ME_AppendTableRow(editor, para);
2194 editor->pCursors[0].pPara = para;
2195 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2196 editor->pCursors[0].nOffset = 0;
2197 editor->pCursors[1] = editor->pCursors[0];
2198 ME_CommitCoalescingUndo(editor);
2199 ME_UpdateRepaint(editor);
2200 return TRUE;
2202 } else {
2203 ME_ContinueCoalescingTransaction(editor);
2204 if (cursor.pRun->member.run.nCharOfs + cursor.nOffset == 0 &&
2205 !ME_IsInTable(para->member.para.prev_para))
2207 /* Insert newline before table */
2208 cursor.pRun = ME_FindItemBack(para, diRun);
2209 if (cursor.pRun) {
2210 editor->pCursors[0].pRun = cursor.pRun;
2211 editor->pCursors[0].pPara = para->member.para.prev_para;
2213 editor->pCursors[0].nOffset = 0;
2214 editor->pCursors[1] = editor->pCursors[0];
2215 ME_InsertTextFromCursor(editor, 0, &endl, 1,
2216 editor->pCursors[0].pRun->member.run.style);
2217 } else {
2218 editor->pCursors[1] = editor->pCursors[0];
2219 para = ME_AppendTableRow(editor, para);
2220 editor->pCursors[0].pPara = para;
2221 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2222 editor->pCursors[0].nOffset = 0;
2223 editor->pCursors[1] = editor->pCursors[0];
2225 ME_CommitCoalescingUndo(editor);
2226 ME_UpdateRepaint(editor);
2227 return TRUE;
2232 style = ME_GetInsertStyle(editor, 0);
2233 ME_SaveTempStyle(editor);
2234 ME_ContinueCoalescingTransaction(editor);
2235 if (shift_is_down)
2236 ME_InsertEndRowFromCursor(editor, 0);
2237 else
2238 ME_InsertTextFromCursor(editor, 0, &endl, 1, style);
2239 ME_ReleaseStyle(style);
2240 ME_CommitCoalescingUndo(editor);
2241 SetCursor(NULL);
2243 ME_UpdateSelectionLinkAttribute(editor);
2244 ME_UpdateRepaint(editor);
2246 return TRUE;
2248 break;
2249 case 'A':
2250 if (ctrl_is_down)
2252 ME_SetSelection(editor, 0, -1);
2253 return TRUE;
2255 break;
2256 case 'V':
2257 if (ctrl_is_down)
2258 return ME_Paste(editor);
2259 break;
2260 case 'C':
2261 case 'X':
2262 if (ctrl_is_down)
2264 CHARRANGE range;
2265 BOOL result;
2267 ME_GetSelection(editor, &range.cpMin, &range.cpMax);
2268 result = ME_Copy(editor, &range);
2269 if (result && nKey == 'X')
2271 ME_InternalDeleteText(editor, range.cpMin, range.cpMax-range.cpMin, FALSE);
2272 ME_CommitUndo(editor);
2273 ME_UpdateRepaint(editor);
2275 return result;
2277 break;
2278 case 'Z':
2279 if (ctrl_is_down)
2281 ME_Undo(editor);
2282 return TRUE;
2284 break;
2285 case 'Y':
2286 if (ctrl_is_down)
2288 ME_Redo(editor);
2289 return TRUE;
2291 break;
2293 default:
2294 if (nKey != VK_SHIFT && nKey != VK_CONTROL && nKey && nKey != VK_MENU)
2295 editor->nUDArrowX = -1;
2296 if (ctrl_is_down)
2298 if (nKey == 'W')
2300 CHARFORMAT2W chf;
2301 char buf[2048];
2302 chf.cbSize = sizeof(chf);
2304 ME_GetSelectionCharFormat(editor, &chf);
2305 ME_DumpStyleToBuf(&chf, buf);
2306 MessageBoxA(NULL, buf, "Style dump", MB_OK);
2308 if (nKey == 'Q')
2310 ME_CheckCharOffsets(editor);
2314 return FALSE;
2317 static LRESULT ME_Char(ME_TextEditor *editor, WPARAM charCode,
2318 LPARAM flags, BOOL unicode)
2320 WCHAR wstr;
2322 if (editor->bMouseCaptured)
2323 return 0;
2325 if (unicode)
2326 wstr = (WCHAR)charCode;
2327 else
2329 CHAR charA = charCode;
2330 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &wstr, 1);
2333 if (editor->styleFlags & ES_READONLY) {
2334 MessageBeep(MB_ICONERROR);
2335 return 0; /* FIXME really 0 ? */
2338 if ((unsigned)wstr >= ' ' || wstr == '\t')
2340 ME_Cursor cursor = editor->pCursors[0];
2341 ME_DisplayItem *para = cursor.pPara;
2342 int from, to;
2343 BOOL ctrl_is_down = GetKeyState(VK_CONTROL) & 0x8000;
2344 ME_GetSelection(editor, &from, &to);
2345 if (wstr == '\t' &&
2346 /* v4.1 allows tabs to be inserted with ctrl key down */
2347 !(ctrl_is_down && !editor->bEmulateVersion10))
2349 ME_DisplayItem *para;
2350 BOOL bSelectedRow = FALSE;
2352 para = cursor.pPara;
2353 if (ME_IsSelection(editor) &&
2354 cursor.pRun->member.run.nCharOfs + cursor.nOffset == 0 &&
2355 to == ME_GetCursorOfs(&editor->pCursors[0]) &&
2356 para->member.para.prev_para->type == diParagraph)
2358 para = para->member.para.prev_para;
2359 bSelectedRow = TRUE;
2361 if (ME_IsInTable(para))
2363 ME_TabPressedInTable(editor, bSelectedRow);
2364 ME_CommitUndo(editor);
2365 return 0;
2367 } else if (!editor->bEmulateVersion10) { /* v4.1 */
2368 if (para->member.para.nFlags & MEPF_ROWEND) {
2369 if (from == to) {
2370 para = para->member.para.next_para;
2371 if (para->member.para.nFlags & MEPF_ROWSTART)
2372 para = para->member.para.next_para;
2373 editor->pCursors[0].pPara = para;
2374 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2375 editor->pCursors[0].nOffset = 0;
2376 editor->pCursors[1] = editor->pCursors[0];
2379 } else { /* v1.0 - 3.0 */
2380 if (ME_IsInTable(cursor.pRun) &&
2381 cursor.pRun->member.run.nFlags & MERF_ENDPARA &&
2382 from == to)
2384 /* Text should not be inserted at the end of the table. */
2385 MessageBeep(-1);
2386 return 0;
2389 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
2390 /* WM_CHAR is restricted to nTextLimit */
2391 if(editor->nTextLimit > ME_GetTextLength(editor) - (to-from))
2393 ME_Style *style = ME_GetInsertStyle(editor, 0);
2394 ME_SaveTempStyle(editor);
2395 ME_ContinueCoalescingTransaction(editor);
2396 ME_InsertTextFromCursor(editor, 0, &wstr, 1, style);
2397 ME_ReleaseStyle(style);
2398 ME_CommitCoalescingUndo(editor);
2399 ITextHost_TxSetCursor(editor->texthost, NULL, FALSE);
2402 ME_UpdateSelectionLinkAttribute(editor);
2403 ME_UpdateRepaint(editor);
2405 return 0;
2408 /* Process the message and calculate the new click count.
2410 * returns: The click count if it is mouse down event, else returns 0. */
2411 static int ME_CalculateClickCount(ME_TextEditor *editor, UINT msg, WPARAM wParam,
2412 LPARAM lParam)
2414 static int clickNum = 0;
2415 if (msg < WM_MOUSEFIRST || msg > WM_MOUSELAST)
2416 return 0;
2418 if ((msg == WM_LBUTTONDBLCLK) ||
2419 (msg == WM_RBUTTONDBLCLK) ||
2420 (msg == WM_MBUTTONDBLCLK) ||
2421 (msg == WM_XBUTTONDBLCLK))
2423 msg -= (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN);
2426 if ((msg == WM_LBUTTONDOWN) ||
2427 (msg == WM_RBUTTONDOWN) ||
2428 (msg == WM_MBUTTONDOWN) ||
2429 (msg == WM_XBUTTONDOWN))
2431 static MSG prevClickMsg;
2432 MSG clickMsg;
2433 /* Compare the editor instead of the hwnd so that the this
2434 * can still be done for windowless richedit controls. */
2435 clickMsg.hwnd = (HWND)editor;
2436 clickMsg.message = msg;
2437 clickMsg.wParam = wParam;
2438 clickMsg.lParam = lParam;
2439 clickMsg.time = GetMessageTime();
2440 clickMsg.pt.x = (short)LOWORD(lParam);
2441 clickMsg.pt.y = (short)HIWORD(lParam);
2442 if ((clickNum != 0) &&
2443 (clickMsg.message == prevClickMsg.message) &&
2444 (clickMsg.hwnd == prevClickMsg.hwnd) &&
2445 (clickMsg.wParam == prevClickMsg.wParam) &&
2446 (clickMsg.time - prevClickMsg.time < GetDoubleClickTime()) &&
2447 (abs(clickMsg.pt.x - prevClickMsg.pt.x) < GetSystemMetrics(SM_CXDOUBLECLK)/2) &&
2448 (abs(clickMsg.pt.y - prevClickMsg.pt.y) < GetSystemMetrics(SM_CYDOUBLECLK)/2))
2450 clickNum++;
2451 } else {
2452 clickNum = 1;
2454 prevClickMsg = clickMsg;
2455 } else {
2456 return 0;
2458 return clickNum;
2461 static BOOL ME_SetCursor(ME_TextEditor *editor)
2463 POINT pt;
2464 BOOL isExact;
2465 int offset;
2466 SCROLLBARINFO sbi;
2467 DWORD messagePos = GetMessagePos();
2468 pt.x = (short)LOWORD(messagePos);
2469 pt.y = (short)HIWORD(messagePos);
2471 if (editor->hWnd)
2473 sbi.cbSize = sizeof(sbi);
2474 GetScrollBarInfo(editor->hWnd, OBJID_HSCROLL, &sbi);
2475 if (!(sbi.rgstate[0] & (STATE_SYSTEM_INVISIBLE|STATE_SYSTEM_OFFSCREEN)) &&
2476 PtInRect(&sbi.rcScrollBar, pt))
2478 ITextHost_TxSetCursor(editor->texthost,
2479 LoadCursorW(NULL, (WCHAR*)IDC_ARROW), FALSE);
2480 return TRUE;
2482 sbi.cbSize = sizeof(sbi);
2483 GetScrollBarInfo(editor->hWnd, OBJID_VSCROLL, &sbi);
2484 if (!(sbi.rgstate[0] & (STATE_SYSTEM_INVISIBLE|STATE_SYSTEM_OFFSCREEN)) &&
2485 PtInRect(&sbi.rcScrollBar, pt))
2487 ITextHost_TxSetCursor(editor->texthost,
2488 LoadCursorW(NULL, (WCHAR*)IDC_ARROW), FALSE);
2489 return TRUE;
2492 ITextHost_TxScreenToClient(editor->texthost, &pt);
2494 if (editor->nSelectionType == stLine && editor->bMouseCaptured) {
2495 ITextHost_TxSetCursor(editor->texthost, hLeft, FALSE);
2496 return TRUE;
2498 if (!editor->bEmulateVersion10 /* v4.1 */ &&
2499 pt.y < editor->rcFormat.top &&
2500 pt.x < editor->rcFormat.left)
2502 ITextHost_TxSetCursor(editor->texthost, hLeft, FALSE);
2503 return TRUE;
2505 if (pt.y < editor->rcFormat.top || pt.y > editor->rcFormat.bottom)
2507 if (editor->bEmulateVersion10) /* v1.0 - 3.0 */
2508 ITextHost_TxSetCursor(editor->texthost,
2509 LoadCursorW(NULL, (WCHAR*)IDC_ARROW), FALSE);
2510 else /* v4.1 */
2511 ITextHost_TxSetCursor(editor->texthost,
2512 LoadCursorW(NULL, (WCHAR*)IDC_IBEAM), TRUE);
2513 return TRUE;
2515 if (pt.x < editor->rcFormat.left)
2517 ITextHost_TxSetCursor(editor->texthost, hLeft, FALSE);
2518 return TRUE;
2520 offset = ME_CharFromPos(editor, pt.x, pt.y, &isExact);
2521 if (isExact)
2523 ME_Cursor cursor;
2524 ME_Run *run;
2526 ME_CursorFromCharOfs(editor, offset, &cursor);
2527 run = &cursor.pRun->member.run;
2528 if (run->style->fmt.dwMask & CFM_LINK &&
2529 run->style->fmt.dwEffects & CFE_LINK)
2531 ITextHost_TxSetCursor(editor->texthost,
2532 LoadCursorW(NULL, (WCHAR*)IDC_HAND),
2533 FALSE);
2534 return TRUE;
2537 if (ME_IsSelection(editor))
2539 int selStart, selEnd;
2540 ME_GetSelection(editor, &selStart, &selEnd);
2541 if (selStart <= offset && selEnd >= offset) {
2542 ITextHost_TxSetCursor(editor->texthost,
2543 LoadCursorW(NULL, (WCHAR*)IDC_ARROW),
2544 FALSE);
2545 return TRUE;
2549 ITextHost_TxSetCursor(editor->texthost,
2550 LoadCursorW(NULL, (WCHAR*)IDC_IBEAM), TRUE);
2551 return TRUE;
2554 static void ME_SetDefaultFormatRect(ME_TextEditor *editor)
2556 ITextHost_TxGetClientRect(editor->texthost, &editor->rcFormat);
2557 editor->rcFormat.top += editor->exStyleFlags & WS_EX_CLIENTEDGE ? 1 : 0;
2558 editor->rcFormat.left += 1 + editor->selofs;
2559 editor->rcFormat.right -= 1;
2562 static BOOL ME_ShowContextMenu(ME_TextEditor *editor, int x, int y)
2564 CHARRANGE selrange;
2565 HMENU menu;
2566 int seltype = 0;
2567 if(!editor->lpOleCallback || !editor->hWnd)
2568 return FALSE;
2569 ME_GetSelection(editor, &selrange.cpMin, &selrange.cpMax);
2570 if(selrange.cpMin == selrange.cpMax)
2571 seltype |= SEL_EMPTY;
2572 else
2574 /* FIXME: Handle objects */
2575 seltype |= SEL_TEXT;
2576 if(selrange.cpMax-selrange.cpMin > 1)
2577 seltype |= SEL_MULTICHAR;
2579 if(SUCCEEDED(IRichEditOleCallback_GetContextMenu(editor->lpOleCallback, seltype, NULL, &selrange, &menu)))
2581 TrackPopupMenu(menu, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, GetParent(editor->hWnd), NULL);
2582 DestroyMenu(menu);
2584 return TRUE;
2587 ME_TextEditor *ME_MakeEditor(ITextHost *texthost, BOOL bEmulateVersion10)
2589 ME_TextEditor *ed = ALLOC_OBJ(ME_TextEditor);
2590 int i;
2591 DWORD props;
2592 LONG selbarwidth;
2594 ed->hWnd = NULL;
2595 ed->texthost = texthost;
2596 ed->bEmulateVersion10 = bEmulateVersion10;
2597 ITextHost_TxGetPropertyBits(texthost,
2598 (TXTBIT_RICHTEXT|TXTBIT_MULTILINE|
2599 TXTBIT_READONLY|TXTBIT_USEPASSWORD|
2600 TXTBIT_HIDESELECTION|TXTBIT_SAVESELECTION|
2601 TXTBIT_AUTOWORDSEL|TXTBIT_VERTICAL|
2602 TXTBIT_WORDWRAP|TXTBIT_DISABLEDRAG),
2603 &props);
2604 ITextHost_TxGetScrollBars(texthost, &ed->styleFlags);
2605 ed->styleFlags &= (WS_VSCROLL|WS_HSCROLL|ES_AUTOVSCROLL|
2606 ES_AUTOHSCROLL|ES_DISABLENOSCROLL);
2607 ed->pBuffer = ME_MakeText();
2608 ed->nZoomNumerator = ed->nZoomDenominator = 0;
2609 ed->nAvailWidth = 0; /* wrap to client area */
2610 ME_MakeFirstParagraph(ed);
2611 /* The four cursors are for:
2612 * 0 - The position where the caret is shown
2613 * 1 - The anchored end of the selection (for normal selection)
2614 * 2 & 3 - The anchored start and end respectively for word, line,
2615 * or paragraph selection.
2617 ed->nCursors = 4;
2618 ed->pCursors = ALLOC_N_OBJ(ME_Cursor, ed->nCursors);
2619 ed->pCursors[0].pPara = ed->pBuffer->pFirst->member.para.next_para;
2620 ed->pCursors[0].pRun = ME_FindItemFwd(ed->pCursors[0].pPara, diRun);
2621 ed->pCursors[0].nOffset = 0;
2622 ed->pCursors[1] = ed->pCursors[0];
2623 ed->pCursors[2] = ed->pCursors[0];
2624 ed->pCursors[3] = ed->pCursors[1];
2625 ed->nLastTotalLength = ed->nTotalLength = 0;
2626 ed->nLastTotalWidth = ed->nTotalWidth = 0;
2627 ed->nUDArrowX = -1;
2628 ed->nSequence = 0;
2629 ed->rgbBackColor = -1;
2630 ed->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
2631 ed->bCaretAtEnd = FALSE;
2632 ed->nEventMask = 0;
2633 ed->nModifyStep = 0;
2634 ed->nTextLimit = TEXT_LIMIT_DEFAULT;
2635 ed->pUndoStack = ed->pRedoStack = ed->pUndoStackBottom = NULL;
2636 ed->nUndoStackSize = 0;
2637 ed->nUndoLimit = STACK_SIZE_DEFAULT;
2638 ed->nUndoMode = umAddToUndo;
2639 ed->nParagraphs = 1;
2640 ed->nLastSelStart = ed->nLastSelEnd = 0;
2641 ed->pLastSelStartPara = ed->pLastSelEndPara = ME_FindItemFwd(ed->pBuffer->pFirst, diParagraph);
2642 ed->bHideSelection = FALSE;
2643 ed->pfnWordBreak = NULL;
2644 ed->lpOleCallback = NULL;
2645 ed->mode = TM_MULTILEVELUNDO | TM_MULTICODEPAGE;
2646 ed->mode |= (props & TXTBIT_RICHTEXT) ? TM_RICHTEXT : TM_PLAINTEXT;
2647 ed->AutoURLDetect_bEnable = FALSE;
2648 ed->bHaveFocus = FALSE;
2649 ed->bMouseCaptured = FALSE;
2650 for (i=0; i<HFONT_CACHE_SIZE; i++)
2652 ed->pFontCache[i].nRefs = 0;
2653 ed->pFontCache[i].nAge = 0;
2654 ed->pFontCache[i].hFont = NULL;
2657 ME_CheckCharOffsets(ed);
2658 ed->bDefaultFormatRect = TRUE;
2659 ITextHost_TxGetSelectionBarWidth(ed->texthost, &selbarwidth);
2660 if (selbarwidth) {
2661 /* FIXME: Convert selbarwidth from HIMETRIC to pixels */
2662 ed->selofs = SELECTIONBAR_WIDTH;
2663 ed->styleFlags |= ES_SELECTIONBAR;
2664 } else {
2665 ed->selofs = 0;
2667 ed->nSelectionType = stPosition;
2669 ed->cPasswordMask = 0;
2670 if (props & TXTBIT_USEPASSWORD)
2671 ITextHost_TxGetPasswordChar(texthost, &ed->cPasswordMask);
2673 if (props & TXTBIT_AUTOWORDSEL)
2674 ed->styleFlags |= ECO_AUTOWORDSELECTION;
2675 if (props & TXTBIT_MULTILINE) {
2676 ed->styleFlags |= ES_MULTILINE;
2677 ed->bWordWrap = (props & TXTBIT_WORDWRAP) != 0;
2678 } else {
2679 ed->bWordWrap = FALSE;
2681 if (props & TXTBIT_READONLY)
2682 ed->styleFlags |= ES_READONLY;
2683 if (!(props & TXTBIT_HIDESELECTION))
2684 ed->styleFlags |= ES_NOHIDESEL;
2685 if (props & TXTBIT_SAVESELECTION)
2686 ed->styleFlags |= ES_SAVESEL;
2687 if (props & TXTBIT_VERTICAL)
2688 ed->styleFlags |= ES_VERTICAL;
2689 if (props & TXTBIT_DISABLEDRAG)
2690 ed->styleFlags |= ES_NOOLEDRAGDROP;
2692 ed->notified_cr.cpMin = ed->notified_cr.cpMax = 0;
2694 /* Default scrollbar information */
2695 ed->vert_si.cbSize = sizeof(SCROLLINFO);
2696 ed->vert_si.nMin = 0;
2697 ed->vert_si.nMax = 0;
2698 ed->vert_si.nPage = 0;
2699 ed->vert_si.nPos = 0;
2701 ed->horz_si.cbSize = sizeof(SCROLLINFO);
2702 ed->horz_si.nMin = 0;
2703 ed->horz_si.nMax = 0;
2704 ed->horz_si.nPage = 0;
2705 ed->horz_si.nPos = 0;
2707 OleInitialize(NULL);
2709 return ed;
2712 static void ME_DestroyEditor(ME_TextEditor *editor)
2714 ME_DisplayItem *pFirst = editor->pBuffer->pFirst;
2715 ME_DisplayItem *p = pFirst, *pNext = NULL;
2716 int i;
2718 ME_ClearTempStyle(editor);
2719 ME_EmptyUndoStack(editor);
2720 while(p) {
2721 pNext = p->next;
2722 ME_DestroyDisplayItem(p);
2723 p = pNext;
2725 ME_ReleaseStyle(editor->pBuffer->pDefaultStyle);
2726 for (i=0; i<HFONT_CACHE_SIZE; i++)
2728 if (editor->pFontCache[i].hFont)
2729 DeleteObject(editor->pFontCache[i].hFont);
2731 if (editor->rgbBackColor != -1)
2732 DeleteObject(editor->hbrBackground);
2733 if(editor->lpOleCallback)
2734 IUnknown_Release(editor->lpOleCallback);
2735 IUnknown_Release(editor->texthost);
2736 OleUninitialize();
2738 FREE_OBJ(editor->pBuffer);
2739 FREE_OBJ(editor->pCursors);
2741 FREE_OBJ(editor);
2744 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
2746 TRACE("\n");
2747 switch (fdwReason)
2749 case DLL_PROCESS_ATTACH:
2750 DisableThreadLibraryCalls(hinstDLL);
2751 me_heap = HeapCreate (0, 0x10000, 0);
2752 if (!ME_RegisterEditorClass(hinstDLL)) return FALSE;
2753 hLeft = LoadCursorW(hinstDLL, MAKEINTRESOURCEW(OCR_REVERSE));
2754 LookupInit();
2755 break;
2757 case DLL_PROCESS_DETACH:
2758 UnregisterClassW(RICHEDIT_CLASS20W, 0);
2759 UnregisterClassW(MSFTEDIT_CLASS, 0);
2760 UnregisterClassA(RICHEDIT_CLASS20A, 0);
2761 UnregisterClassA("RichEdit50A", 0);
2762 if (ME_ListBoxRegistered)
2763 UnregisterClassW(REListBox20W, 0);
2764 if (ME_ComboBoxRegistered)
2765 UnregisterClassW(REComboBox20W, 0);
2766 LookupCleanup();
2767 HeapDestroy (me_heap);
2768 me_heap = NULL;
2769 break;
2771 return TRUE;
2775 static const char * const edit_messages[] = {
2776 "EM_GETSEL",
2777 "EM_SETSEL",
2778 "EM_GETRECT",
2779 "EM_SETRECT",
2780 "EM_SETRECTNP",
2781 "EM_SCROLL",
2782 "EM_LINESCROLL",
2783 "EM_SCROLLCARET",
2784 "EM_GETMODIFY",
2785 "EM_SETMODIFY",
2786 "EM_GETLINECOUNT",
2787 "EM_LINEINDEX",
2788 "EM_SETHANDLE",
2789 "EM_GETHANDLE",
2790 "EM_GETTHUMB",
2791 "EM_UNKNOWN_BF",
2792 "EM_UNKNOWN_C0",
2793 "EM_LINELENGTH",
2794 "EM_REPLACESEL",
2795 "EM_UNKNOWN_C3",
2796 "EM_GETLINE",
2797 "EM_LIMITTEXT",
2798 "EM_CANUNDO",
2799 "EM_UNDO",
2800 "EM_FMTLINES",
2801 "EM_LINEFROMCHAR",
2802 "EM_UNKNOWN_CA",
2803 "EM_SETTABSTOPS",
2804 "EM_SETPASSWORDCHAR",
2805 "EM_EMPTYUNDOBUFFER",
2806 "EM_GETFIRSTVISIBLELINE",
2807 "EM_SETREADONLY",
2808 "EM_SETWORDBREAKPROC",
2809 "EM_GETWORDBREAKPROC",
2810 "EM_GETPASSWORDCHAR",
2811 "EM_SETMARGINS",
2812 "EM_GETMARGINS",
2813 "EM_GETLIMITTEXT",
2814 "EM_POSFROMCHAR",
2815 "EM_CHARFROMPOS",
2816 "EM_SETIMESTATUS",
2817 "EM_GETIMESTATUS"
2820 static const char * const richedit_messages[] = {
2821 "EM_CANPASTE",
2822 "EM_DISPLAYBAND",
2823 "EM_EXGETSEL",
2824 "EM_EXLIMITTEXT",
2825 "EM_EXLINEFROMCHAR",
2826 "EM_EXSETSEL",
2827 "EM_FINDTEXT",
2828 "EM_FORMATRANGE",
2829 "EM_GETCHARFORMAT",
2830 "EM_GETEVENTMASK",
2831 "EM_GETOLEINTERFACE",
2832 "EM_GETPARAFORMAT",
2833 "EM_GETSELTEXT",
2834 "EM_HIDESELECTION",
2835 "EM_PASTESPECIAL",
2836 "EM_REQUESTRESIZE",
2837 "EM_SELECTIONTYPE",
2838 "EM_SETBKGNDCOLOR",
2839 "EM_SETCHARFORMAT",
2840 "EM_SETEVENTMASK",
2841 "EM_SETOLECALLBACK",
2842 "EM_SETPARAFORMAT",
2843 "EM_SETTARGETDEVICE",
2844 "EM_STREAMIN",
2845 "EM_STREAMOUT",
2846 "EM_GETTEXTRANGE",
2847 "EM_FINDWORDBREAK",
2848 "EM_SETOPTIONS",
2849 "EM_GETOPTIONS",
2850 "EM_FINDTEXTEX",
2851 "EM_GETWORDBREAKPROCEX",
2852 "EM_SETWORDBREAKPROCEX",
2853 "EM_SETUNDOLIMIT",
2854 "EM_UNKNOWN_USER_83",
2855 "EM_REDO",
2856 "EM_CANREDO",
2857 "EM_GETUNDONAME",
2858 "EM_GETREDONAME",
2859 "EM_STOPGROUPTYPING",
2860 "EM_SETTEXTMODE",
2861 "EM_GETTEXTMODE",
2862 "EM_AUTOURLDETECT",
2863 "EM_GETAUTOURLDETECT",
2864 "EM_SETPALETTE",
2865 "EM_GETTEXTEX",
2866 "EM_GETTEXTLENGTHEX",
2867 "EM_SHOWSCROLLBAR",
2868 "EM_SETTEXTEX",
2869 "EM_UNKNOWN_USER_98",
2870 "EM_UNKNOWN_USER_99",
2871 "EM_SETPUNCTUATION",
2872 "EM_GETPUNCTUATION",
2873 "EM_SETWORDWRAPMODE",
2874 "EM_GETWORDWRAPMODE",
2875 "EM_SETIMECOLOR",
2876 "EM_GETIMECOLOR",
2877 "EM_SETIMEOPTIONS",
2878 "EM_GETIMEOPTIONS",
2879 "EM_CONVPOSITION",
2880 "EM_UNKNOWN_USER_109",
2881 "EM_UNKNOWN_USER_110",
2882 "EM_UNKNOWN_USER_111",
2883 "EM_UNKNOWN_USER_112",
2884 "EM_UNKNOWN_USER_113",
2885 "EM_UNKNOWN_USER_114",
2886 "EM_UNKNOWN_USER_115",
2887 "EM_UNKNOWN_USER_116",
2888 "EM_UNKNOWN_USER_117",
2889 "EM_UNKNOWN_USER_118",
2890 "EM_UNKNOWN_USER_119",
2891 "EM_SETLANGOPTIONS",
2892 "EM_GETLANGOPTIONS",
2893 "EM_GETIMECOMPMODE",
2894 "EM_FINDTEXTW",
2895 "EM_FINDTEXTEXW",
2896 "EM_RECONVERSION",
2897 "EM_SETIMEMODEBIAS",
2898 "EM_GETIMEMODEBIAS"
2901 static const char *
2902 get_msg_name(UINT msg)
2904 if (msg >= EM_GETSEL && msg <= EM_CHARFROMPOS)
2905 return edit_messages[msg - EM_GETSEL];
2906 if (msg >= EM_CANPASTE && msg <= EM_GETIMEMODEBIAS)
2907 return richedit_messages[msg - EM_CANPASTE];
2908 return "";
2911 static void ME_LinkNotify(ME_TextEditor *editor, UINT msg, WPARAM wParam, LPARAM lParam)
2913 int x,y;
2914 ME_DisplayItem *para, *run;
2915 BOOL isExact;
2916 int nCharOfs; /* The start of the clicked text. Absolute character offset */
2918 ENLINK info;
2919 x = (short)LOWORD(lParam);
2920 y = (short)HIWORD(lParam);
2921 nCharOfs = ME_CharFromPos(editor, x, y, &isExact);
2922 if (!isExact) return;
2924 ME_RunOfsFromCharOfs(editor, nCharOfs, &para, &run, NULL);
2926 if ((run->member.run.style->fmt.dwMask & CFM_LINK)
2927 && (run->member.run.style->fmt.dwEffects & CFE_LINK))
2928 { /* The clicked run has CFE_LINK set */
2929 info.nmhdr.hwndFrom = editor->hWnd;
2930 info.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
2931 info.nmhdr.code = EN_LINK;
2932 info.msg = msg;
2933 info.wParam = wParam;
2934 info.lParam = lParam;
2935 info.chrg.cpMin = ME_CharOfsFromRunOfs(editor, para, run, 0);
2936 info.chrg.cpMax = info.chrg.cpMin + run->member.run.strText->nLen;
2937 SendMessageW(GetParent(editor->hWnd), WM_NOTIFY,info.nmhdr.idFrom, (LPARAM)&info);
2941 #define UNSUPPORTED_MSG(e) \
2942 case e: \
2943 FIXME(#e ": stub\n"); \
2944 *phresult = S_FALSE; \
2945 return 0;
2947 /* Handle messages for windowless and windoweded richedit controls.
2949 * The LRESULT that is returned is a return value for window procs,
2950 * and the phresult parameter is the COM return code needed by the
2951 * text services interface. */
2952 LRESULT ME_HandleMessage(ME_TextEditor *editor, UINT msg, WPARAM wParam,
2953 LPARAM lParam, BOOL unicode, HRESULT* phresult)
2955 *phresult = S_OK;
2957 switch(msg) {
2959 UNSUPPORTED_MSG(EM_DISPLAYBAND)
2960 UNSUPPORTED_MSG(EM_FINDWORDBREAK)
2961 UNSUPPORTED_MSG(EM_FMTLINES)
2962 UNSUPPORTED_MSG(EM_FORMATRANGE)
2963 UNSUPPORTED_MSG(EM_GETBIDIOPTIONS)
2964 UNSUPPORTED_MSG(EM_GETEDITSTYLE)
2965 UNSUPPORTED_MSG(EM_GETIMECOMPMODE)
2966 UNSUPPORTED_MSG(EM_GETIMESTATUS)
2967 UNSUPPORTED_MSG(EM_SETIMESTATUS)
2968 UNSUPPORTED_MSG(EM_GETLANGOPTIONS)
2969 UNSUPPORTED_MSG(EM_GETREDONAME)
2970 UNSUPPORTED_MSG(EM_GETTYPOGRAPHYOPTIONS)
2971 UNSUPPORTED_MSG(EM_GETUNDONAME)
2972 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX)
2973 UNSUPPORTED_MSG(EM_PASTESPECIAL)
2974 UNSUPPORTED_MSG(EM_SELECTIONTYPE)
2975 UNSUPPORTED_MSG(EM_SETBIDIOPTIONS)
2976 UNSUPPORTED_MSG(EM_SETEDITSTYLE)
2977 UNSUPPORTED_MSG(EM_SETFONTSIZE)
2978 UNSUPPORTED_MSG(EM_SETLANGOPTIONS)
2979 UNSUPPORTED_MSG(EM_SETMARGINS)
2980 UNSUPPORTED_MSG(EM_SETPALETTE)
2981 UNSUPPORTED_MSG(EM_SETTABSTOPS)
2982 UNSUPPORTED_MSG(EM_SETTYPOGRAPHYOPTIONS)
2983 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX)
2984 UNSUPPORTED_MSG(WM_STYLECHANGING)
2985 UNSUPPORTED_MSG(WM_STYLECHANGED)
2987 /* Messages specific to Richedit controls */
2989 case EM_STREAMIN:
2990 return ME_StreamIn(editor, wParam, (EDITSTREAM*)lParam, TRUE);
2991 case EM_STREAMOUT:
2992 return ME_StreamOut(editor, wParam, (EDITSTREAM *)lParam);
2993 case WM_GETDLGCODE:
2995 UINT code = DLGC_WANTCHARS|DLGC_WANTTAB|DLGC_WANTARROWS|DLGC_HASSETSEL;
2996 if (editor->styleFlags & ES_MULTILINE)
2997 code |= DLGC_WANTMESSAGE;
2998 return code;
3000 case EM_EMPTYUNDOBUFFER:
3001 ME_EmptyUndoStack(editor);
3002 return 0;
3003 case EM_GETSEL:
3005 /* Note: wParam/lParam can be NULL */
3006 UINT from, to;
3007 PUINT pfrom = wParam ? (PUINT)wParam : &from;
3008 PUINT pto = lParam ? (PUINT)lParam : &to;
3009 ME_GetSelection(editor, (int *)pfrom, (int *)pto);
3010 if ((*pfrom|*pto) & 0xFFFF0000)
3011 return -1;
3012 return MAKELONG(*pfrom,*pto);
3014 case EM_EXGETSEL:
3016 CHARRANGE *pRange = (CHARRANGE *)lParam;
3017 ME_GetSelection(editor, &pRange->cpMin, &pRange->cpMax);
3018 TRACE("EM_EXGETSEL = (%d,%d)\n", pRange->cpMin, pRange->cpMax);
3019 return 0;
3021 case EM_SETUNDOLIMIT:
3023 if ((int)wParam < 0)
3024 editor->nUndoLimit = STACK_SIZE_DEFAULT;
3025 else
3026 editor->nUndoLimit = min(wParam, STACK_SIZE_MAX);
3027 /* Setting a max stack size keeps wine from getting killed
3028 for hogging memory. Windows allocates all this memory at once, so
3029 no program would realistically set a value above our maximum. */
3030 return editor->nUndoLimit;
3032 case EM_CANUNDO:
3033 return editor->pUndoStack != NULL;
3034 case EM_CANREDO:
3035 return editor->pRedoStack != NULL;
3036 case WM_UNDO: /* FIXME: actually not the same */
3037 case EM_UNDO:
3038 return ME_Undo(editor);
3039 case EM_REDO:
3040 return ME_Redo(editor);
3041 case EM_GETOPTIONS:
3043 /* these flags are equivalent to the ES_* counterparts */
3044 DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
3045 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN | ECO_SELECTIONBAR;
3046 DWORD settings = editor->styleFlags & mask;
3048 return settings;
3050 case EM_SETOPTIONS:
3052 /* these flags are equivalent to ES_* counterparts, except for
3053 * ECO_AUTOWORDSELECTION that doesn't have an ES_* counterpart,
3054 * but is still stored in editor->styleFlags. */
3055 const DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
3056 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN |
3057 ECO_SELECTIONBAR | ECO_AUTOWORDSELECTION;
3058 DWORD settings = mask & editor->styleFlags;
3059 DWORD oldSettings = settings;
3060 DWORD changedSettings;
3062 switch(wParam)
3064 case ECOOP_SET:
3065 settings = lParam;
3066 break;
3067 case ECOOP_OR:
3068 settings |= lParam;
3069 break;
3070 case ECOOP_AND:
3071 settings &= lParam;
3072 break;
3073 case ECOOP_XOR:
3074 settings ^= lParam;
3076 changedSettings = oldSettings ^ settings;
3078 if (changedSettings) {
3079 editor->styleFlags = (editor->styleFlags & ~mask) | (settings & mask);
3081 if (changedSettings & ECO_SELECTIONBAR)
3083 ITextHost_TxInvalidateRect(editor->texthost, &editor->rcFormat, TRUE);
3084 if (settings & ECO_SELECTIONBAR) {
3085 assert(!editor->selofs);
3086 editor->selofs = SELECTIONBAR_WIDTH;
3087 editor->rcFormat.left += editor->selofs;
3088 } else {
3089 editor->rcFormat.left -= editor->selofs;
3090 editor->selofs = 0;
3092 ME_RewrapRepaint(editor);
3095 if (changedSettings & settings & ECO_VERTICAL)
3096 FIXME("ECO_VERTICAL not implemented yet!\n");
3097 if (changedSettings & settings & ECO_AUTOHSCROLL)
3098 FIXME("ECO_AUTOHSCROLL not implemented yet!\n");
3099 if (changedSettings & settings & ECO_AUTOVSCROLL)
3100 FIXME("ECO_AUTOVSCROLL not implemented yet!\n");
3101 if (changedSettings & settings & ECO_NOHIDESEL)
3102 FIXME("ECO_NOHIDESEL not implemented yet!\n");
3103 if (changedSettings & settings & ECO_WANTRETURN)
3104 FIXME("ECO_WANTRETURN not implemented yet!\n");
3105 if (changedSettings & settings & ECO_AUTOWORDSELECTION)
3106 FIXME("ECO_AUTOWORDSELECTION not implemented yet!\n");
3109 return settings;
3111 case EM_SETSEL:
3113 ME_InvalidateSelection(editor);
3114 ME_SetSelection(editor, wParam, lParam);
3115 ME_InvalidateSelection(editor);
3116 ITextHost_TxShowCaret(editor->texthost, FALSE);
3117 ME_ShowCaret(editor);
3118 ME_SendSelChange(editor);
3119 return 0;
3121 case EM_SETSCROLLPOS:
3123 POINT *point = (POINT *)lParam;
3124 ME_ScrollAbs(editor, point->x, point->y);
3125 return 0;
3127 case EM_AUTOURLDETECT:
3129 if (wParam==1 || wParam ==0)
3131 editor->AutoURLDetect_bEnable = (BOOL)wParam;
3132 return 0;
3134 return E_INVALIDARG;
3136 case EM_GETAUTOURLDETECT:
3138 return editor->AutoURLDetect_bEnable;
3140 case EM_EXSETSEL:
3142 int end;
3143 CHARRANGE range = *(CHARRANGE *)lParam;
3145 TRACE("EM_EXSETSEL (%d,%d)\n", range.cpMin, range.cpMax);
3147 ME_InvalidateSelection(editor);
3148 end = ME_SetSelection(editor, range.cpMin, range.cpMax);
3149 ME_InvalidateSelection(editor);
3150 ITextHost_TxShowCaret(editor->texthost, FALSE);
3151 ME_ShowCaret(editor);
3152 ME_SendSelChange(editor);
3154 return end;
3156 case EM_SHOWSCROLLBAR:
3158 DWORD flags;
3160 switch (wParam)
3162 case SB_HORZ:
3163 flags = WS_HSCROLL;
3164 break;
3165 case SB_VERT:
3166 flags = WS_VSCROLL;
3167 break;
3168 case SB_BOTH:
3169 flags = WS_HSCROLL|WS_VSCROLL;
3170 break;
3171 default:
3172 return 0;
3175 if (lParam) {
3176 editor->styleFlags |= flags;
3177 if (flags & WS_HSCROLL)
3178 ITextHost_TxShowScrollBar(editor->texthost, SB_HORZ,
3179 editor->nTotalWidth > editor->sizeWindow.cx);
3180 if (flags & WS_VSCROLL)
3181 ITextHost_TxShowScrollBar(editor->texthost, SB_VERT,
3182 editor->nTotalLength > editor->sizeWindow.cy);
3183 } else {
3184 editor->styleFlags &= ~flags;
3185 ITextHost_TxShowScrollBar(editor->texthost, wParam, FALSE);
3187 return 0;
3189 case EM_SETTEXTEX:
3191 LPWSTR wszText;
3192 SETTEXTEX *pStruct = (SETTEXTEX *)wParam;
3193 size_t len = 0;
3194 int from, to;
3195 ME_Style *style;
3196 BOOL bRtf, bUnicode, bSelection;
3197 int oldModify = editor->nModifyStep;
3199 if (!pStruct) return 0;
3201 /* If we detect ascii rtf at the start of the string,
3202 * we know it isn't unicode. */
3203 bRtf = (lParam && (!strncmp((char *)lParam, "{\\rtf", 5) ||
3204 !strncmp((char *)lParam, "{\\urtf", 6)));
3205 bUnicode = !bRtf && pStruct->codepage == 1200;
3207 TRACE("EM_SETTEXTEX - %s, flags %d, cp %d\n",
3208 bUnicode ? debugstr_w((LPCWSTR)lParam) : debugstr_a((LPCSTR)lParam),
3209 pStruct->flags, pStruct->codepage);
3211 bSelection = (pStruct->flags & ST_SELECTION) != 0;
3212 if (bSelection) {
3213 ME_GetSelection(editor, &from, &to);
3214 style = ME_GetSelectionInsertStyle(editor);
3215 ME_InternalDeleteText(editor, from, to - from, FALSE);
3216 } else {
3217 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor), FALSE);
3218 style = editor->pBuffer->pDefaultStyle;
3221 if (bRtf) {
3222 ME_StreamInRTFString(editor, bSelection, (char *)lParam);
3223 if (bSelection) {
3224 /* FIXME: The length returned is doesn't include the rtf control
3225 * characters, only the actual text. */
3226 len = lParam ? strlen((char *)lParam) : 0;
3228 } else {
3229 /* FIXME: make use of pStruct->codepage in the to unicode translation */
3230 wszText = lParam ? ME_ToUnicode(bUnicode, (void *)lParam) : NULL;
3231 len = wszText ? lstrlenW(wszText) : 0;
3232 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
3235 if (bSelection) {
3236 ME_ReleaseStyle(style);
3237 ME_UpdateSelectionLinkAttribute(editor);
3238 } else {
3239 len = 1;
3240 ME_UpdateLinkAttribute(editor, 0, -1);
3242 ME_CommitUndo(editor);
3243 if (!(pStruct->flags & ST_KEEPUNDO))
3245 editor->nModifyStep = oldModify;
3246 ME_EmptyUndoStack(editor);
3248 ME_UpdateRepaint(editor);
3249 return len;
3251 case EM_SETBKGNDCOLOR:
3253 LRESULT lColor;
3254 if (editor->rgbBackColor != -1) {
3255 DeleteObject(editor->hbrBackground);
3256 lColor = editor->rgbBackColor;
3258 else lColor = ITextHost_TxGetSysColor(editor->texthost, COLOR_WINDOW);
3260 if (wParam)
3262 editor->rgbBackColor = -1;
3263 editor->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
3265 else
3267 editor->rgbBackColor = lParam;
3268 editor->hbrBackground = CreateSolidBrush(editor->rgbBackColor);
3270 ITextHost_TxInvalidateRect(editor->texthost, NULL, TRUE);
3271 ITextHost_TxViewChange(editor->texthost, TRUE);
3272 return lColor;
3274 case EM_GETMODIFY:
3275 return editor->nModifyStep == 0 ? 0 : -1;
3276 case EM_SETMODIFY:
3278 if (wParam)
3279 editor->nModifyStep = 1;
3280 else
3281 editor->nModifyStep = 0;
3283 return 0;
3285 case EM_SETREADONLY:
3287 if (wParam)
3288 editor->styleFlags |= ES_READONLY;
3289 else
3290 editor->styleFlags &= ~ES_READONLY;
3291 return 0;
3293 case EM_SETEVENTMASK:
3295 DWORD nOldMask = editor->nEventMask;
3297 editor->nEventMask = lParam;
3298 return nOldMask;
3300 case EM_GETEVENTMASK:
3301 return editor->nEventMask;
3302 case EM_SETCHARFORMAT:
3304 CHARFORMAT2W buf, *p;
3305 BOOL bRepaint = TRUE;
3306 p = ME_ToCF2W(&buf, (CHARFORMAT2W *)lParam);
3307 if (p == NULL) return 0;
3308 if (!wParam)
3309 ME_SetDefaultCharFormat(editor, p);
3310 else if (wParam == (SCF_WORD | SCF_SELECTION)) {
3311 FIXME("EM_SETCHARFORMAT: word selection not supported\n");
3312 return 0;
3313 } else if (wParam == SCF_ALL) {
3314 if (editor->mode & TM_PLAINTEXT)
3315 ME_SetDefaultCharFormat(editor, p);
3316 else {
3317 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), p);
3318 editor->nModifyStep = 1;
3320 } else if (editor->mode & TM_PLAINTEXT) {
3321 return 0;
3322 } else {
3323 int from, to;
3324 ME_GetSelection(editor, &from, &to);
3325 bRepaint = (from != to);
3326 ME_SetSelectionCharFormat(editor, p);
3327 if (from != to) editor->nModifyStep = 1;
3329 ME_CommitUndo(editor);
3330 if (bRepaint)
3332 ME_WrapMarkedParagraphs(editor);
3333 ME_UpdateScrollBar(editor);
3334 ME_Repaint(editor);
3336 return 1;
3338 case EM_GETCHARFORMAT:
3340 CHARFORMAT2W tmp, *dst = (CHARFORMAT2W *)lParam;
3341 if (dst->cbSize != sizeof(CHARFORMATA) &&
3342 dst->cbSize != sizeof(CHARFORMATW) &&
3343 dst->cbSize != sizeof(CHARFORMAT2A) &&
3344 dst->cbSize != sizeof(CHARFORMAT2W))
3345 return 0;
3346 tmp.cbSize = sizeof(tmp);
3347 if (!wParam)
3348 ME_GetDefaultCharFormat(editor, &tmp);
3349 else
3350 ME_GetSelectionCharFormat(editor, &tmp);
3351 ME_CopyToCFAny(dst, &tmp);
3352 return tmp.dwMask;
3354 case EM_SETPARAFORMAT:
3356 BOOL result = ME_SetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
3357 ME_WrapMarkedParagraphs(editor);
3358 ME_UpdateScrollBar(editor);
3359 ME_Repaint(editor);
3360 ME_CommitUndo(editor);
3361 return result;
3363 case EM_GETPARAFORMAT:
3364 ME_GetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
3365 return ((PARAFORMAT2 *)lParam)->dwMask;
3366 case EM_GETFIRSTVISIBLELINE:
3368 ME_DisplayItem *p = editor->pBuffer->pFirst;
3369 int y = editor->vert_si.nPos;
3370 int ypara = 0;
3371 int count = 0;
3372 int ystart, yend;
3373 while(p) {
3374 p = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
3375 if (p->type == diTextEnd)
3376 break;
3377 if (p->type == diParagraph) {
3378 ypara = p->member.para.pt.y;
3379 continue;
3381 ystart = ypara + p->member.row.pt.y;
3382 yend = ystart + p->member.row.nHeight;
3383 if (y < yend) {
3384 break;
3386 count++;
3388 return count;
3390 case EM_HIDESELECTION:
3392 editor->bHideSelection = (wParam != 0);
3393 ME_InvalidateSelection(editor);
3394 return 0;
3396 case EM_LINESCROLL:
3398 if (!(editor->styleFlags & ES_MULTILINE))
3399 return FALSE;
3400 ME_ScrollDown(editor, lParam * 8); /* FIXME follow the original */
3401 return TRUE;
3403 case WM_CLEAR:
3405 int from, to;
3406 ME_GetSelection(editor, &from, &to);
3407 ME_InternalDeleteText(editor, from, to-from, FALSE);
3408 ME_CommitUndo(editor);
3409 ME_UpdateRepaint(editor);
3410 return 0;
3412 case EM_REPLACESEL:
3414 int from, to;
3415 ME_Style *style;
3416 LPWSTR wszText = lParam ? ME_ToUnicode(unicode, (void *)lParam) : NULL;
3417 size_t len = wszText ? lstrlenW(wszText) : 0;
3418 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText));
3420 ME_GetSelection(editor, &from, &to);
3421 style = ME_GetSelectionInsertStyle(editor);
3422 ME_InternalDeleteText(editor, from, to-from, FALSE);
3423 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
3424 ME_ReleaseStyle(style);
3425 /* drop temporary style if line end */
3427 * FIXME question: does abc\n mean: put abc,
3428 * clear temp style, put \n? (would require a change)
3430 if (len>0 && wszText[len-1] == '\n')
3431 ME_ClearTempStyle(editor);
3432 ME_EndToUnicode(unicode, wszText);
3433 ME_CommitUndo(editor);
3434 ME_UpdateSelectionLinkAttribute(editor);
3435 if (!wParam)
3436 ME_EmptyUndoStack(editor);
3437 ME_UpdateRepaint(editor);
3438 return len;
3440 case EM_SCROLLCARET:
3441 ME_EnsureVisible(editor, &editor->pCursors[0]);
3442 return 0;
3443 case WM_SETFONT:
3445 LOGFONTW lf;
3446 CHARFORMAT2W fmt;
3447 HDC hDC;
3448 BOOL bRepaint = LOWORD(lParam);
3450 if (!wParam)
3451 wParam = (WPARAM)GetStockObject(SYSTEM_FONT);
3452 GetObjectW((HGDIOBJ)wParam, sizeof(LOGFONTW), &lf);
3453 hDC = ITextHost_TxGetDC(editor->texthost);
3454 ME_CharFormatFromLogFont(hDC, &lf, &fmt);
3455 ITextHost_TxReleaseDC(editor->texthost, hDC);
3456 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), &fmt);
3457 ME_SetDefaultCharFormat(editor, &fmt);
3459 ME_CommitUndo(editor);
3460 ME_MarkAllForWrapping(editor);
3461 ME_WrapMarkedParagraphs(editor);
3462 ME_UpdateScrollBar(editor);
3463 if (bRepaint)
3464 ME_Repaint(editor);
3465 return 0;
3467 case WM_SETTEXT:
3469 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor), FALSE);
3470 if (lParam)
3472 TRACE("WM_SETTEXT lParam==%lx\n",lParam);
3473 if (!strncmp((char *)lParam, "{\\rtf", 5) ||
3474 !strncmp((char *)lParam, "{\\urtf", 6))
3476 /* Undocumented: WM_SETTEXT supports RTF text */
3477 ME_StreamInRTFString(editor, 0, (char *)lParam);
3479 else
3481 LPWSTR wszText = ME_ToUnicode(unicode, (void *)lParam);
3482 TRACE("WM_SETTEXT - %s\n", debugstr_w(wszText)); /* debugstr_w() */
3483 if (lstrlenW(wszText) > 0)
3485 int len = -1;
3487 /* uses default style! */
3488 if (!(editor->styleFlags & ES_MULTILINE))
3490 WCHAR * p;
3492 p = wszText;
3493 while (*p != '\0' && *p != '\r' && *p != '\n') p++;
3494 len = p - wszText;
3496 ME_InsertTextFromCursor(editor, 0, wszText, len, editor->pBuffer->pDefaultStyle);
3498 ME_EndToUnicode(unicode, wszText);
3501 else
3502 TRACE("WM_SETTEXT - NULL\n");
3503 ME_UpdateLinkAttribute(editor, 0, -1);
3504 ME_SetSelection(editor, 0, 0);
3505 editor->nModifyStep = 0;
3506 ME_CommitUndo(editor);
3507 ME_EmptyUndoStack(editor);
3508 ME_UpdateRepaint(editor);
3509 return 1;
3511 case EM_CANPASTE:
3513 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
3514 if (IsClipboardFormatAvailable(nRTFFormat))
3515 return TRUE;
3516 if (IsClipboardFormatAvailable(CF_UNICODETEXT))
3517 return TRUE;
3518 return FALSE;
3520 case WM_PASTE:
3521 ME_Paste(editor);
3522 return 0;
3523 case WM_CUT:
3524 case WM_COPY:
3526 CHARRANGE range;
3527 ME_GetSelection(editor, &range.cpMin, &range.cpMax);
3529 if (ME_Copy(editor, &range) && msg == WM_CUT)
3531 ME_InternalDeleteText(editor, range.cpMin, range.cpMax-range.cpMin, FALSE);
3532 ME_CommitUndo(editor);
3533 ME_UpdateRepaint(editor);
3535 return 0;
3537 case WM_GETTEXTLENGTH:
3539 GETTEXTLENGTHEX how;
3541 /* CR/LF conversion required in 2.0 mode, verbatim in 1.0 mode */
3542 how.flags = GTL_CLOSE | (editor->bEmulateVersion10 ? 0 : GTL_USECRLF) | GTL_NUMCHARS;
3543 how.codepage = unicode ? 1200 : CP_ACP;
3544 return ME_GetTextLengthEx(editor, &how);
3546 case EM_GETTEXTLENGTHEX:
3547 return ME_GetTextLengthEx(editor, (GETTEXTLENGTHEX *)wParam);
3548 case WM_GETTEXT:
3550 GETTEXTEX ex;
3551 ex.cb = wParam * (unicode ? sizeof(WCHAR) : sizeof(CHAR));
3552 ex.flags = GT_USECRLF;
3553 ex.codepage = unicode ? 1200 : CP_ACP;
3554 ex.lpDefaultChar = NULL;
3555 ex.lpUsedDefChar = NULL;
3556 return ME_GetTextEx(editor, &ex, lParam);
3558 case EM_GETTEXTEX:
3559 return ME_GetTextEx(editor, (GETTEXTEX*)wParam, lParam);
3560 case EM_GETSELTEXT:
3562 int from, to;
3563 ME_GetSelection(editor, &from, &to);
3564 return ME_GetTextRange(editor, (WCHAR *)lParam, from,
3565 to - from, unicode);
3567 case EM_GETSCROLLPOS:
3569 POINT *point = (POINT *)lParam;
3570 point->x = editor->horz_si.nPos;
3571 point->y = editor->vert_si.nPos;
3572 /* 16-bit scaled value is returned as stored in scrollinfo */
3573 if (editor->horz_si.nMax > 0xffff)
3574 point->x = MulDiv(point->x, 0xffff, editor->horz_si.nMax);
3575 if (editor->vert_si.nMax > 0xffff)
3576 point->y = MulDiv(point->y, 0xffff, editor->vert_si.nMax);
3577 return 1;
3579 case EM_GETTEXTRANGE:
3581 TEXTRANGEW *rng = (TEXTRANGEW *)lParam;
3582 int start = rng->chrg.cpMin;
3583 int end = rng->chrg.cpMax;
3584 int textlength = ME_GetTextLength(editor);
3585 TRACE("EM_GETTEXTRANGE min=%d max=%d unicode=%d emul1.0=%d length=%d\n",
3586 rng->chrg.cpMin, rng->chrg.cpMax, unicode,
3587 editor->bEmulateVersion10, ME_GetTextLength(editor));
3588 if (start < 0) return 0;
3589 if ((start == 0 && end == -1) || end > textlength)
3590 end = textlength;
3591 if (start >= end) return 0;
3592 return ME_GetTextRange(editor, rng->lpstrText, start, end - start, unicode);
3594 case EM_GETLINE:
3596 ME_DisplayItem *run;
3597 const unsigned int nMaxChars = *(WORD *) lParam;
3598 unsigned int nCharsLeft = nMaxChars;
3599 char *dest = (char *) lParam;
3600 BOOL wroteNull = FALSE;
3602 TRACE("EM_GETLINE: row=%d, nMaxChars=%d (%s)\n", (int) wParam, nMaxChars,
3603 unicode ? "Unicode" : "Ansi");
3605 run = ME_FindRowWithNumber(editor, wParam);
3606 if (run == NULL)
3607 return 0;
3609 while (nCharsLeft && (run = ME_FindItemFwd(run, diRunOrStartRow))
3610 && run->type == diRun)
3612 unsigned int nCopy;
3613 ME_String *strText;
3615 strText = run->member.run.strText;
3616 nCopy = min(nCharsLeft, strText->nLen);
3618 if (unicode)
3619 memcpy(dest, strText->szData, nCopy * sizeof(WCHAR));
3620 else
3621 nCopy = WideCharToMultiByte(CP_ACP, 0, strText->szData, nCopy, dest,
3622 nCharsLeft, NULL, NULL);
3623 dest += nCopy * (unicode ? sizeof(WCHAR) : 1);
3624 nCharsLeft -= nCopy;
3627 /* append line termination, space allowing */
3628 if (nCharsLeft > 0)
3630 if (unicode)
3631 *((WCHAR *)dest) = '\0';
3632 else
3633 *dest = '\0';
3634 nCharsLeft--;
3635 wroteNull = TRUE;
3638 TRACE("EM_GETLINE: got %u characters\n", nMaxChars - nCharsLeft);
3639 return nMaxChars - nCharsLeft - (wroteNull ? 1 : 0);
3641 case EM_GETLINECOUNT:
3643 ME_DisplayItem *item = editor->pBuffer->pFirst->next;
3644 int nRows = 0;
3646 ME_DisplayItem *prev_para = NULL, *last_para = NULL;
3648 while (item != editor->pBuffer->pLast)
3650 assert(item->type == diParagraph);
3651 prev_para = ME_FindItemBack(item, diRun);
3652 if (prev_para) {
3653 assert(prev_para->member.run.nFlags & MERF_ENDPARA);
3655 nRows += item->member.para.nRows;
3656 item = item->member.para.next_para;
3658 last_para = ME_FindItemBack(item, diRun);
3659 assert(last_para);
3660 assert(last_para->member.run.nFlags & MERF_ENDPARA);
3661 if (editor->bEmulateVersion10 && prev_para &&
3662 last_para->member.run.nCharOfs == 0 &&
3663 prev_para->member.run.strText->nLen == 1 &&
3664 prev_para->member.run.strText->szData[0] == '\r')
3666 /* In 1.0 emulation, the last solitary \r at the very end of the text
3667 (if one exists) is NOT a line break.
3668 FIXME: this is an ugly hack. This should have a more regular model. */
3669 nRows--;
3672 TRACE("EM_GETLINECOUNT: nRows==%d\n", nRows);
3673 return max(1, nRows);
3675 case EM_LINEFROMCHAR:
3677 if (wParam == -1)
3678 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(&editor->pCursors[1]));
3679 else
3680 return ME_RowNumberFromCharOfs(editor, wParam);
3682 case EM_EXLINEFROMCHAR:
3684 if (lParam == -1)
3685 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(&editor->pCursors[1]));
3686 else
3687 return ME_RowNumberFromCharOfs(editor, lParam);
3689 case EM_LINEINDEX:
3691 ME_DisplayItem *item, *para;
3692 int nCharOfs;
3694 if (wParam == -1)
3695 item = ME_FindItemBack(editor->pCursors[0].pRun, diStartRow);
3696 else
3697 item = ME_FindRowWithNumber(editor, wParam);
3698 if (!item)
3699 return -1;
3700 para = ME_GetParagraph(item);
3701 item = ME_FindItemFwd(item, diRun);
3702 nCharOfs = para->member.para.nCharOfs + item->member.run.nCharOfs;
3703 TRACE("EM_LINEINDEX: nCharOfs==%d\n", nCharOfs);
3704 return nCharOfs;
3706 case EM_LINELENGTH:
3708 ME_DisplayItem *item, *item_end;
3709 int nChars = 0, nThisLineOfs = 0, nNextLineOfs = 0;
3710 ME_DisplayItem *para, *run;
3712 if (wParam > ME_GetTextLength(editor))
3713 return 0;
3714 if (wParam == -1)
3716 FIXME("EM_LINELENGTH: returning number of unselected characters on lines with selection unsupported.\n");
3717 return 0;
3719 ME_RunOfsFromCharOfs(editor, wParam, &para, &run, NULL);
3720 item = ME_RowStart(run);
3721 nThisLineOfs = ME_CharOfsFromRunOfs(editor, para, ME_FindItemFwd(item, diRun), 0);
3722 item_end = ME_FindItemFwd(item, diStartRowOrParagraphOrEnd);
3723 if (item_end->type == diStartRow) {
3724 nNextLineOfs = ME_CharOfsFromRunOfs(editor, para, ME_FindItemFwd(item_end, diRun), 0);
3725 } else {
3726 ME_DisplayItem *endRun = ME_FindItemBack(item_end, diRun);
3727 assert(endRun && endRun->member.run.nFlags & MERF_ENDPARA);
3728 nNextLineOfs = item_end->member.para.nCharOfs - endRun->member.run.strText->nLen;
3730 nChars = nNextLineOfs - nThisLineOfs;
3731 TRACE("EM_LINELENGTH(%ld)==%d\n",wParam, nChars);
3732 return nChars;
3734 case EM_EXLIMITTEXT:
3736 if ((int)lParam < 0)
3737 return 0;
3738 if (lParam == 0)
3739 editor->nTextLimit = 65536;
3740 else
3741 editor->nTextLimit = (int) lParam;
3742 return 0;
3744 case EM_LIMITTEXT:
3746 if (wParam == 0)
3747 editor->nTextLimit = 65536;
3748 else
3749 editor->nTextLimit = (int) wParam;
3750 return 0;
3752 case EM_GETLIMITTEXT:
3754 return editor->nTextLimit;
3756 case EM_FINDTEXT:
3758 FINDTEXTA *ft = (FINDTEXTA *)lParam;
3759 int nChars = MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, NULL, 0);
3760 WCHAR *tmp;
3761 LRESULT r;
3763 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
3764 MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, tmp, nChars);
3765 r = ME_FindText(editor, wParam, &ft->chrg, tmp, NULL);
3766 FREE_OBJ( tmp );
3767 return r;
3769 case EM_FINDTEXTEX:
3771 FINDTEXTEXA *ex = (FINDTEXTEXA *)lParam;
3772 int nChars = MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, NULL, 0);
3773 WCHAR *tmp;
3774 LRESULT r;
3776 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
3777 MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, tmp, nChars);
3778 r = ME_FindText(editor, wParam, &ex->chrg, tmp, &ex->chrgText);
3779 FREE_OBJ( tmp );
3780 return r;
3782 case EM_FINDTEXTW:
3784 FINDTEXTW *ft = (FINDTEXTW *)lParam;
3785 return ME_FindText(editor, wParam, &ft->chrg, ft->lpstrText, NULL);
3787 case EM_FINDTEXTEXW:
3789 FINDTEXTEXW *ex = (FINDTEXTEXW *)lParam;
3790 return ME_FindText(editor, wParam, &ex->chrg, ex->lpstrText, &ex->chrgText);
3792 case EM_GETZOOM:
3793 if (!wParam || !lParam)
3794 return FALSE;
3795 *(int *)wParam = editor->nZoomNumerator;
3796 *(int *)lParam = editor->nZoomDenominator;
3797 return TRUE;
3798 case EM_SETZOOM:
3799 return ME_SetZoom(editor, wParam, lParam);
3800 case EM_CHARFROMPOS:
3801 return ME_CharFromPos(editor, ((POINTL *)lParam)->x, ((POINTL *)lParam)->y, NULL);
3802 case EM_POSFROMCHAR:
3804 ME_DisplayItem *pPara, *pRun;
3805 int nCharOfs, nOffset, nLength;
3806 POINTL pt = {0,0};
3808 nCharOfs = wParam;
3809 /* detect which API version we're dealing with */
3810 if (wParam >= 0x40000)
3811 nCharOfs = lParam;
3812 nLength = ME_GetTextLength(editor);
3813 nCharOfs = min(nCharOfs, nLength);
3814 nCharOfs = max(nCharOfs, 0);
3816 ME_RunOfsFromCharOfs(editor, nCharOfs, &pPara, &pRun, &nOffset);
3817 assert(pRun->type == diRun);
3818 pt.y = pRun->member.run.pt.y;
3819 pt.x = pRun->member.run.pt.x + ME_PointFromChar(editor, &pRun->member.run, nOffset);
3820 pt.y += pPara->member.para.pt.y + editor->rcFormat.top;
3821 pt.x += editor->rcFormat.left;
3823 pt.x -= editor->horz_si.nPos;
3824 pt.y -= editor->vert_si.nPos;
3826 if (wParam >= 0x40000) {
3827 *(POINTL *)wParam = pt;
3829 return (wParam >= 0x40000) ? 0 : MAKELONG( pt.x, pt.y );
3831 case WM_CREATE:
3833 INT max;
3835 ME_SetDefaultFormatRect(editor);
3837 max = (editor->styleFlags & ES_DISABLENOSCROLL) ? 1 : 0;
3838 if (~editor->styleFlags & ES_DISABLENOSCROLL || editor->styleFlags & WS_VSCROLL)
3839 ITextHost_TxSetScrollRange(editor->texthost, SB_VERT, 0, max, TRUE);
3841 if (~editor->styleFlags & ES_DISABLENOSCROLL || editor->styleFlags & WS_HSCROLL)
3842 ITextHost_TxSetScrollRange(editor->texthost, SB_HORZ, 0, max, TRUE);
3844 if (editor->styleFlags & ES_DISABLENOSCROLL)
3846 if (editor->styleFlags & WS_VSCROLL)
3848 ITextHost_TxEnableScrollBar(editor->texthost, SB_VERT, ESB_DISABLE_BOTH);
3849 ITextHost_TxShowScrollBar(editor->texthost, SB_VERT, TRUE);
3851 if (editor->styleFlags & WS_HSCROLL)
3853 ITextHost_TxEnableScrollBar(editor->texthost, SB_HORZ, ESB_DISABLE_BOTH);
3854 ITextHost_TxShowScrollBar(editor->texthost, SB_HORZ, TRUE);
3858 ME_CommitUndo(editor);
3859 ME_WrapMarkedParagraphs(editor);
3860 ME_MoveCaret(editor);
3861 return 0;
3863 case WM_DESTROY:
3864 ME_DestroyEditor(editor);
3865 return 0;
3866 case WM_SETCURSOR:
3868 return ME_SetCursor(editor);
3870 case WM_LBUTTONDBLCLK:
3871 case WM_LBUTTONDOWN:
3873 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
3874 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
3875 !ME_FilterEvent(editor, msg, &wParam, &lParam))
3876 return 0;
3877 ITextHost_TxSetFocus(editor->texthost);
3878 ME_LButtonDown(editor, (short)LOWORD(lParam), (short)HIWORD(lParam),
3879 ME_CalculateClickCount(editor, msg, wParam, lParam));
3880 ITextHost_TxSetCapture(editor->texthost, TRUE);
3881 editor->bMouseCaptured = TRUE;
3882 ME_LinkNotify(editor,msg,wParam,lParam);
3883 if (!ME_SetCursor(editor)) goto do_default;
3884 break;
3886 case WM_MOUSEMOVE:
3887 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
3888 !ME_FilterEvent(editor, msg, &wParam, &lParam))
3889 return 0;
3890 if (editor->bMouseCaptured)
3891 ME_MouseMove(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
3892 ME_LinkNotify(editor,msg,wParam,lParam);
3893 /* Set cursor if mouse is captured, since WM_SETCURSOR won't be received. */
3894 if (editor->bMouseCaptured)
3895 ME_SetCursor(editor);
3896 break;
3897 case WM_LBUTTONUP:
3898 if (editor->bMouseCaptured) {
3899 ITextHost_TxSetCapture(editor->texthost, FALSE);
3900 editor->bMouseCaptured = FALSE;
3902 if (editor->nSelectionType == stDocument)
3903 editor->nSelectionType = stPosition;
3904 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
3905 !ME_FilterEvent(editor, msg, &wParam, &lParam))
3906 return 0;
3907 else
3909 ME_SetCursor(editor);
3910 ME_LinkNotify(editor,msg,wParam,lParam);
3912 break;
3913 case WM_RBUTTONUP:
3914 case WM_RBUTTONDOWN:
3915 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
3916 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
3917 !ME_FilterEvent(editor, msg, &wParam, &lParam))
3918 return 0;
3919 goto do_default;
3920 case WM_CONTEXTMENU:
3921 if (!ME_ShowContextMenu(editor, (short)LOWORD(lParam), (short)HIWORD(lParam)))
3922 goto do_default;
3923 break;
3924 case WM_SETFOCUS:
3925 editor->bHaveFocus = TRUE;
3926 ME_ShowCaret(editor);
3927 ME_SendOldNotify(editor, EN_SETFOCUS);
3928 return 0;
3929 case WM_KILLFOCUS:
3930 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
3931 editor->bHaveFocus = FALSE;
3932 ME_HideCaret(editor);
3933 ME_SendOldNotify(editor, EN_KILLFOCUS);
3934 return 0;
3935 case WM_COMMAND:
3936 TRACE("editor wnd command = %d\n", LOWORD(wParam));
3937 return 0;
3938 case WM_KEYUP:
3939 if ((editor->nEventMask & ENM_KEYEVENTS) &&
3940 !ME_FilterEvent(editor, msg, &wParam, &lParam))
3941 return 0;
3942 goto do_default;
3943 case WM_KEYDOWN:
3944 if ((editor->nEventMask & ENM_KEYEVENTS) &&
3945 !ME_FilterEvent(editor, msg, &wParam, &lParam))
3946 return 0;
3947 if (ME_KeyDown(editor, LOWORD(wParam)))
3948 return 0;
3949 goto do_default;
3950 case WM_CHAR:
3951 return ME_Char(editor, wParam, lParam, unicode);
3952 case WM_UNICHAR:
3953 if (unicode)
3955 if(wParam == UNICODE_NOCHAR) return TRUE;
3956 if(wParam <= 0x000fffff)
3958 if(wParam > 0xffff) /* convert to surrogates */
3960 wParam -= 0x10000;
3961 ME_Char(editor, (wParam >> 10) + 0xd800, 0, TRUE);
3962 ME_Char(editor, (wParam & 0x03ff) + 0xdc00, 0, TRUE);
3963 } else {
3964 ME_Char(editor, wParam, 0, TRUE);
3967 return 0;
3969 break;
3970 case EM_STOPGROUPTYPING:
3971 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
3972 return 0;
3973 case WM_HSCROLL:
3975 const int scrollUnit = 7;
3977 switch(LOWORD(wParam))
3979 case SB_LEFT:
3980 ME_ScrollAbs(editor, 0, 0);
3981 break;
3982 case SB_RIGHT:
3983 ME_ScrollAbs(editor,
3984 editor->horz_si.nMax - (int)editor->horz_si.nPage,
3985 editor->vert_si.nMax - (int)editor->vert_si.nPage);
3986 break;
3987 case SB_LINELEFT:
3988 ME_ScrollLeft(editor, scrollUnit);
3989 break;
3990 case SB_LINERIGHT:
3991 ME_ScrollRight(editor, scrollUnit);
3992 break;
3993 case SB_PAGELEFT:
3994 ME_ScrollLeft(editor, editor->sizeWindow.cx);
3995 break;
3996 case SB_PAGERIGHT:
3997 ME_ScrollRight(editor, editor->sizeWindow.cx);
3998 break;
3999 case SB_THUMBTRACK:
4000 case SB_THUMBPOSITION:
4002 int pos = HIWORD(wParam);
4003 if (editor->horz_si.nMax > 0xffff)
4004 pos = MulDiv(pos, editor->horz_si.nMax, 0xffff);
4005 ME_HScrollAbs(editor, pos);
4006 break;
4009 break;
4011 case EM_SCROLL: /* fall through */
4012 case WM_VSCROLL:
4014 int origNPos;
4015 int lineHeight;
4017 origNPos = editor->vert_si.nPos;
4018 lineHeight = 24;
4020 if (editor->pBuffer && editor->pBuffer->pDefaultStyle)
4021 lineHeight = editor->pBuffer->pDefaultStyle->tm.tmHeight;
4022 if (lineHeight <= 0) lineHeight = 24;
4024 switch(LOWORD(wParam))
4026 case SB_TOP:
4027 ME_ScrollAbs(editor, 0, 0);
4028 break;
4029 case SB_BOTTOM:
4030 ME_ScrollAbs(editor,
4031 editor->horz_si.nMax - (int)editor->horz_si.nPage,
4032 editor->vert_si.nMax - (int)editor->vert_si.nPage);
4033 break;
4034 case SB_LINEUP:
4035 ME_ScrollUp(editor,lineHeight);
4036 break;
4037 case SB_LINEDOWN:
4038 ME_ScrollDown(editor,lineHeight);
4039 break;
4040 case SB_PAGEUP:
4041 ME_ScrollUp(editor,editor->sizeWindow.cy);
4042 break;
4043 case SB_PAGEDOWN:
4044 ME_ScrollDown(editor,editor->sizeWindow.cy);
4045 break;
4046 case SB_THUMBTRACK:
4047 case SB_THUMBPOSITION:
4049 int pos = HIWORD(wParam);
4050 if (editor->vert_si.nMax > 0xffff)
4051 pos = MulDiv(pos, editor->vert_si.nMax, 0xffff);
4052 ME_VScrollAbs(editor, pos);
4053 break;
4056 if (msg == EM_SCROLL)
4057 return 0x00010000 | (((editor->vert_si.nPos - origNPos)/lineHeight) & 0xffff);
4058 break;
4060 case WM_MOUSEWHEEL:
4062 int gcWheelDelta;
4063 UINT pulScrollLines;
4064 BOOL ctrl_is_down;
4066 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
4067 !ME_FilterEvent(editor, msg, &wParam, &lParam))
4068 return 0;
4070 ctrl_is_down = GetKeyState(VK_CONTROL) & 0x8000;
4072 gcWheelDelta = GET_WHEEL_DELTA_WPARAM(wParam);
4074 if (abs(gcWheelDelta) >= WHEEL_DELTA)
4076 if (ctrl_is_down) {
4077 int numerator;
4078 if (!editor->nZoomNumerator || !editor->nZoomDenominator)
4080 numerator = 100;
4081 } else {
4082 numerator = editor->nZoomNumerator * 100 / editor->nZoomDenominator;
4084 numerator = numerator + (gcWheelDelta / WHEEL_DELTA) * 10;
4085 if (numerator >= 10 && numerator <= 500)
4086 ME_SetZoom(editor, numerator, 100);
4087 } else {
4088 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
4089 /* FIXME follow the original */
4090 if (pulScrollLines)
4091 ME_ScrollDown(editor,pulScrollLines * (-gcWheelDelta / WHEEL_DELTA) * 8);
4094 break;
4096 case EM_GETRECT:
4098 *((RECT *)lParam) = editor->rcFormat;
4099 if (editor->bDefaultFormatRect)
4100 ((RECT *)lParam)->left -= editor->selofs;
4101 return 0;
4103 case EM_SETRECT:
4104 case EM_SETRECTNP:
4106 if (lParam)
4108 int border = 0;
4109 RECT clientRect;
4110 RECT *rc = (RECT *)lParam;
4112 border = editor->exStyleFlags & WS_EX_CLIENTEDGE ? 1 : 0;
4113 ITextHost_TxGetClientRect(editor->texthost, &clientRect);
4114 if (wParam == 0)
4116 editor->rcFormat.top = max(0, rc->top - border);
4117 editor->rcFormat.left = max(0, rc->left - border);
4118 editor->rcFormat.bottom = min(clientRect.bottom, rc->bottom);
4119 editor->rcFormat.right = min(clientRect.right, rc->right + border);
4120 } else if (wParam == 1) {
4121 /* MSDN incorrectly says a wParam value of 1 causes the
4122 * lParam rect to be used as a relative offset,
4123 * however, the tests show it just prevents min/max bound
4124 * checking. */
4125 editor->rcFormat.top = rc->top - border;
4126 editor->rcFormat.left = rc->left - border;
4127 editor->rcFormat.bottom = rc->bottom;
4128 editor->rcFormat.right = rc->right + border;
4129 } else {
4130 return 0;
4132 editor->bDefaultFormatRect = FALSE;
4134 else
4136 ME_SetDefaultFormatRect(editor);
4137 editor->bDefaultFormatRect = TRUE;
4139 ME_MarkAllForWrapping(editor);
4140 ME_WrapMarkedParagraphs(editor);
4141 ME_UpdateScrollBar(editor);
4142 if (msg != EM_SETRECTNP)
4143 ME_Repaint(editor);
4144 return 0;
4146 case EM_REQUESTRESIZE:
4147 ME_SendRequestResize(editor, TRUE);
4148 return 0;
4149 case WM_SETREDRAW:
4150 goto do_default;
4151 case WM_SIZE:
4153 RECT clientRect;
4155 ITextHost_TxGetClientRect(editor->texthost, &clientRect);
4156 if (editor->bDefaultFormatRect) {
4157 ME_SetDefaultFormatRect(editor);
4158 } else {
4159 editor->rcFormat.right += clientRect.right - editor->prevClientRect.right;
4160 editor->rcFormat.bottom += clientRect.bottom - editor->prevClientRect.bottom;
4162 editor->prevClientRect = clientRect;
4163 ME_RewrapRepaint(editor);
4164 goto do_default;
4166 /* IME messages to make richedit controls IME aware */
4167 case WM_IME_SETCONTEXT:
4168 case WM_IME_CONTROL:
4169 case WM_IME_SELECT:
4170 case WM_IME_COMPOSITIONFULL:
4171 return 0;
4172 case WM_IME_STARTCOMPOSITION:
4174 editor->imeStartIndex=ME_GetCursorOfs(&editor->pCursors[0]);
4175 ME_DeleteSelection(editor);
4176 ME_CommitUndo(editor);
4177 ME_UpdateRepaint(editor);
4178 return 0;
4180 case WM_IME_COMPOSITION:
4182 HIMC hIMC;
4184 ME_Style *style = ME_GetInsertStyle(editor, 0);
4185 hIMC = ITextHost_TxImmGetContext(editor->texthost);
4186 ME_DeleteSelection(editor);
4187 ME_CommitUndo(editor);
4188 ME_SaveTempStyle(editor);
4189 if (lParam & GCS_RESULTSTR)
4191 LPWSTR lpCompStr = NULL;
4192 DWORD dwBufLen;
4194 dwBufLen = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
4195 lpCompStr = HeapAlloc(GetProcessHeap(),0,dwBufLen + sizeof(WCHAR));
4196 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, lpCompStr, dwBufLen);
4197 lpCompStr[dwBufLen/sizeof(WCHAR)] = 0;
4198 ME_InsertTextFromCursor(editor,0,lpCompStr,dwBufLen/sizeof(WCHAR),style);
4200 else if (lParam & GCS_COMPSTR)
4202 LPWSTR lpCompStr = NULL;
4203 DWORD dwBufLen;
4205 dwBufLen = ImmGetCompositionStringW(hIMC, GCS_COMPSTR, NULL, 0);
4206 lpCompStr = HeapAlloc(GetProcessHeap(),0,dwBufLen + sizeof(WCHAR));
4207 ImmGetCompositionStringW(hIMC, GCS_COMPSTR, lpCompStr, dwBufLen);
4208 lpCompStr[dwBufLen/sizeof(WCHAR)] = 0;
4210 ME_InsertTextFromCursor(editor,0,lpCompStr,dwBufLen/sizeof(WCHAR),style);
4211 ME_SetSelection(editor,editor->imeStartIndex,
4212 editor->imeStartIndex + dwBufLen/sizeof(WCHAR));
4214 ME_ReleaseStyle(style);
4215 ME_UpdateRepaint(editor);
4216 return 0;
4218 case WM_IME_ENDCOMPOSITION:
4220 ME_DeleteSelection(editor);
4221 editor->imeStartIndex=-1;
4222 return 0;
4224 case EM_GETOLEINTERFACE:
4226 LPVOID *ppvObj = (LPVOID*) lParam;
4227 return CreateIRichEditOle(editor, ppvObj);
4229 case EM_GETPASSWORDCHAR:
4231 return editor->cPasswordMask;
4233 case EM_SETOLECALLBACK:
4234 if(editor->lpOleCallback)
4235 IUnknown_Release(editor->lpOleCallback);
4236 editor->lpOleCallback = (LPRICHEDITOLECALLBACK)lParam;
4237 if(editor->lpOleCallback)
4238 IUnknown_AddRef(editor->lpOleCallback);
4239 return TRUE;
4240 case EM_GETWORDBREAKPROC:
4241 return (LRESULT)editor->pfnWordBreak;
4242 case EM_SETWORDBREAKPROC:
4244 EDITWORDBREAKPROCW pfnOld = editor->pfnWordBreak;
4246 editor->pfnWordBreak = (EDITWORDBREAKPROCW)lParam;
4247 return (LRESULT)pfnOld;
4249 case EM_GETTEXTMODE:
4250 return editor->mode;
4251 case EM_SETTEXTMODE:
4253 LRESULT ret;
4254 int mask = 0;
4255 int changes = 0;
4256 GETTEXTLENGTHEX how;
4258 /* CR/LF conversion required in 2.0 mode, verbatim in 1.0 mode */
4259 how.flags = GTL_CLOSE | (editor->bEmulateVersion10 ? 0 : GTL_USECRLF) | GTL_NUMCHARS;
4260 how.codepage = unicode ? 1200 : CP_ACP;
4261 ret = ME_GetTextLengthEx(editor, &how);
4262 if (!ret)
4264 /*Check for valid wParam*/
4265 if ((((wParam & TM_RICHTEXT) && ((wParam & TM_PLAINTEXT) << 1))) ||
4266 (((wParam & TM_MULTILEVELUNDO) && ((wParam & TM_SINGLELEVELUNDO) << 1))) ||
4267 (((wParam & TM_MULTICODEPAGE) && ((wParam & TM_SINGLECODEPAGE) << 1))))
4268 return 1;
4269 else
4271 if (wParam & (TM_RICHTEXT | TM_PLAINTEXT))
4273 mask |= (TM_RICHTEXT | TM_PLAINTEXT);
4274 changes |= (wParam & (TM_RICHTEXT | TM_PLAINTEXT));
4276 /*FIXME: Currently no support for undo level and code page options*/
4277 editor->mode = (editor->mode & (~mask)) | changes;
4278 return 0;
4281 return ret;
4283 case EM_SETPASSWORDCHAR:
4285 editor->cPasswordMask = wParam;
4286 ME_RewrapRepaint(editor);
4287 return 0;
4289 case EM_SETTARGETDEVICE:
4290 if (wParam == 0)
4292 BOOL new = (lParam == 0 && (editor->styleFlags & ES_MULTILINE));
4293 if (editor->nAvailWidth || editor->bWordWrap != new)
4295 editor->bWordWrap = new;
4296 editor->nAvailWidth = 0; /* wrap to client area */
4297 ME_RewrapRepaint(editor);
4299 } else {
4300 int width = max(0, lParam);
4301 if ((editor->styleFlags & ES_MULTILINE) &&
4302 (!editor->bWordWrap || editor->nAvailWidth != width))
4304 editor->nAvailWidth = width;
4305 editor->bWordWrap = TRUE;
4306 ME_RewrapRepaint(editor);
4308 FIXME("EM_SETTARGETDEVICE doesn't use non-NULL target devices\n");
4310 return TRUE;
4311 default:
4312 do_default:
4313 *phresult = S_FALSE;
4314 break;
4316 return 0L;
4319 static LRESULT RichEditWndProc_common(HWND hWnd, UINT msg, WPARAM wParam,
4320 LPARAM lParam, BOOL unicode)
4322 ME_TextEditor *editor;
4323 HRESULT hresult;
4324 LRESULT lresult = 0;
4326 TRACE("enter hwnd %p msg %04x (%s) %lx %lx, unicode %d\n",
4327 hWnd, msg, get_msg_name(msg), wParam, lParam, unicode);
4329 editor = (ME_TextEditor *)GetWindowLongPtrW(hWnd, 0);
4330 if (!editor)
4332 if (msg == WM_NCCREATE)
4334 CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
4335 ITextHost *texthost;
4337 TRACE("WM_NCCREATE: hWnd %p style 0x%08x\n", hWnd, pcs->style);
4338 texthost = ME_CreateTextHost(hWnd, FALSE);
4339 return texthost != NULL;
4341 else if (msg != WM_NCDESTROY)
4343 ERR("called with invalid hWnd %p - application bug?\n", hWnd);
4344 return 0;
4348 switch (msg)
4350 case WM_PAINT:
4352 HDC hDC;
4353 RECT rc;
4354 PAINTSTRUCT ps;
4356 hDC = BeginPaint(editor->hWnd, &ps);
4357 /* Erase area outside of the formatting rectangle */
4358 if (ps.rcPaint.top < editor->rcFormat.top)
4360 rc = ps.rcPaint;
4361 rc.bottom = editor->rcFormat.top;
4362 FillRect(hDC, &rc, editor->hbrBackground);
4363 ps.rcPaint.top = editor->rcFormat.top;
4365 if (ps.rcPaint.bottom > editor->rcFormat.bottom) {
4366 rc = ps.rcPaint;
4367 rc.top = editor->rcFormat.bottom;
4368 FillRect(hDC, &rc, editor->hbrBackground);
4369 ps.rcPaint.bottom = editor->rcFormat.bottom;
4371 if (ps.rcPaint.left < editor->rcFormat.left) {
4372 rc = ps.rcPaint;
4373 rc.right = editor->rcFormat.left;
4374 FillRect(hDC, &rc, editor->hbrBackground);
4375 ps.rcPaint.left = editor->rcFormat.left;
4377 if (ps.rcPaint.right > editor->rcFormat.right) {
4378 rc = ps.rcPaint;
4379 rc.left = editor->rcFormat.right;
4380 FillRect(hDC, &rc, editor->hbrBackground);
4381 ps.rcPaint.right = editor->rcFormat.right;
4384 ME_PaintContent(editor, hDC, FALSE, &ps.rcPaint);
4385 EndPaint(editor->hWnd, &ps);
4386 return 0;
4388 case WM_ERASEBKGND:
4390 HDC hDC = (HDC)wParam;
4391 RECT rc;
4393 if (GetUpdateRect(editor->hWnd, &rc, TRUE))
4394 FillRect(hDC, &rc, editor->hbrBackground);
4395 return 1;
4397 case EM_SETOPTIONS:
4399 DWORD dwStyle;
4400 const DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
4401 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN |
4402 ECO_SELECTIONBAR;
4403 lresult = ME_HandleMessage(editor, msg, wParam, lParam, unicode, &hresult);
4404 dwStyle = GetWindowLongW(hWnd, GWL_STYLE);
4405 dwStyle = (dwStyle & ~mask) | (lresult & mask);
4406 SetWindowLongW(hWnd, GWL_STYLE, dwStyle);
4407 return lresult;
4409 case EM_SETREADONLY:
4411 DWORD dwStyle;
4412 lresult = ME_HandleMessage(editor, msg, wParam, lParam, unicode, &hresult);
4413 dwStyle = GetWindowLongW(hWnd, GWL_STYLE);
4414 dwStyle &= ~ES_READONLY;
4415 if (wParam)
4416 dwStyle |= ES_READONLY;
4417 SetWindowLongW(hWnd, GWL_STYLE, dwStyle);
4418 return lresult;
4420 default:
4421 lresult = ME_HandleMessage(editor, msg, wParam, lParam, unicode, &hresult);
4424 if (hresult == S_FALSE)
4425 lresult = DefWindowProcW(hWnd, msg, wParam, lParam);
4427 TRACE("exit hwnd %p msg %04x (%s) %lx %lx, unicode %d -> %lu\n",
4428 hWnd, msg, get_msg_name(msg), wParam, lParam, unicode, lresult);
4430 return lresult;
4433 static LRESULT WINAPI RichEditWndProcW(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
4435 BOOL unicode = TRUE;
4437 /* Under Win9x RichEdit20W returns ANSI strings, see the tests. */
4438 if (msg == WM_GETTEXT && (GetVersion() & 0x80000000))
4439 unicode = FALSE;
4441 return RichEditWndProc_common(hWnd, msg, wParam, lParam, unicode);
4444 static LRESULT WINAPI RichEditWndProcA(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
4446 return RichEditWndProc_common(hWnd, msg, wParam, lParam, FALSE);
4449 /******************************************************************
4450 * RichEditANSIWndProc (RICHED20.10)
4452 LRESULT WINAPI RichEditANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
4454 return RichEditWndProcA(hWnd, msg, wParam, lParam);
4457 /******************************************************************
4458 * RichEdit10ANSIWndProc (RICHED20.9)
4460 LRESULT WINAPI RichEdit10ANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
4462 if (msg == WM_NCCREATE && !GetWindowLongPtrW(hWnd, 0))
4464 ITextHost *texthost;
4465 CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
4467 TRACE("WM_NCCREATE: hWnd %p style 0x%08x\n", hWnd, pcs->style);
4468 texthost = ME_CreateTextHost(hWnd, TRUE);
4469 return texthost != NULL;
4471 return RichEditANSIWndProc(hWnd, msg, wParam, lParam);
4474 void ME_SendOldNotify(ME_TextEditor *editor, int nCode)
4476 ITextHost_TxNotify(editor->texthost, nCode, NULL);
4479 int ME_CountParagraphsBetween(ME_TextEditor *editor, int from, int to)
4481 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
4482 int i = 0;
4484 while(item && item->member.para.next_para->member.para.nCharOfs <= from)
4485 item = item->member.para.next_para;
4486 if (!item)
4487 return 0;
4488 while(item && item->member.para.next_para->member.para.nCharOfs <= to) {
4489 item = item->member.para.next_para;
4490 i++;
4492 return i;
4496 int ME_GetTextW(ME_TextEditor *editor, WCHAR *buffer, int nStart,
4497 int nChars, int bCRLF)
4499 ME_DisplayItem *pRun;
4500 int nOffset, nWritten = 0;
4501 WCHAR *pStart = buffer;
4503 ME_RunOfsFromCharOfs(editor, nStart, NULL, &pRun, &nOffset);
4505 /* bCRLF flag is only honored in 2.0 and up. 1.0 must always return text verbatim */
4506 if (editor->bEmulateVersion10) bCRLF = 0;
4508 while (nChars && pRun)
4510 int nLen;
4512 if (pRun->member.run.nFlags & MERF_ENDCELL &&
4513 pRun->member.run.nFlags & MERF_ENDPARA)
4515 *buffer = '\t';
4516 nLen = 1;
4517 } else if (pRun->member.run.nFlags & MERF_ENDPARA) {
4518 if (!ME_FindItemFwd(pRun, diRun)) {
4519 /* No '\r' is appended to the last paragraph. */
4520 nLen = 0;
4521 } else if (bCRLF && nChars == 1) {
4522 nLen = 0;
4523 nChars = 0;
4524 } else {
4525 WCHAR cr_lf[] = {'\r', '\n', 0};
4526 WCHAR *szData;
4528 if (bCRLF)
4530 nLen = 2;
4531 szData = cr_lf;
4532 } else {
4533 nLen = pRun->member.run.strText->nLen;
4534 szData = pRun->member.run.strText->szData;
4536 nLen = min(nChars, nLen - nOffset);
4537 CopyMemory(buffer, szData + nOffset, sizeof(WCHAR) * nLen);
4539 } else {
4540 nLen = min(nChars, pRun->member.run.strText->nLen - nOffset);
4541 CopyMemory(buffer, pRun->member.run.strText->szData + nOffset,
4542 sizeof(WCHAR) * nLen);
4544 nChars -= nLen;
4545 nWritten += nLen;
4546 buffer += nLen;
4547 nOffset = 0;
4549 pRun = ME_FindItemFwd(pRun, diRun);
4551 *buffer = 0;
4552 TRACE("nWritten=%d, actual=%d\n", nWritten, buffer-pStart);
4553 return nWritten;
4556 static BOOL ME_RegisterEditorClass(HINSTANCE hInstance)
4558 WNDCLASSW wcW;
4559 WNDCLASSA wcA;
4561 wcW.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
4562 wcW.lpfnWndProc = RichEditWndProcW;
4563 wcW.cbClsExtra = 0;
4564 wcW.cbWndExtra = sizeof(ME_TextEditor *);
4565 wcW.hInstance = NULL; /* hInstance would register DLL-local class */
4566 wcW.hIcon = NULL;
4567 wcW.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
4568 wcW.hbrBackground = GetStockObject(NULL_BRUSH);
4569 wcW.lpszMenuName = NULL;
4571 if (is_version_nt())
4573 wcW.lpszClassName = RICHEDIT_CLASS20W;
4574 if (!RegisterClassW(&wcW)) return FALSE;
4575 wcW.lpszClassName = MSFTEDIT_CLASS;
4576 if (!RegisterClassW(&wcW)) return FALSE;
4578 else
4580 /* WNDCLASSA/W have the same layout */
4581 wcW.lpszClassName = (LPCWSTR)"RichEdit20W";
4582 if (!RegisterClassA((WNDCLASSA *)&wcW)) return FALSE;
4583 wcW.lpszClassName = (LPCWSTR)"RichEdit50W";
4584 if (!RegisterClassA((WNDCLASSA *)&wcW)) return FALSE;
4587 wcA.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
4588 wcA.lpfnWndProc = RichEditWndProcA;
4589 wcA.cbClsExtra = 0;
4590 wcA.cbWndExtra = sizeof(ME_TextEditor *);
4591 wcA.hInstance = NULL; /* hInstance would register DLL-local class */
4592 wcA.hIcon = NULL;
4593 wcA.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
4594 wcA.hbrBackground = GetStockObject(NULL_BRUSH);
4595 wcA.lpszMenuName = NULL;
4596 wcA.lpszClassName = RICHEDIT_CLASS20A;
4597 if (!RegisterClassA(&wcA)) return FALSE;
4598 wcA.lpszClassName = "RichEdit50A";
4599 if (!RegisterClassA(&wcA)) return FALSE;
4601 return TRUE;
4604 static LRESULT WINAPI REComboWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
4605 /* FIXME: Not implemented */
4606 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
4607 hWnd, msg, get_msg_name(msg), wParam, lParam);
4608 return DefWindowProcW(hWnd, msg, wParam, lParam);
4611 static LRESULT WINAPI REListWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
4612 /* FIXME: Not implemented */
4613 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
4614 hWnd, msg, get_msg_name(msg), wParam, lParam);
4615 return DefWindowProcW(hWnd, msg, wParam, lParam);
4618 /******************************************************************
4619 * REExtendedRegisterClass (RICHED20.8)
4621 * FIXME undocumented
4622 * Need to check for errors and implement controls and callbacks
4624 LRESULT WINAPI REExtendedRegisterClass(void)
4626 WNDCLASSW wcW;
4627 UINT result;
4629 FIXME("semi stub\n");
4631 wcW.cbClsExtra = 0;
4632 wcW.cbWndExtra = 4;
4633 wcW.hInstance = NULL;
4634 wcW.hIcon = NULL;
4635 wcW.hCursor = NULL;
4636 wcW.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
4637 wcW.lpszMenuName = NULL;
4639 if (!ME_ListBoxRegistered)
4641 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS;
4642 wcW.lpfnWndProc = REListWndProc;
4643 wcW.lpszClassName = REListBox20W;
4644 if (RegisterClassW(&wcW)) ME_ListBoxRegistered = TRUE;
4647 if (!ME_ComboBoxRegistered)
4649 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
4650 wcW.lpfnWndProc = REComboWndProc;
4651 wcW.lpszClassName = REComboBox20W;
4652 if (RegisterClassW(&wcW)) ME_ComboBoxRegistered = TRUE;
4655 result = 0;
4656 if (ME_ListBoxRegistered)
4657 result += 1;
4658 if (ME_ComboBoxRegistered)
4659 result += 2;
4661 return result;
4664 static BOOL isurlspecial(WCHAR c)
4666 static const WCHAR special_chars[] = {'.','/','%','@','*','|','\\','+','#',0};
4667 return strchrW( special_chars, c ) != NULL;
4671 * This proc takes a selection, and scans it forward in order to select the span
4672 * of a possible URL candidate. A possible URL candidate must start with isalnum
4673 * or one of the following special characters: *|/\+%#@ and must consist entirely
4674 * of the characters allowed to start the URL, plus : (colon) which may occur
4675 * at most once, and not at either end.
4677 * sel_max == -1 indicates scan to end of text.
4679 static BOOL ME_FindNextURLCandidate(ME_TextEditor *editor, int sel_min, int sel_max,
4680 int * candidate_min, int * candidate_max)
4682 ME_DisplayItem * item;
4683 ME_DisplayItem * para;
4684 int nStart;
4685 BOOL foundColon = FALSE;
4686 WCHAR lastAcceptedChar = '\0';
4688 TRACE("sel_min = %d sel_max = %d\n", sel_min, sel_max);
4690 *candidate_min = *candidate_max = -1;
4691 ME_RunOfsFromCharOfs(editor, sel_min, &para, &item, &nStart);
4692 TRACE("nStart = %d\n", nStart);
4693 if (sel_max == -1) sel_max = ME_GetTextLength(editor);
4694 while (item && para->member.para.nCharOfs + item->member.run.nCharOfs + nStart < sel_max)
4696 ME_DisplayItem * next_item;
4698 if (!(item->member.run.nFlags & MERF_ENDPARA)) {
4699 /* Find start of candidate */
4700 if (*candidate_min == -1) {
4701 while (nStart < item->member.run.strText->nLen &&
4702 !(isalnumW(item->member.run.strText->szData[nStart]) ||
4703 isurlspecial(item->member.run.strText->szData[nStart]))) {
4704 nStart++;
4706 if (nStart < item->member.run.strText->nLen &&
4707 (isalnumW(item->member.run.strText->szData[nStart]) ||
4708 isurlspecial(item->member.run.strText->szData[nStart]))) {
4709 *candidate_min = para->member.para.nCharOfs + item->member.run.nCharOfs + nStart;
4710 lastAcceptedChar = item->member.run.strText->szData[nStart];
4711 nStart++;
4715 /* Find end of candidate */
4716 if (*candidate_min >= 0) {
4717 while (nStart < item->member.run.strText->nLen &&
4718 (isalnumW(item->member.run.strText->szData[nStart]) ||
4719 isurlspecial(item->member.run.strText->szData[nStart]) ||
4720 (!foundColon && item->member.run.strText->szData[nStart] == ':') )) {
4721 if (item->member.run.strText->szData[nStart] == ':') foundColon = TRUE;
4722 lastAcceptedChar = item->member.run.strText->szData[nStart];
4723 nStart++;
4725 if (nStart < item->member.run.strText->nLen &&
4726 !(isalnumW(item->member.run.strText->szData[nStart]) ||
4727 isurlspecial(item->member.run.strText->szData[nStart]) )) {
4728 *candidate_max = para->member.para.nCharOfs + item->member.run.nCharOfs + nStart;
4729 nStart++;
4730 if (lastAcceptedChar == ':') (*candidate_max)--;
4731 return TRUE;
4734 } else {
4735 /* End of paragraph: skip it if before candidate span, or terminates
4736 current active span */
4737 if (*candidate_min >= 0) {
4738 *candidate_max = para->member.para.nCharOfs + item->member.run.nCharOfs;
4739 if (lastAcceptedChar == ':') (*candidate_max)--;
4740 return TRUE;
4744 /* Reaching this point means no span was found, so get next span */
4745 next_item = ME_FindItemFwd(item, diRun);
4746 if (!next_item) {
4747 if (*candidate_min >= 0) {
4748 /* There are no further runs, so take end of text as end of candidate */
4749 *candidate_max = para->member.para.nCharOfs + item->member.run.nCharOfs + nStart;
4750 if (lastAcceptedChar == ':') (*candidate_max)--;
4751 return TRUE;
4754 item = next_item;
4755 para = ME_GetParagraph(item);
4756 nStart = 0;
4759 if (item) {
4760 if (*candidate_min >= 0) {
4761 /* There are no further runs, so take end of text as end of candidate */
4762 *candidate_max = para->member.para.nCharOfs + item->member.run.nCharOfs + nStart;
4763 if (lastAcceptedChar == ':') (*candidate_max)--;
4764 return TRUE;
4767 return FALSE;
4771 * This proc evaluates the selection and returns TRUE if it can be considered an URL
4773 static BOOL ME_IsCandidateAnURL(ME_TextEditor *editor, int sel_min, int sel_max)
4775 struct prefix_s {
4776 const char *text;
4777 int length;
4778 } prefixes[12] = {
4779 /* Code below depends on these being in decreasing length order! */
4780 {"prospero:", 10},
4781 {"telnet:", 8},
4782 {"gopher:", 8},
4783 {"mailto:", 8},
4784 {"https:", 7},
4785 {"file:", 6},
4786 {"news:", 6},
4787 {"wais:", 6},
4788 {"nntp:", 6},
4789 {"http:", 5},
4790 {"www.", 5},
4791 {"ftp:", 5},
4793 LPWSTR bufferW = NULL;
4794 WCHAR bufW[32];
4795 unsigned int i;
4797 if (sel_max == -1) sel_max = ME_GetTextLength(editor);
4798 assert(sel_min <= sel_max);
4799 for (i = 0; i < sizeof(prefixes) / sizeof(struct prefix_s); i++)
4801 if (sel_max - sel_min < prefixes[i].length) continue;
4802 if (bufferW == NULL) {
4803 bufferW = heap_alloc((sel_max - sel_min + 1) * sizeof(WCHAR));
4805 ME_GetTextW(editor, bufferW, sel_min, min(sel_max - sel_min, lstrlenA(prefixes[i].text)), 0);
4806 MultiByteToWideChar(CP_ACP, 0, prefixes[i].text, -1, bufW, 32);
4807 if (!lstrcmpW(bufW, bufferW))
4809 heap_free(bufferW);
4810 return TRUE;
4813 heap_free(bufferW);
4814 return FALSE;
4818 * This proc walks through the indicated selection and evaluates whether each
4819 * section identified by ME_FindNextURLCandidate and in-between sections have
4820 * their proper CFE_LINK attributes set or unset. If the CFE_LINK attribute is
4821 * not what it is supposed to be, this proc sets or unsets it as appropriate.
4823 * Returns TRUE if at least one section was modified.
4825 static BOOL ME_UpdateLinkAttribute(ME_TextEditor *editor, int sel_min, int sel_max)
4827 BOOL modified = FALSE;
4828 int cMin, cMax;
4830 if (!editor->AutoURLDetect_bEnable) return FALSE;
4832 if (sel_max == -1) sel_max = ME_GetTextLength(editor);
4835 int beforeURL[2];
4836 int inURL[2];
4837 CHARFORMAT2W link;
4839 if (ME_FindNextURLCandidate(editor, sel_min, sel_max, &cMin, &cMax))
4841 /* Section before candidate is not an URL */
4842 beforeURL[0] = sel_min;
4843 beforeURL[1] = cMin;
4845 if (ME_IsCandidateAnURL(editor, cMin, cMax))
4847 inURL[0] = cMin; inURL[1] = cMax;
4849 else
4851 beforeURL[1] = cMax;
4852 inURL[0] = inURL[1] = -1;
4854 sel_min = cMax;
4856 else
4858 /* No more candidates until end of selection */
4859 beforeURL[0] = sel_min;
4860 beforeURL[1] = sel_max;
4861 inURL[0] = inURL[1] = -1;
4862 sel_min = sel_max;
4865 if (beforeURL[0] < beforeURL[1])
4867 /* CFE_LINK effect should be consistently unset */
4868 link.cbSize = sizeof(link);
4869 ME_GetCharFormat(editor, beforeURL[0], beforeURL[1], &link);
4870 if (!(link.dwMask & CFM_LINK) || (link.dwEffects & CFE_LINK))
4872 /* CFE_LINK must be unset from this range */
4873 memset(&link, 0, sizeof(CHARFORMAT2W));
4874 link.cbSize = sizeof(link);
4875 link.dwMask = CFM_LINK;
4876 link.dwEffects = 0;
4877 ME_SetCharFormat(editor, beforeURL[0], beforeURL[1] - beforeURL[0], &link);
4878 modified = TRUE;
4881 if (inURL[0] < inURL[1])
4883 /* CFE_LINK effect should be consistently set */
4884 link.cbSize = sizeof(link);
4885 ME_GetCharFormat(editor, inURL[0], inURL[1], &link);
4886 if (!(link.dwMask & CFM_LINK) || !(link.dwEffects & CFE_LINK))
4888 /* CFE_LINK must be set on this range */
4889 memset(&link, 0, sizeof(CHARFORMAT2W));
4890 link.cbSize = sizeof(link);
4891 link.dwMask = CFM_LINK;
4892 link.dwEffects = CFE_LINK;
4893 ME_SetCharFormat(editor, inURL[0], inURL[1] - inURL[0], &link);
4894 modified = TRUE;
4897 } while (sel_min < sel_max);
4898 return modified;