riched20: Don't use EM_STREAMOUT for copy/cut.
[wine/wine-kai.git] / dlls / riched20 / editor.c
blob3274a41ec7f2818a229cc1383007a3cad95b3ff8
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 "winreg.h"
227 #define NO_SHLWAPI_STREAM
228 #include "shlwapi.h"
229 #include "imm.h"
230 #include "textserv.h"
231 #include "rtf.h"
233 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
235 int me_debug = 0;
236 HANDLE me_heap = NULL;
238 static BOOL ME_ListBoxRegistered = FALSE;
239 static BOOL ME_ComboBoxRegistered = FALSE;
241 static ME_TextBuffer *ME_MakeText(void) {
243 ME_TextBuffer *buf = ALLOC_OBJ(ME_TextBuffer);
245 ME_DisplayItem *p1 = ME_MakeDI(diTextStart);
246 ME_DisplayItem *p2 = ME_MakeDI(diTextEnd);
248 p1->prev = NULL;
249 p1->next = p2;
250 p2->prev = p1;
251 p2->next = NULL;
252 p1->member.para.next_para = p2;
253 p2->member.para.prev_para = p1;
254 p2->member.para.nCharOfs = 0;
256 buf->pFirst = p1;
257 buf->pLast = p2;
258 buf->pCharStyle = NULL;
260 return buf;
264 static LRESULT ME_StreamInText(ME_TextEditor *editor, DWORD dwFormat, ME_InStream *stream, ME_Style *style)
266 WCHAR wszText[STREAMIN_BUFFER_SIZE+1];
267 WCHAR *pText;
269 TRACE("%08lx %p\n", dwFormat, stream);
271 do {
272 long nWideChars = 0;
274 if (!stream->dwSize)
276 ME_StreamInFill(stream);
277 if (stream->editstream->dwError)
278 break;
279 if (!stream->dwSize)
280 break;
283 if (!(dwFormat & SF_UNICODE))
285 /* FIXME? this is doomed to fail on true MBCS like UTF-8, luckily they're unlikely to be used as CP_ACP */
286 nWideChars = MultiByteToWideChar(CP_ACP, 0, stream->buffer, stream->dwSize, wszText, STREAMIN_BUFFER_SIZE);
287 pText = wszText;
289 else
291 nWideChars = stream->dwSize >> 1;
292 pText = (WCHAR *)stream->buffer;
295 ME_InsertTextFromCursor(editor, 0, pText, nWideChars, style);
296 if (stream->dwSize == 0)
297 break;
298 stream->dwSize = 0;
299 } while(1);
300 ME_CommitUndo(editor);
301 ME_UpdateRepaint(editor);
302 return 0;
305 static void ME_RTFCharAttrHook(RTF_Info *info)
307 CHARFORMAT2W fmt;
308 fmt.cbSize = sizeof(fmt);
309 fmt.dwMask = 0;
311 switch(info->rtfMinor)
313 case rtfPlain:
314 /* FIXME add more flags once they're implemented */
315 fmt.dwMask = CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_COLOR | CFM_BACKCOLOR | CFM_SIZE | CFM_WEIGHT;
316 fmt.dwEffects = CFE_AUTOCOLOR | CFE_AUTOBACKCOLOR;
317 fmt.yHeight = 12*20; /* 12pt */
318 fmt.wWeight = 400;
319 break;
320 case rtfBold:
321 fmt.dwMask = CFM_BOLD;
322 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
323 break;
324 case rtfItalic:
325 fmt.dwMask = CFM_ITALIC;
326 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
327 break;
328 case rtfUnderline:
329 fmt.dwMask = CFM_UNDERLINE;
330 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
331 fmt.bUnderlineType = CFU_CF1UNDERLINE;
332 break;
333 case rtfNoUnderline:
334 fmt.dwMask = CFM_UNDERLINE;
335 fmt.dwEffects = 0;
336 break;
337 case rtfStrikeThru:
338 fmt.dwMask = CFM_STRIKEOUT;
339 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
340 break;
341 case rtfSubScript:
342 case rtfSuperScript:
343 case rtfSubScrShrink:
344 case rtfSuperScrShrink:
345 case rtfNoSuperSub:
346 fmt.dwMask = CFM_SUBSCRIPT|CFM_SUPERSCRIPT;
347 if (info->rtfMinor == rtfSubScrShrink) fmt.dwEffects = CFE_SUBSCRIPT;
348 if (info->rtfMinor == rtfSuperScrShrink) fmt.dwEffects = CFE_SUPERSCRIPT;
349 if (info->rtfMinor == rtfNoSuperSub) fmt.dwEffects = 0;
350 break;
351 case rtfInvisible:
352 fmt.dwMask = CFM_HIDDEN;
353 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
354 break;
355 case rtfBackColor:
356 fmt.dwMask = CFM_BACKCOLOR;
357 fmt.dwEffects = 0;
358 if (info->rtfParam == 0)
359 fmt.dwEffects = CFE_AUTOBACKCOLOR;
360 else if (info->rtfParam != rtfNoParam)
362 RTFColor *c = RTFGetColor(info, info->rtfParam);
363 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
365 break;
366 case rtfForeColor:
367 fmt.dwMask = CFM_COLOR;
368 fmt.dwEffects = 0;
369 if (info->rtfParam == 0)
370 fmt.dwEffects = CFE_AUTOCOLOR;
371 else if (info->rtfParam != rtfNoParam)
373 RTFColor *c = RTFGetColor(info, info->rtfParam);
374 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
376 break;
377 case rtfFontNum:
378 if (info->rtfParam != rtfNoParam)
380 RTFFont *f = RTFGetFont(info, info->rtfParam);
381 if (f)
383 MultiByteToWideChar(CP_ACP, 0, f->rtfFName, -1, fmt.szFaceName, sizeof(fmt.szFaceName)/sizeof(WCHAR));
384 fmt.szFaceName[sizeof(fmt.szFaceName)/sizeof(WCHAR)-1] = '\0';
385 fmt.bCharSet = f->rtfFCharSet;
386 fmt.dwMask = CFM_FACE | CFM_CHARSET;
389 break;
390 case rtfFontSize:
391 fmt.dwMask = CFM_SIZE;
392 if (info->rtfParam != rtfNoParam)
393 fmt.yHeight = info->rtfParam*10;
394 break;
396 if (fmt.dwMask) {
397 ME_Style *style2;
398 RTFFlushOutputBuffer(info);
399 /* FIXME too slow ? how come ? */
400 style2 = ME_ApplyStyle(info->style, &fmt);
401 ME_ReleaseStyle(info->style);
402 info->style = style2;
403 info->styleChanged = TRUE;
407 /* FIXME this function doesn't get any information about context of the RTF tag, which is very bad,
408 the same tags mean different things in different contexts */
409 static void ME_RTFParAttrHook(RTF_Info *info)
411 PARAFORMAT2 fmt;
412 fmt.cbSize = sizeof(fmt);
413 fmt.dwMask = 0;
415 switch(info->rtfMinor)
417 case rtfParDef: /* restores default paragraph attributes */
418 fmt.dwMask = PFM_ALIGNMENT | PFM_TABSTOPS | PFM_OFFSET | PFM_STARTINDENT;
419 fmt.wAlignment = PFA_LEFT;
420 fmt.cTabCount = 0;
421 fmt.dxOffset = fmt.dxStartIndent = 0;
422 RTFFlushOutputBuffer(info);
423 ME_GetParagraph(info->editor->pCursors[0].pRun)->member.para.bTable = FALSE;
424 break;
425 case rtfInTable:
427 ME_DisplayItem *para;
429 RTFFlushOutputBuffer(info);
430 para = ME_GetParagraph(info->editor->pCursors[0].pRun);
431 assert(para->member.para.pCells);
432 para->member.para.bTable = TRUE;
433 return;
435 case rtfFirstIndent:
436 ME_GetSelectionParaFormat(info->editor, &fmt);
437 fmt.dwMask = PFM_STARTINDENT | PFM_OFFSET;
438 fmt.dxStartIndent += info->rtfParam + fmt.dxOffset;
439 fmt.dxOffset = -info->rtfParam;
440 break;
441 case rtfLeftIndent:
442 ME_GetSelectionParaFormat(info->editor, &fmt);
443 fmt.dwMask = PFM_STARTINDENT;
444 fmt.dxStartIndent = -fmt.dxOffset + info->rtfParam;
445 break;
446 case rtfRightIndent:
447 fmt.dwMask = PFM_RIGHTINDENT;
448 fmt.dxRightIndent = info->rtfParam;
449 break;
450 case rtfQuadLeft:
451 case rtfQuadJust:
452 fmt.dwMask = PFM_ALIGNMENT;
453 fmt.wAlignment = PFA_LEFT;
454 break;
455 case rtfQuadRight:
456 fmt.dwMask = PFM_ALIGNMENT;
457 fmt.wAlignment = PFA_RIGHT;
458 break;
459 case rtfQuadCenter:
460 fmt.dwMask = PFM_ALIGNMENT;
461 fmt.wAlignment = PFA_CENTER;
462 break;
463 case rtfTabPos:
464 ME_GetSelectionParaFormat(info->editor, &fmt);
465 if (!(fmt.dwMask & PFM_TABSTOPS))
467 fmt.dwMask |= PFM_TABSTOPS;
468 fmt.cTabCount = 0;
470 if (fmt.cTabCount < MAX_TAB_STOPS)
471 fmt.rgxTabs[fmt.cTabCount++] = info->rtfParam;
472 break;
474 if (fmt.dwMask) {
475 RTFFlushOutputBuffer(info);
476 /* FIXME too slow ? how come ?*/
477 ME_SetSelectionParaFormat(info->editor, &fmt);
481 static void ME_RTFTblAttrHook(RTF_Info *info)
483 ME_DisplayItem *para;
485 switch (info->rtfMinor)
487 case rtfRowDef:
488 RTFFlushOutputBuffer(info);
489 para = ME_GetParagraph(info->editor->pCursors[0].pRun);
491 para->member.para.pCells = ALLOC_OBJ(ME_TableCell);
492 para->member.para.pCells->nRightBoundary = 0;
493 para->member.para.pCells->next = NULL;
494 para->member.para.pLastCell = para->member.para.pCells;
495 break;
496 case rtfCellPos:
497 RTFFlushOutputBuffer(info);
498 para = ME_GetParagraph(info->editor->pCursors[0].pRun);
500 if (para->member.para.pLastCell->nRightBoundary)
502 ME_TableCell *pCell = ALLOC_OBJ(ME_TableCell);
504 pCell->next = NULL;
505 para->member.para.pLastCell->next = pCell;
506 para->member.para.pLastCell = pCell;
508 para->member.para.pLastCell->nRightBoundary = info->rtfParam;
509 break;
513 static void ME_RTFReadHook(RTF_Info *info) {
514 switch(info->rtfClass)
516 case rtfGroup:
517 switch(info->rtfMajor)
519 case rtfBeginGroup:
520 if (info->stackTop < maxStack) {
521 memcpy(&info->stack[info->stackTop].fmt, &info->style->fmt, sizeof(CHARFORMAT2W));
522 info->stack[info->stackTop].codePage = info->codePage;
523 info->stack[info->stackTop].unicodeLength = info->unicodeLength;
525 info->stackTop++;
526 info->styleChanged = FALSE;
527 break;
528 case rtfEndGroup:
530 ME_Style *s;
531 RTFFlushOutputBuffer(info);
532 if (info->stackTop<=1) {
533 info->rtfClass = rtfEOF;
534 return;
536 info->stackTop--;
537 assert(info->stackTop >= 0);
538 if (info->styleChanged)
540 /* FIXME too slow ? how come ? */
541 s = ME_ApplyStyle(info->style, &info->stack[info->stackTop].fmt);
542 ME_ReleaseStyle(info->style);
543 info->style = s;
544 info->codePage = info->stack[info->stackTop].codePage;
545 info->unicodeLength = info->stack[info->stackTop].unicodeLength;
547 break;
550 break;
551 case rtfControl:
552 switch(info->rtfMajor)
554 case rtfCharAttr:
555 ME_RTFCharAttrHook(info);
556 break;
557 case rtfParAttr:
558 ME_RTFParAttrHook(info);
559 break;
560 case rtfTblAttr:
561 ME_RTFTblAttrHook(info);
562 break;
563 case rtfSpecialChar:
564 if (info->rtfMinor == rtfCell)
566 RTFFlushOutputBuffer(info);
567 ME_InsertTableCellFromCursor(info->editor, 0);
570 break;
574 void
575 ME_StreamInFill(ME_InStream *stream)
577 stream->editstream->dwError = stream->editstream->pfnCallback(stream->editstream->dwCookie,
578 (BYTE *)stream->buffer,
579 sizeof(stream->buffer),
580 (LONG *)&stream->dwSize);
581 stream->dwUsed = 0;
584 static LRESULT ME_StreamIn(ME_TextEditor *editor, DWORD format, EDITSTREAM *stream)
586 RTF_Info parser;
587 ME_Style *style;
588 int from, to, to2, nUndoMode;
589 ME_UndoItem *pUI;
590 int nEventMask = editor->nEventMask;
591 ME_InStream inStream;
593 TRACE("stream==%p hWnd==%p format==0x%X\n", stream, editor->hWnd, (UINT)format);
594 editor->nEventMask = 0;
596 ME_GetSelection(editor, &from, &to);
597 if (format & SFF_SELECTION) {
598 style = ME_GetSelectionInsertStyle(editor);
600 ME_InternalDeleteText(editor, from, to-from);
602 else {
603 style = editor->pBuffer->pDefaultStyle;
604 ME_AddRefStyle(style);
605 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
606 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
607 from = to = 0;
608 ME_ClearTempStyle(editor);
609 /* FIXME restore default paragraph formatting ! */
612 nUndoMode = editor->nUndoMode;
613 editor->nUndoMode = umIgnore;
615 inStream.editstream = stream;
616 inStream.editstream->dwError = 0;
617 inStream.dwSize = 0;
618 inStream.dwUsed = 0;
620 if (format & SF_RTF)
622 /* Check if it's really RTF, and if it is not, use plain text */
623 ME_StreamInFill(&inStream);
624 if (!inStream.editstream->dwError)
626 if (strncmp(inStream.buffer, "{\\rtf1", 6) && strncmp(inStream.buffer, "{\\urtf", 6))
628 format &= ~SF_RTF;
629 format |= SF_TEXT;
634 if (!inStream.editstream->dwError)
636 if (format & SF_RTF) {
637 /* setup the RTF parser */
638 memset(&parser, 0, sizeof parser);
639 RTFSetEditStream(&parser, &inStream);
640 parser.rtfFormat = format&(SF_TEXT|SF_RTF);
641 parser.hwndEdit = editor->hWnd;
642 parser.editor = editor;
643 parser.style = style;
644 WriterInit(&parser);
645 RTFInit(&parser);
646 RTFSetReadHook(&parser, ME_RTFReadHook);
647 BeginFile(&parser);
649 /* do the parsing */
650 RTFRead(&parser);
651 RTFFlushOutputBuffer(&parser);
652 RTFDestroy(&parser);
654 style = parser.style;
656 else if (format & SF_TEXT)
657 ME_StreamInText(editor, format, &inStream, style);
658 else
659 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
660 ME_GetSelection(editor, &to, &to2);
661 /* put the cursor at the top */
662 if (!(format & SFF_SELECTION))
663 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
666 editor->nUndoMode = nUndoMode;
667 if (format & SFF_SELECTION)
669 pUI = ME_AddUndoItem(editor, diUndoDeleteRun, NULL);
670 TRACE("from %d to %d\n", from, to);
671 if (pUI && from < to)
673 pUI->nStart = from;
674 pUI->nLen = to-from;
676 ME_CommitUndo(editor);
678 else
680 ME_EmptyUndoStack(editor);
683 ME_ReleaseStyle(style);
684 editor->nEventMask = nEventMask;
685 if (editor->bRedraw)
687 ME_UpdateRepaint(editor);
689 if (!(format & SFF_SELECTION)) {
690 ME_ClearTempStyle(editor);
692 ME_MoveCaret(editor);
693 ME_SendSelChange(editor);
694 ME_SendRequestResize(editor, FALSE);
696 return 0;
700 ME_DisplayItem *
701 ME_FindItemAtOffset(ME_TextEditor *editor, ME_DIType nItemType, int nOffset, int *nItemOffset)
703 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
705 while (item && item->member.para.next_para->member.para.nCharOfs <= nOffset)
706 item = ME_FindItemFwd(item, diParagraph);
708 if (!item)
709 return item;
711 nOffset -= item->member.para.nCharOfs;
712 if (nItemType == diParagraph) {
713 if (nItemOffset)
714 *nItemOffset = nOffset;
715 return item;
718 do {
719 item = ME_FindItemFwd(item, diRun);
720 } while (item && (item->member.run.nCharOfs + ME_StrLen(item->member.run.strText) <= nOffset));
721 if (item) {
722 nOffset -= item->member.run.nCharOfs;
723 if (nItemOffset)
724 *nItemOffset = nOffset;
726 return item;
730 static int
731 ME_FindText(ME_TextEditor *editor, DWORD flags, CHARRANGE *chrg, WCHAR *text, CHARRANGE *chrgText)
733 int nStart, nEnd;
734 int nLen = lstrlenW(text);
735 int nMin, nMax;
736 ME_DisplayItem *item;
737 ME_DisplayItem *para;
739 TRACE("flags==0x%08lx, chrg->cpMin==%ld, chrg->cpMax==%ld text==%s\n",
740 flags, chrg->cpMin, chrg->cpMax, debugstr_w(text));
742 if (!(flags & FR_MATCHCASE))
743 FIXME("Case-insensitive search not implemented\n");
744 if (flags & ~(FR_DOWN | FR_MATCHCASE))
745 FIXME("Flags 0x%08lx not implemented\n", flags & ~(FR_DOWN | FR_MATCHCASE));
747 if (chrg->cpMax == -1)
749 nMin = chrg->cpMin;
750 nMax = ME_GetTextLength(editor);
752 else
754 nMin = min(chrg->cpMin, chrg->cpMax);
755 nMax = max(chrg->cpMin, chrg->cpMax);
758 if (!nLen)
760 if (chrgText)
761 chrgText->cpMin = chrgText->cpMax = ((flags & FR_DOWN) ? nMin : nMax);
762 return chrgText->cpMin;
765 if (flags & FR_DOWN) /* Forward search */
767 nStart = nMin;
768 item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
769 if (!item)
770 return -1;
772 para = ME_GetParagraph(item);
773 while (item
774 && para->member.para.nCharOfs + item->member.run.nCharOfs + nStart + nLen < nMax)
776 ME_DisplayItem *pCurItem = item;
777 int nCurStart = nStart;
778 int nMatched = 0;
780 while (pCurItem && pCurItem->member.run.strText->szData[nCurStart + nMatched] == text[nMatched])
782 nMatched++;
783 if (nMatched == nLen)
785 nStart += para->member.para.nCharOfs + item->member.run.nCharOfs;
786 if (chrgText)
788 chrgText->cpMin = nStart;
789 chrgText->cpMax = nStart + nLen;
791 TRACE("found at %d-%d\n", nStart, nStart + nLen);
792 return nStart;
794 if (nCurStart + nMatched == ME_StrLen(pCurItem->member.run.strText))
796 pCurItem = ME_FindItemFwd(pCurItem, diRun);
797 para = ME_GetParagraph(pCurItem);
798 nCurStart = -nMatched;
801 nStart++;
802 if (nStart == ME_StrLen(item->member.run.strText))
804 item = ME_FindItemFwd(item, diRun);
805 para = ME_GetParagraph(item);
806 nStart = 0;
810 else /* Backward search */
812 nEnd = nMax;
813 item = ME_FindItemAtOffset(editor, diRun, nEnd, &nEnd);
814 if (!item)
815 return -1;
817 para = ME_GetParagraph(item);
819 while (item
820 && para->member.para.nCharOfs + item->member.run.nCharOfs + nEnd - nLen >= nMin)
822 ME_DisplayItem *pCurItem = item;
823 int nCurEnd = nEnd;
824 int nMatched = 0;
826 while (pCurItem && pCurItem->member.run.strText->szData[nCurEnd - nMatched - 1] == text[nLen - nMatched - 1])
828 nMatched++;
829 if (nMatched == nLen)
831 nStart = para->member.para.nCharOfs + item->member.run.nCharOfs + nCurEnd - nMatched;
832 if (chrgText)
834 chrgText->cpMin = nStart;
835 chrgText->cpMax = nStart + nLen;
837 TRACE("found at %d-%d\n", nStart, nStart + nLen);
838 return nStart;
840 if (nCurEnd - nMatched == 0)
842 pCurItem = ME_FindItemBack(pCurItem, diRun);
843 para = ME_GetParagraph(pCurItem);
844 nCurEnd = ME_StrLen(pCurItem->member.run.strText) + nMatched;
847 nEnd--;
848 if (nEnd < 0)
850 item = ME_FindItemBack(item, diRun);
851 para = ME_GetParagraph(item);
852 nEnd = ME_StrLen(item->member.run.strText);
856 TRACE("not found\n");
857 return -1;
861 static BOOL
862 ME_KeyDown(ME_TextEditor *editor, WORD nKey)
864 BOOL ctrl_is_down = GetKeyState(VK_CONTROL) & 0x8000;
865 BOOL shift_is_down = GetKeyState(VK_SHIFT) & 0x8000;
867 switch (nKey)
869 case VK_LEFT:
870 case VK_RIGHT:
871 case VK_UP:
872 case VK_DOWN:
873 case VK_HOME:
874 case VK_END:
875 case VK_PRIOR:
876 case VK_NEXT:
877 ME_ArrowKey(editor, nKey, shift_is_down, ctrl_is_down);
878 return TRUE;
879 case VK_BACK:
880 case VK_DELETE:
881 /* FIXME backspace and delete aren't the same, they act different wrt paragraph style of the merged paragraph */
882 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY)
883 return FALSE;
884 if (ME_IsSelection(editor))
885 ME_DeleteSelection(editor);
886 else if (nKey == VK_DELETE || ME_ArrowKey(editor, VK_LEFT, FALSE, FALSE))
887 ME_DeleteTextAtCursor(editor, 1, 1);
888 else
889 return TRUE;
890 ME_QueueInvalidateFromCursor(editor, 1);
891 ME_UpdateRepaint(editor);
892 ME_SendRequestResize(editor, FALSE);
893 return TRUE;
895 default:
896 if (ctrl_is_down)
898 if (nKey == 'W')
900 CHARFORMAT2W chf;
901 char buf[2048];
903 ME_GetSelectionCharFormat(editor, &chf);
904 ME_DumpStyleToBuf(&chf, buf);
905 MessageBoxA(NULL, buf, "Style dump", MB_OK);
907 if (nKey == 'Q')
909 ME_CheckCharOffsets(editor);
913 return FALSE;
916 static void ME_ShowContextMenu(ME_TextEditor *editor, int x, int y)
918 CHARRANGE selrange;
919 HMENU menu;
920 int seltype = 0;
921 if(!editor->lpOleCallback)
922 return;
923 ME_GetSelection(editor, (int *)&selrange.cpMin, (int *)&selrange.cpMax);
924 if(selrange.cpMin == selrange.cpMax)
925 seltype |= SEL_EMPTY;
926 else
928 /* FIXME: Handle objects */
929 seltype |= SEL_TEXT;
930 if(selrange.cpMax-selrange.cpMin > 1)
931 seltype |= SEL_MULTICHAR;
933 if(SUCCEEDED(IRichEditOleCallback_GetContextMenu(editor->lpOleCallback, seltype, NULL, &selrange, &menu)))
935 TrackPopupMenu(menu, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, GetParent(editor->hWnd), NULL);
936 DestroyMenu(menu);
940 ME_TextEditor *ME_MakeEditor(HWND hWnd) {
941 ME_TextEditor *ed = ALLOC_OBJ(ME_TextEditor);
942 HDC hDC;
943 int i;
944 ed->hWnd = hWnd;
945 ed->bEmulateVersion10 = FALSE;
946 ed->pBuffer = ME_MakeText();
947 hDC = GetDC(hWnd);
948 ME_MakeFirstParagraph(hDC, ed->pBuffer);
949 ReleaseDC(hWnd, hDC);
950 ed->bCaretShown = FALSE;
951 ed->nCursors = 3;
952 ed->pCursors = ALLOC_N_OBJ(ME_Cursor, ed->nCursors);
953 ed->pCursors[0].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
954 ed->pCursors[0].nOffset = 0;
955 ed->pCursors[1].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
956 ed->pCursors[1].nOffset = 0;
957 ed->nLastTotalLength = ed->nTotalLength = 0;
958 ed->nUDArrowX = -1;
959 ed->nSequence = 0;
960 ed->rgbBackColor = -1;
961 ed->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
962 ed->bCaretAtEnd = FALSE;
963 ed->nEventMask = 0;
964 ed->nModifyStep = 0;
965 ed->pUndoStack = ed->pRedoStack = NULL;
966 ed->nUndoMode = umAddToUndo;
967 ed->nParagraphs = 1;
968 ed->nLastSelStart = ed->nLastSelEnd = 0;
969 ed->nScrollPosY = 0;
970 ed->nZoomNumerator = ed->nZoomDenominator = 0;
971 ed->bRedraw = TRUE;
972 ed->nInvalidOfs = -1;
973 ed->pfnWordBreak = NULL;
974 ed->lpOleCallback = NULL;
975 GetClientRect(hWnd, &ed->rcFormat);
976 for (i=0; i<HFONT_CACHE_SIZE; i++)
978 ed->pFontCache[i].nRefs = 0;
979 ed->pFontCache[i].nAge = 0;
980 ed->pFontCache[i].hFont = NULL;
982 ME_CheckCharOffsets(ed);
983 return ed;
986 typedef struct tagME_GlobalDestStruct
988 HGLOBAL hData;
989 int nLength;
990 } ME_GlobalDestStruct;
992 static DWORD CALLBACK ME_AppendToHGLOBAL(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
994 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
995 int nMaxSize;
996 BYTE *pDest;
998 nMaxSize = GlobalSize(pData->hData);
999 if (pData->nLength+cb+1 >= cb)
1001 /* round up to 2^17 */
1002 int nNewSize = (((nMaxSize+cb+1)|0x1FFFF)+1) & 0xFFFE0000;
1003 pData->hData = GlobalReAlloc(pData->hData, nNewSize, 0);
1005 pDest = (BYTE *)GlobalLock(pData->hData);
1006 memcpy(pDest + pData->nLength, lpBuff, cb);
1007 pData->nLength += cb;
1008 pDest[pData->nLength] = '\0';
1009 GlobalUnlock(pData->hData);
1010 *pcb = cb;
1012 return 0;
1015 static DWORD CALLBACK ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1017 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
1018 int i;
1019 WORD *pSrc, *pDest;
1021 cb = cb >> 1;
1022 pDest = (WORD *)lpBuff;
1023 pSrc = (WORD *)GlobalLock(pData->hData);
1024 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
1025 pDest[i] = pSrc[pData->nLength+i];
1027 pData->nLength += i;
1028 *pcb = 2*i;
1029 GlobalUnlock(pData->hData);
1030 return 0;
1033 static DWORD CALLBACK ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1035 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
1036 int i;
1037 BYTE *pSrc, *pDest;
1039 pDest = lpBuff;
1040 pSrc = (BYTE *)GlobalLock(pData->hData);
1041 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
1042 pDest[i] = pSrc[pData->nLength+i];
1044 pData->nLength += i;
1045 *pcb = i;
1046 GlobalUnlock(pData->hData);
1047 return 0;
1051 void ME_DestroyEditor(ME_TextEditor *editor)
1053 ME_DisplayItem *pFirst = editor->pBuffer->pFirst;
1054 ME_DisplayItem *p = pFirst, *pNext = NULL;
1055 int i;
1057 ME_ClearTempStyle(editor);
1058 ME_EmptyUndoStack(editor);
1059 while(p) {
1060 pNext = p->next;
1061 ME_DestroyDisplayItem(p);
1062 p = pNext;
1064 ME_ReleaseStyle(editor->pBuffer->pDefaultStyle);
1065 for (i=0; i<HFONT_CACHE_SIZE; i++)
1067 if (editor->pFontCache[i].hFont)
1068 DeleteObject(editor->pFontCache[i].hFont);
1070 DeleteObject(editor->hbrBackground);
1071 if(editor->lpOleCallback)
1072 IUnknown_Release(editor->lpOleCallback);
1074 FREE_OBJ(editor);
1077 static const WCHAR wszClassName[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '2', '0', 'W', 0};
1078 static const WCHAR wszClassName50[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '5', '0', 'W', 0};
1079 static const WCHAR wszClassNameListBox[] = {'R','E','L','i','s','t','B','o','x','2','0','W', 0};
1080 static const WCHAR wszClassNameComboBox[] = {'R','E','C','o','m','b','o','B','o','x','2','0','W', 0};
1082 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
1084 TRACE("\n");
1085 switch (fdwReason)
1087 case DLL_PROCESS_ATTACH:
1088 DisableThreadLibraryCalls(hinstDLL);
1089 me_heap = HeapCreate (0, 0x10000, 0);
1090 ME_RegisterEditorClass(hinstDLL);
1091 break;
1093 case DLL_PROCESS_DETACH:
1094 UnregisterClassW(wszClassName, 0);
1095 UnregisterClassW(wszClassName50, 0);
1096 UnregisterClassA("RichEdit20A", 0);
1097 UnregisterClassA("RichEdit50A", 0);
1098 if (ME_ListBoxRegistered)
1099 UnregisterClassW(wszClassNameListBox, 0);
1100 if (ME_ComboBoxRegistered)
1101 UnregisterClassW(wszClassNameComboBox, 0);
1102 HeapDestroy (me_heap);
1103 me_heap = NULL;
1104 break;
1106 return TRUE;
1110 #define UNSUPPORTED_MSG(e) \
1111 case e: \
1112 FIXME(#e ": stub\n"); \
1113 return DefWindowProcW(hWnd, msg, wParam, lParam);
1115 static const char * const edit_messages[] = {
1116 "EM_GETSEL",
1117 "EM_SETSEL",
1118 "EM_GETRECT",
1119 "EM_SETRECT",
1120 "EM_SETRECTNP",
1121 "EM_SCROLL",
1122 "EM_LINESCROLL",
1123 "EM_SCROLLCARET",
1124 "EM_GETMODIFY",
1125 "EM_SETMODIFY",
1126 "EM_GETLINECOUNT",
1127 "EM_LINEINDEX",
1128 "EM_SETHANDLE",
1129 "EM_GETHANDLE",
1130 "EM_GETTHUMB",
1131 "EM_UNKNOWN_BF",
1132 "EM_UNKNOWN_C0",
1133 "EM_LINELENGTH",
1134 "EM_REPLACESEL",
1135 "EM_UNKNOWN_C3",
1136 "EM_GETLINE",
1137 "EM_LIMITTEXT",
1138 "EM_CANUNDO",
1139 "EM_UNDO",
1140 "EM_FMTLINES",
1141 "EM_LINEFROMCHAR",
1142 "EM_UNKNOWN_CA",
1143 "EM_SETTABSTOPS",
1144 "EM_SETPASSWORDCHAR",
1145 "EM_EMPTYUNDOBUFFER",
1146 "EM_GETFIRSTVISIBLELINE",
1147 "EM_SETREADONLY",
1148 "EM_SETWORDBREAKPROC",
1149 "EM_GETWORDBREAKPROC",
1150 "EM_GETPASSWORDCHAR",
1151 "EM_SETMARGINS",
1152 "EM_GETMARGINS",
1153 "EM_GETLIMITTEXT",
1154 "EM_POSFROMCHAR",
1155 "EM_CHARFROMPOS"
1158 static const char * const richedit_messages[] = {
1159 "EM_CANPASTE",
1160 "EM_DISPLAYBAND",
1161 "EM_EXGETSEL",
1162 "EM_EXLIMITTEXT",
1163 "EM_EXLINEFROMCHAR",
1164 "EM_EXSETSEL",
1165 "EM_FINDTEXT",
1166 "EM_FORMATRANGE",
1167 "EM_GETCHARFORMAT",
1168 "EM_GETEVENTMASK",
1169 "EM_GETOLEINTERFACE",
1170 "EM_GETPARAFORMAT",
1171 "EM_GETSELTEXT",
1172 "EM_HIDESELECTION",
1173 "EM_PASTESPECIAL",
1174 "EM_REQUESTRESIZE",
1175 "EM_SELECTIONTYPE",
1176 "EM_SETBKGNDCOLOR",
1177 "EM_SETCHARFORMAT",
1178 "EM_SETEVENTMASK",
1179 "EM_SETOLECALLBACK",
1180 "EM_SETPARAFORMAT",
1181 "EM_SETTARGETDEVICE",
1182 "EM_STREAMIN",
1183 "EM_STREAMOUT",
1184 "EM_GETTEXTRANGE",
1185 "EM_FINDWORDBREAK",
1186 "EM_SETOPTIONS",
1187 "EM_GETOPTIONS",
1188 "EM_FINDTEXTEX",
1189 "EM_GETWORDBREAKPROCEX",
1190 "EM_SETWORDBREAKPROCEX",
1191 "EM_SETUNDOLIMIT",
1192 "EM_UNKNOWN_USER_83",
1193 "EM_REDO",
1194 "EM_CANREDO",
1195 "EM_GETUNDONAME",
1196 "EM_GETREDONAME",
1197 "EM_STOPGROUPTYPING",
1198 "EM_SETTEXTMODE",
1199 "EM_GETTEXTMODE",
1200 "EM_AUTOURLDETECT",
1201 "EM_GETAUTOURLDETECT",
1202 "EM_SETPALETTE",
1203 "EM_GETTEXTEX",
1204 "EM_GETTEXTLENGTHEX",
1205 "EM_SHOWSCROLLBAR",
1206 "EM_SETTEXTEX",
1207 "EM_UNKNOWN_USER_98",
1208 "EM_UNKNOWN_USER_99",
1209 "EM_SETPUNCTUATION",
1210 "EM_GETPUNCTUATION",
1211 "EM_SETWORDWRAPMODE",
1212 "EM_GETWORDWRAPMODE",
1213 "EM_SETIMECOLOR",
1214 "EM_GETIMECOLOR",
1215 "EM_SETIMEOPTIONS",
1216 "EM_GETIMEOPTIONS",
1217 "EM_CONVPOSITION",
1218 "EM_UNKNOWN_USER_109",
1219 "EM_UNKNOWN_USER_110",
1220 "EM_UNKNOWN_USER_111",
1221 "EM_UNKNOWN_USER_112",
1222 "EM_UNKNOWN_USER_113",
1223 "EM_UNKNOWN_USER_114",
1224 "EM_UNKNOWN_USER_115",
1225 "EM_UNKNOWN_USER_116",
1226 "EM_UNKNOWN_USER_117",
1227 "EM_UNKNOWN_USER_118",
1228 "EM_UNKNOWN_USER_119",
1229 "EM_SETLANGOPTIONS",
1230 "EM_GETLANGOPTIONS",
1231 "EM_GETIMECOMPMODE",
1232 "EM_FINDTEXTW",
1233 "EM_FINDTEXTEXW",
1234 "EM_RECONVERSION",
1235 "EM_SETIMEMODEBIAS",
1236 "EM_GETIMEMODEBIAS"
1239 static const char *
1240 get_msg_name(UINT msg)
1242 if (msg >= EM_GETSEL && msg <= EM_SETLIMITTEXT)
1243 return edit_messages[msg - EM_GETSEL];
1244 if (msg >= EM_CANPASTE && msg <= EM_GETIMEMODEBIAS)
1245 return richedit_messages[msg - EM_CANPASTE];
1246 return "";
1249 /******************************************************************
1250 * RichEditANSIWndProc (RICHED20.10)
1252 LRESULT WINAPI RichEditANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
1253 SCROLLINFO si;
1254 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongW(hWnd, 0);
1256 TRACE("hWnd %p msg %04x (%s) %08x %08lx\n",
1257 hWnd, msg, get_msg_name(msg), wParam, lParam);
1259 switch(msg) {
1261 UNSUPPORTED_MSG(EM_AUTOURLDETECT)
1262 UNSUPPORTED_MSG(EM_DISPLAYBAND)
1263 UNSUPPORTED_MSG(EM_EXLIMITTEXT)
1264 UNSUPPORTED_MSG(EM_FINDWORDBREAK)
1265 UNSUPPORTED_MSG(EM_FMTLINES)
1266 UNSUPPORTED_MSG(EM_FORMATRANGE)
1267 UNSUPPORTED_MSG(EM_GETAUTOURLDETECT)
1268 UNSUPPORTED_MSG(EM_GETBIDIOPTIONS)
1269 UNSUPPORTED_MSG(EM_GETEDITSTYLE)
1270 UNSUPPORTED_MSG(EM_GETIMECOMPMODE)
1271 /* UNSUPPORTED_MSG(EM_GETIMESTATUS) missing in Wine headers */
1272 UNSUPPORTED_MSG(EM_GETLANGOPTIONS)
1273 UNSUPPORTED_MSG(EM_GETLIMITTEXT)
1274 UNSUPPORTED_MSG(EM_GETLINE)
1275 /* UNSUPPORTED_MSG(EM_GETOLEINTERFACE) separate stub */
1276 UNSUPPORTED_MSG(EM_GETOPTIONS)
1277 UNSUPPORTED_MSG(EM_GETPASSWORDCHAR)
1278 UNSUPPORTED_MSG(EM_GETREDONAME)
1279 UNSUPPORTED_MSG(EM_GETSCROLLPOS)
1280 UNSUPPORTED_MSG(EM_GETTEXTMODE)
1281 UNSUPPORTED_MSG(EM_GETTYPOGRAPHYOPTIONS)
1282 UNSUPPORTED_MSG(EM_GETUNDONAME)
1283 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX)
1284 UNSUPPORTED_MSG(EM_HIDESELECTION)
1285 UNSUPPORTED_MSG(EM_LIMITTEXT) /* also known as EM_SETLIMITTEXT */
1286 UNSUPPORTED_MSG(EM_PASTESPECIAL)
1287 UNSUPPORTED_MSG(EM_SCROLL)
1288 UNSUPPORTED_MSG(EM_SCROLLCARET)
1289 UNSUPPORTED_MSG(EM_SELECTIONTYPE)
1290 UNSUPPORTED_MSG(EM_SETBIDIOPTIONS)
1291 UNSUPPORTED_MSG(EM_SETEDITSTYLE)
1292 UNSUPPORTED_MSG(EM_SETFONTSIZE)
1293 UNSUPPORTED_MSG(EM_SETLANGOPTIONS)
1294 UNSUPPORTED_MSG(EM_SETOPTIONS)
1295 UNSUPPORTED_MSG(EM_SETPALETTE)
1296 UNSUPPORTED_MSG(EM_SETPASSWORDCHAR)
1297 UNSUPPORTED_MSG(EM_SETSCROLLPOS)
1298 UNSUPPORTED_MSG(EM_SETTABSTOPS)
1299 UNSUPPORTED_MSG(EM_SETTARGETDEVICE)
1300 UNSUPPORTED_MSG(EM_SETTEXTMODE)
1301 UNSUPPORTED_MSG(EM_SETTYPOGRAPHYOPTIONS)
1302 UNSUPPORTED_MSG(EM_SETUNDOLIMIT)
1303 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX)
1304 UNSUPPORTED_MSG(EM_SHOWSCROLLBAR)
1305 UNSUPPORTED_MSG(WM_SETFONT)
1306 UNSUPPORTED_MSG(WM_STYLECHANGING)
1307 UNSUPPORTED_MSG(WM_STYLECHANGED)
1308 /* UNSUPPORTED_MSG(WM_UNICHAR) FIXME missing in Wine headers */
1310 /* Messages specific to Richedit controls */
1312 case EM_STREAMIN:
1313 return ME_StreamIn(editor, wParam, (EDITSTREAM*)lParam);
1314 case EM_STREAMOUT:
1315 return ME_StreamOut(editor, wParam, (EDITSTREAM *)lParam);
1316 case WM_GETDLGCODE:
1318 UINT code = DLGC_WANTCHARS|DLGC_WANTARROWS;
1319 if (GetWindowLongW(hWnd, GWL_STYLE)&ES_WANTRETURN)
1320 code |= 0; /* FIXME what can we do here ? ask for messages and censor them ? */
1321 return code;
1323 case WM_NCCREATE:
1325 CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
1326 TRACE("WM_NCCREATE: style 0x%08lx\n", pcs->style);
1327 editor = ME_MakeEditor(hWnd);
1328 SetWindowLongW(hWnd, 0, (long)editor);
1329 pcs = 0; /* ignore */
1330 return TRUE;
1332 case EM_EMPTYUNDOBUFFER:
1333 ME_EmptyUndoStack(editor);
1334 return 0;
1335 case EM_GETSEL:
1337 /* Note: wParam/lParam can be NULL */
1338 UINT from, to;
1339 PUINT pfrom = wParam ? (PUINT)wParam : &from;
1340 PUINT pto = lParam ? (PUINT)lParam : &to;
1341 ME_GetSelection(editor, (int *)pfrom, (int *)pto);
1342 if ((*pfrom|*pto) & 0xFFFF0000)
1343 return -1;
1344 return MAKELONG(*pfrom,*pto);
1346 case EM_EXGETSEL:
1348 CHARRANGE *pRange = (CHARRANGE *)lParam;
1349 ME_GetSelection(editor, (int *)&pRange->cpMin, (int *)&pRange->cpMax);
1350 TRACE("EM_EXGETSEL = (%ld,%ld)\n", pRange->cpMin, pRange->cpMax);
1351 return 0;
1353 case EM_CANUNDO:
1354 return editor->pUndoStack != NULL;
1355 case EM_CANREDO:
1356 return editor->pRedoStack != NULL;
1357 case WM_UNDO: /* FIXME: actually not the same */
1358 case EM_UNDO:
1359 ME_Undo(editor);
1360 return 0;
1361 case EM_REDO:
1362 ME_Redo(editor);
1363 return 0;
1364 case EM_SETSEL:
1366 ME_InvalidateSelection(editor);
1367 ME_SetSelection(editor, wParam, lParam);
1368 ME_InvalidateSelection(editor);
1369 ME_SendSelChange(editor);
1370 return 0;
1372 case EM_EXSETSEL:
1374 CHARRANGE *pRange = (CHARRANGE *)lParam;
1375 TRACE("EM_EXSETSEL (%ld,%ld)\n", pRange->cpMin, pRange->cpMax);
1376 ME_InvalidateSelection(editor);
1377 ME_SetSelection(editor, pRange->cpMin, pRange->cpMax);
1378 ME_InvalidateSelection(editor);
1379 ME_SendSelChange(editor);
1380 return 0;
1382 case EM_SETTEXTEX:
1384 LPWSTR wszText = (LPWSTR)lParam;
1385 SETTEXTEX *pStruct = (SETTEXTEX *)wParam;
1386 size_t len = lstrlenW(wszText);
1387 int from, to;
1388 ME_Style *style;
1389 TRACE("EM_SETTEXEX - %s, flags %d, cp %d\n", debugstr_w(wszText), (int)pStruct->flags, pStruct->codepage);
1390 if (pStruct->codepage != 1200) {
1391 FIXME("EM_SETTEXTEX only supports unicode right now!\n");
1392 return 0;
1394 /* FIXME: this should support RTF strings too, according to MSDN */
1395 if (pStruct->flags & ST_SELECTION) {
1396 ME_GetSelection(editor, &from, &to);
1397 style = ME_GetSelectionInsertStyle(editor);
1398 ME_InternalDeleteText(editor, from, to - from);
1399 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
1400 ME_ReleaseStyle(style);
1402 else {
1403 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
1404 ME_InsertTextFromCursor(editor, 0, wszText, -1, editor->pBuffer->pDefaultStyle);
1405 len = 1;
1407 ME_CommitUndo(editor);
1408 if (!(pStruct->flags & ST_KEEPUNDO))
1409 ME_EmptyUndoStack(editor);
1410 ME_UpdateRepaint(editor);
1411 return len;
1413 case EM_SETBKGNDCOLOR:
1415 LRESULT lColor = ME_GetBackColor(editor);
1416 if (editor->rgbBackColor != -1)
1417 DeleteObject(editor->hbrBackground);
1418 if (wParam)
1420 editor->rgbBackColor = -1;
1421 editor->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
1423 else
1425 editor->rgbBackColor = lParam;
1426 editor->hbrBackground = CreateSolidBrush(editor->rgbBackColor);
1428 if (editor->bRedraw)
1430 InvalidateRect(hWnd, NULL, TRUE);
1431 UpdateWindow(hWnd);
1433 return lColor;
1435 case EM_GETMODIFY:
1436 return editor->nModifyStep == 0 ? 0 : 1;
1437 case EM_SETMODIFY:
1439 if (wParam)
1440 editor->nModifyStep = 0x80000000;
1441 else
1442 editor->nModifyStep = 0;
1444 return 0;
1446 case EM_SETREADONLY:
1448 long nStyle = GetWindowLongW(hWnd, GWL_STYLE);
1449 if (wParam)
1450 nStyle |= ES_READONLY;
1451 else
1452 nStyle &= ~ES_READONLY;
1453 SetWindowLongW(hWnd, GWL_STYLE, nStyle);
1454 return 0;
1456 case EM_SETEVENTMASK:
1458 DWORD nOldMask = editor->nEventMask;
1460 editor->nEventMask = lParam;
1461 return nOldMask;
1463 case EM_GETEVENTMASK:
1464 return editor->nEventMask;
1465 case EM_SETCHARFORMAT:
1467 CHARFORMAT2W buf, *p;
1468 BOOL bRepaint = TRUE;
1469 p = ME_ToCF2W(&buf, (CHARFORMAT2W *)lParam);
1470 if (!wParam)
1471 ME_SetDefaultCharFormat(editor, p);
1472 else if (wParam == (SCF_WORD | SCF_SELECTION))
1473 FIXME("EM_SETCHARFORMAT: word selection not supported\n");
1474 else if (wParam == SCF_ALL)
1475 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), p);
1476 else {
1477 int from, to;
1478 ME_GetSelection(editor, &from, &to);
1479 bRepaint = (from != to);
1480 ME_SetSelectionCharFormat(editor, p);
1482 ME_CommitUndo(editor);
1483 if (bRepaint)
1484 ME_UpdateRepaint(editor);
1485 return 0;
1487 case EM_GETCHARFORMAT:
1489 CHARFORMAT2W tmp, *dst = (CHARFORMAT2W *)lParam;
1490 if (dst->cbSize != sizeof(CHARFORMATA) &&
1491 dst->cbSize != sizeof(CHARFORMATW) &&
1492 dst->cbSize != sizeof(CHARFORMAT2A) &&
1493 dst->cbSize != sizeof(CHARFORMAT2W))
1494 return 0;
1495 tmp.cbSize = sizeof(tmp);
1496 if (!wParam)
1497 ME_GetDefaultCharFormat(editor, &tmp);
1498 else
1499 ME_GetSelectionCharFormat(editor, &tmp);
1500 ME_CopyToCFAny(dst, &tmp);
1501 return tmp.dwMask;
1503 case EM_SETPARAFORMAT:
1504 ME_SetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
1505 ME_UpdateRepaint(editor);
1506 ME_CommitUndo(editor);
1507 return 0;
1508 case EM_GETPARAFORMAT:
1509 ME_GetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
1510 return 0;
1511 case EM_GETFIRSTVISIBLELINE:
1513 ME_DisplayItem *p = editor->pBuffer->pFirst;
1514 int y = editor->nScrollPosY;
1515 int ypara = 0;
1516 int count = 0;
1517 int ystart, yend;
1518 while(p) {
1519 p = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
1520 if (p->type == diTextEnd)
1521 break;
1522 if (p->type == diParagraph) {
1523 ypara = p->member.para.nYPos;
1524 continue;
1526 ystart = ypara + p->member.row.nYPos;
1527 yend = ystart + p->member.row.nHeight;
1528 if (y < yend) {
1529 break;
1531 count++;
1533 return count;
1535 case EM_LINESCROLL:
1537 int nPos = editor->nScrollPosY, nEnd= editor->nTotalLength - editor->sizeWindow.cy;
1538 nPos += 8 * lParam; /* FIXME follow the original */
1539 if (nPos>=nEnd)
1540 nPos = nEnd;
1541 if (nPos<0)
1542 nPos = 0;
1543 if (nPos != editor->nScrollPosY) {
1544 int dy = editor->nScrollPosY - nPos;
1545 editor->nScrollPosY = nPos;
1546 SetScrollPos(hWnd, SB_VERT, nPos, TRUE);
1547 if (editor->bRedraw)
1549 ScrollWindow(hWnd, 0, dy, NULL, NULL);
1550 UpdateWindow(hWnd);
1553 return TRUE; /* Should return false if a single line richedit control */
1555 case WM_CLEAR:
1557 int from, to;
1558 ME_GetSelection(editor, &from, &to);
1559 ME_InternalDeleteText(editor, from, to-from);
1560 ME_CommitUndo(editor);
1561 ME_UpdateRepaint(editor);
1562 return 0;
1564 case EM_REPLACESEL:
1566 int from, to;
1567 ME_Style *style;
1568 LPWSTR wszText = ME_ToUnicode(hWnd, (void *)lParam);
1569 size_t len = lstrlenW(wszText);
1570 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText));
1572 ME_GetSelection(editor, &from, &to);
1573 style = ME_GetSelectionInsertStyle(editor);
1574 ME_InternalDeleteText(editor, from, to-from);
1575 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
1576 ME_ReleaseStyle(style);
1577 ME_EndToUnicode(hWnd, wszText);
1578 /* drop temporary style if line end */
1579 /* FIXME question: does abc\n mean: put abc, clear temp style, put \n? (would require a change) */
1580 if (len>0 && wszText[len-1] == '\n')
1581 ME_ClearTempStyle(editor);
1583 ME_CommitUndo(editor);
1584 if (!wParam)
1585 ME_EmptyUndoStack(editor);
1586 ME_UpdateRepaint(editor);
1587 return 0;
1589 case WM_SETTEXT:
1591 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
1592 if (lParam)
1594 LPWSTR wszText = ME_ToUnicode(hWnd, (void *)lParam);
1595 TRACE("WM_SETTEXT lParam==%lx\n",lParam);
1596 TRACE("WM_SETTEXT - %s\n", debugstr_w(wszText)); /* debugstr_w() */
1597 if (lstrlenW(wszText) > 0)
1599 /* uses default style! */
1600 ME_InsertTextFromCursor(editor, 0, wszText, -1, editor->pBuffer->pDefaultStyle);
1602 ME_EndToUnicode(hWnd, wszText);
1604 else
1605 TRACE("WM_SETTEXT - NULL\n");
1606 ME_CommitUndo(editor);
1607 ME_EmptyUndoStack(editor);
1608 ME_SetSelection(editor, 0, 0);
1609 ME_UpdateRepaint(editor);
1610 return 0;
1612 case EM_CANPASTE:
1614 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
1615 if (IsClipboardFormatAvailable(nRTFFormat))
1616 return TRUE;
1617 if (IsClipboardFormatAvailable(CF_UNICODETEXT))
1618 return TRUE;
1619 return FALSE;
1621 case WM_PASTE:
1623 DWORD dwFormat = 0;
1624 EDITSTREAM es;
1625 ME_GlobalDestStruct gds;
1626 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
1627 UINT cf = 0;
1629 if (IsClipboardFormatAvailable(nRTFFormat))
1630 cf = nRTFFormat, dwFormat = SF_RTF;
1631 else if (IsClipboardFormatAvailable(CF_UNICODETEXT))
1632 cf = CF_UNICODETEXT, dwFormat = SF_TEXT|SF_UNICODE;
1633 else
1634 return 0;
1636 if (!OpenClipboard(hWnd))
1637 return 0;
1638 gds.hData = GetClipboardData(cf);
1639 gds.nLength = 0;
1640 es.dwCookie = (DWORD)&gds;
1641 es.pfnCallback = dwFormat == SF_RTF ? ME_ReadFromHGLOBALRTF : ME_ReadFromHGLOBALUnicode;
1642 SendMessageW(hWnd, EM_STREAMIN, dwFormat|SFF_SELECTION, (LPARAM)&es);
1644 CloseClipboard();
1645 return 0;
1647 case WM_CUT:
1648 case WM_COPY:
1650 int from, to, pars;
1651 WCHAR *data;
1652 HANDLE hData;
1653 EDITSTREAM es;
1654 ME_GlobalDestStruct gds;
1656 if (!OpenClipboard(hWnd))
1657 return 0;
1659 EmptyClipboard();
1660 ME_GetSelection(editor, &from, &to);
1661 pars = ME_CountParagraphsBetween(editor, from, to);
1662 hData = GlobalAlloc(GMEM_MOVEABLE, sizeof(WCHAR)*(to-from+pars+1));
1663 data = (WCHAR *)GlobalLock(hData);
1664 ME_GetTextW(editor, data, from, to-from, TRUE);
1665 GlobalUnlock(hData);
1667 gds.hData = GlobalAlloc(GMEM_MOVEABLE, 0);
1668 gds.nLength = 0;
1669 es.dwCookie = (DWORD)&gds;
1670 es.pfnCallback = ME_AppendToHGLOBAL;
1671 ME_StreamOutRange(editor, SF_RTF, from, to, &es);
1672 GlobalReAlloc(gds.hData, gds.nLength+1, 0);
1674 SetClipboardData(CF_UNICODETEXT, hData);
1675 SetClipboardData(RegisterClipboardFormatA("Rich Text Format"), gds.hData);
1677 CloseClipboard();
1678 if (msg == WM_CUT)
1680 ME_InternalDeleteText(editor, from, to-from);
1681 ME_CommitUndo(editor);
1682 ME_UpdateRepaint(editor);
1684 return 0;
1686 case WM_GETTEXTLENGTH:
1687 return ME_GetTextLength(editor);
1688 case EM_GETTEXTLENGTHEX:
1689 return ME_GetTextLengthEx(editor, (GETTEXTLENGTHEX *)wParam);
1690 case WM_GETTEXT:
1692 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
1693 tr.chrg.cpMin = 0;
1694 tr.chrg.cpMax = wParam-1;
1695 tr.lpstrText = (WCHAR *)lParam;
1696 return RichEditANSIWndProc(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
1698 case EM_GETTEXTEX:
1700 GETTEXTEX *ex = (GETTEXTEX*)wParam;
1701 int nStart, nCount;
1703 if (ex->flags & ~(GT_SELECTION | GT_USECRLF))
1704 FIXME("GETTEXTEX flags 0x%08lx not supported\n", ex->flags & ~(GT_SELECTION | GT_USECRLF));
1706 if (ex->flags & GT_SELECTION)
1708 ME_GetSelection(editor, &nStart, &nCount);
1709 nCount -= nStart;
1710 nCount = min(nCount, ex->cb - 1);
1712 else
1714 nStart = 0;
1715 nCount = ex->cb - 1;
1717 if (ex->codepage == 1200 || IsWindowUnicode(hWnd))
1719 nCount = min(nCount, ex->cb / sizeof(WCHAR) - 1);
1720 return ME_GetTextW(editor, (LPWSTR)lParam, nStart, nCount, ex->flags & GT_USECRLF);
1722 else
1724 /* potentially each char may be a CR, why calculate the exact value with O(N) when
1725 we can just take a bigger buffer? :) */
1726 int crlfmul = (ex->flags & GT_USECRLF) ? 2 : 1;
1727 LPWSTR buffer = HeapAlloc(GetProcessHeap(), 0, (crlfmul*nCount + 1) * sizeof(WCHAR));
1728 DWORD buflen = ex->cb;
1729 LRESULT rc;
1730 DWORD flags = 0;
1732 buflen = ME_GetTextW(editor, buffer, nStart, nCount, ex->flags & GT_USECRLF);
1733 rc = WideCharToMultiByte(ex->codepage, flags, buffer, buflen, (LPSTR)lParam, ex->cb, ex->lpDefaultChar, ex->lpUsedDefaultChar);
1735 HeapFree(GetProcessHeap(),0,buffer);
1736 return rc;
1739 case EM_GETSELTEXT:
1741 int from, to;
1742 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
1743 ME_GetSelection(editor, &from, &to);
1744 tr.chrg.cpMin = from;
1745 tr.chrg.cpMax = to;
1746 tr.lpstrText = (WCHAR *)lParam;
1747 return RichEditANSIWndProc(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
1749 case EM_GETTEXTRANGE:
1751 TEXTRANGEW *rng = (TEXTRANGEW *)lParam;
1752 TRACE("EM_GETTEXTRANGE min=%ld max=%ld unicode=%d emul1.0=%d length=%d\n",
1753 rng->chrg.cpMin, rng->chrg.cpMax, IsWindowUnicode(hWnd),
1754 editor->bEmulateVersion10, ME_GetTextLength(editor));
1755 if (IsWindowUnicode(hWnd))
1756 return ME_GetTextW(editor, rng->lpstrText, rng->chrg.cpMin, rng->chrg.cpMax-rng->chrg.cpMin, editor->bEmulateVersion10);
1757 else
1759 int nLen = rng->chrg.cpMax-rng->chrg.cpMin;
1760 WCHAR *p = ALLOC_N_OBJ(WCHAR, nLen+1);
1761 int nChars = ME_GetTextW(editor, p, rng->chrg.cpMin, nLen, editor->bEmulateVersion10);
1762 /* FIXME this is a potential security hole (buffer overrun)
1763 if you know more about wchar->mbyte conversion please explain
1765 WideCharToMultiByte(CP_ACP, 0, p, nChars+1, (char *)rng->lpstrText, nLen+1, NULL, NULL);
1766 FREE_OBJ(p);
1767 return nChars;
1770 case EM_GETLINECOUNT:
1772 ME_DisplayItem *item = editor->pBuffer->pFirst->next;
1773 int nRows = 0;
1775 while (item != editor->pBuffer->pLast)
1777 assert(item->type == diParagraph);
1778 nRows += item->member.para.nRows;
1779 item = item->member.para.next_para;
1781 TRACE("EM_GETLINECOUNT: nRows==%d\n", nRows);
1782 return max(1, nRows);
1784 case EM_LINEFROMCHAR:
1786 if (wParam == -1)
1787 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(editor, 1));
1788 else
1789 return ME_RowNumberFromCharOfs(editor, wParam);
1791 case EM_EXLINEFROMCHAR:
1793 return ME_RowNumberFromCharOfs(editor, lParam);
1795 case EM_LINEINDEX:
1797 ME_DisplayItem *item, *para;
1798 int nCharOfs;
1800 if (wParam == -1)
1801 item = ME_FindItemBack(editor->pCursors[0].pRun, diStartRow);
1802 else
1803 item = ME_FindRowWithNumber(editor, wParam);
1804 if (!item)
1805 return -1;
1806 para = ME_GetParagraph(item);
1807 item = ME_FindItemFwd(item, diRun);
1808 nCharOfs = para->member.para.nCharOfs + item->member.run.nCharOfs;
1809 TRACE("EM_LINEINDEX: nCharOfs==%d\n", nCharOfs);
1810 return nCharOfs;
1812 case EM_LINELENGTH:
1814 ME_DisplayItem *item, *item_end;
1815 int nChars = 0, nThisLineOfs = 0, nNextLineOfs = 0;
1817 if (wParam > ME_GetTextLength(editor))
1818 return 0;
1819 if (wParam == -1)
1821 FIXME("EM_LINELENGTH: returning number of unselected characters on lines with selection unsupported.\n");
1822 return 0;
1824 item = ME_FindItemAtOffset(editor, diRun, wParam, NULL);
1825 item = ME_RowStart(item);
1826 nThisLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item, diRun), 0);
1827 item_end = ME_FindItemFwd(item, diStartRow);
1828 if (item_end)
1829 nNextLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item_end, diRun), 0);
1830 else
1831 nNextLineOfs = ME_FindItemFwd(item, diParagraphOrEnd)->member.para.nCharOfs
1832 - (editor->bEmulateVersion10?2:1);
1833 nChars = nNextLineOfs - nThisLineOfs;
1834 TRACE("EM_LINELENGTH(%d)==%d\n",wParam, nChars);
1835 return nChars;
1837 case EM_FINDTEXT:
1839 FINDTEXTA *ft = (FINDTEXTA *)lParam;
1840 int nChars = MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, NULL, 0);
1841 WCHAR *tmp;
1843 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
1844 MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, tmp, nChars);
1845 return ME_FindText(editor, wParam, &ft->chrg, tmp, NULL);
1847 case EM_FINDTEXTEX:
1849 FINDTEXTEXA *ex = (FINDTEXTEXA *)lParam;
1850 int nChars = MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, NULL, 0);
1851 WCHAR *tmp;
1853 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
1854 MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, tmp, nChars);
1855 return ME_FindText(editor, wParam, &ex->chrg, tmp, &ex->chrgText);
1857 case EM_FINDTEXTW:
1859 FINDTEXTW *ft = (FINDTEXTW *)lParam;
1860 return ME_FindText(editor, wParam, &ft->chrg, ft->lpstrText, NULL);
1862 case EM_FINDTEXTEXW:
1864 FINDTEXTEXW *ex = (FINDTEXTEXW *)lParam;
1865 return ME_FindText(editor, wParam, &ex->chrg, ex->lpstrText, &ex->chrgText);
1867 case EM_GETZOOM:
1868 if (!wParam || !lParam)
1869 return FALSE;
1870 *(int *)wParam = editor->nZoomNumerator;
1871 *(int *)lParam = editor->nZoomDenominator;
1872 return TRUE;
1873 case EM_SETZOOM:
1874 return ME_SetZoom(editor, wParam, lParam);
1875 case EM_CHARFROMPOS:
1876 return ME_CharFromPos(editor, ((POINTL *)lParam)->x, ((POINTL *)lParam)->y);
1877 case EM_POSFROMCHAR:
1879 ME_DisplayItem *pRun;
1880 int nCharOfs, nOffset, nLength;
1881 POINTL pt = {0,0};
1883 nCharOfs = wParam;
1884 /* detect which API version we're dealing with */
1885 if (wParam >= 0x40000)
1886 nCharOfs = lParam;
1887 nLength = ME_GetTextLength(editor);
1889 if (nCharOfs < nLength) {
1890 ME_RunOfsFromCharOfs(editor, nCharOfs, &pRun, &nOffset);
1891 assert(pRun->type == diRun);
1892 pt.y = pRun->member.run.pt.y;
1893 pt.x = pRun->member.run.pt.x + ME_PointFromChar(editor, &pRun->member.run, nOffset);
1894 pt.y += ME_GetParagraph(pRun)->member.para.nYPos;
1895 } else {
1896 pt.x = 0;
1897 pt.y = editor->pBuffer->pLast->member.para.nYPos;
1899 if (wParam >= 0x40000) {
1900 *(POINTL *)wParam = pt;
1902 return MAKELONG( pt.x, pt.y );
1904 case WM_CREATE:
1905 ME_CommitUndo(editor);
1906 ME_WrapMarkedParagraphs(editor);
1907 ME_MoveCaret(editor);
1908 return 0;
1909 case WM_DESTROY:
1910 ME_DestroyEditor(editor);
1911 SetWindowLongW(hWnd, 0, 0);
1912 return 0;
1913 case WM_LBUTTONDOWN:
1914 SetFocus(hWnd);
1915 ME_LButtonDown(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
1916 SetCapture(hWnd);
1917 break;
1918 case WM_MOUSEMOVE:
1919 if (GetCapture() == hWnd)
1920 ME_MouseMove(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
1921 break;
1922 case WM_LBUTTONUP:
1923 if (GetCapture() == hWnd)
1924 ReleaseCapture();
1925 break;
1926 case WM_LBUTTONDBLCLK:
1927 ME_SelectWord(editor);
1928 break;
1929 case WM_CONTEXTMENU:
1930 ME_ShowContextMenu(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
1931 break;
1932 case WM_PAINT:
1933 if (editor->bRedraw)
1935 HDC hDC;
1936 PAINTSTRUCT ps;
1938 hDC = BeginPaint(hWnd, &ps);
1939 ME_PaintContent(editor, hDC, FALSE, &ps.rcPaint);
1940 EndPaint(hWnd, &ps);
1942 break;
1943 case WM_SETFOCUS:
1944 ME_ShowCaret(editor);
1945 ME_SendOldNotify(editor, EN_SETFOCUS);
1946 return 0;
1947 case WM_KILLFOCUS:
1948 ME_HideCaret(editor);
1949 ME_SendOldNotify(editor, EN_KILLFOCUS);
1950 return 0;
1951 case WM_ERASEBKGND:
1953 if (editor->bRedraw)
1955 HDC hDC = (HDC)wParam;
1956 RECT rc;
1957 if (GetUpdateRect(hWnd,&rc,TRUE))
1959 FillRect(hDC, &rc, editor->hbrBackground);
1962 return 1;
1964 case WM_COMMAND:
1965 TRACE("editor wnd command = %d\n", LOWORD(wParam));
1966 return 0;
1967 case WM_KEYDOWN:
1968 if (ME_KeyDown(editor, LOWORD(wParam)))
1969 return 0;
1970 goto do_default;
1971 case WM_CHAR:
1973 WCHAR wstr = LOWORD(wParam);
1975 switch (wstr)
1977 case 3: /* Ctrl-C */
1978 SendMessageW(editor->hWnd, WM_COPY, 0, 0);
1979 return 0;
1982 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY) {
1983 MessageBeep(MB_ICONERROR);
1984 return 0; /* FIXME really 0 ? */
1987 switch (wstr)
1989 case 1: /* Ctrl-A */
1990 ME_SetSelection(editor, 0, -1);
1991 return 0;
1992 case 22: /* Ctrl-V */
1993 SendMessageW(editor->hWnd, WM_PASTE, 0, 0);
1994 return 0;
1995 case 24: /* Ctrl-X */
1996 SendMessageW(editor->hWnd, WM_CUT, 0, 0);
1997 return 0;
1998 case 25: /* Ctrl-Y */
1999 SendMessageW(editor->hWnd, EM_REDO, 0, 0);
2000 return 0;
2001 case 26: /* Ctrl-Z */
2002 SendMessageW(editor->hWnd, EM_UNDO, 0, 0);
2003 return 0;
2005 if (((unsigned)wstr)>=' ' || wstr=='\r' || wstr=='\t') {
2006 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
2007 ME_Style *style = ME_GetInsertStyle(editor, 0);
2008 ME_SaveTempStyle(editor);
2009 ME_InsertTextFromCursor(editor, 0, &wstr, 1, style);
2010 ME_ReleaseStyle(style);
2011 ME_CommitUndo(editor);
2012 ME_UpdateRepaint(editor);
2014 return 0;
2016 case WM_VSCROLL:
2018 int nPos = editor->nScrollPosY;
2019 si.cbSize = sizeof(SCROLLINFO);
2020 si.fMask = SIF_PAGE|SIF_POS|SIF_RANGE|SIF_TRACKPOS;
2021 GetScrollInfo(hWnd, SB_VERT, &si);
2022 switch(LOWORD(wParam)) {
2023 case SB_LINEUP:
2024 nPos -= 24; /* FIXME follow the original */
2025 if (nPos<0) nPos = 0;
2026 break;
2027 case SB_LINEDOWN:
2029 int nEnd = editor->nTotalLength - editor->sizeWindow.cy;
2030 nPos += 24; /* FIXME follow the original */
2031 if (nPos>=nEnd) nPos = nEnd;
2032 break;
2034 case SB_PAGEUP:
2035 nPos -= editor->sizeWindow.cy;
2036 if (nPos<0) nPos = 0;
2037 break;
2038 case SB_PAGEDOWN:
2039 nPos += editor->sizeWindow.cy;
2040 if (nPos>=editor->nTotalLength) nPos = editor->nTotalLength-1;
2041 break;
2042 case SB_THUMBTRACK:
2043 case SB_THUMBPOSITION:
2044 nPos = si.nTrackPos;
2045 break;
2047 if (nPos != editor->nScrollPosY) {
2048 int dy = editor->nScrollPosY - nPos;
2049 editor->nScrollPosY = nPos;
2050 SetScrollPos(hWnd, SB_VERT, nPos, TRUE);
2051 if (editor->bRedraw)
2053 ScrollWindow(hWnd, 0, dy, NULL, NULL);
2054 UpdateWindow(hWnd);
2057 break;
2059 case WM_MOUSEWHEEL:
2061 int gcWheelDelta = 0, nPos = editor->nScrollPosY, nEnd = editor->nTotalLength - editor->sizeWindow.cy;
2062 UINT pulScrollLines;
2063 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
2064 gcWheelDelta -= GET_WHEEL_DELTA_WPARAM(wParam);
2065 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
2066 nPos += pulScrollLines * (gcWheelDelta / WHEEL_DELTA) * 8; /* FIXME follow the original */
2067 if (nPos>=nEnd)
2068 nPos = nEnd;
2069 if (nPos<0)
2070 nPos = 0;
2071 if (nPos != editor->nScrollPosY) {
2072 int dy = editor->nScrollPosY - nPos;
2073 editor->nScrollPosY = nPos;
2074 SetScrollPos(hWnd, SB_VERT, nPos, TRUE);
2075 if (editor->bRedraw)
2077 ScrollWindow(hWnd, 0, dy, NULL, NULL);
2078 UpdateWindow(hWnd);
2081 break;
2083 case EM_GETRECT:
2085 *((RECT *)lParam) = editor->rcFormat;
2086 return 0;
2088 case EM_SETRECT:
2089 case EM_SETRECTNP:
2091 if (lParam)
2093 RECT *rc = (RECT *)lParam;
2095 if (wParam)
2097 editor->rcFormat.left += rc->left;
2098 editor->rcFormat.top += rc->top;
2099 editor->rcFormat.right += rc->right;
2100 editor->rcFormat.bottom += rc->bottom;
2102 else
2104 editor->rcFormat = *rc;
2107 else
2109 GetClientRect(hWnd, &editor->rcFormat);
2111 if (msg != EM_SETRECTNP)
2112 ME_RewrapRepaint(editor);
2113 return 0;
2115 case EM_REQUESTRESIZE:
2116 ME_SendRequestResize(editor, TRUE);
2117 return 0;
2118 case WM_SETREDRAW:
2119 editor->bRedraw = wParam;
2120 return 0;
2121 case WM_SIZE:
2123 GetClientRect(hWnd, &editor->rcFormat);
2124 ME_RewrapRepaint(editor);
2125 return DefWindowProcW(hWnd, msg, wParam, lParam);
2127 case EM_GETOLEINTERFACE:
2129 LPVOID *ppvObj = (LPVOID*) lParam;
2130 FIXME("EM_GETOLEINTERFACE %p: stub\n", ppvObj);
2131 return CreateIRichEditOle(ppvObj);
2133 case EM_SETOLECALLBACK:
2134 if(editor->lpOleCallback)
2135 IUnknown_Release(editor->lpOleCallback);
2136 editor->lpOleCallback = (LPRICHEDITOLECALLBACK)lParam;
2137 if(editor->lpOleCallback)
2138 IUnknown_AddRef(editor->lpOleCallback);
2139 return TRUE;
2140 case EM_GETWORDBREAKPROC:
2141 return (LRESULT)editor->pfnWordBreak;
2142 case EM_SETWORDBREAKPROC:
2144 EDITWORDBREAKPROCW pfnOld = editor->pfnWordBreak;
2146 editor->pfnWordBreak = (EDITWORDBREAKPROCW)lParam;
2147 return (LRESULT)pfnOld;
2149 default:
2150 do_default:
2151 return DefWindowProcW(hWnd, msg, wParam, lParam);
2153 return 0L;
2157 /******************************************************************
2158 * RichEdit10ANSIWndProc (RICHED20.9)
2160 LRESULT WINAPI RichEdit10ANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2162 LRESULT result;
2164 /* FIXME: this is NOT the same as 2.0 version */
2165 result = RichEditANSIWndProc(hWnd, msg, wParam, lParam);
2166 if (msg == WM_NCCREATE)
2168 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongW(hWnd, 0);
2170 editor->bEmulateVersion10 = TRUE;
2171 editor->pBuffer->pLast->member.para.nCharOfs = 2;
2173 return result;
2176 void ME_SendOldNotify(ME_TextEditor *editor, int nCode)
2178 HWND hWnd = editor->hWnd;
2179 SendMessageA(GetParent(hWnd), WM_COMMAND, (nCode<<16)|GetWindowLongW(hWnd, GWLP_ID), (LPARAM)hWnd);
2182 int ME_CountParagraphsBetween(ME_TextEditor *editor, int from, int to)
2184 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
2185 int i = 0;
2187 while(item && item->member.para.next_para->member.para.nCharOfs <= from)
2188 item = item->member.para.next_para;
2189 if (!item)
2190 return 0;
2191 while(item && item->member.para.next_para->member.para.nCharOfs <= to) {
2192 item = item->member.para.next_para;
2193 i++;
2195 return i;
2199 int ME_GetTextW(ME_TextEditor *editor, WCHAR *buffer, int nStart, int nChars, int bCRLF)
2201 ME_DisplayItem *item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
2202 int nWritten = 0;
2203 WCHAR *pStart = buffer;
2205 if (!item) {
2206 *buffer = L'\0';
2207 return 0;
2210 if (nStart)
2212 int nLen = ME_StrLen(item->member.run.strText) - nStart;
2213 if (nLen > nChars)
2214 nLen = nChars;
2215 CopyMemory(buffer, item->member.run.strText->szData + nStart, sizeof(WCHAR)*nLen);
2216 nChars -= nLen;
2217 nWritten += nLen;
2218 if (!nChars)
2219 return nWritten;
2220 buffer += nLen;
2221 nStart = 0;
2222 item = ME_FindItemFwd(item, diRun);
2225 while(nChars && item)
2227 int nLen = ME_StrLen(item->member.run.strText);
2228 if (nLen > nChars)
2229 nLen = nChars;
2231 if (item->member.run.nFlags & MERF_ENDPARA)
2233 *buffer = '\r';
2234 if (bCRLF)
2236 *(++buffer) = '\n';
2237 nWritten++;
2239 assert(nLen == 1);
2240 /* our end paragraph consists of 2 characters now */
2241 if (editor->bEmulateVersion10)
2242 nChars--;
2244 else
2245 CopyMemory(buffer, item->member.run.strText->szData, sizeof(WCHAR)*nLen);
2246 nChars -= nLen;
2247 nWritten += nLen;
2248 buffer += nLen;
2250 if (!nChars)
2252 TRACE("nWritten=%d, actual=%d\n", nWritten, buffer-pStart);
2253 *buffer = L'\0';
2254 return nWritten;
2256 item = ME_FindItemFwd(item, diRun);
2258 *buffer = L'\0';
2259 TRACE("nWritten=%d, actual=%d\n", nWritten, buffer-pStart);
2260 return nWritten;
2263 void ME_RegisterEditorClass(HINSTANCE hInstance)
2265 BOOL bResult;
2266 WNDCLASSW wcW;
2267 WNDCLASSA wcA;
2269 wcW.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
2270 wcW.lpfnWndProc = RichEditANSIWndProc;
2271 wcW.cbClsExtra = 0;
2272 wcW.cbWndExtra = 4;
2273 wcW.hInstance = NULL; /* hInstance would register DLL-local class */
2274 wcW.hIcon = NULL;
2275 wcW.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
2276 wcW.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
2277 wcW.lpszMenuName = NULL;
2278 wcW.lpszClassName = wszClassName;
2279 bResult = RegisterClassW(&wcW);
2280 assert(bResult);
2281 wcW.lpszClassName = wszClassName50;
2282 bResult = RegisterClassW(&wcW);
2283 assert(bResult);
2285 wcA.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
2286 wcA.lpfnWndProc = RichEditANSIWndProc;
2287 wcA.cbClsExtra = 0;
2288 wcA.cbWndExtra = 4;
2289 wcA.hInstance = NULL; /* hInstance would register DLL-local class */
2290 wcA.hIcon = NULL;
2291 wcA.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
2292 wcA.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
2293 wcA.lpszMenuName = NULL;
2294 wcA.lpszClassName = "RichEdit20A";
2295 bResult = RegisterClassA(&wcA);
2296 assert(bResult);
2297 wcA.lpszClassName = "RichEdit50A";
2298 bResult = RegisterClassA(&wcA);
2299 assert(bResult);
2302 LRESULT WINAPI REComboWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
2303 /* FIXME: Not implemented */
2304 TRACE("hWnd %p msg %04x (%s) %08x %08lx\n",
2305 hWnd, msg, get_msg_name(msg), wParam, lParam);
2306 return DefWindowProcW(hWnd, msg, wParam, lParam);
2309 LRESULT WINAPI REListWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
2310 /* FIXME: Not implemented */
2311 TRACE("hWnd %p msg %04x (%s) %08x %08lx\n",
2312 hWnd, msg, get_msg_name(msg), wParam, lParam);
2313 return DefWindowProcW(hWnd, msg, wParam, lParam);
2316 /******************************************************************
2317 * REExtendedRegisterClass (RICHED20.8)
2319 * FIXME undocumented
2320 * Need to check for errors and implement controls and callbacks
2322 LRESULT WINAPI REExtendedRegisterClass(void)
2324 WNDCLASSW wcW;
2325 UINT result;
2327 FIXME("semi stub\n");
2329 wcW.cbClsExtra = 0;
2330 wcW.cbWndExtra = 4;
2331 wcW.hInstance = NULL;
2332 wcW.hIcon = NULL;
2333 wcW.hCursor = NULL;
2334 wcW.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
2335 wcW.lpszMenuName = NULL;
2337 if (!ME_ListBoxRegistered)
2339 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS;
2340 wcW.lpfnWndProc = REListWndProc;
2341 wcW.lpszClassName = wszClassNameListBox;
2342 if (RegisterClassW(&wcW)) ME_ListBoxRegistered = TRUE;
2345 if (!ME_ComboBoxRegistered)
2347 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
2348 wcW.lpfnWndProc = REComboWndProc;
2349 wcW.lpszClassName = wszClassNameComboBox;
2350 if (RegisterClassW(&wcW)) ME_ComboBoxRegistered = TRUE;
2353 result = 0;
2354 if (ME_ListBoxRegistered)
2355 result += 1;
2356 if (ME_ComboBoxRegistered)
2357 result += 2;
2359 return result;