TOGGLE.CPP 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //
  2. // Copyright 2020 Electronic Arts Inc.
  3. //
  4. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is free
  5. // software: you can redistribute it and/or modify it under the terms of
  6. // the GNU General Public License as published by the Free Software Foundation,
  7. // either version 3 of the License, or (at your option) any later version.
  8. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is distributed
  9. // in the hope that it will be useful, but with permitted additional restrictions
  10. // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT
  11. // distributed with this program. You should have received a copy of the
  12. // GNU General Public License along with permitted additional restrictions
  13. // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
  14. /* $Header: F:\projects\c&c\vcs\code\toggle.cpv 2.18 16 Oct 1995 16:50:56 JOE_BOSTIC $ */
  15. /***********************************************************************************************
  16. *** 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 ***
  17. ***********************************************************************************************
  18. * *
  19. * Project Name : Command & Conquer *
  20. * *
  21. * File Name : TOGGLE.CPP *
  22. * *
  23. * Programmer : Joe L. Bostic *
  24. * *
  25. * Start Date : 01/15/95 *
  26. * *
  27. * Last Update : February 2, 1995 [JLB] *
  28. * *
  29. *---------------------------------------------------------------------------------------------*
  30. * Functions: *
  31. * ToggleClass::ToggleClass -- Normal constructor for toggle button gadgets. *
  32. * ToggleClass::Turn_Off -- Turns the toggle button to the "OFF" state. *
  33. * ToggleClass::Turn_On -- Turns the toggle button to the "ON" state. *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "function.h"
  36. #include "toggle.h"
  37. /***********************************************************************************************
  38. * ToggleClass::ToggleClass -- Normal constructor for toggle button gadgets. *
  39. * *
  40. * This is the normal constructor for toggle buttons. A toggle button is one that most *
  41. * closesly resembles the Windows style. It has an up and down state as well as an on *
  42. * and off state. *
  43. * *
  44. * INPUT: id -- ID number for this button. *
  45. * *
  46. * x,y -- Pixel coordinate of upper left corner of this button. *
  47. * *
  48. * w,h -- Width and height of the button. *
  49. * *
  50. * OUTPUT: none *
  51. * *
  52. * WARNINGS: none *
  53. * *
  54. * HISTORY: *
  55. * 01/15/1995 JLB : Created. *
  56. *=============================================================================================*/
  57. ToggleClass::ToggleClass(unsigned id, int x, int y, int w, int h)
  58. : ControlClass(id, x, y, w, h, LEFTPRESS|LEFTRELEASE, true)
  59. {
  60. IsPressed = false;
  61. IsOn = false;
  62. IsToggleType = false;
  63. }
  64. /***********************************************************************************************
  65. * ToggleClass::Turn_On -- Turns the toggle button to the "ON" state. *
  66. * *
  67. * This routine will turn the button on. The button will also be flagged to be redrawn *
  68. * at the next opportunity. *
  69. * *
  70. * INPUT: none *
  71. * *
  72. * OUTPUT: none *
  73. * *
  74. * WARNINGS: none *
  75. * *
  76. * HISTORY: *
  77. * 01/15/1995 JLB : Created. *
  78. *=============================================================================================*/
  79. void ToggleClass::Turn_On(void)
  80. {
  81. IsOn = true;
  82. Flag_To_Redraw();
  83. }
  84. /***********************************************************************************************
  85. * ToggleClass::Turn_Off -- Turns the toggle button to the "OFF" state. *
  86. * *
  87. * This routine will turn the toggle button "off". It will also be flagged to be redrawn *
  88. * at the next opportunity. *
  89. * *
  90. * INPUT: none *
  91. * *
  92. * OUTPUT: none *
  93. * *
  94. * WARNINGS: none *
  95. * *
  96. * HISTORY: *
  97. * 01/15/1995 JLB : Created. *
  98. *=============================================================================================*/
  99. void ToggleClass::Turn_Off(void)
  100. {
  101. IsOn = false;
  102. Flag_To_Redraw();
  103. }
  104. /***********************************************************************************************
  105. * ToggleClass::Action -- Handles mouse clicks on a text button. *
  106. * *
  107. * This routine will process any mouse or keyboard event that is associated with this *
  108. * button object. It detects and flags the text button so that it will properly be drawn *
  109. * in a pressed or raised state. It also handles any toggle state for the button. *
  110. * *
  111. * INPUT: flags -- The event flags that triggered this button. *
  112. * *
  113. * key -- The keyboard code associated with this event. Usually this is KN_LMOUSE *
  114. * or similar, but it could be a regular key if this text button is given *
  115. * a hotkey. *
  116. * *
  117. * OUTPUT: Returns whatever the lower level processing for buttons decides. This is usually *
  118. * true. *
  119. * *
  120. * WARNINGS: The button is flagged to be redrawn by this routine. *
  121. * *
  122. * HISTORY: *
  123. * 01/14/1995 JLB : Created. *
  124. * 02/02/1995 JLB : Left press doesn't get passed to other buttons now *
  125. *=============================================================================================*/
  126. int ToggleClass::Action(unsigned flags, KeyNumType &key)
  127. {
  128. /*
  129. ** If there are no action flag bits set, then this must be a forced call. A forced call
  130. ** must never actually function like a real call, but rather only performs any necessary
  131. ** graphic updating.
  132. */
  133. if (!flags) {
  134. if ((unsigned)(Get_Mouse_X() - X) < (unsigned)Width && (unsigned)(Get_Mouse_Y() - Y) < (unsigned)Height ) {
  135. if (!IsPressed) {
  136. IsPressed = true;
  137. Flag_To_Redraw();
  138. }
  139. } else {
  140. if (IsPressed) {
  141. IsPressed = false;
  142. Flag_To_Redraw();
  143. }
  144. }
  145. }
  146. /*
  147. ** Handle the sticky state for this gadget. It must be processed here
  148. ** because the event flags might be cleared before the action function
  149. ** is called.
  150. */
  151. Sticky_Process(flags);
  152. /*
  153. ** Flag the button to show the pressed down imagery if this mouse button
  154. ** was pressed over this gadget.
  155. */
  156. if (flags & LEFTPRESS) {
  157. IsPressed = true;
  158. Flag_To_Redraw();
  159. flags &= ~LEFTPRESS;
  160. ControlClass::Action(flags, key);
  161. key = KN_NONE; // erase the event
  162. return(true); // stop processing other buttons now
  163. }
  164. if (flags & LEFTRELEASE) {
  165. if (IsPressed) {
  166. if (IsToggleType) {
  167. IsOn = (IsOn == false);
  168. }
  169. IsPressed = false;
  170. } else {
  171. flags &= ~LEFTRELEASE;
  172. }
  173. }
  174. /*
  175. ** Do normal button processing. This ends up causing the button's ID number to
  176. ** be returned from the controlling Input() function.
  177. */
  178. return(ControlClass::Action(flags, key));
  179. }