home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / amiga / utility / misc / toolmana.lha / ToolManager / Source / library / iconobj.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-26  |  11.2 KB  |  320 lines

  1. /*
  2.  * iconobj.c  V2.0
  3.  *
  4.  * TMObject, Type: Icon
  5.  *
  6.  * (c) 1990-1992 Stefan Becker
  7.  */
  8.  
  9. #include "ToolManagerLib.h"
  10.  
  11. /* extended TMObject structure for TMOBJTYPE_ICON objects */
  12. struct TMObjectIcon {
  13.                      struct TMObject     io_Object;
  14.                      ULONG               io_Flags;
  15.                      ULONG               io_LeftEdge;
  16.                      ULONG               io_TopEdge;
  17.                      struct TMLink      *io_ExecLink;
  18.                      struct TMLinkImage *io_ImageLink;
  19.                      struct TMLink      *io_SoundLink;
  20.                      struct TMLink       io_Link;
  21.                      struct DiskObject  *io_DiskObj;
  22.                      void               *io_AppIcon;
  23.                     };
  24.  
  25. /* io_Flags */
  26. #define IO_ShowName (1L<<0)
  27.  
  28. /* Tag to Flag mapping table for PackBoolTags */
  29. static struct TagItem flagmap[]={TMOP_ShowName, IO_ShowName,
  30.                                  TAG_DONE};
  31.  
  32. /* Create an AppIcon */
  33. static void CreateAppIcon(struct TMObjectIcon *tmobj, char *name)
  34. {
  35.  /* Add AppIcon to Workbench if image is supplied */
  36.  if (tmobj->io_ImageLink && tmobj->io_ImageLink->tmli_Normal) {
  37.   struct DiskObject *dobj;
  38.  
  39.   if (dobj=AllocMem(sizeof(struct DiskObject),MEMF_PUBLIC|MEMF_CLEAR)) {
  40.    /* Init DiskObject structure */
  41.    dobj->do_Version=WB_DISKVERSION;
  42.    dobj->do_Gadget.Width=tmobj->io_ImageLink->tmli_Width;
  43.    dobj->do_Gadget.Height=tmobj->io_ImageLink->tmli_Height;
  44.    dobj->do_Gadget.GadgetRender=tmobj->io_ImageLink->tmli_Normal;
  45.    if (dobj->do_Gadget.SelectRender=tmobj->io_ImageLink->tmli_Selected)
  46.     dobj->do_Gadget.Flags=GFLG_GADGHIMAGE|GFLG_GADGIMAGE;
  47.    else
  48.     dobj->do_Gadget.Flags=GFLG_GADGHCOMP|GFLG_GADGIMAGE;
  49.    dobj->do_Gadget.Activation=GACT_RELVERIFY|GACT_IMMEDIATE;
  50.    dobj->do_Gadget.GadgetType=GTYP_BOOLGADGET;
  51.    dobj->do_Gadget.UserData=(APTR) WB_DISKREVISION;
  52.    dobj->do_CurrentX=tmobj->io_LeftEdge;
  53.    dobj->do_CurrentY=tmobj->io_TopEdge;
  54.  
  55.    DEBUG_PRINTF("Normal 0x%08lx",dobj->do_Gadget.GadgetRender);
  56.    DEBUG_PRINTF(" Selected 0x%08lx",dobj->do_Gadget.SelectRender);
  57.    DEBUG_PRINTF(" Width %4ld",dobj->do_Gadget.Width);
  58.    DEBUG_PRINTF(" Height %4ld",dobj->do_Gadget.Height);
  59.    DEBUG_PRINTF(" X %4ld",dobj->do_CurrentX);
  60.    DEBUG_PRINTF(" Y %4ld\n",dobj->do_CurrentY);
  61.  
  62.    if (tmobj->io_AppIcon=AddAppIconA((ULONG) &tmobj->io_Link,NULL,
  63.                                      (tmobj->io_Flags & IO_ShowName) ?
  64.                                       name : DefaultNoName,
  65.                                      AppMsgPort,NULL,dobj,NULL))
  66.     tmobj->io_DiskObj=dobj;
  67.    else
  68.     FreeMem(dobj,sizeof(struct DiskObject));
  69.   }
  70.  }
  71. }
  72.  
  73. /* Create an Icon object */
  74. struct TMObject *CreateTMObjectIcon(struct TMHandle *handle, char *name,
  75.                                     struct TagItem *tags)
  76. {
  77.  /* Open Workbench */
  78.  if (GetWorkbench()) {
  79.   struct TMObjectIcon *tmobj;
  80.  
  81.   /* allocate memory for object */
  82.   if (tmobj=AllocateTMObject(sizeof(struct TMObjectIcon))) {
  83.    struct TagItem *ti,*tstate;
  84.  
  85.    /* Scan tag list */
  86.    tstate=tags;
  87.    while (ti=NextTagItem(&tstate)) {
  88.  
  89.     DEBUG_PRINTF("Got Tag (0x%08lx)\n",ti->ti_Tag);
  90.  
  91.     switch (ti->ti_Tag) {
  92.      case TMOP_Exec:    if (ti->ti_Data) {
  93.                          if (tmobj->io_ExecLink) /* Already got a link? */
  94.                           /* Yes, remove it! */
  95.                           RemLinkTMObject(tmobj->io_ExecLink);
  96.  
  97.                          /* Create new link to exec object */
  98.                          tmobj->io_ExecLink=AddLinkTMObject(handle,
  99.                                                             (char *)
  100.                                                              ti->ti_Data,
  101.                                                             TMOBJTYPE_EXEC,
  102.                                                             (struct TMObject *)
  103.                                                              tmobj);
  104.                         }
  105.                         break;
  106.      case TMOP_Image:   if (ti->ti_Data) {
  107.                          if (tmobj->io_ImageLink) /* Already got a link? */
  108.                           /* Yes, remove it! */
  109.                           RemLinkTMObject((struct TMLink *)
  110.                                           tmobj->io_ImageLink);
  111.  
  112.                          /* Create new link to exec object */
  113.                          tmobj->io_ImageLink=AddLinkTMObject(handle,
  114.                                                              (char *)
  115.                                                               ti->ti_Data,
  116.                                                              TMOBJTYPE_IMAGE,
  117.                                                              (struct TMObject *)
  118.                                                               tmobj);
  119.                         }
  120.                         break;
  121.      case TMOP_LeftEdge:tmobj->io_LeftEdge=ti->ti_Data;
  122.                         break;
  123.      case TMOP_Sound:   if (ti->ti_Data) {
  124.                          if (tmobj->io_SoundLink) /* Already got a link? */
  125.                           RemLinkTMObject(tmobj->io_SoundLink); /* Remove it! */
  126.  
  127.                          /* Create new link to exec object */
  128.                          tmobj->io_SoundLink=AddLinkTMObject(handle,
  129.                                                              (char *)
  130.                                                               ti->ti_Data,
  131.                                                              TMOBJTYPE_SOUND,
  132.                                                              (struct TMObject *)
  133.                                                               tmobj);
  134.                         }
  135.                         break;
  136.      case TMOP_TopEdge: tmobj->io_TopEdge=ti->ti_Data;
  137.                         break;
  138.     }
  139.    }
  140.  
  141.    /* Set flags */
  142.    tmobj->io_Flags=PackBoolTags(IO_ShowName,tags,flagmap);
  143.  
  144.    /* Create AppIcon */
  145.    CreateAppIcon(tmobj,name);
  146.  
  147.    /* Initialize rest of structure */
  148.    tmobj->io_Link.tml_Linked=tmobj;
  149.  
  150.    /* All OK */
  151.    return(tmobj);
  152.   }
  153.   FreeWorkbench();
  154.  }
  155.  /* call failed */
  156.  return(NULL);
  157. }
  158.  
  159. /* Delete an Icon object */
  160. BOOL DeleteTMObjectIcon(struct TMObjectIcon *tmobj)
  161. {
  162.  DEBUG_PRINTF("Delete/Icon (0x%08lx)\n",tmobj);
  163.  
  164.  /* Free resources */
  165.  if (tmobj->io_AppIcon) {
  166.   SafeRemoveAppIcon(tmobj->io_AppIcon,&tmobj->io_Link);
  167.   FreeMem(tmobj->io_DiskObj,sizeof(struct DiskObject));
  168.  }
  169.  FreeWorkbench();
  170.  
  171.  /* Remove links */
  172.  if (tmobj->io_ExecLink) RemLinkTMObject(tmobj->io_ExecLink);
  173.  if (tmobj->io_ImageLink)
  174.   RemLinkTMObject((struct TMLink *) tmobj->io_ImageLink);
  175.  if (tmobj->io_SoundLink) RemLinkTMObject(tmobj->io_SoundLink);
  176.  
  177.  /* Remove object from list */
  178.  Remove((struct Node *) tmobj);
  179.  
  180.  /* Free object */
  181.  FreeMem(tmobj,sizeof(struct TMObjectIcon));
  182.  
  183.  /* All OK. */
  184.  return(TRUE);
  185. }
  186.  
  187. /* Change an Icon object */
  188. struct TMObject *ChangeTMObjectIcon(struct TMHandle *handle,
  189.                                     struct TMObjectIcon *tmobj,
  190.                                     struct TagItem *tags)
  191. {
  192.  struct TagItem *ti,*tstate;
  193.  struct TMLinkImage *oldimage=NULL;
  194.  void *oldicon=NULL;
  195.  
  196.  /* Scan tag list */
  197.  tstate=tags;
  198.  while (ti=NextTagItem(&tstate)) {
  199.  
  200.   DEBUG_PRINTF("Got Tag (0x%08lx)\n",ti->ti_Tag);
  201.  
  202.   switch (ti->ti_Tag) {
  203.    case TMOP_Exec:     if (tmobj->io_ExecLink) { /* Already got a link? */
  204.                         /* Yes, remove it! */
  205.                         RemLinkTMObject(tmobj->io_ExecLink);
  206.                         tmobj->io_ExecLink=NULL;
  207.                        }
  208.  
  209.                        if (ti->ti_Data) {
  210.                         /* Create new link to exec object */
  211.                         tmobj->io_ExecLink=AddLinkTMObject(handle,
  212.                                                            (char *) ti->ti_Data,
  213.                                                            TMOBJTYPE_EXEC,
  214.                                                            (struct TMObject *)
  215.                                                             tmobj);
  216.                        }
  217.                        break;
  218.    case TMOP_Image:    oldimage=tmobj->io_ImageLink;
  219.                        oldicon=tmobj->io_AppIcon;
  220.  
  221.                        if (ti->ti_Data) {
  222.                         /* Create new link to exec object */
  223.                         tmobj->io_ImageLink=AddLinkTMObject(handle,
  224.                                                             (char *)
  225.                                                              ti->ti_Data,
  226.                                                             TMOBJTYPE_IMAGE,
  227.                                                             (struct TMObject *)
  228.                                                              tmobj);
  229.                        }
  230.                        break;
  231.    case TMOP_LeftEdge: tmobj->io_LeftEdge=ti->ti_Data;
  232.                        oldicon=tmobj->io_AppIcon;
  233.                        break;
  234.    case TMOP_Sound:    if (tmobj->io_SoundLink) { /* Already got a link? */
  235.                         /* Yes, remove it! */
  236.                         RemLinkTMObject(tmobj->io_SoundLink);
  237.                         tmobj->io_SoundLink=NULL;
  238.                        }
  239.  
  240.                        if (ti->ti_Data) {
  241.                         /* Create new link to exec object */
  242.                         tmobj->io_SoundLink=AddLinkTMObject(handle,
  243.                                                             (char *)
  244.                                                              ti->ti_Data,
  245.                                                             TMOBJTYPE_SOUND,
  246.                                                             (struct TMObject *)
  247.                                                              tmobj);
  248.                        }
  249.                        break;
  250.    case TMOP_TopEdge:  tmobj->io_TopEdge=ti->ti_Data;
  251.                        oldicon=tmobj->io_AppIcon;
  252.                        break;
  253.   }
  254.  }
  255.  
  256.  /* Set new flags */
  257.  {
  258.   ULONG oldflags=tmobj->io_Flags;
  259.  
  260.   /* Flags changed? */
  261.   if ((tmobj->io_Flags=PackBoolTags(oldflags,tags,flagmap)) != oldflags)
  262.    /* Yes. Create new icon */
  263.    oldicon=tmobj->io_AppIcon;
  264.  }
  265.  
  266.  /* Create new icon? */
  267.  if (oldicon) {
  268.   /* Yes! Remove old one */
  269.   SafeRemoveAppIcon(tmobj->io_AppIcon,&tmobj->io_Link);
  270.   tmobj->io_AppIcon=NULL;
  271.   FreeMem(tmobj->io_DiskObj,sizeof(struct DiskObject));
  272.   tmobj->io_DiskObj=NULL;
  273.  
  274.   /* Remove old image link? */
  275.   if (oldimage) RemLinkTMObject((struct TMLink *) oldimage);
  276.  
  277.   /* Create AppIcon */
  278.   CreateAppIcon(tmobj,tmobj->io_Object.tmo_Name);
  279.  }
  280.  
  281.  /* All OK */
  282.  return(TRUE);
  283. }
  284.  
  285. /* Update link structures */
  286. void DeleteLinkTMObjectIcon(struct TMLink *tml)
  287. {
  288.  struct TMObjectIcon *tmobj=tml->tml_LinkedTo;
  289.  
  290.  /* Clear link */
  291.  if (tml==tmobj->io_ExecLink)
  292.   tmobj->io_ExecLink=NULL;
  293.  else if (tml==(struct TMLink *) tmobj->io_ImageLink) {
  294.   if (tmobj->io_AppIcon) {
  295.    SafeRemoveAppIcon(tmobj->io_AppIcon,&tmobj->io_Link);
  296.    tmobj->io_AppIcon=NULL;
  297.   }
  298.   if (tmobj->io_DiskObj) {
  299.    FreeMem(tmobj->io_DiskObj,sizeof(struct DiskObject));
  300.    tmobj->io_DiskObj=NULL;
  301.   }
  302.   tmobj->io_ImageLink=NULL;
  303.  }
  304.  else if (tml==tmobj->io_SoundLink)
  305.   tmobj->io_SoundLink=NULL;
  306. }
  307.  
  308. /* Activate an Icon object */
  309. void ActivateTMObjectIcon(struct TMLink *tml, struct AppMessage *msg)
  310. {
  311.  struct TMObjectIcon *tmobj=tml->tml_Linked;
  312.  
  313.  /* Activate Sound object */
  314.  if (tmobj->io_SoundLink) CallActivateTMObject(tmobj->io_SoundLink,NULL);
  315.  
  316.  /* Activate Exec object */
  317.  if (tmobj->io_ExecLink) CallActivateTMObject(tmobj->io_ExecLink,msg);
  318. }
  319.  
  320.