

#include "windows.h"
#include "math.h"
#include <malloc.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "uiscreen.h"
#include "dxquad.h"
#include "giged3d.h"
#include "globals.h"
#include "gigeapp.h"
#include "prefsXML.h"
#include "project.h"
#include "camcontroller.h"
#include "buffer.h"
#include "convert.h"
#include "plathread.h"

#include "acqthread.h"
#include "blkthread.h"
#include "config.h"
#include "timeline.h"
#include "lut3d.h"
#include "siusbtimecode.h"
#include "playlist.h"
#include "uimacro.h"
#include "macro.h"
#include "acqthread.h"
#include "device.h"
#include "simenmonitor.h"
#include "logger.h"
#include "quathread.h"
#include "alelogfile.h"
#include "devicemanager.h"
#include "playwnd.h"
#include "vector"
#include "diskinfo.h"
#include "siblist.h"
#include <crtdbg.h>
#include "timecodecontroller.h"
#include "slate.h"
#include "oglthread.h"

void OnSaveLookFile( int, int );
extern void OnStartGrabbing( void );
extern void StopClip(void);
extern void CloseAVIClip( bool deleteClip );
extern void OnStopRecording( void );
extern void resetImageCoordinates( void );
extern DWORD CountWalk(void);
//extern void ConfigPrecache( void );
extern void CheckDisk( bool force );
extern void UpdateHistogramSIV();
extern void FlushFiles( char volumeLetter );
extern bool OpenProject( char *filename );
extern bool dgOK;
extern void OnFileExportDestDriveError( bool ntfsLimit );
extern CSIDSMediaFilePlayer *gSIPlayer;

float scol[BUFFER_STATE_LAST+1][3] = {
	0, 0, 1,	// blue		FREE
	0, 1, 0,	// green	COMPLETE
	1, 0, 0,	// red		DRAWN
	1, 1, 0	// yellow : in process
};


// #define ARI_TEST
#define RECORD_TICKS 2000

static UINT m1;
static UINT m2;
static UINT md;
extern int gFails;
extern int gSends;
extern CDiskInfo gDiskInfo;
extern int gLockCompression;
int disp3DGuiModeindex = 0;

float pixelY[MOD_TABLE_ENTRIES] = {
	0,
	5,
	10,
	15,
	41,
	68,
	103,
	131,
	155,
	175,
	178,
	181,
	183,
	186,
	188,
	192,
	195
};

float pct[MOD_TABLE_ENTRIES] = {
	100,
	99,
	97,
	93,
	81,
	66,
	51,
	35,
	21,
	10,
	5,
	3,
	2,
	1,
	0.5f,
	0.25f,
	0
};

#define FC_NUM_BOXES 9

int fc_heights[FC_NUM_BOXES] = {
10,
15,
50,
75,
75,
65,
50,
60,
100
};

char *fc_labels[FC_NUM_BOXES+1] = {
"100",
"",
"95",
"85",
"70",
"55",
"42",
"32",
"20",
"0"
};


unsigned char fc_colors[FC_NUM_BOXES+1][3] = {
	255, 0, 0,
	255, 0, 0,
	255, 102, 0,
	255, 102, 0,
	255, 234, 0,
	130, 130, 130,
	107, 107, 107,
	28, 76, 34,
	0, 8, 51,
	0, 3, 16
};

char * mMetaDlgText[ 7 ] = { "Director", "Producer", "DirPhotography", "Production", "Location", "Keywords", "Comments" };
char * mSceneShotTakeDlgText[ 4 ] = { "Scene", "Shot", "Modifier", "Take" };

#define METER1_X 1188
#define METER2_X 1238
#define METER1_FS_Y 755
#define METER2_FS_Y 790
#define METER_W 22
#define METER1_C (METER1_X+METER_W/2)
#define METER2_C (METER2_X+METER_W/2)
#define METER_FS_H 10
#define METER1_FS_C ( METER1_FS_Y + METER_FS_H / 2 )
#define METER2_FS_C ( METER2_FS_Y + METER_FS_H / 2 )


extern void OnToggleLoupe( int x, int y );
extern void OnCycleFocusCheck( int x, int y );
extern void OnToggleFocusCheckSobel( int x, int y );
extern void OnExtMonDispMode( int x, int y );
extern void OnSaveFrame( int x, int y );
extern void OnGrabFrame( int x, int y );
extern void OnLoadFrame( int x, int y );
extern void OnFrameStore( int x, int y );
extern void OnProjectManager( int x, int y );
extern void OnABCamSetup( int x, int y );
extern void OnLoadLooksPanel( int x, int y );
extern void OnWhiteBalanceSetup( int x, int y );
extern void OnRecallFrame( int x, int y );
extern void OnRecallFrame50( int x, int y );
extern void CheckTemperature( bool force );
extern bool AddStereoFile( char *playbackPath, std::vector<char*> *SIVPlaybackPaths );
extern void OnUpdateConnectProgress();

extern int gDeltaBad;
extern int gDeltaTotal;
extern CBufferManager *gDualBufferManager[2];

int gIRIcounter;



void dummyOnClick(int x,int y)
{
}


void onPanN(int, int)
{
	Globals.SetPanV( Globals.GetPanV() - 1.0f );
	if ( Globals.GetPanV() < - ( Globals.GetZoomPanMaxV() ) )
		Globals.SetPanV(  - ( Globals.GetZoomPanMaxV() ) );
	InvalidateRect( Globals.hwnd, NULL, false );
}

void onPanS(int, int)
{
	Globals.SetPanV( Globals.GetPanV() + 1.0f );
	if ( Globals.GetPanV() > Globals.GetZoomPanMaxV() )
		Globals.SetPanV( Globals.GetZoomPanMaxV() );
	InvalidateRect( Globals.hwnd, NULL, false );
}

void onPanE(int, int)
{
	Globals.SetPanH( Globals.GetPanH() + 1.0f );
	if ( Globals.GetPanH() > Globals.GetZoomPanMaxH() )
		Globals.SetPanH( Globals.GetZoomPanMaxH() );
	InvalidateRect( Globals.hwnd, NULL, false );
}

void onPanW(int, int)
{
	Globals.SetPanH( Globals.GetPanH() - 1.0f );
	if ( Globals.GetPanH() < - ( Globals.GetZoomPanMaxH() ) )
		Globals.SetPanH(  - ( Globals.GetZoomPanMaxH() ) );
	InvalidateRect( Globals.hwnd, NULL, false );
}

// The following 4 event handlers add or subtract the parallax shift step size to/from the X and Y values
// The values are defined in prefs.xml as follows: X = 0; Y = 1; R = 2; step size = 4
// TODO: implementation for rotation value

void onXplus( int x, int y )
{
	float val = gPrefs.GetParallaxShift( PREFS_PARALLAX_X );

	if ( x > 1110 )
	{
		val += gPrefs.GetParallaxShift( PREFS_PARALLAX_S ) * 5;
	}
	else
	{
		val += gPrefs.GetParallaxShift( PREFS_PARALLAX_S );
	}
	gPrefs.SetParallaxShift( PREFS_PARALLAX_X, val );
	SignalStereo();
	InvalidateRect( Globals.hwnd, NULL, false );
}

void onXminus( int x, int y )
{
	float val = gPrefs.GetParallaxShift( PREFS_PARALLAX_X );

	if ( x < 165 )
	{
		val -= gPrefs.GetParallaxShift( PREFS_PARALLAX_S ) * 5;
	}
	else
	{
		val -= gPrefs.GetParallaxShift( PREFS_PARALLAX_S );
	}

	gPrefs.SetParallaxShift( PREFS_PARALLAX_X, val );
	SignalStereo();
	InvalidateRect( Globals.hwnd, NULL, false );
}

void onYplus( int x, int y )
{
	float val = gPrefs.GetParallaxShift( PREFS_PARALLAX_Y );

	if ( y < 120 )
	{
		val += gPrefs.GetParallaxShift( PREFS_PARALLAX_S ) * 5;
	}
	else
	{
		val += gPrefs.GetParallaxShift( PREFS_PARALLAX_S );
	}

	gPrefs.SetParallaxShift( PREFS_PARALLAX_Y, val );
	SignalStereo();
	InvalidateRect( Globals.hwnd, NULL, false );
}

void onYminus( int x, int y )
{
	float val = gPrefs.GetParallaxShift( PREFS_PARALLAX_Y );

	if ( y > 610 )
	{
		val -= gPrefs.GetParallaxShift( PREFS_PARALLAX_S ) * 5;
	}
	else
	{
		val -= gPrefs.GetParallaxShift( PREFS_PARALLAX_S );
	}

	gPrefs.SetParallaxShift( PREFS_PARALLAX_Y, val );
	SignalStereo();
	InvalidateRect( Globals.hwnd, NULL, false );
}

void on3DGuidesPlus( int, int )
{
	int guidesMode = gPrefs.Get3DAlignmentGuideMode();

	if ( guidesMode == GUIDES_3D_PRCT )
	{
		gUIScreen->Adjust3DGuidesPercentage( 0.5f );
	}
	else
	{
		gUIScreen->Adjust3DGuidesPercentage( 5.0f );
	}
}

void on3DGuidesMinus( int, int )
{
	int guidesMode = gPrefs.Get3DAlignmentGuideMode();
	if ( guidesMode == GUIDES_3D_PRCT )
	{
		gUIScreen->Adjust3DGuidesPercentage( - 0.5f );
	}
	else
	{
		gUIScreen->Adjust3DGuidesPercentage( - 5.0f );
	}
}



CUIScreen::CUIScreen()
{
	mLeftPanel = NULL;
	mMeterPanel = NULL;
	mBottomPanel = NULL;
	mCenterPanel = NULL;
	mFullPanel = NULL;
	mFSPlayPanel = NULL;
	mFSRecPanel = NULL;
	mCenterZoomPanel = NULL;
	mFullZoomPanel = NULL;
	mLeftPlayPanel = NULL;
	mBottomPlayPanel = NULL;
	mBottomPlayPanelFS = NULL;
	mLeftUtilityPanel = NULL;
	mRightUtilityPanel = NULL;
	mRightFramestorePanel = NULL;
	mRightProjectPanel = NULL;
	mRightCamSetupPanel = NULL;
	mRightPlayPanel = NULL;
	mWhiteBalancePanel = NULL;
	mBottomUtilityPanel = NULL;
	mRightLoadLookPanel = NULL;
	mParallaxShiftPanel= NULL;
	mCamAlignPanel = NULL;
	mHotspotList = NULL;

	mMode = SCREEN_MODE_PREVIEW;
	mVideoDisplayMode = VIDEO_DISPLAY_NORMAL;
	mMessageBox = NULL;
	mMessageBox2 = NULL;
	mProjectDialog = NULL;
	mSerialDialog = NULL;
	mAboutDialog = NULL;
	mBlackDialog = NULL;
	mFirmwareDialog = NULL;
	mExportDialog = NULL;
	mProgressDialog = NULL;
	mShutterDialog = NULL;
	mTimecodeDialog = NULL;
	mTimecode2Dialog = NULL;
	mModesDialog = NULL;
	mFileDialog = NULL;
	mMacroListDialog = NULL;
	mDisplayModesListDialog = NULL;
	mKeyboard = NULL;
	mPrefs = NULL;
	mIsRecording = false;
	mUtilityMode = false;
	mFrameStoreMode = false;
	mProjectManagerMode = false;
	mCamSetupMode = false;
	mWhiteBalanceMode = false;
	mLoadLookMode = false;
	mCamAlignMode = false;
	mAudioOverTicks = 0;
	mMetadataDialog = NULL;
	mSceneShotTakeDialog = NULL;
	mWhiteBalanceDialog = NULL;
	mSDIModesDialog = NULL;
	mUserWBDialog = NULL;
	mMetaDataCtrl = NULL;
	mDisplayPrefsDialog = NULL;
	mRepairDialog = NULL;
	mDefectivePixelmapDlg = NULL;
	mFindFileMatchDlg = NULL;
	mMultiCamModeDlg = NULL;
	mCamConnectionDlg = NULL;
	mParallaxShiftDlg = NULL;
	mHighBlackLevelDialog = NULL;
	mGuidesDialog = NULL;

	mRightPanelInfo.part_gb = 0.40f;
	mRightPanelInfo.total_gb = 1;
	mRightPanelInfo.part_audio = 0.80f;
	mRightPanelInfo.time = 0.0f;
	mRightPanelInfo.drive_full = false;
    mKeyboardController = KEYBOARD_CONTROLLER_NONE;

	InitAudioLut();
    for (int i = 0; i < MAX_LOGGER_LINES; i++)
        mOverlayText[i][0] = 0;
    mOverlayTextEndTimeMS = 0;
}

CUIScreen::~CUIScreen()
{
	if ( mLeftPanel )   delete mLeftPanel;
	if ( mMeterPanel )  delete mMeterPanel;
	if ( mBottomPanel ) delete mBottomPanel;
	if ( mCenterPanel ) delete mCenterPanel;
	if ( mFullPanel )   delete mFullPanel;
	if ( mFSPlayPanel )   delete mFSPlayPanel;
	if ( mFSRecPanel )   delete mFSRecPanel;
	if ( mCenterZoomPanel ) delete mCenterZoomPanel;
	if ( mFullZoomPanel )   delete mFullZoomPanel;

	if ( mLeftPlayPanel )   delete mLeftPlayPanel;
	if ( mBottomPlayPanel )   delete mBottomPlayPanel;
	if ( mBottomPlayPanelFS )   delete mBottomPlayPanelFS;
	if ( mLeftUtilityPanel )   delete mLeftUtilityPanel;
	if ( mRightUtilityPanel )   delete mRightUtilityPanel;
	if ( mBottomUtilityPanel ) delete mBottomUtilityPanel;
	if ( mRightFramestorePanel ) delete mRightFramestorePanel;
	if ( mRightProjectPanel ) delete mRightProjectPanel;
	if ( mRightCamSetupPanel ) delete mRightCamSetupPanel;
	if ( mRightPlayPanel )   delete mRightPlayPanel;
	if ( mWhiteBalancePanel ) delete mWhiteBalancePanel;
	if ( mRightLoadLookPanel ) delete mRightLoadLookPanel;
	if ( mParallaxShiftPanel ) delete mParallaxShiftPanel;
	if ( mCamAlignPanel ) delete mCamAlignPanel;
}

int linearToHeight( int v, int *table )
{
	int index = 0;

	while ( table[index+1] > v )
		index++;

	int a = table[index];
	int b = table[index+1];

	float ay = pixelY[index];
	float by = pixelY[index+1];

	float delta = float( v - a ) / float(b-a);

	return (int) ((by - ay) * delta + ay + 0.5f);
}


void CUIScreen::InitAudioLut( void )
{
	int linear[MOD_TABLE_ENTRIES];

	for ( int i = 0; i < MOD_TABLE_ENTRIES; i++ )
	{
		linear[i] = (int) ( pct[i] * 32767.0f / 100.0f + 0.5 );
	}
	for ( int i = 0; i < 32768; i++ )
	{
		mAudioHeight[i] = linearToHeight( i, linear );
	}
}


void CUIScreen::InitLeftPanel( void )
{
	mLeftPanel = new CUIPanel(0,0,120,656,"LeftPanel");

	float x = 3;
	float y = 65;
	float w = 109;
	float h = 94;
	float gap = 5;

	mPlayModeButton = new CUITextButton( x, y, w, h, -1, OnPlayModeClick );
	mPlayModeButton->SetText( "PLAY", "MODE" );
	mPlayModeButton->SetRGBA( 180, 180, 0, 128 );
	mLeftPanel->AddButton( mPlayModeButton );

	y += ( h + gap );
	mUtilityButton = new CUITextButton( x, y, w, h, -1, OnEnterUtilityMenu );
	mUtilityButton->SetText( "UTILITY", "MENU" );
	mUtilityButton->SetRGBA( 180, 180, 0, 128 );
	mLeftPanel->AddButton( mUtilityButton );

	y += ( h + gap );
	mFrameStore = new CUITextButton( x, y, w, h, -1, OnFrameStore );
	mFrameStore->SetText( "FRAME", "STORE" );
	mFrameStore->SetActive( true );
	mLeftPanel->AddButton( mFrameStore );

	y += ( h + gap );
	mProjectManager = new CUITextButton( x, y, w, h, -1, OnProjectManager );
	mProjectManager->SetText( "PROJ", "MANAGR" );
	mProjectManager->SetActive( true );
	mLeftPanel->AddButton( mProjectManager );

	y += ( h + gap );
	mABCamSetup = new CUITextButton( x, y, w, h, -1, OnABCamSetup );
	mABCamSetup->SetText( "CAMERA", "SETUP" );
	mLeftPanel->AddButton( mABCamSetup );

	if ( ! gDeviceManager.GetDevice( 0 ) )
	{
		mABCamSetup->SetActive( false );
	}

	y += ( h + gap );
	mLoadLooks = new CUITextButton( x, y, w, h, -1, OnLoadLooksPanel );
	mLoadLooks->SetText( "SET", "LOOK" );
	mLeftPanel->AddButton( mLoadLooks );
}

void CUIScreen::InitLeftPlayPanel( void )
{
	mLeftPlayPanel = new CUIPanel( 0, 0, 120, 700, "LeftPlayPanel");

	float x = 3;
	float y = 65;
	float w = 109;
	float h = 94;
	float gap = 5;

	mExitPlayModeButton = new CUITextButton( x, y, w , h, -1, OnPlayModeClick );
	mExitPlayModeButton->SetText( "EXIT", "PLAY", "MODE" );
	mExitPlayModeButton->SetRGBA( 180, 180, 0, 128);
	mLeftPlayPanel->AddButton( mExitPlayModeButton );

	y += ( h + gap );
	mFrameStorePlay = new CUITextButton( x, y, w, h, -1, OnFrameStore );
	mFrameStorePlay->SetText( "FRAME", "STORE" );
	mFrameStorePlay->SetActive( true );
	mLeftPlayPanel->AddButton( mFrameStorePlay );

	y += ( h + gap );
	mClipInfoEditor = new CUITextButton( x, y, w, h, -1, OnLoadClipInfoEditor );
	mClipInfoEditor->SetText( "EDIT", "CLIP", "INFO" );
	mClipInfoEditor->SetActive( true );
	mLeftPlayPanel->AddButton( mClipInfoEditor );

	y += ( h + gap );
	mExportClip = new CUITextButton( x, y, w, h, -1, OnExportClip );
	mExportClip->SetText( "EXPORT", "CLIP" );
	mExportClip->SetActive( true );
	mLeftPlayPanel->AddButton( mExportClip );

	y += ( h + gap );
	mColorCorrect = new CUITextButton( x, y, w, h, -1, OnLoadLooksPanel );
	mColorCorrect->SetText( "SET", "LOOK" );
	mLeftPlayPanel->AddButton( mColorCorrect );

	y += ( h + gap );
	CUITextButton *tbutton = new CUITextButton( x, y, w, h, -1, OnClickPlayFile );
	tbutton->SetText( "LOAD", "CLIP" );
	mLeftPlayPanel->AddButton( tbutton );
}


void CUIScreen::InitLeftUtilityPanel( void )
{
	mLeftUtilityPanel = new CUIPanel(0,0,120,656,"LeftUtilityPanel");

	float x = 3;
	float y = 65;
	float w = 109;
	float h = 94;
	float gap = 5;

	CUITextButton *tbutton = new CUITextButton( x, y, w, h, -1, OnExitUtilityMenu );
	tbutton->SetText( "EXIT", "UTILITY", "MENU" );
	tbutton->SetRGBA( 180, 180, 0, 128 );
	mLeftUtilityPanel->AddButton( tbutton );

	y += ( h + gap );
	tbutton = new CUITextButton( x, y, w, h, -1, OnSendSerial );
	tbutton->SetText( "SERIAL", "COM" );
	mLeftUtilityPanel->AddButton( tbutton );

	y += ( h + gap );
	tbutton = new CUITextButton( x, y, w, h, -1, OnUpdateFirmware );
	tbutton->SetText( "LOAD", "FIRMWR" );
	mLeftUtilityPanel->AddButton( tbutton );

	y += ( h + gap );
	tbutton = new CUITextButton( x, y, w, h, -1, OnAboutBox );
	tbutton->SetText( "CAM", "INFO" );
	mLeftUtilityPanel->AddButton( tbutton );

	y += ( h + gap );
	tbutton = new CUITextButton( x, y, w, h, -1, OnToggleColorBars );
	tbutton->SetText( "COLOR", "BARS" );
	mLeftUtilityPanel->AddButton( tbutton );

	y += (h + gap);
	tbutton = new CUITextButton( x, y, w, h, -1, OnDefectivePixelMap );
	tbutton->SetText( "DEFECT", "PIX MAP" );
	mLeftUtilityPanel->AddButton( tbutton );

}


void CUIScreen::InitRightUtilityPanel( void )
{
	mRightUtilityPanel = new CUIPanel( 1160, 0, 120, 660, "RightUtilityPanel" );

	float x = 1169;
	float y = 362;
	float w = 109;
	float h = 94;
	float gap = 5;

	CUITextButton *tbutton = new CUITextButton( x, y, w, h, -1, OnLoadDispModes );
	tbutton->SetText( "SET", "DISPLAY", "MODES" );
	mRightUtilityPanel->AddButton( tbutton );

	y += ( h + gap );
	tbutton = new CUITextButton( x, y, w, h, -1, OnReset );
	tbutton->SetText( "RESET" );
	mRightUtilityPanel->AddButton( tbutton );

	y += ( h + gap );
	mQuitButton = new CUITextButton( x, y, w, h, -1, OnQuit );
	mQuitButton->SetRGBA( 180, 0, 0, 255 );
	mQuitButton->SetText( "QUIT", NULL );
	mRightUtilityPanel->AddButton( mQuitButton );
}


void CUIScreen::InitRightFramestorePanel( void )
{
	mRightFramestorePanel = new CUIPanel( 1160, 0, 110, 600, "RightFramestorePanel" );

	float x = 1169;
	float y = 110;
	float w = 109;
	float h = 94;
	float gap = 5;

	mSaveButton = new CUITextButton( x, y, w, h, -1, OnSaveFrame );
	mSaveButton->SetRGBA( 180, 180, 0, 128 );
	mSaveButton->SetText( "SAVE", "IMAGE" );
	mRightFramestorePanel->AddButton( mSaveButton );

	y += ( h + gap );
	mLoadButton = new CUITextButton( x, y, w, h, -1, OnLoadFrame );
	mLoadButton->SetRGBA( 180, 180, 0, 128 );
	mLoadButton->SetText( "LOAD", "IMAGE" );
	mRightFramestorePanel->AddButton( mLoadButton );

	y += ( h + gap );
	mGrabButton = new CUITextButton( x, y, w, h, -1, OnGrabFrame );
	mGrabButton->SetRGBA( 180, 180, 0, 128 );
	mGrabButton->SetText( "GRAB", "FRAME" );
	mRightFramestorePanel->AddButton( mGrabButton );

	y += ( h + gap );
	mRecall100Button = new CUITextButton( x, y, w, h, -1, OnRecallFrame );
	mRecall100Button->SetRGBA( 180, 180, 0, 128 );
	mRecall100Button->SetText( "RECALL", "100%" );
	mRightFramestorePanel->AddButton( mRecall100Button );

	y += ( h + gap );
	mRecall50Button = new CUITextButton( x, y, w, h, -1, OnRecallFrame50 );
	mRecall50Button->SetRGBA( 180, 180, 0, 128 );
	mRecall50Button->SetText( "RECALL", "50%" );
	mRightFramestorePanel->AddButton( mRecall50Button );
}

void CUIScreen::InitRightProjectPanel( void )
{
	mRightProjectPanel = new CUIPanel( 1160, 0, 155, 600, "RightProjectPanel" );

	float x = 1169;
	float y = 110;
	float w = 109;
	float h = 94;
	float gap = 5;

	mOpenProject = new CUITextButton( x, y, w, h, -1, OnOpenProject );
	mOpenProject->SetRGBA( 180, 180, 0, 128 );
	mOpenProject->SetText( "OPEN", "PROJ" );
	mOpenProject->SetActive( true );
	mRightProjectPanel->AddButton( mOpenProject );

	y += ( h + gap );
	mNewProject = new CUITextButton( x, y, w, h, -1, OnNewProject );
	mNewProject->SetRGBA( 180, 180, 0, 128 );
	mNewProject->SetText( "NEW", "PROJ" );
	mNewProject->SetActive( true );
	mRightProjectPanel->AddButton( mNewProject );

	y += ( h + gap );
	mMetadata = new CUITextButton( x, y, w, h, -1, OnMetadata );
	mMetadata->SetRGBA( 180, 180, 0, 128 );
	mMetadata->SetText( "PROJ", "DATA" );
	mMetadata->SetActive( true );
	mRightProjectPanel->AddButton( mMetadata );

	y += ( h + gap );
	mSceneShotTake = new CUITextButton( x, y, w, h, -1, OnSceneShotTake );
	mSceneShotTake->SetRGBA( 180, 180, 0, 128 );
	mSceneShotTake->SetText( "SCENE/", "SHOT/", "TAKE" );
	mSceneShotTake->SetActive( true );
	mRightProjectPanel->AddButton( mSceneShotTake );

	y += ( h + gap );
	mSlatePreview = new CUITextButton( x, y, w, h, -1, OnSlatePreview );
	mSlatePreview->SetRGBA( 180, 180, 0, 128 );
	mSlatePreview->SetText( "SHOW", "SLATE" );

	// Make sure the slate preview can only be selected if we have at least one camera connected
	extern CDevice *gDevice[];
	if ( gDeviceManager.GetDevice( 0 ) )
	{
		mSlatePreview->SetActive( true );
	}
	else
	{
		mSlatePreview->SetActive( false );
	}

	mRightProjectPanel->AddButton( mSlatePreview );
}

void CUIScreen::InitRightCamSetupPanel( void )
{
	float x = 1169;
	float y = 110;
	float w = 109;
	float h = 94;
	float gap = 5;
	if ( gPrefs.GetSdiMode() )
	{
		mRightCamSetupPanel = new CUIPanel( 1160, 0, 110, 680, "RightCamSetupPanel" );
		x = 1169;
		y = 65;
		w = 109;
		h = 94;
		gap = 5;
	}
	else
	{
		mRightCamSetupPanel = new CUIPanel( 1160, 0, 155, 600, "RightCamSetupPanel" );
	}


	CUITextButton *tbutton = new CUITextButton( x, y, w, h, -1, OnCamSetupMode );
	tbutton->SetRGBA( 180, 180, 0, 128 );
	tbutton->SetText( "CAM", "CONN" );
	mRightCamSetupPanel->AddButton( tbutton );

	y += ( h + gap );
	mFlipCamA = new CUITextButton( x, y, w, h, -1, OnFlipCamA );
	mFlipCamA->SetRGBA( 180, 180, 0, 128 );

	if ( Globals.GetDraw3D() )
	{
		mFlipCamA->SetText( "FLIP", "CAM L" );
	}
	else
	{
		mFlipCamA->SetText( "FLIP", "CAM" );
	}

	mRightCamSetupPanel->AddButton( mFlipCamA );

	if ( ! gDeviceManager.GetDevice( 0 ) )
	{
		mFlipCamA->SetActive( false );
	}

	y += ( h + gap );
	mFlipCamB = new CUITextButton( x, y, w, h, -1, OnFlipCamB );
	mFlipCamB->SetRGBA( 180, 180, 0, 128 );
	mFlipCamB->SetText( "FLIP", "CAM R" );
	mRightCamSetupPanel->AddButton( mFlipCamB );

	// If we are in 3D mode, enable FLIP CAM R button
	if ( Globals.GetDraw3D() )
	{
		mFlipCamB->SetVisible( true );
	}
	else
	{
		mFlipCamB->SetVisible( false );
	}

	y += ( h + gap );
	mCamAlignModeBtn = new CUITextButton( x, y, w, h, -1, On3DAlignmentSettings );
	mCamAlignModeBtn->SetRGBA( 180, 180, 0, 128 );
	mCamAlignModeBtn->SetText( "CAM", "ALIGN", "MODE" );

	// If 3D guides are checked in DISPLAY PREFS, and we are in 3D mode, enable CAM ALIGN button
	if ( ( gPrefs.Get3DAlignmentGuideMode() ) && ( Globals.GetDraw3D() ) )
	{
		mCamAlignModeBtn->SetVisible( true );
	}
	else
	{
		mCamAlignModeBtn->SetVisible( false );
	}

	mRightCamSetupPanel->AddButton( mCamAlignModeBtn );

	y += ( h + gap );
	mParallaxShift = new CUITextButton( x, y, w, h, -1, OnParallaxShift );
	mParallaxShift->SetRGBA( 180, 180, 0, 128 );
	mParallaxShift->SetText( "PARALX", "SHIFT" );
	mRightCamSetupPanel->AddButton( mParallaxShift );

	// If we are in 3D mode, enable PARALX SHIFT button
	if ( Globals.GetDraw3D() )
	{
		mParallaxShift->SetVisible( true );
	}
	else
	{
		mParallaxShift->SetVisible( false );
	}

	if ( gPrefs.GetSdiMode() )
	{
		y += ( h + gap );
		mSDIModes = new CUITextButton( x, y, w, h, -1, OnSDIModes );
		mSDIModes->SetRGBA( 180, 180, 0, 128 );
		mSDIModes->SetText( "SDI", "MODES" );
		mRightCamSetupPanel->AddButton( mSDIModes );
	}
}

void CUIScreen::InitRightLoadLookPanel()
{
	mRightLoadLookPanel = new CUIPanel( 1160, 0, 110, 700, "RightLoadLookPanel" );

	float x = 1169;
	float y = 65;
	float w = 109;
	float h = 94;
	float gap = 5;
	
	mLookAsShot = new CUITextButton( x, y, w, h, -1, OnLoadAsShot );
	mLookAsShot->SetRGBA( 180, 180, 0, 128 );
	mLookAsShot->SetText( "AS", "SHOT" );
	mRightLoadLookPanel->AddButton( mLookAsShot );
	y += ( h + gap );
	mLoadLookBtnA = new CUITextButton( x, y, w, h, -1, OnLoadLookA );
	mLoadLookBtnA->SetRGBA( 180, 180, 0, 128 );
	mLoadLookBtnA->SetText( "LOOK A" );
	mRightLoadLookPanel->AddButton( mLoadLookBtnA );

	y += ( h + gap );
	mLoadLookBtnB = new CUITextButton( x, y, w, h, -1, OnLoadLookB );
	mLoadLookBtnB->SetRGBA( 180, 180, 0, 128 );
	mLoadLookBtnB->SetText( "LOOK B" );
	mRightLoadLookPanel->AddButton( mLoadLookBtnB );

	y += ( h + gap );
	mLoadLookBtnC = new CUITextButton( x, y, w, h, -1, OnLoadLookC );
	mLoadLookBtnC->SetRGBA( 180, 180, 0, 128 );
	mLoadLookBtnC->SetText( "LOOK C" );
	mRightLoadLookPanel->AddButton( mLoadLookBtnC );

	y += ( h + gap );
	mLoadLookBtnD = new CUITextButton( x, y, w, h, -1, OnLoadLookD );
	mLoadLookBtnD->SetRGBA( 180, 180, 0, 128 );
	mLoadLookBtnD->SetText( "LOOK D" );
	mRightLoadLookPanel->AddButton( mLoadLookBtnD );

	y += ( h + gap );
	mLoadLookDlg = new CUITextButton( x, y, w, h, -1, OnLoadLooks );
	mLoadLookDlg->SetRGBA( 180, 180, 0, 128 );
	mLoadLookDlg->SetText( "MODIFY", "LOOKS" );
	mRightLoadLookPanel->AddButton( mLoadLookDlg );

	switch ( gPrefs.GetRecordLook() )
	{
		case 0:
			mLoadLookBtnA->SetHighlight( true );
			break;
		case 1:
			mLoadLookBtnB->SetHighlight( true );
			break;
		case 2:
		    mLoadLookBtnC->SetHighlight( true );
			break;
		case 3:
			mLoadLookBtnD->SetHighlight( true );
			break;
		default:
			break;
	}
}


