#define DEBUG_DELAY 50
//#define DEBUG
#include <debug.h>
#include <extras/macros/intuition.h>

#include "MPP.h"
#include "mppexterns.h"
#include "palettereq.h"
#include <ctype.h>
#include <utility/hooks.h>

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

long __stack = 10240;

extern LONG FillShinePen,FillShadowPen;
extern struct LG_Control *GadControl;

BOOL MyScreen;

LONG MsgsOut=0;

struct MPMessage *ForcedEditMsg;
UBYTE  ForcedSig;

extern UBYTE   Version[];
extern UBYTE   ModePro[];
extern LONG SemVersion,
            SemRevision;

ULONG  Arg_Promo, Arg_NLM, Arg_Ask, Arg_CH, Arg_CV, Arg_Debug, Arg_Import;


UBYTE   AboutText[]="ModePro\n"
                     "©1994-1998 by Michael Rivers\n"
                     "  DonationWare (see Docs)\n\n"
                     "%s\n"
                     "%s\n"
                     "  E-Mail:\n"
                     "    mrivers@gte.net\n"
                     "  WWW:\n"
                     "    http://home1.gte.net/mrivers/\n"
                     "  Mail:\n"
                     "    Michael Rivers\n"
                     "    4302 Wisconsin Court\n"
                     "    Tampa, FL, USA, 33616";


UBYTE EnvFile[]   ="ENV:ModePro.pref",
      EnvArcFile[]="ENVARC:ModePro.pref";

ULONG ActivateStrGad=0;
struct List PenList;
LONG ActivePen=0;
extern UBYTE PenNameGad[];
struct PenNode pennode[DRIPENS];
UWORD NumDriPens;
UWORD defaultpens[]=
{
  0,1,1,2,1,3,1,0,2,1,2,1
};
float XScale=1.0,
      YScale=1.0,
      PXScale,
      PYScale;
extern STRPTR MiscText[];
extern struct Hook StrHook,PenLVHook,StrCursorHook,PromotionLVHook;

extern struct NewGadget nglist[];
extern ULONG gtype[];
extern ULONG gtags[];
extern struct BevelBox BBox[];
extern UBYTE PenNames[DRIPENS][32];
extern UWORD PenNumbers[];
extern struct GUI_String GText[];
extern WORD PenNameLength[],
            BevelBoxes;
struct TextAttr *TAttr, User, SysFont;
struct Menu *MenuStrip;
extern struct   NewMenu nm[];      
struct TextAttr Topaz8 = { "topaz.font",8,0,0};

WORD LeftEdge,TopEdge; 

struct FontRequester *FontReq=0;
struct FileRequester *FileReq=0;
struct FileRequester *PalFileReq=0;
struct ScreenModeRequester *ScrReq=0;
struct FileRequester *BackdropFileReq=0;
struct PaletteRequest *PalettePReq=0,*BackdropPReq=0;

APTR VI=0;
struct Gadget   *glist=0;
struct Screen   *Scr=0;
struct Window   *Win=0;
struct DrawInfo *DrawInfo;

LONG BackdropPen;

BOOL    V39;

UBYTE   filename[513];

LONG   LVActive=0,NodeCnt=0;

ULONG  ArgEdit,ArgUse,ArgSave;
STRPTR ArgFrom,ArgPubscreen;
UBYTE  *UserFontName;
LONG   UserFontSize;

UBYTE *WindowName,
      *WinAdd,
      *Title1=" = <",
      *Title2=">";

struct Catalog *Catalog;

BOOL ClosePrefs=FALSE;
struct MPSem   *MPSem;
struct MsgPort *PrefsPort,*ReplyPort;

struct IntuitionBase *IntuitionBase; 
struct LocaleBase    *LocaleBase;
struct GfxBase       *GfxBase;
struct Library       *AslBase,
                     *CxBase,
                     *DiskfontBase,
                     *GadToolsBase,
                     *IconBase,
                     *IFFParseBase,
                     *UtilityBase;

