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