[RICHED20] Sync with Wine Staging 1.9.4. CORE-10912
[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 fmt.wEffects = 0;
548
549 switch(info->rtfMinor)
550 {
551 case rtfParDef: /* restores default paragraph attributes */
552 if (!info->editor->bEmulateVersion10) /* v4.1 */
553 info->borderType = RTFBorderParaLeft;
554 else /* v1.0 - 3.0 */
555 info->borderType = RTFBorderParaTop;
556 fmt.dwMask = PFM_ALIGNMENT | PFM_BORDER | PFM_LINESPACING | PFM_TABSTOPS |
557 PFM_OFFSET | PFM_RIGHTINDENT | PFM_SPACEAFTER | PFM_SPACEBEFORE |
558 PFM_STARTINDENT;
559 /* TODO: numbering, shading */
560 fmt.wAlignment = PFA_LEFT;
561 fmt.cTabCount = 0;
562 fmt.dxOffset = fmt.dxStartIndent = fmt.dxRightIndent = 0;
563 fmt.wBorderWidth = fmt.wBorders = 0;
564 fmt.wBorderSpace = 0;
565 fmt.bLineSpacingRule = 0;
566 fmt.dySpaceBefore = fmt.dySpaceAfter = 0;
567 fmt.dyLineSpacing = 0;
568 if (!info->editor->bEmulateVersion10) /* v4.1 */
569 {
570 if (info->tableDef && info->tableDef->tableRowStart &&
571 info->tableDef->tableRowStart->member.para.nFlags & MEPF_ROWEND)
572 {
573 ME_Cursor cursor;
574 ME_DisplayItem *para;
575 /* We are just after a table row. */
576 RTFFlushOutputBuffer(info);
577 cursor = info->editor->pCursors[0];
578 para = cursor.pPara;
579 if (para == info->tableDef->tableRowStart->member.para.next_para
580 && !cursor.nOffset && !cursor.pRun->member.run.nCharOfs)
581 {
582 /* Since the table row end, no text has been inserted, and the \intbl
583 * control word has not be used. We can confirm that we are not in a
584 * table anymore.
585 */
586 info->tableDef->tableRowStart = NULL;
587 info->canInheritInTbl = FALSE;
588 }
589 }
590 } else { /* v1.0 - v3.0 */
591 fmt.dwMask |= PFM_TABLE;
592 fmt.wEffects &= ~PFE_TABLE;
593 }
594 break;
595 case rtfNestLevel:
596 if (!info->editor->bEmulateVersion10) /* v4.1 */
597 {
598 while (info->rtfParam > info->nestingLevel) {
599 RTFTable *tableDef = ALLOC_OBJ(RTFTable);
600 ZeroMemory(tableDef, sizeof(RTFTable));
601 tableDef->parent = info->tableDef;
602 info->tableDef = tableDef;
603
604 RTFFlushOutputBuffer(info);
605 if (tableDef->tableRowStart &&
606 tableDef->tableRowStart->member.para.nFlags & MEPF_ROWEND)
607 {
608 ME_DisplayItem *para = tableDef->tableRowStart;
609 para = para->member.para.next_para;
610 para = ME_InsertTableRowStartAtParagraph(info->editor, para);
611 tableDef->tableRowStart = para;
612 } else {
613 ME_Cursor cursor;
614 WCHAR endl = '\r';
615 cursor = info->editor->pCursors[0];
616 if (cursor.nOffset || cursor.pRun->member.run.nCharOfs)
617 ME_InsertTextFromCursor(info->editor, 0, &endl, 1, info->style);
618 tableDef->tableRowStart = ME_InsertTableRowStartFromCursor(info->editor);
619 }
620
621 info->nestingLevel++;
622 }
623 info->canInheritInTbl = FALSE;
624 }
625 break;
626 case rtfInTable:
627 {
628 if (!info->editor->bEmulateVersion10) /* v4.1 */
629 {
630 if (info->nestingLevel < 1)
631 {
632 RTFTable *tableDef;
633 if (!info->tableDef)
634 {
635 info->tableDef = ALLOC_OBJ(RTFTable);
636 ZeroMemory(info->tableDef, sizeof(RTFTable));
637 }
638 tableDef = info->tableDef;
639 RTFFlushOutputBuffer(info);
640 if (tableDef->tableRowStart &&
641 tableDef->tableRowStart->member.para.nFlags & MEPF_ROWEND)
642 {
643 ME_DisplayItem *para = tableDef->tableRowStart;
644 para = para->member.para.next_para;
645 para = ME_InsertTableRowStartAtParagraph(info->editor, para);
646 tableDef->tableRowStart = para;
647 } else {
648 ME_Cursor cursor;
649 WCHAR endl = '\r';
650 cursor = info->editor->pCursors[0];
651 if (cursor.nOffset || cursor.pRun->member.run.nCharOfs)
652 ME_InsertTextFromCursor(info->editor, 0, &endl, 1, info->style);
653 tableDef->tableRowStart = ME_InsertTableRowStartFromCursor(info->editor);
654 }
655 info->nestingLevel = 1;
656 info->canInheritInTbl = TRUE;
657 }
658 return;
659 } else { /* v1.0 - v3.0 */
660 fmt.dwMask |= PFM_TABLE;
661 fmt.wEffects |= PFE_TABLE;
662 }
663 break;
664 }
665 case rtfFirstIndent:
666 ME_GetSelectionParaFormat(info->editor, &fmt);
667 fmt.dwMask = PFM_STARTINDENT | PFM_OFFSET;
668 fmt.dxStartIndent += fmt.dxOffset + info->rtfParam;
669 fmt.dxOffset = -info->rtfParam;
670 break;
671 case rtfLeftIndent:
672 ME_GetSelectionParaFormat(info->editor, &fmt);
673 fmt.dwMask = PFM_STARTINDENT;
674 fmt.dxStartIndent = info->rtfParam - fmt.dxOffset;
675 break;
676 case rtfRightIndent:
677 fmt.dwMask = PFM_RIGHTINDENT;
678 fmt.dxRightIndent = info->rtfParam;
679 break;
680 case rtfQuadLeft:
681 case rtfQuadJust:
682 fmt.dwMask = PFM_ALIGNMENT;
683 fmt.wAlignment = PFA_LEFT;
684 break;
685 case rtfQuadRight:
686 fmt.dwMask = PFM_ALIGNMENT;
687 fmt.wAlignment = PFA_RIGHT;
688 break;
689 case rtfQuadCenter:
690 fmt.dwMask = PFM_ALIGNMENT;
691 fmt.wAlignment = PFA_CENTER;
692 break;
693 case rtfTabPos:
694 ME_GetSelectionParaFormat(info->editor, &fmt);
695 if (!(fmt.dwMask & PFM_TABSTOPS))
696 {
697 fmt.cTabCount = 0;
698 }
699 if (fmt.cTabCount < MAX_TAB_STOPS && info->rtfParam < 0x1000000)
700 fmt.rgxTabs[fmt.cTabCount++] = info->rtfParam;
701 fmt.dwMask = PFM_TABSTOPS;
702 break;
703 case rtfKeep:
704 fmt.dwMask = PFM_KEEP;
705 fmt.wEffects = PFE_KEEP;
706 break;
707 case rtfNoWidowControl:
708 fmt.dwMask = PFM_NOWIDOWCONTROL;
709 fmt.wEffects = PFE_NOWIDOWCONTROL;
710 break;
711 case rtfKeepNext:
712 fmt.dwMask = PFM_KEEPNEXT;
713 fmt.wEffects = PFE_KEEPNEXT;
714 break;
715 case rtfSpaceAfter:
716 fmt.dwMask = PFM_SPACEAFTER;
717 fmt.dySpaceAfter = info->rtfParam;
718 break;
719 case rtfSpaceBefore:
720 fmt.dwMask = PFM_SPACEBEFORE;
721 fmt.dySpaceBefore = info->rtfParam;
722 break;
723 case rtfSpaceBetween:
724 fmt.dwMask = PFM_LINESPACING;
725 if ((int)info->rtfParam > 0)
726 {
727 fmt.dyLineSpacing = info->rtfParam;
728 fmt.bLineSpacingRule = 3;
729 }
730 else
731 {
732 fmt.dyLineSpacing = info->rtfParam;
733 fmt.bLineSpacingRule = 4;
734 }
735 break;
736 case rtfSpaceMultiply:
737 fmt.dwMask = PFM_LINESPACING;
738 fmt.dyLineSpacing = info->rtfParam * 20;
739 fmt.bLineSpacingRule = 5;
740 break;
741 case rtfParBullet:
742 fmt.dwMask = PFM_NUMBERING;
743 fmt.wNumbering = PFN_BULLET;
744 break;
745 case rtfParSimple:
746 fmt.dwMask = PFM_NUMBERING;
747 fmt.wNumbering = 2; /* FIXME: MSDN says it's not used ?? */
748 break;
749 case rtfParNumDecimal:
750 fmt.dwMask = PFM_NUMBERING;
751 fmt.wNumbering = 2; /* FIXME: MSDN says it's not used ?? */
752 break;
753 case rtfParNumIndent:
754 fmt.dwMask = PFM_NUMBERINGTAB;
755 fmt.wNumberingTab = info->rtfParam;
756 break;
757 case rtfParNumStartAt:
758 fmt.dwMask = PFM_NUMBERINGSTART;
759 fmt.wNumberingStart = info->rtfParam;
760 break;
761 case rtfBorderLeft:
762 info->borderType = RTFBorderParaLeft;
763 ME_GetSelectionParaFormat(info->editor, &fmt);
764 if (!(fmt.dwMask & PFM_BORDER))
765 {
766 fmt.wBorderSpace = 0;
767 fmt.wBorderWidth = 1;
768 fmt.wBorders = 0;
769 }
770 fmt.wBorders |= 1;
771 fmt.dwMask = PFM_BORDER;
772 break;
773 case rtfBorderRight:
774 info->borderType = RTFBorderParaRight;
775 ME_GetSelectionParaFormat(info->editor, &fmt);
776 if (!(fmt.dwMask & PFM_BORDER))
777 {
778 fmt.wBorderSpace = 0;
779 fmt.wBorderWidth = 1;
780 fmt.wBorders = 0;
781 }
782 fmt.wBorders |= 2;
783 fmt.dwMask = PFM_BORDER;
784 break;
785 case rtfBorderTop:
786 info->borderType = RTFBorderParaTop;
787 ME_GetSelectionParaFormat(info->editor, &fmt);
788 if (!(fmt.dwMask & PFM_BORDER))
789 {
790 fmt.wBorderSpace = 0;
791 fmt.wBorderWidth = 1;
792 fmt.wBorders = 0;
793 }
794 fmt.wBorders |= 4;
795 fmt.dwMask = PFM_BORDER;
796 break;
797 case rtfBorderBottom:
798 info->borderType = RTFBorderParaBottom;
799 ME_GetSelectionParaFormat(info->editor, &fmt);
800 if (!(fmt.dwMask & PFM_BORDER))
801 {
802 fmt.wBorderSpace = 0;
803 fmt.wBorderWidth = 1;
804 fmt.wBorders = 0;
805 }
806 fmt.wBorders |= 8;
807 fmt.dwMask = PFM_BORDER;
808 break;
809 case rtfBorderSingle:
810 ME_GetSelectionParaFormat(info->editor, &fmt);
811 /* we assume that borders have been created before (RTF spec) */
812 fmt.wBorders &= ~0x700;
813 fmt.wBorders |= 1 << 8;
814 fmt.dwMask = PFM_BORDER;
815 break;
816 case rtfBorderThick:
817 ME_GetSelectionParaFormat(info->editor, &fmt);
818 /* we assume that borders have been created before (RTF spec) */
819 fmt.wBorders &= ~0x700;
820 fmt.wBorders |= 2 << 8;
821 fmt.dwMask = PFM_BORDER;
822 break;
823 case rtfBorderShadow:
824 ME_GetSelectionParaFormat(info->editor, &fmt);
825 /* we assume that borders have been created before (RTF spec) */
826 fmt.wBorders &= ~0x700;
827 fmt.wBorders |= 10 << 8;
828 fmt.dwMask = PFM_BORDER;
829 break;
830 case rtfBorderDouble:
831 ME_GetSelectionParaFormat(info->editor, &fmt);
832 /* we assume that borders have been created before (RTF spec) */
833 fmt.wBorders &= ~0x700;
834 fmt.wBorders |= 7 << 8;
835 fmt.dwMask = PFM_BORDER;
836 break;
837 case rtfBorderDot:
838 ME_GetSelectionParaFormat(info->editor, &fmt);
839 /* we assume that borders have been created before (RTF spec) */
840 fmt.wBorders &= ~0x700;
841 fmt.wBorders |= 11 << 8;
842 fmt.dwMask = PFM_BORDER;
843 break;
844 case rtfBorderWidth:
845 {
846 int borderSide = info->borderType & RTFBorderSideMask;
847 RTFTable *tableDef = info->tableDef;
848 ME_GetSelectionParaFormat(info->editor, &fmt);
849 /* we assume that borders have been created before (RTF spec) */
850 fmt.wBorderWidth |= ((info->rtfParam / 15) & 7) << 8;
851 if ((info->borderType & RTFBorderTypeMask) == RTFBorderTypeCell)
852 {
853 RTFBorder *border;
854 if (!tableDef || tableDef->numCellsDefined >= MAX_TABLE_CELLS)
855 break;
856 border = &tableDef->cells[tableDef->numCellsDefined].border[borderSide];
857 border->width = info->rtfParam;
858 break;
859 }
860 fmt.dwMask = PFM_BORDER;
861 break;
862 }
863 case rtfBorderSpace:
864 ME_GetSelectionParaFormat(info->editor, &fmt);
865 /* we assume that borders have been created before (RTF spec) */
866 fmt.wBorderSpace = info->rtfParam;
867 fmt.dwMask = PFM_BORDER;
868 break;
869 case rtfBorderColor:
870 {
871 RTFTable *tableDef = info->tableDef;
872 int borderSide = info->borderType & RTFBorderSideMask;
873 int borderType = info->borderType & RTFBorderTypeMask;
874 switch(borderType)
875 {
876 case RTFBorderTypePara:
877 if (!info->editor->bEmulateVersion10) /* v4.1 */
878 break;
879 /* v1.0 - 3.0 treat paragraph and row borders the same. */
880 case RTFBorderTypeRow:
881 if (tableDef) {
882 tableDef->border[borderSide].color = info->rtfParam;
883 }
884 break;
885 case RTFBorderTypeCell:
886 if (tableDef && tableDef->numCellsDefined < MAX_TABLE_CELLS) {
887 tableDef->cells[tableDef->numCellsDefined].border[borderSide].color = info->rtfParam;
888 }
889 break;
890 }
891 break;
892 }
893 case rtfRTLPar:
894 fmt.dwMask = PFM_RTLPARA;
895 fmt.wEffects = PFE_RTLPARA;
896 break;
897 case rtfLTRPar:
898 fmt.dwMask = PFM_RTLPARA;
899 fmt.wEffects = 0;
900 break;
901 }
902 if (fmt.dwMask) {
903 RTFFlushOutputBuffer(info);
904 /* FIXME too slow ? how come ?*/
905 ME_SetSelectionParaFormat(info->editor, &fmt);
906 }
907 }
908
909 void ME_RTFTblAttrHook(RTF_Info *info)
910 {
911 switch (info->rtfMinor)
912 {
913 case rtfRowDef:
914 {
915 if (!info->editor->bEmulateVersion10) /* v4.1 */
916 info->borderType = 0; /* Not sure */
917 else /* v1.0 - 3.0 */
918 info->borderType = RTFBorderRowTop;
919 if (!info->tableDef) {
920 info->tableDef = ME_MakeTableDef(info->editor);
921 } else {
922 ME_InitTableDef(info->editor, info->tableDef);
923 }
924 break;
925 }
926 case rtfCellPos:
927 {
928 int cellNum;
929 if (!info->tableDef)
930 {
931 info->tableDef = ME_MakeTableDef(info->editor);
932 }
933 cellNum = info->tableDef->numCellsDefined;
934 if (cellNum >= MAX_TABLE_CELLS)
935 break;
936 info->tableDef->cells[cellNum].rightBoundary = info->rtfParam;
937 if (cellNum < MAX_TAB_STOPS) {
938 /* Tab stops were used to store cell positions before v4.1 but v4.1
939 * still seems to set the tabstops without using them. */
940 ME_DisplayItem *para = info->editor->pCursors[0].pPara;
941 PARAFORMAT2 *pFmt = para->member.para.pFmt;
942 pFmt->rgxTabs[cellNum] &= ~0x00FFFFFF;
943 pFmt->rgxTabs[cellNum] |= 0x00FFFFFF & info->rtfParam;
944 }
945 info->tableDef->numCellsDefined++;
946 break;
947 }
948 case rtfRowBordTop:
949 info->borderType = RTFBorderRowTop;
950 break;
951 case rtfRowBordLeft:
952 info->borderType = RTFBorderRowLeft;
953 break;
954 case rtfRowBordBottom:
955 info->borderType = RTFBorderRowBottom;
956 break;
957 case rtfRowBordRight:
958 info->borderType = RTFBorderRowRight;
959 break;
960 case rtfCellBordTop:
961 info->borderType = RTFBorderCellTop;
962 break;
963 case rtfCellBordLeft:
964 info->borderType = RTFBorderCellLeft;
965 break;
966 case rtfCellBordBottom:
967 info->borderType = RTFBorderCellBottom;
968 break;
969 case rtfCellBordRight:
970 info->borderType = RTFBorderCellRight;
971 break;
972 case rtfRowGapH:
973 if (info->tableDef)
974 info->tableDef->gapH = info->rtfParam;
975 break;
976 case rtfRowLeftEdge:
977 if (info->tableDef)
978 info->tableDef->leftEdge = info->rtfParam;
979 break;
980 }
981 }
982
983 void ME_RTFSpecialCharHook(RTF_Info *info)
984 {
985 RTFTable *tableDef = info->tableDef;
986 switch (info->rtfMinor)
987 {
988 case rtfNestCell:
989 if (info->editor->bEmulateVersion10) /* v1.0 - v3.0 */
990 break;
991 /* else fall through since v4.1 treats rtfNestCell and rtfCell the same */
992 case rtfCell:
993 if (!tableDef)
994 break;
995 RTFFlushOutputBuffer(info);
996 if (!info->editor->bEmulateVersion10) { /* v4.1 */
997 if (tableDef->tableRowStart)
998 {
999 if (!info->nestingLevel &&
1000 tableDef->tableRowStart->member.para.nFlags & MEPF_ROWEND)
1001 {
1002 ME_DisplayItem *para = tableDef->tableRowStart;
1003 para = para->member.para.next_para;
1004 para = ME_InsertTableRowStartAtParagraph(info->editor, para);
1005 tableDef->tableRowStart = para;
1006 info->nestingLevel = 1;
1007 }
1008 ME_InsertTableCellFromCursor(info->editor);
1009 }
1010 } else { /* v1.0 - v3.0 */
1011 ME_DisplayItem *para = info->editor->pCursors[0].pPara;
1012 PARAFORMAT2 *pFmt = para->member.para.pFmt;
1013 if (pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE &&
1014 tableDef->numCellsInserted < tableDef->numCellsDefined)
1015 {
1016 WCHAR tab = '\t';
1017 ME_InsertTextFromCursor(info->editor, 0, &tab, 1, info->style);
1018 tableDef->numCellsInserted++;
1019 }
1020 }
1021 break;
1022 case rtfNestRow:
1023 if (info->editor->bEmulateVersion10) /* v1.0 - v3.0 */
1024 break;
1025 /* else fall through since v4.1 treats rtfNestRow and rtfRow the same */
1026 case rtfRow:
1027 {
1028 ME_DisplayItem *para, *cell, *run;
1029 int i;
1030
1031 if (!tableDef)
1032 break;
1033 RTFFlushOutputBuffer(info);
1034 if (!info->editor->bEmulateVersion10) { /* v4.1 */
1035 if (!tableDef->tableRowStart)
1036 break;
1037 if (!info->nestingLevel &&
1038 tableDef->tableRowStart->member.para.nFlags & MEPF_ROWEND)
1039 {
1040 para = tableDef->tableRowStart;
1041 para = para->member.para.next_para;
1042 para = ME_InsertTableRowStartAtParagraph(info->editor, para);
1043 tableDef->tableRowStart = para;
1044 info->nestingLevel++;
1045 }
1046 para = tableDef->tableRowStart;
1047 cell = ME_FindItemFwd(para, diCell);
1048 assert(cell && !cell->member.cell.prev_cell);
1049 if (tableDef->numCellsDefined < 1)
1050 {
1051 /* 2000 twips appears to be the cell size that native richedit uses
1052 * when no cell sizes are specified. */
1053 const int defaultCellSize = 2000;
1054 int nRightBoundary = defaultCellSize;
1055 cell->member.cell.nRightBoundary = nRightBoundary;
1056 while (cell->member.cell.next_cell) {
1057 cell = cell->member.cell.next_cell;
1058 nRightBoundary += defaultCellSize;
1059 cell->member.cell.nRightBoundary = nRightBoundary;
1060 }
1061 para = ME_InsertTableCellFromCursor(info->editor);
1062 cell = para->member.para.pCell;
1063 cell->member.cell.nRightBoundary = nRightBoundary;
1064 } else {
1065 for (i = 0; i < tableDef->numCellsDefined; i++)
1066 {
1067 RTFCell *cellDef = &tableDef->cells[i];
1068 cell->member.cell.nRightBoundary = cellDef->rightBoundary;
1069 ME_ApplyBorderProperties(info, &cell->member.cell.border,
1070 cellDef->border);
1071 cell = cell->member.cell.next_cell;
1072 if (!cell)
1073 {
1074 para = ME_InsertTableCellFromCursor(info->editor);
1075 cell = para->member.para.pCell;
1076 }
1077 }
1078 /* Cell for table row delimiter is empty */
1079 cell->member.cell.nRightBoundary = tableDef->cells[i-1].rightBoundary;
1080 }
1081
1082 run = ME_FindItemFwd(cell, diRun);
1083 if (info->editor->pCursors[0].pRun != run ||
1084 info->editor->pCursors[0].nOffset)
1085 {
1086 int nOfs, nChars;
1087 /* Delete inserted cells that aren't defined. */
1088 info->editor->pCursors[1].pRun = run;
1089 info->editor->pCursors[1].pPara = ME_GetParagraph(run);
1090 info->editor->pCursors[1].nOffset = 0;
1091 nOfs = ME_GetCursorOfs(&info->editor->pCursors[1]);
1092 nChars = ME_GetCursorOfs(&info->editor->pCursors[0]) - nOfs;
1093 ME_InternalDeleteText(info->editor, &info->editor->pCursors[1],
1094 nChars, TRUE);
1095 }
1096
1097 para = ME_InsertTableRowEndFromCursor(info->editor);
1098 para->member.para.pFmt->dxOffset = abs(info->tableDef->gapH);
1099 para->member.para.pFmt->dxStartIndent = info->tableDef->leftEdge;
1100 ME_ApplyBorderProperties(info, &para->member.para.border,
1101 tableDef->border);
1102 info->nestingLevel--;
1103 if (!info->nestingLevel)
1104 {
1105 if (info->canInheritInTbl) {
1106 tableDef->tableRowStart = para;
1107 } else {
1108 while (info->tableDef) {
1109 tableDef = info->tableDef;
1110 info->tableDef = tableDef->parent;
1111 heap_free(tableDef);
1112 }
1113 }
1114 } else {
1115 info->tableDef = tableDef->parent;
1116 heap_free(tableDef);
1117 }
1118 } else { /* v1.0 - v3.0 */
1119 WCHAR endl = '\r';
1120 ME_DisplayItem *para = info->editor->pCursors[0].pPara;
1121 PARAFORMAT2 *pFmt = para->member.para.pFmt;
1122 pFmt->dxOffset = info->tableDef->gapH;
1123 pFmt->dxStartIndent = info->tableDef->leftEdge;
1124
1125 ME_ApplyBorderProperties(info, &para->member.para.border,
1126 tableDef->border);
1127 while (tableDef->numCellsInserted < tableDef->numCellsDefined)
1128 {
1129 WCHAR tab = '\t';
1130 ME_InsertTextFromCursor(info->editor, 0, &tab, 1, info->style);
1131 tableDef->numCellsInserted++;
1132 }
1133 pFmt->cTabCount = min(tableDef->numCellsDefined, MAX_TAB_STOPS);
1134 if (!tableDef->numCellsDefined)
1135 pFmt->wEffects &= ~PFE_TABLE;
1136 ME_InsertTextFromCursor(info->editor, 0, &endl, 1, info->style);
1137 tableDef->numCellsInserted = 0;
1138 }
1139 break;
1140 }
1141 case rtfTab:
1142 case rtfPar:
1143 if (info->editor->bEmulateVersion10) { /* v1.0 - 3.0 */
1144 ME_DisplayItem *para;
1145 PARAFORMAT2 *pFmt;
1146 RTFFlushOutputBuffer(info);
1147 para = info->editor->pCursors[0].pPara;
1148 pFmt = para->member.para.pFmt;
1149 if (pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE)
1150 {
1151 /* rtfPar is treated like a space within a table. */
1152 info->rtfClass = rtfText;
1153 info->rtfMajor = ' ';
1154 }
1155 else if (info->rtfMinor == rtfPar && tableDef)
1156 tableDef->numCellsInserted = 0;
1157 }
1158 break;
1159 }
1160 }
1161
1162 static BOOL ME_RTFInsertOleObject(RTF_Info *info, HENHMETAFILE hemf, HBITMAP hbmp,
1163 const SIZEL* sz)
1164 {
1165 LPOLEOBJECT lpObject = NULL;
1166 LPSTORAGE lpStorage = NULL;
1167 LPOLECLIENTSITE lpClientSite = NULL;
1168 LPDATAOBJECT lpDataObject = NULL;
1169 LPOLECACHE lpOleCache = NULL;
1170 STGMEDIUM stgm;
1171 FORMATETC fm;
1172 CLSID clsid;
1173 BOOL ret = FALSE;
1174 DWORD conn;
1175
1176 if (hemf)
1177 {
1178 stgm.tymed = TYMED_ENHMF;
1179 stgm.u.hEnhMetaFile = hemf;
1180 fm.cfFormat = CF_ENHMETAFILE;
1181 }
1182 else if (hbmp)
1183 {
1184 stgm.tymed = TYMED_GDI;
1185 stgm.u.hBitmap = hbmp;
1186 fm.cfFormat = CF_BITMAP;
1187 }
1188 stgm.pUnkForRelease = NULL;
1189
1190 fm.ptd = NULL;
1191 fm.dwAspect = DVASPECT_CONTENT;
1192 fm.lindex = -1;
1193 fm.tymed = stgm.tymed;
1194
1195 if (!info->lpRichEditOle)
1196 {
1197 CreateIRichEditOle(NULL, info->editor, (VOID**)&info->lpRichEditOle);
1198 }
1199
1200 if (OleCreateDefaultHandler(&CLSID_NULL, NULL, &IID_IOleObject, (void**)&lpObject) == S_OK &&
1201 IRichEditOle_GetClientSite(info->lpRichEditOle, &lpClientSite) == S_OK &&
1202 IOleObject_SetClientSite(lpObject, lpClientSite) == S_OK &&
1203 IOleObject_GetUserClassID(lpObject, &clsid) == S_OK &&
1204 IOleObject_QueryInterface(lpObject, &IID_IOleCache, (void**)&lpOleCache) == S_OK &&
1205 IOleCache_Cache(lpOleCache, &fm, 0, &conn) == S_OK &&
1206 IOleObject_QueryInterface(lpObject, &IID_IDataObject, (void**)&lpDataObject) == S_OK &&
1207 IDataObject_SetData(lpDataObject, &fm, &stgm, TRUE) == S_OK)
1208 {
1209 REOBJECT reobject;
1210
1211 reobject.cbStruct = sizeof(reobject);
1212 reobject.cp = REO_CP_SELECTION;
1213 reobject.clsid = clsid;
1214 reobject.poleobj = lpObject;
1215 reobject.pstg = lpStorage;
1216 reobject.polesite = lpClientSite;
1217 /* convert from twips to .01 mm */
1218 reobject.sizel.cx = MulDiv(sz->cx, 254, 144);
1219 reobject.sizel.cy = MulDiv(sz->cy, 254, 144);
1220 reobject.dvaspect = DVASPECT_CONTENT;
1221 reobject.dwFlags = 0; /* FIXME */
1222 reobject.dwUser = 0;
1223
1224 ME_InsertOLEFromCursor(info->editor, &reobject, 0);
1225 ret = TRUE;
1226 }
1227
1228 if (lpObject) IOleObject_Release(lpObject);
1229 if (lpClientSite) IOleClientSite_Release(lpClientSite);
1230 if (lpStorage) IStorage_Release(lpStorage);
1231 if (lpDataObject) IDataObject_Release(lpDataObject);
1232 if (lpOleCache) IOleCache_Release(lpOleCache);
1233
1234 return ret;
1235 }
1236
1237 static void ME_RTFReadShpPictGroup( RTF_Info *info )
1238 {
1239 int level = 1;
1240
1241 for (;;)
1242 {
1243 RTFGetToken (info);
1244
1245 if (info->rtfClass == rtfEOF) return;
1246 if (RTFCheckCM( info, rtfGroup, rtfEndGroup ))
1247 {
1248 if (--level == 0) break;
1249 }
1250 else if (RTFCheckCM( info, rtfGroup, rtfBeginGroup ))
1251 {
1252 level++;
1253 }
1254 else
1255 {
1256 RTFRouteToken( info );
1257 if (RTFCheckCM( info, rtfGroup, rtfEndGroup ))
1258 level--;
1259 }
1260 }
1261
1262 RTFRouteToken( info ); /* feed "}" back to router */
1263 return;
1264 }
1265
1266 static DWORD read_hex_data( RTF_Info *info, BYTE **out )
1267 {
1268 DWORD read = 0, size = 1024;
1269 BYTE *buf, val;
1270 BOOL flip;
1271
1272 *out = NULL;
1273
1274 if (info->rtfClass != rtfText)
1275 {
1276 ERR("Called with incorrect token\n");
1277 return 0;
1278 }
1279
1280 buf = HeapAlloc( GetProcessHeap(), 0, size );
1281 if (!buf) return 0;
1282
1283 val = info->rtfMajor;
1284 for (flip = TRUE;; flip = !flip)
1285 {
1286 RTFGetToken( info );
1287 if (info->rtfClass == rtfEOF)
1288 {
1289 HeapFree( GetProcessHeap(), 0, buf );
1290 return 0;
1291 }
1292 if (info->rtfClass != rtfText) break;
1293 if (flip)
1294 {
1295 if (read >= size)
1296 {
1297 size *= 2;
1298 buf = HeapReAlloc( GetProcessHeap(), 0, buf, size );
1299 if (!buf) return 0;
1300 }
1301 buf[read++] = RTFCharToHex(val) * 16 + RTFCharToHex(info->rtfMajor);
1302 }
1303 else
1304 val = info->rtfMajor;
1305 }
1306 if (flip) FIXME("wrong hex string\n");
1307
1308 *out = buf;
1309 return read;
1310 }
1311
1312 static void ME_RTFReadPictGroup(RTF_Info *info)
1313 {
1314 SIZEL sz;
1315 BYTE *buffer = NULL;
1316 DWORD size = 0;
1317 METAFILEPICT mfp;
1318 HENHMETAFILE hemf;
1319 HBITMAP hbmp;
1320 enum gfxkind {gfx_unknown = 0, gfx_enhmetafile, gfx_metafile, gfx_dib} gfx = gfx_unknown;
1321 int level = 1;
1322
1323 mfp.mm = MM_TEXT;
1324 sz.cx = sz.cy = 0;
1325
1326 for (;;)
1327 {
1328 RTFGetToken( info );
1329
1330 if (info->rtfClass == rtfText)
1331 {
1332 if (level == 1)
1333 {
1334 if (!buffer)
1335 size = read_hex_data( info, &buffer );
1336 }
1337 else
1338 {
1339 RTFSkipGroup( info );
1340 }
1341 } /* We potentially have a new token so fall through. */
1342
1343 if (info->rtfClass == rtfEOF) return;
1344
1345 if (RTFCheckCM( info, rtfGroup, rtfEndGroup ))
1346 {
1347 if (--level == 0) break;
1348 continue;
1349 }
1350 if (RTFCheckCM( info, rtfGroup, rtfBeginGroup ))
1351 {
1352 level++;
1353 continue;
1354 }
1355 if (!RTFCheckCM( info, rtfControl, rtfPictAttr ))
1356 {
1357 RTFRouteToken( info );
1358 if (RTFCheckCM( info, rtfGroup, rtfEndGroup ))
1359 level--;
1360 continue;
1361 }
1362
1363 if (RTFCheckMM( info, rtfPictAttr, rtfWinMetafile ))
1364 {
1365 mfp.mm = info->rtfParam;
1366 gfx = gfx_metafile;
1367 }
1368 else if (RTFCheckMM( info, rtfPictAttr, rtfDevIndBitmap ))
1369 {
1370 if (info->rtfParam != 0) FIXME("dibitmap should be 0 (%d)\n", info->rtfParam);
1371 gfx = gfx_dib;
1372 }
1373 else if (RTFCheckMM( info, rtfPictAttr, rtfEmfBlip ))
1374 gfx = gfx_enhmetafile;
1375 else if (RTFCheckMM( info, rtfPictAttr, rtfPicWid ))
1376 mfp.xExt = info->rtfParam;
1377 else if (RTFCheckMM( info, rtfPictAttr, rtfPicHt ))
1378 mfp.yExt = info->rtfParam;
1379 else if (RTFCheckMM( info, rtfPictAttr, rtfPicGoalWid ))
1380 sz.cx = info->rtfParam;
1381 else if (RTFCheckMM( info, rtfPictAttr, rtfPicGoalHt ))
1382 sz.cy = info->rtfParam;
1383 else
1384 FIXME("Non supported attribute: %d %d %d\n", info->rtfClass, info->rtfMajor, info->rtfMinor);
1385 }
1386
1387 if (buffer)
1388 {
1389 switch (gfx)
1390 {
1391 case gfx_enhmetafile:
1392 if ((hemf = SetEnhMetaFileBits( size, buffer )))
1393 ME_RTFInsertOleObject( info, hemf, NULL, &sz );
1394 break;
1395 case gfx_metafile:
1396 if ((hemf = SetWinMetaFileBits( size, buffer, NULL, &mfp )))
1397 ME_RTFInsertOleObject( info, hemf, NULL, &sz );
1398 break;
1399 case gfx_dib:
1400 {
1401 BITMAPINFO *bi = (BITMAPINFO*)buffer;
1402 HDC hdc = GetDC(0);
1403 unsigned nc = bi->bmiHeader.biClrUsed;
1404
1405 /* not quite right, especially for bitfields type of compression */
1406 if (!nc && bi->bmiHeader.biBitCount <= 8)
1407 nc = 1 << bi->bmiHeader.biBitCount;
1408 if ((hbmp = CreateDIBitmap( hdc, &bi->bmiHeader,
1409 CBM_INIT, (char*)(bi + 1) + nc * sizeof(RGBQUAD),
1410 bi, DIB_RGB_COLORS)) )
1411 ME_RTFInsertOleObject( info, NULL, hbmp, &sz );
1412 ReleaseDC( 0, hdc );
1413 break;
1414 }
1415 default:
1416 break;
1417 }
1418 }
1419 HeapFree( GetProcessHeap(), 0, buffer );
1420 RTFRouteToken( info ); /* feed "}" back to router */
1421 return;
1422 }
1423
1424 /* for now, lookup the \result part and use it, whatever the object */
1425 static void ME_RTFReadObjectGroup(RTF_Info *info)
1426 {
1427 for (;;)
1428 {
1429 RTFGetToken (info);
1430 if (info->rtfClass == rtfEOF)
1431 return;
1432 if (RTFCheckCM(info, rtfGroup, rtfEndGroup))
1433 break;
1434 if (RTFCheckCM(info, rtfGroup, rtfBeginGroup))
1435 {
1436 RTFGetToken (info);
1437 if (info->rtfClass == rtfEOF)
1438 return;
1439 if (RTFCheckCMM(info, rtfControl, rtfDestination, rtfObjResult))
1440 {
1441 int level = 1;
1442
1443 while (RTFGetToken (info) != rtfEOF)
1444 {
1445 if (info->rtfClass == rtfGroup)
1446 {
1447 if (info->rtfMajor == rtfBeginGroup) level++;
1448 else if (info->rtfMajor == rtfEndGroup && --level < 0) break;
1449 }
1450 RTFRouteToken(info);
1451 }
1452 }
1453 else RTFSkipGroup(info);
1454 continue;
1455 }
1456 if (!RTFCheckCM (info, rtfControl, rtfObjAttr))
1457 {
1458 FIXME("Non supported attribute: %d %d %d\n", info->rtfClass, info->rtfMajor, info->rtfMinor);
1459 return;
1460 }
1461 }
1462 RTFRouteToken(info); /* feed "}" back to router */
1463 }
1464
1465 static void ME_RTFReadHook(RTF_Info *info)
1466 {
1467 switch(info->rtfClass)
1468 {
1469 case rtfGroup:
1470 switch(info->rtfMajor)
1471 {
1472 case rtfBeginGroup:
1473 if (info->stackTop < maxStack) {
1474 info->stack[info->stackTop].style = info->style;
1475 ME_AddRefStyle(info->style);
1476 info->stack[info->stackTop].codePage = info->codePage;
1477 info->stack[info->stackTop].unicodeLength = info->unicodeLength;
1478 }
1479 info->stackTop++;
1480 info->styleChanged = FALSE;
1481 break;
1482 case rtfEndGroup:
1483 {
1484 RTFFlushOutputBuffer(info);
1485 info->stackTop--;
1486 if (info->stackTop <= 0)
1487 info->rtfClass = rtfEOF;
1488 if (info->stackTop < 0)
1489 return;
1490
1491 ME_ReleaseStyle(info->style);
1492 info->style = info->stack[info->stackTop].style;
1493 info->codePage = info->stack[info->stackTop].codePage;
1494 info->unicodeLength = info->stack[info->stackTop].unicodeLength;
1495 break;
1496 }
1497 }
1498 break;
1499 }
1500 }
1501
1502 void
1503 ME_StreamInFill(ME_InStream *stream)
1504 {
1505 stream->editstream->dwError = stream->editstream->pfnCallback(stream->editstream->dwCookie,
1506 (BYTE *)stream->buffer,
1507 sizeof(stream->buffer),
1508 (LONG *)&stream->dwSize);
1509 stream->dwUsed = 0;
1510 }
1511
1512 static LRESULT ME_StreamIn(ME_TextEditor *editor, DWORD format, EDITSTREAM *stream, BOOL stripLastCR)
1513 {
1514 RTF_Info parser;
1515 ME_Style *style;
1516 int from, to, nUndoMode;
1517 int nEventMask = editor->nEventMask;
1518 ME_InStream inStream;
1519 BOOL invalidRTF = FALSE;
1520 ME_Cursor *selStart, *selEnd;
1521 LRESULT num_read = 0; /* bytes read for SF_TEXT, non-control chars inserted for SF_RTF */
1522
1523 TRACE("stream==%p editor==%p format==0x%X\n", stream, editor, format);
1524 editor->nEventMask = 0;
1525
1526 ME_GetSelectionOfs(editor, &from, &to);
1527 if (format & SFF_SELECTION && editor->mode & TM_RICHTEXT)
1528 {
1529 ME_GetSelection(editor, &selStart, &selEnd);
1530 style = ME_GetSelectionInsertStyle(editor);
1531
1532 ME_InternalDeleteText(editor, selStart, to - from, FALSE);
1533
1534 /* Don't insert text at the end of the table row */
1535 if (!editor->bEmulateVersion10) { /* v4.1 */
1536 ME_DisplayItem *para = editor->pCursors->pPara;
1537 if (para->member.para.nFlags & MEPF_ROWEND)
1538 {
1539 para = para->member.para.next_para;
1540 editor->pCursors[0].pPara = para;
1541 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
1542 editor->pCursors[0].nOffset = 0;
1543 }
1544 if (para->member.para.nFlags & MEPF_ROWSTART)
1545 {
1546 para = para->member.para.next_para;
1547 editor->pCursors[0].pPara = para;
1548 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
1549 editor->pCursors[0].nOffset = 0;
1550 }
1551 editor->pCursors[1] = editor->pCursors[0];
1552 } else { /* v1.0 - 3.0 */
1553 if (editor->pCursors[0].pRun->member.run.nFlags & MERF_ENDPARA &&
1554 ME_IsInTable(editor->pCursors[0].pRun))
1555 return 0;
1556 }
1557 } else {
1558 style = editor->pBuffer->pDefaultStyle;
1559 ME_AddRefStyle(style);
1560 ME_SetSelection(editor, 0, 0);
1561 ME_InternalDeleteText(editor, &editor->pCursors[1],
1562 ME_GetTextLength(editor), FALSE);
1563 from = to = 0;
1564 ME_ClearTempStyle(editor);
1565 ME_SetDefaultParaFormat(editor, editor->pCursors[0].pPara->member.para.pFmt);
1566 }
1567
1568
1569 /* Back up undo mode to a local variable */
1570 nUndoMode = editor->nUndoMode;
1571
1572 /* Only create an undo if SFF_SELECTION is set */
1573 if (!(format & SFF_SELECTION))
1574 editor->nUndoMode = umIgnore;
1575
1576 inStream.editstream = stream;
1577 inStream.editstream->dwError = 0;
1578 inStream.dwSize = 0;
1579 inStream.dwUsed = 0;
1580
1581 if (format & SF_RTF)
1582 {
1583 /* Check if it's really RTF, and if it is not, use plain text */
1584 ME_StreamInFill(&inStream);
1585 if (!inStream.editstream->dwError)
1586 {
1587 if ((!editor->bEmulateVersion10 && strncmp(inStream.buffer, "{\\rtf", 5) && strncmp(inStream.buffer, "{\\urtf", 6))
1588 || (editor->bEmulateVersion10 && *inStream.buffer != '{'))
1589 {
1590 invalidRTF = TRUE;
1591 inStream.editstream->dwError = -16;
1592 }
1593 }
1594 }
1595
1596 if (!invalidRTF && !inStream.editstream->dwError)
1597 {
1598 if (format & SF_RTF) {
1599 from = ME_GetCursorOfs(&editor->pCursors[0]);
1600
1601 /* setup the RTF parser */
1602 memset(&parser, 0, sizeof parser);
1603 RTFSetEditStream(&parser, &inStream);
1604 parser.rtfFormat = format&(SF_TEXT|SF_RTF);
1605 parser.editor = editor;
1606 parser.style = style;
1607 WriterInit(&parser);
1608 RTFInit(&parser);
1609 RTFSetReadHook(&parser, ME_RTFReadHook);
1610 RTFSetDestinationCallback(&parser, rtfShpPict, ME_RTFReadShpPictGroup);
1611 RTFSetDestinationCallback(&parser, rtfPict, ME_RTFReadPictGroup);
1612 RTFSetDestinationCallback(&parser, rtfObject, ME_RTFReadObjectGroup);
1613 if (!parser.editor->bEmulateVersion10) /* v4.1 */
1614 {
1615 RTFSetDestinationCallback(&parser, rtfNoNestTables, RTFSkipGroup);
1616 RTFSetDestinationCallback(&parser, rtfNestTableProps, RTFReadGroup);
1617 }
1618 BeginFile(&parser);
1619
1620 /* do the parsing */
1621 RTFRead(&parser);
1622 RTFFlushOutputBuffer(&parser);
1623 if (!editor->bEmulateVersion10) { /* v4.1 */
1624 if (parser.tableDef && parser.tableDef->tableRowStart &&
1625 (parser.nestingLevel > 0 || parser.canInheritInTbl))
1626 {
1627 /* Delete any incomplete table row at the end of the rich text. */
1628 int nOfs, nChars;
1629 ME_DisplayItem *para;
1630
1631 parser.rtfMinor = rtfRow;
1632 /* Complete the table row before deleting it.
1633 * By doing it this way we will have the current paragraph format set
1634 * properly to reflect that is not in the complete table, and undo items
1635 * will be added for this change to the current paragraph format. */
1636 if (parser.nestingLevel > 0)
1637 {
1638 while (parser.nestingLevel > 1)
1639 ME_RTFSpecialCharHook(&parser); /* Decrements nestingLevel */
1640 para = parser.tableDef->tableRowStart;
1641 ME_RTFSpecialCharHook(&parser);
1642 } else {
1643 para = parser.tableDef->tableRowStart;
1644 ME_RTFSpecialCharHook(&parser);
1645 assert(para->member.para.nFlags & MEPF_ROWEND);
1646 para = para->member.para.next_para;
1647 }
1648
1649 editor->pCursors[1].pPara = para;
1650 editor->pCursors[1].pRun = ME_FindItemFwd(para, diRun);
1651 editor->pCursors[1].nOffset = 0;
1652 nOfs = ME_GetCursorOfs(&editor->pCursors[1]);
1653 nChars = ME_GetCursorOfs(&editor->pCursors[0]) - nOfs;
1654 ME_InternalDeleteText(editor, &editor->pCursors[1], nChars, TRUE);
1655 if (parser.tableDef)
1656 parser.tableDef->tableRowStart = NULL;
1657 }
1658 }
1659 ME_CheckTablesForCorruption(editor);
1660 RTFDestroy(&parser);
1661 if (parser.lpRichEditOle)
1662 IRichEditOle_Release(parser.lpRichEditOle);
1663
1664 if (parser.stackTop > 0)
1665 {
1666 while (--parser.stackTop >= 0)
1667 {
1668 ME_ReleaseStyle(parser.style);
1669 parser.style = parser.stack[parser.stackTop].style;
1670 }
1671 if (!inStream.editstream->dwError)
1672 inStream.editstream->dwError = HRESULT_FROM_WIN32(ERROR_HANDLE_EOF);
1673 }
1674
1675 /* Remove last line break, as mandated by tests. This is not affected by
1676 CR/LF counters, since RTF streaming presents only \para tokens, which
1677 are converted according to the standard rules: \r for 2.0, \r\n for 1.0
1678 */
1679 if (stripLastCR && !(format & SFF_SELECTION)) {
1680 int newto;
1681 ME_GetSelection(editor, &selStart, &selEnd);
1682 newto = ME_GetCursorOfs(selEnd);
1683 if (newto > to + (editor->bEmulateVersion10 ? 1 : 0)) {
1684 WCHAR lastchar[3] = {'\0', '\0'};
1685 int linebreakSize = editor->bEmulateVersion10 ? 2 : 1;
1686 ME_Cursor linebreakCursor = *selEnd;
1687
1688 ME_MoveCursorChars(editor, &linebreakCursor, -linebreakSize);
1689 ME_GetTextW(editor, lastchar, 2, &linebreakCursor, linebreakSize, FALSE, FALSE);
1690 if (lastchar[0] == '\r' && (lastchar[1] == '\n' || lastchar[1] == '\0')) {
1691 ME_InternalDeleteText(editor, &linebreakCursor, linebreakSize, FALSE);
1692 }
1693 }
1694 }
1695 to = ME_GetCursorOfs(&editor->pCursors[0]);
1696 num_read = to - from;
1697
1698 style = parser.style;
1699 }
1700 else if (format & SF_TEXT)
1701 num_read = ME_StreamInText(editor, format, &inStream, style);
1702 else
1703 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
1704 /* put the cursor at the top */
1705 if (!(format & SFF_SELECTION))
1706 ME_SetSelection(editor, 0, 0);
1707 }
1708
1709 /* Restore saved undo mode */
1710 editor->nUndoMode = nUndoMode;
1711
1712 /* even if we didn't add an undo, we need to commit anything on the stack */
1713 ME_CommitUndo(editor);
1714
1715 /* If SFF_SELECTION isn't set, delete any undos from before we started too */
1716 if (!(format & SFF_SELECTION))
1717 ME_EmptyUndoStack(editor);
1718
1719 ME_ReleaseStyle(style);
1720 editor->nEventMask = nEventMask;
1721 ME_UpdateRepaint(editor, FALSE);
1722 if (!(format & SFF_SELECTION)) {
1723 ME_ClearTempStyle(editor);
1724 }
1725 ITextHost_TxShowCaret(editor->texthost, FALSE);
1726 ME_MoveCaret(editor);
1727 ITextHost_TxShowCaret(editor->texthost, TRUE);
1728 ME_SendSelChange(editor);
1729 ME_SendRequestResize(editor, FALSE);
1730
1731 return num_read;
1732 }
1733
1734
1735 typedef struct tagME_RTFStringStreamStruct
1736 {
1737 char *string;
1738 int pos;
1739 int length;
1740 } ME_RTFStringStreamStruct;
1741
1742 static DWORD CALLBACK ME_ReadFromRTFString(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1743 {
1744 ME_RTFStringStreamStruct *pStruct = (ME_RTFStringStreamStruct *)dwCookie;
1745 int count;
1746
1747 count = min(cb, pStruct->length - pStruct->pos);
1748 memmove(lpBuff, pStruct->string + pStruct->pos, count);
1749 pStruct->pos += count;
1750 *pcb = count;
1751 return 0;
1752 }
1753
1754 static void
1755 ME_StreamInRTFString(ME_TextEditor *editor, BOOL selection, char *string)
1756 {
1757 EDITSTREAM es;
1758 ME_RTFStringStreamStruct data;
1759
1760 data.string = string;
1761 data.length = strlen(string);
1762 data.pos = 0;
1763 es.dwCookie = (DWORD_PTR)&data;
1764 es.pfnCallback = ME_ReadFromRTFString;
1765 ME_StreamIn(editor, SF_RTF | (selection ? SFF_SELECTION : 0), &es, TRUE);
1766 }
1767
1768
1769 static int
1770 ME_FindText(ME_TextEditor *editor, DWORD flags, const CHARRANGE *chrg, const WCHAR *text, CHARRANGE *chrgText)
1771 {
1772 const int nLen = lstrlenW(text);
1773 const int nTextLen = ME_GetTextLength(editor);
1774 int nMin, nMax;
1775 ME_Cursor cursor;
1776 WCHAR wLastChar = ' ';
1777
1778 TRACE("flags==0x%08x, chrg->cpMin==%d, chrg->cpMax==%d text==%s\n",
1779 flags, chrg->cpMin, chrg->cpMax, debugstr_w(text));
1780
1781 if (flags & ~(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD))
1782 FIXME("Flags 0x%08x not implemented\n",
1783 flags & ~(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD));
1784
1785 nMin = chrg->cpMin;
1786 if (chrg->cpMax == -1)
1787 nMax = nTextLen;
1788 else
1789 nMax = chrg->cpMax > nTextLen ? nTextLen : chrg->cpMax;
1790
1791 /* In 1.0 emulation, if cpMax reaches end of text, add the FR_DOWN flag */
1792 if (editor->bEmulateVersion10 && nMax == nTextLen)
1793 {
1794 flags |= FR_DOWN;
1795 }
1796
1797 /* In 1.0 emulation, cpMin must always be no greater than cpMax */
1798 if (editor->bEmulateVersion10 && nMax < nMin)
1799 {
1800 if (chrgText)
1801 {
1802 chrgText->cpMin = -1;
1803 chrgText->cpMax = -1;
1804 }
1805 return -1;
1806 }
1807
1808 /* when searching up, if cpMin < cpMax, then instead of searching
1809 * on [cpMin,cpMax], we search on [0,cpMin], otherwise, search on
1810 * [cpMax, cpMin]. The exception is when cpMax is -1, in which
1811 * case, it is always bigger than cpMin.
1812 */
1813 if (!editor->bEmulateVersion10 && !(flags & FR_DOWN))
1814 {
1815 int nSwap = nMax;
1816
1817 nMax = nMin > nTextLen ? nTextLen : nMin;
1818 if (nMin < nSwap || chrg->cpMax == -1)
1819 nMin = 0;
1820 else
1821 nMin = nSwap;
1822 }
1823
1824 if (!nLen || nMin < 0 || nMax < 0 || nMax < nMin)
1825 {
1826 if (chrgText)
1827 chrgText->cpMin = chrgText->cpMax = -1;
1828 return -1;
1829 }
1830
1831 if (flags & FR_DOWN) /* Forward search */
1832 {
1833 /* If possible, find the character before where the search starts */
1834 if ((flags & FR_WHOLEWORD) && nMin)
1835 {
1836 ME_CursorFromCharOfs(editor, nMin - 1, &cursor);
1837 wLastChar = *get_text( &cursor.pRun->member.run, cursor.nOffset );
1838 ME_MoveCursorChars(editor, &cursor, 1);
1839 } else {
1840 ME_CursorFromCharOfs(editor, nMin, &cursor);
1841 }
1842
1843 while (cursor.pRun && ME_GetCursorOfs(&cursor) + nLen <= nMax)
1844 {
1845 ME_DisplayItem *pCurItem = cursor.pRun;
1846 int nCurStart = cursor.nOffset;
1847 int nMatched = 0;
1848
1849 while (pCurItem && ME_CharCompare( *get_text( &pCurItem->member.run, nCurStart + nMatched ), text[nMatched], (flags & FR_MATCHCASE)))
1850 {
1851 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
1852 break;
1853
1854 nMatched++;
1855 if (nMatched == nLen)
1856 {
1857 ME_DisplayItem *pNextItem = pCurItem;
1858 int nNextStart = nCurStart;
1859 WCHAR wNextChar;
1860
1861 /* Check to see if next character is a whitespace */
1862 if (flags & FR_WHOLEWORD)
1863 {
1864 if (nCurStart + nMatched == pCurItem->member.run.len)
1865 {
1866 pNextItem = ME_FindItemFwd(pCurItem, diRun);
1867 nNextStart = -nMatched;
1868 }
1869
1870 if (pNextItem)
1871 wNextChar = *get_text( &pNextItem->member.run, nNextStart + nMatched );
1872 else
1873 wNextChar = ' ';
1874
1875 if (isalnumW(wNextChar))
1876 break;
1877 }
1878
1879 cursor.nOffset += cursor.pPara->member.para.nCharOfs + cursor.pRun->member.run.nCharOfs;
1880 if (chrgText)
1881 {
1882 chrgText->cpMin = cursor.nOffset;
1883 chrgText->cpMax = cursor.nOffset + nLen;
1884 }
1885 TRACE("found at %d-%d\n", cursor.nOffset, cursor.nOffset + nLen);
1886 return cursor.nOffset;
1887 }
1888 if (nCurStart + nMatched == pCurItem->member.run.len)
1889 {
1890 pCurItem = ME_FindItemFwd(pCurItem, diRun);
1891 nCurStart = -nMatched;
1892 }
1893 }
1894 if (pCurItem)
1895 wLastChar = *get_text( &pCurItem->member.run, nCurStart + nMatched );
1896 else
1897 wLastChar = ' ';
1898
1899 cursor.nOffset++;
1900 if (cursor.nOffset == cursor.pRun->member.run.len)
1901 {
1902 ME_NextRun(&cursor.pPara, &cursor.pRun);
1903 cursor.nOffset = 0;
1904 }
1905 }
1906 }
1907 else /* Backward search */
1908 {
1909 /* If possible, find the character after where the search ends */
1910 if ((flags & FR_WHOLEWORD) && nMax < nTextLen - 1)
1911 {
1912 ME_CursorFromCharOfs(editor, nMax + 1, &cursor);
1913 wLastChar = *get_text( &cursor.pRun->member.run, cursor.nOffset );
1914 ME_MoveCursorChars(editor, &cursor, -1);
1915 } else {
1916 ME_CursorFromCharOfs(editor, nMax, &cursor);
1917 }
1918
1919 while (cursor.pRun && ME_GetCursorOfs(&cursor) - nLen >= nMin)
1920 {
1921 ME_DisplayItem *pCurItem = cursor.pRun;
1922 ME_DisplayItem *pCurPara = cursor.pPara;
1923 int nCurEnd = cursor.nOffset;
1924 int nMatched = 0;
1925
1926 if (nCurEnd == 0)
1927 {
1928 ME_PrevRun(&pCurPara, &pCurItem);
1929 nCurEnd = pCurItem->member.run.len;
1930 }
1931
1932 while (pCurItem && ME_CharCompare( *get_text( &pCurItem->member.run, nCurEnd - nMatched - 1 ),
1933 text[nLen - nMatched - 1], (flags & FR_MATCHCASE) ))
1934 {
1935 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
1936 break;
1937
1938 nMatched++;
1939 if (nMatched == nLen)
1940 {
1941 ME_DisplayItem *pPrevItem = pCurItem;
1942 int nPrevEnd = nCurEnd;
1943 WCHAR wPrevChar;
1944 int nStart;
1945
1946 /* Check to see if previous character is a whitespace */
1947 if (flags & FR_WHOLEWORD)
1948 {
1949 if (nPrevEnd - nMatched == 0)
1950 {
1951 pPrevItem = ME_FindItemBack(pCurItem, diRun);
1952 if (pPrevItem)
1953 nPrevEnd = pPrevItem->member.run.len + nMatched;
1954 }
1955
1956 if (pPrevItem)
1957 wPrevChar = *get_text( &pPrevItem->member.run, nPrevEnd - nMatched - 1 );
1958 else
1959 wPrevChar = ' ';
1960
1961 if (isalnumW(wPrevChar))
1962 break;
1963 }
1964
1965 nStart = pCurPara->member.para.nCharOfs
1966 + pCurItem->member.run.nCharOfs + nCurEnd - nMatched;
1967 if (chrgText)
1968 {
1969 chrgText->cpMin = nStart;
1970 chrgText->cpMax = nStart + nLen;
1971 }
1972 TRACE("found at %d-%d\n", nStart, nStart + nLen);
1973 return nStart;
1974 }
1975 if (nCurEnd - nMatched == 0)
1976 {
1977 ME_PrevRun(&pCurPara, &pCurItem);
1978 /* Don't care about pCurItem becoming NULL here; it's already taken
1979 * care of in the exterior loop condition */
1980 nCurEnd = pCurItem->member.run.len + nMatched;
1981 }
1982 }
1983 if (pCurItem)
1984 wLastChar = *get_text( &pCurItem->member.run, nCurEnd - nMatched - 1 );
1985 else
1986 wLastChar = ' ';
1987
1988 cursor.nOffset--;
1989 if (cursor.nOffset < 0)
1990 {
1991 ME_PrevRun(&cursor.pPara, &cursor.pRun);
1992 cursor.nOffset = cursor.pRun->member.run.len;
1993 }
1994 }
1995 }
1996 TRACE("not found\n");
1997 if (chrgText)
1998 chrgText->cpMin = chrgText->cpMax = -1;
1999 return -1;
2000 }
2001
2002 static int ME_GetTextEx(ME_TextEditor *editor, GETTEXTEX *ex, LPARAM pText)
2003 {
2004 int nChars;
2005 ME_Cursor start;
2006
2007 if (!ex->cb || !pText) return 0;
2008
2009 if (ex->flags & ~(GT_SELECTION | GT_USECRLF))
2010 FIXME("GETTEXTEX flags 0x%08x not supported\n", ex->flags & ~(GT_SELECTION | GT_USECRLF));
2011
2012 if (ex->flags & GT_SELECTION)
2013 {
2014 int from, to;
2015 int nStartCur = ME_GetSelectionOfs(editor, &from, &to);
2016 start = editor->pCursors[nStartCur];
2017 nChars = to - from;
2018 }
2019 else
2020 {
2021 ME_SetCursorToStart(editor, &start);
2022 nChars = INT_MAX;
2023 }
2024 if (ex->codepage == CP_UNICODE)
2025 {
2026 return ME_GetTextW(editor, (LPWSTR)pText, ex->cb / sizeof(WCHAR) - 1,
2027 &start, nChars, ex->flags & GT_USECRLF, FALSE);
2028 }
2029 else
2030 {
2031 /* potentially each char may be a CR, why calculate the exact value with O(N) when
2032 we can just take a bigger buffer? :)
2033 The above assumption still holds with CR/LF counters, since CR->CRLF expansion
2034 occurs only in richedit 2.0 mode, in which line breaks have only one CR
2035 */
2036 int crlfmul = (ex->flags & GT_USECRLF) ? 2 : 1;
2037 DWORD buflen;
2038 LPWSTR buffer;
2039 LRESULT rc;
2040
2041 buflen = min(crlfmul * nChars, ex->cb - 1);
2042 buffer = heap_alloc((buflen + 1) * sizeof(WCHAR));
2043
2044 nChars = ME_GetTextW(editor, buffer, buflen, &start, nChars, ex->flags & GT_USECRLF, FALSE);
2045 rc = WideCharToMultiByte(ex->codepage, 0, buffer, nChars + 1,
2046 (LPSTR)pText, ex->cb, ex->lpDefaultChar, ex->lpUsedDefChar);
2047 if (rc) rc--; /* do not count 0 terminator */
2048
2049 heap_free(buffer);
2050 return rc;
2051 }
2052 }
2053
2054 static int ME_GetTextRange(ME_TextEditor *editor, WCHAR *strText,
2055 const ME_Cursor *start, int nLen, BOOL unicode)
2056 {
2057 if (!strText) return 0;
2058 if (unicode) {
2059 return ME_GetTextW(editor, strText, INT_MAX, start, nLen, FALSE, FALSE);
2060 } else {
2061 int nChars;
2062 WCHAR *p = ALLOC_N_OBJ(WCHAR, nLen+1);
2063 if (!p) return 0;
2064 nChars = ME_GetTextW(editor, p, nLen, start, nLen, FALSE, FALSE);
2065 WideCharToMultiByte(CP_ACP, 0, p, nChars+1, (char *)strText,
2066 nLen+1, NULL, NULL);
2067 FREE_OBJ(p);
2068 return nChars;
2069 }
2070 }
2071
2072 static int handle_EM_EXSETSEL( ME_TextEditor *editor, int to, int from )
2073 {
2074 int end;
2075
2076 TRACE("%d - %d\n", to, from );
2077
2078 ME_InvalidateSelection( editor );
2079 end = ME_SetSelection( editor, to, from );
2080 ME_InvalidateSelection( editor );
2081 ITextHost_TxShowCaret( editor->texthost, FALSE );
2082 ME_ShowCaret( editor );
2083 ME_SendSelChange( editor );
2084
2085 return end;
2086 }
2087
2088 typedef struct tagME_GlobalDestStruct
2089 {
2090 HGLOBAL hData;
2091 int nLength;
2092 } ME_GlobalDestStruct;
2093
2094 static DWORD CALLBACK ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
2095 {
2096 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
2097 int i;
2098 WORD *pSrc, *pDest;
2099
2100 cb = cb >> 1;
2101 pDest = (WORD *)lpBuff;
2102 pSrc = GlobalLock(pData->hData);
2103 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
2104 pDest[i] = pSrc[pData->nLength+i];
2105 }
2106 pData->nLength += i;
2107 *pcb = 2*i;
2108 GlobalUnlock(pData->hData);
2109 return 0;
2110 }
2111
2112 static DWORD CALLBACK ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
2113 {
2114 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
2115 int i;
2116 BYTE *pSrc, *pDest;
2117
2118 pDest = lpBuff;
2119 pSrc = GlobalLock(pData->hData);
2120 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
2121 pDest[i] = pSrc[pData->nLength+i];
2122 }
2123 pData->nLength += i;
2124 *pcb = i;
2125 GlobalUnlock(pData->hData);
2126 return 0;
2127 }
2128
2129 static BOOL ME_Paste(ME_TextEditor *editor)
2130 {
2131 DWORD dwFormat = 0;
2132 EDITSTREAM es;
2133 ME_GlobalDestStruct gds;
2134 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
2135 UINT cf = 0;
2136
2137 if (IsClipboardFormatAvailable(nRTFFormat))
2138 cf = nRTFFormat, dwFormat = SF_RTF;
2139 else if (IsClipboardFormatAvailable(CF_UNICODETEXT))
2140 cf = CF_UNICODETEXT, dwFormat = SF_TEXT|SF_UNICODE;
2141 else
2142 return FALSE;
2143
2144 if (!OpenClipboard(editor->hWnd))
2145 return FALSE;
2146 gds.hData = GetClipboardData(cf);
2147 gds.nLength = 0;
2148 es.dwCookie = (DWORD_PTR)&gds;
2149 es.pfnCallback = dwFormat == SF_RTF ? ME_ReadFromHGLOBALRTF : ME_ReadFromHGLOBALUnicode;
2150 ME_StreamIn(editor, dwFormat|SFF_SELECTION, &es, FALSE);
2151
2152 CloseClipboard();
2153 return TRUE;
2154 }
2155
2156 static BOOL ME_Copy(ME_TextEditor *editor, const ME_Cursor *start, int nChars)
2157 {
2158 LPDATAOBJECT dataObj = NULL;
2159 HRESULT hr = S_OK;
2160
2161 if (editor->cPasswordMask)
2162 return FALSE; /* Copying or Cutting masked text isn't allowed */
2163
2164 if(editor->lpOleCallback)
2165 {
2166 CHARRANGE range;
2167 range.cpMin = ME_GetCursorOfs(start);
2168 range.cpMax = range.cpMin + nChars;
2169 hr = IRichEditOleCallback_GetClipboardData(editor->lpOleCallback, &range, RECO_COPY, &dataObj);
2170 }
2171 if(FAILED(hr) || !dataObj)
2172 hr = ME_GetDataObject(editor, start, nChars, &dataObj);
2173 if(SUCCEEDED(hr)) {
2174 hr = OleSetClipboard(dataObj);
2175 IDataObject_Release(dataObj);
2176 }
2177 return SUCCEEDED(hr);
2178 }
2179
2180 /* helper to send a msg filter notification */
2181 static BOOL
2182 ME_FilterEvent(ME_TextEditor *editor, UINT msg, WPARAM* wParam, LPARAM* lParam)
2183 {
2184 MSGFILTER msgf;
2185
2186 if (!editor->hWnd || !editor->hwndParent) return FALSE;
2187 msgf.nmhdr.hwndFrom = editor->hWnd;
2188 msgf.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
2189 msgf.nmhdr.code = EN_MSGFILTER;
2190 msgf.msg = msg;
2191 msgf.wParam = *wParam;
2192 msgf.lParam = *lParam;
2193 if (SendMessageW(editor->hwndParent, WM_NOTIFY, msgf.nmhdr.idFrom, (LPARAM)&msgf))
2194 return FALSE;
2195 *wParam = msgf.wParam;
2196 *lParam = msgf.lParam;
2197 msgf.wParam = *wParam;
2198
2199 return TRUE;
2200 }
2201
2202 static void ME_UpdateSelectionLinkAttribute(ME_TextEditor *editor)
2203 {
2204 ME_DisplayItem *startPara, *endPara;
2205 ME_DisplayItem *prev_para;
2206 ME_Cursor *from, *to;
2207 ME_Cursor start;
2208 int nChars;
2209
2210 if (!editor->AutoURLDetect_bEnable) return;
2211
2212 ME_GetSelection(editor, &from, &to);
2213
2214 /* Find paragraph previous to the one that contains start cursor */
2215 startPara = from->pPara;
2216 prev_para = startPara->member.para.prev_para;
2217 if (prev_para->type == diParagraph) startPara = prev_para;
2218
2219 /* Find paragraph that contains end cursor */
2220 endPara = to->pPara->member.para.next_para;
2221
2222 start.pPara = startPara;
2223 start.pRun = ME_FindItemFwd(startPara, diRun);
2224 start.nOffset = 0;
2225 nChars = endPara->member.para.nCharOfs - startPara->member.para.nCharOfs;
2226
2227 ME_UpdateLinkAttribute(editor, &start, nChars);
2228 }
2229
2230 static BOOL
2231 ME_KeyDown(ME_TextEditor *editor, WORD nKey)
2232 {
2233 BOOL ctrl_is_down = GetKeyState(VK_CONTROL) & 0x8000;
2234 BOOL shift_is_down = GetKeyState(VK_SHIFT) & 0x8000;
2235
2236 if (editor->bMouseCaptured)
2237 return FALSE;
2238 if (nKey != VK_SHIFT && nKey != VK_CONTROL && nKey != VK_MENU)
2239 editor->nSelectionType = stPosition;
2240
2241 switch (nKey)
2242 {
2243 case VK_LEFT:
2244 case VK_RIGHT:
2245 case VK_HOME:
2246 case VK_END:
2247 editor->nUDArrowX = -1;
2248 /* fall through */
2249 case VK_UP:
2250 case VK_DOWN:
2251 case VK_PRIOR:
2252 case VK_NEXT:
2253 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
2254 ME_ArrowKey(editor, nKey, shift_is_down, ctrl_is_down);
2255 return TRUE;
2256 case VK_BACK:
2257 case VK_DELETE:
2258 editor->nUDArrowX = -1;
2259 /* FIXME backspace and delete aren't the same, they act different wrt paragraph style of the merged paragraph */
2260 if (editor->styleFlags & ES_READONLY)
2261 return FALSE;
2262 if (ME_IsSelection(editor))
2263 {
2264 ME_DeleteSelection(editor);
2265 ME_CommitUndo(editor);
2266 }
2267 else if (nKey == VK_DELETE)
2268 {
2269 /* Delete stops group typing.
2270 * (See MSDN remarks on EM_STOPGROUPTYPING message) */
2271 ME_DeleteTextAtCursor(editor, 1, 1);
2272 ME_CommitUndo(editor);
2273 }
2274 else if (ME_ArrowKey(editor, VK_LEFT, FALSE, FALSE))
2275 {
2276 BOOL bDeletionSucceeded;
2277 /* Backspace can be grouped for a single undo */
2278 ME_ContinueCoalescingTransaction(editor);
2279 bDeletionSucceeded = ME_DeleteTextAtCursor(editor, 1, 1);
2280 if (!bDeletionSucceeded && !editor->bEmulateVersion10) { /* v4.1 */
2281 /* Deletion was prevented so the cursor is moved back to where it was.
2282 * (e.g. this happens when trying to delete cell boundaries)
2283 */
2284 ME_ArrowKey(editor, VK_RIGHT, FALSE, FALSE);
2285 }
2286 ME_CommitCoalescingUndo(editor);
2287 }
2288 else
2289 return TRUE;
2290 ME_MoveCursorFromTableRowStartParagraph(editor);
2291 ME_UpdateSelectionLinkAttribute(editor);
2292 ME_UpdateRepaint(editor, FALSE);
2293 ME_SendRequestResize(editor, FALSE);
2294 return TRUE;
2295 case VK_RETURN:
2296 if (editor->bDialogMode)
2297 {
2298 if (ctrl_is_down)
2299 return TRUE;
2300
2301 if (!(editor->styleFlags & ES_WANTRETURN))
2302 {
2303 if (editor->hwndParent)
2304 {
2305 DWORD dw;
2306 dw = SendMessageW(editor->hwndParent, DM_GETDEFID, 0, 0);
2307 if (HIWORD(dw) == DC_HASDEFID)
2308 {
2309 HWND hwDefCtrl = GetDlgItem(editor->hwndParent, LOWORD(dw));
2310 if (hwDefCtrl)
2311 {
2312 SendMessageW(editor->hwndParent, WM_NEXTDLGCTL, (WPARAM)hwDefCtrl, TRUE);
2313 PostMessageW(hwDefCtrl, WM_KEYDOWN, VK_RETURN, 0);
2314 }
2315 }
2316 }
2317 return TRUE;
2318 }
2319 }
2320
2321 if (editor->styleFlags & ES_MULTILINE)
2322 {
2323 ME_Cursor cursor = editor->pCursors[0];
2324 ME_DisplayItem *para = cursor.pPara;
2325 int from, to;
2326 const WCHAR endl = '\r';
2327 const WCHAR endlv10[] = {'\r','\n'};
2328 ME_Style *style;
2329
2330 if (editor->styleFlags & ES_READONLY) {
2331 MessageBeep(MB_ICONERROR);
2332 return TRUE;
2333 }
2334
2335 ME_GetSelectionOfs(editor, &from, &to);
2336 if (editor->nTextLimit > ME_GetTextLength(editor) - (to-from))
2337 {
2338 if (!editor->bEmulateVersion10) { /* v4.1 */
2339 if (para->member.para.nFlags & MEPF_ROWEND) {
2340 /* Add a new table row after this row. */
2341 para = ME_AppendTableRow(editor, para);
2342 para = para->member.para.next_para;
2343 editor->pCursors[0].pPara = para;
2344 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2345 editor->pCursors[0].nOffset = 0;
2346 editor->pCursors[1] = editor->pCursors[0];
2347 ME_CommitUndo(editor);
2348 ME_CheckTablesForCorruption(editor);
2349 ME_UpdateRepaint(editor, FALSE);
2350 return TRUE;
2351 }
2352 else if (para == editor->pCursors[1].pPara &&
2353 cursor.nOffset + cursor.pRun->member.run.nCharOfs == 0 &&
2354 para->member.para.prev_para->member.para.nFlags & MEPF_ROWSTART &&
2355 !para->member.para.prev_para->member.para.nCharOfs)
2356 {
2357 /* Insert a newline before the table. */
2358 para = para->member.para.prev_para;
2359 para->member.para.nFlags &= ~MEPF_ROWSTART;
2360 editor->pCursors[0].pPara = para;
2361 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2362 editor->pCursors[1] = editor->pCursors[0];
2363 ME_InsertTextFromCursor(editor, 0, &endl, 1,
2364 editor->pCursors[0].pRun->member.run.style);
2365 para = editor->pBuffer->pFirst->member.para.next_para;
2366 ME_SetDefaultParaFormat(editor, para->member.para.pFmt);
2367 para->member.para.nFlags = MEPF_REWRAP;
2368 editor->pCursors[0].pPara = para;
2369 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2370 editor->pCursors[1] = editor->pCursors[0];
2371 para->member.para.next_para->member.para.nFlags |= MEPF_ROWSTART;
2372 ME_CommitCoalescingUndo(editor);
2373 ME_CheckTablesForCorruption(editor);
2374 ME_UpdateRepaint(editor, FALSE);
2375 return TRUE;
2376 }
2377 } else { /* v1.0 - 3.0 */
2378 ME_DisplayItem *para = cursor.pPara;
2379 if (ME_IsInTable(para))
2380 {
2381 if (cursor.pRun->member.run.nFlags & MERF_ENDPARA)
2382 {
2383 if (from == to) {
2384 ME_ContinueCoalescingTransaction(editor);
2385 para = ME_AppendTableRow(editor, para);
2386 editor->pCursors[0].pPara = para;
2387 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2388 editor->pCursors[0].nOffset = 0;
2389 editor->pCursors[1] = editor->pCursors[0];
2390 ME_CommitCoalescingUndo(editor);
2391 ME_UpdateRepaint(editor, FALSE);
2392 return TRUE;
2393 }
2394 } else {
2395 ME_ContinueCoalescingTransaction(editor);
2396 if (cursor.pRun->member.run.nCharOfs + cursor.nOffset == 0 &&
2397 !ME_IsInTable(para->member.para.prev_para))
2398 {
2399 /* Insert newline before table */
2400 cursor.pRun = ME_FindItemBack(para, diRun);
2401 if (cursor.pRun) {
2402 editor->pCursors[0].pRun = cursor.pRun;
2403 editor->pCursors[0].pPara = para->member.para.prev_para;
2404 }
2405 editor->pCursors[0].nOffset = 0;
2406 editor->pCursors[1] = editor->pCursors[0];
2407 ME_InsertTextFromCursor(editor, 0, &endl, 1,
2408 editor->pCursors[0].pRun->member.run.style);
2409 } else {
2410 editor->pCursors[1] = editor->pCursors[0];
2411 para = ME_AppendTableRow(editor, para);
2412 editor->pCursors[0].pPara = para;
2413 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2414 editor->pCursors[0].nOffset = 0;
2415 editor->pCursors[1] = editor->pCursors[0];
2416 }
2417 ME_CommitCoalescingUndo(editor);
2418 ME_UpdateRepaint(editor, FALSE);
2419 return TRUE;
2420 }
2421 }
2422 }
2423
2424 style = ME_GetInsertStyle(editor, 0);
2425 ME_SaveTempStyle(editor);
2426 ME_ContinueCoalescingTransaction(editor);
2427 if (shift_is_down)
2428 ME_InsertEndRowFromCursor(editor, 0);
2429 else
2430 if (!editor->bEmulateVersion10)
2431 ME_InsertTextFromCursor(editor, 0, &endl, 1, style);
2432 else
2433 ME_InsertTextFromCursor(editor, 0, endlv10, 2, style);
2434 ME_ReleaseStyle(style);
2435 ME_CommitCoalescingUndo(editor);
2436 SetCursor(NULL);
2437
2438 ME_UpdateSelectionLinkAttribute(editor);
2439 ME_UpdateRepaint(editor, FALSE);
2440 }
2441 return TRUE;
2442 }
2443 break;
2444 case VK_ESCAPE:
2445 if (editor->bDialogMode && editor->hwndParent)
2446 PostMessageW(editor->hwndParent, WM_CLOSE, 0, 0);
2447 return TRUE;
2448 case VK_TAB:
2449 if (editor->bDialogMode && editor->hwndParent)
2450 SendMessageW(editor->hwndParent, WM_NEXTDLGCTL, shift_is_down, 0);
2451 return TRUE;
2452 case 'A':
2453 if (ctrl_is_down)
2454 {
2455 handle_EM_EXSETSEL( editor, 0, -1 );
2456 return TRUE;
2457 }
2458 break;
2459 case 'V':
2460 if (ctrl_is_down)
2461 return ME_Paste(editor);
2462 break;
2463 case 'C':
2464 case 'X':
2465 if (ctrl_is_down)
2466 {
2467 BOOL result;
2468 int nOfs, nChars;
2469 int nStartCur = ME_GetSelectionOfs(editor, &nOfs, &nChars);
2470 ME_Cursor *selStart = &editor->pCursors[nStartCur];
2471
2472 nChars -= nOfs;
2473 result = ME_Copy(editor, selStart, nChars);
2474 if (result && nKey == 'X')
2475 {
2476 ME_InternalDeleteText(editor, selStart, nChars, FALSE);
2477 ME_CommitUndo(editor);
2478 ME_UpdateRepaint(editor, TRUE);
2479 }
2480 return result;
2481 }
2482 break;
2483 case 'Z':
2484 if (ctrl_is_down)
2485 {
2486 ME_Undo(editor);
2487 return TRUE;
2488 }
2489 break;
2490 case 'Y':
2491 if (ctrl_is_down)
2492 {
2493 ME_Redo(editor);
2494 return TRUE;
2495 }
2496 break;
2497
2498 default:
2499 if (nKey != VK_SHIFT && nKey != VK_CONTROL && nKey && nKey != VK_MENU)
2500 editor->nUDArrowX = -1;
2501 if (ctrl_is_down)
2502 {
2503 if (nKey == 'W')
2504 {
2505 CHARFORMAT2W chf;
2506 char buf[2048];
2507 chf.cbSize = sizeof(chf);
2508
2509 ME_GetSelectionCharFormat(editor, &chf);
2510 ME_DumpStyleToBuf(&chf, buf);
2511 MessageBoxA(NULL, buf, "Style dump", MB_OK);
2512 }
2513 if (nKey == 'Q')
2514 {
2515 ME_CheckCharOffsets(editor);
2516 }
2517 }
2518 }
2519 return FALSE;
2520 }
2521
2522 static LRESULT ME_Char(ME_TextEditor *editor, WPARAM charCode,
2523 LPARAM flags, BOOL unicode)
2524 {
2525 WCHAR wstr;
2526
2527 if (editor->bMouseCaptured)
2528 return 0;
2529
2530 if (unicode)
2531 wstr = (WCHAR)charCode;
2532 else
2533 {
2534 CHAR charA = charCode;
2535 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &wstr, 1);
2536 }
2537
2538 if (editor->styleFlags & ES_READONLY) {
2539 MessageBeep(MB_ICONERROR);
2540 return 0; /* FIXME really 0 ? */
2541 }
2542
2543 if ((unsigned)wstr >= ' ' || wstr == '\t')
2544 {
2545 ME_Cursor cursor = editor->pCursors[0];
2546 ME_DisplayItem *para = cursor.pPara;
2547 int from, to;
2548 BOOL ctrl_is_down = GetKeyState(VK_CONTROL) & 0x8000;
2549 ME_GetSelectionOfs(editor, &from, &to);
2550 if (wstr == '\t' &&
2551 /* v4.1 allows tabs to be inserted with ctrl key down */
2552 !(ctrl_is_down && !editor->bEmulateVersion10))
2553 {
2554 ME_DisplayItem *para;
2555 BOOL bSelectedRow = FALSE;
2556
2557 para = cursor.pPara;
2558 if (ME_IsSelection(editor) &&
2559 cursor.pRun->member.run.nCharOfs + cursor.nOffset == 0 &&
2560 to == ME_GetCursorOfs(&editor->pCursors[0]) &&
2561 para->member.para.prev_para->type == diParagraph)
2562 {
2563 para = para->member.para.prev_para;
2564 bSelectedRow = TRUE;
2565 }
2566 if (ME_IsInTable(para))
2567 {
2568 ME_TabPressedInTable(editor, bSelectedRow);
2569 ME_CommitUndo(editor);
2570 return 0;
2571 }
2572 } else if (!editor->bEmulateVersion10) { /* v4.1 */
2573 if (para->member.para.nFlags & MEPF_ROWEND) {
2574 if (from == to) {
2575 para = para->member.para.next_para;
2576 if (para->member.para.nFlags & MEPF_ROWSTART)
2577 para = para->member.para.next_para;
2578 editor->pCursors[0].pPara = para;
2579 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2580 editor->pCursors[0].nOffset = 0;
2581 editor->pCursors[1] = editor->pCursors[0];
2582 }
2583 }
2584 } else { /* v1.0 - 3.0 */
2585 if (ME_IsInTable(cursor.pRun) &&
2586 cursor.pRun->member.run.nFlags & MERF_ENDPARA &&
2587 from == to)
2588 {
2589 /* Text should not be inserted at the end of the table. */
2590 MessageBeep(-1);
2591 return 0;
2592 }
2593 }
2594 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
2595 /* WM_CHAR is restricted to nTextLimit */
2596 if(editor->nTextLimit > ME_GetTextLength(editor) - (to-from))
2597 {
2598 ME_Style *style = ME_GetInsertStyle(editor, 0);
2599 ME_SaveTempStyle(editor);
2600 ME_ContinueCoalescingTransaction(editor);
2601 ME_InsertTextFromCursor(editor, 0, &wstr, 1, style);
2602 ME_ReleaseStyle(style);
2603 ME_CommitCoalescingUndo(editor);
2604 ITextHost_TxSetCursor(editor->texthost, NULL, FALSE);
2605 }
2606
2607 ME_UpdateSelectionLinkAttribute(editor);
2608 ME_UpdateRepaint(editor, FALSE);
2609 }
2610 return 0;
2611 }
2612
2613 /* Process the message and calculate the new click count.
2614 *
2615 * returns: The click count if it is mouse down event, else returns 0. */
2616 static int ME_CalculateClickCount(ME_TextEditor *editor, UINT msg, WPARAM wParam,
2617 LPARAM lParam)
2618 {
2619 static int clickNum = 0;
2620 if (msg < WM_MOUSEFIRST || msg > WM_MOUSELAST)
2621 return 0;
2622
2623 if ((msg == WM_LBUTTONDBLCLK) ||
2624 (msg == WM_RBUTTONDBLCLK) ||
2625 (msg == WM_MBUTTONDBLCLK) ||
2626 (msg == WM_XBUTTONDBLCLK))
2627 {
2628 msg -= (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN);
2629 }
2630
2631 if ((msg == WM_LBUTTONDOWN) ||
2632 (msg == WM_RBUTTONDOWN) ||
2633 (msg == WM_MBUTTONDOWN) ||
2634 (msg == WM_XBUTTONDOWN))
2635 {
2636 static MSG prevClickMsg;
2637 MSG clickMsg;
2638 /* Compare the editor instead of the hwnd so that the this
2639 * can still be done for windowless richedit controls. */
2640 clickMsg.hwnd = (HWND)editor;
2641 clickMsg.message = msg;
2642 clickMsg.wParam = wParam;
2643 clickMsg.lParam = lParam;
2644 clickMsg.time = GetMessageTime();
2645 clickMsg.pt.x = (short)LOWORD(lParam);
2646 clickMsg.pt.y = (short)HIWORD(lParam);
2647 if ((clickNum != 0) &&
2648 (clickMsg.message == prevClickMsg.message) &&
2649 (clickMsg.hwnd == prevClickMsg.hwnd) &&
2650 (clickMsg.wParam == prevClickMsg.wParam) &&
2651 (clickMsg.time - prevClickMsg.time < GetDoubleClickTime()) &&
2652 (abs(clickMsg.pt.x - prevClickMsg.pt.x) < GetSystemMetrics(SM_CXDOUBLECLK)/2) &&
2653 (abs(clickMsg.pt.y - prevClickMsg.pt.y) < GetSystemMetrics(SM_CYDOUBLECLK)/2))
2654 {
2655 clickNum++;
2656 } else {
2657 clickNum = 1;
2658 }
2659 prevClickMsg = clickMsg;
2660 } else {
2661 return 0;
2662 }
2663 return clickNum;
2664 }
2665
2666 static BOOL ME_SetCursor(ME_TextEditor *editor)
2667 {
2668 ME_Cursor cursor;
2669 POINT pt;
2670 BOOL isExact;
2671 SCROLLBARINFO sbi;
2672 DWORD messagePos = GetMessagePos();
2673 pt.x = (short)LOWORD(messagePos);
2674 pt.y = (short)HIWORD(messagePos);
2675
2676 if (editor->hWnd)
2677 {
2678 sbi.cbSize = sizeof(sbi);
2679 GetScrollBarInfo(editor->hWnd, OBJID_HSCROLL, &sbi);
2680 if (!(sbi.rgstate[0] & (STATE_SYSTEM_INVISIBLE|STATE_SYSTEM_OFFSCREEN)) &&
2681 PtInRect(&sbi.rcScrollBar, pt))
2682 {
2683 ITextHost_TxSetCursor(editor->texthost,
2684 LoadCursorW(NULL, (WCHAR*)IDC_ARROW), FALSE);
2685 return TRUE;
2686 }
2687 sbi.cbSize = sizeof(sbi);
2688 GetScrollBarInfo(editor->hWnd, OBJID_VSCROLL, &sbi);
2689 if (!(sbi.rgstate[0] & (STATE_SYSTEM_INVISIBLE|STATE_SYSTEM_OFFSCREEN)) &&
2690 PtInRect(&sbi.rcScrollBar, pt))
2691 {
2692 ITextHost_TxSetCursor(editor->texthost,
2693 LoadCursorW(NULL, (WCHAR*)IDC_ARROW), FALSE);
2694 return TRUE;
2695 }
2696 }
2697 ITextHost_TxScreenToClient(editor->texthost, &pt);
2698
2699 if (editor->nSelectionType == stLine && editor->bMouseCaptured) {
2700 ITextHost_TxSetCursor(editor->texthost, hLeft, FALSE);
2701 return TRUE;
2702 }
2703 if (!editor->bEmulateVersion10 /* v4.1 */ &&
2704 pt.y < editor->rcFormat.top &&
2705 pt.x < editor->rcFormat.left)
2706 {
2707 ITextHost_TxSetCursor(editor->texthost, hLeft, FALSE);
2708 return TRUE;
2709 }
2710 if (pt.y < editor->rcFormat.top || pt.y > editor->rcFormat.bottom)
2711 {
2712 if (editor->bEmulateVersion10) /* v1.0 - 3.0 */
2713 ITextHost_TxSetCursor(editor->texthost,
2714 LoadCursorW(NULL, (WCHAR*)IDC_ARROW), FALSE);
2715 else /* v4.1 */
2716 ITextHost_TxSetCursor(editor->texthost,
2717 LoadCursorW(NULL, (WCHAR*)IDC_IBEAM), TRUE);
2718 return TRUE;
2719 }
2720 if (pt.x < editor->rcFormat.left)
2721 {
2722 ITextHost_TxSetCursor(editor->texthost, hLeft, FALSE);
2723 return TRUE;
2724 }
2725 ME_CharFromPos(editor, pt.x, pt.y, &cursor, &isExact);
2726 if (isExact)
2727 {
2728 ME_Run *run;
2729
2730 run = &cursor.pRun->member.run;
2731 if (run->style->fmt.dwMask & CFM_LINK &&
2732 run->style->fmt.dwEffects & CFE_LINK)
2733 {
2734 ITextHost_TxSetCursor(editor->texthost,
2735 LoadCursorW(NULL, (WCHAR*)IDC_HAND),
2736 FALSE);
2737 return TRUE;
2738 }
2739
2740 if (ME_IsSelection(editor))
2741 {
2742 int selStart, selEnd;
2743 int offset = ME_GetCursorOfs(&cursor);
2744
2745 ME_GetSelectionOfs(editor, &selStart, &selEnd);
2746 if (selStart <= offset && selEnd >= offset) {
2747 ITextHost_TxSetCursor(editor->texthost,
2748 LoadCursorW(NULL, (WCHAR*)IDC_ARROW),
2749 FALSE);
2750 return TRUE;
2751 }
2752 }
2753 }
2754 ITextHost_TxSetCursor(editor->texthost,
2755 LoadCursorW(NULL, (WCHAR*)IDC_IBEAM), TRUE);
2756 return TRUE;
2757 }
2758
2759 static void ME_SetDefaultFormatRect(ME_TextEditor *editor)
2760 {
2761 ITextHost_TxGetClientRect(editor->texthost, &editor->rcFormat);
2762 editor->rcFormat.top += editor->exStyleFlags & WS_EX_CLIENTEDGE ? 1 : 0;
2763 editor->rcFormat.left += 1 + editor->selofs;
2764 editor->rcFormat.right -= 1;
2765 }
2766
2767 static BOOL ME_ShowContextMenu(ME_TextEditor *editor, int x, int y)
2768 {
2769 CHARRANGE selrange;
2770 HMENU menu;
2771 int seltype = 0;
2772 if(!editor->lpOleCallback || !editor->hWnd)
2773 return FALSE;
2774 ME_GetSelectionOfs(editor, &selrange.cpMin, &selrange.cpMax);
2775 if(selrange.cpMin == selrange.cpMax)
2776 seltype |= SEL_EMPTY;
2777 else
2778 {
2779 /* FIXME: Handle objects */
2780 seltype |= SEL_TEXT;
2781 if(selrange.cpMax-selrange.cpMin > 1)
2782 seltype |= SEL_MULTICHAR;
2783 }
2784 if(SUCCEEDED(IRichEditOleCallback_GetContextMenu(editor->lpOleCallback, seltype, NULL, &selrange, &menu)))
2785 {
2786 TrackPopupMenu(menu, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, editor->hwndParent, NULL);
2787 DestroyMenu(menu);
2788 }
2789 return TRUE;
2790 }
2791
2792 ME_TextEditor *ME_MakeEditor(ITextHost *texthost, BOOL bEmulateVersion10, DWORD csStyle)
2793 {
2794 ME_TextEditor *ed = ALLOC_OBJ(ME_TextEditor);
2795 int i;
2796 DWORD props;
2797 LONG selbarwidth;
2798
2799 ed->hWnd = NULL;
2800 ed->hwndParent = NULL;
2801 ed->sizeWindow.cx = ed->sizeWindow.cy = 0;
2802 ed->texthost = texthost;
2803 ed->reOle = NULL;
2804 ed->bEmulateVersion10 = bEmulateVersion10;
2805 ed->styleFlags = 0;
2806 ed->alignStyle = PFA_LEFT;
2807 if (csStyle & ES_RIGHT)
2808 ed->alignStyle = PFA_RIGHT;
2809 if (csStyle & ES_CENTER)
2810 ed->alignStyle = PFA_CENTER;
2811 ITextHost_TxGetPropertyBits(texthost,
2812 (TXTBIT_RICHTEXT|TXTBIT_MULTILINE|
2813 TXTBIT_READONLY|TXTBIT_USEPASSWORD|
2814 TXTBIT_HIDESELECTION|TXTBIT_SAVESELECTION|
2815 TXTBIT_AUTOWORDSEL|TXTBIT_VERTICAL|
2816 TXTBIT_WORDWRAP|TXTBIT_DISABLEDRAG),
2817 &props);
2818 ITextHost_TxGetScrollBars(texthost, &ed->styleFlags);
2819 ed->styleFlags &= (WS_VSCROLL|WS_HSCROLL|ES_AUTOVSCROLL|
2820 ES_AUTOHSCROLL|ES_DISABLENOSCROLL);
2821 ed->pBuffer = ME_MakeText();
2822 ed->nZoomNumerator = ed->nZoomDenominator = 0;
2823 ed->nAvailWidth = 0; /* wrap to client area */
2824 ME_MakeFirstParagraph(ed);
2825 /* The four cursors are for:
2826 * 0 - The position where the caret is shown
2827 * 1 - The anchored end of the selection (for normal selection)
2828 * 2 & 3 - The anchored start and end respectively for word, line,
2829 * or paragraph selection.
2830 */
2831 ed->nCursors = 4;
2832 ed->pCursors = ALLOC_N_OBJ(ME_Cursor, ed->nCursors);
2833 ME_SetCursorToStart(ed, &ed->pCursors[0]);
2834 ed->pCursors[1] = ed->pCursors[0];
2835 ed->pCursors[2] = ed->pCursors[0];
2836 ed->pCursors[3] = ed->pCursors[1];
2837 ed->nLastTotalLength = ed->nTotalLength = 0;
2838 ed->nLastTotalWidth = ed->nTotalWidth = 0;
2839 ed->nUDArrowX = -1;
2840 ed->rgbBackColor = -1;
2841 ed->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
2842 ed->bCaretAtEnd = FALSE;
2843 ed->nEventMask = 0;
2844 ed->nModifyStep = 0;
2845 ed->nTextLimit = TEXT_LIMIT_DEFAULT;
2846 list_init( &ed->undo_stack );
2847 list_init( &ed->redo_stack );
2848 ed->nUndoStackSize = 0;
2849 ed->nUndoLimit = STACK_SIZE_DEFAULT;
2850 ed->nUndoMode = umAddToUndo;
2851 ed->nParagraphs = 1;
2852 ed->nLastSelStart = ed->nLastSelEnd = 0;
2853 ed->pLastSelStartPara = ed->pLastSelEndPara = ed->pCursors[0].pPara;
2854 ed->bHideSelection = FALSE;
2855 ed->pfnWordBreak = NULL;
2856 ed->lpOleCallback = NULL;
2857 ed->mode = TM_MULTILEVELUNDO | TM_MULTICODEPAGE;
2858 ed->mode |= (props & TXTBIT_RICHTEXT) ? TM_RICHTEXT : TM_PLAINTEXT;
2859 ed->AutoURLDetect_bEnable = FALSE;
2860 ed->bHaveFocus = FALSE;
2861 ed->bDialogMode = FALSE;
2862 ed->bMouseCaptured = FALSE;
2863 for (i=0; i<HFONT_CACHE_SIZE; i++)
2864 {
2865 ed->pFontCache[i].nRefs = 0;
2866 ed->pFontCache[i].nAge = 0;
2867 ed->pFontCache[i].hFont = NULL;
2868 }
2869
2870 ME_CheckCharOffsets(ed);
2871 ed->bDefaultFormatRect = TRUE;
2872 ITextHost_TxGetSelectionBarWidth(ed->texthost, &selbarwidth);
2873 if (selbarwidth) {
2874 /* FIXME: Convert selbarwidth from HIMETRIC to pixels */
2875 ed->selofs = SELECTIONBAR_WIDTH;
2876 ed->styleFlags |= ES_SELECTIONBAR;
2877 } else {
2878 ed->selofs = 0;
2879 }
2880 ed->nSelectionType = stPosition;
2881
2882 ed->cPasswordMask = 0;
2883 if (props & TXTBIT_USEPASSWORD)
2884 ITextHost_TxGetPasswordChar(texthost, &ed->cPasswordMask);
2885
2886 if (props & TXTBIT_AUTOWORDSEL)
2887 ed->styleFlags |= ECO_AUTOWORDSELECTION;
2888 if (props & TXTBIT_MULTILINE) {
2889 ed->styleFlags |= ES_MULTILINE;
2890 ed->bWordWrap = (props & TXTBIT_WORDWRAP) != 0;
2891 } else {
2892 ed->bWordWrap = FALSE;
2893 }
2894 if (props & TXTBIT_READONLY)
2895 ed->styleFlags |= ES_READONLY;
2896 if (!(props & TXTBIT_HIDESELECTION))
2897 ed->styleFlags |= ES_NOHIDESEL;
2898 if (props & TXTBIT_SAVESELECTION)
2899 ed->styleFlags |= ES_SAVESEL;
2900 if (props & TXTBIT_VERTICAL)
2901 ed->styleFlags |= ES_VERTICAL;
2902 if (props & TXTBIT_DISABLEDRAG)
2903 ed->styleFlags |= ES_NOOLEDRAGDROP;
2904
2905 ed->notified_cr.cpMin = ed->notified_cr.cpMax = 0;
2906
2907 /* Default scrollbar information */
2908 ed->vert_si.cbSize = sizeof(SCROLLINFO);
2909 ed->vert_si.nMin = 0;
2910 ed->vert_si.nMax = 0;
2911 ed->vert_si.nPage = 0;
2912 ed->vert_si.nPos = 0;
2913
2914 ed->horz_si.cbSize = sizeof(SCROLLINFO);
2915 ed->horz_si.nMin = 0;
2916 ed->horz_si.nMax = 0;
2917 ed->horz_si.nPage = 0;
2918 ed->horz_si.nPos = 0;
2919
2920 ed->wheel_remain = 0;
2921
2922 list_init( &ed->style_list );
2923 OleInitialize(NULL);
2924
2925 return ed;
2926 }
2927
2928 void ME_DestroyEditor(ME_TextEditor *editor)
2929 {
2930 ME_DisplayItem *pFirst = editor->pBuffer->pFirst;
2931 ME_DisplayItem *p = pFirst, *pNext = NULL;
2932 ME_Style *s, *cursor2;
2933 int i;
2934
2935 ME_ClearTempStyle(editor);
2936 ME_EmptyUndoStack(editor);
2937 while(p) {
2938 pNext = p->next;
2939 ME_DestroyDisplayItem(p);
2940 p = pNext;
2941 }
2942
2943 LIST_FOR_EACH_ENTRY_SAFE( s, cursor2, &editor->style_list, ME_Style, entry )
2944 ME_DestroyStyle( s );
2945
2946 ME_ReleaseStyle(editor->pBuffer->pDefaultStyle);
2947 for (i=0; i<HFONT_CACHE_SIZE; i++)
2948 {
2949 if (editor->pFontCache[i].hFont)
2950 DeleteObject(editor->pFontCache[i].hFont);
2951 }
2952 if (editor->rgbBackColor != -1)
2953 DeleteObject(editor->hbrBackground);
2954 if(editor->lpOleCallback)
2955 IRichEditOleCallback_Release(editor->lpOleCallback);
2956 ITextHost_Release(editor->texthost);
2957 if (editor->reOle)
2958 {
2959 IRichEditOle_Release(editor->reOle);
2960 editor->reOle = NULL;
2961 }
2962 OleUninitialize();
2963
2964 FREE_OBJ(editor->pBuffer);
2965 FREE_OBJ(editor->pCursors);
2966
2967 FREE_OBJ(editor);
2968 }
2969
2970 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
2971 {
2972 TRACE("\n");
2973 switch (fdwReason)
2974 {
2975 case DLL_PROCESS_ATTACH:
2976 DisableThreadLibraryCalls(hinstDLL);
2977 me_heap = HeapCreate (0, 0x10000, 0);
2978 if (!ME_RegisterEditorClass(hinstDLL)) return FALSE;
2979 hLeft = LoadCursorW(hinstDLL, MAKEINTRESOURCEW(OCR_REVERSE));
2980 LookupInit();
2981 break;
2982
2983 case DLL_PROCESS_DETACH:
2984 if (lpvReserved) break;
2985 UnregisterClassW(RICHEDIT_CLASS20W, 0);
2986 UnregisterClassW(MSFTEDIT_CLASS, 0);
2987 UnregisterClassA(RICHEDIT_CLASS20A, 0);
2988 UnregisterClassA("RichEdit50A", 0);
2989 if (ME_ListBoxRegistered)
2990 UnregisterClassW(REListBox20W, 0);
2991 if (ME_ComboBoxRegistered)
2992 UnregisterClassW(REComboBox20W, 0);
2993 LookupCleanup();
2994 HeapDestroy (me_heap);
2995 release_typelib();
2996 break;
2997 }
2998 return TRUE;
2999 }
3000
3001 static inline int get_default_line_height( ME_TextEditor *editor )
3002 {
3003 int height = 0;
3004
3005 if (editor->pBuffer && editor->pBuffer->pDefaultStyle)
3006 height = editor->pBuffer->pDefaultStyle->tm.tmHeight;
3007 if (height <= 0) height = 24;
3008
3009 return height;
3010 }
3011
3012 static inline int calc_wheel_change( int *remain, int amount_per_click )
3013 {
3014 int change = amount_per_click * (float)*remain / WHEEL_DELTA;
3015 *remain -= WHEEL_DELTA * change / amount_per_click;
3016 return change;
3017 }
3018
3019 static const char * const edit_messages[] = {
3020 "EM_GETSEL",
3021 "EM_SETSEL",
3022 "EM_GETRECT",
3023 "EM_SETRECT",
3024 "EM_SETRECTNP",
3025 "EM_SCROLL",
3026 "EM_LINESCROLL",
3027 "EM_SCROLLCARET",
3028 "EM_GETMODIFY",
3029 "EM_SETMODIFY",
3030 "EM_GETLINECOUNT",
3031 "EM_LINEINDEX",
3032 "EM_SETHANDLE",
3033 "EM_GETHANDLE",
3034 "EM_GETTHUMB",
3035 "EM_UNKNOWN_BF",
3036 "EM_UNKNOWN_C0",
3037 "EM_LINELENGTH",
3038 "EM_REPLACESEL",
3039 "EM_UNKNOWN_C3",
3040 "EM_GETLINE",
3041 "EM_LIMITTEXT",
3042 "EM_CANUNDO",
3043 "EM_UNDO",
3044 "EM_FMTLINES",
3045 "EM_LINEFROMCHAR",
3046 "EM_UNKNOWN_CA",
3047 "EM_SETTABSTOPS",
3048 "EM_SETPASSWORDCHAR",
3049 "EM_EMPTYUNDOBUFFER",