#include "mppexterns.h"
#include <proto/gadtools.h>
#include <proto/graphics.h>
#include <clib/extras_protos.h>
#include <extras/layoutgt.h>
#include <extras/ext_text.h>

#include <clib/extras/palettereq_protos.h>
#include <extras/palettereq.h>

extern  struct PaletteRequest *PalettePReq;

void PaletteReq(struct DefaultNode *DN)
{
  struct DimensionInfo dim;
  struct Screen *pscr;
  STRPTR pwintitle,pwinfmt;
  LONG pwinfmtlen;
  WORD dripens[DRIPENS+1];
  WORD swidth=320,sheight=200;
  LONG l;
  struct Requester *req;

  pwinfmt=GetString(MSG_REQ_EDIT_PALETTE);
  pwinfmtlen=strlen(pwinfmt);
  if(pwintitle=AllocVec(pwinfmtlen+strlen(DN->Def_Node.ln_Name)+1,MEMF_PUBLIC|MEMF_CLEAR))
    sprintf(pwintitle,pwinfmt,DN->Def_Node.ln_Name);
  
  if(DN->Look3D)
  {
    for(l=0;l<DRIPENS;l++)
      dripens[l]=DN->Pens[l];
    dripens[DRIPENS]=~0;
  }
  else
    dripens[0]=~0;

  req=Busy(Win);  

  if(GetDisplayInfoData(0,(UBYTE *)&dim,sizeof(dim),DTAG_DIMS,DN->ModeID))
  {
    swidth  =max(swidth ,dim.Nominal.MaxX);
    sheight =max(sheight,dim.Nominal.MaxY);
  }
  
  if(pscr=OpenScreenTags(0,SA_Depth     ,DN->Depth,
                           SA_DisplayID ,DN->ModeID,
                           SA_Pens      ,dripens,
                           SA_Overscan  ,OSCAN_TEXT,
                           SA_Width     ,swidth,
                           SA_Height    ,sheight,
                           SA_Title     ,pwintitle,
                           SA_AutoScroll,TRUE,
                           SA_ModeProNode,DN,
                           TAG_DONE))
  {
    LONG colors;
    
    colors=1<<(pscr->RastPort.BitMap->Depth);
    
    if(!PalettePReq)
    {
      PalettePReq=PR_AllocPaletteRequest(
                  PR_TextAttr,        TAttr,
                  TAG_DONE);
    }
    
    if(PalettePReq)
    {
      struct RGB *newpalette;
      
      if(newpalette=AllocVec(sizeof(struct RGB)*colors,MEMF_CLEAR|MEMF_PUBLIC))
      {
        if(DN->Palette)
          CopyMem(DN->Palette,newpalette,sizeof(struct RGB)*DN->Colors);
          
        if(PR_DoPaletteRequest(PalettePReq, 
                          PR_Screen,            pscr,
                          PR_Colors,            colors,
                          PR_Title,             pwintitle,
                          PR_InitialLeftEdge,   0,
                          PR_InitialTopEdge,    pscr->BarHeight+1,
                          PR_InitialWidth,      pscr->Width,
                          PR_InitialHeight,     pscr->Height-pscr->BarHeight-1,
                          PR_InitialPalette,    (DN->Palette?newpalette:0),
                          TAG_DONE))
        {
          APTR dummy;
          
          dummy=DN->Palette;
          DN->Palette=newpalette;
          newpalette=dummy;

          DN->Colors=colors;
          CopyMem(PalettePReq->pr_Palette,DN->Palette,sizeof(struct RGB)*colors);          
        }
        FreeVec(newpalette);
      }
    }
    CloseScreen(pscr);
  }
  FreeVec(pwintitle);
  
  NotBusy(Win,req);
}

