Sync with trunk (r48123)
[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 /*******************************************************************************
303 *
304 * FUNCTION: ChildWndProc(HWND, unsigned, WORD, LONG)
305 *
306 * PURPOSE: Processes messages for the child windows.
307 *
308 * WM_COMMAND - process the application menu
309 * WM_PAINT - Paint the main window
310 * WM_DESTROY - post a quit message and return
311 *
312 */
313 LRESULT CALLBACK ChildWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
314 {
315 BOOL Result;
316 ChildWnd* pChildWnd = g_pChildWnd;
317
318 switch (message) {
319 case WM_CREATE:
320 {
321 WNDPROC oldproc;
322 HFONT hFont;
323 TCHAR buffer[MAX_PATH];
324 /* load "My Computer" string */
325 LoadString(hInst, IDS_MY_COMPUTER, buffer, sizeof(buffer)/sizeof(TCHAR));
326
327 g_pChildWnd = pChildWnd = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ChildWnd));
328
329 if (!pChildWnd) return 0;
330 _tcsncpy(pChildWnd->szPath, buffer, MAX_PATH);
331 pChildWnd->nSplitPos = 250;
332 pChildWnd->hWnd = hWnd;
333 pChildWnd->hAddressBarWnd = CreateWindowEx(WS_EX_CLIENTEDGE, _T("Edit"), NULL, WS_CHILD | WS_VISIBLE | WS_CHILDWINDOW | WS_TABSTOP,
334 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
335 hWnd, (HMENU)0, hInst, 0);
336 pChildWnd->hTreeWnd = CreateTreeView(hWnd, pChildWnd->szPath, (HMENU) TREE_WINDOW);
337 pChildWnd->hListWnd = CreateListView(hWnd, (HMENU) LIST_WINDOW/*, pChildWnd->szPath*/);
338 SetFocus(pChildWnd->hTreeWnd);
339
340 /* set the address bar font */
341 if (pChildWnd->hAddressBarWnd)
342 {
343 hFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
344 SendMessage(pChildWnd->hAddressBarWnd,
345 WM_SETFONT,
346 (WPARAM)hFont,
347 0);
348 }
349
350 /* Subclass the AddressBar */
351 oldproc = (WNDPROC)(LONG_PTR)GetWindowLongPtr(pChildWnd->hAddressBarWnd, GWL_WNDPROC);
352 SetWindowLongPtr(pChildWnd->hAddressBarWnd, GWL_USERDATA, (DWORD_PTR)oldproc);
353 SetWindowLongPtr(pChildWnd->hAddressBarWnd, GWL_WNDPROC, (DWORD_PTR)AddressBarProc);
354 break;
355 }
356 case WM_COMMAND:
357 if (!_CmdWndProc(hWnd, message, wParam, lParam)) {
358 goto def;
359 }
360 break;
361 case WM_PAINT:
362 OnPaint(hWnd);
363 return 0;
364 case WM_SETCURSOR:
365 if (LOWORD(lParam) == HTCLIENT) {
366 POINT pt;
367 GetCursorPos(&pt);
368 ScreenToClient(hWnd, &pt);
369 if (pt.x>=pChildWnd->nSplitPos-SPLIT_WIDTH/2 && pt.x<pChildWnd->nSplitPos+SPLIT_WIDTH/2+1) {
370 SetCursor(LoadCursor(0, IDC_SIZEWE));
371 return TRUE;
372 }
373 }
374 goto def;
375 case WM_DESTROY:
376 DestroyTreeView();
377 DestroyListView(pChildWnd->hListWnd);
378 DestroyMainMenu();
379 HeapFree(GetProcessHeap(), 0, pChildWnd);
380 pChildWnd = NULL;
381 PostQuitMessage(0);
382 break;
383 case WM_LBUTTONDOWN: {
384 RECT rt;
385 int x = (short)LOWORD(lParam);
386 GetClientRect(hWnd, &rt);
387 if (x>=pChildWnd->nSplitPos-SPLIT_WIDTH/2 && x<pChildWnd->nSplitPos+SPLIT_WIDTH/2+1) {
388 last_split = pChildWnd->nSplitPos;
389 draw_splitbar(hWnd, last_split);
390 SetCapture(hWnd);
391 }
392 break;
393 }
394
395 case WM_LBUTTONUP:
396 case WM_RBUTTONDOWN:
397 if (GetCapture() == hWnd) {
398 finish_splitbar(hWnd, LOWORD(lParam));
399 }
400 break;
401
402 case WM_CAPTURECHANGED:
403 if (GetCapture()==hWnd && last_split>=0)
404 draw_splitbar(hWnd, last_split);
405 break;
406
407 case WM_KEYDOWN:
408 if (wParam == VK_ESCAPE)
409 if (GetCapture() == hWnd) {
410 RECT rt;
411 draw_splitbar(hWnd, last_split);
412 GetClientRect(hWnd, &rt);
413 ResizeWnd(pChildWnd, rt.right, rt.bottom);
414 last_split = -1;
415 ReleaseCapture();
416 SetCursor(LoadCursor(0, IDC_ARROW));
417 }
418 break;
419
420 case WM_MOUSEMOVE:
421 if (GetCapture() == hWnd) {
422 HDC hdc;
423 RECT rt;
424 HGDIOBJ OldObj;
425 int x = LOWORD(lParam);
426 if(!SizingPattern)
427 {
428 const DWORD Pattern[4] = {0x5555AAAA, 0x5555AAAA, 0x5555AAAA, 0x5555AAAA};
429 SizingPattern = CreateBitmap(8, 8, 1, 1, Pattern);
430 }
431 if(!SizingBrush)
432 {
433 SizingBrush = CreatePatternBrush(SizingPattern);
434 }
435
436 GetClientRect(hWnd, &rt);
437 x = (SHORT) min(max(x, SPLIT_MIN), rt.right - SPLIT_MIN);
438 if(last_split != x)
439 {
440 rt.left = last_split-SPLIT_WIDTH/2;
441 rt.right = last_split+SPLIT_WIDTH/2+1;
442 hdc = GetDC(hWnd);
443 OldObj = SelectObject(hdc, SizingBrush);
444 PatBlt(hdc, rt.left, rt.top, rt.right - rt.left, rt.bottom - rt.top, PATINVERT);
445 last_split = x;
446 rt.left = x-SPLIT_WIDTH/2;
447 rt.right = x+SPLIT_WIDTH/2+1;
448 PatBlt(hdc, rt.left, rt.top, rt.right - rt.left, rt.bottom - rt.top, PATINVERT);
449 SelectObject(hdc, OldObj);
450 ReleaseDC(hWnd, hdc);
451 }
452 }
453 break;
454
455 case WM_SETFOCUS:
456 if (pChildWnd != NULL) {
457 SetFocus(pChildWnd->nFocusPanel? pChildWnd->hListWnd: pChildWnd->hTreeWnd);
458 }
459 break;
460
461 case WM_TIMER:
462 break;
463
464 case WM_NOTIFY:
465 if ((int)wParam == TREE_WINDOW) {
466 switch (((LPNMHDR)lParam)->code) {
467 case TVN_ITEMEXPANDING:
468 return !OnTreeExpanding(pChildWnd->hTreeWnd, (NMTREEVIEW*)lParam);
469 case TVN_SELCHANGED: {
470 LPCTSTR keyPath, rootName;
471 LPTSTR fullPath;
472 HKEY hRootKey;
473
474 keyPath = GetItemPath(pChildWnd->hTreeWnd, ((NMTREEVIEW*)lParam)->itemNew.hItem, &hRootKey);
475 if (keyPath) {
476 RefreshListView(pChildWnd->hListWnd, hRootKey, keyPath);
477 rootName = get_root_key_name(hRootKey);
478 fullPath = HeapAlloc(GetProcessHeap(), 0, (_tcslen(rootName) + 1 + _tcslen(keyPath) + 1) * sizeof(TCHAR));
479 if (fullPath) {
480 _stprintf(fullPath, _T("%s\\%s"), rootName, keyPath);
481 SendMessage(hStatusBar, SB_SETTEXT, 0, (LPARAM)fullPath);
482 SendMessage(pChildWnd->hAddressBarWnd, WM_SETTEXT, 0, (LPARAM)fullPath);
483 HeapFree(GetProcessHeap(), 0, fullPath);
484
485 {
486 HKEY hKey;
487 TCHAR szBuffer[MAX_PATH];
488 _sntprintf(szBuffer, sizeof(szBuffer) / sizeof(szBuffer[0]), _T("My Computer\\%s\\%s"), rootName, keyPath);
489
490 if (RegCreateKey(HKEY_CURRENT_USER,
491 g_szGeneralRegKey,
492 &hKey) == ERROR_SUCCESS)
493 {
494 RegSetValueEx(hKey, _T("LastKey"), 0, REG_SZ, (LPBYTE) szBuffer, (DWORD) _tcslen(szBuffer) * sizeof(szBuffer[0]));
495 RegCloseKey(hKey);
496 }
497 }
498 }
499 }
500 }
501 break;
502 case NM_SETFOCUS:
503 pChildWnd->nFocusPanel = 0;
504 break;
505 case TVN_BEGINLABELEDIT:
506 {
507 LPNMTVDISPINFO ptvdi;
508 /* cancel label edit for rootkeys */
509 ptvdi = (LPNMTVDISPINFO) lParam;
510 if (!TreeView_GetParent(pChildWnd->hTreeWnd, ptvdi->item.hItem) ||
511 !TreeView_GetParent(pChildWnd->hTreeWnd, TreeView_GetParent(pChildWnd->hTreeWnd, ptvdi->item.hItem)))
512 return TRUE;
513 break;
514 }
515 case TVN_ENDLABELEDIT:
516 {
517 LPCTSTR keyPath;
518 HKEY hRootKey;
519 HKEY hKey = NULL;
520 LPNMTVDISPINFO ptvdi;
521 LONG lResult = TRUE;
522 TCHAR szBuffer[MAX_PATH];
523
524 ptvdi = (LPNMTVDISPINFO) lParam;
525 if (ptvdi->item.pszText)
526 {
527 keyPath = GetItemPath(pChildWnd->hTreeWnd, TreeView_GetParent(pChildWnd->hTreeWnd, ptvdi->item.hItem), &hRootKey);
528 _sntprintf(szBuffer, sizeof(szBuffer) / sizeof(szBuffer[0]), _T("%s\\%s"), keyPath, ptvdi->item.pszText);
529 keyPath = GetItemPath(pChildWnd->hTreeWnd, ptvdi->item.hItem, &hRootKey);
530 if (RegOpenKeyEx(hRootKey, szBuffer, 0, KEY_READ, &hKey) == ERROR_SUCCESS)
531 {
532 lResult = FALSE;
533 RegCloseKey(hKey);
534 (void)TreeView_EditLabel(pChildWnd->hTreeWnd, ptvdi->item.hItem);
535 }
536 else
537 {
538 if (RegRenameKey(hRootKey, keyPath, ptvdi->item.pszText) != ERROR_SUCCESS)
539 lResult = FALSE;
540 }
541 return lResult;
542 }
543 }
544 default:
545 return 0;
546 }
547 } else
548 {
549 if ((int)wParam == LIST_WINDOW)
550 {
551 switch (((LPNMHDR)lParam)->code) {
552 case NM_SETFOCUS:
553 pChildWnd->nFocusPanel = 1;
554 break;
555 default:
556 if(!ListWndNotifyProc(pChildWnd->hListWnd, wParam, lParam, &Result))
557 {
558 goto def;
559 }
560 return Result;
561 break;
562 }
563 }
564 }
565 break;
566
567 case WM_CONTEXTMENU:
568 {
569 POINT pt;
570 if((HWND)wParam == pChildWnd->hListWnd)
571 {
572 int i, cnt;
573 BOOL IsDefault;
574 pt.x = (short) LOWORD(lParam);
575 pt.y = (short) HIWORD(lParam);
576 cnt = ListView_GetSelectedCount(pChildWnd->hListWnd);
577 i = ListView_GetNextItem(pChildWnd->hListWnd, -1, LVNI_FOCUSED | LVNI_SELECTED);
578 if (pt.x == -1 && pt.y == -1)
579 {
580 RECT rc;
581 if (i != -1)
582 {
583 rc.left = LVIR_BOUNDS;
584 SendMessage(pChildWnd->hListWnd, LVM_GETITEMRECT, i, (LPARAM) &rc);
585 pt.x = rc.left + 8;
586 pt.y = rc.top + 8;
587 }
588 else
589 pt.x = pt.y = 0;
590 }
591 ClientToScreen(pChildWnd->hListWnd, &pt);
592 if(i == -1)
593 {
594 TrackPopupMenu(GetSubMenu(hPopupMenus, PM_NEW), TPM_RIGHTBUTTON, pt.x, pt.y, 0, hFrameWnd, NULL);
595 }
596 else
597 {
598 HMENU mnu = GetSubMenu(hPopupMenus, PM_MODIFYVALUE);
599 SetMenuDefaultItem(mnu, ID_EDIT_MODIFY, MF_BYCOMMAND);
600 IsDefault = IsDefaultValue(pChildWnd->hListWnd, i);
601 if(cnt == 1)
602 EnableMenuItem(mnu, ID_EDIT_RENAME, MF_BYCOMMAND | (IsDefault ? MF_DISABLED | MF_GRAYED : MF_ENABLED));
603 else
604 EnableMenuItem(mnu, ID_EDIT_RENAME, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
605 EnableMenuItem(mnu, ID_EDIT_MODIFY, MF_BYCOMMAND | (cnt == 1 ? MF_ENABLED : MF_DISABLED | MF_GRAYED));
606 EnableMenuItem(mnu, ID_EDIT_MODIFY_BIN, MF_BYCOMMAND | (cnt == 1 ? MF_ENABLED : MF_DISABLED | MF_GRAYED));
607
608 TrackPopupMenu(mnu, TPM_RIGHTBUTTON, pt.x, pt.y, 0, hFrameWnd, NULL);
609 }
610 }
611 else if ((HWND)wParam == pChildWnd->hTreeWnd)
612 {
613 TVHITTESTINFO hti;
614 HMENU hContextMenu;
615 TVITEM item;
616 MENUITEMINFO mii;
617 TCHAR resource[256];
618 TCHAR buffer[256];
619 LPTSTR s;
620 LPCTSTR keyPath;
621 HKEY hRootKey;
622 int iLastPos;
623 WORD wID;
624
625 pt.x = (short) LOWORD(lParam);
626 pt.y = (short) HIWORD(lParam);
627
628 if (pt.x == -1 && pt.y == -1)
629 {
630 RECT rc;
631 hti.hItem = TreeView_GetSelection(pChildWnd->hTreeWnd);
632 if (hti.hItem != NULL)
633 {
634 TreeView_GetItemRect(pChildWnd->hTreeWnd, hti.hItem, &rc, TRUE);
635 pt.x = rc.left + 8;
636 pt.y = rc.top + 8;
637 ClientToScreen(pChildWnd->hTreeWnd, &pt);
638 hti.flags = TVHT_ONITEM;
639 }
640 else
641 hti.flags = 0;
642 }
643 else
644 {
645 hti.pt.x = pt.x;
646 hti.pt.y = pt.y;
647 ScreenToClient(pChildWnd->hTreeWnd, &hti.pt);
648 (void)TreeView_HitTest(pChildWnd->hTreeWnd, &hti);
649 }
650
651 if (hti.flags & TVHT_ONITEM)
652 {
653 hContextMenu = GetSubMenu(hPopupMenus, PM_TREECONTEXT);
654 (void)TreeView_SelectItem(pChildWnd->hTreeWnd, hti.hItem);
655
656 memset(&item, 0, sizeof(item));
657 item.mask = TVIF_STATE | TVIF_CHILDREN;
658 item.hItem = hti.hItem;
659 (void)TreeView_GetItem(pChildWnd->hTreeWnd, &item);
660
661 /* Set the Expand/Collapse menu item appropriately */
662 LoadString(hInst, (item.state & TVIS_EXPANDED) ? IDS_COLLAPSE : IDS_EXPAND, buffer, sizeof(buffer) / sizeof(buffer[0]));
663 memset(&mii, 0, sizeof(mii));
664 mii.cbSize = sizeof(mii);
665 mii.fMask = MIIM_STRING | MIIM_STATE | MIIM_ID;
666 mii.fState = (item.cChildren > 0) ? MFS_DEFAULT : MFS_GRAYED;
667 mii.wID = (item.state & TVIS_EXPANDED) ? ID_TREE_COLLAPSEBRANCH : ID_TREE_EXPANDBRANCH;
668 mii.dwTypeData = (LPTSTR) buffer;
669 SetMenuItemInfo(hContextMenu, 0, TRUE, &mii);
670
671 /* Remove any existing suggestions */
672 memset(&mii, 0, sizeof(mii));
673 mii.cbSize = sizeof(mii);
674 mii.fMask = MIIM_ID;
675 GetMenuItemInfo(hContextMenu, GetMenuItemCount(hContextMenu) - 1, TRUE, &mii);
676 if ((mii.wID >= ID_TREE_SUGGESTION_MIN) && (mii.wID <= ID_TREE_SUGGESTION_MAX))
677 {
678 do
679 {
680 iLastPos = GetMenuItemCount(hContextMenu) - 1;
681 GetMenuItemInfo(hContextMenu, iLastPos, TRUE, &mii);
682 RemoveMenu(hContextMenu, iLastPos, MF_BYPOSITION);
683 }
684 while((mii.wID >= ID_TREE_SUGGESTION_MIN) && (mii.wID <= ID_TREE_SUGGESTION_MAX));
685 }
686
687 /* Come up with suggestions */
688 keyPath = GetItemPath(pChildWnd->hTreeWnd, NULL, &hRootKey);
689 SuggestKeys(hRootKey, keyPath, Suggestions, sizeof(Suggestions) / sizeof(Suggestions[0]));
690 if (Suggestions[0])
691 {
692 AppendMenu(hContextMenu, MF_SEPARATOR, 0, NULL);
693
694 LoadString(hInst, IDS_GOTO_SUGGESTED_KEY, resource, sizeof(resource) / sizeof(resource[0]));
695
696 s = Suggestions;
697 wID = ID_TREE_SUGGESTION_MIN;
698 while(*s && (wID <= ID_TREE_SUGGESTION_MAX))
699 {
700 _sntprintf(buffer, sizeof(buffer) / sizeof(buffer[0]), resource, s);
701
702 memset(&mii, 0, sizeof(mii));
703 mii.cbSize = sizeof(mii);
704 mii.fMask = MIIM_STRING | MIIM_ID;
705 mii.wID = wID++;
706 mii.dwTypeData = buffer;
707 InsertMenuItem(hContextMenu, GetMenuItemCount(hContextMenu), TRUE, &mii);
708
709 s += _tcslen(s) + 1;
710 }
711 }
712 TrackPopupMenu(hContextMenu, TPM_RIGHTBUTTON, pt.x, pt.y, 0, pChildWnd->hWnd, NULL);
713 }
714 }
715 break;
716 }
717
718 case WM_SIZE:
719 if (wParam != SIZE_MINIMIZED && pChildWnd != NULL) {
720 ResizeWnd(pChildWnd, LOWORD(lParam), HIWORD(lParam));
721 }
722 /* fall through */
723 default: def:
724 return DefWindowProc(hWnd, message, wParam, lParam);
725 }
726 return 0;
727 }