/* Title:   wimpmsg.c
 * Purpose: functions for wimp message handling
 * Author:  IDJ
 * History: 19-Jun-94:     IDJ:   created
 *          30th Aug 1995  J R C  Hacked for Support library
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "os.h"

#include "wimpmsg.h"
#include "x.h"

static event_message_handler_item *Handlers = NULL; /*the list of all
      wimp message handlers*/

/* ------------------ dispatching a wimp message ------------------ */
void wimpmsg_dispatch (wimp_block *poll_block, toolbox_block *id_block)

{  event_message_handler_item *h;

   id_block = id_block;

   for (h = Handlers; h != NULL; h = h->next)
      if (h->handler != NULL && (h->msg_no == -1 ||
            h->msg_no == poll_block->message.action))
         if (h->handler (&poll_block->message, h->handle))
            return;
}

void wimpmsg_register_message_handler (int msg_no,
      event_message_handler *handler, void *handle)

{  event_message_handler_item *h;

   h = x_ALLOC (sizeof *h);
   h->msg_no     = msg_no;
   h->handler    = handler;
   h->handle     = handle;
   h->next       = Handlers;
   Handlers = h;
}

void wimpmsg_deregister_message_handler (int msg_no,
     event_message_handler *handler, void *handle)

{  event_message_handler_item **h;

   /*search for exact match of handler, list and handle*/
   for (h = &Handlers; *h != NULL; h = &(*h)->next)
      if ((*h)->handler == handler && (*h)->handle == handle &&
            (*h)->msg_no == msg_no)
      {  event_message_handler_item *hh = *h;

         *h = hh->next;
         x_FREE (hh, sizeof *hh);
      }
}
