EDIT.CPP 23 KB

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