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