void mxExpressionTray::DrawThumbNail( CExpClass *active, CExpression *current, CChoreoWidgetDrawHelper& helper, int rcx, int rcy, int rcw, int rch, int c, int selected, bool updateselection )
{
	if ( !current )
		return;

	HDC dc = helper.GrabDC();

	helper.DrawFilledRect( GetSysColor( COLOR_BTNFACE ), rcx, rcy, rcw + rcx, rch + rcy );

	if ( current->m_Bitmap[ models->GetActiveModelIndex() ].valid )
	{
		DrawBitmapToDC( dc, rcx, rcy, rcw, rch - m_nDescriptionHeight, current->m_Bitmap[ models->GetActiveModelIndex() ] );
		helper.DrawOutlinedRect( RGB( 127, 127, 127 ), PS_SOLID, 1, rcx, rcy, rcx + rcw, rcy + rch - m_nDescriptionHeight );
	}

	DrawDirtyFlag( helper, current, rcx, rcy, rcw, rch );

	DrawExpressionDescription( helper, rcx, rcy, rcw, rch, current->name, current->description );

	if ( c == selected )
	{
		DrawExpressionFocusRect( helper, rcx, rcy, rcw, rch - m_nDescriptionHeight, RGB( 255, 100, 63 ) );

		if ( updateselection )
		{
			m_nPrevCell = -1;
			m_nCurCell = c;
		}

		if ( current->CanUndo() || current->CanRedo() )
		{
			if ( current->CanUndo() )
			{
				DrawButton( helper, c, FindButton( "undo" ) );
			}

			if ( current->CanRedo() )
			{
				DrawButton( helper, c, FindButton( "redo" ) );
			}

			RECT rc;
			rc.left =  rcx + rcw - 2 * ( m_nButtonSquare + 4 );
			rc.top = rcy + m_nButtonSquare + 6;
			rc.right = rc.left + 2 * ( m_nButtonSquare + 4 );
			rc.bottom = rc.top + 15;

			helper.DrawColoredText( "Arial", 9, FW_NORMAL, RGB( 200, 200, 200 ), rc, 
				"%i/%i", current->UndoCurrent(), current->UndoLevels() );
		}

	}
	else
	{
		if ( current->GetSelected() )
		{
			DrawExpressionFocusRect( helper, rcx, rcy, rcw, rch - m_nDescriptionHeight, RGB( 127, 127, 220 ) );
		}
	}
}
void mxBitmapButton::redraw()
{
	HWND wnd = (HWND)getHandle();
	if ( !wnd )
		return;

	if ( !m_bmImage.valid )
		return;

	RECT rc;
	GetClientRect( wnd, &rc );
	
	HDC dc = GetDC( wnd );

	DrawBitmapToDC( dc, 0, 0, w(), h(), m_bmImage );

	ReleaseDC( wnd, dc );

	ValidateRect( wnd, &rc );
}
void mxExpressionTray::DrawButton( CChoreoWidgetDrawHelper& helper, int cell, mxETButton *btn )
{
	if ( !btn || !btn->m_pImage || !btn->m_pImage->valid )
		return;

	if ( !btn->m_bActive )
		return;

	int x, y, w, h;
	if ( !ComputeRect( cell, x, y, w, h ) )
		return;

	x += btn->m_rc.left;
	y += btn->m_rc.top;
	w = btn->m_rc.right - btn->m_rc.left;
	h = btn->m_rc.bottom - btn->m_rc.top;

	HDC dc = helper.GrabDC();

	DrawBitmapToDC( dc, x, y, w, h, *btn->m_pImage );
	helper.DrawOutlinedRect( RGB( 170, 170, 170 ), PS_SOLID, 1, x, y, x + w, y + h );
}
//-----------------------------------------------------------------------------
// Redraw to screen
//-----------------------------------------------------------------------------
void CChoreoGlobalEventWidget::redraw( CChoreoWidgetDrawHelper& drawHelper )
{
	if ( !getVisible() )
		return;

	CChoreoEvent *event = GetEvent();
	if ( !event )
		return;

	RECT rcTab;
	rcTab = getBounds();

	bool isLoop = false;
	COLORREF pointColor = COLOR_CHOREO_SEGMENTDIVIDER;
	COLORREF clr = COLOR_CHOREO_SEGMENTDIVIDER_BG;
	switch ( event->GetType() )
	{
	default:
		break;
	case CChoreoEvent::LOOP:
		{
			clr				= COLOR_CHOREO_LOOPPOINT_BG;
			pointColor		= COLOR_CHOREO_LOOPPOINT;
			isLoop			= true;
		}
		break;
	case CChoreoEvent::STOPPOINT:
		{
			clr				= COLOR_CHOREO_STOPPOINT_BG;
			pointColor		= COLOR_CHOREO_STOPPOINT;
		}
		break;
	}

	if ( IsSelected() )
	{
		InflateRect( &rcTab, 2, 2 );

		drawHelper.DrawTriangleMarker( rcTab, pointColor );

		InflateRect( &rcTab, -2, -2 );

		drawHelper.DrawTriangleMarker( rcTab, RGB( 240, 240, 220 ) );

	}
	else
	{
		drawHelper.DrawTriangleMarker( rcTab, pointColor );
	}

	RECT rcClient;
	drawHelper.GetClientRect( rcClient );

	RECT rcLine = rcTab;
	rcLine.top = rcTab.bottom + 2;
	rcLine.bottom = rcClient.bottom;
	rcLine.left = ( rcLine.left + rcLine.right ) / 2;
	rcLine.right = rcLine.left;

	if ( IsSelected() )
	{
		drawHelper.DrawColoredLine( clr, PS_DOT, 2, rcLine.left, rcLine.top, rcLine.right, rcLine.bottom );
	}
	else
	{
		drawHelper.DrawColoredLine( clr, PS_DOT, 1, rcLine.left, rcLine.top, rcLine.right, rcLine.bottom );
	}

	if ( event->GetType() == CChoreoEvent::STOPPOINT )
	{
		OffsetRect( &rcTab, -4, 15 );

		mxbitmapdata_t *image = CChoreoEventWidget::GetImage( event->GetType() );
		if ( image )
		{
			drawHelper.OffsetSubRect( rcTab );
			DrawBitmapToDC( drawHelper.GrabDC(), rcTab.left, rcTab.top, 16, 16, *image );	
		}
	}

	if ( !isLoop )
		return;

	COLORREF labelText = COLOR_INFO_TEXT;
	DrawLabel( drawHelper, labelText, rcLine.left, rcLine.top + 2, false );

	// Figure out loop spot
	float looptime = (float)atof( event->GetParameters() );

	// Find pixel for that
	bool clipped = false;
	int x = m_pView->GetPixelForTimeValue( looptime, &clipped );
	if ( clipped )
		return;

	rcLine.left = x;
	rcLine.right = x;

	clr = COLOR_CHOREO_LOOPPOINT_START_BG;
	drawHelper.DrawColoredLine( clr, PS_SOLID, 1, rcLine.left, rcLine.top, rcLine.right, rcLine.top + 28);

	DrawLabel( drawHelper, labelText, rcLine.left, rcLine.top + 2, true );
}
//-----------------------------------------------------------------------------
// Purpose: FIXME:  This should either be embedded or we should draw the caption
//  here
//-----------------------------------------------------------------------------
void CChoreoEventWidget::redraw( CChoreoWidgetDrawHelper& drawHelper )
{
	if ( !getVisible() )
		return;

	CChoreoEvent *event = GetEvent();
	if ( !event )
		return;

	int deflateborder = 1;
	int fontsize = 9;

	HDC dc = drawHelper.GrabDC();
	RECT rcClient = getBounds();

	RECT rcDC;
	drawHelper.GetClientRect( rcDC );

	RECT dummy;
	if ( !IntersectRect( &dummy, &rcDC, &rcClient ) )
		return;

	bool ramponly = m_pView->IsRampOnly();

	if ( IsSelected() && !ramponly )
	{
		InflateRect( &rcClient, 3, 1 );
		//rcClient.bottom -= 1;
		rcClient.right += 1;

		RECT rcFrame = rcClient;
		RECT rcBorder = rcClient;

		rcFrame.bottom = rcFrame.top + 17;
		rcBorder.bottom = rcFrame.top + 17;

		COLORREF clrSelection = RGB( 0, 63, 63 );
		COLORREF clrBorder = RGB( 100, 200, 255 );

		HBRUSH brBorder = CreateSolidBrush( clrBorder );
		HBRUSH brSelected = CreateHatchBrush( HS_FDIAGONAL, clrSelection );
		for ( int i = 0; i < 2; i++ )
		{
			FrameRect( dc, &rcFrame, brSelected );
			InflateRect( &rcFrame, -1, -1 );
		}
		FrameRect( dc, &rcBorder, brBorder );
		FrameRect( dc, &rcFrame, brBorder );

		DeleteObject( brSelected );
		DeleteObject( brBorder );
		rcClient.right -= 1;
		//rcClient.bottom += 1;
		InflateRect( &rcClient, -3, -1 );
	}	

	RECT rcEvent;
	rcEvent = rcClient;

	InflateRect( &rcEvent, 0, -deflateborder );

	rcEvent.bottom = rcEvent.top + 10;

	if ( event->GetType() == CChoreoEvent::SPEAK && m_pWaveFile && !event->HasEndTime() )
	{
		event->SetEndTime( event->GetStartTime() + m_pWaveFile->GetRunningLength() );
		rcEvent.right = ( int )( m_pWaveFile->GetRunningLength() * m_pView->GetPixelsPerSecond() );  
	}

	if ( event->HasEndTime() )
	{
		rcEvent.right = rcEvent.left + m_nDurationRightEdge;

		RECT rcEventLine = rcEvent;
		OffsetRect( &rcEventLine, 0, 1 );

		if ( event->GetType() == CChoreoEvent::SPEAK )
		{
			if ( m_pWaveFile )
			{
				HBRUSH brEvent = CreateSolidBrush( COLOR_CHOREO_EVENT );
				HBRUSH brBackground = CreateSolidBrush( COLOR_CHOREO_DARKBACKGROUND );

				if ( !ramponly )
				{
					FillRect( dc, &rcEventLine, brBackground );
				}

				// Only draw wav form here if selected
				if ( IsSelected() )
				{
					sound->RenderWavToDC( dc, rcEventLine, IsSelected() ? COLOR_CHOREO_EVENT_SELECTED : COLOR_CHOREO_EVENT, 0.0, m_pWaveFile->GetRunningLength(), m_pWaveFile );
				}

				//FrameRect( dc, &rcEventLine, brEvent );
				drawHelper.DrawColoredLine( COLOR_CHOREO_EVENT, PS_SOLID, 3,
					rcEventLine.left, rcEventLine.top, rcEventLine.left, rcEventLine.bottom );
				drawHelper.DrawColoredLine( COLOR_CHOREO_EVENT, PS_SOLID, 3,
					rcEventLine.right, rcEventLine.top, rcEventLine.right, rcEventLine.bottom );
				
				DeleteObject( brBackground );
				DeleteObject( brEvent );

				//rcEventLine.top -= 3;
				DrawRelativeTags( drawHelper, rcEventLine, m_pWaveFile->GetRunningLength(), event );
			}
		}
		else
		{
			COLORREF clrEvent = IsSelected() ? COLOR_CHOREO_EVENT_SELECTED : COLOR_CHOREO_EVENT;
			if ( event->GetType() == CChoreoEvent::SUBSCENE )
			{
				clrEvent = RGB( 200, 180, 200 );
			}

			HBRUSH brEvent = CreateSolidBrush( clrEvent );
		
			if ( !ramponly )
			{
				FillRect( dc, &rcEventLine, brEvent );
			}
		
			DeleteObject( brEvent );

			if ( ramponly && IsSelected() )
			{
				drawHelper.DrawOutlinedRect( RGB( 150, 180, 250 ), PS_SOLID, 1,
					rcEventLine );
			}
			else
			{
				drawHelper.DrawColoredLine( RGB( 127, 127, 127 ), PS_SOLID, 1, rcEventLine.left, rcEventLine.bottom,
					rcEventLine.left, rcEventLine.top );
				drawHelper.DrawColoredLine( RGB( 127, 127, 127 ), PS_SOLID, 1, rcEventLine.left, rcEventLine.top,
					rcEventLine.right, rcEventLine.top );
				drawHelper.DrawColoredLine( RGB( 31, 31, 31 ), PS_SOLID, 1, rcEventLine.right, rcEventLine.top,
					rcEventLine.right, rcEventLine.bottom );
				drawHelper.DrawColoredLine( RGB( 0, 0, 0 ), PS_SOLID, 1, rcEventLine.right, rcEventLine.bottom,
					rcEventLine.left, rcEventLine.bottom );
			}

			g_pRampTool->DrawSamplesSimple( drawHelper, event, false, RGB( 63, 63, 63 ), rcEventLine );

			DrawRelativeTags( drawHelper, rcEventLine, event->GetDuration(), event );
			DrawAbsoluteTags( drawHelper, rcEventLine, event->GetDuration(), event );

		}
	}
	else
	{
		RECT rcEventLine = rcEvent;
		OffsetRect( &rcEventLine, 0, 1 );

		drawHelper.DrawColoredLine( COLOR_CHOREO_EVENT, PS_SOLID, 3,
			rcEventLine.left - 1, rcEventLine.top, rcEventLine.left - 1, rcEventLine.bottom );
	}

	if ( event->IsUsingRelativeTag() )
	{
		RECT rcTagName;
		rcTagName = rcClient;

		int length = drawHelper.CalcTextWidth( "Arial", 9, FW_NORMAL, event->GetRelativeTagName() );

		rcTagName.right = rcTagName.left;
		rcTagName.left = rcTagName.right - length - 4;
		rcTagName.top += 3;
		rcTagName.bottom = rcTagName.top + 10;
		
		drawHelper.DrawColoredText( "Arial", 9, FW_NORMAL, RGB( 0, 100, 200 ), rcTagName, event->GetRelativeTagName() );

		drawHelper.DrawFilledRect( RGB( 0, 100, 250 ), rcTagName.right-1, rcTagName.top-2,
			rcTagName.right+2, rcTagName.bottom + 2 );

	}

	// Now draw the label
	RECT rcEventLabel;
	rcEventLabel = rcClient;

	InflateRect( &rcEventLabel, 0, -deflateborder );

	rcEventLabel.top += 15; // rcEventLabel.bottom - 2 * ( fontsize + 2 ) - 1;
	rcEventLabel.bottom = rcEventLabel.top + fontsize + 2;
	rcEventLabel.left += 1;

	//rcEventLabel.left -= 8;

	int leftAdd = 16;

	if ( CChoreoEventWidget::GetImage( event->GetType() ) )
	{
		mxbitmapdata_t *image = CChoreoEventWidget::GetImage( event->GetType() );
		if ( image )
		{
			DrawBitmapToDC( dc, rcEventLabel.left, rcEventLabel.top, leftAdd, leftAdd,
				*image );	
		}
	}

	if ( event->IsResumeCondition() )
	{
		RECT rc = rcEventLabel;
		OffsetRect( &rc, leftAdd, 0 );
		rc.right = rc.left + leftAdd;

		DrawBitmapToDC( dc, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
			*CChoreoEventWidget::GetPauseImage() );
	}

	//rcEventLabel.left += 8;

	OffsetRect( &rcEventLabel, 18, 1 );
	
	int len = drawHelper.CalcTextWidth( "Arial", fontsize, FW_NORMAL, event->GetName() );

	rcEventLabel.right = rcEventLabel.left + len + 2;
	drawHelper.DrawColoredText( "Arial", fontsize, FW_NORMAL, RGB( 0, 0, 120 ), 
		rcEventLabel, event->GetName() );
}
//-----------------------------------------------------------------------------
// Purpose: 
// Input  : drawHelper - 
//			rcBounds - 
//-----------------------------------------------------------------------------
void CChoreoEventWidget::redrawStatus( CChoreoWidgetDrawHelper& drawHelper, RECT& rcClient )
{
	if ( !getVisible() )
		return;

	CChoreoEvent *event = GetEvent();
	if ( !event )
		return;

	int deflateborder = 1;
	int fontsize = 9;

	HDC dc = drawHelper.GrabDC();

	// Now draw the label
	RECT rcEventLabel;
	rcEventLabel = rcClient;

	InflateRect( &rcEventLabel, 0, -deflateborder );

	// rcEventLabel.top += 2;
	rcEventLabel.left += 2;
	//rcEventLabel.top = rcEventLabel.bottom - 2 * ( fontsize + 2 ) - 1;
	//rcEventLabel.bottom = rcEventLabel.top + fontsize + 2;

	int leftAdd = 16;

	if ( CChoreoEventWidget::GetImage( event->GetType() ) )
	{
		mxbitmapdata_t *image = CChoreoEventWidget::GetImage( event->GetType() );
		if ( image )
		{
			RECT rcFixed = rcEventLabel;
			drawHelper.OffsetSubRect( rcFixed );
			DrawBitmapToDC( dc, rcFixed.left, rcFixed.top, leftAdd, leftAdd,
				*image );	
		}
	}

	if ( event->IsResumeCondition() )
	{
		RECT rc = rcEventLabel;
		OffsetRect( &rc, 16, 0 );
		rc.right = rc.left + 16;

		RECT rcFixed = rc;
		drawHelper.OffsetSubRect( rcFixed );
		DrawBitmapToDC( dc, rcFixed.left, rcFixed.top, 
			rcFixed.right - rcFixed.left, rcFixed.bottom - rcFixed.top,
			*CChoreoEventWidget::GetPauseImage() );
	}

	// Draw Type Name:
	//rcEventLabel.top -= 4;

	rcEventLabel.left = rcClient.left + 32;
	rcEventLabel.bottom = rcEventLabel.top + fontsize + 2;
	// OffsetRect( &rcEventLabel, 0, 2 );

	int len = drawHelper.CalcTextWidth( "Arial", fontsize, FW_NORMAL, "%s event \"%s\"", event->NameForType( event->GetType() ), event->GetName() );
	drawHelper.DrawColoredText( "Arial", fontsize, FW_NORMAL, COLOR_INFO_TEXT, rcEventLabel, "%s event \"%s\"", event->NameForType( event->GetType() ), event->GetName() );

	OffsetRect( &rcEventLabel, 0, fontsize + 2 );

	drawHelper.DrawColoredText( "Arial", fontsize, FW_NORMAL, COLOR_INFO_TEXT, rcEventLabel, "parameters \"%s\"", GetLabelText() );

}