⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chpanmod.cpp

📁 Windows上的MUD客户端程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*----------------------------------------------------------------------------
                        _                              _ _       
        /\             | |                            | (_)      
       /  \   _ __   __| |_ __ ___  _ __ ___   ___  __| |_  __ _ 
      / /\ \ | '_ \ / _` | '__/ _ \| '_ ` _ \ / _ \/ _` | |/ _` |
     / ____ \| | | | (_| | | | (_) | | | | | |  __/ (_| | | (_| |
    /_/    \_\_| |_|\__,_|_|  \___/|_| |_| |_|\___|\__,_|_|\__,_|

    The contents of this file are subject to the Andromedia Public
	License Version 1.0 (the "License"); you may not use this file
	except in compliance with the License. You may obtain a copy of
	the License at http://www.andromedia.com/APL/

    Software distributed under the License is distributed on an
	"AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
	implied. See the License for the specific language governing
	rights and limitations under the License.

    The Original Code is Pueblo client code, released November 4, 1998.

    The Initial Developer of the Original Code is Andromedia Incorporated.
	Portions created by Andromedia are Copyright (C) 1998 Andromedia
	Incorporated.  All Rights Reserved.

	Andromedia Incorporated                         415.365.6700
	818 Mission Street - 2nd Floor                  415.365.6701 fax
	San Francisco, CA 94103

    Contributor(s):
	--------------------------------------------------------------------------
	   Chaco team:  Dan Greening, Glenn Crocker, Jim Doubek,
	                Coyote Lussier, Pritham Shetty.

					Wrote and designed original codebase.

------------------------------------------------------------------------------

	Implementation for the ChGraphicPaneInfo class, which is used to manage the 
	graphics pane.

----------------------------------------------------------------------------*/

#include "grheader.h"
#include <ctype.h>
#include <ChCore.h>

#include "ChPanMod.h"
#include <ChHtmWnd.h>

#include "ChGrStrm.h"

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

#define GRAPHICS_PANE 1


/*----------------------------------------------------------------------------
	Handler declarations
----------------------------------------------------------------------------*/

CH_DECLARE_MESSAGE_HANDLER( paneLoadCompleteHandler )
CH_DECLARE_MESSAGE_HANDLER( paneLoadErrorHandler )
CH_DECLARE_MESSAGE_HANDLER( paneLoadImageHandler )
CH_DECLARE_MESSAGE_HANDLER( paneInitHandler )
CH_DECLARE_MESSAGE_HANDLER( paneLoadSceneHandler )
CH_DECLARE_MESSAGE_HANDLER( paneImageHookHandler )
CH_DECLARE_MESSAGE_HANDLER( paneCmdHookHandler )
CH_DECLARE_MESSAGE_HANDLER( paneShowModuleHandler )
CH_DECLARE_MESSAGE_HANDLER( paneResetHandler )
CH_DECLARE_MESSAGE_HANDLER( paneInstallHookHandler )
CH_DECLARE_MESSAGE_HANDLER( panePromoteHookHandler )
CH_DECLARE_MESSAGE_HANDLER( paneUninstallHookHandler )
CH_DECLARE_MESSAGE_HANDLER( panePaletteChangeHandler )


static ChMsgHandlerDesc	paneHandlers[] =
							{	{CH_MSG_LOAD_COMPLETE,	paneLoadCompleteHandler},
								{CH_MSG_LOAD_ERROR,		paneLoadErrorHandler},
								{CH_MSG_LOAD_SCENE,		paneLoadSceneHandler},
								{CH_MSG_SHOW_MODULE,	paneShowModuleHandler},
								{CH_MSG_RESET,			paneResetHandler},
								{CH_MSG_INIT, 			paneInitHandler},
								{CH_MSG_INLINE,			paneImageHookHandler},
								{CH_MSG_CMD,			paneCmdHookHandler},
								{CH_MSG_INSTALL_HOOK, 	paneInstallHookHandler},
								{CH_MSG_PROMOTE_HOOK, 	panePromoteHookHandler},
								{CH_MSG_UNINSTALL_HOOK, paneUninstallHookHandler},
								{CH_MSG_LOAD_IMAGE,		paneLoadImageHandler}
							};

#if defined( CH_MSW ) && defined( CH_ARCH_16 )
#include <ChUtil.h>
#endif

/*----------------------------------------------------------------------------
	ChGraphicPaneInfo class
----------------------------------------------------------------------------*/

ChGraphicPaneInfo::ChGraphicPaneInfo( ChModuleID idModule,
										ChCore* pCore ) :
					ChMainInfo( idModule, pCore ),
					m_graphicDispatcher( pCore, idModule, defGraphicHandler ),
					m_imageHookMgr( pCore, idModule ),
					m_cmdHookMgr( pCore, idModule ),
					m_pViewers(0),
					m_pPendingViewers(0),
					m_pCurrentViewer(0),
					m_currentViewerID(0),
					m_pPendingLoad(0),
					m_pSuspendedLoad(0),
					m_boolShown( false )
{
 	m_pGraphicStream = new ChGraphicStreamManager( pCore, 0 ); 
	ASSERT( m_pGraphicStream );
}

class DeleteViewerOp : public 
						#if !defined( NO_TEMPLATES )
						ChVisitor2<ChHTTPConn::tagMimeTypes, pChGraphicViewer>  
						#else 
						ChSplayHTTPViewerVisitor2
						#endif
{
	public:
		 bool Visit( const ChHTTPConn::tagMimeTypes& key,  const pChGraphicViewer& pVwr )
				{
					delete pVwr;
					return true;
				}
};

ChGraphicPaneInfo::~ChGraphicPaneInfo()
{
	ShowModule( false );
											// Clean out viewer lookup tables
	DeleteViewerOp		zapViewers;
	if (m_pViewers)
	{
		m_pViewers->Infix( zapViewers );
		delete m_pViewers;
		m_pViewers = 0;
	}

	if (m_pPendingViewers)
	{
		m_pPendingViewers->Infix( zapViewers );
		delete m_pPendingViewers;
		m_pPendingViewers = 0;
	}
											// Get rid of preloaded viewers
	if (m_idMazeModule)
	{
		UnloadClientModule( m_idMazeModule );
	}

	if (m_idAnimModule)
	{
		UnloadClientModule( m_idAnimModule );
	}

	// Cleanup HTTP Stream
	delete m_pGraphicStream;

}


void ChGraphicPaneInfo::Initialize()
{	
											// Load graphics viewers

	RegisterPendingViewer( CH_MODULE_GRAPHICS_ANIMATION, ChHTTPConn::typeGIF );
	RegisterPendingViewer( CH_MODULE_GRAPHICS_ANIMATION, ChHTTPConn::typeJPEG );
	RegisterPendingViewer( CH_MODULE_GRAPHICS_ANIMATION, ChHTTPConn::typeBMP );
	RegisterPendingViewer( CH_MODULE_GRAPHICS_MAZE, ChHTTPConn::typeVRML );

	LoadClientModule( CH_MODULE_GRAPHICS_MAZE, CH_MODULE_GRAPHICS_BASE,
						GetModuleID() );
	LoadClientModule( CH_MODULE_GRAPHICS_ANIMATION, CH_MODULE_GRAPHICS_BASE,
						GetModuleID() );

}


void ChGraphicPaneInfo::RegisterDispatchers()
{
	chint16		sHandlerCount = sizeof( paneHandlers ) /
								sizeof( ChMsgHandlerDesc );

	m_graphicDispatcher.AddHandler( paneHandlers, sHandlerCount );
}


void ChGraphicPaneInfo::HookViewerMessages(ChModuleID idModule)
{
	ChInstallHookMsg	msg( GetModuleID(), CH_MSG_CMD );

	GetCore()->DispatchMsg( idModule, msg );
}


void ChGraphicPaneInfo::ShowModule( bool boolShow )
{
	if (boolShow && !IsShown())
	{
		InstallHook( CH_MSG_INLINE, GetModuleID() );
		m_boolShown = true;
	}
	else if (!boolShow && IsShown())
	{
		UninstallHook( CH_MSG_INLINE, GetModuleID() );
		m_boolShown = false;
	}

	if (GetCurrentViewer())
	{
		ChShowModuleMsg		msg( boolShow );

		GetCurrentViewer()->Send( msg );
	}
}


// ===========================
// Command methods and helpers
// ===========================


string TrimArg(string & strVal )
{
	string strValue = strVal;

	#if defined( CH_ARCH_16 )    
	TrimLeft( strValue );
	#else
	strValue.TrimLeft();
	#endif
	char cmd[100];
		#if defined( CH_ARCH_16 ) 
	    
	for ( int i = 0; i < 99 && !isspace( strValue[i] );  i++ )
	{
		cmd[i] = strValue[i];
	}  
	cmd[i] = 0;
	#else
	::sscanf(LPCTSTR(strValue), "%s", cmd);
	#endif
	string strCmd(cmd);
	#if defined( CH_ARCH_16 )   
	TrimLeft( strCmd );
	TrimRight( strCmd ); 
	#else
	strCmd.TrimLeft();
	strCmd.TrimRight();
	#endif
	return strCmd;
}


bool ChGraphicPaneInfo::DoCommand( string& strArgs )
{
	string strValue;
//	TRACE1("DoCommand: %s\n", strArgs);
	bool boolProcessed = false;
	//  respond to hooked img messages

	// We already screened in the hook handler for img's of interest
	
	if (ChHtmlWnd::GetHTMLAttribute( strArgs, CH_GRAPH_CMD, strValue ))
	{
		string strCmd = TrimArg(strValue);
		if (!strCmd.CompareNoCase( "load" ))
		{
			DoLoadCommand( strArgs );
			boolProcessed = true;
		}
		else if(!strCmd.CompareNoCase( "show" ) || !strCmd.CompareNoCase( "hide" ))
		{
			DoShowCommand( strArgs );
			boolProcessed = true;
		}
	}

	if(!boolProcessed)
	{
		DoViewerCommand( strArgs );
		boolProcessed = false;		// allow others to hook
	}

	return boolProcessed;
}


void ChGraphicPaneInfo::DoViewerCommand(string& strArgs)
{
	ChGraphicDocumentHTTPReq*		pDependent;
	if (pDependent = GetPendingLoad())
	{
									// Queue up message
		pDependent->AddCommand( strArgs );
	}
	else
	{	
		SendViewerCommand( strArgs );
	}
}


void ChGraphicPaneInfo::SendViewerCommand(string& strArgs, ChViewerMsgDispatch* pMod)
{
	ChInlineMsg msg( strArgs );

	if (!pMod)
	{
		pMod = GetCurrentViewer();
	}

	if (pMod)
	{
		pMod->Post( msg );
	}

	//if(pMod) pMod->Send(msg);
}

void ChGraphicPaneInfo::DoLoadCommand( string& strArgs )
{
	//  respond to hooked img load message; preempt outstanding loads if any
	string		strURL;
									// Needs to prempt in-progress load

	if(ChHtmlWnd::GetHTMLHref( strArgs, true, strURL ))
	{
		ChGraphicDocumentHTTPReq *pHTTPReq = new ChGraphicDocumentHTTPReq ( GetModuleID(), strURL );

		if (GetPendingLoad())
		{
			GetPendingLoad()->Cancel();
			SetPendingLoad( 0 );
		}

		SetPendingLoad( pHTTPReq );
		
		string strOption;

		ChHtmlWnd::GetHTMLAttribute( strArgs, "xch_notify", strOption );
		pHTTPReq->SetNotificationOption(strOption);

		ChViewerMsgDispatch* pViewer	= GetCurrentViewer();
		if(pViewer)
		{
			ChInlineMsg msg(strArgs);
			pViewer->Send(msg);

			// Turn off animation, so we get a little time to think
			ChPlayGraphicMsg stopPlayingRightNow(0, false, ChPlayGraphicMsg::immediate);
			pViewer->Send(stopPlayingRightNow);
		}
		GetCore()->GetURL( strURL, 0, GetStream(), (chparam)pHTTPReq );
	}

	return;
}

void ChGraphicPaneInfo::DoShowCommand(string& strArgs)
{
	string strValue;
									//  Show or hide the current viewer
	if (ChHtmlWnd::GetHTMLAttribute( strArgs, CH_GRAPH_CMD, strValue ))
	{
		string strCmd = TrimArg(strValue);
		bool boolShow = !strCmd.CompareNoCase( "show" );
		ChShowModuleMsg msg(boolShow);

		if(GetCurrentViewer()) GetCurrentViewer()->Send(msg);
	}
	return;
}

void ChGraphicPaneInfo::DoQueuedCommands(ChGraphicDocumentHTTPReq *pReq)
{
	// Sends queued commands on to current viewer
	ChStrList *pCommandQ = pReq->GetCommandQueue();

	if(pCommandQ)
	{
		string strCmd;

		while (!pCommandQ->IsEmpty())
		{
			strCmd = pCommandQ->RemoveHead();
			SendViewerCommand(strCmd);
		}

		delete pCommandQ;
		pReq->SetCommandQueue(0);
	}

	return;
}

// ===========================
// Viewer registration methods
// ===========================

void ChGraphicPaneInfo::RegisterViewer( const string &strModuleName,
										ChModuleID idModule )
{
	if(CH_MODULE_GRAPHICS_ANIMATION == strModuleName) 
	{
		RegisterViewer( strModuleName,idModule, ChHTTPConn::typeGIF );
		RegisterViewer( strModuleName, idModule, ChHTTPConn::typeJPEG );
		RegisterViewer( strModuleName, idModule, ChHTTPConn::typeBMP );
		//HookViewerMessages(idModule);						   // it doesn't understand yet
	}
	else if (CH_MODULE_GRAPHICS_MAZE == strModuleName)
	{
		RegisterViewer( strModuleName,idModule, ChHTTPConn::typeVRML );
		HookViewerMessages(idModule);
	}
}


void ChGraphicPaneInfo::RegisterViewer( const string &strModuleName,
										ChModuleID idModule,
										ChHTTPConn::tagMimeTypes type )
{
											/* This should do a use on the
												module in the future, then
												release when we destroy the
												table */

	ChGraphicViewer*	pViewer = new ChGraphicViewer( this, strModuleName, idModule );

	if (!m_pViewers)
	{
		m_pViewers = new ChViewerSplay;
	}

	m_pViewers->Insert( type, pViewer );

											// Now delete from pending, if found
	ChGraphicViewer**	ppPendingViewer;

	if (m_pPendingViewers)
	{										/* Note we assume we never have
												more than one outstanding
												viewer load for any one mime
												type - having more than one
												registered is bogus anyways */

		ppPendingViewer = m_pPendingViewers->Find( type );
		if (ppPendingViewer)
		{
			ChGraphicViewer*	pDeletedViewer = *ppPendingViewer;

			m_pPendingViewers->Delete( type );
											// Delete the viewer object
			delete pDeletedViewer;
		}

		#if 0

		if(m_pPendingViewers->IsEmpty())
		{
			delete m_pPendingViewers;
			m_pPendingViewers = 0;
		}

		#endif
	}

	// Now, if a load was waiting for this viewer, do it
	if(m_pSuspendedLoad)
	{
		string strType = m_pSuspendedLoad->GetMimeType();
		if(type == (ChHTTPConn::tagMimeTypes)ChHTTPConn::GetMimeType(strType))
		{
			Load(m_pSuspendedLoad);
		}
	}

}


void ChGraphicPaneInfo::RegisterPendingViewer( const string &strModuleName,
												ChHTTPConn::tagMimeTypes type )
{
	ChGraphicViewer*	pViewer = new ChGraphicViewer( this, strModuleName );

	if (!m_pPendingViewers)
	{
		m_pPendingViewers = new ChViewerSplay;
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -