home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 468.lha / ToolManager_v1.2 / toollist.c < prev    next >
C/C++ Source or Header  |  1991-02-08  |  4KB  |  120 lines

  1. #include "ToolManager.h"
  2.  
  3. /* Add one program to tool list */
  4. BOOL AddToolNode(BPTR dir, char *name, char *realname)
  5. {
  6.  static ULONG ToolID=0; /* Counter for Tool ID */
  7.  struct ToolNode *tn;
  8.  
  9.  if (ToolCount>=99) goto e1; /* Not more than 99 Tools :-) possible */
  10.  
  11.  /* Get memory for ToolNode */
  12.  if (!(tn=malloc(sizeof(struct ToolNode))))
  13.   goto e1;
  14.  
  15.  /* Get memory for menu item name and copy it */
  16.  if (!(tn->tn_Node.ln_Name=strdup(name)))
  17.   goto e2;
  18.  
  19.  /* Does the program have a real name? */
  20.  if (realname)
  21.   {
  22.    /* Get memory for real program name and copy it */
  23.    if (!(tn->tn_RealName=strdup(realname)))
  24.     goto e3;
  25.   }
  26.  else
  27.   tn->tn_RealName=NULL; /* Otherwise: menu item name == real program name */
  28.  
  29.  /* Add a new menu entry */
  30.  if (!(tn->tn_MenuItem=AddAppMenuItem(ToolID+1,NULL,tn->tn_Node.ln_Name,
  31.                                       MyMP,NULL)))
  32.   goto e4;
  33.  
  34.  if (!(tn->tn_DirLock=DupLock(dir))) /* Duplicate directory lock */
  35.   goto e5;
  36.  
  37.  /* Add node to tool list */
  38.  tn->tn_ID=++ToolID;    /* Set Tool ID */
  39.  ToolCount++;           /* Increment active tool count */
  40.  DetachToolList();      /* Detach list from ListView gadget */
  41.  AddTail(&ToolList,tn);
  42.  AttachToolList();      /* Attach list to ListView gadget */
  43.  
  44.  /* Node successfully added to list! */
  45.  return(TRUE);
  46.  
  47.  /* Something went wrong.... */
  48. e5: RemoveAppMenuItem(tn->tn_MenuItem);
  49. e4: if (tn->tn_RealName) free(tn->tn_RealName);
  50. e3: free(tn->tn_Node.ln_Name);
  51. e2: free(tn);
  52. e1: return(FALSE);
  53. }
  54.  
  55. /* Scan Workbench parameters and add them to tool list */
  56. BOOL WBAddToolNode(struct WBArg *wbarg, int numargs)
  57. {
  58.  int i;
  59.  BOOL rc=TRUE;
  60.  BPTR fl;
  61.  struct DiskObject *dobj;
  62.  
  63.  for (i=0; i<numargs; i++, wbarg++) /* Scan all parameters */
  64.   if ((wbarg->wa_Lock) && (strlen(wbarg->wa_Name)!=0)) /* Sanity check */
  65.    {
  66.     fl=CurrentDir(wbarg->wa_Lock);          /* Change to icon's directory */
  67.     if (dobj=GetDiskObject(wbarg->wa_Name)) /* Get icon data */
  68.      {
  69.       switch(dobj->do_Type) /* Perform action depending on icon type */
  70.        {
  71.         case WBTOOL:    /* Icon is a Tool. Menu entry == real program name */
  72.          rc&=AddToolNode((struct FileLock *) wbarg->wa_Lock,wbarg->wa_Name,
  73.                          NULL);
  74.          break;
  75.         case WBPROJECT: /* Icon is a Project. Menu entry = icon name,
  76.                            real program name = default tool name */
  77.          rc&=AddToolNode((struct FileLock *) wbarg->wa_Lock,wbarg->wa_Name,
  78.                          dobj->do_DefaultTool);
  79.          break;
  80.         default:        /* Every other icon type is erroneous */
  81.          rc&=FALSE;
  82.          break;
  83.        }
  84.       FreeDiskObject(dobj); /* Release icon data */
  85.      }
  86.     else
  87.      rc&=FALSE; /* Error: Can't get icon data */
  88.  
  89.     CurrentDir(fl); /* Change to old directory */
  90.    }
  91.   else
  92.    rc&=FALSE; /* Error: Bad Workbench parameter */
  93.  
  94.  return(rc); /* Return TRUE if no error */
  95. }
  96.  
  97. /* Remove ONE node from the tool list */
  98. void RemToolNode(struct ToolNode *tn)
  99. {
  100.  Remove(tn);                                 /* Remove node from list */
  101.  UnLock(tn->tn_DirLock);                     /* Free directory */
  102.  RemoveAppMenuItem(tn->tn_MenuItem);         /* Remove menu entry */
  103.  if (tn->tn_RealName) free(tn->tn_RealName); /* Free memory */
  104.  free(tn->tn_Node.ln_Name);
  105.  free(tn);
  106.  ToolCount--;                                /* decrement active tool count */
  107. }
  108.  
  109. /* Remove ALL nodes from the tool list */
  110. void RemoveTools(void)
  111. {
  112.  struct ToolNode *tn1,*tn2=GetHead(&ToolList); /* Begin of list */
  113.  
  114.  while (tn1=tn2)
  115.   {
  116.    tn2=GetSucc(tn1); /* Next in list */
  117.    RemToolNode(tn1); /* Remove node */
  118.   }
  119. }
  120.