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