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