regedit: Fix leak in GetItemFullPathW.
[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 <tchar.h>
25 #include <stdio.h>
26
27 #include "main.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 /*******************************************************************************
38  * Local module support methods
39  */
40
41 LPCTSTR GetRootKeyName(HKEY hRootKey)
42 {
43     if (hRootKey == HKEY_CLASSES_ROOT) return _T("HKEY_CLASSES_ROOT");
44     if (hRootKey == HKEY_CURRENT_USER) return _T("HKEY_CURRENT_USER");
45     if (hRootKey == HKEY_LOCAL_MACHINE) return _T("HKEY_LOCAL_MACHINE");
46     if (hRootKey == HKEY_USERS) return _T("HKEY_USERS");
47     if (hRootKey == HKEY_CURRENT_CONFIG) return _T("HKEY_CURRENT_CONFIG");
48     if (hRootKey == HKEY_DYN_DATA) return _T("HKEY_DYN_DATA");
49     return _T("UNKNOWN HKEY, PLEASE REPORT");
50 }
51
52 LPCWSTR GetRootKeyNameW(HKEY hRootKey)
53 {
54     if(hRootKey == HKEY_CLASSES_ROOT)
55         return reg_class_namesW[INDEX_HKEY_CLASSES_ROOT];
56     if(hRootKey == HKEY_CURRENT_USER)
57         return reg_class_namesW[INDEX_HKEY_CURRENT_USER];
58     if(hRootKey == HKEY_LOCAL_MACHINE)
59         return reg_class_namesW[INDEX_HKEY_LOCAL_MACHINE];
60     if(hRootKey == HKEY_USERS)
61         return reg_class_namesW[INDEX_HKEY_USERS];
62     if(hRootKey == HKEY_CURRENT_CONFIG)
63         return reg_class_namesW[INDEX_HKEY_CURRENT_CONFIG];
64     if(hRootKey == HKEY_DYN_DATA)
65         return reg_class_namesW[INDEX_HKEY_DYN_DATA];
66     else
67     {
68         static const WCHAR unknown_key[] = {'U','N','K','N','O','W','N',' ','H','K','E','Y',',',' ',
69                                             'P','L','E','A','S','E',' ','R','E','P','O','R','T',0};
70         return unknown_key;
71     }
72 }
73
74 static void draw_splitbar(HWND hWnd, int x)
75 {
76     RECT rt;
77     HDC hdc = GetDC(hWnd);
78
79     GetClientRect(hWnd, &rt);
80     rt.left = x - SPLIT_WIDTH/2;
81     rt.right = x + SPLIT_WIDTH/2+1;
82     InvertRect(hdc, &rt);
83     ReleaseDC(hWnd, hdc);
84 }
85
86 static void ResizeWnd(int cx, int cy)
87 {
88     HDWP hdwp = BeginDeferWindowPos(2);
89     RECT rt = {0, 0, cx, cy};
90
91     cx = g_pChildWnd->nSplitPos + SPLIT_WIDTH/2;
92     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);
93     DeferWindowPos(hdwp, g_pChildWnd->hListWnd, 0, rt.left+cx  , rt.top, rt.right-cx, rt.bottom-rt.top, SWP_NOZORDER|SWP_NOACTIVATE);
94     EndDeferWindowPos(hdwp);
95 }
96
97 static void OnPaint(HWND hWnd)
98 {
99     PAINTSTRUCT ps;
100     RECT rt;
101     HDC hdc;
102
103     GetClientRect(hWnd, &rt);
104     hdc = BeginPaint(hWnd, &ps);
105     FillRect(ps.hdc, &rt, GetSysColorBrush(COLOR_BTNFACE));
106     EndPaint(hWnd, &ps);
107 }
108
109 static LPTSTR CombinePaths(LPCTSTR pPaths[], int nPaths) {
110     int i, len, pos;
111     LPTSTR combined;
112     for (i=0, len=0; i<nPaths; i++) {
113         if (pPaths[i] && *pPaths[i]) {
114             len += lstrlen(pPaths[i])+1;
115         }
116     }
117     combined = HeapAlloc(GetProcessHeap(), 0, len * sizeof(TCHAR));
118     *combined = '\0';
119     for (i=0, pos=0; i<nPaths; i++) {
120         if (pPaths[i] && *pPaths[i]) {
121             int llen = _tcslen(pPaths[i]);
122             if (!*combined)
123                 _tcscpy(combined, pPaths[i]);
124             else {
125                 combined[pos++] = (TCHAR)'\\';
126                 _tcscpy(combined+pos, pPaths[i]);
127             }
128             pos += llen;
129         }
130     }
131     return combined;
132 }
133
134 static LPWSTR CombinePathsW(LPCWSTR pPaths[], int nPaths) {
135     int i, len, pos;
136     LPWSTR combined;
137     for (i=0, len=0; i<nPaths; i++) {
138         if (pPaths[i] && *pPaths[i]) {
139             len += lstrlenW(pPaths[i])+1;
140         }
141     }
142     combined = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
143     *combined = '\0';
144     for (i=0, pos=0; i<nPaths; i++) {
145         if (pPaths[i] && *pPaths[i]) {
146             int llen = lstrlenW(pPaths[i]);
147             if (!*combined)
148                 lstrcpyW(combined, pPaths[i]);
149             else {
150                 combined[pos++] = (TCHAR)'\\';
151                 lstrcpyW(combined+pos, pPaths[i]);
152             }
153             pos += llen;
154         }
155     }
156     return combined;
157 }
158
159 static LPTSTR GetPathRoot(HWND hwndTV, HTREEITEM hItem, BOOL bFull) {
160     LPCTSTR parts[2] = {_T(""), _T("")};
161     TCHAR text[260];
162     HKEY hRootKey = NULL;
163     if (!hItem)
164         hItem = TreeView_GetSelection(hwndTV);
165     GetItemPath(hwndTV, hItem, &hRootKey);
166     if (!bFull && !hRootKey)
167         return NULL;
168     if (hRootKey)
169         parts[1] = GetRootKeyName(hRootKey);
170     if (bFull) {
171         DWORD dwSize = sizeof(text)/sizeof(TCHAR);
172         GetComputerName(text, &dwSize);
173         parts[0] = text;
174     }
175     return CombinePaths(parts, 2);
176 }
177
178 static LPWSTR GetPathRootW(HWND hwndTV, HTREEITEM hItem, BOOL bFull) {
179     LPCWSTR parts[2] = {0,0};
180     WCHAR text[260];
181     HKEY hRootKey = NULL;
182     if (!hItem)
183         hItem = TreeView_GetSelection(hwndTV);
184     GetItemPathW(hwndTV, hItem, &hRootKey);
185     if (!bFull && !hRootKey)
186         return NULL;
187     if (hRootKey)
188         parts[1] = GetRootKeyNameW(hRootKey);
189     if (bFull) {
190         DWORD dwSize = sizeof(text)/sizeof(TCHAR);
191         GetComputerNameW(text, &dwSize);
192         parts[0] = text;
193     }
194     return CombinePathsW(parts, 2);
195 }
196
197 LPTSTR GetItemFullPath(HWND hwndTV, HTREEITEM hItem, BOOL bFull) {
198     LPTSTR parts[2];
199     LPTSTR ret;
200     HKEY hRootKey = NULL;
201
202     parts[0] = GetPathRoot(hwndTV, hItem, bFull);
203     parts[1] = GetItemPath(hwndTV, hItem, &hRootKey);
204     ret = CombinePaths((LPCTSTR *)parts, 2);
205     HeapFree(GetProcessHeap(), 0, parts[0]);
206     return ret;
207 }
208
209 LPWSTR GetItemFullPathW(HWND hwndTV, HTREEITEM hItem, BOOL bFull) {
210     LPWSTR parts[2];
211     LPWSTR ret;
212     HKEY hRootKey = NULL;
213
214     parts[0] = GetPathRootW(hwndTV, hItem, bFull);
215     parts[1] = GetItemPathW(hwndTV, hItem, &hRootKey);
216     ret = CombinePathsW((LPCWSTR *)parts, 2);
217     HeapFree(GetProcessHeap(), 0, parts[0]);
218     HeapFree(GetProcessHeap(), 0, parts[1]);
219     return ret;
220 }
221
222 static LPTSTR GetPathFullPath(HWND hwndTV, LPTSTR path) {
223     LPTSTR parts[2];
224     LPTSTR ret;
225
226     parts[0] = GetPathRoot(hwndTV, 0, TRUE);
227     parts[1] = path;
228     ret = CombinePaths((LPCTSTR *)parts, 2);
229     HeapFree(GetProcessHeap(), 0, parts[0]);
230     return ret;
231 }
232
233 static void OnTreeSelectionChanged(HWND hwndTV, HWND hwndLV, HTREEITEM hItem, BOOL bRefreshLV)
234 {
235     if (bRefreshLV) {
236         LPCTSTR keyPath;
237         HKEY hRootKey = NULL;
238         keyPath = GetItemPath(hwndTV, hItem, &hRootKey);
239         RefreshListView(hwndLV, hRootKey, keyPath, NULL);
240     }
241     UpdateStatusBar();
242 }
243
244 /*******************************************************************************
245  * finish_splitbar [internal]
246  *
247  * make the splitbar invisible and resize the windows
248  * (helper for ChildWndProc)
249  */
250 static void finish_splitbar(HWND hWnd, int x)
251 {
252     RECT rt;
253
254     draw_splitbar(hWnd, last_split);
255     last_split = -1;
256     GetClientRect(hWnd, &rt);
257     g_pChildWnd->nSplitPos = x;
258     ResizeWnd(rt.right, rt.bottom);
259     ReleaseCapture();
260 }
261
262 /*******************************************************************************
263  *
264  *  FUNCTION: _CmdWndProc(HWND, unsigned, WORD, LONG)
265  *
266  *  PURPOSE:  Processes WM_COMMAND messages for the main frame window.
267  *
268  */
269
270 static BOOL _CmdWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
271 {
272     switch (LOWORD(wParam)) {
273         /* Parse the menu selections: */
274     case ID_REGISTRY_EXIT:
275         DestroyWindow(hWnd);
276         break;
277     case ID_VIEW_REFRESH:
278         WINE_TRACE("Is this ever called or is it just dead code?\n");
279         /* TODO */
280         break;
281     case ID_SWITCH_PANELS:
282         g_pChildWnd->nFocusPanel = !g_pChildWnd->nFocusPanel;
283         SetFocus(g_pChildWnd->nFocusPanel? g_pChildWnd->hListWnd: g_pChildWnd->hTreeWnd);
284         break;
285     default:
286         return FALSE;
287     }
288     return TRUE;
289 }
290
291 /*******************************************************************************
292  *
293  *  FUNCTION: ChildWndProc(HWND, unsigned, WORD, LONG)
294  *
295  *  PURPOSE:  Processes messages for the child windows.
296  *
297  *  WM_COMMAND  - process the application menu
298  *  WM_PAINT    - Paint the main window
299  *  WM_DESTROY  - post a quit message and return
300  *
301  */
302 LRESULT CALLBACK ChildWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
303 {
304     switch (message) {
305     case WM_CREATE:
306         g_pChildWnd = HeapAlloc(GetProcessHeap(), 0, sizeof(ChildWnd));
307         if (!g_pChildWnd) return 0;
308         LoadStringW(hInst, IDS_REGISTRY_ROOT_NAME, g_pChildWnd->szPath, MAX_PATH);
309         g_pChildWnd->nSplitPos = 250;
310         g_pChildWnd->hWnd = hWnd;
311         g_pChildWnd->hTreeWnd = CreateTreeView(hWnd, g_pChildWnd->szPath, TREE_WINDOW);
312         g_pChildWnd->hListWnd = CreateListView(hWnd, LIST_WINDOW/*, g_pChildWnd->szPath*/);
313         g_pChildWnd->nFocusPanel = 1;
314         SetFocus(g_pChildWnd->hTreeWnd);
315         break;
316     case WM_COMMAND:
317         if (!_CmdWndProc(hWnd, message, wParam, lParam)) {
318             goto def;
319         }
320         break;
321     case WM_PAINT:
322         OnPaint(hWnd);
323         return 0;
324     case WM_SETCURSOR:
325         if (LOWORD(lParam) == HTCLIENT) {
326             POINT pt;
327             GetCursorPos(&pt);
328             ScreenToClient(hWnd, &pt);
329             if (pt.x>=g_pChildWnd->nSplitPos-SPLIT_WIDTH/2 && pt.x<g_pChildWnd->nSplitPos+SPLIT_WIDTH/2+1) {
330                 SetCursor(LoadCursor(0, IDC_SIZEWE));
331                 return TRUE;
332             }
333         }
334         goto def;
335     case WM_DESTROY:
336         HeapFree(GetProcessHeap(), 0, g_pChildWnd);
337         g_pChildWnd = NULL;
338         PostQuitMessage(0);
339         break;
340     case WM_LBUTTONDOWN: {
341             RECT rt;
342             int x = (short)LOWORD(lParam);
343             GetClientRect(hWnd, &rt);
344             if (x>=g_pChildWnd->nSplitPos-SPLIT_WIDTH/2 && x<g_pChildWnd->nSplitPos+SPLIT_WIDTH/2+1) {
345                 last_split = g_pChildWnd->nSplitPos;
346                 draw_splitbar(hWnd, last_split);
347                 SetCapture(hWnd);
348             }
349             break;
350         }
351
352     /* WM_RBUTTONDOWN sets the splitbar the same way as WM_LBUTTONUP */
353     case WM_LBUTTONUP:
354     case WM_RBUTTONDOWN:
355         if (GetCapture() == hWnd) {
356             finish_splitbar(hWnd, LOWORD(lParam));
357         }
358         break;
359
360     case WM_CAPTURECHANGED:
361         if (GetCapture()==hWnd && last_split>=0)
362             draw_splitbar(hWnd, last_split);
363         break;
364
365     case WM_KEYDOWN:
366         if (wParam == VK_ESCAPE)
367             if (GetCapture() == hWnd) {
368                 RECT rt;
369                 draw_splitbar(hWnd, last_split);
370                 GetClientRect(hWnd, &rt);
371                 ResizeWnd(rt.right, rt.bottom);
372                 last_split = -1;
373                 ReleaseCapture();
374                 SetCursor(LoadCursor(0, IDC_ARROW));
375             }
376         break;
377
378     case WM_MOUSEMOVE:
379         if (GetCapture() == hWnd) {
380             RECT rt;
381             int x = LOWORD(lParam);
382             HDC hdc = GetDC(hWnd);
383             GetClientRect(hWnd, &rt);
384             rt.left = last_split-SPLIT_WIDTH/2;
385             rt.right = last_split+SPLIT_WIDTH/2+1;
386             InvertRect(hdc, &rt);
387             last_split = x;
388             rt.left = x-SPLIT_WIDTH/2;
389             rt.right = x+SPLIT_WIDTH/2+1;
390             InvertRect(hdc, &rt);
391             ReleaseDC(hWnd, hdc);
392         }
393         break;
394
395     case WM_SETFOCUS:
396         if (g_pChildWnd != NULL) {
397             SetFocus(g_pChildWnd->nFocusPanel? g_pChildWnd->hListWnd: g_pChildWnd->hTreeWnd);
398         }
399         break;
400
401     case WM_TIMER:
402         break;
403
404     case WM_NOTIFY:
405         if (((int)wParam == TREE_WINDOW) && (g_pChildWnd != NULL)) {
406             switch (((LPNMHDR)lParam)->code) {
407             case TVN_ITEMEXPANDING:
408                 return !OnTreeExpanding(g_pChildWnd->hTreeWnd, (NMTREEVIEW*)lParam);
409             case TVN_SELCHANGED:
410                 OnTreeSelectionChanged(g_pChildWnd->hTreeWnd, g_pChildWnd->hListWnd,
411                     ((NMTREEVIEW *)lParam)->itemNew.hItem, TRUE);
412                 break;
413             case NM_SETFOCUS:
414                 g_pChildWnd->nFocusPanel = 0;
415                 break;
416             case NM_RCLICK: {
417                 POINT pt;
418                 GetCursorPos(&pt);
419                 TrackPopupMenu(GetSubMenu(hPopupMenus, PM_NEW),
420                                TPM_RIGHTBUTTON, pt.x, pt.y, 0, hFrameWnd, NULL);
421                 break;
422             }
423             case TVN_ENDLABELEDIT: {
424                 HKEY hRootKey;
425                 LPNMTVDISPINFO dispInfo = (LPNMTVDISPINFO)lParam;
426                 LPCTSTR path = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hRootKey);
427                 BOOL res = RenameKey(hWnd, hRootKey, path, dispInfo->item.pszText);
428                 if (res) {
429                     TVITEMEX item;
430                     LPTSTR fullPath = GetPathFullPath(g_pChildWnd->hTreeWnd,
431                      dispInfo->item.pszText);
432                     item.mask = TVIF_HANDLE | TVIF_TEXT;
433                     item.hItem = TreeView_GetSelection(g_pChildWnd->hTreeWnd);
434                     item.pszText = dispInfo->item.pszText;
435                     SendMessage( g_pChildWnd->hTreeWnd, TVM_SETITEMW, 0, (LPARAM)&item );
436                     SendMessage(hStatusBar, SB_SETTEXT, 0, (LPARAM)fullPath);
437                     HeapFree(GetProcessHeap(), 0, fullPath);
438                 }
439                 return res;
440             }
441             default:
442                 return 0; /* goto def; */
443             }
444         } else
445             if (((int)wParam == LIST_WINDOW) && (g_pChildWnd != NULL)) {
446                 if (((LPNMHDR)lParam)->code == NM_SETFOCUS) {
447                     g_pChildWnd->nFocusPanel = 1;
448                 } else if (!SendMessage(g_pChildWnd->hListWnd, WM_NOTIFY_REFLECT, wParam, lParam)) {
449                     goto def;
450                 }
451             }
452         break;
453
454     case WM_SIZE:
455         if (wParam != SIZE_MINIMIZED && g_pChildWnd != NULL) {
456             ResizeWnd(LOWORD(lParam), HIWORD(lParam));
457         }
458         /* fall through */
459 default: def:
460         return DefWindowProc(hWnd, message, wParam, lParam);
461     }
462     return 0;
463 }