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
25 API implementation status:
27 Messages (ANSI versions not done yet)
28 + EM_AUTOURLDETECT 2.0
39 + EM_FINDTEXT (only FR_DOWN flag implemented)
40 + EM_FINDTEXTEX (only FR_DOWN flag implemented)
44 + EM_GETAUTOURLDETECT 2.0
45 - EM_GETBIDIOPTIONS 3.0
46 - EM_GETCHARFORMAT (partly done)
49 + EM_GETFIRSTVISIBLELINE (can be optimized if needed)
50 - EM_GETIMECOLOR 1.0asian
51 - EM_GETIMECOMPMODE 2.0
52 - EM_GETIMEOPTIONS 1.0asian
54 - EM_GETLANGOPTIONS 2.0
57 + EM_GETLINECOUNT returns number of rows, not of paragraphs
62 + EM_GETPASSWORDCHAR 2.0
63 - EM_GETPUNCTUATION 1.0asian
67 + EM_GETSELTEXT (ANSI&Unicode)
68 + EM_GETSCROLLPOS 3.0 (only Y value valid)
71 + EM_GETTEXTLENGTHEX (GTL_PRECISE unimplemented)
73 ? + EM_GETTEXTRANGE (ANSI&Unicode)
74 - EM_GETTYPOGRAPHYOPTIONS 3.0
77 - EM_GETWORDBREAKPROCEX
78 - EM_GETWORDWRAPMODE 1.0asian
81 + EM_LIMITTEXT (Also called EM_SETLIMITTEXT)
90 + EM_REPLACESEL (proper style?) ANSI&Unicode
94 - EM_SETBIDIOPTIONS 3.0
96 + EM_SETCHARFORMAT (partly done, no ANSI)
98 + EM_SETEVENTMASK (few notifications supported)
100 - EM_SETIMECOLOR 1.0asian
101 - EM_SETIMEOPTIONS 1.0asian
103 - EM_SETLANGOPTIONS 2.0
106 + EM_SETMODIFY (not sure if implementation is correct)
108 + EM_SETOPTIONS (partially implemented)
111 + EM_SETPASSWORDCHAR 2.0
112 - EM_SETPUNCTUATION 1.0asian
113 + EM_SETREADONLY no beep on modification attempt
115 + EM_SETRECTNP (EM_SETRECT without repainting)
117 + EM_SETSCROLLPOS 3.0
119 - EM_SETTARGETDEVICE (partial)
120 + EM_SETTEXTEX 3.0 (proper style?)
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
128 + EM_SHOWSCROLLBAR 2.0
129 + EM_STOPGROUPTYPING 2.0
137 + WM_GETDLGCODE (the current implementation is incomplete)
138 + WM_GETTEXT (ANSI&Unicode)
139 + WM_GETTEXTLENGTH (ANSI version sucks)
142 + WM_SETTEXT (resets undo stack !) (proper style?) ANSI&Unicode
144 - WM_STYLECHANGED (things like read-only flag)
149 * EN_CHANGE (sent from the wrong place)
166 * EN_UPDATE (sent from the wrong place)
174 + ES_DISABLENOSCROLL (scrollbar is always visible)
175 - ES_EX_NOCALLOLEINIT
177 - ES_MULTILINE (currently single line controls aren't supported)
179 - ES_READONLY (I'm not sure if beeping is the proper behaviour)
185 - ES_WANTRETURN (don't know how to do WM_GETDLGCODE part)
192 * RICHED20 TODO (incomplete):
194 * - messages/styles/notifications listed above
195 * - add remaining CHARFORMAT/PARAFORMAT fields
196 * - right/center align should strip spaces from the beginning
197 * - pictures/OLE objects (not just smiling faces that lack API support ;-) )
198 * - COM interface (looks like a major pain in the TODO list)
199 * - calculate heights of pictures (half-done)
200 * - horizontal scrolling (not even started)
201 * - hysteresis during wrapping (related to scrollbars appearing/disappearing)
203 * - how to implement EM_FORMATRANGE and EM_DISPLAYBAND ? (Mission Impossible)
204 * - italic caret with italic fonts
206 * - most notifications aren't sent at all (the most important ones are)
207 * - when should EN_SELCHANGE be sent after text change ? (before/after EN_UPDATE?)
208 * - WM_SETTEXT may use wrong style (but I'm 80% sure it's OK)
209 * - EM_GETCHARFORMAT with SCF_SELECTION may not behave 100% like in original (but very close)
210 * - full justification
213 * - ListBox & ComboBox not implemented
215 * Bugs that are probably fixed, but not so easy to verify:
216 * - EN_UPDATE/EN_CHANGE are handled very incorrectly (should be OK now)
217 * - undo for ME_JoinParagraphs doesn't store paragraph format ? (it does)
218 * - check/fix artificial EOL logic (bCursorAtEnd, hardly logical)
219 * - caret shouldn't be displayed when selection isn't empty
220 * - check refcounting in style management functions (looks perfect now, but no bugs is suspicious)
221 * - undo for setting default format (done, might be buggy)
222 * - styles might be not released properly (looks like they work like charm, but who knows?
229 #define NO_SHLWAPI_STREAM
235 #define STACK_SIZE_DEFAULT 100
236 #define STACK_SIZE_MAX 1000
238 #define TEXT_LIMIT_DEFAULT 32767
240 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
242 static BOOL ME_RegisterEditorClass(HINSTANCE);
243 static BOOL ME_UpdateLinkAttribute(ME_TextEditor *editor, int sel_min, int sel_max);
245 static const WCHAR RichEdit20W[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '2', '0', 'W', 0};
246 static const WCHAR RichEdit50W[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '5', '0', 'W', 0};
247 static const WCHAR REListBox20W[] = {'R','E','L','i','s','t','B','o','x','2','0','W', 0};
248 static const WCHAR REComboBox20W[] = {'R','E','C','o','m','b','o','B','o','x','2','0','W', 0};
249 static HCURSOR hLeft;
252 HANDLE me_heap = NULL;
254 static BOOL ME_ListBoxRegistered = FALSE;
255 static BOOL ME_ComboBoxRegistered = FALSE;
257 static inline int is_version_nt(void)
259 return !(GetVersion() & 0x80000000);
262 static ME_TextBuffer *ME_MakeText(void) {
264 ME_TextBuffer *buf = ALLOC_OBJ(ME_TextBuffer);
266 ME_DisplayItem *p1 = ME_MakeDI(diTextStart);
267 ME_DisplayItem *p2 = ME_MakeDI(diTextEnd);
273 p1->member.para.next_para = p2;
274 p2->member.para.prev_para = p1;
275 p2->member.para.nCharOfs = 0;
279 buf->pCharStyle = NULL;
285 static LRESULT ME_StreamInText(ME_TextEditor *editor, DWORD dwFormat, ME_InStream *stream, ME_Style *style)
287 WCHAR wszText[STREAMIN_BUFFER_SIZE+1];
290 TRACE("%08x %p\n", dwFormat, stream);
297 ME_StreamInFill(stream);
298 if (stream->editstream->dwError)
304 if (!(dwFormat & SF_UNICODE))
306 /* FIXME? this is doomed to fail on true MBCS like UTF-8, luckily they're unlikely to be used as CP_ACP */
307 nWideChars = MultiByteToWideChar(CP_ACP, 0, stream->buffer, stream->dwSize, wszText, STREAMIN_BUFFER_SIZE);
312 nWideChars = stream->dwSize >> 1;
313 pText = (WCHAR *)stream->buffer;
316 ME_InsertTextFromCursor(editor, 0, pText, nWideChars, style);
317 if (stream->dwSize == 0)
321 ME_CommitUndo(editor);
322 ME_UpdateRepaint(editor);
326 static void ME_ApplyBorderProperties(RTF_Info *info,
327 ME_BorderRect *borderRect,
328 RTFBorder *borderDef)
331 ME_Border *pBorders[] = {&borderRect->top,
335 for (i = 0; i < 4; i++)
337 RTFColor *colorDef = info->colorList;
338 pBorders[i]->width = borderDef[i].width;
339 colorNum = borderDef[i].color;
340 while (colorDef && colorDef->rtfCNum != colorNum)
341 colorDef = colorDef->rtfNextColor;
343 pBorders[i]->colorRef = RGB(
344 colorDef->rtfCRed >= 0 ? colorDef->rtfCRed : 0,
345 colorDef->rtfCGreen >= 0 ? colorDef->rtfCGreen : 0,
346 colorDef->rtfCBlue >= 0 ? colorDef->rtfCBlue : 0);
348 pBorders[i]->colorRef = RGB(0, 0, 0);
352 void ME_RTFCharAttrHook(RTF_Info *info)
355 fmt.cbSize = sizeof(fmt);
359 switch(info->rtfMinor)
362 /* FIXME add more flags once they're implemented */
363 fmt.dwMask = CFM_BOLD | CFM_ITALIC | CFM_UNDERLINETYPE | CFM_STRIKEOUT | CFM_COLOR | CFM_BACKCOLOR | CFM_SIZE | CFM_WEIGHT;
364 fmt.dwEffects = CFE_AUTOCOLOR | CFE_AUTOBACKCOLOR;
365 fmt.yHeight = 12*20; /* 12pt */
366 fmt.wWeight = FW_NORMAL;
367 fmt.bUnderlineType = CFU_UNDERLINENONE;
370 fmt.dwMask = CFM_BOLD | CFM_WEIGHT;
371 fmt.dwEffects = info->rtfParam ? CFE_BOLD : 0;
372 fmt.wWeight = info->rtfParam ? FW_BOLD : FW_NORMAL;
375 fmt.dwMask = CFM_ITALIC;
376 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
379 fmt.dwMask = CFM_UNDERLINETYPE;
380 fmt.bUnderlineType = info->rtfParam ? CFU_CF1UNDERLINE : CFU_UNDERLINENONE;
382 case rtfDotUnderline:
383 fmt.dwMask = CFM_UNDERLINETYPE;
384 fmt.bUnderlineType = info->rtfParam ? CFU_UNDERLINEDOTTED : CFU_UNDERLINENONE;
387 fmt.dwMask = CFM_UNDERLINETYPE;
388 fmt.bUnderlineType = info->rtfParam ? CFU_UNDERLINEDOUBLE : CFU_UNDERLINENONE;
390 case rtfWordUnderline:
391 fmt.dwMask = CFM_UNDERLINETYPE;
392 fmt.bUnderlineType = info->rtfParam ? CFU_UNDERLINEWORD : CFU_UNDERLINENONE;
395 fmt.dwMask = CFM_UNDERLINETYPE;
396 fmt.bUnderlineType = CFU_UNDERLINENONE;
399 fmt.dwMask = CFM_STRIKEOUT;
400 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
404 case rtfSubScrShrink:
405 case rtfSuperScrShrink:
407 fmt.dwMask = CFM_SUBSCRIPT|CFM_SUPERSCRIPT;
408 if (info->rtfMinor == rtfSubScrShrink) fmt.dwEffects = CFE_SUBSCRIPT;
409 if (info->rtfMinor == rtfSuperScrShrink) fmt.dwEffects = CFE_SUPERSCRIPT;
410 if (info->rtfMinor == rtfNoSuperSub) fmt.dwEffects = 0;
413 fmt.dwMask = CFM_HIDDEN;
414 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
417 fmt.dwMask = CFM_BACKCOLOR;
419 if (info->rtfParam == 0)
420 fmt.dwEffects = CFE_AUTOBACKCOLOR;
421 else if (info->rtfParam != rtfNoParam)
423 RTFColor *c = RTFGetColor(info, info->rtfParam);
424 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
428 fmt.dwMask = CFM_COLOR;
430 if (info->rtfParam == 0)
431 fmt.dwEffects = CFE_AUTOCOLOR;
432 else if (info->rtfParam != rtfNoParam)
434 RTFColor *c = RTFGetColor(info, info->rtfParam);
436 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
442 if (info->rtfParam != rtfNoParam)
444 RTFFont *f = RTFGetFont(info, info->rtfParam);
447 MultiByteToWideChar(CP_ACP, 0, f->rtfFName, -1, fmt.szFaceName, sizeof(fmt.szFaceName)/sizeof(WCHAR));
448 fmt.szFaceName[sizeof(fmt.szFaceName)/sizeof(WCHAR)-1] = '\0';
449 fmt.bCharSet = f->rtfFCharSet;
450 fmt.dwMask = CFM_FACE | CFM_CHARSET;
451 fmt.bPitchAndFamily = f->rtfFPitch | (f->rtfFFamily << 4);
456 fmt.dwMask = CFM_SIZE;
457 if (info->rtfParam != rtfNoParam)
458 fmt.yHeight = info->rtfParam*10;
463 RTFFlushOutputBuffer(info);
464 /* FIXME too slow ? how come ? */
465 style2 = ME_ApplyStyle(info->style, &fmt);
466 ME_ReleaseStyle(info->style);
467 info->style = style2;
468 info->styleChanged = TRUE;
472 /* FIXME this function doesn't get any information about context of the RTF tag, which is very bad,
473 the same tags mean different things in different contexts */
474 void ME_RTFParAttrHook(RTF_Info *info)
477 fmt.cbSize = sizeof(fmt);
480 switch(info->rtfMinor)
482 case rtfParDef: /* restores default paragraph attributes */
483 if (!info->editor->bEmulateVersion10) /* v4.1 */
484 info->borderType = RTFBorderParaLeft;
485 else /* v1.0 - 3.0 */
486 info->borderType = RTFBorderParaTop;
487 fmt.dwMask = PFM_ALIGNMENT | PFM_BORDER | PFM_LINESPACING | PFM_TABSTOPS |
488 PFM_OFFSET | PFM_RIGHTINDENT | PFM_SPACEAFTER | PFM_SPACEBEFORE |
490 /* TODO: numbering, shading */
491 fmt.wAlignment = PFA_LEFT;
493 fmt.dxOffset = fmt.dxStartIndent = fmt.dxRightIndent = 0;
494 fmt.wBorderWidth = fmt.wBorders = 0;
495 fmt.wBorderSpace = 0;
496 fmt.bLineSpacingRule = 0;
497 fmt.dySpaceBefore = fmt.dySpaceAfter = 0;
498 fmt.dyLineSpacing = 0;
499 if (!info->editor->bEmulateVersion10) /* v4.1 */
501 if (info->tableDef && info->tableDef->tableRowStart &&
502 info->tableDef->tableRowStart->member.para.nFlags & MEPF_ROWEND)
505 ME_DisplayItem *para;
506 /* We are just after a table row. */
507 RTFFlushOutputBuffer(info);
508 cursor = info->editor->pCursors[0];
509 para = ME_GetParagraph(cursor.pRun);
510 if (para == info->tableDef->tableRowStart->member.para.next_para
511 && !cursor.nOffset && !cursor.pRun->member.run.nCharOfs)
513 /* Since the table row end, no text has been inserted, and the \intbl
514 * control word has not be used. We can confirm that we are not in a
517 info->tableDef->tableRowStart = NULL;
518 info->canInheritInTbl = FALSE;
521 } else { /* v1.0 - v3.0 */
522 fmt.dwMask |= PFM_TABLE;
523 fmt.wEffects &= ~PFE_TABLE;
527 if (!info->editor->bEmulateVersion10) /* v4.1 */
529 while (info->rtfParam > info->nestingLevel) {
530 RTFTable *tableDef = ALLOC_OBJ(RTFTable);
531 ZeroMemory(tableDef, sizeof(RTFTable));
532 tableDef->parent = info->tableDef;
533 info->tableDef = tableDef;
535 RTFFlushOutputBuffer(info);
536 if (tableDef->tableRowStart &&
537 tableDef->tableRowStart->member.para.nFlags & MEPF_ROWEND)
539 ME_DisplayItem *para = tableDef->tableRowStart;
540 para = para->member.para.next_para;
541 para = ME_InsertTableRowStartAtParagraph(info->editor, para);
542 tableDef->tableRowStart = para;
546 cursor = info->editor->pCursors[0];
547 if (cursor.nOffset || cursor.pRun->member.run.nCharOfs)
548 ME_InsertTextFromCursor(info->editor, 0, &endl, 1, info->style);
549 tableDef->tableRowStart = ME_InsertTableRowStartFromCursor(info->editor);
552 info->nestingLevel++;
554 info->canInheritInTbl = FALSE;
559 if (!info->editor->bEmulateVersion10) /* v4.1 */
561 if (info->nestingLevel < 1)
566 info->tableDef = ALLOC_OBJ(RTFTable);
567 ZeroMemory(info->tableDef, sizeof(RTFTable));
569 tableDef = info->tableDef;
570 RTFFlushOutputBuffer(info);
571 if (tableDef->tableRowStart &&
572 tableDef->tableRowStart->member.para.nFlags & MEPF_ROWEND)
574 ME_DisplayItem *para = tableDef->tableRowStart;
575 para = para->member.para.next_para;
576 para = ME_InsertTableRowStartAtParagraph(info->editor, para);
577 tableDef->tableRowStart = para;
581 cursor = info->editor->pCursors[0];
582 if (cursor.nOffset || cursor.pRun->member.run.nCharOfs)
583 ME_InsertTextFromCursor(info->editor, 0, &endl, 1, info->style);
584 tableDef->tableRowStart = ME_InsertTableRowStartFromCursor(info->editor);
586 info->nestingLevel = 1;
587 info->canInheritInTbl = TRUE;
590 } else { /* v1.0 - v3.0 */
591 fmt.dwMask |= PFM_TABLE;
592 fmt.wEffects |= PFE_TABLE;
597 ME_GetSelectionParaFormat(info->editor, &fmt);
598 fmt.dwMask = PFM_STARTINDENT | PFM_OFFSET;
599 fmt.dxStartIndent += fmt.dxOffset + info->rtfParam;
600 fmt.dxOffset = -info->rtfParam;
603 ME_GetSelectionParaFormat(info->editor, &fmt);
604 fmt.dwMask = PFM_STARTINDENT;
605 fmt.dxStartIndent = info->rtfParam - fmt.dxOffset;
608 fmt.dwMask = PFM_RIGHTINDENT;
609 fmt.dxRightIndent = info->rtfParam;
613 fmt.dwMask = PFM_ALIGNMENT;
614 fmt.wAlignment = PFA_LEFT;
617 fmt.dwMask = PFM_ALIGNMENT;
618 fmt.wAlignment = PFA_RIGHT;
621 fmt.dwMask = PFM_ALIGNMENT;
622 fmt.wAlignment = PFA_CENTER;
625 ME_GetSelectionParaFormat(info->editor, &fmt);
626 if (!(fmt.dwMask & PFM_TABSTOPS))
630 if (fmt.cTabCount < MAX_TAB_STOPS && info->rtfParam < 0x1000000)
631 fmt.rgxTabs[fmt.cTabCount++] = info->rtfParam;
632 fmt.dwMask = PFM_TABSTOPS;
635 fmt.dwMask = PFM_KEEP;
636 fmt.wEffects = PFE_KEEP;
638 case rtfNoWidowControl:
639 fmt.dwMask = PFM_NOWIDOWCONTROL;
640 fmt.wEffects = PFE_NOWIDOWCONTROL;
643 fmt.dwMask = PFM_KEEPNEXT;
644 fmt.wEffects = PFE_KEEPNEXT;
647 fmt.dwMask = PFM_SPACEAFTER;
648 fmt.dySpaceAfter = info->rtfParam;
651 fmt.dwMask = PFM_SPACEBEFORE;
652 fmt.dySpaceBefore = info->rtfParam;
654 case rtfSpaceBetween:
655 fmt.dwMask = PFM_LINESPACING;
656 if ((int)info->rtfParam > 0)
658 fmt.dyLineSpacing = info->rtfParam;
659 fmt.bLineSpacingRule = 3;
663 fmt.dyLineSpacing = info->rtfParam;
664 fmt.bLineSpacingRule = 4;
666 case rtfSpaceMultiply:
667 fmt.dwMask = PFM_LINESPACING;
668 fmt.dyLineSpacing = info->rtfParam * 20;
669 fmt.bLineSpacingRule = 5;
672 fmt.dwMask = PFM_NUMBERING;
673 fmt.wNumbering = PFN_BULLET;
676 fmt.dwMask = PFM_NUMBERING;
677 fmt.wNumbering = 2; /* FIXME: MSDN says it's not used ?? */
679 case rtfParNumDecimal:
680 fmt.dwMask = PFM_NUMBERING;
681 fmt.wNumbering = 2; /* FIXME: MSDN says it's not used ?? */
683 case rtfParNumIndent:
684 fmt.dwMask = PFM_NUMBERINGTAB;
685 fmt.wNumberingTab = info->rtfParam;
687 case rtfParNumStartAt:
688 fmt.dwMask = PFM_NUMBERINGSTART;
689 fmt.wNumberingStart = info->rtfParam;
692 info->borderType = RTFBorderParaLeft;
693 ME_GetSelectionParaFormat(info->editor, &fmt);
694 if (!(fmt.dwMask & PFM_BORDER))
696 fmt.wBorderSpace = 0;
697 fmt.wBorderWidth = 1;
701 fmt.dwMask = PFM_BORDER;
704 info->borderType = RTFBorderParaRight;
705 ME_GetSelectionParaFormat(info->editor, &fmt);
706 if (!(fmt.dwMask & PFM_BORDER))
708 fmt.wBorderSpace = 0;
709 fmt.wBorderWidth = 1;
713 fmt.dwMask = PFM_BORDER;
716 info->borderType = RTFBorderParaTop;
717 ME_GetSelectionParaFormat(info->editor, &fmt);
718 if (!(fmt.dwMask & PFM_BORDER))
720 fmt.wBorderSpace = 0;
721 fmt.wBorderWidth = 1;
725 fmt.dwMask = PFM_BORDER;
727 case rtfBorderBottom:
728 info->borderType = RTFBorderParaBottom;
729 ME_GetSelectionParaFormat(info->editor, &fmt);
730 if (!(fmt.dwMask & PFM_BORDER))
732 fmt.wBorderSpace = 0;
733 fmt.wBorderWidth = 1;
737 fmt.dwMask = PFM_BORDER;
739 case rtfBorderSingle:
740 ME_GetSelectionParaFormat(info->editor, &fmt);
741 /* we assume that borders have been created before (RTF spec) */
742 fmt.wBorders &= ~0x700;
743 fmt.wBorders |= 1 << 8;
744 fmt.dwMask = PFM_BORDER;
747 ME_GetSelectionParaFormat(info->editor, &fmt);
748 /* we assume that borders have been created before (RTF spec) */
749 fmt.wBorders &= ~0x700;
750 fmt.wBorders |= 2 << 8;
751 fmt.dwMask = PFM_BORDER;
753 case rtfBorderShadow:
754 ME_GetSelectionParaFormat(info->editor, &fmt);
755 /* we assume that borders have been created before (RTF spec) */
756 fmt.wBorders &= ~0x700;
757 fmt.wBorders |= 10 << 8;
758 fmt.dwMask = PFM_BORDER;
760 case rtfBorderDouble:
761 ME_GetSelectionParaFormat(info->editor, &fmt);
762 /* we assume that borders have been created before (RTF spec) */
763 fmt.wBorders &= ~0x700;
764 fmt.wBorders |= 7 << 8;
765 fmt.dwMask = PFM_BORDER;
768 ME_GetSelectionParaFormat(info->editor, &fmt);
769 /* we assume that borders have been created before (RTF spec) */
770 fmt.wBorders &= ~0x700;
771 fmt.wBorders |= 11 << 8;
772 fmt.dwMask = PFM_BORDER;
776 int borderSide = info->borderType & RTFBorderSideMask;
777 RTFTable *tableDef = info->tableDef;
778 ME_GetSelectionParaFormat(info->editor, &fmt);
779 /* we assume that borders have been created before (RTF spec) */
780 fmt.wBorderWidth |= ((info->rtfParam / 15) & 7) << 8;
781 if ((info->borderType & RTFBorderTypeMask) == RTFBorderTypeCell)
784 if (!tableDef || tableDef->numCellsDefined >= MAX_TABLE_CELLS)
786 border = &tableDef->cells[tableDef->numCellsDefined].border[borderSide];
787 border->width = info->rtfParam;
790 fmt.dwMask = PFM_BORDER;
794 ME_GetSelectionParaFormat(info->editor, &fmt);
795 /* we assume that borders have been created before (RTF spec) */
796 fmt.wBorderSpace = info->rtfParam;
797 fmt.dwMask = PFM_BORDER;
801 RTFTable *tableDef = info->tableDef;
802 int borderSide = info->borderType & RTFBorderSideMask;
803 int borderType = info->borderType & RTFBorderTypeMask;
806 case RTFBorderTypePara:
807 if (!info->editor->bEmulateVersion10) /* v4.1 */
809 /* v1.0 - 3.0 treat paragraph and row borders the same. */
810 case RTFBorderTypeRow:
812 tableDef->border[borderSide].color = info->rtfParam;
815 case RTFBorderTypeCell:
816 if (tableDef && tableDef->numCellsDefined < MAX_TABLE_CELLS) {
817 tableDef->cells[tableDef->numCellsDefined].border[borderSide].color = info->rtfParam;
825 RTFFlushOutputBuffer(info);
826 /* FIXME too slow ? how come ?*/
827 ME_SetSelectionParaFormat(info->editor, &fmt);
831 void ME_RTFTblAttrHook(RTF_Info *info)
833 switch (info->rtfMinor)
837 if (!info->editor->bEmulateVersion10) /* v4.1 */
838 info->borderType = 0; /* Not sure */
839 else /* v1.0 - 3.0 */
840 info->borderType = RTFBorderRowTop;
841 if (!info->tableDef) {
842 info->tableDef = ME_MakeTableDef(info->editor);
844 ME_InitTableDef(info->editor, info->tableDef);
853 info->tableDef = ME_MakeTableDef(info->editor);
855 cellNum = info->tableDef->numCellsDefined;
856 if (cellNum >= MAX_TABLE_CELLS)
858 info->tableDef->cells[cellNum].rightBoundary = info->rtfParam;
859 if (cellNum < MAX_TAB_STOPS) {
860 /* Tab stops were used to store cell positions before v4.1 but v4.1
861 * still seems to set the tabstops without using them. */
862 ME_DisplayItem *para = ME_GetParagraph(info->editor->pCursors[0].pRun);
863 PARAFORMAT2 *pFmt = para->member.para.pFmt;
864 pFmt->rgxTabs[cellNum] &= ~0x00FFFFFF;
865 pFmt->rgxTabs[cellNum] = 0x00FFFFFF & info->rtfParam;
867 info->tableDef->numCellsDefined++;
871 info->borderType = RTFBorderRowTop;
874 info->borderType = RTFBorderRowLeft;
876 case rtfRowBordBottom:
877 info->borderType = RTFBorderRowBottom;
879 case rtfRowBordRight:
880 info->borderType = RTFBorderRowRight;
883 info->borderType = RTFBorderCellTop;
885 case rtfCellBordLeft:
886 info->borderType = RTFBorderCellLeft;
888 case rtfCellBordBottom:
889 info->borderType = RTFBorderCellBottom;
891 case rtfCellBordRight:
892 info->borderType = RTFBorderCellRight;
896 info->tableDef->gapH = info->rtfParam;
900 info->tableDef->leftEdge = info->rtfParam;
905 void ME_RTFSpecialCharHook(RTF_Info *info)
907 RTFTable *tableDef = info->tableDef;
908 switch (info->rtfMinor)
911 if (info->editor->bEmulateVersion10) /* v1.0 - v3.0 */
913 /* else fall through since v4.1 treats rtfNestCell and rtfCell the same */
917 RTFFlushOutputBuffer(info);
918 if (!info->editor->bEmulateVersion10) { /* v4.1 */
919 if (tableDef->tableRowStart)
921 if (!info->nestingLevel &&
922 tableDef->tableRowStart->member.para.nFlags & MEPF_ROWEND)
924 ME_DisplayItem *para = tableDef->tableRowStart;
925 para = para->member.para.next_para;
926 para = ME_InsertTableRowStartAtParagraph(info->editor, para);
927 tableDef->tableRowStart = para;
928 info->nestingLevel = 1;
930 ME_InsertTableCellFromCursor(info->editor);
932 } else { /* v1.0 - v3.0 */
933 ME_DisplayItem *para = ME_GetParagraph(info->editor->pCursors[0].pRun);
934 PARAFORMAT2 *pFmt = para->member.para.pFmt;
935 if (pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE &&
936 tableDef->numCellsInserted < tableDef->numCellsDefined)
939 ME_InsertTextFromCursor(info->editor, 0, &tab, 1, info->style);
940 tableDef->numCellsInserted++;
945 if (info->editor->bEmulateVersion10) /* v1.0 - v3.0 */
947 /* else fall through since v4.1 treats rtfNestRow and rtfRow the same */
950 ME_DisplayItem *para, *cell, *run;
955 RTFFlushOutputBuffer(info);
956 if (!info->editor->bEmulateVersion10) { /* v4.1 */
957 if (!tableDef->tableRowStart)
959 if (!info->nestingLevel &&
960 tableDef->tableRowStart->member.para.nFlags & MEPF_ROWEND)
962 para = tableDef->tableRowStart;
963 para = para->member.para.next_para;
964 para = ME_InsertTableRowStartAtParagraph(info->editor, para);
965 tableDef->tableRowStart = para;
966 info->nestingLevel++;
968 para = tableDef->tableRowStart;
969 cell = ME_FindItemFwd(para, diCell);
970 assert(cell && !cell->member.cell.prev_cell);
971 if (tableDef->numCellsDefined < 1)
973 /* 2000 twips appears to be the cell size that native richedit uses
974 * when no cell sizes are specified. */
975 const int defaultCellSize = 2000;
976 int nRightBoundary = defaultCellSize;
977 cell->member.cell.nRightBoundary = nRightBoundary;
978 while (cell->member.cell.next_cell) {
979 cell = cell->member.cell.next_cell;
980 nRightBoundary += defaultCellSize;
981 cell->member.cell.nRightBoundary = nRightBoundary;
983 para = ME_InsertTableCellFromCursor(info->editor);
984 cell = para->member.para.pCell;
985 cell->member.cell.nRightBoundary = nRightBoundary;
987 for (i = 0; i < tableDef->numCellsDefined; i++)
989 RTFCell *cellDef = &tableDef->cells[i];
990 cell->member.cell.nRightBoundary = cellDef->rightBoundary;
991 ME_ApplyBorderProperties(info, &cell->member.cell.border,
993 cell = cell->member.cell.next_cell;
996 para = ME_InsertTableCellFromCursor(info->editor);
997 cell = para->member.para.pCell;
1000 /* Cell for table row delimiter is empty */
1001 cell->member.cell.nRightBoundary = tableDef->cells[i-1].rightBoundary;
1004 run = ME_FindItemFwd(cell, diRun);
1005 if (info->editor->pCursors[0].pRun != run ||
1006 info->editor->pCursors[0].nOffset)
1009 /* Delete inserted cells that aren't defined. */
1010 info->editor->pCursors[1].pRun = run;
1011 info->editor->pCursors[1].nOffset = 0;
1012 nOfs = ME_GetCursorOfs(info->editor, 1);
1013 nChars = ME_GetCursorOfs(info->editor, 0) - nOfs;
1014 ME_InternalDeleteText(info->editor, nOfs, nChars, TRUE);
1017 para = ME_InsertTableRowEndFromCursor(info->editor);
1018 para->member.para.pFmt->dxOffset = abs(info->tableDef->gapH);
1019 para->member.para.pFmt->dxStartIndent = info->tableDef->leftEdge;
1020 ME_ApplyBorderProperties(info, ¶->member.para.border,
1022 info->nestingLevel--;
1023 if (!info->nestingLevel)
1025 if (info->canInheritInTbl) {
1026 tableDef->tableRowStart = para;
1028 while (info->tableDef) {
1029 tableDef = info->tableDef;
1030 info->tableDef = tableDef->parent;
1031 heap_free(tableDef);
1035 info->tableDef = tableDef->parent;
1036 heap_free(tableDef);
1038 } else { /* v1.0 - v3.0 */
1040 ME_DisplayItem *para = ME_GetParagraph(info->editor->pCursors[0].pRun);
1041 PARAFORMAT2 *pFmt = para->member.para.pFmt;
1042 pFmt->dxOffset = info->tableDef->gapH;
1043 pFmt->dxStartIndent = info->tableDef->leftEdge;
1045 para = ME_GetParagraph(info->editor->pCursors[0].pRun);
1046 ME_ApplyBorderProperties(info, ¶->member.para.border,
1048 while (tableDef->numCellsInserted < tableDef->numCellsDefined)
1051 ME_InsertTextFromCursor(info->editor, 0, &tab, 1, info->style);
1052 tableDef->numCellsInserted++;
1054 pFmt->cTabCount = min(tableDef->numCellsDefined, MAX_TAB_STOPS);
1055 if (!tableDef->numCellsDefined)
1056 pFmt->wEffects &= ~PFE_TABLE;
1057 ME_InsertTextFromCursor(info->editor, 0, &endl, 1, info->style);
1058 tableDef->numCellsInserted = 0;
1064 if (info->editor->bEmulateVersion10) { /* v1.0 - 3.0 */
1065 ME_DisplayItem *para;
1067 RTFFlushOutputBuffer(info);
1068 para = ME_GetParagraph(info->editor->pCursors[0].pRun);
1069 pFmt = para->member.para.pFmt;
1070 if (pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE)
1072 /* rtfPar is treated like a space within a table. */
1073 info->rtfClass = rtfText;
1074 info->rtfMajor = ' ';
1076 else if (info->rtfMinor == rtfPar && tableDef)
1077 tableDef->numCellsInserted = 0;
1083 static BOOL ME_RTFInsertOleObject(RTF_Info *info, HENHMETAFILE hemf, HBITMAP hbmp,
1086 LPOLEOBJECT lpObject = NULL;
1087 LPSTORAGE lpStorage = NULL;
1088 LPOLECLIENTSITE lpClientSite = NULL;
1089 LPDATAOBJECT lpDataObject = NULL;
1090 LPOLECACHE lpOleCache = NULL;
1099 stgm.tymed = TYMED_ENHMF;
1100 stgm.u.hEnhMetaFile = hemf;
1101 fm.cfFormat = CF_ENHMETAFILE;
1105 stgm.tymed = TYMED_GDI;
1106 stgm.u.hBitmap = hbmp;
1107 fm.cfFormat = CF_BITMAP;
1109 stgm.pUnkForRelease = NULL;
1112 fm.dwAspect = DVASPECT_CONTENT;
1114 fm.tymed = stgm.tymed;
1116 if (!info->lpRichEditOle)
1118 CreateIRichEditOle(info->editor, (VOID**)&info->lpRichEditOle);
1121 if (OleCreateDefaultHandler(&CLSID_NULL, NULL, &IID_IOleObject, (void**)&lpObject) == S_OK &&
1123 /* FIXME: enable it when rich-edit properly implements this method */
1124 IRichEditOle_GetClientSite(info->lpRichEditOle, &lpClientSite) == S_OK &&
1125 IOleObject_SetClientSite(lpObject, lpClientSite) == S_OK &&
1127 IOleObject_GetUserClassID(lpObject, &clsid) == S_OK &&
1128 IOleObject_QueryInterface(lpObject, &IID_IOleCache, (void**)&lpOleCache) == S_OK &&
1129 IOleCache_Cache(lpOleCache, &fm, 0, &conn) == S_OK &&
1130 IOleObject_QueryInterface(lpObject, &IID_IDataObject, (void**)&lpDataObject) == S_OK &&
1131 IDataObject_SetData(lpDataObject, &fm, &stgm, TRUE) == S_OK)
1135 reobject.cbStruct = sizeof(reobject);
1136 reobject.cp = REO_CP_SELECTION;
1137 reobject.clsid = clsid;
1138 reobject.poleobj = lpObject;
1139 reobject.pstg = lpStorage;
1140 reobject.polesite = lpClientSite;
1141 /* convert from twips to .01 mm */
1142 reobject.sizel.cx = MulDiv(sz->cx, 254, 144);
1143 reobject.sizel.cy = MulDiv(sz->cy, 254, 144);
1144 reobject.dvaspect = DVASPECT_CONTENT;
1145 reobject.dwFlags = 0; /* FIXME */
1146 reobject.dwUser = 0;
1148 /* FIXME: could be simpler */
1149 ret = IRichEditOle_InsertObject(info->lpRichEditOle, &reobject) == S_OK;
1152 if (lpObject) IOleObject_Release(lpObject);
1153 if (lpClientSite) IOleClientSite_Release(lpClientSite);
1154 if (lpStorage) IStorage_Release(lpStorage);
1155 if (lpDataObject) IDataObject_Release(lpDataObject);
1156 if (lpOleCache) IOleCache_Release(lpOleCache);
1161 static void ME_RTFReadPictGroup(RTF_Info *info)
1164 BYTE* buffer = NULL;
1165 unsigned bufsz, bufidx;
1171 enum gfxkind {gfx_unknown = 0, gfx_enhmetafile, gfx_metafile, gfx_dib} gfx = gfx_unknown;
1174 if (info->rtfClass == rtfEOF)
1177 /* fetch picture type */
1178 if (RTFCheckMM (info, rtfPictAttr, rtfWinMetafile))
1180 mfp.mm = info->rtfParam;
1183 else if (RTFCheckMM (info, rtfPictAttr, rtfDevIndBitmap))
1185 if (info->rtfParam != 0) FIXME("dibitmap should be 0 (%d)\n", info->rtfParam);
1188 else if (RTFCheckMM (info, rtfPictAttr, rtfEmfBlip))
1190 gfx = gfx_enhmetafile;
1194 FIXME("%d %d\n", info->rtfMajor, info->rtfMinor);
1198 /* fetch picture attributes */
1202 if (info->rtfClass == rtfEOF)
1204 if (info->rtfClass == rtfText)
1206 if (!RTFCheckCM (info, rtfControl, rtfPictAttr))
1208 ERR("Expected picture attribute (%d %d)\n",
1209 info->rtfClass, info->rtfMajor);
1212 else if (RTFCheckMM (info, rtfPictAttr, rtfPicWid))
1214 if (gfx == gfx_metafile) mfp.xExt = info->rtfParam;
1216 else if (RTFCheckMM (info, rtfPictAttr, rtfPicHt))
1218 if (gfx == gfx_metafile) mfp.yExt = info->rtfParam;
1220 else if (RTFCheckMM (info, rtfPictAttr, rtfPicGoalWid))
1221 sz.cx = info->rtfParam;
1222 else if (RTFCheckMM (info, rtfPictAttr, rtfPicGoalHt))
1223 sz.cy = info->rtfParam;
1225 FIXME("Non supported attribute: %d %d %d\n", info->rtfClass, info->rtfMajor, info->rtfMinor);
1227 /* fetch picture data */
1230 buffer = HeapAlloc(GetProcessHeap(), 0, bufsz);
1231 val = info->rtfMajor;
1232 for (flip = TRUE;; flip = !flip)
1235 if (info->rtfClass == rtfEOF)
1237 HeapFree(GetProcessHeap(), 0, buffer);
1238 return; /* Warn ?? */
1240 if (RTFCheckCM(info, rtfGroup, rtfEndGroup))
1242 if (info->rtfClass != rtfText) goto skip_group;
1245 if (bufidx >= bufsz &&
1246 !(buffer = HeapReAlloc(GetProcessHeap(), 0, buffer, bufsz += 1024)))
1248 buffer[bufidx++] = RTFCharToHex(val) * 16 + RTFCharToHex(info->rtfMajor);
1251 val = info->rtfMajor;
1253 if (flip) FIXME("wrong hex string\n");
1257 case gfx_enhmetafile:
1258 if ((hemf = SetEnhMetaFileBits(bufidx, buffer)))
1259 ME_RTFInsertOleObject(info, hemf, NULL, &sz);
1262 if ((hemf = SetWinMetaFileBits(bufidx, buffer, NULL, &mfp)))
1263 ME_RTFInsertOleObject(info, hemf, NULL, &sz);
1267 BITMAPINFO* bi = (BITMAPINFO*)buffer;
1269 unsigned nc = bi->bmiHeader.biClrUsed;
1271 /* not quite right, especially for bitfields type of compression */
1272 if (!nc && bi->bmiHeader.biBitCount <= 8)
1273 nc = 1 << bi->bmiHeader.biBitCount;
1274 if ((hbmp = CreateDIBitmap(hdc, &bi->bmiHeader,
1275 CBM_INIT, (char*)(bi + 1) + nc * sizeof(RGBQUAD),
1276 bi, DIB_RGB_COLORS)))
1277 ME_RTFInsertOleObject(info, NULL, hbmp, &sz);
1284 HeapFree(GetProcessHeap(), 0, buffer);
1285 RTFRouteToken (info); /* feed "}" back to router */
1288 HeapFree(GetProcessHeap(), 0, buffer);
1290 RTFRouteToken(info); /* feed "}" back to router */
1293 /* for now, lookup the \result part and use it, whatever the object */
1294 static void ME_RTFReadObjectGroup(RTF_Info *info)
1299 if (info->rtfClass == rtfEOF)
1301 if (RTFCheckCM(info, rtfGroup, rtfEndGroup))
1303 if (RTFCheckCM(info, rtfGroup, rtfBeginGroup))
1306 if (info->rtfClass == rtfEOF)
1308 if (RTFCheckCMM(info, rtfControl, rtfDestination, rtfObjResult))
1312 while (RTFGetToken (info) != rtfEOF)
1314 if (info->rtfClass == rtfGroup)
1316 if (info->rtfMajor == rtfBeginGroup) level++;
1317 else if (info->rtfMajor == rtfEndGroup && --level < 0) break;
1319 RTFRouteToken(info);
1322 else RTFSkipGroup(info);
1325 if (!RTFCheckCM (info, rtfControl, rtfObjAttr))
1327 FIXME("Non supported attribute: %d %d %d\n", info->rtfClass, info->rtfMajor, info->rtfMinor);
1331 RTFRouteToken(info); /* feed "}" back to router */
1334 static void ME_RTFReadHook(RTF_Info *info)
1336 switch(info->rtfClass)
1339 switch(info->rtfMajor)
1342 if (info->stackTop < maxStack) {
1343 info->stack[info->stackTop].fmt = info->style->fmt;
1344 info->stack[info->stackTop].codePage = info->codePage;
1345 info->stack[info->stackTop].unicodeLength = info->unicodeLength;
1348 info->styleChanged = FALSE;
1353 RTFFlushOutputBuffer(info);
1355 if (info->stackTop<=0) {
1356 info->rtfClass = rtfEOF;
1359 assert(info->stackTop >= 0);
1360 if (info->styleChanged)
1362 /* FIXME too slow ? how come ? */
1363 s = ME_ApplyStyle(info->style, &info->stack[info->stackTop].fmt);
1364 ME_ReleaseStyle(info->style);
1366 info->codePage = info->stack[info->stackTop].codePage;
1367 info->unicodeLength = info->stack[info->stackTop].unicodeLength;
1377 ME_StreamInFill(ME_InStream *stream)
1379 stream->editstream->dwError = stream->editstream->pfnCallback(stream->editstream->dwCookie,
1380 (BYTE *)stream->buffer,
1381 sizeof(stream->buffer),
1382 (LONG *)&stream->dwSize);
1386 static LRESULT ME_StreamIn(ME_TextEditor *editor, DWORD format, EDITSTREAM *stream, BOOL stripLastCR)
1390 int from, to, to2, nUndoMode;
1391 int nEventMask = editor->nEventMask;
1392 ME_InStream inStream;
1393 BOOL invalidRTF = FALSE;
1395 TRACE("stream==%p hWnd==%p format==0x%X\n", stream, editor->hWnd, format);
1396 editor->nEventMask = 0;
1398 ME_GetSelection(editor, &from, &to);
1399 if ((format & SFF_SELECTION) && (editor->mode & TM_RICHTEXT)) {
1400 style = ME_GetSelectionInsertStyle(editor);
1402 ME_InternalDeleteText(editor, from, to-from, FALSE);
1404 /* Don't insert text at the end of the table row */
1405 if (!editor->bEmulateVersion10) { /* v4.1 */
1406 ME_DisplayItem *para = ME_GetParagraph(editor->pCursors->pRun);
1407 if (para->member.para.nFlags & MEPF_ROWEND)
1409 para = para->member.para.next_para;
1410 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
1411 editor->pCursors[0].nOffset = 0;
1413 if (para->member.para.nFlags & MEPF_ROWSTART)
1415 para = para->member.para.next_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))
1427 ME_DisplayItem *para_item;
1428 style = editor->pBuffer->pDefaultStyle;
1429 ME_AddRefStyle(style);
1430 ME_SetSelection(editor, 0, 0);
1431 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor), FALSE);
1433 ME_ClearTempStyle(editor);
1435 para_item = ME_GetParagraph(editor->pCursors[0].pRun);
1436 ME_SetDefaultParaFormat(para_item->member.para.pFmt);
1440 /* Back up undo mode to a local variable */
1441 nUndoMode = editor->nUndoMode;
1443 /* Only create an undo if SFF_SELECTION is set */
1444 if (!(format & SFF_SELECTION))
1445 editor->nUndoMode = umIgnore;
1447 inStream.editstream = stream;
1448 inStream.editstream->dwError = 0;
1449 inStream.dwSize = 0;
1450 inStream.dwUsed = 0;
1452 if (format & SF_RTF)
1454 /* Check if it's really RTF, and if it is not, use plain text */
1455 ME_StreamInFill(&inStream);
1456 if (!inStream.editstream->dwError)
1458 if ((!editor->bEmulateVersion10 && strncmp(inStream.buffer, "{\\rtf", 5) && strncmp(inStream.buffer, "{\\urtf", 6))
1459 || (editor->bEmulateVersion10 && *inStream.buffer != '{'))
1462 inStream.editstream->dwError = -16;
1467 if (!invalidRTF && !inStream.editstream->dwError)
1469 if (format & SF_RTF) {
1470 /* setup the RTF parser */
1471 memset(&parser, 0, sizeof parser);
1472 RTFSetEditStream(&parser, &inStream);
1473 parser.rtfFormat = format&(SF_TEXT|SF_RTF);
1474 parser.editor = editor;
1475 parser.style = style;
1476 WriterInit(&parser);
1478 RTFSetReadHook(&parser, ME_RTFReadHook);
1479 RTFSetDestinationCallback(&parser, rtfPict, ME_RTFReadPictGroup);
1480 RTFSetDestinationCallback(&parser, rtfObject, ME_RTFReadObjectGroup);
1481 if (!parser.editor->bEmulateVersion10) /* v4.1 */
1483 RTFSetDestinationCallback(&parser, rtfNoNestTables, RTFSkipGroup);
1484 RTFSetDestinationCallback(&parser, rtfNestTableProps, RTFReadGroup);
1488 /* do the parsing */
1490 RTFFlushOutputBuffer(&parser);
1491 if (!editor->bEmulateVersion10) { /* v4.1 */
1492 if (parser.tableDef && parser.tableDef->tableRowStart &&
1493 (parser.nestingLevel > 0 || parser.canInheritInTbl))
1495 /* Delete any incomplete table row at the end of the rich text. */
1497 ME_DisplayItem *pCell;
1498 ME_DisplayItem *para;
1500 parser.rtfMinor = rtfRow;
1501 /* Complete the table row before deleting it.
1502 * By doing it this way we will have the current paragraph format set
1503 * properly to reflect that is not in the complete table, and undo items
1504 * will be added for this change to the current paragraph format. */
1505 if (parser.nestingLevel > 0)
1507 while (parser.nestingLevel > 1)
1508 ME_RTFSpecialCharHook(&parser); /* Decrements nestingLevel */
1509 para = parser.tableDef->tableRowStart;
1510 ME_RTFSpecialCharHook(&parser);
1512 para = parser.tableDef->tableRowStart;
1513 ME_RTFSpecialCharHook(&parser);
1514 assert(para->member.para.nFlags & MEPF_ROWEND);
1515 para = para->member.para.next_para;
1517 pCell = para->member.para.pCell;
1519 editor->pCursors[1].pRun = ME_FindItemFwd(para, diRun);
1520 editor->pCursors[1].nOffset = 0;
1521 nOfs = ME_GetCursorOfs(editor, 1);
1522 nChars = ME_GetCursorOfs(editor, 0) - nOfs;
1523 ME_InternalDeleteText(editor, nOfs, nChars, TRUE);
1524 if (parser.tableDef)
1525 parser.tableDef->tableRowStart = NULL;
1528 ME_CheckTablesForCorruption(editor);
1529 RTFDestroy(&parser);
1530 if (parser.lpRichEditOle)
1531 IRichEditOle_Release(parser.lpRichEditOle);
1533 if (!inStream.editstream->dwError && parser.stackTop > 0)
1534 inStream.editstream->dwError = HRESULT_FROM_WIN32(ERROR_HANDLE_EOF);
1536 /* Remove last line break, as mandated by tests. This is not affected by
1537 CR/LF counters, since RTF streaming presents only \para tokens, which
1538 are converted according to the standard rules: \r for 2.0, \r\n for 1.0
1542 ME_GetSelection(editor, &newfrom, &newto);
1543 if (newto > to + (editor->bEmulateVersion10 ? 1 : 0)) {
1544 WCHAR lastchar[3] = {'\0', '\0'};
1545 int linebreakSize = editor->bEmulateVersion10 ? 2 : 1;
1547 ME_GetTextW(editor, lastchar, newto - linebreakSize, linebreakSize, 0);
1548 if (lastchar[0] == '\r' && (lastchar[1] == '\n' || lastchar[1] == '\0')) {
1549 ME_InternalDeleteText(editor, newto - linebreakSize, linebreakSize, FALSE);
1554 style = parser.style;
1556 else if (format & SF_TEXT)
1557 ME_StreamInText(editor, format, &inStream, style);
1559 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
1560 ME_GetSelection(editor, &to, &to2);
1561 /* put the cursor at the top */
1562 if (!(format & SFF_SELECTION))
1563 ME_SetSelection(editor, 0, 0);
1566 /* Restore saved undo mode */
1567 editor->nUndoMode = nUndoMode;
1569 /* even if we didn't add an undo, we need to commit anything on the stack */
1570 ME_CommitUndo(editor);
1572 /* If SFF_SELECTION isn't set, delete any undos from before we started too */
1573 if (!(format & SFF_SELECTION))
1574 ME_EmptyUndoStack(editor);
1576 ME_ReleaseStyle(style);
1577 editor->nEventMask = nEventMask;
1578 ME_UpdateRepaint(editor);
1579 if (!(format & SFF_SELECTION)) {
1580 ME_ClearTempStyle(editor);
1582 HideCaret(editor->hWnd);
1583 ME_MoveCaret(editor);
1584 ShowCaret(editor->hWnd);
1585 ME_SendSelChange(editor);
1586 ME_SendRequestResize(editor, FALSE);
1592 typedef struct tagME_RTFStringStreamStruct
1597 } ME_RTFStringStreamStruct;
1599 static DWORD CALLBACK ME_ReadFromRTFString(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1601 ME_RTFStringStreamStruct *pStruct = (ME_RTFStringStreamStruct *)dwCookie;
1604 count = min(cb, pStruct->length - pStruct->pos);
1605 memmove(lpBuff, pStruct->string + pStruct->pos, count);
1606 pStruct->pos += count;
1612 ME_StreamInRTFString(ME_TextEditor *editor, BOOL selection, char *string)
1615 ME_RTFStringStreamStruct data;
1617 data.string = string;
1618 data.length = strlen(string);
1620 es.dwCookie = (DWORD_PTR)&data;
1621 es.pfnCallback = ME_ReadFromRTFString;
1622 ME_StreamIn(editor, SF_RTF | (selection ? SFF_SELECTION : 0), &es, FALSE);
1627 ME_FindItemAtOffset(ME_TextEditor *editor, ME_DIType nItemType, int nOffset, int *nItemOffset)
1629 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
1632 while (item && item->member.para.next_para->member.para.nCharOfs <= nOffset)
1633 item = ME_FindItemFwd(item, diParagraph);
1638 nOffset -= item->member.para.nCharOfs;
1639 if (nItemType == diParagraph) {
1641 *nItemOffset = nOffset;
1646 item = ME_FindItemFwd(item, diRun);
1647 runLength = ME_StrLen(item->member.run.strText);
1648 if (item->member.run.nFlags & MERF_ENDPARA)
1649 runLength = item->member.run.nCR + item->member.run.nLF;
1650 } while (item && (item->member.run.nCharOfs + runLength <= nOffset));
1652 nOffset -= item->member.run.nCharOfs;
1654 /* Special case: nOffset may not point exactly at the division between the
1655 \r and the \n in 1.0 emulation. If such a case happens, it is sent
1656 into the next run, if one exists
1658 if ( item->member.run.nFlags & MERF_ENDPARA
1659 && nOffset == item->member.run.nCR
1660 && item->member.run.nLF > 0) {
1661 ME_DisplayItem *nextItem;
1662 nextItem = ME_FindItemFwd(item, diRun);
1669 *nItemOffset = nOffset;
1676 ME_FindText(ME_TextEditor *editor, DWORD flags, const CHARRANGE *chrg, const WCHAR *text, CHARRANGE *chrgText)
1678 const int nLen = lstrlenW(text);
1679 const int nTextLen = ME_GetTextLength(editor);
1682 ME_DisplayItem *item;
1683 ME_DisplayItem *para;
1684 WCHAR wLastChar = ' ';
1686 TRACE("flags==0x%08x, chrg->cpMin==%d, chrg->cpMax==%d text==%s\n",
1687 flags, chrg->cpMin, chrg->cpMax, debugstr_w(text));
1689 if (flags & ~(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD))
1690 FIXME("Flags 0x%08x not implemented\n",
1691 flags & ~(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD));
1694 if (chrg->cpMax == -1)
1697 nMax = chrg->cpMax > nTextLen ? nTextLen : chrg->cpMax;
1699 /* In 1.0 emulation, if cpMax reaches end of text, add the FR_DOWN flag */
1700 if (editor->bEmulateVersion10 && nMax == nTextLen)
1705 /* In 1.0 emulation, cpMin must always be no greater than cpMax */
1706 if (editor->bEmulateVersion10 && nMax < nMin)
1710 chrgText->cpMin = -1;
1711 chrgText->cpMax = -1;
1716 /* when searching up, if cpMin < cpMax, then instead of searching
1717 * on [cpMin,cpMax], we search on [0,cpMin], otherwise, search on
1718 * [cpMax, cpMin]. The exception is when cpMax is -1, in which
1719 * case, it is always bigger than cpMin.
1721 if (!editor->bEmulateVersion10 && !(flags & FR_DOWN))
1725 nMax = nMin > nTextLen ? nTextLen : nMin;
1726 if (nMin < nSwap || chrg->cpMax == -1)
1732 if (!nLen || nMin < 0 || nMax < 0 || nMax < nMin)
1735 chrgText->cpMin = chrgText->cpMax = -1;
1739 if (flags & FR_DOWN) /* Forward search */
1741 /* If possible, find the character before where the search starts */
1742 if ((flags & FR_WHOLEWORD) && nMin)
1745 item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
1749 chrgText->cpMin = chrgText->cpMax = -1;
1752 wLastChar = item->member.run.strText->szData[nStart];
1756 item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
1760 chrgText->cpMin = chrgText->cpMax = -1;
1764 para = ME_GetParagraph(item);
1766 && para->member.para.nCharOfs + item->member.run.nCharOfs + nStart + nLen <= nMax)
1768 ME_DisplayItem *pCurItem = item;
1769 int nCurStart = nStart;
1772 while (pCurItem && ME_CharCompare(pCurItem->member.run.strText->szData[nCurStart + nMatched], text[nMatched], (flags & FR_MATCHCASE)))
1774 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
1778 if (nMatched == nLen)
1780 ME_DisplayItem *pNextItem = pCurItem;
1781 int nNextStart = nCurStart;
1784 /* Check to see if next character is a whitespace */
1785 if (flags & FR_WHOLEWORD)
1787 if (nCurStart + nMatched == ME_StrLen(pCurItem->member.run.strText))
1789 pNextItem = ME_FindItemFwd(pCurItem, diRun);
1790 nNextStart = -nMatched;
1794 wNextChar = pNextItem->member.run.strText->szData[nNextStart + nMatched];
1798 if (isalnumW(wNextChar))
1802 nStart += para->member.para.nCharOfs + item->member.run.nCharOfs;
1805 chrgText->cpMin = nStart;
1806 chrgText->cpMax = nStart + nLen;
1808 TRACE("found at %d-%d\n", nStart, nStart + nLen);
1811 if (nCurStart + nMatched == ME_StrLen(pCurItem->member.run.strText))
1813 pCurItem = ME_FindItemFwd(pCurItem, diRun);
1814 para = ME_GetParagraph(pCurItem);
1815 nCurStart = -nMatched;
1819 wLastChar = pCurItem->member.run.strText->szData[nCurStart + nMatched];
1824 if (nStart == ME_StrLen(item->member.run.strText))
1826 item = ME_FindItemFwd(item, diRun);
1827 para = ME_GetParagraph(item);
1832 else /* Backward search */
1834 /* If possible, find the character after where the search ends */
1835 if ((flags & FR_WHOLEWORD) && nMax < nTextLen - 1)
1838 item = ME_FindItemAtOffset(editor, diRun, nEnd, &nEnd);
1842 chrgText->cpMin = chrgText->cpMax = -1;
1845 wLastChar = item->member.run.strText->szData[nEnd];
1849 item = ME_FindItemAtOffset(editor, diRun, nEnd, &nEnd);
1853 chrgText->cpMin = chrgText->cpMax = -1;
1857 para = ME_GetParagraph(item);
1860 && para->member.para.nCharOfs + item->member.run.nCharOfs + nEnd - nLen >= nMin)
1862 ME_DisplayItem *pCurItem = item;
1866 if (nCurEnd - nMatched == 0)
1868 pCurItem = ME_FindItemBack(pCurItem, diRun);
1869 para = ME_GetParagraph(pCurItem);
1870 nCurEnd = ME_StrLen(pCurItem->member.run.strText) + nMatched;
1873 while (pCurItem && ME_CharCompare(pCurItem->member.run.strText->szData[nCurEnd - nMatched - 1], text[nLen - nMatched - 1], (flags & FR_MATCHCASE)))
1875 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
1879 if (nMatched == nLen)
1881 ME_DisplayItem *pPrevItem = pCurItem;
1882 int nPrevEnd = nCurEnd;
1885 /* Check to see if previous character is a whitespace */
1886 if (flags & FR_WHOLEWORD)
1888 if (nPrevEnd - nMatched == 0)
1890 pPrevItem = ME_FindItemBack(pCurItem, diRun);
1892 nPrevEnd = ME_StrLen(pPrevItem->member.run.strText) + nMatched;
1896 wPrevChar = pPrevItem->member.run.strText->szData[nPrevEnd - nMatched - 1];
1900 if (isalnumW(wPrevChar))
1904 nStart = para->member.para.nCharOfs + pCurItem->member.run.nCharOfs + nCurEnd - nMatched;
1907 chrgText->cpMin = nStart;
1908 chrgText->cpMax = nStart + nLen;
1910 TRACE("found at %d-%d\n", nStart, nStart + nLen);
1913 if (nCurEnd - nMatched == 0)
1915 pCurItem = ME_FindItemBack(pCurItem, diRun);
1916 /* Don't care about pCurItem becoming NULL here; it's already taken
1917 * care of in the exterior loop condition */
1918 para = ME_GetParagraph(pCurItem);
1919 nCurEnd = ME_StrLen(pCurItem->member.run.strText) + nMatched;
1923 wLastChar = pCurItem->member.run.strText->szData[nCurEnd - nMatched - 1];
1930 item = ME_FindItemBack(item, diRun);
1931 para = ME_GetParagraph(item);
1932 nEnd = ME_StrLen(item->member.run.strText);
1936 TRACE("not found\n");
1938 chrgText->cpMin = chrgText->cpMax = -1;
1942 static int ME_GetTextEx(ME_TextEditor *editor, GETTEXTEX *ex, LPARAM pText)
1944 int nStart, nCount; /* in chars */
1946 if (ex->flags & ~(GT_SELECTION | GT_USECRLF))
1947 FIXME("GETTEXTEX flags 0x%08x not supported\n", ex->flags & ~(GT_SELECTION | GT_USECRLF));
1949 if (ex->flags & GT_SELECTION)
1951 ME_GetSelection(editor, &nStart, &nCount);
1957 nCount = 0x7fffffff;
1959 if (ex->codepage == 1200)
1961 nCount = min(nCount, ex->cb / sizeof(WCHAR) - 1);
1962 return ME_GetTextW(editor, (LPWSTR)pText, nStart, nCount, ex->flags & GT_USECRLF);
1966 /* potentially each char may be a CR, why calculate the exact value with O(N) when
1967 we can just take a bigger buffer? :)
1968 The above assumption still holds with CR/LF counters, since CR->CRLF expansion
1969 occurs only in richedit 2.0 mode, in which line breaks have only one CR
1971 int crlfmul = (ex->flags & GT_USECRLF) ? 2 : 1;
1973 DWORD buflen = ex->cb;
1977 nCount = min(nCount, ex->cb - 1);
1978 buffer = heap_alloc((crlfmul*nCount + 1) * sizeof(WCHAR));
1980 buflen = ME_GetTextW(editor, buffer, nStart, nCount, ex->flags & GT_USECRLF);
1981 rc = WideCharToMultiByte(ex->codepage, flags, buffer, buflen+1,
1982 (LPSTR)pText, ex->cb, ex->lpDefaultChar, ex->lpUsedDefChar);
1983 if (rc) rc--; /* do not count 0 terminator */
1990 static int ME_GetTextRange(ME_TextEditor *editor, TEXTRANGEW *rng, BOOL unicode)
1993 return ME_GetTextW(editor, rng->lpstrText, rng->chrg.cpMin,
1994 rng->chrg.cpMax-rng->chrg.cpMin, 0);
1997 int nLen = rng->chrg.cpMax-rng->chrg.cpMin;
1998 WCHAR *p = ALLOC_N_OBJ(WCHAR, nLen+1);
1999 int nChars = ME_GetTextW(editor, p, rng->chrg.cpMin, nLen, 0);
2000 /* FIXME this is a potential security hole (buffer overrun)
2001 if you know more about wchar->mbyte conversion please explain
2003 WideCharToMultiByte(CP_ACP, 0, p, nChars+1, (char *)rng->lpstrText, nLen+1, NULL, NULL);
2009 typedef struct tagME_GlobalDestStruct
2013 } ME_GlobalDestStruct;
2015 static DWORD CALLBACK ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
2017 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
2022 pDest = (WORD *)lpBuff;
2023 pSrc = GlobalLock(pData->hData);
2024 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
2025 pDest[i] = pSrc[pData->nLength+i];
2027 pData->nLength += i;
2029 GlobalUnlock(pData->hData);
2033 static DWORD CALLBACK ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
2035 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
2040 pSrc = GlobalLock(pData->hData);
2041 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
2042 pDest[i] = pSrc[pData->nLength+i];
2044 pData->nLength += i;
2046 GlobalUnlock(pData->hData);
2050 static BOOL ME_Paste(ME_TextEditor *editor)
2054 ME_GlobalDestStruct gds;
2055 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
2058 if (IsClipboardFormatAvailable(nRTFFormat))
2059 cf = nRTFFormat, dwFormat = SF_RTF;
2060 else if (IsClipboardFormatAvailable(CF_UNICODETEXT))
2061 cf = CF_UNICODETEXT, dwFormat = SF_TEXT|SF_UNICODE;
2065 if (!OpenClipboard(editor->hWnd))
2067 gds.hData = GetClipboardData(cf);
2069 es.dwCookie = (DWORD_PTR)&gds;
2070 es.pfnCallback = dwFormat == SF_RTF ? ME_ReadFromHGLOBALRTF : ME_ReadFromHGLOBALUnicode;
2071 ME_StreamIn(editor, dwFormat|SFF_SELECTION, &es, FALSE);
2077 static BOOL ME_Copy(ME_TextEditor *editor, CHARRANGE *range)
2079 LPDATAOBJECT dataObj = NULL;
2082 if (editor->cPasswordMask)
2083 return FALSE; /* Copying or Cutting masked text isn't allowed */
2085 if(editor->lpOleCallback)
2086 hr = IRichEditOleCallback_GetClipboardData(editor->lpOleCallback, range, RECO_COPY, &dataObj);
2087 if(FAILED(hr) || !dataObj)
2088 hr = ME_GetDataObject(editor, range, &dataObj);
2090 hr = OleSetClipboard(dataObj);
2091 IDataObject_Release(dataObj);
2093 return SUCCEEDED(hr) != 0;
2096 /* helper to send a msg filter notification */
2098 ME_FilterEvent(ME_TextEditor *editor, UINT msg, WPARAM* wParam, LPARAM* lParam)
2102 msgf.nmhdr.hwndFrom = editor->hWnd;
2103 msgf.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
2104 msgf.nmhdr.code = EN_MSGFILTER;
2107 msgf.wParam = *wParam;
2108 msgf.lParam = *lParam;
2109 if (SendMessageW(GetParent(editor->hWnd), WM_NOTIFY, msgf.nmhdr.idFrom, (LPARAM)&msgf))
2111 *wParam = msgf.wParam;
2112 *lParam = msgf.lParam;
2113 msgf.wParam = *wParam;
2118 static void ME_UpdateSelectionLinkAttribute(ME_TextEditor *editor)
2120 ME_DisplayItem * startPara, * endPara;
2121 ME_DisplayItem * item;
2125 ME_GetSelection(editor, &from, &to);
2126 if (from > to) from ^= to, to ^=from, from ^= to;
2127 startPara = NULL; endPara = NULL;
2129 /* Find paragraph previous to the one that contains start cursor */
2130 item = ME_FindItemAtOffset(editor, diRun, from, &dummy);
2132 startPara = ME_FindItemBack(item, diParagraph);
2133 item = ME_FindItemBack(startPara, diParagraph);
2134 if (item) startPara = item;
2137 /* Find paragraph that contains end cursor */
2138 item = ME_FindItemAtOffset(editor, diRun, to, &dummy);
2140 endPara = ME_FindItemFwd(item, diParagraph);
2143 if (startPara && endPara) {
2144 ME_UpdateLinkAttribute(editor,
2145 startPara->member.para.nCharOfs,
2146 endPara->member.para.nCharOfs);
2147 } else if (startPara) {
2148 ME_UpdateLinkAttribute(editor,
2149 startPara->member.para.nCharOfs,
2155 ME_KeyDown(ME_TextEditor *editor, WORD nKey)
2157 BOOL ctrl_is_down = GetKeyState(VK_CONTROL) & 0x8000;
2158 BOOL shift_is_down = GetKeyState(VK_SHIFT) & 0x8000;
2159 DWORD dwStyle = GetWindowLongW(editor->hWnd, GWL_STYLE);
2161 if (nKey != VK_SHIFT && nKey != VK_CONTROL && nKey != VK_MENU)
2162 editor->nSelectionType = stPosition;
2170 editor->nUDArrowX = -1;
2176 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
2177 ME_ArrowKey(editor, nKey, shift_is_down, ctrl_is_down);
2181 editor->nUDArrowX = -1;
2182 /* FIXME backspace and delete aren't the same, they act different wrt paragraph style of the merged paragraph */
2183 if (dwStyle & ES_READONLY)
2185 if (ME_IsSelection(editor))
2187 ME_DeleteSelection(editor);
2188 ME_CommitUndo(editor);
2190 else if (nKey == VK_DELETE)
2192 /* Delete stops group typing.
2193 * (See MSDN remarks on EM_STOPGROUPTYPING message) */
2194 ME_DeleteTextAtCursor(editor, 1, 1);
2195 ME_CommitUndo(editor);
2197 else if (ME_ArrowKey(editor, VK_LEFT, FALSE, FALSE))
2199 BOOL bDeletionSucceeded;
2200 /* Backspace can be grouped for a single undo */
2201 ME_ContinueCoalescingTransaction(editor);
2202 bDeletionSucceeded = ME_DeleteTextAtCursor(editor, 1, 1);
2203 if (!bDeletionSucceeded && !editor->bEmulateVersion10) { /* v4.1 */
2204 /* Deletion was prevented so the cursor is moved back to where it was.
2205 * (e.g. this happens when trying to delete cell boundaries)
2207 ME_ArrowKey(editor, VK_RIGHT, FALSE, FALSE);
2209 ME_CommitCoalescingUndo(editor);
2213 ME_MoveCursorFromTableRowStartParagraph(editor);
2214 ME_UpdateSelectionLinkAttribute(editor);
2215 ME_UpdateRepaint(editor);
2216 ME_SendRequestResize(editor, FALSE);
2219 if (dwStyle & ES_MULTILINE)
2221 ME_Cursor cursor = editor->pCursors[0];
2222 ME_DisplayItem *para = ME_GetParagraph(cursor.pRun);
2224 const WCHAR endl = '\r';
2227 if (dwStyle & ES_READONLY) {
2228 MessageBeep(MB_ICONERROR);
2232 ME_GetSelection(editor, &from, &to);
2233 if (editor->nTextLimit > ME_GetTextLength(editor) - (to-from))
2235 if (!editor->bEmulateVersion10) { /* v4.1 */
2236 if (para->member.para.nFlags & MEPF_ROWEND) {
2237 /* Add a new table row after this row. */
2238 para = ME_AppendTableRow(editor, para);
2239 para = para->member.para.next_para;
2240 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2241 editor->pCursors[0].nOffset = 0;
2242 editor->pCursors[1] = editor->pCursors[0];
2243 ME_CommitUndo(editor);
2244 ME_CheckTablesForCorruption(editor);
2245 ME_UpdateRepaint(editor);
2248 else if (para == ME_GetParagraph(editor->pCursors[1].pRun) &&
2249 cursor.nOffset + cursor.pRun->member.run.nCharOfs == 0 &&
2250 para->member.para.prev_para->member.para.nFlags & MEPF_ROWSTART &&
2251 !para->member.para.prev_para->member.para.nCharOfs)
2253 /* Insert a newline before the table. */
2254 para = para->member.para.prev_para;
2255 para->member.para.nFlags &= ~MEPF_ROWSTART;
2256 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2257 editor->pCursors[1] = editor->pCursors[0];
2258 ME_InsertTextFromCursor(editor, 0, &endl, 1,
2259 editor->pCursors[0].pRun->member.run.style);
2260 para = editor->pBuffer->pFirst->member.para.next_para;
2261 ME_SetDefaultParaFormat(para->member.para.pFmt);
2262 para->member.para.nFlags = MEPF_REWRAP;
2263 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2264 editor->pCursors[1] = editor->pCursors[0];
2265 para->member.para.next_para->member.para.nFlags |= MEPF_ROWSTART;
2266 ME_CommitCoalescingUndo(editor);
2267 ME_CheckTablesForCorruption(editor);
2268 ME_UpdateRepaint(editor);
2271 } else { /* v1.0 - 3.0 */
2272 ME_DisplayItem *para = ME_GetParagraph(cursor.pRun);
2273 if (ME_IsInTable(para))
2275 if (cursor.pRun->member.run.nFlags & MERF_ENDPARA)
2278 ME_ContinueCoalescingTransaction(editor);
2279 para = ME_AppendTableRow(editor, para);
2280 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2281 editor->pCursors[0].nOffset = 0;
2282 editor->pCursors[1] = editor->pCursors[0];
2283 ME_CommitCoalescingUndo(editor);
2284 ME_UpdateRepaint(editor);
2288 ME_ContinueCoalescingTransaction(editor);
2289 if (cursor.pRun->member.run.nCharOfs + cursor.nOffset == 0 &&
2290 !ME_IsInTable(para->member.para.prev_para))
2292 /* Insert newline before table */
2293 cursor.pRun = ME_FindItemBack(para, diRun);
2295 editor->pCursors[0].pRun = cursor.pRun;
2296 editor->pCursors[0].nOffset = 0;
2297 editor->pCursors[1] = editor->pCursors[0];
2298 ME_InsertTextFromCursor(editor, 0, &endl, 1,
2299 editor->pCursors[0].pRun->member.run.style);
2301 editor->pCursors[1] = editor->pCursors[0];
2302 para = ME_AppendTableRow(editor, para);
2303 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2304 editor->pCursors[0].nOffset = 0;
2305 editor->pCursors[1] = editor->pCursors[0];
2307 ME_CommitCoalescingUndo(editor);
2308 ME_UpdateRepaint(editor);
2314 style = ME_GetInsertStyle(editor, 0);
2315 ME_SaveTempStyle(editor);
2316 ME_ContinueCoalescingTransaction(editor);
2318 ME_InsertEndRowFromCursor(editor, 0);
2320 ME_InsertTextFromCursor(editor, 0, &endl, 1, style);
2321 ME_ReleaseStyle(style);
2322 ME_CommitCoalescingUndo(editor);
2325 if (editor->AutoURLDetect_bEnable)
2326 ME_UpdateSelectionLinkAttribute(editor);
2328 ME_UpdateRepaint(editor);
2336 ME_SetSelection(editor, 0, -1);
2342 return ME_Paste(editor);
2351 ME_GetSelection(editor, &range.cpMin, &range.cpMax);
2352 result = ME_Copy(editor, &range);
2353 if (result && nKey == 'X')
2355 ME_InternalDeleteText(editor, range.cpMin, range.cpMax-range.cpMin, FALSE);
2356 ME_CommitUndo(editor);
2357 ME_UpdateRepaint(editor);
2378 if (nKey != VK_SHIFT && nKey != VK_CONTROL && nKey && nKey != VK_MENU)
2379 editor->nUDArrowX = -1;
2386 chf.cbSize = sizeof(chf);
2388 ME_GetSelectionCharFormat(editor, &chf);
2389 ME_DumpStyleToBuf(&chf, buf);
2390 MessageBoxA(NULL, buf, "Style dump", MB_OK);
2394 ME_CheckCharOffsets(editor);
2401 static LRESULT ME_Char(ME_TextEditor *editor, WPARAM charCode,
2402 LPARAM flags, BOOL unicode)
2407 wstr = (WCHAR)charCode;
2410 CHAR charA = charCode;
2411 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &wstr, 1);
2414 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY) {
2415 MessageBeep(MB_ICONERROR);
2416 return 0; /* FIXME really 0 ? */
2419 if ((unsigned)wstr >= ' ' || wstr == '\t')
2421 ME_Cursor cursor = editor->pCursors[0];
2422 ME_DisplayItem *para = ME_GetParagraph(cursor.pRun);
2424 BOOL ctrl_is_down = GetKeyState(VK_CONTROL) & 0x8000;
2425 ME_GetSelection(editor, &from, &to);
2427 /* v4.1 allows tabs to be inserted with ctrl key down */
2428 !(ctrl_is_down && !editor->bEmulateVersion10))
2430 ME_DisplayItem *para;
2431 BOOL bSelectedRow = FALSE;
2433 para = ME_GetParagraph(cursor.pRun);
2434 if (ME_IsSelection(editor) &&
2435 cursor.pRun->member.run.nCharOfs + cursor.nOffset == 0 &&
2436 to == ME_GetCursorOfs(editor, 0) &&
2437 para->member.para.prev_para->type == diParagraph)
2439 para = para->member.para.prev_para;
2440 bSelectedRow = TRUE;
2442 if (ME_IsInTable(para))
2444 ME_TabPressedInTable(editor, bSelectedRow);
2445 ME_CommitUndo(editor);
2448 } else if (!editor->bEmulateVersion10) { /* v4.1 */
2449 if (para->member.para.nFlags & MEPF_ROWEND) {
2451 para = para->member.para.next_para;
2452 if (para->member.para.nFlags & MEPF_ROWSTART)
2453 para = para->member.para.next_para;
2454 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2455 editor->pCursors[0].nOffset = 0;
2456 editor->pCursors[1] = editor->pCursors[0];
2459 } else { /* v1.0 - 3.0 */
2460 if (ME_IsInTable(cursor.pRun) &&
2461 cursor.pRun->member.run.nFlags & MERF_ENDPARA &&
2464 /* Text should not be inserted at the end of the table. */
2469 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
2470 /* WM_CHAR is restricted to nTextLimit */
2471 if(editor->nTextLimit > ME_GetTextLength(editor) - (to-from))
2473 ME_Style *style = ME_GetInsertStyle(editor, 0);
2474 ME_SaveTempStyle(editor);
2475 ME_ContinueCoalescingTransaction(editor);
2476 ME_InsertTextFromCursor(editor, 0, &wstr, 1, style);
2477 ME_ReleaseStyle(style);
2478 ME_CommitCoalescingUndo(editor);
2482 if (editor->AutoURLDetect_bEnable) ME_UpdateSelectionLinkAttribute(editor);
2484 ME_UpdateRepaint(editor);
2489 /* Process the message and calculate the new click count.
2491 * returns: The click count if it is mouse down event, else returns 0. */
2492 static int ME_CalculateClickCount(ME_TextEditor *editor, UINT msg, WPARAM wParam,
2495 static int clickNum = 0;
2496 if (msg < WM_MOUSEFIRST || msg > WM_MOUSELAST)
2499 if ((msg == WM_LBUTTONDBLCLK) ||
2500 (msg == WM_RBUTTONDBLCLK) ||
2501 (msg == WM_MBUTTONDBLCLK) ||
2502 (msg == WM_XBUTTONDBLCLK))
2504 msg -= (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN);
2507 if ((msg == WM_LBUTTONDOWN) ||
2508 (msg == WM_RBUTTONDOWN) ||
2509 (msg == WM_MBUTTONDOWN) ||
2510 (msg == WM_XBUTTONDOWN))
2512 static MSG prevClickMsg;
2514 /* Compare the editor instead of the hwnd so that the this
2515 * can still be done for windowless richedit controls. */
2516 clickMsg.hwnd = (HWND)editor;
2517 clickMsg.message = msg;
2518 clickMsg.wParam = wParam;
2519 clickMsg.lParam = lParam;
2520 clickMsg.time = GetMessageTime();
2521 clickMsg.pt.x = (short)LOWORD(lParam);
2522 clickMsg.pt.y = (short)HIWORD(lParam);
2523 if ((clickNum != 0) &&
2524 (clickMsg.message == prevClickMsg.message) &&
2525 (clickMsg.hwnd == prevClickMsg.hwnd) &&
2526 (clickMsg.wParam == prevClickMsg.wParam) &&
2527 (clickMsg.time - prevClickMsg.time < GetDoubleClickTime()) &&
2528 (abs(clickMsg.pt.x - prevClickMsg.pt.x) < GetSystemMetrics(SM_CXDOUBLECLK)/2) &&
2529 (abs(clickMsg.pt.y - prevClickMsg.pt.y) < GetSystemMetrics(SM_CYDOUBLECLK)/2))
2535 prevClickMsg = clickMsg;
2542 static BOOL ME_SetCursor(ME_TextEditor *editor)
2548 DWORD messagePos = GetMessagePos();
2549 pt.x = (short)LOWORD(messagePos);
2550 pt.y = (short)HIWORD(messagePos);
2552 sbi.cbSize = sizeof(sbi);
2553 GetScrollBarInfo(editor->hWnd, OBJID_HSCROLL, &sbi);
2554 if (!(sbi.rgstate[0] & (STATE_SYSTEM_INVISIBLE|STATE_SYSTEM_OFFSCREEN)) &&
2555 PtInRect(&sbi.rcScrollBar, pt))
2557 SetCursor(LoadCursorW(NULL, (WCHAR*)IDC_ARROW));
2560 sbi.cbSize = sizeof(sbi);
2561 GetScrollBarInfo(editor->hWnd, OBJID_VSCROLL, &sbi);
2562 if (!(sbi.rgstate[0] & (STATE_SYSTEM_INVISIBLE|STATE_SYSTEM_OFFSCREEN)) &&
2563 PtInRect(&sbi.rcScrollBar, pt))
2565 SetCursor(LoadCursorW(NULL, (WCHAR*)IDC_ARROW));
2568 ScreenToClient(editor->hWnd, &pt);
2570 if (editor->nSelectionType == stLine && editor->bMouseCaptured) {
2574 if (!editor->bEmulateVersion10 /* v4.1 */ &&
2575 pt.y < editor->rcFormat.top &&
2576 pt.x < editor->rcFormat.left)
2581 if (pt.y < editor->rcFormat.top || pt.y > editor->rcFormat.bottom)
2583 if (editor->bEmulateVersion10) /* v1.0 - 3.0 */
2584 SetCursor(LoadCursorW(NULL, (WCHAR*)IDC_ARROW));
2586 SetCursor(LoadCursorW(NULL, (WCHAR*)IDC_IBEAM));
2589 if (pt.x < editor->rcFormat.left)
2594 offset = ME_CharFromPos(editor, pt.x, pt.y, &isExact);
2597 if (editor->AutoURLDetect_bEnable)
2601 ME_CursorFromCharOfs(editor, offset, &cursor);
2602 run = &cursor.pRun->member.run;
2603 if (editor->AutoURLDetect_bEnable &&
2604 run->style->fmt.dwMask & CFM_LINK &&
2605 run->style->fmt.dwEffects & CFE_LINK)
2607 SetCursor(LoadCursorW(NULL, (WCHAR*)IDC_HAND));
2611 if (ME_IsSelection(editor))
2613 int selStart, selEnd;
2614 ME_GetSelection(editor, &selStart, &selEnd);
2615 if (selStart <= offset && selEnd >= offset) {
2616 SetCursor(LoadCursorW(NULL, (WCHAR*)IDC_ARROW));
2621 SetCursor(LoadCursorW(NULL, (WCHAR*)IDC_IBEAM));
2625 static void ME_SetDefaultFormatRect(ME_TextEditor *editor)
2627 DWORD exstyle = GetWindowLongW(editor->hWnd, GWL_EXSTYLE);
2629 GetClientRect(editor->hWnd, &editor->rcFormat);
2630 editor->rcFormat.top += (exstyle & WS_EX_CLIENTEDGE ? 1 : 0);
2631 editor->rcFormat.left += 1 + editor->selofs;
2632 editor->rcFormat.right -= 1;
2635 static BOOL ME_ShowContextMenu(ME_TextEditor *editor, int x, int y)
2640 if(!editor->lpOleCallback)
2642 ME_GetSelection(editor, &selrange.cpMin, &selrange.cpMax);
2643 if(selrange.cpMin == selrange.cpMax)
2644 seltype |= SEL_EMPTY;
2647 /* FIXME: Handle objects */
2648 seltype |= SEL_TEXT;
2649 if(selrange.cpMax-selrange.cpMin > 1)
2650 seltype |= SEL_MULTICHAR;
2652 if(SUCCEEDED(IRichEditOleCallback_GetContextMenu(editor->lpOleCallback, seltype, NULL, &selrange, &menu)))
2654 TrackPopupMenu(menu, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, GetParent(editor->hWnd), NULL);
2660 static ME_TextEditor *ME_MakeEditor(HWND hWnd, BOOL bEmulateVersion10)
2662 ME_TextEditor *ed = ALLOC_OBJ(ME_TextEditor);
2665 ed->bEmulateVersion10 = bEmulateVersion10;
2666 ed->pBuffer = ME_MakeText();
2667 ed->nZoomNumerator = ed->nZoomDenominator = 0;
2668 ME_MakeFirstParagraph(ed);
2669 /* The four cursors are for:
2670 * 0 - The position where the caret is shown
2671 * 1 - The anchored end of the selection (for normal selection)
2672 * 2 & 3 - The anchored start and end respectively for word, line,
2673 * or paragraph selection.
2676 ed->pCursors = ALLOC_N_OBJ(ME_Cursor, ed->nCursors);
2677 ed->pCursors[0].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
2678 ed->pCursors[0].nOffset = 0;
2679 ed->pCursors[1].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
2680 ed->pCursors[1].nOffset = 0;
2681 ed->pCursors[2] = ed->pCursors[0];
2682 ed->pCursors[3] = ed->pCursors[1];
2683 ed->nLastTotalLength = ed->nTotalLength = 0;
2686 ed->rgbBackColor = -1;
2687 ed->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
2688 ed->bCaretAtEnd = FALSE;
2690 ed->nModifyStep = 0;
2691 ed->nTextLimit = TEXT_LIMIT_DEFAULT;
2692 ed->pUndoStack = ed->pRedoStack = ed->pUndoStackBottom = NULL;
2693 ed->nUndoStackSize = 0;
2694 ed->nUndoLimit = STACK_SIZE_DEFAULT;
2695 ed->nUndoMode = umAddToUndo;
2696 ed->nParagraphs = 1;
2697 ed->nLastSelStart = ed->nLastSelEnd = 0;
2698 ed->pLastSelStartPara = ed->pLastSelEndPara = ME_FindItemFwd(ed->pBuffer->pFirst, diParagraph);
2699 if (ed->bEmulateVersion10)
2700 ed->bWordWrap = (GetWindowLongW(hWnd, GWL_STYLE) & ES_AUTOHSCROLL) ? FALSE : TRUE;
2702 ed->bWordWrap = (GetWindowLongW(hWnd, GWL_STYLE) & (WS_HSCROLL|ES_AUTOHSCROLL)) ? FALSE : TRUE;
2703 ed->bHideSelection = FALSE;
2704 ed->nInvalidOfs = -1;
2705 ed->pfnWordBreak = NULL;
2706 ed->lpOleCallback = NULL;
2707 ed->mode = TM_RICHTEXT | TM_MULTILEVELUNDO | TM_MULTICODEPAGE;
2708 ed->AutoURLDetect_bEnable = FALSE;
2709 ed->bHaveFocus = FALSE;
2710 ed->bMouseCaptured = FALSE;
2711 for (i=0; i<HFONT_CACHE_SIZE; i++)
2713 ed->pFontCache[i].nRefs = 0;
2714 ed->pFontCache[i].nAge = 0;
2715 ed->pFontCache[i].hFont = NULL;
2718 ME_CheckCharOffsets(ed);
2719 if (GetWindowLongW(hWnd, GWL_STYLE) & ES_SELECTIONBAR)
2720 ed->selofs = SELECTIONBAR_WIDTH;
2723 ed->bDefaultFormatRect = TRUE;
2724 ed->nSelectionType = stPosition;
2726 if (GetWindowLongW(hWnd, GWL_STYLE) & ES_PASSWORD)
2727 ed->cPasswordMask = '*';
2729 ed->cPasswordMask = 0;
2731 ed->notified_cr.cpMin = ed->notified_cr.cpMax = 0;
2733 /* Default vertical scrollbar information */
2734 ed->vert_si.cbSize = sizeof(SCROLLINFO);
2735 ed->vert_si.nMin = 0;
2736 ed->vert_si.nMax = 0;
2737 ed->vert_si.nPage = 0;
2738 ed->vert_si.nPos = 0;
2740 OleInitialize(NULL);
2745 static void ME_DestroyEditor(ME_TextEditor *editor)
2747 ME_DisplayItem *pFirst = editor->pBuffer->pFirst;
2748 ME_DisplayItem *p = pFirst, *pNext = NULL;
2751 ME_ClearTempStyle(editor);
2752 ME_EmptyUndoStack(editor);
2755 ME_DestroyDisplayItem(p);
2758 ME_ReleaseStyle(editor->pBuffer->pDefaultStyle);
2759 for (i=0; i<HFONT_CACHE_SIZE; i++)
2761 if (editor->pFontCache[i].hFont)
2762 DeleteObject(editor->pFontCache[i].hFont);
2764 if (editor->rgbBackColor != -1)
2765 DeleteObject(editor->hbrBackground);
2766 if(editor->lpOleCallback)
2767 IUnknown_Release(editor->lpOleCallback);
2768 SetWindowLongPtrW(editor->hWnd, 0, 0);
2771 FREE_OBJ(editor->pBuffer);
2772 FREE_OBJ(editor->pCursors);
2777 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
2782 case DLL_PROCESS_ATTACH:
2783 DisableThreadLibraryCalls(hinstDLL);
2784 me_heap = HeapCreate (0, 0x10000, 0);
2785 if (!ME_RegisterEditorClass(hinstDLL)) return FALSE;
2786 hLeft = LoadCursorW(hinstDLL, MAKEINTRESOURCEW(OCR_REVERSE));
2790 case DLL_PROCESS_DETACH:
2791 UnregisterClassW(RichEdit20W, 0);
2792 UnregisterClassW(RichEdit50W, 0);
2793 UnregisterClassA("RichEdit20A", 0);
2794 UnregisterClassA("RichEdit50A", 0);
2795 if (ME_ListBoxRegistered)
2796 UnregisterClassW(REListBox20W, 0);
2797 if (ME_ComboBoxRegistered)
2798 UnregisterClassW(REComboBox20W, 0);
2800 HeapDestroy (me_heap);
2808 static const char * const edit_messages[] = {
2837 "EM_SETPASSWORDCHAR",
2838 "EM_EMPTYUNDOBUFFER",
2839 "EM_GETFIRSTVISIBLELINE",
2841 "EM_SETWORDBREAKPROC",
2842 "EM_GETWORDBREAKPROC",
2843 "EM_GETPASSWORDCHAR",
2853 static const char * const richedit_messages[] = {
2858 "EM_EXLINEFROMCHAR",
2864 "EM_GETOLEINTERFACE",
2874 "EM_SETOLECALLBACK",
2876 "EM_SETTARGETDEVICE",
2884 "EM_GETWORDBREAKPROCEX",
2885 "EM_SETWORDBREAKPROCEX",
2887 "EM_UNKNOWN_USER_83",
2892 "EM_STOPGROUPTYPING",
2896 "EM_GETAUTOURLDETECT",
2899 "EM_GETTEXTLENGTHEX",
2902 "EM_UNKNOWN_USER_98",
2903 "EM_UNKNOWN_USER_99",
2904 "EM_SETPUNCTUATION",
2905 "EM_GETPUNCTUATION",
2906 "EM_SETWORDWRAPMODE",
2907 "EM_GETWORDWRAPMODE",
2913 "EM_UNKNOWN_USER_109",
2914 "EM_UNKNOWN_USER_110",
2915 "EM_UNKNOWN_USER_111",
2916 "EM_UNKNOWN_USER_112",
2917 "EM_UNKNOWN_USER_113",
2918 "EM_UNKNOWN_USER_114",
2919 "EM_UNKNOWN_USER_115",
2920 "EM_UNKNOWN_USER_116",
2921 "EM_UNKNOWN_USER_117",
2922 "EM_UNKNOWN_USER_118",
2923 "EM_UNKNOWN_USER_119",
2924 "EM_SETLANGOPTIONS",
2925 "EM_GETLANGOPTIONS",
2926 "EM_GETIMECOMPMODE",
2930 "EM_SETIMEMODEBIAS",
2935 get_msg_name(UINT msg)
2937 if (msg >= EM_GETSEL && msg <= EM_CHARFROMPOS)
2938 return edit_messages[msg - EM_GETSEL];
2939 if (msg >= EM_CANPASTE && msg <= EM_GETIMEMODEBIAS)
2940 return richedit_messages[msg - EM_CANPASTE];
2944 void ME_LinkNotify(ME_TextEditor *editor, UINT msg, WPARAM wParam, LPARAM lParam)
2947 ME_Cursor tmpCursor;
2949 int nCharOfs; /* The start of the clicked text. Absolute character offset */
2954 x = (short)LOWORD(lParam);
2955 y = (short)HIWORD(lParam);
2956 nCharOfs = ME_CharFromPos(editor, x, y, &isExact);
2957 if (!isExact) return;
2959 ME_CursorFromCharOfs(editor, nCharOfs, &tmpCursor);
2960 tmpRun = &tmpCursor.pRun->member.run;
2962 if ((tmpRun->style->fmt.dwMask & CFM_LINK)
2963 && (tmpRun->style->fmt.dwEffects & CFE_LINK))
2964 { /* The clicked run has CFE_LINK set */
2965 info.nmhdr.hwndFrom = editor->hWnd;
2966 info.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
2967 info.nmhdr.code = EN_LINK;
2969 info.wParam = wParam;
2970 info.lParam = lParam;
2971 info.chrg.cpMin = ME_CharOfsFromRunOfs(editor,tmpCursor.pRun,0);
2972 info.chrg.cpMax = info.chrg.cpMin + ME_StrVLen(tmpRun->strText);
2973 SendMessageW(GetParent(editor->hWnd), WM_NOTIFY,info.nmhdr.idFrom, (LPARAM)&info);
2977 #define UNSUPPORTED_MSG(e) \
2979 FIXME(#e ": stub\n"); \
2980 *phresult = S_FALSE; \
2983 /* Handle messages for windowless and windoweded richedit controls.
2985 * The LRESULT that is returned is a return value for window procs,
2986 * and the phresult parameter is the COM return code needed by the
2987 * text services interface. */
2988 static LRESULT ME_HandleMessage(ME_TextEditor *editor, UINT msg, WPARAM wParam,
2989 LPARAM lParam, BOOL unicode, HRESULT* phresult)
2995 UNSUPPORTED_MSG(EM_DISPLAYBAND)
2996 UNSUPPORTED_MSG(EM_FINDWORDBREAK)
2997 UNSUPPORTED_MSG(EM_FMTLINES)
2998 UNSUPPORTED_MSG(EM_FORMATRANGE)
2999 UNSUPPORTED_MSG(EM_GETBIDIOPTIONS)
3000 UNSUPPORTED_MSG(EM_GETEDITSTYLE)
3001 UNSUPPORTED_MSG(EM_GETIMECOMPMODE)
3002 UNSUPPORTED_MSG(EM_GETIMESTATUS)
3003 UNSUPPORTED_MSG(EM_SETIMESTATUS)
3004 UNSUPPORTED_MSG(EM_GETLANGOPTIONS)
3005 /* UNSUPPORTED_MSG(EM_GETOLEINTERFACE) separate stub */
3006 UNSUPPORTED_MSG(EM_GETREDONAME)
3007 UNSUPPORTED_MSG(EM_GETTEXTMODE)
3008 UNSUPPORTED_MSG(EM_GETTYPOGRAPHYOPTIONS)
3009 UNSUPPORTED_MSG(EM_GETUNDONAME)
3010 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX)
3011 UNSUPPORTED_MSG(EM_PASTESPECIAL)
3012 UNSUPPORTED_MSG(EM_SELECTIONTYPE)
3013 UNSUPPORTED_MSG(EM_SETBIDIOPTIONS)
3014 UNSUPPORTED_MSG(EM_SETEDITSTYLE)
3015 UNSUPPORTED_MSG(EM_SETFONTSIZE)
3016 UNSUPPORTED_MSG(EM_SETLANGOPTIONS)
3017 UNSUPPORTED_MSG(EM_SETMARGINS)
3018 UNSUPPORTED_MSG(EM_SETPALETTE)
3019 UNSUPPORTED_MSG(EM_SETTABSTOPS)
3020 UNSUPPORTED_MSG(EM_SETTYPOGRAPHYOPTIONS)
3021 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX)
3022 UNSUPPORTED_MSG(WM_STYLECHANGING)
3023 UNSUPPORTED_MSG(WM_STYLECHANGED)
3025 /* Messages specific to Richedit controls */
3028 return ME_StreamIn(editor, wParam, (EDITSTREAM*)lParam, TRUE);
3030 return ME_StreamOut(editor, wParam, (EDITSTREAM *)lParam);
3033 UINT code = DLGC_WANTCHARS|DLGC_WANTTAB|DLGC_WANTARROWS|DLGC_HASSETSEL;
3034 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_MULTILINE)
3035 code |= DLGC_WANTMESSAGE;
3038 case EM_EMPTYUNDOBUFFER:
3039 ME_EmptyUndoStack(editor);
3043 /* Note: wParam/lParam can be NULL */
3045 PUINT pfrom = wParam ? (PUINT)wParam : &from;
3046 PUINT pto = lParam ? (PUINT)lParam : &to;
3047 ME_GetSelection(editor, (int *)pfrom, (int *)pto);
3048 if ((*pfrom|*pto) & 0xFFFF0000)
3050 return MAKELONG(*pfrom,*pto);
3054 CHARRANGE *pRange = (CHARRANGE *)lParam;
3055 ME_GetSelection(editor, &pRange->cpMin, &pRange->cpMax);
3056 TRACE("EM_EXGETSEL = (%d,%d)\n", pRange->cpMin, pRange->cpMax);
3059 case EM_SETUNDOLIMIT:
3061 if ((int)wParam < 0)
3062 editor->nUndoLimit = STACK_SIZE_DEFAULT;
3064 editor->nUndoLimit = min(wParam, STACK_SIZE_MAX);
3065 /* Setting a max stack size keeps wine from getting killed
3066 for hogging memory. Windows allocates all this memory at once, so
3067 no program would realistically set a value above our maximum. */
3068 return editor->nUndoLimit;
3071 return editor->pUndoStack != NULL;
3073 return editor->pRedoStack != NULL;
3074 case WM_UNDO: /* FIXME: actually not the same */
3076 return ME_Undo(editor);
3078 return ME_Redo(editor);
3081 /* these flags are equivalent to the ES_* counterparts */
3082 DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
3083 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN | ECO_SELECTIONBAR;
3084 DWORD settings = GetWindowLongW(editor->hWnd, GWL_STYLE) & mask;
3090 /* these flags are equivalent to ES_* counterparts
3091 * ECO_READONLY is already implemented in the code, only requires
3092 * setting the bit to work
3094 DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
3095 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN | ECO_SELECTIONBAR;
3096 DWORD raw = GetWindowLongW(editor->hWnd, GWL_STYLE);
3097 DWORD settings = mask & raw;
3098 DWORD oldSettings = settings;
3099 DWORD changedSettings;
3115 SetWindowLongW(editor->hWnd, GWL_STYLE, (raw & ~mask) | (settings & mask));
3117 changedSettings = oldSettings ^ settings;
3119 if (settings & ECO_AUTOWORDSELECTION)
3120 FIXME("ECO_AUTOWORDSELECTION not implemented yet!\n");
3122 if (oldSettings ^ settings) {
3123 if (settings & ECO_SELECTIONBAR) {
3124 editor->selofs = SELECTIONBAR_WIDTH;
3125 editor->rcFormat.left += SELECTIONBAR_WIDTH;
3128 editor->rcFormat.left -= SELECTIONBAR_WIDTH;
3130 ME_WrapMarkedParagraphs(editor);
3133 if (settings & ECO_VERTICAL)
3134 FIXME("ECO_VERTICAL not implemented yet!\n");
3135 if (settings & ECO_AUTOHSCROLL)
3136 FIXME("ECO_AUTOHSCROLL not implemented yet!\n");
3137 if (settings & ECO_AUTOVSCROLL)
3138 FIXME("ECO_AUTOVSCROLL not implemented yet!\n");
3139 if (settings & ECO_NOHIDESEL)
3140 FIXME("ECO_NOHIDESEL not implemented yet!\n");
3141 if (settings & ECO_WANTRETURN)
3142 FIXME("ECO_WANTRETURN not implemented yet!\n");
3148 ME_InvalidateSelection(editor);
3149 ME_SetSelection(editor, wParam, lParam);
3150 ME_InvalidateSelection(editor);
3151 HideCaret(editor->hWnd);
3152 ME_ShowCaret(editor);
3153 ME_SendSelChange(editor);
3156 case EM_SETSCROLLPOS:
3158 POINT *point = (POINT *)lParam;
3159 ME_ScrollAbs(editor, point->y);
3162 case EM_AUTOURLDETECT:
3164 if (wParam==1 || wParam ==0)
3166 editor->AutoURLDetect_bEnable = (BOOL)wParam;
3169 return E_INVALIDARG;
3171 case EM_GETAUTOURLDETECT:
3173 return editor->AutoURLDetect_bEnable;
3178 CHARRANGE range = *(CHARRANGE *)lParam;
3180 TRACE("EM_EXSETSEL (%d,%d)\n", range.cpMin, range.cpMax);
3182 ME_InvalidateSelection(editor);
3183 end = ME_SetSelection(editor, range.cpMin, range.cpMax);
3184 ME_InvalidateSelection(editor);
3185 HideCaret(editor->hWnd);
3186 ME_ShowCaret(editor);
3187 ME_SendSelChange(editor);
3191 case EM_SHOWSCROLLBAR:
3193 ShowScrollBar(editor->hWnd, wParam, lParam);
3199 SETTEXTEX *pStruct = (SETTEXTEX *)wParam;
3203 int oldModify = editor->nModifyStep;
3205 if (!pStruct) return 0;
3207 TRACE("EM_SETTEXTEX - %s, flags %d, cp %d\n",
3208 pStruct->codepage == 1200 ? debugstr_w((LPCWSTR)lParam) : debugstr_a((LPCSTR)lParam),
3209 pStruct->flags, pStruct->codepage);
3211 /* FIXME: make use of pStruct->codepage in the to unicode translation */
3212 wszText = lParam ? ME_ToUnicode(pStruct->codepage == 1200, (void *)lParam) : NULL;
3213 len = wszText ? lstrlenW(wszText) : 0;
3215 if (pStruct->flags & ST_SELECTION) {
3216 ME_GetSelection(editor, &from, &to);
3217 style = ME_GetSelectionInsertStyle(editor);
3218 ME_InternalDeleteText(editor, from, to - from, FALSE);
3219 if (pStruct->codepage != 1200 && lParam &&
3220 (!strncmp((char *)lParam, "{\\rtf", 5) || !strncmp((char *)lParam, "{\\urtf}", 6)))
3221 ME_StreamInRTFString(editor, 1, (char *)lParam);
3222 else ME_InsertTextFromCursor(editor, 0, wszText, len, style);
3223 ME_ReleaseStyle(style);
3225 if (editor->AutoURLDetect_bEnable) ME_UpdateSelectionLinkAttribute(editor);
3228 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor), FALSE);
3229 if (pStruct->codepage != 1200 && lParam &&
3230 (!strncmp((char *)lParam, "{\\rtf", 5) || !strncmp((char *)lParam, "{\\urtf}", 6)))
3231 ME_StreamInRTFString(editor, 0, (char *)lParam);
3232 else ME_InsertTextFromCursor(editor, 0, wszText, len, editor->pBuffer->pDefaultStyle);
3235 if (editor->AutoURLDetect_bEnable) ME_UpdateLinkAttribute(editor, 0, -1);
3237 ME_CommitUndo(editor);
3238 if (!(pStruct->flags & ST_KEEPUNDO))
3240 editor->nModifyStep = oldModify;
3241 ME_EmptyUndoStack(editor);
3243 ME_UpdateRepaint(editor);
3246 case EM_SETBKGNDCOLOR:
3249 if (editor->rgbBackColor != -1) {
3250 DeleteObject(editor->hbrBackground);
3251 lColor = editor->rgbBackColor;
3253 else lColor = GetSysColor(COLOR_WINDOW);
3257 editor->rgbBackColor = -1;
3258 editor->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
3262 editor->rgbBackColor = lParam;
3263 editor->hbrBackground = CreateSolidBrush(editor->rgbBackColor);
3265 InvalidateRect(editor->hWnd, NULL, TRUE);
3266 UpdateWindow(editor->hWnd);
3270 return editor->nModifyStep == 0 ? 0 : -1;
3274 editor->nModifyStep = 1;
3276 editor->nModifyStep = 0;
3280 case EM_SETREADONLY:
3282 long nStyle = GetWindowLongW(editor->hWnd, GWL_STYLE);
3284 nStyle |= ES_READONLY;
3286 nStyle &= ~ES_READONLY;
3287 SetWindowLongW(editor->hWnd, GWL_STYLE, nStyle);
3290 case EM_SETEVENTMASK:
3292 DWORD nOldMask = editor->nEventMask;
3294 editor->nEventMask = lParam;
3297 case EM_GETEVENTMASK:
3298 return editor->nEventMask;
3299 case EM_SETCHARFORMAT:
3301 CHARFORMAT2W buf, *p;
3302 BOOL bRepaint = TRUE;
3303 p = ME_ToCF2W(&buf, (CHARFORMAT2W *)lParam);
3304 if (p == NULL) return 0;
3306 ME_SetDefaultCharFormat(editor, p);
3307 else if (wParam == (SCF_WORD | SCF_SELECTION)) {
3308 FIXME("EM_SETCHARFORMAT: word selection not supported\n");
3310 } else if (wParam == SCF_ALL) {
3311 if (editor->mode & TM_PLAINTEXT)
3312 ME_SetDefaultCharFormat(editor, p);
3314 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), p);
3315 editor->nModifyStep = 1;
3317 } else if (editor->mode & TM_PLAINTEXT) {
3321 ME_GetSelection(editor, &from, &to);
3322 bRepaint = (from != to);
3323 ME_SetSelectionCharFormat(editor, p);
3324 if (from != to) editor->nModifyStep = 1;
3326 ME_CommitUndo(editor);
3329 ME_WrapMarkedParagraphs(editor);
3330 ME_UpdateScrollBar(editor);
3335 case EM_GETCHARFORMAT:
3337 CHARFORMAT2W tmp, *dst = (CHARFORMAT2W *)lParam;
3338 if (dst->cbSize != sizeof(CHARFORMATA) &&
3339 dst->cbSize != sizeof(CHARFORMATW) &&
3340 dst->cbSize != sizeof(CHARFORMAT2A) &&
3341 dst->cbSize != sizeof(CHARFORMAT2W))
3343 tmp.cbSize = sizeof(tmp);
3345 ME_GetDefaultCharFormat(editor, &tmp);
3347 ME_GetSelectionCharFormat(editor, &tmp);
3348 ME_CopyToCFAny(dst, &tmp);
3351 case EM_SETPARAFORMAT:
3353 BOOL result = ME_SetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
3354 ME_WrapMarkedParagraphs(editor);
3355 ME_UpdateScrollBar(editor);
3357 ME_CommitUndo(editor);
3360 case EM_GETPARAFORMAT:
3361 ME_GetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
3362 return ((PARAFORMAT2 *)lParam)->dwMask;
3363 case EM_GETFIRSTVISIBLELINE:
3365 ME_DisplayItem *p = editor->pBuffer->pFirst;
3366 int y = ME_GetYScrollPos(editor);
3371 p = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
3372 if (p->type == diTextEnd)
3374 if (p->type == diParagraph) {
3375 ypara = p->member.para.pt.y;
3378 ystart = ypara + p->member.row.pt.y;
3379 yend = ystart + p->member.row.nHeight;
3387 case EM_HIDESELECTION:
3389 editor->bHideSelection = (wParam != 0);
3390 ME_InvalidateSelection(editor);
3395 ME_ScrollDown(editor, lParam * 8); /* FIXME follow the original */
3396 return TRUE; /* Should return false if a single line richedit control */
3401 ME_GetSelection(editor, &from, &to);
3402 ME_InternalDeleteText(editor, from, to-from, FALSE);
3403 ME_CommitUndo(editor);
3404 ME_UpdateRepaint(editor);
3411 LPWSTR wszText = lParam ? ME_ToUnicode(unicode, (void *)lParam) : NULL;
3412 size_t len = wszText ? lstrlenW(wszText) : 0;
3413 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText));
3415 ME_GetSelection(editor, &from, &to);
3416 style = ME_GetSelectionInsertStyle(editor);
3417 ME_InternalDeleteText(editor, from, to-from, FALSE);
3418 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
3419 ME_ReleaseStyle(style);
3420 /* drop temporary style if line end */
3422 * FIXME question: does abc\n mean: put abc,
3423 * clear temp style, put \n? (would require a change)
3425 if (len>0 && wszText[len-1] == '\n')
3426 ME_ClearTempStyle(editor);
3427 ME_EndToUnicode(unicode, wszText);
3428 ME_CommitUndo(editor);
3429 if (editor->AutoURLDetect_bEnable) ME_UpdateSelectionLinkAttribute(editor);
3431 ME_EmptyUndoStack(editor);
3432 ME_UpdateRepaint(editor);
3435 case EM_SCROLLCARET:
3436 ME_EnsureVisible(editor, editor->pCursors[0].pRun);
3443 BOOL bRepaint = LOWORD(lParam);
3446 wParam = (WPARAM)GetStockObject(SYSTEM_FONT);
3447 GetObjectW((HGDIOBJ)wParam, sizeof(LOGFONTW), &lf);
3448 hDC = GetDC(editor->hWnd);
3449 ME_CharFormatFromLogFont(hDC, &lf, &fmt);
3450 ReleaseDC(editor->hWnd, hDC);
3451 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), &fmt);
3452 ME_SetDefaultCharFormat(editor, &fmt);
3454 ME_CommitUndo(editor);
3456 ME_RewrapRepaint(editor);
3461 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor), FALSE);
3464 TRACE("WM_SETTEXT lParam==%lx\n",lParam);
3465 if (!unicode && !strncmp((char *)lParam, "{\\rtf", 5))
3467 /* Undocumented: WM_SETTEXT supports RTF text */
3468 ME_StreamInRTFString(editor, 0, (char *)lParam);
3472 LPWSTR wszText = ME_ToUnicode(unicode, (void *)lParam);
3473 TRACE("WM_SETTEXT - %s\n", debugstr_w(wszText)); /* debugstr_w() */
3474 if (lstrlenW(wszText) > 0)
3478 /* uses default style! */
3479 if (!(GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_MULTILINE))
3484 while (*p != '\0' && *p != '\r' && *p != '\n') p++;
3487 ME_InsertTextFromCursor(editor, 0, wszText, len, editor->pBuffer->pDefaultStyle);
3489 ME_EndToUnicode(unicode, wszText);
3493 TRACE("WM_SETTEXT - NULL\n");
3494 if (editor->AutoURLDetect_bEnable)
3496 ME_UpdateLinkAttribute(editor, 0, -1);
3498 ME_SetSelection(editor, 0, 0);
3499 editor->nModifyStep = 0;
3500 ME_CommitUndo(editor);
3501 ME_EmptyUndoStack(editor);
3502 ME_UpdateRepaint(editor);
3507 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
3508 if (IsClipboardFormatAvailable(nRTFFormat))
3510 if (IsClipboardFormatAvailable(CF_UNICODETEXT))
3521 ME_GetSelection(editor, &range.cpMin, &range.cpMax);
3523 if (ME_Copy(editor, &range) && msg == WM_CUT)
3525 ME_InternalDeleteText(editor, range.cpMin, range.cpMax-range.cpMin, FALSE);
3526 ME_CommitUndo(editor);
3527 ME_UpdateRepaint(editor);
3531 case WM_GETTEXTLENGTH:
3533 GETTEXTLENGTHEX how;
3535 /* CR/LF conversion required in 2.0 mode, verbatim in 1.0 mode */
3536 how.flags = GTL_CLOSE | (editor->bEmulateVersion10 ? 0 : GTL_USECRLF) | GTL_NUMCHARS;
3537 how.codepage = unicode ? 1200 : CP_ACP;
3538 return ME_GetTextLengthEx(editor, &how);
3540 case EM_GETTEXTLENGTHEX:
3541 return ME_GetTextLengthEx(editor, (GETTEXTLENGTHEX *)wParam);
3546 LPSTR bufferA = NULL;
3547 LPWSTR bufferW = NULL;
3550 bufferW = heap_alloc((wParam + 2) * sizeof(WCHAR));
3552 bufferA = heap_alloc(wParam + 2);
3554 ex.cb = (wParam + 2) * (unicode ? sizeof(WCHAR) : sizeof(CHAR));
3555 ex.flags = GT_USECRLF;
3556 ex.codepage = unicode ? 1200 : CP_ACP;
3557 ex.lpDefaultChar = NULL;
3558 ex.lpUsedDefChar = NULL;
3560 rc = ME_GetTextEx(editor, &ex, unicode ? (LPARAM)bufferW : (LPARAM)bufferA);
3564 memcpy((LPWSTR)lParam, bufferW, wParam * sizeof(WCHAR));
3565 if (strlenW(bufferW) >= wParam) rc = 0;
3569 memcpy((LPSTR)lParam, bufferA, wParam);
3570 if (strlen(bufferA) >= wParam) rc = 0;
3577 return ME_GetTextEx(editor, (GETTEXTEX*)wParam, lParam);
3581 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
3582 ME_GetSelection(editor, &from, &to);
3583 tr.chrg.cpMin = from;
3585 tr.lpstrText = (WCHAR *)lParam;
3586 return ME_GetTextRange(editor, &tr, unicode);
3588 case EM_GETSCROLLPOS:
3590 POINT *point = (POINT *)lParam;
3591 point->x = 0; /* FIXME side scrolling not implemented */
3592 point->y = ME_GetYScrollPos(editor);
3595 case EM_GETTEXTRANGE:
3597 TEXTRANGEW *rng = (TEXTRANGEW *)lParam;
3598 TRACE("EM_GETTEXTRANGE min=%d max=%d unicode=%d emul1.0=%d length=%d\n",
3599 rng->chrg.cpMin, rng->chrg.cpMax, unicode,
3600 editor->bEmulateVersion10, ME_GetTextLength(editor));
3601 return ME_GetTextRange(editor, rng, unicode);
3605 ME_DisplayItem *run;
3606 const unsigned int nMaxChars = *(WORD *) lParam;
3607 unsigned int nCharsLeft = nMaxChars;
3608 char *dest = (char *) lParam;
3609 BOOL wroteNull = FALSE;
3611 TRACE("EM_GETLINE: row=%d, nMaxChars=%d (%s)\n", (int) wParam, nMaxChars,
3612 unicode ? "Unicode" : "Ansi");
3614 run = ME_FindRowWithNumber(editor, wParam);
3618 while (nCharsLeft && (run = ME_FindItemFwd(run, diRunOrStartRow))
3619 && !(run->member.run.nFlags & MERF_ENDPARA))
3623 if (run->type != diRun)
3625 strText = run->member.run.strText;
3626 nCopy = min(nCharsLeft, strText->nLen);
3629 memcpy(dest, strText->szData, nCopy * sizeof(WCHAR));
3631 nCopy = WideCharToMultiByte(CP_ACP, 0, strText->szData, nCopy, dest,
3632 nCharsLeft, NULL, NULL);
3633 dest += nCopy * (unicode ? sizeof(WCHAR) : 1);
3634 nCharsLeft -= nCopy;
3637 /* append line termination, space allowing */
3640 if (run && (run->member.run.nFlags & MERF_ENDPARA))
3643 /* Write as many \r as encoded in end-of-paragraph, space allowing */
3644 for (i = 0; i < run->member.run.nCR && nCharsLeft > 0; i++, nCharsLeft--)
3646 *((WCHAR *)dest) = '\r';
3647 dest += unicode ? sizeof(WCHAR) : 1;
3649 /* Write as many \n as encoded in end-of-paragraph, space allowing */
3650 for (i = 0; i < run->member.run.nLF && nCharsLeft > 0; i++, nCharsLeft--)
3652 *((WCHAR *)dest) = '\n';
3653 dest += unicode ? sizeof(WCHAR) : 1;
3659 *((WCHAR *)dest) = '\0';
3667 TRACE("EM_GETLINE: got %u characters\n", nMaxChars - nCharsLeft);
3668 return nMaxChars - nCharsLeft - (wroteNull ? 1 : 0);
3670 case EM_GETLINECOUNT:
3672 ME_DisplayItem *item = editor->pBuffer->pFirst->next;
3675 ME_DisplayItem *prev_para = NULL, *last_para = NULL;
3677 while (item != editor->pBuffer->pLast)
3679 assert(item->type == diParagraph);
3680 prev_para = ME_FindItemBack(item, diRun);
3682 assert(prev_para->member.run.nFlags & MERF_ENDPARA);
3684 nRows += item->member.para.nRows;
3685 item = item->member.para.next_para;
3687 last_para = ME_FindItemBack(item, diRun);
3689 assert(last_para->member.run.nFlags & MERF_ENDPARA);
3690 if (editor->bEmulateVersion10 && prev_para && last_para->member.run.nCharOfs == 0
3691 && prev_para->member.run.nCR == 1 && prev_para->member.run.nLF == 0)
3693 /* In 1.0 emulation, the last solitary \r at the very end of the text
3694 (if one exists) is NOT a line break.
3695 FIXME: this is an ugly hack. This should have a more regular model. */
3699 TRACE("EM_GETLINECOUNT: nRows==%d\n", nRows);
3700 return max(1, nRows);
3702 case EM_LINEFROMCHAR:
3705 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(editor, 1));
3707 return ME_RowNumberFromCharOfs(editor, wParam);
3709 case EM_EXLINEFROMCHAR:
3712 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(editor,1));
3714 return ME_RowNumberFromCharOfs(editor, lParam);
3718 ME_DisplayItem *item, *para;
3722 item = ME_FindItemBack(editor->pCursors[0].pRun, diStartRow);
3724 item = ME_FindRowWithNumber(editor, wParam);
3727 para = ME_GetParagraph(item);
3728 item = ME_FindItemFwd(item, diRun);
3729 nCharOfs = para->member.para.nCharOfs + item->member.run.nCharOfs;
3730 TRACE("EM_LINEINDEX: nCharOfs==%d\n", nCharOfs);
3735 ME_DisplayItem *item, *item_end;
3736 int nChars = 0, nThisLineOfs = 0, nNextLineOfs = 0;
3738 if (wParam > ME_GetTextLength(editor))
3742 FIXME("EM_LINELENGTH: returning number of unselected characters on lines with selection unsupported.\n");
3745 item = ME_FindItemAtOffset(editor, diRun, wParam, NULL);
3746 item = ME_RowStart(item);
3747 nThisLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item, diRun), 0);
3748 item_end = ME_FindItemFwd(item, diStartRowOrParagraphOrEnd);
3749 if (item_end->type == diStartRow)
3750 nNextLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item_end, diRun), 0);
3753 ME_DisplayItem *endPara;
3755 nNextLineOfs = ME_FindItemFwd(item, diParagraphOrEnd)->member.para.nCharOfs;
3756 endPara = ME_FindItemFwd(item, diParagraphOrEnd);
3757 endPara = ME_FindItemBack(endPara, diRun);
3759 assert(endPara->type == diRun);
3760 assert(endPara->member.run.nFlags & MERF_ENDPARA);
3761 nNextLineOfs -= endPara->member.run.nCR + endPara->member.run.nLF;
3763 nChars = nNextLineOfs - nThisLineOfs;
3764 TRACE("EM_LINELENGTH(%ld)==%d\n",wParam, nChars);
3767 case EM_EXLIMITTEXT:
3769 if ((int)lParam < 0)
3772 editor->nTextLimit = 65536;
3774 editor->nTextLimit = (int) lParam;
3780 editor->nTextLimit = 65536;
3782 editor->nTextLimit = (int) wParam;
3785 case EM_GETLIMITTEXT:
3787 return editor->nTextLimit;
3791 FINDTEXTA *ft = (FINDTEXTA *)lParam;
3792 int nChars = MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, NULL, 0);
3796 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
3797 MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, tmp, nChars);
3798 r = ME_FindText(editor, wParam, &ft->chrg, tmp, NULL);
3804 FINDTEXTEXA *ex = (FINDTEXTEXA *)lParam;
3805 int nChars = MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, NULL, 0);
3809 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
3810 MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, tmp, nChars);
3811 r = ME_FindText(editor, wParam, &ex->chrg, tmp, &ex->chrgText);
3817 FINDTEXTW *ft = (FINDTEXTW *)lParam;
3818 return ME_FindText(editor, wParam, &ft->chrg, ft->lpstrText, NULL);
3820 case EM_FINDTEXTEXW:
3822 FINDTEXTEXW *ex = (FINDTEXTEXW *)lParam;
3823 return ME_FindText(editor, wParam, &ex->chrg, ex->lpstrText, &ex->chrgText);
3826 if (!wParam || !lParam)
3828 *(int *)wParam = editor->nZoomNumerator;
3829 *(int *)lParam = editor->nZoomDenominator;
3832 return ME_SetZoom(editor, wParam, lParam);
3833 case EM_CHARFROMPOS:
3834 return ME_CharFromPos(editor, ((POINTL *)lParam)->x, ((POINTL *)lParam)->y, NULL);
3835 case EM_POSFROMCHAR:
3837 ME_DisplayItem *pRun;
3838 int nCharOfs, nOffset, nLength;
3843 /* detect which API version we're dealing with */
3844 if (wParam >= 0x40000)
3846 nLength = ME_GetTextLength(editor);
3847 nCharOfs = min(nCharOfs, nLength);
3848 nCharOfs = max(nCharOfs, 0);
3850 ME_RunOfsFromCharOfs(editor, nCharOfs, &pRun, &nOffset);
3851 assert(pRun->type == diRun);
3852 pt.y = pRun->member.run.pt.y;
3853 pt.x = pRun->member.run.pt.x + ME_PointFromChar(editor, &pRun->member.run, nOffset);
3854 pt.y += ME_GetParagraph(pRun)->member.para.pt.y + editor->rcFormat.top;
3855 pt.x += editor->rcFormat.left;
3857 pt.y -= editor->vert_si.nPos;
3858 si.cbSize = sizeof(si);
3860 if (GetScrollInfo(editor->hWnd, SB_HORZ, &si)) pt.x -= si.nPos;
3862 if (wParam >= 0x40000) {
3863 *(POINTL *)wParam = pt;
3865 return (wParam >= 0x40000) ? 0 : MAKELONG( pt.x, pt.y );
3871 ME_SetDefaultFormatRect(editor);
3872 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & WS_HSCROLL)
3873 { /* Squelch the default horizontal scrollbar it would make */
3874 ShowScrollBar(editor->hWnd, SB_HORZ, FALSE);
3877 si.cbSize = sizeof(si);
3878 si.fMask = SIF_PAGE | SIF_RANGE;
3879 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_DISABLENOSCROLL)
3880 si.fMask |= SIF_DISABLENOSCROLL;
3881 si.nMax = (si.fMask & SIF_DISABLENOSCROLL) ? 1 : 0;
3884 SetScrollInfo(editor->hWnd, SB_VERT, &si, TRUE);
3886 ME_CommitUndo(editor);
3887 ME_WrapMarkedParagraphs(editor);
3888 ME_MoveCaret(editor);
3892 ME_DestroyEditor(editor);
3896 return ME_SetCursor(editor);
3898 case WM_LBUTTONDBLCLK:
3899 case WM_LBUTTONDOWN:
3901 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
3902 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
3903 !ME_FilterEvent(editor, msg, &wParam, &lParam))
3905 SetFocus(editor->hWnd);
3906 ME_LButtonDown(editor, (short)LOWORD(lParam), (short)HIWORD(lParam),
3907 ME_CalculateClickCount(editor, msg, wParam, lParam));
3908 SetCapture(editor->hWnd);
3909 editor->bMouseCaptured = TRUE;
3910 ME_LinkNotify(editor,msg,wParam,lParam);
3911 if (!ME_SetCursor(editor)) goto do_default;
3915 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
3916 !ME_FilterEvent(editor, msg, &wParam, &lParam))
3918 if (editor->bMouseCaptured)
3919 ME_MouseMove(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
3920 ME_LinkNotify(editor,msg,wParam,lParam);
3921 /* Set cursor if mouse is captured, since WM_SETCURSOR won't be received. */
3922 if (editor->bMouseCaptured)
3923 ME_SetCursor(editor);
3926 if (editor->bMouseCaptured) {
3928 editor->bMouseCaptured = FALSE;
3930 if (editor->nSelectionType == stDocument)
3931 editor->nSelectionType = stPosition;
3932 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
3933 !ME_FilterEvent(editor, msg, &wParam, &lParam))
3937 ME_SetCursor(editor);
3938 ME_LinkNotify(editor,msg,wParam,lParam);
3942 case WM_RBUTTONDOWN:
3943 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
3944 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
3945 !ME_FilterEvent(editor, msg, &wParam, &lParam))
3948 case WM_CONTEXTMENU:
3949 if (!ME_ShowContextMenu(editor, (short)LOWORD(lParam), (short)HIWORD(lParam)))
3958 hDC = BeginPaint(editor->hWnd, &ps);
3959 /* Erase area outside of the formatting rectangle */
3960 if (ps.rcPaint.top < editor->rcFormat.top)
3963 rc.bottom = editor->rcFormat.top;
3964 FillRect(hDC, &rc, editor->hbrBackground);
3965 ps.rcPaint.top = editor->rcFormat.top;
3967 if (ps.rcPaint.bottom > editor->rcFormat.bottom) {
3969 rc.top = editor->rcFormat.bottom;
3970 FillRect(hDC, &rc, editor->hbrBackground);
3971 ps.rcPaint.bottom = editor->rcFormat.bottom;
3973 if (ps.rcPaint.left < editor->rcFormat.left) {
3975 rc.right = editor->rcFormat.left;
3976 FillRect(hDC, &rc, editor->hbrBackground);
3977 ps.rcPaint.left = editor->rcFormat.left;
3979 if (ps.rcPaint.right > editor->rcFormat.right) {
3981 rc.left = editor->rcFormat.right;
3982 FillRect(hDC, &rc, editor->hbrBackground);
3983 ps.rcPaint.right = editor->rcFormat.right;
3986 ME_PaintContent(editor, hDC, FALSE, &ps.rcPaint);
3987 EndPaint(editor->hWnd, &ps);
3991 editor->bHaveFocus = TRUE;
3992 ME_ShowCaret(editor);
3993 ME_SendOldNotify(editor, EN_SETFOCUS);
3996 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
3997 editor->bHaveFocus = FALSE;
3998 ME_HideCaret(editor);
3999 ME_SendOldNotify(editor, EN_KILLFOCUS);
4003 HDC hDC = (HDC)wParam;
4005 if (GetUpdateRect(editor->hWnd,&rc,TRUE))
4007 FillRect(hDC, &rc, editor->hbrBackground);
4012 TRACE("editor wnd command = %d\n", LOWORD(wParam));
4015 if ((editor->nEventMask & ENM_KEYEVENTS) &&
4016 !ME_FilterEvent(editor, msg, &wParam, &lParam))
4020 if ((editor->nEventMask & ENM_KEYEVENTS) &&
4021 !ME_FilterEvent(editor, msg, &wParam, &lParam))
4023 if (ME_KeyDown(editor, LOWORD(wParam)))
4027 return ME_Char(editor, wParam, lParam, unicode);
4031 if(wParam == UNICODE_NOCHAR) return TRUE;
4032 if(wParam <= 0x000fffff)
4034 if(wParam > 0xffff) /* convert to surrogates */
4037 ME_Char(editor, (wParam >> 10) + 0xd800, 0, TRUE);
4038 ME_Char(editor, (wParam & 0x03ff) + 0xdc00, 0, TRUE);
4040 ME_Char(editor, wParam, 0, TRUE);
4046 case EM_STOPGROUPTYPING:
4047 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
4049 case EM_SCROLL: /* fall through */
4055 origNPos = ME_GetYScrollPos(editor);
4058 if (editor && editor->pBuffer && editor->pBuffer->pDefaultStyle)
4059 lineHeight = editor->pBuffer->pDefaultStyle->tm.tmHeight;
4060 if (lineHeight <= 0) lineHeight = 24;
4062 switch(LOWORD(wParam))
4065 ME_ScrollUp(editor,lineHeight);
4068 ME_ScrollDown(editor,lineHeight);
4071 ME_ScrollUp(editor,editor->sizeWindow.cy);
4074 ME_ScrollDown(editor,editor->sizeWindow.cy);
4077 case SB_THUMBPOSITION:
4080 sbi.cbSize = sizeof(sbi);
4081 sbi.fMask = SIF_TRACKPOS;
4082 /* Try to get 32-bit track position value. */
4083 if (!GetScrollInfo(editor->hWnd, SB_VERT, &sbi))
4084 /* GetScrollInfo failed, settle for 16-bit value in wParam. */
4085 sbi.nTrackPos = HIWORD(wParam);
4087 ME_ScrollAbs(editor, sbi.nTrackPos);
4091 if (msg == EM_SCROLL)
4092 return 0x00010000 | (((ME_GetYScrollPos(editor) - origNPos)/lineHeight) & 0xffff);
4098 UINT pulScrollLines;
4101 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
4102 !ME_FilterEvent(editor, msg, &wParam, &lParam))
4105 ctrl_is_down = GetKeyState(VK_CONTROL) & 0x8000;
4107 gcWheelDelta = GET_WHEEL_DELTA_WPARAM(wParam);
4109 if (abs(gcWheelDelta) >= WHEEL_DELTA)
4113 if (!editor->nZoomNumerator || !editor->nZoomDenominator)
4117 numerator = editor->nZoomNumerator * 100 / editor->nZoomDenominator;
4119 numerator = numerator + (gcWheelDelta / WHEEL_DELTA) * 10;
4120 if (numerator >= 10 && numerator <= 500)
4121 ME_SetZoom(editor, numerator, 100);
4123 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
4124 /* FIXME follow the original */
4126 ME_ScrollDown(editor,pulScrollLines * (-gcWheelDelta / WHEEL_DELTA) * 8);
4133 *((RECT *)lParam) = editor->rcFormat;
4134 if (editor->bDefaultFormatRect)
4135 ((RECT *)lParam)->left -= editor->selofs;
4143 DWORD exstyle = GetWindowLongW(editor->hWnd, GWL_EXSTYLE);
4144 int border = (exstyle & WS_EX_CLIENTEDGE) ? 1 : 0;
4146 RECT *rc = (RECT *)lParam;
4148 GetClientRect(editor->hWnd, &clientRect);
4151 editor->rcFormat.top = max(0, rc->top - border);
4152 editor->rcFormat.left = max(0, rc->left - border);
4153 editor->rcFormat.bottom = min(clientRect.bottom, rc->bottom);
4154 editor->rcFormat.right = min(clientRect.right, rc->right + border);
4155 } else if (wParam == 1) {
4156 /* MSDN incorrectly says a wParam value of 1 causes the
4157 * lParam rect to be used as a relative offset,
4158 * however, the tests show it just prevents min/max bound
4160 editor->rcFormat.top = rc->top - border;
4161 editor->rcFormat.left = rc->left - border;
4162 editor->rcFormat.bottom = rc->bottom;
4163 editor->rcFormat.right = rc->right + border;
4167 editor->bDefaultFormatRect = FALSE;
4171 ME_SetDefaultFormatRect(editor);
4172 editor->bDefaultFormatRect = TRUE;
4174 if (msg != EM_SETRECTNP)
4175 ME_RewrapRepaint(editor);
4178 case EM_REQUESTRESIZE:
4179 ME_SendRequestResize(editor, TRUE);
4187 GetClientRect(editor->hWnd, &clientRect);
4188 if (editor->bDefaultFormatRect) {
4189 ME_SetDefaultFormatRect(editor);
4191 editor->rcFormat.right += clientRect.right - editor->prevClientRect.right;
4192 editor->rcFormat.bottom += clientRect.bottom - editor->prevClientRect.bottom;
4194 editor->prevClientRect = clientRect;
4195 ME_RewrapRepaint(editor);
4198 /* IME messages to make richedit controls IME aware */
4199 case WM_IME_SETCONTEXT:
4200 case WM_IME_CONTROL:
4202 case WM_IME_COMPOSITIONFULL:
4204 case WM_IME_STARTCOMPOSITION:
4206 editor->imeStartIndex=ME_GetCursorOfs(editor,0);
4207 ME_DeleteSelection(editor);
4208 ME_CommitUndo(editor);
4209 ME_UpdateRepaint(editor);
4212 case WM_IME_COMPOSITION:
4216 ME_Style *style = ME_GetInsertStyle(editor, 0);
4217 hIMC = ImmGetContext(editor->hWnd);
4218 ME_DeleteSelection(editor);
4219 ME_CommitUndo(editor);
4220 ME_SaveTempStyle(editor);
4221 if (lParam & GCS_RESULTSTR)
4223 LPWSTR lpCompStr = NULL;
4226 dwBufLen = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
4227 lpCompStr = HeapAlloc(GetProcessHeap(),0,dwBufLen + sizeof(WCHAR));
4228 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, lpCompStr, dwBufLen);
4229 lpCompStr[dwBufLen/sizeof(WCHAR)] = 0;
4230 ME_InsertTextFromCursor(editor,0,lpCompStr,dwBufLen/sizeof(WCHAR),style);
4232 else if (lParam & GCS_COMPSTR)
4234 LPWSTR lpCompStr = NULL;
4237 dwBufLen = ImmGetCompositionStringW(hIMC, GCS_COMPSTR, NULL, 0);
4238 lpCompStr = HeapAlloc(GetProcessHeap(),0,dwBufLen + sizeof(WCHAR));
4239 ImmGetCompositionStringW(hIMC, GCS_COMPSTR, lpCompStr, dwBufLen);
4240 lpCompStr[dwBufLen/sizeof(WCHAR)] = 0;
4242 ME_InsertTextFromCursor(editor,0,lpCompStr,dwBufLen/sizeof(WCHAR),style);
4243 ME_SetSelection(editor,editor->imeStartIndex,
4244 editor->imeStartIndex + dwBufLen/sizeof(WCHAR));
4246 ME_ReleaseStyle(style);
4247 ME_UpdateRepaint(editor);
4250 case WM_IME_ENDCOMPOSITION:
4252 ME_DeleteSelection(editor);
4253 editor->imeStartIndex=-1;
4256 case EM_GETOLEINTERFACE:
4258 LPVOID *ppvObj = (LPVOID*) lParam;
4259 return CreateIRichEditOle(editor, ppvObj);
4261 case EM_GETPASSWORDCHAR:
4263 return editor->cPasswordMask;
4265 case EM_SETOLECALLBACK:
4266 if(editor->lpOleCallback)
4267 IUnknown_Release(editor->lpOleCallback);
4268 editor->lpOleCallback = (LPRICHEDITOLECALLBACK)lParam;
4269 if(editor->lpOleCallback)
4270 IUnknown_AddRef(editor->lpOleCallback);
4272 case EM_GETWORDBREAKPROC:
4273 return (LRESULT)editor->pfnWordBreak;
4274 case EM_SETWORDBREAKPROC:
4276 EDITWORDBREAKPROCW pfnOld = editor->pfnWordBreak;
4278 editor->pfnWordBreak = (EDITWORDBREAKPROCW)lParam;
4279 return (LRESULT)pfnOld;
4281 case EM_SETTEXTMODE:
4286 GETTEXTLENGTHEX how;
4288 /* CR/LF conversion required in 2.0 mode, verbatim in 1.0 mode */
4289 how.flags = GTL_CLOSE | (editor->bEmulateVersion10 ? 0 : GTL_USECRLF) | GTL_NUMCHARS;
4290 how.codepage = unicode ? 1200 : CP_ACP;
4291 ret = ME_GetTextLengthEx(editor, &how);
4294 /*Check for valid wParam*/
4295 if ((((wParam & TM_RICHTEXT) && ((wParam & TM_PLAINTEXT) << 1))) ||
4296 (((wParam & TM_MULTILEVELUNDO) && ((wParam & TM_SINGLELEVELUNDO) << 1))) ||
4297 (((wParam & TM_MULTICODEPAGE) && ((wParam & TM_SINGLECODEPAGE) << 1))))
4301 if (wParam & (TM_RICHTEXT | TM_PLAINTEXT))
4303 mask |= (TM_RICHTEXT | TM_PLAINTEXT);
4304 changes |= (wParam & (TM_RICHTEXT | TM_PLAINTEXT));
4306 /*FIXME: Currently no support for undo level and code page options*/
4307 editor->mode = (editor->mode & (~mask)) | changes;
4313 case EM_SETPASSWORDCHAR:
4315 editor->cPasswordMask = wParam;
4316 ME_RewrapRepaint(editor);
4319 case EM_SETTARGETDEVICE:
4322 BOOL new = (lParam == 0);
4323 if (editor->bWordWrap != new)
4325 editor->bWordWrap = new;
4326 ME_RewrapRepaint(editor);
4329 else FIXME("Unsupported yet non NULL device in EM_SETTARGETDEVICE\n");
4333 *phresult = S_FALSE;
4339 static LRESULT RichEditWndProc_common(HWND hWnd, UINT msg, WPARAM wParam,
4340 LPARAM lParam, BOOL unicode)
4342 ME_TextEditor *editor;
4346 TRACE("enter hwnd %p msg %04x (%s) %lx %lx, unicode %d\n",
4347 hWnd, msg, get_msg_name(msg), wParam, lParam, unicode);
4349 editor = (ME_TextEditor *)GetWindowLongPtrW(hWnd, 0);
4352 if (msg == WM_NCCREATE)
4354 CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
4355 TRACE("WM_NCCREATE: hWnd %p style 0x%08x\n", hWnd, pcs->style);
4356 editor = ME_MakeEditor(hWnd, FALSE);
4357 SetWindowLongPtrW(hWnd, 0, (LONG_PTR)editor);
4360 else if (msg != WM_NCDESTROY)
4362 ERR("called with invalid hWnd %p - application bug?\n", hWnd);
4367 lresult = ME_HandleMessage(editor, msg, wParam, lParam, unicode, &hresult);
4369 if (hresult == S_FALSE)
4370 lresult = DefWindowProcW(hWnd, msg, wParam, lParam);
4372 TRACE("exit hwnd %p msg %04x (%s) %lx %lx, unicode %d -> %lu\n",
4373 hWnd, msg, get_msg_name(msg), wParam, lParam, unicode, lresult);
4378 static LRESULT WINAPI RichEditWndProcW(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
4380 BOOL unicode = TRUE;
4382 /* Under Win9x RichEdit20W returns ANSI strings, see the tests. */
4383 if (msg == WM_GETTEXT && (GetVersion() & 0x80000000))
4386 return RichEditWndProc_common(hWnd, msg, wParam, lParam, unicode);
4389 static LRESULT WINAPI RichEditWndProcA(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
4391 return RichEditWndProc_common(hWnd, msg, wParam, lParam, FALSE);
4394 /******************************************************************
4395 * RichEditANSIWndProc (RICHED20.10)
4397 LRESULT WINAPI RichEditANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
4399 return RichEditWndProcA(hWnd, msg, wParam, lParam);
4402 /******************************************************************
4403 * RichEdit10ANSIWndProc (RICHED20.9)
4405 LRESULT WINAPI RichEdit10ANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
4407 if (msg == WM_NCCREATE && !GetWindowLongPtrW(hWnd, 0))
4409 ME_TextEditor *editor;
4410 CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
4412 TRACE("WM_NCCREATE: hWnd %p style 0x%08x\n", hWnd, pcs->style);
4413 editor = ME_MakeEditor(hWnd, TRUE);
4414 SetWindowLongPtrW(hWnd, 0, (LONG_PTR)editor);
4417 return RichEditANSIWndProc(hWnd, msg, wParam, lParam);
4420 void ME_SendOldNotify(ME_TextEditor *editor, int nCode)
4422 HWND hWnd = editor->hWnd;
4423 SendMessageA(GetParent(hWnd), WM_COMMAND, MAKEWPARAM(GetWindowLongW(hWnd, GWLP_ID), nCode), (LPARAM)hWnd);
4426 int ME_CountParagraphsBetween(ME_TextEditor *editor, int from, int to)
4428 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
4431 while(item && item->member.para.next_para->member.para.nCharOfs <= from)
4432 item = item->member.para.next_para;
4435 while(item && item->member.para.next_para->member.para.nCharOfs <= to) {
4436 item = item->member.para.next_para;
4443 int ME_GetTextW(ME_TextEditor *editor, WCHAR *buffer, int nStart, int nChars, int bCRLF)
4445 ME_DisplayItem *item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
4447 WCHAR *pStart = buffer;
4454 /* bCRLF flag is only honored in 2.0 and up. 1.0 must always return text verbatim */
4455 if (editor->bEmulateVersion10) bCRLF = 0;
4459 int nLen = ME_StrLen(item->member.run.strText) - nStart;
4462 CopyMemory(buffer, item->member.run.strText->szData + nStart, sizeof(WCHAR)*nLen);
4471 item = ME_FindItemFwd(item, diRun);
4474 while(nChars && item)
4476 int nLen = ME_StrLen(item->member.run.strText);
4477 if (item->member.run.nFlags & MERF_ENDPARA)
4478 nLen = item->member.run.nCR + item->member.run.nLF;
4482 if (item->member.run.nFlags & MERF_ENDCELL &&
4483 item->member.run.nFlags & MERF_ENDPARA)
4487 else if (item->member.run.nFlags & MERF_ENDPARA)
4489 if (!ME_FindItemFwd(item, diRun))
4490 /* No '\r' is appended to the last paragraph. */
4492 else if (bCRLF && nChars == 1) {
4498 /* richedit 2.0 case - actual line-break is \r but should report \r\n */
4499 if (ME_GetParagraph(item)->member.para.nFlags & (MEPF_ROWSTART|MEPF_ROWEND))
4504 *buffer = '\n'; /* Later updated by nLen==1 at the end of the loop */
4514 /* richedit 2.0 verbatim has only \r. richedit 1.0 should honor encodings */
4516 while (nChars - i > 0 && i < item->member.run.nCR)
4518 buffer[i] = '\r'; i++;
4521 while (nChars - i - j > 0 && j < item->member.run.nLF)
4523 buffer[i+j] = '\n'; j++;
4529 CopyMemory(buffer, item->member.run.strText->szData, sizeof(WCHAR)*nLen);
4536 TRACE("nWritten=%d, actual=%d\n", nWritten, buffer-pStart);
4540 item = ME_FindItemFwd(item, diRun);
4543 TRACE("nWritten=%d, actual=%d\n", nWritten, buffer-pStart);
4547 static BOOL ME_RegisterEditorClass(HINSTANCE hInstance)
4552 wcW.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
4553 wcW.lpfnWndProc = RichEditWndProcW;
4555 wcW.cbWndExtra = sizeof(ME_TextEditor *);
4556 wcW.hInstance = NULL; /* hInstance would register DLL-local class */
4558 wcW.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
4559 wcW.hbrBackground = GetStockObject(NULL_BRUSH);
4560 wcW.lpszMenuName = NULL;
4562 if (is_version_nt())
4564 wcW.lpszClassName = RichEdit20W;
4565 if (!RegisterClassW(&wcW)) return FALSE;
4566 wcW.lpszClassName = RichEdit50W;
4567 if (!RegisterClassW(&wcW)) return FALSE;
4571 /* WNDCLASSA/W have the same layout */
4572 wcW.lpszClassName = (LPCWSTR)"RichEdit20W";
4573 if (!RegisterClassA((WNDCLASSA *)&wcW)) return FALSE;
4574 wcW.lpszClassName = (LPCWSTR)"RichEdit50W";
4575 if (!RegisterClassA((WNDCLASSA *)&wcW)) return FALSE;
4578 wcA.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
4579 wcA.lpfnWndProc = RichEditWndProcA;
4581 wcA.cbWndExtra = sizeof(ME_TextEditor *);
4582 wcA.hInstance = NULL; /* hInstance would register DLL-local class */
4584 wcA.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
4585 wcA.hbrBackground = GetStockObject(NULL_BRUSH);
4586 wcA.lpszMenuName = NULL;
4587 wcA.lpszClassName = "RichEdit20A";
4588 if (!RegisterClassA(&wcA)) return FALSE;
4589 wcA.lpszClassName = "RichEdit50A";
4590 if (!RegisterClassA(&wcA)) return FALSE;
4595 LRESULT WINAPI REComboWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
4596 /* FIXME: Not implemented */
4597 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
4598 hWnd, msg, get_msg_name(msg), wParam, lParam);
4599 return DefWindowProcW(hWnd, msg, wParam, lParam);
4602 LRESULT WINAPI REListWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
4603 /* FIXME: Not implemented */
4604 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
4605 hWnd, msg, get_msg_name(msg), wParam, lParam);
4606 return DefWindowProcW(hWnd, msg, wParam, lParam);
4609 /******************************************************************
4610 * REExtendedRegisterClass (RICHED20.8)
4612 * FIXME undocumented
4613 * Need to check for errors and implement controls and callbacks
4615 LRESULT WINAPI REExtendedRegisterClass(void)
4620 FIXME("semi stub\n");
4624 wcW.hInstance = NULL;
4627 wcW.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
4628 wcW.lpszMenuName = NULL;
4630 if (!ME_ListBoxRegistered)
4632 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS;
4633 wcW.lpfnWndProc = REListWndProc;
4634 wcW.lpszClassName = REListBox20W;
4635 if (RegisterClassW(&wcW)) ME_ListBoxRegistered = TRUE;
4638 if (!ME_ComboBoxRegistered)
4640 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
4641 wcW.lpfnWndProc = REComboWndProc;
4642 wcW.lpszClassName = REComboBox20W;
4643 if (RegisterClassW(&wcW)) ME_ComboBoxRegistered = TRUE;
4647 if (ME_ListBoxRegistered)
4649 if (ME_ComboBoxRegistered)
4655 static BOOL isurlspecial(WCHAR c)
4657 static const WCHAR special_chars[] = {'.','/','%','@','*','|','\\','+','#',0};
4658 return strchrW( special_chars, c ) != NULL;
4662 * This proc takes a selection, and scans it forward in order to select the span
4663 * of a possible URL candidate. A possible URL candidate must start with isalnum
4664 * or one of the following special characters: *|/\+%#@ and must consist entirely
4665 * of the characters allowed to start the URL, plus : (colon) which may occur
4666 * at most once, and not at either end.
4668 * sel_max == -1 indicates scan to end of text.
4670 static BOOL ME_FindNextURLCandidate(ME_TextEditor *editor, int sel_min, int sel_max,
4671 int * candidate_min, int * candidate_max)
4673 ME_DisplayItem * item;
4674 ME_DisplayItem * para;
4676 BOOL foundColon = FALSE;
4677 WCHAR lastAcceptedChar = '\0';
4679 TRACE("sel_min = %d sel_max = %d\n", sel_min, sel_max);
4681 *candidate_min = *candidate_max = -1;
4682 item = ME_FindItemAtOffset(editor, diRun, sel_min, &nStart);
4683 if (!item) return FALSE;
4684 TRACE("nStart = %d\n", nStart);
4685 para = ME_GetParagraph(item);
4686 if (sel_max == -1) sel_max = ME_GetTextLength(editor);
4687 while (item && para->member.para.nCharOfs + item->member.run.nCharOfs + nStart < sel_max)
4689 ME_DisplayItem * next_item;
4691 if (!(item->member.run.nFlags & MERF_ENDPARA)) {
4692 /* Find start of candidate */
4693 if (*candidate_min == -1) {
4694 while (nStart < ME_StrLen(item->member.run.strText) &&
4695 !(isalnumW(item->member.run.strText->szData[nStart]) ||
4696 isurlspecial(item->member.run.strText->szData[nStart]))) {
4699 if (nStart < ME_StrLen(item->member.run.strText) &&
4700 (isalnumW(item->member.run.strText->szData[nStart]) ||
4701 isurlspecial(item->member.run.strText->szData[nStart]))) {
4702 *candidate_min = para->member.para.nCharOfs + item->member.run.nCharOfs + nStart;
4703 lastAcceptedChar = item->member.run.strText->szData[nStart];
4708 /* Find end of candidate */
4709 if (*candidate_min >= 0) {
4710 while (nStart < ME_StrLen(item->member.run.strText) &&
4711 (isalnumW(item->member.run.strText->szData[nStart]) ||
4712 isurlspecial(item->member.run.strText->szData[nStart]) ||
4713 (!foundColon && item->member.run.strText->szData[nStart] == ':') )) {
4714 if (item->member.run.strText->szData[nStart] == ':') foundColon = TRUE;
4715 lastAcceptedChar = item->member.run.strText->szData[nStart];
4718 if (nStart < ME_StrLen(item->member.run.strText) &&
4719 !(isalnumW(item->member.run.strText->szData[nStart]) ||
4720 isurlspecial(item->member.run.strText->szData[nStart]) )) {
4721 *candidate_max = para->member.para.nCharOfs + item->member.run.nCharOfs + nStart;
4723 if (lastAcceptedChar == ':') (*candidate_max)--;
4728 /* End of paragraph: skip it if before candidate span, or terminates
4729 current active span */
4730 if (*candidate_min >= 0) {
4731 *candidate_max = para->member.para.nCharOfs + item->member.run.nCharOfs;
4732 if (lastAcceptedChar == ':') (*candidate_max)--;
4737 /* Reaching this point means no span was found, so get next span */
4738 next_item = ME_FindItemFwd(item, diRun);
4740 if (*candidate_min >= 0) {
4741 /* There are no further runs, so take end of text as end of candidate */
4742 *candidate_max = para->member.para.nCharOfs + item->member.run.nCharOfs + nStart;
4743 if (lastAcceptedChar == ':') (*candidate_max)--;
4748 para = ME_GetParagraph(item);
4753 if (*candidate_min >= 0) {
4754 /* There are no further runs, so take end of text as end of candidate */
4755 *candidate_max = para->member.para.nCharOfs + item->member.run.nCharOfs + nStart;
4756 if (lastAcceptedChar == ':') (*candidate_max)--;
4764 * This proc evaluates the selection and returns TRUE if it can be considered an URL
4766 static BOOL ME_IsCandidateAnURL(ME_TextEditor *editor, int sel_min, int sel_max)
4772 /* Code below depends on these being in decreasing length order! */
4786 LPWSTR bufferW = NULL;
4790 if (sel_max == -1) sel_max = ME_GetTextLength(editor);
4791 assert(sel_min <= sel_max);
4792 for (i = 0; i < sizeof(prefixes) / sizeof(struct prefix_s); i++)
4794 if (sel_max - sel_min < prefixes[i].length) continue;
4795 if (bufferW == NULL) {
4796 bufferW = heap_alloc((sel_max - sel_min + 1) * sizeof(WCHAR));
4798 ME_GetTextW(editor, bufferW, sel_min, min(sel_max - sel_min, lstrlenA(prefixes[i].text)), 0);
4799 MultiByteToWideChar(CP_ACP, 0, prefixes[i].text, -1, bufW, 32);
4800 if (!lstrcmpW(bufW, bufferW))
4811 * This proc walks through the indicated selection and evaluates whether each
4812 * section identified by ME_FindNextURLCandidate and in-between sections have
4813 * their proper CFE_LINK attributes set or unset. If the CFE_LINK attribute is
4814 * not what it is supposed to be, this proc sets or unsets it as appropriate.
4816 * Returns TRUE if at least one section was modified.
4818 static BOOL ME_UpdateLinkAttribute(ME_TextEditor *editor, int sel_min, int sel_max)
4820 BOOL modified = FALSE;
4823 if (sel_max == -1) sel_max = ME_GetTextLength(editor);
4830 if (ME_FindNextURLCandidate(editor, sel_min, sel_max, &cMin, &cMax))
4832 /* Section before candidate is not an URL */
4833 beforeURL[0] = sel_min;
4834 beforeURL[1] = cMin;
4836 if (ME_IsCandidateAnURL(editor, cMin, cMax))
4838 inURL[0] = cMin; inURL[1] = cMax;
4842 beforeURL[1] = cMax;
4843 inURL[0] = inURL[1] = -1;
4849 /* No more candidates until end of selection */
4850 beforeURL[0] = sel_min;
4851 beforeURL[1] = sel_max;
4852 inURL[0] = inURL[1] = -1;
4856 if (beforeURL[0] < beforeURL[1])
4858 /* CFE_LINK effect should be consistently unset */
4859 link.cbSize = sizeof(link);
4860 ME_GetCharFormat(editor, beforeURL[0], beforeURL[1], &link);
4861 if (!(link.dwMask & CFM_LINK) || (link.dwEffects & CFE_LINK))
4863 /* CFE_LINK must be unset from this range */
4864 memset(&link, 0, sizeof(CHARFORMAT2W));
4865 link.cbSize = sizeof(link);
4866 link.dwMask = CFM_LINK;
4868 ME_SetCharFormat(editor, beforeURL[0], beforeURL[1] - beforeURL[0], &link);
4872 if (inURL[0] < inURL[1])
4874 /* CFE_LINK effect should be consistently set */
4875 link.cbSize = sizeof(link);
4876 ME_GetCharFormat(editor, inURL[0], inURL[1], &link);
4877 if (!(link.dwMask & CFM_LINK) || !(link.dwEffects & CFE_LINK))
4879 /* CFE_LINK must be set on this range */
4880 memset(&link, 0, sizeof(CHARFORMAT2W));
4881 link.cbSize = sizeof(link);
4882 link.dwMask = CFM_LINK;
4883 link.dwEffects = CFE_LINK;
4884 ME_SetCharFormat(editor, inURL[0], inURL[1] - inURL[0], &link);
4888 } while (sel_min < sel_max);