home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 109 / EnigmaAmiga109CD.iso / software / varie / freewheel / cx.c < prev    next >
C/C++ Source or Header  |  1999-12-27  |  3KB  |  162 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #include <exec/types.h>
  5. #include <exec/ports.h>
  6. #include <exec/memory.h>
  7. #include <libraries/commodities.h>
  8. #include <clib/exec_protos.h>
  9. #include <clib/commodities_protos.h>
  10. #include <clib/alib_protos.h>
  11.  
  12. #include "Icon.h"
  13.  
  14. #include "Cx.h"
  15.  
  16. BOOL HandleCxMessages(struct CxContext *cx,unsigned long signals);
  17. void DisposeCxContext(struct CxContext *cx);
  18. BOOL Cx_SetHotKey(struct CxContext *cx,char *hotkey);
  19. BOOL Cx_SetCustomRoutine(struct CxContext *cx,void (*rout)(CxMsg *msg,CxObj *obj));
  20.  
  21.  
  22. BOOL Cx_SetHotKey(struct CxContext *cx,char *hotkey)
  23. {
  24.   if(cx->HotKey)
  25.     DeleteCxObjAll(cx->HotKey);
  26.   if(cx->HotKey=HotKey(hotkey,cx->Port,CXCMD_APPEAR))
  27.     AttachCxObj(cx->Broker,cx->HotKey);
  28.   if(cx->HotKey)
  29.     return(TRUE);
  30.   else
  31.     return(FALSE);
  32. }
  33.  
  34.  
  35. BOOL Cx_SetCustomRoutine(struct CxContext *cx,void (*rout)(CxMsg *msg,CxObj *obj))
  36. {
  37.   if(cx->CustomObject)
  38.     DeleteCxObjAll(cx->CustomObject);
  39.   if(cx->CustomObject=CxCustom(rout,0))
  40.     AttachCxObj(cx->Broker,cx->CustomObject);
  41.   if(cx->CustomObject)
  42.     return(TRUE);
  43.   else
  44.     return(FALSE);
  45. }
  46.  
  47.  
  48. BOOL HandleCxMessages(struct CxContext *cx,unsigned long signals)
  49. {
  50.   struct Message *msg;
  51.   long id;
  52.   BOOL result=TRUE;
  53.   if(cx)
  54.   {
  55.     if(signals&cx->Signals)
  56.     {
  57.       while(msg=GetMsg(cx->Port))
  58.       {
  59.         id=CxMsgID((CxMsg *)msg);
  60.         ReplyMsg(msg);
  61.         switch(id)
  62.         {
  63.           case CXCMD_DISABLE:
  64.             ActivateCxObj(cx->CustomObject,FALSE);
  65.             break;
  66.           case CXCMD_ENABLE:
  67.             ActivateCxObj(cx->CustomObject,TRUE);
  68.             break;
  69.           case CXCMD_UNIQUE:
  70.           case CXCMD_APPEAR:
  71.             if(cx->ShowCallback)
  72.               cx->ShowCallback(cx);
  73.             break;
  74.           case CXCMD_DISAPPEAR:
  75.             if(cx->HideCallback)
  76.               cx->HideCallback(cx);
  77.             break;
  78.           case CXCMD_KILL:
  79.             result=FALSE;
  80.             break;
  81.         }
  82.       }
  83.     }
  84.   }
  85.   return(result);
  86. }
  87.  
  88.  
  89. struct CxContext *CxContext_Create(char *name,char *title,char *descr,void *userdata)
  90. {
  91.   struct CxContext *cx;
  92.   char *hotkeystring;
  93.   CxObj *hotkeyobj;
  94.  
  95.   struct NewBroker MyNewBroker =
  96.   {
  97.     NB_VERSION,
  98.     NULL,
  99.     NULL,
  100.     NULL,
  101.     NBU_UNIQUE|NBU_NOTIFY,
  102.     COF_SHOW_HIDE,
  103.     127,
  104.     NULL
  105.   };
  106.  
  107.   MyNewBroker.nb_Name=name;
  108.   MyNewBroker.nb_Title=title;
  109.   MyNewBroker.nb_Descr=descr;
  110.  
  111.   if(!(cx=malloc(sizeof(struct CxContext))))
  112.     return(NULL);
  113.   memset(cx,0,sizeof(struct CxContext));
  114.  
  115.   cx->Dispose=DisposeCxContext;
  116.   cx->UserData=userdata;
  117.   cx->Handle=HandleCxMessages;
  118.   cx->SetHotKey=Cx_SetHotKey;
  119.   cx->SetCustom=Cx_SetCustomRoutine;
  120.  
  121.   cx->HotKey=NULL;
  122.   cx->CustomObject=NULL;
  123.  
  124.   if(!(cx->Port=CreateMsgPort()))
  125.   {
  126.     cx->Dispose(cx);
  127.     return(NULL);
  128.   }
  129.   cx->Signals=(1<<cx->Port->mp_SigBit);
  130.   MyNewBroker.nb_Port=cx->Port;
  131.  
  132.   if(!(cx->Broker=CxBroker(&MyNewBroker,NULL)))
  133.   {
  134.     cx->Dispose(cx);
  135.     return(NULL);
  136.   }
  137.  
  138.   ActivateCxObj(cx->Broker,TRUE);
  139.  
  140.   return(cx);
  141. }
  142.  
  143.  
  144. void DisposeCxContext(struct CxContext *cx)
  145. {
  146.   if(cx)
  147.   {
  148.     if(cx->Broker)
  149.     {
  150.       DeleteCxObjAll(cx->Broker);
  151.       cx->Broker=NULL;
  152.     }
  153.     if(cx->Port)
  154.     {
  155.       DeleteMsgPort(cx->Port);
  156.       cx->Port=NULL;
  157.     }
  158.     free(cx);
  159.   }
  160. }
  161.  
  162.