/* $Header$ */

/*********************************** Changes ***********************************
Date			Edit				Bug					Description
22-Jan-21	rmm_svg3								Support for themed fat client SVG icons.
23 NOV 01	mpm5031									New platform independent hiliting
*********************************************************************************/

#include <extcomp.he>
#include <extfval.he>
#include <hwnd.he>
#include <gdi.he>
#include "piclist.he"

const qshort	cPicListNum 				= 1;

ECOproperty PICLISTproperties[1] =
{ 
	cPicListNum,			4000, 	fftInteger, EXTD_FLAG_PROPDATA, 0, 0, 0
};

/* Component library entry point (name as declared in resource) */
extern "C" LRESULT OMNISWNDPROC ListWndProc(HWND hwnd, UINT Msg,WPARAM wParam,LPARAM lParam,EXTCompInfo* eci)
{
   ECOsetupCallbacks(hwnd,eci);		/* Initialize callback tables */
	 switch (Msg)
	 {
			case ECM_GETVERSION:
			{
				return ECOreturnVersion( 1, 1 ); // mpm5031
			}
			case ECM_LISTDRAWLINE:   /* Draw list line */
			{
				tqfListObject* object = (tqfListObject*)ECOfindObject( eci, hwnd );
				if ( NULL!=object )
					return object->drawSingleLine( (EXTListLineInfo*)lParam, eci );
				break;
			}
			case ECM_OBJCONSTRUCT:				/* Construct an object */
			{
				tqfListObject* object = new tqfListObject(hwnd, eci);	// rmm_svg3
				ECOinsertObject( eci, hwnd, (void*)object );
				return qtrue;
			}
			case ECM_OBJDESTRUCT:					/* Destruct the object */
			{
				tqfListObject* object = (tqfListObject*)ECOremoveObject( eci, hwnd );
				if ( NULL!=object )
					delete object;
				return qtrue;
			}
      case ECM_GETCOMPLIBINFO: /* Return component library name & component count */
      {
      	return ECOreturnCompInfo( gInstLib, eci, LIB_RES_NAME, OBJECT_COUNT );
      }
			case ECM_GETCOMPICON: 	/* Return component icon (as a hbitmap) */
			{
				if ( eci->mCompId==OBJECT_ID1 ) return ECOreturnIcon( gInstLib, eci, LIST_ICON );
				return qfalse;
			}
			case ECM_GETCOMPID:			/* Get the external object name & id (from a sequential wParam) */
			{	
				if (wParam == 1)
					return ECOreturnCompID(gInstLib, eci, OBJECT_ID1, cObjType_List);
				return 0L;
			}
			case ECM_GETPROPNAME:
			{
				return ECOreturnProperties( gInstLib, eci, &PICLISTproperties[0], 1 );
			}                                                                                 
			case ECM_PROPERTYCANASSIGN:  	/* Object: Is the property assignable (ie readonly?) */
			case ECM_SETPROPERTY: 				/* Object: Assignment to a property */
			case ECM_GETPROPERTY:					/* Object: Retrieve value from property */
			{
				tqfListObject* object = (tqfListObject*)ECOfindObject( eci, hwnd );
				if ( object )
					return object->attributeSupport( Msg, wParam, lParam, eci );
				return 0L;
			}
			case WM_FLD_FONTCHANGED:	// rmm_svg3
			{
				tqfListObject* object = (tqfListObject*)ECOfindObject(eci, hwnd);
				if (object)
				{
					object->getTextSpec(eci);
					WNDinvalidateRect(hwnd, 0);
				}
				break;
			}
	 }
	 return WNDdefWindowProc(hwnd,Msg,wParam,lParam,eci);
}

tqfListObject::tqfListObject(HWND pFieldHWnd, EXTCompInfo* pEci)	// rmm_svg3
{
	mHWnd = pFieldHWnd;
	mApp = ECOgetApp(pEci->mInstLocp);	// rmm_svg3
	mIconColumn = 1;
	ECOlistSetLineHeight( mHWnd, 32); // Set lineheight
	getTextSpec(pEci);	// rmm_svg3
}

tqfListObject::~tqfListObject()
{
}

// rmm_svg3:
void tqfListObject::getTextSpec(EXTCompInfo* pEci)
{
	EXTfldval fval, fval2;
	ECOgetProperty(mHWnd, anumFont, fval);
	ECOgetProperty(mHWnd, anumFontsize, fval2);
	ECOgetFont(mHWnd, &mTextSpec.mFnt, (qshort)fval.getLong(), (qshort)fval2.getLong());

	ECOgetProperty(mHWnd, anumFontstyle, fval);
	mTextSpec.mSty = (qsty)fval.getLong();

	ECOgetProperty(mHWnd, anumAlign, fval);
	mTextSpec.mJst = (qjst)fval.getLong();

	ECOgetProperty(mHWnd, anumTextColor, fval);
	mTextSpec.mTextColor = (qcol)fval.getLong();
}

