home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 2 / DATAFILE_PDCD2.iso / utilities2 / desklib / Libraries / Event / c / MClaimInit < prev    next >
Encoding:
Text File  |  1992-04-19  |  4.1 KB  |  141 lines

  1. /*
  2.     ####             #    #     # #
  3.     #   #            #    #       #          The FreeWare C library for 
  4.     #   #  ##   ###  #  # #     # ###             RISC OS machines
  5.     #   # #  # #     # #  #     # #  #   ___________________________________
  6.     #   # ####  ###  ##   #     # #  #                                      
  7.     #   # #        # # #  #     # #  #    Please refer to the accompanying
  8.     ####   ### ####  #  # ##### # ###    documentation for conditions of use
  9.     ________________________________________________________________________
  10.  
  11.     File:    Event.MClaimInit.c
  12.     Author:  Copyright © 1992 Jason Williams
  13.     Version: 1.03 (11 Apr 1992)
  14.     Purpose: Extension to Event.c to allow routing of specific message types
  15.              to different windows' message handlers.
  16. */
  17.  
  18.  
  19. #include "EMsgDefs.h"
  20.  
  21. #define ERRBASE 1
  22. #define ERR1 ERRBASE+0
  23. #define ERRMESS1 "Unable to allocate memory for eventmsg claim"
  24.  
  25. linklist_header eventmsg__claimanchor = {NULL, NULL};
  26.  
  27.  
  28. static BOOL EventMsg_DispatchMessage(event_pollblock *event, void *reference)
  29. {
  30.   message_action action;
  31.   window_handle  window;
  32.   eventmsg_claimrecord *ptr;
  33.   eventmsg_windowrecord *wptr;
  34.  
  35.   action = event->data.message.header.action;
  36.   switch(action)
  37.   {
  38.     case message_DATASAVE:
  39.     case message_DATASAVEACK:
  40.     case message_DATALOAD:
  41.       window = event->data.message.data.words[0];
  42.       break;
  43.  
  44.     case message_HELPREQUEST:
  45.       window = event->data.message.data.helprequest.where.window;
  46.       break;
  47.  
  48.     default:
  49.       window = event_ANY;
  50.       break;
  51.   }
  52.  
  53.   ptr = (eventmsg_claimrecord *) eventmsg__claimanchor.next;
  54.   while (ptr != NULL)
  55.   {
  56.     if (ptr->messagetype == event_ANY || ptr->messagetype == action)
  57.     {
  58.       wptr = (eventmsg_windowrecord *) ptr->windowlist.next;
  59.       while (wptr != NULL)
  60.       {
  61.         if (window == event_ANY || wptr->window == event_ANY ||
  62.             wptr->window == window)
  63.           if (wptr->handler(event, wptr->reference) == TRUE)
  64.             return(TRUE);
  65.  
  66.         wptr = (eventmsg_windowrecord *) wptr->header.next;
  67.       }
  68.     }
  69.     ptr = (eventmsg_claimrecord *) ptr->header.next;
  70.   }
  71.   return(FALSE);
  72. }
  73.  
  74.  
  75.  
  76. extern BOOL EventMsg_Claim(message_action messagetype, window_handle window,
  77.                            event_handler handler, void *reference)
  78. {
  79.   eventmsg_claimrecord *ptr;
  80.   eventmsg_windowrecord *wrecord;
  81.  
  82.   ptr = (eventmsg_claimrecord *) eventmsg__claimanchor.next;
  83.   while (ptr != NULL)            /* Find claims for this message action type */
  84.   {
  85.     if (ptr->messagetype == messagetype)
  86.       break;
  87.  
  88.     ptr = (eventmsg_claimrecord *) ptr->header.next;
  89.   }
  90.  
  91.   if (ptr == NULL)                /* No current claims, so add new claimlist */
  92.   {
  93.     ptr = (eventmsg_claimrecord *) malloc(sizeof(eventmsg_claimrecord));
  94.     if (ptr == NULL)
  95.     {
  96.       Error_ReportInternal(ERR1, ERRMESS1);
  97.       return(FALSE);
  98.     }
  99.  
  100.     LinkList_Init(&(ptr->header));
  101.     ptr->messagetype = messagetype;
  102.     LinkList_Init(&(ptr->windowlist));
  103.     LinkList_AddToHead(&eventmsg__claimanchor, &(ptr->header));
  104.                                           /* Insert new message_handler list */
  105.   }
  106.  
  107.   wrecord = (eventmsg_windowrecord *) malloc(sizeof(eventmsg_windowrecord));
  108.   if (wrecord == NULL)
  109.   {
  110.     Error_ReportInternal(ERR1, ERRMESS1);
  111.     return(FALSE);
  112.   }
  113.  
  114.   LinkList_Init(&(wrecord)->header);
  115.   wrecord->window    = window;
  116.   wrecord->handler   = handler;
  117.   wrecord->reference = reference;
  118.  
  119.  /* Insert new window's handler into the window list for this message action.
  120.   * Specific-window handlers are placed on the front of the list, and non-
  121.   * specific handlers are placed on the end, to give priority to the most
  122.   * specific claim on any message.
  123.   */
  124.   if (window == event_ANY)            /* insert new window's handler in list */
  125.     LinkList_AddToTail(&(ptr->windowlist), &(wrecord->header));
  126.   else
  127.     LinkList_AddToHead(&(ptr->windowlist), &(wrecord->header));
  128.  
  129.   return(TRUE);
  130. }
  131.  
  132.  
  133.  
  134. extern void EventMsg_Initialise(void)
  135. {
  136.   Event_Claim(event_USERMESSAGE, event_ANY, event_ANY,
  137.               EventMsg_DispatchMessage, NULL);
  138.   Event_Claim(event_USERMESSAGERECORDED, event_ANY, event_ANY,
  139.               EventMsg_DispatchMessage, NULL);
  140. }
  141.