void CUIScreen::InitRightPlayPanel()
{
	mRightPlayPanel = new CUIPanel( 1160, 0, 120, 656, "RightPlayPanel" );

	float x = 1169;
	float y = 65;
	float w = 109;
	float h = 94;
	float gap = 5;

	y += 79 * 5;
	CUITextButton *tbutton = new CUITextButton( x, y, w, h, -1, OnRepairFile );
	tbutton->SetText( "REPAIR", "FILE" );
	mRightPlayPanel->AddButton( tbutton );

	y += ( h + gap );

	tbutton = new CUITextButton( x, y, w, h, -1, OnDeleteClip );
	tbutton->SetText( "DELETE", "CLIP" );
	tbutton->SetRGBA( 180, 0, 0, 128 );
	mRightPlayPanel->AddButton( tbutton );
}


void CUIScreen::InitWhiteBalancePanel( void )
{
	mWhiteBalancePanel = new CUIPanel( 1160, 0, 110, 680, "WhiteBalancePanel" );

	float x = 1169;
	float y = 65;
	float w = 109;
	float h = 94;
	float gap = 5;

	mWhiteBalance3200 = new CUITextButton( x, y, w, h, -1, OnSetWB3200 );
	mWhiteBalance3200->SetRGBA( 180, 180, 0, 128 );
	mWhiteBalance3200->SetText( "3200K" );
	mWhiteBalancePanel->AddButton( mWhiteBalance3200 );

	y += ( h + gap );
	mWhiteBalance5600 = new CUITextButton( x, y, w, h, -1, OnSetWB5600 );
	mWhiteBalance5600->SetRGBA( 180, 180, 0, 128 );
	mWhiteBalance5600->SetText( "5600K" );
	mWhiteBalancePanel->AddButton( mWhiteBalance5600 );

	y += ( h + gap );
	mWhiteBalanceUser1 = new CUITextButton( x, y, w, h, -1, OnSetWBUser1 );
	mWhiteBalanceUser1->SetRGBA( 180, 180, 0, 128 );
	mWhiteBalanceUser1->SetText( "USER-1", "PRESET" );
	mWhiteBalancePanel->AddButton( mWhiteBalanceUser1 );

	y += ( h + gap );
	mWhiteBalanceUser2 = new CUITextButton( x, y, w, h, -1, OnSetWBUser2 );
	mWhiteBalanceUser2->SetRGBA( 180, 180, 0, 128 );
	mWhiteBalanceUser2->SetText( "USER-2", "PRESET" );
	mWhiteBalancePanel->AddButton( mWhiteBalanceUser2 );

	y += ( h + gap );
	mWhiteBalanceSet = new CUITextButton( x, y, w, h, -1, OnSetWhite );
	mWhiteBalanceSet->SetRGBA( 180, 180, 0, 128 );
	mWhiteBalanceSet->SetText( "SET", "WHITE" );
	mWhiteBalancePanel->AddButton( mWhiteBalanceSet );

	y += ( h + gap );
	mWhiteBalanceAdjust = new CUITextButton( x, y, w, h, -1, OnWhiteBalanceAdjust );
	mWhiteBalanceAdjust->SetRGBA( 180, 180, 0, 128 );
	mWhiteBalanceAdjust->SetText( "USER", "ADJUST" );
	
	if ( ( gPrefs.GetWhiteBalance() == PREFS_WB_3200K ) 
	  || ( gPrefs.GetWhiteBalance() == PREFS_WB_5600K )
	  || ( gPrefs.GetWhiteBalance() == PREFS_WB_NONE ) )
	{
		mWhiteBalanceAdjust->SetActive( false );
	}
	else
	{
		mWhiteBalanceAdjust->SetActive( true );
	}

	if ( gPrefs.GetWhiteBalance() == PREFS_WB_NONE )
	{
		mWhiteBalanceUser1->SetActive( false );
		mWhiteBalanceUser2->SetActive( false );
	}

	mWhiteBalancePanel->AddButton( mWhiteBalanceAdjust );

	switch ( gPrefs.GetWhiteBalance() )
	{
		case PREFS_WB_3200K:
			mWhiteBalance3200->SetHighlight( true );
			break;
		case PREFS_WB_5600K:
			mWhiteBalance5600->SetHighlight( true );
			break;
		case PREFS_WB_USER_1:
			mWhiteBalanceUser1->SetHighlight( true );
			break;
		case PREFS_WB_USER_2:
			mWhiteBalanceUser2->SetHighlight( true );
			break;
	}
}

void CUIScreen::InitMeterPanel( void )
{
	int x = 1175;
	int yInc = 100;
	int yDec = 295;
	int ySwitch = 420;
	int w = 90;
	int h1 = 120;
	int h2 = 240;

	mMeterPanel = new CUIPanel( 1160, 0, 320, 656, "MeterPanel" );

	mAudioInputInc = new CUIHotSpotButton( x, yInc, w, h1, -1 , OnIncAudioInputLevel );
	mAudioInputInc->SetRenderable(false);
	mMeterPanel->AddButton( mAudioInputInc );

	mAudioInputDec = new CUIHotSpotButton( x, yDec, w, h1, -1 , OnDecAudioInputLevel );
	mAudioInputDec->SetRenderable(false);
	mMeterPanel->AddButton( mAudioInputDec );

	mDiskTimeMeterSwitch = new CUIHotSpotButton( x, ySwitch, w, h2, -1 , OnSwitchDiskTimeMeter );
	mDiskTimeMeterSwitch->SetRenderable(false);
	mMeterPanel->AddButton( mDiskTimeMeterSwitch );
}

void CUIScreen::InitCenterPanel( void )
{
	mCenterPanel = new CUIPanel( UI_IMAGE_X, UI_IMAGE_Y_STATUSBAR, UI_IMAGE_W, UI_IMAGE_H + UI_IMAGE_Y_GAP, "CenterPanel" );

	float x = UI_IMAGE_X;
	float y = UI_IMAGE_Y;
	float w = UI_IMAGE_W;
	float h = UI_IMAGE_H;
	float part = 0.5f;

	float w1 = 290;
	float w2 = 460;
	float h1 = 164;
	float h2 = 257;

	CUIImageButton *ibutton = new CUIImageButton( x, y, w, h, -1, NULL );
	ibutton->SetRGBA( 68, 68, 68, 255 );
	mCenterPanel->AddButton( ibutton );


	x = UI_IMAGE_X + w1;
	y = UI_IMAGE_Y_STATUSBAR;
	CUIHotSpotButton *hbutton = new CUIHotSpotButton( x, y, w2, 60, -1, OnToggleStatusbarMode );
	hbutton->SetRenderable(false);
	mCenterPanel->AddButton( hbutton );

	x = UI_IMAGE_X;
	y = UI_IMAGE_Y;
	hbutton = new CUIHotSpotButton( x, 
									y, 
									w1 + ( (float) 2 * Globals.mMiniGuiMag ), 
									h1 + ( (float) 2 * Globals.mMiniGuiMag ), 
									-1, 
									OnExposureMeterClick );
	hbutton->SetRGBA( 80, 80, 0, 28 );
	hbutton->SetText( "FALSE", "COLOR", "EXPOSURE" );
	mCenterPanel->AddButton( hbutton );

	y = UI_IMAGE_Y + h1 + h2;
	hbutton = new CUIHotSpotButton( x, 
									y, 
									w1 + ( (float) 2 * Globals.mMiniGuiMag ), 
									h1, 
									-1, 
									OnSceneShotTake );
	hbutton->SetRGBA( 80, 80, 0, 28 );
	hbutton->SetText( "SCENE", "SHOT", "TAKE" );
	mCenterPanel->AddButton( hbutton );

	y = UI_IMAGE_Y + h1;
    if ( gD3D->IsUsingExternalMonitor() && Globals.si2k() ) 
    	hbutton = new CUIHotSpotButton( x, 
									y, 
									w1 + ( (float) 2 * Globals.mMiniGuiMag ), 
									h2 * part + ( (float) 2 * Globals.mMiniGuiMag ), 
									-1, 
									OnToggleFocusCheckSobel );
    else
    	hbutton = new CUIHotSpotButton( x, 
									y, 
									w1 + ( (float) 2 * Globals.mMiniGuiMag ), 
									h2 * part + ( (float) 2 * Globals.mMiniGuiMag ), 
									-1, 
									OnToggleSobel );
	hbutton->SetRGBA( 80, 80, 0, 28 );
	hbutton->SetText( "EDGE", "DETECT" );
	mCenterPanel->AddButton( hbutton );


    hbutton = new CUIHotSpotButton( x, 
									y + h2 * part, 
									w1 + ( (float) 2 * Globals.mMiniGuiMag ), 
									h2 * ( 1.0f - part ) + ( (float) 2 * Globals.mMiniGuiMag ), 
									-1, 
									OnCycleFocusCheck );
	hbutton->SetRGBA( 80, 80, 0, 28 );
	hbutton->SetText( "FOCUS", "CHECK" );
	mCenterPanel->AddButton( hbutton );

	x = UI_IMAGE_X + w1 + w2;
	y = UI_IMAGE_Y;
	hbutton = new CUIHotSpotButton( x, 
									y, 
									w1, 
									h1 + ( (float) 2 * Globals.mMiniGuiMag ), 
									-1, 
									OnFullScreenClick );
	hbutton->SetRGBA( 80, 80, 0, 28 );
	hbutton->SetText( "FULL", "SCREEN" );
	mCenterPanel->AddButton( hbutton );

	x = UI_IMAGE_X + w1 + w2;
	y = UI_IMAGE_Y + h1;
	hbutton = new CUIHotSpotButton( x, 
									y, 
									w1, 
									h2 * part + ( (float) 2 * Globals.mMiniGuiMag ), 
									-1, 
									OnToggleLut3D );
	hbutton->SetRGBA( 80, 80, 0, 28 );
	hbutton->SetText( "TOGGLE", "3D", "LUT" );
	mCenterPanel->AddButton( hbutton );

	hbutton = new CUIHotSpotButton( x, 
									y + h2 * part, 
									w1, 
									h2 * ( 1.0f - part ) + ( (float) 2 * Globals.mMiniGuiMag ), 
									-1, 
									OnExtMonDispMode );
	hbutton->SetRGBA( 80, 80, 0, 28 );
	hbutton->SetText( "EXT", "MON", "VIEW" );
	mCenterPanel->AddButton( hbutton );

	x = UI_IMAGE_X + w1 + w2;
	y = UI_IMAGE_Y + h1 + h2;
	hbutton = new CUIHotSpotButton( x, y, w1, h1, -1, OnGuidesClick );
	hbutton->SetRGBA( 80, 80, 0, 28 );
	hbutton->SetText( "GUIDES" );
	mCenterPanel->AddButton( hbutton );

   
	x = UI_IMAGE_X + w1;
	y = UI_IMAGE_Y;
   
	hbutton = new CUIHotSpotButton( x, 
									y, 
									w2 * part + ( (float) 2 * Globals.mMiniGuiMag ), 
									h1 + ( (float) 2 * Globals.mMiniGuiMag ), 
									-1 , 
									OnIncLeftRightMode );
	hbutton->SetRGBA( 80, 80, 0, 28 );
	hbutton->SetText( "SLATE", "PREVIEW" );
	hbutton->Set3DText( "L-R" );
	mCenterPanel->AddButton( hbutton );

	hbutton = new CUIHotSpotButton( x + 
									w2 * part, 
									y, 
									w2 * ( 1.0f - part ) + ( (float) 2 * Globals.mMiniGuiMag ), 
									h1 + ( (float) 2 * Globals.mMiniGuiMag ), 
									-1, 
									OnIncDisplay3DMode );
	hbutton->SetRGBA( 80, 80, 0, 28 );
	hbutton->SetText( "COLOR", "CORRECTION" );
	hbutton->Set3DText( "50-50", "ANAGLYPH", "WIGGLE" );
	mCenterPanel->AddButton( hbutton );
	
	x = UI_IMAGE_X + w1;
	y = UI_IMAGE_Y + h1;
	hbutton = new CUIHotSpotButton( x, 
									y, 
									w2 + ( (float) 2 * Globals.mMiniGuiMag ), 
									h2 + ( (float) 2 * Globals.mMiniGuiMag ), 
									-1, 
									OnZoomClick );
	hbutton->SetRGBA( 80, 80, 0, 28 );
	hbutton->SetText( "ZOOM" );
	mCenterPanel->AddButton( hbutton );

	y = UI_IMAGE_Y + h1 + h2;
	hbutton = new CUIHotSpotButton( x, 
									y, 
									w2 + ( (float) 2 * Globals.mMiniGuiMag ), 
									h1, 
									-1, 
									OnToggleHistogram );
	hbutton->SetRGBA( 80, 80, 0, 28 );
	hbutton->SetText( "HISTOGRAM" );
	mCenterPanel->AddButton( hbutton );
	mHotspotList = hbutton;
}

void CUIScreen::InitFullPanel( void )
{
	float x = gConfig.mAppFullX;
	float y = gConfig.mAppFullY;
	float w = gConfig.mAppFullW;
	float h = gConfig.mAppFullH + gConfig.mAppFullY * 2;
	float part = 0.5f;

	float w1 = ( gConfig.mAppFullW * 28 / 100 );
	float w2 = ( gConfig.mAppFullW * 44 / 100 );
	float w3 = 100.0f;
	float h1 = ( gConfig.mAppFullH * 28 / 100 );
	float h2 = ( gConfig.mAppFullH * 44 / 100 );
	float h3 = 42 * Globals.mMiniGuiMin;

	mFullPanel = new CUIPanel( x, y, w, h + h3, "FullPanel", true );

	CUIImageButton *ibutton = new CUIImageButton( x, y, w, gConfig.mAppFullH, -1, NULL, true );
	ibutton->SetRGBA( 68, 68, 68, 255 );
	mFullPanel->AddButton( ibutton );

	x = gConfig.mAppFullX;
	y = gConfig.mAppFullY;
	CUIHotSpotButton *hbutton = new CUIHotSpotButton( x, y, w1 + 2, h1 + 2, -1, OnExposureMeterClick, true );
	hbutton->SetRGBA( 80, 80, 0, 28 );
	hbutton->SetText( "FALSE", "COLOR", "EXPOSURE" );
	mFullPanel->AddButton( hbutton );

	y = h1 + (float) gConfig.mAppFullY;
    if ( gD3D->IsUsingExternalMonitor() && Globals.si2k() ) 
    	hbutton = new CUIHotSpotButton( x, y, w1 + 2, h2 * part + 2, -1, OnToggleFocusCheckSobel, true );
    else
    	hbutton = new CUIHotSpotButton( x, y, w1 + 2, h2 * part + 2, -1, OnToggleSobel, true );
	hbutton->SetRGBA( 80, 80, 0, 28 );
	hbutton->SetText( "EDGE", "DETECT" );
	mFullPanel->AddButton( hbutton );

	hbutton = new CUIHotSpotButton( x, y + h2 * part, w1 + 2, h2 * ( 1.0f - part ) + 2, -1, OnCycleFocusCheck, true );
	hbutton->SetRGBA( 80, 80, 0, 28 );
	hbutton->SetText( "FOCUS", "CHECK" );
	mFullPanel->AddButton( hbutton );

	y = h1 + h2 + gConfig.mAppFullY;
	hbutton = new CUIHotSpotButton( x, y, w1 + 2, h1, -1, OnSceneShotTake, true );
	hbutton->SetRGBA( 80, 80, 0, 28 );
	hbutton->SetText( "SCENE", "SHOT", "TAKE" );
	mFullPanel->AddButton( hbutton );

	x = w1 + w2 + gConfig.mAppFullX;
	y = gConfig.mAppFullY;
	hbutton = new CUIHotSpotButton( x, y, w1, h1 + 2, -1, OnFullScreenClick, true );
	hbutton->SetRGBA( 80, 80, 0, 28 );
	hbutton->SetText( "FULL", "SCREEN" );
	mFullPanel->AddButton( hbutton );

	x = w1 + w2 + gConfig.mAppFullX;
	y = h1 + gConfig.mAppFullY;
	hbutton = new CUIHotSpotButton( x, y, w1, h2 * part + 2, -1, OnToggleLut3D, true );
	hbutton->SetRGBA( 80, 80, 0, 28 );
	hbutton->SetText( "TOGGLE", "3D", "LUT" );
	mFullPanel->AddButton( hbutton );

	hbutton = new CUIHotSpotButton( x, y + h2 * part, w1, h2 * ( 1.0f - part ) + 2, -1, OnExtMonDispMode, true );
	hbutton->SetRGBA( 80, 80, 0, 28 );
	hbutton->SetText( "EXT", "MON", "VIEW" );
	mFullPanel->AddButton( hbutton );


	x = w1 + w2 + gConfig.mAppFullX;
	y = h1 + h2 + gConfig.mAppFullY;
	hbutton = new CUIHotSpotButton( x, y, w1, h1, -1, OnGuidesClick, true );
	hbutton->SetRGBA( 80, 80, 0, 28 );
	hbutton->SetText( "GUIDES" );
	mFullPanel->AddButton( hbutton );

	x = w1 + gConfig.mAppFullX;
	y = gConfig.mAppFullY;
	
	hbutton = new CUIHotSpotButton( x, y, w2 * part + 2, h1 + 2, -1 , OnIncLeftRightMode, true );
	hbutton->SetRGBA( 80, 80, 0, 28 );
	hbutton->SetText( "SLATE", "PREVIEW" );
	hbutton->Set3DText( "L-R" );
	mFullPanel->AddButton( hbutton );


	hbutton = new CUIHotSpotButton( x + w2 * part, y, w2 * ( 1.0f - part ) + 2, h1 + 2, -1, OnIncDisplay3DMode, true );
	hbutton->SetRGBA( 80, 80, 0, 28 );
	hbutton->SetText( "COLOR", "CORRECTION" );
	hbutton->Set3DText( "50-50", "ANAGLYPH", "WIGGLE" );
	mFullPanel->AddButton( hbutton );
	
    x = w1 + gConfig.mAppFullX;
	y = h1 + gConfig.mAppFullY;
	hbutton = new CUIHotSpotButton( x, y, w2 + 2, h2 + 2, -1, OnZoomClick, true );
	hbutton->SetRGBA( 80, 80, 0, 28 );
	hbutton->SetText( "ZOOM" );
	mFullPanel->AddButton( hbutton );

	y = h1 + h2 + gConfig.mAppFullY;
	hbutton = new CUIHotSpotButton( x, y, w2 + 2, h1, -1, OnToggleHistogram, true );
	hbutton->SetRGBA( 80, 80, 0, 28 );
	hbutton->SetText( "HISTOGRAM" );
	mFullPanel->AddButton( hbutton );

	// Full screen PLAY MODE button
	y = gConfig.mAppFullH + gConfig.mAppFullOffsetY - h3;
	x = gConfig.mAppFullW - w3;

	mPlayFS = new CUITextButton( x, y, w3, h3, -1, OnFullScreenPlayClick, true );
	mPlayFS->SetRGBA( 180, 180, 0, 128 );
	mPlayFS->SetFontSize( 12 );
	mPlayFS->SetText( "PLAY" );
	mFullPanel->AddButton( mPlayFS );
}

void CUIScreen::InitFSPlayPanel()
{
	float x = 0.0f;
	float y = 0.0f;
	float w = 100;
	float h = 42 * Globals.mMiniGuiMin;

	y = gConfig.mAppFullH + gConfig.mAppFullOffsetY - h;

	mFSPlayPanel = new CUIPanel( x - 5, y - 5, w + 10, h + 10, "FSPlayPanel", true );

	mLoadFileFS = new CUITextButton( x, y, w, h, -1, OnClickPlayFile, true );
	mLoadFileFS->SetRGBA( 180, 180, 0, 128 );
	mLoadFileFS->SetText( "LOAD" );
	mFSPlayPanel->AddButton( mLoadFileFS );
}

void CUIScreen::InitFSRecPanel()
{
	float x = 0.0f;
	float y = 0.0f;
	float w = 100;
	float h = 42 * Globals.mMiniGuiMin;

	y = gConfig.mAppFullH + gConfig.mAppFullOffsetY - h;

	mFSRecPanel = new CUIPanel( x - 5, y - 5, w + 10, h + 10, "FSRecPanel", true );

	mRecFS = new CUITextButton( x, y, w, h, -1, OnFullScreenRecClick, true );
	mRecFS->SetRGBA( 180, 0, 0, 255 );
	mRecFS->SetFontSize( 12 );
	mRecFS->SetText( "REC" );
	mFSRecPanel->AddButton( mRecFS );
}

void CUIScreen::InitCenterZoomPanel( void )
{
	mCenterZoomPanel = new CUIPanel(UI_IMAGE_X,UI_IMAGE_Y,UI_IMAGE_W,UI_IMAGE_H,"CenterZoomPanel");
	mCenterZoomPanel->mButtonList = mHotspotList;

	float x, y, w, h;

	float w1 = 290;
	float w2 = 460;
	float h1 = 164;
	float h2 = 257;

	// north
	w = ZOOM_W;
	h = ZOOM_H;
	x = UI_IMAGE_X + UI_IMAGE_W/2 - w/2;
	y = UI_IMAGE_Y + 3;


	CUIIconButton *ibutton = new CUIIconButton( x, y, w, h, -1, onPanN, CUIIconButton::kIconPanN );
	ibutton->SetRGBA( 68,68,68,255);
	mCenterZoomPanel->AddButton( ibutton );

	// south
	y = UI_IMAGE_Y + UI_IMAGE_H - h - 3;
	ibutton = new CUIIconButton( x, y, w, h, -1, onPanS, CUIIconButton::kIconPanS );
	ibutton->SetRGBA( 68,68,68,255);
	mCenterZoomPanel->AddButton( ibutton );

	// west
	w = ZOOM_H;
	h = ZOOM_W;
	y = UI_IMAGE_Y + UI_IMAGE_H / 2 - h / 2;
	x = UI_IMAGE_X + 3;
	ibutton = new CUIIconButton( x, y, w, h, -1, onPanW, CUIIconButton::kIconPanW );
	ibutton->SetRGBA( 68, 68, 68, 255 );
	mCenterZoomPanel->AddButton( ibutton );

	// east
	x = UI_IMAGE_X + UI_IMAGE_W - w - 3;
	ibutton = new CUIIconButton( x, y, w, h, -1, onPanE, CUIIconButton::kIconPanE );
	ibutton->SetRGBA( 68, 68, 68, 255 );
	mCenterZoomPanel->AddButton( ibutton );

	// zoom toggle hotspot
	x = UI_IMAGE_X + w1;
	y = UI_IMAGE_Y + h1;
	CUIHotSpotButton *hbutton = new CUIHotSpotButton( x, y, w2, h2, -1, OnZoomClick );
	hbutton->SetRenderable( false );
	mCenterZoomPanel->AddButton( hbutton );
}

void CUIScreen::InitFullZoomPanel( void )
{
	float x = 0.0f, y = 0.0f, w = 0.0f, h = 0.0f;

	mFullZoomPanel = new CUIPanel( gConfig.mAppFullX, 
								   gConfig.mAppFullY, 
								   gConfig.mAppFullW,
								   gConfig.mAppFullH, 
								   "FullZoomPanel", 
								   true );

	// north
	w = ZOOM_W;
	h = ZOOM_H;
	x = gConfig.mAppFullX + gConfig.mAppFullW / 2 - w / 2;
	y = gConfig.mAppFullY + 3;

	CUIIconButton *ibutton = new CUIIconButton( x, y, w, h, -1, onPanN, CUIIconButton::kIconPanN, true );
	ibutton->SetRGBA( 68, 68, 68, 255 );
	mFullZoomPanel->AddButton( ibutton );

	// south
	y = gConfig.mAppFullY + gConfig.mAppFullH - h - 3;
	ibutton = new CUIIconButton( x, y, w, h, -1, onPanS, CUIIconButton::kIconPanS, true );
	ibutton->SetRGBA( 68, 68, 68, 255 );
	mFullZoomPanel->AddButton( ibutton );

	// west
	w = ZOOM_H;
	h = ZOOM_W;
	y = gConfig.mAppFullY + gConfig.mAppFullH / 2 - h / 2;
	x = gConfig.mAppFullX + 3;
	ibutton = new CUIIconButton( x, y, w, h, -1, onPanW, CUIIconButton::kIconPanW, true );
	ibutton->SetRGBA( 68, 68, 68, 255 );
	mFullZoomPanel->AddButton( ibutton );

	// east
	x = gConfig.mAppFullX + gConfig.mAppFullW - w - 3;
	ibutton = new CUIIconButton( x, y, w, h, -1, onPanE, CUIIconButton::kIconPanE, true );
	ibutton->SetRGBA( 68, 68, 68, 255 );
	mFullZoomPanel->AddButton( ibutton );

	float h1 = gConfig.mAppFullH * 28 / 100;
	float w1 = gConfig.mAppFullW * 28 / 100;
	float h2 = gConfig.mAppFullH * 44 / 100;
	float w2 = gConfig.mAppFullW * 44 / 100;
	
	x = gConfig.mAppFullX + w1;
	y = gConfig.mAppFullY + h1;
	CUIHotSpotButton *hbutton = new CUIHotSpotButton( x, y, w2, h2, -1, OnZoomClick, true );
	hbutton->SetRenderable( false );
	mFullZoomPanel->AddButton( hbutton );
}

void CUIScreen::InitParallaxShiftPanel()
{
	mParallaxShiftPanel = new CUIPanel( UI_IMAGE_X, UI_IMAGE_Y, UI_IMAGE_W, UI_IMAGE_H, "ParallaxShiftPanel" );
	mParallaxShiftPanel->mButtonList = mHotspotList;

	float x, y, w, h;

	float w1 = 300;
	float w2 = 480;
	float h1 = 175;
	float h2 = 275;

	w = ZOOM_W;
	h = ZOOM_H;
	x = UI_IMAGE_X + UI_IMAGE_W/2 - w/2;
	y = UI_IMAGE_Y + 3;

	// Y plus
	CUIIconButton *ibutton = new CUIIconButton( x, y ,w, h, -1, onYplus, CUIIconButton::kIconYplus );
	ibutton->SetRGBA( 68, 68, 68, 255 );
	mParallaxShiftPanel->AddButton( ibutton );

	// Y minus
	y = UI_IMAGE_Y + UI_IMAGE_H - h - 3;
	ibutton = new CUIIconButton( x, y, w, h, -1, onYminus, CUIIconButton::kIconYminus );
	ibutton->SetRGBA( 68, 68, 68, 255 );
	mParallaxShiftPanel->AddButton( ibutton );

	// X minus
	w = ZOOM_H;
	h = ZOOM_W;
	y = UI_IMAGE_Y + UI_IMAGE_H/2 - h/2;
	x = UI_IMAGE_X + 3;
	ibutton = new CUIIconButton( x, y, w, h, -1, onXminus, CUIIconButton::kIconXminus );
	ibutton->SetRGBA( 68, 68, 68, 255 );
	mParallaxShiftPanel->AddButton( ibutton );

	// X plus
	x = UI_IMAGE_X + UI_IMAGE_W - w - 3;
	ibutton = new CUIIconButton( x, y, w, h, -1, onXplus, CUIIconButton::kIconXplus );
	ibutton->SetRGBA( 68, 68, 68, 255 );
	mParallaxShiftPanel->AddButton( ibutton );
}

void CUIScreen::InitCamAlignPanel()
{
	mCamAlignPanel = new CUIPanel( UI_IMAGE_X, UI_IMAGE_Y, UI_IMAGE_W, UI_IMAGE_H, "CamAlignPanel" );
	mCamAlignPanel->mButtonList = mHotspotList;

	float x, y, w, h;

	float w1 = 300;
	float w2 = 480;
	float h1 = 175;
	float h2 = 275;

	w = ZOOM_W;
	h = ZOOM_H;
	x = UI_IMAGE_X + UI_IMAGE_W/2 - w/2;
	y = UI_IMAGE_Y + 3;
  

	// Plus
	CUIIconButton *ibutton = new CUIIconButton( x, y ,w, h, -1, on3DGuidesPlus, CUIIconButton::kIcon3DGuidesPlus );
	ibutton->SetRGBA( 68, 68, 68, 255 );
	mCamAlignPanel->AddButton( ibutton );

	// Minus
	y = UI_IMAGE_Y + UI_IMAGE_H - h - 3;
	ibutton = new CUIIconButton( x, y, w, h, -1, on3DGuidesMinus, CUIIconButton::kIcon3DGuidesMinus );
	ibutton->SetRGBA( 68, 68, 68, 255 );
	mCamAlignPanel->AddButton( ibutton );
}

void CUIScreen::InitBottomPanel( void )
{
	mBottomPanel = new CUIPanel(0,656,1280,64,"BottomPanel");

	float gap = 8;
	float x = 3;
	float y = 660;
	float w = 206;
	float h = 57;

	mRecordButton = new CUITextButton(x,y,w,h,-1,OnRecordClick);
	mRecordButton->SetText( "RECORD" );
	mRecordButton->SetRGBA( 180,0,0,255);
	mBottomPanel->AddButton( mRecordButton );

	x += (w + gap);
	mLoadModesButton = new CUITextButton(x,y,w,h,-1,OnLoadModes);
	mLoadModesButton->SetText( "RESOLUTION/", "FRAME RATE" );
	mBottomPanel->AddButton( mLoadModesButton );

	x += (w + gap);
	mGainButton = new CUITextButton(x,y,w,h,-1,dummyOnClick);
	if ( !gPrefs.GetMode800() )
		mGainButton->SetText( "-        GAIN        +" );
	else
		mGainButton->SetText( "-         GAIN         +" );
	mBottomPanel->AddButton( mGainButton );

	CUIHotSpotButton *hbutton = new CUIHotSpotButton(x,y,w/2,h,-1,OnGainMinusClick);
	hbutton->SetRenderable(false);
	mBottomPanel->AddButton( hbutton );
	hbutton = new CUIHotSpotButton(x+w/2,y,w/2,h,-1,OnGainPlusClick);
	hbutton->SetRenderable(false);
	mBottomPanel->AddButton( hbutton );

	x += (w + gap);
	mWhiteBalance = new CUITextButton( x, y, w, h, -1, OnWhiteBalanceSetup );
	mWhiteBalance->SetText( "WHITE BALANCE" );
	mWhiteBalance->SetActive( true );
	mBottomPanel->AddButton( mWhiteBalance );

	x += (w + gap);
	mShutterControl = new CUITextButton( x, y ,w, h, -1, OnShutterDialog);
	mShutterControl->SetText( "SHUTTER", "CONTROL" );

	if ( gPrefs.GetCustomMode() )
	{
		mShutterControl->SetActive( false );
	}
	mBottomPanel->AddButton( mShutterControl );

	x += ( w + gap );
	mSpotFocusMeter = new CUITextButton( x, y, w, h, -1, OnToggleLoupe ); 
	mSpotFocusMeter->SetText( "SPOT/FOCUS", "METER" );
	mBottomPanel->AddButton( mSpotFocusMeter );
}


void CUIScreen::InitBottomUtilityPanel( void )
{
	mBottomUtilityPanel = new CUIPanel(0,656,1280,64,"BottomUtilityPanel");

	float gap = 8;
	float x = 3;
	float y = 660;
	float w = 206;
	float h = 57;

	CUITextButton *tbutton  = new CUITextButton( x, y, w, h, -1, OnLoadPrefs );
	tbutton->SetText( "CAM PREFS" );
	mBottomUtilityPanel->AddButton( tbutton );

	x += ( w + gap );
	tbutton = new CUITextButton( x, y, w, h, -1, OnDisplayPrefsDialog );
	tbutton->SetText( "DISPLAY PREFS" );
	mBottomUtilityPanel->AddButton( tbutton );

	x += ( w + gap );
	tbutton = new CUITextButton( x, y, w, h, -1, OnTimecode2Dialog );
	tbutton->SetText( "TIMECODE", "SETTINGS" );
	mBottomUtilityPanel->AddButton( tbutton );

	x += ( w + gap );
	tbutton = new CUITextButton( x, y, w, h, -1, OnLoadMacros );
	tbutton->SetText( "RIGHT-CLICK", "SETTINGS" );
	mBottomUtilityPanel->AddButton( tbutton );

	x += ( w + gap );
	tbutton = new CUITextButton( x, y, w, h, -1, OnBlackCalibration );
	tbutton->SetText( "BLACK", "CALIBRATION" );
	mBottomUtilityPanel->AddButton( tbutton );

	x += ( w + gap );
	tbutton = new CUITextButton( x, y, w, h, -1, OnMulticamMode );
	tbutton->SetText( "MASTER-SLAVE", "MODE" );

	// Activate multi cam mode button only if we are in single cam mode
	if ( gDeviceManager.NumDevices() > 1 )
	{
		tbutton->SetActive( false ); 
	}

	mBottomUtilityPanel->AddButton( tbutton );
}

