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