struct Libs MP_Libs[]=
{
  (APTR *)&AslBase,      "asl.library",        36,0,
  (APTR *)&CxBase,       "commodities.library",36,0,
  (APTR *)&DiskfontBase, "diskfont.library",   36,0,
  (APTR *)&GadToolsBase, "gadtools.library",   36,0,
  (APTR *)&GfxBase,      "graphics.library",   36,0,
  (APTR *)&IconBase,     "icon.library",       36,0,
  (APTR *)&IFFParseBase, "iffparse.library",   36,0,
  (APTR *)&IntuitionBase,"intuition.library",  36,0,
  (APTR *)&UtilityBase,  "utility.library",    36,0,
  0,0,0
};

ULONG ListToCyc[]={1,0,2,3};
WORD  CurrentList_2=0,
      CurrentList=1;

struct NewMenu *FindNM(struct NewMenu *Array, APTR UserData);


void SetMPSem(void);

void PrintPStrings(void);

void main(int argc, char **argv)
{
  ULONG l;
  struct Screen *ps;
  LONG gotsem=0;



  if(LocaleBase=OpenLibrary("locale.library",38))
    Catalog=OpenCatalog(NULL,(STRPTR)"modeproprefs.catalog", TAG_DONE);
  else
    Catalog=NULL;
  
  InitStrings();
  //NewList(&ForcedEditList);
  ForcedEditMsg=0;

  if(ex_OpenLibs(argc,//                                                                               (4.63.1) (09/17/00)//  (4.64.1) (09/17/00)
              "ModeProPrefs",
              GetString(MSG_REQ_COULDNT_OPEN_LIBS),
              GetString(MSG_REQ_NAME_VER_FMT),
              GetString(MSG_REQ_OK),
              MP_Libs))
  {
    
//      PrintPStrings();
    
    if(ps=LockPubScreen(NULL))
    {
      if(DrawInfo=GetScreenDrawInfo(ps))
      {
        for(l=0;l<DrawInfo->dri_NumPens && l<NumDriPens;l++)
          defaultpens[l]=DrawInfo->dri_Pens[l];      
        FreeScreenDrawInfo(ps,DrawInfo);
      }
      UnlockPubScreen(NULL,ps);
    }

    if(GetArgs(argc,argv))
    {
//      printf("Got Args\n");
      
//        printf("Got Scr\n");
        Forbid();
        MPSem=(struct MPSem *)FindSemaphore((UBYTE *)"ModePro");
        if(MPSem)
          gotsem=AttemptSemaphoreShared(&MPSem->SharedSem);
        Permit();
        
        if(!gotsem)
        {
          EZReq(0,0,ModePro,GetString(MSG_ERR_NOPATCH),GetString(MSG_REQ_OK),0);
        }
        else
        {
//          printf("Got Sem\n");
//        SetMPSem();//                                                                                (4.62.9)
          if(AttemptSemaphore(&MPSem->PrefsSem))
          {
//            printf("Got P Args\n");
            if(MPSem->Version==SemVersion && MPSem->Revision==SemRevision)
            {
//              printf("Got Vers\n");
              SetMPSem();//                                                                            (4.62.10)
 
              if(ArgFrom)
              {
                //printf("ArgFrom = %s\n",ArgFrom);
                ObtainSemaphore(&MPSem->ListSem);
                ObtainSemaphore(&MPSem->NodeSem);
                FreeAllDefLists(MPSem->PromotionList);
                PrefLoadFile(ArgFrom);
                ReleaseSemaphore(&MPSem->NodeSem);
                ReleaseSemaphore(&MPSem->ListSem);
              }
              
              if(Arg_Import)
              {
                ObtainSemaphore(&MPSem->ListSem);
                ImportFile(Arg_Import);
                ReleaseSemaphore(&MPSem->ListSem);
              }
              
              if(ArgEdit)
              {
                if(ReplyPort=CreateMsgPort())
                {
                  //printf("Got RPort\n");
                  if(PrefsPort=CreateMsgPort())
                  {
                    if((ForcedSig=AllocSignal(-1))!=-1)
                    {
                      //printf("Got PPort\n");
                      ObtainSemaphore(&MPSem->PortSem);
                      MPSem->PrefsPort=PrefsPort;
                      MPSem->PrefsToDoPort.mp_SigBit  =ForcedSig;//                                    (4.62.3)
                      MPSem->PrefsToDoPort.mp_SigTask =FindTask(0);//                                  (4.62.3)
                      MPSem->PrefsToDoPort.mp_Flags   =PA_SIGNAL;//                                    (4.62.3)
                      
                      ForcedEditMsg=(struct MPMessage *)GetMsg(&MPSem->PrefsToDoPort);//               (4.62.4)
                      
                      ReleaseSemaphore(&MPSem->PortSem);
                      StrHook.h_Entry             =(HOOKFUNC)ExcludeHook;
                      PenLVHook.h_Entry           =(HOOKFUNC)PensLVFunc;
                      StrCursorHook.h_Entry       =(HOOKFUNC)PenValFunc;
                      PromotionLVHook.h_Entry     =(HOOKFUNC)PromotionLVFunc;
                      //MainWinHook.h_Entry=WinBackFillHook;
                      //PaletteWinHook.h_Entry=WinBackFillHook;
                 
                      NumDriPens=9;
                      V39=FALSE;
                  
                      if(((struct Library *)IntuitionBase)->lib_Version>38)
                      {
                        NumDriPens=12;
                        V39=TRUE;
                      }
                      
                      if(WinAdd=WindowName=AllocVec(strlen(MPSem->HotKey)+
                                                    strlen(GetString(MSG_WINDOW_TITLE))+
                                                    strlen(Title1)+
                                                    strlen(Title2)+1,MEMF_CLEAR|MEMF_PUBLIC))
                      {
                        strcpy(WindowName,GetString(MSG_WINDOW_TITLE));
                        strcat(WindowName,Title1);
                        strcat(WindowName,MPSem->HotKey);
                        strcat(WindowName,Title2);
                      }
                      else
                      {
                        WindowName=ModePro; 
                      }
                        
//                    printf("Got WinName\n");
                      SetupPenNodes();
//            printf("Got PenNodes\n");
        
                      ObtainSemaphoreShared(&MPSem->ListSem);
                      FixScreenModeNames();
                      
//                    printf("Got ModeNames\n");
                      if(GadgetInit())
                      {
                        if(ShowGUI())
                        {
                          struct Process *proc;
                          struct Window *winptr;
    
//                            printf("Got GUI\n");
                          proc=(struct Process *)FindTask(0);
                          winptr=proc->pr_WindowPtr;
                          proc->pr_WindowPtr=Win;
                 
                          ProcessInput();
                          
//                        DKP("main() 119\n");
                          proc->pr_WindowPtr=winptr;
//                        DKP("main() 120\n");
                          HideGUI();
                        }
                        GadgetCleanup();
                      }
//                    DKP("main() 200\n");
                      ReleaseSemaphore(&MPSem->ListSem);
//                    DKP("main() 201\n");                     
                      FreeVec(WinAdd);
//                    DKP("main() 202\n");             
                      ObtainSemaphore(&MPSem->PortSem);
//                    DKP("main() 203\n");
                      MPSem->PrefsPort=0;
                      
                      MPSem->PrefsToDoPort.mp_SigBit  =255;//                                          (4.62.5)
                      MPSem->PrefsToDoPort.mp_SigTask =0;//                                            (4.62.5)
                      MPSem->PrefsToDoPort.mp_Flags   =PA_IGNORE;//                                    (4.62.5)
                      {
                        struct Message *msg;
                        while(msg=GetMsg(PrefsPort))
                          ReplyMsg(msg);
                        
                        if(ForcedEditMsg)
                          ReplyMsg((struct Message *)ForcedEditMsg);
                      
                        while(msg=GetMsg(&MPSem->PrefsToDoPort))//                                     (4.62.6)
                          ReplyMsg(msg);
                      
                      }
//                    DKP("main() 204\n");
                      ReleaseSemaphore(&MPSem->PortSem);
                      FreeSignal(ForcedSig);
                    }
//                  DKP("main() 205\n");
                    DeleteMsgPort(PrefsPort);
//                  DKP("main() 206\n");                    
                  }// endif CreateMsgPort(PrefsPort)
                  DeleteMsgPort(ReplyPort);
//                  DKP("main() 207\n");
                }// if CreateMsgPort(ReplyPort)
              }// endif(ArgEdit)
              else
                if(ArgSave)
                {
                  ObtainSemaphore(&MPSem->ListSem);
                  ObtainSemaphore(&MPSem->NodeSem);
                  PrefSaveFile(EnvArcFile);
                  PrefSaveFile(EnvFile);
                  ReleaseSemaphore(&MPSem->NodeSem);            
                  ReleaseSemaphore(&MPSem->ListSem);
                }
            }
            else
              EZReq(0,0,ModePro,GetString(MSG_ERR_WRONGVERSIONS),MiscText[OK],
                                         MPSem->Version,MPSem->Revision,SemVersion,SemRevision);          
            ReleaseSemaphore(&MPSem->PrefsSem);
          }//if(attemptsemshared()        
          ReleaseSemaphore(&MPSem->SharedSem);//                                                        (4.61.3)
        }//endelse (gotsem)
      CleanupArgs();
    }
    ex_CloseLibs(MP_Libs);//                                                                           (4.64.2) (09/17/00)
  } 
  if(LocaleBase)
  {
    CloseCatalog(Catalog);
    CloseLibrary(LocaleBase);
  }
}

