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