Sync to trunk (r46918)
[reactos.git] / base / applications / regedit / childwnd.c
1 /*
2 * Regedit child window
3 *
4 * Copyright (C) 2002 Robert Dickenson <robd@reactos.org>
5 *
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.
10 *
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.
15 *
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 Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <regedit.h>
22
23 ChildWnd* g_pChildWnd;
24 static int last_split;
25 HBITMAP SizingPattern = 0;
26 HBRUSH SizingBrush = 0;
27 static TCHAR Suggestions[256];
28
29 /*******************************************************************************
30 * Local module support methods
31 */
32
33 static LPCTSTR get_root_key_name(HKEY hRootKey)
34 {
35 if (hRootKey == HKEY_CLASSES_ROOT) return _T("HKEY_CLASSES_ROOT");
36 if (hRootKey == HKEY_CURRENT_USER) return _T("HKEY_CURRENT_USER");
37 if (hRootKey == HKEY_LOCAL_MACHINE) return _T("HKEY_LOCAL_MACHINE");
38 if (hRootKey == HKEY_USERS) return _T("HKEY_USERS");
39 if (hRootKey == HKEY_CURRENT_CONFIG) return _T("HKEY_CURRENT_CONFIG");
40 if (hRootKey == HKEY_DYN_DATA) return _T("HKEY_DYN_DATA");
41 return _T("UKNOWN HKEY, PLEASE REPORT");
42 }
43
44 static void draw_splitbar(HWND hWnd, int x)
45 {
46 RECT rt;
47 HGDIOBJ OldObj;
48 HDC hdc = GetDC(hWnd);
49
50 if(!SizingPattern)
51 {
52 const DWORD Pattern[4] = {0x5555AAAA, 0x5555AAAA, 0x5555AAAA, 0x5555AAAA};
53 SizingPattern = CreateBitmap(8, 8, 1, 1, Pattern);
54 }
55 if(!SizingBrush)
56 {
57 SizingBrush = CreatePatternBrush(SizingPattern);
58 }
59 GetClientRect(hWnd, &rt);
60 rt.left = x - SPLIT_WIDTH/2;
61 rt.right = x + SPLIT_WIDTH/2+1;
62 OldObj = SelectObject(hdc, SizingBrush);
63 PatBlt(hdc, rt.left, rt.top, rt.right - rt.left, rt.bottom - rt.top, PATINVERT);
64 SelectObject(hdc, OldObj);
65 ReleaseDC(hWnd, hdc);
66 }
67
68 static void ResizeWnd(ChildWnd* pChildWnd, int cx, int cy)
69 {
70 HDWP hdwp = BeginDeferWindowPos(2);
71 RECT rt, rs;
72
73 SetRect(&rt, 0, 0, cx, cy);
74 cy = 0;
75 if (hStatusBar != NULL) {
76 GetWindowRect(hStatusBar, &rs);
77 cy = rs.bottom - rs.top + 8;
78 }
79 cx = pChildWnd->nSplitPos + SPLIT_WIDTH/2;
80 DeferWindowPos(hdwp, pChildWnd->hAddressBarWnd, 0, rt.left, rt.top, rt.right-rt.left, 23, SWP_NOZORDER|SWP_NOACTIVATE);
81 DeferWindowPos(hdwp, pChildWnd->hTreeWnd, 0, rt.left, rt.top + 25, pChildWnd->nSplitPos-SPLIT_WIDTH/2-rt.left, rt.bottom-rt.top-cy, SWP_NOZORDER|SWP_NOACTIVATE);
82 DeferWindowPos(hdwp, pChildWnd->hListWnd, 0, rt.left+cx , rt.top + 25, rt.right-cx, rt.bottom-rt.top-cy, SWP_NOZORDER|SWP_NOACTIVATE);
83 EndDeferWindowPos(hdwp);
84 }
85
86 static void OnPaint(HWND hWnd)
87 {
88 PAINTSTRUCT ps;
89 RECT rt;
90
91 GetClientRect(hWnd, &rt);
92 BeginPaint(hWnd, &ps);
93 FillRect(ps.hdc, &rt, GetSysColorBrush(COLOR_BTNFACE));
94 EndPaint(hWnd, &ps);
95 }
96
97 /*******************************************************************************
98 * finish_splitbar [internal]
99 *
100 * make the splitbar invisible and resize the windows
101 * (helper for ChildWndProc)
102 */
103 static void finish_splitbar(HWND hWnd, int x)
104 {
105 RECT rt;
106
107 draw_splitbar(hWnd, last_split);
108 last_split = -1;
109 GetClientRect(hWnd, &rt);
110 g_pChildWnd->nSplitPos = x;
111 ResizeWnd(g_pChildWnd, rt.right, rt.bottom);
112 ReleaseCapture();
113 }
114
115 /*******************************************************************************
116 *
117 * FUNCTION: _CmdWndProc(HWND, unsigned, WORD, LONG)
118 *
119 * PURPOSE: Processes WM_COMMAND messages for the main frame window.
120 *
121 */
122
123 static BOOL _CmdWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
124 {
125 ChildWnd* pChildWnd = g_pChildWnd;
126 HTREEITEM hSelection;
127 HKEY hRootKey;
128 LPCTSTR keyPath, s;
129 WORD wID = LOWORD(wParam);
130
131 UNREFERENCED_PARAMETER(message);
132
133 switch (wID) {
134 /* Parse the menu selections: */
135 case ID_REGISTRY_EXIT:
136 DestroyWindow(hWnd);
137 break;
138 case ID_VIEW_REFRESH:
139 /* TODO */
140 break;
141 case ID_TREE_EXPANDBRANCH:
142 (void)TreeView_Expand(pChildWnd->hTreeWnd, TreeView_GetSelection(pChildWnd->hTreeWnd), TVE_EXPAND);
143 break;
144 case ID_TREE_COLLAPSEBRANCH:
145 (void)TreeView_Expand(pChildWnd->hTreeWnd, TreeView_GetSelection(pChildWnd->hTreeWnd), TVE_COLLAPSE);
146 break;
147 case ID_TREE_RENAME:
148 SetFocus(pChildWnd->hTreeWnd);
149 (void)TreeView_EditLabel(pChildWnd->hTreeWnd, TreeView_GetSelection(pChildWnd->hTreeWnd));
150 break;
151 case ID_TREE_DELETE:
152 hSelection = TreeView_GetSelection(pChildWnd->hTreeWnd);
153 keyPath = GetItemPath(pChildWnd->hTreeWnd, hSelection, &hRootKey);
154
155 if (keyPath == 0 || *keyPath == 0)
156 {
157 MessageBeep(MB_ICONHAND);
158 } else
159 if (DeleteKey(hWnd, hRootKey, keyPath))
160 DeleteNode(g_pChildWnd->hTreeWnd, 0);
161 break;
162 case ID_TREE_EXPORT:
163 ExportRegistryFile(pChildWnd->hTreeWnd);
164 break;
165 case ID_EDIT_FIND:
166 FindDialog(hWnd);
167 break;
168 case ID_EDIT_COPYKEYNAME:
169 hSelection = TreeView_GetSelection(pChildWnd->hTreeWnd);
170 keyPath = GetItemPath(pChildWnd->hTreeWnd, hSelection, &hRootKey);
171 CopyKeyName(hWnd, hRootKey, keyPath);
172 break;
173 case ID_EDIT_NEW_KEY:
174 CreateNewKey(pChildWnd->hTreeWnd, TreeView_GetSelection(pChildWnd->hTreeWnd));
175 break;
176 case ID_EDIT_NEW_STRINGVALUE:
177 case ID_EDIT_NEW_BINARYVALUE:
178 case ID_EDIT_NEW_DWORDVALUE:
179 SendMessage(hFrameWnd, WM_COMMAND, wParam, lParam);
180 break;
181 case ID_SWITCH_PANELS:
182 pChildWnd->nFocusPanel = !pChildWnd->nFocusPanel;
183 SetFocus(pChildWnd->nFocusPanel? pChildWnd->hListWnd: pChildWnd->hTreeWnd);
184 break;
185 default:
186 if ((wID >= ID_TREE_SUGGESTION_MIN) && (wID <= ID_TREE_SUGGESTION_MAX))
187 {
188 s = Suggestions;
189 while(wID > ID_TREE_SUGGESTION_MIN)
190 {
191 if (*s)
192 s += _tcslen(s) + 1;
193 wID--;
194 }
195 SelectNode(pChildWnd->hTreeWnd, s);
196 break;
197 }
198 return FALSE;
199 }
200 return TRUE;
201 }
202
203 /*******************************************************************************
204 *
205 * Key suggestion
206 */
207
208 #define MIN(a,b) ((a < b) ? (a) : (b))
209
210 static void SuggestKeys(HKEY hRootKey, LPCTSTR pszKeyPath, LPTSTR pszSuggestions,
211 size_t iSuggestionsLength)
212 {
213 TCHAR szBuffer[256];
214 TCHAR szLastFound[256];
215 size_t i;
216 HKEY hOtherKey, hSubKey;
217 BOOL bFound;
218
219 memset(pszSuggestions, 0, iSuggestionsLength * sizeof(*pszSuggestions));
220 iSuggestionsLength--;
221
222 /* Are we a root key in HKEY_CLASSES_ROOT? */
223 if ((hRootKey == HKEY_CLASSES_ROOT) && pszKeyPath[0] && !_tcschr(pszKeyPath, TEXT('\\')))
224 {
225 do
226 {
227 bFound = FALSE;
228
229 /* Check default key */
230 if (RegQueryStringValue(hRootKey, pszKeyPath, NULL,
231 szBuffer, sizeof(szBuffer) / sizeof(szBuffer[0])) == ERROR_SUCCESS)
232 {
233 /* Sanity check this key; it cannot be empty, nor can it be a
234 * loop back */
235 if ((szBuffer[0] != '\0') && _tcsicmp(szBuffer, pszKeyPath))
236 {
237 if (RegOpenKey(hRootKey, szBuffer, &hOtherKey) == ERROR_SUCCESS)
238 {
239 lstrcpyn(pszSuggestions, TEXT("HKCR\\"), (int) iSuggestionsLength);
240 i = _tcslen(pszSuggestions);
241 pszSuggestions += i;
242 iSuggestionsLength -= i;
243
244 lstrcpyn(pszSuggestions, szBuffer, (int) iSuggestionsLength);
245 i = MIN(_tcslen(pszSuggestions) + 1, iSuggestionsLength);
246 pszSuggestions += i;
247 iSuggestionsLength -= i;
248 RegCloseKey(hOtherKey);
249
250 bFound = TRUE;
251 _tcscpy(szLastFound, szBuffer);
252 pszKeyPath = szLastFound;
253 }
254 }
255 }
256 }
257 while(bFound && (iSuggestionsLength > 0));
258
259 /* Check CLSID key */
260 if (RegOpenKey(hRootKey, pszKeyPath, &hSubKey) == ERROR_SUCCESS)
261 {
262 if (RegQueryStringValue(hSubKey, TEXT("CLSID"), NULL,
263 szBuffer, sizeof(szBuffer) / sizeof(szBuffer[0])) == ERROR_SUCCESS)
264 {
265 lstrcpyn(pszSuggestions, TEXT("HKCR\\CLSID\\"), (int) iSuggestionsLength);
266 i = _tcslen(pszSuggestions);
267 pszSuggestions += i;
268 iSuggestionsLength -= i;
269
270 lstrcpyn(pszSuggestions, szBuffer, (int) iSuggestionsLength);
271 i = MIN(_tcslen(pszSuggestions) + 1, iSuggestionsLength);
272 pszSuggestions += i;
273 iSuggestionsLength -= i;
274 }
275 RegCloseKey(hSubKey);
276 }
277 }
278 }
279
280
281 LRESULT CALLBACK AddressBarProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
282 {
283 WNDPROC oldwndproc;
284 static TCHAR s_szNode[256];
285 oldwndproc = (WNDPROC)(LONG_PTR)GetWindowLongPtr(hwnd, GWL_USERDATA);
286
287 switch (uMsg)
288 {
289 case WM_KEYUP:
290 if (wParam == VK_RETURN)
291 {
292 GetWindowText(hwnd, s_szNode, sizeof(s_szNode) / sizeof(s_szNode[0]));
293 SelectNode(g_pChildWnd->hTreeWnd, s_szNode);
294 }
295 break;
296 default:
297 break;
298 }
299 return CallWindowProc(oldwndproc, hwnd, uMsg, wParam, lParam);
300 }
301
302 /* fix coords to top-left when SHIFT-F10 is pressed */
303 void FixPointIfContext(POINTS *pt, HWND hWnd)
304 {
305 if (pt->x == -1 && pt->y == -1) {
306 POINT p = { 0, 0 };
307 ClientToScreen(hWnd, &p);
308 pt->x = (WORD)(p.x);
309 pt->y = (WORD)(p.y);
310 }
311 }
312
313 /*******************************************************************************
314 *
315 * FUNCTION: ChildWndProc(HWND, unsigned, WORD, LONG)
316 *
317 * PURPOSE: Processes messages for the child windows.
318 *
319 * WM_COMMAND - process the application menu
320 * WM_PAINT - Paint the main window
321 * WM_DESTROY - post a quit message and return
322 *
323 */
324 LRESULT CALLBACK ChildWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
325 {
326 BOOL Result;
327 ChildWnd* pChildWnd = g_pChildWnd;
328
329 switch (message) {
330 case WM_CREATE:
331 {
332 WNDPROC oldproc;
333 HFONT hFont;
334 TCHAR buffer[MAX_PATH];
335 /* load "My Computer" string */
336 LoadString(hInst, IDS_MY_COMPUTER, buffer, sizeof(buffer)/sizeof(TCHAR));
337
338 g_pChildWnd = pChildWnd = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ChildWnd));
339
340 if (!pChildWnd) return 0;
341 _tcsncpy(pChildWnd->szPath, buffer, MAX_PATH);
342 pChildWnd->nSplitPos = 250;
343 pChildWnd->hWnd = hWnd;
344 pChildWnd->hAddressBarWnd = CreateWindowEx(WS_EX_CLIENTEDGE, _T("Edit"), NULL, WS_CHILD | WS_VISIBLE | WS_CHILDWINDOW | WS_TABSTOP,
345 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
346 hWnd, (HMENU)0, hInst, 0);
347 pChildWnd->hTreeWnd = CreateTreeView(hWnd, pChildWnd->szPath, (HMENU) TREE_WINDOW);
348 pChildWnd->hListWnd = CreateListView(hWnd, (HMENU) LIST_WINDOW/*, pChildWnd->szPath*/);
349 SetFocus(pChildWnd->hTreeWnd);
350
351 /* set the address bar font */
352 if (pChildWnd->hAddressBarWnd)
353 {
354 hFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
355 SendMessage(pChildWnd->hAddressBarWnd,
356 WM_SETFONT,
357 (WPARAM)hFont,
358 0);
359 }
360
361 /* Subclass the AddressBar */
362 oldproc = (WNDPROC)(LONG_PTR)GetWindowLongPtr(pChildWnd->hAddressBarWnd, GWL_WNDPROC);
363 SetWindowLongPtr(pChildWnd->hAddressBarWnd, GWL_USERDATA, (DWORD_PTR)oldproc);
364 SetWindowLongPtr(pChildWnd->hAddressBarWnd, GWL_WNDPROC, (DWORD_PTR)AddressBarProc);
365 break;
366 }
367 case WM_COMMAND:
368 if (!_CmdWndProc(hWnd, message, wParam, lParam)) {
369 goto def;
370 }
371 break;
372 case WM_PAINT:
373 OnPaint(hWnd);
374 return 0;
375 case WM_SETCURSOR:
376 if (LOWORD(lParam) == HTCLIENT) {
377 POINT pt;
378 GetCursorPos(&pt);
379 ScreenToClient(hWnd, &pt);
380 if (pt.x>=pChildWnd->nSplitPos-SPLIT_WIDTH/2 && pt.x<pChildWnd->nSplitPos+SPLIT_WIDTH/2+1) {
381 SetCursor(LoadCursor(0, IDC_SIZEWE));
382 return TRUE;
383 }
384 }
385 goto def;
386 case WM_DESTROY:
387 DestroyTreeView();
388 DestroyListView(pChildWnd->hListWnd);
389 DestroyMainMenu();
390 HeapFree(GetProcessHeap(), 0, pChildWnd);
391 pChildWnd = NULL;
392 PostQuitMessage(0);
393 break;
394 case WM_LBUTTONDOWN: {
395 RECT rt;
396 int x = (short)LOWORD(lParam);
397 GetClientRect(hWnd, &rt);
398 if (x>=pChildWnd->nSplitPos-SPLIT_WIDTH/2 && x<pChildWnd->nSplitPos+SPLIT_WIDTH/2+1) {
399 last_split = pChildWnd->nSplitPos;
400 draw_splitbar(hWnd, last_split);
401 SetCapture(hWnd);
402 }
403 break;
404 }
405
406 case WM_LBUTTONUP:
407 case WM_RBUTTONDOWN:
408 if (GetCapture() == hWnd) {
409 finish_splitbar(hWnd, LOWORD(lParam));
410 }
411 break;
412
413 case WM_CAPTURECHANGED:
414 if (GetCapture()==hWnd && last_split>=0)
415 draw_splitbar(hWnd, last_split);
416 break;
417
418 case WM_KEYDOWN:
419 if (wParam == VK_ESCAPE)
420 if (GetCapture() == hWnd) {
421 RECT rt;
422 draw_splitbar(hWnd, last_split);
423 GetClientRect(hWnd, &rt);
424 ResizeWnd(pChildWnd, rt.right, rt.bottom);
425 last_split = -1;
426 ReleaseCapture();
427 SetCursor(LoadCursor(0, IDC_ARROW));
428 }
429 break;
430
431 case WM_MOUSEMOVE:
432 if (GetCapture() == hWnd) {
433 HDC hdc;
434 RECT rt;
435 HGDIOBJ OldObj;
436 int x = LOWORD(lParam);
437 if(!SizingPattern)
438 {
439 const DWORD Pattern[4] = {0x5555AAAA, 0x5555AAAA, 0x5555AAAA, 0x5555AAAA};
440 SizingPattern = CreateBitmap(8, 8, 1, 1, Pattern);
441 }
442 if(!SizingBrush)
443 {
444 SizingBrush = CreatePatternBrush(SizingPattern);
445 }
446
447 GetClientRect(hWnd, &rt);
448 x = (SHORT) min(max(x, SPLIT_MIN), rt.right - SPLIT_MIN);
449 if(last_split != x)
450 {
451 rt.left = last_split-SPLIT_WIDTH/2;
452 rt.right = last_split+SPLIT_WIDTH/2+1;
453 hdc = GetDC(hWnd);
454 OldObj = SelectObject(hdc, SizingBrush);
455 PatBlt(hdc, rt.left, rt.top, rt.right - rt.left, rt.bottom - rt.top, PATINVERT);
456 last_split = x;
457 rt.left = x-SPLIT_WIDTH/2;
458 rt.right = x+SPLIT_WIDTH/2+1;
459 PatBlt(hdc, rt.left, rt.top, rt.right - rt.left, rt.bottom - rt.top, PATINVERT);
460 SelectObject(hdc, OldObj);
461 ReleaseDC(hWnd, hdc);
462 }
463 }
464 break;
465
466 case WM_SETFOCUS:
467 if (pChildWnd != NULL) {
468 SetFocus(pChildWnd->nFocusPanel? pChildWnd->hListWnd: pChildWnd->hTreeWnd);
469 }
470 break;
471
472 case WM_TIMER:
473 break;
474
475 case WM_NOTIFY:
476 if ((int)wParam == TREE_WINDOW) {
477 switch (((LPNMHDR)lParam)->code) {
478 case TVN_ITEMEXPANDING:
479 return !OnTreeExpanding(pChildWnd->hTreeWnd, (NMTREEVIEW*)lParam);
480 case TVN_SELCHANGED: {
481 LPCTSTR keyPath, rootName;
482 LPTSTR fullPath;
483 HKEY hRootKey;
484
485 keyPath = GetItemPath(pChildWnd->hTreeWnd, ((NMTREEVIEW*)lParam)->itemNew.hItem, &hRootKey);
486 if (keyPath) {
487 RefreshListView(pChildWnd->hListWnd, hRootKey, keyPath);
488 rootName = get_root_key_name(hRootKey);
489 fullPath = HeapAlloc(GetProcessHeap(), 0, (_tcslen(rootName) + 1 + _tcslen(keyPath) + 1) * sizeof(TCHAR));
490 if (fullPath) {
491 _stprintf(fullPath, _T("%s\\%s"), rootName, keyPath);
492 SendMessage(hStatusBar, SB_SETTEXT, 0, (LPARAM)fullPath);
493 SendMessage(pChildWnd->hAddressBarWnd, WM_SETTEXT, 0, (LPARAM)fullPath);
494 HeapFree(GetProcessHeap(), 0, fullPath);
495
496 {
497 HKEY hKey;
498 TCHAR szBuffer[MAX_PATH];
499 _sntprintf(szBuffer, sizeof(szBuffer) / sizeof(szBuffer[0]), _T("My Computer\\%s\\%s"), rootName, keyPath);
500
501 if (RegCreateKey(HKEY_CURRENT_USER,
502 g_szGeneralRegKey,
503 &hKey) == ERROR_SUCCESS)
504 {
505 RegSetValueEx(hKey, _T("LastKey"), 0, REG_SZ, (LPBYTE) szBuffer, (DWORD) _tcslen(szBuffer) * sizeof(szBuffer[0]));
506 RegCloseKey(hKey);
507 }
508 }
509 }
510 }
511 }
512 break;
513 case NM_SETFOCUS:
514 pChildWnd->nFocusPanel = 0;
515 break;
516 case TVN_BEGINLABELEDIT:
517 {
518 LPNMTVDISPINFO ptvdi;
519 /* cancel label edit for rootkeys */
520 ptvdi = (LPNMTVDISPINFO) lParam;
521 if (!TreeView_GetParent(pChildWnd->hTreeWnd, ptvdi->item.hItem) ||
522 !TreeView_GetParent(pChildWnd->hTreeWnd, TreeView_GetParent(pChildWnd->hTreeWnd, ptvdi->item.hItem)))
523 return TRUE;
524 break;
525 }
526 case TVN_ENDLABELEDIT:
527 {
528 LPCTSTR keyPath;
529 HKEY hRootKey;
530 HKEY hKey = NULL;
531 LPNMTVDISPINFO ptvdi;
532 LONG lResult = TRUE;
533 TCHAR szBuffer[MAX_PATH];
534
535 ptvdi = (LPNMTVDISPINFO) lParam;
536 if (ptvdi->item.pszText)
537 {
538 keyPath = GetItemPath(pChildWnd->hTreeWnd, TreeView_GetParent(pChildWnd->hTreeWnd, ptvdi->item.hItem), &hRootKey);
539 _sntprintf(szBuffer, sizeof(szBuffer) / sizeof(szBuffer[0]), _T("%s\\%s"), keyPath, ptvdi->item.pszText);
540 keyPath = GetItemPath(pChildWnd->hTreeWnd, ptvdi->item.hItem, &hRootKey);
541 if (RegOpenKeyEx(hRootKey, szBuffer, 0, KEY_READ, &hKey) == ERROR_SUCCESS)
542 {
543 lResult = FALSE;
544 RegCloseKey(hKey);
545 (void)TreeView_EditLabel(pChildWnd->hTreeWnd, ptvdi->item.hItem);
546 }
547 else
548 {
549 if (RegRenameKey(hRootKey, keyPath, ptvdi->item.pszText) != ERROR_SUCCESS)
550 lResult = FALSE;
551 }
552 return lResult;
553 }
554 }
555 default:
556 return 0;
557 }
558 } else
559 {
560 if ((int)wParam == LIST_WINDOW)
561 {
562 switch (((LPNMHDR)lParam)->code) {
563 case NM_SETFOCUS:
564 pChildWnd->nFocusPanel = 1;
565 break;
566 default:
567 if(!ListWndNotifyProc(pChildWnd->hListWnd, wParam, lParam, &Result))
568 {
569 goto def;
570 }
571 return Result;
572 break;
573 }
574 }
575 }
576 break;
577
578 case WM_CONTEXTMENU:
579 {
580 POINTS pt;
581 if((HWND)wParam == pChildWnd->hListWnd)
582 {
583 int i, cnt;
584 BOOL IsDefault;
585 pt.x = LOWORD(lParam);
586 pt.y = HIWORD(lParam);
587 cnt = ListView_GetSelectedCount(pChildWnd->hListWnd);
588 i = ListView_GetNextItem(pChildWnd->hListWnd, -1, LVNI_FOCUSED | LVNI_SELECTED);
589 FixPointIfContext(&pt, pChildWnd->hListWnd);
590 if(i == -1)
591 {
592 TrackPopupMenu(GetSubMenu(hPopupMenus, PM_NEW), TPM_RIGHTBUTTON, pt.x, pt.y, 0, hFrameWnd, NULL);
593 }
594 else
595 {
596 HMENU mnu = GetSubMenu(hPopupMenus, PM_MODIFYVALUE);
597 SetMenuDefaultItem(mnu, ID_EDIT_MODIFY, MF_BYCOMMAND);
598 IsDefault = IsDefaultValue(pChildWnd->hListWnd, i);
599 if(cnt == 1)
600 EnableMenuItem(mnu, ID_EDIT_RENAME, MF_BYCOMMAND | (IsDefault ? MF_DISABLED | MF_GRAYED : MF_ENABLED));
601 else
602 EnableMenuItem(mnu, ID_EDIT_RENAME, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
603 EnableMenuItem(mnu, ID_EDIT_MODIFY, MF_BYCOMMAND | (cnt == 1 ? MF_ENABLED : MF_DISABLED | MF_GRAYED));
604 EnableMenuItem(mnu, ID_EDIT_MODIFY_BIN, MF_BYCOMMAND | (cnt == 1 ? MF_ENABLED : MF_DISABLED | MF_GRAYED));
605
606 TrackPopupMenu(mnu, TPM_RIGHTBUTTON, pt.x, pt.y, 0, hFrameWnd, NULL);
607 }
608 }
609 else if ((HWND)wParam == pChildWnd->hTreeWnd)
610 {
611 TVHITTESTINFO hti;
612 HMENU hContextMenu;
613 TVITEM item;
614 MENUITEMINFO mii;
615 TCHAR resource[256];
616 TCHAR buffer[256];
617 LPTSTR s;
618 LPCTSTR keyPath;
619 HKEY hRootKey;
620 int iLastPos;
621 WORD wID;
622
623 pt.x = LOWORD(lParam);
624 pt.y = HIWORD(lParam);
625 hti.pt.x = pt.x;
626 hti.pt.y = pt.y;
627 ScreenToClient(pChildWnd->hTreeWnd, &hti.pt);
628 (void)TreeView_HitTest(pChildWnd->hTreeWnd, &hti);
629
630 if ((hti.flags & TVHT_ONITEM) != 0 || (pt.x == -1 && pt.y == -1))
631 {
632 hContextMenu = GetSubMenu(hPopupMenus, PM_TREECONTEXT);
633 (void)TreeView_SelectItem(pChildWnd->hTreeWnd, hti.hItem);
634
635 memset(&item, 0, sizeof(item));
636 item.mask = TVIF_STATE | TVIF_CHILDREN;
637 item.hItem = hti.hItem;
638 (void)TreeView_GetItem(pChildWnd->hTreeWnd, &item);
639
640 /* Set the Expand/Collapse menu item appropriately */
641 LoadString(hInst, (item.state & TVIS_EXPANDED) ? IDS_COLLAPSE : IDS_EXPAND, buffer, sizeof(buffer) / sizeof(buffer[0]));
642 memset(&mii, 0, sizeof(mii));
643 mii.cbSize = sizeof(mii);
644 mii.fMask = MIIM_STRING | MIIM_STATE | MIIM_ID;
645 mii.fState = (item.cChildren > 0) ? MFS_DEFAULT : MFS_GRAYED;
646 mii.wID = (item.state & TVIS_EXPANDED) ? ID_TREE_COLLAPSEBRANCH : ID_TREE_EXPANDBRANCH;
647 mii.dwTypeData = (LPTSTR) buffer;
648 SetMenuItemInfo(hContextMenu, 0, TRUE, &mii);
649
650 /* Remove any existing suggestions */
651 memset(&mii, 0, sizeof(mii));
652 mii.cbSize = sizeof(mii);
653 mii.fMask = MIIM_ID;
654 GetMenuItemInfo(hContextMenu, GetMenuItemCount(hContextMenu) - 1, TRUE, &mii);
655 if ((mii.wID >= ID_TREE_SUGGESTION_MIN) && (mii.wID <= ID_TREE_SUGGESTION_MAX))
656 {
657 do
658 {
659 iLastPos = GetMenuItemCount(hContextMenu) - 1;
660 GetMenuItemInfo(hContextMenu, iLastPos, TRUE, &mii);
661 RemoveMenu(hContextMenu, iLastPos, MF_BYPOSITION);
662 }
663 while((mii.wID >= ID_TREE_SUGGESTION_MIN) && (mii.wID <= ID_TREE_SUGGESTION_MAX));
664 }
665
666 /* Come up with suggestions */
667 keyPath = GetItemPath(pChildWnd->hTreeWnd, NULL, &hRootKey);
668 SuggestKeys(hRootKey, keyPath, Suggestions, sizeof(Suggestions) / sizeof(Suggestions[0]));
669 if (Suggestions[0])
670 {
671 AppendMenu(hContextMenu, MF_SEPARATOR, 0, NULL);
672
673 LoadString(hInst, IDS_GOTO_SUGGESTED_KEY, resource, sizeof(resource) / sizeof(resource[0]));
674
675 s = Suggestions;
676 wID = ID_TREE_SUGGESTION_MIN;
677 while(*s && (wID <= ID_TREE_SUGGESTION_MAX))
678 {
679 _sntprintf(buffer, sizeof(buffer) / sizeof(buffer[0]), resource, s);
680
681 memset(&mii, 0, sizeof(mii));
682 mii.cbSize = sizeof(mii);
683 mii.fMask = MIIM_STRING | MIIM_ID;
684 mii.wID = wID++;
685 mii.dwTypeData = buffer;
686 InsertMenuItem(hContextMenu, GetMenuItemCount(hContextMenu), TRUE, &mii);
687
688 s += _tcslen(s) + 1;
689 }
690 }
691 FixPointIfContext(&pt, pChildWnd->hTreeWnd);
692 TrackPopupMenu(hContextMenu, TPM_RIGHTBUTTON, pt.x, pt.y, 0, pChildWnd->hWnd, NULL);
693 }
694 }
695 break;
696 }
697
698 case WM_SIZE:
699 if (wParam != SIZE_MINIMIZED && pChildWnd != NULL) {
700 ResizeWnd(pChildWnd, LOWORD(lParam), HIWORD(lParam));
701 }
702 /* fall through */
703 default: def:
704 return DefWindowProc(hWnd, message, wParam, lParam);
705 }
706 return 0;
707 }