push d2761731c253bfe9a5961252b22d8cea093833f5
[wine/hacks.git] / programs / regedit / childwnd.c
blob42fefca542231bdc7439299dc38618337385c663
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;
35 static int last_split;
37 /*******************************************************************************
38 * Local module support methods
41 LPCTSTR GetRootKeyName(HKEY hRootKey)
43 if (hRootKey == HKEY_CLASSES_ROOT) return _T("HKEY_CLASSES_ROOT");
44 if (hRootKey == HKEY_CURRENT_USER) return _T("HKEY_CURRENT_USER");
45 if (hRootKey == HKEY_LOCAL_MACHINE) return _T("HKEY_LOCAL_MACHINE");
46 if (hRootKey == HKEY_USERS) return _T("HKEY_USERS");
47 if (hRootKey == HKEY_CURRENT_CONFIG) return _T("HKEY_CURRENT_CONFIG");
48 if (hRootKey == HKEY_DYN_DATA) return _T("HKEY_DYN_DATA");
49 return _T("UNKNOWN HKEY, PLEASE REPORT");
52 LPCWSTR GetRootKeyNameW(HKEY hRootKey)
54 if(hRootKey == HKEY_CLASSES_ROOT)
55 return reg_class_namesW[INDEX_HKEY_CLASSES_ROOT];
56 if(hRootKey == HKEY_CURRENT_USER)
57 return reg_class_namesW[INDEX_HKEY_CURRENT_USER];
58 if(hRootKey == HKEY_LOCAL_MACHINE)
59 return reg_class_namesW[INDEX_HKEY_LOCAL_MACHINE];
60 if(hRootKey == HKEY_USERS)
61 return reg_class_namesW[INDEX_HKEY_USERS];
62 if(hRootKey == HKEY_CURRENT_CONFIG)
63 return reg_class_namesW[INDEX_HKEY_CURRENT_CONFIG];
64 if(hRootKey == HKEY_DYN_DATA)
65 return reg_class_namesW[INDEX_HKEY_DYN_DATA];
66 else
68 static const WCHAR unknown_key[] = {'U','N','K','N','O','W','N',' ','H','K','E','Y',',',' ',
69 'P','L','E','A','S','E',' ','R','E','P','O','R','T',0};
70 return unknown_key;
74 static void draw_splitbar(HWND hWnd, int x)
76 RECT rt;
77 HDC hdc = GetDC(hWnd);
79 GetClientRect(hWnd, &rt);
80 rt.left = x - SPLIT_WIDTH/2;
81 rt.right = x + SPLIT_WIDTH/2+1;
82 InvertRect(hdc, &rt);
83 ReleaseDC(hWnd, hdc);
86 static void ResizeWnd(int cx, int cy)
88 HDWP hdwp = BeginDeferWindowPos(2);
89 RECT rt = {0, 0, cx, cy};
91 cx = g_pChildWnd->nSplitPos + SPLIT_WIDTH/2;
92 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);
93 DeferWindowPos(hdwp, g_pChildWnd->hListWnd, 0, rt.left+cx , rt.top, rt.right-cx, rt.bottom-rt.top, SWP_NOZORDER|SWP_NOACTIVATE);
94 EndDeferWindowPos(hdwp);
97 static void OnPaint(HWND hWnd)
99 PAINTSTRUCT ps;
100 RECT rt;
101 HDC hdc;
103 GetClientRect(hWnd, &rt);
104 hdc = BeginPaint(hWnd, &ps);
105 FillRect(ps.hdc, &rt, GetSysColorBrush(COLOR_BTNFACE));
106 EndPaint(hWnd, &ps);
109 static LPTSTR CombinePaths(LPCTSTR pPaths[], int nPaths) {
110 int i, len, pos;
111 LPTSTR combined;
112 for (i=0, len=0; i<nPaths; i++) {
113 if (pPaths[i] && *pPaths[i]) {
114 len += lstrlen(pPaths[i])+1;
117 combined = HeapAlloc(GetProcessHeap(), 0, len * sizeof(TCHAR));
118 *combined = '\0';
119 for (i=0, pos=0; i<nPaths; i++) {
120 if (pPaths[i] && *pPaths[i]) {
121 int llen = _tcslen(pPaths[i]);
122 if (!*combined)
123 _tcscpy(combined, pPaths[i]);
124 else {
125 combined[pos++] = (TCHAR)'\\';
126 _tcscpy(combined+pos, pPaths[i]);
128 pos += llen;
131 return combined;
134 static LPWSTR CombinePathsW(LPCWSTR pPaths[], int nPaths) {
135 int i, len, pos;
136 LPWSTR combined;
137 for (i=0, len=0; i<nPaths; i++) {
138 if (pPaths[i] && *pPaths[i]) {
139 len += lstrlenW(pPaths[i])+1;
142 combined = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
143 *combined = '\0';
144 for (i=0, pos=0; i<nPaths; i++) {
145 if (pPaths[i] && *pPaths[i]) {
146 int llen = lstrlenW(pPaths[i]);
147 if (!*combined)
148 lstrcpyW(combined, pPaths[i]);
149 else {
150 combined[pos++] = (TCHAR)'\\';
151 lstrcpyW(combined+pos, pPaths[i]);
153 pos += llen;
156 return combined;
159 static LPTSTR GetPathRoot(HWND hwndTV, HTREEITEM hItem, BOOL bFull) {
160 LPCTSTR parts[2] = {_T(""), _T("")};
161 TCHAR text[260];
162 HKEY hRootKey = NULL;
163 if (!hItem)
164 hItem = TreeView_GetSelection(hwndTV);
165 GetItemPath(hwndTV, hItem, &hRootKey);
166 if (!bFull && !hRootKey)
167 return NULL;
168 if (hRootKey)
169 parts[1] = GetRootKeyName(hRootKey);
170 if (bFull) {
171 DWORD dwSize = sizeof(text)/sizeof(TCHAR);
172 GetComputerName(text, &dwSize);
173 parts[0] = text;
175 return CombinePaths(parts, 2);
178 static LPWSTR GetPathRootW(HWND hwndTV, HTREEITEM hItem, BOOL bFull) {
179 LPCWSTR parts[2] = {0,0};
180 WCHAR text[260];
181 HKEY hRootKey = NULL;
182 if (!hItem)
183 hItem = TreeView_GetSelection(hwndTV);
184 GetItemPathW(hwndTV, hItem, &hRootKey);
185 if (!bFull && !hRootKey)
186 return NULL;
187 if (hRootKey)
188 parts[1] = GetRootKeyNameW(hRootKey);
189 if (bFull) {
190 DWORD dwSize = sizeof(text)/sizeof(TCHAR);
191 GetComputerNameW(text, &dwSize);
192 parts[0] = text;
194 return CombinePathsW(parts, 2);
197 LPTSTR GetItemFullPath(HWND hwndTV, HTREEITEM hItem, BOOL bFull) {
198 LPTSTR parts[2];
199 LPTSTR ret;
200 HKEY hRootKey = NULL;
202 parts[0] = GetPathRoot(hwndTV, hItem, bFull);
203 parts[1] = GetItemPath(hwndTV, hItem, &hRootKey);
204 ret = CombinePaths((LPCTSTR *)parts, 2);
205 HeapFree(GetProcessHeap(), 0, parts[0]);
206 return ret;
209 LPWSTR GetItemFullPathW(HWND hwndTV, HTREEITEM hItem, BOOL bFull) {
210 LPWSTR parts[2];
211 LPWSTR ret;
212 HKEY hRootKey = NULL;
214 parts[0] = GetPathRootW(hwndTV, hItem, bFull);
215 parts[1] = GetItemPathW(hwndTV, hItem, &hRootKey);
216 ret = CombinePathsW((LPCWSTR *)parts, 2);
217 HeapFree(GetProcessHeap(), 0, parts[0]);
218 return ret;
221 static LPTSTR GetPathFullPath(HWND hwndTV, LPTSTR path) {
222 LPTSTR parts[2];
223 LPTSTR ret;
225 parts[0] = GetPathRoot(hwndTV, 0, TRUE);
226 parts[1] = path;
227 ret = CombinePaths((LPCTSTR *)parts, 2);
228 HeapFree(GetProcessHeap(), 0, parts[0]);
229 return ret;
232 static void OnTreeSelectionChanged(HWND hwndTV, HWND hwndLV, HTREEITEM hItem, BOOL bRefreshLV)
234 if (bRefreshLV) {
235 LPCTSTR keyPath;
236 HKEY hRootKey = NULL;
237 keyPath = GetItemPath(hwndTV, hItem, &hRootKey);
238 RefreshListView(hwndLV, hRootKey, keyPath, NULL);
240 UpdateStatusBar();
243 /*******************************************************************************
244 * finish_splitbar [internal]
246 * make the splitbar invisible and resize the windows
247 * (helper for ChildWndProc)
249 static void finish_splitbar(HWND hWnd, int x)
251 RECT rt;
253 draw_splitbar(hWnd, last_split);
254 last_split = -1;
255 GetClientRect(hWnd, &rt);
256 g_pChildWnd->nSplitPos = x;
257 ResizeWnd(rt.right, rt.bottom);
258 ReleaseCapture();
261 /*******************************************************************************
263 * FUNCTION: _CmdWndProc(HWND, unsigned, WORD, LONG)
265 * PURPOSE: Processes WM_COMMAND messages for the main frame window.
269 static BOOL _CmdWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
271 switch (LOWORD(wParam)) {
272 /* Parse the menu selections: */
273 case ID_REGISTRY_EXIT:
274 DestroyWindow(hWnd);
275 break;
276 case ID_VIEW_REFRESH:
277 WINE_TRACE("Is this ever called or is it just dead code?\n");
278 /* TODO */
279 break;
280 case ID_SWITCH_PANELS:
281 g_pChildWnd->nFocusPanel = !g_pChildWnd->nFocusPanel;
282 SetFocus(g_pChildWnd->nFocusPanel? g_pChildWnd->hListWnd: g_pChildWnd->hTreeWnd);
283 break;
284 default:
285 return FALSE;
287 return TRUE;
290 /*******************************************************************************
292 * FUNCTION: ChildWndProc(HWND, unsigned, WORD, LONG)
294 * PURPOSE: Processes messages for the child windows.
296 * WM_COMMAND - process the application menu
297 * WM_PAINT - Paint the main window
298 * WM_DESTROY - post a quit message and return
301 LRESULT CALLBACK ChildWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
303 switch (message) {
304 case WM_CREATE:
305 g_pChildWnd = HeapAlloc(GetProcessHeap(), 0, sizeof(ChildWnd));
306 if (!g_pChildWnd) return 0;
307 LoadString(hInst, IDS_REGISTRY_ROOT_NAME, g_pChildWnd->szPath, MAX_PATH);
308 g_pChildWnd->nSplitPos = 250;
309 g_pChildWnd->hWnd = hWnd;
310 g_pChildWnd->hTreeWnd = CreateTreeView(hWnd, g_pChildWnd->szPath, TREE_WINDOW);
311 g_pChildWnd->hListWnd = CreateListView(hWnd, LIST_WINDOW/*, g_pChildWnd->szPath*/);
312 g_pChildWnd->nFocusPanel = 1;
313 SetFocus(g_pChildWnd->hTreeWnd);
314 break;
315 case WM_COMMAND:
316 if (!_CmdWndProc(hWnd, message, wParam, lParam)) {
317 goto def;
319 break;
320 case WM_PAINT:
321 OnPaint(hWnd);
322 return 0;
323 case WM_SETCURSOR:
324 if (LOWORD(lParam) == HTCLIENT) {
325 POINT pt;
326 GetCursorPos(&pt);
327 ScreenToClient(hWnd, &pt);
328 if (pt.x>=g_pChildWnd->nSplitPos-SPLIT_WIDTH/2 && pt.x<g_pChildWnd->nSplitPos+SPLIT_WIDTH/2+1) {
329 SetCursor(LoadCursor(0, IDC_SIZEWE));
330 return TRUE;
333 goto def;
334 case WM_DESTROY:
335 HeapFree(GetProcessHeap(), 0, g_pChildWnd);
336 g_pChildWnd = NULL;
337 PostQuitMessage(0);
338 break;
339 case WM_LBUTTONDOWN: {
340 RECT rt;
341 int x = (short)LOWORD(lParam);
342 GetClientRect(hWnd, &rt);
343 if (x>=g_pChildWnd->nSplitPos-SPLIT_WIDTH/2 && x<g_pChildWnd->nSplitPos+SPLIT_WIDTH/2+1) {
344 last_split = g_pChildWnd->nSplitPos;
345 draw_splitbar(hWnd, last_split);
346 SetCapture(hWnd);
348 break;
351 /* WM_RBUTTONDOWN sets the splitbar the same way as WM_LBUTTONUP */
352 case WM_LBUTTONUP:
353 case WM_RBUTTONDOWN:
354 if (GetCapture() == hWnd) {
355 finish_splitbar(hWnd, LOWORD(lParam));
357 break;
359 case WM_CAPTURECHANGED:
360 if (GetCapture()==hWnd && last_split>=0)
361 draw_splitbar(hWnd, last_split);
362 break;
364 case WM_KEYDOWN:
365 if (wParam == VK_ESCAPE)
366 if (GetCapture() == hWnd) {
367 RECT rt;
368 draw_splitbar(hWnd, last_split);
369 GetClientRect(hWnd, &rt);
370 ResizeWnd(rt.right, rt.bottom);
371 last_split = -1;
372 ReleaseCapture();
373 SetCursor(LoadCursor(0, IDC_ARROW));
375 break;
377 case WM_MOUSEMOVE:
378 if (GetCapture() == hWnd) {
379 RECT rt;
380 int x = LOWORD(lParam);
381 HDC hdc = GetDC(hWnd);
382 GetClientRect(hWnd, &rt);
383 rt.left = last_split-SPLIT_WIDTH/2;
384 rt.right = last_split+SPLIT_WIDTH/2+1;
385 InvertRect(hdc, &rt);
386 last_split = x;
387 rt.left = x-SPLIT_WIDTH/2;
388 rt.right = x+SPLIT_WIDTH/2+1;
389 InvertRect(hdc, &rt);
390 ReleaseDC(hWnd, hdc);
392 break;
394 case WM_SETFOCUS:
395 if (g_pChildWnd != NULL) {
396 SetFocus(g_pChildWnd->nFocusPanel? g_pChildWnd->hListWnd: g_pChildWnd->hTreeWnd);
398 break;
400 case WM_TIMER:
401 break;
403 case WM_NOTIFY:
404 if (((int)wParam == TREE_WINDOW) && (g_pChildWnd != NULL)) {
405 switch (((LPNMHDR)lParam)->code) {
406 case TVN_ITEMEXPANDING:
407 return !OnTreeExpanding(g_pChildWnd->hTreeWnd, (NMTREEVIEW*)lParam);
408 case TVN_SELCHANGED:
409 OnTreeSelectionChanged(g_pChildWnd->hTreeWnd, g_pChildWnd->hListWnd,
410 ((NMTREEVIEW *)lParam)->itemNew.hItem, TRUE);
411 break;
412 case NM_SETFOCUS:
413 g_pChildWnd->nFocusPanel = 0;
414 break;
415 case NM_RCLICK: {
416 POINT pt;
417 GetCursorPos(&pt);
418 TrackPopupMenu(GetSubMenu(hPopupMenus, PM_NEW),
419 TPM_RIGHTBUTTON, pt.x, pt.y, 0, hFrameWnd, NULL);
420 break;
422 case TVN_ENDLABELEDIT: {
423 HKEY hRootKey;
424 LPNMTVDISPINFO dispInfo = (LPNMTVDISPINFO)lParam;
425 LPCTSTR path = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hRootKey);
426 BOOL res = RenameKey(hWnd, hRootKey, path, dispInfo->item.pszText);
427 if (res) {
428 TVITEMEX item;
429 LPTSTR fullPath = GetPathFullPath(g_pChildWnd->hTreeWnd,
430 dispInfo->item.pszText);
431 item.mask = TVIF_HANDLE | TVIF_TEXT;
432 item.hItem = TreeView_GetSelection(g_pChildWnd->hTreeWnd);
433 item.pszText = dispInfo->item.pszText;
434 SendMessage( g_pChildWnd->hTreeWnd, TVM_SETITEMW, 0, (LPARAM)&item );
435 SendMessage(hStatusBar, SB_SETTEXT, 0, (LPARAM)fullPath);
436 HeapFree(GetProcessHeap(), 0, fullPath);
438 return res;
440 default:
441 return 0; /* goto def; */
443 } else
444 if (((int)wParam == LIST_WINDOW) && (g_pChildWnd != NULL)) {
445 if (((LPNMHDR)lParam)->code == NM_SETFOCUS) {
446 g_pChildWnd->nFocusPanel = 1;
447 } else if (!SendMessage(g_pChildWnd->hListWnd, WM_NOTIFY_REFLECT, wParam, lParam)) {
448 goto def;
451 break;
453 case WM_SIZE:
454 if (wParam != SIZE_MINIMIZED && g_pChildWnd != NULL) {
455 ResizeWnd(LOWORD(lParam), HIWORD(lParam));
457 /* fall through */
458 default: def:
459 return DefWindowProc(hWnd, message, wParam, lParam);
461 return 0;