home *** CD-ROM | disk | FTP | other *** search
- // Zinc Interface Library - EVENT.CPP
- // COPYRIGHT (C) 1990, 1991. All Rights Reserved.
- // Zinc Software Incorporated. Pleasant Grove, Utah USA
-
- #include "ui_evt.hpp"
- #include <dos.h>
-
- UI_EVENT_MANAGER::UI_EVENT_MANAGER(int _noOfElements, UI_DISPLAY *_display) :
- UI_LIST(UI_DEVICE::CompareFunction), queueBlock(_noOfElements),
- display(_display), level(1)
- {
- _display->eventManager = this;
- }
-
- void UI_EVENT_MANAGER::Add(UI_DEVICE *device)
- {
- // Check to see if already in device list.
- if (UI_LIST::Index(device) != -1)
- return;
-
- // Add the device to the event device list.
- UI_LIST::Add(device);
- device->display = display;
- device->eventManager = this;
- UI_EVENT event;
- event.type = device->type;
- event.rawCode = D_INITIALIZE;
- device->Event(event);
- }
-
- void UI_EVENT_MANAGER::DevicesHide(const UI_REGION ®ion)
- {
- // Hide devices when at the highest level.
- if (level-- <= 0)
- return;
-
- UI_EVENT event;
- event.type = E_DEVICE;
- event.rawCode = D_HIDE;
- event.region = region;
- for (UI_DEVICE *device = First(); device; device = device->Next())
- device->Event(event);
- }
-
- void UI_EVENT_MANAGER::DevicesShow(const UI_REGION ®ion)
- {
- // Show devices only when at the highest level.
- if (level > -1)
- {
- UI_EVENT event;
- event.type = E_DEVICE;
- event.rawCode = D_SHOW;
- event.region = region;
- for (UI_DEVICE *device = Last(); device; device = device->Previous())
- device->Event(event);
- }
- level++;
- }
-
- void UI_EVENT_MANAGER::DevicePosition(int deviceType, int column, int line)
- {
- UI_EVENT event;
- event.type = deviceType;
- event.rawCode = D_POSITION;
- event.position.column = column;
- event.position.line = line;
- UI_EVENT_MANAGER::Event(event);
- }
-
- int UI_EVENT_MANAGER::DeviceState(int deviceType, USHORT deviceState)
- {
- UI_EVENT event;
- event.type = deviceType;
- event.rawCode = deviceState;
- return (UI_EVENT_MANAGER::Event(event));
- }
-
- int UI_EVENT_MANAGER::Event(const UI_EVENT &event)
- {
- // Process the event.
- int ccode = event.type;
- int status = D_OFF;
- if (ccode == S_RESET_DISPLAY && !event.data)
- {
- UI_EVENT tEvent;
- tEvent.type = E_DEVICE;
- tEvent.rawCode = D_RESTORE;
- for (UI_DEVICE *device = First(); device; device = device->Next())
- status = device->Event(tEvent);
- }
- else if (ccode == S_RESET_DISPLAY)
- {
- display = (UI_DISPLAY *)event.data;
- display->eventManager = this;
- UI_DEVICE *tDevice = First();
- first = current = last = NULL;
- while (tDevice)
- {
- UI_DEVICE *device = tDevice;
- tDevice = device->Next();
- UI_EVENT_MANAGER::Add(device);
- }
- }
- else
- {
- for (UI_DEVICE *device = First(); device; device = device->Next())
- if (ccode == E_DEVICE || device->type < E_ALL_INPUT_DEVICES || ccode == device->type)
- status = device->Event(event);
- }
- return (status);
- }
-
- int UI_EVENT_MANAGER::Get(UI_EVENT &event, USHORT flags)
- {
- UI_DEVICE *device;
- UI_QUEUE_ELEMENT *element;
- int error = -1;
-
- // Stay in loop while no event conditions are met.
- do
- {
- // Call all the polled devices.
- if (!FlagSet(flags, Q_NO_POLL))
- for (device = First(); device; device = device->Next())
- device->Poll();
-
- // Get the event.
- element = (FlagSet(flags, Q_END)) ?
- queueBlock.Last() : queueBlock.First();
- if (element)
- {
- event = element->event;
- if (!FlagSet(flags, Q_NO_DESTROY))
- queueBlock.Subtract((UI_ELEMENT *)element);
- error = 0;
- }
- else if (FlagSet(flags, Q_NO_BLOCK))
- {
- return (-2);
- }
-
- } while (error);
-
- // Return the error status.
- return (error);
- }
-
- void UI_EVENT_MANAGER::Put(const UI_EVENT &event, USHORT flags)
- {
- // Place the event back in the event queue.
- UI_QUEUE_ELEMENT *element = (FlagSet(flags, Q_END)) ?
- (UI_QUEUE_ELEMENT *)queueBlock.Add(0) :
- (UI_QUEUE_ELEMENT *)queueBlock.Add(queueBlock.First());
- if (element)
- element->event = event;
- }
-
- void UI_EVENT_MANAGER::Subtract(UI_DEVICE *device)
- {
- UI_EVENT event;
- event.type = device->type;
- event.rawCode = D_RESTORE;
- device->Event(event);
- if (UI_LIST::Index(device) != -1)
- UI_LIST::Subtract(device);
- }
-
- int UI_DEVICE::CompareFunction(void *device1, void *device2)
- {
- return (((UI_DEVICE *)device1)->type - ((UI_DEVICE *)device2)->type);
- }
-
- UI_QUEUE_BLOCK::UI_QUEUE_BLOCK(int a_noOfElements) :
- UI_LIST_BLOCK(a_noOfElements, 0)
- {
- // Initialize the queue block.
- UI_QUEUE_ELEMENT *queueBlock = new UI_QUEUE_ELEMENT[a_noOfElements];
- elementArray = queueBlock;
- for (int i = 0; i < a_noOfElements; i++)
- freeList.Add(0, &queueBlock[i]);
- }
-
- UI_QUEUE_BLOCK::~UI_QUEUE_BLOCK()
- {
- // Free the queue block.
- UI_QUEUE_ELEMENT *queueBlock = (UI_QUEUE_ELEMENT *)elementArray;
- delete [noOfElements]queueBlock;
- }
-