/*
 * $Header: /home/gene/library/website/docsrc/cloop/src/RCS/test0002.c,v 1.1 2006/10/31 14:15:40 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 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 General Public License for more details.
 *
 * You should have received a copy of the GNU 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
 */

/*
 * For Windows only!
 *
 */

#include "this.h"

#define Trace() { FILE *fp; \
                  fp = fopen ("./tmp/cloop.log", "a"); \
                  if (fp != NULL) { \
                    fprintf (fp, "\n%s:%d: trace", __FILE__, __LINE__); \
                    fclose (fp); \
                  } else { \
                    MessageBox (NULL, TEXT ("fopen failed"), TEXT (__FILE__), MB_OK); \
                  } \
                }

/*
 */
static TCHAR const S_windowClassName[] =
TEXT ("com.cybertiggyr.gene.cloop.src/demow000");
/*
 */
typedef struct {
  HINSTANCE instance;
  HWND button;
  HWND url;
  HWND urlLabel;
  HWND list;
} WindowData;

/*
 */
static WindowData *
S_GetWindowData (HWND wnd)
{
  return (WindowData *) GetProp (wnd, S_windowClassName);
}

/*
 */
static LRESULT
S_OnCreate (HWND wnd, UINT msg, WPARAM warg, LPARAM larg)
{
  LRESULT rc = 0;
  WindowData *data;
  CREATESTRUCT *cs;

  rc = DefWindowProc (wnd, msg, warg, larg);
  cs = (CREATESTRUCT *) larg;
  data = new WindowData;
  SetProp (wnd, S_windowClassName, data);
  data->instance = cs->hInstance;
  data->button = CreateWindow (TEXT ("BUTTON"), TEXT ("Fetch"), WS_CHILD,
                               CW_USEDEFAULT, CW_USEDEFAULT,
                               CW_USEDEFAULT, CW_USEDEFAULT, wnd, NULL,
                               data->instance, NULL);
  ShowWindow (data->button, SW_SHOW);
  data->url = CreateWindow (TEXT ("EDIT"), TEXT ("type an url here"),
                            WS_CHILD | ES_LEFT,
                            CW_USEDEFAULT, CW_USEDEFAULT,
                            CW_USEDEFAULT, CW_USEDEFAULT, wnd, NULL,
                            data->instance, NULL);
  ShowWindow (data->url, SW_SHOW);
  data->urlLabel = CreateWindow (TEXT ("STATIC"), TEXT ("url:"), WS_CHILD,
                                 CW_USEDEFAULT, CW_USEDEFAULT,
                                 CW_USEDEFAULT, CW_USEDEFAULT, wnd, NULL,
                                 data->instance, NULL);
  ShowWindow (data->url, SW_SHOW);
  return rc;
}

/*
 */
static LRESULT
S_OnDestroy (HWND wnd, UINT msg, WPARAM warg, LPARAM larg)
{
  LRESULT rc = 0;
  WindowData *data;

  CLOOP_SetDone (0);
  data = S_GetWindowData (wnd);
  if (data->button != NULL) {
    DestroyWindow (data->button);
    data->button = NULL;
  }
  if (data->url != NULL) {
    DestroyWindow (data->url);
    data->url = NULL;
  }
  if (data->urlLabel != NULL) {
    DestroyWindow (data->urlLabel);
    data->urlLabel = NULL;
  }
  if (data->list != NULL) {
    DestroyWindow (data->list);
    data->list = NULL;
  }
  return rc;
}

/*
 */
static LRESULT
S_OnSize (HWND wnd, UINT msg, WPARAM warg, LPARAM larg)
{
  LRESULT rc = 0;
  RECT rect;
  WindowData *data;
  static int const ButtonWidth = 150;
  static int const ButtonHeight = 80;
  static int const Border = 10;
  int buttonX, urlLabelRight, urlX, urlWidth;

  rc = DefWindowProc (wnd, msg, warg, larg);
  GetClientRect (wnd, &rect);
  data = S_GetWindowData (wnd);
  if (data->button != NULL) {
    buttonX = rect.right - ButtonWidth - Border;
    MoveWindow (data->button,
                buttonX,
                Border, /* y */
                ButtonWidth,
                ButtonHeight,
                TRUE); /* repaint */
  }
  if (data->urlLabel != NULL) {
    urlLabelRight = Border + ButtonWidth;
    MoveWindow (data->urlLabel,
                Border, /* x */
                Border, /* y */
                urlLabelRight - Border,
                ButtonHeight,
                TRUE); /* repaint */
  }
  if (data->url != NULL) {
    /*
     * url is between the button & urlLabel.
     */
    urlX = urlLabelRight + Border;
    urlWidth = buttonX - Border - urlX;
    MoveWindow (data->url,
                urlX,
                Border, /* y */
                urlWidth,
                ButtonHeight,
                TRUE); /* repaint */
  }
  return rc;
}

