TOGGLE.CPP 11 KB

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