Removed buffer overrun error (incrementing output pointer one time too
[wine/wine-kai.git] / dlls / riched20 / editor.c
blobb572d370ec6c2957884f8363284e27d58a1a10e5
1 /*
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
21 /*
22 API implementation status:
24 Messages (ANSI versions not done yet)
25 - EM_AUTOURLDETECT 2.0
26 + EM_CANPASTE
27 + EM_CANREDO 2.0
28 + EM_CANUNDO
29 + EM_CHARFROMPOS
30 - EM_DISPLAYBAND
31 + EM_EMPTYUNDOBUFFER
32 + EM_EXGETSEL
33 - EM_EXLIMITTEXT
34 + EM_EXLINEFROMCHAR
35 + EM_EXSETSEL
36 + EM_FINDTEXT (only FR_DOWN flag implemented)
37 + EM_FINDTEXTEX (only FR_DOWN flag implemented)
38 - EM_FINDWORDBREAK
39 - EM_FMTLINES
40 - EM_FORMATRANGE
41 - EM_GETAUTOURLDETECT 2.0
42 - EM_GETBIDIOPTIONS 3.0
43 - EM_GETCHARFORMAT (partly done)
44 - EM_GETEDITSTYLE
45 + EM_GETEVENTMASK
46 + EM_GETFIRSTVISIBLELINE (can be optimized if needed)
47 - EM_GETIMECOLOR 1.0asian
48 - EM_GETIMECOMPMODE 2.0
49 - EM_GETIMEOPTIONS 1.0asian
50 - EM_GETIMESTATUS
51 - EM_GETLANGOPTIONS 2.0
52 - EM_GETLIMITTEXT
53 - EM_GETLINE
54 + EM_GETLINECOUNT returns number of rows, not of paragraphs
55 + EM_GETMODIFY
56 - EM_GETOLEINTERFACE
57 - EM_GETOPTIONS
58 + EM_GETPARAFORMAT
59 - EM_GETPASSWORDCHAR 2.0
60 - EM_GETPUNCTUATION 1.0asian
61 + EM_GETRECT
62 - EM_GETREDONAME 2.0
63 + EM_GETSEL
64 + EM_GETSELTEXT (ANSI&Unicode)
65 - EM_GETSCROLLPOS 3.0
66 ! - EM_GETTHUMB
67 - EM_GETTEXTEX 2.0
68 + EM_GETTEXTLENGTHEX (GTL_PRECISE unimplemented)
69 - EM_GETTEXTMODE 2.0
70 ? + EM_GETTEXTRANGE (ANSI&Unicode)
71 - EM_GETTYPOGRAPHYOPTIONS 3.0
72 - EM_GETUNDONAME
73 - EM_GETWORDBREAKPROC
74 - EM_GETWORDBREAKPROCEX
75 - EM_GETWORDWRAPMODE 1.0asian
76 + EM_GETZOOM 3.0
77 - EM_HIDESELECTION
78 - EM_LIMITTEXT
79 + EM_LINEFROMCHAR
80 + EM_LINEINDEX
81 + EM_LINELENGTH
82 + EM_LINESCROLL
83 - EM_PASTESPECIAL
84 + EM_POSFROMCHAR
85 + EM_REDO 2.0
86 - EM_REQUESTRESIZE
87 + EM_REPLACESEL (proper style?) ANSI&Unicode
88 - EM_SCROLL
89 - EM_SCROLLCARET
90 - EM_SELECTIONTYPE
91 - EM_SETBIDIOPTIONS 3.0
92 + EM_SETBKGNDCOLOR
93 - EM_SETCHARFORMAT (partly done, no ANSI)
94 - EM_SETEDITSTYLE
95 + EM_SETEVENTMASK (few notifications supported)
96 - EM_SETFONTSIZE
97 - EM_SETIMECOLOR 1.0asian
98 - EM_SETIMEOPTIONS 1.0asian
99 - EM_SETLANGOPTIONS 2.0
100 - EM_SETLIMITTEXT
101 + EM_SETMODIFY (not sure if implementation is correct)
102 - EM_SETOLECALLBACK
103 - EM_SETOPTIONS
104 - EM_SETPALETTE 2.0
105 + EM_SETPARAFORMAT
106 - EM_SETPASSWORDCHAR 2.0
107 - EM_SETPUNCTUATION 1.0asian
108 + EM_SETREADONLY no beep on modification attempt
109 + EM_SETRECT
110 + EM_SETRECTNP (EM_SETRECT without repainting)
111 + EM_SETSEL
112 - EM_SETSCROLLPOS 3.0
113 - EM_SETTABSTOPS 3.0
114 - EM_SETTARGETDEVICE
115 + EM_SETTEXTEX 3.0 (unicode only, no rich text insertion handling, proper style?)
116 - EM_SETTEXTMODE 2.0
117 - EM_SETTYPOGRAPHYOPTIONS 3.0
118 - EM_SETUNDOLIMIT 2.0
119 - EM_SETWORDBREAKPROC
120 - EM_SETWORDBREAKPROCEX
121 - EM_SETWORDWRAPMODE 1.0asian
122 + EM_SETZOOM 3.0
123 - EM_SHOWSCROLLBAR 2.0
124 - EM_STOPGROUPTYPING 2.0
125 + EM_STREAMIN
126 + EM_STREAMOUT
127 + EM_UNDO
128 + WM_CHAR
129 + WM_CLEAR
130 + WM_COPY
131 + WM_CUT
132 + WM_GETDLGCODE (the current implementation is incomplete)
133 + WM_GETTEXT (ANSI&Unicode)
134 + WM_GETTEXTLENGTH (ANSI version sucks)
135 + WM_PASTE
136 - WM_SETFONT
137 + WM_SETTEXT (resets undo stack !) (proper style?) ANSI&Unicode
138 - WM_STYLECHANGING
139 - WM_STYLECHANGED (things like read-only flag)
140 - WM_UNICHAR
142 Notifications
144 * EN_CHANGE (sent from the wrong place)
145 - EN_CORRECTTEXT
146 - EN_DROPFILES
147 - EN_ERRSPACE
148 - EN_HSCROLL
149 - EN_IMECHANGE
150 + EN_KILLFOCUS
151 - EN_LINK
152 - EN_MAXTEXT
153 - EN_MSGFILTER
154 - EN_OLEOPFAILED
155 - EN_PROTECTED
156 - EN_REQUESTRESIZE
157 - EN_SAVECLIPBOARD
158 + EN_SELCHANGE
159 + EN_SETFOCUS
160 - EN_STOPNOUNDO
161 * EN_UPDATE (sent from the wrong place)
162 - EN_VSCROLL
164 Styles
166 - ES_AUTOHSCROLL
167 - ES_AUTOVSCROLL
168 - ES_CENTER
169 - ES_DISABLENOSCROLL (scrollbar is always visible)
170 - ES_EX_NOCALLOLEINIT
171 - ES_LEFT
172 - ES_MULTILINE (currently single line controls aren't supported)
173 - ES_NOIME
174 - ES_READONLY (I'm not sure if beeping is the proper behaviour)
175 - ES_RIGHT
176 - ES_SAVESEL
177 - ES_SELFIME
178 - ES_SUNKEN
179 - ES_VERTICAL
180 - ES_WANTRETURN (don't know how to do WM_GETDLGCODE part)
181 - WS_SETFONT
182 - WS_HSCROLL
183 - WS_VSCROLL
187 * RICHED20 TODO (incomplete):
189 * - messages/styles/notifications listed above
190 * - Undo coalescing
191 * - add remaining CHARFORMAT/PARAFORMAT fields
192 * - right/center align should strip spaces from the beginning
193 * - more advanced navigation (Ctrl-arrows)
194 * - tabs
195 * - pictures/OLE objects (not just smiling faces that lack API support ;-) )
196 * - COM interface (looks like a major pain in the TODO list)
197 * - calculate heights of pictures (half-done)
198 * - horizontal scrolling (not even started)
199 * - hysteresis during wrapping (related to scrollbars appearing/disappearing)
200 * - find/replace
201 * - how to implement EM_FORMATRANGE and EM_DISPLAYBAND ? (Mission Impossible)
202 * - italic caret with italic fonts
203 * - IME
204 * - most notifications aren't sent at all (the most important ones are)
205 * - when should EN_SELCHANGE be sent after text change ? (before/after EN_UPDATE?)
206 * - WM_SETTEXT may use wrong style (but I'm 80% sure it's OK)
207 * - EM_GETCHARFORMAT with SCF_SELECTION may not behave 100% like in original (but very close)
208 * - full justification
209 * - hyphenation
210 * - tables
212 * Bugs that are probably fixed, but not so easy to verify:
213 * - EN_UPDATE/EN_CHANGE are handled very incorrectly (should be OK now)
214 * - undo for ME_JoinParagraphs doesn't store paragraph format ? (it does)
215 * - check/fix artificial EOL logic (bCursorAtEnd, hardly logical)
216 * - caret shouldn't be displayed when selection isn't empty
217 * - check refcounting in style management functions (looks perfect now, but no bugs is suspicious)
218 * - undo for setting default format (done, might be buggy)
219 * - styles might be not released properly (looks like they work like charm, but who knows?
223 #include "editor.h"
224 #include "commdlg.h"
225 #include "ole2.h"
226 #include "richole.h"
227 #include "winreg.h"
228 #define NO_SHLWAPI_STREAM
229 #include "shlwapi.h"
231 #include "rtf.h"
233 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
235 int me_debug = 0;
236 HANDLE me_heap = NULL;
238 static ME_TextBuffer *ME_MakeText(void) {
240 ME_TextBuffer *buf = ALLOC_OBJ(ME_TextBuffer);
242 ME_DisplayItem *p1 = ME_MakeDI(diTextStart);
243 ME_DisplayItem *p2 = ME_MakeDI(diTextEnd);
245 p1->prev = NULL;
246 p1->next = p2;
247 p2->prev = p1;
248 p2->next = NULL;
249 p1->member.para.next_para = p2;
250 p2->member.para.prev_para = p1;
251 p2->member.para.nCharOfs = 0;
253 buf->pFirst = p1;
254 buf->pLast = p2;
255 buf->pCharStyle = NULL;
257 return buf;
261 static LRESULT ME_StreamInText(ME_TextEditor *editor, DWORD dwFormat, ME_InStream *stream, ME_Style *style)
263 WCHAR wszText[STREAMIN_BUFFER_SIZE+1];
264 WCHAR *pText;
266 TRACE("%08lx %p\n", dwFormat, stream);
268 do {
269 long nWideChars = 0;
271 if (!stream->dwSize)
273 ME_StreamInFill(stream);
274 if (stream->editstream->dwError)
275 break;
276 if (!stream->dwSize)
277 break;
280 if (!(dwFormat & SF_UNICODE))
282 /* FIXME? this is doomed to fail on true MBCS like UTF-8, luckily they're unlikely to be used as CP_ACP */
283 nWideChars = MultiByteToWideChar(CP_ACP, 0, stream->buffer, stream->dwSize, wszText, STREAMIN_BUFFER_SIZE);
284 pText = wszText;
286 else
288 nWideChars = stream->dwSize >> 1;
289 pText = (WCHAR *)stream->buffer;
292 ME_InsertTextFromCursor(editor, 0, pText, nWideChars, style);
293 if (stream->dwSize == 0)
294 break;
295 stream->dwSize = 0;
296 } while(1);
297 ME_CommitUndo(editor);
298 ME_Repaint(editor);
299 return 0;
302 static void ME_RTFCharAttrHook(RTF_Info *info)
304 CHARFORMAT2W fmt;
305 fmt.cbSize = sizeof(fmt);
306 fmt.dwMask = 0;
308 switch(info->rtfMinor)
310 case rtfPlain:
311 /* FIXME add more flags once they're implemented */
312 fmt.dwMask = CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_COLOR | CFM_BACKCOLOR | CFM_SIZE | CFM_WEIGHT;
313 fmt.dwEffects = CFE_AUTOCOLOR | CFE_AUTOBACKCOLOR;
314 fmt.yHeight = 12*20; /* 12pt */
315 fmt.wWeight = 400;
316 break;
317 case rtfBold:
318 fmt.dwMask = CFM_BOLD;
319 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
320 break;
321 case rtfItalic:
322 fmt.dwMask = CFM_ITALIC;
323 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
324 break;
325 case rtfUnderline:
326 fmt.dwMask = CFM_UNDERLINE;
327 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
328 fmt.bUnderlineType = CFU_CF1UNDERLINE;
329 break;
330 case rtfNoUnderline:
331 fmt.dwMask = CFM_UNDERLINE;
332 fmt.dwEffects = 0;
333 break;
334 case rtfStrikeThru:
335 fmt.dwMask = CFM_STRIKEOUT;
336 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
337 break;
338 case rtfSubScript:
339 case rtfSuperScript:
340 case rtfSubScrShrink:
341 case rtfSuperScrShrink:
342 case rtfNoSuperSub:
343 fmt.dwMask = CFM_SUBSCRIPT|CFM_SUPERSCRIPT;
344 if (info->rtfMinor == rtfSubScrShrink) fmt.dwEffects = CFE_SUBSCRIPT;
345 if (info->rtfMinor == rtfSuperScrShrink) fmt.dwEffects = CFE_SUPERSCRIPT;
346 if (info->rtfMinor == rtfNoSuperSub) fmt.dwEffects = 0;
347 break;
348 case rtfBackColor:
349 fmt.dwMask = CFM_BACKCOLOR;
350 fmt.dwEffects = 0;
351 if (info->rtfParam == 0)
352 fmt.dwEffects = CFE_AUTOBACKCOLOR;
353 else if (info->rtfParam != rtfNoParam)
355 RTFColor *c = RTFGetColor(info, info->rtfParam);
356 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
358 break;
359 case rtfForeColor:
360 fmt.dwMask = CFM_COLOR;
361 fmt.dwEffects = 0;
362 if (info->rtfParam == 0)
363 fmt.dwEffects = CFE_AUTOCOLOR;
364 else if (info->rtfParam != rtfNoParam)
366 RTFColor *c = RTFGetColor(info, info->rtfParam);
367 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
369 break;
370 case rtfFontNum:
371 if (info->rtfParam != rtfNoParam)
373 RTFFont *f = RTFGetFont(info, info->rtfParam);
374 if (f)
376 MultiByteToWideChar(CP_ACP, 0, f->rtfFName, -1, fmt.szFaceName, sizeof(fmt.szFaceName)/sizeof(WCHAR));
377 fmt.szFaceName[sizeof(fmt.szFaceName)/sizeof(WCHAR)-1] = '\0';
378 fmt.bCharSet = f->rtfFCharSet;
379 fmt.dwMask = CFM_FACE | CFM_CHARSET;
382 break;
383 case rtfFontSize:
384 fmt.dwMask = CFM_SIZE;
385 if (info->rtfParam != rtfNoParam)
386 fmt.yHeight = info->rtfParam*10;
387 break;
389 if (fmt.dwMask) {
390 ME_Style *style2;
391 RTFFlushOutputBuffer(info);
392 /* FIXME too slow ? how come ? */
393 style2 = ME_ApplyStyle(info->style, &fmt);
394 ME_ReleaseStyle(info->style);
395 info->style = style2;
396 info->styleChanged = TRUE;
400 /* FIXME this function doesn't get any information about context of the RTF tag, which is very bad,
401 the same tags mean different things in different contexts */
402 static void ME_RTFParAttrHook(RTF_Info *info)
404 PARAFORMAT2 fmt;
405 fmt.cbSize = sizeof(fmt);
406 fmt.dwMask = 0;
408 switch(info->rtfMinor)
410 case rtfParDef: /* I'm not 100% sure what does it do, but I guess it restores default paragraph attributes */
411 fmt.dwMask = PFM_ALIGNMENT | PFM_TABSTOPS | PFM_OFFSET | PFM_STARTINDENT;
412 fmt.wAlignment = PFA_LEFT;
413 fmt.cTabCount = 0;
414 fmt.dxOffset = fmt.dxStartIndent = 0;
415 break;
416 case rtfFirstIndent:
417 ME_GetSelectionParaFormat(info->editor, &fmt);
418 fmt.dwMask = PFM_STARTINDENT;
419 fmt.dxStartIndent = info->rtfParam + fmt.dxOffset;
420 break;
421 case rtfLeftIndent:
423 int first, left;
424 ME_GetSelectionParaFormat(info->editor, &fmt);
425 first = fmt.dxStartIndent;
426 left = info->rtfParam;
427 fmt.dwMask = PFM_STARTINDENT|PFM_OFFSET;
428 fmt.dxStartIndent = first + left;
429 fmt.dxOffset = -first;
430 break;
432 case rtfRightIndent:
433 fmt.dwMask = PFM_RIGHTINDENT;
434 fmt.dxRightIndent = info->rtfParam;
435 break;
436 case rtfQuadLeft:
437 case rtfQuadJust:
438 fmt.dwMask = PFM_ALIGNMENT;
439 fmt.wAlignment = PFA_LEFT;
440 break;
441 case rtfQuadRight:
442 fmt.dwMask = PFM_ALIGNMENT;
443 fmt.wAlignment = PFA_RIGHT;
444 break;
445 case rtfQuadCenter:
446 fmt.dwMask = PFM_ALIGNMENT;
447 fmt.wAlignment = PFA_CENTER;
448 break;
449 case rtfTabPos:
450 ME_GetSelectionParaFormat(info->editor, &fmt);
451 if (!(fmt.dwMask & PFM_TABSTOPS))
453 fmt.dwMask |= PFM_TABSTOPS;
454 fmt.cTabCount = 0;
456 if (fmt.cTabCount < MAX_TAB_STOPS)
457 fmt.rgxTabs[fmt.cTabCount++] = info->rtfParam;
458 break;
460 if (fmt.dwMask) {
461 RTFFlushOutputBuffer(info);
462 /* FIXME too slow ? how come ?*/
463 ME_SetSelectionParaFormat(info->editor, &fmt);
467 static void ME_RTFReadHook(RTF_Info *info) {
468 switch(info->rtfClass)
470 case rtfGroup:
471 switch(info->rtfMajor)
473 case rtfBeginGroup:
474 if (info->stackTop < maxStack) {
475 memcpy(&info->stack[info->stackTop].fmt, &info->style->fmt, sizeof(CHARFORMAT2W));
476 info->stack[info->stackTop].codePage = info->codePage;
477 info->stack[info->stackTop].unicodeLength = info->unicodeLength;
479 info->stackTop++;
480 info->styleChanged = FALSE;
481 break;
482 case rtfEndGroup:
484 ME_Style *s;
485 RTFFlushOutputBuffer(info);
486 if (info->stackTop<=1) {
487 info->rtfClass = rtfEOF;
488 return;
490 info->stackTop--;
491 assert(info->stackTop >= 0);
492 if (info->styleChanged)
494 /* FIXME too slow ? how come ? */
495 s = ME_ApplyStyle(info->style, &info->stack[info->stackTop].fmt);
496 ME_ReleaseStyle(info->style);
497 info->style = s;
498 info->codePage = info->stack[info->stackTop].codePage;
499 info->unicodeLength = info->stack[info->stackTop].unicodeLength;
501 break;
504 break;
505 case rtfControl:
506 switch(info->rtfMajor)
508 case rtfCharAttr:
509 ME_RTFCharAttrHook(info);
510 break;
511 case rtfParAttr:
512 ME_RTFParAttrHook(info);
513 break;
515 break;
519 void
520 ME_StreamInFill(ME_InStream *stream)
522 stream->editstream->dwError = stream->editstream->pfnCallback(stream->editstream->dwCookie,
523 (BYTE *)stream->buffer,
524 sizeof(stream->buffer),
525 (LONG *)&stream->dwSize);
526 stream->dwUsed = 0;
529 static LRESULT ME_StreamIn(ME_TextEditor *editor, DWORD format, EDITSTREAM *stream)
531 RTF_Info parser;
532 ME_Style *style;
533 int from, to, to2, nUndoMode;
534 ME_UndoItem *pUI;
535 int nEventMask = editor->nEventMask;
536 ME_InStream inStream;
538 TRACE("stream==%p hWnd==%p format==0x%X\n", stream, editor->hWnd, (UINT)format);
539 editor->nEventMask = 0;
541 ME_GetSelection(editor, &from, &to);
542 if (format & SFF_SELECTION) {
543 style = ME_GetSelectionInsertStyle(editor);
545 ME_InternalDeleteText(editor, from, to-from);
547 else {
548 style = editor->pBuffer->pDefaultStyle;
549 ME_AddRefStyle(style);
550 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
551 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
552 from = to = 0;
553 ME_ClearTempStyle(editor);
554 /* FIXME restore default paragraph formatting ! */
557 nUndoMode = editor->nUndoMode;
558 editor->nUndoMode = umIgnore;
560 inStream.editstream = stream;
561 inStream.editstream->dwError = 0;
562 inStream.dwSize = 0;
563 inStream.dwUsed = 0;
565 if (format & SF_RTF)
567 /* Check if it's really RTF, and if it is not, use plain text */
568 ME_StreamInFill(&inStream);
569 if (!inStream.editstream->dwError)
571 if (strncmp(inStream.buffer, "{\\rtf1", 6) && strncmp(inStream.buffer, "{\\urtf", 6))
573 format &= ~SF_RTF;
574 format |= SF_TEXT;
579 if (!inStream.editstream->dwError)
581 if (format & SF_RTF) {
582 /* setup the RTF parser */
583 memset(&parser, 0, sizeof parser);
584 RTFSetEditStream(&parser, &inStream);
585 parser.rtfFormat = format&(SF_TEXT|SF_RTF);
586 parser.hwndEdit = editor->hWnd;
587 parser.editor = editor;
588 parser.style = style;
589 WriterInit(&parser);
590 RTFInit(&parser);
591 RTFSetReadHook(&parser, ME_RTFReadHook);
592 BeginFile(&parser);
594 /* do the parsing */
595 RTFRead(&parser);
596 RTFFlushOutputBuffer(&parser);
597 RTFDestroy(&parser);
599 style = parser.style;
601 else if (format & SF_TEXT)
602 ME_StreamInText(editor, format, &inStream, style);
603 else
604 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
605 ME_GetSelection(editor, &to, &to2);
606 /* put the cursor at the top */
607 if (!(format & SFF_SELECTION))
608 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
609 else
611 /* FIXME where to put cursor now ? */
615 editor->nUndoMode = nUndoMode;
616 pUI = ME_AddUndoItem(editor, diUndoDeleteRun, NULL);
617 TRACE("from %d to %d\n", from, to);
618 if (pUI && from < to)
620 pUI->nStart = from;
621 pUI->nLen = to-from;
623 ME_CommitUndo(editor);
624 ME_ReleaseStyle(style);
625 editor->nEventMask = nEventMask;
626 if (editor->bRedraw)
628 InvalidateRect(editor->hWnd, NULL, TRUE);
629 ME_UpdateRepaint(editor);
631 if (!(format & SFF_SELECTION)) {
632 ME_ClearTempStyle(editor);
634 ME_MoveCaret(editor);
635 ME_SendSelChange(editor);
637 return 0;
641 ME_DisplayItem *
642 ME_FindItemAtOffset(ME_TextEditor *editor, ME_DIType nItemType, int nOffset, int *nItemOffset)
644 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
646 while (item && item->member.para.next_para->member.para.nCharOfs <= nOffset)
647 item = ME_FindItemFwd(item, diParagraph);
649 if (!item)
650 return item;
652 nOffset -= item->member.para.nCharOfs;
653 if (nItemType == diParagraph) {
654 if (nItemOffset)
655 *nItemOffset = nOffset;
656 return item;
659 do {
660 item = ME_FindItemFwd(item, diRun);
661 } while (item && (item->member.run.nCharOfs + ME_StrLen(item->member.run.strText) <= nOffset));
662 if (item) {
663 nOffset -= item->member.run.nCharOfs;
664 if (nItemOffset)
665 *nItemOffset = nOffset;
667 return item;
671 static int
672 ME_FindText(ME_TextEditor *editor, DWORD flags, CHARRANGE *chrg, WCHAR *text, CHARRANGE *chrgText)
674 int nStart, nEnd;
675 int nLen = lstrlenW(text);
676 int nMin, nMax;
677 ME_DisplayItem *item;
678 ME_DisplayItem *para;
680 TRACE("flags==0x%08lx, chrg->cpMin==%ld, chrg->cpMax==%ld text==%s\n",
681 flags, chrg->cpMin, chrg->cpMax, debugstr_w(text));
683 if (!(flags & FR_MATCHCASE))
684 FIXME("Case-insensitive search not implemented\n");
685 if (flags & ~(FR_DOWN | FR_MATCHCASE))
686 FIXME("Flags 0x%08lx not implemented\n", flags & ~(FR_DOWN | FR_MATCHCASE));
688 if (chrg->cpMax == -1)
690 nMin = chrg->cpMin;
691 nMax = ME_GetTextLength(editor);
693 else
695 nMin = min(chrg->cpMin, chrg->cpMax);
696 nMax = max(chrg->cpMin, chrg->cpMax);
699 if (!nLen)
701 if (chrgText)
702 chrgText->cpMin = chrgText->cpMax = ((flags & FR_DOWN) ? nMin : nMax);
703 return chrgText->cpMin;
706 if (flags & FR_DOWN) /* Forward search */
708 nStart = nMin;
709 item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
710 if (!item)
711 return -1;
713 para = ME_GetParagraph(item);
714 while (item
715 && para->member.para.nCharOfs + item->member.run.nCharOfs + nStart + nLen < nMax)
717 ME_DisplayItem *pCurItem = item;
718 int nCurStart = nStart;
719 int nMatched = 0;
721 while (pCurItem && pCurItem->member.run.strText->szData[nCurStart + nMatched] == text[nMatched])
723 nMatched++;
724 if (nMatched == nLen)
726 nStart += para->member.para.nCharOfs + item->member.run.nCharOfs;
727 if (chrgText)
729 chrgText->cpMin = nStart;
730 chrgText->cpMax = nStart + nLen;
732 TRACE("found at %d-%d\n", nStart, nStart + nLen);
733 return nStart;
735 if (nCurStart + nMatched == ME_StrLen(pCurItem->member.run.strText))
737 pCurItem = ME_FindItemFwd(pCurItem, diRun);
738 para = ME_GetParagraph(pCurItem);
739 nCurStart = -nMatched;
742 nStart++;
743 if (nStart == ME_StrLen(item->member.run.strText))
745 item = ME_FindItemFwd(item, diRun);
746 para = ME_GetParagraph(item);
747 nStart = 0;
751 else /* Backward search */
753 nEnd = nMax;
754 item = ME_FindItemAtOffset(editor, diRun, nEnd, &nEnd);
755 if (!item)
756 return -1;
758 para = ME_GetParagraph(item);
760 while (item
761 && para->member.para.nCharOfs + item->member.run.nCharOfs + nEnd - nLen >= nMin)
763 ME_DisplayItem *pCurItem = item;
764 int nCurEnd = nEnd;
765 int nMatched = 0;
767 while (pCurItem && pCurItem->member.run.strText->szData[nCurEnd - nMatched - 1] == text[nLen - nMatched - 1])
769 nMatched++;
770 if (nMatched == nLen)
772 nStart = para->member.para.nCharOfs + item->member.run.nCharOfs + nCurEnd - nMatched;
773 if (chrgText)
775 chrgText->cpMin = nStart;
776 chrgText->cpMax = nStart + nLen;
778 TRACE("found at %d-%d\n", nStart, nStart + nLen);
779 return nStart;
781 if (nCurEnd - nMatched == 0)
783 pCurItem = ME_FindItemBack(pCurItem, diRun);
784 para = ME_GetParagraph(pCurItem);
785 nCurEnd = ME_StrLen(pCurItem->member.run.strText) + nMatched;
788 nEnd--;
789 if (nEnd < 0)
791 item = ME_FindItemBack(item, diRun);
792 para = ME_GetParagraph(item);
793 nEnd = ME_StrLen(item->member.run.strText);
797 TRACE("not found\n");
798 return -1;
802 ME_TextEditor *ME_MakeEditor(HWND hWnd) {
803 ME_TextEditor *ed = ALLOC_OBJ(ME_TextEditor);
804 HDC hDC;
805 int i;
806 ed->hWnd = hWnd;
807 ed->bEmulateVersion10 = FALSE;
808 ed->pBuffer = ME_MakeText();
809 hDC = GetDC(hWnd);
810 ME_MakeFirstParagraph(hDC, ed->pBuffer);
811 ReleaseDC(hWnd, hDC);
812 ed->bCaretShown = FALSE;
813 ed->nCursors = 3;
814 ed->pCursors = ALLOC_N_OBJ(ME_Cursor, ed->nCursors);
815 ed->pCursors[0].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
816 ed->pCursors[0].nOffset = 0;
817 ed->pCursors[1].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
818 ed->pCursors[1].nOffset = 0;
819 ed->nLastTotalLength = ed->nTotalLength = 0;
820 ed->nUDArrowX = -1;
821 ed->nSequence = 0;
822 ed->rgbBackColor = -1;
823 ed->bCaretAtEnd = FALSE;
824 ed->nEventMask = 0;
825 ed->nModifyStep = 0;
826 ed->pUndoStack = ed->pRedoStack = NULL;
827 ed->nUndoMode = umAddToUndo;
828 ed->nParagraphs = 1;
829 ed->nLastSelStart = ed->nLastSelEnd = 0;
830 ed->nScrollPosY = 0;
831 ed->nZoomNumerator = ed->nZoomDenominator = 0;
832 ed->bRedraw = TRUE;
833 GetClientRect(hWnd, &ed->rcFormat);
834 for (i=0; i<HFONT_CACHE_SIZE; i++)
836 ed->pFontCache[i].nRefs = 0;
837 ed->pFontCache[i].nAge = 0;
838 ed->pFontCache[i].hFont = NULL;
840 ME_CheckCharOffsets(ed);
841 return ed;
844 typedef struct tagME_GlobalDestStruct
846 HGLOBAL hData;
847 int nLength;
848 } ME_GlobalDestStruct;
850 static DWORD CALLBACK ME_AppendToHGLOBAL(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
852 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
853 int nMaxSize;
854 BYTE *pDest;
856 nMaxSize = GlobalSize(pData->hData);
857 if (pData->nLength+cb+1 >= cb)
859 /* round up to 2^17 */
860 int nNewSize = (((nMaxSize+cb+1)|0x1FFFF)+1) & 0xFFFE0000;
861 pData->hData = GlobalReAlloc(pData->hData, nNewSize, 0);
863 pDest = (BYTE *)GlobalLock(pData->hData);
864 memcpy(pDest + pData->nLength, lpBuff, cb);
865 pData->nLength += cb;
866 pDest[pData->nLength] = '\0';
867 GlobalUnlock(pData->hData);
868 *pcb = cb;
870 return 0;
873 static DWORD CALLBACK ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
875 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
876 int i;
877 WORD *pSrc, *pDest;
879 cb = cb >> 1;
880 pDest = (WORD *)lpBuff;
881 pSrc = (WORD *)GlobalLock(pData->hData);
882 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
883 pDest[i] = pSrc[pData->nLength+i];
885 pData->nLength += i;
886 *pcb = 2*i;
887 GlobalUnlock(pData->hData);
888 return 0;
891 static DWORD CALLBACK ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
893 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
894 int i;
895 BYTE *pSrc, *pDest;
897 pDest = lpBuff;
898 pSrc = (BYTE *)GlobalLock(pData->hData);
899 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
900 pDest[i] = pSrc[pData->nLength+i];
902 pData->nLength += i;
903 *pcb = i;
904 GlobalUnlock(pData->hData);
905 return 0;
909 void ME_DestroyEditor(ME_TextEditor *editor)
911 ME_DisplayItem *pFirst = editor->pBuffer->pFirst;
912 ME_DisplayItem *p = pFirst, *pNext = NULL;
913 int i;
915 ME_ClearTempStyle(editor);
916 ME_EmptyUndoStack(editor);
917 while(p) {
918 pNext = p->next;
919 ME_DestroyDisplayItem(p);
920 p = pNext;
922 ME_ReleaseStyle(editor->pBuffer->pDefaultStyle);
923 for (i=0; i<HFONT_CACHE_SIZE; i++)
925 if (editor->pFontCache[i].hFont)
926 DeleteObject(editor->pFontCache[i].hFont);
929 FREE_OBJ(editor);
932 static WCHAR wszClassName[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '2', '0', 'W', 0};
933 static WCHAR wszClassName50[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '5', '0', 'W', 0};
935 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
937 TRACE("\n");
938 switch (fdwReason)
940 case DLL_PROCESS_ATTACH:
941 DisableThreadLibraryCalls(hinstDLL);
942 me_heap = HeapCreate (0, 0x10000, 0);
943 ME_RegisterEditorClass(hinstDLL);
944 break;
946 case DLL_PROCESS_DETACH:
947 UnregisterClassW(wszClassName, 0);
948 UnregisterClassW(wszClassName50, 0);
949 UnregisterClassA("RichEdit20A", 0);
950 UnregisterClassA("RichEdit50A", 0);
951 HeapDestroy (me_heap);
952 me_heap = NULL;
953 break;
955 return TRUE;
959 #define UNSUPPORTED_MSG(e) \
960 case e: \
961 FIXME(#e ": stub\n"); \
962 return DefWindowProcW(hWnd, msg, wParam, lParam);
964 static const char * const edit_messages[] = {
965 "EM_GETSEL",
966 "EM_SETSEL",
967 "EM_GETRECT",
968 "EM_SETRECT",
969 "EM_SETRECTNP",
970 "EM_SCROLL",
971 "EM_LINESCROLL",
972 "EM_SCROLLCARET",
973 "EM_GETMODIFY",
974 "EM_SETMODIFY",
975 "EM_GETLINECOUNT",
976 "EM_LINEINDEX",
977 "EM_SETHANDLE",
978 "EM_GETHANDLE",
979 "EM_GETTHUMB",
980 "EM_UNKNOWN_BF",
981 "EM_UNKNOWN_C0",
982 "EM_LINELENGTH",
983 "EM_REPLACESEL",
984 "EM_UNKNOWN_C3",
985 "EM_GETLINE",
986 "EM_LIMITTEXT",
987 "EM_CANUNDO",
988 "EM_UNDO",
989 "EM_FMTLINES",
990 "EM_LINEFROMCHAR",
991 "EM_UNKNOWN_CA",
992 "EM_SETTABSTOPS",
993 "EM_SETPASSWORDCHAR",
994 "EM_EMPTYUNDOBUFFER",
995 "EM_GETFIRSTVISIBLELINE",
996 "EM_SETREADONLY",
997 "EM_SETWORDBREAKPROC",
998 "EM_GETWORDBREAKPROC",
999 "EM_GETPASSWORDCHAR",
1000 "EM_SETMARGINS",
1001 "EM_GETMARGINS",
1002 "EM_GETLIMITTEXT",
1003 "EM_POSFROMCHAR",
1004 "EM_CHARFROMPOS"
1007 static const char * const richedit_messages[] = {
1008 "EM_CANPASTE",
1009 "EM_DISPLAYBAND",
1010 "EM_EXGETSEL",
1011 "EM_EXLIMITTEXT",
1012 "EM_EXLINEFROMCHAR",
1013 "EM_EXSETSEL",
1014 "EM_FINDTEXT",
1015 "EM_FORMATRANGE",
1016 "EM_GETCHARFORMAT",
1017 "EM_GETEVENTMASK",
1018 "EM_GETOLEINTERFACE",
1019 "EM_GETPARAFORMAT",
1020 "EM_GETSELTEXT",
1021 "EM_HIDESELECTION",
1022 "EM_PASTESPECIAL",
1023 "EM_REQUESTRESIZE",
1024 "EM_SELECTIONTYPE",
1025 "EM_SETBKGNDCOLOR",
1026 "EM_SETCHARFORMAT",
1027 "EM_SETEVENTMASK",
1028 "EM_SETOLECALLBACK",
1029 "EM_SETPARAFORMAT",
1030 "EM_SETTARGETDEVICE",
1031 "EM_STREAMIN",
1032 "EM_STREAMOUT",
1033 "EM_GETTEXTRANGE",
1034 "EM_FINDWORDBREAK",
1035 "EM_SETOPTIONS",
1036 "EM_GETOPTIONS",
1037 "EM_FINDTEXTEX",
1038 "EM_GETWORDBREAKPROCEX",
1039 "EM_SETWORDBREAKPROCEX",
1040 "EM_SETUNDOLIMIT",
1041 "EM_UNKNOWN_USER_83",
1042 "EM_REDO",
1043 "EM_CANREDO",
1044 "EM_GETUNDONAME",
1045 "EM_GETREDONAME",
1046 "EM_STOPGROUPTYPING",
1047 "EM_SETTEXTMODE",
1048 "EM_GETTEXTMODE",
1049 "EM_AUTOURLDETECT",
1050 "EM_GETAUTOURLDETECT",
1051 "EM_SETPALETTE",
1052 "EM_GETTEXTEX",
1053 "EM_GETTEXTLENGTHEX",
1054 "EM_SHOWSCROLLBAR",
1055 "EM_SETTEXTEX",
1056 "EM_UNKNOWN_USER_98",
1057 "EM_UNKNOWN_USER_99",
1058 "EM_SETPUNCTUATION",
1059 "EM_GETPUNCTUATION",
1060 "EM_SETWORDWRAPMODE",
1061 "EM_GETWORDWRAPMODE",
1062 "EM_SETIMECOLOR",
1063 "EM_GETIMECOLOR",
1064 "EM_SETIMEOPTIONS",
1065 "EM_GETIMEOPTIONS",
1066 "EM_CONVPOSITION",
1067 "EM_UNKNOWN_USER_109",
1068 "EM_UNKNOWN_USER_110",
1069 "EM_UNKNOWN_USER_111",
1070 "EM_UNKNOWN_USER_112",
1071 "EM_UNKNOWN_USER_113",
1072 "EM_UNKNOWN_USER_114",
1073 "EM_UNKNOWN_USER_115",
1074 "EM_UNKNOWN_USER_116",
1075 "EM_UNKNOWN_USER_117",
1076 "EM_UNKNOWN_USER_118",
1077 "EM_UNKNOWN_USER_119",
1078 "EM_SETLANGOPTIONS",
1079 "EM_GETLANGOPTIONS",
1080 "EM_GETIMECOMPMODE",
1081 "EM_FINDTEXTW",
1082 "EM_FINDTEXTEXW",
1083 "EM_RECONVERSION",
1084 "EM_SETIMEMODEBIAS",
1085 "EM_GETIMEMODEBIAS"
1088 static const char *
1089 get_msg_name(UINT msg)
1091 if (msg >= EM_GETSEL && msg <= EM_SETLIMITTEXT)
1092 return edit_messages[msg - EM_GETSEL];
1093 if (msg >= EM_CANPASTE && msg <= EM_GETIMEMODEBIAS)
1094 return richedit_messages[msg - EM_CANPASTE];
1095 return "";
1098 /******************************************************************
1099 * RichEditANSIWndProc (RICHED20.10)
1101 LRESULT WINAPI RichEditANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
1102 SCROLLINFO si;
1103 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongW(hWnd, 0);
1105 TRACE("hWnd %p msg %04x (%s) %08x %08lx\n",
1106 hWnd, msg, get_msg_name(msg), wParam, lParam);
1108 switch(msg) {
1110 UNSUPPORTED_MSG(EM_AUTOURLDETECT)
1111 UNSUPPORTED_MSG(EM_DISPLAYBAND)
1112 UNSUPPORTED_MSG(EM_EXLIMITTEXT)
1113 UNSUPPORTED_MSG(EM_FINDWORDBREAK)
1114 UNSUPPORTED_MSG(EM_FMTLINES)
1115 UNSUPPORTED_MSG(EM_FORMATRANGE)
1116 UNSUPPORTED_MSG(EM_GETAUTOURLDETECT)
1117 UNSUPPORTED_MSG(EM_GETBIDIOPTIONS)
1118 UNSUPPORTED_MSG(EM_GETEDITSTYLE)
1119 UNSUPPORTED_MSG(EM_GETIMECOMPMODE)
1120 /* UNSUPPORTED_MSG(EM_GETIMESTATUS) missing in Wine headers */
1121 UNSUPPORTED_MSG(EM_GETLANGOPTIONS)
1122 UNSUPPORTED_MSG(EM_GETLIMITTEXT)
1123 UNSUPPORTED_MSG(EM_GETLINE)
1124 /* UNSUPPORTED_MSG(EM_GETOLEINTERFACE) separate stub */
1125 UNSUPPORTED_MSG(EM_GETOPTIONS)
1126 UNSUPPORTED_MSG(EM_GETPASSWORDCHAR)
1127 UNSUPPORTED_MSG(EM_GETREDONAME)
1128 UNSUPPORTED_MSG(EM_GETSCROLLPOS)
1129 UNSUPPORTED_MSG(EM_GETTEXTMODE)
1130 UNSUPPORTED_MSG(EM_GETTYPOGRAPHYOPTIONS)
1131 UNSUPPORTED_MSG(EM_GETUNDONAME)
1132 UNSUPPORTED_MSG(EM_GETWORDBREAKPROC)
1133 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX)
1134 UNSUPPORTED_MSG(EM_HIDESELECTION)
1135 UNSUPPORTED_MSG(EM_LIMITTEXT) /* also known as EM_SETLIMITTEXT */
1136 UNSUPPORTED_MSG(EM_PASTESPECIAL)
1137 UNSUPPORTED_MSG(EM_REQUESTRESIZE)
1138 UNSUPPORTED_MSG(EM_SCROLL)
1139 UNSUPPORTED_MSG(EM_SCROLLCARET)
1140 UNSUPPORTED_MSG(EM_SELECTIONTYPE)
1141 UNSUPPORTED_MSG(EM_SETBIDIOPTIONS)
1142 UNSUPPORTED_MSG(EM_SETEDITSTYLE)
1143 UNSUPPORTED_MSG(EM_SETFONTSIZE)
1144 UNSUPPORTED_MSG(EM_SETLANGOPTIONS)
1145 UNSUPPORTED_MSG(EM_SETOLECALLBACK)
1146 UNSUPPORTED_MSG(EM_SETOPTIONS)
1147 UNSUPPORTED_MSG(EM_SETPALETTE)
1148 UNSUPPORTED_MSG(EM_SETPASSWORDCHAR)
1149 UNSUPPORTED_MSG(EM_SETSCROLLPOS)
1150 UNSUPPORTED_MSG(EM_SETTABSTOPS)
1151 UNSUPPORTED_MSG(EM_SETTARGETDEVICE)
1152 UNSUPPORTED_MSG(EM_SETTEXTMODE)
1153 UNSUPPORTED_MSG(EM_SETTYPOGRAPHYOPTIONS)
1154 UNSUPPORTED_MSG(EM_SETUNDOLIMIT)
1155 UNSUPPORTED_MSG(EM_SETWORDBREAKPROC)
1156 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX)
1157 UNSUPPORTED_MSG(EM_SHOWSCROLLBAR)
1158 UNSUPPORTED_MSG(WM_SETFONT)
1159 UNSUPPORTED_MSG(WM_STYLECHANGING)
1160 UNSUPPORTED_MSG(WM_STYLECHANGED)
1161 /* UNSUPPORTED_MSG(WM_UNICHAR) FIXME missing in Wine headers */
1163 /* Messages specific to Richedit controls */
1165 case EM_STREAMIN:
1166 return ME_StreamIn(editor, wParam, (EDITSTREAM*)lParam);
1167 case EM_STREAMOUT:
1168 return ME_StreamOut(editor, wParam, (EDITSTREAM *)lParam);
1169 case WM_GETDLGCODE:
1171 UINT code = DLGC_WANTCHARS|DLGC_WANTARROWS;
1172 if (GetWindowLongW(hWnd, GWL_STYLE)&ES_WANTRETURN)
1173 code |= 0; /* FIXME what can we do here ? ask for messages and censor them ? */
1174 return code;
1176 case WM_NCCREATE:
1178 CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
1179 TRACE("WM_NCCREATE: style 0x%08lx\n", pcs->style);
1180 editor = ME_MakeEditor(hWnd);
1181 SetWindowLongW(hWnd, 0, (long)editor);
1182 pcs = 0; /* ignore */
1183 return TRUE;
1185 case EM_EMPTYUNDOBUFFER:
1186 ME_EmptyUndoStack(editor);
1187 return 0;
1188 case EM_GETSEL:
1190 /* Note: wParam/lParam can be NULL */
1191 UINT from, to;
1192 PUINT pfrom = wParam ? (PUINT)wParam : &from;
1193 PUINT pto = lParam ? (PUINT)lParam : &to;
1194 ME_GetSelection(editor, (int *)pfrom, (int *)pto);
1195 if ((*pfrom|*pto) & 0xFFFF0000)
1196 return -1;
1197 return MAKELONG(*pfrom,*pto);
1199 case EM_EXGETSEL:
1201 CHARRANGE *pRange = (CHARRANGE *)lParam;
1202 ME_GetSelection(editor, (int *)&pRange->cpMin, (int *)&pRange->cpMax);
1203 TRACE("EM_EXGETSEL = (%ld,%ld)\n", pRange->cpMin, pRange->cpMax);
1204 return 0;
1206 case EM_CANUNDO:
1207 return editor->pUndoStack != NULL;
1208 case EM_CANREDO:
1209 return editor->pRedoStack != NULL;
1210 case WM_UNDO: /* FIXME: actually not the same */
1211 case EM_UNDO:
1212 ME_Undo(editor);
1213 return 0;
1214 case EM_REDO:
1215 ME_Redo(editor);
1216 return 0;
1217 case EM_SETSEL:
1219 ME_SetSelection(editor, wParam, lParam);
1220 ME_Repaint(editor);
1221 ME_SendSelChange(editor);
1222 return 0;
1224 case EM_EXSETSEL:
1226 CHARRANGE *pRange = (CHARRANGE *)lParam;
1227 TRACE("EM_EXSETSEL (%ld,%ld)\n", pRange->cpMin, pRange->cpMax);
1228 ME_SetSelection(editor, pRange->cpMin, pRange->cpMax);
1229 /* FIXME optimize */
1230 ME_Repaint(editor);
1231 ME_SendSelChange(editor);
1232 return 0;
1234 case EM_SETTEXTEX:
1236 LPWSTR wszText = (LPWSTR)lParam;
1237 SETTEXTEX *pStruct = (SETTEXTEX *)wParam;
1238 size_t len = lstrlenW(wszText);
1239 int from, to;
1240 ME_Style *style;
1241 TRACE("EM_SETTEXEX - %s, flags %d, cp %d\n", debugstr_w(wszText), (int)pStruct->flags, pStruct->codepage);
1242 if (pStruct->codepage != 1200) {
1243 FIXME("EM_SETTEXTEX only supports unicode right now!\n");
1244 return 0;
1246 /* FIXME: this should support RTF strings too, according to MSDN */
1247 if (pStruct->flags & ST_SELECTION) {
1248 ME_GetSelection(editor, &from, &to);
1249 style = ME_GetSelectionInsertStyle(editor);
1250 ME_InternalDeleteText(editor, from, to - from);
1251 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
1252 ME_ReleaseStyle(style);
1254 else {
1255 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
1256 ME_InsertTextFromCursor(editor, 0, wszText, -1, editor->pBuffer->pDefaultStyle);
1257 len = 1;
1259 ME_CommitUndo(editor);
1260 if (!(pStruct->flags & ST_KEEPUNDO))
1261 ME_EmptyUndoStack(editor);
1262 ME_UpdateRepaint(editor);
1263 return len;
1265 case EM_SETBKGNDCOLOR:
1267 LRESULT lColor = ME_GetBackColor(editor);
1268 if (wParam)
1269 editor->rgbBackColor = -1;
1270 else
1271 editor->rgbBackColor = lParam;
1272 if (editor->bRedraw)
1274 InvalidateRect(hWnd, NULL, TRUE);
1275 UpdateWindow(hWnd);
1277 return lColor;
1279 case EM_GETMODIFY:
1280 return editor->nModifyStep == 0 ? 0 : 1;
1281 case EM_SETMODIFY:
1283 if (wParam)
1284 editor->nModifyStep = 0x80000000;
1285 else
1286 editor->nModifyStep = 0;
1288 return 0;
1290 case EM_SETREADONLY:
1292 long nStyle = GetWindowLongW(hWnd, GWL_STYLE);
1293 if (wParam)
1294 nStyle |= ES_READONLY;
1295 else
1296 nStyle &= ~ES_READONLY;
1297 SetWindowLongW(hWnd, GWL_STYLE, nStyle);
1298 ME_Repaint(editor);
1299 return 0;
1301 case EM_SETEVENTMASK:
1303 DWORD nOldMask = editor->nEventMask;
1305 editor->nEventMask = lParam;
1306 return nOldMask;
1308 case EM_GETEVENTMASK:
1309 return editor->nEventMask;
1310 case EM_SETCHARFORMAT:
1312 CHARFORMAT2W buf, *p;
1313 BOOL bRepaint = TRUE;
1314 p = ME_ToCF2W(&buf, (CHARFORMAT2W *)lParam);
1315 if (!wParam)
1316 ME_SetDefaultCharFormat(editor, p);
1317 else if (wParam == (SCF_WORD | SCF_SELECTION))
1318 FIXME("EM_SETCHARFORMAT: word selection not supported\n");
1319 else if (wParam == SCF_ALL)
1320 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), p);
1321 else {
1322 int from, to;
1323 ME_GetSelection(editor, &from, &to);
1324 bRepaint = (from != to);
1325 ME_SetSelectionCharFormat(editor, p);
1327 ME_CommitUndo(editor);
1328 if (bRepaint)
1329 ME_UpdateRepaint(editor);
1330 return 0;
1332 case EM_GETCHARFORMAT:
1334 CHARFORMAT2W tmp, *dst = (CHARFORMAT2W *)lParam;
1335 if (dst->cbSize != sizeof(CHARFORMATA) &&
1336 dst->cbSize != sizeof(CHARFORMATW) &&
1337 dst->cbSize != sizeof(CHARFORMAT2A) &&
1338 dst->cbSize != sizeof(CHARFORMAT2W))
1339 return 0;
1340 tmp.cbSize = sizeof(tmp);
1341 if (!wParam)
1342 ME_GetDefaultCharFormat(editor, &tmp);
1343 else
1344 ME_GetSelectionCharFormat(editor, &tmp);
1345 ME_CopyToCFAny(dst, &tmp);
1346 return tmp.dwMask;
1348 case EM_SETPARAFORMAT:
1349 ME_SetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
1350 ME_UpdateRepaint(editor);
1351 ME_CommitUndo(editor);
1352 return 0;
1353 case EM_GETPARAFORMAT:
1354 ME_GetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
1355 return 0;
1356 case EM_GETFIRSTVISIBLELINE:
1358 ME_DisplayItem *p = editor->pBuffer->pFirst;
1359 int y = editor->nScrollPosY;
1360 int ypara = 0;
1361 int count = 0;
1362 int ystart, yend;
1363 while(p) {
1364 p = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
1365 if (p->type == diTextEnd)
1366 break;
1367 if (p->type == diParagraph) {
1368 ypara = p->member.para.nYPos;
1369 continue;
1371 ystart = ypara + p->member.row.nYPos;
1372 yend = ystart + p->member.row.nHeight;
1373 if (y < yend) {
1374 break;
1376 count++;
1378 return count;
1380 case EM_LINESCROLL:
1382 int nPos = editor->nScrollPosY, nEnd= editor->nTotalLength - editor->sizeWindow.cy;
1383 nPos += 8 * lParam; /* FIXME follow the original */
1384 if (nPos>=nEnd)
1385 nPos = nEnd;
1386 if (nPos<0)
1387 nPos = 0;
1388 if (nPos != editor->nScrollPosY) {
1389 int dy = editor->nScrollPosY - nPos;
1390 editor->nScrollPosY = nPos;
1391 SetScrollPos(hWnd, SB_VERT, nPos, TRUE);
1392 if (editor->bRedraw)
1394 ScrollWindow(hWnd, 0, dy, NULL, NULL);
1395 UpdateWindow(hWnd);
1398 return TRUE; /* Should return false if a single line richedit control */
1400 case WM_CLEAR:
1402 int from, to;
1403 ME_GetSelection(editor, &from, &to);
1404 ME_InternalDeleteText(editor, from, to-from);
1405 ME_CommitUndo(editor);
1406 ME_UpdateRepaint(editor);
1407 return 0;
1409 case EM_REPLACESEL:
1411 int from, to;
1412 ME_Style *style;
1413 LPWSTR wszText = ME_ToUnicode(hWnd, (void *)lParam);
1414 size_t len = lstrlenW(wszText);
1415 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText));
1417 ME_GetSelection(editor, &from, &to);
1418 style = ME_GetSelectionInsertStyle(editor);
1419 ME_InternalDeleteText(editor, from, to-from);
1420 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
1421 ME_ReleaseStyle(style);
1422 ME_EndToUnicode(hWnd, wszText);
1423 /* drop temporary style if line end */
1424 /* FIXME question: does abc\n mean: put abc, clear temp style, put \n? (would require a change) */
1425 if (len>0 && wszText[len-1] == '\n')
1426 ME_ClearTempStyle(editor);
1428 ME_CommitUndo(editor);
1429 if (!wParam)
1430 ME_EmptyUndoStack(editor);
1431 ME_UpdateRepaint(editor);
1432 return 0;
1434 case WM_SETTEXT:
1436 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
1437 if (lParam)
1439 LPWSTR wszText = ME_ToUnicode(hWnd, (void *)lParam);
1440 TRACE("WM_SETTEXT lParam==%lx\n",lParam);
1441 TRACE("WM_SETTEXT - %s\n", debugstr_w(wszText)); /* debugstr_w() */
1442 if (lstrlenW(wszText) > 0)
1444 /* uses default style! */
1445 ME_InsertTextFromCursor(editor, 0, wszText, -1, editor->pBuffer->pDefaultStyle);
1447 ME_EndToUnicode(hWnd, wszText);
1449 else
1450 TRACE("WM_SETTEXT - NULL\n");
1451 ME_CommitUndo(editor);
1452 ME_EmptyUndoStack(editor);
1453 ME_UpdateRepaint(editor);
1454 return 0;
1456 case EM_CANPASTE:
1458 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
1459 if (IsClipboardFormatAvailable(nRTFFormat))
1460 return TRUE;
1461 if (IsClipboardFormatAvailable(CF_UNICODETEXT))
1462 return TRUE;
1463 return FALSE;
1465 case WM_PASTE:
1467 DWORD dwFormat = 0;
1468 EDITSTREAM es;
1469 ME_GlobalDestStruct gds;
1470 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
1471 UINT cf = 0;
1473 if (IsClipboardFormatAvailable(nRTFFormat))
1474 cf = nRTFFormat, dwFormat = SF_RTF;
1475 else if (IsClipboardFormatAvailable(CF_UNICODETEXT))
1476 cf = CF_UNICODETEXT, dwFormat = SF_TEXT|SF_UNICODE;
1477 else
1478 return 0;
1480 if (!OpenClipboard(hWnd))
1481 return 0;
1482 gds.hData = GetClipboardData(cf);
1483 gds.nLength = 0;
1484 es.dwCookie = (DWORD)&gds;
1485 es.pfnCallback = dwFormat == SF_RTF ? ME_ReadFromHGLOBALRTF : ME_ReadFromHGLOBALUnicode;
1486 SendMessageW(hWnd, EM_STREAMIN, dwFormat|SFF_SELECTION, (LPARAM)&es);
1488 CloseClipboard();
1489 return 0;
1491 case WM_CUT:
1492 case WM_COPY:
1494 int from, to, pars;
1495 WCHAR *data;
1496 HANDLE hData;
1497 EDITSTREAM es;
1498 ME_GlobalDestStruct gds;
1500 if (!OpenClipboard(hWnd))
1501 return 0;
1503 EmptyClipboard();
1504 ME_GetSelection(editor, &from, &to);
1505 pars = ME_CountParagraphsBetween(editor, from, to);
1506 hData = GlobalAlloc(GMEM_MOVEABLE, sizeof(WCHAR)*(to-from+pars+1));
1507 data = (WCHAR *)GlobalLock(hData);
1508 ME_GetTextW(editor, data, from, to-from, TRUE);
1509 GlobalUnlock(hData);
1511 gds.hData = GlobalAlloc(GMEM_MOVEABLE, 0);
1512 gds.nLength = 0;
1513 es.dwCookie = (DWORD)&gds;
1514 es.pfnCallback = ME_AppendToHGLOBAL;
1515 SendMessageW(hWnd, EM_STREAMOUT, SFF_SELECTION|SF_RTF, (LPARAM)&es);
1516 GlobalReAlloc(gds.hData, gds.nLength+1, 0);
1518 SetClipboardData(CF_UNICODETEXT, hData);
1519 SetClipboardData(RegisterClipboardFormatA("Rich Text Format"), gds.hData);
1521 CloseClipboard();
1522 if (msg == WM_CUT)
1524 ME_InternalDeleteText(editor, from, to-from);
1525 ME_CommitUndo(editor);
1526 ME_UpdateRepaint(editor);
1528 return 0;
1530 case WM_GETTEXTLENGTH:
1531 return ME_GetTextLength(editor);
1532 case EM_GETTEXTLENGTHEX:
1533 return ME_GetTextLengthEx(editor, (GETTEXTLENGTHEX *)wParam);
1534 case WM_GETTEXT:
1536 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
1537 tr.chrg.cpMin = 0;
1538 tr.chrg.cpMax = wParam-1;
1539 tr.lpstrText = (WCHAR *)lParam;
1540 return RichEditANSIWndProc(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
1542 case EM_GETTEXTEX:
1544 GETTEXTEX *ex = (GETTEXTEX*)wParam;
1545 int nStart, nCount;
1547 if (ex->flags & ~(GT_SELECTION | GT_USECRLF))
1548 FIXME("GETTEXTEX flags 0x%08lx not supported\n", ex->flags & ~(GT_SELECTION | GT_USECRLF));
1550 if (ex->flags & GT_SELECTION)
1552 ME_GetSelection(editor, &nStart, &nCount);
1553 nCount -= nStart;
1554 nCount = min(nCount, ex->cb - 1);
1556 else
1558 nStart = 0;
1559 nCount = ex->cb - 1;
1561 if (ex->codepage == 1200 || IsWindowUnicode(hWnd))
1563 nCount = min(nCount, ex->cb / sizeof(WCHAR) - 1);
1564 return ME_GetTextW(editor, (LPWSTR)lParam, nStart, nCount, ex->flags & GT_USECRLF);
1566 else
1568 LPWSTR buffer = HeapAlloc(GetProcessHeap(), 0, (nCount + 1) * sizeof(WCHAR));
1569 DWORD buflen = ex->cb;
1570 LRESULT rc;
1571 DWORD flags = 0;
1573 buflen = ME_GetTextW(editor, buffer, nStart, nCount, ex->flags & GT_USECRLF);
1574 rc = WideCharToMultiByte(ex->codepage, flags, buffer, buflen, (LPSTR)lParam, ex->cb, ex->lpDefaultChar, ex->lpUsedDefaultChar);
1576 HeapFree(GetProcessHeap(),0,buffer);
1577 return rc;
1580 case EM_GETSELTEXT:
1582 int from, to;
1583 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
1584 ME_GetSelection(editor, &from, &to);
1585 tr.chrg.cpMin = from;
1586 tr.chrg.cpMax = to;
1587 tr.lpstrText = (WCHAR *)lParam;
1588 return RichEditANSIWndProc(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
1590 case EM_GETTEXTRANGE:
1592 TEXTRANGEW *rng = (TEXTRANGEW *)lParam;
1593 if (IsWindowUnicode(hWnd))
1594 return ME_GetTextW(editor, rng->lpstrText, rng->chrg.cpMin, rng->chrg.cpMax-rng->chrg.cpMin, editor->bEmulateVersion10);
1595 else
1597 int nLen = rng->chrg.cpMax-rng->chrg.cpMin;
1598 WCHAR *p = ALLOC_N_OBJ(WCHAR, nLen+1);
1599 int nChars = ME_GetTextW(editor, p, rng->chrg.cpMin, nLen, editor->bEmulateVersion10);
1600 /* FIXME this is a potential security hole (buffer overrun)
1601 if you know more about wchar->mbyte conversion please explain
1603 WideCharToMultiByte(CP_ACP, 0, p, nChars+1, (char *)rng->lpstrText, nLen+1, NULL, NULL);
1604 FREE_OBJ(p);
1605 return nChars;
1608 case EM_GETLINECOUNT:
1610 ME_DisplayItem *item = editor->pBuffer->pFirst->next;
1611 int nRows = 0;
1613 while (item != editor->pBuffer->pLast)
1615 assert(item->type == diParagraph);
1616 nRows += item->member.para.nRows;
1617 item = item->member.para.next_para;
1619 TRACE("EM_GETLINECOUNT: nRows==%d\n", nRows);
1620 return max(1, nRows);
1622 case EM_LINEFROMCHAR:
1624 if (wParam == -1)
1625 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(editor, 1));
1626 else
1627 return ME_RowNumberFromCharOfs(editor, wParam);
1629 case EM_EXLINEFROMCHAR:
1631 return ME_RowNumberFromCharOfs(editor, lParam);
1633 case EM_LINEINDEX:
1635 ME_DisplayItem *item, *para;
1636 int nCharOfs;
1638 if (wParam == -1)
1639 item = ME_FindItemBack(editor->pCursors[0].pRun, diStartRow);
1640 else
1641 item = ME_FindRowWithNumber(editor, wParam);
1642 if (!item)
1643 return -1;
1644 para = ME_GetParagraph(item);
1645 item = ME_FindItemFwd(item, diRun);
1646 nCharOfs = para->member.para.nCharOfs + item->member.run.nCharOfs;
1647 TRACE("EM_LINEINDEX: nCharOfs==%d\n", nCharOfs);
1648 return nCharOfs;
1650 case EM_LINELENGTH:
1652 ME_DisplayItem *item, *item_end;
1653 int nChars = 0, nThisLineOfs = 0, nNextLineOfs = 0;
1655 if (wParam > ME_GetTextLength(editor))
1656 return 0;
1657 if (wParam == -1)
1659 FIXME("EM_LINELENGTH: returning number of unselected characters on lines with selection unsupported.\n");
1660 return 0;
1662 item = ME_FindItemAtOffset(editor, diRun, wParam, NULL);
1663 item = ME_RowStart(item);
1664 nThisLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item, diRun), 0);
1665 item_end = ME_FindItemFwd(item, diStartRow);
1666 if (item_end)
1667 nNextLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item_end, diRun), 0);
1668 else
1669 nNextLineOfs = ME_FindItemFwd(item, diParagraphOrEnd)->member.para.nCharOfs
1670 - (editor->bEmulateVersion10?2:1);
1671 nChars = nNextLineOfs - nThisLineOfs;
1672 TRACE("EM_LINELENGTH(%d)==%d\n",wParam, nChars);
1673 return nChars;
1675 case EM_FINDTEXT:
1677 FINDTEXTA *ft = (FINDTEXTA *)lParam;
1678 int nChars = MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, NULL, 0);
1679 WCHAR *tmp;
1681 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
1682 MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, tmp, nChars);
1683 return ME_FindText(editor, wParam, &ft->chrg, tmp, NULL);
1685 case EM_FINDTEXTEX:
1687 FINDTEXTEXA *ex = (FINDTEXTEXA *)lParam;
1688 int nChars = MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, NULL, 0);
1689 WCHAR *tmp;
1691 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
1692 MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, tmp, nChars);
1693 return ME_FindText(editor, wParam, &ex->chrg, tmp, &ex->chrgText);
1695 case EM_FINDTEXTW:
1697 FINDTEXTW *ft = (FINDTEXTW *)lParam;
1698 return ME_FindText(editor, wParam, &ft->chrg, ft->lpstrText, NULL);
1700 case EM_FINDTEXTEXW:
1702 FINDTEXTEXW *ex = (FINDTEXTEXW *)lParam;
1703 return ME_FindText(editor, wParam, &ex->chrg, ex->lpstrText, &ex->chrgText);
1705 case EM_GETZOOM:
1706 if (!wParam || !lParam)
1707 return FALSE;
1708 *(int *)wParam = editor->nZoomNumerator;
1709 *(int *)lParam = editor->nZoomDenominator;
1710 return TRUE;
1711 case EM_SETZOOM:
1712 return ME_SetZoom(editor, wParam, lParam);
1713 case EM_CHARFROMPOS:
1714 return ME_CharFromPos(editor, ((POINTL *)lParam)->x, ((POINTL *)lParam)->y);
1715 case EM_POSFROMCHAR:
1717 ME_DisplayItem *pRun;
1718 int nCharOfs, nOffset, nLength;
1719 POINTL pt = {0,0};
1721 nCharOfs = wParam;
1722 /* detect which API version we're dealing with */
1723 if (wParam >= 0x40000)
1724 nCharOfs = lParam;
1725 nLength = ME_GetTextLength(editor);
1727 if (nCharOfs < nLength) {
1728 ME_RunOfsFromCharOfs(editor, nCharOfs, &pRun, &nOffset);
1729 assert(pRun->type == diRun);
1730 pt.y = pRun->member.run.pt.y;
1731 pt.x = pRun->member.run.pt.x + ME_PointFromChar(editor, &pRun->member.run, nOffset);
1732 pt.y += ME_GetParagraph(pRun)->member.para.nYPos;
1733 } else {
1734 pt.x = 0;
1735 pt.y = editor->pBuffer->pLast->member.para.nYPos;
1737 pt.y += ME_GetParagraph(pRun)->member.para.nYPos;
1738 if (wParam >= 0x40000) {
1739 *(POINTL *)wParam = pt;
1741 return MAKELONG( pt.x, pt.y );
1743 case WM_CREATE:
1744 ME_CommitUndo(editor);
1745 ME_WrapMarkedParagraphs(editor);
1746 ME_MoveCaret(editor);
1747 return 0;
1748 case WM_DESTROY:
1749 ME_DestroyEditor(editor);
1750 SetWindowLongW(hWnd, 0, 0);
1751 return 0;
1752 case WM_LBUTTONDOWN:
1753 SetFocus(hWnd);
1754 ME_LButtonDown(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
1755 SetCapture(hWnd);
1756 break;
1757 case WM_MOUSEMOVE:
1758 if (GetCapture() == hWnd)
1759 ME_MouseMove(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
1760 break;
1761 case WM_LBUTTONUP:
1762 if (GetCapture() == hWnd)
1763 ReleaseCapture();
1764 break;
1765 case WM_PAINT:
1766 if (editor->bRedraw)
1768 HDC hDC;
1769 PAINTSTRUCT ps;
1771 hDC = BeginPaint(hWnd, &ps);
1772 ME_PaintContent(editor, hDC, FALSE, &ps.rcPaint);
1773 EndPaint(hWnd, &ps);
1775 break;
1776 case WM_SETFOCUS:
1777 ME_ShowCaret(editor);
1778 ME_SendOldNotify(editor, EN_SETFOCUS);
1779 return 0;
1780 case WM_KILLFOCUS:
1781 ME_HideCaret(editor);
1782 ME_SendOldNotify(editor, EN_KILLFOCUS);
1783 return 0;
1784 case WM_ERASEBKGND:
1786 if (editor->bRedraw)
1788 HDC hDC = (HDC)wParam;
1789 RECT rc;
1790 COLORREF rgbBG = ME_GetBackColor(editor);
1791 if (GetUpdateRect(hWnd,&rc,TRUE))
1793 HBRUSH hbr = CreateSolidBrush(rgbBG);
1794 FillRect(hDC, &rc, hbr);
1795 DeleteObject(hbr);
1798 return 1;
1800 case WM_COMMAND:
1801 TRACE("editor wnd command = %d\n", LOWORD(wParam));
1802 return 0;
1803 case WM_KEYDOWN:
1804 if (ME_ArrowKey(editor, LOWORD(wParam), GetKeyState(VK_CONTROL)<0)) {
1805 ME_CommitUndo(editor);
1806 ME_EnsureVisible(editor, editor->pCursors[0].pRun);
1807 HideCaret(hWnd);
1808 ME_MoveCaret(editor);
1809 ShowCaret(hWnd);
1810 return 0;
1812 if (GetKeyState(VK_CONTROL)<0)
1814 if (LOWORD(wParam)=='W')
1816 CHARFORMAT2W chf;
1817 char buf[2048];
1818 ME_GetSelectionCharFormat(editor, &chf);
1819 ME_DumpStyleToBuf(&chf, buf);
1820 MessageBoxA(NULL, buf, "Style dump", MB_OK);
1822 if (LOWORD(wParam)=='Q')
1824 ME_CheckCharOffsets(editor);
1827 goto do_default;
1828 case WM_CHAR:
1830 WCHAR wstr = LOWORD(wParam);
1832 switch (wstr)
1834 case 3: /* Ctrl-C */
1835 SendMessageW(editor->hWnd, WM_COPY, 0, 0);
1836 return 0;
1839 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY) {
1840 MessageBeep(MB_ICONERROR);
1841 return 0; /* FIXME really 0 ? */
1844 switch (wstr)
1846 case 1: /* Ctrl-A */
1847 ME_SetSelection(editor, 0, -1);
1848 return 0;
1849 case 22: /* Ctrl-V */
1850 SendMessageW(editor->hWnd, WM_PASTE, 0, 0);
1851 return 0;
1852 case 24: /* Ctrl-X */
1853 SendMessageW(editor->hWnd, WM_CUT, 0, 0);
1854 return 0;
1855 case 25: /* Ctrl-Y */
1856 SendMessageW(editor->hWnd, EM_REDO, 0, 0);
1857 return 0;
1858 case 26: /* Ctrl-Z */
1859 SendMessageW(editor->hWnd, EM_UNDO, 0, 0);
1860 return 0;
1862 if (((unsigned)wstr)>=' ' || wstr=='\r' || wstr=='\t') {
1863 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
1864 ME_Style *style = ME_GetInsertStyle(editor, 0);
1865 ME_SaveTempStyle(editor);
1866 ME_InsertTextFromCursor(editor, 0, &wstr, 1, style);
1867 ME_ReleaseStyle(style);
1868 ME_CommitUndo(editor);
1869 ME_UpdateRepaint(editor);
1871 return 0;
1873 case WM_VSCROLL:
1875 int nPos = editor->nScrollPosY;
1876 si.cbSize = sizeof(SCROLLINFO);
1877 si.fMask = SIF_PAGE|SIF_POS|SIF_RANGE|SIF_TRACKPOS;
1878 GetScrollInfo(hWnd, SB_VERT, &si);
1879 switch(LOWORD(wParam)) {
1880 case SB_LINEUP:
1881 nPos -= 24; /* FIXME follow the original */
1882 if (nPos<0) nPos = 0;
1883 break;
1884 case SB_LINEDOWN:
1886 int nEnd = editor->nTotalLength - editor->sizeWindow.cy;
1887 nPos += 24; /* FIXME follow the original */
1888 if (nPos>=nEnd) nPos = nEnd;
1889 break;
1891 case SB_PAGEUP:
1892 nPos -= editor->sizeWindow.cy;
1893 if (nPos<0) nPos = 0;
1894 break;
1895 case SB_PAGEDOWN:
1896 nPos += editor->sizeWindow.cy;
1897 if (nPos>=editor->nTotalLength) nPos = editor->nTotalLength-1;
1898 break;
1899 case SB_THUMBTRACK:
1900 case SB_THUMBPOSITION:
1901 nPos = si.nTrackPos;
1902 break;
1904 if (nPos != editor->nScrollPosY) {
1905 int dy = editor->nScrollPosY - nPos;
1906 editor->nScrollPosY = nPos;
1907 SetScrollPos(hWnd, SB_VERT, nPos, TRUE);
1908 if (editor->bRedraw)
1910 ScrollWindow(hWnd, 0, dy, NULL, NULL);
1911 UpdateWindow(hWnd);
1914 break;
1916 case WM_MOUSEWHEEL:
1918 int gcWheelDelta = 0, nPos = editor->nScrollPosY, nEnd = editor->nTotalLength - editor->sizeWindow.cy;
1919 UINT pulScrollLines;
1920 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
1921 gcWheelDelta -= GET_WHEEL_DELTA_WPARAM(wParam);
1922 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
1923 nPos += pulScrollLines * (gcWheelDelta / WHEEL_DELTA) * 8; /* FIXME follow the original */
1924 if (nPos>=nEnd)
1925 nPos = nEnd;
1926 if (nPos<0)
1927 nPos = 0;
1928 if (nPos != editor->nScrollPosY) {
1929 int dy = editor->nScrollPosY - nPos;
1930 editor->nScrollPosY = nPos;
1931 SetScrollPos(hWnd, SB_VERT, nPos, TRUE);
1932 if (editor->bRedraw)
1934 ScrollWindow(hWnd, 0, dy, NULL, NULL);
1935 UpdateWindow(hWnd);
1938 break;
1940 case EM_GETRECT:
1942 *((RECT *)lParam) = editor->rcFormat;
1943 return 0;
1945 case EM_SETRECT:
1946 case EM_SETRECTNP:
1948 if (lParam)
1950 RECT *rc = (RECT *)lParam;
1952 if (wParam)
1954 editor->rcFormat.left += rc->left;
1955 editor->rcFormat.top += rc->top;
1956 editor->rcFormat.right += rc->right;
1957 editor->rcFormat.bottom += rc->bottom;
1959 else
1961 editor->rcFormat = *rc;
1964 else
1966 GetClientRect(hWnd, &editor->rcFormat);
1968 if (msg != EM_SETRECTNP)
1969 ME_RewrapRepaint(editor);
1970 return 0;
1972 case WM_SETREDRAW:
1973 editor->bRedraw = wParam;
1974 return 0;
1975 case WM_SIZE:
1977 GetClientRect(hWnd, &editor->rcFormat);
1978 ME_RewrapRepaint(editor);
1979 return DefWindowProcW(hWnd, msg, wParam, lParam);
1981 case EM_GETOLEINTERFACE:
1983 LPVOID *ppvObj = (LPVOID*) lParam;
1984 FIXME("EM_GETOLEINTERFACE %p: stub\n", ppvObj);
1985 return CreateIRichEditOle(ppvObj);
1987 default:
1988 do_default:
1989 return DefWindowProcW(hWnd, msg, wParam, lParam);
1991 return 0L;
1995 /******************************************************************
1996 * RichEdit10ANSIWndProc (RICHED20.9)
1998 LRESULT WINAPI RichEdit10ANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2000 LRESULT result;
2002 /* FIXME: this is NOT the same as 2.0 version */
2003 result = RichEditANSIWndProc(hWnd, msg, wParam, lParam);
2004 if (msg == WM_NCCREATE)
2006 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongW(hWnd, 0);
2008 editor->bEmulateVersion10 = TRUE;
2009 editor->pBuffer->pLast->member.para.nCharOfs = 2;
2011 return result;
2014 void ME_SendOldNotify(ME_TextEditor *editor, int nCode)
2016 HWND hWnd = editor->hWnd;
2017 SendMessageA(GetParent(hWnd), WM_COMMAND, (nCode<<16)|GetWindowLongW(hWnd, GWLP_ID), (LPARAM)hWnd);
2020 int ME_CountParagraphsBetween(ME_TextEditor *editor, int from, int to)
2022 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
2023 int i = 0;
2025 while(item && item->member.para.next_para->member.para.nCharOfs <= from)
2026 item = item->member.para.next_para;
2027 if (!item)
2028 return 0;
2029 while(item && item->member.para.next_para->member.para.nCharOfs <= to) {
2030 item = item->member.para.next_para;
2031 i++;
2033 return i;
2037 int ME_GetTextW(ME_TextEditor *editor, WCHAR *buffer, int nStart, int nChars, int bCRLF)
2039 ME_DisplayItem *item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
2040 int nWritten = 0;
2042 if (!item) {
2043 *buffer = L'\0';
2044 return 0;
2046 assert(item);
2048 if (nStart)
2050 int nLen = ME_StrLen(item->member.run.strText) - nStart;
2051 if (nLen > nChars)
2052 nLen = nChars;
2053 CopyMemory(buffer, item->member.run.strText->szData + nStart, sizeof(WCHAR)*nLen);
2054 nChars -= nLen;
2055 nWritten += nLen;
2056 if (!nChars)
2057 return nWritten;
2058 buffer += nLen;
2059 nStart = 0;
2060 item = ME_FindItemFwd(item, diRun);
2063 while(nChars && item)
2065 int nLen = ME_StrLen(item->member.run.strText);
2066 if (nLen > nChars)
2067 nLen = nChars;
2069 if (item->member.run.nFlags & MERF_ENDPARA)
2071 *buffer = '\r';
2072 if (bCRLF)
2074 *(++buffer) = '\n';
2075 nWritten++;
2077 assert(nLen == 1);
2078 /* our end paragraph consists of 2 characters now */
2079 if (editor->bEmulateVersion10)
2080 nChars--;
2082 else
2083 CopyMemory(buffer, item->member.run.strText->szData, sizeof(WCHAR)*nLen);
2084 nChars -= nLen;
2085 nWritten += nLen;
2086 buffer += nLen;
2088 if (!nChars)
2090 *buffer = L'\0';
2091 return nWritten;
2093 item = ME_FindItemFwd(item, diRun);
2095 *buffer = L'\0';
2096 return nWritten;
2099 void ME_RegisterEditorClass(HINSTANCE hInstance)
2101 BOOL bResult;
2102 WNDCLASSW wcW;
2103 WNDCLASSA wcA;
2105 wcW.style = CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
2106 wcW.lpfnWndProc = RichEditANSIWndProc;
2107 wcW.cbClsExtra = 0;
2108 wcW.cbWndExtra = 4;
2109 wcW.hInstance = NULL; /* hInstance would register DLL-local class */
2110 wcW.hIcon = NULL;
2111 wcW.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
2112 wcW.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
2113 wcW.lpszMenuName = NULL;
2114 wcW.lpszClassName = wszClassName;
2115 bResult = RegisterClassW(&wcW);
2116 assert(bResult);
2117 wcW.lpszClassName = wszClassName50;
2118 bResult = RegisterClassW(&wcW);
2119 assert(bResult);
2121 wcA.style = CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
2122 wcA.lpfnWndProc = RichEditANSIWndProc;
2123 wcA.cbClsExtra = 0;
2124 wcA.cbWndExtra = 4;
2125 wcA.hInstance = NULL; /* hInstance would register DLL-local class */
2126 wcA.hIcon = NULL;
2127 wcA.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
2128 wcA.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
2129 wcA.lpszMenuName = NULL;
2130 wcA.lpszClassName = "RichEdit20A";
2131 bResult = RegisterClassA(&wcA);
2132 assert(bResult);
2133 wcA.lpszClassName = "RichEdit50A";
2134 bResult = RegisterClassA(&wcA);
2135 assert(bResult);
2137 /******************************************************************
2138 * CreateTextServices (RICHED20.4)
2140 * FIXME should be ITextHost instead of void*
2142 HRESULT WINAPI CreateTextServices(IUnknown *punkOuter, void *pITextHost,
2143 IUnknown **ppUnk)
2145 FIXME("stub\n");
2146 /* FIXME should support aggregation */
2147 if (punkOuter)
2148 return CLASS_E_NOAGGREGATION;
2150 return E_FAIL; /* E_NOTIMPL isn't allowed by MSDN */
2153 /******************************************************************
2154 * REExtendedRegisterClass (RICHED20.8)
2156 * FIXME undocumented
2158 void WINAPI REExtendedRegisterClass(void)
2160 FIXME("stub\n");