- Sync with trunk up to r46941.
[reactos.git] / base / applications / rapps / richedit.c
1 /*
2 * PROJECT: ReactOS Applications Manager
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/applications/rapps/richedit.c
5 * PURPOSE: RichEdit functions
6 * PROGRAMMERS: Dmitry Chapyshev (dmitry@reactos.org)
7 */
8
9 #include "rapps.h"
10
11 HWND hRichEdit;
12 PWSTR pLink = NULL;
13
14
15 VOID
16 RichEditOnLink(HWND hwnd, ENLINK *Link)
17 {
18 switch (Link->msg)
19 {
20 case WM_LBUTTONUP:
21 case WM_RBUTTONUP:
22 {
23 if (pLink) HeapFree(GetProcessHeap(), 0, pLink);
24
25 pLink = (PWSTR) HeapAlloc(GetProcessHeap(),
26 0,
27 (max(Link->chrg.cpMin, Link->chrg.cpMax) -
28 min(Link->chrg.cpMin, Link->chrg.cpMax))*sizeof(WCHAR));
29 if (!pLink)
30 {
31 /* TODO: Error message */
32 return;
33 }
34
35 SendMessageW(hRichEdit, EM_SETSEL, Link->chrg.cpMin, Link->chrg.cpMax);
36 SendMessageW(hRichEdit, EM_GETSELTEXT, 0, (LPARAM)pLink);
37
38 ShowPopupMenu(hwnd, IDR_LINKMENU);
39 }
40 break;
41 }
42 }
43
44 static VOID
45 SetRangeFormatting(LONG Start, LONG End, DWORD dwEffects)
46 {
47 CHARFORMAT2 CharFormat;
48
49 SendMessageW(hRichEdit, EM_SETSEL, Start, End);
50
51 ZeroMemory(&CharFormat, sizeof(CHARFORMAT2));
52
53 CharFormat.cbSize = sizeof(CHARFORMAT2);
54 CharFormat.dwMask = dwEffects;
55 CharFormat.dwEffects = dwEffects;
56
57 SendMessageW(hRichEdit, EM_SETCHARFORMAT, SCF_WORD | SCF_SELECTION, (LPARAM)&CharFormat);
58
59 SendMessageW(hRichEdit, EM_SETSEL, End, End + 1);
60 }
61
62 static LONG
63 GetRichEditTextLen(VOID)
64 {
65 GETTEXTLENGTHEX TxtLenStruct;
66
67 TxtLenStruct.flags = GTL_NUMCHARS;
68 TxtLenStruct.codepage = 1200;
69
70 return (LONG) SendMessageW(hRichEdit, EM_GETTEXTLENGTHEX, (WPARAM)&TxtLenStruct, 0);
71 }
72
73 /*
74 * Insert text (without cleaning old text)
75 * Supported effects:
76 * - CFM_BOLD
77 * - CFM_ITALIC
78 * - CFM_UNDERLINE
79 * - CFM_LINK
80 */
81 VOID
82 InsertRichEditText(LPCWSTR lpszText, DWORD dwEffects)
83 {
84 SETTEXTEX SetText;
85 LONG Len = GetRichEditTextLen();
86
87 /* Insert new text */
88 SetText.flags = ST_SELECTION;
89 SetText.codepage = 1200;
90
91 SendMessageW(hRichEdit, EM_SETTEXTEX, (WPARAM)&SetText, (LPARAM)lpszText);
92
93 SetRangeFormatting(Len, Len + wcslen(lpszText),
94 (dwEffects == CFM_LINK) ? (PathIsURLW(lpszText) ? dwEffects : 0) : dwEffects);
95 }
96
97 /*
98 * Clear old text and add new
99 */
100 VOID
101 NewRichEditText(LPCWSTR lpszText, DWORD dwEffects)
102 {
103 SetWindowTextW(hRichEdit, L"");
104 InsertRichEditText(lpszText, dwEffects);
105 }
106
107 BOOL
108 CreateRichEdit(HWND hwnd)
109 {
110 LoadLibraryW(L"riched20.dll");
111
112 hRichEdit = CreateWindowExW(0,
113 L"RichEdit20W",
114 NULL,
115 WS_CHILD | WS_VISIBLE | ES_MULTILINE |
116 ES_LEFT | ES_READONLY,
117 205, 28, 465, 100,
118 hwnd,
119 NULL,
120 hInst,
121 NULL);
122
123 if (!hRichEdit)
124 {
125 /* TODO: Show error message */
126 return FALSE;
127 }
128
129 SendMessageW(hRichEdit, EM_SETBKGNDCOLOR, 0, GetSysColor(COLOR_BTNFACE));
130 SendMessageW(hRichEdit, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), 0);
131 SendMessageW(hRichEdit, EM_SETEVENTMASK, 0, ENM_LINK | ENM_MOUSEEVENTS);
132 SendMessageW(hRichEdit, EM_SHOWSCROLLBAR, SB_VERT, TRUE);
133
134 return TRUE;
135 }