#include "mpp.h"
#include <graphics/rpattr.h>
#include <extras/ext_text.h>


LONG   PenListOffset;
extern UBYTE  PenNames[DRIPENS][32];
extern WORD   PenNameLength[DRIPENS];
extern UWORD  NumDriPens;
extern LONG ActivePen;

void FindMaxPenNameSize(struct TextAttr *TA)
{
  struct TextFont *font;
  LONG clen,dashlen;
  char c[2];
  
  c[1]=0;
  PenListOffset=0;

  if(font=OpenDiskFont(TA))
  {
    c[0]='-';

    dashlen=gui_StrFontLen(font,c);//                                                                  (4.64.6) (09/17/00)
  
    for(c[0]='0';c[0]<='9';c[0]++)
    {
      clen=gui_StrFontLen(font,c);//                                                                   (4.64.5) (09/17/00)
      PenListOffset=max(PenListOffset,clen);
    }
    
    PenListOffset=PenListOffset*5 + dashlen;
    CloseFont(font);
  }
}

ULONG __saveds __asm PensLVFunc(register __a0 struct Hook *Hook,
                                register __a1 struct LVDrawMsg *Dm,
                                register __a2 struct PenNode *PN)
{ 
  struct RastPort *rp;
  LONG top,bottom,left,right,baseline;
  LONG fgpen,bgpen;
  LONG pen,penlen,offset;
  ULONG state;
  
  if(Dm->lvdm_MethodID!=LV_DRAW)
    return(LVCB_UNKNOWN);

  left  =Dm->lvdm_Bounds.MinX;
  top   =Dm->lvdm_Bounds.MinY;
  right =Dm->lvdm_Bounds.MaxX;
  bottom=Dm->lvdm_Bounds.MaxY;

  rp=Dm->lvdm_RastPort;
  
  state=Dm->lvdm_State;

  if(LVR_SELECTED == state || LVR_SELECTEDDISABLED == state || ActivePen == PN->PenNumber)
  {
    fgpen=Dm->lvdm_DrawInfo->dri_Pens[FILLTEXTPEN];
    bgpen=Dm->lvdm_DrawInfo->dri_Pens[FILLPEN];
  }
  else
/*    if (  )
    {
      fgpen=Dm->lvdm_DrawInfo->dri_Pens[TEXTPEN];
      bgpen=Dm->lvdm_DrawInfo->dri_Pens[FILLPEN];
    }
    else*/
  {
    fgpen=Dm->lvdm_DrawInfo->dri_Pens[TEXTPEN];
    bgpen=Dm->lvdm_DrawInfo->dri_Pens[BACKGROUNDPEN];
  }

  SetAPen(rp,bgpen);

  RectFill(rp,left,top,right,bottom);

  baseline=rp->Font->tf_Baseline+top;

  SetRPAttrs(rp,
             RPTAG_APen   ,fgpen,
             RPTAG_DrMd   ,JAM1,
             TAG_DONE);

  pen=PN->PenNumber;
  penlen=PenNameLength[pen];
  
//  offset=right-2-PenListOffset-TextLength(rp,PenNames[pen],penlen);

  offset=right-2-PenListOffset;
  
  gui_RenderText(rp,PenNames[pen],
             RT_XPos          ,offset,
             RT_Baseline      ,baseline,
             RT_MaxWidth      ,offset-left,
             RT_Strlen        ,penlen,
             RT_Justification ,RTJ_RIGHT,
             TAG_DONE); 
  gui_RenderText(rp,&PenNames[pen][penlen],
             RT_XPos          ,offset,
             TAG_DONE);

/*
  Move(rp,offset,baseline);
  Text(rp,PenNames[pen],strlen(PenNames[pen]));
*/

  if(LVR_NORMALDISABLED == state || LVR_SELECTEDDISABLED == state )
    gui_GhostRect(rp,Dm->lvdm_DrawInfo->dri_Pens[TEXTPEN],left,top,right,bottom);//                        (4.64.7) (09/17/00)
  
  return(LVCB_OK);
}
