regedit: Do not write junk after the LastKey value.
[wine] / programs / 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 St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
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>
25
26 #include "main.h"
27 #include "regproc.h"
28
29 #include "wine/debug.h"
30 #include "wine/unicode.h"
31                                                                                                                              
32 WINE_DEFAULT_DEBUG_CHANNEL(regedit);
33
34 ChildWnd* g_pChildWnd;
35 static int last_split;
36
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};
43
44 /*******************************************************************************
45  * Local module support methods
46  */
47
48 static LPCWSTR GetRootKeyName(HKEY hRootKey)
49 {
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
63     {
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;
67     }
68 }
69
70 static void draw_splitbar(HWND hWnd, int x)
71 {
72     RECT rt;
73     HDC hdc = GetDC(hWnd);
74
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);
80 }
81
82 static void ResizeWnd(int cx, int cy)
83 {
84     HDWP hdwp = BeginDeferWindowPos(2);
85     RECT rt = {0, 0, cx, cy};
86
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);
91 }
92
93 static void OnPaint(HWND hWnd)
94 {
95     PAINTSTRUCT ps;
96     RECT rt;
97     HDC hdc;
98
99     GetClientRect(hWnd, &rt);
100     hdc = BeginPaint(hWnd, &ps);
101     FillRect(ps.hdc, &rt, GetSysColorBrush(COLOR_BTNFACE));
102     EndPaint(hWnd, &ps);
103 }
104
105 static LPWSTR CombinePaths(LPCWSTR pPaths[], int nPaths) {
106     int i, len, pos;
107     LPWSTR combined;
108     for (i=0, len=0; i<nPaths; i++) {
109         if (pPaths[i] && *pPaths[i]) {
110             len += lstrlenW(pPaths[i])+1;
111         }
112     }
113     combined = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
114     *combined = '\0';
115     for (i=0, pos=0; i<nPaths; i++) {
116         if (pPaths[i] && *pPaths[i]) {
117             int llen = lstrlenW(pPaths[i]);
118             if (!*combined)
119                 lstrcpyW(combined, pPaths[i]);
120             else {
121                 combined[pos++] = '\\';
122                 lstrcpyW(combined+pos, pPaths[i]);
123             }
124             pos += llen;
125         }
126     }
127     return combined;
128 }
129
130 static LPWSTR GetPathRoot(HWND hwndTV, HTREEITEM hItem, BOOL bFull) {
131     LPCWSTR parts[2] = {0,0};
132     WCHAR text[260];
133     HKEY hRootKey = NULL;
134     if (!hItem)
135         hItem = TreeView_GetSelection(hwndTV);
136     HeapFree(GetProcessHeap(), 0, GetItemPath(hwndTV, hItem, &hRootKey));
137     if (!bFull && !hRootKey)
138         return NULL;
139     if (hRootKey)
140         parts[1] = GetRootKeyName(hRootKey);
141     if (bFull) {
142         DWORD dwSize = sizeof(text)/sizeof(WCHAR);
143         GetComputerNameW(text, &dwSize);
144         parts[0] = text;
145     }
146     return CombinePaths(parts, 2);
147 }
148
149 LPWSTR GetItemFullPath(HWND hwndTV, HTREEITEM hItem, BOOL bFull) {
150     LPWSTR parts[2];
151     LPWSTR ret;
152     HKEY hRootKey = NULL;
153
154     parts[0] = GetPathRoot(hwndTV, hItem, bFull);
155     parts[1] = GetItemPath(hwndTV, hItem, &hRootKey);
156     ret = CombinePaths((LPCWSTR *)parts, 2);
157     HeapFree(GetProcessHeap(), 0, parts[0]);
158     HeapFree(GetProcessHeap(), 0, parts[1]);
159     return ret;
160 }
161
162 static LPWSTR GetPathFullPath(HWND hwndTV, LPWSTR path) {
163     LPWSTR parts[2];
164     LPWSTR ret;
165
166     parts[0] = GetPathRoot(hwndTV, 0, TRUE);
167     parts[1] = path;
168     ret = CombinePaths((LPCWSTR*)parts, 2);
169     HeapFree(GetProcessHeap(), 0, parts[0]);
170     return ret;
171 }
172
173 static void OnTreeSelectionChanged(HWND hwndTV, HWND hwndLV, HTREEITEM hItem, BOOL bRefreshLV)
174 {
175     if (bRefreshLV) {
176         LPWSTR keyPath;
177         HKEY hRootKey = NULL;
178         keyPath = GetItemPath(hwndTV, hItem, &hRootKey);
179         RefreshListView(hwndLV, hRootKey, keyPath, NULL);
180         HeapFree(GetProcessHeap(), 0, keyPath);
181     }
182     UpdateStatusBar();
183 }
184
185 /*******************************************************************************
186  * finish_splitbar [internal]
187  *
188  * make the splitbar invisible and resize the windows
189  * (helper for ChildWndProc)
190  */
191 static void finish_splitbar(HWND hWnd, int x)
192 {
193     RECT rt;
194
195     draw_splitbar(hWnd, last_split);
196     last_split = -1;
197     GetClientRect(hWnd, &rt);
198     g_pChildWnd->nSplitPos = x;
199     ResizeWnd(rt.right, rt.bottom);
200     ReleaseCapture();
201 }
202
203 /*******************************************************************************
204  *
205  *  FUNCTION: _CmdWndProc(HWND, unsigned, WORD, LONG)
206  *
207  *  PURPOSE:  Processes WM_COMMAND messages for the main frame window.
208  *
209  */
210
211 static BOOL _CmdWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
212 {
213     switch (LOWORD(wParam)) {
214         /* Parse the menu selections: */
215     case ID_REGISTRY_EXIT:
216         DestroyWindow(hWnd);
217         break;
218     case ID_VIEW_REFRESH:
219         WINE_TRACE("Is this ever called or is it just dead code?\n");
220         /* TODO */
221         break;
222     case ID_SWITCH_PANELS:
223         g_pChildWnd->nFocusPanel = !g_pChildWnd->nFocusPanel;
224         SetFocus(g_pChildWnd->nFocusPanel? g_pChildWnd->hListWnd: g_pChildWnd->hTreeWnd);
225         break;
226     default:
227         return FALSE;
228     }
229     return TRUE;
230 }
231
232 /*******************************************************************************
233  * get_last_key [internal]
234  *
235  * open last key
236  *
237  */
238 static void get_last_key(HWND hwndTV)
239 {
240     HKEY hkey;
241     WCHAR wszVal[KEY_MAX_LEN];
242     DWORD dwSize = sizeof(wszVal);
243
244     if (RegCreateKeyExW(HKEY_CURRENT_USER, wszKeyName, 0, NULL, 0, KEY_READ, NULL, &hkey, NULL) == ERROR_SUCCESS)
245     {
246         if (RegQueryValueExW(hkey, wszLastKey, NULL, NULL, (LPBYTE)wszVal, &dwSize) == ERROR_SUCCESS)
247             SendMessageW(hwndTV, TVM_SELECTITEM, TVGN_CARET, (LPARAM)FindPathInTree(hwndTV, wszVal));
248
249         RegCloseKey(hkey);
250     }
251 }
252
253 /*******************************************************************************
254  * set_last_key [internal]
255  *
256  * save last key
257  *
258  */
259 static void set_last_key(HWND hwndTV)
260 {
261     HKEY hkey;
262     WCHAR *wszVal;
263
264     if (RegCreateKeyExW(HKEY_CURRENT_USER, wszKeyName, 0, NULL, 0, KEY_WRITE, NULL, &hkey, NULL) == ERROR_SUCCESS)
265     {
266         wszVal = GetItemFullPath(g_pChildWnd->hTreeWnd, TreeView_GetSelection(g_pChildWnd->hTreeWnd), FALSE);
267         RegSetValueExW(hkey, wszLastKey, 0, REG_SZ, (LPBYTE)wszVal, (lstrlenW(wszVal) + 1) * sizeof(WCHAR));
268         HeapFree(GetProcessHeap(), 0, wszVal);
269         RegCloseKey(hkey);
270     }
271 }
272
273 /*******************************************************************************
274  *
275  *  FUNCTION: ChildWndProc(HWND, unsigned, WORD, LONG)
276  *
277  *  PURPOSE:  Processes messages for the child windows.
278  *
279  *  WM_COMMAND  - process the application menu
280  *  WM_PAINT    - Paint the main window
281  *  WM_DESTROY  - post a quit message and return
282  *
283  */
284 LRESULT CALLBACK ChildWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
285 {
286     switch (message) {
287     case WM_CREATE:
288         g_pChildWnd = HeapAlloc(GetProcessHeap(), 0, sizeof(ChildWnd));
289         if (!g_pChildWnd) return 0;
290         LoadStringW(hInst, IDS_REGISTRY_ROOT_NAME, g_pChildWnd->szPath, MAX_PATH);
291         g_pChildWnd->nSplitPos = 250;
292         g_pChildWnd->hWnd = hWnd;
293         g_pChildWnd->hTreeWnd = CreateTreeView(hWnd, g_pChildWnd->szPath, TREE_WINDOW);
294         g_pChildWnd->hListWnd = CreateListView(hWnd, LIST_WINDOW/*, g_pChildWnd->szPath*/);
295         g_pChildWnd->nFocusPanel = 1;
296         SetFocus(g_pChildWnd->hTreeWnd);
297         get_last_key(g_pChildWnd->hTreeWnd);
298         break;
299     case WM_COMMAND:
300         if (!_CmdWndProc(hWnd, message, wParam, lParam)) {
301             goto def;
302         }
303         break;
304     case WM_PAINT:
305         OnPaint(hWnd);
306         return 0;
307     case WM_SETCURSOR:
308         if (LOWORD(lParam) == HTCLIENT) {
309             POINT pt;
310             GetCursorPos(&pt);
311             ScreenToClient(hWnd, &pt);
312             if (pt.x>=g_pChildWnd->nSplitPos-SPLIT_WIDTH/2 && pt.x<g_pChildWnd->nSplitPos+SPLIT_WIDTH/2+1) {
313                 SetCursor(LoadCursor(0, IDC_SIZEWE));
314                 return TRUE;
315             }
316         }
317         goto def;
318     case WM_DESTROY:
319         set_last_key(g_pChildWnd->hTreeWnd);
320         HeapFree(GetProcessHeap(), 0, g_pChildWnd);
321         g_pChildWnd = NULL;
322         PostQuitMessage(0);
323         break;
324     case WM_LBUTTONDOWN: {
325             RECT rt;
326             int x = (short)LOWORD(lParam);
327             GetClientRect(hWnd, &rt);
328             if (x>=g_pChildWnd->nSplitPos-SPLIT_WIDTH/2 && x<g_pChildWnd->nSplitPos+SPLIT_WIDTH/2+1) {
329                 last_split = g_pChildWnd->nSplitPos;
330                 draw_splitbar(hWnd, last_split);
331                 SetCapture(hWnd);
332             }
333             break;
334         }
335
336     /* WM_RBUTTONDOWN sets the splitbar the same way as WM_LBUTTONUP */
337     case WM_LBUTTONUP:
338     case WM_RBUTTONDOWN:
339         if (GetCapture() == hWnd) {
340             finish_splitbar(hWnd, LOWORD(lParam));
341         }
342         break;
343
344     case WM_CAPTURECHANGED:
345         if (GetCapture()==hWnd && last_split>=0)
346             draw_splitbar(hWnd, last_split);
347         break;
348
349     case WM_KEYDOWN:
350         if (wParam == VK_ESCAPE)
351             if (GetCapture() == hWnd) {
352                 RECT rt;
353                 draw_splitbar(hWnd, last_split);
354                 GetClientRect(hWnd, &rt);
355                 ResizeWnd(rt.right, rt.bottom);
356                 last_split = -1;
357                 ReleaseCapture();
358                 SetCursor(LoadCursor(0, IDC_ARROW));
359             }
360         break;
361
362     case WM_MOUSEMOVE:
363         if (GetCapture() == hWnd) {
364             RECT rt;
365             int x = LOWORD(lParam);
366             HDC hdc = GetDC(hWnd);
367             GetClientRect(hWnd, &rt);
368             rt.left = last_split-SPLIT_WIDTH/2;
369             rt.right = last_split+SPLIT_WIDTH/2+1;
370             InvertRect(hdc, &rt);
371             last_split = x;
372             rt.left = x-SPLIT_WIDTH/2;
373             rt.right = x+SPLIT_WIDTH/2+1;
374             InvertRect(hdc, &rt);
375             ReleaseDC(hWnd, hdc);
376         }
377         break;
378
379     case WM_SETFOCUS:
380         if (g_pChildWnd != NULL) {
381             SetFocus(g_pChildWnd->nFocusPanel? g_pChildWnd->hListWnd: g_pChildWnd->hTreeWnd);
382         }
383         break;
384
385     case WM_TIMER:
386         break;
387
388     case WM_NOTIFY:
389         if (((int)wParam == TREE_WINDOW) && (g_pChildWnd != NULL)) {
390             switch (((LPNMHDR)lParam)->code) {
391             case TVN_ITEMEXPANDINGW:
392                 return !OnTreeExpanding(g_pChildWnd->hTreeWnd, (NMTREEVIEW*)lParam);
393             case TVN_SELCHANGEDW:
394                 OnTreeSelectionChanged(g_pChildWnd->hTreeWnd, g_pChildWnd->hListWnd,
395                     ((NMTREEVIEWW *)lParam)->itemNew.hItem, TRUE);
396                 break;
397             case NM_SETFOCUS:
398                 g_pChildWnd->nFocusPanel = 0;
399                 break;
400             case NM_RCLICK: {
401                 POINT pt;
402                 GetCursorPos(&pt);
403                 TrackPopupMenu(GetSubMenu(hPopupMenus, PM_NEW),
404                                TPM_RIGHTBUTTON, pt.x, pt.y, 0, hFrameWnd, NULL);
405                 break;
406             }
407             case TVN_ENDLABELEDITW: {
408                 HKEY hRootKey;
409                 LPNMTVDISPINFOW dispInfo = (LPNMTVDISPINFOW)lParam;
410                 LPWSTR path = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hRootKey);
411                 BOOL res = RenameKey(hWnd, hRootKey, path, dispInfo->item.pszText);
412                 if (res) {
413                     TVITEMEXW item;
414                     LPWSTR fullPath = GetPathFullPath(g_pChildWnd->hTreeWnd,
415                      dispInfo->item.pszText);
416                     item.mask = TVIF_HANDLE | TVIF_TEXT;
417                     item.hItem = TreeView_GetSelection(g_pChildWnd->hTreeWnd);
418                     item.pszText = dispInfo->item.pszText;
419                     SendMessageW( g_pChildWnd->hTreeWnd, TVM_SETITEMW, 0, (LPARAM)&item );
420                     SendMessageW(hStatusBar, SB_SETTEXTW, 0, (LPARAM)fullPath);
421                     HeapFree(GetProcessHeap(), 0, fullPath);
422                 }
423                 HeapFree(GetProcessHeap(), 0, path);
424                 return res;
425             }
426             default:
427                 return 0; /* goto def; */
428             }
429         } else
430             if (((int)wParam == LIST_WINDOW) && (g_pChildWnd != NULL)) {
431                 if (((LPNMHDR)lParam)->code == NM_SETFOCUS) {
432                     g_pChildWnd->nFocusPanel = 1;
433                 } else if (!SendMessageW(g_pChildWnd->hListWnd, WM_NOTIFY_REFLECT, wParam, lParam)) {
434                     goto def;
435                 }
436             }
437         break;
438
439     case WM_SIZE:
440         if (wParam != SIZE_MINIMIZED && g_pChildWnd != NULL) {
441             ResizeWnd(LOWORD(lParam), HIWORD(lParam));
442         }
443         /* fall through */
444 default: def:
445         return DefWindowProc(hWnd, message, wParam, lParam);
446     }
447     return 0;
448 }