Implemented EM_LINELENGTH RichEdit message.
[wine/hacks.git] / dlls / riched20 / editor.c
blobb98ded00d83547c5e84a0f6d8de3defa4f7d3607
1 /*
2 * RichEdit - functions dealing with editor object
4 * Copyright 2004 by Krzysztof Foltman
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 /*
22 API implementation status:
24 Messages (ANSI versions not done yet)
25 - EM_AUTOURLDETECT 2.0
26 + EM_CANPASTE
27 + EM_CANREDO 2.0
28 + EM_CANUNDO
29 - EM_CHARFROMPOS
30 - EM_DISPLAYBAND
31 + EM_EMPTYUNDOBUFFER
32 + EM_EXGETSEL
33 - EM_EXLIMITTEXT
34 + EM_EXLINEFROMCHAR
35 + EM_EXSETSEL
36 + EM_FINDTEXT (only FR_DOWN flag implemented)
37 + EM_FINDTEXTEX (only FR_DOWN flag implemented)
38 - EM_FINDWORDBREAK
39 - EM_FMTLINES
40 - EM_FORMATRANGE
41 - EM_GETAUTOURLDETECT 2.0
42 - EM_GETBIDIOPTIONS 3.0
43 - EM_GETCHARFORMAT (partly done)
44 - EM_GETEDITSTYLE
45 + EM_GETEVENTMASK
46 - EM_GETFIRSTVISIBLELINE
47 - EM_GETIMECOLOR 1.0asian
48 - EM_GETIMECOMPMODE 2.0
49 - EM_GETIMEOPTIONS 1.0asian
50 - EM_GETIMESTATUS
51 - EM_GETLANGOPTIONS 2.0
52 - EM_GETLIMITTEXT
53 - EM_GETLINE
54 + EM_GETLINECOUNT returns number of rows, not of paragraphs
55 + EM_GETMODIFY
56 - EM_GETOLEINTERFACE
57 - EM_GETOPTIONS
58 + EM_GETPARAFORMAT
59 - EM_GETPASSWORDCHAR 2.0
60 - EM_GETPUNCTUATION 1.0asian
61 - EM_GETRECT
62 - EM_GETREDONAME 2.0
63 + EM_GETSEL
64 + EM_GETSELTEXT (ANSI&Unicode)
65 - EM_GETSCROLLPOS 3.0
66 ! - EM_GETTHUMB
67 - EM_GETTEXTEX 2.0
68 + EM_GETTEXTLENGTHEX (GTL_PRECISE unimplemented)
69 - EM_GETTEXTMODE 2.0
70 ? + EM_GETTEXTRANGE (ANSI&Unicode)
71 - EM_GETTYPOGRAPHYOPTIONS 3.0
72 - EM_GETUNDONAME
73 - EM_GETWORDBREAKPROC
74 - EM_GETWORDBREAKPROCEX
75 - EM_GETWORDWRAPMODE 1.0asian
76 - EM_SETZOOM 3.0
77 - EM_HIDESELECTION
78 - EM_LIMITTEXT
79 + EM_LINEFROMCHAR
80 + EM_LINEINDEX
81 + EM_LINELENGTH
82 + EM_LINESCROLL
83 - EM_PASTESPECIAL
84 - EM_POSFROMCHARS
85 + EM_REDO 2.0
86 - EM_REQUESTRESIZE
87 + EM_REPLACESEL (proper style?) ANSI&Unicode
88 - EM_SCROLL
89 - EM_SCROLLCARET
90 - EM_SELECTIONTYPE
91 - EM_SETBIDIOPTIONS 3.0
92 + EM_SETBKGNDCOLOR
93 - EM_SETCHARFORMAT (partly done, no ANSI)
94 - EM_SETEDITSTYLE
95 + EM_SETEVENTMASK (few notifications supported)
96 - EM_SETFONTSIZE
97 - EM_SETIMECOLOR 1.0asian
98 - EM_SETIMEOPTIONS 1.0asian
99 - EM_SETLANGOPTIONS 2.0
100 - EM_SETLIMITTEXT
101 + EM_SETMODIFY (not sure if implementation is correct)
102 - EM_SETOLECALLBACK
103 - EM_SETOPTIONS
104 - EM_SETPALETTE 2.0
105 + EM_SETPARAFORMAT
106 - EM_SETPASSWORDCHAR 2.0
107 - EM_SETPUNCTUATION 1.0asian
108 + EM_SETREADONLY no beep on modification attempt
109 - EM_SETRECT
110 - EM_SETRECTNP (EM_SETRECT without repainting) - not supported in RICHEDIT
111 + EM_SETSEL
112 - EM_SETSCROLLPOS 3.0
113 - EM_SETTABSTOPS 3.0
114 - EM_SETTARGETDEVICE
115 + EM_SETTEXTEX 3.0 (unicode only, no rich text insertion handling, proper style?)
116 - EM_SETTEXTMODE 2.0
117 - EM_SETTYPOGRAPHYOPTIONS 3.0
118 - EM_SETUNDOLIMIT 2.0
119 - EM_SETWORDBREAKPROC
120 - EM_SETWORDBREAKPROCEX
121 - EM_SETWORDWRAPMODE 1.0asian
122 - EM_SETZOOM 3.0
123 - EM_SHOWSCROLLBAR 2.0
124 - EM_STOPGROUPTYPING 2.0
125 + EM_STREAMIN
126 + EM_STREAMOUT
127 + EM_UNDO
128 + WM_CHAR
129 + WM_CLEAR
130 + WM_COPY
131 + WM_CUT
132 + WM_GETDLGCODE (the current implementation is incomplete)
133 + WM_GETTEXT (ANSI&Unicode)
134 + WM_GETTEXTLENGTH (ANSI version sucks)
135 + WM_PASTE
136 - WM_SETFONT
137 + WM_SETTEXT (resets undo stack !) (proper style?) ANSI&Unicode
138 - WM_STYLECHANGING
139 - WM_STYLECHANGED (things like read-only flag)
140 - WM_UNICHAR
142 Notifications
144 * EN_CHANGE (sent from the wrong place)
145 - EN_CORRECTTEXT
146 - EN_DROPFILES
147 - EN_ERRSPACE
148 - EN_HSCROLL
149 - EN_IMECHANGE
150 + EN_KILLFOCUS
151 - EN_LINK
152 - EN_MAXTEXT
153 - EN_MSGFILTER
154 - EN_OLEOPFAILED
155 - EN_PROTECTED
156 - EN_REQUESTRESIZE
157 - EN_SAVECLIPBOARD
158 + EN_SELCHANGE
159 + EN_SETFOCUS
160 - EN_STOPNOUNDO
161 * EN_UPDATE (sent from the wrong place)
162 - EN_VSCROLL
164 Styles
166 - ES_AUTOHSCROLL
167 - ES_AUTOVSCROLL
168 - ES_CENTER
169 - ES_DISABLENOSCROLL (scrollbar is always visible)
170 - ES_EX_NOCALLOLEINIT
171 - ES_LEFT
172 - ES_MULTILINE (currently single line controls aren't supported)
173 - ES_NOIME
174 - ES_READONLY (I'm not sure if beeping is the proper behaviour)
175 - ES_RIGHT
176 - ES_SAVESEL
177 - ES_SELFIME
178 - ES_SUNKEN
179 - ES_VERTICAL
180 - ES_WANTRETURN (don't know how to do WM_GETDLGCODE part)
181 - WS_SETFONT
182 - WS_HSCROLL
183 - WS_VSCROLL
187 * RICHED20 TODO (incomplete):
189 * - messages/styles/notifications listed above
190 * - Undo coalescing
191 * - add remaining CHARFORMAT/PARAFORMAT fields
192 * - right/center align should strip spaces from the beginning
193 * - more advanced navigation (Ctrl-arrows)
194 * - tabs
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)
209 * Bugs that are probably fixed, but not so easy to verify:
210 * - EN_UPDATE/EN_CHANGE are handled very incorrectly (should be OK now)
211 * - undo for ME_JoinParagraphs doesn't store paragraph format ? (it does)
212 * - check/fix artificial EOL logic (bCursorAtEnd, hardly logical)
213 * - caret shouldn't be displayed when selection isn't empty
214 * - check refcounting in style management functions (looks perfect now, but no bugs is suspicious)
215 * - undo for setting default format (done, might be buggy)
216 * - styles might be not released properly (looks like they work like charm, but who knows?
220 #include "editor.h"
221 #include "commdlg.h"
222 #include "ole2.h"
223 #include "richole.h"
224 #include "winreg.h"
225 #define NO_SHLWAPI_STREAM
226 #include "shlwapi.h"
228 #include "rtf.h"
230 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
232 int me_debug = 0;
233 HANDLE me_heap = NULL;
235 static ME_TextBuffer *ME_MakeText(void) {
237 ME_TextBuffer *buf = ALLOC_OBJ(ME_TextBuffer);
239 ME_DisplayItem *p1 = ME_MakeDI(diTextStart);
240 ME_DisplayItem *p2 = ME_MakeDI(diTextEnd);
242 p1->prev = NULL;
243 p1->next = p2;
244 p2->prev = p1;
245 p2->next = NULL;
246 p1->member.para.next_para = p2;
247 p2->member.para.prev_para = p1;
248 p2->member.para.nCharOfs = 0;
250 buf->pFirst = p1;
251 buf->pLast = p2;
252 buf->pCharStyle = NULL;
254 return buf;
258 static LRESULT ME_StreamInText(ME_TextEditor *editor, DWORD dwFormat, ME_InStream *stream, ME_Style *style)
260 WCHAR wszText[STREAMIN_BUFFER_SIZE+1];
261 WCHAR *pText;
263 TRACE("%08lx %p\n", dwFormat, stream);
265 do {
266 long nWideChars = 0;
268 if (!stream->dwSize)
270 ME_StreamInFill(stream);
271 if (stream->editstream->dwError)
272 break;
273 if (!stream->dwSize)
274 break;
277 if (!(dwFormat & SF_UNICODE))
279 /* FIXME? this is doomed to fail on true MBCS like UTF-8, luckily they're unlikely to be used as CP_ACP */
280 nWideChars = MultiByteToWideChar(CP_ACP, 0, stream->buffer, stream->dwSize, wszText, STREAMIN_BUFFER_SIZE);
281 pText = wszText;
283 else
285 nWideChars = stream->dwSize >> 1;
286 pText = (WCHAR *)stream->buffer;
289 ME_InsertTextFromCursor(editor, 0, pText, nWideChars, style);
290 if (stream->dwSize < STREAMIN_BUFFER_SIZE)
291 break;
292 stream->dwSize = 0;
293 } while(1);
294 ME_CommitUndo(editor);
295 ME_Repaint(editor);
296 return 0;
299 static void ME_RTFCharAttrHook(RTF_Info *info)
301 CHARFORMAT2W fmt;
302 fmt.cbSize = sizeof(fmt);
303 fmt.dwMask = 0;
305 switch(info->rtfMinor)
307 case rtfPlain:
308 /* FIXME add more flags once they're implemented */
309 fmt.dwMask = CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_COLOR | CFM_BACKCOLOR | CFM_SIZE | CFM_WEIGHT;
310 fmt.dwEffects = CFE_AUTOCOLOR | CFE_AUTOBACKCOLOR;
311 fmt.yHeight = 12*20; /* 12pt */
312 fmt.wWeight = 400;
313 break;
314 case rtfBold:
315 fmt.dwMask = CFM_BOLD;
316 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
317 break;
318 case rtfItalic:
319 fmt.dwMask = CFM_ITALIC;
320 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
321 break;
322 case rtfUnderline:
323 fmt.dwMask = CFM_UNDERLINE;
324 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
325 fmt.bUnderlineType = CFU_CF1UNDERLINE;
326 break;
327 case rtfNoUnderline:
328 fmt.dwMask = CFM_UNDERLINE;
329 fmt.dwEffects = 0;
330 break;
331 case rtfStrikeThru:
332 fmt.dwMask = CFM_STRIKEOUT;
333 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
334 break;
335 case rtfSubScript:
336 case rtfSuperScript:
337 case rtfSubScrShrink:
338 case rtfSuperScrShrink:
339 case rtfNoSuperSub:
340 fmt.dwMask = CFM_SUBSCRIPT|CFM_SUPERSCRIPT;
341 if (info->rtfMinor == rtfSubScrShrink) fmt.dwEffects = CFE_SUBSCRIPT;
342 if (info->rtfMinor == rtfSuperScrShrink) fmt.dwEffects = CFE_SUPERSCRIPT;
343 if (info->rtfMinor == rtfNoSuperSub) fmt.dwEffects = 0;
344 break;
345 case rtfBackColor:
346 fmt.dwMask = CFM_BACKCOLOR;
347 fmt.dwEffects = 0;
348 if (info->rtfParam == 0)
349 fmt.dwEffects = CFE_AUTOBACKCOLOR;
350 else if (info->rtfParam != rtfNoParam)
352 RTFColor *c = RTFGetColor(info, info->rtfParam);
353 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
355 break;
356 case rtfForeColor:
357 fmt.dwMask = CFM_COLOR;
358 fmt.dwEffects = 0;
359 if (info->rtfParam == 0)
360 fmt.dwEffects = CFE_AUTOCOLOR;
361 else if (info->rtfParam != rtfNoParam)
363 RTFColor *c = RTFGetColor(info, info->rtfParam);
364 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
366 break;
367 case rtfFontNum:
368 if (info->rtfParam != rtfNoParam)
370 RTFFont *f = RTFGetFont(info, info->rtfParam);
371 if (f)
373 MultiByteToWideChar(CP_ACP, 0, f->rtfFName, -1, fmt.szFaceName, sizeof(fmt.szFaceName)/sizeof(WCHAR));
374 fmt.szFaceName[sizeof(fmt.szFaceName)/sizeof(WCHAR)-1] = '\0';
375 fmt.bCharSet = f->rtfFCharSet;
376 fmt.dwMask = CFM_FACE | CFM_CHARSET;
379 break;
380 case rtfFontSize:
381 fmt.dwMask = CFM_SIZE;
382 if (info->rtfParam != rtfNoParam)
383 fmt.yHeight = info->rtfParam*10;
384 break;
386 if (fmt.dwMask) {
387 ME_Style *style2;
388 RTFFlushOutputBuffer(info);
389 /* FIXME too slow ? how come ? */
390 style2 = ME_ApplyStyle(info->style, &fmt);
391 ME_ReleaseStyle(info->style);
392 info->style = style2;
396 /* FIXME this function doesn't get any information about context of the RTF tag, which is very bad,
397 the same tags mean different things in different contexts */
398 static void ME_RTFParAttrHook(RTF_Info *info)
400 PARAFORMAT2 fmt;
401 fmt.cbSize = sizeof(fmt);
402 fmt.dwMask = 0;
404 switch(info->rtfMinor)
406 case rtfParDef: /* I'm not 100% sure what does it do, but I guess it restores default paragraph attributes */
407 fmt.dwMask = PFM_ALIGNMENT | PFM_TABSTOPS | PFM_OFFSET | PFM_STARTINDENT;
408 fmt.wAlignment = PFA_LEFT;
409 fmt.cTabCount = 0;
410 fmt.dxOffset = fmt.dxStartIndent = 0;
411 break;
412 case rtfFirstIndent:
413 ME_GetSelectionParaFormat(info->editor, &fmt);
414 fmt.dwMask = PFM_STARTINDENT;
415 fmt.dxStartIndent = info->rtfParam + fmt.dxOffset;
416 break;
417 case rtfLeftIndent:
419 int first, left;
420 ME_GetSelectionParaFormat(info->editor, &fmt);
421 first = fmt.dxStartIndent;
422 left = info->rtfParam;
423 fmt.dwMask = PFM_STARTINDENT|PFM_OFFSET;
424 fmt.dxStartIndent = first + left;
425 fmt.dxOffset = -first;
426 break;
428 case rtfRightIndent:
429 fmt.dwMask = PFM_RIGHTINDENT;
430 fmt.dxRightIndent = info->rtfParam;
431 break;
432 case rtfQuadLeft:
433 case rtfQuadJust:
434 fmt.dwMask = PFM_ALIGNMENT;
435 fmt.wAlignment = PFA_LEFT;
436 break;
437 case rtfQuadRight:
438 fmt.dwMask = PFM_ALIGNMENT;
439 fmt.wAlignment = PFA_RIGHT;
440 break;
441 case rtfQuadCenter:
442 fmt.dwMask = PFM_ALIGNMENT;
443 fmt.wAlignment = PFA_CENTER;
444 break;
445 case rtfTabPos:
446 ME_GetSelectionParaFormat(info->editor, &fmt);
447 if (!(fmt.dwMask & PFM_TABSTOPS))
449 fmt.dwMask |= PFM_TABSTOPS;
450 fmt.cTabCount = 0;
452 if (fmt.cTabCount < MAX_TAB_STOPS)
453 fmt.rgxTabs[fmt.cTabCount++] = info->rtfParam;
454 break;
456 if (fmt.dwMask) {
457 RTFFlushOutputBuffer(info);
458 /* FIXME too slow ? how come ?*/
459 ME_SetSelectionParaFormat(info->editor, &fmt);
463 static void ME_RTFReadHook(RTF_Info *info) {
464 switch(info->rtfClass)
466 case rtfGroup:
467 switch(info->rtfMajor)
469 case rtfBeginGroup:
470 if (info->stackTop < maxStack) {
471 memcpy(&info->stack[info->stackTop].fmt, &info->style->fmt, sizeof(CHARFORMAT2W));
472 info->stack[info->stackTop].codePage = info->codePage;
473 info->stack[info->stackTop].unicodeLength = info->unicodeLength;
475 info->stackTop++;
476 break;
477 case rtfEndGroup:
479 ME_Style *s;
480 RTFFlushOutputBuffer(info);
481 info->stackTop--;
482 /* FIXME too slow ? how come ? */
483 s = ME_ApplyStyle(info->style, &info->stack[info->stackTop].fmt);
484 ME_ReleaseStyle(info->style);
485 info->style = s;
486 info->codePage = info->stack[info->stackTop].codePage;
487 info->unicodeLength = info->stack[info->stackTop].unicodeLength;
488 break;
491 break;
492 case rtfControl:
493 switch(info->rtfMajor)
495 case rtfCharAttr:
496 ME_RTFCharAttrHook(info);
497 break;
498 case rtfParAttr:
499 ME_RTFParAttrHook(info);
500 break;
502 break;
506 void
507 ME_StreamInFill(ME_InStream *stream)
509 stream->editstream->dwError = stream->editstream->pfnCallback(stream->editstream->dwCookie,
510 stream->buffer,
511 sizeof(stream->buffer),
512 &stream->dwSize);
513 stream->dwUsed = 0;
516 static LRESULT ME_StreamIn(ME_TextEditor *editor, DWORD format, EDITSTREAM *stream)
518 RTF_Info parser;
519 ME_Style *style;
520 int from, to, to2, nUndoMode;
521 ME_UndoItem *pUI;
522 int nEventMask = editor->nEventMask;
523 ME_InStream inStream;
525 TRACE("stream==%p hWnd==%p format==0x%X\n", stream, editor->hWnd, (UINT)format);
526 editor->nEventMask = 0;
528 ME_GetSelection(editor, &from, &to);
529 if (format & SFF_SELECTION) {
530 style = ME_GetSelectionInsertStyle(editor);
532 ME_InternalDeleteText(editor, from, to-from);
534 else {
535 style = editor->pBuffer->pDefaultStyle;
536 ME_AddRefStyle(style);
537 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
538 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
539 from = to = 0;
540 ME_ClearTempStyle(editor);
541 /* FIXME restore default paragraph formatting ! */
544 nUndoMode = editor->nUndoMode;
545 editor->nUndoMode = umIgnore;
547 inStream.editstream = stream;
548 inStream.editstream->dwError = 0;
549 inStream.dwSize = 0;
550 inStream.dwUsed = 0;
552 if (format & SF_RTF)
554 /* Check if it's really RTF, and if it is not, use plain text */
555 ME_StreamInFill(&inStream);
556 if (!inStream.editstream->dwError)
558 if (strncmp(inStream.buffer, "{\\rtf1", 6) && strncmp(inStream.buffer, "{\\urtf", 6))
560 format &= ~SF_RTF;
561 format |= SF_TEXT;
566 if (!inStream.editstream->dwError)
568 if (format & SF_RTF) {
569 /* setup the RTF parser */
570 memset(&parser, 0, sizeof parser);
571 RTFSetEditStream(&parser, &inStream);
572 parser.rtfFormat = format&(SF_TEXT|SF_RTF);
573 parser.hwndEdit = editor->hWnd;
574 parser.editor = editor;
575 parser.style = style;
576 WriterInit(&parser);
577 RTFInit(&parser);
578 RTFSetReadHook(&parser, ME_RTFReadHook);
579 BeginFile(&parser);
581 /* do the parsing */
582 RTFRead(&parser);
583 RTFFlushOutputBuffer(&parser);
584 RTFDestroy(&parser);
586 style = parser.style;
588 else if (format & SF_TEXT)
589 ME_StreamInText(editor, format, &inStream, style);
590 else
591 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
592 ME_GetSelection(editor, &to, &to2);
593 /* put the cursor at the top */
594 if (!(format & SFF_SELECTION))
595 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
596 else
598 /* FIXME where to put cursor now ? */
602 editor->nUndoMode = nUndoMode;
603 pUI = ME_AddUndoItem(editor, diUndoDeleteRun, NULL);
604 TRACE("from %d to %d\n", from, to);
605 if (pUI && from < to)
607 pUI->nStart = from;
608 pUI->nLen = to-from;
610 ME_CommitUndo(editor);
611 ME_ReleaseStyle(style);
612 editor->nEventMask = nEventMask;
613 InvalidateRect(editor->hWnd, NULL, TRUE);
614 ME_UpdateRepaint(editor);
615 if (!(format & SFF_SELECTION)) {
616 ME_ClearTempStyle(editor);
618 ME_MoveCaret(editor);
619 ME_SendSelChange(editor);
621 return 0;
625 ME_DisplayItem *
626 ME_FindItemAtOffset(ME_TextEditor *editor, ME_DIType nItemType, int nOffset, int *nItemOffset)
628 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
630 while (item && item->member.para.next_para->member.para.nCharOfs <= nOffset)
631 item = ME_FindItemFwd(item, diParagraph);
633 if (!item)
634 return item;
636 nOffset -= item->member.para.nCharOfs;
637 if (nItemType == diParagraph) {
638 if (nItemOffset)
639 *nItemOffset = nOffset;
640 return item;
643 do {
644 item = ME_FindItemFwd(item, diRun);
645 } while (item && (item->member.run.nCharOfs + ME_StrLen(item->member.run.strText) <= nOffset));
646 if (item) {
647 nOffset -= item->member.run.nCharOfs;
648 if (nItemOffset)
649 *nItemOffset = nOffset;
651 return item;
655 static int
656 ME_FindText(ME_TextEditor *editor, DWORD flags, CHARRANGE *chrg, WCHAR *text, CHARRANGE *chrgText)
658 int nStart = chrg->cpMin;
659 int nLen = lstrlenW(text);
660 ME_DisplayItem *item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
661 ME_DisplayItem *para;
663 if (!item)
664 return -1;
666 if (!nLen)
668 if (chrgText)
669 chrgText->cpMin = chrgText->cpMax = chrg->cpMin;
670 return chrg->cpMin;
673 if (!(flags & FR_DOWN))
674 FIXME("Backward search not implemented\n");
675 if (!(flags & FR_MATCHCASE))
676 FIXME("Case-insensitive search not implemented\n");
677 if (flags & ~(FR_DOWN | FR_MATCHCASE))
678 FIXME("Flags 0x%08lx not implemented\n", flags & ~(FR_DOWN | FR_MATCHCASE));
680 para = ME_GetParagraph(item);
681 while (item && para->member.para.nCharOfs + item->member.run.nCharOfs + nStart + nLen < chrg->cpMax)
683 ME_DisplayItem *pCurItem = item;
684 int nCurStart = nStart;
685 int nMatched = 0;
687 while (pCurItem->member.run.strText->szData[nCurStart + nMatched] == text[nMatched])
689 nMatched++;
690 if (nMatched == nLen)
692 nStart += para->member.para.nCharOfs + item->member.run.nCharOfs;
693 if (chrgText)
695 chrgText->cpMin = nStart;
696 chrgText->cpMax = nStart + nLen;
698 return nStart;
700 if (nCurStart + nMatched == ME_StrLen(pCurItem->member.run.strText))
702 pCurItem = ME_FindItemFwd(pCurItem, diRun);
703 nCurStart = -nMatched;
706 nStart++;
707 if (nStart == ME_StrLen(item->member.run.strText))
709 item = ME_FindItemFwd(item, diRun);
710 para = ME_GetParagraph(item);
711 nStart = 0;
714 return -1;
718 ME_TextEditor *ME_MakeEditor(HWND hWnd) {
719 ME_TextEditor *ed = ALLOC_OBJ(ME_TextEditor);
720 HDC hDC;
721 int i;
722 ed->hWnd = hWnd;
723 ed->bEmulateVersion10 = FALSE;
724 ed->pBuffer = ME_MakeText();
725 hDC = GetDC(hWnd);
726 ME_MakeFirstParagraph(hDC, ed->pBuffer);
727 ReleaseDC(hWnd, hDC);
728 ed->bCaretShown = FALSE;
729 ed->nCursors = 3;
730 ed->pCursors = ALLOC_N_OBJ(ME_Cursor, ed->nCursors);
731 ed->pCursors[0].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
732 ed->pCursors[0].nOffset = 0;
733 ed->pCursors[1].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
734 ed->pCursors[1].nOffset = 0;
735 ed->nLastTotalLength = ed->nTotalLength = 0;
736 ed->nUDArrowX = -1;
737 ed->nSequence = 0;
738 ed->rgbBackColor = -1;
739 ed->bCaretAtEnd = FALSE;
740 ed->nEventMask = 0;
741 ed->nModifyStep = 0;
742 ed->pUndoStack = ed->pRedoStack = NULL;
743 ed->nUndoMode = umAddToUndo;
744 ed->nParagraphs = 1;
745 ed->nLastSelStart = ed->nLastSelEnd = 0;
746 ed->nScrollPosY = 0;
747 for (i=0; i<HFONT_CACHE_SIZE; i++)
749 ed->pFontCache[i].nRefs = 0;
750 ed->pFontCache[i].nAge = 0;
751 ed->pFontCache[i].hFont = NULL;
753 ME_CheckCharOffsets(ed);
754 return ed;
757 typedef struct tagME_GlobalDestStruct
759 HGLOBAL hData;
760 int nLength;
761 } ME_GlobalDestStruct;
763 static DWORD CALLBACK ME_AppendToHGLOBAL(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
765 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
766 int nMaxSize;
767 BYTE *pDest;
769 nMaxSize = GlobalSize(pData->hData);
770 if (pData->nLength+cb+1 >= cb)
772 /* round up to 2^17 */
773 int nNewSize = (((nMaxSize+cb+1)|0x1FFFF)+1) & 0xFFFE0000;
774 pData->hData = GlobalReAlloc(pData->hData, nNewSize, 0);
776 pDest = (BYTE *)GlobalLock(pData->hData);
777 memcpy(pDest + pData->nLength, lpBuff, cb);
778 pData->nLength += cb;
779 pDest[pData->nLength] = '\0';
780 GlobalUnlock(pData->hData);
781 *pcb = cb;
783 return 0;
786 static DWORD CALLBACK ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
788 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
789 int i;
790 WORD *pSrc, *pDest;
792 cb = cb >> 1;
793 pDest = (WORD *)lpBuff;
794 pSrc = (WORD *)GlobalLock(pData->hData);
795 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
796 pDest[i] = pSrc[pData->nLength+i];
798 pData->nLength += i;
799 *pcb = 2*i;
800 GlobalUnlock(pData->hData);
801 return 0;
804 static DWORD CALLBACK ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
806 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
807 int i;
808 BYTE *pSrc, *pDest;
810 pDest = lpBuff;
811 pSrc = (BYTE *)GlobalLock(pData->hData);
812 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
813 pDest[i] = pSrc[pData->nLength+i];
815 pData->nLength += i;
816 *pcb = i;
817 GlobalUnlock(pData->hData);
818 return 0;
822 void ME_DestroyEditor(ME_TextEditor *editor)
824 ME_DisplayItem *pFirst = editor->pBuffer->pFirst;
825 ME_DisplayItem *p = pFirst, *pNext = NULL;
826 int i;
828 ME_ClearTempStyle(editor);
829 ME_EmptyUndoStack(editor);
830 while(p) {
831 pNext = p->next;
832 ME_DestroyDisplayItem(p);
833 p = pNext;
835 ME_ReleaseStyle(editor->pBuffer->pDefaultStyle);
836 for (i=0; i<HFONT_CACHE_SIZE; i++)
838 if (editor->pFontCache[i].hFont)
839 DeleteObject(editor->pFontCache[i].hFont);
842 FREE_OBJ(editor);
845 static WCHAR wszClassName[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '2', '0', 'W', 0};
846 static WCHAR wszClassName50[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '5', '0', 'W', 0};
848 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
850 TRACE("\n");
851 switch (fdwReason)
853 case DLL_PROCESS_ATTACH:
854 DisableThreadLibraryCalls(hinstDLL);
855 me_heap = HeapCreate (0, 0x10000, 0);
856 ME_RegisterEditorClass(hinstDLL);
857 break;
859 case DLL_PROCESS_DETACH:
860 UnregisterClassW(wszClassName, 0);
861 UnregisterClassW(wszClassName50, 0);
862 UnregisterClassA("RichEdit20A", 0);
863 UnregisterClassA("RichEdit50A", 0);
864 HeapDestroy (me_heap);
865 me_heap = NULL;
866 break;
868 return TRUE;
872 #define UNSUPPORTED_MSG(e) \
873 case e: \
874 FIXME(#e ": stub\n"); \
875 return DefWindowProcW(hWnd, msg, wParam, lParam);
877 /******************************************************************
878 * RichEditANSIWndProc (RICHED20.10)
880 LRESULT WINAPI RichEditANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
881 HDC hDC;
882 PAINTSTRUCT ps;
883 SCROLLINFO si;
884 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongW(hWnd, 0);
885 TRACE("msg %d %08x %08lx\n", msg, wParam, lParam);
886 switch(msg) {
888 UNSUPPORTED_MSG(EM_AUTOURLDETECT)
889 UNSUPPORTED_MSG(EM_CHARFROMPOS)
890 UNSUPPORTED_MSG(EM_DISPLAYBAND)
891 UNSUPPORTED_MSG(EM_EXLIMITTEXT)
892 UNSUPPORTED_MSG(EM_FINDWORDBREAK)
893 UNSUPPORTED_MSG(EM_FMTLINES)
894 UNSUPPORTED_MSG(EM_FORMATRANGE)
895 UNSUPPORTED_MSG(EM_GETAUTOURLDETECT)
896 UNSUPPORTED_MSG(EM_GETBIDIOPTIONS)
897 UNSUPPORTED_MSG(EM_GETEDITSTYLE)
898 UNSUPPORTED_MSG(EM_GETFIRSTVISIBLELINE)
899 UNSUPPORTED_MSG(EM_GETIMECOMPMODE)
900 /* UNSUPPORTED_MSG(EM_GETIMESTATUS) missing in Wine headers */
901 UNSUPPORTED_MSG(EM_GETLANGOPTIONS)
902 UNSUPPORTED_MSG(EM_GETLIMITTEXT)
903 UNSUPPORTED_MSG(EM_GETLINE)
904 /* UNSUPPORTED_MSG(EM_GETOLEINTERFACE) separate stub */
905 UNSUPPORTED_MSG(EM_GETOPTIONS)
906 UNSUPPORTED_MSG(EM_GETPASSWORDCHAR)
907 UNSUPPORTED_MSG(EM_GETRECT)
908 UNSUPPORTED_MSG(EM_GETREDONAME)
909 UNSUPPORTED_MSG(EM_GETSCROLLPOS)
910 UNSUPPORTED_MSG(EM_GETTEXTMODE)
911 UNSUPPORTED_MSG(EM_GETTYPOGRAPHYOPTIONS)
912 UNSUPPORTED_MSG(EM_GETUNDONAME)
913 UNSUPPORTED_MSG(EM_GETWORDBREAKPROC)
914 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX)
915 UNSUPPORTED_MSG(EM_GETZOOM)
916 UNSUPPORTED_MSG(EM_HIDESELECTION)
917 UNSUPPORTED_MSG(EM_LIMITTEXT) /* also known as EM_SETLIMITTEXT */
918 UNSUPPORTED_MSG(EM_PASTESPECIAL)
919 /* UNSUPPORTED_MSG(EM_POSFROMCHARS) missing in Wine headers */
920 UNSUPPORTED_MSG(EM_REQUESTRESIZE)
921 UNSUPPORTED_MSG(EM_SCROLL)
922 UNSUPPORTED_MSG(EM_SCROLLCARET)
923 UNSUPPORTED_MSG(EM_SELECTIONTYPE)
924 UNSUPPORTED_MSG(EM_SETBIDIOPTIONS)
925 UNSUPPORTED_MSG(EM_SETEDITSTYLE)
926 UNSUPPORTED_MSG(EM_SETFONTSIZE)
927 UNSUPPORTED_MSG(EM_SETLANGOPTIONS)
928 UNSUPPORTED_MSG(EM_SETOLECALLBACK)
929 UNSUPPORTED_MSG(EM_SETOPTIONS)
930 UNSUPPORTED_MSG(EM_SETPALETTE)
931 UNSUPPORTED_MSG(EM_SETPASSWORDCHAR)
932 UNSUPPORTED_MSG(EM_SETRECT)
933 UNSUPPORTED_MSG(EM_SETRECTNP)
934 UNSUPPORTED_MSG(EM_SETSCROLLPOS)
935 UNSUPPORTED_MSG(EM_SETTABSTOPS)
936 UNSUPPORTED_MSG(EM_SETTARGETDEVICE)
937 UNSUPPORTED_MSG(EM_SETTEXTMODE)
938 UNSUPPORTED_MSG(EM_SETTYPOGRAPHYOPTIONS)
939 UNSUPPORTED_MSG(EM_SETUNDOLIMIT)
940 UNSUPPORTED_MSG(EM_SETWORDBREAKPROC)
941 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX)
942 UNSUPPORTED_MSG(EM_SHOWSCROLLBAR)
943 UNSUPPORTED_MSG(EM_SETZOOM)
944 UNSUPPORTED_MSG(WM_SETFONT)
945 UNSUPPORTED_MSG(WM_STYLECHANGING)
946 UNSUPPORTED_MSG(WM_STYLECHANGED)
947 /* UNSUPPORTED_MSG(WM_UNICHAR) FIXME missing in Wine headers */
949 /* Messages specific to Richedit controls */
951 case EM_STREAMIN:
952 return ME_StreamIn(editor, wParam, (EDITSTREAM*)lParam);
953 case EM_STREAMOUT:
954 return ME_StreamOut(editor, wParam, (EDITSTREAM *)lParam);
955 case WM_GETDLGCODE:
957 UINT code = DLGC_WANTCHARS|DLGC_WANTARROWS;
958 if (GetWindowLongW(hWnd, GWL_STYLE)&ES_WANTRETURN)
959 code |= 0; /* FIXME what can we do here ? ask for messages and censor them ? */
960 return code;
962 case WM_NCCREATE:
964 CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
965 editor = ME_MakeEditor(hWnd);
966 SetWindowLongW(hWnd, 0, (long)editor);
967 pcs = 0; /* ignore */
968 return TRUE;
970 case EM_EMPTYUNDOBUFFER:
971 ME_EmptyUndoStack(editor);
972 return 0;
973 case EM_GETSEL:
975 /* Note: wParam/lParam can be NULL */
976 UINT from, to;
977 PUINT pfrom = wParam ? (PUINT)wParam : &from;
978 PUINT pto = lParam ? (PUINT)lParam : &to;
979 ME_GetSelection(editor, pfrom, pto);
980 if ((*pfrom|*pto) & 0xFFFF0000)
981 return -1;
982 return MAKELONG(*pfrom,*pto);
984 case EM_EXGETSEL:
986 CHARRANGE *pRange = (CHARRANGE *)lParam;
987 ME_GetSelection(editor, (int *)&pRange->cpMin, (int *)&pRange->cpMax);
988 return 0;
990 case EM_CANUNDO:
991 return editor->pUndoStack != NULL;
992 case EM_CANREDO:
993 return editor->pRedoStack != NULL;
994 case EM_UNDO:
995 ME_Undo(editor);
996 return 0;
997 case EM_REDO:
998 ME_Redo(editor);
999 return 0;
1000 case EM_SETSEL:
1002 ME_SetSelection(editor, wParam, lParam);
1003 ME_Repaint(editor);
1004 ME_SendSelChange(editor);
1005 return 0;
1007 case EM_EXSETSEL:
1009 CHARRANGE *pRange = (CHARRANGE *)lParam;
1010 ME_SetSelection(editor, pRange->cpMin, pRange->cpMax);
1011 /* FIXME optimize */
1012 ME_Repaint(editor);
1013 ME_SendSelChange(editor);
1014 return 0;
1016 case EM_SETTEXTEX:
1018 LPWSTR wszText = (LPWSTR)lParam;
1019 SETTEXTEX *pStruct = (SETTEXTEX *)wParam;
1020 size_t len = lstrlenW(wszText);
1021 int from, to;
1022 ME_Style *style;
1023 TRACE("EM_SETTEXEX - %s, flags %d, cp %d\n", debugstr_w(wszText), (int)pStruct->flags, pStruct->codepage);
1024 if (pStruct->codepage != 1200) {
1025 FIXME("EM_SETTEXTEX only supports unicode right now!\n");
1026 return 0;
1028 /* FIXME: this should support RTF strings too, according to MSDN */
1029 if (pStruct->flags & ST_SELECTION) {
1030 ME_GetSelection(editor, &from, &to);
1031 style = ME_GetSelectionInsertStyle(editor);
1032 ME_InternalDeleteText(editor, from, to - from);
1033 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
1034 ME_ReleaseStyle(style);
1036 else {
1037 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
1038 ME_InsertTextFromCursor(editor, 0, wszText, -1, editor->pBuffer->pDefaultStyle);
1039 len = 1;
1041 ME_CommitUndo(editor);
1042 if (!(pStruct->flags & ST_KEEPUNDO))
1043 ME_EmptyUndoStack(editor);
1044 ME_UpdateRepaint(editor);
1045 return len;
1047 case EM_SETBKGNDCOLOR:
1049 LRESULT lColor = ME_GetBackColor(editor);
1050 if (wParam)
1051 editor->rgbBackColor = -1;
1052 else
1053 editor->rgbBackColor = lParam;
1054 InvalidateRect(hWnd, NULL, TRUE);
1055 UpdateWindow(hWnd);
1056 return lColor;
1058 case EM_GETMODIFY:
1059 return editor->nModifyStep == 0 ? 0 : 1;
1060 case EM_SETMODIFY:
1062 if (wParam)
1063 editor->nModifyStep = 0x80000000;
1064 else
1065 editor->nModifyStep = 0;
1067 return 0;
1069 case EM_SETREADONLY:
1071 long nStyle = GetWindowLongW(hWnd, GWL_STYLE);
1072 if (wParam)
1073 nStyle |= ES_READONLY;
1074 else
1075 nStyle &= ~ES_READONLY;
1076 SetWindowLongW(hWnd, GWL_STYLE, nStyle);
1077 ME_Repaint(editor);
1078 return 0;
1080 case EM_SETEVENTMASK:
1081 editor->nEventMask = lParam;
1082 return 0;
1083 case EM_GETEVENTMASK:
1084 return editor->nEventMask;
1085 case EM_SETCHARFORMAT:
1087 CHARFORMAT2W buf, *p;
1088 BOOL bRepaint = TRUE;
1089 p = ME_ToCF2W(&buf, (CHARFORMAT2W *)lParam);
1090 if (!wParam)
1091 ME_SetDefaultCharFormat(editor, p);
1092 else if (wParam == (SCF_WORD | SCF_SELECTION))
1093 FIXME("word selection not supported\n");
1094 else if (wParam == SCF_ALL)
1095 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), p);
1096 else {
1097 int from, to;
1098 ME_GetSelection(editor, &from, &to);
1099 bRepaint = (from != to);
1100 ME_SetSelectionCharFormat(editor, p);
1102 ME_CommitUndo(editor);
1103 if (bRepaint)
1104 ME_UpdateRepaint(editor);
1105 return 0;
1107 case EM_GETCHARFORMAT:
1109 CHARFORMAT2W tmp, *dst = (CHARFORMAT2W *)lParam;
1110 if (dst->cbSize != sizeof(CHARFORMATA) &&
1111 dst->cbSize != sizeof(CHARFORMATW) &&
1112 dst->cbSize != sizeof(CHARFORMAT2A) &&
1113 dst->cbSize != sizeof(CHARFORMAT2W))
1114 return 0;
1115 tmp.cbSize = sizeof(tmp);
1116 if (!wParam)
1117 ME_GetDefaultCharFormat(editor, &tmp);
1118 else
1119 ME_GetSelectionCharFormat(editor, &tmp);
1120 ME_CopyToCFAny(dst, &tmp);
1121 return tmp.dwMask;
1123 case EM_SETPARAFORMAT:
1124 ME_SetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
1125 ME_UpdateRepaint(editor);
1126 ME_CommitUndo(editor);
1127 return 0;
1128 case EM_GETPARAFORMAT:
1129 ME_GetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
1130 return 0;
1131 case EM_LINESCROLL:
1133 int nPos = editor->nScrollPosY, nEnd= editor->nTotalLength - editor->sizeWindow.cy;
1134 nPos += 8 * lParam; /* FIXME follow the original */
1135 if (nPos>=nEnd)
1136 nPos = nEnd;
1137 if (nPos<0)
1138 nPos = 0;
1139 if (nPos != editor->nScrollPosY) {
1140 ScrollWindow(hWnd, 0, editor->nScrollPosY-nPos, NULL, NULL);
1141 editor->nScrollPosY = nPos;
1142 SetScrollPos(hWnd, SB_VERT, nPos, TRUE);
1143 UpdateWindow(hWnd);
1145 return TRUE; /* Should return false if a single line richedit control */
1147 case WM_CLEAR:
1149 int from, to;
1150 ME_GetSelection(editor, &from, &to);
1151 ME_InternalDeleteText(editor, from, to-from);
1152 ME_CommitUndo(editor);
1153 ME_UpdateRepaint(editor);
1154 return 0;
1156 case EM_REPLACESEL:
1158 int from, to;
1159 ME_Style *style;
1160 LPWSTR wszText = ME_ToUnicode(hWnd, (void *)lParam);
1161 size_t len = lstrlenW(wszText);
1162 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText));
1164 ME_GetSelection(editor, &from, &to);
1165 style = ME_GetSelectionInsertStyle(editor);
1166 ME_InternalDeleteText(editor, from, to-from);
1167 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
1168 ME_ReleaseStyle(style);
1169 ME_EndToUnicode(hWnd, wszText);
1170 /* drop temporary style if line end */
1171 /* FIXME question: does abc\n mean: put abc, clear temp style, put \n? (would require a change) */
1172 if (len>0 && wszText[len-1] == '\n')
1173 ME_ClearTempStyle(editor);
1175 ME_CommitUndo(editor);
1176 if (!wParam)
1177 ME_EmptyUndoStack(editor);
1178 ME_UpdateRepaint(editor);
1179 return 0;
1181 case WM_SETTEXT:
1183 LPWSTR wszText = ME_ToUnicode(hWnd, (void *)lParam);
1184 TRACE("WM_SETTEXT - %s\n", (char *)(wszText)); /* debugstr_w() */
1185 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
1186 /* uses default style! */
1187 ME_InsertTextFromCursor(editor, 0, wszText, -1, editor->pBuffer->pDefaultStyle);
1188 ME_EndToUnicode(hWnd, wszText);
1189 ME_CommitUndo(editor);
1190 ME_EmptyUndoStack(editor);
1191 ME_UpdateRepaint(editor);
1192 return 0;
1194 case EM_CANPASTE:
1196 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
1197 if (IsClipboardFormatAvailable(nRTFFormat))
1198 return TRUE;
1199 if (IsClipboardFormatAvailable(CF_UNICODETEXT))
1200 return TRUE;
1201 return FALSE;
1203 case WM_PASTE:
1205 DWORD dwFormat = 0;
1206 EDITSTREAM es;
1207 ME_GlobalDestStruct gds;
1208 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
1209 UINT cf = 0;
1211 if (IsClipboardFormatAvailable(nRTFFormat))
1212 cf = nRTFFormat, dwFormat = SF_RTF;
1213 else if (IsClipboardFormatAvailable(CF_UNICODETEXT))
1214 cf = CF_UNICODETEXT, dwFormat = SF_TEXT|SF_UNICODE;
1215 else
1216 return 0;
1218 if (!OpenClipboard(hWnd))
1219 return 0;
1220 gds.hData = GetClipboardData(cf);
1221 gds.nLength = 0;
1222 es.dwCookie = (DWORD)&gds;
1223 es.pfnCallback = dwFormat == SF_RTF ? ME_ReadFromHGLOBALRTF : ME_ReadFromHGLOBALUnicode;
1224 SendMessageW(hWnd, EM_STREAMIN, dwFormat|SFF_SELECTION, (LPARAM)&es);
1226 CloseClipboard();
1227 return 0;
1229 case WM_CUT:
1230 case WM_COPY:
1232 int from, to, pars;
1233 WCHAR *data;
1234 HANDLE hData;
1235 EDITSTREAM es;
1236 ME_GlobalDestStruct gds;
1238 if (!OpenClipboard(hWnd))
1239 return 0;
1241 EmptyClipboard();
1242 ME_GetSelection(editor, &from, &to);
1243 pars = ME_CountParagraphsBetween(editor, from, to);
1244 hData = GlobalAlloc(GMEM_MOVEABLE, sizeof(WCHAR)*(to-from+pars+1));
1245 data = (WCHAR *)GlobalLock(hData);
1246 ME_GetTextW(editor, data, from, to-from, TRUE);
1247 GlobalUnlock(hData);
1249 gds.hData = GlobalAlloc(GMEM_MOVEABLE, 0);
1250 gds.nLength = 0;
1251 es.dwCookie = (DWORD)&gds;
1252 es.pfnCallback = ME_AppendToHGLOBAL;
1253 SendMessageW(hWnd, EM_STREAMOUT, SFF_SELECTION|SF_RTF, (LPARAM)&es);
1254 GlobalReAlloc(gds.hData, gds.nLength+1, 0);
1256 SetClipboardData(CF_UNICODETEXT, hData);
1257 SetClipboardData(RegisterClipboardFormatA("Rich Text Format"), gds.hData);
1259 CloseClipboard();
1260 if (msg == WM_CUT)
1262 ME_InternalDeleteText(editor, from, to-from);
1263 ME_CommitUndo(editor);
1264 ME_UpdateRepaint(editor);
1266 return 0;
1268 case WM_GETTEXTLENGTH:
1269 return ME_GetTextLength(editor);
1270 case EM_GETTEXTLENGTHEX:
1271 return ME_GetTextLengthEx(editor, (GETTEXTLENGTHEX *)wParam);
1272 case WM_GETTEXT:
1274 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
1275 tr.chrg.cpMin = 0;
1276 tr.chrg.cpMax = wParam-1;
1277 tr.lpstrText = (WCHAR *)lParam;
1278 return RichEditANSIWndProc(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
1280 case EM_GETTEXTEX:
1282 GETTEXTEX *ex = (GETTEXTEX*)wParam;
1284 if (ex->flags != 0)
1285 FIXME("Unhandled EM_GETTEXTEX flags 0x%lx\n",ex->flags);
1287 if (IsWindowUnicode(hWnd))
1288 return ME_GetTextW(editor, (LPWSTR)lParam, 0, ex->cb, FALSE);
1289 else
1291 LPWSTR buffer = HeapAlloc(GetProcessHeap(),0,ex->cb*sizeof(WCHAR));
1292 DWORD buflen = ex->cb;
1293 LRESULT rc;
1294 DWORD flags = 0;
1296 buflen = ME_GetTextW(editor, buffer, 0, buflen, FALSE);
1297 rc = WideCharToMultiByte(ex->codepage, flags, buffer, buflen, (LPSTR)lParam, ex->cb, ex->lpDefaultChar, ex->lpUsedDefaultChar);
1299 HeapFree(GetProcessHeap(),0,buffer);
1300 return rc;
1303 case EM_GETSELTEXT:
1305 int from, to;
1306 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
1307 ME_GetSelection(editor, &from, &to);
1308 tr.chrg.cpMin = from;
1309 tr.chrg.cpMax = to;
1310 tr.lpstrText = (WCHAR *)lParam;
1311 return RichEditANSIWndProc(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
1313 case EM_GETTEXTRANGE:
1315 TEXTRANGEW *rng = (TEXTRANGEW *)lParam;
1316 if (IsWindowUnicode(hWnd))
1317 return ME_GetTextW(editor, rng->lpstrText, rng->chrg.cpMin, rng->chrg.cpMax-rng->chrg.cpMin, FALSE);
1318 else
1320 int nLen = rng->chrg.cpMax-rng->chrg.cpMin;
1321 WCHAR *p = ALLOC_N_OBJ(WCHAR, nLen+1);
1322 int nChars = ME_GetTextW(editor, p, rng->chrg.cpMin, nLen, FALSE);
1323 /* FIXME this is a potential security hole (buffer overrun)
1324 if you know more about wchar->mbyte conversion please explain
1326 WideCharToMultiByte(CP_ACP, 0, p, nChars+1, (char *)rng->lpstrText, nLen+1, NULL, NULL);
1327 FREE_OBJ(p);
1328 return nChars;
1330 return ME_GetTextW(editor, rng->lpstrText, rng->chrg.cpMin, rng->chrg.cpMax-rng->chrg.cpMin, FALSE);
1332 case EM_GETLINECOUNT:
1334 ME_DisplayItem *item = editor->pBuffer->pFirst->next;
1335 int nRows = 0;
1337 while (item != editor->pBuffer->pLast)
1339 assert(item->type == diParagraph);
1340 nRows += item->member.para.nRows;
1341 item = item->member.para.next_para;
1343 return max(1, nRows);
1345 case EM_LINEFROMCHAR:
1347 if (wParam == -1)
1348 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(editor, 1));
1349 else
1350 return ME_RowNumberFromCharOfs(editor, wParam);
1352 case EM_EXLINEFROMCHAR:
1354 return ME_RowNumberFromCharOfs(editor, lParam);
1356 case EM_LINEINDEX:
1358 ME_DisplayItem *item, *para;
1360 if (wParam == -1)
1361 item = ME_FindItemBack(editor->pCursors[0].pRun, diStartRow);
1362 else
1363 item = ME_FindRowWithNumber(editor, wParam);
1364 if (!item)
1365 return -1;
1366 para = ME_GetParagraph(item);
1367 item = ME_FindItemFwd(item, diRun);
1368 return para->member.para.nCharOfs + item->member.run.nCharOfs;
1370 case EM_LINELENGTH:
1372 ME_DisplayItem *item, *item_end;
1373 int nChars = 0;
1375 if (wParam > ME_GetTextLength(editor))
1376 return 0;
1377 if (wParam == -1)
1379 FIXME("EM_LINELENGTH: returning number of unselected characters on lines with selection unsupported.\n");
1380 return 0;
1382 item = ME_FindItemAtOffset(editor, diRun, wParam, NULL);
1383 item = ME_RowStart(item);
1384 item_end = ME_RowEnd(item);
1385 if (!item_end)
1387 /* Empty buffer, no runs */
1388 nChars = 0;
1390 else
1392 nChars = ME_CharOfsFromRunOfs(editor, item_end, ME_StrLen(item_end->member.run.strText));
1393 nChars -= ME_CharOfsFromRunOfs(editor, item, 0);
1395 TRACE("EM_LINELENGTH(%d)==%d\n",wParam, nChars);
1396 return nChars;
1398 case EM_FINDTEXT:
1400 FINDTEXTA *ft = (FINDTEXTA *)lParam;
1401 int nChars = MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, NULL, 0);
1402 WCHAR *tmp;
1404 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
1405 MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, tmp, nChars);
1406 return ME_FindText(editor, wParam, &ft->chrg, tmp, NULL);
1408 case EM_FINDTEXTEX:
1410 FINDTEXTEXA *ex = (FINDTEXTEXA *)lParam;
1411 int nChars = MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, NULL, 0);
1412 WCHAR *tmp;
1414 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
1415 MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, tmp, nChars);
1416 return ME_FindText(editor, wParam, &ex->chrg, tmp, &ex->chrgText);
1418 case EM_FINDTEXTW:
1420 FINDTEXTW *ft = (FINDTEXTW *)lParam;
1421 return ME_FindText(editor, wParam, &ft->chrg, ft->lpstrText, NULL);
1423 case EM_FINDTEXTEXW:
1425 FINDTEXTEXW *ex = (FINDTEXTEXW *)lParam;
1426 return ME_FindText(editor, wParam, &ex->chrg, ex->lpstrText, &ex->chrgText);
1428 case WM_CREATE:
1429 ME_CommitUndo(editor);
1430 ME_WrapMarkedParagraphs(editor);
1431 ME_MoveCaret(editor);
1432 return 0;
1433 case WM_DESTROY:
1434 ME_DestroyEditor(editor);
1435 SetWindowLongW(hWnd, 0, 0);
1436 return 0;
1437 case WM_LBUTTONDOWN:
1438 SetFocus(hWnd);
1439 ME_LButtonDown(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
1440 SetCapture(hWnd);
1441 break;
1442 case WM_MOUSEMOVE:
1443 if (GetCapture() == hWnd)
1444 ME_MouseMove(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
1445 break;
1446 case WM_LBUTTONUP:
1447 if (GetCapture() == hWnd)
1448 ReleaseCapture();
1449 break;
1450 case WM_PAINT:
1451 hDC = BeginPaint(hWnd, &ps);
1452 ME_PaintContent(editor, hDC, FALSE, &ps.rcPaint);
1453 EndPaint(hWnd, &ps);
1454 break;
1455 case WM_SETFOCUS:
1456 ME_ShowCaret(editor);
1457 ME_SendOldNotify(editor, EN_SETFOCUS);
1458 return 0;
1459 case WM_KILLFOCUS:
1460 ME_HideCaret(editor);
1461 ME_SendOldNotify(editor, EN_KILLFOCUS);
1462 return 0;
1463 case WM_ERASEBKGND:
1465 HDC hDC = (HDC)wParam;
1466 RECT rc;
1467 COLORREF rgbBG = ME_GetBackColor(editor);
1468 if (GetUpdateRect(hWnd,&rc,TRUE))
1470 HBRUSH hbr = CreateSolidBrush(rgbBG);
1471 FillRect(hDC, &rc, hbr);
1472 DeleteObject(hbr);
1474 return 1;
1476 case WM_COMMAND:
1477 TRACE("editor wnd command = %d\n", LOWORD(wParam));
1478 return 0;
1479 case WM_KEYDOWN:
1480 if (ME_ArrowKey(editor, LOWORD(wParam), GetKeyState(VK_CONTROL)<0)) {
1481 ME_CommitUndo(editor);
1482 ME_EnsureVisible(editor, editor->pCursors[0].pRun);
1483 HideCaret(hWnd);
1484 ME_MoveCaret(editor);
1485 ShowCaret(hWnd);
1486 return 0;
1488 if (GetKeyState(VK_CONTROL)<0)
1490 if (LOWORD(wParam)=='W')
1492 CHARFORMAT2W chf;
1493 char buf[2048];
1494 ME_GetSelectionCharFormat(editor, &chf);
1495 ME_DumpStyleToBuf(&chf, buf);
1496 MessageBoxA(NULL, buf, "Style dump", MB_OK);
1498 if (LOWORD(wParam)=='Q')
1500 ME_CheckCharOffsets(editor);
1503 goto do_default;
1504 case WM_CHAR:
1506 WCHAR wstr;
1507 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY) {
1508 MessageBeep(MB_ICONERROR);
1509 return 0; /* FIXME really 0 ? */
1511 wstr = LOWORD(wParam);
1512 if (((unsigned)wstr)>=' ' || wstr=='\r' || wstr=='\t') {
1513 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
1514 ME_Style *style = ME_GetInsertStyle(editor, 0);
1515 ME_SaveTempStyle(editor);
1516 ME_InsertTextFromCursor(editor, 0, &wstr, 1, style);
1517 ME_ReleaseStyle(style);
1518 ME_CommitUndo(editor);
1519 ME_UpdateRepaint(editor);
1521 return 0;
1523 case WM_VSCROLL:
1525 int nPos = editor->nScrollPosY;
1526 si.cbSize = sizeof(SCROLLINFO);
1527 si.fMask = SIF_PAGE|SIF_POS|SIF_RANGE|SIF_TRACKPOS;
1528 GetScrollInfo(hWnd, SB_VERT, &si);
1529 switch(LOWORD(wParam)) {
1530 case SB_LINEUP:
1531 nPos -= 24; /* FIXME follow the original */
1532 if (nPos<0) nPos = 0;
1533 break;
1534 case SB_LINEDOWN:
1536 int nEnd = editor->nTotalLength - editor->sizeWindow.cy;
1537 nPos += 24; /* FIXME follow the original */
1538 if (nPos>=nEnd) nPos = nEnd;
1539 break;
1541 case SB_PAGEUP:
1542 nPos -= editor->sizeWindow.cy;
1543 if (nPos<0) nPos = 0;
1544 break;
1545 case SB_PAGEDOWN:
1546 nPos += editor->sizeWindow.cy;
1547 if (nPos>=editor->nTotalLength) nPos = editor->nTotalLength-1;
1548 break;
1549 case SB_THUMBTRACK:
1550 case SB_THUMBPOSITION:
1551 nPos = si.nTrackPos;
1552 break;
1554 if (nPos != editor->nScrollPosY) {
1555 ScrollWindow(hWnd, 0, editor->nScrollPosY-nPos, NULL, NULL);
1556 editor->nScrollPosY = nPos;
1557 SetScrollPos(hWnd, SB_VERT, nPos, TRUE);
1558 UpdateWindow(hWnd);
1560 break;
1562 case WM_MOUSEWHEEL:
1564 int gcWheelDelta = 0, nPos = editor->nScrollPosY, nEnd = editor->nTotalLength - editor->sizeWindow.cy;
1565 UINT pulScrollLines;
1566 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
1567 gcWheelDelta -= GET_WHEEL_DELTA_WPARAM(wParam);
1568 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
1569 nPos += pulScrollLines * (gcWheelDelta / WHEEL_DELTA) * 8; /* FIXME follow the original */
1570 if (nPos>=nEnd)
1571 nPos = nEnd;
1572 if (nPos<0)
1573 nPos = 0;
1574 if (nPos != editor->nScrollPosY) {
1575 ScrollWindow(hWnd, 0, editor->nScrollPosY-nPos, NULL, NULL);
1576 editor->nScrollPosY = nPos;
1577 SetScrollPos(hWnd, SB_VERT, nPos, TRUE);
1578 UpdateWindow(hWnd);
1580 break;
1582 case WM_SIZE:
1584 ME_MarkAllForWrapping(editor);
1585 ME_WrapMarkedParagraphs(editor);
1586 ME_UpdateScrollBar(editor);
1587 ME_Repaint(editor);
1588 return DefWindowProcW(hWnd, msg, wParam, lParam);
1590 case EM_GETOLEINTERFACE:
1592 LPVOID *ppvObj = (LPVOID*) lParam;
1593 FIXME("EM_GETOLEINTERFACE %p: stub\n", ppvObj);
1594 return CreateIRichEditOle(ppvObj);
1596 default:
1597 do_default:
1598 return DefWindowProcW(hWnd, msg, wParam, lParam);
1600 return 0L;
1603 /******************************************************************
1604 * RichEdit10ANSIWndProc (RICHED20.9)
1606 LRESULT WINAPI RichEdit10ANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1608 LRESULT result;
1610 /* FIXME: this is NOT the same as 2.0 version */
1611 result = RichEditANSIWndProc(hWnd, msg, wParam, lParam);
1612 if (msg == WM_NCCREATE)
1614 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongW(hWnd, 0);
1616 editor->bEmulateVersion10 = TRUE;
1617 editor->pBuffer->pLast->member.para.nCharOfs = 2;
1619 return result;
1622 void ME_SendOldNotify(ME_TextEditor *editor, int nCode)
1624 HWND hWnd = editor->hWnd;
1625 SendMessageA(GetParent(hWnd), WM_COMMAND, (nCode<<16)|GetWindowLongW(hWnd, GWLP_ID), (LPARAM)hWnd);
1628 int ME_CountParagraphsBetween(ME_TextEditor *editor, int from, int to)
1630 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
1631 int i = 0;
1633 while(item && item->member.para.next_para->member.para.nCharOfs <= from)
1634 item = item->member.para.next_para;
1635 if (!item)
1636 return 0;
1637 while(item && item->member.para.next_para->member.para.nCharOfs <= to) {
1638 item = item->member.para.next_para;
1639 i++;
1641 return i;
1645 int ME_GetTextW(ME_TextEditor *editor, WCHAR *buffer, int nStart, int nChars, int bCRLF)
1647 ME_DisplayItem *item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
1648 int nWritten = 0;
1650 if (!item) {
1651 *buffer = L'\0';
1652 return 0;
1654 assert(item);
1656 if (nStart)
1658 int nLen = ME_StrLen(item->member.run.strText) - nStart;
1659 if (nLen > nChars)
1660 nLen = nChars;
1661 CopyMemory(buffer, item->member.run.strText->szData + nStart, sizeof(WCHAR)*nLen);
1662 nChars -= nLen;
1663 nWritten += nLen;
1664 if (!nChars)
1665 return nWritten;
1666 buffer += nLen;
1667 nStart = 0;
1668 item = ME_FindItemFwd(item, diRun);
1671 while(nChars && item)
1673 int nLen = ME_StrLen(item->member.run.strText);
1674 if (nLen > nChars)
1675 nLen = nChars;
1677 if (item->member.run.nFlags & MERF_ENDPARA)
1679 if (bCRLF) {
1680 *buffer++ = '\r';
1681 nWritten++;
1683 *buffer = '\n';
1684 assert(nLen == 1);
1686 else
1687 CopyMemory(buffer, item->member.run.strText->szData, sizeof(WCHAR)*nLen);
1688 nChars -= nLen;
1689 nWritten += nLen;
1690 buffer += nLen;
1692 if (!nChars)
1694 *buffer = L'\0';
1695 return nWritten;
1697 item = ME_FindItemFwd(item, diRun);
1699 *buffer = L'\0';
1700 return nWritten;
1703 void ME_RegisterEditorClass(HINSTANCE hInstance)
1705 BOOL bResult;
1706 WNDCLASSW wcW;
1707 WNDCLASSA wcA;
1709 wcW.style = CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
1710 wcW.lpfnWndProc = RichEditANSIWndProc;
1711 wcW.cbClsExtra = 0;
1712 wcW.cbWndExtra = 4;
1713 wcW.hInstance = NULL; /* hInstance would register DLL-local class */
1714 wcW.hIcon = NULL;
1715 wcW.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
1716 wcW.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
1717 wcW.lpszMenuName = NULL;
1718 wcW.lpszClassName = wszClassName;
1719 bResult = RegisterClassW(&wcW);
1720 assert(bResult);
1721 wcW.lpszClassName = wszClassName50;
1722 bResult = RegisterClassW(&wcW);
1723 assert(bResult);
1725 wcA.style = CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
1726 wcA.lpfnWndProc = RichEditANSIWndProc;
1727 wcA.cbClsExtra = 0;
1728 wcA.cbWndExtra = 4;
1729 wcA.hInstance = NULL; /* hInstance would register DLL-local class */
1730 wcA.hIcon = NULL;
1731 wcA.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
1732 wcA.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
1733 wcA.lpszMenuName = NULL;
1734 wcA.lpszClassName = "RichEdit20A";
1735 bResult = RegisterClassA(&wcA);
1736 assert(bResult);
1737 wcA.lpszClassName = "RichEdit50A";
1738 bResult = RegisterClassA(&wcA);
1739 assert(bResult);
1741 /******************************************************************
1742 * CreateTextServices (RICHED20.4)
1744 * FIXME should be ITextHost instead of void*
1746 HRESULT WINAPI CreateTextServices(IUnknown *punkOuter, void *pITextHost,
1747 IUnknown **ppUnk)
1749 FIXME("stub\n");
1750 /* FIXME should support aggregation */
1751 if (punkOuter)
1752 return CLASS_E_NOAGGREGATION;
1754 return E_FAIL; /* E_NOTIMPL isn't allowed by MSDN */
1757 /******************************************************************
1758 * REExtendedRegisterClass (RICHED20.8)
1760 * FIXME undocumented
1762 void WINAPI REExtendedRegisterClass(void)
1764 FIXME("stub\n");