richedit: Accept RTF stream that does not start with {\rtf1 in 1.0 emulation.
[wine/wine64.git] / dlls / riched20 / editor.c
blob9c4a013addfbecc0bf01dc127386b5670687072d
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)
1027 RTF_Info parser;
1028 ME_Style *style;
1029 int from, to, to2, nUndoMode;
1030 int nEventMask = editor->nEventMask;
1031 ME_InStream inStream;
1033 TRACE("stream==%p hWnd==%p format==0x%X\n", stream, editor->hWnd, format);
1034 editor->nEventMask = 0;
1036 ME_GetSelection(editor, &from, &to);
1037 if ((format & SFF_SELECTION) && (editor->mode & TM_RICHTEXT)) {
1038 style = ME_GetSelectionInsertStyle(editor);
1040 ME_InternalDeleteText(editor, from, to-from);
1042 else {
1043 style = editor->pBuffer->pDefaultStyle;
1044 ME_AddRefStyle(style);
1045 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
1046 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
1047 from = to = 0;
1048 ME_ClearTempStyle(editor);
1049 /* FIXME restore default paragraph formatting ! */
1053 /* Back up undo mode to a local variable */
1054 nUndoMode = editor->nUndoMode;
1056 /* Only create an undo if SFF_SELECTION is set */
1057 if (!(format & SFF_SELECTION))
1058 editor->nUndoMode = umIgnore;
1060 inStream.editstream = stream;
1061 inStream.editstream->dwError = 0;
1062 inStream.dwSize = 0;
1063 inStream.dwUsed = 0;
1065 if (format & SF_RTF)
1067 /* Check if it's really RTF, and if it is not, use plain text */
1068 ME_StreamInFill(&inStream);
1069 if (!inStream.editstream->dwError)
1071 if ((!editor->bEmulateVersion10 && strncmp(inStream.buffer, "{\\rtf", 5) && strncmp(inStream.buffer, "{\\urtf", 6))
1072 || (editor->bEmulateVersion10 && *inStream.buffer != '{'))
1074 format &= ~SF_RTF;
1075 format |= SF_TEXT;
1080 if (!inStream.editstream->dwError)
1082 if (format & SF_RTF) {
1083 /* setup the RTF parser */
1084 memset(&parser, 0, sizeof parser);
1085 RTFSetEditStream(&parser, &inStream);
1086 parser.rtfFormat = format&(SF_TEXT|SF_RTF);
1087 parser.hwndEdit = editor->hWnd;
1088 parser.editor = editor;
1089 parser.style = style;
1090 WriterInit(&parser);
1091 RTFInit(&parser);
1092 RTFSetReadHook(&parser, ME_RTFReadHook);
1093 RTFSetDestinationCallback(&parser, rtfPict, ME_RTFReadPictGroup);
1094 RTFSetDestinationCallback(&parser, rtfObject, ME_RTFReadObjectGroup);
1095 BeginFile(&parser);
1097 /* do the parsing */
1098 RTFRead(&parser);
1099 RTFFlushOutputBuffer(&parser);
1100 RTFDestroy(&parser);
1101 if (parser.lpRichEditOle)
1102 IRichEditOle_Release(parser.lpRichEditOle);
1104 style = parser.style;
1106 else if (format & SF_TEXT)
1107 ME_StreamInText(editor, format, &inStream, style);
1108 else
1109 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
1110 ME_GetSelection(editor, &to, &to2);
1111 /* put the cursor at the top */
1112 if (!(format & SFF_SELECTION))
1113 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
1116 /* Restore saved undo mode */
1117 editor->nUndoMode = nUndoMode;
1119 /* even if we didn't add an undo, we need to commit anything on the stack */
1120 ME_CommitUndo(editor);
1122 /* If SFF_SELECTION isn't set, delete any undos from before we started too */
1123 if (!(format & SFF_SELECTION))
1124 ME_EmptyUndoStack(editor);
1126 ME_ReleaseStyle(style);
1127 editor->nEventMask = nEventMask;
1128 if (editor->bRedraw)
1130 ME_UpdateRepaint(editor);
1132 if (!(format & SFF_SELECTION)) {
1133 ME_ClearTempStyle(editor);
1135 ME_MoveCaret(editor);
1136 ME_SendSelChange(editor);
1137 ME_SendRequestResize(editor, FALSE);
1139 return 0;
1143 typedef struct tagME_RTFStringStreamStruct
1145 char *string;
1146 int pos;
1147 int length;
1148 } ME_RTFStringStreamStruct;
1150 static DWORD CALLBACK ME_ReadFromRTFString(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1152 ME_RTFStringStreamStruct *pStruct = (ME_RTFStringStreamStruct *)dwCookie;
1153 int count;
1155 count = min(cb, pStruct->length - pStruct->pos);
1156 memmove(lpBuff, pStruct->string + pStruct->pos, count);
1157 pStruct->pos += count;
1158 *pcb = count;
1159 return 0;
1162 static void
1163 ME_StreamInRTFString(ME_TextEditor *editor, BOOL selection, char *string)
1165 EDITSTREAM es;
1166 ME_RTFStringStreamStruct data;
1168 data.string = string;
1169 data.length = strlen(string);
1170 data.pos = 0;
1171 es.dwCookie = (DWORD)&data;
1172 es.pfnCallback = ME_ReadFromRTFString;
1173 ME_StreamIn(editor, SF_RTF | (selection ? SFF_SELECTION : 0), &es);
1177 ME_DisplayItem *
1178 ME_FindItemAtOffset(ME_TextEditor *editor, ME_DIType nItemType, int nOffset, int *nItemOffset)
1180 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
1182 while (item && item->member.para.next_para->member.para.nCharOfs <= nOffset)
1183 item = ME_FindItemFwd(item, diParagraph);
1185 if (!item)
1186 return item;
1188 nOffset -= item->member.para.nCharOfs;
1189 if (nItemType == diParagraph) {
1190 if (nItemOffset)
1191 *nItemOffset = nOffset;
1192 return item;
1195 do {
1196 item = ME_FindItemFwd(item, diRun);
1197 } while (item && (item->member.run.nCharOfs + ME_StrLen(item->member.run.strText) <= nOffset));
1198 if (item) {
1199 nOffset -= item->member.run.nCharOfs;
1200 if (nItemOffset)
1201 *nItemOffset = nOffset;
1203 return item;
1207 static int
1208 ME_FindText(ME_TextEditor *editor, DWORD flags, const CHARRANGE *chrg, const WCHAR *text, CHARRANGE *chrgText)
1210 const int nLen = lstrlenW(text);
1211 const int nTextLen = ME_GetTextLength(editor);
1212 int nStart, nEnd;
1213 int nMin, nMax;
1214 ME_DisplayItem *item;
1215 ME_DisplayItem *para;
1216 WCHAR wLastChar = ' ';
1218 TRACE("flags==0x%08x, chrg->cpMin==%d, chrg->cpMax==%d text==%s\n",
1219 flags, chrg->cpMin, chrg->cpMax, debugstr_w(text));
1221 if (flags & ~(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD))
1222 FIXME("Flags 0x%08x not implemented\n",
1223 flags & ~(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD));
1225 nMin = chrg->cpMin;
1226 if (chrg->cpMax == -1)
1227 nMax = nTextLen;
1228 else
1229 nMax = chrg->cpMax > nTextLen ? nTextLen : chrg->cpMax;
1231 /* when searching up, if cpMin < cpMax, then instead of searching
1232 * on [cpMin,cpMax], we search on [0,cpMin], otherwise, search on
1233 * [cpMax, cpMin]. The exception is when cpMax is -1, in which
1234 * case, it is always bigger than cpMin.
1236 if (!(flags & FR_DOWN))
1238 int nSwap = nMax;
1240 nMax = nMin > nTextLen ? nTextLen : nMin;
1241 if (nMin < nSwap || chrg->cpMax == -1)
1242 nMin = 0;
1243 else
1244 nMin = nSwap;
1247 if (!nLen || nMin < 0 || nMax < 0 || nMax < nMin)
1249 if (chrgText)
1250 chrgText->cpMin = chrgText->cpMax = -1;
1251 return -1;
1254 if (flags & FR_DOWN) /* Forward search */
1256 /* If possible, find the character before where the search starts */
1257 if ((flags & FR_WHOLEWORD) && nMin)
1259 nStart = nMin - 1;
1260 item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
1261 if (!item)
1263 if (chrgText)
1264 chrgText->cpMin = chrgText->cpMax = -1;
1265 return -1;
1267 wLastChar = item->member.run.strText->szData[nStart];
1270 nStart = nMin;
1271 item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
1272 if (!item)
1274 if (chrgText)
1275 chrgText->cpMin = chrgText->cpMax = -1;
1276 return -1;
1279 para = ME_GetParagraph(item);
1280 while (item
1281 && para->member.para.nCharOfs + item->member.run.nCharOfs + nStart + nLen <= nMax)
1283 ME_DisplayItem *pCurItem = item;
1284 int nCurStart = nStart;
1285 int nMatched = 0;
1287 while (pCurItem && ME_CharCompare(pCurItem->member.run.strText->szData[nCurStart + nMatched], text[nMatched], (flags & FR_MATCHCASE)))
1289 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
1290 break;
1292 nMatched++;
1293 if (nMatched == nLen)
1295 ME_DisplayItem *pNextItem = pCurItem;
1296 int nNextStart = nCurStart;
1297 WCHAR wNextChar;
1299 /* Check to see if next character is a whitespace */
1300 if (flags & FR_WHOLEWORD)
1302 if (nCurStart + nMatched == ME_StrLen(pCurItem->member.run.strText))
1304 pNextItem = ME_FindItemFwd(pCurItem, diRun);
1305 nNextStart = -nMatched;
1308 if (pNextItem)
1309 wNextChar = pNextItem->member.run.strText->szData[nNextStart + nMatched];
1310 else
1311 wNextChar = ' ';
1313 if (isalnumW(wNextChar))
1314 break;
1317 nStart += para->member.para.nCharOfs + pCurItem->member.run.nCharOfs;
1318 if (chrgText)
1320 chrgText->cpMin = nStart;
1321 chrgText->cpMax = nStart + nLen;
1323 TRACE("found at %d-%d\n", nStart, nStart + nLen);
1324 return nStart;
1326 if (nCurStart + nMatched == ME_StrLen(pCurItem->member.run.strText))
1328 pCurItem = ME_FindItemFwd(pCurItem, diRun);
1329 para = ME_GetParagraph(pCurItem);
1330 nCurStart = -nMatched;
1333 if (pCurItem)
1334 wLastChar = pCurItem->member.run.strText->szData[nCurStart + nMatched];
1335 else
1336 wLastChar = ' ';
1338 nStart++;
1339 if (nStart == ME_StrLen(item->member.run.strText))
1341 item = ME_FindItemFwd(item, diRun);
1342 para = ME_GetParagraph(item);
1343 nStart = 0;
1347 else /* Backward search */
1349 /* If possible, find the character after where the search ends */
1350 if ((flags & FR_WHOLEWORD) && nMax < nTextLen - 1)
1352 nEnd = nMax + 1;
1353 item = ME_FindItemAtOffset(editor, diRun, nEnd, &nEnd);
1354 if (!item)
1356 if (chrgText)
1357 chrgText->cpMin = chrgText->cpMax = -1;
1358 return -1;
1360 wLastChar = item->member.run.strText->szData[nEnd];
1363 nEnd = nMax;
1364 item = ME_FindItemAtOffset(editor, diRun, nEnd, &nEnd);
1365 if (!item)
1367 if (chrgText)
1368 chrgText->cpMin = chrgText->cpMax = -1;
1369 return -1;
1372 para = ME_GetParagraph(item);
1374 while (item
1375 && para->member.para.nCharOfs + item->member.run.nCharOfs + nEnd - nLen >= nMin)
1377 ME_DisplayItem *pCurItem = item;
1378 int nCurEnd = nEnd;
1379 int nMatched = 0;
1381 if (nCurEnd - nMatched == 0)
1383 pCurItem = ME_FindItemBack(pCurItem, diRun);
1384 para = ME_GetParagraph(pCurItem);
1385 nCurEnd = ME_StrLen(pCurItem->member.run.strText) + nMatched;
1388 while (pCurItem && ME_CharCompare(pCurItem->member.run.strText->szData[nCurEnd - nMatched - 1], text[nLen - nMatched - 1], (flags & FR_MATCHCASE)))
1390 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
1391 break;
1393 nMatched++;
1394 if (nMatched == nLen)
1396 ME_DisplayItem *pPrevItem = pCurItem;
1397 int nPrevEnd = nCurEnd;
1398 WCHAR wPrevChar;
1400 /* Check to see if previous character is a whitespace */
1401 if (flags & FR_WHOLEWORD)
1403 if (nPrevEnd - nMatched == 0)
1405 pPrevItem = ME_FindItemBack(pCurItem, diRun);
1406 if (pPrevItem)
1407 nPrevEnd = ME_StrLen(pPrevItem->member.run.strText) + nMatched;
1410 if (pPrevItem)
1411 wPrevChar = pPrevItem->member.run.strText->szData[nPrevEnd - nMatched - 1];
1412 else
1413 wPrevChar = ' ';
1415 if (isalnumW(wPrevChar))
1416 break;
1419 nStart = para->member.para.nCharOfs + pCurItem->member.run.nCharOfs + nCurEnd - nMatched;
1420 if (chrgText)
1422 chrgText->cpMin = nStart;
1423 chrgText->cpMax = nStart + nLen;
1425 TRACE("found at %d-%d\n", nStart, nStart + nLen);
1426 return nStart;
1428 if (nCurEnd - nMatched == 0)
1430 pCurItem = ME_FindItemBack(pCurItem, diRun);
1431 /* Don't care about pCurItem becoming NULL here; it's already taken
1432 * care of in the exterior loop condition */
1433 para = ME_GetParagraph(pCurItem);
1434 nCurEnd = ME_StrLen(pCurItem->member.run.strText) + nMatched;
1437 if (pCurItem)
1438 wLastChar = pCurItem->member.run.strText->szData[nCurEnd - nMatched - 1];
1439 else
1440 wLastChar = ' ';
1442 nEnd--;
1443 if (nEnd < 0)
1445 item = ME_FindItemBack(item, diRun);
1446 para = ME_GetParagraph(item);
1447 nEnd = ME_StrLen(item->member.run.strText);
1451 TRACE("not found\n");
1452 if (chrgText)
1453 chrgText->cpMin = chrgText->cpMax = -1;
1454 return -1;
1457 /* helper to send a msg filter notification */
1458 static BOOL
1459 ME_FilterEvent(ME_TextEditor *editor, UINT msg, WPARAM* wParam, LPARAM* lParam)
1461 MSGFILTER msgf;
1463 msgf.nmhdr.hwndFrom = editor->hWnd;
1464 msgf.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
1465 msgf.nmhdr.code = EN_MSGFILTER;
1466 msgf.msg = msg;
1468 msgf.wParam = *wParam;
1469 msgf.lParam = *lParam;
1470 if (SendMessageW(GetParent(editor->hWnd), WM_NOTIFY, msgf.nmhdr.idFrom, (LPARAM)&msgf))
1471 return FALSE;
1472 *wParam = msgf.wParam;
1473 *lParam = msgf.lParam;
1474 msgf.wParam = *wParam;
1476 return TRUE;
1479 static BOOL
1480 ME_KeyDown(ME_TextEditor *editor, WORD nKey)
1482 BOOL ctrl_is_down = GetKeyState(VK_CONTROL) & 0x8000;
1483 BOOL shift_is_down = GetKeyState(VK_SHIFT) & 0x8000;
1485 switch (nKey)
1487 case VK_LEFT:
1488 case VK_RIGHT:
1489 case VK_UP:
1490 case VK_DOWN:
1491 case VK_HOME:
1492 case VK_END:
1493 case VK_PRIOR:
1494 case VK_NEXT:
1495 ME_ArrowKey(editor, nKey, shift_is_down, ctrl_is_down);
1496 return TRUE;
1497 case VK_BACK:
1498 case VK_DELETE:
1499 /* FIXME backspace and delete aren't the same, they act different wrt paragraph style of the merged paragraph */
1500 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY)
1501 return FALSE;
1502 if (ME_IsSelection(editor))
1503 ME_DeleteSelection(editor);
1504 else if (nKey == VK_DELETE || ME_ArrowKey(editor, VK_LEFT, FALSE, FALSE))
1505 ME_DeleteTextAtCursor(editor, 1, 1);
1506 else
1507 return TRUE;
1508 ME_CommitUndo(editor);
1509 ME_UpdateRepaint(editor);
1510 ME_SendRequestResize(editor, FALSE);
1511 return TRUE;
1513 default:
1514 if (ctrl_is_down)
1516 if (nKey == 'W')
1518 CHARFORMAT2W chf;
1519 char buf[2048];
1520 chf.cbSize = sizeof(chf);
1522 ME_GetSelectionCharFormat(editor, &chf);
1523 ME_DumpStyleToBuf(&chf, buf);
1524 MessageBoxA(NULL, buf, "Style dump", MB_OK);
1526 if (nKey == 'Q')
1528 ME_CheckCharOffsets(editor);
1532 return FALSE;
1535 static BOOL ME_SetCursor(ME_TextEditor *editor, int x)
1537 if ((GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_SELECTIONBAR) &&
1538 (x < editor->selofs || editor->linesel))
1540 SetCursor(hLeft);
1541 return TRUE;
1543 return FALSE;
1546 static BOOL ME_ShowContextMenu(ME_TextEditor *editor, int x, int y)
1548 CHARRANGE selrange;
1549 HMENU menu;
1550 int seltype = 0;
1551 if(!editor->lpOleCallback)
1552 return FALSE;
1553 ME_GetSelection(editor, &selrange.cpMin, &selrange.cpMax);
1554 if(selrange.cpMin == selrange.cpMax)
1555 seltype |= SEL_EMPTY;
1556 else
1558 /* FIXME: Handle objects */
1559 seltype |= SEL_TEXT;
1560 if(selrange.cpMax-selrange.cpMin > 1)
1561 seltype |= SEL_MULTICHAR;
1563 if(SUCCEEDED(IRichEditOleCallback_GetContextMenu(editor->lpOleCallback, seltype, NULL, &selrange, &menu)))
1565 TrackPopupMenu(menu, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, GetParent(editor->hWnd), NULL);
1566 DestroyMenu(menu);
1568 return TRUE;
1571 ME_TextEditor *ME_MakeEditor(HWND hWnd) {
1572 ME_TextEditor *ed = ALLOC_OBJ(ME_TextEditor);
1573 int i;
1574 ed->hWnd = hWnd;
1575 ed->bEmulateVersion10 = FALSE;
1576 ed->pBuffer = ME_MakeText();
1577 ed->nZoomNumerator = ed->nZoomDenominator = 0;
1578 ME_MakeFirstParagraph(ed);
1579 ed->bCaretShown = FALSE;
1580 ed->nCursors = 4;
1581 ed->pCursors = ALLOC_N_OBJ(ME_Cursor, ed->nCursors);
1582 ed->pCursors[0].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
1583 ed->pCursors[0].nOffset = 0;
1584 ed->pCursors[1].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
1585 ed->pCursors[1].nOffset = 0;
1586 ed->pCursors[2] = ed->pCursors[0];
1587 ed->pCursors[3] = ed->pCursors[1];
1588 ed->nLastTotalLength = ed->nTotalLength = 0;
1589 ed->nHeight = 0;
1590 ed->nUDArrowX = -1;
1591 ed->nSequence = 0;
1592 ed->rgbBackColor = -1;
1593 ed->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
1594 ed->bCaretAtEnd = FALSE;
1595 ed->nEventMask = 0;
1596 ed->nModifyStep = 0;
1597 ed->nTextLimit = TEXT_LIMIT_DEFAULT;
1598 ed->pUndoStack = ed->pRedoStack = ed->pUndoStackBottom = NULL;
1599 ed->nUndoStackSize = 0;
1600 ed->nUndoLimit = STACK_SIZE_DEFAULT;
1601 ed->nUndoMode = umAddToUndo;
1602 ed->nParagraphs = 1;
1603 ed->nLastSelStart = ed->nLastSelEnd = 0;
1604 ed->pLastSelStartPara = ed->pLastSelEndPara = ME_FindItemFwd(ed->pBuffer->pFirst, diParagraph);
1605 ed->bRedraw = TRUE;
1606 ed->bWordWrap = (GetWindowLongW(hWnd, GWL_STYLE) & WS_HSCROLL) ? FALSE : TRUE;
1607 ed->bHideSelection = FALSE;
1608 ed->nInvalidOfs = -1;
1609 ed->pfnWordBreak = NULL;
1610 ed->lpOleCallback = NULL;
1611 ed->mode = TM_RICHTEXT | TM_MULTILEVELUNDO | TM_MULTICODEPAGE;
1612 ed->AutoURLDetect_bEnable = FALSE;
1613 ed->bHaveFocus = FALSE;
1614 GetClientRect(hWnd, &ed->rcFormat);
1615 for (i=0; i<HFONT_CACHE_SIZE; i++)
1617 ed->pFontCache[i].nRefs = 0;
1618 ed->pFontCache[i].nAge = 0;
1619 ed->pFontCache[i].hFont = NULL;
1622 ME_CheckCharOffsets(ed);
1623 if (GetWindowLongW(hWnd, GWL_STYLE) & ES_SELECTIONBAR)
1624 ed->selofs = 16;
1625 else
1626 ed->selofs = 0;
1627 ed->linesel = 0;
1629 if (GetWindowLongW(hWnd, GWL_STYLE) & ES_PASSWORD)
1630 ed->cPasswordMask = '*';
1631 else
1632 ed->cPasswordMask = 0;
1634 return ed;
1637 typedef struct tagME_GlobalDestStruct
1639 HGLOBAL hData;
1640 int nLength;
1641 } ME_GlobalDestStruct;
1643 static DWORD CALLBACK ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1645 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
1646 int i;
1647 WORD *pSrc, *pDest;
1649 cb = cb >> 1;
1650 pDest = (WORD *)lpBuff;
1651 pSrc = (WORD *)GlobalLock(pData->hData);
1652 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
1653 pDest[i] = pSrc[pData->nLength+i];
1655 pData->nLength += i;
1656 *pcb = 2*i;
1657 GlobalUnlock(pData->hData);
1658 return 0;
1661 static DWORD CALLBACK ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1663 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
1664 int i;
1665 BYTE *pSrc, *pDest;
1667 pDest = lpBuff;
1668 pSrc = (BYTE *)GlobalLock(pData->hData);
1669 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
1670 pDest[i] = pSrc[pData->nLength+i];
1672 pData->nLength += i;
1673 *pcb = i;
1674 GlobalUnlock(pData->hData);
1675 return 0;
1679 void ME_DestroyEditor(ME_TextEditor *editor)
1681 ME_DisplayItem *pFirst = editor->pBuffer->pFirst;
1682 ME_DisplayItem *p = pFirst, *pNext = NULL;
1683 int i;
1685 ME_ClearTempStyle(editor);
1686 ME_EmptyUndoStack(editor);
1687 while(p) {
1688 pNext = p->next;
1689 ME_DestroyDisplayItem(p);
1690 p = pNext;
1692 ME_ReleaseStyle(editor->pBuffer->pDefaultStyle);
1693 for (i=0; i<HFONT_CACHE_SIZE; i++)
1695 if (editor->pFontCache[i].hFont)
1696 DeleteObject(editor->pFontCache[i].hFont);
1698 if (editor->rgbBackColor != -1)
1699 DeleteObject(editor->hbrBackground);
1700 if(editor->lpOleCallback)
1701 IUnknown_Release(editor->lpOleCallback);
1703 FREE_OBJ(editor->pBuffer);
1704 FREE_OBJ(editor->pCursors);
1706 FREE_OBJ(editor);
1709 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
1711 TRACE("\n");
1712 switch (fdwReason)
1714 case DLL_PROCESS_ATTACH:
1715 DisableThreadLibraryCalls(hinstDLL);
1716 me_heap = HeapCreate (0, 0x10000, 0);
1717 if (!ME_RegisterEditorClass(hinstDLL)) return FALSE;
1718 hLeft = LoadCursorW(hinstDLL, MAKEINTRESOURCEW(OCR_REVERSE));
1719 LookupInit();
1720 break;
1722 case DLL_PROCESS_DETACH:
1723 UnregisterClassW(RichEdit20W, 0);
1724 UnregisterClassW(RichEdit50W, 0);
1725 UnregisterClassA("RichEdit20A", 0);
1726 UnregisterClassA("RichEdit50A", 0);
1727 if (ME_ListBoxRegistered)
1728 UnregisterClassW(REListBox20W, 0);
1729 if (ME_ComboBoxRegistered)
1730 UnregisterClassW(REComboBox20W, 0);
1731 LookupCleanup();
1732 HeapDestroy (me_heap);
1733 me_heap = NULL;
1734 break;
1736 return TRUE;
1740 #define UNSUPPORTED_MSG(e) \
1741 case e: \
1742 FIXME(#e ": stub\n"); \
1743 return DefWindowProcW(hWnd, msg, wParam, lParam);
1745 static const char * const edit_messages[] = {
1746 "EM_GETSEL",
1747 "EM_SETSEL",
1748 "EM_GETRECT",
1749 "EM_SETRECT",
1750 "EM_SETRECTNP",
1751 "EM_SCROLL",
1752 "EM_LINESCROLL",
1753 "EM_SCROLLCARET",
1754 "EM_GETMODIFY",
1755 "EM_SETMODIFY",
1756 "EM_GETLINECOUNT",
1757 "EM_LINEINDEX",
1758 "EM_SETHANDLE",
1759 "EM_GETHANDLE",
1760 "EM_GETTHUMB",
1761 "EM_UNKNOWN_BF",
1762 "EM_UNKNOWN_C0",
1763 "EM_LINELENGTH",
1764 "EM_REPLACESEL",
1765 "EM_UNKNOWN_C3",
1766 "EM_GETLINE",
1767 "EM_LIMITTEXT",
1768 "EM_CANUNDO",
1769 "EM_UNDO",
1770 "EM_FMTLINES",
1771 "EM_LINEFROMCHAR",
1772 "EM_UNKNOWN_CA",
1773 "EM_SETTABSTOPS",
1774 "EM_SETPASSWORDCHAR",
1775 "EM_EMPTYUNDOBUFFER",
1776 "EM_GETFIRSTVISIBLELINE",
1777 "EM_SETREADONLY",
1778 "EM_SETWORDBREAKPROC",
1779 "EM_GETWORDBREAKPROC",
1780 "EM_GETPASSWORDCHAR",
1781 "EM_SETMARGINS",
1782 "EM_GETMARGINS",
1783 "EM_GETLIMITTEXT",
1784 "EM_POSFROMCHAR",
1785 "EM_CHARFROMPOS"
1788 static const char * const richedit_messages[] = {
1789 "EM_CANPASTE",
1790 "EM_DISPLAYBAND",
1791 "EM_EXGETSEL",
1792 "EM_EXLIMITTEXT",
1793 "EM_EXLINEFROMCHAR",
1794 "EM_EXSETSEL",
1795 "EM_FINDTEXT",
1796 "EM_FORMATRANGE",
1797 "EM_GETCHARFORMAT",
1798 "EM_GETEVENTMASK",
1799 "EM_GETOLEINTERFACE",
1800 "EM_GETPARAFORMAT",
1801 "EM_GETSELTEXT",
1802 "EM_HIDESELECTION",
1803 "EM_PASTESPECIAL",
1804 "EM_REQUESTRESIZE",
1805 "EM_SELECTIONTYPE",
1806 "EM_SETBKGNDCOLOR",
1807 "EM_SETCHARFORMAT",
1808 "EM_SETEVENTMASK",
1809 "EM_SETOLECALLBACK",
1810 "EM_SETPARAFORMAT",
1811 "EM_SETTARGETDEVICE",
1812 "EM_STREAMIN",
1813 "EM_STREAMOUT",
1814 "EM_GETTEXTRANGE",
1815 "EM_FINDWORDBREAK",
1816 "EM_SETOPTIONS",
1817 "EM_GETOPTIONS",
1818 "EM_FINDTEXTEX",
1819 "EM_GETWORDBREAKPROCEX",
1820 "EM_SETWORDBREAKPROCEX",
1821 "EM_SETUNDOLIMIT",
1822 "EM_UNKNOWN_USER_83",
1823 "EM_REDO",
1824 "EM_CANREDO",
1825 "EM_GETUNDONAME",
1826 "EM_GETREDONAME",
1827 "EM_STOPGROUPTYPING",
1828 "EM_SETTEXTMODE",
1829 "EM_GETTEXTMODE",
1830 "EM_AUTOURLDETECT",
1831 "EM_GETAUTOURLDETECT",
1832 "EM_SETPALETTE",
1833 "EM_GETTEXTEX",
1834 "EM_GETTEXTLENGTHEX",
1835 "EM_SHOWSCROLLBAR",
1836 "EM_SETTEXTEX",
1837 "EM_UNKNOWN_USER_98",
1838 "EM_UNKNOWN_USER_99",
1839 "EM_SETPUNCTUATION",
1840 "EM_GETPUNCTUATION",
1841 "EM_SETWORDWRAPMODE",
1842 "EM_GETWORDWRAPMODE",
1843 "EM_SETIMECOLOR",
1844 "EM_GETIMECOLOR",
1845 "EM_SETIMEOPTIONS",
1846 "EM_GETIMEOPTIONS",
1847 "EM_CONVPOSITION",
1848 "EM_UNKNOWN_USER_109",
1849 "EM_UNKNOWN_USER_110",
1850 "EM_UNKNOWN_USER_111",
1851 "EM_UNKNOWN_USER_112",
1852 "EM_UNKNOWN_USER_113",
1853 "EM_UNKNOWN_USER_114",
1854 "EM_UNKNOWN_USER_115",
1855 "EM_UNKNOWN_USER_116",
1856 "EM_UNKNOWN_USER_117",
1857 "EM_UNKNOWN_USER_118",
1858 "EM_UNKNOWN_USER_119",
1859 "EM_SETLANGOPTIONS",
1860 "EM_GETLANGOPTIONS",
1861 "EM_GETIMECOMPMODE",
1862 "EM_FINDTEXTW",
1863 "EM_FINDTEXTEXW",
1864 "EM_RECONVERSION",
1865 "EM_SETIMEMODEBIAS",
1866 "EM_GETIMEMODEBIAS"
1869 static const char *
1870 get_msg_name(UINT msg)
1872 if (msg >= EM_GETSEL && msg <= EM_CHARFROMPOS)
1873 return edit_messages[msg - EM_GETSEL];
1874 if (msg >= EM_CANPASTE && msg <= EM_GETIMEMODEBIAS)
1875 return richedit_messages[msg - EM_CANPASTE];
1876 return "";
1879 static LRESULT RichEditWndProc_common(HWND hWnd, UINT msg, WPARAM wParam,
1880 LPARAM lParam, BOOL unicode)
1882 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongPtrW(hWnd, 0);
1884 TRACE("hwnd %p msg %04x (%s) %lx %lx, unicode %d\n",
1885 hWnd, msg, get_msg_name(msg), wParam, lParam, unicode);
1887 if (!editor && msg != WM_NCCREATE && msg != WM_NCDESTROY) {
1888 ERR("called with invalid hWnd %p - application bug?\n", hWnd);
1889 return 0;
1892 switch(msg) {
1894 UNSUPPORTED_MSG(EM_DISPLAYBAND)
1895 UNSUPPORTED_MSG(EM_FINDWORDBREAK)
1896 UNSUPPORTED_MSG(EM_FMTLINES)
1897 UNSUPPORTED_MSG(EM_FORMATRANGE)
1898 UNSUPPORTED_MSG(EM_GETBIDIOPTIONS)
1899 UNSUPPORTED_MSG(EM_GETEDITSTYLE)
1900 UNSUPPORTED_MSG(EM_GETIMECOMPMODE)
1901 /* UNSUPPORTED_MSG(EM_GETIMESTATUS) missing in Wine headers */
1902 UNSUPPORTED_MSG(EM_GETLANGOPTIONS)
1903 /* UNSUPPORTED_MSG(EM_GETOLEINTERFACE) separate stub */
1904 UNSUPPORTED_MSG(EM_GETREDONAME)
1905 UNSUPPORTED_MSG(EM_GETTEXTMODE)
1906 UNSUPPORTED_MSG(EM_GETTYPOGRAPHYOPTIONS)
1907 UNSUPPORTED_MSG(EM_GETUNDONAME)
1908 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX)
1909 UNSUPPORTED_MSG(EM_PASTESPECIAL)
1910 UNSUPPORTED_MSG(EM_SELECTIONTYPE)
1911 UNSUPPORTED_MSG(EM_SETBIDIOPTIONS)
1912 UNSUPPORTED_MSG(EM_SETEDITSTYLE)
1913 UNSUPPORTED_MSG(EM_SETFONTSIZE)
1914 UNSUPPORTED_MSG(EM_SETLANGOPTIONS)
1915 UNSUPPORTED_MSG(EM_SETPALETTE)
1916 UNSUPPORTED_MSG(EM_SETTABSTOPS)
1917 UNSUPPORTED_MSG(EM_SETTYPOGRAPHYOPTIONS)
1918 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX)
1919 UNSUPPORTED_MSG(WM_STYLECHANGING)
1920 UNSUPPORTED_MSG(WM_STYLECHANGED)
1921 /* UNSUPPORTED_MSG(WM_UNICHAR) FIXME missing in Wine headers */
1923 /* Messages specific to Richedit controls */
1925 case EM_STREAMIN:
1926 return ME_StreamIn(editor, wParam, (EDITSTREAM*)lParam);
1927 case EM_STREAMOUT:
1928 return ME_StreamOut(editor, wParam, (EDITSTREAM *)lParam);
1929 case WM_GETDLGCODE:
1931 UINT code = DLGC_WANTCHARS|DLGC_WANTARROWS;
1932 if(lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
1934 int vk = (int)((LPMSG)lParam)->wParam;
1935 /* if style says we want return key */
1936 if((vk == VK_RETURN) && (GetWindowLongW(hWnd, GWL_STYLE) & ES_WANTRETURN))
1938 code |= DLGC_WANTMESSAGE;
1940 /* we always handle ctrl-tab */
1941 if((vk == VK_TAB) && (GetKeyState(VK_CONTROL) & 0x8000))
1943 code |= DLGC_WANTMESSAGE;
1946 return code;
1948 case WM_NCCREATE:
1950 CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
1951 TRACE("WM_NCCREATE: style 0x%08x\n", pcs->style);
1952 editor = ME_MakeEditor(hWnd);
1953 SetWindowLongPtrW(hWnd, 0, (LONG_PTR)editor);
1954 return TRUE;
1956 case EM_EMPTYUNDOBUFFER:
1957 ME_EmptyUndoStack(editor);
1958 return 0;
1959 case EM_GETSEL:
1961 /* Note: wParam/lParam can be NULL */
1962 UINT from, to;
1963 PUINT pfrom = wParam ? (PUINT)wParam : &from;
1964 PUINT pto = lParam ? (PUINT)lParam : &to;
1965 ME_GetSelection(editor, (int *)pfrom, (int *)pto);
1966 if ((*pfrom|*pto) & 0xFFFF0000)
1967 return -1;
1968 return MAKELONG(*pfrom,*pto);
1970 case EM_EXGETSEL:
1972 CHARRANGE *pRange = (CHARRANGE *)lParam;
1973 ME_GetSelection(editor, &pRange->cpMin, &pRange->cpMax);
1974 TRACE("EM_EXGETSEL = (%d,%d)\n", pRange->cpMin, pRange->cpMax);
1975 return 0;
1977 case EM_SETUNDOLIMIT:
1979 if ((int)wParam < 0)
1980 editor->nUndoLimit = STACK_SIZE_DEFAULT;
1981 else
1982 editor->nUndoLimit = min(wParam, STACK_SIZE_MAX);
1983 /* Setting a max stack size keeps wine from getting killed
1984 for hogging memory. Windows allocates all this memory at once, so
1985 no program would realistically set a value above our maximum. */
1986 return editor->nUndoLimit;
1988 case EM_CANUNDO:
1989 return editor->pUndoStack != NULL;
1990 case EM_CANREDO:
1991 return editor->pRedoStack != NULL;
1992 case WM_UNDO: /* FIXME: actually not the same */
1993 case EM_UNDO:
1994 ME_Undo(editor);
1995 return 0;
1996 case EM_REDO:
1997 ME_Redo(editor);
1998 return 0;
1999 case EM_GETOPTIONS:
2001 /* these flags are equivalent to the ES_* counterparts */
2002 DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
2003 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN;
2004 DWORD settings = GetWindowLongW(hWnd, GWL_STYLE) & mask;
2006 return settings;
2008 case EM_SETOPTIONS:
2010 /* these flags are equivalent to ES_* counterparts
2011 * ECO_READONLY is already implemented in the code, only requires
2012 * setting the bit to work
2014 DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
2015 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN | ECO_SELECTIONBAR;
2016 DWORD raw = GetWindowLongW(hWnd, GWL_STYLE);
2017 DWORD settings = mask & raw;
2019 switch(wParam)
2021 case ECOOP_SET:
2022 settings = lParam;
2023 break;
2024 case ECOOP_OR:
2025 settings |= lParam;
2026 break;
2027 case ECOOP_AND:
2028 settings &= lParam;
2029 break;
2030 case ECOOP_XOR:
2031 settings ^= lParam;
2033 SetWindowLongW(hWnd, GWL_STYLE, (raw & ~mask) | (settings & mask));
2035 if (settings & ECO_AUTOWORDSELECTION)
2036 FIXME("ECO_AUTOWORDSELECTION not implemented yet!\n");
2037 if (settings & ECO_SELECTIONBAR)
2038 editor->selofs = 16;
2039 else
2040 editor->selofs = 0;
2041 ME_WrapMarkedParagraphs(editor);
2043 if (settings & ECO_VERTICAL)
2044 FIXME("ECO_VERTICAL not implemented yet!\n");
2045 if (settings & ECO_AUTOHSCROLL)
2046 FIXME("ECO_AUTOHSCROLL not implemented yet!\n");
2047 if (settings & ECO_AUTOVSCROLL)
2048 FIXME("ECO_AUTOVSCROLL not implemented yet!\n");
2049 if (settings & ECO_NOHIDESEL)
2050 FIXME("ECO_NOHIDESEL not implemented yet!\n");
2051 if (settings & ECO_WANTRETURN)
2052 FIXME("ECO_WANTRETURN not implemented yet!\n");
2054 return settings;
2056 case EM_SETSEL:
2058 ME_InvalidateSelection(editor);
2059 ME_SetSelection(editor, wParam, lParam);
2060 ME_InvalidateSelection(editor);
2061 ME_SendSelChange(editor);
2062 return 0;
2064 case EM_SETSCROLLPOS:
2066 POINT *point = (POINT *)lParam;
2067 ME_ScrollAbs(editor, point->y);
2068 return 0;
2070 case EM_AUTOURLDETECT:
2072 if (wParam==1 || wParam ==0)
2074 editor->AutoURLDetect_bEnable = (BOOL)wParam;
2075 return 0;
2077 return E_INVALIDARG;
2079 case EM_GETAUTOURLDETECT:
2081 return editor->AutoURLDetect_bEnable;
2083 case EM_EXSETSEL:
2085 int end;
2086 CHARRANGE range = *(CHARRANGE *)lParam;
2088 TRACE("EM_EXSETSEL (%d,%d)\n", range.cpMin, range.cpMax);
2090 ME_InvalidateSelection(editor);
2091 end = ME_SetSelection(editor, range.cpMin, range.cpMax);
2092 ME_InvalidateSelection(editor);
2093 ME_SendSelChange(editor);
2095 return end;
2097 case EM_SHOWSCROLLBAR:
2099 ShowScrollBar(editor->hWnd, wParam, lParam);
2100 return 0;
2102 case EM_SETTEXTEX:
2104 LPWSTR wszText;
2105 SETTEXTEX *pStruct = (SETTEXTEX *)wParam;
2106 size_t len;
2107 int from, to;
2108 ME_Style *style;
2109 int oldModify = editor->nModifyStep;
2111 if (!pStruct) return 0;
2113 TRACE("EM_SETTEXTEX - %s, flags %d, cp %d\n",
2114 pStruct->codepage == 1200 ? debugstr_w((LPCWSTR)lParam) : debugstr_a((LPCSTR)lParam),
2115 pStruct->flags, pStruct->codepage);
2117 /* FIXME: make use of pStruct->codepage in the to unicode translation */
2118 wszText = lParam ? ME_ToUnicode(pStruct->codepage == 1200, (void *)lParam) : NULL;
2119 len = wszText ? lstrlenW(wszText) : 0;
2121 if (pStruct->flags & ST_SELECTION) {
2122 ME_GetSelection(editor, &from, &to);
2123 style = ME_GetSelectionInsertStyle(editor);
2124 ME_InternalDeleteText(editor, from, to - from);
2125 if (pStruct->codepage != 1200 && lParam && !strncmp((char *)lParam, "{\\rtf", 5))
2126 ME_StreamInRTFString(editor, 0, (char *)lParam);
2127 else ME_InsertTextFromCursor(editor, 0, wszText, len, style);
2128 ME_ReleaseStyle(style);
2130 else {
2131 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
2132 if (pStruct->codepage != 1200 && lParam && !strncmp((char *)lParam, "{\\rtf", 5))
2133 ME_StreamInRTFString(editor, 0, (char *)lParam);
2134 else ME_InsertTextFromCursor(editor, 0, wszText, len, editor->pBuffer->pDefaultStyle);
2135 len = 1;
2137 ME_CommitUndo(editor);
2138 if (!(pStruct->flags & ST_KEEPUNDO))
2140 editor->nModifyStep = oldModify;
2141 ME_EmptyUndoStack(editor);
2143 ME_UpdateRepaint(editor);
2144 return len;
2146 case EM_SETBKGNDCOLOR:
2148 LRESULT lColor;
2149 if (editor->rgbBackColor != -1) {
2150 DeleteObject(editor->hbrBackground);
2151 lColor = editor->rgbBackColor;
2153 else lColor = GetSysColor(COLOR_WINDOW);
2155 if (wParam)
2157 editor->rgbBackColor = -1;
2158 editor->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
2160 else
2162 editor->rgbBackColor = lParam;
2163 editor->hbrBackground = CreateSolidBrush(editor->rgbBackColor);
2165 if (editor->bRedraw)
2167 InvalidateRect(hWnd, NULL, TRUE);
2168 UpdateWindow(hWnd);
2170 return lColor;
2172 case EM_GETMODIFY:
2173 return editor->nModifyStep == 0 ? 0 : 1;
2174 case EM_SETMODIFY:
2176 if (wParam)
2177 editor->nModifyStep = 1;
2178 else
2179 editor->nModifyStep = 0;
2181 return 0;
2183 case EM_SETREADONLY:
2185 long nStyle = GetWindowLongW(hWnd, GWL_STYLE);
2186 if (wParam)
2187 nStyle |= ES_READONLY;
2188 else
2189 nStyle &= ~ES_READONLY;
2190 SetWindowLongW(hWnd, GWL_STYLE, nStyle);
2191 return 0;
2193 case EM_SETEVENTMASK:
2195 DWORD nOldMask = editor->nEventMask;
2197 editor->nEventMask = lParam;
2198 return nOldMask;
2200 case EM_GETEVENTMASK:
2201 return editor->nEventMask;
2202 case EM_SETCHARFORMAT:
2204 CHARFORMAT2W buf, *p;
2205 BOOL bRepaint = TRUE;
2206 p = ME_ToCF2W(&buf, (CHARFORMAT2W *)lParam);
2207 if (p == NULL) return 0;
2208 if (!wParam)
2209 ME_SetDefaultCharFormat(editor, p);
2210 else if (wParam == (SCF_WORD | SCF_SELECTION)) {
2211 FIXME("EM_SETCHARFORMAT: word selection not supported\n");
2212 return 0;
2213 } else if (wParam == SCF_ALL) {
2214 if (editor->mode & TM_PLAINTEXT)
2215 ME_SetDefaultCharFormat(editor, p);
2216 else
2217 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), p);
2218 } else if (editor->mode & TM_PLAINTEXT) {
2219 return 0;
2220 } else {
2221 int from, to;
2222 ME_GetSelection(editor, &from, &to);
2223 bRepaint = (from != to);
2224 ME_SetSelectionCharFormat(editor, p);
2226 editor->nModifyStep = 1;
2227 ME_CommitUndo(editor);
2228 if (bRepaint)
2229 ME_RewrapRepaint(editor);
2230 return 1;
2232 case EM_GETCHARFORMAT:
2234 CHARFORMAT2W tmp, *dst = (CHARFORMAT2W *)lParam;
2235 if (dst->cbSize != sizeof(CHARFORMATA) &&
2236 dst->cbSize != sizeof(CHARFORMATW) &&
2237 dst->cbSize != sizeof(CHARFORMAT2A) &&
2238 dst->cbSize != sizeof(CHARFORMAT2W))
2239 return 0;
2240 tmp.cbSize = sizeof(tmp);
2241 if (!wParam)
2242 ME_GetDefaultCharFormat(editor, &tmp);
2243 else
2244 ME_GetSelectionCharFormat(editor, &tmp);
2245 ME_CopyToCFAny(dst, &tmp);
2246 return tmp.dwMask;
2248 case EM_SETPARAFORMAT:
2249 ME_SetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
2250 ME_RewrapRepaint(editor);
2251 ME_CommitUndo(editor);
2252 return 0;
2253 case EM_GETPARAFORMAT:
2254 ME_GetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
2255 return 0;
2256 case EM_GETFIRSTVISIBLELINE:
2258 ME_DisplayItem *p = editor->pBuffer->pFirst;
2259 int y = ME_GetYScrollPos(editor);
2260 int ypara = 0;
2261 int count = 0;
2262 int ystart, yend;
2263 while(p) {
2264 p = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
2265 if (p->type == diTextEnd)
2266 break;
2267 if (p->type == diParagraph) {
2268 ypara = p->member.para.nYPos;
2269 continue;
2271 ystart = ypara + p->member.row.nYPos;
2272 yend = ystart + p->member.row.nHeight;
2273 if (y < yend) {
2274 break;
2276 count++;
2278 return count;
2280 case EM_HIDESELECTION:
2282 editor->bHideSelection = (wParam != 0);
2283 ME_InvalidateSelection(editor);
2284 return 0;
2286 case EM_LINESCROLL:
2288 ME_ScrollDown(editor, lParam * 8); /* FIXME follow the original */
2289 return TRUE; /* Should return false if a single line richedit control */
2291 case WM_CLEAR:
2293 int from, to;
2294 ME_GetSelection(editor, &from, &to);
2295 ME_InternalDeleteText(editor, from, to-from);
2296 ME_CommitUndo(editor);
2297 ME_UpdateRepaint(editor);
2298 return 0;
2300 case EM_REPLACESEL:
2302 int from, to;
2303 ME_Style *style;
2304 LPWSTR wszText = lParam ? ME_ToUnicode(unicode, (void *)lParam) : NULL;
2305 size_t len = wszText ? lstrlenW(wszText) : 0;
2306 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText));
2308 ME_GetSelection(editor, &from, &to);
2309 style = ME_GetSelectionInsertStyle(editor);
2310 ME_InternalDeleteText(editor, from, to-from);
2311 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
2312 ME_ReleaseStyle(style);
2313 /* drop temporary style if line end */
2315 * FIXME question: does abc\n mean: put abc,
2316 * clear temp style, put \n? (would require a change)
2318 if (len>0 && wszText[len-1] == '\n')
2319 ME_ClearTempStyle(editor);
2320 ME_EndToUnicode(unicode, wszText);
2321 ME_CommitUndo(editor);
2322 if (!wParam)
2323 ME_EmptyUndoStack(editor);
2324 ME_UpdateRepaint(editor);
2325 return len;
2327 case EM_SCROLLCARET:
2329 int top, bottom; /* row's edges relative to document top */
2330 int nPos;
2331 ME_DisplayItem *para, *row;
2333 nPos = ME_GetYScrollPos(editor);
2334 row = ME_RowStart(editor->pCursors[0].pRun);
2335 para = ME_GetParagraph(row);
2336 top = para->member.para.nYPos + row->member.row.nYPos;
2337 bottom = top + row->member.row.nHeight;
2339 if (top < nPos) /* caret above window */
2340 ME_ScrollAbs(editor, top);
2341 else if (nPos + editor->sizeWindow.cy < bottom) /*below*/
2342 ME_ScrollAbs(editor, bottom - editor->sizeWindow.cy);
2343 return 0;
2345 case WM_SETFONT:
2347 LOGFONTW lf;
2348 CHARFORMAT2W fmt;
2349 HDC hDC;
2350 BOOL bRepaint = LOWORD(lParam);
2352 if (!wParam)
2353 wParam = (WPARAM)GetStockObject(SYSTEM_FONT);
2354 GetObjectW((HGDIOBJ)wParam, sizeof(LOGFONTW), &lf);
2355 hDC = GetDC(hWnd);
2356 ME_CharFormatFromLogFont(hDC, &lf, &fmt);
2357 ReleaseDC(hWnd, hDC);
2358 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), &fmt);
2359 ME_SetDefaultCharFormat(editor, &fmt);
2361 ME_CommitUndo(editor);
2362 if (bRepaint)
2363 ME_RewrapRepaint(editor);
2364 return 0;
2366 case WM_SETTEXT:
2368 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
2369 if (lParam)
2371 TRACE("WM_SETTEXT lParam==%lx\n",lParam);
2372 if (!unicode && !strncmp((char *)lParam, "{\\rtf", 5))
2374 /* Undocumented: WM_SETTEXT supports RTF text */
2375 ME_StreamInRTFString(editor, 0, (char *)lParam);
2377 else
2379 LPWSTR wszText = ME_ToUnicode(unicode, (void *)lParam);
2380 TRACE("WM_SETTEXT - %s\n", debugstr_w(wszText)); /* debugstr_w() */
2381 if (lstrlenW(wszText) > 0)
2383 int len = -1;
2385 /* uses default style! */
2386 if (!(GetWindowLongW(hWnd, GWL_STYLE) & ES_MULTILINE))
2388 WCHAR * p;
2390 p = wszText;
2391 while (*p != '\0' && *p != '\r' && *p != '\n') p++;
2392 len = p - wszText;
2394 ME_InsertTextFromCursor(editor, 0, wszText, len, editor->pBuffer->pDefaultStyle);
2396 ME_EndToUnicode(unicode, wszText);
2399 else
2400 TRACE("WM_SETTEXT - NULL\n");
2401 ME_CommitUndo(editor);
2402 ME_EmptyUndoStack(editor);
2403 ME_SetSelection(editor, 0, 0);
2404 editor->nModifyStep = 0;
2405 ME_UpdateRepaint(editor);
2406 return 1;
2408 case EM_CANPASTE:
2410 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
2411 if (IsClipboardFormatAvailable(nRTFFormat))
2412 return TRUE;
2413 if (IsClipboardFormatAvailable(CF_UNICODETEXT))
2414 return TRUE;
2415 return FALSE;
2417 case WM_PASTE:
2419 DWORD dwFormat = 0;
2420 EDITSTREAM es;
2421 ME_GlobalDestStruct gds;
2422 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
2423 UINT cf = 0;
2425 if (IsClipboardFormatAvailable(nRTFFormat))
2426 cf = nRTFFormat, dwFormat = SF_RTF;
2427 else if (IsClipboardFormatAvailable(CF_UNICODETEXT))
2428 cf = CF_UNICODETEXT, dwFormat = SF_TEXT|SF_UNICODE;
2429 else
2430 return 0;
2432 if (!OpenClipboard(hWnd))
2433 return 0;
2434 gds.hData = GetClipboardData(cf);
2435 gds.nLength = 0;
2436 es.dwCookie = (DWORD)&gds;
2437 es.pfnCallback = dwFormat == SF_RTF ? ME_ReadFromHGLOBALRTF : ME_ReadFromHGLOBALUnicode;
2438 ME_StreamIn(editor, dwFormat|SFF_SELECTION, &es);
2440 CloseClipboard();
2441 return 0;
2443 case WM_CUT:
2444 case WM_COPY:
2446 LPDATAOBJECT dataObj = NULL;
2447 CHARRANGE range;
2448 HRESULT hr = S_OK;
2450 if (editor->cPasswordMask)
2451 return 0; /* Copying or Cutting masked text isn't allowed */
2453 ME_GetSelection(editor, &range.cpMin, &range.cpMax);
2454 if(editor->lpOleCallback)
2455 hr = IRichEditOleCallback_GetClipboardData(editor->lpOleCallback, &range, RECO_COPY, &dataObj);
2456 if(FAILED(hr) || !dataObj)
2457 hr = ME_GetDataObject(editor, &range, &dataObj);
2458 if(SUCCEEDED(hr)) {
2459 hr = OleSetClipboard(dataObj);
2460 IDataObject_Release(dataObj);
2462 if (SUCCEEDED(hr) && msg == WM_CUT)
2464 ME_InternalDeleteText(editor, range.cpMin, range.cpMax-range.cpMin);
2465 ME_CommitUndo(editor);
2466 ME_UpdateRepaint(editor);
2468 return 0;
2470 case WM_GETTEXTLENGTH:
2472 GETTEXTLENGTHEX how;
2474 how.flags = GTL_CLOSE | (editor->bEmulateVersion10 ? 0 : GTL_USECRLF) | GTL_NUMCHARS;
2475 how.codepage = unicode ? 1200 : CP_ACP;
2476 return ME_GetTextLengthEx(editor, &how);
2478 case EM_GETTEXTLENGTHEX:
2479 return ME_GetTextLengthEx(editor, (GETTEXTLENGTHEX *)wParam);
2480 case WM_GETTEXT:
2482 GETTEXTEX ex;
2483 LRESULT rc;
2484 LPSTR bufferA = NULL;
2485 LPWSTR bufferW = NULL;
2487 if (unicode)
2488 bufferW = heap_alloc((wParam + 2) * sizeof(WCHAR));
2489 else
2490 bufferA = heap_alloc(wParam + 2);
2492 ex.cb = (wParam + 2) * (unicode ? sizeof(WCHAR) : sizeof(CHAR));
2493 ex.flags = GT_USECRLF;
2494 ex.codepage = unicode ? 1200 : CP_ACP;
2495 ex.lpDefaultChar = NULL;
2496 ex.lpUsedDefChar = NULL;
2497 rc = RichEditWndProc_common(hWnd, EM_GETTEXTEX, (WPARAM)&ex, unicode ? (LPARAM)bufferW : (LPARAM)bufferA, unicode);
2499 if (unicode)
2501 memcpy((LPWSTR)lParam, bufferW, wParam * sizeof(WCHAR));
2502 if (lstrlenW(bufferW) >= wParam) rc = 0;
2504 else
2506 memcpy((LPSTR)lParam, bufferA, wParam);
2507 if (strlen(bufferA) >= wParam) rc = 0;
2509 heap_free(bufferA);
2510 heap_free(bufferW);
2511 return rc;
2513 case EM_GETTEXTEX:
2515 GETTEXTEX *ex = (GETTEXTEX*)wParam;
2516 int nStart, nCount; /* in chars */
2518 if (ex->flags & ~(GT_SELECTION | GT_USECRLF))
2519 FIXME("GETTEXTEX flags 0x%08x not supported\n", ex->flags & ~(GT_SELECTION | GT_USECRLF));
2521 if (ex->flags & GT_SELECTION)
2523 ME_GetSelection(editor, &nStart, &nCount);
2524 nCount -= nStart;
2526 else
2528 nStart = 0;
2529 nCount = 0x7fffffff;
2531 if (ex->codepage == 1200)
2533 nCount = min(nCount, ex->cb / sizeof(WCHAR) - 1);
2534 return ME_GetTextW(editor, (LPWSTR)lParam, nStart, nCount, ex->flags & GT_USECRLF);
2536 else
2538 /* potentially each char may be a CR, why calculate the exact value with O(N) when
2539 we can just take a bigger buffer? :) */
2540 int crlfmul = (ex->flags & GT_USECRLF) ? 2 : 1;
2541 LPWSTR buffer;
2542 DWORD buflen = ex->cb;
2543 LRESULT rc;
2544 DWORD flags = 0;
2546 nCount = min(nCount, ex->cb - 1);
2547 buffer = heap_alloc((crlfmul*nCount + 1) * sizeof(WCHAR));
2549 buflen = ME_GetTextW(editor, buffer, nStart, nCount, ex->flags & GT_USECRLF);
2550 rc = WideCharToMultiByte(ex->codepage, flags, buffer, -1, (LPSTR)lParam, ex->cb, ex->lpDefaultChar, ex->lpUsedDefChar);
2551 if (rc) rc--; /* do not count 0 terminator */
2553 heap_free(buffer);
2554 return rc;
2557 case EM_GETSELTEXT:
2559 int from, to;
2560 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
2561 ME_GetSelection(editor, &from, &to);
2562 tr.chrg.cpMin = from;
2563 tr.chrg.cpMax = to;
2564 tr.lpstrText = (WCHAR *)lParam;
2565 return RichEditWndProc_common(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr, unicode);
2567 case EM_GETSCROLLPOS:
2569 POINT *point = (POINT *)lParam;
2570 point->x = 0; /* FIXME side scrolling not implemented */
2571 point->y = ME_GetYScrollPos(editor);
2572 return 1;
2574 case EM_GETTEXTRANGE:
2576 TEXTRANGEW *rng = (TEXTRANGEW *)lParam;
2577 TRACE("EM_GETTEXTRANGE min=%d max=%d unicode=%d emul1.0=%d length=%d\n",
2578 rng->chrg.cpMin, rng->chrg.cpMax, unicode,
2579 editor->bEmulateVersion10, ME_GetTextLength(editor));
2580 if (unicode)
2581 return ME_GetTextW(editor, rng->lpstrText, rng->chrg.cpMin, rng->chrg.cpMax-rng->chrg.cpMin, editor->bEmulateVersion10);
2582 else
2584 int nLen = rng->chrg.cpMax-rng->chrg.cpMin;
2585 WCHAR *p = ALLOC_N_OBJ(WCHAR, nLen+1);
2586 int nChars = ME_GetTextW(editor, p, rng->chrg.cpMin, nLen, editor->bEmulateVersion10);
2587 /* FIXME this is a potential security hole (buffer overrun)
2588 if you know more about wchar->mbyte conversion please explain
2590 WideCharToMultiByte(CP_ACP, 0, p, nChars+1, (char *)rng->lpstrText, nLen+1, NULL, NULL);
2591 FREE_OBJ(p);
2592 return nChars;
2595 case EM_GETLINE:
2597 ME_DisplayItem *run;
2598 const unsigned int nMaxChars = *(WORD *) lParam;
2599 unsigned int nEndChars, nCharsLeft = nMaxChars;
2600 char *dest = (char *) lParam;
2601 /* rich text editor 1.0 uses \r\n for line end, 2.0 uses just \r;
2602 we need to know how if we have the extra \n or not */
2603 int nLF = editor->bEmulateVersion10;
2605 TRACE("EM_GETLINE: row=%d, nMaxChars=%d (%s)\n", (int) wParam, nMaxChars,
2606 unicode ? "Unicode" : "Ansi");
2608 run = ME_FindRowWithNumber(editor, wParam);
2609 if (run == NULL)
2610 return 0;
2612 while (nCharsLeft && (run = ME_FindItemFwd(run, diRunOrStartRow))
2613 && !(run->member.run.nFlags & MERF_ENDPARA))
2615 unsigned int nCopy;
2616 ME_String *strText;
2617 if (run->type != diRun)
2618 break;
2619 strText = run->member.run.strText;
2620 nCopy = min(nCharsLeft, strText->nLen);
2622 if (unicode)
2623 lstrcpynW((LPWSTR) dest, strText->szData, nCopy);
2624 else
2625 nCopy = WideCharToMultiByte(CP_ACP, 0, strText->szData, nCopy, dest,
2626 nCharsLeft, NULL, NULL);
2627 dest += nCopy * (unicode ? sizeof(WCHAR) : 1);
2628 nCharsLeft -= nCopy;
2631 /* append \r\0 (or \r\n\0 in 1.0), space allowing */
2632 nEndChars = min(nCharsLeft, 2 + nLF);
2633 nCharsLeft -= nEndChars;
2634 if (unicode)
2636 const WCHAR src[] = {'\r', '\0'};
2637 const WCHAR src10[] = {'\r', '\n', '\0'};
2638 lstrcpynW((LPWSTR) dest, nLF ? src10 : src, nEndChars);
2640 else
2641 lstrcpynA(dest, nLF ? "\r\n" : "\r", nEndChars);
2643 TRACE("EM_GETLINE: got %u bytes\n", nMaxChars - nCharsLeft);
2645 if (nEndChars == 2 + nLF)
2646 return nMaxChars - nCharsLeft - 1; /* don't count \0 */
2647 else
2648 return nMaxChars - nCharsLeft;
2650 case EM_GETLINECOUNT:
2652 ME_DisplayItem *item = editor->pBuffer->pFirst->next;
2653 int nRows = 0;
2655 while (item != editor->pBuffer->pLast)
2657 assert(item->type == diParagraph);
2658 nRows += item->member.para.nRows;
2659 item = item->member.para.next_para;
2661 TRACE("EM_GETLINECOUNT: nRows==%d\n", nRows);
2662 return max(1, nRows);
2664 case EM_LINEFROMCHAR:
2666 if (wParam == -1)
2667 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(editor, 1));
2668 else
2669 return ME_RowNumberFromCharOfs(editor, wParam);
2671 case EM_EXLINEFROMCHAR:
2673 if (lParam == -1)
2674 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(editor,1));
2675 else
2676 return ME_RowNumberFromCharOfs(editor, lParam);
2678 case EM_LINEINDEX:
2680 ME_DisplayItem *item, *para;
2681 int nCharOfs;
2683 if (wParam == -1)
2684 item = ME_FindItemBack(editor->pCursors[0].pRun, diStartRow);
2685 else
2686 item = ME_FindRowWithNumber(editor, wParam);
2687 if (!item)
2688 return -1;
2689 para = ME_GetParagraph(item);
2690 item = ME_FindItemFwd(item, diRun);
2691 nCharOfs = para->member.para.nCharOfs + item->member.run.nCharOfs;
2692 TRACE("EM_LINEINDEX: nCharOfs==%d\n", nCharOfs);
2693 return nCharOfs;
2695 case EM_LINELENGTH:
2697 ME_DisplayItem *item, *item_end;
2698 int nChars = 0, nThisLineOfs = 0, nNextLineOfs = 0;
2700 if (wParam > ME_GetTextLength(editor))
2701 return 0;
2702 if (wParam == -1)
2704 FIXME("EM_LINELENGTH: returning number of unselected characters on lines with selection unsupported.\n");
2705 return 0;
2707 item = ME_FindItemAtOffset(editor, diRun, wParam, NULL);
2708 item = ME_RowStart(item);
2709 nThisLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item, diRun), 0);
2710 item_end = ME_FindItemFwd(item, diStartRowOrParagraphOrEnd);
2711 if (item_end->type == diStartRow)
2712 nNextLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item_end, diRun), 0);
2713 else
2714 nNextLineOfs = ME_FindItemFwd(item, diParagraphOrEnd)->member.para.nCharOfs
2715 - (editor->bEmulateVersion10?2:1);
2716 nChars = nNextLineOfs - nThisLineOfs;
2717 TRACE("EM_LINELENGTH(%ld)==%d\n",wParam, nChars);
2718 return nChars;
2720 case EM_EXLIMITTEXT:
2722 if ((int)lParam < 0)
2723 return 0;
2724 if (lParam == 0)
2725 editor->nTextLimit = 65536;
2726 else
2727 editor->nTextLimit = (int) lParam;
2728 return 0;
2730 case EM_LIMITTEXT:
2732 if (wParam == 0)
2733 editor->nTextLimit = 65536;
2734 else
2735 editor->nTextLimit = (int) wParam;
2736 return 0;
2738 case EM_GETLIMITTEXT:
2740 return editor->nTextLimit;
2742 case EM_FINDTEXT:
2744 FINDTEXTA *ft = (FINDTEXTA *)lParam;
2745 int nChars = MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, NULL, 0);
2746 WCHAR *tmp;
2747 LRESULT r;
2749 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
2750 MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, tmp, nChars);
2751 r = ME_FindText(editor, wParam, &ft->chrg, tmp, NULL);
2752 FREE_OBJ( tmp );
2753 return r;
2755 case EM_FINDTEXTEX:
2757 FINDTEXTEXA *ex = (FINDTEXTEXA *)lParam;
2758 int nChars = MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, NULL, 0);
2759 WCHAR *tmp;
2760 LRESULT r;
2762 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
2763 MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, tmp, nChars);
2764 r = ME_FindText(editor, wParam, &ex->chrg, tmp, &ex->chrgText);
2765 FREE_OBJ( tmp );
2766 return r;
2768 case EM_FINDTEXTW:
2770 FINDTEXTW *ft = (FINDTEXTW *)lParam;
2771 return ME_FindText(editor, wParam, &ft->chrg, ft->lpstrText, NULL);
2773 case EM_FINDTEXTEXW:
2775 FINDTEXTEXW *ex = (FINDTEXTEXW *)lParam;
2776 return ME_FindText(editor, wParam, &ex->chrg, ex->lpstrText, &ex->chrgText);
2778 case EM_GETZOOM:
2779 if (!wParam || !lParam)
2780 return FALSE;
2781 *(int *)wParam = editor->nZoomNumerator;
2782 *(int *)lParam = editor->nZoomDenominator;
2783 return TRUE;
2784 case EM_SETZOOM:
2785 return ME_SetZoom(editor, wParam, lParam);
2786 case EM_CHARFROMPOS:
2787 return ME_CharFromPos(editor, ((POINTL *)lParam)->x, ((POINTL *)lParam)->y);
2788 case EM_POSFROMCHAR:
2790 ME_DisplayItem *pRun;
2791 int nCharOfs, nOffset, nLength;
2792 POINTL pt = {0,0};
2794 nCharOfs = wParam;
2795 /* detect which API version we're dealing with */
2796 if (wParam >= 0x40000)
2797 nCharOfs = lParam;
2798 nLength = ME_GetTextLength(editor);
2800 if (nCharOfs < nLength) {
2801 ME_RunOfsFromCharOfs(editor, nCharOfs, &pRun, &nOffset);
2802 assert(pRun->type == diRun);
2803 pt.y = pRun->member.run.pt.y;
2804 pt.x = pRun->member.run.pt.x + ME_PointFromChar(editor, &pRun->member.run, nOffset);
2805 pt.y += ME_GetParagraph(pRun)->member.para.nYPos;
2806 } else {
2807 pt.x = 0;
2808 pt.y = editor->pBuffer->pLast->member.para.nYPos;
2810 pt.x += editor->selofs;
2811 if (wParam >= 0x40000) {
2812 *(POINTL *)wParam = pt;
2814 return MAKELONG( pt.x, pt.y );
2816 case WM_CREATE:
2817 if (GetWindowLongW(hWnd, GWL_STYLE) & WS_HSCROLL)
2818 { /* Squelch the default horizontal scrollbar it would make */
2819 ShowScrollBar(editor->hWnd, SB_HORZ, FALSE);
2821 ME_CommitUndo(editor);
2822 ME_WrapMarkedParagraphs(editor);
2823 ME_MoveCaret(editor);
2824 return 0;
2825 case WM_DESTROY:
2826 ME_DestroyEditor(editor);
2827 SetWindowLongPtrW(hWnd, 0, 0);
2828 return 0;
2829 case WM_LBUTTONDOWN:
2830 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
2831 !ME_FilterEvent(editor, msg, &wParam, &lParam))
2832 return 0;
2833 SetFocus(hWnd);
2834 ME_LButtonDown(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
2835 SetCapture(hWnd);
2836 ME_LinkNotify(editor,msg,wParam,lParam);
2837 if (!ME_SetCursor(editor, LOWORD(lParam))) goto do_default;
2838 break;
2839 case WM_MOUSEMOVE:
2840 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
2841 !ME_FilterEvent(editor, msg, &wParam, &lParam))
2842 return 0;
2843 if (GetCapture() == hWnd)
2844 ME_MouseMove(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
2845 ME_LinkNotify(editor,msg,wParam,lParam);
2846 if (!ME_SetCursor(editor, LOWORD(lParam))) goto do_default;
2847 break;
2848 case WM_LBUTTONUP:
2849 if (GetCapture() == hWnd)
2850 ReleaseCapture();
2851 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
2852 !ME_FilterEvent(editor, msg, &wParam, &lParam))
2853 return 0;
2854 else
2856 BOOL ret;
2857 editor->linesel = 0;
2858 ret = ME_SetCursor(editor, LOWORD(lParam));
2859 ME_LinkNotify(editor,msg,wParam,lParam);
2860 if (!ret) goto do_default;
2862 break;
2863 case WM_LBUTTONDBLCLK:
2864 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
2865 !ME_FilterEvent(editor, msg, &wParam, &lParam))
2866 return 0;
2867 ME_LinkNotify(editor,msg,wParam,lParam);
2868 ME_SelectWord(editor);
2869 break;
2870 case WM_RBUTTONUP:
2871 case WM_RBUTTONDOWN:
2872 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
2873 !ME_FilterEvent(editor, msg, &wParam, &lParam))
2874 return 0;
2875 goto do_default;
2876 case WM_CONTEXTMENU:
2877 if (!ME_ShowContextMenu(editor, (short)LOWORD(lParam), (short)HIWORD(lParam)))
2878 goto do_default;
2879 break;
2880 case WM_PAINT:
2881 if (editor->bRedraw)
2883 HDC hDC;
2884 PAINTSTRUCT ps;
2886 hDC = BeginPaint(hWnd, &ps);
2887 ME_PaintContent(editor, hDC, FALSE, &ps.rcPaint);
2888 EndPaint(hWnd, &ps);
2890 break;
2891 case WM_SETFOCUS:
2892 editor->bHaveFocus = TRUE;
2893 ME_ShowCaret(editor);
2894 ME_SendOldNotify(editor, EN_SETFOCUS);
2895 return 0;
2896 case WM_KILLFOCUS:
2897 ME_HideCaret(editor);
2898 editor->bHaveFocus = FALSE;
2899 ME_SendOldNotify(editor, EN_KILLFOCUS);
2900 return 0;
2901 case WM_ERASEBKGND:
2903 if (editor->bRedraw)
2905 HDC hDC = (HDC)wParam;
2906 RECT rc;
2907 if (GetUpdateRect(hWnd,&rc,TRUE))
2909 FillRect(hDC, &rc, editor->hbrBackground);
2912 return 1;
2914 case WM_COMMAND:
2915 TRACE("editor wnd command = %d\n", LOWORD(wParam));
2916 return 0;
2917 case WM_KEYUP:
2918 if ((editor->nEventMask & ENM_KEYEVENTS) &&
2919 !ME_FilterEvent(editor, msg, &wParam, &lParam))
2920 return 0;
2921 goto do_default;
2922 case WM_KEYDOWN:
2923 if ((editor->nEventMask & ENM_KEYEVENTS) &&
2924 !ME_FilterEvent(editor, msg, &wParam, &lParam))
2925 return 0;
2926 if (ME_KeyDown(editor, LOWORD(wParam)))
2927 return 0;
2928 goto do_default;
2929 case WM_CHAR:
2931 WCHAR wstr;
2933 if (unicode)
2934 wstr = (WCHAR)wParam;
2935 else
2937 CHAR charA = wParam;
2938 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &wstr, 1);
2940 if (editor->AutoURLDetect_bEnable)
2941 ME_AutoURLDetect(editor, wstr);
2943 switch (wstr)
2945 case 1: /* Ctrl-A */
2946 ME_SetSelection(editor, 0, -1);
2947 return 0;
2948 case 3: /* Ctrl-C */
2949 SendMessageW(editor->hWnd, WM_COPY, 0, 0);
2950 return 0;
2953 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY) {
2954 MessageBeep(MB_ICONERROR);
2955 return 0; /* FIXME really 0 ? */
2958 switch (wstr)
2960 case 22: /* Ctrl-V */
2961 SendMessageW(editor->hWnd, WM_PASTE, 0, 0);
2962 return 0;
2963 case 24: /* Ctrl-X */
2964 SendMessageW(editor->hWnd, WM_CUT, 0, 0);
2965 return 0;
2966 case 25: /* Ctrl-Y */
2967 SendMessageW(editor->hWnd, EM_REDO, 0, 0);
2968 return 0;
2969 case 26: /* Ctrl-Z */
2970 SendMessageW(editor->hWnd, EM_UNDO, 0, 0);
2971 return 0;
2973 if (((unsigned)wstr)>=' '
2974 || (wstr=='\r' && (GetWindowLongW(hWnd, GWL_STYLE) & ES_MULTILINE))
2975 || wstr=='\t') {
2976 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
2977 /* WM_CHAR is restricted to nTextLimit */
2978 int from, to;
2979 ME_GetSelection(editor, &from, &to);
2980 if(editor->nTextLimit > ME_GetTextLength(editor) - (to-from))
2982 ME_Style *style = ME_GetInsertStyle(editor, 0);
2983 ME_SaveTempStyle(editor);
2984 if (wstr == '\r' && (GetKeyState(VK_SHIFT) & 0x8000))
2985 ME_InsertEndRowFromCursor(editor, 0);
2986 else
2987 ME_InsertTextFromCursor(editor, 0, &wstr, 1, style);
2988 ME_ReleaseStyle(style);
2989 ME_CommitUndo(editor);
2991 ME_UpdateRepaint(editor);
2993 return 0;
2995 case EM_SCROLL: /* fall through */
2996 case WM_VSCROLL:
2998 int origNPos;
2999 int lineHeight;
3001 origNPos = ME_GetYScrollPos(editor);
3002 lineHeight = 24;
3004 if (editor && editor->pBuffer && editor->pBuffer->pDefaultStyle)
3005 lineHeight = editor->pBuffer->pDefaultStyle->tm.tmHeight;
3006 if (lineHeight <= 0) lineHeight = 24;
3008 switch(LOWORD(wParam))
3010 case SB_LINEUP:
3011 ME_ScrollUp(editor,lineHeight);
3012 break;
3013 case SB_LINEDOWN:
3014 ME_ScrollDown(editor,lineHeight);
3015 break;
3016 case SB_PAGEUP:
3017 ME_ScrollUp(editor,editor->sizeWindow.cy);
3018 break;
3019 case SB_PAGEDOWN:
3020 ME_ScrollDown(editor,editor->sizeWindow.cy);
3021 break;
3022 case SB_THUMBTRACK:
3023 case SB_THUMBPOSITION:
3024 ME_ScrollAbs(editor,HIWORD(wParam));
3025 break;
3027 if (msg == EM_SCROLL)
3028 return 0x00010000 | (((ME_GetYScrollPos(editor) - origNPos)/lineHeight) & 0xffff);
3029 break;
3031 case WM_MOUSEWHEEL:
3033 int gcWheelDelta;
3034 UINT pulScrollLines;
3036 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
3037 !ME_FilterEvent(editor, msg, &wParam, &lParam))
3038 return 0;
3040 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
3041 gcWheelDelta = -GET_WHEEL_DELTA_WPARAM(wParam);
3043 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
3045 /* FIXME follow the original */
3046 ME_ScrollDown(editor,pulScrollLines * (gcWheelDelta / WHEEL_DELTA) * 8);
3048 break;
3050 case EM_GETRECT:
3052 *((RECT *)lParam) = editor->rcFormat;
3053 return 0;
3055 case EM_SETRECT:
3056 case EM_SETRECTNP:
3058 if (lParam)
3060 RECT *rc = (RECT *)lParam;
3062 if (wParam)
3064 editor->rcFormat.left += rc->left;
3065 editor->rcFormat.top += rc->top;
3066 editor->rcFormat.right += rc->right;
3067 editor->rcFormat.bottom += rc->bottom;
3069 else
3071 editor->rcFormat = *rc;
3074 else
3076 GetClientRect(hWnd, &editor->rcFormat);
3078 if (msg != EM_SETRECTNP)
3079 ME_RewrapRepaint(editor);
3080 return 0;
3082 case EM_REQUESTRESIZE:
3083 ME_SendRequestResize(editor, TRUE);
3084 return 0;
3085 case WM_SETREDRAW:
3086 if ((editor->bRedraw = wParam))
3087 ME_RewrapRepaint(editor);
3088 return 0;
3089 case WM_SIZE:
3091 GetClientRect(hWnd, &editor->rcFormat);
3092 ME_RewrapRepaint(editor);
3093 return DefWindowProcW(hWnd, msg, wParam, lParam);
3095 /* IME messages to make richedit controls IME aware */
3096 case WM_IME_SETCONTEXT:
3097 case WM_IME_CONTROL:
3098 case WM_IME_SELECT:
3099 case WM_IME_COMPOSITIONFULL:
3100 return 0;
3101 case WM_IME_STARTCOMPOSITION:
3103 editor->imeStartIndex=ME_GetCursorOfs(editor,0);
3104 ME_DeleteSelection(editor);
3105 ME_CommitUndo(editor);
3106 ME_UpdateRepaint(editor);
3107 return 0;
3109 case WM_IME_COMPOSITION:
3111 HIMC hIMC;
3113 ME_Style *style = ME_GetInsertStyle(editor, 0);
3114 hIMC = ImmGetContext(hWnd);
3115 ME_DeleteSelection(editor);
3116 ME_CommitUndo(editor);
3117 ME_SaveTempStyle(editor);
3118 if (lParam & GCS_RESULTSTR)
3120 LPWSTR lpCompStr = NULL;
3121 DWORD dwBufLen;
3123 dwBufLen = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
3124 lpCompStr = HeapAlloc(GetProcessHeap(),0,dwBufLen + sizeof(WCHAR));
3125 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, lpCompStr, dwBufLen);
3126 lpCompStr[dwBufLen/sizeof(WCHAR)] = 0;
3127 ME_InsertTextFromCursor(editor,0,lpCompStr,dwBufLen/sizeof(WCHAR),style);
3129 else if (lParam & GCS_COMPSTR)
3131 LPWSTR lpCompStr = NULL;
3132 DWORD dwBufLen;
3134 dwBufLen = ImmGetCompositionStringW(hIMC, GCS_COMPSTR, NULL, 0);
3135 lpCompStr = HeapAlloc(GetProcessHeap(),0,dwBufLen + sizeof(WCHAR));
3136 ImmGetCompositionStringW(hIMC, GCS_COMPSTR, lpCompStr, dwBufLen);
3137 lpCompStr[dwBufLen/sizeof(WCHAR)] = 0;
3139 ME_InsertTextFromCursor(editor,0,lpCompStr,dwBufLen/sizeof(WCHAR),style);
3140 ME_SetSelection(editor,editor->imeStartIndex,
3141 editor->imeStartIndex + dwBufLen/sizeof(WCHAR));
3143 ME_ReleaseStyle(style);
3144 ME_UpdateRepaint(editor);
3145 return 0;
3147 case WM_IME_ENDCOMPOSITION:
3149 ME_DeleteSelection(editor);
3150 editor->imeStartIndex=-1;
3151 return 0;
3153 case EM_GETOLEINTERFACE:
3155 LPVOID *ppvObj = (LPVOID*) lParam;
3156 return CreateIRichEditOle(editor, ppvObj);
3158 case EM_GETPASSWORDCHAR:
3160 return editor->cPasswordMask;
3162 case EM_SETOLECALLBACK:
3163 if(editor->lpOleCallback)
3164 IUnknown_Release(editor->lpOleCallback);
3165 editor->lpOleCallback = (LPRICHEDITOLECALLBACK)lParam;
3166 if(editor->lpOleCallback)
3167 IUnknown_AddRef(editor->lpOleCallback);
3168 return TRUE;
3169 case EM_GETWORDBREAKPROC:
3170 return (LRESULT)editor->pfnWordBreak;
3171 case EM_SETWORDBREAKPROC:
3173 EDITWORDBREAKPROCW pfnOld = editor->pfnWordBreak;
3175 editor->pfnWordBreak = (EDITWORDBREAKPROCW)lParam;
3176 return (LRESULT)pfnOld;
3178 case EM_SETTEXTMODE:
3180 LRESULT ret;
3181 int mask = 0;
3182 int changes = 0;
3183 ret = RichEditWndProc_common(hWnd, WM_GETTEXTLENGTH, 0, 0, unicode);
3184 if (!ret)
3186 /*Check for valid wParam*/
3187 if ((((wParam & TM_RICHTEXT) && ((wParam & TM_PLAINTEXT) << 1))) ||
3188 (((wParam & TM_MULTILEVELUNDO) && ((wParam & TM_SINGLELEVELUNDO) << 1))) ||
3189 (((wParam & TM_MULTICODEPAGE) && ((wParam & TM_SINGLECODEPAGE) << 1))))
3190 return 1;
3191 else
3193 if (wParam & (TM_RICHTEXT | TM_PLAINTEXT))
3195 mask |= (TM_RICHTEXT | TM_PLAINTEXT);
3196 changes |= (wParam & (TM_RICHTEXT | TM_PLAINTEXT));
3198 /*FIXME: Currently no support for undo level and code page options*/
3199 editor->mode = (editor->mode & (~mask)) | changes;
3200 return 0;
3203 return ret;
3205 case EM_SETPASSWORDCHAR:
3207 editor->cPasswordMask = wParam;
3208 ME_RewrapRepaint(editor);
3209 return 0;
3211 case EM_SETTARGETDEVICE:
3212 if (wParam == 0)
3214 BOOL new = (lParam == 0);
3215 if (editor->bWordWrap != new)
3217 editor->bWordWrap = new;
3218 ME_RewrapRepaint(editor);
3221 else FIXME("Unsupported yet non NULL device in EM_SETTARGETDEVICE\n");
3222 break;
3223 default:
3224 do_default:
3225 return DefWindowProcW(hWnd, msg, wParam, lParam);
3227 return 0L;
3230 static LRESULT WINAPI RichEditWndProcW(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
3232 BOOL unicode = TRUE;
3234 /* Under Win9x RichEdit20W returns ANSI strings, see the tests. */
3235 if (msg == WM_GETTEXT && (GetVersion() & 0x80000000))
3236 unicode = FALSE;
3238 return RichEditWndProc_common(hWnd, msg, wParam, lParam, unicode);
3241 static LRESULT WINAPI RichEditWndProcA(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
3243 return RichEditWndProc_common(hWnd, msg, wParam, lParam, FALSE);
3246 /******************************************************************
3247 * RichEditANSIWndProc (RICHED20.10)
3249 LRESULT WINAPI RichEditANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
3251 return RichEditWndProcA(hWnd, msg, wParam, lParam);
3254 /******************************************************************
3255 * RichEdit10ANSIWndProc (RICHED20.9)
3257 LRESULT WINAPI RichEdit10ANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
3259 LRESULT result;
3261 /* FIXME: this is NOT the same as 2.0 version */
3262 result = RichEditANSIWndProc(hWnd, msg, wParam, lParam);
3263 if (msg == WM_NCCREATE)
3265 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongPtrW(hWnd, 0);
3267 editor->bEmulateVersion10 = TRUE;
3268 editor->pBuffer->pLast->member.para.nCharOfs = 2;
3270 return result;
3273 void ME_SendOldNotify(ME_TextEditor *editor, int nCode)
3275 HWND hWnd = editor->hWnd;
3276 SendMessageA(GetParent(hWnd), WM_COMMAND, (nCode<<16)|GetWindowLongW(hWnd, GWLP_ID), (LPARAM)hWnd);
3279 void ME_LinkNotify(ME_TextEditor *editor, UINT msg, WPARAM wParam, LPARAM lParam)
3281 int x,y;
3282 ME_Cursor tmpCursor;
3283 int nCharOfs; /* The start of the clicked text. Absolute character offset */
3285 ME_Run *tmpRun;
3287 ENLINK info;
3288 x = (short)LOWORD(lParam);
3289 y = (short)HIWORD(lParam);
3290 nCharOfs = ME_CharFromPos(editor, x, y);
3291 if (nCharOfs < 0) return;
3293 ME_CursorFromCharOfs(editor, nCharOfs, &tmpCursor);
3294 tmpRun = &tmpCursor.pRun->member.run;
3296 if ((tmpRun->style->fmt.dwMask & CFM_LINK)
3297 && (tmpRun->style->fmt.dwEffects & CFE_LINK))
3298 { /* The clicked run has CFE_LINK set */
3299 info.nmhdr.hwndFrom = editor->hWnd;
3300 info.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
3301 info.nmhdr.code = EN_LINK;
3302 info.msg = msg;
3303 info.wParam = wParam;
3304 info.lParam = lParam;
3305 info.chrg.cpMin = ME_CharOfsFromRunOfs(editor,tmpCursor.pRun,0);
3306 info.chrg.cpMax = info.chrg.cpMin + ME_StrVLen(tmpRun->strText);
3307 SendMessageW(GetParent(editor->hWnd), WM_NOTIFY,info.nmhdr.idFrom, (LPARAM)&info);
3311 int ME_CountParagraphsBetween(ME_TextEditor *editor, int from, int to)
3313 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
3314 int i = 0;
3316 while(item && item->member.para.next_para->member.para.nCharOfs <= from)
3317 item = item->member.para.next_para;
3318 if (!item)
3319 return 0;
3320 while(item && item->member.para.next_para->member.para.nCharOfs <= to) {
3321 item = item->member.para.next_para;
3322 i++;
3324 return i;
3328 int ME_GetTextW(ME_TextEditor *editor, WCHAR *buffer, int nStart, int nChars, int bCRLF)
3330 ME_DisplayItem *item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
3331 int nWritten = 0;
3332 WCHAR *pStart = buffer;
3334 if (!item) {
3335 *buffer = 0;
3336 return 0;
3339 if (nStart)
3341 int nLen = ME_StrLen(item->member.run.strText) - nStart;
3342 if (nLen > nChars)
3343 nLen = nChars;
3344 CopyMemory(buffer, item->member.run.strText->szData + nStart, sizeof(WCHAR)*nLen);
3345 nChars -= nLen;
3346 nWritten += nLen;
3347 if (!nChars)
3348 return nWritten;
3349 buffer += nLen;
3350 nStart = 0;
3351 item = ME_FindItemFwd(item, diRun);
3354 while(nChars && item)
3356 int nLen = ME_StrLen(item->member.run.strText);
3357 if (nLen > nChars)
3358 nLen = nChars;
3360 if (item->member.run.nFlags & MERF_ENDPARA)
3362 if (!ME_FindItemFwd(item, diRun))
3363 /* No '\r' is appended to the last paragraph. */
3364 nLen = 0;
3365 else if (bCRLF && nChars == 1) {
3366 nLen = 0;
3367 nChars = 0;
3368 } else {
3369 *buffer = '\r';
3370 if (bCRLF)
3372 *(++buffer) = '\n';
3373 nWritten++;
3375 assert(nLen == 1);
3376 /* our end paragraph consists of 2 characters now */
3377 if (editor->bEmulateVersion10)
3378 nChars--;
3381 else
3382 CopyMemory(buffer, item->member.run.strText->szData, sizeof(WCHAR)*nLen);
3383 nChars -= nLen;
3384 nWritten += nLen;
3385 buffer += nLen;
3387 if (!nChars)
3389 TRACE("nWritten=%d, actual=%d\n", nWritten, buffer-pStart);
3390 *buffer = 0;
3391 return nWritten;
3393 item = ME_FindItemFwd(item, diRun);
3395 *buffer = 0;
3396 TRACE("nWritten=%d, actual=%d\n", nWritten, buffer-pStart);
3397 return nWritten;
3400 static BOOL ME_RegisterEditorClass(HINSTANCE hInstance)
3402 WNDCLASSW wcW;
3403 WNDCLASSA wcA;
3405 wcW.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
3406 wcW.lpfnWndProc = RichEditWndProcW;
3407 wcW.cbClsExtra = 0;
3408 wcW.cbWndExtra = sizeof(ME_TextEditor *);
3409 wcW.hInstance = NULL; /* hInstance would register DLL-local class */
3410 wcW.hIcon = NULL;
3411 wcW.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
3412 wcW.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
3413 wcW.lpszMenuName = NULL;
3415 if (is_version_nt())
3417 wcW.lpszClassName = RichEdit20W;
3418 if (!RegisterClassW(&wcW)) return FALSE;
3419 wcW.lpszClassName = RichEdit50W;
3420 if (!RegisterClassW(&wcW)) return FALSE;
3422 else
3424 /* WNDCLASSA/W have the same layout */
3425 wcW.lpszClassName = (LPCWSTR)"RichEdit20W";
3426 if (!RegisterClassA((WNDCLASSA *)&wcW)) return FALSE;
3427 wcW.lpszClassName = (LPCWSTR)"RichEdit50W";
3428 if (!RegisterClassA((WNDCLASSA *)&wcW)) return FALSE;
3431 wcA.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
3432 wcA.lpfnWndProc = RichEditWndProcA;
3433 wcA.cbClsExtra = 0;
3434 wcA.cbWndExtra = sizeof(ME_TextEditor *);
3435 wcA.hInstance = NULL; /* hInstance would register DLL-local class */
3436 wcA.hIcon = NULL;
3437 wcA.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
3438 wcA.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
3439 wcA.lpszMenuName = NULL;
3440 wcA.lpszClassName = "RichEdit20A";
3441 if (!RegisterClassA(&wcA)) return FALSE;
3442 wcA.lpszClassName = "RichEdit50A";
3443 if (!RegisterClassA(&wcA)) return FALSE;
3445 return TRUE;
3448 LRESULT WINAPI REComboWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
3449 /* FIXME: Not implemented */
3450 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
3451 hWnd, msg, get_msg_name(msg), wParam, lParam);
3452 return DefWindowProcW(hWnd, msg, wParam, lParam);
3455 LRESULT WINAPI REListWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
3456 /* FIXME: Not implemented */
3457 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
3458 hWnd, msg, get_msg_name(msg), wParam, lParam);
3459 return DefWindowProcW(hWnd, msg, wParam, lParam);
3462 /******************************************************************
3463 * REExtendedRegisterClass (RICHED20.8)
3465 * FIXME undocumented
3466 * Need to check for errors and implement controls and callbacks
3468 LRESULT WINAPI REExtendedRegisterClass(void)
3470 WNDCLASSW wcW;
3471 UINT result;
3473 FIXME("semi stub\n");
3475 wcW.cbClsExtra = 0;
3476 wcW.cbWndExtra = 4;
3477 wcW.hInstance = NULL;
3478 wcW.hIcon = NULL;
3479 wcW.hCursor = NULL;
3480 wcW.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
3481 wcW.lpszMenuName = NULL;
3483 if (!ME_ListBoxRegistered)
3485 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS;
3486 wcW.lpfnWndProc = REListWndProc;
3487 wcW.lpszClassName = REListBox20W;
3488 if (RegisterClassW(&wcW)) ME_ListBoxRegistered = TRUE;
3491 if (!ME_ComboBoxRegistered)
3493 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
3494 wcW.lpfnWndProc = REComboWndProc;
3495 wcW.lpszClassName = REComboBox20W;
3496 if (RegisterClassW(&wcW)) ME_ComboBoxRegistered = TRUE;
3499 result = 0;
3500 if (ME_ListBoxRegistered)
3501 result += 1;
3502 if (ME_ComboBoxRegistered)
3503 result += 2;
3505 return result;
3508 int ME_AutoURLDetect(ME_TextEditor *editor, WCHAR curChar)
3510 struct prefix_s {
3511 const char *text;
3512 int length;
3513 } prefixes[12] = {
3514 {"http:", 5},
3515 {"file:", 6},
3516 {"mailto:", 8},
3517 {"ftp:", 5},
3518 {"https:", 7},
3519 {"gopher:", 8},
3520 {"nntp:", 6},
3521 {"prospero:", 10},
3522 {"telnet:", 8},
3523 {"news:", 6},
3524 {"wais:", 6},
3525 {"www.", 5}
3527 CHARRANGE ins_pt;
3528 int curf_ef, link_ef, def_ef;
3529 int cur_prefx, prefx_cnt;
3530 int sel_min, sel_max;
3531 int car_pos = 0;
3532 int text_pos=-1;
3533 int URLmin, URLmax = 0;
3534 CHARRANGE url;
3535 FINDTEXTA ft;
3536 CHARFORMAT2W cur_format;
3537 CHARFORMAT2W default_format;
3538 CHARFORMAT2W link;
3539 RichEditANSIWndProc(editor->hWnd, EM_EXGETSEL, (WPARAM) 0, (LPARAM) &ins_pt);
3540 sel_min = ins_pt.cpMin;
3541 sel_max = ins_pt.cpMax;
3542 if (sel_min==sel_max)
3543 car_pos = sel_min;
3544 if (sel_min!=sel_max)
3545 car_pos = ME_GetTextLength(editor)+1;
3546 cur_format.cbSize = sizeof(cur_format);
3547 default_format.cbSize = sizeof(default_format);
3548 RichEditANSIWndProc(editor->hWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM) &cur_format);
3549 RichEditANSIWndProc(editor->hWnd, EM_GETCHARFORMAT, SCF_DEFAULT, (LPARAM) &default_format);
3550 link.cbSize = sizeof(link);
3551 link.dwMask = CFM_LINK;
3552 link.dwEffects = CFE_LINK;
3553 curf_ef = cur_format.dwEffects & link.dwEffects;
3554 def_ef = default_format.dwEffects & link.dwEffects;
3555 link_ef = link.dwEffects & link.dwEffects;
3556 if (curf_ef == link_ef)
3558 if( curChar == '\n' || curChar=='\r' || curChar==' ')
3560 ME_SetSelection(editor, car_pos, car_pos);
3561 RichEditANSIWndProc(editor->hWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &default_format);
3562 text_pos=-1;
3563 return 0;
3566 if (curf_ef == def_ef)
3568 cur_prefx = 0;
3569 prefx_cnt = (sizeof(prefixes)/sizeof(struct prefix_s))-1;
3570 while (cur_prefx<=prefx_cnt)
3572 if (text_pos == -1)
3574 ft.lpstrText = prefixes[cur_prefx].text;
3575 URLmin=max(0,(car_pos-prefixes[cur_prefx].length));
3576 URLmax=max(0, car_pos);
3577 if ((car_pos == 0) && (ME_GetTextLength(editor) != 0))
3579 URLmax = ME_GetTextLength(editor)+1;
3581 ft.chrg.cpMin = URLmin;
3582 ft.chrg.cpMax = URLmax;
3583 text_pos=RichEditANSIWndProc(editor->hWnd, EM_FINDTEXT, FR_DOWN, (LPARAM)&ft);
3584 cur_prefx++;
3586 if (text_pos != -1)
3588 url.cpMin=text_pos;
3589 url.cpMax=car_pos-1;
3590 ME_SetCharFormat(editor, text_pos, (URLmax-text_pos), &link);
3591 ME_RewrapRepaint(editor);
3592 break;
3596 return 0;