// AamymovCtl.cpp : Implementation of the CAamymovCtrl ActiveX Control class.

#include "stdafx.h"
#include "aamymov.h"
#include "AamymovCtl.h"
#include "AamymovPpg.h"

////////////////////////////////////// YU >>
#include "resource.h"
#include "cmovie.h"
#include "extra.h"
////////////////////////////////////// YU <<

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define ID_MOVIE   1000

IMPLEMENT_DYNCREATE(CAamymovCtrl, COleControl)

////////////////////////////////////// YU >>
/*
static UINT BASED_CODE buttons[] =
{
	// same order as in the bitmap 'toolbar.bmp'
	ID_FILE_OPEN,
	ID_FILE_CLOSE,
		ID_SEPARATOR,
	ID_MOVIE_PLAY,
	ID_MOVIE_PAUSE,
	ID_MOVIE_STOP,
		ID_SEPARATOR,
	ID_APP_ABOUT,
};
*/
////////////////////////////////////// YU <<

/////////////////////////////////////////////////////////////////////////////
// Message map

BEGIN_MESSAGE_MAP(CAamymovCtrl, COleControl)
	//{{AFX_MSG_MAP(CAamymovCtrl)
	ON_WM_CREATE()
	ON_WM_SIZE()
	ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
	ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
	ON_COMMAND(ID_FILE_CLOSE, OnFileClose)
	ON_COMMAND(ID_MOVIE_PLAY, OnMoviePlay)
	ON_COMMAND(ID_MOVIE_PAUSE, OnMoviePause)
	ON_COMMAND(ID_MOVIE_STOP, OnMovieStop)
	ON_WM_CONTEXTMENU()
	ON_COMMAND(ID_PROPERTY, OnProperty)
	//}}AFX_MSG_MAP
	ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// Dispatch map

BEGIN_DISPATCH_MAP(CAamymovCtrl, COleControl)
	//{{AFX_DISPATCH_MAP(CAamymovCtrl)
	DISP_PROPERTY_NOTIFY(CAamymovCtrl, "DataBackColor", m_DataBackColor, OnDataBackColorChanged, VT_COLOR)
	DISP_PROPERTY_NOTIFY(CAamymovCtrl, "StringColor", m_StringColor, OnStringColorChanged, VT_COLOR)
	DISP_PROPERTY_NOTIFY(CAamymovCtrl, "EditText", m_editText, OnEditTextChanged, VT_BSTR)
	DISP_PROPERTY_EX(CAamymovCtrl, "Title", GetTitle, SetTitle, VT_BSTR)
	DISP_FUNCTION(CAamymovCtrl, "Play", Play, VT_EMPTY, VTS_NONE)
	DISP_FUNCTION(CAamymovCtrl, "Open", Open, VT_EMPTY, VTS_NONE)
	DISP_FUNCTION(CAamymovCtrl, "Stop", Stop, VT_EMPTY, VTS_NONE)
	DISP_STOCKPROP_BACKCOLOR()
	DISP_STOCKPROP_FONT()
	//}}AFX_DISPATCH_MAP
	DISP_FUNCTION_ID(CAamymovCtrl, "AboutBox", DISPID_ABOUTBOX, AboutBox, VT_EMPTY, VTS_NONE)
END_DISPATCH_MAP()


/////////////////////////////////////////////////////////////////////////////
// Event map

BEGIN_EVENT_MAP(CAamymovCtrl, COleControl)
	//{{AFX_EVENT_MAP(CAamymovCtrl)
	EVENT_CUSTOM("OnStartPlay", FireOnStartPlay, VTS_NONE)
	EVENT_STOCK_DBLCLICK()
	//}}AFX_EVENT_MAP
END_EVENT_MAP()


/////////////////////////////////////////////////////////////////////////////
// Property pages

// TODO: Add more property pages as needed.  Remember to increase the count!
BEGIN_PROPPAGEIDS(CAamymovCtrl, 4)
	PROPPAGEID(CAamymovPropPage::guid)
	PROPPAGEID(CLSID_CColorPropPage)
	PROPPAGEID(CLSID_CFontPropPage)
	PROPPAGEID(CExtra::guid)
