//====START_GENERATED_PROLOG======================================
//
//
//   COMPONENT_NAME: odother
//
//   CLASSES: none
//
//   ORIGINS: 27
//
//
//   (C) COPYRIGHT International Business Machines Corp. 1995,1996
//   All Rights Reserved
//   Licensed Materials - Property of IBM
//   US Government Users Restricted Rights - Use, duplication or
//   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
//
//   IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
//   ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
//   PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR
//   CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
//   USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
//   OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
//   OR PERFORMANCE OF THIS SOFTWARE.
//
//====END_GENERATED_PROLOG========================================
//
// @(#) 1.4 nt/src/samples/cdplayer/cddisp.cpp, odcdplayerpart, od96nt, odnt9712d 11/5/96 17:10:01 [ 3/21/97 17:32:44 ]

#ifndef IBMSample_CDDisplay
#include "CDDisp.h"
#endif

#ifndef SOM_ODFacet_xh
#include <Facet.xh>
#endif

CDDisplay gDisplay;


/*-------------------------------------------------------------------+
|                      CDDisplay::CDDisplay ()                       |
| ================================================================== |
|                                                                    |
| Function:    CDDisplay default constructor                         |
|                                                                    |
+-------------------------------------------------------------------*/
CDDisplay::CDDisplay ()
{
	;
}


/*-------------------------------------------------------------------+
|                     CDDisplay::~CDDisplay ()                       |
| ================================================================== |
|                                                                    |
| Function:    CDDisplay destructor...                               |
|                                                                    |
+-------------------------------------------------------------------*/
CDDisplay::~CDDisplay ()
{
	delete fCDControl;
}


/*-------------------------------------------------------------------+
|                     CDDisplay::Initialize ()                       |
| ================================================================== |
|                                                                    |
| Function:    Initilize the CDPlayer display...                     |
|                                                                    |
| Parameters:  part - Pointer to the CDPlayer SOM object             |
|                                                                    |
+-------------------------------------------------------------------*/
void
CDDisplay::Initialize (CDPlayer* part)
{
	/*
	 *  Get the parent window from the CDPlayer part...
	 */
	Environment *ev = somGetGlobalEnvironment ();

	fPart = part;
	ODFacet* facet = fPart->_get_fFacet(ev);
	HWND parentWindow = facet->GetFacetHWND (ev);

	char szAppName[] = "CDPlayer";
	fInstance = (HINSTANCE) LoadLibrary ("iodcdply.dll");

	/*
	*  Initialize common controls...
	*/
	InitCommonControls ();

	WNDCLASSEX  wndclass;

	wndclass.cbSize        = sizeof (wndclass);
	wndclass.style         = CS_HREDRAW | CS_VREDRAW;
	wndclass.lpfnWndProc   = (WNDPROC) ProcessWindowsMessages;
	wndclass.cbClsExtra    = 0;
	wndclass.cbWndExtra    = DLGWINDOWEXTRA;
	wndclass.hInstance     = fInstance;
	wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION);
	wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW);
	wndclass.hbrBackground = (HBRUSH) (COLOR_WINDOW);
	wndclass.lpszMenuName  = NULL;
	wndclass.lpszClassName = szAppName;
	wndclass.hIconSm       = LoadIcon (NULL, IDI_APPLICATION);

	RegisterClassEx (&wndclass);

	fRootWindow = CreateDialog (fInstance, szAppName, 0, NULL);
	SetParent (fRootWindow, parentWindow);

	ShowWindow (fRootWindow, SW_SHOWNORMAL);
	UpdateWindow (fRootWindow);

	ULONG style = GetWindowLong (fRootWindow, GWL_STYLE);
	style &= ~(WS_THICKFRAME | WS_CAPTION | WS_SYSMENU | WS_OVERLAPPED | 
		   WS_MINIMIZEBOX | WS_MAXIMIZEBOX);
	SetWindowLong (fRootWindow, GWL_STYLE, style);
}


