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 */
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(regedit
);
31 ChildWnd
* g_pChildWnd
;
32 static int last_split
;
34 static const WCHAR wszLastKey
[] = L
"LastKey";
35 static const WCHAR wszKeyName
[] = L
"Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\Regedit";
37 /*******************************************************************************
38 * Local module support methods
41 static LPCWSTR
GetRootKeyName(HKEY hRootKey
)
43 if(hRootKey
== HKEY_CLASSES_ROOT
)
44 return reg_class_namesW
[INDEX_HKEY_CLASSES_ROOT
];
45 if(hRootKey
== HKEY_CURRENT_USER
)
46 return reg_class_namesW
[INDEX_HKEY_CURRENT_USER
];
47 if(hRootKey
== HKEY_LOCAL_MACHINE
)
48 return reg_class_namesW
[INDEX_HKEY_LOCAL_MACHINE
];
49 if(hRootKey
== HKEY_USERS
)
50 return reg_class_namesW
[INDEX_HKEY_USERS
];
51 if(hRootKey
== HKEY_CURRENT_CONFIG
)
52 return reg_class_namesW
[INDEX_HKEY_CURRENT_CONFIG
];
53 if(hRootKey
== HKEY_DYN_DATA
)
54 return reg_class_namesW
[INDEX_HKEY_DYN_DATA
];
56 return L
"Unknown HKEY. Please report.";
59 static void draw_splitbar(HWND hWnd
, int x
)
62 HDC hdc
= GetDC(hWnd
);
64 GetClientRect(hWnd
, &rt
);
65 rt
.left
= x
- SPLIT_WIDTH
/2;
66 rt
.right
= x
+ SPLIT_WIDTH
/2+1;
71 static void ResizeWnd(int cx
, int cy
)
73 HDWP hdwp
= BeginDeferWindowPos(2);
74 RECT rt
= {0, 0, cx
, cy
};
76 cx
= g_pChildWnd
->nSplitPos
+ SPLIT_WIDTH
/2;
77 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
);
78 DeferWindowPos(hdwp
, g_pChildWnd
->hListWnd
, 0, rt
.left
+cx
, rt
.top
, rt
.right
-cx
, rt
.bottom
-rt
.top
, SWP_NOZORDER
|SWP_NOACTIVATE
);
79 EndDeferWindowPos(hdwp
);
82 static LPWSTR
CombinePaths(LPCWSTR pPaths
[], int nPaths
) {
85 for (i
=0, len
=0; i
<nPaths
; i
++) {
86 if (pPaths
[i
] && *pPaths
[i
]) {
87 len
+= lstrlenW(pPaths
[i
])+1;
90 combined
= malloc(len
* sizeof(WCHAR
));
92 for (i
=0, pos
=0; i
<nPaths
; i
++) {
93 if (pPaths
[i
] && *pPaths
[i
]) {
94 int llen
= lstrlenW(pPaths
[i
]);
96 lstrcpyW(combined
, pPaths
[i
]);
98 combined
[pos
++] = '\\';
99 lstrcpyW(combined
+pos
, pPaths
[i
]);
107 static LPWSTR
GetPathRoot(HWND hwndTV
, HTREEITEM hItem
, BOOL bFull
) {
108 LPCWSTR parts
[2] = {0,0};
110 HKEY hRootKey
= NULL
;
112 hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CARET
, 0);
113 free(GetItemPath(hwndTV
, hItem
, &hRootKey
));
114 if (!bFull
&& !hRootKey
)
117 parts
[1] = GetRootKeyName(hRootKey
);
119 DWORD dwSize
= ARRAY_SIZE(text
);
120 GetComputerNameW(text
, &dwSize
);
123 return CombinePaths(parts
, 2);
126 LPWSTR
GetItemFullPath(HWND hwndTV
, HTREEITEM hItem
, BOOL bFull
) {
129 HKEY hRootKey
= NULL
;
131 parts
[0] = GetPathRoot(hwndTV
, hItem
, bFull
);
132 parts
[1] = GetItemPath(hwndTV
, hItem
, &hRootKey
);
133 ret
= CombinePaths((LPCWSTR
*)parts
, 2);
139 static void OnTreeSelectionChanged(HWND hwndTV
, HWND hwndLV
, HTREEITEM hItem
, BOOL bRefreshLV
)
143 HKEY hRootKey
= NULL
;
146 rootitem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_ROOT
, 0);
147 if (rootitem
== hItem
)
149 SendMessageW(hwndLV
, LVM_DELETEALLITEMS
, 0, 0);
154 keyPath
= GetItemPath(hwndTV
, hItem
, &hRootKey
);
155 RefreshListView(hwndLV
, hRootKey
, keyPath
, NULL
);
161 /*******************************************************************************
162 * finish_splitbar [internal]
164 * make the splitbar invisible and resize the windows
165 * (helper for ChildWndProc)
167 static void finish_splitbar(HWND hWnd
, int x
)
171 draw_splitbar(hWnd
, last_split
);
173 GetClientRect(hWnd
, &rt
);
174 g_pChildWnd
->nSplitPos
= x
;
175 ResizeWnd(rt
.right
, rt
.bottom
);
179 /*******************************************************************************
181 * FUNCTION: _CmdWndProc(HWND, unsigned, WORD, LONG)
183 * PURPOSE: Processes WM_COMMAND messages for the main frame window.
187 static BOOL
_CmdWndProc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
189 switch (LOWORD(wParam
)) {
190 /* Parse the menu selections: */
191 case ID_REGISTRY_EXIT
:
194 case ID_VIEW_REFRESH
:
195 WINE_TRACE("Is this ever called or is it just dead code?\n");
198 case ID_SWITCH_PANELS
:
199 g_pChildWnd
->nFocusPanel
= !g_pChildWnd
->nFocusPanel
;
200 SetFocus(g_pChildWnd
->nFocusPanel
? g_pChildWnd
->hListWnd
: g_pChildWnd
->hTreeWnd
);
208 /*******************************************************************************
209 * get_last_key [internal]
214 static void get_last_key(HWND hwndTV
)
217 WCHAR wszVal
[KEY_MAX_LEN
];
218 DWORD dwSize
= sizeof(wszVal
);
220 if (RegCreateKeyExW(HKEY_CURRENT_USER
, wszKeyName
, 0, NULL
, 0, KEY_READ
, NULL
, &hkey
, NULL
) == ERROR_SUCCESS
)
222 HTREEITEM selection
= NULL
;
223 if (RegQueryValueExW(hkey
, wszLastKey
, NULL
, NULL
, (LPBYTE
)wszVal
, &dwSize
) == ERROR_SUCCESS
)
225 if (lstrcmpW(wszVal
, g_pChildWnd
->szPath
))
226 selection
= FindPathInTree(hwndTV
, wszVal
);
231 selection
= (HTREEITEM
)SendMessageW(g_pChildWnd
->hTreeWnd
, TVM_GETNEXTITEM
, TVGN_ROOT
, 0);
232 SendMessageW(hwndTV
, TVM_EXPAND
, TVE_EXPAND
, (LPARAM
)selection
);
235 SendMessageW(hwndTV
, TVM_SELECTITEM
, TVGN_CARET
, (LPARAM
)selection
);
241 /*******************************************************************************
242 * set_last_key [internal]
247 static void set_last_key(HWND hwndTV
)
251 if (RegCreateKeyExW(HKEY_CURRENT_USER
, wszKeyName
, 0, NULL
, 0, KEY_WRITE
, NULL
, &hkey
, NULL
) == ERROR_SUCCESS
)
253 HTREEITEM selection
= (HTREEITEM
)SendMessageW(g_pChildWnd
->hTreeWnd
, TVM_GETNEXTITEM
, TVGN_CARET
, 0);
254 HTREEITEM root
= (HTREEITEM
)SendMessageW(g_pChildWnd
->hTreeWnd
, TVM_GETNEXTITEM
, TVGN_ROOT
, 0);
257 if (selection
== root
)
258 value
= g_pChildWnd
->szPath
;
260 value
= GetItemFullPath(g_pChildWnd
->hTreeWnd
, selection
, FALSE
);
261 RegSetValueExW(hkey
, wszLastKey
, 0, REG_SZ
, (LPBYTE
)value
, (lstrlenW(value
) + 1) * sizeof(WCHAR
));
262 if (selection
!= root
)
268 static int treeview_notify(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
270 switch (((NMHDR
*)lParam
)->code
)
273 g_pChildWnd
->nFocusPanel
= 0;
275 case TVN_BEGINLABELEDITW
:
280 if (!GetWindowLongPtrW(g_pChildWnd
->hTreeWnd
, GWLP_USERDATA
))
283 path
= GetItemPath(g_pChildWnd
->hTreeWnd
, 0, &hRootKey
);
288 case TVN_ENDLABELEDITW
:
291 NMTVDISPINFOW
*dispInfo
= (NMTVDISPINFOW
*)lParam
;
292 WCHAR
*path
= GetItemPath(g_pChildWnd
->hTreeWnd
, 0, &hRootKey
);
293 BOOL res
= RenameKey(hWnd
, hRootKey
, path
, dispInfo
->item
.pszText
);
301 item
.mask
= TVIF_HANDLE
| TVIF_TEXT
;
302 item
.hItem
= dispInfo
->item
.hItem
;
303 item
.pszText
= dispInfo
->item
.pszText
;
304 SendMessageW(g_pChildWnd
->hTreeWnd
, TVM_SETITEMW
, 0, (LPARAM
)&item
);
306 path
= GetItemPath(g_pChildWnd
->hTreeWnd
, 0, &hRootKey
);
307 update_listview_path(path
);
313 SetWindowLongPtrW(g_pChildWnd
->hTreeWnd
, GWLP_USERDATA
, 0);
316 case TVN_ITEMEXPANDINGW
:
317 return !OnTreeExpanding(g_pChildWnd
->hTreeWnd
, (NMTREEVIEWW
*)lParam
);
318 case TVN_SELCHANGEDW
:
319 OnTreeSelectionChanged(g_pChildWnd
->hTreeWnd
, g_pChildWnd
->hListWnd
,
320 ((NMTREEVIEWW
*)lParam
)->itemNew
.hItem
, TRUE
);
326 static int listview_notify(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
328 switch (((NMHDR
*)lParam
)->code
)
332 NMITEMACTIVATE
*nmitem
= (NMITEMACTIVATE
*)lParam
;
334 if (nmitem
->iItem
!= -1)
339 item
.stateMask
= LVIS_FOCUSED
| LVIS_SELECTED
;
340 SendMessageW(g_pChildWnd
->hListWnd
, LVM_SETITEMSTATE
, (UINT
)-1, (LPARAM
)&item
);
342 item
.state
= LVIS_FOCUSED
| LVIS_SELECTED
;
343 item
.stateMask
= LVIS_FOCUSED
| LVIS_SELECTED
;
344 SendMessageW(g_pChildWnd
->hListWnd
, LVM_SETITEMSTATE
, nmitem
->iItem
, (LPARAM
)&item
);
346 SendMessageW(hFrameWnd
, WM_COMMAND
, ID_EDIT_MODIFY
, 0);
352 int cnt
= SendMessageW(g_pChildWnd
->hListWnd
, LVM_GETNEXTITEM
, -1,
353 MAKELPARAM(LVNI_FOCUSED
| LVNI_SELECTED
, 0));
355 SendMessageW(hFrameWnd
, WM_COMMAND
, ID_EDIT_MODIFY
, 0);
359 g_pChildWnd
->nFocusPanel
= 1;
361 case LVN_BEGINLABELEDITW
:
362 if (!((NMLVDISPINFOW
*)lParam
)->item
.iItem
)
365 case LVN_COLUMNCLICK
:
366 if (g_columnToSort
== ((NMLISTVIEW
*)lParam
)->iSubItem
)
367 g_invertSort
= !g_invertSort
;
370 g_columnToSort
= ((NMLISTVIEW
*)lParam
)->iSubItem
;
371 g_invertSort
= FALSE
;
374 SendMessageW(g_pChildWnd
->hListWnd
, LVM_SORTITEMS
,
375 (WPARAM
)g_pChildWnd
->hListWnd
, (LPARAM
)CompareFunc
);
379 NMLISTVIEW
*nmlv
= (NMLISTVIEW
*)lParam
;
380 LINE_INFO
*info
= (LINE_INFO
*)nmlv
->lParam
;
387 case LVN_ENDLABELEDITW
:
389 NMLVDISPINFOW
*dispInfo
= (NMLVDISPINFOW
*)lParam
;
390 WCHAR
*oldName
= GetItemText(g_pChildWnd
->hListWnd
, dispInfo
->item
.iItem
);
393 if (!oldName
) return -1; /* cannot rename a default value */
394 ret
= RenameValue(g_pChildWnd
->hListWnd
, g_currentRootKey
, g_currentPath
,
395 oldName
, dispInfo
->item
.pszText
);
398 dispInfo
->item
.iSubItem
= 0;
399 SendMessageW(g_pChildWnd
->hListWnd
, LVM_SETITEMTEXTW
,
400 dispInfo
->item
.iItem
, (LPARAM
)&dispInfo
->item
);
406 case LVN_GETDISPINFOW
:
407 OnGetDispInfo((NMLVDISPINFOW
*)lParam
);
413 #define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
414 #define GET_Y_LPARAM(lp) ((int)(short)HIWORD(lp))
416 /*******************************************************************************
418 * FUNCTION: ChildWndProc(HWND, unsigned, WORD, LONG)
420 * PURPOSE: Processes messages for the child windows.
422 * WM_COMMAND - process the application menu
423 * WM_PAINT - Paint the main window
424 * WM_DESTROY - post a quit message and return
427 LRESULT CALLBACK
ChildWndProc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
431 g_pChildWnd
= malloc(sizeof(ChildWnd
));
432 if (!g_pChildWnd
) return 0;
433 LoadStringW(hInst
, IDS_REGISTRY_ROOT_NAME
, g_pChildWnd
->szPath
, MAX_PATH
);
434 g_pChildWnd
->nSplitPos
= 250;
435 g_pChildWnd
->hWnd
= hWnd
;
436 g_pChildWnd
->hTreeWnd
= CreateTreeView(hWnd
, g_pChildWnd
->szPath
, TREE_WINDOW
);
437 g_pChildWnd
->hListWnd
= CreateListView(hWnd
, LIST_WINDOW
/*, g_pChildWnd->szPath*/);
438 g_pChildWnd
->nFocusPanel
= 1;
439 SetFocus(g_pChildWnd
->hTreeWnd
);
440 get_last_key(g_pChildWnd
->hTreeWnd
);
443 if (!_CmdWndProc(hWnd
, message
, wParam
, lParam
)) {
448 if (LOWORD(lParam
) == HTCLIENT
) {
451 ScreenToClient(hWnd
, &pt
);
452 if (pt
.x
>=g_pChildWnd
->nSplitPos
-SPLIT_WIDTH
/2 && pt
.x
<g_pChildWnd
->nSplitPos
+SPLIT_WIDTH
/2+1) {
453 SetCursor(LoadCursorW(0, (LPCWSTR
)IDC_SIZEWE
));
459 set_last_key(g_pChildWnd
->hTreeWnd
);
464 case WM_LBUTTONDOWN
: {
466 int x
= (short)LOWORD(lParam
);
467 GetClientRect(hWnd
, &rt
);
468 if (x
>=g_pChildWnd
->nSplitPos
-SPLIT_WIDTH
/2 && x
<g_pChildWnd
->nSplitPos
+SPLIT_WIDTH
/2+1) {
469 last_split
= g_pChildWnd
->nSplitPos
;
470 draw_splitbar(hWnd
, last_split
);
476 /* WM_RBUTTONDOWN sets the splitbar the same way as WM_LBUTTONUP */
479 if (GetCapture() == hWnd
) {
480 finish_splitbar(hWnd
, LOWORD(lParam
));
484 case WM_CAPTURECHANGED
:
485 if (GetCapture()==hWnd
&& last_split
>=0)
486 draw_splitbar(hWnd
, last_split
);
491 POINT pt
= {GET_X_LPARAM(lParam
), GET_Y_LPARAM(lParam
)};
492 short int menu_id
= -1;
494 if ((HWND
)wParam
== g_pChildWnd
->hTreeWnd
)
499 ScreenToClient(g_pChildWnd
->hTreeWnd
, &ht
.pt
);
501 if (SendMessageW(g_pChildWnd
->hTreeWnd
, TVM_HITTEST
, 0, (LPARAM
)&ht
))
505 SendMessageW(g_pChildWnd
->hTreeWnd
, TVM_SELECTITEM
, TVGN_CARET
, (LPARAM
)ht
.hItem
);
506 root
= (HTREEITEM
)SendMessageW(g_pChildWnd
->hTreeWnd
, TVM_GETNEXTITEM
, TVGN_ROOT
, 0);
507 menu_id
= (ht
.hItem
== root
) ? PM_COMPUTER
: PM_TREEVIEW
;
512 int sel
= SendMessageW(g_pChildWnd
->hListWnd
, LVM_GETNEXTITEM
, -1,
513 MAKELPARAM(LVNI_SELECTED
, 0));
514 menu_id
= (sel
== -1) ? PM_NEW_VALUE
: PM_MODIFY_VALUE
;
517 TrackPopupMenu(GetSubMenu(hPopupMenus
, menu_id
), TPM_RIGHTBUTTON
,
518 pt
.x
, pt
.y
, 0, hFrameWnd
, NULL
);
523 if (wParam
== VK_ESCAPE
)
524 if (GetCapture() == hWnd
) {
526 draw_splitbar(hWnd
, last_split
);
527 GetClientRect(hWnd
, &rt
);
528 ResizeWnd(rt
.right
, rt
.bottom
);
531 SetCursor(LoadCursorW(0, (LPCWSTR
)IDC_ARROW
));
536 if (GetCapture() == hWnd
) {
538 int x
= LOWORD(lParam
);
539 HDC hdc
= GetDC(hWnd
);
540 GetClientRect(hWnd
, &rt
);
541 rt
.left
= last_split
-SPLIT_WIDTH
/2;
542 rt
.right
= last_split
+SPLIT_WIDTH
/2+1;
543 InvertRect(hdc
, &rt
);
545 rt
.left
= x
-SPLIT_WIDTH
/2;
546 rt
.right
= x
+SPLIT_WIDTH
/2+1;
547 InvertRect(hdc
, &rt
);
548 ReleaseDC(hWnd
, hdc
);
553 if (g_pChildWnd
!= NULL
) {
554 SetFocus(g_pChildWnd
->nFocusPanel
? g_pChildWnd
->hListWnd
: g_pChildWnd
->hTreeWnd
);
562 if (wParam
== TREE_WINDOW
&& g_pChildWnd
)
563 return treeview_notify(hWnd
, message
, wParam
, lParam
);
564 else if (wParam
== LIST_WINDOW
&& g_pChildWnd
)
565 return listview_notify(hWnd
, message
, wParam
, lParam
);
569 if (wParam
!= SIZE_MINIMIZED
&& g_pChildWnd
!= NULL
) {
570 ResizeWnd(LOWORD(lParam
), HIWORD(lParam
));
574 return DefWindowProcW(hWnd
, message
, wParam
, lParam
);