END_PROPPAGEIDS(CAamymovCtrl)


/////////////////////////////////////////////////////////////////////////////
// Initialize class factory and guid

IMPLEMENT_OLECREATE_EX(CAamymovCtrl, "AAMYMOV.AamymovCtrl.1",
	0xa44343e7, 0xbfb2, 0x11d0, 0x94, 0x2b, 0, 0x80, 0xc8, 0x38, 0xd5, 0xf9)


/////////////////////////////////////////////////////////////////////////////
// Type library ID and version

IMPLEMENT_OLETYPELIB(CAamymovCtrl, _tlid, _wVerMajor, _wVerMinor)


/////////////////////////////////////////////////////////////////////////////
// Interface IDs

const IID BASED_CODE IID_DAamymov =
		{ 0xa44343e5, 0xbfb2, 0x11d0, { 0x94, 0x2b, 0, 0x80, 0xc8, 0x38, 0xd5, 0xf9 } };
const IID BASED_CODE IID_DAamymovEvents =
		{ 0xa44343e6, 0xbfb2, 0x11d0, { 0x94, 0x2b, 0, 0x80, 0xc8, 0x38, 0xd5, 0xf9 } };


/////////////////////////////////////////////////////////////////////////////
// Control type information

static const DWORD BASED_CODE _dwAamymovOleMisc =
	OLEMISC_ACTIVATEWHENVISIBLE |
	OLEMISC_SETCLIENTSITEFIRST |
	OLEMISC_INSIDEOUT |
	OLEMISC_CANTLINKINSIDE |
	OLEMISC_RECOMPOSEONRESIZE;

IMPLEMENT_OLECTLTYPE(CAamymovCtrl, IDS_AAMYMOV, _dwAamymovOleMisc)


/////////////////////////////////////////////////////////////////////////////
// CAamymovCtrl::CAamymovCtrlFactory::UpdateRegistry -
// Adds or removes system registry entries for CAamymovCtrl

BOOL CAamymovCtrl::CAamymovCtrlFactory::UpdateRegistry(BOOL bRegister)
{
	// TODO: Verify that your control follows apartment-model threading rules.
	// Refer to MFC TechNote 64 for more information.
	// If your control does not conform to the apartment-model rules, then
	// you must modify the code below, changing the 6th parameter from
	// afxRegApartmentThreading to 0.

	if (bRegister)
		return AfxOleRegisterControlClass(
			AfxGetInstanceHandle(),
			m_clsid,
			m_lpszProgID,
			IDS_AAMYMOV,
			IDB_AAMYMOV,
			afxRegApartmentThreading,
			_dwAamymovOleMisc,
			_tlid,
			_wVerMajor,
			_wVerMinor);
	else
		return AfxOleUnregisterClass(m_clsid, m_lpszProgID);
}


/////////////////////////////////////////////////////////////////////////////
// CAamymovCtrl::CAamymovCtrl - Constructor

CAamymovCtrl::CAamymovCtrl()
{
	InitializeIIDs(&IID_DAamymov, &IID_DAamymovEvents);

	// TODO: Initialize your control's instance data here.
}


/////////////////////////////////////////////////////////////////////////////
// CAamymovCtrl::~CAamymovCtrl - Destructor

CAamymovCtrl::~CAamymovCtrl()
{
	// TODO: Cleanup your control's instance data here.
}


/////////////////////////////////////////////////////////////////////////////
// CAamymovCtrl::OnDraw - Drawing function