/*-------------------------------------------------------------------+
|                   CDDisplay::ProcessMessages ()                    |
| ================================================================== |
|                                                                    |
| Function:    Process windows messages...                           |
|                                                                    |
+-------------------------------------------------------------------*/
LRESULT CDDisplay::ProcessMessages (HWND hwnd, 
	UINT iMsg, WPARAM wParam, LPARAM lParam)
{
	HDC         hdc ;
	PAINTSTRUCT ps ;
	RECT        rect ;

	switch (iMsg) {
		case WM_MOUSEACTIVATE :
		{
			/*
			 *  Activate the window...
			 */
			Environment *ev = somGetGlobalEnvironment ();
			ODFacet* facet = fPart->_get_fFacet(ev);
			fPart->Activate (ev, facet);
		}
		return 0;

		case MM_MCINOTIFY :
		case WM_TIMER :
		{
			fCDControl->cdPoll ();
			/*
			 *  Update the status...
			 */
			SetDlgItemText (hwnd, ID_STATUS, fCDControl->Status ());
			SendMessage (fStatusBar, SB_SETTEXT, 0, 
				    (LPARAM) fCDControl->Status ());

			/*
			 *  Update the playing time...
			 */
			char trackStr [30];

			sprintf (trackStr, "[%d] %d:%02d",
				fCDControl->GetTrack (),
				fCDControl->GetMinute (),
				fCDControl->GetSecond ());

			SetDlgItemText (hwnd, ID_TRACK, trackStr);
			SendMessage (fStatusBar, SB_SETTEXT, 1, 
				    (LPARAM) trackStr);

			/*
			 *  Set total number of tracks...
			 */
			sprintf (trackStr, "%d", fCDControl->GetNumTracks ());
			SetDlgItemText (hwnd, ID_NUM_TRACKS, trackStr);

			/*
			 *  Update the progress bar...
			 */
			SendMessage (fProgressBar, PBM_SETPOS, 
				(LPARAM) fCDControl->GetSecond (), 0L);

			/*
			 *  Update the Album, Artist and song title...
			 */
			SetDlgItemText (hwnd, ID_ARTIST, fCDControl->GetArtist ());
			SetDlgItemText (hwnd, ID_ALBUM, fCDControl->GetAlbum ());
			SetDlgItemText (hwnd, ID_TITLE, fCDControl->GetSong ());
		}
		return 0;

		case WM_CREATE :

			SetColorsAndFonts (hwnd, hdc);

			/* 
			 *  Add a status bar...
			 */
			CreateStatusBar (hwnd);
			
			rect.left  = 0; rect.top = 0;
			rect.right = 0; rect.bottom = 0;

			fProgressBar = CreateProgressBar (hwnd, rect,
					    ID_PROGRESS, MAKELONG(0, 100), 1);

			/*
			 *  Create the CD controller...
			 */
			fCDControl = new CDControl (hwnd);

			/*
			 *  If there is no CDROM drive attached, disable
			 *  all of the controls...
			 */
			if (fCDControl->GetState () == CDControl::kNoDrive)
				DisableControls ();

			/*
			 *  Update the status...
			 */
			SetDlgItemText (hwnd, ID_STATUS, fCDControl->Status ());
			SendMessage (fStatusBar, SB_SETTEXT, 0, 
				    (LPARAM) fCDControl->Status ());

			return 0 ;

		case WM_SIZE : 
			{
			UpdateWindow (fRootWindow);

			/*
			 *  Resize the status bar...
			 */
			ResizeStatusBar (0, HIWORD(lParam) - 10, 
					 LOWORD(lParam), HIWORD(lParam));

			/*
			 *  Resize the progress bar...
			 */
			SendMessage (fStatusBar, SB_GETRECT, 2, 
				    (LPARAM) (LPRECT) &rect);

			rect.top = HIWORD(lParam) - 15;

			ResizeProgressBar (rect.left, rect.top, 
				    rect.right-rect.left, 10);
			}
			return 0;

		case WM_PAINT :
			{
				hdc = BeginPaint (hwnd, &ps) ;

				/*
				 *  All the nice stuff...
				 */
				SetColorsAndFonts (hwnd, hdc);

				EndPaint (hwnd, &ps) ;
			}
			return 0 ;
		
		case WM_COMMAND:
			if (ProcessCommands (hwnd, iMsg, wParam, lParam) == 0)
				return 1;
			else 
				return 0;
			break;
	}

	return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
}


/*-------------------------------------------------------------------+
|                   CDDisplay::ProcessCommands ()                    |
| ================================================================== |
|                                                                    |
| Function:    Process command messages...                           |
|                                                                    |
+-------------------------------------------------------------------*/
LRESULT CDDisplay::ProcessCommands (HWND hwnd, 
UINT iMsg, WPARAM wParam, LPARAM lParam)
{
	switch (LOWORD (wParam)) {

		case IDM_DEVICE_EJECT:
			fCDControl->cdEject ();
			return 0;

		case IDM_DEVICE_PLAY:
			fCDControl->cdPlay ();
			return 0;

		case IDM_DEVICE_PAUSE:
			fCDControl->cdPause ();
			return 0;

		case IDM_DEVICE_STOP:
			fCDControl->cdStop ();
			return 0;

		case ID_TRACK_1:
			fCDControl->SelectTrack (1);
			return 0;

		case ID_TRACK_2:
			fCDControl->SelectTrack (2);
			return 0;

		case ID_TRACK_3:
			fCDControl->SelectTrack (3);
			return 0;

		case ID_TRACK_4:
			fCDControl->SelectTrack (4);
			return 0;

		case ID_TRACK_5:
			fCDControl->SelectTrack (5);
			return 0;

		case ID_TRACK_6:
			fCDControl->SelectTrack (6);
			return 0;

		case ID_TRACK_7:
			fCDControl->SelectTrack (7);
			return 0;

		case ID_TRACK_8:
			fCDControl->SelectTrack (8);
			return 0;

		case ID_TRACK_9:
			fCDControl->SelectTrack (9);
			return 0;

		case ID_TRACK_10:
			fCDControl->SelectTrack (10);
			return 0;

		case ID_TRACK_NEXT:
			fCDControl->ChangeTrack (+1);
			return 0;

		case ID_TRACK_PREV:
			fCDControl->ChangeTrack (-1);
			return 0;

		case IDM_EXIT:
			SendMessage (hwnd, WM_CLOSE, 0, 0L);
			return 0;
		break;
	}
	return 1;
}


