regedit: Update status bar after editing key name.
[wine/wine-kai.git] / programs / regedit / childwnd.c
blobc284d8858cd952b20e4978b8bc27a339c92e1b16
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 <tchar.h>
25 #include <stdio.h>
27 #include "main.h"
29 #include "wine/debug.h"
30 #include "wine/unicode.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(regedit);
34 ChildWnd* g_pChildWnd;
36 /*******************************************************************************
37 * Local module support methods
40 LPCTSTR GetRootKeyName(HKEY hRootKey)
42 if (hRootKey == HKEY_CLASSES_ROOT) return _T("HKEY_CLASSES_ROOT");
43 if (hRootKey == HKEY_CURRENT_USER) return _T("HKEY_CURRENT_USER");
44 if (hRootKey == HKEY_LOCAL_MACHINE) return _T("HKEY_LOCAL_MACHINE");
45 if (hRootKey == HKEY_USERS) return _T("HKEY_USERS");
46 if (hRootKey == HKEY_CURRENT_CONFIG) return _T("HKEY_CURRENT_CONFIG");
47 if (hRootKey == HKEY_DYN_DATA) return _T("HKEY_DYN_DATA");
48 return _T("UNKNOWN HKEY, PLEASE REPORT");
51 static void draw_splitbar(HWND hWnd, int x)
53 RECT rt;
54 HDC hdc = GetDC(hWnd);
56 GetClientRect(hWnd, &rt);
57 rt.left = x - SPLIT_WIDTH/2;
58 rt.right = x + SPLIT_WIDTH/2+1;
59 InvertRect(hdc, &rt);
60 ReleaseDC(hWnd, hdc);
63 static void ResizeWnd(ChildWnd* pChildWnd, int cx, int cy)
65 HDWP hdwp = BeginDeferWindowPos(2);
66 RECT rt = {0, 0, cx, cy};
68 cx = pChildWnd->nSplitPos + SPLIT_WIDTH/2;
69 DeferWindowPos(hdwp, pChildWnd->hTreeWnd, 0, rt.left, rt.top, pChildWnd->nSplitPos-SPLIT_WIDTH/2-rt.left, rt.bottom-rt.top, SWP_NOZORDER|SWP_NOACTIVATE);
70 DeferWindowPos(hdwp, pChildWnd->hListWnd, 0, rt.left+cx , rt.top, rt.right-cx, rt.bottom-rt.top, SWP_NOZORDER|SWP_NOACTIVATE);
71 EndDeferWindowPos(hdwp);
74 static void OnPaint(HWND hWnd)
76 PAINTSTRUCT ps;
77 RECT rt;
78 HDC hdc;
80 GetClientRect(hWnd, &rt);
81 hdc = BeginPaint(hWnd, &ps);
82 FillRect(ps.hdc, &rt, GetSysColorBrush(COLOR_BTNFACE));
83 EndPaint(hWnd, &ps);
86 static LPTSTR CombinePaths(LPCTSTR pPaths[], int nPaths) {
87 int i, len, pos;
88 LPTSTR combined;
89 for (i=0, len=0; i<nPaths; i++) {
90 if (pPaths[i] && *pPaths[i]) {
91 len += lstrlen(pPaths[i])+1;
94 combined = HeapAlloc(GetProcessHeap(), 0, len * sizeof(TCHAR));
95 *combined = '\0';
96 for (i=0, pos=0; i<nPaths; i++) {
97 if (pPaths[i] && *pPaths[i]) {
98 int llen = _tcslen(pPaths[i]);
99 if (!*combined)
100 _tcscpy(combined, pPaths[i]);
101 else {
102 combined[pos++] = (TCHAR)'\\';
103 _tcscpy(combined+pos, pPaths[i]);
105 pos += llen;
108 return combined;
111 static LPTSTR GetPathRoot(HWND hwndTV, HTREEITEM hItem, BOOL bFull) {
112 LPCTSTR parts[2] = {_T(""), _T("")};
113 TCHAR text[260];
114 HKEY hRootKey = NULL;
115 if (!hItem)
116 hItem = TreeView_GetSelection(hwndTV);
117 GetItemPath(hwndTV, hItem, &hRootKey);
118 if (!bFull && !hRootKey)
119 return NULL;
120 if (hRootKey)
121 parts[1] = GetRootKeyName(hRootKey);
122 if (bFull) {
123 DWORD dwSize = sizeof(text)/sizeof(TCHAR);
124 GetComputerName(text, &dwSize);
125 parts[0] = text;
127 return CombinePaths(parts, 2);
130 LPTSTR GetItemFullPath(HWND hwndTV, HTREEITEM hItem, BOOL bFull) {
131 LPTSTR parts[2] = {_T(""), _T("")};
132 HKEY hRootKey = NULL;
133 LPTSTR ret;
134 parts[0] = GetPathRoot(hwndTV, hItem, bFull);
135 parts[1] = GetItemPath(hwndTV, hItem, &hRootKey);
136 ret = CombinePaths((LPCTSTR *)parts, 2);
137 HeapFree(GetProcessHeap(), 0, parts[0]);
138 return ret;
141 LPTSTR GetPathFullPath(HWND hwndTV, LPTSTR path) {
142 LPTSTR parts[2] = {_T(""), _T("")}, ret;
143 parts[0] = GetPathRoot(hwndTV, 0, TRUE);
144 parts[1] = path;
145 ret = CombinePaths((LPCTSTR *)parts, 2);
146 HeapFree(GetProcessHeap(), 0, parts[0]);
147 return ret;
150 static void OnTreeSelectionChanged(HWND hwndTV, HWND hwndLV, HTREEITEM hItem, BOOL bRefreshLV)
152 if (bRefreshLV) {
153 LPCTSTR keyPath;
154 HKEY hRootKey = NULL;
155 keyPath = GetItemPath(hwndTV, hItem, &hRootKey);
156 RefreshListView(hwndLV, hRootKey, keyPath, NULL);
158 UpdateStatusBar();
161 /*******************************************************************************
163 * FUNCTION: _CmdWndProc(HWND, unsigned, WORD, LONG)
165 * PURPOSE: Processes WM_COMMAND messages for the main frame window.
169 static BOOL _CmdWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
171 ChildWnd* pChildWnd = g_pChildWnd;
172 switch (LOWORD(wParam)) {
173 /* Parse the menu selections: */
174 case ID_REGISTRY_EXIT:
175 DestroyWindow(hWnd);
176 break;
177 case ID_VIEW_REFRESH:
178 WINE_TRACE("Is this ever called or is it just dead code?\n");
179 /* TODO */
180 break;
181 case ID_SWITCH_PANELS:
182 pChildWnd->nFocusPanel = !pChildWnd->nFocusPanel;
183 SetFocus(pChildWnd->nFocusPanel? pChildWnd->hListWnd: pChildWnd->hTreeWnd);
184 break;
185 default:
186 return FALSE;
188 return TRUE;
191 /*******************************************************************************
193 * FUNCTION: ChildWndProc(HWND, unsigned, WORD, LONG)
195 * PURPOSE: Processes messages for the child windows.
197 * WM_COMMAND - process the application menu
198 * WM_PAINT - Paint the main window
199 * WM_DESTROY - post a quit message and return
202 LRESULT CALLBACK ChildWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
204 static int last_split;
205 ChildWnd* pChildWnd = g_pChildWnd;
207 switch (message) {
208 case WM_CREATE:
209 g_pChildWnd = pChildWnd = HeapAlloc(GetProcessHeap(), 0, sizeof(ChildWnd));
210 if (!pChildWnd) return 0;
211 _tcsncpy(pChildWnd->szPath, _T("My Computer"), MAX_PATH);
212 pChildWnd->nSplitPos = 250;
213 pChildWnd->hWnd = hWnd;
214 pChildWnd->hTreeWnd = CreateTreeView(hWnd, pChildWnd->szPath, TREE_WINDOW);
215 pChildWnd->hListWnd = CreateListView(hWnd, LIST_WINDOW/*, pChildWnd->szPath*/);
216 pChildWnd->nFocusPanel = 1;
217 SetFocus(pChildWnd->hTreeWnd);
218 break;
219 case WM_COMMAND:
220 if (!_CmdWndProc(hWnd, message, wParam, lParam)) {
221 goto def;
223 break;
224 case WM_PAINT:
225 OnPaint(hWnd);
226 return 0;
227 case WM_SETCURSOR:
228 if (LOWORD(lParam) == HTCLIENT) {
229 POINT pt;
230 GetCursorPos(&pt);
231 ScreenToClient(hWnd, &pt);
232 if (pt.x>=pChildWnd->nSplitPos-SPLIT_WIDTH/2 && pt.x<pChildWnd->nSplitPos+SPLIT_WIDTH/2+1) {
233 SetCursor(LoadCursor(0, IDC_SIZEWE));
234 return TRUE;
237 goto def;
238 case WM_DESTROY:
239 HeapFree(GetProcessHeap(), 0, pChildWnd);
240 pChildWnd = NULL;
241 PostQuitMessage(0);
242 break;
243 case WM_LBUTTONDOWN: {
244 RECT rt;
245 int x = LOWORD(lParam);
246 GetClientRect(hWnd, &rt);
247 if (x>=pChildWnd->nSplitPos-SPLIT_WIDTH/2 && x<pChildWnd->nSplitPos+SPLIT_WIDTH/2+1) {
248 last_split = pChildWnd->nSplitPos;
249 draw_splitbar(hWnd, last_split);
250 SetCapture(hWnd);
252 break;
255 case WM_LBUTTONUP:
256 if (GetCapture() == hWnd) {
257 RECT rt;
258 int x = LOWORD(lParam);
259 draw_splitbar(hWnd, last_split);
260 last_split = -1;
261 GetClientRect(hWnd, &rt);
262 pChildWnd->nSplitPos = x;
263 ResizeWnd(pChildWnd, rt.right, rt.bottom);
264 ReleaseCapture();
266 break;
268 case WM_CAPTURECHANGED:
269 if (GetCapture()==hWnd && last_split>=0)
270 draw_splitbar(hWnd, last_split);
271 break;
273 case WM_KEYDOWN:
274 if (wParam == VK_ESCAPE)
275 if (GetCapture() == hWnd) {
276 RECT rt;
277 draw_splitbar(hWnd, last_split);
278 GetClientRect(hWnd, &rt);
279 ResizeWnd(pChildWnd, rt.right, rt.bottom);
280 last_split = -1;
281 ReleaseCapture();
282 SetCursor(LoadCursor(0, IDC_ARROW));
284 break;
286 case WM_MOUSEMOVE:
287 if (GetCapture() == hWnd) {
288 RECT rt;
289 int x = LOWORD(lParam);
290 HDC hdc = GetDC(hWnd);
291 GetClientRect(hWnd, &rt);
292 rt.left = last_split-SPLIT_WIDTH/2;
293 rt.right = last_split+SPLIT_WIDTH/2+1;
294 InvertRect(hdc, &rt);
295 last_split = x;
296 rt.left = x-SPLIT_WIDTH/2;
297 rt.right = x+SPLIT_WIDTH/2+1;
298 InvertRect(hdc, &rt);
299 ReleaseDC(hWnd, hdc);
301 break;
303 case WM_SETFOCUS:
304 if (pChildWnd != NULL) {
305 SetFocus(pChildWnd->nFocusPanel? pChildWnd->hListWnd: pChildWnd->hTreeWnd);
307 break;
309 case WM_TIMER:
310 break;
312 case WM_NOTIFY:
313 if ((int)wParam == TREE_WINDOW) {
314 switch (((LPNMHDR)lParam)->code) {
315 case TVN_ITEMEXPANDING:
316 return !OnTreeExpanding(pChildWnd->hTreeWnd, (NMTREEVIEW*)lParam);
317 case TVN_SELCHANGED:
318 OnTreeSelectionChanged(pChildWnd->hTreeWnd, pChildWnd->hListWnd,
319 ((NMTREEVIEW *)lParam)->itemNew.hItem, TRUE);
320 break;
321 case NM_SETFOCUS:
322 pChildWnd->nFocusPanel = 0;
323 break;
324 case NM_RCLICK: {
325 POINT pt;
326 GetCursorPos(&pt);
327 TrackPopupMenu(GetSubMenu(hPopupMenus, PM_NEW),
328 TPM_RIGHTBUTTON, pt.x, pt.y, 0, hFrameWnd, NULL);
329 break;
331 case TVN_ENDLABELEDIT: {
332 HKEY hRootKey;
333 LPNMTVDISPINFO dispInfo = (LPNMTVDISPINFO)lParam;
334 LPCTSTR path = GetItemPath(pChildWnd->hTreeWnd, 0, &hRootKey);
335 BOOL res = RenameKey(hWnd, hRootKey, path, dispInfo->item.pszText);
336 if (res) {
337 TVITEMEX item;
338 LPTSTR fullPath = GetPathFullPath(pChildWnd->hTreeWnd,
339 dispInfo->item.pszText);
340 item.mask = TVIF_HANDLE | TVIF_TEXT;
341 item.hItem = TreeView_GetSelection(pChildWnd->hTreeWnd);
342 item.pszText = dispInfo->item.pszText;
343 SendMessage( pChildWnd->hTreeWnd, TVM_SETITEMW, 0, (LPARAM)&item );
344 SendMessage(hStatusBar, SB_SETTEXT, 0, (LPARAM)fullPath);
345 HeapFree(GetProcessHeap(), 0, fullPath);
347 return res;
349 default:
350 return 0; /* goto def; */
352 } else
353 if ((int)wParam == LIST_WINDOW) {
354 if (((LPNMHDR)lParam)->code == NM_SETFOCUS) {
355 pChildWnd->nFocusPanel = 1;
356 } else if (!SendMessage(pChildWnd->hListWnd, WM_NOTIFY_REFLECT, wParam, lParam)) {
357 goto def;
360 break;
362 case WM_SIZE:
363 if (wParam != SIZE_MINIMIZED && pChildWnd != NULL) {
364 ResizeWnd(pChildWnd, LOWORD(lParam), HIWORD(lParam));
366 /* fall through */
367 default: def:
368 return DefWindowProc(hWnd, message, wParam, lParam);
370 return 0;