//====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.7 nt/src/samples/cdplayer/iodcdply.cpp, odcdplayerpart, od96nt, odnt9712d 11/5/96 16:13:02 [ 3/21/97 17:36:05 ]

#ifndef SOM_Module_CDPlayer_Source
#define SOM_Module_CDPlayer_Source
#endif

#ifndef _ALTPOINT_
#include <AltPoint.h>                   // Use C++ savvy ODPoint and ODRect
#endif

#define CDPlayer_Class_Source
#define VARIABLE_MACROS

#ifndef _ODEXCEPT_
#include "ODExcept.h"
#endif

#ifndef SOM_CDPlayer_xih
#include "iodcdply.xih"
#endif

#ifndef SOM_ODFrameFacetIterator_xh
#include <FrFaItr.xh>
#endif

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

#ifndef SOM_ODFrame_xh
#include <Frame.xh>
#endif

#ifndef SOM_ODShape_xh
#include <Shape.xh>
#endif

#ifndef SOM_ODTransform_xh
#include <Trnsform.xh>
#endif

#ifndef _TEMPOBJ_
#include <TempObj.h>
#endif

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

#include <string.h>

/*
 *  Meta Class constants...
 */
const ODType   kCDPlayerKind            = "CDPlayer";
const ODType   kCDPlayerKindDisplayName = "CD Player";
const ODType   kCDPlayerHandlerName     = "CDPlayer";
const ODType   kCDPlayerCategory        = "OpenDoc:Category:Sample";


/*-------------------------------------------------------------------+
|                              Log ()                                |
| ================================================================== |
|                                                                    |
| Function:    This function will output a message to the utility    |
|              mgsdisp.exe for MethodDebug call.  This allows the    |
|              developer to see the OpenDoc method calls being made  |
|              to this part in real-time.                            |
|                                                                    |
| Note(s):     This function only exists if DEBUG is defined.        |
|                                                                    |
| Parameters:  char *c;    description of class                      |
|              char *m;    description of method                     |
|                                                                    |
| Returns:     none                                                  |
|                                                                    |
+-------------------------------------------------------------------*/
#ifdef DEBUG
       #undef CDPlayerMethodDebug
       #undef M_CDPlayerMethodDebug

       #define CDPlayerMethodDebug(c,m) Log(c,m)
       #define M_CDPlayerMethodDebug(c,m) Log(c,m)

       #include <qprintf.h>  // output to MSGDISP.EXE

       static void Log(char* className, char* methodName)
       {
	      qprintf ("%s:%s\n", className, methodName);
       }
#endif


/*-------------------------------------------------------------------+
|                       CDPlayer::InitPart ()                        |
| ================================================================== |
|                                                                    |
| Function:    Initialize the CDPlayer.                              |
|                                                                    |
| Algorithm:   o  Initialize all instance data                       |
|                                                                    |
| Parameters:  storageUnit - empty storage unit                      |
|              partWrapper - part wrapper                            |
|                                                                    |
+-------------------------------------------------------------------*/
SOM_Scope void SOMLINK CDPlayerInitPart(
	CDPlayer*	somSelf,
	Environment*	ev,
	ODStorageUnit*	storageUnit,
	ODPart*		partWrapper)
{
	CDPlayerData*	somThis = CDPlayerGetData(somSelf);
	CDPlayerMethodDebug("CDPlayer","InitPart");

	/*
	 *  Initialize our parent part...
	 */
	CDPlayer_parent_SimplePart_InitPart (somSelf, ev, storageUnit, 
					    partWrapper);
	/*
	 *  Initialize all instance data...
	 */
	_fFacet       = kODNULL;
}


/*-------------------------------------------------------------------+
|                 CDPlayer::InitPartFromStorage ()                   |
| ================================================================== |
|                                                                    |
| Function:    Initialize the CDPlayer from storage.                 |
|                                                                    |
| Algorithm:   o  Initialize parent part (SimplePart)                |
|              o  Initialize instance data and persistent values     |
|                 from the storage unit.                             |
|                                                                    |
| Parameters:  storageUnit - storage unit containing persistent data |
|              partWrapper - part wrapper                            |
|                                                                    |
+-------------------------------------------------------------------*/
SOM_Scope void SOMLINK CDPlayerInitPartFromStorage(
	CDPlayer*	somSelf,
	Environment*	ev,
	ODStorageUnit*	storageUnit,
	ODPart*		partWrapper)
{
	CDPlayerData*	somThis = CDPlayerGetData(somSelf);
	CDPlayerMethodDebug("CDPlayer","InitPartFromStorage");

	/*
	 *  Initialize our parent part...
	 */
	CDPlayer_parent_SimplePart_InitPartFromStorage (somSelf, ev, 
						storageUnit, partWrapper);
	/*
	 *  Initialize all instance data...
	 */
	_fFacet       = kODNULL;
}


