EDIT.CPP 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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: /CounterStrike/EDIT.CPP 1 3/03/97 10:24a 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 : EDIT.CPP *
  22. * *
  23. * Programmer : Joe L. Bostic, Maria del Mar McCready Legg *
  24. * *
  25. * Start Date : 01/15/95 *
  26. * *
  27. * Last Update : June 25, 1995 [JLB] *
  28. * *
  29. *---------------------------------------------------------------------------------------------*
  30. * Functions: *
  31. * EditClass::Action -- Handles input events. *
  32. * EditClass::Draw_Background -- Draw the background to the edit gadget. *
  33. * EditClass::Draw_Me -- Draws the edit box and embedded text. *
  34. * EditClass::Draw_Text -- Draws the edit gadget text. *
  35. * EditClass::EditClass -- Normal constructor for edit class object. *
  36. * EditClass::Handle_Key -- Handles keyboard input to edit gadget. *
  37. * EditClass::Set_Text -- Sets the text to the edit gadget. *
  38. * EditClass::~EditClass -- Default destructor for the edit gadget. *
  39. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  40. #include "function.h"
  41. /***********************************************************************************************
  42. * EditClass::EditClass -- Normal constructor for edit class object. *
  43. * *
  44. * This is the normal constructor used to create an edit object. *
  45. * *
  46. * INPUT: id -- The ID number for this edit object. This is the ID number that will be *
  47. * returned by the Input() function when the <RETURN> key is pressed if this *
  48. * gadget has the keyboard input focus. *
  49. * *
  50. * text -- Reference to the text buffer that the edit gadget will modify as keyboard *
  51. * input is processed. The value that this buffer contains is the default *
  52. * text displayed. *
  53. * *
  54. * maxlen-- The maximum size of the text buffer specified. This length INCLUDES the *
  55. * trailing null character so a simple sizeof() function call can be used. *
  56. * *
  57. * flags -- These are the text print control flags. It is used to control how the *
  58. * text looks in the edit box. Use the normal TPF_??? flags. *
  59. * *
  60. * x,y -- The pixel coordinates of the upper left corner of the edit gadget area. *
  61. * *
  62. * w,h -- The pixel dimensions of the edit box. If either of these are no provided, *
  63. * or set to -1, then the dimension is determined from the string itself. *
  64. * *
  65. * sytle -- This style flag parameter control what kind of characters are allowed in *
  66. * the edit box. The initial string in the text buffer may contain illegal *
  67. * characters, but they are NOT removed regardless of this parameter. *
  68. * *
  69. * OUTPUT: none *
  70. * WARNINGS: none *
  71. * HISTORY: *
  72. * 01/05/1995 MML : Created. *
  73. * 01/21/1995 JLB : Modified. *
  74. *=============================================================================================*/
  75. EditClass::EditClass(int id, char * text, int max_len, TextPrintType flags, int x, int y, int w, int h, EditStyle style) :
  76. ControlClass (id, x, y, w, h, LEFTPRESS), String(text)
  77. {
  78. TextFlags = flags & ~(TPF_CENTER);
  79. EditFlags = style;
  80. Set_Text(text, max_len);
  81. Color = GadgetClass::Get_Color_Scheme();
  82. if (w == -1 || h == -1) {
  83. // PG_TO_FIX
  84. //Fancy_Text_Print(TXT_NONE, 0, 0, TBLACK, TBLACK, TextFlags);
  85. if (h == -1) {
  86. Height = FontHeight+1;
  87. }
  88. if (w == -1) {
  89. if (strlen(String) > 0) {
  90. Width = String_Pixel_Width(String) + 6;
  91. } else {
  92. Width = ((Char_Pixel_Width('X')+FontXSpacing) * (MaxLength+1)) + 2;
  93. }
  94. }
  95. }
  96. IsReadOnly = 0;
  97. }
  98. /***********************************************************************************************
  99. * EditClass::~EditClass -- Default destructor for the edit gadget. *
  100. * *
  101. * This default destructor removes the focus setting if it currently has it. *
  102. * *
  103. * INPUT: none *
  104. * *
  105. * OUTPUT: none *
  106. * *
  107. * WARNINGS: none *
  108. * *
  109. * HISTORY: *
  110. * 01/24/1995 JLB : Created. *
  111. *=============================================================================================*/
  112. EditClass::~EditClass(void)
  113. {
  114. if (Has_Focus()) {
  115. Clear_Focus();
  116. }
  117. }
  118. /***********************************************************************************************
  119. * EditClass::Set_Text -- Sets the text to the edit gadget. *
  120. * *
  121. * Use this routine to change the text that this edit gadget refers to. *
  122. * *
  123. * INPUT: text -- Reference to the character array that this edit gadget will be *
  124. * modifying. *
  125. * max_len -- The maximum size of the buffer that will be modified. *
  126. * *
  127. * OUTPUT: none *
  128. * WARNINGS: none *
  129. * HISTORY: *
  130. * 01/21/1995 JLB : Created. *
  131. *=============================================================================================*/
  132. void EditClass::Set_Text(char * text, int max_len)
  133. {
  134. String = text;
  135. MaxLength = max_len-1;
  136. Length = strlen(String);
  137. Flag_To_Redraw();
  138. }
  139. /***********************************************************************************************
  140. * EditClass::Draw_Me -- Draws the edit box and embedded text. *
  141. * *
  142. * This routine will render the edit box. This will show the box outline as well as any *
  143. * text it may contain. *
  144. * *
  145. * INPUT: forced -- Should the edit box be drawn even if it thinks it doesn't have to? *
  146. * *
  147. * OUTPUT: Was the edit box drawn? *
  148. * *
  149. * WARNINGS: none *
  150. * *
  151. * HISTORY: *
  152. * 06/25/1995 JLB : Created. *
  153. *=============================================================================================*/
  154. int EditClass::Draw_Me(int forced)
  155. {
  156. if (ControlClass::Draw_Me(forced)) {
  157. /*
  158. ** Hide the mouse.
  159. */
  160. if (LogicPage == &SeenBuff) {
  161. Conditional_Hide_Mouse(X, Y, X+Width, Y+Height);
  162. }
  163. /*
  164. ** Draw the body & set text color.
  165. */
  166. Draw_Background();
  167. /*
  168. ** Display the text.
  169. */
  170. Draw_Text(String);
  171. /*
  172. ** Display the mouse.
  173. */
  174. if (LogicPage == &SeenBuff) {
  175. Conditional_Show_Mouse();
  176. }
  177. return(true);
  178. }
  179. return(false);
  180. }
  181. /***********************************************************************************************
  182. * EditClass::Action -- Handles input events. *
  183. * *
  184. * This routine will handle all mouse and keyboard events directed at this edit box *
  185. * gadget. For keyboard events, this will insert the characters into the edit box. *
  186. * *
  187. * INPUT: flags -- The event flag that triggered this function call. *
  188. * *
  189. * key -- Reference to the keyboard/mouse event that triggered this function call. *
  190. * *
  191. * OUTPUT: Should the list be processed further? *
  192. * *
  193. * WARNINGS: none *
  194. * *
  195. * HISTORY: *
  196. * 06/25/1995 JLB : Created. *
  197. *=============================================================================================*/
  198. int EditClass::Action(unsigned flags, KeyNumType & key)
  199. {
  200. /*
  201. ** If this is a read-only edit box, it's a display-only device
  202. */
  203. if (IsReadOnly) {
  204. return(false);
  205. }
  206. /*
  207. ** If the left mouse button is pressed over this gadget, then set the focus to
  208. ** this gadget. The event flag is cleared so that no button ID number is returned.
  209. */
  210. if ((flags & LEFTPRESS)) {
  211. flags &= ~LEFTPRESS;
  212. Set_Focus();
  213. Flag_To_Redraw(); // force to draw cursor
  214. }
  215. /*
  216. ** Handle keyboard events here. Normally, the key is added to the string, but if the
  217. ** RETURN key is pressed, then the button ID number is returned from the Input()
  218. ** function.
  219. */
  220. if ((flags & KEYBOARD) && Has_Focus()) {
  221. /*
  222. ** Process the keyboard character. If indicated, consume this keyboard event
  223. ** so that the edit gadget ID number is not returned.
  224. */
  225. if (key == KN_ESC) {
  226. Clear_Focus();
  227. flags = 0;
  228. } else {
  229. #ifdef WIN32
  230. KeyASCIIType ascii = (KeyASCIIType)(Keyboard->To_ASCII(key) & 0xff);
  231. /*
  232. ** Allow numeric keypad presses to map to ascii numbers
  233. */
  234. if ((key & WWKEY_VK_BIT) && ascii >='0' && ascii <= '9') {
  235. key = (KeyNumType)(key & ~WWKEY_VK_BIT);
  236. if ( (!(flags & LEFTRELEASE)) && (!(flags & RIGHTRELEASE))) {
  237. if (Handle_Key (ascii) ) {
  238. flags &= ~KEYBOARD;
  239. key = KN_NONE;
  240. }
  241. }
  242. } else {
  243. /*
  244. ** Filter out all special keys except return and backspace
  245. */ if ((!(key & WWKEY_VK_BIT) && ascii >= ' ' && ascii <= 255)
  246. || key == KN_RETURN || key == KN_BACKSPACE) {
  247. if ((!(flags & LEFTRELEASE)) && (!(flags & RIGHTRELEASE))) {
  248. if (Handle_Key(Keyboard->To_ASCII(key))) {
  249. flags &= ~KEYBOARD;
  250. key = KN_NONE;
  251. }
  252. }
  253. } else {
  254. flags &= ~KEYBOARD;
  255. key = KN_NONE;
  256. }
  257. }
  258. }
  259. #else //WIN32
  260. if (Handle_Key(Keyboard->To_ASCII(key))) {
  261. flags &= ~KEYBOARD;
  262. key = KN_NONE;
  263. }
  264. }
  265. #endif //WIN32
  266. }
  267. return(ControlClass::Action(flags, key));
  268. }
  269. /***********************************************************************************************
  270. * EditClass::Draw_Background -- Draw the background to the edit gadget. *
  271. * *
  272. * This routine will redraw the edit gadget background. The overlaying text is handled by *
  273. * a different routine. The mouse is guaranteed to be hidden when this routine is called. *
  274. * *
  275. * INPUT: none *
  276. * *
  277. * OUTPUT: none *
  278. * *
  279. * WARNINGS: none *
  280. * *
  281. * HISTORY: *
  282. * 01/21/1995 JLB : Created. *
  283. *=============================================================================================*/
  284. void EditClass::Draw_Background(void)
  285. {
  286. Draw_Box (X, Y, Width, Height, BOXSTYLE_BOX, true);
  287. }
  288. /***********************************************************************************************
  289. * EditClass::Draw_Text -- Draws the edit gadget text. *
  290. * *
  291. * This routine is called when the edit gadget text needs to be drawn. The background has *
  292. * already been drawn by the time this function is called. The mouse is guaranteed to be *
  293. * hidden as well. *
  294. * *
  295. * INPUT: text -- The text to draw in the edit gadget. *
  296. * *
  297. * OUTPUT: none *
  298. * *
  299. * WARNINGS: none *
  300. * *
  301. * HISTORY: *
  302. * 01/21/1995 JLB : Created. *
  303. *=============================================================================================*/
  304. void EditClass::Draw_Text(char const * text)
  305. {
  306. TextPrintType flags;
  307. if (Has_Focus()) {
  308. flags = TPF_BRIGHT_COLOR;
  309. } else {
  310. flags = (TextPrintType)0;
  311. }
  312. Conquer_Clip_Text_Print(text, X+1, Y+1, Color, TBLACK, TextFlags | flags, Width-2);
  313. if (Has_Focus() && (int)strlen(text) < MaxLength &&
  314. ((int)String_Pixel_Width(text) + (int)String_Pixel_Width ("_") < Width-2) ) {
  315. Conquer_Clip_Text_Print( "_", X+1+String_Pixel_Width(text), Y+1, Color, TBLACK, TextFlags | flags);
  316. }
  317. }
  318. /***********************************************************************************************
  319. * EditClass::Handle_Key -- Handles keyboard input to edit gadget. *
  320. * *
  321. * This is the gruntwork routine that processes keyboard input to the edit gadget. This *
  322. * routine will be called when keyboard input has been detected and this gadget has the *
  323. * current focus. *
  324. * *
  325. * INPUT: ascii -- The ASCII key code that was fetched from the keyboard buffer. *
  326. * *
  327. * OUTPUT: bool; Should this keyboard input NOT cause the gadget ID number to be returned *
  328. * from the controlling Input() routine? Typically, the return value would be *
  329. * true unless the focus is lost due to the <RETURN> key being pressed. *
  330. * *
  331. * WARNINGS: none *
  332. * HISTORY: *
  333. * 01/21/1995 JLB : Created. *
  334. *=============================================================================================*/
  335. bool EditClass::Handle_Key(KeyASCIIType ascii)
  336. {
  337. switch (ascii) {
  338. /*
  339. ** Handle the special case of a non-keyboard event. It is possible that this
  340. ** key code might be passed to this routine if this routine has been overridden
  341. ** and the key event was consumed.
  342. */
  343. case 0:
  344. break;
  345. /*
  346. ** If the return key is pressed, then remove the focus from this edit
  347. ** gadget but otherwise let the normal gadget processing proceed. This
  348. ** causes the gadget ID number to be returned from the Input() function
  349. ** so that the controlling program will know that the text can be
  350. ** processed.
  351. */
  352. case KA_RETURN:
  353. Clear_Focus();
  354. return(false);
  355. /*
  356. ** When the BACKSPACE key is pressed, remove the last character in the edit string.
  357. */
  358. case KA_BACKSPACE:
  359. if (Length) {
  360. Length--;
  361. String[Length] = '\0';
  362. Flag_To_Redraw();
  363. }
  364. break;
  365. /*
  366. ** If the keyboard event was not a recognized special key, then examine to see
  367. ** if it can legally be added to the edit string and do so if possible.
  368. */
  369. default:
  370. /*
  371. ** Don't add a character if the length is greater than edit width.
  372. */
  373. if (((int)String_Pixel_Width(String) + (int)Char_Pixel_Width(ascii) ) >= (Width-2)) {
  374. break;
  375. }
  376. /*
  377. ** Don't add a character if the length is already at maximum.
  378. */
  379. if (Length >= MaxLength) break;
  380. /*
  381. ** Invisible characters are never added to the string. This is
  382. ** especially true for spaces at the beginning of the string.
  383. */
  384. if (!isgraph(ascii) && ascii != ' ') break;
  385. if (ascii == ' ' && Length == 0) break;
  386. /*
  387. ** If this is an upper case only edit gadget, then force the alphabetic
  388. ** character to upper case.
  389. */
  390. if ((EditFlags & UPPERCASE) && isalpha(ascii)) {
  391. ascii = (KeyASCIIType)toupper(ascii);
  392. }
  393. if ((!(EditFlags & NUMERIC) || !isdigit(ascii)) &&
  394. (!(EditFlags & ALPHA) || !isalpha(ascii)) &&
  395. (!(EditFlags & MISC) || isalnum(ascii)) &&
  396. ascii != ' ') {
  397. break;
  398. }
  399. /*
  400. ** The character passed all legality checks, so add it to the edit string
  401. ** and flag this gadget to be redrawn. The manual flag to redraw is needed
  402. ** because the event flag has been cleared. This prevents the gadget's ID
  403. ** number from being returned just because the gadget has been edited.
  404. */
  405. String[Length++] = ascii;
  406. String[Length] = '\0';
  407. Flag_To_Redraw();
  408. break;
  409. }
  410. return(true);
  411. }
  412. void EditClass::Set_Focus(void)
  413. {
  414. Length = 0;
  415. if (String) {
  416. Length = strlen(String);
  417. }
  418. ControlClass::Set_Focus();
  419. }