void SetMPSem(void)
{
  if(Arg_Promo!=5)
  {
    MPSem->Enabled=Arg_Promo;
  }
    
  if(Arg_NLM!=5)
  {
    MPSem->EnabledNLM=Arg_NLM;
  }
    
  if(Arg_Ask!=5)
  {
    MPSem->CatchScreens=Arg_Ask;
  }
    
  if(Arg_CH!=5)
  {
    MPSem->CenterAll=(MPSem->CenterAll & ~1) | ( Arg_CH ? 1 : 0);
  }
  
  if(Arg_CV!=5)
  {
    MPSem->CenterAll=(MPSem->CenterAll & ~2) | ( Arg_CV ? 2 : 0);
  }
   
  if(Arg_Debug!=5)
  {
    MPSem->Debug=Arg_Debug;
  }
}



struct TextFont *GUIFont=0;

BOOL RemakeGads(void)
{
  FreeMPPGadgets();
  if(MakeMPPGadgets(Win,VI,TAttr))
  {
    WORD x1,x2,y1,y2;
              
    x1=Win->BorderLeft;
    x2=Win->Width-Win->BorderRight-1;
    y1=Win->BorderTop;
    y2=Win->Height-Win->BorderBottom-1;
    if(x1<=x2 && y1<=y2)
      EraseRect(Win->RPort,x1,y1,x2,y2);
    
    LG_AddGadgets(Win,GadControl);
    UpdateGadgets();
    RefreshGUI();
    RefreshWindowFrame(Win);
    return(1);
  }
  return(0);
}

