#include <common/accelerator.hpp>
#include <common/guiwnd.hpp>

Accelerator::Accelerator()
: mhAccelerator(0)
{
}			

Accelerator::Accelerator(const String &strTableName,HINSTANCE hInstance)
: mhAccelerator(0)
{
	mhAccelerator=::LoadAccelerators(hInstance,strTableName);
}

Accelerator::Accelerator(UINT resID,HINSTANCE hInstance)
: mhAccelerator(0)
{
	mhAccelerator=::LoadAccelerators(hInstance,MAKEINTRESOURCE(resID));
}

Accelerator::~Accelerator()
{
  destroy();
}

Accelerator &Accelerator::operator=(const Accelerator &/*accelerator*/)
{
  return *this;
}
 
bool Accelerator::loadAccelerators(const String &strTableName,HINSTANCE hInstance)
{
  destroy();
	mhAccelerator=::LoadAccelerators(hInstance,strTableName);
	return true;
}

bool Accelerator::loadAccelerators(UINT resID,HINSTANCE hInstance)
{
  destroy();
	mhAccelerator=::LoadAccelerators(hInstance,MAKEINTRESOURCE(resID));
	return true;
}

bool Accelerator::translate(const GUIWindow &parent,MSG &msg)const
{
  if(!isOkay())return false;
  return ::TranslateAccelerator(parent,mhAccelerator,&msg);
} 

bool Accelerator::translateMDISysAccelerator(const GUIWindow &parent,MSG &msg)const
{
  HWND hClient;
  if(!isOkay())return false;
	hClient=parent.getChild();
	return ::TranslateMDISysAccel(hClient,&msg);
}

void Accelerator::destroy()
{
  if(!isOkay())return;
	::DestroyAcceleratorTable(mhAccelerator);
	mhAccelerator=0;
}

bool Accelerator::isOkay(void)const
{
  return mhAccelerator?true:false;
}
