Regedit: Implemented accelerator keys
[reactos.git] / reactos / subsys / system / regedit / main.c
index 268d9c7..174e3c8 100644 (file)
 #define WIN32_LEAN_AND_MEAN     /* Exclude rarely-used stuff from Windows headers */
 #include <windows.h>
 #include <commctrl.h>
+#include <accctrl.h>
+#include <unknwn.h>
 #include <stdlib.h>
 #include <tchar.h>
 #include <process.h>
 #include <stdio.h>
 #include <fcntl.h>
+#include <aclui.h>
+#include <cguid.h>
 
 #include "main.h"
-
+#include "hexedit.h"
+#include "security.h"
+#include "regproc.h"
 
 BOOL ProcessCmdLine(LPSTR lpCmdLine);
 
@@ -41,8 +47,10 @@ HINSTANCE hInst;
 HWND hFrameWnd;
 HWND hStatusBar;
 HMENU hMenuFrame;
+HMENU hPopupMenus = 0;
 UINT nClipboardFormat;
 LPCTSTR strClipboardFormat = _T("TODO: SET CORRECT FORMAT");
+const TCHAR g_szGeneralRegKey[] = _T("Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\Regedit");
 
 
 #define MAX_LOADSTRING  100
@@ -66,6 +74,10 @@ TCHAR szChildClass[MAX_LOADSTRING];
 
 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
 {
+    BOOL AclUiAvailable;
+    HMENU hEditMenu;
+    TCHAR szBuffer[256];
+
     WNDCLASSEX wcFrame = {
                              sizeof(WNDCLASSEX),
                              CS_HREDRAW | CS_VREDRAW/*style*/,
@@ -102,11 +114,31 @@ BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
     ATOM hChildWndClass = RegisterClassEx(&wcChild); /* register child windows class */
     hChildWndClass = hChildWndClass; /* warning eater */
 
+    RegisterHexEditorClass(hInstance);
+
     hMenuFrame = LoadMenu(hInstance, MAKEINTRESOURCE(IDR_REGEDIT_MENU));
+    hPopupMenus = LoadMenu(hInstance, MAKEINTRESOURCE(IDR_POPUP_MENUS));
 
     /* Initialize the Windows Common Controls DLL */
     InitCommonControls();
 
+    hEditMenu = GetSubMenu(hMenuFrame, 1);
+
+    AclUiAvailable = InitializeAclUiDll();
+    if(!AclUiAvailable)
+    {
+      /* hide the Edit/Permissions... menu entry */
+      if(hEditMenu != NULL)
+      {
+        RemoveMenu(hEditMenu, ID_EDIT_PERMISSIONS, MF_BYCOMMAND);
+        /* remove the separator after the menu item */
+        RemoveMenu(hEditMenu, 4, MF_BYPOSITION);
+      }
+    }
+
+    if(hEditMenu != NULL)
+        SetMenuDefaultItem(hEditMenu, ID_EDIT_MODIFY, MF_BYCOMMAND);
+
     nClipboardFormat = RegisterClipboardFormat(strClipboardFormat);
     /* if (nClipboardFormat == 0) {
         DWORD dwError = GetLastError();
@@ -129,6 +161,15 @@ BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
         SetupStatusBar(hFrameWnd, FALSE);
         CheckMenuItem(GetSubMenu(hMenuFrame, ID_VIEW_MENU), ID_VIEW_STATUSBAR, MF_BYCOMMAND|MF_CHECKED);
     }
+
+    /* Restore position */
+    if (RegQueryStringValue(HKEY_CURRENT_USER, g_szGeneralRegKey,
+        _T("LastKey"),
+        szBuffer, sizeof(szBuffer) / sizeof(szBuffer[0])) == ERROR_SUCCESS)
+    {
+        SelectNode(g_pChildWnd->hTreeWnd, szBuffer);
+    }
+
     ShowWindow(hFrameWnd, nCmdShow);
     UpdateWindow(hFrameWnd);
     return TRUE;
@@ -136,9 +177,30 @@ BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
 
 /******************************************************************************/
 
-void ExitInstance(void)
+/* we need to destroy the main menu before destroying the main window
+   to avoid a memory leak */
+
+void DestroyMainMenu() {
+       DestroyMenu(hMenuFrame);
+}
+
+/******************************************************************************/
+
+void ExitInstance(HINSTANCE hInstance)
 {
-    DestroyMenu(hMenuFrame);
+    UnregisterHexEditorClass(hInstance);
+    
+    DestroyMenu(hPopupMenus);
+    UnloadAclUiDll();
+}
+
+BOOL TranslateChildTabMessage(MSG *msg)
+{
+  if (msg->message != WM_KEYDOWN) return FALSE;
+  if (msg->wParam != VK_TAB) return FALSE;
+  if (GetParent(msg->hwnd) != g_pChildWnd->hWnd) return FALSE;
+  PostMessage(g_pChildWnd->hWnd, WM_COMMAND, ID_SWITCH_PANELS, 0);
+  return TRUE;
 }
 
 int APIENTRY WinMain(HINSTANCE hInstance,
@@ -148,6 +210,7 @@ int APIENTRY WinMain(HINSTANCE hInstance,
 {
     MSG msg;
     HACCEL hAccel;
+
     /*
         int hCrt;
         FILE *hf;
@@ -156,7 +219,7 @@ int APIENTRY WinMain(HINSTANCE hInstance,
         hf = _fdopen(hCrt, "w");
         *stdout = *hf;
         setvbuf(stdout, NULL, _IONBF, 0);
-     
+
        wprintf(L"command line exit, hInstance = %d\n", hInstance);
        getch();
        FreeConsole();
@@ -179,15 +242,17 @@ int APIENTRY WinMain(HINSTANCE hInstance,
     if (!InitInstance(hInstance, nCmdShow)) {
         return FALSE;
     }
-    hAccel = LoadAccelerators(hInstance, (LPCTSTR)IDC_REGEDIT);
+    hAccel = LoadAccelerators(hInstance, MAKEINTRESOURCE(ID_ACCEL));
 
     /* Main message loop */
     while (GetMessage(&msg, (HWND)NULL, 0, 0)) {
-        if (!TranslateAccelerator(msg.hwnd, hAccel, &msg)) {
+        if (!TranslateAccelerator(hFrameWnd, hAccel, &msg)
+            && !TranslateChildTabMessage(&msg)) {
             TranslateMessage(&msg);
             DispatchMessage(&msg);
         }
     }
-    ExitInstance();
+    
+    ExitInstance(hInstance);
     return msg.wParam;
 }