use INT_PTR as return type for dialog callbacks as documented in favor of portability
[reactos.git] / reactos / subsys / system / 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 HBITMAP SizingPattern = 0;
31 HBRUSH SizingBrush = 0;
32
33 /*******************************************************************************
34 * Local module support methods
35 */
36
37 static LPCTSTR get_root_key_name(HKEY hRootKey)
38 {
39 if (hRootKey == HKEY_CLASSES_ROOT) return _T("HKEY_CLASSES_ROOT");
40 if (hRootKey == HKEY_CURRENT_USER) return _T("HKEY_CURRENT_USER");
41 if (hRootKey == HKEY_LOCAL_MACHINE) return _T("HKEY_LOCAL_MACHINE");
42 if (hRootKey == HKEY_USERS) return _T("HKEY_USERS");
43 if (hRootKey == HKEY_CURRENT_CONFIG) return _T("HKEY_CURRENT_CONFIG");
44 return _T("UKNOWN HKEY, PLEASE REPORT");
45 }
46
47 static void draw_splitbar(HWND hWnd, int x)
48 {
49 RECT rt;
50 HGDIOBJ OldObj;
51 HDC hdc = GetDC(hWnd);
52
53 if(!SizingPattern)
54 {
55 const DWORD Pattern[4] = {0x5555AAAA, 0x5555AAAA, 0x5555AAAA, 0x5555AAAA};
56 SizingPattern = CreateBitmap(8, 8, 1, 1, Pattern);
57 }
58 if(!SizingBrush)
59 {
60 SizingBrush = CreatePatternBrush(SizingPattern);
61 }
62 GetClientRect(hWnd, &rt);
63 rt.left = x - SPLIT_WIDTH/2;
64 rt.right = x + SPLIT_WIDTH/2+1;
65 OldObj = SelectObject(hdc, SizingBrush);
66 PatBlt(hdc, rt.left, rt.top, rt.right - rt.left, rt.bottom - rt.top, PATINVERT);
67 SelectObject(hdc, OldObj);
68 ReleaseDC(hWnd, hdc);
69 }
70
71 static void ResizeWnd(ChildWnd* pChildWnd, int cx, int cy)
72 {
73 HDWP hdwp = BeginDeferWindowPos(2);
74 RECT rt = {0, 0, cx, cy};
75
76 cx = pChildWnd->nSplitPos + SPLIT_WIDTH/2;
77 DeferWindowPos(hdwp, pChildWnd->hTreeWnd, 0, rt.left, rt.top, pChildWnd->nSplitPos-SPLIT_WIDTH/2-rt.left, rt.bottom-rt.top, SWP_NOZORDER|SWP_NOACTIVATE);
78 DeferWindowPos(hdwp, pChildWnd->hListWnd, 0, rt.left+cx , rt.top, rt.right-cx, rt.bottom-rt.top, SWP_NOZORDER|SWP_NOACTIVATE);
79 EndDeferWindowPos(hdwp);
80 }
81
82 static void OnPaint(HWND hWnd)
83 {
84 PAINTSTRUCT ps;
85 RECT rt;
86 HDC hdc;
87
88 GetClientRect(hWnd, &rt);
89 hdc = BeginPaint(hWnd, &ps);
90 FillRect(ps.hdc, &rt, GetSysColorBrush(COLOR_BTNFACE));
91 EndPaint(hWnd, &ps);
92 }
93
94 /*******************************************************************************
95 *
96 * FUNCTION: _CmdWndProc(HWND, unsigned, WORD, LONG)
97 *
98 * PURPOSE: Processes WM_COMMAND messages for the main frame window.
99 *
100 */
101
102 static BOOL _CmdWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
103 {
104 ChildWnd* pChildWnd = g_pChildWnd;
105 switch (LOWORD(wParam)) {
106 /* Parse the menu selections: */
107 case ID_REGISTRY_EXIT:
108 DestroyWindow(hWnd);
109 break;
110 case ID_VIEW_REFRESH:
111 /* TODO */
112 break;
113 case ID_SWITCH_PANELS:
114 pChildWnd->nFocusPanel = !pChildWnd->nFocusPanel;
115 SetFocus(pChildWnd->nFocusPanel? pChildWnd->hListWnd: pChildWnd->hTreeWnd);
116 break;
117 default:
118 return FALSE;
119 }
120 return TRUE;
121 }
122
123 /*******************************************************************************
124 *
125 * FUNCTION: ChildWndProc(HWND, unsigned, WORD, LONG)
126 *
127 * PURPOSE: Processes messages for the child windows.
128 *
129 * WM_COMMAND - process the application menu
130 * WM_PAINT - Paint the main window
131 * WM_DESTROY - post a quit message and return
132 *
133 */
134 INT_PTR CALLBACK
135 ChildWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
136 {
137 static short last_split;
138 BOOL Result;
139 ChildWnd* pChildWnd = g_pChildWnd;
140
141 switch (message) {
142 case WM_CREATE:
143 {
144 TCHAR buffer[MAX_PATH];
145 /* load "My Computer" string */
146 LoadString(hInst, IDS_MY_COMPUTER, buffer, sizeof(buffer)/sizeof(TCHAR));
147
148 g_pChildWnd = pChildWnd = HeapAlloc(GetProcessHeap(), 0, sizeof(ChildWnd));
149 if (!pChildWnd) return 0;
150 _tcsncpy(pChildWnd->szPath, buffer, MAX_PATH);
151 pChildWnd->nSplitPos = 250;
152 pChildWnd->hWnd = hWnd;
153 pChildWnd->hTreeWnd = CreateTreeView(hWnd, pChildWnd->szPath, TREE_WINDOW);
154 pChildWnd->hListWnd = CreateListView(hWnd, LIST_WINDOW/*, pChildWnd->szPath*/);
155 SetFocus(pChildWnd->hTreeWnd);
156 break;
157 }
158 case WM_COMMAND:
159 if (!_CmdWndProc(hWnd, message, wParam, lParam)) {
160 goto def;
161 }
162 break;
163 case WM_PAINT:
164 OnPaint(hWnd);
165 return 0;
166 case WM_SETCURSOR:
167 if (LOWORD(lParam) == HTCLIENT) {
168 POINT pt;
169 GetCursorPos(&pt);
170 ScreenToClient(hWnd, &pt);
171 if (pt.x>=pChildWnd->nSplitPos-SPLIT_WIDTH/2 && pt.x<pChildWnd->nSplitPos+SPLIT_WIDTH/2+1) {
172 SetCursor(LoadCursor(0, IDC_SIZEWE));
173 return TRUE;
174 }
175 }
176 goto def;
177 case WM_DESTROY:
178 HeapFree(GetProcessHeap(), 0, pChildWnd);
179 pChildWnd = NULL;
180 PostQuitMessage(0);
181 break;
182 case WM_LBUTTONDOWN: {
183 RECT rt;
184 POINTS pt;
185 pt = MAKEPOINTS(lParam);
186 GetClientRect(hWnd, &rt);
187 if (pt.x>=pChildWnd->nSplitPos-SPLIT_WIDTH/2 && pt.x<pChildWnd->nSplitPos+SPLIT_WIDTH/2+1) {
188 last_split = pChildWnd->nSplitPos;
189 draw_splitbar(hWnd, last_split);
190 SetCapture(hWnd);
191 }
192 break;
193 }
194
195 case WM_LBUTTONUP:
196 if (GetCapture() == hWnd) {
197 RECT rt;
198 POINTS pt;
199 pt = MAKEPOINTS(lParam);
200 GetClientRect(hWnd, &rt);
201 pt.x = min(max(pt.x, SPLIT_MIN), rt.right - SPLIT_MIN);
202 draw_splitbar(hWnd, last_split);
203 last_split = -1;
204 pChildWnd->nSplitPos = pt.x;
205 ResizeWnd(pChildWnd, rt.right, rt.bottom);
206 ReleaseCapture();
207 }
208 break;
209
210 case WM_CAPTURECHANGED:
211 if (GetCapture()==hWnd && last_split>=0)
212 draw_splitbar(hWnd, last_split);
213 break;
214
215 case WM_KEYDOWN:
216 if (wParam == VK_ESCAPE)
217 if (GetCapture() == hWnd) {
218 RECT rt;
219 draw_splitbar(hWnd, last_split);
220 GetClientRect(hWnd, &rt);
221 ResizeWnd(pChildWnd, rt.right, rt.bottom);
222 last_split = -1;
223 ReleaseCapture();
224 SetCursor(LoadCursor(0, IDC_ARROW));
225 }
226 break;
227
228 case WM_MOUSEMOVE:
229 if (GetCapture() == hWnd) {
230 HDC hdc;
231 RECT rt;
232 HGDIOBJ OldObj;
233 POINTS pt;
234 if(!SizingPattern)
235 {
236 const DWORD Pattern[4] = {0x5555AAAA, 0x5555AAAA, 0x5555AAAA, 0x5555AAAA};
237 SizingPattern = CreateBitmap(8, 8, 1, 1, Pattern);
238 }
239 if(!SizingBrush)
240 {
241 SizingBrush = CreatePatternBrush(SizingPattern);
242 }
243
244 pt = MAKEPOINTS(lParam);
245 GetClientRect(hWnd, &rt);
246 pt.x = min(max(pt.x, SPLIT_MIN), rt.right - SPLIT_MIN);
247 if(last_split != pt.x)
248 {
249 rt.left = last_split-SPLIT_WIDTH/2;
250 rt.right = last_split+SPLIT_WIDTH/2+1;
251 hdc = GetDC(hWnd);
252 OldObj = SelectObject(hdc, SizingBrush);
253 PatBlt(hdc, rt.left, rt.top, rt.right - rt.left, rt.bottom - rt.top, PATINVERT);
254 last_split = pt.x;
255 rt.left = pt.x-SPLIT_WIDTH/2;
256 rt.right = pt.x+SPLIT_WIDTH/2+1;
257 PatBlt(hdc, rt.left, rt.top, rt.right - rt.left, rt.bottom - rt.top, PATINVERT);
258 SelectObject(hdc, OldObj);
259 ReleaseDC(hWnd, hdc);
260 }
261 }
262 break;
263
264 case WM_SETFOCUS:
265 if (pChildWnd != NULL) {
266 SetFocus(pChildWnd->nFocusPanel? pChildWnd->hListWnd: pChildWnd->hTreeWnd);
267 }
268 break;
269
270 case WM_TIMER:
271 break;
272
273 case WM_NOTIFY:
274 if ((int)wParam == TREE_WINDOW) {
275 switch (((LPNMHDR)lParam)->code) {
276 case TVN_ITEMEXPANDING:
277 return !OnTreeExpanding(pChildWnd->hTreeWnd, (NMTREEVIEW*)lParam);
278 case TVN_SELCHANGED: {
279 LPCTSTR keyPath, rootName;
280 LPTSTR fullPath;
281 HKEY hRootKey;
282
283 keyPath = GetItemPath(pChildWnd->hTreeWnd, ((NMTREEVIEW*)lParam)->itemNew.hItem, &hRootKey);
284 if (keyPath) {
285 RefreshListView(pChildWnd->hListWnd, hRootKey, keyPath);
286 rootName = get_root_key_name(hRootKey);
287 fullPath = HeapAlloc(GetProcessHeap(), 0, (_tcslen(rootName) + 1 + _tcslen(keyPath) + 1) * sizeof(TCHAR));
288 if (fullPath) {
289 _stprintf(fullPath, _T("%s\\%s"), rootName, keyPath);
290 SendMessage(hStatusBar, SB_SETTEXT, 0, (LPARAM)fullPath);
291 HeapFree(GetProcessHeap(), 0, fullPath);
292 }
293 }
294 }
295 break;
296 case NM_SETFOCUS:
297 pChildWnd->nFocusPanel = 0;
298 break;
299 default:
300 return 0;
301 }
302 } else
303 {
304 if ((int)wParam == LIST_WINDOW)
305 {
306 switch (((LPNMHDR)lParam)->code) {
307 case NM_SETFOCUS:
308 pChildWnd->nFocusPanel = 1;
309 break;
310 default:
311 if(!ListWndNotifyProc(pChildWnd->hListWnd, wParam, lParam, &Result))
312 {
313 goto def;
314 }
315 break;
316 }
317 }
318 }
319 break;
320
321 case WM_CONTEXTMENU:
322 {
323 POINTS pt;
324 if((HWND)wParam == pChildWnd->hListWnd)
325 {
326 int i, cnt;
327 BOOL IsDefault;
328 pt = MAKEPOINTS(lParam);
329 cnt = ListView_GetSelectedCount(pChildWnd->hListWnd);
330 i = ListView_GetNextItem(pChildWnd->hListWnd, -1, LVNI_FOCUSED | LVNI_SELECTED);
331 if(i == -1)
332 {
333 TrackPopupMenu(GetSubMenu(hPopupMenus, PM_NEW), TPM_RIGHTBUTTON, pt.x, pt.y, 0, hFrameWnd, NULL);
334 }
335 else
336 {
337 HMENU mnu = GetSubMenu(hPopupMenus, PM_MODIFYVALUE);
338 SetMenuDefaultItem(mnu, ID_EDIT_MODIFY, MF_BYCOMMAND);
339 IsDefault = IsDefaultValue(pChildWnd->hListWnd, i);
340 if(cnt == 1)
341 EnableMenuItem(mnu, ID_EDIT_RENAME, MF_BYCOMMAND | (IsDefault ? MF_DISABLED | MF_GRAYED : MF_ENABLED));
342 else
343 EnableMenuItem(mnu, ID_EDIT_RENAME, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
344 EnableMenuItem(mnu, ID_EDIT_MODIFY, MF_BYCOMMAND | (cnt == 1 ? MF_ENABLED : MF_DISABLED | MF_GRAYED));
345 EnableMenuItem(mnu, ID_EDIT_MODIFY_BIN, MF_BYCOMMAND | (cnt == 1 ? MF_ENABLED : MF_DISABLED | MF_GRAYED));
346
347 TrackPopupMenu(mnu, TPM_RIGHTBUTTON, pt.x, pt.y, 0, hFrameWnd, NULL);
348 }
349 }
350 break;
351 }
352
353 case WM_SIZE:
354 if (wParam != SIZE_MINIMIZED && pChildWnd != NULL) {
355 ResizeWnd(pChildWnd, LOWORD(lParam), HIWORD(lParam));
356 }
357 /* fall through */
358 default: def:
359 return DefWindowProc(hWnd, message, wParam, lParam);
360 }
361 return 0;
362 }