8d995f7446f58315ad43be84ff1c8beb43c8fc9c
[reactos.git] / reactos / dll / win32 / riched20 / editor.c
1 /*
2 * RichEdit - functions dealing with editor object
3 *
4 * Copyright 2004 by Krzysztof Foltman
5 * Copyright 2005 by Cihan Altinay
6 * Copyright 2005 by Phil Krylov
7 * Copyright 2008 Eric Pouech
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24 /*
25 API implementation status:
26
27 Messages (ANSI versions not done yet)
28 + EM_AUTOURLDETECT 2.0
29 + EM_CANPASTE
30 + EM_CANREDO 2.0
31 + EM_CANUNDO
32 + EM_CHARFROMPOS
33 - EM_DISPLAYBAND
34 + EM_EMPTYUNDOBUFFER
35 + EM_EXGETSEL
36 + EM_EXLIMITTEXT
37 + EM_EXLINEFROMCHAR
38 + EM_EXSETSEL
39 + EM_FINDTEXT (only FR_DOWN flag implemented)
40 + EM_FINDTEXTEX (only FR_DOWN flag implemented)
41 - EM_FINDWORDBREAK
42 - EM_FMTLINES
43 - EM_FORMATRANGE
44 + EM_GETAUTOURLDETECT 2.0
45 - EM_GETBIDIOPTIONS 3.0
46 - EM_GETCHARFORMAT (partly done)
47 - EM_GETEDITSTYLE
48 + EM_GETEVENTMASK
49 + EM_GETFIRSTVISIBLELINE (can be optimized if needed)
50 - EM_GETIMECOLOR 1.0asian
51 - EM_GETIMECOMPMODE 2.0
52 - EM_GETIMEOPTIONS 1.0asian
53 - EM_GETIMESTATUS
54 - EM_GETLANGOPTIONS 2.0
55 + EM_GETLIMITTEXT
56 + EM_GETLINE
57 + EM_GETLINECOUNT returns number of rows, not of paragraphs
58 + EM_GETMODIFY
59 + EM_GETOLEINTERFACE
60 + EM_GETOPTIONS
61 + EM_GETPARAFORMAT
62 + EM_GETPASSWORDCHAR 2.0
63 - EM_GETPUNCTUATION 1.0asian
64 + EM_GETRECT
65 - EM_GETREDONAME 2.0
66 + EM_GETSEL
67 + EM_GETSELTEXT (ANSI&Unicode)
68 + EM_GETSCROLLPOS 3.0
69 ! - EM_GETTHUMB
70 + EM_GETTEXTEX 2.0
71 + EM_GETTEXTLENGTHEX (GTL_PRECISE unimplemented)
72 + EM_GETTEXTMODE 2.0
73 ? + EM_GETTEXTRANGE (ANSI&Unicode)
74 - EM_GETTYPOGRAPHYOPTIONS 3.0
75 - EM_GETUNDONAME
76 + EM_GETWORDBREAKPROC
77 - EM_GETWORDBREAKPROCEX
78 - EM_GETWORDWRAPMODE 1.0asian
79 + EM_GETZOOM 3.0
80 + EM_HIDESELECTION
81 + EM_LIMITTEXT (Also called EM_SETLIMITTEXT)
82 + EM_LINEFROMCHAR
83 + EM_LINEINDEX
84 + EM_LINELENGTH
85 + EM_LINESCROLL
86 - EM_PASTESPECIAL
87 + EM_POSFROMCHAR
88 + EM_REDO 2.0
89 + EM_REQUESTRESIZE
90 + EM_REPLACESEL (proper style?) ANSI&Unicode
91 + EM_SCROLL
92 + EM_SCROLLCARET
93 - EM_SELECTIONTYPE
94 - EM_SETBIDIOPTIONS 3.0
95 + EM_SETBKGNDCOLOR
96 + EM_SETCHARFORMAT (partly done, no ANSI)
97 - EM_SETEDITSTYLE
98 + EM_SETEVENTMASK (few notifications supported)
99 - EM_SETFONTSIZE
100 - EM_SETIMECOLOR 1.0asian
101 - EM_SETIMEOPTIONS 1.0asian
102 - EM_SETIMESTATUS
103 - EM_SETLANGOPTIONS 2.0
104 - EM_SETLIMITTEXT
105 - EM_SETMARGINS
106 + EM_SETMODIFY (not sure if implementation is correct)
107 - EM_SETOLECALLBACK
108 + EM_SETOPTIONS (partially implemented)
109 - EM_SETPALETTE 2.0
110 + EM_SETPARAFORMAT
111 + EM_SETPASSWORDCHAR 2.0
112 - EM_SETPUNCTUATION 1.0asian
113 + EM_SETREADONLY no beep on modification attempt
114 + EM_SETRECT
115 + EM_SETRECTNP (EM_SETRECT without repainting)
116 + EM_SETSEL
117 + EM_SETSCROLLPOS 3.0
118 - EM_SETTABSTOPS 3.0
119 - EM_SETTARGETDEVICE (partial)
120 + EM_SETTEXTEX 3.0 (proper style?)
121 - EM_SETTEXTMODE 2.0
122 - EM_SETTYPOGRAPHYOPTIONS 3.0
123 + EM_SETUNDOLIMIT 2.0
124 + EM_SETWORDBREAKPROC (used only for word movement at the moment)
125 - EM_SETWORDBREAKPROCEX
126 - EM_SETWORDWRAPMODE 1.0asian
127 + EM_SETZOOM 3.0
128 + EM_SHOWSCROLLBAR 2.0
129 + EM_STOPGROUPTYPING 2.0
130 + EM_STREAMIN
131 + EM_STREAMOUT
132 + EM_UNDO
133 + WM_CHAR
134 + WM_CLEAR
135 + WM_COPY
136 + WM_CUT
137 + WM_GETDLGCODE (the current implementation is incomplete)
138 + WM_GETTEXT (ANSI&Unicode)
139 + WM_GETTEXTLENGTH (ANSI version sucks)
140 + WM_HSCROLL
141 + WM_PASTE
142 + WM_SETFONT
143 + WM_SETTEXT (resets undo stack !) (proper style?) ANSI&Unicode
144 + WM_STYLECHANGING (seems to do nothing)
145 + WM_STYLECHANGED (seems to do nothing)
146 + WM_UNICHAR
147 + WM_VSCROLL
148
149 Notifications
150
151 * EN_CHANGE (sent from the wrong place)
152 - EN_CORRECTTEXT
153 - EN_DROPFILES
154 - EN_ERRSPACE
155 - EN_HSCROLL
156 - EN_IMECHANGE
157 + EN_KILLFOCUS
158 - EN_LINK
159 - EN_MAXTEXT
160 - EN_MSGFILTER
161 - EN_OLEOPFAILED
162 - EN_PROTECTED
163 + EN_REQUESTRESIZE
164 - EN_SAVECLIPBOARD
165 + EN_SELCHANGE
166 + EN_SETFOCUS
167 - EN_STOPNOUNDO
168 * EN_UPDATE (sent from the wrong place)
169 - EN_VSCROLL
170
171 Styles
172
173 - ES_AUTOHSCROLL
174 - ES_AUTOVSCROLL
175 - ES_CENTER
176 + ES_DISABLENOSCROLL (scrollbar is always visible)
177 - ES_EX_NOCALLOLEINIT
178 - ES_LEFT
179 - ES_MULTILINE (currently single line controls aren't supported)
180 - ES_NOIME
181 - ES_READONLY (I'm not sure if beeping is the proper behaviour)
182 - ES_RIGHT
183 - ES_SAVESEL
184 - ES_SELFIME
185 - ES_SUNKEN
186 - ES_VERTICAL
187 - ES_WANTRETURN (don't know how to do WM_GETDLGCODE part)
188 - WS_SETFONT
189 + WS_HSCROLL
190 + WS_VSCROLL
191 */
192
193 /*
194 * RICHED20 TODO (incomplete):
195 *
196 * - messages/styles/notifications listed above
197 * - add remaining CHARFORMAT/PARAFORMAT fields
198 * - right/center align should strip spaces from the beginning
199 * - pictures/OLE objects (not just smiling faces that lack API support ;-) )
200 * - COM interface (looks like a major pain in the TODO list)
201 * - calculate heights of pictures (half-done)
202 * - hysteresis during wrapping (related to scrollbars appearing/disappearing)
203 * - find/replace
204 * - how to implement EM_FORMATRANGE and EM_DISPLAYBAND ? (Mission Impossible)
205 * - italic caret with italic fonts
206 * - IME
207 * - most notifications aren't sent at all (the most important ones are)
208 * - when should EN_SELCHANGE be sent after text change ? (before/after EN_UPDATE?)
209 * - WM_SETTEXT may use wrong style (but I'm 80% sure it's OK)
210 * - EM_GETCHARFORMAT with SCF_SELECTION may not behave 100% like in original (but very close)
211 * - full justification
212 * - hyphenation
213 * - tables
214 * - ListBox & ComboBox not implemented
215 *
216 * Bugs that are probably fixed, but not so easy to verify:
217 * - EN_UPDATE/EN_CHANGE are handled very incorrectly (should be OK now)
218 * - undo for ME_JoinParagraphs doesn't store paragraph format ? (it does)
219 * - check/fix artificial EOL logic (bCursorAtEnd, hardly logical)
220 * - caret shouldn't be displayed when selection isn't empty
221 * - check refcounting in style management functions (looks perfect now, but no bugs is suspicious)
222 * - undo for setting default format (done, might be buggy)
223 * - styles might be not released properly (looks like they work like charm, but who knows?
224 *
225 */
226
227 #include "editor.h"
228
229 #include <commdlg.h>
230 #include <undocuser.h>
231
232 #include "rtf.h"
233 #include "res.h"
234
235 #define STACK_SIZE_DEFAULT 100
236 #define STACK_SIZE_MAX 1000
237
238 #define TEXT_LIMIT_DEFAULT 32767
239
240 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
241
242 static BOOL ME_RegisterEditorClass(HINSTANCE);
243 static BOOL ME_UpdateLinkAttribute(ME_TextEditor *editor, ME_Cursor *start, int nChars);
244
245 static const WCHAR REListBox20W[] = {'R','E','L','i','s','t','B','o','x','2','0','W', 0};
246 static const WCHAR REComboBox20W[] = {'R','E','C','o','m','b','o','B','o','x','2','0','W', 0};
247 static HCURSOR hLeft;
248
249 BOOL me_debug = FALSE;
250 HANDLE me_heap = NULL;
251
252 static BOOL ME_ListBoxRegistered = FALSE;
253 static BOOL ME_ComboBoxRegistered = FALSE;
254
255 static inline BOOL is_version_nt(void)
256 {
257 return !(GetVersion() & 0x80000000);
258 }
259
260 static ME_TextBuffer *ME_MakeText(void) {
261
262 ME_TextBuffer *buf = ALLOC_OBJ(ME_TextBuffer);
263
264 ME_DisplayItem *p1 = ME_MakeDI(diTextStart);
265 ME_DisplayItem *p2 = ME_MakeDI(diTextEnd);
266
267 p1->prev = NULL;
268 p1->next = p2;
269 p2->prev = p1;
270 p2->next = NULL;
271 p1->member.para.next_para = p2;
272 p2->member.para.prev_para = p1;
273 p2->member.para.nCharOfs = 0;
274
275 buf->pFirst = p1;
276 buf->pLast = p2;
277 buf->pCharStyle = NULL;
278
279 return buf;
280 }
281
282
283 static LRESULT ME_StreamInText(ME_TextEditor *editor, DWORD dwFormat, ME_InStream *stream, ME_Style *style)
284 {
285 WCHAR *pText;
286 LRESULT total_bytes_read = 0;
287 BOOL is_read = FALSE;
288 DWORD cp = CP_ACP, copy = 0;
289 char conv_buf[4 + STREAMIN_BUFFER_SIZE]; /* up to 4 additional UTF-8 bytes */
290
291 static const char bom_utf8[] = {0xEF, 0xBB, 0xBF};
292
293 TRACE("%08x %p\n", dwFormat, stream);
294
295 do {
296 LONG nWideChars = 0;
297 WCHAR wszText[STREAMIN_BUFFER_SIZE+1];
298
299 if (!stream->dwSize)
300 {
301 ME_StreamInFill(stream);
302 if (stream->editstream->dwError)
303 break;
304 if (!stream->dwSize)
305 break;
306 total_bytes_read += stream->dwSize;
307 }
308
309 if (!(dwFormat & SF_UNICODE))
310 {
311 char * buf = stream->buffer;
312 DWORD size = stream->dwSize, end;
313
314 if (!is_read)
315 {
316 is_read = TRUE;
317 if (stream->dwSize >= 3 && !memcmp(stream->buffer, bom_utf8, 3))
318 {
319 cp = CP_UTF8;
320 buf += 3;
321 size -= 3;
322 }
323 }
324
325 if (cp == CP_UTF8)
326 {
327 if (copy)
328 {
329 memcpy(conv_buf + copy, buf, size);
330 buf = conv_buf;
331 size += copy;
332 }
333 end = size;
334 while ((buf[end-1] & 0xC0) == 0x80)
335 {
336 --end;
337 --total_bytes_read; /* strange, but seems to match windows */
338 }
339 if (buf[end-1] & 0x80)
340 {
341 DWORD need = 0;
342 if ((buf[end-1] & 0xE0) == 0xC0)
343 need = 1;
344 if ((buf[end-1] & 0xF0) == 0xE0)
345 need = 2;
346 if ((buf[end-1] & 0xF8) == 0xF0)
347 need = 3;
348
349 if (size - end >= need)
350 {
351 /* we have enough bytes for this sequence */
352 end = size;
353 }
354 else
355 {
356 /* need more bytes, so don't transcode this sequence */
357 --end;
358 }
359 }
360 }
361 else
362 end = size;
363
364 nWideChars = MultiByteToWideChar(cp, 0, buf, end, wszText, STREAMIN_BUFFER_SIZE);
365 pText = wszText;
366
367 if (cp == CP_UTF8)
368 {
369 if (end != size)
370 {
371 memcpy(conv_buf, buf + end, size - end);
372 copy = size - end;
373 }
374 }
375 }
376 else
377 {
378 nWideChars = stream->dwSize >> 1;
379 pText = (WCHAR *)stream->buffer;
380 }
381
382 ME_InsertTextFromCursor(editor, 0, pText, nWideChars, style);
383 if (stream->dwSize == 0)
384 break;
385 stream->dwSize = 0;
386 } while(1);
387 return total_bytes_read;
388 }
389
390 static void ME_ApplyBorderProperties(RTF_Info *info,
391 ME_BorderRect *borderRect,
392 RTFBorder *borderDef)
393 {
394 int i, colorNum;
395 ME_Border *pBorders[] = {&borderRect->top,
396 &borderRect->left,
397 &borderRect->bottom,
398 &borderRect->right};
399 for (i = 0; i < 4; i++)
400 {
401 RTFColor *colorDef = info->colorList;
402 pBorders[i]->width = borderDef[i].width;
403 colorNum = borderDef[i].color;
404 while (colorDef && colorDef->rtfCNum != colorNum)
405 colorDef = colorDef->rtfNextColor;
406 if (colorDef)
407 pBorders[i]->colorRef = RGB(
408 colorDef->rtfCRed >= 0 ? colorDef->rtfCRed : 0,
409 colorDef->rtfCGreen >= 0 ? colorDef->rtfCGreen : 0,
410 colorDef->rtfCBlue >= 0 ? colorDef->rtfCBlue : 0);
411 else
412 pBorders[i]->colorRef = RGB(0, 0, 0);
413 }
414 }
415
416 void ME_RTFCharAttrHook(RTF_Info *info)
417 {
418 CHARFORMAT2W fmt;
419 fmt.cbSize = sizeof(fmt);
420 fmt.dwMask = 0;
421 fmt.dwEffects = 0;
422
423 switch(info->rtfMinor)
424 {
425 case rtfPlain:
426 /* FIXME add more flags once they're implemented */
427 fmt.dwMask = CFM_BOLD | CFM_ITALIC | CFM_UNDERLINETYPE | CFM_STRIKEOUT | CFM_COLOR | CFM_BACKCOLOR | CFM_SIZE | CFM_WEIGHT;
428 fmt.dwEffects = CFE_AUTOCOLOR | CFE_AUTOBACKCOLOR;
429 fmt.yHeight = 12*20; /* 12pt */
430 fmt.wWeight = FW_NORMAL;
431 fmt.bUnderlineType = CFU_UNDERLINENONE;
432 break;
433 case rtfBold:
434 fmt.dwMask = CFM_BOLD | CFM_WEIGHT;
435 fmt.dwEffects = info->rtfParam ? CFE_BOLD : 0;
436 fmt.wWeight = info->rtfParam ? FW_BOLD : FW_NORMAL;
437 break;
438 case rtfItalic:
439 fmt.dwMask = CFM_ITALIC;
440 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
441 break;
442 case rtfUnderline:
443 fmt.dwMask = CFM_UNDERLINETYPE;
444 fmt.bUnderlineType = info->rtfParam ? CFU_CF1UNDERLINE : CFU_UNDERLINENONE;
445 break;
446 case rtfDotUnderline:
447 fmt.dwMask = CFM_UNDERLINETYPE;
448 fmt.bUnderlineType = info->rtfParam ? CFU_UNDERLINEDOTTED : CFU_UNDERLINENONE;
449 break;
450 case rtfDbUnderline:
451 fmt.dwMask = CFM_UNDERLINETYPE;
452 fmt.bUnderlineType = info->rtfParam ? CFU_UNDERLINEDOUBLE : CFU_UNDERLINENONE;
453 break;
454 case rtfWordUnderline:
455 fmt.dwMask = CFM_UNDERLINETYPE;
456 fmt.bUnderlineType = info->rtfParam ? CFU_UNDERLINEWORD : CFU_UNDERLINENONE;
457 break;
458 case rtfNoUnderline:
459 fmt.dwMask = CFM_UNDERLINETYPE;
460 fmt.bUnderlineType = CFU_UNDERLINENONE;
461 break;
462 case rtfStrikeThru:
463 fmt.dwMask = CFM_STRIKEOUT;
464 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
465 break;
466 case rtfSubScript:
467 case rtfSuperScript:
468 case rtfSubScrShrink:
469 case rtfSuperScrShrink:
470 case rtfNoSuperSub:
471 fmt.dwMask = CFM_SUBSCRIPT|CFM_SUPERSCRIPT;
472 if (info->rtfMinor == rtfSubScrShrink) fmt.dwEffects = CFE_SUBSCRIPT;
473 if (info->rtfMinor == rtfSuperScrShrink) fmt.dwEffects = CFE_SUPERSCRIPT;
474 if (info->rtfMinor == rtfNoSuperSub) fmt.dwEffects = 0;
475 break;
476 case rtfInvisible:
477 fmt.dwMask = CFM_HIDDEN;
478 fmt.dwEffects = info->rtfParam ? fmt.dwMask : 0;
479 break;
480 case rtfBackColor:
481 fmt.dwMask = CFM_BACKCOLOR;
482 fmt.dwEffects = 0;
483 if (info->rtfParam == 0)
484 fmt.dwEffects = CFE_AUTOBACKCOLOR;
485 else if (info->rtfParam != rtfNoParam)
486 {
487 RTFColor *c = RTFGetColor(info, info->rtfParam);
488 if (c && c->rtfCBlue >= 0)
489 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
490 else
491 fmt.dwEffects = CFE_AUTOBACKCOLOR;
492 }
493 break;
494 case rtfForeColor:
495 fmt.dwMask = CFM_COLOR;
496 fmt.dwEffects = 0;
497 if (info->rtfParam == 0)
498 fmt.dwEffects = CFE_AUTOCOLOR;
499 else if (info->rtfParam != rtfNoParam)
500 {
501 RTFColor *c = RTFGetColor(info, info->rtfParam);
502 if (c && c->rtfCBlue >= 0)
503 fmt.crTextColor = (c->rtfCBlue<<16)|(c->rtfCGreen<<8)|(c->rtfCRed);
504 else {
505 fmt.dwEffects = CFE_AUTOCOLOR;
506 }
507 }
508 break;
509 case rtfFontNum:
510 if (info->rtfParam != rtfNoParam)
511 {
512 RTFFont *f = RTFGetFont(info, info->rtfParam);
513 if (f)
514 {
515 MultiByteToWideChar(CP_ACP, 0, f->rtfFName, -1, fmt.szFaceName, sizeof(fmt.szFaceName)/sizeof(WCHAR));
516 fmt.szFaceName[sizeof(fmt.szFaceName)/sizeof(WCHAR)-1] = '\0';
517 fmt.bCharSet = f->rtfFCharSet;
518 fmt.dwMask = CFM_FACE | CFM_CHARSET;
519 fmt.bPitchAndFamily = f->rtfFPitch | (f->rtfFFamily << 4);
520 }
521 }
522 break;
523 case rtfFontSize:
524 fmt.dwMask = CFM_SIZE;
525 if (info->rtfParam != rtfNoParam)
526 fmt.yHeight = info->rtfParam*10;
527 break;
528 }
529 if (fmt.dwMask) {
530 ME_Style *style2;
531 RTFFlushOutputBuffer(info);
532 /* FIXME too slow ? how come ? */
533 style2 = ME_ApplyStyle(info->style, &fmt);
534 ME_ReleaseStyle(info->style);
535 info->style = style2;
536 info->styleChanged = TRUE;
537 }
538 }
539
540 /* FIXME this function doesn't get any information about context of the RTF tag, which is very bad,
541 the same tags mean different things in different contexts */
542 void ME_RTFParAttrHook(RTF_Info *info)
543 {
544 PARAFORMAT2 fmt;
545 fmt.cbSize = sizeof(fmt);
546 fmt.dwMask = 0;
547
548 switch(info->rtfMinor)
549 {
550 case rtfParDef: /* restores default paragraph attributes */
551 if (!info->editor->bEmulateVersion10) /* v4.1 */
552 info->borderType = RTFBorderParaLeft;
553 else /* v1.0 - 3.0 */
554 info->borderType = RTFBorderParaTop;
555 fmt.dwMask = PFM_ALIGNMENT | PFM_BORDER | PFM_LINESPACING | PFM_TABSTOPS |
556 PFM_OFFSET | PFM_RIGHTINDENT | PFM_SPACEAFTER | PFM_SPACEBEFORE |
557 PFM_STARTINDENT;
558 /* TODO: numbering, shading */
559 fmt.wAlignment = PFA_LEFT;
560 fmt.cTabCount = 0;
561 fmt.dxOffset = fmt.dxStartIndent = fmt.dxRightIndent = 0;
562 fmt.wBorderWidth = fmt.wBorders = 0;
563 fmt.wBorderSpace = 0;
564 fmt.bLineSpacingRule = 0;
565 fmt.dySpaceBefore = fmt.dySpaceAfter = 0;
566 fmt.dyLineSpacing = 0;
567 if (!info->editor->bEmulateVersion10) /* v4.1 */
568 {
569 if (info->tableDef && info->tableDef->tableRowStart &&
570 info->tableDef->tableRowStart->member.para.nFlags & MEPF_ROWEND)
571 {
572 ME_Cursor cursor;
573 ME_DisplayItem *para;
574 /* We are just after a table row. */
575 RTFFlushOutputBuffer(info);
576 cursor = info->editor->pCursors[0];
577 para = cursor.pPara;
578 if (para == info->tableDef->tableRowStart->member.para.next_para
579 && !cursor.nOffset && !cursor.pRun->member.run.nCharOfs)
580 {
581 /* Since the table row end, no text has been inserted, and the \intbl
582 * control word has not be used. We can confirm that we are not in a
583 * table anymore.
584 */
585 info->tableDef->tableRowStart = NULL;
586 info->canInheritInTbl = FALSE;
587 }
588 }
589 } else { /* v1.0 - v3.0 */
590 fmt.dwMask |= PFM_TABLE;
591 fmt.wEffects &= ~PFE_TABLE;
592 }
593 break;
594 case rtfNestLevel:
595 if (!info->editor->bEmulateVersion10) /* v4.1 */
596 {
597 while (info->rtfParam > info->nestingLevel) {
598 RTFTable *tableDef = ALLOC_OBJ(RTFTable);
599 ZeroMemory(tableDef, sizeof(RTFTable));
600 tableDef->parent = info->tableDef;
601 info->tableDef = tableDef;
602
603 RTFFlushOutputBuffer(info);
604 if (tableDef->tableRowStart &&
605 tableDef->tableRowStart->member.para.nFlags & MEPF_ROWEND)
606 {
607 ME_DisplayItem *para = tableDef->tableRowStart;
608 para = para->member.para.next_para;
609 para = ME_InsertTableRowStartAtParagraph(info->editor, para);
610 tableDef->tableRowStart = para;
611 } else {
612 ME_Cursor cursor;
613 WCHAR endl = '\r';
614 cursor = info->editor->pCursors[0];
615 if (cursor.nOffset || cursor.pRun->member.run.nCharOfs)
616 ME_InsertTextFromCursor(info->editor, 0, &endl, 1, info->style);
617 tableDef->tableRowStart = ME_InsertTableRowStartFromCursor(info->editor);
618 }
619
620 info->nestingLevel++;
621 }
622 info->canInheritInTbl = FALSE;
623 }
624 break;
625 case rtfInTable:
626 {
627 if (!info->editor->bEmulateVersion10) /* v4.1 */
628 {
629 if (info->nestingLevel < 1)
630 {
631 RTFTable *tableDef;
632 if (!info->tableDef)
633 {
634 info->tableDef = ALLOC_OBJ(RTFTable);
635 ZeroMemory(info->tableDef, sizeof(RTFTable));
636 }
637 tableDef = info->tableDef;
638 RTFFlushOutputBuffer(info);
639 if (tableDef->tableRowStart &&
640 tableDef->tableRowStart->member.para.nFlags & MEPF_ROWEND)
641 {
642 ME_DisplayItem *para = tableDef->tableRowStart;
643 para = para->member.para.next_para;
644 para = ME_InsertTableRowStartAtParagraph(info->editor, para);
645 tableDef->tableRowStart = para;
646 } else {
647 ME_Cursor cursor;
648 WCHAR endl = '\r';
649 cursor = info->editor->pCursors[0];
650 if (cursor.nOffset || cursor.pRun->member.run.nCharOfs)
651 ME_InsertTextFromCursor(info->editor, 0, &endl, 1, info->style);
652 tableDef->tableRowStart = ME_InsertTableRowStartFromCursor(info->editor);
653 }
654 info->nestingLevel = 1;
655 info->canInheritInTbl = TRUE;
656 }
657 return;
658 } else { /* v1.0 - v3.0 */
659 fmt.dwMask |= PFM_TABLE;
660 fmt.wEffects |= PFE_TABLE;
661 }
662 break;
663 }
664 case rtfFirstIndent:
665 ME_GetSelectionParaFormat(info->editor, &fmt);
666 fmt.dwMask = PFM_STARTINDENT | PFM_OFFSET;
667 fmt.dxStartIndent += fmt.dxOffset + info->rtfParam;
668 fmt.dxOffset = -info->rtfParam;
669 break;
670 case rtfLeftIndent:
671 ME_GetSelectionParaFormat(info->editor, &fmt);
672 fmt.dwMask = PFM_STARTINDENT;
673 fmt.dxStartIndent = info->rtfParam - fmt.dxOffset;
674 break;
675 case rtfRightIndent:
676 fmt.dwMask = PFM_RIGHTINDENT;
677 fmt.dxRightIndent = info->rtfParam;
678 break;
679 case rtfQuadLeft:
680 case rtfQuadJust:
681 fmt.dwMask = PFM_ALIGNMENT;
682 fmt.wAlignment = PFA_LEFT;
683 break;
684 case rtfQuadRight:
685 fmt.dwMask = PFM_ALIGNMENT;
686 fmt.wAlignment = PFA_RIGHT;
687 break;
688 case rtfQuadCenter:
689 fmt.dwMask = PFM_ALIGNMENT;
690 fmt.wAlignment = PFA_CENTER;
691 break;
692 case rtfTabPos:
693 ME_GetSelectionParaFormat(info->editor, &fmt);
694 if (!(fmt.dwMask & PFM_TABSTOPS))
695 {
696 fmt.cTabCount = 0;
697 }
698 if (fmt.cTabCount < MAX_TAB_STOPS && info->rtfParam < 0x1000000)
699 fmt.rgxTabs[fmt.cTabCount++] = info->rtfParam;
700 fmt.dwMask = PFM_TABSTOPS;
701 break;
702 case rtfKeep:
703 fmt.dwMask = PFM_KEEP;
704 fmt.wEffects = PFE_KEEP;
705 break;
706 case rtfNoWidowControl:
707 fmt.dwMask = PFM_NOWIDOWCONTROL;
708 fmt.wEffects = PFE_NOWIDOWCONTROL;
709 break;
710 case rtfKeepNext:
711 fmt.dwMask = PFM_KEEPNEXT;
712 fmt.wEffects = PFE_KEEPNEXT;
713 break;
714 case rtfSpaceAfter:
715 fmt.dwMask = PFM_SPACEAFTER;
716 fmt.dySpaceAfter = info->rtfParam;
717 break;
718 case rtfSpaceBefore:
719 fmt.dwMask = PFM_SPACEBEFORE;
720 fmt.dySpaceBefore = info->rtfParam;
721 break;
722 case rtfSpaceBetween:
723 fmt.dwMask = PFM_LINESPACING;
724 if ((int)info->rtfParam > 0)
725 {
726 fmt.dyLineSpacing = info->rtfParam;
727 fmt.bLineSpacingRule = 3;
728 }
729 else
730 {
731 fmt.dyLineSpacing = info->rtfParam;
732 fmt.bLineSpacingRule = 4;
733 }
734 break;
735 case rtfSpaceMultiply:
736 fmt.dwMask = PFM_LINESPACING;
737 fmt.dyLineSpacing = info->rtfParam * 20;
738 fmt.bLineSpacingRule = 5;
739 break;
740 case rtfParBullet:
741 fmt.dwMask = PFM_NUMBERING;
742 fmt.wNumbering = PFN_BULLET;
743 break;
744 case rtfParSimple:
745 fmt.dwMask = PFM_NUMBERING;
746 fmt.wNumbering = 2; /* FIXME: MSDN says it's not used ?? */
747 break;
748 case rtfParNumDecimal:
749 fmt.dwMask = PFM_NUMBERING;
750 fmt.wNumbering = 2; /* FIXME: MSDN says it's not used ?? */
751 break;
752 case rtfParNumIndent:
753 fmt.dwMask = PFM_NUMBERINGTAB;
754 fmt.wNumberingTab = info->rtfParam;
755 break;
756 case rtfParNumStartAt:
757 fmt.dwMask = PFM_NUMBERINGSTART;
758 fmt.wNumberingStart = info->rtfParam;
759 break;
760 case rtfBorderLeft:
761 info->borderType = RTFBorderParaLeft;
762 ME_GetSelectionParaFormat(info->editor, &fmt);
763 if (!(fmt.dwMask & PFM_BORDER))
764 {
765 fmt.wBorderSpace = 0;
766 fmt.wBorderWidth = 1;
767 fmt.wBorders = 0;
768 }
769 fmt.wBorders |= 1;
770 fmt.dwMask = PFM_BORDER;
771 break;
772 case rtfBorderRight:
773 info->borderType = RTFBorderParaRight;
774 ME_GetSelectionParaFormat(info->editor, &fmt);
775 if (!(fmt.dwMask & PFM_BORDER))
776 {
777 fmt.wBorderSpace = 0;
778 fmt.wBorderWidth = 1;
779 fmt.wBorders = 0;
780 }
781 fmt.wBorders |= 2;
782 fmt.dwMask = PFM_BORDER;
783 break;
784 case rtfBorderTop:
785 info->borderType = RTFBorderParaTop;
786 ME_GetSelectionParaFormat(info->editor, &fmt);
787 if (!(fmt.dwMask & PFM_BORDER))
788 {
789 fmt.wBorderSpace = 0;
790 fmt.wBorderWidth = 1;
791 fmt.wBorders = 0;
792 }
793 fmt.wBorders |= 4;
794 fmt.dwMask = PFM_BORDER;
795 break;
796 case rtfBorderBottom:
797 info->borderType = RTFBorderParaBottom;
798 ME_GetSelectionParaFormat(info->editor, &fmt);
799 if (!(fmt.dwMask & PFM_BORDER))
800 {
801 fmt.wBorderSpace = 0;
802 fmt.wBorderWidth = 1;
803 fmt.wBorders = 0;
804 }
805 fmt.wBorders |= 8;
806 fmt.dwMask = PFM_BORDER;
807 break;
808 case rtfBorderSingle:
809 ME_GetSelectionParaFormat(info->editor, &fmt);
810 /* we assume that borders have been created before (RTF spec) */
811 fmt.wBorders &= ~0x700;
812 fmt.wBorders |= 1 << 8;
813 fmt.dwMask = PFM_BORDER;
814 break;
815 case rtfBorderThick:
816 ME_GetSelectionParaFormat(info->editor, &fmt);
817 /* we assume that borders have been created before (RTF spec) */
818 fmt.wBorders &= ~0x700;
819 fmt.wBorders |= 2 << 8;
820 fmt.dwMask = PFM_BORDER;
821 break;
822 case rtfBorderShadow:
823 ME_GetSelectionParaFormat(info->editor, &fmt);
824 /* we assume that borders have been created before (RTF spec) */
825 fmt.wBorders &= ~0x700;
826 fmt.wBorders |= 10 << 8;
827 fmt.dwMask = PFM_BORDER;
828 break;
829 case rtfBorderDouble:
830 ME_GetSelectionParaFormat(info->editor, &fmt);
831 /* we assume that borders have been created before (RTF spec) */
832 fmt.wBorders &= ~0x700;
833 fmt.wBorders |= 7 << 8;
834 fmt.dwMask = PFM_BORDER;
835 break;
836 case rtfBorderDot:
837 ME_GetSelectionParaFormat(info->editor, &fmt);
838 /* we assume that borders have been created before (RTF spec) */
839 fmt.wBorders &= ~0x700;
840 fmt.wBorders |= 11 << 8;
841 fmt.dwMask = PFM_BORDER;
842 break;
843 case rtfBorderWidth:
844 {
845 int borderSide = info->borderType & RTFBorderSideMask;
846 RTFTable *tableDef = info->tableDef;
847 ME_GetSelectionParaFormat(info->editor, &fmt);
848 /* we assume that borders have been created before (RTF spec) */
849 fmt.wBorderWidth |= ((info->rtfParam / 15) & 7) << 8;
850 if ((info->borderType & RTFBorderTypeMask) == RTFBorderTypeCell)
851 {
852 RTFBorder *border;
853 if (!tableDef || tableDef->numCellsDefined >= MAX_TABLE_CELLS)
854 break;
855 border = &tableDef->cells[tableDef->numCellsDefined].border[borderSide];
856 border->width = info->rtfParam;
857 break;
858 }
859 fmt.dwMask = PFM_BORDER;
860 break;
861 }
862 case rtfBorderSpace:
863 ME_GetSelectionParaFormat(info->editor, &fmt);
864 /* we assume that borders have been created before (RTF spec) */
865 fmt.wBorderSpace = info->rtfParam;
866 fmt.dwMask = PFM_BORDER;
867 break;
868 case rtfBorderColor:
869 {
870 RTFTable *tableDef = info->tableDef;
871 int borderSide = info->borderType & RTFBorderSideMask;
872 int borderType = info->borderType & RTFBorderTypeMask;
873 switch(borderType)
874 {
875 case RTFBorderTypePara:
876 if (!info->editor->bEmulateVersion10) /* v4.1 */
877 break;
878 /* v1.0 - 3.0 treat paragraph and row borders the same. */
879 case RTFBorderTypeRow:
880 if (tableDef) {
881 tableDef->border[borderSide].color = info->rtfParam;
882 }
883 break;
884 case RTFBorderTypeCell:
885 if (tableDef && tableDef->numCellsDefined < MAX_TABLE_CELLS) {
886 tableDef->cells[tableDef->numCellsDefined].border[borderSide].color = info->rtfParam;
887 }
888 break;
889 }
890 break;
891 }
892 case rtfRTLPar:
893 fmt.dwMask = PFM_RTLPARA;
894 fmt.wEffects = PFE_RTLPARA;
895 break;
896 case rtfLTRPar:
897 fmt.dwMask = PFM_RTLPARA;
898 fmt.wEffects = 0;
899 break;
900 }
901 if (fmt.dwMask) {
902 RTFFlushOutputBuffer(info);
903 /* FIXME too slow ? how come ?*/
904 ME_SetSelectionParaFormat(info->editor, &fmt);
905 }
906 }
907
908 void ME_RTFTblAttrHook(RTF_Info *info)
909 {
910 switch (info->rtfMinor)
911 {
912 case rtfRowDef:
913 {
914 if (!info->editor->bEmulateVersion10) /* v4.1 */
915 info->borderType = 0; /* Not sure */
916 else /* v1.0 - 3.0 */
917 info->borderType = RTFBorderRowTop;
918 if (!info->tableDef) {
919 info->tableDef = ME_MakeTableDef(info->editor);
920 } else {
921 ME_InitTableDef(info->editor, info->tableDef);
922 }
923 break;
924 }
925 case rtfCellPos:
926 {
927 int cellNum;
928 if (!info->tableDef)
929 {
930 info->tableDef = ME_MakeTableDef(info->editor);
931 }
932 cellNum = info->tableDef->numCellsDefined;
933 if (cellNum >= MAX_TABLE_CELLS)
934 break;
935 info->tableDef->cells[cellNum].rightBoundary = info->rtfParam;
936 if (cellNum < MAX_TAB_STOPS) {
937 /* Tab stops were used to store cell positions before v4.1 but v4.1
938 * still seems to set the tabstops without using them. */
939 ME_DisplayItem *para = info->editor->pCursors[0].pPara;
940 PARAFORMAT2 *pFmt = para->member.para.pFmt;
941 pFmt->rgxTabs[cellNum] &= ~0x00FFFFFF;
942 pFmt->rgxTabs[cellNum] = 0x00FFFFFF & info->rtfParam;
943 }
944 info->tableDef->numCellsDefined++;
945 break;
946 }
947 case rtfRowBordTop:
948 info->borderType = RTFBorderRowTop;
949 break;
950 case rtfRowBordLeft:
951 info->borderType = RTFBorderRowLeft;
952 break;
953 case rtfRowBordBottom:
954 info->borderType = RTFBorderRowBottom;
955 break;
956 case rtfRowBordRight:
957 info->borderType = RTFBorderRowRight;
958 break;
959 case rtfCellBordTop:
960 info->borderType = RTFBorderCellTop;
961 break;
962 case rtfCellBordLeft:
963 info->borderType = RTFBorderCellLeft;
964 break;
965 case rtfCellBordBottom:
966 info->borderType = RTFBorderCellBottom;
967 break;
968 case rtfCellBordRight:
969 info->borderType = RTFBorderCellRight;
970 break;
971 case rtfRowGapH:
972 if (info->tableDef)
973 info->tableDef->gapH = info->rtfParam;
974 break;
975 case rtfRowLeftEdge:
976 if (info->tableDef)
977 info->tableDef->leftEdge = info->rtfParam;
978 break;
979 }
980 }
981
982 void ME_RTFSpecialCharHook(RTF_Info *info)
983 {
984 RTFTable *tableDef = info->tableDef;
985 switch (info->rtfMinor)
986 {
987 case rtfNestCell:
988 if (info->editor->bEmulateVersion10) /* v1.0 - v3.0 */
989 break;
990 /* else fall through since v4.1 treats rtfNestCell and rtfCell the same */
991 case rtfCell:
992 if (!tableDef)
993 break;
994 RTFFlushOutputBuffer(info);
995 if (!info->editor->bEmulateVersion10) { /* v4.1 */
996 if (tableDef->tableRowStart)
997 {
998 if (!info->nestingLevel &&
999 tableDef->tableRowStart->member.para.nFlags & MEPF_ROWEND)
1000 {
1001 ME_DisplayItem *para = tableDef->tableRowStart;
1002 para = para->member.para.next_para;
1003 para = ME_InsertTableRowStartAtParagraph(info->editor, para);
1004 tableDef->tableRowStart = para;
1005 info->nestingLevel = 1;
1006 }
1007 ME_InsertTableCellFromCursor(info->editor);
1008 }
1009 } else { /* v1.0 - v3.0 */
1010 ME_DisplayItem *para = info->editor->pCursors[0].pPara;
1011 PARAFORMAT2 *pFmt = para->member.para.pFmt;
1012 if (pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE &&
1013 tableDef->numCellsInserted < tableDef->numCellsDefined)
1014 {
1015 WCHAR tab = '\t';
1016 ME_InsertTextFromCursor(info->editor, 0, &tab, 1, info->style);
1017 tableDef->numCellsInserted++;
1018 }
1019 }
1020 break;
1021 case rtfNestRow:
1022 if (info->editor->bEmulateVersion10) /* v1.0 - v3.0 */
1023 break;
1024 /* else fall through since v4.1 treats rtfNestRow and rtfRow the same */
1025 case rtfRow:
1026 {
1027 ME_DisplayItem *para, *cell, *run;
1028 int i;
1029
1030 if (!tableDef)
1031 break;
1032 RTFFlushOutputBuffer(info);
1033 if (!info->editor->bEmulateVersion10) { /* v4.1 */
1034 if (!tableDef->tableRowStart)
1035 break;
1036 if (!info->nestingLevel &&
1037 tableDef->tableRowStart->member.para.nFlags & MEPF_ROWEND)
1038 {
1039 para = tableDef->tableRowStart;
1040 para = para->member.para.next_para;
1041 para = ME_InsertTableRowStartAtParagraph(info->editor, para);
1042 tableDef->tableRowStart = para;
1043 info->nestingLevel++;
1044 }
1045 para = tableDef->tableRowStart;
1046 cell = ME_FindItemFwd(para, diCell);
1047 assert(cell && !cell->member.cell.prev_cell);
1048 if (tableDef->numCellsDefined < 1)
1049 {
1050 /* 2000 twips appears to be the cell size that native richedit uses
1051 * when no cell sizes are specified. */
1052 const int defaultCellSize = 2000;
1053 int nRightBoundary = defaultCellSize;
1054 cell->member.cell.nRightBoundary = nRightBoundary;
1055 while (cell->member.cell.next_cell) {
1056 cell = cell->member.cell.next_cell;
1057 nRightBoundary += defaultCellSize;
1058 cell->member.cell.nRightBoundary = nRightBoundary;
1059 }
1060 para = ME_InsertTableCellFromCursor(info->editor);
1061 cell = para->member.para.pCell;
1062 cell->member.cell.nRightBoundary = nRightBoundary;
1063 } else {
1064 for (i = 0; i < tableDef->numCellsDefined; i++)
1065 {
1066 RTFCell *cellDef = &tableDef->cells[i];
1067 cell->member.cell.nRightBoundary = cellDef->rightBoundary;
1068 ME_ApplyBorderProperties(info, &cell->member.cell.border,
1069 cellDef->border);
1070 cell = cell->member.cell.next_cell;
1071 if (!cell)
1072 {
1073 para = ME_InsertTableCellFromCursor(info->editor);
1074 cell = para->member.para.pCell;
1075 }
1076 }
1077 /* Cell for table row delimiter is empty */
1078 cell->member.cell.nRightBoundary = tableDef->cells[i-1].rightBoundary;
1079 }
1080
1081 run = ME_FindItemFwd(cell, diRun);
1082 if (info->editor->pCursors[0].pRun != run ||
1083 info->editor->pCursors[0].nOffset)
1084 {
1085 int nOfs, nChars;
1086 /* Delete inserted cells that aren't defined. */
1087 info->editor->pCursors[1].pRun = run;
1088 info->editor->pCursors[1].pPara = ME_GetParagraph(run);
1089 info->editor->pCursors[1].nOffset = 0;
1090 nOfs = ME_GetCursorOfs(&info->editor->pCursors[1]);
1091 nChars = ME_GetCursorOfs(&info->editor->pCursors[0]) - nOfs;
1092 ME_InternalDeleteText(info->editor, &info->editor->pCursors[1],
1093 nChars, TRUE);
1094 }
1095
1096 para = ME_InsertTableRowEndFromCursor(info->editor);
1097 para->member.para.pFmt->dxOffset = abs(info->tableDef->gapH);
1098 para->member.para.pFmt->dxStartIndent = info->tableDef->leftEdge;
1099 ME_ApplyBorderProperties(info, &para->member.para.border,
1100 tableDef->border);
1101 info->nestingLevel--;
1102 if (!info->nestingLevel)
1103 {
1104 if (info->canInheritInTbl) {
1105 tableDef->tableRowStart = para;
1106 } else {
1107 while (info->tableDef) {
1108 tableDef = info->tableDef;
1109 info->tableDef = tableDef->parent;
1110 heap_free(tableDef);
1111 }
1112 }
1113 } else {
1114 info->tableDef = tableDef->parent;
1115 heap_free(tableDef);
1116 }
1117 } else { /* v1.0 - v3.0 */
1118 WCHAR endl = '\r';
1119 ME_DisplayItem *para = info->editor->pCursors[0].pPara;
1120 PARAFORMAT2 *pFmt = para->member.para.pFmt;
1121 pFmt->dxOffset = info->tableDef->gapH;
1122 pFmt->dxStartIndent = info->tableDef->leftEdge;
1123
1124 ME_ApplyBorderProperties(info, &para->member.para.border,
1125 tableDef->border);
1126 while (tableDef->numCellsInserted < tableDef->numCellsDefined)
1127 {
1128 WCHAR tab = '\t';
1129 ME_InsertTextFromCursor(info->editor, 0, &tab, 1, info->style);
1130 tableDef->numCellsInserted++;
1131 }
1132 pFmt->cTabCount = min(tableDef->numCellsDefined, MAX_TAB_STOPS);
1133 if (!tableDef->numCellsDefined)
1134 pFmt->wEffects &= ~PFE_TABLE;
1135 ME_InsertTextFromCursor(info->editor, 0, &endl, 1, info->style);
1136 tableDef->numCellsInserted = 0;
1137 }
1138 break;
1139 }
1140 case rtfTab:
1141 case rtfPar:
1142 if (info->editor->bEmulateVersion10) { /* v1.0 - 3.0 */
1143 ME_DisplayItem *para;
1144 PARAFORMAT2 *pFmt;
1145 RTFFlushOutputBuffer(info);
1146 para = info->editor->pCursors[0].pPara;
1147 pFmt = para->member.para.pFmt;
1148 if (pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE)
1149 {
1150 /* rtfPar is treated like a space within a table. */
1151 info->rtfClass = rtfText;
1152 info->rtfMajor = ' ';
1153 }
1154 else if (info->rtfMinor == rtfPar && tableDef)
1155 tableDef->numCellsInserted = 0;
1156 }
1157 break;
1158 }
1159 }
1160
1161 static BOOL ME_RTFInsertOleObject(RTF_Info *info, HENHMETAFILE hemf, HBITMAP hbmp,
1162 const SIZEL* sz)
1163 {
1164 LPOLEOBJECT lpObject = NULL;
1165 LPSTORAGE lpStorage = NULL;
1166 LPOLECLIENTSITE lpClientSite = NULL;
1167 LPDATAOBJECT lpDataObject = NULL;
1168 LPOLECACHE lpOleCache = NULL;
1169 STGMEDIUM stgm;
1170 FORMATETC fm;
1171 CLSID clsid;
1172 BOOL ret = FALSE;
1173 DWORD conn;
1174
1175 if (hemf)
1176 {
1177 stgm.tymed = TYMED_ENHMF;
1178 stgm.u.hEnhMetaFile = hemf;
1179 fm.cfFormat = CF_ENHMETAFILE;
1180 }
1181 else if (hbmp)
1182 {
1183 stgm.tymed = TYMED_GDI;
1184 stgm.u.hBitmap = hbmp;
1185 fm.cfFormat = CF_BITMAP;
1186 }
1187 stgm.pUnkForRelease = NULL;
1188
1189 fm.ptd = NULL;
1190 fm.dwAspect = DVASPECT_CONTENT;
1191 fm.lindex = -1;
1192 fm.tymed = stgm.tymed;
1193
1194 if (!info->lpRichEditOle)
1195 {
1196 CreateIRichEditOle(NULL, info->editor, (VOID**)&info->lpRichEditOle);
1197 }
1198
1199 if (OleCreateDefaultHandler(&CLSID_NULL, NULL, &IID_IOleObject, (void**)&lpObject) == S_OK &&
1200 IRichEditOle_GetClientSite(info->lpRichEditOle, &lpClientSite) == S_OK &&
1201 IOleObject_SetClientSite(lpObject, lpClientSite) == S_OK &&
1202 IOleObject_GetUserClassID(lpObject, &clsid) == S_OK &&
1203 IOleObject_QueryInterface(lpObject, &IID_IOleCache, (void**)&lpOleCache) == S_OK &&
1204 IOleCache_Cache(lpOleCache, &fm, 0, &conn) == S_OK &&
1205 IOleObject_QueryInterface(lpObject, &IID_IDataObject, (void**)&lpDataObject) == S_OK &&
1206 IDataObject_SetData(lpDataObject, &fm, &stgm, TRUE) == S_OK)
1207 {
1208 REOBJECT reobject;
1209
1210 reobject.cbStruct = sizeof(reobject);
1211 reobject.cp = REO_CP_SELECTION;
1212 reobject.clsid = clsid;
1213 reobject.poleobj = lpObject;
1214 reobject.pstg = lpStorage;
1215 reobject.polesite = lpClientSite;
1216 /* convert from twips to .01 mm */
1217 reobject.sizel.cx = MulDiv(sz->cx, 254, 144);
1218 reobject.sizel.cy = MulDiv(sz->cy, 254, 144);
1219 reobject.dvaspect = DVASPECT_CONTENT;
1220 reobject.dwFlags = 0; /* FIXME */
1221 reobject.dwUser = 0;
1222
1223 ME_InsertOLEFromCursor(info->editor, &reobject, 0);
1224 ret = TRUE;
1225 }
1226
1227 if (lpObject) IOleObject_Release(lpObject);
1228 if (lpClientSite) IOleClientSite_Release(lpClientSite);
1229 if (lpStorage) IStorage_Release(lpStorage);
1230 if (lpDataObject) IDataObject_Release(lpDataObject);
1231 if (lpOleCache) IOleCache_Release(lpOleCache);
1232
1233 return ret;
1234 }
1235
1236 static void ME_RTFReadPictGroup(RTF_Info *info)
1237 {
1238 SIZEL sz;
1239 BYTE* buffer = NULL;
1240 unsigned bufsz, bufidx;
1241 BOOL flip;
1242 BYTE val;
1243 METAFILEPICT mfp;
1244 HENHMETAFILE hemf;
1245 HBITMAP hbmp;
1246 enum gfxkind {gfx_unknown = 0, gfx_enhmetafile, gfx_metafile, gfx_dib} gfx = gfx_unknown;
1247
1248 RTFGetToken (info);
1249 if (info->rtfClass == rtfEOF)
1250 return;
1251 mfp.mm = MM_TEXT;
1252 /* fetch picture type */
1253 if (RTFCheckMM (info, rtfPictAttr, rtfWinMetafile))
1254 {
1255 mfp.mm = info->rtfParam;
1256 gfx = gfx_metafile;
1257 }
1258 else if (RTFCheckMM (info, rtfPictAttr, rtfDevIndBitmap))
1259 {
1260 if (info->rtfParam != 0) FIXME("dibitmap should be 0 (%d)\n", info->rtfParam);
1261 gfx = gfx_dib;
1262 }
1263 else if (RTFCheckMM (info, rtfPictAttr, rtfEmfBlip))
1264 {
1265 gfx = gfx_enhmetafile;
1266 }
1267 else
1268 {
1269 FIXME("%d %d\n", info->rtfMajor, info->rtfMinor);
1270 goto skip_group;
1271 }
1272 sz.cx = sz.cy = 0;
1273 /* fetch picture attributes */
1274 for (;;)
1275 {
1276 RTFGetToken (info);
1277 if (info->rtfClass == rtfEOF)
1278 return;
1279 if (info->rtfClass == rtfText)
1280 break;
1281 if (!RTFCheckCM (info, rtfControl, rtfPictAttr))
1282 {
1283 ERR("Expected picture attribute (%d %d)\n",
1284 info->rtfClass, info->rtfMajor);
1285 goto skip_group;
1286 }
1287 else if (RTFCheckMM (info, rtfPictAttr, rtfPicWid))
1288 {
1289 if (gfx == gfx_metafile) mfp.xExt = info->rtfParam;
1290 }
1291 else if (RTFCheckMM (info, rtfPictAttr, rtfPicHt))
1292 {
1293 if (gfx == gfx_metafile) mfp.yExt = info->rtfParam;
1294 }
1295 else if (RTFCheckMM (info, rtfPictAttr, rtfPicGoalWid))
1296 sz.cx = info->rtfParam;
1297 else if (RTFCheckMM (info, rtfPictAttr, rtfPicGoalHt))
1298 sz.cy = info->rtfParam;
1299 else
1300 FIXME("Non supported attribute: %d %d %d\n", info->rtfClass, info->rtfMajor, info->rtfMinor);
1301 }
1302 /* fetch picture data */
1303 bufsz = 1024;
1304 bufidx = 0;
1305 buffer = HeapAlloc(GetProcessHeap(), 0, bufsz);
1306 val = info->rtfMajor;
1307 for (flip = TRUE;; flip = !flip)
1308 {
1309 RTFGetToken (info);
1310 if (info->rtfClass == rtfEOF)
1311 {
1312 HeapFree(GetProcessHeap(), 0, buffer);
1313 return; /* Warn ?? */
1314 }
1315 if (RTFCheckCM(info, rtfGroup, rtfEndGroup))
1316 break;
1317 if (info->rtfClass != rtfText) goto skip_group;
1318 if (flip)
1319 {
1320 if (bufidx >= bufsz &&
1321 !(buffer = HeapReAlloc(GetProcessHeap(), 0, buffer, bufsz += 1024)))
1322 goto skip_group;
1323 buffer[bufidx++] = RTFCharToHex(val) * 16 + RTFCharToHex(info->rtfMajor);
1324 }
1325 else
1326 val = info->rtfMajor;
1327 }
1328 if (flip) FIXME("wrong hex string\n");
1329
1330 switch (gfx)
1331 {
1332 case gfx_enhmetafile:
1333 if ((hemf = SetEnhMetaFileBits(bufidx, buffer)))
1334 ME_RTFInsertOleObject(info, hemf, NULL, &sz);
1335 break;
1336 case gfx_metafile:
1337 if ((hemf = SetWinMetaFileBits(bufidx, buffer, NULL, &mfp)))
1338 ME_RTFInsertOleObject(info, hemf, NULL, &sz);
1339 break;
1340 case gfx_dib:
1341 {
1342 BITMAPINFO* bi = (BITMAPINFO*)buffer;
1343 HDC hdc = GetDC(0);
1344 unsigned nc = bi->bmiHeader.biClrUsed;
1345
1346 /* not quite right, especially for bitfields type of compression */
1347 if (!nc && bi->bmiHeader.biBitCount <= 8)
1348 nc = 1 << bi->bmiHeader.biBitCount;
1349 if ((hbmp = CreateDIBitmap(hdc, &bi->bmiHeader,
1350 CBM_INIT, (char*)(bi + 1) + nc * sizeof(RGBQUAD),
1351 bi, DIB_RGB_COLORS)))
1352 ME_RTFInsertOleObject(info, NULL, hbmp, &sz);
1353 ReleaseDC(0, hdc);
1354 }
1355 break;
1356 default:
1357 break;
1358 }
1359 HeapFree(GetProcessHeap(), 0, buffer);
1360 RTFRouteToken (info); /* feed "}" back to router */
1361 return;
1362 skip_group:
1363 HeapFree(GetProcessHeap(), 0, buffer);
1364 RTFSkipGroup(info);
1365 RTFRouteToken(info); /* feed "}" back to router */
1366 }
1367
1368 /* for now, lookup the \result part and use it, whatever the object */
1369 static void ME_RTFReadObjectGroup(RTF_Info *info)
1370 {
1371 for (;;)
1372 {
1373 RTFGetToken (info);
1374 if (info->rtfClass == rtfEOF)
1375 return;
1376 if (RTFCheckCM(info, rtfGroup, rtfEndGroup))
1377 break;
1378 if (RTFCheckCM(info, rtfGroup, rtfBeginGroup))
1379 {
1380 RTFGetToken (info);
1381 if (info->rtfClass == rtfEOF)
1382 return;
1383 if (RTFCheckCMM(info, rtfControl, rtfDestination, rtfObjResult))
1384 {
1385 int level = 1;
1386
1387 while (RTFGetToken (info) != rtfEOF)
1388 {
1389 if (info->rtfClass == rtfGroup)
1390 {
1391 if (info->rtfMajor == rtfBeginGroup) level++;
1392 else if (info->rtfMajor == rtfEndGroup && --level < 0) break;
1393 }
1394 RTFRouteToken(info);
1395 }
1396 }
1397 else RTFSkipGroup(info);
1398 continue;
1399 }
1400 if (!RTFCheckCM (info, rtfControl, rtfObjAttr))
1401 {
1402 FIXME("Non supported attribute: %d %d %d\n", info->rtfClass, info->rtfMajor, info->rtfMinor);
1403 return;
1404 }
1405 }
1406 RTFRouteToken(info); /* feed "}" back to router */
1407 }
1408
1409 static void ME_RTFReadHook(RTF_Info *info)
1410 {
1411 switch(info->rtfClass)
1412 {
1413 case rtfGroup:
1414 switch(info->rtfMajor)
1415 {
1416 case rtfBeginGroup:
1417 if (info->stackTop < maxStack) {
1418 info->stack[info->stackTop].style = info->style;
1419 ME_AddRefStyle(info->style);
1420 info->stack[info->stackTop].codePage = info->codePage;
1421 info->stack[info->stackTop].unicodeLength = info->unicodeLength;
1422 }
1423 info->stackTop++;
1424 info->styleChanged = FALSE;
1425 break;
1426 case rtfEndGroup:
1427 {
1428 RTFFlushOutputBuffer(info);
1429 info->stackTop--;
1430 if (info->stackTop <= 0)
1431 info->rtfClass = rtfEOF;
1432 if (info->stackTop < 0)
1433 return;
1434
1435 ME_ReleaseStyle(info->style);
1436 info->style = info->stack[info->stackTop].style;
1437 info->codePage = info->stack[info->stackTop].codePage;
1438 info->unicodeLength = info->stack[info->stackTop].unicodeLength;
1439 break;
1440 }
1441 }
1442 break;
1443 }
1444 }
1445
1446 void
1447 ME_StreamInFill(ME_InStream *stream)
1448 {
1449 stream->editstream->dwError = stream->editstream->pfnCallback(stream->editstream->dwCookie,
1450 (BYTE *)stream->buffer,
1451 sizeof(stream->buffer),
1452 (LONG *)&stream->dwSize);
1453 stream->dwUsed = 0;
1454 }
1455
1456 static LRESULT ME_StreamIn(ME_TextEditor *editor, DWORD format, EDITSTREAM *stream, BOOL stripLastCR)
1457 {
1458 RTF_Info parser;
1459 ME_Style *style;
1460 int from, to, nUndoMode;
1461 int nEventMask = editor->nEventMask;
1462 ME_InStream inStream;
1463 BOOL invalidRTF = FALSE;
1464 ME_Cursor *selStart, *selEnd;
1465 LRESULT num_read = 0; /* bytes read for SF_TEXT, non-control chars inserted for SF_RTF */
1466
1467 TRACE("stream==%p editor==%p format==0x%X\n", stream, editor, format);
1468 editor->nEventMask = 0;
1469
1470 ME_GetSelectionOfs(editor, &from, &to);
1471 if (format & SFF_SELECTION && editor->mode & TM_RICHTEXT)
1472 {
1473 ME_GetSelection(editor, &selStart, &selEnd);
1474 style = ME_GetSelectionInsertStyle(editor);
1475
1476 ME_InternalDeleteText(editor, selStart, to - from, FALSE);
1477
1478 /* Don't insert text at the end of the table row */
1479 if (!editor->bEmulateVersion10) { /* v4.1 */
1480 ME_DisplayItem *para = editor->pCursors->pPara;
1481 if (para->member.para.nFlags & MEPF_ROWEND)
1482 {
1483 para = para->member.para.next_para;
1484 editor->pCursors[0].pPara = para;
1485 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
1486 editor->pCursors[0].nOffset = 0;
1487 }
1488 if (para->member.para.nFlags & MEPF_ROWSTART)
1489 {
1490 para = para->member.para.next_para;
1491 editor->pCursors[0].pPara = para;
1492 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
1493 editor->pCursors[0].nOffset = 0;
1494 }
1495 editor->pCursors[1] = editor->pCursors[0];
1496 } else { /* v1.0 - 3.0 */
1497 if (editor->pCursors[0].pRun->member.run.nFlags & MERF_ENDPARA &&
1498 ME_IsInTable(editor->pCursors[0].pRun))
1499 return 0;
1500 }
1501 } else {
1502 style = editor->pBuffer->pDefaultStyle;
1503 ME_AddRefStyle(style);
1504 ME_SetSelection(editor, 0, 0);
1505 ME_InternalDeleteText(editor, &editor->pCursors[1],
1506 ME_GetTextLength(editor), FALSE);
1507 from = to = 0;
1508 ME_ClearTempStyle(editor);
1509 ME_SetDefaultParaFormat(editor->pCursors[0].pPara->member.para.pFmt);
1510 }
1511
1512
1513 /* Back up undo mode to a local variable */
1514 nUndoMode = editor->nUndoMode;
1515
1516 /* Only create an undo if SFF_SELECTION is set */
1517 if (!(format & SFF_SELECTION))
1518 editor->nUndoMode = umIgnore;
1519
1520 inStream.editstream = stream;
1521 inStream.editstream->dwError = 0;
1522 inStream.dwSize = 0;
1523 inStream.dwUsed = 0;
1524
1525 if (format & SF_RTF)
1526 {
1527 /* Check if it's really RTF, and if it is not, use plain text */
1528 ME_StreamInFill(&inStream);
1529 if (!inStream.editstream->dwError)
1530 {
1531 if ((!editor->bEmulateVersion10 && strncmp(inStream.buffer, "{\\rtf", 5) && strncmp(inStream.buffer, "{\\urtf", 6))
1532 || (editor->bEmulateVersion10 && *inStream.buffer != '{'))
1533 {
1534 invalidRTF = TRUE;
1535 inStream.editstream->dwError = -16;
1536 }
1537 }
1538 }
1539
1540 if (!invalidRTF && !inStream.editstream->dwError)
1541 {
1542 if (format & SF_RTF) {
1543 from = ME_GetCursorOfs(&editor->pCursors[0]);
1544
1545 /* setup the RTF parser */
1546 memset(&parser, 0, sizeof parser);
1547 RTFSetEditStream(&parser, &inStream);
1548 parser.rtfFormat = format&(SF_TEXT|SF_RTF);
1549 parser.editor = editor;
1550 parser.style = style;
1551 WriterInit(&parser);
1552 RTFInit(&parser);
1553 RTFSetReadHook(&parser, ME_RTFReadHook);
1554 RTFSetDestinationCallback(&parser, rtfPict, ME_RTFReadPictGroup);
1555 RTFSetDestinationCallback(&parser, rtfObject, ME_RTFReadObjectGroup);
1556 if (!parser.editor->bEmulateVersion10) /* v4.1 */
1557 {
1558 RTFSetDestinationCallback(&parser, rtfNoNestTables, RTFSkipGroup);
1559 RTFSetDestinationCallback(&parser, rtfNestTableProps, RTFReadGroup);
1560 }
1561 BeginFile(&parser);
1562
1563 /* do the parsing */
1564 RTFRead(&parser);
1565 RTFFlushOutputBuffer(&parser);
1566 if (!editor->bEmulateVersion10) { /* v4.1 */
1567 if (parser.tableDef && parser.tableDef->tableRowStart &&
1568 (parser.nestingLevel > 0 || parser.canInheritInTbl))
1569 {
1570 /* Delete any incomplete table row at the end of the rich text. */
1571 int nOfs, nChars;
1572 ME_DisplayItem *para;
1573
1574 parser.rtfMinor = rtfRow;
1575 /* Complete the table row before deleting it.
1576 * By doing it this way we will have the current paragraph format set
1577 * properly to reflect that is not in the complete table, and undo items
1578 * will be added for this change to the current paragraph format. */
1579 if (parser.nestingLevel > 0)
1580 {
1581 while (parser.nestingLevel > 1)
1582 ME_RTFSpecialCharHook(&parser); /* Decrements nestingLevel */
1583 para = parser.tableDef->tableRowStart;
1584 ME_RTFSpecialCharHook(&parser);
1585 } else {
1586 para = parser.tableDef->tableRowStart;
1587 ME_RTFSpecialCharHook(&parser);
1588 assert(para->member.para.nFlags & MEPF_ROWEND);
1589 para = para->member.para.next_para;
1590 }
1591
1592 editor->pCursors[1].pPara = para;
1593 editor->pCursors[1].pRun = ME_FindItemFwd(para, diRun);
1594 editor->pCursors[1].nOffset = 0;
1595 nOfs = ME_GetCursorOfs(&editor->pCursors[1]);
1596 nChars = ME_GetCursorOfs(&editor->pCursors[0]) - nOfs;
1597 ME_InternalDeleteText(editor, &editor->pCursors[1], nChars, TRUE);
1598 if (parser.tableDef)
1599 parser.tableDef->tableRowStart = NULL;
1600 }
1601 }
1602 ME_CheckTablesForCorruption(editor);
1603 RTFDestroy(&parser);
1604 if (parser.lpRichEditOle)
1605 IRichEditOle_Release(parser.lpRichEditOle);
1606
1607 if (parser.stackTop > 0)
1608 {
1609 while (--parser.stackTop >= 0)
1610 {
1611 ME_ReleaseStyle(parser.style);
1612 parser.style = parser.stack[parser.stackTop].style;
1613 }
1614 if (!inStream.editstream->dwError)
1615 inStream.editstream->dwError = HRESULT_FROM_WIN32(ERROR_HANDLE_EOF);
1616 }
1617
1618 /* Remove last line break, as mandated by tests. This is not affected by
1619 CR/LF counters, since RTF streaming presents only \para tokens, which
1620 are converted according to the standard rules: \r for 2.0, \r\n for 1.0
1621 */
1622 if (stripLastCR && !(format & SFF_SELECTION)) {
1623 int newto;
1624 ME_GetSelection(editor, &selStart, &selEnd);
1625 newto = ME_GetCursorOfs(selEnd);
1626 if (newto > to + (editor->bEmulateVersion10 ? 1 : 0)) {
1627 WCHAR lastchar[3] = {'\0', '\0'};
1628 int linebreakSize = editor->bEmulateVersion10 ? 2 : 1;
1629 ME_Cursor linebreakCursor = *selEnd;
1630
1631 ME_MoveCursorChars(editor, &linebreakCursor, -linebreakSize);
1632 ME_GetTextW(editor, lastchar, 2, &linebreakCursor, linebreakSize, FALSE, FALSE);
1633 if (lastchar[0] == '\r' && (lastchar[1] == '\n' || lastchar[1] == '\0')) {
1634 ME_InternalDeleteText(editor, &linebreakCursor, linebreakSize, FALSE);
1635 }
1636 }
1637 }
1638 to = ME_GetCursorOfs(&editor->pCursors[0]);
1639 num_read = to - from;
1640
1641 style = parser.style;
1642 }
1643 else if (format & SF_TEXT)
1644 num_read = ME_StreamInText(editor, format, &inStream, style);
1645 else
1646 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
1647 /* put the cursor at the top */
1648 if (!(format & SFF_SELECTION))
1649 ME_SetSelection(editor, 0, 0);
1650 }
1651
1652 /* Restore saved undo mode */
1653 editor->nUndoMode = nUndoMode;
1654
1655 /* even if we didn't add an undo, we need to commit anything on the stack */
1656 ME_CommitUndo(editor);
1657
1658 /* If SFF_SELECTION isn't set, delete any undos from before we started too */
1659 if (!(format & SFF_SELECTION))
1660 ME_EmptyUndoStack(editor);
1661
1662 ME_ReleaseStyle(style);
1663 editor->nEventMask = nEventMask;
1664 ME_UpdateRepaint(editor, FALSE);
1665 if (!(format & SFF_SELECTION)) {
1666 ME_ClearTempStyle(editor);
1667 }
1668 ITextHost_TxShowCaret(editor->texthost, FALSE);
1669 ME_MoveCaret(editor);
1670 ITextHost_TxShowCaret(editor->texthost, TRUE);
1671 ME_SendSelChange(editor);
1672 ME_SendRequestResize(editor, FALSE);
1673
1674 return num_read;
1675 }
1676
1677
1678 typedef struct tagME_RTFStringStreamStruct
1679 {
1680 char *string;
1681 int pos;
1682 int length;
1683 } ME_RTFStringStreamStruct;
1684
1685 static DWORD CALLBACK ME_ReadFromRTFString(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
1686 {
1687 ME_RTFStringStreamStruct *pStruct = (ME_RTFStringStreamStruct *)dwCookie;
1688 int count;
1689
1690 count = min(cb, pStruct->length - pStruct->pos);
1691 memmove(lpBuff, pStruct->string + pStruct->pos, count);
1692 pStruct->pos += count;
1693 *pcb = count;
1694 return 0;
1695 }
1696
1697 static void
1698 ME_StreamInRTFString(ME_TextEditor *editor, BOOL selection, char *string)
1699 {
1700 EDITSTREAM es;
1701 ME_RTFStringStreamStruct data;
1702
1703 data.string = string;
1704 data.length = strlen(string);
1705 data.pos = 0;
1706 es.dwCookie = (DWORD_PTR)&data;
1707 es.pfnCallback = ME_ReadFromRTFString;
1708 ME_StreamIn(editor, SF_RTF | (selection ? SFF_SELECTION : 0), &es, TRUE);
1709 }
1710
1711
1712 static int
1713 ME_FindText(ME_TextEditor *editor, DWORD flags, const CHARRANGE *chrg, const WCHAR *text, CHARRANGE *chrgText)
1714 {
1715 const int nLen = lstrlenW(text);
1716 const int nTextLen = ME_GetTextLength(editor);
1717 int nMin, nMax;
1718 ME_Cursor cursor;
1719 WCHAR wLastChar = ' ';
1720
1721 TRACE("flags==0x%08x, chrg->cpMin==%d, chrg->cpMax==%d text==%s\n",
1722 flags, chrg->cpMin, chrg->cpMax, debugstr_w(text));
1723
1724 if (flags & ~(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD))
1725 FIXME("Flags 0x%08x not implemented\n",
1726 flags & ~(FR_DOWN | FR_MATCHCASE | FR_WHOLEWORD));
1727
1728 nMin = chrg->cpMin;
1729 if (chrg->cpMax == -1)
1730 nMax = nTextLen;
1731 else
1732 nMax = chrg->cpMax > nTextLen ? nTextLen : chrg->cpMax;
1733
1734 /* In 1.0 emulation, if cpMax reaches end of text, add the FR_DOWN flag */
1735 if (editor->bEmulateVersion10 && nMax == nTextLen)
1736 {
1737 flags |= FR_DOWN;
1738 }
1739
1740 /* In 1.0 emulation, cpMin must always be no greater than cpMax */
1741 if (editor->bEmulateVersion10 && nMax < nMin)
1742 {
1743 if (chrgText)
1744 {
1745 chrgText->cpMin = -1;
1746 chrgText->cpMax = -1;
1747 }
1748 return -1;
1749 }
1750
1751 /* when searching up, if cpMin < cpMax, then instead of searching
1752 * on [cpMin,cpMax], we search on [0,cpMin], otherwise, search on
1753 * [cpMax, cpMin]. The exception is when cpMax is -1, in which
1754 * case, it is always bigger than cpMin.
1755 */
1756 if (!editor->bEmulateVersion10 && !(flags & FR_DOWN))
1757 {
1758 int nSwap = nMax;
1759
1760 nMax = nMin > nTextLen ? nTextLen : nMin;
1761 if (nMin < nSwap || chrg->cpMax == -1)
1762 nMin = 0;
1763 else
1764 nMin = nSwap;
1765 }
1766
1767 if (!nLen || nMin < 0 || nMax < 0 || nMax < nMin)
1768 {
1769 if (chrgText)
1770 chrgText->cpMin = chrgText->cpMax = -1;
1771 return -1;
1772 }
1773
1774 if (flags & FR_DOWN) /* Forward search */
1775 {
1776 /* If possible, find the character before where the search starts */
1777 if ((flags & FR_WHOLEWORD) && nMin)
1778 {
1779 ME_CursorFromCharOfs(editor, nMin - 1, &cursor);
1780 wLastChar = *get_text( &cursor.pRun->member.run, cursor.nOffset );
1781 ME_MoveCursorChars(editor, &cursor, 1);
1782 } else {
1783 ME_CursorFromCharOfs(editor, nMin, &cursor);
1784 }
1785
1786 while (cursor.pRun && ME_GetCursorOfs(&cursor) + nLen <= nMax)
1787 {
1788 ME_DisplayItem *pCurItem = cursor.pRun;
1789 int nCurStart = cursor.nOffset;
1790 int nMatched = 0;
1791
1792 while (pCurItem && ME_CharCompare( *get_text( &pCurItem->member.run, nCurStart + nMatched ), text[nMatched], (flags & FR_MATCHCASE)))
1793 {
1794 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
1795 break;
1796
1797 nMatched++;
1798 if (nMatched == nLen)
1799 {
1800 ME_DisplayItem *pNextItem = pCurItem;
1801 int nNextStart = nCurStart;
1802 WCHAR wNextChar;
1803
1804 /* Check to see if next character is a whitespace */
1805 if (flags & FR_WHOLEWORD)
1806 {
1807 if (nCurStart + nMatched == pCurItem->member.run.len)
1808 {
1809 pNextItem = ME_FindItemFwd(pCurItem, diRun);
1810 nNextStart = -nMatched;
1811 }
1812
1813 if (pNextItem)
1814 wNextChar = *get_text( &pNextItem->member.run, nNextStart + nMatched );
1815 else
1816 wNextChar = ' ';
1817
1818 if (isalnumW(wNextChar))
1819 break;
1820 }
1821
1822 cursor.nOffset += cursor.pPara->member.para.nCharOfs + cursor.pRun->member.run.nCharOfs;
1823 if (chrgText)
1824 {
1825 chrgText->cpMin = cursor.nOffset;
1826 chrgText->cpMax = cursor.nOffset + nLen;
1827 }
1828 TRACE("found at %d-%d\n", cursor.nOffset, cursor.nOffset + nLen);
1829 return cursor.nOffset;
1830 }
1831 if (nCurStart + nMatched == pCurItem->member.run.len)
1832 {
1833 pCurItem = ME_FindItemFwd(pCurItem, diRun);
1834 nCurStart = -nMatched;
1835 }
1836 }
1837 if (pCurItem)
1838 wLastChar = *get_text( &pCurItem->member.run, nCurStart + nMatched );
1839 else
1840 wLastChar = ' ';
1841
1842 cursor.nOffset++;
1843 if (cursor.nOffset == cursor.pRun->member.run.len)
1844 {
1845 ME_NextRun(&cursor.pPara, &cursor.pRun);
1846 cursor.nOffset = 0;
1847 }
1848 }
1849 }
1850 else /* Backward search */
1851 {
1852 /* If possible, find the character after where the search ends */
1853 if ((flags & FR_WHOLEWORD) && nMax < nTextLen - 1)
1854 {
1855 ME_CursorFromCharOfs(editor, nMax + 1, &cursor);
1856 wLastChar = *get_text( &cursor.pRun->member.run, cursor.nOffset );
1857 ME_MoveCursorChars(editor, &cursor, -1);
1858 } else {
1859 ME_CursorFromCharOfs(editor, nMax, &cursor);
1860 }
1861
1862 while (cursor.pRun && ME_GetCursorOfs(&cursor) - nLen >= nMin)
1863 {
1864 ME_DisplayItem *pCurItem = cursor.pRun;
1865 ME_DisplayItem *pCurPara = cursor.pPara;
1866 int nCurEnd = cursor.nOffset;
1867 int nMatched = 0;
1868
1869 if (nCurEnd == 0)
1870 {
1871 ME_PrevRun(&pCurPara, &pCurItem);
1872 nCurEnd = pCurItem->member.run.len;
1873 }
1874
1875 while (pCurItem && ME_CharCompare( *get_text( &pCurItem->member.run, nCurEnd - nMatched - 1 ),
1876 text[nLen - nMatched - 1], (flags & FR_MATCHCASE) ))
1877 {
1878 if ((flags & FR_WHOLEWORD) && isalnumW(wLastChar))
1879 break;
1880
1881 nMatched++;
1882 if (nMatched == nLen)
1883 {
1884 ME_DisplayItem *pPrevItem = pCurItem;
1885 int nPrevEnd = nCurEnd;
1886 WCHAR wPrevChar;
1887 int nStart;
1888
1889 /* Check to see if previous character is a whitespace */
1890 if (flags & FR_WHOLEWORD)
1891 {
1892 if (nPrevEnd - nMatched == 0)
1893 {
1894 pPrevItem = ME_FindItemBack(pCurItem, diRun);
1895 if (pPrevItem)
1896 nPrevEnd = pPrevItem->member.run.len + nMatched;
1897 }
1898
1899 if (pPrevItem)
1900 wPrevChar = *get_text( &pPrevItem->member.run, nPrevEnd - nMatched - 1 );
1901 else
1902 wPrevChar = ' ';
1903
1904 if (isalnumW(wPrevChar))
1905 break;
1906 }
1907
1908 nStart = pCurPara->member.para.nCharOfs
1909 + pCurItem->member.run.nCharOfs + nCurEnd - nMatched;
1910 if (chrgText)
1911 {
1912 chrgText->cpMin = nStart;
1913 chrgText->cpMax = nStart + nLen;
1914 }
1915 TRACE("found at %d-%d\n", nStart, nStart + nLen);
1916 return nStart;
1917 }
1918 if (nCurEnd - nMatched == 0)
1919 {
1920 ME_PrevRun(&pCurPara, &pCurItem);
1921 /* Don't care about pCurItem becoming NULL here; it's already taken
1922 * care of in the exterior loop condition */
1923 nCurEnd = pCurItem->member.run.len + nMatched;
1924 }
1925 }
1926 if (pCurItem)
1927 wLastChar = *get_text( &pCurItem->member.run, nCurEnd - nMatched - 1 );
1928 else
1929 wLastChar = ' ';
1930
1931 cursor.nOffset--;
1932 if (cursor.nOffset < 0)
1933 {
1934 ME_PrevRun(&cursor.pPara, &cursor.pRun);
1935 cursor.nOffset = cursor.pRun->member.run.len;
1936 }
1937 }
1938 }
1939 TRACE("not found\n");
1940 if (chrgText)
1941 chrgText->cpMin = chrgText->cpMax = -1;
1942 return -1;
1943 }
1944
1945 static int ME_GetTextEx(ME_TextEditor *editor, GETTEXTEX *ex, LPARAM pText)
1946 {
1947 int nChars;
1948 ME_Cursor start;
1949
1950 if (!ex->cb || !pText) return 0;
1951
1952 if (ex->flags & ~(GT_SELECTION | GT_USECRLF))
1953 FIXME("GETTEXTEX flags 0x%08x not supported\n", ex->flags & ~(GT_SELECTION | GT_USECRLF));
1954
1955 if (ex->flags & GT_SELECTION)
1956 {
1957 int from, to;
1958 int nStartCur = ME_GetSelectionOfs(editor, &from, &to);
1959 start = editor->pCursors[nStartCur];
1960 nChars = to - from;
1961 }
1962 else
1963 {
1964 ME_SetCursorToStart(editor, &start);
1965 nChars = INT_MAX;
1966 }
1967 if (ex->codepage == CP_UNICODE)
1968 {
1969 return ME_GetTextW(editor, (LPWSTR)pText, ex->cb / sizeof(WCHAR) - 1,
1970 &start, nChars, ex->flags & GT_USECRLF, FALSE);
1971 }
1972 else
1973 {
1974 /* potentially each char may be a CR, why calculate the exact value with O(N) when
1975 we can just take a bigger buffer? :)
1976 The above assumption still holds with CR/LF counters, since CR->CRLF expansion
1977 occurs only in richedit 2.0 mode, in which line breaks have only one CR
1978 */
1979 int crlfmul = (ex->flags & GT_USECRLF) ? 2 : 1;
1980 DWORD buflen;
1981 LPWSTR buffer;
1982 LRESULT rc;
1983
1984 buflen = min(crlfmul * nChars, ex->cb - 1);
1985 buffer = heap_alloc((buflen + 1) * sizeof(WCHAR));
1986
1987 nChars = ME_GetTextW(editor, buffer, buflen, &start, nChars, ex->flags & GT_USECRLF, FALSE);
1988 rc = WideCharToMultiByte(ex->codepage, 0, buffer, nChars + 1,
1989 (LPSTR)pText, ex->cb, ex->lpDefaultChar, ex->lpUsedDefChar);
1990 if (rc) rc--; /* do not count 0 terminator */
1991
1992 heap_free(buffer);
1993 return rc;
1994 }
1995 }
1996
1997 static int ME_GetTextRange(ME_TextEditor *editor, WCHAR *strText,
1998 const ME_Cursor *start, int nLen, BOOL unicode)
1999 {
2000 if (!strText) return 0;
2001 if (unicode) {
2002 return ME_GetTextW(editor, strText, INT_MAX, start, nLen, FALSE, FALSE);
2003 } else {
2004 int nChars;
2005 WCHAR *p = ALLOC_N_OBJ(WCHAR, nLen+1);
2006 if (!p) return 0;
2007 nChars = ME_GetTextW(editor, p, nLen, start, nLen, FALSE, FALSE);
2008 WideCharToMultiByte(CP_ACP, 0, p, nChars+1, (char *)strText,
2009 nLen+1, NULL, NULL);
2010 FREE_OBJ(p);
2011 return nChars;
2012 }
2013 }
2014
2015 static int handle_EM_EXSETSEL( ME_TextEditor *editor, int to, int from )
2016 {
2017 int end;
2018
2019 TRACE("%d - %d\n", to, from );
2020
2021 ME_InvalidateSelection( editor );
2022 end = ME_SetSelection( editor, to, from );
2023 ME_InvalidateSelection( editor );
2024 ITextHost_TxShowCaret( editor->texthost, FALSE );
2025 ME_ShowCaret( editor );
2026 ME_SendSelChange( editor );
2027
2028 return end;
2029 }
2030
2031 typedef struct tagME_GlobalDestStruct
2032 {
2033 HGLOBAL hData;
2034 int nLength;
2035 } ME_GlobalDestStruct;
2036
2037 static DWORD CALLBACK ME_ReadFromHGLOBALUnicode(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
2038 {
2039 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
2040 int i;
2041 WORD *pSrc, *pDest;
2042
2043 cb = cb >> 1;
2044 pDest = (WORD *)lpBuff;
2045 pSrc = GlobalLock(pData->hData);
2046 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
2047 pDest[i] = pSrc[pData->nLength+i];
2048 }
2049 pData->nLength += i;
2050 *pcb = 2*i;
2051 GlobalUnlock(pData->hData);
2052 return 0;
2053 }
2054
2055 static DWORD CALLBACK ME_ReadFromHGLOBALRTF(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, LONG *pcb)
2056 {
2057 ME_GlobalDestStruct *pData = (ME_GlobalDestStruct *)dwCookie;
2058 int i;
2059 BYTE *pSrc, *pDest;
2060
2061 pDest = lpBuff;
2062 pSrc = GlobalLock(pData->hData);
2063 for (i = 0; i<cb && pSrc[pData->nLength+i]; i++) {
2064 pDest[i] = pSrc[pData->nLength+i];
2065 }
2066 pData->nLength += i;
2067 *pcb = i;
2068 GlobalUnlock(pData->hData);
2069 return 0;
2070 }
2071
2072 static BOOL ME_Paste(ME_TextEditor *editor)
2073 {
2074 DWORD dwFormat = 0;
2075 EDITSTREAM es;
2076 ME_GlobalDestStruct gds;
2077 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
2078 UINT cf = 0;
2079
2080 if (IsClipboardFormatAvailable(nRTFFormat))
2081 cf = nRTFFormat, dwFormat = SF_RTF;
2082 else if (IsClipboardFormatAvailable(CF_UNICODETEXT))
2083 cf = CF_UNICODETEXT, dwFormat = SF_TEXT|SF_UNICODE;
2084 else
2085 return FALSE;
2086
2087 if (!OpenClipboard(editor->hWnd))
2088 return FALSE;
2089 gds.hData = GetClipboardData(cf);
2090 gds.nLength = 0;
2091 es.dwCookie = (DWORD_PTR)&gds;
2092 es.pfnCallback = dwFormat == SF_RTF ? ME_ReadFromHGLOBALRTF : ME_ReadFromHGLOBALUnicode;
2093 ME_StreamIn(editor, dwFormat|SFF_SELECTION, &es, FALSE);
2094
2095 CloseClipboard();
2096 return TRUE;
2097 }
2098
2099 static BOOL ME_Copy(ME_TextEditor *editor, const ME_Cursor *start, int nChars)
2100 {
2101 LPDATAOBJECT dataObj = NULL;
2102 HRESULT hr = S_OK;
2103
2104 if (editor->cPasswordMask)
2105 return FALSE; /* Copying or Cutting masked text isn't allowed */
2106
2107 if(editor->lpOleCallback)
2108 {
2109 CHARRANGE range;
2110 range.cpMin = ME_GetCursorOfs(start);
2111 range.cpMax = range.cpMin + nChars;
2112 hr = IRichEditOleCallback_GetClipboardData(editor->lpOleCallback, &range, RECO_COPY, &dataObj);
2113 }
2114 if(FAILED(hr) || !dataObj)
2115 hr = ME_GetDataObject(editor, start, nChars, &dataObj);
2116 if(SUCCEEDED(hr)) {
2117 hr = OleSetClipboard(dataObj);
2118 IDataObject_Release(dataObj);
2119 }
2120 return SUCCEEDED(hr);
2121 }
2122
2123 /* helper to send a msg filter notification */
2124 static BOOL
2125 ME_FilterEvent(ME_TextEditor *editor, UINT msg, WPARAM* wParam, LPARAM* lParam)
2126 {
2127 MSGFILTER msgf;
2128
2129 if (!editor->hWnd || !editor->hwndParent) return FALSE;
2130 msgf.nmhdr.hwndFrom = editor->hWnd;
2131 msgf.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
2132 msgf.nmhdr.code = EN_MSGFILTER;
2133 msgf.msg = msg;
2134 msgf.wParam = *wParam;
2135 msgf.lParam = *lParam;
2136 if (SendMessageW(editor->hwndParent, WM_NOTIFY, msgf.nmhdr.idFrom, (LPARAM)&msgf))
2137 return FALSE;
2138 *wParam = msgf.wParam;
2139 *lParam = msgf.lParam;
2140 msgf.wParam = *wParam;
2141
2142 return TRUE;
2143 }
2144
2145 static void ME_UpdateSelectionLinkAttribute(ME_TextEditor *editor)
2146 {
2147 ME_DisplayItem *startPara, *endPara;
2148 ME_DisplayItem *prev_para;
2149 ME_Cursor *from, *to;
2150 ME_Cursor start;
2151 int nChars;
2152
2153 if (!editor->AutoURLDetect_bEnable) return;
2154
2155 ME_GetSelection(editor, &from, &to);
2156
2157 /* Find paragraph previous to the one that contains start cursor */
2158 startPara = from->pPara;
2159 prev_para = startPara->member.para.prev_para;
2160 if (prev_para->type == diParagraph) startPara = prev_para;
2161
2162 /* Find paragraph that contains end cursor */
2163 endPara = to->pPara->member.para.next_para;
2164
2165 start.pPara = startPara;
2166 start.pRun = ME_FindItemFwd(startPara, diRun);
2167 start.nOffset = 0;
2168 nChars = endPara->member.para.nCharOfs - startPara->member.para.nCharOfs;
2169
2170 ME_UpdateLinkAttribute(editor, &start, nChars);
2171 }
2172
2173 static BOOL
2174 ME_KeyDown(ME_TextEditor *editor, WORD nKey)
2175 {
2176 BOOL ctrl_is_down = GetKeyState(VK_CONTROL) & 0x8000;
2177 BOOL shift_is_down = GetKeyState(VK_SHIFT) & 0x8000;
2178
2179 if (editor->bMouseCaptured)
2180 return FALSE;
2181 if (nKey != VK_SHIFT && nKey != VK_CONTROL && nKey != VK_MENU)
2182 editor->nSelectionType = stPosition;
2183
2184 switch (nKey)
2185 {
2186 case VK_LEFT:
2187 case VK_RIGHT:
2188 case VK_HOME:
2189 case VK_END:
2190 editor->nUDArrowX = -1;
2191 /* fall through */
2192 case VK_UP:
2193 case VK_DOWN:
2194 case VK_PRIOR:
2195 case VK_NEXT:
2196 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
2197 ME_ArrowKey(editor, nKey, shift_is_down, ctrl_is_down);
2198 return TRUE;
2199 case VK_BACK:
2200 case VK_DELETE:
2201 editor->nUDArrowX = -1;
2202 /* FIXME backspace and delete aren't the same, they act different wrt paragraph style of the merged paragraph */
2203 if (editor->styleFlags & ES_READONLY)
2204 return FALSE;
2205 if (ME_IsSelection(editor))
2206 {
2207 ME_DeleteSelection(editor);
2208 ME_CommitUndo(editor);
2209 }
2210 else if (nKey == VK_DELETE)
2211 {
2212 /* Delete stops group typing.
2213 * (See MSDN remarks on EM_STOPGROUPTYPING message) */
2214 ME_DeleteTextAtCursor(editor, 1, 1);
2215 ME_CommitUndo(editor);
2216 }
2217 else if (ME_ArrowKey(editor, VK_LEFT, FALSE, FALSE))
2218 {
2219 BOOL bDeletionSucceeded;
2220 /* Backspace can be grouped for a single undo */
2221 ME_ContinueCoalescingTransaction(editor);
2222 bDeletionSucceeded = ME_DeleteTextAtCursor(editor, 1, 1);
2223 if (!bDeletionSucceeded && !editor->bEmulateVersion10) { /* v4.1 */
2224 /* Deletion was prevented so the cursor is moved back to where it was.
2225 * (e.g. this happens when trying to delete cell boundaries)
2226 */
2227 ME_ArrowKey(editor, VK_RIGHT, FALSE, FALSE);
2228 }
2229 ME_CommitCoalescingUndo(editor);
2230 }
2231 else
2232 return TRUE;
2233 ME_MoveCursorFromTableRowStartParagraph(editor);
2234 ME_UpdateSelectionLinkAttribute(editor);
2235 ME_UpdateRepaint(editor, FALSE);
2236 ME_SendRequestResize(editor, FALSE);
2237 return TRUE;
2238 case VK_RETURN:
2239 if (editor->bDialogMode)
2240 {
2241 if (ctrl_is_down)
2242 return TRUE;
2243
2244 if (!(editor->styleFlags & ES_WANTRETURN))
2245 {
2246 if (editor->hwndParent)
2247 {
2248 DWORD dw;
2249 dw = SendMessageW(editor->hwndParent, DM_GETDEFID, 0, 0);
2250 if (HIWORD(dw) == DC_HASDEFID)
2251 {
2252 HWND hwDefCtrl = GetDlgItem(editor->hwndParent, LOWORD(dw));
2253 if (hwDefCtrl)
2254 {
2255 SendMessageW(editor->hwndParent, WM_NEXTDLGCTL, (WPARAM)hwDefCtrl, TRUE);
2256 PostMessageW(hwDefCtrl, WM_KEYDOWN, VK_RETURN, 0);
2257 }
2258 }
2259 }
2260 return TRUE;
2261 }
2262 }
2263
2264 if (editor->styleFlags & ES_MULTILINE)
2265 {
2266 ME_Cursor cursor = editor->pCursors[0];
2267 ME_DisplayItem *para = cursor.pPara;
2268 int from, to;
2269 const WCHAR endl = '\r';
2270 const WCHAR endlv10[] = {'\r','\n'};
2271 ME_Style *style;
2272
2273 if (editor->styleFlags & ES_READONLY) {
2274 MessageBeep(MB_ICONERROR);
2275 return TRUE;
2276 }
2277
2278 ME_GetSelectionOfs(editor, &from, &to);
2279 if (editor->nTextLimit > ME_GetTextLength(editor) - (to-from))
2280 {
2281 if (!editor->bEmulateVersion10) { /* v4.1 */
2282 if (para->member.para.nFlags & MEPF_ROWEND) {
2283 /* Add a new table row after this row. */
2284 para = ME_AppendTableRow(editor, para);
2285 para = para->member.para.next_para;
2286 editor->pCursors[0].pPara = para;
2287 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2288 editor->pCursors[0].nOffset = 0;
2289 editor->pCursors[1] = editor->pCursors[0];
2290 ME_CommitUndo(editor);
2291 ME_CheckTablesForCorruption(editor);
2292 ME_UpdateRepaint(editor, FALSE);
2293 return TRUE;
2294 }
2295 else if (para == editor->pCursors[1].pPara &&
2296 cursor.nOffset + cursor.pRun->member.run.nCharOfs == 0 &&
2297 para->member.para.prev_para->member.para.nFlags & MEPF_ROWSTART &&
2298 !para->member.para.prev_para->member.para.nCharOfs)
2299 {
2300 /* Insert a newline before the table. */
2301 para = para->member.para.prev_para;
2302 para->member.para.nFlags &= ~MEPF_ROWSTART;
2303 editor->pCursors[0].pPara = para;
2304 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2305 editor->pCursors[1] = editor->pCursors[0];
2306 ME_InsertTextFromCursor(editor, 0, &endl, 1,
2307 editor->pCursors[0].pRun->member.run.style);
2308 para = editor->pBuffer->pFirst->member.para.next_para;
2309 ME_SetDefaultParaFormat(para->member.para.pFmt);
2310 para->member.para.nFlags = MEPF_REWRAP;
2311 editor->pCursors[0].pPara = para;
2312 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2313 editor->pCursors[1] = editor->pCursors[0];
2314 para->member.para.next_para->member.para.nFlags |= MEPF_ROWSTART;
2315 ME_CommitCoalescingUndo(editor);
2316 ME_CheckTablesForCorruption(editor);
2317 ME_UpdateRepaint(editor, FALSE);
2318 return TRUE;
2319 }
2320 } else { /* v1.0 - 3.0 */
2321 ME_DisplayItem *para = cursor.pPara;
2322 if (ME_IsInTable(para))
2323 {
2324 if (cursor.pRun->member.run.nFlags & MERF_ENDPARA)
2325 {
2326 if (from == to) {
2327 ME_ContinueCoalescingTransaction(editor);
2328 para = ME_AppendTableRow(editor, para);
2329 editor->pCursors[0].pPara = para;
2330 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2331 editor->pCursors[0].nOffset = 0;
2332 editor->pCursors[1] = editor->pCursors[0];
2333 ME_CommitCoalescingUndo(editor);
2334 ME_UpdateRepaint(editor, FALSE);
2335 return TRUE;
2336 }
2337 } else {
2338 ME_ContinueCoalescingTransaction(editor);
2339 if (cursor.pRun->member.run.nCharOfs + cursor.nOffset == 0 &&
2340 !ME_IsInTable(para->member.para.prev_para))
2341 {
2342 /* Insert newline before table */
2343 cursor.pRun = ME_FindItemBack(para, diRun);
2344 if (cursor.pRun) {
2345 editor->pCursors[0].pRun = cursor.pRun;
2346 editor->pCursors[0].pPara = para->member.para.prev_para;
2347 }
2348 editor->pCursors[0].nOffset = 0;
2349 editor->pCursors[1] = editor->pCursors[0];
2350 ME_InsertTextFromCursor(editor, 0, &endl, 1,
2351 editor->pCursors[0].pRun->member.run.style);
2352 } else {
2353 editor->pCursors[1] = editor->pCursors[0];
2354 para = ME_AppendTableRow(editor, para);
2355 editor->pCursors[0].pPara = para;
2356 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2357 editor->pCursors[0].nOffset = 0;
2358 editor->pCursors[1] = editor->pCursors[0];
2359 }
2360 ME_CommitCoalescingUndo(editor);
2361 ME_UpdateRepaint(editor, FALSE);
2362 return TRUE;
2363 }
2364 }
2365 }
2366
2367 style = ME_GetInsertStyle(editor, 0);
2368 ME_SaveTempStyle(editor);
2369 ME_ContinueCoalescingTransaction(editor);
2370 if (shift_is_down)
2371 ME_InsertEndRowFromCursor(editor, 0);
2372 else
2373 if (!editor->bEmulateVersion10)
2374 ME_InsertTextFromCursor(editor, 0, &endl, 1, style);
2375 else
2376 ME_InsertTextFromCursor(editor, 0, endlv10, 2, style);
2377 ME_ReleaseStyle(style);
2378 ME_CommitCoalescingUndo(editor);
2379 SetCursor(NULL);
2380
2381 ME_UpdateSelectionLinkAttribute(editor);
2382 ME_UpdateRepaint(editor, FALSE);
2383 }
2384 return TRUE;
2385 }
2386 break;
2387 case VK_ESCAPE:
2388 if (editor->bDialogMode && editor->hwndParent)
2389 PostMessageW(editor->hwndParent, WM_CLOSE, 0, 0);
2390 return TRUE;
2391 case VK_TAB:
2392 if (editor->bDialogMode && editor->hwndParent)
2393 SendMessageW(editor->hwndParent, WM_NEXTDLGCTL, shift_is_down, 0);
2394 return TRUE;
2395 case 'A':
2396 if (ctrl_is_down)
2397 {
2398 handle_EM_EXSETSEL( editor, 0, -1 );
2399 return TRUE;
2400 }
2401 break;
2402 case 'V':
2403 if (ctrl_is_down)
2404 return ME_Paste(editor);
2405 break;
2406 case 'C':
2407 case 'X':
2408 if (ctrl_is_down)
2409 {
2410 BOOL result;
2411 int nOfs, nChars;
2412 int nStartCur = ME_GetSelectionOfs(editor, &nOfs, &nChars);
2413 ME_Cursor *selStart = &editor->pCursors[nStartCur];
2414
2415 nChars -= nOfs;
2416 result = ME_Copy(editor, selStart, nChars);
2417 if (result && nKey == 'X')
2418 {
2419 ME_InternalDeleteText(editor, selStart, nChars, FALSE);
2420 ME_CommitUndo(editor);
2421 ME_UpdateRepaint(editor, TRUE);
2422 }
2423 return result;
2424 }
2425 break;
2426 case 'Z':
2427 if (ctrl_is_down)
2428 {
2429 ME_Undo(editor);
2430 return TRUE;
2431 }
2432 break;
2433 case 'Y':
2434 if (ctrl_is_down)
2435 {
2436 ME_Redo(editor);
2437 return TRUE;
2438 }
2439 break;
2440
2441 default:
2442 if (nKey != VK_SHIFT && nKey != VK_CONTROL && nKey && nKey != VK_MENU)
2443 editor->nUDArrowX = -1;
2444 if (ctrl_is_down)
2445 {
2446 if (nKey == 'W')
2447 {
2448 CHARFORMAT2W chf;
2449 char buf[2048];
2450 chf.cbSize = sizeof(chf);
2451
2452 ME_GetSelectionCharFormat(editor, &chf);
2453 ME_DumpStyleToBuf(&chf, buf);
2454 MessageBoxA(NULL, buf, "Style dump", MB_OK);
2455 }
2456 if (nKey == 'Q')
2457 {
2458 ME_CheckCharOffsets(editor);
2459 }
2460 }
2461 }
2462 return FALSE;
2463 }
2464
2465 static LRESULT ME_Char(ME_TextEditor *editor, WPARAM charCode,
2466 LPARAM flags, BOOL unicode)
2467 {
2468 WCHAR wstr;
2469
2470 if (editor->bMouseCaptured)
2471 return 0;
2472
2473 if (unicode)
2474 wstr = (WCHAR)charCode;
2475 else
2476 {
2477 CHAR charA = charCode;
2478 MultiByteToWideChar(CP_ACP, 0, &charA, 1, &wstr, 1);
2479 }
2480
2481 if (editor->styleFlags & ES_READONLY) {
2482 MessageBeep(MB_ICONERROR);
2483 return 0; /* FIXME really 0 ? */
2484 }
2485
2486 if ((unsigned)wstr >= ' ' || wstr == '\t')
2487 {
2488 ME_Cursor cursor = editor->pCursors[0];
2489 ME_DisplayItem *para = cursor.pPara;
2490 int from, to;
2491 BOOL ctrl_is_down = GetKeyState(VK_CONTROL) & 0x8000;
2492 ME_GetSelectionOfs(editor, &from, &to);
2493 if (wstr == '\t' &&
2494 /* v4.1 allows tabs to be inserted with ctrl key down */
2495 !(ctrl_is_down && !editor->bEmulateVersion10))
2496 {
2497 ME_DisplayItem *para;
2498 BOOL bSelectedRow = FALSE;
2499
2500 para = cursor.pPara;
2501 if (ME_IsSelection(editor) &&
2502 cursor.pRun->member.run.nCharOfs + cursor.nOffset == 0 &&
2503 to == ME_GetCursorOfs(&editor->pCursors[0]) &&
2504 para->member.para.prev_para->type == diParagraph)
2505 {
2506 para = para->member.para.prev_para;
2507 bSelectedRow = TRUE;
2508 }
2509 if (ME_IsInTable(para))
2510 {
2511 ME_TabPressedInTable(editor, bSelectedRow);
2512 ME_CommitUndo(editor);
2513 return 0;
2514 }
2515 } else if (!editor->bEmulateVersion10) { /* v4.1 */
2516 if (para->member.para.nFlags & MEPF_ROWEND) {
2517 if (from == to) {
2518 para = para->member.para.next_para;
2519 if (para->member.para.nFlags & MEPF_ROWSTART)
2520 para = para->member.para.next_para;
2521 editor->pCursors[0].pPara = para;
2522 editor->pCursors[0].pRun = ME_FindItemFwd(para, diRun);
2523 editor->pCursors[0].nOffset = 0;
2524 editor->pCursors[1] = editor->pCursors[0];
2525 }
2526 }
2527 } else { /* v1.0 - 3.0 */
2528 if (ME_IsInTable(cursor.pRun) &&
2529 cursor.pRun->member.run.nFlags & MERF_ENDPARA &&
2530 from == to)
2531 {
2532 /* Text should not be inserted at the end of the table. */
2533 MessageBeep(-1);
2534 return 0;
2535 }
2536 }
2537 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
2538 /* WM_CHAR is restricted to nTextLimit */
2539 if(editor->nTextLimit > ME_GetTextLength(editor) - (to-from))
2540 {
2541 ME_Style *style = ME_GetInsertStyle(editor, 0);
2542 ME_SaveTempStyle(editor);
2543 ME_ContinueCoalescingTransaction(editor);
2544 ME_InsertTextFromCursor(editor, 0, &wstr, 1, style);
2545 ME_ReleaseStyle(style);
2546 ME_CommitCoalescingUndo(editor);
2547 ITextHost_TxSetCursor(editor->texthost, NULL, FALSE);
2548 }
2549
2550 ME_UpdateSelectionLinkAttribute(editor);
2551 ME_UpdateRepaint(editor, FALSE);
2552 }
2553 return 0;
2554 }
2555
2556 /* Process the message and calculate the new click count.
2557 *
2558 * returns: The click count if it is mouse down event, else returns 0. */
2559 static int ME_CalculateClickCount(ME_TextEditor *editor, UINT msg, WPARAM wParam,
2560 LPARAM lParam)
2561 {
2562 static int clickNum = 0;
2563 if (msg < WM_MOUSEFIRST || msg > WM_MOUSELAST)
2564 return 0;
2565
2566 if ((msg == WM_LBUTTONDBLCLK) ||
2567 (msg == WM_RBUTTONDBLCLK) ||
2568 (msg == WM_MBUTTONDBLCLK) ||
2569 (msg == WM_XBUTTONDBLCLK))
2570 {
2571 msg -= (WM_LBUTTONDBLCLK - WM_LBUTTONDOWN);
2572 }
2573
2574 if ((msg == WM_LBUTTONDOWN) ||
2575 (msg == WM_RBUTTONDOWN) ||
2576 (msg == WM_MBUTTONDOWN) ||
2577 (msg == WM_XBUTTONDOWN))
2578 {
2579 static MSG prevClickMsg;
2580 MSG clickMsg;
2581 /* Compare the editor instead of the hwnd so that the this
2582 * can still be done for windowless richedit controls. */
2583 clickMsg.hwnd = (HWND)editor;
2584 clickMsg.message = msg;
2585 clickMsg.wParam = wParam;
2586 clickMsg.lParam = lParam;
2587 clickMsg.time = GetMessageTime();
2588 clickMsg.pt.x = (short)LOWORD(lParam);
2589 clickMsg.pt.y = (short)HIWORD(lParam);
2590 if ((clickNum != 0) &&
2591 (clickMsg.message == prevClickMsg.message) &&
2592 (clickMsg.hwnd == prevClickMsg.hwnd) &&
2593 (clickMsg.wParam == prevClickMsg.wParam) &&
2594 (clickMsg.time - prevClickMsg.time < GetDoubleClickTime()) &&
2595 (abs(clickMsg.pt.x - prevClickMsg.pt.x) < GetSystemMetrics(SM_CXDOUBLECLK)/2) &&
2596 (abs(clickMsg.pt.y - prevClickMsg.pt.y) < GetSystemMetrics(SM_CYDOUBLECLK)/2))
2597 {
2598 clickNum++;
2599 } else {
2600 clickNum = 1;
2601 }
2602 prevClickMsg = clickMsg;
2603 } else {
2604 return 0;
2605 }
2606 return clickNum;
2607 }
2608
2609 static BOOL ME_SetCursor(ME_TextEditor *editor)
2610 {
2611 ME_Cursor cursor;
2612 POINT pt;
2613 BOOL isExact;
2614 SCROLLBARINFO sbi;
2615 DWORD messagePos = GetMessagePos();
2616 pt.x = (short)LOWORD(messagePos);
2617 pt.y = (short)HIWORD(messagePos);
2618
2619 if (editor->hWnd)
2620 {
2621 sbi.cbSize = sizeof(sbi);
2622 GetScrollBarInfo(editor->hWnd, OBJID_HSCROLL, &sbi);
2623 if (!(sbi.rgstate[0] & (STATE_SYSTEM_INVISIBLE|STATE_SYSTEM_OFFSCREEN)) &&
2624 PtInRect(&sbi.rcScrollBar, pt))
2625 {
2626 ITextHost_TxSetCursor(editor->texthost,
2627 LoadCursorW(NULL, (WCHAR*)IDC_ARROW), FALSE);
2628 return TRUE;
2629 }
2630 sbi.cbSize = sizeof(sbi);
2631 GetScrollBarInfo(editor->hWnd, OBJID_VSCROLL, &sbi);
2632 if (!(sbi.rgstate[0] & (STATE_SYSTEM_INVISIBLE|STATE_SYSTEM_OFFSCREEN)) &&
2633 PtInRect(&sbi.rcScrollBar, pt))
2634 {
2635 ITextHost_TxSetCursor(editor->texthost,
2636 LoadCursorW(NULL, (WCHAR*)IDC_ARROW), FALSE);
2637 return TRUE;
2638 }
2639 }
2640 ITextHost_TxScreenToClient(editor->texthost, &pt);
2641
2642 if (editor->nSelectionType == stLine && editor->bMouseCaptured) {
2643 ITextHost_TxSetCursor(editor->texthost, hLeft, FALSE);
2644 return TRUE;
2645 }
2646 if (!editor->bEmulateVersion10 /* v4.1 */ &&
2647 pt.y < editor->rcFormat.top &&
2648 pt.x < editor->rcFormat.left)
2649 {
2650 ITextHost_TxSetCursor(editor->texthost, hLeft, FALSE);
2651 return TRUE;
2652 }
2653 if (pt.y < editor->rcFormat.top || pt.y > editor->rcFormat.bottom)
2654 {
2655 if (editor->bEmulateVersion10) /* v1.0 - 3.0 */
2656 ITextHost_TxSetCursor(editor->texthost,
2657 LoadCursorW(NULL, (WCHAR*)IDC_ARROW), FALSE);
2658 else /* v4.1 */
2659 ITextHost_TxSetCursor(editor->texthost,
2660 LoadCursorW(NULL, (WCHAR*)IDC_IBEAM), TRUE);
2661 return TRUE;
2662 }
2663 if (pt.x < editor->rcFormat.left)
2664 {
2665 ITextHost_TxSetCursor(editor->texthost, hLeft, FALSE);
2666 return TRUE;
2667 }
2668 ME_CharFromPos(editor, pt.x, pt.y, &cursor, &isExact);
2669 if (isExact)
2670 {
2671 ME_Run *run;
2672
2673 run = &cursor.pRun->member.run;
2674 if (run->style->fmt.dwMask & CFM_LINK &&
2675 run->style->fmt.dwEffects & CFE_LINK)
2676 {
2677 ITextHost_TxSetCursor(editor->texthost,
2678 LoadCursorW(NULL, (WCHAR*)IDC_HAND),
2679 FALSE);
2680 return TRUE;
2681 }
2682
2683 if (ME_IsSelection(editor))
2684 {
2685 int selStart, selEnd;
2686 int offset = ME_GetCursorOfs(&cursor);
2687
2688 ME_GetSelectionOfs(editor, &selStart, &selEnd);
2689 if (selStart <= offset && selEnd >= offset) {
2690 ITextHost_TxSetCursor(editor->texthost,
2691 LoadCursorW(NULL, (WCHAR*)IDC_ARROW),
2692 FALSE);
2693 return TRUE;
2694 }
2695 }
2696 }
2697 ITextHost_TxSetCursor(editor->texthost,
2698 LoadCursorW(NULL, (WCHAR*)IDC_IBEAM), TRUE);
2699 return TRUE;
2700 }
2701
2702 static void ME_SetDefaultFormatRect(ME_TextEditor *editor)
2703 {
2704 ITextHost_TxGetClientRect(editor->texthost, &editor->rcFormat);
2705 editor->rcFormat.top += editor->exStyleFlags & WS_EX_CLIENTEDGE ? 1 : 0;
2706 editor->rcFormat.left += 1 + editor->selofs;
2707 editor->rcFormat.right -= 1;
2708 }
2709
2710 static BOOL ME_ShowContextMenu(ME_TextEditor *editor, int x, int y)
2711 {
2712 CHARRANGE selrange;
2713 HMENU menu;
2714 int seltype = 0;
2715 if(!editor->lpOleCallback || !editor->hWnd)
2716 return FALSE;
2717 ME_GetSelectionOfs(editor, &selrange.cpMin, &selrange.cpMax);
2718 if(selrange.cpMin == selrange.cpMax)
2719 seltype |= SEL_EMPTY;
2720 else
2721 {
2722 /* FIXME: Handle objects */
2723 seltype |= SEL_TEXT;
2724 if(selrange.cpMax-selrange.cpMin > 1)
2725 seltype |= SEL_MULTICHAR;
2726 }
2727 if(SUCCEEDED(IRichEditOleCallback_GetContextMenu(editor->lpOleCallback, seltype, NULL, &selrange, &menu)))
2728 {
2729 TrackPopupMenu(menu, TPM_LEFTALIGN | TPM_RIGHTBUTTON, x, y, 0, editor->hwndParent, NULL);
2730 DestroyMenu(menu);
2731 }
2732 return TRUE;
2733 }
2734
2735 ME_TextEditor *ME_MakeEditor(ITextHost *texthost, BOOL bEmulateVersion10)
2736 {
2737 ME_TextEditor *ed = ALLOC_OBJ(ME_TextEditor);
2738 int i;
2739 DWORD props;
2740 LONG selbarwidth;
2741
2742 ed->hWnd = NULL;
2743 ed->hwndParent = NULL;
2744 ed->sizeWindow.cx = ed->sizeWindow.cy = 0;
2745 ed->texthost = texthost;
2746 ed->reOle = NULL;
2747 ed->bEmulateVersion10 = bEmulateVersion10;
2748 ed->styleFlags = 0;
2749 ITextHost_TxGetPropertyBits(texthost,
2750 (TXTBIT_RICHTEXT|TXTBIT_MULTILINE|
2751 TXTBIT_READONLY|TXTBIT_USEPASSWORD|
2752 TXTBIT_HIDESELECTION|TXTBIT_SAVESELECTION|
2753 TXTBIT_AUTOWORDSEL|TXTBIT_VERTICAL|
2754 TXTBIT_WORDWRAP|TXTBIT_DISABLEDRAG),
2755 &props);
2756 ITextHost_TxGetScrollBars(texthost, &ed->styleFlags);
2757 ed->styleFlags &= (WS_VSCROLL|WS_HSCROLL|ES_AUTOVSCROLL|
2758 ES_AUTOHSCROLL|ES_DISABLENOSCROLL);
2759 ed->pBuffer = ME_MakeText();
2760 ed->nZoomNumerator = ed->nZoomDenominator = 0;
2761 ed->nAvailWidth = 0; /* wrap to client area */
2762 ME_MakeFirstParagraph(ed);
2763 /* The four cursors are for:
2764 * 0 - The position where the caret is shown
2765 * 1 - The anchored end of the selection (for normal selection)
2766 * 2 & 3 - The anchored start and end respectively for word, line,
2767 * or paragraph selection.
2768 */
2769 ed->nCursors = 4;
2770 ed->pCursors = ALLOC_N_OBJ(ME_Cursor, ed->nCursors);
2771 ME_SetCursorToStart(ed, &ed->pCursors[0]);
2772 ed->pCursors[1] = ed->pCursors[0];
2773 ed->pCursors[2] = ed->pCursors[0];
2774 ed->pCursors[3] = ed->pCursors[1];
2775 ed->nLastTotalLength = ed->nTotalLength = 0;
2776 ed->nLastTotalWidth = ed->nTotalWidth = 0;
2777 ed->nUDArrowX = -1;
2778 ed->rgbBackColor = -1;
2779 ed->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
2780 ed->bCaretAtEnd = FALSE;
2781 ed->nEventMask = 0;
2782 ed->nModifyStep = 0;
2783 ed->nTextLimit = TEXT_LIMIT_DEFAULT;
2784 list_init( &ed->undo_stack );
2785 list_init( &ed->redo_stack );
2786 ed->nUndoStackSize = 0;
2787 ed->nUndoLimit = STACK_SIZE_DEFAULT;
2788 ed->nUndoMode = umAddToUndo;
2789 ed->nParagraphs = 1;
2790 ed->nLastSelStart = ed->nLastSelEnd = 0;
2791 ed->pLastSelStartPara = ed->pLastSelEndPara = ed->pCursors[0].pPara;
2792 ed->bHideSelection = FALSE;
2793 ed->pfnWordBreak = NULL;
2794 ed->lpOleCallback = NULL;
2795 ed->mode = TM_MULTILEVELUNDO | TM_MULTICODEPAGE;
2796 ed->mode |= (props & TXTBIT_RICHTEXT) ? TM_RICHTEXT : TM_PLAINTEXT;
2797 ed->AutoURLDetect_bEnable = FALSE;
2798 ed->bHaveFocus = FALSE;
2799 ed->bDialogMode = FALSE;
2800 ed->bMouseCaptured = FALSE;
2801 for (i=0; i<HFONT_CACHE_SIZE; i++)
2802 {
2803 ed->pFontCache[i].nRefs = 0;
2804 ed->pFontCache[i].nAge = 0;
2805 ed->pFontCache[i].hFont = NULL;
2806 }
2807
2808 ME_CheckCharOffsets(ed);
2809 ed->bDefaultFormatRect = TRUE;
2810 ITextHost_TxGetSelectionBarWidth(ed->texthost, &selbarwidth);
2811 if (selbarwidth) {
2812 /* FIXME: Convert selbarwidth from HIMETRIC to pixels */
2813 ed->selofs = SELECTIONBAR_WIDTH;
2814 ed->styleFlags |= ES_SELECTIONBAR;
2815 } else {
2816 ed->selofs = 0;
2817 }
2818 ed->nSelectionType = stPosition;
2819
2820 ed->cPasswordMask = 0;
2821 if (props & TXTBIT_USEPASSWORD)
2822 ITextHost_TxGetPasswordChar(texthost, &ed->cPasswordMask);
2823
2824 if (props & TXTBIT_AUTOWORDSEL)
2825 ed->styleFlags |= ECO_AUTOWORDSELECTION;
2826 if (props & TXTBIT_MULTILINE) {
2827 ed->styleFlags |= ES_MULTILINE;
2828 ed->bWordWrap = (props & TXTBIT_WORDWRAP) != 0;
2829 } else {
2830 ed->bWordWrap = FALSE;
2831 }
2832 if (props & TXTBIT_READONLY)
2833 ed->styleFlags |= ES_READONLY;
2834 if (!(props & TXTBIT_HIDESELECTION))
2835 ed->styleFlags |= ES_NOHIDESEL;
2836 if (props & TXTBIT_SAVESELECTION)
2837 ed->styleFlags |= ES_SAVESEL;
2838 if (props & TXTBIT_VERTICAL)
2839 ed->styleFlags |= ES_VERTICAL;
2840 if (props & TXTBIT_DISABLEDRAG)
2841 ed->styleFlags |= ES_NOOLEDRAGDROP;
2842
2843 ed->notified_cr.cpMin = ed->notified_cr.cpMax = 0;
2844
2845 /* Default scrollbar information */
2846 ed->vert_si.cbSize = sizeof(SCROLLINFO);
2847 ed->vert_si.nMin = 0;
2848 ed->vert_si.nMax = 0;
2849 ed->vert_si.nPage = 0;
2850 ed->vert_si.nPos = 0;
2851
2852 ed->horz_si.cbSize = sizeof(SCROLLINFO);
2853 ed->horz_si.nMin = 0;
2854 ed->horz_si.nMax = 0;
2855 ed->horz_si.nPage = 0;
2856 ed->horz_si.nPos = 0;
2857
2858 ed->wheel_remain = 0;
2859
2860 OleInitialize(NULL);
2861
2862 return ed;
2863 }
2864
2865 void ME_DestroyEditor(ME_TextEditor *editor)
2866 {
2867 ME_DisplayItem *pFirst = editor->pBuffer->pFirst;
2868 ME_DisplayItem *p = pFirst, *pNext = NULL;
2869 int i;
2870
2871 ME_ClearTempStyle(editor);
2872 ME_EmptyUndoStack(editor);
2873 while(p) {
2874 pNext = p->next;
2875 ME_DestroyDisplayItem(p);
2876 p = pNext;
2877 }
2878 ME_ReleaseStyle(editor->pBuffer->pDefaultStyle);
2879 for (i=0; i<HFONT_CACHE_SIZE; i++)
2880 {
2881 if (editor->pFontCache[i].hFont)
2882 DeleteObject(editor->pFontCache[i].hFont);
2883 }
2884 if (editor->rgbBackColor != -1)
2885 DeleteObject(editor->hbrBackground);
2886 if(editor->lpOleCallback)
2887 IRichEditOleCallback_Release(editor->lpOleCallback);
2888 ITextHost_Release(editor->texthost);
2889 if (editor->reOle)
2890 {
2891 IRichEditOle_Release(editor->reOle);
2892 editor->reOle = NULL;
2893 }
2894 OleUninitialize();
2895
2896 FREE_OBJ(editor->pBuffer);
2897 FREE_OBJ(editor->pCursors);
2898
2899 FREE_OBJ(editor);
2900 }
2901
2902 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
2903 {
2904 TRACE("\n");
2905 switch (fdwReason)
2906 {
2907 case DLL_PROCESS_ATTACH:
2908 DisableThreadLibraryCalls(hinstDLL);
2909 me_heap = HeapCreate (0, 0x10000, 0);
2910 if (!ME_RegisterEditorClass(hinstDLL)) return FALSE;
2911 hLeft = LoadCursorW(hinstDLL, MAKEINTRESOURCEW(OCR_REVERSE));
2912 LookupInit();
2913 break;
2914
2915 case DLL_PROCESS_DETACH:
2916 if (lpvReserved) break;
2917 UnregisterClassW(RICHEDIT_CLASS20W, 0);
2918 UnregisterClassW(MSFTEDIT_CLASS, 0);
2919 UnregisterClassA(RICHEDIT_CLASS20A, 0);
2920 UnregisterClassA("RichEdit50A", 0);
2921 if (ME_ListBoxRegistered)
2922 UnregisterClassW(REListBox20W, 0);
2923 if (ME_ComboBoxRegistered)
2924 UnregisterClassW(REComboBox20W, 0);
2925 LookupCleanup();
2926 HeapDestroy (me_heap);
2927 release_typelib();
2928 break;
2929 }
2930 return TRUE;
2931 }
2932
2933 static inline int get_default_line_height( ME_TextEditor *editor )
2934 {
2935 int height = 0;
2936
2937 if (editor->pBuffer && editor->pBuffer->pDefaultStyle)
2938 height = editor->pBuffer->pDefaultStyle->tm.tmHeight;
2939 if (height <= 0) height = 24;
2940
2941 return height;
2942 }
2943
2944 static inline int calc_wheel_change( int *remain, int amount_per_click )
2945 {
2946 int change = amount_per_click * (float)*remain / WHEEL_DELTA;
2947 *remain -= WHEEL_DELTA * change / amount_per_click;
2948 return change;
2949 }
2950
2951 static const char * const edit_messages[] = {
2952 "EM_GETSEL",
2953 "EM_SETSEL",
2954 "EM_GETRECT",
2955 "EM_SETRECT",
2956 "EM_SETRECTNP",
2957 "EM_SCROLL",
2958 "EM_LINESCROLL",
2959 "EM_SCROLLCARET",
2960 "EM_GETMODIFY",
2961 "EM_SETMODIFY",
2962 "EM_GETLINECOUNT",
2963 "EM_LINEINDEX",
2964 "EM_SETHANDLE",
2965 "EM_GETHANDLE",
2966 "EM_GETTHUMB",
2967 "EM_UNKNOWN_BF",
2968 "EM_UNKNOWN_C0",
2969 "EM_LINELENGTH",
2970 "EM_REPLACESEL",
2971 "EM_UNKNOWN_C3",
2972 "EM_GETLINE",
2973 "EM_LIMITTEXT",
2974 "EM_CANUNDO",
2975 "EM_UNDO",
2976 "EM_FMTLINES",
2977 "EM_LINEFROMCHAR",
2978 "EM_UNKNOWN_CA",
2979 "EM_SETTABSTOPS",
2980 "EM_SETPASSWORDCHAR",
2981 "EM_EMPTYUNDOBUFFER",
2982 "EM_GETFIRSTVISIBLELINE",
2983 "EM_SETREADONLY",
2984 "EM_SETWORDBREAKPROC",
2985 "EM_GETWORDBREAKPROC",
2986 "EM_GETPASSWORDCHAR",
2987 "EM_SETMARGINS",
2988 "EM_GETMARGINS",
2989 "EM_GETLIMITTEXT",
2990 "EM_POSFROMCHAR",
2991 "EM_CHARFROMPOS",
2992 "EM_SETIMESTATUS",
2993 "EM_GETIMESTATUS"
2994 };
2995
2996 static const char * const richedit_messages[] = {
2997 "EM_CANPASTE",
2998 "EM_DISPLAYBAND",
2999 "EM_EXGETSEL",
3000 "EM_EXLIMITTEXT",
3001 "EM_EXLINEFROMCHAR",
3002 "EM_EXSETSEL",
3003 "EM_FINDTEXT",
3004 "EM_FORMATRANGE",
3005 "EM_GETCHARFORMAT",
3006 "EM_GETEVENTMASK",
3007 "EM_GETOLEINTERFACE",
3008 "EM_GETPARAFORMAT",
3009 "EM_GETSELTEXT",
3010 "EM_HIDESELECTION",
3011 "EM_PASTESPECIAL",
3012 "EM_REQUESTRESIZE",
3013 "EM_SELECTIONTYPE",
3014 "EM_SETBKGNDCOLOR",
3015 "EM_SETCHARFORMAT",
3016 "EM_SETEVENTMASK",
3017 "EM_SETOLECALLBACK",
3018 "EM_SETPARAFORMAT",
3019 "EM_SETTARGETDEVICE",
3020 "EM_STREAMIN",
3021 "EM_STREAMOUT",
3022 "EM_GETTEXTRANGE",
3023 "EM_FINDWORDBREAK",
3024 "EM_SETOPTIONS",
3025 "EM_GETOPTIONS",
3026 "EM_FINDTEXTEX",
3027 "EM_GETWORDBREAKPROCEX",
3028 "EM_SETWORDBREAKPROCEX",
3029 "EM_SETUNDOLIMIT",
3030 "EM_UNKNOWN_USER_83",
3031 "EM_REDO",
3032 "EM_CANREDO",
3033 "EM_GETUNDONAME",
3034 "EM_GETREDONAME",
3035 "EM_STOPGROUPTYPING",
3036 "EM_SETTEXTMODE",
3037 "EM_GETTEXTMODE",
3038 "EM_AUTOURLDETECT",
3039 "EM_GETAUTOURLDETECT",
3040 "EM_SETPALETTE",
3041 "EM_GETTEXTEX",
3042 "EM_GETTEXTLENGTHEX",
3043 "EM_SHOWSCROLLBAR",
3044 "EM_SETTEXTEX",
3045 "EM_UNKNOWN_USER_98",
3046 "EM_UNKNOWN_USER_99",
3047 "EM_SETPUNCTUATION",
3048 "EM_GETPUNCTUATION",
3049 "EM_SETWORDWRAPMODE",
3050 "EM_GETWORDWRAPMODE",
3051 "EM_SETIMECOLOR",
3052 "EM_GETIMECOLOR",
3053 "EM_SETIMEOPTIONS",
3054 "EM_GETIMEOPTIONS",
3055 "EM_CONVPOSITION",
3056 "EM_UNKNOWN_USER_109",
3057 "EM_UNKNOWN_USER_110",
3058 "EM_UNKNOWN_USER_111",
3059 "EM_UNKNOWN_USER_112",
3060 "EM_UNKNOWN_USER_113",
3061 "EM_UNKNOWN_USER_114",
3062 "EM_UNKNOWN_USER_115",
3063 "EM_UNKNOWN_USER_116",
3064 "EM_UNKNOWN_USER_117",
3065 "EM_UNKNOWN_USER_118",
3066 "EM_UNKNOWN_USER_119",
3067 "EM_SETLANGOPTIONS",
3068 "EM_GETLANGOPTIONS",
3069 "EM_GETIMECOMPMODE",
3070 "EM_FINDTEXTW",
3071 "EM_FINDTEXTEXW",
3072 "EM_RECONVERSION",
3073 "EM_SETIMEMODEBIAS",
3074 "EM_GETIMEMODEBIAS"
3075 };
3076
3077 static const char *
3078 get_msg_name(UINT msg)
3079 {
3080 if (msg >= EM_GETSEL && msg <= EM_CHARFROMPOS)
3081 return edit_messages[msg - EM_GETSEL];
3082 if (msg >= EM_CANPASTE && msg <= EM_GETIMEMODEBIAS)
3083 return richedit_messages[msg - EM_CANPASTE];
3084 return "";
3085 }
3086
3087 static void ME_LinkNotify(ME_TextEditor *editor, UINT msg, WPARAM wParam, LPARAM lParam)
3088 {
3089 int x,y;
3090 BOOL isExact;
3091 ME_Cursor cursor; /* The start of the clicked text. */
3092
3093 ENLINK info;
3094 x = (short)LOWORD(lParam);
3095 y = (short)HIWORD(lParam);
3096 ME_CharFromPos(editor, x, y, &cursor, &isExact);
3097 if (!isExact) return;
3098
3099 if (cursor.pRun->member.run.style->fmt.dwMask & CFM_LINK &&
3100 cursor.pRun->member.run.style->fmt.dwEffects & CFE_LINK)
3101 { /* The clicked run has CFE_LINK set */
3102 ME_DisplayItem *di;
3103
3104 info.nmhdr.hwndFrom = NULL;
3105 info.nmhdr.idFrom = 0;
3106 info.nmhdr.code = EN_LINK;
3107 info.msg = msg;
3108 info.wParam = wParam;
3109 info.lParam = lParam;
3110 cursor.nOffset = 0;
3111
3112 /* find the first contiguous run with CFE_LINK set */
3113 info.chrg.cpMin = ME_GetCursorOfs(&cursor);
3114 for (di = cursor.pRun->prev;
3115 di && di->type == diRun && (di->member.run.style->fmt.dwMask & CFM_LINK) && (di->member.run.style->fmt.dwEffects & CFE_LINK);
3116 di = di->prev)
3117 {
3118 info.chrg.cpMin -= di->member.run.len;
3119 }
3120
3121 /* find the last contiguous run with CFE_LINK set */
3122 info.chrg.cpMax = ME_GetCursorOfs(&cursor) + cursor.pRun->member.run.len;
3123 for (di = cursor.pRun->next;
3124 di && di->type == diRun && (di->member.run.style->fmt.dwMask & CFM_LINK) && (di->member.run.style->fmt.dwEffects & CFE_LINK);
3125 di = di->next)
3126 {
3127 info.chrg.cpMax += di->member.run.len;
3128 }
3129
3130 ITextHost_TxNotify(editor->texthost, info.nmhdr.code, &info);
3131 }
3132 }
3133
3134 void ME_ReplaceSel(ME_TextEditor *editor, BOOL can_undo, const WCHAR *str, int len)
3135 {
3136 int from, to, nStartCursor;
3137 ME_Style *style;
3138
3139 nStartCursor = ME_GetSelectionOfs(editor, &from, &to);
3140 style = ME_GetSelectionInsertStyle(editor);
3141 ME_InternalDeleteText(editor, &editor->pCursors[nStartCursor], to-from, FALSE);
3142 ME_InsertTextFromCursor(editor, 0, str, len, style);
3143 ME_ReleaseStyle(style);
3144 /* drop temporary style if line end */
3145 /*
3146 * FIXME question: does abc\n mean: put abc,
3147 * clear temp style, put \n? (would require a change)
3148 */
3149 if (len>0 && str[len-1] == '\n')
3150 ME_ClearTempStyle(editor);
3151 ME_CommitUndo(editor);
3152 ME_UpdateSelectionLinkAttribute(editor);
3153 if (!can_undo)
3154 ME_EmptyUndoStack(editor);
3155 ME_UpdateRepaint(editor, FALSE);
3156 }
3157
3158 static void ME_SetText(ME_TextEditor *editor, void *text, BOOL unicode)
3159 {
3160 LONG codepage = unicode ? CP_UNICODE : CP_ACP;
3161 int textLen;
3162
3163 LPWSTR wszText = ME_ToUnicode(codepage, text, &textLen);
3164
3165 if (textLen > 0)
3166 {
3167 int len = -1;
3168
3169 /* uses default style! */
3170 if (!(editor->styleFlags & ES_MULTILINE))
3171 {
3172 WCHAR *p = wszText;
3173
3174 while (*p != '\0' && *p != '\r' && *p != '\n') p++;
3175 len = p - wszText;
3176 }
3177 ME_InsertTextFromCursor(editor, 0, wszText, len, editor->pBuffer->pDefaultStyle);
3178 }
3179 ME_EndToUnicode(codepage, wszText);
3180 }
3181
3182 static LRESULT ME_WmCreate(ME_TextEditor *editor, LPARAM lParam, BOOL unicode)
3183 {
3184 CREATESTRUCTW *createW = (CREATESTRUCTW*)lParam;
3185 CREATESTRUCTA *createA = (CREATESTRUCTA*)lParam;
3186 void *text = NULL;
3187 INT max;
3188
3189 if (lParam)
3190 text = unicode ? (void*)createW->lpszName : (void*)createA->lpszName;
3191
3192 ME_SetDefaultFormatRect(editor);
3193
3194 max = (editor->styleFlags & ES_DISABLENOSCROLL) ? 1 : 0;
3195 if (~editor->styleFlags & ES_DISABLENOSCROLL || editor->styleFlags & WS_VSCROLL)
3196 ITextHost_TxSetScrollRange(editor->texthost, SB_VERT, 0, max, TRUE);
3197
3198 if (~editor->styleFlags & ES_DISABLENOSCROLL || editor->styleFlags & WS_HSCROLL)
3199 ITextHost_TxSetScrollRange(editor->texthost, SB_HORZ, 0, max, TRUE);
3200
3201 if (editor->styleFlags & ES_DISABLENOSCROLL)
3202 {
3203 if (editor->styleFlags & WS_VSCROLL)
3204 {
3205 ITextHost_TxEnableScrollBar(editor->texthost, SB_VERT, ESB_DISABLE_BOTH);
3206 ITextHost_TxShowScrollBar(editor->texthost, SB_VERT, TRUE);
3207 }
3208 if (editor->styleFlags & WS_HSCROLL)
3209 {
3210 ITextHost_TxEnableScrollBar(editor->texthost, SB_HORZ, ESB_DISABLE_BOTH);
3211 ITextHost_TxShowScrollBar(editor->texthost, SB_HORZ, TRUE);
3212 }
3213 }
3214
3215 if (text)
3216 {
3217 ME_SetText(editor, text, unicode);
3218 ME_SetCursorToStart(editor, &editor->pCursors[0]);
3219 ME_SetCursorToStart(editor, &editor->pCursors[1]);
3220 }
3221
3222 ME_CommitUndo(editor);
3223 ME_WrapMarkedParagraphs(editor);
3224 ME_MoveCaret(editor);
3225 return 0;
3226 }
3227
3228
3229 #define UNSUPPORTED_MSG(e) \
3230 case e: \
3231 FIXME(#e ": stub\n"); \
3232 *phresult = S_FALSE; \
3233 return 0;
3234
3235 /* Handle messages for windowless and windowed richedit controls.
3236 *
3237 * The LRESULT that is returned is a return value for window procs,
3238 * and the phresult parameter is the COM return code needed by the
3239 * text services interface. */
3240 LRESULT ME_HandleMessage(ME_TextEditor *editor, UINT msg, WPARAM wParam,
3241 LPARAM lParam, BOOL unicode, HRESULT* phresult)
3242 {
3243 *phresult = S_OK;
3244
3245 switch(msg) {
3246
3247 UNSUPPORTED_MSG(EM_DISPLAYBAND)
3248 UNSUPPORTED_MSG(EM_FINDWORDBREAK)
3249 UNSUPPORTED_MSG(EM_FMTLINES)
3250 UNSUPPORTED_MSG(EM_FORMATRANGE)
3251 UNSUPPORTED_MSG(EM_GETBIDIOPTIONS)
3252 UNSUPPORTED_MSG(EM_GETEDITSTYLE)
3253 UNSUPPORTED_MSG(EM_GETIMECOMPMODE)
3254 UNSUPPORTED_MSG(EM_GETIMESTATUS)
3255 UNSUPPORTED_MSG(EM_SETIMESTATUS)
3256 UNSUPPORTED_MSG(EM_GETLANGOPTIONS)
3257 UNSUPPORTED_MSG(EM_GETREDONAME)
3258 UNSUPPORTED_MSG(EM_GETTYPOGRAPHYOPTIONS)
3259 UNSUPPORTED_MSG(EM_GETUNDONAME)
3260 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX)
3261 UNSUPPORTED_MSG(EM_PASTESPECIAL)
3262 UNSUPPORTED_MSG(EM_SELECTIONTYPE)
3263 UNSUPPORTED_MSG(EM_SETBIDIOPTIONS)
3264 UNSUPPORTED_MSG(EM_SETEDITSTYLE)
3265 UNSUPPORTED_MSG(EM_SETFONTSIZE)
3266 UNSUPPORTED_MSG(EM_SETLANGOPTIONS)
3267 UNSUPPORTED_MSG(EM_SETMARGINS)
3268 UNSUPPORTED_MSG(EM_SETPALETTE)
3269 UNSUPPORTED_MSG(EM_SETTABSTOPS)
3270 UNSUPPORTED_MSG(EM_SETTYPOGRAPHYOPTIONS)
3271 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX)
3272
3273 /* Messages specific to Richedit controls */
3274
3275 case EM_STREAMIN:
3276 return ME_StreamIn(editor, wParam, (EDITSTREAM*)lParam, TRUE);
3277 case EM_STREAMOUT:
3278 return ME_StreamOut(editor, wParam, (EDITSTREAM *)lParam);
3279 case WM_GETDLGCODE:
3280 {
3281 UINT code = DLGC_WANTCHARS|DLGC_WANTTAB|DLGC_WANTARROWS;
3282
3283 if (lParam)
3284 editor->bDialogMode = TRUE;
3285 if (editor->styleFlags & ES_MULTILINE)
3286 code |= DLGC_WANTMESSAGE;
3287 if (!(editor->styleFlags & ES_SAVESEL))
3288 code |= DLGC_HASSETSEL;
3289 return code;
3290 }
3291 case EM_EMPTYUNDOBUFFER:
3292 ME_EmptyUndoStack(editor);
3293 return 0;
3294 case EM_GETSEL:
3295 {
3296 /* Note: wParam/lParam can be NULL */
3297 UINT from, to;
3298 PUINT pfrom = wParam ? (PUINT)wParam : &from;
3299 PUINT pto = lParam ? (PUINT)lParam : &to;
3300 ME_GetSelectionOfs(editor, (int *)pfrom, (int *)pto);
3301 if ((*pfrom|*pto) & 0xFFFF0000)
3302 return -1;
3303 return MAKELONG(*pfrom,*pto);
3304 }
3305 case EM_EXGETSEL:
3306 {
3307 CHARRANGE *pRange = (CHARRANGE *)lParam;
3308 ME_GetSelectionOfs(editor, &pRange->cpMin, &pRange->cpMax);
3309 TRACE("EM_EXGETSEL = (%d,%d)\n", pRange->cpMin, pRange->cpMax);
3310 return 0;
3311 }
3312 case EM_SETUNDOLIMIT:
3313 {
3314 if ((int)wParam < 0)
3315 editor->nUndoLimit = STACK_SIZE_DEFAULT;
3316 else
3317 editor->nUndoLimit = min(wParam, STACK_SIZE_MAX);
3318 /* Setting a max stack size keeps wine from getting killed
3319 for hogging memory. Windows allocates all this memory at once, so
3320 no program would realistically set a value above our maximum. */
3321 return editor->nUndoLimit;
3322 }
3323 case EM_CANUNDO:
3324 return !list_empty( &editor->undo_stack );
3325 case EM_CANREDO:
3326 return !list_empty( &editor->redo_stack );
3327 case WM_UNDO: /* FIXME: actually not the same */
3328 case EM_UNDO:
3329 return ME_Undo(editor);
3330 case EM_REDO:
3331 return ME_Redo(editor);
3332 case EM_GETOPTIONS:
3333 {
3334 /* these flags are equivalent to the ES_* counterparts */
3335 DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
3336 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN | ECO_SELECTIONBAR;
3337 DWORD settings = editor->styleFlags & mask;
3338
3339 return settings;
3340 }
3341 case EM_SETOPTIONS:
3342 {
3343 /* these flags are equivalent to ES_* counterparts, except for
3344 * ECO_AUTOWORDSELECTION that doesn't have an ES_* counterpart,
3345 * but is still stored in editor->styleFlags. */
3346 const DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
3347 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN |
3348 ECO_SELECTIONBAR | ECO_AUTOWORDSELECTION;
3349 DWORD settings = mask & editor->styleFlags;
3350 DWORD oldSettings = settings;
3351 DWORD changedSettings;
3352
3353 switch(wParam)
3354 {
3355 case ECOOP_SET:
3356 settings = lParam;
3357 break;
3358 case ECOOP_OR:
3359 settings |= lParam;
3360 break;
3361 case ECOOP_AND:
3362 settings &= lParam;
3363 break;
3364 case ECOOP_XOR:
3365 settings ^= lParam;
3366 }
3367 changedSettings = oldSettings ^ settings;
3368
3369 if (changedSettings) {
3370 editor->styleFlags = (editor->styleFlags & ~mask) | (settings & mask);
3371
3372 if (changedSettings & ECO_SELECTIONBAR)
3373 {
3374 ITextHost_TxInvalidateRect(editor->texthost, &editor->rcFormat, TRUE);
3375 if (settings & ECO_SELECTIONBAR) {
3376 assert(!editor->selofs);
3377 editor->selofs = SELECTIONBAR_WIDTH;
3378 editor->rcFormat.left += editor->selofs;
3379 } else {
3380 editor->rcFormat.left -= editor->selofs;
3381 editor->selofs = 0;
3382 }
3383 ME_RewrapRepaint(editor);
3384 }
3385
3386 if (changedSettings & settings & ECO_VERTICAL)
3387 FIXME("ECO_VERTICAL not implemented yet!\n");
3388 if (changedSettings & settings & ECO_AUTOHSCROLL)
3389 FIXME("ECO_AUTOHSCROLL not implemented yet!\n");
3390 if (changedSettings & settings & ECO_AUTOVSCROLL)
3391 FIXME("ECO_AUTOVSCROLL not implemented yet!\n");
3392 if (changedSettings & settings & ECO_NOHIDESEL)
3393 FIXME("ECO_NOHIDESEL not implemented yet!\n");
3394 if (changedSettings & settings & ECO_WANTRETURN)
3395 FIXME("ECO_WANTRETURN not implemented yet!\n");
3396 if (changedSettings & settings & ECO_AUTOWORDSELECTION)
3397 FIXME("ECO_AUTOWORDSELECTION not implemented yet!\n");
3398 }
3399
3400 return settings;
3401 }
3402 case EM_SETSEL:
3403 {
3404 return handle_EM_EXSETSEL( editor, wParam, lParam );
3405 }
3406 case EM_SETSCROLLPOS:
3407 {
3408 POINT *point = (POINT *)lParam;
3409 ME_ScrollAbs(editor, point->x, point->y);
3410 return 0;
3411 }
3412 case EM_AUTOURLDETECT:
3413 {
3414 if (wParam==1 || wParam ==0)
3415 {
3416 editor->AutoURLDetect_bEnable = (BOOL)wParam;
3417 return 0;
3418 }
3419 return E_INVALIDARG;
3420 }
3421 case EM_GETAUTOURLDETECT:
3422 {
3423 return editor->AutoURLDetect_bEnable;
3424 }
3425 case EM_EXSETSEL:
3426 {
3427 CHARRANGE range = *(CHARRANGE *)lParam;
3428
3429 return handle_EM_EXSETSEL( editor, range.cpMin, range.cpMax );
3430 }
3431 case EM_SHOWSCROLLBAR:
3432 {
3433 DWORD flags;
3434
3435 switch (wParam)
3436 {
3437 case SB_HORZ:
3438 flags = WS_HSCROLL;
3439 break;
3440 case SB_VERT:
3441 flags = WS_VSCROLL;
3442 break;
3443 case SB_BOTH:
3444 flags = WS_HSCROLL|WS_VSCROLL;
3445 break;
3446 default:
3447 return 0;
3448 }
3449
3450 if (lParam) {
3451 editor->styleFlags |= flags;
3452 if (flags & WS_HSCROLL)
3453 ITextHost_TxShowScrollBar(editor->texthost, SB_HORZ,
3454 editor->nTotalWidth > editor->sizeWindow.cx);
3455 if (flags & WS_VSCROLL)
3456 ITextHost_TxShowScrollBar(editor->texthost, SB_VERT,
3457 editor->nTotalLength > editor->sizeWindow.cy);
3458 } else {
3459 editor->styleFlags &= ~flags;
3460 ITextHost_TxShowScrollBar(editor->texthost, wParam, FALSE);
3461 }
3462 return 0;
3463 }
3464 case EM_SETTEXTEX:
3465 {
3466 LPWSTR wszText;
3467 SETTEXTEX *pStruct = (SETTEXTEX *)wParam;
3468 int from, to, len;
3469 ME_Style *style;
3470 BOOL bRtf, bUnicode, bSelection, bUTF8;
3471 int oldModify = editor->nModifyStep;
3472 static const char utf8_bom[] = {0xef, 0xbb, 0xbf};
3473
3474 if (!pStruct) return 0;
3475
3476 /* If we detect ascii rtf at the start of the string,
3477 * we know it isn't unicode. */
3478 bRtf = (lParam && (!strncmp((char *)lParam, "{\\rtf", 5) ||
3479 !strncmp((char *)lParam, "{\\urtf", 6)));
3480 bUnicode = !bRtf && pStruct->codepage == CP_UNICODE;
3481 bUTF8 = (lParam && (!strncmp((char *)lParam, utf8_bom, 3)));
3482
3483 TRACE("EM_SETTEXTEX - %s, flags %d, cp %d\n",
3484 bUnicode ? debugstr_w((LPCWSTR)lParam) : debugstr_a((LPCSTR)lParam),
3485 pStruct->flags, pStruct->codepage);
3486
3487 bSelection = (pStruct->flags & ST_SELECTION) != 0;
3488 if (bSelection) {
3489 int nStartCursor = ME_GetSelectionOfs(editor, &from, &to);
3490 style = ME_GetSelectionInsertStyle(editor);
3491 ME_InternalDeleteText(editor, &editor->pCursors[nStartCursor], to - from, FALSE);
3492 } else {
3493 ME_Cursor start;
3494 ME_SetCursorToStart(editor, &start);
3495 ME_InternalDeleteText(editor, &start, ME_GetTextLength(editor), FALSE);
3496 style = editor->pBuffer->pDefaultStyle;
3497 }
3498
3499 if (bRtf) {
3500 ME_StreamInRTFString(editor, bSelection, (char *)lParam);
3501 if (bSelection) {
3502 /* FIXME: The length returned doesn't include the rtf control
3503 * characters, only the actual text. */
3504 len = lParam ? strlen((char *)lParam) : 0;
3505 }
3506 } else {
3507 if (bUTF8 && !bUnicode) {
3508 wszText = ME_ToUnicode(CP_UTF8, (void *)(lParam+3), &len);
3509 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
3510 ME_EndToUnicode(CP_UTF8, wszText);
3511 } else {
3512 wszText = ME_ToUnicode(pStruct->codepage, (void *)lParam, &len);
3513 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
3514 ME_EndToUnicode(pStruct->codepage, wszText);
3515 }
3516 }
3517
3518 if (bSelection) {
3519 ME_ReleaseStyle(style);
3520 ME_UpdateSelectionLinkAttribute(editor);
3521 } else {
3522 ME_Cursor cursor;
3523 len = 1;
3524 ME_SetCursorToStart(editor, &cursor);
3525 ME_UpdateLinkAttribute(editor, &cursor, INT_MAX);
3526 }
3527 ME_CommitUndo(editor);
3528 if (!(pStruct->flags & ST_KEEPUNDO))
3529 {
3530 editor->nModifyStep = oldModify;
3531 ME_EmptyUndoStack(editor);
3532 }
3533 ME_UpdateRepaint(editor, FALSE);
3534 return len;
3535 }
3536 case EM_SETBKGNDCOLOR:
3537 {
3538 LRESULT lColor;
3539 if (editor->rgbBackColor != -1) {
3540 DeleteObject(editor->hbrBackground);
3541 lColor = editor->rgbBackColor;
3542 }
3543 else lColor = ITextHost_TxGetSysColor(editor->texthost, COLOR_WINDOW);
3544
3545 if (wParam)
3546 {
3547 editor->rgbBackColor = -1;
3548 editor->hbrBackground = GetSysColorBrush(COLOR_WINDOW);
3549 }
3550 else
3551 {
3552 editor->rgbBackColor = lParam;
3553 editor->hbrBackground = CreateSolidBrush(editor->rgbBackColor);
3554 }
3555 ITextHost_TxInvalidateRect(editor->texthost, NULL, TRUE);
3556 ITextHost_TxViewChange(editor->texthost, TRUE);
3557 return lColor;
3558 }
3559 case EM_GETMODIFY:
3560 return editor->nModifyStep == 0 ? 0 : -1;
3561 case EM_SETMODIFY:
3562 {
3563 if (wParam)
3564 editor->nModifyStep = 1;
3565 else
3566 editor->nModifyStep = 0;
3567
3568 return 0;
3569 }
3570 case EM_SETREADONLY:
3571 {
3572 if (wParam)
3573 editor->styleFlags |= ES_READONLY;
3574 else
3575 editor->styleFlags &= ~ES_READONLY;
3576 return 1;
3577 }
3578 case EM_SETEVENTMASK:
3579 {
3580 DWORD nOldMask = editor->nEventMask;
3581
3582 editor->nEventMask = lParam;
3583 return nOldMask;
3584 }
3585 case EM_GETEVENTMASK:
3586 return editor->nEventMask;
3587 case EM_SETCHARFORMAT:
3588 {
3589 CHARFORMAT2W buf, *p;
3590 BOOL bRepaint = TRUE;
3591 p = ME_ToCF2W(&buf, (CHARFORMAT2W *)lParam);
3592 if (p == NULL) return 0;
3593 if (wParam & SCF_ALL) {
3594 if (editor->mode & TM_PLAINTEXT) {
3595 ME_SetDefaultCharFormat(editor, p);
3596 } else {
3597 ME_Cursor start;
3598 ME_SetCursorToStart(editor, &start);
3599 ME_SetCharFormat(editor, &start, NULL, p);
3600 editor->nModifyStep = 1;
3601 }
3602 } else if (wParam & SCF_SELECTION) {
3603 if (editor->mode & TM_PLAINTEXT)
3604 return 0;
3605 if (wParam & SCF_WORD) {
3606 ME_Cursor start;
3607 ME_Cursor end = editor->pCursors[0];
3608 ME_MoveCursorWords(editor, &end, +1);
3609 start = end;
3610 ME_MoveCursorWords(editor, &start, -1);
3611 ME_SetCharFormat(editor, &start, &end, p);
3612 }
3613 bRepaint = ME_IsSelection(editor);
3614 ME_SetSelectionCharFormat(editor, p);
3615 if (bRepaint) editor->nModifyStep = 1;
3616 } else { /* SCF_DEFAULT */
3617 ME_SetDefaultCharFormat(editor, p);
3618 }
3619 ME_CommitUndo(editor);
3620 if (bRepaint)
3621 {
3622 ME_WrapMarkedParagraphs(editor);
3623 ME_UpdateScrollBar(editor);
3624 ME_Repaint(editor);
3625 }
3626 return 1;
3627 }
3628 case EM_GETCHARFORMAT:
3629 {
3630 CHARFORMAT2W tmp, *dst = (CHARFORMAT2W *)lParam;
3631 if (dst->cbSize != sizeof(CHARFORMATA) &&
3632 dst->cbSize != sizeof(CHARFORMATW) &&
3633 dst->cbSize != sizeof(CHARFORMAT2A) &&
3634 dst->cbSize != sizeof(CHARFORMAT2W))
3635 return 0;
3636 tmp.cbSize = sizeof(tmp);
3637 if (!wParam)
3638 ME_GetDefaultCharFormat(editor, &tmp);
3639 else
3640 ME_GetSelectionCharFormat(editor, &tmp);
3641 ME_CopyToCFAny(dst, &tmp);
3642 return tmp.dwMask;
3643 }
3644 case EM_SETPARAFORMAT:
3645 {
3646 BOOL result = ME_SetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
3647 ME_WrapMarkedParagraphs(editor);
3648 ME_UpdateScrollBar(editor);
3649 ME_Repaint(editor);
3650 ME_CommitUndo(editor);
3651 return result;
3652 }
3653 case EM_GETPARAFORMAT:
3654 ME_GetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
3655 return ((PARAFORMAT2 *)lParam)->dwMask;
3656 case EM_GETFIRSTVISIBLELINE:
3657 {
3658 ME_DisplayItem *p = editor->pBuffer->pFirst;
3659 int y = editor->vert_si.nPos;
3660 int ypara = 0;
3661 int count = 0;
3662 int ystart, yend;
3663 while(p) {
3664 p = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
3665 if (p->type == diTextEnd)
3666 break;
3667 if (p->type == diParagraph) {
3668 ypara = p->member.para.pt.y;
3669 continue;
3670 }
3671 ystart = ypara + p->member.row.pt.y;
3672 yend = ystart + p->member.row.nHeight;
3673 if (y < yend) {
3674 break;
3675 }
3676 count++;
3677 }
3678 return count;
3679 }
3680 case EM_HIDESELECTION:
3681 {
3682 editor->bHideSelection = (wParam != 0);
3683 ME_InvalidateSelection(editor);
3684 return 0;
3685 }
3686 case EM_LINESCROLL:
3687 {
3688 if (!(editor->styleFlags & ES_MULTILINE))
3689 return FALSE;
3690 ME_ScrollDown( editor, lParam * get_default_line_height( editor ) );
3691 return TRUE;
3692 }
3693 case WM_CLEAR:
3694 {
3695 int from, to;
3696 int nStartCursor = ME_GetSelectionOfs(editor, &from, &to);
3697 ME_InternalDeleteText(editor, &editor->pCursors[nStartCursor], to-from, FALSE);
3698 ME_CommitUndo(editor);
3699 ME_UpdateRepaint(editor, TRUE);
3700 return 0;
3701 }
3702 case EM_REPLACESEL:
3703 {
3704 int len = 0;
3705 LONG codepage = unicode ? CP_UNICODE : CP_ACP;
3706 LPWSTR wszText = ME_ToUnicode(codepage, (void *)lParam, &len);
3707
3708 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText));
3709
3710 ME_ReplaceSel(editor, !!wParam, wszText, len);
3711 ME_EndToUnicode(codepage, wszText);
3712 return len;
3713 }
3714 case EM_SCROLLCARET:
3715 ME_EnsureVisible(editor, &editor->pCursors[0]);
3716 return 0;
3717 case WM_SETFONT:
3718 {
3719 LOGFONTW lf;
3720 CHARFORMAT2W fmt;
3721 HDC hDC;
3722 BOOL bRepaint = LOWORD(lParam);
3723
3724 if (!wParam)
3725 wParam = (WPARAM)GetStockObject(SYSTEM_FONT);
3726 GetObjectW((HGDIOBJ)wParam, sizeof(LOGFONTW), &lf);
3727 hDC = ITextHost_TxGetDC(editor->texthost);
3728 ME_CharFormatFromLogFont(hDC, &lf, &fmt);
3729 ITextHost_TxReleaseDC(editor->texthost, hDC);
3730 if (editor->mode & TM_RICHTEXT) {
3731 ME_Cursor start;
3732 ME_SetCursorToStart(editor, &start);
3733 ME_SetCharFormat(editor, &start, NULL, &fmt);
3734 }
3735 ME_SetDefaultCharFormat(editor, &fmt);
3736
3737 ME_CommitUndo(editor);
3738 ME_MarkAllForWrapping(editor);
3739 ME_WrapMarkedParagraphs(editor);
3740 ME_UpdateScrollBar(editor);
3741 if (bRepaint)
3742 ME_Repaint(editor);
3743 return 0;
3744 }
3745 case WM_SETTEXT:
3746 {
3747 ME_Cursor cursor;
3748 ME_SetCursorToStart(editor, &cursor);
3749 ME_InternalDeleteText(editor, &cursor, ME_GetTextLength(editor), FALSE);
3750 if (lParam)
3751 {
3752 TRACE("WM_SETTEXT lParam==%lx\n",lParam);
3753 if (!strncmp((char *)lParam, "{\\rtf", 5) ||
3754 !strncmp((char *)lParam, "{\\urtf", 6))
3755 {
3756 /* Undocumented: WM_SETTEXT supports RTF text */
3757 ME_StreamInRTFString(editor, 0, (char *)lParam);
3758 }
3759 else
3760 ME_SetText(editor, (void*)lParam, unicode);
3761 }
3762 else
3763 TRACE("WM_SETTEXT - NULL\n");
3764 ME_SetCursorToStart(editor, &cursor);
3765 ME_UpdateLinkAttribute(editor, &cursor, INT_MAX);
3766 ME_SetSelection(editor, 0, 0);
3767 editor->nModifyStep = 0;
3768 ME_CommitUndo(editor);
3769 ME_EmptyUndoStack(editor);
3770 ME_UpdateRepaint(editor, FALSE);
3771 return 1;
3772 }
3773 case EM_CANPASTE:
3774 {
3775 UINT nRTFFormat = RegisterClipboardFormatA("Rich Text Format");
3776 if (IsClipboardFormatAvailable(nRTFFormat))
3777 return TRUE;
3778 if (IsClipboardFormatAvailable(CF_UNICODETEXT))
3779 return TRUE;
3780 return FALSE;
3781 }
3782 case WM_PASTE:
3783 case WM_MBUTTONDOWN:
3784 ME_Paste(editor);
3785 return 0;
3786 case WM_CUT:
3787 case WM_COPY:
3788 {
3789 int nFrom, nTo, nStartCur = ME_GetSelectionOfs(editor, &nFrom, &nTo);
3790 int nChars = nTo - nFrom;
3791 ME_Cursor *selStart = &editor->pCursors[nStartCur];
3792
3793 if (ME_Copy(editor, selStart, nChars) && msg == WM_CUT)
3794 {
3795 ME_InternalDeleteText(editor, selStart, nChars, FALSE);
3796 ME_CommitUndo(editor);
3797 ME_UpdateRepaint(editor, TRUE);
3798 }
3799 return 0;
3800 }
3801 case WM_GETTEXTLENGTH:
3802 {
3803 GETTEXTLENGTHEX how;
3804
3805 /* CR/LF conversion required in 2.0 mode, verbatim in 1.0 mode */
3806 how.flags = GTL_CLOSE | (editor->bEmulateVersion10 ? 0 : GTL_USECRLF) | GTL_NUMCHARS;
3807 how.codepage = unicode ? CP_UNICODE : CP_ACP;
3808 return ME_GetTextLengthEx(editor, &how);
3809 }
3810 case EM_GETTEXTLENGTHEX:
3811 return ME_GetTextLengthEx(editor, (GETTEXTLENGTHEX *)wParam);
3812 case WM_GETTEXT:
3813 {
3814 GETTEXTEX ex;
3815 ex.cb = wParam * (unicode ? sizeof(WCHAR) : sizeof(CHAR));
3816 ex.flags = GT_USECRLF;
3817 ex.codepage = unicode ? CP_UNICODE : CP_ACP;
3818 ex.lpDefaultChar = NULL;
3819 ex.lpUsedDefChar = NULL;
3820 return ME_GetTextEx(editor, &ex, lParam);
3821 }
3822 case EM_GETTEXTEX:
3823 return ME_GetTextEx(editor, (GETTEXTEX*)wParam, lParam);
3824 case EM_GETSELTEXT:
3825 {
3826 int nFrom, nTo, nStartCur = ME_GetSelectionOfs(editor, &nFrom, &nTo);
3827 ME_Cursor *from = &editor->pCursors[nStartCur];
3828 return ME_GetTextRange(editor, (WCHAR *)lParam, from,
3829 nTo - nFrom, unicode);
3830 }
3831 case EM_GETSCROLLPOS:
3832 {
3833 POINT *point = (POINT *)lParam;
3834 point->x = editor->horz_si.nPos;
3835 point->y = editor->vert_si.nPos;
3836 /* 16-bit scaled value is returned as stored in scrollinfo */
3837 if (editor->horz_si.nMax > 0xffff)
3838 point->x = MulDiv(point->x, 0xffff, editor->horz_si.nMax);
3839 if (editor->vert_si.nMax > 0xffff)
3840 point->y = MulDiv(point->y, 0xffff, editor->vert_si.nMax);
3841 return 1;
3842 }
3843 case EM_GETTEXTRANGE:
3844 {
3845 TEXTRANGEW *rng = (TEXTRANGEW *)lParam;
3846 ME_Cursor start;
3847 int nStart = rng->chrg.cpMin;
3848 int nEnd = rng->chrg.cpMax;
3849 int textlength = ME_GetTextLength(editor);
3850
3851 TRACE("EM_GETTEXTRANGE min=%d max=%d unicode=%d textlength=%d\n",
3852 rng->chrg.cpMin, rng->chrg.cpMax, unicode, textlength);
3853 if (nStart < 0) return 0;
3854 if ((nStart == 0 && nEnd == -1) || nEnd > textlength)
3855 nEnd = textlength;
3856 if (nStart >= nEnd) return 0;
3857
3858 ME_CursorFromCharOfs(editor, nStart, &start);
3859 return ME_GetTextRange(editor, rng->lpstrText, &start, nEnd - nStart, unicode);
3860 }
3861 case EM_GETLINE:
3862 {
3863 ME_DisplayItem *run;
3864 const unsigned int nMaxChars = *(WORD *) lParam;
3865 unsigned int nCharsLeft = nMaxChars;
3866 char *dest = (char *) lParam;
3867 BOOL wroteNull = FALSE;
3868
3869 TRACE("EM_GETLINE: row=%d, nMaxChars=%d (%s)\n", (int) wParam, nMaxChars,
3870 unicode ? "Unicode" : "Ansi");
3871
3872 run = ME_FindRowWithNumber(editor, wParam);
3873 if (run == NULL)
3874 return 0;
3875
3876 while (nCharsLeft && (run = ME_FindItemFwd(run, diRunOrStartRow))
3877 && run->type == diRun)
3878 {
3879 WCHAR *str = get_text( &run->member.run, 0 );
3880 unsigned int nCopy;
3881
3882 nCopy = min(nCharsLeft, run->member.run.len);
3883
3884 if (unicode)
3885 memcpy(dest, str, nCopy * sizeof(WCHAR));
3886 else
3887 nCopy = WideCharToMultiByte(CP_ACP, 0, str, nCopy, dest,
3888 nCharsLeft, NULL, NULL);
3889 dest += nCopy * (unicode ? sizeof(WCHAR) : 1);
3890 nCharsLeft -= nCopy;
3891 }
3892
3893 /* append line termination, space allowing */
3894 if (nCharsLeft > 0)
3895 {
3896 if (unicode)
3897 *((WCHAR *)dest) = '\0';
3898 else
3899 *dest = '\0';
3900 nCharsLeft--;
3901 wroteNull = TRUE;
3902 }
3903
3904 TRACE("EM_GETLINE: got %u characters\n", nMaxChars - nCharsLeft);
3905 return nMaxChars - nCharsLeft - (wroteNull ? 1 : 0);
3906 }
3907 case EM_GETLINECOUNT:
3908 {
3909 ME_DisplayItem *item = editor->pBuffer->pFirst->next;
3910 int nRows = 0;
3911
3912 ME_DisplayItem *prev_para = NULL, *last_para = NULL;
3913
3914 while (item != editor->pBuffer->pLast)
3915 {
3916 assert(item->type == diParagraph);
3917 prev_para = ME_FindItemBack(item, diRun);
3918 if (prev_para) {
3919 assert(prev_para->member.run.nFlags & MERF_ENDPARA);
3920 }
3921 nRows += item->member.para.nRows;
3922 item = item->member.para.next_para;
3923 }
3924 last_para = ME_FindItemBack(item, diRun);
3925 assert(last_para);
3926 assert(last_para->member.run.nFlags & MERF_ENDPARA);
3927 if (editor->bEmulateVersion10 && prev_para &&
3928 last_para->member.run.nCharOfs == 0 &&
3929 prev_para->member.run.len == 1 &&
3930 *get_text( &prev_para->member.run, 0 ) == '\r')
3931 {
3932 /* In 1.0 emulation, the last solitary \r at the very end of the text
3933 (if one exists) is NOT a line break.
3934 FIXME: this is an ugly hack. This should have a more regular model. */
3935 nRows--;
3936 }
3937
3938 TRACE("EM_GETLINECOUNT: nRows==%d\n", nRows);
3939 return max(1, nRows);
3940 }
3941 case EM_LINEFROMCHAR:
3942 {
3943 if (wParam == -1)
3944 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(&editor->pCursors[1]));
3945 else
3946 return ME_RowNumberFromCharOfs(editor, wParam);
3947 }
3948 case EM_EXLINEFROMCHAR:
3949 {
3950 if (lParam == -1)
3951 return ME_RowNumberFromCharOfs(editor, ME_GetCursorOfs(&editor->pCursors[1]));
3952 else
3953 return ME_RowNumberFromCharOfs(editor, lParam);
3954 }
3955 case EM_LINEINDEX:
3956 {
3957 ME_DisplayItem *item, *para;
3958 int nCharOfs;
3959
3960 if (wParam == -1)
3961 item = ME_FindItemBack(editor->pCursors[0].pRun, diStartRow);
3962 else
3963 item = ME_FindRowWithNumber(editor, wParam);
3964 if (!item)
3965 return -1;
3966 para = ME_GetParagraph(item);
3967 item = ME_FindItemFwd(item, diRun);
3968 nCharOfs = para->member.para.nCharOfs + item->member.run.nCharOfs;
3969 TRACE("EM_LINEINDEX: nCharOfs==%d\n", nCharOfs);
3970 return nCharOfs;
3971 }
3972 case EM_LINELENGTH:
3973 {
3974 ME_DisplayItem *item, *item_end;
3975 int nChars = 0, nThisLineOfs = 0, nNextLineOfs = 0;
3976 ME_DisplayItem *para, *run;
3977
3978 if (wParam > ME_GetTextLength(editor))
3979 return 0;
3980 if (wParam == -1)
3981 {
3982 FIXME("EM_LINELENGTH: returning number of unselected characters on lines with selection unsupported.\n");
3983 return 0;
3984 }
3985 ME_RunOfsFromCharOfs(editor, wParam, &para, &run, NULL);
3986 item = ME_RowStart(run);
3987 nThisLineOfs = ME_CharOfsFromRunOfs(editor, para, ME_FindItemFwd(item, diRun), 0);
3988 item_end = ME_FindItemFwd(item, diStartRowOrParagraphOrEnd);
3989 if (item_end->type == diStartRow) {
3990 nNextLineOfs = ME_CharOfsFromRunOfs(editor, para, ME_FindItemFwd(item_end, diRun), 0);
3991 } else {
3992 ME_DisplayItem *endRun = ME_FindItemBack(item_end, diRun);
3993 assert(endRun && endRun->member.run.nFlags & MERF_ENDPARA);
3994 nNextLineOfs = item_end->member.para.nCharOfs - endRun->member.run.len;
3995 }
3996 nChars = nNextLineOfs - nThisLineOfs;
3997 TRACE("EM_LINELENGTH(%ld)==%d\n",wParam, nChars);
3998 return nChars;
3999 }
4000 case EM_EXLIMITTEXT:
4001 {
4002 if ((int)lParam < 0)
4003 return 0;
4004 if (lParam == 0)
4005 editor->nTextLimit = 65536;
4006 else
4007 editor->nTextLimit = (int) lParam;
4008 return 0;
4009 }
4010 case EM_LIMITTEXT:
4011 {
4012 if (wParam == 0)
4013 editor->nTextLimit = 65536;
4014 else
4015 editor->nTextLimit = (int) wParam;
4016 return 0;
4017 }
4018 case EM_GETLIMITTEXT:
4019 {
4020 return editor->nTextLimit;
4021 }
4022 case EM_FINDTEXT:
4023 {
4024 LRESULT r;
4025 if(!unicode){
4026 FINDTEXTA *ft = (FINDTEXTA *)lParam;
4027 int nChars = MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, NULL, 0);
4028 WCHAR *tmp;
4029
4030 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
4031 MultiByteToWideChar(CP_ACP, 0, ft->lpstrText, -1, tmp, nChars);
4032 r = ME_FindText(editor, wParam, &ft->chrg, tmp, NULL);
4033 FREE_OBJ( tmp );
4034 }else{
4035 FINDTEXTW *ft = (FINDTEXTW *)lParam;
4036 r = ME_FindText(editor, wParam, &ft->chrg, ft->lpstrText, NULL);
4037 }
4038 return r;
4039 }
4040 case EM_FINDTEXTEX:
4041 {
4042 LRESULT r;
4043 if(!unicode){
4044 FINDTEXTEXA *ex = (FINDTEXTEXA *)lParam;
4045 int nChars = MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, NULL, 0);
4046 WCHAR *tmp;
4047
4048 if ((tmp = ALLOC_N_OBJ(WCHAR, nChars)) != NULL)
4049 MultiByteToWideChar(CP_ACP, 0, ex->lpstrText, -1, tmp, nChars);
4050 r = ME_FindText(editor, wParam, &ex->chrg, tmp, &ex->chrgText);
4051 FREE_OBJ( tmp );
4052 }else{
4053 FINDTEXTEXW *ex = (FINDTEXTEXW *)lParam;
4054 r = ME_FindText(editor, wParam, &ex->chrg, ex->lpstrText, &ex->chrgText);
4055 }
4056 return r;
4057 }
4058 case EM_FINDTEXTW:
4059 {
4060 FINDTEXTW *ft = (FINDTEXTW *)lParam;
4061 return ME_FindText(editor, wParam, &ft->chrg, ft->lpstrText, NULL);
4062 }
4063 case EM_FINDTEXTEXW:
4064 {
4065 FINDTEXTEXW *ex = (FINDTEXTEXW *)lParam;
4066 return ME_FindText(editor, wParam, &ex->chrg, ex->lpstrText, &ex->chrgText);
4067 }
4068 case EM_GETZOOM:
4069 if (!wParam || !lParam)
4070 return FALSE;
4071 *(int *)wParam = editor->nZoomNumerator;
4072 *(int *)lParam = editor->nZoomDenominator;
4073 return TRUE;
4074 case EM_SETZOOM:
4075 return ME_SetZoom(editor, wParam, lParam);
4076 case EM_CHARFROMPOS:
4077 {
4078 ME_Cursor cursor;
4079 if (ME_CharFromPos(editor, ((POINTL *)lParam)->x, ((POINTL *)lParam)->y,
4080 &cursor, NULL))
4081 return ME_GetCursorOfs(&cursor);
4082 else
4083 return -1;
4084 }
4085 case EM_POSFROMCHAR:
4086 {
4087 ME_DisplayItem *pPara, *pRun;
4088 int nCharOfs, nOffset, nLength;
4089 POINTL pt = {0,0};
4090
4091 nCharOfs = wParam;
4092 /* detect which API version we're dealing with */
4093 if (wParam >= 0x40000)
4094 nCharOfs = lParam;
4095 nLength = ME_GetTextLength(editor);
4096 nCharOfs = min(nCharOfs, nLength);
4097 nCharOfs = max(nCharOfs, 0);
4098
4099 ME_RunOfsFromCharOfs(editor, nCharOfs, &pPara, &pRun, &nOffset);
4100 assert(pRun->type == diRun);
4101 pt.y = pRun->member.run.pt.y;
4102 pt.x = pRun->member.run.pt.x + ME_PointFromChar(editor, &pRun->member.run, nOffset, TRUE);
4103 pt.y += pPara->member.para.pt.y + editor->rcFormat.top;
4104 pt.x += editor->rcFormat.left;
4105
4106 pt.x -= editor->horz_si.nPos;
4107 pt.y -= editor->vert_si.nPos;
4108
4109 if (wParam >= 0x40000) {
4110 *(POINTL *)wParam = pt;
4111 }
4112 return (wParam >= 0x40000) ? 0 : MAKELONG( pt.x, pt.y );
4113 }
4114 case WM_CREATE:
4115 return ME_WmCreate(editor, lParam, unicode);
4116 case WM_DESTROY:
4117 ME_DestroyEditor(editor);
4118 return 0;
4119 case WM_SETCURSOR:
4120 {
4121 POINT cursor_pos;
4122 if (wParam == (WPARAM)editor->hWnd && GetCursorPos(&cursor_pos) &&
4123 ScreenToClient(editor->hWnd, &cursor_pos))
4124 ME_LinkNotify(editor, msg, 0, MAKELPARAM(cursor_pos.x, cursor_pos.y));
4125 return ME_SetCursor(editor);
4126 }
4127 case WM_LBUTTONDBLCLK:
4128 case WM_LBUTTONDOWN:
4129 {
4130 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
4131 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
4132 !ME_FilterEvent(editor, msg, &wParam, &lParam))
4133 return 0;
4134 ITextHost_TxSetFocus(editor->texthost);
4135 ME_LButtonDown(editor, (short)LOWORD(lParam), (short)HIWORD(lParam),
4136 ME_CalculateClickCount(editor, msg, wParam, lParam));
4137 ITextHost_TxSetCapture(editor->texthost, TRUE);
4138 editor->bMouseCaptured = TRUE;
4139 ME_LinkNotify(editor, msg, wParam, lParam);
4140 if (!ME_SetCursor(editor)) goto do_default;
4141 break;
4142 }
4143 case WM_MOUSEMOVE:
4144 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
4145 !ME_FilterEvent(editor, msg, &wParam, &lParam))
4146 return 0;
4147 if (editor->bMouseCaptured)
4148 ME_MouseMove(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
4149 else
4150 ME_LinkNotify(editor, msg, wParam, lParam);
4151 /* Set cursor if mouse is captured, since WM_SETCURSOR won't be received. */
4152 if (editor->bMouseCaptured)
4153 ME_SetCursor(editor);
4154 break;
4155 case WM_LBUTTONUP:
4156 if (editor->bMouseCaptured) {
4157 ITextHost_TxSetCapture(editor->texthost, FALSE);
4158 editor->bMouseCaptured = FALSE;
4159 }
4160 if (editor->nSelectionType == stDocument)
4161 editor->nSelectionType = stPosition;
4162 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
4163 !ME_FilterEvent(editor, msg, &wParam, &lParam))
4164 return 0;
4165 else
4166 {
4167 ME_SetCursor(editor);
4168 ME_LinkNotify(editor, msg, wParam, lParam);
4169 }
4170 break;
4171 case WM_RBUTTONUP:
4172 case WM_RBUTTONDOWN:
4173 case WM_RBUTTONDBLCLK:
4174 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
4175 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
4176 !ME_FilterEvent(editor, msg, &wParam, &lParam))
4177 return 0;
4178 ME_LinkNotify(editor, msg, wParam, lParam);
4179 goto do_default;
4180 case WM_CONTEXTMENU:
4181 if (!ME_ShowContextMenu(editor, (short)LOWORD(lParam), (short)HIWORD(lParam)))
4182 goto do_default;
4183 break;
4184 case WM_SETFOCUS:
4185 editor->bHaveFocus = TRUE;
4186 ME_ShowCaret(editor);
4187 ME_SendOldNotify(editor, EN_SETFOCUS);
4188 return 0;
4189 case WM_KILLFOCUS:
4190 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
4191 editor->bHaveFocus = FALSE;
4192 editor->wheel_remain = 0;
4193 ME_HideCaret(editor);
4194 ME_SendOldNotify(editor, EN_KILLFOCUS);
4195 return 0;
4196 case WM_COMMAND:
4197 TRACE("editor wnd command = %d\n", LOWORD(wParam));
4198 return 0;
4199 case WM_KEYUP:
4200 if ((editor->nEventMask & ENM_KEYEVENTS) &&
4201 !ME_FilterEvent(editor, msg, &wParam, &lParam))
4202 return 0;
4203 goto do_default;
4204 case WM_KEYDOWN:
4205 if ((editor->nEventMask & ENM_KEYEVENTS) &&
4206 !ME_FilterEvent(editor, msg, &wParam, &lParam))
4207 return 0;
4208 if (ME_KeyDown(editor, LOWORD(wParam)))
4209 return 0;
4210 goto do_default;
4211 case WM_CHAR:
4212 if ((editor->nEventMask & ENM_KEYEVENTS) &&
4213 !ME_FilterEvent(editor, msg, &wParam, &lParam))
4214 return 0;
4215 return ME_Char(editor, wParam, lParam, unicode);
4216 case WM_UNICHAR:
4217 if (unicode)
4218 {
4219 if(wParam == UNICODE_NOCHAR) return TRUE;
4220 if(wParam <= 0x000fffff)
4221 {
4222 if(wParam > 0xffff) /* convert to surrogates */
4223 {
4224 wParam -= 0x10000;
4225 ME_Char(editor, (wParam >> 10) + 0xd800, 0, TRUE);
4226 ME_Char(editor, (wParam & 0x03ff) + 0xdc00, 0, TRUE);
4227 } else {
4228 ME_Char(editor, wParam, 0, TRUE);
4229 }
4230 }
4231 return 0;
4232 }
4233 break;
4234 case EM_STOPGROUPTYPING:
4235 ME_CommitUndo(editor); /* End coalesced undos for typed characters */
4236 return 0;
4237 case WM_HSCROLL:
4238 {
4239 const int scrollUnit = 7;
4240
4241 switch(LOWORD(wParam))
4242 {
4243 case SB_LEFT:
4244 ME_ScrollAbs(editor, 0, 0);
4245 break;
4246 case SB_RIGHT:
4247 ME_ScrollAbs(editor,
4248 editor->horz_si.nMax - (int)editor->horz_si.nPage,
4249 editor->vert_si.nMax - (int)editor->vert_si.nPage);
4250 break;
4251 case SB_LINELEFT:
4252 ME_ScrollLeft(editor, scrollUnit);
4253 break;
4254 case SB_LINERIGHT:
4255 ME_ScrollRight(editor, scrollUnit);
4256 break;
4257 case SB_PAGELEFT:
4258 ME_ScrollLeft(editor, editor->sizeWindow.cx);
4259 break;
4260 case SB_PAGERIGHT:
4261 ME_ScrollRight(editor, editor->sizeWindow.cx);
4262 break;
4263 case SB_THUMBTRACK:
4264 case SB_THUMBPOSITION:
4265 {
4266 int pos = HIWORD(wParam);
4267 if (editor->horz_si.nMax > 0xffff)
4268 pos = MulDiv(pos, editor->horz_si.nMax, 0xffff);
4269 ME_HScrollAbs(editor, pos);
4270 break;
4271 }
4272 }
4273 break;
4274 }
4275 case EM_SCROLL: /* fall through */
4276 case WM_VSCROLL:
4277 {
4278 int origNPos;
4279 int lineHeight = get_default_line_height( editor );
4280
4281 origNPos = editor->vert_si.nPos;
4282
4283 switch(LOWORD(wParam))
4284 {
4285 case SB_TOP:
4286 ME_ScrollAbs(editor, 0, 0);
4287 break;
4288 case SB_BOTTOM:
4289 ME_ScrollAbs(editor,
4290 editor->horz_si.nMax - (int)editor->horz_si.nPage,
4291 editor->vert_si.nMax - (int)editor->vert_si.nPage);
4292 break;
4293 case SB_LINEUP:
4294 ME_ScrollUp(editor,lineHeight);
4295 break;
4296 case SB_LINEDOWN:
4297 ME_ScrollDown(editor,lineHeight);
4298 break;
4299 case SB_PAGEUP:
4300 ME_ScrollUp(editor,editor->sizeWindow.cy);
4301 break;
4302 case SB_PAGEDOWN:
4303 ME_ScrollDown(editor,editor->sizeWindow.cy);
4304 break;
4305 case SB_THUMBTRACK:
4306 case SB_THUMBPOSITION:
4307 {
4308 int pos = HIWORD(wParam);
4309 if (editor->vert_si.nMax > 0xffff)
4310 pos = MulDiv(pos, editor->vert_si.nMax, 0xffff);
4311 ME_VScrollAbs(editor, pos);
4312 break;
4313 }
4314 }
4315 if (msg == EM_SCROLL)
4316 return 0x00010000 | (((editor->vert_si.nPos - origNPos)/lineHeight) & 0xffff);
4317 break;
4318 }
4319 case WM_MOUSEWHEEL:
4320 {
4321 int delta;
4322 BOOL ctrl_is_down;
4323
4324 if ((editor->nEventMask & ENM_MOUSEEVENTS) &&
4325 !ME_FilterEvent(editor, msg, &wParam, &lParam))
4326 return 0;
4327
4328 ctrl_is_down = GetKeyState(VK_CONTROL) & 0x8000;
4329
4330 delta = GET_WHEEL_DELTA_WPARAM(wParam);
4331
4332 /* if scrolling changes direction, ignore left overs */
4333 if ((delta < 0 && editor->wheel_remain < 0) ||
4334 (delta > 0 && editor->wheel_remain > 0))
4335 editor->wheel_remain += delta;
4336 else
4337 editor->wheel_remain = delta;
4338
4339 if (editor->wheel_remain)
4340 {
4341 if (ctrl_is_down) {
4342 int numerator;
4343 if (!editor->nZoomNumerator || !editor->nZoomDenominator)
4344 {
4345 numerator = 100;
4346 } else {
4347 numerator = editor->nZoomNumerator * 100 / editor->nZoomDenominator;
4348 }
4349 numerator += calc_wheel_change( &editor->wheel_remain, 10 );
4350 if (numerator >= 10 && numerator <= 500)
4351 ME_SetZoom(editor, numerator, 100);
4352 } else {
4353 UINT max_lines = 3;
4354 int lines = 0;
4355
4356 SystemParametersInfoW( SPI_GETWHEELSCROLLLINES, 0, &max_lines, 0 );
4357 if (max_lines)
4358 lines = calc_wheel_change( &editor->wheel_remain, (int)max_lines );
4359 if (lines)
4360 ME_ScrollDown( editor, -lines * get_default_line_height( editor ) );
4361 }
4362 }
4363 break;
4364 }
4365 case EM_GETRECT:
4366 {
4367 *((RECT *)lParam) = editor->rcFormat;
4368 if (editor->bDefaultFormatRect)
4369 ((RECT *)lParam)->left -= editor->selofs;
4370 return 0;
4371 }
4372 case EM_SETRECT:
4373 case EM_SETRECTNP:
4374 {
4375 if (lParam)
4376 {
4377 int border = 0;
4378 RECT clientRect;
4379 RECT *rc = (RECT *)lParam;
4380
4381 border = editor->exStyleFlags & WS_EX_CLIENTEDGE ? 1 : 0;
4382 ITextHost_TxGetClientRect(editor->texthost, &clientRect);
4383 if (wParam == 0)
4384 {
4385 editor->rcFormat.top = max(0, rc->top - border);
4386 editor->rcFormat.left = max(0, rc->left - border);
4387 editor->rcFormat.bottom = min(clientRect.bottom, rc->bottom);
4388 editor->rcFormat.right = min(clientRect.right, rc->right + border);
4389 } else if (wParam == 1) {
4390 /* MSDN incorrectly says a wParam value of 1 causes the
4391 * lParam rect to be used as a relative offset,
4392 * however, the tests show it just prevents min/max bound
4393 * checking. */
4394 editor->rcFormat.top = rc->top - border;
4395 editor->rcFormat.left = rc->left - border;
4396 editor->rcFormat.bottom = rc->bottom;
4397 editor->rcFormat.right = rc->right + border;
4398 } else {
4399 return 0;
4400 }
4401 editor->bDefaultFormatRect = FALSE;
4402 }
4403 else
4404 {
4405 ME_SetDefaultFormatRect(editor);
4406 editor->bDefaultFormatRect = TRUE;
4407 }
4408 ME_MarkAllForWrapping(editor);
4409 ME_WrapMarkedParagraphs(editor);
4410 ME_UpdateScrollBar(editor);
4411 if (msg != EM_SETRECTNP)
4412 ME_Repaint(editor);
4413 return 0;
4414 }
4415 case EM_REQUESTRESIZE:
4416 ME_SendRequestResize(editor, TRUE);
4417 return 0;
4418 case WM_SETREDRAW:
4419 goto do_default;
4420 case WM_WINDOWPOSCHANGED:
4421 {
4422 RECT clientRect;
4423 WINDOWPOS *winpos = (WINDOWPOS *)lParam;
4424
4425 if (winpos->flags & SWP_NOCLIENTSIZE) goto do_default;
4426 ITextHost_TxGetClientRect(editor->texthost, &clientRect);
4427 if (editor->bDefaultFormatRect) {
4428 ME_SetDefaultFormatRect(editor);
4429 } else {
4430 editor->rcFormat.right += clientRect.right - editor->prevClientRect.right;
4431 editor->rcFormat.bottom += clientRect.bottom - editor->prevClientRect.bottom;
4432 }
4433 editor->prevClientRect = clientRect;
4434 ME_RewrapRepaint(editor);
4435 goto do_default;
4436 }
4437 /* IME messages to make richedit controls IME aware */
4438 case WM_IME_SETCONTEXT:
4439 case WM_IME_CONTROL:
4440 case WM_IME_SELECT:
4441 case WM_IME_COMPOSITIONFULL:
4442 return 0;
4443 case WM_IME_STARTCOMPOSITION:
4444 {
4445 editor->imeStartIndex=ME_GetCursorOfs(&editor->pCursors[0]);
4446 ME_DeleteSelection(editor);
4447 ME_CommitUndo(editor);
4448 ME_UpdateRepaint(editor, FALSE);
4449 return 0;
4450 }
4451 case WM_IME_COMPOSITION:
4452 {
4453 HIMC hIMC;
4454
4455 ME_Style *style = ME_GetInsertStyle(editor, 0);
4456 hIMC = ITextHost_TxImmGetContext(editor->texthost);
4457 ME_DeleteSelection(editor);
4458 ME_SaveTempStyle(editor);
4459 if (lParam & (GCS_RESULTSTR|GCS_COMPSTR))
4460 {
4461 LPWSTR lpCompStr = NULL;
4462 DWORD dwBufLen;
4463 DWORD dwIndex = lParam & GCS_RESULTSTR;
4464 if (!dwIndex)
4465 dwIndex = GCS_COMPSTR;
4466
4467 dwBufLen = ImmGetCompositionStringW(hIMC, dwIndex, NULL, 0);
4468 lpCompStr = HeapAlloc(GetProcessHeap(),0,dwBufLen + sizeof(WCHAR));
4469 ImmGetCompositionStringW(hIMC, dwIndex, lpCompStr, dwBufLen);
4470 lpCompStr[dwBufLen/sizeof(WCHAR)] = 0;
4471 ME_InsertTextFromCursor(editor,0,lpCompStr,dwBufLen/sizeof(WCHAR),style);
4472 HeapFree(GetProcessHeap(), 0, lpCompStr);
4473
4474 if (dwIndex == GCS_COMPSTR)
4475 ME_SetSelection(editor,editor->imeStartIndex,
4476 editor->imeStartIndex + dwBufLen/sizeof(WCHAR));
4477 }
4478 ME_ReleaseStyle(style);
4479 ME_CommitUndo(editor);
4480 ME_UpdateRepaint(editor, FALSE);
4481 return 0;
4482 }
4483 case WM_IME_ENDCOMPOSITION:
4484 {
4485 ME_DeleteSelection(editor);
4486 editor->imeStartIndex=-1;
4487 return 0;
4488 }
4489 case EM_GETOLEINTERFACE:
4490 {
4491 if (!editor->reOle)
4492 if (!CreateIRichEditOle(NULL, editor, (LPVOID *)&editor->reOle))
4493 return 0;
4494 *(LPVOID *)lParam = editor->reOle;
4495 IRichEditOle_AddRef(editor->reOle);
4496 return 1;
4497 }
4498 case EM_GETPASSWORDCHAR:
4499 {
4500 return editor->cPasswordMask;
4501 }
4502 case EM_SETOLECALLBACK:
4503 if(editor->lpOleCallback)
4504 IRichEditOleCallback_Release(editor->lpOleCallback);
4505 editor->lpOleCallback = (IRichEditOleCallback*)lParam;
4506 if(editor->lpOleCallback)
4507 IRichEditOleCallback_AddRef(editor->lpOleCallback);
4508 return TRUE;
4509 case EM_GETWORDBREAKPROC:
4510 return (LRESULT)editor->pfnWordBreak;
4511 case EM_SETWORDBREAKPROC:
4512 {
4513 EDITWORDBREAKPROCW pfnOld = editor->pfnWordBreak;
4514
4515 editor->pfnWordBreak = (EDITWORDBREAKPROCW)lParam;
4516 return (LRESULT)pfnOld;
4517 }
4518 case EM_GETTEXTMODE:
4519 return editor->mode;
4520 case EM_SETTEXTMODE:
4521 {
4522 int mask = 0;
4523 int changes = 0;
4524
4525 if (ME_GetTextLength(editor) ||
4526 !list_empty( &editor->undo_stack ) || !list_empty( &editor->redo_stack ))
4527 return E_UNEXPECTED;
4528
4529 /* Check for mutually exclusive flags in adjacent bits of wParam */
4530 if ((wParam & (TM_RICHTEXT | TM_MULTILEVELUNDO | TM_MULTICODEPAGE)) &
4531 (wParam & (TM_PLAINTEXT | TM_SINGLELEVELUNDO | TM_SINGLECODEPAGE)) << 1)
4532 return E_INVALIDARG;
4533
4534 if (wParam & (TM_RICHTEXT | TM_PLAINTEXT))
4535 {
4536 mask |= TM_RICHTEXT | TM_PLAINTEXT;
4537 changes |= wParam & (TM_RICHTEXT | TM_PLAINTEXT);
4538 if (wParam & TM_PLAINTEXT) {
4539 /* Clear selection since it should be possible to select the
4540 * end of text run for rich text */
4541 ME_InvalidateSelection(editor);
4542 ME_SetCursorToStart(editor, &editor->pCursors[0]);
4543 editor->pCursors[1] = editor->pCursors[0];
4544 /* plain text can only have the default style. */
4545 ME_ClearTempStyle(editor);
4546 ME_AddRefStyle(editor->pBuffer->pDefaultStyle);
4547 ME_ReleaseStyle(editor->pCursors[0].pRun->member.run.style);
4548 editor->pCursors[0].pRun->member.run.style = editor->pBuffer->pDefaultStyle;
4549 }
4550 }
4551 /* FIXME: Currently no support for undo level and code page options */
4552 editor->mode = (editor->mode & ~mask) | changes;
4553 return 0;
4554 }
4555 case EM_SETPASSWORDCHAR:
4556 {
4557 editor->cPasswordMask = wParam;
4558 ME_RewrapRepaint(editor);
4559 return 0;
4560 }
4561 case EM_SETTARGETDEVICE:
4562 if (wParam == 0)
4563 {
4564 BOOL new = (lParam == 0 && (editor->styleFlags & ES_MULTILINE));
4565 if (editor->nAvailWidth || editor->bWordWrap != new)
4566 {
4567 editor->bWordWrap = new;
4568 editor->nAvailWidth = 0; /* wrap to client area */
4569 ME_RewrapRepaint(editor);
4570 }
4571 } else {
4572 int width = max(0, lParam);
4573 if ((editor->styleFlags & ES_MULTILINE) &&
4574 (!editor->bWordWrap || editor->nAvailWidth != width))
4575 {
4576 editor->nAvailWidth = width;
4577 editor->bWordWrap = TRUE;
4578 ME_RewrapRepaint(editor);
4579 }
4580 FIXME("EM_SETTARGETDEVICE doesn't use non-NULL target devices\n");
4581 }
4582 return TRUE;
4583 default:
4584 do_default:
4585 *phresult = S_FALSE;
4586 break;
4587 }
4588 return 0L;
4589 }
4590
4591 static LRESULT RichEditWndProc_common(HWND hWnd, UINT msg, WPARAM wParam,
4592 LPARAM lParam, BOOL unicode)
4593 {
4594 ME_TextEditor *editor;
4595 HRESULT hresult;
4596 LRESULT lresult = 0;
4597
4598 TRACE("enter hwnd %p msg %04x (%s) %lx %lx, unicode %d\n",
4599 hWnd, msg, get_msg_name(msg), wParam, lParam, unicode);
4600
4601 editor = (ME_TextEditor *)GetWindowLongPtrW(hWnd, 0);
4602 if (!editor)
4603 {
4604 if (msg == WM_NCCREATE)
4605 {
4606 CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
4607 ITextHost *texthost;
4608
4609 TRACE("WM_NCCREATE: hWnd %p style 0x%08x\n", hWnd, pcs->style);
4610 texthost = ME_CreateTextHost(hWnd, pcs, FALSE);
4611 return texthost != NULL;
4612 }
4613 else
4614 {
4615 return DefWindowProcW(hWnd, msg, wParam, lParam);
4616 }
4617 }
4618
4619 switch (msg)
4620 {
4621 case WM_PAINT:
4622 {
4623 HDC hDC;
4624 RECT rc;
4625 PAINTSTRUCT ps;
4626
4627 hDC = BeginPaint(editor->hWnd, &ps);
4628 if (!editor->bEmulateVersion10 || (editor->nEventMask & ENM_UPDATE))
4629 ME_SendOldNotify(editor, EN_UPDATE);
4630 /* Erase area outside of the formatting rectangle */
4631 if (ps.rcPaint.top < editor->rcFormat.top)
4632 {
4633 rc = ps.rcPaint;
4634 rc.bottom = editor->rcFormat.top;
4635 FillRect(hDC, &rc, editor->hbrBackground);
4636 ps.rcPaint.top = editor->rcFormat.top;
4637 }
4638 if (ps.rcPaint.bottom > editor->rcFormat.bottom) {
4639 rc = ps.rcPaint;
4640 rc.top = editor->rcFormat.bottom;
4641 FillRect(hDC, &rc, editor->hbrBackground);
4642 ps.rcPaint.bottom = editor->rcFormat.bottom;
4643 }
4644 if (ps.rcPaint.left < editor->rcFormat.left) {
4645 rc = ps.rcPaint;
4646 rc.right = editor->rcFormat.left;
4647 FillRect(hDC, &rc, editor->hbrBackground);
4648 ps.rcPaint.left = editor->rcFormat.left;
4649 }
4650 if (ps.rcPaint.right > editor->rcFormat.right) {
4651 rc = ps.rcPaint;
4652 rc.left = editor->rcFormat.right;
4653 FillRect(hDC, &rc, editor->hbrBackground);
4654 ps.rcPaint.right = editor->rcFormat.right;
4655 }
4656
4657 ME_PaintContent(editor, hDC, &ps.rcPaint);
4658 EndPaint(editor->hWnd, &ps);
4659 return 0;
4660 }
4661 case WM_ERASEBKGND:
4662 {
4663 HDC hDC = (HDC)wParam;
4664 RECT rc;
4665
4666 if (GetUpdateRect(editor->hWnd, &rc, TRUE))
4667 FillRect(hDC, &rc, editor->hbrBackground);
4668 return 1;
4669 }
4670 case EM_SETOPTIONS:
4671 {
4672 DWORD dwStyle;
4673 const DWORD mask = ECO_VERTICAL | ECO_AUTOHSCROLL | ECO_AUTOVSCROLL |
4674 ECO_NOHIDESEL | ECO_READONLY | ECO_WANTRETURN |
4675 ECO_SELECTIONBAR;
4676 lresult = ME_HandleMessage(editor, msg, wParam, lParam, unicode, &hresult);
4677 dwStyle = GetWindowLongW(hWnd, GWL_STYLE);
4678 dwStyle = (dwStyle & ~mask) | (lresult & mask);
4679 SetWindowLongW(hWnd, GWL_STYLE, dwStyle);
4680 return lresult;
4681 }
4682 case EM_SETREADONLY:
4683 {
4684 DWORD dwStyle;
4685 lresult = ME_HandleMessage(editor, msg, wParam, lParam, unicode, &hresult);
4686 dwStyle = GetWindowLongW(hWnd, GWL_STYLE);
4687 dwStyle &= ~ES_READONLY;
4688 if (wParam)
4689 dwStyle |= ES_READONLY;
4690 SetWindowLongW(hWnd, GWL_STYLE, dwStyle);
4691 return lresult;
4692 }
4693 default:
4694 lresult = ME_HandleMessage(editor, msg, wParam, lParam, unicode, &hresult);
4695 }
4696
4697 if (hresult == S_FALSE)
4698 lresult = DefWindowProcW(hWnd, msg, wParam, lParam);
4699
4700 TRACE("exit hwnd %p msg %04x (%s) %lx %lx, unicode %d -> %lu\n",
4701 hWnd, msg, get_msg_name(msg), wParam, lParam, unicode, lresult);
4702
4703 return lresult;
4704 }
4705
4706 static LRESULT WINAPI RichEditWndProcW(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
4707 {
4708 BOOL unicode = TRUE;
4709
4710 /* Under Win9x RichEdit20W returns ANSI strings, see the tests. */
4711 if (msg == WM_GETTEXT && (GetVersion() & 0x80000000))
4712 unicode = FALSE;
4713
4714 return RichEditWndProc_common(hWnd, msg, wParam, lParam, unicode);
4715 }
4716
4717 static LRESULT WINAPI RichEditWndProcA(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
4718 {
4719 return RichEditWndProc_common(hWnd, msg, wParam, lParam, FALSE);
4720 }
4721
4722 /******************************************************************
4723 * RichEditANSIWndProc (RICHED20.10)
4724 */
4725 LRESULT WINAPI RichEditANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
4726 {
4727 return RichEditWndProcA(hWnd, msg, wParam, lParam);
4728 }
4729
4730 /******************************************************************
4731 * RichEdit10ANSIWndProc (RICHED20.9)
4732 */
4733 LRESULT WINAPI RichEdit10ANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
4734 {
4735 if (msg == WM_NCCREATE && !GetWindowLongPtrW(hWnd, 0))
4736 {
4737 ITextHost *texthost;
4738 CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
4739
4740 TRACE("WM_NCCREATE: hWnd %p style 0x%08x\n", hWnd, pcs->style);
4741 texthost = ME_CreateTextHost(hWnd, pcs, TRUE);
4742 return texthost != NULL;
4743 }
4744 return RichEditANSIWndProc(hWnd, msg, wParam, lParam);
4745 }
4746
4747 void ME_SendOldNotify(ME_TextEditor *editor, int nCode)
4748 {
4749 ITextHost_TxNotify(editor->texthost, nCode, NULL);
4750 }
4751
4752 /* Fill buffer with srcChars unicode characters from the start cursor.
4753 *
4754 * buffer: destination buffer
4755 * buflen: length of buffer in characters excluding the NULL terminator.
4756 * start: start of editor text to copy into buffer.
4757 * srcChars: Number of characters to use from the editor text.
4758 * bCRLF: if true, replaces all end of lines with \r\n pairs.
4759 *
4760 * returns the number of characters written excluding the NULL terminator.
4761 *
4762 * The written text is always NULL terminated.
4763 */
4764 int ME_GetTextW(ME_TextEditor *editor, WCHAR *buffer, int buflen,
4765 const ME_Cursor *start, int srcChars, BOOL bCRLF,
4766 BOOL bEOP)
4767 {
4768 ME_DisplayItem *pRun, *pNextRun;
4769 const WCHAR *pStart = buffer;
4770 const WCHAR cr_lf[] = {'\r', '\n', 0};
4771 const WCHAR *str;
4772 int nLen;
4773
4774 /* bCRLF flag is only honored in 2.0 and up. 1.0 must always return text verbatim */
4775 if (editor->bEmulateVersion10) bCRLF = FALSE;
4776
4777 pRun = start->pRun;
4778 assert(pRun);
4779 pNextRun = ME_FindItemFwd(pRun, diRun);
4780
4781 nLen = pRun->member.run.len - start->nOffset;
4782 str = get_text( &pRun->member.run, start->nOffset );
4783
4784 while (srcChars && buflen && pNextRun)
4785 {
4786 int nFlags = pRun->member.run.nFlags;
4787
4788 if (bCRLF && nFlags & MERF_ENDPARA && ~nFlags & MERF_ENDCELL)
4789 {
4790 if (buflen == 1) break;
4791 /* FIXME: native fails to reduce srcChars here for WM_GETTEXT or
4792 * EM_GETTEXTEX, however, this is done for copying text which
4793 * also uses this function. */
4794 srcChars -= min(nLen, srcChars);
4795 nLen = 2;
4796 str = cr_lf;
4797 } else {
4798 nLen = min(nLen, srcChars);
4799 srcChars -= nLen;
4800 }
4801
4802 nLen = min(nLen, buflen);
4803 buflen -= nLen;
4804
4805 CopyMemory(buffer, str, sizeof(WCHAR) * nLen);
4806
4807 buffer += nLen;
4808
4809 pRun = pNextRun;
4810 pNextRun = ME_FindItemFwd(pRun, diRun);
4811
4812 nLen = pRun->member.run.len;
4813 str = get_text( &pRun->member.run, 0 );
4814 }
4815 /* append '\r' to the last paragraph. */
4816 if (pRun->next->type == diTextEnd && bEOP)
4817 {
4818 *buffer = '\r';
4819 buffer ++;
4820 }
4821 *buffer = 0;
4822 return buffer - pStart;
4823 }
4824
4825 static BOOL ME_RegisterEditorClass(HINSTANCE hInstance)
4826 {
4827 WNDCLASSW wcW;
4828 WNDCLASSA wcA;
4829
4830 wcW.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
4831 wcW.lpfnWndProc = RichEditWndProcW;
4832 wcW.cbClsExtra = 0;
4833 wcW.cbWndExtra = sizeof(ME_TextEditor *);
4834 wcW.hInstance = NULL; /* hInstance would register DLL-local class */
4835 wcW.hIcon = NULL;
4836 wcW.hCursor = LoadCursorW(NULL, (LPWSTR)IDC_IBEAM);
4837 wcW.hbrBackground = GetStockObject(NULL_BRUSH);
4838 wcW.lpszMenuName = NULL;
4839
4840 if (is_version_nt())
4841 {
4842 wcW.lpszClassName = RICHEDIT_CLASS20W;
4843 if (!RegisterClassW(&wcW)) return FALSE;
4844 wcW.lpszClassName = MSFTEDIT_CLASS;
4845 if (!RegisterClassW(&wcW)) return FALSE;
4846 }
4847 else
4848 {
4849 /* WNDCLASSA/W have the same layout */
4850 wcW.lpszClassName = (LPCWSTR)"RichEdit20W";
4851 if (!RegisterClassA((WNDCLASSA *)&wcW)) return FALSE;
4852 wcW.lpszClassName = (LPCWSTR)"RichEdit50W";
4853 if (!RegisterClassA((WNDCLASSA *)&wcW)) return FALSE;
4854 }
4855
4856 wcA.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
4857 wcA.lpfnWndProc = RichEditWndProcA;
4858 wcA.cbClsExtra = 0;
4859 wcA.cbWndExtra = sizeof(ME_TextEditor *);
4860 wcA.hInstance = NULL; /* hInstance would register DLL-local class */
4861 wcA.hIcon = NULL;
4862 wcA.hCursor = LoadCursorW(NULL, (LPWSTR)IDC_IBEAM);
4863 wcA.hbrBackground = GetStockObject(NULL_BRUSH);
4864 wcA.lpszMenuName = NULL;
4865 wcA.lpszClassName = RICHEDIT_CLASS20A;
4866 if (!RegisterClassA(&wcA)) return FALSE;
4867 wcA.lpszClassName = "RichEdit50A";
4868 if (!RegisterClassA(&wcA)) return FALSE;
4869
4870 return TRUE;
4871 }
4872
4873 static LRESULT WINAPI REComboWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
4874 /* FIXME: Not implemented */
4875 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
4876 hWnd, msg, get_msg_name(msg), wParam, lParam);
4877 return DefWindowProcW(hWnd, msg, wParam, lParam);
4878 }
4879
4880 static LRESULT WINAPI REListWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
4881 /* FIXME: Not implemented */
4882 TRACE("hWnd %p msg %04x (%s) %08lx %08lx\n",
4883 hWnd, msg, get_msg_name(msg), wParam, lParam);
4884 return DefWindowProcW(hWnd, msg, wParam, lParam);
4885 }
4886
4887 /******************************************************************
4888 * REExtendedRegisterClass (RICHED20.8)
4889 *
4890 * FIXME undocumented
4891 * Need to check for errors and implement controls and callbacks
4892 */
4893 LRESULT WINAPI REExtendedRegisterClass(void)
4894 {
4895 WNDCLASSW wcW;
4896 UINT result;
4897
4898 FIXME("semi stub\n");
4899
4900 wcW.cbClsExtra = 0;
4901 wcW.cbWndExtra = 4;
4902 wcW.hInstance = NULL;
4903 wcW.hIcon = NULL;
4904 wcW.hCursor = NULL;
4905 wcW.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
4906 wcW.lpszMenuName = NULL;
4907
4908 if (!ME_ListBoxRegistered)
4909 {
4910 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS;
4911 wcW.lpfnWndProc = REListWndProc;
4912 wcW.lpszClassName = REListBox20W;
4913 if (RegisterClassW(&wcW)) ME_ListBoxRegistered = TRUE;
4914 }
4915
4916 if (!ME_ComboBoxRegistered)
4917 {
4918 wcW.style = CS_PARENTDC | CS_DBLCLKS | CS_GLOBALCLASS | CS_VREDRAW | CS_HREDRAW;
4919 wcW.lpfnWndProc = REComboWndProc;
4920 wcW.lpszClassName = REComboBox20W;
4921 if (RegisterClassW(&wcW)) ME_ComboBoxRegistered = TRUE;
4922 }
4923
4924 result = 0;
4925 if (ME_ListBoxRegistered)
4926 result += 1;
4927 if (ME_ComboBoxRegistered)
4928 result += 2;
4929
4930 return result;
4931 }
4932
4933 static BOOL isurlspecial(WCHAR c)
4934 {
4935 static const WCHAR special_chars[] = {'.','/','%','@','*','|','\\','+','#',0};
4936 return strchrW( special_chars, c ) != NULL;
4937 }
4938
4939 /**
4940 * This proc takes a selection, and scans it forward in order to select the span
4941 * of a possible URL candidate. A possible URL candidate must start with isalnum
4942 * or one of the following special characters: *|/\+%#@ and must consist entirely
4943 * of the characters allowed to start the URL, plus : (colon) which may occur
4944 * at most once, and not at either end.
4945 */
4946 static BOOL ME_FindNextURLCandidate(ME_TextEditor *editor,
4947 const ME_Cursor *start,
4948 int nChars,
4949 ME_Cursor *candidate_min,
4950 ME_Cursor *candidate_max)
4951 {
4952 ME_Cursor cursor = *start;
4953 BOOL foundColon = FALSE;
4954 BOOL candidateStarted = FALSE;
4955 WCHAR lastAcceptedChar = '\0';
4956
4957 while (nChars > 0)
4958 {
4959 WCHAR *strStart = get_text( &cursor.pRun->member.run, 0 );
4960 WCHAR *str = strStart + cursor.nOffset;
4961 int nLen = cursor.pRun->member.run.len - cursor.nOffset;
4962 nChars -= nLen;
4963
4964 if (~cursor.pRun->member.run.nFlags & MERF_ENDPARA)
4965 {
4966 /* Find start of candidate */
4967 if (!candidateStarted)
4968 {
4969 while (nLen)
4970 {
4971 nLen--;
4972 if (isalnumW(*str) || isurlspecial(*str))
4973 {
4974 cursor.nOffset = str - strStart;
4975 *candidate_min = cursor;
4976 candidateStarted = TRUE;
4977 lastAcceptedChar = *str++;
4978 break;
4979 }
4980 str++;
4981 }
4982 }
4983
4984 /* Find end of candidate */
4985 if (candidateStarted) {
4986 while (nLen)
4987 {
4988 nLen--;
4989 if (*str == ':' && !foundColon) {
4990 foundColon = TRUE;
4991 } else if (!isalnumW(*str) && !isurlspecial(*str)) {
4992 cursor.nOffset = str - strStart;
4993 if (lastAcceptedChar == ':')
4994 ME_MoveCursorChars(editor, &cursor, -1);
4995 *candidate_max = cursor;
4996 return TRUE;
4997 }
4998 lastAcceptedChar = *str++;
4999 }
5000 }
5001 } else {
5002 /* End of paragraph: skip it if before candidate span, or terminates
5003 current active span */
5004 if (candidateStarted) {
5005 if (lastAcceptedChar == ':')
5006 ME_MoveCursorChars(editor, &cursor, -1);
5007 *candidate_max = cursor;
5008 return TRUE;
5009 }
5010 }
5011
5012 /* Reaching this point means no span was found, so get next span */
5013 if (!ME_NextRun(&cursor.pPara, &cursor.pRun)) {
5014 if (candidateStarted) {
5015 /* There are no further runs, so take end of text as end of candidate */
5016 cursor.nOffset = str - strStart;
5017 if (lastAcceptedChar == ':')
5018 ME_MoveCursorChars(editor, &cursor, -1);
5019 *candidate_max = cursor;
5020 return TRUE;
5021 }
5022 *candidate_max = *candidate_min = cursor;
5023 return FALSE;
5024 }
5025 cursor.nOffset = 0;
5026 }
5027
5028 if (candidateStarted) {
5029 /* There are no further runs, so take end of text as end of candidate */
5030 if (lastAcceptedChar == ':')
5031 ME_MoveCursorChars(editor, &cursor, -1);
5032 *candidate_max = cursor;
5033 return TRUE;
5034 }
5035 *candidate_max = *candidate_min = cursor;
5036 return FALSE;
5037 }
5038
5039 /**
5040 * This proc evaluates the selection and returns TRUE if it can be considered an URL
5041 */
5042 static BOOL ME_IsCandidateAnURL(ME_TextEditor *editor, const ME_Cursor *start, int nChars)
5043 {
5044 #define MAX_PREFIX_LEN 9
5045 struct prefix_s {
5046 const WCHAR text[MAX_PREFIX_LEN];
5047 int length;
5048 }prefixes[] = {
5049 {{'p','r','o','s','p','e','r','o',':'}, 9},
5050 {{'t','e','l','n','e','t',':'}, 7},
5051 {{'g','o','p','h','e','r',':'}, 7},
5052 {{'m','a','i','l','t','o',':'}, 7},
5053 {{'h','t','t','p','s',':'}, 6},
5054 {{'f','i','l','e',':'}, 5},
5055 {{'n','e','w','s',':'}, 5},
5056 {{'w','a','i','s',':'}, 5},
5057 {{'n','n','t','p',':'}, 5},
5058 {{'h','t','t','p',':'}, 5},
5059 {{'w','w','w','.'}, 4},
5060 {{'f','t','p',':'}, 4},
5061 };
5062 WCHAR bufferW[MAX_PREFIX_LEN + 1];
5063 unsigned int i;
5064
5065 ME_GetTextW(editor, bufferW, MAX_PREFIX_LEN, start, nChars, FALSE, FALSE);
5066 for (i = 0; i < sizeof(prefixes) / sizeof(*prefixes); i++)
5067 {
5068 if (nChars < prefixes[i].length) continue;
5069 if (!memcmp(prefixes[i].text, bufferW, prefixes[i].length * sizeof(WCHAR)))
5070 return TRUE;
5071 }
5072 return FALSE;
5073 #undef MAX_PREFIX_LEN
5074 }
5075
5076 /**
5077 * This proc walks through the indicated selection and evaluates whether each
5078 * section identified by ME_FindNextURLCandidate and in-between sections have
5079 * their proper CFE_LINK attributes set or unset. If the CFE_LINK attribute is
5080 * not what it is supposed to be, this proc sets or unsets it as appropriate.
5081 *
5082 * Since this function can cause runs to be split, do not depend on the value
5083 * of the start cursor at the end of the function.
5084 *
5085 * nChars may be set to INT_MAX to update to the end of the text.
5086 *
5087 * Returns TRUE if at least one section was modified.
5088 */
5089 static BOOL ME_UpdateLinkAttribute(ME_TextEditor *editor, ME_Cursor *start, int nChars)
5090 {
5091 BOOL modified = FALSE;
5092 ME_Cursor startCur = *start;
5093
5094 if (!editor->AutoURLDetect_bEnable) return FALSE;
5095
5096 do
5097 {
5098 CHARFORMAT2W link;
5099 ME_Cursor candidateStart, candidateEnd;
5100
5101 if (ME_FindNextURLCandidate(editor, &startCur, nChars,
5102 &candidateStart, &candidateEnd))
5103 {
5104 /* Section before candidate is not an URL */
5105 int cMin = ME_GetCursorOfs(&candidateStart);
5106 int cMax = ME_GetCursorOfs(&candidateEnd);
5107
5108 if (!ME_IsCandidateAnURL(editor, &candidateStart, cMax - cMin))
5109 candidateStart = candidateEnd;
5110 nChars -= cMax - ME_GetCursorOfs(&startCur);
5111 }
5112 else
5113 {
5114 /* No more candidates until end of selection */
5115 nChars = 0;
5116 }
5117
5118 if (startCur.pRun != candidateStart.pRun ||
5119 startCur.nOffset != candidateStart.nOffset)
5120 {
5121 /* CFE_LINK effect should be consistently unset */
5122 link.cbSize = sizeof(link);
5123 ME_GetCharFormat(editor, &startCur, &candidateStart, &link);
5124 if (!(link.dwMask & CFM_LINK) || (link.dwEffects & CFE_LINK))
5125 {
5126 /* CFE_LINK must be unset from this range */
5127 memset(&link, 0, sizeof(CHARFORMAT2W));
5128 link.cbSize = sizeof(link);
5129 link.dwMask = CFM_LINK;
5130 link.dwEffects = 0;
5131 ME_SetCharFormat(editor, &startCur, &candidateStart, &link);
5132 /* Update candidateEnd since setting character formats may split
5133 * runs, which can cause a cursor to be at an invalid offset within
5134 * a split run. */
5135 while (candidateEnd.nOffset >= candidateEnd.pRun->member.run.len)
5136 {
5137 candidateEnd.nOffset -= candidateEnd.pRun->member.run.len;
5138 candidateEnd.pRun = ME_FindItemFwd(candidateEnd.pRun, diRun);
5139 }
5140 modified = TRUE;
5141 }
5142 }
5143 if (candidateStart.pRun != candidateEnd.pRun ||
5144 candidateStart.nOffset != candidateEnd.nOffset)
5145 {
5146 /* CFE_LINK effect should be consistently set */
5147 link.cbSize = sizeof(link);
5148 ME_GetCharFormat(editor, &candidateStart, &candidateEnd, &link);
5149 if (!(link.dwMask & CFM_LINK) || !(link.dwEffects & CFE_LINK))
5150 {
5151 /* CFE_LINK must be set on this range */
5152 memset(&link, 0, sizeof(CHARFORMAT2W));
5153 link.cbSize = sizeof(link);
5154 link.dwMask = CFM_LINK;
5155 link.dwEffects = CFE_LINK;
5156 ME_SetCharFormat(editor, &candidateStart, &candidateEnd, &link);
5157 modified = TRUE;
5158 }
5159 }
5160 startCur = candidateEnd;
5161 } while (nChars > 0);
5162 return modified;
5163 }