extern WORD WindowWidth,WindowHeight;  

BOOL ShowGUI(void)
{
  WORD zoom[4];

  if(Scr=GetScreen())
  {
    if(VI=GetVisualInfo(Scr,TAG_END))
    {
      zoom[2]=160;
      zoom[3]=Scr->WBorTop + Scr->RastPort.TxHeight + 1;
      if(V39)
      {
        zoom[0]=zoom[1]=~0;
      }
      else
      {
        zoom[0]=0;
        zoom[1]=zoom[3];
      }
      
      if(MPSem->FirstPrefsOpen)
      {
        MPSem->TopEdge  =Scr->BarHeight+1;
        MPSem->LeftEdge =0;
        MPSem->FirstPrefsOpen=0;
        MPSem->WinWidth=(WORD)(WindowWidth  * XScale);
        MPSem->WinHeight=(WORD)(WindowHeight  * YScale);
      }
      else
      {
        LVActive    =MPSem->LVActive;
        CurrentList =MPSem->CurrentList;

        // 4.53
        MPSem->WinWidth   =max(MPSem->WinWidth ,(WORD)(WindowWidth  * XScale));
        MPSem->WinHeight  =max(MPSem->WinHeight,(WORD)(WindowHeight * YScale));

//        MPSem->WinWidth   =max(MPSem->WinWidth ,(WORD)(WindowWidth));
//        MPSem->WinHeight  =max(MPSem->WinHeight,(WORD)(WindowHeight));

      }
       if(Win=OpenWindowTags(NULL, 
                          WA_MaxWidth         ,~0,
                          WA_MaxHeight        ,~0,
                          
                          TAG_SKIP            ,(MyScreen?13:0),
                          //WA_MinHeight        ,(LONG)(WindowHeight * YScale + Scr->WBorTop  + Scr->RastPort.TxHeight + 1 + Scr->WBorBottom) ,
                          WA_MinHeight        ,(LONG)(WindowHeight + Scr->WBorTop  + Scr->RastPort.TxHeight + 1 + Scr->WBorBottom) ,
                          WA_InnerWidth       ,MPSem->WinWidth,
                          WA_InnerHeight      ,MPSem->WinHeight,
                          WA_Left             ,MPSem->LeftEdge,
                          WA_Top              ,MPSem->TopEdge,
                          WA_DragBar          ,TRUE,
                          WA_DepthGadget      ,TRUE,
                          WA_Zoom             ,zoom,
                          WA_Title            ,WindowName ,
                          WA_SizeGadget       ,1,
                          WA_SizeBBottom      ,1,
                          TAG_SKIP            ,6,
                          
                          WA_InnerWidth       ,(LONG)(WindowWidth  * XScale),
                          WA_InnerHeight      ,(LONG)(WindowHeight  * YScale),
                          WA_Backdrop         ,MyScreen,
                          WA_Borderless       ,TRUE,
                          WA_Left             ,0,
                          WA_Top              ,Scr->BarHeight+1,
                          
                          WA_NewLookMenus     ,TRUE,
                          WA_AutoAdjust       ,TRUE,
                          WA_Activate         ,TRUE,
                          WA_SimpleRefresh    ,TRUE,
                          WA_IDCMP            ,MPP_IDCMP | IDCMP_NEWSIZE,
                          WA_PubScreen        ,Scr,//                                                  (4.56.1)
                          TAG_DONE  ))//    TAG_DONE was missing                                       (4.56.1)
      {
// 4.53
        Win->MinWidth =(WORD)(WindowWidth  * XScale + Win->BorderLeft + Win->BorderRight);
        Win->MinHeight=(WORD)(WindowHeight * YScale + Win->BorderTop  + Win->BorderBottom);
//        Win->MinWidth =(WORD)(WindowWidth  + Win->BorderLeft + Win->BorderRight);
//        Win->MinHeight=(WORD)(WindowHeight + Win->BorderTop  + Win->BorderBottom);
        SetFont(Win->RPort,GUIFont);
        FindMaxPenNameSize(TAttr);
        if(MakeMPPGadgets(Win,VI,TAttr))
        {
          LG_AddGadgets(Win,GadControl);
          GT_RefreshWindow(Win,0);
          //DrawBevelBoxes(Win,VI,BBox,BevelBoxes,XScale,YScale);
          RefreshGUI();
           {
            struct NewMenu *menu;
            
            if(menu=FindNM(nm,(APTR)M_ENABLENL))
            {
              if(!V39)
                menu->nm_Flags =((menu->nm_Flags | NM_ITEMDISABLED) & ~CHECKED);
            }
          }
           if((MenuStrip=CreateMenus(nm,TAG_END))!=NULL)
          {
            if((LayoutMenus(MenuStrip,VI,
                            GTMN_NewLookMenus,TRUE,
                            TAG_END))!=NULL)
            {          
              if((SetMenuStrip(Win,MenuStrip))!=NULL)
              {
                //LVActive=0;
                 GT_RefreshWindow(Win,NULL);
                RefreshMenus();
                UpdateGadgets();
                ScreenToFront(Scr);
                return(TRUE);
              }
            }
          }
        }
      }
    }
  }
  HideGUI();
  return(FALSE);
}