void CUIScreen::InitBottomPlayPanel( void )
{
	mBottomPlayPanel = new CUIPanel( 0, 656, 1280, 64, "BottomPlayPanel" );

	float gap = 3;
	float x = 3;
	float y = 662;
	float w = 100;
	float h = 57;

	CUIIconButton *ibutton = new CUIIconButton( x, y, w, h, -1, OnPlayPrevious, CUIIconButton::kIconPrevious );
	mBottomPlayPanel->AddButton( ibutton );
	x += w;
	x += gap;

	ibutton = new CUIIconButton( x, y, w, h, -1, OnPlayStepBack, CUIIconButton::kIconStepBack );
	mBottomPlayPanel->AddButton( ibutton );
	x += w;
	x += gap;

	ibutton = new CUIIconButton( x, y, w, h, -1, OnPlayPlay, CUIIconButton::kIconPlay );
	mBottomPlayPanel->AddButton( ibutton );
	x += w;
	x += gap;

	ibutton = new CUIIconButton( x, y, w, h, -1, OnPlayPause, CUIIconButton::kIconPause );
	mBottomPlayPanel->AddButton( ibutton );
	x += w;
	x += gap;

	ibutton = new CUIIconButton( x, y, w, h, -1, OnPlayStepForward, CUIIconButton::kIconStepForward );
	mBottomPlayPanel->AddButton( ibutton );
	x += w;
	x += gap;

	w = 650;
	CUITextButton *tbutton = new CUITextButton( x, y, w, h, -1, NULL );
	tbutton->SetText( "" );
	mBottomPlayPanel->AddButton( tbutton );
	x += w;
	x += gap;

	w = 100;
	ibutton = new CUIIconButton( x, y, w, h, -1, OnPlayNext, CUIIconButton::kIconNext );
	mBottomPlayPanel->AddButton( ibutton );
}

void CUIScreen::InitBottomPlayPanelFS()
{
	float x = 0, y = 0, w = 0, h = 0;
	float panelH = 42 * Globals.mMiniGuiMin;
	float panelW = 100 * Globals.mMiniGuiMin;
	float timelineW = 650 * Globals.mMiniGuiMin;
	float gap = 3 * Globals.mMiniGuiMin;

	x = ( gConfig.mAppFullW / 2 - ( 1280 * Globals.mMiniGuiMin ) / 2 );
	y = gConfig.mAppFullH + gConfig.mAppFullOffsetY - ( panelH * Globals.mMiniGuiMin );
	w = gConfig.mAppFullW;

	if ( gConfig.mMonitorW[ 0 ] <= 1280 )
	{
		x = ( gConfig.mAppFullW / 2 - ( 960 * Globals.mMiniGuiMin ) / 2 );
		y = gConfig.mAppFullH + gConfig.mAppFullOffsetY - ( panelH );
		gap *= 0.75f;
		panelW *= 0.75f;
		timelineW *= 0.75f;
	}

	mBottomPlayPanelFS = new CUIPanel( 0, y, w, panelH, "BottomPlayPanelFullScreen", true );

	CUIIconButton *ibutton = new CUIIconButton( x, y, panelW, panelH, -1, OnPlayPrevious, CUIIconButton::kIconPrevious, true );
	mBottomPlayPanelFS->AddButton( ibutton );
	x += panelW;
	x += gap;

	ibutton = new CUIIconButton( x, y, panelW, panelH, -1, OnPlayStepBack, CUIIconButton::kIconStepBack, true );
	mBottomPlayPanelFS->AddButton( ibutton );
	x += panelW;
	x += gap;

	ibutton = new CUIIconButton( x, y, panelW, panelH, -1, OnPlayPlay, CUIIconButton::kIconPlay, true );
	mBottomPlayPanelFS->AddButton( ibutton );
	x += panelW;
	x += gap;

	ibutton = new CUIIconButton( x, y, panelW, panelH, -1, OnPlayPause, CUIIconButton::kIconPause, true );
	mBottomPlayPanelFS->AddButton( ibutton );
	x += panelW;
	x += gap;

	ibutton = new CUIIconButton( x, y, panelW, panelH, -1, OnPlayStepForward, CUIIconButton::kIconStepForward, true );
	mBottomPlayPanelFS->AddButton( ibutton );
	x += panelW;
	x += gap;

	CUITextButton *tbutton = new CUITextButton( x, y, timelineW, panelH, -1, NULL, true );
	tbutton->SetText( "" );
	mBottomPlayPanelFS->AddButton( tbutton );

	x += timelineW;
	x += gap;
	ibutton = new CUIIconButton( x, y, panelW, panelH, -1, OnPlayNext, CUIIconButton::kIconNext, true );
	mBottomPlayPanelFS->AddButton( ibutton );
}

void CUIScreen::StartupFullScreenMode()
{
	EVideoDisplayMode startupMode = VIDEO_DISPLAY_NORMAL;

	if ( gPrefs.GetStartupFullScreen() )
	{
		startupMode = VIDEO_DISPLAY_FULL_OVERLAY;
		Globals.SetGeometry( GEOMETRY_FULL );
	}
	SetVideoDisplayMode( startupMode );
}


bool CUIScreen::Init( void )
{
	mFileDialog = NULL;
	mMacroListDialog = NULL;
	mDisplayModesListDialog = NULL;
	mMetaDataCtrl = NULL;
	mDisplayPrefsDialog = NULL;
	gD3D->SetFontSize( 18 );

	InitLeftPanel();
	InitMeterPanel();
	InitBottomPanel();
	InitCenterPanel();
	InitFullZoomPanel();
	InitFullPanel();
	InitFSPlayPanel();
	InitFSRecPanel();
	InitCenterZoomPanel();
	InitParallaxShiftPanel();
	InitCamAlignPanel();
	InitLeftPlayPanel();
	InitRightFramestorePanel();
	InitRightProjectPanel();
	InitRightCamSetupPanel();
	InitRightLoadLookPanel();
	InitRightPlayPanel();
	InitWhiteBalancePanel();
	InitBottomPlayPanel();
	InitBottomPlayPanelFS();

	InitLeftUtilityPanel();
	InitRightUtilityPanel();
	InitBottomUtilityPanel();

	StartupFullScreenMode();

	return true;
}


void TrimString( char *projStr, int maxWidth )
{
	float len = gD3D->GetStringWidth(projStr);
	if ( len <= maxWidth ) return;

	strcpy(projStr,projStr);
	while( len > maxWidth )
	{
		len = gD3D->GetStringWidth(projStr);
		projStr[strlen(projStr)-1] = 0;
	}
}


void CUIScreen::RenderTopPanel( void )
{
	float x = 0.0f, y = 0.0f, xOffset = 0.0f;
	char str[64];
	
	///////////
	// ROW 1 //
	///////////
	if ( mVideoDisplayMode == VIDEO_DISPLAY_NORMAL )
	{
		y = UI_TOP_Y1;
	}
	else
	{
		xOffset = ( gConfig.mAppFullW / 2 - ( 1280 * Globals.mMiniGuiMin ) / 2 );
		y = 0;
	}

	x = UI_TOP_LEFT + xOffset;

	// If we are in PREVIEW or PLAYBACK mode, display the RECALL or 3D DISPLAY MODE
	bool is3DMode = Globals.GetDraw3D();

	if ( mIsRecording )
	{
		GIGE_SETFONTCOLOR( 1, 0, 0 );
		GIGE_STRINGOUT((float)x, (float)y, "RECORD" );

		// Write out a warning if we record in demo mode
		if ( ! Globals.IsLicensed() )
		{
			int cnt = Globals.GetDisplayDuration();
			gD3D->SetFontSize( 24 );
			GIGE_SETFONTCOLOR( 1, 0, 0 );
			if ( cnt < 200 )
			{
				GIGE_STRINGOUTCENTER( (float) x + 650, (float) y + 350, "WARNING - RECORDING IN DEMO MODE" );
				cnt ++;
				Globals.SetDisplayDuration( cnt );
			}
			// Reset font color and size
			GIGE_SETFONTCOLOR( 1, 1, 1 );
			gD3D->SetFontSize( 18 );
		}
	}
	else
	{
		GIGE_SETFONTCOLOR( 0, 1, 0 );
	}

	int channel = Get3DDisplayChannel();
	if( Globals.GetRecall() != GLOBALS_RECALL_NONE && channel >= 0)
	{
		GIGE_SETFONTCOLOR( 0, 1, 0 );

		if ( Globals.GetRecall() == GLOBALS_RECALL_100 )
		{
			if ( channel == 1 )
			{
    			GIGE_STRINGOUT((float)x, (float)y, "L-RECALL" );
			}
			else if ( channel == 2 )
			{
				GIGE_STRINGOUT((float)x, (float)y, "R-RECALL" );
			}
			else
			{
				GIGE_STRINGOUT((float)x, (float)y, "RECALL" );
			}
		}
	    else if ( Globals.GetRecall() == GLOBALS_RECALL_50)
		{
		    if ( channel == 1 )
			{
    			GIGE_STRINGOUT((float)x, (float)y, "L-RECALL-50" );
			}
			else if ( channel == 2 )
			{
				GIGE_STRINGOUT((float)x, (float)y, "R-RECALL-50" );
			}
			else
			{
				GIGE_STRINGOUT((float)x, (float)y, "RECALL-50" );
			}
		}
		else
		{
			if ( mIsRecording )
			{
				GIGE_STRINGOUT((float)x, (float)y, "RECORD" );
			}
			else if ( mMode == SCREEN_MODE_PREVIEW )
			{		
				GIGE_STRINGOUT((float)x, (float)y, "PREVIEW" );
			}
			else
			{
				GIGE_STRINGOUT((float)x, (float)y, "PLAYBACK" );
			}
		}
	}
	else
	{
		if ( mIsRecording )
		{
			GIGE_SETFONTCOLOR( 1, 0, 0 );
		}
		else
		{
			GIGE_SETFONTCOLOR( 0, 1, 0 );
		}

		if ( is3DMode && ! mIsRecording )
		{
            if ( !gPrefs.GetScaleExternalMonitor() )
            {
        		int mode3D = Globals.GetDisplay3DMode();
				if ( mode3D == MODE3D_ANAGLYPH )
					GIGE_STRINGOUT((float)x, (float)y, "ANAGLYPH" );
				else if ( mode3D == MODE3D_5050 )
    				GIGE_STRINGOUT((float)x, (float)y, "50-50" );
				else if ( mode3D == MODE3D_DIFFERENCE )
    				GIGE_STRINGOUT((float)x, (float)y, "DMAP" );
	    		else if ( mode3D == MODE3D_LEFT )
		    		GIGE_STRINGOUT((float)x, (float)y, "LEFT" );
			    else if ( mode3D == MODE3D_RIGHT )
				    GIGE_STRINGOUT((float)x, (float)y, "RIGHT" );
			    else if ( mode3D == MODE3D_WIGGLE )
				    GIGE_STRINGOUT((float)x, (float)y, "WIGGLE" );
			    else if ( mode3D == MODE3D_SIDE_BY_SIDE )
				    GIGE_STRINGOUT((float)x, (float)y, "SxS" );
			    else if ( mode3D == MODE3D_SPLITSCREEN )
				    GIGE_STRINGOUT((float)x, (float)y, "SPLIT" );
				else
					GIGE_STRINGOUT((float)x, (float)y, "UNKNOWN" );
            }
            else
            {
        		int scalingMode = Globals.mScalingMode;
				//if ( scalingMode == SCALEMODE3D_LEFTLEFT )
				//	GIGE_STRINGOUT((float)x, (float)y, "LEFT" );
				//else if ( scalingMode == SCALEMODE3D_RIGHTRIGHT )
    				//GIGE_STRINGOUT((float)x, (float)y, "RIGHT" );
	    		if ( scalingMode == SCALEMODE3D_LEFTRIGHT )
		    		GIGE_STRINGOUT((float)x, (float)y, "LEFT-RIGHT" );
	    		if ( scalingMode == SCALEMODE3D_DIFFERENCE )
		    		GIGE_STRINGOUT((float)x, (float)y, "DMAP" );
            }
		}
		else
		{
			if ( mIsRecording )
			{
				GIGE_STRINGOUT((float)x, (float)y, "RECORD" );
			}
			else if ( mMode == SCREEN_MODE_PREVIEW )
			{		
				GIGE_STRINGOUT((float)x, (float)y, "PREVIEW" );
			}
			else
			{
				GIGE_STRINGOUT((float)x, (float)y, "PLAYBACK" );
			}
		}
	}
	GIGE_SETFONTCOLOR( 1, 1, 1 );
	
	// External monitor 3D display mode
	x = UI_TOP_LEFT + xOffset + 150;
	if ( ( gPrefs.GetDispActivateExtDisplay() ) && ( is3DMode ) )
	{
		GIGE_SETFONTCOLOR( 0, 1, 0 );

		if ( gPrefs.GetOpenGL() )
		{
			switch ( gPrefs.GetDispExtMode() )
			{
				case MODE3D_LEFT:
					strcpy( str, "EXT: LEFT" );
					break;

				case MODE3D_RIGHT:
					strcpy( str, "EXT: RIGHT" );
					break;

				case MODE3D_ANAGLYPH:
					strcpy( str, "EXT: STEREO-G" );
					break;

				case MODE3D_5050:
					strcpy( str, "EXT: STEREO-A" );
					break;

				case MODE3D_SIDE_BY_SIDE:
					strcpy( str, "EXT: STEREO-S" );
					break;

				case MODE3D_SPLITSCREEN:
					strcpy( str, "EXT: STEREO-P" );
					break;

				case MODE3D_MESH:
					strcpy( str, "EXT: STEREO-M" );
					break;

				case MODE3D_INTERLACE:
					strcpy( str, "EXT: STEREO-I" );
					break;
					
			}
		}
		else
		{
			switch ( gPrefs.GetDispExtMode() )
			{
				case MODE3D_LEFT:
					strcpy( str, "EXT: LEFT" );
					break;

				case MODE3D_RIGHT:
					strcpy( str, "EXT: RIGHT" );
					break;

				case MODE3D_ANAGLYPH:
					strcpy( str, "EXT: ANAGLYPH" );
					break;

				case MODE3D_5050:
					strcpy( str, "EXT: 50-50" );
					break;

				case MODE3D_SIDE_BY_SIDE:
					strcpy( str, "EXT: SxS" );
					break;

				case MODE3D_SPLITSCREEN:
					strcpy( str, "EXT: SPLIT" );
					break;

				case MODE3D_MESH:
					strcpy( str, "EXT: MESH" );
					break;

				case MODE3D_INTERLACE:
					strcpy( str, "EXT: INTERLACE" );
					break;
					
			}
		}

		GIGE_STRINGOUT( (float) x, (float) y, str );
	}
	
	// Project
	GIGE_SETFONTCOLOR( 1, 1, 1 );
	x = UI_TOP_CENTER + xOffset;
	char projStr[ 256 ];

	if ( isProgramPlayingAVI() || isProgramPlayingSIV() || isProgramRendering() )
	{
		char playFile[ 256 ] = "\0";
		int len = (int) strlen( gScreenPlayFile );
		if ( len >= 30 )
		{
			strncpy( playFile, gScreenPlayFile, 30 );
			strcat( playFile, "..." );
			playFile[ 33 ] = '\0'; // Ensure string is null terminated
			sprintf( projStr, "%s", playFile );
		}
		else
		{
			strcpy( projStr, gScreenPlayFile );
			sprintf( projStr, "%s", gScreenPlayFile );
		}
	}
	else if ( Globals.GetFailedSend() )
	{
		GIGE_SETFONTCOLOR( 1, 0, 0 );
		sprintf( projStr, "LOADING ERROR - RELOAD MODE" );
	}
	else if ( Globals.GetStatusBarMode() )
	{
		// Scene-shot-take numbers for statusbar
		sprintf( projStr, "%s", Globals.GetNextFileName() );
	}
	else if ( gProject )
	{
		GIGE_SETFONTCOLOR( 1, 0, 0 );
		
		if ( Globals.GetDriveFull() )
		{
			sprintf( projStr,"Drive Full - Record Disabled" );
		}
		else if ( mRightPanelInfo.drive_full == true )
		{
			sprintf( projStr, "Drive almost full - time remaining: %4.1f min.", mRightPanelInfo.time );
		}
		
		else if ( ! gProject->mIsNTFS )
		{
			sprintf( projStr,"No NTFS volume - Record Disabled" );
		}
		else
		{
			int cnt = Globals.GetDisplayDuration();
			if ( ( cnt < 100 ) && ( Globals.GetIridasRemoteComplete() ) )
			{
				sprintf( projStr, "REMOTE IRIDAS UPDATE COMPLETE" );
				cnt ++;
				Globals.SetDisplayDuration( cnt );
			}
			else
			{
				Globals.SetIridasRemoteComplete( false );
				if ( Globals.GetProjectError() )
				{
					sprintf( projStr, "ERROR - PROJECT EXISTS" );
				}
				else
				{
#ifndef ARI_TEST
					sprintf( projStr,"Proj: %s", gProject->mName );
					GIGE_SETFONTCOLOR( 1, 1, 1 );
					GIGE_STRINGOUTCENTER( x , (float)y, projStr );
#else
					sprintf( projStr,"..." );
					if ( gCinThread )
					{
						if ( gCinThread->mCineformSDK )
						{
							sprintf( projStr,"Pct: %d  State: %d LastResult: %d", 
								gDualBufferManager[0]->QueueSize(BUFFER_STATE_COMPLETE), 
								gCinThread->mCineformSDK->mGovernor.mLastPct,
								gCinThread->mCineformSDK->mGovernor.mState,
								gCinThread->mCineformSDK->mGovernor.mLastResult );
							GIGE_SETFONTCOLOR( 1, 1, 1 );
							GIGE_STRINGOUTCENTER( x , (float)y, projStr );
						}
					}
#endif
				}
			}
		}
	}
	else
	{
		GIGE_SETFONTCOLOR( 1, 0, 0 );
        
		if ( Globals.GetProjectError() )
		{
			sprintf( projStr, "ERROR - PROJECT EXISTS" );
		}
		else 
        {
		    sprintf( projStr, "No Project - Record Disabled" );
        }
	}

	if ( ! Globals.GetBatteryLow() )
	{
		GIGE_STRINGOUTCENTER( x , (float)y, projStr );
	}
	else
	{
		GIGE_SETFONTCOLOR( 1, 0, 0 );
		GIGE_STRINGOUTCENTER( x, (float)y, "BATTERY LOW" );
		GIGE_SETFONTCOLOR( 1, 1, 1 );
	}

	// 3D alignment percentage
	if ( Globals.GetGuide() == GUIDE_3D )
	{
		float val = gPrefs.Get3DAlignmentValue();

		if ( gPrefs.Get3DAlignmentGuideMode() == GUIDES_3D_PRCT )
		{
			sprintf( str, "%-.1f%%", val );
		}
		else
		{
			sprintf( str, "%d PX", (int) val );
		}

		GIGE_SETFONTCOLOR( 1, 1, 1 );
		GIGE_STRINGOUT( x + 275, (float) y, str );
	}
	
	// Zebra
	if ( !gPrefs.GetMode800() )
		TrimString( projStr, 440 );
	else
		TrimString( projStr, 260 );

	// Indicate 3D playback for dual channel SIV and Cineform
	if ( Globals.GetDraw3D() )
	{
		GIGE_SETFONTCOLOR( 0, 1, 0 );
		GIGE_STRINGOUTRIGHT( x + 250, (float)y, "3D" );
		GIGE_SETFONTCOLOR( 1, 1, 1 );
	}

	GIGE_SETFONTCOLOR(1, 1, 1);
	// Quality/wrapper settings
	char qualStr[32];
	qualStr[0] = 0;

	if ( isProgramPlayingAVI() || isProgramPlayingSIV() )
	{
		// TODO: read the info from the file
		char fileName[ 128 ];
		strcpy( fileName, Globals.GetCurrentPlaybackFile() );

		// For SIV, get the resolution
		if ( isFile ( fileName, "siv" ) )
		{
			int res = Globals.GetSIVResolution();
			if ( ( res == 1 ) || ( res == 3 ) )
			{
				strcpy( qualStr, "10-bit" );
			}
			else
			{
				strcpy( qualStr, "12-bit" );
			}
		}
		else
		{
			strcpy( qualStr, "CF-RAW" );
		}
	}
	else
	{
		if ( gPrefs.GetRecordFormat() == PREFS_RECORD_FORMAT_LINEAR12 ) 
		{
			strcpy( qualStr, "12-bit" );
		}
		else 
		{
			strcpy( qualStr, "10-bit" );
		}
		
		if ( gPrefs.GetRecordFormat() == PREFS_RECORD_FORMAT_CINEFORM ) 
		{
            strcpy(qualStr,cineformModeDesc[gPrefs.GetCineformMode3()]);
			if ( gPrefs.GetWrapperType() == WRAPPER_TYPE_AVI )
				strcat( qualStr,"-AVI" );
			else
				strcat( qualStr,"-QT" );
            if ( gPrefs.GetCineformMode3() == PREFS_CINEFORM_RECORD_MODE_FS2 && isProgramCompressing())
            {
                char level[8];
                sprintf(level,"-%d",CCineformSDK::mLosslessLevel);
				strcat( qualStr, level );
            }
		}
	}

	if ( gPrefs.GetDebugMode() )
	{
    	char debugStr1[32];
    	char debugStr2[32];
		extern int avi_iter;
		if ( gAcqThread && gAcqThread->mNumFrames )
		{	
			float seconds = (float) ( GetTickCount() - gAcqThread->mFirstTicks );
			seconds = seconds / 1000.0f;
			float framerate = (float) gAcqThread->mNumFrames[0] / seconds;
			extern DWORD ttl_ints;
			if ( gD3D->mLookHasAlpha )
				sprintf(debugStr1,"%06.04f",framerate);
			else
				sprintf(debugStr1,"%06.04f",framerate);
    		GIGE_STRINGOUTRIGHT((float)(UI_TOP_RIGHT-100), (float)y, debugStr1 );
		}
		if ( gShoThread )
		{	
			if ( gPrefs.GetSdiMode() )
			{
				if ( !gSdiThread->mDoFlush )
					sprintf(debugStr2,"Drn%dDrp%dQue %d",gShoThread->mDrawn, gShoThread->mDroppedTotal, gSdiThread->mQueued);
				else
					sprintf(debugStr2,"Drn%dDrp%dQueF%d",gShoThread->mDrawn, gShoThread->mDroppedTotal, gSdiThread->mQueued);
				GIGE_STRINGOUTRIGHT((float)(UI_TOP_RIGHT-200), (float)y, debugStr2 );
			}
			else
			{
				sprintf(debugStr2,"Drn%dDrp%d",gShoThread->mDrawn, gShoThread->mDroppedTotal);
    			GIGE_STRINGOUTRIGHT((float)(UI_TOP_RIGHT-200), (float)y, debugStr2 );
			}
		}
	}
	
	GIGE_STRINGOUTRIGHT((float)( UI_TOP_RIGHT + xOffset ), (float)y, qualStr );
	
	///////////
	// ROW 2 //
	///////////
	x = UI_TOP_LEFT + xOffset;
	y += 28;

	// Record time code or look file name
	if ( mIsRecording )
	{
		GIGE_SETFONTCOLOR( 1, 0, 0 );
		char str[ 32 ];
		gTimecodeController.recordTimecode()->String( str );
		GIGE_STRINGOUT((float)( x ), (float) y, str );

		char recMode[ 4 ];
		if ( gPrefs.GetTimeLapseMode() == TIME_LAPSE_MODE_SKIP )
		{
			strcpy( recMode, "TL" );
			GIGE_STRINGOUT((float)( x + 130 ), (float) y, recMode );
		}
		else if ( gPrefs.GetTimeLapseMode() == TIME_LAPSE_MODE_SINGLE )
		{
			strcpy( recMode, "SF" );
			GIGE_STRINGOUT((float)( x + 130 ), (float) y, recMode );
		}
	}
	else
	{
		// Parallax values
		float parallaxX = 0.0f, parallaxY = 0.0f, parallaxK = 0.0f;

		parallaxX = Globals.GetParallaxShift( PREFS_PARALLAX_X );
		parallaxY = Globals.GetParallaxShift( PREFS_PARALLAX_Y );
		parallaxK = Globals.GetParallaxShift( PREFS_PARALLAX_K );

		// If one of the values is non-zero, display values for X, Y, and R
		if ( ( parallaxX != 0 ) || ( parallaxY != 0 ) || ( parallaxK != 0 ) )
		{
			char prlxStr[ 128 ];
			sprintf( prlxStr, "X:%.2f Y:%.2f R:%.2f", parallaxX, parallaxY, parallaxK );
			GIGE_SETFONTCOLOR( 1, 1, 1 );
			GIGE_STRINGOUT( (float) x, (float) y, prlxStr );
		}
		else // display the current LUT
		{
			GIGE_SETFONTCOLOR( 1, 1, 1 );
			
			if ( isProgramPlayingAVI() || isProgramPlayingSIV() )
				mActiveLook = gPrefs.GetPlayerLook();
			else if ( gPrefs.GetPreviewLookActive() )
				mActiveLook = gPrefs.GetViewLook();
			else
				mActiveLook = gPrefs.GetRecordLook();
		
			gD3D->SetFontSize( 17 );

			if ( ! Globals.GetLut3DOn() )
			{
				GIGE_STRINGOUT( (float) x, (float) y, "NO LOOK" );
			}
			else
			{
				char lookFileNameOrig [128] = "\0";
				char lookFileNameNew [24] = "\0";
				char tempStr [128] = "\0";
				strcpy( tempStr, gPrefs.GetLookName( mActiveLook ) );
				char * look = gUtils->TrimFileExtension( tempStr );
				strcpy( lookFileNameOrig, look );
				lookFileNameOrig[ sizeof( lookFileNameOrig ) - 1 ] = '\0'; // Ensure string is null terminated
				
				int len = (int)strlen( lookFileNameOrig );
				if ( len >= 20 )
				{
					strncpy( lookFileNameNew, lookFileNameOrig, 20 );
					lookFileNameNew[ sizeof( lookFileNameNew ) - 1 ] = '\0'; // Ensure string is null terminated
					strcat( lookFileNameNew, ".." );
					lookFileNameNew[ sizeof( lookFileNameNew ) - 1 ] = '\0'; // Ensure string is null terminated
					GIGE_STRINGOUT( (float) ( x ), (float) y, lookFileNameNew );
				}
				else
				{
					lookFileNameOrig[ sizeof( lookFileNameOrig ) - 1 ] = '\0'; // Ensure string is null terminated
					GIGE_STRINGOUT( (float) ( x ), (float) y, lookFileNameOrig );
				}
			}
		}
	}

	GIGE_SETFONTCOLOR( 1, 1, 1 );
	gD3D->SetFontSize( 18 );


	// Mode
	x = UI_TOP_X2 + xOffset;
	
	if ( isProgramPlayingAVI() || isProgramPlayingSIV() || isProgramRendering() )
	{
		if ( ( gProject ) && ( gPrefs.GetConformProjectFPS() ) && ( isProgramPlayingAVI() ) )
		{
			char str[ 24 ];
			SI32f framerate = 0.0f;
			framerate = gSIPlayer->m_Vmd.originalFPS;

			sprintf( str,"%s %4.2fP", Globals.GetCameraModeTextPlay(), framerate );
			GIGE_STRINGOUTCENTER( (float) x, (float) y, str );
		}
		else
		{
			GIGE_STRINGOUTCENTER( (float) x, (float) y, Globals.GetCameraModeTextPlay() );
		}
	}
	else
	{
		GIGE_STRINGOUTCENTER( (float) x, (float) y, Globals.GetCameraModeText() );
	}

	// Temperature
	if ( Globals.GetTemperature() != -1 )
	{
		char str[ 8 ];
		x = UI_TOP_X3 + 25 + xOffset;
		sprintf(str,"%d",Globals.GetTemperature());
		int len = (int)strlen(str);
		str[len] = (char)176;
		str[len+1] = 0;
		GIGE_STRINGOUTCENTER((float)(x), (float)y, str );
	}

	// Flip
	x = UI_TOP_X4 + xOffset;
	DrawFlipText( str );
	GIGE_STRINGOUTCENTER( (float) x, (float) y, str );

	// Guides
	x = UI_TOP_X5 + xOffset;
	if ( Globals.GetGuide() )
	{
		GIGE_SETFONTCOLOR( 1, 1, 1 );

		switch( Globals.GetGuide()  )
		{
			case GUIDE_178:
				strcpy( str,"16:9" );
				break;

			case GUIDE_185:
				strcpy( str,"1.85:1" );
				break;

			case GUIDE_239:
				strcpy( str,"2.39:1" );
				break;

			case GUIDE_133:
				strcpy( str,"4:3" );
				break;

			case GUIDE_3D:
				strcpy( str, "3D" );
				break;

			case GUIDE_HOTSPOTS:
				strcpy( str, "HS" );
				break;
		}
		GIGE_STRINGOUT( (float) x, (float) y, str );
	}
	
	

	// Master-slave
	x = UI_TOP_X6 + xOffset;
	switch ( gPrefs.Get3DSync() )
	{
		case PREFS_3DSYNC_DISABLE:
			strcpy( str, "" );
			break;
		case PREFS_3DSYNC_MASTER:
			strcpy( str, "M" );
			break;
		case PREFS_3DSYNC_SLAVE:
			strcpy( str, "S" );
			break;
	}
	GIGE_STRINGOUTCENTER( (float) x, (float) y, str );

	// Gain
	x = UI_TOP_X7 + xOffset;
	if ( isProgramPlayingAVI() || isProgramPlayingSIV() )
	{
		sprintf( str,"%ddB", Globals.GetGainValue() );
		if ( ! gPrefs.GetDebugMode() )
			GIGE_STRINGOUTCENTER( (float) x, (float) y, str );
	}
	else
	{
		sprintf( str,"%ddB", gPrefs.GetGain() );
		if ( ! gPrefs.GetDebugMode() )
			GIGE_STRINGOUTCENTER( (float) x, (float) y, str );
	}

	// Zoom
	x = UI_TOP_X8 + xOffset;
	if ( Globals.GetZoomMode() == 1 )
		GIGE_SETFONTCOLOR( 1, 1, 1 );
	else
		GIGE_SETFONTCOLOR( 1, 1, 0 );

	if ( gPrefs.GetWorkingDrawingMode() == PREFS_DISPLAY_BAYER_QUADLET )
	{
		sprintf( str, "QUAD-%dX", Globals.GetZoomMode() );
	}
	else if ( gPrefs.GetWorkingDrawingMode() == PREFS_DISPLAY_BAYER_HEXLET )
	{
		sprintf( str, "HEX-%dX", Globals.GetZoomMode() );
	}
	else
	{
		sprintf( str, "BILIN-%dX", Globals.GetZoomMode() );
	}
	GIGE_STRINGOUTCENTER( (float) x, (float) y, str );

	// White balance
	x = UI_TOP_X9 + xOffset;
	if ( isProgramPlayingAVI() || isProgramPlayingSIV() )
	{
		switch( Globals.GetWhiteBalancePlayMode() )
		{
		case PREFS_WB_3200K:
			strcpy(str,"3200K");
			break;

		case PREFS_WB_5600K:
			strcpy(str,"5600K");
			break;

		default:
			strcpy(str,"CUSTOM");
			break;
		}
	}
	else
	{
		switch( gPrefs.GetWhiteBalance() )
		{
			case PREFS_WB_3200K:
				strcpy(str,"3200K");
				break;

			case PREFS_WB_5600K:
				strcpy(str,"5600K");
				break;

			case PREFS_WB_USER_1:
				strcpy(str,"User WB 1");
				break;

			case PREFS_WB_USER_2:
				strcpy( str, "User WB 2" );
				break;

			case PREFS_WB_NONE:
				strcpy( str, "NO WB" );
				break;
		}
	}
	
    GIGE_STRINGOUTCENTER((float)(x), (float)y, str );


	// Shutter
	x = UI_TOP_RIGHT + xOffset;
	if ( isProgramPlayingAVI() || isProgramPlayingSIV() )
	{
		sprintf( str,"1/%d", Globals.GetShutterSpeed() );
	}
	else
	{
		/*if ( Globals.GetCameraRaster() == CAMRASTER_DCI )
		{
			if ( Globals.GetCameraFramerate() == CAMFRAMERATE_23_360 )
				sprintf( str,"1/24" );
			else if ( Globals.GetCameraFramerate() == CAMFRAMERATE_24_360 )
				sprintf( str,"1/24" );
			else if ( Globals.GetCameraFramerate() == CAMFRAMERATE_25_360 )
				sprintf( str,"1/25" );
			else
				sprintf( str,"1/%d", (int) ( gPrefs.GetShutterFPS() + 0.82f ) );
		}
		else*/
		sprintf( str,"1/%d", (int) ( gPrefs.GetShutterFPS() + 0.82f ) );
	}
    GIGE_STRINGOUTRIGHT((float)( x - 15 ), (float) y, str );
	gD3D->SetFontSize( 12 );
	GIGE_STRINGOUTRIGHT((float)(x), (float) y, "th" );
	gD3D->SetFontSize( 18 );

	// Timecode
	char timecode[32];
	strcpy( timecode, "" );
	x = UI_TOP_CENTER + xOffset;

	if ( mVideoDisplayMode == VIDEO_DISPLAY_NORMAL )
	{
		// Only erase background when in normal or play mode
		GIGE_SETCOLOR3( 0, 0, 0 );
		CUIVisibleButton::RenderRectScale(x-88,y+4,176,24);
	}
	gTimecodeController.displayedTimecode()->String( timecode );

	GIGE_STRINGOUTCENTER((float)(x), (float)y, timecode );
}

