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