/*
                       Free Space

                       (c) Eric Lapaille - Belgium - 1990

                       Do not remove this comment

*/

#define NOMINMAX
#define NOCOMM
#include <windows.h>
#include <string.h>
#include <stdlib.h>
#include <dos.h>

BOOL FAR PASCAL About(HWND , unsigned , WORD , LONG );
void emm_get_num_pages( int *, int *);

#define EMS 1
#define IDM_ABOUT 100

BOOL    bMode = FALSE;
HANDLE  hInst;

void emm_get_num_pages( int *num_pages, int *unalloc_pages )
  {
  union REGS regs;

  regs.h.ah = 0x42;
  int86( 0x67, &regs, &regs );
  *num_pages = regs.x.dx;
  *unalloc_pages = regs.x.bx;
  }



long FAR PASCAL WndProc( HWND hWnd, unsigned message, WORD wParam, LONG lParam )
{
  static int mem2,mem1,lastmem1,lastmem2;
  char buffer[20];
  PAINTSTRUCT ps;
  FARPROC lpProcAbout;
  int num_pages, unalloc_pages;

  switch (message)
     {
     case WM_TIMER:
        mem1 = (int) (GetFreeSpace(0) / 1024L);
        if (bMode == EMS)
           {
           emm_get_num_pages( &num_pages, &unalloc_pages );
           mem2 = unalloc_pages * 16;
           }
        if (mem1 != lastmem1 || mem2 !=lastmem2 )
           InvalidateRect( hWnd, NULL, TRUE );
        lastmem1 = mem1;
        lastmem2 = mem2;
        break;

     case WM_PAINT:
        BeginPaint( hWnd, (LPPAINTSTRUCT)&ps );
        strcat( itoa( mem1, buffer, 10 ), "K" );
        TextOut( ps.hdc, 0, 0, buffer, strlen( buffer ) );
        if (bMode == EMS)
           {
           strcat( itoa( mem2, buffer, 10 ), "K" );
           TextOut( ps.hdc, 0, 14, buffer, strlen( buffer ) );
           }
        EndPaint( hWnd, (LPPAINTSTRUCT)&ps );
        break;

     case WM_SYSCOMMAND:
        if (wParam == IDM_ABOUT)
           {
           lpProcAbout = MakeProcInstance(About, hInst);
           DialogBox(hInst,"AboutBox",hWnd,lpProcAbout);
           FreeProcInstance(lpProcAbout);
           break;
           }
        return DefWindowProc( hWnd, message, wParam, lParam );

    case WM_DESTROY:
       KillTimer( hWnd, 1 );
       PostQuitMessage( 0 );
       break;

    default:
       return DefWindowProc( hWnd, message, wParam, lParam );
       break;
    }
    return(0L);
}

int PASCAL WinMain( HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpszCmdLine, int cmdShow )
  {
  static char szAppName[] = "FreeSpace";
  WNDCLASS WndClass;
  MSG      msg;
  HWND     hWnd;
  DWORD    dwFlags;
  HMENU    hMenu;

  if (hPrevInstance)
     return FALSE;

  WndClass.hCursor          = LoadCursor( NULL, IDC_ARROW );
  WndClass.hIcon            = NULL;
  WndClass.cbClsExtra		= 0;
  WndClass.cbWndExtra		= 0;
  WndClass.lpszMenuName		= szAppName;
  WndClass.lpszClassName	= szAppName;
  WndClass.hbrBackground	= (HBRUSH)GetStockObject( WHITE_BRUSH );
  WndClass.hInstance		= hInstance;
  WndClass.style            = CS_HREDRAW | CS_VREDRAW;
  WndClass.lpfnWndProc		= WndProc;

  if (!RegisterClass( (LPWNDCLASS)&WndClass ) )
     return(FALSE);

  hWnd = CreateWindow(szAppName, szAppName, WS_TILEDWINDOW,
		      0, 0, 0, 0, NULL, NULL, hInstance, NULL );

  if (!SetTimer( hWnd, 1, 1000, NULL ))
     return FALSE;

  hInst = hInstance;

  dwFlags = GetWinFlags();

  if (dwFlags & WF_ENHANCED)
     SetWindowText(hWnd," Enhanced Mode ");
  else
     {
     if (dwFlags & WF_STANDARD)
        SetWindowText(hWnd, " Standard Mode ");
     else
        SetWindowText(hWnd, " Real Mode ");
     }
  if (dwFlags & WF_SMALLFRAME || dwFlags & WF_LARGEFRAME)
     bMode = EMS;

  hMenu = GetSystemMenu(hWnd,FALSE);
  ChangeMenu(hMenu, 0, NULL, 999, MF_APPEND | MF_SEPARATOR);
  ChangeMenu(hMenu, 0, (LPSTR)"About", IDM_ABOUT, MF_APPEND | MF_STRING);


  ShowWindow( hWnd, SW_SHOWMINIMIZED );
  UpdateWindow( hWnd );

  while (GetMessage( &msg, NULL, 0, 0 ))
     {
     TranslateMessage( &msg );
     DispatchMessage( &msg );
     }

  return (int)msg.wParam;
  }


BOOL FAR PASCAL About(HWND hDlg, unsigned message, WORD wParam, LONG lParam)
{
    switch (message)
       {
       case WM_INITDIALOG:
          return (TRUE);

       case WM_COMMAND:
          if (wParam == IDOK || wParam == IDCANCEL)
             {
             EndDialog(hDlg, TRUE);
             return (TRUE);
             }
          break;
       }
    return (FALSE);
}
