eec98474719de4e8b9d0ff5a63424c578426f01b
[reactos.git] / reactos / lib / riched20 / editor.c
1 /*
2 * RichEdit - functions dealing with editor object
3 *
4 * Copyright 2004 by Krzysztof Foltman
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 /*
22 API implementation status:
23
24 Messages (ANSI versions not done yet)
25 - EM_AUTOURLDETECT 2.0
26 - EM_CANPASTE
27 + EM_CANREDO 2.0
28 + EM_CANUNDO
29 - EM_CHARFROMPOS
30 - EM_DISPLAYBAND
31 + EM_EMPTYUNDOBUFFER
32 + EM_EXGETSEL
33 - EM_EXLIMITTEXT
34 - EM_EXLINEFROMCHAR
35 + EM_EXSETSEL
36 - EM_FINDTEXT
37 - EM_FINDTEXTEX
38 - EM_FINDWORDBREAK
39 - EM_FMTLINES
40 - EM_FORMATRANGE
41 - EM_GETCHARFORMAT (partly done)
42 + EM_GETEVENTMASK
43 - EM_GETFIRSTVISIBLELINE
44 - EM_GETIMECOLOR 1.0asian
45 - EM_GETIMECOMPMODE 2.0
46 - EM_GETIMEOPTIONS 1.0asian
47 - EM_GETIMESTATUS
48 - EM_GETLANGOPTIONS 2.0
49 - EM_GETLIMITTEXT
50 - EM_GETLINE
51 - EM_GETLINECOUNT returns number of rows, not of paragraphs
52 + EM_GETMODIFY
53 - EM_GETOLEINTERFACE
54 - EM_GETOPTIONS
55 + EM_GETPARAFORMAT
56 - EM_GETPUNCTUATION 1.0asian
57 - EM_GETRECT
58 - EM_GETREDONAME 2.0
59 + EM_GETSEL
60 + EM_GETSELTEXT (ANSI&Unicode)
61 ! - EM_GETTHUMB
62 - EM_GETTEXTMODE 2.0
63 ? + EM_GETTEXTRANGE (ANSI&Unicode)
64 - EM_GETUNDONAME
65 - EM_GETWORDBREAKPROC
66 - EM_GETWORDBREAKPROCEX
67 - EM_GETWORDWRAPMODE 1.0asian
68 - EM_HIDESELECTION
69 - EM_LIMITTEXT
70 - EM_LINEFROMCHAR
71 - EM_LINEINDEX
72 - EM_LINELENGTH
73 - EM_LINESCROLL
74 - EM_PASTESPECIAL
75 - EM_POSFROMCHARS
76 - EM_REDO 2.0
77 - EM_REQUESTRESIZE
78 + EM_REPLACESEL (proper style?) ANSI&Unicode
79 - EM_SCROLL
80 - EM_SCROLLCARET
81 - EM_SELECTIONTYPE
82 + EM_SETBKGNDCOLOR
83 - EM_SETCHARFORMAT (partly done, no ANSI)
84 + EM_SETEVENTMASK (few notifications supported)
85 - EM_SETIMECOLOR 1.0asian
86 - EM_SETIMEOPTIONS 1.0asian
87 - EM_SETLANGOPTIONS 2.0
88 - EM_SETLIMITTEXT
89 + EM_SETMODIFY (not sure if implementation is correct)
90 - EM_SETOLECALLBACK
91 - EM_SETOPTIONS
92 + EM_SETPARAFORMAT
93 - EM_SETPUNCTUATION 1.0asian
94 + EM_SETREADONLY no beep on modification attempt
95 - EM_SETRECT
96 - EM_SETRECTNP (EM_SETRECT without repainting) - not supported in RICHEDIT
97 + EM_SETSEL
98 - EM_SETTARGETDEVICE
99 - EM_SETTEXTMODE 2.0
100 - EM_SETUNDOLIMIT 2.0
101 - EM_SETWORDBREAKPROC
102 - EM_SETWORDBREAKPROCEX
103 - EM_SETWORDWRAPMODE 1.0asian
104 - EM_STOPGROUPTYPING 2.0
105 - EM_STREAMIN
106 - EM_STREAMOUT
107 - EM_UNDO
108 + WM_CHAR
109 + WM_CLEAR
110 - WM_COPY (lame implementation, no RTF support)
111 - WM_CUT (lame implementation, no RTF support)
112 + WM_GETDLGCODE (the current implementation is incomplete)
113 + WM_GETTEXT (ANSI&Unicode)
114 + WM_GETTEXTLENGTH (ANSI version sucks)
115 - WM_PASTE
116 - WM_SETFONT
117 + WM_SETTEXT (resets undo stack !) (proper style?) ANSI&Unicode
118 - WM_STYLECHANGING
119 - WM_STYLECHANGED (things like read-only flag)
120 - WM_UNICHAR
121
122 Notifications
123
124 * EN_CHANGE (sent from the wrong place)
125 - EN_CORRECTTEXT
126 - EN_DROPFILES
127 - EN_ERRSPACE
128 - EN_HSCROLL
129 - EN_IMECHANGE
130 + EN_KILLFOCUS
131 - EN_LINK
132 - EN_MAXTEXT
133 - EN_MSGFILTER
134 - EN_OLEOPFAILED
135 - EN_PROTECTED
136 - EN_REQUESTRESIZE
137 - EN_SAVECLIPBOARD
138 + EN_SELCHANGE
139 + EN_SETFOCUS
140 - EN_STOPNOUNDO
141 * EN_UPDATE (sent from the wrong place)
142 - EN_VSCROLL
143
144 Styles
145
146 - ES_AUTOHSCROLL
147 - ES_AUTOVSCROLL
148 - ES_CENTER
149 - ES_DISABLENOSCROLL (scrollbar is always visible)
150 - ES_EX_NOCALLOLEINIT
151 - ES_LEFT
152 - ES_MULTILINE (currently single line controls aren't supported)
153 - ES_NOIME
154 - ES_READONLY (I'm not sure if beeping is the proper behaviour)
155 - ES_RIGHT
156 - ES_SAVESEL
157 - ES_SELFIME
158 - ES_SUNKEN
159 - ES_VERTICAL
160 - ES_WANTRETURN (don't know how to do WM_GETDLGCODE part)
161 - WS_SETFONT
162 - WS_HSCROLL
163 - WS_VSCROLL
164 */
165
166 /*
167 * RICHED20 TODO (incomplete):
168 *
169 * - font caching
170 * - add remaining CHARFORMAT/PARAFORMAT fields
171 * - right/center align should strip spaces from the beginning
172 * - more advanced navigation (Ctrl-arrows, PageUp/PageDn)
173 * - tabs
174 * - pictures (not just smiling faces that lack API support ;-) )
175 * - OLE objects
176 * - calculate heights of pictures (half-done)
177 * - EM_STREAMIN/EM_STREAMOUT
178 * - horizontal scrolling (not even started)
179 * - fix scrollbars and refresh (it sucks bigtime)
180 * - hysteresis during wrapping (related to scrollbars appearing/disappearing)
181 * - should remember maximum row width for wrap hysteresis
182 * - find/replace
183 * - how to implement EM_FORMATRANGE and EM_DISPLAYBAND ? (Mission Impossible)
184 * - italic cursor with italic fonts
185 * - IME
186 * - most notifications aren't sent at all (the most important ones are)
187 * - when should EN_SELCHANGE be sent after text change ? (before/after EN_UPDATE?)
188 * - WM_SETTEXT may use wrong style (but I'm 80% sure it's OK)
189 * - EM_GETCHARFORMAT with SCF_SELECTION may not behave 100% like in original (but very close)
190 * - bugs in end-of-text handling (the gray bar) could get me in jail ;-)
191 * - determination of row size
192 * - end-of-paragraph marks should be of reasonable size
193 *
194 * Bugs that are probably fixed, but not so easy to verify:
195 * - EN_UPDATE/EN_CHANGE are handled very incorrectly (should be OK now)
196 * - undo for ME_JoinParagraphs doesn't store paragraph format ? (it does)
197 * - check/fix artificial EOL logic (bCursorAtEnd, hardly logical)
198 * - caret shouldn't be displayed when selection isn't empty
199 * - check refcounting in style management functions (looks perfect now, but no bugs is suspicious)
200 * - undo for setting default format (done, might be buggy)
201 * - styles might be not released properly (looks like they work like charm, but who knows?
202 *
203 */
204
205 #include "editor.h"
206 #include <ole2.h>
207 #include <richole.h>
208 #include <winreg.h>
209 #define NO_SHLWAPI_STREAM
210 #include <shlwapi.h>
211
212 #include "rtf.h"
213
214 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
215
216 int me_debug = 0;
217 HANDLE me_heap = NULL;
218
219 ME_TextBuffer *ME_MakeText() {
220
221 ME_TextBuffer *buf = ALLOC_OBJ(ME_TextBuffer);
222
223 ME_DisplayItem *p1 = ME_MakeDI(diTextStart);
224 ME_DisplayItem *p2 = ME_MakeDI(diTextEnd);
225
226 p1->prev = NULL;
227 p1->next = p2;
228 p2->prev = p1;
229 p2->next = NULL;
230 p1->member.para.next_para = p2;
231 p2->member.para.prev_para = p1;
232 p2->member.para.nCharOfs = 0;
233
234 buf->pFirst = p1;
235 buf->pLast = p2;
236 buf->pCharStyle = NULL;
237
238 return buf;
239 }
240
241 #define STREAMIN_BUFFER_SIZE 4096 /* M$ compatibility */
242
243 static LRESULT ME_StreamInText(ME_TextEditor *editor, DWORD dwFormat, EDITSTREAM *stream, ME_Style *style)
244 {
245 BYTE buffer[STREAMIN_BUFFER_SIZE+1];
246 WCHAR wszText[STREAMIN_BUFFER_SIZE+1];
247
248 TRACE("%08lx %p\n", dwFormat, stream);
249 stream->dwError = 0;
250
251 do {
252 long nDataSize = 0, nWideChars = 0;
253 stream->dwError = stream->pfnCallback(stream->dwCookie,
254 (dwFormat & SF_UNICODE ? (BYTE *)wszText : buffer),
255 STREAMIN_BUFFER_SIZE, &nDataSize);
256
257 if (stream->dwError)
258 break;
259 if (!nDataSize)
260 break;
261
262 if (!(dwFormat & SF_UNICODE))
263 {
264 /* FIXME? this is doomed to fail on true MBCS like UTF-8, luckily they're unlikely to be used as CP_ACP */
265 nWideChars = MultiByteToWideChar(CP_ACP, 0, buffer, nDataSize, wszText, STREAMIN_BUFFER_SIZE);
266 }
267 else
268 nWideChars = nDataSize>>1;
269 ME_InsertTextFromCursor(editor, 0, wszText, nWideChars, style);
270 if (nDataSize<STREAMIN_BUFFER_SIZE)
271 break;
272 } while(1);
273 ME_CommitUndo(editor);
274 return 0;
275 }
276
277 static LRESULT ME_StreamIn(ME_TextEditor *editor, DWORD format, EDITSTREAM *stream)
278 {
279 RTF_Info parser;
280 ME_Style *style;
281
282 TRACE("%p %p\n", stream, editor->hWnd);
283
284 if (format & SFF_SELECTION) {
285 style = ME_GetSelectionInsertStyle(editor);
286 SendMessageW(editor->hWnd, WM_CLEAR, 0, 0);
287 }
288 else {
289 style = editor->pBuffer->pDefaultStyle;
290 ME_AddRefStyle(style);
291 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
292 SetWindowTextA(editor->hWnd, "");
293 ME_ClearTempStyle(editor);
294 }
295
296 if (format & SF_RTF) {
297 /* setup the RTF parser */
298 memset(&parser, 0, sizeof parser);
299 RTFSetEditStream(&parser, stream);
300 parser.rtfFormat = format&(SF_TEXT|SF_RTF);
301 parser.hwndEdit = editor->hWnd;
302 WriterInit(&parser);
303 RTFInit(&parser);
304 BeginFile(&parser);
305
306 /* do the parsing */
307 RTFRead(&parser);
308 RTFFlushOutputBuffer(&parser);
309 }
310 else if (format & SF_TEXT)
311 ME_StreamInText(editor, format, stream, style);
312 else
313 ERR("EM_STREAMIN without SF_TEXT or SF_RTF\n");
314 /* put the cursor at the top */
315 if (!(format & SFF_SELECTION))
316 SendMessageA(editor->hWnd, EM_SETSEL, 0, 0);
317 else
318 {
319 /* FIXME where to put cursor now ? */
320 }
321 ME_ReleaseStyle(style);
322
323 return 0;
324 }
325
326 ME_TextEditor *ME_MakeEditor(HWND hWnd) {
327 ME_TextEditor *ed = ALLOC_OBJ(ME_TextEditor);
328 HDC hDC;
329 int i;
330 ed->hWnd = hWnd;
331 ed->pBuffer = ME_MakeText();
332 hDC = GetDC(hWnd);
333 ME_MakeFirstParagraph(hDC, ed->pBuffer);
334 ReleaseDC(hWnd, hDC);
335 ed->bCaretShown = FALSE;
336 ed->nCursors = 3;
337 ed->pCursors = ALLOC_N_OBJ(ME_Cursor, ed->nCursors);
338 ed->pCursors[0].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
339 ed->pCursors[0].nOffset = 0;
340 ed->pCursors[1].pRun = ME_FindItemFwd(ed->pBuffer->pFirst, diRun);
341 ed->pCursors[1].nOffset = 0;
342 ed->nLastTotalLength = ed->nTotalLength = 0;
343 ed->nScrollPos = 0;
344 ed->nUDArrowX = -1;
345 ed->nSequence = 0;
346 ed->rgbBackColor = -1;
347 ed->bCaretAtEnd = FALSE;
348 ed->nEventMask = 0;
349 ed->nModifyStep = 0;
350 ed->pUndoStack = ed->pRedoStack = NULL;
351 ed->nUndoMode = umAddToUndo;
352 ed->nParagraphs = 1;
353 ed->nLastSelStart = ed->nLastSelEnd = 0;
354 for (i=0; i<HFONT_CACHE_SIZE; i++)
355 {
356 ed->pFontCache[i].nRefs = 0;
357 ed->pFontCache[i].nAge = 0;
358 ed->pFontCache[i].hFont = NULL;
359 }
360 ME_CheckCharOffsets(ed);
361 return ed;
362 }
363
364 void ME_DestroyEditor(ME_TextEditor *editor)
365 {
366 ME_DisplayItem *pFirst = editor->pBuffer->pFirst;
367 ME_DisplayItem *p = pFirst, *pNext = NULL;
368 int i;
369
370 ME_ClearTempStyle(editor);
371 ME_EmptyUndoStack(editor);
372 while(p) {
373 pNext = p->next;
374 ME_DestroyDisplayItem(p);
375 p = pNext;
376 }
377 ME_ReleaseStyle(editor->pBuffer->pDefaultStyle);
378 for (i=0; i<HFONT_CACHE_SIZE; i++)
379 {
380 if (editor->pFontCache[i].hFont)
381 DeleteObject(editor->pFontCache[i].hFont);
382 }
383
384 FREE_OBJ(editor);
385 }
386
387 #define UNSUPPORTED_MSG(e) \
388 case e: \
389 FIXME(#e ": stub\n"); \
390 return DefWindowProcW(hWnd, msg, wParam, lParam);
391
392 /******************************************************************
393 * RichEditANSIWndProc (RICHED20.10)
394 */
395 LRESULT WINAPI RichEditANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
396 HDC hDC;
397 PAINTSTRUCT ps;
398 SCROLLINFO si;
399 ME_TextEditor *editor = (ME_TextEditor *)GetWindowLongW(hWnd, 0);
400 TRACE("msg %d %08x %08lx\n", msg, wParam, lParam);
401 switch(msg) {
402
403 UNSUPPORTED_MSG(EM_AUTOURLDETECT)
404 UNSUPPORTED_MSG(EM_CANPASTE)
405 UNSUPPORTED_MSG(EM_CHARFROMPOS)
406 UNSUPPORTED_MSG(EM_DISPLAYBAND)
407 UNSUPPORTED_MSG(EM_EXLIMITTEXT)
408 UNSUPPORTED_MSG(EM_EXLINEFROMCHAR)
409 UNSUPPORTED_MSG(EM_FINDTEXT)
410 UNSUPPORTED_MSG(EM_FINDTEXTEX)
411 UNSUPPORTED_MSG(EM_FINDWORDBREAK)
412 UNSUPPORTED_MSG(EM_FMTLINES)
413 UNSUPPORTED_MSG(EM_FORMATRANGE)
414 UNSUPPORTED_MSG(EM_GETFIRSTVISIBLELINE)
415 UNSUPPORTED_MSG(EM_GETIMECOMPMODE)
416 /* UNSUPPORTED_MSG(EM_GETIMESTATUS) missing in Wine headers */
417 UNSUPPORTED_MSG(EM_GETLANGOPTIONS)
418 UNSUPPORTED_MSG(EM_GETLIMITTEXT)
419 UNSUPPORTED_MSG(EM_GETLINE)
420 UNSUPPORTED_MSG(EM_GETLINECOUNT)
421 /* UNSUPPORTED_MSG(EM_GETOLEINTERFACE) separate stub */
422 UNSUPPORTED_MSG(EM_GETOPTIONS)
423 UNSUPPORTED_MSG(EM_GETRECT)
424 UNSUPPORTED_MSG(EM_GETREDONAME)
425 UNSUPPORTED_MSG(EM_GETTEXTMODE)
426 UNSUPPORTED_MSG(EM_GETUNDONAME)
427 UNSUPPORTED_MSG(EM_GETWORDBREAKPROC)
428 UNSUPPORTED_MSG(EM_GETWORDBREAKPROCEX)
429 UNSUPPORTED_MSG(EM_HIDESELECTION)
430 UNSUPPORTED_MSG(EM_LIMITTEXT) /* also known as EM_SETLIMITTEXT */
431 UNSUPPORTED_MSG(EM_LINEFROMCHAR)
432 UNSUPPORTED_MSG(EM_LINEINDEX)
433 UNSUPPORTED_MSG(EM_LINELENGTH)
434 UNSUPPORTED_MSG(EM_LINESCROLL)
435 UNSUPPORTED_MSG(EM_PASTESPECIAL)
436 /* UNSUPPORTED_MSG(EM_POSFROMCHARS) missing in Wine headers */
437 UNSUPPORTED_MSG(EM_REQUESTRESIZE)
438 UNSUPPORTED_MSG(EM_SCROLL)
439 UNSUPPORTED_MSG(EM_SCROLLCARET)
440 UNSUPPORTED_MSG(EM_SELECTIONTYPE)
441 UNSUPPORTED_MSG(EM_SETLANGOPTIONS)
442 UNSUPPORTED_MSG(EM_SETOLECALLBACK)
443 UNSUPPORTED_MSG(EM_SETOPTIONS)
444 UNSUPPORTED_MSG(EM_SETRECT)
445 UNSUPPORTED_MSG(EM_SETRECTNP)
446 UNSUPPORTED_MSG(EM_SETTARGETDEVICE)
447 UNSUPPORTED_MSG(EM_SETTEXTMODE)
448 UNSUPPORTED_MSG(EM_SETUNDOLIMIT)
449 UNSUPPORTED_MSG(EM_SETWORDBREAKPROC)
450 UNSUPPORTED_MSG(EM_SETWORDBREAKPROCEX)
451 UNSUPPORTED_MSG(EM_STREAMOUT)
452 UNSUPPORTED_MSG(WM_SETFONT)
453 UNSUPPORTED_MSG(WM_PASTE)
454 UNSUPPORTED_MSG(WM_STYLECHANGING)
455 UNSUPPORTED_MSG(WM_STYLECHANGED)
456 /* UNSUPPORTED_MSG(WM_UNICHAR) FIXME missing in Wine headers */
457
458 /* Messages specific to Richedit controls */
459
460 case EM_STREAMIN:
461 return ME_StreamIn(editor, wParam, (EDITSTREAM*)lParam);
462 case WM_GETDLGCODE:
463 {
464 UINT code = DLGC_WANTCHARS|DLGC_WANTARROWS;
465 if (GetWindowLongW(hWnd, GWL_STYLE)&ES_WANTRETURN)
466 code |= 0; /* FIXME what can we do here ? ask for messages and censor them ? */
467 return code;
468 }
469 case WM_NCCREATE:
470 {
471 CREATESTRUCTW *pcs = (CREATESTRUCTW *)lParam;
472 editor = ME_MakeEditor(hWnd);
473 SetWindowLongW(hWnd, 0, (long)editor);
474 pcs = 0; /* ignore */
475 return TRUE;
476 }
477 case EM_EMPTYUNDOBUFFER:
478 ME_EmptyUndoStack(editor);
479 return 0;
480 case EM_GETSEL:
481 {
482 ME_GetSelection(editor, (int *)wParam, (int *)lParam);
483 if (!((wParam|lParam) & 0xFFFF0000))
484 return (lParam<<16)|wParam;
485 return -1;
486 }
487 case EM_EXGETSEL:
488 {
489 CHARRANGE *pRange = (CHARRANGE *)lParam;
490 ME_GetSelection(editor, (int *)&pRange->cpMin, (int *)&pRange->cpMax);
491 return 0;
492 }
493 case EM_CANUNDO:
494 return editor->pUndoStack != NULL;
495 case EM_CANREDO:
496 return editor->pRedoStack != NULL;
497 case EM_UNDO:
498 ME_Undo(editor);
499 return 0;
500 case EM_REDO:
501 ME_Redo(editor);
502 return 0;
503 case EM_SETSEL:
504 {
505 ME_SetSelection(editor, wParam, lParam);
506 ME_Repaint(editor);
507 ME_SendSelChange(editor);
508 return 0;
509 }
510 case EM_EXSETSEL:
511 {
512 CHARRANGE *pRange = (CHARRANGE *)lParam;
513 ME_SetSelection(editor, pRange->cpMin, pRange->cpMax);
514 /* FIXME optimize */
515 ME_Repaint(editor);
516 ME_SendSelChange(editor);
517 return 0;
518 }
519 case EM_SETBKGNDCOLOR:
520 {
521 LRESULT lColor = ME_GetBackColor(editor);
522 if (wParam)
523 editor->rgbBackColor = -1;
524 else
525 editor->rgbBackColor = lParam;
526 InvalidateRect(hWnd, NULL, TRUE);
527 UpdateWindow(hWnd);
528 return lColor;
529 }
530 case EM_GETMODIFY:
531 return editor->nModifyStep == 0 ? 0 : 1;
532 case EM_SETMODIFY:
533 {
534 if (wParam)
535 editor->nModifyStep = 0x80000000;
536 else
537 editor->nModifyStep = 0;
538
539 return 0;
540 }
541 case EM_SETREADONLY:
542 {
543 long nStyle = GetWindowLongW(hWnd, GWL_STYLE);
544 if (wParam)
545 nStyle |= ES_READONLY;
546 else
547 nStyle &= ~ES_READONLY;
548 SetWindowLongW(hWnd, GWL_STYLE, nStyle);
549 ME_Repaint(editor);
550 return 0;
551 }
552 case EM_SETEVENTMASK:
553 editor->nEventMask = lParam;
554 return 0;
555 case EM_GETEVENTMASK:
556 return editor->nEventMask;
557 case EM_SETCHARFORMAT:
558 {
559 CHARFORMAT2W buf, *p;
560 p = ME_ToCF2W(&buf, (CHARFORMAT2W *)lParam);
561 if (!wParam)
562 ME_SetDefaultCharFormat(editor, p);
563 else if (wParam == (SCF_WORD | SCF_SELECTION))
564 FIXME("word selection not supported\n");
565 else if (wParam == SCF_ALL)
566 ME_SetCharFormat(editor, 0, ME_GetTextLength(editor), p);
567 else
568 ME_SetSelectionCharFormat(editor, p);
569 ME_CommitUndo(editor);
570 ME_UpdateRepaint(editor);
571 return 0;
572 }
573 case EM_GETCHARFORMAT:
574 {
575 CHARFORMAT2W tmp;
576 tmp.cbSize = sizeof(tmp);
577 if (!wParam)
578 ME_GetDefaultCharFormat(editor, &tmp);
579 else
580 ME_GetSelectionCharFormat(editor, &tmp);
581 ME_CopyToCFAny((CHARFORMAT2W *)lParam, &tmp);
582 return 0;
583 }
584 case EM_SETPARAFORMAT:
585 ME_SetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
586 ME_CommitUndo(editor);
587 return 0;
588 case EM_GETPARAFORMAT:
589 ME_GetSelectionParaFormat(editor, (PARAFORMAT2 *)lParam);
590 return 0;
591 case WM_CLEAR:
592 {
593 int from, to;
594 ME_GetSelection(editor, &from, &to);
595 ME_InternalDeleteText(editor, from, to-from);
596 ME_CommitUndo(editor);
597 ME_UpdateRepaint(editor);
598 return 0;
599 }
600 case EM_REPLACESEL:
601 {
602 int from, to;
603 ME_Style *style;
604 LPWSTR wszText = ME_ToUnicode(hWnd, (void *)lParam);
605 size_t len = lstrlenW(wszText);
606 TRACE("EM_REPLACESEL - %s\n", debugstr_w(wszText));
607
608 ME_GetSelection(editor, &from, &to);
609 style = ME_GetSelectionInsertStyle(editor);
610 ME_InternalDeleteText(editor, from, to-from);
611 ME_InsertTextFromCursor(editor, 0, wszText, len, style);
612 ME_ReleaseStyle(style);
613 ME_EndToUnicode(hWnd, wszText);
614 /* drop temporary style if line end */
615 /* FIXME question: does abc\n mean: put abc, clear temp style, put \n? (would require a change) */
616 if (len>0 && wszText[len-1] == '\n')
617 ME_ClearTempStyle(editor);
618
619 ME_CommitUndo(editor);
620 if (!wParam)
621 ME_EmptyUndoStack(editor);
622 ME_UpdateRepaint(editor);
623 return 0;
624 }
625 case WM_SETTEXT:
626 {
627 LPWSTR wszText = ME_ToUnicode(hWnd, (void *)lParam);
628 TRACE("WM_SETTEXT - %s\n", (char *)(wszText)); /* debugstr_w() */
629 ME_InternalDeleteText(editor, 0, ME_GetTextLength(editor));
630 /* uses default style! */
631 ME_InsertTextFromCursor(editor, 0, wszText, -1, editor->pBuffer->pDefaultStyle);
632 ME_EndToUnicode(hWnd, wszText);
633 ME_CommitUndo(editor);
634 ME_EmptyUndoStack(editor);
635 ME_UpdateRepaint(editor);
636 return 0;
637 }
638 case WM_CUT:
639 case WM_COPY:
640 {
641 int from, to, pars;
642 WCHAR *data;
643 HANDLE hData;
644
645 if (!OpenClipboard(hWnd))
646 return 0;
647
648 EmptyClipboard();
649 ME_GetSelection(editor, &from, &to);
650 pars = ME_CountParagraphsBetween(editor, from, to);
651 hData = GlobalAlloc(GMEM_MOVEABLE, sizeof(WCHAR)*(to-from+pars+1));
652 data = (WCHAR *)GlobalLock(hData);
653 ME_GetTextW(editor, data, from, to-from, TRUE);
654 GlobalUnlock(hData);
655 SetClipboardData(CF_UNICODETEXT, hData);
656 CloseClipboard();
657 if (msg == WM_CUT)
658 {
659 ME_InternalDeleteText(editor, from, to-from);
660 ME_CommitUndo(editor);
661 ME_UpdateRepaint(editor);
662 }
663 return 0;
664 }
665 case WM_GETTEXTLENGTH:
666 return ME_GetTextLength(editor);
667 case WM_GETTEXT:
668 {
669 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
670 tr.chrg.cpMin = 0;
671 tr.chrg.cpMax = wParam-1;
672 tr.lpstrText = (WCHAR *)lParam;
673 return RichEditANSIWndProc(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
674 }
675 case EM_GETSELTEXT:
676 {
677 int from, to;
678 TEXTRANGEW tr; /* W and A differ only by rng->lpstrText */
679 ME_GetSelection(editor, &from, &to);
680 tr.chrg.cpMin = from;
681 tr.chrg.cpMax = to;
682 tr.lpstrText = (WCHAR *)lParam;
683 return RichEditANSIWndProc(hWnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
684 }
685 case EM_GETTEXTRANGE:
686 {
687 TEXTRANGEW *rng = (TEXTRANGEW *)lParam;
688 if (IsWindowUnicode(hWnd))
689 return ME_GetTextW(editor, rng->lpstrText, rng->chrg.cpMin, rng->chrg.cpMax-rng->chrg.cpMin, FALSE);
690 else
691 {
692 int nLen = rng->chrg.cpMax-rng->chrg.cpMin;
693 WCHAR *p = ALLOC_N_OBJ(WCHAR, nLen+1);
694 int nChars = ME_GetTextW(editor, p, rng->chrg.cpMin, nLen, FALSE);
695 /* FIXME this is a potential security hole (buffer overrun)
696 if you know more about wchar->mbyte conversion please explain
697 */
698 WideCharToMultiByte(CP_ACP, 0, p, nChars+1, (char *)rng->lpstrText, nLen+1, NULL, NULL);
699 FREE_OBJ(p);
700 return nChars;
701 }
702 return ME_GetTextW(editor, rng->lpstrText, rng->chrg.cpMin, rng->chrg.cpMax-rng->chrg.cpMin, FALSE);
703 }
704 case WM_CREATE:
705 ME_CommitUndo(editor);
706 ME_WrapMarkedParagraphs(editor);
707 ME_MoveCaret(editor);
708 return 0;
709 case WM_DESTROY:
710 ME_DestroyEditor(editor);
711 SetWindowLongW(hWnd, 0, 0);
712 return 0;
713 case WM_LBUTTONDOWN:
714 SetFocus(hWnd);
715 ME_LButtonDown(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
716 SetCapture(hWnd);
717 break;
718 case WM_MOUSEMOVE:
719 if (GetCapture() == hWnd)
720 ME_MouseMove(editor, (short)LOWORD(lParam), (short)HIWORD(lParam));
721 break;
722 case WM_LBUTTONUP:
723 if (GetCapture() == hWnd)
724 ReleaseCapture();
725 break;
726 case WM_PAINT:
727 hDC = BeginPaint(hWnd, &ps);
728 ME_PaintContent(editor, hDC, FALSE, &ps.rcPaint);
729 EndPaint(hWnd, &ps);
730 break;
731 case WM_SETFOCUS:
732 ME_ShowCaret(editor);
733 ME_SendOldNotify(editor, EN_SETFOCUS);
734 return 0;
735 case WM_KILLFOCUS:
736 ME_HideCaret(editor);
737 ME_SendOldNotify(editor, EN_KILLFOCUS);
738 return 0;
739 case WM_ERASEBKGND:
740 {
741 HDC hDC = (HDC)wParam;
742 RECT rc;
743 COLORREF rgbBG = ME_GetBackColor(editor);
744 if (GetUpdateRect(hWnd,&rc,TRUE))
745 {
746 HBRUSH hbr = CreateSolidBrush(rgbBG);
747 FillRect(hDC, &rc, hbr);
748 DeleteObject(hbr);
749 }
750 return 1;
751 }
752 case WM_COMMAND:
753 TRACE("editor wnd command = %d\n", LOWORD(wParam));
754 return 0;
755 case WM_KEYDOWN:
756 if (ME_ArrowKey(editor, LOWORD(wParam), GetKeyState(VK_CONTROL)<0)) {
757 ME_CommitUndo(editor);
758 ME_EnsureVisible(editor, editor->pCursors[0].pRun);
759 HideCaret(hWnd);
760 ME_MoveCaret(editor);
761 ShowCaret(hWnd);
762 return 0;
763 }
764 if (GetKeyState(VK_CONTROL)<0)
765 {
766 if (LOWORD(wParam)=='W')
767 {
768 CHARFORMAT2W chf;
769 char buf[2048];
770 ME_GetSelectionCharFormat(editor, &chf);
771 ME_DumpStyleToBuf(&chf, buf);
772 MessageBoxA(NULL, buf, "Style dump", MB_OK);
773 }
774 if (LOWORD(wParam)=='Q')
775 {
776 ME_CheckCharOffsets(editor);
777 }
778 }
779 goto do_default;
780 case WM_CHAR:
781 {
782 WCHAR wstr;
783 if (GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_READONLY) {
784 MessageBeep(MB_ICONERROR);
785 return 0; /* FIXME really 0 ? */
786 }
787 wstr = LOWORD(wParam);
788 if (((unsigned)wstr)>=' ' || wstr=='\r') {
789 /* FIXME maybe it would make sense to call EM_REPLACESEL instead ? */
790 ME_Style *style = ME_GetInsertStyle(editor, 0);
791 ME_SaveTempStyle(editor);
792 ME_InsertTextFromCursor(editor, 0, &wstr, 1, style);
793 ME_ReleaseStyle(style);
794 ME_CommitUndo(editor);
795 ME_UpdateRepaint(editor);
796 }
797 return 0;
798 }
799 case WM_VSCROLL:
800 {
801 si.cbSize = sizeof(SCROLLINFO);
802 si.fMask = SIF_PAGE|SIF_POS|SIF_RANGE|SIF_TRACKPOS;
803 GetScrollInfo(hWnd, SB_VERT, &si);
804 switch(LOWORD(wParam)) {
805 case SB_THUMBTRACK:
806 SetScrollPos(hWnd, SB_VERT, si.nTrackPos, FALSE);
807 ScrollWindow(hWnd, 0, si.nPos-si.nTrackPos, NULL, NULL);
808 /* InvalidateRect(hWnd, NULL, TRUE); */
809 UpdateWindow(hWnd);
810 break;
811 }
812 break;
813 }
814 case WM_SIZE:
815 {
816 ME_MarkAllForWrapping(editor);
817 ME_Repaint(editor);
818 return DefWindowProcW(hWnd, msg, wParam, lParam);
819 }
820 case EM_GETOLEINTERFACE:
821 {
822 LPVOID *ppvObj = (LPVOID*) lParam;
823 FIXME("EM_GETOLEINTERFACE %p: stub\n", ppvObj);
824 return CreateIRichEditOle(ppvObj);
825 }
826 default:
827 do_default:
828 return DefWindowProcW(hWnd, msg, wParam, lParam);
829 }
830 return 0L;
831 }
832
833 /******************************************************************
834 * RichEdit10ANSIWndProc (RICHED20.9)
835 */
836 LRESULT WINAPI RichEdit10ANSIWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
837 {
838 /* FIXME: this is NOT the same as 2.0 version */
839 return RichEditANSIWndProc(hWnd, msg, wParam, lParam);
840 }
841
842 void ME_SendOldNotify(ME_TextEditor *editor, int nCode)
843 {
844 HWND hWnd = editor->hWnd;
845 SendMessageA(GetParent(hWnd), WM_COMMAND, (nCode<<16)|GetWindowLongW(hWnd, GWLP_ID), (LPARAM)hWnd);
846 }
847
848 int ME_CountParagraphsBetween(ME_TextEditor *editor, int from, int to)
849 {
850 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
851 int i = 0;
852
853 while(item && item->member.para.next_para->member.para.nCharOfs <= from)
854 item = item->member.para.next_para;
855 if (!item)
856 return 0;
857 while(item && item->member.para.next_para->member.para.nCharOfs <= to) {
858 item = item->member.para.next_para;
859 i++;
860 }
861 return i;
862 }
863
864 int ME_GetTextW(ME_TextEditor *editor, WCHAR *buffer, int nStart, int nChars, int bCRLF)
865 {
866 ME_DisplayItem *item = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
867 int nWritten = 0;
868
869 while(item && item->member.para.next_para->member.para.nCharOfs <= nStart)
870 item = ME_FindItemFwd(item, diParagraph);
871 if (!item) {
872 *buffer = L'\0';
873 return 0;
874 }
875 nStart -= item->member.para.nCharOfs;
876
877 do {
878 item = ME_FindItemFwd(item, diRun);
879 } while(item && (item->member.run.nCharOfs + ME_StrLen(item->member.run.strText) <= nStart));
880 assert(item);
881
882 nStart -= item->member.run.nCharOfs;
883
884 if (nStart)
885 {
886 int nLen = ME_StrLen(item->member.run.strText) - nStart;
887 if (nLen > nChars)
888 nLen = nChars;
889 CopyMemory(buffer, item->member.run.strText->szData + nStart, sizeof(WCHAR)*nLen);
890 nChars -= nLen;
891 nWritten += nLen;
892 if (!nChars)
893 return nWritten;
894 buffer += nLen;
895 nStart = 0;
896 item = ME_FindItemFwd(item, diRun);
897 }
898
899 while(nChars && item)
900 {
901 int nLen = ME_StrLen(item->member.run.strText);
902 if (nLen > nChars)
903 nLen = nChars;
904
905 if (item->member.run.nFlags & MERF_ENDPARA)
906 {
907 if (bCRLF) {
908 *buffer++ = '\r';
909 nWritten++;
910 }
911 *buffer = '\n';
912 assert(nLen == 1);
913 }
914 else
915 CopyMemory(buffer, item->member.run.strText->szData, sizeof(WCHAR)*nLen);
916 nChars -= nLen;
917 nWritten += nLen;
918 buffer += nLen;
919
920 if (!nChars)
921 {
922 *buffer = L'\0';
923 return nWritten;
924 }
925 item = ME_FindItemFwd(item, diRun);
926 }
927 *buffer = L'\0';
928 return nWritten;
929 }
930
931 static WCHAR wszClassName[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '2', '0', 'W', 0};
932 static WCHAR wszClassName50[] = {'R', 'i', 'c', 'h', 'E', 'd', 'i', 't', '5', '0', 'W', 0};
933
934 void ME_RegisterEditorClass(HINSTANCE hInstance)
935 {
936 BOOL bResult;
937 WNDCLASSW wcW;
938 WNDCLASSA wcA;
939
940 wcW.style = CS_HREDRAW | CS_VREDRAW;
941 wcW.lpfnWndProc = RichEditANSIWndProc;
942 wcW.cbClsExtra = 0;
943 wcW.cbWndExtra = 4;
944 wcW.hInstance = NULL; /* hInstance would register DLL-local class */
945 wcW.hIcon = NULL;
946 wcW.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
947 wcW.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
948 wcW.lpszMenuName = NULL;
949 wcW.lpszClassName = wszClassName;
950 bResult = RegisterClassW(&wcW);
951 assert(bResult);
952 wcW.lpszClassName = wszClassName50;
953 bResult = RegisterClassW(&wcW);
954 assert(bResult);
955
956 wcA.style = CS_HREDRAW | CS_VREDRAW;
957 wcA.lpfnWndProc = RichEditANSIWndProc;
958 wcA.cbClsExtra = 0;
959 wcA.cbWndExtra = 4;
960 wcA.hInstance = NULL; /* hInstance would register DLL-local class */
961 wcA.hIcon = NULL;
962 wcA.hCursor = LoadCursorW(NULL, MAKEINTRESOURCEW(IDC_IBEAM));
963 wcA.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
964 wcA.lpszMenuName = NULL;
965 wcA.lpszClassName = "RichEdit20A";
966 bResult = RegisterClassA(&wcA);
967 assert(bResult);
968 wcA.lpszClassName = "RichEdit50A";
969 bResult = RegisterClassA(&wcA);
970 assert(bResult);
971 }
972
973 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
974 {
975 TRACE("\n");
976 switch (fdwReason)
977 {
978 case DLL_PROCESS_ATTACH:
979 DisableThreadLibraryCalls(hinstDLL);
980 me_heap = HeapCreate (0, 0x10000, 0);
981 ME_RegisterEditorClass(hinstDLL);
982 break;
983
984 case DLL_PROCESS_DETACH:
985 UnregisterClassW(wszClassName, 0);
986 UnregisterClassW(wszClassName50, 0);
987 UnregisterClassA("RichEdit20A", 0);
988 UnregisterClassA("RichEdit50A", 0);
989 HeapDestroy (me_heap);
990 me_heap = NULL;
991 break;
992 }
993 return TRUE;
994 }
995
996 /******************************************************************
997 * CreateTextServices (RICHED20.4)
998 *
999 * FIXME should be ITextHost instead of void*
1000 */
1001 HRESULT WINAPI CreateTextServices(IUnknown *punkOuter, void *pITextHost,
1002 IUnknown **ppUnk)
1003 {
1004 FIXME("stub\n");
1005 /* FIXME should support aggregation */
1006 if (punkOuter)
1007 return CLASS_E_NOAGGREGATION;
1008
1009 return E_FAIL; /* E_NOTIMPL isn't allowed by MSDN */
1010 }
1011
1012 /******************************************************************
1013 * REExtendedRegisterClass (RICHED20.8)
1014 *
1015 * FIXME undocumented
1016 */
1017 void WINAPI REExtendedRegisterClass(void)
1018 {
1019 FIXME("stub\n");
1020 }