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 */
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
];
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};
70 static void draw_splitbar(HWND hWnd
, int x
)
73 HDC hdc
= GetDC(hWnd
);
75 GetClientRect(hWnd
, &rt
);
76 rt
.left
= x
- SPLIT_WIDTH
/2;
77 rt
.right
= x
+ SPLIT_WIDTH
/2+1;
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
)
98 GetClientRect(hWnd
, &rt
);
99 BeginPaint(hWnd
, &ps
);
100 FillRect(ps
.hdc
, &rt
, GetSysColorBrush(COLOR_BTNFACE
));
104 static LPWSTR
CombinePaths(LPCWSTR pPaths
[], int nPaths
) {
107 for (i
=0, len
=0; i
<nPaths
; i
++) {
108 if (pPaths
[i
] && *pPaths
[i
]) {
109 len
+= lstrlenW(pPaths
[i
])+1;
112 combined
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
114 for (i
=0, pos
=0; i
<nPaths
; i
++) {
115 if (pPaths
[i
] && *pPaths
[i
]) {
116 int llen
= lstrlenW(pPaths
[i
]);
118 lstrcpyW(combined
, pPaths
[i
]);
120 combined
[pos
++] = '\\';
121 lstrcpyW(combined
+pos
, pPaths
[i
]);
129 static LPWSTR
GetPathRoot(HWND hwndTV
, HTREEITEM hItem
, BOOL bFull
) {
130 LPCWSTR parts
[2] = {0,0};
132 HKEY hRootKey
= NULL
;
134 hItem
= (HTREEITEM
)SendMessageW(hwndTV
, TVM_GETNEXTITEM
, TVGN_CARET
, 0);
135 HeapFree(GetProcessHeap(), 0, GetItemPath(hwndTV
, hItem
, &hRootKey
));
136 if (!bFull
&& !hRootKey
)
139 parts
[1] = GetRootKeyName(hRootKey
);
141 DWORD dwSize
= sizeof(text
)/sizeof(WCHAR
);
142 GetComputerNameW(text
, &dwSize
);
145 return CombinePaths(parts
, 2);
148 LPWSTR
GetItemFullPath(HWND hwndTV
, HTREEITEM hItem
, BOOL bFull
) {
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 HeapFree(GetProcessHeap(), 0, parts
[0]);
157 HeapFree(GetProcessHeap(), 0, parts
[1]);
161 static LPWSTR
GetPathFullPath(HWND hwndTV
, LPWSTR path
) {
165 parts
[0] = GetPathRoot(hwndTV
, 0, TRUE
);
167 ret
= CombinePaths((LPCWSTR
*)parts
, 2);
168 HeapFree(GetProcessHeap(), 0, parts
[0]);
172 static void OnTreeSelectionChanged(HWND hwndTV
, HWND hwndLV
, HTREEITEM hItem
, BOOL bRefreshLV
)
176 HKEY hRootKey
= NULL
;
177 keyPath
= GetItemPath(hwndTV
, hItem
, &hRootKey
);
178 RefreshListView(hwndLV
, hRootKey
, keyPath
, NULL
);
179 HeapFree(GetProcessHeap(), 0, keyPath
);
184 /*******************************************************************************
185 * finish_splitbar [internal]
187 * make the splitbar invisible and resize the windows
188 * (helper for ChildWndProc)
190 static void finish_splitbar(HWND hWnd
, int x
)
194 draw_splitbar(hWnd
, last_split
);
196 GetClientRect(hWnd
, &rt
);
197 g_pChildWnd
->nSplitPos
= x
;
198 ResizeWnd(rt
.right
, rt
.bottom
);
202 /*******************************************************************************
204 * FUNCTION: _CmdWndProc(HWND, unsigned, WORD, LONG)
206 * PURPOSE: Processes WM_COMMAND messages for the main frame window.
210 static BOOL
_CmdWndProc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
212 switch (LOWORD(wParam
)) {
213 /* Parse the menu selections: */
214 case ID_REGISTRY_EXIT
:
217 case ID_VIEW_REFRESH
:
218 WINE_TRACE("Is this ever called or is it just dead code?\n");
221 case ID_SWITCH_PANELS
:
222 g_pChildWnd
->nFocusPanel
= !g_pChildWnd
->nFocusPanel
;
223 SetFocus(g_pChildWnd
->nFocusPanel
? g_pChildWnd
->hListWnd
: g_pChildWnd
->hTreeWnd
);
231 /*******************************************************************************
232 * get_last_key [internal]
237 static void get_last_key(HWND hwndTV
)
240 WCHAR wszVal
[KEY_MAX_LEN
];
241 DWORD dwSize
= sizeof(wszVal
);
243 if (RegCreateKeyExW(HKEY_CURRENT_USER
, wszKeyName
, 0, NULL
, 0, KEY_READ
, NULL
, &hkey
, NULL
) == ERROR_SUCCESS
)
245 if (RegQueryValueExW(hkey
, wszLastKey
, NULL
, NULL
, (LPBYTE
)wszVal
, &dwSize
) == ERROR_SUCCESS
)
248 if (!strcmpW(wszVal
, g_pChildWnd
->szPath
))
249 selection
= (HTREEITEM
)SendMessageW(g_pChildWnd
->hTreeWnd
, TVM_GETNEXTITEM
, TVGN_ROOT
, 0);
251 selection
= FindPathInTree(hwndTV
, wszVal
);
252 SendMessageW(hwndTV
, TVM_SELECTITEM
, TVGN_CARET
, (LPARAM
)selection
);
259 /*******************************************************************************
260 * set_last_key [internal]
265 static void set_last_key(HWND hwndTV
)
269 if (RegCreateKeyExW(HKEY_CURRENT_USER
, wszKeyName
, 0, NULL
, 0, KEY_WRITE
, NULL
, &hkey
, NULL
) == ERROR_SUCCESS
)
271 HTREEITEM selection
= (HTREEITEM
)SendMessageW(g_pChildWnd
->hTreeWnd
, TVM_GETNEXTITEM
, TVGN_CARET
, 0);
272 HTREEITEM root
= (HTREEITEM
)SendMessageW(g_pChildWnd
->hTreeWnd
, TVM_GETNEXTITEM
, TVGN_ROOT
, 0);
275 if (selection
== root
)
276 value
= g_pChildWnd
->szPath
;
278 value
= GetItemFullPath(g_pChildWnd
->hTreeWnd
, selection
, FALSE
);
279 RegSetValueExW(hkey
, wszLastKey
, 0, REG_SZ
, (LPBYTE
)value
, (lstrlenW(value
) + 1) * sizeof(WCHAR
));
280 if (selection
!= root
)
281 HeapFree(GetProcessHeap(), 0, value
);
286 /*******************************************************************************
288 * FUNCTION: ChildWndProc(HWND, unsigned, WORD, LONG)
290 * PURPOSE: Processes messages for the child windows.
292 * WM_COMMAND - process the application menu
293 * WM_PAINT - Paint the main window
294 * WM_DESTROY - post a quit message and return
297 LRESULT CALLBACK
ChildWndProc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
301 g_pChildWnd
= HeapAlloc(GetProcessHeap(), 0, sizeof(ChildWnd
));
302 if (!g_pChildWnd
) return 0;
303 LoadStringW(hInst
, IDS_REGISTRY_ROOT_NAME
, g_pChildWnd
->szPath
, MAX_PATH
);
304 g_pChildWnd
->nSplitPos
= 250;
305 g_pChildWnd
->hWnd
= hWnd
;
306 g_pChildWnd
->hTreeWnd
= CreateTreeView(hWnd
, g_pChildWnd
->szPath
, TREE_WINDOW
);
307 g_pChildWnd
->hListWnd
= CreateListView(hWnd
, LIST_WINDOW
/*, g_pChildWnd->szPath*/);
308 g_pChildWnd
->nFocusPanel
= 1;
309 SetFocus(g_pChildWnd
->hTreeWnd
);
310 get_last_key(g_pChildWnd
->hTreeWnd
);
313 if (!_CmdWndProc(hWnd
, message
, wParam
, lParam
)) {
321 if (LOWORD(lParam
) == HTCLIENT
) {
324 ScreenToClient(hWnd
, &pt
);
325 if (pt
.x
>=g_pChildWnd
->nSplitPos
-SPLIT_WIDTH
/2 && pt
.x
<g_pChildWnd
->nSplitPos
+SPLIT_WIDTH
/2+1) {
326 SetCursor(LoadCursorW(0, (LPCWSTR
)IDC_SIZEWE
));
332 set_last_key(g_pChildWnd
->hTreeWnd
);
333 HeapFree(GetProcessHeap(), 0, g_pChildWnd
);
337 case WM_LBUTTONDOWN
: {
339 int x
= (short)LOWORD(lParam
);
340 GetClientRect(hWnd
, &rt
);
341 if (x
>=g_pChildWnd
->nSplitPos
-SPLIT_WIDTH
/2 && x
<g_pChildWnd
->nSplitPos
+SPLIT_WIDTH
/2+1) {
342 last_split
= g_pChildWnd
->nSplitPos
;
343 draw_splitbar(hWnd
, last_split
);
349 /* WM_RBUTTONDOWN sets the splitbar the same way as WM_LBUTTONUP */
352 if (GetCapture() == hWnd
) {
353 finish_splitbar(hWnd
, LOWORD(lParam
));
357 case WM_CAPTURECHANGED
:
358 if (GetCapture()==hWnd
&& last_split
>=0)
359 draw_splitbar(hWnd
, last_split
);
363 if (wParam
== VK_ESCAPE
)
364 if (GetCapture() == hWnd
) {
366 draw_splitbar(hWnd
, last_split
);
367 GetClientRect(hWnd
, &rt
);
368 ResizeWnd(rt
.right
, rt
.bottom
);
371 SetCursor(LoadCursorW(0, (LPCWSTR
)IDC_ARROW
));
376 if (GetCapture() == hWnd
) {
378 int x
= LOWORD(lParam
);
379 HDC hdc
= GetDC(hWnd
);
380 GetClientRect(hWnd
, &rt
);
381 rt
.left
= last_split
-SPLIT_WIDTH
/2;
382 rt
.right
= last_split
+SPLIT_WIDTH
/2+1;
383 InvertRect(hdc
, &rt
);
385 rt
.left
= x
-SPLIT_WIDTH
/2;
386 rt
.right
= x
+SPLIT_WIDTH
/2+1;
387 InvertRect(hdc
, &rt
);
388 ReleaseDC(hWnd
, hdc
);
393 if (g_pChildWnd
!= NULL
) {
394 SetFocus(g_pChildWnd
->nFocusPanel
? g_pChildWnd
->hListWnd
: g_pChildWnd
->hTreeWnd
);
402 if (((int)wParam
== TREE_WINDOW
) && (g_pChildWnd
!= NULL
)) {
403 switch (((LPNMHDR
)lParam
)->code
) {
404 case TVN_ITEMEXPANDINGW
:
405 return !OnTreeExpanding(g_pChildWnd
->hTreeWnd
, (NMTREEVIEWW
*)lParam
);
406 case TVN_SELCHANGEDW
:
407 OnTreeSelectionChanged(g_pChildWnd
->hTreeWnd
, g_pChildWnd
->hListWnd
,
408 ((NMTREEVIEWW
*)lParam
)->itemNew
.hItem
, TRUE
);
411 g_pChildWnd
->nFocusPanel
= 0;
416 TrackPopupMenu(GetSubMenu(hPopupMenus
, PM_NEW
),
417 TPM_RIGHTBUTTON
, pt
.x
, pt
.y
, 0, hFrameWnd
, NULL
);
420 case TVN_BEGINLABELEDITW
: {
422 LPWSTR path
= GetItemPath(g_pChildWnd
->hTreeWnd
, 0, &hRootKey
);
423 if (!path
|| !*path
) return 1;
426 case TVN_ENDLABELEDITW
: {
428 LPNMTVDISPINFOW dispInfo
= (LPNMTVDISPINFOW
)lParam
;
429 LPWSTR path
= GetItemPath(g_pChildWnd
->hTreeWnd
, 0, &hRootKey
);
430 BOOL res
= RenameKey(hWnd
, hRootKey
, path
, dispInfo
->item
.pszText
);
433 LPWSTR fullPath
= GetPathFullPath(g_pChildWnd
->hTreeWnd
,
434 dispInfo
->item
.pszText
);
435 item
.mask
= TVIF_HANDLE
| TVIF_TEXT
;
436 item
.hItem
= (HTREEITEM
)SendMessageW(g_pChildWnd
->hTreeWnd
, TVM_GETNEXTITEM
, TVGN_CARET
, 0);
437 item
.pszText
= dispInfo
->item
.pszText
;
438 SendMessageW( g_pChildWnd
->hTreeWnd
, TVM_SETITEMW
, 0, (LPARAM
)&item
);
439 SendMessageW(hStatusBar
, SB_SETTEXTW
, 0, (LPARAM
)fullPath
);
440 HeapFree(GetProcessHeap(), 0, fullPath
);
442 HeapFree(GetProcessHeap(), 0, path
);
446 return 0; /* goto def; */
449 if (((int)wParam
== LIST_WINDOW
) && (g_pChildWnd
!= NULL
)) {
450 if (((LPNMHDR
)lParam
)->code
== NM_SETFOCUS
) {
451 g_pChildWnd
->nFocusPanel
= 1;
452 } else if (!SendMessageW(g_pChildWnd
->hListWnd
, WM_NOTIFY_REFLECT
, wParam
, lParam
)) {
459 if (wParam
!= SIZE_MINIMIZED
&& g_pChildWnd
!= NULL
) {
460 ResizeWnd(LOWORD(lParam
), HIWORD(lParam
));
464 return DefWindowProcW(hWnd
, message
, wParam
, lParam
);