/*
 * $Header: /home/gene/library/website/docsrc/sdkskel/src/RCS/demo0000.c,v 395.1 2008/04/20 17:25:51 gene Exp $
 *
 * Copyright (c) 2005 Gene Michael Stover.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 * USA
 */
/*
 * Simple window.  It overloads nothing except WM_DESTROY.
 */

#include "this.h"

/*
 */
static LRESULT CALLBACK
S_OnDestroy (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
  PostQuitMessage (0);
  return DefWindowProc (hwnd, uMsg, wParam, lParam);
}

/*
 */
static int
S_Init (HINSTANCE hInstance)
{
  int rc = 0;
  WNDCLASS wndclass;
  MsgMap *map;

  setbuf (stdout, NULL);
  bzero (&wndclass, sizeof wndclass);
  wndclass.lpfnWndProc = &GWP_WindowProc;
  wndclass.hInstance = hInstance;
  wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
  wndclass.hbrBackground = GetStockObject (DKGRAY_BRUSH);
  wndclass.lpszClassName = __FILE__;
  map = MSGMAP_Create ();
  MSGMAP_Insert (WM_DESTROY, &S_OnDestroy, map);
  if (GWP_RegisterClass (&wndclass, map, &DefWindowProc)) {
    /* good */
  } else {
    printf ("\n%s:%d: GWP_RegisterClass failed", __FILE__, __LINE__);
    rc = 44;
  }
  return rc;
}

/*
 */
static HWND
S_CreateWindow (HINSTANCE hInstance, int nShowCmd)
{
  HWND wnd;

  wnd = CreateWindow (__FILE__, /* class name */
                      __FILE__, /* window name */
                      WS_OVERLAPPEDWINDOW,
                      CW_USEDEFAULT, /* x position */
                      CW_USEDEFAULT, /* y position */
                      CW_USEDEFAULT, /* width */
                      CW_USEDEFAULT, /* height */
                      NULL, /* parent window */
                      NULL, /* menu */
                      hInstance,
                      NULL); /* not used */
  if (wnd != NULL) {
    ShowWindow (wnd, nShowCmd);
  } else {
    LOG_LastError ("CreateWindow", __FILE__, __LINE__);
  }
  return wnd;
}

/*
 */
static int
S_Loop ()
{
  int rc = 0;
  MSG msg;

  while (rc == 0 && GetMessage (&msg, NULL, 0, 0)) {
    TranslateMessage (&msg);
    DispatchMessage (&msg);
  }
  return rc;
}

/*
 */
static void
S_Uninit ()
{
}

/*
 */
int WINAPI
WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, char lpCmdLine[],
         int nShowCmd)
{
  int rc = 0;
  HWND wnd;

  if (S_Init (hInstance) == 0) {
    wnd = S_CreateWindow (hInstance, nShowCmd);
    if (wnd != NULL) {
      if (S_Loop () == 0) {
        /* good */
      } else {
        printf ("\n%s:%d: S_Loop failed", __FILE__, __LINE__);
        rc = -11;
      }
    } else {
      printf ("\n%s:%d: S_CreateWindow failed", __FILE__, __LINE__);
      rc = 40;
    }
    S_Uninit ();
  } else {
    printf ("\n%s:%d: S_Init failed", __FILE__, __LINE__);
    rc = 3;
  }
  return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}

/* --- end of file --- */
