regedit: Use a separate pop-up menu for the 'Computer' root node.
[wine.git] / programs / regedit / childwnd.c
blob34c6a942e239c3d407448e5f5b8dad9b03cc6f87
1 /*
2 * Regedit child window
4 * Copyright (C) 2002 Robert Dickenson <robd@reactos.org>
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.
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #define WIN32_LEAN_AND_MEAN /* Exclude rarely-used stuff from Windows headers */
22 #include <windows.h>
23 #include <commctrl.h>
24 #include <stdio.h>
26 #include "main.h"
27 #include "regproc.h"
29 #include "wine/debug.h"
30 #include "wine/unicode.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(regedit);
34 ChildWnd* g_pChildWnd;
35 static int last_split;
37 static const WCHAR wszLastKey[] = {'L','a','s','t','K','e','y',0};
38 static const WCHAR wszKeyName[] = {'S','o','f','t','w','a','r','e','\\',
39 'M','i','c','r','o','s','o','f','t','\\',
40 'W','i','n','d','o','w','s','\\',
41 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
42 'A','p','p','l','e','t','s','\\','R','e','g','e','d','i','t',0};
44 /*******************************************************************************
45 * Local module support methods
48 static LPCWSTR GetRootKeyName(HKEY hRootKey)
50 if(hRootKey == HKEY_CLASSES_ROOT)
51 return reg_class_namesW[INDEX_HKEY_CLASSES_ROOT];
52 if(hRootKey == HKEY_CURRENT_USER)
53 return reg_class_namesW[INDEX_HKEY_CURRENT_USER];
54 if(hRootKey == HKEY_LOCAL_MACHINE)
55 return reg_class_namesW[INDEX_HKEY_LOCAL_MACHINE];
56 if(hRootKey == HKEY_USERS)
57 return reg_class_namesW[INDEX_HKEY_USERS];
58 if(hRootKey == HKEY_CURRENT_CONFIG)
59 return reg_class_namesW[INDEX_HKEY_CURRENT_CONFIG];
60 if(hRootKey == HKEY_DYN_DATA)
61 return reg_class_namesW[INDEX_HKEY_DYN_DATA];
62 else
64 static const WCHAR unknown_key[] = {'U','N','K','N','O','W','N',' ','H','K','E','Y',',',' ',
65 'P','L','E','A','S','E',' ','R','E','P','O','R','T',0};
66 return unknown_key;
70 static void draw_splitbar(HWND hWnd, int x)
72 RECT rt;
73 HDC hdc = GetDC(hWnd);
75 GetClientRect(hWnd, &rt);
76 rt.left = x - SPLIT_WIDTH/2;
77 rt.right = x + SPLIT_WIDTH/2+1;
78 InvertRect(hdc, &rt);
79 ReleaseDC(hWnd, hdc);
82 static void ResizeWnd(int cx, int cy)
84 HDWP hdwp = BeginDeferWindowPos(2);
85 RECT rt = {0, 0, cx, cy};
87 cx = g_pChildWnd->nSplitPos + SPLIT_WIDTH/2;
88 DeferWindowPos(hdwp, g_pChildWnd->hTreeWnd, 0, rt.left, rt.top, g_pChildWnd->nSplitPos-SPLIT_WIDTH/2-rt.left, rt.bottom-rt.top, SWP_NOZORDER|SWP_NOACTIVATE);
89 DeferWindowPos(hdwp, g_pChildWnd->hListWnd, 0, rt.left+cx , rt.top, rt.right-cx, rt.bottom-rt.top, SWP_NOZORDER|SWP_NOACTIVATE);
90 EndDeferWindowPos(hdwp);
93 static void OnPaint(HWND hWnd)
95 PAINTSTRUCT ps;
96 RECT rt;
98 GetClientRect(hWnd, &rt);
99 BeginPaint(hWnd, &ps);
100 FillRect(ps.hdc, &rt, GetSysColorBrush(COLOR_BTNFACE));
101 EndPaint(hWnd, &ps);
104 static LPWSTR CombinePaths(LPCWSTR pPaths[], int nPaths) {
105 int i, len, pos;
106 LPWSTR combined;
107 for (i=0, len=0; i<nPaths; i++) {
108 if (pPaths[i] && *pPaths[i]) {
109 len += lstrlenW(pPaths[i])+1;
112 combined = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
113 *combined = '\0';
114 for (i=0, pos=0; i<nPaths; i++) {
115 if (pPaths[i] && *pPaths[i]) {
116 int llen = lstrlenW(pPaths[i]);
117 if (!*combined)
118 lstrcpyW(combined, pPaths[i]);
119 else {
120 combined[pos++] = '\\';
121 lstrcpyW(combined+pos, pPaths[i]);
123 pos += llen;
126 return combined;
129 static LPWSTR GetPathRoot(HWND hwndTV, HTREEITEM hItem, BOOL bFull) {
130 LPCWSTR parts[2] = {0,0};
131 WCHAR text[260];
132 HKEY hRootKey = NULL;
133 if (!hItem)
134 hItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CARET, 0);
135 HeapFree(GetProcessHeap(), 0, GetItemPath(hwndTV, hItem, &hRootKey));
136 if (!bFull && !hRootKey)
137 return NULL;
138 if (hRootKey)
139 parts[1] = GetRootKeyName(hRootKey);
140 if (bFull) {
141 DWORD dwSize = sizeof(text)/sizeof(WCHAR);
142 GetComputerNameW(text, &dwSize);
143 parts[0] = text;
145 return CombinePaths(parts, 2);
148 LPWSTR GetItemFullPath(HWND hwndTV, HTREEITEM hItem, BOOL bFull) {
149 LPWSTR parts[2];
150 LPWSTR ret;
151 HKEY hRootKey = NULL;
153 parts[0] = GetPathRoot(hwndTV, hItem, bFull);
154 parts[1] = GetItemPath(hwndTV, hItem, &hRootKey);
155 ret = CombinePaths((LPCWSTR *)parts, 2);
156 HeapFree(GetProcessHeap(), 0, parts[0]);
157 HeapFree(GetProcessHeap(), 0, parts[1]);
158 return ret;
161 static LPWSTR GetPathFullPath(HWND hwndTV, LPWSTR path) {
162 LPWSTR parts[2];
163 LPWSTR ret;
165 parts[0] = GetPathRoot(hwndTV, 0, TRUE);
166 parts[1] = path;
167 ret = CombinePaths((LPCWSTR*)parts, 2);
168 HeapFree(GetProcessHeap(), 0, parts[0]);
169 return ret;
172 static void OnTreeSelectionChanged(HWND hwndTV, HWND hwndLV, HTREEITEM hItem, BOOL bRefreshLV)
174 if (bRefreshLV) {
175 LPWSTR keyPath;
176 HKEY hRootKey = NULL;
177 HTREEITEM rootitem;
179 rootitem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_ROOT, 0);
180 if (rootitem == hItem)
182 SendMessageW(hwndLV, LVM_DELETEALLITEMS, 0, 0);
183 return;
186 keyPath = GetItemPath(hwndTV, hItem, &hRootKey);
187 RefreshListView(hwndLV, hRootKey, keyPath, NULL);
188 HeapFree(GetProcessHeap(), 0, keyPath);
190 UpdateStatusBar();
193 /*******************************************************************************
194 * finish_splitbar [internal]
196 * make the splitbar invisible and resize the windows
197 * (helper for ChildWndProc)
199 static void finish_splitbar(HWND hWnd, int x)
201 RECT rt;
203 draw_splitbar(hWnd, last_split);
204 last_split = -1;
205 GetClientRect(hWnd, &rt);
206 g_pChildWnd->nSplitPos = x;
207 ResizeWnd(rt.right, rt.bottom);
208 ReleaseCapture();
211 /*******************************************************************************
213 * FUNCTION: _CmdWndProc(HWND, unsigned, WORD, LONG)
215 * PURPOSE: Processes WM_COMMAND messages for the main frame window.
219 static BOOL _CmdWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
221 switch (LOWORD(wParam)) {
222 /* Parse the menu selections: */
223 case ID_REGISTRY_EXIT:
224 DestroyWindow(hWnd);
225 break;
226 case ID_VIEW_REFRESH:
227 WINE_TRACE("Is this ever called or is it just dead code?\n");
228 /* TODO */
229 break;
230 case ID_SWITCH_PANELS:
231 g_pChildWnd->nFocusPanel = !g_pChildWnd->nFocusPanel;
232 SetFocus(g_pChildWnd->nFocusPanel? g_pChildWnd->hListWnd: g_pChildWnd->hTreeWnd);
233 break;
234 default:
235 return FALSE;
237 return TRUE;
240 /*******************************************************************************
241 * get_last_key [internal]
243 * open last key
246 static void get_last_key(HWND hwndTV)
248 HKEY hkey;
249 WCHAR wszVal[KEY_MAX_LEN];
250 DWORD dwSize = sizeof(wszVal);
252 if (RegCreateKeyExW(HKEY_CURRENT_USER, wszKeyName, 0, NULL, 0, KEY_READ, NULL, &hkey, NULL) == ERROR_SUCCESS)
254 if (RegQueryValueExW(hkey, wszLastKey, NULL, NULL, (LPBYTE)wszVal, &dwSize) == ERROR_SUCCESS)
256 HTREEITEM selection;
257 if (!strcmpW(wszVal, g_pChildWnd->szPath))
258 selection = (HTREEITEM)SendMessageW(g_pChildWnd->hTreeWnd, TVM_GETNEXTITEM, TVGN_ROOT, 0);
259 else
260 selection = FindPathInTree(hwndTV, wszVal);
261 SendMessageW(hwndTV, TVM_SELECTITEM, TVGN_CARET, (LPARAM)selection);
264 RegCloseKey(hkey);
268 /*******************************************************************************
269 * set_last_key [internal]
271 * save last key
274 static void set_last_key(HWND hwndTV)
276 HKEY hkey;
278 if (RegCreateKeyExW(HKEY_CURRENT_USER, wszKeyName, 0, NULL, 0, KEY_WRITE, NULL, &hkey, NULL) == ERROR_SUCCESS)
280 HTREEITEM selection = (HTREEITEM)SendMessageW(g_pChildWnd->hTreeWnd, TVM_GETNEXTITEM, TVGN_CARET, 0);
281 HTREEITEM root = (HTREEITEM)SendMessageW(g_pChildWnd->hTreeWnd, TVM_GETNEXTITEM, TVGN_ROOT, 0);
282 WCHAR *value;
284 if (selection == root)
285 value = g_pChildWnd->szPath;
286 else
287 value = GetItemFullPath(g_pChildWnd->hTreeWnd, selection, FALSE);
288 RegSetValueExW(hkey, wszLastKey, 0, REG_SZ, (LPBYTE)value, (lstrlenW(value) + 1) * sizeof(WCHAR));
289 if (selection != root)
290 HeapFree(GetProcessHeap(), 0, value);
291 RegCloseKey(hkey);
295 #define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
296 #define GET_Y_LPARAM(lp) ((int)(short)HIWORD(lp))
298 /*******************************************************************************
300 * FUNCTION: ChildWndProc(HWND, unsigned, WORD, LONG)
302 * PURPOSE: Processes messages for the child windows.
304 * WM_COMMAND - process the application menu
305 * WM_PAINT - Paint the main window
306 * WM_DESTROY - post a quit message and return
309 LRESULT CALLBACK ChildWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
311 switch (message) {
312 case WM_CREATE:
313 g_pChildWnd = HeapAlloc(GetProcessHeap(), 0, sizeof(ChildWnd));
314 if (!g_pChildWnd) return 0;
315 LoadStringW(hInst, IDS_REGISTRY_ROOT_NAME, g_pChildWnd->szPath, MAX_PATH);
316 g_pChildWnd->nSplitPos = 250;
317 g_pChildWnd->hWnd = hWnd;
318 g_pChildWnd->hTreeWnd = CreateTreeView(hWnd, g_pChildWnd->szPath, TREE_WINDOW);
319 g_pChildWnd->hListWnd = CreateListView(hWnd, LIST_WINDOW/*, g_pChildWnd->szPath*/);
320 g_pChildWnd->nFocusPanel = 1;
321 SetFocus(g_pChildWnd->hTreeWnd);
322 get_last_key(g_pChildWnd->hTreeWnd);
323 break;
324 case WM_COMMAND:
325 if (!_CmdWndProc(hWnd, message, wParam, lParam)) {
326 goto def;
328 break;
329 case WM_PAINT:
330 OnPaint(hWnd);
331 return 0;
332 case WM_SETCURSOR:
333 if (LOWORD(lParam) == HTCLIENT) {
334 POINT pt;
335 GetCursorPos(&pt);
336 ScreenToClient(hWnd, &pt);
337 if (pt.x>=g_pChildWnd->nSplitPos-SPLIT_WIDTH/2 && pt.x<g_pChildWnd->nSplitPos+SPLIT_WIDTH/2+1) {
338 SetCursor(LoadCursorW(0, (LPCWSTR)IDC_SIZEWE));
339 return TRUE;
342 goto def;
343 case WM_DESTROY:
344 set_last_key(g_pChildWnd->hTreeWnd);
345 HeapFree(GetProcessHeap(), 0, g_pChildWnd);
346 g_pChildWnd = NULL;
347 PostQuitMessage(0);
348 break;
349 case WM_LBUTTONDOWN: {
350 RECT rt;
351 int x = (short)LOWORD(lParam);
352 GetClientRect(hWnd, &rt);
353 if (x>=g_pChildWnd->nSplitPos-SPLIT_WIDTH/2 && x<g_pChildWnd->nSplitPos+SPLIT_WIDTH/2+1) {
354 last_split = g_pChildWnd->nSplitPos;
355 draw_splitbar(hWnd, last_split);
356 SetCapture(hWnd);
358 break;
361 /* WM_RBUTTONDOWN sets the splitbar the same way as WM_LBUTTONUP */
362 case WM_LBUTTONUP:
363 case WM_RBUTTONDOWN:
364 if (GetCapture() == hWnd) {
365 finish_splitbar(hWnd, LOWORD(lParam));
367 break;
369 case WM_CAPTURECHANGED:
370 if (GetCapture()==hWnd && last_split>=0)
371 draw_splitbar(hWnd, last_split);
372 break;
374 case WM_CONTEXTMENU: {
375 POINT pt = {GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)};
376 TVHITTESTINFO ht;
377 ht.pt = pt;
378 ScreenToClient(g_pChildWnd->hTreeWnd, &ht.pt);
379 if (SendMessageW(g_pChildWnd->hTreeWnd, TVM_HITTEST, 0, (LPARAM)&ht)) {
380 HTREEITEM root;
381 SendMessageW(g_pChildWnd->hTreeWnd, TVM_SELECTITEM, TVGN_CARET, (LPARAM)ht.hItem);
382 root = (HTREEITEM)SendMessageW(g_pChildWnd->hTreeWnd, TVM_GETNEXTITEM, TVGN_ROOT, 0);
383 TrackPopupMenu(GetSubMenu(hPopupMenus, ht.hItem == root ? PM_COMPUTER : PM_TREEVIEW),
384 TPM_RIGHTBUTTON, pt.x, pt.y, 0, hFrameWnd, NULL);
386 break;
389 case WM_KEYDOWN:
390 if (wParam == VK_ESCAPE)
391 if (GetCapture() == hWnd) {
392 RECT rt;
393 draw_splitbar(hWnd, last_split);
394 GetClientRect(hWnd, &rt);
395 ResizeWnd(rt.right, rt.bottom);
396 last_split = -1;
397 ReleaseCapture();
398 SetCursor(LoadCursorW(0, (LPCWSTR)IDC_ARROW));
400 break;
402 case WM_MOUSEMOVE:
403 if (GetCapture() == hWnd) {
404 RECT rt;
405 int x = LOWORD(lParam);
406 HDC hdc = GetDC(hWnd);
407 GetClientRect(hWnd, &rt);
408 rt.left = last_split-SPLIT_WIDTH/2;
409 rt.right = last_split+SPLIT_WIDTH/2+1;
410 InvertRect(hdc, &rt);
411 last_split = x;
412 rt.left = x-SPLIT_WIDTH/2;
413 rt.right = x+SPLIT_WIDTH/2+1;
414 InvertRect(hdc, &rt);
415 ReleaseDC(hWnd, hdc);
417 break;
419 case WM_SETFOCUS:
420 if (g_pChildWnd != NULL) {
421 SetFocus(g_pChildWnd->nFocusPanel? g_pChildWnd->hListWnd: g_pChildWnd->hTreeWnd);
423 break;
425 case WM_TIMER:
426 break;
428 case WM_NOTIFY:
429 if (((int)wParam == TREE_WINDOW) && (g_pChildWnd != NULL)) {
430 switch (((LPNMHDR)lParam)->code) {
431 case TVN_ITEMEXPANDINGW:
432 return !OnTreeExpanding(g_pChildWnd->hTreeWnd, (NMTREEVIEWW*)lParam);
433 case TVN_SELCHANGEDW:
434 OnTreeSelectionChanged(g_pChildWnd->hTreeWnd, g_pChildWnd->hListWnd,
435 ((NMTREEVIEWW *)lParam)->itemNew.hItem, TRUE);
436 break;
437 case NM_SETFOCUS:
438 g_pChildWnd->nFocusPanel = 0;
439 break;
440 case TVN_BEGINLABELEDITW: {
441 HKEY hRootKey;
442 LPWSTR path;
444 if (!GetWindowLongPtrW(g_pChildWnd->hTreeWnd, GWLP_USERDATA))
445 return 1;
447 path = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hRootKey);
448 if (!path || !*path) return 1;
449 return 0;
451 case TVN_ENDLABELEDITW: {
452 HKEY hRootKey;
453 LPNMTVDISPINFOW dispInfo = (LPNMTVDISPINFOW)lParam;
454 LPWSTR path = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hRootKey);
455 BOOL res = RenameKey(hWnd, hRootKey, path, dispInfo->item.pszText);
456 if (res) {
457 TVITEMEXW item;
458 LPWSTR fullPath = GetPathFullPath(g_pChildWnd->hTreeWnd,
459 dispInfo->item.pszText);
460 item.mask = TVIF_HANDLE | TVIF_TEXT;
461 item.hItem = (HTREEITEM)SendMessageW(g_pChildWnd->hTreeWnd, TVM_GETNEXTITEM, TVGN_CARET, 0);
462 item.pszText = dispInfo->item.pszText;
463 SendMessageW( g_pChildWnd->hTreeWnd, TVM_SETITEMW, 0, (LPARAM)&item );
464 SendMessageW(hStatusBar, SB_SETTEXTW, 0, (LPARAM)fullPath);
465 HeapFree(GetProcessHeap(), 0, fullPath);
467 HeapFree(GetProcessHeap(), 0, path);
468 SetWindowLongPtrW(g_pChildWnd->hTreeWnd, GWLP_USERDATA, 0);
469 return res;
471 default:
472 return 0; /* goto def; */
474 } else
475 if (((int)wParam == LIST_WINDOW) && (g_pChildWnd != NULL)) {
476 if (((LPNMHDR)lParam)->code == NM_SETFOCUS) {
477 g_pChildWnd->nFocusPanel = 1;
478 } else if (!SendMessageW(g_pChildWnd->hListWnd, WM_NOTIFY_REFLECT, wParam, lParam)) {
479 goto def;
482 break;
484 case WM_SIZE:
485 if (wParam != SIZE_MINIMIZED && g_pChildWnd != NULL) {
486 ResizeWnd(LOWORD(lParam), HIWORD(lParam));
488 /* fall through */
489 default: def:
490 return DefWindowProcW(hWnd, message, wParam, lParam);
492 return 0;