[RICHED20] Sync with Wine Staging 1.7.55. CORE-10536
[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->editor, 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_RTFReadShpPictGroup( RTF_Info *info )
1237 {
1238 int level = 1;
1239
1240 for (;;)
1241 {
1242 RTFGetToken (info);
1243
1244 if (info->rtfClass == rtfEOF) return;
1245 if (RTFCheckCM( info, rtfGroup, rtfEndGroup ))
1246 {
1247 if (--level == 0) break;
1248 }
1249 else if (RTFCheckCM( info, rtfGroup, rtfBeginGroup ))
1250 {
1251 level++;
1252 }
1253 else
1254 {
1255 RTFRouteToken( info );
1256 if (RTFCheckCM( info, rtfGroup, rtfEndGroup ))
1257 level--;
1258 }
1259 }
1260
1261 RTFRouteToken( info ); /* feed "}" back to router */
1262 return;
1263 }
1264
1265 static DWORD read_hex_data( RTF_Info *info, BYTE **out )
1266 {
1267 DWORD read = 0, size = 1024;
1268 BYTE *buf, val;
1269 BOOL flip;
1270
1271 *out = NULL;
1272
1273 if (info->rtfClass != rtfText)
1274 {
1275 ERR("Called with incorrect token\n");
1276 return 0;
1277 }
1278
1279 buf = HeapAlloc( GetProcessHeap(), 0, size );
1280 if (!buf) return 0;
1281
1282 val = info->rtfMajor;
1283 for (flip = TRUE;; flip = !flip)
1284 {
1285 RTFGetToken( info );
1286 if (info->rtfClass == rtfEOF)
1287 {
1288 HeapFree( GetProcessHeap(), 0, buf );
1289 return 0;
1290 }
1291 if (info->rtfClass != rtfText) break;
1292 if (flip)
1293 {
1294 if (read >= size)
1295 {
1296 size *= 2;
1297 buf = HeapReAlloc( GetProcessHeap(), 0, buf, size );
1298 if (!buf) return 0;
1299 }
1300 buf[read++] = RTFCharToHex(val) * 16 + RTFCharToHex(info->rtfMajor);
1301 }
1302 else
1303 val = info->rtfMajor;
1304 }
1305 if (flip) FIXME("wrong hex string\n");
1306
1307 *out = buf;
1308 return read;
1309 }
1310
1311 static void ME_RTFReadPictGroup(RTF_Info *info)
1312 {
1313 SIZEL sz;
1314 BYTE *buffer = NULL;
1315 DWORD size = 0;
1316 METAFILEPICT mfp;
1317 HENHMETAFILE hemf;
1318 HBITMAP hbmp;
1319 enum gfxkind {gfx_unknown = 0, gfx_enhmetafile, gfx_metafile, gfx_dib} gfx = gfx_unknown;
1320 int level = 1;
1321
1322 mfp.mm = MM_TEXT;
1323 sz.cx = sz.cy = 0;
1324
1325 for (;;)
1326 {
1327 RTFGetToken( info );
1328
1329 if (info->rtfClass == rtfText)
1330 {
1331 if (level == 1)
1332 {
1333 if (!buffer)
1334 size = read_hex_data( info, &buffer );
1335 }
1336 else
1337 {
1338 RTFSkipGroup( info );
1339 }
1340 } /* We potentially have a new token so fall through. */
1341
1342 if (info->rtfClass == rtfEOF) return;
1343
1344 if (RTFCheckCM( info, rtfGroup, rtfEndGroup ))
1345 {
1346 if (--level == 0) break;
1347 continue;
1348 }
1349 if (RTFCheckCM( info, rtfGroup, rtfBeginGroup ))
1350 {
1351 level++;
1352 continue;
1353 }
1354 if (!RTFCheckCM( info, rtfControl, rtfPictAttr ))
1355 {
1356 RTFRouteToken( info );
1357 if (RTFCheckCM( info, rtfGroup, rtfEndGroup ))
1358 level--;
1359 continue;
1360 }
1361
1362 if (RTFCheckMM( info, rtfPictAttr, rtfWinMetafile ))
1363 {
1364 mfp.mm = info->rtfParam;
1365 gfx = gfx_metafile;
1366 }
1367 else if (RTFCheckMM( info, rtfPictAttr, rtfDevIndBitmap ))
1368 {
1369 if (info->rtfParam != 0) FIXME("dibitmap should be 0 (%d)\n", info->rtfParam);
1370 gfx = gfx_dib;
1371 }
1372 else if (RTFCheckMM( info, rtfPictAttr, rtfEmfBlip ))
1373 gfx = gfx_enhmetafile;
1374 else if (RTFCheckMM( info, rtfPictAttr, rtfPicWid ))
1375 mfp.xExt = info->rtfParam;
1376 else if (RTFCheckMM( info, rtfPictAttr, rtfPicHt ))
1377 mfp.yExt = info->rtfParam;
1378 else if (RTFCheckMM( info, rtfPictAttr, rtfPicGoalWid ))
1379 sz.cx = info->rtfParam;
1380 else if (RTFCheckMM( info, rtfPictAttr, rtfPicGoalHt ))
1381 sz.cy = info->rtfParam;
1382 else
1383 FIXME("Non supported attribute: %d %d %d\n", info->rtfClass, info->rtfMajor, info->rtfMinor);
1384 }
1385
1386 if (buffer)
1387 {
1388 switch (gfx)
1389 {
1390 case gfx_enhmetafile:
1391 if ((hemf = SetEnhMetaFileBits( size, buffer )))
1392 ME_RTFInsertOleObject( info, hemf, NULL, &sz );
1393 break;
1394 case gfx_metafile:
1395 if ((hemf = SetWinMetaFileBits( size, buffer, NULL, &mfp )))
1396 ME_RTFInsertOleObject( info, hemf, NULL, &sz );
1397 break;
1398 case gfx_dib:
1399 {
1400 BITMAPINFO *bi = (BITMAPINFO*)buffer;
1401 HDC hdc = GetDC(0);
1402 unsigned nc = bi->bmiHeader.biClrUsed;
1403
1404 /* not quite right, especially for bitfields type of compression */
1405 if (!nc && bi->bmiHeader.biBitCount <= 8)
1406 nc = 1 << bi->bmiHeader.biBitCount;
1407 if ((hbmp = CreateDIBitmap( hdc, &bi->bmiHeader,
1408 CBM_INIT, (char*)(bi + 1) + nc * sizeof(RGBQUAD),
1409 bi, DIB_RGB_COLORS)) )
1410 ME_RTFInsertOleObject( info, NULL, hbmp, &sz );
1411 ReleaseDC( 0, hdc );
1412 break;
1413 }
1414 default:
1415 break;
1416 }
1417 }
1418 HeapFree( GetProcessHeap(), 0, buffer );
1419 RTFRouteToken( info ); /* feed "}" back to router */
1420 return;
1421 }
1422
1423 /* for now, lookup the \result part and use it, whatever the object */
1424 static void ME_RTFReadObjectGroup(RTF_Info *info)
1425 {
1426 for (;;)
1427 {
1428 RTFGetToken (info);
1429 if (info->rtfClass == rtfEOF)
1430 return;
1431 if (RTFCheckCM(info, rtfGroup, rtfEndGroup))
1432 break;
1433 if (RTFCheckCM(info, rtfGroup, rtfBeginGroup))
1434 {
1435 RTFGetToken (info);
1436 if (info->rtfClass == rtfEOF)
1437 return;
1438 if (RTFCheckCMM(info, rtfControl, rtfDestination, rtfObjResult))
1439 {
1440 int level = 1;
1441
1442 while (RTFGetToken (info) != rtfEOF)
1443 {
1444 if (info->rtfClass == rtfGroup)
1445 {
1446 if (info->rtfMajor == rtfBeginGroup) level++;
1447 else if (info->rtfMajor == rtfEndGroup && --level < 0) break;
1448 }
1449 RTFRouteToken(info);
1450 }
1451 }
1452 else RTFSkipGroup(info);
1453 continue;
1454 }
1455 if (!RTFCheckCM (info, rtfControl, rtfObjAttr))
1456 {
1457 FIXME("Non supported attribute: %d %d %d\n", info->rtfClass, info->rtfMajor, info->rtfMinor);
1458 return;
1459 }
1460 }
1461 RTFRouteToken(info); /* feed "}" back to router */
1462 }
1463
1464 static void ME_RTFReadHook(RTF_Info *info)
1465 {
1466 switch(info->rtfClass)
1467 {
1468 case rtfGroup:
1469 switch(info->rtfMajor)
1470 {
1471 case rtfBeginGroup:
1472 if (info->stackTop < maxStack) {
1473 info->stack[info->stackTop].style = info->style;
1474 ME_AddRefStyle(info->style);
1475 info->stack[info->stackTop].codePage = info->codePage;
1476 info->stack[info->stackTop].unicodeLength = info->unicodeLength;
1477 }
1478 info->stackTop++;
1479 info->styleChanged = FALSE;
1480 break;
1481 case rtfEndGroup:
1482 {
1483 RTFFlushOutputBuffer(info);
1484 info->stackTop--;
1485 if (info->stackTop <= 0)
1486 info->rtfClass = rtfEOF;
1487 if (info->stackTop < 0)
1488 return;
1489
1490 ME_ReleaseStyle(info->style);
1491 info->style = info->stack[info->stackTop].style;
1492 info->codePage = info->stack[info->stackTop].codePage;
1493 info->unicodeLength = info->stack[info->stackTop].unicodeLength;
1494 break;
1495 }
1496 }
1497 break;
1498 }
1499 }
1500
1501 void
1502 ME_StreamInFill(ME_InStream *stream)
1503 {
1504 stream->editstream->dwError = stream->editstream->pfnCallback(stream->editstream->dwCookie,
1505 (BYTE *)stream->buffer,
1506 sizeof(stream->buffer),
1507 (LONG *)&stream->dwSize);
1508 stream->dwUsed = 0;
1509 }
1510
1511 static LRESULT ME_StreamIn(ME_TextEditor *editor, DWORD format, EDITSTREAM *stream, BOOL stripLastCR)
1512 {
1513 RTF_Info parser;
1514 ME_Style *style;
1515 int from, to, nUndoMode;
1516 int nEventMask = editor->nEventMask;
1517 ME_InStream inStream;
1518 BOOL invalidRTF = FALSE;
1519 ME_Cursor *selStart, *selEnd;
1520 LRESULT num_read = 0; /* bytes read for SF_TEXT, non-control chars inserted for SF_RTF */
1521
1522 TRACE("stream==%p editor==%p format==0x%X\n", stream, editor, format);
1523 editor->nEventMask = 0;
1524
1525 ME_GetSelectionOfs(editor, &from, &to);
1526 if (format & SFF_SELECTION && editor->mode & TM_RICHTEXT)
1527 {
1528 ME_GetSelection(editor, &selStart, &selEnd);
1529 style = ME_GetSelectionInsertStyle(editor);
1530
1531 ME_InternalDeleteText(editor, selStart, to - from, FALSE);
1532
1533 /* Don't insert text at the end of the table row */
1534 if (!editor->bEmulateVersion10) { /* v4.1 */
1535 ME_DisplayItem *para = editor->pCursors->pPara;
1536 if (para->member.para.nFlags & MEPF_ROWEND)
1537 {
1538 para = para->member.para.next_para;
1539 editor->pCursors[0].pPara = para;
1540 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
1541 editor->pCursors[0].nOffset = 0;
1542 }
1543 if (para->member.para.nFlags & MEPF_ROWSTART)
1544 {
1545 para = para->member.para.next_para;
1546 editor->pCursors[0].pPara = para;
1547 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
1548 editor->pCursors[0].nOffset = 0;
1549 }
1550 editor->pCursors[1] = editor->pCursors[0];
1551 } else { /* v1.0 - 3.0 */
1552 if (editor->pCursors[0].pRun->member.run.nFlags & MERF_ENDPARA &&
1553 ME_IsInTable(editor->pCursors[0].pRun))
1554 return 0;
1555 }
1556 } else {
1557 style = editor->pBuffer->pDefaultStyle;
1558 ME_AddRefStyle(style);
1559 ME_SetSelection(editor, 0, 0);
1560 ME_InternalDeleteText(editor, &editor->pCursors[1],
1561 ME_GetTextLength(editor), FALSE);
1562 from = to = 0;
1563 ME_ClearTempStyle(editor);
1564 ME_SetDefaultParaFormat(editor, editor->pCursors[0].pPara->member.para.pFmt);
1565 }
1566
1567
1568 /* Back up undo mode to a local variable */
1569 nUndoMode = editor->nUndoMode;
1570
1571 /* Only create an undo if SFF_SELECTION is set */
1572 if (!(format & SFF_SELECTION))
1573 editor->nUndoMode = umIgnore;
1574
1575 inStream.editstream = stream;
1576 inStream.editstream->dwError = 0;
1577 inStream.dwSize = 0;
1578 inStream.dwUsed = 0;
1579
1580 if (format & SF_RTF)
1581 {
1582 /* Check if it's really RTF, and if it is not, use plain text */
1583 ME_StreamInFill(&inStream);
1584 if (!inStream.editstream->dwError)
1585 {
1586 if ((!editor->bEmulateVersion10 && strncmp(inStream.buffer, "{\\rtf", 5) && strncmp(inStream.buffer, "{\\urtf", 6))
1587 || (editor->bEmulateVersion10 && *inStream.buffer != '{'))
1588 {
1589 invalidRTF = TRUE;
1590 inStream.editstream->dwError = -16;
1591 }
1592 }
1593 }
1594
1595 if (!invalidRTF && !inStream.editstream->dwError)
1596 {
1597 if (format & SF_RTF) {
1598 from = ME_GetCursorOfs(&editor->pCursors[0]);
1599
1600 /* setup the RTF parser */
1601 memset(&parser, 0, sizeof parser);
1602 RTFSetEditStream(&parser, &inStream);
1603 parser.rtfFormat = format&(SF_TEXT|SF_RTF);
1604 parser.editor = editor;
1605 parser.style = style;
1606 WriterInit(&parser);
1607 RTFInit(&parser);
1608 RTFSetReadHook(&parser, ME_RTFReadHook);
1609 RTFSetDestinationCallback(&parser, rtfShpPict, ME_RTFReadShpPictGroup);
1610 RTFSetDestinationCallback(&parser, rtfPict, ME_RTFReadPictGroup);
1611 RTFSetDestinationCallback(&parser, rtfObject, ME_RTFReadObjectGroup);
1612 if (!parser.editor->bEmulateVersion10) /* v4.1 */
1613 {
1614 RTFSetDestinationCallback(&parser, rtfNoNestTables, RTFSkipGroup);
1615 RTFSetDestinationCallback(&parser, rtfNestTableProps, RTFReadGroup);
1616 }
1617 BeginFile(&parser);
1618
1619 /* do the parsing */
1620 RTFRead(&parser);
1621 RTFFlushOutputBuffer(&parser);
1622 if (!editor->bEmulateVersion10) { /* v4.1 */
1623 if (parser.tableDef && parser.tableDef->tableRowStart &&
1624 (parser.nestingLevel > 0 || parser.canInheritInTbl))
1625 {
1626 /* Delete any incomplete table row at the end of the rich text. */
1627 int nOfs, nChars;
1628 ME_DisplayItem *para;
1629
1630 parser.rtfMinor = rtfRow;
1631 /* Complete the table row before deleting it.
1632 * By doing it this way we will have the current paragraph format set
1633 * properly to reflect that is not in the complete table, and undo items
1634 * will be added for this change to the current paragraph format. */
1635 if (parser.nestingLevel > 0)
1636 {
1637 while (parser.nestingLevel > 1)
1638 ME_RTFSpecialCharHook(&parser); /* Decrements nestingLevel */
1639 para = parser.tableDef->tableRowStart;
1640 ME_RTFSpecialCharHook(&parser);
1641 } else {
1642 para = parser.tableDef->tableRowStart;
1643 ME_RTFSpecialCharHook(&parser);
1644 assert(para->member.para.nFlags & MEPF_ROWEND);
1645 para = para->member.para.next_para;
1646 }
1647
1648 editor->pCursors[1].pPara = para;
1649 editor->pCursors[1].pRun = ME_FindItemFwd(para, diRun);
1650 editor->pCursors[1].nOffset = 0;
1651 nOfs = ME_GetCursorOfs(&editor->pCursors[1]);
1652 nChars = ME_GetCursorOfs(&editor->pCursors[0]) - nOfs;
1653 ME_InternalDeleteText(editor, &editor->pCursors[1], nChars, TRUE);
1654 if (parser.tableDef)
1655 parser.tableDef->tableRowStart = NULL;
1656 }
1657 }
1658 ME_CheckTablesForCorruption(editor);
1659 RTFDestroy(&parser);
1660 if (parser.lpRichEditOle)
1661 IRichEditOle_Release(parser.lpRichEditOle);
1662
1663 if (parser.stackTop > 0)
1664 {
1665 while (--parser.stackTop >= 0)
1666 {
1667 ME_ReleaseStyle(parser.style);
1668 parser.style = parser.stack[parser.stackTop].style;
1669 }
1670 if (!inStream.editstream->dwError)
1671 inStream.editstream->dwError = HRESULT_FROM_WIN32(ERROR_HANDLE_EOF);
1672 }
1673
1674 /* Remove last line break, as mandated by tests. This is not affected by
1675 CR/LF counters, since RTF streaming presents only \para tokens, which
1676 are converted according to the standard rules: \r for 2.0, \r\n for 1.0
1677 */
1678 if (stripLastCR && !(format & SFF_SELECTION)) {
1679 int newto;
1680 ME_GetSelection(editor, &selStart, &selEnd);
1681 newto = ME_GetCursorOfs(selEnd);
1682 if (newto > to + (editor->bEmulateVersion10 ? 1 : 0)) {
1683 WCHAR lastchar[3] = {'\0', '\0'};
1684 int linebreakSize = editor->bEmulateVersion10 ? 2 : 1;
1685 ME_Cursor linebreakCursor = *selEnd;
1686
1687 ME_MoveCursorChars(editor, &linebreakCursor, -linebreakSize);
1688 ME_GetTextW(editor, lastchar, 2, &linebreakCursor, linebreakSize, FALSE, FALSE);
1689 if (lastchar[0] == '\r' && (lastchar[1] == '\n' || lastchar[1] == '\0')) {
1690 ME_InternalDeleteText(editor, &linebreakCursor, linebreakSize, FALSE);
1691 }
1692 }
1693 }
1694 to = ME_GetCursorOfs(&editor->pCursors[0]);
1695 num_read = to - from;
1696
1697 style = parser.style;
1698 }
1699 else if (format & SF_TEXT)
1700 num_read = ME_StreamInText(editor, format, &inStream, style);
1701 else
1702 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
1703 /* put the cursor at the top */
1704 if (!(format & SFF_SELECTION))
1705 ME_SetSelection(editor, 0, 0);
1706 }
1707
1708 /* Restore saved undo mode */
1709 editor->nUndoMode = nUndoMode;
1710
1711 /* even if we didn't add an undo, we need to commit anything on the stack */
1712 ME_CommitUndo(editor);
1713
1714 /* If SFF_SELECTION isn't set, delete any undos from before we started too */
1715 if (!(format & SFF_SELECTION))
1716 ME_EmptyUndoStack(editor);
1717
1718 ME_ReleaseStyle(style);
1719 editor->nEventMask = nEventMask;
1720 ME_UpdateRepaint(editor, FALSE);
1721 if (!(format & SFF_SELECTION)) {
1722 ME_ClearTempStyle(editor);
1723 }
1724 ITextHost_TxShowCaret(editor->texthost, FALSE);
1725 ME_MoveCaret(editor);
1726 ITextHost_TxShowCaret(editor->texthost, TRUE);
1727 ME_SendSelChange(editor);
1728 ME_SendRequestResize(editor, FALSE);
1729
1730 return num_read;
1731 }
1732
1733
1734 typedef struct tagME_RTFStringStreamStruct
1735 {
1736 char *string;
1737 int pos;
1738 int length;
1739 } ME_RTFStringStreamStruct;
1740
1741 static DWORD CALLBACK ME_ReadFromRTFString(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1742 {
1743 ME_RTFStringStreamStruct *pStruct = (ME_RTFStringStreamStruct *)dwCookie;
1744 int count;
1745
1746 count = min(cb, pStruct->length - pStruct->pos);
1747 memmove(lpBuff, pStruct->string + pStruct->pos, count);
1748 pStruct->pos += count;
1749 *pcb = count;
1750 return 0;
1751 }
1752
1753 static void
1754 ME_StreamInRTFString(ME_TextEditor *editor, BOOL selection, char *string)
1755 {
1756 EDITSTREAM es;
1757 ME_RTFStringStreamStruct data;
1758
1759 data.string = string;
1760 data.length = strlen(string);
1761 data.pos = 0;
1762 es.dwCookie = (DWORD_PTR)&data;
1763 es.pfnCallback = ME_ReadFromRTFString;
1764 ME_StreamIn(editor, SF_RTF | (selection ? SFF_SELECTION : 0), &es, TRUE);
1765 }
1766
1767
1768 static int
1769 ME_FindText(ME_TextEditor *editor, DWORD flags, const CHARRANGE *chrg, const WCHAR *text, CHARRANGE *chrgText)
1770 {
1771 const int nLen = lstrlenW(text);
1772 const int nTextLen = ME_GetTextLength(editor);
1773 int nMin, nMax;
1774 ME_Cursor cursor;
1775 WCHAR wLastChar = ' ';
1776
1777 TRACE("flags==0x%08x, chrg->cpMin==%d, chrg->cpMax==%d text==%s\n",
1778 flags, chrg->cpMin, chrg->cpMax, debugstr_w(text));
1779
1780 if (flags & ~(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD))
1781 FIXME("Flags 0x%08x not implemented\n",
1782 flags & ~(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD));
1783
1784 nMin = chrg->cpMin;
1785 if (chrg->cpMax == -1)
1786 nMax = nTextLen;
1787 else
1788 nMax = chrg->cpMax > nTextLen ? nTextLen : chrg->cpMax;
1789
1790 /* In 1.0 emulation, if cpMax reaches end of text, add the FR_DOWN flag */
1791 if (editor->bEmulateVersion10 && nMax == nTextLen)
1792 {
1793 flags |= FR_DOWN;
1794 }
1795
1796 /* In 1.0 emulation, cpMin must always be no greater than cpMax */
1797 if (editor->bEmulateVersion10 && nMax < nMin)
1798 {
1799 if (chrgText)
1800 {
1801 chrgText->cpMin = -1;
1802 chrgText->cpMax = -1;
1803 }
1804 return -1;
1805 }
1806
1807 /* when searching up, if cpMin < cpMax, then instead of searching
1808 * on [cpMin,cpMax], we search on [0,cpMin], otherwise, search on
1809 * [cpMax, cpMin]. The exception is when cpMax is -1, in which
1810 * case, it is always bigger than cpMin.
1811 */
1812 if (!editor->bEmulateVersion10 && !(flags & FR_DOWN))
1813 {
1814 int nSwap = nMax;
1815
1816 nMax = nMin > nTextLen ? nTextLen : nMin;
1817 if (nMin < nSwap || chrg->cpMax == -1)
1818 nMin = 0;
1819 else
1820 nMin = nSwap;
1821 }
1822
1823 if (!nLen || nMin < 0 || nMax < 0 || nMax < nMin)
1824 {
1825 if (chrgText)
1826 chrgText->cpMin = chrgText->cpMax = -1;
1827 return -1;
1828 }
1829
1830 if (flags & FR_DOWN) /* Forward search */
1831 {
1832 /* If possible, find the character before where the search starts */
1833 if ((flags & FR_WHOLEWORD) && nMin)
1834 {
1835 ME_CursorFromCharOfs(editor, nMin - 1, &cursor);
1836 wLastChar = *get_text( &cursor.pRun->member.run, cursor.nOffset );
1837 ME_MoveCursorChars(editor, &cursor, 1);
1838 } else {
1839 ME_CursorFromCharOfs(editor, nMin, &cursor);
1840 }
1841
1842 while (cursor.pRun && ME_GetCursorOfs(&cursor) + nLen <= nMax)
1843 {
1844 ME_DisplayItem *pCurItem = cursor.pRun;
1845 int nCurStart = cursor.nOffset;
1846 int nMatched = 0;
1847
1848 while (pCurItem && ME_CharCompare( *get_text( &pCurItem->member.run, nCurStart + nMatched ), text[nMatched], (flags & FR_MATCHCASE)))
1849 {
1850 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
1851 break;
1852
1853 nMatched++;
1854 if (nMatched == nLen)
1855 {
1856 ME_DisplayItem *pNextItem = pCurItem;
1857 int nNextStart = nCurStart;
1858 WCHAR wNextChar;
1859
1860 /* Check to see if next character is a whitespace */
1861 if (flags & FR_WHOLEWORD)
1862 {
1863 if (nCurStart + nMatched == pCurItem->member.run.len)
1864 {
1865 pNextItem = ME_FindItemFwd(pCurItem, diRun);
1866 nNextStart = -nMatched;
1867 }
1868
1869 if (pNextItem)
1870 wNextChar = *get_text( &pNextItem->member.run, nNextStart + nMatched );
1871 else
1872 wNextChar = ' ';
1873
1874 if (isalnumW(wNextChar))
1875 break;
1876 }
1877
1878 cursor.nOffset += cursor.pPara->member.para.nCharOfs + cursor.pRun->member.run.nCharOfs;
1879 if (chrgText)
1880 {
1881 chrgText->cpMin = cursor.nOffset;
1882 chrgText->cpMax = cursor.nOffset + nLen;
1883 }
1884 TRACE("found at %d-%d\n", cursor.nOffset, cursor.nOffset + nLen);
1885 return cursor.nOffset;
1886 }
1887 if (nCurStart + nMatched == pCurItem->member.run.len)
1888 {
1889 pCurItem = ME_FindItemFwd(pCurItem, diRun);
1890 nCurStart = -nMatched;
1891 }
1892 }
1893 if (pCurItem)
1894 wLastChar = *get_text( &pCurItem->member.run, nCurStart + nMatched );
1895 else
1896 wLastChar = ' ';
1897
1898 cursor.nOffset++;
1899 if (cursor.nOffset == cursor.pRun->member.run.len)
1900 {
1901 ME_NextRun(&cursor.pPara, &cursor.pRun);
1902 cursor.nOffset = 0;
1903 }
1904 }
1905 }
1906 else /* Backward search */
1907 {
1908 /* If possible, find the character after where the search ends */
1909 if ((flags & FR_WHOLEWORD) && nMax < nTextLen - 1)
1910 {
1911 ME_CursorFromCharOfs(editor, nMax + 1, &cursor);
1912 wLastChar = *get_text( &cursor.pRun->member.run, cursor.nOffset );
1913 ME_MoveCursorChars(editor, &cursor, -1);
1914 } else {
1915 ME_CursorFromCharOfs(editor, nMax, &cursor);
1916 }
1917
1918 while (cursor.pRun && ME_GetCursorOfs(&cursor) - nLen >= nMin)
1919 {
1920 ME_DisplayItem *pCurItem = cursor.pRun;
1921 ME_DisplayItem *pCurPara = cursor.pPara;
1922 int nCurEnd = cursor.nOffset;
1923 int nMatched = 0;
1924
1925 if (nCurEnd == 0)
1926 {
1927 ME_PrevRun(&pCurPara, &pCurItem);
1928 nCurEnd = pCurItem->member.run.len;
1929 }
1930
1931 while (pCurItem && ME_CharCompare( *get_text( &pCurItem->member.run, nCurEnd - nMatched - 1 ),
1932 text[nLen - nMatched - 1], (flags & FR_MATCHCASE) ))
1933 {
1934 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
1935 break;
1936
1937 nMatched++;
1938 if (nMatched == nLen)
1939 {
1940 ME_DisplayItem *pPrevItem = pCurItem;
1941 int nPrevEnd = nCurEnd;
1942 WCHAR wPrevChar;
1943 int nStart;
1944
1945 /* Check to see if previous character is a whitespace */
1946 if (flags & FR_WHOLEWORD)
1947 {
1948 if (nPrevEnd - nMatched == 0)
1949 {
1950 pPrevItem = ME_FindItemBack(pCurItem, diRun);
1951 if (pPrevItem)
1952 nPrevEnd = pPrevItem->member.run.len + nMatched;
1953 }
1954
1955 if (pPrevItem)
1956 wPrevChar = *get_text( &pPrevItem->member.run, nPrevEnd - nMatched - 1 );
1957 else
1958 wPrevChar = ' ';
1959
1960 if (isalnumW(wPrevChar))
1961 break;
1962 }
1963
1964 nStart = pCurPara->member.para.nCharOfs
1965 + pCurItem->member.run.nCharOfs + nCurEnd - nMatched;
1966 if (chrgText)
1967 {
1968 chrgText->cpMin = nStart;
1969 chrgText->cpMax = nStart + nLen;
1970 }
1971 TRACE("found at %d-%d\n", nStart, nStart + nLen);
1972 return nStart;
1973 }
1974 if (nCurEnd - nMatched == 0)
1975 {
1976 ME_PrevRun(&pCurPara, &pCurItem);
1977 /* Don't care about pCurItem becoming NULL here; it's already taken
1978 * care of in the exterior loop condition */
1979 nCurEnd = pCurItem->member.run.len + nMatched;
1980 }
1981 }
1982 if (pCurItem)
1983 wLastChar = *get_text( &pCurItem->member.run, nCurEnd - nMatched - 1 );
1984 else
1985 wLastChar = ' ';
1986
1987 cursor.nOffset--;
1988 if (cursor.nOffset < 0)
1989 {
1990 ME_PrevRun(&cursor.pPara, &cursor.pRun);
1991 cursor.nOffset = cursor.pRun->member.run.len;
1992 }
1993 }
1994 }
1995 TRACE("not found\n");
1996 if (chrgText)
1997 chrgText->cpMin = chrgText->cpMax = -1;
1998 return -1;
1999 }
2000
2001 static int ME_GetTextEx(ME_TextEditor *editor, GETTEXTEX *ex, LPARAM pText)
2002 {
2003 int nChars;
2004 ME_Cursor start;
2005
2006 if (!ex->cb || !pText) return 0;
2007
2008 if (ex->flags & ~(GT_SELECTION | GT_USECRLF))
2009 FIXME("GETTEXTEX flags 0x%08x not supported\n", ex->flags & ~(GT_SELECTION | GT_USECRLF));
2010
2011 if (ex->flags & GT_SELECTION)
2012 {
2013 int from, to;
2014 int nStartCur = ME_GetSelectionOfs(editor, &from, &to);
2015 start = editor->pCursors[nStartCur];
2016 nChars = to - from;
2017 }
2018 else
2019 {
2020 ME_SetCursorToStart(editor, &start);
2021 nChars = INT_MAX;
2022 }
2023 if (ex->codepage == CP_UNICODE)
2024 {
2025 return ME_GetTextW(editor, (LPWSTR)pText, ex->cb / sizeof(WCHAR) - 1,
2026 &start, nChars, ex->flags & GT_USECRLF, FALSE);
2027 }
2028 else
2029 {
2030 /* potentially each char may be a CR, why calculate the exact value with O(N) when
2031 we can just take a bigger buffer? :)
2032 The above assumption still holds with CR/LF counters, since CR->CRLF expansion
2033 occurs only in richedit 2.0 mode, in which line breaks have only one CR
2034 */
2035 int crlfmul = (ex->flags & GT_USECRLF) ? 2 : 1;
2036 DWORD buflen;
2037 LPWSTR buffer;
2038 LRESULT rc;
2039
2040 buflen = min(crlfmul * nChars, ex->cb - 1);
2041 buffer = heap_alloc((buflen + 1) * sizeof(WCHAR));
2042
2043 nChars = ME_GetTextW(editor, buffer, buflen, &start, nChars, ex->flags & GT_USECRLF, FALSE);
2044 rc = WideCharToMultiByte(ex->codepage, 0, buffer, nChars + 1,
2045 (LPSTR)pText, ex->cb, ex->lpDefaultChar, ex->lpUsedDefChar);
2046 if (rc) rc--; /* do not count 0 terminator */
2047
2048 heap_free(buffer);
2049 return rc;
2050 }
2051 }
2052
2053 static int ME_GetTextRange(ME_TextEditor *editor, WCHAR *strText,
2054 const ME_Cursor *start, int nLen, BOOL unicode)
2055 {
2056 if (!strText) return 0;
2057 if (unicode) {
2058 return ME_GetTextW(editor, strText, INT_MAX, start, nLen, FALSE, FALSE);
2059 } else {
2060 int nChars;
2061 WCHAR *p = ALLOC_N_OBJ(WCHAR, nLen+1);
2062 if (!p) return 0;
2063 nChars = ME_GetTextW(editor, p, nLen, start, nLen, FALSE, FALSE);
2064 WideCharToMultiByte(CP_ACP, 0, p, nChars+1, (char *)strText,
2065 nLen+1, NULL, NULL);
2066 FREE_OBJ(p);
2067 return nChars;
2068 }
2069 }
2070
2071 static int handle_EM_EXSETSEL( ME_TextEditor *editor, int to, int from )
2072 {
2073 int end;
2074
2075 TRACE("%d - %d\n", to, from );
2076
2077 ME_InvalidateSelection( editor );
2078 end = ME_SetSelection( editor, to, from );
2079 ME_InvalidateSelection( editor );
2080 ITextHost_TxShowCaret( editor->texthost, FALSE );
2081 ME_ShowCaret( editor );
2082 ME_SendSelChange( editor );
2083
2084 return end;
2085 }
2086
2087 typedef struct tagME_GlobalDestStruct
2088 {
2089 HGLOBAL hData;
2090 int nLength;
2091 } ME_GlobalDestStruct;
2092
2093 static DWORD CALLBACK ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
2094 {
2095 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
2096 int i;
2097 WORD *pSrc, *pDest;
2098
2099 cb = cb >> 1;
2100 pDest = (WORD *)lpBuff;
2101 pSrc = GlobalLock(pData->hData);
2102 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
2103 pDest[i] = pSrc[pData->nLength+i];
2104 }
2105 pData->nLength += i;
2106 *pcb = 2*i;
2107 GlobalUnlock(pData->hData);
2108 return 0;
2109 }
2110
2111 static DWORD CALLBACK ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
2112 {
2113 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
2114 int i;
2115 BYTE *pSrc, *pDest;
2116
2117 pDest = lpBuff;
2118 pSrc = GlobalLock(pData->hData);
2119 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
2120 pDest[i] = pSrc[pData->nLength+i];
2121 }
2122 pData->nLength += i;
2123 *pcb = i;
2124 GlobalUnlock(pData->hData);
2125 return 0;
2126 }
2127
2128 static BOOL ME_Paste(ME_TextEditor *editor)
2129 {
2130 DWORD dwFormat = 0;
2131 EDITSTREAM es;
2132 ME_GlobalDestStruct gds;
2133 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
2134 UINT cf = 0;
2135
2136 if (IsClipboardFormatAvailable(nRTFFormat))
2137 cf = nRTFFormat, dwFormat = SF_RTF;
2138 else if (IsClipboardFormatAvailable(CF_UNICODETEXT))
2139 cf = CF_UNICODETEXT, dwFormat = SF_TEXT|SF_UNICODE;
2140 else
2141 return FALSE;
2142
2143 if (!OpenClipboard(editor->hWnd))
2144 return FALSE;
2145 gds.hData = GetClipboardData(cf);
2146 gds.nLength = 0;
2147 es.dwCookie = (DWORD_PTR)&gds;
2148 es.pfnCallback = dwFormat == SF_RTF ? ME_ReadFromHGLOBALRTF : ME_ReadFromHGLOBALUnicode;
2149 ME_StreamIn(editor, dwFormat|SFF_SELECTION, &es, FALSE);
2150
2151 CloseClipboard();
2152 return TRUE;
2153 }
2154
2155 static BOOL ME_Copy(ME_TextEditor *editor, const ME_Cursor *start, int nChars)
2156 {
2157 LPDATAOBJECT dataObj = NULL;
2158 HRESULT hr = S_OK;
2159
2160 if (editor->cPasswordMask)
2161 return FALSE; /* Copying or Cutting masked text isn't allowed */
2162
2163 if(editor->lpOleCallback)
2164 {
2165 CHARRANGE range;
2166 range.cpMin = ME_GetCursorOfs(start);
2167 range.cpMax = range.cpMin + nChars;
2168 hr = IRichEditOleCallback_GetClipboardData(editor->lpOleCallback, &range, RECO_COPY, &dataObj);
2169 }
2170 if(FAILED(hr) || !dataObj)
2171 hr = ME_GetDataObject(editor, start, nChars, &dataObj);
2172 if(SUCCEEDED(hr)) {
2173 hr = OleSetClipboard(dataObj);
2174 IDataObject_Release(dataObj);
2175 }
2176 return SUCCEEDED(hr);
2177 }
2178
2179 /* helper to send a msg filter notification */
2180 static BOOL
2181 ME_FilterEvent(ME_TextEditor *editor, UINT msg, WPARAM* wParam, LPARAM* lParam)
2182 {
2183 MSGFILTER msgf;
2184
2185 if (!editor->hWnd || !editor->hwndParent) return FALSE;
2186 msgf.nmhdr.hwndFrom = editor->hWnd;
2187 msgf.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
2188 msgf.nmhdr.code = EN_MSGFILTER;
2189 msgf.msg = msg;
2190 msgf.wParam = *wParam;
2191 msgf.lParam = *lParam;
2192 if (SendMessageW(editor->hwndParent, WM_NOTIFY, msgf.nmhdr.idFrom, (LPARAM)&msgf))
2193 return FALSE;
2194 *wParam = msgf.wParam;
2195 *lParam = msgf.lParam;
2196 msgf.wParam = *wParam;
2197
2198 return TRUE;
2199 }
2200
2201 static void ME_UpdateSelectionLinkAttribute(ME_TextEditor *editor)
2202 {
2203 ME_DisplayItem *startPara, *endPara;
2204 ME_DisplayItem *prev_para;
2205 ME_Cursor *from, *to;
2206 ME_Cursor start;
2207 int nChars;
2208
2209 if (!editor->AutoURLDetect_bEnable) return;
2210
2211 ME_GetSelection(editor, &from, &to);
2212
2213 /* Find paragraph previous to the one that contains start cursor */
2214 startPara = from->pPara;
2215 prev_para = startPara->member.para.prev_para;
2216 if (prev_para->type == diParagraph) startPara = prev_para;
2217
2218 /* Find paragraph that contains end cursor */
2219 endPara = to->pPara->member.para.next_para;
2220
2221 start.pPara = startPara;
2222 start.pRun = ME_FindItemFwd(startPara, diRun);
2223 start.nOffset = 0;
2224 nChars = endPara->member.para.nCharOfs - startPara->member.para.nCharOfs;
2225
2226 ME_UpdateLinkAttribute(editor, &start, nChars);
2227 }
2228
2229 static BOOL
2230 ME_KeyDown(ME_TextEditor *editor, WORD nKey)
2231 {
2232 BOOL ctrl_is_down = GetKeyState(VK_CONTROL) & 0x8000;
2233 BOOL shift_is_down = GetKeyState(VK_SHIFT) & 0x8000;
2234
2235 if (editor->bMouseCaptured)
2236 return FALSE;
2237 if (nKey != VK_SHIFT && nKey != VK_CONTROL && nKey != VK_MENU)
2238 editor->nSelectionType = stPosition;
2239
2240 switch (nKey)
2241 {
2242 case VK_LEFT:
2243 case VK_RIGHT:
2244 case VK_HOME:
2245 case VK_END:
2246 editor->nUDArrowX = -1;
2247 /* fall through */
2248 case VK_UP:
2249 case VK_DOWN:
2250 case VK_PRIOR:
2251 case VK_NEXT:
2252 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
2253 ME_ArrowKey(editor, nKey, shift_is_down, ctrl_is_down);
2254 return TRUE;
2255 case VK_BACK:
2256 case VK_DELETE:
2257 editor->nUDArrowX = -1;
2258 /* FIXME backspace and delete aren't the same, they act different wrt paragraph style of the merged paragraph */
2259 if (editor->styleFlags & ES_READONLY)
2260 return FALSE;
2261 if (ME_IsSelection(editor))
2262 {
2263 ME_DeleteSelection(editor);
2264 ME_CommitUndo(editor);
2265 }
2266 else if (nKey == VK_DELETE)
2267 {
2268 /* Delete stops group typing.
2269 * (See MSDN remarks on EM_STOPGROUPTYPING message) */
2270 ME_DeleteTextAtCursor(editor, 1, 1);
2271 ME_CommitUndo(editor);
2272 }
2273 else if (ME_ArrowKey(editor, VK_LEFT, FALSE, FALSE))
2274 {
2275 BOOL bDeletionSucceeded;
2276 /* Backspace can be grouped for a single undo */
2277 ME_ContinueCoalescingTransaction(editor);
2278 bDeletionSucceeded = ME_DeleteTextAtCursor(editor, 1, 1);
2279 if (!bDeletionSucceeded && !editor->bEmulateVersion10) { /* v4.1 */
2280 /* Deletion was prevented so the cursor is moved back to where it was.
2281 * (e.g. this happens when trying to delete cell boundaries)
2282 */
2283 ME_ArrowKey(editor, VK_RIGHT, FALSE, FALSE);
2284 }
2285 ME_CommitCoalescingUndo(editor);
2286 }
2287 else
2288 return TRUE;
2289 ME_MoveCursorFromTableRowStartParagraph(editor);
2290 ME_UpdateSelectionLinkAttribute(editor);
2291 ME_UpdateRepaint(editor, FALSE);
2292 ME_SendRequestResize(editor, FALSE);
2293 return TRUE;
2294 case VK_RETURN:
2295 if (editor->bDialogMode)
2296 {
2297 if (ctrl_is_down)
2298 return TRUE;
2299
2300 if (!(editor->styleFlags & ES_WANTRETURN))
2301 {
2302 if (editor->hwndParent)
2303 {
2304 DWORD dw;
2305 dw = SendMessageW(editor->hwndParent, DM_GETDEFID, 0, 0);
2306 if (HIWORD(dw) == DC_HASDEFID)
2307 {
2308 HWND hwDefCtrl = GetDlgItem(editor->hwndParent, LOWORD(dw));
2309 if (hwDefCtrl)
2310 {
2311 SendMessageW(editor->hwndParent, WM_NEXTDLGCTL, (WPARAM)hwDefCtrl, TRUE);
2312 PostMessageW(hwDefCtrl, WM_KEYDOWN, VK_RETURN, 0);
2313 }
2314 }
2315 }
2316 return TRUE;
2317 }
2318 }
2319
2320 if (editor->styleFlags & ES_MULTILINE)
2321 {
2322 ME_Cursor cursor = editor->pCursors[0];
2323 ME_DisplayItem *para = cursor.pPara;
2324 int from, to;
2325 const WCHAR endl = '\r';
2326 const WCHAR endlv10[] = {'\r','\n'};
2327 ME_Style *style;
2328
2329 if (editor->styleFlags & ES_READONLY) {
2330 MessageBeep(MB_ICONERROR);
2331 return TRUE;
2332 }
2333
2334 ME_GetSelectionOfs(editor, &from, &to);
2335 if (editor->nTextLimit > ME_GetTextLength(editor) - (to-from))
2336 {
2337 if (!editor->bEmulateVersion10) { /* v4.1 */
2338 if (para->member.para.nFlags & MEPF_ROWEND) {
2339 /* Add a new table row after this row. */
2340 para = ME_AppendTableRow(editor, para);
2341 para = para->member.para.next_para;
2342 editor->pCursors[0].pPara = para;
2343 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2344 editor->pCursors[0].nOffset = 0;
2345 editor->pCursors[1] = editor->pCursors[0];
2346 ME_CommitUndo(editor);
2347 ME_CheckTablesForCorruption(editor);
2348 ME_UpdateRepaint(editor, FALSE);
2349 return TRUE;
2350 }
2351 else if (para == editor->pCursors[1].pPara &&
2352 cursor.nOffset + cursor.pRun->member.run.nCharOfs == 0 &&
2353 para->member.para.prev_para->member.para.nFlags & MEPF_ROWSTART &&
2354 !para->member.para.prev_para->member.para.nCharOfs)
2355 {
2356 /* Insert a newline before the table. */
2357 para = para->member.para.prev_para;
2358 para->member.para.nFlags &= ~MEPF_ROWSTART;
2359 editor->pCursors[0].pPara = para;
2360 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2361 editor->pCursors[1] = editor->pCursors[0];
2362 ME_InsertTextFromCursor(editor, 0, &endl, 1,
2363 editor->pCursors[0].pRun->member.run.style);
2364 para = editor->pBuffer->pFirst->member.para.next_para;
2365 ME_SetDefaultParaFormat(editor, para->member.para.pFmt);
2366 para->member.para.nFlags = MEPF_REWRAP;
2367 editor->pCursors[0].pPara = para;
2368 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2369 editor->pCursors[1] = editor->pCursors[0];
2370 para->member.para.next_para->member.para.nFlags |= MEPF_ROWSTART;
2371 ME_CommitCoalescingUndo(editor);
2372 ME_CheckTablesForCorruption(editor);
2373 ME_UpdateRepaint(editor, FALSE);
2374 return TRUE;
2375 }
2376 } else { /* v1.0 - 3.0 */
2377 ME_DisplayItem *para = cursor.pPara;
2378 if (ME_IsInTable(para))
2379 {
2380 if (cursor.pRun->member.run.nFlags & MERF_ENDPARA)
2381 {
2382 if (from == to) {
2383 ME_ContinueCoalescingTransaction(editor);
2384 para = ME_AppendTableRow(editor, para);
2385 editor->pCursors[0].pPara = para;
2386 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2387 editor->pCursors[0].nOffset = 0;
2388 editor->pCursors[1] = editor->pCursors[0];
2389 ME_CommitCoalescingUndo(editor);
2390 ME_UpdateRepaint(editor, FALSE);
2391 return TRUE;
2392 }
2393 } else {
2394 ME_ContinueCoalescingTransaction(editor);
2395 if (cursor.pRun->member.run.nCharOfs + cursor.nOffset == 0 &&
2396 !ME_IsInTable(para->member.para.prev_para))
2397 {
2398 /* Insert newline before table */
2399 cursor.pRun = ME_FindItemBack(para, diRun);
2400 if (cursor.pRun) {
2401 editor->pCursors[0].pRun = cursor.pRun;
2402 editor->pCursors[0].pPara = para->member.para.prev_para;
2403 }
2404 editor->pCursors[0].nOffset = 0;
2405 editor->pCursors[1] = editor->pCursors[0];
2406 ME_InsertTextFromCursor(editor, 0, &endl, 1,
2407 editor->pCursors[0].pRun->member.run.style);
2408 } else {
2409 editor->pCursors[1] = editor->pCursors[0];
2410 para = ME_AppendTableRow(editor, para);
2411 editor->pCursors[0].pPara = para;
2412 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2413 editor->pCursors[0].nOffset = 0;
2414 editor->pCursors[1] = editor->pCursors[0];
2415 }
2416 ME_CommitCoalescingUndo(editor);
2417 ME_UpdateRepaint(editor, FALSE);
2418 return TRUE;
2419 }
2420 }
2421 }
2422
2423 style = ME_GetInsertStyle(editor, 0);
2424 ME_SaveTempStyle(editor);
2425 ME_ContinueCoalescingTransaction(editor);
2426 if (shift_is_down)
2427 ME_InsertEndRowFromCursor(editor, 0);
2428 else
2429 if (!editor->bEmulateVersion10)
2430 ME_InsertTextFromCursor(editor, 0, &endl, 1, style);
2431 else
2432 ME_InsertTextFromCursor(editor, 0, endlv10, 2, style);
2433 ME_ReleaseStyle(style);
2434 ME_CommitCoalescingUndo(editor);
2435 SetCursor(NULL);
2436
2437 ME_UpdateSelectionLinkAttribute(editor);
2438 ME_UpdateRepaint(editor, FALSE);
2439 }
2440 return TRUE;
2441 }
2442 break;
2443 case VK_ESCAPE:
2444 if (editor->bDialogMode && editor->hwndParent)
2445 PostMessageW(editor->hwndParent, WM_CLOSE, 0, 0);
2446 return TRUE;
2447 case VK_TAB:
2448 if (editor->bDialogMode && editor->hwndParent)
2449 SendMessageW(editor->hwndParent, WM_NEXTDLGCTL, shift_is_down, 0);
2450 return TRUE;
2451 case 'A':
2452 if (ctrl_is_down)
2453 {
2454 handle_EM_EXSETSEL( editor, 0, -1 );
2455 return TRUE;
2456 }
2457 break;
2458 case 'V':
2459 if (ctrl_is_down)
2460 return ME_Paste(editor);
2461 break;
2462 case 'C':
2463 case 'X':
2464 if (ctrl_is_down)
2465 {
2466 BOOL result;
2467 int nOfs, nChars;
2468 int nStartCur = ME_GetSelectionOfs(editor, &nOfs, &nChars);
2469 ME_Cursor *selStart = &editor->pCursors[nStartCur];
2470
2471 nChars -= nOfs;
2472 result = ME_Copy(editor, selStart, nChars);
2473 if (result && nKey == 'X')
2474 {
2475 ME_InternalDeleteText(editor, selStart, nChars, FALSE);
2476 ME_CommitUndo(editor);
2477 ME_UpdateRepaint(editor, TRUE);
2478 }
2479 return result;
2480 }
2481 break;
2482 case 'Z':
2483 if (ctrl_is_down)
2484 {
2485 ME_Undo(editor);
2486 return TRUE;
2487 }
2488 break;
2489 case 'Y':
2490 if (ctrl_is_down)
2491 {
2492 ME_Redo(editor);
2493 return TRUE;
2494 }
2495 break;
2496
2497 default:
2498 if (nKey != VK_SHIFT && nKey != VK_CONTROL && nKey && nKey != VK_MENU)
2499 editor->nUDArrowX = -1;
2500 if (ctrl_is_down)
2501 {
2502 if (nKey == 'W')
2503 {
2504 CHARFORMAT2W chf;
2505 char buf[2048];
2506 chf.cbSize = sizeof(chf);
2507
2508 ME_GetSelectionCharFormat(editor, &chf);
2509 ME_DumpStyleToBuf(&chf, buf);
2510 MessageBoxA(NULL, buf, "Style dump", MB_OK);
2511 }
2512 if (nKey == 'Q')
2513 {
2514 ME_CheckCharOffsets(editor);
2515 }
2516 }
2517 }
2518 return FALSE;
2519 }
2520
2521 static LRESULT ME_Char(ME_TextEditor *editor, WPARAM charCode,
2522 LPARAM flags, BOOL unicode)
2523 {
2524 WCHAR wstr;
2525
2526 if (editor->bMouseCaptured)
2527 return 0;
2528
2529 if (unicode)
2530 wstr = (WCHAR)charCode;
2531 else
2532 {
2533 CHAR charA = charCode;
2534 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &wstr, 1);
2535 }
2536
2537 if (editor->styleFlags & ES_READONLY) {
2538 MessageBeep(MB_ICONERROR);
2539 return 0; /* FIXME really 0 ? */
2540 }
2541
2542 if ((unsigned)wstr >= ' ' || wstr == '\t')
2543 {
2544 ME_Cursor cursor = editor->pCursors[0];
2545 ME_DisplayItem *para = cursor.pPara;
2546 int from, to;
2547 BOOL ctrl_is_down = GetKeyState(VK_CONTROL) & 0x8000;
2548 ME_GetSelectionOfs(editor, &from, &to);
2549 if (wstr == '\t' &&
2550 /* v4.1 allows tabs to be inserted with ctrl key down */
2551 !(ctrl_is_down && !editor->bEmulateVersion10))
2552 {
2553 ME_DisplayItem *para;
2554 BOOL bSelectedRow = FALSE;
2555
2556 para = cursor.pPara;
2557 if (ME_IsSelection(editor) &&
2558 cursor.pRun->member.run.nCharOfs + cursor.nOffset == 0 &&
2559 to == ME_GetCursorOfs(&editor->pCursors[0]) &&
2560 para->member.para.prev_para->type == diParagraph)
2561 {
2562 para = para->member.para.prev_para;
2563 bSelectedRow = TRUE;
2564 }
2565 if (ME_IsInTable(para))
2566 {
2567 ME_TabPressedInTable(editor, bSelectedRow);
2568 ME_CommitUndo(editor);
2569 return 0;
2570 }
2571 } else if (!editor->bEmulateVersion10) { /* v4.1 */
2572 if (para->member.para.nFlags & MEPF_ROWEND) {
2573 if (from == to) {
2574 para = para->member.para.next_para;
2575 if (para->member.para.nFlags & MEPF_ROWSTART)
2576 para = para->member.para.next_para;
2577 editor->pCursors[0].pPara = para;
2578 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2579 editor->pCursors[0].nOffset = 0;
2580 editor->pCursors[1] = editor->pCursors[0];
2581 }
2582 }
2583 } else { /* v1.0 - 3.0 */
2584 if (ME_IsInTable(cursor.pRun) &&
2585 cursor.pRun->member.run.nFlags & MERF_ENDPARA &&
2586 from == to)
2587 {
2588 /* Text should not be inserted at the end of the table. */
2589 MessageBeep(-1);
2590 return 0;
2591 }
2592 }
2593 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
2594 /* WM_CHAR is restricted to nTextLimit */
2595 if(editor->nTextLimit > ME_GetTextLength(editor) - (to-from))
2596 {
2597 ME_Style *style = ME_GetInsertStyle(editor, 0);
2598 ME_SaveTempStyle(editor);
2599 ME_ContinueCoalescingTransaction(editor);
2600 ME_InsertTextFromCursor(editor, 0, &wstr, 1, style);
2601 ME_ReleaseStyle(style);
2602 ME_CommitCoalescingUndo(editor);
2603 ITextHost_TxSetCursor(editor->texthost, NULL, FALSE);
2604 }
2605
2606 ME_UpdateSelectionLinkAttribute(editor);
2607 ME_UpdateRepaint(editor, FALSE);
2608 }
2609 return 0;
2610 }
2611
2612 /* Process the message and calculate the new click count.
2613 *
2614 * returns: The click count if it is mouse down event, else returns 0. */
2615 static int ME_CalculateClickCount(ME_TextEditor *editor, UINT msg, WPARAM wParam,
2616 LPARAM lParam)
2617 {
2618 static int clickNum = 0;
2619 if (msg < WM_MOUSEFIRST || msg > WM_MOUSELAST)
2620 return 0;
2621
2622 if ((msg == WM_LBUTTONDBLCLK) ||
2623 (msg == WM_RBUTTONDBLCLK) ||
2624 (msg == WM_MBUTTONDBLCLK) ||
2625 (msg == WM_XBUTTONDBLCLK))
2626 {
2627 msg -= (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN);
2628 }
2629
2630 if ((msg == WM_LBUTTONDOWN) ||
2631 (msg == WM_RBUTTONDOWN) ||
2632 (msg == WM_MBUTTONDOWN) ||
2633 (msg == WM_XBUTTONDOWN))
2634 {
2635 static MSG prevClickMsg;
2636 MSG clickMsg;
2637 /* Compare the editor instead of the hwnd so that the this
2638 * can still be done for windowless richedit controls. */
2639 clickMsg.hwnd = (HWND)editor;
2640 clickMsg.message = msg;
2641 clickMsg.wParam = wParam;
2642 clickMsg.lParam = lParam;
2643 clickMsg.time = GetMessageTime();
2644 clickMsg.pt.x = (short)LOWORD(lParam);
2645 clickMsg.pt.y = (short)HIWORD(lParam);
2646 if ((clickNum != 0) &&
2647 (clickMsg.message == prevClickMsg.message) &&
2648 (clickMsg.hwnd == prevClickMsg.hwnd) &&
2649 (clickMsg.wParam == prevClickMsg.wParam) &&
2650 (clickMsg.time - prevClickMsg.time < GetDoubleClickTime()) &&
2651 (abs(clickMsg.pt.x - prevClickMsg.pt.x) < GetSystemMetrics(SM_CXDOUBLECLK)/2) &&
2652 (abs(clickMsg.pt.y - prevClickMsg.pt.y) < GetSystemMetrics(SM_CYDOUBLECLK)/2))
2653 {
2654 clickNum++;
2655 } else {
2656 clickNum = 1;
2657 }
2658 prevClickMsg = clickMsg;
2659 } else {
2660 return 0;
2661 }
2662 return clickNum;
2663 }
2664
2665 static BOOL ME_SetCursor(ME_TextEditor *editor)
2666 {
2667 ME_Cursor cursor;
2668 POINT pt;
2669 BOOL isExact;
2670 SCROLLBARINFO sbi;
2671 DWORD messagePos = GetMessagePos();
2672 pt.x = (short)LOWORD(messagePos);
2673 pt.y = (short)HIWORD(messagePos);
2674
2675 if (editor->hWnd)
2676 {
2677 sbi.cbSize = sizeof(sbi);
2678 GetScrollBarInfo(editor->hWnd, OBJID_HSCROLL, &sbi);
2679 if (!(sbi.rgstate[0] & (STATE_SYSTEM_INVISIBLE|STATE_SYSTEM_OFFSCREEN)) &&
2680 PtInRect(&sbi.rcScrollBar, pt))
2681 {
2682 ITextHost_TxSetCursor(editor->texthost,
2683 LoadCursorW(NULL, (WCHAR*)IDC_ARROW), FALSE);
2684 return TRUE;
2685 }
2686 sbi.cbSize = sizeof(sbi);
2687 GetScrollBarInfo(editor->hWnd, OBJID_VSCROLL, &sbi);
2688 if (!(sbi.rgstate[0] & (STATE_SYSTEM_INVISIBLE|STATE_SYSTEM_OFFSCREEN)) &&
2689 PtInRect(&sbi.rcScrollBar, pt))
2690 {
2691 ITextHost_TxSetCursor(editor->texthost,
2692 LoadCursorW(NULL, (WCHAR*)IDC_ARROW), FALSE);
2693 return TRUE;
2694 }
2695 }
2696 ITextHost_TxScreenToClient(editor->texthost, &pt);
2697
2698 if (editor->nSelectionType == stLine && editor->bMouseCaptured) {
2699 ITextHost_TxSetCursor(editor->texthost, hLeft, FALSE);
2700 return TRUE;
2701 }
2702 if (!editor->bEmulateVersion10 /* v4.1 */ &&
2703 pt.y < editor->rcFormat.top &&
2704 pt.x < editor->rcFormat.left)
2705 {
2706 ITextHost_TxSetCursor(editor->texthost, hLeft, FALSE);
2707 return TRUE;
2708 }
2709 if (pt.y < editor->rcFormat.top || pt.y > editor->rcFormat.bottom)
2710 {
2711 if (editor->bEmulateVersion10) /* v1.0 - 3.0 */
2712 ITextHost_TxSetCursor(editor->texthost,
2713 LoadCursorW(NULL, (WCHAR*)IDC_ARROW), FALSE);
2714 else /* v4.1 */
2715 ITextHost_TxSetCursor(editor->texthost,
2716 LoadCursorW(NULL, (WCHAR*)IDC_IBEAM), TRUE);
2717 return TRUE;
2718 }
2719 if (pt.x < editor->rcFormat.left)
2720 {
2721 ITextHost_TxSetCursor(editor->texthost, hLeft, FALSE);
2722 return TRUE;
2723 }
2724 ME_CharFromPos(editor, pt.x, pt.y, &cursor, &isExact);
2725 if (isExact)
2726 {
2727 ME_Run *run;
2728
2729 run = &cursor.pRun->member.run;
2730 if (run->style->fmt.dwMask & CFM_LINK &&
2731 run->style->fmt.dwEffects & CFE_LINK)
2732 {
2733 ITextHost_TxSetCursor(editor->texthost,
2734 LoadCursorW(NULL, (WCHAR*)IDC_HAND),
2735 FALSE);
2736 return TRUE;
2737 }
2738
2739 if (ME_IsSelection(editor))
2740 {
2741 int selStart, selEnd;
2742 int offset = ME_GetCursorOfs(&cursor);
2743
2744 ME_GetSelectionOfs(editor, &selStart, &selEnd);
2745 if (selStart <= offset && selEnd >= offset) {
2746 ITextHost_TxSetCursor(editor->texthost,
2747 LoadCursorW(NULL, (WCHAR*)IDC_ARROW),
2748 FALSE);
2749 return TRUE;
2750 }
2751 }
2752 }
2753 ITextHost_TxSetCursor(editor->texthost,
2754 LoadCursorW(NULL, (WCHAR*)IDC_IBEAM), TRUE);
2755 return TRUE;
2756 }
2757
2758 static void ME_SetDefaultFormatRect(ME_TextEditor *editor)
2759 {
2760 ITextHost_TxGetClientRect(editor->texthost, &editor->rcFormat);
2761 editor->rcFormat.top += editor->exStyleFlags & WS_EX_CLIENTEDGE ? 1 : 0;
2762 editor->rcFormat.left += 1 + editor->selofs;
2763 editor->rcFormat.right -= 1;
2764 }
2765
2766 static BOOL ME_ShowContextMenu(ME_TextEditor *editor, int x, int y)
2767 {
2768 CHARRANGE selrange;
2769 HMENU menu;
2770 int seltype = 0;
2771 if(!editor->lpOleCallback || !editor->hWnd)
2772 return FALSE;
2773 ME_GetSelectionOfs(editor, &selrange.cpMin, &selrange.cpMax);
2774 if(selrange.cpMin == selrange.cpMax)
2775 seltype |= SEL_EMPTY;
2776 else
2777 {
2778 /* FIXME: Handle objects */
2779 seltype |= SEL_TEXT;
2780 if(selrange.cpMax-selrange.cpMin > 1)
2781 seltype |= SEL_MULTICHAR;
2782 }
2783 if(SUCCEEDED(IRichEditOleCallback_GetContextMenu(editor->lpOleCallback, seltype, NULL, &selrange, &menu)))
2784 {
2785 TrackPopupMenu(menu, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, editor->hwndParent, NULL);
2786 DestroyMenu(menu);
2787 }
2788 return TRUE;
2789 }
2790
2791 ME_TextEditor *ME_MakeEditor(ITextHost *texthost, BOOL bEmulateVersion10, DWORD csStyle)
2792 {
2793 ME_TextEditor *ed = ALLOC_OBJ(ME_TextEditor);
2794 int i;
2795 DWORD props;
2796 LONG selbarwidth;
2797
2798 ed->hWnd = NULL;
2799 ed->hwndParent = NULL;
2800 ed->sizeWindow.cx = ed->sizeWindow.cy = 0;
2801 ed->texthost = texthost;
2802 ed->reOle = NULL;
2803 ed->bEmulateVersion10 = bEmulateVersion10;
2804 ed->styleFlags = 0;
2805 ed->alignStyle = PFA_LEFT;
2806 if (csStyle & ES_RIGHT)
2807 ed->alignStyle = PFA_RIGHT;
2808 if (csStyle & ES_CENTER)
2809 ed->alignStyle = PFA_CENTER;
2810 ITextHost_TxGetPropertyBits(texthost,
2811 (TXTBIT_RICHTEXT|TXTBIT_MULTILINE|
2812 TXTBIT_READONLY|TXTBIT_USEPASSWORD|
2813 TXTBIT_HIDESELECTION|TXTBIT_SAVESELECTION|
2814 TXTBIT_AUTOWORDSEL|TXTBIT_VERTICAL|
2815 TXTBIT_WORDWRAP|TXTBIT_DISABLEDRAG),
2816 &props);
2817 ITextHost_TxGetScrollBars(texthost, &ed->styleFlags);
2818 ed->styleFlags &= (WS_VSCROLL|WS_HSCROLL|ES_AUTOVSCROLL|
2819 ES_AUTOHSCROLL|ES_DISABLENOSCROLL);
2820 ed->pBuffer = ME_MakeText();
2821 ed->nZoomNumerator = ed->nZoomDenominator = 0;
2822 ed->nAvailWidth = 0; /* wrap to client area */
2823 ME_MakeFirstParagraph(ed);
2824 /* The four cursors are for:
2825 * 0 - The position where the caret is shown
2826 * 1 - The anchored end of the selection (for normal selection)
2827 * 2 & 3 - The anchored start and end respectively for word, line,
2828 * or paragraph selection.
2829 */
2830 ed->nCursors = 4;
2831 ed->pCursors = ALLOC_N_OBJ(ME_Cursor, ed->nCursors);
2832 ME_SetCursorToStart(ed, &ed->pCursors[0]);
2833 ed->pCursors[1] = ed->pCursors[0];
2834 ed->pCursors[2] = ed->pCursors[0];
2835 ed->pCursors[3] = ed->pCursors[1];
2836 ed->nLastTotalLength = ed->nTotalLength = 0;
2837 ed->nLastTotalWidth = ed->nTotalWidth = 0;
2838 ed->nUDArrowX = -1;
2839 ed->rgbBackColor = -1;
2840 ed->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
2841 ed->bCaretAtEnd = FALSE;
2842 ed->nEventMask = 0;
2843 ed->nModifyStep = 0;
2844 ed->nTextLimit = TEXT_LIMIT_DEFAULT;
2845 list_init( &ed->undo_stack );
2846 list_init( &ed->redo_stack );
2847 ed->nUndoStackSize = 0;
2848 ed->nUndoLimit = STACK_SIZE_DEFAULT;
2849 ed->nUndoMode = umAddToUndo;
2850 ed->nParagraphs = 1;
2851 ed->nLastSelStart = ed->nLastSelEnd = 0;
2852 ed->pLastSelStartPara = ed->pLastSelEndPara = ed->pCursors[0].pPara;
2853 ed->bHideSelection = FALSE;
2854 ed->pfnWordBreak = NULL;
2855 ed->lpOleCallback = NULL;
2856 ed->mode = TM_MULTILEVELUNDO | TM_MULTICODEPAGE;
2857 ed->mode |= (props & TXTBIT_RICHTEXT) ? TM_RICHTEXT : TM_PLAINTEXT;
2858 ed->AutoURLDetect_bEnable = FALSE;
2859 ed->bHaveFocus = FALSE;
2860 ed->bDialogMode = FALSE;
2861 ed->bMouseCaptured = FALSE;
2862 for (i=0; i<HFONT_CACHE_SIZE; i++)
2863 {
2864 ed->pFontCache[i].nRefs = 0;
2865 ed->pFontCache[i].nAge = 0;
2866 ed->pFontCache[i].hFont = NULL;
2867 }
2868
2869 ME_CheckCharOffsets(ed);
2870 ed->bDefaultFormatRect = TRUE;
2871 ITextHost_TxGetSelectionBarWidth(ed->texthost, &selbarwidth);
2872 if (selbarwidth) {
2873 /* FIXME: Convert selbarwidth from HIMETRIC to pixels */
2874 ed->selofs = SELECTIONBAR_WIDTH;
2875 ed->styleFlags |= ES_SELECTIONBAR;
2876 } else {
2877 ed->selofs = 0;
2878 }
2879 ed->nSelectionType = stPosition;
2880
2881 ed->cPasswordMask = 0;
2882 if (props & TXTBIT_USEPASSWORD)
2883 ITextHost_TxGetPasswordChar(texthost, &ed->cPasswordMask);
2884
2885 if (props & TXTBIT_AUTOWORDSEL)
2886 ed->styleFlags |= ECO_AUTOWORDSELECTION;
2887 if (props & TXTBIT_MULTILINE) {
2888 ed->styleFlags |= ES_MULTILINE;
2889 ed->bWordWrap = (props & TXTBIT_WORDWRAP) != 0;
2890 } else {
2891 ed->bWordWrap = FALSE;
2892 }
2893 if (props & TXTBIT_READONLY)
2894 ed->styleFlags |= ES_READONLY;
2895 if (!(props & TXTBIT_HIDESELECTION))
2896 ed->styleFlags |= ES_NOHIDESEL;
2897 if (props & TXTBIT_SAVESELECTION)
2898 ed->styleFlags |= ES_SAVESEL;
2899 if (props & TXTBIT_VERTICAL)
2900 ed->styleFlags |= ES_VERTICAL;
2901 if (props & TXTBIT_DISABLEDRAG)
2902 ed->styleFlags |= ES_NOOLEDRAGDROP;
2903
2904 ed->notified_cr.cpMin = ed->notified_cr.cpMax = 0;
2905
2906 /* Default scrollbar information */
2907 ed->vert_si.cbSize = sizeof(SCROLLINFO);
2908 ed->vert_si.nMin = 0;
2909 ed->vert_si.nMax = 0;
2910 ed->vert_si.nPage = 0;
2911 ed->vert_si.nPos = 0;
2912
2913 ed->horz_si.cbSize = sizeof(SCROLLINFO);
2914 ed->horz_si.nMin = 0;
2915 ed->horz_si.nMax = 0;
2916 ed->horz_si.nPage = 0;
2917 ed->horz_si.nPos = 0;
2918
2919 ed->wheel_remain = 0;
2920
2921 list_init( &ed->style_list );
2922 OleInitialize(NULL);
2923
2924 return ed;
2925 }
2926
2927 void ME_DestroyEditor(ME_TextEditor *editor)
2928 {
2929 ME_DisplayItem *pFirst = editor->pBuffer->pFirst;
2930 ME_DisplayItem *p = pFirst, *pNext = NULL;
2931 ME_Style *s, *cursor2;
2932 int i;
2933
2934 ME_ClearTempStyle(editor);
2935 ME_EmptyUndoStack(editor);
2936 while(p) {
2937 pNext = p->next;
2938 ME_DestroyDisplayItem(p);
2939 p = pNext;
2940 }
2941
2942 LIST_FOR_EACH_ENTRY_SAFE( s, cursor2, &editor->style_list, ME_Style, entry )
2943 ME_DestroyStyle( s );
2944
2945 ME_ReleaseStyle(editor->pBuffer->pDefaultStyle);
2946 for (i=0; i<HFONT_CACHE_SIZE; i++)
2947 {
2948 if (editor->pFontCache[i].hFont)
2949 DeleteObject(editor->pFontCache[i].hFont);
2950 }
2951 if (editor->rgbBackColor != -1)
2952 DeleteObject(editor->hbrBackground);
2953 if(editor->lpOleCallback)
2954 IRichEditOleCallback_Release(editor->lpOleCallback);
2955 ITextHost_Release(editor->texthost);
2956 if (editor->reOle)
2957 {
2958 IRichEditOle_Release(editor->reOle);
2959 editor->reOle = NULL;
2960 }
2961 OleUninitialize();
2962
2963 FREE_OBJ(editor->pBuffer);
2964 FREE_OBJ(editor->pCursors);
2965
2966 FREE_OBJ(editor);
2967 }
2968
2969 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
2970 {
2971 TRACE("\n");
2972 switch (fdwReason)
2973 {
2974 case DLL_PROCESS_ATTACH:
2975 DisableThreadLibraryCalls(hinstDLL);
2976 me_heap = HeapCreate (0, 0x10000, 0);
2977 if (!ME_RegisterEditorClass(hinstDLL)) return FALSE;
2978 hLeft = LoadCursorW(hinstDLL, MAKEINTRESOURCEW(OCR_REVERSE));
2979 LookupInit();
2980 break;
2981
2982 case DLL_PROCESS_DETACH:
2983 if (lpvReserved) break;
2984 UnregisterClassW(RICHEDIT_CLASS20W, 0);
2985 UnregisterClassW(MSFTEDIT_CLASS, 0);
2986 UnregisterClassA(RICHEDIT_CLASS20A, 0);
2987 UnregisterClassA("RichEdit50A", 0);
2988 if (ME_ListBoxRegistered)
2989 UnregisterClassW(REListBox20W, 0);
2990 if (ME_ComboBoxRegistered)
2991 UnregisterClassW(REComboBox20W, 0);
2992 LookupCleanup();
2993 HeapDestroy (me_heap);
2994 release_typelib();
2995 break;
2996 }
2997 return TRUE;
2998 }
2999
3000 static inline int get_default_line_height( ME_TextEditor *editor )
3001 {
3002 int height = 0;
3003
3004 if (editor->pBuffer && editor->pBuffer->pDefaultStyle)
3005 height = editor->pBuffer->pDefaultStyle->tm.tmHeight;
3006 if (height <= 0) height = 24;
3007
3008 return height;
3009 }
3010
3011 static inline int calc_wheel_change( int *remain, int amount_per_click )
3012 {
3013 int change = amount_per_click * (float)*remain / WHEEL_DELTA;
3014 *remain -= WHEEL_DELTA * change / amount_per_click;
3015 return change;
3016 }
3017
3018 static const char * const edit_messages[] = {
3019 "EM_GETSEL",
3020 "EM_SETSEL",
3021 "EM_GETRECT",
3022 "EM_SETRECT",
3023 "EM_SETRECTNP",
3024 "EM_SCROLL",
3025 "EM_LINESCROLL",
3026 "EM_SCROLLCARET",
3027 "EM_GETMODIFY",
3028 "EM_SETMODIFY",
3029 "EM_GETLINECOUNT",
3030 "EM_LINEINDEX",
3031 "EM_SETHANDLE",
3032 "EM_GETHANDLE",
3033 "EM_GETTHUMB",
3034 "EM_UNKNOWN_BF",
3035 "EM_UNKNOWN_C0",
3036 "EM_LINELENGTH",
3037 "EM_REPLACESEL",
3038 "EM_UNKNOWN_C3",
3039 "EM_GETLINE",
3040 "EM_LIMITTEXT",
3041 "EM_CANUNDO",
3042 "EM_UNDO",
3043 "EM_FMTLINES",
3044 "EM_LINEFROMCHAR",
3045 "EM_UNKNOWN_CA",
3046 "EM_SETTABSTOPS",
3047 "EM_SETPASSWORDCHAR",
3048 "EM_EMPTYUNDOBUFFER",
3049 "EM_GETFIRSTVISIBLELINE",