richedit: Allow reading an enhanced metafile from an RTF stream.
[wine.git] / dlls / riched20 / editor.c
blob39980bd76becf5a800fb838f880a0cf9d016aa7c
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 (no rich text insertion handling, 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.bLineSpacingRule = 0;
463 fmt.dySpaceBefore = fmt.dySpaceAfter = 0;
464 RTFFlushOutputBuffer(info);
465 ME_GetParagraph(info->editor->pCursors[0].pRun)->member.para.bTable = FALSE;
466 break;
467 case rtfInTable:
469 ME_DisplayItem *para;
471 RTFFlushOutputBuffer(info);
472 para = ME_GetParagraph(info->editor->pCursors[0].pRun);
473 assert(para->member.para.pCells);
474 para->member.para.bTable = TRUE;
475 return;
477 case rtfFirstIndent:
478 ME_GetSelectionParaFormat(info->editor, &fmt);
479 fmt.dwMask |= PFM_STARTINDENT | PFM_OFFSET;
480 fmt.dxStartIndent += info->rtfParam;
481 fmt.dxOffset = -info->rtfParam;
482 break;
483 case rtfLeftIndent:
484 /* we assume rtfLeftIndent is always specified before rtfFirstIndent */
485 ME_GetSelectionParaFormat(info->editor, &fmt);
486 fmt.dwMask |= PFM_STARTINDENT;
487 fmt.dxStartIndent = info->rtfParam;
488 fmt.dxOffset = 0;
489 break;
490 case rtfRightIndent:
491 fmt.dwMask = PFM_RIGHTINDENT;
492 fmt.dxRightIndent = info->rtfParam;
493 break;
494 case rtfQuadLeft:
495 case rtfQuadJust:
496 fmt.dwMask = PFM_ALIGNMENT;
497 fmt.wAlignment = PFA_LEFT;
498 break;
499 case rtfQuadRight:
500 fmt.dwMask = PFM_ALIGNMENT;
501 fmt.wAlignment = PFA_RIGHT;
502 break;
503 case rtfQuadCenter:
504 fmt.dwMask = PFM_ALIGNMENT;
505 fmt.wAlignment = PFA_CENTER;
506 break;
507 case rtfTabPos:
508 ME_GetSelectionParaFormat(info->editor, &fmt);
509 if (!(fmt.dwMask & PFM_TABSTOPS))
511 fmt.dwMask |= PFM_TABSTOPS;
512 fmt.cTabCount = 0;
514 if (fmt.cTabCount < MAX_TAB_STOPS)
515 fmt.rgxTabs[fmt.cTabCount++] = info->rtfParam;
516 break;
517 case rtfKeep:
518 fmt.dwMask = PFM_KEEP;
519 fmt.wEffects = PFE_KEEP;
520 break;
521 case rtfNoWidowControl:
522 fmt.dwMask = PFM_NOWIDOWCONTROL;
523 fmt.wEffects = PFE_NOWIDOWCONTROL;
524 break;
525 case rtfKeepNext:
526 fmt.dwMask = PFM_KEEPNEXT;
527 fmt.wEffects = PFE_KEEPNEXT;
528 break;
529 case rtfSpaceAfter:
530 fmt.dwMask = PFM_SPACEAFTER;
531 fmt.dySpaceAfter = info->rtfParam;
532 break;
533 case rtfSpaceBefore:
534 fmt.dwMask = PFM_SPACEBEFORE;
535 fmt.dySpaceBefore = info->rtfParam;
536 break;
537 case rtfSpaceBetween:
538 fmt.dwMask = PFM_LINESPACING;
539 if ((int)info->rtfParam > 0)
541 fmt.dyLineSpacing = info->rtfParam;
542 fmt.bLineSpacingRule = 3;
544 else
546 fmt.dyLineSpacing = info->rtfParam;
547 fmt.bLineSpacingRule = 4;
549 case rtfSpaceMultiply:
550 fmt.dwMask = PFM_LINESPACING;
551 fmt.dyLineSpacing = info->rtfParam * 20;
552 fmt.bLineSpacingRule = 5;
553 break;
554 case rtfParBullet:
555 fmt.dwMask = PFM_NUMBERING;
556 fmt.wNumbering = PFN_BULLET;
557 break;
558 case rtfParSimple:
559 fmt.dwMask = PFM_NUMBERING;
560 fmt.wNumbering = 2; /* FIXME: MSDN says it's not used ?? */
561 break;
562 case rtfParNumDecimal:
563 fmt.dwMask = PFM_NUMBERING;
564 fmt.wNumbering = 2; /* FIXME: MSDN says it's not used ?? */
565 break;
566 case rtfParNumIndent:
567 fmt.dwMask = PFM_NUMBERINGTAB;
568 fmt.wNumberingTab = info->rtfParam;
569 break;
570 case rtfParNumStartAt:
571 fmt.dwMask = PFM_NUMBERINGSTART;
572 fmt.wNumberingStart = info->rtfParam;
573 break;
574 case rtfBorderLeft:
575 ME_GetSelectionParaFormat(info->editor, &fmt);
576 if (!(fmt.dwMask & PFM_BORDER))
578 fmt.dwMask |= PFM_BORDER;
579 fmt.wBorderSpace = 0;
580 fmt.wBorderWidth = 1;
581 fmt.wBorders = 0;
583 fmt.wBorders |= 1;
584 break;
585 case rtfBorderRight:
586 ME_GetSelectionParaFormat(info->editor, &fmt);
587 if (!(fmt.dwMask & PFM_BORDER))
589 fmt.dwMask |= PFM_BORDER;
590 fmt.wBorderSpace = 0;
591 fmt.wBorderWidth = 1;
592 fmt.wBorders = 0;
594 fmt.wBorders |= 2;
595 break;
596 case rtfBorderTop:
597 ME_GetSelectionParaFormat(info->editor, &fmt);
598 if (!(fmt.dwMask & PFM_BORDER))
600 fmt.dwMask |= PFM_BORDER;
601 fmt.wBorderSpace = 0;
602 fmt.wBorderWidth = 1;
603 fmt.wBorders = 0;
605 fmt.wBorders |= 4;
606 break;
607 case rtfBorderBottom:
608 ME_GetSelectionParaFormat(info->editor, &fmt);
609 if (!(fmt.dwMask & PFM_BORDER))
611 fmt.dwMask |= PFM_BORDER;
612 fmt.wBorderSpace = 0;
613 fmt.wBorderWidth = 1;
614 fmt.wBorders = 0;
616 fmt.wBorders |= 8;
617 break;
618 case rtfBorderSingle:
619 ME_GetSelectionParaFormat(info->editor, &fmt);
620 /* we assume that borders have been created before (RTF spec) */
621 fmt.wBorders &= ~0x70;
622 fmt.wBorders |= 1 << 8;
623 break;
624 case rtfBorderThick:
625 ME_GetSelectionParaFormat(info->editor, &fmt);
626 /* we assume that borders have been created before (RTF spec) */
627 fmt.wBorders &= ~0x70;
628 fmt.wBorders |= 2 << 8;
629 break;
630 case rtfBorderShadow:
631 ME_GetSelectionParaFormat(info->editor, &fmt);
632 /* we assume that borders have been created before (RTF spec) */
633 fmt.wBorders &= ~0x70;
634 fmt.wBorders |= 10 << 8;
635 break;
636 case rtfBorderDouble:
637 ME_GetSelectionParaFormat(info->editor, &fmt);
638 /* we assume that borders have been created before (RTF spec) */
639 fmt.wBorders &= ~0x70;
640 fmt.wBorders |= 7 << 8;
641 break;
642 case rtfBorderDot:
643 ME_GetSelectionParaFormat(info->editor, &fmt);
644 /* we assume that borders have been created before (RTF spec) */
645 fmt.wBorders &= ~0x70;
646 fmt.wBorders |= 11 << 8;
647 break;
648 case rtfBorderWidth:
649 ME_GetSelectionParaFormat(info->editor, &fmt);
650 /* we assume that borders have been created before (RTF spec) */
651 fmt.wBorderWidth |= ((info->rtfParam / 15) & 7) << 8;
652 break;
653 case rtfBorderSpace:
654 ME_GetSelectionParaFormat(info->editor, &fmt);
655 /* we assume that borders have been created before (RTF spec) */
656 fmt.wBorderSpace = info->rtfParam;
657 break;
659 if (fmt.dwMask) {
660 RTFFlushOutputBuffer(info);
661 /* FIXME too slow ? how come ?*/
662 ME_SetSelectionParaFormat(info->editor, &fmt);
666 static void ME_RTFTblAttrHook(RTF_Info *info)
668 ME_DisplayItem *para;
670 switch (info->rtfMinor)
672 case rtfRowDef:
673 RTFFlushOutputBuffer(info);
674 para = ME_GetParagraph(info->editor->pCursors[0].pRun);
676 /* Release possibly inherited cell definitions */
677 ME_DestroyTableCellList(para);
679 para->member.para.pCells = ALLOC_OBJ(ME_TableCell);
680 para->member.para.pCells->nRightBoundary = 0;
681 para->member.para.pCells->next = NULL;
682 para->member.para.pLastCell = para->member.para.pCells;
683 break;
684 case rtfCellPos:
685 RTFFlushOutputBuffer(info);
686 para = ME_GetParagraph(info->editor->pCursors[0].pRun);
688 if (para->member.para.pLastCell->nRightBoundary)
690 ME_TableCell *pCell = ALLOC_OBJ(ME_TableCell);
692 pCell->next = NULL;
693 para->member.para.pLastCell->next = pCell;
694 para->member.para.pLastCell = pCell;
696 para->member.para.pLastCell->nRightBoundary = info->rtfParam;
697 break;
701 static BOOL ME_RTFInsertOleObject(RTF_Info *info, HENHMETAFILE hemf, HBITMAP hbmp,
702 const SIZEL* sz)
704 LPOLEOBJECT lpObject = NULL;
705 LPSTORAGE lpStorage = NULL;
706 LPOLECLIENTSITE lpClientSite = NULL;
707 LPDATAOBJECT lpDataObject = NULL;
708 LPOLECACHE lpOleCache = NULL;
709 STGMEDIUM stgm;
710 FORMATETC fm;
711 CLSID clsid;
712 BOOL ret = FALSE;
713 DWORD conn;
715 if (hemf)
717 stgm.tymed = TYMED_ENHMF;
718 stgm.u.hEnhMetaFile = hemf;
719 fm.cfFormat = CF_ENHMETAFILE;
721 else if (hbmp)
723 stgm.tymed = TYMED_GDI;
724 stgm.u.hBitmap = hbmp;
725 fm.cfFormat = CF_BITMAP;
727 stgm.pUnkForRelease = NULL;
729 fm.ptd = NULL;
730 fm.dwAspect = DVASPECT_CONTENT;
731 fm.lindex = -1;
732 fm.tymed = stgm.tymed;
734 if (!info->lpRichEditOle)
736 CreateIRichEditOle(info->editor, (VOID**)&info->lpRichEditOle);
739 if (OleCreateDefaultHandler(&CLSID_NULL, NULL, &IID_IOleObject, (void**)&lpObject) == S_OK &&
740 #if 0
741 /* FIXME: enable it when rich-edit properly implements this method */
742 IRichEditOle_GetClientSite(info->lpRichEditOle, &lpClientSite) == S_OK &&
743 IOleObject_SetClientSite(lpObject, lpClientSite) == S_OK &&
744 #endif
745 IOleObject_GetUserClassID(lpObject, &clsid) == S_OK &&
746 IOleObject_QueryInterface(lpObject, &IID_IOleCache, (void**)&lpOleCache) == S_OK &&
747 IOleCache_Cache(lpOleCache, &fm, 0, &conn) == S_OK &&
748 IOleObject_QueryInterface(lpObject, &IID_IDataObject, (void**)&lpDataObject) == S_OK &&
749 IDataObject_SetData(lpDataObject, &fm, &stgm, TRUE) == S_OK)
751 REOBJECT reobject;
753 reobject.cbStruct = sizeof(reobject);
754 reobject.cp = REO_CP_SELECTION;
755 reobject.clsid = clsid;
756 reobject.poleobj = lpObject;
757 reobject.pstg = lpStorage;
758 reobject.polesite = lpClientSite;
759 /* convert from twips to .01 mm */
760 reobject.sizel.cx = MulDiv(sz->cx, 254, 144);
761 reobject.sizel.cy = MulDiv(sz->cy, 254, 144);
762 reobject.dvaspect = DVASPECT_CONTENT;
763 reobject.dwFlags = 0; /* FIXME */
764 reobject.dwUser = 0;
766 /* FIXME: could be simpler */
767 ret = IRichEditOle_InsertObject(info->lpRichEditOle, &reobject) == S_OK;
770 if (lpObject) IOleObject_Release(lpObject);
771 if (lpClientSite) IOleClientSite_Release(lpClientSite);
772 if (lpStorage) IStorage_Release(lpStorage);
773 if (lpDataObject) IDataObject_Release(lpDataObject);
774 if (lpOleCache) IOleCache_Release(lpOleCache);
776 return ret;
779 static void ME_RTFReadPictGroup(RTF_Info *info)
781 SIZEL sz;
782 BYTE* buffer = NULL;
783 unsigned bufsz, bufidx;
784 BOOL flip;
785 BYTE val;
786 METAFILEPICT mfp;
787 HENHMETAFILE hemf;
788 HBITMAP hbmp;
789 enum gfxkind {gfx_unknown = 0, gfx_enhmetafile, gfx_metafile, gfx_dib} gfx = gfx_unknown;
791 RTFGetToken (info);
792 if (info->rtfClass == rtfEOF)
793 return;
794 mfp.mm = MM_TEXT;
795 /* fetch picture type */
796 if (RTFCheckMM (info, rtfPictAttr, rtfWinMetafile))
798 mfp.mm = info->rtfParam;
799 gfx = gfx_metafile;
801 else if (RTFCheckMM (info, rtfPictAttr, rtfDevIndBitmap))
803 if (info->rtfParam != 0) FIXME("dibitmap should be 0 (%d)\n", info->rtfParam);
804 gfx = gfx_dib;
806 else if (RTFCheckMM (info, rtfPictAttr, rtfEmfBlip))
808 gfx = gfx_enhmetafile;
810 else
812 FIXME("%d %d\n", info->rtfMajor, info->rtfMinor);
813 goto skip_group;
815 sz.cx = sz.cy = 0;
816 /* fetch picture attributes */
817 for (;;)
819 RTFGetToken (info);
820 if (info->rtfClass == rtfEOF)
821 return;
822 if (info->rtfClass == rtfText)
823 break;
824 if (!RTFCheckCM (info, rtfControl, rtfPictAttr))
826 ERR("Expected picture attribute (%d %d)\n",
827 info->rtfClass, info->rtfMajor);
828 goto skip_group;
830 else if (RTFCheckMM (info, rtfPictAttr, rtfPicWid))
832 if (gfx == gfx_metafile) mfp.xExt = info->rtfParam;
834 else if (RTFCheckMM (info, rtfPictAttr, rtfPicHt))
836 if (gfx == gfx_metafile) mfp.yExt = info->rtfParam;
838 else if (RTFCheckMM (info, rtfPictAttr, rtfPicGoalWid))
839 sz.cx = info->rtfParam;
840 else if (RTFCheckMM (info, rtfPictAttr, rtfPicGoalHt))
841 sz.cy = info->rtfParam;
842 else
843 FIXME("Non supported attribute: %d %d %d\n", info->rtfClass, info->rtfMajor, info->rtfMinor);
845 /* fetch picture data */
846 bufsz = 1024;
847 bufidx = 0;
848 buffer = HeapAlloc(GetProcessHeap(), 0, bufsz);
849 val = info->rtfMajor;
850 for (flip = TRUE;; flip = !flip)
852 RTFGetToken (info);
853 if (info->rtfClass == rtfEOF)
855 HeapFree(GetProcessHeap(), 0, buffer);
856 return; /* Warn ?? */
858 if (RTFCheckCM(info, rtfGroup, rtfEndGroup))
859 break;
860 if (info->rtfClass != rtfText) goto skip_group;
861 if (flip)
863 if (bufidx >= bufsz &&
864 !(buffer = HeapReAlloc(GetProcessHeap(), 0, buffer, bufsz += 1024)))
865 goto skip_group;
866 buffer[bufidx++] = RTFCharToHex(val) * 16 + RTFCharToHex(info->rtfMajor);
868 else
869 val = info->rtfMajor;
871 if (flip) FIXME("wrong hex string\n");
873 switch (gfx)
875 case gfx_enhmetafile:
876 if ((hemf = SetEnhMetaFileBits(bufidx, buffer)))
877 ME_RTFInsertOleObject(info, hemf, NULL, &sz);
878 break;
879 case gfx_metafile:
880 if ((hemf = SetWinMetaFileBits(bufidx, buffer, NULL, &mfp)))
881 ME_RTFInsertOleObject(info, hemf, NULL, &sz);
882 break;
883 case gfx_dib:
885 BITMAPINFO* bi = (BITMAPINFO*)buffer;
886 HDC hdc = GetDC(0);
887 unsigned nc = bi->bmiHeader.biClrUsed;
889 /* not quite right, especially for bitfields type of compression */
890 if (!nc && bi->bmiHeader.biBitCount <= 8)
891 nc = 1 << bi->bmiHeader.biBitCount;
892 if ((hbmp = CreateDIBitmap(hdc, &bi->bmiHeader,
893 CBM_INIT, (char*)(bi + 1) + nc * sizeof(RGBQUAD),
894 bi, DIB_RGB_COLORS)))
895 ME_RTFInsertOleObject(info, NULL, hbmp, &sz);
896 ReleaseDC(0, hdc);
898 break;
899 default:
900 break;
902 HeapFree(GetProcessHeap(), 0, buffer);
903 RTFRouteToken (info); /* feed "}" back to router */
904 return;
905 skip_group:
906 HeapFree(GetProcessHeap(), 0, buffer);
907 RTFSkipGroup(info);
908 RTFRouteToken(info); /* feed "}" back to router */
911 /* for now, lookup the \result part and use it, whatever the object */
912 static void ME_RTFReadObjectGroup(RTF_Info *info)
914 for (;;)
916 RTFGetToken (info);
917 if (info->rtfClass == rtfEOF)
918 return;
919 if (RTFCheckCM(info, rtfGroup, rtfEndGroup))
920 break;
921 if (RTFCheckCM(info, rtfGroup, rtfBeginGroup))
923 RTFGetToken (info);
924 if (info->rtfClass == rtfEOF)
925 return;
926 if (RTFCheckCMM(info, rtfControl, rtfDestination, rtfObjResult))
928 int level = 1;
930 while (RTFGetToken (info) != rtfEOF)
932 if (info->rtfClass == rtfGroup)
934 if (info->rtfMajor == rtfBeginGroup) level++;
935 else if (info->rtfMajor == rtfEndGroup && --level < 0) break;
937 RTFRouteToken(info);
940 else RTFSkipGroup(info);
941 continue;
943 if (!RTFCheckCM (info, rtfControl, rtfObjAttr))
945 FIXME("Non supported attribute: %d %d %d\n", info->rtfClass, info->rtfMajor, info->rtfMinor);
946 return;
949 RTFRouteToken(info); /* feed "}" back to router */
952 static void ME_RTFReadHook(RTF_Info *info) {
953 switch(info->rtfClass)
955 case rtfGroup:
956 switch(info->rtfMajor)
958 case rtfBeginGroup:
959 if (info->stackTop < maxStack) {
960 info->stack[info->stackTop].fmt = info->style->fmt;
961 info->stack[info->stackTop].codePage = info->codePage;
962 info->stack[info->stackTop].unicodeLength = info->unicodeLength;
964 info->stackTop++;
965 info->styleChanged = FALSE;
966 break;
967 case rtfEndGroup:
969 ME_Style *s;
970 RTFFlushOutputBuffer(info);
971 if (info->stackTop<=1) {
972 info->rtfClass = rtfEOF;
973 return;
975 info->stackTop--;
976 assert(info->stackTop >= 0);
977 if (info->styleChanged)
979 /* FIXME too slow ? how come ? */
980 s = ME_ApplyStyle(info->style, &info->stack[info->stackTop].fmt);
981 ME_ReleaseStyle(info->style);
982 info->style = s;
983 info->codePage = info->stack[info->stackTop].codePage;
984 info->unicodeLength = info->stack[info->stackTop].unicodeLength;
986 break;
989 break;
990 case rtfControl:
991 switch(info->rtfMajor)
993 case rtfCharAttr:
994 ME_RTFCharAttrHook(info);
995 break;
996 case rtfParAttr:
997 ME_RTFParAttrHook(info);
998 break;
999 case rtfTblAttr:
1000 ME_RTFTblAttrHook(info);
1001 break;
1002 case rtfSpecialChar:
1003 if (info->rtfMinor == rtfCell)
1005 RTFFlushOutputBuffer(info);
1006 ME_InsertTableCellFromCursor(info->editor, 0);
1009 break;
1013 void
1014 ME_StreamInFill(ME_InStream *stream)
1016 stream->editstream->dwError = stream->editstream->pfnCallback(stream->editstream->dwCookie,
1017 (BYTE *)stream->buffer,
1018 sizeof(stream->buffer),
1019 (LONG *)&stream->dwSize);
1020 stream->dwUsed = 0;
1023 static LRESULT ME_StreamIn(ME_TextEditor *editor, DWORD format, EDITSTREAM *stream)
1025 RTF_Info parser;
1026 ME_Style *style;
1027 int from, to, to2, nUndoMode;
1028 int nEventMask = editor->nEventMask;
1029 ME_InStream inStream;
1031 TRACE("stream==%p hWnd==%p format==0x%X\n", stream, editor->hWnd, format);
1032 editor->nEventMask = 0;
1034 ME_GetSelection(editor, &from, &to);
1035 if ((format & SFF_SELECTION) && (editor->mode & TM_RICHTEXT)) {
1036 style = ME_GetSelectionInsertStyle(editor);
1038 ME_InternalDeleteText(editor, from, to-from);
1040 else {
1041 style = editor->pBuffer->pDefaultStyle;
1042 ME_AddRefStyle(style);
1043 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
1044 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
1045 from = to = 0;
1046 ME_ClearTempStyle(editor);
1047 /* FIXME restore default paragraph formatting ! */
1051 /* Back up undo mode to a local variable */
1052 nUndoMode = editor->nUndoMode;
1054 /* Only create an undo if SFF_SELECTION is set */
1055 if (!(format & SFF_SELECTION))
1056 editor->nUndoMode = umIgnore;
1058 inStream.editstream = stream;
1059 inStream.editstream->dwError = 0;
1060 inStream.dwSize = 0;
1061 inStream.dwUsed = 0;
1063 if (format & SF_RTF)
1065 /* Check if it's really RTF, and if it is not, use plain text */
1066 ME_StreamInFill(&inStream);
1067 if (!inStream.editstream->dwError)
1069 if (strncmp(inStream.buffer, "{\\rtf", 5) && strncmp(inStream.buffer, "{\\urtf", 6))
1071 format &= ~SF_RTF;
1072 format |= SF_TEXT;
1077 if (!inStream.editstream->dwError)
1079 if (format & SF_RTF) {
1080 /* setup the RTF parser */
1081 memset(&parser, 0, sizeof parser);
1082 RTFSetEditStream(&parser, &inStream);
1083 parser.rtfFormat = format&(SF_TEXT|SF_RTF);
1084 parser.hwndEdit = editor->hWnd;
1085 parser.editor = editor;
1086 parser.style = style;
1087 WriterInit(&parser);
1088 RTFInit(&parser);
1089 RTFSetReadHook(&parser, ME_RTFReadHook);
1090 RTFSetDestinationCallback(&parser, rtfPict, ME_RTFReadPictGroup);
1091 RTFSetDestinationCallback(&parser, rtfObject, ME_RTFReadObjectGroup);
1092 BeginFile(&parser);
1094 /* do the parsing */
1095 RTFRead(&parser);
1096 RTFFlushOutputBuffer(&parser);
1097 RTFDestroy(&parser);
1098 if (parser.lpRichEditOle)
1099 IRichEditOle_Release(parser.lpRichEditOle);
1101 style = parser.style;
1103 else if (format & SF_TEXT)
1104 ME_StreamInText(editor, format, &inStream, style);
1105 else
1106 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
1107 ME_GetSelection(editor, &to, &to2);
1108 /* put the cursor at the top */
1109 if (!(format & SFF_SELECTION))
1110 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
1113 /* Restore saved undo mode */
1114 editor->nUndoMode = nUndoMode;
1116 /* even if we didn't add an undo, we need to commit anything on the stack */
1117 ME_CommitUndo(editor);
1119 /* If SFF_SELECTION isn't set, delete any undos from before we started too */
1120 if (!(format & SFF_SELECTION))
1121 ME_EmptyUndoStack(editor);
1123 ME_ReleaseStyle(style);
1124 editor->nEventMask = nEventMask;
1125 if (editor->bRedraw)
1127 ME_UpdateRepaint(editor);
1129 if (!(format & SFF_SELECTION)) {
1130 ME_ClearTempStyle(editor);
1132 ME_MoveCaret(editor);
1133 ME_SendSelChange(editor);
1134 ME_SendRequestResize(editor, FALSE);
1136 return 0;
1140 typedef struct tagME_RTFStringStreamStruct
1142 char *string;
1143 int pos;
1144 int length;
1145 } ME_RTFStringStreamStruct;
1147 static DWORD CALLBACK ME_ReadFromRTFString(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1149 ME_RTFStringStreamStruct *pStruct = (ME_RTFStringStreamStruct *)dwCookie;
1150 int count;
1152 count = min(cb, pStruct->length - pStruct->pos);
1153 memmove(lpBuff, pStruct->string + pStruct->pos, count);
1154 pStruct->pos += count;
1155 *pcb = count;
1156 return 0;
1159 static void
1160 ME_StreamInRTFString(ME_TextEditor *editor, BOOL selection, char *string)
1162 EDITSTREAM es;
1163 ME_RTFStringStreamStruct data;
1165 data.string = string;
1166 data.length = strlen(string);
1167 data.pos = 0;
1168 es.dwCookie = (DWORD)&data;
1169 es.pfnCallback = ME_ReadFromRTFString;
1170 ME_StreamIn(editor, SF_RTF | (selection ? SFF_SELECTION : 0), &es);
1174 ME_DisplayItem *
1175 ME_FindItemAtOffset(ME_TextEditor *editor, ME_DIType nItemType, int nOffset, int *nItemOffset)
1177 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
1179 while (item && item->member.para.next_para->member.para.nCharOfs <= nOffset)
1180 item = ME_FindItemFwd(item, diParagraph);
1182 if (!item)
1183 return item;
1185 nOffset -= item->member.para.nCharOfs;
1186 if (nItemType == diParagraph) {
1187 if (nItemOffset)
1188 *nItemOffset = nOffset;
1189 return item;
1192 do {
1193 item = ME_FindItemFwd(item, diRun);
1194 } while (item && (item->member.run.nCharOfs + ME_StrLen(item->member.run.strText) <= nOffset));
1195 if (item) {
1196 nOffset -= item->member.run.nCharOfs;
1197 if (nItemOffset)
1198 *nItemOffset = nOffset;
1200 return item;
1204 static int
1205 ME_FindText(ME_TextEditor *editor, DWORD flags, const CHARRANGE *chrg, const WCHAR *text, CHARRANGE *chrgText)
1207 const int nLen = lstrlenW(text);
1208 const int nTextLen = ME_GetTextLength(editor);
1209 int nStart, nEnd;
1210 int nMin, nMax;
1211 ME_DisplayItem *item;
1212 ME_DisplayItem *para;
1213 WCHAR wLastChar = ' ';
1215 TRACE("flags==0x%08x, chrg->cpMin==%d, chrg->cpMax==%d text==%s\n",
1216 flags, chrg->cpMin, chrg->cpMax, debugstr_w(text));
1218 if (flags & ~(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD))
1219 FIXME("Flags 0x%08x not implemented\n",
1220 flags & ~(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD));
1222 nMin = chrg->cpMin;
1223 if (chrg->cpMax == -1)
1224 nMax = nTextLen;
1225 else
1226 nMax = chrg->cpMax > nTextLen ? nTextLen : chrg->cpMax;
1228 /* when searching up, if cpMin < cpMax, then instead of searching
1229 * on [cpMin,cpMax], we search on [0,cpMin], otherwise, search on
1230 * [cpMax, cpMin]. The exception is when cpMax is -1, in which
1231 * case, it is always bigger than cpMin.
1233 if (!(flags & FR_DOWN))
1235 int nSwap = nMax;
1237 nMax = nMin > nTextLen ? nTextLen : nMin;
1238 if (nMin < nSwap || chrg->cpMax == -1)
1239 nMin = 0;
1240 else
1241 nMin = nSwap;
1244 if (!nLen || nMin < 0 || nMax < 0 || nMax < nMin)
1246 if (chrgText)
1247 chrgText->cpMin = chrgText->cpMax = -1;
1248 return -1;
1251 if (flags & FR_DOWN) /* Forward search */
1253 /* If possible, find the character before where the search starts */
1254 if ((flags & FR_WHOLEWORD) && nMin)
1256 nStart = nMin - 1;
1257 item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
1258 if (!item)
1260 if (chrgText)
1261 chrgText->cpMin = chrgText->cpMax = -1;
1262 return -1;
1264 wLastChar = item->member.run.strText->szData[nStart];
1267 nStart = nMin;
1268 item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
1269 if (!item)
1271 if (chrgText)
1272 chrgText->cpMin = chrgText->cpMax = -1;
1273 return -1;
1276 para = ME_GetParagraph(item);
1277 while (item
1278 && para->member.para.nCharOfs + item->member.run.nCharOfs + nStart + nLen <= nMax)
1280 ME_DisplayItem *pCurItem = item;
1281 int nCurStart = nStart;
1282 int nMatched = 0;
1284 while (pCurItem && ME_CharCompare(pCurItem->member.run.strText->szData[nCurStart + nMatched], text[nMatched], (flags & FR_MATCHCASE)))
1286 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
1287 break;
1289 nMatched++;
1290 if (nMatched == nLen)
1292 ME_DisplayItem *pNextItem = pCurItem;
1293 int nNextStart = nCurStart;
1294 WCHAR wNextChar;
1296 /* Check to see if next character is a whitespace */
1297 if (flags & FR_WHOLEWORD)
1299 if (nCurStart + nMatched == ME_StrLen(pCurItem->member.run.strText))
1301 pNextItem = ME_FindItemFwd(pCurItem, diRun);
1302 nNextStart = -nMatched;
1305 if (pNextItem)
1306 wNextChar = pNextItem->member.run.strText->szData[nNextStart + nMatched];
1307 else
1308 wNextChar = ' ';
1310 if (isalnumW(wNextChar))
1311 break;
1314 nStart += para->member.para.nCharOfs + pCurItem->member.run.nCharOfs;
1315 if (chrgText)
1317 chrgText->cpMin = nStart;
1318 chrgText->cpMax = nStart + nLen;
1320 TRACE("found at %d-%d\n", nStart, nStart + nLen);
1321 return nStart;
1323 if (nCurStart + nMatched == ME_StrLen(pCurItem->member.run.strText))
1325 pCurItem = ME_FindItemFwd(pCurItem, diRun);
1326 para = ME_GetParagraph(pCurItem);
1327 nCurStart = -nMatched;
1330 if (pCurItem)
1331 wLastChar = pCurItem->member.run.strText->szData[nCurStart + nMatched];
1332 else
1333 wLastChar = ' ';
1335 nStart++;
1336 if (nStart == ME_StrLen(item->member.run.strText))
1338 item = ME_FindItemFwd(item, diRun);
1339 para = ME_GetParagraph(item);
1340 nStart = 0;
1344 else /* Backward search */
1346 /* If possible, find the character after where the search ends */
1347 if ((flags & FR_WHOLEWORD) && nMax < nTextLen - 1)
1349 nEnd = nMax + 1;
1350 item = ME_FindItemAtOffset(editor, diRun, nEnd, &nEnd);
1351 if (!item)
1353 if (chrgText)
1354 chrgText->cpMin = chrgText->cpMax = -1;
1355 return -1;
1357 wLastChar = item->member.run.strText->szData[nEnd];
1360 nEnd = nMax;
1361 item = ME_FindItemAtOffset(editor, diRun, nEnd, &nEnd);
1362 if (!item)
1364 if (chrgText)
1365 chrgText->cpMin = chrgText->cpMax = -1;
1366 return -1;
1369 para = ME_GetParagraph(item);
1371 while (item
1372 && para->member.para.nCharOfs + item->member.run.nCharOfs + nEnd - nLen >= nMin)
1374 ME_DisplayItem *pCurItem = item;
1375 int nCurEnd = nEnd;
1376 int nMatched = 0;
1378 if (nCurEnd - nMatched == 0)
1380 pCurItem = ME_FindItemBack(pCurItem, diRun);
1381 para = ME_GetParagraph(pCurItem);
1382 nCurEnd = ME_StrLen(pCurItem->member.run.strText) + nMatched;
1385 while (pCurItem && ME_CharCompare(pCurItem->member.run.strText->szData[nCurEnd - nMatched - 1], text[nLen - nMatched - 1], (flags & FR_MATCHCASE)))
1387 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
1388 break;
1390 nMatched++;
1391 if (nMatched == nLen)
1393 ME_DisplayItem *pPrevItem = pCurItem;
1394 int nPrevEnd = nCurEnd;
1395 WCHAR wPrevChar;
1397 /* Check to see if previous character is a whitespace */
1398 if (flags & FR_WHOLEWORD)
1400 if (nPrevEnd - nMatched == 0)
1402 pPrevItem = ME_FindItemBack(pCurItem, diRun);
1403 if (pPrevItem)
1404 nPrevEnd = ME_StrLen(pPrevItem->member.run.strText) + nMatched;
1407 if (pPrevItem)
1408 wPrevChar = pPrevItem->member.run.strText->szData[nPrevEnd - nMatched - 1];
1409 else
1410 wPrevChar = ' ';
1412 if (isalnumW(wPrevChar))
1413 break;
1416 nStart = para->member.para.nCharOfs + pCurItem->member.run.nCharOfs + nCurEnd - nMatched;
1417 if (chrgText)
1419 chrgText->cpMin = nStart;
1420 chrgText->cpMax = nStart + nLen;
1422 TRACE("found at %d-%d\n", nStart, nStart + nLen);
1423 return nStart;
1425 if (nCurEnd - nMatched == 0)
1427 pCurItem = ME_FindItemBack(pCurItem, diRun);
1428 /* Don't care about pCurItem becoming NULL here; it's already taken
1429 * care of in the exterior loop condition */
1430 para = ME_GetParagraph(pCurItem);
1431 nCurEnd = ME_StrLen(pCurItem->member.run.strText) + nMatched;
1434 if (pCurItem)
1435 wLastChar = pCurItem->member.run.strText->szData[nCurEnd - nMatched - 1];
1436 else
1437 wLastChar = ' ';
1439 nEnd--;
1440 if (nEnd < 0)
1442 item = ME_FindItemBack(item, diRun);
1443 para = ME_GetParagraph(item);
1444 nEnd = ME_StrLen(item->member.run.strText);
1448 TRACE("not found\n");
1449 if (chrgText)
1450 chrgText->cpMin = chrgText->cpMax = -1;
1451 return -1;
1454 /* helper to send a msg filter notification */
1455 static BOOL
1456 ME_FilterEvent(ME_TextEditor *editor, UINT msg, WPARAM* wParam, LPARAM* lParam)
1458 MSGFILTER msgf;
1460 msgf.nmhdr.hwndFrom = editor->hWnd;
1461 msgf.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
1462 msgf.nmhdr.code = EN_MSGFILTER;
1463 msgf.msg = msg;
1465 msgf.wParam = *wParam;
1466 msgf.lParam = *lParam;
1467 if (SendMessageW(GetParent(editor->hWnd), WM_NOTIFY, msgf.nmhdr.idFrom, (LPARAM)&msgf))
1468 return FALSE;
1469 *wParam = msgf.wParam;
1470 *lParam = msgf.lParam;
1471 msgf.wParam = *wParam;
1473 return TRUE;
1476 static BOOL
1477 ME_KeyDown(ME_TextEditor *editor, WORD nKey)
1479 BOOL ctrl_is_down = GetKeyState(VK_CONTROL) & 0x8000;
1480 BOOL shift_is_down = GetKeyState(VK_SHIFT) & 0x8000;
1482 switch (nKey)
1484 case VK_LEFT:
1485 case VK_RIGHT:
1486 case VK_UP:
1487 case VK_DOWN:
1488 case VK_HOME:
1489 case VK_END:
1490 case VK_PRIOR:
1491 case VK_NEXT:
1492 ME_ArrowKey(editor, nKey, shift_is_down, ctrl_is_down);
1493 return TRUE;
1494 case VK_BACK:
1495 case VK_DELETE:
1496 /* FIXME backspace and delete aren't the same, they act different wrt paragraph style of the merged paragraph */
1497 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY)
1498 return FALSE;
1499 if (ME_IsSelection(editor))
1500 ME_DeleteSelection(editor);
1501 else if (nKey == VK_DELETE || ME_ArrowKey(editor, VK_LEFT, FALSE, FALSE))
1502 ME_DeleteTextAtCursor(editor, 1, 1);
1503 else
1504 return TRUE;
1505 ME_CommitUndo(editor);
1506 ME_UpdateRepaint(editor);
1507 ME_SendRequestResize(editor, FALSE);
1508 return TRUE;
1510 default:
1511 if (ctrl_is_down)
1513 if (nKey == 'W')
1515 CHARFORMAT2W chf;
1516 char buf[2048];
1517 chf.cbSize = sizeof(chf);
1519 ME_GetSelectionCharFormat(editor, &chf);
1520 ME_DumpStyleToBuf(&chf, buf);
1521 MessageBoxA(NULL, buf, "Style dump", MB_OK);
1523 if (nKey == 'Q')
1525 ME_CheckCharOffsets(editor);
1529 return FALSE;
1532 static BOOL ME_SetCursor(ME_TextEditor *editor, int x)
1534 if ((GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_SELECTIONBAR) &&
1535 (x < editor->selofs || editor->linesel))
1537 SetCursor(hLeft);
1538 return TRUE;
1540 return FALSE;
1543 static BOOL ME_ShowContextMenu(ME_TextEditor *editor, int x, int y)
1545 CHARRANGE selrange;
1546 HMENU menu;
1547 int seltype = 0;
1548 if(!editor->lpOleCallback)
1549 return FALSE;
1550 ME_GetSelection(editor, &selrange.cpMin, &selrange.cpMax);
1551 if(selrange.cpMin == selrange.cpMax)
1552 seltype |= SEL_EMPTY;
1553 else
1555 /* FIXME: Handle objects */
1556 seltype |= SEL_TEXT;
1557 if(selrange.cpMax-selrange.cpMin > 1)
1558 seltype |= SEL_MULTICHAR;
1560 if(SUCCEEDED(IRichEditOleCallback_GetContextMenu(editor->lpOleCallback, seltype, NULL, &selrange, &menu)))
1562 TrackPopupMenu(menu, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, GetParent(editor->hWnd), NULL);
1563 DestroyMenu(menu);
1565 return TRUE;
1568 ME_TextEditor *ME_MakeEditor(HWND hWnd) {
1569 ME_TextEditor *ed = ALLOC_OBJ(ME_TextEditor);
1570 int i;
1571 ed->hWnd = hWnd;
1572 ed->bEmulateVersion10 = FALSE;
1573 ed->pBuffer = ME_MakeText();
1574 ed->nZoomNumerator = ed->nZoomDenominator = 0;
1575 ME_MakeFirstParagraph(ed);
1576 ed->bCaretShown = FALSE;
1577 ed->nCursors = 4;
1578 ed->pCursors = ALLOC_N_OBJ(ME_Cursor, ed->nCursors);
1579 ed->pCursors[0].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
1580 ed->pCursors[0].nOffset = 0;
1581 ed->pCursors[1].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
1582 ed->pCursors[1].nOffset = 0;
1583 ed->pCursors[2] = ed->pCursors[0];
1584 ed->pCursors[3] = ed->pCursors[1];
1585 ed->nLastTotalLength = ed->nTotalLength = 0;
1586 ed->nHeight = 0;
1587 ed->nUDArrowX = -1;
1588 ed->nSequence = 0;
1589 ed->rgbBackColor = -1;
1590 ed->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
1591 ed->bCaretAtEnd = FALSE;
1592 ed->nEventMask = 0;
1593 ed->nModifyStep = 0;
1594 ed->nTextLimit = TEXT_LIMIT_DEFAULT;
1595 ed->pUndoStack = ed->pRedoStack = ed->pUndoStackBottom = NULL;
1596 ed->nUndoStackSize = 0;
1597 ed->nUndoLimit = STACK_SIZE_DEFAULT;
1598 ed->nUndoMode = umAddToUndo;
1599 ed->nParagraphs = 1;
1600 ed->nLastSelStart = ed->nLastSelEnd = 0;
1601 ed->pLastSelStartPara = ed->pLastSelEndPara = ME_FindItemFwd(ed->pBuffer->pFirst, diParagraph);
1602 ed->bRedraw = TRUE;
1603 ed->bWordWrap = FALSE;
1604 ed->bHideSelection = FALSE;
1605 ed->nInvalidOfs = -1;
1606 ed->pfnWordBreak = NULL;
1607 ed->lpOleCallback = NULL;
1608 ed->mode = TM_RICHTEXT | TM_MULTILEVELUNDO | TM_MULTICODEPAGE;
1609 ed->AutoURLDetect_bEnable = FALSE;
1610 ed->bHaveFocus = FALSE;
1611 GetClientRect(hWnd, &ed->rcFormat);
1612 for (i=0; i<HFONT_CACHE_SIZE; i++)
1614 ed->pFontCache[i].nRefs = 0;
1615 ed->pFontCache[i].nAge = 0;
1616 ed->pFontCache[i].hFont = NULL;
1619 ME_CheckCharOffsets(ed);
1620 if (GetWindowLongW(hWnd, GWL_STYLE) & ES_SELECTIONBAR)
1621 ed->selofs = 16;
1622 else
1623 ed->selofs = 0;
1624 ed->linesel = 0;
1626 if (GetWindowLongW(hWnd, GWL_STYLE) & ES_PASSWORD)
1627 ed->cPasswordMask = '*';
1628 else
1629 ed->cPasswordMask = 0;
1631 return ed;
1634 typedef struct tagME_GlobalDestStruct
1636 HGLOBAL hData;
1637 int nLength;
1638 } ME_GlobalDestStruct;
1640 static DWORD CALLBACK ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1642 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
1643 int i;
1644 WORD *pSrc, *pDest;
1646 cb = cb >> 1;
1647 pDest = (WORD *)lpBuff;
1648 pSrc = (WORD *)GlobalLock(pData->hData);
1649 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
1650 pDest[i] = pSrc[pData->nLength+i];
1652 pData->nLength += i;
1653 *pcb = 2*i;
1654 GlobalUnlock(pData->hData);
1655 return 0;
1658 static DWORD CALLBACK ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1660 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
1661 int i;
1662 BYTE *pSrc, *pDest;
1664 pDest = lpBuff;
1665 pSrc = (BYTE *)GlobalLock(pData->hData);
1666 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
1667 pDest[i] = pSrc[pData->nLength+i];
1669 pData->nLength += i;
1670 *pcb = i;
1671 GlobalUnlock(pData->hData);
1672 return 0;
1676 void ME_DestroyEditor(ME_TextEditor *editor)
1678 ME_DisplayItem *pFirst = editor->pBuffer->pFirst;
1679 ME_DisplayItem *p = pFirst, *pNext = NULL;
1680 int i;
1682 ME_ClearTempStyle(editor);
1683 ME_EmptyUndoStack(editor);
1684 while(p) {
1685 pNext = p->next;
1686 ME_DestroyDisplayItem(p);
1687 p = pNext;
1689 ME_ReleaseStyle(editor->pBuffer->pDefaultStyle);
1690 for (i=0; i<HFONT_CACHE_SIZE; i++)
1692 if (editor->pFontCache[i].hFont)
1693 DeleteObject(editor->pFontCache[i].hFont);
1695 if (editor->rgbBackColor != -1)
1696 DeleteObject(editor->hbrBackground);
1697 if(editor->lpOleCallback)
1698 IUnknown_Release(editor->lpOleCallback);
1700 FREE_OBJ(editor->pBuffer);
1701 FREE_OBJ(editor->pCursors);
1703 FREE_OBJ(editor);
1706 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
1708 TRACE("\n");
1709 switch (fdwReason)
1711 case DLL_PROCESS_ATTACH:
1712 DisableThreadLibraryCalls(hinstDLL);
1713 me_heap = HeapCreate (0, 0x10000, 0);
1714 if (!ME_RegisterEditorClass(hinstDLL)) return FALSE;
1715 hLeft = LoadCursorW(hinstDLL, MAKEINTRESOURCEW(OCR_REVERSE));
1716 LookupInit();
1717 break;
1719 case DLL_PROCESS_DETACH:
1720 UnregisterClassW(RichEdit20W, 0);
1721 UnregisterClassW(RichEdit50W, 0);
1722 UnregisterClassA("RichEdit20A", 0);
1723 UnregisterClassA("RichEdit50A", 0);
1724 if (ME_ListBoxRegistered)
1725 UnregisterClassW(REListBox20W, 0);
1726 if (ME_ComboBoxRegistered)
1727 UnregisterClassW(REComboBox20W, 0);
1728 LookupCleanup();
1729 HeapDestroy (me_heap);
1730 me_heap = NULL;
1731 break;
1733 return TRUE;
1737 #define UNSUPPORTED_MSG(e) \
1738 case e: \
1739 FIXME(#e ": stub\n"); \
1740 return DefWindowProcW(hWnd, msg, wParam, lParam);
1742 static const char * const edit_messages[] = {
1743 "EM_GETSEL",
1744 "EM_SETSEL",
1745 "EM_GETRECT",
1746 "EM_SETRECT",
1747 "EM_SETRECTNP",
1748 "EM_SCROLL",
1749 "EM_LINESCROLL",
1750 "EM_SCROLLCARET",
1751 "EM_GETMODIFY",
1752 "EM_SETMODIFY",
1753 "EM_GETLINECOUNT",
1754 "EM_LINEINDEX",
1755 "EM_SETHANDLE",
1756 "EM_GETHANDLE",
1757 "EM_GETTHUMB",
1758 "EM_UNKNOWN_BF",
1759 "EM_UNKNOWN_C0",
1760 "EM_LINELENGTH",
1761 "EM_REPLACESEL",
1762 "EM_UNKNOWN_C3",
1763 "EM_GETLINE",
1764 "EM_LIMITTEXT",
1765 "EM_CANUNDO",
1766 "EM_UNDO",
1767 "EM_FMTLINES",
1768 "EM_LINEFROMCHAR",
1769 "EM_UNKNOWN_CA",
1770 "EM_SETTABSTOPS",
1771 "EM_SETPASSWORDCHAR",
1772 "EM_EMPTYUNDOBUFFER",
1773 "EM_GETFIRSTVISIBLELINE",
1774 "EM_SETREADONLY",
1775 "EM_SETWORDBREAKPROC",
1776 "EM_GETWORDBREAKPROC",
1777 "EM_GETPASSWORDCHAR",
1778 "EM_SETMARGINS",
1779 "EM_GETMARGINS",
1780 "EM_GETLIMITTEXT",
1781 "EM_POSFROMCHAR",
1782 "EM_CHARFROMPOS"
1785 static const char * const richedit_messages[] = {
1786 "EM_CANPASTE",
1787 "EM_DISPLAYBAND",
1788 "EM_EXGETSEL",
1789 "EM_EXLIMITTEXT",
1790 "EM_EXLINEFROMCHAR",
1791 "EM_EXSETSEL",
1792 "EM_FINDTEXT",
1793 "EM_FORMATRANGE",
1794 "EM_GETCHARFORMAT",
1795 "EM_GETEVENTMASK",
1796 "EM_GETOLEINTERFACE",
1797 "EM_GETPARAFORMAT",
1798 "EM_GETSELTEXT",
1799 "EM_HIDESELECTION",
1800 "EM_PASTESPECIAL",
1801 "EM_REQUESTRESIZE",
1802 "EM_SELECTIONTYPE",
1803 "EM_SETBKGNDCOLOR",
1804 "EM_SETCHARFORMAT",
1805 "EM_SETEVENTMASK",
1806 "EM_SETOLECALLBACK",
1807 "EM_SETPARAFORMAT",
1808 "EM_SETTARGETDEVICE",
1809 "EM_STREAMIN",
1810 "EM_STREAMOUT",
1811 "EM_GETTEXTRANGE",
1812 "EM_FINDWORDBREAK",
1813 "EM_SETOPTIONS",
1814 "EM_GETOPTIONS",
1815 "EM_FINDTEXTEX",
1816 "EM_GETWORDBREAKPROCEX",
1817 "EM_SETWORDBREAKPROCEX",
1818 "EM_SETUNDOLIMIT",
1819 "EM_UNKNOWN_USER_83",
1820 "EM_REDO",
1821 "EM_CANREDO",
1822 "EM_GETUNDONAME",
1823 "EM_GETREDONAME",
1824 "EM_STOPGROUPTYPING",
1825 "EM_SETTEXTMODE",
1826 "EM_GETTEXTMODE",
1827 "EM_AUTOURLDETECT",
1828 "EM_GETAUTOURLDETECT",
1829 "EM_SETPALETTE",
1830 "EM_GETTEXTEX",
1831 "EM_GETTEXTLENGTHEX",
1832 "EM_SHOWSCROLLBAR",
1833 "EM_SETTEXTEX",
1834 "EM_UNKNOWN_USER_98",
1835 "EM_UNKNOWN_USER_99",
1836 "EM_SETPUNCTUATION",
1837 "EM_GETPUNCTUATION",
1838 "EM_SETWORDWRAPMODE",
1839 "EM_GETWORDWRAPMODE",
1840 "EM_SETIMECOLOR",
1841 "EM_GETIMECOLOR",
1842 "EM_SETIMEOPTIONS",
1843 "EM_GETIMEOPTIONS",
1844 "EM_CONVPOSITION",
1845 "EM_UNKNOWN_USER_109",
1846 "EM_UNKNOWN_USER_110",
1847 "EM_UNKNOWN_USER_111",
1848 "EM_UNKNOWN_USER_112",
1849 "EM_UNKNOWN_USER_113",
1850 "EM_UNKNOWN_USER_114",
1851 "EM_UNKNOWN_USER_115",
1852 "EM_UNKNOWN_USER_116",
1853 "EM_UNKNOWN_USER_117",
1854 "EM_UNKNOWN_USER_118",
1855 "EM_UNKNOWN_USER_119",
1856 "EM_SETLANGOPTIONS",
1857 "EM_GETLANGOPTIONS",
1858 "EM_GETIMECOMPMODE",
1859 "EM_FINDTEXTW",
1860 "EM_FINDTEXTEXW",
1861 "EM_RECONVERSION",
1862 "EM_SETIMEMODEBIAS",
1863 "EM_GETIMEMODEBIAS"
1866 static const char *
1867 get_msg_name(UINT msg)
1869 if (msg >= EM_GETSEL && msg <= EM_CHARFROMPOS)
1870 return edit_messages[msg - EM_GETSEL];
1871 if (msg >= EM_CANPASTE && msg <= EM_GETIMEMODEBIAS)
1872 return richedit_messages[msg - EM_CANPASTE];
1873 return "";
1876 static LRESULT RichEditWndProc_common(HWND hWnd, UINT msg, WPARAM wParam,
1877 LPARAM lParam, BOOL unicode)
1879 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongPtrW(hWnd, 0);
1881 TRACE("hwnd %p msg %04x (%s) %lx %lx, unicode %d\n",
1882 hWnd, msg, get_msg_name(msg), wParam, lParam, unicode);
1884 if (!editor && msg != WM_NCCREATE && msg != WM_NCDESTROY) {
1885 ERR("called with invalid hWnd %p - application bug?\n", hWnd);
1886 return 0;
1889 switch(msg) {
1891 UNSUPPORTED_MSG(EM_DISPLAYBAND)
1892 UNSUPPORTED_MSG(EM_FINDWORDBREAK)
1893 UNSUPPORTED_MSG(EM_FMTLINES)
1894 UNSUPPORTED_MSG(EM_FORMATRANGE)
1895 UNSUPPORTED_MSG(EM_GETBIDIOPTIONS)
1896 UNSUPPORTED_MSG(EM_GETEDITSTYLE)
1897 UNSUPPORTED_MSG(EM_GETIMECOMPMODE)
1898 /* UNSUPPORTED_MSG(EM_GETIMESTATUS) missing in Wine headers */
1899 UNSUPPORTED_MSG(EM_GETLANGOPTIONS)
1900 /* UNSUPPORTED_MSG(EM_GETOLEINTERFACE) separate stub */
1901 UNSUPPORTED_MSG(EM_GETREDONAME)
1902 UNSUPPORTED_MSG(EM_GETTEXTMODE)
1903 UNSUPPORTED_MSG(EM_GETTYPOGRAPHYOPTIONS)
1904 UNSUPPORTED_MSG(EM_GETUNDONAME)
1905 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX)
1906 UNSUPPORTED_MSG(EM_PASTESPECIAL)
1907 UNSUPPORTED_MSG(EM_SELECTIONTYPE)
1908 UNSUPPORTED_MSG(EM_SETBIDIOPTIONS)
1909 UNSUPPORTED_MSG(EM_SETEDITSTYLE)
1910 UNSUPPORTED_MSG(EM_SETFONTSIZE)
1911 UNSUPPORTED_MSG(EM_SETLANGOPTIONS)
1912 UNSUPPORTED_MSG(EM_SETPALETTE)
1913 UNSUPPORTED_MSG(EM_SETTABSTOPS)
1914 UNSUPPORTED_MSG(EM_SETTYPOGRAPHYOPTIONS)
1915 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX)
1916 UNSUPPORTED_MSG(WM_STYLECHANGING)
1917 UNSUPPORTED_MSG(WM_STYLECHANGED)
1918 /* UNSUPPORTED_MSG(WM_UNICHAR) FIXME missing in Wine headers */
1920 /* Messages specific to Richedit controls */
1922 case EM_STREAMIN:
1923 return ME_StreamIn(editor, wParam, (EDITSTREAM*)lParam);
1924 case EM_STREAMOUT:
1925 return ME_StreamOut(editor, wParam, (EDITSTREAM *)lParam);
1926 case WM_GETDLGCODE:
1928 UINT code = DLGC_WANTCHARS|DLGC_WANTARROWS;
1929 if(lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
1931 int vk = (int)((LPMSG)lParam)->wParam;
1932 /* if style says we want return key */
1933 if((vk == VK_RETURN) && (GetWindowLongW(hWnd, GWL_STYLE) & ES_WANTRETURN))
1935 code |= DLGC_WANTMESSAGE;
1937 /* we always handle ctrl-tab */
1938 if((vk == VK_TAB) && (GetKeyState(VK_CONTROL) & 0x8000))
1940 code |= DLGC_WANTMESSAGE;
1943 return code;
1945 case WM_NCCREATE:
1947 CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
1948 TRACE("WM_NCCREATE: style 0x%08x\n", pcs->style);
1949 editor = ME_MakeEditor(hWnd);
1950 SetWindowLongPtrW(hWnd, 0, (LONG_PTR)editor);
1951 return TRUE;
1953 case EM_EMPTYUNDOBUFFER:
1954 ME_EmptyUndoStack(editor);
1955 return 0;
1956 case EM_GETSEL:
1958 /* Note: wParam/lParam can be NULL */
1959 UINT from, to;
1960 PUINT pfrom = wParam ? (PUINT)wParam : &from;
1961 PUINT pto = lParam ? (PUINT)lParam : &to;
1962 ME_GetSelection(editor, (int *)pfrom, (int *)pto);
1963 if ((*pfrom|*pto) & 0xFFFF0000)
1964 return -1;
1965 return MAKELONG(*pfrom,*pto);
1967 case EM_EXGETSEL:
1969 CHARRANGE *pRange = (CHARRANGE *)lParam;
1970 ME_GetSelection(editor, &pRange->cpMin, &pRange->cpMax);
1971 TRACE("EM_EXGETSEL = (%d,%d)\n", pRange->cpMin, pRange->cpMax);
1972 return 0;
1974 case EM_SETUNDOLIMIT:
1976 if ((int)wParam < 0)
1977 editor->nUndoLimit = STACK_SIZE_DEFAULT;
1978 else
1979 editor->nUndoLimit = min(wParam, STACK_SIZE_MAX);
1980 /* Setting a max stack size keeps wine from getting killed
1981 for hogging memory. Windows allocates all this memory at once, so
1982 no program would realistically set a value above our maximum. */
1983 return editor->nUndoLimit;
1985 case EM_CANUNDO:
1986 return editor->pUndoStack != NULL;
1987 case EM_CANREDO:
1988 return editor->pRedoStack != NULL;
1989 case WM_UNDO: /* FIXME: actually not the same */
1990 case EM_UNDO:
1991 ME_Undo(editor);
1992 return 0;
1993 case EM_REDO:
1994 ME_Redo(editor);
1995 return 0;
1996 case EM_GETOPTIONS:
1998 /* these flags are equivalent to the ES_* counterparts */
1999 DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
2000 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN;
2001 DWORD settings = GetWindowLongW(hWnd, GWL_STYLE) & mask;
2003 return settings;
2005 case EM_SETOPTIONS:
2007 /* these flags are equivalent to ES_* counterparts
2008 * ECO_READONLY is already implemented in the code, only requires
2009 * setting the bit to work
2011 DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
2012 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN | ECO_SELECTIONBAR;
2013 DWORD raw = GetWindowLongW(hWnd, GWL_STYLE);
2014 DWORD settings = mask & raw;
2016 switch(wParam)
2018 case ECOOP_SET:
2019 settings = lParam;
2020 break;
2021 case ECOOP_OR:
2022 settings |= lParam;
2023 break;
2024 case ECOOP_AND:
2025 settings &= lParam;
2026 break;
2027 case ECOOP_XOR:
2028 settings ^= lParam;
2030 SetWindowLongW(hWnd, GWL_STYLE, (raw & ~mask) | (settings & mask));
2032 if (settings & ECO_AUTOWORDSELECTION)
2033 FIXME("ECO_AUTOWORDSELECTION not implemented yet!\n");
2034 if (settings & ECO_SELECTIONBAR)
2035 editor->selofs = 16;
2036 else
2037 editor->selofs = 0;
2038 ME_WrapMarkedParagraphs(editor);
2040 if (settings & ECO_VERTICAL)
2041 FIXME("ECO_VERTICAL not implemented yet!\n");
2042 if (settings & ECO_AUTOHSCROLL)
2043 FIXME("ECO_AUTOHSCROLL not implemented yet!\n");
2044 if (settings & ECO_AUTOVSCROLL)
2045 FIXME("ECO_AUTOVSCROLL not implemented yet!\n");
2046 if (settings & ECO_NOHIDESEL)
2047 FIXME("ECO_NOHIDESEL not implemented yet!\n");
2048 if (settings & ECO_WANTRETURN)
2049 FIXME("ECO_WANTRETURN not implemented yet!\n");
2051 return settings;
2053 case EM_SETSEL:
2055 ME_InvalidateSelection(editor);
2056 ME_SetSelection(editor, wParam, lParam);
2057 ME_InvalidateSelection(editor);
2058 ME_SendSelChange(editor);
2059 return 0;
2061 case EM_SETSCROLLPOS:
2063 POINT *point = (POINT *)lParam;
2064 ME_ScrollAbs(editor, point->y);
2065 return 0;
2067 case EM_AUTOURLDETECT:
2069 if (wParam==1 || wParam ==0)
2071 editor->AutoURLDetect_bEnable = (BOOL)wParam;
2072 return 0;
2074 return E_INVALIDARG;
2076 case EM_GETAUTOURLDETECT:
2078 return editor->AutoURLDetect_bEnable;
2080 case EM_EXSETSEL:
2082 int end;
2083 CHARRANGE range = *(CHARRANGE *)lParam;
2085 TRACE("EM_EXSETSEL (%d,%d)\n", range.cpMin, range.cpMax);
2087 ME_InvalidateSelection(editor);
2088 end = ME_SetSelection(editor, range.cpMin, range.cpMax);
2089 ME_InvalidateSelection(editor);
2090 ME_SendSelChange(editor);
2092 return end;
2094 case EM_SHOWSCROLLBAR:
2096 ShowScrollBar(editor->hWnd, wParam, lParam);
2097 return 0;
2099 case EM_SETTEXTEX:
2101 LPWSTR wszText;
2102 SETTEXTEX *pStruct = (SETTEXTEX *)wParam;
2103 size_t len;
2104 int from, to;
2105 ME_Style *style;
2106 int oldModify = editor->nModifyStep;
2108 if (!pStruct) return 0;
2110 TRACE("EM_SETTEXTEX - %s, flags %d, cp %d\n",
2111 pStruct->codepage == 1200 ? debugstr_w((LPCWSTR)lParam) : debugstr_a((LPCSTR)lParam),
2112 pStruct->flags, pStruct->codepage);
2114 /* FIXME: make use of pStruct->codepage in the to unicode translation */
2115 wszText = lParam ? ME_ToUnicode(pStruct->codepage == 1200, (void *)lParam) : NULL;
2116 len = wszText ? lstrlenW(wszText) : 0;
2118 /* FIXME: this should support RTF strings too, according to MSDN */
2119 if (pStruct->flags & ST_SELECTION) {
2120 ME_GetSelection(editor, &from, &to);
2121 style = ME_GetSelectionInsertStyle(editor);
2122 ME_InternalDeleteText(editor, from, to - from);
2123 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
2124 ME_ReleaseStyle(style);
2126 else {
2127 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
2128 ME_InsertTextFromCursor(editor, 0, wszText, len, editor->pBuffer->pDefaultStyle);
2129 len = 1;
2131 ME_CommitUndo(editor);
2132 if (!(pStruct->flags & ST_KEEPUNDO))
2134 editor->nModifyStep = oldModify;
2135 ME_EmptyUndoStack(editor);
2137 ME_UpdateRepaint(editor);
2138 return len;
2140 case EM_SETBKGNDCOLOR:
2142 LRESULT lColor;
2143 if (editor->rgbBackColor != -1) {
2144 DeleteObject(editor->hbrBackground);
2145 lColor = editor->rgbBackColor;
2147 else lColor = GetSysColor(COLOR_WINDOW);
2149 if (wParam)
2151 editor->rgbBackColor = -1;
2152 editor->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
2154 else
2156 editor->rgbBackColor = lParam;
2157 editor->hbrBackground = CreateSolidBrush(editor->rgbBackColor);
2159 if (editor->bRedraw)
2161 InvalidateRect(hWnd, NULL, TRUE);
2162 UpdateWindow(hWnd);
2164 return lColor;
2166 case EM_GETMODIFY:
2167 return editor->nModifyStep == 0 ? 0 : 1;
2168 case EM_SETMODIFY:
2170 if (wParam)
2171 editor->nModifyStep = 1;
2172 else
2173 editor->nModifyStep = 0;
2175 return 0;
2177 case EM_SETREADONLY:
2179 long nStyle = GetWindowLongW(hWnd, GWL_STYLE);
2180 if (wParam)
2181 nStyle |= ES_READONLY;
2182 else
2183 nStyle &= ~ES_READONLY;
2184 SetWindowLongW(hWnd, GWL_STYLE, nStyle);
2185 return 0;
2187 case EM_SETEVENTMASK:
2189 DWORD nOldMask = editor->nEventMask;
2191 editor->nEventMask = lParam;
2192 return nOldMask;
2194 case EM_GETEVENTMASK:
2195 return editor->nEventMask;
2196 case EM_SETCHARFORMAT:
2198 CHARFORMAT2W buf, *p;
2199 BOOL bRepaint = TRUE;
2200 p = ME_ToCF2W(&buf, (CHARFORMAT2W *)lParam);
2201 if (p == NULL) return 0;
2202 if (!wParam)
2203 ME_SetDefaultCharFormat(editor, p);
2204 else if (wParam == (SCF_WORD | SCF_SELECTION)) {
2205 FIXME("EM_SETCHARFORMAT: word selection not supported\n");
2206 return 0;
2207 } else if (wParam == SCF_ALL) {
2208 if (editor->mode & TM_PLAINTEXT)
2209 ME_SetDefaultCharFormat(editor, p);
2210 else
2211 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), p);
2212 } else if (editor->mode & TM_PLAINTEXT) {
2213 return 0;
2214 } else {
2215 int from, to;
2216 ME_GetSelection(editor, &from, &to);
2217 bRepaint = (from != to);
2218 ME_SetSelectionCharFormat(editor, p);
2220 editor->nModifyStep = 1;
2221 ME_CommitUndo(editor);
2222 if (bRepaint)
2223 ME_RewrapRepaint(editor);
2224 return 1;
2226 case EM_GETCHARFORMAT:
2228 CHARFORMAT2W tmp, *dst = (CHARFORMAT2W *)lParam;
2229 if (dst->cbSize != sizeof(CHARFORMATA) &&
2230 dst->cbSize != sizeof(CHARFORMATW) &&
2231 dst->cbSize != sizeof(CHARFORMAT2A) &&
2232 dst->cbSize != sizeof(CHARFORMAT2W))
2233 return 0;
2234 tmp.cbSize = sizeof(tmp);
2235 if (!wParam)
2236 ME_GetDefaultCharFormat(editor, &tmp);
2237 else
2238 ME_GetSelectionCharFormat(editor, &tmp);
2239 ME_CopyToCFAny(dst, &tmp);
2240 return tmp.dwMask;
2242 case EM_SETPARAFORMAT:
2243 ME_SetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
2244 ME_RewrapRepaint(editor);
2245 ME_CommitUndo(editor);
2246 return 0;
2247 case EM_GETPARAFORMAT:
2248 ME_GetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
2249 return 0;
2250 case EM_GETFIRSTVISIBLELINE:
2252 ME_DisplayItem *p = editor->pBuffer->pFirst;
2253 int y = ME_GetYScrollPos(editor);
2254 int ypara = 0;
2255 int count = 0;
2256 int ystart, yend;
2257 while(p) {
2258 p = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
2259 if (p->type == diTextEnd)
2260 break;
2261 if (p->type == diParagraph) {
2262 ypara = p->member.para.nYPos;
2263 continue;
2265 ystart = ypara + p->member.row.nYPos;
2266 yend = ystart + p->member.row.nHeight;
2267 if (y < yend) {
2268 break;
2270 count++;
2272 return count;
2274 case EM_HIDESELECTION:
2276 editor->bHideSelection = (wParam != 0);
2277 ME_InvalidateSelection(editor);
2278 return 0;
2280 case EM_LINESCROLL:
2282 ME_ScrollDown(editor, lParam * 8); /* FIXME follow the original */
2283 return TRUE; /* Should return false if a single line richedit control */
2285 case WM_CLEAR:
2287 int from, to;
2288 ME_GetSelection(editor, &from, &to);
2289 ME_InternalDeleteText(editor, from, to-from);
2290 ME_CommitUndo(editor);
2291 ME_UpdateRepaint(editor);
2292 return 0;
2294 case EM_REPLACESEL:
2296 int from, to;
2297 ME_Style *style;
2298 LPWSTR wszText = lParam ? ME_ToUnicode(unicode, (void *)lParam) : NULL;
2299 size_t len = wszText ? lstrlenW(wszText) : 0;
2300 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText));
2302 ME_GetSelection(editor, &from, &to);
2303 style = ME_GetSelectionInsertStyle(editor);
2304 ME_InternalDeleteText(editor, from, to-from);
2305 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
2306 ME_ReleaseStyle(style);
2307 /* drop temporary style if line end */
2309 * FIXME question: does abc\n mean: put abc,
2310 * clear temp style, put \n? (would require a change)
2312 if (len>0 && wszText[len-1] == '\n')
2313 ME_ClearTempStyle(editor);
2314 ME_EndToUnicode(unicode, wszText);
2315 ME_CommitUndo(editor);
2316 if (!wParam)
2317 ME_EmptyUndoStack(editor);
2318 ME_UpdateRepaint(editor);
2319 return len;
2321 case EM_SCROLLCARET:
2323 int top, bottom; /* row's edges relative to document top */
2324 int nPos;
2325 ME_DisplayItem *para, *row;
2327 nPos = ME_GetYScrollPos(editor);
2328 row = ME_RowStart(editor->pCursors[0].pRun);
2329 para = ME_GetParagraph(row);
2330 top = para->member.para.nYPos + row->member.row.nYPos;
2331 bottom = top + row->member.row.nHeight;
2333 if (top < nPos) /* caret above window */
2334 ME_ScrollAbs(editor, top);
2335 else if (nPos + editor->sizeWindow.cy < bottom) /*below*/
2336 ME_ScrollAbs(editor, bottom - editor->sizeWindow.cy);
2337 return 0;
2339 case WM_SETFONT:
2341 LOGFONTW lf;
2342 CHARFORMAT2W fmt;
2343 HDC hDC;
2344 BOOL bRepaint = LOWORD(lParam);
2346 if (!wParam)
2347 wParam = (WPARAM)GetStockObject(SYSTEM_FONT);
2348 GetObjectW((HGDIOBJ)wParam, sizeof(LOGFONTW), &lf);
2349 hDC = GetDC(hWnd);
2350 ME_CharFormatFromLogFont(hDC, &lf, &fmt);
2351 ReleaseDC(hWnd, hDC);
2352 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), &fmt);
2353 ME_SetDefaultCharFormat(editor, &fmt);
2355 ME_CommitUndo(editor);
2356 if (bRepaint)
2357 ME_RewrapRepaint(editor);
2358 return 0;
2360 case WM_SETTEXT:
2362 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
2363 if (lParam)
2365 TRACE("WM_SETTEXT lParam==%lx\n",lParam);
2366 if (!unicode && !strncmp((char *)lParam, "{\\rtf", 5))
2368 /* Undocumented: WM_SETTEXT supports RTF text */
2369 ME_StreamInRTFString(editor, 0, (char *)lParam);
2371 else
2373 LPWSTR wszText = ME_ToUnicode(unicode, (void *)lParam);
2374 TRACE("WM_SETTEXT - %s\n", debugstr_w(wszText)); /* debugstr_w() */
2375 if (lstrlenW(wszText) > 0)
2377 int len = -1;
2379 /* uses default style! */
2380 if (!(GetWindowLongW(hWnd, GWL_STYLE) & ES_MULTILINE))
2382 WCHAR * p;
2384 p = wszText;
2385 while (*p != '\0' && *p != '\r' && *p != '\n') p++;
2386 len = p - wszText;
2388 ME_InsertTextFromCursor(editor, 0, wszText, len, editor->pBuffer->pDefaultStyle);
2390 ME_EndToUnicode(unicode, wszText);
2393 else
2394 TRACE("WM_SETTEXT - NULL\n");
2395 ME_CommitUndo(editor);
2396 ME_EmptyUndoStack(editor);
2397 ME_SetSelection(editor, 0, 0);
2398 editor->nModifyStep = 0;
2399 ME_UpdateRepaint(editor);
2400 return 1;
2402 case EM_CANPASTE:
2404 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
2405 if (IsClipboardFormatAvailable(nRTFFormat))
2406 return TRUE;
2407 if (IsClipboardFormatAvailable(CF_UNICODETEXT))
2408 return TRUE;
2409 return FALSE;
2411 case WM_PASTE:
2413 DWORD dwFormat = 0;
2414 EDITSTREAM es;
2415 ME_GlobalDestStruct gds;
2416 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
2417 UINT cf = 0;
2419 if (IsClipboardFormatAvailable(nRTFFormat))
2420 cf = nRTFFormat, dwFormat = SF_RTF;
2421 else if (IsClipboardFormatAvailable(CF_UNICODETEXT))
2422 cf = CF_UNICODETEXT, dwFormat = SF_TEXT|SF_UNICODE;
2423 else
2424 return 0;
2426 if (!OpenClipboard(hWnd))
2427 return 0;
2428 gds.hData = GetClipboardData(cf);
2429 gds.nLength = 0;
2430 es.dwCookie = (DWORD)&gds;
2431 es.pfnCallback = dwFormat == SF_RTF ? ME_ReadFromHGLOBALRTF : ME_ReadFromHGLOBALUnicode;
2432 ME_StreamIn(editor, dwFormat|SFF_SELECTION, &es);
2434 CloseClipboard();
2435 return 0;
2437 case WM_CUT:
2438 case WM_COPY:
2440 LPDATAOBJECT dataObj = NULL;
2441 CHARRANGE range;
2442 HRESULT hr = S_OK;
2444 if (editor->cPasswordMask)
2445 return 0; /* Copying or Cutting masked text isn't allowed */
2447 ME_GetSelection(editor, &range.cpMin, &range.cpMax);
2448 if(editor->lpOleCallback)
2449 hr = IRichEditOleCallback_GetClipboardData(editor->lpOleCallback, &range, RECO_COPY, &dataObj);
2450 if(FAILED(hr) || !dataObj)
2451 hr = ME_GetDataObject(editor, &range, &dataObj);
2452 if(SUCCEEDED(hr)) {
2453 hr = OleSetClipboard(dataObj);
2454 IDataObject_Release(dataObj);
2456 if (SUCCEEDED(hr) && msg == WM_CUT)
2458 ME_InternalDeleteText(editor, range.cpMin, range.cpMax-range.cpMin);
2459 ME_CommitUndo(editor);
2460 ME_UpdateRepaint(editor);
2462 return 0;
2464 case WM_GETTEXTLENGTH:
2466 GETTEXTLENGTHEX how;
2468 how.flags = GTL_CLOSE | (editor->bEmulateVersion10 ? 0 : GTL_USECRLF) | GTL_NUMCHARS;
2469 how.codepage = unicode ? 1200 : CP_ACP;
2470 return ME_GetTextLengthEx(editor, &how);
2472 case EM_GETTEXTLENGTHEX:
2473 return ME_GetTextLengthEx(editor, (GETTEXTLENGTHEX *)wParam);
2474 case WM_GETTEXT:
2476 GETTEXTEX ex;
2477 LRESULT rc;
2478 LPSTR bufferA = NULL;
2479 LPWSTR bufferW = NULL;
2481 if (unicode)
2482 bufferW = heap_alloc((wParam + 2) * sizeof(WCHAR));
2483 else
2484 bufferA = heap_alloc(wParam + 2);
2486 ex.cb = (wParam + 2) * (unicode ? sizeof(WCHAR) : sizeof(CHAR));
2487 ex.flags = GT_USECRLF;
2488 ex.codepage = unicode ? 1200 : CP_ACP;
2489 ex.lpDefaultChar = NULL;
2490 ex.lpUsedDefChar = NULL;
2491 rc = RichEditWndProc_common(hWnd, EM_GETTEXTEX, (WPARAM)&ex, unicode ? (LPARAM)bufferW : (LPARAM)bufferA, unicode);
2493 if (unicode)
2495 memcpy((LPWSTR)lParam, bufferW, wParam * sizeof(WCHAR));
2496 if (lstrlenW(bufferW) >= wParam) rc = 0;
2498 else
2500 memcpy((LPSTR)lParam, bufferA, wParam);
2501 if (strlen(bufferA) >= wParam) rc = 0;
2503 heap_free(bufferA);
2504 heap_free(bufferW);
2505 return rc;
2507 case EM_GETTEXTEX:
2509 GETTEXTEX *ex = (GETTEXTEX*)wParam;
2510 int nStart, nCount; /* in chars */
2512 if (ex->flags & ~(GT_SELECTION | GT_USECRLF))
2513 FIXME("GETTEXTEX flags 0x%08x not supported\n", ex->flags & ~(GT_SELECTION | GT_USECRLF));
2515 if (ex->flags & GT_SELECTION)
2517 ME_GetSelection(editor, &nStart, &nCount);
2518 nCount -= nStart;
2520 else
2522 nStart = 0;
2523 nCount = 0x7fffffff;
2525 if (ex->codepage == 1200)
2527 nCount = min(nCount, ex->cb / sizeof(WCHAR) - 1);
2528 return ME_GetTextW(editor, (LPWSTR)lParam, nStart, nCount, ex->flags & GT_USECRLF);
2530 else
2532 /* potentially each char may be a CR, why calculate the exact value with O(N) when
2533 we can just take a bigger buffer? :) */
2534 int crlfmul = (ex->flags & GT_USECRLF) ? 2 : 1;
2535 LPWSTR buffer;
2536 DWORD buflen = ex->cb;
2537 LRESULT rc;
2538 DWORD flags = 0;
2540 nCount = min(nCount, ex->cb - 1);
2541 buffer = heap_alloc((crlfmul*nCount + 1) * sizeof(WCHAR));
2543 buflen = ME_GetTextW(editor, buffer, nStart, nCount, ex->flags & GT_USECRLF);
2544 rc = WideCharToMultiByte(ex->codepage, flags, buffer, -1, (LPSTR)lParam, ex->cb, ex->lpDefaultChar, ex->lpUsedDefChar);
2545 if (rc) rc--; /* do not count 0 terminator */
2547 heap_free(buffer);
2548 return rc;
2551 case EM_GETSELTEXT:
2553 int from, to;
2554 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
2555 ME_GetSelection(editor, &from, &to);
2556 tr.chrg.cpMin = from;
2557 tr.chrg.cpMax = to;
2558 tr.lpstrText = (WCHAR *)lParam;
2559 return RichEditWndProc_common(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr, unicode);
2561 case EM_GETSCROLLPOS:
2563 POINT *point = (POINT *)lParam;
2564 point->x = 0; /* FIXME side scrolling not implemented */
2565 point->y = ME_GetYScrollPos(editor);
2566 return 1;
2568 case EM_GETTEXTRANGE:
2570 TEXTRANGEW *rng = (TEXTRANGEW *)lParam;
2571 TRACE("EM_GETTEXTRANGE min=%d max=%d unicode=%d emul1.0=%d length=%d\n",
2572 rng->chrg.cpMin, rng->chrg.cpMax, unicode,
2573 editor->bEmulateVersion10, ME_GetTextLength(editor));
2574 if (unicode)
2575 return ME_GetTextW(editor, rng->lpstrText, rng->chrg.cpMin, rng->chrg.cpMax-rng->chrg.cpMin, editor->bEmulateVersion10);
2576 else
2578 int nLen = rng->chrg.cpMax-rng->chrg.cpMin;
2579 WCHAR *p = ALLOC_N_OBJ(WCHAR, nLen+1);
2580 int nChars = ME_GetTextW(editor, p, rng->chrg.cpMin, nLen, editor->bEmulateVersion10);
2581 /* FIXME this is a potential security hole (buffer overrun)
2582 if you know more about wchar->mbyte conversion please explain
2584 WideCharToMultiByte(CP_ACP, 0, p, nChars+1, (char *)rng->lpstrText, nLen+1, NULL, NULL);
2585 FREE_OBJ(p);
2586 return nChars;
2589 case EM_GETLINE:
2591 ME_DisplayItem *run;
2592 const unsigned int nMaxChars = *(WORD *) lParam;
2593 unsigned int nEndChars, nCharsLeft = nMaxChars;
2594 char *dest = (char *) lParam;
2595 /* rich text editor 1.0 uses \r\n for line end, 2.0 uses just \r;
2596 we need to know how if we have the extra \n or not */
2597 int nLF = editor->bEmulateVersion10;
2599 TRACE("EM_GETLINE: row=%d, nMaxChars=%d (%s)\n", (int) wParam, nMaxChars,
2600 unicode ? "Unicode" : "Ansi");
2602 run = ME_FindRowWithNumber(editor, wParam);
2603 if (run == NULL)
2604 return 0;
2606 while (nCharsLeft && (run = ME_FindItemFwd(run, diRunOrStartRow))
2607 && !(run->member.run.nFlags & MERF_ENDPARA))
2609 unsigned int nCopy;
2610 ME_String *strText;
2611 if (run->type != diRun)
2612 break;
2613 strText = run->member.run.strText;
2614 nCopy = min(nCharsLeft, strText->nLen);
2616 if (unicode)
2617 lstrcpynW((LPWSTR) dest, strText->szData, nCopy);
2618 else
2619 nCopy = WideCharToMultiByte(CP_ACP, 0, strText->szData, nCopy, dest,
2620 nCharsLeft, NULL, NULL);
2621 dest += nCopy * (unicode ? sizeof(WCHAR) : 1);
2622 nCharsLeft -= nCopy;
2625 /* append \r\0 (or \r\n\0 in 1.0), space allowing */
2626 nEndChars = min(nCharsLeft, 2 + nLF);
2627 nCharsLeft -= nEndChars;
2628 if (unicode)
2630 const WCHAR src[] = {'\r', '\0'};
2631 const WCHAR src10[] = {'\r', '\n', '\0'};
2632 lstrcpynW((LPWSTR) dest, nLF ? src10 : src, nEndChars);
2634 else
2635 lstrcpynA(dest, nLF ? "\r\n" : "\r", nEndChars);
2637 TRACE("EM_GETLINE: got %u bytes\n", nMaxChars - nCharsLeft);
2639 if (nEndChars == 2 + nLF)
2640 return nMaxChars - nCharsLeft - 1; /* don't count \0 */
2641 else
2642 return nMaxChars - nCharsLeft;
2644 case EM_GETLINECOUNT:
2646 ME_DisplayItem *item = editor->pBuffer->pFirst->next;
2647 int nRows = 0;
2649 while (item != editor->pBuffer->pLast)
2651 assert(item->type == diParagraph);
2652 nRows += item->member.para.nRows;
2653 item = item->member.para.next_para;
2655 TRACE("EM_GETLINECOUNT: nRows==%d\n", nRows);
2656 return max(1, nRows);
2658 case EM_LINEFROMCHAR:
2660 if (wParam == -1)
2661 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(editor, 1));
2662 else
2663 return ME_RowNumberFromCharOfs(editor, wParam);
2665 case EM_EXLINEFROMCHAR:
2667 if (lParam == -1)
2668 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(editor,1));
2669 else
2670 return ME_RowNumberFromCharOfs(editor, lParam);
2672 case EM_LINEINDEX:
2674 ME_DisplayItem *item, *para;
2675 int nCharOfs;
2677 if (wParam == -1)
2678 item = ME_FindItemBack(editor->pCursors[0].pRun, diStartRow);
2679 else
2680 item = ME_FindRowWithNumber(editor, wParam);
2681 if (!item)
2682 return -1;
2683 para = ME_GetParagraph(item);
2684 item = ME_FindItemFwd(item, diRun);
2685 nCharOfs = para->member.para.nCharOfs + item->member.run.nCharOfs;
2686 TRACE("EM_LINEINDEX: nCharOfs==%d\n", nCharOfs);
2687 return nCharOfs;
2689 case EM_LINELENGTH:
2691 ME_DisplayItem *item, *item_end;
2692 int nChars = 0, nThisLineOfs = 0, nNextLineOfs = 0;
2694 if (wParam > ME_GetTextLength(editor))
2695 return 0;
2696 if (wParam == -1)
2698 FIXME("EM_LINELENGTH: returning number of unselected characters on lines with selection unsupported.\n");
2699 return 0;
2701 item = ME_FindItemAtOffset(editor, diRun, wParam, NULL);
2702 item = ME_RowStart(item);
2703 nThisLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item, diRun), 0);
2704 item_end = ME_FindItemFwd(item, diStartRowOrParagraphOrEnd);
2705 if (item_end->type == diStartRow)
2706 nNextLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item_end, diRun), 0);
2707 else
2708 nNextLineOfs = ME_FindItemFwd(item, diParagraphOrEnd)->member.para.nCharOfs
2709 - (editor->bEmulateVersion10?2:1);
2710 nChars = nNextLineOfs - nThisLineOfs;
2711 TRACE("EM_LINELENGTH(%ld)==%d\n",wParam, nChars);
2712 return nChars;
2714 case EM_EXLIMITTEXT:
2716 if ((int)lParam < 0)
2717 return 0;
2718 if (lParam == 0)
2719 editor->nTextLimit = 65536;
2720 else
2721 editor->nTextLimit = (int) lParam;
2722 return 0;
2724 case EM_LIMITTEXT:
2726 if (wParam == 0)
2727 editor->nTextLimit = 65536;
2728 else
2729 editor->nTextLimit = (int) wParam;
2730 return 0;
2732 case EM_GETLIMITTEXT:
2734 return editor->nTextLimit;
2736 case EM_FINDTEXT:
2738 FINDTEXTA *ft = (FINDTEXTA *)lParam;
2739 int nChars = MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, NULL, 0);
2740 WCHAR *tmp;
2741 LRESULT r;
2743 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
2744 MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, tmp, nChars);
2745 r = ME_FindText(editor, wParam, &ft->chrg, tmp, NULL);
2746 FREE_OBJ( tmp );
2747 return r;
2749 case EM_FINDTEXTEX:
2751 FINDTEXTEXA *ex = (FINDTEXTEXA *)lParam;
2752 int nChars = MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, NULL, 0);
2753 WCHAR *tmp;
2754 LRESULT r;
2756 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
2757 MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, tmp, nChars);
2758 r = ME_FindText(editor, wParam, &ex->chrg, tmp, &ex->chrgText);
2759 FREE_OBJ( tmp );
2760 return r;
2762 case EM_FINDTEXTW:
2764 FINDTEXTW *ft = (FINDTEXTW *)lParam;
2765 return ME_FindText(editor, wParam, &ft->chrg, ft->lpstrText, NULL);
2767 case EM_FINDTEXTEXW:
2769 FINDTEXTEXW *ex = (FINDTEXTEXW *)lParam;
2770 return ME_FindText(editor, wParam, &ex->chrg, ex->lpstrText, &ex->chrgText);
2772 case EM_GETZOOM:
2773 if (!wParam || !lParam)
2774 return FALSE;
2775 *(int *)wParam = editor->nZoomNumerator;
2776 *(int *)lParam = editor->nZoomDenominator;
2777 return TRUE;
2778 case EM_SETZOOM:
2779 return ME_SetZoom(editor, wParam, lParam);
2780 case EM_CHARFROMPOS:
2781 return ME_CharFromPos(editor, ((POINTL *)lParam)->x, ((POINTL *)lParam)->y);
2782 case EM_POSFROMCHAR:
2784 ME_DisplayItem *pRun;
2785 int nCharOfs, nOffset, nLength;
2786 POINTL pt = {0,0};
2788 nCharOfs = wParam;
2789 /* detect which API version we're dealing with */
2790 if (wParam >= 0x40000)
2791 nCharOfs = lParam;
2792 nLength = ME_GetTextLength(editor);
2794 if (nCharOfs < nLength) {
2795 ME_RunOfsFromCharOfs(editor, nCharOfs, &pRun, &nOffset);
2796 assert(pRun->type == diRun);
2797 pt.y = pRun->member.run.pt.y;
2798 pt.x = pRun->member.run.pt.x + ME_PointFromChar(editor, &pRun->member.run, nOffset);
2799 pt.y += ME_GetParagraph(pRun)->member.para.nYPos;
2800 } else {
2801 pt.x = 0;
2802 pt.y = editor->pBuffer->pLast->member.para.nYPos;
2804 pt.x += editor->selofs;
2805 if (wParam >= 0x40000) {
2806 *(POINTL *)wParam = pt;
2808 return MAKELONG( pt.x, pt.y );
2810 case WM_CREATE:
2811 if (GetWindowLongW(hWnd, GWL_STYLE) & WS_HSCROLL)
2812 { /* Squelch the default horizontal scrollbar it would make */
2813 ShowScrollBar(editor->hWnd, SB_HORZ, FALSE);
2815 ME_CommitUndo(editor);
2816 ME_WrapMarkedParagraphs(editor);
2817 ME_MoveCaret(editor);
2818 return 0;
2819 case WM_DESTROY:
2820 ME_DestroyEditor(editor);
2821 SetWindowLongPtrW(hWnd, 0, 0);
2822 return 0;
2823 case WM_LBUTTONDOWN:
2824 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
2825 !ME_FilterEvent(editor, msg, &wParam, &lParam))
2826 return 0;
2827 SetFocus(hWnd);
2828 ME_LButtonDown(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
2829 SetCapture(hWnd);
2830 ME_LinkNotify(editor,msg,wParam,lParam);
2831 if (!ME_SetCursor(editor, LOWORD(lParam))) goto do_default;
2832 break;
2833 case WM_MOUSEMOVE:
2834 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
2835 !ME_FilterEvent(editor, msg, &wParam, &lParam))
2836 return 0;
2837 if (GetCapture() == hWnd)
2838 ME_MouseMove(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
2839 ME_LinkNotify(editor,msg,wParam,lParam);
2840 if (!ME_SetCursor(editor, LOWORD(lParam))) goto do_default;
2841 break;
2842 case WM_LBUTTONUP:
2843 if (GetCapture() == hWnd)
2844 ReleaseCapture();
2845 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
2846 !ME_FilterEvent(editor, msg, &wParam, &lParam))
2847 return 0;
2848 else
2850 BOOL ret;
2851 editor->linesel = 0;
2852 ret = ME_SetCursor(editor, LOWORD(lParam));
2853 ME_LinkNotify(editor,msg,wParam,lParam);
2854 if (!ret) goto do_default;
2856 break;
2857 case WM_LBUTTONDBLCLK:
2858 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
2859 !ME_FilterEvent(editor, msg, &wParam, &lParam))
2860 return 0;
2861 ME_LinkNotify(editor,msg,wParam,lParam);
2862 ME_SelectWord(editor);
2863 break;
2864 case WM_RBUTTONUP:
2865 case WM_RBUTTONDOWN:
2866 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
2867 !ME_FilterEvent(editor, msg, &wParam, &lParam))
2868 return 0;
2869 goto do_default;
2870 case WM_CONTEXTMENU:
2871 if (!ME_ShowContextMenu(editor, (short)LOWORD(lParam), (short)HIWORD(lParam)))
2872 goto do_default;
2873 break;
2874 case WM_PAINT:
2875 if (editor->bRedraw)
2877 HDC hDC;
2878 PAINTSTRUCT ps;
2880 hDC = BeginPaint(hWnd, &ps);
2881 ME_PaintContent(editor, hDC, FALSE, &ps.rcPaint);
2882 EndPaint(hWnd, &ps);
2884 break;
2885 case WM_SETFOCUS:
2886 editor->bHaveFocus = TRUE;
2887 ME_ShowCaret(editor);
2888 ME_SendOldNotify(editor, EN_SETFOCUS);
2889 return 0;
2890 case WM_KILLFOCUS:
2891 ME_HideCaret(editor);
2892 editor->bHaveFocus = FALSE;
2893 ME_SendOldNotify(editor, EN_KILLFOCUS);
2894 return 0;
2895 case WM_ERASEBKGND:
2897 if (editor->bRedraw)
2899 HDC hDC = (HDC)wParam;
2900 RECT rc;
2901 if (GetUpdateRect(hWnd,&rc,TRUE))
2903 FillRect(hDC, &rc, editor->hbrBackground);
2906 return 1;
2908 case WM_COMMAND:
2909 TRACE("editor wnd command = %d\n", LOWORD(wParam));
2910 return 0;
2911 case WM_KEYUP:
2912 if ((editor->nEventMask & ENM_KEYEVENTS) &&
2913 !ME_FilterEvent(editor, msg, &wParam, &lParam))
2914 return 0;
2915 goto do_default;
2916 case WM_KEYDOWN:
2917 if ((editor->nEventMask & ENM_KEYEVENTS) &&
2918 !ME_FilterEvent(editor, msg, &wParam, &lParam))
2919 return 0;
2920 if (ME_KeyDown(editor, LOWORD(wParam)))
2921 return 0;
2922 goto do_default;
2923 case WM_CHAR:
2925 WCHAR wstr;
2927 if (unicode)
2928 wstr = (WCHAR)wParam;
2929 else
2931 CHAR charA = wParam;
2932 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &wstr, 1);
2934 if (editor->AutoURLDetect_bEnable)
2935 ME_AutoURLDetect(editor, wstr);
2937 switch (wstr)
2939 case 1: /* Ctrl-A */
2940 ME_SetSelection(editor, 0, -1);
2941 return 0;
2942 case 3: /* Ctrl-C */
2943 SendMessageW(editor->hWnd, WM_COPY, 0, 0);
2944 return 0;
2947 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY) {
2948 MessageBeep(MB_ICONERROR);
2949 return 0; /* FIXME really 0 ? */
2952 switch (wstr)
2954 case 22: /* Ctrl-V */
2955 SendMessageW(editor->hWnd, WM_PASTE, 0, 0);
2956 return 0;
2957 case 24: /* Ctrl-X */
2958 SendMessageW(editor->hWnd, WM_CUT, 0, 0);
2959 return 0;
2960 case 25: /* Ctrl-Y */
2961 SendMessageW(editor->hWnd, EM_REDO, 0, 0);
2962 return 0;
2963 case 26: /* Ctrl-Z */
2964 SendMessageW(editor->hWnd, EM_UNDO, 0, 0);
2965 return 0;
2967 if (((unsigned)wstr)>=' '
2968 || (wstr=='\r' && (GetWindowLongW(hWnd, GWL_STYLE) & ES_MULTILINE))
2969 || wstr=='\t') {
2970 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
2971 /* WM_CHAR is restricted to nTextLimit */
2972 int from, to;
2973 ME_GetSelection(editor, &from, &to);
2974 if(editor->nTextLimit > ME_GetTextLength(editor) - (to-from))
2976 ME_Style *style = ME_GetInsertStyle(editor, 0);
2977 ME_SaveTempStyle(editor);
2978 if (wstr == '\r' && (GetKeyState(VK_SHIFT) & 0x8000))
2979 ME_InsertEndRowFromCursor(editor, 0);
2980 else
2981 ME_InsertTextFromCursor(editor, 0, &wstr, 1, style);
2982 ME_ReleaseStyle(style);
2983 ME_CommitUndo(editor);
2985 ME_UpdateRepaint(editor);
2987 return 0;
2989 case EM_SCROLL: /* fall through */
2990 case WM_VSCROLL:
2992 int origNPos;
2993 int lineHeight;
2995 origNPos = ME_GetYScrollPos(editor);
2996 lineHeight = 24;
2998 if (editor && editor->pBuffer && editor->pBuffer->pDefaultStyle)
2999 lineHeight = editor->pBuffer->pDefaultStyle->tm.tmHeight;
3000 if (lineHeight <= 0) lineHeight = 24;
3002 switch(LOWORD(wParam))
3004 case SB_LINEUP:
3005 ME_ScrollUp(editor,lineHeight);
3006 break;
3007 case SB_LINEDOWN:
3008 ME_ScrollDown(editor,lineHeight);
3009 break;
3010 case SB_PAGEUP:
3011 ME_ScrollUp(editor,editor->sizeWindow.cy);
3012 break;
3013 case SB_PAGEDOWN:
3014 ME_ScrollDown(editor,editor->sizeWindow.cy);
3015 break;
3016 case SB_THUMBTRACK:
3017 case SB_THUMBPOSITION:
3018 ME_ScrollAbs(editor,HIWORD(wParam));
3019 break;
3021 if (msg == EM_SCROLL)
3022 return 0x00010000 | (((ME_GetYScrollPos(editor) - origNPos)/lineHeight) & 0xffff);
3023 break;
3025 case WM_MOUSEWHEEL:
3027 int gcWheelDelta;
3028 UINT pulScrollLines;
3030 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
3031 !ME_FilterEvent(editor, msg, &wParam, &lParam))
3032 return 0;
3034 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
3035 gcWheelDelta = -GET_WHEEL_DELTA_WPARAM(wParam);
3037 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
3039 /* FIXME follow the original */
3040 ME_ScrollDown(editor,pulScrollLines * (gcWheelDelta / WHEEL_DELTA) * 8);
3042 break;
3044 case EM_GETRECT:
3046 *((RECT *)lParam) = editor->rcFormat;
3047 return 0;
3049 case EM_SETRECT:
3050 case EM_SETRECTNP:
3052 if (lParam)
3054 RECT *rc = (RECT *)lParam;
3056 if (wParam)
3058 editor->rcFormat.left += rc->left;
3059 editor->rcFormat.top += rc->top;
3060 editor->rcFormat.right += rc->right;
3061 editor->rcFormat.bottom += rc->bottom;
3063 else
3065 editor->rcFormat = *rc;
3068 else
3070 GetClientRect(hWnd, &editor->rcFormat);
3072 if (msg != EM_SETRECTNP)
3073 ME_RewrapRepaint(editor);
3074 return 0;
3076 case EM_REQUESTRESIZE:
3077 ME_SendRequestResize(editor, TRUE);
3078 return 0;
3079 case WM_SETREDRAW:
3080 if ((editor->bRedraw = wParam))
3081 ME_RewrapRepaint(editor);
3082 return 0;
3083 case WM_SIZE:
3085 GetClientRect(hWnd, &editor->rcFormat);
3086 ME_RewrapRepaint(editor);
3087 return DefWindowProcW(hWnd, msg, wParam, lParam);
3089 /* IME messages to make richedit controls IME aware */
3090 case WM_IME_SETCONTEXT:
3091 case WM_IME_CONTROL:
3092 case WM_IME_SELECT:
3093 case WM_IME_COMPOSITIONFULL:
3094 return 0;
3095 case WM_IME_STARTCOMPOSITION:
3097 editor->imeStartIndex=ME_GetCursorOfs(editor,0);
3098 ME_DeleteSelection(editor);
3099 ME_CommitUndo(editor);
3100 ME_UpdateRepaint(editor);
3101 return 0;
3103 case WM_IME_COMPOSITION:
3105 HIMC hIMC;
3107 ME_Style *style = ME_GetInsertStyle(editor, 0);
3108 hIMC = ImmGetContext(hWnd);
3109 ME_DeleteSelection(editor);
3110 ME_CommitUndo(editor);
3111 ME_SaveTempStyle(editor);
3112 if (lParam & GCS_RESULTSTR)
3114 LPWSTR lpCompStr = NULL;
3115 DWORD dwBufLen;
3117 dwBufLen = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
3118 lpCompStr = HeapAlloc(GetProcessHeap(),0,dwBufLen + sizeof(WCHAR));
3119 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, lpCompStr, dwBufLen);
3120 lpCompStr[dwBufLen/sizeof(WCHAR)] = 0;
3121 ME_InsertTextFromCursor(editor,0,lpCompStr,dwBufLen/sizeof(WCHAR),style);
3123 else if (lParam & GCS_COMPSTR)
3125 LPWSTR lpCompStr = NULL;
3126 DWORD dwBufLen;
3128 dwBufLen = ImmGetCompositionStringW(hIMC, GCS_COMPSTR, NULL, 0);
3129 lpCompStr = HeapAlloc(GetProcessHeap(),0,dwBufLen + sizeof(WCHAR));
3130 ImmGetCompositionStringW(hIMC, GCS_COMPSTR, lpCompStr, dwBufLen);
3131 lpCompStr[dwBufLen/sizeof(WCHAR)] = 0;
3133 ME_InsertTextFromCursor(editor,0,lpCompStr,dwBufLen/sizeof(WCHAR),style);
3134 ME_SetSelection(editor,editor->imeStartIndex,
3135 editor->imeStartIndex + dwBufLen/sizeof(WCHAR));
3137 ME_ReleaseStyle(style);
3138 ME_UpdateRepaint(editor);
3139 return 0;
3141 case WM_IME_ENDCOMPOSITION:
3143 ME_DeleteSelection(editor);
3144 editor->imeStartIndex=-1;
3145 return 0;
3147 case EM_GETOLEINTERFACE:
3149 LPVOID *ppvObj = (LPVOID*) lParam;
3150 return CreateIRichEditOle(editor, ppvObj);
3152 case EM_GETPASSWORDCHAR:
3154 return editor->cPasswordMask;
3156 case EM_SETOLECALLBACK:
3157 if(editor->lpOleCallback)
3158 IUnknown_Release(editor->lpOleCallback);
3159 editor->lpOleCallback = (LPRICHEDITOLECALLBACK)lParam;
3160 if(editor->lpOleCallback)
3161 IUnknown_AddRef(editor->lpOleCallback);
3162 return TRUE;
3163 case EM_GETWORDBREAKPROC:
3164 return (LRESULT)editor->pfnWordBreak;
3165 case EM_SETWORDBREAKPROC:
3167 EDITWORDBREAKPROCW pfnOld = editor->pfnWordBreak;
3169 editor->pfnWordBreak = (EDITWORDBREAKPROCW)lParam;
3170 return (LRESULT)pfnOld;
3172 case EM_SETTEXTMODE:
3174 LRESULT ret;
3175 int mask = 0;
3176 int changes = 0;
3177 ret = RichEditWndProc_common(hWnd, WM_GETTEXTLENGTH, 0, 0, unicode);
3178 if (!ret)
3180 /*Check for valid wParam*/
3181 if ((((wParam & TM_RICHTEXT) && ((wParam & TM_PLAINTEXT) << 1))) ||
3182 (((wParam & TM_MULTILEVELUNDO) && ((wParam & TM_SINGLELEVELUNDO) << 1))) ||
3183 (((wParam & TM_MULTICODEPAGE) && ((wParam & TM_SINGLECODEPAGE) << 1))))
3184 return 1;
3185 else
3187 if (wParam & (TM_RICHTEXT | TM_PLAINTEXT))
3189 mask |= (TM_RICHTEXT | TM_PLAINTEXT);
3190 changes |= (wParam & (TM_RICHTEXT | TM_PLAINTEXT));
3192 /*FIXME: Currently no support for undo level and code page options*/
3193 editor->mode = (editor->mode & (~mask)) | changes;
3194 return 0;
3197 return ret;
3199 case EM_SETPASSWORDCHAR:
3201 editor->cPasswordMask = wParam;
3202 ME_RewrapRepaint(editor);
3203 return 0;
3205 case EM_SETTARGETDEVICE:
3206 if (wParam == 0)
3208 switch (lParam)
3210 case 0: editor->bWordWrap = TRUE; break;
3211 case 1: editor->bWordWrap = FALSE; break;
3212 default: FIXME("Unknown option to EM_SETTARGETDEVICE(NULL,%ld)\n", lParam);
3215 else FIXME("Unsupported yet non NULL device in EM_SETTARGETDEVICE\n");
3216 break;
3217 default:
3218 do_default:
3219 return DefWindowProcW(hWnd, msg, wParam, lParam);
3221 return 0L;
3224 static LRESULT WINAPI RichEditWndProcW(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
3226 BOOL unicode = TRUE;
3228 /* Under Win9x RichEdit20W returns ANSI strings, see the tests. */
3229 if (msg == WM_GETTEXT && (GetVersion() & 0x80000000))
3230 unicode = FALSE;
3232 return RichEditWndProc_common(hWnd, msg, wParam, lParam, unicode);
3235 static LRESULT WINAPI RichEditWndProcA(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
3237 return RichEditWndProc_common(hWnd, msg, wParam, lParam, FALSE);
3240 /******************************************************************
3241 * RichEditANSIWndProc (RICHED20.10)
3243 LRESULT WINAPI RichEditANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
3245 return RichEditWndProcA(hWnd, msg, wParam, lParam);
3248 /******************************************************************
3249 * RichEdit10ANSIWndProc (RICHED20.9)
3251 LRESULT WINAPI RichEdit10ANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
3253 LRESULT result;
3255 /* FIXME: this is NOT the same as 2.0 version */
3256 result = RichEditANSIWndProc(hWnd, msg, wParam, lParam);
3257 if (msg == WM_NCCREATE)
3259 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongPtrW(hWnd, 0);
3261 editor->bEmulateVersion10 = TRUE;
3262 editor->pBuffer->pLast->member.para.nCharOfs = 2;
3264 return result;
3267 void ME_SendOldNotify(ME_TextEditor *editor, int nCode)
3269 HWND hWnd = editor->hWnd;
3270 SendMessageA(GetParent(hWnd), WM_COMMAND, (nCode<<16)|GetWindowLongW(hWnd, GWLP_ID), (LPARAM)hWnd);
3273 void ME_LinkNotify(ME_TextEditor *editor, UINT msg, WPARAM wParam, LPARAM lParam)
3275 int x,y;
3276 ME_Cursor tmpCursor;
3277 int nCharOfs; /* The start of the clicked text. Absolute character offset */
3279 ME_Run *tmpRun;
3281 ENLINK info;
3282 x = (short)LOWORD(lParam);
3283 y = (short)HIWORD(lParam);
3284 nCharOfs = ME_CharFromPos(editor, x, y);
3285 if (nCharOfs < 0) return;
3287 ME_CursorFromCharOfs(editor, nCharOfs, &tmpCursor);
3288 tmpRun = &tmpCursor.pRun->member.run;
3290 if ((tmpRun->style->fmt.dwMask & CFM_LINK)
3291 && (tmpRun->style->fmt.dwEffects & CFE_LINK))
3292 { /* The clicked run has CFE_LINK set */
3293 info.nmhdr.hwndFrom = editor->hWnd;
3294 info.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
3295 info.nmhdr.code = EN_LINK;
3296 info.msg = msg;
3297 info.wParam = wParam;
3298 info.lParam = lParam;
3299 info.chrg.cpMin = ME_CharOfsFromRunOfs(editor,tmpCursor.pRun,0);
3300 info.chrg.cpMax = info.chrg.cpMin + ME_StrVLen(tmpRun->strText);
3301 SendMessageW(GetParent(editor->hWnd), WM_NOTIFY,info.nmhdr.idFrom, (LPARAM)&info);
3305 int ME_CountParagraphsBetween(ME_TextEditor *editor, int from, int to)
3307 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
3308 int i = 0;
3310 while(item && item->member.para.next_para->member.para.nCharOfs <= from)
3311 item = item->member.para.next_para;
3312 if (!item)
3313 return 0;
3314 while(item && item->member.para.next_para->member.para.nCharOfs <= to) {
3315 item = item->member.para.next_para;
3316 i++;
3318 return i;
3322 int ME_GetTextW(ME_TextEditor *editor, WCHAR *buffer, int nStart, int nChars, int bCRLF)
3324 ME_DisplayItem *item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
3325 int nWritten = 0;
3326 WCHAR *pStart = buffer;
3328 if (!item) {
3329 *buffer = 0;
3330 return 0;
3333 if (nStart)
3335 int nLen = ME_StrLen(item->member.run.strText) - nStart;
3336 if (nLen > nChars)
3337 nLen = nChars;
3338 CopyMemory(buffer, item->member.run.strText->szData + nStart, sizeof(WCHAR)*nLen);
3339 nChars -= nLen;
3340 nWritten += nLen;
3341 if (!nChars)
3342 return nWritten;
3343 buffer += nLen;
3344 nStart = 0;
3345 item = ME_FindItemFwd(item, diRun);
3348 while(nChars && item)
3350 int nLen = ME_StrLen(item->member.run.strText);
3351 if (nLen > nChars)
3352 nLen = nChars;
3354 if (item->member.run.nFlags & MERF_ENDPARA)
3356 if (!ME_FindItemFwd(item, diRun))
3357 /* No '\r' is appended to the last paragraph. */
3358 nLen = 0;
3359 else if (bCRLF && nChars == 1) {
3360 nLen = 0;
3361 nChars = 0;
3362 } else {
3363 *buffer = '\r';
3364 if (bCRLF)
3366 *(++buffer) = '\n';
3367 nWritten++;
3369 assert(nLen == 1);
3370 /* our end paragraph consists of 2 characters now */
3371 if (editor->bEmulateVersion10)
3372 nChars--;
3375 else
3376 CopyMemory(buffer, item->member.run.strText->szData, sizeof(WCHAR)*nLen);
3377 nChars -= nLen;
3378 nWritten += nLen;
3379 buffer += nLen;
3381 if (!nChars)
3383 TRACE("nWritten=%d, actual=%d\n", nWritten, buffer-pStart);
3384 *buffer = 0;
3385 return nWritten;
3387 item = ME_FindItemFwd(item, diRun);
3389 *buffer = 0;
3390 TRACE("nWritten=%d, actual=%d\n", nWritten, buffer-pStart);
3391 return nWritten;
3394 static BOOL ME_RegisterEditorClass(HINSTANCE hInstance)
3396 WNDCLASSW wcW;
3397 WNDCLASSA wcA;
3399 wcW.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
3400 wcW.lpfnWndProc = RichEditWndProcW;
3401 wcW.cbClsExtra = 0;
3402 wcW.cbWndExtra = sizeof(ME_TextEditor *);
3403 wcW.hInstance = NULL; /* hInstance would register DLL-local class */
3404 wcW.hIcon = NULL;
3405 wcW.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
3406 wcW.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
3407 wcW.lpszMenuName = NULL;
3409 if (is_version_nt())
3411 wcW.lpszClassName = RichEdit20W;
3412 if (!RegisterClassW(&wcW)) return FALSE;
3413 wcW.lpszClassName = RichEdit50W;
3414 if (!RegisterClassW(&wcW)) return FALSE;
3416 else
3418 /* WNDCLASSA/W have the same layout */
3419 wcW.lpszClassName = (LPCWSTR)"RichEdit20W";
3420 if (!RegisterClassA((WNDCLASSA *)&wcW)) return FALSE;
3421 wcW.lpszClassName = (LPCWSTR)"RichEdit50W";
3422 if (!RegisterClassA((WNDCLASSA *)&wcW)) return FALSE;
3425 wcA.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
3426 wcA.lpfnWndProc = RichEditWndProcA;
3427 wcA.cbClsExtra = 0;
3428 wcA.cbWndExtra = sizeof(ME_TextEditor *);
3429 wcA.hInstance = NULL; /* hInstance would register DLL-local class */
3430 wcA.hIcon = NULL;
3431 wcA.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
3432 wcA.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
3433 wcA.lpszMenuName = NULL;
3434 wcA.lpszClassName = "RichEdit20A";
3435 if (!RegisterClassA(&wcA)) return FALSE;
3436 wcA.lpszClassName = "RichEdit50A";
3437 if (!RegisterClassA(&wcA)) return FALSE;
3439 return TRUE;
3442 LRESULT WINAPI REComboWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
3443 /* FIXME: Not implemented */
3444 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
3445 hWnd, msg, get_msg_name(msg), wParam, lParam);
3446 return DefWindowProcW(hWnd, msg, wParam, lParam);
3449 LRESULT WINAPI REListWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
3450 /* FIXME: Not implemented */
3451 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
3452 hWnd, msg, get_msg_name(msg), wParam, lParam);
3453 return DefWindowProcW(hWnd, msg, wParam, lParam);
3456 /******************************************************************
3457 * REExtendedRegisterClass (RICHED20.8)
3459 * FIXME undocumented
3460 * Need to check for errors and implement controls and callbacks
3462 LRESULT WINAPI REExtendedRegisterClass(void)
3464 WNDCLASSW wcW;
3465 UINT result;
3467 FIXME("semi stub\n");
3469 wcW.cbClsExtra = 0;
3470 wcW.cbWndExtra = 4;
3471 wcW.hInstance = NULL;
3472 wcW.hIcon = NULL;
3473 wcW.hCursor = NULL;
3474 wcW.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
3475 wcW.lpszMenuName = NULL;
3477 if (!ME_ListBoxRegistered)
3479 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS;
3480 wcW.lpfnWndProc = REListWndProc;
3481 wcW.lpszClassName = REListBox20W;
3482 if (RegisterClassW(&wcW)) ME_ListBoxRegistered = TRUE;
3485 if (!ME_ComboBoxRegistered)
3487 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
3488 wcW.lpfnWndProc = REComboWndProc;
3489 wcW.lpszClassName = REComboBox20W;
3490 if (RegisterClassW(&wcW)) ME_ComboBoxRegistered = TRUE;
3493 result = 0;
3494 if (ME_ListBoxRegistered)
3495 result += 1;
3496 if (ME_ComboBoxRegistered)
3497 result += 2;
3499 return result;
3502 int ME_AutoURLDetect(ME_TextEditor *editor, WCHAR curChar)
3504 struct prefix_s {
3505 const char *text;
3506 int length;
3507 } prefixes[12] = {
3508 {"http:", 5},
3509 {"file:", 6},
3510 {"mailto:", 8},
3511 {"ftp:", 5},
3512 {"https:", 7},
3513 {"gopher:", 8},
3514 {"nntp:", 6},
3515 {"prospero:", 10},
3516 {"telnet:", 8},
3517 {"news:", 6},
3518 {"wais:", 6},
3519 {"www.", 5}
3521 CHARRANGE ins_pt;
3522 int curf_ef, link_ef, def_ef;
3523 int cur_prefx, prefx_cnt;
3524 int sel_min, sel_max;
3525 int car_pos = 0;
3526 int text_pos=-1;
3527 int URLmin, URLmax = 0;
3528 CHARRANGE url;
3529 FINDTEXTA ft;
3530 CHARFORMAT2W cur_format;
3531 CHARFORMAT2W default_format;
3532 CHARFORMAT2W link;
3533 RichEditANSIWndProc(editor->hWnd, EM_EXGETSEL, (WPARAM) 0, (LPARAM) &ins_pt);
3534 sel_min = ins_pt.cpMin;
3535 sel_max = ins_pt.cpMax;
3536 if (sel_min==sel_max)
3537 car_pos = sel_min;
3538 if (sel_min!=sel_max)
3539 car_pos = ME_GetTextLength(editor)+1;
3540 cur_format.cbSize = sizeof(cur_format);
3541 default_format.cbSize = sizeof(default_format);
3542 RichEditANSIWndProc(editor->hWnd, EM_GETCHARFORMAT, SCF_SELECTION, (LPARAM) &cur_format);
3543 RichEditANSIWndProc(editor->hWnd, EM_GETCHARFORMAT, SCF_DEFAULT, (LPARAM) &default_format);
3544 link.cbSize = sizeof(link);
3545 link.dwMask = CFM_LINK;
3546 link.dwEffects = CFE_LINK;
3547 curf_ef = cur_format.dwEffects & link.dwEffects;
3548 def_ef = default_format.dwEffects & link.dwEffects;
3549 link_ef = link.dwEffects & link.dwEffects;
3550 if (curf_ef == link_ef)
3552 if( curChar == '\n' || curChar=='\r' || curChar==' ')
3554 ME_SetSelection(editor, car_pos, car_pos);
3555 RichEditANSIWndProc(editor->hWnd, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &default_format);
3556 text_pos=-1;
3557 return 0;
3560 if (curf_ef == def_ef)
3562 cur_prefx = 0;
3563 prefx_cnt = (sizeof(prefixes)/sizeof(struct prefix_s))-1;
3564 while (cur_prefx<=prefx_cnt)
3566 if (text_pos == -1)
3568 ft.lpstrText = prefixes[cur_prefx].text;
3569 URLmin=max(0,(car_pos-prefixes[cur_prefx].length));
3570 URLmax=max(0, car_pos);
3571 if ((car_pos == 0) && (ME_GetTextLength(editor) != 0))
3573 URLmax = ME_GetTextLength(editor)+1;
3575 ft.chrg.cpMin = URLmin;
3576 ft.chrg.cpMax = URLmax;
3577 text_pos=RichEditANSIWndProc(editor->hWnd, EM_FINDTEXT, FR_DOWN, (LPARAM)&ft);
3578 cur_prefx++;
3580 if (text_pos != -1)
3582 url.cpMin=text_pos;
3583 url.cpMax=car_pos-1;
3584 ME_SetCharFormat(editor, text_pos, (URLmax-text_pos), &link);
3585 ME_RewrapRepaint(editor);
3586 break;
3590 return 0;