Sync to Wine-0_9_3:
[reactos.git] / reactos / lib / riched20 / editor.c
1 /*
2 * RichEdit - functions dealing with editor object
3 *
4 * Copyright 2004 by Krzysztof Foltman
5 * Copyright 2005 by Cihan Altinay
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 /*
23 API implementation status:
24
25 Messages (ANSI versions not done yet)
26 - EM_AUTOURLDETECT 2.0
27 + EM_CANPASTE
28 + EM_CANREDO 2.0
29 + EM_CANUNDO
30 + EM_CHARFROMPOS
31 - EM_DISPLAYBAND
32 + EM_EMPTYUNDOBUFFER
33 + EM_EXGETSEL
34 - EM_EXLIMITTEXT
35 + EM_EXLINEFROMCHAR
36 + EM_EXSETSEL
37 + EM_FINDTEXT (only FR_DOWN flag implemented)
38 + EM_FINDTEXTEX (only FR_DOWN flag implemented)
39 - EM_FINDWORDBREAK
40 - EM_FMTLINES
41 - EM_FORMATRANGE
42 - EM_GETAUTOURLDETECT 2.0
43 - EM_GETBIDIOPTIONS 3.0
44 - EM_GETCHARFORMAT (partly done)
45 - EM_GETEDITSTYLE
46 + EM_GETEVENTMASK
47 + EM_GETFIRSTVISIBLELINE (can be optimized if needed)
48 - EM_GETIMECOLOR 1.0asian
49 - EM_GETIMECOMPMODE 2.0
50 - EM_GETIMEOPTIONS 1.0asian
51 - EM_GETIMESTATUS
52 - EM_GETLANGOPTIONS 2.0
53 - EM_GETLIMITTEXT
54 - EM_GETLINE
55 + EM_GETLINECOUNT returns number of rows, not of paragraphs
56 + EM_GETMODIFY
57 - EM_GETOLEINTERFACE
58 - EM_GETOPTIONS
59 + EM_GETPARAFORMAT
60 - EM_GETPASSWORDCHAR 2.0
61 - EM_GETPUNCTUATION 1.0asian
62 + EM_GETRECT
63 - EM_GETREDONAME 2.0
64 + EM_GETSEL
65 + EM_GETSELTEXT (ANSI&Unicode)
66 - EM_GETSCROLLPOS 3.0
67 ! - EM_GETTHUMB
68 - EM_GETTEXTEX 2.0
69 + EM_GETTEXTLENGTHEX (GTL_PRECISE unimplemented)
70 - EM_GETTEXTMODE 2.0
71 ? + EM_GETTEXTRANGE (ANSI&Unicode)
72 - EM_GETTYPOGRAPHYOPTIONS 3.0
73 - EM_GETUNDONAME
74 - EM_GETWORDBREAKPROC
75 - EM_GETWORDBREAKPROCEX
76 - EM_GETWORDWRAPMODE 1.0asian
77 + EM_GETZOOM 3.0
78 - EM_HIDESELECTION
79 - EM_LIMITTEXT
80 + EM_LINEFROMCHAR
81 + EM_LINEINDEX
82 + EM_LINELENGTH
83 + EM_LINESCROLL
84 - EM_PASTESPECIAL
85 + EM_POSFROMCHAR
86 + EM_REDO 2.0
87 + EM_REQUESTRESIZE
88 + EM_REPLACESEL (proper style?) ANSI&Unicode
89 - EM_SCROLL
90 - EM_SCROLLCARET
91 - EM_SELECTIONTYPE
92 - EM_SETBIDIOPTIONS 3.0
93 + EM_SETBKGNDCOLOR
94 - EM_SETCHARFORMAT (partly done, no ANSI)
95 - EM_SETEDITSTYLE
96 + EM_SETEVENTMASK (few notifications supported)
97 - EM_SETFONTSIZE
98 - EM_SETIMECOLOR 1.0asian
99 - EM_SETIMEOPTIONS 1.0asian
100 - EM_SETLANGOPTIONS 2.0
101 - EM_SETLIMITTEXT
102 + EM_SETMODIFY (not sure if implementation is correct)
103 - EM_SETOLECALLBACK
104 - EM_SETOPTIONS
105 - EM_SETPALETTE 2.0
106 + EM_SETPARAFORMAT
107 - EM_SETPASSWORDCHAR 2.0
108 - EM_SETPUNCTUATION 1.0asian
109 + EM_SETREADONLY no beep on modification attempt
110 + EM_SETRECT
111 + EM_SETRECTNP (EM_SETRECT without repainting)
112 + EM_SETSEL
113 - EM_SETSCROLLPOS 3.0
114 - EM_SETTABSTOPS 3.0
115 - EM_SETTARGETDEVICE
116 + EM_SETTEXTEX 3.0 (unicode only, no rich text insertion handling, proper style?)
117 - EM_SETTEXTMODE 2.0
118 - EM_SETTYPOGRAPHYOPTIONS 3.0
119 - EM_SETUNDOLIMIT 2.0
120 - EM_SETWORDBREAKPROC
121 - EM_SETWORDBREAKPROCEX
122 - EM_SETWORDWRAPMODE 1.0asian
123 + EM_SETZOOM 3.0
124 - EM_SHOWSCROLLBAR 2.0
125 - EM_STOPGROUPTYPING 2.0
126 + EM_STREAMIN
127 + EM_STREAMOUT
128 + EM_UNDO
129 + WM_CHAR
130 + WM_CLEAR
131 + WM_COPY
132 + WM_CUT
133 + WM_GETDLGCODE (the current implementation is incomplete)
134 + WM_GETTEXT (ANSI&Unicode)
135 + WM_GETTEXTLENGTH (ANSI version sucks)
136 + WM_PASTE
137 - WM_SETFONT
138 + WM_SETTEXT (resets undo stack !) (proper style?) ANSI&Unicode
139 - WM_STYLECHANGING
140 - WM_STYLECHANGED (things like read-only flag)
141 - WM_UNICHAR
142
143 Notifications
144
145 * EN_CHANGE (sent from the wrong place)
146 - EN_CORRECTTEXT
147 - EN_DROPFILES
148 - EN_ERRSPACE
149 - EN_HSCROLL
150 - EN_IMECHANGE
151 + EN_KILLFOCUS
152 - EN_LINK
153 - EN_MAXTEXT
154 - EN_MSGFILTER
155 - EN_OLEOPFAILED
156 - EN_PROTECTED
157 + EN_REQUESTRESIZE
158 - EN_SAVECLIPBOARD
159 + EN_SELCHANGE
160 + EN_SETFOCUS
161 - EN_STOPNOUNDO
162 * EN_UPDATE (sent from the wrong place)
163 - EN_VSCROLL
164
165 Styles
166
167 - ES_AUTOHSCROLL
168 - ES_AUTOVSCROLL
169 - ES_CENTER
170 - ES_DISABLENOSCROLL (scrollbar is always visible)
171 - ES_EX_NOCALLOLEINIT
172 - ES_LEFT
173 - ES_MULTILINE (currently single line controls aren't supported)
174 - ES_NOIME
175 - ES_READONLY (I'm not sure if beeping is the proper behaviour)
176 - ES_RIGHT
177 - ES_SAVESEL
178 - ES_SELFIME
179 - ES_SUNKEN
180 - ES_VERTICAL
181 - ES_WANTRETURN (don't know how to do WM_GETDLGCODE part)
182 - WS_SETFONT
183 - WS_HSCROLL
184 - WS_VSCROLL
185 */
186
187 /*
188 * RICHED20 TODO (incomplete):
189 *
190 * - messages/styles/notifications listed above
191 * - Undo coalescing
192 * - add remaining CHARFORMAT/PARAFORMAT fields
193 * - right/center align should strip spaces from the beginning
194 * - more advanced navigation (Ctrl-arrows)
195 * - tabs
196 * - pictures/OLE objects (not just smiling faces that lack API support ;-) )
197 * - COM interface (looks like a major pain in the TODO list)
198 * - calculate heights of pictures (half-done)
199 * - horizontal scrolling (not even started)
200 * - hysteresis during wrapping (related to scrollbars appearing/disappearing)
201 * - find/replace
202 * - how to implement EM_FORMATRANGE and EM_DISPLAYBAND ? (Mission Impossible)
203 * - italic caret with italic fonts
204 * - IME
205 * - most notifications aren't sent at all (the most important ones are)
206 * - when should EN_SELCHANGE be sent after text change ? (before/after EN_UPDATE?)
207 * - WM_SETTEXT may use wrong style (but I'm 80% sure it's OK)
208 * - EM_GETCHARFORMAT with SCF_SELECTION may not behave 100% like in original (but very close)
209 * - full justification
210 * - hyphenation
211 * - tables
212 * - ListBox & ComboBox not implemented
213 *
214 * Bugs that are probably fixed, but not so easy to verify:
215 * - EN_UPDATE/EN_CHANGE are handled very incorrectly (should be OK now)
216 * - undo for ME_JoinParagraphs doesn't store paragraph format ? (it does)
217 * - check/fix artificial EOL logic (bCursorAtEnd, hardly logical)
218 * - caret shouldn't be displayed when selection isn't empty
219 * - check refcounting in style management functions (looks perfect now, but no bugs is suspicious)
220 * - undo for setting default format (done, might be buggy)
221 * - styles might be not released properly (looks like they work like charm, but who knows?
222 *
223 */
224
225 #include "editor.h"
226 #include "commdlg.h"
227 #include "ole2.h"
228 #include "richole.h"
229 #include "winreg.h"
230 #define NO_SHLWAPI_STREAM
231 #include "shlwapi.h"
232
233 #include "rtf.h"
234
235 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
236
237 int me_debug = 0;
238 HANDLE me_heap = NULL;
239
240 static BOOL ME_ListBoxRegistered = FALSE;
241 static BOOL ME_ComboBoxRegistered = FALSE;
242
243 static ME_TextBuffer *ME_MakeText(void) {
244
245 ME_TextBuffer *buf = ALLOC_OBJ(ME_TextBuffer);
246
247 ME_DisplayItem *p1 = ME_MakeDI(diTextStart);
248 ME_DisplayItem *p2 = ME_MakeDI(diTextEnd);
249
250 p1->prev = NULL;
251 p1->next = p2;
252 p2->prev = p1;
253 p2->next = NULL;
254 p1->member.para.next_para = p2;
255 p2->member.para.prev_para = p1;
256 p2->member.para.nCharOfs = 0;
257
258 buf->pFirst = p1;
259 buf->pLast = p2;
260 buf->pCharStyle = NULL;
261
262 return buf;
263 }
264
265
266 static LRESULT ME_StreamInText(ME_TextEditor *editor, DWORD dwFormat, ME_InStream *stream, ME_Style *style)
267 {
268 WCHAR wszText[STREAMIN_BUFFER_SIZE+1];
269 WCHAR *pText;
270
271 TRACE("%08lx %p\n", dwFormat, stream);
272
273 do {
274 long nWideChars = 0;
275
276 if (!stream->dwSize)
277 {
278 ME_StreamInFill(stream);
279 if (stream->editstream->dwError)
280 break;
281 if (!stream->dwSize)
282 break;
283 }
284
285 if (!(dwFormat & SF_UNICODE))
286 {
287 /* FIXME? this is doomed to fail on true MBCS like UTF-8, luckily they're unlikely to be used as CP_ACP */
288 nWideChars = MultiByteToWideChar(CP_ACP, 0, stream->buffer, stream->dwSize, wszText, STREAMIN_BUFFER_SIZE);
289 pText = wszText;
290 }
291 else
292 {
293 nWideChars = stream->dwSize >> 1;
294 pText = (WCHAR *)stream->buffer;
295 }
296
297 ME_InsertTextFromCursor(editor, 0, pText, nWideChars, style);
298 if (stream->dwSize == 0)
299 break;
300 stream->dwSize = 0;
301 } while(1);
302 ME_CommitUndo(editor);
303 ME_Repaint(editor);
304 return 0;
305 }
306
307 static void ME_RTFCharAttrHook(RTF_Info *info)
308 {
309 CHARFORMAT2W fmt;
310 fmt.cbSize = sizeof(fmt);
311 fmt.dwMask = 0;
312
313 switch(info->rtfMinor)
314 {
315 case rtfPlain:
316 /* FIXME add more flags once they're implemented */
317 fmt.dwMask = CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_COLOR | CFM_BACKCOLOR | CFM_SIZE | CFM_WEIGHT;
318 fmt.dwEffects = CFE_AUTOCOLOR | CFE_AUTOBACKCOLOR;
319 fmt.yHeight = 12*20; /* 12pt */
320 fmt.wWeight = 400;
321 break;
322 case rtfBold:
323 fmt.dwMask = CFM_BOLD;
324 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
325 break;
326 case rtfItalic:
327 fmt.dwMask = CFM_ITALIC;
328 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
329 break;
330 case rtfUnderline:
331 fmt.dwMask = CFM_UNDERLINE;
332 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
333 fmt.bUnderlineType = CFU_CF1UNDERLINE;
334 break;
335 case rtfNoUnderline:
336 fmt.dwMask = CFM_UNDERLINE;
337 fmt.dwEffects = 0;
338 break;
339 case rtfStrikeThru:
340 fmt.dwMask = CFM_STRIKEOUT;
341 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
342 break;
343 case rtfSubScript:
344 case rtfSuperScript:
345 case rtfSubScrShrink:
346 case rtfSuperScrShrink:
347 case rtfNoSuperSub:
348 fmt.dwMask = CFM_SUBSCRIPT|CFM_SUPERSCRIPT;
349 if (info->rtfMinor == rtfSubScrShrink) fmt.dwEffects = CFE_SUBSCRIPT;
350 if (info->rtfMinor == rtfSuperScrShrink) fmt.dwEffects = CFE_SUPERSCRIPT;
351 if (info->rtfMinor == rtfNoSuperSub) fmt.dwEffects = 0;
352 break;
353 case rtfBackColor:
354 fmt.dwMask = CFM_BACKCOLOR;
355 fmt.dwEffects = 0;
356 if (info->rtfParam == 0)
357 fmt.dwEffects = CFE_AUTOBACKCOLOR;
358 else if (info->rtfParam != rtfNoParam)
359 {
360 RTFColor *c = RTFGetColor(info, info->rtfParam);
361 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
362 }
363 break;
364 case rtfForeColor:
365 fmt.dwMask = CFM_COLOR;
366 fmt.dwEffects = 0;
367 if (info->rtfParam == 0)
368 fmt.dwEffects = CFE_AUTOCOLOR;
369 else if (info->rtfParam != rtfNoParam)
370 {
371 RTFColor *c = RTFGetColor(info, info->rtfParam);
372 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
373 }
374 break;
375 case rtfFontNum:
376 if (info->rtfParam != rtfNoParam)
377 {
378 RTFFont *f = RTFGetFont(info, info->rtfParam);
379 if (f)
380 {
381 MultiByteToWideChar(CP_ACP, 0, f->rtfFName, -1, fmt.szFaceName, sizeof(fmt.szFaceName)/sizeof(WCHAR));
382 fmt.szFaceName[sizeof(fmt.szFaceName)/sizeof(WCHAR)-1] = '\0';
383 fmt.bCharSet = f->rtfFCharSet;
384 fmt.dwMask = CFM_FACE | CFM_CHARSET;
385 }
386 }
387 break;
388 case rtfFontSize:
389 fmt.dwMask = CFM_SIZE;
390 if (info->rtfParam != rtfNoParam)
391 fmt.yHeight = info->rtfParam*10;
392 break;
393 }
394 if (fmt.dwMask) {
395 ME_Style *style2;
396 RTFFlushOutputBuffer(info);
397 /* FIXME too slow ? how come ? */
398 style2 = ME_ApplyStyle(info->style, &fmt);
399 ME_ReleaseStyle(info->style);
400 info->style = style2;
401 info->styleChanged = TRUE;
402 }
403 }
404
405 /* FIXME this function doesn't get any information about context of the RTF tag, which is very bad,
406 the same tags mean different things in different contexts */
407 static void ME_RTFParAttrHook(RTF_Info *info)
408 {
409 PARAFORMAT2 fmt;
410 fmt.cbSize = sizeof(fmt);
411 fmt.dwMask = 0;
412
413 switch(info->rtfMinor)
414 {
415 case rtfParDef: /* I'm not 100% sure what does it do, but I guess it restores default paragraph attributes */
416 fmt.dwMask = PFM_ALIGNMENT | PFM_TABSTOPS | PFM_OFFSET | PFM_STARTINDENT;
417 fmt.wAlignment = PFA_LEFT;
418 fmt.cTabCount = 0;
419 fmt.dxOffset = fmt.dxStartIndent = 0;
420 break;
421 case rtfFirstIndent:
422 ME_GetSelectionParaFormat(info->editor, &fmt);
423 fmt.dwMask = PFM_STARTINDENT;
424 fmt.dxStartIndent = info->rtfParam + fmt.dxOffset;
425 break;
426 case rtfLeftIndent:
427 {
428 int first, left;
429 ME_GetSelectionParaFormat(info->editor, &fmt);
430 first = fmt.dxStartIndent;
431 left = info->rtfParam;
432 fmt.dwMask = PFM_STARTINDENT|PFM_OFFSET;
433 fmt.dxStartIndent = first + left;
434 fmt.dxOffset = -first;
435 break;
436 }
437 case rtfRightIndent:
438 fmt.dwMask = PFM_RIGHTINDENT;
439 fmt.dxRightIndent = info->rtfParam;
440 break;
441 case rtfQuadLeft:
442 case rtfQuadJust:
443 fmt.dwMask = PFM_ALIGNMENT;
444 fmt.wAlignment = PFA_LEFT;
445 break;
446 case rtfQuadRight:
447 fmt.dwMask = PFM_ALIGNMENT;
448 fmt.wAlignment = PFA_RIGHT;
449 break;
450 case rtfQuadCenter:
451 fmt.dwMask = PFM_ALIGNMENT;
452 fmt.wAlignment = PFA_CENTER;
453 break;
454 case rtfTabPos:
455 ME_GetSelectionParaFormat(info->editor, &fmt);
456 if (!(fmt.dwMask & PFM_TABSTOPS))
457 {
458 fmt.dwMask |= PFM_TABSTOPS;
459 fmt.cTabCount = 0;
460 }
461 if (fmt.cTabCount < MAX_TAB_STOPS)
462 fmt.rgxTabs[fmt.cTabCount++] = info->rtfParam;
463 break;
464 }
465 if (fmt.dwMask) {
466 RTFFlushOutputBuffer(info);
467 /* FIXME too slow ? how come ?*/
468 ME_SetSelectionParaFormat(info->editor, &fmt);
469 }
470 }
471
472 static void ME_RTFReadHook(RTF_Info *info) {
473 switch(info->rtfClass)
474 {
475 case rtfGroup:
476 switch(info->rtfMajor)
477 {
478 case rtfBeginGroup:
479 if (info->stackTop < maxStack) {
480 memcpy(&info->stack[info->stackTop].fmt, &info->style->fmt, sizeof(CHARFORMAT2W));
481 info->stack[info->stackTop].codePage = info->codePage;
482 info->stack[info->stackTop].unicodeLength = info->unicodeLength;
483 }
484 info->stackTop++;
485 info->styleChanged = FALSE;
486 break;
487 case rtfEndGroup:
488 {
489 ME_Style *s;
490 RTFFlushOutputBuffer(info);
491 if (info->stackTop<=1) {
492 info->rtfClass = rtfEOF;
493 return;
494 }
495 info->stackTop--;
496 assert(info->stackTop >= 0);
497 if (info->styleChanged)
498 {
499 /* FIXME too slow ? how come ? */
500 s = ME_ApplyStyle(info->style, &info->stack[info->stackTop].fmt);
501 ME_ReleaseStyle(info->style);
502 info->style = s;
503 info->codePage = info->stack[info->stackTop].codePage;
504 info->unicodeLength = info->stack[info->stackTop].unicodeLength;
505 }
506 break;
507 }
508 }
509 break;
510 case rtfControl:
511 switch(info->rtfMajor)
512 {
513 case rtfCharAttr:
514 ME_RTFCharAttrHook(info);
515 break;
516 case rtfParAttr:
517 ME_RTFParAttrHook(info);
518 break;
519 }
520 break;
521 }
522 }
523
524 void
525 ME_StreamInFill(ME_InStream *stream)
526 {
527 stream->editstream->dwError = stream->editstream->pfnCallback(stream->editstream->dwCookie,
528 (BYTE *)stream->buffer,
529 sizeof(stream->buffer),
530 (LONG *)&stream->dwSize);
531 stream->dwUsed = 0;
532 }
533
534 static LRESULT ME_StreamIn(ME_TextEditor *editor, DWORD format, EDITSTREAM *stream)
535 {
536 RTF_Info parser;
537 ME_Style *style;
538 int from, to, to2, nUndoMode;
539 ME_UndoItem *pUI;
540 int nEventMask = editor->nEventMask;
541 ME_InStream inStream;
542
543 TRACE("stream==%p hWnd==%p format==0x%X\n", stream, editor->hWnd, (UINT)format);
544 editor->nEventMask = 0;
545
546 ME_GetSelection(editor, &from, &to);
547 if (format & SFF_SELECTION) {
548 style = ME_GetSelectionInsertStyle(editor);
549
550 ME_InternalDeleteText(editor, from, to-from);
551 }
552 else {
553 style = editor->pBuffer->pDefaultStyle;
554 ME_AddRefStyle(style);
555 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
556 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
557 from = to = 0;
558 ME_ClearTempStyle(editor);
559 /* FIXME restore default paragraph formatting ! */
560 }
561
562 nUndoMode = editor->nUndoMode;
563 editor->nUndoMode = umIgnore;
564
565 inStream.editstream = stream;
566 inStream.editstream->dwError = 0;
567 inStream.dwSize = 0;
568 inStream.dwUsed = 0;
569
570 if (format & SF_RTF)
571 {
572 /* Check if it's really RTF, and if it is not, use plain text */
573 ME_StreamInFill(&inStream);
574 if (!inStream.editstream->dwError)
575 {
576 if (strncmp(inStream.buffer, "{\\rtf1", 6) && strncmp(inStream.buffer, "{\\urtf", 6))
577 {
578 format &= ~SF_RTF;
579 format |= SF_TEXT;
580 }
581 }
582 }
583
584 if (!inStream.editstream->dwError)
585 {
586 if (format & SF_RTF) {
587 /* setup the RTF parser */
588 memset(&parser, 0, sizeof parser);
589 RTFSetEditStream(&parser, &inStream);
590 parser.rtfFormat = format&(SF_TEXT|SF_RTF);
591 parser.hwndEdit = editor->hWnd;
592 parser.editor = editor;
593 parser.style = style;
594 WriterInit(&parser);
595 RTFInit(&parser);
596 RTFSetReadHook(&parser, ME_RTFReadHook);
597 BeginFile(&parser);
598
599 /* do the parsing */
600 RTFRead(&parser);
601 RTFFlushOutputBuffer(&parser);
602 RTFDestroy(&parser);
603
604 style = parser.style;
605 }
606 else if (format & SF_TEXT)
607 ME_StreamInText(editor, format, &inStream, style);
608 else
609 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
610 ME_GetSelection(editor, &to, &to2);
611 /* put the cursor at the top */
612 if (!(format & SFF_SELECTION))
613 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
614 else
615 {
616 /* FIXME where to put cursor now ? */
617 }
618 }
619
620 editor->nUndoMode = nUndoMode;
621 pUI = ME_AddUndoItem(editor, diUndoDeleteRun, NULL);
622 TRACE("from %d to %d\n", from, to);
623 if (pUI && from < to)
624 {
625 pUI->nStart = from;
626 pUI->nLen = to-from;
627 }
628 ME_CommitUndo(editor);
629 ME_ReleaseStyle(style);
630 editor->nEventMask = nEventMask;
631 if (editor->bRedraw)
632 {
633 InvalidateRect(editor->hWnd, NULL, TRUE);
634 ME_UpdateRepaint(editor);
635 }
636 if (!(format & SFF_SELECTION)) {
637 ME_ClearTempStyle(editor);
638 }
639 ME_MoveCaret(editor);
640 ME_SendSelChange(editor);
641 ME_SendRequestResize(editor, FALSE);
642
643 return 0;
644 }
645
646
647 ME_DisplayItem *
648 ME_FindItemAtOffset(ME_TextEditor *editor, ME_DIType nItemType, int nOffset, int *nItemOffset)
649 {
650 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
651
652 while (item && item->member.para.next_para->member.para.nCharOfs <= nOffset)
653 item = ME_FindItemFwd(item, diParagraph);
654
655 if (!item)
656 return item;
657
658 nOffset -= item->member.para.nCharOfs;
659 if (nItemType == diParagraph) {
660 if (nItemOffset)
661 *nItemOffset = nOffset;
662 return item;
663 }
664
665 do {
666 item = ME_FindItemFwd(item, diRun);
667 } while (item && (item->member.run.nCharOfs + ME_StrLen(item->member.run.strText) <= nOffset));
668 if (item) {
669 nOffset -= item->member.run.nCharOfs;
670 if (nItemOffset)
671 *nItemOffset = nOffset;
672 }
673 return item;
674 }
675
676
677 static int
678 ME_FindText(ME_TextEditor *editor, DWORD flags, CHARRANGE *chrg, WCHAR *text, CHARRANGE *chrgText)
679 {
680 int nStart, nEnd;
681 int nLen = lstrlenW(text);
682 int nMin, nMax;
683 ME_DisplayItem *item;
684 ME_DisplayItem *para;
685
686 TRACE("flags==0x%08lx, chrg->cpMin==%ld, chrg->cpMax==%ld text==%s\n",
687 flags, chrg->cpMin, chrg->cpMax, debugstr_w(text));
688
689 if (!(flags & FR_MATCHCASE))
690 FIXME("Case-insensitive search not implemented\n");
691 if (flags & ~(FR_DOWN | FR_MATCHCASE))
692 FIXME("Flags 0x%08lx not implemented\n", flags & ~(FR_DOWN | FR_MATCHCASE));
693
694 if (chrg->cpMax == -1)
695 {
696 nMin = chrg->cpMin;
697 nMax = ME_GetTextLength(editor);
698 }
699 else
700 {
701 nMin = min(chrg->cpMin, chrg->cpMax);
702 nMax = max(chrg->cpMin, chrg->cpMax);
703 }
704
705 if (!nLen)
706 {
707 if (chrgText)
708 chrgText->cpMin = chrgText->cpMax = ((flags & FR_DOWN) ? nMin : nMax);
709 return chrgText->cpMin;
710 }
711
712 if (flags & FR_DOWN) /* Forward search */
713 {
714 nStart = nMin;
715 item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
716 if (!item)
717 return -1;
718
719 para = ME_GetParagraph(item);
720 while (item
721 && para->member.para.nCharOfs + item->member.run.nCharOfs + nStart + nLen < nMax)
722 {
723 ME_DisplayItem *pCurItem = item;
724 int nCurStart = nStart;
725 int nMatched = 0;
726
727 while (pCurItem && pCurItem->member.run.strText->szData[nCurStart + nMatched] == text[nMatched])
728 {
729 nMatched++;
730 if (nMatched == nLen)
731 {
732 nStart += para->member.para.nCharOfs + item->member.run.nCharOfs;
733 if (chrgText)
734 {
735 chrgText->cpMin = nStart;
736 chrgText->cpMax = nStart + nLen;
737 }
738 TRACE("found at %d-%d\n", nStart, nStart + nLen);
739 return nStart;
740 }
741 if (nCurStart + nMatched == ME_StrLen(pCurItem->member.run.strText))
742 {
743 pCurItem = ME_FindItemFwd(pCurItem, diRun);
744 para = ME_GetParagraph(pCurItem);
745 nCurStart = -nMatched;
746 }
747 }
748 nStart++;
749 if (nStart == ME_StrLen(item->member.run.strText))
750 {
751 item = ME_FindItemFwd(item, diRun);
752 para = ME_GetParagraph(item);
753 nStart = 0;
754 }
755 }
756 }
757 else /* Backward search */
758 {
759 nEnd = nMax;
760 item = ME_FindItemAtOffset(editor, diRun, nEnd, &nEnd);
761 if (!item)
762 return -1;
763
764 para = ME_GetParagraph(item);
765
766 while (item
767 && para->member.para.nCharOfs + item->member.run.nCharOfs + nEnd - nLen >= nMin)
768 {
769 ME_DisplayItem *pCurItem = item;
770 int nCurEnd = nEnd;
771 int nMatched = 0;
772
773 while (pCurItem && pCurItem->member.run.strText->szData[nCurEnd - nMatched - 1] == text[nLen - nMatched - 1])
774 {
775 nMatched++;
776 if (nMatched == nLen)
777 {
778 nStart = para->member.para.nCharOfs + item->member.run.nCharOfs + nCurEnd - nMatched;
779 if (chrgText)
780 {
781 chrgText->cpMin = nStart;
782 chrgText->cpMax = nStart + nLen;
783 }
784 TRACE("found at %d-%d\n", nStart, nStart + nLen);
785 return nStart;
786 }
787 if (nCurEnd - nMatched == 0)
788 {
789 pCurItem = ME_FindItemBack(pCurItem, diRun);
790 para = ME_GetParagraph(pCurItem);
791 nCurEnd = ME_StrLen(pCurItem->member.run.strText) + nMatched;
792 }
793 }
794 nEnd--;
795 if (nEnd < 0)
796 {
797 item = ME_FindItemBack(item, diRun);
798 para = ME_GetParagraph(item);
799 nEnd = ME_StrLen(item->member.run.strText);
800 }
801 }
802 }
803 TRACE("not found\n");
804 return -1;
805 }
806
807
808 ME_TextEditor *ME_MakeEditor(HWND hWnd) {
809 ME_TextEditor *ed = ALLOC_OBJ(ME_TextEditor);
810 HDC hDC;
811 int i;
812 ed->hWnd = hWnd;
813 ed->bEmulateVersion10 = FALSE;
814 ed->pBuffer = ME_MakeText();
815 hDC = GetDC(hWnd);
816 ME_MakeFirstParagraph(hDC, ed->pBuffer);
817 ReleaseDC(hWnd, hDC);
818 ed->bCaretShown = FALSE;
819 ed->nCursors = 3;
820 ed->pCursors = ALLOC_N_OBJ(ME_Cursor, ed->nCursors);
821 ed->pCursors[0].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
822 ed->pCursors[0].nOffset = 0;
823 ed->pCursors[1].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
824 ed->pCursors[1].nOffset = 0;
825 ed->nLastTotalLength = ed->nTotalLength = 0;
826 ed->nUDArrowX = -1;
827 ed->nSequence = 0;
828 ed->rgbBackColor = -1;
829 ed->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
830 ed->bCaretAtEnd = FALSE;
831 ed->nEventMask = 0;
832 ed->nModifyStep = 0;
833 ed->pUndoStack = ed->pRedoStack = NULL;
834 ed->nUndoMode = umAddToUndo;
835 ed->nParagraphs = 1;
836 ed->nLastSelStart = ed->nLastSelEnd = 0;
837 ed->nScrollPosY = 0;
838 ed->nZoomNumerator = ed->nZoomDenominator = 0;
839 ed->bRedraw = TRUE;
840 GetClientRect(hWnd, &ed->rcFormat);
841 for (i=0; i<HFONT_CACHE_SIZE; i++)
842 {
843 ed->pFontCache[i].nRefs = 0;
844 ed->pFontCache[i].nAge = 0;
845 ed->pFontCache[i].hFont = NULL;
846 }
847 ME_CheckCharOffsets(ed);
848 return ed;
849 }
850
851 typedef struct tagME_GlobalDestStruct
852 {
853 HGLOBAL hData;
854 int nLength;
855 } ME_GlobalDestStruct;
856
857 static DWORD CALLBACK ME_AppendToHGLOBAL(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
858 {
859 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
860 int nMaxSize;
861 BYTE *pDest;
862
863 nMaxSize = GlobalSize(pData->hData);
864 if (pData->nLength+cb+1 >= cb)
865 {
866 /* round up to 2^17 */
867 int nNewSize = (((nMaxSize+cb+1)|0x1FFFF)+1) & 0xFFFE0000;
868 pData->hData = GlobalReAlloc(pData->hData, nNewSize, 0);
869 }
870 pDest = (BYTE *)GlobalLock(pData->hData);
871 memcpy(pDest + pData->nLength, lpBuff, cb);
872 pData->nLength += cb;
873 pDest[pData->nLength] = '\0';
874 GlobalUnlock(pData->hData);
875 *pcb = cb;
876
877 return 0;
878 }
879
880 static DWORD CALLBACK ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
881 {
882 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
883 int i;
884 WORD *pSrc, *pDest;
885
886 cb = cb >> 1;
887 pDest = (WORD *)lpBuff;
888 pSrc = (WORD *)GlobalLock(pData->hData);
889 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
890 pDest[i] = pSrc[pData->nLength+i];
891 }
892 pData->nLength += i;
893 *pcb = 2*i;
894 GlobalUnlock(pData->hData);
895 return 0;
896 }
897
898 static DWORD CALLBACK ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
899 {
900 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
901 int i;
902 BYTE *pSrc, *pDest;
903
904 pDest = lpBuff;
905 pSrc = (BYTE *)GlobalLock(pData->hData);
906 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
907 pDest[i] = pSrc[pData->nLength+i];
908 }
909 pData->nLength += i;
910 *pcb = i;
911 GlobalUnlock(pData->hData);
912 return 0;
913 }
914
915
916 void ME_DestroyEditor(ME_TextEditor *editor)
917 {
918 ME_DisplayItem *pFirst = editor->pBuffer->pFirst;
919 ME_DisplayItem *p = pFirst, *pNext = NULL;
920 int i;
921
922 ME_ClearTempStyle(editor);
923 ME_EmptyUndoStack(editor);
924 while(p) {
925 pNext = p->next;
926 ME_DestroyDisplayItem(p);
927 p = pNext;
928 }
929 ME_ReleaseStyle(editor->pBuffer->pDefaultStyle);
930 for (i=0; i<HFONT_CACHE_SIZE; i++)
931 {
932 if (editor->pFontCache[i].hFont)
933 DeleteObject(editor->pFontCache[i].hFont);
934 }
935 DeleteObject(editor->hbrBackground);
936
937 FREE_OBJ(editor);
938 }
939
940 static WCHAR wszClassName[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '2', '0', 'W', 0};
941 static WCHAR wszClassName50[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '5', '0', 'W', 0};
942 static WCHAR wszClassNameListBox[] = {'R','E','L','i','s','t','B','o','x','2','0','W', 0};
943 static WCHAR wszClassNameComboBox[] = {'R','E','C','o','m','b','o','B','o','x','2','0','W', 0};
944
945 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
946 {
947 TRACE("\n");
948 switch (fdwReason)
949 {
950 case DLL_PROCESS_ATTACH:
951 DisableThreadLibraryCalls(hinstDLL);
952 me_heap = HeapCreate (0, 0x10000, 0);
953 ME_RegisterEditorClass(hinstDLL);
954 break;
955
956 case DLL_PROCESS_DETACH:
957 UnregisterClassW(wszClassName, 0);
958 UnregisterClassW(wszClassName50, 0);
959 UnregisterClassA("RichEdit20A", 0);
960 UnregisterClassA("RichEdit50A", 0);
961 if (ME_ListBoxRegistered)
962 UnregisterClassW(wszClassNameListBox, 0);
963 if (ME_ComboBoxRegistered)
964 UnregisterClassW(wszClassNameComboBox, 0);
965 HeapDestroy (me_heap);
966 me_heap = NULL;
967 break;
968 }
969 return TRUE;
970 }
971
972
973 #define UNSUPPORTED_MSG(e) \
974 case e: \
975 FIXME(#e ": stub\n"); \
976 return DefWindowProcW(hWnd, msg, wParam, lParam);
977
978 static const char * const edit_messages[] = {
979 "EM_GETSEL",
980 "EM_SETSEL",
981 "EM_GETRECT",
982 "EM_SETRECT",
983 "EM_SETRECTNP",
984 "EM_SCROLL",
985 "EM_LINESCROLL",
986 "EM_SCROLLCARET",
987 "EM_GETMODIFY",
988 "EM_SETMODIFY",
989 "EM_GETLINECOUNT",
990 "EM_LINEINDEX",
991 "EM_SETHANDLE",
992 "EM_GETHANDLE",
993 "EM_GETTHUMB",
994 "EM_UNKNOWN_BF",
995 "EM_UNKNOWN_C0",
996 "EM_LINELENGTH",
997 "EM_REPLACESEL",
998 "EM_UNKNOWN_C3",
999 "EM_GETLINE",
1000 "EM_LIMITTEXT",
1001 "EM_CANUNDO",
1002 "EM_UNDO",
1003 "EM_FMTLINES",
1004 "EM_LINEFROMCHAR",
1005 "EM_UNKNOWN_CA",
1006 "EM_SETTABSTOPS",
1007 "EM_SETPASSWORDCHAR",
1008 "EM_EMPTYUNDOBUFFER",
1009 "EM_GETFIRSTVISIBLELINE",
1010 "EM_SETREADONLY",
1011 "EM_SETWORDBREAKPROC",
1012 "EM_GETWORDBREAKPROC",
1013 "EM_GETPASSWORDCHAR",
1014 "EM_SETMARGINS",
1015 "EM_GETMARGINS",
1016 "EM_GETLIMITTEXT",
1017 "EM_POSFROMCHAR",
1018 "EM_CHARFROMPOS"
1019 };
1020
1021 static const char * const richedit_messages[] = {
1022 "EM_CANPASTE",
1023 "EM_DISPLAYBAND",
1024 "EM_EXGETSEL",
1025 "EM_EXLIMITTEXT",
1026 "EM_EXLINEFROMCHAR",
1027 "EM_EXSETSEL",
1028 "EM_FINDTEXT",
1029 "EM_FORMATRANGE",
1030 "EM_GETCHARFORMAT",
1031 "EM_GETEVENTMASK",
1032 "EM_GETOLEINTERFACE",
1033 "EM_GETPARAFORMAT",
1034 "EM_GETSELTEXT",
1035 "EM_HIDESELECTION",
1036 "EM_PASTESPECIAL",
1037 "EM_REQUESTRESIZE",
1038 "EM_SELECTIONTYPE",
1039 "EM_SETBKGNDCOLOR",
1040 "EM_SETCHARFORMAT",
1041 "EM_SETEVENTMASK",
1042 "EM_SETOLECALLBACK",
1043 "EM_SETPARAFORMAT",
1044 "EM_SETTARGETDEVICE",
1045 "EM_STREAMIN",
1046 "EM_STREAMOUT",
1047 "EM_GETTEXTRANGE",
1048 "EM_FINDWORDBREAK",
1049 "EM_SETOPTIONS",
1050 "EM_GETOPTIONS",
1051 "EM_FINDTEXTEX",
1052 "EM_GETWORDBREAKPROCEX",
1053 "EM_SETWORDBREAKPROCEX",
1054 "EM_SETUNDOLIMIT",
1055 "EM_UNKNOWN_USER_83",
1056 "EM_REDO",
1057 "EM_CANREDO",
1058 "EM_GETUNDONAME",
1059 "EM_GETREDONAME",
1060 "EM_STOPGROUPTYPING",
1061 "EM_SETTEXTMODE",
1062 "EM_GETTEXTMODE",
1063 "EM_AUTOURLDETECT",
1064 "EM_GETAUTOURLDETECT",
1065 "EM_SETPALETTE",
1066 "EM_GETTEXTEX",
1067 "EM_GETTEXTLENGTHEX",
1068 "EM_SHOWSCROLLBAR",
1069 "EM_SETTEXTEX",
1070 "EM_UNKNOWN_USER_98",
1071 "EM_UNKNOWN_USER_99",
1072 "EM_SETPUNCTUATION",
1073 "EM_GETPUNCTUATION",
1074 "EM_SETWORDWRAPMODE",
1075 "EM_GETWORDWRAPMODE",
1076 "EM_SETIMECOLOR",
1077 "EM_GETIMECOLOR",
1078 "EM_SETIMEOPTIONS",
1079 "EM_GETIMEOPTIONS",
1080 "EM_CONVPOSITION",
1081 "EM_UNKNOWN_USER_109",
1082 "EM_UNKNOWN_USER_110",
1083 "EM_UNKNOWN_USER_111",
1084 "EM_UNKNOWN_USER_112",
1085 "EM_UNKNOWN_USER_113",
1086 "EM_UNKNOWN_USER_114",
1087 "EM_UNKNOWN_USER_115",
1088 "EM_UNKNOWN_USER_116",
1089 "EM_UNKNOWN_USER_117",
1090 "EM_UNKNOWN_USER_118",
1091 "EM_UNKNOWN_USER_119",
1092 "EM_SETLANGOPTIONS",
1093 "EM_GETLANGOPTIONS",
1094 "EM_GETIMECOMPMODE",
1095 "EM_FINDTEXTW",
1096 "EM_FINDTEXTEXW",
1097 "EM_RECONVERSION",
1098 "EM_SETIMEMODEBIAS",
1099 "EM_GETIMEMODEBIAS"
1100 };
1101
1102 static const char *
1103 get_msg_name(UINT msg)
1104 {
1105 if (msg >= EM_GETSEL && msg <= EM_SETLIMITTEXT)
1106 return edit_messages[msg - EM_GETSEL];
1107 if (msg >= EM_CANPASTE && msg <= EM_GETIMEMODEBIAS)
1108 return richedit_messages[msg - EM_CANPASTE];
1109 return "";
1110 }
1111
1112 /******************************************************************
1113 * RichEditANSIWndProc (RICHED20.10)
1114 */
1115 LRESULT WINAPI RichEditANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
1116 SCROLLINFO si;
1117 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongW(hWnd, 0);
1118
1119 TRACE("hWnd %p msg %04x (%s) %08x %08lx\n",
1120 hWnd, msg, get_msg_name(msg), wParam, lParam);
1121
1122 switch(msg) {
1123
1124 UNSUPPORTED_MSG(EM_AUTOURLDETECT)
1125 UNSUPPORTED_MSG(EM_DISPLAYBAND)
1126 UNSUPPORTED_MSG(EM_EXLIMITTEXT)
1127 UNSUPPORTED_MSG(EM_FINDWORDBREAK)
1128 UNSUPPORTED_MSG(EM_FMTLINES)
1129 UNSUPPORTED_MSG(EM_FORMATRANGE)
1130 UNSUPPORTED_MSG(EM_GETAUTOURLDETECT)
1131 UNSUPPORTED_MSG(EM_GETBIDIOPTIONS)
1132 UNSUPPORTED_MSG(EM_GETEDITSTYLE)
1133 UNSUPPORTED_MSG(EM_GETIMECOMPMODE)
1134 /* UNSUPPORTED_MSG(EM_GETIMESTATUS) missing in Wine headers */
1135 UNSUPPORTED_MSG(EM_GETLANGOPTIONS)
1136 UNSUPPORTED_MSG(EM_GETLIMITTEXT)
1137 UNSUPPORTED_MSG(EM_GETLINE)
1138 /* UNSUPPORTED_MSG(EM_GETOLEINTERFACE) separate stub */
1139 UNSUPPORTED_MSG(EM_GETOPTIONS)
1140 UNSUPPORTED_MSG(EM_GETPASSWORDCHAR)
1141 UNSUPPORTED_MSG(EM_GETREDONAME)
1142 UNSUPPORTED_MSG(EM_GETSCROLLPOS)
1143 UNSUPPORTED_MSG(EM_GETTEXTMODE)
1144 UNSUPPORTED_MSG(EM_GETTYPOGRAPHYOPTIONS)
1145 UNSUPPORTED_MSG(EM_GETUNDONAME)
1146 UNSUPPORTED_MSG(EM_GETWORDBREAKPROC)
1147 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX)
1148 UNSUPPORTED_MSG(EM_HIDESELECTION)
1149 UNSUPPORTED_MSG(EM_LIMITTEXT) /* also known as EM_SETLIMITTEXT */
1150 UNSUPPORTED_MSG(EM_PASTESPECIAL)
1151 UNSUPPORTED_MSG(EM_SCROLL)
1152 UNSUPPORTED_MSG(EM_SCROLLCARET)
1153 UNSUPPORTED_MSG(EM_SELECTIONTYPE)
1154 UNSUPPORTED_MSG(EM_SETBIDIOPTIONS)
1155 UNSUPPORTED_MSG(EM_SETEDITSTYLE)
1156 UNSUPPORTED_MSG(EM_SETFONTSIZE)
1157 UNSUPPORTED_MSG(EM_SETLANGOPTIONS)
1158 UNSUPPORTED_MSG(EM_SETOLECALLBACK)
1159 UNSUPPORTED_MSG(EM_SETOPTIONS)
1160 UNSUPPORTED_MSG(EM_SETPALETTE)
1161 UNSUPPORTED_MSG(EM_SETPASSWORDCHAR)
1162 UNSUPPORTED_MSG(EM_SETSCROLLPOS)
1163 UNSUPPORTED_MSG(EM_SETTABSTOPS)
1164 UNSUPPORTED_MSG(EM_SETTARGETDEVICE)
1165 UNSUPPORTED_MSG(EM_SETTEXTMODE)
1166 UNSUPPORTED_MSG(EM_SETTYPOGRAPHYOPTIONS)
1167 UNSUPPORTED_MSG(EM_SETUNDOLIMIT)
1168 UNSUPPORTED_MSG(EM_SETWORDBREAKPROC)
1169 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX)
1170 UNSUPPORTED_MSG(EM_SHOWSCROLLBAR)
1171 UNSUPPORTED_MSG(WM_SETFONT)
1172 UNSUPPORTED_MSG(WM_STYLECHANGING)
1173 UNSUPPORTED_MSG(WM_STYLECHANGED)
1174 /* UNSUPPORTED_MSG(WM_UNICHAR) FIXME missing in Wine headers */
1175
1176 /* Messages specific to Richedit controls */
1177
1178 case EM_STREAMIN:
1179 return ME_StreamIn(editor, wParam, (EDITSTREAM*)lParam);
1180 case EM_STREAMOUT:
1181 return ME_StreamOut(editor, wParam, (EDITSTREAM *)lParam);
1182 case WM_GETDLGCODE:
1183 {
1184 UINT code = DLGC_WANTCHARS|DLGC_WANTARROWS;
1185 if (GetWindowLongW(hWnd, GWL_STYLE)&ES_WANTRETURN)
1186 code |= 0; /* FIXME what can we do here ? ask for messages and censor them ? */
1187 return code;
1188 }
1189 case WM_NCCREATE:
1190 {
1191 CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
1192 TRACE("WM_NCCREATE: style 0x%08lx\n", pcs->style);
1193 editor = ME_MakeEditor(hWnd);
1194 SetWindowLongW(hWnd, 0, (long)editor);
1195 pcs = 0; /* ignore */
1196 return TRUE;
1197 }
1198 case EM_EMPTYUNDOBUFFER:
1199 ME_EmptyUndoStack(editor);
1200 return 0;
1201 case EM_GETSEL:
1202 {
1203 /* Note: wParam/lParam can be NULL */
1204 UINT from, to;
1205 PUINT pfrom = wParam ? (PUINT)wParam : &from;
1206 PUINT pto = lParam ? (PUINT)lParam : &to;
1207 ME_GetSelection(editor, (int *)pfrom, (int *)pto);
1208 if ((*pfrom|*pto) & 0xFFFF0000)
1209 return -1;
1210 return MAKELONG(*pfrom,*pto);
1211 }
1212 case EM_EXGETSEL:
1213 {
1214 CHARRANGE *pRange = (CHARRANGE *)lParam;
1215 ME_GetSelection(editor, (int *)&pRange->cpMin, (int *)&pRange->cpMax);
1216 TRACE("EM_EXGETSEL = (%ld,%ld)\n", pRange->cpMin, pRange->cpMax);
1217 return 0;
1218 }
1219 case EM_CANUNDO:
1220 return editor->pUndoStack != NULL;
1221 case EM_CANREDO:
1222 return editor->pRedoStack != NULL;
1223 case WM_UNDO: /* FIXME: actually not the same */
1224 case EM_UNDO:
1225 ME_Undo(editor);
1226 return 0;
1227 case EM_REDO:
1228 ME_Redo(editor);
1229 return 0;
1230 case EM_SETSEL:
1231 {
1232 ME_SetSelection(editor, wParam, lParam);
1233 ME_Repaint(editor);
1234 ME_SendSelChange(editor);
1235 return 0;
1236 }
1237 case EM_EXSETSEL:
1238 {
1239 CHARRANGE *pRange = (CHARRANGE *)lParam;
1240 TRACE("EM_EXSETSEL (%ld,%ld)\n", pRange->cpMin, pRange->cpMax);
1241 ME_SetSelection(editor, pRange->cpMin, pRange->cpMax);
1242 /* FIXME optimize */
1243 ME_Repaint(editor);
1244 ME_SendSelChange(editor);
1245 return 0;
1246 }
1247 case EM_SETTEXTEX:
1248 {
1249 LPWSTR wszText = (LPWSTR)lParam;
1250 SETTEXTEX *pStruct = (SETTEXTEX *)wParam;
1251 size_t len = lstrlenW(wszText);
1252 int from, to;
1253 ME_Style *style;
1254 TRACE("EM_SETTEXEX - %s, flags %d, cp %d\n", debugstr_w(wszText), (int)pStruct->flags, pStruct->codepage);
1255 if (pStruct->codepage != 1200) {
1256 FIXME("EM_SETTEXTEX only supports unicode right now!\n");
1257 return 0;
1258 }
1259 /* FIXME: this should support RTF strings too, according to MSDN */
1260 if (pStruct->flags & ST_SELECTION) {
1261 ME_GetSelection(editor, &from, &to);
1262 style = ME_GetSelectionInsertStyle(editor);
1263 ME_InternalDeleteText(editor, from, to - from);
1264 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
1265 ME_ReleaseStyle(style);
1266 }
1267 else {
1268 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
1269 ME_InsertTextFromCursor(editor, 0, wszText, -1, editor->pBuffer->pDefaultStyle);
1270 len = 1;
1271 }
1272 ME_CommitUndo(editor);
1273 if (!(pStruct->flags & ST_KEEPUNDO))
1274 ME_EmptyUndoStack(editor);
1275 ME_UpdateRepaint(editor);
1276 return len;
1277 }
1278 case EM_SETBKGNDCOLOR:
1279 {
1280 LRESULT lColor = ME_GetBackColor(editor);
1281 if (editor->rgbBackColor != -1)
1282 DeleteObject(editor->hbrBackground);
1283 if (wParam)
1284 {
1285 editor->rgbBackColor = -1;
1286 editor->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
1287 }
1288 else
1289 {
1290 editor->rgbBackColor = lParam;
1291 editor->hbrBackground = CreateSolidBrush(editor->rgbBackColor);
1292 }
1293 if (editor->bRedraw)
1294 {
1295 InvalidateRect(hWnd, NULL, TRUE);
1296 UpdateWindow(hWnd);
1297 }
1298 return lColor;
1299 }
1300 case EM_GETMODIFY:
1301 return editor->nModifyStep == 0 ? 0 : 1;
1302 case EM_SETMODIFY:
1303 {
1304 if (wParam)
1305 editor->nModifyStep = 0x80000000;
1306 else
1307 editor->nModifyStep = 0;
1308
1309 return 0;
1310 }
1311 case EM_SETREADONLY:
1312 {
1313 long nStyle = GetWindowLongW(hWnd, GWL_STYLE);
1314 if (wParam)
1315 nStyle |= ES_READONLY;
1316 else
1317 nStyle &= ~ES_READONLY;
1318 SetWindowLongW(hWnd, GWL_STYLE, nStyle);
1319 ME_Repaint(editor);
1320 return 0;
1321 }
1322 case EM_SETEVENTMASK:
1323 {
1324 DWORD nOldMask = editor->nEventMask;
1325
1326 editor->nEventMask = lParam;
1327 return nOldMask;
1328 }
1329 case EM_GETEVENTMASK:
1330 return editor->nEventMask;
1331 case EM_SETCHARFORMAT:
1332 {
1333 CHARFORMAT2W buf, *p;
1334 BOOL bRepaint = TRUE;
1335 p = ME_ToCF2W(&buf, (CHARFORMAT2W *)lParam);
1336 if (!wParam)
1337 ME_SetDefaultCharFormat(editor, p);
1338 else if (wParam == (SCF_WORD | SCF_SELECTION))
1339 FIXME("EM_SETCHARFORMAT: word selection not supported\n");
1340 else if (wParam == SCF_ALL)
1341 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), p);
1342 else {
1343 int from, to;
1344 ME_GetSelection(editor, &from, &to);
1345 bRepaint = (from != to);
1346 ME_SetSelectionCharFormat(editor, p);
1347 }
1348 ME_CommitUndo(editor);
1349 if (bRepaint)
1350 ME_UpdateRepaint(editor);
1351 return 0;
1352 }
1353 case EM_GETCHARFORMAT:
1354 {
1355 CHARFORMAT2W tmp, *dst = (CHARFORMAT2W *)lParam;
1356 if (dst->cbSize != sizeof(CHARFORMATA) &&
1357 dst->cbSize != sizeof(CHARFORMATW) &&
1358 dst->cbSize != sizeof(CHARFORMAT2A) &&
1359 dst->cbSize != sizeof(CHARFORMAT2W))
1360 return 0;
1361 tmp.cbSize = sizeof(tmp);
1362 if (!wParam)
1363 ME_GetDefaultCharFormat(editor, &tmp);
1364 else
1365 ME_GetSelectionCharFormat(editor, &tmp);
1366 ME_CopyToCFAny(dst, &tmp);
1367 return tmp.dwMask;
1368 }
1369 case EM_SETPARAFORMAT:
1370 ME_SetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
1371 ME_UpdateRepaint(editor);
1372 ME_CommitUndo(editor);
1373 return 0;
1374 case EM_GETPARAFORMAT:
1375 ME_GetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
1376 return 0;
1377 case EM_GETFIRSTVISIBLELINE:
1378 {
1379 ME_DisplayItem *p = editor->pBuffer->pFirst;
1380 int y = editor->nScrollPosY;
1381 int ypara = 0;
1382 int count = 0;
1383 int ystart, yend;
1384 while(p) {
1385 p = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
1386 if (p->type == diTextEnd)
1387 break;
1388 if (p->type == diParagraph) {
1389 ypara = p->member.para.nYPos;
1390 continue;
1391 }
1392 ystart = ypara + p->member.row.nYPos;
1393 yend = ystart + p->member.row.nHeight;
1394 if (y < yend) {
1395 break;
1396 }
1397 count++;
1398 }
1399 return count;
1400 }
1401 case EM_LINESCROLL:
1402 {
1403 int nPos = editor->nScrollPosY, nEnd= editor->nTotalLength - editor->sizeWindow.cy;
1404 nPos += 8 * lParam; /* FIXME follow the original */
1405 if (nPos>=nEnd)
1406 nPos = nEnd;
1407 if (nPos<0)
1408 nPos = 0;
1409 if (nPos != editor->nScrollPosY) {
1410 int dy = editor->nScrollPosY - nPos;
1411 editor->nScrollPosY = nPos;
1412 SetScrollPos(hWnd, SB_VERT, nPos, TRUE);
1413 if (editor->bRedraw)
1414 {
1415 ScrollWindow(hWnd, 0, dy, NULL, NULL);
1416 UpdateWindow(hWnd);
1417 }
1418 }
1419 return TRUE; /* Should return false if a single line richedit control */
1420 }
1421 case WM_CLEAR:
1422 {
1423 int from, to;
1424 ME_GetSelection(editor, &from, &to);
1425 ME_InternalDeleteText(editor, from, to-from);
1426 ME_CommitUndo(editor);
1427 ME_UpdateRepaint(editor);
1428 return 0;
1429 }
1430 case EM_REPLACESEL:
1431 {
1432 int from, to;
1433 ME_Style *style;
1434 LPWSTR wszText = ME_ToUnicode(hWnd, (void *)lParam);
1435 size_t len = lstrlenW(wszText);
1436 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText));
1437
1438 ME_GetSelection(editor, &from, &to);
1439 style = ME_GetSelectionInsertStyle(editor);
1440 ME_InternalDeleteText(editor, from, to-from);
1441 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
1442 ME_ReleaseStyle(style);
1443 ME_EndToUnicode(hWnd, wszText);
1444 /* drop temporary style if line end */
1445 /* FIXME question: does abc\n mean: put abc, clear temp style, put \n? (would require a change) */
1446 if (len>0 && wszText[len-1] == '\n')
1447 ME_ClearTempStyle(editor);
1448
1449 ME_CommitUndo(editor);
1450 if (!wParam)
1451 ME_EmptyUndoStack(editor);
1452 ME_UpdateRepaint(editor);
1453 return 0;
1454 }
1455 case WM_SETTEXT:
1456 {
1457 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
1458 if (lParam)
1459 {
1460 LPWSTR wszText = ME_ToUnicode(hWnd, (void *)lParam);
1461 TRACE("WM_SETTEXT lParam==%lx\n",lParam);
1462 TRACE("WM_SETTEXT - %s\n", debugstr_w(wszText)); /* debugstr_w() */
1463 if (lstrlenW(wszText) > 0)
1464 {
1465 /* uses default style! */
1466 ME_InsertTextFromCursor(editor, 0, wszText, -1, editor->pBuffer->pDefaultStyle);
1467 }
1468 ME_EndToUnicode(hWnd, wszText);
1469 }
1470 else
1471 TRACE("WM_SETTEXT - NULL\n");
1472 ME_CommitUndo(editor);
1473 ME_EmptyUndoStack(editor);
1474 ME_UpdateRepaint(editor);
1475 return 0;
1476 }
1477 case EM_CANPASTE:
1478 {
1479 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
1480 if (IsClipboardFormatAvailable(nRTFFormat))
1481 return TRUE;
1482 if (IsClipboardFormatAvailable(CF_UNICODETEXT))
1483 return TRUE;
1484 return FALSE;
1485 }
1486 case WM_PASTE:
1487 {
1488 DWORD dwFormat = 0;
1489 EDITSTREAM es;
1490 ME_GlobalDestStruct gds;
1491 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
1492 UINT cf = 0;
1493
1494 if (IsClipboardFormatAvailable(nRTFFormat))
1495 cf = nRTFFormat, dwFormat = SF_RTF;
1496 else if (IsClipboardFormatAvailable(CF_UNICODETEXT))
1497 cf = CF_UNICODETEXT, dwFormat = SF_TEXT|SF_UNICODE;
1498 else
1499 return 0;
1500
1501 if (!OpenClipboard(hWnd))
1502 return 0;
1503 gds.hData = GetClipboardData(cf);
1504 gds.nLength = 0;
1505 es.dwCookie = (DWORD)&gds;
1506 es.pfnCallback = dwFormat == SF_RTF ? ME_ReadFromHGLOBALRTF : ME_ReadFromHGLOBALUnicode;
1507 SendMessageW(hWnd, EM_STREAMIN, dwFormat|SFF_SELECTION, (LPARAM)&es);
1508
1509 CloseClipboard();
1510 return 0;
1511 }
1512 case WM_CUT:
1513 case WM_COPY:
1514 {
1515 int from, to, pars;
1516 WCHAR *data;
1517 HANDLE hData;
1518 EDITSTREAM es;
1519 ME_GlobalDestStruct gds;
1520
1521 if (!OpenClipboard(hWnd))
1522 return 0;
1523
1524 EmptyClipboard();
1525 ME_GetSelection(editor, &from, &to);
1526 pars = ME_CountParagraphsBetween(editor, from, to);
1527 hData = GlobalAlloc(GMEM_MOVEABLE, sizeof(WCHAR)*(to-from+pars+1));
1528 data = (WCHAR *)GlobalLock(hData);
1529 ME_GetTextW(editor, data, from, to-from, TRUE);
1530 GlobalUnlock(hData);
1531
1532 gds.hData = GlobalAlloc(GMEM_MOVEABLE, 0);
1533 gds.nLength = 0;
1534 es.dwCookie = (DWORD)&gds;
1535 es.pfnCallback = ME_AppendToHGLOBAL;
1536 SendMessageW(hWnd, EM_STREAMOUT, SFF_SELECTION|SF_RTF, (LPARAM)&es);
1537 GlobalReAlloc(gds.hData, gds.nLength+1, 0);
1538
1539 SetClipboardData(CF_UNICODETEXT, hData);
1540 SetClipboardData(RegisterClipboardFormatA("Rich Text Format"), gds.hData);
1541
1542 CloseClipboard();
1543 if (msg == WM_CUT)
1544 {
1545 ME_InternalDeleteText(editor, from, to-from);
1546 ME_CommitUndo(editor);
1547 ME_UpdateRepaint(editor);
1548 }
1549 return 0;
1550 }
1551 case WM_GETTEXTLENGTH:
1552 return ME_GetTextLength(editor);
1553 case EM_GETTEXTLENGTHEX:
1554 return ME_GetTextLengthEx(editor, (GETTEXTLENGTHEX *)wParam);
1555 case WM_GETTEXT:
1556 {
1557 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
1558 tr.chrg.cpMin = 0;
1559 tr.chrg.cpMax = wParam-1;
1560 tr.lpstrText = (WCHAR *)lParam;
1561 return RichEditANSIWndProc(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
1562 }
1563 case EM_GETTEXTEX:
1564 {
1565 GETTEXTEX *ex = (GETTEXTEX*)wParam;
1566 int nStart, nCount;
1567
1568 if (ex->flags & ~(GT_SELECTION | GT_USECRLF))
1569 FIXME("GETTEXTEX flags 0x%08lx not supported\n", ex->flags & ~(GT_SELECTION | GT_USECRLF));
1570
1571 if (ex->flags & GT_SELECTION)
1572 {
1573 ME_GetSelection(editor, &nStart, &nCount);
1574 nCount -= nStart;
1575 nCount = min(nCount, ex->cb - 1);
1576 }
1577 else
1578 {
1579 nStart = 0;
1580 nCount = ex->cb - 1;
1581 }
1582 if (ex->codepage == 1200 || IsWindowUnicode(hWnd))
1583 {
1584 nCount = min(nCount, ex->cb / sizeof(WCHAR) - 1);
1585 return ME_GetTextW(editor, (LPWSTR)lParam, nStart, nCount, ex->flags & GT_USECRLF);
1586 }
1587 else
1588 {
1589 /* potentially each char may be a CR, why calculate the exact value with O(N) when
1590 we can just take a bigger buffer? :) */
1591 int crlfmul = (ex->flags & GT_USECRLF) ? 2 : 1;
1592 LPWSTR buffer = HeapAlloc(GetProcessHeap(), 0, (crlfmul*nCount + 1) * sizeof(WCHAR));
1593 DWORD buflen = ex->cb;
1594 LRESULT rc;
1595 DWORD flags = 0;
1596
1597 buflen = ME_GetTextW(editor, buffer, nStart, nCount, ex->flags & GT_USECRLF);
1598 rc = WideCharToMultiByte(ex->codepage, flags, buffer, buflen, (LPSTR)lParam, ex->cb, ex->lpDefaultChar, ex->lpUsedDefaultChar);
1599
1600 HeapFree(GetProcessHeap(),0,buffer);
1601 return rc;
1602 }
1603 }
1604 case EM_GETSELTEXT:
1605 {
1606 int from, to;
1607 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
1608 ME_GetSelection(editor, &from, &to);
1609 tr.chrg.cpMin = from;
1610 tr.chrg.cpMax = to;
1611 tr.lpstrText = (WCHAR *)lParam;
1612 return RichEditANSIWndProc(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
1613 }
1614 case EM_GETTEXTRANGE:
1615 {
1616 TEXTRANGEW *rng = (TEXTRANGEW *)lParam;
1617 TRACE("EM_GETTEXTRANGE min=%ld max=%ld unicode=%d emul1.0=%d length=%d\n",
1618 rng->chrg.cpMin, rng->chrg.cpMax, IsWindowUnicode(hWnd),
1619 editor->bEmulateVersion10, ME_GetTextLength(editor));
1620 if (IsWindowUnicode(hWnd))
1621 return ME_GetTextW(editor, rng->lpstrText, rng->chrg.cpMin, rng->chrg.cpMax-rng->chrg.cpMin, editor->bEmulateVersion10);
1622 else
1623 {
1624 int nLen = rng->chrg.cpMax-rng->chrg.cpMin;
1625 WCHAR *p = ALLOC_N_OBJ(WCHAR, nLen+1);
1626 int nChars = ME_GetTextW(editor, p, rng->chrg.cpMin, nLen, editor->bEmulateVersion10);
1627 /* FIXME this is a potential security hole (buffer overrun)
1628 if you know more about wchar->mbyte conversion please explain
1629 */
1630 WideCharToMultiByte(CP_ACP, 0, p, nChars+1, (char *)rng->lpstrText, nLen+1, NULL, NULL);
1631 FREE_OBJ(p);
1632 return nChars;
1633 }
1634 }
1635 case EM_GETLINECOUNT:
1636 {
1637 ME_DisplayItem *item = editor->pBuffer->pFirst->next;
1638 int nRows = 0;
1639
1640 while (item != editor->pBuffer->pLast)
1641 {
1642 assert(item->type == diParagraph);
1643 nRows += item->member.para.nRows;
1644 item = item->member.para.next_para;
1645 }
1646 TRACE("EM_GETLINECOUNT: nRows==%d\n", nRows);
1647 return max(1, nRows);
1648 }
1649 case EM_LINEFROMCHAR:
1650 {
1651 if (wParam == -1)
1652 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(editor, 1));
1653 else
1654 return ME_RowNumberFromCharOfs(editor, wParam);
1655 }
1656 case EM_EXLINEFROMCHAR:
1657 {
1658 return ME_RowNumberFromCharOfs(editor, lParam);
1659 }
1660 case EM_LINEINDEX:
1661 {
1662 ME_DisplayItem *item, *para;
1663 int nCharOfs;
1664
1665 if (wParam == -1)
1666 item = ME_FindItemBack(editor->pCursors[0].pRun, diStartRow);
1667 else
1668 item = ME_FindRowWithNumber(editor, wParam);
1669 if (!item)
1670 return -1;
1671 para = ME_GetParagraph(item);
1672 item = ME_FindItemFwd(item, diRun);
1673 nCharOfs = para->member.para.nCharOfs + item->member.run.nCharOfs;
1674 TRACE("EM_LINEINDEX: nCharOfs==%d\n", nCharOfs);
1675 return nCharOfs;
1676 }
1677 case EM_LINELENGTH:
1678 {
1679 ME_DisplayItem *item, *item_end;
1680 int nChars = 0, nThisLineOfs = 0, nNextLineOfs = 0;
1681
1682 if (wParam > ME_GetTextLength(editor))
1683 return 0;
1684 if (wParam == -1)
1685 {
1686 FIXME("EM_LINELENGTH: returning number of unselected characters on lines with selection unsupported.\n");
1687 return 0;
1688 }
1689 item = ME_FindItemAtOffset(editor, diRun, wParam, NULL);
1690 item = ME_RowStart(item);
1691 nThisLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item, diRun), 0);
1692 item_end = ME_FindItemFwd(item, diStartRow);
1693 if (item_end)
1694 nNextLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item_end, diRun), 0);
1695 else
1696 nNextLineOfs = ME_FindItemFwd(item, diParagraphOrEnd)->member.para.nCharOfs
1697 - (editor->bEmulateVersion10?2:1);
1698 nChars = nNextLineOfs - nThisLineOfs;
1699 TRACE("EM_LINELENGTH(%d)==%d\n",wParam, nChars);
1700 return nChars;
1701 }
1702 case EM_FINDTEXT:
1703 {
1704 FINDTEXTA *ft = (FINDTEXTA *)lParam;
1705 int nChars = MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, NULL, 0);
1706 WCHAR *tmp;
1707
1708 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
1709 MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, tmp, nChars);
1710 return ME_FindText(editor, wParam, &ft->chrg, tmp, NULL);
1711 }
1712 case EM_FINDTEXTEX:
1713 {
1714 FINDTEXTEXA *ex = (FINDTEXTEXA *)lParam;
1715 int nChars = MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, NULL, 0);
1716 WCHAR *tmp;
1717
1718 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
1719 MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, tmp, nChars);
1720 return ME_FindText(editor, wParam, &ex->chrg, tmp, &ex->chrgText);
1721 }
1722 case EM_FINDTEXTW:
1723 {
1724 FINDTEXTW *ft = (FINDTEXTW *)lParam;
1725 return ME_FindText(editor, wParam, &ft->chrg, ft->lpstrText, NULL);
1726 }
1727 case EM_FINDTEXTEXW:
1728 {
1729 FINDTEXTEXW *ex = (FINDTEXTEXW *)lParam;
1730 return ME_FindText(editor, wParam, &ex->chrg, ex->lpstrText, &ex->chrgText);
1731 }
1732 case EM_GETZOOM:
1733 if (!wParam || !lParam)
1734 return FALSE;
1735 *(int *)wParam = editor->nZoomNumerator;
1736 *(int *)lParam = editor->nZoomDenominator;
1737 return TRUE;
1738 case EM_SETZOOM:
1739 return ME_SetZoom(editor, wParam, lParam);
1740 case EM_CHARFROMPOS:
1741 return ME_CharFromPos(editor, ((POINTL *)lParam)->x, ((POINTL *)lParam)->y);
1742 case EM_POSFROMCHAR:
1743 {
1744 ME_DisplayItem *pRun;
1745 int nCharOfs, nOffset, nLength;
1746 POINTL pt = {0,0};
1747
1748 nCharOfs = wParam;
1749 /* detect which API version we're dealing with */
1750 if (wParam >= 0x40000)
1751 nCharOfs = lParam;
1752 nLength = ME_GetTextLength(editor);
1753
1754 if (nCharOfs < nLength) {
1755 ME_RunOfsFromCharOfs(editor, nCharOfs, &pRun, &nOffset);
1756 assert(pRun->type == diRun);
1757 pt.y = pRun->member.run.pt.y;
1758 pt.x = pRun->member.run.pt.x + ME_PointFromChar(editor, &pRun->member.run, nOffset);
1759 pt.y += ME_GetParagraph(pRun)->member.para.nYPos;
1760 } else {
1761 pt.x = 0;
1762 pt.y = editor->pBuffer->pLast->member.para.nYPos;
1763 }
1764 if (wParam >= 0x40000) {
1765 *(POINTL *)wParam = pt;
1766 }
1767 return MAKELONG( pt.x, pt.y );
1768 }
1769 case WM_CREATE:
1770 ME_CommitUndo(editor);
1771 ME_WrapMarkedParagraphs(editor);
1772 ME_MoveCaret(editor);
1773 return 0;
1774 case WM_DESTROY:
1775 ME_DestroyEditor(editor);
1776 SetWindowLongW(hWnd, 0, 0);
1777 return 0;
1778 case WM_LBUTTONDOWN:
1779 SetFocus(hWnd);
1780 ME_LButtonDown(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
1781 SetCapture(hWnd);
1782 break;
1783 case WM_MOUSEMOVE:
1784 if (GetCapture() == hWnd)
1785 ME_MouseMove(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
1786 break;
1787 case WM_LBUTTONUP:
1788 if (GetCapture() == hWnd)
1789 ReleaseCapture();
1790 break;
1791 case WM_PAINT:
1792 if (editor->bRedraw)
1793 {
1794 HDC hDC;
1795 PAINTSTRUCT ps;
1796
1797 hDC = BeginPaint(hWnd, &ps);
1798 ME_PaintContent(editor, hDC, FALSE, &ps.rcPaint);
1799 EndPaint(hWnd, &ps);
1800 }
1801 break;
1802 case WM_SETFOCUS:
1803 ME_ShowCaret(editor);
1804 ME_SendOldNotify(editor, EN_SETFOCUS);
1805 return 0;
1806 case WM_KILLFOCUS:
1807 ME_HideCaret(editor);
1808 ME_SendOldNotify(editor, EN_KILLFOCUS);
1809 return 0;
1810 case WM_ERASEBKGND:
1811 {
1812 if (editor->bRedraw)
1813 {
1814 HDC hDC = (HDC)wParam;
1815 RECT rc;
1816 if (GetUpdateRect(hWnd,&rc,TRUE))
1817 {
1818 FillRect(hDC, &rc, editor->hbrBackground);
1819 }
1820 }
1821 return 1;
1822 }
1823 case WM_COMMAND:
1824 TRACE("editor wnd command = %d\n", LOWORD(wParam));
1825 return 0;
1826 case WM_KEYDOWN:
1827 if (ME_ArrowKey(editor, LOWORD(wParam), GetKeyState(VK_CONTROL)<0)) {
1828 ME_CommitUndo(editor);
1829 ME_EnsureVisible(editor, editor->pCursors[0].pRun);
1830 HideCaret(hWnd);
1831 ME_MoveCaret(editor);
1832 ShowCaret(hWnd);
1833 return 0;
1834 }
1835 if (GetKeyState(VK_CONTROL)<0)
1836 {
1837 if (LOWORD(wParam)=='W')
1838 {
1839 CHARFORMAT2W chf;
1840 char buf[2048];
1841 ME_GetSelectionCharFormat(editor, &chf);
1842 ME_DumpStyleToBuf(&chf, buf);
1843 MessageBoxA(NULL, buf, "Style dump", MB_OK);
1844 }
1845 if (LOWORD(wParam)=='Q')
1846 {
1847 ME_CheckCharOffsets(editor);
1848 }
1849 }
1850 goto do_default;
1851 case WM_CHAR:
1852 {
1853 WCHAR wstr = LOWORD(wParam);
1854
1855 switch (wstr)
1856 {
1857 case 3: /* Ctrl-C */
1858 SendMessageW(editor->hWnd, WM_COPY, 0, 0);
1859 return 0;
1860 }
1861
1862 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY) {
1863 MessageBeep(MB_ICONERROR);
1864 return 0; /* FIXME really 0 ? */
1865 }
1866
1867 switch (wstr)
1868 {
1869 case 1: /* Ctrl-A */
1870 ME_SetSelection(editor, 0, -1);
1871 return 0;
1872 case 22: /* Ctrl-V */
1873 SendMessageW(editor->hWnd, WM_PASTE, 0, 0);
1874 return 0;
1875 case 24: /* Ctrl-X */
1876 SendMessageW(editor->hWnd, WM_CUT, 0, 0);
1877 return 0;
1878 case 25: /* Ctrl-Y */
1879 SendMessageW(editor->hWnd, EM_REDO, 0, 0);
1880 return 0;
1881 case 26: /* Ctrl-Z */
1882 SendMessageW(editor->hWnd, EM_UNDO, 0, 0);
1883 return 0;
1884 }
1885 if (((unsigned)wstr)>=' ' || wstr=='\r' || wstr=='\t') {
1886 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
1887 ME_Style *style = ME_GetInsertStyle(editor, 0);
1888 ME_SaveTempStyle(editor);
1889 ME_InsertTextFromCursor(editor, 0, &wstr, 1, style);
1890 ME_ReleaseStyle(style);
1891 ME_CommitUndo(editor);
1892 ME_UpdateRepaint(editor);
1893 }
1894 return 0;
1895 }
1896 case WM_VSCROLL:
1897 {
1898 int nPos = editor->nScrollPosY;
1899 si.cbSize = sizeof(SCROLLINFO);
1900 si.fMask = SIF_PAGE|SIF_POS|SIF_RANGE|SIF_TRACKPOS;
1901 GetScrollInfo(hWnd, SB_VERT, &si);
1902 switch(LOWORD(wParam)) {
1903 case SB_LINEUP:
1904 nPos -= 24; /* FIXME follow the original */
1905 if (nPos<0) nPos = 0;
1906 break;
1907 case SB_LINEDOWN:
1908 {
1909 int nEnd = editor->nTotalLength - editor->sizeWindow.cy;
1910 nPos += 24; /* FIXME follow the original */
1911 if (nPos>=nEnd) nPos = nEnd;
1912 break;
1913 }
1914 case SB_PAGEUP:
1915 nPos -= editor->sizeWindow.cy;
1916 if (nPos<0) nPos = 0;
1917 break;
1918 case SB_PAGEDOWN:
1919 nPos += editor->sizeWindow.cy;
1920 if (nPos>=editor->nTotalLength) nPos = editor->nTotalLength-1;
1921 break;
1922 case SB_THUMBTRACK:
1923 case SB_THUMBPOSITION:
1924 nPos = si.nTrackPos;
1925 break;
1926 }
1927 if (nPos != editor->nScrollPosY) {
1928 int dy = editor->nScrollPosY - nPos;
1929 editor->nScrollPosY = nPos;
1930 SetScrollPos(hWnd, SB_VERT, nPos, TRUE);
1931 if (editor->bRedraw)
1932 {
1933 ScrollWindow(hWnd, 0, dy, NULL, NULL);
1934 UpdateWindow(hWnd);
1935 }
1936 }
1937 break;
1938 }
1939 case WM_MOUSEWHEEL:
1940 {
1941 int gcWheelDelta = 0, nPos = editor->nScrollPosY, nEnd = editor->nTotalLength - editor->sizeWindow.cy;
1942 UINT pulScrollLines;
1943 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
1944 gcWheelDelta -= GET_WHEEL_DELTA_WPARAM(wParam);
1945 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
1946 nPos += pulScrollLines * (gcWheelDelta / WHEEL_DELTA) * 8; /* FIXME follow the original */
1947 if (nPos>=nEnd)
1948 nPos = nEnd;
1949 if (nPos<0)
1950 nPos = 0;
1951 if (nPos != editor->nScrollPosY) {
1952 int dy = editor->nScrollPosY - nPos;
1953 editor->nScrollPosY = nPos;
1954 SetScrollPos(hWnd, SB_VERT, nPos, TRUE);
1955 if (editor->bRedraw)
1956 {
1957 ScrollWindow(hWnd, 0, dy, NULL, NULL);
1958 UpdateWindow(hWnd);
1959 }
1960 }
1961 break;
1962 }
1963 case EM_GETRECT:
1964 {
1965 *((RECT *)lParam) = editor->rcFormat;
1966 return 0;
1967 }
1968 case EM_SETRECT:
1969 case EM_SETRECTNP:
1970 {
1971 if (lParam)
1972 {
1973 RECT *rc = (RECT *)lParam;
1974
1975 if (wParam)
1976 {
1977 editor->rcFormat.left += rc->left;
1978 editor->rcFormat.top += rc->top;
1979 editor->rcFormat.right += rc->right;
1980 editor->rcFormat.bottom += rc->bottom;
1981 }
1982 else
1983 {
1984 editor->rcFormat = *rc;
1985 }
1986 }
1987 else
1988 {
1989 GetClientRect(hWnd, &editor->rcFormat);
1990 }
1991 if (msg != EM_SETRECTNP)
1992 ME_RewrapRepaint(editor);
1993 return 0;
1994 }
1995 case EM_REQUESTRESIZE:
1996 ME_SendRequestResize(editor, TRUE);
1997 return 0;
1998 case WM_SETREDRAW:
1999 editor->bRedraw = wParam;
2000 return 0;
2001 case WM_SIZE:
2002 {
2003 GetClientRect(hWnd, &editor->rcFormat);
2004 ME_RewrapRepaint(editor);
2005 return DefWindowProcW(hWnd, msg, wParam, lParam);
2006 }
2007 case EM_GETOLEINTERFACE:
2008 {
2009 LPVOID *ppvObj = (LPVOID*) lParam;
2010 FIXME("EM_GETOLEINTERFACE %p: stub\n", ppvObj);
2011 return CreateIRichEditOle(ppvObj);
2012 }
2013 default:
2014 do_default:
2015 return DefWindowProcW(hWnd, msg, wParam, lParam);
2016 }
2017 return 0L;
2018 }
2019
2020
2021 /******************************************************************
2022 * RichEdit10ANSIWndProc (RICHED20.9)
2023 */
2024 LRESULT WINAPI RichEdit10ANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
2025 {
2026 LRESULT result;
2027
2028 /* FIXME: this is NOT the same as 2.0 version */
2029 result = RichEditANSIWndProc(hWnd, msg, wParam, lParam);
2030 if (msg == WM_NCCREATE)
2031 {
2032 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongW(hWnd, 0);
2033
2034 editor->bEmulateVersion10 = TRUE;
2035 editor->pBuffer->pLast->member.para.nCharOfs = 2;
2036 }
2037 return result;
2038 }
2039
2040 void ME_SendOldNotify(ME_TextEditor *editor, int nCode)
2041 {
2042 HWND hWnd = editor->hWnd;
2043 SendMessageA(GetParent(hWnd), WM_COMMAND, (nCode<<16)|GetWindowLongW(hWnd, GWLP_ID), (LPARAM)hWnd);
2044 }
2045
2046 int ME_CountParagraphsBetween(ME_TextEditor *editor, int from, int to)
2047 {
2048 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
2049 int i = 0;
2050
2051 while(item && item->member.para.next_para->member.para.nCharOfs <= from)
2052 item = item->member.para.next_para;
2053 if (!item)
2054 return 0;
2055 while(item && item->member.para.next_para->member.para.nCharOfs <= to) {
2056 item = item->member.para.next_para;
2057 i++;
2058 }
2059 return i;
2060 }
2061
2062
2063 int ME_GetTextW(ME_TextEditor *editor, WCHAR *buffer, int nStart, int nChars, int bCRLF)
2064 {
2065 ME_DisplayItem *item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
2066 int nWritten = 0;
2067 WCHAR *pStart = buffer;
2068
2069 if (!item) {
2070 *buffer = L'\0';
2071 return 0;
2072 }
2073
2074 if (nStart)
2075 {
2076 int nLen = ME_StrLen(item->member.run.strText) - nStart;
2077 if (nLen > nChars)
2078 nLen = nChars;
2079 CopyMemory(buffer, item->member.run.strText->szData + nStart, sizeof(WCHAR)*nLen);
2080 nChars -= nLen;
2081 nWritten += nLen;
2082 if (!nChars)
2083 return nWritten;
2084 buffer += nLen;
2085 nStart = 0;
2086 item = ME_FindItemFwd(item, diRun);
2087 }
2088
2089 while(nChars && item)
2090 {
2091 int nLen = ME_StrLen(item->member.run.strText);
2092 if (nLen > nChars)
2093 nLen = nChars;
2094
2095 if (item->member.run.nFlags & MERF_ENDPARA)
2096 {
2097 *buffer = '\r';
2098 if (bCRLF)
2099 {
2100 *(++buffer) = '\n';
2101 nWritten++;
2102 }
2103 assert(nLen == 1);
2104 /* our end paragraph consists of 2 characters now */
2105 if (editor->bEmulateVersion10)
2106 nChars--;
2107 }
2108 else
2109 CopyMemory(buffer, item->member.run.strText->szData, sizeof(WCHAR)*nLen);
2110 nChars -= nLen;
2111 nWritten += nLen;
2112 buffer += nLen;
2113
2114 if (!nChars)
2115 {
2116 TRACE("nWritten=%d, actual=%d\n", nWritten, buffer-pStart);
2117 *buffer = L'\0';
2118 return nWritten;
2119 }
2120 item = ME_FindItemFwd(item, diRun);
2121 }
2122 *buffer = L'\0';
2123 TRACE("nWritten=%d, actual=%d\n", nWritten, buffer-pStart);
2124 return nWritten;
2125 }
2126
2127 void ME_RegisterEditorClass(HINSTANCE hInstance)
2128 {
2129 BOOL bResult;
2130 WNDCLASSW wcW;
2131 WNDCLASSA wcA;
2132
2133 wcW.style = CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
2134 wcW.lpfnWndProc = RichEditANSIWndProc;
2135 wcW.cbClsExtra = 0;
2136 wcW.cbWndExtra = 4;
2137 wcW.hInstance = NULL; /* hInstance would register DLL-local class */
2138 wcW.hIcon = NULL;
2139 wcW.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
2140 wcW.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
2141 wcW.lpszMenuName = NULL;
2142 wcW.lpszClassName = wszClassName;
2143 bResult = RegisterClassW(&wcW);
2144 assert(bResult);
2145 wcW.lpszClassName = wszClassName50;
2146 bResult = RegisterClassW(&wcW);
2147 assert(bResult);
2148
2149 wcA.style = CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
2150 wcA.lpfnWndProc = RichEditANSIWndProc;
2151 wcA.cbClsExtra = 0;
2152 wcA.cbWndExtra = 4;
2153 wcA.hInstance = NULL; /* hInstance would register DLL-local class */
2154 wcA.hIcon = NULL;
2155 wcA.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
2156 wcA.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
2157 wcA.lpszMenuName = NULL;
2158 wcA.lpszClassName = "RichEdit20A";
2159 bResult = RegisterClassA(&wcA);
2160 assert(bResult);
2161 wcA.lpszClassName = "RichEdit50A";
2162 bResult = RegisterClassA(&wcA);
2163 assert(bResult);
2164 }
2165 /******************************************************************
2166 * CreateTextServices (RICHED20.4)
2167 *
2168 * FIXME should be ITextHost instead of void*
2169 */
2170 HRESULT WINAPI CreateTextServices(IUnknown *punkOuter, void *pITextHost,
2171 IUnknown **ppUnk)
2172 {
2173 FIXME("stub\n");
2174 /* FIXME should support aggregation */
2175 if (punkOuter)
2176 return CLASS_E_NOAGGREGATION;
2177
2178 return E_FAIL; /* E_NOTIMPL isn't allowed by MSDN */
2179 }
2180
2181 LRESULT WINAPI REComboWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
2182 /* FIXME: Not implemented */
2183 TRACE("hWnd %p msg %04x (%s) %08x %08lx\n",
2184 hWnd, msg, get_msg_name(msg), wParam, lParam);
2185 return DefWindowProcW(hWnd, msg, wParam, lParam);
2186 }
2187
2188 LRESULT WINAPI REListWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
2189 /* FIXME: Not implemented */
2190 TRACE("hWnd %p msg %04x (%s) %08x %08lx\n",
2191 hWnd, msg, get_msg_name(msg), wParam, lParam);
2192 return DefWindowProcW(hWnd, msg, wParam, lParam);
2193 }
2194
2195 /******************************************************************
2196 * REExtendedRegisterClass (RICHED20.8)
2197 *
2198 * FIXME undocumented
2199 * Need to check for errors and implement controls and callbacks
2200 */
2201 LRESULT WINAPI REExtendedRegisterClass(void)
2202 {
2203 WNDCLASSW wcW;
2204 UINT result;
2205
2206 FIXME("semi stub\n");
2207
2208 wcW.cbClsExtra = 0;
2209 wcW.cbWndExtra = 4;
2210 wcW.hInstance = NULL;
2211 wcW.hIcon = NULL;
2212 wcW.hCursor = NULL;
2213 wcW.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
2214 wcW.lpszMenuName = NULL;
2215
2216 if (!ME_ListBoxRegistered)
2217 {
2218 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS;
2219 wcW.lpfnWndProc = REListWndProc;
2220 wcW.lpszClassName = wszClassNameListBox;
2221 if (RegisterClassW(&wcW)) ME_ListBoxRegistered = TRUE;
2222 }
2223
2224 if (!ME_ComboBoxRegistered)
2225 {
2226 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
2227 wcW.lpfnWndProc = REComboWndProc;
2228 wcW.lpszClassName = wszClassNameComboBox;
2229 if (RegisterClassW(&wcW)) ME_ComboBoxRegistered = TRUE;
2230 }
2231
2232 result = 0;
2233 if (ME_ListBoxRegistered)
2234 result += 1;
2235 if (ME_ComboBoxRegistered)
2236 result += 2;
2237
2238 return result;
2239 }