/*
* Copyright 2004, 2005 Perforce Software. All rights reserved.
*
* Developed by Data Shades Ltd.
*/
#include <xsi_pluginregistrar.h>
#include <xsi_application.h>
#include <xsi_viewcontext.h>
#include <xsi_context.h>
#include <xsi_command.h>
#include <xsi_menu.h>
#include <xsi_comapihandler.h>
using namespace XSI;
#define P4SOFTIMAGE_MAIN
#include "P4GTSoftImage.h"
#include "Translate.h"
#ifdef _DEBUG
#include "debug.h"
#endif
#define MAIN_WINDOW_TITLE _T("SOFTIMAGE")
#define MAIN_WINDOW_TITLE2 _T("Autodesk Softimage")
#ifndef LANG_ja
#include "PluginNewMessages.h"
#define PLUGIN_PATH L"\\Application\\Plugins\\P4GT-Softimage.dll"
#else
#include "PluginNewMessages_ja.h"
#define PLUGIN_PATH L"\\Application\\Plugins\\P4GT-Softimage_ja.dll"
#endif
/*
* Holds handle to top level Softimage window
*/
static HWND mainWindow = 0;
/*
* Register interest for events.
*/
static CStatus RegisterEvent( CString event )
{
Application app;
CString plugin = app.GetInstallationPath(siFactoryPath) + PLUGIN_PATH;
CValueArray args(8);
args[0] = CValue(event);
args[1] = CValue(plugin);
args[2] = CValue(L"");
args[3] = CValue(L"cpp");
args[4] = CValue(L"P4GT_" + event);
args[5] = CValue();
args[6] = CValue();
args[7] = CValue(false);
CValue ret;
CComAPIHandler com( app.GetRef() );
return com.Call( L"Advise", ret, args );
}
/*
* Plugin Initialisation. This gets all first.
*/
CStatus XSILoadPlugin( XSI::PluginRegistrar& in_reg )
{
in_reg.PutAuthor( L"Perforce Software Inc." );
in_reg.PutName( L"P4GT - Softimage" );
in_reg.PutVersion( 2004, 2 );
in_reg.PutURL( L"www.perforce.com" );
in_reg.PutEmail( L"support@perforce.com" );
Application app;
/* Don't do anything if we are not in interactive mode. */
if ( app.IsInteractive() && mainWindow ) {
// Register menu commands
in_reg.RegisterCommand( L"PerforceAddMenuItem", L"PerforceAddMenuItem" );
in_reg.RegisterCommand( L"PerforceCheckOutMenuItem", L"PerforceCheckOutMenuItem" );
in_reg.RegisterCommand( L"PerforceCheckInMenuItem", L"PerforceCheckInMenuItem" );
in_reg.RegisterCommand( L"PerforceLockMenuItem", L"PerforceLockMenuItem" );
in_reg.RegisterCommand( L"PerforceUndoMenuItem", L"PerforceUndoMenuItem" );
in_reg.RegisterCommand( L"PerforceResultsMenuItem", L"PerforceResultsMenuItem" );
in_reg.RegisterCommand( L"PerforceStatusMenuItem", L"PerforceStatusMenuItem" );
in_reg.RegisterCommand( L"PerforceHistoryMenuItem", L"PerforceHistoryMenuItem" );
in_reg.RegisterCommand( L"PerforceOpenedMenuItem", L"PerforceOpenedMenuItem" );
in_reg.RegisterCommand( L"PerforceInfoMenuItem", L"PerforceInfoMenuItem" );
in_reg.RegisterCommand( L"PerforceOpenMenuItem", L"PerforceOpenMenuItem" );
in_reg.RegisterCommand( L"PerforceOptionsMenuItem", L"PerforceOptionsMenuItem" );
in_reg.RegisterCommand( L"PerforceHelpMenuItem", L"PerforceHelpMenuItem" );
in_reg.RegisterCommand( L"PerforceAboutMenuItem", L"PerforceAboutMenuItem" );
in_reg.RegisterCommand( L"PerforceOnlineMenuItem", L"PerforceOnlineMenuItem" );
// Register Perforce menu plugin
in_reg.RegisterMenu( siMenuMainTopLevelID, L"Perforce", true, true );
// Register for events
RegisterEvent(L"OnEndSceneOpen");
RegisterEvent(L"OnBeginNewScene");
RegisterEvent(L"OnBeginSceneOpen");
}
return XSI::CStatus::OK;
}
/*
* Called when plugin is unloaded. Note that DLL can be unloaded
* without this being called which is why we do all init/shutdown
* in DllMain
*/
XSI::CStatus XSIUnloadPlugin( const XSI::PluginRegistrar& in_reg )
{
return XSI::CStatus::OK;
}
/*
* Event callback. All events come through here
*/
XSI::CStatus XSIOnEventCPP( long in_eventID, XSI::CValueArray& in_args )
{
switch (in_eventID)
{
case siOnEndSceneOpen:
perforceOps->FileOpenEvent();
break;
case siOnBeginNewScene:
case siOnBeginSceneOpen:
perforceOps->FileCloseEvent();
break;
};
return CStatus::OK;
}
/*
* Enable/disable menu command
*/
static void SetCommandState( CommandArray &cmds, wchar_t *name, bool enable )
{
Command c = cmds.GetItem(name);
c.PutEnabled( enable );
c.Update();
}
/*
* This function gets called everytime Perforce menu is about to be shown
*/
XSI::CStatus Perforce_Init( XSI::CRef& in_ref )
{
Context ctxt = in_ref;
Menu menu = ctxt.GetSource();
MenuState *state = perforceOps->GetMenuState();
Application app;
CommandArray cmds = app.GetCommands();
/*
* Set enabled/disabled state of all menu items
*/
SetCommandState(cmds, L"PerforceAddMenuItem", state->AddEnabled());
SetCommandState(cmds, L"PerforceCheckOutMenuItem", state->CheckOutEnabled() );
SetCommandState(cmds, L"PerforceCheckInMenuItem", state->CheckInEnabled() );
SetCommandState(cmds, L"PerforceLockMenuItem", state->LockUnlockEnabled() );
SetCommandState(cmds, L"PerforceUndoMenuItem", state->UndoEnabled() );
SetCommandState(cmds, L"PerforceResultsMenuItem", state->ResultsEnabled() );
SetCommandState(cmds, L"PerforceStatusMenuItem", state->DocumentStatusEnabled() );
SetCommandState(cmds, L"PerforceHistoryMenuItem", state->DocumentHistoryEnabled() );
SetCommandState(cmds, L"PerforceOpenedMenuItem", state->CheckedOutEnabled() );
SetCommandState(cmds, L"PerforceInfoMenuItem", state->InfoEnabled() );
SetCommandState(cmds, L"PerforceOpenMenuItem", state->OpenEnabled() );
/*
* Create menu
*/
MenuItem item;
menu.AddCommandItem( P4_MENU_ADD, L"PerforceAddMenuItem", item );
menu.AddCommandItem( P4_MENU_CHECKOUT, L"PerforceCheckOutMenuItem", item );
menu.AddCommandItem( P4_MENU_CHECKIN, L"PerforceCheckInMenuItem", item );
menu.AddCommandItem( state->LockUnlockLabel(), L"PerforceLockMenuItem", item );
menu.AddCommandItem( state->UndoLabel(), L"PerforceUndoMenuItem", item );
menu.AddSeparatorItem();
menu.AddCommandItem( P4_MENU_RESULT_CMD, L"PerforceResultsMenuItem", item );
menu.AddCommandItem( P4_MENU_DOC_STAT, L"PerforceStatusMenuItem", item );
menu.AddCommandItem( P4_MENU_DOC_HIST, L"PerforceHistoryMenuItem", item );
menu.AddCommandItem( P4_MENU_DOC_CHECKED_OUT, L"PerforceOpenedMenuItem", item );
menu.AddCommandItem( P4_MENU_SERVER_INFO, L"PerforceInfoMenuItem", item );
menu.AddSeparatorItem();
menu.AddCommandItem( P4_MENU_GET_LATEST, L"PerforceOpenMenuItem", item );
menu.AddSeparatorItem();
menu.AddCommandItem( P4_MENU_OPTIONS, L"PerforceOptionsMenuItem", item );
menu.AddCommandItem( P4_MENU_HELP, L"PerforceHelpMenuItem", item );
menu.AddCommandItem( P4_MENU_ABOUT, L"PerforceAboutMenuItem", item );
menu.AddSeparatorItem();
menu.AddCommandItem( state->EnableDisableLabel(), L"PerforceOnlineMenuItem", item );
delete state;
return CStatus::OK;
}
/*
* Find the handle of the main softimage window
*/
BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM id)
{
DWORD windProcId;
GetWindowThreadProcessId( hWnd, &windProcId );
if ( windProcId == id ) {
TCHAR titlename[256];
GetWindowText( hWnd, titlename, 256);
/*
* Look for main window title
*/
if ( _tcsncmp(titlename, MAIN_WINDOW_TITLE, _tcslen(MAIN_WINDOW_TITLE)) == 0) {
mainWindow = hWnd;
return FALSE;
} else if ( _tcsncmp(titlename, MAIN_WINDOW_TITLE2,
_tcslen(MAIN_WINDOW_TITLE2)) == 0) {
mainWindow = hWnd;
return FALSE;
}
}
return TRUE;
}
/*
* DLL entry function
*/
BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved )
{
static int G_processes = 0;
switch ( ul_reason_for_call ) {
case DLL_PROCESS_ATTACH:
if ( G_processes == 0 ) {
#ifdef _DEBUG
DBG_open();
#endif
// Save plugin instance handle
hInstance = (HINSTANCE)hModule;
// Get main window handle
EnumWindows( EnumWindowsProc, GetCurrentProcessId() );
// Need to init when DLL is loaded as Softimage
// can load/unload DLL at any time
if ( mainWindow ) {
host = new SoftImageHost( hInstance, mainWindow );
perforceOps = new PluginOperations( host );
}
}
G_processes++;
break;
case DLL_PROCESS_DETACH:
G_processes--;
if ( G_processes == 0 ) {
#ifdef _DEBUG
DBG_close();
#endif
if ( mainWindow ) {
delete host;
delete perforceOps;
}
}
break;
}
return TRUE;
}