void HideGUI()
{
  DKP("HideGUI()\n");
  CleanUp();
  if(Win)           
  { 
    MPSem->CurrentList=CurrentList;
    MPSem->LVActive   =LVActive;
  DKP("HideGUI() 1\n");
    ClearMenuStrip(Win);
  DKP("HideGUI() 2\n");
    MPSem->LeftEdge=Win->LeftEdge;
    MPSem->TopEdge=Win->TopEdge;
    MPSem->WinWidth=GetWinInnerWidth(Win);
    MPSem->WinHeight=GetWinInnerHeight(Win);
  DKP("HideGUI() 3\n");
    FreeMPPGadgets();
  DKP("HideGUI() 5\n");
    CloseWindow(Win);    
  DKP("HideGUI() 6\n");
    
    Win=0;
  }
    DKP("HideGUI() 7\n");       
  FreeMenus(MenuStrip);       MenuStrip=0;
    DKP("HideGUI() 8\n");
  FreeVisualInfo(VI);         VI=0;
    DKP("HideGUI() 9\n");
  
  DKP("HideGUI() 10\n");
  if(GUIFont) CloseFont(GUIFont);
    DKP("HideGUI() 11\n");
  
  
  
  ReleaseScreen(Scr);
  /* These functions I KNOW are safe to call with a NULL */

}