/*-------------------------------------------------------------------+
|                      CDPlayer::FacetAdded ()                       |
| ================================================================== |
|                                                                    |
| Function:    Create the CDPlayer window when we are given the      |
|              first facet.                                          |
|                                                                    |
| Parameters:  facet - facet that has been added                     |
|                                                                    |
+-------------------------------------------------------------------*/
SOM_Scope void  SOMLINK CDPlayerFacetAdded (
	CDPlayer*	somSelf,
	Environment*	ev, 
	ODFacet* 	facet)
{
	CDPlayerData* somThis = CDPlayerGetData (somSelf);
	CDPlayerMethodDebug("CDPlayer","CDPlayerFacetAdded");

	/*
	 *  Startup the CDPlayer when the first facet is added...
	 */
	if (_fFacet == kODNULL) {
		_fFacet = facet;

		somSelf->SetFrameSize (ev, facet->GetFrame (ev), 425, 205);
		gDisplay.Initialize (somSelf);
	}
}


/*-------------------------------------------------------------------+
|                         CDPlayer::Draw ()                          |
| ================================================================== |
|                                                                    |
| Function:    Redraw the CDPlayer...                                |
|                                                                    |
| Parameters:  facet        - facet that has been added              |
|              invalidShape - the region that needs to be redrawn    |
|                                                                    |
+-------------------------------------------------------------------*/
SOM_Scope void  SOMLINK CDPlayerDraw (
	CDPlayer*	somSelf,  
	Environment*	ev, 
	ODFacet* 	facet, 
	ODShape*	invalidShape)
{
	CDPlayerMethodDebug("CDPlayer","CDPlayerDraw");

	RECT rect;
	GetClientRect (facet->GetFacetHWND(ev), &rect);

	HWND hwnd = gDisplay.GetRootWindow ();
	MoveWindow (hwnd, rect.left, rect.top, 
		    rect.right, rect.bottom, TRUE);
	InvalidateRect (hwnd, NULL, TRUE);
}


/*-------------------------------------------------------------------+
|                     CDPlayer::SetFrameSize ()                      |
| ================================================================== |
|                                                                    |
| Function:    Set the specified frame's size                        |
|                                                                    |
| Algorithm:   o  Use transforms to set the size based on pixels..   |
|                                                                    |
| Parameters:  facet - the given facet                               |
|              size  - size of facet in pixels                       |
|                                                                    |
+-------------------------------------------------------------------*/
SOM_Scope void SOMLINK CDPlayerSetFrameSize(
	CDPlayer*	somSelf,
	Environment*	ev,
	ODFrame*	frame,
	long		width,
	long		height)
{
	CDPlayerMethodDebug("CDPlayer","SetFrameSize");

	/*
	 *  No point in continuing of the frame is NULL...
	 */
	if (!frame) return;

	POINT	WantOriginOffset;
	SIZE	WantSize;

	SIZEL szl;
	WantSize.cx = (int) width;
	WantSize.cy = (int) height;
	WantOriginOffset.x = 0;
	WantOriginOffset.y = 0;

	ODFacet* facet;
	ODFrameFacetIterator* facets = frame->CreateFacetIterator(ev);
	if ((facet = facets->First(ev)) != kODNULL)
	{
		TempODTransform transform = 
			facet->GetWindow(ev)->GetRootFacet(ev)->
			AcquireExternalTransform(ev, kODNULL);

		Point originPoint;
		originPoint.x = WantOriginOffset.x;
		originPoint.y = WantOriginOffset.y;

		ODPoint logicalOrigin(originPoint);
		transform->InvertPoint(ev, &logicalOrigin);

		Point wantsizePoint;
		wantsizePoint.x = WantSize.cx;
		wantsizePoint.y = WantSize.cy;

		ODPoint logicalWantsize(wantsizePoint);
		transform->InvertPoint(ev, &logicalWantsize);

		ODRect NewRectShape;

		NewRectShape.Set(logicalOrigin.x,
				 logicalOrigin.y,
				 logicalOrigin.x + logicalWantsize.x,
				 logicalOrigin.y + logicalWantsize.y);
		
		ODShape *wantShape = new ODShape;
		wantShape->InitShape(ev);

		wantShape->SetRectangle(ev, &NewRectShape);

		{
			TempODShape shape = 
				frame->AcquireUsedShape(ev, kODNULL);
			frame->Invalidate(ev, shape, kODNULL);
			frame->InvalidateActiveBorder(ev);
		}

		{
			TempODShape shape = 
				frame->AcquireUsedShape(ev, kODNULL);
			frame->RequestFrameShape(ev, wantShape, kODNULL);
			frame->Invalidate(ev, shape, kODNULL);
			frame->InvalidateActiveBorder(ev);
		}

		frame->ChangeUsedShape(ev, kODNULL, kODNULL);
	}
	delete facets;
}


