shell32/tests: Use the available ARRAY_SIZE() macro.
[wine.git] / programs / regedit / childwnd.c
blobd8431f10ec11b0efea772bb170cd48f9322ae3e9
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"
29 #include "wine/unicode.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(regedit);
33 ChildWnd* g_pChildWnd;
34 static int last_split;
36 static const WCHAR wszLastKey[] = {'L','a','s','t','K','e','y',0};
37 static const WCHAR wszKeyName[] = {'S','o','f','t','w','a','r','e','\\',
38 'M','i','c','r','o','s','o','f','t','\\',
39 'W','i','n','d','o','w','s','\\',
40 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
41 'A','p','p','l','e','t','s','\\','R','e','g','e','d','i','t',0};
43 /*******************************************************************************
44 * Local module support methods
47 static LPCWSTR GetRootKeyName(HKEY hRootKey)
49 if(hRootKey == HKEY_CLASSES_ROOT)
50 return reg_class_namesW[INDEX_HKEY_CLASSES_ROOT];
51 if(hRootKey == HKEY_CURRENT_USER)
52 return reg_class_namesW[INDEX_HKEY_CURRENT_USER];
53 if(hRootKey == HKEY_LOCAL_MACHINE)
54 return reg_class_namesW[INDEX_HKEY_LOCAL_MACHINE];
55 if(hRootKey == HKEY_USERS)
56 return reg_class_namesW[INDEX_HKEY_USERS];
57 if(hRootKey == HKEY_CURRENT_CONFIG)
58 return reg_class_namesW[INDEX_HKEY_CURRENT_CONFIG];
59 if(hRootKey == HKEY_DYN_DATA)
60 return reg_class_namesW[INDEX_HKEY_DYN_DATA];
61 else
63 static const WCHAR unknown_key[] = {'U','N','K','N','O','W','N',' ','H','K','E','Y',',',' ',
64 'P','L','E','A','S','E',' ','R','E','P','O','R','T',0};
65 return unknown_key;
69 static void draw_splitbar(HWND hWnd, int x)
71 RECT rt;
72 HDC hdc = GetDC(hWnd);
74 GetClientRect(hWnd, &rt);
75 rt.left = x - SPLIT_WIDTH/2;
76 rt.right = x + SPLIT_WIDTH/2+1;
77 InvertRect(hdc, &rt);
78 ReleaseDC(hWnd, hdc);
81 static void ResizeWnd(int cx, int cy)
83 HDWP hdwp = BeginDeferWindowPos(2);
84 RECT rt = {0, 0, cx, cy};
86 cx = g_pChildWnd->nSplitPos + SPLIT_WIDTH/2;
87 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);
88 DeferWindowPos(hdwp, g_pChildWnd->hListWnd, 0, rt.left+cx , rt.top, rt.right-cx, rt.bottom-rt.top, SWP_NOZORDER|SWP_NOACTIVATE);
89 EndDeferWindowPos(hdwp);
92 static void OnPaint(HWND hWnd)
94 PAINTSTRUCT ps;
95 RECT rt;
97 GetClientRect(hWnd, &rt);
98 BeginPaint(hWnd, &ps);
99 FillRect(ps.hdc, &rt, GetSysColorBrush(COLOR_BTNFACE));
100 EndPaint(hWnd, &ps);
103 static LPWSTR CombinePaths(LPCWSTR pPaths[], int nPaths) {
104 int i, len, pos;
105 LPWSTR combined;
106 for (i=0, len=0; i<nPaths; i++) {
107 if (pPaths[i] && *pPaths[i]) {
108 len += lstrlenW(pPaths[i])+1;
111 combined = heap_xalloc(len * sizeof(WCHAR));
112 *combined = '\0';
113 for (i=0, pos=0; i<nPaths; i++) {
114 if (pPaths[i] && *pPaths[i]) {
115 int llen = lstrlenW(pPaths[i]);
116 if (!*combined)
117 lstrcpyW(combined, pPaths[i]);
118 else {
119 combined[pos++] = '\\';
120 lstrcpyW(combined+pos, pPaths[i]);
122 pos += llen;
125 return combined;
128 static LPWSTR GetPathRoot(HWND hwndTV, HTREEITEM hItem, BOOL bFull) {
129 LPCWSTR parts[2] = {0,0};
130 WCHAR text[260];
131 HKEY hRootKey = NULL;
132 if (!hItem)
133 hItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CARET, 0);
134 heap_free(GetItemPath(hwndTV, hItem, &hRootKey));
135 if (!bFull && !hRootKey)
136 return NULL;
137 if (hRootKey)
138 parts[1] = GetRootKeyName(hRootKey);
139 if (bFull) {
140 DWORD dwSize = ARRAY_SIZE(text);
141 GetComputerNameW(text, &dwSize);
142 parts[0] = text;
144 return CombinePaths(parts, 2);
147 LPWSTR GetItemFullPath(HWND hwndTV, HTREEITEM hItem, BOOL bFull) {
148 LPWSTR parts[2];
149 LPWSTR ret;
150 HKEY hRootKey = NULL;
152 parts[0] = GetPathRoot(hwndTV, hItem, bFull);
153 parts[1] = GetItemPath(hwndTV, hItem, &hRootKey);
154 ret = CombinePaths((LPCWSTR *)parts, 2);
155 heap_free(parts[0]);
156 heap_free(parts[1]);
157 return ret;
160 static void OnTreeSelectionChanged(HWND hwndTV, HWND hwndLV, HTREEITEM hItem, BOOL bRefreshLV)
162 if (bRefreshLV) {
163 LPWSTR keyPath;
164 HKEY hRootKey = NULL;
165 HTREEITEM rootitem;
167 rootitem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_ROOT, 0);
168 if (rootitem == hItem)
170 SendMessageW(hwndLV, LVM_DELETEALLITEMS, 0, 0);
171 UpdateStatusBar();
172 return;
175 keyPath = GetItemPath(hwndTV, hItem, &hRootKey);
176 RefreshListView(hwndLV, hRootKey, keyPath, NULL);
177 heap_free(keyPath);
179 UpdateStatusBar();
182 /*******************************************************************************
183 * finish_splitbar [internal]
185 * make the splitbar invisible and resize the windows
186 * (helper for ChildWndProc)
188 static void finish_splitbar(HWND hWnd, int x)
190 RECT rt;
192 draw_splitbar(hWnd, last_split);
193 last_split = -1;
194 GetClientRect(hWnd, &rt);
195 g_pChildWnd->nSplitPos = x;
196 ResizeWnd(rt.right, rt.bottom);
197 ReleaseCapture();
200 /*******************************************************************************
202 * FUNCTION: _CmdWndProc(HWND, unsigned, WORD, LONG)
204 * PURPOSE: Processes WM_COMMAND messages for the main frame window.
208 static BOOL _CmdWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
210 switch (LOWORD(wParam)) {
211 /* Parse the menu selections: */
212 case ID_REGISTRY_EXIT:
213 DestroyWindow(hWnd);
214 break;
215 case ID_VIEW_REFRESH:
216 WINE_TRACE("Is this ever called or is it just dead code?\n");
217 /* TODO */
218 break;
219 case ID_SWITCH_PANELS:
220 g_pChildWnd->nFocusPanel = !g_pChildWnd->nFocusPanel;
221 SetFocus(g_pChildWnd->nFocusPanel? g_pChildWnd->hListWnd: g_pChildWnd->hTreeWnd);
222 break;
223 default:
224 return FALSE;
226 return TRUE;
229 /*******************************************************************************
230 * get_last_key [internal]
232 * open last key
235 static void get_last_key(HWND hwndTV)
237 HKEY hkey;
238 WCHAR wszVal[KEY_MAX_LEN];
239 DWORD dwSize = sizeof(wszVal);
241 if (RegCreateKeyExW(HKEY_CURRENT_USER, wszKeyName, 0, NULL, 0, KEY_READ, NULL, &hkey, NULL) == ERROR_SUCCESS)
243 if (RegQueryValueExW(hkey, wszLastKey, NULL, NULL, (LPBYTE)wszVal, &dwSize) == ERROR_SUCCESS)
245 HTREEITEM selection;
246 if (!strcmpW(wszVal, g_pChildWnd->szPath))
247 selection = (HTREEITEM)SendMessageW(g_pChildWnd->hTreeWnd, TVM_GETNEXTITEM, TVGN_ROOT, 0);
248 else
249 selection = FindPathInTree(hwndTV, wszVal);
250 SendMessageW(hwndTV, TVM_SELECTITEM, TVGN_CARET, (LPARAM)selection);
253 RegCloseKey(hkey);
257 /*******************************************************************************
258 * set_last_key [internal]
260 * save last key
263 static void set_last_key(HWND hwndTV)
265 HKEY hkey;
267 if (RegCreateKeyExW(HKEY_CURRENT_USER, wszKeyName, 0, NULL, 0, KEY_WRITE, NULL, &hkey, NULL) == ERROR_SUCCESS)
269 HTREEITEM selection = (HTREEITEM)SendMessageW(g_pChildWnd->hTreeWnd, TVM_GETNEXTITEM, TVGN_CARET, 0);
270 HTREEITEM root = (HTREEITEM)SendMessageW(g_pChildWnd->hTreeWnd, TVM_GETNEXTITEM, TVGN_ROOT, 0);
271 WCHAR *value;
273 if (selection == root)
274 value = g_pChildWnd->szPath;
275 else
276 value = GetItemFullPath(g_pChildWnd->hTreeWnd, selection, FALSE);
277 RegSetValueExW(hkey, wszLastKey, 0, REG_SZ, (LPBYTE)value, (lstrlenW(value) + 1) * sizeof(WCHAR));
278 if (selection != root)
279 heap_free(value);
280 RegCloseKey(hkey);
284 static int treeview_notify(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
286 switch (((NMHDR *)lParam)->code)
288 case NM_SETFOCUS:
289 g_pChildWnd->nFocusPanel = 0;
290 break;
291 case TVN_BEGINLABELEDITW:
293 HKEY hRootKey;
294 WCHAR *path;
296 if (!GetWindowLongPtrW(g_pChildWnd->hTreeWnd, GWLP_USERDATA))
297 return 1;
299 path = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hRootKey);
300 if (!path || !*path)
301 return 1;
302 return 0;
304 case TVN_ENDLABELEDITW:
306 HKEY hRootKey;
307 NMTVDISPINFOW *dispInfo = (NMTVDISPINFOW *)lParam;
308 WCHAR *path = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hRootKey);
309 BOOL res = RenameKey(hWnd, hRootKey, path, dispInfo->item.pszText);
311 heap_free(path);
313 if (res)
315 TVITEMW item;
317 item.mask = TVIF_HANDLE | TVIF_TEXT;
318 item.hItem = dispInfo->item.hItem;
319 item.pszText = dispInfo->item.pszText;
320 SendMessageW(g_pChildWnd->hTreeWnd, TVM_SETITEMW, 0, (LPARAM)&item);
322 path = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hRootKey);
323 update_listview_path(path);
324 heap_free(path);
326 UpdateStatusBar();
329 SetWindowLongPtrW(g_pChildWnd->hTreeWnd, GWLP_USERDATA, 0);
330 return res;
332 case TVN_ITEMEXPANDINGW:
333 return !OnTreeExpanding(g_pChildWnd->hTreeWnd, (NMTREEVIEWW *)lParam);
334 case TVN_SELCHANGEDW:
335 OnTreeSelectionChanged(g_pChildWnd->hTreeWnd, g_pChildWnd->hListWnd,
336 ((NMTREEVIEWW *)lParam)->itemNew.hItem, TRUE);
337 break;
339 return 0;
342 static int listview_notify(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
344 switch (((NMHDR *)lParam)->code)
346 case NM_DBLCLK:
348 NMITEMACTIVATE *nmitem = (NMITEMACTIVATE *)lParam;
350 if (nmitem->iItem != -1)
352 LVITEMW item;
354 item.state = 0;
355 item.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
356 SendMessageW(g_pChildWnd->hListWnd, LVM_SETITEMSTATE, (UINT)-1, (LPARAM)&item);
358 item.state = LVIS_FOCUSED | LVIS_SELECTED;
359 item.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
360 SendMessageW(g_pChildWnd->hListWnd, LVM_SETITEMSTATE, nmitem->iItem, (LPARAM)&item);
362 SendMessageW(hFrameWnd, WM_COMMAND, ID_EDIT_MODIFY, 0);
364 break;
366 case NM_RETURN:
368 int cnt = SendMessageW(g_pChildWnd->hListWnd, LVM_GETNEXTITEM, -1,
369 MAKELPARAM(LVNI_FOCUSED | LVNI_SELECTED, 0));
370 if (cnt != -1)
371 SendMessageW(hFrameWnd, WM_COMMAND, ID_EDIT_MODIFY, 0);
372 break;
374 case NM_SETFOCUS:
375 g_pChildWnd->nFocusPanel = 1;
376 break;
377 case LVN_BEGINLABELEDITW:
378 if (!((NMLVDISPINFOW *)lParam)->item.iItem)
379 return 1;
380 return 0;
381 case LVN_COLUMNCLICK:
382 if (g_columnToSort == ((NMLISTVIEW *)lParam)->iSubItem)
383 g_invertSort = !g_invertSort;
384 else
386 g_columnToSort = ((NMLISTVIEW *)lParam)->iSubItem;
387 g_invertSort = FALSE;
390 SendMessageW(g_pChildWnd->hListWnd, LVM_SORTITEMS,
391 (WPARAM)g_pChildWnd->hListWnd, (LPARAM)CompareFunc);
392 break;
393 case LVN_DELETEITEM:
395 NMLISTVIEW *nmlv = (NMLISTVIEW *)lParam;
396 LINE_INFO *info = (LINE_INFO *)nmlv->lParam;
398 heap_free(info->name);
399 heap_free(info->val);
400 heap_free(info);
401 break;
403 case LVN_ENDLABELEDITW:
405 NMLVDISPINFOW *dispInfo = (NMLVDISPINFOW *)lParam;
406 WCHAR *oldName = GetItemText(g_pChildWnd->hListWnd, dispInfo->item.iItem);
407 LONG ret;
409 if (!oldName) return -1; /* cannot rename a default value */
410 ret = RenameValue(g_pChildWnd->hListWnd, g_currentRootKey, g_currentPath,
411 oldName, dispInfo->item.pszText);
412 if (ret)
414 dispInfo->item.iSubItem = 0;
415 SendMessageW(g_pChildWnd->hListWnd, LVM_SETITEMTEXTW,
416 dispInfo->item.iItem, (LPARAM)&dispInfo->item);
419 heap_free(oldName);
420 return 0;
422 case LVN_GETDISPINFOW:
423 OnGetDispInfo((NMLVDISPINFOW *)lParam);
424 break;
426 return 0;
429 #define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
430 #define GET_Y_LPARAM(lp) ((int)(short)HIWORD(lp))
432 /*******************************************************************************
434 * FUNCTION: ChildWndProc(HWND, unsigned, WORD, LONG)
436 * PURPOSE: Processes messages for the child windows.
438 * WM_COMMAND - process the application menu
439 * WM_PAINT - Paint the main window
440 * WM_DESTROY - post a quit message and return
443 LRESULT CALLBACK ChildWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
445 switch (message) {
446 case WM_CREATE:
447 g_pChildWnd = heap_xalloc(sizeof(ChildWnd));
448 if (!g_pChildWnd) return 0;
449 LoadStringW(hInst, IDS_REGISTRY_ROOT_NAME, g_pChildWnd->szPath, MAX_PATH);
450 g_pChildWnd->nSplitPos = 250;
451 g_pChildWnd->hWnd = hWnd;
452 g_pChildWnd->hTreeWnd = CreateTreeView(hWnd, g_pChildWnd->szPath, TREE_WINDOW);
453 g_pChildWnd->hListWnd = CreateListView(hWnd, LIST_WINDOW/*, g_pChildWnd->szPath*/);
454 g_pChildWnd->nFocusPanel = 1;
455 SetFocus(g_pChildWnd->hTreeWnd);
456 get_last_key(g_pChildWnd->hTreeWnd);
457 break;
458 case WM_COMMAND:
459 if (!_CmdWndProc(hWnd, message, wParam, lParam)) {
460 goto def;
462 break;
463 case WM_PAINT:
464 OnPaint(hWnd);
465 return 0;
466 case WM_SETCURSOR:
467 if (LOWORD(lParam) == HTCLIENT) {
468 POINT pt;
469 GetCursorPos(&pt);
470 ScreenToClient(hWnd, &pt);
471 if (pt.x>=g_pChildWnd->nSplitPos-SPLIT_WIDTH/2 && pt.x<g_pChildWnd->nSplitPos+SPLIT_WIDTH/2+1) {
472 SetCursor(LoadCursorW(0, (LPCWSTR)IDC_SIZEWE));
473 return TRUE;
476 goto def;
477 case WM_DESTROY:
478 set_last_key(g_pChildWnd->hTreeWnd);
479 heap_free(g_pChildWnd);
480 g_pChildWnd = NULL;
481 PostQuitMessage(0);
482 break;
483 case WM_LBUTTONDOWN: {
484 RECT rt;
485 int x = (short)LOWORD(lParam);
486 GetClientRect(hWnd, &rt);
487 if (x>=g_pChildWnd->nSplitPos-SPLIT_WIDTH/2 && x<g_pChildWnd->nSplitPos+SPLIT_WIDTH/2+1) {
488 last_split = g_pChildWnd->nSplitPos;
489 draw_splitbar(hWnd, last_split);
490 SetCapture(hWnd);
492 break;
495 /* WM_RBUTTONDOWN sets the splitbar the same way as WM_LBUTTONUP */
496 case WM_LBUTTONUP:
497 case WM_RBUTTONDOWN:
498 if (GetCapture() == hWnd) {
499 finish_splitbar(hWnd, LOWORD(lParam));
501 break;
503 case WM_CAPTURECHANGED:
504 if (GetCapture()==hWnd && last_split>=0)
505 draw_splitbar(hWnd, last_split);
506 break;
508 case WM_CONTEXTMENU:
510 POINT pt = {GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)};
511 short int menu_id = -1;
513 if ((HWND)wParam == g_pChildWnd->hTreeWnd)
515 TVHITTESTINFO ht;
517 ht.pt = pt;
518 ScreenToClient(g_pChildWnd->hTreeWnd, &ht.pt);
520 if (SendMessageW(g_pChildWnd->hTreeWnd, TVM_HITTEST, 0, (LPARAM)&ht))
522 HTREEITEM root;
524 SendMessageW(g_pChildWnd->hTreeWnd, TVM_SELECTITEM, TVGN_CARET, (LPARAM)ht.hItem);
525 root = (HTREEITEM)SendMessageW(g_pChildWnd->hTreeWnd, TVM_GETNEXTITEM, TVGN_ROOT, 0);
526 menu_id = (ht.hItem == root) ? PM_COMPUTER : PM_TREEVIEW;
529 else
531 int sel = SendMessageW(g_pChildWnd->hListWnd, LVM_GETNEXTITEM, -1,
532 MAKELPARAM(LVNI_SELECTED, 0));
533 menu_id = (sel == -1) ? PM_NEW_VALUE : PM_MODIFY_VALUE;
536 TrackPopupMenu(GetSubMenu(hPopupMenus, menu_id), TPM_RIGHTBUTTON,
537 pt.x, pt.y, 0, hFrameWnd, NULL);
538 break;
541 case WM_KEYDOWN:
542 if (wParam == VK_ESCAPE)
543 if (GetCapture() == hWnd) {
544 RECT rt;
545 draw_splitbar(hWnd, last_split);
546 GetClientRect(hWnd, &rt);
547 ResizeWnd(rt.right, rt.bottom);
548 last_split = -1;
549 ReleaseCapture();
550 SetCursor(LoadCursorW(0, (LPCWSTR)IDC_ARROW));
552 break;
554 case WM_MOUSEMOVE:
555 if (GetCapture() == hWnd) {
556 RECT rt;
557 int x = LOWORD(lParam);
558 HDC hdc = GetDC(hWnd);
559 GetClientRect(hWnd, &rt);
560 rt.left = last_split-SPLIT_WIDTH/2;
561 rt.right = last_split+SPLIT_WIDTH/2+1;
562 InvertRect(hdc, &rt);
563 last_split = x;
564 rt.left = x-SPLIT_WIDTH/2;
565 rt.right = x+SPLIT_WIDTH/2+1;
566 InvertRect(hdc, &rt);
567 ReleaseDC(hWnd, hdc);
569 break;
571 case WM_SETFOCUS:
572 if (g_pChildWnd != NULL) {
573 SetFocus(g_pChildWnd->nFocusPanel? g_pChildWnd->hListWnd: g_pChildWnd->hTreeWnd);
575 break;
577 case WM_TIMER:
578 break;
580 case WM_NOTIFY:
581 if (wParam == TREE_WINDOW && g_pChildWnd)
582 return treeview_notify(hWnd, message, wParam, lParam);
583 else if (wParam == LIST_WINDOW && g_pChildWnd)
584 return listview_notify(hWnd, message, wParam, lParam);
585 break;
587 case WM_SIZE:
588 if (wParam != SIZE_MINIMIZED && g_pChildWnd != NULL) {
589 ResizeWnd(LOWORD(lParam), HIWORD(lParam));
591 /* fall through */
592 default: def:
593 return DefWindowProcW(hWnd, message, wParam, lParam);
595 return 0;