/*
 */
static LRESULT CALLBACK
S_WindowProc (HWND wnd, UINT msg, WPARAM warg, LPARAM larg)
{
  LRESULT rc = 0;

  switch (msg) {
  case WM_CREATE:
    rc = S_OnCreate (wnd, msg, warg, larg);
    break;
  case WM_DESTROY:
    rc = S_OnDestroy (wnd, msg, warg, larg);
    break;
  case WM_SIZE:
    rc = S_OnSize (wnd, msg, warg, larg);
    break;
  default:
    rc = DefWindowProc (wnd, msg, warg, larg);
  }
  return rc;
}

/*
 */
static WNDCLASS S_wndclass = {
  0, /* style */
  &S_WindowProc,
  0, /* class extra */
  0, /* window extra */
  NULL, /* HINSTANCE */
  NULL, /* icon */
  NULL, /* cursor.  Initialize later */
  (HBRUSH) COLOR_BACKGROUND, /* background */
  NULL, /* menu */
  TEXT (__FILE__) /* class name */
};

/*
 */
static WSADATA S_wsadata;

/*
 */
static int
S_Init (HINSTANCE hInstance, HINSTANCE hPrevInstance)
{
  int rc = 0;
  TCHAR str[100];

  if (WSAStartup (2, &S_wsadata) == 0) {
    if (CLOOP_LibStartup (hInstance) == 0) {
      S_wndclass.hInstance = hInstance;
      S_wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
      if (RegisterClass (&S_wndclass)) {
        /* good */
      } else {
        StringCchPrintf (str, sizeof str / sizeof str[0],
                         TEXT ("%s:%d: RegisterClass failed"),
                         TEXT (__FILE__), __LINE__);
        MessageBox (NULL, str, TEXT (__FILE__), MB_OK);
        rc = 36;
      }
    } else {
      StringCchPrintf (str, sizeof str / sizeof str[0],
                       TEXT ("%s:%d: CLOOP_LibStartup failed"),
                       TEXT (__FILE__), __LINE__);
      MessageBox (NULL, str, TEXT (__FILE__), MB_OK);
      rc = 32;
    }
  } else {
    StringCchPrintf (str, sizeof str / sizeof str[0],
                     TEXT ("%s:%d: WSAStartup failed"),
                     TEXT (__FILE__), __LINE__);
    MessageBox (NULL, str, TEXT (__FILE__), MB_OK);
    rc = 30;
  }
  return rc;
}

/*
 */
static HWND
S_CreateWindow (HINSTANCE hInstance, int show)
{
  HWND wnd;

  wnd = CreateWindow (TEXT (__FILE__), /* class name */
                      TEXT (__FILE__), /* window name */
                      WS_OVERLAPPEDWINDOW, /* style */
                      CW_USEDEFAULT, /* x */
                      CW_USEDEFAULT, /* y */
                      CW_USEDEFAULT, /* width */
                      CW_USEDEFAULT, /* height */
                      NULL, /* parent */
                      NULL, /* menu */
                      hInstance,
                      NULL); /* param */
  if (wnd != NULL) {
    ShowWindow (wnd, show);
  }
  return wnd;
}
                      
/*
 */
static void
S_Uninit (HINSTANCE hInstance, HINSTANCE hPrevInstance)
{
  if (hPrevInstance == 0) {
    UnregisterClass (TEXT (__FILE__), hInstance);
  }
  CLOOP_LibShutdown ();
}

/*
 */
int
WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,
         int nCmdShow)
{
  int rc = 0, i;
  TCHAR str[100];
  HWND wnd;

  MessageBox (NULL, TEXT ("WinMain entry"), TEXT (__FILE__), MB_OK);
  Trace ();
  if (S_Init (hInstance, hPrevInstance) == 0) {
    MessageBox (NULL, TEXT ("Call S_CreateWindow"), TEXT (__FILE__), MB_OK);
    Trace ();
    wnd = S_CreateWindow (hInstance, nCmdShow);
    if (wnd != NULL) {
      Trace ();
      if (CLOOP_Run () == 0) {
        /* good */
      } else {
        MessageBox (NULL, TEXT ("CLOOP_Run failed"), TEXT ("Error"),
                    MB_ICONEXCLAMATION | MB_OK);
        rc = 5;
      }
    } else {
      StringCchPrintf (str, sizeof str / sizeof str[0],
                       TEXT ("%s:%d: S_CreateWindow failed"),
                       TEXT (__FILE__), __LINE__);
      MessageBox (NULL, str, TEXT ("Error"), MB_OK);
      rc = 5;
    }
    S_Uninit (hInstance, hPrevInstance);
  } else {
    MessageBox (NULL,
                TEXT ("CLOOP_LibStartup failed"),
                TEXT ("Error"),
                MB_ICONEXCLAMATION | MB_OK);
    rc = 15;
  }
  return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}

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