//#############################################################################
//##
//##  TBarDemo.cpp 
//##  
//##  Developer: Henry Guennadi Levkin
//##
//##  Version 0.4
//##
//##  Template application for using of toolbar and status line
//##
//##
//#############################################################################

#include <windows.h>

#include <commctrl.h> // comctl32.lib

#include "resource.h"

//-----------------------------------------------------------------------------
#define IDC_STATUSWND 20000

HINSTANCE ghInst;

//-----------------------------------------------------------------------------
// message handler
//-----------------------------------------------------------------------------
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
  TBBUTTON pTbButtons[10] =
  {
    {0, ID_BUTTON0, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0L, 0},
    {1, ID_BUTTON1, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0L, 0},
    {2, ID_BUTTON2, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0L, 0},
  };

  int wmId, wmEvent;

  static HWND hStatusWnd;
  static HWND hToolbarWnd;

  switch (message)
  {
  //-----------------------------------
  case WM_CREATE:
    InitCommonControls();
    hStatusWnd = CreateStatusWindow(WS_CHILD|WS_VISIBLE|CCS_BOTTOM|SBARS_SIZEGRIP,
                              "informaion in statusbar", hWnd, IDC_STATUSWND);
    hToolbarWnd = CreateToolbarEx(hWnd, WS_CHILD|WS_BORDER|WS_VISIBLE,
                            IDR_TOOLBAR, 3, ghInst, IDR_TOOLBAR,  pTbButtons, 3,
                            16, 15, 200, 20, sizeof(TBBUTTON));
    if(hToolbarWnd)
    {
      HBITMAP hBMPToolbar;
      TBADDBITMAP tbBmp;
      hBMPToolbar = CreateMappedBitmap(ghInst, IDR_TOOLBAR, 0, NULL, 0);
      tbBmp.hInst = NULL;
      tbBmp.nID =(UINT) hBMPToolbar;
      SendMessage(hToolbarWnd, TB_ADDBITMAP, 1, (LPARAM)&tbBmp);
  }
  break;
    
  //-----------------------------------
  case WM_SIZE:
    {
      RECT rMain, rStatus;
      GetClientRect(hWnd, &rMain);
      GetClientRect(hStatusWnd, &rStatus);
      MoveWindow(hStatusWnd, 0, rMain.bottom-rStatus.bottom,
                 LOWORD(lParam), HIWORD(lParam), TRUE);
    }
    break;

  //-----------------------------------
  case WM_COMMAND:
    wmId    = LOWORD(wParam);
    wmEvent = HIWORD(wParam);
    switch (wmId)
    {
    case ID_BUTTON0: // from toolbar
      SetWindowText(hStatusWnd, "BUTTON 0 PUSH");
      Beep(100, 100);
      break;
    case ID_BUTTON1: // from toolbar
      SetWindowText(hStatusWnd, "BUTTON 1 PUSH");
      Beep(400, 100);
      break;
    case ID_BUTTON2: // from toolbar
      SetWindowText(hStatusWnd, "BUTTON 2 PUSH");
      Beep(1500, 100);
      break;
    case IDM_EXIT: // from menu
      DestroyWindow(hWnd);
      break;
    default:
      return DefWindowProc(hWnd, message, wParam, lParam);
    }
    break;

  //-----------------------------------
  case WM_DESTROY:
    PostQuitMessage(0);
    break;

  //-----------------------------------
  default:
    return DefWindowProc(hWnd, message, wParam, lParam);
  }
  return 0;
}

//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
ATOM MyRegisterClass(HINSTANCE hInstance)
{
  WNDCLASSEX wcex;

  wcex.cbSize = sizeof(WNDCLASSEX);

  wcex.style        = CS_HREDRAW | CS_VREDRAW;
  wcex.lpfnWndProc  = (WNDPROC)WndProc;
  wcex.cbClsExtra	  = 0;
  wcex.cbWndExtra   = 0;
  wcex.hInstance    = hInstance;
  wcex.hIcon        = NULL;//LoadIcon(hInstance, (LPCTSTR)IDI_ICON);
  wcex.hCursor      = LoadCursor(NULL, IDC_ARROW);
  wcex.hbrBackground= (HBRUSH)(COLOR_WINDOW+1);
  wcex.lpszMenuName = MAKEINTRESOURCE(IDR_MENU);
  wcex.lpszClassName= "TBarClass";
  wcex.hIconSm      = NULL;//LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

  return RegisterClassEx(&wcex);
}

//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
  HWND hWnd;

  hWnd = CreateWindow("TBarClass", "DemoApplication", WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

  if (!hWnd)
  {
    return FALSE;
  }

  ShowWindow(hWnd, nCmdShow);
  UpdateWindow(hWnd);

  return TRUE;
}

//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
  MSG msg;

  ghInst = hInstance;

  MyRegisterClass(hInstance);

  // Perform application initialization:
  if (!InitInstance (hInstance, nCmdShow))
  {
    return FALSE;
  }

  // Main message loop:
  while (GetMessage(&msg, NULL, 0, 0))
  {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }

  return msg.wParam;
}
