home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0040 - 0049 / ibm0040-0049 / ibm0040.tar / ibm0040 / ZINC_5.ZIP / WINSRC.ZIP / EVENT.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-01  |  4.7 KB  |  189 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.     // Check to see if already in device list.
  18.     if (UI_LIST::Index(device) != -1)
  19.         return;
  20.  
  21.     // Add the device to the event device list.
  22.     UI_LIST::Add(device);
  23.     device->display = display;
  24.     device->eventManager = this;
  25.     UI_EVENT event;
  26.     event.type = device->type;
  27.     event.rawCode = D_INITIALIZE;
  28.     device->Event(event);
  29. }
  30.  
  31. void UI_EVENT_MANAGER::DevicesHide(const UI_REGION ®ion)
  32. {
  33.     // Hide devices when at the highest level.
  34.     if (level-- <= 0)
  35.         return;
  36.  
  37.     UI_EVENT event;
  38.     event.type = E_DEVICE;
  39.     event.rawCode = D_HIDE;
  40.     event.region = region;
  41.     for (UI_DEVICE *device = First(); device; device = device->Next())
  42.          device->Event(event);
  43. }
  44.  
  45. void UI_EVENT_MANAGER::DevicesShow(const UI_REGION ®ion)
  46. {
  47.     // Show devices only when at the highest level.
  48.     if (level > -1)
  49.     {
  50.         UI_EVENT event;
  51.         event.type = E_DEVICE;
  52.         event.rawCode = D_SHOW;
  53.         event.region = region;
  54.         for (UI_DEVICE *device = Last(); device; device = device->Previous())
  55.              device->Event(event);
  56.     }
  57.     level++;
  58. }
  59.  
  60. void UI_EVENT_MANAGER::DevicePosition(int deviceType, int column, int line)
  61. {
  62.     UI_EVENT event;
  63.     event.type = deviceType;
  64.     event.rawCode = D_POSITION;
  65.     event.position.column = column;
  66.     event.position.line = line;
  67.     UI_EVENT_MANAGER::Event(event);
  68. }
  69.  
  70. int UI_EVENT_MANAGER::DeviceState(int deviceType, USHORT deviceState)
  71. {
  72.     UI_EVENT event;
  73.     event.type = deviceType;
  74.     event.rawCode = deviceState;
  75.     return (UI_EVENT_MANAGER::Event(event));
  76. }
  77.  
  78. int UI_EVENT_MANAGER::Event(const UI_EVENT &event)
  79. {
  80.     // Process the event.
  81.     int ccode = event.type;
  82.     int status = D_OFF;
  83.     if (ccode == S_RESET_DISPLAY && !event.data)
  84.     {
  85.         UI_EVENT tEvent;
  86.         tEvent.type = E_DEVICE;
  87.         tEvent.rawCode = D_RESTORE;
  88.         for (UI_DEVICE *device = First(); device; device = device->Next())
  89.              status = device->Event(tEvent);
  90.     }
  91.     else if (ccode == S_RESET_DISPLAY)
  92.     {
  93.         display = (UI_DISPLAY *)event.data;
  94.         display->eventManager = this;
  95.         UI_DEVICE *tDevice = First();
  96.         first = current = last = NULL;
  97.         while (tDevice)
  98.         {
  99.             UI_DEVICE *device = tDevice;
  100.             tDevice = device->Next();
  101.             UI_EVENT_MANAGER::Add(device);
  102.         }
  103.     }
  104.     else
  105.     {
  106.         for (UI_DEVICE *device = First(); device; device = device->Next())
  107.             if (ccode == E_DEVICE || device->type < E_ALL_INPUT_DEVICES || ccode == device->type)
  108.                  status = device->Event(event);
  109.     }
  110.     return (status);
  111. }
  112.  
  113. int UI_EVENT_MANAGER::Get(UI_EVENT &event, USHORT flags)
  114. {
  115.     UI_DEVICE *device;
  116.     UI_QUEUE_ELEMENT *element;
  117.     int error = -1;
  118.  
  119.     // Stay in loop while no event conditions are met.
  120.     do
  121.     {
  122.         // Call all the polled devices.
  123.         if (!FlagSet(flags, Q_NO_POLL))
  124.             for (device = First(); device; device = device->Next())
  125.                  device->Poll();
  126.  
  127.         // Get the event.
  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.             return (-2);
  140.         }
  141.  
  142.     } while (error);
  143.  
  144.     // Return the error status.
  145.     return (error);
  146. }
  147.  
  148. void UI_EVENT_MANAGER::Put(const UI_EVENT &event, USHORT flags)
  149. {
  150.     // Place the event back in the event queue.
  151.     UI_QUEUE_ELEMENT *element = (FlagSet(flags, Q_END)) ?
  152.         (UI_QUEUE_ELEMENT *)queueBlock.Add(0) :
  153.         (UI_QUEUE_ELEMENT *)queueBlock.Add(queueBlock.First());
  154.     if (element)
  155.         element->event = event;
  156. }
  157.  
  158. void UI_EVENT_MANAGER::Subtract(UI_DEVICE *device)
  159. {
  160.     UI_EVENT event;
  161.     event.type = device->type;
  162.     event.rawCode = D_RESTORE;
  163.     device->Event(event);
  164.     if (UI_LIST::Index(device) != -1)
  165.         UI_LIST::Subtract(device);
  166. }
  167.  
  168. int UI_DEVICE::CompareFunction(void *device1, void *device2)
  169. {
  170.     return (((UI_DEVICE *)device1)->type - ((UI_DEVICE *)device2)->type);
  171. }
  172.  
  173. UI_QUEUE_BLOCK::UI_QUEUE_BLOCK(int a_noOfElements) :
  174.     UI_LIST_BLOCK(a_noOfElements, 0)
  175. {
  176.     // Initialize the queue block.
  177.     UI_QUEUE_ELEMENT *queueBlock = new UI_QUEUE_ELEMENT[a_noOfElements];
  178.     elementArray = queueBlock;
  179.     for (int i = 0; i < a_noOfElements; i++)
  180.         freeList.Add(0, &queueBlock[i]);
  181. }
  182.  
  183. UI_QUEUE_BLOCK::~UI_QUEUE_BLOCK()
  184. {
  185.     // Free the queue block.
  186.     UI_QUEUE_ELEMENT *queueBlock = (UI_QUEUE_ELEMENT *)elementArray;
  187.     delete [noOfElements]queueBlock;
  188. }
  189.