AeRefFieldUI.c++ 3.05 KB
#include <assert.h>
#include <string.h>
#include <Vk/VkPopupMenu.h>

#include "GList.h"
#include "AeTypes.h"
#include "AeBinEditor.h"
#include "AeAsset.h"
#include "AeAssetBin.h"
#include "AeAssetBinUI.h"
#include "AeAssetUI.h"
#include "AeFieldUI.h"
#include "AeBinView.h"



AeRefFieldUI::AeRefFieldUI (const char * name,
			    Boolean isEditable,
			    Boolean isSelectable,
			    Boolean isHierarchical,
			    TJustify justify,
			    AeAssetBin * refBin,
			    TRefGetProc getProc,
			    TRefSetProc setProc) : 
    AeFieldUI (name, isEditable, isSelectable, isHierarchical, False, justify, kStringMaxLen)
{
    fGetProc = getProc;
    fSetProc = setProc;
    fRefBin = refBin;
}


AeRefFieldUI::~AeRefFieldUI (void)
{
}


void
AeRefFieldUI::GetValueString (AeAsset * asset, String string)
{
    AeAsset * value = fGetProc (asset);

    if (value)
	strcpy (string, value->GetName ());
    else
	strcpy (string, "--");
}


Boolean
AeRefFieldUI::SetValueString (AeAsset * asset, String string)
{
    assert (fSetProc);

    if (fRefBin)
    {
	AeAsset *	refAsset;
	
	if (fRefBin->FindNamedAsset (string, refAsset))
	{
	    if (refAsset != fGetProc (asset))
	    {
		fSetProc (asset, refAsset);
		return True;
	    }
	}
    }

    return False;
}


Boolean
AeRefFieldUI::VerifyValueString (String string)
{
    AeAsset *	refAsset;
    Boolean	okay = True;

    if (fRefBin)
	okay = fRefBin->FindNamedAsset (string, refAsset);

    return okay;
}


Boolean
AeRefFieldUI::SetValue (AeAsset * asset, void * refAsset)
{
    assert (fSetProc);
    
    if (refAsset == fGetProc (asset))
	return False;

    fSetProc (asset, (AeAsset *)refAsset);
    return True;
}


AeAsset *
AeRefFieldUI::GetValue (AeAsset * asset)
{
    assert (fGetProc);
    return fGetProc (asset);
}


Boolean
AeRefFieldUI::HandleButtonPress (AeBinEditor * editor,
				 AeAssetUI * assetUI,
				 XEvent * event,
				 TEditModifier modifier)
{
    assert (editor);
    assert (assetUI);

    if (modifier == kModMotion)
	return False;

    AeBinEditor::fMenuEditor = editor;
    AeBinEditor::fMenuAssetUI = assetUI;
    AeBinEditor::fMenuFieldUI = this;

    AeBinView * view = editor->GetView ();
    addMenuItems (view->PopupGet ());

    view->PopupShow (event);

    return True;
}


void
AeRefFieldUI::addMenuItems (VkPopupMenu * popup)
{
    // if we have a reference asset bin and a popup menu to
    // display the reference assets in, then continue
    if (fRefBin)
    {
	GList<AeAsset *> *	refAssetList;
	AeAsset *	       	refAsset;
	int	       		i;

	refAssetList = fRefBin->GetAssetList ();

	refAssetList->Start (i);
	while (refAssetList->Next (i, refAsset))
	    popup->addAction (refAsset->GetName (), &AeBinEditor::MenuCallback, (XtPointer)refAsset);

	if (popup->numItems () == 0)
	    popup->addLabel ("none available");
    }
}


void
AeRefFieldUI::HandleDoubleClick (AeBinEditor * editor,
				 AeAssetUI * assetUI,
				 XEvent * event,
				 TEditModifier modifier)
{
    assert (editor);
    assert (assetUI);

    AeAsset * refAsset = GetValue (assetUI->GetAsset ());

    editor->SelectTypedBinUI (refAsset->GetType ());
}