Merge 14981:15268 from trunk
[reactos.git] / reactos / lib / riched20 / editor.c
1 /*
2 * RichEdit - functions dealing with editor object
3 *
4 * Copyright 2004 by Krzysztof Foltman
5 *
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.
10 *
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.
15 *
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
19 */
20
21 /*
22 API implementation status:
23
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_GETCHARFORMAT (partly done)
42 + EM_GETEVENTMASK
43 - EM_GETFIRSTVISIBLELINE
44 - EM_GETIMECOLOR 1.0asian
45 - EM_GETIMECOMPMODE 2.0
46 - EM_GETIMEOPTIONS 1.0asian
47 - EM_GETIMESTATUS
48 - EM_GETLANGOPTIONS 2.0
49 - EM_GETLIMITTEXT
50 - EM_GETLINE
51 - EM_GETLINECOUNT returns number of rows, not of paragraphs
52 + EM_GETMODIFY
53 - EM_GETOLEINTERFACE
54 - EM_GETOPTIONS
55 + EM_GETPARAFORMAT
56 - EM_GETPUNCTUATION 1.0asian
57 - EM_GETRECT
58 - EM_GETREDONAME 2.0
59 + EM_GETSEL
60 + EM_GETSELTEXT (ANSI&Unicode)
61 ! - EM_GETTHUMB
62 - EM_GETTEXTMODE 2.0
63 ? + EM_GETTEXTRANGE (ANSI&Unicode)
64 - EM_GETUNDONAME
65 - EM_GETWORDBREAKPROC
66 - EM_GETWORDBREAKPROCEX
67 - EM_GETWORDWRAPMODE 1.0asian
68 - EM_HIDESELECTION
69 - EM_LIMITTEXT
70 - EM_LINEFROMCHAR
71 - EM_LINEINDEX
72 - EM_LINELENGTH
73 - EM_LINESCROLL
74 - EM_PASTESPECIAL
75 - EM_POSFROMCHARS
76 + EM_REDO 2.0
77 - EM_REQUESTRESIZE
78 + EM_REPLACESEL (proper style?) ANSI&Unicode
79 - EM_SCROLL
80 - EM_SCROLLCARET
81 - EM_SELECTIONTYPE
82 + EM_SETBKGNDCOLOR
83 - EM_SETCHARFORMAT (partly done, no ANSI)
84 + EM_SETEVENTMASK (few notifications supported)
85 - EM_SETIMECOLOR 1.0asian
86 - EM_SETIMEOPTIONS 1.0asian
87 - EM_SETLANGOPTIONS 2.0
88 - EM_SETLIMITTEXT
89 + EM_SETMODIFY (not sure if implementation is correct)
90 - EM_SETOLECALLBACK
91 - EM_SETOPTIONS
92 + EM_SETPARAFORMAT
93 - EM_SETPUNCTUATION 1.0asian
94 + EM_SETREADONLY no beep on modification attempt
95 - EM_SETRECT
96 - EM_SETRECTNP (EM_SETRECT without repainting) - not supported in RICHEDIT
97 + EM_SETSEL
98 - EM_SETTARGETDEVICE
99 - EM_SETTEXTMODE 2.0
100 - EM_SETUNDOLIMIT 2.0
101 - EM_SETWORDBREAKPROC
102 - EM_SETWORDBREAKPROCEX
103 - EM_SETWORDWRAPMODE 1.0asian
104 - EM_STOPGROUPTYPING 2.0
105 + EM_STREAMIN (can't fall back to text when the RTF isn't really RTF)
106 + EM_STREAMOUT
107 + EM_UNDO
108 + WM_CHAR
109 + WM_CLEAR
110 + WM_COPY
111 + WM_CUT
112 + WM_GETDLGCODE (the current implementation is incomplete)
113 + WM_GETTEXT (ANSI&Unicode)
114 + WM_GETTEXTLENGTH (ANSI version sucks)
115 + WM_PASTE
116 - WM_SETFONT
117 + WM_SETTEXT (resets undo stack !) (proper style?) ANSI&Unicode
118 - WM_STYLECHANGING
119 - WM_STYLECHANGED (things like read-only flag)
120 - WM_UNICHAR
121
122 Notifications
123
124 * EN_CHANGE (sent from the wrong place)
125 - EN_CORRECTTEXT
126 - EN_DROPFILES
127 - EN_ERRSPACE
128 - EN_HSCROLL
129 - EN_IMECHANGE
130 + EN_KILLFOCUS
131 - EN_LINK
132 - EN_MAXTEXT
133 - EN_MSGFILTER
134 - EN_OLEOPFAILED
135 - EN_PROTECTED
136 - EN_REQUESTRESIZE
137 - EN_SAVECLIPBOARD
138 + EN_SELCHANGE
139 + EN_SETFOCUS
140 - EN_STOPNOUNDO
141 * EN_UPDATE (sent from the wrong place)
142 - EN_VSCROLL
143
144 Styles
145
146 - ES_AUTOHSCROLL
147 - ES_AUTOVSCROLL
148 - ES_CENTER
149 - ES_DISABLENOSCROLL (scrollbar is always visible)
150 - ES_EX_NOCALLOLEINIT
151 - ES_LEFT
152 - ES_MULTILINE (currently single line controls aren't supported)
153 - ES_NOIME
154 - ES_READONLY (I'm not sure if beeping is the proper behaviour)
155 - ES_RIGHT
156 - ES_SAVESEL
157 - ES_SELFIME
158 - ES_SUNKEN
159 - ES_VERTICAL
160 - ES_WANTRETURN (don't know how to do WM_GETDLGCODE part)
161 - WS_SETFONT
162 - WS_HSCROLL
163 - WS_VSCROLL
164 */
165
166 /*
167 * RICHED20 TODO (incomplete):
168 *
169 * - messages/styles/notifications listed above
170 * - Undo coalescing
171 * - add remaining CHARFORMAT/PARAFORMAT fields
172 * - right/center align should strip spaces from the beginning
173 * - more advanced navigation (Ctrl-arrows)
174 * - tabs
175 * - pictures/OLE objects (not just smiling faces that lack API support ;-) )
176 * - COM interface (looks like a major pain in the TODO list)
177 * - calculate heights of pictures (half-done)
178 * - horizontal scrolling (not even started)
179 * - hysteresis during wrapping (related to scrollbars appearing/disappearing)
180 * - find/replace
181 * - how to implement EM_FORMATRANGE and EM_DISPLAYBAND ? (Mission Impossible)
182 * - italic caret with italic fonts
183 * - IME
184 * - most notifications aren't sent at all (the most important ones are)
185 * - when should EN_SELCHANGE be sent after text change ? (before/after EN_UPDATE?)
186 * - WM_SETTEXT may use wrong style (but I'm 80% sure it's OK)
187 * - EM_GETCHARFORMAT with SCF_SELECTION may not behave 100% like in original (but very close)
188 *
189 * Bugs that are probably fixed, but not so easy to verify:
190 * - EN_UPDATE/EN_CHANGE are handled very incorrectly (should be OK now)
191 * - undo for ME_JoinParagraphs doesn't store paragraph format ? (it does)
192 * - check/fix artificial EOL logic (bCursorAtEnd, hardly logical)
193 * - caret shouldn't be displayed when selection isn't empty
194 * - check refcounting in style management functions (looks perfect now, but no bugs is suspicious)
195 * - undo for setting default format (done, might be buggy)
196 * - styles might be not released properly (looks like they work like charm, but who knows?
197 *
198 */
199
200 #include "editor.h"
201 #include "ole2.h"
202 #include "richole.h"
203 #include "winreg.h"
204 #define NO_SHLWAPI_STREAM
205 #include "shlwapi.h"
206
207 #include "rtf.h"
208
209 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
210
211 int me_debug = 0;
212 HANDLE me_heap = NULL;
213
214 ME_TextBuffer *ME_MakeText() {
215
216 ME_TextBuffer *buf = ALLOC_OBJ(ME_TextBuffer);
217
218 ME_DisplayItem *p1 = ME_MakeDI(diTextStart);
219 ME_DisplayItem *p2 = ME_MakeDI(diTextEnd);
220
221 p1->prev = NULL;
222 p1->next = p2;
223 p2->prev = p1;
224 p2->next = NULL;
225 p1->member.para.next_para = p2;
226 p2->member.para.prev_para = p1;
227 p2->member.para.nCharOfs = 0;
228
229 buf->pFirst = p1;
230 buf->pLast = p2;
231 buf->pCharStyle = NULL;
232
233 return buf;
234 }
235
236
237 static LRESULT ME_StreamInText(ME_TextEditor *editor, DWORD dwFormat, ME_InStream *stream, ME_Style *style)
238 {
239 WCHAR wszText[STREAMIN_BUFFER_SIZE+1];
240 WCHAR *pText;
241
242 TRACE("%08lx %p\n", dwFormat, stream);
243
244 do {
245 long nWideChars = 0;
246
247 if (!stream->dwSize)
248 {
249 ME_StreamInFill(stream);
250 if (stream->editstream->dwError)
251 break;
252 if (!stream->dwSize)
253 break;
254 }
255
256 if (!(dwFormat & SF_UNICODE))
257 {
258 /* FIXME? this is doomed to fail on true MBCS like UTF-8, luckily they're unlikely to be used as CP_ACP */
259 nWideChars = MultiByteToWideChar(CP_ACP, 0, stream->buffer, stream->dwSize, wszText, STREAMIN_BUFFER_SIZE);
260 pText = wszText;
261 }
262 else
263 {
264 nWideChars = stream->dwSize >> 1;
265 pText = (WCHAR *)stream->buffer;
266 }
267
268 ME_InsertTextFromCursor(editor, 0, pText, nWideChars, style);
269 if (stream->dwSize < STREAMIN_BUFFER_SIZE)
270 break;
271 } while(1);
272 ME_CommitUndo(editor);
273 ME_Repaint(editor);
274 return 0;
275 }
276
277 void ME_RTFCharAttrHook(RTF_Info *info)
278 {
279 CHARFORMAT2W fmt;
280 fmt.cbSize = sizeof(fmt);
281 fmt.dwMask = 0;
282
283 switch(info->rtfMinor)
284 {
285 case rtfPlain:
286 /* FIXME add more flags once they're implemented */
287 fmt.dwMask = CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_COLOR | CFM_BACKCOLOR | CFM_SIZE | CFM_WEIGHT;
288 fmt.dwEffects = CFE_AUTOCOLOR | CFE_AUTOBACKCOLOR;
289 fmt.yHeight = 12*20; /* 12pt */
290 fmt.wWeight = 400;
291 break;
292 case rtfBold:
293 fmt.dwMask = CFM_BOLD;
294 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
295 break;
296 case rtfItalic:
297 fmt.dwMask = CFM_ITALIC;
298 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
299 break;
300 case rtfUnderline:
301 fmt.dwMask = CFM_UNDERLINE;
302 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
303 break;
304 case rtfStrikeThru:
305 fmt.dwMask = CFM_STRIKEOUT;
306 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
307 break;
308 case rtfBackColor:
309 fmt.dwMask = CFM_BACKCOLOR;
310 fmt.dwEffects = 0;
311 if (info->rtfParam == 0)
312 fmt.dwEffects = CFE_AUTOBACKCOLOR;
313 else if (info->rtfParam != rtfNoParam)
314 {
315 RTFColor *c = RTFGetColor(info, info->rtfParam);
316 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
317 }
318 break;
319 case rtfForeColor:
320 fmt.dwMask = CFM_COLOR;
321 fmt.dwEffects = 0;
322 if (info->rtfParam == 0)
323 fmt.dwEffects = CFE_AUTOCOLOR;
324 else if (info->rtfParam != rtfNoParam)
325 {
326 RTFColor *c = RTFGetColor(info, info->rtfParam);
327 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
328 }
329 break;
330 case rtfFontNum:
331 if (info->rtfParam != rtfNoParam)
332 {
333 RTFFont *f = RTFGetFont(info, info->rtfParam);
334 if (f)
335 {
336 MultiByteToWideChar(CP_ACP, 0, f->rtfFName, -1, fmt.szFaceName, sizeof(fmt.szFaceName)/sizeof(WCHAR));
337 fmt.szFaceName[sizeof(fmt.szFaceName)/sizeof(WCHAR)-1] = '\0';
338 fmt.bCharSet = f->rtfFCharSet;
339 fmt.dwMask = CFM_FACE | CFM_CHARSET;
340 }
341 }
342 break;
343 case rtfFontSize:
344 fmt.dwMask = CFM_SIZE;
345 if (info->rtfParam != rtfNoParam)
346 fmt.yHeight = info->rtfParam*10;
347 break;
348 }
349 if (fmt.dwMask) {
350 ME_Style *style2;
351 RTFFlushOutputBuffer(info);
352 /* FIXME too slow ? how come ? */
353 style2 = ME_ApplyStyle(info->style, &fmt);
354 ME_ReleaseStyle(info->style);
355 info->style = style2;
356 }
357 }
358
359 /* FIXME this function doesn't get any information about context of the RTF tag, which is very bad,
360 the same tags mean different things in different contexts */
361 void ME_RTFParAttrHook(RTF_Info *info)
362 {
363 PARAFORMAT2 fmt;
364 fmt.cbSize = sizeof(fmt);
365 fmt.dwMask = 0;
366
367 switch(info->rtfMinor)
368 {
369 case rtfParDef: /* I'm not 100% sure what does it do, but I guess it restores default paragraph attributes */
370 fmt.dwMask = PFM_ALIGNMENT | PFM_TABSTOPS | PFM_OFFSET | PFM_STARTINDENT;
371 fmt.wAlignment = PFA_LEFT;
372 fmt.cTabCount = 0;
373 fmt.dxOffset = fmt.dxStartIndent = 0;
374 break;
375 case rtfFirstIndent:
376 ME_GetSelectionParaFormat(info->editor, &fmt);
377 fmt.dwMask = PFM_STARTINDENT;
378 fmt.dxStartIndent = info->rtfParam + fmt.dxOffset;
379 break;
380 case rtfLeftIndent:
381 {
382 int first, left;
383 ME_GetSelectionParaFormat(info->editor, &fmt);
384 first = fmt.dxStartIndent;
385 left = info->rtfParam;
386 fmt.dwMask = PFM_STARTINDENT|PFM_OFFSET;
387 fmt.dxStartIndent = first + left;
388 fmt.dxOffset = -first;
389 break;
390 }
391 case rtfRightIndent:
392 fmt.dwMask = PFM_RIGHTINDENT;
393 fmt.dxRightIndent = info->rtfParam;
394 break;
395 case rtfQuadLeft:
396 case rtfQuadJust:
397 fmt.dwMask = PFM_ALIGNMENT;
398 fmt.wAlignment = PFA_LEFT;
399 break;
400 case rtfQuadRight:
401 fmt.dwMask = PFM_ALIGNMENT;
402 fmt.wAlignment = PFA_RIGHT;
403 break;
404 case rtfQuadCenter:
405 fmt.dwMask = PFM_ALIGNMENT;
406 fmt.wAlignment = PFA_CENTER;
407 break;
408 case rtfTabPos:
409 ME_GetSelectionParaFormat(info->editor, &fmt);
410 if (!(fmt.dwMask & PFM_TABSTOPS))
411 {
412 fmt.dwMask |= PFM_TABSTOPS;
413 fmt.cTabCount = 0;
414 }
415 if (fmt.cTabCount < MAX_TAB_STOPS)
416 fmt.rgxTabs[fmt.cTabCount++] = info->rtfParam;
417 break;
418 }
419 if (fmt.dwMask) {
420 RTFFlushOutputBuffer(info);
421 /* FIXME too slow ? how come ?*/
422 ME_SetSelectionParaFormat(info->editor, &fmt);
423 }
424 }
425
426 void ME_RTFReadHook(RTF_Info *info) {
427 switch(info->rtfClass)
428 {
429 case rtfGroup:
430 switch(info->rtfMajor)
431 {
432 case rtfBeginGroup:
433 if (info->stackTop < maxStack) {
434 memcpy(&info->stack[info->stackTop].fmt, &info->style->fmt, sizeof(CHARFORMAT2W));
435 info->stack[info->stackTop].codePage = info->codePage;
436 info->stack[info->stackTop].unicodeLength = info->unicodeLength;
437 }
438 info->stackTop++;
439 break;
440 case rtfEndGroup:
441 {
442 ME_Style *s;
443 RTFFlushOutputBuffer(info);
444 info->stackTop--;
445 /* FIXME too slow ? how come ? */
446 s = ME_ApplyStyle(info->style, &info->stack[info->stackTop].fmt);
447 ME_ReleaseStyle(info->style);
448 info->style = s;
449 info->codePage = info->stack[info->stackTop].codePage;
450 info->unicodeLength = info->stack[info->stackTop].unicodeLength;
451 break;
452 }
453 }
454 break;
455 case rtfControl:
456 switch(info->rtfMajor)
457 {
458 case rtfCharAttr:
459 ME_RTFCharAttrHook(info);
460 break;
461 case rtfParAttr:
462 ME_RTFParAttrHook(info);
463 break;
464 }
465 break;
466 }
467 }
468
469 void
470 ME_StreamInFill(ME_InStream *stream)
471 {
472 stream->editstream->dwError = stream->editstream->pfnCallback(stream->editstream->dwCookie,
473 stream->buffer,
474 sizeof(stream->buffer),
475 &stream->dwSize);
476 stream->dwUsed = 0;
477 }
478
479 static LRESULT ME_StreamIn(ME_TextEditor *editor, DWORD format, EDITSTREAM *stream)
480 {
481 RTF_Info parser;
482 ME_Style *style;
483 int from, to, to2, nUndoMode;
484 ME_UndoItem *pUI;
485 int nEventMask = editor->nEventMask;
486 ME_InStream inStream;
487
488 TRACE("%p %p\n", stream, editor->hWnd);
489 editor->nEventMask = 0;
490
491 ME_GetSelection(editor, &from, &to);
492 if (format & SFF_SELECTION) {
493 style = ME_GetSelectionInsertStyle(editor);
494
495 ME_InternalDeleteText(editor, from, to-from);
496 }
497 else {
498 style = editor->pBuffer->pDefaultStyle;
499 ME_AddRefStyle(style);
500 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
501 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
502 from = to = 0;
503 ME_ClearTempStyle(editor);
504 /* FIXME restore default paragraph formatting ! */
505 }
506
507 nUndoMode = editor->nUndoMode;
508 editor->nUndoMode = umIgnore;
509
510 inStream.editstream = stream;
511 inStream.editstream->dwError = 0;
512 inStream.dwSize = 0;
513 inStream.dwUsed = 0;
514
515 if (format & SF_RTF)
516 {
517 /* Check if it's really RTF, and if it is not, use plain text */
518 ME_StreamInFill(&inStream);
519 if (!inStream.editstream->dwError)
520 {
521 if (strncmp(inStream.buffer, "{\\rtf1", 6) && strncmp(inStream.buffer, "{\\urtf", 6))
522 {
523 format &= ~SF_RTF;
524 format |= SF_TEXT;
525 }
526 }
527 }
528
529 if (!inStream.editstream->dwError)
530 {
531 if (format & SF_RTF) {
532 /* setup the RTF parser */
533 memset(&parser, 0, sizeof parser);
534 RTFSetEditStream(&parser, &inStream);
535 parser.rtfFormat = format&(SF_TEXT|SF_RTF);
536 parser.hwndEdit = editor->hWnd;
537 parser.editor = editor;
538 parser.style = style;
539 WriterInit(&parser);
540 RTFInit(&parser);
541 RTFSetReadHook(&parser, ME_RTFReadHook);
542 BeginFile(&parser);
543
544 /* do the parsing */
545 RTFRead(&parser);
546 RTFFlushOutputBuffer(&parser);
547 RTFDestroy(&parser);
548
549 style = parser.style;
550 }
551 else if (format & SF_TEXT)
552 ME_StreamInText(editor, format, &inStream, style);
553 else
554 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
555 ME_GetSelection(editor, &to, &to2);
556 /* put the cursor at the top */
557 if (!(format & SFF_SELECTION))
558 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
559 else
560 {
561 /* FIXME where to put cursor now ? */
562 }
563 }
564
565 editor->nUndoMode = nUndoMode;
566 pUI = ME_AddUndoItem(editor, diUndoDeleteRun, NULL);
567 TRACE("from %d to %d\n", from, to);
568 if (pUI && from < to)
569 {
570 pUI->nStart = from;
571 pUI->nLen = to-from;
572 }
573 ME_CommitUndo(editor);
574 ME_ReleaseStyle(style);
575 editor->nEventMask = nEventMask;
576 InvalidateRect(editor->hWnd, NULL, TRUE);
577 ME_UpdateRepaint(editor);
578 if (!(format & SFF_SELECTION)) {
579 ME_ClearTempStyle(editor);
580 }
581 ME_MoveCaret(editor);
582 ME_SendSelChange(editor);
583
584 return 0;
585 }
586
587
588 ME_DisplayItem *
589 ME_FindItemAtOffset(ME_TextEditor *editor, ME_DIType nItemType, int nOffset, int *nItemOffset)
590 {
591 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
592
593 while (item && item->member.para.next_para->member.para.nCharOfs <= nOffset)
594 item = ME_FindItemFwd(item, diParagraph);
595
596 if (!item)
597 return item;
598
599 nOffset -= item->member.para.nCharOfs;
600 if (nItemType == diParagraph) {
601 if (nItemOffset)
602 *nItemOffset = nOffset;
603 return item;
604 }
605
606 do {
607 item = ME_FindItemFwd(item, diRun);
608 } while (item && (item->member.run.nCharOfs + ME_StrLen(item->member.run.strText) <= nOffset));
609 if (item) {
610 nOffset -= item->member.run.nCharOfs;
611 if (nItemOffset)
612 *nItemOffset = nOffset;
613 }
614 return item;
615 }
616
617
618 ME_TextEditor *ME_MakeEditor(HWND hWnd) {
619 ME_TextEditor *ed = ALLOC_OBJ(ME_TextEditor);
620 HDC hDC;
621 int i;
622 ed->hWnd = hWnd;
623 ed->pBuffer = ME_MakeText();
624 hDC = GetDC(hWnd);
625 ME_MakeFirstParagraph(hDC, ed->pBuffer);
626 ReleaseDC(hWnd, hDC);
627 ed->bCaretShown = FALSE;
628 ed->nCursors = 3;
629 ed->pCursors = ALLOC_N_OBJ(ME_Cursor, ed->nCursors);
630 ed->pCursors[0].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
631 ed->pCursors[0].nOffset = 0;
632 ed->pCursors[1].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
633 ed->pCursors[1].nOffset = 0;
634 ed->nLastTotalLength = ed->nTotalLength = 0;
635 ed->nUDArrowX = -1;
636 ed->nSequence = 0;
637 ed->rgbBackColor = -1;
638 ed->bCaretAtEnd = FALSE;
639 ed->nEventMask = 0;
640 ed->nModifyStep = 0;
641 ed->pUndoStack = ed->pRedoStack = NULL;
642 ed->nUndoMode = umAddToUndo;
643 ed->nParagraphs = 1;
644 ed->nLastSelStart = ed->nLastSelEnd = 0;
645 ed->nScrollPosY = 0;
646 for (i=0; i<HFONT_CACHE_SIZE; i++)
647 {
648 ed->pFontCache[i].nRefs = 0;
649 ed->pFontCache[i].nAge = 0;
650 ed->pFontCache[i].hFont = NULL;
651 }
652 ME_CheckCharOffsets(ed);
653 return ed;
654 }
655
656 typedef struct tagME_GlobalDestStruct
657 {
658 HGLOBAL hData;
659 int nLength;
660 } ME_GlobalDestStruct;
661
662 static DWORD CALLBACK ME_AppendToHGLOBAL(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
663 {
664 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
665 int nMaxSize;
666 BYTE *pDest;
667
668 nMaxSize = GlobalSize(pData->hData);
669 if (pData->nLength+cb+1 >= cb)
670 {
671 /* round up to 2^17 */
672 int nNewSize = (((nMaxSize+cb+1)|0x1FFFF)+1) & 0xFFFE0000;
673 pData->hData = GlobalReAlloc(pData->hData, nNewSize, 0);
674 }
675 pDest = (BYTE *)GlobalLock(pData->hData);
676 memcpy(pDest + pData->nLength, lpBuff, cb);
677 pData->nLength += cb;
678 pDest[pData->nLength] = '\0';
679 GlobalUnlock(pData->hData);
680 *pcb = cb;
681
682 return 0;
683 }
684
685 static DWORD CALLBACK ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
686 {
687 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
688 int i;
689 WORD *pSrc, *pDest;
690
691 cb = cb >> 1;
692 pDest = (WORD *)lpBuff;
693 pSrc = (WORD *)GlobalLock(pData->hData);
694 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
695 pDest[i] = pSrc[pData->nLength+i];
696 }
697 pData->nLength += i;
698 *pcb = 2*i;
699 GlobalUnlock(pData->hData);
700 return 0;
701 }
702
703 static DWORD CALLBACK ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
704 {
705 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
706 int i;
707 BYTE *pSrc, *pDest;
708
709 pDest = lpBuff;
710 pSrc = (BYTE *)GlobalLock(pData->hData);
711 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
712 pDest[i] = pSrc[pData->nLength+i];
713 }
714 pData->nLength += i;
715 *pcb = i;
716 GlobalUnlock(pData->hData);
717 return 0;
718 }
719
720 void ME_DestroyEditor(ME_TextEditor *editor)
721 {
722 ME_DisplayItem *pFirst = editor->pBuffer->pFirst;
723 ME_DisplayItem *p = pFirst, *pNext = NULL;
724 int i;
725
726 ME_ClearTempStyle(editor);
727 ME_EmptyUndoStack(editor);
728 while(p) {
729 pNext = p->next;
730 ME_DestroyDisplayItem(p);
731 p = pNext;
732 }
733 ME_ReleaseStyle(editor->pBuffer->pDefaultStyle);
734 for (i=0; i<HFONT_CACHE_SIZE; i++)
735 {
736 if (editor->pFontCache[i].hFont)
737 DeleteObject(editor->pFontCache[i].hFont);
738 }
739
740 FREE_OBJ(editor);
741 }
742
743 #define UNSUPPORTED_MSG(e) \
744 case e: \
745 FIXME(#e ": stub\n"); \
746 return DefWindowProcW(hWnd, msg, wParam, lParam);
747
748 /******************************************************************
749 * RichEditANSIWndProc (RICHED20.10)
750 */
751 LRESULT WINAPI RichEditANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
752 HDC hDC;
753 PAINTSTRUCT ps;
754 SCROLLINFO si;
755 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongW(hWnd, 0);
756 TRACE("msg %d %08x %08lx\n", msg, wParam, lParam);
757 switch(msg) {
758
759 UNSUPPORTED_MSG(EM_AUTOURLDETECT)
760 UNSUPPORTED_MSG(EM_CHARFROMPOS)
761 UNSUPPORTED_MSG(EM_DISPLAYBAND)
762 UNSUPPORTED_MSG(EM_EXLIMITTEXT)
763 UNSUPPORTED_MSG(EM_EXLINEFROMCHAR)
764 UNSUPPORTED_MSG(EM_FINDTEXT)
765 UNSUPPORTED_MSG(EM_FINDTEXTEX)
766 UNSUPPORTED_MSG(EM_FINDWORDBREAK)
767 UNSUPPORTED_MSG(EM_FMTLINES)
768 UNSUPPORTED_MSG(EM_FORMATRANGE)
769 UNSUPPORTED_MSG(EM_GETFIRSTVISIBLELINE)
770 UNSUPPORTED_MSG(EM_GETIMECOMPMODE)
771 /* UNSUPPORTED_MSG(EM_GETIMESTATUS) missing in Wine headers */
772 UNSUPPORTED_MSG(EM_GETLANGOPTIONS)
773 UNSUPPORTED_MSG(EM_GETLIMITTEXT)
774 UNSUPPORTED_MSG(EM_GETLINE)
775 UNSUPPORTED_MSG(EM_GETLINECOUNT)
776 /* UNSUPPORTED_MSG(EM_GETOLEINTERFACE) separate stub */
777 UNSUPPORTED_MSG(EM_GETOPTIONS)
778 UNSUPPORTED_MSG(EM_GETRECT)
779 UNSUPPORTED_MSG(EM_GETREDONAME)
780 UNSUPPORTED_MSG(EM_GETTEXTMODE)
781 UNSUPPORTED_MSG(EM_GETUNDONAME)
782 UNSUPPORTED_MSG(EM_GETWORDBREAKPROC)
783 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX)
784 UNSUPPORTED_MSG(EM_HIDESELECTION)
785 UNSUPPORTED_MSG(EM_LIMITTEXT) /* also known as EM_SETLIMITTEXT */
786 UNSUPPORTED_MSG(EM_LINEFROMCHAR)
787 UNSUPPORTED_MSG(EM_LINEINDEX)
788 UNSUPPORTED_MSG(EM_LINELENGTH)
789 UNSUPPORTED_MSG(EM_LINESCROLL)
790 UNSUPPORTED_MSG(EM_PASTESPECIAL)
791 /* UNSUPPORTED_MSG(EM_POSFROMCHARS) missing in Wine headers */
792 UNSUPPORTED_MSG(EM_REQUESTRESIZE)
793 UNSUPPORTED_MSG(EM_SCROLL)
794 UNSUPPORTED_MSG(EM_SCROLLCARET)
795 UNSUPPORTED_MSG(EM_SELECTIONTYPE)
796 UNSUPPORTED_MSG(EM_SETLANGOPTIONS)
797 UNSUPPORTED_MSG(EM_SETOLECALLBACK)
798 UNSUPPORTED_MSG(EM_SETOPTIONS)
799 UNSUPPORTED_MSG(EM_SETRECT)
800 UNSUPPORTED_MSG(EM_SETRECTNP)
801 UNSUPPORTED_MSG(EM_SETTARGETDEVICE)
802 UNSUPPORTED_MSG(EM_SETTEXTMODE)
803 UNSUPPORTED_MSG(EM_SETUNDOLIMIT)
804 UNSUPPORTED_MSG(EM_SETWORDBREAKPROC)
805 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX)
806 UNSUPPORTED_MSG(WM_SETFONT)
807 UNSUPPORTED_MSG(WM_STYLECHANGING)
808 UNSUPPORTED_MSG(WM_STYLECHANGED)
809 /* UNSUPPORTED_MSG(WM_UNICHAR) FIXME missing in Wine headers */
810
811 /* Messages specific to Richedit controls */
812
813 case EM_STREAMIN:
814 return ME_StreamIn(editor, wParam, (EDITSTREAM*)lParam);
815 case EM_STREAMOUT:
816 return ME_StreamOut(editor, wParam, (EDITSTREAM *)lParam);
817 case WM_GETDLGCODE:
818 {
819 UINT code = DLGC_WANTCHARS|DLGC_WANTARROWS;
820 if (GetWindowLongW(hWnd, GWL_STYLE)&ES_WANTRETURN)
821 code |= 0; /* FIXME what can we do here ? ask for messages and censor them ? */
822 return code;
823 }
824 case WM_NCCREATE:
825 {
826 CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
827 editor = ME_MakeEditor(hWnd);
828 SetWindowLongW(hWnd, 0, (long)editor);
829 pcs = 0; /* ignore */
830 return TRUE;
831 }
832 case EM_EMPTYUNDOBUFFER:
833 ME_EmptyUndoStack(editor);
834 return 0;
835 case EM_GETSEL:
836 {
837 ME_GetSelection(editor, (int *)wParam, (int *)lParam);
838 if (!((wParam|lParam) & 0xFFFF0000))
839 return (lParam<<16)|wParam;
840 return -1;
841 }
842 case EM_EXGETSEL:
843 {
844 CHARRANGE *pRange = (CHARRANGE *)lParam;
845 ME_GetSelection(editor, (int *)&pRange->cpMin, (int *)&pRange->cpMax);
846 return 0;
847 }
848 case EM_CANUNDO:
849 return editor->pUndoStack != NULL;
850 case EM_CANREDO:
851 return editor->pRedoStack != NULL;
852 case EM_UNDO:
853 ME_Undo(editor);
854 return 0;
855 case EM_REDO:
856 ME_Redo(editor);
857 return 0;
858 case EM_SETSEL:
859 {
860 ME_SetSelection(editor, wParam, lParam);
861 ME_Repaint(editor);
862 ME_SendSelChange(editor);
863 return 0;
864 }
865 case EM_EXSETSEL:
866 {
867 CHARRANGE *pRange = (CHARRANGE *)lParam;
868 ME_SetSelection(editor, pRange->cpMin, pRange->cpMax);
869 /* FIXME optimize */
870 ME_Repaint(editor);
871 ME_SendSelChange(editor);
872 return 0;
873 }
874 case EM_SETBKGNDCOLOR:
875 {
876 LRESULT lColor = ME_GetBackColor(editor);
877 if (wParam)
878 editor->rgbBackColor = -1;
879 else
880 editor->rgbBackColor = lParam;
881 InvalidateRect(hWnd, NULL, TRUE);
882 UpdateWindow(hWnd);
883 return lColor;
884 }
885 case EM_GETMODIFY:
886 return editor->nModifyStep == 0 ? 0 : 1;
887 case EM_SETMODIFY:
888 {
889 if (wParam)
890 editor->nModifyStep = 0x80000000;
891 else
892 editor->nModifyStep = 0;
893
894 return 0;
895 }
896 case EM_SETREADONLY:
897 {
898 long nStyle = GetWindowLongW(hWnd, GWL_STYLE);
899 if (wParam)
900 nStyle |= ES_READONLY;
901 else
902 nStyle &= ~ES_READONLY;
903 SetWindowLongW(hWnd, GWL_STYLE, nStyle);
904 ME_Repaint(editor);
905 return 0;
906 }
907 case EM_SETEVENTMASK:
908 editor->nEventMask = lParam;
909 return 0;
910 case EM_GETEVENTMASK:
911 return editor->nEventMask;
912 case EM_SETCHARFORMAT:
913 {
914 CHARFORMAT2W buf, *p;
915 BOOL bRepaint = TRUE;
916 p = ME_ToCF2W(&buf, (CHARFORMAT2W *)lParam);
917 if (!wParam)
918 ME_SetDefaultCharFormat(editor, p);
919 else if (wParam == (SCF_WORD | SCF_SELECTION))
920 FIXME("word selection not supported\n");
921 else if (wParam == SCF_ALL)
922 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), p);
923 else {
924 int from, to;
925 ME_GetSelection(editor, &from, &to);
926 bRepaint = (from != to);
927 ME_SetSelectionCharFormat(editor, p);
928 }
929 ME_CommitUndo(editor);
930 if (bRepaint)
931 ME_UpdateRepaint(editor);
932 return 0;
933 }
934 case EM_GETCHARFORMAT:
935 {
936 CHARFORMAT2W tmp;
937 tmp.cbSize = sizeof(tmp);
938 if (!wParam)
939 ME_GetDefaultCharFormat(editor, &tmp);
940 else
941 ME_GetSelectionCharFormat(editor, &tmp);
942 ME_CopyToCFAny((CHARFORMAT2W *)lParam, &tmp);
943 return 0;
944 }
945 case EM_SETPARAFORMAT:
946 ME_SetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
947 ME_UpdateRepaint(editor);
948 ME_CommitUndo(editor);
949 return 0;
950 case EM_GETPARAFORMAT:
951 ME_GetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
952 return 0;
953 case WM_CLEAR:
954 {
955 int from, to;
956 ME_GetSelection(editor, &from, &to);
957 ME_InternalDeleteText(editor, from, to-from);
958 ME_CommitUndo(editor);
959 ME_UpdateRepaint(editor);
960 return 0;
961 }
962 case EM_REPLACESEL:
963 {
964 int from, to;
965 ME_Style *style;
966 LPWSTR wszText = ME_ToUnicode(hWnd, (void *)lParam);
967 size_t len = lstrlenW(wszText);
968 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText));
969
970 ME_GetSelection(editor, &from, &to);
971 style = ME_GetSelectionInsertStyle(editor);
972 ME_InternalDeleteText(editor, from, to-from);
973 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
974 ME_ReleaseStyle(style);
975 ME_EndToUnicode(hWnd, wszText);
976 /* drop temporary style if line end */
977 /* FIXME question: does abc\n mean: put abc, clear temp style, put \n? (would require a change) */
978 if (len>0 && wszText[len-1] == '\n')
979 ME_ClearTempStyle(editor);
980
981 ME_CommitUndo(editor);
982 if (!wParam)
983 ME_EmptyUndoStack(editor);
984 ME_UpdateRepaint(editor);
985 return 0;
986 }
987 case WM_SETTEXT:
988 {
989 LPWSTR wszText = ME_ToUnicode(hWnd, (void *)lParam);
990 TRACE("WM_SETTEXT - %s\n", (char *)(wszText)); /* debugstr_w() */
991 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
992 /* uses default style! */
993 ME_InsertTextFromCursor(editor, 0, wszText, -1, editor->pBuffer->pDefaultStyle);
994 ME_EndToUnicode(hWnd, wszText);
995 ME_CommitUndo(editor);
996 ME_EmptyUndoStack(editor);
997 ME_UpdateRepaint(editor);
998 return 0;
999 }
1000 case EM_CANPASTE:
1001 {
1002 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
1003 if (IsClipboardFormatAvailable(nRTFFormat))
1004 return TRUE;
1005 if (IsClipboardFormatAvailable(CF_UNICODETEXT))
1006 return TRUE;
1007 return FALSE;
1008 }
1009 case WM_PASTE:
1010 {
1011 DWORD dwFormat = 0;
1012 EDITSTREAM es;
1013 ME_GlobalDestStruct gds;
1014 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
1015 UINT cf = 0;
1016
1017 if (IsClipboardFormatAvailable(nRTFFormat))
1018 cf = nRTFFormat, dwFormat = SF_RTF;
1019 else if (IsClipboardFormatAvailable(CF_UNICODETEXT))
1020 cf = CF_UNICODETEXT, dwFormat = SF_TEXT|SF_UNICODE;
1021 else
1022 return 0;
1023
1024 if (!OpenClipboard(hWnd))
1025 return 0;
1026 gds.hData = GetClipboardData(cf);
1027 gds.nLength = 0;
1028 es.dwCookie = (DWORD)&gds;
1029 es.pfnCallback = dwFormat == SF_RTF ? ME_ReadFromHGLOBALRTF : ME_ReadFromHGLOBALUnicode;
1030 SendMessageW(hWnd, EM_STREAMIN, dwFormat|SFF_SELECTION, (LPARAM)&es);
1031
1032 CloseClipboard();
1033 return 0;
1034 }
1035 case WM_CUT:
1036 case WM_COPY:
1037 {
1038 int from, to, pars;
1039 WCHAR *data;
1040 HANDLE hData;
1041 EDITSTREAM es;
1042 ME_GlobalDestStruct gds;
1043
1044 if (!OpenClipboard(hWnd))
1045 return 0;
1046
1047 EmptyClipboard();
1048 ME_GetSelection(editor, &from, &to);
1049 pars = ME_CountParagraphsBetween(editor, from, to);
1050 hData = GlobalAlloc(GMEM_MOVEABLE, sizeof(WCHAR)*(to-from+pars+1));
1051 data = (WCHAR *)GlobalLock(hData);
1052 ME_GetTextW(editor, data, from, to-from, TRUE);
1053 GlobalUnlock(hData);
1054
1055 gds.hData = GlobalAlloc(GMEM_MOVEABLE, 0);
1056 gds.nLength = 0;
1057 es.dwCookie = (DWORD)&gds;
1058 es.pfnCallback = ME_AppendToHGLOBAL;
1059 SendMessageW(hWnd, EM_STREAMOUT, SFF_SELECTION|SF_RTF, (LPARAM)&es);
1060 GlobalReAlloc(gds.hData, gds.nLength+1, 0);
1061
1062 SetClipboardData(CF_UNICODETEXT, hData);
1063 SetClipboardData(RegisterClipboardFormatA("Rich Text Format"), gds.hData);
1064
1065 CloseClipboard();
1066 if (msg == WM_CUT)
1067 {
1068 ME_InternalDeleteText(editor, from, to-from);
1069 ME_CommitUndo(editor);
1070 ME_UpdateRepaint(editor);
1071 }
1072 return 0;
1073 }
1074 case WM_GETTEXTLENGTH:
1075 return ME_GetTextLength(editor);
1076 case WM_GETTEXT:
1077 {
1078 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
1079 tr.chrg.cpMin = 0;
1080 tr.chrg.cpMax = wParam-1;
1081 tr.lpstrText = (WCHAR *)lParam;
1082 return RichEditANSIWndProc(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
1083 }
1084 case EM_GETSELTEXT:
1085 {
1086 int from, to;
1087 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
1088 ME_GetSelection(editor, &from, &to);
1089 tr.chrg.cpMin = from;
1090 tr.chrg.cpMax = to;
1091 tr.lpstrText = (WCHAR *)lParam;
1092 return RichEditANSIWndProc(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
1093 }
1094 case EM_GETTEXTRANGE:
1095 {
1096 TEXTRANGEW *rng = (TEXTRANGEW *)lParam;
1097 if (IsWindowUnicode(hWnd))
1098 return ME_GetTextW(editor, rng->lpstrText, rng->chrg.cpMin, rng->chrg.cpMax-rng->chrg.cpMin, FALSE);
1099 else
1100 {
1101 int nLen = rng->chrg.cpMax-rng->chrg.cpMin;
1102 WCHAR *p = ALLOC_N_OBJ(WCHAR, nLen+1);
1103 int nChars = ME_GetTextW(editor, p, rng->chrg.cpMin, nLen, FALSE);
1104 /* FIXME this is a potential security hole (buffer overrun)
1105 if you know more about wchar->mbyte conversion please explain
1106 */
1107 WideCharToMultiByte(CP_ACP, 0, p, nChars+1, (char *)rng->lpstrText, nLen+1, NULL, NULL);
1108 FREE_OBJ(p);
1109 return nChars;
1110 }
1111 return ME_GetTextW(editor, rng->lpstrText, rng->chrg.cpMin, rng->chrg.cpMax-rng->chrg.cpMin, FALSE);
1112 }
1113 case WM_CREATE:
1114 ME_CommitUndo(editor);
1115 ME_WrapMarkedParagraphs(editor);
1116 ME_MoveCaret(editor);
1117 return 0;
1118 case WM_DESTROY:
1119 ME_DestroyEditor(editor);
1120 SetWindowLongW(hWnd, 0, 0);
1121 return 0;
1122 case WM_LBUTTONDOWN:
1123 SetFocus(hWnd);
1124 ME_LButtonDown(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
1125 SetCapture(hWnd);
1126 break;
1127 case WM_MOUSEMOVE:
1128 if (GetCapture() == hWnd)
1129 ME_MouseMove(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
1130 break;
1131 case WM_LBUTTONUP:
1132 if (GetCapture() == hWnd)
1133 ReleaseCapture();
1134 break;
1135 case WM_PAINT:
1136 hDC = BeginPaint(hWnd, &ps);
1137 ME_PaintContent(editor, hDC, FALSE, &ps.rcPaint);
1138 EndPaint(hWnd, &ps);
1139 break;
1140 case WM_SETFOCUS:
1141 ME_ShowCaret(editor);
1142 ME_SendOldNotify(editor, EN_SETFOCUS);
1143 return 0;
1144 case WM_KILLFOCUS:
1145 ME_HideCaret(editor);
1146 ME_SendOldNotify(editor, EN_KILLFOCUS);
1147 return 0;
1148 case WM_ERASEBKGND:
1149 {
1150 HDC hDC = (HDC)wParam;
1151 RECT rc;
1152 COLORREF rgbBG = ME_GetBackColor(editor);
1153 if (GetUpdateRect(hWnd,&rc,TRUE))
1154 {
1155 HBRUSH hbr = CreateSolidBrush(rgbBG);
1156 FillRect(hDC, &rc, hbr);
1157 DeleteObject(hbr);
1158 }
1159 return 1;
1160 }
1161 case WM_COMMAND:
1162 TRACE("editor wnd command = %d\n", LOWORD(wParam));
1163 return 0;
1164 case WM_KEYDOWN:
1165 if (ME_ArrowKey(editor, LOWORD(wParam), GetKeyState(VK_CONTROL)<0)) {
1166 ME_CommitUndo(editor);
1167 ME_EnsureVisible(editor, editor->pCursors[0].pRun);
1168 HideCaret(hWnd);
1169 ME_MoveCaret(editor);
1170 ShowCaret(hWnd);
1171 return 0;
1172 }
1173 if (GetKeyState(VK_CONTROL)<0)
1174 {
1175 if (LOWORD(wParam)=='W')
1176 {
1177 CHARFORMAT2W chf;
1178 char buf[2048];
1179 ME_GetSelectionCharFormat(editor, &chf);
1180 ME_DumpStyleToBuf(&chf, buf);
1181 MessageBoxA(NULL, buf, "Style dump", MB_OK);
1182 }
1183 if (LOWORD(wParam)=='Q')
1184 {
1185 ME_CheckCharOffsets(editor);
1186 }
1187 }
1188 goto do_default;
1189 case WM_CHAR:
1190 {
1191 WCHAR wstr;
1192 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY) {
1193 MessageBeep(MB_ICONERROR);
1194 return 0; /* FIXME really 0 ? */
1195 }
1196 wstr = LOWORD(wParam);
1197 if (((unsigned)wstr)>=' ' || wstr=='\r' || wstr=='\t') {
1198 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
1199 ME_Style *style = ME_GetInsertStyle(editor, 0);
1200 ME_SaveTempStyle(editor);
1201 ME_InsertTextFromCursor(editor, 0, &wstr, 1, style);
1202 ME_ReleaseStyle(style);
1203 ME_CommitUndo(editor);
1204 ME_UpdateRepaint(editor);
1205 }
1206 return 0;
1207 }
1208 case WM_VSCROLL:
1209 {
1210 int nPos = editor->nScrollPosY;
1211 si.cbSize = sizeof(SCROLLINFO);
1212 si.fMask = SIF_PAGE|SIF_POS|SIF_RANGE|SIF_TRACKPOS;
1213 GetScrollInfo(hWnd, SB_VERT, &si);
1214 switch(LOWORD(wParam)) {
1215 case SB_LINEUP:
1216 nPos -= 24; /* FIXME follow the original */
1217 if (nPos<0) nPos = 0;
1218 break;
1219 case SB_LINEDOWN:
1220 {
1221 int nEnd = editor->nTotalLength - editor->sizeWindow.cy;
1222 nPos += 24; /* FIXME follow the original */
1223 if (nPos>=nEnd) nPos = nEnd;
1224 break;
1225 }
1226 case SB_PAGEUP:
1227 nPos -= editor->sizeWindow.cy;
1228 if (nPos<0) nPos = 0;
1229 break;
1230 case SB_PAGEDOWN:
1231 nPos += editor->sizeWindow.cy;
1232 if (nPos>=editor->nTotalLength) nPos = editor->nTotalLength-1;
1233 break;
1234 case SB_THUMBTRACK:
1235 case SB_THUMBPOSITION:
1236 nPos = si.nTrackPos;
1237 break;
1238 }
1239 if (nPos != editor->nScrollPosY) {
1240 ScrollWindow(hWnd, 0, editor->nScrollPosY-nPos, NULL, NULL);
1241 editor->nScrollPosY = nPos;
1242 SetScrollPos(hWnd, SB_VERT, nPos, TRUE);
1243 UpdateWindow(hWnd);
1244 }
1245 break;
1246 }
1247 case WM_MOUSEWHEEL:
1248 {
1249 int gcWheelDelta = 0, nPos = editor->nScrollPosY;
1250 UINT pulScrollLines;
1251
1252 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
1253 gcWheelDelta -= GET_WHEEL_DELTA_WPARAM(wParam);
1254 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
1255 nPos += pulScrollLines * (gcWheelDelta / WHEEL_DELTA) * 8;
1256 if(nPos>=editor->nTotalLength)
1257 nPos = editor->nTotalLength - 1;
1258 if (nPos<0)
1259 nPos = 0;
1260 if (nPos != editor->nScrollPosY) {
1261 ScrollWindow(hWnd, 0, editor->nScrollPosY-nPos, NULL, NULL);
1262 editor->nScrollPosY = nPos;
1263 SetScrollPos(hWnd, SB_VERT, nPos, TRUE);
1264 UpdateWindow(hWnd);
1265 }
1266 break;
1267 }
1268 case WM_SIZE:
1269 {
1270 ME_MarkAllForWrapping(editor);
1271 ME_WrapMarkedParagraphs(editor);
1272 ME_UpdateScrollBar(editor);
1273 ME_Repaint(editor);
1274 return DefWindowProcW(hWnd, msg, wParam, lParam);
1275 }
1276 case EM_GETOLEINTERFACE:
1277 {
1278 LPVOID *ppvObj = (LPVOID*) lParam;
1279 FIXME("EM_GETOLEINTERFACE %p: stub\n", ppvObj);
1280 return CreateIRichEditOle(ppvObj);
1281 }
1282 default:
1283 do_default:
1284 return DefWindowProcW(hWnd, msg, wParam, lParam);
1285 }
1286 return 0L;
1287 }
1288
1289 /******************************************************************
1290 * RichEdit10ANSIWndProc (RICHED20.9)
1291 */
1292 LRESULT WINAPI RichEdit10ANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1293 {
1294 /* FIXME: this is NOT the same as 2.0 version */
1295 return RichEditANSIWndProc(hWnd, msg, wParam, lParam);
1296 }
1297
1298 void ME_SendOldNotify(ME_TextEditor *editor, int nCode)
1299 {
1300 HWND hWnd = editor->hWnd;
1301 SendMessageA(GetParent(hWnd), WM_COMMAND, (nCode<<16)|GetWindowLongW(hWnd, GWLP_ID), (LPARAM)hWnd);
1302 }
1303
1304 int ME_CountParagraphsBetween(ME_TextEditor *editor, int from, int to)
1305 {
1306 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
1307 int i = 0;
1308
1309 while(item && item->member.para.next_para->member.para.nCharOfs <= from)
1310 item = item->member.para.next_para;
1311 if (!item)
1312 return 0;
1313 while(item && item->member.para.next_para->member.para.nCharOfs <= to) {
1314 item = item->member.para.next_para;
1315 i++;
1316 }
1317 return i;
1318 }
1319
1320
1321 int ME_GetTextW(ME_TextEditor *editor, WCHAR *buffer, int nStart, int nChars, int bCRLF)
1322 {
1323 ME_DisplayItem *item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
1324 int nWritten = 0;
1325
1326 if (!item) {
1327 *buffer = L'\0';
1328 return 0;
1329 }
1330 assert(item);
1331
1332 if (nStart)
1333 {
1334 int nLen = ME_StrLen(item->member.run.strText) - nStart;
1335 if (nLen > nChars)
1336 nLen = nChars;
1337 CopyMemory(buffer, item->member.run.strText->szData + nStart, sizeof(WCHAR)*nLen);
1338 nChars -= nLen;
1339 nWritten += nLen;
1340 if (!nChars)
1341 return nWritten;
1342 buffer += nLen;
1343 nStart = 0;
1344 item = ME_FindItemFwd(item, diRun);
1345 }
1346
1347 while(nChars && item)
1348 {
1349 int nLen = ME_StrLen(item->member.run.strText);
1350 if (nLen > nChars)
1351 nLen = nChars;
1352
1353 if (item->member.run.nFlags & MERF_ENDPARA)
1354 {
1355 if (bCRLF) {
1356 *buffer++ = '\r';
1357 nWritten++;
1358 }
1359 *buffer = '\n';
1360 assert(nLen == 1);
1361 }
1362 else
1363 CopyMemory(buffer, item->member.run.strText->szData, sizeof(WCHAR)*nLen);
1364 nChars -= nLen;
1365 nWritten += nLen;
1366 buffer += nLen;
1367
1368 if (!nChars)
1369 {
1370 *buffer = L'\0';
1371 return nWritten;
1372 }
1373 item = ME_FindItemFwd(item, diRun);
1374 }
1375 *buffer = L'\0';
1376 return nWritten;
1377 }
1378
1379 static WCHAR wszClassName[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '2', '0', 'W', 0};
1380 static WCHAR wszClassName50[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '5', '0', 'W', 0};
1381
1382 void ME_RegisterEditorClass(HINSTANCE hInstance)
1383 {
1384 BOOL bResult;
1385 WNDCLASSW wcW;
1386 WNDCLASSA wcA;
1387
1388 wcW.style = CS_HREDRAW | CS_VREDRAW;
1389 wcW.lpfnWndProc = RichEditANSIWndProc;
1390 wcW.cbClsExtra = 0;
1391 wcW.cbWndExtra = 4;
1392 wcW.hInstance = NULL; /* hInstance would register DLL-local class */
1393 wcW.hIcon = NULL;
1394 wcW.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
1395 wcW.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
1396 wcW.lpszMenuName = NULL;
1397 wcW.lpszClassName = wszClassName;
1398 bResult = RegisterClassW(&wcW);
1399 assert(bResult);
1400 wcW.lpszClassName = wszClassName50;
1401 bResult = RegisterClassW(&wcW);
1402 assert(bResult);
1403
1404 wcA.style = CS_HREDRAW | CS_VREDRAW;
1405 wcA.lpfnWndProc = RichEditANSIWndProc;
1406 wcA.cbClsExtra = 0;
1407 wcA.cbWndExtra = 4;
1408 wcA.hInstance = NULL; /* hInstance would register DLL-local class */
1409 wcA.hIcon = NULL;
1410 wcA.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
1411 wcA.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
1412 wcA.lpszMenuName = NULL;
1413 wcA.lpszClassName = "RichEdit20A";
1414 bResult = RegisterClassA(&wcA);
1415 assert(bResult);
1416 wcA.lpszClassName = "RichEdit50A";
1417 bResult = RegisterClassA(&wcA);
1418 assert(bResult);
1419 }
1420
1421 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
1422 {
1423 TRACE("\n");
1424 switch (fdwReason)
1425 {
1426 case DLL_PROCESS_ATTACH:
1427 DisableThreadLibraryCalls(hinstDLL);
1428 me_heap = HeapCreate (0, 0x10000, 0);
1429 ME_RegisterEditorClass(hinstDLL);
1430 break;
1431
1432 case DLL_PROCESS_DETACH:
1433 UnregisterClassW(wszClassName, 0);
1434 UnregisterClassW(wszClassName50, 0);
1435 UnregisterClassA("RichEdit20A", 0);
1436 UnregisterClassA("RichEdit50A", 0);
1437 HeapDestroy (me_heap);
1438 me_heap = NULL;
1439 break;
1440 }
1441 return TRUE;
1442 }
1443
1444 /******************************************************************
1445 * CreateTextServices (RICHED20.4)
1446 *
1447 * FIXME should be ITextHost instead of void*
1448 */
1449 HRESULT WINAPI CreateTextServices(IUnknown *punkOuter, void *pITextHost,
1450 IUnknown **ppUnk)
1451 {
1452 FIXME("stub\n");
1453 /* FIXME should support aggregation */
1454 if (punkOuter)
1455 return CLASS_E_NOAGGREGATION;
1456
1457 return E_FAIL; /* E_NOTIMPL isn't allowed by MSDN */
1458 }
1459
1460 /******************************************************************
1461 * REExtendedRegisterClass (RICHED20.8)
1462 *
1463 * FIXME undocumented
1464 */
1465 void WINAPI REExtendedRegisterClass(void)
1466 {
1467 FIXME("stub\n");
1468 }