#include "mpp.h"

void CreateMonitorList(struct List *List);
struct ModeNode *CreateMonitorNode(ULONG ID);

ULONG SelectMonitor(struct DefaultNode *DN)
{
  struct List monlist;
  struct ModeNode *mn;
  LONG   item;
  ULONG rv=INVALID_ID;
  UBYTE listtitle[100];
  
  sprintf(listtitle,GetString(MSG_REQ_SELECT_MONITOR_FOR),DN->Def_Node.ln_Name);

  NewList(&monlist);  
  CreateMonitorList(&monlist);

  item=SelectReq(&monlist,listtitle);
  
  if(item>=0)
  {
    mn=(struct ModeNode *)monlist.lh_Head;
    while(item)
    {
      mn=(struct ModeNode *)mn->mn_Node.ln_Succ;
      item--;
    }
    rv=mn->mn_DisplayID;
    DN->ModeID=rv;
  }
  FreeNameNodes(&monlist);

  return(rv);
}

void CreateMonitorList(struct List *List)
{
  struct ModeNode    *mn;
  ULONG previd=(INVALID_ID & MONITOR_ID_MASK),
        id,mid;
  
  id=NextDisplayInfo(INVALID_ID);

  while(id!=INVALID_ID)
  {
    mid=id & MONITOR_ID_MASK;
    if(previd!=mid && mid)
    { // if the monitor id is different and not 0 then make a node .
      previd=mid;
      if(mn=CreateMonitorNode(id))
        EnqueueName(List,(struct Node *)mn);      
    } 
    id=NextDisplayInfo(id);
  }
  
  if(mn=CreateMonitorNode(INVALID_ID))//                                                               (4.57.4)
  { //                                                                                                 (4.57.4)
    AddHead(List,(struct Node *)mn);      //                                                           (4.57.4)
  }  //                                                                                                (4.57.4)
}

struct ModeNode *CreateMonitorNode(ULONG ID)//                                                         (4.57.8)
{
  struct ModeNode *mn;
  struct MonitorInfo mi;

  if(mn=AllocVec(sizeof(struct ModeNode),MEMF_PUBLIC|MEMF_CLEAR))
  {
    mn->mn_DisplayID=ID;
    if(mn->mn_Node.ln_Name=AllocVec(64,MEMF_PUBLIC))
    {
      if(GetMonitorName(ID,mn->mn_Node.ln_Name,64))
      {
        return(mn);
      }
      FreeVec(mn->mn_Node.ln_Name);
    }
    FreeVec(mn);   
  }       
  return(0);
}

/*
struct ModeNode *CreateMonitorNode(ULONG ID)
{
  struct ModeNode *mn;
  struct MonitorInfo mi;

  if(mn=AllocVec(sizeof(struct ModeNode),MEMF_PUBLIC|MEMF_CLEAR))
  {
    mn->mn_DisplayID=ID;
    if(ID & MONITOR_ID_MASK == MONITOR_ID_MASK)//                                                      (4.57.5)
    {//                                                                                                (4.57.5)
      if(mn->mn_Node.ln_Name=CopyString(GetString(MSG_ANY_MONITOR),MEMF_PUBLIC))//                     (4.57.5)
      {//                                                                                              (4.57.5)
        return(mn);//                                                                                  (4.57.5)
      }//                                                                                              (4.57.5)
    }//                                                                                                (4.57.5)
    else
    {
      if(GetDisplayInfoData(NULL,(UBYTE *)&mi,sizeof(struct MonitorInfo),DTAG_MNTR,ID)>0)
      {
        if(mi.Mspc)
          if(mn->mn_Node.ln_Name=CopyString(mi.Mspc->ms_Node.xln_Name,MEMF_PUBLIC))
            return(mn);
      }
    }
    FreeVec(mn);   
  }       
  return(0);
}

*/