eventi.inc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. {
  2. Free Pascal port of the OpenPTC C++ library.
  3. Copyright (C) 2001-2003 Nikolay Nikolov ([email protected])
  4. Original C++ version by Glenn Fiedler ([email protected])
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with this library; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. }
  17. {Function TPTCExposeEvent.GetType : TPTCEventType;
  18. Begin
  19. Result := PTCExposeEvent;
  20. End;}
  21. Type
  22. PEventLinkedList = ^TEventLinkedList;
  23. TEventLinkedList = Record
  24. Event : TPTCEvent;
  25. Next : PEventLinkedList;
  26. End;
  27. TEventQueue = Class(TObject)
  28. Private
  29. FHead, FTail : PEventLinkedList;
  30. Public
  31. Constructor Create;
  32. Destructor Destroy; Override;
  33. Procedure AddEvent(event : TPTCEvent);
  34. Function PeekEvent(Const EventMask : TPTCEventMask) : TPTCEvent;
  35. Function NextEvent(Const EventMask : TPTCEventMask) : TPTCEvent;
  36. End;
  37. Constructor TEventQueue.Create;
  38. Begin
  39. FHead := Nil;
  40. FTail := Nil;
  41. End;
  42. Destructor TEventQueue.Destroy;
  43. Var
  44. p, pnext : PEventLinkedList;
  45. Begin
  46. p := FHead;
  47. While p <> Nil Do
  48. Begin
  49. FreeAndNil(p^.Event);
  50. pnext := p^.Next;
  51. Dispose(p);
  52. p := pnext;
  53. End;
  54. Inherited Destroy;
  55. End;
  56. Procedure TEventQueue.AddEvent(event : TPTCEvent);
  57. Var
  58. tmp : PEventLinkedList;
  59. Begin
  60. New(tmp);
  61. FillChar(tmp^, SizeOf(tmp^), 0);
  62. tmp^.Next := Nil;
  63. tmp^.Event := event;
  64. If FTail <> Nil Then
  65. Begin
  66. FTail^.Next := tmp;
  67. FTail := tmp;
  68. End
  69. Else
  70. Begin { FTail = Nil }
  71. FHead := tmp;
  72. FTail := tmp;
  73. End;
  74. End;
  75. Function TEventQueue.PeekEvent(Const EventMask : TPTCEventMask) : TPTCEvent;
  76. Var
  77. p : PEventLinkedList;
  78. Begin
  79. p := FHead;
  80. While p <> Nil Do
  81. Begin
  82. If p^.Event.EventType In EventMask Then
  83. Begin
  84. Result := p^.Event;
  85. Exit;
  86. End;
  87. p := p^.Next;
  88. End;
  89. Result := Nil;
  90. End;
  91. Function TEventQueue.NextEvent(Const EventMask : TPTCEventMask) : TPTCEvent;
  92. Var
  93. prev, p : PEventLinkedList;
  94. Begin
  95. prev := Nil;
  96. p := FHead;
  97. While p <> Nil Do
  98. Begin
  99. If p^.Event.EventType In EventMask Then
  100. Begin
  101. Result := p^.Event;
  102. { delete the element from the linked list }
  103. If prev <> Nil Then
  104. prev^.Next := p^.Next
  105. Else
  106. FHead := p^.Next;
  107. If p^.Next = Nil Then
  108. FTail := prev;
  109. Dispose(p);
  110. Exit;
  111. End;
  112. prev := p;
  113. p := p^.Next;
  114. End;
  115. Result := Nil;
  116. End;