AeAssetUI.c++ 3.34 KB
#include <string.h>
#include <assert.h>
#include <Xm/Xm.h>

#include "AeTypes.h"
#include "AeAssetUI.h"
#include "AeFieldUI.h"

const char kShiftString[] = "     ";


AeAssetUI::AeAssetUI (AeAsset * asset)
{
    fAsset = asset;
    fRow = 0;
    fLevel = 0;
    fIsOpen = False;
}

AeAssetUI::~AeAssetUI (void)
{
}


void
AeAssetUI::SetRow (int row)
{
    assert (row >= 0);
    fRow = row;
}


void
AeAssetUI::SetLevel (int level)
{
    assert (level >= 0);
    fLevel = level;
}


void
AeAssetUI::SetOpen (Boolean isOpen)
{
    if (fAsset->CanContain ())
	fIsOpen = isOpen;
}


Boolean
AeAssetUI::IsEditable (AeFieldUI * fieldUI)
{
    assert (fieldUI);

    if (!isFieldInUI (fieldUI))
	return False;

    return fieldUI->IsEditable (fAsset);
}


TAssetType
AeAssetUI::GetType (void)
{
    return fAsset->GetType ();
}


Pixmap
AeAssetUI::GetIcon (void)
{
    return fAsset->GetIcon ();
}


void
AeAssetUI::GetFieldString (AeFieldUI * fieldUI, String string)
{
    assert (fieldUI);

    if (!isFieldInUI (fieldUI))
    {
	*string = '\0';
    }
    else 
    {
	// otherwise, get the field's string and make it look nice
	// if we have to.
	fieldUI->GetValueString (fAsset, string);

	// If the field is hierarchical and the asset is not at the top level
	// shift the string over to indicate it's place in the hierarchy.
	if (fLevel && fieldUI->IsHierarchical ())
	    shiftString (string);
    }
}


Boolean
AeAssetUI::VerifyAndSetFieldString (AeFieldUI * fieldUI, String string, Boolean * pChanged)
{
    Boolean	okay = False;     

    assert (fieldUI);
    assert (IsEditable (fieldUI));

    // always strip leading spaces.  spaces used for hierarchical display.
    stripString (string);

    if (fieldUI->VerifyValueString (string))
    {
	TString63	oldString;

	// get the current "old" string
	fieldUI->GetValueString (fAsset, oldString);
	
	if (*pChanged = fieldUI->SetValueString (fAsset, string))
	    updateWidth (fieldUI, string, oldString);
	
	okay = True;
    }

    return okay;
}


void
AeAssetUI::updateWidth (AeFieldUI * fieldUI, String string, String oldString)
{
    TString63	newString;

    // make a copy of the string
    strcpy (newString, string);

    // shift the strings if necessary
    if (fLevel && fieldUI->IsHierarchical ())
    {
	shiftString (oldString);
	shiftString (newString);
    }

    // ask the field to update its width variables
    fieldUI->UpdateWidth (newString, oldString);
}


void
AeAssetUI::shiftString (String string)
{
    char	tempString[kNameMaxLen];
    int		i, level;

    // copy the string so we can insert spaces in front of it
    strcpy (tempString, string);

    // make sure we don't exceed the allocated string length
    level = (kNameMaxLen - strlen(string))/strlen(kShiftString);
    if (level > fLevel)
	level = fLevel;

    // insert spaces for each level down in the hierarchy
    *string = '\0';
    for (i=0; i<level; i++)
	strcat (string, kShiftString);

    // append the string to the inserted spaces
    strcat (string, tempString);
}


void
AeAssetUI::stripString (String string)
{
    char *	p = string;

    while (*p == ' ')
	p++;

    if (p != string)
	strcpy (string, p);
}


// if the assetUI is not at the top of the hierarchy,
// then see if its fields match the top field and if it doesn't
// the don't display anything.
Boolean
AeAssetUI::isFieldInUI (AeFieldUI * fieldUI)
{
    return fLevel == 0 || fieldUI->IsCommon ();
}