Reverse the order for deleting the items in resetcontent to correctly
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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
36 /*******************************************************************************
37  * Local module support methods
38  */
39
40 static LPCTSTR get_root_key_name(HKEY hRootKey)
41 {
42     if (hRootKey == HKEY_CLASSES_ROOT) return _T("HKEY_CLASSES_ROOT");
43     if (hRootKey == HKEY_CURRENT_USER) return _T("HKEY_CURRENT_USER");
44     if (hRootKey == HKEY_LOCAL_MACHINE) return _T("HKEY_LOCAL_MACHINE");
45     if (hRootKey == HKEY_USERS) return _T("HKEY_USERS");
46     if (hRootKey == HKEY_CURRENT_CONFIG) return _T("HKEY_CURRENT_CONFIG");
47     if (hRootKey == HKEY_DYN_DATA) return _T("HKEY_DYN_DATA");
48     return _T("UKNOWN HKEY, PLEASE REPORT");
49 }
50
51 static void draw_splitbar(HWND hWnd, int x)
52 {
53     RECT rt;
54     HDC hdc = GetDC(hWnd);
55
56     GetClientRect(hWnd, &rt);
57     rt.left = x - SPLIT_WIDTH/2;
58     rt.right = x + SPLIT_WIDTH/2+1;
59     InvertRect(hdc, &rt);
60     ReleaseDC(hWnd, hdc);
61 }
62
63 static void ResizeWnd(ChildWnd* pChildWnd, int cx, int cy)
64 {
65     HDWP hdwp = BeginDeferWindowPos(2);
66     RECT rt = {0, 0, cx, cy};
67
68     cx = pChildWnd->nSplitPos + SPLIT_WIDTH/2;
69     DeferWindowPos(hdwp, pChildWnd->hTreeWnd, 0, rt.left, rt.top, pChildWnd->nSplitPos-SPLIT_WIDTH/2-rt.left, rt.bottom-rt.top, SWP_NOZORDER|SWP_NOACTIVATE);
70     DeferWindowPos(hdwp, pChildWnd->hListWnd, 0, rt.left+cx  , rt.top, rt.right-cx, rt.bottom-rt.top, SWP_NOZORDER|SWP_NOACTIVATE);
71     EndDeferWindowPos(hdwp);
72 }
73
74 static void OnPaint(HWND hWnd)
75 {
76     PAINTSTRUCT ps;
77     RECT rt;
78     HDC hdc;
79
80     GetClientRect(hWnd, &rt);
81     hdc = BeginPaint(hWnd, &ps);
82     FillRect(ps.hdc, &rt, GetSysColorBrush(COLOR_BTNFACE));
83     EndPaint(hWnd, &ps);
84 }
85
86 /*******************************************************************************
87  *
88  *  FUNCTION: _CmdWndProc(HWND, unsigned, WORD, LONG)
89  *
90  *  PURPOSE:  Processes WM_COMMAND messages for the main frame window.
91  *
92  */
93
94 static BOOL _CmdWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
95 {
96     ChildWnd* pChildWnd = g_pChildWnd;
97     switch (LOWORD(wParam)) {
98         /* Parse the menu selections: */
99     case ID_REGISTRY_EXIT:
100         DestroyWindow(hWnd);
101         break;
102     case ID_VIEW_REFRESH:
103         WINE_TRACE("Is this ever called or is it just dead code?\n");
104         /* TODO */
105         break;
106     case ID_SWITCH_PANELS:
107         pChildWnd->nFocusPanel = !pChildWnd->nFocusPanel;
108         SetFocus(pChildWnd->nFocusPanel? pChildWnd->hListWnd: pChildWnd->hTreeWnd);
109         break;
110     default:
111         return FALSE;
112     }
113     return TRUE;
114 }
115
116 /*******************************************************************************
117  *
118  *  FUNCTION: ChildWndProc(HWND, unsigned, WORD, LONG)
119  *
120  *  PURPOSE:  Processes messages for the child windows.
121  *
122  *  WM_COMMAND  - process the application menu
123  *  WM_PAINT    - Paint the main window
124  *  WM_DESTROY  - post a quit message and return
125  *
126  */
127 LRESULT CALLBACK ChildWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
128 {
129     static int last_split;
130     ChildWnd* pChildWnd = g_pChildWnd;
131
132     switch (message) {
133     case WM_CREATE:
134         g_pChildWnd = pChildWnd = HeapAlloc(GetProcessHeap(), 0, sizeof(ChildWnd));
135         if (!pChildWnd) return 0;
136         _tcsncpy(pChildWnd->szPath, _T("My Computer"), MAX_PATH);
137         pChildWnd->nSplitPos = 250;
138         pChildWnd->hWnd = hWnd;
139         pChildWnd->hTreeWnd = CreateTreeView(hWnd, pChildWnd->szPath, TREE_WINDOW);
140         pChildWnd->hListWnd = CreateListView(hWnd, LIST_WINDOW/*, pChildWnd->szPath*/);
141         SetFocus(pChildWnd->hTreeWnd);
142         break;
143     case WM_COMMAND:
144         if (!_CmdWndProc(hWnd, message, wParam, lParam)) {
145             goto def;
146         }
147         break;
148     case WM_PAINT:
149         OnPaint(hWnd);
150         return 0;
151     case WM_SETCURSOR:
152         if (LOWORD(lParam) == HTCLIENT) {
153             POINT pt;
154             GetCursorPos(&pt);
155             ScreenToClient(hWnd, &pt);
156             if (pt.x>=pChildWnd->nSplitPos-SPLIT_WIDTH/2 && pt.x<pChildWnd->nSplitPos+SPLIT_WIDTH/2+1) {
157                 SetCursor(LoadCursor(0, IDC_SIZEWE));
158                 return TRUE;
159             }
160         }
161         goto def;
162     case WM_DESTROY:
163         HeapFree(GetProcessHeap(), 0, pChildWnd);
164         pChildWnd = NULL;
165         PostQuitMessage(0);
166         break;
167     case WM_LBUTTONDOWN: {
168             RECT rt;
169             int x = LOWORD(lParam);
170             GetClientRect(hWnd, &rt);
171             if (x>=pChildWnd->nSplitPos-SPLIT_WIDTH/2 && x<pChildWnd->nSplitPos+SPLIT_WIDTH/2+1) {
172                 last_split = pChildWnd->nSplitPos;
173                 draw_splitbar(hWnd, last_split);
174                 SetCapture(hWnd);
175             }
176             break;
177         }
178
179     case WM_LBUTTONUP:
180         if (GetCapture() == hWnd) {
181             RECT rt;
182             int x = LOWORD(lParam);
183             draw_splitbar(hWnd, last_split);
184             last_split = -1;
185             GetClientRect(hWnd, &rt);
186             pChildWnd->nSplitPos = x;
187             ResizeWnd(pChildWnd, rt.right, rt.bottom);
188             ReleaseCapture();
189         }
190         break;
191
192     case WM_CAPTURECHANGED:
193         if (GetCapture()==hWnd && last_split>=0)
194             draw_splitbar(hWnd, last_split);
195         break;
196
197     case WM_KEYDOWN:
198         if (wParam == VK_ESCAPE)
199             if (GetCapture() == hWnd) {
200                 RECT rt;
201                 draw_splitbar(hWnd, last_split);
202                 GetClientRect(hWnd, &rt);
203                 ResizeWnd(pChildWnd, rt.right, rt.bottom);
204                 last_split = -1;
205                 ReleaseCapture();
206                 SetCursor(LoadCursor(0, IDC_ARROW));
207             }
208         break;
209
210     case WM_MOUSEMOVE:
211         if (GetCapture() == hWnd) {
212             RECT rt;
213             int x = LOWORD(lParam);
214             HDC hdc = GetDC(hWnd);
215             GetClientRect(hWnd, &rt);
216             rt.left = last_split-SPLIT_WIDTH/2;
217             rt.right = last_split+SPLIT_WIDTH/2+1;
218             InvertRect(hdc, &rt);
219             last_split = x;
220             rt.left = x-SPLIT_WIDTH/2;
221             rt.right = x+SPLIT_WIDTH/2+1;
222             InvertRect(hdc, &rt);
223             ReleaseDC(hWnd, hdc);
224         }
225         break;
226
227     case WM_SETFOCUS:
228         if (pChildWnd != NULL) {
229             SetFocus(pChildWnd->nFocusPanel? pChildWnd->hListWnd: pChildWnd->hTreeWnd);
230         }
231         break;
232
233     case WM_TIMER:
234         break;
235
236     case WM_NOTIFY:
237         if ((int)wParam == TREE_WINDOW) {
238             switch (((LPNMHDR)lParam)->code) {
239             case TVN_ITEMEXPANDING:
240                 return !OnTreeExpanding(pChildWnd->hTreeWnd, (NMTREEVIEW*)lParam);
241             case TVN_SELCHANGED: {
242                     LPCTSTR keyPath, rootName;
243                     LPTSTR fullPath;
244                     HKEY hRootKey;
245
246                     keyPath = GetItemPath(pChildWnd->hTreeWnd, ((NMTREEVIEW*)lParam)->itemNew.hItem, &hRootKey);
247                     if (keyPath) {
248                         RefreshListView(pChildWnd->hListWnd, hRootKey, keyPath, NULL);
249                         rootName = get_root_key_name(hRootKey);
250                         fullPath = HeapAlloc(GetProcessHeap(), 0, (lstrlen(rootName) + 1 + lstrlen(keyPath) + 1) * sizeof(TCHAR));
251                         if (fullPath) {
252                             _stprintf(fullPath, "%s\\%s", rootName, keyPath);
253                             SendMessage(hStatusBar, SB_SETTEXT, 0, (LPARAM)fullPath);
254                             HeapFree(GetProcessHeap(), 0, fullPath);
255                         }
256                     }
257                 }
258                 break;
259             case NM_SETFOCUS:
260                 pChildWnd->nFocusPanel = 0;
261                 break;
262             case NM_RCLICK: {
263                 POINT pt;
264                 GetCursorPos(&pt);
265                 TrackPopupMenu(GetSubMenu(hPopupMenus, PM_NEW),
266                                TPM_RIGHTBUTTON, pt.x, pt.y, 0, hFrameWnd, NULL);
267                 break;
268             }
269             case TVN_ENDLABELEDIT: {
270                 HKEY hRootKey;
271                 LPNMTVDISPINFO dispInfo = (LPNMTVDISPINFO)lParam;
272                 LPCTSTR path = GetItemPath(pChildWnd->hTreeWnd, 0, &hRootKey);
273                 BOOL res = RenameKey(hWnd, hRootKey, path, dispInfo->item.pszText);
274                 if (res) {
275                     TVITEMEX item;
276                     item.mask = TVIF_HANDLE | TVIF_TEXT;
277                     item.hItem = TreeView_GetSelection(pChildWnd->hTreeWnd);
278                     item.pszText = dispInfo->item.pszText;
279                     TreeView_SetItem(pChildWnd->hTreeWnd, &item);
280                 }
281                 return res;
282             }
283             default:
284                 return 0; /* goto def; */
285             }
286         } else
287             if ((int)wParam == LIST_WINDOW) {
288                 if (((LPNMHDR)lParam)->code == NM_SETFOCUS) {
289                     pChildWnd->nFocusPanel = 1;
290                 } else if (!SendMessage(pChildWnd->hListWnd, WM_NOTIFY_REFLECT, wParam, lParam)) {
291                     goto def;
292                 }
293             }
294         break;
295
296     case WM_SIZE:
297         if (wParam != SIZE_MINIMIZED && pChildWnd != NULL) {
298             ResizeWnd(pChildWnd, LOWORD(lParam), HIWORD(lParam));
299         }
300         /* fall through */
301 default: def:
302         return DefWindowProc(hWnd, message, wParam, lParam);
303     }
304     return 0;
305 }