/*-------------------------------------------------------------------+
|                   CDDisplay::CreateStatusBar ()                    |
| ================================================================== |
|                                                                    |
| Function:    Create a status bar...                                |
|                                                                    |
+-------------------------------------------------------------------*/
void CDDisplay::CreateStatusBar (HWND parentWindow)
{
	/* 
	*  Add a status bar...
	*/
	fStatusBar = CreateWindowEx (
			    0L,		 		// extended style
			    STATUSCLASSNAME,		// create status bar
			    "Status Bar",  		// window caption
			    WS_CHILD | WS_BORDER |  	// window style
			    WS_VISIBLE | SBS_SIZEGRIP,
			    0,0,0,0,
			    parentWindow,              	// parent window handle
			    (HMENU) ID_STATUSBAR,	// ID
			    fInstance,			// program instance
			    NULL);	             	// creation parameters

	SendMessage (fStatusBar, SB_SETPARTS, 3, (LPARAM) fStatusBarPanels);

	SendMessage (fStatusBar, SB_SETTEXT, 0, (LPARAM) "");
	SendMessage (fStatusBar, SB_SETTEXT, 1, (LPARAM) "");
	SendMessage (fStatusBar, SB_SETTEXT, 2, (LPARAM) "");

}


/*-------------------------------------------------------------------+
|                   CDDisplay::ResizeStatusBar ()                    |
| ================================================================== |
|                                                                    |
| Function:    Resize the status bar...                              |
|                                                                    |
+-------------------------------------------------------------------*/
void CDDisplay::ResizeStatusBar (int x, int y, int nWidth, int nHeight)
{
	MoveWindow (fStatusBar, x, y, nWidth, nHeight, TRUE);

	fStatusBarPanels [0] = nWidth / 4;
	fStatusBarPanels [1] = nWidth / 2;
	fStatusBarPanels [2] = -1;

	SendMessage (fStatusBar, SB_SETPARTS, 3, 
		    (LPARAM) fStatusBarPanels);
}


/*-------------------------------------------------------------------+
|                  CDDisplay::CreateProgressBar ()                   |
| ================================================================== |
|                                                                    |
| Function:    Create a progress bar...                              |
|                                                                    |
+-------------------------------------------------------------------*/
HWND CDDisplay::CreateProgressBar (HWND hWndParent, RECT rect, WORD wID, 
LONG lRange, LONG lStep)
{
	HWND bar = CreateWindowEx (
			    0L,				// extended style
			    PROGRESS_CLASS,		// create status bar
			    "Progress Bar",		// window caption
			    WS_CHILD | WS_VISIBLE,   	// window style
			    rect.left, rect.top, rect.right, rect.bottom,
			    hWndParent,                 // parent window handle
			    (HMENU) wID,	 	// ID
			    fInstance,                  // program instance handle
			    NULL) ;		        // creation parameters

	// Set the range for the status bar...
	SendMessage (bar, PBM_SETRANGE, 0L, lRange);

	// Set the step
	SendMessage (bar, PBM_SETSTEP, lStep, 0L);

	return bar;
}


/*-------------------------------------------------------------------+
|                  CDDisplay::ResizeProgressBar ()                   |
| ================================================================== |
|                                                                    |
| Function:    Resize the progress bar...                            |
|                                                                    |
+-------------------------------------------------------------------*/
void CDDisplay::ResizeProgressBar (int x, int y, int nWidth, int nHeight)
{
	MoveWindow (fProgressBar, x, y, nWidth, nHeight, TRUE);
}