void CUIScreen::RenderRAMBuffer( void )
{
	int total = 0;
	int state_counts[ BUFFER_STATE_LAST + 1 ];
	int dual_counts [ BUFFER_STATE_LAST + 1 ];
	float gap = 18;
	float fudge = 4;
	float sx = METER2_X + 3 + fudge;
	float sy = 424+3;
	float sw = METER_W - 6;
	float sh = 195.0f;
	int count;

	int last_count = 0;

	if ( !gPrefs.GetMode800() )
		gD3D->SetFontSize( 12 );
	else
		gD3D->SetFontSize( 7 );
    if ( dgOK )
	    gConfig.BufferState( state_counts, dual_counts );
    else
	    gConfig.BufferState( state_counts, NULL );

    for ( int i = 0; i < BUFFER_STATE_LAST+1; i++ )
	    total += state_counts[i];

	char pct[16];
	if ( total )
	{
		sprintf( pct, "%d", ( total-state_counts[ 0 ] ) * 100 / total );
		strcat( pct, "%" );
	}
	else
		strcpy( pct, "0%" );

	GIGE_STRINGOUTCENTER( (float) (METER2_C) + fudge, 403.0f, pct );

	gD3D->SetFontSize( 12 );
	GIGE_STRINGOUTCENTER( (float) (METER2_C) + fudge, 635.0f, "RAM" );

    if ( !gDualBufferManager[0] ) 
    {
	    for ( int i = 0; i < BUFFER_STATE_LAST+1; i++ )
	    {
		    count = state_counts[i];
		    if ( count )
		    {
			    float y1 = (float) last_count / (float) total;
			    last_count += count;
			    float y2 = (float) last_count / (float) total;
			    GIGE_SETCOLOR3( scol[i][0], scol[i][1], scol[i][2] );
			    CUIVisibleButton::RenderRectScale(sx,sy+sh*y1,sw,sh*y2-sh*y1);
		    }
	    }
    }
    else
    {
	    for ( int i = 0; i < BUFFER_STATE_LAST+1; i++ )
	    {
		    count = state_counts[i];
		    if ( count )
		    {
			    float y1 = (float) last_count / (float) total;
			    last_count += count;
			    float y2 = (float) last_count / (float) total;
			    GIGE_SETCOLOR3( scol[i][0], scol[i][1], scol[i][2] );
			    CUIVisibleButton::RenderRectScale(sx,sy+sh*y1,sw/2.0f,sh*y2-sh*y1);
		    }
	    }
        total = 0;
    	last_count = 0;
        for ( int i = 0; i < BUFFER_STATE_LAST+1; i++ )
	        total += dual_counts[i];
    	for ( int i = 0; i < BUFFER_STATE_LAST+1; i++ )
	    {
    		count = dual_counts[i];
	    	if ( count )
		    {
			    float y1 = (float) last_count / (float) total;
			    last_count += count;
			    float y2 = (float) last_count / (float) total;
			    GIGE_SETCOLOR3( scol[i][0], scol[i][1], scol[i][2] );
			    CUIVisibleButton::RenderRectScale(sx+sw/2.0f,sy+sh*y1,sw/2.0f,sh*y2-sh*y1);
		    }
	    }
    }
}

void CUIScreen::RenderRAMBufferFullScreen()
{
	int total = 0;
	int state_counts[ BUFFER_STATE_LAST + 1 ];
	int dual_counts[ BUFFER_STATE_LAST + 1 ];
	float gap = 9;
	float fudge = 2;
	float x = 0.0f;
	float y = 0.0f;
	float w = INNER_CONTAINER_FS_W;
	float h = INNER_CONTAINER_FS_H + 2;

	x = ( gConfig.mAppFullW * 0.33f );
	y = gConfig.mAppFullH + gConfig.mAppFullOffsetY - 20;

	if ( gPrefs.GetMode800() )
	{
		x *= Globals.mMiniGuiMag;
		y *= Globals.mMiniGuiMag;
	}
	
	GIGE_SETCOLOR3( 128, 128, 128 );
	CUIVisibleButton::RenderRectScale( x, y - 1, CONTAINER_FS_W, CONTAINER_FS_H + 2 );

	GIGE_SETCOLOR3( 0.18f, 0.18f, 0.18f );
	CUIVisibleButton::RenderRectScale( x + fudge, y + fudge, CONTAINER_FS_W - 4, INNER_CONTAINER_FS_H + 1 );

	int count = 0;
	int last_count = 0;

	if ( ! gPrefs.GetMode800() )
	{
		gD3D->SetFontSize( 12 );
	}
	else
	{
		gD3D->SetFontSize( 7 );
	}

	x += 2;

    if ( dgOK )
	{
	    gConfig.BufferState( state_counts, dual_counts );
	}
    else
	{
	    gConfig.BufferState( state_counts, NULL );
	}

    for ( int i = 0; i < BUFFER_STATE_LAST + 1; i++ )
	{
	    total += state_counts[ i ];
	}
	
	char pct[16];
	if ( total )
	{
		sprintf( pct,"%d", ( total-state_counts[ 0 ] ) * 100 / total );
		strcat( pct,"%" );
	}
	else
	{
		strcpy( pct,"0%" );
	}
	
	gD3D->SetFontSize( 12 );
	GIGE_STRINGOUTCENTER( x + CONTAINER_FS_W + 25, y - 2, pct );
	GIGE_STRINGOUTCENTER( x - 33, y - 2, "RAM" );
	
	
	if ( ! gDualBufferManager[ 0 ] ) 
    {
		for ( int i = 0; i < BUFFER_STATE_LAST + 1; i++ )
		{
			count = state_counts[ i ];
			if ( count )
			{
				float w1 = (float) last_count / (float) total;
				last_count += count;
				float w2 = (float) last_count / (float) total;

				GIGE_SETCOLOR3( scol[ i ][ 0 ], scol[ i ][ 1 ], scol[ i ][ 2 ] );
				CUIVisibleButton::RenderRectScale( ( x - w * w1 ) + CONTAINER_FS_W - 4, y + 1, ( ( w * w1 ) - ( w * w2 ) ), h - 1 );
			}
		}
	}
	else
	{
		for ( int i = 0; i < BUFFER_STATE_LAST + 1; i++ )
	    {
		    count = state_counts[ i ];
		    if ( count )
		    {
			    float w1 = (float) last_count / (float) total;
			    last_count += count;
			    float w2 = (float) last_count / (float) total;
			    
				GIGE_SETCOLOR3( scol[ i ][ 0 ], scol[ i ][ 1 ], scol[ i ][ 2 ] );
				CUIVisibleButton::RenderRectScale( ( x - w * w1 ) + CONTAINER_FS_W - 4, y + 1, ( ( w * w1 ) - ( w * w2 ) ), h / 2 );
			}
	    }

        total = 0;
    	last_count = 0;

        for ( int i = 0; i < BUFFER_STATE_LAST + 1; i++ )
		{
	        total += dual_counts[ i ];
		}

    	for ( int i = 0; i < BUFFER_STATE_LAST + 1; i++ )
	    {
    		count = dual_counts[ i ];
	    	if ( count )
		    {
			    float w1 = (float) last_count / (float) total;
			    last_count += count;
			    float w2 = (float) last_count / (float) total;
			    
				GIGE_SETCOLOR3( scol[ i ][ 0 ], scol[ i ][ 1 ], scol[ i ][ 2 ] );
				CUIVisibleButton::RenderRectScale( ( x - w * w1 ) + CONTAINER_FS_W - 4, y + 7, ( ( w * w1 ) - ( w * w2 ) ), h / 2 );
		    }
	    }
	}
}

void CUIScreen::RenderDiskMeterFullScreen()
{
	float x = 0.0f;
	float y = 0.0f;
	float sh = 195;
	float gap = 9;
	float fudge = 1;
	char gigs[32] = "\0";
	static int gb_tenth = 0;
	static int gb_int = 0;

	if ( gPrefs.GetScratchAudio() )
	{
		x = ( gConfig.mAppFullW * 0.33f );
		y = gConfig.mAppFullH + gConfig.mAppFullOffsetY - 40;
	}
	else
	{
		x = ( gConfig.mAppFullW * 0.55f );
		y = gConfig.mAppFullH + gConfig.mAppFullOffsetY - 20;
	}

	if ( gPrefs.GetMode800() )
	{
		x *= Globals.mMiniGuiMag;
		y *= Globals.mMiniGuiMag;
	}

	GIGE_SETFONTCOLOR( 1, 1, 1 );

	// Disk meter
	GIGE_SETCOLOR3( 128, 128, 128 );
	CUIVisibleButton::RenderRectScale( x, y - 1, CONTAINER_FS_W, CONTAINER_FS_H + 1 );

	if ( gPrefs.GetMode800() )
	{
		if ( gConfig.mMonitorH[ 0 ] > 600 )
		{
			CUIVisibleButton::RenderRectScale( x, y - 1, CONTAINER_FS_W, CONTAINER_FS_H + 2 );
		}
	}

	GIGE_SETCOLOR3( 0.18f, 0.18f, 0.18f );
	CUIVisibleButton::RenderRectScale( x + fudge, y + fudge, CONTAINER_FS_W - 4, INNER_CONTAINER_FS_H + 1 );

    for ( int i = 0; i < gDiskInfo.numDrives(); i++ )
    {
	    float free_gb = 0;
		float bandwidth = 0.0f;
        free_gb = gDiskInfo.mDiskFreeMB[i] / 1024.0f;
	    gb_int = (int) free_gb;
	    free_gb -= (float) gb_int;
	    free_gb *= 10;
	    gb_tenth = (int) floor( free_gb );
	    float dw = ( sh * gDiskInfo.mDiskPct[i] / 100.0f ) * 0.7179f;
        
		if ( ! isProgramConfigured() )
		{
			bandwidth = 5.25f;
		}
		else
		{
			bandwidth =  ( ( Globals.GetAvgBandwidth() * 60 ) / 1000 );
		}

		float time = ( ( gDiskInfo.mDiskFreeMB[i] - 2000.0f ) / 1024.0f ) / bandwidth;

		if ( time < 0 )
		{
			time = 0.0f;
		}

		if ( gDiskInfo.numDrives() > 1 )
		{
			time *= 2;
		}

        if ( Globals.GetTimeMeter() )
		{
			sprintf( gigs, "%4.1f MIN", time );
			mRightPanelInfo.time = time;
		}
		else
		{
			if ( i == 0 ) 
				sprintf( gigs, "%d.%1d GB", gb_int, gb_tenth );
			else if ( gDiskInfo.mDiskFreeMB[i] < gDiskInfo.mDiskFreeMB[0] )
				sprintf( gigs, "%d.%1dGB", gb_int, gb_tenth );
		}
	
		if ( gDiskInfo.mDiskFreeMB[i] < 5120 )
		{
			GIGE_SETCOLOR3( 0.9f, 0, 0 );
			if ( gProject )
			{
				mRightPanelInfo.drive_full = true;
			}
		}
		else
		{
			GIGE_SETCOLOR3( 0, 0.77f, 0 );
			mRightPanelInfo.drive_full = false;
		}
		
	    CUIVisibleButton::RenderRectScale( x + 1 + (float) i * dw / gDiskInfo.numDrives() , 
										   y + 1, 
										   dw / gDiskInfo.numDrives(), 
										   INNER_CONTAINER_FS_H + 1 );
    }

	gD3D->SetFontSize( 12 );
	GIGE_STRINGOUTCENTER( x + CONTAINER_FS_W + 45, y - 2, gigs );
	
	if ( Globals.GetTimeMeter() )
	{
		GIGE_STRINGOUTCENTER( x - 30, y - 2, "TIME" );
	}
	else
		GIGE_STRINGOUTCENTER( x - 30, y - 2, "DISK" );
}

void CUIScreen::RenderAudioMeter()
{
	int l = 0;
	int r = 0;
	int peakL = 0;
	int peakR = 0;

	if ( gAudioPlayback )
	{
		gAudioPlayback->GetCurrentPeakValues( &l, &r );
		// gLogger.info( 1, "%d RenderAudioMeter SIV LEFT %d RIGHT %d\n", GetTickCount(), l, r );
	}
	else if ( isProgramPlayingAVI() )
	{
		gPlayWnd.GetAVIAudioPeakValues( &l, &r );
		// gLogger.info( 1, "%d RenderAudioMeter AVI LEFT %d RIGHT %d\n", GetTickCount(), l, r );
	}
	else
	{
		gConfig.GetAudioPeak( &l, &r );
		// gLogger.info( 1, "%d RenderAudioMeter CONFIG LEFT %d RIGHT %d\n", GetTickCount(), l, r );
	}

	if ( l == 0x7fff ) 
	{
		mAudioOverTicks = GetTickCount() + 2000;
	}
	else if ( r == 0x7fff ) 
	{
		mAudioOverTicks = GetTickCount() + 2000;
	}

	if ( ( l > 0x7fff ) || ( l < 0 ) ) 
	{
		l = 0;
	}
	if ( ( r > 0x7fff ) || ( r < 0 ) )
	{
		r = 0;
	}

	peakL = mAudioHeight[l];
	peakR = mAudioHeight[r];

	// left channel border
	float x = METER1_X;

	if ( gPrefs.GetMode800() )
		x -= 6;

	float y = CONTAINER_Y;
	GIGE_SETCOLOR3( 0.6f, 0.6f, 0.6f );
	CUIVisibleButton::RenderRectScale(x,y,CONTAINER_W,CONTAINER_H);

	x += CONTAINER_BORDER;
	y += CONTAINER_BORDER;
	GIGE_SETCOLOR3( 0.27f, 0, 0 );
	CUIVisibleButton::RenderRectScale(
		x,y,INNER_CONTAINER_W,CONTAINER_RED_H);

	y += CONTAINER_RED_H;
	GIGE_SETCOLOR3( 0.27f, 0.27f, 0 );
	CUIVisibleButton::RenderRectScale(
		x,y,INNER_CONTAINER_W,CONTAINER_YELLOW_H);

	y += CONTAINER_YELLOW_H;
	GIGE_SETCOLOR3( 0, 0.25f, 0.0625 );
	CUIVisibleButton::RenderRectScale(
		x,y,INNER_CONTAINER_W,CONTAINER_GREEN_H);

	// Draw full left channel
	y = CONTAINER_Y + CONTAINER_BORDER;
	if ( peakL < CONTAINER_RED_H )
	{
		float temp = CONTAINER_RED_H-(float)peakL;
		GIGE_SETCOLOR3( 1, 0, 0 );
		CUIVisibleButton::RenderRectScale(x,y+CONTAINER_RED_H-temp,INNER_CONTAINER_W,temp);
		GIGE_SETCOLOR3( 1, 1, 0 );
		CUIVisibleButton::RenderRectScale(x,y+CONTAINER_RED_H,INNER_CONTAINER_W,CONTAINER_YELLOW_H);
		GIGE_SETCOLOR3( 0, 1, 0 );
		CUIVisibleButton::RenderRectScale(x,y+CONTAINER_RED_H+CONTAINER_YELLOW_H,INNER_CONTAINER_W,CONTAINER_GREEN_H);
	}
	else if ( peakL < ( CONTAINER_YELLOW_H + CONTAINER_RED_H ) )
	{
		float temp = (CONTAINER_YELLOW_H+CONTAINER_RED_H)-(float)peakL;
		GIGE_SETCOLOR3( 1, 1, 0 );
		CUIVisibleButton::RenderRectScale(x,y+CONTAINER_RED_H+CONTAINER_YELLOW_H-temp,INNER_CONTAINER_W,temp);
		GIGE_SETCOLOR3( 0, 1, 0 );
		CUIVisibleButton::RenderRectScale(x,y+CONTAINER_RED_H+CONTAINER_YELLOW_H,INNER_CONTAINER_W,CONTAINER_GREEN_H);
	}
	else
	{
		float temp = INNER_CONTAINER_H-(float)peakL;
		GIGE_SETCOLOR3( 0, 1, 0 );
		CUIVisibleButton::RenderRectScale(x,y+INNER_CONTAINER_H-temp,INNER_CONTAINER_W,temp);
	}

	// right channel border
	x = METER2_X;
	if ( gPrefs.GetMode800() )
		x += 6;
	y = CONTAINER_Y;
	GIGE_SETCOLOR3( 0.6f, 0.6f, 0.6f );
	CUIVisibleButton::RenderRectScale(x,y,CONTAINER_W,CONTAINER_H);

	x += CONTAINER_BORDER;
	y += CONTAINER_BORDER;
	GIGE_SETCOLOR3( 0.27f, 0, 0 );
	CUIVisibleButton::RenderRectScale(
		x,y,INNER_CONTAINER_W,CONTAINER_RED_H);

	y += CONTAINER_RED_H;
	GIGE_SETCOLOR3( 0.27f, 0.27f, 0 );
	CUIVisibleButton::RenderRectScale(
		x,y,INNER_CONTAINER_W,CONTAINER_YELLOW_H);

	y += CONTAINER_YELLOW_H;
	GIGE_SETCOLOR3( 0, 0.25f, 0.0625 );
	CUIVisibleButton::RenderRectScale(
		x,y,INNER_CONTAINER_W,CONTAINER_GREEN_H);

	// Draw full right channel
	y = CONTAINER_Y + CONTAINER_BORDER;
	if ( peakR < CONTAINER_RED_H )
	{
		float temp = CONTAINER_RED_H-(float)peakR;
		GIGE_SETCOLOR3( 1, 0, 0 );
		CUIVisibleButton::RenderRectScale(x,y+CONTAINER_RED_H-temp,INNER_CONTAINER_W,temp);
		GIGE_SETCOLOR3( 1, 1, 0 );
		CUIVisibleButton::RenderRectScale(x,y+CONTAINER_RED_H,INNER_CONTAINER_W,CONTAINER_YELLOW_H);
		GIGE_SETCOLOR3( 0, 1, 0 );
		CUIVisibleButton::RenderRectScale(
			x,
			y+CONTAINER_RED_H+CONTAINER_YELLOW_H,
			INNER_CONTAINER_W,
			CONTAINER_GREEN_H);
	}
	else if ( peakR < (CONTAINER_YELLOW_H+CONTAINER_RED_H) )
	{
		float temp = (CONTAINER_YELLOW_H+CONTAINER_RED_H)-(float)peakR;
		GIGE_SETCOLOR3( 1, 1, 0 );
		CUIVisibleButton::RenderRectScale( x, y + CONTAINER_RED_H + CONTAINER_YELLOW_H - temp, INNER_CONTAINER_W, temp );
		GIGE_SETCOLOR3( 0, 1, 0 );
		CUIVisibleButton::RenderRectScale( x, y + CONTAINER_RED_H + CONTAINER_YELLOW_H, INNER_CONTAINER_W, CONTAINER_GREEN_H );
	}
	else
	{
		float temp = INNER_CONTAINER_H-(float)peakR;
		GIGE_SETCOLOR3( 0, 1, 0 );
		CUIVisibleButton::RenderRectScale( x, y + INNER_CONTAINER_H - temp, INNER_CONTAINER_W, temp );
	}

	// Draw input level indicator
	DWORD inputLevel = Globals.GetAudioInputLevel();
	y = 350;
	int pos = inputLevel / 335;
	GIGE_SETCOLOR3( 1, 1, 0 );

	if ( gPrefs.GetMode800() )
	{
		CUIVisibleButton::RenderRectScale( x - 67, y - pos, AUDIO_LEVEL_SLIDER_W, 4 );
		CUIVisibleButton::RenderRectScale( x - 4, y - pos, AUDIO_LEVEL_SLIDER_W, 4 );
	}
	else
	{
		CUIVisibleButton::RenderRectScale( x - 55, y - pos, AUDIO_LEVEL_SLIDER_W, 4 );
		CUIVisibleButton::RenderRectScale( x - 5, y - pos, AUDIO_LEVEL_SLIDER_W, 4 );
	}
	

	// Draw left side text
	gD3D->SetFontSize( 12 );
	GIGE_SETFONTCOLOR(1, 1, 1);

	x = (METER1_C + METER2_C) / 2;
	y = 131;

	GIGE_STRINGOUTCENTER( x, y, "db" );

	if ( !gPrefs.GetMode800() )
		GIGE_STRINGOUTCENTER( METER1_C, y, "L" );
	else
		GIGE_STRINGOUTCENTER( METER1_C-6, y, "L" );
	if ( !gPrefs.GetMode800() )
		GIGE_STRINGOUTCENTER( METER2_C, y, "R" );
	else
		GIGE_STRINGOUTCENTER( METER2_C+6, y, "R" );

	y += 17;
	GIGE_STRINGOUTCENTER((float)(x), (float)y, "-0" );

	y += 16;
	GIGE_STRINGOUTCENTER((float)(x), (float)y, "-6" );

	y += 23;
	GIGE_STRINGOUTCENTER((float)(x), (float)y, "-10" );
	y += 26;
	GIGE_STRINGOUTCENTER((float)(x), (float)y, "-14" );

	y += 31;
	GIGE_STRINGOUTCENTER((float)(x), (float)y, "-18" );

	y += 30;
	GIGE_STRINGOUTCENTER((float)(x), (float)y, "-22" );

	y += 34;
	GIGE_STRINGOUTCENTER((float)(x), (float)y, "-30" );
	y += 32;
	GIGE_STRINGOUTCENTER((float)(x), (float)y, "-60" );

	y += 25;
	GIGE_STRINGOUTCENTER( (float) ( x ), (float) ( y ), "AUDIO" );

	x = (METER1_C + METER2_C)/2;
	y = 365;
	GIGE_SETFONTCOLOR(1, 1, 1);

	// Draw over button
	GIGE_SETCOLOR3( 0.22f, 0, 0 );
	x = OVER_X;
	y = OVER_Y;
	CUIVisibleButton::RenderRectScale( x, y, OVER_W, OVER_H );
	GIGE_SETFONTCOLOR( 0.33f, 0.33f, 0.33f );
	GIGE_STRINGOUTCENTER( (float) ( x + 32 - 2 ), (float) y + 3.0f, "OVER" );

	// Over button
	if ( mAudioOverTicks )
	{
		if ( mAudioOverTicks > GetTickCount() )
		{
			// Draw over button
			GIGE_SETCOLOR3( 0.77f, 0, 0 );
			x = OVER_X;
			y = OVER_Y;
			CUIVisibleButton::RenderRectScale( x, y, OVER_W, OVER_H );
			GIGE_SETFONTCOLOR(1, 1, 1);
			GIGE_STRINGOUTCENTER((float)( x + 32 - 2 ), (float) y + 3.0f, "OVER" );
		}
		else
		{
			// Erase OVER button
			GIGE_SETCOLOR3( 0.22f, 0, 0 );
			CUIVisibleButton::RenderRectScale( OVER_X, OVER_Y, OVER_W, OVER_H );
			GIGE_SETFONTCOLOR( 0.33f, 0.33f, 0.33f );
			mAudioOverTicks = 0;
		}
	}
}

void CUIScreen::RenderAudioMeterFullScreen()
{
	int l = 0;
	int r = 0;
	int peakL = 0;
	int peakR = 0;
	float x = 0.0f;
	float y = 0.0f;
	float fudge = 1;

	if ( gAudioPlayback )
	{
		gAudioPlayback->GetCurrentPeakValues( &l, &r );
	}
	else if ( isProgramPlayingAVI() )
	{
		gPlayWnd.GetAVIAudioPeakValues( &l, &r );
		gLogger.info( 1, "%d RenderAudioMeter AVI LEFT %d RIGHT %d\n", GetTickCount(), l, r );
	}
	else
	{
		gConfig.GetAudioPeak( &l, &r );
	}

	if ( l == 0x7fff ) 
	{
		mAudioOverTicks = GetTickCount() + 2000;
	}
	else if ( r == 0x7fff ) 
	{
		mAudioOverTicks = GetTickCount() + 2000;
	}

	if ( ( l > 0x7fff ) || ( l < 0 ) ) 
	{
		l = 0;
	}
	if ( ( r > 0x7fff ) || ( r < 0 ) )
	{
		r = 0;
	}

	gLogger.info( 3, "%d RenderAudioMeter LEFT %d RIGHT %d\n", GetTickCount(), l, r );

	peakL = mAudioHeight[l];
	peakR = mAudioHeight[r];

	peakL = (int)( (float) peakL * 0.7179f );
	peakR = (int)( (float) peakR * 0.7179f );

	x = ( gConfig.mAppFullW * 0.6f );
	y = gConfig.mAppFullH + gConfig.mAppFullOffsetY - 20;

	if ( gPrefs.GetMode800() )
	{
		x *= Globals.mMiniGuiMag;
		y *= Globals.mMiniGuiMag;
	}

	GIGE_SETCOLOR3( 0.6f, 0.6f, 0.6f );
	CUIVisibleButton::RenderRectScale( x - 2, y - 1, CONTAINER_FS_W, CONTAINER_FS_H );

	if ( gPrefs.GetMode800() )
	{
		if ( gConfig.mMonitorH[ 0 ] == 600 )
		{
			CUIVisibleButton::RenderRectScale( x - 2, y - 2, CONTAINER_FS_W, CONTAINER_FS_H );
		}
	}

	GIGE_SETCOLOR3( 0, 0.25f, 0.0625f );
	CUIVisibleButton::RenderRectScale( x, y, CONTAINER_FS_GREEN_W, INNER_CONTAINER_FS_H );

	x += CONTAINER_FS_GREEN_W;
	GIGE_SETCOLOR3( 0.27f, 0.27f, 0 );
	CUIVisibleButton::RenderRectScale( x, y, CONTAINER_FS_YELLOW_W, INNER_CONTAINER_FS_H );

	x += CONTAINER_FS_YELLOW_W;
	GIGE_SETCOLOR3( 0.27f, 0, 0 );
	CUIVisibleButton::RenderRectScale( x, y, CONTAINER_FS_RED_W, INNER_CONTAINER_FS_H );

	// Draw full left channel
	x = ( gConfig.mAppFullW * 0.6f );
	if ( gPrefs.GetMode800() )
	{
		x *= Globals.mMiniGuiMag;
	}

	if ( peakL < CONTAINER_FS_RED_W )
	{
		// Green - yellow - red
		float temp = CONTAINER_FS_RED_W - (float) peakL;
		GIGE_SETCOLOR3( 1, 0, 0 );
		CUIVisibleButton::RenderRectScale( x + CONTAINER_FS_GREEN_W + CONTAINER_FS_YELLOW_W, y, temp, INNER_CONTAINER_FS_H );
		GIGE_SETCOLOR3( 1, 1, 0 );
		CUIVisibleButton::RenderRectScale( x + CONTAINER_FS_GREEN_W, y, CONTAINER_FS_YELLOW_W, INNER_CONTAINER_FS_H );
		GIGE_SETCOLOR3( 0, 1, 0 );
		CUIVisibleButton::RenderRectScale( x, y, CONTAINER_FS_GREEN_W, INNER_CONTAINER_FS_H );
	}
	else if ( peakL < ( CONTAINER_FS_YELLOW_W + CONTAINER_FS_RED_W) )
	{
		// Green - yellow
		float temp = ( CONTAINER_FS_YELLOW_W + CONTAINER_FS_RED_W ) - (float) peakL;
		GIGE_SETCOLOR3( 1, 1, 0 );
		CUIVisibleButton::RenderRectScale( x + CONTAINER_FS_GREEN_W, y, temp, INNER_CONTAINER_FS_H );
		GIGE_SETCOLOR3( 0, 1, 0 );
		CUIVisibleButton::RenderRectScale( x, y, CONTAINER_FS_GREEN_W, INNER_CONTAINER_FS_H );
	}
	else
	{
		// Green
		float temp = ( INNER_CONTAINER_FS_W - (float) peakL );
		GIGE_SETCOLOR3( 0, 1, 0 );
		CUIVisibleButton::RenderRectScale( x, y, temp, INNER_CONTAINER_FS_H );
	}

	// Right channel border
	x = ( gConfig.mAppFullW * 0.6f );
	y = gConfig.mAppFullH + gConfig.mAppFullOffsetY - 40;

	if ( gPrefs.GetMode800() )
	{
		x *= Globals.mMiniGuiMag;
		y *= Globals.mMiniGuiMag;
	}

	GIGE_SETCOLOR3( 0.6f, 0.6f, 0.6f );
	CUIVisibleButton::RenderRectScale( x - 2, y - 2, CONTAINER_FS_W, CONTAINER_FS_H );

	if ( gPrefs.GetMode800() )
	{
		if ( gConfig.mMonitorH[ 0 ] == 600 )
		{
			CUIVisibleButton::RenderRectScale( x - 2, y + 1, CONTAINER_FS_W, CONTAINER_FS_H - 1 );
		}
	}

	GIGE_SETCOLOR3( 0, 0.25f, 0.0625 );
	CUIVisibleButton::RenderRectScale( x, y, CONTAINER_FS_GREEN_W, INNER_CONTAINER_FS_H );

	x += CONTAINER_FS_GREEN_W;
	GIGE_SETCOLOR3( 0.27f, 0.27f, 0 );
	CUIVisibleButton::RenderRectScale( x, y, CONTAINER_FS_YELLOW_W, INNER_CONTAINER_FS_H );

	x += CONTAINER_FS_YELLOW_W;
	GIGE_SETCOLOR3( 0.27f, 0, 0 );
	CUIVisibleButton::RenderRectScale( x, y, CONTAINER_FS_RED_W, INNER_CONTAINER_FS_H );

	// Draw full right channel
	x = ( gConfig.mAppFullW * 0.6f );
	if ( gPrefs.GetMode800() )
	{
		x *= Globals.mMiniGuiMag;
	}

	if ( peakR < CONTAINER_FS_RED_W )
	{
		// Green - yellow - red
		float temp = CONTAINER_FS_RED_W - (float) peakR;
		GIGE_SETCOLOR3( 1, 0, 0 );
		CUIVisibleButton::RenderRectScale( x + CONTAINER_FS_GREEN_W + CONTAINER_FS_YELLOW_W, y, temp, INNER_CONTAINER_FS_H + 1);
		GIGE_SETCOLOR3( 1, 1, 0 );
		CUIVisibleButton::RenderRectScale( x + CONTAINER_FS_GREEN_W, y, CONTAINER_FS_YELLOW_W, INNER_CONTAINER_FS_H + 1);
		GIGE_SETCOLOR3( 0, 1, 0 );
		CUIVisibleButton::RenderRectScale( x, y, CONTAINER_FS_GREEN_W, INNER_CONTAINER_FS_H + 1);
	}
	else if ( peakR < ( CONTAINER_FS_YELLOW_W + CONTAINER_FS_RED_W) )
	{
		// Green - yellow
		float temp = ( CONTAINER_FS_YELLOW_W + CONTAINER_FS_RED_W ) - (float) peakR;
		GIGE_SETCOLOR3( 1, 1, 0 );
		CUIVisibleButton::RenderRectScale( x + CONTAINER_FS_GREEN_W, y, temp, INNER_CONTAINER_FS_H + 1);
		GIGE_SETCOLOR3( 0, 1, 0 );
		CUIVisibleButton::RenderRectScale( x, y, CONTAINER_FS_GREEN_W, INNER_CONTAINER_FS_H + 1);
	}
	else
	{
		// Green
		float temp = ( INNER_CONTAINER_FS_W - (float) peakR );
		GIGE_SETCOLOR3( 0, 1, 0 );
		CUIVisibleButton::RenderRectScale( x, y, temp, INNER_CONTAINER_FS_H + 1);
	}

	// Indicators
	x = x + CONTAINER_FS_W + 20;

	GIGE_SETFONTCOLOR( 1, 1, 1 );
	GIGE_STRINGOUTCENTER( x - CONTAINER_FS_W - 55, y + 4, "AUDIO" );
	GIGE_STRINGOUTCENTER( x, y - 4, "L" );
	GIGE_STRINGOUTCENTER( x, y + 16, "R" );

	// Over button
	x += 55;
	y -= 15;
	if ( mAudioOverTicks )
	{
		if ( mAudioOverTicks > GetTickCount() )
		{
			// Draw OVER button
			GIGE_SETCOLOR3( 0.77f, 0, 0 );
			CUIVisibleButton::RenderRectScale( x, y, OVER_FS_W, OVER_FS_H );
			GIGE_SETFONTCOLOR( 1, 1, 1 );
			GIGE_STRINGOUTCENTER( (float) ( x + 24 ), (float) y + 1.0f, "OVER" );
		}
		else
		{
			// Erase OVER button
			GIGE_SETCOLOR3( 0, 0, 0 );
			CUIVisibleButton::RenderRectScale( x, y, OVER_FS_W, OVER_FS_H );
			mAudioOverTicks = 0;
		}
	}
}


