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