void CAamymovCtrl::OnDraw(
			CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
{
	// TODO: Replace the following code with your own drawing code.
//	pdc->FillRect(rcBounds, CBrush::FromHandle((HBRUSH)GetStockObject(WHITE_BRUSH)));
//	pdc->Ellipse(rcBounds);

	CBrush* pOldBrush, * pBrush;
	CFont*  pOldFont;

	pBrush = new CBrush;
	pBrush->CreateSolidBrush(TranslateColor(GetBackColor()));
	pOldBrush = pdc->SelectObject( pBrush );
	pdc->FillRect(rcBounds, pBrush); 
	pdc->SelectObject( pOldBrush );
	delete pBrush;

	pBrush = new CBrush;
	pBrush->CreateSolidBrush(m_DataBackColor);
	pOldBrush = pdc->SelectObject( pBrush );
	pdc->FillRect(&m_rectShowData, pBrush); 
	pOldFont = SelectStockFont( pdc );
	pdc->SetBkColor( m_DataBackColor );
	pdc->SetTextColor( m_StringColor );
	pdc->TextOut(3, m_rectShowData.top+3, m_cstrTitle );
	pdc->TextOut(m_rectShowData.left+3, m_rectShowData.top+3, m_editText );
	pdc->SelectObject( pOldFont );
	pdc->SelectObject( pOldBrush );
	delete pBrush;
}


/////////////////////////////////////////////////////////////////////////////
// CAamymovCtrl::DoPropExchange - Persistence support

void CAamymovCtrl::DoPropExchange(CPropExchange* pPX)
{
	ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
	COleControl::DoPropExchange(pPX);

	// TODO: Call PX_ functions for each persistent custom property.
	SetBackColor( RGB( 0,0,0 ) );
	PX_Color( pPX, _T("DataBackColor"), m_DataBackColor, RGB( 0,0,0 ) );
	PX_Color( pPX, _T("StringColor"), m_StringColor, RGB( 255,255,0 ) );
	PX_String( pPX, _T("Title"), m_cstrTitle, _T("Title Text") );
	PX_String( pPX, _T("EditText"), m_editText, _T("Edit Text") );
}


/////////////////////////////////////////////////////////////
////////////////
// CAamymovCtrl::OnResetState - Reset control to default state

void CAamymovCtrl::OnResetState()
{
	COleControl::OnResetState();  // Resets defaults found in DoPropExchange

	// TODO: Reset any other control state here.
//	SetBackColor( RGB( 0,0,0 ) );
//	m_DataBackColor = RGB( 0,0,0 );
//	m_cstrTitle = _T("Title Text");
}


/////////////////////////////////////////////////////////////////////////////
// CAamymovCtrl::AboutBox - Display an "About" box to the user

void CAamymovCtrl::AboutBox()
{
	CDialog dlgAbout(IDD_ABOUTBOX_AAMYMOV);
	dlgAbout.DoModal();
}

////////////////////////////////////// YU
// Add the following message map functions

/////////////////////////////////////////////////////////////////////////////
// CAamymovCtrl message handlers

int CAamymovCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (COleControl::OnCreate(lpCreateStruct) == -1)
		return -1;

////////////////////////////////////// YU >>
	// TODO: Add your specialized creation code here
     RECT rect = { 0, 0, 0, 0 };

	m_ctrlToolBar.Create(this, CBRS_TOP|CBRS_TOOLTIPS|CBRS_FLYBY|WS_VISIBLE);
//	m_ctrlToolBar.LoadBitmap(IDB_TOOLBAR);
//	m_ctrlToolBar.SetButtons(buttons, sizeof(buttons)/sizeof(UINT));
	m_ctrlToolBar.LoadToolBar(IDR_TOOLBAR);

	m_Menu.LoadMenu(IDR_MENU);
	m_ctrlMovie.Create( _T(" AVI Display Control"), WS_VISIBLE|WS_CHILD, rect, this, ID_MOVIE );
////////////////////////////////////// YU <<

	return 0;
}

/////////////////////////////////////////////////////////////////////////////
void CAamymovCtrl::OnSize(UINT nType, int cx, int cy)
{
	COleControl::OnSize(nType, cx, cy);

////////////////////////////////////// YU >>
	// TODO: Add your message handler code here
	CBitmap bmp;
	BITMAP  bmpinfo;

	bmp.LoadBitmap(IDB_TOOLBAR);
	bmp.GetBitmap(&bmpinfo);

	m_ctrlToolBar.MoveWindow( 0, 0, cx, bmpinfo.bmHeight+15, TRUE );

	m_rectShowData.left   = cx/2;
	m_rectShowData.top    = bmpinfo.bmHeight+15;
	m_rectShowData.right  = cx;
	m_rectShowData.bottom = m_rectShowData.top + 30;

	m_ctrlMovie.MoveWindow( 0, m_rectShowData.bottom, cx, cy-bmpinfo.bmHeight-15, TRUE );
	m_ctrlMovie.PositionMovie();
////////////////////////////////////// YU <<
}