struct TextAttr *WhichFont(struct Screen *Scr, struct TextAttr *TA, ... );

struct TextAttr *WhichFont(struct Screen *Scr, struct TextAttr *TA, ... )
{
  struct TextAttr **ta;
  
  ta=&TA;
  
  while(*ta)
  {
    if(GUIFont=OpenDiskFont(*ta))
    {
      // 4.53
      GetGUIScale(*ta,GText,&PXScale,&PYScale);
      XScale=PXScale;
      YScale=PYScale;
      if(CheckInnerWindowSize(Scr,WindowWidth,WindowHeight,XScale,YScale))
      {
        return(*ta);
      }
      CloseFont(GUIFont);
      GUIFont=0;
    }
    ta++;
  }
  return(0);
}

struct Screen *GetScreen(void)
{
  struct Screen *gs;
  WORD pens[]={~0};
  ULONG dispid;
  struct TextAttr *ta;
  BOOL cps,cs;
  
  MyScreen=FALSE;
  
  cps=MPSem->CatchPubScreens;
  cs =MPSem->CatchScreens;
  
  MPSem->CatchPubScreens=0;
  MPSem->CatchScreens=0;
  
  if(gs=LockPubScreen(ArgPubscreen))
  {
    SysFont=*gs->Font;
    
    if(UserFontName)
    {
      User.ta_Name  =UserFontName;
      User.ta_YSize =UserFontSize;
      User.ta_Style =0;
      User.ta_Flags =0;
      ta=&User;
    }
    else
      ta=&SysFont;

    /* Pick which font works with the PubScreen gs */
    if(!(TAttr=WhichFont(gs,ta,&SysFont,&Topaz8,0)))
    { /* If no fonts fits the screen */
//      TAttr=ta;
      TAttr=&Topaz8;
      dispid=GetVPModeID(&(gs->ViewPort));//                                                         (4.63.1)//  (4.63.3)
      UnlockPubScreen(0,gs);//                                                                       (4.63.1)//  (4.63.2)
      gs=0;      //                                                                                  (4.63.1)//  (4.63.2)
      
      if(GUIFont=OpenDiskFont(TAttr))
      {
        GetGUIScale(TAttr,GText,&XScale,&YScale);//                                                    (4.63.1)
        printf("%f %f\n",XScale,YScale);
        MyScreen=TRUE;
        gs=OpenScreenTags(0, SA_LikeWorkbench, TRUE,
                             SA_Title        , ModePro,
                             SA_DisplayID    , dispid,
                             SA_Overscan     , OSCAN_TEXT,
                             SA_AutoScroll   , TRUE,
                             SA_Font         , TAttr,
                             SA_Width        , (LONG)(WindowWidth  * XScale),
                             SA_Height       , (LONG)(WindowHeight * YScale + TAttr->ta_YSize + 3),
                             SA_Depth        , (V39?3:2), // Open a 8 color screen if V39 (for backdrop requester)
                             SA_SharePens    ,1,          // Sharepens for backdrop requester
                             SA_Pens         , pens,
                             TAG_DONE);
      }
    }
  }
  if(gs)
  {
    if(DrawInfo=GetScreenDrawInfo(gs))
    {
      if(V39)
      {
        struct ColorMap *cmap;
        struct RGB fill,pen;
//        LONG diff;

        cmap=gs->ViewPort.ColorMap;

#define FSHINE_PERCENT (float)6/10
#define FSHADOW_PERCENT (float)4/10

        GetRGB32(cmap,DrawInfo->dri_Pens[FILLPEN],      1,(ULONG *)&fill);        
        
//        GetRGB32(cmap,DrawInfo->dri_Pens[SHINEPEN],     1,(ULONG *)&pen);
        pen.Red=pen.Green=pen.Blue=0xffffffff;
        FillShinePen=ObtainBestPen(cmap,
                    (ULONG)(fill.Red   - ((float)fill.Red   - pen.Red)      *FSHINE_PERCENT),
                    (ULONG)(fill.Green - ((float)fill.Green - pen.Green)    *FSHINE_PERCENT),
                    (ULONG)(fill.Blue  - ((float)fill.Blue  - pen.Blue)     *FSHINE_PERCENT),
                    0);

//        GetRGB32(cmap,DrawInfo->dri_Pens[SHADOWPEN],     1,(ULONG *)&pen);
        pen.Red=pen.Green=pen.Blue=0;
        FillShadowPen=ObtainBestPen(cmap,
                    (ULONG)(fill.Red   - ((float)fill.Red   - pen.Red)      *FSHADOW_PERCENT),
                    (ULONG)(fill.Green - ((float)fill.Green - pen.Green)    *FSHADOW_PERCENT),
                    (ULONG)(fill.Blue  - ((float)fill.Blue  - pen.Blue)     *FSHADOW_PERCENT),
                    0);

        BackdropPen=ObtainPen(gs->ViewPort.ColorMap,-1,0,0,0,PEN_EXCLUSIVE);

      
      }
    //  MainWinHook.h_Data=DrawInfo->dri_Pens[BACKGROUNDPEN];
    }
  }
  else
  {
    if(GUIFont)
      CloseFont(GUIFont);
  }
  
  MPSem->CatchPubScreens=cps;
  MPSem->CatchScreens=cs;
  
  return(gs);
}

void ReleaseScreen(struct Screen *S)
{
  if(S)
  {
    if(V39)
    {
      ReleasePen(S->ViewPort.ColorMap,BackdropPen);
      ReleasePen(S->ViewPort.ColorMap,FillShinePen);
      ReleasePen(S->ViewPort.ColorMap,FillShadowPen);
    }
    
    FreeScreenDrawInfo(S,DrawInfo);
    if(MyScreen)
      CloseScreen(S);
    else
      UnlockPubScreen(0,S);
  }
  if(GUIFont)
    CloseFont(GUIFont);
}

void CleanUp(void)
{
  FreeAslRequest(ScrReq);
  FreeAslRequest(FileReq);
  FreeAslRequest(PalFileReq);
  FreeAslRequest(FontReq);
  FreeAslRequest(BackdropFileReq);
  PR_FreePaletteRequest(BackdropPReq);
  PR_FreePaletteRequest(PalettePReq);
}




