1 /*
2 * RichEdit - functions dealing with editor object
3 *
4 * Copyright 2004 by Krzysztof Foltman
5 * Copyright 2005 by Cihan Altinay
6 * Copyright 2005 by Phil Krylov
7 * Copyright 2008 Eric Pouech
8 *
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.
13 *
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.
18 *
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
22 */
23
24 /*
25 API implementation status:
26
27 Messages (ANSI versions not done yet)
28 + EM_AUTOURLDETECT 2.0
29 + EM_CANPASTE
30 + EM_CANREDO 2.0
31 + EM_CANUNDO
32 + EM_CHARFROMPOS
33 - EM_DISPLAYBAND
34 + EM_EMPTYUNDOBUFFER
35 + EM_EXGETSEL
36 + EM_EXLIMITTEXT
37 + EM_EXLINEFROMCHAR
38 + EM_EXSETSEL
39 + EM_FINDTEXT (only FR_DOWN flag implemented)
40 + EM_FINDTEXTEX (only FR_DOWN flag implemented)
41 - EM_FINDWORDBREAK
42 - EM_FMTLINES
43 - EM_FORMATRANGE
44 + EM_GETAUTOURLDETECT 2.0
45 - EM_GETBIDIOPTIONS 3.0
46 - EM_GETCHARFORMAT (partly done)
47 - EM_GETEDITSTYLE
48 + EM_GETEVENTMASK
49 + EM_GETFIRSTVISIBLELINE (can be optimized if needed)
50 - EM_GETIMECOLOR 1.0asian
51 - EM_GETIMECOMPMODE 2.0
52 - EM_GETIMEOPTIONS 1.0asian
53 - EM_GETIMESTATUS
54 - EM_GETLANGOPTIONS 2.0
55 + EM_GETLIMITTEXT
56 + EM_GETLINE
57 + EM_GETLINECOUNT returns number of rows, not of paragraphs
58 + EM_GETMODIFY
59 - EM_GETOLEINTERFACE
60 + EM_GETOPTIONS
61 + EM_GETPARAFORMAT
62 + EM_GETPASSWORDCHAR 2.0
63 - EM_GETPUNCTUATION 1.0asian
64 + EM_GETRECT
65 - EM_GETREDONAME 2.0
66 + EM_GETSEL
67 + EM_GETSELTEXT (ANSI&Unicode)
68 + EM_GETSCROLLPOS 3.0 (only Y value valid)
69 ! - EM_GETTHUMB
70 + EM_GETTEXTEX 2.0
71 + EM_GETTEXTLENGTHEX (GTL_PRECISE unimplemented)
72 - EM_GETTEXTMODE 2.0
73 ? + EM_GETTEXTRANGE (ANSI&Unicode)
74 - EM_GETTYPOGRAPHYOPTIONS 3.0
75 - EM_GETUNDONAME
76 + EM_GETWORDBREAKPROC
77 - EM_GETWORDBREAKPROCEX
78 - EM_GETWORDWRAPMODE 1.0asian
79 + EM_GETZOOM 3.0
80 + EM_HIDESELECTION
81 + EM_LIMITTEXT (Also called EM_SETLIMITTEXT)
82 + EM_LINEFROMCHAR
83 + EM_LINEINDEX
84 + EM_LINELENGTH
85 + EM_LINESCROLL
86 - EM_PASTESPECIAL
87 + EM_POSFROMCHAR
88 + EM_REDO 2.0
89 + EM_REQUESTRESIZE
90 + EM_REPLACESEL (proper style?) ANSI&Unicode
91 + EM_SCROLL
92 + EM_SCROLLCARET
93 - EM_SELECTIONTYPE
94 - EM_SETBIDIOPTIONS 3.0
95 + EM_SETBKGNDCOLOR
96 + EM_SETCHARFORMAT (partly done, no ANSI)
97 - EM_SETEDITSTYLE
98 + EM_SETEVENTMASK (few notifications supported)
99 - EM_SETFONTSIZE
100 - EM_SETIMECOLOR 1.0asian
101 - EM_SETIMEOPTIONS 1.0asian
102 - EM_SETLANGOPTIONS 2.0
103 - EM_SETLIMITTEXT
104 + EM_SETMODIFY (not sure if implementation is correct)
105 - EM_SETOLECALLBACK
106 + EM_SETOPTIONS (partially implemented)
107 - EM_SETPALETTE 2.0
108 + EM_SETPARAFORMAT
109 + EM_SETPASSWORDCHAR 2.0
110 - EM_SETPUNCTUATION 1.0asian
111 + EM_SETREADONLY no beep on modification attempt
112 + EM_SETRECT
113 + EM_SETRECTNP (EM_SETRECT without repainting)
114 + EM_SETSEL
115 + EM_SETSCROLLPOS 3.0
116 - EM_SETTABSTOPS 3.0
117 - EM_SETTARGETDEVICE (partial)
118 + EM_SETTEXTEX 3.0 (proper style?)
119 - EM_SETTEXTMODE 2.0
120 - EM_SETTYPOGRAPHYOPTIONS 3.0
121 + EM_SETUNDOLIMIT 2.0
122 + EM_SETWORDBREAKPROC (used only for word movement at the moment)
123 - EM_SETWORDBREAKPROCEX
124 - EM_SETWORDWRAPMODE 1.0asian
125 + EM_SETZOOM 3.0
126 + EM_SHOWSCROLLBAR 2.0
127 + EM_STOPGROUPTYPING 2.0
128 + EM_STREAMIN
129 + EM_STREAMOUT
130 + EM_UNDO
131 + WM_CHAR
132 + WM_CLEAR
133 + WM_COPY
134 + WM_CUT
135 + WM_GETDLGCODE (the current implementation is incomplete)
136 + WM_GETTEXT (ANSI&Unicode)
137 + WM_GETTEXTLENGTH (ANSI version sucks)
138 + WM_PASTE
139 + WM_SETFONT
140 + WM_SETTEXT (resets undo stack !) (proper style?) ANSI&Unicode
141 - WM_STYLECHANGING
142 - WM_STYLECHANGED (things like read-only flag)
143 - WM_UNICHAR
144
145 Notifications
146
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
166
167 Styles
168
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
187 */
188
189 /*
190 * RICHED20 TODO (incomplete):
191 *
192 * - messages/styles/notifications listed above
193 * - add remaining CHARFORMAT/PARAFORMAT fields
194 * - right/center align should strip spaces from the beginning
195 * - pictures/OLE objects (not just smiling faces that lack API support ;-) )
196 * - COM interface (looks like a major pain in the TODO list)
197 * - calculate heights of pictures (half-done)
198 * - horizontal scrolling (not even started)
199 * - hysteresis during wrapping (related to scrollbars appearing/disappearing)
200 * - find/replace
201 * - how to implement EM_FORMATRANGE and EM_DISPLAYBAND ? (Mission Impossible)
202 * - italic caret with italic fonts
203 * - IME
204 * - most notifications aren't sent at all (the most important ones are)
205 * - when should EN_SELCHANGE be sent after text change ? (before/after EN_UPDATE?)
206 * - WM_SETTEXT may use wrong style (but I'm 80% sure it's OK)
207 * - EM_GETCHARFORMAT with SCF_SELECTION may not behave 100% like in original (but very close)
208 * - full justification
209 * - hyphenation
210 * - tables
211 * - ListBox & ComboBox not implemented
212 *
213 * Bugs that are probably fixed, but not so easy to verify:
214 * - EN_UPDATE/EN_CHANGE are handled very incorrectly (should be OK now)
215 * - undo for ME_JoinParagraphs doesn't store paragraph format ? (it does)
216 * - check/fix artificial EOL logic (bCursorAtEnd, hardly logical)
217 * - caret shouldn't be displayed when selection isn't empty
218 * - check refcounting in style management functions (looks perfect now, but no bugs is suspicious)
219 * - undo for setting default format (done, might be buggy)
220 * - styles might be not released properly (looks like they work like charm, but who knows?
221 *
222 */
223
224 #include "editor.h"
225 #include "commdlg.h"
226 #include "winreg.h"
227 #define NO_SHLWAPI_STREAM
228 #include "shlwapi.h"
229 #include "rtf.h"
230 #include "imm.h"
231 #include "res.h"
232
233 #define STACK_SIZE_DEFAULT 100
234 #define STACK_SIZE_MAX 1000
235
236 #define TEXT_LIMIT_DEFAULT 32767
237
238 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
239
240 static BOOL ME_RegisterEditorClass(HINSTANCE);
241
242 static const WCHAR RichEdit20W[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '2', '', 'W', 0};
243 static const WCHAR RichEdit50W[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '5', '', 'W', 0};
244 static const WCHAR REListBox20W[] = {'R','E','L','i','s','t','B','o','x','2','','W', 0};
245 static const WCHAR REComboBox20W[] = {'R','E','C','o','m','b','o','B','o','x','2','','W', 0};
246 static HCURSOR hLeft;
247
248 int me_debug = 0;
249 HANDLE me_heap = NULL;
250
251 static BOOL ME_ListBoxRegistered = FALSE;
252 static BOOL ME_ComboBoxRegistered = FALSE;
253
254 static inline int is_version_nt(void)
255 {
256 return !(GetVersion() & 0x80000000);
257 }
258
259 static ME_TextBuffer *ME_MakeText(void) {
260
261 ME_TextBuffer *buf = ALLOC_OBJ(ME_TextBuffer);
262
263 ME_DisplayItem *p1 = ME_MakeDI(diTextStart);
264 ME_DisplayItem *p2 = ME_MakeDI(diTextEnd);
265
266 p1->prev = NULL;
267 p1->next = p2;
268 p2->prev = p1;
269 p2->next = NULL;
270 p1->member.para.next_para = p2;
271 p2->member.para.prev_para = p1;
272 p2->member.para.nCharOfs = 0;
273
274 buf->pFirst = p1;
275 buf->pLast = p2;
276 buf->pCharStyle = NULL;
277
278 return buf;
279 }
280
281
282 static LRESULT ME_StreamInText(ME_TextEditor *editor, DWORD dwFormat, ME_InStream *stream, ME_Style *style)
283 {
284 WCHAR wszText[STREAMIN_BUFFER_SIZE+1];
285 WCHAR *pText;
286
287 TRACE("%08x %p\n", dwFormat, stream);
288
289 do {
290 long nWideChars = 0;
291
292 if (!stream->dwSize)
293 {
294 ME_StreamInFill(stream);
295 if (stream->editstream->dwError)
296 break;
297 if (!stream->dwSize)
298 break;
299 }
300
301 if (!(dwFormat & SF_UNICODE))
302 {
303 /* FIXME? this is doomed to fail on true MBCS like UTF-8, luckily they're unlikely to be used as CP_ACP */
304 nWideChars = MultiByteToWideChar(CP_ACP, 0, stream->buffer, stream->dwSize, wszText, STREAMIN_BUFFER_SIZE);
305 pText = wszText;
306 }
307 else
308 {
309 nWideChars = stream->dwSize >> 1;
310 pText = (WCHAR *)stream->buffer;
311 }
312
313 ME_InsertTextFromCursor(editor, 0, pText, nWideChars, style);
314 if (stream->dwSize == 0)
315 break;
316 stream->dwSize = 0;
317 } while(1);
318 ME_CommitUndo(editor);
319 ME_UpdateRepaint(editor);
320 return 0;
321 }
322
323 static void ME_RTFCharAttrHook(RTF_Info *info)
324 {
325 CHARFORMAT2W fmt;
326 fmt.cbSize = sizeof(fmt);
327 fmt.dwMask = 0;
328 fmt.dwEffects = 0;
329
330 switch(info->rtfMinor)
331 {
332 case rtfPlain:
333 /* FIXME add more flags once they're implemented */
334 fmt.dwMask = CFM_BOLD | CFM_ITALIC | CFM_UNDERLINETYPE | CFM_STRIKEOUT | CFM_COLOR | CFM_BACKCOLOR | CFM_SIZE | CFM_WEIGHT;
335 fmt.dwEffects = CFE_AUTOCOLOR | CFE_AUTOBACKCOLOR;
336 fmt.yHeight = 12*20; /* 12pt */
337 fmt.wWeight = FW_NORMAL;
338 fmt.bUnderlineType = CFU_UNDERLINENONE;
339 break;
340 case rtfBold:
341 fmt.dwMask = CFM_BOLD | CFM_WEIGHT;
342 fmt.dwEffects = info->rtfParam ? CFE_BOLD : 0;
343 fmt.wWeight = info->rtfParam ? FW_BOLD : FW_NORMAL;
344 break;
345 case rtfItalic:
346 fmt.dwMask = CFM_ITALIC;
347 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
348 break;
349 case rtfUnderline:
350 fmt.dwMask = CFM_UNDERLINETYPE;
351 fmt.bUnderlineType = info->rtfParam ? CFU_CF1UNDERLINE : CFU_UNDERLINENONE;
352 break;
353 case rtfDotUnderline:
354 fmt.dwMask = CFM_UNDERLINETYPE;
355 fmt.bUnderlineType = info->rtfParam ? CFU_UNDERLINEDOTTED : CFU_UNDERLINENONE;
356 break;
357 case rtfDbUnderline:
358 fmt.dwMask = CFM_UNDERLINETYPE;
359 fmt.bUnderlineType = info->rtfParam ? CFU_UNDERLINEDOUBLE : CFU_UNDERLINENONE;
360 break;
361 case rtfWordUnderline:
362 fmt.dwMask = CFM_UNDERLINETYPE;
363 fmt.bUnderlineType = info->rtfParam ? CFU_UNDERLINEWORD : CFU_UNDERLINENONE;
364 break;
365 case rtfNoUnderline:
366 fmt.dwMask = CFM_UNDERLINETYPE;
367 fmt.bUnderlineType = CFU_UNDERLINENONE;
368 break;
369 case rtfStrikeThru:
370 fmt.dwMask = CFM_STRIKEOUT;
371 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
372 break;
373 case rtfSubScript:
374 case rtfSuperScript:
375 case rtfSubScrShrink:
376 case rtfSuperScrShrink:
377 case rtfNoSuperSub:
378 fmt.dwMask = CFM_SUBSCRIPT|CFM_SUPERSCRIPT;
379 if (info->rtfMinor == rtfSubScrShrink) fmt.dwEffects = CFE_SUBSCRIPT;
380 if (info->rtfMinor == rtfSuperScrShrink) fmt.dwEffects = CFE_SUPERSCRIPT;
381 if (info->rtfMinor == rtfNoSuperSub) fmt.dwEffects = 0;
382 break;
383 case rtfInvisible:
384 fmt.dwMask = CFM_HIDDEN;
385 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
386 break;
387 case rtfBackColor:
388 fmt.dwMask = CFM_BACKCOLOR;
389 fmt.dwEffects = 0;
390 if (info->rtfParam == 0)
391 fmt.dwEffects = CFE_AUTOBACKCOLOR;
392 else if (info->rtfParam != rtfNoParam)
393 {
394 RTFColor *c = RTFGetColor(info, info->rtfParam);
395 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
396 }
397 break;
398 case rtfForeColor:
399 fmt.dwMask = CFM_COLOR;
400 fmt.dwEffects = 0;
401 if (info->rtfParam == 0)
402 fmt.dwEffects = CFE_AUTOCOLOR;
403 else if (info->rtfParam != rtfNoParam)
404 {
405 RTFColor *c = RTFGetColor(info, info->rtfParam);
406 if (c)
407 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
408 else
409 fmt.crTextColor = 0;
410 }
411 break;
412 case rtfFontNum:
413 if (info->rtfParam != rtfNoParam)
414 {
415 RTFFont *f = RTFGetFont(info, info->rtfParam);
416 if (f)
417 {
418 MultiByteToWideChar(CP_ACP, 0, f->rtfFName, -1, fmt.szFaceName, sizeof(fmt.szFaceName)/sizeof(WCHAR));
419 fmt.szFaceName[sizeof(fmt.szFaceName)/sizeof(WCHAR)-1] = '\0';
420 fmt.bCharSet = f->rtfFCharSet;
421 fmt.dwMask = CFM_FACE | CFM_CHARSET;
422 fmt.bPitchAndFamily = f->rtfFPitch | (f->rtfFFamily << 4);
423 }
424 }
425 break;
426 case rtfFontSize:
427 fmt.dwMask = CFM_SIZE;
428 if (info->rtfParam != rtfNoParam)
429 fmt.yHeight = info->rtfParam*10;
430 break;
431 }
432 if (fmt.dwMask) {
433 ME_Style *style2;
434 RTFFlushOutputBuffer(info);
435 /* FIXME too slow ? how come ? */
436 style2 = ME_ApplyStyle(info->style, &fmt);
437 ME_ReleaseStyle(info->style);
438 info->style = style2;
439 info->styleChanged = TRUE;
440 }
441 }
442
443 /* FIXME this function doesn't get any information about context of the RTF tag, which is very bad,
444 the same tags mean different things in different contexts */
445 static void ME_RTFParAttrHook(RTF_Info *info)
446 {
447 PARAFORMAT2 fmt;
448 fmt.cbSize = sizeof(fmt);
449 fmt.dwMask = 0;
450
451 switch(info->rtfMinor)
452 {
453 case rtfParDef: /* restores default paragraph attributes */
454 fmt.dwMask = PFM_ALIGNMENT | PFM_BORDER | PFM_LINESPACING | PFM_TABSTOPS | PFM_OFFSET |
455 PFM_RIGHTINDENT | PFM_SPACEAFTER | PFM_SPACEBEFORE | PFM_STARTINDENT;
456 /* TODO: numbering, shading */
457 fmt.wAlignment = PFA_LEFT;
458 fmt.cTabCount = 0;
459 fmt.dxOffset = fmt.dxStartIndent = fmt.dxRightIndent = 0;
460 fmt.wBorderWidth = fmt.wBorders = 0;
461 fmt.wBorderSpace = 0;
462 fmt.bLineSpacingRule = 0;
463 fmt.dySpaceBefore = fmt.dySpaceAfter = 0;
464 fmt.dyLineSpacing = 0;
465 RTFFlushOutputBuffer(info);
466 ME_GetParagraph(info->editor->pCursors[0].pRun)->member.para.bTable = FALSE;
467 break;
468 case rtfInTable:
469 {
470 ME_DisplayItem *para;
471
472 RTFFlushOutputBuffer(info);
473 para = ME_GetParagraph(info->editor->pCursors[0].pRun);
474 assert(para->member.para.pCells);
475 para->member.para.bTable = TRUE;
476 return;
477 }
478 case rtfFirstIndent:
479 ME_GetSelectionParaFormat(info->editor, &fmt);
480 fmt.dwMask |= PFM_STARTINDENT | PFM_OFFSET;
481 fmt.dxStartIndent += info->rtfParam;
482 fmt.dxOffset = -info->rtfParam;
483 break;
484 case rtfLeftIndent:
485 /* we assume rtfLeftIndent is always specified before rtfFirstIndent */
486 ME_GetSelectionParaFormat(info->editor, &fmt);
487 fmt.dwMask |= PFM_STARTINDENT;
488 fmt.dxStartIndent = info->rtfParam;
489 fmt.dxOffset = 0;
490 break;
491 case rtfRightIndent:
492 fmt.dwMask = PFM_RIGHTINDENT;
493 fmt.dxRightIndent = info->rtfParam;
494 break;
495 case rtfQuadLeft:
496 case rtfQuadJust:
497 fmt.dwMask = PFM_ALIGNMENT;
498 fmt.wAlignment = PFA_LEFT;
499 break;
500 case rtfQuadRight:
501 fmt.dwMask = PFM_ALIGNMENT;
502 fmt.wAlignment = PFA_RIGHT;
503 break;
504 case rtfQuadCenter:
505 fmt.dwMask = PFM_ALIGNMENT;
506 fmt.wAlignment = PFA_CENTER;
507 break;
508 case rtfTabPos:
509 ME_GetSelectionParaFormat(info->editor, &fmt);
510 if (!(fmt.dwMask & PFM_TABSTOPS))
511 {
512 fmt.dwMask |= PFM_TABSTOPS;
513 fmt.cTabCount = 0;
514 }
515 if (fmt.cTabCount < MAX_TAB_STOPS)
516 fmt.rgxTabs[fmt.cTabCount++] = info->rtfParam;
517 break;
518 case rtfKeep:
519 fmt.dwMask = PFM_KEEP;
520 fmt.wEffects = PFE_KEEP;
521 break;
522 case rtfNoWidowControl:
523 fmt.dwMask = PFM_NOWIDOWCONTROL;
524 fmt.wEffects = PFE_NOWIDOWCONTROL;
525 break;
526 case rtfKeepNext:
527 fmt.dwMask = PFM_KEEPNEXT;
528 fmt.wEffects = PFE_KEEPNEXT;
529 break;
530 case rtfSpaceAfter:
531 fmt.dwMask = PFM_SPACEAFTER;
532 fmt.dySpaceAfter = info->rtfParam;
533 break;
534 case rtfSpaceBefore:
535 fmt.dwMask = PFM_SPACEBEFORE;
536 fmt.dySpaceBefore = info->rtfParam;
537 break;
538 case rtfSpaceBetween:
539 fmt.dwMask = PFM_LINESPACING;
540 if ((int)info->rtfParam > 0)
541 {
542 fmt.dyLineSpacing = info->rtfParam;
543 fmt.bLineSpacingRule = 3;
544 }
545 else
546 {
547 fmt.dyLineSpacing = info->rtfParam;
548 fmt.bLineSpacingRule = 4;
549 }
550 case rtfSpaceMultiply:
551 fmt.dwMask = PFM_LINESPACING;
552 fmt.dyLineSpacing = info->rtfParam * 20;
553 fmt.bLineSpacingRule = 5;
554 break;
555 case rtfParBullet:
556 fmt.dwMask = PFM_NUMBERING;
557 fmt.wNumbering = PFN_BULLET;
558 break;
559 case rtfParSimple:
560 fmt.dwMask = PFM_NUMBERING;
561 fmt.wNumbering = 2; /* FIXME: MSDN says it's not used ?? */
562 break;
563 case rtfParNumDecimal:
564 fmt.dwMask = PFM_NUMBERING;
565 fmt.wNumbering = 2; /* FIXME: MSDN says it's not used ?? */
566 break;
567 case rtfParNumIndent:
568 fmt.dwMask = PFM_NUMBERINGTAB;
569 fmt.wNumberingTab = info->rtfParam;
570 break;
571 case rtfParNumStartAt:
572 fmt.dwMask = PFM_NUMBERINGSTART;
573 fmt.wNumberingStart = info->rtfParam;
574 break;
575 case rtfBorderLeft:
576 ME_GetSelectionParaFormat(info->editor, &fmt);
577 if (!(fmt.dwMask & PFM_BORDER))
578 {
579 fmt.dwMask |= PFM_BORDER;
580 fmt.wBorderSpace = 0;
581 fmt.wBorderWidth = 1;
582 fmt.wBorders = 0;
583 }
584 fmt.wBorders |= 1;
585 break;
586 case rtfBorderRight:
587 ME_GetSelectionParaFormat(info->editor, &fmt);
588 if (!(fmt.dwMask & PFM_BORDER))
589 {
590 fmt.dwMask |= PFM_BORDER;
591 fmt.wBorderSpace = 0;
592 fmt.wBorderWidth = 1;
593 fmt.wBorders = 0;
594 }
595 fmt.wBorders |= 2;
596 break;
597 case rtfBorderTop:
598 ME_GetSelectionParaFormat(info->editor, &fmt);
599 if (!(fmt.dwMask & PFM_BORDER))
600 {
601 fmt.dwMask |= PFM_BORDER;
602 fmt.wBorderSpace = 0;
603 fmt.wBorderWidth = 1;
604 fmt.wBorders = 0;
605 }
606 fmt.wBorders |= 4;
607 break;
608 case rtfBorderBottom:
609 ME_GetSelectionParaFormat(info->editor, &fmt);
610 if (!(fmt.dwMask & PFM_BORDER))
611 {
612 fmt.dwMask |= PFM_BORDER;
613 fmt.wBorderSpace = 0;
614 fmt.wBorderWidth = 1;
615 fmt.wBorders = 0;
616 }
617 fmt.wBorders |= 8;
618 break;
619 case rtfBorderSingle:
620 ME_GetSelectionParaFormat(info->editor, &fmt);
621 /* we assume that borders have been created before (RTF spec) */
622 fmt.wBorders &= ~0x700;
623 fmt.wBorders |= 1 << 8;
624 break;
625 case rtfBorderThick:
626 ME_GetSelectionParaFormat(info->editor, &fmt);
627 /* we assume that borders have been created before (RTF spec) */
628 fmt.wBorders &= ~0x700;
629 fmt.wBorders |= 2 << 8;
630 break;
631 case rtfBorderShadow:
632 ME_GetSelectionParaFormat(info->editor, &fmt);
633 /* we assume that borders have been created before (RTF spec) */
634 fmt.wBorders &= ~0x700;
635 fmt.wBorders |= 10 << 8;
636 break;
637 case rtfBorderDouble:
638 ME_GetSelectionParaFormat(info->editor, &fmt);
639 /* we assume that borders have been created before (RTF spec) */
640 fmt.wBorders &= ~0x700;
641 fmt.wBorders |= 7 << 8;
642 break;
643 case rtfBorderDot:
644 ME_GetSelectionParaFormat(info->editor, &fmt);
645 /* we assume that borders have been created before (RTF spec) */
646 fmt.wBorders &= ~0x700;
647 fmt.wBorders |= 11 << 8;
648 break;
649 case rtfBorderWidth:
650 ME_GetSelectionParaFormat(info->editor, &fmt);
651 /* we assume that borders have been created before (RTF spec) */
652 fmt.wBorderWidth |= ((info->rtfParam / 15) & 7) << 8;
653 break;
654 case rtfBorderSpace:
655 ME_GetSelectionParaFormat(info->editor, &fmt);
656 /* we assume that borders have been created before (RTF spec) */
657 fmt.wBorderSpace = info->rtfParam;
658 break;
659 }
660 if (fmt.dwMask) {
661 RTFFlushOutputBuffer(info);
662 /* FIXME too slow ? how come ?*/
663 ME_SetSelectionParaFormat(info->editor, &fmt);
664 }
665 }
666
667 static void ME_RTFTblAttrHook(RTF_Info *info)
668 {
669 ME_DisplayItem *para;
670
671 switch (info->rtfMinor)
672 {
673 case rtfRowDef:
674 RTFFlushOutputBuffer(info);
675 para = ME_GetParagraph(info->editor->pCursors[0].pRun);
676
677 /* Release possibly inherited cell definitions */
678 ME_DestroyTableCellList(para);
679
680 para->member.para.pCells = ALLOC_OBJ(ME_TableCell);
681 para->member.para.pCells->nRightBoundary = 0;
682 para->member.para.pCells->next = NULL;
683 para->member.para.pLastCell = para->member.para.pCells;
684 break;
685 case rtfCellPos:
686 RTFFlushOutputBuffer(info);
687 para = ME_GetParagraph(info->editor->pCursors[0].pRun);
688
689 if (para->member.para.pLastCell->nRightBoundary)
690 {
691 ME_TableCell *pCell = ALLOC_OBJ(ME_TableCell);
692
693 pCell->next = NULL;
694 para->member.para.pLastCell->next = pCell;
695 para->member.para.pLastCell = pCell;
696 }
697 para->member.para.pLastCell->nRightBoundary = info->rtfParam;
698 break;
699 }
700 }
701
702 static BOOL ME_RTFInsertOleObject(RTF_Info *info, HENHMETAFILE hemf, HBITMAP hbmp,
703 const SIZEL* sz)
704 {
705 LPOLEOBJECT lpObject = NULL;
706 LPSTORAGE lpStorage = NULL;
707 LPOLECLIENTSITE lpClientSite = NULL;
708 LPDATAOBJECT lpDataObject = NULL;
709 LPOLECACHE lpOleCache = NULL;
710 STGMEDIUM stgm;
711 FORMATETC fm;
712 CLSID clsid;
713 BOOL ret = FALSE;
714 DWORD conn;
715
716 if (hemf)
717 {
718 stgm.tymed = TYMED_ENHMF;
719 stgm.u.hEnhMetaFile = hemf;
720 fm.cfFormat = CF_ENHMETAFILE;
721 }
722 else if (hbmp)
723 {
724 stgm.tymed = TYMED_GDI;
725 stgm.u.hBitmap = hbmp;
726 fm.cfFormat = CF_BITMAP;
727 }
728 stgm.pUnkForRelease = NULL;
729
730 fm.ptd = NULL;
731 fm.dwAspect = DVASPECT_CONTENT;
732 fm.lindex = -1;
733 fm.tymed = stgm.tymed;
734
735 if (!info->lpRichEditOle)
736 {
737 CreateIRichEditOle(info->editor, (VOID**)&info->lpRichEditOle);
738 }
739
740 if (OleCreateDefaultHandler(&CLSID_NULL, NULL, &IID_IOleObject, (void**)&lpObject) == S_OK &&
741 #if 0
742 /* FIXME: enable it when rich-edit properly implements this method */
743 IRichEditOle_GetClientSite(info->lpRichEditOle, &lpClientSite) == S_OK &&
744 IOleObject_SetClientSite(lpObject, lpClientSite) == S_OK &&
745 #endif
746 IOleObject_GetUserClassID(lpObject, &clsid) == S_OK &&
747 IOleObject_QueryInterface(lpObject, &IID_IOleCache, (void**)&lpOleCache) == S_OK &&
748 IOleCache_Cache(lpOleCache, &fm, 0, &conn) == S_OK &&
749 IOleObject_QueryInterface(lpObject, &IID_IDataObject, (void**)&lpDataObject) == S_OK &&
750 IDataObject_SetData(lpDataObject, &fm, &stgm, TRUE) == S_OK)
751 {
752 REOBJECT reobject;
753
754 reobject.cbStruct = sizeof(reobject);
755 reobject.cp = REO_CP_SELECTION;
756 reobject.clsid = clsid;
757 reobject.poleobj = lpObject;
758 reobject.pstg = lpStorage;
759 reobject.polesite = lpClientSite;
760 /* convert from twips to .01 mm */
761 reobject.sizel.cx = MulDiv(sz->cx, 254, 144);
762 reobject.sizel.cy = MulDiv(sz->cy, 254, 144);
763 reobject.dvaspect = DVASPECT_CONTENT;
764 reobject.dwFlags = 0; /* FIXME */
765 reobject.dwUser = 0;
766
767 /* FIXME: could be simpler */
768 ret = IRichEditOle_InsertObject(info->lpRichEditOle, &reobject) == S_OK;
769 }
770
771 if (lpObject) IOleObject_Release(lpObject);
772 if (lpClientSite) IOleClientSite_Release(lpClientSite);
773 if (lpStorage) IStorage_Release(lpStorage);
774 if (lpDataObject) IDataObject_Release(lpDataObject);
775 if (lpOleCache) IOleCache_Release(lpOleCache);
776
777 return ret;
778 }
779
780 static void ME_RTFReadPictGroup(RTF_Info *info)
781 {
782 SIZEL sz;
783 BYTE* buffer = NULL;
784 unsigned bufsz, bufidx;
785 BOOL flip;
786 BYTE val;
787 METAFILEPICT mfp;
788 HENHMETAFILE hemf;
789 HBITMAP hbmp;
790 enum gfxkind {gfx_unknown = 0, gfx_enhmetafile, gfx_metafile, gfx_dib} gfx = gfx_unknown;
791
792 RTFGetToken (info);
793 if (info->rtfClass == rtfEOF)
794 return;
795 mfp.mm = MM_TEXT;
796 /* fetch picture type */
797 if (RTFCheckMM (info, rtfPictAttr, rtfWinMetafile))
798 {
799 mfp.mm = info->rtfParam;
800 gfx = gfx_metafile;
801 }
802 else if (RTFCheckMM (info, rtfPictAttr, rtfDevIndBitmap))
803 {
804 if (info->rtfParam != 0) FIXME("dibitmap should be 0 (%d)\n", info->rtfParam);
805 gfx = gfx_dib;
806 }
807 else if (RTFCheckMM (info, rtfPictAttr, rtfEmfBlip))
808 {
809 gfx = gfx_enhmetafile;
810 }
811 else
812 {
813 FIXME("%d %d\n", info->rtfMajor, info->rtfMinor);
814 goto skip_group;
815 }
816 sz.cx = sz.cy = 0;
817 /* fetch picture attributes */
818 for (;;)
819 {
820 RTFGetToken (info);
821 if (info->rtfClass == rtfEOF)
822 return;
823 if (info->rtfClass == rtfText)
824 break;
825 if (!RTFCheckCM (info, rtfControl, rtfPictAttr))
826 {
827 ERR("Expected picture attribute (%d %d)\n",
828 info->rtfClass, info->rtfMajor);
829 goto skip_group;
830 }
831 else if (RTFCheckMM (info, rtfPictAttr, rtfPicWid))
832 {
833 if (gfx == gfx_metafile) mfp.xExt = info->rtfParam;
834 }
835 else if (RTFCheckMM (info, rtfPictAttr, rtfPicHt))
836 {
837 if (gfx == gfx_metafile) mfp.yExt = info->rtfParam;
838 }
839 else if (RTFCheckMM (info, rtfPictAttr, rtfPicGoalWid))
840 sz.cx = info->rtfParam;
841 else if (RTFCheckMM (info, rtfPictAttr, rtfPicGoalHt))
842 sz.cy = info->rtfParam;
843 else
844 FIXME("Non supported attribute: %d %d %d\n", info->rtfClass, info->rtfMajor, info->rtfMinor);
845 }
846 /* fetch picture data */
847 bufsz = 1024;
848 bufidx = 0;
849 buffer = HeapAlloc(GetProcessHeap(), 0, bufsz);
850 val = info->rtfMajor;
851 for (flip = TRUE;; flip = !flip)
852 {
853 RTFGetToken (info);
854 if (info->rtfClass == rtfEOF)
855 {
856 HeapFree(GetProcessHeap(), 0, buffer);
857 return; /* Warn ?? */
858 }
859 if (RTFCheckCM(info, rtfGroup, rtfEndGroup))
860 break;
861 if (info->rtfClass != rtfText) goto skip_group;
862 if (flip)
863 {
864 if (bufidx >= bufsz &&
865 !(buffer = HeapReAlloc(GetProcessHeap(), 0, buffer, bufsz += 1024)))
866 goto skip_group;
867 buffer[bufidx++] = RTFCharToHex(val) * 16 + RTFCharToHex(info->rtfMajor);
868 }
869 else
870 val = info->rtfMajor;
871 }
872 if (flip) FIXME("wrong hex string\n");
873
874 switch (gfx)
875 {
876 case gfx_enhmetafile:
877 if ((hemf = SetEnhMetaFileBits(bufidx, buffer)))
878 ME_RTFInsertOleObject(info, hemf, NULL, &sz);
879 break;
880 case gfx_metafile:
881 if ((hemf = SetWinMetaFileBits(bufidx, buffer, NULL, &mfp)))
882 ME_RTFInsertOleObject(info, hemf, NULL, &sz);
883 break;
884 case gfx_dib:
885 {
886 BITMAPINFO* bi = (BITMAPINFO*)buffer;
887 HDC hdc = GetDC(0);
888 unsigned nc = bi->bmiHeader.biClrUsed;
889
890 /* not quite right, especially for bitfields type of compression */
891 if (!nc && bi->bmiHeader.biBitCount <= 8)
892 nc = 1 << bi->bmiHeader.biBitCount;
893 if ((hbmp = CreateDIBitmap(hdc, &bi->bmiHeader,
894 CBM_INIT, (char*)(bi + 1) + nc * sizeof(RGBQUAD),
895 bi, DIB_RGB_COLORS)))
896 ME_RTFInsertOleObject(info, NULL, hbmp, &sz);
897 ReleaseDC(0, hdc);
898 }
899 break;
900 default:
901 break;
902 }
903 HeapFree(GetProcessHeap(), 0, buffer);
904 RTFRouteToken (info); /* feed "}" back to router */
905 return;
906 skip_group:
907 HeapFree(GetProcessHeap(), 0, buffer);
908 RTFSkipGroup(info);
909 RTFRouteToken(info); /* feed "}" back to router */
910 }
911
912 /* for now, lookup the \result part and use it, whatever the object */
913 static void ME_RTFReadObjectGroup(RTF_Info *info)
914 {
915 for (;;)
916 {
917 RTFGetToken (info);
918 if (info->rtfClass == rtfEOF)
919 return;
920 if (RTFCheckCM(info, rtfGroup, rtfEndGroup))
921 break;
922 if (RTFCheckCM(info, rtfGroup, rtfBeginGroup))
923 {
924 RTFGetToken (info);
925 if (info->rtfClass == rtfEOF)
926 return;
927 if (RTFCheckCMM(info, rtfControl, rtfDestination, rtfObjResult))
928 {
929 int level = 1;
930
931 while (RTFGetToken (info) != rtfEOF)
932 {
933 if (info->rtfClass == rtfGroup)
934 {
935 if (info->rtfMajor == rtfBeginGroup) level++;
936 else if (info->rtfMajor == rtfEndGroup && --level < 0) break;
937 }
938 RTFRouteToken(info);
939 }
940 }
941 else RTFSkipGroup(info);
942 continue;
943 }
944 if (!RTFCheckCM (info, rtfControl, rtfObjAttr))
945 {
946 FIXME("Non supported attribute: %d %d %d\n", info->rtfClass, info->rtfMajor, info->rtfMinor);
947 return;
948 }
949 }
950 RTFRouteToken(info); /* feed "}" back to router */
951 }
952
953 static void ME_RTFReadHook(RTF_Info *info) {
954 switch(info->rtfClass)
955 {
956 case rtfGroup:
957 switch(info->rtfMajor)
958 {
959 case rtfBeginGroup:
960 if (info->stackTop < maxStack) {
961 info->stack[info->stackTop].fmt = info->style->fmt;
962 info->stack[info->stackTop].codePage = info->codePage;
963 info->stack[info->stackTop].unicodeLength = info->unicodeLength;
964 }
965 info->stackTop++;
966 info->styleChanged = FALSE;
967 break;
968 case rtfEndGroup:
969 {
970 ME_Style *s;
971 RTFFlushOutputBuffer(info);
972 info->stackTop--;
973 if (info->stackTop<=0) {
974 info->rtfClass = rtfEOF;
975 return;
976 }
977 assert(info->stackTop >= 0);
978 if (info->styleChanged)
979 {
980 /* FIXME too slow ? how come ? */
981 s = ME_ApplyStyle(info->style, &info->stack[info->stackTop].fmt);
982 ME_ReleaseStyle(info->style);
983 info->style = s;
984 info->codePage = info->stack[info->stackTop].codePage;
985 info->unicodeLength = info->stack[info->stackTop].unicodeLength;
986 }
987 break;
988 }
989 }
990 break;
991 case rtfControl:
992 switch(info->rtfMajor)
993 {
994 case rtfCharAttr:
995 ME_RTFCharAttrHook(info);
996 break;
997 case rtfParAttr:
998 ME_RTFParAttrHook(info);
999 break;
1000 case rtfTblAttr:
1001 ME_RTFTblAttrHook(info);
1002 break;
1003 case rtfSpecialChar:
1004 if (info->rtfMinor == rtfCell)
1005 {
1006 RTFFlushOutputBuffer(info);
1007 ME_InsertTableCellFromCursor(info->editor, 0);
1008 }
1009 }
1010 break;
1011 }
1012 }
1013
1014 void
1015 ME_StreamInFill(ME_InStream *stream)
1016 {
1017 stream->editstream->dwError = stream->editstream->pfnCallback(stream->editstream->dwCookie,
1018 (BYTE *)stream->buffer,
1019 sizeof(stream->buffer),
1020 (LONG *)&stream->dwSize);
1021 stream->dwUsed = 0;
1022 }
1023
1024 static LRESULT ME_StreamIn(ME_TextEditor *editor, DWORD format, EDITSTREAM *stream, BOOL stripLastCR)
1025 {
1026 RTF_Info parser;
1027 ME_Style *style;
1028 int from, to, to2, nUndoMode;
1029 int nEventMask = editor->nEventMask;
1030 ME_InStream inStream;
1031 BOOL invalidRTF = FALSE;
1032
1033 TRACE("stream==%p hWnd==%p format==0x%X\n", stream, editor->hWnd, format);
1034 editor->nEventMask = 0;
1035
1036 ME_GetSelection(editor, &from, &to);
1037 if ((format & SFF_SELECTION) && (editor->mode & TM_RICHTEXT)) {
1038 style = ME_GetSelectionInsertStyle(editor);
1039
1040 ME_InternalDeleteText(editor, from, to-from);
1041 }
1042 else {
1043 style = editor->pBuffer->pDefaultStyle;
1044 ME_AddRefStyle(style);
1045 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
1046 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
1047 from = to = 0;
1048 ME_ClearTempStyle(editor);
1049 /* FIXME restore default paragraph formatting ! */
1050 }
1051
1052
1053 /* Back up undo mode to a local variable */
1054 nUndoMode = editor->nUndoMode;
1055
1056 /* Only create an undo if SFF_SELECTION is set */
1057 if (!(format & SFF_SELECTION))
1058 editor->nUndoMode = umIgnore;
1059
1060 inStream.editstream = stream;
1061 inStream.editstream->dwError = 0;
1062 inStream.dwSize = 0;
1063 inStream.dwUsed = 0;
1064
1065 if (format & SF_RTF)
1066 {
1067 /* Check if it's really RTF, and if it is not, use plain text */
1068 ME_StreamInFill(&inStream);
1069 if (!inStream.editstream->dwError)
1070 {
1071 if ((!editor->bEmulateVersion10 && strncmp(inStream.buffer, "{\\rtf", 5) && strncmp(inStream.buffer, "{\\urtf", 6))
1072 || (editor->bEmulateVersion10 && *inStream.buffer != '{'))
1073 {
1074 invalidRTF = TRUE;
1075 inStream.editstream->dwError = -16;
1076 }
1077 }
1078 }
1079
1080 if (!invalidRTF && !inStream.editstream->dwError)
1081 {
1082 if (format & SF_RTF) {
1083 /* setup the RTF parser */
1084 memset(&parser, 0, sizeof parser);
1085 RTFSetEditStream(&parser, &inStream);
1086 parser.rtfFormat = format&(SF_TEXT|SF_RTF);
1087 parser.hwndEdit = editor->hWnd;
1088 parser.editor = editor;
1089 parser.style = style;
1090 WriterInit(&parser);
1091 RTFInit(&parser);
1092 RTFSetReadHook(&parser, ME_RTFReadHook);
1093 RTFSetDestinationCallback(&parser, rtfPict, ME_RTFReadPictGroup);
1094 RTFSetDestinationCallback(&parser, rtfObject, ME_RTFReadObjectGroup);
1095 BeginFile(&parser);
1096
1097 /* do the parsing */
1098 RTFRead(&parser);
1099 RTFFlushOutputBuffer(&parser);
1100 RTFDestroy(&parser);
1101 if (parser.lpRichEditOle)
1102 IRichEditOle_Release(parser.lpRichEditOle);
1103
1104 if (!inStream.editstream->dwError && parser.stackTop > 0)
1105 inStream.editstream->dwError = HRESULT_FROM_WIN32(ERROR_HANDLE_EOF);
1106
1107 /* Remove last line break, as mandated by tests. This is not affected by
1108 CR/LF counters, since RTF streaming presents only \para tokens, which
1109 are converted according to the standard rules: \r for 2.0, \r\n for 1.0
1110 */
1111 if (stripLastCR) {
1112 int newfrom, newto;
1113 ME_GetSelection(editor, &newfrom, &newto);
1114 if (newto > to + (editor->bEmulateVersion10 ? 1 : 0)) {
1115 WCHAR lastchar[3] = {'\0', '\0'};
1116 int linebreakSize = editor->bEmulateVersion10 ? 2 : 1;
1117
1118 ME_GetTextW(editor, lastchar, newto - linebreakSize, linebreakSize, 0);
1119 if (lastchar[0] == '\r' && (lastchar[1] == '\n' || lastchar[1] == '\0')) {
1120 ME_InternalDeleteText(editor, newto - linebreakSize, linebreakSize);
1121 }
1122 }
1123 }
1124
1125 style = parser.style;
1126 }
1127 else if (format & SF_TEXT)
1128 ME_StreamInText(editor, format, &inStream, style);
1129 else
1130 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
1131 ME_GetSelection(editor, &to, &to2);
1132 /* put the cursor at the top */
1133 if (!(format & SFF_SELECTION))
1134 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
1135 }
1136
1137 /* Restore saved undo mode */
1138 editor->nUndoMode = nUndoMode;
1139
1140 /* even if we didn't add an undo, we need to commit anything on the stack */
1141 ME_CommitUndo(editor);
1142
1143 /* If SFF_SELECTION isn't set, delete any undos from before we started too */
1144 if (!(format & SFF_SELECTION))
1145 ME_EmptyUndoStack(editor);
1146
1147 ME_ReleaseStyle(style);
1148 editor->nEventMask = nEventMask;
1149 ME_UpdateRepaint(editor);
1150 if (!(format & SFF_SELECTION)) {
1151 ME_ClearTempStyle(editor);
1152 }
1153 HideCaret(editor->hWnd);
1154 ME_MoveCaret(editor);
1155 ShowCaret(editor->hWnd);
1156 ME_SendSelChange(editor);
1157 ME_SendRequestResize(editor, FALSE);
1158
1159 return 0;
1160 }
1161
1162
1163 typedef struct tagME_RTFStringStreamStruct
1164 {
1165 char *string;
1166 int pos;
1167 int length;
1168 } ME_RTFStringStreamStruct;
1169
1170 static DWORD CALLBACK ME_ReadFromRTFString(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1171 {
1172 ME_RTFStringStreamStruct *pStruct = (ME_RTFStringStreamStruct *)dwCookie;
1173 int count;
1174
1175 count = min(cb, pStruct->length - pStruct->pos);
1176 memmove(lpBuff, pStruct->string + pStruct->pos, count);
1177 pStruct->pos += count;
1178 *pcb = count;
1179 return 0;
1180 }
1181
1182 static void
1183 ME_StreamInRTFString(ME_TextEditor *editor, BOOL selection, char *string)
1184 {
1185 EDITSTREAM es;
1186 ME_RTFStringStreamStruct data;
1187
1188 data.string = string;
1189 data.length = strlen(string);
1190 data.pos = 0;
1191 es.dwCookie = (DWORD)&data;
1192 es.pfnCallback = ME_ReadFromRTFString;
1193 ME_StreamIn(editor, SF_RTF | (selection ? SFF_SELECTION : 0), &es, FALSE);
1194 }
1195
1196
1197 ME_DisplayItem *
1198 ME_FindItemAtOffset(ME_TextEditor *editor, ME_DIType nItemType, int nOffset, int *nItemOffset)
1199 {
1200 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
1201 int runLength;
1202
1203 while (item && item->member.para.next_para->member.para.nCharOfs <= nOffset)
1204 item = ME_FindItemFwd(item, diParagraph);
1205
1206 if (!item)
1207 return item;
1208
1209 nOffset -= item->member.para.nCharOfs;
1210 if (nItemType == diParagraph) {
1211 if (nItemOffset)
1212 *nItemOffset = nOffset;
1213 return item;
1214 }
1215
1216 do {
1217 item = ME_FindItemFwd(item, diRun);
1218 runLength = ME_StrLen(item->member.run.strText);
1219 if (item->member.run.nFlags & MERF_ENDPARA)
1220 runLength = item->member.run.nCR + item->member.run.nLF;
1221 } while (item && (item->member.run.nCharOfs + runLength <= nOffset));
1222 if (item) {
1223 nOffset -= item->member.run.nCharOfs;
1224
1225 /* Special case: nOffset may not point exactly at the division between the
1226 \r and the \n in 1.0 emulation. If such a case happens, it is sent
1227 into the next run, if one exists
1228 */
1229 if ( item->member.run.nFlags & MERF_ENDPARA
1230 && nOffset == item->member.run.nCR
1231 && item->member.run.nLF > 0) {
1232 ME_DisplayItem *nextItem;
1233 nextItem = ME_FindItemFwd(item, diRun);
1234 if (nextItem) {
1235 nOffset = 0;
1236 item = nextItem;
1237 }
1238 }
1239 if (nItemOffset)
1240 *nItemOffset = nOffset;
1241 }
1242 return item;
1243 }
1244
1245
1246 static int
1247 ME_FindText(ME_TextEditor *editor, DWORD flags, const CHARRANGE *chrg, const WCHAR *text, CHARRANGE *chrgText)
1248 {
1249 const int nLen = lstrlenW(text);
1250 const int nTextLen = ME_GetTextLength(editor);
1251 int nStart, nEnd;
1252 int nMin, nMax;
1253 ME_DisplayItem *item;
1254 ME_DisplayItem *para;
1255 WCHAR wLastChar = ' ';
1256
1257 TRACE("flags==0x%08x, chrg->cpMin==%d, chrg->cpMax==%d text==%s\n",
1258 flags, chrg->cpMin, chrg->cpMax, debugstr_w(text));
1259
1260 if (flags & ~(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD))
1261 FIXME("Flags 0x%08x not implemented\n",
1262 flags & ~(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD));
1263
1264 nMin = chrg->cpMin;
1265 if (chrg->cpMax == -1)
1266 nMax = nTextLen;
1267 else
1268 nMax = chrg->cpMax > nTextLen ? nTextLen : chrg->cpMax;
1269
1270 /* In 1.0 emulation, if cpMax reaches end of text, add the FR_DOWN flag */
1271 if (editor->bEmulateVersion10 && nMax == nTextLen)
1272 {
1273 flags |= FR_DOWN;
1274 }
1275
1276 /* In 1.0 emulation, cpMin must always be no greater than cpMax */
1277 if (editor->bEmulateVersion10 && nMax < nMin)
1278 {
1279 if (chrgText)
1280 {
1281 chrgText->cpMin = -1;
1282 chrgText->cpMax = -1;
1283 }
1284 return -1;
1285 }
1286
1287 /* when searching up, if cpMin < cpMax, then instead of searching
1288 * on [cpMin,cpMax], we search on [0,cpMin], otherwise, search on
1289 * [cpMax, cpMin]. The exception is when cpMax is -1, in which
1290 * case, it is always bigger than cpMin.
1291 */
1292 if (!editor->bEmulateVersion10 && !(flags & FR_DOWN))
1293 {
1294 int nSwap = nMax;
1295
1296 nMax = nMin > nTextLen ? nTextLen : nMin;
1297 if (nMin < nSwap || chrg->cpMax == -1)
1298 nMin = 0;
1299 else
1300 nMin = nSwap;
1301 }
1302
1303 if (!nLen || nMin < 0 || nMax < 0 || nMax < nMin)
1304 {
1305 if (chrgText)
1306 chrgText->cpMin = chrgText->cpMax = -1;
1307 return -1;
1308 }
1309
1310 if (flags & FR_DOWN) /* Forward search */
1311 {
1312 /* If possible, find the character before where the search starts */
1313 if ((flags & FR_WHOLEWORD) && nMin)
1314 {
1315 nStart = nMin - 1;
1316 item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
1317 if (!item)
1318 {
1319 if (chrgText)
1320 chrgText->cpMin = chrgText->cpMax = -1;
1321 return -1;
1322 }
1323 wLastChar = item->member.run.strText->szData[nStart];
1324 }
1325
1326 nStart = nMin;
1327 item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
1328 if (!item)
1329 {
1330 if (chrgText)
1331 chrgText->cpMin = chrgText->cpMax = -1;
1332 return -1;
1333 }
1334
1335 para = ME_GetParagraph(item);
1336 while (item
1337 && para->member.para.nCharOfs + item->member.run.nCharOfs + nStart + nLen <= nMax)
1338 {
1339 ME_DisplayItem *pCurItem = item;
1340 int nCurStart = nStart;
1341 int nMatched = 0;
1342
1343 while (pCurItem && ME_CharCompare(pCurItem->member.run.strText->szData[nCurStart + nMatched], text[nMatched], (flags & FR_MATCHCASE)))
1344 {
1345 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
1346 break;
1347
1348 nMatched++;
1349 if (nMatched == nLen)
1350 {
1351 ME_DisplayItem *pNextItem = pCurItem;
1352 int nNextStart = nCurStart;
1353 WCHAR wNextChar;
1354
1355 /* Check to see if next character is a whitespace */
1356 if (flags & FR_WHOLEWORD)
1357 {
1358 if (nCurStart + nMatched == ME_StrLen(pCurItem->member.run.strText))
1359 {
1360 pNextItem = ME_FindItemFwd(pCurItem, diRun);
1361 nNextStart = -nMatched;
1362 }
1363
1364 if (pNextItem)
1365 wNextChar = pNextItem->member.run.strText->szData[nNextStart + nMatched];
1366 else
1367 wNextChar = ' ';
1368
1369 if (isalnumW(wNextChar))
1370 break;
1371 }
1372
1373 nStart += para->member.para.nCharOfs + item->member.run.nCharOfs;
1374 if (chrgText)
1375 {
1376 chrgText->cpMin = nStart;
1377 chrgText->cpMax = nStart + nLen;
1378 }
1379 TRACE("found at %d-%d\n", nStart, nStart + nLen);
1380 return nStart;
1381 }
1382 if (nCurStart + nMatched == ME_StrLen(pCurItem->member.run.strText))
1383 {
1384 pCurItem = ME_FindItemFwd(pCurItem, diRun);
1385 para = ME_GetParagraph(pCurItem);
1386 nCurStart = -nMatched;
1387 }
1388 }
1389 if (pCurItem)
1390 wLastChar = pCurItem->member.run.strText->szData[nCurStart + nMatched];
1391 else
1392 wLastChar = ' ';
1393
1394 nStart++;
1395 if (nStart == ME_StrLen(item->member.run.strText))
1396 {
1397 item = ME_FindItemFwd(item, diRun);
1398 para = ME_GetParagraph(item);
1399 nStart = 0;
1400 }
1401 }
1402 }
1403 else /* Backward search */
1404 {
1405 /* If possible, find the character after where the search ends */
1406 if ((flags & FR_WHOLEWORD) && nMax < nTextLen - 1)
1407 {
1408 nEnd = nMax + 1;
1409 item = ME_FindItemAtOffset(editor, diRun, nEnd, &nEnd);
1410 if (!item)
1411 {
1412 if (chrgText)
1413 chrgText->cpMin = chrgText->cpMax = -1;
1414 return -1;
1415 }
1416 wLastChar = item->member.run.strText->szData[nEnd];
1417 }
1418
1419 nEnd = nMax;
1420 item = ME_FindItemAtOffset(editor, diRun, nEnd, &nEnd);
1421 if (!item)
1422 {
1423 if (chrgText)
1424 chrgText->cpMin = chrgText->cpMax = -1;
1425 return -1;
1426 }
1427
1428 para = ME_GetParagraph(item);
1429
1430 while (item
1431 && para->member.para.nCharOfs + item->member.run.nCharOfs + nEnd - nLen >= nMin)
1432 {
1433 ME_DisplayItem *pCurItem = item;
1434 int nCurEnd = nEnd;
1435 int nMatched = 0;
1436
1437 if (nCurEnd - nMatched == 0)
1438 {
1439 pCurItem = ME_FindItemBack(pCurItem, diRun);
1440 para = ME_GetParagraph(pCurItem);
1441 nCurEnd = ME_StrLen(pCurItem->member.run.strText) + nMatched;
1442 }
1443
1444 while (pCurItem && ME_CharCompare(pCurItem->member.run.strText->szData[nCurEnd - nMatched - 1], text[nLen - nMatched - 1], (flags & FR_MATCHCASE)))
1445 {
1446 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
1447 break;
1448
1449 nMatched++;
1450 if (nMatched == nLen)
1451 {
1452 ME_DisplayItem *pPrevItem = pCurItem;
1453 int nPrevEnd = nCurEnd;
1454 WCHAR wPrevChar;
1455
1456 /* Check to see if previous character is a whitespace */
1457 if (flags & FR_WHOLEWORD)
1458 {
1459 if (nPrevEnd - nMatched == 0)
1460 {
1461 pPrevItem = ME_FindItemBack(pCurItem, diRun);
1462 if (pPrevItem)
1463 nPrevEnd = ME_StrLen(pPrevItem->member.run.strText) + nMatched;
1464 }
1465
1466 if (pPrevItem)
1467 wPrevChar = pPrevItem->member.run.strText->szData[nPrevEnd - nMatched - 1];
1468 else
1469 wPrevChar = ' ';
1470
1471 if (isalnumW(wPrevChar))
1472 break;
1473 }
1474
1475 nStart = para->member.para.nCharOfs + pCurItem->member.run.nCharOfs + nCurEnd - nMatched;
1476 if (chrgText)
1477 {
1478 chrgText->cpMin = nStart;
1479 chrgText->cpMax = nStart + nLen;
1480 }
1481 TRACE("found at %d-%d\n", nStart, nStart + nLen);
1482 return nStart;
1483 }
1484 if (nCurEnd - nMatched == 0)
1485 {
1486 pCurItem = ME_FindItemBack(pCurItem, diRun);
1487 /* Don't care about pCurItem becoming NULL here; it's already taken
1488 * care of in the exterior loop condition */
1489 para = ME_GetParagraph(pCurItem);
1490 nCurEnd = ME_StrLen(pCurItem->member.run.strText) + nMatched;
1491 }
1492 }
1493 if (pCurItem)
1494 wLastChar = pCurItem->member.run.strText->szData[nCurEnd - nMatched - 1];
1495 else
1496 wLastChar = ' ';
1497
1498 nEnd--;
1499 if (nEnd < 0)
1500 {
1501 item = ME_FindItemBack(item, diRun);
1502 para = ME_GetParagraph(item);
1503 nEnd = ME_StrLen(item->member.run.strText);
1504 }
1505 }
1506 }
1507 TRACE("not found\n");
1508 if (chrgText)
1509 chrgText->cpMin = chrgText->cpMax = -1;
1510 return -1;
1511 }
1512
1513 /* helper to send a msg filter notification */
1514 static BOOL
1515 ME_FilterEvent(ME_TextEditor *editor, UINT msg, WPARAM* wParam, LPARAM* lParam)
1516 {
1517 MSGFILTER msgf;
1518
1519 msgf.nmhdr.hwndFrom = editor->hWnd;
1520 msgf.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
1521 msgf.nmhdr.code = EN_MSGFILTER;
1522 msgf.msg = msg;
1523
1524 msgf.wParam = *wParam;
1525 msgf.lParam = *lParam;
1526 if (SendMessageW(GetParent(editor->hWnd), WM_NOTIFY, msgf.nmhdr.idFrom, (LPARAM)&msgf))
1527 return FALSE;
1528 *wParam = msgf.wParam;
1529 *lParam = msgf.lParam;
1530 msgf.wParam = *wParam;
1531
1532 return TRUE;
1533 }
1534
1535 static BOOL
1536 ME_KeyDown(ME_TextEditor *editor, WORD nKey)
1537 {
1538 BOOL ctrl_is_down = GetKeyState(VK_CONTROL) & 0x8000;
1539 BOOL shift_is_down = GetKeyState(VK_SHIFT) & 0x8000;
1540
1541 switch (nKey)
1542 {
1543 case VK_LEFT:
1544 case VK_RIGHT:
1545 case VK_UP:
1546 case VK_DOWN:
1547 case VK_HOME:
1548 case VK_END:
1549 case VK_PRIOR:
1550 case VK_NEXT:
1551 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
1552 ME_ArrowKey(editor, nKey, shift_is_down, ctrl_is_down);
1553 return TRUE;
1554 case VK_BACK:
1555 case VK_DELETE:
1556 /* FIXME backspace and delete aren't the same, they act different wrt paragraph style of the merged paragraph */
1557 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY)
1558 return FALSE;
1559 if (ME_IsSelection(editor))
1560 {
1561 ME_DeleteSelection(editor);
1562 ME_CommitUndo(editor);
1563 }
1564 else if (nKey == VK_DELETE)
1565 {
1566 /* Delete stops group typing.
1567 * (See MSDN remarks on EM_STOPGROUPTYPING message) */
1568 ME_DeleteTextAtCursor(editor, 1, 1);
1569 ME_CommitUndo(editor);
1570 }
1571 else if (ME_ArrowKey(editor, VK_LEFT, FALSE, FALSE))
1572 {
1573 /* Backspace can be grouped for a single undo */
1574 ME_ContinueCoalescingTransaction(editor);
1575 ME_DeleteTextAtCursor(editor, 1, 1);
1576 ME_CommitCoalescingUndo(editor);
1577 }
1578 else
1579 return TRUE;
1580 ME_UpdateSelectionLinkAttribute(editor);
1581 ME_UpdateRepaint(editor);
1582 ME_SendRequestResize(editor, FALSE);
1583 return TRUE;
1584
1585 default:
1586 if (ctrl_is_down)
1587 {
1588 if (nKey == 'W')
1589 {
1590 CHARFORMAT2W chf;
1591 char buf[2048];
1592 chf.cbSize = sizeof(chf);
1593
1594 ME_GetSelectionCharFormat(editor, &chf);
1595 ME_DumpStyleToBuf(&chf, buf);
1596 MessageBoxA(NULL, buf, "Style dump", MB_OK);
1597 }
1598 if (nKey == 'Q')
1599 {
1600 ME_CheckCharOffsets(editor);
1601 }
1602 }
1603 }
1604 return FALSE;
1605 }
1606
1607 static BOOL ME_SetCursor(ME_TextEditor *editor, int x)
1608 {
1609 if ((GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_SELECTIONBAR) &&
1610 (x < editor->selofs || editor->linesel))
1611 {
1612 SetCursor(hLeft);
1613 return TRUE;
1614 }
1615 return FALSE;
1616 }
1617
1618 static BOOL ME_ShowContextMenu(ME_TextEditor *editor, int x, int y)
1619 {
1620 CHARRANGE selrange;
1621 HMENU menu;
1622 int seltype = 0;
1623 if(!editor->lpOleCallback)
1624 return FALSE;
1625 ME_GetSelection(editor, &selrange.cpMin, &selrange.cpMax);
1626 if(selrange.cpMin == selrange.cpMax)
1627 seltype |= SEL_EMPTY;
1628 else
1629 {
1630 /* FIXME: Handle objects */
1631 seltype |= SEL_TEXT;
1632 if(selrange.cpMax-selrange.cpMin > 1)
1633 seltype |= SEL_MULTICHAR;
1634 }
1635 if(SUCCEEDED(IRichEditOleCallback_GetContextMenu(editor->lpOleCallback, seltype, NULL, &selrange, &menu)))
1636 {
1637 TrackPopupMenu(menu, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, GetParent(editor->hWnd), NULL);
1638 DestroyMenu(menu);
1639 }
1640 return TRUE;
1641 }
1642
1643 ME_TextEditor *ME_MakeEditor(HWND hWnd) {
1644 ME_TextEditor *ed = ALLOC_OBJ(ME_TextEditor);
1645 int i;
1646 ed->hWnd = hWnd;
1647 ed->bEmulateVersion10 = FALSE;
1648 ed->pBuffer = ME_MakeText();
1649 ed->nZoomNumerator = ed->nZoomDenominator = 0;
1650 ME_MakeFirstParagraph(ed);
1651 ed->bCaretShown = FALSE;
1652 ed->nCursors = 4;
1653 ed->pCursors = ALLOC_N_OBJ(ME_Cursor, ed->nCursors);
1654 ed->pCursors[0].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
1655 ed->pCursors[0].nOffset = 0;
1656 ed->pCursors[1].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
1657 ed->pCursors[1].nOffset = 0;
1658 ed->pCursors[2] = ed->pCursors[0];
1659 ed->pCursors[3] = ed->pCursors[1];
1660 ed->nLastTotalLength = ed->nTotalLength = 0;
1661 ed->nHeight = 0;
1662 ed->nUDArrowX = -1;
1663 ed->nSequence = 0;
1664 ed->rgbBackColor = -1;
1665 ed->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
1666 ed->bCaretAtEnd = FALSE;
1667 ed->nEventMask = 0;
1668 ed->nModifyStep = 0;
1669 ed->nTextLimit = TEXT_LIMIT_DEFAULT;
1670 ed->pUndoStack = ed->pRedoStack = ed->pUndoStackBottom = NULL;
1671 ed->nUndoStackSize = 0;
1672 ed->nUndoLimit = STACK_SIZE_DEFAULT;
1673 ed->nUndoMode = umAddToUndo;
1674 ed->nParagraphs = 1;
1675 ed->nLastSelStart = ed->nLastSelEnd = 0;
1676 ed->pLastSelStartPara = ed->pLastSelEndPara = ME_FindItemFwd(ed->pBuffer->pFirst, diParagraph);
1677 ed->bRedraw = TRUE;
1678 ed->bWordWrap = (GetWindowLongW(hWnd, GWL_STYLE) & WS_HSCROLL) ? FALSE : TRUE;
1679 ed->bHideSelection = FALSE;
1680 ed->nInvalidOfs = -1;
1681 ed->pfnWordBreak = NULL;
1682 ed->lpOleCallback = NULL;
1683 ed->mode = TM_RICHTEXT | TM_MULTILEVELUNDO | TM_MULTICODEPAGE;
1684 ed->AutoURLDetect_bEnable = FALSE;
1685 ed->bHaveFocus = FALSE;
1686 for (i=0; i<HFONT_CACHE_SIZE; i++)
1687 {
1688 ed->pFontCache[i].nRefs = 0;
1689 ed->pFontCache[i].nAge = 0;
1690 ed->pFontCache[i].hFont = NULL;
1691 }
1692
1693 ME_CheckCharOffsets(ed);
1694 if (GetWindowLongW(hWnd, GWL_STYLE) & ES_SELECTIONBAR)
1695 ed->selofs = 16;
1696 else
1697 ed->selofs = 0;
1698 ed->linesel = 0;
1699
1700 if (GetWindowLongW(hWnd, GWL_STYLE) & ES_PASSWORD)
1701 ed->cPasswordMask = '*';
1702 else
1703 ed->cPasswordMask = 0;
1704
1705 ed->notified_cr.cpMin = ed->notified_cr.cpMax = 0;
1706
1707 return ed;
1708 }
1709
1710 typedef struct tagME_GlobalDestStruct
1711 {
1712 HGLOBAL hData;
1713 int nLength;
1714 } ME_GlobalDestStruct;
1715
1716 static DWORD CALLBACK ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1717 {
1718 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
1719 int i;
1720 WORD *pSrc, *pDest;
1721
1722 cb = cb >> 1;
1723 pDest = (WORD *)lpBuff;
1724 pSrc = (WORD *)GlobalLock(pData->hData);
1725 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
1726 pDest[i] = pSrc[pData->nLength+i];
1727 }
1728 pData->nLength += i;
1729 *pcb = 2*i;
1730 GlobalUnlock(pData->hData);
1731 return 0;
1732 }
1733
1734 static DWORD CALLBACK ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1735 {
1736 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
1737 int i;
1738 BYTE *pSrc, *pDest;
1739
1740 pDest = lpBuff;
1741 pSrc = (BYTE *)GlobalLock(pData->hData);
1742 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
1743 pDest[i] = pSrc[pData->nLength+i];
1744 }
1745 pData->nLength += i;
1746 *pcb = i;
1747 GlobalUnlock(pData->hData);
1748 return 0;
1749 }
1750
1751
1752 void ME_DestroyEditor(ME_TextEditor *editor)
1753 {
1754 ME_DisplayItem *pFirst = editor->pBuffer->pFirst;
1755 ME_DisplayItem *p = pFirst, *pNext = NULL;
1756 int i;
1757
1758 ME_ClearTempStyle(editor);
1759 ME_EmptyUndoStack(editor);
1760 while(p) {
1761 pNext = p->next;
1762 ME_DestroyDisplayItem(p);
1763 p = pNext;
1764 }
1765 ME_ReleaseStyle(editor->pBuffer->pDefaultStyle);
1766 for (i=0; i<HFONT_CACHE_SIZE; i++)
1767 {
1768 if (editor->pFontCache[i].hFont)
1769 DeleteObject(editor->pFontCache[i].hFont);
1770 }
1771 if (editor->rgbBackColor != -1)
1772 DeleteObject(editor->hbrBackground);
1773 if(editor->lpOleCallback)
1774 IUnknown_Release(editor->lpOleCallback);
1775
1776 FREE_OBJ(editor->pBuffer);
1777 FREE_OBJ(editor->pCursors);
1778
1779 FREE_OBJ(editor);
1780 }
1781
1782 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
1783 {
1784 TRACE("\n");
1785 switch (fdwReason)
1786 {
1787 case DLL_PROCESS_ATTACH:
1788 DisableThreadLibraryCalls(hinstDLL);
1789 me_heap = HeapCreate (0, 0x10000, 0);
1790 if (!ME_RegisterEditorClass(hinstDLL)) return FALSE;
1791 hLeft = LoadCursorW(hinstDLL, MAKEINTRESOURCEW(OCR_REVERSE));
1792 LookupInit();
1793 break;
1794
1795 case DLL_PROCESS_DETACH:
1796 UnregisterClassW(RichEdit20W, 0);
1797 UnregisterClassW(RichEdit50W, 0);
1798 UnregisterClassA("RichEdit20A", 0);
1799 UnregisterClassA("RichEdit50A", 0);
1800 if (ME_ListBoxRegistered)
1801 UnregisterClassW(REListBox20W, 0);
1802 if (ME_ComboBoxRegistered)
1803 UnregisterClassW(REComboBox20W, 0);
1804 LookupCleanup();
1805 HeapDestroy (me_heap);
1806 me_heap = NULL;
1807 break;
1808 }
1809 return TRUE;
1810 }
1811
1812
1813 #define UNSUPPORTED_MSG(e) \
1814 case e: \
1815 FIXME(#e ": stub\n"); \
1816 return DefWindowProcW(hWnd, msg, wParam, lParam);
1817
1818 static const char * const edit_messages[] = {
1819 "EM_GETSEL",
1820 "EM_SETSEL",
1821 "EM_GETRECT",
1822 "EM_SETRECT",
1823 "EM_SETRECTNP",
1824 "EM_SCROLL",
1825 "EM_LINESCROLL",
1826 "EM_SCROLLCARET",
1827 "EM_GETMODIFY",
1828 "EM_SETMODIFY",
1829 "EM_GETLINECOUNT",
1830 "EM_LINEINDEX",
1831 "EM_SETHANDLE",
1832 "EM_GETHANDLE",
1833 "EM_GETTHUMB",
1834 "EM_UNKNOWN_BF",
1835 "EM_UNKNOWN_C0",
1836 "EM_LINELENGTH",
1837 "EM_REPLACESEL",
1838 "EM_UNKNOWN_C3",
1839 "EM_GETLINE",
1840 "EM_LIMITTEXT",
1841 "EM_CANUNDO",
1842 "EM_UNDO",
1843 "EM_FMTLINES",
1844 "EM_LINEFROMCHAR",
1845 "EM_UNKNOWN_CA",
1846 "EM_SETTABSTOPS",
1847 "EM_SETPASSWORDCHAR",
1848 "EM_EMPTYUNDOBUFFER",
1849 "EM_GETFIRSTVISIBLELINE",
1850 "EM_SETREADONLY",
1851 "EM_SETWORDBREAKPROC",
1852 "EM_GETWORDBREAKPROC",
1853 "EM_GETPASSWORDCHAR",
1854 "EM_SETMARGINS",
1855 "EM_GETMARGINS",
1856 "EM_GETLIMITTEXT",
1857 "EM_POSFROMCHAR",
1858 "EM_CHARFROMPOS"
1859 };
1860
1861 static const char * const richedit_messages[] = {
1862 "EM_CANPASTE",
1863 "EM_DISPLAYBAND",
1864 "EM_EXGETSEL",
1865 "EM_EXLIMITTEXT",
1866 "EM_EXLINEFROMCHAR",
1867 "EM_EXSETSEL",
1868 "EM_FINDTEXT",
1869 "EM_FORMATRANGE",
1870 "EM_GETCHARFORMAT",
1871 "EM_GETEVENTMASK",
1872 "EM_GETOLEINTERFACE",
1873 "EM_GETPARAFORMAT",
1874 "EM_GETSELTEXT",
1875 "EM_HIDESELECTION",
1876 "EM_PASTESPECIAL",
1877 "EM_REQUESTRESIZE",
1878 "EM_SELECTIONTYPE",
1879 "EM_SETBKGNDCOLOR",
1880 "EM_SETCHARFORMAT",
1881 "EM_SETEVENTMASK",
1882 "EM_SETOLECALLBACK",
1883 "EM_SETPARAFORMAT",
1884 "EM_SETTARGETDEVICE",
1885 "EM_STREAMIN",
1886 "EM_STREAMOUT",
1887 "EM_GETTEXTRANGE",
1888 "EM_FINDWORDBREAK",
1889 "EM_SETOPTIONS",
1890 "EM_GETOPTIONS",
1891 "EM_FINDTEXTEX",
1892 "EM_GETWORDBREAKPROCEX",
1893 "EM_SETWORDBREAKPROCEX",
1894 "EM_SETUNDOLIMIT",
1895 "EM_UNKNOWN_USER_83",
1896 "EM_REDO",
1897 "EM_CANREDO",
1898 "EM_GETUNDONAME",
1899 "EM_GETREDONAME",
1900 "EM_STOPGROUPTYPING",
1901 "EM_SETTEXTMODE",
1902 "EM_GETTEXTMODE",
1903 "EM_AUTOURLDETECT",
1904 "EM_GETAUTOURLDETECT",
1905 "EM_SETPALETTE",
1906 "EM_GETTEXTEX",
1907 "EM_GETTEXTLENGTHEX",
1908 "EM_SHOWSCROLLBAR",
1909 "EM_SETTEXTEX",
1910 "EM_UNKNOWN_USER_98",
1911 "EM_UNKNOWN_USER_99",
1912 "EM_SETPUNCTUATION",
1913 "EM_GETPUNCTUATION",
1914 "EM_SETWORDWRAPMODE",
1915 "EM_GETWORDWRAPMODE",
1916 "EM_SETIMECOLOR",
1917 "EM_GETIMECOLOR",
1918 "EM_SETIMEOPTIONS",
1919 "EM_GETIMEOPTIONS",
1920 "EM_CONVPOSITION",
1921 "EM_UNKNOWN_USER_109",
1922 "EM_UNKNOWN_USER_110",
1923 "EM_UNKNOWN_USER_111",
1924 "EM_UNKNOWN_USER_112",
1925 "EM_UNKNOWN_USER_113",
1926 "EM_UNKNOWN_USER_114",
1927 "EM_UNKNOWN_USER_115",
1928 "EM_UNKNOWN_USER_116",
1929 "EM_UNKNOWN_USER_117",
1930 "EM_UNKNOWN_USER_118",
1931 "EM_UNKNOWN_USER_119",
1932 "EM_SETLANGOPTIONS",
1933 "EM_GETLANGOPTIONS",
1934 "EM_GETIMECOMPMODE",
1935 "EM_FINDTEXTW",
1936 "EM_FINDTEXTEXW",
1937 "EM_RECONVERSION",
1938 "EM_SETIMEMODEBIAS",
1939 "EM_GETIMEMODEBIAS"
1940 };
1941
1942 static const char *
1943 get_msg_name(UINT msg)
1944 {
1945 if (msg >= EM_GETSEL && msg <= EM_CHARFROMPOS)
1946 return edit_messages[msg - EM_GETSEL];
1947 if (msg >= EM_CANPASTE && msg <= EM_GETIMEMODEBIAS)
1948 return richedit_messages[msg - EM_CANPASTE];
1949 return "";
1950 }
1951
1952 static LRESULT RichEditWndProc_common(HWND hWnd, UINT msg, WPARAM wParam,
1953 LPARAM lParam, BOOL unicode)
1954 {
1955 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongPtrW(hWnd, 0);
1956
1957 TRACE("hwnd %p msg %04x (%s) %lx %lx, unicode %d\n",
1958 hWnd, msg, get_msg_name(msg), wParam, lParam, unicode);
1959
1960 if (!editor && msg != WM_NCCREATE && msg != WM_NCDESTROY) {
1961 ERR("called with invalid hWnd %p - application bug?\n", hWnd);
1962 return 0;
1963 }
1964
1965 switch(msg) {
1966
1967 UNSUPPORTED_MSG(EM_DISPLAYBAND)
1968 UNSUPPORTED_MSG(EM_FINDWORDBREAK)
1969 UNSUPPORTED_MSG(EM_FMTLINES)
1970 UNSUPPORTED_MSG(EM_FORMATRANGE)
1971 UNSUPPORTED_MSG(EM_GETBIDIOPTIONS)
1972 UNSUPPORTED_MSG(EM_GETEDITSTYLE)
1973 UNSUPPORTED_MSG(EM_GETIMECOMPMODE)
1974 /* UNSUPPORTED_MSG(EM_GETIMESTATUS) missing in Wine headers */
1975 UNSUPPORTED_MSG(EM_GETLANGOPTIONS)
1976 /* UNSUPPORTED_MSG(EM_GETOLEINTERFACE) separate stub */
1977 UNSUPPORTED_MSG(EM_GETREDONAME)
1978 UNSUPPORTED_MSG(EM_GETTEXTMODE)
1979 UNSUPPORTED_MSG(EM_GETTYPOGRAPHYOPTIONS)
1980 UNSUPPORTED_MSG(EM_GETUNDONAME)
1981 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX)
1982 UNSUPPORTED_MSG(EM_PASTESPECIAL)
1983 UNSUPPORTED_MSG(EM_SELECTIONTYPE)
1984 UNSUPPORTED_MSG(EM_SETBIDIOPTIONS)
1985 UNSUPPORTED_MSG(EM_SETEDITSTYLE)
1986 UNSUPPORTED_MSG(EM_SETFONTSIZE)
1987 UNSUPPORTED_MSG(EM_SETLANGOPTIONS)
1988 UNSUPPORTED_MSG(EM_SETPALETTE)
1989 UNSUPPORTED_MSG(EM_SETTABSTOPS)
1990 UNSUPPORTED_MSG(EM_SETTYPOGRAPHYOPTIONS)
1991 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX)
1992 UNSUPPORTED_MSG(WM_STYLECHANGING)
1993 UNSUPPORTED_MSG(WM_STYLECHANGED)
1994 /* UNSUPPORTED_MSG(WM_UNICHAR) FIXME missing in Wine headers */
1995
1996 /* Messages specific to Richedit controls */
1997
1998 case EM_STREAMIN:
1999 return ME_StreamIn(editor, wParam, (EDITSTREAM*)lParam, TRUE);
2000 case EM_STREAMOUT:
2001 return ME_StreamOut(editor, wParam, (EDITSTREAM *)lParam);
2002 case WM_GETDLGCODE:
2003 {
2004 UINT code = DLGC_WANTCHARS|DLGC_WANTARROWS;
2005 if(lParam && (((LPMSG)lParam)->message == WM_KEYDOWN))
2006 {
2007 int vk = (int)((LPMSG)lParam)->wParam;
2008 /* if style says we want return key */
2009 if((vk == VK_RETURN) && (GetWindowLongW(hWnd, GWL_STYLE) & ES_WANTRETURN))
2010 {
2011 code |= DLGC_WANTMESSAGE;
2012 }
2013 /* we always handle ctrl-tab */
2014 if((vk == VK_TAB) && (GetKeyState(VK_CONTROL) & 0x8000))
2015 {
2016 code |= DLGC_WANTMESSAGE;
2017 }
2018 }
2019 return code;
2020 }
2021 case WM_NCCREATE:
2022 {
2023 CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
2024 TRACE("WM_NCCREATE: style 0x%08x\n", pcs->style);
2025 editor = ME_MakeEditor(hWnd);
2026 SetWindowLongPtrW(hWnd, 0, (LONG_PTR)editor);
2027 return TRUE;
2028 }
2029 case EM_EMPTYUNDOBUFFER:
2030 ME_EmptyUndoStack(editor);
2031 return 0;
2032 case EM_GETSEL:
2033 {
2034 /* Note: wParam/lParam can be NULL */
2035 UINT from, to;
2036 PUINT pfrom = wParam ? (PUINT)wParam : &from;
2037 PUINT pto = lParam ? (PUINT)lParam : &to;
2038 ME_GetSelection(editor, (int *)pfrom, (int *)pto);
2039 if ((*pfrom|*pto) & 0xFFFF0000)
2040 return -1;
2041 return MAKELONG(*pfrom,*pto);
2042 }
2043 case EM_EXGETSEL:
2044 {
2045 CHARRANGE *pRange = (CHARRANGE *)lParam;
2046 ME_GetSelection(editor, &pRange->cpMin, &pRange->cpMax);
2047 TRACE("EM_EXGETSEL = (%d,%d)\n", pRange->cpMin, pRange->cpMax);
2048 return 0;
2049 }
2050 case EM_SETUNDOLIMIT:
2051 {
2052 if ((int)wParam < 0)
2053 editor->nUndoLimit = STACK_SIZE_DEFAULT;
2054 else
2055 editor->nUndoLimit = min(wParam, STACK_SIZE_MAX);
2056 /* Setting a max stack size keeps wine from getting killed
2057 for hogging memory. Windows allocates all this memory at once, so
2058 no program would realistically set a value above our maximum. */
2059 return editor->nUndoLimit;
2060 }
2061 case EM_CANUNDO:
2062 return editor->pUndoStack != NULL;
2063 case EM_CANREDO:
2064 return editor->pRedoStack != NULL;
2065 case WM_UNDO: /* FIXME: actually not the same */
2066 case EM_UNDO:
2067 return ME_Undo(editor);
2068 case EM_REDO:
2069 return ME_Redo(editor);
2070 case EM_GETOPTIONS:
2071 {
2072 /* these flags are equivalent to the ES_* counterparts */
2073 DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
2074 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN;
2075 DWORD settings = GetWindowLongW(hWnd, GWL_STYLE) & mask;
2076
2077 return settings;
2078 }
2079 case EM_SETOPTIONS:
2080 {
2081 /* these flags are equivalent to ES_* counterparts
2082 * ECO_READONLY is already implemented in the code, only requires
2083 * setting the bit to work
2084 */
2085 DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
2086 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN | ECO_SELECTIONBAR;
2087 DWORD raw = GetWindowLongW(hWnd, GWL_STYLE);
2088 DWORD settings = mask & raw;
2089
2090 switch(wParam)
2091 {
2092 case ECOOP_SET:
2093 settings = lParam;
2094 break;
2095 case ECOOP_OR:
2096 settings |= lParam;
2097 break;
2098 case ECOOP_AND:
2099 settings &= lParam;
2100 break;
2101 case ECOOP_XOR:
2102 settings ^= lParam;
2103 }
2104 SetWindowLongW(hWnd, GWL_STYLE, (raw & ~mask) | (settings & mask));
2105
2106 if (settings & ECO_AUTOWORDSELECTION)
2107 FIXME("ECO_AUTOWORDSELECTION not implemented yet!\n");
2108 if (settings & ECO_SELECTIONBAR)
2109 editor->selofs = 16;
2110 else
2111 editor->selofs = 0;
2112 ME_WrapMarkedParagraphs(editor);
2113
2114 if (settings & ECO_VERTICAL)
2115 FIXME("ECO_VERTICAL not implemented yet!\n");
2116 if (settings & ECO_AUTOHSCROLL)
2117 FIXME("ECO_AUTOHSCROLL not implemented yet!\n");
2118 if (settings & ECO_AUTOVSCROLL)
2119 FIXME("ECO_AUTOVSCROLL not implemented yet!\n");
2120 if (settings & ECO_NOHIDESEL)
2121 FIXME("ECO_NOHIDESEL not implemented yet!\n");
2122 if (settings & ECO_WANTRETURN)
2123 FIXME("ECO_WANTRETURN not implemented yet!\n");
2124
2125 return settings;
2126 }
2127 case EM_SETSEL:
2128 {
2129 ME_InvalidateSelection(editor);
2130 ME_SetSelection(editor, wParam, lParam);
2131 ME_InvalidateSelection(editor);
2132 ME_SendSelChange(editor);
2133 return 0;
2134 }
2135 case EM_SETSCROLLPOS:
2136 {
2137 POINT *point = (POINT *)lParam;
2138 ME_ScrollAbs(editor, point->y);
2139 return 0;
2140 }
2141 case EM_AUTOURLDETECT:
2142 {
2143 if (wParam==1 || wParam ==0)
2144 {
2145 editor->AutoURLDetect_bEnable = (BOOL)wParam;
2146 return 0;
2147 }
2148 return E_INVALIDARG;
2149 }
2150 case EM_GETAUTOURLDETECT:
2151 {
2152 return editor->AutoURLDetect_bEnable;
2153 }
2154 case EM_EXSETSEL:
2155 {
2156 int end;
2157 CHARRANGE range = *(CHARRANGE *)lParam;
2158
2159 TRACE("EM_EXSETSEL (%d,%d)\n", range.cpMin, range.cpMax);
2160
2161 ME_InvalidateSelection(editor);
2162 end = ME_SetSelection(editor, range.cpMin, range.cpMax);
2163 ME_InvalidateSelection(editor);
2164 ME_SendSelChange(editor);
2165
2166 return end;
2167 }
2168 case EM_SHOWSCROLLBAR:
2169 {
2170 ShowScrollBar(editor->hWnd, wParam, lParam);
2171 return 0;
2172 }
2173 case EM_SETTEXTEX:
2174 {
2175 LPWSTR wszText;
2176 SETTEXTEX *pStruct = (SETTEXTEX *)wParam;
2177 size_t len;
2178 int from, to;
2179 ME_Style *style;
2180 int oldModify = editor->nModifyStep;
2181
2182 if (!pStruct) return 0;
2183
2184 TRACE("EM_SETTEXTEX - %s, flags %d, cp %d\n",
2185 pStruct->codepage == 1200 ? debugstr_w((LPCWSTR)lParam) : debugstr_a((LPCSTR)lParam),
2186 pStruct->flags, pStruct->codepage);
2187
2188 /* FIXME: make use of pStruct->codepage in the to unicode translation */
2189 wszText = lParam ? ME_ToUnicode(pStruct->codepage == 1200, (void *)lParam) : NULL;
2190 len = wszText ? lstrlenW(wszText) : 0;
2191
2192 if (pStruct->flags & ST_SELECTION) {
2193 ME_GetSelection(editor, &from, &to);
2194 style = ME_GetSelectionInsertStyle(editor);
2195 ME_InternalDeleteText(editor, from, to - from);
2196 if (pStruct->codepage != 1200 && lParam && !strncmp((char *)lParam, "{\\rtf", 5))
2197 ME_StreamInRTFString(editor, 0, (char *)lParam);
2198 else ME_InsertTextFromCursor(editor, 0, wszText, len, style);
2199 ME_ReleaseStyle(style);
2200
2201 if (editor->AutoURLDetect_bEnable) ME_UpdateSelectionLinkAttribute(editor);
2202 }
2203 else {
2204 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
2205 if (pStruct->codepage != 1200 && lParam && !strncmp((char *)lParam, "{\\rtf", 5))
2206 ME_StreamInRTFString(editor, 0, (char *)lParam);
2207 else ME_InsertTextFromCursor(editor, 0, wszText, len, editor->pBuffer->pDefaultStyle);
2208 len = 1;
2209
2210 if (editor->AutoURLDetect_bEnable) ME_UpdateLinkAttribute(editor, 0, -1);
2211 }
2212 ME_CommitUndo(editor);
2213 if (!(pStruct->flags & ST_KEEPUNDO))
2214 {
2215 editor->nModifyStep = oldModify;
2216 ME_EmptyUndoStack(editor);
2217 }
2218 ME_UpdateRepaint(editor);
2219 return len;
2220 }
2221 case EM_SETBKGNDCOLOR:
2222 {
2223 LRESULT lColor;
2224 if (editor->rgbBackColor != -1) {
2225 DeleteObject(editor->hbrBackground);
2226 lColor = editor->rgbBackColor;
2227 }
2228 else lColor = GetSysColor(COLOR_WINDOW);
2229
2230 if (wParam)
2231 {
2232 editor->rgbBackColor = -1;
2233 editor->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
2234 }
2235 else
2236 {
2237 editor->rgbBackColor = lParam;
2238 editor->hbrBackground = CreateSolidBrush(editor->rgbBackColor);
2239 }
2240 if (editor->bRedraw)
2241 {
2242 InvalidateRect(hWnd, NULL, TRUE);
2243 UpdateWindow(hWnd);
2244 }
2245 return lColor;
2246 }
2247 case EM_GETMODIFY:
2248 return editor->nModifyStep == 0 ? 0 : -1;
2249 case EM_SETMODIFY:
2250 {
2251 if (wParam)
2252 editor->nModifyStep = 1;
2253 else
2254 editor->nModifyStep = 0;
2255
2256 return 0;
2257 }
2258 case EM_SETREADONLY:
2259 {
2260 long nStyle = GetWindowLongW(hWnd, GWL_STYLE);
2261 if (wParam)
2262 nStyle |= ES_READONLY;
2263 else
2264 nStyle &= ~ES_READONLY;
2265 SetWindowLongW(hWnd, GWL_STYLE, nStyle);
2266 return 0;
2267 }
2268 case EM_SETEVENTMASK:
2269 {
2270 DWORD nOldMask = editor->nEventMask;
2271
2272 editor->nEventMask = lParam;
2273 return nOldMask;
2274 }
2275 case EM_GETEVENTMASK:
2276 return editor->nEventMask;
2277 case EM_SETCHARFORMAT:
2278 {
2279 CHARFORMAT2W buf, *p;
2280 BOOL bRepaint = TRUE;
2281 p = ME_ToCF2W(&buf, (CHARFORMAT2W *)lParam);
2282 if (p == NULL) return 0;
2283 if (!wParam)
2284 ME_SetDefaultCharFormat(editor, p);
2285 else if (wParam == (SCF_WORD | SCF_SELECTION)) {
2286 FIXME("EM_SETCHARFORMAT: word selection not supported\n");
2287 return 0;
2288 } else if (wParam == SCF_ALL) {
2289 if (editor->mode & TM_PLAINTEXT)
2290 ME_SetDefaultCharFormat(editor, p);
2291 else {
2292 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), p);
2293 editor->nModifyStep = 1;
2294 }
2295 } else if (editor->mode & TM_PLAINTEXT) {
2296 return 0;
2297 } else {
2298 int from, to;
2299 ME_GetSelection(editor, &from, &to);
2300 bRepaint = (from != to);
2301 ME_SetSelectionCharFormat(editor, p);
2302 if (from != to) editor->nModifyStep = 1;
2303 }
2304 ME_CommitUndo(editor);
2305 if (bRepaint)
2306 ME_RewrapRepaint(editor);
2307 return 1;
2308 }
2309 case EM_GETCHARFORMAT:
2310 {
2311 CHARFORMAT2W tmp, *dst = (CHARFORMAT2W *)lParam;
2312 if (dst->cbSize != sizeof(CHARFORMATA) &&
2313 dst->cbSize != sizeof(CHARFORMATW) &&
2314 dst->cbSize != sizeof(CHARFORMAT2A) &&
2315 dst->cbSize != sizeof(CHARFORMAT2W))
2316 return 0;
2317 tmp.cbSize = sizeof(tmp);
2318 if (!wParam)
2319 ME_GetDefaultCharFormat(editor, &tmp);
2320 else
2321 ME_GetSelectionCharFormat(editor, &tmp);
2322 ME_CopyToCFAny(dst, &tmp);
2323 return tmp.dwMask;
2324 }
2325 case EM_SETPARAFORMAT:
2326 ME_SetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
2327 ME_RewrapRepaint(editor);
2328 ME_CommitUndo(editor);
2329 return 0;
2330 case EM_GETPARAFORMAT:
2331 ME_GetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
2332 return 0;
2333 case EM_GETFIRSTVISIBLELINE:
2334 {
2335 ME_DisplayItem *p = editor->pBuffer->pFirst;
2336 int y = ME_GetYScrollPos(editor);
2337 int ypara = 0;
2338 int count = 0;
2339 int ystart, yend;
2340 while(p) {
2341 p = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
2342 if (p->type == diTextEnd)
2343 break;
2344 if (p->type == diParagraph) {
2345 ypara = p->member.para.nYPos;
2346 continue;
2347 }
2348 ystart = ypara + p->member.row.nYPos;
2349 yend = ystart + p->member.row.nHeight;
2350 if (y < yend) {
2351 break;
2352 }
2353 count++;
2354 }
2355 return count;
2356 }
2357 case EM_HIDESELECTION:
2358 {
2359 editor->bHideSelection = (wParam != 0);
2360 ME_InvalidateSelection(editor);
2361 return 0;
2362 }
2363 case EM_LINESCROLL:
2364 {
2365 ME_ScrollDown(editor, lParam * 8); /* FIXME follow the original */
2366 return TRUE; /* Should return false if a single line richedit control */
2367 }
2368 case WM_CLEAR:
2369 {
2370 int from, to;
2371 ME_GetSelection(editor, &from, &to);
2372 ME_InternalDeleteText(editor, from, to-from);
2373 ME_CommitUndo(editor);
2374 ME_UpdateRepaint(editor);
2375 return 0;
2376 }
2377 case EM_REPLACESEL:
2378 {
2379 int from, to;
2380 ME_Style *style;
2381 LPWSTR wszText = lParam ? ME_ToUnicode(unicode, (void *)lParam) : NULL;
2382 size_t len = wszText ? lstrlenW(wszText) : 0;
2383 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText));
2384
2385 ME_GetSelection(editor, &from, &to);
2386 style = ME_GetSelectionInsertStyle(editor);
2387 ME_InternalDeleteText(editor, from, to-from);
2388 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
2389 ME_ReleaseStyle(style);
2390 /* drop temporary style if line end */
2391 /*
2392 * FIXME question: does abc\n mean: put abc,
2393 * clear temp style, put \n? (would require a change)
2394 */
2395 if (len>0 && wszText[len-1] == '\n')
2396 ME_ClearTempStyle(editor);
2397 ME_EndToUnicode(unicode, wszText);
2398 ME_CommitUndo(editor);
2399 if (editor->AutoURLDetect_bEnable) ME_UpdateSelectionLinkAttribute(editor);
2400 if (!wParam)
2401 ME_EmptyUndoStack(editor);
2402 ME_UpdateRepaint(editor);
2403 return len;
2404 }
2405 case EM_SCROLLCARET:
2406 {
2407 int top, bottom; /* row's edges relative to document top */
2408 int nPos;
2409 ME_DisplayItem *para, *row;
2410
2411 nPos = ME_GetYScrollPos(editor);
2412 row = ME_RowStart(editor->pCursors[0].pRun);
2413 para = ME_GetParagraph(row);
2414 top = para->member.para.nYPos + row->member.row.nYPos;
2415 bottom = top + row->member.row.nHeight;
2416
2417 if (top < nPos) /* caret above window */
2418 ME_ScrollAbs(editor, top);
2419 else if (nPos + editor->sizeWindow.cy < bottom) /*below*/
2420 ME_ScrollAbs(editor, bottom - editor->sizeWindow.cy);
2421 return 0;
2422 }
2423 case WM_SETFONT:
2424 {
2425 LOGFONTW lf;
2426 CHARFORMAT2W fmt;
2427 HDC hDC;
2428 BOOL bRepaint = LOWORD(lParam);
2429
2430 if (!wParam)
2431 wParam = (WPARAM)GetStockObject(SYSTEM_FONT);
2432 GetObjectW((HGDIOBJ)wParam, sizeof(LOGFONTW), &lf);
2433 hDC = GetDC(hWnd);
2434 ME_CharFormatFromLogFont(hDC, &lf, &fmt);
2435 ReleaseDC(hWnd, hDC);
2436 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), &fmt);
2437 ME_SetDefaultCharFormat(editor, &fmt);
2438
2439 ME_CommitUndo(editor);
2440 if (bRepaint)
2441 ME_RewrapRepaint(editor);
2442 return 0;
2443 }
2444 case WM_SETTEXT:
2445 {
2446 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
2447 if (lParam)
2448 {
2449 TRACE("WM_SETTEXT lParam==%lx\n",lParam);
2450 if (!unicode && !strncmp((char *)lParam, "{\\rtf", 5))
2451 {
2452 /* Undocumented: WM_SETTEXT supports RTF text */
2453 ME_StreamInRTFString(editor, 0, (char *)lParam);
2454 }
2455 else
2456 {
2457 LPWSTR wszText = ME_ToUnicode(unicode, (void *)lParam);
2458 TRACE("WM_SETTEXT - %s\n", debugstr_w(wszText)); /* debugstr_w() */
2459 if (lstrlenW(wszText) > 0)
2460 {
2461 int len = -1;
2462
2463 /* uses default style! */
2464 if (!(GetWindowLongW(hWnd, GWL_STYLE) & ES_MULTILINE))
2465 {
2466 WCHAR * p;
2467
2468 p = wszText;
2469 while (*p != '\0' && *p != '\r' && *p != '\n') p++;
2470 len = p - wszText;
2471 }
2472 ME_InsertTextFromCursor(editor, 0, wszText, len, editor->pBuffer->pDefaultStyle);
2473 }
2474 ME_EndToUnicode(unicode, wszText);
2475 }
2476 }
2477 else
2478 TRACE("WM_SETTEXT - NULL\n");
2479 if (editor->AutoURLDetect_bEnable)
2480 {
2481 ME_UpdateLinkAttribute(editor, 0, -1);
2482 }
2483 ME_SetSelection(editor, 0, 0);
2484 editor->nModifyStep = 0;
2485 ME_CommitUndo(editor);
2486 ME_EmptyUndoStack(editor);
2487 ME_UpdateRepaint(editor);
2488 return 1;
2489 }
2490 case EM_CANPASTE:
2491 {
2492 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
2493 if (IsClipboardFormatAvailable(nRTFFormat))
2494 return TRUE;
2495 if (IsClipboardFormatAvailable(CF_UNICODETEXT))
2496 return TRUE;
2497 return FALSE;
2498 }
2499 case WM_PASTE:
2500 {
2501 DWORD dwFormat = 0;
2502 EDITSTREAM es;
2503 ME_GlobalDestStruct gds;
2504 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
2505 UINT cf = 0;
2506
2507 if (IsClipboardFormatAvailable(nRTFFormat))
2508 cf = nRTFFormat, dwFormat = SF_RTF;
2509 else if (IsClipboardFormatAvailable(CF_UNICODETEXT))
2510 cf = CF_UNICODETEXT, dwFormat = SF_TEXT|SF_UNICODE;
2511 else
2512 return 0;
2513
2514 if (!OpenClipboard(hWnd))
2515 return 0;
2516 gds.hData = GetClipboardData(cf);
2517 gds.nLength = 0;
2518 es.dwCookie = (DWORD)&gds;
2519 es.pfnCallback = dwFormat == SF_RTF ? ME_ReadFromHGLOBALRTF : ME_ReadFromHGLOBALUnicode;
2520 ME_StreamIn(editor, dwFormat|SFF_SELECTION, &es, FALSE);
2521
2522 CloseClipboard();
2523 return 0;
2524 }
2525 case WM_CUT:
2526 case WM_COPY:
2527 {
2528 LPDATAOBJECT dataObj = NULL;
2529 CHARRANGE range;
2530 HRESULT hr = S_OK;
2531
2532 if (editor->cPasswordMask)
2533 return 0; /* Copying or Cutting masked text isn't allowed */
2534
2535 ME_GetSelection(editor, &range.cpMin, &range.cpMax);
2536 if(editor->lpOleCallback)
2537 hr = IRichEditOleCallback_GetClipboardData(editor->lpOleCallback, &range, RECO_COPY, &dataObj);
2538 if(FAILED(hr) || !dataObj)
2539 hr = ME_GetDataObject(editor, &range, &dataObj);
2540 if(SUCCEEDED(hr)) {
2541 hr = OleSetClipboard(dataObj);
2542 IDataObject_Release(dataObj);
2543 }
2544 if (SUCCEEDED(hr) && msg == WM_CUT)
2545 {
2546 ME_InternalDeleteText(editor, range.cpMin, range.cpMax-range.cpMin);
2547 ME_CommitUndo(editor);
2548 ME_UpdateRepaint(editor);
2549 }
2550 return 0;
2551 }
2552 case WM_GETTEXTLENGTH:
2553 {
2554 GETTEXTLENGTHEX how;
2555
2556 /* CR/LF conversion required in 2.0 mode, verbatim in 1.0 mode */
2557 how.flags = GTL_CLOSE | (editor->bEmulateVersion10 ? 0 : GTL_USECRLF) | GTL_NUMCHARS;
2558 how.codepage = unicode ? 1200 : CP_ACP;
2559 return ME_GetTextLengthEx(editor, &how);
2560 }
2561 case EM_GETTEXTLENGTHEX:
2562 return ME_GetTextLengthEx(editor, (GETTEXTLENGTHEX *)wParam);
2563 case WM_GETTEXT:
2564 {
2565 GETTEXTEX ex;
2566 LRESULT rc;
2567 LPSTR bufferA = NULL;
2568 LPWSTR bufferW = NULL;
2569
2570 if (unicode)
2571 bufferW = heap_alloc((wParam + 2) * sizeof(WCHAR));
2572 else
2573 bufferA = heap_alloc(wParam + 2);
2574
2575 ex.cb = (wParam + 2) * (unicode ? sizeof(WCHAR) : sizeof(CHAR));
2576 ex.flags = GT_USECRLF;
2577 ex.codepage = unicode ? 1200 : CP_ACP;
2578 ex.lpDefaultChar = NULL;
2579 ex.lpUsedDefChar = NULL;
2580 rc = RichEditWndProc_common(hWnd, EM_GETTEXTEX, (WPARAM)&ex, unicode ? (LPARAM)bufferW : (LPARAM)bufferA, unicode);
2581
2582 if (unicode)
2583 {
2584 memcpy((LPWSTR)lParam, bufferW, wParam * sizeof(WCHAR));
2585 if (lstrlenW(bufferW) >= wParam) rc = 0;
2586 }
2587 else
2588 {
2589 memcpy((LPSTR)lParam, bufferA, wParam);
2590 if (strlen(bufferA) >= wParam) rc = 0;
2591 }
2592 heap_free(bufferA);
2593 heap_free(bufferW);
2594 return rc;
2595 }
2596 case EM_GETTEXTEX:
2597 {
2598 GETTEXTEX *ex = (GETTEXTEX*)wParam;
2599 int nStart, nCount; /* in chars */
2600
2601 if (ex->flags & ~(GT_SELECTION | GT_USECRLF))
2602 FIXME("GETTEXTEX flags 0x%08x not supported\n", ex->flags & ~(GT_SELECTION | GT_USECRLF));
2603
2604 if (ex->flags & GT_SELECTION)
2605 {
2606 ME_GetSelection(editor, &nStart, &nCount);
2607 nCount -= nStart;
2608 }
2609 else
2610 {
2611 nStart = 0;
2612 nCount = 0x7fffffff;
2613 }
2614 if (ex->codepage == 1200)
2615 {
2616 nCount = min(nCount, ex->cb / sizeof(WCHAR) - 1);
2617 return ME_GetTextW(editor, (LPWSTR)lParam, nStart, nCount, ex->flags & GT_USECRLF);
2618 }
2619 else
2620 {
2621 /* potentially each char may be a CR, why calculate the exact value with O(N) when
2622 we can just take a bigger buffer? :)
2623 The above assumption still holds with CR/LF counters, since CR->CRLF expansion
2624 occurs only in richedit 2.0 mode, in which line breaks have only one CR
2625 */
2626 int crlfmul = (ex->flags & GT_USECRLF) ? 2 : 1;
2627 LPWSTR buffer;
2628 DWORD buflen = ex->cb;
2629 LRESULT rc;
2630 DWORD flags = 0;
2631
2632 nCount = min(nCount, ex->cb - 1);
2633 buffer = heap_alloc((crlfmul*nCount + 1) * sizeof(WCHAR));
2634
2635 buflen = ME_GetTextW(editor, buffer, nStart, nCount, ex->flags & GT_USECRLF);
2636 rc = WideCharToMultiByte(ex->codepage, flags, buffer, buflen+1, (LPSTR)lParam, ex->cb, ex->lpDefaultChar, ex->lpUsedDefChar);
2637 if (rc) rc--; /* do not count 0 terminator */
2638
2639 heap_free(buffer);
2640 return rc;
2641 }
2642 }
2643 case EM_GETSELTEXT:
2644 {
2645 int from, to;
2646 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
2647 ME_GetSelection(editor, &from, &to);
2648 tr.chrg.cpMin = from;
2649 tr.chrg.cpMax = to;
2650 tr.lpstrText = (WCHAR *)lParam;
2651 return RichEditWndProc_common(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr, unicode);
2652 }
2653 case EM_GETSCROLLPOS:
2654 {
2655 POINT *point = (POINT *)lParam;
2656 point->x = 0; /* FIXME side scrolling not implemented */
2657 point->y = ME_GetYScrollPos(editor);
2658 return 1;
2659 }
2660 case EM_GETTEXTRANGE:
2661 {
2662 TEXTRANGEW *rng = (TEXTRANGEW *)lParam;
2663 TRACE("EM_GETTEXTRANGE min=%d max=%d unicode=%d emul1.0=%d length=%d\n",
2664 rng->chrg.cpMin, rng->chrg.cpMax, unicode,
2665 editor->bEmulateVersion10, ME_GetTextLength(editor));
2666 if (unicode)
2667 return ME_GetTextW(editor, rng->lpstrText, rng->chrg.cpMin, rng->chrg.cpMax-rng->chrg.cpMin, 0);
2668 else
2669 {
2670 int nLen = rng->chrg.cpMax-rng->chrg.cpMin;
2671 WCHAR *p = ALLOC_N_OBJ(WCHAR, nLen+1);
2672 int nChars = ME_GetTextW(editor, p, rng->chrg.cpMin, nLen, 0);
2673 /* FIXME this is a potential security hole (buffer overrun)
2674 if you know more about wchar->mbyte conversion please explain
2675 */
2676 WideCharToMultiByte(CP_ACP, 0, p, nChars+1, (char *)rng->lpstrText, nLen+1, NULL, NULL);
2677 FREE_OBJ(p);
2678 return nChars;
2679 }
2680 }
2681 case EM_GETLINE:
2682 {
2683 ME_DisplayItem *run;
2684 const unsigned int nMaxChars = *(WORD *) lParam;
2685 unsigned int nCharsLeft = nMaxChars;
2686 char *dest = (char *) lParam;
2687 BOOL wroteNull = FALSE;
2688
2689 TRACE("EM_GETLINE: row=%d, nMaxChars=%d (%s)\n", (int) wParam, nMaxChars,
2690 unicode ? "Unicode" : "Ansi");
2691
2692 run = ME_FindRowWithNumber(editor, wParam);
2693 if (run == NULL)
2694 return 0;
2695
2696 while (nCharsLeft && (run = ME_FindItemFwd(run, diRunOrStartRow))
2697 && !(run->member.run.nFlags & MERF_ENDPARA))
2698 {
2699 unsigned int nCopy;
2700 ME_String *strText;
2701 if (run->type != diRun)
2702 break;
2703 strText = run->member.run.strText;
2704 nCopy = min(nCharsLeft, strText->nLen);
2705
2706 if (unicode)
2707 lstrcpynW((LPWSTR) dest, strText->szData, nCopy);
2708 else
2709 nCopy = WideCharToMultiByte(CP_ACP, 0, strText->szData, nCopy, dest,
2710 nCharsLeft, NULL, NULL);
2711 dest += nCopy * (unicode ? sizeof(WCHAR) : 1);
2712 nCharsLeft -= nCopy;
2713 }
2714
2715 /* append line termination, space allowing */
2716 if (nCharsLeft > 0)
2717 {
2718 if (run && (run->member.run.nFlags & MERF_ENDPARA))
2719 {
2720 unsigned int i;
2721 /* Write as many \r as encoded in end-of-paragraph, space allowing */
2722 for (i = 0; i < run->member.run.nCR && nCharsLeft > 0; i++, nCharsLeft--)
2723 {
2724 *((WCHAR *)dest) = '\r';
2725 dest += unicode ? sizeof(WCHAR) : 1;
2726 }
2727 /* Write as many \n as encoded in end-of-paragraph, space allowing */
2728 for (i = 0; i < run->member.run.nLF && nCharsLeft > 0; i++, nCharsLeft--)
2729 {
2730 *((WCHAR *)dest) = '\n';
2731 dest += unicode ? sizeof(WCHAR) : 1;
2732 }
2733 }
2734 if (nCharsLeft > 0)
2735 {
2736 if (unicode)
2737 *((WCHAR *)dest) = '\0';
2738 else
2739 *dest = '\0';
2740 nCharsLeft--;
2741 wroteNull = TRUE;
2742 }
2743 }
2744
2745 TRACE("EM_GETLINE: got %u characters\n", nMaxChars - nCharsLeft);
2746 return nMaxChars - nCharsLeft - (wroteNull ? 1 : 0);
2747 }
2748 case EM_GETLINECOUNT:
2749 {
2750 ME_DisplayItem *item = editor->pBuffer->pFirst->next;
2751 int nRows = 0;
2752
2753 ME_DisplayItem *prev_para = NULL, *last_para = NULL;
2754
2755 while (item != editor->pBuffer->pLast)
2756 {
2757 assert(item->type == diParagraph);
2758 prev_para = ME_FindItemBack(item, diRun);
2759 if (prev_para) {
2760 assert(prev_para->member.run.nFlags & MERF_ENDPARA);
2761 }
2762 nRows += item->member.para.nRows;
2763 item = item->member.para.next_para;
2764 }
2765 last_para = ME_FindItemBack(item, diRun);
2766 assert(last_para);
2767 assert(last_para->member.run.nFlags & MERF_ENDPARA);
2768 if (editor->bEmulateVersion10 && prev_para && last_para->member.run.nCharOfs == 0
2769 && prev_para->member.run.nCR == 1 && prev_para->member.run.nLF == 0)
2770 {
2771 /* In 1.0 emulation, the last solitary \r at the very end of the text
2772 (if one exists) is NOT a line break.
2773 FIXME: this is an ugly hack. This should have a more regular model. */
2774 nRows--;
2775 }
2776
2777 TRACE("EM_GETLINECOUNT: nRows==%d\n", nRows);
2778 return max(1, nRows);
2779 }
2780 case EM_LINEFROMCHAR:
2781 {
2782 if (wParam == -1)
2783 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(editor, 1));
2784 else
2785 return ME_RowNumberFromCharOfs(editor, wParam);
2786 }
2787 case EM_EXLINEFROMCHAR:
2788 {
2789 if (lParam == -1)
2790 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(editor,1));
2791 else
2792 return ME_RowNumberFromCharOfs(editor, lParam);
2793 }
2794 case EM_LINEINDEX:
2795 {
2796 ME_DisplayItem *item, *para;
2797 int nCharOfs;
2798
2799 if (wParam == -1)
2800 item = ME_FindItemBack(editor->pCursors[0].pRun, diStartRow);
2801 else
2802 item = ME_FindRowWithNumber(editor, wParam);
2803 if (!item)
2804 return -1;
2805 para = ME_GetParagraph(item);
2806 item = ME_FindItemFwd(item, diRun);
2807 nCharOfs = para->member.para.nCharOfs + item->member.run.nCharOfs;
2808 TRACE("EM_LINEINDEX: nCharOfs==%d\n", nCharOfs);
2809 return nCharOfs;
2810 }
2811 case EM_LINELENGTH:
2812 {
2813 ME_DisplayItem *item, *item_end;
2814 int nChars = 0, nThisLineOfs = 0, nNextLineOfs = 0;
2815
2816 if (wParam > ME_GetTextLength(editor))
2817 return 0;
2818 if (wParam == -1)
2819 {
2820 FIXME("EM_LINELENGTH: returning number of unselected characters on lines with selection unsupported.\n");
2821 return 0;
2822 }
2823 item = ME_FindItemAtOffset(editor, diRun, wParam, NULL);
2824 item = ME_RowStart(item);
2825 nThisLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item, diRun), 0);
2826 item_end = ME_FindItemFwd(item, diStartRowOrParagraphOrEnd);
2827 if (item_end->type == diStartRow)
2828 nNextLineOfs = ME_CharOfsFromRunOfs(editor, ME_FindItemFwd(item_end, diRun), 0);
2829 else
2830 {
2831 ME_DisplayItem *endPara;
2832
2833 nNextLineOfs = ME_FindItemFwd(item, diParagraphOrEnd)->member.para.nCharOfs;
2834 endPara = ME_FindItemFwd(item, diParagraphOrEnd);
2835 endPara = ME_FindItemBack(endPara, diRun);
2836 assert(endPara);
2837 assert(endPara->type == diRun);
2838 assert(endPara->member.run.nFlags & MERF_ENDPARA);
2839 nNextLineOfs -= endPara->member.run.nCR + endPara->member.run.nLF;
2840 }
2841 nChars = nNextLineOfs - nThisLineOfs;
2842 TRACE("EM_LINELENGTH(%ld)==%d\n",wParam, nChars);
2843 return nChars;
2844 }
2845 case EM_EXLIMITTEXT:
2846 {
2847 if ((int)lParam < 0)
2848 return 0;
2849 if (lParam == 0)
2850 editor->nTextLimit = 65536;
2851 else
2852 editor->nTextLimit = (int) lParam;
2853 return 0;
2854 }
2855 case EM_LIMITTEXT:
2856 {
2857 if (wParam == 0)
2858 editor->nTextLimit = 65536;
2859 else
2860 editor->nTextLimit = (int) wParam;
2861 return 0;
2862 }
2863 case EM_GETLIMITTEXT:
2864 {
2865 return editor->nTextLimit;
2866 }
2867 case EM_FINDTEXT:
2868 {
2869 FINDTEXTA *ft = (FINDTEXTA *)lParam;
2870 int nChars = MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, NULL, 0);
2871 WCHAR *tmp;
2872 LRESULT r;
2873
2874 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
2875 MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, tmp, nChars);
2876 r = ME_FindText(editor, wParam, &ft->chrg, tmp, NULL);
2877 FREE_OBJ( tmp );
2878 return r;
2879 }
2880 case EM_FINDTEXTEX:
2881 {
2882 FINDTEXTEXA *ex = (FINDTEXTEXA *)lParam;
2883 int nChars = MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, NULL, 0);
2884 WCHAR *tmp;
2885 LRESULT r;
2886
2887 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
2888 MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, tmp, nChars);
2889 r = ME_FindText(editor, wParam, &ex->chrg, tmp, &ex->chrgText);
2890 FREE_OBJ( tmp );
2891 return r;
2892 }
2893 case EM_FINDTEXTW:
2894 {
2895 FINDTEXTW *ft = (FINDTEXTW *)lParam;
2896 return ME_FindText(editor, wParam, &ft->chrg, ft->lpstrText, NULL);
2897 }
2898 case EM_FINDTEXTEXW:
2899 {
2900 FINDTEXTEXW *ex = (FINDTEXTEXW *)lParam;
2901 return ME_FindText(editor, wParam, &ex->chrg, ex->lpstrText, &ex->chrgText);
2902 }
2903 case EM_GETZOOM:
2904 if (!wParam || !lParam)
2905 return FALSE;
2906 *(int *)wParam = editor->nZoomNumerator;
2907 *(int *)lParam = editor->nZoomDenominator;
2908 return TRUE;
2909 case EM_SETZOOM:
2910 return ME_SetZoom(editor, wParam, lParam);
2911 case EM_CHARFROMPOS:
2912 return ME_CharFromPos(editor, ((POINTL *)lParam)->x, ((POINTL *)lParam)->y);
2913 case EM_POSFROMCHAR:
2914 {
2915 ME_DisplayItem *pRun;
2916 int nCharOfs, nOffset, nLength;
2917 POINTL pt = {0,0};
2918 SCROLLINFO si;
2919
2920 nCharOfs = wParam;
2921 /* detect which API version we're dealing with */
2922 if (wParam >= 0x40000)
2923 nCharOfs = lParam;
2924 nLength = ME_GetTextLength(editor);
2925
2926 if (nCharOfs < nLength) {
2927 ME_RunOfsFromCharOfs(editor, nCharOfs, &pRun, &nOffset);
2928 assert(pRun->type == diRun);
2929 pt.y = pRun->member.run.pt.y;
2930 pt.x = pRun->member.run.pt.x + ME_PointFromChar(editor, &pRun->member.run, nOffset);
2931 pt.y += ME_GetParagraph(pRun)->member.para.nYPos;
2932 } else {
2933 pt.x = 0;
2934 pt.y = editor->pBuffer->pLast->member.para.nYPos;
2935 }
2936 pt.x += editor->selofs;
2937
2938 si.cbSize = sizeof(si);
2939 si.fMask = SIF_POS;
2940 if (GetScrollInfo(editor->hWnd, SB_VERT, &si)) pt.y -= si.nPos;
2941 si.cbSize = sizeof(si);
2942 si.fMask = SIF_POS;
2943 if (GetScrollInfo(editor->hWnd, SB_HORZ, &si)) pt.x -= si.nPos;
2944
2945 if (wParam >= 0x40000) {
2946 *(POINTL *)wParam = pt;
2947 }
2948 return (wParam >= 0x40000) ? 0 : MAKELONG( pt.x, pt.y );
2949 }
2950 case WM_CREATE:
2951 GetClientRect(hWnd, &editor->rcFormat);
2952 if (GetWindowLongW(hWnd, GWL_STYLE) & WS_HSCROLL)
2953 { /* Squelch the default horizontal scrollbar it would make */
2954 ShowScrollBar(editor->hWnd, SB_HORZ, FALSE);
2955 }
2956 ME_CommitUndo(editor);
2957 ME_WrapMarkedParagraphs(editor);
2958 ME_MoveCaret(editor);
2959 return 0;
2960 case WM_DESTROY:
2961 ME_DestroyEditor(editor);
2962 SetWindowLongPtrW(hWnd, 0, 0);
2963 return 0;
2964 case WM_LBUTTONDOWN:
2965 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
2966 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
2967 !ME_FilterEvent(editor, msg, &wParam, &lParam))
2968 return 0;
2969 SetFocus(hWnd);
2970 ME_LButtonDown(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
2971 SetCapture(hWnd);
2972 ME_LinkNotify(editor,msg,wParam,lParam);
2973 if (!ME_SetCursor(editor, LOWORD(lParam))) goto do_default;
2974 break;
2975 case WM_MOUSEMOVE:
2976 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
2977 !ME_FilterEvent(editor, msg, &wParam, &lParam))
2978 return 0;
2979 if (GetCapture() == hWnd)
2980 ME_MouseMove(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
2981 ME_LinkNotify(editor,msg,wParam,lParam);
2982 if (!ME_SetCursor(editor, LOWORD(lParam))) goto do_default;
2983 break;
2984 case WM_LBUTTONUP:
2985 if (GetCapture() == hWnd)
2986 ReleaseCapture();
2987 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
2988 !ME_FilterEvent(editor, msg, &wParam, &lParam))
2989 return 0;
2990 else
2991 {
2992 BOOL ret;
2993 editor->linesel = 0;
2994 ret = ME_SetCursor(editor, LOWORD(lParam));
2995 ME_LinkNotify(editor,msg,wParam,lParam);
2996 if (!ret) goto do_default;
2997 }
2998 break;
2999 case WM_LBUTTONDBLCLK:
3000 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
3001 !ME_FilterEvent(editor, msg, &wParam, &lParam))
3002 return 0;
3003 ME_LinkNotify(editor,msg,wParam,lParam);
3004 ME_SelectWord(editor);
3005 break;
3006 case WM_RBUTTONUP:
3007 case WM_RBUTTONDOWN:
3008 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
3009 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
3010 !ME_FilterEvent(editor, msg, &wParam, &lParam))
3011 return 0;
3012 goto do_default;
3013 case WM_CONTEXTMENU:
3014 if (!ME_ShowContextMenu(editor, (short)LOWORD(lParam), (short)HIWORD(lParam)))
3015 goto do_default;
3016 break;
3017 case WM_PAINT:
3018 if (editor->bRedraw)
3019 {
3020 HDC hDC;
3021 PAINTSTRUCT ps;
3022
3023 hDC = BeginPaint(hWnd, &ps);
3024 ME_PaintContent(editor, hDC, FALSE, &ps.rcPaint);
3025 EndPaint(hWnd, &ps);
3026 }
3027 break;
3028 case WM_SETFOCUS:
3029 editor->bHaveFocus = TRUE;
3030 ME_ShowCaret(editor);
3031 ME_SendOldNotify(editor, EN_SETFOCUS);
3032 return 0;
3033 case WM_KILLFOCUS:
3034 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
3035 ME_HideCaret(editor);
3036 editor->bHaveFocus = FALSE;
3037 ME_SendOldNotify(editor, EN_KILLFOCUS);
3038 return 0;
3039 case WM_ERASEBKGND:
3040 {
3041 if (editor->bRedraw)
3042 {
3043 HDC hDC = (HDC)wParam;
3044 RECT rc;
3045 if (GetUpdateRect(hWnd,&rc,TRUE))
3046 {
3047 FillRect(hDC, &rc, editor->hbrBackground);
3048 }
3049 }
3050 return 1;
3051 }
3052 case WM_COMMAND:
3053 TRACE("editor wnd command = %d\n", LOWORD(wParam));
3054 return 0;
3055 case WM_KEYUP:
3056 if ((editor->nEventMask & ENM_KEYEVENTS) &&
3057 !ME_FilterEvent(editor, msg, &wParam, &lParam))
3058 return 0;
3059 goto do_default;
3060 case WM_KEYDOWN:
3061 if ((editor->nEventMask & ENM_KEYEVENTS) &&
3062 !ME_FilterEvent(editor, msg, &wParam, &lParam))
3063 return 0;
3064 if (ME_KeyDown(editor, LOWORD(wParam)))
3065 return 0;
3066 goto do_default;
3067 case WM_CHAR:
3068 {
3069 WCHAR wstr;
3070
3071 if (unicode)
3072 wstr = (WCHAR)wParam;
3073 else
3074 {
3075 CHAR charA = wParam;
3076 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &wstr, 1);
3077 }
3078
3079 switch (wstr)
3080 {
3081 case 1: /* Ctrl-A */
3082 ME_SetSelection(editor, 0, -1);
3083 return 0;
3084 case 3: /* Ctrl-C */
3085 SendMessageW(editor->hWnd, WM_COPY, 0, 0);
3086 return 0;
3087 }
3088
3089 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY) {
3090 MessageBeep(MB_ICONERROR);
3091 return 0; /* FIXME really 0 ? */
3092 }
3093
3094 switch (wstr)
3095 {
3096 case 22: /* Ctrl-V */
3097 SendMessageW(editor->hWnd, WM_PASTE, 0, 0);
3098 return 0;
3099 case 24: /* Ctrl-X */
3100 SendMessageW(editor->hWnd, WM_CUT, 0, 0);
3101 return 0;
3102 case 25: /* Ctrl-Y */
3103 SendMessageW(editor->hWnd, EM_REDO, 0, 0);
3104 return 0;
3105 case 26: /* Ctrl-Z */
3106 SendMessageW(editor->hWnd, EM_UNDO, 0, 0);
3107 return 0;
3108 }
3109 if (((unsigned)wstr)>=' '
3110 || (wstr=='\r' && (GetWindowLongW(hWnd, GWL_STYLE) & ES_MULTILINE))
3111 || wstr=='\t') {
3112 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
3113 /* WM_CHAR is restricted to nTextLimit */
3114 int from, to;
3115 ME_GetSelection(editor, &from, &to);
3116 if(editor->nTextLimit > ME_GetTextLength(editor) - (to-from))
3117 {
3118 ME_Style *style = ME_GetInsertStyle(editor, 0);
3119 ME_SaveTempStyle(editor);
3120 ME_ContinueCoalescingTransaction(editor);
3121 if (wstr == '\r' && (GetKeyState(VK_SHIFT) & 0x8000))
3122 ME_InsertEndRowFromCursor(editor, 0);
3123 else
3124 ME_InsertTextFromCursor(editor, 0, &wstr, 1, style);
3125 ME_ReleaseStyle(style);
3126 ME_CommitCoalescingUndo(editor);
3127 }
3128
3129 if (editor->AutoURLDetect_bEnable) ME_UpdateSelectionLinkAttribute(editor);
3130
3131 ME_UpdateRepaint(editor);
3132 }
3133 return 0;
3134 }
3135 case EM_STOPGROUPTYPING:
3136 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
3137 return 0;
3138 case EM_SCROLL: /* fall through */
3139 case WM_VSCROLL:
3140 {
3141 int origNPos;
3142 int lineHeight;
3143
3144 origNPos = ME_GetYScrollPos(editor);
3145 lineHeight = 24;
3146
3147 if (editor && editor->pBuffer && editor->pBuffer->pDefaultStyle)
3148 lineHeight = editor->pBuffer->pDefaultStyle->tm.tmHeight;
3149 if (lineHeight <= 0) lineHeight = 24;
3150
3151 switch(LOWORD(wParam))
3152 {
3153 case SB_LINEUP:
3154 ME_ScrollUp(editor,lineHeight);
3155 break;
3156 case SB_LINEDOWN:
3157 ME_ScrollDown(editor,lineHeight);
3158 break;
3159 case SB_PAGEUP:
3160 ME_ScrollUp(editor,editor->sizeWindow.cy);
3161 break;
3162 case SB_PAGEDOWN:
3163 ME_ScrollDown(editor,editor->sizeWindow.cy);
3164 break;
3165 case SB_THUMBTRACK:
3166 case SB_THUMBPOSITION:
3167 ME_ScrollAbs(editor,HIWORD(wParam));
3168 break;
3169 }
3170 if (msg == EM_SCROLL)
3171 return 0x00010000 | (((ME_GetYScrollPos(editor) - origNPos)/lineHeight) & 0xffff);
3172 break;
3173 }
3174 case WM_MOUSEWHEEL:
3175 {
3176 int gcWheelDelta;
3177 UINT pulScrollLines;
3178
3179 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
3180 !ME_FilterEvent(editor, msg, &wParam, &lParam))
3181 return 0;
3182
3183 SystemParametersInfoW(SPI_GETWHEELSCROLLLINES,0, &pulScrollLines, 0);
3184 gcWheelDelta = -GET_WHEEL_DELTA_WPARAM(wParam);
3185
3186 if (abs(gcWheelDelta) >= WHEEL_DELTA && pulScrollLines)
3187 {
3188 /* FIXME follow the original */
3189 ME_ScrollDown(editor,pulScrollLines * (gcWheelDelta / WHEEL_DELTA) * 8);
3190 }
3191 break;
3192 }
3193 case EM_GETRECT:
3194 {
3195 *((RECT *)lParam) = editor->rcFormat;
3196 return 0;
3197 }
3198 case EM_SETRECT:
3199 case EM_SETRECTNP:
3200 {
3201 if (lParam)
3202 {
3203 RECT *rc = (RECT *)lParam;
3204
3205 if (wParam)
3206 {
3207 editor->rcFormat.left += rc->left;
3208 editor->rcFormat.top += rc->top;
3209 editor->rcFormat.right += rc->right;
3210 editor->rcFormat.bottom += rc->bottom;
3211 }
3212 else
3213 {
3214 editor->rcFormat = *rc;
3215 }
3216 }
3217 else
3218 {
3219 GetClientRect(hWnd, &editor->rcFormat);
3220 }
3221 if (msg != EM_SETRECTNP)
3222 ME_RewrapRepaint(editor);
3223 return 0;
3224 }
3225 case EM_REQUESTRESIZE:
3226 ME_SendRequestResize(editor, TRUE);
3227 return 0;
3228 case WM_SETREDRAW:
3229 if ((editor->bRedraw = wParam))
3230 ME_RewrapRepaint(editor);
3231 return 0;
3232 case WM_SIZE:
3233 {
3234 GetClientRect(hWnd, &editor->rcFormat);
3235 ME_RewrapRepaint(editor);
3236 return DefWindowProcW(hWnd, msg, wParam, lParam);
3237 }
3238 /* IME messages to make richedit controls IME aware */
3239 case WM_IME_SETCONTEXT:
3240 case WM_IME_CONTROL:
3241 case WM_IME_SELECT:
3242 case WM_IME_COMPOSITIONFULL:
3243 return 0;
3244 case WM_IME_STARTCOMPOSITION:
3245 {
3246 editor->imeStartIndex=ME_GetCursorOfs(editor,0);
3247 ME_DeleteSelection(editor);
3248 ME_CommitUndo(editor);
3249 ME_UpdateRepaint(editor);
3250 return 0;
3251 }
3252 case WM_IME_COMPOSITION:
3253 {
3254 HIMC hIMC;
3255
3256 ME_Style *style = ME_GetInsertStyle(editor, 0);
3257 hIMC = ImmGetContext(hWnd);
3258 ME_DeleteSelection(editor);
3259 ME_CommitUndo(editor);
3260 ME_SaveTempStyle(editor);
3261 if (lParam & GCS_RESULTSTR)
3262 {
3263 LPWSTR lpCompStr = NULL;
3264 DWORD dwBufLen;
3265
3266 dwBufLen = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
3267 lpCompStr = HeapAlloc(GetProcessHeap(),0,dwBufLen + sizeof(WCHAR));
3268 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, lpCompStr, dwBufLen);
3269 lpCompStr[dwBufLen/sizeof(WCHAR)] = 0;
3270 ME_InsertTextFromCursor(editor,0,lpCompStr,dwBufLen/sizeof(WCHAR),style);
3271 }
3272 else if (lParam & GCS_COMPSTR)
3273 {
3274 LPWSTR lpCompStr = NULL;
3275 DWORD dwBufLen;
3276
3277 dwBufLen = ImmGetCompositionStringW(hIMC, GCS_COMPSTR, NULL, 0);
3278 lpCompStr = HeapAlloc(GetProcessHeap(),0,dwBufLen + sizeof(WCHAR));
3279 ImmGetCompositionStringW(hIMC, GCS_COMPSTR, lpCompStr, dwBufLen);
3280 lpCompStr[dwBufLen/sizeof(WCHAR)] = 0;
3281
3282 ME_InsertTextFromCursor(editor,0,lpCompStr,dwBufLen/sizeof(WCHAR),style);
3283 ME_SetSelection(editor,editor->imeStartIndex,
3284 editor->imeStartIndex + dwBufLen/sizeof(WCHAR));
3285 }
3286 ME_ReleaseStyle(style);
3287 ME_UpdateRepaint(editor);
3288 return 0;
3289 }
3290 case WM_IME_ENDCOMPOSITION:
3291 {
3292 ME_DeleteSelection(editor);
3293 editor->imeStartIndex=-1;
3294 return 0;
3295 }
3296 case EM_GETOLEINTERFACE:
3297 {
3298 LPVOID *ppvObj = (LPVOID*) lParam;
3299 return CreateIRichEditOle(editor, ppvObj);
3300 }
3301 case EM_GETPASSWORDCHAR:
3302 {
3303 return editor->cPasswordMask;
3304 }
3305 case EM_SETOLECALLBACK:
3306 if(