/////////////////////////////////////////////////////////////////////////////
void CAamymovCtrl::OnAppAbout()
{
     AboutBox();
}

/////////////////////////////////////////////////////////////////////////////
// OK
void CAamymovCtrl::OnFileOpen()
{
	CFileDialog  fdlg( TRUE, "avi", "*.avi", OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
		              "avi", this );
	if ( fdlg.DoModal() == IDOK )
	{
		m_ctrlMovie.Open( fdlg.GetPathName() );
	}
}

/////////////////////////////////////////////////////////////////////////////
// OK
void CAamymovCtrl::OnFileClose()
{
	m_ctrlMovie.Close();
}

/////////////////////////////////////////////////////////////////////////////
// OK
void CAamymovCtrl::OnMoviePlay()
{
	FireOnStartPlay();
	m_ctrlMovie.Play();
}

/////////////////////////////////////////////////////////////////////////////
// OK
void CAamymovCtrl::OnMoviePause()
{
	m_ctrlMovie.Pause();
}

/////////////////////////////////////////////////////////////////////////////
// OK
void CAamymovCtrl::OnMovieStop()
{
	m_ctrlMovie.Stop();
}

/////////////////////////////////////////////////////////////////////////////
// OK
void CAamymovCtrl::OnContextMenu(CWnd* pWnd, CPoint point)
{
	// TODO: Add your message handler code here
	CMenu* pSubMenu;

	pSubMenu = m_Menu.GetSubMenu(0);
	pSubMenu->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON, point.x, point.y, this, NULL);
}

/////////////////////////////////////////////////////////////////////////////
// OK
BOOL CAamymovCtrl::OnProperties(LPMSG lpMsg, HWND hWndParent, LPCRECT lpRect)
{
	// TODO: Add your specialized code here and/or call the base class
	return COleControl::OnProperties(lpMsg, hWndParent, lpRect);
}

/////////////////////////////////////////////////////////////////////////////
// OK
void CAamymovCtrl::OnProperty()
{
	// TODO: Add your command handler code here
	RECT rect = { 0, 0, 0, 0 };
	OnProperties( NULL, m_hWnd, &rect );
}

/////////////////////////////////////////////////////////////////////////////
void CAamymovCtrl::RePaint()
{
	RECT rect; 
	CDC* pdc = GetDC();
  
	GetClientRect( &rect );
	OnDraw( pdc, rect, rect );
	ReleaseDC( pdc );
}

/////////////////////////////////////////////////////////////////////////////
void CAamymovCtrl::OnDataBackColorChanged()
{
	// TODO: Add notification handler code
	RePaint();
	SetModifiedFlag();
}

/////////////////////////////////////////////////////////////////////////////
void CAamymovCtrl::OnStringColorChanged() 
{
	// TODO: Add notification handler code
	RePaint();
	SetModifiedFlag();
}

/////////////////////////////////////////////////////////////////////////////
BSTR CAamymovCtrl::GetTitle() 
{
	return m_cstrTitle.AllocSysString();
}

/////////////////////////////////////////////////////////////////////////////
void CAamymovCtrl::SetTitle(LPCTSTR lpszNewValue) 
{
	// TODO: Add your property handler here
	m_cstrTitle = lpszNewValue;
	RePaint();
	SetModifiedFlag();
}

void CAamymovCtrl::Play() 
{
	// TODO: Add your dispatch handler code here
     OnMoviePlay();
}

void CAamymovCtrl::Open() 
{
	// TODO: Add your dispatch handler code here
	OnFileOpen();
}

void CAamymovCtrl::Stop() 
{
	// TODO: Add your dispatch handler code here
     OnMovieStop();
}

void CAamymovCtrl::OnEditTextChanged() 
{
	// TODO: Add notification handler code
	RePaint();
	SetModifiedFlag();
}
