riched20: Implement CreateTextServices.
[wine.git] / dlls / riched20 / editor.c
blob19c7b3fd968fcc0d9a5a25463eb4cbea2829727a
1 /*
2 * RichEdit - functions dealing with editor object
4 * Copyright 2004 by Krzysztof Foltman
5 * Copyright 2005 by Cihan Altinay
6 * Copyright 2005 by Phil Krylov
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 /*
24 API implementation status:
26 Messages (ANSI versions not done yet)
27 - EM_AUTOURLDETECT 2.0
28 + EM_CANPASTE
29 + EM_CANREDO 2.0
30 + EM_CANUNDO
31 + EM_CHARFROMPOS
32 - EM_DISPLAYBAND
33 + EM_EMPTYUNDOBUFFER
34 + EM_EXGETSEL
35 - EM_EXLIMITTEXT
36 + EM_EXLINEFROMCHAR
37 + EM_EXSETSEL
38 + EM_FINDTEXT (only FR_DOWN flag implemented)
39 + EM_FINDTEXTEX (only FR_DOWN flag implemented)
40 - EM_FINDWORDBREAK
41 - EM_FMTLINES
42 - EM_FORMATRANGE
43 - EM_GETAUTOURLDETECT 2.0
44 - EM_GETBIDIOPTIONS 3.0
45 - EM_GETCHARFORMAT (partly done)
46 - EM_GETEDITSTYLE
47 + EM_GETEVENTMASK
48 + EM_GETFIRSTVISIBLELINE (can be optimized if needed)
49 - EM_GETIMECOLOR 1.0asian
50 - EM_GETIMECOMPMODE 2.0
51 - EM_GETIMEOPTIONS 1.0asian
52 - EM_GETIMESTATUS
53 - EM_GETLANGOPTIONS 2.0
54 - EM_GETLIMITTEXT
55 - EM_GETLINE
56 + EM_GETLINECOUNT returns number of rows, not of paragraphs
57 + EM_GETMODIFY
58 - EM_GETOLEINTERFACE
59 - EM_GETOPTIONS
60 + EM_GETPARAFORMAT
61 - EM_GETPASSWORDCHAR 2.0
62 - EM_GETPUNCTUATION 1.0asian
63 + EM_GETRECT
64 - EM_GETREDONAME 2.0
65 + EM_GETSEL
66 + EM_GETSELTEXT (ANSI&Unicode)
67 - EM_GETSCROLLPOS 3.0
68 ! - EM_GETTHUMB
69 - EM_GETTEXTEX 2.0
70 + EM_GETTEXTLENGTHEX (GTL_PRECISE unimplemented)
71 - EM_GETTEXTMODE 2.0
72 ? + EM_GETTEXTRANGE (ANSI&Unicode)
73 - EM_GETTYPOGRAPHYOPTIONS 3.0
74 - EM_GETUNDONAME
75 + EM_GETWORDBREAKPROC
76 - EM_GETWORDBREAKPROCEX
77 - EM_GETWORDWRAPMODE 1.0asian
78 + EM_GETZOOM 3.0
79 - EM_HIDESELECTION
80 - EM_LIMITTEXT
81 + EM_LINEFROMCHAR
82 + EM_LINEINDEX
83 + EM_LINELENGTH
84 + EM_LINESCROLL
85 - EM_PASTESPECIAL
86 + EM_POSFROMCHAR
87 + EM_REDO 2.0
88 + EM_REQUESTRESIZE
89 + EM_REPLACESEL (proper style?) ANSI&Unicode
90 - EM_SCROLL
91 - EM_SCROLLCARET
92 - EM_SELECTIONTYPE
93 - EM_SETBIDIOPTIONS 3.0
94 + EM_SETBKGNDCOLOR
95 - EM_SETCHARFORMAT (partly done, no ANSI)
96 - EM_SETEDITSTYLE
97 + EM_SETEVENTMASK (few notifications supported)
98 - EM_SETFONTSIZE
99 - EM_SETIMECOLOR 1.0asian
100 - EM_SETIMEOPTIONS 1.0asian
101 - EM_SETLANGOPTIONS 2.0
102 - EM_SETLIMITTEXT
103 + EM_SETMODIFY (not sure if implementation is correct)
104 - EM_SETOLECALLBACK
105 - EM_SETOPTIONS
106 - EM_SETPALETTE 2.0
107 + EM_SETPARAFORMAT
108 - EM_SETPASSWORDCHAR 2.0
109 - EM_SETPUNCTUATION 1.0asian
110 + EM_SETREADONLY no beep on modification attempt
111 + EM_SETRECT
112 + EM_SETRECTNP (EM_SETRECT without repainting)
113 + EM_SETSEL
114 - EM_SETSCROLLPOS 3.0
115 - EM_SETTABSTOPS 3.0
116 - EM_SETTARGETDEVICE
117 + EM_SETTEXTEX 3.0 (unicode only, no rich text insertion handling, proper style?)
118 - EM_SETTEXTMODE 2.0
119 - EM_SETTYPOGRAPHYOPTIONS 3.0
120 - EM_SETUNDOLIMIT 2.0
121 + EM_SETWORDBREAKPROC (used only for word movement at the moment)
122 - EM_SETWORDBREAKPROCEX
123 - EM_SETWORDWRAPMODE 1.0asian
124 + EM_SETZOOM 3.0
125 - EM_SHOWSCROLLBAR 2.0
126 - EM_STOPGROUPTYPING 2.0
127 + EM_STREAMIN
128 + EM_STREAMOUT
129 + EM_UNDO
130 + WM_CHAR
131 + WM_CLEAR
132 + WM_COPY
133 + WM_CUT
134 + WM_GETDLGCODE (the current implementation is incomplete)
135 + WM_GETTEXT (ANSI&Unicode)
136 + WM_GETTEXTLENGTH (ANSI version sucks)
137 + WM_PASTE
138 - WM_SETFONT
139 + WM_SETTEXT (resets undo stack !) (proper style?) ANSI&Unicode
140 - WM_STYLECHANGING
141 - WM_STYLECHANGED (things like read-only flag)
142 - WM_UNICHAR
144 Notifications
146 * EN_CHANGE (sent from the wrong place)
147 - EN_CORRECTTEXT
148 - EN_DROPFILES
149 - EN_ERRSPACE
150 - EN_HSCROLL
151 - EN_IMECHANGE
152 + EN_KILLFOCUS
153 - EN_LINK
154 - EN_MAXTEXT
155 - EN_MSGFILTER
156 - EN_OLEOPFAILED
157 - EN_PROTECTED
158 + EN_REQUESTRESIZE
159 - EN_SAVECLIPBOARD
160 + EN_SELCHANGE
161 + EN_SETFOCUS
162 - EN_STOPNOUNDO
163 * EN_UPDATE (sent from the wrong place)
164 - EN_VSCROLL
166 Styles
168 - ES_AUTOHSCROLL
169 - ES_AUTOVSCROLL
170 - ES_CENTER
171 - ES_DISABLENOSCROLL (scrollbar is always visible)
172 - ES_EX_NOCALLOLEINIT
173 - ES_LEFT
174 - ES_MULTILINE (currently single line controls aren't supported)
175 - ES_NOIME
176 - ES_READONLY (I'm not sure if beeping is the proper behaviour)
177 - ES_RIGHT
178 - ES_SAVESEL
179 - ES_SELFIME
180 - ES_SUNKEN
181 - ES_VERTICAL
182 - ES_WANTRETURN (don't know how to do WM_GETDLGCODE part)
183 - WS_SETFONT
184 - WS_HSCROLL
185 - WS_VSCROLL
189 * RICHED20 TODO (incomplete):
191 * - messages/styles/notifications listed above
192 * - Undo coalescing
193 * - add remaining CHARFORMAT/PARAFORMAT fields
194 * - right/center align should strip spaces from the beginning
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
211 * - ListBox & ComboBox not implemented
213 * Bugs that are probably fixed, but not so easy to verify:
214 * - EN_UPDATE/EN_CHANGE are handled very incorrectly (should be OK now)
215 * - undo for ME_JoinParagraphs doesn't store paragraph format ? (it does)
216 * - check/fix artificial EOL logic (bCursorAtEnd, hardly logical)
217 * - caret shouldn't be displayed when selection isn't empty
218 * - check refcounting in style management functions (looks perfect now, but no bugs is suspicious)
219 * - undo for setting default format (done, might be buggy)
220 * - styles might be not released properly (looks like they work like charm, but who knows?
224 #include "editor.h"
225 #include "commdlg.h"
226 #include "ole2.h"
227 #include "richole.h"
228 #include "winreg.h"
229 #define NO_SHLWAPI_STREAM
230 #include "shlwapi.h"
231 #include "imm.h"
232 #include "textserv.h"
233 #include "rtf.h"
235 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
237 int me_debug = 0;
238 HANDLE me_heap = NULL;
240 static BOOL ME_ListBoxRegistered = FALSE;
241 static BOOL ME_ComboBoxRegistered = FALSE;
243 static ME_TextBuffer *ME_MakeText(void) {
245 ME_TextBuffer *buf = ALLOC_OBJ(ME_TextBuffer);
247 ME_DisplayItem *p1 = ME_MakeDI(diTextStart);
248 ME_DisplayItem *p2 = ME_MakeDI(diTextEnd);
250 p1->prev = NULL;
251 p1->next = p2;
252 p2->prev = p1;
253 p2->next = NULL;
254 p1->member.para.next_para = p2;
255 p2->member.para.prev_para = p1;
256 p2->member.para.nCharOfs = 0;
258 buf->pFirst = p1;
259 buf->pLast = p2;
260 buf->pCharStyle = NULL;
262 return buf;
266 static LRESULT ME_StreamInText(ME_TextEditor *editor, DWORD dwFormat, ME_InStream *stream, ME_Style *style)
268 WCHAR wszText[STREAMIN_BUFFER_SIZE+1];
269 WCHAR *pText;
271 TRACE("%08lx %p\n", dwFormat, stream);
273 do {
274 long nWideChars = 0;
276 if (!stream->dwSize)
278 ME_StreamInFill(stream);
279 if (stream->editstream->dwError)
280 break;
281 if (!stream->dwSize)
282 break;
285 if (!(dwFormat & SF_UNICODE))
287 /* FIXME? this is doomed to fail on true MBCS like UTF-8, luckily they're unlikely to be used as CP_ACP */
288 nWideChars = MultiByteToWideChar(CP_ACP, 0, stream->buffer, stream->dwSize, wszText, STREAMIN_BUFFER_SIZE);
289 pText = wszText;
291 else
293 nWideChars = stream->dwSize >> 1;
294 pText = (WCHAR *)stream->buffer;
297 ME_InsertTextFromCursor(editor, 0, pText, nWideChars, style);
298 if (stream->dwSize == 0)
299 break;
300 stream->dwSize = 0;
301 } while(1);
302 ME_CommitUndo(editor);
303 ME_UpdateRepaint(editor);
304 return 0;
307 static void ME_RTFCharAttrHook(RTF_Info *info)
309 CHARFORMAT2W fmt;
310 fmt.cbSize = sizeof(fmt);
311 fmt.dwMask = 0;
313 switch(info->rtfMinor)
315 case rtfPlain:
316 /* FIXME add more flags once they're implemented */
317 fmt.dwMask = CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_COLOR | CFM_BACKCOLOR | CFM_SIZE | CFM_WEIGHT;
318 fmt.dwEffects = CFE_AUTOCOLOR | CFE_AUTOBACKCOLOR;
319 fmt.yHeight = 12*20; /* 12pt */
320 fmt.wWeight = 400;
321 break;
322 case rtfBold:
323 fmt.dwMask = CFM_BOLD;
324 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
325 break;
326 case rtfItalic:
327 fmt.dwMask = CFM_ITALIC;
328 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
329 break;
330 case rtfUnderline:
331 fmt.dwMask = CFM_UNDERLINE;
332 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
333 fmt.bUnderlineType = CFU_CF1UNDERLINE;
334 break;
335 case rtfNoUnderline:
336 fmt.dwMask = CFM_UNDERLINE;
337 fmt.dwEffects = 0;
338 break;
339 case rtfStrikeThru:
340 fmt.dwMask = CFM_STRIKEOUT;
341 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
342 break;
343 case rtfSubScript:
344 case rtfSuperScript:
345 case rtfSubScrShrink:
346 case rtfSuperScrShrink:
347 case rtfNoSuperSub:
348 fmt.dwMask = CFM_SUBSCRIPT|CFM_SUPERSCRIPT;
349 if (info->rtfMinor == rtfSubScrShrink) fmt.dwEffects = CFE_SUBSCRIPT;
350 if (info->rtfMinor == rtfSuperScrShrink) fmt.dwEffects = CFE_SUPERSCRIPT;
351 if (info->rtfMinor == rtfNoSuperSub) fmt.dwEffects = 0;
352 break;
353 case rtfInvisible:
354 fmt.dwMask = CFM_HIDDEN;
355 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
356 break;
357 case rtfBackColor:
358 fmt.dwMask = CFM_BACKCOLOR;
359 fmt.dwEffects = 0;
360 if (info->rtfParam == 0)
361 fmt.dwEffects = CFE_AUTOBACKCOLOR;
362 else if (info->rtfParam != rtfNoParam)
364 RTFColor *c = RTFGetColor(info, info->rtfParam);
365 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
367 break;
368 case rtfForeColor:
369 fmt.dwMask = CFM_COLOR;
370 fmt.dwEffects = 0;
371 if (info->rtfParam == 0)
372 fmt.dwEffects = CFE_AUTOCOLOR;
373 else if (info->rtfParam != rtfNoParam)
375 RTFColor *c = RTFGetColor(info, info->rtfParam);
376 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
378 break;
379 case rtfFontNum:
380 if (info->rtfParam != rtfNoParam)
382 RTFFont *f = RTFGetFont(info, info->rtfParam);
383 if (f)
385 MultiByteToWideChar(CP_ACP, 0, f->rtfFName, -1, fmt.szFaceName, sizeof(fmt.szFaceName)/sizeof(WCHAR));
386 fmt.szFaceName[sizeof(fmt.szFaceName)/sizeof(WCHAR)-1] = '\0';
387 fmt.bCharSet = f->rtfFCharSet;
388 fmt.dwMask = CFM_FACE | CFM_CHARSET;
391 break;
392 case rtfFontSize:
393 fmt.dwMask = CFM_SIZE;
394 if (info->rtfParam != rtfNoParam)
395 fmt.yHeight = info->rtfParam*10;
396 break;
398 if (fmt.dwMask) {
399 ME_Style *style2;
400 RTFFlushOutputBuffer(info);
401 /* FIXME too slow ? how come ? */
402 style2 = ME_ApplyStyle(info->style, &fmt);
403 ME_ReleaseStyle(info->style);
404 info->style = style2;
405 info->styleChanged = TRUE;
409 /* FIXME this function doesn't get any information about context of the RTF tag, which is very bad,
410 the same tags mean different things in different contexts */
411 static void ME_RTFParAttrHook(RTF_Info *info)
413 PARAFORMAT2 fmt;
414 fmt.cbSize = sizeof(fmt);
415 fmt.dwMask = 0;
417 switch(info->rtfMinor)
419 case rtfParDef: /* I'm not 100% sure what does it do, but I guess it restores default paragraph attributes */
420 fmt.dwMask = PFM_ALIGNMENT | PFM_TABSTOPS | PFM_OFFSET | PFM_STARTINDENT;
421 fmt.wAlignment = PFA_LEFT;
422 fmt.cTabCount = 0;
423 fmt.dxOffset = fmt.dxStartIndent = 0;
424 break;
425 case rtfFirstIndent:
426 ME_GetSelectionParaFormat(info->editor, &fmt);
427 fmt.dwMask = PFM_STARTINDENT | PFM_OFFSET;
428 fmt.dxStartIndent += info->rtfParam + fmt.dxOffset;
429 fmt.dxOffset = -info->rtfParam;
430 break;
431 case rtfLeftIndent:
432 ME_GetSelectionParaFormat(info->editor, &fmt);
433 fmt.dwMask = PFM_STARTINDENT;
434 fmt.dxStartIndent = -fmt.dxOffset + info->rtfParam;
435 break;
436 case rtfRightIndent:
437 fmt.dwMask = PFM_RIGHTINDENT;
438 fmt.dxRightIndent = info->rtfParam;
439 break;
440 case rtfQuadLeft:
441 case rtfQuadJust:
442 fmt.dwMask = PFM_ALIGNMENT;
443 fmt.wAlignment = PFA_LEFT;
444 break;
445 case rtfQuadRight:
446 fmt.dwMask = PFM_ALIGNMENT;
447 fmt.wAlignment = PFA_RIGHT;
448 break;
449 case rtfQuadCenter:
450 fmt.dwMask = PFM_ALIGNMENT;
451 fmt.wAlignment = PFA_CENTER;
452 break;
453 case rtfTabPos:
454 ME_GetSelectionParaFormat(info->editor, &fmt);
455 if (!(fmt.dwMask & PFM_TABSTOPS))
457 fmt.dwMask |= PFM_TABSTOPS;
458 fmt.cTabCount = 0;
460 if (fmt.cTabCount < MAX_TAB_STOPS)
461 fmt.rgxTabs[fmt.cTabCount++] = info->rtfParam;
462 break;
464 if (fmt.dwMask) {
465 RTFFlushOutputBuffer(info);
466 /* FIXME too slow ? how come ?*/
467 ME_SetSelectionParaFormat(info->editor, &fmt);
471 static void ME_RTFReadHook(RTF_Info *info) {
472 switch(info->rtfClass)
474 case rtfGroup:
475 switch(info->rtfMajor)
477 case rtfBeginGroup:
478 if (info->stackTop < maxStack) {
479 memcpy(&info->stack[info->stackTop].fmt, &info->style->fmt, sizeof(CHARFORMAT2W));
480 info->stack[info->stackTop].codePage = info->codePage;
481 info->stack[info->stackTop].unicodeLength = info->unicodeLength;
483 info->stackTop++;
484 info->styleChanged = FALSE;
485 break;
486 case rtfEndGroup:
488 ME_Style *s;
489 RTFFlushOutputBuffer(info);
490 if (info->stackTop<=1) {
491 info->rtfClass = rtfEOF;
492 return;
494 info->stackTop--;
495 assert(info->stackTop >= 0);
496 if (info->styleChanged)
498 /* FIXME too slow ? how come ? */
499 s = ME_ApplyStyle(info->style, &info->stack[info->stackTop].fmt);
500 ME_ReleaseStyle(info->style);
501 info->style = s;
502 info->codePage = info->stack[info->stackTop].codePage;
503 info->unicodeLength = info->stack[info->stackTop].unicodeLength;
505 break;
508 break;
509 case rtfControl:
510 switch(info->rtfMajor)
512 case rtfCharAttr:
513 ME_RTFCharAttrHook(info);
514 break;
515 case rtfParAttr:
516 ME_RTFParAttrHook(info);
517 break;
519 break;
523 void
524 ME_StreamInFill(ME_InStream *stream)
526 stream->editstream->dwError = stream->editstream->pfnCallback(stream->editstream->dwCookie,
527 (BYTE *)stream->buffer,
528 sizeof(stream->buffer),
529 (LONG *)&stream->dwSize);
530 stream->dwUsed = 0;
533 static LRESULT ME_StreamIn(ME_TextEditor *editor, DWORD format, EDITSTREAM *stream)
535 RTF_Info parser;
536 ME_Style *style;
537 int from, to, to2, nUndoMode;
538 ME_UndoItem *pUI;
539 int nEventMask = editor->nEventMask;
540 ME_InStream inStream;
542 TRACE("stream==%p hWnd==%p format==0x%X\n", stream, editor->hWnd, (UINT)format);
543 editor->nEventMask = 0;
545 ME_GetSelection(editor, &from, &to);
546 if (format & SFF_SELECTION) {
547 style = ME_GetSelectionInsertStyle(editor);
549 ME_InternalDeleteText(editor, from, to-from);
551 else {
552 style = editor->pBuffer->pDefaultStyle;
553 ME_AddRefStyle(style);
554 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
555 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
556 from = to = 0;
557 ME_ClearTempStyle(editor);
558 /* FIXME restore default paragraph formatting ! */
561 nUndoMode = editor->nUndoMode;
562 editor->nUndoMode = umIgnore;
564 inStream.editstream = stream;
565 inStream.editstream->dwError = 0;
566 inStream.dwSize = 0;
567 inStream.dwUsed = 0;
569 if (format & SF_RTF)
571 /* Check if it's really RTF, and if it is not, use plain text */
572 ME_StreamInFill(&inStream);
573 if (!inStream.editstream->dwError)
575 if (strncmp(inStream.buffer, "{\\rtf1", 6) && strncmp(inStream.buffer, "{\\urtf", 6))
577 format &= ~SF_RTF;
578 format |= SF_TEXT;
583 if (!inStream.editstream->dwError)
585 if (format & SF_RTF) {
586 /* setup the RTF parser */
587 memset(&parser, 0, sizeof parser);
588 RTFSetEditStream(&parser, &inStream);
589 parser.rtfFormat = format&(SF_TEXT|SF_RTF);
590 parser.hwndEdit = editor->hWnd;
591 parser.editor = editor;
592 parser.style = style;
593 WriterInit(&parser);
594 RTFInit(&parser);
595 RTFSetReadHook(&parser, ME_RTFReadHook);
596 BeginFile(&parser);
598 /* do the parsing */
599 RTFRead(&parser);
600 RTFFlushOutputBuffer(&parser);
601 RTFDestroy(&parser);
603 style = parser.style;
605 else if (format & SF_TEXT)
606 ME_StreamInText(editor, format, &inStream, style);
607 else
608 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
609 ME_GetSelection(editor, &to, &to2);
610 /* put the cursor at the top */
611 if (!(format & SFF_SELECTION))
612 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
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 ME_UpdateRepaint(editor);
630 if (!(format & SFF_SELECTION)) {
631 ME_ClearTempStyle(editor);
633 ME_MoveCaret(editor);
634 ME_SendSelChange(editor);
635 ME_SendRequestResize(editor, FALSE);
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 static BOOL
803 ME_KeyDown(ME_TextEditor *editor, WORD nKey)
805 BOOL ctrl_is_down = GetKeyState(VK_CONTROL) & 0x8000;
806 BOOL shift_is_down = GetKeyState(VK_SHIFT) & 0x8000;
808 switch (nKey)
810 case VK_LEFT:
811 case VK_RIGHT:
812 case VK_UP:
813 case VK_DOWN:
814 case VK_HOME:
815 case VK_END:
816 case VK_PRIOR:
817 case VK_NEXT:
818 ME_ArrowKey(editor, nKey, shift_is_down, ctrl_is_down);
819 return TRUE;
820 case VK_BACK:
821 case VK_DELETE:
822 /* FIXME backspace and delete aren't the same, they act different wrt paragraph style of the merged paragraph */
823 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY)
824 return FALSE;
825 if (ME_IsSelection(editor))
826 ME_DeleteSelection(editor);
827 else if (nKey == VK_DELETE || ME_ArrowKey(editor, VK_LEFT, FALSE, FALSE))
828 ME_DeleteTextAtCursor(editor, 1, 1);
829 else
830 return TRUE;
831 ME_QueueInvalidateFromCursor(editor, 1);
832 ME_UpdateRepaint(editor);
833 ME_SendRequestResize(editor, FALSE);
834 return TRUE;
836 default:
837 if (ctrl_is_down)
839 if (nKey == 'W')
841 CHARFORMAT2W chf;
842 char buf[2048];
844 ME_GetSelectionCharFormat(editor, &chf);
845 ME_DumpStyleToBuf(&chf, buf);
846 MessageBoxA(NULL, buf, "Style dump", MB_OK);
848 if (nKey == 'Q')
850 ME_CheckCharOffsets(editor);
854 return FALSE;
858 ME_TextEditor *ME_MakeEditor(HWND hWnd) {
859 ME_TextEditor *ed = ALLOC_OBJ(ME_TextEditor);
860 HDC hDC;
861 int i;
862 ed->hWnd = hWnd;
863 ed->bEmulateVersion10 = FALSE;
864 ed->pBuffer = ME_MakeText();
865 hDC = GetDC(hWnd);
866 ME_MakeFirstParagraph(hDC, ed->pBuffer);
867 ReleaseDC(hWnd, hDC);
868 ed->bCaretShown = FALSE;
869 ed->nCursors = 3;
870 ed->pCursors = ALLOC_N_OBJ(ME_Cursor, ed->nCursors);
871 ed->pCursors[0].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
872 ed->pCursors[0].nOffset = 0;
873 ed->pCursors[1].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
874 ed->pCursors[1].nOffset = 0;
875 ed->nLastTotalLength = ed->nTotalLength = 0;
876 ed->nUDArrowX = -1;
877 ed->nSequence = 0;
878 ed->rgbBackColor = -1;
879 ed->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
880 ed->bCaretAtEnd = FALSE;
881 ed->nEventMask = 0;
882 ed->nModifyStep = 0;
883 ed->pUndoStack = ed->pRedoStack = NULL;
884 ed->nUndoMode = umAddToUndo;
885 ed->nParagraphs = 1;
886 ed->nLastSelStart = ed->nLastSelEnd = 0;
887 ed->nScrollPosY = 0;
888 ed->nZoomNumerator = ed->nZoomDenominator = 0;
889 ed->bRedraw = TRUE;
890 ed->nInvalidOfs = -1;
891 ed->pfnWordBreak = NULL;
892 GetClientRect(hWnd, &ed->rcFormat);
893 for (i=0; i<HFONT_CACHE_SIZE; i++)
895 ed->pFontCache[i].nRefs = 0;
896 ed->pFontCache[i].nAge = 0;
897 ed->pFontCache[i].hFont = NULL;
899 ME_CheckCharOffsets(ed);
900 return ed;
903 typedef struct tagME_GlobalDestStruct
905 HGLOBAL hData;
906 int nLength;
907 } ME_GlobalDestStruct;
909 static DWORD CALLBACK ME_AppendToHGLOBAL(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
911 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
912 int nMaxSize;
913 BYTE *pDest;
915 nMaxSize = GlobalSize(pData->hData);
916 if (pData->nLength+cb+1 >= cb)
918 /* round up to 2^17 */
919 int nNewSize = (((nMaxSize+cb+1)|0x1FFFF)+1) & 0xFFFE0000;
920 pData->hData = GlobalReAlloc(pData->hData, nNewSize, 0);
922 pDest = (BYTE *)GlobalLock(pData->hData);
923 memcpy(pDest + pData->nLength, lpBuff, cb);
924 pData->nLength += cb;
925 pDest[pData->nLength] = '\0';
926 GlobalUnlock(pData->hData);
927 *pcb = cb;
929 return 0;
932 static DWORD CALLBACK ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
934 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
935 int i;
936 WORD *pSrc, *pDest;
938 cb = cb >> 1;
939 pDest = (WORD *)lpBuff;
940 pSrc = (WORD *)GlobalLock(pData->hData);
941 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
942 pDest[i] = pSrc[pData->nLength+i];
944 pData->nLength += i;
945 *pcb = 2*i;
946 GlobalUnlock(pData->hData);
947 return 0;
950 static DWORD CALLBACK ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
952 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
953 int i;
954 BYTE *pSrc, *pDest;
956 pDest = lpBuff;
957 pSrc = (BYTE *)GlobalLock(pData->hData);
958 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
959 pDest[i] = pSrc[pData->nLength+i];
961 pData->nLength += i;
962 *pcb = i;
963 GlobalUnlock(pData->hData);
964 return 0;
968 void ME_DestroyEditor(ME_TextEditor *editor)
970 ME_DisplayItem *pFirst = editor->pBuffer->pFirst;
971 ME_DisplayItem *p = pFirst, *pNext = NULL;
972 int i;
974 ME_ClearTempStyle(editor);
975 ME_EmptyUndoStack(editor);
976 while(p) {
977 pNext = p->next;
978 ME_DestroyDisplayItem(p);
979 p = pNext;
981 ME_ReleaseStyle(editor->pBuffer->pDefaultStyle);
982 for (i=0; i<HFONT_CACHE_SIZE; i++)
984 if (editor->pFontCache[i].hFont)
985 DeleteObject(editor->pFontCache[i].hFont);
987 DeleteObject(editor->hbrBackground);
989 FREE_OBJ(editor);
992 static const WCHAR wszClassName[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '2', '0', 'W', 0};
993 static const WCHAR wszClassName50[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '5', '0', 'W', 0};
994 static const WCHAR wszClassNameListBox[] = {'R','E','L','i','s','t','B','o','x','2','0','W', 0};
995 static const WCHAR wszClassNameComboBox[] = {'R','E','C','o','m','b','o','B','o','x','2','0','W', 0};
997 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
999 TRACE("\n");
1000 switch (fdwReason)
1002 case DLL_PROCESS_ATTACH:
1003 DisableThreadLibraryCalls(hinstDLL);
1004 me_heap = HeapCreate (0, 0x10000, 0);
1005 ME_RegisterEditorClass(hinstDLL);
1006 break;
1008 case DLL_PROCESS_DETACH:
1009 UnregisterClassW(wszClassName, 0);
1010 UnregisterClassW(wszClassName50, 0);
1011 UnregisterClassA("RichEdit20A", 0);
1012 UnregisterClassA("RichEdit50A", 0);
1013 if (ME_ListBoxRegistered)
1014 UnregisterClassW(wszClassNameListBox, 0);
1015 if (ME_ComboBoxRegistered)
1016 UnregisterClassW(wszClassNameComboBox, 0);
1017 HeapDestroy (me_heap);
1018 me_heap = NULL;
1019 break;
1021 return TRUE;
1025 #define UNSUPPORTED_MSG(e) \
1026 case e: \
1027 FIXME(#e ": stub\n"); \
1028 return DefWindowProcW(hWnd, msg, wParam, lParam);
1030 static const char * const edit_messages[] = {
1031 "EM_GETSEL",
1032 "EM_SETSEL",
1033 "EM_GETRECT",
1034 "EM_SETRECT",
1035 "EM_SETRECTNP",
1036 "EM_SCROLL",
1037 "EM_LINESCROLL",
1038 "EM_SCROLLCARET",
1039 "EM_GETMODIFY",
1040 "EM_SETMODIFY",
1041 "EM_GETLINECOUNT",
1042 "EM_LINEINDEX",
1043 "EM_SETHANDLE",
1044 "EM_GETHANDLE",
1045 "EM_GETTHUMB",
1046 "EM_UNKNOWN_BF",
1047 "EM_UNKNOWN_C0",
1048 "EM_LINELENGTH",
1049 "EM_REPLACESEL",
1050 "EM_UNKNOWN_C3",
1051 "EM_GETLINE",
1052 "EM_LIMITTEXT",
1053 "EM_CANUNDO",
1054 "EM_UNDO",
1055 "EM_FMTLINES",
1056 "EM_LINEFROMCHAR",
1057 "EM_UNKNOWN_CA",
1058 "EM_SETTABSTOPS",
1059 "EM_SETPASSWORDCHAR",
1060 "EM_EMPTYUNDOBUFFER",
1061 "EM_GETFIRSTVISIBLELINE",
1062 "EM_SETREADONLY",
1063 "EM_SETWORDBREAKPROC",
1064 "EM_GETWORDBREAKPROC",
1065 "EM_GETPASSWORDCHAR",
1066 "EM_SETMARGINS",
1067 "EM_GETMARGINS",
1068 "EM_GETLIMITTEXT",
1069 "EM_POSFROMCHAR",
1070 "EM_CHARFROMPOS"
1073 static const char * const richedit_messages[] = {
1074 "EM_CANPASTE",
1075 "EM_DISPLAYBAND",
1076 "EM_EXGETSEL",
1077 "EM_EXLIMITTEXT",
1078 "EM_EXLINEFROMCHAR",
1079 "EM_EXSETSEL",
1080 "EM_FINDTEXT",
1081 "EM_FORMATRANGE",
1082 "EM_GETCHARFORMAT",
1083 "EM_GETEVENTMASK",
1084 "EM_GETOLEINTERFACE",
1085 "EM_GETPARAFORMAT",
1086 "EM_GETSELTEXT",
1087 "EM_HIDESELECTION",
1088 "EM_PASTESPECIAL",
1089 "EM_REQUESTRESIZE",
1090 "EM_SELECTIONTYPE",
1091 "EM_SETBKGNDCOLOR",
1092 "EM_SETCHARFORMAT",
1093 "EM_SETEVENTMASK",
1094 "EM_SETOLECALLBACK",
1095 "EM_SETPARAFORMAT",
1096 "EM_SETTARGETDEVICE",
1097 "EM_STREAMIN",
1098 "EM_STREAMOUT",
1099 "EM_GETTEXTRANGE",
1100 "EM_FINDWORDBREAK",
1101 "EM_SETOPTIONS",
1102 "EM_GETOPTIONS",
1103 "EM_FINDTEXTEX",
1104 "EM_GETWORDBREAKPROCEX",
1105 "EM_SETWORDBREAKPROCEX",
1106 "EM_SETUNDOLIMIT",
1107 "EM_UNKNOWN_USER_83",
1108 "EM_REDO",
1109 "EM_CANREDO",
1110 "EM_GETUNDONAME",
1111 "EM_GETREDONAME",
1112 "EM_STOPGROUPTYPING",
1113 "EM_SETTEXTMODE",
1114 "EM_GETTEXTMODE",
1115 "EM_AUTOURLDETECT",
1116 "EM_GETAUTOURLDETECT",
1117 "EM_SETPALETTE",
1118 "EM_GETTEXTEX",
1119 "EM_GETTEXTLENGTHEX",
1120 "EM_SHOWSCROLLBAR",
1121 "EM_SETTEXTEX",
1122 "EM_UNKNOWN_USER_98",
1123 "EM_UNKNOWN_USER_99",
1124 "EM_SETPUNCTUATION",
1125 "EM_GETPUNCTUATION",
1126 "EM_SETWORDWRAPMODE",
1127 "EM_GETWORDWRAPMODE",
1128 "EM_SETIMECOLOR",
1129 "EM_GETIMECOLOR",
1130 "EM_SETIMEOPTIONS",
1131 "EM_GETIMEOPTIONS",
1132 "EM_CONVPOSITION",
1133 "EM_UNKNOWN_USER_109",
1134 "EM_UNKNOWN_USER_110",
1135 "EM_UNKNOWN_USER_111",
1136 "EM_UNKNOWN_USER_112",
1137 "EM_UNKNOWN_USER_113",
1138 "EM_UNKNOWN_USER_114",
1139 "EM_UNKNOWN_USER_115",
1140 "EM_UNKNOWN_USER_116",
1141 "EM_UNKNOWN_USER_117",
1142 "EM_UNKNOWN_USER_118",
1143 "EM_UNKNOWN_USER_119",
1144 "EM_SETLANGOPTIONS",
1145 "EM_GETLANGOPTIONS",
1146 "EM_GETIMECOMPMODE",
1147 "EM_FINDTEXTW",
1148 "EM_FINDTEXTEXW",
1149 "EM_RECONVERSION",
1150 "EM_SETIMEMODEBIAS",
1151 "EM_GETIMEMODEBIAS"
1154 static const char *
1155 get_msg_name(UINT msg)
1157 if (msg >= EM_GETSEL && msg <= EM_SETLIMITTEXT)
1158 return edit_messages[msg - EM_GETSEL];
1159 if (msg >= EM_CANPASTE && msg <= EM_GETIMEMODEBIAS)
1160 return richedit_messages[msg - EM_CANPASTE];
1161 return "";
1164 /******************************************************************
1165 * RichEditANSIWndProc (RICHED20.10)
1167 LRESULT WINAPI RichEditANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
1168 SCROLLINFO si;
1169 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongW(hWnd, 0);
1171 TRACE("hWnd %p msg %04x (%s) %08x %08lx\n",
1172 hWnd, msg, get_msg_name(msg), wParam, lParam);
1174 switch(msg) {
1176 UNSUPPORTED_MSG(EM_AUTOURLDETECT)
1177 UNSUPPORTED_MSG(EM_DISPLAYBAND)
1178 UNSUPPORTED_MSG(EM_EXLIMITTEXT)
1179 UNSUPPORTED_MSG(EM_FINDWORDBREAK)
1180 UNSUPPORTED_MSG(EM_FMTLINES)
1181 UNSUPPORTED_MSG(EM_FORMATRANGE)
1182 UNSUPPORTED_MSG(EM_GETAUTOURLDETECT)
1183 UNSUPPORTED_MSG(EM_GETBIDIOPTIONS)
1184 UNSUPPORTED_MSG(EM_GETEDITSTYLE)
1185 UNSUPPORTED_MSG(EM_GETIMECOMPMODE)
1186 /* UNSUPPORTED_MSG(EM_GETIMESTATUS) missing in Wine headers */
1187 UNSUPPORTED_MSG(EM_GETLANGOPTIONS)
1188 UNSUPPORTED_MSG(EM_GETLIMITTEXT)
1189 UNSUPPORTED_MSG(EM_GETLINE)
1190 /* UNSUPPORTED_MSG(EM_GETOLEINTERFACE) separate stub */
1191 UNSUPPORTED_MSG(EM_GETOPTIONS)
1192 UNSUPPORTED_MSG(EM_GETPASSWORDCHAR)
1193 UNSUPPORTED_MSG(EM_GETREDONAME)
1194 UNSUPPORTED_MSG(EM_GETSCROLLPOS)
1195 UNSUPPORTED_MSG(EM_GETTEXTMODE)
1196 UNSUPPORTED_MSG(EM_GETTYPOGRAPHYOPTIONS)
1197 UNSUPPORTED_MSG(EM_GETUNDONAME)
1198 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX)
1199 UNSUPPORTED_MSG(EM_HIDESELECTION)
1200 UNSUPPORTED_MSG(EM_LIMITTEXT) /* also known as EM_SETLIMITTEXT */
1201 UNSUPPORTED_MSG(EM_PASTESPECIAL)
1202 UNSUPPORTED_MSG(EM_SCROLL)
1203 UNSUPPORTED_MSG(EM_SCROLLCARET)
1204 UNSUPPORTED_MSG(EM_SELECTIONTYPE)
1205 UNSUPPORTED_MSG(EM_SETBIDIOPTIONS)
1206 UNSUPPORTED_MSG(EM_SETEDITSTYLE)
1207 UNSUPPORTED_MSG(EM_SETFONTSIZE)
1208 UNSUPPORTED_MSG(EM_SETLANGOPTIONS)
1209 UNSUPPORTED_MSG(EM_SETOLECALLBACK)
1210 UNSUPPORTED_MSG(EM_SETOPTIONS)
1211 UNSUPPORTED_MSG(EM_SETPALETTE)
1212 UNSUPPORTED_MSG(EM_SETPASSWORDCHAR)
1213 UNSUPPORTED_MSG(EM_SETSCROLLPOS)
1214 UNSUPPORTED_MSG(EM_SETTABSTOPS)
1215 UNSUPPORTED_MSG(EM_SETTARGETDEVICE)
1216 UNSUPPORTED_MSG(EM_SETTEXTMODE)
1217 UNSUPPORTED_MSG(EM_SETTYPOGRAPHYOPTIONS)
1218 UNSUPPORTED_MSG(EM_SETUNDOLIMIT)
1219 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX)
1220 UNSUPPORTED_MSG(EM_SHOWSCROLLBAR)
1221 UNSUPPORTED_MSG(WM_SETFONT)
1222 UNSUPPORTED_MSG(WM_STYLECHANGING)
1223 UNSUPPORTED_MSG(WM_STYLECHANGED)
1224 /* UNSUPPORTED_MSG(WM_UNICHAR) FIXME missing in Wine headers */
1226 /* Messages specific to Richedit controls */
1228 case EM_STREAMIN:
1229 return ME_StreamIn(editor, wParam, (EDITSTREAM*)lParam);
1230 case EM_STREAMOUT:
1231 return ME_StreamOut(editor, wParam, (EDITSTREAM *)lParam);
1232 case WM_GETDLGCODE:
1234 UINT code = DLGC_WANTCHARS|DLGC_WANTARROWS;
1235 if (GetWindowLongW(hWnd, GWL_STYLE)&ES_WANTRETURN)
1236 code |= 0; /* FIXME what can we do here ? ask for messages and censor them ? */
1237 return code;
1239 case WM_NCCREATE:
1241 CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
1242 TRACE("WM_NCCREATE: style 0x%08lx\n", pcs->style);
1243 editor = ME_MakeEditor(hWnd);
1244 SetWindowLongW(hWnd, 0, (long)editor);
1245 pcs = 0; /* ignore */
1246 return TRUE;
1248 case EM_EMPTYUNDOBUFFER:
1249 ME_EmptyUndoStack(editor);
1250 return 0;
1251 case EM_GETSEL:
1253 /* Note: wParam/lParam can be NULL */
1254 UINT from, to;
1255 PUINT pfrom = wParam ? (PUINT)wParam : &from;
1256 PUINT pto = lParam ? (PUINT)lParam : &to;
1257 ME_GetSelection(editor, (int *)pfrom, (int *)pto);
1258 if ((*pfrom|*pto) & 0xFFFF0000)
1259 return -1;
1260 return MAKELONG(*pfrom,*pto);
1262 case EM_EXGETSEL:
1264 CHARRANGE *pRange = (CHARRANGE *)lParam;
1265 ME_GetSelection(editor, (int *)&pRange->cpMin, (int *)&pRange->cpMax);
1266 TRACE("EM_EXGETSEL = (%ld,%ld)\n", pRange->cpMin, pRange->cpMax);
1267 return 0;
1269 case EM_CANUNDO:
1270 return editor->pUndoStack != NULL;
1271 case EM_CANREDO:
1272 return editor->pRedoStack != NULL;
1273 case WM_UNDO: /* FIXME: actually not the same */
1274 case EM_UNDO:
1275 ME_Undo(editor);
1276 return 0;
1277 case EM_REDO:
1278 ME_Redo(editor);
1279 return 0;
1280 case EM_SETSEL:
1282 ME_InvalidateSelection(editor);
1283 ME_SetSelection(editor, wParam, lParam);
1284 ME_InvalidateSelection(editor);
1285 ME_SendSelChange(editor);
1286 return 0;
1288 case EM_EXSETSEL:
1290 CHARRANGE *pRange = (CHARRANGE *)lParam;
1291 TRACE("EM_EXSETSEL (%ld,%ld)\n", pRange->cpMin, pRange->cpMax);
1292 ME_InvalidateSelection(editor);
1293 ME_SetSelection(editor, pRange->cpMin, pRange->cpMax);
1294 ME_InvalidateSelection(editor);
1295 ME_SendSelChange(editor);
1296 return 0;
1298 case EM_SETTEXTEX:
1300 LPWSTR wszText = (LPWSTR)lParam;
1301 SETTEXTEX *pStruct = (SETTEXTEX *)wParam;
1302 size_t len = lstrlenW(wszText);
1303 int from, to;
1304 ME_Style *style;
1305 TRACE("EM_SETTEXEX - %s, flags %d, cp %d\n", debugstr_w(wszText), (int)pStruct->flags, pStruct->codepage);
1306 if (pStruct->codepage != 1200) {
1307 FIXME("EM_SETTEXTEX only supports unicode right now!\n");
1308 return 0;
1310 /* FIXME: this should support RTF strings too, according to MSDN */
1311 if (pStruct->flags & ST_SELECTION) {
1312 ME_GetSelection(editor, &from, &to);
1313 style = ME_GetSelectionInsertStyle(editor);
1314 ME_InternalDeleteText(editor, from, to - from);
1315 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
1316 ME_ReleaseStyle(style);
1318 else {
1319 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
1320 ME_InsertTextFromCursor(editor, 0, wszText, -1, editor->pBuffer->pDefaultStyle);
1321 len = 1;
1323 ME_CommitUndo(editor);
1324 if (!(pStruct->flags & ST_KEEPUNDO))
1325 ME_EmptyUndoStack(editor);
1326 ME_UpdateRepaint(editor);
1327 return len;
1329 case EM_SETBKGNDCOLOR:
1331 LRESULT lColor = ME_GetBackColor(editor);
1332 if (editor->rgbBackColor != -1)
1333 DeleteObject(editor->hbrBackground);
1334 if (wParam)
1336 editor->rgbBackColor = -1;
1337 editor->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
1339 else
1341 editor->rgbBackColor = lParam;
1342 editor->hbrBackground = CreateSolidBrush(editor->rgbBackColor);
1344 if (editor->bRedraw)
1346 InvalidateRect(hWnd, NULL, TRUE);
1347 UpdateWindow(hWnd);
1349 return lColor;
1351 case EM_GETMODIFY:
1352 return editor->nModifyStep == 0 ? 0 : 1;
1353 case EM_SETMODIFY:
1355 if (wParam)
1356 editor->nModifyStep = 0x80000000;
1357 else
1358 editor->nModifyStep = 0;
1360 return 0;
1362 case EM_SETREADONLY:
1364 long nStyle = GetWindowLongW(hWnd, GWL_STYLE);
1365 if (wParam)
1366 nStyle |= ES_READONLY;
1367 else
1368 nStyle &= ~ES_READONLY;
1369 SetWindowLongW(hWnd, GWL_STYLE, nStyle);
1370 return 0;
1372 case EM_SETEVENTMASK:
1374 DWORD nOldMask = editor->nEventMask;
1376 editor->nEventMask = lParam;
1377 return nOldMask;
1379 case EM_GETEVENTMASK:
1380 return editor->nEventMask;
1381 case EM_SETCHARFORMAT:
1383 CHARFORMAT2W buf, *p;
1384 BOOL bRepaint = TRUE;
1385 p = ME_ToCF2W(&buf, (CHARFORMAT2W *)lParam);
1386 if (!wParam)
1387 ME_SetDefaultCharFormat(editor, p);
1388 else if (wParam == (SCF_WORD | SCF_SELECTION))
1389 FIXME("EM_SETCHARFORMAT: word selection not supported\n");
1390 else if (wParam == SCF_ALL)
1391 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), p);
1392 else {
1393 int from, to;
1394 ME_GetSelection(editor, &from, &to);
1395 bRepaint = (from != to);
1396 ME_SetSelectionCharFormat(editor, p);
1398 ME_CommitUndo(editor);
1399 if (bRepaint)
1400 ME_UpdateRepaint(editor);
1401 return 0;
1403 case EM_GETCHARFORMAT:
1405 CHARFORMAT2W tmp, *dst = (CHARFORMAT2W *)lParam;
1406 if (dst->cbSize != sizeof(CHARFORMATA) &&
1407 dst->cbSize != sizeof(CHARFORMATW) &&
1408 dst->cbSize != sizeof(CHARFORMAT2A) &&
1409 dst->cbSize != sizeof(CHARFORMAT2W))
1410 return 0;
1411 tmp.cbSize = sizeof(tmp);
1412 if (!wParam)
1413 ME_GetDefaultCharFormat(editor, &tmp);
1414 else
1415 ME_GetSelectionCharFormat(editor, &tmp);
1416 ME_CopyToCFAny(dst, &tmp);
1417 return tmp.dwMask;
1419 case EM_SETPARAFORMAT:
1420 ME_SetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
1421 ME_UpdateRepaint(editor);
1422 ME_CommitUndo(editor);
1423 return 0;
1424 case EM_GETPARAFORMAT:
1425 ME_GetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
1426 return 0;
1427 case EM_GETFIRSTVISIBLELINE:
1429 ME_DisplayItem *p = editor->pBuffer->pFirst;
1430 int y = editor->nScrollPosY;
1431 int ypara = 0;
1432 int count = 0;
1433 int ystart, yend;
1434 while(p) {
1435 p = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
1436 if (p->type == diTextEnd)
1437 break;
1438 if (p->type == diParagraph) {
1439 ypara = p->member.para.nYPos;
1440 continue;
1442 ystart = ypara + p->member.row.nYPos;
1443 yend = ystart + p->member.row.nHeight;
1444 if (y < yend) {
1445 break;
1447 count++;
1449 return count;
1451 case EM_LINESCROLL:
1453 int nPos = editor->nScrollPosY, nEnd= editor->nTotalLength - editor->sizeWindow.cy;
1454 nPos += 8 * lParam; /* FIXME follow the original */
1455 if (nPos>=nEnd)
1456 nPos = nEnd;
1457 if (nPos<0)
1458 nPos = 0;
1459 if (nPos != editor->nScrollPosY) {
1460 int dy = editor->nScrollPosY - nPos;
1461 editor->nScrollPosY = nPos;
1462 SetScrollPos(hWnd, SB_VERT, nPos, TRUE);
1463 if (editor->bRedraw)
1465 ScrollWindow(hWnd, 0, dy, NULL, NULL);
1466 UpdateWindow(hWnd);
1469 return TRUE; /* Should return false if a single line richedit control */
1471 case WM_CLEAR:
1473 int from, to;
1474 ME_GetSelection(editor, &from, &to);
1475 ME_InternalDeleteText(editor, from, to-from);
1476 ME_CommitUndo(editor);
1477 ME_UpdateRepaint(editor);
1478 return 0;
1480 case EM_REPLACESEL:
1482 int from, to;
1483 ME_Style *style;
1484 LPWSTR wszText = ME_ToUnicode(hWnd, (void *)lParam);
1485 size_t len = lstrlenW(wszText);
1486 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText));
1488 ME_GetSelection(editor, &from, &to);
1489 style = ME_GetSelectionInsertStyle(editor);
1490 ME_InternalDeleteText(editor, from, to-from);
1491 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
1492 ME_ReleaseStyle(style);
1493 ME_EndToUnicode(hWnd, wszText);
1494 /* drop temporary style if line end */
1495 /* FIXME question: does abc\n mean: put abc, clear temp style, put \n? (would require a change) */
1496 if (len>0 && wszText[len-1] == '\n')
1497 ME_ClearTempStyle(editor);
1499 ME_CommitUndo(editor);
1500 if (!wParam)
1501 ME_EmptyUndoStack(editor);
1502 ME_UpdateRepaint(editor);
1503 return 0;
1505 case WM_SETTEXT:
1507 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
1508 if (lParam)
1510 LPWSTR wszText = ME_ToUnicode(hWnd, (void *)lParam);
1511 TRACE("WM_SETTEXT lParam==%lx\n",lParam);
1512 TRACE("WM_SETTEXT - %s\n", debugstr_w(wszText)); /* debugstr_w() */
1513 if (lstrlenW(wszText) > 0)
1515 /* uses default style! */
1516 ME_InsertTextFromCursor(editor, 0, wszText, -1, editor->pBuffer->pDefaultStyle);
1518 ME_EndToUnicode(hWnd, wszText);
1520 else
1521 TRACE("WM_SETTEXT - NULL\n");
1522 ME_CommitUndo(editor);
1523 ME_EmptyUndoStack(editor);
1524 ME_SetSelection(editor, 0, 0);
1525 ME_UpdateRepaint(editor);
1526 return 0;
1528 case EM_CANPASTE:
1530 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
1531 if (IsClipboardFormatAvailable(nRTFFormat))
1532 return TRUE;
1533 if (IsClipboardFormatAvailable(CF_UNICODETEXT))
1534 return TRUE;
1535 return FALSE;
1537 case WM_PASTE:
1539 DWORD dwFormat = 0;
1540 EDITSTREAM es;
1541 ME_GlobalDestStruct gds;
1542 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
1543 UINT cf = 0;
1545 if (IsClipboardFormatAvailable(nRTFFormat))
1546 cf = nRTFFormat, dwFormat = SF_RTF;
1547 else if (IsClipboardFormatAvailable(CF_UNICODETEXT))
1548 cf = CF_UNICODETEXT, dwFormat = SF_TEXT|SF_UNICODE;
1549 else
1550 return 0;
1552 if (!OpenClipboard(hWnd))
1553 return 0;
1554 gds.hData = GetClipboardData(cf);
1555 gds.nLength = 0;
1556 es.dwCookie = (DWORD)&gds;
1557 es.pfnCallback = dwFormat == SF_RTF ? ME_ReadFromHGLOBALRTF : ME_ReadFromHGLOBALUnicode;
1558 SendMessageW(hWnd, EM_STREAMIN, dwFormat|SFF_SELECTION, (LPARAM)&es);
1560 CloseClipboard();
1561 return 0;
1563 case WM_CUT:
1564 case WM_COPY:
1566 int from, to, pars;
1567 WCHAR *data;
1568 HANDLE hData;
1569 EDITSTREAM es;
1570 ME_GlobalDestStruct gds;
1572 if (!OpenClipboard(hWnd))
1573 return 0;
1575 EmptyClipboard();
1576 ME_GetSelection(editor, &from, &to);
1577 pars = ME_CountParagraphsBetween(editor, from, to);
1578 hData = GlobalAlloc(GMEM_MOVEABLE, sizeof(WCHAR)*(to-from+pars+1));
1579 data = (WCHAR *)GlobalLock(hData);
1580 ME_GetTextW(editor, data, from, to-from, TRUE);
1581 GlobalUnlock(hData);
1583 gds.hData = GlobalAlloc(GMEM_MOVEABLE, 0);
1584 gds.nLength = 0;
1585 es.dwCookie = (DWORD)&gds;
1586 es.pfnCallback = ME_AppendToHGLOBAL;
1587 SendMessageW(hWnd, EM_STREAMOUT, SFF_SELECTION|SF_RTF, (LPARAM)&es);
1588 GlobalReAlloc(gds.hData, gds.nLength+1, 0);
1590 SetClipboardData(CF_UNICODETEXT, hData);
1591 SetClipboardData(RegisterClipboardFormatA("Rich Text Format"), gds.hData);
1593 CloseClipboard();
1594 if (msg == WM_CUT)
1596 ME_InternalDeleteText(editor, from, to-from);
1597 ME_CommitUndo(editor);
1598 ME_UpdateRepaint(editor);
1600 return 0;
1602 case WM_GETTEXTLENGTH:
1603 return ME_GetTextLength(editor);
1604 case EM_GETTEXTLENGTHEX:
1605 return ME_GetTextLengthEx(editor, (GETTEXTLENGTHEX *)wParam);
1606 case WM_GETTEXT:
1608 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
1609 tr.chrg.cpMin = 0;
1610 tr.chrg.cpMax = wParam-1;
1611 tr.lpstrText = (WCHAR *)lParam;
1612 return RichEditANSIWndProc(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
1614 case EM_GETTEXTEX:
1616 GETTEXTEX *ex = (GETTEXTEX*)wParam;
1617 int nStart, nCount;
1619 if (ex->flags & ~(GT_SELECTION | GT_USECRLF))
1620 FIXME("GETTEXTEX flags 0x%08lx not supported\n", ex->flags & ~(GT_SELECTION | GT_USECRLF));
1622 if (ex->flags & GT_SELECTION)
1624 ME_GetSelection(editor, &nStart, &nCount);
1625 nCount -= nStart;
1626 nCount = min(nCount, ex->cb - 1);
1628 else
1630 nStart = 0;
1631 nCount = ex->cb - 1;
1633 if (ex->codepage == 1200 || IsWindowUnicode(hWnd))
1635 nCount = min(nCount, ex->cb / sizeof(WCHAR) - 1);
1636 return ME_GetTextW(editor, (LPWSTR)lParam, nStart, nCount, ex->flags & GT_USECRLF);
1638 else
1640 /* potentially each char may be a CR, why calculate the exact value with O(N) when
1641 we can just take a bigger buffer? :) */
1642 int crlfmul = (ex->flags & GT_USECRLF) ? 2 : 1;
1643 LPWSTR buffer = HeapAlloc(GetProcessHeap(), 0, (crlfmul*nCount + 1) * sizeof(WCHAR));
1644 DWORD buflen = ex->cb;
1645 LRESULT rc;
1646 DWORD flags = 0;
1648 buflen = ME_GetTextW(editor, buffer, nStart, nCount, ex->flags & GT_USECRLF);
1649 rc = WideCharToMultiByte(ex->codepage, flags, buffer, buflen, (LPSTR)lParam, ex->cb, ex->lpDefaultChar, ex->lpUsedDefaultChar);
1651 HeapFree(GetProcessHeap(),0,buffer);
1652 return rc;
1655 case EM_GETSELTEXT:
1657 int from, to;
1658 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
1659 ME_GetSelection(editor, &from, &to);
1660 tr.chrg.cpMin = from;
1661 tr.chrg.cpMax = to;
1662 tr.lpstrText = (WCHAR *)lParam;
1663 return RichEditANSIWndProc(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
1665 case EM_GETTEXTRANGE:
1667 TEXTRANGEW *rng = (TEXTRANGEW *)lParam;
1668 TRACE("EM_GETTEXTRANGE min=%ld max=%ld unicode=%d emul1.0=%d length=%d\n",
1669 rng->chrg.cpMin, rng->chrg.cpMax, IsWindowUnicode(hWnd),
1670 editor->bEmulateVersion10, ME_GetTextLength(editor));
1671 if (IsWindowUnicode(hWnd))
1672 return ME_GetTextW(editor, rng->lpstrText, rng->chrg.cpMin, rng->chrg.cpMax-rng->chrg.cpMin, editor->bEmulateVersion10);
1673 else
1675 int nLen = rng->chrg.cpMax-rng->chrg.cpMin;
1676 WCHAR *p = ALLOC_N_OBJ(WCHAR, nLen+1);
1677 int nChars = ME_GetTextW(editor, p, rng->chrg.cpMin, nLen, editor->bEmulateVersion10);
1678 /* FIXME this is a potential security hole (buffer overrun)
1679 if you know more about wchar->mbyte conversion please explain
1681 WideCharToMultiByte(CP_ACP, 0, p, nChars+1, (char *)rng->lpstrText, nLen+1, NULL, NULL);
1682 FREE_OBJ(p);
1683 return nChars;
1686 case EM_GETLINECOUNT:
1688 ME_DisplayItem *item = editor->pBuffer->pFirst->next;
1689 int nRows = 0;
1691 while (item != editor->pBuffer->pLast)
1693 assert(item->type == diParagraph);
1694 nRows += item->member.para.nRows;
1695 item = item->member.para.next_para;
1697 TRACE("EM_GETLINECOUNT: nRows==%d\n", nRows);
1698 return max(1, nRows);
1700 case EM_LINEFROMCHAR:
1702 if (wParam == -1)
1703 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(editor, 1));
1704 else
1705 return ME_RowNumberFromCharOfs(editor, wParam);
1707 case EM_EXLINEFROMCHAR:
1709 return ME_RowNumberFromCharOfs(editor, lParam);
1711 case EM_LINEINDEX:
1713 ME_DisplayItem *item, *para;
1714 int nCharOfs;
1716 if (wParam == -1)
1717 item = ME_FindItemBack(editor->pCursors[0].pRun, diStartRow);
1718 else
1719 item = ME_FindRowWithNumber(editor, wParam);
1720 if (!item)
1721 return -1;
1722 para = ME_GetParagraph(item);
1723 item = ME_FindItemFwd(item, diRun);
1724 nCharOfs = para->member.para.nCharOfs + item->member.run.nCharOfs;
1725 TRACE("EM_LINEINDEX: nCharOfs==%d\n", nCharOfs);
1726 return nCharOfs;
1728 case EM_LINELENGTH:
1730 ME_DisplayItem *item, *item_end;
1731 int nChars = 0, nThisLineOfs = 0, nNextLineOfs = 0;
1733 if (wParam > ME_GetTextLength(editor))
1734 return 0;
1735 if (wParam == -1)
1737 FIXME("EM_LINELENGTH: returning number of unselected characters on lines with selection unsupported.\n");
1738 return 0;
1740 item = ME_FindItemAtOffset(editor, diRun, wParam, NULL);
1741 item = ME_RowStart(item);
1742 nThisLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item, diRun), 0);
1743 item_end = ME_FindItemFwd(item, diStartRow);
1744 if (item_end)
1745 nNextLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item_end, diRun), 0);
1746 else
1747 nNextLineOfs = ME_FindItemFwd(item, diParagraphOrEnd)->member.para.nCharOfs
1748 - (editor->bEmulateVersion10?2:1);
1749 nChars = nNextLineOfs - nThisLineOfs;
1750 TRACE("EM_LINELENGTH(%d)==%d\n",wParam, nChars);
1751 return nChars;
1753 case EM_FINDTEXT:
1755 FINDTEXTA *ft = (FINDTEXTA *)lParam;
1756 int nChars = MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, NULL, 0);
1757 WCHAR *tmp;
1759 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
1760 MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, tmp, nChars);
1761 return ME_FindText(editor, wParam, &ft->chrg, tmp, NULL);
1763 case EM_FINDTEXTEX:
1765 FINDTEXTEXA *ex = (FINDTEXTEXA *)lParam;
1766 int nChars = MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, NULL, 0);
1767 WCHAR *tmp;
1769 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
1770 MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, tmp, nChars);
1771 return ME_FindText(editor, wParam, &ex->chrg, tmp, &ex->chrgText);
1773 case EM_FINDTEXTW:
1775 FINDTEXTW *ft = (FINDTEXTW *)lParam;
1776 return ME_FindText(editor, wParam, &ft->chrg, ft->lpstrText, NULL);
1778 case EM_FINDTEXTEXW:
1780 FINDTEXTEXW *ex = (FINDTEXTEXW *)lParam;
1781 return ME_FindText(editor, wParam, &ex->chrg, ex->lpstrText, &ex->chrgText);
1783 case EM_GETZOOM:
1784 if (!wParam || !lParam)
1785 return FALSE;
1786 *(int *)wParam = editor->nZoomNumerator;
1787 *(int *)lParam = editor->nZoomDenominator;
1788 return TRUE;
1789 case EM_SETZOOM:
1790 return ME_SetZoom(editor, wParam, lParam);
1791 case EM_CHARFROMPOS:
1792 return ME_CharFromPos(editor, ((POINTL *)lParam)->x, ((POINTL *)lParam)->y);
1793 case EM_POSFROMCHAR:
1795 ME_DisplayItem *pRun;
1796 int nCharOfs, nOffset, nLength;
1797 POINTL pt = {0,0};
1799 nCharOfs = wParam;
1800 /* detect which API version we're dealing with */
1801 if (wParam >= 0x40000)
1802 nCharOfs = lParam;
1803 nLength = ME_GetTextLength(editor);
1805 if (nCharOfs < nLength) {
1806 ME_RunOfsFromCharOfs(editor, nCharOfs, &pRun, &nOffset);
1807 assert(pRun->type == diRun);
1808 pt.y = pRun->member.run.pt.y;
1809 pt.x = pRun->member.run.pt.x + ME_PointFromChar(editor, &pRun->member.run, nOffset);
1810 pt.y += ME_GetParagraph(pRun)->member.para.nYPos;
1811 } else {
1812 pt.x = 0;
1813 pt.y = editor->pBuffer->pLast->member.para.nYPos;
1815 if (wParam >= 0x40000) {
1816 *(POINTL *)wParam = pt;
1818 return MAKELONG( pt.x, pt.y );
1820 case WM_CREATE:
1821 ME_CommitUndo(editor);
1822 ME_WrapMarkedParagraphs(editor);
1823 ME_MoveCaret(editor);
1824 return 0;
1825 case WM_DESTROY:
1826 ME_DestroyEditor(editor);
1827 SetWindowLongW(hWnd, 0, 0);
1828 return 0;
1829 case WM_LBUTTONDOWN:
1830 SetFocus(hWnd);
1831 ME_LButtonDown(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
1832 SetCapture(hWnd);
1833 break;
1834 case WM_MOUSEMOVE:
1835 if (GetCapture() == hWnd)
1836 ME_MouseMove(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
1837 break;
1838 case WM_LBUTTONUP:
1839 if (GetCapture() == hWnd)
1840 ReleaseCapture();
1841 break;
1842 case WM_LBUTTONDBLCLK:
1843 ME_SelectWord(editor);
1844 break;
1845 case WM_PAINT:
1846 if (editor->bRedraw)
1848 HDC hDC;
1849 PAINTSTRUCT ps;
1851 hDC = BeginPaint(hWnd, &ps);
1852 ME_PaintContent(editor, hDC, FALSE, &ps.rcPaint);
1853 EndPaint(hWnd, &ps);
1855 break;
1856 case WM_SETFOCUS:
1857 ME_ShowCaret(editor);
1858 ME_SendOldNotify(editor, EN_SETFOCUS);
1859 return 0;
1860 case WM_KILLFOCUS:
1861 ME_HideCaret(editor);
1862 ME_SendOldNotify(editor, EN_KILLFOCUS);
1863 return 0;
1864 case WM_ERASEBKGND:
1866 if (editor->bRedraw)
1868 HDC hDC = (HDC)wParam;
1869 RECT rc;
1870 if (GetUpdateRect(hWnd,&rc,TRUE))
1872 FillRect(hDC, &rc, editor->hbrBackground);
1875 return 1;
1877 case WM_COMMAND:
1878 TRACE("editor wnd command = %d\n", LOWORD(wParam));
1879 return 0;
1880 case WM_KEYDOWN:
1881 if (ME_KeyDown(editor, LOWORD(wParam)))
1882 return 0;
1883 goto do_default;
1884 case WM_CHAR:
1886 WCHAR wstr = LOWORD(wParam);
1888 switch (wstr)
1890 case 3: /* Ctrl-C */
1891 SendMessageW(editor->hWnd, WM_COPY, 0, 0);
1892 return 0;
1895 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY) {
1896 MessageBeep(MB_ICONERROR);
1897 return 0; /* FIXME really 0 ? */
1900 switch (wstr)
1902 case 1: /* Ctrl-A */
1903 ME_SetSelection(editor, 0, -1);
1904 return 0;
1905 case 22: /* Ctrl-V */
1906 SendMessageW(editor->hWnd, WM_PASTE, 0, 0);
1907 return 0;
1908 case 24: /* Ctrl-X */
1909 SendMessageW(editor->hWnd, WM_CUT, 0, 0);
1910 return 0;
1911 case 25: /* Ctrl-Y */
1912 SendMessageW(editor->hWnd, EM_REDO, 0, 0);
1913 return 0;
1914 case 26: /* Ctrl-Z */
1915 SendMessageW(editor->hWnd, EM_UNDO, 0, 0);
1916 return 0;
1918 if (((unsigned)wstr)>=' ' || wstr=='\r' || wstr=='\t') {
1919 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
1920 ME_Style *style = ME_GetInsertStyle(editor, 0);
1921 ME_SaveTempStyle(editor);
1922 ME_InsertTextFromCursor(editor, 0, &wstr, 1, style);
1923 ME_ReleaseStyle(style);
1924 ME_CommitUndo(editor);
1925 ME_UpdateRepaint(editor);
1927 return 0;
1929 case WM_VSCROLL:
1931 int nPos = editor->nScrollPosY;
1932 si.cbSize = sizeof(SCROLLINFO);
1933 si.fMask = SIF_PAGE|SIF_POS|SIF_RANGE|SIF_TRACKPOS;
1934 GetScrollInfo(hWnd, SB_VERT, &si);
1935 switch(LOWORD(wParam)) {
1936 case SB_LINEUP:
1937 nPos -= 24; /* FIXME follow the original */
1938 if (nPos<0) nPos = 0;
1939 break;
1940 case SB_LINEDOWN:
1942 int nEnd = editor->nTotalLength - editor->sizeWindow.cy;
1943 nPos += 24; /* FIXME follow the original */
1944 if (nPos>=nEnd) nPos = nEnd;
1945 break;
1947 case SB_PAGEUP:
1948 nPos -= editor->sizeWindow.cy;
1949 if (nPos<0) nPos = 0;
1950 break;
1951 case SB_PAGEDOWN:
1952 nPos += editor->sizeWindow.cy;
1953 if (nPos>=editor->nTotalLength) nPos = editor->nTotalLength-1;
1954 break;
1955 case SB_THUMBTRACK:
1956 case SB_THUMBPOSITION:
1957 nPos = si.nTrackPos;
1958 break;
1960 if (nPos != editor->nScrollPosY) {
1961 int dy = editor->nScrollPosY - nPos;
1962 editor->nScrollPosY = nPos;
1963 SetScrollPos(hWnd, SB_VERT, nPos, TRUE);
1964 if (editor->bRedraw)
1966 ScrollWindow(hWnd, 0, dy, NULL, NULL);
1967 UpdateWindow(hWnd);
1970 break;
1972 case WM_MOUSEWHEEL:
1974 int gcWheelDelta = 0, nPos = editor->nScrollPosY, nEnd = editor->nTotalLength - editor->sizeWindow.cy;
1975 UINT pulScrollLines;
1976 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
1977 gcWheelDelta -= GET_WHEEL_DELTA_WPARAM(wParam);
1978 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
1979 nPos += pulScrollLines * (gcWheelDelta / WHEEL_DELTA) * 8; /* FIXME follow the original */
1980 if (nPos>=nEnd)
1981 nPos = nEnd;
1982 if (nPos<0)
1983 nPos = 0;
1984 if (nPos != editor->nScrollPosY) {
1985 int dy = editor->nScrollPosY - nPos;
1986 editor->nScrollPosY = nPos;
1987 SetScrollPos(hWnd, SB_VERT, nPos, TRUE);
1988 if (editor->bRedraw)
1990 ScrollWindow(hWnd, 0, dy, NULL, NULL);
1991 UpdateWindow(hWnd);
1994 break;
1996 case EM_GETRECT:
1998 *((RECT *)lParam) = editor->rcFormat;
1999 return 0;
2001 case EM_SETRECT:
2002 case EM_SETRECTNP:
2004 if (lParam)
2006 RECT *rc = (RECT *)lParam;
2008 if (wParam)
2010 editor->rcFormat.left += rc->left;
2011 editor->rcFormat.top += rc->top;
2012 editor->rcFormat.right += rc->right;
2013 editor->rcFormat.bottom += rc->bottom;
2015 else
2017 editor->rcFormat = *rc;
2020 else
2022 GetClientRect(hWnd, &editor->rcFormat);
2024 if (msg != EM_SETRECTNP)
2025 ME_RewrapRepaint(editor);
2026 return 0;
2028 case EM_REQUESTRESIZE:
2029 ME_SendRequestResize(editor, TRUE);
2030 return 0;
2031 case WM_SETREDRAW:
2032 editor->bRedraw = wParam;
2033 return 0;
2034 case WM_SIZE:
2036 GetClientRect(hWnd, &editor->rcFormat);
2037 ME_RewrapRepaint(editor);
2038 return DefWindowProcW(hWnd, msg, wParam, lParam);
2040 case EM_GETOLEINTERFACE:
2042 LPVOID *ppvObj = (LPVOID*) lParam;
2043 FIXME("EM_GETOLEINTERFACE %p: stub\n", ppvObj);
2044 return CreateIRichEditOle(ppvObj);
2046 case EM_GETWORDBREAKPROC:
2047 return (LRESULT)editor->pfnWordBreak;
2048 case EM_SETWORDBREAKPROC:
2050 EDITWORDBREAKPROCW pfnOld = editor->pfnWordBreak;
2052 editor->pfnWordBreak = (EDITWORDBREAKPROCW)lParam;
2053 return (LRESULT)pfnOld;
2055 default:
2056 do_default:
2057 return DefWindowProcW(hWnd, msg, wParam, lParam);
2059 return 0L;
2063 /******************************************************************
2064 * RichEdit10ANSIWndProc (RICHED20.9)
2066 LRESULT WINAPI RichEdit10ANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2068 LRESULT result;
2070 /* FIXME: this is NOT the same as 2.0 version */
2071 result = RichEditANSIWndProc(hWnd, msg, wParam, lParam);
2072 if (msg == WM_NCCREATE)
2074 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongW(hWnd, 0);
2076 editor->bEmulateVersion10 = TRUE;
2077 editor->pBuffer->pLast->member.para.nCharOfs = 2;
2079 return result;
2082 void ME_SendOldNotify(ME_TextEditor *editor, int nCode)
2084 HWND hWnd = editor->hWnd;
2085 SendMessageA(GetParent(hWnd), WM_COMMAND, (nCode<<16)|GetWindowLongW(hWnd, GWLP_ID), (LPARAM)hWnd);
2088 int ME_CountParagraphsBetween(ME_TextEditor *editor, int from, int to)
2090 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
2091 int i = 0;
2093 while(item && item->member.para.next_para->member.para.nCharOfs <= from)
2094 item = item->member.para.next_para;
2095 if (!item)
2096 return 0;
2097 while(item && item->member.para.next_para->member.para.nCharOfs <= to) {
2098 item = item->member.para.next_para;
2099 i++;
2101 return i;
2105 int ME_GetTextW(ME_TextEditor *editor, WCHAR *buffer, int nStart, int nChars, int bCRLF)
2107 ME_DisplayItem *item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
2108 int nWritten = 0;
2109 WCHAR *pStart = buffer;
2111 if (!item) {
2112 *buffer = L'\0';
2113 return 0;
2116 if (nStart)
2118 int nLen = ME_StrLen(item->member.run.strText) - nStart;
2119 if (nLen > nChars)
2120 nLen = nChars;
2121 CopyMemory(buffer, item->member.run.strText->szData + nStart, sizeof(WCHAR)*nLen);
2122 nChars -= nLen;
2123 nWritten += nLen;
2124 if (!nChars)
2125 return nWritten;
2126 buffer += nLen;
2127 nStart = 0;
2128 item = ME_FindItemFwd(item, diRun);
2131 while(nChars && item)
2133 int nLen = ME_StrLen(item->member.run.strText);
2134 if (nLen > nChars)
2135 nLen = nChars;
2137 if (item->member.run.nFlags & MERF_ENDPARA)
2139 *buffer = '\r';
2140 if (bCRLF)
2142 *(++buffer) = '\n';
2143 nWritten++;
2145 assert(nLen == 1);
2146 /* our end paragraph consists of 2 characters now */
2147 if (editor->bEmulateVersion10)
2148 nChars--;
2150 else
2151 CopyMemory(buffer, item->member.run.strText->szData, sizeof(WCHAR)*nLen);
2152 nChars -= nLen;
2153 nWritten += nLen;
2154 buffer += nLen;
2156 if (!nChars)
2158 TRACE("nWritten=%d, actual=%d\n", nWritten, buffer-pStart);
2159 *buffer = L'\0';
2160 return nWritten;
2162 item = ME_FindItemFwd(item, diRun);
2164 *buffer = L'\0';
2165 TRACE("nWritten=%d, actual=%d\n", nWritten, buffer-pStart);
2166 return nWritten;
2169 void ME_RegisterEditorClass(HINSTANCE hInstance)
2171 BOOL bResult;
2172 WNDCLASSW wcW;
2173 WNDCLASSA wcA;
2175 wcW.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
2176 wcW.lpfnWndProc = RichEditANSIWndProc;
2177 wcW.cbClsExtra = 0;
2178 wcW.cbWndExtra = 4;
2179 wcW.hInstance = NULL; /* hInstance would register DLL-local class */
2180 wcW.hIcon = NULL;
2181 wcW.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
2182 wcW.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
2183 wcW.lpszMenuName = NULL;
2184 wcW.lpszClassName = wszClassName;
2185 bResult = RegisterClassW(&wcW);
2186 assert(bResult);
2187 wcW.lpszClassName = wszClassName50;
2188 bResult = RegisterClassW(&wcW);
2189 assert(bResult);
2191 wcA.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
2192 wcA.lpfnWndProc = RichEditANSIWndProc;
2193 wcA.cbClsExtra = 0;
2194 wcA.cbWndExtra = 4;
2195 wcA.hInstance = NULL; /* hInstance would register DLL-local class */
2196 wcA.hIcon = NULL;
2197 wcA.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
2198 wcA.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
2199 wcA.lpszMenuName = NULL;
2200 wcA.lpszClassName = "RichEdit20A";
2201 bResult = RegisterClassA(&wcA);
2202 assert(bResult);
2203 wcA.lpszClassName = "RichEdit50A";
2204 bResult = RegisterClassA(&wcA);
2205 assert(bResult);
2208 LRESULT WINAPI REComboWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
2209 /* FIXME: Not implemented */
2210 TRACE("hWnd %p msg %04x (%s) %08x %08lx\n",
2211 hWnd, msg, get_msg_name(msg), wParam, lParam);
2212 return DefWindowProcW(hWnd, msg, wParam, lParam);
2215 LRESULT WINAPI REListWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
2216 /* FIXME: Not implemented */
2217 TRACE("hWnd %p msg %04x (%s) %08x %08lx\n",
2218 hWnd, msg, get_msg_name(msg), wParam, lParam);
2219 return DefWindowProcW(hWnd, msg, wParam, lParam);
2222 /******************************************************************
2223 * REExtendedRegisterClass (RICHED20.8)
2225 * FIXME undocumented
2226 * Need to check for errors and implement controls and callbacks
2228 LRESULT WINAPI REExtendedRegisterClass(void)
2230 WNDCLASSW wcW;
2231 UINT result;
2233 FIXME("semi stub\n");
2235 wcW.cbClsExtra = 0;
2236 wcW.cbWndExtra = 4;
2237 wcW.hInstance = NULL;
2238 wcW.hIcon = NULL;
2239 wcW.hCursor = NULL;
2240 wcW.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
2241 wcW.lpszMenuName = NULL;
2243 if (!ME_ListBoxRegistered)
2245 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS;
2246 wcW.lpfnWndProc = REListWndProc;
2247 wcW.lpszClassName = wszClassNameListBox;
2248 if (RegisterClassW(&wcW)) ME_ListBoxRegistered = TRUE;
2251 if (!ME_ComboBoxRegistered)
2253 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
2254 wcW.lpfnWndProc = REComboWndProc;
2255 wcW.lpszClassName = wszClassNameComboBox;
2256 if (RegisterClassW(&wcW)) ME_ComboBoxRegistered = TRUE;
2259 result = 0;
2260 if (ME_ListBoxRegistered)
2261 result += 1;
2262 if (ME_ComboBoxRegistered)
2263 result += 2;
2265 return result;