Fix the resources to remove extraneous spaces in '...'.
[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 static void draw_splitbar(HWND hWnd, int x)
53 {
54     RECT rt;
55     HDC hdc = GetDC(hWnd);
56
57     GetClientRect(hWnd, &rt);
58     rt.left = x - SPLIT_WIDTH/2;
59     rt.right = x + SPLIT_WIDTH/2+1;
60     InvertRect(hdc, &rt);
61     ReleaseDC(hWnd, hdc);
62 }
63
64 static void ResizeWnd(int cx, int cy)
65 {
66     HDWP hdwp = BeginDeferWindowPos(2);
67     RECT rt = {0, 0, cx, cy};
68
69     cx = g_pChildWnd->nSplitPos + SPLIT_WIDTH/2;
70     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);
71     DeferWindowPos(hdwp, g_pChildWnd->hListWnd, 0, rt.left+cx  , rt.top, rt.right-cx, rt.bottom-rt.top, SWP_NOZORDER|SWP_NOACTIVATE);
72     EndDeferWindowPos(hdwp);
73 }
74
75 static void OnPaint(HWND hWnd)
76 {
77     PAINTSTRUCT ps;
78     RECT rt;
79     HDC hdc;
80
81     GetClientRect(hWnd, &rt);
82     hdc = BeginPaint(hWnd, &ps);
83     FillRect(ps.hdc, &rt, GetSysColorBrush(COLOR_BTNFACE));
84     EndPaint(hWnd, &ps);
85 }
86
87 static LPTSTR CombinePaths(LPCTSTR pPaths[], int nPaths) {
88     int i, len, pos;
89     LPTSTR combined;
90     for (i=0, len=0; i<nPaths; i++) {
91         if (pPaths[i] && *pPaths[i]) {
92             len += lstrlen(pPaths[i])+1;
93         }
94     }
95     combined = HeapAlloc(GetProcessHeap(), 0, len * sizeof(TCHAR));
96     *combined = '\0';
97     for (i=0, pos=0; i<nPaths; i++) {
98         if (pPaths[i] && *pPaths[i]) {
99             int llen = _tcslen(pPaths[i]);
100             if (!*combined)
101                 _tcscpy(combined, pPaths[i]);
102             else {
103                 combined[pos++] = (TCHAR)'\\';
104                 _tcscpy(combined+pos, pPaths[i]);
105             }
106             pos += llen;
107         }
108     }
109     return combined;
110 }
111
112 static LPTSTR GetPathRoot(HWND hwndTV, HTREEITEM hItem, BOOL bFull) {
113     LPCTSTR parts[2] = {_T(""), _T("")};
114     TCHAR text[260];
115     HKEY hRootKey = NULL;
116     if (!hItem)
117         hItem = TreeView_GetSelection(hwndTV);
118     GetItemPath(hwndTV, hItem, &hRootKey);
119     if (!bFull && !hRootKey)
120         return NULL;
121     if (hRootKey)
122         parts[1] = GetRootKeyName(hRootKey);
123     if (bFull) {
124         DWORD dwSize = sizeof(text)/sizeof(TCHAR);
125         GetComputerName(text, &dwSize);
126         parts[0] = text;
127     }
128     return CombinePaths(parts, 2);
129 }
130
131 LPTSTR GetItemFullPath(HWND hwndTV, HTREEITEM hItem, BOOL bFull) {
132     LPTSTR parts[2];
133     LPTSTR ret;
134     HKEY hRootKey = NULL;
135
136     parts[0] = GetPathRoot(hwndTV, hItem, bFull);
137     parts[1] = GetItemPath(hwndTV, hItem, &hRootKey);
138     ret = CombinePaths((LPCTSTR *)parts, 2);
139     HeapFree(GetProcessHeap(), 0, parts[0]);
140     return ret;
141 }
142
143 static LPTSTR GetPathFullPath(HWND hwndTV, LPTSTR path) {
144     LPTSTR parts[2];
145     LPTSTR ret;
146
147     parts[0] = GetPathRoot(hwndTV, 0, TRUE);
148     parts[1] = path;
149     ret = CombinePaths((LPCTSTR *)parts, 2);
150     HeapFree(GetProcessHeap(), 0, parts[0]);
151     return ret;
152 }
153
154 static void OnTreeSelectionChanged(HWND hwndTV, HWND hwndLV, HTREEITEM hItem, BOOL bRefreshLV)
155 {
156     if (bRefreshLV) {
157         LPCTSTR keyPath;
158         HKEY hRootKey = NULL;
159         keyPath = GetItemPath(hwndTV, hItem, &hRootKey);
160         RefreshListView(hwndLV, hRootKey, keyPath, NULL);
161     }
162     UpdateStatusBar();
163 }
164
165 /*******************************************************************************
166  * finish_splitbar [internal]
167  *
168  * make the splitbar invisible and resize the windows
169  * (helper for ChildWndProc)
170  */
171 static void finish_splitbar(HWND hWnd, int x)
172 {
173     RECT rt;
174
175     draw_splitbar(hWnd, last_split);
176     last_split = -1;
177     GetClientRect(hWnd, &rt);
178     g_pChildWnd->nSplitPos = x;
179     ResizeWnd(rt.right, rt.bottom);
180     ReleaseCapture();
181 }
182
183 /*******************************************************************************
184  *
185  *  FUNCTION: _CmdWndProc(HWND, unsigned, WORD, LONG)
186  *
187  *  PURPOSE:  Processes WM_COMMAND messages for the main frame window.
188  *
189  */
190
191 static BOOL _CmdWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
192 {
193     switch (LOWORD(wParam)) {
194         /* Parse the menu selections: */
195     case ID_REGISTRY_EXIT:
196         DestroyWindow(hWnd);
197         break;
198     case ID_VIEW_REFRESH:
199         WINE_TRACE("Is this ever called or is it just dead code?\n");
200         /* TODO */
201         break;
202     case ID_SWITCH_PANELS:
203         g_pChildWnd->nFocusPanel = !g_pChildWnd->nFocusPanel;
204         SetFocus(g_pChildWnd->nFocusPanel? g_pChildWnd->hListWnd: g_pChildWnd->hTreeWnd);
205         break;
206     default:
207         return FALSE;
208     }
209     return TRUE;
210 }
211
212 /*******************************************************************************
213  *
214  *  FUNCTION: ChildWndProc(HWND, unsigned, WORD, LONG)
215  *
216  *  PURPOSE:  Processes messages for the child windows.
217  *
218  *  WM_COMMAND  - process the application menu
219  *  WM_PAINT    - Paint the main window
220  *  WM_DESTROY  - post a quit message and return
221  *
222  */
223 LRESULT CALLBACK ChildWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
224 {
225     switch (message) {
226     case WM_CREATE:
227         g_pChildWnd = HeapAlloc(GetProcessHeap(), 0, sizeof(ChildWnd));
228         if (!g_pChildWnd) return 0;
229         LoadString(hInst, IDS_REGISTRY_ROOT_NAME, g_pChildWnd->szPath, MAX_PATH);
230         g_pChildWnd->nSplitPos = 250;
231         g_pChildWnd->hWnd = hWnd;
232         g_pChildWnd->hTreeWnd = CreateTreeView(hWnd, g_pChildWnd->szPath, TREE_WINDOW);
233         g_pChildWnd->hListWnd = CreateListView(hWnd, LIST_WINDOW/*, g_pChildWnd->szPath*/);
234         g_pChildWnd->nFocusPanel = 1;
235         SetFocus(g_pChildWnd->hTreeWnd);
236         break;
237     case WM_COMMAND:
238         if (!_CmdWndProc(hWnd, message, wParam, lParam)) {
239             goto def;
240         }
241         break;
242     case WM_PAINT:
243         OnPaint(hWnd);
244         return 0;
245     case WM_SETCURSOR:
246         if (LOWORD(lParam) == HTCLIENT) {
247             POINT pt;
248             GetCursorPos(&pt);
249             ScreenToClient(hWnd, &pt);
250             if (pt.x>=g_pChildWnd->nSplitPos-SPLIT_WIDTH/2 && pt.x<g_pChildWnd->nSplitPos+SPLIT_WIDTH/2+1) {
251                 SetCursor(LoadCursor(0, IDC_SIZEWE));
252                 return TRUE;
253             }
254         }
255         goto def;
256     case WM_DESTROY:
257         HeapFree(GetProcessHeap(), 0, g_pChildWnd);
258         g_pChildWnd = NULL;
259         PostQuitMessage(0);
260         break;
261     case WM_LBUTTONDOWN: {
262             RECT rt;
263             int x = (short)LOWORD(lParam);
264             GetClientRect(hWnd, &rt);
265             if (x>=g_pChildWnd->nSplitPos-SPLIT_WIDTH/2 && x<g_pChildWnd->nSplitPos+SPLIT_WIDTH/2+1) {
266                 last_split = g_pChildWnd->nSplitPos;
267                 draw_splitbar(hWnd, last_split);
268                 SetCapture(hWnd);
269             }
270             break;
271         }
272
273     /* WM_RBUTTONDOWN sets the splitbar the same way as WM_LBUTTONUP */
274     case WM_LBUTTONUP:
275     case WM_RBUTTONDOWN:
276         if (GetCapture() == hWnd) {
277             finish_splitbar(hWnd, LOWORD(lParam));
278         }
279         break;
280
281     case WM_CAPTURECHANGED:
282         if (GetCapture()==hWnd && last_split>=0)
283             draw_splitbar(hWnd, last_split);
284         break;
285
286     case WM_KEYDOWN:
287         if (wParam == VK_ESCAPE)
288             if (GetCapture() == hWnd) {
289                 RECT rt;
290                 draw_splitbar(hWnd, last_split);
291                 GetClientRect(hWnd, &rt);
292                 ResizeWnd(rt.right, rt.bottom);
293                 last_split = -1;
294                 ReleaseCapture();
295                 SetCursor(LoadCursor(0, IDC_ARROW));
296             }
297         break;
298
299     case WM_MOUSEMOVE:
300         if (GetCapture() == hWnd) {
301             RECT rt;
302             int x = LOWORD(lParam);
303             HDC hdc = GetDC(hWnd);
304             GetClientRect(hWnd, &rt);
305             rt.left = last_split-SPLIT_WIDTH/2;
306             rt.right = last_split+SPLIT_WIDTH/2+1;
307             InvertRect(hdc, &rt);
308             last_split = x;
309             rt.left = x-SPLIT_WIDTH/2;
310             rt.right = x+SPLIT_WIDTH/2+1;
311             InvertRect(hdc, &rt);
312             ReleaseDC(hWnd, hdc);
313         }
314         break;
315
316     case WM_SETFOCUS:
317         if (g_pChildWnd != NULL) {
318             SetFocus(g_pChildWnd->nFocusPanel? g_pChildWnd->hListWnd: g_pChildWnd->hTreeWnd);
319         }
320         break;
321
322     case WM_TIMER:
323         break;
324
325     case WM_NOTIFY:
326         if (((int)wParam == TREE_WINDOW) && (g_pChildWnd != NULL)) {
327             switch (((LPNMHDR)lParam)->code) {
328             case TVN_ITEMEXPANDING:
329                 return !OnTreeExpanding(g_pChildWnd->hTreeWnd, (NMTREEVIEW*)lParam);
330             case TVN_SELCHANGED:
331                 OnTreeSelectionChanged(g_pChildWnd->hTreeWnd, g_pChildWnd->hListWnd,
332                     ((NMTREEVIEW *)lParam)->itemNew.hItem, TRUE);
333                 break;
334             case NM_SETFOCUS:
335                 g_pChildWnd->nFocusPanel = 0;
336                 break;
337             case NM_RCLICK: {
338                 POINT pt;
339                 GetCursorPos(&pt);
340                 TrackPopupMenu(GetSubMenu(hPopupMenus, PM_NEW),
341                                TPM_RIGHTBUTTON, pt.x, pt.y, 0, hFrameWnd, NULL);
342                 break;
343             }
344             case TVN_ENDLABELEDIT: {
345                 HKEY hRootKey;
346                 LPNMTVDISPINFO dispInfo = (LPNMTVDISPINFO)lParam;
347                 LPCTSTR path = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hRootKey);
348                 BOOL res = RenameKey(hWnd, hRootKey, path, dispInfo->item.pszText);
349                 if (res) {
350                     TVITEMEX item;
351                     LPTSTR fullPath = GetPathFullPath(g_pChildWnd->hTreeWnd,
352                      dispInfo->item.pszText);
353                     item.mask = TVIF_HANDLE | TVIF_TEXT;
354                     item.hItem = TreeView_GetSelection(g_pChildWnd->hTreeWnd);
355                     item.pszText = dispInfo->item.pszText;
356                     SendMessage( g_pChildWnd->hTreeWnd, TVM_SETITEMW, 0, (LPARAM)&item );
357                     SendMessage(hStatusBar, SB_SETTEXT, 0, (LPARAM)fullPath);
358                     HeapFree(GetProcessHeap(), 0, fullPath);
359                 }
360                 return res;
361             }
362             default:
363                 return 0; /* goto def; */
364             }
365         } else
366             if (((int)wParam == LIST_WINDOW) && (g_pChildWnd != NULL)) {
367                 if (((LPNMHDR)lParam)->code == NM_SETFOCUS) {
368                     g_pChildWnd->nFocusPanel = 1;
369                 } else if (!SendMessage(g_pChildWnd->hListWnd, WM_NOTIFY_REFLECT, wParam, lParam)) {
370                     goto def;
371                 }
372             }
373         break;
374
375     case WM_SIZE:
376         if (wParam != SIZE_MINIMIZED && g_pChildWnd != NULL) {
377             ResizeWnd(LOWORD(lParam), HIWORD(lParam));
378         }
379         /* fall through */
380 default: def:
381         return DefWindowProc(hWnd, message, wParam, lParam);
382     }
383     return 0;
384 }