/*
 * $Header: /home/gene/library/website/docsrc/vwu/src/RCS/choose-compressor.c,v 395.1 2008/04/20 17:25:48 gene Exp $
 *
 * Copyright (c) 2006 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
 */

#include "this.h"

/*
 */
#define BUTTON_CHOOSE 123
#define BUTTON_SAVE 124

/*
 */
static LRESULT
S_OnCommandChoose (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
  LRESULT rc = 0;
  static UINT flags = ICMF_CHOOSE_ALLCOMPRESSORS | ICMF_CHOOSE_DATARATE |
    ICMF_CHOOSE_KEYFRAME | ICMF_CHOOSE_PREVIEW;
  COMPVARS compvars;
  BITMAPINFO bitmapinfo;
  FILE *fp;

  memset (&compvars, 0, sizeof compvars);
  compvars.cbSize = sizeof compvars;
  compvars.lpbiOut = &bitmapinfo;
  memset (&bitmapinfo, 0, sizeof bitmapinfo);
  bitmapinfo.bmiHeader.biSize = sizeof bitmapinfo;
  if (ICCompressorChoose (hwnd, flags, NULL, NULL, &compvars,
                          "ICCompressorChoose")) {
    fp = fopen ("./choose-compressor.txt", "w");
    setbuf (fp, NULL);
    fprintf (fp, "\ncbSize = %ld;", compvars.cbSize);
    fprintf (fp, "\ndwFlags = 0x%lu;", (unsigned long) compvars.dwFlags);
    fprintf (fp, "\nhic = %p;", (void *) compvars.hic);
    fprintf (fp, "\nfccType = %c%c%c%c;",
             compvars.fccType & 0x0FF,
             (compvars.fccType >> 8) & 0x0FF,
             (compvars.fccType >> 16) & 0x0FF,
             (compvars.fccType >> 24) & 0x0FF);
    fprintf (fp, "\nfccHandler = %c%c%c%c;", 
             compvars.fccHandler & 0x0FF,
             (compvars.fccHandler >> 8) & 0x0FF,
             (compvars.fccHandler >> 16) & 0x0FF,
             (compvars.fccHandler >> 24) & 0x0FF);
    fprintf (fp, "\n/* LPBITMAPINFO lpbiIn is reserved.  Do not use. */");
    fprintf (fp, "\nlpbiOut = %p;", compvars.lpbiOut);
    fprintf (fp, "\nbitmapinfo.bmiHeader.biSize = %lu;",
             (unsigned long) bitmapinfo.bmiHeader.biSize);
    fprintf (fp, "\nbitmapinfo.bmiHeader.biWidth = %ld;",
             (long) bitmapinfo.bmiHeader.biWidth);
    fprintf (fp, "\nbitmapinfo.bmiHeader.biHeight = %ld;",
             (long) bitmapinfo.bmiHeader.biHeight);
    fprintf (fp, "\nbitmapinfo.bmiHeader.biPlanes = %lu;",
             (unsigned long) bitmapinfo.bmiHeader.biPlanes);
    fprintf (fp, "\nbitmapinfo.bmiHeader.biBitCount = %lu;",
             (unsigned long) bitmapinfo.bmiHeader.biBitCount);
    fprintf (fp, "\nbitmapinfo.bmiHeader.biCompression = %lu;",
             (unsigned long) bitmapinfo.bmiHeader.biCompression);
    fprintf (fp, "\nbitmapinfo.bmiHeader.biSizeImage = %lu;",
             (unsigned long) bitmapinfo.bmiHeader.biSizeImage);
    fprintf (fp, "\nbitmapinfo.bmiHeader.biXPelsPerMeter = %ld;",
             (long) bitmapinfo.bmiHeader.biXPelsPerMeter);
    fprintf (fp, "\nbitmapinfo.bmiHeader.biYPelsPerMeter = %ld;",
             (long) bitmapinfo.bmiHeader.biYPelsPerMeter);
    fprintf (fp, "\nbitmapinfo.bmiHeader.biClrUsed = %lu;",
             (unsigned long) bitmapinfo.bmiHeader.biClrUsed);
    fprintf (fp, "\nbitmapinfo.bmiHeader.biClrImportant = %lu;",
             (unsigned long) bitmapinfo.bmiHeader.biClrImportant);
    fprintf (fp, "\n /* LPVOID lpBitsOut is reserved.  Do not use. */");
    fprintf (fp, "\n /* LPVOID lpBitsPrev is reserved.  Do not use. */");
    fprintf (fp, "\n /* LONG lFrame is reserved.  Do not use. */");
    fprintf (fp, "\nlKey = %ld;", (long) compvars.lKey);
    fprintf (fp, "\nlDataRate = %ld;", (long) compvars.lDataRate);
    fprintf (fp, "\nlQ = %ld;", (long) compvars.lQ);
    fprintf (fp, "\n /* LONG lKeyCount;  is reserved.  Do not use. */");
    fprintf (fp, "\n /* LPVOID lpState;  is reserved.  Do not use. */");
    fprintf (fp, "\n /* LONG cbState;  is reserved.  Do not use. */");
    fclose (fp);
  } else {
    printf ("\n%s:%d: ICCompressorChoose failed", __FILE__, __LINE__);
    rc = 3;
  }
  return rc;
}

