//-------------------- testBmp.cpp ---------------------------
//-----------------------------------------------------------
#include <windows.h>
//-----------------------------------------------------------
#include "resource.h"

#include "pRollTraspBitmap.h"
#include "pSeqTraspBitmap.h"
//-----------------------------------------------------------
static const char* ClassName = "TestBitmapClass";
HINSTANCE ghInstance = 0;
HWND ghWndMain = 0;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
//-------------------------------------------------
#define MAX_SEQ 8
static PBitmap *bmpBase;
static PBitmap *bmpBaseSeq[MAX_SEQ];
static PBitmap *bmpBaseRoll;

static PRollTraspBitmap *bmpRoll;
static PSeqTraspBitmap *bmpSeq;

static POINT ptBase = { 10, 10 };
static SIZE  szBase = { 300, 182 };
static POINT ptSeq = { ptBase.x, 10 + ptBase.y };
static POINT ptRoll = { 0 + ptBase.x, 150 + ptBase.y };
static POINT ptStep = { 3, 0 };
static int idTimer;
//-----------------------------------------------------------
static int getMinWidth()
{
  return szBase.cx + ptBase.x * 2 +
         GetSystemMetrics(SM_CXEDGE) * 4;
}
//-----------------------------------------------------------
static int getMinHeight()
{
  return szBase.cy + ptBase.y * 2 +
         GetSystemMetrics(SM_CYCAPTION) +
         GetSystemMetrics(SM_CYEDGE) * 4;
}
//-----------------------------------------------------------
int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE, //hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
  WNDCLASS wc;

  ghInstance = hInstance;

  wc.style        = CS_HREDRAW | CS_VREDRAW;
  wc.lpfnWndProc    = (WNDPROC) WndProc;
  wc.cbClsExtra   = 0;
  wc.cbWndExtra   = 0;
  wc.hInstance    = hInstance;
  wc.hIcon        = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON));
  wc.hCursor      = LoadCursor(NULL, IDC_ARROW);
  wc.hbrBackground  = (HBRUSH)(COLOR_WINDOW);
  wc.lpszMenuName = 0;
  wc.lpszClassName  = ClassName;
  RegisterClass(&wc);

  ghWndMain = CreateWindow(
        ClassName,
        "Test Bitmap",
        WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        getMinWidth(),
        getMinHeight(),
        0,
        0,
        ghInstance,
        NULL
        );

  ShowWindow(ghWndMain, nCmdShow);
  UpdateWindow(ghWndMain);

  MSG msg;
  while(GetMessage(&msg, NULL, 0, 0)) {
    if(!IsDialogMessage(msg.hwnd, &msg)) {
      TranslateMessage(&msg);
      DispatchMessage(&msg);
      }
    }
   return msg.wParam;
}
//-------------------------------------------------
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
  switch(uMsg)  {
    case WM_CREATE:
      {
      SetTimer(hWnd, idTimer = 1, 100, 0);
      bmpBase = new PBitmap(IDB_BITMAP_BASE, ghInstance);
      for(int i = 0; i < MAX_SEQ; ++i)
        bmpBaseSeq[i] = new PBitmap(IDB_BITMAP1 + i, ghInstance);
      bmpSeq = new PSeqTraspBitmap(hWnd, bmpBaseSeq, MAX_SEQ, ptSeq);
      bmpBaseRoll = new PBitmap(IDB_BITMAP_ROLL, ghInstance);
      bmpRoll = new PRollTraspBitmap(hWnd, bmpBaseRoll, ptRoll, ptStep, true);
      }
      break;
    case WM_GETMINMAXINFO:
      {
      LPMINMAXINFO lpmmi = (LPMINMAXINFO) lParam;
      lpmmi->ptMinTrackSize.x = getMinWidth();
      lpmmi->ptMinTrackSize.y = getMinHeight();
      }
      break;
    case WM_PAINT:
      {
      RECT r;
      if(!GetUpdateRect(hWnd, &r, true))
        break;
      PAINTSTRUCT Paint;
      HDC hdc = BeginPaint(hWnd, &Paint);
      bmpBase->draw(hdc, ptBase);
      bmpRoll->Draw(hdc);
      bmpSeq->Draw(hdc);
      EndPaint(hWnd, &Paint);
      }
      break;
    case WM_TIMER:
      bmpRoll->next();
      {
      int curr = bmpSeq->getCurr();
      int tot =  bmpSeq->getNBmp();
      curr = (curr + 1) % tot;
      
      // se si sposta, cattura lo sfondo anche se la window è coperta
      // verificando se ha il focus si riducono i problemi
      if(!curr && hWnd == GetFocus()) {
        POINT newpos = bmpSeq->getPoint();
        newpos.x += 3;
        if(newpos.x >= szBase.cx + ptBase.x)
          newpos.x = ptBase.x;
        bmpSeq->moveTo(newpos, false);
        }
      bmpSeq->setCurr(curr);
      }
      break;
    case WM_DESTROY:
      if(idTimer) {
        KillTimer(hWnd, idTimer);
        idTimer = 0;
        }
      delete bmpBase;
      for(int i = 0; i < MAX_SEQ; ++i)
        delete bmpBaseSeq[i];
      delete bmpSeq;
      delete bmpRoll;
      PostQuitMessage(0);
      break;
  }
  return DefWindowProc(hWnd, uMsg, wParam, lParam);
}