void CUIScreen::RenderMeterPanel( void )
{
	float x = 1223;
	float y = 597;
	float w;
	float gap = 18;
	float fudge = 4;

	GIGE_SETFONTCOLOR(1, 1, 1);

	GIGE_SETCOLOR3( 128, 128, 128 );
	CUIVisibleButton::RenderRectScale( METER2_X + fudge, 424, METER_W, 201 );
	GIGE_SETCOLOR3( 0, 0, 0 );
	CUIVisibleButton::RenderRectScale( METER2_X + 3 + fudge, 424 + 3, METER_W - 6, 201 - 6 );
	
	RenderRAMBuffer();

	// Disk meter and remaining time panel
	char gigs[32];

	static int gb_int = 0;
	static int gb_tenth = 0;

    // Only erase background when in normal or play mode
    GIGE_SETCOLOR3( 128, 128, 128 );
    CUIVisibleButton::RenderRectScale( METER1_X, 424, METER_W, 201 );

    x = METER1_X + 3;
    y = 427;
    w = METER_W - 6;
    GIGE_SETCOLOR3( 0, 0, 0 );
    CUIVisibleButton::RenderRectScale( x, y, w, 195 );
    y += 195;

    for ( int i = 0; i < gDiskInfo.numDrives(); i++ )
    {
        float free_gb = 0;
		float bandwidth = 0.0f;
        free_gb = gDiskInfo.mDiskFreeMB[i] / 1024.0f;
	    gb_int = (int) free_gb;
	    free_gb -= (float) gb_int;
	    free_gb *= 10;
	    gb_tenth = (int) floor( free_gb );

		if ( ! isProgramConfigured() )
		{
			bandwidth = 5.25f;
		}
		else
		{
			bandwidth =  ( ( Globals.GetAvgBandwidth() * 60 ) / 1000 );
		}

		float time = ( ( gDiskInfo.mDiskFreeMB[i] - 2000.0f ) / 1024.0f ) / bandwidth;

		if ( time < 0 )
		{
			time = 0.0f;
		}

		if ( gDiskInfo.numDrives() > 1 )
		{
			time *= 2;
		}

        if ( Globals.GetTimeMeter() )
		{
			sprintf( gigs, "%4.1f MIN", time );
			mRightPanelInfo.time = time;
		}
		else
		{
			if ( i == 0 ) 
				sprintf( gigs, "%d.%1d GB", gb_int, gb_tenth );
			else if ( gDiskInfo.mDiskFreeMB[i] < gDiskInfo.mDiskFreeMB[0] )
				sprintf( gigs, "%d.%1dGB", gb_int, gb_tenth );
		}
	
		if ( gDiskInfo.mDiskFreeMB[i] < 5120 )
		{
			GIGE_SETCOLOR3( 0.9f, 0, 0 );
			if ( gProject )
			{
				mRightPanelInfo.drive_full = true;
			}
		}
		else
		{
			GIGE_SETCOLOR3( 0, 0.77f, 0 );
			mRightPanelInfo.drive_full = false;
		}

	    float sh = 195;
	    float dh = sh * gDiskInfo.mDiskPct[i] / 100.0f;

	    CUIVisibleButton::RenderRectScale( x + (float) i * w / gDiskInfo.numDrives(),
										   y - dh,
										   w / gDiskInfo.numDrives(),
										   dh );
    }

	if ( !gPrefs.GetMode800() )
		gD3D->SetFontSize( 12 );
	else
		gD3D->SetFontSize( 7 );
	
	GIGE_STRINGOUTCENTER( METER1_C, 403.0f, gigs );

	gD3D->SetFontSize( 12 );

	if ( Globals.GetTimeMeter() )
	{
		GIGE_STRINGOUTCENTER( METER1_C, 635.0f, "TIME" );
	}
	else
	{
		GIGE_STRINGOUTCENTER( METER1_C, 635.0f, "DISK" );
	}

	RenderAudioMeter();
	gD3D->SetFontSize( 18 );
}

void CUIScreen::RenderBottomMeterPanel( void )
{
	RenderDiskMeterFullScreen();
	RenderRAMBufferFullScreen();

	if ( gPrefs.GetScratchAudio() )
	{
		RenderAudioMeterFullScreen();
	}

	gD3D->SetFontSize( 18 );
}

void CUIScreen::RenderAudioMeterPanel( void )
{
	GIGE_SETFONTCOLOR( 1, 1, 1 );
	GIGE_SETCOLOR3( 128, 128, 128 );

	RenderAudioMeter();

	gD3D->SetFontSize( 18 );
}


void CUIScreen::RenderExposureMeter( void )
{
	float x = 1210;
	float y = 106;
	float w = 35;
	float h = 506;

	GIGE_SETFONTCOLOR(1, 0, 0);

	GIGE_SETCOLOR3( 1, 1, 1 );
	CUIVisibleButton::RenderRectScale(x,y,w,h);

	x += 3;
	y += 3;
	w -= 6;
	h -= 6;
	GIGE_SETCOLOR3( 0, 0, 0 );
	CUIVisibleButton::RenderRectScale(x,y,w,h);

	gD3D->SetFontSize( 12 );
	GIGE_SETFONTCOLOR(1, 1, 1);

	for ( int i = 0; i < FC_NUM_BOXES; i++ )
	{
		//float tw = GIGE_GETTEXTWIDTH(fc_labels[i]);
		GIGE_STRINGOUTRIGHT((float)(x - 10), (float)y - 10.0f, fc_labels[i] );
		GIGE_SETCOLOR3( (float) fc_colors[i][0] / 255.0f, (float) fc_colors[i][1] / 255.0f, (float) fc_colors[i][2] / 255.0f );
		GIGE_SETCOLOR3B( (float) fc_colors[i+1][0] / 255.0f, (float) fc_colors[i+1][1] / 255.0f, (float) fc_colors[i+1][2] / 255.0f );
		CUIVisibleButton::RenderRectScale(x,y,w,(float)fc_heights[i]);
		y += fc_heights[i];
	}

	GIGE_STRINGOUTRIGHT((float)(x - 10), (float)y - 10.0f, fc_labels[FC_NUM_BOXES] );

	gD3D->SetFontSize( 18 );
}


void CUIScreen::Render( void )
{
	GIGE_STRINGOUT(10, 10, "" );
	if ( mFileDialog )
	{
		if ( ! mKeyboard )
		{
			mFileDialog->Render();
		}
		else
		{
			mKeyboard->Render();
		}
	}
	else if ( mMacroListDialog )
	{
		mMacroListDialog->Render();
	}
	else if ( mDisplayModesListDialog )
	{
		mDisplayModesListDialog->Render();
	}
	else if ( mKeyboard )
	{
		mKeyboard->Render();
	}
	else if ( gIridas )
	{
		gIridas->Render();
	}
	else if ( mPrefs && !mMessageBox )
	{
		mPrefs->Render();
	}
	else
	{
		if ( mVideoDisplayMode != VIDEO_DISPLAY_NORMAL )
		{
			if ( mVideoDisplayMode == VIDEO_DISPLAY_FULL_OVERLAY )
			{
				mFullPanel->Render();
				RenderTopPanel();

				// Render the bottom meter panel only if we are not playing back
				if ( ( mMode != SCREEN_MODE_PLAY ) && ( ! Globals.GetHistogramOn() ) )
				{
					RenderBottomMeterPanel();
				}

				if ( mMode == SCREEN_MODE_PLAY )
				{
					mFSPlayPanel->Render();
					mBottomPlayPanelFS->Render();
				}
				else
				{
					mFSRecPanel->Render();
				}

				if ( Globals.GetLoupeState() )
				{
					gLoupe->Render();
				}

				if ( isProgramPlayingAVI() || isProgramPlayingSIV() )
				{
					gTimeline.Render();
				}
				
				// Render the bottom meter panel only if we are not playing back
				if ( ( mMode != SCREEN_MODE_PLAY ) && ( ! Globals.GetHistogramOn() ) )
				{
					RenderBottomMeterPanel();
				}
				
				if ( ( Globals.GetHistogramOn() ) && ( mMode != SCREEN_MODE_PLAY ) )
				{
					if ( ( gDeviceManager.NumDevices() == 1 ) || ( ! Globals.GetDraw3D()) )
					{
						gHistogram.Render( true, HISTOGRAM_DISP_FULL, 0 );
					}
					else
					{
						gHistogram.Render( true, HISTOGRAM_DISP_L, 0 );
						gHistogram.Render( true, HISTOGRAM_DISP_R, 0 );
					}
				}
			}

			if ( mSceneShotTakeDialog )
			{
				mSceneShotTakeDialog->Render();
			}

			if ( mBlackDialog )
			{
				mBlackDialog->Render();
			}

			if ( mMessageBox )
			{
				if ( mMessageBox->GetType() == MB_TYPE_RAMOVERFLOWWRITE ||
					 mMessageBox->GetType() == MB_TYPE_SAVE_IRIDAS )
				{
					mMessageBox->Render();
				}
			}
			else if ( Globals.GetZoomMode() != 1 )
			{
				mFullZoomPanel->Render();
			}
		}
		else
		{
			if ( mMode == SCREEN_MODE_PREVIEW )
			{
				if ( !mUtilityMode )
				{
					mLeftPanel->Render();
					mBottomPanel->Render();
					if ( Globals.Flags & GLOBALS_FLAG_EXPOSUREMETER )
					{
						RenderExposureMeter();
					}
					else if ( mFrameStoreMode )
					{
						mRightFramestorePanel->Render();
					}
					else if ( mProjectManagerMode )
					{
						mRightProjectPanel->Render();
					}
					else if ( mCamSetupMode )
					{
						// Update buttons depending on 2D/3D mode
						if ( Globals.GetDraw3D() )
						{
							mFlipCamA->SetText( "FLIP", "CAM L" );
						}
						else
						{
							mFlipCamA->SetText( "FLIP", "CAM" );
						}

						if ( Globals.GetDraw3D() )
						{
							mFlipCamB->SetVisible( true );
						}
						else
						{
							mFlipCamB->SetVisible( false );
						}

						if ( ( gPrefs.Get3DAlignmentGuideMode() ) && ( Globals.GetDraw3D() ) )
						{
							mCamAlignModeBtn->SetVisible( true );
						}
						else
						{
							mCamAlignModeBtn->SetVisible( false );
						}

						if ( Globals.GetDraw3D() )
						{
							mParallaxShift->SetVisible( true );
						}
						else
						{
							mParallaxShift->SetVisible( false );
						}

						mRightCamSetupPanel->Render();
					}
					else if ( mWhiteBalanceMode )
					{
						mWhiteBalancePanel->Render();
					}
					else if ( mLoadLookMode )
					{
						mRightLoadLookPanel->Render();
					}
					else
					{
						RenderMeterPanel();
					}
				}
				else
				{
					mLeftUtilityPanel->Render();
					mRightUtilityPanel->Render();
					mBottomUtilityPanel->Render();
				}
				mCenterPanel->Render();
				if ( Globals.GetLoupeState() )
				{
					gLoupe->Render();
				}
				if ( Globals.GetHistogramOn() )
				{
					if ( gDeviceManager.NumDevices() == 1 )
					{
						gHistogram.Render( false, HISTOGRAM_DISP_FULL, 0 );
					}
					else
					{
						gHistogram.Render( false, HISTOGRAM_DISP_L, 0 );
						gHistogram.Render( false, HISTOGRAM_DISP_R, 0 );
					}
				}
			}
			else if ( mMode == SCREEN_MODE_PLAY )
			{
				mLeftPlayPanel->Render();
				mBottomPlayPanel->Render();

				// Right play panel only visible if 'right panel' modes are off, and exposure meter is not rendered
				if ( ( mFrameStoreMode == false ) && 
					 ( mCamSetupMode == false ) && 
					 ( mLoadLookMode == false ) && 
					 ( mCamAlignMode == false ) &&
					 ( ! ( Globals.Flags & GLOBALS_FLAG_EXPOSUREMETER ) )
					 )
				{
					mRightPlayPanel->Render();
				}
				
				if ( isProgramPlayingAVI() || isProgramPlayingSIV() )
				{
					gTimeline.Render();
				}

				mCenterPanel->Render();

				if ( Globals.Flags & GLOBALS_FLAG_EXPOSUREMETER )
				{
					RenderExposureMeter();
				}
				else if ( mFrameStoreMode )
				{
					mRightFramestorePanel->Render();

					if ( ! Globals.GetGrabOn() )
					{
						mGrabButton->SetHighlight( false );
					}
				}
				else if ( mLoadLookMode )
				{
					mRightLoadLookPanel->Render();
				}
				else if ( mCamSetupMode )
				{
					// Update buttons depending on 2D/3D mode
					if ( Globals.GetDraw3D() )
					{
						mFlipCamA->SetText( "FLIP", "CAM L" );
					}
					else
					{
						mFlipCamA->SetText( "FLIP", "CAM" );
					}

					if ( Globals.GetDraw3D() )
					{
						mFlipCamB->SetVisible( true );
					}
					else
					{
						mFlipCamB->SetVisible( false );
					}

					if ( ( gPrefs.Get3DAlignmentGuideMode() ) && ( Globals.GetDraw3D() ) )
					{
						mCamAlignModeBtn->SetVisible( true );
					}
					else
					{
						mCamAlignModeBtn->SetVisible( false );
					}

					if ( Globals.GetDraw3D() )
					{
						mParallaxShift->SetVisible( true );
					}
					else
					{
						mParallaxShift->SetVisible( false );
					}

					mRightCamSetupPanel->Render();
				}
				
				if ( gAudioPlayback || isProgramPlayingAVI() )
				{
					if ( ( ! mFrameStoreMode ) && ( ! ( Globals.Flags & GLOBALS_FLAG_EXPOSUREMETER ) ) && ( ! mLoadLookMode ) )
					{
						RenderAudioMeterPanel();
					}
				}

				if ( Globals.GetHistogramOn() )
				{
					if ( Globals.GetDraw3D() )
					{
						gHistogram.Render( false, HISTOGRAM_DISP_L, 0 );
						gHistogram.Render( false, HISTOGRAM_DISP_R, 0 );
					}
					else
					{
						gHistogram.Render( false, HISTOGRAM_DISP_FULL, 0 );
					}
				}
			}

			if ( gBlkThread )
			{
				if ( Globals.GetHighBlackLevel() )
					mHighBlackLevelDialog->Render();
			}

			RenderTopPanel();
			if ( mMessageBox )
			{
				if ( mTimecode2Dialog )
				{
					mTimecode2Dialog->Render();
				}

				mMessageBox->Render();
			}
			else if ( mMessageBox2 )
			{
				mMessageBox2->Render();
			}
			else if ( mProjectDialog )
			{
				mProjectDialog->Render();
			}
			else if ( gLookDialog )
			{
				gLookDialog->Render();
			}
			else if ( gMacroDialog )
			{
				gMacroDialog->Render();
			}
			else if ( gDispModesDialog )
			{
				gDispModesDialog->Render();
			}
			else if ( mSerialDialog )
			{
				mSerialDialog->Render();
			}
			else if ( mAboutDialog )
			{
				mAboutDialog->Render();
			}
			else if ( mBlackDialog )
			{
				if ( ! Globals.GetHighBlackLevel() )
				{
					mBlackDialog->Render();
				}
			}
			else if ( mFirmwareDialog )
			{
				mFirmwareDialog->Render();
			}
			else if ( mExportDialog )
			{
				mExportDialog->Render();
			}
			else if ( mProgressDialog )
			{
				if ( ! Globals.GetHighBlackLevel() )
				{
					mProgressDialog->Render();
				}
			}
			else if ( mShutterDialog )
			{
				mShutterDialog->Render();
			}
			else if ( mTimecode2Dialog )
			{
				mTimecode2Dialog->Render();
			}
			else if ( mModesDialog )
			{
				mModesDialog->Render();
			}
			else if ( mMetadataDialog )
			{
				mMetadataDialog->Render();
			}
			else if ( mSceneShotTakeDialog )
			{
				mSceneShotTakeDialog->Render();
			}
			else if ( mWhiteBalanceDialog )
			{
				mWhiteBalanceDialog->Render();
			}
			else if ( mUserWBDialog )
			{
				mUserWBDialog->Render();
			}
			else if ( mMetaDataCtrl )
			{
				mMetaDataCtrl->Render();
			}
			else if ( mDisplayPrefsDialog )
			{ 
				mDisplayPrefsDialog->Render();
			}
			else if ( mRepairDialog )
			{
				mRepairDialog->Render();
			}
			else if ( mDefectivePixelmapDlg )
			{
				mDefectivePixelmapDlg->Render();
			}
			else if ( mFindFileMatchDlg )
			{
				mFindFileMatchDlg->Render();
			}
			else if ( mMultiCamModeDlg )
			{
				mMultiCamModeDlg->Render();
			}
			else if ( mCamConnectionDlg )
			{
				mCamConnectionDlg->Render();
			}
			else if ( mParallaxShiftDlg )
			{
				mParallaxShiftDlg->Render();
			}
			else if ( mGuidesDialog )
			{
				mGuidesDialog->Render();
			}
			else if ( mCamAlignMode )
			{
				mCamAlignPanel->Render();
			}
			else if ( mSDIModesDialog )
			{
				mSDIModesDialog->Render();
			}
			else if ( Globals.GetParallaxMode() != PARALLAX_MODE_OFF )
			{
				if ( Globals.GetParallaxMode() == PARALLAX_MODE_PANEL )
				{
					mParallaxShiftPanel->Render();
				}
				else if ( Globals.GetParallaxMode() == PARALLAX_MODE_DIALOG )
				{
					mParallaxShiftDlg->Render();
				}
			}
			else if ( Globals.GetZoomMode() != 1 )
			{
				mCenterZoomPanel->Render();
			}
		}
	}

    GIGE_SETFONTCOLOR(1, 1, 1);

	if ( strlen(CUIButton::mDebugText) )
	    GIGE_STRINGOUT(20, 750, CUIButton::mDebugText );

}

void CUIScreen::OnKeydown( DWORD key )
{
	if ( mKeyboard )
	{
		mKeyboard->OnKeydown( key );
	}
    else
    {
        HandleGuiKeydown( key );
    }
}

void CUIScreen::HandleGuiVK( DWORD key )
{
    switch ( mKeyboardController )
    {
    case KEYBOARD_CONTROLLER_PARALLAX:
        switch ( key )
        {
        case VK_LEFT:
            onXminus(-1,-1);
            break;
        case VK_RIGHT:
            onXplus(-1,-1);
            break;
        case VK_UP:
            onYplus(-1,-1);
            break;
        case VK_DOWN:
            onYminus(-1,-1);
            break;
        }
        break;
    default:
        break;
    }
}

void CUIScreen::HandleGuiKeydown( DWORD key )
{
    float skew = 0;

    switch ( mKeyboardController )
    {
    case KEYBOARD_CONTROLLER_NONE:
        switch ( key )
        {
			case 'g':
			case 'G':
				OnGuidesClick( 0, 0 );
				break;
			case 'l':
			case 'L':
				if ( gPrefs.GetPolarization() )
				{
					gPrefs.SetPolarization( false );
				}
				else
				{
					gPrefs.SetPolarization( true );
				}
				break;
			case 'p':
			case 'P':
				mKeyboardController = KEYBOARD_CONTROLLER_PARALLAX;
				SetOverlayText("SET PARALLAX",0);
				break;
			case '.':
				if ( gPrefs.GetDebugMode() > 0 )
					Globals.mDebugDPC ^= 1;
				break;
        case 'f':
        case 'F':
			if ( gPrefs.GetSdiMode() )
				gSdiThread->mDoFlush = true;
            break;
        }
        break;
    case KEYBOARD_CONTROLLER_PARALLAX:
        switch ( key )
        {
			case 'p':
			case 'P':
				mKeyboardController = KEYBOARD_CONTROLLER_NONE;
				ClearOverlayText();
				break;
			case 'r':
			case 'R':
        		gPrefs.SetParallaxShift( PREFS_PARALLAX_X, 0 );
        		gPrefs.SetParallaxShift( PREFS_PARALLAX_Y, 0 );
        		gPrefs.SetParallaxShift( PREFS_PARALLAX_K, 0 );
				SignalStereo();
				break;

			case 's':
			case 'S':
				skew = gPrefs.GetParallaxShift( PREFS_PARALLAX_K );
				skew += SKEW_STEP;
				gPrefs.SetParallaxShift( PREFS_PARALLAX_K, skew );
				SignalStereo();
				InvalidateRect( Globals.hwnd, NULL, false );
				break;

			case 'x':
			case 'X':
				skew = gPrefs.GetParallaxShift( PREFS_PARALLAX_K );
				skew -= SKEW_STEP;
				gPrefs.SetParallaxShift( PREFS_PARALLAX_K, skew );
				SignalStereo();
				InvalidateRect( Globals.hwnd, NULL, false );
				break;

			}
			break;
		default:
			break;
    }
}

void CUIScreen::OnVK( DWORD key )
{
	if ( mKeyboard )
	{
		mKeyboard->OnVK( key );
	}
    else
    {
        HandleGuiVK( key );
    }
	InvalidateRect( Globals.hwnd, NULL, false );
}



void CUIScreen::OnIncPlay( int delta )
{
    gLogger.info(0,"%d   CUIScreen::OnIncPlay\n",GetTickCount());
	if ( mMode != SCREEN_MODE_PLAY ) return;

	if ( ! gPlayList ) return;

	char * currentFile = Globals.GetCurrentPlaybackFile();
	char * tempPath = gUtils->GetParentDir( currentFile );

	// Project directory
	char location[ 256 ];
	gProject->GetDirectory( location );

	if ( strcmp( location, tempPath ) != 0 )
	{
		strcpy( location, tempPath );
	}

	char *playfile = NULL;

	switch ( delta )
	{
		case -1:
			playfile = gPlayList->GetPrevious();
			break;
		case 0:
			playfile = gPlayList->GetCurrent();
			break;
		case 1:
			playfile = gPlayList->GetNext();
			break;
	}

	if ( !playfile ) return;

	
	strcat( location, "\\" );
	strcat( location, playfile );
	Globals.SetCurrentPlaybackFile( location );


    gLogger.info(0,"%d   CUIScreen::OnIncPlay A\n",GetTickCount());
	if ( isProgramPlayingAVI() )
	{
		gConfig.StopPlayingAVI(false);
	}
	else if ( isProgramPlayingSIV() )
	{
		gConfig.StopPlayingSIV(false);
	}

    gLogger.info(0,"%d   CUIScreen::OnIncPlay B\n",GetTickCount());
	if ( isFile( playfile, "siv" ) )
	{
    	char secondFile[ 512 ];
        std::vector<char*> *SIVPlaybackPaths = new std::vector<char*>();
        SIVPlaybackPaths->push_back( playfile );
        strcpy(secondFile,playfile);
        AddStereoFile( secondFile, SIVPlaybackPaths );
		gConfig.StartPlayingSIV( SIVPlaybackPaths );
        delete SIVPlaybackPaths;
	}
	else if ( isFile( playfile, "avi" ) || isFile( playfile, "mov" ) )
	{
		gConfig.StartPlayingAVI( playfile );
	}
	else
		assert(0);
	InvalidateRect( Globals.hwnd, NULL, false );
}


void CUIScreen::OnClick( int x, int y )
{
	if ( mFileDialog )
	{
		if ( ! mKeyboard )
		{
			mFileDialog->OnClick( x, y );
		}
		else
		{
			mKeyboard->OnClick( x, y );

			// Create new folder if OK was clicked on keyboard
			if ( ( x > 930 ) && ( x < 1090 ) && ( y > 645 ) && ( y < 680 ) )
			{
				mFileDialog->CreateDir();
			}
		}
		return;
	}
	if ( mMacroListDialog )
	{
		mMacroListDialog->OnClick( x, y );
		return;
	}
	if ( mDisplayModesListDialog )
	{
		mDisplayModesListDialog->OnClick( x, y );
		return;
	}
	if ( mKeyboard )
	{
		mKeyboard->OnClick( x, y );
		return;
	}
	if ( gIridas )
	{
		gIridas->OnClick( x, y );
		return;
	}
	if ( mPrefs )
	{
		mPrefs->OnClick( x, y );
		return;
	}
	if ( mMessageBox )
	{
		mMessageBox->HandleClick( x, y );
		return;
	}
	if ( mMessageBox2 )
	{
		mMessageBox2->HandleClick( x, y );
		return;
	}
	if ( mProjectDialog )
	{
		mProjectDialog->HandleClick( x, y );
		return;
	}
	if ( gLookDialog )
	{
		gLookDialog->HandleClick( x, y );
		return;
	}
	if ( gMacroDialog )
	{
		gMacroDialog->HandleClick( x, y );
		return;
	}
	if ( gDispModesDialog )
	{
		gDispModesDialog->HandleClick( x, y );
		return;
	}
	if ( mSerialDialog )
	{
		mSerialDialog->HandleClick( x, y );
		return;
	}
	if ( mAboutDialog )
	{
		mAboutDialog->HandleClick( x, y );
		return;
	}
	if ( mBlackDialog )
	{
		if ( ! Globals.GetHighBlackLevel() )
		{
			mBlackDialog->HandleClick( x, y );
			return;
		}
	}
	if ( gBlkThread )
	{
		if ( Globals.GetHighBlackLevel() )
		{
			mHighBlackLevelDialog->HandleClick( x, y );
			return;
		}
	}
	if ( mFirmwareDialog )
	{
		mFirmwareDialog->HandleClick( x, y );
		return;
	}
	if ( mExportDialog )
	{
		mExportDialog->HandleClick( x, y );
		return;
	}
	if ( mProgressDialog )
	{
		if ( ! Globals.GetHighBlackLevel() )
		{
			mProgressDialog->HandleClick( x, y );
			return;
		}
	}
	if ( mShutterDialog )
	{
		mShutterDialog->HandleClick( x, y );
		return;
	}
	/*if ( mTimecodeDialog )
	{
		mTimecodeDialog->HandleClick( x, y );
		return;
	}*/
	if ( mTimecode2Dialog )
	{
		mTimecode2Dialog->HandleClick( x, y );
		return;
	}
	if ( mModesDialog )
	{
		mModesDialog->HandleClick( x, y );
		return;
	}
	if ( mMetadataDialog )
	{
		mMetadataDialog->HandleClick( x, y );
		return;
	}
	if ( mSceneShotTakeDialog )
	{
		mSceneShotTakeDialog->HandleClick( x, y );
		return;
	}
	if ( mWhiteBalanceDialog )
	{
		mWhiteBalanceDialog->HandleClick( x, y );
		return;
	}
	if ( mUserWBDialog )
	{
		mUserWBDialog->HandleClick( x, y );
		return;
	}
	if ( mMetaDataCtrl )
	{
		mMetaDataCtrl->OnClick( x, y );
		return;
	}
	if ( mDisplayPrefsDialog )
	{
		mDisplayPrefsDialog->OnClick( x, y );
		return;
	}
	if ( mRepairDialog )
	{
		mRepairDialog->HandleClick( x, y );
		return;
	}
	if ( mDefectivePixelmapDlg )
	{
		mDefectivePixelmapDlg->HandleClick( x, y );
		return;
	}
	if ( mFindFileMatchDlg )
	{ 
		mFindFileMatchDlg->HandleClick( x, y );
		return;
	}
	if ( mMultiCamModeDlg )
	{
		mMultiCamModeDlg->HandleClick( x, y );
		return;
	}
	if ( mCamConnectionDlg )
	{
		mCamConnectionDlg->HandleClick( x, y );
		return;
	}
	if ( mSDIModesDialog )
	{
		mSDIModesDialog->HandleClick( x, y );
		return;
	}
	if ( mParallaxShiftDlg )
	{
		// Need to handle special case of a non-modal dialog
		// We want to respond to PARALX SHIFT button click if parallax shift dialog is still displayed
		if ( ( x > 1170 ) && 
			 ( x < 1270 ) && 
			 ( y > 510 ) && 
			 ( y < 600 ) && 
			 ( Globals.GetParallaxMode() == PARALLAX_MODE_DIALOG ) ) // coordinates of PARALX SHIFT button
		{
			mParallaxShiftDlg->Save();
			mParallaxShift->SetHighlight( false );
			Globals.SetParallaxMode( PARALLAX_MODE_OFF );
			return;
		}
		else
		{
			mParallaxShiftDlg->HandleClick( x, y );
			return;
		}
	}
	if ( mGuidesDialog )
	{
		mGuidesDialog->HandleClick( x, y );
		return;
	}

	if ( mVideoDisplayMode == VIDEO_DISPLAY_NORMAL )
	{
		if ( mMode == SCREEN_MODE_PREVIEW )
		{
			if ( ! mUtilityMode )
			{
				// Only handle click of frame store panel when we are in frame store mode and exposure meter is not rendered
				if ( ( mFrameStoreMode ) && ( ! Globals.Flags & GLOBALS_FLAG_EXPOSUREMETER ) )
				{
					if ( mRightFramestorePanel->HandleClick( x, y ) )
						return;
				}
				if ( mProjectManagerMode )
				{
					if ( mRightProjectPanel->HandleClick( x, y ) )
						return;
				}
				if ( mCamSetupMode )
				{
					if ( mRightCamSetupPanel->HandleClick( x, y ) )
						return;
				}
				if ( mWhiteBalanceMode )
				{
					if ( mWhiteBalancePanel->HandleClick( x, y ) )
					{
						UpdateButtonStates();
						return;
					}
				}
				if ( mLoadLookMode )
				{
					if ( mRightLoadLookPanel->HandleClick( x, y ) )
						return;
				}
				if ( mCamAlignMode )
				{
					if ( mCamAlignPanel->HandleClick( x, y ) )
						return;
				}

				if ( mLeftPanel->HandleClick( x, y ) )
					return;
				if ( mMeterPanel->HandleClick( x, y ) )
					return;
				if ( mBottomPanel->HandleClick( x, y ) )
					return;
			}
			else
			{
				if ( mLeftUtilityPanel->HandleClick( x, y ) )
					return;
				if ( mRightUtilityPanel->HandleClick( x, y ) )
					return;
				if ( mBottomUtilityPanel->HandleClick( x, y ) )
					return;
			}

			if ( Globals.GetLoupeState() )
			{
				if ( Globals.GetFocusCheck() )
				{
					mCenterPanel->HandleClick( x, y );
				}
				else
				{
					gLoupe->HandleClick( x, y );
				}
				return;
			}
		}
		else if ( mMode == SCREEN_MODE_PLAY )
		{
			if ( mLeftPlayPanel->HandleClick( x, y ) )
				return;

			if ( isProgramPlayingAVI() || isProgramPlayingSIV())
			{
				if ( gTimeline.HandleClick( x, y ) )
					return;
			}

			if ( mBottomPlayPanel->HandleClick( x, y ) )
				return;

			// Only handle click of right play panel when we are not in frame store mode and we are not rendering exposure meter.
			if ( ( ! mFrameStoreMode ) && ( ! Globals.Flags & GLOBALS_FLAG_EXPOSUREMETER ) && ( ! mLoadLookMode ) ) 
			{
				if ( mRightPlayPanel->HandleClick( x, y ) )
					return;
			}
			// Only handle click of right frame store panel when we are not rendering exposure meter
			else if ( ( ! Globals.Flags & GLOBALS_FLAG_EXPOSUREMETER ) && ( ! mLoadLookMode ) )
			{
				if ( mRightFramestorePanel->HandleClick( x, y ) )
					return;
			}
			else if ( mLoadLookMode )
			{
				if ( mRightLoadLookPanel->HandleClick( x, y ) )
					return;
			}
		}

		// Handle parallax buttons
		if ( Globals.GetParallaxMode() != PARALLAX_MODE_OFF )
		{
			if ( mParallaxShiftPanel->HandleClick( x, y ) )
				return;
		}
		
		// If we are in zoom mode, pan arrows need to handled instead of hot spots
		if ( Globals.GetZoomMode() != 1 )
		{
			if ( mCenterZoomPanel->HandleClick( x, y ) )
				return;
		}
		else
		{
			if ( mCenterPanel->HandleClick( x, y ) )
				return;
		}
	}
	else if ( mVideoDisplayMode != VIDEO_DISPLAY_NORMAL )
	{
		// Full screen record and play mode
		if ( mMode == SCREEN_MODE_PLAY )
		{
			if ( gTimeline.HandleClick( x, y ) )
				return;

			if ( mBottomPlayPanelFS->HandleClick( x, y ) )
				return;

			if ( mFSPlayPanel->HandleClick( x, y ) )
				return;
		}

		if ( mSceneShotTakeDialog )
		{
			mSceneShotTakeDialog->HandleClick( x, y );
			return;
		}

		if ( isProgramPreviewing() )
		{
			if ( mFSRecPanel->HandleClick( x, y ) )
				return;
		}

		if ( Globals.GetZoomMode() != 1 )
		{
			if ( mFullZoomPanel->HandleClick( x, y ) )
			return;
		}

		if ( mFullPanel->HandleClick( x, y ) )
			return;
	}
	else
	{
		assert(0);
	}
}


