2 * RichEdit - functions dealing with editor object
4 * Copyright 2004 by Krzysztof Foltman
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 API implementation status:
24 Messages (ANSI versions not done yet)
25 - EM_AUTOURLDETECT 2.0
41 - EM_GETCHARFORMAT (partly done)
43 - EM_GETFIRSTVISIBLELINE
44 - EM_GETIMECOLOR 1.0asian
45 - EM_GETIMECOMPMODE 2.0
46 - EM_GETIMEOPTIONS 1.0asian
48 - EM_GETLANGOPTIONS 2.0
51 - EM_GETLINECOUNT returns number of rows, not of paragraphs
56 - EM_GETPUNCTUATION 1.0asian
60 + EM_GETSELTEXT (ANSI&Unicode)
63 ? + EM_GETTEXTRANGE (ANSI&Unicode)
66 - EM_GETWORDBREAKPROCEX
67 - EM_GETWORDWRAPMODE 1.0asian
78 + EM_REPLACESEL (proper style?) ANSI&Unicode
83 - EM_SETCHARFORMAT (partly done, no ANSI)
84 + EM_SETEVENTMASK (few notifications supported)
85 - EM_SETIMECOLOR 1.0asian
86 - EM_SETIMEOPTIONS 1.0asian
87 - EM_SETLANGOPTIONS 2.0
89 + EM_SETMODIFY (not sure if implementation is correct)
93 - EM_SETPUNCTUATION 1.0asian
94 + EM_SETREADONLY no beep on modification attempt
96 - EM_SETRECTNP (EM_SETRECT without repainting) - not supported in RICHEDIT
100 - EM_SETUNDOLIMIT 2.0
101 - EM_SETWORDBREAKPROC
102 - EM_SETWORDBREAKPROCEX
103 - EM_SETWORDWRAPMODE 1.0asian
104 - EM_STOPGROUPTYPING 2.0
110 - WM_COPY (lame implementation, no RTF support)
111 - WM_CUT (lame implementation, no RTF support)
112 + WM_GETDLGCODE (the current implementation is incomplete)
113 + WM_GETTEXT (ANSI&Unicode)
114 + WM_GETTEXTLENGTH (ANSI version sucks)
117 + WM_SETTEXT (resets undo stack !) (proper style?) ANSI&Unicode
119 - WM_STYLECHANGED (things like read-only flag)
124 * EN_CHANGE (sent from the wrong place)
141 * EN_UPDATE (sent from the wrong place)
149 - ES_DISABLENOSCROLL (scrollbar is always visible)
150 - ES_EX_NOCALLOLEINIT
152 - ES_MULTILINE (currently single line controls aren't supported)
154 - ES_READONLY (I'm not sure if beeping is the proper behaviour)
160 - ES_WANTRETURN (don't know how to do WM_GETDLGCODE part)
167 * RICHED20 TODO (incomplete):
170 * - add remaining CHARFORMAT/PARAFORMAT fields
171 * - right/center align should strip spaces from the beginning
172 * - more advanced navigation (Ctrl-arrows, PageUp/PageDn)
174 * - pictures (not just smiling faces that lack API support ;-) )
176 * - calculate heights of pictures (half-done)
177 * - EM_STREAMIN/EM_STREAMOUT
178 * - horizontal scrolling (not even started)
179 * - fix scrollbars and refresh (it sucks bigtime)
180 * - hysteresis during wrapping (related to scrollbars appearing/disappearing)
181 * - should remember maximum row width for wrap hysteresis
183 * - how to implement EM_FORMATRANGE and EM_DISPLAYBAND ? (Mission Impossible)
184 * - italic cursor with italic fonts
186 * - most notifications aren't sent at all (the most important ones are)
187 * - when should EN_SELCHANGE be sent after text change ? (before/after EN_UPDATE?)
188 * - WM_SETTEXT may use wrong style (but I'm 80% sure it's OK)
189 * - EM_GETCHARFORMAT with SCF_SELECTION may not behave 100% like in original (but very close)
190 * - bugs in end-of-text handling (the gray bar) could get me in jail ;-)
191 * - determination of row size
192 * - end-of-paragraph marks should be of reasonable size
194 * Bugs that are probably fixed, but not so easy to verify:
195 * - EN_UPDATE/EN_CHANGE are handled very incorrectly (should be OK now)
196 * - undo for ME_JoinParagraphs doesn't store paragraph format ? (it does)
197 * - check/fix artificial EOL logic (bCursorAtEnd, hardly logical)
198 * - caret shouldn't be displayed when selection isn't empty
199 * - check refcounting in style management functions (looks perfect now, but no bugs is suspicious)
200 * - undo for setting default format (done, might be buggy)
201 * - styles might be not released properly (looks like they work like charm, but who knows?
209 #define NO_SHLWAPI_STREAM
214 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
217 HANDLE me_heap = NULL;
219 void DoWrap(ME_TextEditor *editor) {
220 HDC hDC = GetDC(editor->hWnd);
221 ME_DisplayItem *item;
223 HWND hWnd = editor->hWnd;
224 int yLength = editor->nTotalLength;
225 int nSelFrom, nSelTo;
226 int nMinSel, nMaxSel;
228 ME_GetSelection(editor, &nSelFrom, &nSelTo);
230 nMinSel = nSelFrom < editor->nOldSelFrom ? nSelFrom : editor->nOldSelFrom;
231 nMaxSel = nSelTo > editor->nOldSelTo ? nSelTo : editor->nOldSelTo;
233 ME_InitContext(&c, editor, hDC);
236 item = editor->pBuffer->pFirst->next;
237 while(item != editor->pBuffer->pLast) {
238 int para_from, para_to;
239 BOOL bRedraw = FALSE;
241 para_from = item->member.para.nCharOfs;
242 para_to = item->member.para.next_para->member.para.nCharOfs;
244 if (para_from <= nMaxSel && para_to >= nMinSel && nMinSel != nMaxSel)
247 assert(item->type == diParagraph);
248 if (!(item->member.para.nFlags & MEPF_WRAPPED)
249 || (item->member.para.nYPos != c.pt.y))
251 item->member.para.nYPos = c.pt.y;
253 ME_WrapTextParagraph(&c, item);
256 item->member.para.nFlags |= MEPF_REDRAW;
258 c.pt.y = item->member.para.nYPos + item->member.para.nHeight;
259 item = item->member.para.next_para;
261 editor->sizeWindow.cx = c.rcView.right-c.rcView.left;
262 editor->sizeWindow.cy = c.rcView.bottom-c.rcView.top;
263 editor->nTotalLength = c.pt.y-c.rcView.top;
265 ME_UpdateScrollBar(editor, -1);
266 ME_EnsureVisible(editor, editor->pCursors[0].pRun);
268 /* FIXME this should be marked for update too somehow, so that painting happens in ME_PaintContent */
269 if (yLength != c.pt.y-c.rcView.top) {
271 rc.left = c.rcView.left;
272 rc.right = c.rcView.right;
274 rc.bottom = c.rcView.bottom;
275 InvalidateRect(editor->hWnd, &rc, FALSE);
276 UpdateWindow(editor->hWnd);
279 editor->nOldSelFrom = nSelFrom;
280 editor->nOldSelTo = nSelTo;
281 /* PatBlt(hDC, 0, c.pt.y, c.rcView.right, c.rcView.bottom, BLACKNESS);*/
283 ME_DestroyContext(&c);
284 ReleaseDC(hWnd, hDC);
287 ME_TextBuffer *ME_MakeText() {
289 ME_TextBuffer *buf = ALLOC_OBJ(ME_TextBuffer);
291 ME_DisplayItem *p1 = ME_MakeDI(diTextStart);
292 ME_DisplayItem *p2 = ME_MakeDI(diTextEnd);
298 p1->member.para.next_para = p2;
299 p2->member.para.prev_para = p1;
300 p2->member.para.nCharOfs = 0;
304 buf->pCharStyle = NULL;
309 #define STREAMIN_BUFFER_SIZE 1024
311 static LRESULT ME_StreamInText(ME_TextEditor *editor, DWORD dwFormat, EDITSTREAM *stream, ME_Style *style)
313 BYTE buffer[STREAMIN_BUFFER_SIZE+1];
314 WCHAR wszText[STREAMIN_BUFFER_SIZE+1];
316 TRACE("%08lx %p\n", dwFormat, stream);
320 long nDataSize = 0, nWideChars = 0;
321 stream->dwError = stream->pfnCallback(stream->dwCookie,
322 (dwFormat & SF_UNICODE ? (BYTE *)wszText : buffer),
323 STREAMIN_BUFFER_SIZE, &nDataSize);
330 if (!(dwFormat & SF_UNICODE))
332 /* FIXME? this is doomed to fail on true MBCS like UTF-8, luckily they're unlikely to be used as CP_ACP */
333 nWideChars = MultiByteToWideChar(CP_ACP, 0, buffer, nDataSize, wszText, STREAMIN_BUFFER_SIZE);
336 nWideChars = nDataSize>>1;
337 ME_InsertTextFromCursor(editor, 0, wszText, nWideChars, style);
338 if (nDataSize<STREAMIN_BUFFER_SIZE)
341 ME_CommitUndo(editor);
345 static LRESULT ME_StreamIn(ME_TextEditor *editor, DWORD format, EDITSTREAM *stream)
350 FIXME("%08lx %p\n", format, stream);
351 TRACE("%p %p\n", stream, editor->hWnd);
353 if (format & SFF_SELECTION) {
354 style = ME_GetSelectionInsertStyle(editor);
355 SendMessageW(editor->hWnd, WM_CLEAR, 0, 0);
358 style = editor->pBuffer->pDefaultStyle;
359 ME_AddRefStyle(style);
360 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
361 SetWindowTextA(editor->hWnd, "");
362 ME_ClearTempStyle(editor);
365 if (format & SF_RTF) {
366 /* setup the RTF parser */
367 memset(&parser, 0, sizeof parser);
368 RTFSetEditStream(&parser, stream);
369 parser.rtfFormat = format&(SF_TEXT|SF_RTF);
370 parser.hwndEdit = editor->hWnd;
377 RTFFlushOutputBuffer(&parser);
379 else if (format & SF_TEXT)
380 ME_StreamInText(editor, format, stream, style);
382 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
383 /* put the cursor at the top */
384 if (!(format & SFF_SELECTION))
385 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
388 /* FIXME where to put cursor now ? */
390 ME_ReleaseStyle(style);
395 ME_TextEditor *ME_MakeEditor(HWND hWnd) {
396 ME_TextEditor *ed = ALLOC_OBJ(ME_TextEditor);
400 ed->pBuffer = ME_MakeText();
402 ME_MakeFirstParagraph(hDC, ed->pBuffer);
403 ReleaseDC(hWnd, hDC);
404 ed->bCaretShown = FALSE;
406 ed->pCursors = ALLOC_N_OBJ(ME_Cursor, ed->nCursors);
407 ed->pCursors[0].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
408 ed->pCursors[0].nOffset = 0;
409 ed->pCursors[1].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
410 ed->pCursors[1].nOffset = 0;
411 ed->nTotalLength = 0;
415 ed->rgbBackColor = -1;
416 ed->bCaretAtEnd = FALSE;
419 ed->pUndoStack = ed->pRedoStack = NULL;
420 ed->nUndoMode = umAddToUndo;
422 for (i=0; i<HFONT_CACHE_SIZE; i++)
424 ed->pFontCache[i].nRefs = 0;
425 ed->pFontCache[i].nAge = 0;
426 ed->pFontCache[i].hFont = NULL;
428 ME_CheckCharOffsets(ed);
432 void ME_DestroyEditor(ME_TextEditor *editor)
434 ME_DisplayItem *pFirst = editor->pBuffer->pFirst;
435 ME_DisplayItem *p = pFirst, *pNext = NULL;
438 ME_ClearTempStyle(editor);
439 ME_EmptyUndoStack(editor);
442 ME_DestroyDisplayItem(p);
445 ME_ReleaseStyle(editor->pBuffer->pDefaultStyle);
446 for (i=0; i<HFONT_CACHE_SIZE; i++)
448 if (editor->pFontCache[i].hFont)
449 DeleteObject(editor->pFontCache[i].hFont);
455 #define UNSUPPORTED_MSG(e) \
457 FIXME(#e ": stub\n"); \
458 return DefWindowProcW(hWnd, msg, wParam, lParam);
460 /******************************************************************
461 * RichEditANSIWndProc (RICHED20.10)
463 LRESULT WINAPI RichEditANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
467 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongW(hWnd, 0);
470 UNSUPPORTED_MSG(EM_AUTOURLDETECT)
471 UNSUPPORTED_MSG(EM_CANPASTE)
472 UNSUPPORTED_MSG(EM_CHARFROMPOS)
473 UNSUPPORTED_MSG(EM_DISPLAYBAND)
474 UNSUPPORTED_MSG(EM_EXLIMITTEXT)
475 UNSUPPORTED_MSG(EM_EXLINEFROMCHAR)
476 UNSUPPORTED_MSG(EM_FINDTEXT)
477 UNSUPPORTED_MSG(EM_FINDTEXTEX)
478 UNSUPPORTED_MSG(EM_FINDWORDBREAK)
479 UNSUPPORTED_MSG(EM_FMTLINES)
480 UNSUPPORTED_MSG(EM_FORMATRANGE)
481 UNSUPPORTED_MSG(EM_GETFIRSTVISIBLELINE)
482 UNSUPPORTED_MSG(EM_GETIMECOMPMODE)
483 /* UNSUPPORTED_MSG(EM_GETIMESTATUS) missing in Wine headers */
484 UNSUPPORTED_MSG(EM_GETLANGOPTIONS)
485 UNSUPPORTED_MSG(EM_GETLIMITTEXT)
486 UNSUPPORTED_MSG(EM_GETLINE)
487 UNSUPPORTED_MSG(EM_GETLINECOUNT)
488 /* UNSUPPORTED_MSG(EM_GETOLEINTERFACE) separate stub */
489 UNSUPPORTED_MSG(EM_GETOPTIONS)
490 UNSUPPORTED_MSG(EM_GETRECT)
491 UNSUPPORTED_MSG(EM_GETREDONAME)
492 UNSUPPORTED_MSG(EM_GETTEXTMODE)
493 UNSUPPORTED_MSG(EM_GETUNDONAME)
494 UNSUPPORTED_MSG(EM_GETWORDBREAKPROC)
495 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX)
496 UNSUPPORTED_MSG(EM_HIDESELECTION)
497 UNSUPPORTED_MSG(EM_LIMITTEXT) /* also known as EM_SETLIMITTEXT */
498 UNSUPPORTED_MSG(EM_LINEFROMCHAR)
499 UNSUPPORTED_MSG(EM_LINEINDEX)
500 UNSUPPORTED_MSG(EM_LINELENGTH)
501 UNSUPPORTED_MSG(EM_LINESCROLL)
502 UNSUPPORTED_MSG(EM_PASTESPECIAL)
503 /* UNSUPPORTED_MSG(EM_POSFROMCHARS) missing in Wine headers */
504 UNSUPPORTED_MSG(EM_REQUESTRESIZE)
505 UNSUPPORTED_MSG(EM_SCROLL)
506 UNSUPPORTED_MSG(EM_SCROLLCARET)
507 UNSUPPORTED_MSG(EM_SELECTIONTYPE)
508 UNSUPPORTED_MSG(EM_SETLANGOPTIONS)
509 UNSUPPORTED_MSG(EM_SETOLECALLBACK)
510 UNSUPPORTED_MSG(EM_SETOPTIONS)
511 UNSUPPORTED_MSG(EM_SETRECT)
512 UNSUPPORTED_MSG(EM_SETRECTNP)
513 UNSUPPORTED_MSG(EM_SETTARGETDEVICE)
514 UNSUPPORTED_MSG(EM_SETTEXTMODE)
515 UNSUPPORTED_MSG(EM_SETUNDOLIMIT)
516 UNSUPPORTED_MSG(EM_SETWORDBREAKPROC)
517 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX)
518 UNSUPPORTED_MSG(EM_STREAMOUT)
519 UNSUPPORTED_MSG(WM_SETFONT)
520 UNSUPPORTED_MSG(WM_PASTE)
521 UNSUPPORTED_MSG(WM_STYLECHANGING)
522 UNSUPPORTED_MSG(WM_STYLECHANGED)
523 /* UNSUPPORTED_MSG(WM_UNICHAR) FIXME missing in Wine headers */
525 /* Messages specific to Richedit controls */
528 return ME_StreamIn(editor, wParam, (EDITSTREAM*)lParam);
531 UINT code = DLGC_WANTCHARS|DLGC_WANTARROWS;
532 if (GetWindowLongW(hWnd, GWL_STYLE)&ES_WANTRETURN)
533 code |= 0; /* FIXME what can we do here ? ask for messages and censor them ? */
538 CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
539 editor = ME_MakeEditor(hWnd);
540 SetWindowLongW(hWnd, 0, (long)editor);
541 pcs = 0; /* ignore */
544 case EM_EMPTYUNDOBUFFER:
545 ME_EmptyUndoStack(editor);
549 ME_GetSelection(editor, (int *)wParam, (int *)lParam);
550 if (!((wParam|lParam) & 0xFFFF0000))
551 return (lParam<<16)|wParam;
556 CHARRANGE *pRange = (CHARRANGE *)lParam;
557 ME_GetSelection(editor, (int *)&pRange->cpMin, (int *)&pRange->cpMax);
561 return editor->pUndoStack != NULL;
563 return editor->pRedoStack != NULL;
572 ME_SetSelection(editor, wParam, lParam);
574 ME_SendSelChange(editor);
579 CHARRANGE *pRange = (CHARRANGE *)lParam;
580 ME_SetSelection(editor, pRange->cpMin, pRange->cpMax);
583 ME_SendSelChange(editor);
586 case EM_SETBKGNDCOLOR:
588 LRESULT lColor = ME_GetBackColor(editor);
590 editor->rgbBackColor = -1;
592 editor->rgbBackColor = lParam;
593 InvalidateRect(hWnd, NULL, TRUE);
598 return editor->nModifyStep == 0 ? 0 : 1;
602 editor->nModifyStep = 0x80000000;
604 editor->nModifyStep = 0;
610 long nStyle = GetWindowLongW(hWnd, GWL_STYLE);
612 nStyle |= ES_READONLY;
614 nStyle &= ~ES_READONLY;
615 SetWindowLongW(hWnd, GWL_STYLE, nStyle);
619 case EM_SETEVENTMASK:
620 editor->nEventMask = lParam;
622 case EM_GETEVENTMASK:
623 return editor->nEventMask;
624 case EM_SETCHARFORMAT:
626 CHARFORMAT2W buf, *p;
627 p = ME_ToCF2W(&buf, (CHARFORMAT2W *)lParam);
629 ME_SetDefaultCharFormat(editor, p);
630 else if (wParam == (SCF_WORD | SCF_SELECTION))
631 FIXME("word selection not supported\n");
632 else if (wParam == SCF_ALL)
633 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), p);
635 ME_SetSelectionCharFormat(editor, p);
636 ME_CommitUndo(editor);
637 ME_UpdateRepaint(editor);
640 case EM_GETCHARFORMAT:
643 tmp.cbSize = sizeof(tmp);
645 ME_GetDefaultCharFormat(editor, &tmp);
647 ME_GetSelectionCharFormat(editor, &tmp);
648 ME_CopyToCFAny((CHARFORMAT2W *)lParam, &tmp);
651 case EM_SETPARAFORMAT:
652 ME_SetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
653 ME_CommitUndo(editor);
655 case EM_GETPARAFORMAT:
656 ME_GetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
661 ME_GetSelection(editor, &from, &to);
662 ME_InternalDeleteText(editor, from, to-from);
663 ME_CommitUndo(editor);
664 ME_UpdateRepaint(editor);
671 LPWSTR wszText = ME_ToUnicode(hWnd, (void *)lParam);
672 size_t len = lstrlenW(wszText);
673 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText));
675 ME_GetSelection(editor, &from, &to);
676 style = ME_GetSelectionInsertStyle(editor);
677 ME_InternalDeleteText(editor, from, to-from);
678 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
679 ME_ReleaseStyle(style);
680 ME_EndToUnicode(hWnd, wszText);
681 /* drop temporary style if line end */
682 /* FIXME question: does abc\n mean: put abc, clear temp style, put \n? (would require a change) */
683 if (len>0 && wszText[len-1] == '\n')
684 ME_ClearTempStyle(editor);
686 ME_CommitUndo(editor);
688 ME_EmptyUndoStack(editor);
689 ME_UpdateRepaint(editor);
694 LPWSTR wszText = ME_ToUnicode(hWnd, (void *)lParam);
695 TRACE("WM_SETTEXT - %s\n", (char *)(wszText)); /* debugstr_w() */
696 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
697 /* uses default style! */
698 ME_InsertTextFromCursor(editor, 0, wszText, -1, editor->pBuffer->pDefaultStyle);
699 ME_EndToUnicode(hWnd, wszText);
700 ME_CommitUndo(editor);
701 ME_EmptyUndoStack(editor);
702 ME_UpdateRepaint(editor);
712 if (!OpenClipboard(hWnd))
716 ME_GetSelection(editor, &from, &to);
717 pars = ME_CountParagraphsBetween(editor, from, to);
718 hData = GlobalAlloc(GMEM_MOVEABLE, sizeof(WCHAR)*(to-from+pars+1));
719 data = (WCHAR *)GlobalLock(hData);
720 ME_GetTextW(editor, data, from, to-from, TRUE);
722 SetClipboardData(CF_UNICODETEXT, hData);
726 ME_InternalDeleteText(editor, from, to-from);
727 ME_CommitUndo(editor);
728 ME_UpdateRepaint(editor);
732 case WM_GETTEXTLENGTH:
733 return ME_GetTextLength(editor);
736 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
738 tr.chrg.cpMax = wParam-1;
739 tr.lpstrText = (WCHAR *)lParam;
740 return RichEditANSIWndProc(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
745 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
746 ME_GetSelection(editor, &from, &to);
747 tr.chrg.cpMin = from;
749 tr.lpstrText = (WCHAR *)lParam;
750 return RichEditANSIWndProc(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
752 case EM_GETTEXTRANGE:
754 TEXTRANGEW *rng = (TEXTRANGEW *)lParam;
755 if (IsWindowUnicode(hWnd))
756 return ME_GetTextW(editor, rng->lpstrText, rng->chrg.cpMin, rng->chrg.cpMax-rng->chrg.cpMin, FALSE);
759 int nLen = rng->chrg.cpMax-rng->chrg.cpMin;
760 WCHAR *p = ALLOC_N_OBJ(WCHAR, nLen+1);
761 int nChars = ME_GetTextW(editor, p, rng->chrg.cpMin, nLen, FALSE);
762 /* FIXME this is a potential security hole (buffer overrun)
763 if you know more about wchar->mbyte conversion please explain
765 WideCharToMultiByte(CP_ACP, 0, p, nChars+1, (char *)rng->lpstrText, nLen+1, NULL, NULL);
769 return ME_GetTextW(editor, rng->lpstrText, rng->chrg.cpMin, rng->chrg.cpMax-rng->chrg.cpMin, FALSE);
772 ME_CommitUndo(editor);
773 /* ME_InsertTextFromCursor(editor, 0, (WCHAR *)L"x", 1, editor->pBuffer->pDefaultStyle); */
775 ME_MoveCaret(editor);
778 ME_DestroyEditor(editor);
779 SetWindowLongW(hWnd, 0, 0);
784 ME_LButtonDown(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
788 if (GetCapture() == hWnd)
789 ME_MouseMove(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
792 if (GetCapture() == hWnd)
796 hDC = BeginPaint(hWnd, &ps);
797 ME_PaintContent(editor, hDC, FALSE);
801 ME_ShowCaret(editor);
802 ME_SendOldNotify(editor, EN_SETFOCUS);
805 ME_HideCaret(editor);
806 ME_SendOldNotify(editor, EN_KILLFOCUS);
810 HDC hDC = (HDC)wParam;
812 COLORREF rgbBG = ME_GetBackColor(editor);
813 if (GetUpdateRect(hWnd,&rc,TRUE))
815 HBRUSH hbr = CreateSolidBrush(rgbBG);
816 FillRect(hDC, &rc, hbr);
822 TRACE("editor wnd command = %d\n", LOWORD(wParam));
825 if (ME_ArrowKey(editor, LOWORD(wParam), GetKeyState(VK_CONTROL)<0)) {
826 ME_CommitUndo(editor);
827 ME_EnsureVisible(editor, editor->pCursors[0].pRun);
829 ME_MoveCaret(editor);
833 if (GetKeyState(VK_CONTROL)<0)
835 if (LOWORD(wParam)=='W')
839 ME_GetSelectionCharFormat(editor, &chf);
840 ME_DumpStyleToBuf(&chf, buf);
841 MessageBoxA(NULL, buf, "Style dump", MB_OK);
843 if (LOWORD(wParam)=='Q')
845 ME_CheckCharOffsets(editor);
852 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY) {
853 MessageBeep(MB_ICONERROR);
854 return 0; /* FIXME really 0 ? */
856 wstr = LOWORD(wParam);
857 if (((unsigned)wstr)>=' ' || wstr=='\r') {
858 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
859 ME_Style *style = ME_GetInsertStyle(editor, 0);
860 ME_SaveTempStyle(editor);
861 ME_InsertTextFromCursor(editor, 0, &wstr, 1, style);
862 ME_ReleaseStyle(style);
863 ME_CommitUndo(editor);
864 ME_UpdateRepaint(editor);
870 si.cbSize = sizeof(SCROLLINFO);
871 si.fMask = SIF_PAGE|SIF_POS|SIF_RANGE|SIF_TRACKPOS;
872 GetScrollInfo(hWnd, SB_VERT, &si);
873 switch(LOWORD(wParam)) {
875 SetScrollPos(hWnd, SB_VERT, si.nTrackPos, FALSE);
876 ScrollWindow(hWnd, 0, si.nPos-si.nTrackPos, NULL, NULL);
877 /* InvalidateRect(hWnd, NULL, TRUE); */
885 ME_DisplayItem *tp = editor->pBuffer->pFirst;
888 if (tp->type == diParagraph)
890 tp->member.para.nFlags &= ~MEPF_WRAPPED;
891 tp = tp->member.para.next_para;
897 return DefWindowProcW(hWnd, msg, wParam, lParam);
899 case EM_GETOLEINTERFACE:
901 LPVOID *ppvObj = (LPVOID*) lParam;
902 TRACE("EM_GETOLEINTERFACE %p\n", ppvObj);
903 return CreateIRichEditOle(ppvObj);
907 return DefWindowProcW(hWnd, msg, wParam, lParam);
912 /******************************************************************
913 * RichEdit10ANSIWndProc (RICHED20.9)
915 LRESULT WINAPI RichEdit10ANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
917 /* FIXME: this is NOT the same as 2.0 version */
918 return RichEditANSIWndProc(hWnd, msg, wParam, lParam);
921 void ME_SendOldNotify(ME_TextEditor *editor, int nCode)
923 HWND hWnd = editor->hWnd;
924 SendMessageA(GetParent(hWnd), WM_COMMAND, (nCode<<16)|GetWindowLongW(hWnd, GWLP_ID), (LPARAM)hWnd);
927 int ME_CountParagraphsBetween(ME_TextEditor *editor, int from, int to)
929 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
932 while(item && item->member.para.next_para->member.para.nCharOfs <= from)
933 item = item->member.para.next_para;
936 while(item && item->member.para.next_para->member.para.nCharOfs <= to) {
937 item = item->member.para.next_para;
943 int ME_GetTextW(ME_TextEditor *editor, WCHAR *buffer, int nStart, int nChars, int bCRLF)
945 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
948 while(item && item->member.para.next_para->member.para.nCharOfs <= nStart)
949 item = ME_FindItemFwd(item, diParagraph);
954 nStart -= item->member.para.nCharOfs;
957 item = ME_FindItemFwd(item, diRun);
958 } while(item && (item->member.run.nCharOfs + ME_StrLen(item->member.run.strText) <= nStart));
961 nStart -= item->member.run.nCharOfs;
965 int nLen = ME_StrLen(item->member.run.strText) - nStart;
968 CopyMemory(buffer, item->member.run.strText->szData + nStart, sizeof(WCHAR)*nLen);
975 item = ME_FindItemFwd(item, diRun);
978 while(nChars && item)
980 int nLen = ME_StrLen(item->member.run.strText);
984 if (item->member.run.nFlags & MERF_ENDPARA)
994 CopyMemory(buffer, item->member.run.strText->szData, sizeof(WCHAR)*nLen);
1004 item = ME_FindItemFwd(item, diRun);
1010 static WCHAR wszClassName[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '2', '0', 'W', 0};
1011 static WCHAR wszClassName50[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '5', '0', 'W', 0};
1013 void ME_RegisterEditorClass(HINSTANCE hInstance)
1019 wcW.style = CS_HREDRAW | CS_VREDRAW;
1020 wcW.lpfnWndProc = RichEditANSIWndProc;
1023 wcW.hInstance = NULL; /* hInstance would register DLL-local class */
1025 wcW.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
1026 wcW.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
1027 wcW.lpszMenuName = NULL;
1028 wcW.lpszClassName = wszClassName;
1029 bResult = RegisterClassW(&wcW);
1031 wcW.lpszClassName = wszClassName50;
1032 bResult = RegisterClassW(&wcW);
1035 wcA.style = CS_HREDRAW | CS_VREDRAW;
1036 wcA.lpfnWndProc = RichEditANSIWndProc;
1039 wcA.hInstance = NULL; /* hInstance would register DLL-local class */
1041 wcA.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
1042 wcA.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
1043 wcA.lpszMenuName = NULL;
1044 wcA.lpszClassName = "RichEdit20A";
1045 bResult = RegisterClassA(&wcA);
1047 wcA.lpszClassName = "RichEdit50A";
1048 bResult = RegisterClassA(&wcA);
1052 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
1057 case DLL_PROCESS_ATTACH:
1058 DisableThreadLibraryCalls(hinstDLL);
1059 me_heap = HeapCreate (0, 0x10000, 0);
1060 ME_RegisterEditorClass(hinstDLL);
1063 case DLL_PROCESS_DETACH:
1064 UnregisterClassW(wszClassName, 0);
1065 UnregisterClassW(wszClassName50, 0);
1066 UnregisterClassA("RichEdit20A", 0);
1067 UnregisterClassA("RichEdit50A", 0);
1068 HeapDestroy (me_heap);
1075 /******************************************************************
1076 * CreateTextServices (RICHED20.4)
1078 * FIXME should be ITextHost instead of void*
1080 HRESULT WINAPI CreateTextServices(IUnknown *punkOuter, void *pITextHost,
1084 /* FIXME should support aggregation */
1086 return CLASS_E_NOAGGREGATION;
1088 return E_FAIL; /* E_NOTIMPL isn't allowed by MSDN */
1091 /******************************************************************
1092 * REExtendedRegisterClass (RICHED20.8)
1094 * FIXME undocumented
1096 void WINAPI REExtendedRegisterClass(void)