richedit: On failure to parse incoming text as RTF, EM_STREAMIN must set dwError...
[wine.git] / dlls / riched20 / editor.c
blobc867df9e3f8ef778812400b35b7b594abefe0d27
1 /*
2 * RichEdit - functions dealing with editor object
4 * Copyright 2004 by Krzysztof Foltman
5 * Copyright 2005 by Cihan Altinay
6 * Copyright 2005 by Phil Krylov
7 * Copyright 2008 Eric Pouech
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 /*
25 API implementation status:
27 Messages (ANSI versions not done yet)
28 + EM_AUTOURLDETECT 2.0
29 + EM_CANPASTE
30 + EM_CANREDO 2.0
31 + EM_CANUNDO
32 + EM_CHARFROMPOS
33 - EM_DISPLAYBAND
34 + EM_EMPTYUNDOBUFFER
35 + EM_EXGETSEL
36 + EM_EXLIMITTEXT
37 + EM_EXLINEFROMCHAR
38 + EM_EXSETSEL
39 + EM_FINDTEXT (only FR_DOWN flag implemented)
40 + EM_FINDTEXTEX (only FR_DOWN flag implemented)
41 - EM_FINDWORDBREAK
42 - EM_FMTLINES
43 - EM_FORMATRANGE
44 + EM_GETAUTOURLDETECT 2.0
45 - EM_GETBIDIOPTIONS 3.0
46 - EM_GETCHARFORMAT (partly done)
47 - EM_GETEDITSTYLE
48 + EM_GETEVENTMASK
49 + EM_GETFIRSTVISIBLELINE (can be optimized if needed)
50 - EM_GETIMECOLOR 1.0asian
51 - EM_GETIMECOMPMODE 2.0
52 - EM_GETIMEOPTIONS 1.0asian
53 - EM_GETIMESTATUS
54 - EM_GETLANGOPTIONS 2.0
55 + EM_GETLIMITTEXT
56 + EM_GETLINE
57 + EM_GETLINECOUNT returns number of rows, not of paragraphs
58 + EM_GETMODIFY
59 - EM_GETOLEINTERFACE
60 + EM_GETOPTIONS
61 + EM_GETPARAFORMAT
62 + EM_GETPASSWORDCHAR 2.0
63 - EM_GETPUNCTUATION 1.0asian
64 + EM_GETRECT
65 - EM_GETREDONAME 2.0
66 + EM_GETSEL
67 + EM_GETSELTEXT (ANSI&Unicode)
68 + EM_GETSCROLLPOS 3.0 (only Y value valid)
69 ! - EM_GETTHUMB
70 + EM_GETTEXTEX 2.0
71 + EM_GETTEXTLENGTHEX (GTL_PRECISE unimplemented)
72 - EM_GETTEXTMODE 2.0
73 ? + EM_GETTEXTRANGE (ANSI&Unicode)
74 - EM_GETTYPOGRAPHYOPTIONS 3.0
75 - EM_GETUNDONAME
76 + EM_GETWORDBREAKPROC
77 - EM_GETWORDBREAKPROCEX
78 - EM_GETWORDWRAPMODE 1.0asian
79 + EM_GETZOOM 3.0
80 + EM_HIDESELECTION
81 + EM_LIMITTEXT (Also called EM_SETLIMITTEXT)
82 + EM_LINEFROMCHAR
83 + EM_LINEINDEX
84 + EM_LINELENGTH
85 + EM_LINESCROLL
86 - EM_PASTESPECIAL
87 + EM_POSFROMCHAR
88 + EM_REDO 2.0
89 + EM_REQUESTRESIZE
90 + EM_REPLACESEL (proper style?) ANSI&Unicode
91 + EM_SCROLL
92 + EM_SCROLLCARET
93 - EM_SELECTIONTYPE
94 - EM_SETBIDIOPTIONS 3.0
95 + EM_SETBKGNDCOLOR
96 + EM_SETCHARFORMAT (partly done, no ANSI)
97 - EM_SETEDITSTYLE
98 + EM_SETEVENTMASK (few notifications supported)
99 - EM_SETFONTSIZE
100 - EM_SETIMECOLOR 1.0asian
101 - EM_SETIMEOPTIONS 1.0asian
102 - EM_SETLANGOPTIONS 2.0
103 - EM_SETLIMITTEXT
104 + EM_SETMODIFY (not sure if implementation is correct)
105 - EM_SETOLECALLBACK
106 + EM_SETOPTIONS (partially implemented)
107 - EM_SETPALETTE 2.0
108 + EM_SETPARAFORMAT
109 + EM_SETPASSWORDCHAR 2.0
110 - EM_SETPUNCTUATION 1.0asian
111 + EM_SETREADONLY no beep on modification attempt
112 + EM_SETRECT
113 + EM_SETRECTNP (EM_SETRECT without repainting)
114 + EM_SETSEL
115 + EM_SETSCROLLPOS 3.0
116 - EM_SETTABSTOPS 3.0
117 - EM_SETTARGETDEVICE (partial)
118 + EM_SETTEXTEX 3.0 (proper style?)
119 - EM_SETTEXTMODE 2.0
120 - EM_SETTYPOGRAPHYOPTIONS 3.0
121 + EM_SETUNDOLIMIT 2.0
122 + EM_SETWORDBREAKPROC (used only for word movement at the moment)
123 - EM_SETWORDBREAKPROCEX
124 - EM_SETWORDWRAPMODE 1.0asian
125 + EM_SETZOOM 3.0
126 + EM_SHOWSCROLLBAR 2.0
127 - EM_STOPGROUPTYPING 2.0
128 + EM_STREAMIN
129 + EM_STREAMOUT
130 + EM_UNDO
131 + WM_CHAR
132 + WM_CLEAR
133 + WM_COPY
134 + WM_CUT
135 + WM_GETDLGCODE (the current implementation is incomplete)
136 + WM_GETTEXT (ANSI&Unicode)
137 + WM_GETTEXTLENGTH (ANSI version sucks)
138 + WM_PASTE
139 + WM_SETFONT
140 + WM_SETTEXT (resets undo stack !) (proper style?) ANSI&Unicode
141 - WM_STYLECHANGING
142 - WM_STYLECHANGED (things like read-only flag)
143 - WM_UNICHAR
145 Notifications
147 * EN_CHANGE (sent from the wrong place)
148 - EN_CORRECTTEXT
149 - EN_DROPFILES
150 - EN_ERRSPACE
151 - EN_HSCROLL
152 - EN_IMECHANGE
153 + EN_KILLFOCUS
154 - EN_LINK
155 - EN_MAXTEXT
156 - EN_MSGFILTER
157 - EN_OLEOPFAILED
158 - EN_PROTECTED
159 + EN_REQUESTRESIZE
160 - EN_SAVECLIPBOARD
161 + EN_SELCHANGE
162 + EN_SETFOCUS
163 - EN_STOPNOUNDO
164 * EN_UPDATE (sent from the wrong place)
165 - EN_VSCROLL
167 Styles
169 - ES_AUTOHSCROLL
170 - ES_AUTOVSCROLL
171 - ES_CENTER
172 + ES_DISABLENOSCROLL (scrollbar is always visible)
173 - ES_EX_NOCALLOLEINIT
174 - ES_LEFT
175 - ES_MULTILINE (currently single line controls aren't supported)
176 - ES_NOIME
177 - ES_READONLY (I'm not sure if beeping is the proper behaviour)
178 - ES_RIGHT
179 - ES_SAVESEL
180 - ES_SELFIME
181 - ES_SUNKEN
182 - ES_VERTICAL
183 - ES_WANTRETURN (don't know how to do WM_GETDLGCODE part)
184 - WS_SETFONT
185 - WS_HSCROLL
186 + WS_VSCROLL
190 * RICHED20 TODO (incomplete):
192 * - messages/styles/notifications listed above
193 * - Undo coalescing
194 * - add remaining CHARFORMAT/PARAFORMAT fields
195 * - right/center align should strip spaces from the beginning
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
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?
225 #include "editor.h"
226 #include "commdlg.h"
227 #include "winreg.h"
228 #define NO_SHLWAPI_STREAM
229 #include "shlwapi.h"
230 #include "rtf.h"
231 #include "imm.h"
232 #include "res.h"
234 #define STACK_SIZE_DEFAULT 100
235 #define STACK_SIZE_MAX 1000
237 #define TEXT_LIMIT_DEFAULT 32767
239 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
241 static BOOL ME_RegisterEditorClass(HINSTANCE);
243 static const WCHAR RichEdit20W[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '2', '0', 'W', 0};
244 static const WCHAR RichEdit50W[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '5', '0', 'W', 0};
245 static const WCHAR REListBox20W[] = {'R','E','L','i','s','t','B','o','x','2','0','W', 0};
246 static const WCHAR REComboBox20W[] = {'R','E','C','o','m','b','o','B','o','x','2','0','W', 0};
247 static HCURSOR hLeft;
249 int me_debug = 0;
250 HANDLE me_heap = NULL;
252 static BOOL ME_ListBoxRegistered = FALSE;
253 static BOOL ME_ComboBoxRegistered = FALSE;
255 static inline int is_version_nt(void)
257 return !(GetVersion() & 0x80000000);
260 static ME_TextBuffer *ME_MakeText(void) {
262 ME_TextBuffer *buf = ALLOC_OBJ(ME_TextBuffer);
264 ME_DisplayItem *p1 = ME_MakeDI(diTextStart);
265 ME_DisplayItem *p2 = ME_MakeDI(diTextEnd);
267 p1->prev = NULL;
268 p1->next = p2;
269 p2->prev = p1;
270 p2->next = NULL;
271 p1->member.para.next_para = p2;
272 p2->member.para.prev_para = p1;
273 p2->member.para.nCharOfs = 0;
275 buf->pFirst = p1;
276 buf->pLast = p2;
277 buf->pCharStyle = NULL;
279 return buf;
283 static LRESULT ME_StreamInText(ME_TextEditor *editor, DWORD dwFormat, ME_InStream *stream, ME_Style *style)
285 WCHAR wszText[STREAMIN_BUFFER_SIZE+1];
286 WCHAR *pText;
288 TRACE("%08x %p\n", dwFormat, stream);
290 do {
291 long nWideChars = 0;
293 if (!stream->dwSize)
295 ME_StreamInFill(stream);
296 if (stream->editstream->dwError)
297 break;
298 if (!stream->dwSize)
299 break;
302 if (!(dwFormat & SF_UNICODE))
304 /* FIXME? this is doomed to fail on true MBCS like UTF-8, luckily they're unlikely to be used as CP_ACP */
305 nWideChars = MultiByteToWideChar(CP_ACP, 0, stream->buffer, stream->dwSize, wszText, STREAMIN_BUFFER_SIZE);
306 pText = wszText;
308 else
310 nWideChars = stream->dwSize >> 1;
311 pText = (WCHAR *)stream->buffer;
314 ME_InsertTextFromCursor(editor, 0, pText, nWideChars, style);
315 if (stream->dwSize == 0)
316 break;
317 stream->dwSize = 0;
318 } while(1);
319 ME_CommitUndo(editor);
320 ME_UpdateRepaint(editor);
321 return 0;
324 static void ME_RTFCharAttrHook(RTF_Info *info)
326 CHARFORMAT2W fmt;
327 fmt.cbSize = sizeof(fmt);
328 fmt.dwMask = 0;
329 fmt.dwEffects = 0;
331 switch(info->rtfMinor)
333 case rtfPlain:
334 /* FIXME add more flags once they're implemented */
335 fmt.dwMask = CFM_BOLD | CFM_ITALIC | CFM_UNDERLINETYPE | CFM_STRIKEOUT | CFM_COLOR | CFM_BACKCOLOR | CFM_SIZE | CFM_WEIGHT;
336 fmt.dwEffects = CFE_AUTOCOLOR | CFE_AUTOBACKCOLOR;
337 fmt.yHeight = 12*20; /* 12pt */
338 fmt.wWeight = FW_NORMAL;
339 fmt.bUnderlineType = CFU_UNDERLINENONE;
340 break;
341 case rtfBold:
342 fmt.dwMask = CFM_BOLD | CFM_WEIGHT;
343 fmt.dwEffects = info->rtfParam ? CFE_BOLD : 0;
344 fmt.wWeight = info->rtfParam ? FW_BOLD : FW_NORMAL;
345 break;
346 case rtfItalic:
347 fmt.dwMask = CFM_ITALIC;
348 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
349 break;
350 case rtfUnderline:
351 fmt.dwMask = CFM_UNDERLINETYPE;
352 fmt.bUnderlineType = info->rtfParam ? CFU_CF1UNDERLINE : CFU_UNDERLINENONE;
353 break;
354 case rtfDotUnderline:
355 fmt.dwMask = CFM_UNDERLINETYPE;
356 fmt.bUnderlineType = info->rtfParam ? CFU_UNDERLINEDOTTED : CFU_UNDERLINENONE;
357 break;
358 case rtfDbUnderline:
359 fmt.dwMask = CFM_UNDERLINETYPE;
360 fmt.bUnderlineType = info->rtfParam ? CFU_UNDERLINEDOUBLE : CFU_UNDERLINENONE;
361 break;
362 case rtfWordUnderline:
363 fmt.dwMask = CFM_UNDERLINETYPE;
364 fmt.bUnderlineType = info->rtfParam ? CFU_UNDERLINEWORD : CFU_UNDERLINENONE;
365 break;
366 case rtfNoUnderline:
367 fmt.dwMask = CFM_UNDERLINETYPE;
368 fmt.bUnderlineType = CFU_UNDERLINENONE;
369 break;
370 case rtfStrikeThru:
371 fmt.dwMask = CFM_STRIKEOUT;
372 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
373 break;
374 case rtfSubScript:
375 case rtfSuperScript:
376 case rtfSubScrShrink:
377 case rtfSuperScrShrink:
378 case rtfNoSuperSub:
379 fmt.dwMask = CFM_SUBSCRIPT|CFM_SUPERSCRIPT;
380 if (info->rtfMinor == rtfSubScrShrink) fmt.dwEffects = CFE_SUBSCRIPT;
381 if (info->rtfMinor == rtfSuperScrShrink) fmt.dwEffects = CFE_SUPERSCRIPT;
382 if (info->rtfMinor == rtfNoSuperSub) fmt.dwEffects = 0;
383 break;
384 case rtfInvisible:
385 fmt.dwMask = CFM_HIDDEN;
386 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
387 break;
388 case rtfBackColor:
389 fmt.dwMask = CFM_BACKCOLOR;
390 fmt.dwEffects = 0;
391 if (info->rtfParam == 0)
392 fmt.dwEffects = CFE_AUTOBACKCOLOR;
393 else if (info->rtfParam != rtfNoParam)
395 RTFColor *c = RTFGetColor(info, info->rtfParam);
396 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
398 break;
399 case rtfForeColor:
400 fmt.dwMask = CFM_COLOR;
401 fmt.dwEffects = 0;
402 if (info->rtfParam == 0)
403 fmt.dwEffects = CFE_AUTOCOLOR;
404 else if (info->rtfParam != rtfNoParam)
406 RTFColor *c = RTFGetColor(info, info->rtfParam);
407 if (c)
408 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
409 else
410 fmt.crTextColor = 0;
412 break;
413 case rtfFontNum:
414 if (info->rtfParam != rtfNoParam)
416 RTFFont *f = RTFGetFont(info, info->rtfParam);
417 if (f)
419 MultiByteToWideChar(CP_ACP, 0, f->rtfFName, -1, fmt.szFaceName, sizeof(fmt.szFaceName)/sizeof(WCHAR));
420 fmt.szFaceName[sizeof(fmt.szFaceName)/sizeof(WCHAR)-1] = '\0';
421 fmt.bCharSet = f->rtfFCharSet;
422 fmt.dwMask = CFM_FACE | CFM_CHARSET;
423 fmt.bPitchAndFamily = f->rtfFPitch | (f->rtfFFamily << 4);
426 break;
427 case rtfFontSize:
428 fmt.dwMask = CFM_SIZE;
429 if (info->rtfParam != rtfNoParam)
430 fmt.yHeight = info->rtfParam*10;
431 break;
433 if (fmt.dwMask) {
434 ME_Style *style2;
435 RTFFlushOutputBuffer(info);
436 /* FIXME too slow ? how come ? */
437 style2 = ME_ApplyStyle(info->style, &fmt);
438 ME_ReleaseStyle(info->style);
439 info->style = style2;
440 info->styleChanged = TRUE;
444 /* FIXME this function doesn't get any information about context of the RTF tag, which is very bad,
445 the same tags mean different things in different contexts */
446 static void ME_RTFParAttrHook(RTF_Info *info)
448 PARAFORMAT2 fmt;
449 fmt.cbSize = sizeof(fmt);
450 fmt.dwMask = 0;
452 switch(info->rtfMinor)
454 case rtfParDef: /* restores default paragraph attributes */
455 fmt.dwMask = PFM_ALIGNMENT | PFM_BORDER | PFM_LINESPACING | PFM_TABSTOPS | PFM_OFFSET |
456 PFM_RIGHTINDENT | PFM_SPACEAFTER | PFM_SPACEBEFORE | PFM_STARTINDENT;
457 /* TODO: numbering, shading */
458 fmt.wAlignment = PFA_LEFT;
459 fmt.cTabCount = 0;
460 fmt.dxOffset = fmt.dxStartIndent = fmt.dxRightIndent = 0;
461 fmt.wBorderWidth = fmt.wBorders = 0;
462 fmt.wBorderSpace = 0;
463 fmt.bLineSpacingRule = 0;
464 fmt.dySpaceBefore = fmt.dySpaceAfter = 0;
465 fmt.dyLineSpacing = 0;
466 RTFFlushOutputBuffer(info);
467 ME_GetParagraph(info->editor->pCursors[0].pRun)->member.para.bTable = FALSE;
468 break;
469 case rtfInTable:
471 ME_DisplayItem *para;
473 RTFFlushOutputBuffer(info);
474 para = ME_GetParagraph(info->editor->pCursors[0].pRun);
475 assert(para->member.para.pCells);
476 para->member.para.bTable = TRUE;
477 return;
479 case rtfFirstIndent:
480 ME_GetSelectionParaFormat(info->editor, &fmt);
481 fmt.dwMask |= PFM_STARTINDENT | PFM_OFFSET;
482 fmt.dxStartIndent += info->rtfParam;
483 fmt.dxOffset = -info->rtfParam;
484 break;
485 case rtfLeftIndent:
486 /* we assume rtfLeftIndent is always specified before rtfFirstIndent */
487 ME_GetSelectionParaFormat(info->editor, &fmt);
488 fmt.dwMask |= PFM_STARTINDENT;
489 fmt.dxStartIndent = info->rtfParam;
490 fmt.dxOffset = 0;
491 break;
492 case rtfRightIndent:
493 fmt.dwMask = PFM_RIGHTINDENT;
494 fmt.dxRightIndent = info->rtfParam;
495 break;
496 case rtfQuadLeft:
497 case rtfQuadJust:
498 fmt.dwMask = PFM_ALIGNMENT;
499 fmt.wAlignment = PFA_LEFT;
500 break;
501 case rtfQuadRight:
502 fmt.dwMask = PFM_ALIGNMENT;
503 fmt.wAlignment = PFA_RIGHT;
504 break;
505 case rtfQuadCenter:
506 fmt.dwMask = PFM_ALIGNMENT;
507 fmt.wAlignment = PFA_CENTER;
508 break;
509 case rtfTabPos:
510 ME_GetSelectionParaFormat(info->editor, &fmt);
511 if (!(fmt.dwMask & PFM_TABSTOPS))
513 fmt.dwMask |= PFM_TABSTOPS;
514 fmt.cTabCount = 0;
516 if (fmt.cTabCount < MAX_TAB_STOPS)
517 fmt.rgxTabs[fmt.cTabCount++] = info->rtfParam;
518 break;
519 case rtfKeep:
520 fmt.dwMask = PFM_KEEP;
521 fmt.wEffects = PFE_KEEP;
522 break;
523 case rtfNoWidowControl:
524 fmt.dwMask = PFM_NOWIDOWCONTROL;
525 fmt.wEffects = PFE_NOWIDOWCONTROL;
526 break;
527 case rtfKeepNext:
528 fmt.dwMask = PFM_KEEPNEXT;
529 fmt.wEffects = PFE_KEEPNEXT;
530 break;
531 case rtfSpaceAfter:
532 fmt.dwMask = PFM_SPACEAFTER;
533 fmt.dySpaceAfter = info->rtfParam;
534 break;
535 case rtfSpaceBefore:
536 fmt.dwMask = PFM_SPACEBEFORE;
537 fmt.dySpaceBefore = info->rtfParam;
538 break;
539 case rtfSpaceBetween:
540 fmt.dwMask = PFM_LINESPACING;
541 if ((int)info->rtfParam > 0)
543 fmt.dyLineSpacing = info->rtfParam;
544 fmt.bLineSpacingRule = 3;
546 else
548 fmt.dyLineSpacing = info->rtfParam;
549 fmt.bLineSpacingRule = 4;
551 case rtfSpaceMultiply:
552 fmt.dwMask = PFM_LINESPACING;
553 fmt.dyLineSpacing = info->rtfParam * 20;
554 fmt.bLineSpacingRule = 5;
555 break;
556 case rtfParBullet:
557 fmt.dwMask = PFM_NUMBERING;
558 fmt.wNumbering = PFN_BULLET;
559 break;
560 case rtfParSimple:
561 fmt.dwMask = PFM_NUMBERING;
562 fmt.wNumbering = 2; /* FIXME: MSDN says it's not used ?? */
563 break;
564 case rtfParNumDecimal:
565 fmt.dwMask = PFM_NUMBERING;
566 fmt.wNumbering = 2; /* FIXME: MSDN says it's not used ?? */
567 break;
568 case rtfParNumIndent:
569 fmt.dwMask = PFM_NUMBERINGTAB;
570 fmt.wNumberingTab = info->rtfParam;
571 break;
572 case rtfParNumStartAt:
573 fmt.dwMask = PFM_NUMBERINGSTART;
574 fmt.wNumberingStart = info->rtfParam;
575 break;
576 case rtfBorderLeft:
577 ME_GetSelectionParaFormat(info->editor, &fmt);
578 if (!(fmt.dwMask & PFM_BORDER))
580 fmt.dwMask |= PFM_BORDER;
581 fmt.wBorderSpace = 0;
582 fmt.wBorderWidth = 1;
583 fmt.wBorders = 0;
585 fmt.wBorders |= 1;
586 break;
587 case rtfBorderRight:
588 ME_GetSelectionParaFormat(info->editor, &fmt);
589 if (!(fmt.dwMask & PFM_BORDER))
591 fmt.dwMask |= PFM_BORDER;
592 fmt.wBorderSpace = 0;
593 fmt.wBorderWidth = 1;
594 fmt.wBorders = 0;
596 fmt.wBorders |= 2;
597 break;
598 case rtfBorderTop:
599 ME_GetSelectionParaFormat(info->editor, &fmt);
600 if (!(fmt.dwMask & PFM_BORDER))
602 fmt.dwMask |= PFM_BORDER;
603 fmt.wBorderSpace = 0;
604 fmt.wBorderWidth = 1;
605 fmt.wBorders = 0;
607 fmt.wBorders |= 4;
608 break;
609 case rtfBorderBottom:
610 ME_GetSelectionParaFormat(info->editor, &fmt);
611 if (!(fmt.dwMask & PFM_BORDER))
613 fmt.dwMask |= PFM_BORDER;
614 fmt.wBorderSpace = 0;
615 fmt.wBorderWidth = 1;
616 fmt.wBorders = 0;
618 fmt.wBorders |= 8;
619 break;
620 case rtfBorderSingle:
621 ME_GetSelectionParaFormat(info->editor, &fmt);
622 /* we assume that borders have been created before (RTF spec) */
623 fmt.wBorders &= ~0x70;
624 fmt.wBorders |= 1 << 8;
625 break;
626 case rtfBorderThick:
627 ME_GetSelectionParaFormat(info->editor, &fmt);
628 /* we assume that borders have been created before (RTF spec) */
629 fmt.wBorders &= ~0x70;
630 fmt.wBorders |= 2 << 8;
631 break;
632 case rtfBorderShadow:
633 ME_GetSelectionParaFormat(info->editor, &fmt);
634 /* we assume that borders have been created before (RTF spec) */
635 fmt.wBorders &= ~0x70;
636 fmt.wBorders |= 10 << 8;
637 break;
638 case rtfBorderDouble:
639 ME_GetSelectionParaFormat(info->editor, &fmt);
640 /* we assume that borders have been created before (RTF spec) */
641 fmt.wBorders &= ~0x70;
642 fmt.wBorders |= 7 << 8;
643 break;
644 case rtfBorderDot:
645 ME_GetSelectionParaFormat(info->editor, &fmt);
646 /* we assume that borders have been created before (RTF spec) */
647 fmt.wBorders &= ~0x70;
648 fmt.wBorders |= 11 << 8;
649 break;
650 case rtfBorderWidth:
651 ME_GetSelectionParaFormat(info->editor, &fmt);
652 /* we assume that borders have been created before (RTF spec) */
653 fmt.wBorderWidth |= ((info->rtfParam / 15) & 7) << 8;
654 break;
655 case rtfBorderSpace:
656 ME_GetSelectionParaFormat(info->editor, &fmt);
657 /* we assume that borders have been created before (RTF spec) */
658 fmt.wBorderSpace = info->rtfParam;
659 break;
661 if (fmt.dwMask) {
662 RTFFlushOutputBuffer(info);
663 /* FIXME too slow ? how come ?*/
664 ME_SetSelectionParaFormat(info->editor, &fmt);
668 static void ME_RTFTblAttrHook(RTF_Info *info)
670 ME_DisplayItem *para;
672 switch (info->rtfMinor)
674 case rtfRowDef:
675 RTFFlushOutputBuffer(info);
676 para = ME_GetParagraph(info->editor->pCursors[0].pRun);
678 /* Release possibly inherited cell definitions */
679 ME_DestroyTableCellList(para);
681 para->member.para.pCells = ALLOC_OBJ(ME_TableCell);
682 para->member.para.pCells->nRightBoundary = 0;
683 para->member.para.pCells->next = NULL;
684 para->member.para.pLastCell = para->member.para.pCells;
685 break;
686 case rtfCellPos:
687 RTFFlushOutputBuffer(info);
688 para = ME_GetParagraph(info->editor->pCursors[0].pRun);
690 if (para->member.para.pLastCell->nRightBoundary)
692 ME_TableCell *pCell = ALLOC_OBJ(ME_TableCell);
694 pCell->next = NULL;
695 para->member.para.pLastCell->next = pCell;
696 para->member.para.pLastCell = pCell;
698 para->member.para.pLastCell->nRightBoundary = info->rtfParam;
699 break;
703 static BOOL ME_RTFInsertOleObject(RTF_Info *info, HENHMETAFILE hemf, HBITMAP hbmp,
704 const SIZEL* sz)
706 LPOLEOBJECT lpObject = NULL;
707 LPSTORAGE lpStorage = NULL;
708 LPOLECLIENTSITE lpClientSite = NULL;
709 LPDATAOBJECT lpDataObject = NULL;
710 LPOLECACHE lpOleCache = NULL;
711 STGMEDIUM stgm;
712 FORMATETC fm;
713 CLSID clsid;
714 BOOL ret = FALSE;
715 DWORD conn;
717 if (hemf)
719 stgm.tymed = TYMED_ENHMF;
720 stgm.u.hEnhMetaFile = hemf;
721 fm.cfFormat = CF_ENHMETAFILE;
723 else if (hbmp)
725 stgm.tymed = TYMED_GDI;
726 stgm.u.hBitmap = hbmp;
727 fm.cfFormat = CF_BITMAP;
729 stgm.pUnkForRelease = NULL;
731 fm.ptd = NULL;
732 fm.dwAspect = DVASPECT_CONTENT;
733 fm.lindex = -1;
734 fm.tymed = stgm.tymed;
736 if (!info->lpRichEditOle)
738 CreateIRichEditOle(info->editor, (VOID**)&info->lpRichEditOle);
741 if (OleCreateDefaultHandler(&CLSID_NULL, NULL, &IID_IOleObject, (void**)&lpObject) == S_OK &&
742 #if 0
743 /* FIXME: enable it when rich-edit properly implements this method */
744 IRichEditOle_GetClientSite(info->lpRichEditOle, &lpClientSite) == S_OK &&
745 IOleObject_SetClientSite(lpObject, lpClientSite) == S_OK &&
746 #endif
747 IOleObject_GetUserClassID(lpObject, &clsid) == S_OK &&
748 IOleObject_QueryInterface(lpObject, &IID_IOleCache, (void**)&lpOleCache) == S_OK &&
749 IOleCache_Cache(lpOleCache, &fm, 0, &conn) == S_OK &&
750 IOleObject_QueryInterface(lpObject, &IID_IDataObject, (void**)&lpDataObject) == S_OK &&
751 IDataObject_SetData(lpDataObject, &fm, &stgm, TRUE) == S_OK)
753 REOBJECT reobject;
755 reobject.cbStruct = sizeof(reobject);
756 reobject.cp = REO_CP_SELECTION;
757 reobject.clsid = clsid;
758 reobject.poleobj = lpObject;
759 reobject.pstg = lpStorage;
760 reobject.polesite = lpClientSite;
761 /* convert from twips to .01 mm */
762 reobject.sizel.cx = MulDiv(sz->cx, 254, 144);
763 reobject.sizel.cy = MulDiv(sz->cy, 254, 144);
764 reobject.dvaspect = DVASPECT_CONTENT;
765 reobject.dwFlags = 0; /* FIXME */
766 reobject.dwUser = 0;
768 /* FIXME: could be simpler */
769 ret = IRichEditOle_InsertObject(info->lpRichEditOle, &reobject) == S_OK;
772 if (lpObject) IOleObject_Release(lpObject);
773 if (lpClientSite) IOleClientSite_Release(lpClientSite);
774 if (lpStorage) IStorage_Release(lpStorage);
775 if (lpDataObject) IDataObject_Release(lpDataObject);
776 if (lpOleCache) IOleCache_Release(lpOleCache);
778 return ret;
781 static void ME_RTFReadPictGroup(RTF_Info *info)
783 SIZEL sz;
784 BYTE* buffer = NULL;
785 unsigned bufsz, bufidx;
786 BOOL flip;
787 BYTE val;
788 METAFILEPICT mfp;
789 HENHMETAFILE hemf;
790 HBITMAP hbmp;
791 enum gfxkind {gfx_unknown = 0, gfx_enhmetafile, gfx_metafile, gfx_dib} gfx = gfx_unknown;
793 RTFGetToken (info);
794 if (info->rtfClass == rtfEOF)
795 return;
796 mfp.mm = MM_TEXT;
797 /* fetch picture type */
798 if (RTFCheckMM (info, rtfPictAttr, rtfWinMetafile))
800 mfp.mm = info->rtfParam;
801 gfx = gfx_metafile;
803 else if (RTFCheckMM (info, rtfPictAttr, rtfDevIndBitmap))
805 if (info->rtfParam != 0) FIXME("dibitmap should be 0 (%d)\n", info->rtfParam);
806 gfx = gfx_dib;
808 else if (RTFCheckMM (info, rtfPictAttr, rtfEmfBlip))
810 gfx = gfx_enhmetafile;
812 else
814 FIXME("%d %d\n", info->rtfMajor, info->rtfMinor);
815 goto skip_group;
817 sz.cx = sz.cy = 0;
818 /* fetch picture attributes */
819 for (;;)
821 RTFGetToken (info);
822 if (info->rtfClass == rtfEOF)
823 return;
824 if (info->rtfClass == rtfText)
825 break;
826 if (!RTFCheckCM (info, rtfControl, rtfPictAttr))
828 ERR("Expected picture attribute (%d %d)\n",
829 info->rtfClass, info->rtfMajor);
830 goto skip_group;
832 else if (RTFCheckMM (info, rtfPictAttr, rtfPicWid))
834 if (gfx == gfx_metafile) mfp.xExt = info->rtfParam;
836 else if (RTFCheckMM (info, rtfPictAttr, rtfPicHt))
838 if (gfx == gfx_metafile) mfp.yExt = info->rtfParam;
840 else if (RTFCheckMM (info, rtfPictAttr, rtfPicGoalWid))
841 sz.cx = info->rtfParam;
842 else if (RTFCheckMM (info, rtfPictAttr, rtfPicGoalHt))
843 sz.cy = info->rtfParam;
844 else
845 FIXME("Non supported attribute: %d %d %d\n", info->rtfClass, info->rtfMajor, info->rtfMinor);
847 /* fetch picture data */
848 bufsz = 1024;
849 bufidx = 0;
850 buffer = HeapAlloc(GetProcessHeap(), 0, bufsz);
851 val = info->rtfMajor;
852 for (flip = TRUE;; flip = !flip)
854 RTFGetToken (info);
855 if (info->rtfClass == rtfEOF)
857 HeapFree(GetProcessHeap(), 0, buffer);
858 return; /* Warn ?? */
860 if (RTFCheckCM(info, rtfGroup, rtfEndGroup))
861 break;
862 if (info->rtfClass != rtfText) goto skip_group;
863 if (flip)
865 if (bufidx >= bufsz &&
866 !(buffer = HeapReAlloc(GetProcessHeap(), 0, buffer, bufsz += 1024)))
867 goto skip_group;
868 buffer[bufidx++] = RTFCharToHex(val) * 16 + RTFCharToHex(info->rtfMajor);
870 else
871 val = info->rtfMajor;
873 if (flip) FIXME("wrong hex string\n");
875 switch (gfx)
877 case gfx_enhmetafile:
878 if ((hemf = SetEnhMetaFileBits(bufidx, buffer)))
879 ME_RTFInsertOleObject(info, hemf, NULL, &sz);
880 break;
881 case gfx_metafile:
882 if ((hemf = SetWinMetaFileBits(bufidx, buffer, NULL, &mfp)))
883 ME_RTFInsertOleObject(info, hemf, NULL, &sz);
884 break;
885 case gfx_dib:
887 BITMAPINFO* bi = (BITMAPINFO*)buffer;
888 HDC hdc = GetDC(0);
889 unsigned nc = bi->bmiHeader.biClrUsed;
891 /* not quite right, especially for bitfields type of compression */
892 if (!nc && bi->bmiHeader.biBitCount <= 8)
893 nc = 1 << bi->bmiHeader.biBitCount;
894 if ((hbmp = CreateDIBitmap(hdc, &bi->bmiHeader,
895 CBM_INIT, (char*)(bi + 1) + nc * sizeof(RGBQUAD),
896 bi, DIB_RGB_COLORS)))
897 ME_RTFInsertOleObject(info, NULL, hbmp, &sz);
898 ReleaseDC(0, hdc);
900 break;
901 default:
902 break;
904 HeapFree(GetProcessHeap(), 0, buffer);
905 RTFRouteToken (info); /* feed "}" back to router */
906 return;
907 skip_group:
908 HeapFree(GetProcessHeap(), 0, buffer);
909 RTFSkipGroup(info);
910 RTFRouteToken(info); /* feed "}" back to router */
913 /* for now, lookup the \result part and use it, whatever the object */
914 static void ME_RTFReadObjectGroup(RTF_Info *info)
916 for (;;)
918 RTFGetToken (info);
919 if (info->rtfClass == rtfEOF)
920 return;
921 if (RTFCheckCM(info, rtfGroup, rtfEndGroup))
922 break;
923 if (RTFCheckCM(info, rtfGroup, rtfBeginGroup))
925 RTFGetToken (info);
926 if (info->rtfClass == rtfEOF)
927 return;
928 if (RTFCheckCMM(info, rtfControl, rtfDestination, rtfObjResult))
930 int level = 1;
932 while (RTFGetToken (info) != rtfEOF)
934 if (info->rtfClass == rtfGroup)
936 if (info->rtfMajor == rtfBeginGroup) level++;
937 else if (info->rtfMajor == rtfEndGroup && --level < 0) break;
939 RTFRouteToken(info);
942 else RTFSkipGroup(info);
943 continue;
945 if (!RTFCheckCM (info, rtfControl, rtfObjAttr))
947 FIXME("Non supported attribute: %d %d %d\n", info->rtfClass, info->rtfMajor, info->rtfMinor);
948 return;
951 RTFRouteToken(info); /* feed "}" back to router */
954 static void ME_RTFReadHook(RTF_Info *info) {
955 switch(info->rtfClass)
957 case rtfGroup:
958 switch(info->rtfMajor)
960 case rtfBeginGroup:
961 if (info->stackTop < maxStack) {
962 info->stack[info->stackTop].fmt = info->style->fmt;
963 info->stack[info->stackTop].codePage = info->codePage;
964 info->stack[info->stackTop].unicodeLength = info->unicodeLength;
966 info->stackTop++;
967 info->styleChanged = FALSE;
968 break;
969 case rtfEndGroup:
971 ME_Style *s;
972 RTFFlushOutputBuffer(info);
973 if (info->stackTop<=1) {
974 info->rtfClass = rtfEOF;
975 return;
977 info->stackTop--;
978 assert(info->stackTop >= 0);
979 if (info->styleChanged)
981 /* FIXME too slow ? how come ? */
982 s = ME_ApplyStyle(info->style, &info->stack[info->stackTop].fmt);
983 ME_ReleaseStyle(info->style);
984 info->style = s;
985 info->codePage = info->stack[info->stackTop].codePage;
986 info->unicodeLength = info->stack[info->stackTop].unicodeLength;
988 break;
991 break;
992 case rtfControl:
993 switch(info->rtfMajor)
995 case rtfCharAttr:
996 ME_RTFCharAttrHook(info);
997 break;
998 case rtfParAttr:
999 ME_RTFParAttrHook(info);
1000 break;
1001 case rtfTblAttr:
1002 ME_RTFTblAttrHook(info);
1003 break;
1004 case rtfSpecialChar:
1005 if (info->rtfMinor == rtfCell)
1007 RTFFlushOutputBuffer(info);
1008 ME_InsertTableCellFromCursor(info->editor, 0);
1011 break;
1015 void
1016 ME_StreamInFill(ME_InStream *stream)
1018 stream->editstream->dwError = stream->editstream->pfnCallback(stream->editstream->dwCookie,
1019 (BYTE *)stream->buffer,
1020 sizeof(stream->buffer),
1021 (LONG *)&stream->dwSize);
1022 stream->dwUsed = 0;
1025 static LRESULT ME_StreamIn(ME_TextEditor *editor, DWORD format, EDITSTREAM *stream, BOOL stripLastCR)
1027 RTF_Info parser;
1028 ME_Style *style;
1029 int from, to, to2, nUndoMode;
1030 int nEventMask = editor->nEventMask;
1031 ME_InStream inStream;
1032 BOOL invalidRTF = FALSE;
1034 TRACE("stream==%p hWnd==%p format==0x%X\n", stream, editor->hWnd, format);
1035 editor->nEventMask = 0;
1037 ME_GetSelection(editor, &from, &to);
1038 if ((format & SFF_SELECTION) && (editor->mode & TM_RICHTEXT)) {
1039 style = ME_GetSelectionInsertStyle(editor);
1041 ME_InternalDeleteText(editor, from, to-from);
1043 else {
1044 style = editor->pBuffer->pDefaultStyle;
1045 ME_AddRefStyle(style);
1046 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
1047 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
1048 from = to = 0;
1049 ME_ClearTempStyle(editor);
1050 /* FIXME restore default paragraph formatting ! */
1054 /* Back up undo mode to a local variable */
1055 nUndoMode = editor->nUndoMode;
1057 /* Only create an undo if SFF_SELECTION is set */
1058 if (!(format & SFF_SELECTION))
1059 editor->nUndoMode = umIgnore;
1061 inStream.editstream = stream;
1062 inStream.editstream->dwError = 0;
1063 inStream.dwSize = 0;
1064 inStream.dwUsed = 0;
1066 if (format & SF_RTF)
1068 /* Check if it's really RTF, and if it is not, use plain text */
1069 ME_StreamInFill(&inStream);
1070 if (!inStream.editstream->dwError)
1072 if ((!editor->bEmulateVersion10 && strncmp(inStream.buffer, "{\\rtf", 5) && strncmp(inStream.buffer, "{\\urtf", 6))
1073 || (editor->bEmulateVersion10 && *inStream.buffer != '{'))
1075 invalidRTF = TRUE;
1076 inStream.editstream->dwError = -16;
1081 if (!invalidRTF && !inStream.editstream->dwError)
1083 if (format & SF_RTF) {
1084 /* setup the RTF parser */
1085 memset(&parser, 0, sizeof parser);
1086 RTFSetEditStream(&parser, &inStream);
1087 parser.rtfFormat = format&(SF_TEXT|SF_RTF);
1088 parser.hwndEdit = editor->hWnd;
1089 parser.editor = editor;
1090 parser.style = style;
1091 WriterInit(&parser);
1092 RTFInit(&parser);
1093 RTFSetReadHook(&parser, ME_RTFReadHook);
1094 RTFSetDestinationCallback(&parser, rtfPict, ME_RTFReadPictGroup);
1095 RTFSetDestinationCallback(&parser, rtfObject, ME_RTFReadObjectGroup);
1096 BeginFile(&parser);
1098 /* do the parsing */
1099 RTFRead(&parser);
1100 RTFFlushOutputBuffer(&parser);
1101 RTFDestroy(&parser);
1102 if (parser.lpRichEditOle)
1103 IRichEditOle_Release(parser.lpRichEditOle);
1105 /* Remove last line break, as mandated by tests. This is not affected by
1106 CR/LF counters, since RTF streaming presents only \para tokens, which
1107 are converted according to the standard rules: \r for 2.0, \r\n for 1.0
1109 if (stripLastCR) {
1110 int newfrom, newto;
1111 ME_GetSelection(editor, &newfrom, &newto);
1112 if (newto > to + (editor->bEmulateVersion10 ? 1 : 0)) {
1113 WCHAR lastchar[3] = {'\0', '\0'};
1114 int linebreakSize = editor->bEmulateVersion10 ? 2 : 1;
1116 ME_GetTextW(editor, lastchar, newto - linebreakSize, linebreakSize, 0);
1117 if (lastchar[0] == '\r' && (lastchar[1] == '\n' || lastchar[1] == '\0')) {
1118 ME_InternalDeleteText(editor, newto - linebreakSize, linebreakSize);
1123 style = parser.style;
1125 else if (format & SF_TEXT)
1126 ME_StreamInText(editor, format, &inStream, style);
1127 else
1128 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
1129 ME_GetSelection(editor, &to, &to2);
1130 /* put the cursor at the top */
1131 if (!(format & SFF_SELECTION))
1132 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
1135 /* Restore saved undo mode */
1136 editor->nUndoMode = nUndoMode;
1138 /* even if we didn't add an undo, we need to commit anything on the stack */
1139 ME_CommitUndo(editor);
1141 /* If SFF_SELECTION isn't set, delete any undos from before we started too */
1142 if (!(format & SFF_SELECTION))
1143 ME_EmptyUndoStack(editor);
1145 ME_ReleaseStyle(style);
1146 editor->nEventMask = nEventMask;
1147 ME_UpdateRepaint(editor);
1148 if (!(format & SFF_SELECTION)) {
1149 ME_ClearTempStyle(editor);
1151 ME_MoveCaret(editor);
1152 ME_SendSelChange(editor);
1153 ME_SendRequestResize(editor, FALSE);
1155 return 0;
1159 typedef struct tagME_RTFStringStreamStruct
1161 char *string;
1162 int pos;
1163 int length;
1164 } ME_RTFStringStreamStruct;
1166 static DWORD CALLBACK ME_ReadFromRTFString(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1168 ME_RTFStringStreamStruct *pStruct = (ME_RTFStringStreamStruct *)dwCookie;
1169 int count;
1171 count = min(cb, pStruct->length - pStruct->pos);
1172 memmove(lpBuff, pStruct->string + pStruct->pos, count);
1173 pStruct->pos += count;
1174 *pcb = count;
1175 return 0;
1178 static void
1179 ME_StreamInRTFString(ME_TextEditor *editor, BOOL selection, char *string)
1181 EDITSTREAM es;
1182 ME_RTFStringStreamStruct data;
1184 data.string = string;
1185 data.length = strlen(string);
1186 data.pos = 0;
1187 es.dwCookie = (DWORD)&data;
1188 es.pfnCallback = ME_ReadFromRTFString;
1189 ME_StreamIn(editor, SF_RTF | (selection ? SFF_SELECTION : 0), &es, FALSE);
1193 ME_DisplayItem *
1194 ME_FindItemAtOffset(ME_TextEditor *editor, ME_DIType nItemType, int nOffset, int *nItemOffset)
1196 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
1197 int runLength;
1199 while (item && item->member.para.next_para->member.para.nCharOfs <= nOffset)
1200 item = ME_FindItemFwd(item, diParagraph);
1202 if (!item)
1203 return item;
1205 nOffset -= item->member.para.nCharOfs;
1206 if (nItemType == diParagraph) {
1207 if (nItemOffset)
1208 *nItemOffset = nOffset;
1209 return item;
1212 do {
1213 item = ME_FindItemFwd(item, diRun);
1214 runLength = ME_StrLen(item->member.run.strText);
1215 if (item->member.run.nFlags & MERF_ENDPARA)
1216 runLength = item->member.run.nCR + item->member.run.nLF;
1217 } while (item && (item->member.run.nCharOfs + runLength <= nOffset));
1218 if (item) {
1219 nOffset -= item->member.run.nCharOfs;
1221 /* Special case: nOffset may not point exactly at the division between the
1222 \r and the \n in 1.0 emulation. If such a case happens, it is sent
1223 into the next run, if one exists
1225 if ( item->member.run.nFlags & MERF_ENDPARA
1226 && nOffset == item->member.run.nCR
1227 && item->member.run.nLF > 0) {
1228 ME_DisplayItem *nextItem;
1229 nextItem = ME_FindItemFwd(item, diRun);
1230 if (nextItem) {
1231 nOffset = 0;
1232 item = nextItem;
1235 if (nItemOffset)
1236 *nItemOffset = nOffset;
1238 return item;
1242 static int
1243 ME_FindText(ME_TextEditor *editor, DWORD flags, const CHARRANGE *chrg, const WCHAR *text, CHARRANGE *chrgText)
1245 const int nLen = lstrlenW(text);
1246 const int nTextLen = ME_GetTextLength(editor);
1247 int nStart, nEnd;
1248 int nMin, nMax;
1249 ME_DisplayItem *item;
1250 ME_DisplayItem *para;
1251 WCHAR wLastChar = ' ';
1253 TRACE("flags==0x%08x, chrg->cpMin==%d, chrg->cpMax==%d text==%s\n",
1254 flags, chrg->cpMin, chrg->cpMax, debugstr_w(text));
1256 if (flags & ~(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD))
1257 FIXME("Flags 0x%08x not implemented\n",
1258 flags & ~(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD));
1260 nMin = chrg->cpMin;
1261 if (chrg->cpMax == -1)
1262 nMax = nTextLen;
1263 else
1264 nMax = chrg->cpMax > nTextLen ? nTextLen : chrg->cpMax;
1266 /* when searching up, if cpMin < cpMax, then instead of searching
1267 * on [cpMin,cpMax], we search on [0,cpMin], otherwise, search on
1268 * [cpMax, cpMin]. The exception is when cpMax is -1, in which
1269 * case, it is always bigger than cpMin.
1271 if (!(flags & FR_DOWN))
1273 int nSwap = nMax;
1275 nMax = nMin > nTextLen ? nTextLen : nMin;
1276 if (nMin < nSwap || chrg->cpMax == -1)
1277 nMin = 0;
1278 else
1279 nMin = nSwap;
1282 if (!nLen || nMin < 0 || nMax < 0 || nMax < nMin)
1284 if (chrgText)
1285 chrgText->cpMin = chrgText->cpMax = -1;
1286 return -1;
1289 if (flags & FR_DOWN) /* Forward search */
1291 /* If possible, find the character before where the search starts */
1292 if ((flags & FR_WHOLEWORD) && nMin)
1294 nStart = nMin - 1;
1295 item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
1296 if (!item)
1298 if (chrgText)
1299 chrgText->cpMin = chrgText->cpMax = -1;
1300 return -1;
1302 wLastChar = item->member.run.strText->szData[nStart];
1305 nStart = nMin;
1306 item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
1307 if (!item)
1309 if (chrgText)
1310 chrgText->cpMin = chrgText->cpMax = -1;
1311 return -1;
1314 para = ME_GetParagraph(item);
1315 while (item
1316 && para->member.para.nCharOfs + item->member.run.nCharOfs + nStart + nLen <= nMax)
1318 ME_DisplayItem *pCurItem = item;
1319 int nCurStart = nStart;
1320 int nMatched = 0;
1322 while (pCurItem && ME_CharCompare(pCurItem->member.run.strText->szData[nCurStart + nMatched], text[nMatched], (flags & FR_MATCHCASE)))
1324 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
1325 break;
1327 nMatched++;
1328 if (nMatched == nLen)
1330 ME_DisplayItem *pNextItem = pCurItem;
1331 int nNextStart = nCurStart;
1332 WCHAR wNextChar;
1334 /* Check to see if next character is a whitespace */
1335 if (flags & FR_WHOLEWORD)
1337 if (nCurStart + nMatched == ME_StrLen(pCurItem->member.run.strText))
1339 pNextItem = ME_FindItemFwd(pCurItem, diRun);
1340 nNextStart = -nMatched;
1343 if (pNextItem)
1344 wNextChar = pNextItem->member.run.strText->szData[nNextStart + nMatched];
1345 else
1346 wNextChar = ' ';
1348 if (isalnumW(wNextChar))
1349 break;
1352 nStart += para->member.para.nCharOfs + pCurItem->member.run.nCharOfs;
1353 if (chrgText)
1355 chrgText->cpMin = nStart;
1356 chrgText->cpMax = nStart + nLen;
1358 TRACE("found at %d-%d\n", nStart, nStart + nLen);
1359 return nStart;
1361 if (nCurStart + nMatched == ME_StrLen(pCurItem->member.run.strText))
1363 pCurItem = ME_FindItemFwd(pCurItem, diRun);
1364 para = ME_GetParagraph(pCurItem);
1365 nCurStart = -nMatched;
1368 if (pCurItem)
1369 wLastChar = pCurItem->member.run.strText->szData[nCurStart + nMatched];
1370 else
1371 wLastChar = ' ';
1373 nStart++;
1374 if (nStart == ME_StrLen(item->member.run.strText))
1376 item = ME_FindItemFwd(item, diRun);
1377 para = ME_GetParagraph(item);
1378 nStart = 0;
1382 else /* Backward search */
1384 /* If possible, find the character after where the search ends */
1385 if ((flags & FR_WHOLEWORD) && nMax < nTextLen - 1)
1387 nEnd = nMax + 1;
1388 item = ME_FindItemAtOffset(editor, diRun, nEnd, &nEnd);
1389 if (!item)
1391 if (chrgText)
1392 chrgText->cpMin = chrgText->cpMax = -1;
1393 return -1;
1395 wLastChar = item->member.run.strText->szData[nEnd];
1398 nEnd = nMax;
1399 item = ME_FindItemAtOffset(editor, diRun, nEnd, &nEnd);
1400 if (!item)
1402 if (chrgText)
1403 chrgText->cpMin = chrgText->cpMax = -1;
1404 return -1;
1407 para = ME_GetParagraph(item);
1409 while (item
1410 && para->member.para.nCharOfs + item->member.run.nCharOfs + nEnd - nLen >= nMin)
1412 ME_DisplayItem *pCurItem = item;
1413 int nCurEnd = nEnd;
1414 int nMatched = 0;
1416 if (nCurEnd - nMatched == 0)
1418 pCurItem = ME_FindItemBack(pCurItem, diRun);
1419 para = ME_GetParagraph(pCurItem);
1420 nCurEnd = ME_StrLen(pCurItem->member.run.strText) + nMatched;
1423 while (pCurItem && ME_CharCompare(pCurItem->member.run.strText->szData[nCurEnd - nMatched - 1], text[nLen - nMatched - 1], (flags & FR_MATCHCASE)))
1425 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
1426 break;
1428 nMatched++;
1429 if (nMatched == nLen)
1431 ME_DisplayItem *pPrevItem = pCurItem;
1432 int nPrevEnd = nCurEnd;
1433 WCHAR wPrevChar;
1435 /* Check to see if previous character is a whitespace */
1436 if (flags & FR_WHOLEWORD)
1438 if (nPrevEnd - nMatched == 0)
1440 pPrevItem = ME_FindItemBack(pCurItem, diRun);
1441 if (pPrevItem)
1442 nPrevEnd = ME_StrLen(pPrevItem->member.run.strText) + nMatched;
1445 if (pPrevItem)
1446 wPrevChar = pPrevItem->member.run.strText->szData[nPrevEnd - nMatched - 1];
1447 else
1448 wPrevChar = ' ';
1450 if (isalnumW(wPrevChar))
1451 break;
1454 nStart = para->member.para.nCharOfs + pCurItem->member.run.nCharOfs + nCurEnd - nMatched;
1455 if (chrgText)
1457 chrgText->cpMin = nStart;
1458 chrgText->cpMax = nStart + nLen;
1460 TRACE("found at %d-%d\n", nStart, nStart + nLen);
1461 return nStart;
1463 if (nCurEnd - nMatched == 0)
1465 pCurItem = ME_FindItemBack(pCurItem, diRun);
1466 /* Don't care about pCurItem becoming NULL here; it's already taken
1467 * care of in the exterior loop condition */
1468 para = ME_GetParagraph(pCurItem);
1469 nCurEnd = ME_StrLen(pCurItem->member.run.strText) + nMatched;
1472 if (pCurItem)
1473 wLastChar = pCurItem->member.run.strText->szData[nCurEnd - nMatched - 1];
1474 else
1475 wLastChar = ' ';
1477 nEnd--;
1478 if (nEnd < 0)
1480 item = ME_FindItemBack(item, diRun);
1481 para = ME_GetParagraph(item);
1482 nEnd = ME_StrLen(item->member.run.strText);
1486 TRACE("not found\n");
1487 if (chrgText)
1488 chrgText->cpMin = chrgText->cpMax = -1;
1489 return -1;
1492 /* helper to send a msg filter notification */
1493 static BOOL
1494 ME_FilterEvent(ME_TextEditor *editor, UINT msg, WPARAM* wParam, LPARAM* lParam)
1496 MSGFILTER msgf;
1498 msgf.nmhdr.hwndFrom = editor->hWnd;
1499 msgf.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
1500 msgf.nmhdr.code = EN_MSGFILTER;
1501 msgf.msg = msg;
1503 msgf.wParam = *wParam;
1504 msgf.lParam = *lParam;
1505 if (SendMessageW(GetParent(editor->hWnd), WM_NOTIFY, msgf.nmhdr.idFrom, (LPARAM)&msgf))
1506 return FALSE;
1507 *wParam = msgf.wParam;
1508 *lParam = msgf.lParam;
1509 msgf.wParam = *wParam;
1511 return TRUE;
1514 static BOOL
1515 ME_KeyDown(ME_TextEditor *editor, WORD nKey)
1517 BOOL ctrl_is_down = GetKeyState(VK_CONTROL) & 0x8000;
1518 BOOL shift_is_down = GetKeyState(VK_SHIFT) & 0x8000;
1520 switch (nKey)
1522 case VK_LEFT:
1523 case VK_RIGHT:
1524 case VK_UP:
1525 case VK_DOWN:
1526 case VK_HOME:
1527 case VK_END:
1528 case VK_PRIOR:
1529 case VK_NEXT:
1530 ME_ArrowKey(editor, nKey, shift_is_down, ctrl_is_down);
1531 return TRUE;
1532 case VK_BACK:
1533 case VK_DELETE:
1534 /* FIXME backspace and delete aren't the same, they act different wrt paragraph style of the merged paragraph */
1535 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY)
1536 return FALSE;
1537 if (ME_IsSelection(editor))
1538 ME_DeleteSelection(editor);
1539 else if (nKey == VK_DELETE || ME_ArrowKey(editor, VK_LEFT, FALSE, FALSE))
1540 ME_DeleteTextAtCursor(editor, 1, 1);
1541 else
1542 return TRUE;
1543 ME_CommitUndo(editor);
1544 ME_UpdateRepaint(editor);
1545 ME_SendRequestResize(editor, FALSE);
1546 return TRUE;
1548 default:
1549 if (ctrl_is_down)
1551 if (nKey == 'W')
1553 CHARFORMAT2W chf;
1554 char buf[2048];
1555 chf.cbSize = sizeof(chf);
1557 ME_GetSelectionCharFormat(editor, &chf);
1558 ME_DumpStyleToBuf(&chf, buf);
1559 MessageBoxA(NULL, buf, "Style dump", MB_OK);
1561 if (nKey == 'Q')
1563 ME_CheckCharOffsets(editor);
1567 return FALSE;
1570 static BOOL ME_SetCursor(ME_TextEditor *editor, int x)
1572 if ((GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_SELECTIONBAR) &&
1573 (x < editor->selofs || editor->linesel))
1575 SetCursor(hLeft);
1576 return TRUE;
1578 return FALSE;
1581 static BOOL ME_ShowContextMenu(ME_TextEditor *editor, int x, int y)
1583 CHARRANGE selrange;
1584 HMENU menu;
1585 int seltype = 0;
1586 if(!editor->lpOleCallback)
1587 return FALSE;
1588 ME_GetSelection(editor, &selrange.cpMin, &selrange.cpMax);
1589 if(selrange.cpMin == selrange.cpMax)
1590 seltype |= SEL_EMPTY;
1591 else
1593 /* FIXME: Handle objects */
1594 seltype |= SEL_TEXT;
1595 if(selrange.cpMax-selrange.cpMin > 1)
1596 seltype |= SEL_MULTICHAR;
1598 if(SUCCEEDED(IRichEditOleCallback_GetContextMenu(editor->lpOleCallback, seltype, NULL, &selrange, &menu)))
1600 TrackPopupMenu(menu, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, GetParent(editor->hWnd), NULL);
1601 DestroyMenu(menu);
1603 return TRUE;
1606 ME_TextEditor *ME_MakeEditor(HWND hWnd) {
1607 ME_TextEditor *ed = ALLOC_OBJ(ME_TextEditor);
1608 int i;
1609 ed->hWnd = hWnd;
1610 ed->bEmulateVersion10 = FALSE;
1611 ed->pBuffer = ME_MakeText();
1612 ed->nZoomNumerator = ed->nZoomDenominator = 0;
1613 ME_MakeFirstParagraph(ed);
1614 ed->bCaretShown = FALSE;
1615 ed->nCursors = 4;
1616 ed->pCursors = ALLOC_N_OBJ(ME_Cursor, ed->nCursors);
1617 ed->pCursors[0].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
1618 ed->pCursors[0].nOffset = 0;
1619 ed->pCursors[1].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
1620 ed->pCursors[1].nOffset = 0;
1621 ed->pCursors[2] = ed->pCursors[0];
1622 ed->pCursors[3] = ed->pCursors[1];
1623 ed->nLastTotalLength = ed->nTotalLength = 0;
1624 ed->nHeight = 0;
1625 ed->nUDArrowX = -1;
1626 ed->nSequence = 0;
1627 ed->rgbBackColor = -1;
1628 ed->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
1629 ed->bCaretAtEnd = FALSE;
1630 ed->nEventMask = 0;
1631 ed->nModifyStep = 0;
1632 ed->nTextLimit = TEXT_LIMIT_DEFAULT;
1633 ed->pUndoStack = ed->pRedoStack = ed->pUndoStackBottom = NULL;
1634 ed->nUndoStackSize = 0;
1635 ed->nUndoLimit = STACK_SIZE_DEFAULT;
1636 ed->nUndoMode = umAddToUndo;
1637 ed->nParagraphs = 1;
1638 ed->nLastSelStart = ed->nLastSelEnd = 0;
1639 ed->pLastSelStartPara = ed->pLastSelEndPara = ME_FindItemFwd(ed->pBuffer->pFirst, diParagraph);
1640 ed->bRedraw = TRUE;
1641 ed->bWordWrap = (GetWindowLongW(hWnd, GWL_STYLE) & WS_HSCROLL) ? FALSE : TRUE;
1642 ed->bHideSelection = FALSE;
1643 ed->nInvalidOfs = -1;
1644 ed->pfnWordBreak = NULL;
1645 ed->lpOleCallback = NULL;
1646 ed->mode = TM_RICHTEXT | TM_MULTILEVELUNDO | TM_MULTICODEPAGE;
1647 ed->AutoURLDetect_bEnable = FALSE;
1648 ed->bHaveFocus = FALSE;
1649 GetClientRect(hWnd, &ed->rcFormat);
1650 for (i=0; i<HFONT_CACHE_SIZE; i++)
1652 ed->pFontCache[i].nRefs = 0;
1653 ed->pFontCache[i].nAge = 0;
1654 ed->pFontCache[i].hFont = NULL;
1657 ME_CheckCharOffsets(ed);
1658 if (GetWindowLongW(hWnd, GWL_STYLE) & ES_SELECTIONBAR)
1659 ed->selofs = 16;
1660 else
1661 ed->selofs = 0;
1662 ed->linesel = 0;
1664 if (GetWindowLongW(hWnd, GWL_STYLE) & ES_PASSWORD)
1665 ed->cPasswordMask = '*';
1666 else
1667 ed->cPasswordMask = 0;
1669 ed->notified_cr.cpMin = ed->notified_cr.cpMax = 0;
1671 return ed;
1674 typedef struct tagME_GlobalDestStruct
1676 HGLOBAL hData;
1677 int nLength;
1678 } ME_GlobalDestStruct;
1680 static DWORD CALLBACK ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1682 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
1683 int i;
1684 WORD *pSrc, *pDest;
1686 cb = cb >> 1;
1687 pDest = (WORD *)lpBuff;
1688 pSrc = (WORD *)GlobalLock(pData->hData);
1689 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
1690 pDest[i] = pSrc[pData->nLength+i];
1692 pData->nLength += i;
1693 *pcb = 2*i;
1694 GlobalUnlock(pData->hData);
1695 return 0;
1698 static DWORD CALLBACK ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1700 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
1701 int i;
1702 BYTE *pSrc, *pDest;
1704 pDest = lpBuff;
1705 pSrc = (BYTE *)GlobalLock(pData->hData);
1706 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
1707 pDest[i] = pSrc[pData->nLength+i];
1709 pData->nLength += i;
1710 *pcb = i;
1711 GlobalUnlock(pData->hData);
1712 return 0;
1716 void ME_DestroyEditor(ME_TextEditor *editor)
1718 ME_DisplayItem *pFirst = editor->pBuffer->pFirst;
1719 ME_DisplayItem *p = pFirst, *pNext = NULL;
1720 int i;
1722 ME_ClearTempStyle(editor);
1723 ME_EmptyUndoStack(editor);
1724 while(p) {
1725 pNext = p->next;
1726 ME_DestroyDisplayItem(p);
1727 p = pNext;
1729 ME_ReleaseStyle(editor->pBuffer->pDefaultStyle);
1730 for (i=0; i<HFONT_CACHE_SIZE; i++)
1732 if (editor->pFontCache[i].hFont)
1733 DeleteObject(editor->pFontCache[i].hFont);
1735 if (editor->rgbBackColor != -1)
1736 DeleteObject(editor->hbrBackground);
1737 if(editor->lpOleCallback)
1738 IUnknown_Release(editor->lpOleCallback);
1740 FREE_OBJ(editor->pBuffer);
1741 FREE_OBJ(editor->pCursors);
1743 FREE_OBJ(editor);
1746 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
1748 TRACE("\n");
1749 switch (fdwReason)
1751 case DLL_PROCESS_ATTACH:
1752 DisableThreadLibraryCalls(hinstDLL);
1753 me_heap = HeapCreate (0, 0x10000, 0);
1754 if (!ME_RegisterEditorClass(hinstDLL)) return FALSE;
1755 hLeft = LoadCursorW(hinstDLL, MAKEINTRESOURCEW(OCR_REVERSE));
1756 LookupInit();
1757 break;
1759 case DLL_PROCESS_DETACH:
1760 UnregisterClassW(RichEdit20W, 0);
1761 UnregisterClassW(RichEdit50W, 0);
1762 UnregisterClassA("RichEdit20A", 0);
1763 UnregisterClassA("RichEdit50A", 0);
1764 if (ME_ListBoxRegistered)
1765 UnregisterClassW(REListBox20W, 0);
1766 if (ME_ComboBoxRegistered)
1767 UnregisterClassW(REComboBox20W, 0);
1768 LookupCleanup();
1769 HeapDestroy (me_heap);
1770 me_heap = NULL;
1771 break;
1773 return TRUE;
1777 #define UNSUPPORTED_MSG(e) \
1778 case e: \
1779 FIXME(#e ": stub\n"); \
1780 return DefWindowProcW(hWnd, msg, wParam, lParam);
1782 static const char * const edit_messages[] = {
1783 "EM_GETSEL",
1784 "EM_SETSEL",
1785 "EM_GETRECT",
1786 "EM_SETRECT",
1787 "EM_SETRECTNP",
1788 "EM_SCROLL",
1789 "EM_LINESCROLL",
1790 "EM_SCROLLCARET",
1791 "EM_GETMODIFY",
1792 "EM_SETMODIFY",
1793 "EM_GETLINECOUNT",
1794 "EM_LINEINDEX",
1795 "EM_SETHANDLE",
1796 "EM_GETHANDLE",
1797 "EM_GETTHUMB",
1798 "EM_UNKNOWN_BF",
1799 "EM_UNKNOWN_C0",
1800 "EM_LINELENGTH",
1801 "EM_REPLACESEL",
1802 "EM_UNKNOWN_C3",
1803 "EM_GETLINE",
1804 "EM_LIMITTEXT",
1805 "EM_CANUNDO",
1806 "EM_UNDO",
1807 "EM_FMTLINES",
1808 "EM_LINEFROMCHAR",
1809 "EM_UNKNOWN_CA",
1810 "EM_SETTABSTOPS",
1811 "EM_SETPASSWORDCHAR",
1812 "EM_EMPTYUNDOBUFFER",
1813 "EM_GETFIRSTVISIBLELINE",
1814 "EM_SETREADONLY",
1815 "EM_SETWORDBREAKPROC",
1816 "EM_GETWORDBREAKPROC",
1817 "EM_GETPASSWORDCHAR",
1818 "EM_SETMARGINS",
1819 "EM_GETMARGINS",
1820 "EM_GETLIMITTEXT",
1821 "EM_POSFROMCHAR",
1822 "EM_CHARFROMPOS"
1825 static const char * const richedit_messages[] = {
1826 "EM_CANPASTE",
1827 "EM_DISPLAYBAND",
1828 "EM_EXGETSEL",
1829 "EM_EXLIMITTEXT",
1830 "EM_EXLINEFROMCHAR",
1831 "EM_EXSETSEL",
1832 "EM_FINDTEXT",
1833 "EM_FORMATRANGE",
1834 "EM_GETCHARFORMAT",
1835 "EM_GETEVENTMASK",
1836 "EM_GETOLEINTERFACE",
1837 "EM_GETPARAFORMAT",
1838 "EM_GETSELTEXT",
1839 "EM_HIDESELECTION",
1840 "EM_PASTESPECIAL",
1841 "EM_REQUESTRESIZE",
1842 "EM_SELECTIONTYPE",
1843 "EM_SETBKGNDCOLOR",
1844 "EM_SETCHARFORMAT",
1845 "EM_SETEVENTMASK",
1846 "EM_SETOLECALLBACK",
1847 "EM_SETPARAFORMAT",
1848 "EM_SETTARGETDEVICE",
1849 "EM_STREAMIN",
1850 "EM_STREAMOUT",
1851 "EM_GETTEXTRANGE",
1852 "EM_FINDWORDBREAK",
1853 "EM_SETOPTIONS",
1854 "EM_GETOPTIONS",
1855 "EM_FINDTEXTEX",
1856 "EM_GETWORDBREAKPROCEX",
1857 "EM_SETWORDBREAKPROCEX",
1858 "EM_SETUNDOLIMIT",
1859 "EM_UNKNOWN_USER_83",
1860 "EM_REDO",
1861 "EM_CANREDO",
1862 "EM_GETUNDONAME",
1863 "EM_GETREDONAME",
1864 "EM_STOPGROUPTYPING",
1865 "EM_SETTEXTMODE",
1866 "EM_GETTEXTMODE",
1867 "EM_AUTOURLDETECT",
1868 "EM_GETAUTOURLDETECT",
1869 "EM_SETPALETTE",
1870 "EM_GETTEXTEX",
1871 "EM_GETTEXTLENGTHEX",
1872 "EM_SHOWSCROLLBAR",
1873 "EM_SETTEXTEX",
1874 "EM_UNKNOWN_USER_98",
1875 "EM_UNKNOWN_USER_99",
1876 "EM_SETPUNCTUATION",
1877 "EM_GETPUNCTUATION",
1878 "EM_SETWORDWRAPMODE",
1879 "EM_GETWORDWRAPMODE",
1880 "EM_SETIMECOLOR",
1881 "EM_GETIMECOLOR",
1882 "EM_SETIMEOPTIONS",
1883 "EM_GETIMEOPTIONS",
1884 "EM_CONVPOSITION",
1885 "EM_UNKNOWN_USER_109",
1886 "EM_UNKNOWN_USER_110",
1887 "EM_UNKNOWN_USER_111",
1888 "EM_UNKNOWN_USER_112",
1889 "EM_UNKNOWN_USER_113",
1890 "EM_UNKNOWN_USER_114",
1891 "EM_UNKNOWN_USER_115",
1892 "EM_UNKNOWN_USER_116",
1893 "EM_UNKNOWN_USER_117",
1894 "EM_UNKNOWN_USER_118",
1895 "EM_UNKNOWN_USER_119",
1896 "EM_SETLANGOPTIONS",
1897 "EM_GETLANGOPTIONS",
1898 "EM_GETIMECOMPMODE",
1899 "EM_FINDTEXTW",
1900 "EM_FINDTEXTEXW",
1901 "EM_RECONVERSION",
1902 "EM_SETIMEMODEBIAS",
1903 "EM_GETIMEMODEBIAS"
1906 static const char *
1907 get_msg_name(UINT msg)
1909 if (msg >= EM_GETSEL && msg <= EM_CHARFROMPOS)
1910 return edit_messages[msg - EM_GETSEL];
1911 if (msg >= EM_CANPASTE && msg <= EM_GETIMEMODEBIAS)
1912 return richedit_messages[msg - EM_CANPASTE];
1913 return "";
1916 static LRESULT RichEditWndProc_common(HWND hWnd, UINT msg, WPARAM wParam,
1917 LPARAM lParam, BOOL unicode)
1919 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongPtrW(hWnd, 0);
1921 TRACE("hwnd %p msg %04x (%s) %lx %lx, unicode %d\n",
1922 hWnd, msg, get_msg_name(msg), wParam, lParam, unicode);
1924 if (!editor && msg != WM_NCCREATE && msg != WM_NCDESTROY) {
1925 ERR("called with invalid hWnd %p - application bug?\n", hWnd);
1926 return 0;
1929 switch(msg) {
1931 UNSUPPORTED_MSG(EM_DISPLAYBAND)
1932 UNSUPPORTED_MSG(EM_FINDWORDBREAK)
1933 UNSUPPORTED_MSG(EM_FMTLINES)
1934 UNSUPPORTED_MSG(EM_FORMATRANGE)
1935 UNSUPPORTED_MSG(EM_GETBIDIOPTIONS)
1936 UNSUPPORTED_MSG(EM_GETEDITSTYLE)
1937 UNSUPPORTED_MSG(EM_GETIMECOMPMODE)
1938 /* UNSUPPORTED_MSG(EM_GETIMESTATUS) missing in Wine headers */
1939 UNSUPPORTED_MSG(EM_GETLANGOPTIONS)
1940 /* UNSUPPORTED_MSG(EM_GETOLEINTERFACE) separate stub */
1941 UNSUPPORTED_MSG(EM_GETREDONAME)
1942 UNSUPPORTED_MSG(EM_GETTEXTMODE)
1943 UNSUPPORTED_MSG(EM_GETTYPOGRAPHYOPTIONS)
1944 UNSUPPORTED_MSG(EM_GETUNDONAME)
1945 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX)
1946 UNSUPPORTED_MSG(EM_PASTESPECIAL)
1947 UNSUPPORTED_MSG(EM_SELECTIONTYPE)
1948 UNSUPPORTED_MSG(EM_SETBIDIOPTIONS)
1949 UNSUPPORTED_MSG(EM_SETEDITSTYLE)
1950 UNSUPPORTED_MSG(EM_SETFONTSIZE)
1951 UNSUPPORTED_MSG(EM_SETLANGOPTIONS)
1952 UNSUPPORTED_MSG(EM_SETPALETTE)
1953 UNSUPPORTED_MSG(EM_SETTABSTOPS)
1954 UNSUPPORTED_MSG(EM_SETTYPOGRAPHYOPTIONS)
1955 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX)
1956 UNSUPPORTED_MSG(WM_STYLECHANGING)
1957 UNSUPPORTED_MSG(WM_STYLECHANGED)
1958 /* UNSUPPORTED_MSG(WM_UNICHAR) FIXME missing in Wine headers */
1960 /* Messages specific to Richedit controls */
1962 case EM_STREAMIN:
1963 return ME_StreamIn(editor, wParam, (EDITSTREAM*)lParam, TRUE);
1964 case EM_STREAMOUT:
1965 return ME_StreamOut(editor, wParam, (EDITSTREAM *)lParam);
1966 case WM_GETDLGCODE:
1968 UINT code = DLGC_WANTCHARS|DLGC_WANTARROWS;
1969 if(lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
1971 int vk = (int)((LPMSG)lParam)->wParam;
1972 /* if style says we want return key */
1973 if((vk == VK_RETURN) && (GetWindowLongW(hWnd, GWL_STYLE) & ES_WANTRETURN))
1975 code |= DLGC_WANTMESSAGE;
1977 /* we always handle ctrl-tab */
1978 if((vk == VK_TAB) && (GetKeyState(VK_CONTROL) & 0x8000))
1980 code |= DLGC_WANTMESSAGE;
1983 return code;
1985 case WM_NCCREATE:
1987 CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
1988 TRACE("WM_NCCREATE: style 0x%08x\n", pcs->style);
1989 editor = ME_MakeEditor(hWnd);
1990 SetWindowLongPtrW(hWnd, 0, (LONG_PTR)editor);
1991 return TRUE;
1993 case EM_EMPTYUNDOBUFFER:
1994 ME_EmptyUndoStack(editor);
1995 return 0;
1996 case EM_GETSEL:
1998 /* Note: wParam/lParam can be NULL */
1999 UINT from, to;
2000 PUINT pfrom = wParam ? (PUINT)wParam : &from;
2001 PUINT pto = lParam ? (PUINT)lParam : &to;
2002 ME_GetSelection(editor, (int *)pfrom, (int *)pto);
2003 if ((*pfrom|*pto) & 0xFFFF0000)
2004 return -1;
2005 return MAKELONG(*pfrom,*pto);
2007 case EM_EXGETSEL:
2009 CHARRANGE *pRange = (CHARRANGE *)lParam;
2010 ME_GetSelection(editor, &pRange->cpMin, &pRange->cpMax);
2011 TRACE("EM_EXGETSEL = (%d,%d)\n", pRange->cpMin, pRange->cpMax);
2012 return 0;
2014 case EM_SETUNDOLIMIT:
2016 if ((int)wParam < 0)
2017 editor->nUndoLimit = STACK_SIZE_DEFAULT;
2018 else
2019 editor->nUndoLimit = min(wParam, STACK_SIZE_MAX);
2020 /* Setting a max stack size keeps wine from getting killed
2021 for hogging memory. Windows allocates all this memory at once, so
2022 no program would realistically set a value above our maximum. */
2023 return editor->nUndoLimit;
2025 case EM_CANUNDO:
2026 return editor->pUndoStack != NULL;
2027 case EM_CANREDO:
2028 return editor->pRedoStack != NULL;
2029 case WM_UNDO: /* FIXME: actually not the same */
2030 case EM_UNDO:
2031 ME_Undo(editor);
2032 return 0;
2033 case EM_REDO:
2034 ME_Redo(editor);
2035 return 0;
2036 case EM_GETOPTIONS:
2038 /* these flags are equivalent to the ES_* counterparts */
2039 DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
2040 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN;
2041 DWORD settings = GetWindowLongW(hWnd, GWL_STYLE) & mask;
2043 return settings;
2045 case EM_SETOPTIONS:
2047 /* these flags are equivalent to ES_* counterparts
2048 * ECO_READONLY is already implemented in the code, only requires
2049 * setting the bit to work
2051 DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
2052 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN | ECO_SELECTIONBAR;
2053 DWORD raw = GetWindowLongW(hWnd, GWL_STYLE);
2054 DWORD settings = mask & raw;
2056 switch(wParam)
2058 case ECOOP_SET:
2059 settings = lParam;
2060 break;
2061 case ECOOP_OR:
2062 settings |= lParam;
2063 break;
2064 case ECOOP_AND:
2065 settings &= lParam;
2066 break;
2067 case ECOOP_XOR:
2068 settings ^= lParam;
2070 SetWindowLongW(hWnd, GWL_STYLE, (raw & ~mask) | (settings & mask));
2072 if (settings & ECO_AUTOWORDSELECTION)
2073 FIXME("ECO_AUTOWORDSELECTION not implemented yet!\n");
2074 if (settings & ECO_SELECTIONBAR)
2075 editor->selofs = 16;
2076 else
2077 editor->selofs = 0;
2078 ME_WrapMarkedParagraphs(editor);
2080 if (settings & ECO_VERTICAL)
2081 FIXME("ECO_VERTICAL not implemented yet!\n");
2082 if (settings & ECO_AUTOHSCROLL)
2083 FIXME("ECO_AUTOHSCROLL not implemented yet!\n");
2084 if (settings & ECO_AUTOVSCROLL)
2085 FIXME("ECO_AUTOVSCROLL not implemented yet!\n");
2086 if (settings & ECO_NOHIDESEL)
2087 FIXME("ECO_NOHIDESEL not implemented yet!\n");
2088 if (settings & ECO_WANTRETURN)
2089 FIXME("ECO_WANTRETURN not implemented yet!\n");
2091 return settings;
2093 case EM_SETSEL:
2095 ME_InvalidateSelection(editor);
2096 ME_SetSelection(editor, wParam, lParam);
2097 ME_InvalidateSelection(editor);
2098 ME_SendSelChange(editor);
2099 return 0;
2101 case EM_SETSCROLLPOS:
2103 POINT *point = (POINT *)lParam;
2104 ME_ScrollAbs(editor, point->y);
2105 return 0;
2107 case EM_AUTOURLDETECT:
2109 if (wParam==1 || wParam ==0)
2111 editor->AutoURLDetect_bEnable = (BOOL)wParam;
2112 return 0;
2114 return E_INVALIDARG;
2116 case EM_GETAUTOURLDETECT:
2118 return editor->AutoURLDetect_bEnable;
2120 case EM_EXSETSEL:
2122 int end;
2123 CHARRANGE range = *(CHARRANGE *)lParam;
2125 TRACE("EM_EXSETSEL (%d,%d)\n", range.cpMin, range.cpMax);
2127 ME_InvalidateSelection(editor);
2128 end = ME_SetSelection(editor, range.cpMin, range.cpMax);
2129 ME_InvalidateSelection(editor);
2130 ME_SendSelChange(editor);
2132 return end;
2134 case EM_SHOWSCROLLBAR:
2136 ShowScrollBar(editor->hWnd, wParam, lParam);
2137 return 0;
2139 case EM_SETTEXTEX:
2141 LPWSTR wszText;
2142 SETTEXTEX *pStruct = (SETTEXTEX *)wParam;
2143 size_t len;
2144 int from, to;
2145 ME_Style *style;
2146 int oldModify = editor->nModifyStep;
2148 if (!pStruct) return 0;
2150 TRACE("EM_SETTEXTEX - %s, flags %d, cp %d\n",
2151 pStruct->codepage == 1200 ? debugstr_w((LPCWSTR)lParam) : debugstr_a((LPCSTR)lParam),
2152 pStruct->flags, pStruct->codepage);
2154 /* FIXME: make use of pStruct->codepage in the to unicode translation */
2155 wszText = lParam ? ME_ToUnicode(pStruct->codepage == 1200, (void *)lParam) : NULL;
2156 len = wszText ? lstrlenW(wszText) : 0;
2158 if (pStruct->flags & ST_SELECTION) {
2159 ME_GetSelection(editor, &from, &to);
2160 style = ME_GetSelectionInsertStyle(editor);
2161 ME_InternalDeleteText(editor, from, to - from);
2162 if (pStruct->codepage != 1200 && lParam && !strncmp((char *)lParam, "{\\rtf", 5))
2163 ME_StreamInRTFString(editor, 0, (char *)lParam);
2164 else ME_InsertTextFromCursor(editor, 0, wszText, len, style);
2165 ME_ReleaseStyle(style);
2167 else {
2168 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
2169 if (pStruct->codepage != 1200 && lParam && !strncmp((char *)lParam, "{\\rtf", 5))
2170 ME_StreamInRTFString(editor, 0, (char *)lParam);
2171 else ME_InsertTextFromCursor(editor, 0, wszText, len, editor->pBuffer->pDefaultStyle);
2172 len = 1;
2174 ME_CommitUndo(editor);
2175 if (!(pStruct->flags & ST_KEEPUNDO))
2177 editor->nModifyStep = oldModify;
2178 ME_EmptyUndoStack(editor);
2180 ME_UpdateRepaint(editor);
2181 return len;
2183 case EM_SETBKGNDCOLOR:
2185 LRESULT lColor;
2186 if (editor->rgbBackColor != -1) {
2187 DeleteObject(editor->hbrBackground);
2188 lColor = editor->rgbBackColor;
2190 else lColor = GetSysColor(COLOR_WINDOW);
2192 if (wParam)
2194 editor->rgbBackColor = -1;
2195 editor->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
2197 else
2199 editor->rgbBackColor = lParam;
2200 editor->hbrBackground = CreateSolidBrush(editor->rgbBackColor);
2202 if (editor->bRedraw)
2204 InvalidateRect(hWnd, NULL, TRUE);
2205 UpdateWindow(hWnd);
2207 return lColor;
2209 case EM_GETMODIFY:
2210 return editor->nModifyStep == 0 ? 0 : -1;
2211 case EM_SETMODIFY:
2213 if (wParam)
2214 editor->nModifyStep = 1;
2215 else
2216 editor->nModifyStep = 0;
2218 return 0;
2220 case EM_SETREADONLY:
2222 long nStyle = GetWindowLongW(hWnd, GWL_STYLE);
2223 if (wParam)
2224 nStyle |= ES_READONLY;
2225 else
2226 nStyle &= ~ES_READONLY;
2227 SetWindowLongW(hWnd, GWL_STYLE, nStyle);
2228 return 0;
2230 case EM_SETEVENTMASK:
2232 DWORD nOldMask = editor->nEventMask;
2234 editor->nEventMask = lParam;
2235 return nOldMask;
2237 case EM_GETEVENTMASK:
2238 return editor->nEventMask;
2239 case EM_SETCHARFORMAT:
2241 CHARFORMAT2W buf, *p;
2242 BOOL bRepaint = TRUE;
2243 p = ME_ToCF2W(&buf, (CHARFORMAT2W *)lParam);
2244 if (p == NULL) return 0;
2245 if (!wParam)
2246 ME_SetDefaultCharFormat(editor, p);
2247 else if (wParam == (SCF_WORD | SCF_SELECTION)) {
2248 FIXME("EM_SETCHARFORMAT: word selection not supported\n");
2249 return 0;
2250 } else if (wParam == SCF_ALL) {
2251 if (editor->mode & TM_PLAINTEXT)
2252 ME_SetDefaultCharFormat(editor, p);
2253 else {
2254 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), p);
2255 editor->nModifyStep = 1;
2257 } else if (editor->mode & TM_PLAINTEXT) {
2258 return 0;
2259 } else {
2260 int from, to;
2261 ME_GetSelection(editor, &from, &to);
2262 bRepaint = (from != to);
2263 ME_SetSelectionCharFormat(editor, p);
2264 if (from != to) editor->nModifyStep = 1;
2266 ME_CommitUndo(editor);
2267 if (bRepaint)
2268 ME_RewrapRepaint(editor);
2269 return 1;
2271 case EM_GETCHARFORMAT:
2273 CHARFORMAT2W tmp, *dst = (CHARFORMAT2W *)lParam;
2274 if (dst->cbSize != sizeof(CHARFORMATA) &&
2275 dst->cbSize != sizeof(CHARFORMATW) &&
2276 dst->cbSize != sizeof(CHARFORMAT2A) &&
2277 dst->cbSize != sizeof(CHARFORMAT2W))
2278 return 0;
2279 tmp.cbSize = sizeof(tmp);
2280 if (!wParam)
2281 ME_GetDefaultCharFormat(editor, &tmp);
2282 else
2283 ME_GetSelectionCharFormat(editor, &tmp);
2284 ME_CopyToCFAny(dst, &tmp);
2285 return tmp.dwMask;
2287 case EM_SETPARAFORMAT:
2288 ME_SetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
2289 ME_RewrapRepaint(editor);
2290 ME_CommitUndo(editor);
2291 return 0;
2292 case EM_GETPARAFORMAT:
2293 ME_GetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
2294 return 0;
2295 case EM_GETFIRSTVISIBLELINE:
2297 ME_DisplayItem *p = editor->pBuffer->pFirst;
2298 int y = ME_GetYScrollPos(editor);
2299 int ypara = 0;
2300 int count = 0;
2301 int ystart, yend;
2302 while(p) {
2303 p = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
2304 if (p->type == diTextEnd)
2305 break;
2306 if (p->type == diParagraph) {
2307 ypara = p->member.para.nYPos;
2308 continue;
2310 ystart = ypara + p->member.row.nYPos;
2311 yend = ystart + p->member.row.nHeight;
2312 if (y < yend) {
2313 break;
2315 count++;
2317 return count;
2319 case EM_HIDESELECTION:
2321 editor->bHideSelection = (wParam != 0);
2322 ME_InvalidateSelection(editor);
2323 return 0;
2325 case EM_LINESCROLL:
2327 ME_ScrollDown(editor, lParam * 8); /* FIXME follow the original */
2328 return TRUE; /* Should return false if a single line richedit control */
2330 case WM_CLEAR:
2332 int from, to;
2333 ME_GetSelection(editor, &from, &to);
2334 ME_InternalDeleteText(editor, from, to-from);
2335 ME_CommitUndo(editor);
2336 ME_UpdateRepaint(editor);
2337 return 0;
2339 case EM_REPLACESEL:
2341 int from, to;
2342 ME_Style *style;
2343 LPWSTR wszText = lParam ? ME_ToUnicode(unicode, (void *)lParam) : NULL;
2344 size_t len = wszText ? lstrlenW(wszText) : 0;
2345 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText));
2347 ME_GetSelection(editor, &from, &to);
2348 style = ME_GetSelectionInsertStyle(editor);
2349 ME_InternalDeleteText(editor, from, to-from);
2350 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
2351 ME_ReleaseStyle(style);
2352 /* drop temporary style if line end */
2354 * FIXME question: does abc\n mean: put abc,
2355 * clear temp style, put \n? (would require a change)
2357 if (len>0 && wszText[len-1] == '\n')
2358 ME_ClearTempStyle(editor);
2359 ME_EndToUnicode(unicode, wszText);
2360 ME_CommitUndo(editor);
2361 if (!wParam)
2362 ME_EmptyUndoStack(editor);
2363 ME_UpdateRepaint(editor);
2364 return len;
2366 case EM_SCROLLCARET:
2368 int top, bottom; /* row's edges relative to document top */
2369 int nPos;
2370 ME_DisplayItem *para, *row;
2372 nPos = ME_GetYScrollPos(editor);
2373 row = ME_RowStart(editor->pCursors[0].pRun);
2374 para = ME_GetParagraph(row);
2375 top = para->member.para.nYPos + row->member.row.nYPos;
2376 bottom = top + row->member.row.nHeight;
2378 if (top < nPos) /* caret above window */
2379 ME_ScrollAbs(editor, top);
2380 else if (nPos + editor->sizeWindow.cy < bottom) /*below*/
2381 ME_ScrollAbs(editor, bottom - editor->sizeWindow.cy);
2382 return 0;
2384 case WM_SETFONT:
2386 LOGFONTW lf;
2387 CHARFORMAT2W fmt;
2388 HDC hDC;
2389 BOOL bRepaint = LOWORD(lParam);
2391 if (!wParam)
2392 wParam = (WPARAM)GetStockObject(SYSTEM_FONT);
2393 GetObjectW((HGDIOBJ)wParam, sizeof(LOGFONTW), &lf);
2394 hDC = GetDC(hWnd);
2395 ME_CharFormatFromLogFont(hDC, &lf, &fmt);
2396 ReleaseDC(hWnd, hDC);
2397 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), &fmt);
2398 ME_SetDefaultCharFormat(editor, &fmt);
2400 ME_CommitUndo(editor);
2401 if (bRepaint)
2402 ME_RewrapRepaint(editor);
2403 return 0;
2405 case WM_SETTEXT:
2407 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
2408 if (lParam)
2410 TRACE("WM_SETTEXT lParam==%lx\n",lParam);
2411 if (!unicode && !strncmp((char *)lParam, "{\\rtf", 5))
2413 /* Undocumented: WM_SETTEXT supports RTF text */
2414 ME_StreamInRTFString(editor, 0, (char *)lParam);
2416 else
2418 LPWSTR wszText = ME_ToUnicode(unicode, (void *)lParam);
2419 TRACE("WM_SETTEXT - %s\n", debugstr_w(wszText)); /* debugstr_w() */
2420 if (lstrlenW(wszText) > 0)
2422 int len = -1;
2424 /* uses default style! */
2425 if (!(GetWindowLongW(hWnd, GWL_STYLE) & ES_MULTILINE))
2427 WCHAR * p;
2429 p = wszText;
2430 while (*p != '\0' && *p != '\r' && *p != '\n') p++;
2431 len = p - wszText;
2433 ME_InsertTextFromCursor(editor, 0, wszText, len, editor->pBuffer->pDefaultStyle);
2435 ME_EndToUnicode(unicode, wszText);
2438 else
2439 TRACE("WM_SETTEXT - NULL\n");
2440 ME_SetSelection(editor, 0, 0);
2441 editor->nModifyStep = 0;
2442 ME_CommitUndo(editor);
2443 ME_EmptyUndoStack(editor);
2444 ME_UpdateRepaint(editor);
2445 return 1;
2447 case EM_CANPASTE:
2449 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
2450 if (IsClipboardFormatAvailable(nRTFFormat))
2451 return TRUE;
2452 if (IsClipboardFormatAvailable(CF_UNICODETEXT))
2453 return TRUE;
2454 return FALSE;
2456 case WM_PASTE:
2458 DWORD dwFormat = 0;
2459 EDITSTREAM es;
2460 ME_GlobalDestStruct gds;
2461 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
2462 UINT cf = 0;
2464 if (IsClipboardFormatAvailable(nRTFFormat))
2465 cf = nRTFFormat, dwFormat = SF_RTF;
2466 else if (IsClipboardFormatAvailable(CF_UNICODETEXT))
2467 cf = CF_UNICODETEXT, dwFormat = SF_TEXT|SF_UNICODE;
2468 else
2469 return 0;
2471 if (!OpenClipboard(hWnd))
2472 return 0;
2473 gds.hData = GetClipboardData(cf);
2474 gds.nLength = 0;
2475 es.dwCookie = (DWORD)&gds;
2476 es.pfnCallback = dwFormat == SF_RTF ? ME_ReadFromHGLOBALRTF : ME_ReadFromHGLOBALUnicode;
2477 ME_StreamIn(editor, dwFormat|SFF_SELECTION, &es, FALSE);
2479 CloseClipboard();
2480 return 0;
2482 case WM_CUT:
2483 case WM_COPY:
2485 LPDATAOBJECT dataObj = NULL;
2486 CHARRANGE range;
2487 HRESULT hr = S_OK;
2489 if (editor->cPasswordMask)
2490 return 0; /* Copying or Cutting masked text isn't allowed */
2492 ME_GetSelection(editor, &range.cpMin, &range.cpMax);
2493 if(editor->lpOleCallback)
2494 hr = IRichEditOleCallback_GetClipboardData(editor->lpOleCallback, &range, RECO_COPY, &dataObj);
2495 if(FAILED(hr) || !dataObj)
2496 hr = ME_GetDataObject(editor, &range, &dataObj);
2497 if(SUCCEEDED(hr)) {
2498 hr = OleSetClipboard(dataObj);
2499 IDataObject_Release(dataObj);
2501 if (SUCCEEDED(hr) && msg == WM_CUT)
2503 ME_InternalDeleteText(editor, range.cpMin, range.cpMax-range.cpMin);
2504 ME_CommitUndo(editor);
2505 ME_UpdateRepaint(editor);
2507 return 0;
2509 case WM_GETTEXTLENGTH:
2511 GETTEXTLENGTHEX how;
2513 /* CR/LF conversion required in 2.0 mode, verbatim in 1.0 mode */
2514 how.flags = GTL_CLOSE | (editor->bEmulateVersion10 ? 0 : GTL_USECRLF) | GTL_NUMCHARS;
2515 how.codepage = unicode ? 1200 : CP_ACP;
2516 return ME_GetTextLengthEx(editor, &how);
2518 case EM_GETTEXTLENGTHEX:
2519 return ME_GetTextLengthEx(editor, (GETTEXTLENGTHEX *)wParam);
2520 case WM_GETTEXT:
2522 GETTEXTEX ex;
2523 LRESULT rc;
2524 LPSTR bufferA = NULL;
2525 LPWSTR bufferW = NULL;
2527 if (unicode)
2528 bufferW = heap_alloc((wParam + 2) * sizeof(WCHAR));
2529 else
2530 bufferA = heap_alloc(wParam + 2);
2532 ex.cb = (wParam + 2) * (unicode ? sizeof(WCHAR) : sizeof(CHAR));
2533 ex.flags = GT_USECRLF;
2534 ex.codepage = unicode ? 1200 : CP_ACP;
2535 ex.lpDefaultChar = NULL;
2536 ex.lpUsedDefChar = NULL;
2537 rc = RichEditWndProc_common(hWnd, EM_GETTEXTEX, (WPARAM)&ex, unicode ? (LPARAM)bufferW : (LPARAM)bufferA, unicode);
2539 if (unicode)
2541 memcpy((LPWSTR)lParam, bufferW, wParam * sizeof(WCHAR));
2542 if (lstrlenW(bufferW) >= wParam) rc = 0;
2544 else
2546 memcpy((LPSTR)lParam, bufferA, wParam);
2547 if (strlen(bufferA) >= wParam) rc = 0;
2549 heap_free(bufferA);
2550 heap_free(bufferW);
2551 return rc;
2553 case EM_GETTEXTEX:
2555 GETTEXTEX *ex = (GETTEXTEX*)wParam;
2556 int nStart, nCount; /* in chars */
2558 if (ex->flags & ~(GT_SELECTION | GT_USECRLF))
2559 FIXME("GETTEXTEX flags 0x%08x not supported\n", ex->flags & ~(GT_SELECTION | GT_USECRLF));
2561 if (ex->flags & GT_SELECTION)
2563 ME_GetSelection(editor, &nStart, &nCount);
2564 nCount -= nStart;
2566 else
2568 nStart = 0;
2569 nCount = 0x7fffffff;
2571 if (ex->codepage == 1200)
2573 nCount = min(nCount, ex->cb / sizeof(WCHAR) - 1);
2574 return ME_GetTextW(editor, (LPWSTR)lParam, nStart, nCount, ex->flags & GT_USECRLF);
2576 else
2578 /* potentially each char may be a CR, why calculate the exact value with O(N) when
2579 we can just take a bigger buffer? :)
2580 The above assumption still holds with CR/LF counters, since CR->CRLF expansion
2581 occurs only in richedit 2.0 mode, in which line breaks have only one CR
2583 int crlfmul = (ex->flags & GT_USECRLF) ? 2 : 1;
2584 LPWSTR buffer;
2585 DWORD buflen = ex->cb;
2586 LRESULT rc;
2587 DWORD flags = 0;
2589 nCount = min(nCount, ex->cb - 1);
2590 buffer = heap_alloc((crlfmul*nCount + 1) * sizeof(WCHAR));
2592 buflen = ME_GetTextW(editor, buffer, nStart, nCount, ex->flags & GT_USECRLF);
2593 rc = WideCharToMultiByte(ex->codepage, flags, buffer, -1, (LPSTR)lParam, ex->cb, ex->lpDefaultChar, ex->lpUsedDefChar);
2594 if (rc) rc--; /* do not count 0 terminator */
2596 heap_free(buffer);
2597 return rc;
2600 case EM_GETSELTEXT:
2602 int from, to;
2603 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
2604 ME_GetSelection(editor, &from, &to);
2605 tr.chrg.cpMin = from;
2606 tr.chrg.cpMax = to;
2607 tr.lpstrText = (WCHAR *)lParam;
2608 return RichEditWndProc_common(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr, unicode);
2610 case EM_GETSCROLLPOS:
2612 POINT *point = (POINT *)lParam;
2613 point->x = 0; /* FIXME side scrolling not implemented */
2614 point->y = ME_GetYScrollPos(editor);
2615 return 1;
2617 case EM_GETTEXTRANGE:
2619 TEXTRANGEW *rng = (TEXTRANGEW *)lParam;
2620 TRACE("EM_GETTEXTRANGE min=%d max=%d unicode=%d emul1.0=%d length=%d\n",
2621 rng->chrg.cpMin, rng->chrg.cpMax, unicode,
2622 editor->bEmulateVersion10, ME_GetTextLength(editor));
2623 if (unicode)
2624 return ME_GetTextW(editor, rng->lpstrText, rng->chrg.cpMin, rng->chrg.cpMax-rng->chrg.cpMin, 0);
2625 else
2627 int nLen = rng->chrg.cpMax-rng->chrg.cpMin;
2628 WCHAR *p = ALLOC_N_OBJ(WCHAR, nLen+1);
2629 int nChars = ME_GetTextW(editor, p, rng->chrg.cpMin, nLen, 0);
2630 /* FIXME this is a potential security hole (buffer overrun)
2631 if you know more about wchar->mbyte conversion please explain
2633 WideCharToMultiByte(CP_ACP, 0, p, nChars+1, (char *)rng->lpstrText, nLen+1, NULL, NULL);
2634 FREE_OBJ(p);
2635 return nChars;
2638 case EM_GETLINE:
2640 ME_DisplayItem *run;
2641 const unsigned int nMaxChars = *(WORD *) lParam;
2642 unsigned int nCharsLeft = nMaxChars;
2643 char *dest = (char *) lParam;
2644 BOOL wroteNull = FALSE;
2646 TRACE("EM_GETLINE: row=%d, nMaxChars=%d (%s)\n", (int) wParam, nMaxChars,
2647 unicode ? "Unicode" : "Ansi");
2649 run = ME_FindRowWithNumber(editor, wParam);
2650 if (run == NULL)
2651 return 0;
2653 while (nCharsLeft && (run = ME_FindItemFwd(run, diRunOrStartRow))
2654 && !(run->member.run.nFlags & MERF_ENDPARA))
2656 unsigned int nCopy;
2657 ME_String *strText;
2658 if (run->type != diRun)
2659 break;
2660 strText = run->member.run.strText;
2661 nCopy = min(nCharsLeft, strText->nLen);
2663 if (unicode)
2664 lstrcpynW((LPWSTR) dest, strText->szData, nCopy);
2665 else
2666 nCopy = WideCharToMultiByte(CP_ACP, 0, strText->szData, nCopy, dest,
2667 nCharsLeft, NULL, NULL);
2668 dest += nCopy * (unicode ? sizeof(WCHAR) : 1);
2669 nCharsLeft -= nCopy;
2672 /* append line termination, space allowing */
2673 if (nCharsLeft > 0)
2675 if (run && (run->member.run.nFlags & MERF_ENDPARA))
2677 unsigned int i;
2678 /* Write as many \r as encoded in end-of-paragraph, space allowing */
2679 for (i = 0; i < run->member.run.nCR && nCharsLeft > 0; i++, nCharsLeft--)
2681 *((WCHAR *)dest) = '\r';
2682 dest += unicode ? sizeof(WCHAR) : 1;
2684 /* Write as many \n as encoded in end-of-paragraph, space allowing */
2685 for (i = 0; i < run->member.run.nLF && nCharsLeft > 0; i++, nCharsLeft--)
2687 *((WCHAR *)dest) = '\n';
2688 dest += unicode ? sizeof(WCHAR) : 1;
2691 if (nCharsLeft > 0)
2693 if (unicode)
2694 *((WCHAR *)dest) = '\0';
2695 else
2696 *dest = '\0';
2697 nCharsLeft--;
2698 wroteNull = TRUE;
2702 TRACE("EM_GETLINE: got %u characters\n", nMaxChars - nCharsLeft);
2703 return nMaxChars - nCharsLeft - (wroteNull ? 1 : 0);
2705 case EM_GETLINECOUNT:
2707 ME_DisplayItem *item = editor->pBuffer->pFirst->next;
2708 int nRows = 0;
2710 ME_DisplayItem *prev_para = NULL, *last_para = NULL;
2712 while (item != editor->pBuffer->pLast)
2714 assert(item->type == diParagraph);
2715 prev_para = ME_FindItemBack(item, diRun);
2716 if (prev_para) {
2717 assert(prev_para->member.run.nFlags & MERF_ENDPARA);
2719 nRows += item->member.para.nRows;
2720 item = item->member.para.next_para;
2722 last_para = ME_FindItemBack(item, diRun);
2723 assert(last_para);
2724 assert(last_para->member.run.nFlags & MERF_ENDPARA);
2725 if (editor->bEmulateVersion10 && prev_para && last_para->member.run.nCharOfs == 0
2726 && prev_para->member.run.nCR == 1 && prev_para->member.run.nLF == 0)
2728 /* In 1.0 emulation, the last solitary \r at the very end of the text
2729 (if one exists) is NOT a line break.
2730 FIXME: this is an ugly hack. This should have a more regular model. */
2731 nRows--;
2734 TRACE("EM_GETLINECOUNT: nRows==%d\n", nRows);
2735 return max(1, nRows);
2737 case EM_LINEFROMCHAR:
2739 if (wParam == -1)
2740 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(editor, 1));
2741 else
2742 return ME_RowNumberFromCharOfs(editor, wParam);
2744 case EM_EXLINEFROMCHAR:
2746 if (lParam == -1)
2747 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(editor,1));
2748 else
2749 return ME_RowNumberFromCharOfs(editor, lParam);
2751 case EM_LINEINDEX:
2753 ME_DisplayItem *item, *para;
2754 int nCharOfs;
2756 if (wParam == -1)
2757 item = ME_FindItemBack(editor->pCursors[0].pRun, diStartRow);
2758 else
2759 item = ME_FindRowWithNumber(editor, wParam);
2760 if (!item)
2761 return -1;
2762 para = ME_GetParagraph(item);
2763 item = ME_FindItemFwd(item, diRun);
2764 nCharOfs = para->member.para.nCharOfs + item->member.run.nCharOfs;
2765 TRACE("EM_LINEINDEX: nCharOfs==%d\n", nCharOfs);
2766 return nCharOfs;
2768 case EM_LINELENGTH:
2770 ME_DisplayItem *item, *item_end;
2771 int nChars = 0, nThisLineOfs = 0, nNextLineOfs = 0;
2773 if (wParam > ME_GetTextLength(editor))
2774 return 0;
2775 if (wParam == -1)
2777 FIXME("EM_LINELENGTH: returning number of unselected characters on lines with selection unsupported.\n");
2778 return 0;
2780 item = ME_FindItemAtOffset(editor, diRun, wParam, NULL);
2781 item = ME_RowStart(item);
2782 nThisLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item, diRun), 0);
2783 item_end = ME_FindItemFwd(item, diStartRowOrParagraphOrEnd);
2784 if (item_end->type == diStartRow)
2785 nNextLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item_end, diRun), 0);
2786 else
2788 ME_DisplayItem *endPara;
2790 nNextLineOfs = ME_FindItemFwd(item, diParagraphOrEnd)->member.para.nCharOfs;
2791 endPara = ME_FindItemFwd(item, diParagraphOrEnd);
2792 endPara = ME_FindItemBack(endPara, diRun);
2793 assert(endPara);
2794 assert(endPara->type == diRun);
2795 assert(endPara->member.run.nFlags & MERF_ENDPARA);
2796 nNextLineOfs -= endPara->member.run.nCR + endPara->member.run.nLF;
2798 nChars = nNextLineOfs - nThisLineOfs;
2799 TRACE("EM_LINELENGTH(%ld)==%d\n",wParam, nChars);
2800 return nChars;
2802 case EM_EXLIMITTEXT:
2804 if ((int)lParam < 0)
2805 return 0;
2806 if (lParam == 0)
2807 editor->nTextLimit = 65536;
2808 else
2809 editor->nTextLimit = (int) lParam;
2810 return 0;
2812 case EM_LIMITTEXT:
2814 if (wParam == 0)
2815 editor->nTextLimit = 65536;
2816 else
2817 editor->nTextLimit = (int) wParam;
2818 return 0;
2820 case EM_GETLIMITTEXT:
2822 return editor->nTextLimit;
2824 case EM_FINDTEXT:
2826 FINDTEXTA *ft = (FINDTEXTA *)lParam;
2827 int nChars = MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, NULL, 0);
2828 WCHAR *tmp;
2829 LRESULT r;
2831 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
2832 MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, tmp, nChars);
2833 r = ME_FindText(editor, wParam, &ft->chrg, tmp, NULL);
2834 FREE_OBJ( tmp );
2835 return r;
2837 case EM_FINDTEXTEX:
2839 FINDTEXTEXA *ex = (FINDTEXTEXA *)lParam;
2840 int nChars = MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, NULL, 0);
2841 WCHAR *tmp;
2842 LRESULT r;
2844 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
2845 MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, tmp, nChars);
2846 r = ME_FindText(editor, wParam, &ex->chrg, tmp, &ex->chrgText);
2847 FREE_OBJ( tmp );
2848 return r;
2850 case EM_FINDTEXTW:
2852 FINDTEXTW *ft = (FINDTEXTW *)lParam;
2853 return ME_FindText(editor, wParam, &ft->chrg, ft->lpstrText, NULL);
2855 case EM_FINDTEXTEXW:
2857 FINDTEXTEXW *ex = (FINDTEXTEXW *)lParam;
2858 return ME_FindText(editor, wParam, &ex->chrg, ex->lpstrText, &ex->chrgText);
2860 case EM_GETZOOM:
2861 if (!wParam || !lParam)
2862 return FALSE;
2863 *(int *)wParam = editor->nZoomNumerator;
2864 *(int *)lParam = editor->nZoomDenominator;
2865 return TRUE;
2866 case EM_SETZOOM:
2867 return ME_SetZoom(editor, wParam, lParam);
2868 case EM_CHARFROMPOS:
2869 return ME_CharFromPos(editor, ((POINTL *)lParam)->x, ((POINTL *)lParam)->y);
2870 case EM_POSFROMCHAR:
2872 ME_DisplayItem *pRun;
2873 int nCharOfs, nOffset, nLength;
2874 POINTL pt = {0,0};
2876 nCharOfs = wParam;
2877 /* detect which API version we're dealing with */
2878 if (wParam >= 0x40000)
2879 nCharOfs = lParam;
2880 nLength = ME_GetTextLength(editor);
2882 if (nCharOfs < nLength) {
2883 ME_RunOfsFromCharOfs(editor, nCharOfs, &pRun, &nOffset);
2884 assert(pRun->type == diRun);
2885 pt.y = pRun->member.run.pt.y;
2886 pt.x = pRun->member.run.pt.x + ME_PointFromChar(editor, &pRun->member.run, nOffset);
2887 pt.y += ME_GetParagraph(pRun)->member.para.nYPos;
2888 } else {
2889 pt.x = 0;
2890 pt.y = editor->pBuffer->pLast->member.para.nYPos;
2892 pt.x += editor->selofs;
2893 if (wParam >= 0x40000) {
2894 *(POINTL *)wParam = pt;
2896 return MAKELONG( pt.x, pt.y );
2898 case WM_CREATE:
2899 if (GetWindowLongW(hWnd, GWL_STYLE) & WS_HSCROLL)
2900 { /* Squelch the default horizontal scrollbar it would make */
2901 ShowScrollBar(editor->hWnd, SB_HORZ, FALSE);
2903 ME_CommitUndo(editor);
2904 ME_WrapMarkedParagraphs(editor);
2905 ME_MoveCaret(editor);
2906 return 0;
2907 case WM_DESTROY:
2908 ME_DestroyEditor(editor);
2909 SetWindowLongPtrW(hWnd, 0, 0);
2910 return 0;
2911 case WM_LBUTTONDOWN:
2912 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
2913 !ME_FilterEvent(editor, msg, &wParam, &lParam))
2914 return 0;
2915 SetFocus(hWnd);
2916 ME_LButtonDown(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
2917 SetCapture(hWnd);
2918 ME_LinkNotify(editor,msg,wParam,lParam);
2919 if (!ME_SetCursor(editor, LOWORD(lParam))) goto do_default;
2920 break;
2921 case WM_MOUSEMOVE:
2922 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
2923 !ME_FilterEvent(editor, msg, &wParam, &lParam))
2924 return 0;
2925 if (GetCapture() == hWnd)
2926 ME_MouseMove(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
2927 ME_LinkNotify(editor,msg,wParam,lParam);
2928 if (!ME_SetCursor(editor, LOWORD(lParam))) goto do_default;
2929 break;
2930 case WM_LBUTTONUP:
2931 if (GetCapture() == hWnd)
2932 ReleaseCapture();
2933 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
2934 !ME_FilterEvent(editor, msg, &wParam, &lParam))
2935 return 0;
2936 else
2938 BOOL ret;
2939 editor->linesel = 0;
2940 ret = ME_SetCursor(editor, LOWORD(lParam));
2941 ME_LinkNotify(editor,msg,wParam,lParam);
2942 if (!ret) goto do_default;
2944 break;
2945 case WM_LBUTTONDBLCLK:
2946 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
2947 !ME_FilterEvent(editor, msg, &wParam, &lParam))
2948 return 0;
2949 ME_LinkNotify(editor,msg,wParam,lParam);
2950 ME_SelectWord(editor);
2951 break;
2952 case WM_RBUTTONUP:
2953 case WM_RBUTTONDOWN:
2954 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
2955 !ME_FilterEvent(editor, msg, &wParam, &lParam))
2956 return 0;
2957 goto do_default;
2958 case WM_CONTEXTMENU:
2959 if (!ME_ShowContextMenu(editor, (short)LOWORD(lParam), (short)HIWORD(lParam)))
2960 goto do_default;
2961 break;
2962 case WM_PAINT:
2963 if (editor->bRedraw)
2965 HDC hDC;
2966 PAINTSTRUCT ps;
2968 hDC = BeginPaint(hWnd, &ps);
2969 ME_PaintContent(editor, hDC, FALSE, &ps.rcPaint);
2970 EndPaint(hWnd, &ps);
2972 break;
2973 case WM_SETFOCUS:
2974 editor->bHaveFocus = TRUE;
2975 ME_ShowCaret(editor);
2976 ME_SendOldNotify(editor, EN_SETFOCUS);
2977 return 0;
2978 case WM_KILLFOCUS:
2979 ME_HideCaret(editor);
2980 editor->bHaveFocus = FALSE;
2981 ME_SendOldNotify(editor, EN_KILLFOCUS);
2982 return 0;
2983 case WM_ERASEBKGND:
2985 if (editor->bRedraw)
2987 HDC hDC = (HDC)wParam;
2988 RECT rc;
2989 if (GetUpdateRect(hWnd,&rc,TRUE))
2991 FillRect(hDC, &rc, editor->hbrBackground);
2994 return 1;
2996 case WM_COMMAND:
2997 TRACE("editor wnd command = %d\n", LOWORD(wParam));
2998 return 0;
2999 case WM_KEYUP:
3000 if ((editor->nEventMask & ENM_KEYEVENTS) &&
3001 !ME_FilterEvent(editor, msg, &wParam, &lParam))
3002 return 0;
3003 goto do_default;
3004 case WM_KEYDOWN:
3005 if ((editor->nEventMask & ENM_KEYEVENTS) &&
3006 !ME_FilterEvent(editor, msg, &wParam, &lParam))
3007 return 0;
3008 if (ME_KeyDown(editor, LOWORD(wParam)))
3009 return 0;
3010 goto do_default;
3011 case WM_CHAR:
3013 WCHAR wstr;
3015 if (unicode)
3016 wstr = (WCHAR)wParam;
3017 else
3019 CHAR charA = wParam;
3020 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &wstr, 1);
3022 if (editor->AutoURLDetect_bEnable)
3023 ME_AutoURLDetect(editor, wstr);
3025 switch (wstr)
3027 case 1: /* Ctrl-A */
3028 ME_SetSelection(editor, 0, -1);
3029 return 0;
3030 case 3: /* Ctrl-C */
3031 SendMessageW(editor->hWnd, WM_COPY, 0, 0);
3032 return 0;
3035 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY) {
3036 MessageBeep(MB_ICONERROR);
3037 return 0; /* FIXME really 0 ? */
3040 switch (wstr)
3042 case 22: /* Ctrl-V */
3043 SendMessageW(editor->hWnd, WM_PASTE, 0, 0);
3044 return 0;
3045 case 24: /* Ctrl-X */
3046 SendMessageW(editor->hWnd, WM_CUT, 0, 0);
3047 return 0;
3048 case 25: /* Ctrl-Y */
3049 SendMessageW(editor->hWnd, EM_REDO, 0, 0);
3050 return 0;
3051 case 26: /* Ctrl-Z */
3052 SendMessageW(editor->hWnd, EM_UNDO, 0, 0);
3053 return 0;
3055 if (((unsigned)wstr)>=' '
3056 || (wstr=='\r' && (GetWindowLongW(hWnd, GWL_STYLE) & ES_MULTILINE))
3057 || wstr=='\t') {
3058 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
3059 /* WM_CHAR is restricted to nTextLimit */
3060 int from, to;
3061 ME_GetSelection(editor, &from, &to);
3062 if(editor->nTextLimit > ME_GetTextLength(editor) - (to-from))
3064 ME_Style *style = ME_GetInsertStyle(editor, 0);
3065 ME_SaveTempStyle(editor);
3066 if (wstr == '\r' && (GetKeyState(VK_SHIFT) & 0x8000))
3067 ME_InsertEndRowFromCursor(editor, 0);
3068 else
3069 ME_InsertTextFromCursor(editor, 0, &wstr, 1, style);
3070 ME_ReleaseStyle(style);
3071 ME_CommitUndo(editor);
3073 ME_UpdateRepaint(editor);
3075 return 0;
3077 case EM_SCROLL: /* fall through */
3078 case WM_VSCROLL:
3080 int origNPos;
3081 int lineHeight;
3083 origNPos = ME_GetYScrollPos(editor);
3084 lineHeight = 24;
3086 if (editor && editor->pBuffer && editor->pBuffer->pDefaultStyle)
3087 lineHeight = editor->pBuffer->pDefaultStyle->tm.tmHeight;
3088 if (lineHeight <= 0) lineHeight = 24;
3090 switch(LOWORD(wParam))
3092 case SB_LINEUP:
3093 ME_ScrollUp(editor,lineHeight);
3094 break;
3095 case SB_LINEDOWN:
3096 ME_ScrollDown(editor,lineHeight);
3097 break;
3098 case SB_PAGEUP:
3099 ME_ScrollUp(editor,editor->sizeWindow.cy);
3100 break;
3101 case SB_PAGEDOWN:
3102 ME_ScrollDown(editor,editor->sizeWindow.cy);
3103 break;
3104 case SB_THUMBTRACK:
3105 case SB_THUMBPOSITION:
3106 ME_ScrollAbs(editor,HIWORD(wParam));
3107 break;
3109 if (msg == EM_SCROLL)
3110 return 0x00010000 | (((ME_GetYScrollPos(editor) - origNPos)/lineHeight) & 0xffff);
3111 break;
3113 case WM_MOUSEWHEEL:
3115 int gcWheelDelta;
3116 UINT pulScrollLines;
3118 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
3119 !ME_FilterEvent(editor, msg, &wParam, &lParam))
3120 return 0;
3122 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
3123 gcWheelDelta = -GET_WHEEL_DELTA_WPARAM(wParam);
3125 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
3127 /* FIXME follow the original */
3128 ME_ScrollDown(editor,pulScrollLines * (gcWheelDelta / WHEEL_DELTA) * 8);
3130 break;
3132 case EM_GETRECT:
3134 *((RECT *)lParam) = editor->rcFormat;
3135 return 0;
3137 case EM_SETRECT:
3138 case EM_SETRECTNP:
3140 if (lParam)
3142 RECT *rc = (RECT *)lParam;
3144 if (wParam)
3146 editor->rcFormat.left += rc->left;
3147 editor->rcFormat.top += rc->top;
3148 editor->rcFormat.right += rc->right;
3149 editor->rcFormat.bottom += rc->bottom;
3151 else
3153 editor->rcFormat = *rc;
3156 else
3158 GetClientRect(hWnd, &editor->rcFormat);
3160 if (msg != EM_SETRECTNP)
3161 ME_RewrapRepaint(editor);
3162 return 0;
3164 case EM_REQUESTRESIZE:
3165 ME_SendRequestResize(editor, TRUE);
3166 return 0;
3167 case WM_SETREDRAW:
3168 if ((editor->bRedraw = wParam))
3169 ME_RewrapRepaint(editor);
3170 return 0;
3171 case WM_SIZE:
3173 GetClientRect(hWnd, &editor->rcFormat);
3174 ME_RewrapRepaint(editor);
3175 return DefWindowProcW(hWnd, msg, wParam, lParam);
3177 /* IME messages to make richedit controls IME aware */
3178 case WM_IME_SETCONTEXT:
3179 case WM_IME_CONTROL:
3180 case WM_IME_SELECT:
3181 case WM_IME_COMPOSITIONFULL:
3182 return 0;
3183 case WM_IME_STARTCOMPOSITION:
3185 editor->imeStartIndex=ME_GetCursorOfs(editor,0);
3186 ME_DeleteSelection(editor);
3187 ME_CommitUndo(editor);
3188 ME_UpdateRepaint(editor);
3189 return 0;
3191 case WM_IME_COMPOSITION:
3193 HIMC hIMC;
3195 ME_Style *style = ME_GetInsertStyle(editor, 0);
3196 hIMC = ImmGetContext(hWnd);
3197 ME_DeleteSelection(editor);
3198 ME_CommitUndo(editor);
3199 ME_SaveTempStyle(editor);
3200 if (lParam & GCS_RESULTSTR)
3202 LPWSTR lpCompStr = NULL;
3203 DWORD dwBufLen;
3205 dwBufLen = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
3206 lpCompStr = HeapAlloc(GetProcessHeap(),0,dwBufLen + sizeof(WCHAR));
3207 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, lpCompStr, dwBufLen);
3208 lpCompStr[dwBufLen/sizeof(WCHAR)] = 0;
3209 ME_InsertTextFromCursor(editor,0,lpCompStr,dwBufLen/sizeof(WCHAR),style);
3211 else if (lParam & GCS_COMPSTR)
3213 LPWSTR lpCompStr = NULL;
3214 DWORD dwBufLen;
3216 dwBufLen = ImmGetCompositionStringW(hIMC, GCS_COMPSTR, NULL, 0);
3217 lpCompStr = HeapAlloc(GetProcessHeap(),0,dwBufLen + sizeof(WCHAR));
3218 ImmGetCompositionStringW(hIMC, GCS_COMPSTR, lpCompStr, dwBufLen);
3219 lpCompStr[dwBufLen/sizeof(WCHAR)] = 0;
3221 ME_InsertTextFromCursor(editor,0,lpCompStr,dwBufLen/sizeof(WCHAR),style);
3222 ME_SetSelection(editor,editor->imeStartIndex,
3223 editor->imeStartIndex + dwBufLen/sizeof(WCHAR));
3225 ME_ReleaseStyle(style);
3226 ME_UpdateRepaint(editor);
3227 return 0;
3229 case WM_IME_ENDCOMPOSITION:
3231 ME_DeleteSelection(editor);
3232 editor->imeStartIndex=-1;
3233 return 0;
3235 case EM_GETOLEINTERFACE:
3237 LPVOID *ppvObj = (LPVOID*) lParam;
3238 return CreateIRichEditOle(editor, ppvObj);
3240 case EM_GETPASSWORDCHAR:
3242 return editor->cPasswordMask;
3244 case EM_SETOLECALLBACK:
3245 if(editor->lpOleCallback)
3246 IUnknown_Release(editor->lpOleCallback);
3247 editor->lpOleCallback = (LPRICHEDITOLECALLBACK)lParam;
3248 if(editor->lpOleCallback)
3249 IUnknown_AddRef(editor->lpOleCallback);
3250 return TRUE;
3251 case EM_GETWORDBREAKPROC:
3252 return (LRESULT)editor->pfnWordBreak;
3253 case EM_SETWORDBREAKPROC:
3255 EDITWORDBREAKPROCW pfnOld = editor->pfnWordBreak;
3257 editor->pfnWordBreak = (EDITWORDBREAKPROCW)lParam;
3258 return (LRESULT)pfnOld;
3260 case EM_SETTEXTMODE:
3262 LRESULT ret;
3263 int mask = 0;
3264 int changes = 0;
3265 ret = RichEditWndProc_common(hWnd, WM_GETTEXTLENGTH, 0, 0, unicode);
3266 if (!ret)
3268 /*Check for valid wParam*/
3269 if ((((wParam & TM_RICHTEXT) && ((wParam & TM_PLAINTEXT) << 1))) ||
3270 (((wParam & TM_MULTILEVELUNDO) && ((wParam & TM_SINGLELEVELUNDO) << 1))) ||
3271 (((wParam & TM_MULTICODEPAGE) && ((wParam & TM_SINGLECODEPAGE) << 1))))
3272 return 1;
3273 else
3275 if (wParam & (TM_RICHTEXT | TM_PLAINTEXT))
3277 mask |= (TM_RICHTEXT | TM_PLAINTEXT);
3278 changes |= (wParam & (TM_RICHTEXT | TM_PLAINTEXT));
3280 /*FIXME: Currently no support for undo level and code page options*/
3281 editor->mode = (editor->mode & (~mask)) | changes;
3282 return 0;
3285 return ret;
3287 case EM_SETPASSWORDCHAR:
3289 editor->cPasswordMask = wParam;
3290 ME_RewrapRepaint(editor);
3291 return 0;
3293 case EM_SETTARGETDEVICE:
3294 if (wParam == 0)
3296 BOOL new = (lParam == 0);
3297 if (editor->bWordWrap != new)
3299 editor->bWordWrap = new;
3300 ME_RewrapRepaint(editor);
3303 else FIXME("Unsupported yet non NULL device in EM_SETTARGETDEVICE\n");
3304 break;
3305 default:
3306 do_default:
3307 return DefWindowProcW(hWnd, msg, wParam, lParam);
3309 return 0L;
3312 static LRESULT WINAPI RichEditWndProcW(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
3314 BOOL unicode = TRUE;
3316 /* Under Win9x RichEdit20W returns ANSI strings, see the tests. */
3317 if (msg == WM_GETTEXT && (GetVersion() & 0x80000000))
3318 unicode = FALSE;
3320 return RichEditWndProc_common(hWnd, msg, wParam, lParam, unicode);
3323 static LRESULT WINAPI RichEditWndProcA(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
3325 return RichEditWndProc_common(hWnd, msg, wParam, lParam, FALSE);
3328 /******************************************************************
3329 * RichEditANSIWndProc (RICHED20.10)
3331 LRESULT WINAPI RichEditANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
3333 return RichEditWndProcA(hWnd, msg, wParam, lParam);
3336 /******************************************************************
3337 * RichEdit10ANSIWndProc (RICHED20.9)
3339 LRESULT WINAPI RichEdit10ANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
3341 LRESULT result;
3343 /* FIXME: this is NOT the same as 2.0 version */
3344 result = RichEditANSIWndProc(hWnd, msg, wParam, lParam);
3345 if (msg == WM_NCCREATE)
3347 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongPtrW(hWnd, 0);
3349 editor->bEmulateVersion10 = TRUE;
3350 editor->pBuffer->pLast->member.para.nCharOfs = 2;
3351 assert(editor->pBuffer->pLast->prev->type == diRun);
3352 assert(editor->pBuffer->pLast->prev->member.run.nFlags & MERF_ENDPARA);
3353 editor->pBuffer->pLast->prev->member.run.nLF = 1;
3355 return result;
3358 void ME_SendOldNotify(ME_TextEditor *editor, int nCode)
3360 HWND hWnd = editor->hWnd;
3361 SendMessageA(GetParent(hWnd), WM_COMMAND, (nCode<<16)|GetWindowLongW(hWnd, GWLP_ID), (LPARAM)hWnd);
3364 void ME_LinkNotify(ME_TextEditor *editor, UINT msg, WPARAM wParam, LPARAM lParam)
3366 int x,y;
3367 ME_Cursor tmpCursor;
3368 int nCharOfs; /* The start of the clicked text. Absolute character offset */
3370 ME_Run *tmpRun;
3372 ENLINK info;
3373 x = (short)LOWORD(lParam);
3374 y = (short)HIWORD(lParam);
3375 nCharOfs = ME_CharFromPos(editor, x, y);
3376 if (nCharOfs < 0) return;
3378 ME_CursorFromCharOfs(editor, nCharOfs, &tmpCursor);
3379 tmpRun = &tmpCursor.pRun->member.run;
3381 if ((tmpRun->style->fmt.dwMask & CFM_LINK)
3382 && (tmpRun->style->fmt.dwEffects & CFE_LINK))
3383 { /* The clicked run has CFE_LINK set */
3384 info.nmhdr.hwndFrom = editor->hWnd;
3385 info.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
3386 info.nmhdr.code = EN_LINK;
3387 info.msg = msg;
3388 info.wParam = wParam;
3389 info.lParam = lParam;
3390 info.chrg.cpMin = ME_CharOfsFromRunOfs(editor,tmpCursor.pRun,0);
3391 info.chrg.cpMax = info.chrg.cpMin + ME_StrVLen(tmpRun->strText);
3392 SendMessageW(GetParent(editor->hWnd), WM_NOTIFY,info.nmhdr.idFrom, (LPARAM)&info);
3396 int ME_CountParagraphsBetween(ME_TextEditor *editor, int from, int to)
3398 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
3399 int i = 0;
3401 while(item && item->member.para.next_para->member.para.nCharOfs <= from)
3402 item = item->member.para.next_para;
3403 if (!item)
3404 return 0;
3405 while(item && item->member.para.next_para->member.para.nCharOfs <= to) {
3406 item = item->member.para.next_para;
3407 i++;
3409 return i;
3413 int ME_GetTextW(ME_TextEditor *editor, WCHAR *buffer, int nStart, int nChars, int bCRLF)
3415 ME_DisplayItem *item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
3416 int nWritten = 0;
3417 WCHAR *pStart = buffer;
3419 if (!item) {
3420 *buffer = 0;
3421 return 0;
3424 /* bCRLF flag is only honored in 2.0 and up. 1.0 must always return text verbatim */
3425 if (editor->bEmulateVersion10) bCRLF = 0;
3427 if (nStart)
3429 int nLen = ME_StrLen(item->member.run.strText) - nStart;
3430 if (nLen > nChars)
3431 nLen = nChars;
3432 CopyMemory(buffer, item->member.run.strText->szData + nStart, sizeof(WCHAR)*nLen);
3433 nChars -= nLen;
3434 nWritten += nLen;
3435 if (!nChars)
3436 return nWritten;
3437 buffer += nLen;
3438 nStart = 0;
3439 item = ME_FindItemFwd(item, diRun);
3442 while(nChars && item)
3444 int nLen = ME_StrLen(item->member.run.strText);
3445 if (item->member.run.nFlags & MERF_ENDPARA)
3446 nLen = item->member.run.nCR + item->member.run.nLF;
3447 if (nLen > nChars)
3448 nLen = nChars;
3450 if (item->member.run.nFlags & MERF_ENDPARA)
3452 if (!ME_FindItemFwd(item, diRun))
3453 /* No '\r' is appended to the last paragraph. */
3454 nLen = 0;
3455 else if (bCRLF && nChars == 1) {
3456 nLen = 0;
3457 nChars = 0;
3458 } else {
3459 if (bCRLF)
3461 /* richedit 2.0 case - actual line-break is \r but should report \r\n */
3462 assert(nLen == 1);
3463 *buffer++ = '\r';
3464 *buffer = '\n'; /* Later updated by nLen==1 at the end of the loop */
3465 nWritten++;
3467 else
3469 int i, j;
3471 /* richedit 2.0 verbatim has only \r. richedit 1.0 should honor encodings */
3472 i = 0;
3473 while (nChars - i > 0 && i < item->member.run.nCR)
3475 buffer[i] = '\r'; i++;
3477 j = 0;
3478 while (nChars - i - j > 0 && j < item->member.run.nLF)
3480 buffer[i+j] = '\n'; j++;
3485 else
3486 CopyMemory(buffer, item->member.run.strText->szData, sizeof(WCHAR)*nLen);
3487 nChars -= nLen;
3488 nWritten += nLen;
3489 buffer += nLen;
3491 if (!nChars)
3493 TRACE("nWritten=%d, actual=%d\n", nWritten, buffer-pStart);
3494 *buffer = 0;
3495 return nWritten;
3497 item = ME_FindItemFwd(item, diRun);
3499 *buffer = 0;
3500 TRACE("nWritten=%d, actual=%d\n", nWritten, buffer-pStart);
3501 return nWritten;
3504 static BOOL ME_RegisterEditorClass(HINSTANCE hInstance)
3506 WNDCLASSW wcW;
3507 WNDCLASSA wcA;
3509 wcW.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
3510 wcW.lpfnWndProc = RichEditWndProcW;
3511 wcW.cbClsExtra = 0;
3512 wcW.cbWndExtra = sizeof(ME_TextEditor *);
3513 wcW.hInstance = NULL; /* hInstance would register DLL-local class */
3514 wcW.hIcon = NULL;
3515 wcW.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
3516 wcW.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
3517 wcW.lpszMenuName = NULL;
3519 if (is_version_nt())
3521 wcW.lpszClassName = RichEdit20W;
3522 if (!RegisterClassW(&wcW)) return FALSE;
3523 wcW.lpszClassName = RichEdit50W;
3524 if (!RegisterClassW(&wcW)) return FALSE;
3526 else
3528 /* WNDCLASSA/W have the same layout */
3529 wcW.lpszClassName = (LPCWSTR)"RichEdit20W";
3530 if (!RegisterClassA((WNDCLASSA *)&wcW)) return FALSE;
3531 wcW.lpszClassName = (LPCWSTR)"RichEdit50W";
3532 if (!RegisterClassA((WNDCLASSA *)&wcW)) return FALSE;
3535 wcA.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
3536 wcA.lpfnWndProc = RichEditWndProcA;
3537 wcA.cbClsExtra = 0;
3538 wcA.cbWndExtra = sizeof(ME_TextEditor *);
3539 wcA.hInstance = NULL; /* hInstance would register DLL-local class */
3540 wcA.hIcon = NULL;
3541 wcA.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
3542 wcA.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
3543 wcA.lpszMenuName = NULL;
3544 wcA.lpszClassName = "RichEdit20A";
3545 if (!RegisterClassA(&wcA)) return FALSE;
3546 wcA.lpszClassName = "RichEdit50A";
3547 if (!RegisterClassA(&wcA)) return FALSE;
3549 return TRUE;
3552 LRESULT WINAPI REComboWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
3553 /* FIXME: Not implemented */
3554 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
3555 hWnd, msg, get_msg_name(msg), wParam, lParam);
3556 return DefWindowProcW(hWnd, msg, wParam, lParam);
3559 LRESULT WINAPI REListWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
3560 /* FIXME: Not implemented */
3561 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
3562 hWnd, msg, get_msg_name(msg), wParam, lParam);
3563 return DefWindowProcW(hWnd, msg, wParam, lParam);
3566 /******************************************************************
3567 * REExtendedRegisterClass (RICHED20.8)
3569 * FIXME undocumented
3570 * Need to check for errors and implement controls and callbacks
3572 LRESULT WINAPI REExtendedRegisterClass(void)
3574 WNDCLASSW wcW;
3575 UINT result;
3577 FIXME("semi stub\n");
3579 wcW.cbClsExtra = 0;
3580 wcW.cbWndExtra = 4;
3581 wcW.hInstance = NULL;
3582 wcW.hIcon = NULL;
3583 wcW.hCursor = NULL;
3584 wcW.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
3585 wcW.lpszMenuName = NULL;
3587 if (!ME_ListBoxRegistered)
3589 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS;
3590 wcW.lpfnWndProc = REListWndProc;
3591 wcW.lpszClassName = REListBox20W;
3592 if (RegisterClassW(&wcW)) ME_ListBoxRegistered = TRUE;
3595 if (!ME_ComboBoxRegistered)
3597 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
3598 wcW.lpfnWndProc = REComboWndProc;
3599 wcW.lpszClassName = REComboBox20W;
3600 if (RegisterClassW(&wcW)) ME_ComboBoxRegistered = TRUE;
3603 result = 0;
3604 if (ME_ListBoxRegistered)
3605 result += 1;
3606 if (ME_ComboBoxRegistered)
3607 result += 2;
3609 return result;
3612 int ME_AutoURLDetect(ME_TextEditor *editor, WCHAR curChar)
3614 struct prefix_s {
3615 const char *text;
3616 int length;
3617 } prefixes[12] = {
3618 {"http:", 5},
3619 {"file:", 6},
3620 {"mailto:", 8},
3621 {"ftp:", 5},
3622 {"https:", 7},
3623 {"gopher:", 8},
3624 {"nntp:", 6},
3625 {"prospero:", 10},
3626 {"telnet:", 8},
3627 {"news:", 6},
3628 {"wais:", 6},
3629 {"www.", 5}
3631 CHARRANGE ins_pt;
3632 int curf_ef, link_ef, def_ef;
3633 int cur_prefx, prefx_cnt;
3634 int sel_min, sel_max;
3635 int car_pos = 0;
3636 int text_pos=-1;
3637 int URLmin, URLmax = 0;
3638 FINDTEXTA ft;
3639 CHARFORMAT2W cur_format;
3640 CHARFORMAT2W default_format;
3641 CHARFORMAT2W link;
3642 RichEditANSIWndProc(editor->hWnd, EM_EXGETSEL, (WPARAM) 0, (LPARAM) &ins_pt);
3643 sel_min = ins_pt.cpMin;
3644 sel_max = ins_pt.cpMax;
3645 if (sel_min==sel_max)
3646 car_pos = sel_min;
3647 if (sel_min!=sel_max)
3648 car_pos = ME_GetTextLength(editor)+1;
3649 cur_format.cbSize = sizeof(cur_format);
3650 default_format.cbSize = sizeof(default_format);
3651 RichEditANSIWndProc(editor->hWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM) &cur_format);
3652 RichEditANSIWndProc(editor->hWnd, EM_GETCHARFORMAT, SCF_DEFAULT, (LPARAM) &default_format);
3653 link.cbSize = sizeof(link);
3654 link.dwMask = CFM_LINK;
3655 link.dwEffects = CFE_LINK;
3656 curf_ef = cur_format.dwEffects & link.dwEffects;
3657 def_ef = default_format.dwEffects & link.dwEffects;
3658 link_ef = link.dwEffects & link.dwEffects;
3659 if (curf_ef == link_ef)
3661 if( curChar == '\n' || curChar=='\r' || curChar==' ')
3663 ME_SetSelection(editor, car_pos, car_pos);
3664 RichEditANSIWndProc(editor->hWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &default_format);
3665 text_pos=-1;
3666 return 0;
3669 if (curf_ef == def_ef)
3671 cur_prefx = 0;
3672 prefx_cnt = (sizeof(prefixes)/sizeof(struct prefix_s))-1;
3673 while (cur_prefx<=prefx_cnt)
3675 if (text_pos == -1)
3677 ft.lpstrText = prefixes[cur_prefx].text;
3678 URLmin=max(0,(car_pos-prefixes[cur_prefx].length));
3679 URLmax=max(0, car_pos);
3680 if ((car_pos == 0) && (ME_GetTextLength(editor) != 0))
3682 URLmax = ME_GetTextLength(editor)+1;
3684 ft.chrg.cpMin = URLmin;
3685 ft.chrg.cpMax = URLmax;
3686 text_pos=RichEditANSIWndProc(editor->hWnd, EM_FINDTEXT, FR_DOWN, (LPARAM)&ft);
3687 cur_prefx++;
3689 if (text_pos != -1)
3691 ME_SetCharFormat(editor, text_pos, (URLmax-text_pos), &link);
3692 ME_RewrapRepaint(editor);
3693 break;
3697 return 0;