#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include "app.h"

/*
 * Module: init.c
 *
 * Contains: Application and instance initialization
 * functions.
 *
 */


/*********************************************************************/
/* Local Function Prototypes                                         */
/*********************************************************************/


/*********************************************************************/
/* Local data and structures                                         */
/*********************************************************************/


/*********************************************************************/
/* Global functions                                                  */
/*********************************************************************/

/*-------------------------------------------------------------------*/
/* Initialize the application for only the first time its executed   */
/*-------------------------------------------------------------------*/
BOOL InitApplication(hInstance)
HANDLE hInstance;
{
  WNDCLASS   AppClass;

  AppClass.style           = NULL;
  AppClass.lpfnWndProc     = WinProc;
  AppClass.cbClsExtra      = 0;
  AppClass.cbWndExtra      = 0;
  AppClass.hInstance       = hInstance;
  AppClass.hIcon           = LoadIcon(hInstance, "ClassIcon");
  AppClass.hCursor         = LoadCursor(NULL,IDC_ARROW);
  AppClass.hbrBackground   = GetStockObject(WHITE_BRUSH);
  AppClass.lpszMenuName    = (LPSTR)"ClassMenu";
  AppClass.lpszClassName   = (LPSTR)ClassName;

  return (RegisterClass(&AppClass));
}

/*-------------------------------------------------------------------*/
/* Initialize the instance, done everytime its executed              */
/*-------------------------------------------------------------------*/

BOOL InitInstance(hInstance,nCmdShow)
HANDLE hInstance;
int nCmdShow;
{
  RECT Rect;
  char *HelpFilePtr;
  int i;

  hAccelTable = LoadAccelerators(hInstance, (LPSTR)"ClassAccel");

  hWindow = CreateWindow(
    (LPSTR)ClassName,
    (LPSTR)WindowName,
    WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT,
    CW_USEDEFAULT,
    CW_USEDEFAULT,
    CW_USEDEFAULT,
    (HWND)NULL,
    (HMENU)NULL,
    (HANDLE)hInstance,
    (LPSTR)NULL);

  if (hWindow == NULL) {
    MessageBox(
      GetFocus(),
      (LPSTR)"Could not create the Instance Class Window",
      (LPSTR)"CreateWindow Fatal Error",
      MB_ICONSTOP);
    return FALSE;
  }

  hInst = hInstance;
  hMenu = GetMenu(hWindow);

  GetClientRect(hWindow, (LPRECT) &Rect);

  hEditWindow = CreateWindow(
    "Edit",
    NULL,
    WS_CHILD | WS_VISIBLE |
    ES_MULTILINE |
    WS_VSCROLL | WS_HSCROLL |
    ES_AUTOHSCROLL | ES_AUTOVSCROLL,
    0,
    0,
    (Rect.right-Rect.left),
    (Rect.bottom-Rect.top),
    hWindow,
    IDC_EDIT,                          /* Child control i.d. */
    hInst,
    NULL);

  if (!hEditWindow) {
    DestroyWindow(hWindow);
    MessageBox(
      GetFocus(),
      (LPSTR)"Could not create the Edit Window",
      (LPSTR)"CreateWindow Fatal Error",
      MB_ICONSTOP);
    return FALSE;
  }

  i = GetModuleFileName(hInst,HelpFileName,sizeof(HelpFileName)-1);
  for (; i > 0; i--) {
    if (HelpFileName[i] == '\\' || HelpFileName[i] == ':') {
      HelpFileName[i+1] = '\0';
      break;
    }
  }

  if (i + 13 < sizeof(HelpFileName))
    strcat(HelpFileName, "sndhack.hlp");
  else
    strcpy(HelpFileName, "sndhack.hlp");

  hHourGlass = LoadCursor(NULL, IDC_WAIT);
  hNote = LoadCursor(hInstance, "NoteCursor");
  ShowWindow(hWindow, nCmdShow);
  UpdateWindow(hWindow);

  return TRUE;
}


/**********************************************************************/
/* Local functions                                                    */
/**********************************************************************/

