Sync to Wine-20050725:
[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 (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_GETZOOM 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
141
142 Notifications
143
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
163
164 Styles
165
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
184 */
185
186 /*
187 * RICHED20 TODO (incomplete):
188 *
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)
208 * - full justification
209 * - hyphenation
210 * - tables
211 *
212 * Bugs that are probably fixed, but not so easy to verify:
213 * - EN_UPDATE/EN_CHANGE are handled very incorrectly (should be OK now)
214 * - undo for ME_JoinParagraphs doesn't store paragraph format ? (it does)
215 * - check/fix artificial EOL logic (bCursorAtEnd, hardly logical)
216 * - caret shouldn't be displayed when selection isn't empty
217 * - check refcounting in style management functions (looks perfect now, but no bugs is suspicious)
218 * - undo for setting default format (done, might be buggy)
219 * - styles might be not released properly (looks like they work like charm, but who knows?
220 *
221 */
222
223 #include "editor.h"
224 #include "commdlg.h"
225 #include "ole2.h"
226 #include "richole.h"
227 #include "winreg.h"
228 #define NO_SHLWAPI_STREAM
229 #include "shlwapi.h"
230
231 #include "rtf.h"
232
233 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
234
235 int me_debug = 0;
236 HANDLE me_heap = NULL;
237
238 static ME_TextBuffer *ME_MakeText(void) {
239
240 ME_TextBuffer *buf = ALLOC_OBJ(ME_TextBuffer);
241
242 ME_DisplayItem *p1 = ME_MakeDI(diTextStart);
243 ME_DisplayItem *p2 = ME_MakeDI(diTextEnd);
244
245 p1->prev = NULL;
246 p1->next = p2;
247 p2->prev = p1;
248 p2->next = NULL;
249 p1->member.para.next_para = p2;
250 p2->member.para.prev_para = p1;
251 p2->member.para.nCharOfs = 0;
252
253 buf->pFirst = p1;
254 buf->pLast = p2;
255 buf->pCharStyle = NULL;
256
257 return buf;
258 }
259
260
261 static LRESULT ME_StreamInText(ME_TextEditor *editor, DWORD dwFormat, ME_InStream *stream, ME_Style *style)
262 {
263 WCHAR wszText[STREAMIN_BUFFER_SIZE+1];
264 WCHAR *pText;
265
266 TRACE("%08lx %p\n", dwFormat, stream);
267
268 do {
269 long nWideChars = 0;
270
271 if (!stream->dwSize)
272 {
273 ME_StreamInFill(stream);
274 if (stream->editstream->dwError)
275 break;
276 if (!stream->dwSize)
277 break;
278 }
279
280 if (!(dwFormat & SF_UNICODE))
281 {
282 /* FIXME? this is doomed to fail on true MBCS like UTF-8, luckily they're unlikely to be used as CP_ACP */
283 nWideChars = MultiByteToWideChar(CP_ACP, 0, stream->buffer, stream->dwSize, wszText, STREAMIN_BUFFER_SIZE);
284 pText = wszText;
285 }
286 else
287 {
288 nWideChars = stream->dwSize >> 1;
289 pText = (WCHAR *)stream->buffer;
290 }
291
292 ME_InsertTextFromCursor(editor, 0, pText, nWideChars, style);
293 if (stream->dwSize < STREAMIN_BUFFER_SIZE)
294 break;
295 stream->dwSize = 0;
296 } while(1);
297 ME_CommitUndo(editor);
298 ME_Repaint(editor);
299 return 0;
300 }
301
302 static void ME_RTFCharAttrHook(RTF_Info *info)
303 {
304 CHARFORMAT2W fmt;
305 fmt.cbSize = sizeof(fmt);
306 fmt.dwMask = 0;
307
308 switch(info->rtfMinor)
309 {
310 case rtfPlain:
311 /* FIXME add more flags once they're implemented */
312 fmt.dwMask = CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_COLOR | CFM_BACKCOLOR | CFM_SIZE | CFM_WEIGHT;
313 fmt.dwEffects = CFE_AUTOCOLOR | CFE_AUTOBACKCOLOR;
314 fmt.yHeight = 12*20; /* 12pt */
315 fmt.wWeight = 400;
316 break;
317 case rtfBold:
318 fmt.dwMask = CFM_BOLD;
319 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
320 break;
321 case rtfItalic:
322 fmt.dwMask = CFM_ITALIC;
323 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
324 break;
325 case rtfUnderline:
326 fmt.dwMask = CFM_UNDERLINE;
327 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
328 fmt.bUnderlineType = CFU_CF1UNDERLINE;
329 break;
330 case rtfNoUnderline:
331 fmt.dwMask = CFM_UNDERLINE;
332 fmt.dwEffects = 0;
333 break;
334 case rtfStrikeThru:
335 fmt.dwMask = CFM_STRIKEOUT;
336 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
337 break;
338 case rtfSubScript:
339 case rtfSuperScript:
340 case rtfSubScrShrink:
341 case rtfSuperScrShrink:
342 case rtfNoSuperSub:
343 fmt.dwMask = CFM_SUBSCRIPT|CFM_SUPERSCRIPT;
344 if (info->rtfMinor == rtfSubScrShrink) fmt.dwEffects = CFE_SUBSCRIPT;
345 if (info->rtfMinor == rtfSuperScrShrink) fmt.dwEffects = CFE_SUPERSCRIPT;
346 if (info->rtfMinor == rtfNoSuperSub) fmt.dwEffects = 0;
347 break;
348 case rtfBackColor:
349 fmt.dwMask = CFM_BACKCOLOR;
350 fmt.dwEffects = 0;
351 if (info->rtfParam == 0)
352 fmt.dwEffects = CFE_AUTOBACKCOLOR;
353 else if (info->rtfParam != rtfNoParam)
354 {
355 RTFColor *c = RTFGetColor(info, info->rtfParam);
356 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
357 }
358 break;
359 case rtfForeColor:
360 fmt.dwMask = CFM_COLOR;
361 fmt.dwEffects = 0;
362 if (info->rtfParam == 0)
363 fmt.dwEffects = CFE_AUTOCOLOR;
364 else if (info->rtfParam != rtfNoParam)
365 {
366 RTFColor *c = RTFGetColor(info, info->rtfParam);
367 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
368 }
369 break;
370 case rtfFontNum:
371 if (info->rtfParam != rtfNoParam)
372 {
373 RTFFont *f = RTFGetFont(info, info->rtfParam);
374 if (f)
375 {
376 MultiByteToWideChar(CP_ACP, 0, f->rtfFName, -1, fmt.szFaceName, sizeof(fmt.szFaceName)/sizeof(WCHAR));
377 fmt.szFaceName[sizeof(fmt.szFaceName)/sizeof(WCHAR)-1] = '\0';
378 fmt.bCharSet = f->rtfFCharSet;
379 fmt.dwMask = CFM_FACE | CFM_CHARSET;
380 }
381 }
382 break;
383 case rtfFontSize:
384 fmt.dwMask = CFM_SIZE;
385 if (info->rtfParam != rtfNoParam)
386 fmt.yHeight = info->rtfParam*10;
387 break;
388 }
389 if (fmt.dwMask) {
390 ME_Style *style2;
391 RTFFlushOutputBuffer(info);
392 /* FIXME too slow ? how come ? */
393 style2 = ME_ApplyStyle(info->style, &fmt);
394 ME_ReleaseStyle(info->style);
395 info->style = style2;
396 }
397 }
398
399 /* FIXME this function doesn't get any information about context of the RTF tag, which is very bad,
400 the same tags mean different things in different contexts */
401 static void ME_RTFParAttrHook(RTF_Info *info)
402 {
403 PARAFORMAT2 fmt;
404 fmt.cbSize = sizeof(fmt);
405 fmt.dwMask = 0;
406
407 switch(info->rtfMinor)
408 {
409 case rtfParDef: /* I'm not 100% sure what does it do, but I guess it restores default paragraph attributes */
410 fmt.dwMask = PFM_ALIGNMENT | PFM_TABSTOPS | PFM_OFFSET | PFM_STARTINDENT;
411 fmt.wAlignment = PFA_LEFT;
412 fmt.cTabCount = 0;
413 fmt.dxOffset = fmt.dxStartIndent = 0;
414 break;
415 case rtfFirstIndent:
416 ME_GetSelectionParaFormat(info->editor, &fmt);
417 fmt.dwMask = PFM_STARTINDENT;
418 fmt.dxStartIndent = info->rtfParam + fmt.dxOffset;
419 break;
420 case rtfLeftIndent:
421 {
422 int first, left;
423 ME_GetSelectionParaFormat(info->editor, &fmt);
424 first = fmt.dxStartIndent;
425 left = info->rtfParam;
426 fmt.dwMask = PFM_STARTINDENT|PFM_OFFSET;
427 fmt.dxStartIndent = first + left;
428 fmt.dxOffset = -first;
429 break;
430 }
431 case rtfRightIndent:
432 fmt.dwMask = PFM_RIGHTINDENT;
433 fmt.dxRightIndent = info->rtfParam;
434 break;
435 case rtfQuadLeft:
436 case rtfQuadJust:
437 fmt.dwMask = PFM_ALIGNMENT;
438 fmt.wAlignment = PFA_LEFT;
439 break;
440 case rtfQuadRight:
441 fmt.dwMask = PFM_ALIGNMENT;
442 fmt.wAlignment = PFA_RIGHT;
443 break;
444 case rtfQuadCenter:
445 fmt.dwMask = PFM_ALIGNMENT;
446 fmt.wAlignment = PFA_CENTER;
447 break;
448 case rtfTabPos:
449 ME_GetSelectionParaFormat(info->editor, &fmt);
450 if (!(fmt.dwMask & PFM_TABSTOPS))
451 {
452 fmt.dwMask |= PFM_TABSTOPS;
453 fmt.cTabCount = 0;
454 }
455 if (fmt.cTabCount < MAX_TAB_STOPS)
456 fmt.rgxTabs[fmt.cTabCount++] = info->rtfParam;
457 break;
458 }
459 if (fmt.dwMask) {
460 RTFFlushOutputBuffer(info);
461 /* FIXME too slow ? how come ?*/
462 ME_SetSelectionParaFormat(info->editor, &fmt);
463 }
464 }
465
466 static void ME_RTFReadHook(RTF_Info *info) {
467 switch(info->rtfClass)
468 {
469 case rtfGroup:
470 switch(info->rtfMajor)
471 {
472 case rtfBeginGroup:
473 if (info->stackTop < maxStack) {
474 memcpy(&info->stack[info->stackTop].fmt, &info->style->fmt, sizeof(CHARFORMAT2W));
475 info->stack[info->stackTop].codePage = info->codePage;
476 info->stack[info->stackTop].unicodeLength = info->unicodeLength;
477 }
478 info->stackTop++;
479 break;
480 case rtfEndGroup:
481 {
482 ME_Style *s;
483 RTFFlushOutputBuffer(info);
484 info->stackTop--;
485 /* FIXME too slow ? how come ? */
486 s = ME_ApplyStyle(info->style, &info->stack[info->stackTop].fmt);
487 ME_ReleaseStyle(info->style);
488 info->style = s;
489 info->codePage = info->stack[info->stackTop].codePage;
490 info->unicodeLength = info->stack[info->stackTop].unicodeLength;
491 break;
492 }
493 }
494 break;
495 case rtfControl:
496 switch(info->rtfMajor)
497 {
498 case rtfCharAttr:
499 ME_RTFCharAttrHook(info);
500 break;
501 case rtfParAttr:
502 ME_RTFParAttrHook(info);
503 break;
504 }
505 break;
506 }
507 }
508
509 void
510 ME_StreamInFill(ME_InStream *stream)
511 {
512 stream->editstream->dwError = stream->editstream->pfnCallback(stream->editstream->dwCookie,
513 stream->buffer,
514 sizeof(stream->buffer),
515 &stream->dwSize);
516 stream->dwUsed = 0;
517 }
518
519 static LRESULT ME_StreamIn(ME_TextEditor *editor, DWORD format, EDITSTREAM *stream)
520 {
521 RTF_Info parser;
522 ME_Style *style;
523 int from, to, to2, nUndoMode;
524 ME_UndoItem *pUI;
525 int nEventMask = editor->nEventMask;
526 ME_InStream inStream;
527
528 TRACE("stream==%p hWnd==%p format==0x%X\n", stream, editor->hWnd, (UINT)format);
529 editor->nEventMask = 0;
530
531 ME_GetSelection(editor, &from, &to);
532 if (format & SFF_SELECTION) {
533 style = ME_GetSelectionInsertStyle(editor);
534
535 ME_InternalDeleteText(editor, from, to-from);
536 }
537 else {
538 style = editor->pBuffer->pDefaultStyle;
539 ME_AddRefStyle(style);
540 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
541 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
542 from = to = 0;
543 ME_ClearTempStyle(editor);
544 /* FIXME restore default paragraph formatting ! */
545 }
546
547 nUndoMode = editor->nUndoMode;
548 editor->nUndoMode = umIgnore;
549
550 inStream.editstream = stream;
551 inStream.editstream->dwError = 0;
552 inStream.dwSize = 0;
553 inStream.dwUsed = 0;
554
555 if (format & SF_RTF)
556 {
557 /* Check if it's really RTF, and if it is not, use plain text */
558 ME_StreamInFill(&inStream);
559 if (!inStream.editstream->dwError)
560 {
561 if (strncmp(inStream.buffer, "{\\rtf1", 6) && strncmp(inStream.buffer, "{\\urtf", 6))
562 {
563 format &= ~SF_RTF;
564 format |= SF_TEXT;
565 }
566 }
567 }
568
569 if (!inStream.editstream->dwError)
570 {
571 if (format & SF_RTF) {
572 /* setup the RTF parser */
573 memset(&parser, 0, sizeof parser);
574 RTFSetEditStream(&parser, &inStream);
575 parser.rtfFormat = format&(SF_TEXT|SF_RTF);
576 parser.hwndEdit = editor->hWnd;
577 parser.editor = editor;
578 parser.style = style;
579 WriterInit(&parser);
580 RTFInit(&parser);
581 RTFSetReadHook(&parser, ME_RTFReadHook);
582 BeginFile(&parser);
583
584 /* do the parsing */
585 RTFRead(&parser);
586 RTFFlushOutputBuffer(&parser);
587 RTFDestroy(&parser);
588
589 style = parser.style;
590 }
591 else if (format & SF_TEXT)
592 ME_StreamInText(editor, format, &inStream, style);
593 else
594 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
595 ME_GetSelection(editor, &to, &to2);
596 /* put the cursor at the top */
597 if (!(format & SFF_SELECTION))
598 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
599 else
600 {
601 /* FIXME where to put cursor now ? */
602 }
603 }
604
605 editor->nUndoMode = nUndoMode;
606 pUI = ME_AddUndoItem(editor, diUndoDeleteRun, NULL);
607 TRACE("from %d to %d\n", from, to);
608 if (pUI && from < to)
609 {
610 pUI->nStart = from;
611 pUI->nLen = to-from;
612 }
613 ME_CommitUndo(editor);
614 ME_ReleaseStyle(style);
615 editor->nEventMask = nEventMask;
616 InvalidateRect(editor->hWnd, NULL, TRUE);
617 ME_UpdateRepaint(editor);
618 if (!(format & SFF_SELECTION)) {
619 ME_ClearTempStyle(editor);
620 }
621 ME_MoveCaret(editor);
622 ME_SendSelChange(editor);
623
624 return 0;
625 }
626
627
628 ME_DisplayItem *
629 ME_FindItemAtOffset(ME_TextEditor *editor, ME_DIType nItemType, int nOffset, int *nItemOffset)
630 {
631 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
632
633 while (item && item->member.para.next_para->member.para.nCharOfs <= nOffset)
634 item = ME_FindItemFwd(item, diParagraph);
635
636 if (!item)
637 return item;
638
639 nOffset -= item->member.para.nCharOfs;
640 if (nItemType == diParagraph) {
641 if (nItemOffset)
642 *nItemOffset = nOffset;
643 return item;
644 }
645
646 do {
647 item = ME_FindItemFwd(item, diRun);
648 } while (item && (item->member.run.nCharOfs + ME_StrLen(item->member.run.strText) <= nOffset));
649 if (item) {
650 nOffset -= item->member.run.nCharOfs;
651 if (nItemOffset)
652 *nItemOffset = nOffset;
653 }
654 return item;
655 }
656
657
658 static int
659 ME_FindText(ME_TextEditor *editor, DWORD flags, CHARRANGE *chrg, WCHAR *text, CHARRANGE *chrgText)
660 {
661 int nStart = chrg->cpMin;
662 int nLen = lstrlenW(text);
663 ME_DisplayItem *item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
664 ME_DisplayItem *para;
665
666 if (!item)
667 return -1;
668
669 if (!nLen)
670 {
671 if (chrgText)
672 chrgText->cpMin = chrgText->cpMax = chrg->cpMin;
673 return chrg->cpMin;
674 }
675
676 if (!(flags & FR_DOWN))
677 FIXME("Backward search not implemented\n");
678 if (!(flags & FR_MATCHCASE))
679 FIXME("Case-insensitive search not implemented\n");
680 if (flags & ~(FR_DOWN | FR_MATCHCASE))
681 FIXME("Flags 0x%08lx not implemented\n", flags & ~(FR_DOWN | FR_MATCHCASE));
682
683 para = ME_GetParagraph(item);
684 while (item && para->member.para.nCharOfs + item->member.run.nCharOfs + nStart + nLen < chrg->cpMax)
685 {
686 ME_DisplayItem *pCurItem = item;
687 int nCurStart = nStart;
688 int nMatched = 0;
689
690 while (pCurItem->member.run.strText->szData[nCurStart + nMatched] == text[nMatched])
691 {
692 nMatched++;
693 if (nMatched == nLen)
694 {
695 nStart += para->member.para.nCharOfs + item->member.run.nCharOfs;
696 if (chrgText)
697 {
698 chrgText->cpMin = nStart;
699 chrgText->cpMax = nStart + nLen;
700 }
701 return nStart;
702 }
703 if (nCurStart + nMatched == ME_StrLen(pCurItem->member.run.strText))
704 {
705 pCurItem = ME_FindItemFwd(pCurItem, diRun);
706 nCurStart = -nMatched;
707 }
708 }
709 nStart++;
710 if (nStart == ME_StrLen(item->member.run.strText))
711 {
712 item = ME_FindItemFwd(item, diRun);
713 para = ME_GetParagraph(item);
714 nStart = 0;
715 }
716 }
717 return -1;
718 }
719
720
721 ME_TextEditor *ME_MakeEditor(HWND hWnd) {
722 ME_TextEditor *ed = ALLOC_OBJ(ME_TextEditor);
723 HDC hDC;
724 int i;
725 ed->hWnd = hWnd;
726 ed->bEmulateVersion10 = FALSE;
727 ed->pBuffer = ME_MakeText();
728 hDC = GetDC(hWnd);
729 ME_MakeFirstParagraph(hDC, ed->pBuffer);
730 ReleaseDC(hWnd, hDC);
731 ed->bCaretShown = FALSE;
732 ed->nCursors = 3;
733 ed->pCursors = ALLOC_N_OBJ(ME_Cursor, ed->nCursors);
734 ed->pCursors[0].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
735 ed->pCursors[0].nOffset = 0;
736 ed->pCursors[1].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
737 ed->pCursors[1].nOffset = 0;
738 ed->nLastTotalLength = ed->nTotalLength = 0;
739 ed->nUDArrowX = -1;
740 ed->nSequence = 0;
741 ed->rgbBackColor = -1;
742 ed->bCaretAtEnd = FALSE;
743 ed->nEventMask = 0;
744 ed->nModifyStep = 0;
745 ed->pUndoStack = ed->pRedoStack = NULL;
746 ed->nUndoMode = umAddToUndo;
747 ed->nParagraphs = 1;
748 ed->nLastSelStart = ed->nLastSelEnd = 0;
749 ed->nScrollPosY = 0;
750 ed->nZoomNumerator = ed->nZoomDenominator = 0;
751 for (i=0; i<HFONT_CACHE_SIZE; i++)
752 {
753 ed->pFontCache[i].nRefs = 0;
754 ed->pFontCache[i].nAge = 0;
755 ed->pFontCache[i].hFont = NULL;
756 }
757 ME_CheckCharOffsets(ed);
758 return ed;
759 }
760
761 typedef struct tagME_GlobalDestStruct
762 {
763 HGLOBAL hData;
764 int nLength;
765 } ME_GlobalDestStruct;
766
767 static DWORD CALLBACK ME_AppendToHGLOBAL(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
768 {
769 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
770 int nMaxSize;
771 BYTE *pDest;
772
773 nMaxSize = GlobalSize(pData->hData);
774 if (pData->nLength+cb+1 >= cb)
775 {
776 /* round up to 2^17 */
777 int nNewSize = (((nMaxSize+cb+1)|0x1FFFF)+1) & 0xFFFE0000;
778 pData->hData = GlobalReAlloc(pData->hData, nNewSize, 0);
779 }
780 pDest = (BYTE *)GlobalLock(pData->hData);
781 memcpy(pDest + pData->nLength, lpBuff, cb);
782 pData->nLength += cb;
783 pDest[pData->nLength] = '\0';
784 GlobalUnlock(pData->hData);
785 *pcb = cb;
786
787 return 0;
788 }
789
790 static DWORD CALLBACK ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
791 {
792 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
793 int i;
794 WORD *pSrc, *pDest;
795
796 cb = cb >> 1;
797 pDest = (WORD *)lpBuff;
798 pSrc = (WORD *)GlobalLock(pData->hData);
799 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
800 pDest[i] = pSrc[pData->nLength+i];
801 }
802 pData->nLength += i;
803 *pcb = 2*i;
804 GlobalUnlock(pData->hData);
805 return 0;
806 }
807
808 static DWORD CALLBACK ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
809 {
810 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
811 int i;
812 BYTE *pSrc, *pDest;
813
814 pDest = lpBuff;
815 pSrc = (BYTE *)GlobalLock(pData->hData);
816 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
817 pDest[i] = pSrc[pData->nLength+i];
818 }
819 pData->nLength += i;
820 *pcb = i;
821 GlobalUnlock(pData->hData);
822 return 0;
823 }
824
825
826 void ME_DestroyEditor(ME_TextEditor *editor)
827 {
828 ME_DisplayItem *pFirst = editor->pBuffer->pFirst;
829 ME_DisplayItem *p = pFirst, *pNext = NULL;
830 int i;
831
832 ME_ClearTempStyle(editor);
833 ME_EmptyUndoStack(editor);
834 while(p) {
835 pNext = p->next;
836 ME_DestroyDisplayItem(p);
837 p = pNext;
838 }
839 ME_ReleaseStyle(editor->pBuffer->pDefaultStyle);
840 for (i=0; i<HFONT_CACHE_SIZE; i++)
841 {
842 if (editor->pFontCache[i].hFont)
843 DeleteObject(editor->pFontCache[i].hFont);
844 }
845
846 FREE_OBJ(editor);
847 }
848
849 static WCHAR wszClassName[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '2', '0', 'W', 0};
850 static WCHAR wszClassName50[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '5', '0', 'W', 0};
851
852 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
853 {
854 TRACE("\n");
855 switch (fdwReason)
856 {
857 case DLL_PROCESS_ATTACH:
858 DisableThreadLibraryCalls(hinstDLL);
859 me_heap = HeapCreate (0, 0x10000, 0);
860 ME_RegisterEditorClass(hinstDLL);
861 break;
862
863 case DLL_PROCESS_DETACH:
864 UnregisterClassW(wszClassName, 0);
865 UnregisterClassW(wszClassName50, 0);
866 UnregisterClassA("RichEdit20A", 0);
867 UnregisterClassA("RichEdit50A", 0);
868 HeapDestroy (me_heap);
869 me_heap = NULL;
870 break;
871 }
872 return TRUE;
873 }
874
875
876 #define UNSUPPORTED_MSG(e) \
877 case e: \
878 FIXME(#e ": stub\n"); \
879 return DefWindowProcW(hWnd, msg, wParam, lParam);
880
881 static const char * const edit_messages[] = {
882 "EM_GETSEL",
883 "EM_SETSEL",
884 "EM_GETRECT",
885 "EM_SETRECT",
886 "EM_SETRECTNP",
887 "EM_SCROLL",
888 "EM_LINESCROLL",
889 "EM_SCROLLCARET",
890 "EM_GETMODIFY",
891 "EM_SETMODIFY",
892 "EM_GETLINECOUNT",
893 "EM_LINEINDEX",
894 "EM_SETHANDLE",
895 "EM_GETHANDLE",
896 "EM_GETTHUMB",
897 "EM_UNKNOWN_BF",
898 "EM_UNKNOWN_C0",
899 "EM_LINELENGTH",
900 "EM_REPLACESEL",
901 "EM_UNKNOWN_C3",
902 "EM_GETLINE",
903 "EM_LIMITTEXT",
904 "EM_CANUNDO",
905 "EM_UNDO",
906 "EM_FMTLINES",
907 "EM_LINEFROMCHAR",
908 "EM_UNKNOWN_CA",
909 "EM_SETTABSTOPS",
910 "EM_SETPASSWORDCHAR",
911 "EM_EMPTYUNDOBUFFER",
912 "EM_GETFIRSTVISIBLELINE",
913 "EM_SETREADONLY",
914 "EM_SETWORDBREAKPROC",
915 "EM_GETWORDBREAKPROC",
916 "EM_GETPASSWORDCHAR",
917 "EM_SETMARGINS",
918 "EM_GETMARGINS",
919 "EM_GETLIMITTEXT",
920 "EM_POSFROMCHAR",
921 "EM_CHARFROMPOS"
922 };
923
924 static const char * const richedit_messages[] = {
925 "EM_CANPASTE",
926 "EM_DISPLAYBAND",
927 "EM_EXGETSEL",
928 "EM_EXLIMITTEXT",
929 "EM_EXLINEFROMCHAR",
930 "EM_EXSETSEL",
931 "EM_FINDTEXT",
932 "EM_FORMATRANGE",
933 "EM_GETCHARFORMAT",
934 "EM_GETEVENTMASK",
935 "EM_GETOLEINTERFACE",
936 "EM_GETPARAFORMAT",
937 "EM_GETSELTEXT",
938 "EM_HIDESELECTION",
939 "EM_PASTESPECIAL",
940 "EM_REQUESTRESIZE",
941 "EM_SELECTIONTYPE",
942 "EM_SETBKGNDCOLOR",
943 "EM_SETCHARFORMAT",
944 "EM_SETEVENTMASK",
945 "EM_SETOLECALLBACK",
946 "EM_SETPARAFORMAT",
947 "EM_SETTARGETDEVICE",
948 "EM_STREAMIN",
949 "EM_STREAMOUT",
950 "EM_GETTEXTRANGE",
951 "EM_FINDWORDBREAK",
952 "EM_SETOPTIONS",
953 "EM_GETOPTIONS",
954 "EM_FINDTEXTEX",
955 "EM_GETWORDBREAKPROCEX",
956 "EM_SETWORDBREAKPROCEX",
957 "EM_SETUNDOLIMIT",
958 "EM_UNKNOWN_USER_83",
959 "EM_REDO",
960 "EM_CANREDO",
961 "EM_GETUNDONAME",
962 "EM_GETREDONAME",
963 "EM_STOPGROUPTYPING",
964 "EM_SETTEXTMODE",
965 "EM_GETTEXTMODE",
966 "EM_AUTOURLDETECT",
967 "EM_GETAUTOURLDETECT",
968 "EM_SETPALETTE",
969 "EM_GETTEXTEX",
970 "EM_GETTEXTLENGTHEX",
971 "EM_SHOWSCROLLBAR",
972 "EM_SETTEXTEX",
973 "EM_UNKNOWN_USER_98",
974 "EM_UNKNOWN_USER_99",
975 "EM_SETPUNCTUATION",
976 "EM_GETPUNCTUATION",
977 "EM_SETWORDWRAPMODE",
978 "EM_GETWORDWRAPMODE",
979 "EM_SETIMECOLOR",
980 "EM_GETIMECOLOR",
981 "EM_SETIMEOPTIONS",
982 "EM_GETIMEOPTIONS",
983 "EM_CONVPOSITION",
984 "EM_UNKNOWN_USER_109",
985 "EM_UNKNOWN_USER_110",
986 "EM_UNKNOWN_USER_111",
987 "EM_UNKNOWN_USER_112",
988 "EM_UNKNOWN_USER_113",
989 "EM_UNKNOWN_USER_114",
990 "EM_UNKNOWN_USER_115",
991 "EM_UNKNOWN_USER_116",
992 "EM_UNKNOWN_USER_117",
993 "EM_UNKNOWN_USER_118",
994 "EM_UNKNOWN_USER_119",
995 "EM_SETLANGOPTIONS",
996 "EM_GETLANGOPTIONS",
997 "EM_GETIMECOMPMODE",
998 "EM_FINDTEXTW",
999 "EM_FINDTEXTEXW",
1000 "EM_RECONVERSION",
1001 "EM_SETIMEMODEBIAS",
1002 "EM_GETIMEMODEBIAS"
1003 };
1004
1005 static const char *
1006 get_msg_name(UINT msg)
1007 {
1008 if (msg >= EM_GETSEL && msg <= EM_SETLIMITTEXT)
1009 return edit_messages[msg - EM_GETSEL];
1010 if (msg >= EM_CANPASTE && msg <= EM_GETIMEMODEBIAS)
1011 return richedit_messages[msg - EM_CANPASTE];
1012 return "";
1013 }
1014
1015 /******************************************************************
1016 * RichEditANSIWndProc (RICHED20.10)
1017 */
1018 LRESULT WINAPI RichEditANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
1019 HDC hDC;
1020 PAINTSTRUCT ps;
1021 SCROLLINFO si;
1022 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongW(hWnd, 0);
1023
1024 TRACE("msg %d (%s) %08x %08lx\n", msg, get_msg_name(msg), wParam, lParam);
1025
1026 switch(msg) {
1027
1028 UNSUPPORTED_MSG(EM_AUTOURLDETECT)
1029 UNSUPPORTED_MSG(EM_CHARFROMPOS)
1030 UNSUPPORTED_MSG(EM_DISPLAYBAND)
1031 UNSUPPORTED_MSG(EM_EXLIMITTEXT)
1032 UNSUPPORTED_MSG(EM_FINDWORDBREAK)
1033 UNSUPPORTED_MSG(EM_FMTLINES)
1034 UNSUPPORTED_MSG(EM_FORMATRANGE)
1035 UNSUPPORTED_MSG(EM_GETAUTOURLDETECT)
1036 UNSUPPORTED_MSG(EM_GETBIDIOPTIONS)
1037 UNSUPPORTED_MSG(EM_GETEDITSTYLE)
1038 UNSUPPORTED_MSG(EM_GETFIRSTVISIBLELINE)
1039 UNSUPPORTED_MSG(EM_GETIMECOMPMODE)
1040 /* UNSUPPORTED_MSG(EM_GETIMESTATUS) missing in Wine headers */
1041 UNSUPPORTED_MSG(EM_GETLANGOPTIONS)
1042 UNSUPPORTED_MSG(EM_GETLIMITTEXT)
1043 UNSUPPORTED_MSG(EM_GETLINE)
1044 /* UNSUPPORTED_MSG(EM_GETOLEINTERFACE) separate stub */
1045 UNSUPPORTED_MSG(EM_GETOPTIONS)
1046 UNSUPPORTED_MSG(EM_GETPASSWORDCHAR)
1047 UNSUPPORTED_MSG(EM_GETRECT)
1048 UNSUPPORTED_MSG(EM_GETREDONAME)
1049 UNSUPPORTED_MSG(EM_GETSCROLLPOS)
1050 UNSUPPORTED_MSG(EM_GETTEXTMODE)
1051 UNSUPPORTED_MSG(EM_GETTYPOGRAPHYOPTIONS)
1052 UNSUPPORTED_MSG(EM_GETUNDONAME)
1053 UNSUPPORTED_MSG(EM_GETWORDBREAKPROC)
1054 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX)
1055 UNSUPPORTED_MSG(EM_HIDESELECTION)
1056 UNSUPPORTED_MSG(EM_LIMITTEXT) /* also known as EM_SETLIMITTEXT */
1057 UNSUPPORTED_MSG(EM_PASTESPECIAL)
1058 /* UNSUPPORTED_MSG(EM_POSFROMCHARS) missing in Wine headers */
1059 UNSUPPORTED_MSG(EM_REQUESTRESIZE)
1060 UNSUPPORTED_MSG(EM_SCROLL)
1061 UNSUPPORTED_MSG(EM_SCROLLCARET)
1062 UNSUPPORTED_MSG(EM_SELECTIONTYPE)
1063 UNSUPPORTED_MSG(EM_SETBIDIOPTIONS)
1064 UNSUPPORTED_MSG(EM_SETEDITSTYLE)
1065 UNSUPPORTED_MSG(EM_SETFONTSIZE)
1066 UNSUPPORTED_MSG(EM_SETLANGOPTIONS)
1067 UNSUPPORTED_MSG(EM_SETOLECALLBACK)
1068 UNSUPPORTED_MSG(EM_SETOPTIONS)
1069 UNSUPPORTED_MSG(EM_SETPALETTE)
1070 UNSUPPORTED_MSG(EM_SETPASSWORDCHAR)
1071 UNSUPPORTED_MSG(EM_SETRECT)
1072 UNSUPPORTED_MSG(EM_SETRECTNP)
1073 UNSUPPORTED_MSG(EM_SETSCROLLPOS)
1074 UNSUPPORTED_MSG(EM_SETTABSTOPS)
1075 UNSUPPORTED_MSG(EM_SETTARGETDEVICE)
1076 UNSUPPORTED_MSG(EM_SETTEXTMODE)
1077 UNSUPPORTED_MSG(EM_SETTYPOGRAPHYOPTIONS)
1078 UNSUPPORTED_MSG(EM_SETUNDOLIMIT)
1079 UNSUPPORTED_MSG(EM_SETWORDBREAKPROC)
1080 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX)
1081 UNSUPPORTED_MSG(EM_SHOWSCROLLBAR)
1082 UNSUPPORTED_MSG(WM_SETFONT)
1083 UNSUPPORTED_MSG(WM_STYLECHANGING)
1084 UNSUPPORTED_MSG(WM_STYLECHANGED)
1085 /* UNSUPPORTED_MSG(WM_UNICHAR) FIXME missing in Wine headers */
1086
1087 /* Messages specific to Richedit controls */
1088
1089 case EM_STREAMIN:
1090 return ME_StreamIn(editor, wParam, (EDITSTREAM*)lParam);
1091 case EM_STREAMOUT:
1092 return ME_StreamOut(editor, wParam, (EDITSTREAM *)lParam);
1093 case WM_GETDLGCODE:
1094 {
1095 UINT code = DLGC_WANTCHARS|DLGC_WANTARROWS;
1096 if (GetWindowLongW(hWnd, GWL_STYLE)&ES_WANTRETURN)
1097 code |= 0; /* FIXME what can we do here ? ask for messages and censor them ? */
1098 return code;
1099 }
1100 case WM_NCCREATE:
1101 {
1102 CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
1103 TRACE("WM_NCCREATE: style 0x%08lx\n", pcs->style);
1104 editor = ME_MakeEditor(hWnd);
1105 SetWindowLongW(hWnd, 0, (long)editor);
1106 pcs = 0; /* ignore */
1107 return TRUE;
1108 }
1109 case EM_EMPTYUNDOBUFFER:
1110 ME_EmptyUndoStack(editor);
1111 return 0;
1112 case EM_GETSEL:
1113 {
1114 /* Note: wParam/lParam can be NULL */
1115 UINT from, to;
1116 PUINT pfrom = wParam ? (PUINT)wParam : &from;
1117 PUINT pto = lParam ? (PUINT)lParam : &to;
1118 ME_GetSelection(editor, pfrom, pto);
1119 if ((*pfrom|*pto) & 0xFFFF0000)
1120 return -1;
1121 return MAKELONG(*pfrom,*pto);
1122 }
1123 case EM_EXGETSEL:
1124 {
1125 CHARRANGE *pRange = (CHARRANGE *)lParam;
1126 ME_GetSelection(editor, (int *)&pRange->cpMin, (int *)&pRange->cpMax);
1127 TRACE("EM_EXGETSEL = (%ld,%ld)\n", pRange->cpMin, pRange->cpMax);
1128 return 0;
1129 }
1130 case EM_CANUNDO:
1131 return editor->pUndoStack != NULL;
1132 case EM_CANREDO:
1133 return editor->pRedoStack != NULL;
1134 case EM_UNDO:
1135 ME_Undo(editor);
1136 return 0;
1137 case EM_REDO:
1138 ME_Redo(editor);
1139 return 0;
1140 case EM_SETSEL:
1141 {
1142 ME_SetSelection(editor, wParam, lParam);
1143 ME_Repaint(editor);
1144 ME_SendSelChange(editor);
1145 return 0;
1146 }
1147 case EM_EXSETSEL:
1148 {
1149 CHARRANGE *pRange = (CHARRANGE *)lParam;
1150 TRACE("EM_EXSETSEL (%ld,%ld)\n", pRange->cpMin, pRange->cpMax);
1151 ME_SetSelection(editor, pRange->cpMin, pRange->cpMax);
1152 /* FIXME optimize */
1153 ME_Repaint(editor);
1154 ME_SendSelChange(editor);
1155 return 0;
1156 }
1157 case EM_SETTEXTEX:
1158 {
1159 LPWSTR wszText = (LPWSTR)lParam;
1160 SETTEXTEX *pStruct = (SETTEXTEX *)wParam;
1161 size_t len = lstrlenW(wszText);
1162 int from, to;
1163 ME_Style *style;
1164 TRACE("EM_SETTEXEX - %s, flags %d, cp %d\n", debugstr_w(wszText), (int)pStruct->flags, pStruct->codepage);
1165 if (pStruct->codepage != 1200) {
1166 FIXME("EM_SETTEXTEX only supports unicode right now!\n");
1167 return 0;
1168 }
1169 /* FIXME: this should support RTF strings too, according to MSDN */
1170 if (pStruct->flags & ST_SELECTION) {
1171 ME_GetSelection(editor, &from, &to);
1172 style = ME_GetSelectionInsertStyle(editor);
1173 ME_InternalDeleteText(editor, from, to - from);
1174 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
1175 ME_ReleaseStyle(style);
1176 }
1177 else {
1178 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
1179 ME_InsertTextFromCursor(editor, 0, wszText, -1, editor->pBuffer->pDefaultStyle);
1180 len = 1;
1181 }
1182 ME_CommitUndo(editor);
1183 if (!(pStruct->flags & ST_KEEPUNDO))
1184 ME_EmptyUndoStack(editor);
1185 ME_UpdateRepaint(editor);
1186 return len;
1187 }
1188 case EM_SETBKGNDCOLOR:
1189 {
1190 LRESULT lColor = ME_GetBackColor(editor);
1191 if (wParam)
1192 editor->rgbBackColor = -1;
1193 else
1194 editor->rgbBackColor = lParam;
1195 InvalidateRect(hWnd, NULL, TRUE);
1196 UpdateWindow(hWnd);
1197 return lColor;
1198 }
1199 case EM_GETMODIFY:
1200 return editor->nModifyStep == 0 ? 0 : 1;
1201 case EM_SETMODIFY:
1202 {
1203 if (wParam)
1204 editor->nModifyStep = 0x80000000;
1205 else
1206 editor->nModifyStep = 0;
1207
1208 return 0;
1209 }
1210 case EM_SETREADONLY:
1211 {
1212 long nStyle = GetWindowLongW(hWnd, GWL_STYLE);
1213 if (wParam)
1214 nStyle |= ES_READONLY;
1215 else
1216 nStyle &= ~ES_READONLY;
1217 SetWindowLongW(hWnd, GWL_STYLE, nStyle);
1218 ME_Repaint(editor);
1219 return 0;
1220 }
1221 case EM_SETEVENTMASK:
1222 editor->nEventMask = lParam;
1223 return 0;
1224 case EM_GETEVENTMASK:
1225 return editor->nEventMask;
1226 case EM_SETCHARFORMAT:
1227 {
1228 CHARFORMAT2W buf, *p;
1229 BOOL bRepaint = TRUE;
1230 p = ME_ToCF2W(&buf, (CHARFORMAT2W *)lParam);
1231 if (!wParam)
1232 ME_SetDefaultCharFormat(editor, p);
1233 else if (wParam == (SCF_WORD | SCF_SELECTION))
1234 FIXME("EM_SETCHARFORMAT: word selection not supported\n");
1235 else if (wParam == SCF_ALL)
1236 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), p);
1237 else {
1238 int from, to;
1239 ME_GetSelection(editor, &from, &to);
1240 bRepaint = (from != to);
1241 ME_SetSelectionCharFormat(editor, p);
1242 }
1243 ME_CommitUndo(editor);
1244 if (bRepaint)
1245 ME_UpdateRepaint(editor);
1246 return 0;
1247 }
1248 case EM_GETCHARFORMAT:
1249 {
1250 CHARFORMAT2W tmp, *dst = (CHARFORMAT2W *)lParam;
1251 if (dst->cbSize != sizeof(CHARFORMATA) &&
1252 dst->cbSize != sizeof(CHARFORMATW) &&
1253 dst->cbSize != sizeof(CHARFORMAT2A) &&
1254 dst->cbSize != sizeof(CHARFORMAT2W))
1255 return 0;
1256 tmp.cbSize = sizeof(tmp);
1257 if (!wParam)
1258 ME_GetDefaultCharFormat(editor, &tmp);
1259 else
1260 ME_GetSelectionCharFormat(editor, &tmp);
1261 ME_CopyToCFAny(dst, &tmp);
1262 return tmp.dwMask;
1263 }
1264 case EM_SETPARAFORMAT:
1265 ME_SetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
1266 ME_UpdateRepaint(editor);
1267 ME_CommitUndo(editor);
1268 return 0;
1269 case EM_GETPARAFORMAT:
1270 ME_GetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
1271 return 0;
1272 case EM_LINESCROLL:
1273 {
1274 int nPos = editor->nScrollPosY, nEnd= editor->nTotalLength - editor->sizeWindow.cy;
1275 nPos += 8 * lParam; /* FIXME follow the original */
1276 if (nPos>=nEnd)
1277 nPos = nEnd;
1278 if (nPos<0)
1279 nPos = 0;
1280 if (nPos != editor->nScrollPosY) {
1281 ScrollWindow(hWnd, 0, editor->nScrollPosY-nPos, NULL, NULL);
1282 editor->nScrollPosY = nPos;
1283 SetScrollPos(hWnd, SB_VERT, nPos, TRUE);
1284 UpdateWindow(hWnd);
1285 }
1286 return TRUE; /* Should return false if a single line richedit control */
1287 }
1288 case WM_CLEAR:
1289 {
1290 int from, to;
1291 ME_GetSelection(editor, &from, &to);
1292 ME_InternalDeleteText(editor, from, to-from);
1293 ME_CommitUndo(editor);
1294 ME_UpdateRepaint(editor);
1295 return 0;
1296 }
1297 case EM_REPLACESEL:
1298 {
1299 int from, to;
1300 ME_Style *style;
1301 LPWSTR wszText = ME_ToUnicode(hWnd, (void *)lParam);
1302 size_t len = lstrlenW(wszText);
1303 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText));
1304
1305 ME_GetSelection(editor, &from, &to);
1306 style = ME_GetSelectionInsertStyle(editor);
1307 ME_InternalDeleteText(editor, from, to-from);
1308 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
1309 ME_ReleaseStyle(style);
1310 ME_EndToUnicode(hWnd, wszText);
1311 /* drop temporary style if line end */
1312 /* FIXME question: does abc\n mean: put abc, clear temp style, put \n? (would require a change) */
1313 if (len>0 && wszText[len-1] == '\n')
1314 ME_ClearTempStyle(editor);
1315
1316 ME_CommitUndo(editor);
1317 if (!wParam)
1318 ME_EmptyUndoStack(editor);
1319 ME_UpdateRepaint(editor);
1320 return 0;
1321 }
1322 case WM_SETTEXT:
1323 {
1324 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
1325 if (lParam)
1326 {
1327 LPWSTR wszText = ME_ToUnicode(hWnd, (void *)lParam);
1328 TRACE("WM_SETTEXT lParam==%lx\n",lParam);
1329 TRACE("WM_SETTEXT - %s\n", debugstr_w(wszText)); /* debugstr_w() */
1330 if (lstrlenW(wszText) > 0)
1331 {
1332 /* uses default style! */
1333 ME_InsertTextFromCursor(editor, 0, wszText, -1, editor->pBuffer->pDefaultStyle);
1334 }
1335 ME_EndToUnicode(hWnd, wszText);
1336 }
1337 else
1338 TRACE("WM_SETTEXT - NULL\n");
1339 ME_CommitUndo(editor);
1340 ME_EmptyUndoStack(editor);
1341 ME_UpdateRepaint(editor);
1342 return 0;
1343 }
1344 case EM_CANPASTE:
1345 {
1346 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
1347 if (IsClipboardFormatAvailable(nRTFFormat))
1348 return TRUE;
1349 if (IsClipboardFormatAvailable(CF_UNICODETEXT))
1350 return TRUE;
1351 return FALSE;
1352 }
1353 case WM_PASTE:
1354 {
1355 DWORD dwFormat = 0;
1356 EDITSTREAM es;
1357 ME_GlobalDestStruct gds;
1358 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
1359 UINT cf = 0;
1360
1361 if (IsClipboardFormatAvailable(nRTFFormat))
1362 cf = nRTFFormat, dwFormat = SF_RTF;
1363 else if (IsClipboardFormatAvailable(CF_UNICODETEXT))
1364 cf = CF_UNICODETEXT, dwFormat = SF_TEXT|SF_UNICODE;
1365 else
1366 return 0;
1367
1368 if (!OpenClipboard(hWnd))
1369 return 0;
1370 gds.hData = GetClipboardData(cf);
1371 gds.nLength = 0;
1372 es.dwCookie = (DWORD)&gds;
1373 es.pfnCallback = dwFormat == SF_RTF ? ME_ReadFromHGLOBALRTF : ME_ReadFromHGLOBALUnicode;
1374 SendMessageW(hWnd, EM_STREAMIN, dwFormat|SFF_SELECTION, (LPARAM)&es);
1375
1376 CloseClipboard();
1377 return 0;
1378 }
1379 case WM_CUT:
1380 case WM_COPY:
1381 {
1382 int from, to, pars;
1383 WCHAR *data;
1384 HANDLE hData;
1385 EDITSTREAM es;
1386 ME_GlobalDestStruct gds;
1387
1388 if (!OpenClipboard(hWnd))
1389 return 0;
1390
1391 EmptyClipboard();
1392 ME_GetSelection(editor, &from, &to);
1393 pars = ME_CountParagraphsBetween(editor, from, to);
1394 hData = GlobalAlloc(GMEM_MOVEABLE, sizeof(WCHAR)*(to-from+pars+1));
1395 data = (WCHAR *)GlobalLock(hData);
1396 ME_GetTextW(editor, data, from, to-from, TRUE);
1397 GlobalUnlock(hData);
1398
1399 gds.hData = GlobalAlloc(GMEM_MOVEABLE, 0);
1400 gds.nLength = 0;
1401 es.dwCookie = (DWORD)&gds;
1402 es.pfnCallback = ME_AppendToHGLOBAL;
1403 SendMessageW(hWnd, EM_STREAMOUT, SFF_SELECTION|SF_RTF, (LPARAM)&es);
1404 GlobalReAlloc(gds.hData, gds.nLength+1, 0);
1405
1406 SetClipboardData(CF_UNICODETEXT, hData);
1407 SetClipboardData(RegisterClipboardFormatA("Rich Text Format"), gds.hData);
1408
1409 CloseClipboard();
1410 if (msg == WM_CUT)
1411 {
1412 ME_InternalDeleteText(editor, from, to-from);
1413 ME_CommitUndo(editor);
1414 ME_UpdateRepaint(editor);
1415 }
1416 return 0;
1417 }
1418 case WM_GETTEXTLENGTH:
1419 return ME_GetTextLength(editor);
1420 case EM_GETTEXTLENGTHEX:
1421 return ME_GetTextLengthEx(editor, (GETTEXTLENGTHEX *)wParam);
1422 case WM_GETTEXT:
1423 {
1424 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
1425 tr.chrg.cpMin = 0;
1426 tr.chrg.cpMax = wParam-1;
1427 tr.lpstrText = (WCHAR *)lParam;
1428 return RichEditANSIWndProc(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
1429 }
1430 case EM_GETTEXTEX:
1431 {
1432 GETTEXTEX *ex = (GETTEXTEX*)wParam;
1433
1434 if (ex->flags != 0)
1435 FIXME("Unhandled EM_GETTEXTEX flags 0x%lx\n",ex->flags);
1436
1437 if (IsWindowUnicode(hWnd))
1438 return ME_GetTextW(editor, (LPWSTR)lParam, 0, ex->cb, FALSE);
1439 else
1440 {
1441 LPWSTR buffer = HeapAlloc(GetProcessHeap(),0,ex->cb*sizeof(WCHAR));
1442 DWORD buflen = ex->cb;
1443 LRESULT rc;
1444 DWORD flags = 0;
1445
1446 buflen = ME_GetTextW(editor, buffer, 0, buflen, FALSE);
1447 rc = WideCharToMultiByte(ex->codepage, flags, buffer, buflen, (LPSTR)lParam, ex->cb, ex->lpDefaultChar, ex->lpUsedDefaultChar);
1448
1449 HeapFree(GetProcessHeap(),0,buffer);
1450 return rc;
1451 }
1452 }
1453 case EM_GETSELTEXT:
1454 {
1455 int from, to;
1456 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
1457 ME_GetSelection(editor, &from, &to);
1458 tr.chrg.cpMin = from;
1459 tr.chrg.cpMax = to;
1460 tr.lpstrText = (WCHAR *)lParam;
1461 return RichEditANSIWndProc(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
1462 }
1463 case EM_GETTEXTRANGE:
1464 {
1465 TEXTRANGEW *rng = (TEXTRANGEW *)lParam;
1466 if (IsWindowUnicode(hWnd))
1467 return ME_GetTextW(editor, rng->lpstrText, rng->chrg.cpMin, rng->chrg.cpMax-rng->chrg.cpMin, FALSE);
1468 else
1469 {
1470 int nLen = rng->chrg.cpMax-rng->chrg.cpMin;
1471 WCHAR *p = ALLOC_N_OBJ(WCHAR, nLen+1);
1472 int nChars = ME_GetTextW(editor, p, rng->chrg.cpMin, nLen, FALSE);
1473 /* FIXME this is a potential security hole (buffer overrun)
1474 if you know more about wchar->mbyte conversion please explain
1475 */
1476 WideCharToMultiByte(CP_ACP, 0, p, nChars+1, (char *)rng->lpstrText, nLen+1, NULL, NULL);
1477 FREE_OBJ(p);
1478 return nChars;
1479 }
1480 return ME_GetTextW(editor, rng->lpstrText, rng->chrg.cpMin, rng->chrg.cpMax-rng->chrg.cpMin, FALSE);
1481 }
1482 case EM_GETLINECOUNT:
1483 {
1484 ME_DisplayItem *item = editor->pBuffer->pFirst->next;
1485 int nRows = 0;
1486
1487 while (item != editor->pBuffer->pLast)
1488 {
1489 assert(item->type == diParagraph);
1490 nRows += item->member.para.nRows;
1491 item = item->member.para.next_para;
1492 }
1493 TRACE("EM_GETLINECOUNT: nRows==%d\n", nRows);
1494 return max(1, nRows);
1495 }
1496 case EM_LINEFROMCHAR:
1497 {
1498 if (wParam == -1)
1499 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(editor, 1));
1500 else
1501 return ME_RowNumberFromCharOfs(editor, wParam);
1502 }
1503 case EM_EXLINEFROMCHAR:
1504 {
1505 return ME_RowNumberFromCharOfs(editor, lParam);
1506 }
1507 case EM_LINEINDEX:
1508 {
1509 ME_DisplayItem *item, *para;
1510 int nCharOfs;
1511
1512 if (wParam == -1)
1513 item = ME_FindItemBack(editor->pCursors[0].pRun, diStartRow);
1514 else
1515 item = ME_FindRowWithNumber(editor, wParam);
1516 if (!item)
1517 return -1;
1518 para = ME_GetParagraph(item);
1519 item = ME_FindItemFwd(item, diRun);
1520 nCharOfs = para->member.para.nCharOfs + item->member.run.nCharOfs;
1521 TRACE("EM_LINEINDEX: nCharOfs==%d\n", nCharOfs);
1522 return nCharOfs;
1523 }
1524 case EM_LINELENGTH:
1525 {
1526 ME_DisplayItem *item, *item_end;
1527 int nChars = 0;
1528
1529 if (wParam > ME_GetTextLength(editor))
1530 return 0;
1531 if (wParam == -1)
1532 {
1533 FIXME("EM_LINELENGTH: returning number of unselected characters on lines with selection unsupported.\n");
1534 return 0;
1535 }
1536 item = ME_FindItemAtOffset(editor, diRun, wParam, NULL);
1537 item = ME_RowStart(item);
1538 item_end = ME_RowEnd(item);
1539 if (!item_end)
1540 {
1541 /* Empty buffer, no runs */
1542 nChars = 0;
1543 }
1544 else
1545 {
1546 nChars = ME_CharOfsFromRunOfs(editor, item_end, ME_StrLen(item_end->member.run.strText));
1547 nChars -= ME_CharOfsFromRunOfs(editor, item, 0);
1548 }
1549 TRACE("EM_LINELENGTH(%d)==%d\n",wParam, nChars);
1550 return nChars;
1551 }
1552 case EM_FINDTEXT:
1553 {
1554 FINDTEXTA *ft = (FINDTEXTA *)lParam;
1555 int nChars = MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, NULL, 0);
1556 WCHAR *tmp;
1557
1558 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
1559 MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, tmp, nChars);
1560 return ME_FindText(editor, wParam, &ft->chrg, tmp, NULL);
1561 }
1562 case EM_FINDTEXTEX:
1563 {
1564 FINDTEXTEXA *ex = (FINDTEXTEXA *)lParam;
1565 int nChars = MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, NULL, 0);
1566 WCHAR *tmp;
1567
1568 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
1569 MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, tmp, nChars);
1570 return ME_FindText(editor, wParam, &ex->chrg, tmp, &ex->chrgText);
1571 }
1572 case EM_FINDTEXTW:
1573 {
1574 FINDTEXTW *ft = (FINDTEXTW *)lParam;
1575 return ME_FindText(editor, wParam, &ft->chrg, ft->lpstrText, NULL);
1576 }
1577 case EM_FINDTEXTEXW:
1578 {
1579 FINDTEXTEXW *ex = (FINDTEXTEXW *)lParam;
1580 return ME_FindText(editor, wParam, &ex->chrg, ex->lpstrText, &ex->chrgText);
1581 }
1582 case EM_GETZOOM:
1583 if (!wParam || !lParam)
1584 return FALSE;
1585 *(int *)wParam = editor->nZoomNumerator;
1586 *(int *)lParam = editor->nZoomDenominator;
1587 return TRUE;
1588 case EM_SETZOOM:
1589 return ME_SetZoom(editor, wParam, lParam);
1590 case WM_CREATE:
1591 ME_CommitUndo(editor);
1592 ME_WrapMarkedParagraphs(editor);
1593 ME_MoveCaret(editor);
1594 return 0;
1595 case WM_DESTROY:
1596 ME_DestroyEditor(editor);
1597 SetWindowLongW(hWnd, 0, 0);
1598 return 0;
1599 case WM_LBUTTONDOWN:
1600 SetFocus(hWnd);
1601 ME_LButtonDown(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
1602 SetCapture(hWnd);
1603 break;
1604 case WM_MOUSEMOVE:
1605 if (GetCapture() == hWnd)
1606 ME_MouseMove(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
1607 break;
1608 case WM_LBUTTONUP:
1609 if (GetCapture() == hWnd)
1610 ReleaseCapture();
1611 break;
1612 case WM_PAINT:
1613 hDC = BeginPaint(hWnd, &ps);
1614 ME_PaintContent(editor, hDC, FALSE, &ps.rcPaint);
1615 EndPaint(hWnd, &ps);
1616 break;
1617 case WM_SETFOCUS:
1618 ME_ShowCaret(editor);
1619 ME_SendOldNotify(editor, EN_SETFOCUS);
1620 return 0;
1621 case WM_KILLFOCUS:
1622 ME_HideCaret(editor);
1623 ME_SendOldNotify(editor, EN_KILLFOCUS);
1624 return 0;
1625 case WM_ERASEBKGND:
1626 {
1627 HDC hDC = (HDC)wParam;
1628 RECT rc;
1629 COLORREF rgbBG = ME_GetBackColor(editor);
1630 if (GetUpdateRect(hWnd,&rc,TRUE))
1631 {
1632 HBRUSH hbr = CreateSolidBrush(rgbBG);
1633 FillRect(hDC, &rc, hbr);
1634 DeleteObject(hbr);
1635 }
1636 return 1;
1637 }
1638 case WM_COMMAND:
1639 TRACE("editor wnd command = %d\n", LOWORD(wParam));
1640 return 0;
1641 case WM_KEYDOWN:
1642 if (ME_ArrowKey(editor, LOWORD(wParam), GetKeyState(VK_CONTROL)<0)) {
1643 ME_CommitUndo(editor);
1644 ME_EnsureVisible(editor, editor->pCursors[0].pRun);
1645 HideCaret(hWnd);
1646 ME_MoveCaret(editor);
1647 ShowCaret(hWnd);
1648 return 0;
1649 }
1650 if (GetKeyState(VK_CONTROL)<0)
1651 {
1652 if (LOWORD(wParam)=='W')
1653 {
1654 CHARFORMAT2W chf;
1655 char buf[2048];
1656 ME_GetSelectionCharFormat(editor, &chf);
1657 ME_DumpStyleToBuf(&chf, buf);
1658 MessageBoxA(NULL, buf, "Style dump", MB_OK);
1659 }
1660 if (LOWORD(wParam)=='Q')
1661 {
1662 ME_CheckCharOffsets(editor);
1663 }
1664 }
1665 goto do_default;
1666 case WM_CHAR:
1667 {
1668 WCHAR wstr;
1669 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY) {
1670 MessageBeep(MB_ICONERROR);
1671 return 0; /* FIXME really 0 ? */
1672 }
1673 wstr = LOWORD(wParam);
1674 if (((unsigned)wstr)>=' ' || wstr=='\r' || wstr=='\t') {
1675 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
1676 ME_Style *style = ME_GetInsertStyle(editor, 0);
1677 ME_SaveTempStyle(editor);
1678 ME_InsertTextFromCursor(editor, 0, &wstr, 1, style);
1679 ME_ReleaseStyle(style);
1680 ME_CommitUndo(editor);
1681 ME_UpdateRepaint(editor);
1682 }
1683 return 0;
1684 }
1685 case WM_VSCROLL:
1686 {
1687 int nPos = editor->nScrollPosY;
1688 si.cbSize = sizeof(SCROLLINFO);
1689 si.fMask = SIF_PAGE|SIF_POS|SIF_RANGE|SIF_TRACKPOS;
1690 GetScrollInfo(hWnd, SB_VERT, &si);
1691 switch(LOWORD(wParam)) {
1692 case SB_LINEUP:
1693 nPos -= 24; /* FIXME follow the original */
1694 if (nPos<0) nPos = 0;
1695 break;
1696 case SB_LINEDOWN:
1697 {
1698 int nEnd = editor->nTotalLength - editor->sizeWindow.cy;
1699 nPos += 24; /* FIXME follow the original */
1700 if (nPos>=nEnd) nPos = nEnd;
1701 break;
1702 }
1703 case SB_PAGEUP:
1704 nPos -= editor->sizeWindow.cy;
1705 if (nPos<0) nPos = 0;
1706 break;
1707 case SB_PAGEDOWN:
1708 nPos += editor->sizeWindow.cy;
1709 if (nPos>=editor->nTotalLength) nPos = editor->nTotalLength-1;
1710 break;
1711 case SB_THUMBTRACK:
1712 case SB_THUMBPOSITION:
1713 nPos = si.nTrackPos;
1714 break;
1715 }
1716 if (nPos != editor->nScrollPosY) {
1717 ScrollWindow(hWnd, 0, editor->nScrollPosY-nPos, NULL, NULL);
1718 editor->nScrollPosY = nPos;
1719 SetScrollPos(hWnd, SB_VERT, nPos, TRUE);
1720 UpdateWindow(hWnd);
1721 }
1722 break;
1723 }
1724 case WM_MOUSEWHEEL:
1725 {
1726 int gcWheelDelta = 0, nPos = editor->nScrollPosY, nEnd = editor->nTotalLength - editor->sizeWindow.cy;
1727 UINT pulScrollLines;
1728 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
1729 gcWheelDelta -= GET_WHEEL_DELTA_WPARAM(wParam);
1730 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
1731 nPos += pulScrollLines * (gcWheelDelta / WHEEL_DELTA) * 8; /* FIXME follow the original */
1732 if (nPos>=nEnd)
1733 nPos = nEnd;
1734 if (nPos<0)
1735 nPos = 0;
1736 if (nPos != editor->nScrollPosY) {
1737 ScrollWindow(hWnd, 0, editor->nScrollPosY-nPos, NULL, NULL);
1738 editor->nScrollPosY = nPos;
1739 SetScrollPos(hWnd, SB_VERT, nPos, TRUE);
1740 UpdateWindow(hWnd);
1741 }
1742 break;
1743 }
1744 case WM_SIZE:
1745 {
1746 ME_RewrapRepaint(editor);
1747 return DefWindowProcW(hWnd, msg, wParam, lParam);
1748 }
1749 case EM_GETOLEINTERFACE:
1750 {
1751 LPVOID *ppvObj = (LPVOID*) lParam;
1752 FIXME("EM_GETOLEINTERFACE %p: stub\n", ppvObj);
1753 return CreateIRichEditOle(ppvObj);
1754 }
1755 default:
1756 do_default:
1757 return DefWindowProcW(hWnd, msg, wParam, lParam);
1758 }
1759 return 0L;
1760 }
1761
1762
1763 /******************************************************************
1764 * RichEdit10ANSIWndProc (RICHED20.9)
1765 */
1766 LRESULT WINAPI RichEdit10ANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
1767 {
1768 LRESULT result;
1769
1770 /* FIXME: this is NOT the same as 2.0 version */
1771 result = RichEditANSIWndProc(hWnd, msg, wParam, lParam);
1772 if (msg == WM_NCCREATE)
1773 {
1774 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongW(hWnd, 0);
1775
1776 editor->bEmulateVersion10 = TRUE;
1777 editor->pBuffer->pLast->member.para.nCharOfs = 2;
1778 }
1779 return result;
1780 }
1781
1782 void ME_SendOldNotify(ME_TextEditor *editor, int nCode)
1783 {
1784 HWND hWnd = editor->hWnd;
1785 SendMessageA(GetParent(hWnd), WM_COMMAND, (nCode<<16)|GetWindowLongW(hWnd, GWLP_ID), (LPARAM)hWnd);
1786 }
1787
1788 int ME_CountParagraphsBetween(ME_TextEditor *editor, int from, int to)
1789 {
1790 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
1791 int i = 0;
1792
1793 while(item && item->member.para.next_para->member.para.nCharOfs <= from)
1794 item = item->member.para.next_para;
1795 if (!item)
1796 return 0;
1797 while(item && item->member.para.next_para->member.para.nCharOfs <= to) {
1798 item = item->member.para.next_para;
1799 i++;
1800 }
1801 return i;
1802 }
1803
1804
1805 int ME_GetTextW(ME_TextEditor *editor, WCHAR *buffer, int nStart, int nChars, int bCRLF)
1806 {
1807 ME_DisplayItem *item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
1808 int nWritten = 0;
1809
1810 if (!item) {
1811 *buffer = L'\0';
1812 return 0;
1813 }
1814 assert(item);
1815
1816 if (nStart)
1817 {
1818 int nLen = ME_StrLen(item->member.run.strText) - nStart;
1819 if (nLen > nChars)
1820 nLen = nChars;
1821 CopyMemory(buffer, item->member.run.strText->szData + nStart, sizeof(WCHAR)*nLen);
1822 nChars -= nLen;
1823 nWritten += nLen;
1824 if (!nChars)
1825 return nWritten;
1826 buffer += nLen;
1827 nStart = 0;
1828 item = ME_FindItemFwd(item, diRun);
1829 }
1830
1831 while(nChars && item)
1832 {
1833 int nLen = ME_StrLen(item->member.run.strText);
1834 if (nLen > nChars)
1835 nLen = nChars;
1836
1837 if (item->member.run.nFlags & MERF_ENDPARA)
1838 {
1839 if (bCRLF) {
1840 *buffer++ = '\r';
1841 nWritten++;
1842 }
1843 *buffer = '\n';
1844 assert(nLen == 1);
1845 }
1846 else
1847 CopyMemory(buffer, item->member.run.strText->szData, sizeof(WCHAR)*nLen);
1848 nChars -= nLen;
1849 nWritten += nLen;
1850 buffer += nLen;
1851
1852 if (!nChars)
1853 {
1854 *buffer = L'\0';
1855 return nWritten;
1856 }
1857 item = ME_FindItemFwd(item, diRun);
1858 }
1859 *buffer = L'\0';
1860 return nWritten;
1861 }
1862
1863 void ME_RegisterEditorClass(HINSTANCE hInstance)
1864 {
1865 BOOL bResult;
1866 WNDCLASSW wcW;
1867 WNDCLASSA wcA;
1868
1869 wcW.style = CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
1870 wcW.lpfnWndProc = RichEditANSIWndProc;
1871 wcW.cbClsExtra = 0;
1872 wcW.cbWndExtra = 4;
1873 wcW.hInstance = NULL; /* hInstance would register DLL-local class */
1874 wcW.hIcon = NULL;
1875 wcW.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
1876 wcW.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
1877 wcW.lpszMenuName = NULL;
1878 wcW.lpszClassName = wszClassName;
1879 bResult = RegisterClassW(&wcW);
1880 assert(bResult);
1881 wcW.lpszClassName = wszClassName50;
1882 bResult = RegisterClassW(&wcW);
1883 assert(bResult);
1884
1885 wcA.style = CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
1886 wcA.lpfnWndProc = RichEditANSIWndProc;
1887 wcA.cbClsExtra = 0;
1888 wcA.cbWndExtra = 4;
1889 wcA.hInstance = NULL; /* hInstance would register DLL-local class */
1890 wcA.hIcon = NULL;
1891 wcA.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
1892 wcA.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
1893 wcA.lpszMenuName = NULL;
1894 wcA.lpszClassName = "RichEdit20A";
1895 bResult = RegisterClassA(&wcA);
1896 assert(bResult);
1897 wcA.lpszClassName = "RichEdit50A";
1898 bResult = RegisterClassA(&wcA);
1899 assert(bResult);
1900 }
1901 /******************************************************************
1902 * CreateTextServices (RICHED20.4)
1903 *
1904 * FIXME should be ITextHost instead of void*
1905 */
1906 HRESULT WINAPI CreateTextServices(IUnknown *punkOuter, void *pITextHost,
1907 IUnknown **ppUnk)
1908 {
1909 FIXME("stub\n");
1910 /* FIXME should support aggregation */
1911 if (punkOuter)
1912 return CLASS_E_NOAGGREGATION;
1913
1914 return E_FAIL; /* E_NOTIMPL isn't allowed by MSDN */
1915 }
1916
1917 /******************************************************************
1918 * REExtendedRegisterClass (RICHED20.8)
1919 *
1920 * FIXME undocumented
1921 */
1922 void WINAPI REExtendedRegisterClass(void)
1923 {
1924 FIXME("stub\n");
1925 }