winegstreamer: Make IMFTransform_ProcessOutput checks more consistent.
[wine.git] / programs / regedit / childwnd.c
blobc243a76c8073eb885e47ea78adf32643a6022fc3
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>
25 #include "main.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(regedit);
31 ChildWnd* g_pChildWnd;
32 static int last_split;
34 static const WCHAR wszLastKey[] = L"LastKey";
35 static const WCHAR wszKeyName[] = L"Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\Regedit";
37 /*******************************************************************************
38 * Local module support methods
41 static LPCWSTR GetRootKeyName(HKEY hRootKey)
43 if(hRootKey == HKEY_CLASSES_ROOT)
44 return reg_class_namesW[INDEX_HKEY_CLASSES_ROOT];
45 if(hRootKey == HKEY_CURRENT_USER)
46 return reg_class_namesW[INDEX_HKEY_CURRENT_USER];
47 if(hRootKey == HKEY_LOCAL_MACHINE)
48 return reg_class_namesW[INDEX_HKEY_LOCAL_MACHINE];
49 if(hRootKey == HKEY_USERS)
50 return reg_class_namesW[INDEX_HKEY_USERS];
51 if(hRootKey == HKEY_CURRENT_CONFIG)
52 return reg_class_namesW[INDEX_HKEY_CURRENT_CONFIG];
53 if(hRootKey == HKEY_DYN_DATA)
54 return reg_class_namesW[INDEX_HKEY_DYN_DATA];
55 else
56 return L"Unknown HKEY. Please report.";
59 static void draw_splitbar(HWND hWnd, int x)
61 RECT rt;
62 HDC hdc = GetDC(hWnd);
64 GetClientRect(hWnd, &rt);
65 rt.left = x - SPLIT_WIDTH/2;
66 rt.right = x + SPLIT_WIDTH/2+1;
67 InvertRect(hdc, &rt);
68 ReleaseDC(hWnd, hdc);
71 static void ResizeWnd(int cx, int cy)
73 HDWP hdwp = BeginDeferWindowPos(2);
74 RECT rt = {0, 0, cx, cy};
76 cx = g_pChildWnd->nSplitPos + SPLIT_WIDTH/2;
77 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);
78 DeferWindowPos(hdwp, g_pChildWnd->hListWnd, 0, rt.left+cx , rt.top, rt.right-cx, rt.bottom-rt.top, SWP_NOZORDER|SWP_NOACTIVATE);
79 EndDeferWindowPos(hdwp);
82 static void OnPaint(HWND hWnd)
84 PAINTSTRUCT ps;
85 RECT rt;
87 GetClientRect(hWnd, &rt);
88 BeginPaint(hWnd, &ps);
89 FillRect(ps.hdc, &rt, GetSysColorBrush(COLOR_BTNFACE));
90 EndPaint(hWnd, &ps);
93 static LPWSTR CombinePaths(LPCWSTR pPaths[], int nPaths) {
94 int i, len, pos;
95 LPWSTR combined;
96 for (i=0, len=0; i<nPaths; i++) {
97 if (pPaths[i] && *pPaths[i]) {
98 len += lstrlenW(pPaths[i])+1;
101 combined = malloc(len * sizeof(WCHAR));
102 *combined = '\0';
103 for (i=0, pos=0; i<nPaths; i++) {
104 if (pPaths[i] && *pPaths[i]) {
105 int llen = lstrlenW(pPaths[i]);
106 if (!*combined)
107 lstrcpyW(combined, pPaths[i]);
108 else {
109 combined[pos++] = '\\';
110 lstrcpyW(combined+pos, pPaths[i]);
112 pos += llen;
115 return combined;
118 static LPWSTR GetPathRoot(HWND hwndTV, HTREEITEM hItem, BOOL bFull) {
119 LPCWSTR parts[2] = {0,0};
120 WCHAR text[260];
121 HKEY hRootKey = NULL;
122 if (!hItem)
123 hItem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_CARET, 0);
124 free(GetItemPath(hwndTV, hItem, &hRootKey));
125 if (!bFull && !hRootKey)
126 return NULL;
127 if (hRootKey)
128 parts[1] = GetRootKeyName(hRootKey);
129 if (bFull) {
130 DWORD dwSize = ARRAY_SIZE(text);
131 GetComputerNameW(text, &dwSize);
132 parts[0] = text;
134 return CombinePaths(parts, 2);
137 LPWSTR GetItemFullPath(HWND hwndTV, HTREEITEM hItem, BOOL bFull) {
138 LPWSTR parts[2];
139 LPWSTR ret;
140 HKEY hRootKey = NULL;
142 parts[0] = GetPathRoot(hwndTV, hItem, bFull);
143 parts[1] = GetItemPath(hwndTV, hItem, &hRootKey);
144 ret = CombinePaths((LPCWSTR *)parts, 2);
145 free(parts[0]);
146 free(parts[1]);
147 return ret;
150 static void OnTreeSelectionChanged(HWND hwndTV, HWND hwndLV, HTREEITEM hItem, BOOL bRefreshLV)
152 if (bRefreshLV) {
153 LPWSTR keyPath;
154 HKEY hRootKey = NULL;
155 HTREEITEM rootitem;
157 rootitem = (HTREEITEM)SendMessageW(hwndTV, TVM_GETNEXTITEM, TVGN_ROOT, 0);
158 if (rootitem == hItem)
160 SendMessageW(hwndLV, LVM_DELETEALLITEMS, 0, 0);
161 UpdateStatusBar();
162 return;
165 keyPath = GetItemPath(hwndTV, hItem, &hRootKey);
166 RefreshListView(hwndLV, hRootKey, keyPath, NULL);
167 free(keyPath);
169 UpdateStatusBar();
172 /*******************************************************************************
173 * finish_splitbar [internal]
175 * make the splitbar invisible and resize the windows
176 * (helper for ChildWndProc)
178 static void finish_splitbar(HWND hWnd, int x)
180 RECT rt;
182 draw_splitbar(hWnd, last_split);
183 last_split = -1;
184 GetClientRect(hWnd, &rt);
185 g_pChildWnd->nSplitPos = x;
186 ResizeWnd(rt.right, rt.bottom);
187 ReleaseCapture();
190 /*******************************************************************************
192 * FUNCTION: _CmdWndProc(HWND, unsigned, WORD, LONG)
194 * PURPOSE: Processes WM_COMMAND messages for the main frame window.
198 static BOOL _CmdWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
200 switch (LOWORD(wParam)) {
201 /* Parse the menu selections: */
202 case ID_REGISTRY_EXIT:
203 DestroyWindow(hWnd);
204 break;
205 case ID_VIEW_REFRESH:
206 WINE_TRACE("Is this ever called or is it just dead code?\n");
207 /* TODO */
208 break;
209 case ID_SWITCH_PANELS:
210 g_pChildWnd->nFocusPanel = !g_pChildWnd->nFocusPanel;
211 SetFocus(g_pChildWnd->nFocusPanel? g_pChildWnd->hListWnd: g_pChildWnd->hTreeWnd);
212 break;
213 default:
214 return FALSE;
216 return TRUE;
219 /*******************************************************************************
220 * get_last_key [internal]
222 * open last key
225 static void get_last_key(HWND hwndTV)
227 HKEY hkey;
228 WCHAR wszVal[KEY_MAX_LEN];
229 DWORD dwSize = sizeof(wszVal);
231 if (RegCreateKeyExW(HKEY_CURRENT_USER, wszKeyName, 0, NULL, 0, KEY_READ, NULL, &hkey, NULL) == ERROR_SUCCESS)
233 HTREEITEM selection = NULL;
234 if (RegQueryValueExW(hkey, wszLastKey, NULL, NULL, (LPBYTE)wszVal, &dwSize) == ERROR_SUCCESS)
236 if (lstrcmpW(wszVal, g_pChildWnd->szPath))
237 selection = FindPathInTree(hwndTV, wszVal);
240 if(!selection)
242 selection = (HTREEITEM)SendMessageW(g_pChildWnd->hTreeWnd, TVM_GETNEXTITEM, TVGN_ROOT, 0);
243 SendMessageW(hwndTV, TVM_EXPAND, TVE_EXPAND, (LPARAM)selection );
245 else
246 SendMessageW(hwndTV, TVM_SELECTITEM, TVGN_CARET, (LPARAM)selection);
248 RegCloseKey(hkey);
252 /*******************************************************************************
253 * set_last_key [internal]
255 * save last key
258 static void set_last_key(HWND hwndTV)
260 HKEY hkey;
262 if (RegCreateKeyExW(HKEY_CURRENT_USER, wszKeyName, 0, NULL, 0, KEY_WRITE, NULL, &hkey, NULL) == ERROR_SUCCESS)
264 HTREEITEM selection = (HTREEITEM)SendMessageW(g_pChildWnd->hTreeWnd, TVM_GETNEXTITEM, TVGN_CARET, 0);
265 HTREEITEM root = (HTREEITEM)SendMessageW(g_pChildWnd->hTreeWnd, TVM_GETNEXTITEM, TVGN_ROOT, 0);
266 WCHAR *value;
268 if (selection == root)
269 value = g_pChildWnd->szPath;
270 else
271 value = GetItemFullPath(g_pChildWnd->hTreeWnd, selection, FALSE);
272 RegSetValueExW(hkey, wszLastKey, 0, REG_SZ, (LPBYTE)value, (lstrlenW(value) + 1) * sizeof(WCHAR));
273 if (selection != root)
274 free(value);
275 RegCloseKey(hkey);
279 static int treeview_notify(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
281 switch (((NMHDR *)lParam)->code)
283 case NM_SETFOCUS:
284 g_pChildWnd->nFocusPanel = 0;
285 break;
286 case TVN_BEGINLABELEDITW:
288 HKEY hRootKey;
289 WCHAR *path;
291 if (!GetWindowLongPtrW(g_pChildWnd->hTreeWnd, GWLP_USERDATA))
292 return 1;
294 path = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hRootKey);
295 if (!path || !*path)
296 return 1;
297 return 0;
299 case TVN_ENDLABELEDITW:
301 HKEY hRootKey;
302 NMTVDISPINFOW *dispInfo = (NMTVDISPINFOW *)lParam;
303 WCHAR *path = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hRootKey);
304 BOOL res = RenameKey(hWnd, hRootKey, path, dispInfo->item.pszText);
306 free(path);
308 if (res)
310 TVITEMW item;
312 item.mask = TVIF_HANDLE | TVIF_TEXT;
313 item.hItem = dispInfo->item.hItem;
314 item.pszText = dispInfo->item.pszText;
315 SendMessageW(g_pChildWnd->hTreeWnd, TVM_SETITEMW, 0, (LPARAM)&item);
317 path = GetItemPath(g_pChildWnd->hTreeWnd, 0, &hRootKey);
318 update_listview_path(path);
319 free(path);
321 UpdateStatusBar();
324 SetWindowLongPtrW(g_pChildWnd->hTreeWnd, GWLP_USERDATA, 0);
325 return res;
327 case TVN_ITEMEXPANDINGW:
328 return !OnTreeExpanding(g_pChildWnd->hTreeWnd, (NMTREEVIEWW *)lParam);
329 case TVN_SELCHANGEDW:
330 OnTreeSelectionChanged(g_pChildWnd->hTreeWnd, g_pChildWnd->hListWnd,
331 ((NMTREEVIEWW *)lParam)->itemNew.hItem, TRUE);
332 break;
334 return 0;
337 static int listview_notify(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
339 switch (((NMHDR *)lParam)->code)
341 case NM_DBLCLK:
343 NMITEMACTIVATE *nmitem = (NMITEMACTIVATE *)lParam;
345 if (nmitem->iItem != -1)
347 LVITEMW item;
349 item.state = 0;
350 item.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
351 SendMessageW(g_pChildWnd->hListWnd, LVM_SETITEMSTATE, (UINT)-1, (LPARAM)&item);
353 item.state = LVIS_FOCUSED | LVIS_SELECTED;
354 item.stateMask = LVIS_FOCUSED | LVIS_SELECTED;
355 SendMessageW(g_pChildWnd->hListWnd, LVM_SETITEMSTATE, nmitem->iItem, (LPARAM)&item);
357 SendMessageW(hFrameWnd, WM_COMMAND, ID_EDIT_MODIFY, 0);
359 break;
361 case NM_RETURN:
363 int cnt = SendMessageW(g_pChildWnd->hListWnd, LVM_GETNEXTITEM, -1,
364 MAKELPARAM(LVNI_FOCUSED | LVNI_SELECTED, 0));
365 if (cnt != -1)
366 SendMessageW(hFrameWnd, WM_COMMAND, ID_EDIT_MODIFY, 0);
367 break;
369 case NM_SETFOCUS:
370 g_pChildWnd->nFocusPanel = 1;
371 break;
372 case LVN_BEGINLABELEDITW:
373 if (!((NMLVDISPINFOW *)lParam)->item.iItem)
374 return 1;
375 return 0;
376 case LVN_COLUMNCLICK:
377 if (g_columnToSort == ((NMLISTVIEW *)lParam)->iSubItem)
378 g_invertSort = !g_invertSort;
379 else
381 g_columnToSort = ((NMLISTVIEW *)lParam)->iSubItem;
382 g_invertSort = FALSE;
385 SendMessageW(g_pChildWnd->hListWnd, LVM_SORTITEMS,
386 (WPARAM)g_pChildWnd->hListWnd, (LPARAM)CompareFunc);
387 break;
388 case LVN_DELETEITEM:
390 NMLISTVIEW *nmlv = (NMLISTVIEW *)lParam;
391 LINE_INFO *info = (LINE_INFO *)nmlv->lParam;
393 free(info->name);
394 free(info->val);
395 free(info);
396 break;
398 case LVN_ENDLABELEDITW:
400 NMLVDISPINFOW *dispInfo = (NMLVDISPINFOW *)lParam;
401 WCHAR *oldName = GetItemText(g_pChildWnd->hListWnd, dispInfo->item.iItem);
402 LONG ret;
404 if (!oldName) return -1; /* cannot rename a default value */
405 ret = RenameValue(g_pChildWnd->hListWnd, g_currentRootKey, g_currentPath,
406 oldName, dispInfo->item.pszText);
407 if (ret)
409 dispInfo->item.iSubItem = 0;
410 SendMessageW(g_pChildWnd->hListWnd, LVM_SETITEMTEXTW,
411 dispInfo->item.iItem, (LPARAM)&dispInfo->item);
414 free(oldName);
415 return 0;
417 case LVN_GETDISPINFOW:
418 OnGetDispInfo((NMLVDISPINFOW *)lParam);
419 break;
421 return 0;
424 #define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
425 #define GET_Y_LPARAM(lp) ((int)(short)HIWORD(lp))
427 /*******************************************************************************
429 * FUNCTION: ChildWndProc(HWND, unsigned, WORD, LONG)
431 * PURPOSE: Processes messages for the child windows.
433 * WM_COMMAND - process the application menu
434 * WM_PAINT - Paint the main window
435 * WM_DESTROY - post a quit message and return
438 LRESULT CALLBACK ChildWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
440 switch (message) {
441 case WM_CREATE:
442 g_pChildWnd = malloc(sizeof(ChildWnd));
443 if (!g_pChildWnd) return 0;
444 LoadStringW(hInst, IDS_REGISTRY_ROOT_NAME, g_pChildWnd->szPath, MAX_PATH);
445 g_pChildWnd->nSplitPos = 250;
446 g_pChildWnd->hWnd = hWnd;
447 g_pChildWnd->hTreeWnd = CreateTreeView(hWnd, g_pChildWnd->szPath, TREE_WINDOW);
448 g_pChildWnd->hListWnd = CreateListView(hWnd, LIST_WINDOW/*, g_pChildWnd->szPath*/);
449 g_pChildWnd->nFocusPanel = 1;
450 SetFocus(g_pChildWnd->hTreeWnd);
451 get_last_key(g_pChildWnd->hTreeWnd);
452 break;
453 case WM_COMMAND:
454 if (!_CmdWndProc(hWnd, message, wParam, lParam)) {
455 goto def;
457 break;
458 case WM_PAINT:
459 OnPaint(hWnd);
460 return 0;
461 case WM_SETCURSOR:
462 if (LOWORD(lParam) == HTCLIENT) {
463 POINT pt;
464 GetCursorPos(&pt);
465 ScreenToClient(hWnd, &pt);
466 if (pt.x>=g_pChildWnd->nSplitPos-SPLIT_WIDTH/2 && pt.x<g_pChildWnd->nSplitPos+SPLIT_WIDTH/2+1) {
467 SetCursor(LoadCursorW(0, (LPCWSTR)IDC_SIZEWE));
468 return TRUE;
471 goto def;
472 case WM_DESTROY:
473 set_last_key(g_pChildWnd->hTreeWnd);
474 free(g_pChildWnd);
475 g_pChildWnd = NULL;
476 PostQuitMessage(0);
477 break;
478 case WM_LBUTTONDOWN: {
479 RECT rt;
480 int x = (short)LOWORD(lParam);
481 GetClientRect(hWnd, &rt);
482 if (x>=g_pChildWnd->nSplitPos-SPLIT_WIDTH/2 && x<g_pChildWnd->nSplitPos+SPLIT_WIDTH/2+1) {
483 last_split = g_pChildWnd->nSplitPos;
484 draw_splitbar(hWnd, last_split);
485 SetCapture(hWnd);
487 break;
490 /* WM_RBUTTONDOWN sets the splitbar the same way as WM_LBUTTONUP */
491 case WM_LBUTTONUP:
492 case WM_RBUTTONDOWN:
493 if (GetCapture() == hWnd) {
494 finish_splitbar(hWnd, LOWORD(lParam));
496 break;
498 case WM_CAPTURECHANGED:
499 if (GetCapture()==hWnd && last_split>=0)
500 draw_splitbar(hWnd, last_split);
501 break;
503 case WM_CONTEXTMENU:
505 POINT pt = {GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)};
506 short int menu_id = -1;
508 if ((HWND)wParam == g_pChildWnd->hTreeWnd)
510 TVHITTESTINFO ht;
512 ht.pt = pt;
513 ScreenToClient(g_pChildWnd->hTreeWnd, &ht.pt);
515 if (SendMessageW(g_pChildWnd->hTreeWnd, TVM_HITTEST, 0, (LPARAM)&ht))
517 HTREEITEM root;
519 SendMessageW(g_pChildWnd->hTreeWnd, TVM_SELECTITEM, TVGN_CARET, (LPARAM)ht.hItem);
520 root = (HTREEITEM)SendMessageW(g_pChildWnd->hTreeWnd, TVM_GETNEXTITEM, TVGN_ROOT, 0);
521 menu_id = (ht.hItem == root) ? PM_COMPUTER : PM_TREEVIEW;
524 else
526 int sel = SendMessageW(g_pChildWnd->hListWnd, LVM_GETNEXTITEM, -1,
527 MAKELPARAM(LVNI_SELECTED, 0));
528 menu_id = (sel == -1) ? PM_NEW_VALUE : PM_MODIFY_VALUE;
531 TrackPopupMenu(GetSubMenu(hPopupMenus, menu_id), TPM_RIGHTBUTTON,
532 pt.x, pt.y, 0, hFrameWnd, NULL);
533 break;
536 case WM_KEYDOWN:
537 if (wParam == VK_ESCAPE)
538 if (GetCapture() == hWnd) {
539 RECT rt;
540 draw_splitbar(hWnd, last_split);
541 GetClientRect(hWnd, &rt);
542 ResizeWnd(rt.right, rt.bottom);
543 last_split = -1;
544 ReleaseCapture();
545 SetCursor(LoadCursorW(0, (LPCWSTR)IDC_ARROW));
547 break;
549 case WM_MOUSEMOVE:
550 if (GetCapture() == hWnd) {
551 RECT rt;
552 int x = LOWORD(lParam);
553 HDC hdc = GetDC(hWnd);
554 GetClientRect(hWnd, &rt);
555 rt.left = last_split-SPLIT_WIDTH/2;
556 rt.right = last_split+SPLIT_WIDTH/2+1;
557 InvertRect(hdc, &rt);
558 last_split = x;
559 rt.left = x-SPLIT_WIDTH/2;
560 rt.right = x+SPLIT_WIDTH/2+1;
561 InvertRect(hdc, &rt);
562 ReleaseDC(hWnd, hdc);
564 break;
566 case WM_SETFOCUS:
567 if (g_pChildWnd != NULL) {
568 SetFocus(g_pChildWnd->nFocusPanel? g_pChildWnd->hListWnd: g_pChildWnd->hTreeWnd);
570 break;
572 case WM_TIMER:
573 break;
575 case WM_NOTIFY:
576 if (wParam == TREE_WINDOW && g_pChildWnd)
577 return treeview_notify(hWnd, message, wParam, lParam);
578 else if (wParam == LIST_WINDOW && g_pChildWnd)
579 return listview_notify(hWnd, message, wParam, lParam);
580 break;
582 case WM_SIZE:
583 if (wParam != SIZE_MINIMIZED && g_pChildWnd != NULL) {
584 ResizeWnd(LOWORD(lParam), HIWORD(lParam));
586 /* fall through */
587 default: def:
588 return DefWindowProcW(hWnd, message, wParam, lParam);
590 return 0;