home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0040 - 0049 / ibm0040-0049 / ibm0040.tar / ibm0040 / ZINC_6.ZIP / DOSSRC.ZIP / EVENT.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-01  |  4.6 KB  |  192 lines

  1. //    Zinc Interface Library - EVENT.CPP
  2. //    COPYRIGHT (C) 1990, 1991.  All Rights Reserved.
  3. //    Zinc Software Incorporated.  Pleasant Grove, Utah  USA
  4.  
  5. #include "ui_evt.hpp"
  6. #include <dos.h>
  7.  
  8. UI_EVENT_MANAGER::UI_EVENT_MANAGER(int _noOfElements, UI_DISPLAY *_display) :
  9.     UI_LIST(UI_DEVICE::CompareFunction), queueBlock(_noOfElements),
  10.     display(_display), level(1)
  11. {
  12.     _display->eventManager = this;
  13. }
  14.  
  15. void UI_EVENT_MANAGER::Add(UI_DEVICE *device)
  16. {
  17.     // Add the device to the object list.
  18.     UI_LIST::Add(device);
  19.  
  20.     device->display = display;
  21.     device->eventManager = this;
  22.     UI_EVENT event;
  23.     event.type = device->type;
  24.     event.rawCode = D_INITIALIZE;
  25.     device->Event(event);
  26. }
  27.  
  28. void UI_EVENT_MANAGER::DevicesHide(const UI_REGION ®ion)
  29. {
  30.     // Hide devices when at the highest level.
  31.     if (level-- <= 0)
  32.         return;
  33.  
  34.     disable();
  35.     UI_EVENT event;
  36.     event.type = E_DEVICE;
  37.     event.rawCode = D_HIDE;
  38.     event.region = region;
  39.     for (UI_DEVICE *device = First(); device; device = device->Next())
  40.          device->Event(event);
  41. }
  42.  
  43. void UI_EVENT_MANAGER::DevicesShow(const UI_REGION ®ion)
  44. {
  45.     // Show devices only when at the highest level.
  46.     if (level > -1)
  47.     {
  48.         UI_EVENT event;
  49.         event.type = E_DEVICE;
  50.         event.rawCode = D_SHOW;
  51.         event.region = region;
  52.         for (UI_DEVICE *device = Last(); device; device = device->Previous())
  53.              device->Event(event);
  54.         enable();
  55.     }
  56.     level++;
  57. }
  58.  
  59. void UI_EVENT_MANAGER::DevicePosition(int deviceType, int column, int line)
  60. {
  61.     UI_EVENT event;
  62.     event.type = deviceType;
  63.     event.rawCode = D_POSITION;
  64.     event.position.column = column;
  65.     event.position.line = line;
  66.     UI_EVENT_MANAGER::Event(event);
  67. }
  68.  
  69. int UI_EVENT_MANAGER::DeviceState(int deviceType, USHORT deviceState)
  70. {
  71.     UI_EVENT event;
  72.     event.type = deviceType;
  73.     event.rawCode = deviceState;
  74.     return (UI_EVENT_MANAGER::Event(event));
  75. }
  76.  
  77. int UI_EVENT_MANAGER::Event(const UI_EVENT &event)
  78. {
  79.     // Process the event.
  80.     int ccode = event.type;
  81.     int status = D_OFF;
  82.     if (ccode == S_RESET_DISPLAY && !event.data)
  83.     {
  84.         UI_EVENT tEvent;
  85.         tEvent.type = E_DEVICE;
  86.         tEvent.rawCode = D_RESTORE;
  87.         for (UI_DEVICE *device = First(); device; device = device->Next())
  88.              status = device->Event(tEvent);
  89.     }
  90.     else if (ccode == S_RESET_DISPLAY)
  91.     {
  92.         display = (UI_DISPLAY *)event.data;
  93.         display->eventManager = this;
  94.         UI_DEVICE *tDevice = First();
  95.         first = current = last = NULL;
  96.         while (tDevice)
  97.         {
  98.             UI_DEVICE *device = tDevice;
  99.             tDevice = device->Next();
  100.             UI_EVENT_MANAGER::Add(device);
  101.         }
  102.     }
  103.     else
  104.     {
  105.         for (UI_DEVICE *device = First(); device; device = device->Next())
  106.             if (ccode == E_DEVICE || ccode == device->type)
  107.                  status = device->Event(event);
  108.     }
  109.     return (status);
  110. }
  111.  
  112. int UI_EVENT_MANAGER::Get(UI_EVENT &event, USHORT flags)
  113. {
  114.     UI_DEVICE *device;
  115.     UI_QUEUE_ELEMENT *element;
  116.     int error = -1;
  117.  
  118.     // Stay in loop while no event conditions are met.
  119.     do
  120.     {
  121.         // Call all the polled devices.
  122.         if (!FlagSet(flags, Q_NO_POLL))
  123.             for (device = First(); device; device = device->Next())
  124.                  device->Poll();
  125.  
  126.         // Get the event.
  127.         disable();
  128.         element = (FlagSet(flags, Q_END)) ?
  129.             queueBlock.Last() : queueBlock.First();
  130.         if (element)
  131.         {
  132.             event = element->event;
  133.             if (!FlagSet(flags, Q_NO_DESTROY))
  134.                 queueBlock.Subtract((UI_ELEMENT *)element);
  135.             error = 0;
  136.         }
  137.         else if (FlagSet(flags, Q_NO_BLOCK))
  138.         {
  139.             enable();
  140.             return (-2);
  141.         }
  142.         enable();
  143.  
  144.     } while (error);
  145.  
  146.     // Return the error status.
  147.     return (error);
  148. }
  149.  
  150. void UI_EVENT_MANAGER::Put(const UI_EVENT &event, USHORT flags)
  151. {
  152.     // Place the event back in the event queue.
  153.     disable();
  154.     UI_QUEUE_ELEMENT *element = (FlagSet(flags, Q_END)) ?
  155.         (UI_QUEUE_ELEMENT *)queueBlock.Add(0) :
  156.         (UI_QUEUE_ELEMENT *)queueBlock.Add(queueBlock.First());
  157.     if (element)
  158.         element->event = event;
  159.     enable();
  160. }
  161.  
  162. void UI_EVENT_MANAGER::Subtract(UI_DEVICE *device)
  163. {
  164.     UI_EVENT event;
  165.     event.type = device->type;
  166.     event.rawCode = D_RESTORE;
  167.     device->Event(event);
  168.     UI_LIST::Subtract(device);
  169. }
  170.  
  171. int UI_DEVICE::CompareFunction(void *device1, void *device2)
  172. {
  173.     return (((UI_DEVICE *)device1)->type - ((UI_DEVICE *)device2)->type);
  174. }
  175.  
  176. UI_QUEUE_BLOCK::UI_QUEUE_BLOCK(int a_noOfElements) :
  177.     UI_LIST_BLOCK(a_noOfElements, 0)
  178. {
  179.     // Initialize the queue block.
  180.     UI_QUEUE_ELEMENT *queueBlock = new UI_QUEUE_ELEMENT[a_noOfElements];
  181.     elementArray = queueBlock;
  182.     for (int i = 0; i < a_noOfElements; i++)
  183.         freeList.Add(0, &queueBlock[i]);
  184. }
  185.  
  186. UI_QUEUE_BLOCK::~UI_QUEUE_BLOCK()
  187. {
  188.     // Free the queue block.
  189.     UI_QUEUE_ELEMENT *queueBlock = (UI_QUEUE_ELEMENT *)elementArray;
  190.     delete [noOfElements]queueBlock;
  191. }
  192.