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