Release 0.9.39.
[wine/gsoc-2012-control.git] / dlls / riched20 / editor.c
blobb83fb415d1cd01a49d1bae5e7e98b3193448ec7e
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 (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 "rtf.h"
231 #define STACK_SIZE_DEFAULT 100
232 #define STACK_SIZE_MAX 1000
234 #define TEXT_LIMIT_DEFAULT 32767
236 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
238 static BOOL ME_RegisterEditorClass(HINSTANCE);
240 static const WCHAR RichEdit20W[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '2', '0', 'W', 0};
241 static const WCHAR RichEdit50W[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '5', '0', 'W', 0};
242 static const WCHAR REListBox20W[] = {'R','E','L','i','s','t','B','o','x','2','0','W', 0};
243 static const WCHAR REComboBox20W[] = {'R','E','C','o','m','b','o','B','o','x','2','0','W', 0};
245 int me_debug = 0;
246 HANDLE me_heap = NULL;
248 static BOOL ME_ListBoxRegistered = FALSE;
249 static BOOL ME_ComboBoxRegistered = FALSE;
251 static inline int is_version_nt(void)
253 return !(GetVersion() & 0x80000000);
256 static ME_TextBuffer *ME_MakeText(void) {
258 ME_TextBuffer *buf = ALLOC_OBJ(ME_TextBuffer);
260 ME_DisplayItem *p1 = ME_MakeDI(diTextStart);
261 ME_DisplayItem *p2 = ME_MakeDI(diTextEnd);
263 p1->prev = NULL;
264 p1->next = p2;
265 p2->prev = p1;
266 p2->next = NULL;
267 p1->member.para.next_para = p2;
268 p2->member.para.prev_para = p1;
269 p2->member.para.nCharOfs = 0;
271 buf->pFirst = p1;
272 buf->pLast = p2;
273 buf->pCharStyle = NULL;
275 return buf;
279 static LRESULT ME_StreamInText(ME_TextEditor *editor, DWORD dwFormat, ME_InStream *stream, ME_Style *style)
281 WCHAR wszText[STREAMIN_BUFFER_SIZE+1];
282 WCHAR *pText;
284 TRACE("%08x %p\n", dwFormat, stream);
286 do {
287 long nWideChars = 0;
289 if (!stream->dwSize)
291 ME_StreamInFill(stream);
292 if (stream->editstream->dwError)
293 break;
294 if (!stream->dwSize)
295 break;
298 if (!(dwFormat & SF_UNICODE))
300 /* FIXME? this is doomed to fail on true MBCS like UTF-8, luckily they're unlikely to be used as CP_ACP */
301 nWideChars = MultiByteToWideChar(CP_ACP, 0, stream->buffer, stream->dwSize, wszText, STREAMIN_BUFFER_SIZE);
302 pText = wszText;
304 else
306 nWideChars = stream->dwSize >> 1;
307 pText = (WCHAR *)stream->buffer;
310 ME_InsertTextFromCursor(editor, 0, pText, nWideChars, style);
311 if (stream->dwSize == 0)
312 break;
313 stream->dwSize = 0;
314 } while(1);
315 ME_CommitUndo(editor);
316 ME_UpdateRepaint(editor);
317 return 0;
320 static void ME_RTFCharAttrHook(RTF_Info *info)
322 CHARFORMAT2W fmt;
323 fmt.cbSize = sizeof(fmt);
324 fmt.dwMask = 0;
325 fmt.dwEffects = 0;
327 switch(info->rtfMinor)
329 case rtfPlain:
330 /* FIXME add more flags once they're implemented */
331 fmt.dwMask = CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_COLOR | CFM_BACKCOLOR | CFM_SIZE | CFM_WEIGHT;
332 fmt.dwEffects = CFE_AUTOCOLOR | CFE_AUTOBACKCOLOR;
333 fmt.yHeight = 12*20; /* 12pt */
334 fmt.wWeight = 400;
335 break;
336 case rtfBold:
337 fmt.dwMask = CFM_BOLD;
338 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
339 break;
340 case rtfItalic:
341 fmt.dwMask = CFM_ITALIC;
342 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
343 break;
344 case rtfUnderline:
345 fmt.dwMask = CFM_UNDERLINE;
346 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
347 fmt.bUnderlineType = CFU_CF1UNDERLINE;
348 break;
349 case rtfNoUnderline:
350 fmt.dwMask = CFM_UNDERLINE;
351 fmt.dwEffects = 0;
352 break;
353 case rtfStrikeThru:
354 fmt.dwMask = CFM_STRIKEOUT;
355 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
356 break;
357 case rtfSubScript:
358 case rtfSuperScript:
359 case rtfSubScrShrink:
360 case rtfSuperScrShrink:
361 case rtfNoSuperSub:
362 fmt.dwMask = CFM_SUBSCRIPT|CFM_SUPERSCRIPT;
363 if (info->rtfMinor == rtfSubScrShrink) fmt.dwEffects = CFE_SUBSCRIPT;
364 if (info->rtfMinor == rtfSuperScrShrink) fmt.dwEffects = CFE_SUPERSCRIPT;
365 if (info->rtfMinor == rtfNoSuperSub) fmt.dwEffects = 0;
366 break;
367 case rtfInvisible:
368 fmt.dwMask = CFM_HIDDEN;
369 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
370 break;
371 case rtfBackColor:
372 fmt.dwMask = CFM_BACKCOLOR;
373 fmt.dwEffects = 0;
374 if (info->rtfParam == 0)
375 fmt.dwEffects = CFE_AUTOBACKCOLOR;
376 else if (info->rtfParam != rtfNoParam)
378 RTFColor *c = RTFGetColor(info, info->rtfParam);
379 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
381 break;
382 case rtfForeColor:
383 fmt.dwMask = CFM_COLOR;
384 fmt.dwEffects = 0;
385 if (info->rtfParam == 0)
386 fmt.dwEffects = CFE_AUTOCOLOR;
387 else if (info->rtfParam != rtfNoParam)
389 RTFColor *c = RTFGetColor(info, info->rtfParam);
390 if (c)
391 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
392 else
393 fmt.crTextColor = 0;
395 break;
396 case rtfFontNum:
397 if (info->rtfParam != rtfNoParam)
399 RTFFont *f = RTFGetFont(info, info->rtfParam);
400 if (f)
402 MultiByteToWideChar(CP_ACP, 0, f->rtfFName, -1, fmt.szFaceName, sizeof(fmt.szFaceName)/sizeof(WCHAR));
403 fmt.szFaceName[sizeof(fmt.szFaceName)/sizeof(WCHAR)-1] = '\0';
404 fmt.bCharSet = f->rtfFCharSet;
405 fmt.dwMask = CFM_FACE | CFM_CHARSET;
408 break;
409 case rtfFontSize:
410 fmt.dwMask = CFM_SIZE;
411 if (info->rtfParam != rtfNoParam)
412 fmt.yHeight = info->rtfParam*10;
413 break;
415 if (fmt.dwMask) {
416 ME_Style *style2;
417 RTFFlushOutputBuffer(info);
418 /* FIXME too slow ? how come ? */
419 style2 = ME_ApplyStyle(info->style, &fmt);
420 ME_ReleaseStyle(info->style);
421 info->style = style2;
422 info->styleChanged = TRUE;
426 /* FIXME this function doesn't get any information about context of the RTF tag, which is very bad,
427 the same tags mean different things in different contexts */
428 static void ME_RTFParAttrHook(RTF_Info *info)
430 PARAFORMAT2 fmt;
431 fmt.cbSize = sizeof(fmt);
432 fmt.dwMask = 0;
434 switch(info->rtfMinor)
436 case rtfParDef: /* restores default paragraph attributes */
437 fmt.dwMask = PFM_ALIGNMENT | PFM_TABSTOPS | PFM_OFFSET | PFM_STARTINDENT;
438 fmt.wAlignment = PFA_LEFT;
439 fmt.cTabCount = 0;
440 fmt.dxOffset = fmt.dxStartIndent = 0;
441 RTFFlushOutputBuffer(info);
442 ME_GetParagraph(info->editor->pCursors[0].pRun)->member.para.bTable = FALSE;
443 break;
444 case rtfInTable:
446 ME_DisplayItem *para;
448 RTFFlushOutputBuffer(info);
449 para = ME_GetParagraph(info->editor->pCursors[0].pRun);
450 assert(para->member.para.pCells);
451 para->member.para.bTable = TRUE;
452 return;
454 case rtfFirstIndent:
455 ME_GetSelectionParaFormat(info->editor, &fmt);
456 fmt.dwMask = PFM_STARTINDENT | PFM_OFFSET;
457 fmt.dxStartIndent += info->rtfParam + fmt.dxOffset;
458 fmt.dxOffset = -info->rtfParam;
459 break;
460 case rtfLeftIndent:
461 ME_GetSelectionParaFormat(info->editor, &fmt);
462 fmt.dwMask = PFM_STARTINDENT;
463 fmt.dxStartIndent = -fmt.dxOffset + info->rtfParam;
464 break;
465 case rtfRightIndent:
466 fmt.dwMask = PFM_RIGHTINDENT;
467 fmt.dxRightIndent = info->rtfParam;
468 break;
469 case rtfQuadLeft:
470 case rtfQuadJust:
471 fmt.dwMask = PFM_ALIGNMENT;
472 fmt.wAlignment = PFA_LEFT;
473 break;
474 case rtfQuadRight:
475 fmt.dwMask = PFM_ALIGNMENT;
476 fmt.wAlignment = PFA_RIGHT;
477 break;
478 case rtfQuadCenter:
479 fmt.dwMask = PFM_ALIGNMENT;
480 fmt.wAlignment = PFA_CENTER;
481 break;
482 case rtfTabPos:
483 ME_GetSelectionParaFormat(info->editor, &fmt);
484 if (!(fmt.dwMask & PFM_TABSTOPS))
486 fmt.dwMask |= PFM_TABSTOPS;
487 fmt.cTabCount = 0;
489 if (fmt.cTabCount < MAX_TAB_STOPS)
490 fmt.rgxTabs[fmt.cTabCount++] = info->rtfParam;
491 break;
493 if (fmt.dwMask) {
494 RTFFlushOutputBuffer(info);
495 /* FIXME too slow ? how come ?*/
496 ME_SetSelectionParaFormat(info->editor, &fmt);
500 static void ME_RTFTblAttrHook(RTF_Info *info)
502 ME_DisplayItem *para;
504 switch (info->rtfMinor)
506 case rtfRowDef:
507 RTFFlushOutputBuffer(info);
508 para = ME_GetParagraph(info->editor->pCursors[0].pRun);
510 /* Release possibly inherited cell definitions */
511 ME_DestroyTableCellList(para);
513 para->member.para.pCells = ALLOC_OBJ(ME_TableCell);
514 para->member.para.pCells->nRightBoundary = 0;
515 para->member.para.pCells->next = NULL;
516 para->member.para.pLastCell = para->member.para.pCells;
517 break;
518 case rtfCellPos:
519 RTFFlushOutputBuffer(info);
520 para = ME_GetParagraph(info->editor->pCursors[0].pRun);
522 if (para->member.para.pLastCell->nRightBoundary)
524 ME_TableCell *pCell = ALLOC_OBJ(ME_TableCell);
526 pCell->next = NULL;
527 para->member.para.pLastCell->next = pCell;
528 para->member.para.pLastCell = pCell;
530 para->member.para.pLastCell->nRightBoundary = info->rtfParam;
531 break;
535 static void ME_RTFReadHook(RTF_Info *info) {
536 switch(info->rtfClass)
538 case rtfGroup:
539 switch(info->rtfMajor)
541 case rtfBeginGroup:
542 if (info->stackTop < maxStack) {
543 memcpy(&info->stack[info->stackTop].fmt, &info->style->fmt, sizeof(CHARFORMAT2W));
544 info->stack[info->stackTop].codePage = info->codePage;
545 info->stack[info->stackTop].unicodeLength = info->unicodeLength;
547 info->stackTop++;
548 info->styleChanged = FALSE;
549 break;
550 case rtfEndGroup:
552 ME_Style *s;
553 RTFFlushOutputBuffer(info);
554 if (info->stackTop<=1) {
555 info->rtfClass = rtfEOF;
556 return;
558 info->stackTop--;
559 assert(info->stackTop >= 0);
560 if (info->styleChanged)
562 /* FIXME too slow ? how come ? */
563 s = ME_ApplyStyle(info->style, &info->stack[info->stackTop].fmt);
564 ME_ReleaseStyle(info->style);
565 info->style = s;
566 info->codePage = info->stack[info->stackTop].codePage;
567 info->unicodeLength = info->stack[info->stackTop].unicodeLength;
569 break;
572 break;
573 case rtfControl:
574 switch(info->rtfMajor)
576 case rtfCharAttr:
577 ME_RTFCharAttrHook(info);
578 break;
579 case rtfParAttr:
580 ME_RTFParAttrHook(info);
581 break;
582 case rtfTblAttr:
583 ME_RTFTblAttrHook(info);
584 break;
585 case rtfSpecialChar:
586 if (info->rtfMinor == rtfCell)
588 RTFFlushOutputBuffer(info);
589 ME_InsertTableCellFromCursor(info->editor, 0);
592 break;
596 void
597 ME_StreamInFill(ME_InStream *stream)
599 stream->editstream->dwError = stream->editstream->pfnCallback(stream->editstream->dwCookie,
600 (BYTE *)stream->buffer,
601 sizeof(stream->buffer),
602 (LONG *)&stream->dwSize);
603 stream->dwUsed = 0;
606 static LRESULT ME_StreamIn(ME_TextEditor *editor, DWORD format, EDITSTREAM *stream)
608 RTF_Info parser;
609 ME_Style *style;
610 int from, to, to2, nUndoMode;
611 int nEventMask = editor->nEventMask;
612 ME_InStream inStream;
614 TRACE("stream==%p hWnd==%p format==0x%X\n", stream, editor->hWnd, (UINT)format);
615 editor->nEventMask = 0;
617 ME_GetSelection(editor, &from, &to);
618 if ((format & SFF_SELECTION) && (editor->mode & TM_RICHTEXT)) {
619 style = ME_GetSelectionInsertStyle(editor);
621 ME_InternalDeleteText(editor, from, to-from);
623 else {
624 style = editor->pBuffer->pDefaultStyle;
625 ME_AddRefStyle(style);
626 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
627 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
628 from = to = 0;
629 ME_ClearTempStyle(editor);
630 /* FIXME restore default paragraph formatting ! */
634 /* Back up undo mode to a local variable */
635 nUndoMode = editor->nUndoMode;
637 /* Only create an undo if SFF_SELECTION is set */
638 if (!(format & SFF_SELECTION))
639 editor->nUndoMode = umIgnore;
641 inStream.editstream = stream;
642 inStream.editstream->dwError = 0;
643 inStream.dwSize = 0;
644 inStream.dwUsed = 0;
646 if (format & SF_RTF)
648 /* Check if it's really RTF, and if it is not, use plain text */
649 ME_StreamInFill(&inStream);
650 if (!inStream.editstream->dwError)
652 if (strncmp(inStream.buffer, "{\\rtf", 5) && strncmp(inStream.buffer, "{\\urtf", 6))
654 format &= ~SF_RTF;
655 format |= SF_TEXT;
660 if (!inStream.editstream->dwError)
662 if (format & SF_RTF) {
663 /* setup the RTF parser */
664 memset(&parser, 0, sizeof parser);
665 RTFSetEditStream(&parser, &inStream);
666 parser.rtfFormat = format&(SF_TEXT|SF_RTF);
667 parser.hwndEdit = editor->hWnd;
668 parser.editor = editor;
669 parser.style = style;
670 WriterInit(&parser);
671 RTFInit(&parser);
672 RTFSetReadHook(&parser, ME_RTFReadHook);
673 BeginFile(&parser);
675 /* do the parsing */
676 RTFRead(&parser);
677 RTFFlushOutputBuffer(&parser);
678 RTFDestroy(&parser);
680 style = parser.style;
682 else if (format & SF_TEXT)
683 ME_StreamInText(editor, format, &inStream, style);
684 else
685 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
686 ME_GetSelection(editor, &to, &to2);
687 /* put the cursor at the top */
688 if (!(format & SFF_SELECTION))
689 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
692 /* Restore saved undo mode */
693 editor->nUndoMode = nUndoMode;
695 /* even if we didn't add an undo, we need to commit anything on the stack */
696 ME_CommitUndo(editor);
698 /* If SFF_SELECTION isn't set, delete any undos from before we started too */
699 if (!(format & SFF_SELECTION))
700 ME_EmptyUndoStack(editor);
702 ME_ReleaseStyle(style);
703 editor->nEventMask = nEventMask;
704 if (editor->bRedraw)
706 ME_UpdateRepaint(editor);
708 if (!(format & SFF_SELECTION)) {
709 ME_ClearTempStyle(editor);
711 ME_MoveCaret(editor);
712 ME_SendSelChange(editor);
713 ME_SendRequestResize(editor, FALSE);
715 return 0;
719 typedef struct tagME_RTFStringStreamStruct
721 char *string;
722 int pos;
723 int length;
724 } ME_RTFStringStreamStruct;
726 static DWORD CALLBACK ME_ReadFromRTFString(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
728 ME_RTFStringStreamStruct *pStruct = (ME_RTFStringStreamStruct *)dwCookie;
729 int count;
731 count = min(cb, pStruct->length - pStruct->pos);
732 memmove(lpBuff, pStruct->string + pStruct->pos, count);
733 pStruct->pos += count;
734 *pcb = count;
735 return 0;
738 static void
739 ME_StreamInRTFString(ME_TextEditor *editor, BOOL selection, char *string)
741 EDITSTREAM es;
742 ME_RTFStringStreamStruct data;
744 data.string = string;
745 data.length = strlen(string);
746 data.pos = 0;
747 es.dwCookie = (DWORD)&data;
748 es.pfnCallback = ME_ReadFromRTFString;
749 ME_StreamIn(editor, SF_RTF | (selection ? SFF_SELECTION : 0), &es);
753 ME_DisplayItem *
754 ME_FindItemAtOffset(ME_TextEditor *editor, ME_DIType nItemType, int nOffset, int *nItemOffset)
756 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
758 while (item && item->member.para.next_para->member.para.nCharOfs <= nOffset)
759 item = ME_FindItemFwd(item, diParagraph);
761 if (!item)
762 return item;
764 nOffset -= item->member.para.nCharOfs;
765 if (nItemType == diParagraph) {
766 if (nItemOffset)
767 *nItemOffset = nOffset;
768 return item;
771 do {
772 item = ME_FindItemFwd(item, diRun);
773 } while (item && (item->member.run.nCharOfs + ME_StrLen(item->member.run.strText) <= nOffset));
774 if (item) {
775 nOffset -= item->member.run.nCharOfs;
776 if (nItemOffset)
777 *nItemOffset = nOffset;
779 return item;
783 static int
784 ME_FindText(ME_TextEditor *editor, DWORD flags, CHARRANGE *chrg, const WCHAR *text, CHARRANGE *chrgText)
786 const int nLen = lstrlenW(text);
787 const int nTextLen = ME_GetTextLength(editor);
788 int nStart, nEnd;
789 int nMin, nMax;
790 ME_DisplayItem *item;
791 ME_DisplayItem *para;
792 WCHAR wLastChar = ' ';
794 TRACE("flags==0x%08x, chrg->cpMin==%d, chrg->cpMax==%d text==%s\n",
795 flags, chrg->cpMin, chrg->cpMax, debugstr_w(text));
797 if (flags & ~(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD))
798 FIXME("Flags 0x%08x not implemented\n",
799 flags & ~(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD));
801 nMin = chrg->cpMin;
802 if (chrg->cpMax == -1)
803 nMax = nTextLen;
804 else
805 nMax = chrg->cpMax > nTextLen ? nTextLen : chrg->cpMax;
807 /* when searching up, if cpMin < cpMax, then instead of searching
808 * on [cpMin,cpMax], we search on [0,cpMin], otherwise, search on
809 * [cpMax, cpMin]. The exception is when cpMax is -1, in which
810 * case, it is always bigger than cpMin.
812 if (!(flags & FR_DOWN))
814 int nSwap = nMax;
816 nMax = nMin > nTextLen ? nTextLen : nMin;
817 if (nMin < nSwap || chrg->cpMax == -1)
818 nMin = 0;
819 else
820 nMin = nSwap;
823 if (!nLen || nMin < 0 || nMax < 0 || nMax < nMin)
825 if (chrgText)
826 chrgText->cpMin = chrgText->cpMax = -1;
827 return -1;
830 if (flags & FR_DOWN) /* Forward search */
832 /* If possible, find the character before where the search starts */
833 if ((flags & FR_WHOLEWORD) && nMin)
835 nStart = nMin - 1;
836 item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
837 if (!item)
839 if (chrgText)
840 chrgText->cpMin = chrgText->cpMax = -1;
841 return -1;
843 wLastChar = item->member.run.strText->szData[nStart];
846 nStart = nMin;
847 item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
848 if (!item)
850 if (chrgText)
851 chrgText->cpMin = chrgText->cpMax = -1;
852 return -1;
855 para = ME_GetParagraph(item);
856 while (item
857 && para->member.para.nCharOfs + item->member.run.nCharOfs + nStart + nLen <= nMax)
859 ME_DisplayItem *pCurItem = item;
860 int nCurStart = nStart;
861 int nMatched = 0;
863 while (pCurItem && ME_CharCompare(pCurItem->member.run.strText->szData[nCurStart + nMatched], text[nMatched], (flags & FR_MATCHCASE)))
865 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
866 break;
868 nMatched++;
869 if (nMatched == nLen)
871 ME_DisplayItem *pNextItem = pCurItem;
872 int nNextStart = nCurStart;
873 WCHAR wNextChar;
875 /* Check to see if next character is a whitespace */
876 if (flags & FR_WHOLEWORD)
878 if (nCurStart + nMatched == ME_StrLen(pCurItem->member.run.strText))
880 pNextItem = ME_FindItemFwd(pCurItem, diRun);
881 nNextStart = -nMatched;
884 if (pNextItem)
885 wNextChar = pNextItem->member.run.strText->szData[nNextStart + nMatched];
886 else
887 wNextChar = ' ';
889 if (isalnumW(wNextChar))
890 break;
893 nStart += para->member.para.nCharOfs + pCurItem->member.run.nCharOfs;
894 if (chrgText)
896 chrgText->cpMin = nStart;
897 chrgText->cpMax = nStart + nLen;
899 TRACE("found at %d-%d\n", nStart, nStart + nLen);
900 return nStart;
902 if (nCurStart + nMatched == ME_StrLen(pCurItem->member.run.strText))
904 pCurItem = ME_FindItemFwd(pCurItem, diRun);
905 para = ME_GetParagraph(pCurItem);
906 nCurStart = -nMatched;
909 if (pCurItem)
910 wLastChar = pCurItem->member.run.strText->szData[nCurStart + nMatched];
911 else
912 wLastChar = ' ';
914 nStart++;
915 if (nStart == ME_StrLen(item->member.run.strText))
917 item = ME_FindItemFwd(item, diRun);
918 para = ME_GetParagraph(item);
919 nStart = 0;
923 else /* Backward search */
925 /* If possible, find the character after where the search ends */
926 if ((flags & FR_WHOLEWORD) && nMax < nTextLen - 1)
928 nEnd = nMax + 1;
929 item = ME_FindItemAtOffset(editor, diRun, nEnd, &nEnd);
930 if (!item)
932 if (chrgText)
933 chrgText->cpMin = chrgText->cpMax = -1;
934 return -1;
936 wLastChar = item->member.run.strText->szData[nEnd];
939 nEnd = nMax;
940 item = ME_FindItemAtOffset(editor, diRun, nEnd, &nEnd);
941 if (!item)
943 if (chrgText)
944 chrgText->cpMin = chrgText->cpMax = -1;
945 return -1;
948 para = ME_GetParagraph(item);
950 while (item
951 && para->member.para.nCharOfs + item->member.run.nCharOfs + nEnd - nLen >= nMin)
953 ME_DisplayItem *pCurItem = item;
954 int nCurEnd = nEnd;
955 int nMatched = 0;
957 if (nCurEnd - nMatched == 0)
959 pCurItem = ME_FindItemBack(pCurItem, diRun);
960 para = ME_GetParagraph(pCurItem);
961 nCurEnd = ME_StrLen(pCurItem->member.run.strText) + nMatched;
964 while (pCurItem && ME_CharCompare(pCurItem->member.run.strText->szData[nCurEnd - nMatched - 1], text[nLen - nMatched - 1], (flags & FR_MATCHCASE)))
966 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
967 break;
969 nMatched++;
970 if (nMatched == nLen)
972 ME_DisplayItem *pPrevItem = pCurItem;
973 int nPrevEnd = nCurEnd;
974 WCHAR wPrevChar;
976 /* Check to see if previous character is a whitespace */
977 if (flags & FR_WHOLEWORD)
979 if (nPrevEnd - nMatched == 0)
981 pPrevItem = ME_FindItemBack(pCurItem, diRun);
982 if (pPrevItem)
983 nPrevEnd = ME_StrLen(pPrevItem->member.run.strText) + nMatched;
986 if (pPrevItem)
987 wPrevChar = pPrevItem->member.run.strText->szData[nPrevEnd - nMatched - 1];
988 else
989 wPrevChar = ' ';
991 if (isalnumW(wPrevChar))
992 break;
995 nStart = para->member.para.nCharOfs + pCurItem->member.run.nCharOfs + nCurEnd - nMatched;
996 if (chrgText)
998 chrgText->cpMin = nStart;
999 chrgText->cpMax = nStart + nLen;
1001 TRACE("found at %d-%d\n", nStart, nStart + nLen);
1002 return nStart;
1004 if (nCurEnd - nMatched == 0)
1006 pCurItem = ME_FindItemBack(pCurItem, diRun);
1007 /* Don't care about pCurItem becoming NULL here; it's already taken
1008 * care of in the exterior loop condition */
1009 para = ME_GetParagraph(pCurItem);
1010 nCurEnd = ME_StrLen(pCurItem->member.run.strText) + nMatched;
1013 if (pCurItem)
1014 wLastChar = pCurItem->member.run.strText->szData[nCurEnd - nMatched - 1];
1015 else
1016 wLastChar = ' ';
1018 nEnd--;
1019 if (nEnd < 0)
1021 item = ME_FindItemBack(item, diRun);
1022 para = ME_GetParagraph(item);
1023 nEnd = ME_StrLen(item->member.run.strText);
1027 TRACE("not found\n");
1028 if (chrgText)
1029 chrgText->cpMin = chrgText->cpMax = -1;
1030 return -1;
1034 static BOOL
1035 ME_KeyDown(ME_TextEditor *editor, WORD nKey)
1037 BOOL ctrl_is_down = GetKeyState(VK_CONTROL) & 0x8000;
1038 BOOL shift_is_down = GetKeyState(VK_SHIFT) & 0x8000;
1040 switch (nKey)
1042 case VK_LEFT:
1043 case VK_RIGHT:
1044 case VK_UP:
1045 case VK_DOWN:
1046 case VK_HOME:
1047 case VK_END:
1048 case VK_PRIOR:
1049 case VK_NEXT:
1050 ME_ArrowKey(editor, nKey, shift_is_down, ctrl_is_down);
1051 return TRUE;
1052 case VK_BACK:
1053 case VK_DELETE:
1054 /* FIXME backspace and delete aren't the same, they act different wrt paragraph style of the merged paragraph */
1055 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY)
1056 return FALSE;
1057 if (ME_IsSelection(editor))
1058 ME_DeleteSelection(editor);
1059 else if (nKey == VK_DELETE || ME_ArrowKey(editor, VK_LEFT, FALSE, FALSE))
1060 ME_DeleteTextAtCursor(editor, 1, 1);
1061 else
1062 return TRUE;
1063 ME_CommitUndo(editor);
1064 ME_UpdateRepaint(editor);
1065 ME_SendRequestResize(editor, FALSE);
1066 return TRUE;
1068 default:
1069 if (ctrl_is_down)
1071 if (nKey == 'W')
1073 CHARFORMAT2W chf;
1074 char buf[2048];
1075 chf.cbSize = sizeof(chf);
1077 ME_GetSelectionCharFormat(editor, &chf);
1078 ME_DumpStyleToBuf(&chf, buf);
1079 MessageBoxA(NULL, buf, "Style dump", MB_OK);
1081 if (nKey == 'Q')
1083 ME_CheckCharOffsets(editor);
1087 return FALSE;
1090 static BOOL ME_ShowContextMenu(ME_TextEditor *editor, int x, int y)
1092 CHARRANGE selrange;
1093 HMENU menu;
1094 int seltype = 0;
1095 if(!editor->lpOleCallback)
1096 return FALSE;
1097 ME_GetSelection(editor, (int *)&selrange.cpMin, (int *)&selrange.cpMax);
1098 if(selrange.cpMin == selrange.cpMax)
1099 seltype |= SEL_EMPTY;
1100 else
1102 /* FIXME: Handle objects */
1103 seltype |= SEL_TEXT;
1104 if(selrange.cpMax-selrange.cpMin > 1)
1105 seltype |= SEL_MULTICHAR;
1107 if(SUCCEEDED(IRichEditOleCallback_GetContextMenu(editor->lpOleCallback, seltype, NULL, &selrange, &menu)))
1109 TrackPopupMenu(menu, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, GetParent(editor->hWnd), NULL);
1110 DestroyMenu(menu);
1112 return TRUE;
1115 ME_TextEditor *ME_MakeEditor(HWND hWnd) {
1116 ME_TextEditor *ed = ALLOC_OBJ(ME_TextEditor);
1117 HDC hDC;
1118 int i;
1119 ed->hWnd = hWnd;
1120 ed->bEmulateVersion10 = FALSE;
1121 ed->pBuffer = ME_MakeText();
1122 hDC = GetDC(hWnd);
1123 ME_MakeFirstParagraph(hDC, ed->pBuffer);
1124 ReleaseDC(hWnd, hDC);
1125 ed->bCaretShown = FALSE;
1126 ed->nCursors = 2;
1127 ed->pCursors = ALLOC_N_OBJ(ME_Cursor, ed->nCursors);
1128 ed->pCursors[0].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
1129 ed->pCursors[0].nOffset = 0;
1130 ed->pCursors[1].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
1131 ed->pCursors[1].nOffset = 0;
1132 ed->nLastTotalLength = ed->nTotalLength = 0;
1133 ed->nUDArrowX = -1;
1134 ed->nSequence = 0;
1135 ed->rgbBackColor = -1;
1136 ed->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
1137 ed->bCaretAtEnd = FALSE;
1138 ed->nEventMask = 0;
1139 ed->nModifyStep = 0;
1140 ed->nTextLimit = TEXT_LIMIT_DEFAULT;
1141 ed->pUndoStack = ed->pRedoStack = ed->pUndoStackBottom = NULL;
1142 ed->nUndoStackSize = 0;
1143 ed->nUndoLimit = STACK_SIZE_DEFAULT;
1144 ed->nUndoMode = umAddToUndo;
1145 ed->nParagraphs = 1;
1146 ed->nLastSelStart = ed->nLastSelEnd = 0;
1147 ed->pLastSelStartPara = ed->pLastSelEndPara = ME_FindItemFwd(ed->pBuffer->pFirst, diParagraph);
1148 ed->nZoomNumerator = ed->nZoomDenominator = 0;
1149 ed->bRedraw = TRUE;
1150 ed->bHideSelection = FALSE;
1151 ed->nInvalidOfs = -1;
1152 ed->pfnWordBreak = NULL;
1153 ed->lpOleCallback = NULL;
1154 ed->mode = TM_RICHTEXT | TM_MULTILEVELUNDO | TM_MULTICODEPAGE;
1155 ed->AutoURLDetect_bEnable = FALSE;
1156 ed->bHaveFocus = FALSE;
1157 GetClientRect(hWnd, &ed->rcFormat);
1158 for (i=0; i<HFONT_CACHE_SIZE; i++)
1160 ed->pFontCache[i].nRefs = 0;
1161 ed->pFontCache[i].nAge = 0;
1162 ed->pFontCache[i].hFont = NULL;
1165 ME_CheckCharOffsets(ed);
1167 if (GetWindowLongW(hWnd, GWL_STYLE) & ES_PASSWORD)
1168 ed->cPasswordMask = '*';
1169 else
1170 ed->cPasswordMask = 0;
1172 return ed;
1175 typedef struct tagME_GlobalDestStruct
1177 HGLOBAL hData;
1178 int nLength;
1179 } ME_GlobalDestStruct;
1181 static DWORD CALLBACK ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1183 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
1184 int i;
1185 WORD *pSrc, *pDest;
1187 cb = cb >> 1;
1188 pDest = (WORD *)lpBuff;
1189 pSrc = (WORD *)GlobalLock(pData->hData);
1190 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
1191 pDest[i] = pSrc[pData->nLength+i];
1193 pData->nLength += i;
1194 *pcb = 2*i;
1195 GlobalUnlock(pData->hData);
1196 return 0;
1199 static DWORD CALLBACK ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1201 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
1202 int i;
1203 BYTE *pSrc, *pDest;
1205 pDest = lpBuff;
1206 pSrc = (BYTE *)GlobalLock(pData->hData);
1207 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
1208 pDest[i] = pSrc[pData->nLength+i];
1210 pData->nLength += i;
1211 *pcb = i;
1212 GlobalUnlock(pData->hData);
1213 return 0;
1217 void ME_DestroyEditor(ME_TextEditor *editor)
1219 ME_DisplayItem *pFirst = editor->pBuffer->pFirst;
1220 ME_DisplayItem *p = pFirst, *pNext = NULL;
1221 int i;
1223 ME_ClearTempStyle(editor);
1224 ME_EmptyUndoStack(editor);
1225 while(p) {
1226 pNext = p->next;
1227 ME_DestroyDisplayItem(p);
1228 p = pNext;
1230 ME_ReleaseStyle(editor->pBuffer->pDefaultStyle);
1231 for (i=0; i<HFONT_CACHE_SIZE; i++)
1233 if (editor->pFontCache[i].hFont)
1234 DeleteObject(editor->pFontCache[i].hFont);
1236 DeleteObject(editor->hbrBackground);
1237 if(editor->lpOleCallback)
1238 IUnknown_Release(editor->lpOleCallback);
1240 FREE_OBJ(editor->pBuffer);
1241 FREE_OBJ(editor->pCursors);
1243 FREE_OBJ(editor);
1246 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
1248 TRACE("\n");
1249 switch (fdwReason)
1251 case DLL_PROCESS_ATTACH:
1252 DisableThreadLibraryCalls(hinstDLL);
1253 me_heap = HeapCreate (0, 0x10000, 0);
1254 if (!ME_RegisterEditorClass(hinstDLL)) return FALSE;
1255 LookupInit();
1256 break;
1258 case DLL_PROCESS_DETACH:
1259 UnregisterClassW(RichEdit20W, 0);
1260 UnregisterClassW(RichEdit50W, 0);
1261 UnregisterClassA("RichEdit20A", 0);
1262 UnregisterClassA("RichEdit50A", 0);
1263 if (ME_ListBoxRegistered)
1264 UnregisterClassW(REListBox20W, 0);
1265 if (ME_ComboBoxRegistered)
1266 UnregisterClassW(REComboBox20W, 0);
1267 LookupCleanup();
1268 HeapDestroy (me_heap);
1269 me_heap = NULL;
1270 break;
1272 return TRUE;
1276 #define UNSUPPORTED_MSG(e) \
1277 case e: \
1278 FIXME(#e ": stub\n"); \
1279 return DefWindowProcW(hWnd, msg, wParam, lParam);
1281 static const char * const edit_messages[] = {
1282 "EM_GETSEL",
1283 "EM_SETSEL",
1284 "EM_GETRECT",
1285 "EM_SETRECT",
1286 "EM_SETRECTNP",
1287 "EM_SCROLL",
1288 "EM_LINESCROLL",
1289 "EM_SCROLLCARET",
1290 "EM_GETMODIFY",
1291 "EM_SETMODIFY",
1292 "EM_GETLINECOUNT",
1293 "EM_LINEINDEX",
1294 "EM_SETHANDLE",
1295 "EM_GETHANDLE",
1296 "EM_GETTHUMB",
1297 "EM_UNKNOWN_BF",
1298 "EM_UNKNOWN_C0",
1299 "EM_LINELENGTH",
1300 "EM_REPLACESEL",
1301 "EM_UNKNOWN_C3",
1302 "EM_GETLINE",
1303 "EM_LIMITTEXT",
1304 "EM_CANUNDO",
1305 "EM_UNDO",
1306 "EM_FMTLINES",
1307 "EM_LINEFROMCHAR",
1308 "EM_UNKNOWN_CA",
1309 "EM_SETTABSTOPS",
1310 "EM_SETPASSWORDCHAR",
1311 "EM_EMPTYUNDOBUFFER",
1312 "EM_GETFIRSTVISIBLELINE",
1313 "EM_SETREADONLY",
1314 "EM_SETWORDBREAKPROC",
1315 "EM_GETWORDBREAKPROC",
1316 "EM_GETPASSWORDCHAR",
1317 "EM_SETMARGINS",
1318 "EM_GETMARGINS",
1319 "EM_GETLIMITTEXT",
1320 "EM_POSFROMCHAR",
1321 "EM_CHARFROMPOS"
1324 static const char * const richedit_messages[] = {
1325 "EM_CANPASTE",
1326 "EM_DISPLAYBAND",
1327 "EM_EXGETSEL",
1328 "EM_EXLIMITTEXT",
1329 "EM_EXLINEFROMCHAR",
1330 "EM_EXSETSEL",
1331 "EM_FINDTEXT",
1332 "EM_FORMATRANGE",
1333 "EM_GETCHARFORMAT",
1334 "EM_GETEVENTMASK",
1335 "EM_GETOLEINTERFACE",
1336 "EM_GETPARAFORMAT",
1337 "EM_GETSELTEXT",
1338 "EM_HIDESELECTION",
1339 "EM_PASTESPECIAL",
1340 "EM_REQUESTRESIZE",
1341 "EM_SELECTIONTYPE",
1342 "EM_SETBKGNDCOLOR",
1343 "EM_SETCHARFORMAT",
1344 "EM_SETEVENTMASK",
1345 "EM_SETOLECALLBACK",
1346 "EM_SETPARAFORMAT",
1347 "EM_SETTARGETDEVICE",
1348 "EM_STREAMIN",
1349 "EM_STREAMOUT",
1350 "EM_GETTEXTRANGE",
1351 "EM_FINDWORDBREAK",
1352 "EM_SETOPTIONS",
1353 "EM_GETOPTIONS",
1354 "EM_FINDTEXTEX",
1355 "EM_GETWORDBREAKPROCEX",
1356 "EM_SETWORDBREAKPROCEX",
1357 "EM_SETUNDOLIMIT",
1358 "EM_UNKNOWN_USER_83",
1359 "EM_REDO",
1360 "EM_CANREDO",
1361 "EM_GETUNDONAME",
1362 "EM_GETREDONAME",
1363 "EM_STOPGROUPTYPING",
1364 "EM_SETTEXTMODE",
1365 "EM_GETTEXTMODE",
1366 "EM_AUTOURLDETECT",
1367 "EM_GETAUTOURLDETECT",
1368 "EM_SETPALETTE",
1369 "EM_GETTEXTEX",
1370 "EM_GETTEXTLENGTHEX",
1371 "EM_SHOWSCROLLBAR",
1372 "EM_SETTEXTEX",
1373 "EM_UNKNOWN_USER_98",
1374 "EM_UNKNOWN_USER_99",
1375 "EM_SETPUNCTUATION",
1376 "EM_GETPUNCTUATION",
1377 "EM_SETWORDWRAPMODE",
1378 "EM_GETWORDWRAPMODE",
1379 "EM_SETIMECOLOR",
1380 "EM_GETIMECOLOR",
1381 "EM_SETIMEOPTIONS",
1382 "EM_GETIMEOPTIONS",
1383 "EM_CONVPOSITION",
1384 "EM_UNKNOWN_USER_109",
1385 "EM_UNKNOWN_USER_110",
1386 "EM_UNKNOWN_USER_111",
1387 "EM_UNKNOWN_USER_112",
1388 "EM_UNKNOWN_USER_113",
1389 "EM_UNKNOWN_USER_114",
1390 "EM_UNKNOWN_USER_115",
1391 "EM_UNKNOWN_USER_116",
1392 "EM_UNKNOWN_USER_117",
1393 "EM_UNKNOWN_USER_118",
1394 "EM_UNKNOWN_USER_119",
1395 "EM_SETLANGOPTIONS",
1396 "EM_GETLANGOPTIONS",
1397 "EM_GETIMECOMPMODE",
1398 "EM_FINDTEXTW",
1399 "EM_FINDTEXTEXW",
1400 "EM_RECONVERSION",
1401 "EM_SETIMEMODEBIAS",
1402 "EM_GETIMEMODEBIAS"
1405 static const char *
1406 get_msg_name(UINT msg)
1408 if (msg >= EM_GETSEL && msg <= EM_CHARFROMPOS)
1409 return edit_messages[msg - EM_GETSEL];
1410 if (msg >= EM_CANPASTE && msg <= EM_GETIMEMODEBIAS)
1411 return richedit_messages[msg - EM_CANPASTE];
1412 return "";
1415 static LRESULT RichEditWndProc_common(HWND hWnd, UINT msg, WPARAM wParam,
1416 LPARAM lParam, BOOL unicode)
1418 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongPtrW(hWnd, 0);
1420 TRACE("hwnd %p msg %04x (%s) %lx %lx, unicode %d\n",
1421 hWnd, msg, get_msg_name(msg), wParam, lParam, unicode);
1423 if (!editor && msg != WM_NCCREATE && msg != WM_NCDESTROY) {
1424 ERR("called with invalid hWnd %p - application bug?\n", hWnd);
1425 return 0;
1428 switch(msg) {
1430 UNSUPPORTED_MSG(EM_DISPLAYBAND)
1431 UNSUPPORTED_MSG(EM_FINDWORDBREAK)
1432 UNSUPPORTED_MSG(EM_FMTLINES)
1433 UNSUPPORTED_MSG(EM_FORMATRANGE)
1434 UNSUPPORTED_MSG(EM_GETBIDIOPTIONS)
1435 UNSUPPORTED_MSG(EM_GETEDITSTYLE)
1436 UNSUPPORTED_MSG(EM_GETIMECOMPMODE)
1437 /* UNSUPPORTED_MSG(EM_GETIMESTATUS) missing in Wine headers */
1438 UNSUPPORTED_MSG(EM_GETLANGOPTIONS)
1439 /* UNSUPPORTED_MSG(EM_GETOLEINTERFACE) separate stub */
1440 UNSUPPORTED_MSG(EM_GETREDONAME)
1441 UNSUPPORTED_MSG(EM_GETTEXTMODE)
1442 UNSUPPORTED_MSG(EM_GETTYPOGRAPHYOPTIONS)
1443 UNSUPPORTED_MSG(EM_GETUNDONAME)
1444 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX)
1445 UNSUPPORTED_MSG(EM_PASTESPECIAL)
1446 UNSUPPORTED_MSG(EM_SELECTIONTYPE)
1447 UNSUPPORTED_MSG(EM_SETBIDIOPTIONS)
1448 UNSUPPORTED_MSG(EM_SETEDITSTYLE)
1449 UNSUPPORTED_MSG(EM_SETFONTSIZE)
1450 UNSUPPORTED_MSG(EM_SETLANGOPTIONS)
1451 UNSUPPORTED_MSG(EM_SETPALETTE)
1452 UNSUPPORTED_MSG(EM_SETTABSTOPS)
1453 UNSUPPORTED_MSG(EM_SETTARGETDEVICE)
1454 UNSUPPORTED_MSG(EM_SETTYPOGRAPHYOPTIONS)
1455 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX)
1456 UNSUPPORTED_MSG(WM_STYLECHANGING)
1457 UNSUPPORTED_MSG(WM_STYLECHANGED)
1458 /* UNSUPPORTED_MSG(WM_UNICHAR) FIXME missing in Wine headers */
1460 /* Messages specific to Richedit controls */
1462 case EM_STREAMIN:
1463 return ME_StreamIn(editor, wParam, (EDITSTREAM*)lParam);
1464 case EM_STREAMOUT:
1465 return ME_StreamOut(editor, wParam, (EDITSTREAM *)lParam);
1466 case WM_GETDLGCODE:
1468 UINT code = DLGC_WANTCHARS|DLGC_WANTARROWS;
1469 if(lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
1471 int vk = (int)((LPMSG)lParam)->wParam;
1472 /* if style says we want return key */
1473 if((vk == VK_RETURN) && (GetWindowLongW(hWnd, GWL_STYLE) & ES_WANTRETURN))
1475 code |= DLGC_WANTMESSAGE;
1477 /* we always handle ctrl-tab */
1478 if((vk == VK_TAB) && (GetKeyState(VK_CONTROL) & 0x8000))
1480 code |= DLGC_WANTMESSAGE;
1483 return code;
1485 case WM_NCCREATE:
1487 CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
1488 TRACE("WM_NCCREATE: style 0x%08x\n", pcs->style);
1489 editor = ME_MakeEditor(hWnd);
1490 SetWindowLongPtrW(hWnd, 0, (LONG_PTR)editor);
1491 return TRUE;
1493 case EM_EMPTYUNDOBUFFER:
1494 ME_EmptyUndoStack(editor);
1495 return 0;
1496 case EM_GETSEL:
1498 /* Note: wParam/lParam can be NULL */
1499 UINT from, to;
1500 PUINT pfrom = wParam ? (PUINT)wParam : &from;
1501 PUINT pto = lParam ? (PUINT)lParam : &to;
1502 ME_GetSelection(editor, (int *)pfrom, (int *)pto);
1503 if ((*pfrom|*pto) & 0xFFFF0000)
1504 return -1;
1505 return MAKELONG(*pfrom,*pto);
1507 case EM_EXGETSEL:
1509 CHARRANGE *pRange = (CHARRANGE *)lParam;
1510 ME_GetSelection(editor, (int *)&pRange->cpMin, (int *)&pRange->cpMax);
1511 TRACE("EM_EXGETSEL = (%d,%d)\n", pRange->cpMin, pRange->cpMax);
1512 return 0;
1514 case EM_SETUNDOLIMIT:
1516 if ((int)wParam < 0)
1517 editor->nUndoLimit = STACK_SIZE_DEFAULT;
1518 else
1519 editor->nUndoLimit = min(wParam, STACK_SIZE_MAX);
1520 /* Setting a max stack size keeps wine from getting killed
1521 for hogging memory. Windows allocates all this memory at once, so
1522 no program would realistically set a value above our maximum. */
1523 return editor->nUndoLimit;
1525 case EM_CANUNDO:
1526 return editor->pUndoStack != NULL;
1527 case EM_CANREDO:
1528 return editor->pRedoStack != NULL;
1529 case WM_UNDO: /* FIXME: actually not the same */
1530 case EM_UNDO:
1531 ME_Undo(editor);
1532 return 0;
1533 case EM_REDO:
1534 ME_Redo(editor);
1535 return 0;
1536 case EM_GETOPTIONS:
1538 /* these flags are equivalent to the ES_* counterparts */
1539 DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
1540 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN;
1541 DWORD settings = GetWindowLongW(hWnd, GWL_STYLE) & mask;
1543 return settings;
1545 case EM_SETOPTIONS:
1547 /* these flags are equivalent to ES_* counterparts
1548 * ECO_READONLY is already implemented in the code, only requires
1549 * setting the bit to work
1551 DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
1552 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN;
1553 DWORD raw = GetWindowLongW(hWnd, GWL_STYLE);
1554 DWORD settings = mask & raw;
1556 switch(wParam)
1558 case ECOOP_SET:
1559 settings = lParam;
1560 break;
1561 case ECOOP_OR:
1562 settings |= lParam;
1563 break;
1564 case ECOOP_AND:
1565 settings &= lParam;
1566 break;
1567 case ECOOP_XOR:
1568 settings ^= lParam;
1570 SetWindowLongW(hWnd, GWL_STYLE, (raw & ~mask) | (settings & mask));
1572 if (lParam & ECO_AUTOWORDSELECTION)
1573 FIXME("ECO_AUTOWORDSELECTION not implemented yet!\n");
1574 if (lParam & ECO_SELECTIONBAR)
1575 FIXME("ECO_SELECTIONBAR not implemented yet!\n");
1576 if (lParam & ECO_VERTICAL)
1577 FIXME("ECO_VERTICAL not implemented yet!\n");
1578 if (lParam & ECO_AUTOHSCROLL)
1579 FIXME("ECO_AUTOHSCROLL not implemented yet!\n");
1580 if (lParam & ECO_AUTOVSCROLL)
1581 FIXME("ECO_AUTOVSCROLL not implemented yet!\n");
1582 if (lParam & ECO_NOHIDESEL)
1583 FIXME("ECO_NOHIDESEL not implemented yet!\n");
1584 if (lParam & ECO_WANTRETURN)
1585 FIXME("ECO_WANTRETURN not implemented yet!\n");
1587 return settings;
1589 case EM_SETSEL:
1591 ME_InvalidateSelection(editor);
1592 ME_SetSelection(editor, wParam, lParam);
1593 ME_InvalidateSelection(editor);
1594 ME_SendSelChange(editor);
1595 return 0;
1597 case EM_SETSCROLLPOS:
1599 POINT *point = (POINT *)lParam;
1600 ME_ScrollAbs(editor, point->y);
1601 return 0;
1603 case EM_AUTOURLDETECT:
1605 if (wParam==1 || wParam ==0)
1607 editor->AutoURLDetect_bEnable = (BOOL)wParam;
1608 return 0;
1610 return E_INVALIDARG;
1612 case EM_GETAUTOURLDETECT:
1614 return editor->AutoURLDetect_bEnable;
1616 case EM_EXSETSEL:
1618 int end;
1619 CHARRANGE range = *(CHARRANGE *)lParam;
1621 TRACE("EM_EXSETSEL (%d,%d)\n", range.cpMin, range.cpMax);
1623 ME_InvalidateSelection(editor);
1624 end = ME_SetSelection(editor, range.cpMin, range.cpMax);
1625 ME_InvalidateSelection(editor);
1626 ME_SendSelChange(editor);
1628 return end;
1630 case EM_SHOWSCROLLBAR:
1632 ShowScrollBar(editor->hWnd, wParam, lParam);
1633 return 0;
1635 case EM_SETTEXTEX:
1637 LPWSTR wszText;
1638 SETTEXTEX *pStruct = (SETTEXTEX *)wParam;
1639 size_t len;
1640 int from, to;
1641 ME_Style *style;
1642 int oldModify = editor->nModifyStep;
1644 if (!pStruct) return 0;
1646 TRACE("EM_SETTEXTEX - %s, flags %d, cp %d\n",
1647 pStruct->codepage == 1200 ? debugstr_w((LPCWSTR)lParam) : debugstr_a((LPCSTR)lParam),
1648 pStruct->flags, pStruct->codepage);
1650 /* FIXME: make use of pStruct->codepage in the to unicode translation */
1651 wszText = lParam ? ME_ToUnicode(pStruct->codepage == 1200, (void *)lParam) : NULL;
1652 len = wszText ? lstrlenW(wszText) : 0;
1654 /* FIXME: this should support RTF strings too, according to MSDN */
1655 if (pStruct->flags & ST_SELECTION) {
1656 ME_GetSelection(editor, &from, &to);
1657 style = ME_GetSelectionInsertStyle(editor);
1658 ME_InternalDeleteText(editor, from, to - from);
1659 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
1660 ME_ReleaseStyle(style);
1662 else {
1663 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
1664 ME_InsertTextFromCursor(editor, 0, wszText, len, editor->pBuffer->pDefaultStyle);
1665 len = 1;
1667 ME_CommitUndo(editor);
1668 if (!(pStruct->flags & ST_KEEPUNDO))
1670 editor->nModifyStep = oldModify;
1671 ME_EmptyUndoStack(editor);
1673 ME_UpdateRepaint(editor);
1674 return len;
1676 case EM_SETBKGNDCOLOR:
1678 LRESULT lColor = ME_GetBackColor(editor);
1679 if (editor->rgbBackColor != -1)
1680 DeleteObject(editor->hbrBackground);
1681 if (wParam)
1683 editor->rgbBackColor = -1;
1684 editor->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
1686 else
1688 editor->rgbBackColor = lParam;
1689 editor->hbrBackground = CreateSolidBrush(editor->rgbBackColor);
1691 if (editor->bRedraw)
1693 InvalidateRect(hWnd, NULL, TRUE);
1694 UpdateWindow(hWnd);
1696 return lColor;
1698 case EM_GETMODIFY:
1699 return editor->nModifyStep == 0 ? 0 : 1;
1700 case EM_SETMODIFY:
1702 if (wParam)
1703 editor->nModifyStep = 1;
1704 else
1705 editor->nModifyStep = 0;
1707 return 0;
1709 case EM_SETREADONLY:
1711 long nStyle = GetWindowLongW(hWnd, GWL_STYLE);
1712 if (wParam)
1713 nStyle |= ES_READONLY;
1714 else
1715 nStyle &= ~ES_READONLY;
1716 SetWindowLongW(hWnd, GWL_STYLE, nStyle);
1717 return 0;
1719 case EM_SETEVENTMASK:
1721 DWORD nOldMask = editor->nEventMask;
1723 editor->nEventMask = lParam;
1724 return nOldMask;
1726 case EM_GETEVENTMASK:
1727 return editor->nEventMask;
1728 case EM_SETCHARFORMAT:
1730 CHARFORMAT2W buf, *p;
1731 BOOL bRepaint = TRUE;
1732 p = ME_ToCF2W(&buf, (CHARFORMAT2W *)lParam);
1733 if (!wParam || (editor->mode & TM_PLAINTEXT))
1734 ME_SetDefaultCharFormat(editor, p);
1735 else if (wParam == (SCF_WORD | SCF_SELECTION))
1736 FIXME("EM_SETCHARFORMAT: word selection not supported\n");
1737 else if (wParam == SCF_ALL)
1738 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), p);
1739 else {
1740 int from, to;
1741 ME_GetSelection(editor, &from, &to);
1742 bRepaint = (from != to);
1743 ME_SetSelectionCharFormat(editor, p);
1745 editor->nModifyStep = 1;
1746 ME_CommitUndo(editor);
1747 if (bRepaint)
1748 ME_RewrapRepaint(editor);
1749 return 0;
1751 case EM_GETCHARFORMAT:
1753 CHARFORMAT2W tmp, *dst = (CHARFORMAT2W *)lParam;
1754 if (dst->cbSize != sizeof(CHARFORMATA) &&
1755 dst->cbSize != sizeof(CHARFORMATW) &&
1756 dst->cbSize != sizeof(CHARFORMAT2A) &&
1757 dst->cbSize != sizeof(CHARFORMAT2W))
1758 return 0;
1759 tmp.cbSize = sizeof(tmp);
1760 if (!wParam)
1761 ME_GetDefaultCharFormat(editor, &tmp);
1762 else
1763 ME_GetSelectionCharFormat(editor, &tmp);
1764 ME_CopyToCFAny(dst, &tmp);
1765 return tmp.dwMask;
1767 case EM_SETPARAFORMAT:
1768 ME_SetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
1769 ME_RewrapRepaint(editor);
1770 ME_CommitUndo(editor);
1771 return 0;
1772 case EM_GETPARAFORMAT:
1773 ME_GetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
1774 return 0;
1775 case EM_GETFIRSTVISIBLELINE:
1777 ME_DisplayItem *p = editor->pBuffer->pFirst;
1778 int y = ME_GetYScrollPos(editor);
1779 int ypara = 0;
1780 int count = 0;
1781 int ystart, yend;
1782 while(p) {
1783 p = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
1784 if (p->type == diTextEnd)
1785 break;
1786 if (p->type == diParagraph) {
1787 ypara = p->member.para.nYPos;
1788 continue;
1790 ystart = ypara + p->member.row.nYPos;
1791 yend = ystart + p->member.row.nHeight;
1792 if (y < yend) {
1793 break;
1795 count++;
1797 return count;
1799 case EM_HIDESELECTION:
1801 editor->bHideSelection = (wParam != 0);
1802 ME_InvalidateSelection(editor);
1803 return 0;
1805 case EM_LINESCROLL:
1807 ME_ScrollDown(editor, lParam * 8); /* FIXME follow the original */
1808 return TRUE; /* Should return false if a single line richedit control */
1810 case WM_CLEAR:
1812 int from, to;
1813 ME_GetSelection(editor, &from, &to);
1814 ME_InternalDeleteText(editor, from, to-from);
1815 ME_CommitUndo(editor);
1816 ME_UpdateRepaint(editor);
1817 return 0;
1819 case EM_REPLACESEL:
1821 int from, to;
1822 ME_Style *style;
1823 LPWSTR wszText = ME_ToUnicode(unicode, (void *)lParam);
1824 size_t len = lstrlenW(wszText);
1825 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText));
1827 ME_GetSelection(editor, &from, &to);
1828 style = ME_GetSelectionInsertStyle(editor);
1829 ME_InternalDeleteText(editor, from, to-from);
1830 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
1831 ME_ReleaseStyle(style);
1832 /* drop temporary style if line end */
1834 * FIXME question: does abc\n mean: put abc,
1835 * clear temp style, put \n? (would require a change)
1837 if (len>0 && wszText[len-1] == '\n')
1838 ME_ClearTempStyle(editor);
1839 ME_EndToUnicode(unicode, wszText);
1840 ME_CommitUndo(editor);
1841 if (!wParam)
1842 ME_EmptyUndoStack(editor);
1843 ME_UpdateRepaint(editor);
1844 return 0;
1846 case EM_SCROLLCARET:
1848 int top, bottom; /* row's edges relative to document top */
1849 int nPos;
1850 ME_DisplayItem *para, *row;
1852 nPos = ME_GetYScrollPos(editor);
1853 row = ME_RowStart(editor->pCursors[0].pRun);
1854 para = ME_GetParagraph(row);
1855 top = para->member.para.nYPos + row->member.row.nYPos;
1856 bottom = top + row->member.row.nHeight;
1858 if (top < nPos) /* caret above window */
1859 ME_ScrollAbs(editor, top);
1860 else if (nPos + editor->sizeWindow.cy < bottom) /*below*/
1861 ME_ScrollAbs(editor, bottom - editor->sizeWindow.cy);
1862 return 0;
1864 case WM_SETFONT:
1866 LOGFONTW lf;
1867 CHARFORMAT2W fmt;
1868 HDC hDC;
1869 BOOL bRepaint = LOWORD(lParam);
1871 if (!wParam)
1872 wParam = (WPARAM)GetStockObject(SYSTEM_FONT);
1873 GetObjectW((HGDIOBJ)wParam, sizeof(LOGFONTW), &lf);
1874 hDC = GetDC(hWnd);
1875 ME_CharFormatFromLogFont(hDC, &lf, &fmt);
1876 ReleaseDC(hWnd, hDC);
1877 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), &fmt);
1878 ME_SetDefaultCharFormat(editor, &fmt);
1880 ME_CommitUndo(editor);
1881 if (bRepaint)
1882 ME_RewrapRepaint(editor);
1883 return 0;
1885 case WM_SETTEXT:
1887 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
1888 if (lParam)
1890 TRACE("WM_SETTEXT lParam==%lx\n",lParam);
1891 if (!unicode && !strncmp((char *)lParam, "{\\rtf", 5))
1893 /* Undocumented: WM_SETTEXT supports RTF text */
1894 ME_StreamInRTFString(editor, 0, (char *)lParam);
1896 else
1898 LPWSTR wszText = ME_ToUnicode(unicode, (void *)lParam);
1899 TRACE("WM_SETTEXT - %s\n", debugstr_w(wszText)); /* debugstr_w() */
1900 if (lstrlenW(wszText) > 0)
1902 /* uses default style! */
1903 ME_InsertTextFromCursor(editor, 0, wszText, -1, editor->pBuffer->pDefaultStyle);
1905 ME_EndToUnicode(unicode, wszText);
1908 else
1909 TRACE("WM_SETTEXT - NULL\n");
1910 ME_CommitUndo(editor);
1911 ME_EmptyUndoStack(editor);
1912 ME_SetSelection(editor, 0, 0);
1913 ME_UpdateRepaint(editor);
1914 return 1;
1916 case EM_CANPASTE:
1918 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
1919 if (IsClipboardFormatAvailable(nRTFFormat))
1920 return TRUE;
1921 if (IsClipboardFormatAvailable(CF_UNICODETEXT))
1922 return TRUE;
1923 return FALSE;
1925 case WM_PASTE:
1927 DWORD dwFormat = 0;
1928 EDITSTREAM es;
1929 ME_GlobalDestStruct gds;
1930 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
1931 UINT cf = 0;
1933 if (IsClipboardFormatAvailable(nRTFFormat))
1934 cf = nRTFFormat, dwFormat = SF_RTF;
1935 else if (IsClipboardFormatAvailable(CF_UNICODETEXT))
1936 cf = CF_UNICODETEXT, dwFormat = SF_TEXT|SF_UNICODE;
1937 else
1938 return 0;
1940 if (!OpenClipboard(hWnd))
1941 return 0;
1942 gds.hData = GetClipboardData(cf);
1943 gds.nLength = 0;
1944 es.dwCookie = (DWORD)&gds;
1945 es.pfnCallback = dwFormat == SF_RTF ? ME_ReadFromHGLOBALRTF : ME_ReadFromHGLOBALUnicode;
1946 ME_StreamIn(editor, dwFormat|SFF_SELECTION, &es);
1948 CloseClipboard();
1949 return 0;
1951 case WM_CUT:
1952 case WM_COPY:
1954 LPDATAOBJECT dataObj = NULL;
1955 CHARRANGE range;
1956 HRESULT hr = S_OK;
1958 if (editor->cPasswordMask)
1959 return 0; /* Copying or Cutting masked text isn't allowed */
1961 ME_GetSelection(editor, (int*)&range.cpMin, (int*)&range.cpMax);
1962 if(editor->lpOleCallback)
1963 hr = IRichEditOleCallback_GetClipboardData(editor->lpOleCallback, &range, RECO_COPY, &dataObj);
1964 if(FAILED(hr) || !dataObj)
1965 hr = ME_GetDataObject(editor, &range, &dataObj);
1966 if(SUCCEEDED(hr)) {
1967 hr = OleSetClipboard(dataObj);
1968 IDataObject_Release(dataObj);
1970 if (SUCCEEDED(hr) && msg == WM_CUT)
1972 ME_InternalDeleteText(editor, range.cpMin, range.cpMax-range.cpMin);
1973 ME_CommitUndo(editor);
1974 ME_UpdateRepaint(editor);
1976 return 0;
1978 case WM_GETTEXTLENGTH:
1979 return ME_GetTextLength(editor);
1980 case EM_GETTEXTLENGTHEX:
1981 return ME_GetTextLengthEx(editor, (GETTEXTLENGTHEX *)wParam);
1982 case WM_GETTEXT:
1984 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
1985 tr.chrg.cpMin = 0;
1986 tr.chrg.cpMax = wParam ? (wParam - 1) : 0;
1987 tr.lpstrText = (WCHAR *)lParam;
1988 return RichEditWndProc_common(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr, unicode);
1990 case EM_GETTEXTEX:
1992 GETTEXTEX *ex = (GETTEXTEX*)wParam;
1993 int nStart, nCount;
1995 if (ex->flags & ~(GT_SELECTION | GT_USECRLF))
1996 FIXME("GETTEXTEX flags 0x%08x not supported\n", ex->flags & ~(GT_SELECTION | GT_USECRLF));
1998 if (ex->flags & GT_SELECTION)
2000 ME_GetSelection(editor, &nStart, &nCount);
2001 nCount -= nStart;
2002 nCount = min(nCount, ex->cb - 1);
2004 else
2006 nStart = 0;
2007 nCount = ex->cb - 1;
2009 if (ex->codepage == 1200)
2011 nCount = min(nCount, ex->cb / sizeof(WCHAR) - 1);
2012 return ME_GetTextW(editor, (LPWSTR)lParam, nStart, nCount, ex->flags & GT_USECRLF);
2014 else
2016 /* potentially each char may be a CR, why calculate the exact value with O(N) when
2017 we can just take a bigger buffer? :) */
2018 int crlfmul = (ex->flags & GT_USECRLF) ? 2 : 1;
2019 LPWSTR buffer = richedit_alloc((crlfmul*nCount + 1) * sizeof(WCHAR));
2020 DWORD buflen = ex->cb;
2021 LRESULT rc;
2022 DWORD flags = 0;
2024 buflen = ME_GetTextW(editor, buffer, nStart, nCount, ex->flags & GT_USECRLF);
2025 rc = WideCharToMultiByte(ex->codepage, flags, buffer, -1, (LPSTR)lParam, ex->cb, ex->lpDefaultChar, ex->lpUsedDefaultChar);
2026 if (rc) rc--; /* do not count 0 terminator */
2028 richedit_free(buffer);
2029 return rc;
2032 case EM_GETSELTEXT:
2034 int from, to;
2035 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
2036 ME_GetSelection(editor, &from, &to);
2037 tr.chrg.cpMin = from;
2038 tr.chrg.cpMax = to;
2039 tr.lpstrText = (WCHAR *)lParam;
2040 return RichEditWndProc_common(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr, unicode);
2042 case EM_GETSCROLLPOS:
2044 POINT *point = (POINT *)lParam;
2045 point->x = 0; /* FIXME side scrolling not implemented */
2046 point->y = ME_GetYScrollPos(editor);
2047 return 1;
2049 case EM_GETTEXTRANGE:
2051 TEXTRANGEW *rng = (TEXTRANGEW *)lParam;
2052 TRACE("EM_GETTEXTRANGE min=%d max=%d unicode=%d emul1.0=%d length=%d\n",
2053 rng->chrg.cpMin, rng->chrg.cpMax, unicode,
2054 editor->bEmulateVersion10, ME_GetTextLength(editor));
2055 if (unicode)
2056 return ME_GetTextW(editor, rng->lpstrText, rng->chrg.cpMin, rng->chrg.cpMax-rng->chrg.cpMin, editor->bEmulateVersion10);
2057 else
2059 int nLen = rng->chrg.cpMax-rng->chrg.cpMin;
2060 WCHAR *p = ALLOC_N_OBJ(WCHAR, nLen+1);
2061 int nChars = ME_GetTextW(editor, p, rng->chrg.cpMin, nLen, editor->bEmulateVersion10);
2062 /* FIXME this is a potential security hole (buffer overrun)
2063 if you know more about wchar->mbyte conversion please explain
2065 WideCharToMultiByte(CP_ACP, 0, p, nChars+1, (char *)rng->lpstrText, nLen+1, NULL, NULL);
2066 FREE_OBJ(p);
2067 return nChars;
2070 case EM_GETLINE:
2072 ME_DisplayItem *run;
2073 const unsigned int nMaxChars = *(WORD *) lParam;
2074 unsigned int nEndChars, nCharsLeft = nMaxChars;
2075 char *dest = (char *) lParam;
2076 /* rich text editor 1.0 uses \r\n for line end, 2.0 uses just \r;
2077 we need to know how if we have the extra \n or not */
2078 int nLF = editor->bEmulateVersion10;
2080 TRACE("EM_GETLINE: row=%d, nMaxChars=%d (%s)\n", (int) wParam, nMaxChars,
2081 unicode ? "Unicode" : "Ansi");
2083 run = ME_FindRowWithNumber(editor, wParam);
2084 if (run == NULL)
2085 return 0;
2087 while (nCharsLeft && (run = ME_FindItemFwd(run, diRunOrStartRow))
2088 && !(run->member.run.nFlags & MERF_ENDPARA))
2090 unsigned int nCopy;
2091 ME_String *strText;
2092 if (run->type != diRun)
2093 break;
2094 strText = run->member.run.strText;
2095 nCopy = min(nCharsLeft, strText->nLen);
2097 if (unicode)
2098 lstrcpynW((LPWSTR) dest, strText->szData, nCopy);
2099 else
2100 nCopy = WideCharToMultiByte(CP_ACP, 0, strText->szData, nCopy, dest,
2101 nCharsLeft, NULL, NULL);
2102 dest += nCopy * (unicode ? sizeof(WCHAR) : 1);
2103 nCharsLeft -= nCopy;
2106 /* append \r\0 (or \r\n\0 in 1.0), space allowing */
2107 nEndChars = min(nCharsLeft, 2 + nLF);
2108 nCharsLeft -= nEndChars;
2109 if (unicode)
2111 const WCHAR src[] = {'\r', '\0'};
2112 const WCHAR src10[] = {'\r', '\n', '\0'};
2113 lstrcpynW((LPWSTR) dest, nLF ? src10 : src, nEndChars);
2115 else
2116 lstrcpynA(dest, nLF ? "\r\n" : "\r", nEndChars);
2118 TRACE("EM_GETLINE: got %u bytes\n", nMaxChars - nCharsLeft);
2120 if (nEndChars == 2 + nLF)
2121 return nMaxChars - nCharsLeft - 1; /* don't count \0 */
2122 else
2123 return nMaxChars - nCharsLeft;
2125 case EM_GETLINECOUNT:
2127 ME_DisplayItem *item = editor->pBuffer->pFirst->next;
2128 int nRows = 0;
2130 while (item != editor->pBuffer->pLast)
2132 assert(item->type == diParagraph);
2133 nRows += item->member.para.nRows;
2134 item = item->member.para.next_para;
2136 TRACE("EM_GETLINECOUNT: nRows==%d\n", nRows);
2137 return max(1, nRows);
2139 case EM_LINEFROMCHAR:
2141 if (wParam == -1)
2142 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(editor, 1));
2143 else
2144 return ME_RowNumberFromCharOfs(editor, wParam);
2146 case EM_EXLINEFROMCHAR:
2148 if (lParam == -1)
2149 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(editor,1));
2150 else
2151 return ME_RowNumberFromCharOfs(editor, lParam);
2153 case EM_LINEINDEX:
2155 ME_DisplayItem *item, *para;
2156 int nCharOfs;
2158 if (wParam == -1)
2159 item = ME_FindItemBack(editor->pCursors[0].pRun, diStartRow);
2160 else
2161 item = ME_FindRowWithNumber(editor, wParam);
2162 if (!item)
2163 return -1;
2164 para = ME_GetParagraph(item);
2165 item = ME_FindItemFwd(item, diRun);
2166 nCharOfs = para->member.para.nCharOfs + item->member.run.nCharOfs;
2167 TRACE("EM_LINEINDEX: nCharOfs==%d\n", nCharOfs);
2168 return nCharOfs;
2170 case EM_LINELENGTH:
2172 ME_DisplayItem *item, *item_end;
2173 int nChars = 0, nThisLineOfs = 0, nNextLineOfs = 0;
2175 if (wParam > ME_GetTextLength(editor))
2176 return 0;
2177 if (wParam == -1)
2179 FIXME("EM_LINELENGTH: returning number of unselected characters on lines with selection unsupported.\n");
2180 return 0;
2182 item = ME_FindItemAtOffset(editor, diRun, wParam, NULL);
2183 item = ME_RowStart(item);
2184 nThisLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item, diRun), 0);
2185 item_end = ME_FindItemFwd(item, diStartRowOrParagraphOrEnd);
2186 if (item_end->type == diStartRow)
2187 nNextLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item_end, diRun), 0);
2188 else
2189 nNextLineOfs = ME_FindItemFwd(item, diParagraphOrEnd)->member.para.nCharOfs
2190 - (editor->bEmulateVersion10?2:1);
2191 nChars = nNextLineOfs - nThisLineOfs;
2192 TRACE("EM_LINELENGTH(%ld)==%d\n",wParam, nChars);
2193 return nChars;
2195 case EM_EXLIMITTEXT:
2197 if ((int)lParam < 0)
2198 return 0;
2199 if (lParam == 0)
2200 editor->nTextLimit = 65536;
2201 else
2202 editor->nTextLimit = (int) lParam;
2203 return 0;
2205 case EM_LIMITTEXT:
2207 if (wParam == 0)
2208 editor->nTextLimit = 65536;
2209 else
2210 editor->nTextLimit = (int) wParam;
2211 return 0;
2213 case EM_GETLIMITTEXT:
2215 return editor->nTextLimit;
2217 case EM_FINDTEXT:
2219 FINDTEXTA *ft = (FINDTEXTA *)lParam;
2220 int nChars = MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, NULL, 0);
2221 WCHAR *tmp;
2222 LRESULT r;
2224 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
2225 MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, tmp, nChars);
2226 r = ME_FindText(editor, wParam, &ft->chrg, tmp, NULL);
2227 FREE_OBJ( tmp );
2228 return r;
2230 case EM_FINDTEXTEX:
2232 FINDTEXTEXA *ex = (FINDTEXTEXA *)lParam;
2233 int nChars = MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, NULL, 0);
2234 WCHAR *tmp;
2235 LRESULT r;
2237 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
2238 MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, tmp, nChars);
2239 r = ME_FindText(editor, wParam, &ex->chrg, tmp, &ex->chrgText);
2240 FREE_OBJ( tmp );
2241 return r;
2243 case EM_FINDTEXTW:
2245 FINDTEXTW *ft = (FINDTEXTW *)lParam;
2246 return ME_FindText(editor, wParam, &ft->chrg, ft->lpstrText, NULL);
2248 case EM_FINDTEXTEXW:
2250 FINDTEXTEXW *ex = (FINDTEXTEXW *)lParam;
2251 return ME_FindText(editor, wParam, &ex->chrg, ex->lpstrText, &ex->chrgText);
2253 case EM_GETZOOM:
2254 if (!wParam || !lParam)
2255 return FALSE;
2256 *(int *)wParam = editor->nZoomNumerator;
2257 *(int *)lParam = editor->nZoomDenominator;
2258 return TRUE;
2259 case EM_SETZOOM:
2260 return ME_SetZoom(editor, wParam, lParam);
2261 case EM_CHARFROMPOS:
2262 return ME_CharFromPos(editor, ((POINTL *)lParam)->x, ((POINTL *)lParam)->y);
2263 case EM_POSFROMCHAR:
2265 ME_DisplayItem *pRun;
2266 int nCharOfs, nOffset, nLength;
2267 POINTL pt = {0,0};
2269 nCharOfs = wParam;
2270 /* detect which API version we're dealing with */
2271 if (wParam >= 0x40000)
2272 nCharOfs = lParam;
2273 nLength = ME_GetTextLength(editor);
2275 if (nCharOfs < nLength) {
2276 ME_RunOfsFromCharOfs(editor, nCharOfs, &pRun, &nOffset);
2277 assert(pRun->type == diRun);
2278 pt.y = pRun->member.run.pt.y;
2279 pt.x = pRun->member.run.pt.x + ME_PointFromChar(editor, &pRun->member.run, nOffset);
2280 pt.y += ME_GetParagraph(pRun)->member.para.nYPos;
2281 } else {
2282 pt.x = 0;
2283 pt.y = editor->pBuffer->pLast->member.para.nYPos;
2285 if (wParam >= 0x40000) {
2286 *(POINTL *)wParam = pt;
2288 return MAKELONG( pt.x, pt.y );
2290 case WM_CREATE:
2291 if (GetWindowLongW(hWnd, GWL_STYLE) & WS_HSCROLL)
2292 { /* Squelch the default horizontal scrollbar it would make */
2293 ShowScrollBar(editor->hWnd, SB_HORZ, FALSE);
2295 ME_CommitUndo(editor);
2296 ME_WrapMarkedParagraphs(editor);
2297 ME_MoveCaret(editor);
2298 return 0;
2299 case WM_DESTROY:
2300 ME_DestroyEditor(editor);
2301 SetWindowLongPtrW(hWnd, 0, 0);
2302 return 0;
2303 case WM_LBUTTONDOWN:
2304 SetFocus(hWnd);
2305 ME_LButtonDown(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
2306 SetCapture(hWnd);
2307 ME_LinkNotify(editor,msg,wParam,lParam);
2308 break;
2309 case WM_MOUSEMOVE:
2310 if (GetCapture() == hWnd)
2311 ME_MouseMove(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
2312 ME_LinkNotify(editor,msg,wParam,lParam);
2313 break;
2314 case WM_LBUTTONUP:
2315 if (GetCapture() == hWnd)
2316 ReleaseCapture();
2317 ME_LinkNotify(editor,msg,wParam,lParam);
2318 break;
2319 case WM_LBUTTONDBLCLK:
2320 ME_LinkNotify(editor,msg,wParam,lParam);
2321 ME_SelectWord(editor);
2322 break;
2323 case WM_CONTEXTMENU:
2324 if (!ME_ShowContextMenu(editor, (short)LOWORD(lParam), (short)HIWORD(lParam)))
2325 goto do_default;
2326 break;
2327 case WM_PAINT:
2328 if (editor->bRedraw)
2330 HDC hDC;
2331 PAINTSTRUCT ps;
2333 hDC = BeginPaint(hWnd, &ps);
2334 ME_PaintContent(editor, hDC, FALSE, &ps.rcPaint);
2335 EndPaint(hWnd, &ps);
2337 break;
2338 case WM_SETFOCUS:
2339 editor->bHaveFocus = TRUE;
2340 ME_ShowCaret(editor);
2341 ME_SendOldNotify(editor, EN_SETFOCUS);
2342 return 0;
2343 case WM_KILLFOCUS:
2344 ME_HideCaret(editor);
2345 editor->bHaveFocus = FALSE;
2346 ME_SendOldNotify(editor, EN_KILLFOCUS);
2347 return 0;
2348 case WM_ERASEBKGND:
2350 if (editor->bRedraw)
2352 HDC hDC = (HDC)wParam;
2353 RECT rc;
2354 if (GetUpdateRect(hWnd,&rc,TRUE))
2356 FillRect(hDC, &rc, editor->hbrBackground);
2359 return 1;
2361 case WM_COMMAND:
2362 TRACE("editor wnd command = %d\n", LOWORD(wParam));
2363 return 0;
2364 case WM_KEYDOWN:
2365 if (ME_KeyDown(editor, LOWORD(wParam)))
2366 return 0;
2367 goto do_default;
2368 case WM_CHAR:
2370 WCHAR wstr;
2372 if (unicode)
2373 wstr = (WCHAR)wParam;
2374 else
2376 CHAR charA = wParam;
2377 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &wstr, 1);
2379 if (editor->AutoURLDetect_bEnable)
2380 ME_AutoURLDetect(editor, wstr);
2382 switch (wstr)
2384 case 1: /* Ctrl-A */
2385 ME_SetSelection(editor, 0, -1);
2386 return 0;
2387 case 3: /* Ctrl-C */
2388 SendMessageW(editor->hWnd, WM_COPY, 0, 0);
2389 return 0;
2392 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY) {
2393 MessageBeep(MB_ICONERROR);
2394 return 0; /* FIXME really 0 ? */
2397 switch (wstr)
2399 case 22: /* Ctrl-V */
2400 SendMessageW(editor->hWnd, WM_PASTE, 0, 0);
2401 return 0;
2402 case 24: /* Ctrl-X */
2403 SendMessageW(editor->hWnd, WM_CUT, 0, 0);
2404 return 0;
2405 case 25: /* Ctrl-Y */
2406 SendMessageW(editor->hWnd, EM_REDO, 0, 0);
2407 return 0;
2408 case 26: /* Ctrl-Z */
2409 SendMessageW(editor->hWnd, EM_UNDO, 0, 0);
2410 return 0;
2412 if (((unsigned)wstr)>=' ' || wstr=='\r' || wstr=='\t') {
2413 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
2414 ME_Style *style = ME_GetInsertStyle(editor, 0);
2415 ME_SaveTempStyle(editor);
2416 ME_InsertTextFromCursor(editor, 0, &wstr, 1, style);
2417 ME_ReleaseStyle(style);
2418 ME_CommitUndo(editor);
2419 ME_UpdateRepaint(editor);
2421 return 0;
2423 case EM_SCROLL: /* fall through */
2424 case WM_VSCROLL:
2426 int origNPos;
2427 int lineHeight;
2429 origNPos = ME_GetYScrollPos(editor);
2430 lineHeight = 24;
2432 if (editor && editor->pBuffer && editor->pBuffer->pDefaultStyle)
2433 lineHeight = editor->pBuffer->pDefaultStyle->tm.tmHeight;
2434 if (lineHeight <= 0) lineHeight = 24;
2436 switch(LOWORD(wParam))
2438 case SB_LINEUP:
2439 ME_ScrollUp(editor,lineHeight);
2440 break;
2441 case SB_LINEDOWN:
2442 ME_ScrollDown(editor,lineHeight);
2443 break;
2444 case SB_PAGEUP:
2445 ME_ScrollUp(editor,editor->sizeWindow.cy);
2446 break;
2447 case SB_PAGEDOWN:
2448 ME_ScrollDown(editor,editor->sizeWindow.cy);
2449 break;
2450 case SB_THUMBTRACK:
2451 case SB_THUMBPOSITION:
2452 ME_ScrollAbs(editor,HIWORD(wParam));
2453 break;
2455 if (msg == EM_SCROLL)
2456 return 0x00010000 | (((ME_GetYScrollPos(editor) - origNPos)/lineHeight) & 0xffff);
2457 break;
2459 case WM_MOUSEWHEEL:
2461 int gcWheelDelta;
2462 UINT pulScrollLines;
2464 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
2465 gcWheelDelta = -GET_WHEEL_DELTA_WPARAM(wParam);
2467 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
2469 /* FIXME follow the original */
2470 ME_ScrollDown(editor,pulScrollLines * (gcWheelDelta / WHEEL_DELTA) * 8);
2472 break;
2474 case EM_GETRECT:
2476 *((RECT *)lParam) = editor->rcFormat;
2477 return 0;
2479 case EM_SETRECT:
2480 case EM_SETRECTNP:
2482 if (lParam)
2484 RECT *rc = (RECT *)lParam;
2486 if (wParam)
2488 editor->rcFormat.left += rc->left;
2489 editor->rcFormat.top += rc->top;
2490 editor->rcFormat.right += rc->right;
2491 editor->rcFormat.bottom += rc->bottom;
2493 else
2495 editor->rcFormat = *rc;
2498 else
2500 GetClientRect(hWnd, &editor->rcFormat);
2502 if (msg != EM_SETRECTNP)
2503 ME_RewrapRepaint(editor);
2504 return 0;
2506 case EM_REQUESTRESIZE:
2507 ME_SendRequestResize(editor, TRUE);
2508 return 0;
2509 case WM_SETREDRAW:
2510 editor->bRedraw = wParam;
2511 return 0;
2512 case WM_SIZE:
2514 GetClientRect(hWnd, &editor->rcFormat);
2515 ME_RewrapRepaint(editor);
2516 return DefWindowProcW(hWnd, msg, wParam, lParam);
2518 case EM_GETOLEINTERFACE:
2520 LPVOID *ppvObj = (LPVOID*) lParam;
2521 return CreateIRichEditOle(editor, ppvObj);
2523 case EM_GETPASSWORDCHAR:
2525 return editor->cPasswordMask;
2527 case EM_SETOLECALLBACK:
2528 if(editor->lpOleCallback)
2529 IUnknown_Release(editor->lpOleCallback);
2530 editor->lpOleCallback = (LPRICHEDITOLECALLBACK)lParam;
2531 if(editor->lpOleCallback)
2532 IUnknown_AddRef(editor->lpOleCallback);
2533 return TRUE;
2534 case EM_GETWORDBREAKPROC:
2535 return (LRESULT)editor->pfnWordBreak;
2536 case EM_SETWORDBREAKPROC:
2538 EDITWORDBREAKPROCW pfnOld = editor->pfnWordBreak;
2540 editor->pfnWordBreak = (EDITWORDBREAKPROCW)lParam;
2541 return (LRESULT)pfnOld;
2543 case EM_SETTEXTMODE:
2545 LRESULT ret;
2546 int mask = 0;
2547 int changes = 0;
2548 ret = RichEditWndProc_common(hWnd, WM_GETTEXTLENGTH, 0, 0, unicode);
2549 if (!ret)
2551 /*Check for valid wParam*/
2552 if ((((wParam & TM_RICHTEXT) && ((wParam & TM_PLAINTEXT) << 1))) ||
2553 (((wParam & TM_MULTILEVELUNDO) && ((wParam & TM_SINGLELEVELUNDO) << 1))) ||
2554 (((wParam & TM_MULTICODEPAGE) && ((wParam & TM_SINGLECODEPAGE) << 1))))
2555 return 1;
2556 else
2558 if (wParam & (TM_RICHTEXT | TM_PLAINTEXT))
2560 mask |= (TM_RICHTEXT | TM_PLAINTEXT);
2561 changes |= (wParam & (TM_RICHTEXT | TM_PLAINTEXT));
2563 /*FIXME: Currently no support for undo level and code page options*/
2564 editor->mode = (editor->mode & (~mask)) | changes;
2565 return 0;
2568 return ret;
2570 case EM_SETPASSWORDCHAR:
2572 editor->cPasswordMask = wParam;
2573 ME_RewrapRepaint(editor);
2574 return 0;
2576 default:
2577 do_default:
2578 return DefWindowProcW(hWnd, msg, wParam, lParam);
2580 return 0L;
2583 static LRESULT WINAPI RichEditWndProcW(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2585 BOOL unicode = TRUE;
2587 /* Under Win9x RichEdit20W returns ANSI strings, see the tests. */
2588 if (msg == WM_GETTEXT && (GetVersion() & 0x80000000))
2589 unicode = FALSE;
2591 return RichEditWndProc_common(hWnd, msg, wParam, lParam, unicode);
2594 static LRESULT WINAPI RichEditWndProcA(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2596 return RichEditWndProc_common(hWnd, msg, wParam, lParam, FALSE);
2599 /******************************************************************
2600 * RichEditANSIWndProc (RICHED20.10)
2602 LRESULT WINAPI RichEditANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2604 return RichEditWndProcA(hWnd, msg, wParam, lParam);
2607 /******************************************************************
2608 * RichEdit10ANSIWndProc (RICHED20.9)
2610 LRESULT WINAPI RichEdit10ANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2612 LRESULT result;
2614 /* FIXME: this is NOT the same as 2.0 version */
2615 result = RichEditANSIWndProc(hWnd, msg, wParam, lParam);
2616 if (msg == WM_NCCREATE)
2618 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongPtrW(hWnd, 0);
2620 editor->bEmulateVersion10 = TRUE;
2621 editor->pBuffer->pLast->member.para.nCharOfs = 2;
2623 return result;
2626 void ME_SendOldNotify(ME_TextEditor *editor, int nCode)
2628 HWND hWnd = editor->hWnd;
2629 SendMessageA(GetParent(hWnd), WM_COMMAND, (nCode<<16)|GetWindowLongW(hWnd, GWLP_ID), (LPARAM)hWnd);
2632 void ME_LinkNotify(ME_TextEditor *editor, UINT msg, WPARAM wParam, LPARAM lParam)
2634 int x,y;
2635 ME_Cursor tmpCursor;
2636 int nCharOfs; /* The start of the clicked text. Absolute character offset */
2638 ME_Run *tmpRun;
2640 ENLINK info;
2641 x = (short)LOWORD(lParam);
2642 y = (short)HIWORD(lParam);
2643 nCharOfs = ME_CharFromPos(editor, x, y);
2644 if (nCharOfs < 0) return;
2646 ME_CursorFromCharOfs(editor, nCharOfs, &tmpCursor);
2647 tmpRun = &tmpCursor.pRun->member.run;
2649 if ((tmpRun->style->fmt.dwMask & CFM_LINK)
2650 && (tmpRun->style->fmt.dwEffects & CFE_LINK))
2651 { /* The clicked run has CFE_LINK set */
2652 info.nmhdr.hwndFrom = editor->hWnd;
2653 info.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
2654 info.nmhdr.code = EN_LINK;
2655 info.msg = msg;
2656 info.wParam = wParam;
2657 info.lParam = lParam;
2658 info.chrg.cpMin = ME_CharOfsFromRunOfs(editor,tmpCursor.pRun,0);
2659 info.chrg.cpMax = info.chrg.cpMin + ME_StrVLen(tmpRun->strText);
2660 SendMessageW(GetParent(editor->hWnd), WM_NOTIFY,info.nmhdr.idFrom, (LPARAM)&info);
2664 int ME_CountParagraphsBetween(ME_TextEditor *editor, int from, int to)
2666 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
2667 int i = 0;
2669 while(item && item->member.para.next_para->member.para.nCharOfs <= from)
2670 item = item->member.para.next_para;
2671 if (!item)
2672 return 0;
2673 while(item && item->member.para.next_para->member.para.nCharOfs <= to) {
2674 item = item->member.para.next_para;
2675 i++;
2677 return i;
2681 int ME_GetTextW(ME_TextEditor *editor, WCHAR *buffer, int nStart, int nChars, int bCRLF)
2683 ME_DisplayItem *item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
2684 int nWritten = 0;
2685 WCHAR *pStart = buffer;
2687 if (!item) {
2688 *buffer = 0;
2689 return 0;
2692 if (nStart)
2694 int nLen = ME_StrLen(item->member.run.strText) - nStart;
2695 if (nLen > nChars)
2696 nLen = nChars;
2697 CopyMemory(buffer, item->member.run.strText->szData + nStart, sizeof(WCHAR)*nLen);
2698 nChars -= nLen;
2699 nWritten += nLen;
2700 if (!nChars)
2701 return nWritten;
2702 buffer += nLen;
2703 nStart = 0;
2704 item = ME_FindItemFwd(item, diRun);
2707 while(nChars && item)
2709 int nLen = ME_StrLen(item->member.run.strText);
2710 if (nLen > nChars)
2711 nLen = nChars;
2713 if (item->member.run.nFlags & MERF_ENDPARA)
2715 if (!ME_FindItemFwd(item, diRun))
2716 /* No '\r' is appended to the last paragraph. */
2717 nLen = 0;
2718 else {
2719 *buffer = '\r';
2720 if (bCRLF)
2722 *(++buffer) = '\n';
2723 nWritten++;
2725 assert(nLen == 1);
2726 /* our end paragraph consists of 2 characters now */
2727 if (editor->bEmulateVersion10)
2728 nChars--;
2731 else
2732 CopyMemory(buffer, item->member.run.strText->szData, sizeof(WCHAR)*nLen);
2733 nChars -= nLen;
2734 nWritten += nLen;
2735 buffer += nLen;
2737 if (!nChars)
2739 TRACE("nWritten=%d, actual=%d\n", nWritten, buffer-pStart);
2740 *buffer = 0;
2741 return nWritten;
2743 item = ME_FindItemFwd(item, diRun);
2745 *buffer = 0;
2746 TRACE("nWritten=%d, actual=%d\n", nWritten, buffer-pStart);
2747 return nWritten;
2750 static BOOL ME_RegisterEditorClass(HINSTANCE hInstance)
2752 WNDCLASSW wcW;
2753 WNDCLASSA wcA;
2755 wcW.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
2756 wcW.lpfnWndProc = RichEditWndProcW;
2757 wcW.cbClsExtra = 0;
2758 wcW.cbWndExtra = sizeof(ME_TextEditor *);
2759 wcW.hInstance = NULL; /* hInstance would register DLL-local class */
2760 wcW.hIcon = NULL;
2761 wcW.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
2762 wcW.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
2763 wcW.lpszMenuName = NULL;
2765 if (is_version_nt())
2767 wcW.lpszClassName = RichEdit20W;
2768 if (!RegisterClassW(&wcW)) return FALSE;
2769 wcW.lpszClassName = RichEdit50W;
2770 if (!RegisterClassW(&wcW)) return FALSE;
2772 else
2774 /* WNDCLASSA/W have the same layout */
2775 wcW.lpszClassName = (LPCWSTR)"RichEdit20W";
2776 if (!RegisterClassA((WNDCLASSA *)&wcW)) return FALSE;
2777 wcW.lpszClassName = (LPCWSTR)"RichEdit50W";
2778 if (!RegisterClassA((WNDCLASSA *)&wcW)) return FALSE;
2781 wcA.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
2782 wcA.lpfnWndProc = RichEditWndProcA;
2783 wcA.cbClsExtra = 0;
2784 wcA.cbWndExtra = sizeof(ME_TextEditor *);
2785 wcA.hInstance = NULL; /* hInstance would register DLL-local class */
2786 wcA.hIcon = NULL;
2787 wcA.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
2788 wcA.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
2789 wcA.lpszMenuName = NULL;
2790 wcA.lpszClassName = "RichEdit20A";
2791 if (!RegisterClassA(&wcA)) return FALSE;
2792 wcA.lpszClassName = "RichEdit50A";
2793 if (!RegisterClassA(&wcA)) return FALSE;
2795 return TRUE;
2798 LRESULT WINAPI REComboWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
2799 /* FIXME: Not implemented */
2800 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
2801 hWnd, msg, get_msg_name(msg), wParam, lParam);
2802 return DefWindowProcW(hWnd, msg, wParam, lParam);
2805 LRESULT WINAPI REListWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
2806 /* FIXME: Not implemented */
2807 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
2808 hWnd, msg, get_msg_name(msg), wParam, lParam);
2809 return DefWindowProcW(hWnd, msg, wParam, lParam);
2812 /******************************************************************
2813 * REExtendedRegisterClass (RICHED20.8)
2815 * FIXME undocumented
2816 * Need to check for errors and implement controls and callbacks
2818 LRESULT WINAPI REExtendedRegisterClass(void)
2820 WNDCLASSW wcW;
2821 UINT result;
2823 FIXME("semi stub\n");
2825 wcW.cbClsExtra = 0;
2826 wcW.cbWndExtra = 4;
2827 wcW.hInstance = NULL;
2828 wcW.hIcon = NULL;
2829 wcW.hCursor = NULL;
2830 wcW.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
2831 wcW.lpszMenuName = NULL;
2833 if (!ME_ListBoxRegistered)
2835 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS;
2836 wcW.lpfnWndProc = REListWndProc;
2837 wcW.lpszClassName = REListBox20W;
2838 if (RegisterClassW(&wcW)) ME_ListBoxRegistered = TRUE;
2841 if (!ME_ComboBoxRegistered)
2843 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
2844 wcW.lpfnWndProc = REComboWndProc;
2845 wcW.lpszClassName = REComboBox20W;
2846 if (RegisterClassW(&wcW)) ME_ComboBoxRegistered = TRUE;
2849 result = 0;
2850 if (ME_ListBoxRegistered)
2851 result += 1;
2852 if (ME_ComboBoxRegistered)
2853 result += 2;
2855 return result;
2858 int ME_AutoURLDetect(ME_TextEditor *editor, WCHAR curChar)
2860 struct prefix_s {
2861 const char *text;
2862 int length;
2863 } prefixes[12] = {
2864 {"http:", 5},
2865 {"file:", 6},
2866 {"mailto:", 8},
2867 {"ftp:", 5},
2868 {"https:", 7},
2869 {"gopher:", 8},
2870 {"nntp:", 6},
2871 {"prospero:", 10},
2872 {"telnet:", 8},
2873 {"news:", 6},
2874 {"wais:", 6},
2875 {"www.", 5}
2877 CHARRANGE ins_pt;
2878 int curf_ef, link_ef, def_ef;
2879 int cur_prefx, prefx_cnt;
2880 int sel_min, sel_max;
2881 int car_pos = 0;
2882 int text_pos=-1;
2883 int URLmin, URLmax = 0;
2884 CHARRANGE url;
2885 FINDTEXTA ft;
2886 CHARFORMAT2W cur_format;
2887 CHARFORMAT2W default_format;
2888 CHARFORMAT2W link;
2889 RichEditANSIWndProc(editor->hWnd, EM_EXGETSEL, (WPARAM) 0, (LPARAM) &ins_pt);
2890 sel_min = ins_pt.cpMin;
2891 sel_max = ins_pt.cpMax;
2892 if (sel_min==sel_max)
2893 car_pos = sel_min;
2894 if (sel_min!=sel_max)
2895 car_pos = ME_GetTextLength(editor)+1;
2896 cur_format.cbSize = sizeof(cur_format);
2897 default_format.cbSize = sizeof(default_format);
2898 RichEditANSIWndProc(editor->hWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM) &cur_format);
2899 RichEditANSIWndProc(editor->hWnd, EM_GETCHARFORMAT, SCF_DEFAULT, (LPARAM) &default_format);
2900 link.cbSize = sizeof(link);
2901 link.dwMask = CFM_LINK;
2902 link.dwEffects = CFE_LINK;
2903 curf_ef = cur_format.dwEffects & link.dwEffects;
2904 def_ef = default_format.dwEffects & link.dwEffects;
2905 link_ef = link.dwEffects & link.dwEffects;
2906 if (curf_ef == link_ef)
2908 if( curChar == '\n' || curChar=='\r' || curChar==' ')
2910 ME_SetSelection(editor, car_pos, car_pos);
2911 RichEditANSIWndProc(editor->hWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &default_format);
2912 text_pos=-1;
2913 return 0;
2916 if (curf_ef == def_ef)
2918 cur_prefx = 0;
2919 prefx_cnt = (sizeof(prefixes)/sizeof(struct prefix_s))-1;
2920 while (cur_prefx<=prefx_cnt)
2922 if (text_pos == -1)
2924 ft.lpstrText = prefixes[cur_prefx].text;
2925 URLmin=max(0,(car_pos-prefixes[cur_prefx].length));
2926 URLmax=max(0, car_pos);
2927 if ((car_pos == 0) && (ME_GetTextLength(editor) != 0))
2929 URLmax = ME_GetTextLength(editor)+1;
2931 ft.chrg.cpMin = URLmin;
2932 ft.chrg.cpMax = URLmax;
2933 text_pos=RichEditANSIWndProc(editor->hWnd, EM_FINDTEXT, FR_DOWN, (LPARAM)&ft);
2934 cur_prefx++;
2936 if (text_pos != -1)
2938 url.cpMin=text_pos;
2939 url.cpMax=car_pos-1;
2940 ME_SetCharFormat(editor, text_pos, (URLmax-text_pos), &link);
2941 ME_RewrapRepaint(editor);
2942 break;
2946 return 0;