/*-------------------------------------------------------------------+
|                        SOM Meta Class Methods                      |
+-------------------------------------------------------------------*/


SOMEXTERN void SOMLINK SOMInitModule (long majorVersion, long minorVersion,
	string className)
{
	SOM_IgnoreWarning (majorVersion);  /* This function makes   */
	SOM_IgnoreWarning (minorVersion);  /* no use of the passed  */
	SOM_IgnoreWarning (className);     /* arguments.            */

	CDPlayerNewClass ( CDPlayer_MajorVersion, CDPlayer_MinorVersion);
}

int CALLBACK LibMain (HINSTANCE inst, WORD ds, WORD Heapsize, LPSTR cmdLine)
{
	SOM_ClassLibrary ("iodcdply.dll");

	return 1;
}

SOM_Scope ISOString  SOMLINK M_CDPlayerclsGetODPartHandlerName(
	M_CDPlayer *somSelf, Environment *ev)
{
	M_CDPlayerMethodDebug("M_CDPlayer","M_CDPlayerclsGetODPartHandlerName");

	string handlerName = (string) SOMMalloc (strlen (kCDPlayerHandlerName)+1);
	strcpy (handlerName, kCDPlayerHandlerName);

	return (ISOString) handlerName;
}


SOM_Scope string  SOMLINK M_CDPlayerclsGetODPartHandlerDisplayName(
	M_CDPlayer *somSelf, Environment *ev)
{
	M_CDPlayerMethodDebug("M_CDPlayer","M_CDPlayerclsGetODPartHandlerDisplayName");

	string displayName = (string) SOMMalloc (strlen (kCDPlayerKindDisplayName)+1);
	strcpy (displayName, kCDPlayerKindDisplayName);

	return (ISOString) displayName;
}

SOM_Scope _IDL_SEQUENCE_PartKindInfo  SOMLINK M_CDPlayerclsGetODPartKinds(
		M_CDPlayer *somSelf, Environment *ev)
{
	M_CDPlayerMethodDebug("M_CDPlayer","M_CDPlayerclsGetODPartKinds");

	_IDL_SEQUENCE_PartKindInfo CDPlayerInfo;

	// Create structure PartKindInfo and allocate memory for variable
	PartKindInfo * info = (PartKindInfo *)SOMMalloc (sizeof (PartKindInfo));
	info->partKindName = (ISOString) SOMMalloc (strlen (kCDPlayerKind) + 1);
	info->partKindDisplayName = (string)SOMMalloc (
		strlen (kCDPlayerKindDisplayName)+1);
	info->filenameFilters =  (string)SOMMalloc (strlen ("")+1);
	info->filenameTypes =  (string)SOMMalloc (strlen ("")+1);
	info->categories =  (string)SOMMalloc (strlen (kCDPlayerCategory) + 1);
	info->categoryDisplayName =  (string)SOMMalloc (strlen ("Sample") + 1);
	info->objectID =  (string)SOMMalloc (strlen("") + 1);

	// Copy the information into the structure
	strcpy (info->partKindName, kCDPlayerKind);
	strcpy (info->partKindDisplayName, kCDPlayerKindDisplayName);
	strcpy (info->filenameFilters, "");
	strcpy (info->filenameTypes, "");
	strcpy (info->categories, kCDPlayerCategory);
	strcpy (info->categoryDisplayName, "Sample");
	strcpy (info->objectID, "");

	CDPlayerInfo._maximum = 1;
	CDPlayerInfo._length = 1;
	CDPlayerInfo._buffer = info;

	return CDPlayerInfo;
}

SOM_Scope string  SOMLINK M_CDPlayerclsGetOLE2ClassId(
	M_CDPlayer *somSelf, Environment *ev)
{
	M_CDPlayerMethodDebug("M_CDPlayer","M_CDPlayerclsGetOLE2ClassId");

	string CLSID = "{83e5ecf0-d0c6-11cf-96de-0004ac77f242}";

	string classID = (string) SOMMalloc (strlen (CLSID)+1);
	strcpy (classID, CLSID);

	return (ISOString) classID;
}

SOM_Scope string  SOMLINK M_CDPlayerclsGetWindowsIconFileName (
		M_CDPlayer *somSelf,Environment *ev)
{
	M_CDPlayerMethodDebug("M_CDPlayer","M_CDPlayerclsGetWindowsIconFileName");

	string icon = "iodcdply.ico";

	string iconFilename = (string) SOMMalloc (strlen (icon)+1);
	strcpy (iconFilename, icon);

	return (ISOString) iconFilename;
}
