shell32/tests: End the lines with CR+LF otherwise the profile APIs are unable to...
[wine/wine-gecko.git] / dlls / riched20 / editor.c
blob2e2c54911d8873d7440fa8b117ad9b0e5dd0a378
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 (only Y value valid)
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 (Also called EM_SETLIMITTEXT)
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 (partially implemented)
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 #define STACK_SIZE_DEFAULT 100
234 #define STACK_SIZE_MAX 1000
236 #define TEXT_LIMIT_DEFAULT 32767
238 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
240 static BOOL ME_RegisterEditorClass(HINSTANCE);
242 static const WCHAR RichEdit20W[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '2', '0', 'W', 0};
243 static const WCHAR RichEdit50W[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '5', '0', 'W', 0};
244 static const WCHAR REListBox20W[] = {'R','E','L','i','s','t','B','o','x','2','0','W', 0};
245 static const WCHAR REComboBox20W[] = {'R','E','C','o','m','b','o','B','o','x','2','0','W', 0};
247 int me_debug = 0;
248 HANDLE me_heap = NULL;
250 static BOOL ME_ListBoxRegistered = FALSE;
251 static BOOL ME_ComboBoxRegistered = FALSE;
253 static inline int is_version_nt(void)
255 return !(GetVersion() & 0x80000000);
258 static ME_TextBuffer *ME_MakeText(void) {
260 ME_TextBuffer *buf = ALLOC_OBJ(ME_TextBuffer);
262 ME_DisplayItem *p1 = ME_MakeDI(diTextStart);
263 ME_DisplayItem *p2 = ME_MakeDI(diTextEnd);
265 p1->prev = NULL;
266 p1->next = p2;
267 p2->prev = p1;
268 p2->next = NULL;
269 p1->member.para.next_para = p2;
270 p2->member.para.prev_para = p1;
271 p2->member.para.nCharOfs = 0;
273 buf->pFirst = p1;
274 buf->pLast = p2;
275 buf->pCharStyle = NULL;
277 return buf;
281 static LRESULT ME_StreamInText(ME_TextEditor *editor, DWORD dwFormat, ME_InStream *stream, ME_Style *style)
283 WCHAR wszText[STREAMIN_BUFFER_SIZE+1];
284 WCHAR *pText;
286 TRACE("%08x %p\n", dwFormat, stream);
288 do {
289 long nWideChars = 0;
291 if (!stream->dwSize)
293 ME_StreamInFill(stream);
294 if (stream->editstream->dwError)
295 break;
296 if (!stream->dwSize)
297 break;
300 if (!(dwFormat & SF_UNICODE))
302 /* FIXME? this is doomed to fail on true MBCS like UTF-8, luckily they're unlikely to be used as CP_ACP */
303 nWideChars = MultiByteToWideChar(CP_ACP, 0, stream->buffer, stream->dwSize, wszText, STREAMIN_BUFFER_SIZE);
304 pText = wszText;
306 else
308 nWideChars = stream->dwSize >> 1;
309 pText = (WCHAR *)stream->buffer;
312 ME_InsertTextFromCursor(editor, 0, pText, nWideChars, style);
313 if (stream->dwSize == 0)
314 break;
315 stream->dwSize = 0;
316 } while(1);
317 ME_CommitUndo(editor);
318 ME_UpdateRepaint(editor);
319 return 0;
322 static void ME_RTFCharAttrHook(RTF_Info *info)
324 CHARFORMAT2W fmt;
325 fmt.cbSize = sizeof(fmt);
326 fmt.dwMask = 0;
327 fmt.dwEffects = 0;
329 switch(info->rtfMinor)
331 case rtfPlain:
332 /* FIXME add more flags once they're implemented */
333 fmt.dwMask = CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_COLOR | CFM_BACKCOLOR | CFM_SIZE | CFM_WEIGHT;
334 fmt.dwEffects = CFE_AUTOCOLOR | CFE_AUTOBACKCOLOR;
335 fmt.yHeight = 12*20; /* 12pt */
336 fmt.wWeight = 400;
337 break;
338 case rtfBold:
339 fmt.dwMask = CFM_BOLD;
340 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
341 break;
342 case rtfItalic:
343 fmt.dwMask = CFM_ITALIC;
344 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
345 break;
346 case rtfUnderline:
347 fmt.dwMask = CFM_UNDERLINE;
348 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
349 fmt.bUnderlineType = CFU_CF1UNDERLINE;
350 break;
351 case rtfNoUnderline:
352 fmt.dwMask = CFM_UNDERLINE;
353 fmt.dwEffects = 0;
354 break;
355 case rtfStrikeThru:
356 fmt.dwMask = CFM_STRIKEOUT;
357 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
358 break;
359 case rtfSubScript:
360 case rtfSuperScript:
361 case rtfSubScrShrink:
362 case rtfSuperScrShrink:
363 case rtfNoSuperSub:
364 fmt.dwMask = CFM_SUBSCRIPT|CFM_SUPERSCRIPT;
365 if (info->rtfMinor == rtfSubScrShrink) fmt.dwEffects = CFE_SUBSCRIPT;
366 if (info->rtfMinor == rtfSuperScrShrink) fmt.dwEffects = CFE_SUPERSCRIPT;
367 if (info->rtfMinor == rtfNoSuperSub) fmt.dwEffects = 0;
368 break;
369 case rtfInvisible:
370 fmt.dwMask = CFM_HIDDEN;
371 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
372 break;
373 case rtfBackColor:
374 fmt.dwMask = CFM_BACKCOLOR;
375 fmt.dwEffects = 0;
376 if (info->rtfParam == 0)
377 fmt.dwEffects = CFE_AUTOBACKCOLOR;
378 else if (info->rtfParam != rtfNoParam)
380 RTFColor *c = RTFGetColor(info, info->rtfParam);
381 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
383 break;
384 case rtfForeColor:
385 fmt.dwMask = CFM_COLOR;
386 fmt.dwEffects = 0;
387 if (info->rtfParam == 0)
388 fmt.dwEffects = CFE_AUTOCOLOR;
389 else if (info->rtfParam != rtfNoParam)
391 RTFColor *c = RTFGetColor(info, info->rtfParam);
392 if (c)
393 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
394 else
395 fmt.crTextColor = 0;
397 break;
398 case rtfFontNum:
399 if (info->rtfParam != rtfNoParam)
401 RTFFont *f = RTFGetFont(info, info->rtfParam);
402 if (f)
404 MultiByteToWideChar(CP_ACP, 0, f->rtfFName, -1, fmt.szFaceName, sizeof(fmt.szFaceName)/sizeof(WCHAR));
405 fmt.szFaceName[sizeof(fmt.szFaceName)/sizeof(WCHAR)-1] = '\0';
406 fmt.bCharSet = f->rtfFCharSet;
407 fmt.dwMask = CFM_FACE | CFM_CHARSET;
410 break;
411 case rtfFontSize:
412 fmt.dwMask = CFM_SIZE;
413 if (info->rtfParam != rtfNoParam)
414 fmt.yHeight = info->rtfParam*10;
415 break;
417 if (fmt.dwMask) {
418 ME_Style *style2;
419 RTFFlushOutputBuffer(info);
420 /* FIXME too slow ? how come ? */
421 style2 = ME_ApplyStyle(info->style, &fmt);
422 ME_ReleaseStyle(info->style);
423 info->style = style2;
424 info->styleChanged = TRUE;
428 /* FIXME this function doesn't get any information about context of the RTF tag, which is very bad,
429 the same tags mean different things in different contexts */
430 static void ME_RTFParAttrHook(RTF_Info *info)
432 PARAFORMAT2 fmt;
433 fmt.cbSize = sizeof(fmt);
434 fmt.dwMask = 0;
436 switch(info->rtfMinor)
438 case rtfParDef: /* restores default paragraph attributes */
439 fmt.dwMask = PFM_ALIGNMENT | PFM_TABSTOPS | PFM_OFFSET | PFM_STARTINDENT;
440 fmt.wAlignment = PFA_LEFT;
441 fmt.cTabCount = 0;
442 fmt.dxOffset = fmt.dxStartIndent = 0;
443 RTFFlushOutputBuffer(info);
444 ME_GetParagraph(info->editor->pCursors[0].pRun)->member.para.bTable = FALSE;
445 break;
446 case rtfInTable:
448 ME_DisplayItem *para;
450 RTFFlushOutputBuffer(info);
451 para = ME_GetParagraph(info->editor->pCursors[0].pRun);
452 assert(para->member.para.pCells);
453 para->member.para.bTable = TRUE;
454 return;
456 case rtfFirstIndent:
457 ME_GetSelectionParaFormat(info->editor, &fmt);
458 fmt.dwMask = PFM_STARTINDENT | PFM_OFFSET;
459 fmt.dxStartIndent += info->rtfParam + fmt.dxOffset;
460 fmt.dxOffset = -info->rtfParam;
461 break;
462 case rtfLeftIndent:
463 ME_GetSelectionParaFormat(info->editor, &fmt);
464 fmt.dwMask = PFM_STARTINDENT;
465 fmt.dxStartIndent = -fmt.dxOffset + info->rtfParam;
466 break;
467 case rtfRightIndent:
468 fmt.dwMask = PFM_RIGHTINDENT;
469 fmt.dxRightIndent = info->rtfParam;
470 break;
471 case rtfQuadLeft:
472 case rtfQuadJust:
473 fmt.dwMask = PFM_ALIGNMENT;
474 fmt.wAlignment = PFA_LEFT;
475 break;
476 case rtfQuadRight:
477 fmt.dwMask = PFM_ALIGNMENT;
478 fmt.wAlignment = PFA_RIGHT;
479 break;
480 case rtfQuadCenter:
481 fmt.dwMask = PFM_ALIGNMENT;
482 fmt.wAlignment = PFA_CENTER;
483 break;
484 case rtfTabPos:
485 ME_GetSelectionParaFormat(info->editor, &fmt);
486 if (!(fmt.dwMask & PFM_TABSTOPS))
488 fmt.dwMask |= PFM_TABSTOPS;
489 fmt.cTabCount = 0;
491 if (fmt.cTabCount < MAX_TAB_STOPS)
492 fmt.rgxTabs[fmt.cTabCount++] = info->rtfParam;
493 break;
495 if (fmt.dwMask) {
496 RTFFlushOutputBuffer(info);
497 /* FIXME too slow ? how come ?*/
498 ME_SetSelectionParaFormat(info->editor, &fmt);
502 static void ME_RTFTblAttrHook(RTF_Info *info)
504 ME_DisplayItem *para;
506 switch (info->rtfMinor)
508 case rtfRowDef:
509 RTFFlushOutputBuffer(info);
510 para = ME_GetParagraph(info->editor->pCursors[0].pRun);
512 /* Release possibly inherited cell definitions */
513 ME_DestroyTableCellList(para);
515 para->member.para.pCells = ALLOC_OBJ(ME_TableCell);
516 para->member.para.pCells->nRightBoundary = 0;
517 para->member.para.pCells->next = NULL;
518 para->member.para.pLastCell = para->member.para.pCells;
519 break;
520 case rtfCellPos:
521 RTFFlushOutputBuffer(info);
522 para = ME_GetParagraph(info->editor->pCursors[0].pRun);
524 if (para->member.para.pLastCell->nRightBoundary)
526 ME_TableCell *pCell = ALLOC_OBJ(ME_TableCell);
528 pCell->next = NULL;
529 para->member.para.pLastCell->next = pCell;
530 para->member.para.pLastCell = pCell;
532 para->member.para.pLastCell->nRightBoundary = info->rtfParam;
533 break;
537 static void ME_RTFReadHook(RTF_Info *info) {
538 switch(info->rtfClass)
540 case rtfGroup:
541 switch(info->rtfMajor)
543 case rtfBeginGroup:
544 if (info->stackTop < maxStack) {
545 memcpy(&info->stack[info->stackTop].fmt, &info->style->fmt, sizeof(CHARFORMAT2W));
546 info->stack[info->stackTop].codePage = info->codePage;
547 info->stack[info->stackTop].unicodeLength = info->unicodeLength;
549 info->stackTop++;
550 info->styleChanged = FALSE;
551 break;
552 case rtfEndGroup:
554 ME_Style *s;
555 RTFFlushOutputBuffer(info);
556 if (info->stackTop<=1) {
557 info->rtfClass = rtfEOF;
558 return;
560 info->stackTop--;
561 assert(info->stackTop >= 0);
562 if (info->styleChanged)
564 /* FIXME too slow ? how come ? */
565 s = ME_ApplyStyle(info->style, &info->stack[info->stackTop].fmt);
566 ME_ReleaseStyle(info->style);
567 info->style = s;
568 info->codePage = info->stack[info->stackTop].codePage;
569 info->unicodeLength = info->stack[info->stackTop].unicodeLength;
571 break;
574 break;
575 case rtfControl:
576 switch(info->rtfMajor)
578 case rtfCharAttr:
579 ME_RTFCharAttrHook(info);
580 break;
581 case rtfParAttr:
582 ME_RTFParAttrHook(info);
583 break;
584 case rtfTblAttr:
585 ME_RTFTblAttrHook(info);
586 break;
587 case rtfSpecialChar:
588 if (info->rtfMinor == rtfCell)
590 RTFFlushOutputBuffer(info);
591 ME_InsertTableCellFromCursor(info->editor, 0);
594 break;
598 void
599 ME_StreamInFill(ME_InStream *stream)
601 stream->editstream->dwError = stream->editstream->pfnCallback(stream->editstream->dwCookie,
602 (BYTE *)stream->buffer,
603 sizeof(stream->buffer),
604 (LONG *)&stream->dwSize);
605 stream->dwUsed = 0;
608 static LRESULT ME_StreamIn(ME_TextEditor *editor, DWORD format, EDITSTREAM *stream)
610 RTF_Info parser;
611 ME_Style *style;
612 int from, to, to2, nUndoMode;
613 int nEventMask = editor->nEventMask;
614 ME_InStream inStream;
616 TRACE("stream==%p hWnd==%p format==0x%X\n", stream, editor->hWnd, (UINT)format);
617 editor->nEventMask = 0;
619 ME_GetSelection(editor, &from, &to);
620 if ((format & SFF_SELECTION) && (editor->mode & TM_RICHTEXT)) {
621 style = ME_GetSelectionInsertStyle(editor);
623 ME_InternalDeleteText(editor, from, to-from);
625 else {
626 style = editor->pBuffer->pDefaultStyle;
627 ME_AddRefStyle(style);
628 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
629 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
630 from = to = 0;
631 ME_ClearTempStyle(editor);
632 /* FIXME restore default paragraph formatting ! */
636 /* Back up undo mode to a local variable */
637 nUndoMode = editor->nUndoMode;
639 /* Only create an undo if SFF_SELECTION is set */
640 if (!(format & SFF_SELECTION))
641 editor->nUndoMode = umIgnore;
643 inStream.editstream = stream;
644 inStream.editstream->dwError = 0;
645 inStream.dwSize = 0;
646 inStream.dwUsed = 0;
648 if (format & SF_RTF)
650 /* Check if it's really RTF, and if it is not, use plain text */
651 ME_StreamInFill(&inStream);
652 if (!inStream.editstream->dwError)
654 if (strncmp(inStream.buffer, "{\\rtf", 5) && strncmp(inStream.buffer, "{\\urtf", 6))
656 format &= ~SF_RTF;
657 format |= SF_TEXT;
662 if (!inStream.editstream->dwError)
664 if (format & SF_RTF) {
665 /* setup the RTF parser */
666 memset(&parser, 0, sizeof parser);
667 RTFSetEditStream(&parser, &inStream);
668 parser.rtfFormat = format&(SF_TEXT|SF_RTF);
669 parser.hwndEdit = editor->hWnd;
670 parser.editor = editor;
671 parser.style = style;
672 WriterInit(&parser);
673 RTFInit(&parser);
674 RTFSetReadHook(&parser, ME_RTFReadHook);
675 BeginFile(&parser);
677 /* do the parsing */
678 RTFRead(&parser);
679 RTFFlushOutputBuffer(&parser);
680 RTFDestroy(&parser);
682 style = parser.style;
684 else if (format & SF_TEXT)
685 ME_StreamInText(editor, format, &inStream, style);
686 else
687 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
688 ME_GetSelection(editor, &to, &to2);
689 /* put the cursor at the top */
690 if (!(format & SFF_SELECTION))
691 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
694 /* Restore saved undo mode */
695 editor->nUndoMode = nUndoMode;
697 /* even if we didn't add an undo, we need to commit anything on the stack */
698 ME_CommitUndo(editor);
700 /* If SFF_SELECTION isn't set, delete any undos from before we started too */
701 if (!(format & SFF_SELECTION))
702 ME_EmptyUndoStack(editor);
704 ME_ReleaseStyle(style);
705 editor->nEventMask = nEventMask;
706 if (editor->bRedraw)
708 ME_UpdateRepaint(editor);
710 if (!(format & SFF_SELECTION)) {
711 ME_ClearTempStyle(editor);
713 ME_MoveCaret(editor);
714 ME_SendSelChange(editor);
715 ME_SendRequestResize(editor, FALSE);
717 return 0;
721 typedef struct tagME_RTFStringStreamStruct
723 char *string;
724 int pos;
725 int length;
726 } ME_RTFStringStreamStruct;
728 static DWORD CALLBACK ME_ReadFromRTFString(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
730 ME_RTFStringStreamStruct *pStruct = (ME_RTFStringStreamStruct *)dwCookie;
731 int count;
733 count = min(cb, pStruct->length - pStruct->pos);
734 memmove(lpBuff, pStruct->string + pStruct->pos, count);
735 pStruct->pos += count;
736 *pcb = count;
737 return 0;
740 static void
741 ME_StreamInRTFString(ME_TextEditor *editor, BOOL selection, char *string)
743 EDITSTREAM es;
744 ME_RTFStringStreamStruct data;
746 data.string = string;
747 data.length = strlen(string);
748 data.pos = 0;
749 es.dwCookie = (DWORD)&data;
750 es.pfnCallback = ME_ReadFromRTFString;
751 ME_StreamIn(editor, SF_RTF | (selection ? SFF_SELECTION : 0), &es);
755 ME_DisplayItem *
756 ME_FindItemAtOffset(ME_TextEditor *editor, ME_DIType nItemType, int nOffset, int *nItemOffset)
758 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
760 while (item && item->member.para.next_para->member.para.nCharOfs <= nOffset)
761 item = ME_FindItemFwd(item, diParagraph);
763 if (!item)
764 return item;
766 nOffset -= item->member.para.nCharOfs;
767 if (nItemType == diParagraph) {
768 if (nItemOffset)
769 *nItemOffset = nOffset;
770 return item;
773 do {
774 item = ME_FindItemFwd(item, diRun);
775 } while (item && (item->member.run.nCharOfs + ME_StrLen(item->member.run.strText) <= nOffset));
776 if (item) {
777 nOffset -= item->member.run.nCharOfs;
778 if (nItemOffset)
779 *nItemOffset = nOffset;
781 return item;
785 static int
786 ME_FindText(ME_TextEditor *editor, DWORD flags, CHARRANGE *chrg, const WCHAR *text, CHARRANGE *chrgText)
788 const int nLen = lstrlenW(text);
789 const int nTextLen = ME_GetTextLength(editor);
790 int nStart, nEnd;
791 int nMin, nMax;
792 ME_DisplayItem *item;
793 ME_DisplayItem *para;
794 WCHAR wLastChar = ' ';
796 TRACE("flags==0x%08x, chrg->cpMin==%d, chrg->cpMax==%d text==%s\n",
797 flags, chrg->cpMin, chrg->cpMax, debugstr_w(text));
799 if (flags & ~(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD))
800 FIXME("Flags 0x%08x not implemented\n",
801 flags & ~(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD));
803 nMin = chrg->cpMin;
804 if (chrg->cpMax == -1)
805 nMax = nTextLen;
806 else
807 nMax = chrg->cpMax > nTextLen ? nTextLen : chrg->cpMax;
809 /* when searching up, if cpMin < cpMax, then instead of searching
810 * on [cpMin,cpMax], we search on [0,cpMin], otherwise, search on
811 * [cpMax, cpMin]. The exception is when cpMax is -1, in which
812 * case, it is always bigger than cpMin.
814 if (!(flags & FR_DOWN))
816 int nSwap = nMax;
818 nMax = nMin > nTextLen ? nTextLen : nMin;
819 if (nMin < nSwap || chrg->cpMax == -1)
820 nMin = 0;
821 else
822 nMin = nSwap;
825 if (!nLen || nMin < 0 || nMax < 0 || nMax < nMin)
827 if (chrgText)
828 chrgText->cpMin = chrgText->cpMax = -1;
829 return -1;
832 if (flags & FR_DOWN) /* Forward search */
834 /* If possible, find the character before where the search starts */
835 if ((flags & FR_WHOLEWORD) && nMin)
837 nStart = nMin - 1;
838 item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
839 if (!item)
841 if (chrgText)
842 chrgText->cpMin = chrgText->cpMax = -1;
843 return -1;
845 wLastChar = item->member.run.strText->szData[nStart];
848 nStart = nMin;
849 item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
850 if (!item)
852 if (chrgText)
853 chrgText->cpMin = chrgText->cpMax = -1;
854 return -1;
857 para = ME_GetParagraph(item);
858 while (item
859 && para->member.para.nCharOfs + item->member.run.nCharOfs + nStart + nLen <= nMax)
861 ME_DisplayItem *pCurItem = item;
862 int nCurStart = nStart;
863 int nMatched = 0;
865 while (pCurItem && ME_CharCompare(pCurItem->member.run.strText->szData[nCurStart + nMatched], text[nMatched], (flags & FR_MATCHCASE)))
867 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
868 break;
870 nMatched++;
871 if (nMatched == nLen)
873 ME_DisplayItem *pNextItem = pCurItem;
874 int nNextStart = nCurStart;
875 WCHAR wNextChar;
877 /* Check to see if next character is a whitespace */
878 if (flags & FR_WHOLEWORD)
880 if (nCurStart + nMatched == ME_StrLen(pCurItem->member.run.strText))
882 pNextItem = ME_FindItemFwd(pCurItem, diRun);
883 nNextStart = -nMatched;
886 if (pNextItem)
887 wNextChar = pNextItem->member.run.strText->szData[nNextStart + nMatched];
888 else
889 wNextChar = ' ';
891 if (isalnumW(wNextChar))
892 break;
895 nStart += para->member.para.nCharOfs + pCurItem->member.run.nCharOfs;
896 if (chrgText)
898 chrgText->cpMin = nStart;
899 chrgText->cpMax = nStart + nLen;
901 TRACE("found at %d-%d\n", nStart, nStart + nLen);
902 return nStart;
904 if (nCurStart + nMatched == ME_StrLen(pCurItem->member.run.strText))
906 pCurItem = ME_FindItemFwd(pCurItem, diRun);
907 para = ME_GetParagraph(pCurItem);
908 nCurStart = -nMatched;
911 if (pCurItem)
912 wLastChar = pCurItem->member.run.strText->szData[nCurStart + nMatched];
913 else
914 wLastChar = ' ';
916 nStart++;
917 if (nStart == ME_StrLen(item->member.run.strText))
919 item = ME_FindItemFwd(item, diRun);
920 para = ME_GetParagraph(item);
921 nStart = 0;
925 else /* Backward search */
927 /* If possible, find the character after where the search ends */
928 if ((flags & FR_WHOLEWORD) && nMax < nTextLen - 1)
930 nEnd = nMax + 1;
931 item = ME_FindItemAtOffset(editor, diRun, nEnd, &nEnd);
932 if (!item)
934 if (chrgText)
935 chrgText->cpMin = chrgText->cpMax = -1;
936 return -1;
938 wLastChar = item->member.run.strText->szData[nEnd];
941 nEnd = nMax;
942 item = ME_FindItemAtOffset(editor, diRun, nEnd, &nEnd);
943 if (!item)
945 if (chrgText)
946 chrgText->cpMin = chrgText->cpMax = -1;
947 return -1;
950 para = ME_GetParagraph(item);
952 while (item
953 && para->member.para.nCharOfs + item->member.run.nCharOfs + nEnd - nLen >= nMin)
955 ME_DisplayItem *pCurItem = item;
956 int nCurEnd = nEnd;
957 int nMatched = 0;
959 if (nCurEnd - nMatched == 0)
961 pCurItem = ME_FindItemBack(pCurItem, diRun);
962 para = ME_GetParagraph(pCurItem);
963 nCurEnd = ME_StrLen(pCurItem->member.run.strText) + nMatched;
966 while (pCurItem && ME_CharCompare(pCurItem->member.run.strText->szData[nCurEnd - nMatched - 1], text[nLen - nMatched - 1], (flags & FR_MATCHCASE)))
968 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
969 break;
971 nMatched++;
972 if (nMatched == nLen)
974 ME_DisplayItem *pPrevItem = pCurItem;
975 int nPrevEnd = nCurEnd;
976 WCHAR wPrevChar;
978 /* Check to see if previous character is a whitespace */
979 if (flags & FR_WHOLEWORD)
981 if (nPrevEnd - nMatched == 0)
983 pPrevItem = ME_FindItemBack(pCurItem, diRun);
984 if (pPrevItem)
985 nPrevEnd = ME_StrLen(pPrevItem->member.run.strText) + nMatched;
988 if (pPrevItem)
989 wPrevChar = pPrevItem->member.run.strText->szData[nPrevEnd - nMatched - 1];
990 else
991 wPrevChar = ' ';
993 if (isalnumW(wPrevChar))
994 break;
997 nStart = para->member.para.nCharOfs + pCurItem->member.run.nCharOfs + nCurEnd - nMatched;
998 if (chrgText)
1000 chrgText->cpMin = nStart;
1001 chrgText->cpMax = nStart + nLen;
1003 TRACE("found at %d-%d\n", nStart, nStart + nLen);
1004 return nStart;
1006 if (nCurEnd - nMatched == 0)
1008 pCurItem = ME_FindItemBack(pCurItem, diRun);
1009 /* Don't care about pCurItem becoming NULL here; it's already taken
1010 * care of in the exterior loop condition */
1011 para = ME_GetParagraph(pCurItem);
1012 nCurEnd = ME_StrLen(pCurItem->member.run.strText) + nMatched;
1015 if (pCurItem)
1016 wLastChar = pCurItem->member.run.strText->szData[nCurEnd - nMatched - 1];
1017 else
1018 wLastChar = ' ';
1020 nEnd--;
1021 if (nEnd < 0)
1023 item = ME_FindItemBack(item, diRun);
1024 para = ME_GetParagraph(item);
1025 nEnd = ME_StrLen(item->member.run.strText);
1029 TRACE("not found\n");
1030 if (chrgText)
1031 chrgText->cpMin = chrgText->cpMax = -1;
1032 return -1;
1036 static BOOL
1037 ME_KeyDown(ME_TextEditor *editor, WORD nKey)
1039 BOOL ctrl_is_down = GetKeyState(VK_CONTROL) & 0x8000;
1040 BOOL shift_is_down = GetKeyState(VK_SHIFT) & 0x8000;
1042 switch (nKey)
1044 case VK_LEFT:
1045 case VK_RIGHT:
1046 case VK_UP:
1047 case VK_DOWN:
1048 case VK_HOME:
1049 case VK_END:
1050 case VK_PRIOR:
1051 case VK_NEXT:
1052 ME_ArrowKey(editor, nKey, shift_is_down, ctrl_is_down);
1053 return TRUE;
1054 case VK_BACK:
1055 case VK_DELETE:
1056 /* FIXME backspace and delete aren't the same, they act different wrt paragraph style of the merged paragraph */
1057 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY)
1058 return FALSE;
1059 if (ME_IsSelection(editor))
1060 ME_DeleteSelection(editor);
1061 else if (nKey == VK_DELETE || ME_ArrowKey(editor, VK_LEFT, FALSE, FALSE))
1062 ME_DeleteTextAtCursor(editor, 1, 1);
1063 else
1064 return TRUE;
1065 ME_CommitUndo(editor);
1066 ME_UpdateRepaint(editor);
1067 ME_SendRequestResize(editor, FALSE);
1068 return TRUE;
1070 default:
1071 if (ctrl_is_down)
1073 if (nKey == 'W')
1075 CHARFORMAT2W chf;
1076 char buf[2048];
1077 chf.cbSize = sizeof(chf);
1079 ME_GetSelectionCharFormat(editor, &chf);
1080 ME_DumpStyleToBuf(&chf, buf);
1081 MessageBoxA(NULL, buf, "Style dump", MB_OK);
1083 if (nKey == 'Q')
1085 ME_CheckCharOffsets(editor);
1089 return FALSE;
1092 static BOOL ME_ShowContextMenu(ME_TextEditor *editor, int x, int y)
1094 CHARRANGE selrange;
1095 HMENU menu;
1096 int seltype = 0;
1097 if(!editor->lpOleCallback)
1098 return FALSE;
1099 ME_GetSelection(editor, (int *)&selrange.cpMin, (int *)&selrange.cpMax);
1100 if(selrange.cpMin == selrange.cpMax)
1101 seltype |= SEL_EMPTY;
1102 else
1104 /* FIXME: Handle objects */
1105 seltype |= SEL_TEXT;
1106 if(selrange.cpMax-selrange.cpMin > 1)
1107 seltype |= SEL_MULTICHAR;
1109 if(SUCCEEDED(IRichEditOleCallback_GetContextMenu(editor->lpOleCallback, seltype, NULL, &selrange, &menu)))
1111 TrackPopupMenu(menu, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, GetParent(editor->hWnd), NULL);
1112 DestroyMenu(menu);
1114 return TRUE;
1117 ME_TextEditor *ME_MakeEditor(HWND hWnd) {
1118 ME_TextEditor *ed = ALLOC_OBJ(ME_TextEditor);
1119 HDC hDC;
1120 int i;
1121 ed->hWnd = hWnd;
1122 ed->bEmulateVersion10 = FALSE;
1123 ed->pBuffer = ME_MakeText();
1124 hDC = GetDC(hWnd);
1125 ME_MakeFirstParagraph(hDC, ed->pBuffer);
1126 ReleaseDC(hWnd, hDC);
1127 ed->bCaretShown = FALSE;
1128 ed->nCursors = 2;
1129 ed->pCursors = ALLOC_N_OBJ(ME_Cursor, ed->nCursors);
1130 ed->pCursors[0].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
1131 ed->pCursors[0].nOffset = 0;
1132 ed->pCursors[1].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
1133 ed->pCursors[1].nOffset = 0;
1134 ed->nLastTotalLength = ed->nTotalLength = 0;
1135 ed->nUDArrowX = -1;
1136 ed->nSequence = 0;
1137 ed->rgbBackColor = -1;
1138 ed->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
1139 ed->bCaretAtEnd = FALSE;
1140 ed->nEventMask = 0;
1141 ed->nModifyStep = 0;
1142 ed->nTextLimit = TEXT_LIMIT_DEFAULT;
1143 ed->pUndoStack = ed->pRedoStack = ed->pUndoStackBottom = NULL;
1144 ed->nUndoStackSize = 0;
1145 ed->nUndoLimit = STACK_SIZE_DEFAULT;
1146 ed->nUndoMode = umAddToUndo;
1147 ed->nParagraphs = 1;
1148 ed->nLastSelStart = ed->nLastSelEnd = 0;
1149 ed->pLastSelStartPara = ed->pLastSelEndPara = ME_FindItemFwd(ed->pBuffer->pFirst, diParagraph);
1150 ed->nZoomNumerator = ed->nZoomDenominator = 0;
1151 ed->bRedraw = TRUE;
1152 ed->bHideSelection = FALSE;
1153 ed->nInvalidOfs = -1;
1154 ed->pfnWordBreak = NULL;
1155 ed->lpOleCallback = NULL;
1156 ed->mode = TM_RICHTEXT | TM_MULTILEVELUNDO | TM_MULTICODEPAGE;
1157 ed->AutoURLDetect_bEnable = FALSE;
1158 GetClientRect(hWnd, &ed->rcFormat);
1159 for (i=0; i<HFONT_CACHE_SIZE; i++)
1161 ed->pFontCache[i].nRefs = 0;
1162 ed->pFontCache[i].nAge = 0;
1163 ed->pFontCache[i].hFont = NULL;
1166 ME_CheckCharOffsets(ed);
1168 if (GetWindowLongW(hWnd, GWL_STYLE) & ES_PASSWORD)
1169 ed->cPasswordMask = '*';
1170 else
1171 ed->cPasswordMask = 0;
1173 return ed;
1176 typedef struct tagME_GlobalDestStruct
1178 HGLOBAL hData;
1179 int nLength;
1180 } ME_GlobalDestStruct;
1182 static DWORD CALLBACK ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1184 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
1185 int i;
1186 WORD *pSrc, *pDest;
1188 cb = cb >> 1;
1189 pDest = (WORD *)lpBuff;
1190 pSrc = (WORD *)GlobalLock(pData->hData);
1191 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
1192 pDest[i] = pSrc[pData->nLength+i];
1194 pData->nLength += i;
1195 *pcb = 2*i;
1196 GlobalUnlock(pData->hData);
1197 return 0;
1200 static DWORD CALLBACK ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1202 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
1203 int i;
1204 BYTE *pSrc, *pDest;
1206 pDest = lpBuff;
1207 pSrc = (BYTE *)GlobalLock(pData->hData);
1208 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
1209 pDest[i] = pSrc[pData->nLength+i];
1211 pData->nLength += i;
1212 *pcb = i;
1213 GlobalUnlock(pData->hData);
1214 return 0;
1218 void ME_DestroyEditor(ME_TextEditor *editor)
1220 ME_DisplayItem *pFirst = editor->pBuffer->pFirst;
1221 ME_DisplayItem *p = pFirst, *pNext = NULL;
1222 int i;
1224 ME_ClearTempStyle(editor);
1225 ME_EmptyUndoStack(editor);
1226 while(p) {
1227 pNext = p->next;
1228 ME_DestroyDisplayItem(p);
1229 p = pNext;
1231 ME_ReleaseStyle(editor->pBuffer->pDefaultStyle);
1232 for (i=0; i<HFONT_CACHE_SIZE; i++)
1234 if (editor->pFontCache[i].hFont)
1235 DeleteObject(editor->pFontCache[i].hFont);
1237 DeleteObject(editor->hbrBackground);
1238 if(editor->lpOleCallback)
1239 IUnknown_Release(editor->lpOleCallback);
1241 FREE_OBJ(editor->pBuffer);
1242 FREE_OBJ(editor->pCursors);
1244 FREE_OBJ(editor);
1247 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
1249 TRACE("\n");
1250 switch (fdwReason)
1252 case DLL_PROCESS_ATTACH:
1253 DisableThreadLibraryCalls(hinstDLL);
1254 me_heap = HeapCreate (0, 0x10000, 0);
1255 if (!ME_RegisterEditorClass(hinstDLL)) return FALSE;
1256 LookupInit();
1257 break;
1259 case DLL_PROCESS_DETACH:
1260 UnregisterClassW(RichEdit20W, 0);
1261 UnregisterClassW(RichEdit50W, 0);
1262 UnregisterClassA("RichEdit20A", 0);
1263 UnregisterClassA("RichEdit50A", 0);
1264 if (ME_ListBoxRegistered)
1265 UnregisterClassW(REListBox20W, 0);
1266 if (ME_ComboBoxRegistered)
1267 UnregisterClassW(REComboBox20W, 0);
1268 LookupCleanup();
1269 HeapDestroy (me_heap);
1270 me_heap = NULL;
1271 break;
1273 return TRUE;
1277 #define UNSUPPORTED_MSG(e) \
1278 case e: \
1279 FIXME(#e ": stub\n"); \
1280 return DefWindowProcW(hWnd, msg, wParam, lParam);
1282 static const char * const edit_messages[] = {
1283 "EM_GETSEL",
1284 "EM_SETSEL",
1285 "EM_GETRECT",
1286 "EM_SETRECT",
1287 "EM_SETRECTNP",
1288 "EM_SCROLL",
1289 "EM_LINESCROLL",
1290 "EM_SCROLLCARET",
1291 "EM_GETMODIFY",
1292 "EM_SETMODIFY",
1293 "EM_GETLINECOUNT",
1294 "EM_LINEINDEX",
1295 "EM_SETHANDLE",
1296 "EM_GETHANDLE",
1297 "EM_GETTHUMB",
1298 "EM_UNKNOWN_BF",
1299 "EM_UNKNOWN_C0",
1300 "EM_LINELENGTH",
1301 "EM_REPLACESEL",
1302 "EM_UNKNOWN_C3",
1303 "EM_GETLINE",
1304 "EM_LIMITTEXT",
1305 "EM_CANUNDO",
1306 "EM_UNDO",
1307 "EM_FMTLINES",
1308 "EM_LINEFROMCHAR",
1309 "EM_UNKNOWN_CA",
1310 "EM_SETTABSTOPS",
1311 "EM_SETPASSWORDCHAR",
1312 "EM_EMPTYUNDOBUFFER",
1313 "EM_GETFIRSTVISIBLELINE",
1314 "EM_SETREADONLY",
1315 "EM_SETWORDBREAKPROC",
1316 "EM_GETWORDBREAKPROC",
1317 "EM_GETPASSWORDCHAR",
1318 "EM_SETMARGINS",
1319 "EM_GETMARGINS",
1320 "EM_GETLIMITTEXT",
1321 "EM_POSFROMCHAR",
1322 "EM_CHARFROMPOS"
1325 static const char * const richedit_messages[] = {
1326 "EM_CANPASTE",
1327 "EM_DISPLAYBAND",
1328 "EM_EXGETSEL",
1329 "EM_EXLIMITTEXT",
1330 "EM_EXLINEFROMCHAR",
1331 "EM_EXSETSEL",
1332 "EM_FINDTEXT",
1333 "EM_FORMATRANGE",
1334 "EM_GETCHARFORMAT",
1335 "EM_GETEVENTMASK",
1336 "EM_GETOLEINTERFACE",
1337 "EM_GETPARAFORMAT",
1338 "EM_GETSELTEXT",
1339 "EM_HIDESELECTION",
1340 "EM_PASTESPECIAL",
1341 "EM_REQUESTRESIZE",
1342 "EM_SELECTIONTYPE",
1343 "EM_SETBKGNDCOLOR",
1344 "EM_SETCHARFORMAT",
1345 "EM_SETEVENTMASK",
1346 "EM_SETOLECALLBACK",
1347 "EM_SETPARAFORMAT",
1348 "EM_SETTARGETDEVICE",
1349 "EM_STREAMIN",
1350 "EM_STREAMOUT",
1351 "EM_GETTEXTRANGE",
1352 "EM_FINDWORDBREAK",
1353 "EM_SETOPTIONS",
1354 "EM_GETOPTIONS",
1355 "EM_FINDTEXTEX",
1356 "EM_GETWORDBREAKPROCEX",
1357 "EM_SETWORDBREAKPROCEX",
1358 "EM_SETUNDOLIMIT",
1359 "EM_UNKNOWN_USER_83",
1360 "EM_REDO",
1361 "EM_CANREDO",
1362 "EM_GETUNDONAME",
1363 "EM_GETREDONAME",
1364 "EM_STOPGROUPTYPING",
1365 "EM_SETTEXTMODE",
1366 "EM_GETTEXTMODE",
1367 "EM_AUTOURLDETECT",
1368 "EM_GETAUTOURLDETECT",
1369 "EM_SETPALETTE",
1370 "EM_GETTEXTEX",
1371 "EM_GETTEXTLENGTHEX",
1372 "EM_SHOWSCROLLBAR",
1373 "EM_SETTEXTEX",
1374 "EM_UNKNOWN_USER_98",
1375 "EM_UNKNOWN_USER_99",
1376 "EM_SETPUNCTUATION",
1377 "EM_GETPUNCTUATION",
1378 "EM_SETWORDWRAPMODE",
1379 "EM_GETWORDWRAPMODE",
1380 "EM_SETIMECOLOR",
1381 "EM_GETIMECOLOR",
1382 "EM_SETIMEOPTIONS",
1383 "EM_GETIMEOPTIONS",
1384 "EM_CONVPOSITION",
1385 "EM_UNKNOWN_USER_109",
1386 "EM_UNKNOWN_USER_110",
1387 "EM_UNKNOWN_USER_111",
1388 "EM_UNKNOWN_USER_112",
1389 "EM_UNKNOWN_USER_113",
1390 "EM_UNKNOWN_USER_114",
1391 "EM_UNKNOWN_USER_115",
1392 "EM_UNKNOWN_USER_116",
1393 "EM_UNKNOWN_USER_117",
1394 "EM_UNKNOWN_USER_118",
1395 "EM_UNKNOWN_USER_119",
1396 "EM_SETLANGOPTIONS",
1397 "EM_GETLANGOPTIONS",
1398 "EM_GETIMECOMPMODE",
1399 "EM_FINDTEXTW",
1400 "EM_FINDTEXTEXW",
1401 "EM_RECONVERSION",
1402 "EM_SETIMEMODEBIAS",
1403 "EM_GETIMEMODEBIAS"
1406 static const char *
1407 get_msg_name(UINT msg)
1409 if (msg >= EM_GETSEL && msg <= EM_CHARFROMPOS)
1410 return edit_messages[msg - EM_GETSEL];
1411 if (msg >= EM_CANPASTE && msg <= EM_GETIMEMODEBIAS)
1412 return richedit_messages[msg - EM_CANPASTE];
1413 return "";
1416 static LRESULT RichEditWndProc_common(HWND hWnd, UINT msg, WPARAM wParam,
1417 LPARAM lParam, BOOL unicode)
1419 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongPtrW(hWnd, 0);
1421 TRACE("hwnd %p msg %04x (%s) %x %lx, unicode %d\n",
1422 hWnd, msg, get_msg_name(msg), wParam, lParam, unicode);
1424 if (!editor && msg != WM_NCCREATE && msg != WM_NCDESTROY) {
1425 ERR("called with invalid hWnd %p - application bug?\n", hWnd);
1426 return 0;
1429 switch(msg) {
1431 UNSUPPORTED_MSG(EM_DISPLAYBAND)
1432 UNSUPPORTED_MSG(EM_FINDWORDBREAK)
1433 UNSUPPORTED_MSG(EM_FMTLINES)
1434 UNSUPPORTED_MSG(EM_FORMATRANGE)
1435 UNSUPPORTED_MSG(EM_GETBIDIOPTIONS)
1436 UNSUPPORTED_MSG(EM_GETEDITSTYLE)
1437 UNSUPPORTED_MSG(EM_GETIMECOMPMODE)
1438 /* UNSUPPORTED_MSG(EM_GETIMESTATUS) missing in Wine headers */
1439 UNSUPPORTED_MSG(EM_GETLANGOPTIONS)
1440 /* UNSUPPORTED_MSG(EM_GETOLEINTERFACE) separate stub */
1441 UNSUPPORTED_MSG(EM_GETREDONAME)
1442 UNSUPPORTED_MSG(EM_GETTEXTMODE)
1443 UNSUPPORTED_MSG(EM_GETTYPOGRAPHYOPTIONS)
1444 UNSUPPORTED_MSG(EM_GETUNDONAME)
1445 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX)
1446 UNSUPPORTED_MSG(EM_PASTESPECIAL)
1447 UNSUPPORTED_MSG(EM_SELECTIONTYPE)
1448 UNSUPPORTED_MSG(EM_SETBIDIOPTIONS)
1449 UNSUPPORTED_MSG(EM_SETEDITSTYLE)
1450 UNSUPPORTED_MSG(EM_SETFONTSIZE)
1451 UNSUPPORTED_MSG(EM_SETLANGOPTIONS)
1452 UNSUPPORTED_MSG(EM_SETPALETTE)
1453 UNSUPPORTED_MSG(EM_SETTABSTOPS)
1454 UNSUPPORTED_MSG(EM_SETTARGETDEVICE)
1455 UNSUPPORTED_MSG(EM_SETTYPOGRAPHYOPTIONS)
1456 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX)
1457 UNSUPPORTED_MSG(WM_STYLECHANGING)
1458 UNSUPPORTED_MSG(WM_STYLECHANGED)
1459 /* UNSUPPORTED_MSG(WM_UNICHAR) FIXME missing in Wine headers */
1461 /* Messages specific to Richedit controls */
1463 case EM_STREAMIN:
1464 return ME_StreamIn(editor, wParam, (EDITSTREAM*)lParam);
1465 case EM_STREAMOUT:
1466 return ME_StreamOut(editor, wParam, (EDITSTREAM *)lParam);
1467 case WM_GETDLGCODE:
1469 UINT code = DLGC_WANTCHARS|DLGC_WANTARROWS;
1470 if(lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
1472 int vk = (int)((LPMSG)lParam)->wParam;
1473 /* if style says we want return key */
1474 if((vk == VK_RETURN) && (GetWindowLongW(hWnd, GWL_STYLE) & ES_WANTRETURN))
1476 code |= DLGC_WANTMESSAGE;
1478 /* we always handle ctrl-tab */
1479 if((vk == VK_TAB) && (GetKeyState(VK_CONTROL) & 0x8000))
1481 code |= DLGC_WANTMESSAGE;
1484 return code;
1486 case WM_NCCREATE:
1488 CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
1489 TRACE("WM_NCCREATE: style 0x%08x\n", pcs->style);
1490 editor = ME_MakeEditor(hWnd);
1491 SetWindowLongPtrW(hWnd, 0, (LONG_PTR)editor);
1492 return TRUE;
1494 case EM_EMPTYUNDOBUFFER:
1495 ME_EmptyUndoStack(editor);
1496 return 0;
1497 case EM_GETSEL:
1499 /* Note: wParam/lParam can be NULL */
1500 UINT from, to;
1501 PUINT pfrom = wParam ? (PUINT)wParam : &from;
1502 PUINT pto = lParam ? (PUINT)lParam : &to;
1503 ME_GetSelection(editor, (int *)pfrom, (int *)pto);
1504 if ((*pfrom|*pto) & 0xFFFF0000)
1505 return -1;
1506 return MAKELONG(*pfrom,*pto);
1508 case EM_EXGETSEL:
1510 CHARRANGE *pRange = (CHARRANGE *)lParam;
1511 ME_GetSelection(editor, (int *)&pRange->cpMin, (int *)&pRange->cpMax);
1512 TRACE("EM_EXGETSEL = (%d,%d)\n", pRange->cpMin, pRange->cpMax);
1513 return 0;
1515 case EM_SETUNDOLIMIT:
1517 if ((int)wParam < 0)
1518 editor->nUndoLimit = STACK_SIZE_DEFAULT;
1519 else
1520 editor->nUndoLimit = min(wParam, STACK_SIZE_MAX);
1521 /* Setting a max stack size keeps wine from getting killed
1522 for hogging memory. Windows allocates all this memory at once, so
1523 no program would realistically set a value above our maxiumum. */
1524 return editor->nUndoLimit;
1526 case EM_CANUNDO:
1527 return editor->pUndoStack != NULL;
1528 case EM_CANREDO:
1529 return editor->pRedoStack != NULL;
1530 case WM_UNDO: /* FIXME: actually not the same */
1531 case EM_UNDO:
1532 ME_Undo(editor);
1533 return 0;
1534 case EM_REDO:
1535 ME_Redo(editor);
1536 return 0;
1537 case EM_GETOPTIONS:
1539 /* these flags are equivalent to the ES_* counterparts */
1540 DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
1541 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN;
1542 DWORD settings = GetWindowLongW(hWnd, GWL_STYLE) & mask;
1544 return settings;
1546 case EM_SETOPTIONS:
1548 /* these flags are equivalent to ES_* counterparts
1549 * ECO_READONLY is already implemented in the code, only requires
1550 * setting the bit to work
1552 DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
1553 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN;
1554 DWORD raw = GetWindowLongW(hWnd, GWL_STYLE);
1555 DWORD settings = mask & raw;
1557 switch(wParam)
1559 case ECOOP_SET:
1560 settings = lParam;
1561 break;
1562 case ECOOP_OR:
1563 settings |= lParam;
1564 break;
1565 case ECOOP_AND:
1566 settings &= lParam;
1567 break;
1568 case ECOOP_XOR:
1569 settings ^= lParam;
1571 SetWindowLongW(hWnd, GWL_STYLE, (raw & ~mask) | (settings & mask));
1573 if (lParam & ECO_AUTOWORDSELECTION)
1574 FIXME("ECO_AUTOWORDSELECTION not implemented yet!\n");
1575 if (lParam & ECO_SELECTIONBAR)
1576 FIXME("ECO_SELECTIONBAR not implemented yet!\n");
1577 if (lParam & ECO_VERTICAL)
1578 FIXME("ECO_VERTICAL not implemented yet!\n");
1579 if (lParam & ECO_AUTOHSCROLL)
1580 FIXME("ECO_AUTOHSCROLL not implemented yet!\n");
1581 if (lParam & ECO_AUTOVSCROLL)
1582 FIXME("ECO_AUTOVSCROLL not implemented yet!\n");
1583 if (lParam & ECO_NOHIDESEL)
1584 FIXME("ECO_NOHIDESEL not implemented yet!\n");
1585 if (lParam & ECO_WANTRETURN)
1586 FIXME("ECO_WANTRETURN not implemented yet!\n");
1588 return settings;
1590 case EM_SETSEL:
1592 ME_InvalidateSelection(editor);
1593 ME_SetSelection(editor, wParam, lParam);
1594 ME_InvalidateSelection(editor);
1595 ME_SendSelChange(editor);
1596 return 0;
1598 case EM_SETSCROLLPOS:
1600 POINT *point = (POINT *)lParam;
1601 ME_ScrollAbs(editor, point->y);
1602 return 0;
1604 case EM_AUTOURLDETECT:
1606 if (wParam==1 || wParam ==0)
1608 editor->AutoURLDetect_bEnable = (BOOL)wParam;
1609 return 0;
1611 return E_INVALIDARG;
1613 case EM_GETAUTOURLDETECT:
1615 return editor->AutoURLDetect_bEnable;
1617 case EM_EXSETSEL:
1619 int end;
1620 CHARRANGE range = *(CHARRANGE *)lParam;
1622 TRACE("EM_EXSETSEL (%d,%d)\n", range.cpMin, range.cpMax);
1624 ME_InvalidateSelection(editor);
1625 end = ME_SetSelection(editor, range.cpMin, range.cpMax);
1626 ME_InvalidateSelection(editor);
1627 ME_SendSelChange(editor);
1629 return end;
1631 case EM_SHOWSCROLLBAR:
1633 ShowScrollBar(editor->hWnd, wParam, lParam);
1634 return 0;
1636 case EM_SETTEXTEX:
1638 LPWSTR wszText = (LPWSTR)lParam;
1639 SETTEXTEX *pStruct = (SETTEXTEX *)wParam;
1640 size_t len = wszText ? lstrlenW(wszText) : 0;
1641 int from, to;
1642 ME_Style *style;
1643 int oldModify = editor->nModifyStep;
1644 TRACE("EM_SETTEXTEX - %s, flags %d, cp %d\n", debugstr_w(wszText), (int)pStruct->flags, pStruct->codepage);
1645 if (pStruct->codepage != 1200) {
1646 FIXME("EM_SETTEXTEX only supports unicode right now!\n");
1647 return 0;
1649 /* FIXME: this should support RTF strings too, according to MSDN */
1650 if (pStruct->flags & ST_SELECTION) {
1651 ME_GetSelection(editor, &from, &to);
1652 style = ME_GetSelectionInsertStyle(editor);
1653 ME_InternalDeleteText(editor, from, to - from);
1654 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
1655 ME_ReleaseStyle(style);
1657 else {
1658 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
1659 ME_InsertTextFromCursor(editor, 0, wszText, len, editor->pBuffer->pDefaultStyle);
1660 len = 1;
1662 ME_CommitUndo(editor);
1663 if (!(pStruct->flags & ST_KEEPUNDO))
1665 editor->nModifyStep = oldModify;
1666 ME_EmptyUndoStack(editor);
1668 ME_UpdateRepaint(editor);
1669 return len;
1671 case EM_SETBKGNDCOLOR:
1673 LRESULT lColor = ME_GetBackColor(editor);
1674 if (editor->rgbBackColor != -1)
1675 DeleteObject(editor->hbrBackground);
1676 if (wParam)
1678 editor->rgbBackColor = -1;
1679 editor->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
1681 else
1683 editor->rgbBackColor = lParam;
1684 editor->hbrBackground = CreateSolidBrush(editor->rgbBackColor);
1686 if (editor->bRedraw)
1688 InvalidateRect(hWnd, NULL, TRUE);
1689 UpdateWindow(hWnd);
1691 return lColor;
1693 case EM_GETMODIFY:
1694 return editor->nModifyStep == 0 ? 0 : 1;
1695 case EM_SETMODIFY:
1697 if (wParam)
1698 editor->nModifyStep = 1;
1699 else
1700 editor->nModifyStep = 0;
1702 return 0;
1704 case EM_SETREADONLY:
1706 long nStyle = GetWindowLongW(hWnd, GWL_STYLE);
1707 if (wParam)
1708 nStyle |= ES_READONLY;
1709 else
1710 nStyle &= ~ES_READONLY;
1711 SetWindowLongW(hWnd, GWL_STYLE, nStyle);
1712 return 0;
1714 case EM_SETEVENTMASK:
1716 DWORD nOldMask = editor->nEventMask;
1718 editor->nEventMask = lParam;
1719 return nOldMask;
1721 case EM_GETEVENTMASK:
1722 return editor->nEventMask;
1723 case EM_SETCHARFORMAT:
1725 CHARFORMAT2W buf, *p;
1726 BOOL bRepaint = TRUE;
1727 p = ME_ToCF2W(&buf, (CHARFORMAT2W *)lParam);
1728 if (!wParam || (editor->mode & TM_PLAINTEXT))
1729 ME_SetDefaultCharFormat(editor, p);
1730 else if (wParam == (SCF_WORD | SCF_SELECTION))
1731 FIXME("EM_SETCHARFORMAT: word selection not supported\n");
1732 else if (wParam == SCF_ALL)
1733 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), p);
1734 else {
1735 int from, to;
1736 ME_GetSelection(editor, &from, &to);
1737 bRepaint = (from != to);
1738 ME_SetSelectionCharFormat(editor, p);
1740 editor->nModifyStep = 1;
1741 ME_CommitUndo(editor);
1742 if (bRepaint)
1743 ME_RewrapRepaint(editor);
1744 return 0;
1746 case EM_GETCHARFORMAT:
1748 CHARFORMAT2W tmp, *dst = (CHARFORMAT2W *)lParam;
1749 if (dst->cbSize != sizeof(CHARFORMATA) &&
1750 dst->cbSize != sizeof(CHARFORMATW) &&
1751 dst->cbSize != sizeof(CHARFORMAT2A) &&
1752 dst->cbSize != sizeof(CHARFORMAT2W))
1753 return 0;
1754 tmp.cbSize = sizeof(tmp);
1755 if (!wParam)
1756 ME_GetDefaultCharFormat(editor, &tmp);
1757 else
1758 ME_GetSelectionCharFormat(editor, &tmp);
1759 ME_CopyToCFAny(dst, &tmp);
1760 return tmp.dwMask;
1762 case EM_SETPARAFORMAT:
1763 ME_SetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
1764 ME_RewrapRepaint(editor);
1765 ME_CommitUndo(editor);
1766 return 0;
1767 case EM_GETPARAFORMAT:
1768 ME_GetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
1769 return 0;
1770 case EM_GETFIRSTVISIBLELINE:
1772 ME_DisplayItem *p = editor->pBuffer->pFirst;
1773 int y = ME_GetYScrollPos(editor);
1774 int ypara = 0;
1775 int count = 0;
1776 int ystart, yend;
1777 while(p) {
1778 p = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
1779 if (p->type == diTextEnd)
1780 break;
1781 if (p->type == diParagraph) {
1782 ypara = p->member.para.nYPos;
1783 continue;
1785 ystart = ypara + p->member.row.nYPos;
1786 yend = ystart + p->member.row.nHeight;
1787 if (y < yend) {
1788 break;
1790 count++;
1792 return count;
1794 case EM_HIDESELECTION:
1796 editor->bHideSelection = (wParam != 0);
1797 ME_InvalidateSelection(editor);
1798 return 0;
1800 case EM_LINESCROLL:
1802 ME_ScrollDown(editor, lParam * 8); /* FIXME follow the original */
1803 return TRUE; /* Should return false if a single line richedit control */
1805 case WM_CLEAR:
1807 int from, to;
1808 ME_GetSelection(editor, &from, &to);
1809 ME_InternalDeleteText(editor, from, to-from);
1810 ME_CommitUndo(editor);
1811 ME_UpdateRepaint(editor);
1812 return 0;
1814 case EM_REPLACESEL:
1816 int from, to;
1817 ME_Style *style;
1818 LPWSTR wszText = ME_ToUnicode(unicode, (void *)lParam);
1819 size_t len = lstrlenW(wszText);
1820 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText));
1822 ME_GetSelection(editor, &from, &to);
1823 style = ME_GetSelectionInsertStyle(editor);
1824 ME_InternalDeleteText(editor, from, to-from);
1825 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
1826 ME_ReleaseStyle(style);
1827 /* drop temporary style if line end */
1829 * FIXME question: does abc\n mean: put abc,
1830 * clear temp style, put \n? (would require a change)
1832 if (len>0 && wszText[len-1] == '\n')
1833 ME_ClearTempStyle(editor);
1834 ME_EndToUnicode(unicode, wszText);
1835 ME_CommitUndo(editor);
1836 if (!wParam)
1837 ME_EmptyUndoStack(editor);
1838 ME_UpdateRepaint(editor);
1839 return 0;
1841 case EM_SCROLLCARET:
1843 int top, bottom; /* row's edges relative to document top */
1844 int nPos;
1845 ME_DisplayItem *para, *row;
1847 nPos = ME_GetYScrollPos(editor);
1848 row = ME_RowStart(editor->pCursors[0].pRun);
1849 para = ME_GetParagraph(row);
1850 top = para->member.para.nYPos + row->member.row.nYPos;
1851 bottom = top + row->member.row.nHeight;
1853 if (top < nPos) /* caret above window */
1854 ME_ScrollAbs(editor, top);
1855 else if (nPos + editor->sizeWindow.cy < bottom) /*below*/
1856 ME_ScrollAbs(editor, bottom - editor->sizeWindow.cy);
1857 return 0;
1859 case WM_SETFONT:
1861 LOGFONTW lf;
1862 CHARFORMAT2W fmt;
1863 HDC hDC;
1864 BOOL bRepaint = LOWORD(lParam);
1866 if (!wParam)
1867 wParam = (WPARAM)GetStockObject(SYSTEM_FONT);
1868 GetObjectW((HGDIOBJ)wParam, sizeof(LOGFONTW), &lf);
1869 hDC = GetDC(hWnd);
1870 ME_CharFormatFromLogFont(hDC, &lf, &fmt);
1871 ReleaseDC(hWnd, hDC);
1872 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), &fmt);
1873 ME_SetDefaultCharFormat(editor, &fmt);
1875 ME_CommitUndo(editor);
1876 if (bRepaint)
1877 ME_RewrapRepaint(editor);
1878 return 0;
1880 case WM_SETTEXT:
1882 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
1883 if (lParam)
1885 TRACE("WM_SETTEXT lParam==%lx\n",lParam);
1886 if (!unicode && !strncmp((char *)lParam, "{\\rtf", 5))
1888 /* Undocumented: WM_SETTEXT supports RTF text */
1889 ME_StreamInRTFString(editor, 0, (char *)lParam);
1891 else
1893 LPWSTR wszText = ME_ToUnicode(unicode, (void *)lParam);
1894 TRACE("WM_SETTEXT - %s\n", debugstr_w(wszText)); /* debugstr_w() */
1895 if (lstrlenW(wszText) > 0)
1897 /* uses default style! */
1898 ME_InsertTextFromCursor(editor, 0, wszText, -1, editor->pBuffer->pDefaultStyle);
1900 ME_EndToUnicode(unicode, wszText);
1903 else
1904 TRACE("WM_SETTEXT - NULL\n");
1905 ME_CommitUndo(editor);
1906 ME_EmptyUndoStack(editor);
1907 ME_SetSelection(editor, 0, 0);
1908 ME_UpdateRepaint(editor);
1909 return 1;
1911 case EM_CANPASTE:
1913 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
1914 if (IsClipboardFormatAvailable(nRTFFormat))
1915 return TRUE;
1916 if (IsClipboardFormatAvailable(CF_UNICODETEXT))
1917 return TRUE;
1918 return FALSE;
1920 case WM_PASTE:
1922 DWORD dwFormat = 0;
1923 EDITSTREAM es;
1924 ME_GlobalDestStruct gds;
1925 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
1926 UINT cf = 0;
1928 if (IsClipboardFormatAvailable(nRTFFormat))
1929 cf = nRTFFormat, dwFormat = SF_RTF;
1930 else if (IsClipboardFormatAvailable(CF_UNICODETEXT))
1931 cf = CF_UNICODETEXT, dwFormat = SF_TEXT|SF_UNICODE;
1932 else
1933 return 0;
1935 if (!OpenClipboard(hWnd))
1936 return 0;
1937 gds.hData = GetClipboardData(cf);
1938 gds.nLength = 0;
1939 es.dwCookie = (DWORD)&gds;
1940 es.pfnCallback = dwFormat == SF_RTF ? ME_ReadFromHGLOBALRTF : ME_ReadFromHGLOBALUnicode;
1941 ME_StreamIn(editor, dwFormat|SFF_SELECTION, &es);
1943 CloseClipboard();
1944 return 0;
1946 case WM_CUT:
1947 case WM_COPY:
1949 LPDATAOBJECT dataObj = NULL;
1950 CHARRANGE range;
1951 HRESULT hr = S_OK;
1953 if (editor->cPasswordMask)
1954 return 0; /* Copying or Cutting masked text isn't allowed */
1956 ME_GetSelection(editor, (int*)&range.cpMin, (int*)&range.cpMax);
1957 if(editor->lpOleCallback)
1958 hr = IRichEditOleCallback_GetClipboardData(editor->lpOleCallback, &range, RECO_COPY, &dataObj);
1959 if(FAILED(hr) || !dataObj)
1960 hr = ME_GetDataObject(editor, &range, &dataObj);
1961 if(SUCCEEDED(hr)) {
1962 hr = OleSetClipboard(dataObj);
1963 IDataObject_Release(dataObj);
1965 if (SUCCEEDED(hr) && msg == WM_CUT)
1967 ME_InternalDeleteText(editor, range.cpMin, range.cpMax-range.cpMin);
1968 ME_CommitUndo(editor);
1969 ME_UpdateRepaint(editor);
1971 return 0;
1973 case WM_GETTEXTLENGTH:
1974 return ME_GetTextLength(editor);
1975 case EM_GETTEXTLENGTHEX:
1976 return ME_GetTextLengthEx(editor, (GETTEXTLENGTHEX *)wParam);
1977 case WM_GETTEXT:
1979 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
1980 tr.chrg.cpMin = 0;
1981 tr.chrg.cpMax = wParam ? (wParam - 1) : 0;
1982 tr.lpstrText = (WCHAR *)lParam;
1983 return RichEditWndProc_common(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr, unicode);
1985 case EM_GETTEXTEX:
1987 GETTEXTEX *ex = (GETTEXTEX*)wParam;
1988 int nStart, nCount;
1990 if (ex->flags & ~(GT_SELECTION | GT_USECRLF))
1991 FIXME("GETTEXTEX flags 0x%08x not supported\n", ex->flags & ~(GT_SELECTION | GT_USECRLF));
1993 if (ex->flags & GT_SELECTION)
1995 ME_GetSelection(editor, &nStart, &nCount);
1996 nCount -= nStart;
1997 nCount = min(nCount, ex->cb - 1);
1999 else
2001 nStart = 0;
2002 nCount = ex->cb - 1;
2004 if (ex->codepage == 1200 || unicode)
2006 nCount = min(nCount, ex->cb / sizeof(WCHAR) - 1);
2007 return ME_GetTextW(editor, (LPWSTR)lParam, nStart, nCount, ex->flags & GT_USECRLF);
2009 else
2011 /* potentially each char may be a CR, why calculate the exact value with O(N) when
2012 we can just take a bigger buffer? :) */
2013 int crlfmul = (ex->flags & GT_USECRLF) ? 2 : 1;
2014 LPWSTR buffer = richedit_alloc((crlfmul*nCount + 1) * sizeof(WCHAR));
2015 DWORD buflen = ex->cb;
2016 LRESULT rc;
2017 DWORD flags = 0;
2019 buflen = ME_GetTextW(editor, buffer, nStart, nCount, ex->flags & GT_USECRLF);
2020 rc = WideCharToMultiByte(ex->codepage, flags, buffer, -1, (LPSTR)lParam, ex->cb, ex->lpDefaultChar, ex->lpUsedDefaultChar);
2022 richedit_free(buffer);
2023 return rc;
2026 case EM_GETSELTEXT:
2028 int from, to;
2029 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
2030 ME_GetSelection(editor, &from, &to);
2031 tr.chrg.cpMin = from;
2032 tr.chrg.cpMax = to;
2033 tr.lpstrText = (WCHAR *)lParam;
2034 return RichEditWndProc_common(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr, unicode);
2036 case EM_GETSCROLLPOS:
2038 POINT *point = (POINT *)lParam;
2039 point->x = 0; /* FIXME side scrolling not implemented */
2040 point->y = ME_GetYScrollPos(editor);
2041 return 1;
2043 case EM_GETTEXTRANGE:
2045 TEXTRANGEW *rng = (TEXTRANGEW *)lParam;
2046 TRACE("EM_GETTEXTRANGE min=%d max=%d unicode=%d emul1.0=%d length=%d\n",
2047 rng->chrg.cpMin, rng->chrg.cpMax, unicode,
2048 editor->bEmulateVersion10, ME_GetTextLength(editor));
2049 if (unicode)
2050 return ME_GetTextW(editor, rng->lpstrText, rng->chrg.cpMin, rng->chrg.cpMax-rng->chrg.cpMin, editor->bEmulateVersion10);
2051 else
2053 int nLen = rng->chrg.cpMax-rng->chrg.cpMin;
2054 WCHAR *p = ALLOC_N_OBJ(WCHAR, nLen+1);
2055 int nChars = ME_GetTextW(editor, p, rng->chrg.cpMin, nLen, editor->bEmulateVersion10);
2056 /* FIXME this is a potential security hole (buffer overrun)
2057 if you know more about wchar->mbyte conversion please explain
2059 WideCharToMultiByte(CP_ACP, 0, p, nChars+1, (char *)rng->lpstrText, nLen+1, NULL, NULL);
2060 FREE_OBJ(p);
2061 return nChars;
2064 case EM_GETLINE:
2066 ME_DisplayItem *run;
2067 const unsigned int nMaxChars = *(WORD *) lParam;
2068 unsigned int nEndChars, nCharsLeft = nMaxChars;
2069 char *dest = (char *) lParam;
2070 /* rich text editor 1.0 uses \r\n for line end, 2.0 uses just \r;
2071 we need to know how if we have the extra \n or not */
2072 int nLF = editor->bEmulateVersion10;
2074 TRACE("EM_GETLINE: row=%d, nMaxChars=%d (%s)\n", (int) wParam, nMaxChars,
2075 unicode ? "Unicode" : "Ansi");
2077 run = ME_FindRowWithNumber(editor, wParam);
2078 if (run == NULL)
2079 return 0;
2081 while (nCharsLeft && (run = ME_FindItemFwd(run, diRunOrStartRow))
2082 && !(run->member.run.nFlags & MERF_ENDPARA))
2084 unsigned int nCopy;
2085 ME_String *strText;
2086 if (run->type != diRun)
2087 break;
2088 strText = run->member.run.strText;
2089 nCopy = min(nCharsLeft, strText->nLen);
2091 if (unicode)
2092 lstrcpynW((LPWSTR) dest, strText->szData, nCopy);
2093 else
2094 nCopy = WideCharToMultiByte(CP_ACP, 0, strText->szData, nCopy, dest,
2095 nCharsLeft, NULL, NULL);
2096 dest += nCopy * (unicode ? sizeof(WCHAR) : 1);
2097 nCharsLeft -= nCopy;
2100 /* append \r\0 (or \r\n\0 in 1.0), space allowing */
2101 nEndChars = min(nCharsLeft, 2 + nLF);
2102 nCharsLeft -= nEndChars;
2103 if (unicode)
2105 const WCHAR src[] = {'\r', '\0'};
2106 const WCHAR src10[] = {'\r', '\n', '\0'};
2107 lstrcpynW((LPWSTR) dest, nLF ? src10 : src, nEndChars);
2109 else
2110 lstrcpynA(dest, nLF ? "\r\n" : "\r", nEndChars);
2112 TRACE("EM_GETLINE: got %u bytes\n", nMaxChars - nCharsLeft);
2114 if (nEndChars == 2 + nLF)
2115 return nMaxChars - nCharsLeft - 1; /* don't count \0 */
2116 else
2117 return nMaxChars - nCharsLeft;
2119 case EM_GETLINECOUNT:
2121 ME_DisplayItem *item = editor->pBuffer->pFirst->next;
2122 int nRows = 0;
2124 while (item != editor->pBuffer->pLast)
2126 assert(item->type == diParagraph);
2127 nRows += item->member.para.nRows;
2128 item = item->member.para.next_para;
2130 TRACE("EM_GETLINECOUNT: nRows==%d\n", nRows);
2131 return max(1, nRows);
2133 case EM_LINEFROMCHAR:
2135 if (wParam == -1)
2136 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(editor, 1));
2137 else
2138 return ME_RowNumberFromCharOfs(editor, wParam);
2140 case EM_EXLINEFROMCHAR:
2142 if (lParam == -1)
2143 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(editor,1));
2144 else
2145 return ME_RowNumberFromCharOfs(editor, lParam);
2147 case EM_LINEINDEX:
2149 ME_DisplayItem *item, *para;
2150 int nCharOfs;
2152 if (wParam == -1)
2153 item = ME_FindItemBack(editor->pCursors[0].pRun, diStartRow);
2154 else
2155 item = ME_FindRowWithNumber(editor, wParam);
2156 if (!item)
2157 return -1;
2158 para = ME_GetParagraph(item);
2159 item = ME_FindItemFwd(item, diRun);
2160 nCharOfs = para->member.para.nCharOfs + item->member.run.nCharOfs;
2161 TRACE("EM_LINEINDEX: nCharOfs==%d\n", nCharOfs);
2162 return nCharOfs;
2164 case EM_LINELENGTH:
2166 ME_DisplayItem *item, *item_end;
2167 int nChars = 0, nThisLineOfs = 0, nNextLineOfs = 0;
2169 if (wParam > ME_GetTextLength(editor))
2170 return 0;
2171 if (wParam == -1)
2173 FIXME("EM_LINELENGTH: returning number of unselected characters on lines with selection unsupported.\n");
2174 return 0;
2176 item = ME_FindItemAtOffset(editor, diRun, wParam, NULL);
2177 item = ME_RowStart(item);
2178 nThisLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item, diRun), 0);
2179 item_end = ME_FindItemFwd(item, diStartRowOrParagraphOrEnd);
2180 if (item_end->type == diStartRow)
2181 nNextLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item_end, diRun), 0);
2182 else
2183 nNextLineOfs = ME_FindItemFwd(item, diParagraphOrEnd)->member.para.nCharOfs
2184 - (editor->bEmulateVersion10?2:1);
2185 nChars = nNextLineOfs - nThisLineOfs;
2186 TRACE("EM_LINELENGTH(%d)==%d\n",wParam, nChars);
2187 return nChars;
2189 case EM_EXLIMITTEXT:
2191 if ((int)lParam < 0)
2192 return 0;
2193 if (lParam == 0)
2194 editor->nTextLimit = 65536;
2195 else
2196 editor->nTextLimit = (int) lParam;
2197 return 0;
2199 case EM_LIMITTEXT:
2201 if (wParam == 0)
2202 editor->nTextLimit = 65536;
2203 else
2204 editor->nTextLimit = (int) wParam;
2205 return 0;
2207 case EM_GETLIMITTEXT:
2209 return editor->nTextLimit;
2211 case EM_FINDTEXT:
2213 FINDTEXTA *ft = (FINDTEXTA *)lParam;
2214 int nChars = MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, NULL, 0);
2215 WCHAR *tmp;
2216 LRESULT r;
2218 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
2219 MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, tmp, nChars);
2220 r = ME_FindText(editor, wParam, &ft->chrg, tmp, NULL);
2221 FREE_OBJ( tmp );
2222 return r;
2224 case EM_FINDTEXTEX:
2226 FINDTEXTEXA *ex = (FINDTEXTEXA *)lParam;
2227 int nChars = MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, NULL, 0);
2228 WCHAR *tmp;
2229 LRESULT r;
2231 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
2232 MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, tmp, nChars);
2233 r = ME_FindText(editor, wParam, &ex->chrg, tmp, &ex->chrgText);
2234 FREE_OBJ( tmp );
2235 return r;
2237 case EM_FINDTEXTW:
2239 FINDTEXTW *ft = (FINDTEXTW *)lParam;
2240 return ME_FindText(editor, wParam, &ft->chrg, ft->lpstrText, NULL);
2242 case EM_FINDTEXTEXW:
2244 FINDTEXTEXW *ex = (FINDTEXTEXW *)lParam;
2245 return ME_FindText(editor, wParam, &ex->chrg, ex->lpstrText, &ex->chrgText);
2247 case EM_GETZOOM:
2248 if (!wParam || !lParam)
2249 return FALSE;
2250 *(int *)wParam = editor->nZoomNumerator;
2251 *(int *)lParam = editor->nZoomDenominator;
2252 return TRUE;
2253 case EM_SETZOOM:
2254 return ME_SetZoom(editor, wParam, lParam);
2255 case EM_CHARFROMPOS:
2256 return ME_CharFromPos(editor, ((POINTL *)lParam)->x, ((POINTL *)lParam)->y);
2257 case EM_POSFROMCHAR:
2259 ME_DisplayItem *pRun;
2260 int nCharOfs, nOffset, nLength;
2261 POINTL pt = {0,0};
2263 nCharOfs = wParam;
2264 /* detect which API version we're dealing with */
2265 if (wParam >= 0x40000)
2266 nCharOfs = lParam;
2267 nLength = ME_GetTextLength(editor);
2269 if (nCharOfs < nLength) {
2270 ME_RunOfsFromCharOfs(editor, nCharOfs, &pRun, &nOffset);
2271 assert(pRun->type == diRun);
2272 pt.y = pRun->member.run.pt.y;
2273 pt.x = pRun->member.run.pt.x + ME_PointFromChar(editor, &pRun->member.run, nOffset);
2274 pt.y += ME_GetParagraph(pRun)->member.para.nYPos;
2275 } else {
2276 pt.x = 0;
2277 pt.y = editor->pBuffer->pLast->member.para.nYPos;
2279 if (wParam >= 0x40000) {
2280 *(POINTL *)wParam = pt;
2282 return MAKELONG( pt.x, pt.y );
2284 case WM_CREATE:
2285 if (GetWindowLongW(hWnd, GWL_STYLE) & WS_HSCROLL)
2286 { /* Squelch the default horizontal scrollbar it would make */
2287 ShowScrollBar(editor->hWnd, SB_HORZ, FALSE);
2289 ME_CommitUndo(editor);
2290 ME_WrapMarkedParagraphs(editor);
2291 ME_MoveCaret(editor);
2292 return 0;
2293 case WM_DESTROY:
2294 ME_DestroyEditor(editor);
2295 SetWindowLongPtrW(hWnd, 0, 0);
2296 return 0;
2297 case WM_LBUTTONDOWN:
2298 SetFocus(hWnd);
2299 ME_LButtonDown(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
2300 SetCapture(hWnd);
2301 ME_LinkNotify(editor,msg,wParam,lParam);
2302 break;
2303 case WM_MOUSEMOVE:
2304 if (GetCapture() == hWnd)
2305 ME_MouseMove(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
2306 ME_LinkNotify(editor,msg,wParam,lParam);
2307 break;
2308 case WM_LBUTTONUP:
2309 if (GetCapture() == hWnd)
2310 ReleaseCapture();
2311 ME_LinkNotify(editor,msg,wParam,lParam);
2312 break;
2313 case WM_LBUTTONDBLCLK:
2314 ME_LinkNotify(editor,msg,wParam,lParam);
2315 ME_SelectWord(editor);
2316 break;
2317 case WM_CONTEXTMENU:
2318 if (!ME_ShowContextMenu(editor, (short)LOWORD(lParam), (short)HIWORD(lParam)))
2319 goto do_default;
2320 break;
2321 case WM_PAINT:
2322 if (editor->bRedraw)
2324 HDC hDC;
2325 PAINTSTRUCT ps;
2327 hDC = BeginPaint(hWnd, &ps);
2328 ME_PaintContent(editor, hDC, FALSE, &ps.rcPaint);
2329 EndPaint(hWnd, &ps);
2331 break;
2332 case WM_SETFOCUS:
2333 ME_ShowCaret(editor);
2334 ME_SendOldNotify(editor, EN_SETFOCUS);
2335 return 0;
2336 case WM_KILLFOCUS:
2337 ME_HideCaret(editor);
2338 ME_SendOldNotify(editor, EN_KILLFOCUS);
2339 return 0;
2340 case WM_ERASEBKGND:
2342 if (editor->bRedraw)
2344 HDC hDC = (HDC)wParam;
2345 RECT rc;
2346 if (GetUpdateRect(hWnd,&rc,TRUE))
2348 FillRect(hDC, &rc, editor->hbrBackground);
2351 return 1;
2353 case WM_COMMAND:
2354 TRACE("editor wnd command = %d\n", LOWORD(wParam));
2355 return 0;
2356 case WM_KEYDOWN:
2357 if (ME_KeyDown(editor, LOWORD(wParam)))
2358 return 0;
2359 goto do_default;
2360 case WM_CHAR:
2362 WCHAR wstr;
2364 if (unicode)
2365 wstr = (WCHAR)wParam;
2366 else
2368 CHAR charA = wParam;
2369 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &wstr, 1);
2371 if (editor->AutoURLDetect_bEnable)
2372 ME_AutoURLDetect(editor, wstr);
2374 switch (wstr)
2376 case 1: /* Ctrl-A */
2377 ME_SetSelection(editor, 0, -1);
2378 return 0;
2379 case 3: /* Ctrl-C */
2380 SendMessageW(editor->hWnd, WM_COPY, 0, 0);
2381 return 0;
2384 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY) {
2385 MessageBeep(MB_ICONERROR);
2386 return 0; /* FIXME really 0 ? */
2389 switch (wstr)
2391 case 22: /* Ctrl-V */
2392 SendMessageW(editor->hWnd, WM_PASTE, 0, 0);
2393 return 0;
2394 case 24: /* Ctrl-X */
2395 SendMessageW(editor->hWnd, WM_CUT, 0, 0);
2396 return 0;
2397 case 25: /* Ctrl-Y */
2398 SendMessageW(editor->hWnd, EM_REDO, 0, 0);
2399 return 0;
2400 case 26: /* Ctrl-Z */
2401 SendMessageW(editor->hWnd, EM_UNDO, 0, 0);
2402 return 0;
2404 if (((unsigned)wstr)>=' ' || wstr=='\r' || wstr=='\t') {
2405 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
2406 ME_Style *style = ME_GetInsertStyle(editor, 0);
2407 ME_SaveTempStyle(editor);
2408 ME_InsertTextFromCursor(editor, 0, &wstr, 1, style);
2409 ME_ReleaseStyle(style);
2410 ME_CommitUndo(editor);
2411 ME_UpdateRepaint(editor);
2413 return 0;
2415 case EM_SCROLL: /* fall through */
2416 case WM_VSCROLL:
2418 int origNPos;
2419 int lineHeight;
2421 origNPos = ME_GetYScrollPos(editor);
2422 lineHeight = 24;
2424 if (editor && editor->pBuffer && editor->pBuffer->pDefaultStyle)
2425 lineHeight = editor->pBuffer->pDefaultStyle->tm.tmHeight;
2426 if (lineHeight <= 0) lineHeight = 24;
2428 switch(LOWORD(wParam))
2430 case SB_LINEUP:
2431 ME_ScrollUp(editor,lineHeight);
2432 break;
2433 case SB_LINEDOWN:
2434 ME_ScrollDown(editor,lineHeight);
2435 break;
2436 case SB_PAGEUP:
2437 ME_ScrollUp(editor,editor->sizeWindow.cy);
2438 break;
2439 case SB_PAGEDOWN:
2440 ME_ScrollDown(editor,editor->sizeWindow.cy);
2441 break;
2442 case SB_THUMBTRACK:
2443 case SB_THUMBPOSITION:
2444 ME_ScrollAbs(editor,HIWORD(wParam));
2445 break;
2447 if (msg == EM_SCROLL)
2448 return 0x00010000 | (((ME_GetYScrollPos(editor) - origNPos)/lineHeight) & 0xffff);
2449 break;
2451 case WM_MOUSEWHEEL:
2453 int gcWheelDelta;
2454 UINT pulScrollLines;
2456 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
2457 gcWheelDelta = -GET_WHEEL_DELTA_WPARAM(wParam);
2459 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
2461 /* FIXME follow the original */
2462 ME_ScrollDown(editor,pulScrollLines * (gcWheelDelta / WHEEL_DELTA) * 8);
2464 break;
2466 case EM_GETRECT:
2468 *((RECT *)lParam) = editor->rcFormat;
2469 return 0;
2471 case EM_SETRECT:
2472 case EM_SETRECTNP:
2474 if (lParam)
2476 RECT *rc = (RECT *)lParam;
2478 if (wParam)
2480 editor->rcFormat.left += rc->left;
2481 editor->rcFormat.top += rc->top;
2482 editor->rcFormat.right += rc->right;
2483 editor->rcFormat.bottom += rc->bottom;
2485 else
2487 editor->rcFormat = *rc;
2490 else
2492 GetClientRect(hWnd, &editor->rcFormat);
2494 if (msg != EM_SETRECTNP)
2495 ME_RewrapRepaint(editor);
2496 return 0;
2498 case EM_REQUESTRESIZE:
2499 ME_SendRequestResize(editor, TRUE);
2500 return 0;
2501 case WM_SETREDRAW:
2502 editor->bRedraw = wParam;
2503 return 0;
2504 case WM_SIZE:
2506 GetClientRect(hWnd, &editor->rcFormat);
2507 ME_RewrapRepaint(editor);
2508 return DefWindowProcW(hWnd, msg, wParam, lParam);
2510 case EM_GETOLEINTERFACE:
2512 LPVOID *ppvObj = (LPVOID*) lParam;
2513 return CreateIRichEditOle(editor, ppvObj);
2515 case EM_GETPASSWORDCHAR:
2517 return editor->cPasswordMask;
2519 case EM_SETOLECALLBACK:
2520 if(editor->lpOleCallback)
2521 IUnknown_Release(editor->lpOleCallback);
2522 editor->lpOleCallback = (LPRICHEDITOLECALLBACK)lParam;
2523 if(editor->lpOleCallback)
2524 IUnknown_AddRef(editor->lpOleCallback);
2525 return TRUE;
2526 case EM_GETWORDBREAKPROC:
2527 return (LRESULT)editor->pfnWordBreak;
2528 case EM_SETWORDBREAKPROC:
2530 EDITWORDBREAKPROCW pfnOld = editor->pfnWordBreak;
2532 editor->pfnWordBreak = (EDITWORDBREAKPROCW)lParam;
2533 return (LRESULT)pfnOld;
2535 case EM_SETTEXTMODE:
2537 LRESULT ret;
2538 int mask = 0;
2539 int changes = 0;
2540 ret = RichEditWndProc_common(hWnd, WM_GETTEXTLENGTH, 0, 0, unicode);
2541 if (!ret)
2543 /*Check for valid wParam*/
2544 if ((((wParam & TM_RICHTEXT) && ((wParam & TM_PLAINTEXT) << 1))) ||
2545 (((wParam & TM_MULTILEVELUNDO) && ((wParam & TM_SINGLELEVELUNDO) << 1))) ||
2546 (((wParam & TM_MULTICODEPAGE) && ((wParam & TM_SINGLECODEPAGE) << 1))))
2547 return 1;
2548 else
2550 if (wParam & (TM_RICHTEXT | TM_PLAINTEXT))
2552 mask |= (TM_RICHTEXT | TM_PLAINTEXT);
2553 changes |= (wParam & (TM_RICHTEXT | TM_PLAINTEXT));
2555 /*FIXME: Currently no support for undo level and code page options*/
2556 editor->mode = (editor->mode & (~mask)) | changes;
2557 return 0;
2560 return ret;
2562 case EM_SETPASSWORDCHAR:
2564 editor->cPasswordMask = wParam;
2565 ME_RewrapRepaint(editor);
2566 return 0;
2568 default:
2569 do_default:
2570 return DefWindowProcW(hWnd, msg, wParam, lParam);
2572 return 0L;
2575 static LRESULT WINAPI RichEditWndProcW(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2577 BOOL unicode = TRUE;
2579 /* Under Win9x RichEdit20W returns ANSI strings, see the tests. */
2580 if (msg == WM_GETTEXT && (GetVersion() & 0x80000000))
2581 unicode = FALSE;
2583 return RichEditWndProc_common(hWnd, msg, wParam, lParam, unicode);
2586 static LRESULT WINAPI RichEditWndProcA(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2588 return RichEditWndProc_common(hWnd, msg, wParam, lParam, FALSE);
2591 /******************************************************************
2592 * RichEditANSIWndProc (RICHED20.10)
2594 LRESULT WINAPI RichEditANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2596 return RichEditWndProcA(hWnd, msg, wParam, lParam);
2599 /******************************************************************
2600 * RichEdit10ANSIWndProc (RICHED20.9)
2602 LRESULT WINAPI RichEdit10ANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2604 LRESULT result;
2606 /* FIXME: this is NOT the same as 2.0 version */
2607 result = RichEditANSIWndProc(hWnd, msg, wParam, lParam);
2608 if (msg == WM_NCCREATE)
2610 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongPtrW(hWnd, 0);
2612 editor->bEmulateVersion10 = TRUE;
2613 editor->pBuffer->pLast->member.para.nCharOfs = 2;
2615 return result;
2618 void ME_SendOldNotify(ME_TextEditor *editor, int nCode)
2620 HWND hWnd = editor->hWnd;
2621 SendMessageA(GetParent(hWnd), WM_COMMAND, (nCode<<16)|GetWindowLongW(hWnd, GWLP_ID), (LPARAM)hWnd);
2624 void ME_LinkNotify(ME_TextEditor *editor, UINT msg, WPARAM wParam, LPARAM lParam)
2626 int x,y;
2627 ME_Cursor tmpCursor;
2628 int nCharOfs; /* The start of the clicked text. Absolute character offset */
2630 ME_Run *tmpRun;
2632 ENLINK info;
2633 x = (short)LOWORD(lParam);
2634 y = (short)HIWORD(lParam);
2635 nCharOfs = ME_CharFromPos(editor, x, y);
2636 if (nCharOfs < 0) return;
2638 ME_CursorFromCharOfs(editor, nCharOfs, &tmpCursor);
2639 tmpRun = &tmpCursor.pRun->member.run;
2641 if ((tmpRun->style->fmt.dwMask & CFM_LINK)
2642 && (tmpRun->style->fmt.dwEffects & CFE_LINK))
2643 { /* The clicked run has CFE_LINK set */
2644 info.nmhdr.hwndFrom = editor->hWnd;
2645 info.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
2646 info.nmhdr.code = EN_LINK;
2647 info.msg = msg;
2648 info.wParam = wParam;
2649 info.lParam = lParam;
2650 info.chrg.cpMin = ME_CharOfsFromRunOfs(editor,tmpCursor.pRun,0);
2651 info.chrg.cpMax = info.chrg.cpMin + ME_StrVLen(tmpRun->strText);
2652 SendMessageW(GetParent(editor->hWnd), WM_NOTIFY,info.nmhdr.idFrom, (LPARAM)&info);
2656 int ME_CountParagraphsBetween(ME_TextEditor *editor, int from, int to)
2658 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
2659 int i = 0;
2661 while(item && item->member.para.next_para->member.para.nCharOfs <= from)
2662 item = item->member.para.next_para;
2663 if (!item)
2664 return 0;
2665 while(item && item->member.para.next_para->member.para.nCharOfs <= to) {
2666 item = item->member.para.next_para;
2667 i++;
2669 return i;
2673 int ME_GetTextW(ME_TextEditor *editor, WCHAR *buffer, int nStart, int nChars, int bCRLF)
2675 ME_DisplayItem *item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
2676 int nWritten = 0;
2677 WCHAR *pStart = buffer;
2679 if (!item) {
2680 *buffer = 0;
2681 return 0;
2684 if (nStart)
2686 int nLen = ME_StrLen(item->member.run.strText) - nStart;
2687 if (nLen > nChars)
2688 nLen = nChars;
2689 CopyMemory(buffer, item->member.run.strText->szData + nStart, sizeof(WCHAR)*nLen);
2690 nChars -= nLen;
2691 nWritten += nLen;
2692 if (!nChars)
2693 return nWritten;
2694 buffer += nLen;
2695 nStart = 0;
2696 item = ME_FindItemFwd(item, diRun);
2699 while(nChars && item)
2701 int nLen = ME_StrLen(item->member.run.strText);
2702 if (nLen > nChars)
2703 nLen = nChars;
2705 if (item->member.run.nFlags & MERF_ENDPARA)
2707 if (!ME_FindItemFwd(item, diRun))
2708 /* No '\r' is appended to the last paragraph. */
2709 nLen = 0;
2710 else {
2711 *buffer = '\r';
2712 if (bCRLF)
2714 *(++buffer) = '\n';
2715 nWritten++;
2717 assert(nLen == 1);
2718 /* our end paragraph consists of 2 characters now */
2719 if (editor->bEmulateVersion10)
2720 nChars--;
2723 else
2724 CopyMemory(buffer, item->member.run.strText->szData, sizeof(WCHAR)*nLen);
2725 nChars -= nLen;
2726 nWritten += nLen;
2727 buffer += nLen;
2729 if (!nChars)
2731 TRACE("nWritten=%d, actual=%d\n", nWritten, buffer-pStart);
2732 *buffer = 0;
2733 return nWritten;
2735 item = ME_FindItemFwd(item, diRun);
2737 *buffer = 0;
2738 TRACE("nWritten=%d, actual=%d\n", nWritten, buffer-pStart);
2739 return nWritten;
2742 static BOOL ME_RegisterEditorClass(HINSTANCE hInstance)
2744 WNDCLASSW wcW;
2745 WNDCLASSA wcA;
2747 wcW.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
2748 wcW.lpfnWndProc = RichEditWndProcW;
2749 wcW.cbClsExtra = 0;
2750 wcW.cbWndExtra = sizeof(ME_TextEditor *);
2751 wcW.hInstance = NULL; /* hInstance would register DLL-local class */
2752 wcW.hIcon = NULL;
2753 wcW.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
2754 wcW.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
2755 wcW.lpszMenuName = NULL;
2757 if (is_version_nt())
2759 wcW.lpszClassName = RichEdit20W;
2760 if (!RegisterClassW(&wcW)) return FALSE;
2761 wcW.lpszClassName = RichEdit50W;
2762 if (!RegisterClassW(&wcW)) return FALSE;
2764 else
2766 /* WNDCLASSA/W have the same layout */
2767 wcW.lpszClassName = (LPCWSTR)"RichEdit20W";
2768 if (!RegisterClassA((WNDCLASSA *)&wcW)) return FALSE;
2769 wcW.lpszClassName = (LPCWSTR)"RichEdit50W";
2770 if (!RegisterClassA((WNDCLASSA *)&wcW)) return FALSE;
2773 wcA.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
2774 wcA.lpfnWndProc = RichEditWndProcA;
2775 wcA.cbClsExtra = 0;
2776 wcA.cbWndExtra = sizeof(ME_TextEditor *);
2777 wcA.hInstance = NULL; /* hInstance would register DLL-local class */
2778 wcA.hIcon = NULL;
2779 wcA.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
2780 wcA.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
2781 wcA.lpszMenuName = NULL;
2782 wcA.lpszClassName = "RichEdit20A";
2783 if (!RegisterClassA(&wcA)) return FALSE;
2784 wcA.lpszClassName = "RichEdit50A";
2785 if (!RegisterClassA(&wcA)) return FALSE;
2787 return TRUE;
2790 LRESULT WINAPI REComboWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
2791 /* FIXME: Not implemented */
2792 TRACE("hWnd %p msg %04x (%s) %08x %08lx\n",
2793 hWnd, msg, get_msg_name(msg), wParam, lParam);
2794 return DefWindowProcW(hWnd, msg, wParam, lParam);
2797 LRESULT WINAPI REListWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
2798 /* FIXME: Not implemented */
2799 TRACE("hWnd %p msg %04x (%s) %08x %08lx\n",
2800 hWnd, msg, get_msg_name(msg), wParam, lParam);
2801 return DefWindowProcW(hWnd, msg, wParam, lParam);
2804 /******************************************************************
2805 * REExtendedRegisterClass (RICHED20.8)
2807 * FIXME undocumented
2808 * Need to check for errors and implement controls and callbacks
2810 LRESULT WINAPI REExtendedRegisterClass(void)
2812 WNDCLASSW wcW;
2813 UINT result;
2815 FIXME("semi stub\n");
2817 wcW.cbClsExtra = 0;
2818 wcW.cbWndExtra = 4;
2819 wcW.hInstance = NULL;
2820 wcW.hIcon = NULL;
2821 wcW.hCursor = NULL;
2822 wcW.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
2823 wcW.lpszMenuName = NULL;
2825 if (!ME_ListBoxRegistered)
2827 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS;
2828 wcW.lpfnWndProc = REListWndProc;
2829 wcW.lpszClassName = REListBox20W;
2830 if (RegisterClassW(&wcW)) ME_ListBoxRegistered = TRUE;
2833 if (!ME_ComboBoxRegistered)
2835 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
2836 wcW.lpfnWndProc = REComboWndProc;
2837 wcW.lpszClassName = REComboBox20W;
2838 if (RegisterClassW(&wcW)) ME_ComboBoxRegistered = TRUE;
2841 result = 0;
2842 if (ME_ListBoxRegistered)
2843 result += 1;
2844 if (ME_ComboBoxRegistered)
2845 result += 2;
2847 return result;
2850 int ME_AutoURLDetect(ME_TextEditor *editor, WCHAR curChar)
2852 struct prefix_s {
2853 const char *text;
2854 int length;
2855 } prefixes[12] = {
2856 {"http:", 5},
2857 {"file:", 6},
2858 {"mailto:", 8},
2859 {"ftp:", 5},
2860 {"https:", 7},
2861 {"gopher:", 8},
2862 {"nntp:", 6},
2863 {"prospero:", 10},
2864 {"telnet:", 8},
2865 {"news:", 6},
2866 {"wais:", 6},
2867 {"www.", 5}
2869 CHARRANGE ins_pt;
2870 int curf_ef, link_ef, def_ef;
2871 int cur_prefx, prefx_cnt;
2872 int sel_min, sel_max;
2873 int car_pos = 0;
2874 int text_pos=-1;
2875 int URLmin, URLmax = 0;
2876 CHARRANGE url;
2877 FINDTEXTA ft;
2878 CHARFORMAT2W cur_format;
2879 CHARFORMAT2W default_format;
2880 CHARFORMAT2W link;
2881 RichEditANSIWndProc(editor->hWnd, EM_EXGETSEL, (WPARAM) 0, (LPARAM) &ins_pt);
2882 sel_min = ins_pt.cpMin;
2883 sel_max = ins_pt.cpMax;
2884 if (sel_min==sel_max)
2885 car_pos = sel_min;
2886 if (sel_min!=sel_max)
2887 car_pos = ME_GetTextLength(editor)+1;
2888 cur_format.cbSize = sizeof(cur_format);
2889 default_format.cbSize = sizeof(default_format);
2890 RichEditANSIWndProc(editor->hWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM) &cur_format);
2891 RichEditANSIWndProc(editor->hWnd, EM_GETCHARFORMAT, SCF_DEFAULT, (LPARAM) &default_format);
2892 link.cbSize = sizeof(link);
2893 link.dwMask = CFM_LINK;
2894 link.dwEffects = CFE_LINK;
2895 curf_ef = cur_format.dwEffects & link.dwEffects;
2896 def_ef = default_format.dwEffects & link.dwEffects;
2897 link_ef = link.dwEffects & link.dwEffects;
2898 if (curf_ef == link_ef)
2900 if( curChar == '\n' || curChar=='\r' || curChar==' ')
2902 ME_SetSelection(editor, car_pos, car_pos);
2903 RichEditANSIWndProc(editor->hWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &default_format);
2904 text_pos=-1;
2905 return 0;
2908 if (curf_ef == def_ef)
2910 cur_prefx = 0;
2911 prefx_cnt = (sizeof(prefixes)/sizeof(struct prefix_s))-1;
2912 while (cur_prefx<=prefx_cnt)
2914 if (text_pos == -1)
2916 ft.lpstrText = prefixes[cur_prefx].text;
2917 URLmin=max(0,(car_pos-prefixes[cur_prefx].length));
2918 URLmax=max(0, car_pos);
2919 if ((car_pos == 0) && (ME_GetTextLength(editor) != 0))
2921 URLmax = ME_GetTextLength(editor)+1;
2923 ft.chrg.cpMin = URLmin;
2924 ft.chrg.cpMax = URLmax;
2925 text_pos=RichEditANSIWndProc(editor->hWnd, EM_FINDTEXT, FR_DOWN, (LPARAM)&ft);
2926 cur_prefx++;
2928 if (text_pos != -1)
2930 url.cpMin=text_pos;
2931 url.cpMax=car_pos-1;
2932 ME_SetCharFormat(editor, text_pos, (URLmax-text_pos), &link);
2933 ME_RewrapRepaint(editor);
2934 break;
2938 return 0;