/*
 */
static LRESULT
S_OnCommand (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
  LRESULT rc = 0;

  switch (wParam) {
  case BUTTON_CHOOSE:
    rc = S_OnCommandChoose (hwnd, uMsg, wParam, lParam);
    break;
  case BUTTON_SAVE:
    break;
  default:
    printf ("\n%s:%d: Unknown button id, %lu.", __FILE__, __LINE__,
            (unsigned long) wParam);
  }
  return rc;
}

/*
 */
static LRESULT
S_OnCreate (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
  LRESULT rc = 0;
  HMENU menu;

  rc = DefWindowProc (hwnd, uMsg, wParam, lParam);
  if (rc == 0) {
    menu = CreateMenu ();
    if (menu != NULL) {
      if (AppendMenu (menu, MF_STRING, BUTTON_CHOOSE, "Choose") &&
          AppendMenu (menu, MF_STRING, BUTTON_SAVE, "Save")) {
        if (SetMenu (hwnd, menu)) {
          /* good */
        } else {
          printf ("\n%s:%d: SetMenu failed", __FILE__, __LINE__);
          rc = 40;
        }
      } else {
        printf ("\n%s:%d: AppendMenu failed", __FILE__, __LINE__);
        rc = 39;
      }
    } else {
      printf ("\n%s:%d: CreateMenu failed", __FILE__, __LINE__);
      rc = 3;
    }
  } else {
    /*
     * Error.  We could log an error message here.
     */
  }
  return rc;
}

/*
 */
static LRESULT CALLBACK
S_WindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
  LRESULT rc = 0;

  switch (uMsg) {
  case WM_COMMAND:
    rc = S_OnCommand (hwnd, uMsg, wParam, lParam);
    break;
  case WM_CREATE:
    rc = S_OnCreate (hwnd, uMsg, wParam, lParam);
    break;
  case WM_DESTROY:
    PostQuitMessage (0);
    break;
  default:
    rc = DefWindowProc (hwnd, uMsg, wParam, lParam);
  }
  return rc;
}

/*
 */
static int
S_Init (HINSTANCE hInstance)
{
  int rc = 0;
  WNDCLASS wndclass;

  memset (&wndclass, 0, sizeof wndclass);
  wndclass.style = CS_HREDRAW | CS_VREDRAW;
  wndclass.lpfnWndProc = S_WindowProc;
  wndclass.cbClsExtra = 0;
  wndclass.cbWndExtra = 0;
  wndclass.hInstance = hInstance;
  wndclass.hIcon = NULL;
  wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
  wndclass.hbrBackground = GetStockObject (WHITE_BRUSH);
  wndclass.lpszMenuName = NULL;
  wndclass.lpszClassName = __FILE__;
  if (RegisterClass (&wndclass)) {
    /* good */
  } else {
    printf ("\n%s:%d: RegisterClass failed", __FILE__, __LINE__);
    rc = 16;
  }
  return rc;
}

/*
 */
static HWND
S_CreateMainWindow (HANDLE hInstance, int cmdShow)
{
  int rc = 0;
  HWND hwnd;
  
  hwnd = CreateWindow (__FILE__, __FILE__, WS_OVERLAPPEDWINDOW,
                       CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
                       CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
  if (hwnd != NULL) {
    ShowWindow (hwnd, cmdShow);
    UpdateWindow (hwnd);
  } else {
    printf ("\n%s:%d: CreateWindow failed", __FILE__, __LINE__);
  }
  return hwnd;
}

/*
 */
static int
S_Loop ()
{
  MSG msg;

  while (GetMessage (&msg, NULL, 0, 0)) {
    TranslateMessage (&msg);
    DispatchMessage (&msg);
  }
  return msg.wParam;
}

/*
 */
static void
S_Uninit ()
{
}

int
WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,
         int nCmdShow)
{
  int rc = 0;
  HWND hwnd;

  if (S_Init (hInstance) == 0) {
    hwnd = S_CreateMainWindow (hInstance, nCmdShow);
    if (hwnd != NULL) {
      if (S_Loop () == 0) {
        /* good */
      } else {
        printf ("\n%s:%d: S_Loop failed", __FILE__, __LINE__);
        rc = 11;
      }
    } else {
      printf ("\n%s:%d: S_CreateMainWindow failed", __FILE__, __LINE__);
      rc = 11;
    }
    S_Uninit ();
  } else {
    printf ("\n%s:%d: S_Init failed", __FILE__, __LINE__);
    rc = 11;
  }
  return rc;
}

/* --- end of file --- */