void CUIScreen::OnRightClick( void )
{


/* 

Ari feels this feature is valid (it was his idea).  
The Macro's temporarily "turn off" while in the dialog 
boxes that have a "done" or "okay" or "open".

THIS IS PER DESIGN!!!!

It is intentionally like this...

*/

	if ( mFileDialog )
	{
		// 9) File Browser (it should do the "open" in the file browser)
		mFileDialog->HandleRightClick();
		return;
	}
	if ( mMacroListDialog )
	{
		// 12) Right-Click Settings 
		mMacroListDialog->HandleRightClick();
		return;
	}
	if ( mKeyboard )
	{
		return;
	}
	if ( gIridas )
	{
		return;
	}
	if ( mUserWBDialog )
	{
		return;
	}
	if ( mPrefs )
	{
		// 2) Cam Prefs
		mPrefs->HandleRightClick();
		return;
	}
	if ( mMessageBox )
	{
		MessageBoxOk();
		return;
	}
	if ( mMessageBox2 )
	{
		return;
	}
	if ( mProjectDialog )
	{
		// 10) New Project
		mProjectDialog->HandleRightClick();
		return;
	}
	if ( gLookDialog )
	{
		// 5) Set Look 
		gLookDialog->HandleRightClick();
		return;
	}
	if ( gMacroDialog )
	{
		gMacroDialog->HandleRightClick();
		return;
	}
	if ( gDispModesDialog )
	{
		gDispModesDialog->HandleRightClick();
	}
	if ( mSerialDialog )
	{
		// 7) Serial Command
		mSerialDialog->HandleRightClick();
		return;
	}
	if ( mAboutDialog )
	{
		mAboutDialog->HandleRightClick();
		return;
	}
	if ( mBlackDialog )
	{
		// 1) Set Black
		mBlackDialog->HandleRightClick();
		return;
	}
	if ( mFirmwareDialog )
	{
		// 8) Load Firmware
		mFirmwareDialog->HandleRightClick();
		return;
	}
	if ( mExportDialog )
	{
		return;
	}
	if ( mProgressDialog )
	{
		return;
	}
	if ( mShutterDialog )
	{
		// 6) Shutter Control
		mShutterDialog->HandleRightClick();
		return;
	}
	/*if ( mTimecodeDialog )
	{
		// 11) Timecode settings
		mTimecodeDialog->HandleRightClick();
		return;
	}*/
	if ( mTimecode2Dialog )
	{
		// 11) Timecode settings
		mTimecode2Dialog->HandleRightClick();
		return;
	}
	if ( mModesDialog )
	{
		// 4) Frame-Rate Resolution
		mModesDialog->HandleRightClick();
		return;
	}
	
	macroHandleRightClick();
	InvalidateRect( Globals.hwnd, NULL, false );

}

