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