mfplat: Read queue subscriber within the critical section.
[wine/zf.git] / programs / regedit / childwnd.c
blob582f3c0c177bb4b4e4f95002b0075b01eb37fe8f
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','a','s','t','K','e','y',0};
36 static const WCHAR wszKeyName[] = {'S','o','f','t','w','a','r','e','\\',
37 'M','i','c','r','o','s','o','f','t','\\',
38 'W','i','n','d','o','w','s','\\',
39 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
40 'A','p','p','l','e','t','s','\\','R','e','g','e','d','i','t',0};
42 /*******************************************************************************
43 * Local module support methods
46 static LPCWSTR GetRootKeyName(HKEY hRootKey)
48 if(hRootKey == HKEY_CLASSES_ROOT)
49 return reg_class_namesW[INDEX_HKEY_CLASSES_ROOT];
50 if(hRootKey == HKEY_CURRENT_USER)
51 return reg_class_namesW[INDEX_HKEY_CURRENT_USER];
52 if(hRootKey == HKEY_LOCAL_MACHINE)
53 return reg_class_namesW[INDEX_HKEY_LOCAL_MACHINE];
54 if(hRootKey == HKEY_USERS)
55 return reg_class_namesW[INDEX_HKEY_USERS];
56 if(hRootKey == HKEY_CURRENT_CONFIG)
57 return reg_class_namesW[INDEX_HKEY_CURRENT_CONFIG];
58 if(hRootKey == HKEY_DYN_DATA)
59 return reg_class_namesW[INDEX_HKEY_DYN_DATA];
60 else
62 static const WCHAR unknown_key[] = {'U','N','K','N','O','W','N',' ','H','K','E','Y',',',' ',
63 'P','L','E','A','S','E',' ','R','E','P','O','R','T',0};
64 return unknown_key;
68 static void draw_splitbar(HWND hWnd, int x)
70 RECT rt;
71 HDC hdc = GetDC(hWnd);
73 GetClientRect(hWnd, &rt);
74 rt.left = x - SPLIT_WIDTH/2;
75 rt.right = x + SPLIT_WIDTH/2+1;
76 InvertRect(hdc, &rt);
77 ReleaseDC(hWnd, hdc);
80 static void ResizeWnd(int cx, int cy)
82 HDWP hdwp = BeginDeferWindowPos(2);
83 RECT rt = {0, 0, cx, cy};
85 cx = g_pChildWnd->nSplitPos + SPLIT_WIDTH/2;
86 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);
87 DeferWindowPos(hdwp, g_pChildWnd->hListWnd, 0, rt.left+cx , rt.top, rt.right-cx, rt.bottom-rt.top, SWP_NOZORDER|SWP_NOACTIVATE);
88 EndDeferWindowPos(hdwp);
91 static void OnPaint(HWND hWnd)
93 PAINTSTRUCT ps;
94 RECT rt;
96 GetClientRect(hWnd, &rt);
97 BeginPaint(hWnd, &ps);
98 FillRect(ps.hdc, &rt, GetSysColorBrush(COLOR_BTNFACE));
99 EndPaint(hWnd, &ps);
102 static LPWSTR CombinePaths(LPCWSTR pPaths[], int nPaths) {
103 int i, len, pos;
104 LPWSTR combined;
105 for (i=0, len=0; i<nPaths; i++) {
106 if (pPaths[i] && *pPaths[i]) {
107 len += lstrlenW(pPaths[i])+1;
110 combined = heap_xalloc(len * sizeof(WCHAR));
111 *combined = '\0';
112 for (i=0, pos=0; i<nPaths; i++) {
113 if (pPaths[i] && *pPaths[i]) {
114 int llen = lstrlenW(pPaths[i]);
115 if (!*combined)
116 lstrcpyW(combined, pPaths[i]);
117 else {
118 combined[pos++] = '\\';
119 lstrcpyW(combined+pos, pPaths[i]);
121 pos += llen;
124 return combined;
127 static LPWSTR GetPathRoot(HWND hwndTV, HTREEITEM hItem, BOOL bFull) {
128 LPCWSTR parts[2] = {0,0};
129 WCHAR text[260];
130 HKEY hRootKey = NULL;
131 if (!hItem)
132 hItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CARET, 0);
133 heap_free(GetItemPath(hwndTV, hItem, &hRootKey));
134 if (!bFull && !hRootKey)
135 return NULL;
136 if (hRootKey)
137 parts[1] = GetRootKeyName(hRootKey);
138 if (bFull) {
139 DWORD dwSize = ARRAY_SIZE(text);
140 GetComputerNameW(text, &dwSize);
141 parts[0] = text;
143 return CombinePaths(parts, 2);
146 LPWSTR GetItemFullPath(HWND hwndTV, HTREEITEM hItem, BOOL bFull) {
147 LPWSTR parts[2];
148 LPWSTR ret;
149 HKEY hRootKey = NULL;
151 parts[0] = GetPathRoot(hwndTV, hItem, bFull);
152 parts[1] = GetItemPath(hwndTV, hItem, &hRootKey);
153 ret = CombinePaths((LPCWSTR *)parts, 2);
154 heap_free(parts[0]);
155 heap_free(parts[1]);
156 return ret;
159 static void OnTreeSelectionChanged(HWND hwndTV, HWND hwndLV, HTREEITEM hItem, BOOL bRefreshLV)
161 if (bRefreshLV) {
162 LPWSTR keyPath;
163 HKEY hRootKey = NULL;
164 HTREEITEM rootitem;
166 rootitem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_ROOT, 0);
167 if (rootitem == hItem)
169 SendMessageW(hwndLV, LVM_DELETEALLITEMS, 0, 0);
170 UpdateStatusBar();
171 return;
174 keyPath = GetItemPath(hwndTV, hItem, &hRootKey);
175 RefreshListView(hwndLV, hRootKey, keyPath, NULL);
176 heap_free(keyPath);
178 UpdateStatusBar();
181 /*******************************************************************************
182 * finish_splitbar [internal]
184 * make the splitbar invisible and resize the windows
185 * (helper for ChildWndProc)
187 static void finish_splitbar(HWND hWnd, int x)
189 RECT rt;
191 draw_splitbar(hWnd, last_split);
192 last_split = -1;
193 GetClientRect(hWnd, &rt);
194 g_pChildWnd->nSplitPos = x;
195 ResizeWnd(rt.right, rt.bottom);
196 ReleaseCapture();
199 /*******************************************************************************
201 * FUNCTION: _CmdWndProc(HWND, unsigned, WORD, LONG)
203 * PURPOSE: Processes WM_COMMAND messages for the main frame window.
207 static BOOL _CmdWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
209 switch (LOWORD(wParam)) {
210 /* Parse the menu selections: */
211 case ID_REGISTRY_EXIT:
212 DestroyWindow(hWnd);
213 break;
214 case ID_VIEW_REFRESH:
215 WINE_TRACE("Is this ever called or is it just dead code?\n");
216 /* TODO */
217 break;
218 case ID_SWITCH_PANELS:
219 g_pChildWnd->nFocusPanel = !g_pChildWnd->nFocusPanel;
220 SetFocus(g_pChildWnd->nFocusPanel? g_pChildWnd->hListWnd: g_pChildWnd->hTreeWnd);
221 break;
222 default:
223 return FALSE;
225 return TRUE;
228 /*******************************************************************************
229 * get_last_key [internal]
231 * open last key
234 static void get_last_key(HWND hwndTV)
236 HKEY hkey;
237 WCHAR wszVal[KEY_MAX_LEN];
238 DWORD dwSize = sizeof(wszVal);
240 if (RegCreateKeyExW(HKEY_CURRENT_USER, wszKeyName, 0, NULL, 0, KEY_READ, NULL, &hkey, NULL) == ERROR_SUCCESS)
242 HTREEITEM selection = NULL;
243 if (RegQueryValueExW(hkey, wszLastKey, NULL, NULL, (LPBYTE)wszVal, &dwSize) == ERROR_SUCCESS)
245 if (lstrcmpW(wszVal, g_pChildWnd->szPath))
246 selection = FindPathInTree(hwndTV, wszVal);
249 if(!selection)
251 selection = (HTREEITEM)SendMessageW(g_pChildWnd->hTreeWnd, TVM_GETNEXTITEM, TVGN_ROOT, 0);
252 SendMessageW(hwndTV, TVM_EXPAND, TVE_EXPAND, (LPARAM)selection );
254 else
255 SendMessageW(hwndTV, TVM_SELECTITEM, TVGN_CARET, (LPARAM)selection);
257 RegCloseKey(hkey);
261 /*******************************************************************************
262 * set_last_key [internal]
264 * save last key
267 static void set_last_key(HWND hwndTV)
269 HKEY hkey;
271 if (RegCreateKeyExW(HKEY_CURRENT_USER, wszKeyName, 0, NULL, 0, KEY_WRITE, NULL, &hkey, NULL) == ERROR_SUCCESS)
273 HTREEITEM selection = (HTREEITEM)SendMessageW(g_pChildWnd->hTreeWnd, TVM_GETNEXTITEM, TVGN_CARET, 0);
274 HTREEITEM root = (HTREEITEM)SendMessageW(g_pChildWnd->hTreeWnd, TVM_GETNEXTITEM, TVGN_ROOT, 0);
275 WCHAR *value;
277 if (selection == root)
278 value = g_pChildWnd->szPath;
279 else
280 value = GetItemFullPath(g_pChildWnd->hTreeWnd, selection, FALSE);
281 RegSetValueExW(hkey, wszLastKey, 0, REG_SZ, (LPBYTE)value, (lstrlenW(value) + 1) * sizeof(WCHAR));
282 if (selection != root)
283 heap_free(value);
284 RegCloseKey(hkey);
288 static int treeview_notify(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
290 switch (((NMHDR *)lParam)->code)
292 case NM_SETFOCUS:
293 g_pChildWnd->nFocusPanel = 0;
294 break;
295 case TVN_BEGINLABELEDITW:
297 HKEY hRootKey;
298 WCHAR *path;
300 if (!GetWindowLongPtrW(g_pChildWnd->hTreeWnd, GWLP_USERDATA))
301 return 1;
303 path = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hRootKey);
304 if (!path || !*path)
305 return 1;
306 return 0;
308 case TVN_ENDLABELEDITW:
310 HKEY hRootKey;
311 NMTVDISPINFOW *dispInfo = (NMTVDISPINFOW *)lParam;
312 WCHAR *path = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hRootKey);
313 BOOL res = RenameKey(hWnd, hRootKey, path, dispInfo->item.pszText);
315 heap_free(path);
317 if (res)
319 TVITEMW item;
321 item.mask = TVIF_HANDLE | TVIF_TEXT;
322 item.hItem = dispInfo->item.hItem;
323 item.pszText = dispInfo->item.pszText;
324 SendMessageW(g_pChildWnd->hTreeWnd, TVM_SETITEMW, 0, (LPARAM)&item);
326 path = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hRootKey);
327 update_listview_path(path);
328 heap_free(path);
330 UpdateStatusBar();
333 SetWindowLongPtrW(g_pChildWnd->hTreeWnd, GWLP_USERDATA, 0);
334 return res;
336 case TVN_ITEMEXPANDINGW:
337 return !OnTreeExpanding(g_pChildWnd->hTreeWnd, (NMTREEVIEWW *)lParam);
338 case TVN_SELCHANGEDW:
339 OnTreeSelectionChanged(g_pChildWnd->hTreeWnd, g_pChildWnd->hListWnd,
340 ((NMTREEVIEWW *)lParam)->itemNew.hItem, TRUE);
341 break;
343 return 0;
346 static int listview_notify(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
348 switch (((NMHDR *)lParam)->code)
350 case NM_DBLCLK:
352 NMITEMACTIVATE *nmitem = (NMITEMACTIVATE *)lParam;
354 if (nmitem->iItem != -1)
356 LVITEMW item;
358 item.state = 0;
359 item.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
360 SendMessageW(g_pChildWnd->hListWnd, LVM_SETITEMSTATE, (UINT)-1, (LPARAM)&item);
362 item.state = LVIS_FOCUSED | LVIS_SELECTED;
363 item.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
364 SendMessageW(g_pChildWnd->hListWnd, LVM_SETITEMSTATE, nmitem->iItem, (LPARAM)&item);
366 SendMessageW(hFrameWnd, WM_COMMAND, ID_EDIT_MODIFY, 0);
368 break;
370 case NM_RETURN:
372 int cnt = SendMessageW(g_pChildWnd->hListWnd, LVM_GETNEXTITEM, -1,
373 MAKELPARAM(LVNI_FOCUSED | LVNI_SELECTED, 0));
374 if (cnt != -1)
375 SendMessageW(hFrameWnd, WM_COMMAND, ID_EDIT_MODIFY, 0);
376 break;
378 case NM_SETFOCUS:
379 g_pChildWnd->nFocusPanel = 1;
380 break;
381 case LVN_BEGINLABELEDITW:
382 if (!((NMLVDISPINFOW *)lParam)->item.iItem)
383 return 1;
384 return 0;
385 case LVN_COLUMNCLICK:
386 if (g_columnToSort == ((NMLISTVIEW *)lParam)->iSubItem)
387 g_invertSort = !g_invertSort;
388 else
390 g_columnToSort = ((NMLISTVIEW *)lParam)->iSubItem;
391 g_invertSort = FALSE;
394 SendMessageW(g_pChildWnd->hListWnd, LVM_SORTITEMS,
395 (WPARAM)g_pChildWnd->hListWnd, (LPARAM)CompareFunc);
396 break;
397 case LVN_DELETEITEM:
399 NMLISTVIEW *nmlv = (NMLISTVIEW *)lParam;
400 LINE_INFO *info = (LINE_INFO *)nmlv->lParam;
402 heap_free(info->name);
403 heap_free(info->val);
404 heap_free(info);
405 break;
407 case LVN_ENDLABELEDITW:
409 NMLVDISPINFOW *dispInfo = (NMLVDISPINFOW *)lParam;
410 WCHAR *oldName = GetItemText(g_pChildWnd->hListWnd, dispInfo->item.iItem);
411 LONG ret;
413 if (!oldName) return -1; /* cannot rename a default value */
414 ret = RenameValue(g_pChildWnd->hListWnd, g_currentRootKey, g_currentPath,
415 oldName, dispInfo->item.pszText);
416 if (ret)
418 dispInfo->item.iSubItem = 0;
419 SendMessageW(g_pChildWnd->hListWnd, LVM_SETITEMTEXTW,
420 dispInfo->item.iItem, (LPARAM)&dispInfo->item);
423 heap_free(oldName);
424 return 0;
426 case LVN_GETDISPINFOW:
427 OnGetDispInfo((NMLVDISPINFOW *)lParam);
428 break;
430 return 0;
433 #define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
434 #define GET_Y_LPARAM(lp) ((int)(short)HIWORD(lp))
436 /*******************************************************************************
438 * FUNCTION: ChildWndProc(HWND, unsigned, WORD, LONG)
440 * PURPOSE: Processes messages for the child windows.
442 * WM_COMMAND - process the application menu
443 * WM_PAINT - Paint the main window
444 * WM_DESTROY - post a quit message and return
447 LRESULT CALLBACK ChildWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
449 switch (message) {
450 case WM_CREATE:
451 g_pChildWnd = heap_xalloc(sizeof(ChildWnd));
452 if (!g_pChildWnd) return 0;
453 LoadStringW(hInst, IDS_REGISTRY_ROOT_NAME, g_pChildWnd->szPath, MAX_PATH);
454 g_pChildWnd->nSplitPos = 250;
455 g_pChildWnd->hWnd = hWnd;
456 g_pChildWnd->hTreeWnd = CreateTreeView(hWnd, g_pChildWnd->szPath, TREE_WINDOW);
457 g_pChildWnd->hListWnd = CreateListView(hWnd, LIST_WINDOW/*, g_pChildWnd->szPath*/);
458 g_pChildWnd->nFocusPanel = 1;
459 SetFocus(g_pChildWnd->hTreeWnd);
460 get_last_key(g_pChildWnd->hTreeWnd);
461 break;
462 case WM_COMMAND:
463 if (!_CmdWndProc(hWnd, message, wParam, lParam)) {
464 goto def;
466 break;
467 case WM_PAINT:
468 OnPaint(hWnd);
469 return 0;
470 case WM_SETCURSOR:
471 if (LOWORD(lParam) == HTCLIENT) {
472 POINT pt;
473 GetCursorPos(&pt);
474 ScreenToClient(hWnd, &pt);
475 if (pt.x>=g_pChildWnd->nSplitPos-SPLIT_WIDTH/2 && pt.x<g_pChildWnd->nSplitPos+SPLIT_WIDTH/2+1) {
476 SetCursor(LoadCursorW(0, (LPCWSTR)IDC_SIZEWE));
477 return TRUE;
480 goto def;
481 case WM_DESTROY:
482 set_last_key(g_pChildWnd->hTreeWnd);
483 heap_free(g_pChildWnd);
484 g_pChildWnd = NULL;
485 PostQuitMessage(0);
486 break;
487 case WM_LBUTTONDOWN: {
488 RECT rt;
489 int x = (short)LOWORD(lParam);
490 GetClientRect(hWnd, &rt);
491 if (x>=g_pChildWnd->nSplitPos-SPLIT_WIDTH/2 && x<g_pChildWnd->nSplitPos+SPLIT_WIDTH/2+1) {
492 last_split = g_pChildWnd->nSplitPos;
493 draw_splitbar(hWnd, last_split);
494 SetCapture(hWnd);
496 break;
499 /* WM_RBUTTONDOWN sets the splitbar the same way as WM_LBUTTONUP */
500 case WM_LBUTTONUP:
501 case WM_RBUTTONDOWN:
502 if (GetCapture() == hWnd) {
503 finish_splitbar(hWnd, LOWORD(lParam));
505 break;
507 case WM_CAPTURECHANGED:
508 if (GetCapture()==hWnd && last_split>=0)
509 draw_splitbar(hWnd, last_split);
510 break;
512 case WM_CONTEXTMENU:
514 POINT pt = {GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)};
515 short int menu_id = -1;
517 if ((HWND)wParam == g_pChildWnd->hTreeWnd)
519 TVHITTESTINFO ht;
521 ht.pt = pt;
522 ScreenToClient(g_pChildWnd->hTreeWnd, &ht.pt);
524 if (SendMessageW(g_pChildWnd->hTreeWnd, TVM_HITTEST, 0, (LPARAM)&ht))
526 HTREEITEM root;
528 SendMessageW(g_pChildWnd->hTreeWnd, TVM_SELECTITEM, TVGN_CARET, (LPARAM)ht.hItem);
529 root = (HTREEITEM)SendMessageW(g_pChildWnd->hTreeWnd, TVM_GETNEXTITEM, TVGN_ROOT, 0);
530 menu_id = (ht.hItem == root) ? PM_COMPUTER : PM_TREEVIEW;
533 else
535 int sel = SendMessageW(g_pChildWnd->hListWnd, LVM_GETNEXTITEM, -1,
536 MAKELPARAM(LVNI_SELECTED, 0));
537 menu_id = (sel == -1) ? PM_NEW_VALUE : PM_MODIFY_VALUE;
540 TrackPopupMenu(GetSubMenu(hPopupMenus, menu_id), TPM_RIGHTBUTTON,
541 pt.x, pt.y, 0, hFrameWnd, NULL);
542 break;
545 case WM_KEYDOWN:
546 if (wParam == VK_ESCAPE)
547 if (GetCapture() == hWnd) {
548 RECT rt;
549 draw_splitbar(hWnd, last_split);
550 GetClientRect(hWnd, &rt);
551 ResizeWnd(rt.right, rt.bottom);
552 last_split = -1;
553 ReleaseCapture();
554 SetCursor(LoadCursorW(0, (LPCWSTR)IDC_ARROW));
556 break;
558 case WM_MOUSEMOVE:
559 if (GetCapture() == hWnd) {
560 RECT rt;
561 int x = LOWORD(lParam);
562 HDC hdc = GetDC(hWnd);
563 GetClientRect(hWnd, &rt);
564 rt.left = last_split-SPLIT_WIDTH/2;
565 rt.right = last_split+SPLIT_WIDTH/2+1;
566 InvertRect(hdc, &rt);
567 last_split = x;
568 rt.left = x-SPLIT_WIDTH/2;
569 rt.right = x+SPLIT_WIDTH/2+1;
570 InvertRect(hdc, &rt);
571 ReleaseDC(hWnd, hdc);
573 break;
575 case WM_SETFOCUS:
576 if (g_pChildWnd != NULL) {
577 SetFocus(g_pChildWnd->nFocusPanel? g_pChildWnd->hListWnd: g_pChildWnd->hTreeWnd);
579 break;
581 case WM_TIMER:
582 break;
584 case WM_NOTIFY:
585 if (wParam == TREE_WINDOW && g_pChildWnd)
586 return treeview_notify(hWnd, message, wParam, lParam);
587 else if (wParam == LIST_WINDOW && g_pChildWnd)
588 return listview_notify(hWnd, message, wParam, lParam);
589 break;
591 case WM_SIZE:
592 if (wParam != SIZE_MINIMIZED && g_pChildWnd != NULL) {
593 ResizeWnd(LOWORD(lParam), HIWORD(lParam));
595 /* fall through */
596 default: def:
597 return DefWindowProcW(hWnd, message, wParam, lParam);
599 return 0;