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