void CUIScreen::DeleteClip( void )
{
	mMessageBox = new CUIMessageBox( MB_TYPE_DELETECLIP, 380, 275, 520, 170, "MessageBox" );
	mMessageBox->Init("DO YOU WANT TO DELETE THIS CLIP?", "");
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::OverwriteClip()
{
	mMessageBox = new CUIMessageBox( MB_TYPE_OVERWRITE_CLIP, 380, 275, 520, 170, "MessageBox" );
	mMessageBox->Init( "DO YOU WANT TO OVERWRITE THIS CLIP?", "" );
	gLogger.info( 1, "CUIScreen::OverwriteClip()\n" );
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::SaveIridas()
{
	mMessageBox = new CUIMessageBox( MB_TYPE_SAVE_IRIDAS, 380, 275, 520, 170, "MessageBox" );
	mMessageBox->Init( "DO YOU WANT TO SAVE BEFORE EXITING?", "" );
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::SavePlayerLook()
{
	mMessageBox = new CUIMessageBox( MB_TYPE_SAVE_PLAYER_LOOK, 380, 275, 520, 170, "MessageBox" );
	mMessageBox->Init( "DO YOU WANT TO REPLACE PLAY LOOK?", "" );
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::NoLTCDevice()
{
	mMessageBox = new CUIMessageBox( MB_TYPE_NO_LTC_DEVICE, 380, 275, 520, 170, "MessageBox" );
	mMessageBox->Init( "NO LTC DEVICE FOUND!", "" );
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::ChangeSystemTime()
{
	mMessageBox = new CUIMessageBox( MB_TYPE_CHANGE_SYSTEM_TIME, 380, 275, 520, 170, "MessageBox" );
	mMessageBox->Init( "ARE YOU SURE YOU WANT TO CHANGE\nTHE SYSTEM TIME?", "" );
}

void CUIScreen::OnBlackDialog( void )
{
	mBlackDialog = new CUIBlackDialog;
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::NoBuffers( void )
{
    assert(_CrtCheckMemory() );
    gLogger.info(1, "\n\n%d   CUIScreen::NoBuffers TOP\n", GetTickCount());
	mMessageBox = new CUIMessageBox( MB_TYPE_RAMOVERFLOWWRITE, 380, 275, 520, 170, "MessageBox" );
	mMessageBox->Init("RAM BUFFER OVERFLOW", "WAITING WHILE QUEUE IS WRITTEN...");
    assert(_CrtCheckMemory() );
	InvalidateRect( Globals.hwnd, NULL, false );
    assert(_CrtCheckMemory() );
	mIsRecording = false;
    assert(_CrtCheckMemory() );
	StopRecording();
    assert(_CrtCheckMemory() );
	gConfig.StopGrabbing(false,false);
    assert(_CrtCheckMemory() );

    if ( Globals.GetAudioOn() )
    {
	    gAudioRecorder->Close();
		gAudioRecorder->CloseMixer();
		Globals.SetAudioOn( false );
    }


	delete mMessageBox;
	mMessageBox = NULL;

    // reset cineform lossless level
    CCineformSDK::mLosslessLevel = 0;

    //gLogger.info(0, "%d   CUIScreen::NoBuffers() about to OnStartGrabbing\n", GetTickCount());
	OnStartGrabbing();

}

void CUIScreen::ConnectProgress( void )
{
    Globals.SetConfigStep(0);
	mMessageBox = new CUIMessageBox( MB_TYPE_RAMOVERFLOWWRITE, 380, 275, 520, 170, "MessageBox" );
    mMessageBox->Init("CONNECT PROGRESS", "");
    UpdateConnectProgress();
}

void CUIScreen::UpdateConnectProgress( void )
{
    char str[256];
    if ( !mMessageBox ) return;
    if ( !strcmp(mMessageBox->text1(), "CONNECT PROGRESS" ) )
    {
        Globals.SetConfigStep( Globals.GetConfigStep() + 1 );
        sprintf( str, "CONNECTING TO CAMERA STEP %d", Globals.GetConfigStep () );
	    mMessageBox->Init( "CONNECT PROGRESS", str );
	    SendMessage ( Globals.hwnd, WM_PAINT, 0, 0 );
    }
}

void CUIScreen::ConnectDone( void )
{
    if ( mMessageBox )
    {
	    delete mMessageBox;
	    mMessageBox = NULL;
	    InvalidateRect( Globals.hwnd, NULL, false );
    }
}

void CUIScreen::LostConnection( int cam )
{
	mIsRecording = false;
	gConfig.StopGrabbing( true, false );
	StopRecording();
	gConfig.Clear( false );

	if ( cam == 0 )
	{
		Globals.SetPolledEvent( POLLED_EVENT_RECONNECT_CAM_0 );
		Globals.SetCamState( CAM_STATE_DISCONNECTED, 0 );
	}
	else
	{
		Globals.SetPolledEvent( POLLED_EVENT_RECONNECT_CAM_1 );
		Globals.SetCamState( CAM_STATE_DISCONNECTED, 1 );
	}
}

void CUIScreen::SmallFileError( DWORD size )
{
	char str[128];
	sprintf(str,"RECORDED FILE OF %d BYTES",size);
	mMessageBox2 = new CUIMessageBox( MB_TYPE_SMALLFILE, 380, 275, 520, 170, "MessageBox" );
	mMessageBox2->Init(str, "");
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::FileExportError()
{
	char str[128];
	sprintf( str, "FILE EXPORT ERROR" );
	mMessageBox2 = new CUIMessageBox( MB_TYPE_SMALLFILE, 380, 275, 520, 170, "MessageBox" );
	mMessageBox2->Init(str, "");
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::ExportDestDriveError( bool ntfsLimit )
{
	char str[128];

	if ( ntfsLimit )
	{
		sprintf( str, "EXPORT ERROR\n4 GB FAT FILE SIZE LIMIT REACHED!" );
	}
	else
	{
		sprintf( str, "EXPORT ERROR\nDESTINATION DRIVE FULL OR\nNO FILES TO RENDER." );
	}
	mMessageBox2 = new CUIMessageBox( MB_TYPE_SMALLFILE, 380, 275, 520, 170, "MessageBox" );
	mMessageBox2->Init(str, "");
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::FileIOError()
{
	mMessageBox2 = new CUIMessageBox( MB_TYPE_FILE_IO_ERROR, 380, 275, 520, 170, "MessageBox" );
	mMessageBox2->Init( "FILE I/O ERROR\nRECORDING STOPPED!", "" );
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::CreateDirectoryError()
{
	char str[128];
	sprintf( str, "ERROR CREATING DIRECTORY!" );
	mMessageBox2 = new CUIMessageBox( MB_TYPE_SMALLFILE, 380, 275, 520, 170, "MessageBox" );
	mMessageBox2->Init(str, "");
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::OpenGLStateChanged()
{
	char str[ 64 ];
	sprintf( str, "YOU MUST RESTART SIDVR FOR\nTHESE CHANGES TO TAKE EFFECT!" );
	mMessageBox2 = new CUIMessageBox( MB_TYPE_OPEN_GL_STATE, 380, 275, 520, 170, "MessageBox" );
	mMessageBox2->Init(str, "");
	InvalidateRect( Globals.hwnd, NULL, false );
}


void CUIScreen::MessageBoxCancel( void )
{
	Globals.SetPolledEvent( POLLED_EVENT_DELETEMESSAGEBOX );

	if ( gIridas )
	{
		Globals.SetPolledEvent( POLLED_EVENT_DELETEIRIDAS );
	}
}

void CUIScreen::MessageBoxOk( void )
{
	EMessageBoxType type = mMessageBox->GetType();
	
	switch ( type )
	{
		case MB_TYPE_MISSING_LOOK:
			// Load "default.look" if custom look file not found
			gPrefs.SetLookName( 4, "default.look" );
			gLut3d.LoadLook( 4, "default.look" );
			gPrefs.SetPlayerLook( 4 );
			gD3D->SetLut3DTextures();
			break;
		case MB_TYPE_DELETECLIP:
			if ( isProgramPlayingAVI() )
				gConfig.StopPlayingAVI(true);
			else if ( isProgramPlayingSIV() )
				gConfig.StopPlayingSIV(true);
			else
				assert(0);
			break;
		case MB_TYPE_OVERWRITE_CLIP:
			Globals.SetSkipClip( false );
			gLogger.info( 1, "CUIScreen::MessageBoxOk\n" );
			break;
		case MB_TYPE_SAVE_IRIDAS:
			OnSaveLookFile( 0, 0 );
			break;
		case MB_TYPE_SAVE_PLAYER_LOOK:
			gIridas->SavePlayerLookFile();
			break;
		case MB_TYPE_CHANGE_SYSTEM_TIME:
			mTimecode2Dialog->Set();
			break;
		case MB_TYPE_QUIT:
		case MB_TYPE_QC_INIT:
			// Before the application exits, write the serial numbers to prefsXML
			gPrefs.SetCamSerial( 0, Globals.GetCamSerialNumber( 0 ) );
			gPrefs.SetCamSerial( 1, Globals.GetCamSerialNumber( 1 ) );
			PostQuitMessage(0);
			break;
	}
	
	Globals.SetPolledEvent( POLLED_EVENT_DELETEMESSAGEBOX );
}

void CUIScreen::MessageBoxContinue()
{
	//gUIScreen->CreateQCDialog();
	//Sleep( 50 );
	//StartQCThread();
}

void CUIScreen::MessageBoxOk2( void )
{
	delete mMessageBox2;
	mMessageBox2 = NULL;
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::ProjectOk()
{
	// Create directory
	char filepathPrimary[ MAX_PATH ];
	char filepathSecondary[ MAX_PATH ];
	char aleLogPath[ MAX_PATH ];
	char val[ MAX_PATH ];

	Globals.SetProjectError( false );

	char prevDirPath[ MAX_PATH ];
	strcpy( prevDirPath, mProjectDialog->mLocationPrimary );

	strcpy( filepathPrimary, mProjectDialog->mLocationPrimary );
	if ( filepathPrimary[ strlen( filepathPrimary ) - 1 ] != '\\' )
		strcat( filepathPrimary, "\\" );

	char reelStr[ 4 ];
	sprintf ( reelStr, "%003d", mProjectDialog->mReelNumber );

	strcat( filepathPrimary, mProjectDialog->mName );

	BOOL res = CreateDirectory( filepathPrimary, NULL );
	if ( !res )
	{
		Globals.SetProjectError( true );
		return;
	}

	char primaryPath [ MAX_PATH ] = "\0";
	strcpy( primaryPath, filepathPrimary );

	// Create SIL file
	strcat( filepathPrimary,"\\" );
	strcat( filepathPrimary, mProjectDialog->mName );

	// Create the ALE log file fapth
	strcpy( aleLogPath, filepathPrimary );
	strcat( aleLogPath, ".ale" );

	strcat( filepathPrimary, ".sil" );
	FILE *file = fopen( filepathPrimary, "wt" );
	if ( file )
	{
		fprintf( file,"ProjectName\t%s\n", mProjectDialog->mName );
	}
	fclose( file );
	


	// Create secondary directory
	strcpy( filepathSecondary, mProjectDialog->mLocationSecondary );

	if ( strcmp( filepathSecondary, "" ) != 0 )
	{
		strcpy( prevDirPath, mProjectDialog->mLocationSecondary );

    	if ( filepathSecondary[ strlen( filepathSecondary ) - 1 ] != '\\' )
			strcat( filepathSecondary, "\\" );

		strcat( filepathSecondary, mProjectDialog->mName );

		// Write secondary directory to SIL file
		sprintf( val, "%s", filepathSecondary );
		// Use filepathPrimary here - that is the path to the SIL file
		WritePrivateProfileString( "dirs", "secondaryDir", val, filepathPrimary );

		// Create secondary directory only if primary and secondary paths are different
		if ( strcmp( primaryPath, filepathSecondary ) != 0 )
		{
    		res = CreateDirectory( filepathSecondary, NULL );
			if ( !res )
			{
				gUIScreen->CreateDirectoryError();
			}
		}
	}

	InitializeProjectMetadata( filepathPrimary );

	// Create the ALE log file
	CALELogfile logAle( aleLogPath );
	
	logAle.WriteLogfileHeading();
	logAle.WriteData( "FIELD_DELIM", "TABS" );				// hard coded
	logAle.WriteData( "VIDEO_FORMAT", "1080" );				// hard coded
	logAle.WriteData( "FILM_FORMAT", "35mm, 4 perf" );		// hard coded
	logAle.WriteData( "AUDIO_FORMAT", "48Khz" );			// hard coded
	
	// Write the correct conform FPS to project
	string fpsStr = "";
	int raster = mProjectDialog->GetProjectConformFPS();

	switch ( raster )
	{
		case UIPROJECT_23_976_FPS:
			fpsStr = "23.976";
			break;
		case UIPROJECT_24_FPS:
			fpsStr = "24";
			break;
		case UIPROJECT_25_FPS:
			fpsStr = "25";
			break;
		case UIPROJECT_29_97_FPS:
			fpsStr = "29.97";
			break;
		case UIPROJECT_30_FPS:
			fpsStr = "30";
			break;
	}

	// Write project info to SIL file, prefs and ALE header
	WritePrivateProfileString( "ProjectInfo", "Framerate", fpsStr.c_str(), filepathPrimary );
	gPrefs.SetLastProjectFPS( raster );
	logAle.WriteData( "FPS", (char*) fpsStr.c_str() );

	int value = 0;
	char buffer[ 4 ] = "\0";

	int fileNaming = -1;
	
	fileNaming = mProjectDialog->GetFileNamingMode();

	value = mProjectDialog->mContinueProject->GetState();
	itoa ( value, buffer, 10 );
	WritePrivateProfileString( "ProjectInfo", "ApplyProjectInfo", buffer, filepathPrimary );

	value = fileNaming;
	itoa ( value, buffer, 10 );
	WritePrivateProfileString( "ProjectInfo", "FileNaming", buffer, filepathPrimary );

	value = mProjectDialog->mConformProjectFPS->GetState();
	itoa ( value, buffer, 10 );
	WritePrivateProfileString( "ProjectInfo", "ConformProjFPS", buffer, filepathPrimary );

	int reelNumber = mProjectDialog->GetProjectReelNumber();
	reelNumber += 1;
	itoa ( reelNumber, buffer, 10 );
	WritePrivateProfileString( "ProjectInfo", "ReelNumber", buffer, filepathPrimary );

	// Write conform FPS to prefs
	gPrefs.SetConformProjectFPS( mProjectDialog->mConformProjectFPS->GetState() );

	// Write ALE log file
	logAle.WriteData( "TAPE", mProjectDialog->mName );
	
	logAle.WriteNewline();
	logAle.WriteLogfileColumn();
	logAle.WriteColumnHeading();
	logAle.WriteNewline();
	logAle.WriteLogfileData();
	logAle.Close();

	if ( gProject )
	{
		delete gProject;
	}

	// Project name
	gProject = new CProject( mProjectDialog->mName, filepathPrimary );
	CheckDisk( true );
	gProject->CopyLookFiles();
	FlushFiles( filepathPrimary[ 0 ] );

	char siDir[ MAX_PATH ];
	strcpy( siDir, Globals.GetSIDir() );
	FlushFiles( siDir[0] );
	gPrefs.SetRecentProject( filepathPrimary );

	// Update project and clip info
	gPrefs.SetProjectData( false );

	gSlate.UpdateClipData();
	gProject->UpdateProjectInfo();
	gPrefs.Write();
	Globals.SetNextFileName();

	ProjectCancel();
}

void CUIScreen::ProjectCancel( void )
{
	delete mProjectDialog;
	mProjectDialog = NULL;
	InvalidateRect( Globals.hwnd, NULL, false );
}


void CUIScreen::SerialDone( void )
{
	delete mSerialDialog;
	mSerialDialog = NULL;
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::AboutDone( void )
{
	delete mAboutDialog;
	mAboutDialog = NULL;
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::FirmwareDone( void )
{
	delete mFirmwareDialog;
	mFirmwareDialog = NULL;
	InvalidateRect( Globals.hwnd, NULL, false );
}

bool CUIScreen::ExportOk()
{
	int index = 0;
	int numItems = 0;
	int sivItems = 0;
	int isDual = SIV_FILE_SINGLE;
	int mode = mExportDialog->GetExportMode();
	bool allClips = mExportDialog->GetAllClips();
	bool interleavedCF = mExportDialog->GetInterleaved();
	bool preview = mExportDialog->GetPreview();
	bool overwrite = mExportDialog->GetOverwrite();

	// Get the current playback directories
	char primaryDir[ 256 ] = "\0";
	char secondaryDir[ 256 ] = "\0";

	// If a project is loaded, get the paths for primary and secondary location from SIL file
	if ( gProject )
	{
		strcpy( primaryDir, gProject->mDirectory );
		strcpy( secondaryDir, gProject->GetSecondaryDirectory() );
	}
	else // otherwise, get current playback directory from loaded file name, and secondary directory from SELECT MATCHING FILE dialog
	{
		char tmpStr[ MAX_PATH ] = "\0";
		strcpy( tmpStr, Globals.GetCurrentPlaybackFile() );
		char * playbackFile = tmpStr;
		char * playbackPath = gUtils->GetParentDir( playbackFile );
		strcpy( primaryDir, playbackPath );
		strcpy( secondaryDir, Globals.GetMatchingPairDir() );
	}
	
	gLogger.info( 1, "%d   CUIScreen::ExportOK() primaryDir:   %s\n", GetTickCount(), primaryDir );
	gLogger.info( 1, "%d   CUIScreen::ExportOK() secondaryDir: %s\n", GetTickCount(), secondaryDir );

	if ( mode == UIEXPORT_COPY )
	{
		numItems = mExportDialog->GetPlaylist( primaryDir, FILE_TYPE_ALL );
	}
	else if ( ( mode == UIEXPORT_AVI ) || ( mode == UIEXPORT_QT ) )
	{
		numItems = mExportDialog->GetPlaylist( primaryDir, FILE_TYPE_SIV );
	}
	else
	{
		numItems = mExportDialog->GetPlaylist( primaryDir, FILE_TYPE_VIDEO );
	}

	TRenderJob job;
	std::vector<TRenderJob> jobVector;

	SetProgramStateFlag( PROGRAM_STATE_FLAG_RENDERING );

	char path[ MAX_PATH ] = "\0";
	strcpy( path, mExportDialog->mLocation );

	if ( isProgramPlayingAVI() )
	{
		gConfig.StopPlayingAVI(false);
	}
    if ( isProgramPlayingSIV() )
	{
		gConfig.StopPlayingSIV(false);
	}

	if ( allClips == false )
	{
		numItems = 1;
	}

	if ( mode == UIEXPORT_COPY )
	{
		for ( int i = 0; i < numItems; i++ )
		{
			// Create directory
			char exportFile[ MAX_PATH ] = "\0";
			
			// Add to job vector only the current playback file, or the entire directory
			if ( allClips == false )
			{
				strcpy( job.infile, Globals.GetCurrentPlaybackFile() );
				strcpy( exportFile, job.infile );
			}
			else
			{
				strcpy( exportFile, primaryDir );
				strcat( exportFile, "\\" );
				strcat( exportFile, gPlayList->GetClip( i ) );
				strcpy( job.infile, exportFile );
			}

			strcpy( job.seqInfo.mRoot, path );
			
			job.seqInfo.mFileType = FILE_TYPE_VIDEO;
			job.seqInfo.mStart = 1;
			job.seqInfo.mNumDigits = 6;
			job.seqInfo.mInterleave = false;
			job.singleFrame = false;
			job.offlineFPN = false;

			gLogger.info( 1, "%d   CUIScreen::ExportOK() file DIRECT: %s\n", GetTickCount(), exportFile );

			jobVector.push_back( job );

			// If we export a single SIV clip, add the wave file to the job if there is one
			if ( allClips == false )
			{
				char audioFilename[256];
				char * tmpString;
				tmpString = ( char* ) memchr ( exportFile, '.', strlen( exportFile ) );
				int position = tmpString - exportFile;

				strncpy ( audioFilename, exportFile , position );
				audioFilename[ position ] = '.';
				audioFilename[ position + 1 ] = 'w';
				audioFilename[ position + 2 ] = 'a';
				audioFilename[ position + 3 ] = 'v';
				audioFilename[ position + 4 ] = '\0';

				bool result = gUtils->FileExists( audioFilename );

				if ( result )
				{
					strcpy( job.infile, audioFilename );
					strcpy( job.seqInfo.mRoot, path );
					jobVector.push_back( job );
				}
			}
		}
		mProgressDialog = new CUIProgressDialog( FILE_TYPE_VIDEO, numItems );

		DeleteExportDialog();

		Convert( jobVector, mode, interleavedCF, preview, overwrite );
	}
	else // render list for AVI/QT/DNG
	{
		for ( int i = 0; i < numItems; i++ )
		{	
			char exportFile[ MAX_PATH ] = "\0";

			// Add to job vector only the current playback file, or the entire directory
			if ( allClips == false )
			{
				strcpy( job.infile, Globals.GetCurrentPlaybackFile() );
				strcpy( exportFile, Globals.GetCurrentPlaybackFile() );
			}
			else
			{
				strcpy( exportFile, primaryDir );
				strcat( exportFile, "\\" );
				strcat( exportFile, gPlayList->GetClip( i ) );
				strcpy( job.infile, exportFile );
			}

			strcpy( job.seqInfo.mRoot, path );
			
			switch( mode )
			{
				case UIEXPORT_AVI:
					job.seqInfo.mFileType = FILE_TYPE_AVI;
					break;
				case UIEXPORT_QT:
					job.seqInfo.mFileType = FILE_TYPE_MOV;
					break;
				case UIEXPORT_DNG:
					job.seqInfo.mFileType = FILE_TYPE_DNG;
					break;
				case UIEXPORT_DPX:
					job.seqInfo.mFileType = FILE_TYPE_DPX;
					job.seqInfo.mLut = mExportDialog->GetLUTState();
					job.seqInfo.mColorMatrix = mExportDialog->GetColorMatrixState();
					job.seqInfo.mWB = mExportDialog->GetWBState();
					break;
			}	

			job.seqInfo.mStart = 1;
			job.seqInfo.mNumDigits = 6;
			job.singleFrame = false;
			job.offlineFPN = false;
			job.seqInfo.mInterleave = false;

			// Do we have a single channel or interleaved stereo file?
			isDual = gUtils->IsSIVStereoPair( exportFile );

			if ( isDual == SIV_FILE_SINGLE )
			{
				job.seqInfo.mSIVmode = SIV_FILE_SINGLE;
			}
			else
			{
				job.seqInfo.mSIVmode = SIV_FILE_CAM_LEFT;
			}

			jobVector.push_back( job );

			// For SIV source files, we may need to find the second file of a stereo pair
			if ( isFile( exportFile, "siv" ) )
			{
				isDual = gUtils->IsSIVStereoPair( exportFile );

				// Increase SIV items only for single SIV files, and the first file of a stereo pair, 
				// to get correct number of clips for progress dialog
				if ( isDual == SIV_FILE_SINGLE )
				{
					sivItems++;
				}

				bool res = false;
				job.seqInfo.mSIVmode = SIV_FILE_SINGLE;

				char secondaryFileName[ MAX_PATH ];
				char secondFileTmp[ MAX_PATH ];
				strcpy( secondFileTmp, exportFile );

				if ( ( isDual == SIV_FILE_CAM_LEFT ) || ( isDual == SIV_FILE_CAM_RIGHT ) )
				{
					if ( isDual == SIV_FILE_CAM_LEFT )
					{
						if ( interleavedCF == true )
						{
							sivItems++;
						}
						else
						{
							sivItems += 2;
						}
					}

					// If the second file of the stereo pair is the same directory as the primary file, add it to the vector
					gUtils->SwapCamABExt( secondFileTmp );
					res = gUtils->FileExists( secondFileTmp );
					if ( res )
					{
						strcpy( secondaryFileName, secondFileTmp );
						strcpy( job.infile, secondaryFileName );
						jobVector.push_back( job );
					}
					else // Get the matching _right pair from the secondary directory
					{
						strcpy( secondaryFileName, secondaryDir );
						strcat( secondaryFileName, "\\" );
						strcat( secondaryFileName, gUtils->TrimPath( exportFile ) );
						gUtils->SwapCamABExt( secondaryFileName );

						res = gUtils->FileExists( secondaryFileName );

						if ( res )
						{
							job.seqInfo.mSIVmode = SIV_FILE_CAM_RIGHT;
							strcpy( job.infile, secondaryFileName );

							jobVector.push_back( job );
						}
						else
						{
							// If second file of a pair is not found anywhere, resize the vector to n - 1
							int size = jobVector.size();
							std::vector<TRenderJob>::iterator iter = jobVector.begin() + ( size - 1 );

							if ( size > 1 )
							{
								jobVector.erase( iter );
							}
							else
							{
								if ( allClips == false )
								{
									sivItems = 1;
								}
							}
						}
					}
				}
			}
		}

		switch( mode )
		{
			case UIEXPORT_AVI:
				mProgressDialog = new CUIProgressDialog( FILE_TYPE_AVI, sivItems );
				break;
			case UIEXPORT_QT:
				mProgressDialog = new CUIProgressDialog( FILE_TYPE_MOV, sivItems );
				break;
			case UIEXPORT_DNG:
				mProgressDialog = new CUIProgressDialog( FILE_TYPE_DNG, i );
				break;
			case UIEXPORT_DPX:
				mProgressDialog = new CUIProgressDialog( FILE_TYPE_DPX, i );
				break;
		}	

		DeleteExportDialog();

		Convert( jobVector, mode, interleavedCF, preview, overwrite );
	}
	return true;
}

void CUIScreen::DeleteExportDialog( void )
{
	delete mExportDialog;
	mExportDialog = NULL;
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::ShowExportError()
{
	char str[128];
	sprintf( str, "ERROR - EXPORT FILE/DIRECTORY EXISTS" );
	mMessageBox2 = new CUIMessageBox( MB_TYPE_DIRECTORY_ERROR, 380, 275, 520, 170, "MessageBox" );
	mMessageBox2->Init( str, "" );
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::ProgressCancel( void )
{
	if ( !gRenThread )
	{
		Globals.SetHighBlackLevel( false );
		Globals.SetPolledEvent( POLLED_EVENT_CANCELBLACK );
		return;
	}

	ClearProgramStateFlag( PROGRAM_STATE_FLAG_RENDERING );

	if ( gRenThread )
	{
		while( true )
		{
			if ( Globals.GetPolledEvents() & POLLED_EVENT_DONERENDERTHREAD )
			{
				Globals.ClearPolledEvent(POLLED_EVENT_DONERENDERTHREAD);
				gConfig.DeleteRenderThread();
			}
			if ( !gRenThread )
			{
				break;
			}
			Sleep(10);
		}
	}

	delete mProgressDialog;
	mProgressDialog = NULL;
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::ShutterClose( void )
{
    bool blackOK = (mShutterDialog->mOldExtendedMS == gConfig.mExtendedMS);
	delete mShutterDialog;
	mShutterDialog = NULL;
    if ( !blackOK )
    {
        gConfig.StopGrabbing(false,false);
	    gDeviceManager.ImportBlackImage();
	    gConfig.StartGrabbing();
    }
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::TimecodeClose( bool doit )
{
	/*if ( doit )
	{
		gPrefs.SetTimecodePrefs( &mTimecodeDialog->mTimecodePrefs ); 
        SetGlobalTimecodes( true );
	}

	delete mTimecodeDialog;
	mTimecodeDialog = NULL;
	InvalidateRect( Globals.hwnd, NULL, false );*/
}

void CUIScreen::Timecode2Close( bool doit )
{
	if ( doit && isProgramConfigured() )
	{
		gPrefs.SetTimecodePrefs2( &mTimecode2Dialog->mTimecodePrefs2 ); 
        gPrefs.GetTimecodePrefs2( gTimecodeController.prefs() );
        gTimecodeController.Start( true );
	}

	delete mTimecode2Dialog;
	mTimecode2Dialog = NULL;
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::ModesOk( void )
{
	ModesCancel();
}

void CUIScreen::ModesCancel( void )
{
	delete mModesDialog;
	mModesDialog = NULL;
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::EditText( char *text )
{
	mKeyboard = new CUIKeyboard(text);
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::OpenFileDialog( EFileType type, char *destText )
{
	mFileDialog = new CUIFileDialog( type, destText );
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::OpenMacroListDialog( DWORD index )
{
	mMacroListDialog = new CUIMacroListDialog( index );
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::OpenDisplayModesDialog( DWORD index )
{
	mDisplayModesListDialog = new CUIDisplayModesListDialog( index );
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::LoadPrefs( void )
{
	mPrefs = new CUIPrefs;
	InvalidateRect( Globals.hwnd, NULL, false );
}


void CUIScreen::LoadDisplayPrefs()
{
	mDisplayPrefsDialog = new CUIDisplayPrefs();
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::Load3DAlignmentGuidesDlg()
{
	mGuidesDialog = new CUIGuidesDlg();
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::Close3DAlignmentGuidesDlg( bool action )
{
	if ( action == true )
	{
		// 3D alignment guides
		if ( mDisplayPrefsDialog->m3DAlignmentGuides->GetState() )
		{
			if ( mDisplayPrefsDialog->m3DAlignmentGuidesPrct->GetState() )
			{
				gPrefs.Set3DAlignmentGuideMode( GUIDES_3D_PRCT );
			}
			else
			{
				gPrefs.Set3DAlignmentGuideMode( GUIDES_3D_PIX );
			}
		}
		else
		{
			gPrefs.Set3DAlignmentGuideMode( GUIDES_3D_NONE );
		}

		float val = 1.0f;
		val = strtod( mGuidesDialog->m3DAlignmentGuidesVal->GetText(), NULL );
		gPrefs.Set3DAlignmentValue( val );
	}

	delete mGuidesDialog;
	mGuidesDialog = NULL;
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::CloseDisplayPrefs( bool action )
{
	if ( action )
	{
		// Demosaic settings
		if ( mDisplayPrefsDialog->mBtnDemosaicSettings[ UIDISPLAYPREFS_QUAD_PREV ]->GetState() )
			gPrefs.SetPreviewDrawingMode( PREFS_DISPLAY_BAYER_QUADLET );
		else if ( mDisplayPrefsDialog->mBtnDemosaicSettings[ UIDISPLAYPREFS_BILIN_PREV ]->GetState() )
			gPrefs.SetPreviewDrawingMode( PREFS_DISPLAY_BAYER_BI );
		else if( mDisplayPrefsDialog->mBtnDemosaicSettings[ UIDISPLAYPREFS_HEX_PREV ]->GetState() )
			gPrefs.SetPreviewDrawingMode( PREFS_DISPLAY_BAYER_HEXLET );

		if ( mDisplayPrefsDialog->mBtnDemosaicSettings[ UIDISPLAYPREFS_QUAD_REC ]->GetState() )
			gPrefs.SetRecordDrawingMode( PREFS_DISPLAY_BAYER_QUADLET );
		else if ( mDisplayPrefsDialog->mBtnDemosaicSettings[ UIDISPLAYPREFS_BILIN_REC ]->GetState() )
			gPrefs.SetRecordDrawingMode( PREFS_DISPLAY_BAYER_BI );
		else if( mDisplayPrefsDialog->mBtnDemosaicSettings[ UIDISPLAYPREFS_HEX_REC ]->GetState() )
			gPrefs.SetRecordDrawingMode( PREFS_DISPLAY_BAYER_HEXLET );

		if ( mDisplayPrefsDialog->mBtnDemosaicSettings[ UIDISPLAYPREFS_QUAD_PLAY ]->GetState() )
			gPrefs.SetPlaybackDrawingMode( PREFS_DISPLAY_BAYER_QUADLET );
		else if ( mDisplayPrefsDialog->mBtnDemosaicSettings[ UIDISPLAYPREFS_BILIN_PLAY ]->GetState() )
			gPrefs.SetPlaybackDrawingMode( PREFS_DISPLAY_BAYER_BI );
		else if( mDisplayPrefsDialog->mBtnDemosaicSettings[ UIDISPLAYPREFS_HEX_PLAY ]->GetState() )
			gPrefs.SetPlaybackDrawingMode( PREFS_DISPLAY_BAYER_HEXLET );


		// Open GL state change
		if ( gPrefs.GetOpenGL() != mDisplayPrefsDialog->mOpenGL->GetState() )
		{
			OpenGLStateChanged();
		}

		// General display settings
		if ( mDisplayPrefsDialog->mBtnDisplaySettings[ 0 ]->GetState() )
			gPrefs.SetDispOverlays( 1 );
		else
			gPrefs.SetDispOverlays( 0 );

		if ( mDisplayPrefsDialog->mBtnDisplaySettings[ 1 ]->GetState() )
		{
			gPrefs.SetHistogramExt( 1 );
		}
		else
		{
			gPrefs.SetHistogramExt( 0 );
			Globals.SetHistogramOn( false );
		}

		// Crosshair
		if ( mDisplayPrefsDialog->mDisplayCrosshair->GetState() )
			gPrefs.SetCrosshairState( 1 );
		else
			gPrefs.SetCrosshairState( 0 );

		// Scale external monitor
		if ( mDisplayPrefsDialog->mScaleExtMonitor->GetState() )
			gPrefs.SetScaleExternalMonitor( 1 );
		else
			gPrefs.SetScaleExternalMonitor( 0 );

		// 3D alignment guides
		if ( mDisplayPrefsDialog->m3DAlignmentGuides->GetState() )
		{
			// Convert the char to a float which we can store in prefs
			float val = 1.0f;
			val = strtod( mDisplayPrefsDialog->m3DAlignmentGuidesVal->GetText(), NULL );

			if ( gUtils->IsEqual( 0.0f, val ) )
			{
				val = 1.0f;
			}

			if ( mDisplayPrefsDialog->m3DAlignmentGuidesPrct->GetState() )
			{
				gPrefs.Set3DAlignmentValue( val );
				gPrefs.Set3DAlignmentGuideMode( GUIDES_3D_PRCT );
			}
			else
			{
				gPrefs.Set3DAlignmentValue( (int) val );
				gPrefs.Set3DAlignmentGuideMode( GUIDES_3D_PIX );
			}
		}
		else
		{
			// Convert the char to a float which we can store in prefs
			float val = 1.0f;
			val = strtod( mDisplayPrefsDialog->m3DAlignmentGuidesVal->GetText(), NULL );

			if ( gUtils->IsEqual( 0.0f, val ) )
			{
				val = 1.0f;
			}

			if ( mDisplayPrefsDialog->m3DAlignmentGuidesPrct->GetState() )
			{
				gPrefs.Set3DAlignmentValue( val );
				gPrefs.Set3DAlignmentGuideMode( GUIDES_3D_PRCT );
			}
			else
			{
				gPrefs.Set3DAlignmentValue( (int) val );
				gPrefs.Set3DAlignmentGuideMode( GUIDES_3D_PIX );
			}

			gPrefs.Set3DAlignmentGuideMode( GUIDES_3D_NONE );
		}

		float val = 0.0f;
		val = strtod( mDisplayPrefsDialog->m3DAlignmentGuidesVal->GetText(), NULL );

		if ( gUtils->IsEqual( 0.0f, val ) )
		{
			val = 1.0f;
		}

		gPrefs.Set3DAlignmentValue( val );


		// 3D anaglyph type
		gPrefs.SetAnaglyphType( mDisplayPrefsDialog->mAnaglyphTypeIndex );
		
		// Active 3D main GUI mode
		if ( mDisplayPrefsDialog->mBtnActive3DGUIMode[ 0 ]->GetState() )
			gPrefs.SetDisp3DGuiMix( true );
		else
			gPrefs.SetDisp3DGuiMix( false );

		if ( mDisplayPrefsDialog->mBtnActive3DGUIMode[ 1 ]->GetState() )
			gPrefs.SetDisp3DGuiAnaglyph( true );
		else
			gPrefs.SetDisp3DGuiAnaglyph( false );

		if ( mDisplayPrefsDialog->mBtnActive3DGUIMode[ 2 ]->GetState() )
			gPrefs.SetDisp3DGuiSplit( true );
		else
			gPrefs.SetDisp3DGuiSplit( false );

		if ( mDisplayPrefsDialog->mBtnActive3DGUIMode[ 3 ]->GetState() )
			gPrefs.SetDisp3DGuiSxS( true );
		else
			gPrefs.SetDisp3DGuiSxS( false );

		if ( mDisplayPrefsDialog->mBtnActive3DGUIMode[ 4 ]->GetState() )
			gPrefs.SetDisp3DGuiWiggle( true );
		else
			gPrefs.SetDisp3DGuiWiggle( false );


		if ( mDisplayPrefsDialog->mActivateExternalDispBtn->GetState() )
			gPrefs.SetDispActivateExtDisplay( 1 );
		else
			gPrefs.SetDispActivateExtDisplay( 0 );

		// Standard guide
		if ( mDisplayPrefsDialog->mStandardGuide->GetState() )
			gPrefs.SetStandardGuide( 1 );
		else
			gPrefs.SetStandardGuide( 0 );

		Globals.mNewOpenGLState = mDisplayPrefsDialog->mOpenGLState;
		gPrefs.SetStandardGuideVal( mDisplayPrefsDialog->mStandardGuideIndex );
		gPrefs.Write();
	}

	delete mDisplayPrefsDialog;
	mDisplayPrefsDialog = NULL;
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::LoadClipInfoEditor()
{
	char * filePath = Globals.GetCurrentPlaybackFile();

	if ( ( isFile( filePath, "avi" ) ) || ( isFile( filePath, "mov" ) ) )
	{
		mMetaDataCtrl = new CUIMetaDataCtrl( filePath );
	}
	else
	{
		char sivFileA[ MAX_PATH ];
		char sivFileB[ MAX_PATH ];

    	char secondFile[ 512 ];
        std::vector<char*> *SIVPaths = new std::vector<char*>();
        SIVPaths->push_back( filePath );
        strcpy( secondFile, filePath );
        bool res = AddStereoFile( secondFile, SIVPaths );

		// If the second file of a stereo SIV pair cannot be found, resize the vector to 1
		if ( ! res )
		{
			SIVPaths->resize( 1 );
		}

		int size = SIVPaths->size();
		strcpy( sivFileA, SIVPaths->at( 0 ) );

		if ( size > 1 )
		{
			strcpy( sivFileB, SIVPaths->at( 1 ) );
		}


		// Important - make sure that the 'first' file of a stereo pair is always the left file
		int isLeft = gUtils->IsSIVStereoPair( sivFileA );
		if ( ( isLeft == SIV_FILE_CAM_RIGHT ) && ( size > 1 ) ) 
		{
			strcpy( sivFileA, SIVPaths->at( 1 ) );
			strcpy( sivFileB, SIVPaths->at( 0 ) );

			SIVPaths->clear();

			SIVPaths->push_back( sivFileA );
			SIVPaths->push_back( sivFileB );
		}

		mMetaDataCtrl = new CUIMetaDataCtrl( SIVPaths );

        delete SIVPaths;
	}
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::CloseMetaDataCtrl()
{
	delete mMetaDataCtrl;
	mMetaDataCtrl = NULL;
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::LoadIridas( char *filename, int index )
{
	gIridas = new CUIIridas( filename, index );
	gIridas->mOldVideoDisplayMode = mVideoDisplayMode;
	mVideoDisplayMode = VIDEO_DISPLAY_IRIDAS;
	gDisplay.ResetImageCoordinates();
	Globals.SetIridasOn( true );

	// Update the player histogram
	if ( isProgramPlayingAVI() )
	{
		gPlayWnd.UpdateHistogramAVI();
	}
	else if ( isProgramPlayingSIV() )
	{
		gPlaThread->UpdateHistogramSIV();
	}
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::Quit( void )
{
	mMessageBox = new CUIMessageBox( MB_TYPE_QUIT, 380, 275, 520, 170, "MessageBox" );
	mMessageBox->Init("ARE YOU SURE YOU WANT TO QUIT?", "");
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::LoadModes( void )
{
	mModesDialog = new CUIModesDialog;
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::NewProject( void )
{
	mProjectDialog = new CUIProjectDialog;
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::LoadLooks( void )
{
	gLookDialog = new CUILookDialog;
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::LoadMacros( void )
{
	gMacroDialog = new CUIMacroDialog;
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::LoadDispModes()
{
	gDispModesDialog = new CUIDisplayModeDialog;
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::SendSerial( void )
{
	mSerialDialog = new CUISerialDialog;
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::AboutBox( void )
{
	mAboutDialog = new CUIAboutDialog;
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::UpdateFirmware( void )
{
	mFirmwareDialog = new CUIFirmwareDialog;
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::ExportClip( void )
{
	// Stop playback before opening export dialog
	if ( ( gPlaThread ) && ( gPlaThread->mPlayState == PLAY_STATE_PLAY ) )
	{
		OnPlayPause( 0, 0 );
	}
	if ( isProgramPlayingSIV() || isProgramPlayingAVI() )
	{
		mExportDialog = new CUIExportDialog;
		InvalidateRect( Globals.hwnd, NULL, false );
	}
}

void CUIScreen::ShutterDialog( void )
{
	mShutterDialog = new CUIShutterDialog;
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::TimecodeDialog( void )
{
	//mTimecodeDialog = new CUITimecodeDialog;
	//InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::Timecode2Dialog( void )
{
	mTimecode2Dialog = new CUITimecodeDialog2;
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::OpenMetadataDialog()
{
	mMetadataDialog = new CUIDlgMetadata( 230, 115, 800, 485 );

	char filepath[MAX_PATH];
	char returnVal[256];
	DWORD destSize = 256;
	strcpy( filepath, gPrefs.GetRecentProject() );
	
	for ( int i = 0; i < 7; i++ )
	{
		GetPrivateProfileString( "metadata", mMetaDlgText[ i ], "", returnVal, destSize, filepath );
		mMetadataDialog->SetTextBoxContent( i, returnVal );
	}
		
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::CloseMetadataDialog()
{
	delete mMetadataDialog;
	mMetadataDialog = NULL;
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::OnMetadataDialogOK()
{
	char filepath[ MAX_PATH ];
	char retVal[ 128 ];
	strcpy( filepath, gPrefs.GetRecentProject() );
	
	for ( int i = 0; i < 7; i++ )
	{
		if ( strlen( mMetadataDialog->GetTextBoxContent( i ) ) >= 127 )
		{
			strncpy( retVal, mMetadataDialog->GetTextBoxContent( i ), 127 );
		}
		else
		{
			strcpy( retVal, mMetadataDialog->GetTextBoxContent( i ) );
		}

		WritePrivateProfileString( "metadata", mMetaDlgText[ i ], retVal, filepath );
	}

	delete mMetadataDialog;
	mMetadataDialog = NULL;
	InvalidateRect( Globals.hwnd, NULL, false );

	// Update project data
	gPrefs.SetProjectData( false );
	gSlate.UpdateClipData();
	gProject->UpdateProjectInfo();
}

void CUIScreen::OpenSceneShotTakeDialog()
{
	char filepath[MAX_PATH];
	char returnVal[ 4 ];
	int val = 0;
	DWORD destSize = 256;
	strcpy( filepath, gPrefs.GetRecentProject() );

	float x = 0.0f;
	float y = 0.0f;

	if ( mVideoDisplayMode == VIDEO_DISPLAY_NORMAL )
	{
		x = 315;
		y = 120;
	}
	else
	{
		x = ( ( gConfig.mAppFullW / 2 - ( 620 * Globals.mMiniGuiMin ) / 2 ) ) * Globals.mMiniGuiMag;
		y = ( ( ( gConfig.mAppFullH / 2 ) +  gConfig.mAppFullY ) - ( 460 / 2 ) ) * Globals.mMiniGuiMag;
	}

	mSceneShotTakeDialog = new CUIDlgSceneShotTake( x, y, 620, 460 );

	for ( int i = 0; i < 4; i++ )
	{
		GetPrivateProfileString( "scene-shot-take", mSceneShotTakeDlgText[ i ], "", returnVal, destSize, filepath );
		val = atoi ( returnVal );

		// For a new project, the return value will be 0. However, we need '1' for scene, shot and take
		// and 'NONE' for the modifier - therefore, initialize the modifier with 0.
		if ( val == 0 )
		{
			mSceneShotTakeDialog->SetTextBoxContent( i, 1 );

			if ( i == 2 )
			{
				mSceneShotTakeDialog->SetTextBoxContent( i, 0 );
			}
		}
		else
		{
			mSceneShotTakeDialog->SetTextBoxContent( i, val );
		}
	}
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::CloseSceneShotTakeDialog()
{
	// TODO: implement me please
	delete mSceneShotTakeDialog;
	mSceneShotTakeDialog = NULL;
	InvalidateRect( Globals.hwnd, NULL, false );
}


void CUIScreen::OnSceneShotTakeDialogOK()
{
	char filepath[MAX_PATH];
	strcpy( filepath, gPrefs.GetRecentProject() );
	
	for ( int i = 0; i < 4; i++ )
	{
		char val[ 4 ];
		sprintf( val, "%d", mSceneShotTakeDialog->GetTextBoxValue( i ) );
		WritePrivateProfileString( "scene-shot-take", mSceneShotTakeDlgText[ i ], val, filepath );
	}

	if ( gProject )
	{
		Globals.SetNextFileName();
		gPrefs.SetProjectData( false );
		gSlate.UpdateClipData();
		gProject->UpdateProjectInfo();
	}

	delete mSceneShotTakeDialog;
	mSceneShotTakeDialog = NULL;
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::OpenRepairDialog()
{
	mRepairDialog = new CUIDlgRepair( 380, 265, 550, 200 );
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::CloseRepairDialog( bool action )
{
	if ( action == true )
	{
		bool res = gUIScreen->CheckDestDriveSpace( mRepairDialog->mLocation );

		if ( res )
		{
			if ( isFile( mRepairDialog->mLocation, "sib" ) )
			{
				CSibList *sibList = new CSibList();
				sibList->RepairSibFile( mRepairDialog->mLocation );
				delete sibList;
			}
			else if ( isFile( mRepairDialog->mLocation, "mob" ) )
			{
				CSibList *sibList = new CSibList();
				sibList->RepairMobFile( mRepairDialog->mLocation );
				delete sibList;
			}
		}
		else
		{
			OnFileExportDestDriveError( false );
		}
	}
	delete mRepairDialog;
	mRepairDialog = NULL;
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::OpenDefectivePixelmapDialog()
{
	mDefectivePixelmapDlg = new CUIDlgDefectivePixMap( 230, 235, 830, 240 );
	InvalidateRect( Globals.hwnd, NULL, false );
}
	
void CUIScreen::CloseDefectivePixelmapDialog( bool action )
{
	if ( action == true )
	{
		CDevice *device = gDeviceManager.GetDevice( 0 );
		if ( device )
		{
			device->ExportDPCList(mDefectivePixelmapDlg->mFile->GetText(), mDefectivePixelmapDlg->mCameraRaster );
			device->ReadDPCList();
		}
	}

	delete mDefectivePixelmapDlg;
	mDefectivePixelmapDlg = NULL;
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::OpenFindFileMatchDialog()
{
	mFindFileMatchDlg = new CUIDlgFindFileMatch( 230, 235, 830, 240 );
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::CloseDlgFindFileMatch( bool action )
{
	if ( action )
	{
		if ( Globals.GetSecondaryDir() )
		{
			char * secondaryDir = gUIScreen->mFindFileMatchDlg->GetLocation();
			Globals.SetMatchingPairDir( secondaryDir );
			OpenProject( gPrefs.GetRecentProject() );
			gLogger.info( 1, "%d   CUIScreen::CloseDlgFindFileMatch directory location: %s\n", GetTickCount(), secondaryDir );
		}
		else
		{
			char * secondaryDir = gUtils->GetParentDir( gUIScreen->mFindFileMatchDlg->GetLocation() );
			Globals.SetMatchingPairDir( secondaryDir );
			gLogger.info( 1, "%d   CUIScreen::CloseDlgFindFileMatch file location: %s\n", GetTickCount(), secondaryDir );
		}
	}
	delete mFindFileMatchDlg;
	mFindFileMatchDlg = NULL;
	InvalidateRect( Globals.hwnd, NULL, false );
}


void CUIScreen::OpenMultiCamDialog()
{
	mMultiCamModeDlg = new CUIDlgMultiCamMode( 335, 240, 600, 250 );
	InvalidateRect( Globals.hwnd, NULL, false );
}


void CUIScreen::CloseMultiCamDialog( bool action )
{
	if ( action == true )
	{
		if ( mMultiCamModeDlg->mMultiCamModeMaster->GetState() )
		{
			gPrefs.Set3DSync( PREFS_3DSYNC_MASTER );
		}
		else if ( mMultiCamModeDlg->mMultiCamModeSlave->GetState() )
		{
			gPrefs.Set3DSync( PREFS_3DSYNC_SLAVE );
		}
		else
		{
			gPrefs.Set3DSync( PREFS_3DSYNC_DISABLE );
		}
	}

	delete mMultiCamModeDlg;
	mMultiCamModeDlg = NULL;
	InvalidateRect( Globals.hwnd, NULL, false );
}


void CUIScreen::OpenCamConnectionDialog( int index, char * msg )
{
	if ( mVideoDisplayMode == VIDEO_DISPLAY_FULL || mVideoDisplayMode == VIDEO_DISPLAY_FULL_OVERLAY )
	{
		mVideoDisplayMode = VIDEO_DISPLAY_NORMAL;
		gDisplay.ResetImageCoordinates();
	}
	mCamConnectionDlg = new CUIDlgCamConnect( index, msg );
	InvalidateRect( Globals.hwnd, NULL, false );
}	

void CUIScreen::OpenParallaxShiftDlg()
{
	mParallaxShiftDlg = new CUIParallaxShiftDialog();
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::OpenHighBlackLevelDlg()
{
	mHighBlackLevelDialog = new CUIHighBlackLevelDlg();
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::CloseHighBlackLevelDlg( bool action )
{
	Globals.SetHighBlackLevel( false );
	delete mHighBlackLevelDialog;
	mHighBlackLevelDialog = NULL;
	InvalidateRect( Globals.hwnd, NULL, false );

	if ( action )
	{
		gBlkThread->mWriteNoiseFile = true;
	}
	//else
	//{
	//	// Restart black calibration
	//	OnBlackCalibration();
	//}
}

void CUIScreen::CloseParallaxShiftDlg()
{
	mParallaxShift->SetHighlight( false );
	delete mParallaxShiftDlg;
	mParallaxShiftDlg = NULL;
	SignalStereo();
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::CloseCamConnectionDialog()
{
	delete mCamConnectionDlg;
	mCamConnectionDlg = NULL;
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::OpenWhiteBalanceDialog()
{
	mWhiteBalanceDialog = new CUIDlgWhiteBalance( 380, 280, 550, 150 );
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::CloseWhiteBalanceDialog()
{
	delete mWhiteBalanceDialog;
	mWhiteBalanceDialog = NULL;
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::OnWhiteBalanceUser( int pos )
{
	delete mWhiteBalanceDialog;
	mWhiteBalanceDialog = NULL;
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::OpenUserWBDialog()
{
	mUserWBDialog = new CUIUserWB( 690, 360, 460, 260 );
	// TODO: in order to improve mouse performance, we may need to change the video display mode
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::CloseUserWBDialog()
{
	delete mUserWBDialog;
	mUserWBDialog = NULL;
	InvalidateRect( Globals.hwnd, NULL, false );
}


void CUIScreen::CloseFileDialog( void )
{
	delete mFileDialog;
	mFileDialog = NULL;
	InvalidateRect( Globals.hwnd, NULL, false );
}


void CUIScreen::CloseMacroListDialog( void )
{
	delete mMacroListDialog;
	mMacroListDialog = NULL;
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::CloseDisplayModesListDialog()
{
	delete mDisplayModesListDialog;
	mDisplayModesListDialog = NULL;
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::CloseDisplayModesDialog()
{
	delete gDispModesDialog;
	gDispModesDialog = NULL;
	InvalidateRect( Globals.hwnd, NULL, false );
}


void CUIScreen::CloseKeyboard( char *text )
{
	delete mKeyboard;
	mKeyboard = NULL;

	InvalidateRect( Globals.hwnd, NULL, false );

	if ( mProjectDialog )
	{
		mProjectDialog->UpdateProjectInfo();
	}

	InvalidateRect( Globals.hwnd, NULL, false );
}


void CUIScreen::ClosePrefs( bool doit )
{
	if ( doit )
	{
		if ( isProgramPreviewing() )
		{
			gLogger.info( 1, "%d   ClosePrefs A CONFIG STEP %d\n", GetTickCount(), Globals.GetConfigStep() );
			OnUpdateConnectProgress();
			gConfig.StopGrabbing( false, true );
			gLogger.info( 1, "%d   ClosePrefs B CONFIG STEP %d\n", GetTickCount(), Globals.GetConfigStep() );

			for ( int d = 0; d < gDeviceManager.NumDevices(); d++ )
			{
				OnUpdateConnectProgress();
				gDeviceManager.GetDevice( d )->mGrabber.Disconnect();
				gLogger.info( 1, "%d   ClosePrefs C CONFIG STEP %d\n",GetTickCount(),Globals.GetConfigStep());
				OnUpdateConnectProgress();
        		Globals.SetCamState( CAM_STATE_DISCONNECTED, d );
			}
		}

		if ( mPrefs->m12Bit->GetState() )
			gPrefs.SetRecordFormat( PREFS_RECORD_FORMAT_LINEAR12 );
		else if ( mPrefs->m10Bit->GetState() )
			gPrefs.SetRecordFormat( PREFS_RECORD_FORMAT_LOG10 );
		else
			gPrefs.SetRecordFormat( PREFS_RECORD_FORMAT_CINEFORM );

		gPrefs.SetCineformMode3( mPrefs->mCineformMode3 );
		gPrefs.SetInterleaved( mPrefs->mInterleaved->GetState() );

		if ( ! Globals.si2k() )
		{
			gPrefs.SetSI3DMode( mPrefs->mSI3DMode->GetState() );
		}
		else
		{
			gPrefs.SetSI3DMode( false );
		}

		gPrefs.SetGPIOMapping( mPrefs->mGPIOMode );

#ifndef DISABLE_AVI
		if ( mPrefs->mAVI->GetState() )
			gPrefs.SetWrapperType( WRAPPER_TYPE_AVI );
		else
#endif
			gPrefs.SetWrapperType( WRAPPER_TYPE_QT );

        ReinitTemperature();
		if ( mPrefs->mFanMode1->GetState() )
			gPrefs.SetFanMode( 1 );
		else if ( mPrefs->mFanMode2->GetState() )
			gPrefs.SetFanMode( 2 );

		gPrefs.SetDigitalSlate( mPrefs->mDigitalSlate->GetState() );
		gPrefs.SetConformProjectFPS( mPrefs->mConformProjectFPS->GetState() );
		gPrefs.SetStartupFullScreen( mPrefs->mStartupFullScreen->GetState() );

#ifdef MIRROR_FILE_WRITING
		gPrefs.SetMirrorFileWriting( mPrefs->mMirrorFileWriting->GetState() );
#endif

		// Active metadata
		gPrefs.SetActivateSerialMetadata( mPrefs->mActivateSerialMetadata->GetState() );
		gPrefs.SetControllerProtocol( mPrefs->mProtocol );

		if ( canProgramPlay() )
		{
			gPrefs.SetScratchAudio( mPrefs->mScratchAudio->GetState() );
			gConfig.Autoload( (ECamRaster) gPrefs.GetCameraRaster(), (ECamFramerate) gPrefs.GetCameraFramerate(), true );
		}
		else
		{
			assert(0);
		}
	}
	gLogger.info( 1, "%d   CONFIG DONE%d\n",GetTickCount(),Globals.GetConfigStep());
    FlushFiles( Globals.GetSIDir()[0] );
	Globals.SetPolledEvent( POLLED_EVENT_DELETEPREFS );
	OnUpdateConnectProgress();
	gPrefs.Write();
}


void CUIScreen::CloseBlack( bool doit, bool allGains )
{
    gPrefs.SetSaveAllGains(allGains);
	if ( doit )
	{
		assert(!gBlkThread);
		gDeviceManager.PerformFPN();

        if ( gDeviceManager.NumDevices() == 1 )
            gBlkThread = new BLKThread( gDeviceManager.NumDevices(), gDeviceManager.GetDevice(0)->mFFS, 
                NULL, allGains );
        else
            gBlkThread = new BLKThread( 
                gDeviceManager.NumDevices(), 
                gDeviceManager.GetDevice(0)->mFFS, 
                gDeviceManager.GetDevice(1)->mFFS, 
                allGains );
		gBlkThread->StartThread();
		if ( allGains )
			mProgressDialog = new CUIProgressDialog( PROGRESS_TYPE_GAIN, 1 );
	}
}

void CUIScreen::CloseReconnect( bool doit, bool connect )
{
	if ( doit )
	{
		CloseCamConnectionDialog();
		if ( isProgramPreviewing() )
		{
			OnUpdateConnectProgress();
			gConfig.StopGrabbing( false, true );

			for ( int d = 0; d < gDeviceManager.NumDevices(); d++ )
			{
				OnUpdateConnectProgress();
				gDeviceManager.GetDevice( d )->mGrabber.Disconnect();
				OnUpdateConnectProgress();
        		Globals.SetCamState( CAM_STATE_DISCONNECTED, d );
			}
		}

		if ( canProgramPlay() )
		{
			gConfig.Autoload( (ECamRaster) gPrefs.GetCameraRaster(), (ECamFramerate) gPrefs.GetCameraFramerate(), true );
		}
		else
		{
			assert(0);
		}
		OnUpdateConnectProgress();
	}
}


void CUIScreen::CloseModes( bool doit, bool custom )
{
	if ( doit )
	{
		Globals.SetCameraRaster( mModesDialog->mCameraRaster );
		Globals.SetCameraFramerate( mModesDialog->mCameraFramerate );
		gPrefs.SetCameraRaster( Globals.GetCameraRaster() );
		gPrefs.SetCameraFramerate( Globals.GetCameraFramerate() );

		if ( custom == true )
		{
			int size = mModesDialog->GetCommandStringSize();
			char strCmd[ 128 ];

			for ( int i = 0; i < size; i++ )
			{
				strcpy( strCmd, mModesDialog->mCommandCustomList[ i ] );
				gDeviceManager.GetDevice( 0 )->SendSerialCommand( strCmd );
			}

			// Disable shutter control
			mShutterControl->SetActive( false );
		}
		else
		{
			// Enable shutter control if we leave custom mode
			mShutterControl->SetActive( true );
		}

		int frames = atoi( mModesDialog->mTimelapseText->GetText() );
		gPrefs.SetSkipFrames( frames );
		if ( gPrefs.GetTimeLapseMode() == TIME_LAPSE_MODE_SKIP )
		{
			if ( custom == true )
			{
				int n = atoi( mModesDialog->mSkipCustom );
				gPrefs.SetSkipFrames( n );
			}
			else
			{
				int n = atoi( mModesDialog->mTimelapseText->GetText() );
				if ( n >= 2 && n <= 1000000 )
				{
					gPrefs.SetSkipFrames( n );
				}
			}
		}

		// Load config!
		if ( isProgramPreviewing() )
		{
			gConfig.StopGrabbing( false, true );
            gDeviceManager.GetDevice(0)->mGrabber.Disconnect();
            for ( int d = 0; d < gDeviceManager.NumDevices(); d++ )
            {
                gDeviceManager.GetDevice(d)->mGrabber.Disconnect();
        		Globals.SetCamState( CAM_STATE_DISCONNECTED, d );
            }
		}

		if ( canProgramPlay() )
		{
			gPrefs.SetShutterFPS( 8.0f );
			gConfig.Autoload( (ECamRaster) gPrefs.GetCameraRaster(), (ECamFramerate) gPrefs.GetCameraFramerate(), true );
		}
		else
		{
			assert(0);
		}
	}
    FlushFiles( Globals.GetSIDir()[0] );
	Globals.SetPolledEvent( POLLED_EVENT_DELETEMODES );
    OnUpdateConnectProgress();
	gPrefs.Write();
}

void CUIScreen::OpenSDIModes()
{
	mSDIModesDialog = new CUISDIModesDialog();
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::CloseSDIModes( bool doit )
{
	if ( doit )
	{
		if ( mSDIModesDialog->GetSDIMode() != gPrefs.GetSdiFormat() )
		{
			gPrefs.SetSdiFormat( mSDIModesDialog->GetSDIMode() );
		}
		gPrefs.SetSdiPulldown( mSDIModesDialog->GetSDIPulldown() );
		gPrefs.SetSdiGenlock( mSDIModesDialog->GetSDIGenlock() );
		gSdiThread->mDoRestart = true;
        gLogger.info(1,"%s   Signalling SDI thread with new SDI mode, restart.\n",
			Globals.GetPrettyHighResClockString());
		SetEvent(gSdiSignal);
	}
	delete mSDIModesDialog;
	mSDIModesDialog = NULL;
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::ToggleRecording( void )
{
	if ( !gProject ) return;
	if ( Globals.GetDriveFull() ) return;

	static DWORD lastTicks = 0;
	DWORD ticks = GetTickCount();
	if ( ( ticks - lastTicks ) < RECORD_TICKS )
	{
		return;
	}
	lastTicks = ticks;

	if ( ! mIsRecording )
	{
		if ( canProgramRecord() )
		{
			mPlayFS->SetActive( false );
			mIsRecording = 1;
        	gLogger.info( 1, "%d   ToggleRecording ON\n",GetTickCount() );
			StartRecording();
			Globals.SetDisplayDuration( 0 );

			// Close project manager before recording starts
			SetProjectManagerMode( false );
		}
	}
	else
	{
		mPlayFS->SetActive( true );
		mIsRecording = 0;
       	gLogger.info( 1, "%d   ToggleRecording OFF\n",GetTickCount() );

		IncreaseTakeNumber();
		StopRecording();
	}
}

void CUIScreen::DoRecording( bool startRecording )
{
	if ( !gProject ) return;
	if ( Globals.GetDriveFull() ) return;
	if ( !mIsRecording && startRecording )
		ToggleRecording();
	else if ( mIsRecording && !startRecording )
		ToggleRecording();
}


void CUIScreen::StartRecording( void )
{
	gLogger.info(1, "%d   CUIScreen::StartRecording\n\n\n",GetTickCount() );
	if ( Globals.isMen() )
	{
		gLogger.info( 1, "%d ms   SI_SetSMB2TallyLight before\n\n",GetTickCount() );
		gLogger.info( 1, "%d   ConfigInit: SI_SetSMB2TallyLight to true...\n", GetTickCount());
		int ret = SI_SetSMB2TallyLight( true );
		gLogger.info( 1, "%d ms  SI_SetSMB2TallyLight after\n\n",GetTickCount() );
		gLogger.info( 1, "%d   CUIScreen::StartRecording returns: %d\n", GetTickCount(), ret );
	}

#ifdef LENS_DEVICE
	// Create DL file if lens device is attached
	if ( Globals.GetLensDevice() )
	{
		char dlFile[ MAX_PATH ] = "\0";
		ProjectSetName( dlFile, FILE_TYPE_DLF, 0, false );
		int res = SI_OpenDLFile( dlFile );
		gLogger.info( 1, "%d   CUIScreen::SI_OpenDLFile returns: %d\n", GetTickCount(), res );
	}
#endif

	// Exit utility menu
	gUIScreen->SetUtilityMode( false );

	mRecordButton->SetHighlightRed( true );
	mRecordButton->SetText( "STOP" );

	mRecFS->SetHighlightRed( true );
	mRecFS->SetText( "STOP" );
	TurnOffSidePanelsForRecord();

	gTimecodeController.Start( false );
	gTimecodeController.recordTimecode()->Set( 0, 0, 0, 0 );
	gTimecodeController.recordTimecode()->SetTimebase( FramerateInfo[ Globals.GetCameraFramerate()].timebase );

	gConfig.StartRecording();

	// set button states
	mUtilityButton->SetActive( false );
	mPlayModeButton->SetActive( false );
    gDeviceManager.SetLEDState( true );

	mLoadModesButton->SetActive( false );
	mGainButton->SetActive( false );
	mWhiteBalance->SetActive( false );
	mShutterControl->SetActive( false );
	mSpotFocusMeter->SetActive( false );
	mLoadLooks->SetActive( false );

    if ( gShoThread ) gShoThread->ClearDrawn();
}


void CUIScreen::StopRecording( void )
{
	gLogger.info(1, "%d   CUIScreen::StopRecording\n",GetTickCount() );
	if ( Globals.isMen() )
	{
		gLogger.info( 1, "%d   StopRecording: SI_SetSMB2TallyLight to false...\n", GetTickCount() );
		int ret = SI_SetSMB2TallyLight( false );
		gLogger.info( 1, "%d   CUIScreen::StopRecording SI_SetSMB2TallyLight returns: %d\n", GetTickCount(), ret );
	}

	mIsRecording = 0;

	mRecordButton->SetHighlightRed( false );
	mRecordButton->SetText( "RECORD" );
	mRecFS->SetHighlightRed( false );
	mRecFS->SetText( "REC" );
    if ( isProgramEmptying() )
    {
    	mRecordButton->SetActive( false );
	    mRecFS->SetActive( false );
    }

	OnStopRecording();

	// set button states
	mUtilityButton->SetActive( true );
	mPlayModeButton->SetActive( true );
	CheckTemperature( true );
    gDeviceManager.SetLEDState( false );
	mShutterControl->SetActive( true );
	mSpotFocusMeter->SetActive( true );
	mLoadModesButton->SetActive( true );
	mGainButton->SetActive( true );
	mWhiteBalance->SetActive( true );
	mLoadLooks->SetActive( true );

    gTimecodeController.EndRecord();

    if ( gShoThread ) gShoThread->ClearDrawn();

	gPrefs.Write();

	if ( gPrefs.GetOpenGL() )
	    gOglThread->mNewIsPreview = true;
	gLogger.info(1, "%d   CUIScreen::StopRecording DONE\n",GetTickCount() );
}

void CUIScreen::UpdateProgressDialog( int part, int whole )
{
	if ( !mProgressDialog ) return;

	mProgressDialog->mTotalFrames = whole;
	mProgressDialog->mCurrentFrame = part;
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::UpdateProgressDialog( int part, int whole, char * filename )
{
	if ( !mProgressDialog ) return;

	mProgressDialog->mTotalFrames = whole;
	mProgressDialog->mCurrentFrame = part;
	strcpy( mProgressDialog->mFileName, filename );
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::SetUtilityMode( bool on )
{
	mUtilityMode = on;
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::SetFrameStoreMode( bool status )
{
	if ( status )
	{
		mFrameStoreMode = true;
		mProjectManagerMode = false;
		mCamSetupMode = false;
		mWhiteBalanceMode = false;
		mLoadLookMode = false;
		mCamAlignMode = false;

		// Toggle the 'lights'
		mFrameStore->SetHighlight( true );
		mFrameStorePlay->SetHighlight( true );
		mProjectManager->SetHighlight( false );
		mABCamSetup->SetHighlight( false );
		mWhiteBalance->SetHighlight( false );
		mLoadLooks->SetHighlight( false );
		mColorCorrect->SetHighlight( false );
		mCamAlignModeBtn->SetHighlight( false );
	}
	else
	{
		mFrameStoreMode = false;
		mFrameStore->SetHighlight( false );
		mFrameStorePlay->SetHighlight( false );
	}
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::SetProjectManagerMode( bool status )
{
	if ( status )
	{
		mProjectManagerMode = true;
		mFrameStoreMode = false;
		mCamSetupMode = false;
		mWhiteBalanceMode = false;
		mLoadLookMode = false;
		mCamAlignMode = false;

		// Toggle the 'lights'
		mProjectManager->SetHighlight( true );
		mFrameStore->SetHighlight( false );
		mFrameStorePlay->SetHighlight( false );
		mABCamSetup->SetHighlight( false );
		mWhiteBalance->SetHighlight( false );
		mLoadLooks->SetHighlight( false );
		mColorCorrect->SetHighlight( false );
		mCamAlignModeBtn->SetHighlight( false );
	}
	else
	{
		mProjectManagerMode = false;
		mProjectManager->SetHighlight( false );
	}
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::SetCamSetupMode( bool status )
{
	if ( ( gPrefs.Get3DAlignmentGuideMode() ) && ( Globals.GetDraw3D() ) )
	{
		mCamAlignModeBtn->SetActive( true );
	}
	else
	{
		mCamAlignModeBtn->SetActive( false );
	}

	if ( status )
	{
		mCamSetupMode = true;
		mProjectManagerMode = false;
		mFrameStoreMode = false;
		mWhiteBalanceMode = false;
		mLoadLookMode = false;
		mCamAlignMode = false;

		// Toggle the 'lights'
		mABCamSetup->SetHighlight( true );
		mProjectManager->SetHighlight( false );
		mFrameStore->SetHighlight( false );
		mFrameStorePlay->SetHighlight( false );
		mWhiteBalance->SetHighlight( false );
		mLoadLooks->SetHighlight( false );
		mColorCorrect->SetHighlight( false );
		mCamAlignModeBtn->SetHighlight( false );
	}
	else
	{
		mCamSetupMode = false;
		mABCamSetup->SetHighlight( false );
		mParallaxShift->SetHighlight( false );
		SetCamAlignMode( false );
		Globals.SetParallaxMode( PARALLAX_MODE_OFF );
	}
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::SetLoadLookMode( bool status )
{
	if ( status )
	{
		mLoadLookMode = true;
		mCamSetupMode = false;
		mProjectManagerMode = false;
		mFrameStoreMode = false;
		mWhiteBalanceMode = false;
		mCamAlignMode = false;

		// Toggle the 'lights'
		mLoadLooks->SetHighlight( true );
		mColorCorrect->SetHighlight( true );
		mABCamSetup->SetHighlight( false );
		mProjectManager->SetHighlight( false );
		mFrameStore->SetHighlight( false );
		mFrameStorePlay->SetHighlight( false );
		mCamAlignModeBtn->SetHighlight( false );
		mWhiteBalance->SetHighlight( false );
		if ( ! ( isProgramPlayingAVI() || isProgramPlayingSIV() ) )
		{
			mLookAsShot->SetActive( false );
		}
		else
		{
			mLookAsShot->SetActive( true );
			mLookAsShot->SetHighlight( true );
			mLoadLookBtnA->SetHighlight( false );
			mLoadLookBtnB->SetHighlight( false );
			mLoadLookBtnC->SetHighlight( false );
			mLoadLookBtnD->SetHighlight( false );
		}
	}
	else
	{
		mLoadLookMode = false;
		mLoadLooks->SetHighlight( false );
		mColorCorrect->SetHighlight( false );
		mLookAsShot->SetActive( false );

		// Upon exiting of set look mode, display SAVE message if selected look is not AS SHOT look
		if ( ( gPrefs.GetPlayerLook() != 4 ) && ( isProgramPlayingAVI() || isProgramPlayingSIV() ) )
		{
			gUIScreen->SavePlayerLook();
		}
	}
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::SetCamAlignMode( bool status )
{
	if ( status )
	{
		Globals.SetGuide( GUIDE_3D );
		mCamAlignMode = true;
		
		// Toggle the 'lights'
		mCamAlignModeBtn->SetHighlight( true );
		mParallaxShift->SetHighlight( false );
	}
	else
	{
		mCamAlignMode = false;
		mCamAlignModeBtn->SetHighlight( false );
	}
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::SetWhiteBalanceMode( bool status )
{
	if ( status )
	{
		mWhiteBalanceMode = true;
		mCamSetupMode = false;
		mProjectManagerMode = false;
		mFrameStoreMode = false;
		mLoadLookMode = false;

		// Toggle the 'lights'
		mWhiteBalance->SetHighlight( true );
		mABCamSetup->SetHighlight( false );
		mProjectManager->SetHighlight( false );
		mFrameStore->SetHighlight( false );
		mFrameStorePlay->SetHighlight( false );
		mLoadLooks->SetHighlight( false );
		mColorCorrect->SetHighlight( false );
	}
	else
	{
		mWhiteBalanceMode = false;
		mWhiteBalance->SetHighlight( false );
	}
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::IncDisplayMode( void )
{
	mVideoDisplayMode++;
	if ( mVideoDisplayMode == VIDEO_DISPLAY_IRIDAS )
		mVideoDisplayMode = VIDEO_DISPLAY_NORMAL;
 
	if ( mVideoDisplayMode == VIDEO_DISPLAY_FULL_OVERLAY || mVideoDisplayMode == VIDEO_DISPLAY_FULL )
	{
		Globals.SetGeometry( GEOMETRY_FULL );
	}
	else
	{
		if ( gPrefs.GetMode800() )
		{
			Globals.SetGeometry( GEOMETRY_800 );
		}
		else
		{
			Globals.SetGeometry( GEOMETRY_1280 );
		}
	}
}

void CUIScreen::IncDisplay3DMode( void )
{
	if ( Globals.GetDraw3D() )
	{
		// If previous 3D was LEFT or RIGHT, get last 3D mode setting from PREFS
        if ( !gPrefs.GetScaleExternalMonitor() )
		{
		if ( ( Globals.GetDisplay3DMode() == MODE3D_LEFT ) || ( Globals.GetDisplay3DMode() == MODE3D_RIGHT ) )
		{
			Globals.SetDisplay3DMode( ( E3DDisplayMode ) gPrefs.GetDispMode() );
			return;
		}

		// The size of the dynamic array storing the active main 3D GUI modes
		int size = 0;

		if ( gPrefs.GetDisp3DGuiAnaglyph() )
			size++;

		if ( gPrefs.GetDisp3DGuiMix() )
			size ++;

		if ( gPrefs.GetDisp3DGuiWiggle() )
			size ++;

		if ( gPrefs.GetDisp3DGuiSxS() )
			size ++;

		if ( gPrefs.GetDisp3DGuiSplit() )
			size ++;
		
		int * activeModes = NULL;
		activeModes = new int[ size ];
		int index = 0;

		// If no 3D GUI mode is active, switch L/R and get out
		if ( size == 0 )
		{
			OnIncLeftRightMode( 0, 0 );
			return;
		}
		
		for ( int i = 0; i < size; i++ ) 
		{
			activeModes[ i ] = 0;
		}

		// Fill the dynamic array with active main 3D GUI modes
		if ( gPrefs.GetDisp3DGuiAnaglyph() )
		{
			activeModes[ index ] = MODE3D_ANAGLYPH;
			index ++;
		}

		if ( gPrefs.GetDisp3DGuiMix() )
		{
			activeModes[ index ] = MODE3D_5050;
			index ++;
		}

		activeModes[ index ] = MODE3D_DIFFERENCE;
		index ++;

		if ( gPrefs.GetDisp3DGuiWiggle() )
		{
			activeModes[ index ] = MODE3D_WIGGLE;
			index ++;
		}

		if ( gPrefs.GetDisp3DGuiSxS() )
		{
			activeModes[ index ] = MODE3D_SIDE_BY_SIDE;
			index ++;
		}

		if ( gPrefs.GetDisp3DGuiSplit() )
		{
			activeModes[ index ] = MODE3D_SPLITSCREEN;
			index ++;
		}
		
		if ( index > 0 )
		{
			index --;
		}

		if ( ( disp3DGuiModeindex > index ) || ( disp3DGuiModeindex > MODE3D_SPLITSCREEN ) )
		{
			disp3DGuiModeindex = 0;
		}

        //if ( ! gPrefs.GetScaleExternalMonitor()  )
        //{
			if ( disp3DGuiModeindex <= size )
			{
    			Globals.SetDisplay3DMode( (E3DDisplayMode) activeModes[ disp3DGuiModeindex ] );
				gPrefs.SetDispMode( disp3DGuiModeindex );
				InvalidateRect( Globals.hwnd, NULL, false );
			}
        //}

		disp3DGuiModeindex ++;
		// Clean up
		delete [] activeModes;
		activeModes = NULL;
		}

        //if ( gPrefs.GetScaleExternalMonitor() )
		else
        {
			if ( Globals.mScalingMode == SCALEMODE3D_LEFTRIGHT )
			{
				Globals.mScalingMode = SCALEMODE3D_DIFFERENCE;
       			Globals.SetDisplay3DMode( MODE3D_DIFFERENCE );
			}
			else 
			{
				Globals.mScalingMode = SCALEMODE3D_LEFTRIGHT;
       			Globals.SetDisplay3DMode( MODE3D_LEFT );
				gPrefs.SetDispExtModeNoWrite( MODE3D_RIGHT );
			}
            /*Globals.mScalingMode++;
		    if ( Globals.mScalingMode > SCALEMODE3D_LEFTRIGHT ) Globals.mScalingMode = 0;
            switch ( Globals.mScalingMode )
            {
				case SCALEMODE3D_LEFTLEFT:
        			Globals.SetDisplay3DMode( MODE3D_LEFT );
					gPrefs.SetDispExtModeNoWrite( MODE3D_LEFT );
					break;
				case SCALEMODE3D_RIGHTRIGHT:
        			Globals.SetDisplay3DMode( MODE3D_RIGHT );
					gPrefs.SetDispExtModeNoWrite( MODE3D_RIGHT );
					break;
				case SCALEMODE3D_LEFTRIGHT:
        			Globals.SetDisplay3DMode( MODE3D_LEFT );
					gPrefs.SetDispExtModeNoWrite( MODE3D_RIGHT );
					break;
            }*/
		    InvalidateRect( Globals.hwnd, NULL, false );
        }

	}
}

void CUIScreen::IncLeftRightMode( void )
{
	if ( Globals.GetDraw3D() )
	{
		int mode = Globals.GetDisplay3DMode();
        if ( mode == MODE3D_RIGHT )
		{
   	        Globals.SetDisplay3DMode( MODE3D_LEFT );
		}
        else
		{
   	        Globals.SetDisplay3DMode( MODE3D_RIGHT );
		}
	    InvalidateRect( Globals.hwnd, NULL, false );
    }
}

void CUIScreen::ToggleStatusbarMode()
{
	if ( Globals.GetStatusBarMode() )
	{
		Globals.SetStatusBarMode( false );
	}
	else
	{
		Globals.SetStatusBarMode( true );
	}
	InvalidateRect( Globals.hwnd, NULL, false );
}


void CUIScreen::UpdateButtonStates( void )
{
	if ( ( gPrefs.GetWhiteBalance() == PREFS_WB_3200K ) 
	  || ( gPrefs.GetWhiteBalance() == PREFS_WB_5600K )
	  || ( gPrefs.GetWhiteBalance() == PREFS_WB_NONE ) )
	{
		mWhiteBalanceAdjust->SetActive( false );
	}
	else
	{
		mWhiteBalanceAdjust->SetActive( true );
	}

	if ( isProgramRecording() )
	{
		mSaveButton->SetActive( false );
		mGrabButton->SetActive( false );
		mUtilityButton->SetActive( false );
		mProjectManager->SetActive( false );
		mABCamSetup->SetActive( false );
		mFrameStore->SetActive( false );
	}
	else if ( isProgramPreviewing() )
	{
		if ( mFrameStoreMode )
		{
			mSaveButton->SetActive( true );
			mGrabButton->SetActive( true );
			mLoadButton->SetActive( true );
		}
		else
		{
			mQuitButton->SetActive( true );
			mFrameStore->SetActive( true );
			mFrameStorePlay->SetActive( true );
			mPlayModeButton->SetActive( true );
			mUtilityButton->SetActive( true );
			mProjectManager->SetActive( true );
			mABCamSetup->SetActive( true );
		}
    	mRecordButton->SetActive( true );
	    mRecFS->SetActive( true );
	}
	else if ( isProgramPlayingAVI() || isProgramPlayingSIV() )
	{
		if ( mFrameStoreMode )
		{
			mSaveButton->SetActive( true );
			mGrabButton->SetActive( true );
			mLoadButton->SetActive( true );
		}
		else
		{
			mQuitButton->SetActive( true );
			mFrameStore->SetActive( true );
			mFrameStorePlay->SetActive( true );
			mPlayModeButton->SetActive( true );
			mUtilityButton->SetActive( true );
			mProjectManager->SetActive( true );
			mABCamSetup->SetActive( true );
		}
	}
	else
	{
		if ( mFrameStoreMode )
		{
			mSaveButton->SetActive( true );
			mGrabButton->SetActive( true );
			mLoadButton->SetActive( true );
		}
		else
		{
			mQuitButton->SetActive( true );
			mFrameStore->SetActive( true );
			mFrameStorePlay->SetActive( true );
			mPlayModeButton->SetActive( true );
			mUtilityButton->SetActive( true );
			mProjectManager->SetActive( true );
			mABCamSetup->SetActive( true );
		}
	}
}

void CUIScreen::IncreaseTakeNumber()
{
	char filepath[ MAX_PATH ];
	char returnVal[ 4 ];
	char newVal[ 4 ];
	int val = 0;
	DWORD destSize = 256;
	strcpy( filepath, gPrefs.GetRecentProject() );
	
	GetPrivateProfileString( "scene-shot-take", "Take", "", returnVal, destSize, filepath );
	val = atoi ( returnVal );
	val++;
	itoa( val, newVal, 10 );
	WritePrivateProfileString( "scene-shot-take", "Take", newVal, filepath );

	// Update project data
	gPrefs.SetProjectData( false );
	gSlate.UpdateClipData();
	
	if ( gProject )
	{
		gProject->UpdateProjectInfo();
		Globals.SetNextFileName();
	}
}


void CUIScreen::InitializeProjectMetadata( char * filepath )
{
	// Get values from previous project data from prefs
	if ( mProjectDialog->GetApplyMetadata() )
	{
		TProjectData projectData;
		gPrefs.GetProjectData( &projectData );
		
		WritePrivateProfileString( "scene-shot-take", "Scene", projectData.scene, filepath );
		WritePrivateProfileString( "scene-shot-take", "Shot", projectData.shot, filepath );
		WritePrivateProfileString( "scene-shot-take", "Take", projectData.take, filepath );
		WritePrivateProfileString( "scene-shot-take", "Modifier", projectData.modifier, filepath );
		WritePrivateProfileString( "metadata", "Director", projectData.director, filepath );
		WritePrivateProfileString( "metadata", "Producer", projectData.producer, filepath );
		WritePrivateProfileString( "metadata", "DirPhotography", projectData.dirPhotography, filepath );
		WritePrivateProfileString( "metadata", "Production", projectData.production, filepath );
		WritePrivateProfileString( "metadata", "Location", projectData.location, filepath );
		WritePrivateProfileString( "metadata", "Keywords", projectData.keywords, filepath );
		WritePrivateProfileString( "metadata", "Comments", projectData.comments, filepath );
		WritePrivateProfileString( "metadata", "Scriptpage", projectData.scriptPage, filepath );
		WritePrivateProfileString( "ProjectInfo", "EdgeCode", projectData.edgeCode, filepath );
		WritePrivateProfileString( "ProjectInfo", "Framerate", projectData.framerate, filepath );
		
		bool value = false;
		char buffer[ 4 ] = "\0";

		value = projectData.applyProjectInfo;
		itoa ( value, buffer, 10 );
		WritePrivateProfileString( "ProjectInfo", "ApplyProjectInfo", buffer, filepath );

		value = projectData.fileNaming;
		itoa ( value, buffer, 10 );
		WritePrivateProfileString( "ProjectInfo", "UseSSTNaming", buffer, filepath );

		value = projectData.conformProjFPS;
		itoa ( value, buffer, 10 );
		WritePrivateProfileString( "ProjectInfo", "ConformProjFPS", buffer, filepath );
	}
	else
	{	
		// Initialize the values for scene-shot-take
		WritePrivateProfileString( "scene-shot-take", "Scene", "1", filepath );
		WritePrivateProfileString( "scene-shot-take", "Shot", "1", filepath );
		WritePrivateProfileString( "scene-shot-take", "Take", "1", filepath );
		WritePrivateProfileString( "scene-shot-take", "Modifier", "0", filepath );

		// For the new project, initialize the edge code with 0
		WritePrivateProfileString( "ProjectInfo", "EdgeCode", "0", filepath );
	}

	if ( gProject )
	{
		Globals.SetNextFileName();
	}
}

void CUIScreen::Adjust3DGuidesPercentage( float value )
{
	// Convert the char to a float which we can work with
	value += gPrefs.Get3DAlignmentValue();

	int guidesMode = gPrefs.Get3DAlignmentGuideMode();

	if ( guidesMode == GUIDES_3D_PRCT )
	{
		if ( gUtils->IsSmaller( value, 0.500000f ) )
		{
			value = 0.5f;
		}
		if ( gUtils->IsGreater( value, 15.000000f ) )
		{
			value = 15.0f;
		}
	}
	else
	{
		if ( gUtils->IsSmaller( value, 5.0f ) )
		{
			value = 5;
		}
		if ( gUtils->IsGreater( value, 157.0f ) )
		{
			value = 157;
		}
	}

	gPrefs.Set3DAlignmentValue( value );
	InvalidateRect( Globals.hwnd, NULL, false );
}


void CUIScreen::SetOverlayText( char *text, int durationInSeconds )
{
    strcpy(mOverlayText[2],mOverlayText[1]);
    strcpy(mOverlayText[1],mOverlayText[0]);
    strcpy(mOverlayText[0],text);

    if ( durationInSeconds != 0 )
        mOverlayTextEndTimeMS = GetTickCount() + durationInSeconds * 1000;
    else
        mOverlayTextEndTimeMS = -1;

    InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::ClearOverlayText( void )
{
    for (int i = 0; i < MAX_LOGGER_LINES; i++)
        mOverlayText[i][0] = 0;
    mOverlayTextEndTimeMS = 0;
	InvalidateRect( Globals.hwnd, NULL, false );
}

void CUIScreen::RenderOverlayText( void )
{
    // any text to draw?
    if ( strlen(mOverlayText[0]) == 0 ) 
        return;

    // have we timed out?
    if ( mOverlayTextEndTimeMS != -1 )
    {
        if ( mOverlayTextEndTimeMS < GetTickCount() )
        {
            ClearOverlayText();
            return;
        }
    }

    // find lower left corner of video display
    float x = gDisplay.mPolyX + 8;
    float y = gDisplay.mPolyY + gDisplay.mPolyH - 30;

    // draw the text
	GIGE_SETFONTCOLOR( 1, 0.2f, 0.2f );
    for (int i = 0; i < MAX_LOGGER_LINES; i++)
    {
        if ( strlen(mOverlayText[i]) > 0 )
        {
	        GIGE_STRINGOUT(x, y, mOverlayText[i] );
            y -= 36.0f;
        }
    }
}

void CUIScreen::DrawFlipText( char * text )
{
	// Get values for playback or preview from CDisplay
	int flipL =  gDisplay.mFlip[ 0 ];
	int flipR =  gDisplay.mFlip[ 1 ];

	// Single channel playback/preview
	if ( ! Globals.GetDraw3D() )
	{
		switch ( flipL )
		{
			case 0:
				sprintf( text, "%s", "  " );
				break;
			case 1:
				sprintf( text, "%s", "FX" );
				break;
			case 2:
				sprintf( text, "%s", "FY" );
				break;
			case 3:
				sprintf( text, "%s", "XY" );
				break;
		}
	}
	else // Dual channel playback/preview
	{
		switch ( flipL )
		{
			case 0:
				strcpy( text, "  " );
				break;
			case 1:
				strcpy( text, "FX" );
				break;
			case 2:
				strcpy( text, "FY" );
				break;
			case 3:
				strcpy( text, "XY" );
				break;
		}
		switch ( flipR )
		{
			case 0:
				strcat( text, " " );
				break;
			case 1:
				strcat( text, " FX" );
				break;
			case 2:
				strcat( text, " FY" );
				break;
			case 3:
				strcat( text, " XY" );
				break;
		}
	}
}

int CUIScreen::Get3DDisplayChannel()
{
	if ( Globals.GetDraw3D() )
    {
    	int displayMode = Globals.GetLogical3DMode();
		if ( displayMode == MODE3D_LEFT ) return 1; // RECALL-L
        if ( displayMode == MODE3D_RIGHT ) return 2; // RECALL-R
		return -1; // no recall text
    }
	else
		return 0; // RECALL
}


bool CUIScreen::CheckDestDriveSpace( char * inFile )
{
	__int64 availableSize = 0;
	char dirPath[ MAX_PATH ];
	strcpy( dirPath, inFile );

	char * path = gUtils->GetParentDir( dirPath );
	strcat( path, "\\" );

	// Get the available space on the destination drive
	ULARGE_INTEGER FreeBytesAvailable;
	ULARGE_INTEGER TotalNumberOfBytes;
	BOOL res = GetDiskFreeSpaceEx( path, &FreeBytesAvailable, &TotalNumberOfBytes, NULL );

	availableSize = FreeBytesAvailable.QuadPart;
	gLogger.info( 1, "CUIScreen::CheckDestDriveSpace available space dest drive: %d\n", availableSize );

	__int64 size = gUtils->GetFileSize64( inFile );
	gLogger.info( 1, "CUIScreen::CheckDestDriveSpace src file: %s size: %d\n", inFile, size );

	if ( size > availableSize )
	{
		return false;
	}
	else
	{
		return true;
	}	
}

void CUIScreen::MouseMoveRightClick( int deltaX, int deltaY )
{
	gLogger.info( 1, "CUIScreen::MouseMoveRightClick MOUSE DELTA X: %d DELTA y: %d\n", deltaX, deltaY );

	if ( deltaX > 0 )		// Left
	{
		OnRecordClick( 0, 0 );
	}
	else if ( deltaX < 0 )	// Right
	{
		OnPlayModeClick( 0, 0 );
		OnPlayPlay( 0, 0 );
	}
	else if ( deltaY > 0 )	// Up
	{
		OnZoomClick( 0, 0 );
	}
	else if ( deltaY < 0 )	// Down
	{
		int cycle = Globals.GetRightClickCycle();
		cycle ++; 

		gLogger.info( 1, "CUIScreen::MouseMoveRightClick CYCLE: %d\n", cycle );

		switch ( cycle )
		{
			case RIGHT_CLICK_CYCLE_SOBEL:
				OnToggleSobel( 0, 0 );
				break;

			case RIGHT_CLICK_CYCLE_EXPOSURE:
				Globals.SetSobelOn( false );
				OnExposureMeterClick( 0, 0 );
				break;

			case RIGHT_CLICK_CYCLE_LAST:
				cycle = RIGHT_CLICK_CYCLE_NONE;
				OnExposureMeterClick( 0, 0 );
				Globals.SetSobelOn( false );
				break;
		}

		Globals.SetRightClickCycle( cycle );
	}
}

void CUIScreen::TurnOffSidePanelsForRecord()
{
	if ( gUIScreen->mABCamSetup->GetHighlight() )
		gUIScreen->SetCamSetupMode( false );
	if ( gUIScreen->mFrameStore->GetHighlight() )
		gUIScreen->SetFrameStoreMode( false );
	if ( gUIScreen->mWhiteBalance->GetHighlight() )
		gUIScreen->SetWhiteBalanceMode ( false );
	if ( gUIScreen->mProjectManager->GetHighlight() )
		gUIScreen->SetProjectManagerMode( false );
	if ( gUIScreen->mLoadLooks->GetHighlight() )
		gUIScreen->SetLoadLookMode( false );
	UpdateButtonStates();
}

void CUIScreen::SetSobel( bool state )
{
    if ( gD3D->IsUsingExternalMonitor() && Globals.si2k() ) 
    {
    	if ( state )
	    {
		    int x = ( UI_IMAGE_X + UI_IMAGE_W / 2 ) * Globals.mMiniGuiMin;
		    int y = ( UI_IMAGE_Y + UI_IMAGE_H / 2 ) * Globals.mMiniGuiMin;

    		if ( gUIManager->mScreen->GetVideoDisplayMode() != VIDEO_DISPLAY_NORMAL )
	    	{
		    	x = ( ( gConfig.mAppFullX + ( gConfig.mAppFullW / 2 ) ) );
			    y = ( ( gConfig.mAppFullY + ( gConfig.mAppFullH / 2 ) ) );
		    }
		
    		Globals.SetLoupeState( LOUPE_NUM_STEPS );
        	gLoupe->mIsSobel = true;
	    	gLoupe->HandleClick( x, y );
    		Globals.SetFocusCheck( true );
	    }
    	else
	    {
    	    gLoupe->mIsSobel = false;
		    Globals.SetLoupeState( 0 );
		    Globals.SetFocusCheck( false );
	    }
    }
    else
    {
        if ( state )
    		Globals.SetSobelOn( true );
    }
}

void CUIScreen::SetSobelExternal( bool state )
{
    if ( gD3D->IsUsingExternalMonitor() && Globals.si2k() ) 
    {
    }
    else
    {
        if ( state )
    		Globals.SetSobelExternalOn( true );
    }
}


