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