comctl32/tests: Enable more ListView tests on Comctl32 v6.
[wine.git] / programs / regedit / childwnd.c
blob7a5633fb0dfcd57d8ba41b010a2d187b862eaeaa
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 <stdio.h>
26 #include "main.h"
27 #include "regproc.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 static const WCHAR wszLastKey[] = {'L','a','s','t','K','e','y',0};
38 static const WCHAR wszKeyName[] = {'S','o','f','t','w','a','r','e','\\',
39 'M','i','c','r','o','s','o','f','t','\\',
40 'W','i','n','d','o','w','s','\\',
41 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
42 'A','p','p','l','e','t','s','\\','R','e','g','e','d','i','t',0};
44 /*******************************************************************************
45 * Local module support methods
48 static LPCWSTR GetRootKeyName(HKEY hRootKey)
50 if(hRootKey == HKEY_CLASSES_ROOT)
51 return reg_class_namesW[INDEX_HKEY_CLASSES_ROOT];
52 if(hRootKey == HKEY_CURRENT_USER)
53 return reg_class_namesW[INDEX_HKEY_CURRENT_USER];
54 if(hRootKey == HKEY_LOCAL_MACHINE)
55 return reg_class_namesW[INDEX_HKEY_LOCAL_MACHINE];
56 if(hRootKey == HKEY_USERS)
57 return reg_class_namesW[INDEX_HKEY_USERS];
58 if(hRootKey == HKEY_CURRENT_CONFIG)
59 return reg_class_namesW[INDEX_HKEY_CURRENT_CONFIG];
60 if(hRootKey == HKEY_DYN_DATA)
61 return reg_class_namesW[INDEX_HKEY_DYN_DATA];
62 else
64 static const WCHAR unknown_key[] = {'U','N','K','N','O','W','N',' ','H','K','E','Y',',',' ',
65 'P','L','E','A','S','E',' ','R','E','P','O','R','T',0};
66 return unknown_key;
70 static void draw_splitbar(HWND hWnd, int x)
72 RECT rt;
73 HDC hdc = GetDC(hWnd);
75 GetClientRect(hWnd, &rt);
76 rt.left = x - SPLIT_WIDTH/2;
77 rt.right = x + SPLIT_WIDTH/2+1;
78 InvertRect(hdc, &rt);
79 ReleaseDC(hWnd, hdc);
82 static void ResizeWnd(int cx, int cy)
84 HDWP hdwp = BeginDeferWindowPos(2);
85 RECT rt = {0, 0, cx, cy};
87 cx = g_pChildWnd->nSplitPos + SPLIT_WIDTH/2;
88 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);
89 DeferWindowPos(hdwp, g_pChildWnd->hListWnd, 0, rt.left+cx , rt.top, rt.right-cx, rt.bottom-rt.top, SWP_NOZORDER|SWP_NOACTIVATE);
90 EndDeferWindowPos(hdwp);
93 static void OnPaint(HWND hWnd)
95 PAINTSTRUCT ps;
96 RECT rt;
98 GetClientRect(hWnd, &rt);
99 BeginPaint(hWnd, &ps);
100 FillRect(ps.hdc, &rt, GetSysColorBrush(COLOR_BTNFACE));
101 EndPaint(hWnd, &ps);
104 static LPWSTR CombinePaths(LPCWSTR pPaths[], int nPaths) {
105 int i, len, pos;
106 LPWSTR combined;
107 for (i=0, len=0; i<nPaths; i++) {
108 if (pPaths[i] && *pPaths[i]) {
109 len += lstrlenW(pPaths[i])+1;
112 combined = heap_xalloc(len * sizeof(WCHAR));
113 *combined = '\0';
114 for (i=0, pos=0; i<nPaths; i++) {
115 if (pPaths[i] && *pPaths[i]) {
116 int llen = lstrlenW(pPaths[i]);
117 if (!*combined)
118 lstrcpyW(combined, pPaths[i]);
119 else {
120 combined[pos++] = '\\';
121 lstrcpyW(combined+pos, pPaths[i]);
123 pos += llen;
126 return combined;
129 static LPWSTR GetPathRoot(HWND hwndTV, HTREEITEM hItem, BOOL bFull) {
130 LPCWSTR parts[2] = {0,0};
131 WCHAR text[260];
132 HKEY hRootKey = NULL;
133 if (!hItem)
134 hItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CARET, 0);
135 heap_free(GetItemPath(hwndTV, hItem, &hRootKey));
136 if (!bFull && !hRootKey)
137 return NULL;
138 if (hRootKey)
139 parts[1] = GetRootKeyName(hRootKey);
140 if (bFull) {
141 DWORD dwSize = sizeof(text)/sizeof(WCHAR);
142 GetComputerNameW(text, &dwSize);
143 parts[0] = text;
145 return CombinePaths(parts, 2);
148 LPWSTR GetItemFullPath(HWND hwndTV, HTREEITEM hItem, BOOL bFull) {
149 LPWSTR parts[2];
150 LPWSTR ret;
151 HKEY hRootKey = NULL;
153 parts[0] = GetPathRoot(hwndTV, hItem, bFull);
154 parts[1] = GetItemPath(hwndTV, hItem, &hRootKey);
155 ret = CombinePaths((LPCWSTR *)parts, 2);
156 heap_free(parts[0]);
157 heap_free(parts[1]);
158 return ret;
161 static void OnTreeSelectionChanged(HWND hwndTV, HWND hwndLV, HTREEITEM hItem, BOOL bRefreshLV)
163 if (bRefreshLV) {
164 LPWSTR keyPath;
165 HKEY hRootKey = NULL;
166 HTREEITEM rootitem;
168 rootitem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_ROOT, 0);
169 if (rootitem == hItem)
171 SendMessageW(hwndLV, LVM_DELETEALLITEMS, 0, 0);
172 UpdateStatusBar();
173 return;
176 keyPath = GetItemPath(hwndTV, hItem, &hRootKey);
177 RefreshListView(hwndLV, hRootKey, keyPath, NULL);
178 heap_free(keyPath);
180 UpdateStatusBar();
183 /*******************************************************************************
184 * finish_splitbar [internal]
186 * make the splitbar invisible and resize the windows
187 * (helper for ChildWndProc)
189 static void finish_splitbar(HWND hWnd, int x)
191 RECT rt;
193 draw_splitbar(hWnd, last_split);
194 last_split = -1;
195 GetClientRect(hWnd, &rt);
196 g_pChildWnd->nSplitPos = x;
197 ResizeWnd(rt.right, rt.bottom);
198 ReleaseCapture();
201 /*******************************************************************************
203 * FUNCTION: _CmdWndProc(HWND, unsigned, WORD, LONG)
205 * PURPOSE: Processes WM_COMMAND messages for the main frame window.
209 static BOOL _CmdWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
211 switch (LOWORD(wParam)) {
212 /* Parse the menu selections: */
213 case ID_REGISTRY_EXIT:
214 DestroyWindow(hWnd);
215 break;
216 case ID_VIEW_REFRESH:
217 WINE_TRACE("Is this ever called or is it just dead code?\n");
218 /* TODO */
219 break;
220 case ID_SWITCH_PANELS:
221 g_pChildWnd->nFocusPanel = !g_pChildWnd->nFocusPanel;
222 SetFocus(g_pChildWnd->nFocusPanel? g_pChildWnd->hListWnd: g_pChildWnd->hTreeWnd);
223 break;
224 default:
225 return FALSE;
227 return TRUE;
230 /*******************************************************************************
231 * get_last_key [internal]
233 * open last key
236 static void get_last_key(HWND hwndTV)
238 HKEY hkey;
239 WCHAR wszVal[KEY_MAX_LEN];
240 DWORD dwSize = sizeof(wszVal);
242 if (RegCreateKeyExW(HKEY_CURRENT_USER, wszKeyName, 0, NULL, 0, KEY_READ, NULL, &hkey, NULL) == ERROR_SUCCESS)
244 if (RegQueryValueExW(hkey, wszLastKey, NULL, NULL, (LPBYTE)wszVal, &dwSize) == ERROR_SUCCESS)
246 HTREEITEM selection;
247 if (!strcmpW(wszVal, g_pChildWnd->szPath))
248 selection = (HTREEITEM)SendMessageW(g_pChildWnd->hTreeWnd, TVM_GETNEXTITEM, TVGN_ROOT, 0);
249 else
250 selection = FindPathInTree(hwndTV, wszVal);
251 SendMessageW(hwndTV, TVM_SELECTITEM, TVGN_CARET, (LPARAM)selection);
254 RegCloseKey(hkey);
258 /*******************************************************************************
259 * set_last_key [internal]
261 * save last key
264 static void set_last_key(HWND hwndTV)
266 HKEY hkey;
268 if (RegCreateKeyExW(HKEY_CURRENT_USER, wszKeyName, 0, NULL, 0, KEY_WRITE, NULL, &hkey, NULL) == ERROR_SUCCESS)
270 HTREEITEM selection = (HTREEITEM)SendMessageW(g_pChildWnd->hTreeWnd, TVM_GETNEXTITEM, TVGN_CARET, 0);
271 HTREEITEM root = (HTREEITEM)SendMessageW(g_pChildWnd->hTreeWnd, TVM_GETNEXTITEM, TVGN_ROOT, 0);
272 WCHAR *value;
274 if (selection == root)
275 value = g_pChildWnd->szPath;
276 else
277 value = GetItemFullPath(g_pChildWnd->hTreeWnd, selection, FALSE);
278 RegSetValueExW(hkey, wszLastKey, 0, REG_SZ, (LPBYTE)value, (lstrlenW(value) + 1) * sizeof(WCHAR));
279 if (selection != root)
280 heap_free(value);
281 RegCloseKey(hkey);
285 static int treeview_notify(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
287 switch (((NMHDR *)lParam)->code)
289 case NM_SETFOCUS:
290 g_pChildWnd->nFocusPanel = 0;
291 break;
292 case TVN_BEGINLABELEDITW:
294 HKEY hRootKey;
295 WCHAR *path;
297 if (!GetWindowLongPtrW(g_pChildWnd->hTreeWnd, GWLP_USERDATA))
298 return 1;
300 path = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hRootKey);
301 if (!path || !*path)
302 return 1;
303 return 0;
305 case TVN_ENDLABELEDITW:
307 HKEY hRootKey;
308 NMTVDISPINFOW *dispInfo = (NMTVDISPINFOW *)lParam;
309 WCHAR *path = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hRootKey);
310 BOOL res = RenameKey(hWnd, hRootKey, path, dispInfo->item.pszText);
312 heap_free(path);
314 if (res)
316 TVITEMW item;
318 item.mask = TVIF_HANDLE | TVIF_TEXT;
319 item.hItem = dispInfo->item.hItem;
320 item.pszText = dispInfo->item.pszText;
321 SendMessageW(g_pChildWnd->hTreeWnd, TVM_SETITEMW, 0, (LPARAM)&item);
323 path = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hRootKey);
324 update_listview_path(path);
325 heap_free(path);
327 UpdateStatusBar();
330 SetWindowLongPtrW(g_pChildWnd->hTreeWnd, GWLP_USERDATA, 0);
331 return res;
333 case TVN_ITEMEXPANDINGW:
334 return !OnTreeExpanding(g_pChildWnd->hTreeWnd, (NMTREEVIEWW *)lParam);
335 case TVN_SELCHANGEDW:
336 OnTreeSelectionChanged(g_pChildWnd->hTreeWnd, g_pChildWnd->hListWnd,
337 ((NMTREEVIEWW *)lParam)->itemNew.hItem, TRUE);
338 break;
340 return 0;
343 static int listview_notify(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
345 switch (((NMHDR *)lParam)->code)
347 case NM_DBLCLK:
349 NMITEMACTIVATE *nmitem = (NMITEMACTIVATE *)lParam;
351 if (nmitem->iItem != -1)
353 LVITEMW item;
355 item.state = 0;
356 item.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
357 SendMessageW(g_pChildWnd->hListWnd, LVM_SETITEMSTATE, (UINT)-1, (LPARAM)&item);
359 item.state = LVIS_FOCUSED | LVIS_SELECTED;
360 item.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
361 SendMessageW(g_pChildWnd->hListWnd, LVM_SETITEMSTATE, nmitem->iItem, (LPARAM)&item);
363 SendMessageW(hFrameWnd, WM_COMMAND, ID_EDIT_MODIFY, 0);
365 break;
367 case NM_RETURN:
369 int cnt = SendMessageW(g_pChildWnd->hListWnd, LVM_GETNEXTITEM, -1,
370 MAKELPARAM(LVNI_FOCUSED | LVNI_SELECTED, 0));
371 if (cnt != -1)
372 SendMessageW(hFrameWnd, WM_COMMAND, ID_EDIT_MODIFY, 0);
373 break;
375 case NM_SETFOCUS:
376 g_pChildWnd->nFocusPanel = 1;
377 break;
378 case LVN_BEGINLABELEDITW:
379 if (!((NMLVDISPINFOW *)lParam)->item.iItem)
380 return 1;
381 return 0;
382 case LVN_COLUMNCLICK:
383 if (g_columnToSort == ((NMLISTVIEW *)lParam)->iSubItem)
384 g_invertSort = !g_invertSort;
385 else
387 g_columnToSort = ((NMLISTVIEW *)lParam)->iSubItem;
388 g_invertSort = FALSE;
391 SendMessageW(g_pChildWnd->hListWnd, LVM_SORTITEMS,
392 (WPARAM)g_pChildWnd->hListWnd, (LPARAM)CompareFunc);
393 break;
394 case LVN_DELETEITEM:
396 NMLISTVIEW *nmlv = (NMLISTVIEW *)lParam;
397 LINE_INFO *info = (LINE_INFO *)nmlv->lParam;
399 heap_free(info->name);
400 heap_free(info->val);
401 heap_free(info);
402 break;
404 case LVN_ENDLABELEDITW:
406 NMLVDISPINFOW *dispInfo = (NMLVDISPINFOW *)lParam;
407 WCHAR *oldName = GetItemText(g_pChildWnd->hListWnd, dispInfo->item.iItem);
408 LONG ret;
410 if (!oldName) return -1; /* cannot rename a default value */
411 ret = RenameValue(g_pChildWnd->hListWnd, g_currentRootKey, g_currentPath,
412 oldName, dispInfo->item.pszText);
413 if (ret)
415 dispInfo->item.iSubItem = 0;
416 SendMessageW(g_pChildWnd->hListWnd, LVM_SETITEMTEXTW,
417 dispInfo->item.iItem, (LPARAM)&dispInfo->item);
420 heap_free(oldName);
421 return 0;
423 case LVN_GETDISPINFOW:
424 OnGetDispInfo((NMLVDISPINFOW *)lParam);
425 break;
427 return 0;
430 #define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
431 #define GET_Y_LPARAM(lp) ((int)(short)HIWORD(lp))
433 /*******************************************************************************
435 * FUNCTION: ChildWndProc(HWND, unsigned, WORD, LONG)
437 * PURPOSE: Processes messages for the child windows.
439 * WM_COMMAND - process the application menu
440 * WM_PAINT - Paint the main window
441 * WM_DESTROY - post a quit message and return
444 LRESULT CALLBACK ChildWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
446 switch (message) {
447 case WM_CREATE:
448 g_pChildWnd = heap_xalloc(sizeof(ChildWnd));
449 if (!g_pChildWnd) return 0;
450 LoadStringW(hInst, IDS_REGISTRY_ROOT_NAME, g_pChildWnd->szPath, MAX_PATH);
451 g_pChildWnd->nSplitPos = 250;
452 g_pChildWnd->hWnd = hWnd;
453 g_pChildWnd->hTreeWnd = CreateTreeView(hWnd, g_pChildWnd->szPath, TREE_WINDOW);
454 g_pChildWnd->hListWnd = CreateListView(hWnd, LIST_WINDOW/*, g_pChildWnd->szPath*/);
455 g_pChildWnd->nFocusPanel = 1;
456 SetFocus(g_pChildWnd->hTreeWnd);
457 get_last_key(g_pChildWnd->hTreeWnd);
458 break;
459 case WM_COMMAND:
460 if (!_CmdWndProc(hWnd, message, wParam, lParam)) {
461 goto def;
463 break;
464 case WM_PAINT:
465 OnPaint(hWnd);
466 return 0;
467 case WM_SETCURSOR:
468 if (LOWORD(lParam) == HTCLIENT) {
469 POINT pt;
470 GetCursorPos(&pt);
471 ScreenToClient(hWnd, &pt);
472 if (pt.x>=g_pChildWnd->nSplitPos-SPLIT_WIDTH/2 && pt.x<g_pChildWnd->nSplitPos+SPLIT_WIDTH/2+1) {
473 SetCursor(LoadCursorW(0, (LPCWSTR)IDC_SIZEWE));
474 return TRUE;
477 goto def;
478 case WM_DESTROY:
479 set_last_key(g_pChildWnd->hTreeWnd);
480 heap_free(g_pChildWnd);
481 g_pChildWnd = NULL;
482 PostQuitMessage(0);
483 break;
484 case WM_LBUTTONDOWN: {
485 RECT rt;
486 int x = (short)LOWORD(lParam);
487 GetClientRect(hWnd, &rt);
488 if (x>=g_pChildWnd->nSplitPos-SPLIT_WIDTH/2 && x<g_pChildWnd->nSplitPos+SPLIT_WIDTH/2+1) {
489 last_split = g_pChildWnd->nSplitPos;
490 draw_splitbar(hWnd, last_split);
491 SetCapture(hWnd);
493 break;
496 /* WM_RBUTTONDOWN sets the splitbar the same way as WM_LBUTTONUP */
497 case WM_LBUTTONUP:
498 case WM_RBUTTONDOWN:
499 if (GetCapture() == hWnd) {
500 finish_splitbar(hWnd, LOWORD(lParam));
502 break;
504 case WM_CAPTURECHANGED:
505 if (GetCapture()==hWnd && last_split>=0)
506 draw_splitbar(hWnd, last_split);
507 break;
509 case WM_CONTEXTMENU:
511 POINT pt = {GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)};
512 short int menu_id = -1;
514 if ((HWND)wParam == g_pChildWnd->hTreeWnd)
516 TVHITTESTINFO ht;
518 ht.pt = pt;
519 ScreenToClient(g_pChildWnd->hTreeWnd, &ht.pt);
521 if (SendMessageW(g_pChildWnd->hTreeWnd, TVM_HITTEST, 0, (LPARAM)&ht))
523 HTREEITEM root;
525 SendMessageW(g_pChildWnd->hTreeWnd, TVM_SELECTITEM, TVGN_CARET, (LPARAM)ht.hItem);
526 root = (HTREEITEM)SendMessageW(g_pChildWnd->hTreeWnd, TVM_GETNEXTITEM, TVGN_ROOT, 0);
527 menu_id = (ht.hItem == root) ? PM_COMPUTER : PM_TREEVIEW;
530 else
532 int sel = SendMessageW(g_pChildWnd->hListWnd, LVM_GETNEXTITEM, -1,
533 MAKELPARAM(LVNI_SELECTED, 0));
534 menu_id = (sel == -1) ? PM_NEW_VALUE : PM_MODIFY_VALUE;
537 TrackPopupMenu(GetSubMenu(hPopupMenus, menu_id), TPM_RIGHTBUTTON,
538 pt.x, pt.y, 0, hFrameWnd, NULL);
539 break;
542 case WM_KEYDOWN:
543 if (wParam == VK_ESCAPE)
544 if (GetCapture() == hWnd) {
545 RECT rt;
546 draw_splitbar(hWnd, last_split);
547 GetClientRect(hWnd, &rt);
548 ResizeWnd(rt.right, rt.bottom);
549 last_split = -1;
550 ReleaseCapture();
551 SetCursor(LoadCursorW(0, (LPCWSTR)IDC_ARROW));
553 break;
555 case WM_MOUSEMOVE:
556 if (GetCapture() == hWnd) {
557 RECT rt;
558 int x = LOWORD(lParam);
559 HDC hdc = GetDC(hWnd);
560 GetClientRect(hWnd, &rt);
561 rt.left = last_split-SPLIT_WIDTH/2;
562 rt.right = last_split+SPLIT_WIDTH/2+1;
563 InvertRect(hdc, &rt);
564 last_split = x;
565 rt.left = x-SPLIT_WIDTH/2;
566 rt.right = x+SPLIT_WIDTH/2+1;
567 InvertRect(hdc, &rt);
568 ReleaseDC(hWnd, hdc);
570 break;
572 case WM_SETFOCUS:
573 if (g_pChildWnd != NULL) {
574 SetFocus(g_pChildWnd->nFocusPanel? g_pChildWnd->hListWnd: g_pChildWnd->hTreeWnd);
576 break;
578 case WM_TIMER:
579 break;
581 case WM_NOTIFY:
582 if (wParam == TREE_WINDOW && g_pChildWnd)
583 return treeview_notify(hWnd, message, wParam, lParam);
584 else if (wParam == LIST_WINDOW && g_pChildWnd)
585 return listview_notify(hWnd, message, wParam, lParam);
586 break;
588 case WM_SIZE:
589 if (wParam != SIZE_MINIMIZED && g_pChildWnd != NULL) {
590 ResizeWnd(LOWORD(lParam), HIWORD(lParam));
592 /* fall through */
593 default: def:
594 return DefWindowProcW(hWnd, message, wParam, lParam);
596 return 0;