Release 1.3.2.
[wine/gsoc-2012-control.git] / programs / regedit / childwnd.c
blobbb56cb2c159406b1f3eb267267ed6128624d1079
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>
24 #include <stdio.h>
26 #include "main.h"
27 #include "regproc.h"
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];
62 else
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};
66 return unknown_key;
70 static void draw_splitbar(HWND hWnd, int x)
72 RECT rt;
73 HDC hdc = GetDC(hWnd);
75 GetClientRect(hWnd, &rt);
76 rt.left = x - SPLIT_WIDTH/2;
77 rt.right = x + SPLIT_WIDTH/2+1;
78 InvertRect(hdc, &rt);
79 ReleaseDC(hWnd, hdc);
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)
95 PAINTSTRUCT ps;
96 RECT rt;
98 GetClientRect(hWnd, &rt);
99 BeginPaint(hWnd, &ps);
100 FillRect(ps.hdc, &rt, GetSysColorBrush(COLOR_BTNFACE));
101 EndPaint(hWnd, &ps);
104 static LPWSTR CombinePaths(LPCWSTR pPaths[], int nPaths) {
105 int i, len, pos;
106 LPWSTR combined;
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));
113 *combined = '\0';
114 for (i=0, pos=0; i<nPaths; i++) {
115 if (pPaths[i] && *pPaths[i]) {
116 int llen = lstrlenW(pPaths[i]);
117 if (!*combined)
118 lstrcpyW(combined, pPaths[i]);
119 else {
120 combined[pos++] = '\\';
121 lstrcpyW(combined+pos, pPaths[i]);
123 pos += llen;
126 return combined;
129 static LPWSTR GetPathRoot(HWND hwndTV, HTREEITEM hItem, BOOL bFull) {
130 LPCWSTR parts[2] = {0,0};
131 WCHAR text[260];
132 HKEY hRootKey = NULL;
133 if (!hItem)
134 hItem = TreeView_GetSelection(hwndTV);
135 HeapFree(GetProcessHeap(), 0, GetItemPath(hwndTV, hItem, &hRootKey));
136 if (!bFull && !hRootKey)
137 return NULL;
138 if (hRootKey)
139 parts[1] = GetRootKeyName(hRootKey);
140 if (bFull) {
141 DWORD dwSize = sizeof(text)/sizeof(WCHAR);
142 GetComputerNameW(text, &dwSize);
143 parts[0] = text;
145 return CombinePaths(parts, 2);
148 LPWSTR GetItemFullPath(HWND hwndTV, HTREEITEM hItem, BOOL bFull) {
149 LPWSTR parts[2];
150 LPWSTR ret;
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]);
158 return ret;
161 static LPWSTR GetPathFullPath(HWND hwndTV, LPWSTR path) {
162 LPWSTR parts[2];
163 LPWSTR ret;
165 parts[0] = GetPathRoot(hwndTV, 0, TRUE);
166 parts[1] = path;
167 ret = CombinePaths((LPCWSTR*)parts, 2);
168 HeapFree(GetProcessHeap(), 0, parts[0]);
169 return ret;
172 static void OnTreeSelectionChanged(HWND hwndTV, HWND hwndLV, HTREEITEM hItem, BOOL bRefreshLV)
174 if (bRefreshLV) {
175 LPWSTR keyPath;
176 HKEY hRootKey = NULL;
177 keyPath = GetItemPath(hwndTV, hItem, &hRootKey);
178 RefreshListView(hwndLV, hRootKey, keyPath, NULL);
179 HeapFree(GetProcessHeap(), 0, keyPath);
181 UpdateStatusBar();
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)
192 RECT rt;
194 draw_splitbar(hWnd, last_split);
195 last_split = -1;
196 GetClientRect(hWnd, &rt);
197 g_pChildWnd->nSplitPos = x;
198 ResizeWnd(rt.right, rt.bottom);
199 ReleaseCapture();
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:
215 DestroyWindow(hWnd);
216 break;
217 case ID_VIEW_REFRESH:
218 WINE_TRACE("Is this ever called or is it just dead code?\n");
219 /* TODO */
220 break;
221 case ID_SWITCH_PANELS:
222 g_pChildWnd->nFocusPanel = !g_pChildWnd->nFocusPanel;
223 SetFocus(g_pChildWnd->nFocusPanel? g_pChildWnd->hListWnd: g_pChildWnd->hTreeWnd);
224 break;
225 default:
226 return FALSE;
228 return TRUE;
231 /*******************************************************************************
232 * get_last_key [internal]
234 * open last key
237 static void get_last_key(HWND hwndTV)
239 HKEY hkey;
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)
246 SendMessageW(hwndTV, TVM_SELECTITEM, TVGN_CARET, (LPARAM)FindPathInTree(hwndTV, wszVal));
248 RegCloseKey(hkey);
252 /*******************************************************************************
253 * set_last_key [internal]
255 * save last key
258 static void set_last_key(HWND hwndTV)
260 HKEY hkey;
261 WCHAR *wszVal;
263 if (RegCreateKeyExW(HKEY_CURRENT_USER, wszKeyName, 0, NULL, 0, KEY_WRITE, NULL, &hkey, NULL) == ERROR_SUCCESS)
265 wszVal = GetItemFullPath(g_pChildWnd->hTreeWnd, TreeView_GetSelection(g_pChildWnd->hTreeWnd), FALSE);
266 RegSetValueExW(hkey, wszLastKey, 0, REG_SZ, (LPBYTE)wszVal, (lstrlenW(wszVal) + 1) * sizeof(WCHAR));
267 HeapFree(GetProcessHeap(), 0, wszVal);
268 RegCloseKey(hkey);
272 /*******************************************************************************
274 * FUNCTION: ChildWndProc(HWND, unsigned, WORD, LONG)
276 * PURPOSE: Processes messages for the child windows.
278 * WM_COMMAND - process the application menu
279 * WM_PAINT - Paint the main window
280 * WM_DESTROY - post a quit message and return
283 LRESULT CALLBACK ChildWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
285 switch (message) {
286 case WM_CREATE:
287 g_pChildWnd = HeapAlloc(GetProcessHeap(), 0, sizeof(ChildWnd));
288 if (!g_pChildWnd) return 0;
289 LoadStringW(hInst, IDS_REGISTRY_ROOT_NAME, g_pChildWnd->szPath, MAX_PATH);
290 g_pChildWnd->nSplitPos = 250;
291 g_pChildWnd->hWnd = hWnd;
292 g_pChildWnd->hTreeWnd = CreateTreeView(hWnd, g_pChildWnd->szPath, TREE_WINDOW);
293 g_pChildWnd->hListWnd = CreateListView(hWnd, LIST_WINDOW/*, g_pChildWnd->szPath*/);
294 g_pChildWnd->nFocusPanel = 1;
295 SetFocus(g_pChildWnd->hTreeWnd);
296 get_last_key(g_pChildWnd->hTreeWnd);
297 break;
298 case WM_COMMAND:
299 if (!_CmdWndProc(hWnd, message, wParam, lParam)) {
300 goto def;
302 break;
303 case WM_PAINT:
304 OnPaint(hWnd);
305 return 0;
306 case WM_SETCURSOR:
307 if (LOWORD(lParam) == HTCLIENT) {
308 POINT pt;
309 GetCursorPos(&pt);
310 ScreenToClient(hWnd, &pt);
311 if (pt.x>=g_pChildWnd->nSplitPos-SPLIT_WIDTH/2 && pt.x<g_pChildWnd->nSplitPos+SPLIT_WIDTH/2+1) {
312 SetCursor(LoadCursor(0, IDC_SIZEWE));
313 return TRUE;
316 goto def;
317 case WM_DESTROY:
318 set_last_key(g_pChildWnd->hTreeWnd);
319 HeapFree(GetProcessHeap(), 0, g_pChildWnd);
320 g_pChildWnd = NULL;
321 PostQuitMessage(0);
322 break;
323 case WM_LBUTTONDOWN: {
324 RECT rt;
325 int x = (short)LOWORD(lParam);
326 GetClientRect(hWnd, &rt);
327 if (x>=g_pChildWnd->nSplitPos-SPLIT_WIDTH/2 && x<g_pChildWnd->nSplitPos+SPLIT_WIDTH/2+1) {
328 last_split = g_pChildWnd->nSplitPos;
329 draw_splitbar(hWnd, last_split);
330 SetCapture(hWnd);
332 break;
335 /* WM_RBUTTONDOWN sets the splitbar the same way as WM_LBUTTONUP */
336 case WM_LBUTTONUP:
337 case WM_RBUTTONDOWN:
338 if (GetCapture() == hWnd) {
339 finish_splitbar(hWnd, LOWORD(lParam));
341 break;
343 case WM_CAPTURECHANGED:
344 if (GetCapture()==hWnd && last_split>=0)
345 draw_splitbar(hWnd, last_split);
346 break;
348 case WM_KEYDOWN:
349 if (wParam == VK_ESCAPE)
350 if (GetCapture() == hWnd) {
351 RECT rt;
352 draw_splitbar(hWnd, last_split);
353 GetClientRect(hWnd, &rt);
354 ResizeWnd(rt.right, rt.bottom);
355 last_split = -1;
356 ReleaseCapture();
357 SetCursor(LoadCursor(0, IDC_ARROW));
359 break;
361 case WM_MOUSEMOVE:
362 if (GetCapture() == hWnd) {
363 RECT rt;
364 int x = LOWORD(lParam);
365 HDC hdc = GetDC(hWnd);
366 GetClientRect(hWnd, &rt);
367 rt.left = last_split-SPLIT_WIDTH/2;
368 rt.right = last_split+SPLIT_WIDTH/2+1;
369 InvertRect(hdc, &rt);
370 last_split = x;
371 rt.left = x-SPLIT_WIDTH/2;
372 rt.right = x+SPLIT_WIDTH/2+1;
373 InvertRect(hdc, &rt);
374 ReleaseDC(hWnd, hdc);
376 break;
378 case WM_SETFOCUS:
379 if (g_pChildWnd != NULL) {
380 SetFocus(g_pChildWnd->nFocusPanel? g_pChildWnd->hListWnd: g_pChildWnd->hTreeWnd);
382 break;
384 case WM_TIMER:
385 break;
387 case WM_NOTIFY:
388 if (((int)wParam == TREE_WINDOW) && (g_pChildWnd != NULL)) {
389 switch (((LPNMHDR)lParam)->code) {
390 case TVN_ITEMEXPANDINGW:
391 return !OnTreeExpanding(g_pChildWnd->hTreeWnd, (NMTREEVIEW*)lParam);
392 case TVN_SELCHANGEDW:
393 OnTreeSelectionChanged(g_pChildWnd->hTreeWnd, g_pChildWnd->hListWnd,
394 ((NMTREEVIEWW *)lParam)->itemNew.hItem, TRUE);
395 break;
396 case NM_SETFOCUS:
397 g_pChildWnd->nFocusPanel = 0;
398 break;
399 case NM_RCLICK: {
400 POINT pt;
401 GetCursorPos(&pt);
402 TrackPopupMenu(GetSubMenu(hPopupMenus, PM_NEW),
403 TPM_RIGHTBUTTON, pt.x, pt.y, 0, hFrameWnd, NULL);
404 break;
406 case TVN_ENDLABELEDITW: {
407 HKEY hRootKey;
408 LPNMTVDISPINFOW dispInfo = (LPNMTVDISPINFOW)lParam;
409 LPWSTR path = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hRootKey);
410 BOOL res = RenameKey(hWnd, hRootKey, path, dispInfo->item.pszText);
411 if (res) {
412 TVITEMEXW item;
413 LPWSTR fullPath = GetPathFullPath(g_pChildWnd->hTreeWnd,
414 dispInfo->item.pszText);
415 item.mask = TVIF_HANDLE | TVIF_TEXT;
416 item.hItem = TreeView_GetSelection(g_pChildWnd->hTreeWnd);
417 item.pszText = dispInfo->item.pszText;
418 SendMessageW( g_pChildWnd->hTreeWnd, TVM_SETITEMW, 0, (LPARAM)&item );
419 SendMessageW(hStatusBar, SB_SETTEXTW, 0, (LPARAM)fullPath);
420 HeapFree(GetProcessHeap(), 0, fullPath);
422 HeapFree(GetProcessHeap(), 0, path);
423 return res;
425 default:
426 return 0; /* goto def; */
428 } else
429 if (((int)wParam == LIST_WINDOW) && (g_pChildWnd != NULL)) {
430 if (((LPNMHDR)lParam)->code == NM_SETFOCUS) {
431 g_pChildWnd->nFocusPanel = 1;
432 } else if (!SendMessageW(g_pChildWnd->hListWnd, WM_NOTIFY_REFLECT, wParam, lParam)) {
433 goto def;
436 break;
438 case WM_SIZE:
439 if (wParam != SIZE_MINIMIZED && g_pChildWnd != NULL) {
440 ResizeWnd(LOWORD(lParam), HIWORD(lParam));
442 /* fall through */
443 default: def:
444 return DefWindowProc(hWnd, message, wParam, lParam);
446 return 0;