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