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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #define WIN32_LEAN_AND_MEAN /* Exclude rarely-used stuff from Windows headers */
35 /********************************************************************************
36 * Global and Local Variables:
39 static BOOL bInMenuLoop
= FALSE
; /* Tells us if we are in the menu loop */
41 /*******************************************************************************
42 * Local module support methods
45 static void resize_frame_rect(HWND hWnd
, PRECT prect
)
49 if (IsWindowVisible(hToolBar)) {
50 SendMessage(hToolBar, WM_SIZE, 0, 0);
51 GetClientRect(hToolBar, &rt);
52 prect->top = rt.bottom+3;
53 prect->bottom -= rt.bottom+3;
56 if (IsWindowVisible(hStatusBar
)) {
57 SetupStatusBar(hWnd
, TRUE
);
58 GetClientRect(hStatusBar
, &rt
);
59 prect
->bottom
-= rt
.bottom
;
61 MoveWindow(g_pChildWnd
->hWnd
, prect
->left
, prect
->top
, prect
->right
, prect
->bottom
, TRUE
);
64 void resize_frame_client(HWND hWnd
)
68 GetClientRect(hWnd
, &rect
);
69 resize_frame_rect(hWnd
, &rect
);
72 /********************************************************************************/
74 static void OnEnterMenuLoop(HWND hWnd
)
78 /* Update the status bar pane sizes */
80 SendMessage(hStatusBar
, SB_SETPARTS
, 1, (long)&nParts
);
82 SendMessage(hStatusBar
, SB_SETTEXT
, (WPARAM
)0, (LPARAM
)_T(""));
85 static void OnExitMenuLoop(HWND hWnd
)
88 /* Update the status bar pane sizes*/
89 SetupStatusBar(hWnd
, TRUE
);
93 static void OnMenuSelect(HWND hWnd
, UINT nItemID
, UINT nFlags
, HMENU hSysMenu
)
98 if (nFlags
& MF_POPUP
) {
99 if (hSysMenu
!= GetMenu(hWnd
)) {
100 if (nItemID
== 2) nItemID
= 5;
103 if (LoadString(hInst
, nItemID
, str
, 100)) {
104 /* load appropriate string*/
106 /* first newline terminates actual string*/
107 lpsz
= _tcschr(lpsz
, '\n');
111 SendMessage(hStatusBar
, SB_SETTEXT
, 0, (LPARAM
)str
);
114 void SetupStatusBar(HWND hWnd
, BOOL bResize
)
118 GetClientRect(hWnd
, &rc
);
122 SendMessage(hStatusBar
, WM_SIZE
, 0, 0);
123 SendMessage(hStatusBar
, SB_SETPARTS
, 1, (LPARAM
)&nParts
);
126 void UpdateStatusBar(void)
131 size
= sizeof(text
)/sizeof(TCHAR
);
132 GetComputerName(text
, &size
);
133 SendMessage(hStatusBar
, SB_SETTEXT
, 0, (LPARAM
)text
);
136 static void toggle_child(HWND hWnd
, UINT cmd
, HWND hchild
)
138 BOOL vis
= IsWindowVisible(hchild
);
139 HMENU hMenuView
= GetSubMenu(hMenuFrame
, ID_VIEW_MENU
);
141 CheckMenuItem(hMenuView
, cmd
, vis
?MF_BYCOMMAND
:MF_BYCOMMAND
|MF_CHECKED
);
142 ShowWindow(hchild
, vis
?SW_HIDE
:SW_SHOW
);
143 resize_frame_client(hWnd
);
146 static BOOL
CheckCommDlgError(HWND hWnd
)
148 DWORD dwErrorCode
= CommDlgExtendedError();
149 switch (dwErrorCode
) {
150 case CDERR_DIALOGFAILURE
:
152 case CDERR_FINDRESFAILURE
:
154 case CDERR_NOHINSTANCE
:
156 case CDERR_INITIALIZATION
:
160 case CDERR_LOCKRESFAILURE
:
162 case CDERR_NOTEMPLATE
:
164 case CDERR_LOADRESFAILURE
:
166 case CDERR_STRUCTSIZE
:
168 case CDERR_LOADSTRFAILURE
:
170 case FNERR_BUFFERTOOSMALL
:
172 case CDERR_MEMALLOCFAILURE
:
174 case FNERR_INVALIDFILENAME
:
176 case CDERR_MEMLOCKFAILURE
:
178 case FNERR_SUBCLASSFAILURE
:
186 UINT_PTR CALLBACK
ImportRegistryFile_OFNHookProc(HWND hdlg
, UINT uiMsg
, WPARAM wParam
, LPARAM lParam
)
188 OPENFILENAME
* pOpenFileName
;
193 pOpenFileName
= (OPENFILENAME
*)lParam
;
196 pOfNotify
= (OFNOTIFY
*)lParam
;
197 if (pOfNotify
->hdr
.code
== CDN_INITDONE
) {}
205 #define MAX_CUSTOM_FILTER_SIZE 50
206 TCHAR CustomFilterBuffer
[MAX_CUSTOM_FILTER_SIZE
];
207 TCHAR FileNameBuffer
[_MAX_PATH
];
208 TCHAR FileTitleBuffer
[_MAX_PATH
];
214 } FILTERPAIR
, *PFILTERPAIR
;
217 BuildFilterStrings(TCHAR
*Filter
, PFILTERPAIR Pairs
, int PairCount
)
222 for(i
= 0; i
< PairCount
; i
++)
224 c
+= LoadString(hInst
, Pairs
[i
].DisplayID
, &Filter
[c
], 255 * sizeof(TCHAR
));
226 c
+= LoadString(hInst
, Pairs
[i
].FilterID
, &Filter
[c
], 255 * sizeof(TCHAR
));
232 static BOOL
InitOpenFileName(HWND hWnd
, OPENFILENAME
* pofn
)
234 FILTERPAIR FilterPairs
[3];
235 static TCHAR Filter
[1024];
237 memset(pofn
, 0, sizeof(OPENFILENAME
));
238 pofn
->lStructSize
= sizeof(OPENFILENAME
);
239 pofn
->hwndOwner
= hWnd
;
240 pofn
->hInstance
= hInst
;
242 /* create filter string */
243 FilterPairs
[0].DisplayID
= IDS_FLT_REGFILES
;
244 FilterPairs
[0].FilterID
= IDS_FLT_REGFILES_FLT
;
245 FilterPairs
[1].DisplayID
= IDS_FLT_REGEDIT4
;
246 FilterPairs
[1].FilterID
= IDS_FLT_REGEDIT4_FLT
;
247 FilterPairs
[2].DisplayID
= IDS_FLT_ALLFILES
;
248 FilterPairs
[2].FilterID
= IDS_FLT_ALLFILES_FLT
;
249 BuildFilterStrings(Filter
, FilterPairs
, sizeof(FilterPairs
) / sizeof(FILTERPAIR
));
251 pofn
->lpstrFilter
= Filter
;
252 pofn
->lpstrCustomFilter
= CustomFilterBuffer
;
253 pofn
->nMaxCustFilter
= MAX_CUSTOM_FILTER_SIZE
;
254 pofn
->nFilterIndex
= 0;
255 pofn
->lpstrFile
= FileNameBuffer
;
256 pofn
->nMaxFile
= _MAX_PATH
;
257 pofn
->lpstrFileTitle
= FileTitleBuffer
;
258 pofn
->nMaxFileTitle
= _MAX_PATH
;
259 /* pofn->lpstrInitialDir = _T("");*/
260 /* pofn->lpstrTitle = _T("Import Registry File");*/
261 /* pofn->Flags = OFN_ENABLETEMPLATE + OFN_EXPLORER + OFN_ENABLESIZING;*/
262 pofn
->Flags
= OFN_HIDEREADONLY
;
263 /* pofn->nFileOffset = ;*/
264 /* pofn->nFileExtension = ;*/
265 /* pofn->lpstrDefExt = _T("");*/
266 /* pofn->lCustData = ;*/
267 /* pofn->lpfnHook = ImportRegistryFile_OFNHookProc;*/
268 /* pofn->lpTemplateName = _T("ID_DLG_IMPORT_REGFILE");*/
269 /* pofn->lpTemplateName = MAKEINTRESOURCE(IDD_DIALOG1);*/
270 /* pofn->FlagsEx = ;*/
274 static BOOL
ImportRegistryFile(HWND hWnd
)
279 InitOpenFileName(hWnd
, &ofn
);
280 LoadString(hInst
, IDS_IMPORT_REG_FILE
, Caption
, sizeof(Caption
)/sizeof(TCHAR
));
281 ofn
.lpstrTitle
= Caption
;
282 /* ofn.lCustData = ;*/
283 if (GetOpenFileName(&ofn
)) {
284 /* FIXME - convert to ascii */
285 if (!import_registry_file((LPSTR
)ofn
.lpstrFile
)) {
286 /*printf("Can't open file \"%s\"\n", ofn.lpstrFile);*/
290 get_file_name(&s
, filename
, MAX_PATH
);
292 printf("No file name is specified\n%s", usage
);
296 while (filename
[0]) {
297 if (!import_registry_file(filename
)) {
299 printf("Can't open file \"%s\"\n", filename
);
303 get_file_name(&s
, filename
, MAX_PATH
);
308 CheckCommDlgError(hWnd
);
314 static BOOL
ExportRegistryFile(HWND hWnd
)
317 TCHAR ExportKeyPath
[_MAX_PATH
];
320 ExportKeyPath
[0] = _T('\0');
321 InitOpenFileName(hWnd
, &ofn
);
322 LoadString(hInst
, IDS_EXPORT_REG_FILE
, Caption
, sizeof(Caption
)/sizeof(TCHAR
));
323 ofn
.lpstrTitle
= Caption
;
324 /* ofn.lCustData = ;*/
325 ofn
.Flags
= OFN_ENABLETEMPLATE
+ OFN_EXPLORER
;
326 ofn
.lpfnHook
= ImportRegistryFile_OFNHookProc
;
327 ofn
.lpTemplateName
= MAKEINTRESOURCE(IDD_DIALOG1
);
328 if (GetSaveFileName(&ofn
)) {
330 /* FIXME - convert strings to ascii! */
331 result
= export_registry_key((CHAR
*)ofn
.lpstrFile
, (CHAR
*)ExportKeyPath
);
332 /*result = export_registry_key(ofn.lpstrFile, NULL);*/
333 /*if (!export_registry_key(ofn.lpstrFile, NULL)) {*/
335 /*printf("Can't open file \"%s\"\n", ofn.lpstrFile);*/
339 TCHAR filename
[MAX_PATH
];
341 get_file_name(&s
, filename
, MAX_PATH
);
343 printf("No file name is specified\n%s", usage
);
348 TCHAR reg_key_name
[KEY_MAX_LEN
];
349 get_file_name(&s
, reg_key_name
, KEY_MAX_LEN
);
350 export_registry_key((CHAR
)filename
, reg_key_name
);
352 export_registry_key(filename
, NULL
);
357 CheckCommDlgError(hWnd
);
362 BOOL
PrintRegistryHive(HWND hWnd
, LPTSTR path
)
367 ZeroMemory(&pd
, sizeof(PRINTDLG
));
368 pd
.lStructSize
= sizeof(PRINTDLG
);
370 pd
.hDevMode
= NULL
; /* Don't forget to free or store hDevMode*/
371 pd
.hDevNames
= NULL
; /* Don't forget to free or store hDevNames*/
372 pd
.Flags
= PD_USEDEVMODECOPIESANDCOLLATE
| PD_RETURNDC
;
374 pd
.nFromPage
= 0xFFFF;
377 pd
.nMaxPage
= 0xFFFF;
378 if (PrintDlg(&pd
) == TRUE
) {
379 /* GDI calls to render output. */
380 DeleteDC(pd
.hDC
); /* Delete DC when done.*/
386 hResult
= PrintDlgEx(&pd
);
387 if (hResult
== S_OK
) {
388 switch (pd
.dwResultAction
) {
389 case PD_RESULT_APPLY
:
390 /*The user clicked the Apply button and later clicked the Cancel button. This indicates that the user wants to apply the changes made in the property sheet, but does not yet want to print. The PRINTDLGEX structure contains the information specified by the user at the time the Apply button was clicked. */
392 case PD_RESULT_CANCEL
:
393 /*The user clicked the Cancel button. The information in the PRINTDLGEX structure is unchanged. */
395 case PD_RESULT_PRINT
:
396 /*The user clicked the Print button. The PRINTDLGEX structure contains the information specified by the user. */
404 /*Insufficient memory. */
407 /* One or more arguments are invalid. */
410 /*Invalid pointer. */
416 /*Unspecified error. */
427 BOOL
CopyKeyName(HWND hWnd
, LPTSTR keyName
)
431 result
= OpenClipboard(hWnd
);
433 result
= EmptyClipboard();
436 /*HANDLE hClipData;*/
437 /*hClipData = SetClipboardData(UINT uFormat, HANDLE hMem);*/
440 /* error emptying clipboard*/
441 /* DWORD dwError = GetLastError(); */
444 if (!CloseClipboard()) {
445 /* error closing clipboard*/
446 /* DWORD dwError = GetLastError(); */
450 /* error opening clipboard*/
451 /* DWORD dwError = GetLastError(); */
457 BOOL
RefreshView(HWND hWnd
)
461 MessageBeep(MB_ICONASTERISK
);
462 MessageBeep(MB_ICONEXCLAMATION
);
463 MessageBeep(MB_ICONHAND
);
464 MessageBeep(MB_ICONQUESTION
);
469 /*******************************************************************************
471 * FUNCTION: _CmdWndProc(HWND, unsigned, WORD, LONG)
473 * PURPOSE: Processes WM_COMMAND messages for the main frame window.
476 static BOOL
_CmdWndProc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
478 HKEY hKeyRoot
= 0, hKey
= 0;
482 REGSAM regsam
= KEY_READ
;
486 switch (LOWORD(wParam
)) {
487 case ID_REGISTRY_IMPORTREGISTRYFILE
:
488 ImportRegistryFile(hWnd
);
490 case ID_REGISTRY_EXPORTREGISTRYFILE
:
491 ExportRegistryFile(hWnd
);
493 case ID_REGISTRY_CONNECTNETWORKREGISTRY
:
495 case ID_REGISTRY_DISCONNECTNETWORKREGISTRY
:
497 case ID_REGISTRY_PRINT
:
498 PrintRegistryHive(hWnd
, _T(""));
500 case ID_REGISTRY_EXIT
:
503 case ID_VIEW_STATUSBAR
:
504 toggle_child(hWnd
, LOWORD(wParam
), hStatusBar
);
506 case ID_HELP_HELPTOPICS
:
507 WinHelp(hWnd
, _T("regedit"), HELP_FINDER
, 0);
516 GetClientRect(g_pChildWnd
->hWnd
, &rt
);
517 pt
.x
= rt
.left
+ g_pChildWnd
->nSplitPos
;
518 pt
.y
= (rt
.bottom
/ 2);
520 if(ClientToScreen(g_pChildWnd
->hWnd
, &pts
))
522 SetCursorPos(pts
.x
, pts
.y
);
523 SetCursor(LoadCursor(0, IDC_SIZEWE
));
524 SendMessage(g_pChildWnd
->hWnd
, WM_LBUTTONDOWN
, 0, MAKELPARAM(pt
.x
, pt
.y
));
530 case ID_EDIT_MODIFY_BIN
:
536 keyPath
= GetItemPath(g_pChildWnd
->hTreeWnd
, 0, &hKeyRoot
);
537 valueName
= GetValueName(g_pChildWnd
->hListWnd
, -1);
539 lRet
= RegOpenKeyEx(hKeyRoot
, keyPath
, 0, regsam
, &hKey
);
540 if (lRet
!= ERROR_SUCCESS
) hKey
= 0;
543 switch (LOWORD(wParam
)) {
545 if (valueName
&& ModifyValue(hWnd
, hKey
, valueName
, FALSE
))
546 RefreshListView(g_pChildWnd
->hListWnd
, hKeyRoot
, keyPath
);
548 case ID_EDIT_MODIFY_BIN
:
549 if (valueName
&& ModifyValue(hWnd
, hKey
, valueName
, TRUE
))
550 RefreshListView(g_pChildWnd
->hListWnd
, hKeyRoot
, keyPath
);
553 if(ListView_GetSelectedCount(g_pChildWnd
->hListWnd
) == 1)
555 item
= ListView_GetNextItem(g_pChildWnd
->hListWnd
, -1, LVNI_SELECTED
);
558 ListView_EditLabel(g_pChildWnd
->hListWnd
, item
);
564 UINT nSelected
= ListView_GetSelectedCount(g_pChildWnd
->hListWnd
);
567 TCHAR msg
[128], caption
[128];
568 LoadString(hInst
, IDS_QUERY_DELETE_CONFIRM
, caption
, sizeof(caption
)/sizeof(TCHAR
));
569 LoadString(hInst
, (nSelected
== 1 ? IDS_QUERY_DELETE_ONE
: IDS_QUERY_DELETE_MORE
), msg
, sizeof(msg
)/sizeof(TCHAR
));
570 if(MessageBox(g_pChildWnd
->hWnd
, msg
, caption
, MB_ICONQUESTION
| MB_YESNO
) == IDYES
)
576 while((ni
= ListView_GetNextItem(g_pChildWnd
->hListWnd
, item
, LVNI_SELECTED
)) > -1)
578 valueName
= GetValueName(g_pChildWnd
->hListWnd
, item
);
579 if(RegDeleteValue(hKey
, valueName
) != ERROR_SUCCESS
)
586 RefreshListView(g_pChildWnd
->hListWnd
, hKeyRoot
, keyPath
);
589 LoadString(hInst
, IDS_ERR_DELVAL_CAPTION
, caption
, sizeof(caption
)/sizeof(TCHAR
));
590 LoadString(hInst
, IDS_ERR_DELETEVALUE
, msg
, sizeof(msg
)/sizeof(TCHAR
));
591 MessageBox(g_pChildWnd
->hWnd
, msg
, caption
, MB_ICONSTOP
);
597 case ID_EDIT_COPYKEYNAME
:
598 CopyKeyName(hWnd
, _T(""));
600 case ID_EDIT_PERMISSIONS
:
601 if(keyPath
!= NULL
&& _tcslen(keyPath
) > 0)
603 RegKeyEditPermissions(hWnd
, hKey
, NULL
, keyPath
);
607 MessageBeep(MB_ICONASTERISK
);
610 case ID_REGISTRY_PRINTERSETUP
:
613 /*PAGESETUPDLG psd;*/
614 /*PageSetupDlg(&psd);*/
616 case ID_REGISTRY_OPENLOCAL
:
619 case ID_VIEW_REFRESH
:
622 /*case ID_OPTIONS_TOOLBAR:*/
623 /* toggle_child(hWnd, LOWORD(wParam), hToolBar);*/
634 /********************************************************************************
636 * FUNCTION: FrameWndProc(HWND, unsigned, WORD, LONG)
638 * PURPOSE: Processes messages for the main frame window.
640 * WM_COMMAND - process the application menu
641 * WM_DESTROY - post a quit message and return
646 FrameWndProc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
650 CreateWindowEx(0, szChildClass
, NULL
, WS_CHILD
| WS_VISIBLE
,
651 CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
,
652 hWnd
, (HMENU
)0, hInst
, 0);
655 if (!_CmdWndProc(hWnd
, message
, wParam
, lParam
))
656 return DefWindowProc(hWnd
, message
, wParam
, lParam
);
659 resize_frame_client(hWnd
);
663 case WM_ENTERMENULOOP
:
664 OnEnterMenuLoop(hWnd
);
666 case WM_EXITMENULOOP
:
667 OnExitMenuLoop(hWnd
);
670 OnMenuSelect(hWnd
, LOWORD(wParam
), HIWORD(wParam
), (HMENU
)lParam
);
674 SetFocus(g_pChildWnd
->hWnd
);
677 WinHelp(hWnd
, _T("regedit"), HELP_QUIT
, 0);
680 return DefWindowProc(hWnd
, message
, wParam
, lParam
);