WITH   Ada.Command_Line ;
WITH   Interfaces.C ;
WITH   System ;
WITH   Win32 ;
WITH   Win32.Crt.Strings ;
WITH   Win32.Windef ;
WITH   Win32.Wingdi ;
WITH   Win32.Winmain ;
WITH   Win32.Winuser ;
WITH   CharTb33_Pkg ;  USE CharTb33_Pkg;

PROCEDURE   CharTb33  IS
   USE TYPE   System.Address ;
   USE TYPE   Win32.BOOL ;
   USE TYPE   Win32.Windef.ATOM ;
   USE TYPE   Win32.CHAR_Array ;      --- To get "&"

   Nul : Win32.Char RENAMES Win32.Nul ;

   IgnoredL : Win32.LONG ;

   G_HInstance     : Win32.Windef.HINSTANCE ;
   G_HPrevInstance : Win32.Windef.HINSTANCE ;
   --G_CmdLinePtr    : Win32.PCSTR ;
   G_CmdShow       : Win32.INT ;

   C_MainWindow_ClassName : CONSTANT Win32.CHAR_Array := "CharTb33"  &  Nul ;

   C_MainWndTitle : CONSTANT Win32.CHAR_Array := "CharTb33..."  &  Nul ;

   G_MainWindow : Win32.Windef.HWND ;

   C_sCantRegClass : CONSTANT Win32.CHAR_Array := "Can not register Class."  &  Nul ;
   C_sERROR        : CONSTANT Win32.CHAR_Array := "ERROR"  &  Nul ;

   C_sDotDotDot : CONSTANT Win32.CHAR_Array := "..., "  &  Nul ;

BEGIN
   G_HInstance     := Win32.Winmain.Get_hInstance ;
   G_HPrevInstance := Win32.Winmain.Get_hPrevInstance ;
   --G_CmdLinePtr    := Win32.Winmain.Get_lpCmdline ;
   G_CmdShow       := Win32.Winmain.Get_nCmdShow ;

   ----- 1. Register  MainWindow  class
   IF G_HPrevInstance = System.Null_Address THEN
      C_MainWndClass.style         :=    Win32.Winuser.CS_HREDRAW
                                      or Win32.Winuser.CS_VREDRAW ;
      C_MainWndClass.lpfnWndProc   := MainWindow_Proc'Access ;
      C_MainWndClass.cbClsExtra    := 0 ;
      C_MainWndClass.cbWndExtra    := 0 ;
      C_MainWndClass.hInstance     := G_HInstance ;
      C_MainWndClass.hIcon         := Win32.Winuser.LoadIcon ( System.Null_Address
                                         , Win32.LPCSTR(Win32.Winuser.IDI_APPLICATION) );
      C_MainWndClass.hCursor       := Win32.Winuser.LoadCursor ( System.Null_Address
                                                               , Win32.LPCSTR(Win32.Winuser.IDC_ARROW) );
      C_MainWndClass.hbrBackground := Win32.Wingdi.GetStockObject ( Win32.Wingdi.WHITE_BRUSH );
      C_MainWndClass.lpszMenuName  := null ;      --- Must match with ressource name
      C_MainWndClass.lpszClassName := CP(C_MainWindow_ClassName);

      IF Win32.Winuser.RegisterClass ( C_MainWndClass'Access ) = Win32.Windef.ATOM(0) THEN
         IgnoredI := Win32.Winuser.MessageBox ( Win32.Winuser.GetFocus
                                              , CP(C_sCantRegClass)
                                              , CP(C_sERROR)
                                              , Win32.Winuser.MB_OK or Win32.Winuser.MB_ICONEXCLAMATION );

         Ada.Command_Line.Set_Exit_Status ( Ada.Command_Line.Exit_Status( 255 ) );
         GOTO L_EXIT ;
      END IF;
   END IF;

   ----- Prepare  MainWindow  global parameters
   G_MainPaintedOnceQ := Standard.FALSE ;
   IgnoredP := Win32.Crt.Strings.StrCpy ( CPS(G_MainMsgBuf) , CP(C_sDotDotDot) );

   ----- 2. Create  MainWindow
   G_MainWindow := Win32.Winuser.CreateWindow (
                        CP(C_MainWindow_ClassName)             --ClassName
                      , CP(C_MainWndTitle)                     --WindowName
                      , Win32.Winuser.WS_OVERLAPPEDWINDOW      --Style
                      , Win32.Winuser.CW_USEDEFAULT            --X,Y
                      , Win32.Winuser.CW_USEDEFAULT
                      , Win32.Winuser.CW_USEDEFAULT            --Width,Height
                      , Win32.Winuser.CW_USEDEFAULT
                      , System.Null_Address                    --HWndParent
                      , System.Null_Address                    --HMenu
                      , G_HInstance                            --HInstance
                      , System.Null_Address                    --Param
                   );

   ----- 3. Show  MainWindow
   IgnoredB := Win32.Winuser.ShowWindow ( G_MainWindow , G_CmdShow );
   IgnoredB := Win32.Winuser.UpdateWindow ( G_MainWindow );

   ----- LOOP , Process messages
   WHILE Win32.Winuser.GetMessage ( G_Message'Access , System.Null_Address , 0 , 0 ) /= 0 LOOP
      IgnoredB := Win32.Winuser.TranslateMessage ( G_Message'Access );
      IgnoredL := Win32.Winuser.DispatchMessage ( G_Message'Access );
   END LOOP;

   Ada.Command_Line.Set_Exit_Status ( Ada.Command_Line.Exit_Status( G_Message.wParam ) );
  <<L_EXIT>>
   BEGIN IgnoredL := 0 ; END;
END CharTb33;
