moved several strings to the application resources
[reactos.git] / reactos / subsys / system / regedit / childwnd.c
1 /*
2 * Regedit child window
3 *
4 * Copyright (C) 2002 Robert Dickenson <robd@reactos.org>
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 #define WIN32_LEAN_AND_MEAN /* Exclude rarely-used stuff from Windows headers */
22 #include <windows.h>
23 #include <commctrl.h>
24 #include <tchar.h>
25 #include <stdio.h>
26
27 #include "main.h"
28
29 ChildWnd* g_pChildWnd;
30 HBITMAP SizingPattern = 0;
31 HBRUSH SizingBrush = 0;
32
33 /*******************************************************************************
34 * Local module support methods
35 */
36
37 static LPCTSTR get_root_key_name(HKEY hRootKey)
38 {
39 if (hRootKey == HKEY_CLASSES_ROOT) return _T("HKEY_CLASSES_ROOT");
40 if (hRootKey == HKEY_CURRENT_USER) return _T("HKEY_CURRENT_USER");
41 if (hRootKey == HKEY_LOCAL_MACHINE) return _T("HKEY_LOCAL_MACHINE");
42 if (hRootKey == HKEY_USERS) return _T("HKEY_USERS");
43 if (hRootKey == HKEY_CURRENT_CONFIG) return _T("HKEY_CURRENT_CONFIG");
44 return _T("UKNOWN HKEY, PLEASE REPORT");
45 }
46
47 static void draw_splitbar(HWND hWnd, int x)
48 {
49 RECT rt;
50 HGDIOBJ OldObj;
51 HDC hdc = GetDC(hWnd);
52
53 if(!SizingPattern)
54 {
55 const DWORD Pattern[4] = {0x5555AAAA, 0x5555AAAA, 0x5555AAAA, 0x5555AAAA};
56 SizingPattern = CreateBitmap(8, 8, 1, 1, Pattern);
57 }
58 if(!SizingBrush)
59 {
60 SizingBrush = CreatePatternBrush(SizingPattern);
61 }
62 GetClientRect(hWnd, &rt);
63 rt.left = x - SPLIT_WIDTH/2;
64 rt.right = x + SPLIT_WIDTH/2+1;
65 OldObj = SelectObject(hdc, SizingBrush);
66 PatBlt(hdc, rt.left, rt.top, rt.right - rt.left, rt.bottom - rt.top, PATINVERT);
67 SelectObject(hdc, OldObj);
68 ReleaseDC(hWnd, hdc);
69 }
70
71 static void ResizeWnd(ChildWnd* pChildWnd, int cx, int cy)
72 {
73 HDWP hdwp = BeginDeferWindowPos(2);
74 RECT rt = {0, 0, cx, cy};
75
76 cx = pChildWnd->nSplitPos + SPLIT_WIDTH/2;
77 DeferWindowPos(hdwp, pChildWnd->hTreeWnd, 0, rt.left, rt.top, pChildWnd->nSplitPos-SPLIT_WIDTH/2-rt.left, rt.bottom-rt.top, SWP_NOZORDER|SWP_NOACTIVATE);
78 DeferWindowPos(hdwp, pChildWnd->hListWnd, 0, rt.left+cx , rt.top, rt.right-cx, rt.bottom-rt.top, SWP_NOZORDER|SWP_NOACTIVATE);
79 EndDeferWindowPos(hdwp);
80 }
81
82 static void OnPaint(HWND hWnd)
83 {
84 PAINTSTRUCT ps;
85 RECT rt;
86 HDC hdc;
87
88 GetClientRect(hWnd, &rt);
89 hdc = BeginPaint(hWnd, &ps);
90 FillRect(ps.hdc, &rt, GetSysColorBrush(COLOR_BTNFACE));
91 EndPaint(hWnd, &ps);
92 }
93
94 /*******************************************************************************
95 *
96 * FUNCTION: _CmdWndProc(HWND, unsigned, WORD, LONG)
97 *
98 * PURPOSE: Processes WM_COMMAND messages for the main frame window.
99 *
100 */
101
102 static BOOL _CmdWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
103 {
104 switch (LOWORD(wParam)) {
105 /* Parse the menu selections: */
106 case ID_REGISTRY_EXIT:
107 DestroyWindow(hWnd);
108 break;
109 case ID_VIEW_REFRESH:
110 /* TODO */
111 break;
112 default:
113 return FALSE;
114 }
115 return TRUE;
116 }
117
118 /*******************************************************************************
119 *
120 * FUNCTION: ChildWndProc(HWND, unsigned, WORD, LONG)
121 *
122 * PURPOSE: Processes messages for the child windows.
123 *
124 * WM_COMMAND - process the application menu
125 * WM_PAINT - Paint the main window
126 * WM_DESTROY - post a quit message and return
127 *
128 */
129 LRESULT CALLBACK ChildWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
130 {
131 static short last_split;
132 BOOL Result;
133 ChildWnd* pChildWnd = g_pChildWnd;
134
135 switch (message) {
136 case WM_CREATE:
137 {
138 TCHAR buffer[MAX_PATH];
139 /* load "My Computer" string */
140 LoadString(hInst, IDS_MY_COMPUTER, buffer, sizeof(buffer)/sizeof(TCHAR));
141
142 g_pChildWnd = pChildWnd = HeapAlloc(GetProcessHeap(), 0, sizeof(ChildWnd));
143 if (!pChildWnd) return 0;
144 _tcsncpy(pChildWnd->szPath, buffer, MAX_PATH);
145 pChildWnd->nSplitPos = 250;
146 pChildWnd->hWnd = hWnd;
147 pChildWnd->hTreeWnd = CreateTreeView(hWnd, pChildWnd->szPath, TREE_WINDOW);
148 pChildWnd->hListWnd = CreateListView(hWnd, LIST_WINDOW/*, pChildWnd->szPath*/);
149 SetFocus(pChildWnd->hTreeWnd);
150 break;
151 }
152 case WM_COMMAND:
153 if (!_CmdWndProc(hWnd, message, wParam, lParam)) {
154 goto def;
155 }
156 break;
157 case WM_PAINT:
158 OnPaint(hWnd);
159 return 0;
160 case WM_SETCURSOR:
161 if (LOWORD(lParam) == HTCLIENT) {
162 POINT pt;
163 GetCursorPos(&pt);
164 ScreenToClient(hWnd, &pt);
165 if (pt.x>=pChildWnd->nSplitPos-SPLIT_WIDTH/2 && pt.x<pChildWnd->nSplitPos+SPLIT_WIDTH/2+1) {
166 SetCursor(LoadCursor(0, IDC_SIZEWE));
167 return TRUE;
168 }
169 }
170 goto def;
171 case WM_DESTROY:
172 HeapFree(GetProcessHeap(), 0, pChildWnd);
173 pChildWnd = NULL;
174 PostQuitMessage(0);
175 break;
176 case WM_LBUTTONDOWN: {
177 RECT rt;
178 POINTS pt;
179 pt = MAKEPOINTS(lParam);
180 GetClientRect(hWnd, &rt);
181 if (pt.x>=pChildWnd->nSplitPos-SPLIT_WIDTH/2 && pt.x<pChildWnd->nSplitPos+SPLIT_WIDTH/2+1) {
182 last_split = pChildWnd->nSplitPos;
183 draw_splitbar(hWnd, last_split);
184 SetCapture(hWnd);
185 }
186 break;
187 }
188
189 case WM_LBUTTONUP:
190 if (GetCapture() == hWnd) {
191 RECT rt;
192 POINTS pt;
193 pt = MAKEPOINTS(lParam);
194 GetClientRect(hWnd, &rt);
195 pt.x = min(max(pt.x, SPLIT_MIN), rt.right - SPLIT_MIN);
196 draw_splitbar(hWnd, last_split);
197 last_split = -1;
198 pChildWnd->nSplitPos = pt.x;
199 ResizeWnd(pChildWnd, rt.right, rt.bottom);
200 ReleaseCapture();
201 }
202 break;
203
204 case WM_CAPTURECHANGED:
205 if (GetCapture()==hWnd && last_split>=0)
206 draw_splitbar(hWnd, last_split);
207 break;
208
209 case WM_KEYDOWN:
210 if (wParam == VK_ESCAPE)
211 if (GetCapture() == hWnd) {
212 RECT rt;
213 draw_splitbar(hWnd, last_split);
214 GetClientRect(hWnd, &rt);
215 ResizeWnd(pChildWnd, rt.right, rt.bottom);
216 last_split = -1;
217 ReleaseCapture();
218 SetCursor(LoadCursor(0, IDC_ARROW));
219 }
220 break;
221
222 case WM_MOUSEMOVE:
223 if (GetCapture() == hWnd) {
224 HDC hdc;
225 RECT rt;
226 HGDIOBJ OldObj;
227 POINTS pt;
228 if(!SizingPattern)
229 {
230 const DWORD Pattern[4] = {0x5555AAAA, 0x5555AAAA, 0x5555AAAA, 0x5555AAAA};
231 SizingPattern = CreateBitmap(8, 8, 1, 1, Pattern);
232 }
233 if(!SizingBrush)
234 {
235 SizingBrush = CreatePatternBrush(SizingPattern);
236 }
237
238 pt = MAKEPOINTS(lParam);
239 GetClientRect(hWnd, &rt);
240 pt.x = min(max(pt.x, SPLIT_MIN), rt.right - SPLIT_MIN);
241 if(last_split != pt.x)
242 {
243 rt.left = last_split-SPLIT_WIDTH/2;
244 rt.right = last_split+SPLIT_WIDTH/2+1;
245 hdc = GetDC(hWnd);
246 OldObj = SelectObject(hdc, SizingBrush);
247 PatBlt(hdc, rt.left, rt.top, rt.right - rt.left, rt.bottom - rt.top, PATINVERT);
248 last_split = pt.x;
249 rt.left = pt.x-SPLIT_WIDTH/2;
250 rt.right = pt.x+SPLIT_WIDTH/2+1;
251 PatBlt(hdc, rt.left, rt.top, rt.right - rt.left, rt.bottom - rt.top, PATINVERT);
252 SelectObject(hdc, OldObj);
253 ReleaseDC(hWnd, hdc);
254 }
255 }
256 break;
257
258 case WM_SETFOCUS:
259 if (pChildWnd != NULL) {
260 SetFocus(pChildWnd->nFocusPanel? pChildWnd->hListWnd: pChildWnd->hTreeWnd);
261 }
262 break;
263
264 case WM_TIMER:
265 break;
266
267 case WM_NOTIFY:
268 if ((int)wParam == TREE_WINDOW) {
269 switch (((LPNMHDR)lParam)->code) {
270 case TVN_ITEMEXPANDING:
271 return !OnTreeExpanding(pChildWnd->hTreeWnd, (NMTREEVIEW*)lParam);
272 case TVN_SELCHANGED: {
273 LPCTSTR keyPath, rootName;
274 LPTSTR fullPath;
275 HKEY hRootKey;
276
277 keyPath = GetItemPath(pChildWnd->hTreeWnd, ((NMTREEVIEW*)lParam)->itemNew.hItem, &hRootKey);
278 if(!hRootKey)
279 {
280 RefreshListView(pChildWnd->hListWnd, 0, NULL);
281 }
282 if (keyPath) {
283 RefreshListView(pChildWnd->hListWnd, hRootKey, keyPath);
284 rootName = get_root_key_name(hRootKey);
285 fullPath = HeapAlloc(GetProcessHeap(), 0, (_tcslen(rootName) + 1 + _tcslen(keyPath) + 1) * sizeof(TCHAR));
286 if (fullPath) {
287 _stprintf(fullPath, _T("%s\\%s"), rootName, keyPath);
288 SendMessage(hStatusBar, SB_SETTEXT, 0, (LPARAM)fullPath);
289 HeapFree(GetProcessHeap(), 0, fullPath);
290 }
291 }
292 }
293 break;
294 case NM_SETFOCUS:
295 pChildWnd->nFocusPanel = 1;
296 break;
297 default:
298 goto def;
299 }
300 } else
301 {
302 if ((int)wParam == LIST_WINDOW)
303 {
304 switch (((LPNMHDR)lParam)->code) {
305 case NM_SETFOCUS:
306 pChildWnd->nFocusPanel = 0;
307 break;
308 default:
309 if(ListWndNotifyProc(pChildWnd->hListWnd, wParam, lParam, &Result))
310 {
311 return Result;
312 }
313 break;
314 }
315 }
316 }
317 break;
318
319 case WM_CONTEXTMENU:
320 {
321 POINTS pt;
322 if((HWND)wParam == pChildWnd->hListWnd)
323 {
324 int i, cnt;
325 BOOL IsDefault;
326 pt = MAKEPOINTS(lParam);
327 cnt = ListView_GetSelectedCount(pChildWnd->hListWnd);
328 i = ListView_GetNextItem(pChildWnd->hListWnd, -1, LVNI_FOCUSED | LVNI_SELECTED);
329 if(i == -1)
330 {
331 TrackPopupMenu(GetSubMenu(hPopupMenus, PM_NEW), TPM_RIGHTBUTTON, pt.x, pt.y, 0, hFrameWnd, NULL);
332 }
333 else
334 {
335 HMENU mnu = GetSubMenu(hPopupMenus, PM_MODIFYVALUE);
336 SetMenuDefaultItem(mnu, ID_EDIT_MODIFY, MF_BYCOMMAND);
337 IsDefault = IsDefaultValue(pChildWnd->hListWnd, i);
338 if(cnt == 1)
339 EnableMenuItem(mnu, ID_EDIT_RENAME, MF_BYCOMMAND | (IsDefault ? MF_DISABLED | MF_GRAYED : MF_ENABLED));
340 else
341 EnableMenuItem(mnu, ID_EDIT_RENAME, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
342 EnableMenuItem(mnu, ID_EDIT_MODIFY, MF_BYCOMMAND | (cnt == 1 ? MF_ENABLED : MF_DISABLED | MF_GRAYED));
343 EnableMenuItem(mnu, ID_EDIT_MODIFY_BIN, MF_BYCOMMAND | (cnt == 1 ? MF_ENABLED : MF_DISABLED | MF_GRAYED));
344
345 TrackPopupMenu(mnu, TPM_RIGHTBUTTON, pt.x, pt.y, 0, hFrameWnd, NULL);
346 }
347 }
348 break;
349 }
350
351 case WM_SIZE:
352 if (wParam != SIZE_MINIMIZED && pChildWnd != NULL) {
353 ResizeWnd(pChildWnd, LOWORD(lParam), HIWORD(lParam));
354 }
355 /* fall through */
356 default: def:
357 return DefWindowProc(hWnd, message, wParam, lParam);
358 }
359 return 0;
360 }