riched20: Fix a memory leak.
[wine.git] / dlls / riched20 / editor.c
blob07aa4e0780368fcdbd8ce8ad3b5f2419259fd848
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
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 int me_debug = 0;
241 HANDLE me_heap = NULL;
243 static BOOL ME_ListBoxRegistered = FALSE;
244 static BOOL ME_ComboBoxRegistered = FALSE;
246 static ME_TextBuffer *ME_MakeText(void) {
248 ME_TextBuffer *buf = ALLOC_OBJ(ME_TextBuffer);
250 ME_DisplayItem *p1 = ME_MakeDI(diTextStart);
251 ME_DisplayItem *p2 = ME_MakeDI(diTextEnd);
253 p1->prev = NULL;
254 p1->next = p2;
255 p2->prev = p1;
256 p2->next = NULL;
257 p1->member.para.next_para = p2;
258 p2->member.para.prev_para = p1;
259 p2->member.para.nCharOfs = 0;
261 buf->pFirst = p1;
262 buf->pLast = p2;
263 buf->pCharStyle = NULL;
265 return buf;
269 static LRESULT ME_StreamInText(ME_TextEditor *editor, DWORD dwFormat, ME_InStream *stream, ME_Style *style)
271 WCHAR wszText[STREAMIN_BUFFER_SIZE+1];
272 WCHAR *pText;
274 TRACE("%08x %p\n", dwFormat, stream);
276 do {
277 long nWideChars = 0;
279 if (!stream->dwSize)
281 ME_StreamInFill(stream);
282 if (stream->editstream->dwError)
283 break;
284 if (!stream->dwSize)
285 break;
288 if (!(dwFormat & SF_UNICODE))
290 /* FIXME? this is doomed to fail on true MBCS like UTF-8, luckily they're unlikely to be used as CP_ACP */
291 nWideChars = MultiByteToWideChar(CP_ACP, 0, stream->buffer, stream->dwSize, wszText, STREAMIN_BUFFER_SIZE);
292 pText = wszText;
294 else
296 nWideChars = stream->dwSize >> 1;
297 pText = (WCHAR *)stream->buffer;
300 ME_InsertTextFromCursor(editor, 0, pText, nWideChars, style);
301 if (stream->dwSize == 0)
302 break;
303 stream->dwSize = 0;
304 } while(1);
305 ME_CommitUndo(editor);
306 ME_UpdateRepaint(editor);
307 return 0;
310 static void ME_RTFCharAttrHook(RTF_Info *info)
312 CHARFORMAT2W fmt;
313 fmt.cbSize = sizeof(fmt);
314 fmt.dwMask = 0;
315 fmt.dwEffects = 0;
317 switch(info->rtfMinor)
319 case rtfPlain:
320 /* FIXME add more flags once they're implemented */
321 fmt.dwMask = CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_COLOR | CFM_BACKCOLOR | CFM_SIZE | CFM_WEIGHT;
322 fmt.dwEffects = CFE_AUTOCOLOR | CFE_AUTOBACKCOLOR;
323 fmt.yHeight = 12*20; /* 12pt */
324 fmt.wWeight = 400;
325 break;
326 case rtfBold:
327 fmt.dwMask = CFM_BOLD;
328 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
329 break;
330 case rtfItalic:
331 fmt.dwMask = CFM_ITALIC;
332 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
333 break;
334 case rtfUnderline:
335 fmt.dwMask = CFM_UNDERLINE;
336 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
337 fmt.bUnderlineType = CFU_CF1UNDERLINE;
338 break;
339 case rtfNoUnderline:
340 fmt.dwMask = CFM_UNDERLINE;
341 fmt.dwEffects = 0;
342 break;
343 case rtfStrikeThru:
344 fmt.dwMask = CFM_STRIKEOUT;
345 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
346 break;
347 case rtfSubScript:
348 case rtfSuperScript:
349 case rtfSubScrShrink:
350 case rtfSuperScrShrink:
351 case rtfNoSuperSub:
352 fmt.dwMask = CFM_SUBSCRIPT|CFM_SUPERSCRIPT;
353 if (info->rtfMinor == rtfSubScrShrink) fmt.dwEffects = CFE_SUBSCRIPT;
354 if (info->rtfMinor == rtfSuperScrShrink) fmt.dwEffects = CFE_SUPERSCRIPT;
355 if (info->rtfMinor == rtfNoSuperSub) fmt.dwEffects = 0;
356 break;
357 case rtfInvisible:
358 fmt.dwMask = CFM_HIDDEN;
359 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
360 break;
361 case rtfBackColor:
362 fmt.dwMask = CFM_BACKCOLOR;
363 fmt.dwEffects = 0;
364 if (info->rtfParam == 0)
365 fmt.dwEffects = CFE_AUTOBACKCOLOR;
366 else if (info->rtfParam != rtfNoParam)
368 RTFColor *c = RTFGetColor(info, info->rtfParam);
369 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
371 break;
372 case rtfForeColor:
373 fmt.dwMask = CFM_COLOR;
374 fmt.dwEffects = 0;
375 if (info->rtfParam == 0)
376 fmt.dwEffects = CFE_AUTOCOLOR;
377 else if (info->rtfParam != rtfNoParam)
379 RTFColor *c = RTFGetColor(info, info->rtfParam);
380 if (c)
381 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
382 else
383 fmt.crTextColor = 0;
385 break;
386 case rtfFontNum:
387 if (info->rtfParam != rtfNoParam)
389 RTFFont *f = RTFGetFont(info, info->rtfParam);
390 if (f)
392 MultiByteToWideChar(CP_ACP, 0, f->rtfFName, -1, fmt.szFaceName, sizeof(fmt.szFaceName)/sizeof(WCHAR));
393 fmt.szFaceName[sizeof(fmt.szFaceName)/sizeof(WCHAR)-1] = '\0';
394 fmt.bCharSet = f->rtfFCharSet;
395 fmt.dwMask = CFM_FACE | CFM_CHARSET;
398 break;
399 case rtfFontSize:
400 fmt.dwMask = CFM_SIZE;
401 if (info->rtfParam != rtfNoParam)
402 fmt.yHeight = info->rtfParam*10;
403 break;
405 if (fmt.dwMask) {
406 ME_Style *style2;
407 RTFFlushOutputBuffer(info);
408 /* FIXME too slow ? how come ? */
409 style2 = ME_ApplyStyle(info->style, &fmt);
410 ME_ReleaseStyle(info->style);
411 info->style = style2;
412 info->styleChanged = TRUE;
416 /* FIXME this function doesn't get any information about context of the RTF tag, which is very bad,
417 the same tags mean different things in different contexts */
418 static void ME_RTFParAttrHook(RTF_Info *info)
420 PARAFORMAT2 fmt;
421 fmt.cbSize = sizeof(fmt);
422 fmt.dwMask = 0;
424 switch(info->rtfMinor)
426 case rtfParDef: /* restores default paragraph attributes */
427 fmt.dwMask = PFM_ALIGNMENT | PFM_TABSTOPS | PFM_OFFSET | PFM_STARTINDENT;
428 fmt.wAlignment = PFA_LEFT;
429 fmt.cTabCount = 0;
430 fmt.dxOffset = fmt.dxStartIndent = 0;
431 RTFFlushOutputBuffer(info);
432 ME_GetParagraph(info->editor->pCursors[0].pRun)->member.para.bTable = FALSE;
433 break;
434 case rtfInTable:
436 ME_DisplayItem *para;
438 RTFFlushOutputBuffer(info);
439 para = ME_GetParagraph(info->editor->pCursors[0].pRun);
440 assert(para->member.para.pCells);
441 para->member.para.bTable = TRUE;
442 return;
444 case rtfFirstIndent:
445 ME_GetSelectionParaFormat(info->editor, &fmt);
446 fmt.dwMask = PFM_STARTINDENT | PFM_OFFSET;
447 fmt.dxStartIndent += info->rtfParam + fmt.dxOffset;
448 fmt.dxOffset = -info->rtfParam;
449 break;
450 case rtfLeftIndent:
451 ME_GetSelectionParaFormat(info->editor, &fmt);
452 fmt.dwMask = PFM_STARTINDENT;
453 fmt.dxStartIndent = -fmt.dxOffset + info->rtfParam;
454 break;
455 case rtfRightIndent:
456 fmt.dwMask = PFM_RIGHTINDENT;
457 fmt.dxRightIndent = info->rtfParam;
458 break;
459 case rtfQuadLeft:
460 case rtfQuadJust:
461 fmt.dwMask = PFM_ALIGNMENT;
462 fmt.wAlignment = PFA_LEFT;
463 break;
464 case rtfQuadRight:
465 fmt.dwMask = PFM_ALIGNMENT;
466 fmt.wAlignment = PFA_RIGHT;
467 break;
468 case rtfQuadCenter:
469 fmt.dwMask = PFM_ALIGNMENT;
470 fmt.wAlignment = PFA_CENTER;
471 break;
472 case rtfTabPos:
473 ME_GetSelectionParaFormat(info->editor, &fmt);
474 if (!(fmt.dwMask & PFM_TABSTOPS))
476 fmt.dwMask |= PFM_TABSTOPS;
477 fmt.cTabCount = 0;
479 if (fmt.cTabCount < MAX_TAB_STOPS)
480 fmt.rgxTabs[fmt.cTabCount++] = info->rtfParam;
481 break;
483 if (fmt.dwMask) {
484 RTFFlushOutputBuffer(info);
485 /* FIXME too slow ? how come ?*/
486 ME_SetSelectionParaFormat(info->editor, &fmt);
490 static void ME_RTFTblAttrHook(RTF_Info *info)
492 ME_DisplayItem *para;
494 switch (info->rtfMinor)
496 case rtfRowDef:
497 RTFFlushOutputBuffer(info);
498 para = ME_GetParagraph(info->editor->pCursors[0].pRun);
500 /* Release possibly inherited cell definitions */
501 ME_DestroyTableCellList(para);
503 para->member.para.pCells = ALLOC_OBJ(ME_TableCell);
504 para->member.para.pCells->nRightBoundary = 0;
505 para->member.para.pCells->next = NULL;
506 para->member.para.pLastCell = para->member.para.pCells;
507 break;
508 case rtfCellPos:
509 RTFFlushOutputBuffer(info);
510 para = ME_GetParagraph(info->editor->pCursors[0].pRun);
512 if (para->member.para.pLastCell->nRightBoundary)
514 ME_TableCell *pCell = ALLOC_OBJ(ME_TableCell);
516 pCell->next = NULL;
517 para->member.para.pLastCell->next = pCell;
518 para->member.para.pLastCell = pCell;
520 para->member.para.pLastCell->nRightBoundary = info->rtfParam;
521 break;
525 static void ME_RTFReadHook(RTF_Info *info) {
526 switch(info->rtfClass)
528 case rtfGroup:
529 switch(info->rtfMajor)
531 case rtfBeginGroup:
532 if (info->stackTop < maxStack) {
533 memcpy(&info->stack[info->stackTop].fmt, &info->style->fmt, sizeof(CHARFORMAT2W));
534 info->stack[info->stackTop].codePage = info->codePage;
535 info->stack[info->stackTop].unicodeLength = info->unicodeLength;
537 info->stackTop++;
538 info->styleChanged = FALSE;
539 break;
540 case rtfEndGroup:
542 ME_Style *s;
543 RTFFlushOutputBuffer(info);
544 if (info->stackTop<=1) {
545 info->rtfClass = rtfEOF;
546 return;
548 info->stackTop--;
549 assert(info->stackTop >= 0);
550 if (info->styleChanged)
552 /* FIXME too slow ? how come ? */
553 s = ME_ApplyStyle(info->style, &info->stack[info->stackTop].fmt);
554 ME_ReleaseStyle(info->style);
555 info->style = s;
556 info->codePage = info->stack[info->stackTop].codePage;
557 info->unicodeLength = info->stack[info->stackTop].unicodeLength;
559 break;
562 break;
563 case rtfControl:
564 switch(info->rtfMajor)
566 case rtfCharAttr:
567 ME_RTFCharAttrHook(info);
568 break;
569 case rtfParAttr:
570 ME_RTFParAttrHook(info);
571 break;
572 case rtfTblAttr:
573 ME_RTFTblAttrHook(info);
574 break;
575 case rtfSpecialChar:
576 if (info->rtfMinor == rtfCell)
578 RTFFlushOutputBuffer(info);
579 ME_InsertTableCellFromCursor(info->editor, 0);
582 break;
586 void
587 ME_StreamInFill(ME_InStream *stream)
589 stream->editstream->dwError = stream->editstream->pfnCallback(stream->editstream->dwCookie,
590 (BYTE *)stream->buffer,
591 sizeof(stream->buffer),
592 (LONG *)&stream->dwSize);
593 stream->dwUsed = 0;
596 static LRESULT ME_StreamIn(ME_TextEditor *editor, DWORD format, EDITSTREAM *stream)
598 RTF_Info parser;
599 ME_Style *style;
600 int from, to, to2, nUndoMode;
601 ME_UndoItem *pUI;
602 int nEventMask = editor->nEventMask;
603 ME_InStream inStream;
605 TRACE("stream==%p hWnd==%p format==0x%X\n", stream, editor->hWnd, (UINT)format);
606 editor->nEventMask = 0;
608 ME_GetSelection(editor, &from, &to);
609 if ((format & SFF_SELECTION) && (editor->mode & TM_RICHTEXT)) {
610 style = ME_GetSelectionInsertStyle(editor);
612 ME_InternalDeleteText(editor, from, to-from);
614 else {
615 style = editor->pBuffer->pDefaultStyle;
616 ME_AddRefStyle(style);
617 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
618 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
619 from = to = 0;
620 ME_ClearTempStyle(editor);
621 /* FIXME restore default paragraph formatting ! */
624 nUndoMode = editor->nUndoMode;
625 editor->nUndoMode = umIgnore;
627 inStream.editstream = stream;
628 inStream.editstream->dwError = 0;
629 inStream.dwSize = 0;
630 inStream.dwUsed = 0;
632 if (format & SF_RTF)
634 /* Check if it's really RTF, and if it is not, use plain text */
635 ME_StreamInFill(&inStream);
636 if (!inStream.editstream->dwError)
638 if (strncmp(inStream.buffer, "{\\rtf", 5) && strncmp(inStream.buffer, "{\\urtf", 6))
640 format &= ~SF_RTF;
641 format |= SF_TEXT;
646 if (!inStream.editstream->dwError)
648 if (format & SF_RTF) {
649 /* setup the RTF parser */
650 memset(&parser, 0, sizeof parser);
651 RTFSetEditStream(&parser, &inStream);
652 parser.rtfFormat = format&(SF_TEXT|SF_RTF);
653 parser.hwndEdit = editor->hWnd;
654 parser.editor = editor;
655 parser.style = style;
656 WriterInit(&parser);
657 RTFInit(&parser);
658 RTFSetReadHook(&parser, ME_RTFReadHook);
659 BeginFile(&parser);
661 /* do the parsing */
662 RTFRead(&parser);
663 RTFFlushOutputBuffer(&parser);
664 RTFDestroy(&parser);
666 style = parser.style;
668 else if (format & SF_TEXT)
669 ME_StreamInText(editor, format, &inStream, style);
670 else
671 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
672 ME_GetSelection(editor, &to, &to2);
673 /* put the cursor at the top */
674 if (!(format & SFF_SELECTION))
675 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
678 editor->nUndoMode = nUndoMode;
679 if (format & SFF_SELECTION)
681 pUI = ME_AddUndoItem(editor, diUndoDeleteRun, NULL);
682 TRACE("from %d to %d\n", from, to);
683 if (pUI && from < to)
685 pUI->nStart = from;
686 pUI->nLen = to-from;
688 ME_CommitUndo(editor);
690 else
692 ME_EmptyUndoStack(editor);
695 ME_ReleaseStyle(style);
696 editor->nEventMask = nEventMask;
697 if (editor->bRedraw)
699 ME_UpdateRepaint(editor);
701 if (!(format & SFF_SELECTION)) {
702 ME_ClearTempStyle(editor);
704 ME_MoveCaret(editor);
705 ME_SendSelChange(editor);
706 ME_SendRequestResize(editor, FALSE);
708 return 0;
712 typedef struct tagME_RTFStringStreamStruct
714 char *string;
715 int pos;
716 int length;
717 } ME_RTFStringStreamStruct;
719 static DWORD CALLBACK ME_ReadFromRTFString(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
721 ME_RTFStringStreamStruct *pStruct = (ME_RTFStringStreamStruct *)dwCookie;
722 int count;
724 count = min(cb, pStruct->length - pStruct->pos);
725 memmove(lpBuff, pStruct->string + pStruct->pos, count);
726 pStruct->pos += count;
727 *pcb = count;
728 return 0;
731 static void
732 ME_StreamInRTFString(ME_TextEditor *editor, BOOL selection, char *string)
734 EDITSTREAM es;
735 ME_RTFStringStreamStruct data;
737 data.string = string;
738 data.length = strlen(string);
739 data.pos = 0;
740 es.dwCookie = (DWORD)&data;
741 es.pfnCallback = ME_ReadFromRTFString;
742 ME_StreamIn(editor, SF_RTF | (selection ? SFF_SELECTION : 0), &es);
746 ME_DisplayItem *
747 ME_FindItemAtOffset(ME_TextEditor *editor, ME_DIType nItemType, int nOffset, int *nItemOffset)
749 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
751 while (item && item->member.para.next_para->member.para.nCharOfs <= nOffset)
752 item = ME_FindItemFwd(item, diParagraph);
754 if (!item)
755 return item;
757 nOffset -= item->member.para.nCharOfs;
758 if (nItemType == diParagraph) {
759 if (nItemOffset)
760 *nItemOffset = nOffset;
761 return item;
764 do {
765 item = ME_FindItemFwd(item, diRun);
766 } while (item && (item->member.run.nCharOfs + ME_StrLen(item->member.run.strText) <= nOffset));
767 if (item) {
768 nOffset -= item->member.run.nCharOfs;
769 if (nItemOffset)
770 *nItemOffset = nOffset;
772 return item;
776 static int
777 ME_FindText(ME_TextEditor *editor, DWORD flags, CHARRANGE *chrg, const WCHAR *text, CHARRANGE *chrgText)
779 const int nLen = lstrlenW(text);
780 const int nTextLen = ME_GetTextLength(editor);
781 int nStart, nEnd;
782 int nMin, nMax;
783 ME_DisplayItem *item;
784 ME_DisplayItem *para;
785 WCHAR wLastChar = ' ';
787 TRACE("flags==0x%08x, chrg->cpMin==%d, chrg->cpMax==%d text==%s\n",
788 flags, chrg->cpMin, chrg->cpMax, debugstr_w(text));
790 if (flags & ~(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD))
791 FIXME("Flags 0x%08x not implemented\n",
792 flags & ~(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD));
794 nMin = chrg->cpMin;
795 if (chrg->cpMax == -1)
796 nMax = nTextLen;
797 else
798 nMax = chrg->cpMax > nTextLen ? nTextLen : chrg->cpMax;
800 /* when searching up, if cpMin < cpMax, then instead of searching
801 * on [cpMin,cpMax], we search on [0,cpMin], otherwise, search on
802 * [cpMax, cpMin]. The exception is when cpMax is -1, in which
803 * case, it is always bigger than cpMin.
805 if (!(flags & FR_DOWN))
807 int nSwap = nMax;
809 nMax = nMin > nTextLen ? nTextLen : nMin;
810 if (nMin < nSwap || chrg->cpMax == -1)
811 nMin = 0;
812 else
813 nMin = nSwap;
816 if (!nLen || nMin < 0 || nMax < 0 || nMax < nMin)
818 if (chrgText)
819 chrgText->cpMin = chrgText->cpMax = -1;
820 return -1;
823 if (flags & FR_DOWN) /* Forward search */
825 /* If possible, find the character before where the search starts */
826 if ((flags & FR_WHOLEWORD) && nMin)
828 nStart = nMin - 1;
829 item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
830 if (!item)
832 if (chrgText)
833 chrgText->cpMin = chrgText->cpMax = -1;
834 return -1;
836 wLastChar = item->member.run.strText->szData[nStart];
839 nStart = nMin;
840 item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
841 if (!item)
843 if (chrgText)
844 chrgText->cpMin = chrgText->cpMax = -1;
845 return -1;
848 para = ME_GetParagraph(item);
849 while (item
850 && para->member.para.nCharOfs + item->member.run.nCharOfs + nStart + nLen <= nMax)
852 ME_DisplayItem *pCurItem = item;
853 int nCurStart = nStart;
854 int nMatched = 0;
856 while (pCurItem && ME_CharCompare(pCurItem->member.run.strText->szData[nCurStart + nMatched], text[nMatched], (flags & FR_MATCHCASE)))
858 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
859 break;
861 nMatched++;
862 if (nMatched == nLen)
864 ME_DisplayItem *pNextItem = pCurItem;
865 int nNextStart = nCurStart;
866 WCHAR wNextChar;
868 /* Check to see if next character is a whitespace */
869 if (flags & FR_WHOLEWORD)
871 if (nCurStart + nMatched == ME_StrLen(pCurItem->member.run.strText))
873 pNextItem = ME_FindItemFwd(pCurItem, diRun);
874 nNextStart = -nMatched;
877 if (pNextItem)
878 wNextChar = pNextItem->member.run.strText->szData[nNextStart + nMatched];
879 else
880 wNextChar = ' ';
882 if (isalnumW(wNextChar))
883 break;
886 nStart += para->member.para.nCharOfs + pCurItem->member.run.nCharOfs;
887 if (chrgText)
889 chrgText->cpMin = nStart;
890 chrgText->cpMax = nStart + nLen;
892 TRACE("found at %d-%d\n", nStart, nStart + nLen);
893 return nStart;
895 if (nCurStart + nMatched == ME_StrLen(pCurItem->member.run.strText))
897 pCurItem = ME_FindItemFwd(pCurItem, diRun);
898 para = ME_GetParagraph(pCurItem);
899 nCurStart = -nMatched;
902 if (pCurItem)
903 wLastChar = pCurItem->member.run.strText->szData[nCurStart + nMatched];
904 else
905 wLastChar = ' ';
907 nStart++;
908 if (nStart == ME_StrLen(item->member.run.strText))
910 item = ME_FindItemFwd(item, diRun);
911 para = ME_GetParagraph(item);
912 nStart = 0;
916 else /* Backward search */
918 /* If possible, find the character after where the search ends */
919 if ((flags & FR_WHOLEWORD) && nMax < nTextLen - 1)
921 nEnd = nMax + 1;
922 item = ME_FindItemAtOffset(editor, diRun, nEnd, &nEnd);
923 if (!item)
925 if (chrgText)
926 chrgText->cpMin = chrgText->cpMax = -1;
927 return -1;
929 wLastChar = item->member.run.strText->szData[nEnd];
932 nEnd = nMax;
933 item = ME_FindItemAtOffset(editor, diRun, nEnd, &nEnd);
934 if (!item)
936 if (chrgText)
937 chrgText->cpMin = chrgText->cpMax = -1;
938 return -1;
941 para = ME_GetParagraph(item);
943 while (item
944 && para->member.para.nCharOfs + item->member.run.nCharOfs + nEnd - nLen >= nMin)
946 ME_DisplayItem *pCurItem = item;
947 int nCurEnd = nEnd;
948 int nMatched = 0;
950 if (nCurEnd - nMatched == 0)
952 pCurItem = ME_FindItemBack(pCurItem, diRun);
953 para = ME_GetParagraph(pCurItem);
954 nCurEnd = ME_StrLen(pCurItem->member.run.strText) + nMatched;
957 while (pCurItem && ME_CharCompare(pCurItem->member.run.strText->szData[nCurEnd - nMatched - 1], text[nLen - nMatched - 1], (flags & FR_MATCHCASE)))
959 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
960 break;
962 nMatched++;
963 if (nMatched == nLen)
965 ME_DisplayItem *pPrevItem = pCurItem;
966 int nPrevEnd = nCurEnd;
967 WCHAR wPrevChar;
969 /* Check to see if previous character is a whitespace */
970 if (flags & FR_WHOLEWORD)
972 if (nPrevEnd - nMatched == 0)
974 pPrevItem = ME_FindItemBack(pCurItem, diRun);
975 if (pPrevItem)
976 nPrevEnd = ME_StrLen(pPrevItem->member.run.strText) + nMatched;
979 if (pPrevItem)
980 wPrevChar = pPrevItem->member.run.strText->szData[nPrevEnd - nMatched - 1];
981 else
982 wPrevChar = ' ';
984 if (isalnumW(wPrevChar))
985 break;
988 nStart = para->member.para.nCharOfs + pCurItem->member.run.nCharOfs + nCurEnd - nMatched;
989 if (chrgText)
991 chrgText->cpMin = nStart;
992 chrgText->cpMax = nStart + nLen;
994 TRACE("found at %d-%d\n", nStart, nStart + nLen);
995 return nStart;
997 if (nCurEnd - nMatched == 0)
999 pCurItem = ME_FindItemBack(pCurItem, diRun);
1000 /* Don't care about pCurItem becoming NULL here; it's already taken
1001 * care of in the exterior loop condition */
1002 para = ME_GetParagraph(pCurItem);
1003 nCurEnd = ME_StrLen(pCurItem->member.run.strText) + nMatched;
1006 if (pCurItem)
1007 wLastChar = pCurItem->member.run.strText->szData[nCurEnd - nMatched - 1];
1008 else
1009 wLastChar = ' ';
1011 nEnd--;
1012 if (nEnd < 0)
1014 item = ME_FindItemBack(item, diRun);
1015 para = ME_GetParagraph(item);
1016 nEnd = ME_StrLen(item->member.run.strText);
1020 TRACE("not found\n");
1021 if (chrgText)
1022 chrgText->cpMin = chrgText->cpMax = -1;
1023 return -1;
1027 static BOOL
1028 ME_KeyDown(ME_TextEditor *editor, WORD nKey)
1030 BOOL ctrl_is_down = GetKeyState(VK_CONTROL) & 0x8000;
1031 BOOL shift_is_down = GetKeyState(VK_SHIFT) & 0x8000;
1033 switch (nKey)
1035 case VK_LEFT:
1036 case VK_RIGHT:
1037 case VK_UP:
1038 case VK_DOWN:
1039 case VK_HOME:
1040 case VK_END:
1041 case VK_PRIOR:
1042 case VK_NEXT:
1043 ME_ArrowKey(editor, nKey, shift_is_down, ctrl_is_down);
1044 return TRUE;
1045 case VK_BACK:
1046 case VK_DELETE:
1047 /* FIXME backspace and delete aren't the same, they act different wrt paragraph style of the merged paragraph */
1048 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY)
1049 return FALSE;
1050 if (ME_IsSelection(editor))
1051 ME_DeleteSelection(editor);
1052 else if (nKey == VK_DELETE || ME_ArrowKey(editor, VK_LEFT, FALSE, FALSE))
1053 ME_DeleteTextAtCursor(editor, 1, 1);
1054 else
1055 return TRUE;
1056 ME_CommitUndo(editor);
1057 ME_UpdateRepaint(editor);
1058 ME_SendRequestResize(editor, FALSE);
1059 return TRUE;
1061 default:
1062 if (ctrl_is_down)
1064 if (nKey == 'W')
1066 CHARFORMAT2W chf;
1067 char buf[2048];
1068 chf.cbSize = sizeof(chf);
1070 ME_GetSelectionCharFormat(editor, &chf);
1071 ME_DumpStyleToBuf(&chf, buf);
1072 MessageBoxA(NULL, buf, "Style dump", MB_OK);
1074 if (nKey == 'Q')
1076 ME_CheckCharOffsets(editor);
1080 return FALSE;
1083 static BOOL ME_ShowContextMenu(ME_TextEditor *editor, int x, int y)
1085 CHARRANGE selrange;
1086 HMENU menu;
1087 int seltype = 0;
1088 if(!editor->lpOleCallback)
1089 return FALSE;
1090 ME_GetSelection(editor, (int *)&selrange.cpMin, (int *)&selrange.cpMax);
1091 if(selrange.cpMin == selrange.cpMax)
1092 seltype |= SEL_EMPTY;
1093 else
1095 /* FIXME: Handle objects */
1096 seltype |= SEL_TEXT;
1097 if(selrange.cpMax-selrange.cpMin > 1)
1098 seltype |= SEL_MULTICHAR;
1100 if(SUCCEEDED(IRichEditOleCallback_GetContextMenu(editor->lpOleCallback, seltype, NULL, &selrange, &menu)))
1102 TrackPopupMenu(menu, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, GetParent(editor->hWnd), NULL);
1103 DestroyMenu(menu);
1105 return TRUE;
1108 ME_TextEditor *ME_MakeEditor(HWND hWnd) {
1109 ME_TextEditor *ed = ALLOC_OBJ(ME_TextEditor);
1110 HDC hDC;
1111 int i;
1112 ed->hWnd = hWnd;
1113 ed->bEmulateVersion10 = FALSE;
1114 ed->pBuffer = ME_MakeText();
1115 hDC = GetDC(hWnd);
1116 ME_MakeFirstParagraph(hDC, ed->pBuffer);
1117 ReleaseDC(hWnd, hDC);
1118 ed->bCaretShown = FALSE;
1119 ed->nCursors = 2;
1120 ed->pCursors = ALLOC_N_OBJ(ME_Cursor, ed->nCursors);
1121 ed->pCursors[0].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
1122 ed->pCursors[0].nOffset = 0;
1123 ed->pCursors[1].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
1124 ed->pCursors[1].nOffset = 0;
1125 ed->nLastTotalLength = ed->nTotalLength = 0;
1126 ed->nUDArrowX = -1;
1127 ed->nSequence = 0;
1128 ed->rgbBackColor = -1;
1129 ed->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
1130 ed->bCaretAtEnd = FALSE;
1131 ed->nEventMask = 0;
1132 ed->nModifyStep = 0;
1133 ed->nTextLimit = TEXT_LIMIT_DEFAULT;
1134 ed->pUndoStack = ed->pRedoStack = ed->pUndoStackBottom = NULL;
1135 ed->nUndoStackSize = 0;
1136 ed->nUndoLimit = STACK_SIZE_DEFAULT;
1137 ed->nUndoMode = umAddToUndo;
1138 ed->nParagraphs = 1;
1139 ed->nLastSelStart = ed->nLastSelEnd = 0;
1140 ed->pLastSelStartPara = ed->pLastSelEndPara = ME_FindItemFwd(ed->pBuffer->pFirst, diParagraph);
1141 ed->nZoomNumerator = ed->nZoomDenominator = 0;
1142 ed->bRedraw = TRUE;
1143 ed->bHideSelection = FALSE;
1144 ed->nInvalidOfs = -1;
1145 ed->pfnWordBreak = NULL;
1146 ed->lpOleCallback = NULL;
1147 ed->mode = TM_RICHTEXT | TM_MULTILEVELUNDO | TM_MULTICODEPAGE;
1148 ed->AutoURLDetect_bEnable = FALSE;
1149 GetClientRect(hWnd, &ed->rcFormat);
1150 for (i=0; i<HFONT_CACHE_SIZE; i++)
1152 ed->pFontCache[i].nRefs = 0;
1153 ed->pFontCache[i].nAge = 0;
1154 ed->pFontCache[i].hFont = NULL;
1157 ME_CheckCharOffsets(ed);
1159 if (GetWindowLongW(hWnd, GWL_STYLE) & ES_PASSWORD)
1160 ed->cPasswordMask = '*';
1161 else
1162 ed->cPasswordMask = 0;
1164 return ed;
1167 typedef struct tagME_GlobalDestStruct
1169 HGLOBAL hData;
1170 int nLength;
1171 } ME_GlobalDestStruct;
1173 static DWORD CALLBACK ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1175 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
1176 int i;
1177 WORD *pSrc, *pDest;
1179 cb = cb >> 1;
1180 pDest = (WORD *)lpBuff;
1181 pSrc = (WORD *)GlobalLock(pData->hData);
1182 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
1183 pDest[i] = pSrc[pData->nLength+i];
1185 pData->nLength += i;
1186 *pcb = 2*i;
1187 GlobalUnlock(pData->hData);
1188 return 0;
1191 static DWORD CALLBACK ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1193 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
1194 int i;
1195 BYTE *pSrc, *pDest;
1197 pDest = lpBuff;
1198 pSrc = (BYTE *)GlobalLock(pData->hData);
1199 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
1200 pDest[i] = pSrc[pData->nLength+i];
1202 pData->nLength += i;
1203 *pcb = i;
1204 GlobalUnlock(pData->hData);
1205 return 0;
1209 void ME_DestroyEditor(ME_TextEditor *editor)
1211 ME_DisplayItem *pFirst = editor->pBuffer->pFirst;
1212 ME_DisplayItem *p = pFirst, *pNext = NULL;
1213 int i;
1215 ME_ClearTempStyle(editor);
1216 ME_EmptyUndoStack(editor);
1217 while(p) {
1218 pNext = p->next;
1219 ME_DestroyDisplayItem(p);
1220 p = pNext;
1222 ME_ReleaseStyle(editor->pBuffer->pDefaultStyle);
1223 for (i=0; i<HFONT_CACHE_SIZE; i++)
1225 if (editor->pFontCache[i].hFont)
1226 DeleteObject(editor->pFontCache[i].hFont);
1228 DeleteObject(editor->hbrBackground);
1229 if(editor->lpOleCallback)
1230 IUnknown_Release(editor->lpOleCallback);
1232 FREE_OBJ(editor->pBuffer);
1233 FREE_OBJ(editor->pCursors);
1235 FREE_OBJ(editor);
1238 static const WCHAR wszClassName[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '2', '0', 'W', 0};
1239 static const WCHAR wszClassName50[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '5', '0', 'W', 0};
1240 static const WCHAR wszClassNameListBox[] = {'R','E','L','i','s','t','B','o','x','2','0','W', 0};
1241 static const WCHAR wszClassNameComboBox[] = {'R','E','C','o','m','b','o','B','o','x','2','0','W', 0};
1243 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
1245 TRACE("\n");
1246 switch (fdwReason)
1248 case DLL_PROCESS_ATTACH:
1249 DisableThreadLibraryCalls(hinstDLL);
1250 me_heap = HeapCreate (0, 0x10000, 0);
1251 ME_RegisterEditorClass(hinstDLL);
1252 LookupInit();
1253 break;
1255 case DLL_PROCESS_DETACH:
1256 UnregisterClassW(wszClassName, 0);
1257 UnregisterClassW(wszClassName50, 0);
1258 UnregisterClassA("RichEdit20A", 0);
1259 UnregisterClassA("RichEdit50A", 0);
1260 if (ME_ListBoxRegistered)
1261 UnregisterClassW(wszClassNameListBox, 0);
1262 if (ME_ComboBoxRegistered)
1263 UnregisterClassW(wszClassNameComboBox, 0);
1264 LookupCleanup();
1265 HeapDestroy (me_heap);
1266 me_heap = NULL;
1267 break;
1269 return TRUE;
1273 #define UNSUPPORTED_MSG(e) \
1274 case e: \
1275 FIXME(#e ": stub\n"); \
1276 return DefWindowProcW(hWnd, msg, wParam, lParam);
1278 static const char * const edit_messages[] = {
1279 "EM_GETSEL",
1280 "EM_SETSEL",
1281 "EM_GETRECT",
1282 "EM_SETRECT",
1283 "EM_SETRECTNP",
1284 "EM_SCROLL",
1285 "EM_LINESCROLL",
1286 "EM_SCROLLCARET",
1287 "EM_GETMODIFY",
1288 "EM_SETMODIFY",
1289 "EM_GETLINECOUNT",
1290 "EM_LINEINDEX",
1291 "EM_SETHANDLE",
1292 "EM_GETHANDLE",
1293 "EM_GETTHUMB",
1294 "EM_UNKNOWN_BF",
1295 "EM_UNKNOWN_C0",
1296 "EM_LINELENGTH",
1297 "EM_REPLACESEL",
1298 "EM_UNKNOWN_C3",
1299 "EM_GETLINE",
1300 "EM_LIMITTEXT",
1301 "EM_CANUNDO",
1302 "EM_UNDO",
1303 "EM_FMTLINES",
1304 "EM_LINEFROMCHAR",
1305 "EM_UNKNOWN_CA",
1306 "EM_SETTABSTOPS",
1307 "EM_SETPASSWORDCHAR",
1308 "EM_EMPTYUNDOBUFFER",
1309 "EM_GETFIRSTVISIBLELINE",
1310 "EM_SETREADONLY",
1311 "EM_SETWORDBREAKPROC",
1312 "EM_GETWORDBREAKPROC",
1313 "EM_GETPASSWORDCHAR",
1314 "EM_SETMARGINS",
1315 "EM_GETMARGINS",
1316 "EM_GETLIMITTEXT",
1317 "EM_POSFROMCHAR",
1318 "EM_CHARFROMPOS"
1321 static const char * const richedit_messages[] = {
1322 "EM_CANPASTE",
1323 "EM_DISPLAYBAND",
1324 "EM_EXGETSEL",
1325 "EM_EXLIMITTEXT",
1326 "EM_EXLINEFROMCHAR",
1327 "EM_EXSETSEL",
1328 "EM_FINDTEXT",
1329 "EM_FORMATRANGE",
1330 "EM_GETCHARFORMAT",
1331 "EM_GETEVENTMASK",
1332 "EM_GETOLEINTERFACE",
1333 "EM_GETPARAFORMAT",
1334 "EM_GETSELTEXT",
1335 "EM_HIDESELECTION",
1336 "EM_PASTESPECIAL",
1337 "EM_REQUESTRESIZE",
1338 "EM_SELECTIONTYPE",
1339 "EM_SETBKGNDCOLOR",
1340 "EM_SETCHARFORMAT",
1341 "EM_SETEVENTMASK",
1342 "EM_SETOLECALLBACK",
1343 "EM_SETPARAFORMAT",
1344 "EM_SETTARGETDEVICE",
1345 "EM_STREAMIN",
1346 "EM_STREAMOUT",
1347 "EM_GETTEXTRANGE",
1348 "EM_FINDWORDBREAK",
1349 "EM_SETOPTIONS",
1350 "EM_GETOPTIONS",
1351 "EM_FINDTEXTEX",
1352 "EM_GETWORDBREAKPROCEX",
1353 "EM_SETWORDBREAKPROCEX",
1354 "EM_SETUNDOLIMIT",
1355 "EM_UNKNOWN_USER_83",
1356 "EM_REDO",
1357 "EM_CANREDO",
1358 "EM_GETUNDONAME",
1359 "EM_GETREDONAME",
1360 "EM_STOPGROUPTYPING",
1361 "EM_SETTEXTMODE",
1362 "EM_GETTEXTMODE",
1363 "EM_AUTOURLDETECT",
1364 "EM_GETAUTOURLDETECT",
1365 "EM_SETPALETTE",
1366 "EM_GETTEXTEX",
1367 "EM_GETTEXTLENGTHEX",
1368 "EM_SHOWSCROLLBAR",
1369 "EM_SETTEXTEX",
1370 "EM_UNKNOWN_USER_98",
1371 "EM_UNKNOWN_USER_99",
1372 "EM_SETPUNCTUATION",
1373 "EM_GETPUNCTUATION",
1374 "EM_SETWORDWRAPMODE",
1375 "EM_GETWORDWRAPMODE",
1376 "EM_SETIMECOLOR",
1377 "EM_GETIMECOLOR",
1378 "EM_SETIMEOPTIONS",
1379 "EM_GETIMEOPTIONS",
1380 "EM_CONVPOSITION",
1381 "EM_UNKNOWN_USER_109",
1382 "EM_UNKNOWN_USER_110",
1383 "EM_UNKNOWN_USER_111",
1384 "EM_UNKNOWN_USER_112",
1385 "EM_UNKNOWN_USER_113",
1386 "EM_UNKNOWN_USER_114",
1387 "EM_UNKNOWN_USER_115",
1388 "EM_UNKNOWN_USER_116",
1389 "EM_UNKNOWN_USER_117",
1390 "EM_UNKNOWN_USER_118",
1391 "EM_UNKNOWN_USER_119",
1392 "EM_SETLANGOPTIONS",
1393 "EM_GETLANGOPTIONS",
1394 "EM_GETIMECOMPMODE",
1395 "EM_FINDTEXTW",
1396 "EM_FINDTEXTEXW",
1397 "EM_RECONVERSION",
1398 "EM_SETIMEMODEBIAS",
1399 "EM_GETIMEMODEBIAS"
1402 static const char *
1403 get_msg_name(UINT msg)
1405 if (msg >= EM_GETSEL && msg <= EM_CHARFROMPOS)
1406 return edit_messages[msg - EM_GETSEL];
1407 if (msg >= EM_CANPASTE && msg <= EM_GETIMEMODEBIAS)
1408 return richedit_messages[msg - EM_CANPASTE];
1409 return "";
1412 /******************************************************************
1413 * RichEditANSIWndProc (RICHED20.10)
1415 LRESULT WINAPI RichEditANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
1416 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongPtrW(hWnd, 0);
1418 TRACE("hWnd %p msg %04x (%s) %08x %08lx\n",
1419 hWnd, msg, get_msg_name(msg), wParam, lParam);
1421 if (!editor && msg != WM_NCCREATE && msg != WM_NCDESTROY) {
1422 ERR("RichEditANSIWndProc called with invalid hWnd %p - application bug?\n", hWnd);
1423 return 0;
1426 switch(msg) {
1428 UNSUPPORTED_MSG(EM_DISPLAYBAND)
1429 UNSUPPORTED_MSG(EM_FINDWORDBREAK)
1430 UNSUPPORTED_MSG(EM_FMTLINES)
1431 UNSUPPORTED_MSG(EM_FORMATRANGE)
1432 UNSUPPORTED_MSG(EM_GETBIDIOPTIONS)
1433 UNSUPPORTED_MSG(EM_GETEDITSTYLE)
1434 UNSUPPORTED_MSG(EM_GETIMECOMPMODE)
1435 /* UNSUPPORTED_MSG(EM_GETIMESTATUS) missing in Wine headers */
1436 UNSUPPORTED_MSG(EM_GETLANGOPTIONS)
1437 /* UNSUPPORTED_MSG(EM_GETOLEINTERFACE) separate stub */
1438 UNSUPPORTED_MSG(EM_GETREDONAME)
1439 UNSUPPORTED_MSG(EM_GETTEXTMODE)
1440 UNSUPPORTED_MSG(EM_GETTYPOGRAPHYOPTIONS)
1441 UNSUPPORTED_MSG(EM_GETUNDONAME)
1442 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX)
1443 UNSUPPORTED_MSG(EM_LIMITTEXT) /* also known as EM_SETLIMITTEXT */
1444 UNSUPPORTED_MSG(EM_PASTESPECIAL)
1445 UNSUPPORTED_MSG(EM_SELECTIONTYPE)
1446 UNSUPPORTED_MSG(EM_SETBIDIOPTIONS)
1447 UNSUPPORTED_MSG(EM_SETEDITSTYLE)
1448 UNSUPPORTED_MSG(EM_SETFONTSIZE)
1449 UNSUPPORTED_MSG(EM_SETLANGOPTIONS)
1450 UNSUPPORTED_MSG(EM_SETPALETTE)
1451 UNSUPPORTED_MSG(EM_SETTABSTOPS)
1452 UNSUPPORTED_MSG(EM_SETTARGETDEVICE)
1453 UNSUPPORTED_MSG(EM_SETTYPOGRAPHYOPTIONS)
1454 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX)
1455 UNSUPPORTED_MSG(WM_STYLECHANGING)
1456 UNSUPPORTED_MSG(WM_STYLECHANGED)
1457 /* UNSUPPORTED_MSG(WM_UNICHAR) FIXME missing in Wine headers */
1459 /* Messages specific to Richedit controls */
1461 case EM_STREAMIN:
1462 return ME_StreamIn(editor, wParam, (EDITSTREAM*)lParam);
1463 case EM_STREAMOUT:
1464 return ME_StreamOut(editor, wParam, (EDITSTREAM *)lParam);
1465 case WM_GETDLGCODE:
1467 UINT code = DLGC_WANTCHARS|DLGC_WANTARROWS;
1468 if (GetWindowLongW(hWnd, GWL_STYLE)&ES_WANTRETURN)
1469 code |= 0; /* FIXME what can we do here ? ask for messages and censor them ? */
1470 return code;
1472 case WM_NCCREATE:
1474 CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
1475 TRACE("WM_NCCREATE: style 0x%08x\n", pcs->style);
1476 editor = ME_MakeEditor(hWnd);
1477 SetWindowLongPtrW(hWnd, 0, (LONG_PTR)editor);
1478 pcs = 0; /* ignore */
1479 return TRUE;
1481 case EM_EMPTYUNDOBUFFER:
1482 ME_EmptyUndoStack(editor);
1483 return 0;
1484 case EM_GETSEL:
1486 /* Note: wParam/lParam can be NULL */
1487 UINT from, to;
1488 PUINT pfrom = wParam ? (PUINT)wParam : &from;
1489 PUINT pto = lParam ? (PUINT)lParam : &to;
1490 ME_GetSelection(editor, (int *)pfrom, (int *)pto);
1491 if ((*pfrom|*pto) & 0xFFFF0000)
1492 return -1;
1493 return MAKELONG(*pfrom,*pto);
1495 case EM_EXGETSEL:
1497 CHARRANGE *pRange = (CHARRANGE *)lParam;
1498 ME_GetSelection(editor, (int *)&pRange->cpMin, (int *)&pRange->cpMax);
1499 TRACE("EM_EXGETSEL = (%d,%d)\n", pRange->cpMin, pRange->cpMax);
1500 return 0;
1502 case EM_SETUNDOLIMIT:
1504 if ((int)wParam < 0)
1505 editor->nUndoLimit = STACK_SIZE_DEFAULT;
1506 else
1507 editor->nUndoLimit = min(wParam, STACK_SIZE_MAX);
1508 /* Setting a max stack size keeps wine from getting killed
1509 for hogging memory. Windows allocates all this memory at once, so
1510 no program would realistically set a value above our maxiumum. */
1511 return editor->nUndoLimit;
1513 case EM_CANUNDO:
1514 return editor->pUndoStack != NULL;
1515 case EM_CANREDO:
1516 return editor->pRedoStack != NULL;
1517 case WM_UNDO: /* FIXME: actually not the same */
1518 case EM_UNDO:
1519 ME_Undo(editor);
1520 return 0;
1521 case EM_REDO:
1522 ME_Redo(editor);
1523 return 0;
1524 case EM_GETOPTIONS:
1526 /* these flags are equivalent to the ES_* counterparts */
1527 DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
1528 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN;
1529 DWORD settings = GetWindowLongW(hWnd, GWL_STYLE) & mask;
1531 return settings;
1533 case EM_SETOPTIONS:
1535 /* these flags are equivalent to ES_* counterparts
1536 * ECO_READONLY is already implemented in the code, only requires
1537 * setting the bit to work
1539 DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
1540 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN;
1541 DWORD raw = GetWindowLongW(hWnd, GWL_STYLE);
1542 DWORD settings = mask & raw;
1544 switch(wParam)
1546 case ECOOP_SET:
1547 settings = lParam;
1548 break;
1549 case ECOOP_OR:
1550 settings |= lParam;
1551 break;
1552 case ECOOP_AND:
1553 settings &= lParam;
1554 break;
1555 case ECOOP_XOR:
1556 settings ^= lParam;
1558 SetWindowLongW(hWnd, GWL_STYLE, (raw & ~mask) | (settings & mask));
1560 if (lParam & ECO_AUTOWORDSELECTION)
1561 FIXME("ECO_AUTOWORDSELECTION not implemented yet!\n");
1562 if (lParam & ECO_SELECTIONBAR)
1563 FIXME("ECO_SELECTIONBAR not implemented yet!\n");
1564 if (lParam & ECO_VERTICAL)
1565 FIXME("ECO_VERTICAL not implemented yet!\n");
1566 if (lParam & ECO_AUTOHSCROLL)
1567 FIXME("ECO_AUTOHSCROLL not implemented yet!\n");
1568 if (lParam & ECO_AUTOVSCROLL)
1569 FIXME("ECO_AUTOVSCROLL not implemented yet!\n");
1570 if (lParam & ECO_NOHIDESEL)
1571 FIXME("ECO_NOHIDESEL not implemented yet!\n");
1572 if (lParam & ECO_WANTRETURN)
1573 FIXME("ECO_WANTRETURN not implemented yet!\n");
1575 return settings;
1577 case EM_SETSEL:
1579 ME_InvalidateSelection(editor);
1580 ME_SetSelection(editor, wParam, lParam);
1581 ME_InvalidateSelection(editor);
1582 ME_SendSelChange(editor);
1583 return 0;
1585 case EM_SETSCROLLPOS:
1587 POINT *point = (POINT *)lParam;
1588 ME_ScrollAbs(editor, point->y);
1589 return 0;
1591 case EM_AUTOURLDETECT:
1593 if (wParam==1 || wParam ==0)
1595 editor->AutoURLDetect_bEnable = (BOOL)wParam;
1596 return 0;
1598 return E_INVALIDARG;
1600 case EM_GETAUTOURLDETECT:
1602 return editor->AutoURLDetect_bEnable;
1604 case EM_EXSETSEL:
1606 int start, end;
1607 CHARRANGE range = *(CHARRANGE *)lParam;
1609 TRACE("EM_EXSETSEL (%d,%d)\n", range.cpMin, range.cpMax);
1611 /* if cpMin < 0, then selection is deselected and caret moved to end of
1612 * the current selection */
1613 if (range.cpMin < 0)
1615 ME_GetSelection(editor, &start, &end);
1616 range.cpMin = end;
1617 range.cpMax = end;
1619 else if (range.cpMax > ME_GetTextLength(editor) +1)
1621 range.cpMax = ME_GetTextLength(editor) + 1;
1624 ME_InvalidateSelection(editor);
1625 ME_SetSelection(editor, range.cpMin, range.cpMax);
1626 ME_InvalidateSelection(editor);
1627 ME_SendSelChange(editor);
1629 return range.cpMax;
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 TRACE("EM_SETTEXTEX - %s, flags %d, cp %d\n", debugstr_w(wszText), (int)pStruct->flags, pStruct->codepage);
1644 if (pStruct->codepage != 1200) {
1645 FIXME("EM_SETTEXTEX only supports unicode right now!\n");
1646 return 0;
1648 /* FIXME: this should support RTF strings too, according to MSDN */
1649 if (pStruct->flags & ST_SELECTION) {
1650 ME_GetSelection(editor, &from, &to);
1651 style = ME_GetSelectionInsertStyle(editor);
1652 ME_InternalDeleteText(editor, from, to - from);
1653 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
1654 ME_ReleaseStyle(style);
1656 else {
1657 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
1658 ME_InsertTextFromCursor(editor, 0, wszText, len, editor->pBuffer->pDefaultStyle);
1659 len = 1;
1661 ME_CommitUndo(editor);
1662 if (!(pStruct->flags & ST_KEEPUNDO))
1663 ME_EmptyUndoStack(editor);
1664 ME_UpdateRepaint(editor);
1665 return len;
1667 case EM_SETBKGNDCOLOR:
1669 LRESULT lColor = ME_GetBackColor(editor);
1670 if (editor->rgbBackColor != -1)
1671 DeleteObject(editor->hbrBackground);
1672 if (wParam)
1674 editor->rgbBackColor = -1;
1675 editor->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
1677 else
1679 editor->rgbBackColor = lParam;
1680 editor->hbrBackground = CreateSolidBrush(editor->rgbBackColor);
1682 if (editor->bRedraw)
1684 InvalidateRect(hWnd, NULL, TRUE);
1685 UpdateWindow(hWnd);
1687 return lColor;
1689 case EM_GETMODIFY:
1690 return editor->nModifyStep == 0 ? 0 : 1;
1691 case EM_SETMODIFY:
1693 if (wParam)
1694 editor->nModifyStep = 0x80000000;
1695 else
1696 editor->nModifyStep = 0;
1698 return 0;
1700 case EM_SETREADONLY:
1702 long nStyle = GetWindowLongW(hWnd, GWL_STYLE);
1703 if (wParam)
1704 nStyle |= ES_READONLY;
1705 else
1706 nStyle &= ~ES_READONLY;
1707 SetWindowLongW(hWnd, GWL_STYLE, nStyle);
1708 return 0;
1710 case EM_SETEVENTMASK:
1712 DWORD nOldMask = editor->nEventMask;
1714 editor->nEventMask = lParam;
1715 return nOldMask;
1717 case EM_GETEVENTMASK:
1718 return editor->nEventMask;
1719 case EM_SETCHARFORMAT:
1721 CHARFORMAT2W buf, *p;
1722 BOOL bRepaint = TRUE;
1723 p = ME_ToCF2W(&buf, (CHARFORMAT2W *)lParam);
1724 if (!wParam || (editor->mode & TM_PLAINTEXT))
1725 ME_SetDefaultCharFormat(editor, p);
1726 else if (wParam == (SCF_WORD | SCF_SELECTION))
1727 FIXME("EM_SETCHARFORMAT: word selection not supported\n");
1728 else if (wParam == SCF_ALL)
1729 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), p);
1730 else {
1731 int from, to;
1732 ME_GetSelection(editor, &from, &to);
1733 bRepaint = (from != to);
1734 ME_SetSelectionCharFormat(editor, p);
1736 ME_CommitUndo(editor);
1737 if (bRepaint)
1738 ME_UpdateRepaint(editor);
1739 return 0;
1741 case EM_GETCHARFORMAT:
1743 CHARFORMAT2W tmp, *dst = (CHARFORMAT2W *)lParam;
1744 if (dst->cbSize != sizeof(CHARFORMATA) &&
1745 dst->cbSize != sizeof(CHARFORMATW) &&
1746 dst->cbSize != sizeof(CHARFORMAT2A) &&
1747 dst->cbSize != sizeof(CHARFORMAT2W))
1748 return 0;
1749 tmp.cbSize = sizeof(tmp);
1750 if (!wParam)
1751 ME_GetDefaultCharFormat(editor, &tmp);
1752 else
1753 ME_GetSelectionCharFormat(editor, &tmp);
1754 ME_CopyToCFAny(dst, &tmp);
1755 return tmp.dwMask;
1757 case EM_SETPARAFORMAT:
1758 ME_SetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
1759 ME_UpdateRepaint(editor);
1760 ME_CommitUndo(editor);
1761 return 0;
1762 case EM_GETPARAFORMAT:
1763 ME_GetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
1764 return 0;
1765 case EM_GETFIRSTVISIBLELINE:
1767 ME_DisplayItem *p = editor->pBuffer->pFirst;
1768 int y = ME_GetYScrollPos(editor);
1769 int ypara = 0;
1770 int count = 0;
1771 int ystart, yend;
1772 while(p) {
1773 p = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
1774 if (p->type == diTextEnd)
1775 break;
1776 if (p->type == diParagraph) {
1777 ypara = p->member.para.nYPos;
1778 continue;
1780 ystart = ypara + p->member.row.nYPos;
1781 yend = ystart + p->member.row.nHeight;
1782 if (y < yend) {
1783 break;
1785 count++;
1787 return count;
1789 case EM_HIDESELECTION:
1791 editor->bHideSelection = (wParam != 0);
1792 ME_InvalidateSelection(editor);
1793 return 0;
1795 case EM_LINESCROLL:
1797 ME_ScrollDown(editor, lParam * 8); /* FIXME follow the original */
1798 return TRUE; /* Should return false if a single line richedit control */
1800 case WM_CLEAR:
1802 int from, to;
1803 ME_GetSelection(editor, &from, &to);
1804 ME_InternalDeleteText(editor, from, to-from);
1805 ME_CommitUndo(editor);
1806 ME_UpdateRepaint(editor);
1807 return 0;
1809 case EM_REPLACESEL:
1811 int from, to;
1812 ME_Style *style;
1813 LPWSTR wszText = ME_ToUnicode(hWnd, (void *)lParam);
1814 size_t len = lstrlenW(wszText);
1815 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText));
1817 ME_GetSelection(editor, &from, &to);
1818 style = ME_GetSelectionInsertStyle(editor);
1819 ME_InternalDeleteText(editor, from, to-from);
1820 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
1821 ME_ReleaseStyle(style);
1822 /* drop temporary style if line end */
1824 * FIXME question: does abc\n mean: put abc,
1825 * clear temp style, put \n? (would require a change)
1827 if (len>0 && wszText[len-1] == '\n')
1828 ME_ClearTempStyle(editor);
1829 ME_EndToUnicode(hWnd, wszText);
1830 ME_CommitUndo(editor);
1831 if (!wParam)
1832 ME_EmptyUndoStack(editor);
1833 ME_UpdateRepaint(editor);
1834 return 0;
1836 case EM_SCROLLCARET:
1838 int top, bottom; /* row's edges relative to document top */
1839 int nPos;
1840 ME_DisplayItem *para, *row;
1842 nPos = ME_GetYScrollPos(editor);
1843 row = ME_RowStart(editor->pCursors[0].pRun);
1844 para = ME_GetParagraph(row);
1845 top = para->member.para.nYPos + row->member.row.nYPos;
1846 bottom = top + row->member.row.nHeight;
1848 if (top < nPos) /* caret above window */
1849 ME_ScrollAbs(editor, top);
1850 else if (nPos + editor->sizeWindow.cy < bottom) /*below*/
1851 ME_ScrollAbs(editor, bottom - editor->sizeWindow.cy);
1852 return 0;
1854 case WM_SETFONT:
1856 LOGFONTW lf;
1857 CHARFORMAT2W fmt;
1858 HDC hDC;
1859 BOOL bRepaint = LOWORD(lParam);
1861 if (!wParam)
1862 wParam = (WPARAM)GetStockObject(SYSTEM_FONT);
1863 GetObjectW((HGDIOBJ)wParam, sizeof(LOGFONTW), &lf);
1864 hDC = GetDC(hWnd);
1865 ME_CharFormatFromLogFont(hDC, &lf, &fmt);
1866 ReleaseDC(hWnd, hDC);
1867 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), &fmt);
1868 ME_SetDefaultCharFormat(editor, &fmt);
1870 ME_CommitUndo(editor);
1871 if (bRepaint)
1872 ME_UpdateRepaint(editor);
1873 return 0;
1875 case WM_SETTEXT:
1877 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
1878 if (lParam)
1880 TRACE("WM_SETTEXT lParam==%lx\n",lParam);
1881 if (!IsWindowUnicode(hWnd) && !strncmp((char *)lParam, "{\\rtf", 5))
1883 /* Undocumented: WM_SETTEXT supports RTF text */
1884 ME_StreamInRTFString(editor, 0, (char *)lParam);
1886 else
1888 LPWSTR wszText = ME_ToUnicode(hWnd, (void *)lParam);
1889 TRACE("WM_SETTEXT - %s\n", debugstr_w(wszText)); /* debugstr_w() */
1890 if (lstrlenW(wszText) > 0)
1892 /* uses default style! */
1893 ME_InsertTextFromCursor(editor, 0, wszText, -1, editor->pBuffer->pDefaultStyle);
1895 ME_EndToUnicode(hWnd, wszText);
1898 else
1899 TRACE("WM_SETTEXT - NULL\n");
1900 ME_CommitUndo(editor);
1901 ME_EmptyUndoStack(editor);
1902 ME_SetSelection(editor, 0, 0);
1903 ME_UpdateRepaint(editor);
1904 return 0;
1906 case EM_CANPASTE:
1908 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
1909 if (IsClipboardFormatAvailable(nRTFFormat))
1910 return TRUE;
1911 if (IsClipboardFormatAvailable(CF_UNICODETEXT))
1912 return TRUE;
1913 return FALSE;
1915 case WM_PASTE:
1917 DWORD dwFormat = 0;
1918 EDITSTREAM es;
1919 ME_GlobalDestStruct gds;
1920 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
1921 UINT cf = 0;
1923 if (IsClipboardFormatAvailable(nRTFFormat))
1924 cf = nRTFFormat, dwFormat = SF_RTF;
1925 else if (IsClipboardFormatAvailable(CF_UNICODETEXT))
1926 cf = CF_UNICODETEXT, dwFormat = SF_TEXT|SF_UNICODE;
1927 else
1928 return 0;
1930 if (!OpenClipboard(hWnd))
1931 return 0;
1932 gds.hData = GetClipboardData(cf);
1933 gds.nLength = 0;
1934 es.dwCookie = (DWORD)&gds;
1935 es.pfnCallback = dwFormat == SF_RTF ? ME_ReadFromHGLOBALRTF : ME_ReadFromHGLOBALUnicode;
1936 ME_StreamIn(editor, dwFormat|SFF_SELECTION, &es);
1938 CloseClipboard();
1939 return 0;
1941 case WM_CUT:
1942 case WM_COPY:
1944 LPDATAOBJECT dataObj = NULL;
1945 CHARRANGE range;
1946 HRESULT hr = S_OK;
1948 if (editor->cPasswordMask)
1949 return 0; /* Copying or Cutting masked text isn't allowed */
1951 ME_GetSelection(editor, (int*)&range.cpMin, (int*)&range.cpMax);
1952 if(editor->lpOleCallback)
1953 hr = IRichEditOleCallback_GetClipboardData(editor->lpOleCallback, &range, RECO_COPY, &dataObj);
1954 if(FAILED(hr) || !dataObj)
1955 hr = ME_GetDataObject(editor, &range, &dataObj);
1956 if(SUCCEEDED(hr)) {
1957 hr = OleSetClipboard(dataObj);
1958 IDataObject_Release(dataObj);
1960 if (SUCCEEDED(hr) && msg == WM_CUT)
1962 ME_InternalDeleteText(editor, range.cpMin, range.cpMax-range.cpMin);
1963 ME_CommitUndo(editor);
1964 ME_UpdateRepaint(editor);
1966 return 0;
1968 case WM_GETTEXTLENGTH:
1969 return ME_GetTextLength(editor);
1970 case EM_GETTEXTLENGTHEX:
1971 return ME_GetTextLengthEx(editor, (GETTEXTLENGTHEX *)wParam);
1972 case WM_GETTEXT:
1974 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
1975 tr.chrg.cpMin = 0;
1976 tr.chrg.cpMax = wParam-1;
1977 tr.lpstrText = (WCHAR *)lParam;
1978 return RichEditANSIWndProc(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
1980 case EM_GETTEXTEX:
1982 GETTEXTEX *ex = (GETTEXTEX*)wParam;
1983 int nStart, nCount;
1985 if (ex->flags & ~(GT_SELECTION | GT_USECRLF))
1986 FIXME("GETTEXTEX flags 0x%08x not supported\n", ex->flags & ~(GT_SELECTION | GT_USECRLF));
1988 if (ex->flags & GT_SELECTION)
1990 ME_GetSelection(editor, &nStart, &nCount);
1991 nCount -= nStart;
1992 nCount = min(nCount, ex->cb - 1);
1994 else
1996 nStart = 0;
1997 nCount = ex->cb - 1;
1999 if (ex->codepage == 1200 || IsWindowUnicode(hWnd))
2001 nCount = min(nCount, ex->cb / sizeof(WCHAR) - 1);
2002 return ME_GetTextW(editor, (LPWSTR)lParam, nStart, nCount, ex->flags & GT_USECRLF);
2004 else
2006 /* potentially each char may be a CR, why calculate the exact value with O(N) when
2007 we can just take a bigger buffer? :) */
2008 int crlfmul = (ex->flags & GT_USECRLF) ? 2 : 1;
2009 LPWSTR buffer = richedit_alloc((crlfmul*nCount + 1) * sizeof(WCHAR));
2010 DWORD buflen = ex->cb;
2011 LRESULT rc;
2012 DWORD flags = 0;
2014 buflen = ME_GetTextW(editor, buffer, nStart, nCount, ex->flags & GT_USECRLF);
2015 rc = WideCharToMultiByte(ex->codepage, flags, buffer, -1, (LPSTR)lParam, ex->cb, ex->lpDefaultChar, ex->lpUsedDefaultChar);
2017 richedit_free(buffer);
2018 return rc;
2021 case EM_GETSELTEXT:
2023 int from, to;
2024 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
2025 ME_GetSelection(editor, &from, &to);
2026 tr.chrg.cpMin = from;
2027 tr.chrg.cpMax = to;
2028 tr.lpstrText = (WCHAR *)lParam;
2029 return RichEditANSIWndProc(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
2031 case EM_GETSCROLLPOS:
2033 POINT *point = (POINT *)lParam;
2034 point->x = 0; /* FIXME side scrolling not implemented */
2035 point->y = ME_GetYScrollPos(editor);
2036 return 1;
2038 case EM_GETTEXTRANGE:
2040 TEXTRANGEW *rng = (TEXTRANGEW *)lParam;
2041 TRACE("EM_GETTEXTRANGE min=%d max=%d unicode=%d emul1.0=%d length=%d\n",
2042 rng->chrg.cpMin, rng->chrg.cpMax, IsWindowUnicode(hWnd),
2043 editor->bEmulateVersion10, ME_GetTextLength(editor));
2044 if (IsWindowUnicode(hWnd))
2045 return ME_GetTextW(editor, rng->lpstrText, rng->chrg.cpMin, rng->chrg.cpMax-rng->chrg.cpMin, editor->bEmulateVersion10);
2046 else
2048 int nLen = rng->chrg.cpMax-rng->chrg.cpMin;
2049 WCHAR *p = ALLOC_N_OBJ(WCHAR, nLen+1);
2050 int nChars = ME_GetTextW(editor, p, rng->chrg.cpMin, nLen, editor->bEmulateVersion10);
2051 /* FIXME this is a potential security hole (buffer overrun)
2052 if you know more about wchar->mbyte conversion please explain
2054 WideCharToMultiByte(CP_ACP, 0, p, nChars+1, (char *)rng->lpstrText, nLen+1, NULL, NULL);
2055 FREE_OBJ(p);
2056 return nChars;
2059 case EM_GETLINE:
2061 ME_DisplayItem *run;
2062 const BOOL bUnicode = IsWindowUnicode(hWnd);
2063 const unsigned int nMaxChars = *(WORD *) lParam;
2064 unsigned int nEndChars, nCharsLeft = nMaxChars;
2065 char *dest = (char *) lParam;
2066 /* rich text editor 1.0 uses \r\n for line end, 2.0 uses just \r;
2067 we need to know how if we have the extra \n or not */
2068 int nLF = editor->bEmulateVersion10;
2070 TRACE("EM_GETLINE: row=%d, nMaxChars=%d (%s)\n", (int) wParam, nMaxChars,
2071 bUnicode ? "Unicode" : "Ansi");
2073 run = ME_FindRowWithNumber(editor, wParam);
2074 if (run == NULL)
2075 return 0;
2077 while (nCharsLeft && (run = ME_FindItemFwd(run, diRunOrStartRow))
2078 && !(run->member.run.nFlags & MERF_ENDPARA))
2080 unsigned int nCopy;
2081 ME_String *strText;
2082 if (run->type != diRun)
2083 break;
2084 strText = run->member.run.strText;
2085 nCopy = min(nCharsLeft, strText->nLen);
2087 if (bUnicode)
2088 lstrcpynW((LPWSTR) dest, strText->szData, nCopy);
2089 else
2090 nCopy = WideCharToMultiByte(CP_ACP, 0, strText->szData, nCopy, dest,
2091 nCharsLeft, NULL, NULL);
2092 dest += nCopy * (bUnicode ? sizeof(WCHAR) : 1);
2093 nCharsLeft -= nCopy;
2096 /* append \r\0 (or \r\n\0 in 1.0), space allowing */
2097 nEndChars = min(nCharsLeft, 2 + nLF);
2098 nCharsLeft -= nEndChars;
2099 if (bUnicode)
2101 const WCHAR src[] = {'\r', '\0'};
2102 const WCHAR src10[] = {'\r', '\n', '\0'};
2103 lstrcpynW((LPWSTR) dest, nLF ? src10 : src, nEndChars);
2105 else
2106 lstrcpynA(dest, nLF ? "\r\n" : "\r", nEndChars);
2108 TRACE("EM_GETLINE: got %u bytes\n", nMaxChars - nCharsLeft);
2110 if (nEndChars == 2 + nLF)
2111 return nMaxChars - nCharsLeft - 1; /* don't count \0 */
2112 else
2113 return nMaxChars - nCharsLeft;
2115 case EM_GETLINECOUNT:
2117 ME_DisplayItem *item = editor->pBuffer->pFirst->next;
2118 int nRows = 0;
2120 while (item != editor->pBuffer->pLast)
2122 assert(item->type == diParagraph);
2123 nRows += item->member.para.nRows;
2124 item = item->member.para.next_para;
2126 TRACE("EM_GETLINECOUNT: nRows==%d\n", nRows);
2127 return max(1, nRows);
2129 case EM_LINEFROMCHAR:
2131 if (wParam == -1)
2132 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(editor, 1));
2133 else
2134 return ME_RowNumberFromCharOfs(editor, wParam);
2136 case EM_EXLINEFROMCHAR:
2138 if (lParam == -1)
2139 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(editor,1));
2140 else
2141 return ME_RowNumberFromCharOfs(editor, lParam);
2143 case EM_LINEINDEX:
2145 ME_DisplayItem *item, *para;
2146 int nCharOfs;
2148 if (wParam == -1)
2149 item = ME_FindItemBack(editor->pCursors[0].pRun, diStartRow);
2150 else
2151 item = ME_FindRowWithNumber(editor, wParam);
2152 if (!item)
2153 return -1;
2154 para = ME_GetParagraph(item);
2155 item = ME_FindItemFwd(item, diRun);
2156 nCharOfs = para->member.para.nCharOfs + item->member.run.nCharOfs;
2157 TRACE("EM_LINEINDEX: nCharOfs==%d\n", nCharOfs);
2158 return nCharOfs;
2160 case EM_LINELENGTH:
2162 ME_DisplayItem *item, *item_end;
2163 int nChars = 0, nThisLineOfs = 0, nNextLineOfs = 0;
2165 if (wParam > ME_GetTextLength(editor))
2166 return 0;
2167 if (wParam == -1)
2169 FIXME("EM_LINELENGTH: returning number of unselected characters on lines with selection unsupported.\n");
2170 return 0;
2172 item = ME_FindItemAtOffset(editor, diRun, wParam, NULL);
2173 item = ME_RowStart(item);
2174 nThisLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item, diRun), 0);
2175 item_end = ME_FindItemFwd(item, diStartRowOrParagraphOrEnd);
2176 if (item_end->type == diStartRow)
2177 nNextLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item_end, diRun), 0);
2178 else
2179 nNextLineOfs = ME_FindItemFwd(item, diParagraphOrEnd)->member.para.nCharOfs
2180 - (editor->bEmulateVersion10?2:1);
2181 nChars = nNextLineOfs - nThisLineOfs;
2182 TRACE("EM_LINELENGTH(%d)==%d\n",wParam, nChars);
2183 return nChars;
2185 case EM_EXLIMITTEXT:
2187 if (wParam != 0 || lParam < 0)
2188 return 0;
2189 if (lParam == 0)
2190 editor->nTextLimit = 65536;
2191 else
2192 editor->nTextLimit = (int) lParam;
2193 return 0;
2195 case EM_GETLIMITTEXT:
2197 return editor->nTextLimit;
2199 case EM_FINDTEXT:
2201 FINDTEXTA *ft = (FINDTEXTA *)lParam;
2202 int nChars = MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, NULL, 0);
2203 WCHAR *tmp;
2204 LRESULT r;
2206 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
2207 MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, tmp, nChars);
2208 r = ME_FindText(editor, wParam, &ft->chrg, tmp, NULL);
2209 FREE_OBJ( tmp );
2210 return r;
2212 case EM_FINDTEXTEX:
2214 FINDTEXTEXA *ex = (FINDTEXTEXA *)lParam;
2215 int nChars = MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, NULL, 0);
2216 WCHAR *tmp;
2217 LRESULT r;
2219 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
2220 MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, tmp, nChars);
2221 r = ME_FindText(editor, wParam, &ex->chrg, tmp, &ex->chrgText);
2222 FREE_OBJ( tmp );
2223 return r;
2225 case EM_FINDTEXTW:
2227 FINDTEXTW *ft = (FINDTEXTW *)lParam;
2228 return ME_FindText(editor, wParam, &ft->chrg, ft->lpstrText, NULL);
2230 case EM_FINDTEXTEXW:
2232 FINDTEXTEXW *ex = (FINDTEXTEXW *)lParam;
2233 return ME_FindText(editor, wParam, &ex->chrg, ex->lpstrText, &ex->chrgText);
2235 case EM_GETZOOM:
2236 if (!wParam || !lParam)
2237 return FALSE;
2238 *(int *)wParam = editor->nZoomNumerator;
2239 *(int *)lParam = editor->nZoomDenominator;
2240 return TRUE;
2241 case EM_SETZOOM:
2242 return ME_SetZoom(editor, wParam, lParam);
2243 case EM_CHARFROMPOS:
2244 return ME_CharFromPos(editor, ((POINTL *)lParam)->x, ((POINTL *)lParam)->y);
2245 case EM_POSFROMCHAR:
2247 ME_DisplayItem *pRun;
2248 int nCharOfs, nOffset, nLength;
2249 POINTL pt = {0,0};
2251 nCharOfs = wParam;
2252 /* detect which API version we're dealing with */
2253 if (wParam >= 0x40000)
2254 nCharOfs = lParam;
2255 nLength = ME_GetTextLength(editor);
2257 if (nCharOfs < nLength) {
2258 ME_RunOfsFromCharOfs(editor, nCharOfs, &pRun, &nOffset);
2259 assert(pRun->type == diRun);
2260 pt.y = pRun->member.run.pt.y;
2261 pt.x = pRun->member.run.pt.x + ME_PointFromChar(editor, &pRun->member.run, nOffset);
2262 pt.y += ME_GetParagraph(pRun)->member.para.nYPos;
2263 } else {
2264 pt.x = 0;
2265 pt.y = editor->pBuffer->pLast->member.para.nYPos;
2267 if (wParam >= 0x40000) {
2268 *(POINTL *)wParam = pt;
2270 return MAKELONG( pt.x, pt.y );
2272 case WM_CREATE:
2273 if (GetWindowLongW(hWnd, GWL_STYLE) & WS_HSCROLL)
2274 { /* Squelch the default horizontal scrollbar it would make */
2275 ShowScrollBar(editor->hWnd, SB_HORZ, FALSE);
2277 ME_CommitUndo(editor);
2278 ME_WrapMarkedParagraphs(editor);
2279 ME_MoveCaret(editor);
2280 return 0;
2281 case WM_DESTROY:
2282 ME_DestroyEditor(editor);
2283 SetWindowLongPtrW(hWnd, 0, 0);
2284 return 0;
2285 case WM_LBUTTONDOWN:
2286 SetFocus(hWnd);
2287 ME_LButtonDown(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
2288 SetCapture(hWnd);
2289 ME_LinkNotify(editor,msg,wParam,lParam);
2290 break;
2291 case WM_MOUSEMOVE:
2292 if (GetCapture() == hWnd)
2293 ME_MouseMove(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
2294 ME_LinkNotify(editor,msg,wParam,lParam);
2295 break;
2296 case WM_LBUTTONUP:
2297 if (GetCapture() == hWnd)
2298 ReleaseCapture();
2299 ME_LinkNotify(editor,msg,wParam,lParam);
2300 break;
2301 case WM_LBUTTONDBLCLK:
2302 ME_LinkNotify(editor,msg,wParam,lParam);
2303 ME_SelectWord(editor);
2304 break;
2305 case WM_CONTEXTMENU:
2306 if (!ME_ShowContextMenu(editor, (short)LOWORD(lParam), (short)HIWORD(lParam)))
2307 goto do_default;
2308 break;
2309 case WM_PAINT:
2310 if (editor->bRedraw)
2312 HDC hDC;
2313 PAINTSTRUCT ps;
2315 hDC = BeginPaint(hWnd, &ps);
2316 ME_PaintContent(editor, hDC, FALSE, &ps.rcPaint);
2317 EndPaint(hWnd, &ps);
2319 break;
2320 case WM_SETFOCUS:
2321 ME_ShowCaret(editor);
2322 ME_SendOldNotify(editor, EN_SETFOCUS);
2323 return 0;
2324 case WM_KILLFOCUS:
2325 ME_HideCaret(editor);
2326 ME_SendOldNotify(editor, EN_KILLFOCUS);
2327 return 0;
2328 case WM_ERASEBKGND:
2330 if (editor->bRedraw)
2332 HDC hDC = (HDC)wParam;
2333 RECT rc;
2334 if (GetUpdateRect(hWnd,&rc,TRUE))
2336 FillRect(hDC, &rc, editor->hbrBackground);
2339 return 1;
2341 case WM_COMMAND:
2342 TRACE("editor wnd command = %d\n", LOWORD(wParam));
2343 return 0;
2344 case WM_KEYDOWN:
2345 if (ME_KeyDown(editor, LOWORD(wParam)))
2346 return 0;
2347 goto do_default;
2348 case WM_CHAR:
2350 WCHAR wstr = LOWORD(wParam);
2351 if (editor->AutoURLDetect_bEnable)
2352 ME_AutoURLDetect(editor, wstr);
2354 switch (wstr)
2356 case 1: /* Ctrl-A */
2357 ME_SetSelection(editor, 0, -1);
2358 return 0;
2359 case 3: /* Ctrl-C */
2360 SendMessageW(editor->hWnd, WM_COPY, 0, 0);
2361 return 0;
2364 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY) {
2365 MessageBeep(MB_ICONERROR);
2366 return 0; /* FIXME really 0 ? */
2369 switch (wstr)
2371 case 22: /* Ctrl-V */
2372 SendMessageW(editor->hWnd, WM_PASTE, 0, 0);
2373 return 0;
2374 case 24: /* Ctrl-X */
2375 SendMessageW(editor->hWnd, WM_CUT, 0, 0);
2376 return 0;
2377 case 25: /* Ctrl-Y */
2378 SendMessageW(editor->hWnd, EM_REDO, 0, 0);
2379 return 0;
2380 case 26: /* Ctrl-Z */
2381 SendMessageW(editor->hWnd, EM_UNDO, 0, 0);
2382 return 0;
2384 if (((unsigned)wstr)>=' ' || wstr=='\r' || wstr=='\t') {
2385 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
2386 ME_Style *style = ME_GetInsertStyle(editor, 0);
2387 ME_SaveTempStyle(editor);
2388 ME_InsertTextFromCursor(editor, 0, &wstr, 1, style);
2389 ME_ReleaseStyle(style);
2390 ME_CommitUndo(editor);
2391 ME_UpdateRepaint(editor);
2393 return 0;
2395 case EM_SCROLL: /* fall through */
2396 case WM_VSCROLL:
2398 int origNPos;
2399 int lineHeight;
2401 origNPos = ME_GetYScrollPos(editor);
2402 lineHeight = 24;
2404 if (editor && editor->pBuffer && editor->pBuffer->pDefaultStyle)
2405 lineHeight = editor->pBuffer->pDefaultStyle->tm.tmHeight;
2406 if (lineHeight <= 0) lineHeight = 24;
2408 switch(LOWORD(wParam))
2410 case SB_LINEUP:
2411 ME_ScrollUp(editor,lineHeight);
2412 break;
2413 case SB_LINEDOWN:
2414 ME_ScrollDown(editor,lineHeight);
2415 break;
2416 case SB_PAGEUP:
2417 ME_ScrollUp(editor,editor->sizeWindow.cy);
2418 break;
2419 case SB_PAGEDOWN:
2420 ME_ScrollDown(editor,editor->sizeWindow.cy);
2421 break;
2422 case SB_THUMBTRACK:
2423 case SB_THUMBPOSITION:
2424 ME_ScrollAbs(editor,HIWORD(wParam));
2425 break;
2427 if (msg == EM_SCROLL)
2428 return 0x00010000 | (((ME_GetYScrollPos(editor) - origNPos)/lineHeight) & 0xffff);
2429 break;
2431 case WM_MOUSEWHEEL:
2433 int gcWheelDelta;
2434 UINT pulScrollLines;
2436 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
2437 gcWheelDelta = -GET_WHEEL_DELTA_WPARAM(wParam);
2439 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
2441 /* FIXME follow the original */
2442 ME_ScrollDown(editor,pulScrollLines * (gcWheelDelta / WHEEL_DELTA) * 8);
2444 break;
2446 case EM_GETRECT:
2448 *((RECT *)lParam) = editor->rcFormat;
2449 return 0;
2451 case EM_SETRECT:
2452 case EM_SETRECTNP:
2454 if (lParam)
2456 RECT *rc = (RECT *)lParam;
2458 if (wParam)
2460 editor->rcFormat.left += rc->left;
2461 editor->rcFormat.top += rc->top;
2462 editor->rcFormat.right += rc->right;
2463 editor->rcFormat.bottom += rc->bottom;
2465 else
2467 editor->rcFormat = *rc;
2470 else
2472 GetClientRect(hWnd, &editor->rcFormat);
2474 if (msg != EM_SETRECTNP)
2475 ME_RewrapRepaint(editor);
2476 return 0;
2478 case EM_REQUESTRESIZE:
2479 ME_SendRequestResize(editor, TRUE);
2480 return 0;
2481 case WM_SETREDRAW:
2482 editor->bRedraw = wParam;
2483 return 0;
2484 case WM_SIZE:
2486 GetClientRect(hWnd, &editor->rcFormat);
2487 ME_RewrapRepaint(editor);
2488 return DefWindowProcW(hWnd, msg, wParam, lParam);
2490 case EM_GETOLEINTERFACE:
2492 LPVOID *ppvObj = (LPVOID*) lParam;
2493 return CreateIRichEditOle(editor, ppvObj);
2495 case EM_GETPASSWORDCHAR:
2497 return editor->cPasswordMask;
2499 case EM_SETOLECALLBACK:
2500 if(editor->lpOleCallback)
2501 IUnknown_Release(editor->lpOleCallback);
2502 editor->lpOleCallback = (LPRICHEDITOLECALLBACK)lParam;
2503 if(editor->lpOleCallback)
2504 IUnknown_AddRef(editor->lpOleCallback);
2505 return TRUE;
2506 case EM_GETWORDBREAKPROC:
2507 return (LRESULT)editor->pfnWordBreak;
2508 case EM_SETWORDBREAKPROC:
2510 EDITWORDBREAKPROCW pfnOld = editor->pfnWordBreak;
2512 editor->pfnWordBreak = (EDITWORDBREAKPROCW)lParam;
2513 return (LRESULT)pfnOld;
2515 case EM_SETTEXTMODE:
2517 LRESULT ret;
2518 int mask = 0;
2519 int changes = 0;
2520 if ((ret = RichEditANSIWndProc(hWnd, WM_GETTEXTLENGTH, 0, 0)) == 0)
2522 /*Check for valid wParam*/
2523 if ((((wParam & TM_RICHTEXT) && ((wParam & TM_PLAINTEXT) << 1))) ||
2524 (((wParam & TM_MULTILEVELUNDO) && ((wParam & TM_SINGLELEVELUNDO) << 1))) ||
2525 (((wParam & TM_MULTICODEPAGE) && ((wParam & TM_SINGLECODEPAGE) << 1))))
2526 return 1;
2527 else
2529 if (wParam & (TM_RICHTEXT | TM_PLAINTEXT))
2531 mask |= (TM_RICHTEXT | TM_PLAINTEXT);
2532 changes |= (wParam & (TM_RICHTEXT | TM_PLAINTEXT));
2534 /*FIXME: Currently no support for undo level and code page options*/
2535 editor->mode = (editor->mode & (~mask)) | changes;
2536 return 0;
2539 return ret;
2541 case EM_SETPASSWORDCHAR:
2543 editor->cPasswordMask = wParam;
2544 ME_RewrapRepaint(editor);
2545 return 0;
2547 default:
2548 do_default:
2549 return DefWindowProcW(hWnd, msg, wParam, lParam);
2551 return 0L;
2555 /******************************************************************
2556 * RichEdit10ANSIWndProc (RICHED20.9)
2558 LRESULT WINAPI RichEdit10ANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2560 LRESULT result;
2562 /* FIXME: this is NOT the same as 2.0 version */
2563 result = RichEditANSIWndProc(hWnd, msg, wParam, lParam);
2564 if (msg == WM_NCCREATE)
2566 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongPtrW(hWnd, 0);
2568 editor->bEmulateVersion10 = TRUE;
2569 editor->pBuffer->pLast->member.para.nCharOfs = 2;
2571 return result;
2574 void ME_SendOldNotify(ME_TextEditor *editor, int nCode)
2576 HWND hWnd = editor->hWnd;
2577 SendMessageA(GetParent(hWnd), WM_COMMAND, (nCode<<16)|GetWindowLongW(hWnd, GWLP_ID), (LPARAM)hWnd);
2580 void ME_LinkNotify(ME_TextEditor *editor, UINT msg, WPARAM wParam, LPARAM lParam)
2582 int x,y;
2583 ME_Cursor tmpCursor;
2584 ME_Run *tmpRun;
2585 BOOL bNothing;
2586 ENLINK info;
2587 x = (short)LOWORD(lParam);
2588 y = (short)HIWORD(lParam);
2589 ME_FindPixelPos(editor, x, y, &tmpCursor, &bNothing);
2590 tmpRun = &tmpCursor.pRun->member.run;
2592 if ((tmpRun->style->fmt.dwMask & CFM_LINK)
2593 && (tmpRun->style->fmt.dwEffects & CFE_LINK))
2594 { /* The clicked run has CFE_LINK set */
2595 info.nmhdr.hwndFrom = editor->hWnd;
2596 info.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
2597 info.nmhdr.code = EN_LINK;
2598 info.msg = msg;
2599 info.wParam = wParam;
2600 info.lParam = lParam;
2601 info.chrg.cpMin = ME_CharOfsFromRunOfs(editor,tmpCursor.pRun,0);
2602 info.chrg.cpMax = info.chrg.cpMin + ME_StrVLen(tmpRun->strText);
2603 SendMessageW(GetParent(editor->hWnd), WM_NOTIFY,info.nmhdr.idFrom, (LPARAM)&info);
2607 int ME_CountParagraphsBetween(ME_TextEditor *editor, int from, int to)
2609 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
2610 int i = 0;
2612 while(item && item->member.para.next_para->member.para.nCharOfs <= from)
2613 item = item->member.para.next_para;
2614 if (!item)
2615 return 0;
2616 while(item && item->member.para.next_para->member.para.nCharOfs <= to) {
2617 item = item->member.para.next_para;
2618 i++;
2620 return i;
2624 int ME_GetTextW(ME_TextEditor *editor, WCHAR *buffer, int nStart, int nChars, int bCRLF)
2626 ME_DisplayItem *item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
2627 int nWritten = 0;
2628 WCHAR *pStart = buffer;
2630 if (!item) {
2631 *buffer = L'\0';
2632 return 0;
2635 if (nStart)
2637 int nLen = ME_StrLen(item->member.run.strText) - nStart;
2638 if (nLen > nChars)
2639 nLen = nChars;
2640 CopyMemory(buffer, item->member.run.strText->szData + nStart, sizeof(WCHAR)*nLen);
2641 nChars -= nLen;
2642 nWritten += nLen;
2643 if (!nChars)
2644 return nWritten;
2645 buffer += nLen;
2646 nStart = 0;
2647 item = ME_FindItemFwd(item, diRun);
2650 while(nChars && item)
2652 int nLen = ME_StrLen(item->member.run.strText);
2653 if (nLen > nChars)
2654 nLen = nChars;
2656 if (item->member.run.nFlags & MERF_ENDPARA)
2658 if (!ME_FindItemFwd(item, diRun))
2659 /* No '\r' is appended to the last paragraph. */
2660 nLen = 0;
2661 else {
2662 *buffer = '\r';
2663 if (bCRLF)
2665 *(++buffer) = '\n';
2666 nWritten++;
2668 assert(nLen == 1);
2669 /* our end paragraph consists of 2 characters now */
2670 if (editor->bEmulateVersion10)
2671 nChars--;
2674 else
2675 CopyMemory(buffer, item->member.run.strText->szData, sizeof(WCHAR)*nLen);
2676 nChars -= nLen;
2677 nWritten += nLen;
2678 buffer += nLen;
2680 if (!nChars)
2682 TRACE("nWritten=%d, actual=%d\n", nWritten, buffer-pStart);
2683 *buffer = L'\0';
2684 return nWritten;
2686 item = ME_FindItemFwd(item, diRun);
2688 *buffer = L'\0';
2689 TRACE("nWritten=%d, actual=%d\n", nWritten, buffer-pStart);
2690 return nWritten;
2693 void ME_RegisterEditorClass(HINSTANCE hInstance)
2695 BOOL bResult;
2696 WNDCLASSW wcW;
2697 WNDCLASSA wcA;
2699 wcW.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
2700 wcW.lpfnWndProc = RichEditANSIWndProc;
2701 wcW.cbClsExtra = 0;
2702 wcW.cbWndExtra = sizeof(ME_TextEditor *);
2703 wcW.hInstance = NULL; /* hInstance would register DLL-local class */
2704 wcW.hIcon = NULL;
2705 wcW.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
2706 wcW.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
2707 wcW.lpszMenuName = NULL;
2708 wcW.lpszClassName = wszClassName;
2709 bResult = RegisterClassW(&wcW);
2710 assert(bResult);
2711 wcW.lpszClassName = wszClassName50;
2712 bResult = RegisterClassW(&wcW);
2713 assert(bResult);
2715 wcA.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
2716 wcA.lpfnWndProc = RichEditANSIWndProc;
2717 wcA.cbClsExtra = 0;
2718 wcA.cbWndExtra = sizeof(ME_TextEditor *);
2719 wcA.hInstance = NULL; /* hInstance would register DLL-local class */
2720 wcA.hIcon = NULL;
2721 wcA.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
2722 wcA.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
2723 wcA.lpszMenuName = NULL;
2724 wcA.lpszClassName = "RichEdit20A";
2725 bResult = RegisterClassA(&wcA);
2726 assert(bResult);
2727 wcA.lpszClassName = "RichEdit50A";
2728 bResult = RegisterClassA(&wcA);
2729 assert(bResult);
2732 LRESULT WINAPI REComboWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
2733 /* FIXME: Not implemented */
2734 TRACE("hWnd %p msg %04x (%s) %08x %08lx\n",
2735 hWnd, msg, get_msg_name(msg), wParam, lParam);
2736 return DefWindowProcW(hWnd, msg, wParam, lParam);
2739 LRESULT WINAPI REListWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
2740 /* FIXME: Not implemented */
2741 TRACE("hWnd %p msg %04x (%s) %08x %08lx\n",
2742 hWnd, msg, get_msg_name(msg), wParam, lParam);
2743 return DefWindowProcW(hWnd, msg, wParam, lParam);
2746 /******************************************************************
2747 * REExtendedRegisterClass (RICHED20.8)
2749 * FIXME undocumented
2750 * Need to check for errors and implement controls and callbacks
2752 LRESULT WINAPI REExtendedRegisterClass(void)
2754 WNDCLASSW wcW;
2755 UINT result;
2757 FIXME("semi stub\n");
2759 wcW.cbClsExtra = 0;
2760 wcW.cbWndExtra = 4;
2761 wcW.hInstance = NULL;
2762 wcW.hIcon = NULL;
2763 wcW.hCursor = NULL;
2764 wcW.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
2765 wcW.lpszMenuName = NULL;
2767 if (!ME_ListBoxRegistered)
2769 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS;
2770 wcW.lpfnWndProc = REListWndProc;
2771 wcW.lpszClassName = wszClassNameListBox;
2772 if (RegisterClassW(&wcW)) ME_ListBoxRegistered = TRUE;
2775 if (!ME_ComboBoxRegistered)
2777 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
2778 wcW.lpfnWndProc = REComboWndProc;
2779 wcW.lpszClassName = wszClassNameComboBox;
2780 if (RegisterClassW(&wcW)) ME_ComboBoxRegistered = TRUE;
2783 result = 0;
2784 if (ME_ListBoxRegistered)
2785 result += 1;
2786 if (ME_ComboBoxRegistered)
2787 result += 2;
2789 return result;
2792 int ME_AutoURLDetect(ME_TextEditor *editor, WCHAR curChar)
2794 struct prefix_s {
2795 const char *text;
2796 int length;
2797 } prefixes[12] = {
2798 {"http:", 5},
2799 {"file:", 6},
2800 {"mailto:", 8},
2801 {"ftp:", 5},
2802 {"https:", 7},
2803 {"gopher:", 8},
2804 {"nntp:", 6},
2805 {"prospero:", 10},
2806 {"telnet:", 8},
2807 {"news:", 6},
2808 {"wais:", 6},
2809 {"www.", 5}
2811 CHARRANGE ins_pt;
2812 int curf_ef, link_ef, def_ef;
2813 int cur_prefx, prefx_cnt;
2814 int sel_min, sel_max;
2815 int car_pos = 0;
2816 int text_pos=-1;
2817 int URLmin, URLmax = 0;
2818 CHARRANGE url;
2819 FINDTEXTA ft;
2820 CHARFORMAT2W cur_format;
2821 CHARFORMAT2W default_format;
2822 CHARFORMAT2W link;
2823 RichEditANSIWndProc(editor->hWnd, EM_EXGETSEL, (WPARAM) 0, (LPARAM) &ins_pt);
2824 sel_min = ins_pt.cpMin;
2825 sel_max = ins_pt.cpMax;
2826 if (sel_min==sel_max)
2827 car_pos = sel_min;
2828 if (sel_min!=sel_max)
2829 car_pos = ME_GetTextLength(editor)+1;
2830 cur_format.cbSize = sizeof(cur_format);
2831 default_format.cbSize = sizeof(default_format);
2832 RichEditANSIWndProc(editor->hWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM) &cur_format);
2833 RichEditANSIWndProc(editor->hWnd, EM_GETCHARFORMAT, SCF_DEFAULT, (LPARAM) &default_format);
2834 link.cbSize = sizeof(link);
2835 link.dwMask = CFM_LINK;
2836 link.dwEffects = CFE_LINK;
2837 curf_ef = cur_format.dwEffects & link.dwEffects;
2838 def_ef = default_format.dwEffects & link.dwEffects;
2839 link_ef = link.dwEffects & link.dwEffects;
2840 if (curf_ef == link_ef)
2842 if( curChar == '\n' || curChar=='\r' || curChar==' ')
2844 ME_SetSelection(editor, car_pos, car_pos);
2845 RichEditANSIWndProc(editor->hWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &default_format);
2846 text_pos=-1;
2847 return 0;
2850 if (curf_ef == def_ef)
2852 cur_prefx = 0;
2853 prefx_cnt = (sizeof(prefixes)/sizeof(struct prefix_s))-1;
2854 while (cur_prefx<=prefx_cnt)
2856 if (text_pos == -1)
2858 ft.lpstrText = prefixes[cur_prefx].text;
2859 URLmin=max(0,(car_pos-prefixes[cur_prefx].length));
2860 URLmax=max(0, car_pos);
2861 if ((car_pos == 0) && (ME_GetTextLength(editor) != 0))
2863 URLmax = ME_GetTextLength(editor)+1;
2865 ft.chrg.cpMin = URLmin;
2866 ft.chrg.cpMax = URLmax;
2867 text_pos=RichEditANSIWndProc(editor->hWnd, EM_FINDTEXT, FR_DOWN, (LPARAM)&ft);
2868 cur_prefx++;
2870 if (text_pos != -1)
2872 url.cpMin=text_pos;
2873 url.cpMax=car_pos-1;
2874 ME_SetCharFormat(editor, text_pos, (URLmax-text_pos), &link);
2875 ME_RewrapRepaint(editor);
2876 break;
2880 return 0;