qlong tqfListObject::attributeSupport( LPARAM pMessage, WPARAM wParam, LPARAM lParam, EXTCompInfo* eci )
{
	switch( pMessage )
	{ 
		case ECM_PROPERTYCANASSIGN: 
		{
			return 1L;
		}
		case ECM_SETPROPERTY: 
		{       
			EXTParamInfo* param = ECOfindParamNum( eci, 1 );
			if ( param )
			{
				EXTfldval fval( (qfldval)param->mData );
				switch( ECOgetId(eci) )
				{
					case cPicListNum: 			mIconColumn = (qshort)fval.getLong(); break; 			
				}       
				return 1L;
			}
			break;
		}
		case ECM_GETPROPERTY: 
		{
			EXTfldval fval;
			switch( ECOgetId(eci) )
			{
				case cPicListNum: 			fval.setLong( (qlong)mIconColumn ); break; 			
			}         
			ECOaddParam(eci,&fval);
			return 1L;
		}
	}
	return 0;
}

qbool tqfListObject::drawSingleLine(EXTListLineInfo* pLineInfo,EXTCompInfo* pEci)
{
	EXTParamInfo* cparam = ECOfindParamNum( pEci, 1 );
	if (cparam && pLineInfo)	// Ensure parameter & line information are present
	{
		// Get icon number (assumed to be in list line data,always passed as param one)
		EXTfldval extfldval( (qfldval)cparam->mData );
		// Start rmm_svg3
		qHandle han = extfldval.getHandle(qfalse);
		qHandleTextPtr hp(han, 0);
		qchar* add = *hp;
		qlong len = hp.charLen();
		// End rmm_svg3
		// Get data from list column 2 (if more data than in parameter 1 is required)
		EXTqlist* extqlist=(EXTqlist*)pLineInfo->mListPtr;
		EXTfldval iconid;	// rmm_svg3
		if ( mIconColumn>0 && mIconColumn<=extqlist->colCnt() )
			extqlist->getColValRef(pLineInfo->mLine, mIconColumn, iconid, qfalse);	// rmm_svg3
		EXTBMPref* bmp = new EXTBMPref(iconid, mApp); // rmm_svg3
		// Erase line rectangle
		HBRUSH brush = GDIcreateBrush( patFill );
		GDIfillRect(pLineInfo->mHdc,&pLineInfo->mLineRect,brush);
		GDIdeleteObject(brush);
		// start hiliting
		qcol textColor = mTextSpec.mTextColor; // mpm5031 begins // rmm_svg3
		if ( pLineInfo->mSelected )
		{
			GDIhiliteTextStart( pLineInfo->mHdc, &pLineInfo->mLineRect, textColor); // rmm_svg3
			textColor = GDIgetTextColor( pLineInfo->mHdc );
		} // mpm5031 ends

		// Draw bitmap & line data
		qrect r1; GDIcopyRect(&r1,&pLineInfo->mLineRect);
		if ( bmp ) 
		{
			ePicSize psize = EXTBMPref::getBmpSize( iconid );
			r1.right = r1.left + r1.height();
			// Bitmap
			GDIsetTextColor(pLineInfo->mHdc, textColor);	// rmm_svg3
			bmp->draw(pLineInfo->mHdc,&r1,
								psize , picNormal, qfalse, colNone, qtrue , jstCenter, jstCenter);
			delete bmp;
			r1.left = r1.right + 5;	r1.right = pLineInfo->mLineRect.right;
		}		
		// Draw line text
		// Start rmm_svg3
		qdim top = r1.top + (r1.height() - GDIfontHeight(pLineInfo->mHdc))/2;
		if (pLineInfo->mStyledText)
		{
			GDItextSpecStruct ts(mTextSpec);
			ts.mTextColor = textColor;
			GDIdrawTextStruct dts(pLineInfo->mHdc, r1.left, top, add, (qshort)len, &ts, 0, 0, GDI_DT_FLAGS_ISASCII, mApp, 0);
			GDIdrawTextJst(&dts);
		}
		else
		{
			// End rmm_svg3
			GDIsetTextColor(pLineInfo->mHdc, textColor); // mpm5031
			GDIdrawText(pLineInfo->mHdc, r1.left, top, add, (qshort)len, jstLeft); // rmm_svg3
		}
		// End rmm_svg3
		// end hiliting
		if ( pLineInfo->mSelected ) // mpm5031
			GDIhiliteTextEnd( pLineInfo->mHdc, &pLineInfo->mLineRect, GDI_COLOR_WINDOWTEXT ); // mpm5031
		// Draw focus rectangle
		#ifndef isCOCOA	// rmm_svg3
			if (pLineInfo->mDrawFocusRect && !ECOisVista())	// rmm_svg3
			{
				pLineInfo->mLineRect.right++;
				GDIdrawFocusRect( pLineInfo->mHdc, &pLineInfo->mLineRect );
			}
		#endif
		return qtrue;
	}
	return qfalse;
}
// End of file
