Added YUV routines needed for v4l driver, and in the future possibly
[wine/gsoc-2012-control.git] / dlls / riched20 / editor.c
blob075502853adb8cd9574e1bc54d42991db9a136d4
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
37 - EM_FINDTEXTEX
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
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
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 (can't fall back to text when the RTF isn't really RTF)
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 "ole2.h"
222 #include "richole.h"
223 #include "winreg.h"
224 #define NO_SHLWAPI_STREAM
225 #include "shlwapi.h"
227 #include "rtf.h"
229 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
231 int me_debug = 0;
232 HANDLE me_heap = NULL;
234 ME_TextBuffer *ME_MakeText() {
236 ME_TextBuffer *buf = ALLOC_OBJ(ME_TextBuffer);
238 ME_DisplayItem *p1 = ME_MakeDI(diTextStart);
239 ME_DisplayItem *p2 = ME_MakeDI(diTextEnd);
241 p1->prev = NULL;
242 p1->next = p2;
243 p2->prev = p1;
244 p2->next = NULL;
245 p1->member.para.next_para = p2;
246 p2->member.para.prev_para = p1;
247 p2->member.para.nCharOfs = 0;
249 buf->pFirst = p1;
250 buf->pLast = p2;
251 buf->pCharStyle = NULL;
253 return buf;
257 static LRESULT ME_StreamInText(ME_TextEditor *editor, DWORD dwFormat, ME_InStream *stream, ME_Style *style)
259 WCHAR wszText[STREAMIN_BUFFER_SIZE+1];
260 WCHAR *pText;
262 TRACE("%08lx %p\n", dwFormat, stream);
264 do {
265 long nWideChars = 0;
267 if (!stream->dwSize)
269 ME_StreamInFill(stream);
270 if (stream->editstream->dwError)
271 break;
272 if (!stream->dwSize)
273 break;
276 if (!(dwFormat & SF_UNICODE))
278 /* FIXME? this is doomed to fail on true MBCS like UTF-8, luckily they're unlikely to be used as CP_ACP */
279 nWideChars = MultiByteToWideChar(CP_ACP, 0, stream->buffer, stream->dwSize, wszText, STREAMIN_BUFFER_SIZE);
280 pText = wszText;
282 else
284 nWideChars = stream->dwSize >> 1;
285 pText = (WCHAR *)stream->buffer;
288 ME_InsertTextFromCursor(editor, 0, pText, nWideChars, style);
289 if (stream->dwSize < STREAMIN_BUFFER_SIZE)
290 break;
291 stream->dwSize = 0;
292 } while(1);
293 ME_CommitUndo(editor);
294 ME_Repaint(editor);
295 return 0;
298 void ME_RTFCharAttrHook(RTF_Info *info)
300 CHARFORMAT2W fmt;
301 fmt.cbSize = sizeof(fmt);
302 fmt.dwMask = 0;
304 switch(info->rtfMinor)
306 case rtfPlain:
307 /* FIXME add more flags once they're implemented */
308 fmt.dwMask = CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_COLOR | CFM_BACKCOLOR | CFM_SIZE | CFM_WEIGHT;
309 fmt.dwEffects = CFE_AUTOCOLOR | CFE_AUTOBACKCOLOR;
310 fmt.yHeight = 12*20; /* 12pt */
311 fmt.wWeight = 400;
312 break;
313 case rtfBold:
314 fmt.dwMask = CFM_BOLD;
315 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
316 break;
317 case rtfItalic:
318 fmt.dwMask = CFM_ITALIC;
319 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
320 break;
321 case rtfUnderline:
322 fmt.dwMask = CFM_UNDERLINE;
323 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
324 break;
325 case rtfStrikeThru:
326 fmt.dwMask = CFM_STRIKEOUT;
327 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
328 break;
329 case rtfBackColor:
330 fmt.dwMask = CFM_BACKCOLOR;
331 fmt.dwEffects = 0;
332 if (info->rtfParam == 0)
333 fmt.dwEffects = CFE_AUTOBACKCOLOR;
334 else if (info->rtfParam != rtfNoParam)
336 RTFColor *c = RTFGetColor(info, info->rtfParam);
337 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
339 break;
340 case rtfForeColor:
341 fmt.dwMask = CFM_COLOR;
342 fmt.dwEffects = 0;
343 if (info->rtfParam == 0)
344 fmt.dwEffects = CFE_AUTOCOLOR;
345 else if (info->rtfParam != rtfNoParam)
347 RTFColor *c = RTFGetColor(info, info->rtfParam);
348 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
350 break;
351 case rtfFontNum:
352 if (info->rtfParam != rtfNoParam)
354 RTFFont *f = RTFGetFont(info, info->rtfParam);
355 if (f)
357 MultiByteToWideChar(CP_ACP, 0, f->rtfFName, -1, fmt.szFaceName, sizeof(fmt.szFaceName)/sizeof(WCHAR));
358 fmt.szFaceName[sizeof(fmt.szFaceName)/sizeof(WCHAR)-1] = '\0';
359 fmt.bCharSet = f->rtfFCharSet;
360 fmt.dwMask = CFM_FACE | CFM_CHARSET;
363 break;
364 case rtfFontSize:
365 fmt.dwMask = CFM_SIZE;
366 if (info->rtfParam != rtfNoParam)
367 fmt.yHeight = info->rtfParam*10;
368 break;
370 if (fmt.dwMask) {
371 ME_Style *style2;
372 RTFFlushOutputBuffer(info);
373 /* FIXME too slow ? how come ? */
374 style2 = ME_ApplyStyle(info->style, &fmt);
375 ME_ReleaseStyle(info->style);
376 info->style = style2;
380 /* FIXME this function doesn't get any information about context of the RTF tag, which is very bad,
381 the same tags mean different things in different contexts */
382 void ME_RTFParAttrHook(RTF_Info *info)
384 PARAFORMAT2 fmt;
385 fmt.cbSize = sizeof(fmt);
386 fmt.dwMask = 0;
388 switch(info->rtfMinor)
390 case rtfParDef: /* I'm not 100% sure what does it do, but I guess it restores default paragraph attributes */
391 fmt.dwMask = PFM_ALIGNMENT | PFM_TABSTOPS | PFM_OFFSET | PFM_STARTINDENT;
392 fmt.wAlignment = PFA_LEFT;
393 fmt.cTabCount = 0;
394 fmt.dxOffset = fmt.dxStartIndent = 0;
395 break;
396 case rtfFirstIndent:
397 ME_GetSelectionParaFormat(info->editor, &fmt);
398 fmt.dwMask = PFM_STARTINDENT;
399 fmt.dxStartIndent = info->rtfParam + fmt.dxOffset;
400 break;
401 case rtfLeftIndent:
403 int first, left;
404 ME_GetSelectionParaFormat(info->editor, &fmt);
405 first = fmt.dxStartIndent;
406 left = info->rtfParam;
407 fmt.dwMask = PFM_STARTINDENT|PFM_OFFSET;
408 fmt.dxStartIndent = first + left;
409 fmt.dxOffset = -first;
410 break;
412 case rtfRightIndent:
413 fmt.dwMask = PFM_RIGHTINDENT;
414 fmt.dxRightIndent = info->rtfParam;
415 break;
416 case rtfQuadLeft:
417 case rtfQuadJust:
418 fmt.dwMask = PFM_ALIGNMENT;
419 fmt.wAlignment = PFA_LEFT;
420 break;
421 case rtfQuadRight:
422 fmt.dwMask = PFM_ALIGNMENT;
423 fmt.wAlignment = PFA_RIGHT;
424 break;
425 case rtfQuadCenter:
426 fmt.dwMask = PFM_ALIGNMENT;
427 fmt.wAlignment = PFA_CENTER;
428 break;
429 case rtfTabPos:
430 ME_GetSelectionParaFormat(info->editor, &fmt);
431 if (!(fmt.dwMask & PFM_TABSTOPS))
433 fmt.dwMask |= PFM_TABSTOPS;
434 fmt.cTabCount = 0;
436 if (fmt.cTabCount < MAX_TAB_STOPS)
437 fmt.rgxTabs[fmt.cTabCount++] = info->rtfParam;
438 break;
440 if (fmt.dwMask) {
441 RTFFlushOutputBuffer(info);
442 /* FIXME too slow ? how come ?*/
443 ME_SetSelectionParaFormat(info->editor, &fmt);
447 void ME_RTFReadHook(RTF_Info *info) {
448 switch(info->rtfClass)
450 case rtfGroup:
451 switch(info->rtfMajor)
453 case rtfBeginGroup:
454 if (info->stackTop < maxStack) {
455 memcpy(&info->stack[info->stackTop].fmt, &info->style->fmt, sizeof(CHARFORMAT2W));
456 info->stack[info->stackTop].codePage = info->codePage;
457 info->stack[info->stackTop].unicodeLength = info->unicodeLength;
459 info->stackTop++;
460 break;
461 case rtfEndGroup:
463 ME_Style *s;
464 RTFFlushOutputBuffer(info);
465 info->stackTop--;
466 /* FIXME too slow ? how come ? */
467 s = ME_ApplyStyle(info->style, &info->stack[info->stackTop].fmt);
468 ME_ReleaseStyle(info->style);
469 info->style = s;
470 info->codePage = info->stack[info->stackTop].codePage;
471 info->unicodeLength = info->stack[info->stackTop].unicodeLength;
472 break;
475 break;
476 case rtfControl:
477 switch(info->rtfMajor)
479 case rtfCharAttr:
480 ME_RTFCharAttrHook(info);
481 break;
482 case rtfParAttr:
483 ME_RTFParAttrHook(info);
484 break;
486 break;
490 void
491 ME_StreamInFill(ME_InStream *stream)
493 stream->editstream->dwError = stream->editstream->pfnCallback(stream->editstream->dwCookie,
494 stream->buffer,
495 sizeof(stream->buffer),
496 &stream->dwSize);
497 stream->dwUsed = 0;
500 static LRESULT ME_StreamIn(ME_TextEditor *editor, DWORD format, EDITSTREAM *stream)
502 RTF_Info parser;
503 ME_Style *style;
504 int from, to, to2, nUndoMode;
505 ME_UndoItem *pUI;
506 int nEventMask = editor->nEventMask;
507 ME_InStream inStream;
509 TRACE("stream==%p hWnd==%p format==0x%X\n", stream, editor->hWnd, (UINT)format);
510 editor->nEventMask = 0;
512 ME_GetSelection(editor, &from, &to);
513 if (format & SFF_SELECTION) {
514 style = ME_GetSelectionInsertStyle(editor);
516 ME_InternalDeleteText(editor, from, to-from);
518 else {
519 style = editor->pBuffer->pDefaultStyle;
520 ME_AddRefStyle(style);
521 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
522 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
523 from = to = 0;
524 ME_ClearTempStyle(editor);
525 /* FIXME restore default paragraph formatting ! */
528 nUndoMode = editor->nUndoMode;
529 editor->nUndoMode = umIgnore;
531 inStream.editstream = stream;
532 inStream.editstream->dwError = 0;
533 inStream.dwSize = 0;
534 inStream.dwUsed = 0;
536 if (format & SF_RTF)
538 /* Check if it's really RTF, and if it is not, use plain text */
539 ME_StreamInFill(&inStream);
540 if (!inStream.editstream->dwError)
542 if (strncmp(inStream.buffer, "{\\rtf1", 6) && strncmp(inStream.buffer, "{\\urtf", 6))
544 format &= ~SF_RTF;
545 format |= SF_TEXT;
550 if (!inStream.editstream->dwError)
552 if (format & SF_RTF) {
553 /* setup the RTF parser */
554 memset(&parser, 0, sizeof parser);
555 RTFSetEditStream(&parser, &inStream);
556 parser.rtfFormat = format&(SF_TEXT|SF_RTF);
557 parser.hwndEdit = editor->hWnd;
558 parser.editor = editor;
559 parser.style = style;
560 WriterInit(&parser);
561 RTFInit(&parser);
562 RTFSetReadHook(&parser, ME_RTFReadHook);
563 BeginFile(&parser);
565 /* do the parsing */
566 RTFRead(&parser);
567 RTFFlushOutputBuffer(&parser);
568 RTFDestroy(&parser);
570 style = parser.style;
572 else if (format & SF_TEXT)
573 ME_StreamInText(editor, format, &inStream, style);
574 else
575 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
576 ME_GetSelection(editor, &to, &to2);
577 /* put the cursor at the top */
578 if (!(format & SFF_SELECTION))
579 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
580 else
582 /* FIXME where to put cursor now ? */
586 editor->nUndoMode = nUndoMode;
587 pUI = ME_AddUndoItem(editor, diUndoDeleteRun, NULL);
588 TRACE("from %d to %d\n", from, to);
589 if (pUI && from < to)
591 pUI->nStart = from;
592 pUI->nLen = to-from;
594 ME_CommitUndo(editor);
595 ME_ReleaseStyle(style);
596 editor->nEventMask = nEventMask;
597 InvalidateRect(editor->hWnd, NULL, TRUE);
598 ME_UpdateRepaint(editor);
599 if (!(format & SFF_SELECTION)) {
600 ME_ClearTempStyle(editor);
602 ME_MoveCaret(editor);
603 ME_SendSelChange(editor);
605 return 0;
609 ME_DisplayItem *
610 ME_FindItemAtOffset(ME_TextEditor *editor, ME_DIType nItemType, int nOffset, int *nItemOffset)
612 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
614 while (item && item->member.para.next_para->member.para.nCharOfs <= nOffset)
615 item = ME_FindItemFwd(item, diParagraph);
617 if (!item)
618 return item;
620 nOffset -= item->member.para.nCharOfs;
621 if (nItemType == diParagraph) {
622 if (nItemOffset)
623 *nItemOffset = nOffset;
624 return item;
627 do {
628 item = ME_FindItemFwd(item, diRun);
629 } while (item && (item->member.run.nCharOfs + ME_StrLen(item->member.run.strText) <= nOffset));
630 if (item) {
631 nOffset -= item->member.run.nCharOfs;
632 if (nItemOffset)
633 *nItemOffset = nOffset;
635 return item;
639 ME_TextEditor *ME_MakeEditor(HWND hWnd) {
640 ME_TextEditor *ed = ALLOC_OBJ(ME_TextEditor);
641 HDC hDC;
642 int i;
643 ed->hWnd = hWnd;
644 ed->pBuffer = ME_MakeText();
645 hDC = GetDC(hWnd);
646 ME_MakeFirstParagraph(hDC, ed->pBuffer);
647 ReleaseDC(hWnd, hDC);
648 ed->bCaretShown = FALSE;
649 ed->nCursors = 3;
650 ed->pCursors = ALLOC_N_OBJ(ME_Cursor, ed->nCursors);
651 ed->pCursors[0].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
652 ed->pCursors[0].nOffset = 0;
653 ed->pCursors[1].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
654 ed->pCursors[1].nOffset = 0;
655 ed->nLastTotalLength = ed->nTotalLength = 0;
656 ed->nUDArrowX = -1;
657 ed->nSequence = 0;
658 ed->rgbBackColor = -1;
659 ed->bCaretAtEnd = FALSE;
660 ed->nEventMask = 0;
661 ed->nModifyStep = 0;
662 ed->pUndoStack = ed->pRedoStack = NULL;
663 ed->nUndoMode = umAddToUndo;
664 ed->nParagraphs = 1;
665 ed->nLastSelStart = ed->nLastSelEnd = 0;
666 ed->nScrollPosY = 0;
667 for (i=0; i<HFONT_CACHE_SIZE; i++)
669 ed->pFontCache[i].nRefs = 0;
670 ed->pFontCache[i].nAge = 0;
671 ed->pFontCache[i].hFont = NULL;
673 ME_CheckCharOffsets(ed);
674 return ed;
677 typedef struct tagME_GlobalDestStruct
679 HGLOBAL hData;
680 int nLength;
681 } ME_GlobalDestStruct;
683 static DWORD CALLBACK ME_AppendToHGLOBAL(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
685 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
686 int nMaxSize;
687 BYTE *pDest;
689 nMaxSize = GlobalSize(pData->hData);
690 if (pData->nLength+cb+1 >= cb)
692 /* round up to 2^17 */
693 int nNewSize = (((nMaxSize+cb+1)|0x1FFFF)+1) & 0xFFFE0000;
694 pData->hData = GlobalReAlloc(pData->hData, nNewSize, 0);
696 pDest = (BYTE *)GlobalLock(pData->hData);
697 memcpy(pDest + pData->nLength, lpBuff, cb);
698 pData->nLength += cb;
699 pDest[pData->nLength] = '\0';
700 GlobalUnlock(pData->hData);
701 *pcb = cb;
703 return 0;
706 static DWORD CALLBACK ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
708 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
709 int i;
710 WORD *pSrc, *pDest;
712 cb = cb >> 1;
713 pDest = (WORD *)lpBuff;
714 pSrc = (WORD *)GlobalLock(pData->hData);
715 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
716 pDest[i] = pSrc[pData->nLength+i];
718 pData->nLength += i;
719 *pcb = 2*i;
720 GlobalUnlock(pData->hData);
721 return 0;
724 static DWORD CALLBACK ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
726 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
727 int i;
728 BYTE *pSrc, *pDest;
730 pDest = lpBuff;
731 pSrc = (BYTE *)GlobalLock(pData->hData);
732 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
733 pDest[i] = pSrc[pData->nLength+i];
735 pData->nLength += i;
736 *pcb = i;
737 GlobalUnlock(pData->hData);
738 return 0;
741 void ME_DestroyEditor(ME_TextEditor *editor)
743 ME_DisplayItem *pFirst = editor->pBuffer->pFirst;
744 ME_DisplayItem *p = pFirst, *pNext = NULL;
745 int i;
747 ME_ClearTempStyle(editor);
748 ME_EmptyUndoStack(editor);
749 while(p) {
750 pNext = p->next;
751 ME_DestroyDisplayItem(p);
752 p = pNext;
754 ME_ReleaseStyle(editor->pBuffer->pDefaultStyle);
755 for (i=0; i<HFONT_CACHE_SIZE; i++)
757 if (editor->pFontCache[i].hFont)
758 DeleteObject(editor->pFontCache[i].hFont);
761 FREE_OBJ(editor);
764 static WCHAR wszClassName[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '2', '0', 'W', 0};
765 static WCHAR wszClassName50[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '5', '0', 'W', 0};
767 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
769 TRACE("\n");
770 switch (fdwReason)
772 case DLL_PROCESS_ATTACH:
773 DisableThreadLibraryCalls(hinstDLL);
774 me_heap = HeapCreate (0, 0x10000, 0);
775 ME_RegisterEditorClass(hinstDLL);
776 break;
778 case DLL_PROCESS_DETACH:
779 UnregisterClassW(wszClassName, 0);
780 UnregisterClassW(wszClassName50, 0);
781 UnregisterClassA("RichEdit20A", 0);
782 UnregisterClassA("RichEdit50A", 0);
783 HeapDestroy (me_heap);
784 me_heap = NULL;
785 break;
787 return TRUE;
791 #define UNSUPPORTED_MSG(e) \
792 case e: \
793 FIXME(#e ": stub\n"); \
794 return DefWindowProcW(hWnd, msg, wParam, lParam);
796 /******************************************************************
797 * RichEditANSIWndProc (RICHED20.10)
799 LRESULT WINAPI RichEditANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
800 HDC hDC;
801 PAINTSTRUCT ps;
802 SCROLLINFO si;
803 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongW(hWnd, 0);
804 TRACE("msg %d %08x %08lx\n", msg, wParam, lParam);
805 switch(msg) {
807 UNSUPPORTED_MSG(EM_AUTOURLDETECT)
808 UNSUPPORTED_MSG(EM_CHARFROMPOS)
809 UNSUPPORTED_MSG(EM_DISPLAYBAND)
810 UNSUPPORTED_MSG(EM_EXLIMITTEXT)
811 UNSUPPORTED_MSG(EM_EXLINEFROMCHAR)
812 UNSUPPORTED_MSG(EM_FINDTEXT)
813 UNSUPPORTED_MSG(EM_FINDTEXTEX)
814 UNSUPPORTED_MSG(EM_FINDWORDBREAK)
815 UNSUPPORTED_MSG(EM_FMTLINES)
816 UNSUPPORTED_MSG(EM_FORMATRANGE)
817 UNSUPPORTED_MSG(EM_GETAUTOURLDETECT)
818 UNSUPPORTED_MSG(EM_GETBIDIOPTIONS)
819 UNSUPPORTED_MSG(EM_GETEDITSTYLE)
820 UNSUPPORTED_MSG(EM_GETFIRSTVISIBLELINE)
821 UNSUPPORTED_MSG(EM_GETIMECOMPMODE)
822 /* UNSUPPORTED_MSG(EM_GETIMESTATUS) missing in Wine headers */
823 UNSUPPORTED_MSG(EM_GETLANGOPTIONS)
824 UNSUPPORTED_MSG(EM_GETLIMITTEXT)
825 UNSUPPORTED_MSG(EM_GETLINE)
826 UNSUPPORTED_MSG(EM_GETLINECOUNT)
827 /* UNSUPPORTED_MSG(EM_GETOLEINTERFACE) separate stub */
828 UNSUPPORTED_MSG(EM_GETOPTIONS)
829 UNSUPPORTED_MSG(EM_GETPASSWORDCHAR)
830 UNSUPPORTED_MSG(EM_GETRECT)
831 UNSUPPORTED_MSG(EM_GETREDONAME)
832 UNSUPPORTED_MSG(EM_GETSCROLLPOS)
833 UNSUPPORTED_MSG(EM_GETTEXTEX)
834 UNSUPPORTED_MSG(EM_GETTEXTLENGTHEX)
835 UNSUPPORTED_MSG(EM_GETTEXTMODE)
836 UNSUPPORTED_MSG(EM_GETTYPOGRAPHYOPTIONS)
837 UNSUPPORTED_MSG(EM_GETUNDONAME)
838 UNSUPPORTED_MSG(EM_GETWORDBREAKPROC)
839 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX)
840 UNSUPPORTED_MSG(EM_GETZOOM)
841 UNSUPPORTED_MSG(EM_HIDESELECTION)
842 UNSUPPORTED_MSG(EM_LIMITTEXT) /* also known as EM_SETLIMITTEXT */
843 UNSUPPORTED_MSG(EM_LINEFROMCHAR)
844 UNSUPPORTED_MSG(EM_LINEINDEX)
845 UNSUPPORTED_MSG(EM_LINELENGTH)
846 UNSUPPORTED_MSG(EM_PASTESPECIAL)
847 /* UNSUPPORTED_MSG(EM_POSFROMCHARS) missing in Wine headers */
848 UNSUPPORTED_MSG(EM_REQUESTRESIZE)
849 UNSUPPORTED_MSG(EM_SCROLL)
850 UNSUPPORTED_MSG(EM_SCROLLCARET)
851 UNSUPPORTED_MSG(EM_SELECTIONTYPE)
852 UNSUPPORTED_MSG(EM_SETBIDIOPTIONS)
853 UNSUPPORTED_MSG(EM_SETEDITSTYLE)
854 UNSUPPORTED_MSG(EM_SETFONTSIZE)
855 UNSUPPORTED_MSG(EM_SETLANGOPTIONS)
856 UNSUPPORTED_MSG(EM_SETOLECALLBACK)
857 UNSUPPORTED_MSG(EM_SETOPTIONS)
858 UNSUPPORTED_MSG(EM_SETPALETTE)
859 UNSUPPORTED_MSG(EM_SETPASSWORDCHAR)
860 UNSUPPORTED_MSG(EM_SETRECT)
861 UNSUPPORTED_MSG(EM_SETRECTNP)
862 UNSUPPORTED_MSG(EM_SETSCROLLPOS)
863 UNSUPPORTED_MSG(EM_SETTABSTOPS)
864 UNSUPPORTED_MSG(EM_SETTARGETDEVICE)
865 UNSUPPORTED_MSG(EM_SETTEXTEX)
866 UNSUPPORTED_MSG(EM_SETTEXTMODE)
867 UNSUPPORTED_MSG(EM_SETTYPOGRAPHYOPTIONS)
868 UNSUPPORTED_MSG(EM_SETUNDOLIMIT)
869 UNSUPPORTED_MSG(EM_SETWORDBREAKPROC)
870 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX)
871 UNSUPPORTED_MSG(EM_SHOWSCROLLBAR)
872 UNSUPPORTED_MSG(EM_SETZOOM)
873 UNSUPPORTED_MSG(WM_SETFONT)
874 UNSUPPORTED_MSG(WM_STYLECHANGING)
875 UNSUPPORTED_MSG(WM_STYLECHANGED)
876 /* UNSUPPORTED_MSG(WM_UNICHAR) FIXME missing in Wine headers */
878 /* Messages specific to Richedit controls */
880 case EM_STREAMIN:
881 return ME_StreamIn(editor, wParam, (EDITSTREAM*)lParam);
882 case EM_STREAMOUT:
883 return ME_StreamOut(editor, wParam, (EDITSTREAM *)lParam);
884 case WM_GETDLGCODE:
886 UINT code = DLGC_WANTCHARS|DLGC_WANTARROWS;
887 if (GetWindowLongW(hWnd, GWL_STYLE)&ES_WANTRETURN)
888 code |= 0; /* FIXME what can we do here ? ask for messages and censor them ? */
889 return code;
891 case WM_NCCREATE:
893 CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
894 editor = ME_MakeEditor(hWnd);
895 SetWindowLongW(hWnd, 0, (long)editor);
896 pcs = 0; /* ignore */
897 return TRUE;
899 case EM_EMPTYUNDOBUFFER:
900 ME_EmptyUndoStack(editor);
901 return 0;
902 case EM_GETSEL:
904 ME_GetSelection(editor, (int *)wParam, (int *)lParam);
905 if (!((wParam|lParam) & 0xFFFF0000))
906 return (lParam<<16)|wParam;
907 return -1;
909 case EM_EXGETSEL:
911 CHARRANGE *pRange = (CHARRANGE *)lParam;
912 ME_GetSelection(editor, (int *)&pRange->cpMin, (int *)&pRange->cpMax);
913 return 0;
915 case EM_CANUNDO:
916 return editor->pUndoStack != NULL;
917 case EM_CANREDO:
918 return editor->pRedoStack != NULL;
919 case EM_UNDO:
920 ME_Undo(editor);
921 return 0;
922 case EM_REDO:
923 ME_Redo(editor);
924 return 0;
925 case EM_SETSEL:
927 ME_SetSelection(editor, wParam, lParam);
928 ME_Repaint(editor);
929 ME_SendSelChange(editor);
930 return 0;
932 case EM_EXSETSEL:
934 CHARRANGE *pRange = (CHARRANGE *)lParam;
935 ME_SetSelection(editor, pRange->cpMin, pRange->cpMax);
936 /* FIXME optimize */
937 ME_Repaint(editor);
938 ME_SendSelChange(editor);
939 return 0;
941 case EM_SETBKGNDCOLOR:
943 LRESULT lColor = ME_GetBackColor(editor);
944 if (wParam)
945 editor->rgbBackColor = -1;
946 else
947 editor->rgbBackColor = lParam;
948 InvalidateRect(hWnd, NULL, TRUE);
949 UpdateWindow(hWnd);
950 return lColor;
952 case EM_GETMODIFY:
953 return editor->nModifyStep == 0 ? 0 : 1;
954 case EM_SETMODIFY:
956 if (wParam)
957 editor->nModifyStep = 0x80000000;
958 else
959 editor->nModifyStep = 0;
961 return 0;
963 case EM_SETREADONLY:
965 long nStyle = GetWindowLongW(hWnd, GWL_STYLE);
966 if (wParam)
967 nStyle |= ES_READONLY;
968 else
969 nStyle &= ~ES_READONLY;
970 SetWindowLongW(hWnd, GWL_STYLE, nStyle);
971 ME_Repaint(editor);
972 return 0;
974 case EM_SETEVENTMASK:
975 editor->nEventMask = lParam;
976 return 0;
977 case EM_GETEVENTMASK:
978 return editor->nEventMask;
979 case EM_SETCHARFORMAT:
981 CHARFORMAT2W buf, *p;
982 BOOL bRepaint = TRUE;
983 p = ME_ToCF2W(&buf, (CHARFORMAT2W *)lParam);
984 if (!wParam)
985 ME_SetDefaultCharFormat(editor, p);
986 else if (wParam == (SCF_WORD | SCF_SELECTION))
987 FIXME("word selection not supported\n");
988 else if (wParam == SCF_ALL)
989 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), p);
990 else {
991 int from, to;
992 ME_GetSelection(editor, &from, &to);
993 bRepaint = (from != to);
994 ME_SetSelectionCharFormat(editor, p);
996 ME_CommitUndo(editor);
997 if (bRepaint)
998 ME_UpdateRepaint(editor);
999 return 0;
1001 case EM_GETCHARFORMAT:
1003 CHARFORMAT2W tmp;
1004 tmp.cbSize = sizeof(tmp);
1005 if (!wParam)
1006 ME_GetDefaultCharFormat(editor, &tmp);
1007 else
1008 ME_GetSelectionCharFormat(editor, &tmp);
1009 ME_CopyToCFAny((CHARFORMAT2W *)lParam, &tmp);
1010 return 0;
1012 case EM_SETPARAFORMAT:
1013 ME_SetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
1014 ME_UpdateRepaint(editor);
1015 ME_CommitUndo(editor);
1016 return 0;
1017 case EM_GETPARAFORMAT:
1018 ME_GetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
1019 return 0;
1020 case EM_LINESCROLL:
1022 int nPos = editor->nScrollPosY, nEnd= editor->nTotalLength - editor->sizeWindow.cy;
1023 nPos += 8 * lParam; /* FIXME follow the original */
1024 if (nPos>=nEnd)
1025 nPos = nEnd;
1026 if (nPos<0)
1027 nPos = 0;
1028 if (nPos != editor->nScrollPosY) {
1029 ScrollWindow(hWnd, 0, editor->nScrollPosY-nPos, NULL, NULL);
1030 editor->nScrollPosY = nPos;
1031 SetScrollPos(hWnd, SB_VERT, nPos, TRUE);
1032 UpdateWindow(hWnd);
1034 return TRUE; /* Should return false if a single line richedit control */
1036 case WM_CLEAR:
1038 int from, to;
1039 ME_GetSelection(editor, &from, &to);
1040 ME_InternalDeleteText(editor, from, to-from);
1041 ME_CommitUndo(editor);
1042 ME_UpdateRepaint(editor);
1043 return 0;
1045 case EM_REPLACESEL:
1047 int from, to;
1048 ME_Style *style;
1049 LPWSTR wszText = ME_ToUnicode(hWnd, (void *)lParam);
1050 size_t len = lstrlenW(wszText);
1051 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText));
1053 ME_GetSelection(editor, &from, &to);
1054 style = ME_GetSelectionInsertStyle(editor);
1055 ME_InternalDeleteText(editor, from, to-from);
1056 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
1057 ME_ReleaseStyle(style);
1058 ME_EndToUnicode(hWnd, wszText);
1059 /* drop temporary style if line end */
1060 /* FIXME question: does abc\n mean: put abc, clear temp style, put \n? (would require a change) */
1061 if (len>0 && wszText[len-1] == '\n')
1062 ME_ClearTempStyle(editor);
1064 ME_CommitUndo(editor);
1065 if (!wParam)
1066 ME_EmptyUndoStack(editor);
1067 ME_UpdateRepaint(editor);
1068 return 0;
1070 case WM_SETTEXT:
1072 LPWSTR wszText = ME_ToUnicode(hWnd, (void *)lParam);
1073 TRACE("WM_SETTEXT - %s\n", (char *)(wszText)); /* debugstr_w() */
1074 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
1075 /* uses default style! */
1076 ME_InsertTextFromCursor(editor, 0, wszText, -1, editor->pBuffer->pDefaultStyle);
1077 ME_EndToUnicode(hWnd, wszText);
1078 ME_CommitUndo(editor);
1079 ME_EmptyUndoStack(editor);
1080 ME_UpdateRepaint(editor);
1081 return 0;
1083 case EM_CANPASTE:
1085 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
1086 if (IsClipboardFormatAvailable(nRTFFormat))
1087 return TRUE;
1088 if (IsClipboardFormatAvailable(CF_UNICODETEXT))
1089 return TRUE;
1090 return FALSE;
1092 case WM_PASTE:
1094 DWORD dwFormat = 0;
1095 EDITSTREAM es;
1096 ME_GlobalDestStruct gds;
1097 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
1098 UINT cf = 0;
1100 if (IsClipboardFormatAvailable(nRTFFormat))
1101 cf = nRTFFormat, dwFormat = SF_RTF;
1102 else if (IsClipboardFormatAvailable(CF_UNICODETEXT))
1103 cf = CF_UNICODETEXT, dwFormat = SF_TEXT|SF_UNICODE;
1104 else
1105 return 0;
1107 if (!OpenClipboard(hWnd))
1108 return 0;
1109 gds.hData = GetClipboardData(cf);
1110 gds.nLength = 0;
1111 es.dwCookie = (DWORD)&gds;
1112 es.pfnCallback = dwFormat == SF_RTF ? ME_ReadFromHGLOBALRTF : ME_ReadFromHGLOBALUnicode;
1113 SendMessageW(hWnd, EM_STREAMIN, dwFormat|SFF_SELECTION, (LPARAM)&es);
1115 CloseClipboard();
1116 return 0;
1118 case WM_CUT:
1119 case WM_COPY:
1121 int from, to, pars;
1122 WCHAR *data;
1123 HANDLE hData;
1124 EDITSTREAM es;
1125 ME_GlobalDestStruct gds;
1127 if (!OpenClipboard(hWnd))
1128 return 0;
1130 EmptyClipboard();
1131 ME_GetSelection(editor, &from, &to);
1132 pars = ME_CountParagraphsBetween(editor, from, to);
1133 hData = GlobalAlloc(GMEM_MOVEABLE, sizeof(WCHAR)*(to-from+pars+1));
1134 data = (WCHAR *)GlobalLock(hData);
1135 ME_GetTextW(editor, data, from, to-from, TRUE);
1136 GlobalUnlock(hData);
1138 gds.hData = GlobalAlloc(GMEM_MOVEABLE, 0);
1139 gds.nLength = 0;
1140 es.dwCookie = (DWORD)&gds;
1141 es.pfnCallback = ME_AppendToHGLOBAL;
1142 SendMessageW(hWnd, EM_STREAMOUT, SFF_SELECTION|SF_RTF, (LPARAM)&es);
1143 GlobalReAlloc(gds.hData, gds.nLength+1, 0);
1145 SetClipboardData(CF_UNICODETEXT, hData);
1146 SetClipboardData(RegisterClipboardFormatA("Rich Text Format"), gds.hData);
1148 CloseClipboard();
1149 if (msg == WM_CUT)
1151 ME_InternalDeleteText(editor, from, to-from);
1152 ME_CommitUndo(editor);
1153 ME_UpdateRepaint(editor);
1155 return 0;
1157 case WM_GETTEXTLENGTH:
1158 return ME_GetTextLength(editor);
1159 case WM_GETTEXT:
1161 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
1162 tr.chrg.cpMin = 0;
1163 tr.chrg.cpMax = wParam-1;
1164 tr.lpstrText = (WCHAR *)lParam;
1165 return RichEditANSIWndProc(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
1167 case EM_GETSELTEXT:
1169 int from, to;
1170 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
1171 ME_GetSelection(editor, &from, &to);
1172 tr.chrg.cpMin = from;
1173 tr.chrg.cpMax = to;
1174 tr.lpstrText = (WCHAR *)lParam;
1175 return RichEditANSIWndProc(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
1177 case EM_GETTEXTRANGE:
1179 TEXTRANGEW *rng = (TEXTRANGEW *)lParam;
1180 if (IsWindowUnicode(hWnd))
1181 return ME_GetTextW(editor, rng->lpstrText, rng->chrg.cpMin, rng->chrg.cpMax-rng->chrg.cpMin, FALSE);
1182 else
1184 int nLen = rng->chrg.cpMax-rng->chrg.cpMin;
1185 WCHAR *p = ALLOC_N_OBJ(WCHAR, nLen+1);
1186 int nChars = ME_GetTextW(editor, p, rng->chrg.cpMin, nLen, FALSE);
1187 /* FIXME this is a potential security hole (buffer overrun)
1188 if you know more about wchar->mbyte conversion please explain
1190 WideCharToMultiByte(CP_ACP, 0, p, nChars+1, (char *)rng->lpstrText, nLen+1, NULL, NULL);
1191 FREE_OBJ(p);
1192 return nChars;
1194 return ME_GetTextW(editor, rng->lpstrText, rng->chrg.cpMin, rng->chrg.cpMax-rng->chrg.cpMin, FALSE);
1196 case WM_CREATE:
1197 ME_CommitUndo(editor);
1198 ME_WrapMarkedParagraphs(editor);
1199 ME_MoveCaret(editor);
1200 return 0;
1201 case WM_DESTROY:
1202 ME_DestroyEditor(editor);
1203 SetWindowLongW(hWnd, 0, 0);
1204 return 0;
1205 case WM_LBUTTONDOWN:
1206 SetFocus(hWnd);
1207 ME_LButtonDown(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
1208 SetCapture(hWnd);
1209 break;
1210 case WM_MOUSEMOVE:
1211 if (GetCapture() == hWnd)
1212 ME_MouseMove(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
1213 break;
1214 case WM_LBUTTONUP:
1215 if (GetCapture() == hWnd)
1216 ReleaseCapture();
1217 break;
1218 case WM_PAINT:
1219 hDC = BeginPaint(hWnd, &ps);
1220 ME_PaintContent(editor, hDC, FALSE, &ps.rcPaint);
1221 EndPaint(hWnd, &ps);
1222 break;
1223 case WM_SETFOCUS:
1224 ME_ShowCaret(editor);
1225 ME_SendOldNotify(editor, EN_SETFOCUS);
1226 return 0;
1227 case WM_KILLFOCUS:
1228 ME_HideCaret(editor);
1229 ME_SendOldNotify(editor, EN_KILLFOCUS);
1230 return 0;
1231 case WM_ERASEBKGND:
1233 HDC hDC = (HDC)wParam;
1234 RECT rc;
1235 COLORREF rgbBG = ME_GetBackColor(editor);
1236 if (GetUpdateRect(hWnd,&rc,TRUE))
1238 HBRUSH hbr = CreateSolidBrush(rgbBG);
1239 FillRect(hDC, &rc, hbr);
1240 DeleteObject(hbr);
1242 return 1;
1244 case WM_COMMAND:
1245 TRACE("editor wnd command = %d\n", LOWORD(wParam));
1246 return 0;
1247 case WM_KEYDOWN:
1248 if (ME_ArrowKey(editor, LOWORD(wParam), GetKeyState(VK_CONTROL)<0)) {
1249 ME_CommitUndo(editor);
1250 ME_EnsureVisible(editor, editor->pCursors[0].pRun);
1251 HideCaret(hWnd);
1252 ME_MoveCaret(editor);
1253 ShowCaret(hWnd);
1254 return 0;
1256 if (GetKeyState(VK_CONTROL)<0)
1258 if (LOWORD(wParam)=='W')
1260 CHARFORMAT2W chf;
1261 char buf[2048];
1262 ME_GetSelectionCharFormat(editor, &chf);
1263 ME_DumpStyleToBuf(&chf, buf);
1264 MessageBoxA(NULL, buf, "Style dump", MB_OK);
1266 if (LOWORD(wParam)=='Q')
1268 ME_CheckCharOffsets(editor);
1271 goto do_default;
1272 case WM_CHAR:
1274 WCHAR wstr;
1275 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY) {
1276 MessageBeep(MB_ICONERROR);
1277 return 0; /* FIXME really 0 ? */
1279 wstr = LOWORD(wParam);
1280 if (((unsigned)wstr)>=' ' || wstr=='\r' || wstr=='\t') {
1281 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
1282 ME_Style *style = ME_GetInsertStyle(editor, 0);
1283 ME_SaveTempStyle(editor);
1284 ME_InsertTextFromCursor(editor, 0, &wstr, 1, style);
1285 ME_ReleaseStyle(style);
1286 ME_CommitUndo(editor);
1287 ME_UpdateRepaint(editor);
1289 return 0;
1291 case WM_VSCROLL:
1293 int nPos = editor->nScrollPosY;
1294 si.cbSize = sizeof(SCROLLINFO);
1295 si.fMask = SIF_PAGE|SIF_POS|SIF_RANGE|SIF_TRACKPOS;
1296 GetScrollInfo(hWnd, SB_VERT, &si);
1297 switch(LOWORD(wParam)) {
1298 case SB_LINEUP:
1299 nPos -= 24; /* FIXME follow the original */
1300 if (nPos<0) nPos = 0;
1301 break;
1302 case SB_LINEDOWN:
1304 int nEnd = editor->nTotalLength - editor->sizeWindow.cy;
1305 nPos += 24; /* FIXME follow the original */
1306 if (nPos>=nEnd) nPos = nEnd;
1307 break;
1309 case SB_PAGEUP:
1310 nPos -= editor->sizeWindow.cy;
1311 if (nPos<0) nPos = 0;
1312 break;
1313 case SB_PAGEDOWN:
1314 nPos += editor->sizeWindow.cy;
1315 if (nPos>=editor->nTotalLength) nPos = editor->nTotalLength-1;
1316 break;
1317 case SB_THUMBTRACK:
1318 case SB_THUMBPOSITION:
1319 nPos = si.nTrackPos;
1320 break;
1322 if (nPos != editor->nScrollPosY) {
1323 ScrollWindow(hWnd, 0, editor->nScrollPosY-nPos, NULL, NULL);
1324 editor->nScrollPosY = nPos;
1325 SetScrollPos(hWnd, SB_VERT, nPos, TRUE);
1326 UpdateWindow(hWnd);
1328 break;
1330 case WM_MOUSEWHEEL:
1332 int gcWheelDelta = 0, nPos = editor->nScrollPosY, nEnd = editor->nTotalLength - editor->sizeWindow.cy;
1333 UINT pulScrollLines;
1334 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
1335 gcWheelDelta -= GET_WHEEL_DELTA_WPARAM(wParam);
1336 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
1337 nPos += pulScrollLines * (gcWheelDelta / WHEEL_DELTA) * 8; /* FIXME follow the original */
1338 if (nPos>=nEnd)
1339 nPos = nEnd;
1340 if (nPos<0)
1341 nPos = 0;
1342 if (nPos != editor->nScrollPosY) {
1343 ScrollWindow(hWnd, 0, editor->nScrollPosY-nPos, NULL, NULL);
1344 editor->nScrollPosY = nPos;
1345 SetScrollPos(hWnd, SB_VERT, nPos, TRUE);
1346 UpdateWindow(hWnd);
1348 break;
1350 case WM_SIZE:
1352 ME_MarkAllForWrapping(editor);
1353 ME_WrapMarkedParagraphs(editor);
1354 ME_UpdateScrollBar(editor);
1355 ME_Repaint(editor);
1356 return DefWindowProcW(hWnd, msg, wParam, lParam);
1358 case EM_GETOLEINTERFACE:
1360 LPVOID *ppvObj = (LPVOID*) lParam;
1361 FIXME("EM_GETOLEINTERFACE %p: stub\n", ppvObj);
1362 return CreateIRichEditOle(ppvObj);
1364 default:
1365 do_default:
1366 return DefWindowProcW(hWnd, msg, wParam, lParam);
1368 return 0L;
1371 /******************************************************************
1372 * RichEdit10ANSIWndProc (RICHED20.9)
1374 LRESULT WINAPI RichEdit10ANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1376 /* FIXME: this is NOT the same as 2.0 version */
1377 return RichEditANSIWndProc(hWnd, msg, wParam, lParam);
1380 void ME_SendOldNotify(ME_TextEditor *editor, int nCode)
1382 HWND hWnd = editor->hWnd;
1383 SendMessageA(GetParent(hWnd), WM_COMMAND, (nCode<<16)|GetWindowLongW(hWnd, GWLP_ID), (LPARAM)hWnd);
1386 int ME_CountParagraphsBetween(ME_TextEditor *editor, int from, int to)
1388 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
1389 int i = 0;
1391 while(item && item->member.para.next_para->member.para.nCharOfs <= from)
1392 item = item->member.para.next_para;
1393 if (!item)
1394 return 0;
1395 while(item && item->member.para.next_para->member.para.nCharOfs <= to) {
1396 item = item->member.para.next_para;
1397 i++;
1399 return i;
1403 int ME_GetTextW(ME_TextEditor *editor, WCHAR *buffer, int nStart, int nChars, int bCRLF)
1405 ME_DisplayItem *item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
1406 int nWritten = 0;
1408 if (!item) {
1409 *buffer = L'\0';
1410 return 0;
1412 assert(item);
1414 if (nStart)
1416 int nLen = ME_StrLen(item->member.run.strText) - nStart;
1417 if (nLen > nChars)
1418 nLen = nChars;
1419 CopyMemory(buffer, item->member.run.strText->szData + nStart, sizeof(WCHAR)*nLen);
1420 nChars -= nLen;
1421 nWritten += nLen;
1422 if (!nChars)
1423 return nWritten;
1424 buffer += nLen;
1425 nStart = 0;
1426 item = ME_FindItemFwd(item, diRun);
1429 while(nChars && item)
1431 int nLen = ME_StrLen(item->member.run.strText);
1432 if (nLen > nChars)
1433 nLen = nChars;
1435 if (item->member.run.nFlags & MERF_ENDPARA)
1437 if (bCRLF) {
1438 *buffer++ = '\r';
1439 nWritten++;
1441 *buffer = '\n';
1442 assert(nLen == 1);
1444 else
1445 CopyMemory(buffer, item->member.run.strText->szData, sizeof(WCHAR)*nLen);
1446 nChars -= nLen;
1447 nWritten += nLen;
1448 buffer += nLen;
1450 if (!nChars)
1452 *buffer = L'\0';
1453 return nWritten;
1455 item = ME_FindItemFwd(item, diRun);
1457 *buffer = L'\0';
1458 return nWritten;
1461 void ME_RegisterEditorClass(HINSTANCE hInstance)
1463 BOOL bResult;
1464 WNDCLASSW wcW;
1465 WNDCLASSA wcA;
1467 wcW.style = CS_HREDRAW | CS_VREDRAW;
1468 wcW.lpfnWndProc = RichEditANSIWndProc;
1469 wcW.cbClsExtra = 0;
1470 wcW.cbWndExtra = 4;
1471 wcW.hInstance = NULL; /* hInstance would register DLL-local class */
1472 wcW.hIcon = NULL;
1473 wcW.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
1474 wcW.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
1475 wcW.lpszMenuName = NULL;
1476 wcW.lpszClassName = wszClassName;
1477 bResult = RegisterClassW(&wcW);
1478 assert(bResult);
1479 wcW.lpszClassName = wszClassName50;
1480 bResult = RegisterClassW(&wcW);
1481 assert(bResult);
1483 wcA.style = CS_HREDRAW | CS_VREDRAW;
1484 wcA.lpfnWndProc = RichEditANSIWndProc;
1485 wcA.cbClsExtra = 0;
1486 wcA.cbWndExtra = 4;
1487 wcA.hInstance = NULL; /* hInstance would register DLL-local class */
1488 wcA.hIcon = NULL;
1489 wcA.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
1490 wcA.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
1491 wcA.lpszMenuName = NULL;
1492 wcA.lpszClassName = "RichEdit20A";
1493 bResult = RegisterClassA(&wcA);
1494 assert(bResult);
1495 wcA.lpszClassName = "RichEdit50A";
1496 bResult = RegisterClassA(&wcA);
1497 assert(bResult);
1499 /******************************************************************
1500 * CreateTextServices (RICHED20.4)
1502 * FIXME should be ITextHost instead of void*
1504 HRESULT WINAPI CreateTextServices(IUnknown *punkOuter, void *pITextHost,
1505 IUnknown **ppUnk)
1507 FIXME("stub\n");
1508 /* FIXME should support aggregation */
1509 if (punkOuter)
1510 return CLASS_E_NOAGGREGATION;
1512 return E_FAIL; /* E_NOTIMPL isn't allowed by MSDN */
1515 /******************************************************************
1516 * REExtendedRegisterClass (RICHED20.8)
1518 * FIXME undocumented
1520 void WINAPI REExtendedRegisterClass(void)
1522 FIXME("stub\n");