/*-------------------------------------------------------------------+
|                  CDDisplay::SetColorsAndFonts ()                   |
| ================================================================== |
|                                                                    |
| Function:    Set colors and fonts...                               |
|                                                                    |
+-------------------------------------------------------------------*/
void CDDisplay::SetColorsAndFonts (HWND hWnd, HDC hdc)
{
	HFONT hFont;
	
	/*
	 *  Set the "Artist" font style...
	 */
	hFont = MakeFont (hWnd, hdc, 11, FW_SEMIBOLD, "Arial");
	SendDlgItemMessage (hWnd, ID_ARTIST, WM_SETFONT, 
				(WPARAM)hFont, 0L);
	/*
	 *  Set the "Album" font style...
	 */
	hFont = MakeFont (hWnd, hdc, 10, FW_SEMIBOLD, "Arial");
	SendDlgItemMessage (hWnd, ID_ALBUM, WM_SETFONT, 
				(WPARAM)hFont, 0L);
	/*
	 *  Set the "Title" font style...
	 */
	hFont = MakeFont (hWnd, hdc, 10, FW_MEDIUM, "Arial Narrow", kItalic);
	SendDlgItemMessage (hWnd, ID_TITLE, WM_SETFONT, 
				(WPARAM)hFont, 0L);

	hFont = MakeFont (hWnd, hdc, 10, FW_SEMIBOLD, "Arial");
	SendDlgItemMessage (hWnd, ID_STATUS, WM_SETFONT, 
				(WPARAM)hFont, 0L);

	hFont = MakeFont (hWnd, hdc, 10, FW_SEMIBOLD, "Arial");
	SendDlgItemMessage (hWnd, ID_TRACK, WM_SETFONT, 
				(WPARAM)hFont, 0L);
}


/*-------------------------------------------------------------------+
|                      CDDisplay::MakeFont ()                        |
| ================================================================== |
|                                                                    |
| Function:    Create a font...                                      |
|                                                                    |
+-------------------------------------------------------------------*/
HFONT CDDisplay::MakeFont (HWND hWnd, HDC hdc, int pointSize, int weight, 
	LPCSTR typeface, EFontStyle style)
{
	int nHeight;

	nHeight = -MulDiv (GetDeviceCaps (hdc, LOGPIXELSY), pointSize, 72);

	HFONT hFont = CreateFont (
			nHeight, 		// height of the font
			0, 			// average width
			0, 			// escapement
			0,			// orientation
			weight, 		// weight
			(style & kItalic),	// italic
			(style & kUnderLine), 	// underline
			(style & kStrikeOut), 	// strikeout
			0, 			// char set
			0, 			// output precision
			0, 			// clip precision
			0, 			// quality
			0, 			// pitch and family
			typeface		// type face
	);

	return hFont;
}


/*-------------------------------------------------------------------+
|                   CDDisplay::DisableControls ()                    |
| ================================================================== |
|                                                                    |
| Function:    Disable all of the controls...                        |
|                                                                    |
+-------------------------------------------------------------------*/
void CDDisplay::DisableControls ()
{
	DisableButton (IDM_DEVICE_PLAY);
	DisableButton (IDM_DEVICE_PAUSE);
	DisableButton (IDM_DEVICE_STOP);
	DisableButton (ID_TRACK_PREV);
	DisableButton (ID_TRACK_NEXT);
	DisableButton (ID_TRACK_1);
	DisableButton (ID_TRACK_2);
	DisableButton (ID_TRACK_3);
	DisableButton (ID_TRACK_4);
	DisableButton (ID_TRACK_5);
	DisableButton (ID_TRACK_6);
	DisableButton (ID_TRACK_7);
	DisableButton (ID_TRACK_8);
	DisableButton (ID_TRACK_9);
	DisableButton (ID_TRACK_10);

	InvalidateRect (fRootWindow, NULL, TRUE);
}


/*-------------------------------------------------------------------+
|                    CDDisplay::DisableButton ()                     |
| ================================================================== |
|                                                                    |
| Function:    Disable the specified button...                       |
|                                                                    |
+-------------------------------------------------------------------*/
void CDDisplay::DisableButton (int id)
{
	HWND hwnd = GetDlgItem (fRootWindow, id);

	ULONG style = GetWindowLong (hwnd, GWL_STYLE);
	style |= WS_DISABLED;
	SetWindowLong (hwnd, GWL_STYLE, style);
}


/*-------------------------------------------------------------------+
|                     ProcessWindowsMessages ()                      |
| ================================================================== |
|                                                                    |
| Function:    Pass messages to an instance of the display class...  |
|                                                                    |
+-------------------------------------------------------------------*/
LRESULT CALLBACK ProcessWindowsMessages (HWND hWnd, UINT iMsg, 
				WPARAM wParam, LPARAM lParam)
{
	return gDisplay.ProcessMessages (hWnd, iMsg, wParam, lParam);
}
