// ----------------------------------------
// Приложение BMPINFO
// Просмотр и анализ bmp-файлов в формате DIB
// с возможностью копирования
// соответствующего DDB и палитры в Clipboard
// ----------------------------------------

#define STRICT
#include <windows.h>
#include <windowsx.h>
#include <commdlg.h>
#include <mem.h>
#pragma hdrstop

#include "dib.hpp"
#include "bmpinfo.hpp"

BOOL InitApp(HINSTANCE);
LRESULT CALLBACK _export WndProc(HWND, UINT, WPARAM, LPARAM);

char const szClassName[]   = "BmpInfoClass";
char const szWindowTitle[] = "Bitmap Information";
short cxClient, cyClient;

// =====================================
// Функция WinMain
// =====================================
#pragma argsused

int PASCAL
WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
        LPSTR lpszCmdLine, int nCmdShow)
{
  MSG  msg;
  HWND hwnd;

  if(!hPrevInstance)
    if(!InitApp(hInstance))
      return FALSE;

  hwnd = CreateWindow(
    szClassName, szWindowTitle,
    WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT, CW_USEDEFAULT,
    CW_USEDEFAULT, CW_USEDEFAULT,
    0, 0, hInstance, NULL);      
                       
  if(!hwnd)
    return FALSE;

  ShowWindow(hwnd, nCmdShow);
  UpdateWindow(hwnd);

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

// =====================================
// Функция InitApp
// Выполняет регистрацию класса окна
// =====================================
BOOL
InitApp(HINSTANCE hInstance)
{
  ATOM aWndClass; // атом для кода возврата
  WNDCLASS wc;    // структура для регистрации

  memset(&wc, 0, sizeof(wc));
  wc.lpszMenuName  = "APP_MENU";
  wc.style         = CS_HREDRAW | CS_VREDRAW;
  wc.lpfnWndProc   = (WNDPROC) WndProc;
  wc.cbClsExtra    = 0;
  wc.cbWndExtra    = 0;
  wc.hInstance     = hInstance;
  wc.hIcon         = LoadIcon(hInstance, "APP_ICON");
  wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  wc.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH);
  wc.lpszClassName = (LPSTR)szClassName;

  aWndClass = RegisterClass(&wc);
  return (aWndClass != 0);
}

// =====================================
// Функция WndProc
// =====================================
LRESULT CALLBACK _export
WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
  HDC hdc;
  PAINTSTRUCT ps;

  static HFILE hfDIBFile;
  static HDIB hDib;
  static HPALETTE hPal, hOldPal;
  static DWORD dwFileSize;

  switch (msg)
  {
    case WM_CREATE:
    {
      hfDIBFile = NULL;
      hDib = NULL;
      hPal = NULL;
      return 0;
    }
    case WM_SIZE:
    {
      cxClient = LOWORD(lParam);
      cyClient = HIWORD(lParam);
      return 0;
    }
    case WM_PAINT:
    {
      hdc = BeginPaint(hwnd, &ps);
      if((hDib != NULL) &&
         (DIBType(hDib) == WINRGB_DIB))
      {
        if(hPal)
        {
          hOldPal = SelectPalette(hdc, hPal, FALSE);
          RealizePalette(hdc);
        }
        DIBPaint(hdc, 0, 0, hDib);
        if(hPal)
        {
          SelectPalette(hdc, hOldPal, FALSE);
        }
      }
      else
      {
        if(hDib)
          DIBInfo(hDib, dwFileSize);
      }
      EndPaint(hwnd, &ps);
      return 0;
    }
    case WM_COMMAND:
    {
      switch (wParam)
      {
        case CM_HELPABOUT:
        {
          MessageBox(hwnd,
            "Bitmap Information, v.1.1\n"
            "(C) Frolov A.V., 1995",
            "About BMPINFO", MB_OK | MB_ICONINFORMATION);
          return 0;
        }
        case CM_FILEOPEN:
        {
          hfDIBFile = DIBSelectFile();
          if(hfDIBFile != NULL)
          {
            if(hDib)
              GlobalFree(hDib);
            hDib = DIBReadFile(hfDIBFile, &dwFileSize);
            if((hDib != NULL) &&
               (DIBType(hDib) == WINRGB_DIB))
            {
              if(hPal)
                 DeletePalette(hPal);
              hPal = DIBCreatePalette(hDib);
            }
            InvalidateRect(hwnd, NULL, TRUE);
          }
          return 0;
        }
        case CM_FILEINFO:
        {
          if(hDib != NULL)
            DIBInfo(hDib, dwFileSize);

          return 0;
        }
        case CM_FILEEXIT:
        {
          DestroyWindow(hwnd);
          return 0;
        }

        // Копирование рисунка в Clipboard
        case CM_EDITCOPY:
        {
          if(hDib == NULL) return 0;
          DIBCopyToClipboard(hDib, hwnd);
          return 0;
        }
      }
    }

    case WM_PALETTECHANGED:
    {
       if (hwnd == (HWND) wParam)
         break;
    }
    case WM_QUERYNEWPALETTE:
    {
      HDC hdc;
      HPALETTE hOldPal;
      int nChanged;

      hdc = GetDC(hwnd);

      hOldPal = SelectPalette(hdc, hPal,
        (msg == WM_QUERYNEWPALETTE) ? FALSE : TRUE);

      nChanged = RealizePalette(hdc);
      SelectPalette(hdc, hOldPal, TRUE);

      ReleaseDC(hwnd, hdc);

      if(nChanged)
        InvalidateRect(hwnd, NULL, TRUE);

      return nChanged;
    }

    case WM_DESTROY:
    {
      if(hPal)
        DeletePalette(hPal);
      if(hDib)
        GlobalFree(hDib);

      PostQuitMessage(0);
      return 0;
    }

    default:
      break;
  }
  return DefWindowProc(hwnd, msg, wParam, lParam);
}
