riched20: Reduced screen flicker by using proper update region invalidation.
[wine/wine-kai.git] / dlls / riched20 / editor.c
blob723b69b2386361c68a4e1e66e1b34ef8a00d0028
1 /*
2 * RichEdit - functions dealing with editor object
4 * Copyright 2004 by Krzysztof Foltman
5 * Copyright 2005 by Cihan Altinay
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 /*
23 API implementation status:
25 Messages (ANSI versions not done yet)
26 - EM_AUTOURLDETECT 2.0
27 + EM_CANPASTE
28 + EM_CANREDO 2.0
29 + EM_CANUNDO
30 + EM_CHARFROMPOS
31 - EM_DISPLAYBAND
32 + EM_EMPTYUNDOBUFFER
33 + EM_EXGETSEL
34 - EM_EXLIMITTEXT
35 + EM_EXLINEFROMCHAR
36 + EM_EXSETSEL
37 + EM_FINDTEXT (only FR_DOWN flag implemented)
38 + EM_FINDTEXTEX (only FR_DOWN flag implemented)
39 - EM_FINDWORDBREAK
40 - EM_FMTLINES
41 - EM_FORMATRANGE
42 - EM_GETAUTOURLDETECT 2.0
43 - EM_GETBIDIOPTIONS 3.0
44 - EM_GETCHARFORMAT (partly done)
45 - EM_GETEDITSTYLE
46 + EM_GETEVENTMASK
47 + EM_GETFIRSTVISIBLELINE (can be optimized if needed)
48 - EM_GETIMECOLOR 1.0asian
49 - EM_GETIMECOMPMODE 2.0
50 - EM_GETIMEOPTIONS 1.0asian
51 - EM_GETIMESTATUS
52 - EM_GETLANGOPTIONS 2.0
53 - EM_GETLIMITTEXT
54 - EM_GETLINE
55 + EM_GETLINECOUNT returns number of rows, not of paragraphs
56 + EM_GETMODIFY
57 - EM_GETOLEINTERFACE
58 - EM_GETOPTIONS
59 + EM_GETPARAFORMAT
60 - EM_GETPASSWORDCHAR 2.0
61 - EM_GETPUNCTUATION 1.0asian
62 + EM_GETRECT
63 - EM_GETREDONAME 2.0
64 + EM_GETSEL
65 + EM_GETSELTEXT (ANSI&Unicode)
66 - EM_GETSCROLLPOS 3.0
67 ! - EM_GETTHUMB
68 - EM_GETTEXTEX 2.0
69 + EM_GETTEXTLENGTHEX (GTL_PRECISE unimplemented)
70 - EM_GETTEXTMODE 2.0
71 ? + EM_GETTEXTRANGE (ANSI&Unicode)
72 - EM_GETTYPOGRAPHYOPTIONS 3.0
73 - EM_GETUNDONAME
74 - EM_GETWORDBREAKPROC
75 - EM_GETWORDBREAKPROCEX
76 - EM_GETWORDWRAPMODE 1.0asian
77 + EM_GETZOOM 3.0
78 - EM_HIDESELECTION
79 - EM_LIMITTEXT
80 + EM_LINEFROMCHAR
81 + EM_LINEINDEX
82 + EM_LINELENGTH
83 + EM_LINESCROLL
84 - EM_PASTESPECIAL
85 + EM_POSFROMCHAR
86 + EM_REDO 2.0
87 + EM_REQUESTRESIZE
88 + EM_REPLACESEL (proper style?) ANSI&Unicode
89 - EM_SCROLL
90 - EM_SCROLLCARET
91 - EM_SELECTIONTYPE
92 - EM_SETBIDIOPTIONS 3.0
93 + EM_SETBKGNDCOLOR
94 - EM_SETCHARFORMAT (partly done, no ANSI)
95 - EM_SETEDITSTYLE
96 + EM_SETEVENTMASK (few notifications supported)
97 - EM_SETFONTSIZE
98 - EM_SETIMECOLOR 1.0asian
99 - EM_SETIMEOPTIONS 1.0asian
100 - EM_SETLANGOPTIONS 2.0
101 - EM_SETLIMITTEXT
102 + EM_SETMODIFY (not sure if implementation is correct)
103 - EM_SETOLECALLBACK
104 - EM_SETOPTIONS
105 - EM_SETPALETTE 2.0
106 + EM_SETPARAFORMAT
107 - EM_SETPASSWORDCHAR 2.0
108 - EM_SETPUNCTUATION 1.0asian
109 + EM_SETREADONLY no beep on modification attempt
110 + EM_SETRECT
111 + EM_SETRECTNP (EM_SETRECT without repainting)
112 + EM_SETSEL
113 - EM_SETSCROLLPOS 3.0
114 - EM_SETTABSTOPS 3.0
115 - EM_SETTARGETDEVICE
116 + EM_SETTEXTEX 3.0 (unicode only, no rich text insertion handling, proper style?)
117 - EM_SETTEXTMODE 2.0
118 - EM_SETTYPOGRAPHYOPTIONS 3.0
119 - EM_SETUNDOLIMIT 2.0
120 - EM_SETWORDBREAKPROC
121 - EM_SETWORDBREAKPROCEX
122 - EM_SETWORDWRAPMODE 1.0asian
123 + EM_SETZOOM 3.0
124 - EM_SHOWSCROLLBAR 2.0
125 - EM_STOPGROUPTYPING 2.0
126 + EM_STREAMIN
127 + EM_STREAMOUT
128 + EM_UNDO
129 + WM_CHAR
130 + WM_CLEAR
131 + WM_COPY
132 + WM_CUT
133 + WM_GETDLGCODE (the current implementation is incomplete)
134 + WM_GETTEXT (ANSI&Unicode)
135 + WM_GETTEXTLENGTH (ANSI version sucks)
136 + WM_PASTE
137 - WM_SETFONT
138 + WM_SETTEXT (resets undo stack !) (proper style?) ANSI&Unicode
139 - WM_STYLECHANGING
140 - WM_STYLECHANGED (things like read-only flag)
141 - WM_UNICHAR
143 Notifications
145 * EN_CHANGE (sent from the wrong place)
146 - EN_CORRECTTEXT
147 - EN_DROPFILES
148 - EN_ERRSPACE
149 - EN_HSCROLL
150 - EN_IMECHANGE
151 + EN_KILLFOCUS
152 - EN_LINK
153 - EN_MAXTEXT
154 - EN_MSGFILTER
155 - EN_OLEOPFAILED
156 - EN_PROTECTED
157 + EN_REQUESTRESIZE
158 - EN_SAVECLIPBOARD
159 + EN_SELCHANGE
160 + EN_SETFOCUS
161 - EN_STOPNOUNDO
162 * EN_UPDATE (sent from the wrong place)
163 - EN_VSCROLL
165 Styles
167 - ES_AUTOHSCROLL
168 - ES_AUTOVSCROLL
169 - ES_CENTER
170 - ES_DISABLENOSCROLL (scrollbar is always visible)
171 - ES_EX_NOCALLOLEINIT
172 - ES_LEFT
173 - ES_MULTILINE (currently single line controls aren't supported)
174 - ES_NOIME
175 - ES_READONLY (I'm not sure if beeping is the proper behaviour)
176 - ES_RIGHT
177 - ES_SAVESEL
178 - ES_SELFIME
179 - ES_SUNKEN
180 - ES_VERTICAL
181 - ES_WANTRETURN (don't know how to do WM_GETDLGCODE part)
182 - WS_SETFONT
183 - WS_HSCROLL
184 - WS_VSCROLL
188 * RICHED20 TODO (incomplete):
190 * - messages/styles/notifications listed above
191 * - Undo coalescing
192 * - add remaining CHARFORMAT/PARAFORMAT fields
193 * - right/center align should strip spaces from the beginning
194 * - more advanced navigation (Ctrl-arrows)
195 * - tabs
196 * - pictures/OLE objects (not just smiling faces that lack API support ;-) )
197 * - COM interface (looks like a major pain in the TODO list)
198 * - calculate heights of pictures (half-done)
199 * - horizontal scrolling (not even started)
200 * - hysteresis during wrapping (related to scrollbars appearing/disappearing)
201 * - find/replace
202 * - how to implement EM_FORMATRANGE and EM_DISPLAYBAND ? (Mission Impossible)
203 * - italic caret with italic fonts
204 * - IME
205 * - most notifications aren't sent at all (the most important ones are)
206 * - when should EN_SELCHANGE be sent after text change ? (before/after EN_UPDATE?)
207 * - WM_SETTEXT may use wrong style (but I'm 80% sure it's OK)
208 * - EM_GETCHARFORMAT with SCF_SELECTION may not behave 100% like in original (but very close)
209 * - full justification
210 * - hyphenation
211 * - tables
212 * - ListBox & ComboBox not implemented
214 * Bugs that are probably fixed, but not so easy to verify:
215 * - EN_UPDATE/EN_CHANGE are handled very incorrectly (should be OK now)
216 * - undo for ME_JoinParagraphs doesn't store paragraph format ? (it does)
217 * - check/fix artificial EOL logic (bCursorAtEnd, hardly logical)
218 * - caret shouldn't be displayed when selection isn't empty
219 * - check refcounting in style management functions (looks perfect now, but no bugs is suspicious)
220 * - undo for setting default format (done, might be buggy)
221 * - styles might be not released properly (looks like they work like charm, but who knows?
225 #include "editor.h"
226 #include "commdlg.h"
227 #include "ole2.h"
228 #include "richole.h"
229 #include "winreg.h"
230 #define NO_SHLWAPI_STREAM
231 #include "shlwapi.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_Repaint(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 rtfBackColor:
354 fmt.dwMask = CFM_BACKCOLOR;
355 fmt.dwEffects = 0;
356 if (info->rtfParam == 0)
357 fmt.dwEffects = CFE_AUTOBACKCOLOR;
358 else if (info->rtfParam != rtfNoParam)
360 RTFColor *c = RTFGetColor(info, info->rtfParam);
361 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
363 break;
364 case rtfForeColor:
365 fmt.dwMask = CFM_COLOR;
366 fmt.dwEffects = 0;
367 if (info->rtfParam == 0)
368 fmt.dwEffects = CFE_AUTOCOLOR;
369 else if (info->rtfParam != rtfNoParam)
371 RTFColor *c = RTFGetColor(info, info->rtfParam);
372 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
374 break;
375 case rtfFontNum:
376 if (info->rtfParam != rtfNoParam)
378 RTFFont *f = RTFGetFont(info, info->rtfParam);
379 if (f)
381 MultiByteToWideChar(CP_ACP, 0, f->rtfFName, -1, fmt.szFaceName, sizeof(fmt.szFaceName)/sizeof(WCHAR));
382 fmt.szFaceName[sizeof(fmt.szFaceName)/sizeof(WCHAR)-1] = '\0';
383 fmt.bCharSet = f->rtfFCharSet;
384 fmt.dwMask = CFM_FACE | CFM_CHARSET;
387 break;
388 case rtfFontSize:
389 fmt.dwMask = CFM_SIZE;
390 if (info->rtfParam != rtfNoParam)
391 fmt.yHeight = info->rtfParam*10;
392 break;
394 if (fmt.dwMask) {
395 ME_Style *style2;
396 RTFFlushOutputBuffer(info);
397 /* FIXME too slow ? how come ? */
398 style2 = ME_ApplyStyle(info->style, &fmt);
399 ME_ReleaseStyle(info->style);
400 info->style = style2;
401 info->styleChanged = TRUE;
405 /* FIXME this function doesn't get any information about context of the RTF tag, which is very bad,
406 the same tags mean different things in different contexts */
407 static void ME_RTFParAttrHook(RTF_Info *info)
409 PARAFORMAT2 fmt;
410 fmt.cbSize = sizeof(fmt);
411 fmt.dwMask = 0;
413 switch(info->rtfMinor)
415 case rtfParDef: /* I'm not 100% sure what does it do, but I guess it restores default paragraph attributes */
416 fmt.dwMask = PFM_ALIGNMENT | PFM_TABSTOPS | PFM_OFFSET | PFM_STARTINDENT;
417 fmt.wAlignment = PFA_LEFT;
418 fmt.cTabCount = 0;
419 fmt.dxOffset = fmt.dxStartIndent = 0;
420 break;
421 case rtfFirstIndent:
422 ME_GetSelectionParaFormat(info->editor, &fmt);
423 fmt.dwMask = PFM_STARTINDENT | PFM_OFFSET;
424 fmt.dxStartIndent += info->rtfParam + fmt.dxOffset;
425 fmt.dxOffset = -info->rtfParam;
426 break;
427 case rtfLeftIndent:
428 ME_GetSelectionParaFormat(info->editor, &fmt);
429 fmt.dwMask = PFM_STARTINDENT;
430 fmt.dxStartIndent = -fmt.dxOffset + info->rtfParam;
431 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 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);
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))
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 GetClientRect(hWnd, &ed->rcFormat);
892 for (i=0; i<HFONT_CACHE_SIZE; i++)
894 ed->pFontCache[i].nRefs = 0;
895 ed->pFontCache[i].nAge = 0;
896 ed->pFontCache[i].hFont = NULL;
898 ME_CheckCharOffsets(ed);
899 return ed;
902 typedef struct tagME_GlobalDestStruct
904 HGLOBAL hData;
905 int nLength;
906 } ME_GlobalDestStruct;
908 static DWORD CALLBACK ME_AppendToHGLOBAL(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
910 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
911 int nMaxSize;
912 BYTE *pDest;
914 nMaxSize = GlobalSize(pData->hData);
915 if (pData->nLength+cb+1 >= cb)
917 /* round up to 2^17 */
918 int nNewSize = (((nMaxSize+cb+1)|0x1FFFF)+1) & 0xFFFE0000;
919 pData->hData = GlobalReAlloc(pData->hData, nNewSize, 0);
921 pDest = (BYTE *)GlobalLock(pData->hData);
922 memcpy(pDest + pData->nLength, lpBuff, cb);
923 pData->nLength += cb;
924 pDest[pData->nLength] = '\0';
925 GlobalUnlock(pData->hData);
926 *pcb = cb;
928 return 0;
931 static DWORD CALLBACK ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
933 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
934 int i;
935 WORD *pSrc, *pDest;
937 cb = cb >> 1;
938 pDest = (WORD *)lpBuff;
939 pSrc = (WORD *)GlobalLock(pData->hData);
940 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
941 pDest[i] = pSrc[pData->nLength+i];
943 pData->nLength += i;
944 *pcb = 2*i;
945 GlobalUnlock(pData->hData);
946 return 0;
949 static DWORD CALLBACK ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
951 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
952 int i;
953 BYTE *pSrc, *pDest;
955 pDest = lpBuff;
956 pSrc = (BYTE *)GlobalLock(pData->hData);
957 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
958 pDest[i] = pSrc[pData->nLength+i];
960 pData->nLength += i;
961 *pcb = i;
962 GlobalUnlock(pData->hData);
963 return 0;
967 void ME_DestroyEditor(ME_TextEditor *editor)
969 ME_DisplayItem *pFirst = editor->pBuffer->pFirst;
970 ME_DisplayItem *p = pFirst, *pNext = NULL;
971 int i;
973 ME_ClearTempStyle(editor);
974 ME_EmptyUndoStack(editor);
975 while(p) {
976 pNext = p->next;
977 ME_DestroyDisplayItem(p);
978 p = pNext;
980 ME_ReleaseStyle(editor->pBuffer->pDefaultStyle);
981 for (i=0; i<HFONT_CACHE_SIZE; i++)
983 if (editor->pFontCache[i].hFont)
984 DeleteObject(editor->pFontCache[i].hFont);
986 DeleteObject(editor->hbrBackground);
988 FREE_OBJ(editor);
991 static WCHAR wszClassName[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '2', '0', 'W', 0};
992 static WCHAR wszClassName50[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '5', '0', 'W', 0};
993 static WCHAR wszClassNameListBox[] = {'R','E','L','i','s','t','B','o','x','2','0','W', 0};
994 static WCHAR wszClassNameComboBox[] = {'R','E','C','o','m','b','o','B','o','x','2','0','W', 0};
996 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
998 TRACE("\n");
999 switch (fdwReason)
1001 case DLL_PROCESS_ATTACH:
1002 DisableThreadLibraryCalls(hinstDLL);
1003 me_heap = HeapCreate (0, 0x10000, 0);
1004 ME_RegisterEditorClass(hinstDLL);
1005 break;
1007 case DLL_PROCESS_DETACH:
1008 UnregisterClassW(wszClassName, 0);
1009 UnregisterClassW(wszClassName50, 0);
1010 UnregisterClassA("RichEdit20A", 0);
1011 UnregisterClassA("RichEdit50A", 0);
1012 if (ME_ListBoxRegistered)
1013 UnregisterClassW(wszClassNameListBox, 0);
1014 if (ME_ComboBoxRegistered)
1015 UnregisterClassW(wszClassNameComboBox, 0);
1016 HeapDestroy (me_heap);
1017 me_heap = NULL;
1018 break;
1020 return TRUE;
1024 #define UNSUPPORTED_MSG(e) \
1025 case e: \
1026 FIXME(#e ": stub\n"); \
1027 return DefWindowProcW(hWnd, msg, wParam, lParam);
1029 static const char * const edit_messages[] = {
1030 "EM_GETSEL",
1031 "EM_SETSEL",
1032 "EM_GETRECT",
1033 "EM_SETRECT",
1034 "EM_SETRECTNP",
1035 "EM_SCROLL",
1036 "EM_LINESCROLL",
1037 "EM_SCROLLCARET",
1038 "EM_GETMODIFY",
1039 "EM_SETMODIFY",
1040 "EM_GETLINECOUNT",
1041 "EM_LINEINDEX",
1042 "EM_SETHANDLE",
1043 "EM_GETHANDLE",
1044 "EM_GETTHUMB",
1045 "EM_UNKNOWN_BF",
1046 "EM_UNKNOWN_C0",
1047 "EM_LINELENGTH",
1048 "EM_REPLACESEL",
1049 "EM_UNKNOWN_C3",
1050 "EM_GETLINE",
1051 "EM_LIMITTEXT",
1052 "EM_CANUNDO",
1053 "EM_UNDO",
1054 "EM_FMTLINES",
1055 "EM_LINEFROMCHAR",
1056 "EM_UNKNOWN_CA",
1057 "EM_SETTABSTOPS",
1058 "EM_SETPASSWORDCHAR",
1059 "EM_EMPTYUNDOBUFFER",
1060 "EM_GETFIRSTVISIBLELINE",
1061 "EM_SETREADONLY",
1062 "EM_SETWORDBREAKPROC",
1063 "EM_GETWORDBREAKPROC",
1064 "EM_GETPASSWORDCHAR",
1065 "EM_SETMARGINS",
1066 "EM_GETMARGINS",
1067 "EM_GETLIMITTEXT",
1068 "EM_POSFROMCHAR",
1069 "EM_CHARFROMPOS"
1072 static const char * const richedit_messages[] = {
1073 "EM_CANPASTE",
1074 "EM_DISPLAYBAND",
1075 "EM_EXGETSEL",
1076 "EM_EXLIMITTEXT",
1077 "EM_EXLINEFROMCHAR",
1078 "EM_EXSETSEL",
1079 "EM_FINDTEXT",
1080 "EM_FORMATRANGE",
1081 "EM_GETCHARFORMAT",
1082 "EM_GETEVENTMASK",
1083 "EM_GETOLEINTERFACE",
1084 "EM_GETPARAFORMAT",
1085 "EM_GETSELTEXT",
1086 "EM_HIDESELECTION",
1087 "EM_PASTESPECIAL",
1088 "EM_REQUESTRESIZE",
1089 "EM_SELECTIONTYPE",
1090 "EM_SETBKGNDCOLOR",
1091 "EM_SETCHARFORMAT",
1092 "EM_SETEVENTMASK",
1093 "EM_SETOLECALLBACK",
1094 "EM_SETPARAFORMAT",
1095 "EM_SETTARGETDEVICE",
1096 "EM_STREAMIN",
1097 "EM_STREAMOUT",
1098 "EM_GETTEXTRANGE",
1099 "EM_FINDWORDBREAK",
1100 "EM_SETOPTIONS",
1101 "EM_GETOPTIONS",
1102 "EM_FINDTEXTEX",
1103 "EM_GETWORDBREAKPROCEX",
1104 "EM_SETWORDBREAKPROCEX",
1105 "EM_SETUNDOLIMIT",
1106 "EM_UNKNOWN_USER_83",
1107 "EM_REDO",
1108 "EM_CANREDO",
1109 "EM_GETUNDONAME",
1110 "EM_GETREDONAME",
1111 "EM_STOPGROUPTYPING",
1112 "EM_SETTEXTMODE",
1113 "EM_GETTEXTMODE",
1114 "EM_AUTOURLDETECT",
1115 "EM_GETAUTOURLDETECT",
1116 "EM_SETPALETTE",
1117 "EM_GETTEXTEX",
1118 "EM_GETTEXTLENGTHEX",
1119 "EM_SHOWSCROLLBAR",
1120 "EM_SETTEXTEX",
1121 "EM_UNKNOWN_USER_98",
1122 "EM_UNKNOWN_USER_99",
1123 "EM_SETPUNCTUATION",
1124 "EM_GETPUNCTUATION",
1125 "EM_SETWORDWRAPMODE",
1126 "EM_GETWORDWRAPMODE",
1127 "EM_SETIMECOLOR",
1128 "EM_GETIMECOLOR",
1129 "EM_SETIMEOPTIONS",
1130 "EM_GETIMEOPTIONS",
1131 "EM_CONVPOSITION",
1132 "EM_UNKNOWN_USER_109",
1133 "EM_UNKNOWN_USER_110",
1134 "EM_UNKNOWN_USER_111",
1135 "EM_UNKNOWN_USER_112",
1136 "EM_UNKNOWN_USER_113",
1137 "EM_UNKNOWN_USER_114",
1138 "EM_UNKNOWN_USER_115",
1139 "EM_UNKNOWN_USER_116",
1140 "EM_UNKNOWN_USER_117",
1141 "EM_UNKNOWN_USER_118",
1142 "EM_UNKNOWN_USER_119",
1143 "EM_SETLANGOPTIONS",
1144 "EM_GETLANGOPTIONS",
1145 "EM_GETIMECOMPMODE",
1146 "EM_FINDTEXTW",
1147 "EM_FINDTEXTEXW",
1148 "EM_RECONVERSION",
1149 "EM_SETIMEMODEBIAS",
1150 "EM_GETIMEMODEBIAS"
1153 static const char *
1154 get_msg_name(UINT msg)
1156 if (msg >= EM_GETSEL && msg <= EM_SETLIMITTEXT)
1157 return edit_messages[msg - EM_GETSEL];
1158 if (msg >= EM_CANPASTE && msg <= EM_GETIMEMODEBIAS)
1159 return richedit_messages[msg - EM_CANPASTE];
1160 return "";
1163 /******************************************************************
1164 * RichEditANSIWndProc (RICHED20.10)
1166 LRESULT WINAPI RichEditANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
1167 SCROLLINFO si;
1168 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongW(hWnd, 0);
1170 TRACE("hWnd %p msg %04x (%s) %08x %08lx\n",
1171 hWnd, msg, get_msg_name(msg), wParam, lParam);
1173 switch(msg) {
1175 UNSUPPORTED_MSG(EM_AUTOURLDETECT)
1176 UNSUPPORTED_MSG(EM_DISPLAYBAND)
1177 UNSUPPORTED_MSG(EM_EXLIMITTEXT)
1178 UNSUPPORTED_MSG(EM_FINDWORDBREAK)
1179 UNSUPPORTED_MSG(EM_FMTLINES)
1180 UNSUPPORTED_MSG(EM_FORMATRANGE)
1181 UNSUPPORTED_MSG(EM_GETAUTOURLDETECT)
1182 UNSUPPORTED_MSG(EM_GETBIDIOPTIONS)
1183 UNSUPPORTED_MSG(EM_GETEDITSTYLE)
1184 UNSUPPORTED_MSG(EM_GETIMECOMPMODE)
1185 /* UNSUPPORTED_MSG(EM_GETIMESTATUS) missing in Wine headers */
1186 UNSUPPORTED_MSG(EM_GETLANGOPTIONS)
1187 UNSUPPORTED_MSG(EM_GETLIMITTEXT)
1188 UNSUPPORTED_MSG(EM_GETLINE)
1189 /* UNSUPPORTED_MSG(EM_GETOLEINTERFACE) separate stub */
1190 UNSUPPORTED_MSG(EM_GETOPTIONS)
1191 UNSUPPORTED_MSG(EM_GETPASSWORDCHAR)
1192 UNSUPPORTED_MSG(EM_GETREDONAME)
1193 UNSUPPORTED_MSG(EM_GETSCROLLPOS)
1194 UNSUPPORTED_MSG(EM_GETTEXTMODE)
1195 UNSUPPORTED_MSG(EM_GETTYPOGRAPHYOPTIONS)
1196 UNSUPPORTED_MSG(EM_GETUNDONAME)
1197 UNSUPPORTED_MSG(EM_GETWORDBREAKPROC)
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_SETWORDBREAKPROC)
1220 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX)
1221 UNSUPPORTED_MSG(EM_SHOWSCROLLBAR)
1222 UNSUPPORTED_MSG(WM_SETFONT)
1223 UNSUPPORTED_MSG(WM_STYLECHANGING)
1224 UNSUPPORTED_MSG(WM_STYLECHANGED)
1225 /* UNSUPPORTED_MSG(WM_UNICHAR) FIXME missing in Wine headers */
1227 /* Messages specific to Richedit controls */
1229 case EM_STREAMIN:
1230 return ME_StreamIn(editor, wParam, (EDITSTREAM*)lParam);
1231 case EM_STREAMOUT:
1232 return ME_StreamOut(editor, wParam, (EDITSTREAM *)lParam);
1233 case WM_GETDLGCODE:
1235 UINT code = DLGC_WANTCHARS|DLGC_WANTARROWS;
1236 if (GetWindowLongW(hWnd, GWL_STYLE)&ES_WANTRETURN)
1237 code |= 0; /* FIXME what can we do here ? ask for messages and censor them ? */
1238 return code;
1240 case WM_NCCREATE:
1242 CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
1243 TRACE("WM_NCCREATE: style 0x%08lx\n", pcs->style);
1244 editor = ME_MakeEditor(hWnd);
1245 SetWindowLongW(hWnd, 0, (long)editor);
1246 pcs = 0; /* ignore */
1247 return TRUE;
1249 case EM_EMPTYUNDOBUFFER:
1250 ME_EmptyUndoStack(editor);
1251 return 0;
1252 case EM_GETSEL:
1254 /* Note: wParam/lParam can be NULL */
1255 UINT from, to;
1256 PUINT pfrom = wParam ? (PUINT)wParam : &from;
1257 PUINT pto = lParam ? (PUINT)lParam : &to;
1258 ME_GetSelection(editor, (int *)pfrom, (int *)pto);
1259 if ((*pfrom|*pto) & 0xFFFF0000)
1260 return -1;
1261 return MAKELONG(*pfrom,*pto);
1263 case EM_EXGETSEL:
1265 CHARRANGE *pRange = (CHARRANGE *)lParam;
1266 ME_GetSelection(editor, (int *)&pRange->cpMin, (int *)&pRange->cpMax);
1267 TRACE("EM_EXGETSEL = (%ld,%ld)\n", pRange->cpMin, pRange->cpMax);
1268 return 0;
1270 case EM_CANUNDO:
1271 return editor->pUndoStack != NULL;
1272 case EM_CANREDO:
1273 return editor->pRedoStack != NULL;
1274 case WM_UNDO: /* FIXME: actually not the same */
1275 case EM_UNDO:
1276 ME_Undo(editor);
1277 return 0;
1278 case EM_REDO:
1279 ME_Redo(editor);
1280 return 0;
1281 case EM_SETSEL:
1283 ME_SetSelection(editor, wParam, lParam);
1284 ME_Repaint(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_SetSelection(editor, pRange->cpMin, pRange->cpMax);
1293 /* FIXME optimize */
1294 ME_Repaint(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 ME_Repaint(editor);
1371 return 0;
1373 case EM_SETEVENTMASK:
1375 DWORD nOldMask = editor->nEventMask;
1377 editor->nEventMask = lParam;
1378 return nOldMask;
1380 case EM_GETEVENTMASK:
1381 return editor->nEventMask;
1382 case EM_SETCHARFORMAT:
1384 CHARFORMAT2W buf, *p;
1385 BOOL bRepaint = TRUE;
1386 p = ME_ToCF2W(&buf, (CHARFORMAT2W *)lParam);
1387 if (!wParam)
1388 ME_SetDefaultCharFormat(editor, p);
1389 else if (wParam == (SCF_WORD | SCF_SELECTION))
1390 FIXME("EM_SETCHARFORMAT: word selection not supported\n");
1391 else if (wParam == SCF_ALL)
1392 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), p);
1393 else {
1394 int from, to;
1395 ME_GetSelection(editor, &from, &to);
1396 bRepaint = (from != to);
1397 ME_SetSelectionCharFormat(editor, p);
1399 ME_CommitUndo(editor);
1400 if (bRepaint)
1401 ME_UpdateRepaint(editor);
1402 return 0;
1404 case EM_GETCHARFORMAT:
1406 CHARFORMAT2W tmp, *dst = (CHARFORMAT2W *)lParam;
1407 if (dst->cbSize != sizeof(CHARFORMATA) &&
1408 dst->cbSize != sizeof(CHARFORMATW) &&
1409 dst->cbSize != sizeof(CHARFORMAT2A) &&
1410 dst->cbSize != sizeof(CHARFORMAT2W))
1411 return 0;
1412 tmp.cbSize = sizeof(tmp);
1413 if (!wParam)
1414 ME_GetDefaultCharFormat(editor, &tmp);
1415 else
1416 ME_GetSelectionCharFormat(editor, &tmp);
1417 ME_CopyToCFAny(dst, &tmp);
1418 return tmp.dwMask;
1420 case EM_SETPARAFORMAT:
1421 ME_SetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
1422 ME_UpdateRepaint(editor);
1423 ME_CommitUndo(editor);
1424 return 0;
1425 case EM_GETPARAFORMAT:
1426 ME_GetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
1427 return 0;
1428 case EM_GETFIRSTVISIBLELINE:
1430 ME_DisplayItem *p = editor->pBuffer->pFirst;
1431 int y = editor->nScrollPosY;
1432 int ypara = 0;
1433 int count = 0;
1434 int ystart, yend;
1435 while(p) {
1436 p = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
1437 if (p->type == diTextEnd)
1438 break;
1439 if (p->type == diParagraph) {
1440 ypara = p->member.para.nYPos;
1441 continue;
1443 ystart = ypara + p->member.row.nYPos;
1444 yend = ystart + p->member.row.nHeight;
1445 if (y < yend) {
1446 break;
1448 count++;
1450 return count;
1452 case EM_LINESCROLL:
1454 int nPos = editor->nScrollPosY, nEnd= editor->nTotalLength - editor->sizeWindow.cy;
1455 nPos += 8 * lParam; /* FIXME follow the original */
1456 if (nPos>=nEnd)
1457 nPos = nEnd;
1458 if (nPos<0)
1459 nPos = 0;
1460 if (nPos != editor->nScrollPosY) {
1461 int dy = editor->nScrollPosY - nPos;
1462 editor->nScrollPosY = nPos;
1463 SetScrollPos(hWnd, SB_VERT, nPos, TRUE);
1464 if (editor->bRedraw)
1466 ScrollWindow(hWnd, 0, dy, NULL, NULL);
1467 UpdateWindow(hWnd);
1470 return TRUE; /* Should return false if a single line richedit control */
1472 case WM_CLEAR:
1474 int from, to;
1475 ME_GetSelection(editor, &from, &to);
1476 ME_InternalDeleteText(editor, from, to-from);
1477 ME_CommitUndo(editor);
1478 ME_UpdateRepaint(editor);
1479 return 0;
1481 case EM_REPLACESEL:
1483 int from, to;
1484 ME_Style *style;
1485 LPWSTR wszText = ME_ToUnicode(hWnd, (void *)lParam);
1486 size_t len = lstrlenW(wszText);
1487 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText));
1489 ME_GetSelection(editor, &from, &to);
1490 style = ME_GetSelectionInsertStyle(editor);
1491 ME_InternalDeleteText(editor, from, to-from);
1492 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
1493 ME_ReleaseStyle(style);
1494 ME_EndToUnicode(hWnd, wszText);
1495 /* drop temporary style if line end */
1496 /* FIXME question: does abc\n mean: put abc, clear temp style, put \n? (would require a change) */
1497 if (len>0 && wszText[len-1] == '\n')
1498 ME_ClearTempStyle(editor);
1500 ME_CommitUndo(editor);
1501 if (!wParam)
1502 ME_EmptyUndoStack(editor);
1503 ME_UpdateRepaint(editor);
1504 return 0;
1506 case WM_SETTEXT:
1508 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
1509 if (lParam)
1511 LPWSTR wszText = ME_ToUnicode(hWnd, (void *)lParam);
1512 TRACE("WM_SETTEXT lParam==%lx\n",lParam);
1513 TRACE("WM_SETTEXT - %s\n", debugstr_w(wszText)); /* debugstr_w() */
1514 if (lstrlenW(wszText) > 0)
1516 /* uses default style! */
1517 ME_InsertTextFromCursor(editor, 0, wszText, -1, editor->pBuffer->pDefaultStyle);
1519 ME_EndToUnicode(hWnd, wszText);
1521 else
1522 TRACE("WM_SETTEXT - NULL\n");
1523 ME_CommitUndo(editor);
1524 ME_EmptyUndoStack(editor);
1525 ME_SetSelection(editor, 0, 0);
1526 ME_UpdateRepaint(editor);
1527 return 0;
1529 case EM_CANPASTE:
1531 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
1532 if (IsClipboardFormatAvailable(nRTFFormat))
1533 return TRUE;
1534 if (IsClipboardFormatAvailable(CF_UNICODETEXT))
1535 return TRUE;
1536 return FALSE;
1538 case WM_PASTE:
1540 DWORD dwFormat = 0;
1541 EDITSTREAM es;
1542 ME_GlobalDestStruct gds;
1543 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
1544 UINT cf = 0;
1546 if (IsClipboardFormatAvailable(nRTFFormat))
1547 cf = nRTFFormat, dwFormat = SF_RTF;
1548 else if (IsClipboardFormatAvailable(CF_UNICODETEXT))
1549 cf = CF_UNICODETEXT, dwFormat = SF_TEXT|SF_UNICODE;
1550 else
1551 return 0;
1553 if (!OpenClipboard(hWnd))
1554 return 0;
1555 gds.hData = GetClipboardData(cf);
1556 gds.nLength = 0;
1557 es.dwCookie = (DWORD)&gds;
1558 es.pfnCallback = dwFormat == SF_RTF ? ME_ReadFromHGLOBALRTF : ME_ReadFromHGLOBALUnicode;
1559 SendMessageW(hWnd, EM_STREAMIN, dwFormat|SFF_SELECTION, (LPARAM)&es);
1561 CloseClipboard();
1562 return 0;
1564 case WM_CUT:
1565 case WM_COPY:
1567 int from, to, pars;
1568 WCHAR *data;
1569 HANDLE hData;
1570 EDITSTREAM es;
1571 ME_GlobalDestStruct gds;
1573 if (!OpenClipboard(hWnd))
1574 return 0;
1576 EmptyClipboard();
1577 ME_GetSelection(editor, &from, &to);
1578 pars = ME_CountParagraphsBetween(editor, from, to);
1579 hData = GlobalAlloc(GMEM_MOVEABLE, sizeof(WCHAR)*(to-from+pars+1));
1580 data = (WCHAR *)GlobalLock(hData);
1581 ME_GetTextW(editor, data, from, to-from, TRUE);
1582 GlobalUnlock(hData);
1584 gds.hData = GlobalAlloc(GMEM_MOVEABLE, 0);
1585 gds.nLength = 0;
1586 es.dwCookie = (DWORD)&gds;
1587 es.pfnCallback = ME_AppendToHGLOBAL;
1588 SendMessageW(hWnd, EM_STREAMOUT, SFF_SELECTION|SF_RTF, (LPARAM)&es);
1589 GlobalReAlloc(gds.hData, gds.nLength+1, 0);
1591 SetClipboardData(CF_UNICODETEXT, hData);
1592 SetClipboardData(RegisterClipboardFormatA("Rich Text Format"), gds.hData);
1594 CloseClipboard();
1595 if (msg == WM_CUT)
1597 ME_InternalDeleteText(editor, from, to-from);
1598 ME_CommitUndo(editor);
1599 ME_UpdateRepaint(editor);
1601 return 0;
1603 case WM_GETTEXTLENGTH:
1604 return ME_GetTextLength(editor);
1605 case EM_GETTEXTLENGTHEX:
1606 return ME_GetTextLengthEx(editor, (GETTEXTLENGTHEX *)wParam);
1607 case WM_GETTEXT:
1609 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
1610 tr.chrg.cpMin = 0;
1611 tr.chrg.cpMax = wParam-1;
1612 tr.lpstrText = (WCHAR *)lParam;
1613 return RichEditANSIWndProc(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
1615 case EM_GETTEXTEX:
1617 GETTEXTEX *ex = (GETTEXTEX*)wParam;
1618 int nStart, nCount;
1620 if (ex->flags & ~(GT_SELECTION | GT_USECRLF))
1621 FIXME("GETTEXTEX flags 0x%08lx not supported\n", ex->flags & ~(GT_SELECTION | GT_USECRLF));
1623 if (ex->flags & GT_SELECTION)
1625 ME_GetSelection(editor, &nStart, &nCount);
1626 nCount -= nStart;
1627 nCount = min(nCount, ex->cb - 1);
1629 else
1631 nStart = 0;
1632 nCount = ex->cb - 1;
1634 if (ex->codepage == 1200 || IsWindowUnicode(hWnd))
1636 nCount = min(nCount, ex->cb / sizeof(WCHAR) - 1);
1637 return ME_GetTextW(editor, (LPWSTR)lParam, nStart, nCount, ex->flags & GT_USECRLF);
1639 else
1641 /* potentially each char may be a CR, why calculate the exact value with O(N) when
1642 we can just take a bigger buffer? :) */
1643 int crlfmul = (ex->flags & GT_USECRLF) ? 2 : 1;
1644 LPWSTR buffer = HeapAlloc(GetProcessHeap(), 0, (crlfmul*nCount + 1) * sizeof(WCHAR));
1645 DWORD buflen = ex->cb;
1646 LRESULT rc;
1647 DWORD flags = 0;
1649 buflen = ME_GetTextW(editor, buffer, nStart, nCount, ex->flags & GT_USECRLF);
1650 rc = WideCharToMultiByte(ex->codepage, flags, buffer, buflen, (LPSTR)lParam, ex->cb, ex->lpDefaultChar, ex->lpUsedDefaultChar);
1652 HeapFree(GetProcessHeap(),0,buffer);
1653 return rc;
1656 case EM_GETSELTEXT:
1658 int from, to;
1659 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
1660 ME_GetSelection(editor, &from, &to);
1661 tr.chrg.cpMin = from;
1662 tr.chrg.cpMax = to;
1663 tr.lpstrText = (WCHAR *)lParam;
1664 return RichEditANSIWndProc(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
1666 case EM_GETTEXTRANGE:
1668 TEXTRANGEW *rng = (TEXTRANGEW *)lParam;
1669 TRACE("EM_GETTEXTRANGE min=%ld max=%ld unicode=%d emul1.0=%d length=%d\n",
1670 rng->chrg.cpMin, rng->chrg.cpMax, IsWindowUnicode(hWnd),
1671 editor->bEmulateVersion10, ME_GetTextLength(editor));
1672 if (IsWindowUnicode(hWnd))
1673 return ME_GetTextW(editor, rng->lpstrText, rng->chrg.cpMin, rng->chrg.cpMax-rng->chrg.cpMin, editor->bEmulateVersion10);
1674 else
1676 int nLen = rng->chrg.cpMax-rng->chrg.cpMin;
1677 WCHAR *p = ALLOC_N_OBJ(WCHAR, nLen+1);
1678 int nChars = ME_GetTextW(editor, p, rng->chrg.cpMin, nLen, editor->bEmulateVersion10);
1679 /* FIXME this is a potential security hole (buffer overrun)
1680 if you know more about wchar->mbyte conversion please explain
1682 WideCharToMultiByte(CP_ACP, 0, p, nChars+1, (char *)rng->lpstrText, nLen+1, NULL, NULL);
1683 FREE_OBJ(p);
1684 return nChars;
1687 case EM_GETLINECOUNT:
1689 ME_DisplayItem *item = editor->pBuffer->pFirst->next;
1690 int nRows = 0;
1692 while (item != editor->pBuffer->pLast)
1694 assert(item->type == diParagraph);
1695 nRows += item->member.para.nRows;
1696 item = item->member.para.next_para;
1698 TRACE("EM_GETLINECOUNT: nRows==%d\n", nRows);
1699 return max(1, nRows);
1701 case EM_LINEFROMCHAR:
1703 if (wParam == -1)
1704 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(editor, 1));
1705 else
1706 return ME_RowNumberFromCharOfs(editor, wParam);
1708 case EM_EXLINEFROMCHAR:
1710 return ME_RowNumberFromCharOfs(editor, lParam);
1712 case EM_LINEINDEX:
1714 ME_DisplayItem *item, *para;
1715 int nCharOfs;
1717 if (wParam == -1)
1718 item = ME_FindItemBack(editor->pCursors[0].pRun, diStartRow);
1719 else
1720 item = ME_FindRowWithNumber(editor, wParam);
1721 if (!item)
1722 return -1;
1723 para = ME_GetParagraph(item);
1724 item = ME_FindItemFwd(item, diRun);
1725 nCharOfs = para->member.para.nCharOfs + item->member.run.nCharOfs;
1726 TRACE("EM_LINEINDEX: nCharOfs==%d\n", nCharOfs);
1727 return nCharOfs;
1729 case EM_LINELENGTH:
1731 ME_DisplayItem *item, *item_end;
1732 int nChars = 0, nThisLineOfs = 0, nNextLineOfs = 0;
1734 if (wParam > ME_GetTextLength(editor))
1735 return 0;
1736 if (wParam == -1)
1738 FIXME("EM_LINELENGTH: returning number of unselected characters on lines with selection unsupported.\n");
1739 return 0;
1741 item = ME_FindItemAtOffset(editor, diRun, wParam, NULL);
1742 item = ME_RowStart(item);
1743 nThisLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item, diRun), 0);
1744 item_end = ME_FindItemFwd(item, diStartRow);
1745 if (item_end)
1746 nNextLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item_end, diRun), 0);
1747 else
1748 nNextLineOfs = ME_FindItemFwd(item, diParagraphOrEnd)->member.para.nCharOfs
1749 - (editor->bEmulateVersion10?2:1);
1750 nChars = nNextLineOfs - nThisLineOfs;
1751 TRACE("EM_LINELENGTH(%d)==%d\n",wParam, nChars);
1752 return nChars;
1754 case EM_FINDTEXT:
1756 FINDTEXTA *ft = (FINDTEXTA *)lParam;
1757 int nChars = MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, NULL, 0);
1758 WCHAR *tmp;
1760 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
1761 MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, tmp, nChars);
1762 return ME_FindText(editor, wParam, &ft->chrg, tmp, NULL);
1764 case EM_FINDTEXTEX:
1766 FINDTEXTEXA *ex = (FINDTEXTEXA *)lParam;
1767 int nChars = MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, NULL, 0);
1768 WCHAR *tmp;
1770 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
1771 MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, tmp, nChars);
1772 return ME_FindText(editor, wParam, &ex->chrg, tmp, &ex->chrgText);
1774 case EM_FINDTEXTW:
1776 FINDTEXTW *ft = (FINDTEXTW *)lParam;
1777 return ME_FindText(editor, wParam, &ft->chrg, ft->lpstrText, NULL);
1779 case EM_FINDTEXTEXW:
1781 FINDTEXTEXW *ex = (FINDTEXTEXW *)lParam;
1782 return ME_FindText(editor, wParam, &ex->chrg, ex->lpstrText, &ex->chrgText);
1784 case EM_GETZOOM:
1785 if (!wParam || !lParam)
1786 return FALSE;
1787 *(int *)wParam = editor->nZoomNumerator;
1788 *(int *)lParam = editor->nZoomDenominator;
1789 return TRUE;
1790 case EM_SETZOOM:
1791 return ME_SetZoom(editor, wParam, lParam);
1792 case EM_CHARFROMPOS:
1793 return ME_CharFromPos(editor, ((POINTL *)lParam)->x, ((POINTL *)lParam)->y);
1794 case EM_POSFROMCHAR:
1796 ME_DisplayItem *pRun;
1797 int nCharOfs, nOffset, nLength;
1798 POINTL pt = {0,0};
1800 nCharOfs = wParam;
1801 /* detect which API version we're dealing with */
1802 if (wParam >= 0x40000)
1803 nCharOfs = lParam;
1804 nLength = ME_GetTextLength(editor);
1806 if (nCharOfs < nLength) {
1807 ME_RunOfsFromCharOfs(editor, nCharOfs, &pRun, &nOffset);
1808 assert(pRun->type == diRun);
1809 pt.y = pRun->member.run.pt.y;
1810 pt.x = pRun->member.run.pt.x + ME_PointFromChar(editor, &pRun->member.run, nOffset);
1811 pt.y += ME_GetParagraph(pRun)->member.para.nYPos;
1812 } else {
1813 pt.x = 0;
1814 pt.y = editor->pBuffer->pLast->member.para.nYPos;
1816 if (wParam >= 0x40000) {
1817 *(POINTL *)wParam = pt;
1819 return MAKELONG( pt.x, pt.y );
1821 case WM_CREATE:
1822 ME_CommitUndo(editor);
1823 ME_WrapMarkedParagraphs(editor);
1824 ME_MoveCaret(editor);
1825 return 0;
1826 case WM_DESTROY:
1827 ME_DestroyEditor(editor);
1828 SetWindowLongW(hWnd, 0, 0);
1829 return 0;
1830 case WM_LBUTTONDOWN:
1831 SetFocus(hWnd);
1832 ME_LButtonDown(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
1833 SetCapture(hWnd);
1834 break;
1835 case WM_MOUSEMOVE:
1836 if (GetCapture() == hWnd)
1837 ME_MouseMove(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
1838 break;
1839 case WM_LBUTTONUP:
1840 if (GetCapture() == hWnd)
1841 ReleaseCapture();
1842 break;
1843 case WM_PAINT:
1844 if (editor->bRedraw)
1846 HDC hDC;
1847 PAINTSTRUCT ps;
1849 hDC = BeginPaint(hWnd, &ps);
1850 ME_PaintContent(editor, hDC, FALSE, &ps.rcPaint);
1851 EndPaint(hWnd, &ps);
1853 break;
1854 case WM_SETFOCUS:
1855 ME_ShowCaret(editor);
1856 ME_SendOldNotify(editor, EN_SETFOCUS);
1857 return 0;
1858 case WM_KILLFOCUS:
1859 ME_HideCaret(editor);
1860 ME_SendOldNotify(editor, EN_KILLFOCUS);
1861 return 0;
1862 case WM_ERASEBKGND:
1864 if (editor->bRedraw)
1866 HDC hDC = (HDC)wParam;
1867 RECT rc;
1868 if (GetUpdateRect(hWnd,&rc,TRUE))
1870 FillRect(hDC, &rc, editor->hbrBackground);
1873 return 1;
1875 case WM_COMMAND:
1876 TRACE("editor wnd command = %d\n", LOWORD(wParam));
1877 return 0;
1878 case WM_KEYDOWN:
1879 if (ME_KeyDown(editor, LOWORD(wParam)))
1880 return 0;
1881 goto do_default;
1882 case WM_CHAR:
1884 WCHAR wstr = LOWORD(wParam);
1886 switch (wstr)
1888 case 3: /* Ctrl-C */
1889 SendMessageW(editor->hWnd, WM_COPY, 0, 0);
1890 return 0;
1893 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY) {
1894 MessageBeep(MB_ICONERROR);
1895 return 0; /* FIXME really 0 ? */
1898 switch (wstr)
1900 case 1: /* Ctrl-A */
1901 ME_SetSelection(editor, 0, -1);
1902 return 0;
1903 case 22: /* Ctrl-V */
1904 SendMessageW(editor->hWnd, WM_PASTE, 0, 0);
1905 return 0;
1906 case 24: /* Ctrl-X */
1907 SendMessageW(editor->hWnd, WM_CUT, 0, 0);
1908 return 0;
1909 case 25: /* Ctrl-Y */
1910 SendMessageW(editor->hWnd, EM_REDO, 0, 0);
1911 return 0;
1912 case 26: /* Ctrl-Z */
1913 SendMessageW(editor->hWnd, EM_UNDO, 0, 0);
1914 return 0;
1916 if (((unsigned)wstr)>=' ' || wstr=='\r' || wstr=='\t') {
1917 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
1918 ME_Style *style = ME_GetInsertStyle(editor, 0);
1919 ME_SaveTempStyle(editor);
1920 ME_InsertTextFromCursor(editor, 0, &wstr, 1, style);
1921 ME_ReleaseStyle(style);
1922 ME_CommitUndo(editor);
1923 ME_UpdateRepaint(editor);
1925 return 0;
1927 case WM_VSCROLL:
1929 int nPos = editor->nScrollPosY;
1930 si.cbSize = sizeof(SCROLLINFO);
1931 si.fMask = SIF_PAGE|SIF_POS|SIF_RANGE|SIF_TRACKPOS;
1932 GetScrollInfo(hWnd, SB_VERT, &si);
1933 switch(LOWORD(wParam)) {
1934 case SB_LINEUP:
1935 nPos -= 24; /* FIXME follow the original */
1936 if (nPos<0) nPos = 0;
1937 break;
1938 case SB_LINEDOWN:
1940 int nEnd = editor->nTotalLength - editor->sizeWindow.cy;
1941 nPos += 24; /* FIXME follow the original */
1942 if (nPos>=nEnd) nPos = nEnd;
1943 break;
1945 case SB_PAGEUP:
1946 nPos -= editor->sizeWindow.cy;
1947 if (nPos<0) nPos = 0;
1948 break;
1949 case SB_PAGEDOWN:
1950 nPos += editor->sizeWindow.cy;
1951 if (nPos>=editor->nTotalLength) nPos = editor->nTotalLength-1;
1952 break;
1953 case SB_THUMBTRACK:
1954 case SB_THUMBPOSITION:
1955 nPos = si.nTrackPos;
1956 break;
1958 if (nPos != editor->nScrollPosY) {
1959 int dy = editor->nScrollPosY - nPos;
1960 editor->nScrollPosY = nPos;
1961 SetScrollPos(hWnd, SB_VERT, nPos, TRUE);
1962 if (editor->bRedraw)
1964 ScrollWindow(hWnd, 0, dy, NULL, NULL);
1965 UpdateWindow(hWnd);
1968 break;
1970 case WM_MOUSEWHEEL:
1972 int gcWheelDelta = 0, nPos = editor->nScrollPosY, nEnd = editor->nTotalLength - editor->sizeWindow.cy;
1973 UINT pulScrollLines;
1974 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
1975 gcWheelDelta -= GET_WHEEL_DELTA_WPARAM(wParam);
1976 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
1977 nPos += pulScrollLines * (gcWheelDelta / WHEEL_DELTA) * 8; /* FIXME follow the original */
1978 if (nPos>=nEnd)
1979 nPos = nEnd;
1980 if (nPos<0)
1981 nPos = 0;
1982 if (nPos != editor->nScrollPosY) {
1983 int dy = editor->nScrollPosY - nPos;
1984 editor->nScrollPosY = nPos;
1985 SetScrollPos(hWnd, SB_VERT, nPos, TRUE);
1986 if (editor->bRedraw)
1988 ScrollWindow(hWnd, 0, dy, NULL, NULL);
1989 UpdateWindow(hWnd);
1992 break;
1994 case EM_GETRECT:
1996 *((RECT *)lParam) = editor->rcFormat;
1997 return 0;
1999 case EM_SETRECT:
2000 case EM_SETRECTNP:
2002 if (lParam)
2004 RECT *rc = (RECT *)lParam;
2006 if (wParam)
2008 editor->rcFormat.left += rc->left;
2009 editor->rcFormat.top += rc->top;
2010 editor->rcFormat.right += rc->right;
2011 editor->rcFormat.bottom += rc->bottom;
2013 else
2015 editor->rcFormat = *rc;
2018 else
2020 GetClientRect(hWnd, &editor->rcFormat);
2022 if (msg != EM_SETRECTNP)
2023 ME_RewrapRepaint(editor);
2024 return 0;
2026 case EM_REQUESTRESIZE:
2027 ME_SendRequestResize(editor, TRUE);
2028 return 0;
2029 case WM_SETREDRAW:
2030 editor->bRedraw = wParam;
2031 return 0;
2032 case WM_SIZE:
2034 GetClientRect(hWnd, &editor->rcFormat);
2035 ME_RewrapRepaint(editor);
2036 return DefWindowProcW(hWnd, msg, wParam, lParam);
2038 case EM_GETOLEINTERFACE:
2040 LPVOID *ppvObj = (LPVOID*) lParam;
2041 FIXME("EM_GETOLEINTERFACE %p: stub\n", ppvObj);
2042 return CreateIRichEditOle(ppvObj);
2044 default:
2045 do_default:
2046 return DefWindowProcW(hWnd, msg, wParam, lParam);
2048 return 0L;
2052 /******************************************************************
2053 * RichEdit10ANSIWndProc (RICHED20.9)
2055 LRESULT WINAPI RichEdit10ANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2057 LRESULT result;
2059 /* FIXME: this is NOT the same as 2.0 version */
2060 result = RichEditANSIWndProc(hWnd, msg, wParam, lParam);
2061 if (msg == WM_NCCREATE)
2063 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongW(hWnd, 0);
2065 editor->bEmulateVersion10 = TRUE;
2066 editor->pBuffer->pLast->member.para.nCharOfs = 2;
2068 return result;
2071 void ME_SendOldNotify(ME_TextEditor *editor, int nCode)
2073 HWND hWnd = editor->hWnd;
2074 SendMessageA(GetParent(hWnd), WM_COMMAND, (nCode<<16)|GetWindowLongW(hWnd, GWLP_ID), (LPARAM)hWnd);
2077 int ME_CountParagraphsBetween(ME_TextEditor *editor, int from, int to)
2079 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
2080 int i = 0;
2082 while(item && item->member.para.next_para->member.para.nCharOfs <= from)
2083 item = item->member.para.next_para;
2084 if (!item)
2085 return 0;
2086 while(item && item->member.para.next_para->member.para.nCharOfs <= to) {
2087 item = item->member.para.next_para;
2088 i++;
2090 return i;
2094 int ME_GetTextW(ME_TextEditor *editor, WCHAR *buffer, int nStart, int nChars, int bCRLF)
2096 ME_DisplayItem *item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
2097 int nWritten = 0;
2098 WCHAR *pStart = buffer;
2100 if (!item) {
2101 *buffer = L'\0';
2102 return 0;
2105 if (nStart)
2107 int nLen = ME_StrLen(item->member.run.strText) - nStart;
2108 if (nLen > nChars)
2109 nLen = nChars;
2110 CopyMemory(buffer, item->member.run.strText->szData + nStart, sizeof(WCHAR)*nLen);
2111 nChars -= nLen;
2112 nWritten += nLen;
2113 if (!nChars)
2114 return nWritten;
2115 buffer += nLen;
2116 nStart = 0;
2117 item = ME_FindItemFwd(item, diRun);
2120 while(nChars && item)
2122 int nLen = ME_StrLen(item->member.run.strText);
2123 if (nLen > nChars)
2124 nLen = nChars;
2126 if (item->member.run.nFlags & MERF_ENDPARA)
2128 *buffer = '\r';
2129 if (bCRLF)
2131 *(++buffer) = '\n';
2132 nWritten++;
2134 assert(nLen == 1);
2135 /* our end paragraph consists of 2 characters now */
2136 if (editor->bEmulateVersion10)
2137 nChars--;
2139 else
2140 CopyMemory(buffer, item->member.run.strText->szData, sizeof(WCHAR)*nLen);
2141 nChars -= nLen;
2142 nWritten += nLen;
2143 buffer += nLen;
2145 if (!nChars)
2147 TRACE("nWritten=%d, actual=%d\n", nWritten, buffer-pStart);
2148 *buffer = L'\0';
2149 return nWritten;
2151 item = ME_FindItemFwd(item, diRun);
2153 *buffer = L'\0';
2154 TRACE("nWritten=%d, actual=%d\n", nWritten, buffer-pStart);
2155 return nWritten;
2158 void ME_RegisterEditorClass(HINSTANCE hInstance)
2160 BOOL bResult;
2161 WNDCLASSW wcW;
2162 WNDCLASSA wcA;
2164 wcW.style = CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
2165 wcW.lpfnWndProc = RichEditANSIWndProc;
2166 wcW.cbClsExtra = 0;
2167 wcW.cbWndExtra = 4;
2168 wcW.hInstance = NULL; /* hInstance would register DLL-local class */
2169 wcW.hIcon = NULL;
2170 wcW.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
2171 wcW.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
2172 wcW.lpszMenuName = NULL;
2173 wcW.lpszClassName = wszClassName;
2174 bResult = RegisterClassW(&wcW);
2175 assert(bResult);
2176 wcW.lpszClassName = wszClassName50;
2177 bResult = RegisterClassW(&wcW);
2178 assert(bResult);
2180 wcA.style = CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
2181 wcA.lpfnWndProc = RichEditANSIWndProc;
2182 wcA.cbClsExtra = 0;
2183 wcA.cbWndExtra = 4;
2184 wcA.hInstance = NULL; /* hInstance would register DLL-local class */
2185 wcA.hIcon = NULL;
2186 wcA.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
2187 wcA.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
2188 wcA.lpszMenuName = NULL;
2189 wcA.lpszClassName = "RichEdit20A";
2190 bResult = RegisterClassA(&wcA);
2191 assert(bResult);
2192 wcA.lpszClassName = "RichEdit50A";
2193 bResult = RegisterClassA(&wcA);
2194 assert(bResult);
2196 /******************************************************************
2197 * CreateTextServices (RICHED20.4)
2199 * FIXME should be ITextHost instead of void*
2201 HRESULT WINAPI CreateTextServices(IUnknown *punkOuter, void *pITextHost,
2202 IUnknown **ppUnk)
2204 FIXME("stub\n");
2205 /* FIXME should support aggregation */
2206 if (punkOuter)
2207 return CLASS_E_NOAGGREGATION;
2209 return E_FAIL; /* E_NOTIMPL isn't allowed by MSDN */
2212 LRESULT WINAPI REComboWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
2213 /* FIXME: Not implemented */
2214 TRACE("hWnd %p msg %04x (%s) %08x %08lx\n",
2215 hWnd, msg, get_msg_name(msg), wParam, lParam);
2216 return DefWindowProcW(hWnd, msg, wParam, lParam);
2219 LRESULT WINAPI REListWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
2220 /* FIXME: Not implemented */
2221 TRACE("hWnd %p msg %04x (%s) %08x %08lx\n",
2222 hWnd, msg, get_msg_name(msg), wParam, lParam);
2223 return DefWindowProcW(hWnd, msg, wParam, lParam);
2226 /******************************************************************
2227 * REExtendedRegisterClass (RICHED20.8)
2229 * FIXME undocumented
2230 * Need to check for errors and implement controls and callbacks
2232 LRESULT WINAPI REExtendedRegisterClass(void)
2234 WNDCLASSW wcW;
2235 UINT result;
2237 FIXME("semi stub\n");
2239 wcW.cbClsExtra = 0;
2240 wcW.cbWndExtra = 4;
2241 wcW.hInstance = NULL;
2242 wcW.hIcon = NULL;
2243 wcW.hCursor = NULL;
2244 wcW.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
2245 wcW.lpszMenuName = NULL;
2247 if (!ME_ListBoxRegistered)
2249 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS;
2250 wcW.lpfnWndProc = REListWndProc;
2251 wcW.lpszClassName = wszClassNameListBox;
2252 if (RegisterClassW(&wcW)) ME_ListBoxRegistered = TRUE;
2255 if (!ME_ComboBoxRegistered)
2257 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
2258 wcW.lpfnWndProc = REComboWndProc;
2259 wcW.lpszClassName = wszClassNameComboBox;
2260 if (RegisterClassW(&wcW)) ME_ComboBoxRegistered = TRUE;
2263 result = 0;
2264 if (ME_ListBoxRegistered)
2265 result += 1;
2266 if (ME_ComboBoxRegistered)
2267 result += 2;
2269 return result;