//
// CControlWinButton.cpp
//
// (c)Copyright 2004 Steven J. Eschweiler. All Rights Reserved.
//

#include "ccontrolwinbutton.h"

CControlWinButton::CControlWinButton(void)
{
}

CControlWinButton::~CControlWinButton(void)
{
}

LRESULT CControlWinButton::WndProc(HWND hwnd, UINT mMsg, WPARAM wParam, LPARAM lParam)
{
   switch (mMsg)
   {
      //case WM_KEYUP:
   case WM_KEYDOWN:
      {
         if (this->m_dwStyle&WS_TABSTOP)
         {
            // wParam could == VK_TAB, etc.
            // lParam could be WM_KEYDOWN, etc.
            PostMessage(this->GetHwndParent(),this->GetControlId(),wParam,static_cast<LPARAM>(mMsg));
         }
         break;
      }
   }

   return CBaseClassWindow::WndProc(hwnd, mMsg, wParam, lParam);
}

void CControlWinButton::OnCreate()
{
   /* Alternate Method:
   static HFONT hFont;
   // Load (create) the font
   int nHeight=14;
   hFont = CreateFont( nHeight,
      0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
      ANSI_CHARSET,
      OUT_DEFAULT_PRECIS,
      CLIP_DEFAULT_PRECIS,
      DEFAULT_QUALITY,
      VARIABLE_PITCH,
      "Arial");
      SetFont(hFont,true);
   */
   SetFont(static_cast<HFONT>(GetStockObject(DEFAULT_GUI_FONT)),true);
}

bool CControlWinButton::Init(HWND hwndParent,UINT nControlID,LPCTSTR lpText,DWORD dwButtonStyle,int x,int y,int width,int height)
{
   // For general button info See: ms-help://MS.VSCC/MS.MSDNVS/winui/buttons_7zhv.htm
   // For dwButtonStyles, See: ms-help://MS.VSCC/MS.MSDNVS/winui/buttons_34c3.htm

   HDC hdc=GetDC(hwndParent);
   SelectObject(hdc,GetStockObject(DEFAULT_GUI_FONT));

   SIZE size_x,size_y;
   GetTextExtentPoint32(hdc,"Mg",2,&size_y);
   GetTextExtentPoint32(hdc,lpText,lstrlen(lpText),&size_x);
   ReleaseDC(hwndParent,hdc);

   if (width==0)
      size_x.cx+=32;
   else
      size_x.cx=width;
   if (height==0)
      size_y.cy=23;
   else
      size_y.cy=height;

   return (CBaseClassWindow::Init( NULL, //WS_EX_CONTROLPARENT,
      "button", // ALL CAPS REQUIRED FOR CBaseClassWindow
      lpText,
      //BS_NOTIFY|WS_VISIBLE|WS_CHILD|dwButtonStyle|WS_CLIPSIBLINGS, // WS_CLIPSIBLINGS avoid problems with moved by SplitterBar
      // DO NOT USE BS_NOTIFY OR YOU WILL GET TWO WM_COMMAND MESSAGES
      // HIWORD of WPARAM=6 for setfocus and 7 for killfocus
      // If you get 2 WM_COMMAND messages for buttons, you set BS_NOTIFY
      // for the button causing BN_SETFOCUS BN_KILLFOCUS to be sent for
      // each button click!
      WS_TABSTOP|WS_VISIBLE|WS_CHILD|dwButtonStyle|WS_CLIPSIBLINGS, // WS_CLIPSIBLINGS avoid problems with moved by SplitterBar
      x,y,size_x.cx,size_y.cy,
      reinterpret_cast<HMENU>(nControlID),
      RGB(255,255,255),NULL,NULL,false) );
}