#define INCL_PM
#define INCL_VIO
#define INCL_DOSERRORS
#define INCL_DOSPROCESS

#include <OS2.h>
#include <Stdio.h>
#include <String.h>

#define Main main

#define ESC_CLR_RESET      "\x1B" "[0m"
#define ESC_CLR_BOLD       "\x1B" "[1m"
#define ESC_CLR_DIM        "\x1B" "[2m"
#define ESC_CLR_ITALIC     "\x1B" "[3m"

#define ESC_CLR_BLACK      "\x1B" "[30m"
#define ESC_CLR_RED        "\x1B" "[31m"
#define ESC_CLR_GREEN      "\x1B" "[32m"
#define ESC_CLR_YELLOW     "\x1B" "[33m"
#define ESC_CLR_BLUE       "\x1B" "[34m"
#define ESC_CLR_PINK       "\x1B" "[35m"
#define ESC_CLR_CYAN       "\x1B" "[36m"
#define ESC_CLR_WHITE      "\x1B" "[37m"

HAB MixedPMVIO_Ascend ()
{
  PPIB Process_info = NULL;
  PTIB Thread_info = NULL;

  DosGetInfoBlocks (&Thread_info, &Process_info);
  if (!Process_info || !Thread_info) DosExit (EXIT_PROCESS, ERROR_ARENA_TRASHED);

  if (Process_info->pib_ultype == PROG_WINDOWABLEVIO)
  {
    Process_info->pib_ultype = PROG_PM; return WinInitialize (0);
  }
  else
  {
    return NULLHANDLE;
  }
}

MixedPMVIO_Descend (HAB Shell_handle)
{
  if (Shell_handle)
  {
    PPIB Process_info = NULL;
    PTIB Thread_info = NULL;

    DosGetInfoBlocks (&Thread_info, &Process_info);
    if (!Process_info || !Thread_info) DosExit (EXIT_PROCESS, ERROR_ARENA_TRASHED);

    if (Process_info->pib_ultype == PROG_PM)
    {
      WinTerminate (Shell_handle); Process_info->pib_ultype = PROG_WINDOWABLEVIO;
    }
    else
    {
      DosExit (EXIT_PROCESS, ERROR_BAD_ENVIRONMENT);
    }
  }
  else
  {
    DosExit (EXIT_PROCESS, ERROR_INVALID_HANDLE);
  }

  return 0;
}

VIO_SetColor (PCHAR Color)
{
  printf (Color);

  return 0;
}

Main ()
{
  HAB Shell_handle = NULLHANDLE;
  HMQ Msg_queue = NULLHANDLE;

  VIO_SetColor (ESC_CLR_WHITE);
  printf ("\n"); printf (" Hello World!" "\n");

  VIO_SetColor (ESC_CLR_CYAN);
  printf ("   Morphing into PM application..." "\n");

  Shell_handle = MixedPMVIO_Ascend ();
  if (!Shell_handle) DosExit (EXIT_PROCESS, ERROR_INVALID_FUNCTION);

  Msg_queue = WinCreateMsgQueue (Shell_handle, 0);
  if (!Msg_queue) DosExit (EXIT_PROCESS, ERROR_INVALID_FUNCTION);

  VIO_SetColor (ESC_CLR_GREEN);
  printf ("   Calling " "\"" "WinMessageBox()" "\"" "..." "\n");

  WinMessageBox (HWND_DESKTOP, NULLHANDLE, "Hello World!", "", 0, 0);

  WinDestroyMsgQueue (Msg_queue);

  VIO_SetColor (ESC_CLR_CYAN);
  printf ("   Morphing into VIO application..." "\n");

  MixedPMVIO_Descend (Shell_handle);

  VIO_SetColor (ESC_CLR_WHITE);
  printf (" All done" "\n");

  VIO_SetColor (ESC_CLR_RESET);

  return 0;
}