dmime: Remove shadowing local hr variable.
[wine.git] / programs / regedit / childwnd.c
blob3f1797d31a01855e1a2e93a5dbecdae852cd9d2c
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>
25 #include "main.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(regedit);
31 ChildWnd* g_pChildWnd;
32 static int last_split;
34 static const WCHAR wszLastKey[] = L"LastKey";
35 static const WCHAR wszKeyName[] = L"Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\Regedit";
37 /*******************************************************************************
38 * Local module support methods
41 static LPCWSTR GetRootKeyName(HKEY hRootKey)
43 if(hRootKey == HKEY_CLASSES_ROOT)
44 return reg_class_namesW[INDEX_HKEY_CLASSES_ROOT];
45 if(hRootKey == HKEY_CURRENT_USER)
46 return reg_class_namesW[INDEX_HKEY_CURRENT_USER];
47 if(hRootKey == HKEY_LOCAL_MACHINE)
48 return reg_class_namesW[INDEX_HKEY_LOCAL_MACHINE];
49 if(hRootKey == HKEY_USERS)
50 return reg_class_namesW[INDEX_HKEY_USERS];
51 if(hRootKey == HKEY_CURRENT_CONFIG)
52 return reg_class_namesW[INDEX_HKEY_CURRENT_CONFIG];
53 if(hRootKey == HKEY_DYN_DATA)
54 return reg_class_namesW[INDEX_HKEY_DYN_DATA];
55 else
56 return L"Unknown HKEY. Please report.";
59 static void draw_splitbar(HWND hWnd, int x)
61 RECT rt;
62 HDC hdc = GetDC(hWnd);
64 GetClientRect(hWnd, &rt);
65 rt.left = x - SPLIT_WIDTH/2;
66 rt.right = x + SPLIT_WIDTH/2+1;
67 InvertRect(hdc, &rt);
68 ReleaseDC(hWnd, hdc);
71 static void ResizeWnd(int cx, int cy)
73 HDWP hdwp = BeginDeferWindowPos(2);
74 RECT rt = {0, 0, cx, cy};
76 cx = g_pChildWnd->nSplitPos + SPLIT_WIDTH/2;
77 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);
78 DeferWindowPos(hdwp, g_pChildWnd->hListWnd, 0, rt.left+cx , rt.top, rt.right-cx, rt.bottom-rt.top, SWP_NOZORDER|SWP_NOACTIVATE);
79 EndDeferWindowPos(hdwp);
82 static LPWSTR CombinePaths(LPCWSTR pPaths[], int nPaths) {
83 int i, len, pos;
84 LPWSTR combined;
85 for (i=0, len=0; i<nPaths; i++) {
86 if (pPaths[i] && *pPaths[i]) {
87 len += lstrlenW(pPaths[i])+1;
90 combined = malloc(len * sizeof(WCHAR));
91 *combined = '\0';
92 for (i=0, pos=0; i<nPaths; i++) {
93 if (pPaths[i] && *pPaths[i]) {
94 int llen = lstrlenW(pPaths[i]);
95 if (!*combined)
96 lstrcpyW(combined, pPaths[i]);
97 else {
98 combined[pos++] = '\\';
99 lstrcpyW(combined+pos, pPaths[i]);
101 pos += llen;
104 return combined;
107 static LPWSTR GetPathRoot(HWND hwndTV, HTREEITEM hItem, BOOL bFull) {
108 LPCWSTR parts[2] = {0,0};
109 WCHAR text[260];
110 HKEY hRootKey = NULL;
111 if (!hItem)
112 hItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CARET, 0);
113 free(GetItemPath(hwndTV, hItem, &hRootKey));
114 if (!bFull && !hRootKey)
115 return NULL;
116 if (hRootKey)
117 parts[1] = GetRootKeyName(hRootKey);
118 if (bFull) {
119 DWORD dwSize = ARRAY_SIZE(text);
120 GetComputerNameW(text, &dwSize);
121 parts[0] = text;
123 return CombinePaths(parts, 2);
126 LPWSTR GetItemFullPath(HWND hwndTV, HTREEITEM hItem, BOOL bFull) {
127 LPWSTR parts[2];
128 LPWSTR ret;
129 HKEY hRootKey = NULL;
131 parts[0] = GetPathRoot(hwndTV, hItem, bFull);
132 parts[1] = GetItemPath(hwndTV, hItem, &hRootKey);
133 ret = CombinePaths((LPCWSTR *)parts, 2);
134 free(parts[0]);
135 free(parts[1]);
136 return ret;
139 static void OnTreeSelectionChanged(HWND hwndTV, HWND hwndLV, HTREEITEM hItem, BOOL bRefreshLV)
141 if (bRefreshLV) {
142 LPWSTR keyPath;
143 HKEY hRootKey = NULL;
144 HTREEITEM rootitem;
146 rootitem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_ROOT, 0);
147 if (rootitem == hItem)
149 SendMessageW(hwndLV, LVM_DELETEALLITEMS, 0, 0);
150 UpdateStatusBar();
151 return;
154 keyPath = GetItemPath(hwndTV, hItem, &hRootKey);
155 RefreshListView(hwndLV, hRootKey, keyPath, NULL);
156 free(keyPath);
158 UpdateStatusBar();
161 /*******************************************************************************
162 * finish_splitbar [internal]
164 * make the splitbar invisible and resize the windows
165 * (helper for ChildWndProc)
167 static void finish_splitbar(HWND hWnd, int x)
169 RECT rt;
171 draw_splitbar(hWnd, last_split);
172 last_split = -1;
173 GetClientRect(hWnd, &rt);
174 g_pChildWnd->nSplitPos = x;
175 ResizeWnd(rt.right, rt.bottom);
176 ReleaseCapture();
179 /*******************************************************************************
181 * FUNCTION: _CmdWndProc(HWND, unsigned, WORD, LONG)
183 * PURPOSE: Processes WM_COMMAND messages for the main frame window.
187 static BOOL _CmdWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
189 switch (LOWORD(wParam)) {
190 /* Parse the menu selections: */
191 case ID_REGISTRY_EXIT:
192 DestroyWindow(hWnd);
193 break;
194 case ID_VIEW_REFRESH:
195 WINE_TRACE("Is this ever called or is it just dead code?\n");
196 /* TODO */
197 break;
198 case ID_SWITCH_PANELS:
199 g_pChildWnd->nFocusPanel = !g_pChildWnd->nFocusPanel;
200 SetFocus(g_pChildWnd->nFocusPanel? g_pChildWnd->hListWnd: g_pChildWnd->hTreeWnd);
201 break;
202 default:
203 return FALSE;
205 return TRUE;
208 /*******************************************************************************
209 * get_last_key [internal]
211 * open last key
214 static void get_last_key(HWND hwndTV)
216 HKEY hkey;
217 WCHAR wszVal[KEY_MAX_LEN];
218 DWORD dwSize = sizeof(wszVal);
220 if (RegCreateKeyExW(HKEY_CURRENT_USER, wszKeyName, 0, NULL, 0, KEY_READ, NULL, &hkey, NULL) == ERROR_SUCCESS)
222 HTREEITEM selection = NULL;
223 if (RegQueryValueExW(hkey, wszLastKey, NULL, NULL, (LPBYTE)wszVal, &dwSize) == ERROR_SUCCESS)
225 if (lstrcmpW(wszVal, g_pChildWnd->szPath))
226 selection = FindPathInTree(hwndTV, wszVal);
229 if(!selection)
231 selection = (HTREEITEM)SendMessageW(g_pChildWnd->hTreeWnd, TVM_GETNEXTITEM, TVGN_ROOT, 0);
232 SendMessageW(hwndTV, TVM_EXPAND, TVE_EXPAND, (LPARAM)selection );
234 else
235 SendMessageW(hwndTV, TVM_SELECTITEM, TVGN_CARET, (LPARAM)selection);
237 RegCloseKey(hkey);
241 /*******************************************************************************
242 * set_last_key [internal]
244 * save last key
247 static void set_last_key(HWND hwndTV)
249 HKEY hkey;
251 if (RegCreateKeyExW(HKEY_CURRENT_USER, wszKeyName, 0, NULL, 0, KEY_WRITE, NULL, &hkey, NULL) == ERROR_SUCCESS)
253 HTREEITEM selection = (HTREEITEM)SendMessageW(g_pChildWnd->hTreeWnd, TVM_GETNEXTITEM, TVGN_CARET, 0);
254 HTREEITEM root = (HTREEITEM)SendMessageW(g_pChildWnd->hTreeWnd, TVM_GETNEXTITEM, TVGN_ROOT, 0);
255 WCHAR *value;
257 if (selection == root)
258 value = g_pChildWnd->szPath;
259 else
260 value = GetItemFullPath(g_pChildWnd->hTreeWnd, selection, FALSE);
261 RegSetValueExW(hkey, wszLastKey, 0, REG_SZ, (LPBYTE)value, (lstrlenW(value) + 1) * sizeof(WCHAR));
262 if (selection != root)
263 free(value);
264 RegCloseKey(hkey);
268 static int treeview_notify(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
270 switch (((NMHDR *)lParam)->code)
272 case NM_SETFOCUS:
273 g_pChildWnd->nFocusPanel = 0;
274 break;
275 case TVN_BEGINLABELEDITW:
277 HKEY hRootKey;
278 WCHAR *path;
280 if (!GetWindowLongPtrW(g_pChildWnd->hTreeWnd, GWLP_USERDATA))
281 return 1;
283 path = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hRootKey);
284 if (!path || !*path)
285 return 1;
286 return 0;
288 case TVN_ENDLABELEDITW:
290 HKEY hRootKey;
291 NMTVDISPINFOW *dispInfo = (NMTVDISPINFOW *)lParam;
292 WCHAR *path = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hRootKey);
293 BOOL res = RenameKey(hWnd, hRootKey, path, dispInfo->item.pszText);
295 free(path);
297 if (res)
299 TVITEMW item;
301 item.mask = TVIF_HANDLE | TVIF_TEXT;
302 item.hItem = dispInfo->item.hItem;
303 item.pszText = dispInfo->item.pszText;
304 SendMessageW(g_pChildWnd->hTreeWnd, TVM_SETITEMW, 0, (LPARAM)&item);
306 path = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hRootKey);
307 update_listview_path(path);
308 free(path);
310 UpdateStatusBar();
313 SetWindowLongPtrW(g_pChildWnd->hTreeWnd, GWLP_USERDATA, 0);
314 return res;
316 case TVN_ITEMEXPANDINGW:
317 return !OnTreeExpanding(g_pChildWnd->hTreeWnd, (NMTREEVIEWW *)lParam);
318 case TVN_SELCHANGEDW:
319 OnTreeSelectionChanged(g_pChildWnd->hTreeWnd, g_pChildWnd->hListWnd,
320 ((NMTREEVIEWW *)lParam)->itemNew.hItem, TRUE);
321 break;
323 return 0;
326 static int listview_notify(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
328 switch (((NMHDR *)lParam)->code)
330 case NM_DBLCLK:
332 NMITEMACTIVATE *nmitem = (NMITEMACTIVATE *)lParam;
334 if (nmitem->iItem != -1)
336 LVITEMW item;
338 item.state = 0;
339 item.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
340 SendMessageW(g_pChildWnd->hListWnd, LVM_SETITEMSTATE, (UINT)-1, (LPARAM)&item);
342 item.state = LVIS_FOCUSED | LVIS_SELECTED;
343 item.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
344 SendMessageW(g_pChildWnd->hListWnd, LVM_SETITEMSTATE, nmitem->iItem, (LPARAM)&item);
346 SendMessageW(hFrameWnd, WM_COMMAND, ID_EDIT_MODIFY, 0);
348 break;
350 case NM_RETURN:
352 int cnt = SendMessageW(g_pChildWnd->hListWnd, LVM_GETNEXTITEM, -1,
353 MAKELPARAM(LVNI_FOCUSED | LVNI_SELECTED, 0));
354 if (cnt != -1)
355 SendMessageW(hFrameWnd, WM_COMMAND, ID_EDIT_MODIFY, 0);
356 break;
358 case NM_SETFOCUS:
359 g_pChildWnd->nFocusPanel = 1;
360 break;
361 case LVN_BEGINLABELEDITW:
362 if (!((NMLVDISPINFOW *)lParam)->item.iItem)
363 return 1;
364 return 0;
365 case LVN_COLUMNCLICK:
366 if (g_columnToSort == ((NMLISTVIEW *)lParam)->iSubItem)
367 g_invertSort = !g_invertSort;
368 else
370 g_columnToSort = ((NMLISTVIEW *)lParam)->iSubItem;
371 g_invertSort = FALSE;
374 SendMessageW(g_pChildWnd->hListWnd, LVM_SORTITEMS,
375 (WPARAM)g_pChildWnd->hListWnd, (LPARAM)CompareFunc);
376 break;
377 case LVN_DELETEITEM:
379 NMLISTVIEW *nmlv = (NMLISTVIEW *)lParam;
380 LINE_INFO *info = (LINE_INFO *)nmlv->lParam;
382 free(info->name);
383 free(info->val);
384 free(info);
385 break;
387 case LVN_ENDLABELEDITW:
389 NMLVDISPINFOW *dispInfo = (NMLVDISPINFOW *)lParam;
390 WCHAR *oldName = GetItemText(g_pChildWnd->hListWnd, dispInfo->item.iItem);
391 LONG ret;
393 if (!oldName) return -1; /* cannot rename a default value */
394 ret = RenameValue(g_pChildWnd->hListWnd, g_currentRootKey, g_currentPath,
395 oldName, dispInfo->item.pszText);
396 if (ret)
398 dispInfo->item.iSubItem = 0;
399 SendMessageW(g_pChildWnd->hListWnd, LVM_SETITEMTEXTW,
400 dispInfo->item.iItem, (LPARAM)&dispInfo->item);
403 free(oldName);
404 return 0;
406 case LVN_GETDISPINFOW:
407 OnGetDispInfo((NMLVDISPINFOW *)lParam);
408 break;
410 return 0;
413 #define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
414 #define GET_Y_LPARAM(lp) ((int)(short)HIWORD(lp))
416 /*******************************************************************************
418 * FUNCTION: ChildWndProc(HWND, unsigned, WORD, LONG)
420 * PURPOSE: Processes messages for the child windows.
422 * WM_COMMAND - process the application menu
423 * WM_PAINT - Paint the main window
424 * WM_DESTROY - post a quit message and return
427 LRESULT CALLBACK ChildWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
429 switch (message) {
430 case WM_CREATE:
431 g_pChildWnd = malloc(sizeof(ChildWnd));
432 if (!g_pChildWnd) return 0;
433 LoadStringW(hInst, IDS_REGISTRY_ROOT_NAME, g_pChildWnd->szPath, MAX_PATH);
434 g_pChildWnd->nSplitPos = 250;
435 g_pChildWnd->hWnd = hWnd;
436 g_pChildWnd->hTreeWnd = CreateTreeView(hWnd, g_pChildWnd->szPath, TREE_WINDOW);
437 g_pChildWnd->hListWnd = CreateListView(hWnd, LIST_WINDOW/*, g_pChildWnd->szPath*/);
438 g_pChildWnd->nFocusPanel = 1;
439 SetFocus(g_pChildWnd->hTreeWnd);
440 get_last_key(g_pChildWnd->hTreeWnd);
441 break;
442 case WM_COMMAND:
443 if (!_CmdWndProc(hWnd, message, wParam, lParam)) {
444 goto def;
446 break;
447 case WM_SETCURSOR:
448 if (LOWORD(lParam) == HTCLIENT) {
449 POINT pt;
450 GetCursorPos(&pt);
451 ScreenToClient(hWnd, &pt);
452 if (pt.x>=g_pChildWnd->nSplitPos-SPLIT_WIDTH/2 && pt.x<g_pChildWnd->nSplitPos+SPLIT_WIDTH/2+1) {
453 SetCursor(LoadCursorW(0, (LPCWSTR)IDC_SIZEWE));
454 return TRUE;
457 goto def;
458 case WM_DESTROY:
459 set_last_key(g_pChildWnd->hTreeWnd);
460 free(g_pChildWnd);
461 g_pChildWnd = NULL;
462 PostQuitMessage(0);
463 break;
464 case WM_LBUTTONDOWN: {
465 RECT rt;
466 int x = (short)LOWORD(lParam);
467 GetClientRect(hWnd, &rt);
468 if (x>=g_pChildWnd->nSplitPos-SPLIT_WIDTH/2 && x<g_pChildWnd->nSplitPos+SPLIT_WIDTH/2+1) {
469 last_split = g_pChildWnd->nSplitPos;
470 draw_splitbar(hWnd, last_split);
471 SetCapture(hWnd);
473 break;
476 /* WM_RBUTTONDOWN sets the splitbar the same way as WM_LBUTTONUP */
477 case WM_LBUTTONUP:
478 case WM_RBUTTONDOWN:
479 if (GetCapture() == hWnd) {
480 finish_splitbar(hWnd, LOWORD(lParam));
482 break;
484 case WM_CAPTURECHANGED:
485 if (GetCapture()==hWnd && last_split>=0)
486 draw_splitbar(hWnd, last_split);
487 break;
489 case WM_CONTEXTMENU:
491 POINT pt = {GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)};
492 short int menu_id = -1;
494 if ((HWND)wParam == g_pChildWnd->hTreeWnd)
496 TVHITTESTINFO ht;
498 ht.pt = pt;
499 ScreenToClient(g_pChildWnd->hTreeWnd, &ht.pt);
501 if (SendMessageW(g_pChildWnd->hTreeWnd, TVM_HITTEST, 0, (LPARAM)&ht))
503 HTREEITEM root;
505 SendMessageW(g_pChildWnd->hTreeWnd, TVM_SELECTITEM, TVGN_CARET, (LPARAM)ht.hItem);
506 root = (HTREEITEM)SendMessageW(g_pChildWnd->hTreeWnd, TVM_GETNEXTITEM, TVGN_ROOT, 0);
507 menu_id = (ht.hItem == root) ? PM_COMPUTER : PM_TREEVIEW;
510 else
512 int sel = SendMessageW(g_pChildWnd->hListWnd, LVM_GETNEXTITEM, -1,
513 MAKELPARAM(LVNI_SELECTED, 0));
514 menu_id = (sel == -1) ? PM_NEW_VALUE : PM_MODIFY_VALUE;
517 TrackPopupMenu(GetSubMenu(hPopupMenus, menu_id), TPM_RIGHTBUTTON,
518 pt.x, pt.y, 0, hFrameWnd, NULL);
519 break;
522 case WM_KEYDOWN:
523 if (wParam == VK_ESCAPE)
524 if (GetCapture() == hWnd) {
525 RECT rt;
526 draw_splitbar(hWnd, last_split);
527 GetClientRect(hWnd, &rt);
528 ResizeWnd(rt.right, rt.bottom);
529 last_split = -1;
530 ReleaseCapture();
531 SetCursor(LoadCursorW(0, (LPCWSTR)IDC_ARROW));
533 break;
535 case WM_MOUSEMOVE:
536 if (GetCapture() == hWnd) {
537 RECT rt;
538 int x = LOWORD(lParam);
539 HDC hdc = GetDC(hWnd);
540 GetClientRect(hWnd, &rt);
541 rt.left = last_split-SPLIT_WIDTH/2;
542 rt.right = last_split+SPLIT_WIDTH/2+1;
543 InvertRect(hdc, &rt);
544 last_split = x;
545 rt.left = x-SPLIT_WIDTH/2;
546 rt.right = x+SPLIT_WIDTH/2+1;
547 InvertRect(hdc, &rt);
548 ReleaseDC(hWnd, hdc);
550 break;
552 case WM_SETFOCUS:
553 if (g_pChildWnd != NULL) {
554 SetFocus(g_pChildWnd->nFocusPanel? g_pChildWnd->hListWnd: g_pChildWnd->hTreeWnd);
556 break;
558 case WM_TIMER:
559 break;
561 case WM_NOTIFY:
562 if (wParam == TREE_WINDOW && g_pChildWnd)
563 return treeview_notify(hWnd, message, wParam, lParam);
564 else if (wParam == LIST_WINDOW && g_pChildWnd)
565 return listview_notify(hWnd, message, wParam, lParam);
566 break;
568 case WM_SIZE:
569 if (wParam != SIZE_MINIMIZED && g_pChildWnd != NULL) {
570 ResizeWnd(LOWORD(lParam), HIWORD(lParam));
572 /* fall through */
573 default: def:
574 return DefWindowProcW(hWnd, message, wParam, lParam);
576 return 0;