AeFieldUI.c++ 3.94 KB
#include <assert.h>
#include <string.h>
#include <math.h>
#include <Vk/VkApp.h>

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


double	AeFieldUI::fLabel2CellFontRatio = 1.0;

 
AeFieldUI::AeFieldUI (const char * name,
		      Boolean isEditable,
		      Boolean isSelectable,
		      Boolean isHierarchical,
		      Boolean isCommon,
		      TJustify justify,
		      int maxLen)
{
    strcpy (fName, name);

    fJustify = justify;
    fIsEditable = isEditable;
    fIsSelectable = isSelectable;
    fIsHierarchical = isHierarchical;
    fIsCommon = isCommon;
    fMaxLen = maxLen;

    fMaxWidth = kMaxWidth;
    fWidth = 0;
    fNeedsWidthUpdate = False;	// Will be set to True only by UpdateWidth
    fNeedsWidthRecalc = True;	// Force calculation of field width

    // Since text widths are calculated using the text font, adjust the
    // label width by the width ratio between cell font and label font.
    // Otherwise, labels may get clipped in the UI.
    fLabelWidth = (int)ceil(((double)strlen (name) * fLabel2CellFontRatio)) + 1;
}

AeFieldUI::~AeFieldUI (void)
{
}
 
void
AeFieldUI::GetValueString (AeAsset * asset, String string)
{
    assert (1);		// pure virtual called
}

Boolean
AeFieldUI::SetValueString (AeAsset * asset, String string)
{
    assert (1);		// pure virtual called
    return False;
}

Boolean
AeFieldUI::VerifyValueString (String string)
{
    assert (1);		// pure virtual called
    return True;
}

Boolean
AeFieldUI::VerifyInput (XmTextVerifyCallbackStruct * verify, const char * prevStr)
{
    return True;	// default to a valid string
}
 

void
AeFieldUI::SetWidth (int width)
{
    if (width < kMinWidth)
	width = kMinWidth;
    else if (width > kMaxWidth)
	width = kMaxWidth;

    fWidth = width;
    fNeedsWidthRecalc = False;
}


Boolean
AeFieldUI::HandleButtonPress (AeBinEditor * editor,
			      AeAssetUI * assetUI,
			      XEvent * event,
			      TEditModifier modifier)
{
    return False;	// default action is to do nothing
}

void
AeFieldUI::HandleDoubleClick (AeBinEditor * editor,
			      AeAssetUI * assetUI,
			      XEvent * event,
			      TEditModifier modifier)
{
}


//
// This routine helps keep field widths up to date.
// Whenever a new value is inserted into a field, call
// this routine with the string form of the new value
// to update fWidth.  If fWidth must be recalculated
// a flag is set to indicate that the field needs
// updating.
//
void
AeFieldUI::UpdateWidth (String newString, String oldString)
{
    int		width;

    fNeedsWidthUpdate = False;

    width = strlen(newString);

    if (width < kMinWidth)
	width = kMinWidth;
    else if (width > kMaxWidth)
	width = kMaxWidth;

    // if the width is greater than the current width, update the width
    if (width > fWidth)
    {
	fWidth = width;
	fNeedsWidthUpdate = True;
    }

    // else if the width is less than the current width and the string
    // being replaced is equal to the current width, then
    // we need to recalculate fWidth to find the new max.
    else if (width < fWidth)
    {
	width = strlen(oldString);
	
	if (width == fWidth)
	    fNeedsWidthRecalc = fNeedsWidthUpdate = True;
    }
}


void
AeFieldUI::CalcListFontRatio (void)
{
    XrmDatabase		appDB;
    char 		resFileName[256];
    char *		resType[20];
    XrmValue		resValue;
    XFontStruct *	labelStruct;
    XFontStruct *	cellStruct;

    XrmInitialize ();

    (void) strcpy (resFileName, "ae-defaults");

    if (appDB = XrmGetFileDatabase (resFileName))
    {
	if (XrmGetResource(appDB, "labelFont", "", resType, &resValue) == True)
	    labelStruct = XLoadQueryFont (theApplication->display (), (char *)resValue.addr);

	if (XrmGetResource(appDB, "cellFont", "", resType, &resValue) == True)
	    cellStruct = XLoadQueryFont (theApplication->display (), (char *)resValue.addr);

	if (labelStruct && cellStruct)	
	    AeFieldUI::fLabel2CellFontRatio = (double)labelStruct->max_bounds.width / (double)cellStruct->max_bounds.width;
    }
}