regedit: Add support for REG_QWORD type.
[wine.git] / programs / regedit / childwnd.c
blob704299be8377113690548e080156515589b6906c
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"
28 #include "wine/heap.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(regedit);
32 ChildWnd* g_pChildWnd;
33 static int last_split;
35 static const WCHAR wszLastKey[] = L"LastKey";
36 static const WCHAR wszKeyName[] = L"Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\Regedit";
38 /*******************************************************************************
39 * Local module support methods
42 static LPCWSTR GetRootKeyName(HKEY hRootKey)
44 if(hRootKey == HKEY_CLASSES_ROOT)
45 return reg_class_namesW[INDEX_HKEY_CLASSES_ROOT];
46 if(hRootKey == HKEY_CURRENT_USER)
47 return reg_class_namesW[INDEX_HKEY_CURRENT_USER];
48 if(hRootKey == HKEY_LOCAL_MACHINE)
49 return reg_class_namesW[INDEX_HKEY_LOCAL_MACHINE];
50 if(hRootKey == HKEY_USERS)
51 return reg_class_namesW[INDEX_HKEY_USERS];
52 if(hRootKey == HKEY_CURRENT_CONFIG)
53 return reg_class_namesW[INDEX_HKEY_CURRENT_CONFIG];
54 if(hRootKey == HKEY_DYN_DATA)
55 return reg_class_namesW[INDEX_HKEY_DYN_DATA];
56 else
57 return L"Unknown HKEY. Please report.";
60 static void draw_splitbar(HWND hWnd, int x)
62 RECT rt;
63 HDC hdc = GetDC(hWnd);
65 GetClientRect(hWnd, &rt);
66 rt.left = x - SPLIT_WIDTH/2;
67 rt.right = x + SPLIT_WIDTH/2+1;
68 InvertRect(hdc, &rt);
69 ReleaseDC(hWnd, hdc);
72 static void ResizeWnd(int cx, int cy)
74 HDWP hdwp = BeginDeferWindowPos(2);
75 RECT rt = {0, 0, cx, cy};
77 cx = g_pChildWnd->nSplitPos + SPLIT_WIDTH/2;
78 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);
79 DeferWindowPos(hdwp, g_pChildWnd->hListWnd, 0, rt.left+cx , rt.top, rt.right-cx, rt.bottom-rt.top, SWP_NOZORDER|SWP_NOACTIVATE);
80 EndDeferWindowPos(hdwp);
83 static void OnPaint(HWND hWnd)
85 PAINTSTRUCT ps;
86 RECT rt;
88 GetClientRect(hWnd, &rt);
89 BeginPaint(hWnd, &ps);
90 FillRect(ps.hdc, &rt, GetSysColorBrush(COLOR_BTNFACE));
91 EndPaint(hWnd, &ps);
94 static LPWSTR CombinePaths(LPCWSTR pPaths[], int nPaths) {
95 int i, len, pos;
96 LPWSTR combined;
97 for (i=0, len=0; i<nPaths; i++) {
98 if (pPaths[i] && *pPaths[i]) {
99 len += lstrlenW(pPaths[i])+1;
102 combined = heap_xalloc(len * sizeof(WCHAR));
103 *combined = '\0';
104 for (i=0, pos=0; i<nPaths; i++) {
105 if (pPaths[i] && *pPaths[i]) {
106 int llen = lstrlenW(pPaths[i]);
107 if (!*combined)
108 lstrcpyW(combined, pPaths[i]);
109 else {
110 combined[pos++] = '\\';
111 lstrcpyW(combined+pos, pPaths[i]);
113 pos += llen;
116 return combined;
119 static LPWSTR GetPathRoot(HWND hwndTV, HTREEITEM hItem, BOOL bFull) {
120 LPCWSTR parts[2] = {0,0};
121 WCHAR text[260];
122 HKEY hRootKey = NULL;
123 if (!hItem)
124 hItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CARET, 0);
125 heap_free(GetItemPath(hwndTV, hItem, &hRootKey));
126 if (!bFull && !hRootKey)
127 return NULL;
128 if (hRootKey)
129 parts[1] = GetRootKeyName(hRootKey);
130 if (bFull) {
131 DWORD dwSize = ARRAY_SIZE(text);
132 GetComputerNameW(text, &dwSize);
133 parts[0] = text;
135 return CombinePaths(parts, 2);
138 LPWSTR GetItemFullPath(HWND hwndTV, HTREEITEM hItem, BOOL bFull) {
139 LPWSTR parts[2];
140 LPWSTR ret;
141 HKEY hRootKey = NULL;
143 parts[0] = GetPathRoot(hwndTV, hItem, bFull);
144 parts[1] = GetItemPath(hwndTV, hItem, &hRootKey);
145 ret = CombinePaths((LPCWSTR *)parts, 2);
146 heap_free(parts[0]);
147 heap_free(parts[1]);
148 return ret;
151 static void OnTreeSelectionChanged(HWND hwndTV, HWND hwndLV, HTREEITEM hItem, BOOL bRefreshLV)
153 if (bRefreshLV) {
154 LPWSTR keyPath;
155 HKEY hRootKey = NULL;
156 HTREEITEM rootitem;
158 rootitem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_ROOT, 0);
159 if (rootitem == hItem)
161 SendMessageW(hwndLV, LVM_DELETEALLITEMS, 0, 0);
162 UpdateStatusBar();
163 return;
166 keyPath = GetItemPath(hwndTV, hItem, &hRootKey);
167 RefreshListView(hwndLV, hRootKey, keyPath, NULL);
168 heap_free(keyPath);
170 UpdateStatusBar();
173 /*******************************************************************************
174 * finish_splitbar [internal]
176 * make the splitbar invisible and resize the windows
177 * (helper for ChildWndProc)
179 static void finish_splitbar(HWND hWnd, int x)
181 RECT rt;
183 draw_splitbar(hWnd, last_split);
184 last_split = -1;
185 GetClientRect(hWnd, &rt);
186 g_pChildWnd->nSplitPos = x;
187 ResizeWnd(rt.right, rt.bottom);
188 ReleaseCapture();
191 /*******************************************************************************
193 * FUNCTION: _CmdWndProc(HWND, unsigned, WORD, LONG)
195 * PURPOSE: Processes WM_COMMAND messages for the main frame window.
199 static BOOL _CmdWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
201 switch (LOWORD(wParam)) {
202 /* Parse the menu selections: */
203 case ID_REGISTRY_EXIT:
204 DestroyWindow(hWnd);
205 break;
206 case ID_VIEW_REFRESH:
207 WINE_TRACE("Is this ever called or is it just dead code?\n");
208 /* TODO */
209 break;
210 case ID_SWITCH_PANELS:
211 g_pChildWnd->nFocusPanel = !g_pChildWnd->nFocusPanel;
212 SetFocus(g_pChildWnd->nFocusPanel? g_pChildWnd->hListWnd: g_pChildWnd->hTreeWnd);
213 break;
214 default:
215 return FALSE;
217 return TRUE;
220 /*******************************************************************************
221 * get_last_key [internal]
223 * open last key
226 static void get_last_key(HWND hwndTV)
228 HKEY hkey;
229 WCHAR wszVal[KEY_MAX_LEN];
230 DWORD dwSize = sizeof(wszVal);
232 if (RegCreateKeyExW(HKEY_CURRENT_USER, wszKeyName, 0, NULL, 0, KEY_READ, NULL, &hkey, NULL) == ERROR_SUCCESS)
234 HTREEITEM selection = NULL;
235 if (RegQueryValueExW(hkey, wszLastKey, NULL, NULL, (LPBYTE)wszVal, &dwSize) == ERROR_SUCCESS)
237 if (lstrcmpW(wszVal, g_pChildWnd->szPath))
238 selection = FindPathInTree(hwndTV, wszVal);
241 if(!selection)
243 selection = (HTREEITEM)SendMessageW(g_pChildWnd->hTreeWnd, TVM_GETNEXTITEM, TVGN_ROOT, 0);
244 SendMessageW(hwndTV, TVM_EXPAND, TVE_EXPAND, (LPARAM)selection );
246 else
247 SendMessageW(hwndTV, TVM_SELECTITEM, TVGN_CARET, (LPARAM)selection);
249 RegCloseKey(hkey);
253 /*******************************************************************************
254 * set_last_key [internal]
256 * save last key
259 static void set_last_key(HWND hwndTV)
261 HKEY hkey;
263 if (RegCreateKeyExW(HKEY_CURRENT_USER, wszKeyName, 0, NULL, 0, KEY_WRITE, NULL, &hkey, NULL) == ERROR_SUCCESS)
265 HTREEITEM selection = (HTREEITEM)SendMessageW(g_pChildWnd->hTreeWnd, TVM_GETNEXTITEM, TVGN_CARET, 0);
266 HTREEITEM root = (HTREEITEM)SendMessageW(g_pChildWnd->hTreeWnd, TVM_GETNEXTITEM, TVGN_ROOT, 0);
267 WCHAR *value;
269 if (selection == root)
270 value = g_pChildWnd->szPath;
271 else
272 value = GetItemFullPath(g_pChildWnd->hTreeWnd, selection, FALSE);
273 RegSetValueExW(hkey, wszLastKey, 0, REG_SZ, (LPBYTE)value, (lstrlenW(value) + 1) * sizeof(WCHAR));
274 if (selection != root)
275 heap_free(value);
276 RegCloseKey(hkey);
280 static int treeview_notify(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
282 switch (((NMHDR *)lParam)->code)
284 case NM_SETFOCUS:
285 g_pChildWnd->nFocusPanel = 0;
286 break;
287 case TVN_BEGINLABELEDITW:
289 HKEY hRootKey;
290 WCHAR *path;
292 if (!GetWindowLongPtrW(g_pChildWnd->hTreeWnd, GWLP_USERDATA))
293 return 1;
295 path = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hRootKey);
296 if (!path || !*path)
297 return 1;
298 return 0;
300 case TVN_ENDLABELEDITW:
302 HKEY hRootKey;
303 NMTVDISPINFOW *dispInfo = (NMTVDISPINFOW *)lParam;
304 WCHAR *path = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hRootKey);
305 BOOL res = RenameKey(hWnd, hRootKey, path, dispInfo->item.pszText);
307 heap_free(path);
309 if (res)
311 TVITEMW item;
313 item.mask = TVIF_HANDLE | TVIF_TEXT;
314 item.hItem = dispInfo->item.hItem;
315 item.pszText = dispInfo->item.pszText;
316 SendMessageW(g_pChildWnd->hTreeWnd, TVM_SETITEMW, 0, (LPARAM)&item);
318 path = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hRootKey);
319 update_listview_path(path);
320 heap_free(path);
322 UpdateStatusBar();
325 SetWindowLongPtrW(g_pChildWnd->hTreeWnd, GWLP_USERDATA, 0);
326 return res;
328 case TVN_ITEMEXPANDINGW:
329 return !OnTreeExpanding(g_pChildWnd->hTreeWnd, (NMTREEVIEWW *)lParam);
330 case TVN_SELCHANGEDW:
331 OnTreeSelectionChanged(g_pChildWnd->hTreeWnd, g_pChildWnd->hListWnd,
332 ((NMTREEVIEWW *)lParam)->itemNew.hItem, TRUE);
333 break;
335 return 0;
338 static int listview_notify(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
340 switch (((NMHDR *)lParam)->code)
342 case NM_DBLCLK:
344 NMITEMACTIVATE *nmitem = (NMITEMACTIVATE *)lParam;
346 if (nmitem->iItem != -1)
348 LVITEMW item;
350 item.state = 0;
351 item.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
352 SendMessageW(g_pChildWnd->hListWnd, LVM_SETITEMSTATE, (UINT)-1, (LPARAM)&item);
354 item.state = LVIS_FOCUSED | LVIS_SELECTED;
355 item.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
356 SendMessageW(g_pChildWnd->hListWnd, LVM_SETITEMSTATE, nmitem->iItem, (LPARAM)&item);
358 SendMessageW(hFrameWnd, WM_COMMAND, ID_EDIT_MODIFY, 0);
360 break;
362 case NM_RETURN:
364 int cnt = SendMessageW(g_pChildWnd->hListWnd, LVM_GETNEXTITEM, -1,
365 MAKELPARAM(LVNI_FOCUSED | LVNI_SELECTED, 0));
366 if (cnt != -1)
367 SendMessageW(hFrameWnd, WM_COMMAND, ID_EDIT_MODIFY, 0);
368 break;
370 case NM_SETFOCUS:
371 g_pChildWnd->nFocusPanel = 1;
372 break;
373 case LVN_BEGINLABELEDITW:
374 if (!((NMLVDISPINFOW *)lParam)->item.iItem)
375 return 1;
376 return 0;
377 case LVN_COLUMNCLICK:
378 if (g_columnToSort == ((NMLISTVIEW *)lParam)->iSubItem)
379 g_invertSort = !g_invertSort;
380 else
382 g_columnToSort = ((NMLISTVIEW *)lParam)->iSubItem;
383 g_invertSort = FALSE;
386 SendMessageW(g_pChildWnd->hListWnd, LVM_SORTITEMS,
387 (WPARAM)g_pChildWnd->hListWnd, (LPARAM)CompareFunc);
388 break;
389 case LVN_DELETEITEM:
391 NMLISTVIEW *nmlv = (NMLISTVIEW *)lParam;
392 LINE_INFO *info = (LINE_INFO *)nmlv->lParam;
394 heap_free(info->name);
395 heap_free(info->val);
396 heap_free(info);
397 break;
399 case LVN_ENDLABELEDITW:
401 NMLVDISPINFOW *dispInfo = (NMLVDISPINFOW *)lParam;
402 WCHAR *oldName = GetItemText(g_pChildWnd->hListWnd, dispInfo->item.iItem);
403 LONG ret;
405 if (!oldName) return -1; /* cannot rename a default value */
406 ret = RenameValue(g_pChildWnd->hListWnd, g_currentRootKey, g_currentPath,
407 oldName, dispInfo->item.pszText);
408 if (ret)
410 dispInfo->item.iSubItem = 0;
411 SendMessageW(g_pChildWnd->hListWnd, LVM_SETITEMTEXTW,
412 dispInfo->item.iItem, (LPARAM)&dispInfo->item);
415 heap_free(oldName);
416 return 0;
418 case LVN_GETDISPINFOW:
419 OnGetDispInfo((NMLVDISPINFOW *)lParam);
420 break;
422 return 0;
425 #define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
426 #define GET_Y_LPARAM(lp) ((int)(short)HIWORD(lp))
428 /*******************************************************************************
430 * FUNCTION: ChildWndProc(HWND, unsigned, WORD, LONG)
432 * PURPOSE: Processes messages for the child windows.
434 * WM_COMMAND - process the application menu
435 * WM_PAINT - Paint the main window
436 * WM_DESTROY - post a quit message and return
439 LRESULT CALLBACK ChildWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
441 switch (message) {
442 case WM_CREATE:
443 g_pChildWnd = heap_xalloc(sizeof(ChildWnd));
444 if (!g_pChildWnd) return 0;
445 LoadStringW(hInst, IDS_REGISTRY_ROOT_NAME, g_pChildWnd->szPath, MAX_PATH);
446 g_pChildWnd->nSplitPos = 250;
447 g_pChildWnd->hWnd = hWnd;
448 g_pChildWnd->hTreeWnd = CreateTreeView(hWnd, g_pChildWnd->szPath, TREE_WINDOW);
449 g_pChildWnd->hListWnd = CreateListView(hWnd, LIST_WINDOW/*, g_pChildWnd->szPath*/);
450 g_pChildWnd->nFocusPanel = 1;
451 SetFocus(g_pChildWnd->hTreeWnd);
452 get_last_key(g_pChildWnd->hTreeWnd);
453 break;
454 case WM_COMMAND:
455 if (!_CmdWndProc(hWnd, message, wParam, lParam)) {
456 goto def;
458 break;
459 case WM_PAINT:
460 OnPaint(hWnd);
461 return 0;
462 case WM_SETCURSOR:
463 if (LOWORD(lParam) == HTCLIENT) {
464 POINT pt;
465 GetCursorPos(&pt);
466 ScreenToClient(hWnd, &pt);
467 if (pt.x>=g_pChildWnd->nSplitPos-SPLIT_WIDTH/2 && pt.x<g_pChildWnd->nSplitPos+SPLIT_WIDTH/2+1) {
468 SetCursor(LoadCursorW(0, (LPCWSTR)IDC_SIZEWE));
469 return TRUE;
472 goto def;
473 case WM_DESTROY:
474 set_last_key(g_pChildWnd->hTreeWnd);
475 heap_free(g_pChildWnd);
476 g_pChildWnd = NULL;
477 PostQuitMessage(0);
478 break;
479 case WM_LBUTTONDOWN: {
480 RECT rt;
481 int x = (short)LOWORD(lParam);
482 GetClientRect(hWnd, &rt);
483 if (x>=g_pChildWnd->nSplitPos-SPLIT_WIDTH/2 && x<g_pChildWnd->nSplitPos+SPLIT_WIDTH/2+1) {
484 last_split = g_pChildWnd->nSplitPos;
485 draw_splitbar(hWnd, last_split);
486 SetCapture(hWnd);
488 break;
491 /* WM_RBUTTONDOWN sets the splitbar the same way as WM_LBUTTONUP */
492 case WM_LBUTTONUP:
493 case WM_RBUTTONDOWN:
494 if (GetCapture() == hWnd) {
495 finish_splitbar(hWnd, LOWORD(lParam));
497 break;
499 case WM_CAPTURECHANGED:
500 if (GetCapture()==hWnd && last_split>=0)
501 draw_splitbar(hWnd, last_split);
502 break;
504 case WM_CONTEXTMENU:
506 POINT pt = {GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)};
507 short int menu_id = -1;
509 if ((HWND)wParam == g_pChildWnd->hTreeWnd)
511 TVHITTESTINFO ht;
513 ht.pt = pt;
514 ScreenToClient(g_pChildWnd->hTreeWnd, &ht.pt);
516 if (SendMessageW(g_pChildWnd->hTreeWnd, TVM_HITTEST, 0, (LPARAM)&ht))
518 HTREEITEM root;
520 SendMessageW(g_pChildWnd->hTreeWnd, TVM_SELECTITEM, TVGN_CARET, (LPARAM)ht.hItem);
521 root = (HTREEITEM)SendMessageW(g_pChildWnd->hTreeWnd, TVM_GETNEXTITEM, TVGN_ROOT, 0);
522 menu_id = (ht.hItem == root) ? PM_COMPUTER : PM_TREEVIEW;
525 else
527 int sel = SendMessageW(g_pChildWnd->hListWnd, LVM_GETNEXTITEM, -1,
528 MAKELPARAM(LVNI_SELECTED, 0));
529 menu_id = (sel == -1) ? PM_NEW_VALUE : PM_MODIFY_VALUE;
532 TrackPopupMenu(GetSubMenu(hPopupMenus, menu_id), TPM_RIGHTBUTTON,
533 pt.x, pt.y, 0, hFrameWnd, NULL);
534 break;
537 case WM_KEYDOWN:
538 if (wParam == VK_ESCAPE)
539 if (GetCapture() == hWnd) {
540 RECT rt;
541 draw_splitbar(hWnd, last_split);
542 GetClientRect(hWnd, &rt);
543 ResizeWnd(rt.right, rt.bottom);
544 last_split = -1;
545 ReleaseCapture();
546 SetCursor(LoadCursorW(0, (LPCWSTR)IDC_ARROW));
548 break;
550 case WM_MOUSEMOVE:
551 if (GetCapture() == hWnd) {
552 RECT rt;
553 int x = LOWORD(lParam);
554 HDC hdc = GetDC(hWnd);
555 GetClientRect(hWnd, &rt);
556 rt.left = last_split-SPLIT_WIDTH/2;
557 rt.right = last_split+SPLIT_WIDTH/2+1;
558 InvertRect(hdc, &rt);
559 last_split = x;
560 rt.left = x-SPLIT_WIDTH/2;
561 rt.right = x+SPLIT_WIDTH/2+1;
562 InvertRect(hdc, &rt);
563 ReleaseDC(hWnd, hdc);
565 break;
567 case WM_SETFOCUS:
568 if (g_pChildWnd != NULL) {
569 SetFocus(g_pChildWnd->nFocusPanel? g_pChildWnd->hListWnd: g_pChildWnd->hTreeWnd);
571 break;
573 case WM_TIMER:
574 break;
576 case WM_NOTIFY:
577 if (wParam == TREE_WINDOW && g_pChildWnd)
578 return treeview_notify(hWnd, message, wParam, lParam);
579 else if (wParam == LIST_WINDOW && g_pChildWnd)
580 return listview_notify(hWnd, message, wParam, lParam);
581 break;
583 case WM_SIZE:
584 if (wParam != SIZE_MINIMIZED && g_pChildWnd != NULL) {
585 ResizeWnd(LOWORD(lParam), HIWORD(lParam));
587 /* fall through */
588 default: def:
589 return DefWindowProcW(hWnd, message, wParam, lParam);
591 return 0;