msgloop.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Command & Conquer *
  23. * *
  24. * $Archive:: /Commando/Library/msgloop.cpp $*
  25. * *
  26. * $Author:: Greg_h $*
  27. * *
  28. * $Modtime:: 7/22/97 11:37a $*
  29. * *
  30. * $Revision:: 1 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * Add_Accelerator -- Adds a keyboard accelerator to the message handler. *
  35. * Add_Modeless_Dialog -- Adds a modeless dialog box to the message handler. *
  36. * Remove_Accelerator -- Removes an accelerator from the message processor. *
  37. * Remove_Modeless_Dialog -- Removes the dialog box from the message tracking handler. *
  38. * Windows_Message_Handler -- Handles windows message. *
  39. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  40. #include "always.h"
  41. #include "vector.h"
  42. #include "win.h"
  43. /*
  44. ** Tracks modeless dialog box messages by keeping a record of all active modeless dialog
  45. ** box handles and then determining if the windows message applies to the dialog box. If it
  46. ** does, then the default message handling should not be performed.
  47. */
  48. static DynamicVectorClass<HWND> _ModelessDialogs;
  49. /*
  50. ** Tracks windows accelerators with this structure.
  51. */
  52. struct AcceleratorTracker {
  53. AcceleratorTracker(HWND window = NULL, HACCEL accelerator = NULL) : Accelerator(accelerator), Window(window) {}
  54. int operator == (AcceleratorTracker const & acc) const {return(Accelerator == acc.Accelerator && Window == acc.Window);}
  55. int operator != (AcceleratorTracker const & acc) const {return(!(*this == acc));}
  56. HACCEL Accelerator;
  57. HWND Window;
  58. };
  59. static DynamicVectorClass<AcceleratorTracker> _Accelerators;
  60. /*
  61. ** In those cases where message intercept needs to occur but not for purposes
  62. ** of a modeless dialog box or a windows accelerator, then this is a function
  63. ** pointer to than message intercept handler.
  64. */
  65. bool (*Message_Intercept_Handler)(MSG &msg) = NULL;
  66. /***********************************************************************************************
  67. * Windows_Message_Handler -- Handles windows message. *
  68. * *
  69. * This routine will take all messages that have accumulated in the message queue and *
  70. * dispatch them to their respective recipients. When the message queue has been emptied, *
  71. * then this routine will return. By using this routine, it is possible to have the main *
  72. * program run in the main thread and yet still have it behave like a normal program as *
  73. * far as message handling is concerned. To achieve this, this routine must be called on *
  74. * a semi-frequent basis (a few times a second is plenty). *
  75. * *
  76. * INPUT: none *
  77. * *
  78. * OUTPUT: none *
  79. * *
  80. * WARNINGS: none *
  81. * *
  82. * HISTORY: *
  83. * 05/17/1997 JLB : Created. *
  84. *=============================================================================================*/
  85. void Windows_Message_Handler(void)
  86. {
  87. MSG msg;
  88. /*
  89. ** Process windows messages until the message queue is exhuasted.
  90. */
  91. while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) {
  92. if (!GetMessage( &msg, NULL, 0, 0 )) {
  93. return;
  94. }
  95. /*
  96. ** Pass the message through any loaded accelerators. If the message
  97. ** was processed by an accelerator, then it doesn't need to be
  98. ** processed by the normal message handling procedure.
  99. */
  100. bool processed = false;
  101. for (int aindex = 0; aindex < _Accelerators.Count(); aindex++) {
  102. if (TranslateAccelerator(_Accelerators[aindex].Window, _Accelerators[aindex].Accelerator, &msg)) {
  103. processed = true;
  104. }
  105. break;
  106. }
  107. if (processed) continue;
  108. /*
  109. ** Pass the windows message through any modeless dialogs that may
  110. ** be active. If one of the dialogs processes the message, then
  111. ** it must not be processed by the normal window message handler.
  112. */
  113. for (int index = 0; index < _ModelessDialogs.Count(); index++) {
  114. if (IsDialogMessage(_ModelessDialogs[index], &msg)) {
  115. processed = true;
  116. break;
  117. }
  118. }
  119. if (processed) continue;
  120. /*
  121. ** If the message was not handled by any normal intercept handlers, then
  122. ** submit the message to a custom message handler if one has been provided.
  123. */
  124. if (Message_Intercept_Handler != NULL) {
  125. processed = Message_Intercept_Handler(msg);
  126. }
  127. if (processed) continue;
  128. /*
  129. ** If the message makes it to this point, then it must be a normal message. Process
  130. ** it in the normal fashion. The message will appear in the window message handler
  131. ** for the window that it was directed to.
  132. */
  133. TranslateMessage(&msg);
  134. DispatchMessage(&msg);
  135. }
  136. }
  137. /***********************************************************************************************
  138. * Add_Modeless_Dialog -- Adds a modeless dialog box to the message handler. *
  139. * *
  140. * When a modeless dialog box becomes active, the messages processed by the main message *
  141. * handler must be handled different. This routine is used to inform the message handler *
  142. * that a dialog box is active and messages must be fed to it as appropriate. *
  143. * *
  144. * INPUT: dialog -- Handle to the modeless dialog box. *
  145. * *
  146. * OUTPUT: none *
  147. * *
  148. * WARNINGS: The modeless dialog box must be removed from the tracking system by calling *
  149. * Remove_Modeless_Dialog. Failure to do so when the dialog is destroyed will *
  150. * result in undefined behavior. *
  151. * *
  152. * HISTORY: *
  153. * 05/17/1997 JLB : Created. *
  154. *=============================================================================================*/
  155. void Add_Modeless_Dialog(HWND dialog)
  156. {
  157. _ModelessDialogs.Add(dialog);
  158. }
  159. /***********************************************************************************************
  160. * Remove_Modeless_Dialog -- Removes the dialog box from the message tracking handler. *
  161. * *
  162. * This routine must be called when a modeless dialog is being removed. *
  163. * *
  164. * INPUT: dialog -- Handle to the modeless dialog that was previously submitted to *
  165. * Add_Modeless_Dialog(). *
  166. * *
  167. * OUTPUT: none *
  168. * *
  169. * WARNINGS: Failure to call this routine will result in undefined behavior when the dialog *
  170. * is destroyed. *
  171. * *
  172. * HISTORY: *
  173. * 05/17/1997 JLB : Created. *
  174. *=============================================================================================*/
  175. void Remove_Modeless_Dialog(HWND dialog)
  176. {
  177. _ModelessDialogs.Delete(dialog);
  178. }
  179. /***********************************************************************************************
  180. * Add_Accelerator -- Adds a keyboard accelerator to the message handler. *
  181. * *
  182. * This routine will add a keyboard accelerator to the tracking process for the message *
  183. * handler. If the incoming message is processed by an accelerator, then the normal *
  184. * processing must be altered. By using this routine, the proper behavior of accelerators *
  185. * is maintained. *
  186. * *
  187. * INPUT: window -- The window that the accelerator belongs to. Each accelerator must be *
  188. * assigned to a window. *
  189. * *
  190. * accelerator -- The handler to the windows accelerator. *
  191. * *
  192. * OUTPUT: none *
  193. * *
  194. * WARNINGS: When the accelerator is no longer valid (or the controlling window as been *
  195. * destroyed), the Remove_Accelerator function must be called. *
  196. * *
  197. * HISTORY: *
  198. * 05/17/1997 JLB : Created. *
  199. *=============================================================================================*/
  200. void Add_Accelerator(HWND window, HACCEL accelerator)
  201. {
  202. _Accelerators.Add(AcceleratorTracker(window, accelerator));
  203. }
  204. /***********************************************************************************************
  205. * Remove_Accelerator -- Removes an accelerator from the message processor. *
  206. * *
  207. * This routine must be called when the accelerator or the window it was attached to has *
  208. * been destroyed. *
  209. * *
  210. * INPUT: accelerator -- The accelerator to remove from the tracking system. *
  211. * *
  212. * OUTPUT: none *
  213. * *
  214. * WARNINGS: This routine presumes that the accelerator will not be shared between windows. *
  215. * *
  216. * HISTORY: *
  217. * 05/17/1997 JLB : Created. *
  218. *=============================================================================================*/
  219. void Remove_Accelerator(HACCEL accelerator)
  220. {
  221. for (int index = 0; index < _Accelerators.Count(); index++) {
  222. if (_Accelerators[index].Accelerator == accelerator) {
  223. _Accelerators.Delete(index);
  224. break;
  225. }
  226. }
  227. }